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

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Bolian Yin <bolian.yin@sun.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include "ea-day-view-cell.h"
 29 #include "ea-day-view-main-item.h"
 30 #include "ea-day-view.h"
 31 #include "a11y/ea-factory.h"
 32 
 33 /* EDayViewCell */
 34 
 35 static void e_day_view_cell_class_init (EDayViewCellClass *class);
 36 
 37 EA_FACTORY_GOBJECT (EA_TYPE_DAY_VIEW_CELL, ea_day_view_cell, ea_day_view_cell_new)
 38 
 39 GType
 40 e_day_view_cell_get_type (void)
 41 {
 42 	static GType type = 0;
 43 
 44 	if (!type) {
 45 		static GTypeInfo tinfo = {
 46 			sizeof (EDayViewCellClass),
 47 			(GBaseInitFunc) NULL, /* base init */
 48 			(GBaseFinalizeFunc) NULL, /* base finalize */
 49 			(GClassInitFunc) e_day_view_cell_class_init, /* class init */
 50 			(GClassFinalizeFunc) NULL, /* class finalize */
 51 			NULL, /* class data */
 52 			sizeof (EDayViewCell), /* instance size */
 53 			0, /* nb preallocs */
 54 			(GInstanceInitFunc) NULL, /* instance init */
 55 			NULL /* value table */
 56 		};
 57 
 58 		type = g_type_register_static (
 59 			G_TYPE_OBJECT,
 60 			"EDayViewCell", &tinfo, 0);
 61 	}
 62 
 63 	return type;
 64 }
 65 
 66 static void
 67 e_day_view_cell_class_init (EDayViewCellClass *class)
 68 {
 69     EA_SET_FACTORY (e_day_view_cell_get_type (), ea_day_view_cell);
 70 }
 71 
 72 EDayViewCell *
 73 e_day_view_cell_new (EDayView *day_view,
 74                      gint row,
 75                      gint column)
 76 {
 77 	GObject *object;
 78 	EDayViewCell *cell;
 79 
 80 	g_return_val_if_fail (E_IS_DAY_VIEW (day_view), NULL);
 81 
 82 	object = g_object_new (E_TYPE_DAY_VIEW_CELL, NULL);
 83 	cell = E_DAY_VIEW_CELL (object);
 84 	cell->day_view = day_view;
 85 	cell->row = row;
 86 	cell->column = column;
 87 
 88 #ifdef ACC_DEBUG
 89 	printf ("EvoAcc: e_day_view_cell created %p\n", (gpointer) cell);
 90 #endif
 91 
 92 	return cell;
 93 }
 94 
 95 /* EaDayViewCell */
 96 
 97 static void ea_day_view_cell_class_init (EaDayViewCellClass *class);
 98 
 99 static const gchar * ea_day_view_cell_get_name (AtkObject *accessible);
