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