1 const char w32log_rcs[] = "$Id: w32log.c,v 2.0 2002/06/04 14:34:21 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.0 2002/06/04 14:34:21 jongfoster
36 * Moving source files to src/
38 * Revision 1.25 2002/04/04 00:36:36 gliptak
39 * always use pcre for matching
41 * Revision 1.24 2002/03/31 17:19:00 jongfoster
42 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
44 * Revision 1.23 2002/03/26 22:57:10 jongfoster
45 * Web server name should begin www.
47 * Revision 1.22 2002/03/24 12:48:23 jongfoster
50 * Revision 1.21 2002/03/24 12:07:35 jongfoster
51 * Consistern name for filters file
53 * Revision 1.20 2002/03/24 12:03:47 jongfoster
56 * Revision 1.19 2002/01/17 21:04:17 jongfoster
57 * Replacing hard references to the URL of the config interface
58 * with #defines from project.h
60 * Revision 1.18 2001/11/30 23:37:24 jongfoster
61 * Renaming the Win32 config file to config.txt - this is almost the
62 * same as the corresponding UNIX name "config"
64 * Revision 1.17 2001/11/16 00:46:31 jongfoster
65 * Fixing compiler warnings
67 * Revision 1.16 2001/08/01 19:58:12 jongfoster
68 * Fixing documentation filenames in help menu, and making status
69 * option work without needing the "Junkbuster Status.URL" file.
71 * Revision 1.15 2001/07/30 22:08:36 jongfoster
72 * Tidying up #defines:
73 * - All feature #defines are now of the form FEATURE_xxx
74 * - Permanently turned off WIN_GUI_EDIT
75 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
77 * Revision 1.14 2001/07/29 18:47:05 jongfoster
78 * Adding missing #include "loadcfg.h"
80 * Revision 1.13 2001/07/19 19:15:14 haroon
81 * - Added a FIXME for EditFile but didn't fix :-)
83 * Revision 1.12 2001/07/13 14:04:59 oes
84 * Removed all #ifdef PCRS
86 * Revision 1.11 2001/06/07 23:08:12 jongfoster
87 * Forward and ACL edit options removed.
89 * Revision 1.10 2001/05/31 21:37:11 jongfoster
90 * GUI changes to rename "permissions file" to "actions file".
92 * Revision 1.9 2001/05/31 17:33:13 oes
96 * Revision 1.8 2001/05/29 09:50:24 jongfoster
97 * Unified blocklist/imagelist/permissionslist.
98 * File format is still under discussion, but the internal changes
101 * Also modified interceptor behaviour:
102 * - We now intercept all URLs beginning with one of the following
103 * prefixes (and *only* these prefixes):
105 * * http://ijbswa.sf.net/config/
106 * * http://ijbswa.sourceforge.net/config/
107 * - New interceptors "home page" - go to http://i.j.b/ to see it.
108 * - Internal changes so that intercepted and fast redirect pages
109 * are not replaced with an image.
110 * - Interceptors now have the option to send a binary page direct
111 * to the client. (i.e. ijb-send-banner uses this)
112 * - Implemented show-url-info interceptor. (Which is why I needed
113 * the above interceptors changes - a typical URL is
114 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
115 * The previous mechanism would not have intercepted that, and
116 * if it had been intercepted then it then it would have replaced
119 * Revision 1.7 2001/05/26 01:26:34 jongfoster
120 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
121 * This #define cannot be set from ./configure - there's no point, it
122 * doesn't work yet. See feature request # 425722
124 * Revision 1.6 2001/05/26 00:31:30 jongfoster
125 * Fixing compiler warning about comparing signed/unsigned.
127 * Revision 1.5 2001/05/26 00:28:36 jongfoster
128 * Automatic reloading of config file.
129 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
130 * Most of the global variables have been moved to a new
131 * struct configuration_spec, accessed through csp->config->globalname
132 * Most of the globals remaining are used by the Win32 GUI.
134 * Revision 1.4 2001/05/22 18:56:28 oes
137 * Revision 1.3 2001/05/20 15:07:54 jongfoster
138 * File is now ignored if _WIN_CONSOLE is defined.
140 * Revision 1.2 2001/05/20 01:21:20 jongfoster
141 * Version 2.9.4 checkin.
142 * - Merged popupfile and cookiefile, and added control over PCRS
143 * filtering, in new "permissionsfile".
144 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
145 * file error you now get a message box (in the Win32 GUI) rather
146 * than the program exiting with no explanation.
147 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
149 * - Removed tabs from "config"
150 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
151 * - Bumped up version number.
153 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
154 * Initial import of version 2.9.3 source tree
157 *********************************************************************/
169 #include <richedit.h>
173 #include "w32taskbar.h"
177 #include "miscutil.h"
181 const char w32res_h_rcs[] = W32RES_H_VERSION;
185 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
188 const char w32log_h_rcs[] = W32LOG_H_VERSION;
190 #ifndef _WIN_CONSOLE /* entire file */
193 * Timers and the various durations
195 #define TIMER_ANIM_ID 1
196 #define TIMER_ANIM_TIME 100
197 #define TIMER_ANIMSTOP_ID 2
198 #define TIMER_ANIMSTOP_TIME 1000
199 #define TIMER_CLIPBUFFER_ID 3
200 #define TIMER_CLIPBUFFER_TIME 1000
201 #define TIMER_CLIPBUFFER_FORCE_ID 4
202 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
205 * Styles of text that can be output
208 #define STYLE_HIGHLIGHT 1
210 #define STYLE_HEADER 3
213 * Number of frames of animation in tray activity sequence
215 #define ANIM_FRAMES 8
217 #define DEFAULT_MAX_BUFFER_LINES 200
218 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
219 #define DEFAULT_LOG_FONT_SIZE 8
222 * These values affect the way the log window behaves, they should be read
223 * from a file but for the moment, they are hardcoded here. Some options are
224 * configurable through the UI.
227 /** Indicates whether task bar shows activity animation */
228 BOOL g_bShowActivityAnimation = 1;
230 /** Indicates if the log window appears on the task bar */
231 BOOL g_bShowOnTaskBar = 0;
233 /** Indicates whether closing the log window really just hides it */
234 BOOL g_bCloseHidesWindow = 1;
236 /** Indicates if messages are logged at all */
237 BOOL g_bLogMessages = 1;
239 /** Indicates whether log messages are highlighted */
240 BOOL g_bHighlightMessages = 1;
242 /** Indicates if buffer is limited in size */
243 BOOL g_bLimitBufferSize = 1;
245 /** Maximum number of lines allowed in buffer when limited */
246 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
249 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
251 /** Size of font to use */
252 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
255 /* FIXME: this is a kludge */
257 /** Actions file name. */
258 const char * g_actions_file = NULL;
260 /** Filter file name. */
261 const char * g_re_filterfile = NULL;
265 /** Trust file name. */
266 const char * g_trustfile = NULL;
268 #endif /* def FEATURE_TRUST */
270 /* FIXME: end kludge */
273 /** Regular expression for detected URLs */
274 #define RE_URL "http:[^ \n\r]*"
278 * Regular expressions that are used to perform highlight in the log window.
280 static struct _Pattern
282 const char *str; /**< The pattern to match. */
283 int style; /**< How to highlight it - one of the STYLE_xxx constants. */
284 regex_t buffer; /**< The compiled pattern to use for matching. Generated from str. */
285 } patterns_to_highlight[] =
288 { RE_URL, STYLE_LINK },
289 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
290 /* interesting text to highlight */
291 { "crunch!", STYLE_HIGHLIGHT },
293 { "referer:", STYLE_HEADER },
294 { "proxy-connection:", STYLE_HEADER },
295 { "proxy-agent:", STYLE_HEADER },
296 { "user-agent:", STYLE_HEADER },
297 { "host:", STYLE_HEADER },
298 { "accept:", STYLE_HEADER },
299 { "accept-encoding:", STYLE_HEADER },
300 { "accept-language:", STYLE_HEADER },
301 { "accept-charset:", STYLE_HEADER },
302 { "accept-ranges:", STYLE_HEADER },
303 { "date:", STYLE_HEADER },
304 { "cache-control:", STYLE_HEADER },
305 { "cache-last-checked:", STYLE_HEADER },
306 { "connection:", STYLE_HEADER },
307 { "content-type", STYLE_HEADER },
308 { "content-length", STYLE_HEADER },
309 { "cookie", STYLE_HEADER },
310 { "last-modified:", STYLE_HEADER },
311 { "pragma:", STYLE_HEADER },
312 { "server:", STYLE_HEADER },
313 { "etag:", STYLE_HEADER },
314 { "expires:", STYLE_HEADER },
315 { "warning:", STYLE_HEADER },
316 /* this is the terminator statement - do not delete! */
325 * Window handle for the log window.
334 /** Critical section used to serialize output to the log window. */
335 static CRITICAL_SECTION g_criticalsection;
337 /** The window handle of the tray icon window. */
338 static HWND g_hwndTray;
340 /** The window handle of the log window. */
341 static HWND g_hwndLogBox;
343 /** The default window procedure for the rich edit control. Set when we
344 subclass this window. */
345 static WNDPROC g_fnLogBox;
347 /** The icons for the activity animation. */
348 static HICON g_hiconAnim[ANIM_FRAMES];
350 /** The icon to use when idle. */
351 static HICON g_hiconIdle;
353 /** The main application icon. */
354 static HICON g_hiconApp;
356 /** The index of the current animation frame. */
357 static int g_nAnimFrame;
359 /** If a timer to call LogClipBuffer() has been started. */
360 static BOOL g_bClipPending = FALSE;
362 /** The version of the rich edit control we're using. */
363 static int g_nRichEditVersion = 0;
369 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
370 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
371 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
372 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
373 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
374 static BOOL InitRichEdit(void);
375 static void LogClipBuffer(void);
376 static void LogCreatePatternMatchingBuffers(void);
377 static void LogDestroyPatternMatchingBuffers(void);
378 static int LogPutStringNoMatch(const char *pszText, int style);
381 /*********************************************************************
383 * Function : InitLogWindow
385 * Description : Initialise the log window.
389 * Returns : Always TRUE (there should be error checking on the resources).
391 *********************************************************************/
392 BOOL InitLogWindow(void)
397 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
398 for (i = 0; i < ANIM_FRAMES; i++)
400 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
402 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
404 /* Create the user interface */
405 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
406 g_hwndTray = CreateTrayWindow(g_hInstance);
407 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
409 /* Create pattern matching buffers (for highlighting */
410 LogCreatePatternMatchingBuffers();
412 /* Create a critical section to protect multi-threaded access to certain things */
413 InitializeCriticalSection(&g_criticalsection);
420 /*********************************************************************
422 * Function : TermLogWindow
424 * Description : Cleanup the logwindow.
430 *********************************************************************/
431 void TermLogWindow(void)
435 LogDestroyPatternMatchingBuffers();
437 TrayDeleteIcon(g_hwndTray, 1);
438 DeleteObject(g_hiconApp);
439 DeleteObject(g_hiconIdle);
440 for (i = 0; i < ANIM_FRAMES; i++)
442 DeleteObject(g_hiconAnim[i]);
448 /*********************************************************************
450 * Function : LogCreatePatternMatchingBuffers
452 * Description : Compile the pattern matching buffers.
458 *********************************************************************/
459 void LogCreatePatternMatchingBuffers(void)
462 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
464 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
469 /*********************************************************************
471 * Function : LogDestroyPatternMatchingBuffers
473 * Description : Free up the pattern matching buffers.
479 *********************************************************************/
480 void LogDestroyPatternMatchingBuffers(void)
483 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
485 regfree(&patterns_to_highlight[i].buffer);
490 /*********************************************************************
492 * Function : LogGetURLUnderCursor
494 * Description : Returns the URL from under the cursor (remember to free it!).
498 * Returns : NULL or a pointer to an URL string.
500 *********************************************************************/
501 char *LogGetURLUnderCursor(void)
503 char *szResult = NULL;
508 DWORD nWordStart = 0;
511 regcomp(&re, RE_URL, REG_ICASE);
513 /* Get the position of the cursor over the text window */
514 GetCursorPos(&ptCursor);
515 ScreenToClient(g_hwndLogBox, &ptCursor);
519 /* Search backwards and fowards to obtain the word that is highlighted */
520 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
521 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
522 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
524 /* Compare the string to the pattern */
525 if (nWordEnd > nWordStart)
530 range.chrg.cpMin = nWordStart;
531 range.chrg.cpMax = nWordEnd;
532 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
533 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
535 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
537 szResult = range.lpstrText;
541 free(range.lpstrText);
551 /*********************************************************************
553 * Function : LogPutString
555 * Description : Inserts text into the logging window. This is really
556 * a regexp aware wrapper function to `LogPutStringNoMatch'.
559 * 1 : pszText = pointer to string going to the log window
561 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
562 * FIXME: this is backwards to the rest of IJB and to common
563 * programming practice. Please use 0 => success instead.
565 *********************************************************************/
566 int LogPutString(const char *pszText)
571 if (pszText == NULL || strlen(pszText) == 0)
581 /* Critical section stops multiple threads doing nasty interactions that
582 * foul up the highlighting and output.
584 EnterCriticalSection(&g_criticalsection);
586 if (g_bHighlightMessages)
590 /* First things first, regexp scan for various things that we would like highlighted */
591 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
593 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
595 char *pszBefore = NULL;
596 char *pszMatch = NULL;
597 char *pszAfter = NULL;
600 /* Split the string up into pieces representing the strings, before
601 at and after the matching pattern
605 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
606 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
607 strncpy(pszBefore, pszText, match.rm_so);
609 if (match.rm_eo < (regoff_t)strlen(pszText))
611 pszAfter = strdup(&pszText[match.rm_eo]);
613 nMatchSize = match.rm_eo - match.rm_so;
614 pszMatch = (char *)malloc(nMatchSize + 1);
615 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
616 pszMatch[nMatchSize] = '\0';
618 /* Recursively call LogPutString */
621 LogPutString(pszBefore);
626 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
631 LogPutString(pszAfter);
641 result = LogPutStringNoMatch(pszText, STYLE_NONE);
644 LeaveCriticalSection(&g_criticalsection);
651 /*********************************************************************
653 * Function : LogPutStringNoMatch
655 * Description : Puts a string into the logging window.
658 * 1 : pszText = pointer to string going to the log window
659 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
661 * Returns : Always 1 => success.
662 * FIXME: this is backwards to the rest of IJB and to common
663 * programming practice. Please use 0 => success instead.
665 *********************************************************************/
666 int LogPutStringNoMatch(const char *pszText, int style)
672 assert(g_hwndLogBox);
673 if (g_hwndLogBox == NULL)
678 /* TODO preserve existing selection */
680 /* Go to the end of the text */
681 nTextLength = GetWindowTextLength(g_hwndLogBox);
682 range.cpMin = nTextLength;
683 range.cpMax = nTextLength;
684 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
686 /* Apply a formatting style */
687 memset(&format, 0, sizeof(format));
688 format.cbSize = sizeof(format);
689 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
690 format.yHeight = (g_nFontSize * 1440) / 72;
691 strcpy(format.szFaceName, g_szFontFaceName);
692 if (style == STYLE_NONE)
695 format.dwEffects |= CFE_AUTOCOLOR;
697 else if (style == STYLE_HEADER)
699 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
701 else if (style == STYLE_HIGHLIGHT)
703 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
705 else if (style == STYLE_LINK)
707 format.dwEffects |= CFE_UNDERLINE;
708 format.crTextColor = RGB(0, 0, 255);
710 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
712 /* Append text to the end */
713 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
715 /* TODO Restore the old selection */
718 if (strchr(pszText, '\n') != NULL)
720 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
723 /* Set the force clip timer going. This timer ensures clipping is done
724 intermittently even when there is a sustained burst of logging
726 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
728 g_bClipPending = TRUE;
736 /*********************************************************************
738 * Function : LogShowActivity
740 * Description : Start the spinner.
746 *********************************************************************/
747 void LogShowActivity(void)
749 /* Start some activity timers */
750 if (g_bShowActivityAnimation)
752 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
753 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
759 /*********************************************************************
761 * Function : LogClipBuffer
763 * Description : Prunes old lines from the log.
764 * This is called occasionally, from a timer callback.
770 *********************************************************************/
771 void LogClipBuffer(void)
773 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
774 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
776 /* Compute the range representing the lines to be deleted */
777 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
778 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
781 range.cpMax = nLastChar;
783 /* TODO get current selection */
785 /* TODO adjust and clip old selection against range to be deleted */
787 /* Select range and erase it (turning off autoscroll to prevent
789 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
790 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
791 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
792 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
794 /* Restore old selection */
800 /*********************************************************************
802 * Function : CreateHiddenLogOwnerWindow
804 * Description : Creates a hidden owner window that stops the log
805 * window appearing in the task bar.
808 * 1 : hInstance = application's instance handle
810 * Returns : Handle to newly created window.
812 *********************************************************************/
813 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
815 static const char *szWndName = "PrivoxyLogOwner";
820 wc.lpfnWndProc = LogOwnerWindowProc;
823 wc.hInstance = hInstance;
826 wc.hbrBackground = 0;
828 wc.lpszClassName = szWndName;
832 hwnd = CreateWindow(szWndName, szWndName,
833 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
834 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
841 /*********************************************************************
843 * Function : LogOwnerWindowProc
845 * Description : Dummy procedure that does nothing special.
848 * 1 : hwnd = window handle
849 * 2 : uMsg = message number
850 * 3 : wParam = first param for this message
851 * 4 : lParam = next param for this message
853 * Returns : Same as `DefWindowProc'.
855 *********************************************************************/
856 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
858 return DefWindowProc(hwnd, uMsg, wParam, lParam);
863 /*********************************************************************
865 * Function : CreateLogWindow
867 * Description : Create the logging window.
870 * 1 : hInstance = application's instance handle
871 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
873 * Returns : Handle to newly created window.
875 *********************************************************************/
876 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
878 static const char *szWndName = "PrivoxyLogWindow";
879 static const char *szWndTitle = "Privoxy";
882 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
886 memset(&wc, 0, sizeof(wc));
887 wc.cbSize = sizeof(wc);
888 wc.style = CS_DBLCLKS;
889 wc.lpfnWndProc = LogWindowProc;
892 wc.hInstance = hInstance;
893 wc.hIcon = g_hiconApp;
895 wc.hbrBackground = 0;
896 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
897 wc.lpszClassName = szWndName;
898 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
899 RegisterClassEx(&wc);
901 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
902 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
903 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
905 /* Now create a child list box */
906 GetClientRect(hwnd, &rcClient);
908 /* Create a rich edit control */
910 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
911 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
912 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
913 hwnd, NULL, hInstance, NULL);
914 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
916 /* Subclass the control to catch certain messages */
917 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
918 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
920 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
921 if (!g_bShowOnTaskBar &&
922 (nCmdShow == SW_SHOWMINIMIZED ||
923 nCmdShow == SW_MINIMIZE ||
924 nCmdShow == SW_SHOWMINNOACTIVE))
929 ShowWindow(hwnd, nCmdShow);
932 GetClientRect(g_hwndLogFrame, &rcClient);
933 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
940 /*********************************************************************
942 * Function : InitRichEdit
944 * Description : Initialise the rich edit control library.
948 * Returns : TRUE => success, FALSE => failure.
949 * FIXME: this is backwards to the rest of IJB and to common
950 * programming practice. Please use 0 => success instead.
952 *********************************************************************/
953 BOOL InitRichEdit(void)
955 static HINSTANCE hInstRichEdit;
956 if (hInstRichEdit == NULL)
958 g_nRichEditVersion = 0;
959 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
962 g_nRichEditVersion = _RICHEDIT_VER;
966 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
969 g_nRichEditVersion = 0x0100;
973 return (hInstRichEdit != NULL) ? TRUE : FALSE;
978 /*********************************************************************
980 * Function : ShowLogWindow
982 * Description : Shows or hides the log window. We will also raise the
983 * window on a show command in case it is buried.
986 * 1 : bShow = TRUE to show, FALSE to mimize/hide
990 *********************************************************************/
991 void ShowLogWindow(BOOL bShow)
995 SetForegroundWindow(g_hwndLogFrame);
996 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
998 else if (g_bShowOnTaskBar)
1000 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1004 ShowWindow(g_hwndLogFrame, SW_HIDE);
1010 /*********************************************************************
1012 * Function : EditFile
1014 * Description : Opens the specified setting file for editing.
1015 * FIXME: What if the file has no associated application. Check for return values
1016 * from ShellExecute??
1019 * 1 : filename = filename from the config (aka config.txt) file.
1023 *********************************************************************/
1024 void EditFile(const char *filename)
1028 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1034 /*--------------------------------------------------------------------------*/
1035 /* Windows message handlers */
1036 /*--------------------------------------------------------------------------*/
1039 /*********************************************************************
1041 * Function : OnLogRButtonUp
1043 * Description : Handler for WM_RBUTTONUP messages.
1046 * 1 : nModifier = wParam from mouse message (unused)
1047 * 2 : x = x coordinate of the mouse event
1048 * 3 : y = y coordinate of the mouse event
1052 *********************************************************************/
1053 void OnLogRButtonUp(int nModifier, int x, int y)
1055 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1058 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1060 /* Check if there is a selection */
1062 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1063 if (range.cpMin == range.cpMax)
1065 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1069 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1072 /* Display the popup */
1073 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1080 /*********************************************************************
1082 * Function : OnLogCommand
1084 * Description : Handler for WM_COMMAND messages.
1087 * 1 : nCommand = the command portion of the menu selection event
1091 *********************************************************************/
1092 void OnLogCommand(int nCommand)
1097 ShowLogWindow(TRUE);
1101 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1105 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1108 case ID_VIEW_CLEARLOG:
1109 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1112 case ID_VIEW_LOGMESSAGES:
1113 g_bLogMessages = !g_bLogMessages;
1114 /* SaveLogSettings(); */
1117 case ID_VIEW_MESSAGEHIGHLIGHTING:
1118 g_bHighlightMessages = !g_bHighlightMessages;
1119 /* SaveLogSettings(); */
1122 case ID_VIEW_LIMITBUFFERSIZE:
1123 g_bLimitBufferSize = !g_bLimitBufferSize;
1124 /* SaveLogSettings(); */
1127 case ID_VIEW_ACTIVITYANIMATION:
1128 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1129 /* SaveLogSettings(); */
1132 #ifdef FEATURE_TOGGLE
1133 /* by haroon - change toggle to its opposite value */
1134 case ID_TOGGLE_ENABLED:
1135 g_bToggleIJB = !g_bToggleIJB;
1138 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1142 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1145 #endif /* def FEATURE_TOGGLE */
1147 case ID_TOOLS_EDITCONFIG:
1148 EditFile(configfile);
1151 case ID_TOOLS_EDITACTIONS:
1152 EditFile(g_actions_file);
1155 case ID_TOOLS_EDITFILTERS:
1156 EditFile(g_re_filterfile);
1159 #ifdef FEATURE_TRUST
1160 case ID_TOOLS_EDITTRUST:
1161 EditFile(g_trustfile);
1163 #endif /* def FEATURE_TRUST */
1166 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1170 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1173 case ID_HELP_MANUAL:
1174 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1177 case ID_HELP_STATUS:
1178 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1182 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1193 /*********************************************************************
1195 * Function : OnLogInitMenu
1197 * Description : Handler for WM_INITMENU messages. Enable, disable,
1198 * check, and/or uncheck menu options as apropos.
1201 * 1 : hmenu = handle to menu to "make current"
1205 *********************************************************************/
1206 void OnLogInitMenu(HMENU hmenu)
1208 /* Only enable editors if there is a file to edit */
1209 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1210 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1211 #ifdef FEATURE_TRUST
1212 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1213 #endif /* def FEATURE_TRUST */
1215 /* Check/uncheck options */
1216 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1217 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1218 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1219 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1220 #ifdef FEATURE_TOGGLE
1221 /* by haroon - menu item for Enable toggle on/off */
1222 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1223 #endif /* def FEATURE_TOGGLE */
1228 /*********************************************************************
1230 * Function : OnLogTimer
1232 * Description : Handler for WM_TIMER messages.
1235 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1239 *********************************************************************/
1240 void OnLogTimer(int nTimer)
1245 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1248 case TIMER_ANIMSTOP_ID:
1250 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1251 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1252 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1255 case TIMER_CLIPBUFFER_ID:
1256 case TIMER_CLIPBUFFER_FORCE_ID:
1258 g_bClipPending = FALSE;
1259 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1260 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1271 /*********************************************************************
1273 * Function : LogRichEditProc
1275 * Description : Window subclass routine handles some events for the rich edit control.
1278 * 1 : hwnd = window handle of the rich edit control
1279 * 2 : uMsg = message number
1280 * 3 : wParam = first param for this message
1281 * 4 : lParam = next param for this message
1283 * Returns : Appropriate M$ window message handler codes.
1285 *********************************************************************/
1286 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1293 pt.x = LOWORD(lParam);
1294 pt.y = HIWORD(lParam);
1295 ClientToScreen(hwnd, &pt);
1296 OnLogRButtonUp(wParam, pt.x, pt.y);
1300 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1305 /*********************************************************************
1307 * Function : LogWindowProc
1309 * Description : Windows call back routine handles events on the log window.
1312 * 1 : hwnd = handle of the logging window
1313 * 2 : uMsg = message number
1314 * 3 : wParam = first param for this message
1315 * 4 : lParam = next param for this message
1317 * Returns : Appropriate M$ window message handler codes.
1319 *********************************************************************/
1320 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1328 /* This is the end - beautiful friend - the end */
1329 DestroyWindow(g_hwndLogBox);
1330 DestroyWindow(g_hwndLogFrame);
1339 /* Resize the logging window to fit the new frame */
1343 GetClientRect(g_hwndLogFrame, &rc);
1344 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1349 OnLogInitMenu((HMENU) wParam);
1357 OnLogCommand(LOWORD(wParam));
1364 if (g_bCloseHidesWindow)
1366 ShowLogWindow(FALSE);
1371 ShowLogWindow(FALSE);
1377 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1381 #endif /* ndef _WIN_CONSOLE - entire file */