Check requests more carefully before serving them forcefully
[privoxy.git] / w32taskbar.c
1 const char w32taskbar_rcs[] = "$Id: w32taskbar.c,v 1.13 2012/03/09 16:23:50 fabiankeil 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  *********************************************************************/
34
35
36 #include "config.h"
37
38 #include <stdio.h>
39
40 #ifndef STRICT
41 #define STRICT
42 #endif
43 #include <windows.h>
44
45 #include "w32taskbar.h"
46 #include "w32res.h"
47 #include "w32log.h"
48
49 const char w32taskbar_h_rcs[] = W32TASKBAR_H_VERSION;
50
51 #ifndef _WIN_CONSOLE /* entire file */
52
53 #define WM_TRAYMSG WM_USER+1
54
55 static HMENU g_hmenuTray;
56 static HWND g_hwndTrayX;
57 static UINT g_traycreatedmsg;
58
59 static LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
60
61
62 /*********************************************************************
63  *
64  * Function    :  CreateTrayWindow
65  *
66  * Description :  Creates and returns the invisible window responsible
67  *                for processing tray messages.
68  *
69  * Parameters  :
70  *          1  :  hInstance = instance handle of this application
71  *
72  * Returns     :  Handle of the systray window.
73  *
74  *********************************************************************/
75 HWND CreateTrayWindow(HINSTANCE hInstance)
76 {
77    WNDCLASS wc;
78    static const char *szWndName = "PrivoxyTrayWindow";
79
80    wc.style          = 0;
81    wc.lpfnWndProc    = TrayProc;
82    wc.cbClsExtra     = 0;
83    wc.cbWndExtra     = 0;
84    wc.hInstance      = hInstance;
85    wc.hIcon          = 0;
86    wc.hCursor        = 0;
87    wc.hbrBackground  = 0;
88    wc.lpszMenuName   = 0;
89    wc.lpszClassName  = szWndName;
90
91    RegisterClass(&wc);
92
93    /* TaskbarCreated is sent to a window when it should re-add its tray icons */
94    g_traycreatedmsg = RegisterWindowMessage("TaskbarCreated");
95
96    g_hwndTrayX = CreateWindow(szWndName, szWndName,
97       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
98       CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
99
100    ShowWindow(g_hwndTrayX, SW_HIDE);
101    UpdateWindow(g_hwndTrayX);
102
103    g_hmenuTray = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
104
105    return g_hwndTrayX;
106
107 }
108
109
110 /*********************************************************************
111  *
112  * Function    :  TraySetIcon
113  *
114  * Description :  Sets the tray icon to the specified shape.
115  *
116  * Parameters  :
117  *          1  :  hwnd = handle of the systray window
118  *          2  :  uID = user message number to notify systray window
119  *          3  :  hicon = set the current icon to this handle
120  *
121  * Returns     :  Same value as `Shell_NotifyIcon'.
122  *
123  *********************************************************************/
124 BOOL TraySetIcon(HWND hwnd, UINT uID, HICON hicon)
125 {
126    NOTIFYICONDATA nid;
127
128    memset(&nid, 0, sizeof(nid));
129
130    nid.cbSize = sizeof(nid);
131    nid.hWnd = hwnd;
132    nid.uID = uID;
133    nid.uFlags = NIF_ICON;
134    nid.uCallbackMessage = 0;
135    nid.hIcon = hicon;
136
137    return(Shell_NotifyIcon(NIM_MODIFY, &nid));
138
139 }
140
141
142 /*********************************************************************
143  *
144  * Function    :  TrayAddIcon
145  *
146  * Description :  Adds a tray icon.
147  *
148  * Parameters  :
149  *          1  :  hwnd = handle of the systray window
150  *          2  :  uID = user message number to notify systray window
151  *          3  :  hicon = handle of icon to add to systray window
152  *          4  :  pszToolTip = tool tip when mouse hovers over systray window
153  *
154  * Returns     :  Same as `Shell_NotifyIcon'.
155  *
156  *********************************************************************/
157 BOOL TrayAddIcon(HWND hwnd, UINT uID, HICON hicon, const char *pszToolTip)
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_MESSAGE | NIF_ICON | NIF_TIP;
167    nid.uCallbackMessage = WM_TRAYMSG;
168    nid.hIcon = hicon;
169
170    if (pszToolTip)
171    {
172       strcpy(nid.szTip, pszToolTip);
173    }
174
175    return(Shell_NotifyIcon(NIM_ADD, &nid));
176
177 }
178
179
180 /*********************************************************************
181  *
182  * Function    :  TrayDeleteIcon
183  *
184  * Description :  Deletes a tray icon.
185  *
186  * Parameters  :
187  *          1  :  hwnd = handle of the systray window
188  *          2  :  uID = user message number to notify systray window
189  *
190  * Returns     :  Same as `Shell_NotifyIcon'.
191  *
192  *********************************************************************/
193 BOOL TrayDeleteIcon(HWND hwnd, UINT uID)
194 {
195    NOTIFYICONDATA nid;
196
197    memset(&nid, 0, sizeof(nid));
198
199    nid.cbSize = sizeof(nid);
200    nid.hWnd = hwnd;
201    nid.uID = uID;
202
203    return(Shell_NotifyIcon(NIM_DELETE, &nid));
204
205 }
206
207
208 /*********************************************************************
209  *
210  * Function    :  TrayProc
211  *
212  * Description :  Call back procedure processes tray messages.
213  *
214  * Parameters  :
215  *          1  :  hwnd = handle of the systray window
216  *          2  :  msg = message number
217  *          3  :  wParam = first param for this message
218  *          4  :  lParam = next param for this message
219  *
220  * Returns     :  Appropriate M$ window message handler codes.
221  *
222  *********************************************************************/
223 LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
224 {
225    switch (msg)
226    {
227       case WM_CREATE:
228          return 0;
229
230       case WM_CLOSE:
231          PostQuitMessage(0);
232          return 0;
233
234       case WM_TRAYMSG:
235       {
236          /* UINT uID = (UINT) wParam; */
237          UINT uMouseMsg = (UINT) lParam;
238
239          if (uMouseMsg == WM_RBUTTONDOWN)
240          {
241             POINT pt;
242             HMENU hmenu = GetSubMenu(g_hmenuTray,0);
243             GetCursorPos(&pt);
244             SetForegroundWindow(g_hwndLogFrame);
245             TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, g_hwndLogFrame, NULL);
246             PostMessage(g_hwndLogFrame, WM_NULL, 0, 0);
247          }
248          else if (uMouseMsg == WM_LBUTTONDBLCLK)
249          {
250             ShowLogWindow(TRUE);
251          }
252       }
253       return 0;
254
255       default:
256
257          if (msg == g_traycreatedmsg)
258          {
259             TrayAddIcon(g_hwndTrayX, 1, g_hiconApp, "Privoxy");
260          }
261          break;
262    }
263
264    return DefWindowProc(hwnd, msg, wParam, lParam);
265
266 }
267
268
269 #endif /* ndef _WIN_CONSOLE - entire file */
270
271 /*
272   Local Variables:
273   tab-width: 3
274   end:
275 */