Cosmetic and very minor fix of markup.
[privoxy.git] / 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/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  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 static UINT g_traycreatedmsg;
82
83 static LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
84
85
86 /*********************************************************************
87  *
88  * Function    :  CreateTrayWindow
89  *
90  * Description :  Creates and returns the invisible window responsible
91  *                for processing tray messages.
92  *
93  * Parameters  :
94  *          1  :  hInstance = instance handle of this application
95  *
96  * Returns     :  Handle of the systray window.
97  *
98  *********************************************************************/
99 HWND CreateTrayWindow(HINSTANCE hInstance)
100 {
101    WNDCLASS wc;
102    static const char *szWndName = "PrivoxyTrayWindow";
103
104    wc.style          = 0;
105    wc.lpfnWndProc    = TrayProc;
106    wc.cbClsExtra     = 0;
107    wc.cbWndExtra     = 0;
108    wc.hInstance      = hInstance;
109    wc.hIcon          = 0;
110    wc.hCursor        = 0;
111    wc.hbrBackground  = 0;
112    wc.lpszMenuName   = 0;
113    wc.lpszClassName  = szWndName;
114
115    RegisterClass(&wc);
116
117    /* TaskbarCreated is sent to a window when it should re-add its tray icons */
118    g_traycreatedmsg = RegisterWindowMessage("TaskbarCreated");  
119
120    g_hwndTrayX = CreateWindow(szWndName, szWndName,
121       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
122       CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
123
124    ShowWindow(g_hwndTrayX, SW_HIDE);
125    UpdateWindow(g_hwndTrayX);
126
127    g_hmenuTray = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
128
129    return g_hwndTrayX;
130
131 }
132
133
134 /*********************************************************************
135  *
136  * Function    :  TraySetIcon
137  *
138  * Description :  Sets the tray icon to the specified shape.
139  *
140  * Parameters  :
141  *          1  :  hwnd = handle of the systray window
142  *          2  :  uID = user message number to notify systray window
143  *          3  :  hicon = set the current icon to this handle
144  *
145  * Returns     :  Same value as `Shell_NotifyIcon'.
146  *
147  *********************************************************************/
148 BOOL TraySetIcon(HWND hwnd, UINT uID, HICON hicon)
149 {
150    NOTIFYICONDATA nid;
151
152    memset(&nid, 0, sizeof(nid));
153
154    nid.cbSize = sizeof(nid);
155    nid.hWnd = hwnd;
156    nid.uID = uID;
157    nid.uFlags = NIF_ICON;
158    nid.uCallbackMessage = 0;
159    nid.hIcon = hicon;
160
161    return( Shell_NotifyIcon(NIM_MODIFY, &nid) );
162
163 }
164
165
166 /*********************************************************************
167  *
168  * Function    :  TrayAddIcon
169  *
170  * Description :  Adds a tray icon.
171  *
172  * Parameters  :
173  *          1  :  hwnd = handle of the systray window
174  *          2  :  uID = user message number to notify systray window
175  *          3  :  hicon = handle of icon to add to systray window
176  *          4  :  pszToolTip = tool tip when mouse hovers over systray window
177  *
178  * Returns     :  Same as `Shell_NotifyIcon'.
179  *
180  *********************************************************************/
181 BOOL TrayAddIcon(HWND hwnd, UINT uID, HICON hicon, const char *pszToolTip)
182 {
183    NOTIFYICONDATA nid;
184
185    memset(&nid, 0, sizeof(nid));
186
187    nid.cbSize = sizeof(nid);
188    nid.hWnd = hwnd;
189    nid.uID = uID;
190    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
191    nid.uCallbackMessage = WM_TRAYMSG;
192    nid.hIcon = hicon;
193
194    if (pszToolTip)
195    {
196       strcpy(nid.szTip, pszToolTip);
197    }
198
199    return( Shell_NotifyIcon(NIM_ADD, &nid) );
200
201 }
202
203
204 /*********************************************************************
205  *
206  * Function    :  TrayDeleteIcon
207  *
208  * Description :  Deletes a tray icon.
209  *
210  * Parameters  :
211  *          1  :  hwnd = handle of the systray window
212  *          2  :  uID = user message number to notify systray window
213  *
214  * Returns     :  Same as `Shell_NotifyIcon'.
215  *
216  *********************************************************************/
217 BOOL TrayDeleteIcon(HWND hwnd, UINT uID)
218 {
219    NOTIFYICONDATA nid;
220
221    memset(&nid, 0, sizeof(nid));
222
223    nid.cbSize = sizeof(nid);
224    nid.hWnd = hwnd;
225    nid.uID = uID;
226
227    return( Shell_NotifyIcon(NIM_DELETE, &nid) );
228
229 }
230
231
232 /*********************************************************************
233  *
234  * Function    :  TrayProc
235  *
236  * Description :  Call back procedure processes tray messages.
237  *
238  * Parameters  :
239  *          1  :  hwnd = handle of the systray window
240  *          2  :  msg = message number
241  *          3  :  wParam = first param for this message
242  *          4  :  lParam = next param for this message
243  *
244  * Returns     :  Appropriate M$ window message handler codes.
245  *
246  *********************************************************************/
247 LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
248 {
249    switch (msg)
250    {
251       case WM_CREATE:
252          return 0;
253
254       case WM_CLOSE:
255          PostQuitMessage(0);
256          return 0;
257
258       case WM_TRAYMSG:
259       {
260          /* UINT uID = (UINT) wParam; */
261          UINT uMouseMsg = (UINT) lParam;
262
263          if (uMouseMsg == WM_RBUTTONDOWN)
264          {
265             POINT pt;
266             HMENU hmenu = GetSubMenu(g_hmenuTray,0);
267             GetCursorPos(&pt);
268             SetForegroundWindow(g_hwndLogFrame);
269             TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, g_hwndLogFrame, NULL);
270             PostMessage(g_hwndLogFrame, WM_NULL, 0, 0 ) ;
271          }
272          else if (uMouseMsg == WM_LBUTTONDBLCLK)
273          {
274             ShowLogWindow(TRUE);
275          }
276       }
277       return 0;
278
279       default:
280
281          if (msg == g_traycreatedmsg)
282          {
283             TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
284          }
285          break;
286    }
287
288    return DefWindowProc(hwnd, msg, wParam, lParam);
289
290 }
291
292
293 #endif /* ndef _WIN_CONSOLE - entire file */
294
295 /*
296   Local Variables:
297   tab-width: 3
298   end:
299 */