1 /*
2  * Pidgin is the legal property of its developers, whose names are too numerous
3  * to list here.  Please refer to the COPYRIGHT file distributed with this
4  * source distribution.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
19  */
20 #include "debug.h"
21 
22 #include "gtkmenutray.h"
23 
24 #include <gtk/gtk.h>
25 
26 /******************************************************************************
27  * Enums
28  *****************************************************************************/
29 enum {
30 	PROP_ZERO = 0,
31 	PROP_BOX
32 };
33 
34 /******************************************************************************
35  * Globals
36  *****************************************************************************/
37 static GObjectClass *parent_class = NULL;
38 
39 /******************************************************************************
40  * Internal Stuff
41  *****************************************************************************/
42 
43 /******************************************************************************
44  * Item Stuff
45  *****************************************************************************/
46 static void
pidgin_menu_tray_select(GtkItem * item)47 pidgin_menu_tray_select(GtkItem *item) {
48 	/* this may look like nothing, but it's really overriding the
49 	 * GtkMenuItem's select function so that it doesn't get highlighted like
50 	 * a normal menu item would.
51 	 */
52 }
53 
54 static void
pidgin_menu_tray_deselect(GtkItem * item)55 pidgin_menu_tray_deselect(GtkItem *item) {
56 	/* Probably not necessary, but I'd rather be safe than sorry.  We're
57 	 * overridding the select, so it makes sense to override deselect as well.
58 	 */
59 }
60 
61 /******************************************************************************
62  * Widget Stuff
63  *****************************************************************************/
64 
65 /******************************************************************************
66  * Object Stuff
67  *****************************************************************************/
68 static void
pidgin_menu_tray_get_property(GObject * obj,guint param_id,GValue * value,GParamSpec * pspec)69 pidgin_menu_tray_get_property(GObject *obj, guint param_id, GValue *value,
70 								GParamSpec *pspec)
71 {
72 	PidginMenuTray *menu_tray = PIDGIN_MENU_TRAY(obj);
73 
74 	switch(param_id) {
75 		case PROP_BOX:
76 			g_value_set_object(value, pidgin_menu_tray_get_box(menu_tray));
77 			break;
78 		default:
79 			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
80 			break;
81 	}
82 }
83 
84 static void
pidgin_menu_tray_map(GtkWidget * widget)85 pidgin_menu_tray_map(GtkWidget *widget)
86 {
87 	GTK_WIDGET_CLASS(parent_class)->map(widget);
88 	gtk_container_add(GTK_CONTAINER(widget),
89 			PIDGIN_MENU_TRAY(widget)->tray);
90 }
91 
92 static void
pidgin_menu_tray_finalize(GObject * obj)93 pidgin_menu_tray_finalize(GObject *obj)
94 {
95 	PidginMenuTray *tray = PIDGIN_MENU_TRAY(obj);
96 #if 0
97 	/* This _might_ be leaking, but I have a sneaking suspicion that the widget is
98 	 * getting destroyed in GtkContainer's finalize function.  But if were are
99 	 * leaking here, be sure to figure out why this causes a crash.
100 	 *	-- Gary
101 	 */
102 
103 	if(GTK_IS_WIDGET(tray->tray))
104 		gtk_widget_destroy(GTK_WIDGET(tray->tray));
105 #endif
106 
107 	if (tray->tooltips) {
108 		gtk_object_sink(GTK_OBJECT(tray->tooltips));
109 	}
110 
111 	G_OBJECT_CLASS(parent_class)->finalize(obj);
112 }
113 
114 static void
pidgin_menu_tray_class_init(PidginMenuTrayClass * klass)115 pidgin_menu_tray_class_init(PidginMenuTrayClass *klass) {
116 	GObjectClass *object_class = G_OBJECT_CLASS(klass);
117 	GtkItemClass *item_class = GTK_ITEM_CLASS(klass);
118 	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
119 	GParamSpec *pspec;
120 
121 	parent_class = g_type_class_peek_parent(klass);
122 
123 	object_class->finalize = pidgin_menu_tray_finalize;
124 	object_class->get_property = pidgin_menu_tray_get_property;
125 
126 	item_class->select = pidgin_menu_tray_select;
127 	item_class->deselect = pidgin_menu_tray_deselect;
128 
129 	widget_class->map = pidgin_menu_tray_map;
130 
131 	pspec = g_param_spec_object("box", "The box",
132 								"The box",
133 								GTK_TYPE_BOX,
134 								G_PARAM_READABLE);
135 	g_object_class_install_property(object_class, PROP_BOX, pspec);
136 }
137 
138 static void
pidgin_menu_tray_init(PidginMenuTray * menu_tray)139 pidgin_menu_tray_init(PidginMenuTray *menu_tray) {
140 	GtkWidget *widget = GTK_WIDGET(menu_tray);
141 	GtkSettings *settings;
142 	gint height = -1;
143 
144 	gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_tray), TRUE);
145 
146 	if(!GTK_IS_WIDGET(menu_tray->tray))
147 		menu_tray->tray = gtk_hbox_new(FALSE, 0);
148 
149 	settings =
150 		gtk_settings_get_for_screen(gtk_widget_get_screen(widget));
151 
152 	if(gtk_icon_size_lookup_for_settings(settings, GTK_ICON_SIZE_MENU,
153 										 NULL, &height))
154 	{
155 		gtk_widget_set_size_request(widget, -1, height);
156 	}
157 
158 	gtk_widget_show(menu_tray->tray);
159 }
160 
161 /******************************************************************************
162  * API
163  *****************************************************************************/
164 GType
pidgin_menu_tray_get_gtype(void)165 pidgin_menu_tray_get_gtype(void) {
166 	static GType type = 0;
167 
168 	if(type == 0) {
169 		static const GTypeInfo info = {
170 			sizeof(PidginMenuTrayClass),
171 			NULL,
172 			NULL,
173 			(GClassInitFunc)pidgin_menu_tray_class_init,
174 			NULL,
175 			NULL,
176 			sizeof(PidginMenuTray),
177 			0,
178 			(GInstanceInitFunc)pidgin_menu_tray_init,
179 			NULL
180 		};
181 
182 		type = g_type_register_static(GTK_TYPE_MENU_ITEM,
183 									  "PidginMenuTray",
184 									  &info, 0);
185 	}
186 
187 	return type;
188 }
189 
190 GtkWidget *
pidgin_menu_tray_new()191 pidgin_menu_tray_new() {
192 	return g_object_new(PIDGIN_TYPE_MENU_TRAY, NULL);
193 }
194 
195 GtkWidget *
pidgin_menu_tray_get_box(PidginMenuTray * menu_tray)196 pidgin_menu_tray_get_box(PidginMenuTray *menu_tray) {
197 	g_return_val_if_fail(PIDGIN_IS_MENU_TRAY(menu_tray), NULL);
198 	return menu_tray->tray;
199 }
200 
201 static void
pidgin_menu_tray_add(PidginMenuTray * menu_tray,GtkWidget * widget,const char * tooltip,gboolean prepend)202 pidgin_menu_tray_add(PidginMenuTray *menu_tray, GtkWidget *widget,
203 					   const char *tooltip, gboolean prepend)
204 {
205 	g_return_if_fail(PIDGIN_IS_MENU_TRAY(menu_tray));
206 	g_return_if_fail(GTK_IS_WIDGET(widget));
207 
208 	if (GTK_WIDGET_NO_WINDOW(widget))
209 	{
210 		GtkWidget *event;
211 
212 		event = gtk_event_box_new();
213 		gtk_container_add(GTK_CONTAINER(event), widget);
214 		gtk_widget_show(event);
215 		widget = event;
216 	}
217 
218 	pidgin_menu_tray_set_tooltip(menu_tray, widget, tooltip);
219 
220 	if (prepend)
221 		gtk_box_pack_start(GTK_BOX(menu_tray->tray), widget, FALSE, FALSE, 0);
222 	else
223 		gtk_box_pack_end(GTK_BOX(menu_tray->tray), widget, FALSE, FALSE, 0);
224 }
225 
226 void
pidgin_menu_tray_append(PidginMenuTray * menu_tray,GtkWidget * widget,const char * tooltip)227 pidgin_menu_tray_append(PidginMenuTray *menu_tray, GtkWidget *widget, const char *tooltip)
228 {
229 	pidgin_menu_tray_add(menu_tray, widget, tooltip, FALSE);
230 }
231 
232 void
pidgin_menu_tray_prepend(PidginMenuTray * menu_tray,GtkWidget * widget,const char * tooltip)233 pidgin_menu_tray_prepend(PidginMenuTray *menu_tray, GtkWidget *widget, const char *tooltip)
234 {
235 	pidgin_menu_tray_add(menu_tray, widget, tooltip, TRUE);
236 }
237 
238 void
pidgin_menu_tray_set_tooltip(PidginMenuTray * menu_tray,GtkWidget * widget,const char * tooltip)239 pidgin_menu_tray_set_tooltip(PidginMenuTray *menu_tray, GtkWidget *widget, const char *tooltip)
240 {
241 	if (!menu_tray->tooltips)
242 		menu_tray->tooltips = gtk_tooltips_new();
243 
244 	/* Should we check whether widget is a child of menu_tray? */
245 
246 	/*
247 	 * If the widget does not have it's own window, then it
248 	 * must have automatically been added to an event box
249 	 * when it was added to the menu tray.  If this is the
250 	 * case, we want to set the tooltip on the widget's parent,
251 	 * not on the widget itself.
252 	 */
253 	if (GTK_WIDGET_NO_WINDOW(widget))
254 		widget = widget->parent;
255 
256 	gtk_tooltips_set_tip(menu_tray->tooltips, widget, tooltip, NULL);
257 }
258 
259