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