1 const char w32log_rcs[] = "$Id: w32log.c,v 1.24 2002/03/31 17:19:00 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.24 2002/03/31 17:19:00 jongfoster
36 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
38 * Revision 1.23 2002/03/26 22:57:10 jongfoster
39 * Web server name should begin www.
41 * Revision 1.22 2002/03/24 12:48:23 jongfoster
44 * Revision 1.21 2002/03/24 12:07:35 jongfoster
45 * Consistern name for filters file
47 * Revision 1.20 2002/03/24 12:03:47 jongfoster
50 * Revision 1.19 2002/01/17 21:04:17 jongfoster
51 * Replacing hard references to the URL of the config interface
52 * with #defines from project.h
54 * Revision 1.18 2001/11/30 23:37:24 jongfoster
55 * Renaming the Win32 config file to config.txt - this is almost the
56 * same as the corresponding UNIX name "config"
58 * Revision 1.17 2001/11/16 00:46:31 jongfoster
59 * Fixing compiler warnings
61 * Revision 1.16 2001/08/01 19:58:12 jongfoster
62 * Fixing documentation filenames in help menu, and making status
63 * option work without needing the "Junkbuster Status.URL" file.
65 * Revision 1.15 2001/07/30 22:08:36 jongfoster
66 * Tidying up #defines:
67 * - All feature #defines are now of the form FEATURE_xxx
68 * - Permanently turned off WIN_GUI_EDIT
69 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
71 * Revision 1.14 2001/07/29 18:47:05 jongfoster
72 * Adding missing #include "loadcfg.h"
74 * Revision 1.13 2001/07/19 19:15:14 haroon
75 * - Added a FIXME for EditFile but didn't fix :-)
77 * Revision 1.12 2001/07/13 14:04:59 oes
78 * Removed all #ifdef PCRS
80 * Revision 1.11 2001/06/07 23:08:12 jongfoster
81 * Forward and ACL edit options removed.
83 * Revision 1.10 2001/05/31 21:37:11 jongfoster
84 * GUI changes to rename "permissions file" to "actions file".
86 * Revision 1.9 2001/05/31 17:33:13 oes
90 * Revision 1.8 2001/05/29 09:50:24 jongfoster
91 * Unified blocklist/imagelist/permissionslist.
92 * File format is still under discussion, but the internal changes
95 * Also modified interceptor behaviour:
96 * - We now intercept all URLs beginning with one of the following
97 * prefixes (and *only* these prefixes):
99 * * http://ijbswa.sf.net/config/
100 * * http://ijbswa.sourceforge.net/config/
101 * - New interceptors "home page" - go to http://i.j.b/ to see it.
102 * - Internal changes so that intercepted and fast redirect pages
103 * are not replaced with an image.
104 * - Interceptors now have the option to send a binary page direct
105 * to the client. (i.e. ijb-send-banner uses this)
106 * - Implemented show-url-info interceptor. (Which is why I needed
107 * the above interceptors changes - a typical URL is
108 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
109 * The previous mechanism would not have intercepted that, and
110 * if it had been intercepted then it then it would have replaced
113 * Revision 1.7 2001/05/26 01:26:34 jongfoster
114 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
115 * This #define cannot be set from ./configure - there's no point, it
116 * doesn't work yet. See feature request # 425722
118 * Revision 1.6 2001/05/26 00:31:30 jongfoster
119 * Fixing compiler warning about comparing signed/unsigned.
121 * Revision 1.5 2001/05/26 00:28:36 jongfoster
122 * Automatic reloading of config file.
123 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
124 * Most of the global variables have been moved to a new
125 * struct configuration_spec, accessed through csp->config->globalname
126 * Most of the globals remaining are used by the Win32 GUI.
128 * Revision 1.4 2001/05/22 18:56:28 oes
131 * Revision 1.3 2001/05/20 15:07:54 jongfoster
132 * File is now ignored if _WIN_CONSOLE is defined.
134 * Revision 1.2 2001/05/20 01:21:20 jongfoster
135 * Version 2.9.4 checkin.
136 * - Merged popupfile and cookiefile, and added control over PCRS
137 * filtering, in new "permissionsfile".
138 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
139 * file error you now get a message box (in the Win32 GUI) rather
140 * than the program exiting with no explanation.
141 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
143 * - Removed tabs from "config"
144 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
145 * - Bumped up version number.
147 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
148 * Initial import of version 2.9.3 source tree
151 *********************************************************************/
163 #include <richedit.h>
167 #include "w32taskbar.h"
171 #include "miscutil.h"
175 const char w32res_h_rcs[] = W32RES_H_VERSION;
179 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
182 const char w32log_h_rcs[] = W32LOG_H_VERSION;
184 #ifndef _WIN_CONSOLE /* entire file */
187 * Timers and the various durations
189 #define TIMER_ANIM_ID 1
190 #define TIMER_ANIM_TIME 100
191 #define TIMER_ANIMSTOP_ID 2
192 #define TIMER_ANIMSTOP_TIME 1000
193 #define TIMER_CLIPBUFFER_ID 3
194 #define TIMER_CLIPBUFFER_TIME 1000
195 #define TIMER_CLIPBUFFER_FORCE_ID 4
196 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
199 * Styles of text that can be output
202 #define STYLE_HIGHLIGHT 1
204 #define STYLE_HEADER 3
207 * Number of frames of animation in tray activity sequence
209 #define ANIM_FRAMES 8
211 #define DEFAULT_MAX_BUFFER_LINES 200
212 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
213 #define DEFAULT_LOG_FONT_SIZE 8
216 * These values affect the way the log window behaves, they should be read
217 * from a file but for the moment, they are hardcoded here. Some options are
218 * configurable through the UI.
221 /* Indicates whether task bar shows activity animation */
222 BOOL g_bShowActivityAnimation = 1;
224 /* Indicates if the log window appears on the task bar */
225 BOOL g_bShowOnTaskBar = 0;
227 /* Indicates whether closing the log window really just hides it */
228 BOOL g_bCloseHidesWindow = 1;
230 /* Indicates if messages are logged at all */
231 BOOL g_bLogMessages = 1;
233 /* Indicates whether log messages are highlighted */
234 BOOL g_bHighlightMessages = 1;
236 /* Indicates if buffer is limited in size */
237 BOOL g_bLimitBufferSize = 1;
239 /* Maximum number of lines allowed in buffer when limited */
240 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
243 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
245 /* Size of font to use */
246 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
249 /* FIXME: this is a kludge */
251 const char * g_actions_file = NULL;
252 const char * g_re_filterfile = NULL;
254 const char * g_trustfile = NULL;
255 #endif /* def FEATURE_TRUST */
257 /* FIXME: end kludge */
259 /* Regular expression for detected URLs */
260 #define RE_URL "http:[^ \n\r]*"
263 * Regular expressions that are used to perform highlight in the log window
265 static struct _Pattern
270 } patterns_to_highlight[] =
273 { RE_URL, STYLE_LINK },
274 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
275 /* interesting text to highlight */
276 { "crunch!", STYLE_HIGHLIGHT },
278 { "referer:", STYLE_HEADER },
279 { "proxy-connection:", STYLE_HEADER },
280 { "proxy-agent:", STYLE_HEADER },
281 { "user-agent:", STYLE_HEADER },
282 { "host:", STYLE_HEADER },
283 { "accept:", STYLE_HEADER },
284 { "accept-encoding:", STYLE_HEADER },
285 { "accept-language:", STYLE_HEADER },
286 { "accept-charset:", STYLE_HEADER },
287 { "accept-ranges:", STYLE_HEADER },
288 { "date:", STYLE_HEADER },
289 { "cache-control:", STYLE_HEADER },
290 { "cache-last-checked:", STYLE_HEADER },
291 { "connection:", STYLE_HEADER },
292 { "content-type", STYLE_HEADER },
293 { "content-length", STYLE_HEADER },
294 { "cookie", STYLE_HEADER },
295 { "last-modified:", STYLE_HEADER },
296 { "pragma:", STYLE_HEADER },
297 { "server:", STYLE_HEADER },
298 { "etag:", STYLE_HEADER },
299 { "expires:", STYLE_HEADER },
300 { "warning:", STYLE_HEADER },
301 /* this is the terminator statement - do not delete! */
313 static CRITICAL_SECTION g_criticalsection;
314 static HWND g_hwndTray;
315 static HWND g_hwndLogBox;
316 static WNDPROC g_fnLogBox;
317 static HICON g_hiconAnim[ANIM_FRAMES];
318 static HICON g_hiconIdle;
319 static HICON g_hiconApp;
320 static int g_nAnimFrame;
321 static BOOL g_bClipPending = FALSE;
322 static int g_nRichEditVersion = 0;
327 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
328 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
329 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
330 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
331 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
332 static BOOL InitRichEdit(void);
333 static void LogClipBuffer(void);
334 static void LogCreatePatternMatchingBuffers(void);
335 static void LogDestroyPatternMatchingBuffers(void);
336 static int LogPutStringNoMatch(const char *pszText, int style);
339 /*********************************************************************
341 * Function : InitLogWindow
343 * Description : Initialise the log window.
347 * Returns : Always TRUE (there should be error checking on the resources).
349 *********************************************************************/
350 BOOL InitLogWindow(void)
355 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
356 for (i = 0; i < ANIM_FRAMES; i++)
358 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
360 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
362 /* Create the user interface */
363 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
364 g_hwndTray = CreateTrayWindow(g_hInstance);
365 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
367 /* Create pattern matching buffers (for highlighting */
368 LogCreatePatternMatchingBuffers();
370 /* Create a critical section to protect multi-threaded access to certain things */
371 InitializeCriticalSection(&g_criticalsection);
378 /*********************************************************************
380 * Function : TermLogWindow
382 * Description : Cleanup the logwindow.
388 *********************************************************************/
389 void TermLogWindow(void)
393 LogDestroyPatternMatchingBuffers();
395 TrayDeleteIcon(g_hwndTray, 1);
396 DeleteObject(g_hiconApp);
397 DeleteObject(g_hiconIdle);
398 for (i = 0; i < ANIM_FRAMES; i++)
400 DeleteObject(g_hiconAnim[i]);
406 /*********************************************************************
408 * Function : LogCreatePatternMatchingBuffers
410 * Description : Compile the pattern matching buffers.
416 *********************************************************************/
417 void LogCreatePatternMatchingBuffers(void)
420 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
422 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
427 /*********************************************************************
429 * Function : LogDestroyPatternMatchingBuffers
431 * Description : Free up the pattern matching buffers.
437 *********************************************************************/
438 void LogDestroyPatternMatchingBuffers(void)
441 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
443 regfree(&patterns_to_highlight[i].buffer);
448 /*********************************************************************
450 * Function : LogGetURLUnderCursor
452 * Description : Returns the URL from under the cursor (remember to free it!).
456 * Returns : NULL or a pointer to an URL string.
458 *********************************************************************/
459 char *LogGetURLUnderCursor(void)
461 char *szResult = NULL;
466 DWORD nWordStart = 0;
469 regcomp(&re, RE_URL, REG_ICASE);
471 /* Get the position of the cursor over the text window */
472 GetCursorPos(&ptCursor);
473 ScreenToClient(g_hwndLogBox, &ptCursor);
477 /* Search backwards and fowards to obtain the word that is highlighted */
478 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
479 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
480 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
482 /* Compare the string to the pattern */
483 if (nWordEnd > nWordStart)
488 range.chrg.cpMin = nWordStart;
489 range.chrg.cpMax = nWordEnd;
490 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
491 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
493 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
495 szResult = range.lpstrText;
499 free(range.lpstrText);
509 /*********************************************************************
511 * Function : LogPutString
513 * Description : Inserts text into the logging window. This is really
514 * a regexp aware wrapper function to `LogPutStringNoMatch'.
517 * 1 : pszText = pointer to string going to the log window
519 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
520 * FIXME: this is backwards to the rest of IJB and to common
521 * programming practice. Please use 0 => success instead.
523 *********************************************************************/
524 int LogPutString(const char *pszText)
529 if (pszText == NULL || strlen(pszText) == 0)
539 /* Critical section stops multiple threads doing nasty interactions that
540 * foul up the highlighting and output.
542 EnterCriticalSection(&g_criticalsection);
544 if (g_bHighlightMessages)
548 /* First things first, regexp scan for various things that we would like highlighted */
549 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
551 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
553 char *pszBefore = NULL;
554 char *pszMatch = NULL;
555 char *pszAfter = NULL;
558 /* Split the string up into pieces representing the strings, before
559 at and after the matching pattern
563 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
564 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
565 strncpy(pszBefore, pszText, match.rm_so);
567 if (match.rm_eo < (regoff_t)strlen(pszText))
569 pszAfter = strdup(&pszText[match.rm_eo]);
571 nMatchSize = match.rm_eo - match.rm_so;
572 pszMatch = (char *)malloc(nMatchSize + 1);
573 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
574 pszMatch[nMatchSize] = '\0';
576 /* Recursively call LogPutString */
579 LogPutString(pszBefore);
584 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
589 LogPutString(pszAfter);
599 result = LogPutStringNoMatch(pszText, STYLE_NONE);
602 LeaveCriticalSection(&g_criticalsection);
609 /*********************************************************************
611 * Function : LogPutStringNoMatch
613 * Description : Puts a string into the logging window.
616 * 1 : pszText = pointer to string going to the log window
617 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
619 * Returns : Always 1 => success.
620 * FIXME: this is backwards to the rest of IJB and to common
621 * programming practice. Please use 0 => success instead.
623 *********************************************************************/
624 int LogPutStringNoMatch(const char *pszText, int style)
630 assert(g_hwndLogBox);
631 if (g_hwndLogBox == NULL)
636 /* TODO preserve existing selection */
638 /* Go to the end of the text */
639 nTextLength = GetWindowTextLength(g_hwndLogBox);
640 range.cpMin = nTextLength;
641 range.cpMax = nTextLength;
642 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
644 /* Apply a formatting style */
645 memset(&format, 0, sizeof(format));
646 format.cbSize = sizeof(format);
647 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
648 format.yHeight = (g_nFontSize * 1440) / 72;
649 strcpy(format.szFaceName, g_szFontFaceName);
650 if (style == STYLE_NONE)
653 format.dwEffects |= CFE_AUTOCOLOR;
655 else if (style == STYLE_HEADER)
657 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
659 else if (style == STYLE_HIGHLIGHT)
661 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
663 else if (style == STYLE_LINK)
665 format.dwEffects |= CFE_UNDERLINE;
666 format.crTextColor = RGB(0, 0, 255);
668 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
670 /* Append text to the end */
671 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
673 /* TODO Restore the old selection */
676 if (strchr(pszText, '\n') != NULL)
678 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
681 /* Set the force clip timer going. This timer ensures clipping is done
682 intermittently even when there is a sustained burst of logging
684 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
686 g_bClipPending = TRUE;
694 /*********************************************************************
696 * Function : LogShowActivity
698 * Description : Start the spinner.
704 *********************************************************************/
705 void LogShowActivity(void)
707 /* Start some activity timers */
708 if (g_bShowActivityAnimation)
710 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
711 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
717 /*********************************************************************
719 * Function : LogClipBuffer
721 * Description : Prunes old lines from the log.
727 *********************************************************************/
728 void LogClipBuffer(void)
730 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
731 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
733 /* Compute the range representing the lines to be deleted */
734 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
735 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
738 range.cpMax = nLastChar;
740 /* TODO get current selection */
742 /* TODO adjust and clip old selection against range to be deleted */
744 /* Select range and erase it (turning off autoscroll to prevent
746 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
747 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
748 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
749 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
751 /* Restore old selection */
757 /*********************************************************************
759 * Function : CreateHiddenLogOwnerWindow
761 * Description : Creates a hidden owner window that stops the log
762 * window appearing in the task bar.
765 * 1 : hInstance = application's instance handle
767 * Returns : Handle to newly created window.
769 *********************************************************************/
770 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
772 static const char *szWndName = "PrivoxyLogOwner";
777 wc.lpfnWndProc = LogOwnerWindowProc;
780 wc.hInstance = hInstance;
783 wc.hbrBackground = 0;
785 wc.lpszClassName = szWndName;
789 hwnd = CreateWindow(szWndName, szWndName,
790 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
791 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
798 /*********************************************************************
800 * Function : LogOwnerWindowProc
802 * Description : Dummy procedure that does nothing special.
805 * 1 : hwnd = window handle
806 * 2 : uMsg = message number
807 * 3 : wParam = first param for this message
808 * 4 : lParam = next param for this message
810 * Returns : Same as `DefWindowProc'.
812 *********************************************************************/
813 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
815 return DefWindowProc(hwnd, uMsg, wParam, lParam);
820 /*********************************************************************
822 * Function : CreateLogWindow
824 * Description : Create the logging window.
827 * 1 : hInstance = application's instance handle
828 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
830 * Returns : Handle to newly created window.
832 *********************************************************************/
833 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
835 static const char *szWndName = "PrivoxyLogWindow";
836 static const char *szWndTitle = "Privoxy";
839 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
843 memset(&wc, 0, sizeof(wc));
844 wc.cbSize = sizeof(wc);
845 wc.style = CS_DBLCLKS;
846 wc.lpfnWndProc = LogWindowProc;
849 wc.hInstance = hInstance;
850 wc.hIcon = g_hiconApp;
852 wc.hbrBackground = 0;
853 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
854 wc.lpszClassName = szWndName;
855 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
856 RegisterClassEx(&wc);
858 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
859 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
860 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
862 /* Now create a child list box */
863 GetClientRect(hwnd, &rcClient);
865 /* Create a rich edit control */
867 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
868 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
869 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
870 hwnd, NULL, hInstance, NULL);
871 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
873 /* Subclass the control to catch certain messages */
874 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
875 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
877 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
878 if (!g_bShowOnTaskBar &&
879 (nCmdShow == SW_SHOWMINIMIZED ||
880 nCmdShow == SW_MINIMIZE ||
881 nCmdShow == SW_SHOWMINNOACTIVE))
886 ShowWindow(hwnd, nCmdShow);
889 GetClientRect(g_hwndLogFrame, &rcClient);
890 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
897 /*********************************************************************
899 * Function : InitRichEdit
901 * Description : Initialise the rich edit control library.
905 * Returns : TRUE => success, FALSE => failure.
906 * FIXME: this is backwards to the rest of IJB and to common
907 * programming practice. Please use 0 => success instead.
909 *********************************************************************/
910 BOOL InitRichEdit(void)
912 static HINSTANCE hInstRichEdit;
913 if (hInstRichEdit == NULL)
915 g_nRichEditVersion = 0;
916 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
919 g_nRichEditVersion = _RICHEDIT_VER;
923 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
926 g_nRichEditVersion = 0x0100;
930 return (hInstRichEdit != NULL) ? TRUE : FALSE;
935 /*********************************************************************
937 * Function : ShowLogWindow
939 * Description : Shows or hides the log window. We will also raise the
940 * window on a show command in case it is buried.
943 * 1 : bShow = TRUE to show, FALSE to mimize/hide
947 *********************************************************************/
948 void ShowLogWindow(BOOL bShow)
952 SetForegroundWindow(g_hwndLogFrame);
953 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
955 else if (g_bShowOnTaskBar)
957 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
961 ShowWindow(g_hwndLogFrame, SW_HIDE);
967 /*********************************************************************
969 * Function : EditFile
971 * Description : Opens the specified setting file for editing.
972 * FIXME: What if the file has no associated application. Check for return values
973 * from ShellExecute??
976 * 1 : filename = filename from the config (aka config.txt) file.
980 *********************************************************************/
981 void EditFile(const char *filename)
985 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
991 /*--------------------------------------------------------------------------*/
992 /* Windows message handlers */
993 /*--------------------------------------------------------------------------*/
996 /*********************************************************************
998 * Function : OnLogRButtonUp
1000 * Description : Handler for WM_RBUTTONUP messages.
1003 * 1 : nModifier = wParam from mouse message (unused)
1004 * 2 : x = x coordinate of the mouse event
1005 * 3 : y = y coordinate of the mouse event
1009 *********************************************************************/
1010 void OnLogRButtonUp(int nModifier, int x, int y)
1012 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1015 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1017 /* Check if there is a selection */
1019 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1020 if (range.cpMin == range.cpMax)
1022 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1026 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1029 /* Display the popup */
1030 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1037 /*********************************************************************
1039 * Function : OnLogCommand
1041 * Description : Handler for WM_COMMAND messages.
1044 * 1 : nCommand = the command portion of the menu selection event
1048 *********************************************************************/
1049 void OnLogCommand(int nCommand)
1054 ShowLogWindow(TRUE);
1058 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1062 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1065 case ID_VIEW_CLEARLOG:
1066 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1069 case ID_VIEW_LOGMESSAGES:
1070 g_bLogMessages = !g_bLogMessages;
1071 /* SaveLogSettings(); */
1074 case ID_VIEW_MESSAGEHIGHLIGHTING:
1075 g_bHighlightMessages = !g_bHighlightMessages;
1076 /* SaveLogSettings(); */
1079 case ID_VIEW_LIMITBUFFERSIZE:
1080 g_bLimitBufferSize = !g_bLimitBufferSize;
1081 /* SaveLogSettings(); */
1084 case ID_VIEW_ACTIVITYANIMATION:
1085 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1086 /* SaveLogSettings(); */
1089 #ifdef FEATURE_TOGGLE
1090 /* by haroon - change toggle to its opposite value */
1091 case ID_TOGGLE_ENABLED:
1092 g_bToggleIJB = !g_bToggleIJB;
1095 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1099 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1102 #endif /* def FEATURE_TOGGLE */
1104 case ID_TOOLS_EDITCONFIG:
1105 EditFile(configfile);
1108 case ID_TOOLS_EDITACTIONS:
1109 EditFile(g_actions_file);
1112 case ID_TOOLS_EDITFILTERS:
1113 EditFile(g_re_filterfile);
1116 #ifdef FEATURE_TRUST
1117 case ID_TOOLS_EDITTRUST:
1118 EditFile(g_trustfile);
1120 #endif /* def FEATURE_TRUST */
1123 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1127 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1130 case ID_HELP_MANUAL:
1131 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1134 case ID_HELP_STATUS:
1135 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1139 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1150 /*********************************************************************
1152 * Function : OnLogInitMenu
1154 * Description : Handler for WM_INITMENU messages. Enable, disable,
1155 * check, and/or uncheck menu options as apropos.
1158 * 1 : hmenu = handle to menu to "make current"
1162 *********************************************************************/
1163 void OnLogInitMenu(HMENU hmenu)
1165 /* Only enable editors if there is a file to edit */
1166 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1167 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1168 #ifdef FEATURE_TRUST
1169 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1170 #endif /* def FEATURE_TRUST */
1172 /* Check/uncheck options */
1173 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1174 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1175 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1176 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1177 #ifdef FEATURE_TOGGLE
1178 /* by haroon - menu item for Enable toggle on/off */
1179 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1180 #endif /* def FEATURE_TOGGLE */
1185 /*********************************************************************
1187 * Function : OnLogTimer
1189 * Description : Handler for WM_TIMER messages.
1192 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1196 *********************************************************************/
1197 void OnLogTimer(int nTimer)
1202 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1205 case TIMER_ANIMSTOP_ID:
1207 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1208 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1209 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1212 case TIMER_CLIPBUFFER_ID:
1213 case TIMER_CLIPBUFFER_FORCE_ID:
1215 g_bClipPending = FALSE;
1216 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1217 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1228 /*********************************************************************
1230 * Function : LogRichEditProc
1232 * Description : Window subclass routine handles some events for the rich edit control.
1235 * 1 : hwnd = window handle of the rich edit control
1236 * 2 : uMsg = message number
1237 * 3 : wParam = first param for this message
1238 * 4 : lParam = next param for this message
1240 * Returns : Appropriate M$ window message handler codes.
1242 *********************************************************************/
1243 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1250 pt.x = LOWORD(lParam);
1251 pt.y = HIWORD(lParam);
1252 ClientToScreen(hwnd, &pt);
1253 OnLogRButtonUp(wParam, pt.x, pt.y);
1257 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1262 /*********************************************************************
1264 * Function : LogWindowProc
1266 * Description : Windows call back routine handles events on the log window.
1269 * 1 : hwnd = handle of the logging window
1270 * 2 : uMsg = message number
1271 * 3 : wParam = first param for this message
1272 * 4 : lParam = next param for this message
1274 * Returns : Appropriate M$ window message handler codes.
1276 *********************************************************************/
1277 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1285 /* This is the end - beautiful friend - the end */
1286 DestroyWindow(g_hwndLogBox);
1287 DestroyWindow(g_hwndLogFrame);
1296 /* Resize the logging window to fit the new frame */
1300 GetClientRect(g_hwndLogFrame, &rc);
1301 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1306 OnLogInitMenu((HMENU) wParam);
1314 OnLogCommand(LOWORD(wParam));
1321 if (g_bCloseHidesWindow)
1323 ShowLogWindow(FALSE);
1328 ShowLogWindow(FALSE);
1334 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1338 #endif /* ndef _WIN_CONSOLE - entire file */