1 /*
2  *
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.
6  *
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.
11  *
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/>.
14  *
15  *
16  * Authors:
17  *		Yang Wu <yang.wu@sun.com>
18  *
19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20  *
21  */
22 
23 #include "evolution-config.h"
24 
25 #include "ea-jump-button.h"
26 #include "ea-calendar-helpers.h"
27 #include "ea-week-view.h"
28 #include "e-week-view.h"
29 #include <libgnomecanvas/libgnomecanvas.h>
30 #include <glib/gi18n.h>
31 
32 static void ea_jump_button_class_init (EaJumpButtonClass *klass);
33 
34 static const gchar * ea_jump_button_get_name (AtkObject *accessible);
35 static const gchar * ea_jump_button_get_description (AtkObject *accessible);
36 
37 /* action interface */
38 static void                  atk_action_interface_init  (AtkActionIface *iface);
39 static gboolean              jump_button_do_action      (AtkAction      *action,
40                                                          gint           i);
41 static gint                  jump_button_get_n_actions  (AtkAction      *action);
42 static const gchar * jump_button_get_keybinding (AtkAction      *action,
43                                                          gint           i);
44 
45 static gpointer parent_class = NULL;
46 
47 GType
ea_jump_button_get_type(void)48 ea_jump_button_get_type (void)
49 {
50 	static GType type = 0;
51 	AtkObjectFactory *factory;
52 	GTypeQuery query;
53 	GType derived_atk_type;
54 
55 	if (!type) {
56 		static GTypeInfo tinfo = {
57 			sizeof (EaJumpButtonClass),
58 			(GBaseInitFunc) NULL,
59 			(GBaseFinalizeFunc) NULL,
60 			(GClassInitFunc) ea_jump_button_class_init,
61 			(GClassFinalizeFunc) NULL,
62 			/* class_data */ NULL,
63 			sizeof (EaJumpButton),
64 			/* n_preallocs */ 0,
65 			(GInstanceInitFunc) NULL,
66 			/* value_table */ NULL
67 		};
68 
69 		static const GInterfaceInfo atk_action_info = {
70 			(GInterfaceInitFunc) atk_action_interface_init,
71 			(GInterfaceFinalizeFunc) NULL,
72 			NULL
73 		};
74 
75 		/*
76 		 * Figure out the size of the class and instance
77 		 * we are run-time deriving from (atk object for
78 		 * GNOME_TYPE_CANVAS_ITEM, in this case)
79 		 */
80 
81 		factory = atk_registry_get_factory (
82 			atk_get_default_registry (), GNOME_TYPE_CANVAS_ITEM);
83 		derived_atk_type =
84 			atk_object_factory_get_accessible_type (factory);
85 		g_type_query (derived_atk_type, &query);
86 
87 		tinfo.class_size = query.class_size;
88 		tinfo.instance_size = query.instance_size;
89 
90 		/* we inherit the component and other
91 		 * interfaces from GNOME_TYPE_CANVAS_ITEM */
92 		type = g_type_register_static (
93 			derived_atk_type, "EaJumpButton", &tinfo, 0);
94 
95 		g_type_add_interface_static (
96 			type, ATK_TYPE_ACTION, &atk_action_info);
97 	}
98 
99 	return type;
100 }
101 
102 static void
ea_jump_button_class_init(EaJumpButtonClass * klass)103 ea_jump_button_class_init (EaJumpButtonClass *klass)
104 {
105 	AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
106 
107 	parent_class = g_type_class_peek_parent (klass);
108 
109 	class->get_name = ea_jump_button_get_name;
110 	class->get_description = ea_jump_button_get_description;
111 }
112 
113 AtkObject *
ea_jump_button_new(GObject * obj)114 ea_jump_button_new (GObject *obj)
115 {
116 	AtkObject *atk_obj = NULL;
117 	GObject *target_obj;
118 
119 	g_return_val_if_fail (GNOME_IS_CANVAS_ITEM (obj), NULL);
120 
121 	target_obj = obj;
122 	atk_obj = g_object_get_data (target_obj, "accessible-object");
123 
124 	if (!atk_obj) {
125 		static AtkRole event_role = ATK_ROLE_INVALID;
126 		atk_obj = ATK_OBJECT (
127 			g_object_new (EA_TYPE_JUMP_BUTTON, NULL));
128 		atk_object_initialize (atk_obj, target_obj);
129 		if (event_role == ATK_ROLE_INVALID)
130 			event_role = atk_role_register ("Jump Button");
131 		atk_obj->role = event_role;
132 	}
133 
134 	/* The registered factory for GNOME_TYPE_CANVAS_ITEM cannot create
135 	 * an EaJumpbutton, we should save the EaJumpbutton object in it. */
136 	g_object_set_data (obj, "accessible-object", atk_obj);
137 
138 	return atk_obj;
139 }
140 
141 static const gchar *
ea_jump_button_get_name(AtkObject * accessible)142 ea_jump_button_get_name (AtkObject *accessible)
143 {
144 	g_return_val_if_fail (EA_IS_JUMP_BUTTON (accessible), NULL);
145 
146 	if (accessible->name)
147 		return accessible->name;
148 	return _("Jump button");
149 }
150 
151 static const gchar *
ea_jump_button_get_description(AtkObject * accessible)152 ea_jump_button_get_description (AtkObject *accessible)
153 {
154 	if (accessible->description)
155 		return accessible->description;
156 
157 	return _("Click here, you can find more events.");
158 }
159 
160 static void
atk_action_interface_init(AtkActionIface * iface)161 atk_action_interface_init (AtkActionIface *iface)
162 {
163   g_return_if_fail (iface != NULL);
164 
165   iface->do_action = jump_button_do_action;
166   iface->get_n_actions = jump_button_get_n_actions;
167   iface->get_keybinding = jump_button_get_keybinding;
168 }
169 
170 static gboolean
jump_button_do_action(AtkAction * action,gint i)171 jump_button_do_action (AtkAction *action,
172                        gint i)
173 {
174   gboolean return_value = TRUE;
175   AtkGObjectAccessible *atk_gobj;
176   GObject *g_obj;
177   GnomeCanvasItem *item;
178   ECalendarView *cal_view;
179   EWeekView *week_view;
180 
181   atk_gobj = ATK_GOBJECT_ACCESSIBLE (action);
182   g_obj = atk_gobject_accessible_get_object (atk_gobj);
183   if (!g_obj)
184 	  return FALSE;
185 
186   item = GNOME_CANVAS_ITEM (g_obj);
187   cal_view = ea_calendar_helpers_get_cal_view_from (GNOME_CANVAS_ITEM (item));
188   week_view = E_WEEK_VIEW (cal_view);
189 
190   switch (i)
191     {
192     case 0:
193 	    e_week_view_jump_to_button_item (week_view, GNOME_CANVAS_ITEM (item));
194 	    break;
195     default:
196 	    return_value = FALSE;
197       break;
198     }
199   return return_value;
200 }
201 
202 static gint
jump_button_get_n_actions(AtkAction * action)203 jump_button_get_n_actions (AtkAction *action)
204 {
205   return 1;
206 }
207 
208 static const gchar *
jump_button_get_keybinding(AtkAction * action,gint i)209 jump_button_get_keybinding (AtkAction *action,
210                             gint i)
211 {
212   const gchar *return_value = NULL;
213 
214   switch (i)
215     {
216     case 0:
217       {
218 	      return_value = "space or enter";
219 	      break;
220       }
221     default:
222       break;
223     }
224   return return_value;
225 }
226