Bug 793125 - Crash due to popup menus left attached too long
[evolution.git] / src / calendar / gui / ea-calendar-helpers.c
blobf45b4bd3c651c0df8c445c3346fbf222414e141a
1 /*
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10 * for more details.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 * Authors:
17 * Bolian Yin <bolian.yin@sun.com>
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
23 #include "evolution-config.h"
25 #include "ea-calendar-helpers.h"
26 #include "ea-cal-view-event.h"
27 #include "ea-jump-button.h"
28 #include "e-day-view.h"
29 #include "e-week-view.h"
31 #include <libgnomecanvas/libgnomecanvas.h>
33 /**
34 * ea_calendar_helpers_get_accessible_for
35 * @canvas_item: the canvas item for a event or a jump button
37 * Returns: the atk object for the canvas_item
38 **/
39 AtkObject *
40 ea_calendar_helpers_get_accessible_for (GnomeCanvasItem *canvas_item)
42 AtkObject *atk_obj = NULL;
43 GObject *g_obj;
45 g_return_val_if_fail ((E_IS_TEXT (canvas_item)) ||
46 (GNOME_IS_CANVAS_ITEM (canvas_item)), NULL);
48 g_obj = G_OBJECT (canvas_item);
49 /* we cannot use atk_gobject_accessible_for_object here,
50 * EaDayViewEvent/EaWeekViewEvent cannot be created by the
51 * registered facotry of E_TEXT
53 atk_obj = g_object_get_data (g_obj, "accessible-object");
54 if (!atk_obj) {
55 if (E_IS_TEXT (canvas_item)) {
56 atk_obj = ea_cal_view_event_new (g_obj);
58 else if (GNOME_IS_CANVAS_PIXBUF (canvas_item)) {
59 atk_obj = ea_jump_button_new (g_obj);
61 else
62 return NULL;
64 return atk_obj;
67 /**
68 * ea_calendar_helpers_get_view_widget_from:
69 * @canvas_item: the canvas item for a event or a jump button
71 * Get the cal view widget contains the canvas_item.
73 * Returns: the cal view widget if exists
74 **/
75 ECalendarView *
76 ea_calendar_helpers_get_cal_view_from (GnomeCanvasItem *canvas_item)
78 GnomeCanvas *canvas;
79 GtkWidget *view_widget = NULL;
81 g_return_val_if_fail (canvas_item, NULL);
82 g_return_val_if_fail ((E_IS_TEXT (canvas_item)) ||
83 (GNOME_IS_CANVAS_ITEM (canvas_item)), NULL);
85 /* canvas_item is the e_text for the event */
86 /* canvas_item->canvas is the ECanvas for day view */
87 /* parent of canvas_item->canvas is the EDayView or EWeekView widget */
88 canvas = canvas_item->canvas;
89 view_widget = gtk_widget_get_parent (GTK_WIDGET (canvas));
91 if (view_widget && GTK_IS_BOX (view_widget))
92 view_widget = gtk_widget_get_parent (view_widget);
94 if (!view_widget || !E_IS_CALENDAR_VIEW (view_widget))
95 return NULL;
97 return E_CALENDAR_VIEW (view_widget);
101 * ea_calendar_helpers_get_cal_view_event_from
102 * @canvas_item: the cavas_item (e_text) for the event
104 * Get the ECalendarViewEvent for the canvas_item.
106 * Returns: the ECalendarViewEvent
108 ECalendarViewEvent *
109 ea_calendar_helpers_get_cal_view_event_from (GnomeCanvasItem *canvas_item)
111 ECalendarView *cal_view;
112 gboolean event_found;
113 ECalendarViewEvent *cal_view_event;
115 g_return_val_if_fail (E_IS_TEXT (canvas_item), NULL);
117 cal_view = ea_calendar_helpers_get_cal_view_from (canvas_item);
119 if (!cal_view)
120 return NULL;
122 if (E_IS_DAY_VIEW (cal_view)) {
123 gint event_day, event_num;
124 EDayViewEvent *day_view_event;
125 EDayView *day_view = E_DAY_VIEW (cal_view);
126 event_found = e_day_view_find_event_from_item (
127 day_view, canvas_item,
128 &event_day, &event_num);
129 if (!event_found)
130 return NULL;
131 if (event_day == E_DAY_VIEW_LONG_EVENT) {
132 /* a long event */
133 day_view_event = &g_array_index (day_view->long_events,
134 EDayViewEvent, event_num);
136 else {
137 /* a main canvas event */
138 day_view_event = &g_array_index (day_view->events[event_day],
139 EDayViewEvent, event_num);
141 cal_view_event = (ECalendarViewEvent *) day_view_event;
143 else if (E_IS_WEEK_VIEW (cal_view)) {
144 gint event_num, span_num;
145 EWeekViewEvent *week_view_event;
146 EWeekView *week_view = E_WEEK_VIEW (cal_view);
147 event_found = e_week_view_find_event_from_item (
148 week_view,
149 canvas_item,
150 &event_num,
151 &span_num);
152 if (!event_found)
153 return NULL;
155 week_view_event = &g_array_index (
156 week_view->events, EWeekViewEvent, event_num);
158 cal_view_event = (ECalendarViewEvent *) week_view_event;
160 else {
161 g_return_val_if_reached (NULL);
163 return cal_view_event;