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