also backup doc/pdf
[privoxy.git] / w32taskbar.c
1 const char w32taskbar_rcs[] = "$Id: w32taskbar.c,v 1.7.2.2 2003/03/19 21:27:42 gliptak Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/Attic/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.7.2.2  2003/03/19 21:27:42  gliptak
36  *    Corrected compilation error/typo
37  *
38  *    Revision 1.7.2.1  2002/11/20 14:39:32  oes
39  *    Applied patch by Mattes Dolak which adds re-creation of the win32 taskbar
40  *    icon on reception of the "TaskbarCreated" window message.
41  *
42  *    Revision 1.7  2002/03/31 17:19:00  jongfoster
43  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
44  *
45  *    Revision 1.6  2002/03/26 22:57:10  jongfoster
46  *    Web server name should begin www.
47  *
48  *    Revision 1.5  2002/03/24 12:03:47  jongfoster
49  *    Name change
50  *
51  *    Revision 1.4  2001/11/16 00:46:31  jongfoster
52  *    Fixing compiler warnings
53  *
54  *    Revision 1.3  2001/05/22 18:56:28  oes
55  *    CRLF -> LF
56  *
57  *    Revision 1.2  2001/05/20 15:07:54  jongfoster
58  *    File is now ignored if _WIN_CONSOLE is defined.
59  *
60  *    Revision 1.1.1.1  2001/05/15 13:59:08  oes
61  *    Initial import of version 2.9.3 source tree
62  *
63  *
64  *********************************************************************/
65 \f
66
67 #include "config.h"
68
69 #include <stdio.h>
70
71 #ifndef STRICT
72 #define STRICT
73 #endif
74 #include <windows.h>
75
76 #include "w32taskbar.h"
77 #include "w32res.h"
78 #include "w32log.h"
79
80 const char w32taskbar_h_rcs[] = W32TASKBAR_H_VERSION;
81
82 #ifndef _WIN_CONSOLE /* entire file */
83
84 #define WM_TRAYMSG WM_USER+1
85
86 static HMENU g_hmenuTray;
87 static HWND g_hwndTrayX;
88 static UINT g_traycreatedmsg;
89
90 static LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
91
92
93 /*********************************************************************
94  *
95  * Function    :  CreateTrayWindow
96  *
97  * Description :  Creates and returns the invisible window responsible
98  *                for processing tray messages.
99  *
100  * Parameters  :
101  *          1  :  hInstance = instance handle of this application
102  *
103  * Returns     :  Handle of the systray window.
104  *
105  *********************************************************************/
106 HWND CreateTrayWindow(HINSTANCE hInstance)
107 {
108    WNDCLASS wc;
109    static const char *szWndName = "PrivoxyTrayWindow";
110
111    wc.style          = 0;
112    wc.lpfnWndProc    = TrayProc;
113    wc.cbClsExtra     = 0;
114    wc.cbWndExtra     = 0;
115    wc.hInstance      = hInstance;
116    wc.hIcon          = 0;
117    wc.hCursor        = 0;
118    wc.hbrBackground  = 0;
119    wc.lpszMenuName   = 0;
120    wc.lpszClassName  = szWndName;
121
122    RegisterClass(&wc);
123
124    /* TaskbarCreated is sent to a window when it should re-add its tray icons */
125    g_traycreatedmsg = RegisterWindowMessage("TaskbarCreated");  
126
127    g_hwndTrayX = CreateWindow(szWndName, szWndName,
128       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
129       CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
130
131    ShowWindow(g_hwndTrayX, SW_HIDE);
132    UpdateWindow(g_hwndTrayX);
133
134    g_hmenuTray = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
135
136    return g_hwndTrayX;
137
138 }
139
140
141 /*********************************************************************
142  *
143  * Function    :  TraySetIcon
144  *
145  * Description :  Sets the tray icon to the specified shape.
146  *
147  * Parameters  :
148  *          1  :  hwnd = handle of the systray window
149  *          2  :  uID = user message number to notify systray window
150  *          3  :  hicon = set the current icon to this handle
151  *
152  * Returns     :  Same value as `Shell_NotifyIcon'.
153  *
154  *********************************************************************/
155 BOOL TraySetIcon(HWND hwnd, UINT uID, HICON hicon)
156 {
157    NOTIFYICONDATA nid;
158
159    memset(&nid, 0, sizeof(nid));
160
161    nid.cbSize = sizeof(nid);
162    nid.hWnd = hwnd;
163    nid.uID = uID;
164    nid.uFlags = NIF_ICON;
165    nid.uCallbackMessage = 0;
166    nid.hIcon = hicon;
167
168    return( Shell_NotifyIcon(NIM_MODIFY, &nid) );
169
170 }
171
172
173 /*********************************************************************
174  *
175  * Function    :  TrayAddIcon
176  *
177  * Description :  Adds a tray icon.
178  *
179  * Parameters  :
180  *          1  :  hwnd = handle of the systray window
181  *          2  :  uID = user message number to notify systray window
182  *          3  :  hicon = handle of icon to add to systray window
183  *          4  :  pszToolTip = tool tip when mouse hovers over systray window
184  *
185  * Returns     :  Same as `Shell_NotifyIcon'.
186  *
187  *********************************************************************/
188 BOOL TrayAddIcon(HWND hwnd, UINT uID, HICON hicon, const char *pszToolTip)
189 {
190    NOTIFYICONDATA nid;
191
192    memset(&nid, 0, sizeof(nid));
193
194    nid.cbSize = sizeof(nid);
195    nid.hWnd = hwnd;
196    nid.uID = uID;
197    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
198    nid.uCallbackMessage = WM_TRAYMSG;
199    nid.hIcon = hicon;
200
201    if (pszToolTip)
202    {
203       strcpy(nid.szTip, pszToolTip);
204    }
205
206    return( Shell_NotifyIcon(NIM_ADD, &nid) );
207
208 }
209
210
211 /*********************************************************************
212  *
213  * Function    :  TrayDeleteIcon
214  *
215  * Description :  Deletes a tray icon.
216  *
217  * Parameters  :
218  *          1  :  hwnd = handle of the systray window
219  *          2  :  uID = user message number to notify systray window
220  *
221  * Returns     :  Same as `Shell_NotifyIcon'.
222  *
223  *********************************************************************/
224 BOOL TrayDeleteIcon(HWND hwnd, UINT uID)
225 {
226    NOTIFYICONDATA nid;
227
228    memset(&nid, 0, sizeof(nid));
229
230    nid.cbSize = sizeof(nid);
231    nid.hWnd = hwnd;
232    nid.uID = uID;
233
234    return( Shell_NotifyIcon(NIM_DELETE, &nid) );
235
236 }
237
238
239 /*********************************************************************
240  *
241  * Function    :  TrayProc
242  *
243  * Description :  Call back procedure processes tray messages.
244  *
245  * Parameters  :
246  *          1  :  hwnd = handle of the systray window
247  *          2  :  msg = message number
248  *          3  :  wParam = first param for this message
249  *          4  :  lParam = next param for this message
250  *
251  * Returns     :  Appropriate M$ window message handler codes.
252  *
253  *********************************************************************/
254 LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
255 {
256    switch (msg)
257    {
258       case WM_CREATE:
259          return 0;
260
261       case WM_CLOSE:
262          PostQuitMessage(0);
263          return 0;
264
265       case WM_TRAYMSG:
266       {
267          /* UINT uID = (UINT) wParam; */
268          UINT uMouseMsg = (UINT) lParam;
269
270          if (uMouseMsg == WM_RBUTTONDOWN)
271          {
272             POINT pt;
273             HMENU hmenu = GetSubMenu(g_hmenuTray,0);
274             GetCursorPos(&pt);
275             SetForegroundWindow(g_hwndLogFrame);
276             TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, g_hwndLogFrame, NULL);
277             PostMessage(g_hwndLogFrame, WM_NULL, 0, 0 ) ;
278          }
279          else if (uMouseMsg == WM_LBUTTONDBLCLK)
280          {
281             ShowLogWindow(TRUE);
282          }
283       }
284       return 0;
285
286       default:
287
288          if (msg == g_traycreatedmsg)
289          {
290             TrayAddIcon(g_hwndTrayX, 1, g_hiconApp, "Privoxy");
291          }
292          break;
293    }
294
295    return DefWindowProc(hwnd, msg, wParam, lParam);
296
297 }
298
299
300 #endif /* ndef _WIN_CONSOLE - entire file */
301
302 /*
303   Local Variables:
304   tab-width: 3
305   end:
306 */