More Mac OSX support:
[privoxy.git] / w32taskbar.c
1 const char w32taskbar_rcs[] = "$Id: w32taskbar.c,v 1.5 2002/03/24 12:03:47 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/w32taskbar.c,v $
5  *
6  * Purpose     :  Functions for creating, setting and destroying the
7  *                workspace tray icon
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2002 members of
10  *                the Privoxy team.  http://www.privoxy.org/
11  *
12  *                Written by and Copyright (C) 1999 Adam Lock
13  *                <locka@iol.ie>
14  *
15  *                This program is free software; you can redistribute it 
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  * Revisions   :
34  *    $Log: w32taskbar.c,v $
35  *    Revision 1.5  2002/03/24 12:03:47  jongfoster
36  *    Name change
37  *
38  *    Revision 1.4  2001/11/16 00:46:31  jongfoster
39  *    Fixing compiler warnings
40  *
41  *    Revision 1.3  2001/05/22 18:56:28  oes
42  *    CRLF -> LF
43  *
44  *    Revision 1.2  2001/05/20 15:07:54  jongfoster
45  *    File is now ignored if _WIN_CONSOLE is defined.
46  *
47  *    Revision 1.1.1.1  2001/05/15 13:59:08  oes
48  *    Initial import of version 2.9.3 source tree
49  *
50  *
51  *********************************************************************/
52 \f
53
54 #include "config.h"
55
56 #include <stdio.h>
57
58 #include <windows.h>
59
60 #include "w32taskbar.h"
61 #include "w32res.h"
62 #include "w32log.h"
63
64 const char w32taskbar_h_rcs[] = W32TASKBAR_H_VERSION;
65
66 #ifndef _WIN_CONSOLE /* entire file */
67
68 #define WM_TRAYMSG WM_USER+1
69
70 static HMENU g_hmenuTray;
71 static HWND g_hwndTrayX;
72
73 static LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
74
75
76 /*********************************************************************
77  *
78  * Function    :  CreateTrayWindow
79  *
80  * Description :  Creates and returns the invisible window responsible
81  *                for processing tray messages.
82  *
83  * Parameters  :
84  *          1  :  hInstance = instance handle of this application
85  *
86  * Returns     :  Handle of the systray window.
87  *
88  *********************************************************************/
89 HWND CreateTrayWindow(HINSTANCE hInstance)
90 {
91    WNDCLASS wc;
92    static const char *szWndName = "PrivoxyTrayWindow";
93
94    wc.style          = 0;
95    wc.lpfnWndProc    = TrayProc;
96    wc.cbClsExtra     = 0;
97    wc.cbWndExtra     = 0;
98    wc.hInstance      = hInstance;
99    wc.hIcon          = 0;
100    wc.hCursor        = 0;
101    wc.hbrBackground  = 0;
102    wc.lpszMenuName   = 0;
103    wc.lpszClassName  = szWndName;
104
105    RegisterClass(&wc);
106
107    g_hwndTrayX = CreateWindow(szWndName, szWndName,
108       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
109       CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
110
111    ShowWindow(g_hwndTrayX, SW_HIDE);
112    UpdateWindow(g_hwndTrayX);
113
114    g_hmenuTray = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
115
116    return g_hwndTrayX;
117
118 }
119
120
121 /*********************************************************************
122  *
123  * Function    :  TraySetIcon
124  *
125  * Description :  Sets the tray icon to the specified shape.
126  *
127  * Parameters  :
128  *          1  :  hwnd = handle of the systray window
129  *          2  :  uID = user message number to notify systray window
130  *          3  :  hicon = set the current icon to this handle
131  *
132  * Returns     :  Same value as `Shell_NotifyIcon'.
133  *
134  *********************************************************************/
135 BOOL TraySetIcon(HWND hwnd, UINT uID, HICON hicon)
136 {
137    NOTIFYICONDATA nid;
138
139    memset(&nid, 0, sizeof(nid));
140
141    nid.cbSize = sizeof(nid);
142    nid.hWnd = hwnd;
143    nid.uID = uID;
144    nid.uFlags = NIF_ICON;
145    nid.uCallbackMessage = 0;
146    nid.hIcon = hicon;
147
148    return( Shell_NotifyIcon(NIM_MODIFY, &nid) );
149
150 }
151
152
153 /*********************************************************************
154  *
155  * Function    :  TrayAddIcon
156  *
157  * Description :  Adds a tray icon.
158  *
159  * Parameters  :
160  *          1  :  hwnd = handle of the systray window
161  *          2  :  uID = user message number to notify systray window
162  *          3  :  hicon = handle of icon to add to systray window
163  *          4  :  pszToolTip = tool tip when mouse hovers over systray window
164  *
165  * Returns     :  Same as `Shell_NotifyIcon'.
166  *
167  *********************************************************************/
168 BOOL TrayAddIcon(HWND hwnd, UINT uID, HICON hicon, const char *pszToolTip)
169 {
170    NOTIFYICONDATA nid;
171
172    memset(&nid, 0, sizeof(nid));
173
174    nid.cbSize = sizeof(nid);
175    nid.hWnd = hwnd;
176    nid.uID = uID;
177    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
178    nid.uCallbackMessage = WM_TRAYMSG;
179    nid.hIcon = hicon;
180
181    if (pszToolTip)
182    {
183       strcpy(nid.szTip, pszToolTip);
184    }
185
186    return( Shell_NotifyIcon(NIM_ADD, &nid) );
187
188 }
189
190
191 /*********************************************************************
192  *
193  * Function    :  TrayDeleteIcon
194  *
195  * Description :  Deletes a tray icon.
196  *
197  * Parameters  :
198  *          1  :  hwnd = handle of the systray window
199  *          2  :  uID = user message number to notify systray window
200  *
201  * Returns     :  Same as `Shell_NotifyIcon'.
202  *
203  *********************************************************************/
204 BOOL TrayDeleteIcon(HWND hwnd, UINT uID)
205 {
206    NOTIFYICONDATA nid;
207
208    memset(&nid, 0, sizeof(nid));
209
210    nid.cbSize = sizeof(nid);
211    nid.hWnd = hwnd;
212    nid.uID = uID;
213
214    return( Shell_NotifyIcon(NIM_DELETE, &nid) );
215
216 }
217
218
219 /*********************************************************************
220  *
221  * Function    :  TrayProc
222  *
223  * Description :  Call back procedure processes tray messages.
224  *
225  * Parameters  :
226  *          1  :  hwnd = handle of the systray window
227  *          2  :  msg = message number
228  *          3  :  wParam = first param for this message
229  *          4  :  lParam = next param for this message
230  *
231  * Returns     :  Appropriate M$ window message handler codes.
232  *
233  *********************************************************************/
234 LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
235 {
236    switch (msg)
237    {
238       case WM_CREATE:
239          return 0;
240
241       case WM_CLOSE:
242          PostQuitMessage(0);
243          return 0;
244
245       case WM_TRAYMSG:
246       {
247          /* UINT uID = (UINT) wParam; */
248          UINT uMouseMsg = (UINT) lParam;
249
250          if (uMouseMsg == WM_RBUTTONDOWN)
251          {
252             POINT pt;
253             HMENU hmenu = GetSubMenu(g_hmenuTray,0);
254             GetCursorPos(&pt);
255             SetForegroundWindow(g_hwndLogFrame);
256             TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, g_hwndLogFrame, NULL);
257             PostMessage(g_hwndLogFrame, WM_NULL, 0, 0 ) ;
258          }
259          else if (uMouseMsg == WM_LBUTTONDBLCLK)
260          {
261             ShowLogWindow(TRUE);
262          }
263       }
264       return 0;
265
266       default:
267          /* DO NOTHING */
268          break;
269    }
270
271    return DefWindowProc(hwnd, msg, wParam, lParam);
272
273 }
274
275
276 #endif /* ndef _WIN_CONSOLE - entire file */
277
278 /*
279   Local Variables:
280   tab-width: 3
281   end:
282 */