1 const char w32log_rcs[] = "$Id: w32log.c,v 1.22 2002/03/24 12:48:23 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/w32log.c,v $
6 * Purpose : Functions for creating and destroying the log window,
7 * ouputting strings, processing messages and so on.
9 * Copyright : Written by and Copyright (C) 2001-2002 members of
10 * the Privoxy team. http://www.privoxy.org/
12 * Written by and Copyright (C) 1999 Adam Lock
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.
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.
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.
35 * Revision 1.22 2002/03/24 12:48:23 jongfoster
38 * Revision 1.21 2002/03/24 12:07:35 jongfoster
39 * Consistern name for filters file
41 * Revision 1.20 2002/03/24 12:03:47 jongfoster
44 * Revision 1.19 2002/01/17 21:04:17 jongfoster
45 * Replacing hard references to the URL of the config interface
46 * with #defines from project.h
48 * Revision 1.18 2001/11/30 23:37:24 jongfoster
49 * Renaming the Win32 config file to config.txt - this is almost the
50 * same as the corresponding UNIX name "config"
52 * Revision 1.17 2001/11/16 00:46:31 jongfoster
53 * Fixing compiler warnings
55 * Revision 1.16 2001/08/01 19:58:12 jongfoster
56 * Fixing documentation filenames in help menu, and making status
57 * option work without needing the "Junkbuster Status.URL" file.
59 * Revision 1.15 2001/07/30 22:08:36 jongfoster
60 * Tidying up #defines:
61 * - All feature #defines are now of the form FEATURE_xxx
62 * - Permanently turned off WIN_GUI_EDIT
63 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
65 * Revision 1.14 2001/07/29 18:47:05 jongfoster
66 * Adding missing #include "loadcfg.h"
68 * Revision 1.13 2001/07/19 19:15:14 haroon
69 * - Added a FIXME for EditFile but didn't fix :-)
71 * Revision 1.12 2001/07/13 14:04:59 oes
72 * Removed all #ifdef PCRS
74 * Revision 1.11 2001/06/07 23:08:12 jongfoster
75 * Forward and ACL edit options removed.
77 * Revision 1.10 2001/05/31 21:37:11 jongfoster
78 * GUI changes to rename "permissions file" to "actions file".
80 * Revision 1.9 2001/05/31 17:33:13 oes
84 * Revision 1.8 2001/05/29 09:50:24 jongfoster
85 * Unified blocklist/imagelist/permissionslist.
86 * File format is still under discussion, but the internal changes
89 * Also modified interceptor behaviour:
90 * - We now intercept all URLs beginning with one of the following
91 * prefixes (and *only* these prefixes):
93 * * http://ijbswa.sf.net/config/
94 * * http://ijbswa.sourceforge.net/config/
95 * - New interceptors "home page" - go to http://i.j.b/ to see it.
96 * - Internal changes so that intercepted and fast redirect pages
97 * are not replaced with an image.
98 * - Interceptors now have the option to send a binary page direct
99 * to the client. (i.e. ijb-send-banner uses this)
100 * - Implemented show-url-info interceptor. (Which is why I needed
101 * the above interceptors changes - a typical URL is
102 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
103 * The previous mechanism would not have intercepted that, and
104 * if it had been intercepted then it then it would have replaced
107 * Revision 1.7 2001/05/26 01:26:34 jongfoster
108 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
109 * This #define cannot be set from ./configure - there's no point, it
110 * doesn't work yet. See feature request # 425722
112 * Revision 1.6 2001/05/26 00:31:30 jongfoster
113 * Fixing compiler warning about comparing signed/unsigned.
115 * Revision 1.5 2001/05/26 00:28:36 jongfoster
116 * Automatic reloading of config file.
117 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
118 * Most of the global variables have been moved to a new
119 * struct configuration_spec, accessed through csp->config->globalname
120 * Most of the globals remaining are used by the Win32 GUI.
122 * Revision 1.4 2001/05/22 18:56:28 oes
125 * Revision 1.3 2001/05/20 15:07:54 jongfoster
126 * File is now ignored if _WIN_CONSOLE is defined.
128 * Revision 1.2 2001/05/20 01:21:20 jongfoster
129 * Version 2.9.4 checkin.
130 * - Merged popupfile and cookiefile, and added control over PCRS
131 * filtering, in new "permissionsfile".
132 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
133 * file error you now get a message box (in the Win32 GUI) rather
134 * than the program exiting with no explanation.
135 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
137 * - Removed tabs from "config"
138 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
139 * - Bumped up version number.
141 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
142 * Initial import of version 2.9.3 source tree
145 *********************************************************************/
154 #include <richedit.h>
158 #include "w32taskbar.h"
162 #include "miscutil.h"
166 const char w32res_h_rcs[] = W32RES_H_VERSION;
170 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
173 const char w32log_h_rcs[] = W32LOG_H_VERSION;
175 #ifndef _WIN_CONSOLE /* entire file */
178 * Timers and the various durations
180 #define TIMER_ANIM_ID 1
181 #define TIMER_ANIM_TIME 100
182 #define TIMER_ANIMSTOP_ID 2
183 #define TIMER_ANIMSTOP_TIME 1000
184 #define TIMER_CLIPBUFFER_ID 3
185 #define TIMER_CLIPBUFFER_TIME 1000
186 #define TIMER_CLIPBUFFER_FORCE_ID 4
187 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
190 * Styles of text that can be output
193 #define STYLE_HIGHLIGHT 1
195 #define STYLE_HEADER 3
198 * Number of frames of animation in tray activity sequence
200 #define ANIM_FRAMES 8
202 #define DEFAULT_MAX_BUFFER_LINES 200
203 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
204 #define DEFAULT_LOG_FONT_SIZE 8
207 * These values affect the way the log window behaves, they should be read
208 * from a file but for the moment, they are hardcoded here. Some options are
209 * configurable through the UI.
212 /* Indicates whether task bar shows activity animation */
213 BOOL g_bShowActivityAnimation = 1;
215 /* Indicates if the log window appears on the task bar */
216 BOOL g_bShowOnTaskBar = 0;
218 /* Indicates whether closing the log window really just hides it */
219 BOOL g_bCloseHidesWindow = 1;
221 /* Indicates if messages are logged at all */
222 BOOL g_bLogMessages = 1;
224 /* Indicates whether log messages are highlighted */
225 BOOL g_bHighlightMessages = 1;
227 /* Indicates if buffer is limited in size */
228 BOOL g_bLimitBufferSize = 1;
230 /* Maximum number of lines allowed in buffer when limited */
231 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
234 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
236 /* Size of font to use */
237 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
240 /* FIXME: this is a kludge */
242 const char * g_actions_file = NULL;
243 const char * g_re_filterfile = NULL;
245 const char * g_trustfile = NULL;
246 #endif /* def FEATURE_TRUST */
248 /* FIXME: end kludge */
252 /* Regular expression for detected URLs */
253 #define RE_URL "http:[^ \n\r]*"
256 * Regular expressions that are used to perform highlight in the log window
258 static struct _Pattern
263 } patterns_to_highlight[] =
266 { RE_URL, STYLE_LINK },
267 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
268 /* interesting text to highlight */
269 { "crunch!", STYLE_HIGHLIGHT },
271 { "referer:", STYLE_HEADER },
272 { "proxy-connection:", STYLE_HEADER },
273 { "proxy-agent:", STYLE_HEADER },
274 { "user-agent:", STYLE_HEADER },
275 { "host:", STYLE_HEADER },
276 { "accept:", STYLE_HEADER },
277 { "accept-encoding:", STYLE_HEADER },
278 { "accept-language:", STYLE_HEADER },
279 { "accept-charset:", STYLE_HEADER },
280 { "accept-ranges:", STYLE_HEADER },
281 { "date:", STYLE_HEADER },
282 { "cache-control:", STYLE_HEADER },
283 { "cache-last-checked:", STYLE_HEADER },
284 { "connection:", STYLE_HEADER },
285 { "content-type", STYLE_HEADER },
286 { "content-length", STYLE_HEADER },
287 { "cookie", STYLE_HEADER },
288 { "last-modified:", STYLE_HEADER },
289 { "pragma:", STYLE_HEADER },
290 { "server:", STYLE_HEADER },
291 { "etag:", STYLE_HEADER },
292 { "expires:", STYLE_HEADER },
293 { "warning:", STYLE_HEADER },
294 /* this is the terminator statement - do not delete! */
297 #endif /* def REGEX */
308 static CRITICAL_SECTION g_criticalsection;
309 static HWND g_hwndTray;
310 static HWND g_hwndLogBox;
311 static WNDPROC g_fnLogBox;
312 static HICON g_hiconAnim[ANIM_FRAMES];
313 static HICON g_hiconIdle;
314 static HICON g_hiconApp;
315 static int g_nAnimFrame;
316 static BOOL g_bClipPending = FALSE;
317 static int g_nRichEditVersion = 0;
322 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
323 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
324 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
325 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
326 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
327 static BOOL InitRichEdit(void);
328 static void LogClipBuffer(void);
329 static void LogCreatePatternMatchingBuffers(void);
330 static void LogDestroyPatternMatchingBuffers(void);
331 static int LogPutStringNoMatch(const char *pszText, int style);
334 /*********************************************************************
336 * Function : InitLogWindow
338 * Description : Initialise the log window.
342 * Returns : Always TRUE (there should be error checking on the resources).
344 *********************************************************************/
345 BOOL InitLogWindow(void)
350 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
351 for (i = 0; i < ANIM_FRAMES; i++)
353 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
355 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
357 /* Create the user interface */
358 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
359 g_hwndTray = CreateTrayWindow(g_hInstance);
360 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
362 /* Create pattern matching buffers (for highlighting */
363 LogCreatePatternMatchingBuffers();
365 /* Create a critical section to protect multi-threaded access to certain things */
366 InitializeCriticalSection(&g_criticalsection);
373 /*********************************************************************
375 * Function : TermLogWindow
377 * Description : Cleanup the logwindow.
383 *********************************************************************/
384 void TermLogWindow(void)
388 LogDestroyPatternMatchingBuffers();
390 TrayDeleteIcon(g_hwndTray, 1);
391 DeleteObject(g_hiconApp);
392 DeleteObject(g_hiconIdle);
393 for (i = 0; i < ANIM_FRAMES; i++)
395 DeleteObject(g_hiconAnim[i]);
401 /*********************************************************************
403 * Function : LogCreatePatternMatchingBuffers
405 * Description : Compile the pattern matching buffers.
411 *********************************************************************/
412 void LogCreatePatternMatchingBuffers(void)
416 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
418 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
425 /*********************************************************************
427 * Function : LogDestroyPatternMatchingBuffers
429 * Description : Free up the pattern matching buffers.
435 *********************************************************************/
436 void LogDestroyPatternMatchingBuffers(void)
440 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
442 regfree(&patterns_to_highlight[i].buffer);
449 /*********************************************************************
451 * Function : LogGetURLUnderCursor
453 * Description : Returns the URL from under the cursor (remember to free it!).
457 * Returns : NULL or a pointer to an URL string.
459 *********************************************************************/
460 char *LogGetURLUnderCursor(void)
462 char *szResult = NULL;
468 DWORD nWordStart = 0;
471 regcomp(&re, RE_URL, REG_ICASE);
473 /* Get the position of the cursor over the text window */
474 GetCursorPos(&ptCursor);
475 ScreenToClient(g_hwndLogBox, &ptCursor);
479 /* Search backwards and fowards to obtain the word that is highlighted */
480 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
481 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
482 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
484 /* Compare the string to the pattern */
485 if (nWordEnd > nWordStart)
490 range.chrg.cpMin = nWordStart;
491 range.chrg.cpMax = nWordEnd;
492 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
493 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
495 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
497 szResult = range.lpstrText;
501 free(range.lpstrText);
512 /*********************************************************************
514 * Function : LogPutString
516 * Description : Inserts text into the logging window. This is really
517 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
520 * 1 : pszText = pointer to string going to the log window
522 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
523 * FIXME: this is backwards to the rest of IJB and to common
524 * programming practice. Please use 0 => success instead.
526 *********************************************************************/
527 int LogPutString(const char *pszText)
534 if (pszText == NULL || strlen(pszText) == 0)
544 /* Critical section stops multiple threads doing nasty interactions that
545 * foul up the highlighting and output.
547 EnterCriticalSection(&g_criticalsection);
550 if (g_bHighlightMessages)
554 /* First things first, regexp scan for various things that we would like highlighted */
555 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
557 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
559 char *pszBefore = NULL;
560 char *pszMatch = NULL;
561 char *pszAfter = NULL;
564 /* Split the string up into pieces representing the strings, before
565 at and after the matching pattern
569 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
570 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
571 strncpy(pszBefore, pszText, match.rm_so);
573 if (match.rm_eo < (regoff_t)strlen(pszText))
575 pszAfter = strdup(&pszText[match.rm_eo]);
577 nMatchSize = match.rm_eo - match.rm_so;
578 pszMatch = (char *)malloc(nMatchSize + 1);
579 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
580 pszMatch[nMatchSize] = '\0';
582 /* Recursively call LogPutString */
585 LogPutString(pszBefore);
590 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
595 LogPutString(pszAfter);
606 result = LogPutStringNoMatch(pszText, STYLE_NONE);
611 LeaveCriticalSection(&g_criticalsection);
618 /*********************************************************************
620 * Function : LogPutStringNoMatch
622 * Description : Puts a string into the logging window.
625 * 1 : pszText = pointer to string going to the log window
626 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
628 * Returns : Always 1 => success.
629 * FIXME: this is backwards to the rest of IJB and to common
630 * programming practice. Please use 0 => success instead.
632 *********************************************************************/
633 int LogPutStringNoMatch(const char *pszText, int style)
639 assert(g_hwndLogBox);
640 if (g_hwndLogBox == NULL)
645 /* TODO preserve existing selection */
647 /* Go to the end of the text */
648 nTextLength = GetWindowTextLength(g_hwndLogBox);
649 range.cpMin = nTextLength;
650 range.cpMax = nTextLength;
651 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
653 /* Apply a formatting style */
654 memset(&format, 0, sizeof(format));
655 format.cbSize = sizeof(format);
656 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
657 format.yHeight = (g_nFontSize * 1440) / 72;
658 strcpy(format.szFaceName, g_szFontFaceName);
659 if (style == STYLE_NONE)
662 format.dwEffects |= CFE_AUTOCOLOR;
664 else if (style == STYLE_HEADER)
666 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
668 else if (style == STYLE_HIGHLIGHT)
670 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
672 else if (style == STYLE_LINK)
674 format.dwEffects |= CFE_UNDERLINE;
675 format.crTextColor = RGB(0, 0, 255);
677 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
679 /* Append text to the end */
680 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
682 /* TODO Restore the old selection */
685 if (strchr(pszText, '\n') != NULL)
687 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
690 /* Set the force clip timer going. This timer ensures clipping is done
691 intermittently even when there is a sustained burst of logging
693 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
695 g_bClipPending = TRUE;
703 /*********************************************************************
705 * Function : LogShowActivity
707 * Description : Start the spinner.
713 *********************************************************************/
714 void LogShowActivity(void)
716 /* Start some activity timers */
717 if (g_bShowActivityAnimation)
719 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
720 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
726 /*********************************************************************
728 * Function : LogClipBuffer
730 * Description : Prunes old lines from the log.
736 *********************************************************************/
737 void LogClipBuffer(void)
739 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
740 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
742 /* Compute the range representing the lines to be deleted */
743 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
744 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
747 range.cpMax = nLastChar;
749 /* TODO get current selection */
751 /* TODO adjust and clip old selection against range to be deleted */
753 /* Select range and erase it (turning off autoscroll to prevent
755 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
756 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
757 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
758 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
760 /* Restore old selection */
766 /*********************************************************************
768 * Function : CreateHiddenLogOwnerWindow
770 * Description : Creates a hidden owner window that stops the log
771 * window appearing in the task bar.
774 * 1 : hInstance = application's instance handle
776 * Returns : Handle to newly created window.
778 *********************************************************************/
779 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
781 static const char *szWndName = "PrivoxyLogOwner";
786 wc.lpfnWndProc = LogOwnerWindowProc;
789 wc.hInstance = hInstance;
792 wc.hbrBackground = 0;
794 wc.lpszClassName = szWndName;
798 hwnd = CreateWindow(szWndName, szWndName,
799 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
800 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
807 /*********************************************************************
809 * Function : LogOwnerWindowProc
811 * Description : Dummy procedure that does nothing special.
814 * 1 : hwnd = window handle
815 * 2 : uMsg = message number
816 * 3 : wParam = first param for this message
817 * 4 : lParam = next param for this message
819 * Returns : Same as `DefWindowProc'.
821 *********************************************************************/
822 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
824 return DefWindowProc(hwnd, uMsg, wParam, lParam);
829 /*********************************************************************
831 * Function : CreateLogWindow
833 * Description : Create the logging window.
836 * 1 : hInstance = application's instance handle
837 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
839 * Returns : Handle to newly created window.
841 *********************************************************************/
842 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
844 static const char *szWndName = "PrivoxyLogWindow";
845 static const char *szWndTitle = "Privoxy";
848 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
852 memset(&wc, 0, sizeof(wc));
853 wc.cbSize = sizeof(wc);
854 wc.style = CS_DBLCLKS;
855 wc.lpfnWndProc = LogWindowProc;
858 wc.hInstance = hInstance;
859 wc.hIcon = g_hiconApp;
861 wc.hbrBackground = 0;
862 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
863 wc.lpszClassName = szWndName;
864 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
865 RegisterClassEx(&wc);
867 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
868 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
869 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
871 /* Now create a child list box */
872 GetClientRect(hwnd, &rcClient);
874 /* Create a rich edit control */
876 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
877 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
878 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
879 hwnd, NULL, hInstance, NULL);
880 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
882 /* Subclass the control to catch certain messages */
883 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
884 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
886 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
887 if (!g_bShowOnTaskBar &&
888 (nCmdShow == SW_SHOWMINIMIZED ||
889 nCmdShow == SW_MINIMIZE ||
890 nCmdShow == SW_SHOWMINNOACTIVE))
895 ShowWindow(hwnd, nCmdShow);
898 GetClientRect(g_hwndLogFrame, &rcClient);
899 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
906 /*********************************************************************
908 * Function : InitRichEdit
910 * Description : Initialise the rich edit control library.
914 * Returns : TRUE => success, FALSE => failure.
915 * FIXME: this is backwards to the rest of IJB and to common
916 * programming practice. Please use 0 => success instead.
918 *********************************************************************/
919 BOOL InitRichEdit(void)
921 static HINSTANCE hInstRichEdit;
922 if (hInstRichEdit == NULL)
924 g_nRichEditVersion = 0;
925 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
928 g_nRichEditVersion = _RICHEDIT_VER;
932 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
935 g_nRichEditVersion = 0x0100;
939 return (hInstRichEdit != NULL) ? TRUE : FALSE;
944 /*********************************************************************
946 * Function : ShowLogWindow
948 * Description : Shows or hides the log window. We will also raise the
949 * window on a show command in case it is buried.
952 * 1 : bShow = TRUE to show, FALSE to mimize/hide
956 *********************************************************************/
957 void ShowLogWindow(BOOL bShow)
961 SetForegroundWindow(g_hwndLogFrame);
962 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
964 else if (g_bShowOnTaskBar)
966 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
970 ShowWindow(g_hwndLogFrame, SW_HIDE);
976 /*********************************************************************
978 * Function : EditFile
980 * Description : Opens the specified setting file for editing.
981 * FIXME: What if the file has no associated application. Check for return values
982 * from ShellExecute??
985 * 1 : filename = filename from the config (aka config.txt) file.
989 *********************************************************************/
990 void EditFile(const char *filename)
994 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1000 /*--------------------------------------------------------------------------*/
1001 /* Windows message handlers */
1002 /*--------------------------------------------------------------------------*/
1005 /*********************************************************************
1007 * Function : OnLogRButtonUp
1009 * Description : Handler for WM_RBUTTONUP messages.
1012 * 1 : nModifier = wParam from mouse message (unused)
1013 * 2 : x = x coordinate of the mouse event
1014 * 3 : y = y coordinate of the mouse event
1018 *********************************************************************/
1019 void OnLogRButtonUp(int nModifier, int x, int y)
1021 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1024 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1026 /* Check if there is a selection */
1028 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1029 if (range.cpMin == range.cpMax)
1031 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1035 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1038 /* Display the popup */
1039 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1046 /*********************************************************************
1048 * Function : OnLogCommand
1050 * Description : Handler for WM_COMMAND messages.
1053 * 1 : nCommand = the command portion of the menu selection event
1057 *********************************************************************/
1058 void OnLogCommand(int nCommand)
1063 ShowLogWindow(TRUE);
1067 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1071 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1074 case ID_VIEW_CLEARLOG:
1075 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1078 case ID_VIEW_LOGMESSAGES:
1079 g_bLogMessages = !g_bLogMessages;
1080 /* SaveLogSettings(); */
1083 case ID_VIEW_MESSAGEHIGHLIGHTING:
1084 g_bHighlightMessages = !g_bHighlightMessages;
1085 /* SaveLogSettings(); */
1088 case ID_VIEW_LIMITBUFFERSIZE:
1089 g_bLimitBufferSize = !g_bLimitBufferSize;
1090 /* SaveLogSettings(); */
1093 case ID_VIEW_ACTIVITYANIMATION:
1094 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1095 /* SaveLogSettings(); */
1098 #ifdef FEATURE_TOGGLE
1099 /* by haroon - change toggle to its opposite value */
1100 case ID_TOGGLE_ENABLED:
1101 g_bToggleIJB = !g_bToggleIJB;
1104 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1108 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1111 #endif /* def FEATURE_TOGGLE */
1113 case ID_TOOLS_EDITCONFIG:
1114 EditFile(configfile);
1117 case ID_TOOLS_EDITACTIONS:
1118 EditFile(g_actions_file);
1121 case ID_TOOLS_EDITFILTERS:
1122 EditFile(g_re_filterfile);
1125 #ifdef FEATURE_TRUST
1126 case ID_TOOLS_EDITTRUST:
1127 EditFile(g_trustfile);
1129 #endif /* def FEATURE_TRUST */
1132 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1136 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1139 case ID_HELP_MANUAL:
1140 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1143 case ID_HELP_STATUS:
1144 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1148 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1159 /*********************************************************************
1161 * Function : OnLogInitMenu
1163 * Description : Handler for WM_INITMENU messages. Enable, disable,
1164 * check, and/or uncheck menu options as apropos.
1167 * 1 : hmenu = handle to menu to "make current"
1171 *********************************************************************/
1172 void OnLogInitMenu(HMENU hmenu)
1174 /* Only enable editors if there is a file to edit */
1175 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1176 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1177 #ifdef FEATURE_TRUST
1178 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1179 #endif /* def FEATURE_TRUST */
1181 /* Check/uncheck options */
1182 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1183 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1184 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1185 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1186 #ifdef FEATURE_TOGGLE
1187 /* by haroon - menu item for Enable toggle on/off */
1188 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1189 #endif /* def FEATURE_TOGGLE */
1194 /*********************************************************************
1196 * Function : OnLogTimer
1198 * Description : Handler for WM_TIMER messages.
1201 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1205 *********************************************************************/
1206 void OnLogTimer(int nTimer)
1211 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1214 case TIMER_ANIMSTOP_ID:
1216 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1217 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1218 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1221 case TIMER_CLIPBUFFER_ID:
1222 case TIMER_CLIPBUFFER_FORCE_ID:
1224 g_bClipPending = FALSE;
1225 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1226 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1237 /*********************************************************************
1239 * Function : LogRichEditProc
1241 * Description : Window subclass routine handles some events for the rich edit control.
1244 * 1 : hwnd = window handle of the rich edit control
1245 * 2 : uMsg = message number
1246 * 3 : wParam = first param for this message
1247 * 4 : lParam = next param for this message
1249 * Returns : Appropriate M$ window message handler codes.
1251 *********************************************************************/
1252 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1259 pt.x = LOWORD(lParam);
1260 pt.y = HIWORD(lParam);
1261 ClientToScreen(hwnd, &pt);
1262 OnLogRButtonUp(wParam, pt.x, pt.y);
1266 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1271 /*********************************************************************
1273 * Function : LogWindowProc
1275 * Description : Windows call back routine handles events on the log window.
1278 * 1 : hwnd = handle of the logging window
1279 * 2 : uMsg = message number
1280 * 3 : wParam = first param for this message
1281 * 4 : lParam = next param for this message
1283 * Returns : Appropriate M$ window message handler codes.
1285 *********************************************************************/
1286 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1294 /* This is the end - beautiful friend - the end */
1295 DestroyWindow(g_hwndLogBox);
1296 DestroyWindow(g_hwndLogFrame);
1305 /* Resize the logging window to fit the new frame */
1309 GetClientRect(g_hwndLogFrame, &rc);
1310 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1315 OnLogInitMenu((HMENU) wParam);
1323 OnLogCommand(LOWORD(wParam));
1330 if (g_bCloseHidesWindow)
1332 ShowLogWindow(FALSE);
1337 ShowLogWindow(FALSE);
1343 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1347 #endif /* ndef _WIN_CONSOLE - entire file */