aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/ea-calendar-cell.c
blob: a9784df31a3ba5cfcfa22d607bbe52e76554df67 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Bolian Yin <bolian.yin@sun.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include "ea-calendar-cell.h"
#include "ea-calendar-item.h"
#include "ea-factory.h"

/* ECalendarCell */

static void e_calendar_cell_class_init (ECalendarCellClass *class);

EA_FACTORY_GOBJECT (EA_TYPE_CALENDAR_CELL, ea_calendar_cell, ea_calendar_cell_new)

GType
e_calendar_cell_get_type (void)
{
    static GType type = 0;

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

        type = g_type_register_static (
            G_TYPE_OBJECT,
            "ECalendarCell", &tinfo, 0);
    }

    return type;
}

static void
e_calendar_cell_class_init (ECalendarCellClass *class)
{
    EA_SET_FACTORY (e_calendar_cell_get_type (), ea_calendar_cell);
}

ECalendarCell *
e_calendar_cell_new (ECalendarItem *calitem,
                     gint row,
                     gint column)
{
    GObject *object;
    ECalendarCell *cell;

    g_return_val_if_fail (E_IS_CALENDAR_ITEM (calitem), NULL);

    object = g_object_new (E_TYPE_CALENDAR_CELL, NULL);
    cell = E_CALENDAR_CELL (object);
    cell->calitem = calitem;
    cell->row = row;
    cell->column = column;

#ifdef ACC_DEBUG
    g_print ("EvoAcc: e_calendar_cell created %p\n", (gpointer) cell);
#endif

    return cell;
}

/* EaCalendarCell */

static void ea_calendar_cell_class_init (EaCalendarCellClass *klass);
static void ea_calendar_cell_init (EaCalendarCell *a11y);

static const gchar * ea_calendar_cell_get_name (AtkObject *accessible);
static const gchar * ea_calendar_cell_get_description (AtkObject *accessible);
static AtkObject * ea_calendar_cell_get_parent (AtkObject *accessible);
static gint ea_calendar_cell_get_index_in_parent (AtkObject *accessible);
static AtkStateSet *ea_calendar_cell_ref_state_set (AtkObject *accessible);

/* component interface */
static void atk_component_interface_init (AtkComponentIface *iface);
static void component_interface_get_extents (AtkComponent *component,
                         gint *x, gint *y,
                         gint *width, gint *height,
                         AtkCoordType coord_type);
static gboolean component_interface_grab_focus (AtkComponent *component);

static gpointer parent_class = NULL;

#ifdef ACC_DEBUG
static gint n_ea_calendar_cell_created = 0, n_ea_calendar_cell_destroyed = 0;
static void ea_calendar_cell_finalize (GObject *object);
#endif

GType
ea_calendar_cell_get_type (void)
{
    static GType type = 0;

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

        static const GInterfaceInfo atk_component_info = {
            (GInterfaceInitFunc) atk_component_interface_init,
            (GInterfaceFinalizeFunc) NULL,
            NULL
        };

        type = g_type_register_static (
            ATK_TYPE_GOBJECT_ACCESSIBLE,
            "EaCalendarCell", &tinfo, 0);
        g_type_add_interface_static (
            type, ATK_TYPE_COMPONENT,
            &atk_component_info);
    }

    return type;
}

static void
ea_calendar_cell_class_init (EaCalendarCellClass *klass)
{
    AtkObjectClass *class = ATK_OBJECT_CLASS (klass);

#ifdef ACC_DEBUG
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    gobject_class->finalize = ea_calendar_cell_finalize;
#endif

    parent_class = g_type_class_peek_parent (klass);

    class->get_name = ea_calendar_cell_get_name;
    class->get_description = ea_calendar_cell_get_description;

    class->get_parent = ea_calendar_cell_get_parent;
    class->get_index_in_parent = ea_calendar_cell_get_index_in_parent;
    class->ref_state_set = ea_calendar_cell_ref_state_set;
}

static void
ea_calendar_cell_init (EaCalendarCell *a11y)
{
    a11y->state_set = atk_state_set_new ();
    atk_state_set_add_state (a11y->state_set, ATK_STATE_TRANSIENT);
    atk_state_set_add_state (a11y->state_set, ATK_STATE_ENABLED);
    atk_state_set_add_state (a11y->state_set, ATK_STATE_SENSITIVE);
    atk_state_set_add_state (a11y->state_set, ATK_STATE_SELECTABLE);
    atk_state_set_add_state (a11y->state_set, ATK_STATE_SHOWING);
    atk_state_set_add_state (a11y->state_set, ATK_STATE_FOCUSABLE);
}

AtkObject *
ea_calendar_cell_new (GObject *obj)
{
    gpointer object;
    AtkObject *atk_object;

    g_return_val_if_fail (E_IS_CALENDAR_CELL (obj), NULL);
    object = g_object_new (EA_TYPE_CALENDAR_CELL, NULL);
    atk_object = ATK_OBJECT (object);
    atk_object_initialize (atk_object, obj);
    atk_object->role = ATK_ROLE_TABLE_CELL;

#ifdef ACC_DEBUG
    ++n_ea_calendar_cell_created;
    g_print (
        "ACC_DEBUG: n_ea_calendar_cell_created = %d\n",
        n_ea_calendar_cell_created);
#endif
    return atk_object;
}

#ifdef ACC_DEBUG
static void ea_calendar_cell_finalize (GObject *object)
{
    G_OBJECT_CLASS (parent_class)->finalize (object);

    ++n_ea_calendar_cell_destroyed;
    g_print (
        "ACC_DEBUG: n_ea_calendar_cell_destroyed = %d\n",
        n_ea_calendar_cell_destroyed);
}
#endif

