aboutsummaryrefslogtreecommitdiffstats
path: root/a11y/calendar/ea-day-view.c
blob: b85b40087f211deec86f84f5dc5e54e0f957324f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* vim:expandtab:shiftwidth=8:tabstop=8:
 */
/* Evolution Accessibility: ea-day-view.c
 *
 * Copyright (C) 2003 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Bolian Yin <bolian.yin@sun.com> Sun Microsystem Inc., 2003
 *
 */

#include "ea-day-view.h"
#include "ea-cal-view-event.h"

#include "ea-calendar-helpers.h"
#include "ea-gnome-calendar.h"
#include "calendar-commands.h"
#include <glib/gstrfuncs.h>
#include <libgnome/gnome-i18n.h>

static void ea_day_view_class_init (EaDayViewClass *klass);

static G_CONST_RETURN gchar* ea_day_view_get_name (AtkObject *accessible);
static G_CONST_RETURN gchar* ea_day_view_get_description (AtkObject *accessible);
static gint         ea_day_view_get_n_children      (AtkObject *obj);
static AtkObject*   ea_day_view_ref_child           (AtkObject *obj,
                                                     gint i);
static gpointer parent_class = NULL;

GType
ea_day_view_get_type (void)
{
    static GType type = 0;
    AtkObjectFactory *factory;
    GTypeQuery query;
    GType derived_atk_type;

    if (!type) {
        static GTypeInfo tinfo = {
            sizeof (EaDayViewClass),
            (GBaseInitFunc) NULL, /* base init */
            (GBaseFinalizeFunc) NULL, /* base finalize */
            (GClassInitFunc) ea_day_view_class_init, /* class init */
            (GClassFinalizeFunc) NULL, /* class finalize */
            NULL, /* class data */
            sizeof (EaDayView), /* instance size */
            0, /* nb preallocs */
            (GInstanceInitFunc) NULL, /* instance init */
            NULL /* value table */
        };

        /*
         * Figure out the size of the class and instance
         * we are run-time deriving from (EaCalView, in this case)
         *
         * Note: we must still use run-time deriving here, because
         * our parent class EaCalView is run-time deriving.
         */

        factory = atk_registry_get_factory (atk_get_default_registry (),
                            e_calendar_view_get_type());
        derived_atk_type = atk_object_factory_get_accessible_type (factory);
        g_type_query (derived_atk_type, &query);

        tinfo.class_size = query.class_size;
        tinfo.instance_size = query.instance_size;

        type = g_type_register_static (derived_atk_type,
                           "EaDayView", &tinfo, 0);
    }

    return type;
}

static void
ea_day_view_class_init (EaDayViewClass *klass)
{
    AtkObjectClass *class = ATK_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    class->get_name = ea_day_view_get_name;
    class->get_description = ea_day_view_get_description;

    class->get_n_children = ea_day_view_get_n_children;
    class->ref_child = ea_day_view_ref_child;
}

AtkObject* 
ea_day_view_new (GtkWidget *widget)
{
    GObject *object;
    AtkObject *accessible;

    g_return_val_if_fail (E_IS_DAY_VIEW (widget), NULL);

    object = g_object_new (EA_TYPE_DAY_VIEW, NULL);

    accessible = ATK_OBJECT (object);
    atk_object_initialize (accessible, widget);

#ifdef ACC_DEBUG
    printf ("EvoAcc: ea_day_view created %p\n", (void *)accessible);
#endif

    return accessible;
}

