w32taskbar.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*********************************************************************
  2. *
  3. * File : $Source: /cvsroot/ijbswa/current/w32taskbar.c,v $
  4. *
  5. * Purpose : Functions for creating, setting and destroying the
  6. * workspace tray icon
  7. *
  8. * Copyright : Written by and Copyright (C) 2001-2002 members of
  9. * the Privoxy team. https://www.privoxy.org/
  10. *
  11. * Written by and Copyright (C) 1999 Adam Lock
  12. * <locka@iol.ie>
  13. *
  14. * This program is free software; you can redistribute it
  15. * and/or modify it under the terms of the GNU General
  16. * Public License as published by the Free Software
  17. * Foundation; either version 2 of the License, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will
  21. * be useful, but WITHOUT ANY WARRANTY; without even the
  22. * implied warranty of MERCHANTABILITY or FITNESS FOR A
  23. * PARTICULAR PURPOSE. See the GNU General Public
  24. * License for more details.
  25. *
  26. * The GNU General Public License should be included with
  27. * this file. If not, you can view it at
  28. * http://www.gnu.org/copyleft/gpl.html
  29. * or write to the Free Software Foundation, Inc., 59
  30. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  31. *
  32. *********************************************************************/
  33. #include "config.h"
  34. #include <stdio.h>
  35. #ifndef STRICT
  36. #define STRICT
  37. #endif
  38. #include <windows.h>
  39. #include "w32taskbar.h"
  40. #include "w32res.h"
  41. #include "w32log.h"
  42. #ifndef _WIN_CONSOLE /* entire file */
  43. #define WM_TRAYMSG WM_USER+1
  44. static HMENU g_hmenuTray;
  45. static HWND g_hwndTrayX;
  46. static UINT g_traycreatedmsg;
  47. static LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  48. /*********************************************************************
  49. *
  50. * Function : CreateTrayWindow
  51. *
  52. * Description : Creates and returns the invisible window responsible
  53. * for processing tray messages.
  54. *
  55. * Parameters :
  56. * 1 : hInstance = instance handle of this application
  57. *
  58. * Returns : Handle of the systray window.
  59. *
  60. *********************************************************************/
  61. HWND CreateTrayWindow(HINSTANCE hInstance)
  62. {
  63. WNDCLASS wc;
  64. static const char *szWndName = "PrivoxyTrayWindow";
  65. wc.style = 0;
  66. wc.lpfnWndProc = TrayProc;
  67. wc.cbClsExtra = 0;
  68. wc.cbWndExtra = 0;
  69. wc.hInstance = hInstance;
  70. wc.hIcon = 0;
  71. wc.hCursor = 0;
  72. wc.hbrBackground = 0;
  73. wc.lpszMenuName = 0;
  74. wc.lpszClassName = szWndName;
  75. RegisterClass(&wc);
  76. /* TaskbarCreated is sent to a window when it should re-add its tray icons */
  77. g_traycreatedmsg = RegisterWindowMessage("TaskbarCreated");
  78. g_hwndTrayX = CreateWindow(szWndName, szWndName,
  79. WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  80. CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  81. ShowWindow(g_hwndTrayX, SW_HIDE);
  82. UpdateWindow(g_hwndTrayX);
  83. g_hmenuTray = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
  84. return g_hwndTrayX;
  85. }
  86. /*********************************************************************
  87. *
  88. * Function : TraySetIcon
  89. *
  90. * Description : Sets the tray icon to the specified shape.
  91. *
  92. * Parameters :
  93. * 1 : hwnd = handle of the systray window
  94. * 2 : uID = user message number to notify systray window
  95. * 3 : hicon = set the current icon to this handle
  96. *
  97. * Returns : Same value as `Shell_NotifyIcon'.
  98. *
  99. *********************************************************************/
  100. BOOL TraySetIcon(HWND hwnd, UINT uID, HICON hicon)
  101. {
  102. NOTIFYICONDATA nid;
  103. memset(&nid, 0, sizeof(nid));
  104. nid.cbSize = sizeof(nid);
  105. nid.hWnd = hwnd;
  106. nid.uID = uID;
  107. nid.uFlags = NIF_ICON;
  108. nid.uCallbackMessage = 0;
  109. nid.hIcon = hicon;
  110. return(Shell_NotifyIcon(NIM_MODIFY, &nid));
  111. }
  112. /*********************************************************************
  113. *
  114. * Function : TrayAddIcon
  115. *
  116. * Description : Adds a tray icon.
  117. *
  118. * Parameters :
  119. * 1 : hwnd = handle of the systray window
  120. * 2 : uID = user message number to notify systray window
  121. * 3 : hicon = handle of icon to add to systray window
  122. * 4 : pszToolTip = tool tip when mouse hovers over systray window
  123. *
  124. * Returns : Same as `Shell_NotifyIcon'.
  125. *
  126. *********************************************************************/
  127. BOOL TrayAddIcon(HWND hwnd, UINT uID, HICON hicon, const char *pszToolTip)
  128. {
  129. NOTIFYICONDATA nid;
  130. memset(&nid, 0, sizeof(nid));
  131. nid.cbSize = sizeof(nid);
  132. nid.hWnd = hwnd;
  133. nid.uID = uID;
  134. nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  135. nid.uCallbackMessage = WM_TRAYMSG;
  136. nid.hIcon = hicon;
  137. if (pszToolTip)
  138. {
  139. strcpy(nid.szTip, pszToolTip);
  140. }
  141. return(Shell_NotifyIcon(NIM_ADD, &nid));
  142. }
  143. /*********************************************************************
  144. *
  145. * Function : TrayDeleteIcon
  146. *
  147. * Description : Deletes a tray icon.
  148. *
  149. * Parameters :
  150. * 1 : hwnd = handle of the systray window
  151. * 2 : uID = user message number to notify systray window
  152. *
  153. * Returns : Same as `Shell_NotifyIcon'.
  154. *
  155. *********************************************************************/
  156. BOOL TrayDeleteIcon(HWND hwnd, UINT uID)
  157. {
  158. NOTIFYICONDATA nid;
  159. memset(&nid, 0, sizeof(nid));
  160. nid.cbSize = sizeof(nid);
  161. nid.hWnd = hwnd;
  162. nid.uID = uID;
  163. return(Shell_NotifyIcon(NIM_DELETE, &nid));
  164. }
  165. /*********************************************************************
  166. *
  167. * Function : TrayProc
  168. *
  169. * Description : Call back procedure processes tray messages.
  170. *
  171. * Parameters :
  172. * 1 : hwnd = handle of the systray window
  173. * 2 : msg = message number
  174. * 3 : wParam = first param for this message
  175. * 4 : lParam = next param for this message
  176. *
  177. * Returns : Appropriate M$ window message handler codes.
  178. *
  179. *********************************************************************/
  180. LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  181. {
  182. switch (msg)
  183. {
  184. case WM_CREATE:
  185. return 0;
  186. case WM_CLOSE:
  187. PostQuitMessage(0);
  188. return 0;
  189. case WM_TRAYMSG:
  190. {
  191. /* UINT uID = (UINT) wParam; */
  192. UINT uMouseMsg = (UINT) lParam;
  193. if (uMouseMsg == WM_RBUTTONDOWN)
  194. {
  195. POINT pt;
  196. HMENU hmenu = GetSubMenu(g_hmenuTray,0);
  197. GetCursorPos(&pt);
  198. SetForegroundWindow(g_hwndLogFrame);
  199. TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, g_hwndLogFrame, NULL);
  200. PostMessage(g_hwndLogFrame, WM_NULL, 0, 0);
  201. }
  202. else if (uMouseMsg == WM_LBUTTONDBLCLK)
  203. {
  204. ShowLogWindow(TRUE);
  205. }
  206. }
  207. return 0;
  208. default:
  209. if (msg == g_traycreatedmsg)
  210. {
  211. TrayAddIcon(g_hwndTrayX, 1, g_hiconApp, "Privoxy");
  212. }
  213. break;
  214. }
  215. return DefWindowProc(hwnd, msg, wParam, lParam);
  216. }
  217. #endif /* ndef _WIN_CONSOLE - entire file */
  218. /*
  219. Local Variables:
  220. tab-width: 3
  221. end:
  222. */