1 const char w32log_rcs[] = "$Id: w32log.c,v 2.1 2002/06/04 16:37:48 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa//current/src/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 2.1 2002/06/04 16:37:48 jongfoster
36 * Adding Doxygen-style comments to variables
38 * Revision 2.0 2002/06/04 14:34:21 jongfoster
39 * Moving source files to src/
41 * Revision 1.25 2002/04/04 00:36:36 gliptak
42 * always use pcre for matching
44 * Revision 1.24 2002/03/31 17:19:00 jongfoster
45 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
47 * Revision 1.23 2002/03/26 22:57:10 jongfoster
48 * Web server name should begin www.
50 * Revision 1.22 2002/03/24 12:48:23 jongfoster
53 * Revision 1.21 2002/03/24 12:07:35 jongfoster
54 * Consistern name for filters file
56 * Revision 1.20 2002/03/24 12:03:47 jongfoster
59 * Revision 1.19 2002/01/17 21:04:17 jongfoster
60 * Replacing hard references to the URL of the config interface
61 * with #defines from project.h
63 * Revision 1.18 2001/11/30 23:37:24 jongfoster
64 * Renaming the Win32 config file to config.txt - this is almost the
65 * same as the corresponding UNIX name "config"
67 * Revision 1.17 2001/11/16 00:46:31 jongfoster
68 * Fixing compiler warnings
70 * Revision 1.16 2001/08/01 19:58:12 jongfoster
71 * Fixing documentation filenames in help menu, and making status
72 * option work without needing the "Junkbuster Status.URL" file.
74 * Revision 1.15 2001/07/30 22:08:36 jongfoster
75 * Tidying up #defines:
76 * - All feature #defines are now of the form FEATURE_xxx
77 * - Permanently turned off WIN_GUI_EDIT
78 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
80 * Revision 1.14 2001/07/29 18:47:05 jongfoster
81 * Adding missing #include "loadcfg.h"
83 * Revision 1.13 2001/07/19 19:15:14 haroon
84 * - Added a FIXME for EditFile but didn't fix :-)
86 * Revision 1.12 2001/07/13 14:04:59 oes
87 * Removed all #ifdef PCRS
89 * Revision 1.11 2001/06/07 23:08:12 jongfoster
90 * Forward and ACL edit options removed.
92 * Revision 1.10 2001/05/31 21:37:11 jongfoster
93 * GUI changes to rename "permissions file" to "actions file".
95 * Revision 1.9 2001/05/31 17:33:13 oes
99 * Revision 1.8 2001/05/29 09:50:24 jongfoster
100 * Unified blocklist/imagelist/permissionslist.
101 * File format is still under discussion, but the internal changes
104 * Also modified interceptor behaviour:
105 * - We now intercept all URLs beginning with one of the following
106 * prefixes (and *only* these prefixes):
108 * * http://ijbswa.sf.net/config/
109 * * http://ijbswa.sourceforge.net/config/
110 * - New interceptors "home page" - go to http://i.j.b/ to see it.
111 * - Internal changes so that intercepted and fast redirect pages
112 * are not replaced with an image.
113 * - Interceptors now have the option to send a binary page direct
114 * to the client. (i.e. ijb-send-banner uses this)
115 * - Implemented show-url-info interceptor. (Which is why I needed
116 * the above interceptors changes - a typical URL is
117 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
118 * The previous mechanism would not have intercepted that, and
119 * if it had been intercepted then it then it would have replaced
122 * Revision 1.7 2001/05/26 01:26:34 jongfoster
123 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
124 * This #define cannot be set from ./configure - there's no point, it
125 * doesn't work yet. See feature request # 425722
127 * Revision 1.6 2001/05/26 00:31:30 jongfoster
128 * Fixing compiler warning about comparing signed/unsigned.
130 * Revision 1.5 2001/05/26 00:28:36 jongfoster
131 * Automatic reloading of config file.
132 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
133 * Most of the global variables have been moved to a new
134 * struct configuration_spec, accessed through csp->config->globalname
135 * Most of the globals remaining are used by the Win32 GUI.
137 * Revision 1.4 2001/05/22 18:56:28 oes
140 * Revision 1.3 2001/05/20 15:07:54 jongfoster
141 * File is now ignored if _WIN_CONSOLE is defined.
143 * Revision 1.2 2001/05/20 01:21:20 jongfoster
144 * Version 2.9.4 checkin.
145 * - Merged popupfile and cookiefile, and added control over PCRS
146 * filtering, in new "permissionsfile".
147 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
148 * file error you now get a message box (in the Win32 GUI) rather
149 * than the program exiting with no explanation.
150 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
152 * - Removed tabs from "config"
153 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
154 * - Bumped up version number.
156 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
157 * Initial import of version 2.9.3 source tree
160 *********************************************************************/
172 #include <richedit.h>
176 #include "w32taskbar.h"
180 #include "miscutil.h"
184 const char w32res_h_rcs[] = W32RES_H_VERSION;
188 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
191 const char w32log_h_rcs[] = W32LOG_H_VERSION;
193 #ifndef _WIN_CONSOLE /* entire file */
196 * Timers and the various durations
198 #define TIMER_ANIM_ID 1
199 #define TIMER_ANIM_TIME 100
200 #define TIMER_ANIMSTOP_ID 2
201 #define TIMER_ANIMSTOP_TIME 1000
202 #define TIMER_CLIPBUFFER_ID 3
203 #define TIMER_CLIPBUFFER_TIME 1000
204 #define TIMER_CLIPBUFFER_FORCE_ID 4
205 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
208 * Styles of text that can be output
211 #define STYLE_HIGHLIGHT 1
213 #define STYLE_HEADER 3
216 * Number of frames of animation in tray activity sequence
218 #define ANIM_FRAMES 8
220 #define DEFAULT_MAX_BUFFER_LINES 200
221 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
222 #define DEFAULT_LOG_FONT_SIZE 8
225 * These values affect the way the log window behaves, they should be read
226 * from a file but for the moment, they are hardcoded here. Some options are
227 * configurable through the UI.
230 /** Indicates whether task bar shows activity animation */
231 BOOL g_bShowActivityAnimation = 1;
233 /** Indicates whether the log window is shown */
234 BOOL g_bShowLogWindow = 1;
236 /** Indicates if the log window appears on the task bar */
237 BOOL g_bShowOnTaskBar = 0;
239 /** Indicates whether closing the log window really just hides it */
240 BOOL g_bCloseHidesWindow = 1;
242 /** Indicates if messages are logged at all */
243 BOOL g_bLogMessages = 1;
245 /** Indicates whether log messages are highlighted */
246 BOOL g_bHighlightMessages = 1;
248 /** Indicates if buffer is limited in size */
249 BOOL g_bLimitBufferSize = 1;
251 /** Maximum number of lines allowed in buffer when limited */
252 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
255 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
257 /** Size of font to use */
258 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
261 /* FIXME: this is a kludge */
263 /** Second (=default, hopefully) Actions file name. */
264 const char * g_default_actions_file = NULL;
266 /** Third (=user, hopefully) Actions file name. */
267 const char * g_user_actions_file = NULL;
269 /** Filter file name. */
270 const char * g_re_filterfile = NULL;
274 /** Trust file name. */
275 const char * g_trustfile = NULL;
277 #endif /* def FEATURE_TRUST */
279 /* FIXME: end kludge */
282 /** Regular expression for detected URLs */
283 #define RE_URL "http:[^ \n\r]*"
287 * Regular expressions that are used to perform highlight in the log window.
289 static struct _Pattern
291 const char *str; /**< The pattern to match. */
292 int style; /**< How to highlight it - one of the STYLE_xxx constants. */
293 regex_t buffer; /**< The compiled pattern to use for matching. Generated from str. */
294 } patterns_to_highlight[] =
297 { RE_URL, STYLE_LINK },
298 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
299 /* interesting text to highlight */
300 { "crunch!", STYLE_HIGHLIGHT },
302 { "referer:", STYLE_HEADER },
303 { "proxy-connection:", STYLE_HEADER },
304 { "proxy-agent:", STYLE_HEADER },
305 { "user-agent:", STYLE_HEADER },
306 { "host:", STYLE_HEADER },
307 { "accept:", STYLE_HEADER },
308 { "accept-encoding:", STYLE_HEADER },
309 { "accept-language:", STYLE_HEADER },
310 { "accept-charset:", STYLE_HEADER },
311 { "accept-ranges:", STYLE_HEADER },
312 { "date:", STYLE_HEADER },
313 { "cache-control:", STYLE_HEADER },
314 { "cache-last-checked:", STYLE_HEADER },
315 { "connection:", STYLE_HEADER },
316 { "content-type", STYLE_HEADER },
317 { "content-length", STYLE_HEADER },
318 { "cookie", STYLE_HEADER },
319 { "last-modified:", STYLE_HEADER },
320 { "pragma:", STYLE_HEADER },
321 { "server:", STYLE_HEADER },
322 { "etag:", STYLE_HEADER },
323 { "expires:", STYLE_HEADER },
324 { "warning:", STYLE_HEADER },
325 /* this is the terminator statement - do not delete! */
334 * Window handle for the log window.
343 /** Critical section used to serialize output to the log window. */
344 static CRITICAL_SECTION g_criticalsection;
346 /** The window handle of the tray icon window. */
347 static HWND g_hwndTray;
349 /** The window handle of the log window. */
350 static HWND g_hwndLogBox;
352 /** The default window procedure for the rich edit control. Set when we
353 subclass this window. */
354 static WNDPROC g_fnLogBox;
356 /** The icons for the activity animation. */
357 static HICON g_hiconAnim[ANIM_FRAMES];
359 /** The icon to use when idle. */
360 static HICON g_hiconIdle;
362 /** The main application icon. */
363 static HICON g_hiconApp;
365 /** The index of the current animation frame. */
366 static int g_nAnimFrame;
368 /** If a timer to call LogClipBuffer() has been started. */
369 static BOOL g_bClipPending = FALSE;
371 /** The version of the rich edit control we're using. */
372 static int g_nRichEditVersion = 0;
378 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
379 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
380 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
381 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
382 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
383 static BOOL InitRichEdit(void);
384 static void LogClipBuffer(void);
385 static void LogCreatePatternMatchingBuffers(void);
386 static void LogDestroyPatternMatchingBuffers(void);
387 static int LogPutStringNoMatch(const char *pszText, int style);
390 /*********************************************************************
392 * Function : InitLogWindow
394 * Description : Initialise the log window.
398 * Returns : Always TRUE (there should be error checking on the resources).
400 *********************************************************************/
401 BOOL InitLogWindow(void)
406 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
407 for (i = 0; i < ANIM_FRAMES; i++)
409 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
411 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
413 /* Create the user interface */
414 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
415 g_hwndTray = CreateTrayWindow(g_hInstance);
416 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
418 /* Create pattern matching buffers (for highlighting */
419 LogCreatePatternMatchingBuffers();
421 /* Create a critical section to protect multi-threaded access to certain things */
422 InitializeCriticalSection(&g_criticalsection);
429 /*********************************************************************
431 * Function : TermLogWindow
433 * Description : Cleanup the logwindow.
439 *********************************************************************/
440 void TermLogWindow(void)
444 LogDestroyPatternMatchingBuffers();
446 TrayDeleteIcon(g_hwndTray, 1);
447 DeleteObject(g_hiconApp);
448 DeleteObject(g_hiconIdle);
449 for (i = 0; i < ANIM_FRAMES; i++)
451 DeleteObject(g_hiconAnim[i]);
457 /*********************************************************************
459 * Function : LogCreatePatternMatchingBuffers
461 * Description : Compile the pattern matching buffers.
467 *********************************************************************/
468 void LogCreatePatternMatchingBuffers(void)
471 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
473 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
478 /*********************************************************************
480 * Function : LogDestroyPatternMatchingBuffers
482 * Description : Free up the pattern matching buffers.
488 *********************************************************************/
489 void LogDestroyPatternMatchingBuffers(void)
492 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
494 regfree(&patterns_to_highlight[i].buffer);
499 /*********************************************************************
501 * Function : LogGetURLUnderCursor
503 * Description : Returns the URL from under the cursor (remember to free it!).
507 * Returns : NULL or a pointer to an URL string.
509 *********************************************************************/
510 char *LogGetURLUnderCursor(void)
512 char *szResult = NULL;
517 DWORD nWordStart = 0;
520 regcomp(&re, RE_URL, REG_ICASE);
522 /* Get the position of the cursor over the text window */
523 GetCursorPos(&ptCursor);
524 ScreenToClient(g_hwndLogBox, &ptCursor);
528 /* Search backwards and fowards to obtain the word that is highlighted */
529 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
530 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
531 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
533 /* Compare the string to the pattern */
534 if (nWordEnd > nWordStart)
539 range.chrg.cpMin = nWordStart;
540 range.chrg.cpMax = nWordEnd;
541 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
542 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
544 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
546 szResult = range.lpstrText;
550 free(range.lpstrText);
560 /*********************************************************************
562 * Function : LogPutString
564 * Description : Inserts text into the logging window. This is really
565 * a regexp aware wrapper function to `LogPutStringNoMatch'.
568 * 1 : pszText = pointer to string going to the log window
570 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
571 * FIXME: this is backwards to the rest of IJB and to common
572 * programming practice. Please use 0 => success instead.
574 *********************************************************************/
575 int LogPutString(const char *pszText)
580 if (pszText == NULL || strlen(pszText) == 0)
590 /* Critical section stops multiple threads doing nasty interactions that
591 * foul up the highlighting and output.
593 EnterCriticalSection(&g_criticalsection);
595 if (g_bHighlightMessages)
599 /* First things first, regexp scan for various things that we would like highlighted */
600 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
602 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
604 char *pszBefore = NULL;
605 char *pszMatch = NULL;
606 char *pszAfter = NULL;
609 /* Split the string up into pieces representing the strings, before
610 at and after the matching pattern
614 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
615 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
616 strncpy(pszBefore, pszText, match.rm_so);
618 if (match.rm_eo < (regoff_t)strlen(pszText))
620 pszAfter = strdup(&pszText[match.rm_eo]);
622 nMatchSize = match.rm_eo - match.rm_so;
623 pszMatch = (char *)malloc(nMatchSize + 1);
624 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
625 pszMatch[nMatchSize] = '\0';
627 /* Recursively call LogPutString */
630 LogPutString(pszBefore);
635 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
640 LogPutString(pszAfter);
650 result = LogPutStringNoMatch(pszText, STYLE_NONE);
653 LeaveCriticalSection(&g_criticalsection);
660 /*********************************************************************
662 * Function : LogPutStringNoMatch
664 * Description : Puts a string into the logging window.
667 * 1 : pszText = pointer to string going to the log window
668 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
670 * Returns : Always 1 => success.
671 * FIXME: this is backwards to the rest of IJB and to common
672 * programming practice. Please use 0 => success instead.
674 *********************************************************************/
675 int LogPutStringNoMatch(const char *pszText, int style)
681 assert(g_hwndLogBox);
682 if (g_hwndLogBox == NULL)
687 /* TODO preserve existing selection */
689 /* Go to the end of the text */
690 nTextLength = GetWindowTextLength(g_hwndLogBox);
691 range.cpMin = nTextLength;
692 range.cpMax = nTextLength;
693 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
695 /* Apply a formatting style */
696 memset(&format, 0, sizeof(format));
697 format.cbSize = sizeof(format);
698 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
699 format.yHeight = (g_nFontSize * 1440) / 72;
700 strcpy(format.szFaceName, g_szFontFaceName);
701 if (style == STYLE_NONE)
704 format.dwEffects |= CFE_AUTOCOLOR;
706 else if (style == STYLE_HEADER)
708 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
710 else if (style == STYLE_HIGHLIGHT)
712 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
714 else if (style == STYLE_LINK)
716 format.dwEffects |= CFE_UNDERLINE;
717 format.crTextColor = RGB(0, 0, 255);
719 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
721 /* Append text to the end */
722 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
724 /* TODO Restore the old selection */
727 if (strchr(pszText, '\n') != NULL)
729 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
732 /* Set the force clip timer going. This timer ensures clipping is done
733 intermittently even when there is a sustained burst of logging
735 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
737 g_bClipPending = TRUE;
745 /*********************************************************************
747 * Function : LogShowActivity
749 * Description : Start the spinner.
755 *********************************************************************/
756 void LogShowActivity(void)
758 /* Start some activity timers */
759 if (g_bShowActivityAnimation)
761 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
762 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
768 /*********************************************************************
770 * Function : LogClipBuffer
772 * Description : Prunes old lines from the log.
773 * This is called occasionally, from a timer callback.
779 *********************************************************************/
780 void LogClipBuffer(void)
782 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
783 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
785 /* Compute the range representing the lines to be deleted */
786 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
787 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
790 range.cpMax = nLastChar;
792 /* TODO get current selection */
794 /* TODO adjust and clip old selection against range to be deleted */
796 /* Select range and erase it (turning off autoscroll to prevent
798 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
799 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
800 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
801 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
803 /* Restore old selection */
809 /*********************************************************************
811 * Function : CreateHiddenLogOwnerWindow
813 * Description : Creates a hidden owner window that stops the log
814 * window appearing in the task bar.
817 * 1 : hInstance = application's instance handle
819 * Returns : Handle to newly created window.
821 *********************************************************************/
822 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
824 static const char *szWndName = "PrivoxyLogOwner";
829 wc.lpfnWndProc = LogOwnerWindowProc;
832 wc.hInstance = hInstance;
835 wc.hbrBackground = 0;
837 wc.lpszClassName = szWndName;
841 hwnd = CreateWindow(szWndName, szWndName,
842 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
843 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
850 /*********************************************************************
852 * Function : LogOwnerWindowProc
854 * Description : Dummy procedure that does nothing special.
857 * 1 : hwnd = window handle
858 * 2 : uMsg = message number
859 * 3 : wParam = first param for this message
860 * 4 : lParam = next param for this message
862 * Returns : Same as `DefWindowProc'.
864 *********************************************************************/
865 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
867 return DefWindowProc(hwnd, uMsg, wParam, lParam);
872 /*********************************************************************
874 * Function : CreateLogWindow
876 * Description : Create the logging window.
879 * 1 : hInstance = application's instance handle
880 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
882 * Returns : Handle to newly created window.
884 *********************************************************************/
885 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
887 static const char *szWndName = "PrivoxyLogWindow";
888 static const char *szWndTitle = "Privoxy";
891 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
895 memset(&wc, 0, sizeof(wc));
896 wc.cbSize = sizeof(wc);
897 wc.style = CS_DBLCLKS;
898 wc.lpfnWndProc = LogWindowProc;
901 wc.hInstance = hInstance;
902 wc.hIcon = g_hiconApp;
904 wc.hbrBackground = 0;
905 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
906 wc.lpszClassName = szWndName;
907 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
908 RegisterClassEx(&wc);
910 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
911 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
912 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
914 /* Now create a child list box */
915 GetClientRect(hwnd, &rcClient);
917 /* Create a rich edit control */
919 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
920 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
921 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
922 hwnd, NULL, hInstance, NULL);
923 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
925 /* Subclass the control to catch certain messages */
926 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
927 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
929 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
930 if (!g_bShowOnTaskBar &&
931 (nCmdShow == SW_SHOWMINIMIZED ||
932 nCmdShow == SW_MINIMIZE ||
933 nCmdShow == SW_SHOWMINNOACTIVE))
938 ShowWindow(hwnd, nCmdShow);
941 GetClientRect(g_hwndLogFrame, &rcClient);
942 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
949 /*********************************************************************
951 * Function : InitRichEdit
953 * Description : Initialise the rich edit control library.
957 * Returns : TRUE => success, FALSE => failure.
958 * FIXME: this is backwards to the rest of IJB and to common
959 * programming practice. Please use 0 => success instead.
961 *********************************************************************/
962 BOOL InitRichEdit(void)
964 static HINSTANCE hInstRichEdit;
965 if (hInstRichEdit == NULL)
967 g_nRichEditVersion = 0;
968 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
971 g_nRichEditVersion = _RICHEDIT_VER;
975 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
978 g_nRichEditVersion = 0x0100;
982 return (hInstRichEdit != NULL) ? TRUE : FALSE;
987 /*********************************************************************
989 * Function : ShowLogWindow
991 * Description : Shows or hides the log window. We will also raise the
992 * window on a show command in case it is buried.
995 * 1 : bShow = TRUE to show, FALSE to mimize/hide
999 *********************************************************************/
1000 void ShowLogWindow(BOOL bShow)
1004 SetForegroundWindow(g_hwndLogFrame);
1005 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1008 else if (g_bShowOnTaskBar)
1010 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1014 ShowWindow(g_hwndLogFrame, SW_HIDE);
1019 /*********************************************************************
1021 * Function : EditFile
1023 * Description : Opens the specified setting file for editing.
1024 * FIXME: What if the file has no associated application. Check for return values
1025 * from ShellExecute??
1028 * 1 : filename = filename from the config (aka config.txt) file.
1032 *********************************************************************/
1033 void EditFile(const char *filename)
1037 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1043 /*--------------------------------------------------------------------------*/
1044 /* Windows message handlers */
1045 /*--------------------------------------------------------------------------*/
1048 /*********************************************************************
1050 * Function : OnLogRButtonUp
1052 * Description : Handler for WM_RBUTTONUP messages.
1055 * 1 : nModifier = wParam from mouse message (unused)
1056 * 2 : x = x coordinate of the mouse event
1057 * 3 : y = y coordinate of the mouse event
1061 *********************************************************************/
1062 void OnLogRButtonUp(int nModifier, int x, int y)
1064 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1067 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1069 /* Check if there is a selection */
1071 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1072 if (range.cpMin == range.cpMax)
1074 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1078 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1081 /* Display the popup */
1082 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1089 /*********************************************************************
1091 * Function : OnLogCommand
1093 * Description : Handler for WM_COMMAND messages.
1096 * 1 : nCommand = the command portion of the menu selection event
1100 *********************************************************************/
1101 void OnLogCommand(int nCommand)
1105 case ID_TOGGLE_SHOWWINDOW:
1106 g_bShowLogWindow = !g_bShowLogWindow;
1108 ShowLogWindow(g_bShowLogWindow);
1112 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1116 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1119 case ID_VIEW_CLEARLOG:
1120 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1123 case ID_VIEW_LOGMESSAGES:
1124 g_bLogMessages = !g_bLogMessages;
1125 /* SaveLogSettings(); */
1128 case ID_VIEW_MESSAGEHIGHLIGHTING:
1129 g_bHighlightMessages = !g_bHighlightMessages;
1130 /* SaveLogSettings(); */
1133 case ID_VIEW_LIMITBUFFERSIZE:
1134 g_bLimitBufferSize = !g_bLimitBufferSize;
1135 /* SaveLogSettings(); */
1138 case ID_VIEW_ACTIVITYANIMATION:
1139 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1140 /* SaveLogSettings(); */
1143 #ifdef FEATURE_TOGGLE
1144 /* by haroon - change toggle to its opposite value */
1145 case ID_TOGGLE_ENABLED:
1146 g_bToggleIJB = !g_bToggleIJB;
1149 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1153 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1156 #endif /* def FEATURE_TOGGLE */
1158 case ID_TOOLS_EDITCONFIG:
1159 EditFile(configfile);
1162 case ID_TOOLS_EDITDEFAULTACTIONS:
1163 EditFile(g_default_actions_file);
1166 case ID_TOOLS_EDITUSERACTIONS:
1167 EditFile(g_user_actions_file);
1170 case ID_TOOLS_EDITFILTERS:
1171 EditFile(g_re_filterfile);
1174 #ifdef FEATURE_TRUST
1175 case ID_TOOLS_EDITTRUST:
1176 EditFile(g_trustfile);
1178 #endif /* def FEATURE_TRUST */
1181 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1185 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1188 case ID_HELP_MANUAL:
1189 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1192 case ID_HELP_STATUS:
1193 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1197 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1208 /*********************************************************************
1210 * Function : OnLogInitMenu
1212 * Description : Handler for WM_INITMENU messages. Enable, disable,
1213 * check, and/or uncheck menu options as apropos.
1216 * 1 : hmenu = handle to menu to "make current"
1220 *********************************************************************/
1221 void OnLogInitMenu(HMENU hmenu)
1223 /* Only enable editors if there is a file to edit */
1224 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1225 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1226 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1227 #ifdef FEATURE_TRUST
1228 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1229 #endif /* def FEATURE_TRUST */
1231 /* Check/uncheck options */
1232 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1233 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1234 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1235 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1236 #ifdef FEATURE_TOGGLE
1237 /* by haroon - menu item for Enable toggle on/off */
1238 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1239 #endif /* def FEATURE_TOGGLE */
1240 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1245 /*********************************************************************
1247 * Function : OnLogTimer
1249 * Description : Handler for WM_TIMER messages.
1252 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1256 *********************************************************************/
1257 void OnLogTimer(int nTimer)
1262 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1265 case TIMER_ANIMSTOP_ID:
1267 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1268 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1269 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1272 case TIMER_CLIPBUFFER_ID:
1273 case TIMER_CLIPBUFFER_FORCE_ID:
1275 g_bClipPending = FALSE;
1276 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1277 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1288 /*********************************************************************
1290 * Function : LogRichEditProc
1292 * Description : Window subclass routine handles some events for the rich edit control.
1295 * 1 : hwnd = window handle of the rich edit control
1296 * 2 : uMsg = message number
1297 * 3 : wParam = first param for this message
1298 * 4 : lParam = next param for this message
1300 * Returns : Appropriate M$ window message handler codes.
1302 *********************************************************************/
1303 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1310 pt.x = LOWORD(lParam);
1311 pt.y = HIWORD(lParam);
1312 ClientToScreen(hwnd, &pt);
1313 OnLogRButtonUp(wParam, pt.x, pt.y);
1317 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1322 /*********************************************************************
1324 * Function : LogWindowProc
1326 * Description : Windows call back routine handles events on the log window.
1329 * 1 : hwnd = handle of the logging window
1330 * 2 : uMsg = message number
1331 * 3 : wParam = first param for this message
1332 * 4 : lParam = next param for this message
1334 * Returns : Appropriate M$ window message handler codes.
1336 *********************************************************************/
1337 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1345 /* This is the end - beautiful friend - the end */
1346 DestroyWindow(g_hwndLogBox);
1347 DestroyWindow(g_hwndLogFrame);
1356 /* Resize the logging window to fit the new frame */
1360 GetClientRect(g_hwndLogFrame, &rc);
1361 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1366 OnLogInitMenu((HMENU) wParam);
1374 OnLogCommand(LOWORD(wParam));
1381 if (g_bCloseHidesWindow)
1383 ShowLogWindow(FALSE);
1388 ShowLogWindow(FALSE);
1394 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1398 #endif /* ndef _WIN_CONSOLE - entire file */