]> Pileus Git - ~andy/gtk/blob - gtk/gtktrayicon-win32.c
Estonian translation update by Ivar Smolin.
[~andy/gtk] / gtk / gtktrayicon-win32.c
1 /* gtktrayicon.c
2  * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
3  * Copyright (C) 2005 Hans Breuer  <hans@breuer.org>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /*
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
25  */
26
27 #include <config.h>
28 #include <string.h>
29 #include <libintl.h>
30
31 #include "gtkintl.h"
32 #include "gtkprivate.h"
33 #include "gtktrayicon.h"
34
35 #include "gtkimage.h"
36 #include "gtkiconfactory.h"
37
38 #include "win32/gdkwin32.h"
39
40 #include "gtkalias.h"
41
42 #define WIN32_MEAN_AND_LEAN
43 #include <windows.h>
44
45 #define WM_GTK_TRAY_NOTIFICATION (WM_USER+1)
46
47 struct _GtkTrayIconPrivate
48 {
49   NOTIFYICONDATA nid;
50 };
51
52 static void gtk_tray_icon_add (GtkContainer   *container,
53                                GtkWidget      *widget);
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);
59
60
61 G_DEFINE_TYPE (GtkTrayIcon, gtk_tray_icon, GTK_TYPE_PLUG)
62
63 static void
64 gtk_tray_icon_class_init (GtkTrayIconClass *class)
65 {
66   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
67   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
68   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
69
70   gobject_class->finalize     = gtk_tray_icon_finalize;
71
72   widget_class->size_request = gtk_tray_icon_size_request;
73   //widget_class->size_allocate = gtk_tray_icon_size_allocate;
74
75   container_class->add = gtk_tray_icon_add;
76
77   g_type_class_add_private (class, sizeof (GtkTrayIconPrivate));
78 }
79
80 static LRESULT CALLBACK
81 _win32_on_tray_change (HWND   hwnd,
82                        UINT   message,
83                        WPARAM wparam,
84                        LPARAM lparam)
85 {
86   if (WM_GTK_TRAY_NOTIFICATION == message)
87     {
88       GdkEventButton e = {0, };
89       GtkTrayIcon *tray_icon = GTK_TRAY_ICON (wparam);
90       switch (lparam)
91         {
92         case WM_MOUSEMOVE :
93           g_print ("GtkTrayIcon::WM_MOUSEMOVE\n");
94           break;
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;
100           break;
101         case WM_LBUTTONUP :
102         case WM_RBUTTONUP :
103           /* deliberately ignored */
104           break;
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;
110           break;
111         default :
112           g_print ("GtkTrayIcon::%d\n", lparam);
113           break;
114         }
115         g_signal_emit_by_name (tray_icon, "button-press-event", &e);
116         return 0;
117     }
118   else
119     {
120       return DefWindowProc (hwnd, message, wparam, lparam);
121     }
122 }
123
124 HWND
125 _gdk_win32_create_tray_observer (void)
126 {
127   WNDCLASS    wclass;
128   static HWND hwnd = NULL;
129   ATOM        klass;
130   HINSTANCE   hmodule = GetModuleHandle (NULL);
131
132   if (hwnd)
133     return hwnd;
134
135   memset (&wclass, 0, sizeof(WNDCLASS));
136   wclass.lpszClassName = "GtkTrayNotification";
137   wclass.lpfnWndProc   = _win32_on_tray_change;
138   wclass.hInstance     = hmodule;
139
140   klass = RegisterClass (&wclass);
141   if (!klass)
142     return NULL;
143
144   hwnd = CreateWindow (MAKEINTRESOURCE(klass),
145                        NULL, WS_POPUP,
146                        0, 0, 16, 16, NULL, NULL,
147                        hmodule, NULL);
148   if (!hwnd)
149     {
150       UnregisterClass (MAKEINTRESOURCE(klass), hmodule);
151       return NULL;
152     }
153   return hwnd;
154 }
155
156 static void
157 gtk_tray_icon_init (GtkTrayIcon *icon)
158 {
159   icon->priv = G_TYPE_INSTANCE_GET_PRIVATE (icon, GTK_TYPE_TRAY_ICON,
160                                             GtkTrayIconPrivate);
161   memset (&icon->priv->nid, 0, sizeof (NOTIFYICONDATA));
162
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 
167 }
168
169 static void
170 gtk_tray_icon_finalize (GObject *object)
171 {
172   GtkTrayIcon *icon = GTK_TRAY_ICON (object);
173
174   Shell_NotifyIcon (NIM_DELETE, &icon->priv->nid);
175
176   G_OBJECT_CLASS (gtk_tray_icon_parent_class)->finalize (object);
177 }
178
179 static void
180 tray_image_changed (GObject    *object,
181                     GParamSpec *pspec,
182                     gpointer    data)
183 {
184   GtkImage    *image;
185   GtkWidget   *widget;
186   GdkPixbuf   *pixbuf = NULL;
187   GtkTrayIcon *icon = GTK_TRAY_ICON (data);
188
189   g_return_if_fail (GTK_IS_IMAGE (object));
190   g_return_if_fail (GTK_IS_TRAY_ICON (data));
191
192   widget = GTK_WIDGET (object);
193   image = GTK_IMAGE (object);
194
195   /*
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!
200    */
201   switch (image->storage_type)
202   {
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);
209     break;
210   case GTK_IMAGE_EMPTY :
211     g_print ("GtkTrayIcon::image_changed EMPTY\n");
212     break;
213   case GTK_IMAGE_ICON_NAME :
214     {
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);
219
220       icon_set = gtk_style_lookup_icon_set (widget->style, name);
221
222       pixbuf = gtk_icon_set_render_icon (icon_set, 
223                                          widget->style,
224                                          gtk_widget_get_direction (GTK_WIDGET(icon)),
225                                          GTK_STATE_NORMAL,
226                                          GTK_ICON_SIZE_BUTTON,
227                                          widget, NULL); 
228     }
229     break;
230   default :
231     g_print ("GtkTrayIcon::image_changed %d\n", image->storage_type);
232     break;
233   }
234
235   if (pixbuf)
236     {
237       HICON hIcon = icon->priv->nid.hIcon;
238
239       icon->priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf);
240
241       Shell_NotifyIcon (hIcon ? NIM_MODIFY : NIM_ADD, &icon->priv->nid);
242       if (hIcon)
243         DestroyIcon (hIcon);
244       g_object_unref (pixbuf);
245     }
246 }
247
248 static void 
249 gtk_tray_icon_add (GtkContainer   *container,
250                    GtkWidget      *widget)
251 {
252   g_return_if_fail (GTK_IS_IMAGE (widget));
253
254   if (GTK_CONTAINER_CLASS (gtk_tray_icon_parent_class)->add)
255     GTK_CONTAINER_CLASS (gtk_tray_icon_parent_class)->add (container, widget);
256
257   g_signal_connect (widget, 
258                     "notify",
259                     G_CALLBACK (tray_image_changed),
260                     container);
261 }
262
263 static void
264 gtk_tray_icon_size_request (GtkWidget      *widget,
265                             GtkRequisition *requisition)
266 {
267   requisition->width = 16;
268   requisition->height = 16;
269 }
270
271 static void
272 gtk_tray_icon_size_allocate (GtkWidget     *widget,
273                              GtkAllocation *allocation)
274 {
275   widget->allocation = *allocation;
276 }
277
278 GtkTrayIcon *
279 _gtk_tray_icon_new (const gchar *name)
280 {
281   return g_object_new (GTK_TYPE_TRAY_ICON, 
282                        "title", name, 
283                        NULL);
284 }
285
286 GtkOrientation 
287 _gtk_tray_icon_get_orientation (GtkTrayIcon *icon)
288 {
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;
292 }