1 const char w32log_rcs[] = "$Id: w32log.c,v 2.3 2002/09/12 14:30:32 oes 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.3 2002/09/12 14:30:32 oes
36 * Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
38 * Revision 2.2 2002/09/05 08:43:11 oes
39 * Synced with the stable branch:
40 * Revision 1.25.2.1 2002/08/21 17:59:05 oes
41 * - "Show Privoxy Window" now a toggle
42 * - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
44 * Revision 2.1 2002/06/04 16:37:48 jongfoster
45 * Adding Doxygen-style comments to variables
47 * Revision 2.0 2002/06/04 14:34:21 jongfoster
48 * Moving source files to src/
50 * Revision 1.25 2002/04/04 00:36:36 gliptak
51 * always use pcre for matching
53 * Revision 1.24 2002/03/31 17:19:00 jongfoster
54 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
56 * Revision 1.23 2002/03/26 22:57:10 jongfoster
57 * Web server name should begin www.
59 * Revision 1.22 2002/03/24 12:48:23 jongfoster
62 * Revision 1.21 2002/03/24 12:07:35 jongfoster
63 * Consistern name for filters file
65 * Revision 1.20 2002/03/24 12:03:47 jongfoster
68 * Revision 1.19 2002/01/17 21:04:17 jongfoster
69 * Replacing hard references to the URL of the config interface
70 * with #defines from project.h
72 * Revision 1.18 2001/11/30 23:37:24 jongfoster
73 * Renaming the Win32 config file to config.txt - this is almost the
74 * same as the corresponding UNIX name "config"
76 * Revision 1.17 2001/11/16 00:46:31 jongfoster
77 * Fixing compiler warnings
79 * Revision 1.16 2001/08/01 19:58:12 jongfoster
80 * Fixing documentation filenames in help menu, and making status
81 * option work without needing the "Junkbuster Status.URL" file.
83 * Revision 1.15 2001/07/30 22:08:36 jongfoster
84 * Tidying up #defines:
85 * - All feature #defines are now of the form FEATURE_xxx
86 * - Permanently turned off WIN_GUI_EDIT
87 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
89 * Revision 1.14 2001/07/29 18:47:05 jongfoster
90 * Adding missing #include "loadcfg.h"
92 * Revision 1.13 2001/07/19 19:15:14 haroon
93 * - Added a FIXME for EditFile but didn't fix :-)
95 * Revision 1.12 2001/07/13 14:04:59 oes
96 * Removed all #ifdef PCRS
98 * Revision 1.11 2001/06/07 23:08:12 jongfoster
99 * Forward and ACL edit options removed.
101 * Revision 1.10 2001/05/31 21:37:11 jongfoster
102 * GUI changes to rename "permissions file" to "actions file".
104 * Revision 1.9 2001/05/31 17:33:13 oes
108 * Revision 1.8 2001/05/29 09:50:24 jongfoster
109 * Unified blocklist/imagelist/permissionslist.
110 * File format is still under discussion, but the internal changes
113 * Also modified interceptor behaviour:
114 * - We now intercept all URLs beginning with one of the following
115 * prefixes (and *only* these prefixes):
117 * * http://ijbswa.sf.net/config/
118 * * http://ijbswa.sourceforge.net/config/
119 * - New interceptors "home page" - go to http://i.j.b/ to see it.
120 * - Internal changes so that intercepted and fast redirect pages
121 * are not replaced with an image.
122 * - Interceptors now have the option to send a binary page direct
123 * to the client. (i.e. ijb-send-banner uses this)
124 * - Implemented show-url-info interceptor. (Which is why I needed
125 * the above interceptors changes - a typical URL is
126 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
127 * The previous mechanism would not have intercepted that, and
128 * if it had been intercepted then it then it would have replaced
131 * Revision 1.7 2001/05/26 01:26:34 jongfoster
132 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
133 * This #define cannot be set from ./configure - there's no point, it
134 * doesn't work yet. See feature request # 425722
136 * Revision 1.6 2001/05/26 00:31:30 jongfoster
137 * Fixing compiler warning about comparing signed/unsigned.
139 * Revision 1.5 2001/05/26 00:28:36 jongfoster
140 * Automatic reloading of config file.
141 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
142 * Most of the global variables have been moved to a new
143 * struct configuration_spec, accessed through csp->config->globalname
144 * Most of the globals remaining are used by the Win32 GUI.
146 * Revision 1.4 2001/05/22 18:56:28 oes
149 * Revision 1.3 2001/05/20 15:07:54 jongfoster
150 * File is now ignored if _WIN_CONSOLE is defined.
152 * Revision 1.2 2001/05/20 01:21:20 jongfoster
153 * Version 2.9.4 checkin.
154 * - Merged popupfile and cookiefile, and added control over PCRS
155 * filtering, in new "permissionsfile".
156 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
157 * file error you now get a message box (in the Win32 GUI) rather
158 * than the program exiting with no explanation.
159 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
161 * - Removed tabs from "config"
162 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
163 * - Bumped up version number.
165 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
166 * Initial import of version 2.9.3 source tree
169 *********************************************************************/
181 #include <richedit.h>
185 #include "w32taskbar.h"
189 #include "miscutil.h"
193 const char w32res_h_rcs[] = W32RES_H_VERSION;
197 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
200 const char w32log_h_rcs[] = W32LOG_H_VERSION;
202 #ifndef _WIN_CONSOLE /* entire file */
205 * Timers and the various durations
207 #define TIMER_ANIM_ID 1
208 #define TIMER_ANIM_TIME 100
209 #define TIMER_ANIMSTOP_ID 2
210 #define TIMER_ANIMSTOP_TIME 1000
211 #define TIMER_CLIPBUFFER_ID 3
212 #define TIMER_CLIPBUFFER_TIME 1000
213 #define TIMER_CLIPBUFFER_FORCE_ID 4
214 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
217 * Styles of text that can be output
220 #define STYLE_HIGHLIGHT 1
222 #define STYLE_HEADER 3
225 * Number of frames of animation in tray activity sequence
227 #define ANIM_FRAMES 8
229 #define DEFAULT_MAX_BUFFER_LINES 200
230 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
231 #define DEFAULT_LOG_FONT_SIZE 8
234 * These values affect the way the log window behaves, they should be read
235 * from a file but for the moment, they are hardcoded here. Some options are
236 * configurable through the UI.
239 /** Indicates whether task bar shows activity animation */
240 BOOL g_bShowActivityAnimation = 1;
242 /** Indicates whether the log window is shown */
243 BOOL g_bShowLogWindow = 1;
245 /** Indicates if the log window appears on the task bar */
246 BOOL g_bShowOnTaskBar = 0;
248 /** Indicates whether closing the log window really just hides it */
249 BOOL g_bCloseHidesWindow = 1;
251 /** Indicates if messages are logged at all */
252 BOOL g_bLogMessages = 1;
254 /** Indicates whether log messages are highlighted */
255 BOOL g_bHighlightMessages = 1;
257 /** Indicates if buffer is limited in size */
258 BOOL g_bLimitBufferSize = 1;
260 /** Maximum number of lines allowed in buffer when limited */
261 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
264 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
266 /** Size of font to use */
267 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
270 /* FIXME: this is a kludge */
272 /** Second (=default, hopefully) Actions file name. */
273 const char * g_default_actions_file = NULL;
275 /** Third (=user, hopefully) Actions file name. */
276 const char * g_user_actions_file = NULL;
278 /** Filter file name. */
279 const char * g_re_filterfile = NULL;
283 /** Trust file name. */
284 const char * g_trustfile = NULL;
286 #endif /* def FEATURE_TRUST */
288 /* FIXME: end kludge */
291 /** Regular expression for detected URLs */
292 #define RE_URL "http:[^ \n\r]*"
296 * Regular expressions that are used to perform highlight in the log window.
298 static struct _Pattern
300 const char *str; /**< The pattern to match. */
301 int style; /**< How to highlight it - one of the STYLE_xxx constants. */
302 regex_t buffer; /**< The compiled pattern to use for matching. Generated from str. */
303 } patterns_to_highlight[] =
306 { RE_URL, STYLE_LINK },
307 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
308 /* interesting text to highlight */
309 { "crunch!", STYLE_HIGHLIGHT },
310 { "crunch:", STYLE_HIGHLIGHT },
312 { "referer:", STYLE_HEADER },
313 { "proxy-connection:", STYLE_HEADER },
314 { "proxy-agent:", STYLE_HEADER },
315 { "user-agent:", STYLE_HEADER },
316 { "host:", STYLE_HEADER },
317 { "accept:", STYLE_HEADER },
318 { "accept-encoding:", STYLE_HEADER },
319 { "accept-language:", STYLE_HEADER },
320 { "accept-charset:", STYLE_HEADER },
321 { "accept-ranges:", STYLE_HEADER },
322 { "date:", STYLE_HEADER },
323 { "cache-control:", STYLE_HEADER },
324 { "cache-last-checked:", STYLE_HEADER },
325 { "connection:", STYLE_HEADER },
326 { "content-type", STYLE_HEADER },
327 { "content-length", STYLE_HEADER },
328 { "cookie", STYLE_HEADER },
329 { "last-modified:", STYLE_HEADER },
330 { "pragma:", STYLE_HEADER },
331 { "server:", STYLE_HEADER },
332 { "etag:", STYLE_HEADER },
333 { "expires:", STYLE_HEADER },
334 { "warning:", STYLE_HEADER },
335 /* this is the terminator statement - do not delete! */
344 * Window handle for the log window.
353 /** Critical section used to serialize output to the log window. */
354 static CRITICAL_SECTION g_criticalsection;
356 /** The window handle of the tray icon window. */
357 static HWND g_hwndTray;
359 /** The window handle of the log window. */
360 static HWND g_hwndLogBox;
362 /** The default window procedure for the rich edit control. Set when we
363 subclass this window. */
364 static WNDPROC g_fnLogBox;
366 /** The icons for the activity animation. */
367 static HICON g_hiconAnim[ANIM_FRAMES];
369 /** The icon to use when idle. */
370 static HICON g_hiconIdle;
372 /** The main application icon. */
373 static HICON g_hiconApp;
375 /** The index of the current animation frame. */
376 static int g_nAnimFrame;
378 /** If a timer to call LogClipBuffer() has been started. */
379 static BOOL g_bClipPending = FALSE;
381 /** The version of the rich edit control we're using. */
382 static int g_nRichEditVersion = 0;
388 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
389 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
390 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
391 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
392 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
393 static BOOL InitRichEdit(void);
394 static void LogClipBuffer(void);
395 static void LogCreatePatternMatchingBuffers(void);
396 static void LogDestroyPatternMatchingBuffers(void);
397 static int LogPutStringNoMatch(const char *pszText, int style);
400 /*********************************************************************
402 * Function : InitLogWindow
404 * Description : Initialise the log window.
408 * Returns : Always TRUE (there should be error checking on the resources).
410 *********************************************************************/
411 BOOL InitLogWindow(void)
416 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
417 for (i = 0; i < ANIM_FRAMES; i++)
419 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
421 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
423 /* Create the user interface */
424 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
425 g_hwndTray = CreateTrayWindow(g_hInstance);
426 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
428 /* Create pattern matching buffers (for highlighting */
429 LogCreatePatternMatchingBuffers();
431 /* Create a critical section to protect multi-threaded access to certain things */
432 InitializeCriticalSection(&g_criticalsection);
439 /*********************************************************************
441 * Function : TermLogWindow
443 * Description : Cleanup the logwindow.
449 *********************************************************************/
450 void TermLogWindow(void)
454 LogDestroyPatternMatchingBuffers();
456 TrayDeleteIcon(g_hwndTray, 1);
457 DeleteObject(g_hiconApp);
458 DeleteObject(g_hiconIdle);
459 for (i = 0; i < ANIM_FRAMES; i++)
461 DeleteObject(g_hiconAnim[i]);
467 /*********************************************************************
469 * Function : LogCreatePatternMatchingBuffers
471 * Description : Compile the pattern matching buffers.
477 *********************************************************************/
478 void LogCreatePatternMatchingBuffers(void)
481 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
483 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
488 /*********************************************************************
490 * Function : LogDestroyPatternMatchingBuffers
492 * Description : Free up the pattern matching buffers.
498 *********************************************************************/
499 void LogDestroyPatternMatchingBuffers(void)
502 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
504 regfree(&patterns_to_highlight[i].buffer);
509 /*********************************************************************
511 * Function : LogGetURLUnderCursor
513 * Description : Returns the URL from under the cursor (remember to free it!).
517 * Returns : NULL or a pointer to an URL string.
519 *********************************************************************/
520 char *LogGetURLUnderCursor(void)
522 char *szResult = NULL;
527 DWORD nWordStart = 0;
530 regcomp(&re, RE_URL, REG_ICASE);
532 /* Get the position of the cursor over the text window */
533 GetCursorPos(&ptCursor);
534 ScreenToClient(g_hwndLogBox, &ptCursor);
538 /* Search backwards and fowards to obtain the word that is highlighted */
539 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
540 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
541 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
543 /* Compare the string to the pattern */
544 if (nWordEnd > nWordStart)
549 range.chrg.cpMin = nWordStart;
550 range.chrg.cpMax = nWordEnd;
551 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
552 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
554 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
556 szResult = range.lpstrText;
560 free(range.lpstrText);
570 /*********************************************************************
572 * Function : LogPutString
574 * Description : Inserts text into the logging window. This is really
575 * a regexp aware wrapper function to `LogPutStringNoMatch'.
578 * 1 : pszText = pointer to string going to the log window
580 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
581 * FIXME: this is backwards to the rest of IJB and to common
582 * programming practice. Please use 0 => success instead.
584 *********************************************************************/
585 int LogPutString(const char *pszText)
590 if (pszText == NULL || strlen(pszText) == 0)
600 /* Critical section stops multiple threads doing nasty interactions that
601 * foul up the highlighting and output.
603 EnterCriticalSection(&g_criticalsection);
605 if (g_bHighlightMessages)
609 /* First things first, regexp scan for various things that we would like highlighted */
610 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
612 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
614 char *pszBefore = NULL;
615 char *pszMatch = NULL;
616 char *pszAfter = NULL;
619 /* Split the string up into pieces representing the strings, before
620 at and after the matching pattern
624 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
625 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
626 strncpy(pszBefore, pszText, match.rm_so);
628 if (match.rm_eo < (regoff_t)strlen(pszText))
630 pszAfter = strdup(&pszText[match.rm_eo]);
632 nMatchSize = match.rm_eo - match.rm_so;
633 pszMatch = (char *)malloc(nMatchSize + 1);
634 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
635 pszMatch[nMatchSize] = '\0';
637 /* Recursively call LogPutString */
640 LogPutString(pszBefore);
645 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
650 LogPutString(pszAfter);
660 result = LogPutStringNoMatch(pszText, STYLE_NONE);
663 LeaveCriticalSection(&g_criticalsection);
670 /*********************************************************************
672 * Function : LogPutStringNoMatch
674 * Description : Puts a string into the logging window.
677 * 1 : pszText = pointer to string going to the log window
678 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
680 * Returns : Always 1 => success.
681 * FIXME: this is backwards to the rest of IJB and to common
682 * programming practice. Please use 0 => success instead.
684 *********************************************************************/
685 int LogPutStringNoMatch(const char *pszText, int style)
691 assert(g_hwndLogBox);
692 if (g_hwndLogBox == NULL)
697 /* TODO preserve existing selection */
699 /* Go to the end of the text */
700 nTextLength = GetWindowTextLength(g_hwndLogBox);
701 range.cpMin = nTextLength;
702 range.cpMax = nTextLength;
703 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
705 /* Apply a formatting style */
706 memset(&format, 0, sizeof(format));
707 format.cbSize = sizeof(format);
708 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
709 format.yHeight = (g_nFontSize * 1440) / 72;
710 strcpy(format.szFaceName, g_szFontFaceName);
711 if (style == STYLE_NONE)
714 format.dwEffects |= CFE_AUTOCOLOR;
716 else if (style == STYLE_HEADER)
718 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
720 else if (style == STYLE_HIGHLIGHT)
722 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
724 else if (style == STYLE_LINK)
726 format.dwEffects |= CFE_UNDERLINE;
727 format.crTextColor = RGB(0, 0, 255);
729 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
731 /* Append text to the end */
732 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
734 /* TODO Restore the old selection */
737 if (strchr(pszText, '\n') != NULL)
739 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
742 /* Set the force clip timer going. This timer ensures clipping is done
743 intermittently even when there is a sustained burst of logging
745 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
747 g_bClipPending = TRUE;
755 /*********************************************************************
757 * Function : LogShowActivity
759 * Description : Start the spinner.
765 *********************************************************************/
766 void LogShowActivity(void)
768 /* Start some activity timers */
769 if (g_bShowActivityAnimation)
771 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
772 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
778 /*********************************************************************
780 * Function : LogClipBuffer
782 * Description : Prunes old lines from the log.
783 * This is called occasionally, from a timer callback.
789 *********************************************************************/
790 void LogClipBuffer(void)
792 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
793 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
795 /* Compute the range representing the lines to be deleted */
796 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
797 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
800 range.cpMax = nLastChar;
802 /* TODO get current selection */
804 /* TODO adjust and clip old selection against range to be deleted */
806 /* Select range and erase it (turning off autoscroll to prevent
808 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
809 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
810 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
811 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
813 /* Restore old selection */
819 /*********************************************************************
821 * Function : CreateHiddenLogOwnerWindow
823 * Description : Creates a hidden owner window that stops the log
824 * window appearing in the task bar.
827 * 1 : hInstance = application's instance handle
829 * Returns : Handle to newly created window.
831 *********************************************************************/
832 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
834 static const char *szWndName = "PrivoxyLogOwner";
839 wc.lpfnWndProc = LogOwnerWindowProc;
842 wc.hInstance = hInstance;
845 wc.hbrBackground = 0;
847 wc.lpszClassName = szWndName;
851 hwnd = CreateWindow(szWndName, szWndName,
852 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
853 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
860 /*********************************************************************
862 * Function : LogOwnerWindowProc
864 * Description : Dummy procedure that does nothing special.
867 * 1 : hwnd = window handle
868 * 2 : uMsg = message number
869 * 3 : wParam = first param for this message
870 * 4 : lParam = next param for this message
872 * Returns : Same as `DefWindowProc'.
874 *********************************************************************/
875 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
877 return DefWindowProc(hwnd, uMsg, wParam, lParam);
882 /*********************************************************************
884 * Function : CreateLogWindow
886 * Description : Create the logging window.
889 * 1 : hInstance = application's instance handle
890 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
892 * Returns : Handle to newly created window.
894 *********************************************************************/
895 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
897 static const char *szWndName = "PrivoxyLogWindow";
898 static const char *szWndTitle = "Privoxy";
901 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
905 memset(&wc, 0, sizeof(wc));
906 wc.cbSize = sizeof(wc);
907 wc.style = CS_DBLCLKS;
908 wc.lpfnWndProc = LogWindowProc;
911 wc.hInstance = hInstance;
912 wc.hIcon = g_hiconApp;
914 wc.hbrBackground = 0;
915 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
916 wc.lpszClassName = szWndName;
917 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
918 RegisterClassEx(&wc);
920 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
921 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
922 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
924 /* Now create a child list box */
925 GetClientRect(hwnd, &rcClient);
927 /* Create a rich edit control */
929 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
930 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
931 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
932 hwnd, NULL, hInstance, NULL);
933 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
935 /* Subclass the control to catch certain messages */
936 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
937 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
939 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
940 if (!g_bShowOnTaskBar &&
941 (nCmdShow == SW_SHOWMINIMIZED ||
942 nCmdShow == SW_MINIMIZE ||
943 nCmdShow == SW_SHOWMINNOACTIVE))
948 ShowWindow(hwnd, nCmdShow);
951 GetClientRect(g_hwndLogFrame, &rcClient);
952 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
959 /*********************************************************************
961 * Function : InitRichEdit
963 * Description : Initialise the rich edit control library.
967 * Returns : TRUE => success, FALSE => failure.
968 * FIXME: this is backwards to the rest of IJB and to common
969 * programming practice. Please use 0 => success instead.
971 *********************************************************************/
972 BOOL InitRichEdit(void)
974 static HINSTANCE hInstRichEdit;
975 if (hInstRichEdit == NULL)
977 g_nRichEditVersion = 0;
978 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
981 g_nRichEditVersion = _RICHEDIT_VER;
985 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
988 g_nRichEditVersion = 0x0100;
992 return (hInstRichEdit != NULL) ? TRUE : FALSE;
997 /*********************************************************************
999 * Function : ShowLogWindow
1001 * Description : Shows or hides the log window. We will also raise the
1002 * window on a show command in case it is buried.
1005 * 1 : bShow = TRUE to show, FALSE to mimize/hide
1009 *********************************************************************/
1010 void ShowLogWindow(BOOL bShow)
1014 SetForegroundWindow(g_hwndLogFrame);
1015 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1018 else if (g_bShowOnTaskBar)
1020 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1024 ShowWindow(g_hwndLogFrame, SW_HIDE);
1029 /*********************************************************************
1031 * Function : EditFile
1033 * Description : Opens the specified setting file for editing.
1034 * FIXME: What if the file has no associated application. Check for return values
1035 * from ShellExecute??
1038 * 1 : filename = filename from the config (aka config.txt) file.
1042 *********************************************************************/
1043 void EditFile(const char *filename)
1047 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1053 /*--------------------------------------------------------------------------*/
1054 /* Windows message handlers */
1055 /*--------------------------------------------------------------------------*/
1058 /*********************************************************************
1060 * Function : OnLogRButtonUp
1062 * Description : Handler for WM_RBUTTONUP messages.
1065 * 1 : nModifier = wParam from mouse message (unused)
1066 * 2 : x = x coordinate of the mouse event
1067 * 3 : y = y coordinate of the mouse event
1071 *********************************************************************/
1072 void OnLogRButtonUp(int nModifier, int x, int y)
1074 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1077 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1079 /* Check if there is a selection */
1081 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1082 if (range.cpMin == range.cpMax)
1084 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1088 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1091 /* Display the popup */
1092 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1099 /*********************************************************************
1101 * Function : OnLogCommand
1103 * Description : Handler for WM_COMMAND messages.
1106 * 1 : nCommand = the command portion of the menu selection event
1110 *********************************************************************/
1111 void OnLogCommand(int nCommand)
1115 case ID_TOGGLE_SHOWWINDOW:
1116 g_bShowLogWindow = !g_bShowLogWindow;
1118 ShowLogWindow(g_bShowLogWindow);
1122 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1126 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1129 case ID_VIEW_CLEARLOG:
1130 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1133 case ID_VIEW_LOGMESSAGES:
1134 g_bLogMessages = !g_bLogMessages;
1135 /* SaveLogSettings(); */
1138 case ID_VIEW_MESSAGEHIGHLIGHTING:
1139 g_bHighlightMessages = !g_bHighlightMessages;
1140 /* SaveLogSettings(); */
1143 case ID_VIEW_LIMITBUFFERSIZE:
1144 g_bLimitBufferSize = !g_bLimitBufferSize;
1145 /* SaveLogSettings(); */
1148 case ID_VIEW_ACTIVITYANIMATION:
1149 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1150 /* SaveLogSettings(); */
1153 #ifdef FEATURE_TOGGLE
1154 /* by haroon - change toggle to its opposite value */
1155 case ID_TOGGLE_ENABLED:
1156 g_bToggleIJB = !g_bToggleIJB;
1159 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1163 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1166 #endif /* def FEATURE_TOGGLE */
1168 case ID_TOOLS_EDITCONFIG:
1169 EditFile(configfile);
1172 case ID_TOOLS_EDITDEFAULTACTIONS:
1173 EditFile(g_default_actions_file);
1176 case ID_TOOLS_EDITUSERACTIONS:
1177 EditFile(g_user_actions_file);
1180 case ID_TOOLS_EDITFILTERS:
1181 EditFile(g_re_filterfile);
1184 #ifdef FEATURE_TRUST
1185 case ID_TOOLS_EDITTRUST:
1186 EditFile(g_trustfile);
1188 #endif /* def FEATURE_TRUST */
1191 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1195 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1198 case ID_HELP_MANUAL:
1199 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1202 case ID_HELP_STATUS:
1203 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1207 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1218 /*********************************************************************
1220 * Function : OnLogInitMenu
1222 * Description : Handler for WM_INITMENU messages. Enable, disable,
1223 * check, and/or uncheck menu options as apropos.
1226 * 1 : hmenu = handle to menu to "make current"
1230 *********************************************************************/
1231 void OnLogInitMenu(HMENU hmenu)
1233 /* Only enable editors if there is a file to edit */
1234 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1235 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1236 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1237 #ifdef FEATURE_TRUST
1238 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1239 #endif /* def FEATURE_TRUST */
1241 /* Check/uncheck options */
1242 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1243 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1244 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1245 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1246 #ifdef FEATURE_TOGGLE
1247 /* by haroon - menu item for Enable toggle on/off */
1248 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1249 #endif /* def FEATURE_TOGGLE */
1250 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1255 /*********************************************************************
1257 * Function : OnLogTimer
1259 * Description : Handler for WM_TIMER messages.
1262 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1266 *********************************************************************/
1267 void OnLogTimer(int nTimer)
1272 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1275 case TIMER_ANIMSTOP_ID:
1277 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1278 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1279 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1282 case TIMER_CLIPBUFFER_ID:
1283 case TIMER_CLIPBUFFER_FORCE_ID:
1285 g_bClipPending = FALSE;
1286 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1287 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1298 /*********************************************************************
1300 * Function : LogRichEditProc
1302 * Description : Window subclass routine handles some events for the rich edit control.
1305 * 1 : hwnd = window handle of the rich edit control
1306 * 2 : uMsg = message number
1307 * 3 : wParam = first param for this message
1308 * 4 : lParam = next param for this message
1310 * Returns : Appropriate M$ window message handler codes.
1312 *********************************************************************/
1313 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1320 pt.x = LOWORD(lParam);
1321 pt.y = HIWORD(lParam);
1322 ClientToScreen(hwnd, &pt);
1323 OnLogRButtonUp(wParam, pt.x, pt.y);
1327 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1332 /*********************************************************************
1334 * Function : LogWindowProc
1336 * Description : Windows call back routine handles events on the log window.
1339 * 1 : hwnd = handle of the logging window
1340 * 2 : uMsg = message number
1341 * 3 : wParam = first param for this message
1342 * 4 : lParam = next param for this message
1344 * Returns : Appropriate M$ window message handler codes.
1346 *********************************************************************/
1347 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1355 /* This is the end - beautiful friend - the end */
1356 DestroyWindow(g_hwndLogBox);
1357 DestroyWindow(g_hwndLogFrame);
1365 g_bShowLogWindow = wParam;
1367 /* Resize the logging window to fit the new frame */
1371 GetClientRect(g_hwndLogFrame, &rc);
1372 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1377 OnLogInitMenu((HMENU) wParam);
1385 OnLogCommand(LOWORD(wParam));
1392 if (g_bCloseHidesWindow)
1394 ShowLogWindow(FALSE);
1399 ShowLogWindow(FALSE);
1405 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1409 #endif /* ndef _WIN_CONSOLE - entire file */