static G_CONST_RETURN gchar*
ea_day_view_get_name (AtkObject *accessible)
{
    EDayView *day_view;
    GnomeCalendar *gcal;
    const gchar *label_text;
    GnomeCalendarViewType view_type;
    gint n_events;
    gchar *event_str, *name_str;

    g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), NULL);

    if (!GTK_ACCESSIBLE (accessible)->widget)
        return NULL;

    day_view = E_DAY_VIEW (GTK_ACCESSIBLE (accessible)->widget);
    gcal = e_calendar_view_get_calendar (E_CALENDAR_VIEW (day_view));
    if (!GTK_WIDGET_VISIBLE (GTK_WIDGET (gcal)))
        return NULL;

    label_text = ea_gnome_calendar_get_label_description (gcal);

    n_events = atk_object_get_n_accessible_children (accessible);
    /* the child main item is always there */
    --n_events;
    if (n_events >= 1)
        event_str = g_strdup_printf (ngettext ("It has %d event.", "It has %d events.", n_events), n_events);
    else
        event_str = g_strdup (_("It has no events."));

    view_type = gnome_calendar_get_view (gcal);
    if (view_type == GNOME_CAL_WORK_WEEK_VIEW)
        name_str = g_strdup_printf (_("Work Week View: %s. %s"),
                        label_text, event_str);
    else
        name_str = g_strdup_printf (_("Day View: %s. %s"),
                        label_text, event_str);

    ATK_OBJECT_CLASS (parent_class)->set_name (accessible, name_str);
    g_free (name_str);
    g_free (event_str);

    return accessible->name;
}

static G_CONST_RETURN gchar*
ea_day_view_get_description (AtkObject *accessible)
{
    EDayView *day_view;

    g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), NULL);

    if (!GTK_ACCESSIBLE (accessible)->widget)
        return NULL;
    day_view = E_DAY_VIEW (GTK_ACCESSIBLE (accessible)->widget);

    if (accessible->description)
        return accessible->description;
    else {
        GnomeCalendar *gcal;
        GnomeCalendarViewType view_type;

        gcal = e_calendar_view_get_calendar (E_CALENDAR_VIEW (day_view));
        view_type = gnome_calendar_get_view (gcal);

        if (view_type == GNOME_CAL_WORK_WEEK_VIEW)
            return _("calendar view for a work week");
        else
            return _("calendar view for one or more days");
    }
}

static gint
ea_day_view_get_n_children (AtkObject *accessible)
{
    EDayView *day_view;
    gint day;
    gint child_num = 0;

    g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), -1);

    if (!GTK_ACCESSIBLE (accessible)->widget)
        return -1;

    day_view = E_DAY_VIEW (GTK_ACCESSIBLE (accessible)->widget);

    child_num += day_view->long_events->len;

    for (day = 0; day < day_view->days_shown; day++) {
        child_num += day_view->events[day]->len;
    }

    /* "+1" for the main item */
    return child_num + 1;
}

static AtkObject *
ea_day_view_ref_child (AtkObject *accessible, gint index)
{
    EDayView *day_view;
    gint child_num;
    gint day;
    AtkObject *atk_object = NULL;
    EDayViewEvent *event = NULL;

    g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), NULL);

    child_num = atk_object_get_n_accessible_children (accessible);
    if (child_num <= 0 || index < 0 || index >= child_num)
        return NULL;

    if (!GTK_ACCESSIBLE (accessible)->widget)
        return NULL;
    day_view = E_DAY_VIEW (GTK_ACCESSIBLE (accessible)->widget);

    if (index == 0) {
        /* index == 0 is the main item */
        atk_object = atk_gobject_accessible_for_object (G_OBJECT (day_view->main_canvas_item));
        g_object_ref (atk_object);
    }
    else {
        --index;
        /* a long event */
        if (index < day_view->long_events->len) {
            event = &g_array_index (day_view->long_events,
                        EDayViewEvent, index);
        }
        else {
            index -= day_view->long_events->len;
            day = 0;
            while (index >= day_view->events[day]->len) {
                index -= day_view->events[day]->len;
                ++day;
            }

            event = &g_array_index (day_view->events[day],
                        EDayViewEvent, index);
        }
        if (event && event->canvas_item) {
            /* Not use atk_gobject_accessible_for_object here,
             * we need to do special thing here
             */
            atk_object = ea_calendar_helpers_get_accessible_for (event->canvas_item);
            g_object_ref (atk_object);
        }
    }
    return atk_object;
}