1 const char w32log_rcs[] = "$Id: w32log.c,v 1.25 2002/04/04 00:36:36 gliptak 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.25 2002/04/04 00:36:36 gliptak
36 * always use pcre for matching
38 * Revision 1.24 2002/03/31 17:19:00 jongfoster
39 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
41 * Revision 1.23 2002/03/26 22:57:10 jongfoster
42 * Web server name should begin www.
44 * Revision 1.22 2002/03/24 12:48:23 jongfoster
47 * Revision 1.21 2002/03/24 12:07:35 jongfoster
48 * Consistern name for filters file
50 * Revision 1.20 2002/03/24 12:03:47 jongfoster
53 * Revision 1.19 2002/01/17 21:04:17 jongfoster
54 * Replacing hard references to the URL of the config interface
55 * with #defines from project.h
57 * Revision 1.18 2001/11/30 23:37:24 jongfoster
58 * Renaming the Win32 config file to config.txt - this is almost the
59 * same as the corresponding UNIX name "config"
61 * Revision 1.17 2001/11/16 00:46:31 jongfoster
62 * Fixing compiler warnings
64 * Revision 1.16 2001/08/01 19:58:12 jongfoster
65 * Fixing documentation filenames in help menu, and making status
66 * option work without needing the "Junkbuster Status.URL" file.
68 * Revision 1.15 2001/07/30 22:08:36 jongfoster
69 * Tidying up #defines:
70 * - All feature #defines are now of the form FEATURE_xxx
71 * - Permanently turned off WIN_GUI_EDIT
72 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
74 * Revision 1.14 2001/07/29 18:47:05 jongfoster
75 * Adding missing #include "loadcfg.h"
77 * Revision 1.13 2001/07/19 19:15:14 haroon
78 * - Added a FIXME for EditFile but didn't fix :-)
80 * Revision 1.12 2001/07/13 14:04:59 oes
81 * Removed all #ifdef PCRS
83 * Revision 1.11 2001/06/07 23:08:12 jongfoster
84 * Forward and ACL edit options removed.
86 * Revision 1.10 2001/05/31 21:37:11 jongfoster
87 * GUI changes to rename "permissions file" to "actions file".
89 * Revision 1.9 2001/05/31 17:33:13 oes
93 * Revision 1.8 2001/05/29 09:50:24 jongfoster
94 * Unified blocklist/imagelist/permissionslist.
95 * File format is still under discussion, but the internal changes
98 * Also modified interceptor behaviour:
99 * - We now intercept all URLs beginning with one of the following
100 * prefixes (and *only* these prefixes):
102 * * http://ijbswa.sf.net/config/
103 * * http://ijbswa.sourceforge.net/config/
104 * - New interceptors "home page" - go to http://i.j.b/ to see it.
105 * - Internal changes so that intercepted and fast redirect pages
106 * are not replaced with an image.
107 * - Interceptors now have the option to send a binary page direct
108 * to the client. (i.e. ijb-send-banner uses this)
109 * - Implemented show-url-info interceptor. (Which is why I needed
110 * the above interceptors changes - a typical URL is
111 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
112 * The previous mechanism would not have intercepted that, and
113 * if it had been intercepted then it then it would have replaced
116 * Revision 1.7 2001/05/26 01:26:34 jongfoster
117 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
118 * This #define cannot be set from ./configure - there's no point, it
119 * doesn't work yet. See feature request # 425722
121 * Revision 1.6 2001/05/26 00:31:30 jongfoster
122 * Fixing compiler warning about comparing signed/unsigned.
124 * Revision 1.5 2001/05/26 00:28:36 jongfoster
125 * Automatic reloading of config file.
126 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
127 * Most of the global variables have been moved to a new
128 * struct configuration_spec, accessed through csp->config->globalname
129 * Most of the globals remaining are used by the Win32 GUI.
131 * Revision 1.4 2001/05/22 18:56:28 oes
134 * Revision 1.3 2001/05/20 15:07:54 jongfoster
135 * File is now ignored if _WIN_CONSOLE is defined.
137 * Revision 1.2 2001/05/20 01:21:20 jongfoster
138 * Version 2.9.4 checkin.
139 * - Merged popupfile and cookiefile, and added control over PCRS
140 * filtering, in new "permissionsfile".
141 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
142 * file error you now get a message box (in the Win32 GUI) rather
143 * than the program exiting with no explanation.
144 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
146 * - Removed tabs from "config"
147 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
148 * - Bumped up version number.
150 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
151 * Initial import of version 2.9.3 source tree
154 *********************************************************************/
166 #include <richedit.h>
170 #include "w32taskbar.h"
174 #include "miscutil.h"
178 const char w32res_h_rcs[] = W32RES_H_VERSION;
182 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
185 const char w32log_h_rcs[] = W32LOG_H_VERSION;
187 #ifndef _WIN_CONSOLE /* entire file */
190 * Timers and the various durations
192 #define TIMER_ANIM_ID 1
193 #define TIMER_ANIM_TIME 100
194 #define TIMER_ANIMSTOP_ID 2
195 #define TIMER_ANIMSTOP_TIME 1000
196 #define TIMER_CLIPBUFFER_ID 3
197 #define TIMER_CLIPBUFFER_TIME 1000
198 #define TIMER_CLIPBUFFER_FORCE_ID 4
199 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
202 * Styles of text that can be output
205 #define STYLE_HIGHLIGHT 1
207 #define STYLE_HEADER 3
210 * Number of frames of animation in tray activity sequence
212 #define ANIM_FRAMES 8
214 #define DEFAULT_MAX_BUFFER_LINES 200
215 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
216 #define DEFAULT_LOG_FONT_SIZE 8
219 * These values affect the way the log window behaves, they should be read
220 * from a file but for the moment, they are hardcoded here. Some options are
221 * configurable through the UI.
224 /* Indicates whether task bar shows activity animation */
225 BOOL g_bShowActivityAnimation = 1;
227 /* Indicates if the log window appears on the task bar */
228 BOOL g_bShowOnTaskBar = 0;
230 /* Indicates whether closing the log window really just hides it */
231 BOOL g_bCloseHidesWindow = 1;
233 /* Indicates if messages are logged at all */
234 BOOL g_bLogMessages = 1;
236 /* Indicates whether log messages are highlighted */
237 BOOL g_bHighlightMessages = 1;
239 /* Indicates if buffer is limited in size */
240 BOOL g_bLimitBufferSize = 1;
242 /* Maximum number of lines allowed in buffer when limited */
243 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
246 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
248 /* Size of font to use */
249 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
252 /* FIXME: this is a kludge */
254 const char * g_actions_file = NULL;
255 const char * g_re_filterfile = NULL;
257 const char * g_trustfile = NULL;
258 #endif /* def FEATURE_TRUST */
260 /* FIXME: end kludge */
262 /* Regular expression for detected URLs */
263 #define RE_URL "http:[^ \n\r]*"
266 * Regular expressions that are used to perform highlight in the log window
268 static struct _Pattern
273 } patterns_to_highlight[] =
276 { RE_URL, STYLE_LINK },
277 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
278 /* interesting text to highlight */
279 { "crunch!", STYLE_HIGHLIGHT },
281 { "referer:", STYLE_HEADER },
282 { "proxy-connection:", STYLE_HEADER },
283 { "proxy-agent:", STYLE_HEADER },
284 { "user-agent:", STYLE_HEADER },
285 { "host:", STYLE_HEADER },
286 { "accept:", STYLE_HEADER },
287 { "accept-encoding:", STYLE_HEADER },
288 { "accept-language:", STYLE_HEADER },
289 { "accept-charset:", STYLE_HEADER },
290 { "accept-ranges:", STYLE_HEADER },
291 { "date:", STYLE_HEADER },
292 { "cache-control:", STYLE_HEADER },
293 { "cache-last-checked:", STYLE_HEADER },
294 { "connection:", STYLE_HEADER },
295 { "content-type", STYLE_HEADER },
296 { "content-length", STYLE_HEADER },
297 { "cookie", STYLE_HEADER },
298 { "last-modified:", STYLE_HEADER },
299 { "pragma:", STYLE_HEADER },
300 { "server:", STYLE_HEADER },
301 { "etag:", STYLE_HEADER },
302 { "expires:", STYLE_HEADER },
303 { "warning:", STYLE_HEADER },
304 /* this is the terminator statement - do not delete! */
316 static CRITICAL_SECTION g_criticalsection;
317 static HWND g_hwndTray;
318 static HWND g_hwndLogBox;
319 static WNDPROC g_fnLogBox;
320 static HICON g_hiconAnim[ANIM_FRAMES];
321 static HICON g_hiconIdle;
322 static HICON g_hiconApp;
323 static int g_nAnimFrame;
324 static BOOL g_bClipPending = FALSE;
325 static int g_nRichEditVersion = 0;
330 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
331 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
332 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
333 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
334 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
335 static BOOL InitRichEdit(void);
336 static void LogClipBuffer(void);
337 static void LogCreatePatternMatchingBuffers(void);
338 static void LogDestroyPatternMatchingBuffers(void);
339 static int LogPutStringNoMatch(const char *pszText, int style);
342 /*********************************************************************
344 * Function : InitLogWindow
346 * Description : Initialise the log window.
350 * Returns : Always TRUE (there should be error checking on the resources).
352 *********************************************************************/
353 BOOL InitLogWindow(void)
358 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
359 for (i = 0; i < ANIM_FRAMES; i++)
361 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
363 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
365 /* Create the user interface */
366 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
367 g_hwndTray = CreateTrayWindow(g_hInstance);
368 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
370 /* Create pattern matching buffers (for highlighting */
371 LogCreatePatternMatchingBuffers();
373 /* Create a critical section to protect multi-threaded access to certain things */
374 InitializeCriticalSection(&g_criticalsection);
381 /*********************************************************************
383 * Function : TermLogWindow
385 * Description : Cleanup the logwindow.
391 *********************************************************************/
392 void TermLogWindow(void)
396 LogDestroyPatternMatchingBuffers();
398 TrayDeleteIcon(g_hwndTray, 1);
399 DeleteObject(g_hiconApp);
400 DeleteObject(g_hiconIdle);
401 for (i = 0; i < ANIM_FRAMES; i++)
403 DeleteObject(g_hiconAnim[i]);
409 /*********************************************************************
411 * Function : LogCreatePatternMatchingBuffers
413 * Description : Compile the pattern matching buffers.
419 *********************************************************************/
420 void LogCreatePatternMatchingBuffers(void)
423 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
425 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
430 /*********************************************************************
432 * Function : LogDestroyPatternMatchingBuffers
434 * Description : Free up the pattern matching buffers.
440 *********************************************************************/
441 void LogDestroyPatternMatchingBuffers(void)
444 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
446 regfree(&patterns_to_highlight[i].buffer);
451 /*********************************************************************
453 * Function : LogGetURLUnderCursor
455 * Description : Returns the URL from under the cursor (remember to free it!).
459 * Returns : NULL or a pointer to an URL string.
461 *********************************************************************/
462 char *LogGetURLUnderCursor(void)
464 char *szResult = NULL;
469 DWORD nWordStart = 0;
472 regcomp(&re, RE_URL, REG_ICASE);
474 /* Get the position of the cursor over the text window */
475 GetCursorPos(&ptCursor);
476 ScreenToClient(g_hwndLogBox, &ptCursor);
480 /* Search backwards and fowards to obtain the word that is highlighted */
481 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
482 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
483 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
485 /* Compare the string to the pattern */
486 if (nWordEnd > nWordStart)
491 range.chrg.cpMin = nWordStart;
492 range.chrg.cpMax = nWordEnd;
493 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
494 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
496 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
498 szResult = range.lpstrText;
502 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)
532 if (pszText == NULL || strlen(pszText) == 0)
542 /* Critical section stops multiple threads doing nasty interactions that
543 * foul up the highlighting and output.
545 EnterCriticalSection(&g_criticalsection);
547 if (g_bHighlightMessages)
551 /* First things first, regexp scan for various things that we would like highlighted */
552 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
554 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
556 char *pszBefore = NULL;
557 char *pszMatch = NULL;
558 char *pszAfter = NULL;
561 /* Split the string up into pieces representing the strings, before
562 at and after the matching pattern
566 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
567 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
568 strncpy(pszBefore, pszText, match.rm_so);
570 if (match.rm_eo < (regoff_t)strlen(pszText))
572 pszAfter = strdup(&pszText[match.rm_eo]);
574 nMatchSize = match.rm_eo - match.rm_so;
575 pszMatch = (char *)malloc(nMatchSize + 1);
576 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
577 pszMatch[nMatchSize] = '\0';
579 /* Recursively call LogPutString */
582 LogPutString(pszBefore);
587 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
592 LogPutString(pszAfter);
602 result = LogPutStringNoMatch(pszText, STYLE_NONE);
605 LeaveCriticalSection(&g_criticalsection);
612 /*********************************************************************
614 * Function : LogPutStringNoMatch
616 * Description : Puts a string into the logging window.
619 * 1 : pszText = pointer to string going to the log window
620 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
622 * Returns : Always 1 => success.
623 * FIXME: this is backwards to the rest of IJB and to common
624 * programming practice. Please use 0 => success instead.
626 *********************************************************************/
627 int LogPutStringNoMatch(const char *pszText, int style)
633 assert(g_hwndLogBox);
634 if (g_hwndLogBox == NULL)
639 /* TODO preserve existing selection */
641 /* Go to the end of the text */
642 nTextLength = GetWindowTextLength(g_hwndLogBox);
643 range.cpMin = nTextLength;
644 range.cpMax = nTextLength;
645 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
647 /* Apply a formatting style */
648 memset(&format, 0, sizeof(format));
649 format.cbSize = sizeof(format);
650 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
651 format.yHeight = (g_nFontSize * 1440) / 72;
652 strcpy(format.szFaceName, g_szFontFaceName);
653 if (style == STYLE_NONE)
656 format.dwEffects |= CFE_AUTOCOLOR;
658 else if (style == STYLE_HEADER)
660 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
662 else if (style == STYLE_HIGHLIGHT)
664 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
666 else if (style == STYLE_LINK)
668 format.dwEffects |= CFE_UNDERLINE;
669 format.crTextColor = RGB(0, 0, 255);
671 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
673 /* Append text to the end */
674 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
676 /* TODO Restore the old selection */
679 if (strchr(pszText, '\n') != NULL)
681 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
684 /* Set the force clip timer going. This timer ensures clipping is done
685 intermittently even when there is a sustained burst of logging
687 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
689 g_bClipPending = TRUE;
697 /*********************************************************************
699 * Function : LogShowActivity
701 * Description : Start the spinner.
707 *********************************************************************/
708 void LogShowActivity(void)
710 /* Start some activity timers */
711 if (g_bShowActivityAnimation)
713 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
714 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
720 /*********************************************************************
722 * Function : LogClipBuffer
724 * Description : Prunes old lines from the log.
730 *********************************************************************/
731 void LogClipBuffer(void)
733 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
734 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
736 /* Compute the range representing the lines to be deleted */
737 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
738 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
741 range.cpMax = nLastChar;
743 /* TODO get current selection */
745 /* TODO adjust and clip old selection against range to be deleted */
747 /* Select range and erase it (turning off autoscroll to prevent
749 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
750 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
751 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
752 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
754 /* Restore old selection */
760 /*********************************************************************
762 * Function : CreateHiddenLogOwnerWindow
764 * Description : Creates a hidden owner window that stops the log
765 * window appearing in the task bar.
768 * 1 : hInstance = application's instance handle
770 * Returns : Handle to newly created window.
772 *********************************************************************/
773 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
775 static const char *szWndName = "PrivoxyLogOwner";
780 wc.lpfnWndProc = LogOwnerWindowProc;
783 wc.hInstance = hInstance;
786 wc.hbrBackground = 0;
788 wc.lpszClassName = szWndName;
792 hwnd = CreateWindow(szWndName, szWndName,
793 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
794 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
801 /*********************************************************************
803 * Function : LogOwnerWindowProc
805 * Description : Dummy procedure that does nothing special.
808 * 1 : hwnd = window handle
809 * 2 : uMsg = message number
810 * 3 : wParam = first param for this message
811 * 4 : lParam = next param for this message
813 * Returns : Same as `DefWindowProc'.
815 *********************************************************************/
816 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
818 return DefWindowProc(hwnd, uMsg, wParam, lParam);
823 /*********************************************************************
825 * Function : CreateLogWindow
827 * Description : Create the logging window.
830 * 1 : hInstance = application's instance handle
831 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
833 * Returns : Handle to newly created window.
835 *********************************************************************/
836 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
838 static const char *szWndName = "PrivoxyLogWindow";
839 static const char *szWndTitle = "Privoxy";
842 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
846 memset(&wc, 0, sizeof(wc));
847 wc.cbSize = sizeof(wc);
848 wc.style = CS_DBLCLKS;
849 wc.lpfnWndProc = LogWindowProc;
852 wc.hInstance = hInstance;
853 wc.hIcon = g_hiconApp;
855 wc.hbrBackground = 0;
856 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
857 wc.lpszClassName = szWndName;
858 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
859 RegisterClassEx(&wc);
861 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
862 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
863 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
865 /* Now create a child list box */
866 GetClientRect(hwnd, &rcClient);
868 /* Create a rich edit control */
870 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
871 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
872 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
873 hwnd, NULL, hInstance, NULL);
874 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
876 /* Subclass the control to catch certain messages */
877 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
878 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
880 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
881 if (!g_bShowOnTaskBar &&
882 (nCmdShow == SW_SHOWMINIMIZED ||
883 nCmdShow == SW_MINIMIZE ||
884 nCmdShow == SW_SHOWMINNOACTIVE))
889 ShowWindow(hwnd, nCmdShow);
892 GetClientRect(g_hwndLogFrame, &rcClient);
893 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
900 /*********************************************************************
902 * Function : InitRichEdit
904 * Description : Initialise the rich edit control library.
908 * Returns : TRUE => success, FALSE => failure.
909 * FIXME: this is backwards to the rest of IJB and to common
910 * programming practice. Please use 0 => success instead.
912 *********************************************************************/
913 BOOL InitRichEdit(void)
915 static HINSTANCE hInstRichEdit;
916 if (hInstRichEdit == NULL)
918 g_nRichEditVersion = 0;
919 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
922 g_nRichEditVersion = _RICHEDIT_VER;
926 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
929 g_nRichEditVersion = 0x0100;
933 return (hInstRichEdit != NULL) ? TRUE : FALSE;
938 /*********************************************************************
940 * Function : ShowLogWindow
942 * Description : Shows or hides the log window. We will also raise the
943 * window on a show command in case it is buried.
946 * 1 : bShow = TRUE to show, FALSE to mimize/hide
950 *********************************************************************/
951 void ShowLogWindow(BOOL bShow)
955 SetForegroundWindow(g_hwndLogFrame);
956 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
958 else if (g_bShowOnTaskBar)
960 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
964 ShowWindow(g_hwndLogFrame, SW_HIDE);
970 /*********************************************************************
972 * Function : EditFile
974 * Description : Opens the specified setting file for editing.
975 * FIXME: What if the file has no associated application. Check for return values
976 * from ShellExecute??
979 * 1 : filename = filename from the config (aka config.txt) file.
983 *********************************************************************/
984 void EditFile(const char *filename)
988 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
994 /*--------------------------------------------------------------------------*/
995 /* Windows message handlers */
996 /*--------------------------------------------------------------------------*/
999 /*********************************************************************
1001 * Function : OnLogRButtonUp
1003 * Description : Handler for WM_RBUTTONUP messages.
1006 * 1 : nModifier = wParam from mouse message (unused)
1007 * 2 : x = x coordinate of the mouse event
1008 * 3 : y = y coordinate of the mouse event
1012 *********************************************************************/
1013 void OnLogRButtonUp(int nModifier, int x, int y)
1015 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1018 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1020 /* Check if there is a selection */
1022 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1023 if (range.cpMin == range.cpMax)
1025 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1029 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1032 /* Display the popup */
1033 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1040 /*********************************************************************
1042 * Function : OnLogCommand
1044 * Description : Handler for WM_COMMAND messages.
1047 * 1 : nCommand = the command portion of the menu selection event
1051 *********************************************************************/
1052 void OnLogCommand(int nCommand)
1057 ShowLogWindow(TRUE);
1061 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1065 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1068 case ID_VIEW_CLEARLOG:
1069 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1072 case ID_VIEW_LOGMESSAGES:
1073 g_bLogMessages = !g_bLogMessages;
1074 /* SaveLogSettings(); */
1077 case ID_VIEW_MESSAGEHIGHLIGHTING:
1078 g_bHighlightMessages = !g_bHighlightMessages;
1079 /* SaveLogSettings(); */
1082 case ID_VIEW_LIMITBUFFERSIZE:
1083 g_bLimitBufferSize = !g_bLimitBufferSize;
1084 /* SaveLogSettings(); */
1087 case ID_VIEW_ACTIVITYANIMATION:
1088 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1089 /* SaveLogSettings(); */
1092 #ifdef FEATURE_TOGGLE
1093 /* by haroon - change toggle to its opposite value */
1094 case ID_TOGGLE_ENABLED:
1095 g_bToggleIJB = !g_bToggleIJB;
1098 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1102 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1105 #endif /* def FEATURE_TOGGLE */
1107 case ID_TOOLS_EDITCONFIG:
1108 EditFile(configfile);
1111 case ID_TOOLS_EDITACTIONS:
1112 EditFile(g_actions_file);
1115 case ID_TOOLS_EDITFILTERS:
1116 EditFile(g_re_filterfile);
1119 #ifdef FEATURE_TRUST
1120 case ID_TOOLS_EDITTRUST:
1121 EditFile(g_trustfile);
1123 #endif /* def FEATURE_TRUST */
1126 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1130 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1133 case ID_HELP_MANUAL:
1134 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1137 case ID_HELP_STATUS:
1138 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1142 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1153 /*********************************************************************
1155 * Function : OnLogInitMenu
1157 * Description : Handler for WM_INITMENU messages. Enable, disable,
1158 * check, and/or uncheck menu options as apropos.
1161 * 1 : hmenu = handle to menu to "make current"
1165 *********************************************************************/
1166 void OnLogInitMenu(HMENU hmenu)
1168 /* Only enable editors if there is a file to edit */
1169 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1170 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1171 #ifdef FEATURE_TRUST
1172 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1173 #endif /* def FEATURE_TRUST */
1175 /* Check/uncheck options */
1176 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1177 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1178 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1179 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1180 #ifdef FEATURE_TOGGLE
1181 /* by haroon - menu item for Enable toggle on/off */
1182 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1183 #endif /* def FEATURE_TOGGLE */
1188 /*********************************************************************
1190 * Function : OnLogTimer
1192 * Description : Handler for WM_TIMER messages.
1195 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1199 *********************************************************************/
1200 void OnLogTimer(int nTimer)
1205 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1208 case TIMER_ANIMSTOP_ID:
1210 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1211 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1212 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1215 case TIMER_CLIPBUFFER_ID:
1216 case TIMER_CLIPBUFFER_FORCE_ID:
1218 g_bClipPending = FALSE;
1219 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1220 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1231 /*********************************************************************
1233 * Function : LogRichEditProc
1235 * Description : Window subclass routine handles some events for the rich edit control.
1238 * 1 : hwnd = window handle of the rich edit control
1239 * 2 : uMsg = message number
1240 * 3 : wParam = first param for this message
1241 * 4 : lParam = next param for this message
1243 * Returns : Appropriate M$ window message handler codes.
1245 *********************************************************************/
1246 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1253 pt.x = LOWORD(lParam);
1254 pt.y = HIWORD(lParam);
1255 ClientToScreen(hwnd, &pt);
1256 OnLogRButtonUp(wParam, pt.x, pt.y);
1260 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1265 /*********************************************************************
1267 * Function : LogWindowProc
1269 * Description : Windows call back routine handles events on the log window.
1272 * 1 : hwnd = handle of the logging window
1273 * 2 : uMsg = message number
1274 * 3 : wParam = first param for this message
1275 * 4 : lParam = next param for this message
1277 * Returns : Appropriate M$ window message handler codes.
1279 *********************************************************************/
1280 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1288 /* This is the end - beautiful friend - the end */
1289 DestroyWindow(g_hwndLogBox);
1290 DestroyWindow(g_hwndLogFrame);
1299 /* Resize the logging window to fit the new frame */
1303 GetClientRect(g_hwndLogFrame, &rc);
1304 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1309 OnLogInitMenu((HMENU) wParam);
1317 OnLogCommand(LOWORD(wParam));
1324 if (g_bCloseHidesWindow)
1326 ShowLogWindow(FALSE);
1331 ShowLogWindow(FALSE);
1337 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1341 #endif /* ndef _WIN_CONSOLE - entire file */