100 static const gchar * ea_day_view_cell_get_description (AtkObject *accessible);
101 static AtkStateSet * ea_day_view_cell_ref_state_set (AtkObject *obj);
102 static AtkObject * ea_day_view_cell_get_parent (AtkObject *accessible);
103 static gint ea_day_view_cell_get_index_in_parent (AtkObject *accessible);
104 
105 /* component interface */
106 static void atk_component_interface_init (AtkComponentIface *iface);
107 static void component_interface_get_extents (AtkComponent *component,
108 					     gint *x, gint *y,
109 					     gint *width, gint *height,
110 					     AtkCoordType coord_type);
111 static gboolean component_interface_grab_focus (AtkComponent *component);
112 
113 static gpointer parent_class = NULL;
114 
115 #ifdef ACC_DEBUG
116 static gint n_ea_day_view_cell_created = 0, n_ea_day_view_cell_destroyed = 0;
117 static void ea_day_view_cell_finalize (GObject *object);
118 #endif
119 
120 GType
121 ea_day_view_cell_get_type (void)
122 {
123 	static GType type = 0;
124 
125 	if (!type) {
126 		static GTypeInfo tinfo = {
127 			sizeof (EaDayViewCellClass),
128 			(GBaseInitFunc) NULL, /* base init */
129 			(GBaseFinalizeFunc) NULL, /* base finalize */
130 			(GClassInitFunc) ea_day_view_cell_class_init, /* class init */
131 			(GClassFinalizeFunc) NULL, /* class finalize */
132 			NULL, /* class data */
133 			sizeof (EaDayViewCell), /* instance size */
134 			0, /* nb preallocs */
135 			(GInstanceInitFunc) NULL, /* instance init */
136 			NULL /* value table */
137 		};
138 
139 		static const GInterfaceInfo atk_component_info = {
140 			(GInterfaceInitFunc) atk_component_interface_init,
141 			(GInterfaceFinalizeFunc) NULL,
142 			NULL
143 		};
144 
145 		type = g_type_register_static (
146 			ATK_TYPE_GOBJECT_ACCESSIBLE,
147 			"EaDayViewCell", &tinfo, 0);
148 		g_type_add_interface_static (
149 			type, ATK_TYPE_COMPONENT,
150 			&atk_component_info);
151 	}
152 
153 	return type;
154 }
155 
156 static void
157 ea_day_view_cell_class_init (EaDayViewCellClass *klass)
158 {
159 	AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
160 
161 #ifdef ACC_DEBUG
162 	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
163 	gobject_class->finalize = ea_day_view_cell_finalize;
164 #endif
165 
166 	parent_class = g_type_class_peek_parent (klass);
167 
168 	class->get_name = ea_day_view_cell_get_name;
169 	class->get_description = ea_day_view_cell_get_description;
170 	class->ref_state_set = ea_day_view_cell_ref_state_set;
171 
172 	class->get_parent = ea_day_view_cell_get_parent;
173 	class->get_index_in_parent = ea_day_view_cell_get_index_in_parent;
174 }
175 
176 AtkObject *
177 ea_day_view_cell_new (GObject *obj)
178 {
179 	gpointer object;
180 	AtkObject *atk_object;
181 
182 	g_return_val_if_fail (E_IS_DAY_VIEW_CELL (obj), NULL);
183 
184 	object = g_object_new (EA_TYPE_DAY_VIEW_CELL, NULL);
185 	atk_object = ATK_OBJECT (object);
186 	atk_object_initialize (atk_object, obj);
187 	atk_object->role = ATK_ROLE_UNKNOWN;
188 
189 #ifdef ACC_DEBUG
190 	++n_ea_day_view_cell_created;
191 	printf (
192 		"ACC_DEBUG: n_ea_day_view_cell_created = %d\n",
193 		n_ea_day_view_cell_created);
194 #endif
195 	return atk_object;
196 }
197 
198 #ifdef ACC_DEBUG
199 static void ea_day_view_cell_finalize (GObject *object)
200 {
201 	G_OBJECT_CLASS (parent_class)->finalize (object);
202 
203 	++n_ea_day_view_cell_destroyed;
204 	printf (
205 		"ACC_DEBUG: n_ea_day_view_cell_destroyed = %d\n",
206 		n_ea_day_view_cell_destroyed);
207 }
208 #endif
209 
210 static const gchar *
211 ea_day_view_cell_get_name (AtkObject *accessible)
212 {
213 	AtkGObjectAccessible *atk_gobj;
214 	GObject *g_obj;
215 	EDayViewCell *cell;
216 
217 	g_return_val_if_fail (EA_IS_DAY_VIEW_CELL (accessible), NULL);
218 
219 	if (!accessible->name) {
220 		AtkObject *ea_main_item;
221 		GnomeCanvasItem *main_item;
222 		gchar *new_name;
223 		const gchar *row_label, *column_label;
224 
225 		atk_gobj = ATK_GOBJECT_ACCESSIBLE (accessible);
226 		g_obj = atk_gobject_accessible_get_object (atk_gobj);
227 		if (!g_obj)
228 			return NULL;
229 
230 		cell = E_DAY_VIEW_CELL (g_obj);
231 		main_item = cell->day_view->main_canvas_item;
232 		ea_main_item = atk_gobject_accessible_for_object (G_OBJECT (main_item));
233 		column_label = atk_table_get_column_description (
234 			ATK_TABLE (ea_main_item),
235 			cell->column);
236 		row_label = atk_table_get_row_description (
237 			ATK_TABLE (ea_main_item),
238 			cell->row);
239 		new_name = g_strconcat (column_label, " ", row_label, NULL);
240 		ATK_OBJECT_CLASS (parent_class)->set_name (accessible, new_name);
241 		g_free (new_name);
242 	}
243 	return accessible->name;
244 }
245 
246 static const gchar *
247 ea_day_view_cell_get_description (AtkObject *accessible)
248 {
249 	return ea_day_view_cell_get_name (accessible);
250 }
251 
252 static AtkStateSet *
253 ea_day_view_cell_ref_state_set (AtkObject *obj)
254 {
255   AtkStateSet *state_set;
256   GObject *g_obj;
257   AtkObject *parent;
258   gint x, y, width, height;
259   gint parent_x, parent_y, parent_width, parent_height;
260 
261   state_set = ATK_OBJECT_CLASS (parent_class)->ref_state_set (obj);
262   g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (obj));
263   if (!g_obj)
264 	  return state_set;
265 
266   atk_state_set_add_state (state_set, ATK_STATE_SELECTABLE);
267 
268   parent = atk_object_get_parent (obj);
269   atk_component_get_extents (ATK_COMPONENT (obj), &x, &y,
270 			     &width, &height, ATK_XY_WINDOW);
271   atk_component_get_extents (ATK_COMPONENT (parent), &parent_x, &parent_y,
272 			     &parent_width, &parent_height, ATK_XY_WINDOW);
273 
274   if (x + width < parent_x || x > parent_x + parent_width ||
275       y + height < parent_y || y > parent_y + parent_height)
276 	  /* the cell is out of the main canvas */
277 	  ;
278   else
279 	  atk_state_set_add_state (state_set, ATK_STATE_VISIBLE);
280 
281   return state_set;
282 }
283 
284 static AtkObject *
285 ea_day_view_cell_get_parent (AtkObject *accessible)
286 {
287 	AtkGObjectAccessible *atk_gobj;
288 	GObject *g_obj;
289 	EDayViewCell *cell;
290 
291 	g_return_val_if_fail (EA_IS_DAY_VIEW_CELL (accessible), NULL);
292 
293 	atk_gobj = ATK_GOBJECT_ACCESSIBLE (accessible);
294 	g_obj = atk_gobject_accessible_get_object (atk_gobj);
295 	if (!g_obj)
296 		return NULL;
297 
298 	cell = E_DAY_VIEW_CELL (g_obj);
299 
300 	return atk_gobject_accessible_for_object (
301 		G_OBJECT (cell->day_view->main_canvas_item));
302 }
303 
304 static gint
305 ea_day_view_cell_get_index_in_parent (AtkObject *accessible)
306 {
307 	AtkGObjectAccessible *atk_gobj;
308 	GObject *g_obj;
309 	EDayViewCell *cell;
310 	AtkObject *parent;
311 
312 	g_return_val_if_fail (EA_IS_DAY_VIEW_CELL (accessible), -1);
313 
314 	atk_gobj = ATK_GOBJECT_ACCESSIBLE (accessible);
315 	g_obj = atk_gobject_accessible_get_object (atk_gobj);
316 	if (!g_obj)
317 		return -1;
318 
319 	cell = E_DAY_VIEW_CELL (g_obj);
320 	parent = atk_object_get_parent (accessible);
321 	return atk_table_get_index_at (
322 		ATK_TABLE (parent),
323 		cell->row, cell->column);
324 }
325 
326 /* Atk Component Interface */
327 
328 static void
329 atk_component_interface_init (AtkComponentIface *iface)
330 {
331 	g_return_if_fail (iface != NULL);
332 
333 	iface->get_extents = component_interface_get_extents;
334 	iface->grab_focus = component_interface_grab_focus;
335 }
336 
337 static void
338 component_interface_get_extents (AtkComponent *component,
339                                  gint *x,
340                                  gint *y,
341                                  gint *width,
342                                  gint *height,
343                                  AtkCoordType coord_type)
344 {
345 	GObject *g_obj;
346 	AtkObject *atk_obj;
347 	EDayViewCell *cell;
348 	EDayView *day_view;
349 	GtkWidget *main_canvas;
350 	gint day_view_width, day_view_height;
351 	gint scroll_x, scroll_y;
352 
353 	*x = *y = *width = *height = 0;
354 
355 	g_return_if_fail (EA_IS_DAY_VIEW_CELL (component));
356 
357 	g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (component));
358 	if (!g_obj)
359 		/* defunct object*/
360 		return;
361 
362 	cell = E_DAY_VIEW_CELL (g_obj);
363 	day_view = cell->day_view;
364 	main_canvas = cell->day_view->main_canvas;
365 
366 	atk_obj = atk_gobject_accessible_for_object (G_OBJECT (main_canvas));
367 	atk_component_get_extents (
368 		ATK_COMPONENT (atk_obj),
369 		x, y,
370 		&day_view_width, &day_view_height,
371 		coord_type);
372 	gnome_canvas_get_scroll_offsets (
373 		GNOME_CANVAS (day_view->main_canvas),
374 		&scroll_x, &scroll_y);
375 	*x += day_view->day_offsets[cell->column] - scroll_x;
376 	*y += day_view->row_height * cell->row
377 		- scroll_y;
378 	*width = day_view->day_widths[cell->column];
379 	*height = day_view->row_height;
380 }
381 
382 static gboolean
383 component_interface_grab_focus (AtkComponent *comp)
384 {
385 	GObject *g_obj;
386 	EDayViewCell *cell;
387 	EDayView *day_view;
388 	GtkWidget *toplevel;
389 
390 	g_return_val_if_fail (EA_IS_DAY_VIEW_CELL (comp), FALSE);
391 
392 	g_obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (comp));
393 	if (!g_obj)
394 		return FALSE;
395 
396 	cell = E_DAY_VIEW_CELL (g_obj);
397 	day_view = cell->day_view;
398 
399 	day_view->selection_start_day = cell->column;
400 	day_view->selection_end_day = cell->column;
401 	day_view->selection_start_row = cell->row;
402 	day_view->selection_end_row = cell->row;
403 
404 	e_day_view_ensure_rows_visible (
405 		day_view,
406 		day_view->selection_start_row,
407 		day_view->selection_end_row);
408 	e_day_view_update_calendar_selection_time (day_view);
409 	gtk_widget_queue_draw (day_view->main_canvas);
410 
411 	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (day_view));
412 	if (gtk_widget_is_toplevel (toplevel))
413 		gtk_window_present (GTK_WINDOW (toplevel));
414 
415 	return TRUE;
416 }