evolution-3.6.4/calendar/gui/ea-day-view.c

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-day-view.h"
 28 #include "ea-cal-view-event.h"
 29 
 30 #include "ea-calendar-helpers.h"
 31 #include "ea-gnome-calendar.h"
 32 #include <glib/gi18n.h>
 33 
 34 static void ea_day_view_class_init (EaDayViewClass *klass);
 35 
 36 static const gchar * ea_day_view_get_name (AtkObject *accessible);
 37 static const gchar * ea_day_view_get_description (AtkObject *accessible);
 38 static gint         ea_day_view_get_n_children      (AtkObject *obj);
 39 static AtkObject *   ea_day_view_ref_child           (AtkObject *obj,
 40                                                      gint i);
 41 static gpointer parent_class = NULL;
 42 
 43 GType
 44 ea_day_view_get_type (void)
 45 {
 46 	static GType type = 0;
 47 	AtkObjectFactory *factory;
 48 	GTypeQuery query;
 49 	GType derived_atk_type;
 50 
 51 	if (!type) {
 52 		static GTypeInfo tinfo = {
 53 			sizeof (EaDayViewClass),
 54 			(GBaseInitFunc) NULL, /* base init */
 55 			(GBaseFinalizeFunc) NULL, /* base finalize */
 56 			(GClassInitFunc) ea_day_view_class_init, /* class init */
 57 			(GClassFinalizeFunc) NULL, /* class finalize */
 58 			NULL, /* class data */
 59 			sizeof (EaDayView), /* instance size */
 60 			0, /* nb preallocs */
 61 			(GInstanceInitFunc) NULL, /* instance init */
 62 			NULL /* value table */
 63 		};
 64 
 65 		/*
 66 		 * Figure out the size of the class and instance
 67 		 * we are run-time deriving from (EaCalView, in this case)
 68 		 *
 69 		 * Note: we must still use run-time deriving here, because
 70 		 * our parent class EaCalView is run-time deriving.
 71 		 */
 72 
 73 		factory = atk_registry_get_factory (
 74 			atk_get_default_registry (),
 75 			e_calendar_view_get_type ());
 76 		derived_atk_type = atk_object_factory_get_accessible_type (factory);
 77 		g_type_query (derived_atk_type, &query);
 78 
 79 		tinfo.class_size = query.class_size;
 80 		tinfo.instance_size = query.instance_size;
 81 
 82 		type = g_type_register_static (
 83 			derived_atk_type,
 84 			"EaDayView", &tinfo, 0);
 85 	}
 86 
 87 	return type;
 88 }
 89 
 90 static void
 91 ea_day_view_class_init (EaDayViewClass *klass)
 92 {
 93 	AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
 94 
 95 	parent_class = g_type_class_peek_parent (klass);
 96 
 97 	class->get_name = ea_day_view_get_name;
 98 	class->get_description = ea_day_view_get_description;
 99 
100 	class->get_n_children = ea_day_view_get_n_children;
101 	class->ref_child = ea_day_view_ref_child;
102 }
103 
104 AtkObject *
105 ea_day_view_new (GtkWidget *widget)
106 {
107 	GObject *object;
108 	AtkObject *accessible;
109 
110 	g_return_val_if_fail (E_IS_DAY_VIEW (widget), NULL);
111 
112 	object = g_object_new (EA_TYPE_DAY_VIEW, NULL);
113 
114 	accessible = ATK_OBJECT (object);
115 	atk_object_initialize (accessible, widget);
116 
117 #ifdef ACC_DEBUG
118 	printf ("EvoAcc: ea_day_view created %p\n", (gpointer) accessible);
119 #endif
120 
121 	return accessible;
122 }
123 
124 static const gchar *
125 ea_day_view_get_name (AtkObject *accessible)
126 {
127 	EDayView *day_view;
128 	GnomeCalendar *gcal;
129 	const gchar *label_text;
130 	GnomeCalendarViewType view_type;
131 	GtkWidget *widget;
132 	gint n_events;
133 	gchar *event_str, *name_str;
134 
135 	g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), NULL);
136 
137 	widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
138 	if (widget == NULL)
139 		return NULL;
140 
141 	day_view = E_DAY_VIEW (widget);
142 	gcal = e_calendar_view_get_calendar (E_CALENDAR_VIEW (day_view));
143 
144 	label_text = ea_gnome_calendar_get_label_description (gcal);
145 
146 	n_events = atk_object_get_n_accessible_children (accessible);
147 	/* the child main item is always there */
148 	--n_events;
149 	if (n_events >= 1)
150 		/* To translators: Here, "It" is either like "Work Week View: July
151 		10th - July 14th, 2006." or "Day View: Thursday July 13th, 2006." */
152 		event_str = g_strdup_printf (
153 			ngettext (
154 				"It has %d event.",
155 				"It has %d events.",
156 				n_events),
157 			n_events);
158 	else
159 		/* To translators: Here, "It" is either like "Work Week View: July
160 		10th - July 14th, 2006." or "Day View: Thursday July 13th, 2006." */
161 		event_str = g_strdup (_("It has no events."));
162 
163 	view_type = gnome_calendar_get_view (gcal);
164 	if (view_type == GNOME_CAL_WORK_WEEK_VIEW)
165 		/* To translators: First %s is the week, for example "July 10th -
166 		July 14th, 2006". Second %s is the number of events in this work
167 		week, for example "It has %d event/events." or  "It has no events." */
168 		name_str = g_strdup_printf (
169 			_("Work Week View: %s. %s"),
170 			label_text, event_str);
171 	else
172 		/* To translators: First %s is the day, for example "Thursday July
173 		13th, 2006". Second %s is the number of events on this day, for
174 		example "It has %d event/events." or  "It has no events." */
175 		name_str = g_strdup_printf (
176 			_("Day View: %s. %s"),
177 			label_text, event_str);
178 
179 	ATK_OBJECT_CLASS (parent_class)->set_name (accessible, name_str);
180 	g_free (name_str);
181 	g_free (event_str);
182 
183 	return accessible->name;
184 }
185 
186 static const gchar *
187 ea_day_view_get_description (AtkObject *accessible)
188 {
189 	EDayView *day_view;
190 	GtkWidget *widget;
191 
192 	g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), NULL);
193 
194 	widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
195 	if (widget == NULL)
196 		return NULL;
197 
198 	day_view = E_DAY_VIEW (widget);
199 
200 	if (accessible->description)
201 		return accessible->description;
202 	else {
203 		GnomeCalendar *gcal;
204 		GnomeCalendarViewType view_type;
205 
206 		gcal = e_calendar_view_get_calendar (E_CALENDAR_VIEW (day_view));
207 		view_type = gnome_calendar_get_view (gcal);
208 
209 		if (view_type == GNOME_CAL_WORK_WEEK_VIEW)
210 			return _("calendar view for a work week");
211 		else
212 			return _("calendar view for one or more days");
213 	}
214 }
215 
216 static gint
217 ea_day_view_get_n_children (AtkObject *accessible)
218 {
219 	EDayView *day_view;
220 	GtkWidget *widget;
221 	gint day;
222 	gint child_num = 0;
223 
224 	g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), -1);
225 
226 	widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
227 	if (widget == NULL)
228 		return -1;
229 
230 	day_view = E_DAY_VIEW (widget);
231 
232 	child_num += day_view->long_events->len;
233 
234 	for (day = 0; day < day_view->days_shown; day++) {
235 		child_num += day_view->events[day]->len;
236 	}
237 
238 	/* "+1" for the main item */
239 	return child_num + 1;
240 }
241 
242 static AtkObject *
243 ea_day_view_ref_child (AtkObject *accessible,
244                        gint index)
245 {
246 	EDayView *day_view;
247 	gint child_num;
248 	gint day;
249 	AtkObject *atk_object = NULL;
250 	EDayViewEvent *event = NULL;
251 	GtkWidget *widget;
252 
253 	g_return_val_if_fail (EA_IS_DAY_VIEW (accessible), NULL);
254 
255 	child_num = atk_object_get_n_accessible_children (accessible);
256 	if (child_num <= 0 || index < 0 || index >= child_num)
257 		return NULL;
258 
259 	widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
260 	if (widget == NULL)
261 		return NULL;
262 
263 	day_view = E_DAY_VIEW (widget);
264 
265 	if (index == 0) {
266 		/* index == 0 is the main item */
267 		atk_object = atk_gobject_accessible_for_object (
268 			G_OBJECT (day_view->main_canvas_item));
269 		g_object_ref (atk_object);
270 	}
271 	else {
272 		--index;
273 		/* a long event */
274 		if (index < day_view->long_events->len) {
275 			event = &g_array_index (day_view->long_events,
276 						EDayViewEvent, index);
277 		}
278 		else {
279 			index -= day_view->long_events->len;
280 			day = 0;
281 			while (index >= day_view->events[day]->len) {
282 				index -= day_view->events[day]->len;
283 				++day;
284 			}
285 
286 			event = &g_array_index (day_view->events[day],
287 						EDayViewEvent, index);
288 		}
289 		if (event && event->canvas_item) {
290 			/* Not use atk_gobject_accessible_for_object here,
291 			 * we need to do special thing here
292 			 */
293 			atk_object = ea_calendar_helpers_get_accessible_for (
294 				event->canvas_item);
295 			g_object_ref (atk_object);
296 		}
297 	}
298 	return atk_object;
299 }