static const gchar *
ea_calendar_cell_get_name (AtkObject *accessible)
{
    GObject *g_obj;

    g_return_val_if_fail (EA_IS_CALENDAR_CELL (accessible), NULL);

    g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (accessible));
    if (!g_obj)
        /* defunct object*/
        return NULL;

    if (!accessible->name) {
        AtkObject *atk_obj;
        EaCalendarItem *ea_calitem;
        ECalendarCell *cell;
        gint day_index;
        gint year, month, day;
        gchar buffer[128];

        cell = E_CALENDAR_CELL (g_obj);
        atk_obj = ea_calendar_cell_get_parent (accessible);
        ea_calitem = EA_CALENDAR_ITEM (atk_obj);
        day_index = atk_table_get_index_at (
            ATK_TABLE (ea_calitem),
            cell->row, cell->column);
        e_calendar_item_get_date_for_offset (cell->calitem, day_index,
                             &year, &month, &day);

        g_snprintf (buffer, 128, "%d-%d-%d", year, month + 1, day);
        ATK_OBJECT_CLASS (parent_class)->set_name (accessible, buffer);
    }
    return accessible->name;
}

static const gchar *
ea_calendar_cell_get_description (AtkObject *accessible)
{
    return ea_calendar_cell_get_name (accessible);
}

static AtkObject *
ea_calendar_cell_get_parent (AtkObject *accessible)
{
    GObject *g_obj;
    ECalendarCell *cell;
    ECalendarItem *calitem;

    g_return_val_if_fail (EA_IS_CALENDAR_CELL (accessible), NULL);

    g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (accessible));
    if (!g_obj)
        /* defunct object*/
        return NULL;

    cell = E_CALENDAR_CELL (g_obj);
    calitem = cell->calitem;
    return atk_gobject_accessible_for_object (G_OBJECT (calitem));
}

static gint
ea_calendar_cell_get_index_in_parent (AtkObject *accessible)
{
    GObject *g_obj;
    ECalendarCell *cell;
    AtkObject *parent;

    g_return_val_if_fail (EA_IS_CALENDAR_CELL (accessible), -1);

    g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (accessible));
    if (!g_obj)
        return -1;
    cell = E_CALENDAR_CELL (g_obj);
    parent = atk_object_get_parent (accessible);
    return atk_table_get_index_at (
        ATK_TABLE (parent),
        cell->row, cell->column);
}

static AtkStateSet *
ea_calendar_cell_ref_state_set (AtkObject *accessible)
{
    EaCalendarCell *atk_cell = EA_CALENDAR_CELL (accessible);

    g_return_val_if_fail (atk_cell->state_set, NULL);

    g_object_ref (atk_cell->state_set);

    return atk_cell->state_set;

}

/* Atk Component Interface */

static void
atk_component_interface_init (AtkComponentIface *iface)
{
    g_return_if_fail (iface != NULL);

    iface->get_extents = component_interface_get_extents;
    iface->grab_focus  = component_interface_grab_focus;
}

static void
component_interface_get_extents (AtkComponent *component,
                                 gint *x,
                                 gint *y,
                                 gint *width,
                                 gint *height,
                                 AtkCoordType coord_type)
{
    GObject *g_obj;
    AtkObject *atk_obj, *atk_canvas;
    ECalendarCell *cell;
    ECalendarItem *calitem;
    EaCalendarItem *ea_calitem;
    gint day_index;
    gint year, month, day;
    gint canvas_x, canvas_y, canvas_width, canvas_height;

    *x = *y = *width = *height = 0;

    g_return_if_fail (EA_IS_CALENDAR_CELL (component));

    g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (component));
    if (!g_obj)
        /* defunct object*/
        return;

    cell = E_CALENDAR_CELL (g_obj);
    calitem = cell->calitem;
    atk_obj = atk_gobject_accessible_for_object (G_OBJECT (calitem));
    ea_calitem = EA_CALENDAR_ITEM (atk_obj);
    day_index = atk_table_get_index_at (
        ATK_TABLE (ea_calitem),
        cell->row, cell->column);
    e_calendar_item_get_date_for_offset (calitem, day_index,
                         &year, &month, &day);

    if (!e_calendar_item_get_day_extents (calitem,
                          year, month, day,
                          x, y, width, height))
        return;
    atk_canvas = atk_object_get_parent (ATK_OBJECT (ea_calitem));
    atk_component_get_extents (
        ATK_COMPONENT (atk_canvas),
        &canvas_x, &canvas_y,
        &canvas_width, &canvas_height,
        coord_type);
    *x += canvas_x;
    *y += canvas_y;
}

static gboolean
component_interface_grab_focus (AtkComponent *component)
{
    GObject *g_obj;
    GtkWidget *toplevel;
    AtkObject *ea_calitem;
    ECalendarItem *calitem;
    EaCalendarCell *a11y;
    gint index;

    a11y = EA_CALENDAR_CELL (component);
    ea_calitem = ea_calendar_cell_get_parent (ATK_OBJECT (a11y));

    g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (ea_calitem));
    calitem = E_CALENDAR_ITEM (g_obj);

    index = atk_object_get_index_in_parent (ATK_OBJECT (a11y));

    atk_selection_clear_selection (ATK_SELECTION (ea_calitem));
    atk_selection_add_selection (ATK_SELECTION (ea_calitem), index);

    gtk_widget_grab_focus (GTK_WIDGET (GNOME_CANVAS_ITEM (calitem)->canvas));
    toplevel = gtk_widget_get_toplevel (
        GTK_WIDGET (GNOME_CANVAS_ITEM (calitem)->canvas));
    if (toplevel && gtk_widget_is_toplevel (toplevel))
        gtk_window_present (GTK_WINDOW (toplevel));

    return TRUE;

}