No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * Bolian Yin <bolian.yin@sun.com>
18 *
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "ea-week-view.h"
28 #include "ea-cal-view-event.h"
29 #include "ea-calendar-helpers.h"
30 #include "ea-gnome-calendar.h"
31 #include <text/e-text.h>
32 #include <glib/gi18n.h>
33
34 static void ea_week_view_class_init (EaWeekViewClass *klass);
35
36 static const gchar * ea_week_view_get_name (AtkObject *accessible);
37 static const gchar * ea_week_view_get_description (AtkObject *accessible);
38 static gint ea_week_view_get_n_children (AtkObject *obj);
39 static AtkObject * ea_week_view_ref_child (AtkObject *obj,
40 gint i);
41
42 static gpointer parent_class = NULL;
43
44 GType
45 ea_week_view_get_type (void)
46 {
47 static GType type = 0;
48 AtkObjectFactory *factory;
49 GTypeQuery query;
50 GType derived_atk_type;
51
52 if (!type) {
53 static GTypeInfo tinfo = {
54 sizeof (EaWeekViewClass),
55 (GBaseInitFunc) NULL, /* base init */
56 (GBaseFinalizeFunc) NULL, /* base finalize */
57 (GClassInitFunc) ea_week_view_class_init, /* class init */
58 (GClassFinalizeFunc) NULL, /* class finalize */
59 NULL, /* class data */
60 sizeof (EaWeekView), /* instance size */
61 0, /* nb preallocs */
62 (GInstanceInitFunc) NULL, /* instance init */
63 NULL /* value table */
64 };
65
66 /*
67 * Figure out the size of the class and instance
68 * we are run-time deriving from (EaCalView, in this case)
69 *
70 * Note: we must still use run-time deriving here, because
71 * our parent class EaCalView is run-time deriving.
72 */
73
74 factory = atk_registry_get_factory (
75 atk_get_default_registry (),
76 e_calendar_view_get_type ());
77 derived_atk_type = atk_object_factory_get_accessible_type (factory);
78 g_type_query (derived_atk_type, &query);
79
80 tinfo.class_size = query.class_size;
81 tinfo.instance_size = query.instance_size;
82
83 type = g_type_register_static (
84 derived_atk_type,
85 "EaWeekView", &tinfo, 0);
86
87 }
88
89 return type;
90 }
91
92 static void
93 ea_week_view_class_init (EaWeekViewClass *klass)
94 {
95 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
96
97 parent_class = g_type_class_peek_parent (klass);
98
99 class->get_name = ea_week_view_get_name;
100 class->get_description = ea_week_view_get_description;
101
102 class->get_n_children = ea_week_view_get_n_children;
103 class->ref_child = ea_week_view_ref_child;
104 }
105
106 AtkObject *
107 ea_week_view_new (GtkWidget *widget)
108 {
109 GObject *object;
110 AtkObject *accessible;
111
112 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
113
114 object = g_object_new (EA_TYPE_WEEK_VIEW, NULL);
115
116 accessible = ATK_OBJECT (object);
117 atk_object_initialize (accessible, widget);
118
119 #ifdef ACC_DEBUG
120 printf ("EvoAcc: ea_week_view created %p\n", (gpointer) accessible);
121 #endif
122
123 return accessible;
124 }
125
126 static const gchar *
127 ea_week_view_get_name (AtkObject *accessible)
128 {
129 EWeekView *week_view;
130 GnomeCalendar *gcal;
131 const gchar *label_text;
132 GnomeCalendarViewType view_type;
133 GtkWidget *widget;
134 gint n_events;
135 gchar *event_str, *name_str;
136
137 g_return_val_if_fail (EA_IS_WEEK_VIEW (accessible), NULL);
138
139 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
140 if (widget == NULL)
141 return NULL;
142
143 week_view = E_WEEK_VIEW (widget);
144 gcal = e_calendar_view_get_calendar (E_CALENDAR_VIEW (week_view));
145
146 label_text = ea_gnome_calendar_get_label_description (gcal);
147
148 n_events = atk_object_get_n_accessible_children (accessible);
149 /* the child main item is always there */
150 --n_events;
151 if (n_events >= 1)
152 event_str = g_strdup_printf (
153 ngettext ("It has %d event.", "It has %d events.",
154 n_events), n_events);
155 else
156 event_str = g_strdup (_("It has no events."));
157
158 view_type = gnome_calendar_get_view (gcal);
159
160 if (view_type == GNOME_CAL_MONTH_VIEW)
161 name_str = g_strdup_printf (
162 _("Month View: %s. %s"),
163 label_text, event_str);
164
165 else
166 name_str = g_strdup_printf (
167 _("Week View: %s. %s"),
168 label_text, event_str);
169
170 ATK_OBJECT_CLASS (parent_class)->set_name (accessible, name_str);
171 g_free (name_str);
172 g_free (event_str);
173
174 return accessible->name;
175 }
176
177 static const gchar *
178 ea_week_view_get_description (AtkObject *accessible)
179 {
180 EWeekView *week_view;
181 GtkWidget *widget;
182
183 g_return_val_if_fail (EA_IS_WEEK_VIEW (accessible), NULL);
184
185 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
186 if (widget == NULL)
187 return NULL;
188
189 week_view = E_WEEK_VIEW (widget);
190
191 if (accessible->description)
192 return accessible->description;
193 else {
194 GnomeCalendar *gcal;
195 GnomeCalendarViewType view_type;
196
197 gcal = e_calendar_view_get_calendar (E_CALENDAR_VIEW (week_view));
198 view_type = gnome_calendar_get_view (gcal);
199
200 if (view_type == GNOME_CAL_MONTH_VIEW)
201 return _("calendar view for a month");
202 else
203 return _("calendar view for one or more weeks");
204 }
205 }
206
207 static gint
208 ea_week_view_get_n_children (AtkObject *accessible)
209 {
210 EWeekView *week_view;
211 GtkWidget *widget;
212 gint i, count = 0;
213 gint event_index;
214
215 g_return_val_if_fail (EA_IS_WEEK_VIEW (accessible), -1);
216
217 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
218 if (widget == NULL)
219 return -1;
220
221 week_view = E_WEEK_VIEW (widget);
222
223 for (event_index = 0; event_index < week_view->events->len;
224 ++event_index) {
225 EWeekViewEvent *event;
226 EWeekViewEventSpan *span;
227
228 /* If week_view->spans == NULL, there is no visible events. */
229 if (!week_view->spans)
230 break;
231
232 event = &g_array_index (week_view->events,
233 EWeekViewEvent, event_index);
234 if (!event)
235 continue;
236 span = &g_array_index (week_view->spans, EWeekViewEventSpan,
237 event->spans_index + 0);
238
239 if (!span)
240 continue;
241
242 /* at least one of the event spans is visible, count it */
243 if (span->text_item)
244 ++count;
245 }
246
247 /* add the number of visible jump buttons */
248 for (i = 0; i < E_WEEK_VIEW_MAX_WEEKS * 7; i++) {
249 if (week_view->jump_buttons[i]->flags & GNOME_CANVAS_ITEM_VISIBLE)
250 ++count;
251 }
252
253 /* "+1" for the main item */
254 count++;
255
256 #ifdef ACC_DEBUG
257 printf ("AccDebug: week view %p has %d children\n", (gpointer) week_view, count);
258 #endif
259 return count;
260 }
261
262 static AtkObject *
263 ea_week_view_ref_child (AtkObject *accessible,
264 gint index)
265 {
266 EWeekView *week_view;
267 gint child_num, max_count;
268 AtkObject *atk_object = NULL;
269 GtkWidget *widget;
270 gint event_index;
271 gint jump_button = -1;
272 gint span_num = 0;
273 gint count = 0;
274
275 g_return_val_if_fail (EA_IS_WEEK_VIEW (accessible), NULL);
276
277 child_num = atk_object_get_n_accessible_children (accessible);
278 if (child_num <= 0 || index < 0 || index >= child_num)
279 return NULL;
280
281 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
282 if (widget == NULL)
283 return NULL;
284
285 week_view = E_WEEK_VIEW (widget);
286 max_count = week_view->events->len;
287
288 if (index == 0) {
289 /* index == 0 is the main item */
290 atk_object = atk_gobject_accessible_for_object (
291 G_OBJECT (week_view->main_canvas_item));
292 g_object_ref (atk_object);
293 } else
294 for (event_index = 0; event_index < max_count; ++event_index) {
295 EWeekViewEvent *event;
296 EWeekViewEventSpan *span;
297 gint current_day;
298
299 event = &g_array_index (week_view->events,
300 EWeekViewEvent, event_index);
301 if (!event)
302 continue;
303
304 span = &g_array_index (week_view->spans, EWeekViewEventSpan,
305 event->spans_index + span_num);
306
307 if (!span)
308 continue;
309
310 current_day = span->start_day;
311 if (span->text_item)
312 ++count;
313 else if (current_day != jump_button) {
314 /* we should go to the jump button */
315 jump_button = current_day;
316 ++count;
317 }
318 else
319 continue;
320
321 if (count == index) {
322 if (span->text_item) {
323 /* Not use atk_gobject_accessible_for_object for event
324 * text_item we need to do special thing here
325 */
326 atk_object =
327 ea_calendar_helpers_get_accessible_for (
328 span->text_item);
329 }
330 else {
331 gint index;
332
333 index = (current_day != -1) ? current_day : 0;
334 atk_object =
335 ea_calendar_helpers_get_accessible_for (
336 week_view->jump_buttons[index]);
337 }
338 g_object_ref (atk_object);
339 break;
340 }
341 }
342
343 #ifdef ACC_DEBUG
344 printf (
345 "EvoAcc: ea_week_view_ref_child [%d]=%p\n",
346 index, (gpointer) atk_object);
347 #endif
348 return atk_object;
349 }