2 * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
3 * Copyright (C) 2005 Hans Breuer <hans@breuer.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * This is an implementation of the gtktrayicon interface *not*
23 * following the freedesktop.org "system tray" spec,
24 * http://www.freedesktop.org/wiki/Standards/systemtray-spec
32 #include "gtkprivate.h"
33 #include "gtktrayicon.h"
36 #include "gtkiconfactory.h"
38 #include "win32/gdkwin32.h"
42 #define WIN32_MEAN_AND_LEAN
45 #define WM_GTK_TRAY_NOTIFICATION (WM_USER+1)
47 struct _GtkTrayIconPrivate
52 static void gtk_tray_icon_add (GtkContainer *container,
54 static void gtk_tray_icon_size_request (GtkWidget *widget,
55 GtkRequisition *requisition);
56 static void gtk_tray_icon_size_allocate (GtkWidget *widget,
57 GtkAllocation *allocation);
58 static void gtk_tray_icon_finalize (GObject *object);
61 G_DEFINE_TYPE (GtkTrayIcon, gtk_tray_icon, GTK_TYPE_PLUG)
64 gtk_tray_icon_class_init (GtkTrayIconClass *class)
66 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
67 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
68 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
70 gobject_class->finalize = gtk_tray_icon_finalize;
72 widget_class->size_request = gtk_tray_icon_size_request;
73 //widget_class->size_allocate = gtk_tray_icon_size_allocate;
75 container_class->add = gtk_tray_icon_add;
77 g_type_class_add_private (class, sizeof (GtkTrayIconPrivate));
80 static LRESULT CALLBACK
81 _win32_on_tray_change (HWND hwnd,
86 if (WM_GTK_TRAY_NOTIFICATION == message)
88 GdkEventButton e = {0, };
89 GtkTrayIcon *tray_icon = GTK_TRAY_ICON (wparam);
93 g_print ("GtkTrayIcon::WM_MOUSEMOVE\n");
95 case WM_RBUTTONDBLCLK :
96 case WM_LBUTTONDBLCLK :
97 g_print ("GtkTrayIcon::WM_LBUTTONDBLCLK\n");
98 e.type = GDK_2BUTTON_PRESS;
99 e.button = (WM_LBUTTONDBLCLK == lparam) ? 1 : 3;
103 /* deliberately ignored */
105 case WM_LBUTTONDOWN :
106 case WM_RBUTTONDOWN :
107 g_print ("GtkTrayIcon::WM_RBUTTONUP\n");
108 e.type = GDK_BUTTON_PRESS;
109 e.button = (WM_LBUTTONDOWN == lparam) ? 1 : 3;
112 g_print ("GtkTrayIcon::%d\n", lparam);
115 g_signal_emit_by_name (tray_icon, "button-press-event", &e);
120 return DefWindowProc (hwnd, message, wparam, lparam);
125 _gdk_win32_create_tray_observer (void)
128 static HWND hwnd = NULL;
130 HINSTANCE hmodule = GetModuleHandle (NULL);
135 memset (&wclass, 0, sizeof(WNDCLASS));
136 wclass.lpszClassName = "GtkTrayNotification";
137 wclass.lpfnWndProc = _win32_on_tray_change;
138 wclass.hInstance = hmodule;
140 klass = RegisterClass (&wclass);
144 hwnd = CreateWindow (MAKEINTRESOURCE(klass),
146 0, 0, 16, 16, NULL, NULL,
150 UnregisterClass (MAKEINTRESOURCE(klass), hmodule);
157 gtk_tray_icon_init (GtkTrayIcon *icon)
159 icon->priv = G_TYPE_INSTANCE_GET_PRIVATE (icon, GTK_TYPE_TRAY_ICON,
161 memset (&icon->priv->nid, 0, sizeof (NOTIFYICONDATA));
163 icon->priv->nid.hWnd = _gdk_win32_create_tray_observer ();
164 icon->priv->nid.uID = GPOINTER_TO_UINT (icon);
165 icon->priv->nid.uCallbackMessage = WM_GTK_TRAY_NOTIFICATION;
166 icon->priv->nid.uFlags = NIF_ICON|NIF_MESSAGE; //NIF_TIP
170 gtk_tray_icon_finalize (GObject *object)
172 GtkTrayIcon *icon = GTK_TRAY_ICON (object);
174 Shell_NotifyIcon (NIM_DELETE, &icon->priv->nid);
176 G_OBJECT_CLASS (gtk_tray_icon_parent_class)->finalize (object);
180 tray_image_changed (GObject *object,
186 GdkPixbuf *pixbuf = NULL;
187 GtkTrayIcon *icon = GTK_TRAY_ICON (data);
189 g_return_if_fail (GTK_IS_IMAGE (object));
190 g_return_if_fail (GTK_IS_TRAY_ICON (data));
192 widget = GTK_WIDGET (object);
193 image = GTK_IMAGE (object);
196 * TODO: If nothing changed don't do nothing.
197 * we get called three times for one change - for 'size',
198 * 'storage-type', 'pixbuf' - could be cached.
199 * But 'visible'(==FALSE) needs to be handled!
201 switch (image->storage_type)
203 case GTK_IMAGE_PIXBUF :
204 pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (object));
205 g_object_ref (pixbuf);
206 g_print ("GtkTrayIcon::image_changed size=%dx%d\n",
207 pixbuf ? gdk_pixbuf_get_width (pixbuf) : 0,
208 pixbuf ? gdk_pixbuf_get_height (pixbuf) : 0);
210 case GTK_IMAGE_EMPTY :
211 g_print ("GtkTrayIcon::image_changed EMPTY\n");
213 case GTK_IMAGE_ICON_NAME :
215 GtkIconSet *icon_set = NULL;
216 const char* name = NULL;
217 gtk_image_get_icon_name (image, &name, NULL);
218 g_print ("GtkTrayIcon::image_changed '%s'\n", name);
220 icon_set = gtk_style_lookup_icon_set (widget->style, name);
222 pixbuf = gtk_icon_set_render_icon (icon_set,
224 gtk_widget_get_direction (GTK_WIDGET(icon)),
226 GTK_ICON_SIZE_BUTTON,
231 g_print ("GtkTrayIcon::image_changed %d\n", image->storage_type);
237 HICON hIcon = icon->priv->nid.hIcon;
239 icon->priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf);
241 Shell_NotifyIcon (hIcon ? NIM_MODIFY : NIM_ADD, &icon->priv->nid);
244 g_object_unref (pixbuf);
249 gtk_tray_icon_add (GtkContainer *container,
252 g_return_if_fail (GTK_IS_IMAGE (widget));
254 if (GTK_CONTAINER_CLASS (gtk_tray_icon_parent_class)->add)
255 GTK_CONTAINER_CLASS (gtk_tray_icon_parent_class)->add (container, widget);
257 g_signal_connect (widget,
259 G_CALLBACK (tray_image_changed),
264 gtk_tray_icon_size_request (GtkWidget *widget,
265 GtkRequisition *requisition)
267 requisition->width = 16;
268 requisition->height = 16;
272 gtk_tray_icon_size_allocate (GtkWidget *widget,
273 GtkAllocation *allocation)
275 widget->allocation = *allocation;
279 _gtk_tray_icon_new (const gchar *name)
281 return g_object_new (GTK_TYPE_TRAY_ICON,
287 _gtk_tray_icon_get_orientation (GtkTrayIcon *icon)
289 g_return_val_if_fail (GTK_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
290 //FIXME: should we deliver the orientation of the tray ??
291 return GTK_ORIENTATION_VERTICAL;