1 const char w32log_rcs[] = "$Id: w32log.c,v 1.23 2002/03/26 22:57:10 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/w32log.c,v $
6 * Purpose : Functions for creating and destroying the log window,
7 * ouputting strings, processing messages and so on.
9 * Copyright : Written by and Copyright (C) 2001-2002 members of
10 * the Privoxy team. http://www.privoxy.org/
12 * Written by and Copyright (C) 1999 Adam Lock
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 * Revision 1.23 2002/03/26 22:57:10 jongfoster
36 * Web server name should begin www.
38 * Revision 1.22 2002/03/24 12:48:23 jongfoster
41 * Revision 1.21 2002/03/24 12:07:35 jongfoster
42 * Consistern name for filters file
44 * Revision 1.20 2002/03/24 12:03:47 jongfoster
47 * Revision 1.19 2002/01/17 21:04:17 jongfoster
48 * Replacing hard references to the URL of the config interface
49 * with #defines from project.h
51 * Revision 1.18 2001/11/30 23:37:24 jongfoster
52 * Renaming the Win32 config file to config.txt - this is almost the
53 * same as the corresponding UNIX name "config"
55 * Revision 1.17 2001/11/16 00:46:31 jongfoster
56 * Fixing compiler warnings
58 * Revision 1.16 2001/08/01 19:58:12 jongfoster
59 * Fixing documentation filenames in help menu, and making status
60 * option work without needing the "Junkbuster Status.URL" file.
62 * Revision 1.15 2001/07/30 22:08:36 jongfoster
63 * Tidying up #defines:
64 * - All feature #defines are now of the form FEATURE_xxx
65 * - Permanently turned off WIN_GUI_EDIT
66 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
68 * Revision 1.14 2001/07/29 18:47:05 jongfoster
69 * Adding missing #include "loadcfg.h"
71 * Revision 1.13 2001/07/19 19:15:14 haroon
72 * - Added a FIXME for EditFile but didn't fix :-)
74 * Revision 1.12 2001/07/13 14:04:59 oes
75 * Removed all #ifdef PCRS
77 * Revision 1.11 2001/06/07 23:08:12 jongfoster
78 * Forward and ACL edit options removed.
80 * Revision 1.10 2001/05/31 21:37:11 jongfoster
81 * GUI changes to rename "permissions file" to "actions file".
83 * Revision 1.9 2001/05/31 17:33:13 oes
87 * Revision 1.8 2001/05/29 09:50:24 jongfoster
88 * Unified blocklist/imagelist/permissionslist.
89 * File format is still under discussion, but the internal changes
92 * Also modified interceptor behaviour:
93 * - We now intercept all URLs beginning with one of the following
94 * prefixes (and *only* these prefixes):
96 * * http://ijbswa.sf.net/config/
97 * * http://ijbswa.sourceforge.net/config/
98 * - New interceptors "home page" - go to http://i.j.b/ to see it.
99 * - Internal changes so that intercepted and fast redirect pages
100 * are not replaced with an image.
101 * - Interceptors now have the option to send a binary page direct
102 * to the client. (i.e. ijb-send-banner uses this)
103 * - Implemented show-url-info interceptor. (Which is why I needed
104 * the above interceptors changes - a typical URL is
105 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
106 * The previous mechanism would not have intercepted that, and
107 * if it had been intercepted then it then it would have replaced
110 * Revision 1.7 2001/05/26 01:26:34 jongfoster
111 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
112 * This #define cannot be set from ./configure - there's no point, it
113 * doesn't work yet. See feature request # 425722
115 * Revision 1.6 2001/05/26 00:31:30 jongfoster
116 * Fixing compiler warning about comparing signed/unsigned.
118 * Revision 1.5 2001/05/26 00:28:36 jongfoster
119 * Automatic reloading of config file.
120 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
121 * Most of the global variables have been moved to a new
122 * struct configuration_spec, accessed through csp->config->globalname
123 * Most of the globals remaining are used by the Win32 GUI.
125 * Revision 1.4 2001/05/22 18:56:28 oes
128 * Revision 1.3 2001/05/20 15:07:54 jongfoster
129 * File is now ignored if _WIN_CONSOLE is defined.
131 * Revision 1.2 2001/05/20 01:21:20 jongfoster
132 * Version 2.9.4 checkin.
133 * - Merged popupfile and cookiefile, and added control over PCRS
134 * filtering, in new "permissionsfile".
135 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
136 * file error you now get a message box (in the Win32 GUI) rather
137 * than the program exiting with no explanation.
138 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
140 * - Removed tabs from "config"
141 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
142 * - Bumped up version number.
144 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
145 * Initial import of version 2.9.3 source tree
148 *********************************************************************/
160 #include <richedit.h>
164 #include "w32taskbar.h"
168 #include "miscutil.h"
172 const char w32res_h_rcs[] = W32RES_H_VERSION;
176 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
179 const char w32log_h_rcs[] = W32LOG_H_VERSION;
181 #ifndef _WIN_CONSOLE /* entire file */
184 * Timers and the various durations
186 #define TIMER_ANIM_ID 1
187 #define TIMER_ANIM_TIME 100
188 #define TIMER_ANIMSTOP_ID 2
189 #define TIMER_ANIMSTOP_TIME 1000
190 #define TIMER_CLIPBUFFER_ID 3
191 #define TIMER_CLIPBUFFER_TIME 1000
192 #define TIMER_CLIPBUFFER_FORCE_ID 4
193 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
196 * Styles of text that can be output
199 #define STYLE_HIGHLIGHT 1
201 #define STYLE_HEADER 3
204 * Number of frames of animation in tray activity sequence
206 #define ANIM_FRAMES 8
208 #define DEFAULT_MAX_BUFFER_LINES 200
209 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
210 #define DEFAULT_LOG_FONT_SIZE 8
213 * These values affect the way the log window behaves, they should be read
214 * from a file but for the moment, they are hardcoded here. Some options are
215 * configurable through the UI.
218 /* Indicates whether task bar shows activity animation */
219 BOOL g_bShowActivityAnimation = 1;
221 /* Indicates if the log window appears on the task bar */
222 BOOL g_bShowOnTaskBar = 0;
224 /* Indicates whether closing the log window really just hides it */
225 BOOL g_bCloseHidesWindow = 1;
227 /* Indicates if messages are logged at all */
228 BOOL g_bLogMessages = 1;
230 /* Indicates whether log messages are highlighted */
231 BOOL g_bHighlightMessages = 1;
233 /* Indicates if buffer is limited in size */
234 BOOL g_bLimitBufferSize = 1;
236 /* Maximum number of lines allowed in buffer when limited */
237 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
240 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
242 /* Size of font to use */
243 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
246 /* FIXME: this is a kludge */
248 const char * g_actions_file = NULL;
249 const char * g_re_filterfile = NULL;
251 const char * g_trustfile = NULL;
252 #endif /* def FEATURE_TRUST */
254 /* FIXME: end kludge */
258 /* Regular expression for detected URLs */
259 #define RE_URL "http:[^ \n\r]*"
262 * Regular expressions that are used to perform highlight in the log window
264 static struct _Pattern
269 } patterns_to_highlight[] =
272 { RE_URL, STYLE_LINK },
273 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
274 /* interesting text to highlight */
275 { "crunch!", STYLE_HIGHLIGHT },
277 { "referer:", STYLE_HEADER },
278 { "proxy-connection:", STYLE_HEADER },
279 { "proxy-agent:", STYLE_HEADER },
280 { "user-agent:", STYLE_HEADER },
281 { "host:", STYLE_HEADER },
282 { "accept:", STYLE_HEADER },
283 { "accept-encoding:", STYLE_HEADER },
284 { "accept-language:", STYLE_HEADER },
285 { "accept-charset:", STYLE_HEADER },
286 { "accept-ranges:", STYLE_HEADER },
287 { "date:", STYLE_HEADER },
288 { "cache-control:", STYLE_HEADER },
289 { "cache-last-checked:", STYLE_HEADER },
290 { "connection:", STYLE_HEADER },
291 { "content-type", STYLE_HEADER },
292 { "content-length", STYLE_HEADER },
293 { "cookie", STYLE_HEADER },
294 { "last-modified:", STYLE_HEADER },
295 { "pragma:", STYLE_HEADER },
296 { "server:", STYLE_HEADER },
297 { "etag:", STYLE_HEADER },
298 { "expires:", STYLE_HEADER },
299 { "warning:", STYLE_HEADER },
300 /* this is the terminator statement - do not delete! */
303 #endif /* def REGEX */
314 static CRITICAL_SECTION g_criticalsection;
315 static HWND g_hwndTray;
316 static HWND g_hwndLogBox;
317 static WNDPROC g_fnLogBox;
318 static HICON g_hiconAnim[ANIM_FRAMES];
319 static HICON g_hiconIdle;
320 static HICON g_hiconApp;
321 static int g_nAnimFrame;
322 static BOOL g_bClipPending = FALSE;
323 static int g_nRichEditVersion = 0;
328 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
329 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
330 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
331 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
332 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
333 static BOOL InitRichEdit(void);
334 static void LogClipBuffer(void);
335 static void LogCreatePatternMatchingBuffers(void);
336 static void LogDestroyPatternMatchingBuffers(void);
337 static int LogPutStringNoMatch(const char *pszText, int style);
340 /*********************************************************************
342 * Function : InitLogWindow
344 * Description : Initialise the log window.
348 * Returns : Always TRUE (there should be error checking on the resources).
350 *********************************************************************/
351 BOOL InitLogWindow(void)
356 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
357 for (i = 0; i < ANIM_FRAMES; i++)
359 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
361 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
363 /* Create the user interface */
364 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
365 g_hwndTray = CreateTrayWindow(g_hInstance);
366 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
368 /* Create pattern matching buffers (for highlighting */
369 LogCreatePatternMatchingBuffers();
371 /* Create a critical section to protect multi-threaded access to certain things */
372 InitializeCriticalSection(&g_criticalsection);
379 /*********************************************************************
381 * Function : TermLogWindow
383 * Description : Cleanup the logwindow.
389 *********************************************************************/
390 void TermLogWindow(void)
394 LogDestroyPatternMatchingBuffers();
396 TrayDeleteIcon(g_hwndTray, 1);
397 DeleteObject(g_hiconApp);
398 DeleteObject(g_hiconIdle);
399 for (i = 0; i < ANIM_FRAMES; i++)
401 DeleteObject(g_hiconAnim[i]);
407 /*********************************************************************
409 * Function : LogCreatePatternMatchingBuffers
411 * Description : Compile the pattern matching buffers.
417 *********************************************************************/
418 void LogCreatePatternMatchingBuffers(void)
422 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
424 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
431 /*********************************************************************
433 * Function : LogDestroyPatternMatchingBuffers
435 * Description : Free up the pattern matching buffers.
441 *********************************************************************/
442 void LogDestroyPatternMatchingBuffers(void)
446 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
448 regfree(&patterns_to_highlight[i].buffer);
455 /*********************************************************************
457 * Function : LogGetURLUnderCursor
459 * Description : Returns the URL from under the cursor (remember to free it!).
463 * Returns : NULL or a pointer to an URL string.
465 *********************************************************************/
466 char *LogGetURLUnderCursor(void)
468 char *szResult = NULL;
474 DWORD nWordStart = 0;
477 regcomp(&re, RE_URL, REG_ICASE);
479 /* Get the position of the cursor over the text window */
480 GetCursorPos(&ptCursor);
481 ScreenToClient(g_hwndLogBox, &ptCursor);
485 /* Search backwards and fowards to obtain the word that is highlighted */
486 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
487 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
488 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
490 /* Compare the string to the pattern */
491 if (nWordEnd > nWordStart)
496 range.chrg.cpMin = nWordStart;
497 range.chrg.cpMax = nWordEnd;
498 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
499 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
501 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
503 szResult = range.lpstrText;
507 free(range.lpstrText);
518 /*********************************************************************
520 * Function : LogPutString
522 * Description : Inserts text into the logging window. This is really
523 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
526 * 1 : pszText = pointer to string going to the log window
528 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
529 * FIXME: this is backwards to the rest of IJB and to common
530 * programming practice. Please use 0 => success instead.
532 *********************************************************************/
533 int LogPutString(const char *pszText)
540 if (pszText == NULL || strlen(pszText) == 0)
550 /* Critical section stops multiple threads doing nasty interactions that
551 * foul up the highlighting and output.
553 EnterCriticalSection(&g_criticalsection);
556 if (g_bHighlightMessages)
560 /* First things first, regexp scan for various things that we would like highlighted */
561 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
563 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
565 char *pszBefore = NULL;
566 char *pszMatch = NULL;
567 char *pszAfter = NULL;
570 /* Split the string up into pieces representing the strings, before
571 at and after the matching pattern
575 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
576 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
577 strncpy(pszBefore, pszText, match.rm_so);
579 if (match.rm_eo < (regoff_t)strlen(pszText))
581 pszAfter = strdup(&pszText[match.rm_eo]);
583 nMatchSize = match.rm_eo - match.rm_so;
584 pszMatch = (char *)malloc(nMatchSize + 1);
585 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
586 pszMatch[nMatchSize] = '\0';
588 /* Recursively call LogPutString */
591 LogPutString(pszBefore);
596 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
601 LogPutString(pszAfter);
612 result = LogPutStringNoMatch(pszText, STYLE_NONE);
617 LeaveCriticalSection(&g_criticalsection);
624 /*********************************************************************
626 * Function : LogPutStringNoMatch
628 * Description : Puts a string into the logging window.
631 * 1 : pszText = pointer to string going to the log window
632 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
634 * Returns : Always 1 => success.
635 * FIXME: this is backwards to the rest of IJB and to common
636 * programming practice. Please use 0 => success instead.
638 *********************************************************************/
639 int LogPutStringNoMatch(const char *pszText, int style)
645 assert(g_hwndLogBox);
646 if (g_hwndLogBox == NULL)
651 /* TODO preserve existing selection */
653 /* Go to the end of the text */
654 nTextLength = GetWindowTextLength(g_hwndLogBox);
655 range.cpMin = nTextLength;
656 range.cpMax = nTextLength;
657 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
659 /* Apply a formatting style */
660 memset(&format, 0, sizeof(format));
661 format.cbSize = sizeof(format);
662 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
663 format.yHeight = (g_nFontSize * 1440) / 72;
664 strcpy(format.szFaceName, g_szFontFaceName);
665 if (style == STYLE_NONE)
668 format.dwEffects |= CFE_AUTOCOLOR;
670 else if (style == STYLE_HEADER)
672 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
674 else if (style == STYLE_HIGHLIGHT)
676 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
678 else if (style == STYLE_LINK)
680 format.dwEffects |= CFE_UNDERLINE;
681 format.crTextColor = RGB(0, 0, 255);
683 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
685 /* Append text to the end */
686 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
688 /* TODO Restore the old selection */
691 if (strchr(pszText, '\n') != NULL)
693 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
696 /* Set the force clip timer going. This timer ensures clipping is done
697 intermittently even when there is a sustained burst of logging
699 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
701 g_bClipPending = TRUE;
709 /*********************************************************************
711 * Function : LogShowActivity
713 * Description : Start the spinner.
719 *********************************************************************/
720 void LogShowActivity(void)
722 /* Start some activity timers */
723 if (g_bShowActivityAnimation)
725 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
726 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
732 /*********************************************************************
734 * Function : LogClipBuffer
736 * Description : Prunes old lines from the log.
742 *********************************************************************/
743 void LogClipBuffer(void)
745 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
746 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
748 /* Compute the range representing the lines to be deleted */
749 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
750 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
753 range.cpMax = nLastChar;
755 /* TODO get current selection */
757 /* TODO adjust and clip old selection against range to be deleted */
759 /* Select range and erase it (turning off autoscroll to prevent
761 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
762 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
763 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
764 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
766 /* Restore old selection */
772 /*********************************************************************
774 * Function : CreateHiddenLogOwnerWindow
776 * Description : Creates a hidden owner window that stops the log
777 * window appearing in the task bar.
780 * 1 : hInstance = application's instance handle
782 * Returns : Handle to newly created window.
784 *********************************************************************/
785 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
787 static const char *szWndName = "PrivoxyLogOwner";
792 wc.lpfnWndProc = LogOwnerWindowProc;
795 wc.hInstance = hInstance;
798 wc.hbrBackground = 0;
800 wc.lpszClassName = szWndName;
804 hwnd = CreateWindow(szWndName, szWndName,
805 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
806 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
813 /*********************************************************************
815 * Function : LogOwnerWindowProc
817 * Description : Dummy procedure that does nothing special.
820 * 1 : hwnd = window handle
821 * 2 : uMsg = message number
822 * 3 : wParam = first param for this message
823 * 4 : lParam = next param for this message
825 * Returns : Same as `DefWindowProc'.
827 *********************************************************************/
828 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
830 return DefWindowProc(hwnd, uMsg, wParam, lParam);
835 /*********************************************************************
837 * Function : CreateLogWindow
839 * Description : Create the logging window.
842 * 1 : hInstance = application's instance handle
843 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
845 * Returns : Handle to newly created window.
847 *********************************************************************/
848 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
850 static const char *szWndName = "PrivoxyLogWindow";
851 static const char *szWndTitle = "Privoxy";
854 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
858 memset(&wc, 0, sizeof(wc));
859 wc.cbSize = sizeof(wc);
860 wc.style = CS_DBLCLKS;
861 wc.lpfnWndProc = LogWindowProc;
864 wc.hInstance = hInstance;
865 wc.hIcon = g_hiconApp;
867 wc.hbrBackground = 0;
868 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
869 wc.lpszClassName = szWndName;
870 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
871 RegisterClassEx(&wc);
873 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
874 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
875 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
877 /* Now create a child list box */
878 GetClientRect(hwnd, &rcClient);
880 /* Create a rich edit control */
882 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
883 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
884 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
885 hwnd, NULL, hInstance, NULL);
886 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
888 /* Subclass the control to catch certain messages */
889 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
890 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
892 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
893 if (!g_bShowOnTaskBar &&
894 (nCmdShow == SW_SHOWMINIMIZED ||
895 nCmdShow == SW_MINIMIZE ||
896 nCmdShow == SW_SHOWMINNOACTIVE))
901 ShowWindow(hwnd, nCmdShow);
904 GetClientRect(g_hwndLogFrame, &rcClient);
905 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
912 /*********************************************************************
914 * Function : InitRichEdit
916 * Description : Initialise the rich edit control library.
920 * Returns : TRUE => success, FALSE => failure.
921 * FIXME: this is backwards to the rest of IJB and to common
922 * programming practice. Please use 0 => success instead.
924 *********************************************************************/
925 BOOL InitRichEdit(void)
927 static HINSTANCE hInstRichEdit;
928 if (hInstRichEdit == NULL)
930 g_nRichEditVersion = 0;
931 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
934 g_nRichEditVersion = _RICHEDIT_VER;
938 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
941 g_nRichEditVersion = 0x0100;
945 return (hInstRichEdit != NULL) ? TRUE : FALSE;
950 /*********************************************************************
952 * Function : ShowLogWindow
954 * Description : Shows or hides the log window. We will also raise the
955 * window on a show command in case it is buried.
958 * 1 : bShow = TRUE to show, FALSE to mimize/hide
962 *********************************************************************/
963 void ShowLogWindow(BOOL bShow)
967 SetForegroundWindow(g_hwndLogFrame);
968 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
970 else if (g_bShowOnTaskBar)
972 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
976 ShowWindow(g_hwndLogFrame, SW_HIDE);
982 /*********************************************************************
984 * Function : EditFile
986 * Description : Opens the specified setting file for editing.
987 * FIXME: What if the file has no associated application. Check for return values
988 * from ShellExecute??
991 * 1 : filename = filename from the config (aka config.txt) file.
995 *********************************************************************/
996 void EditFile(const char *filename)
1000 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1006 /*--------------------------------------------------------------------------*/
1007 /* Windows message handlers */
1008 /*--------------------------------------------------------------------------*/
1011 /*********************************************************************
1013 * Function : OnLogRButtonUp
1015 * Description : Handler for WM_RBUTTONUP messages.
1018 * 1 : nModifier = wParam from mouse message (unused)
1019 * 2 : x = x coordinate of the mouse event
1020 * 3 : y = y coordinate of the mouse event
1024 *********************************************************************/
1025 void OnLogRButtonUp(int nModifier, int x, int y)
1027 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1030 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1032 /* Check if there is a selection */
1034 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1035 if (range.cpMin == range.cpMax)
1037 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1041 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1044 /* Display the popup */
1045 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1052 /*********************************************************************
1054 * Function : OnLogCommand
1056 * Description : Handler for WM_COMMAND messages.
1059 * 1 : nCommand = the command portion of the menu selection event
1063 *********************************************************************/
1064 void OnLogCommand(int nCommand)
1069 ShowLogWindow(TRUE);
1073 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1077 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1080 case ID_VIEW_CLEARLOG:
1081 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1084 case ID_VIEW_LOGMESSAGES:
1085 g_bLogMessages = !g_bLogMessages;
1086 /* SaveLogSettings(); */
1089 case ID_VIEW_MESSAGEHIGHLIGHTING:
1090 g_bHighlightMessages = !g_bHighlightMessages;
1091 /* SaveLogSettings(); */
1094 case ID_VIEW_LIMITBUFFERSIZE:
1095 g_bLimitBufferSize = !g_bLimitBufferSize;
1096 /* SaveLogSettings(); */
1099 case ID_VIEW_ACTIVITYANIMATION:
1100 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1101 /* SaveLogSettings(); */
1104 #ifdef FEATURE_TOGGLE
1105 /* by haroon - change toggle to its opposite value */
1106 case ID_TOGGLE_ENABLED:
1107 g_bToggleIJB = !g_bToggleIJB;
1110 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1114 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1117 #endif /* def FEATURE_TOGGLE */
1119 case ID_TOOLS_EDITCONFIG:
1120 EditFile(configfile);
1123 case ID_TOOLS_EDITACTIONS:
1124 EditFile(g_actions_file);
1127 case ID_TOOLS_EDITFILTERS:
1128 EditFile(g_re_filterfile);
1131 #ifdef FEATURE_TRUST
1132 case ID_TOOLS_EDITTRUST:
1133 EditFile(g_trustfile);
1135 #endif /* def FEATURE_TRUST */
1138 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1142 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1145 case ID_HELP_MANUAL:
1146 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1149 case ID_HELP_STATUS:
1150 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1154 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1165 /*********************************************************************
1167 * Function : OnLogInitMenu
1169 * Description : Handler for WM_INITMENU messages. Enable, disable,
1170 * check, and/or uncheck menu options as apropos.
1173 * 1 : hmenu = handle to menu to "make current"
1177 *********************************************************************/
1178 void OnLogInitMenu(HMENU hmenu)
1180 /* Only enable editors if there is a file to edit */
1181 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1182 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1183 #ifdef FEATURE_TRUST
1184 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1185 #endif /* def FEATURE_TRUST */
1187 /* Check/uncheck options */
1188 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1189 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1190 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1191 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1192 #ifdef FEATURE_TOGGLE
1193 /* by haroon - menu item for Enable toggle on/off */
1194 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1195 #endif /* def FEATURE_TOGGLE */
1200 /*********************************************************************
1202 * Function : OnLogTimer
1204 * Description : Handler for WM_TIMER messages.
1207 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1211 *********************************************************************/
1212 void OnLogTimer(int nTimer)
1217 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1220 case TIMER_ANIMSTOP_ID:
1222 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1223 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1224 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1227 case TIMER_CLIPBUFFER_ID:
1228 case TIMER_CLIPBUFFER_FORCE_ID:
1230 g_bClipPending = FALSE;
1231 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1232 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1243 /*********************************************************************
1245 * Function : LogRichEditProc
1247 * Description : Window subclass routine handles some events for the rich edit control.
1250 * 1 : hwnd = window handle of the rich edit control
1251 * 2 : uMsg = message number
1252 * 3 : wParam = first param for this message
1253 * 4 : lParam = next param for this message
1255 * Returns : Appropriate M$ window message handler codes.
1257 *********************************************************************/
1258 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1265 pt.x = LOWORD(lParam);
1266 pt.y = HIWORD(lParam);
1267 ClientToScreen(hwnd, &pt);
1268 OnLogRButtonUp(wParam, pt.x, pt.y);
1272 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1277 /*********************************************************************
1279 * Function : LogWindowProc
1281 * Description : Windows call back routine handles events on the log window.
1284 * 1 : hwnd = handle of the logging window
1285 * 2 : uMsg = message number
1286 * 3 : wParam = first param for this message
1287 * 4 : lParam = next param for this message
1289 * Returns : Appropriate M$ window message handler codes.
1291 *********************************************************************/
1292 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1300 /* This is the end - beautiful friend - the end */
1301 DestroyWindow(g_hwndLogBox);
1302 DestroyWindow(g_hwndLogFrame);
1311 /* Resize the logging window to fit the new frame */
1315 GetClientRect(g_hwndLogFrame, &rc);
1316 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1321 OnLogInitMenu((HMENU) wParam);
1329 OnLogCommand(LOWORD(wParam));
1336 if (g_bCloseHidesWindow)
1338 ShowLogWindow(FALSE);
1343 ShowLogWindow(FALSE);
1349 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1353 #endif /* ndef _WIN_CONSOLE - entire file */