Merged pidgin/main into default
[pidgin-git.git] / pidgin / gtkmenutray.c
blobbd3c12a97982d239f439f0744d52ca6eecadde5c
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.
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.
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.
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
20 #include "internal.h"
22 #include "debug.h"
24 #include "gtkmenutray.h"
26 #include "glibcompat.h"
27 #include "gtk3compat.h"
29 /******************************************************************************
30 * Enums
31 *****************************************************************************/
32 enum {
33 PROP_ZERO = 0,
34 PROP_BOX
37 /******************************************************************************
38 * Globals
39 *****************************************************************************/
40 static GObjectClass *parent_class = NULL;
41 /******************************************************************************
42 * Internal Stuff
43 *****************************************************************************/
45 /******************************************************************************
46 * Item Stuff
47 *****************************************************************************/
48 static void
49 pidgin_menu_tray_select(GtkMenuItem *widget) {
50 /* this may look like nothing, but it's really overriding the
51 * GtkMenuItem's select function so that it doesn't get highlighted like
52 * a normal menu item would.
56 static void
57 pidgin_menu_tray_deselect(GtkMenuItem *widget) {
58 /* Probably not necessary, but I'd rather be safe than sorry. We're
59 * overridding the select, so it makes sense to override deselect as well.
63 /******************************************************************************
64 * Object Stuff
65 *****************************************************************************/
66 static void
67 pidgin_menu_tray_get_property(GObject *obj, guint param_id, GValue *value,
68 GParamSpec *pspec)
70 PidginMenuTray *menu_tray = PIDGIN_MENU_TRAY(obj);
72 switch(param_id) {
73 case PROP_BOX:
74 g_value_set_object(value, pidgin_menu_tray_get_box(menu_tray));
75 break;
76 default:
77 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
78 break;
82 static void
83 pidgin_menu_tray_map(GtkWidget *widget)
85 GTK_WIDGET_CLASS(parent_class)->map(widget);
86 gtk_container_add(GTK_CONTAINER(widget),
87 PIDGIN_MENU_TRAY(widget)->tray);
90 static void
91 pidgin_menu_tray_finalize(GObject *obj)
93 #if 0
94 PidginMenuTray *tray = PIDGIN_MENU_TRAY(obj);
96 /* This _might_ be leaking, but I have a sneaking suspicion that the widget is
97 * getting destroyed in GtkContainer's finalize function. But if were are
98 * leaking here, be sure to figure out why this causes a crash.
99 * -- Gary
102 if(GTK_IS_WIDGET(tray->tray))
103 gtk_widget_destroy(GTK_WIDGET(tray->tray));
104 #endif
106 G_OBJECT_CLASS(parent_class)->finalize(obj);
109 static void
110 pidgin_menu_tray_class_init(PidginMenuTrayClass *klass) {
111 GObjectClass *object_class = G_OBJECT_CLASS(klass);
112 GtkMenuItemClass *menu_item_class = GTK_MENU_ITEM_CLASS(klass);
113 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
114 GParamSpec *pspec;
116 parent_class = g_type_class_peek_parent(klass);
118 object_class->finalize = pidgin_menu_tray_finalize;
119 object_class->get_property = pidgin_menu_tray_get_property;
121 menu_item_class->select = pidgin_menu_tray_select;
122 menu_item_class->deselect = pidgin_menu_tray_deselect;
124 widget_class->map = pidgin_menu_tray_map;
126 pspec = g_param_spec_object("box", "The box",
127 "The box",
128 GTK_TYPE_BOX,
129 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
130 g_object_class_install_property(object_class, PROP_BOX, pspec);
133 static void
134 pidgin_menu_tray_init(PidginMenuTray *menu_tray) {
135 GtkWidget *widget = GTK_WIDGET(menu_tray);
136 GtkSettings *settings;
137 gint height = -1;
139 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
140 /* Gtk3 docs says, it should be replaced with gtk_widget_set_hexpand and
141 * gtk_widget_set_halign. But it doesn't seems to work. */
142 gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_tray), TRUE);
143 G_GNUC_END_IGNORE_DEPRECATIONS
145 if(!GTK_IS_WIDGET(menu_tray->tray))
146 menu_tray->tray = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
148 settings =
149 gtk_settings_get_for_screen(gtk_widget_get_screen(widget));
151 if(gtk_icon_size_lookup_for_settings(settings, GTK_ICON_SIZE_MENU,
152 NULL, &height))
154 gtk_widget_set_size_request(widget, -1, height);
157 gtk_widget_show(menu_tray->tray);
160 /******************************************************************************
161 * API
162 *****************************************************************************/
163 GType
164 pidgin_menu_tray_get_type(void) {
165 static GType type = 0;
167 if(type == 0) {
168 static const GTypeInfo info = {
169 sizeof(PidginMenuTrayClass),
170 NULL,
171 NULL,
172 (GClassInitFunc)pidgin_menu_tray_class_init,
173 NULL,
174 NULL,
175 sizeof(PidginMenuTray),
177 (GInstanceInitFunc)pidgin_menu_tray_init,
178 NULL
181 type = g_type_register_static(GTK_TYPE_MENU_ITEM,
182 "PidginMenuTray",
183 &info, 0);
186 return type;
189 GtkWidget *
190 pidgin_menu_tray_new() {
191 return g_object_new(PIDGIN_TYPE_MENU_TRAY, NULL);
194 GtkWidget *
195 pidgin_menu_tray_get_box(PidginMenuTray *menu_tray) {
196 g_return_val_if_fail(PIDGIN_IS_MENU_TRAY(menu_tray), NULL);
197 return menu_tray->tray;
200 static void
201 pidgin_menu_tray_add(PidginMenuTray *menu_tray, GtkWidget *widget,
202 const char *tooltip, gboolean prepend)
204 g_return_if_fail(PIDGIN_IS_MENU_TRAY(menu_tray));
205 g_return_if_fail(GTK_IS_WIDGET(widget));
207 if (!gtk_widget_get_has_window(widget))
209 GtkWidget *event;
211 event = gtk_event_box_new();
212 gtk_container_add(GTK_CONTAINER(event), widget);
213 gtk_widget_show(event);
214 widget = event;
217 pidgin_menu_tray_set_tooltip(menu_tray, widget, tooltip);
219 if (prepend)
220 gtk_box_pack_start(GTK_BOX(menu_tray->tray), widget, FALSE, FALSE, 0);
221 else
222 gtk_box_pack_end(GTK_BOX(menu_tray->tray), widget, FALSE, FALSE, 0);
225 void
226 pidgin_menu_tray_append(PidginMenuTray *menu_tray, GtkWidget *widget, const char *tooltip)
228 pidgin_menu_tray_add(menu_tray, widget, tooltip, FALSE);
231 void
232 pidgin_menu_tray_prepend(PidginMenuTray *menu_tray, GtkWidget *widget, const char *tooltip)
234 pidgin_menu_tray_add(menu_tray, widget, tooltip, TRUE);
237 void
238 pidgin_menu_tray_set_tooltip(PidginMenuTray *menu_tray, GtkWidget *widget, const char *tooltip)
240 /* Should we check whether widget is a child of menu_tray? */
243 * If the widget does not have its own window, then it
244 * must have automatically been added to an event box
245 * when it was added to the menu tray. If this is the
246 * case, we want to set the tooltip on the widget's parent,
247 * not on the widget itself.
249 if (!gtk_widget_get_has_window(widget))
250 widget = gtk_widget_get_parent(widget);
252 gtk_widget_set_tooltip_text(widget, tooltip);