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