1 const char w32log_rcs[] = "$Id: w32log.c,v 2.2 2002/09/05 08:43:11 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.2 2002/09/05 08:43:11 oes
36 * Synced with the stable branch:
37 * Revision 1.25.2.1 2002/08/21 17:59:05 oes
38 * - "Show Privoxy Window" now a toggle
39 * - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
41 * Revision 2.1 2002/06/04 16:37:48 jongfoster
42 * Adding Doxygen-style comments to variables
44 * Revision 2.0 2002/06/04 14:34:21 jongfoster
45 * Moving source files to src/
47 * Revision 1.25 2002/04/04 00:36:36 gliptak
48 * always use pcre for matching
50 * Revision 1.24 2002/03/31 17:19:00 jongfoster
51 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
53 * Revision 1.23 2002/03/26 22:57:10 jongfoster
54 * Web server name should begin www.
56 * Revision 1.22 2002/03/24 12:48:23 jongfoster
59 * Revision 1.21 2002/03/24 12:07:35 jongfoster
60 * Consistern name for filters file
62 * Revision 1.20 2002/03/24 12:03:47 jongfoster
65 * Revision 1.19 2002/01/17 21:04:17 jongfoster
66 * Replacing hard references to the URL of the config interface
67 * with #defines from project.h
69 * Revision 1.18 2001/11/30 23:37:24 jongfoster
70 * Renaming the Win32 config file to config.txt - this is almost the
71 * same as the corresponding UNIX name "config"
73 * Revision 1.17 2001/11/16 00:46:31 jongfoster
74 * Fixing compiler warnings
76 * Revision 1.16 2001/08/01 19:58:12 jongfoster
77 * Fixing documentation filenames in help menu, and making status
78 * option work without needing the "Junkbuster Status.URL" file.
80 * Revision 1.15 2001/07/30 22:08:36 jongfoster
81 * Tidying up #defines:
82 * - All feature #defines are now of the form FEATURE_xxx
83 * - Permanently turned off WIN_GUI_EDIT
84 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
86 * Revision 1.14 2001/07/29 18:47:05 jongfoster
87 * Adding missing #include "loadcfg.h"
89 * Revision 1.13 2001/07/19 19:15:14 haroon
90 * - Added a FIXME for EditFile but didn't fix :-)
92 * Revision 1.12 2001/07/13 14:04:59 oes
93 * Removed all #ifdef PCRS
95 * Revision 1.11 2001/06/07 23:08:12 jongfoster
96 * Forward and ACL edit options removed.
98 * Revision 1.10 2001/05/31 21:37:11 jongfoster
99 * GUI changes to rename "permissions file" to "actions file".
101 * Revision 1.9 2001/05/31 17:33:13 oes
105 * Revision 1.8 2001/05/29 09:50:24 jongfoster
106 * Unified blocklist/imagelist/permissionslist.
107 * File format is still under discussion, but the internal changes
110 * Also modified interceptor behaviour:
111 * - We now intercept all URLs beginning with one of the following
112 * prefixes (and *only* these prefixes):
114 * * http://ijbswa.sf.net/config/
115 * * http://ijbswa.sourceforge.net/config/
116 * - New interceptors "home page" - go to http://i.j.b/ to see it.
117 * - Internal changes so that intercepted and fast redirect pages
118 * are not replaced with an image.
119 * - Interceptors now have the option to send a binary page direct
120 * to the client. (i.e. ijb-send-banner uses this)
121 * - Implemented show-url-info interceptor. (Which is why I needed
122 * the above interceptors changes - a typical URL is
123 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
124 * The previous mechanism would not have intercepted that, and
125 * if it had been intercepted then it then it would have replaced
128 * Revision 1.7 2001/05/26 01:26:34 jongfoster
129 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
130 * This #define cannot be set from ./configure - there's no point, it
131 * doesn't work yet. See feature request # 425722
133 * Revision 1.6 2001/05/26 00:31:30 jongfoster
134 * Fixing compiler warning about comparing signed/unsigned.
136 * Revision 1.5 2001/05/26 00:28:36 jongfoster
137 * Automatic reloading of config file.
138 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
139 * Most of the global variables have been moved to a new
140 * struct configuration_spec, accessed through csp->config->globalname
141 * Most of the globals remaining are used by the Win32 GUI.
143 * Revision 1.4 2001/05/22 18:56:28 oes
146 * Revision 1.3 2001/05/20 15:07:54 jongfoster
147 * File is now ignored if _WIN_CONSOLE is defined.
149 * Revision 1.2 2001/05/20 01:21:20 jongfoster
150 * Version 2.9.4 checkin.
151 * - Merged popupfile and cookiefile, and added control over PCRS
152 * filtering, in new "permissionsfile".
153 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
154 * file error you now get a message box (in the Win32 GUI) rather
155 * than the program exiting with no explanation.
156 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
158 * - Removed tabs from "config"
159 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
160 * - Bumped up version number.
162 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
163 * Initial import of version 2.9.3 source tree
166 *********************************************************************/
178 #include <richedit.h>
182 #include "w32taskbar.h"
186 #include "miscutil.h"
190 const char w32res_h_rcs[] = W32RES_H_VERSION;
194 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
197 const char w32log_h_rcs[] = W32LOG_H_VERSION;
199 #ifndef _WIN_CONSOLE /* entire file */
202 * Timers and the various durations
204 #define TIMER_ANIM_ID 1
205 #define TIMER_ANIM_TIME 100
206 #define TIMER_ANIMSTOP_ID 2
207 #define TIMER_ANIMSTOP_TIME 1000
208 #define TIMER_CLIPBUFFER_ID 3
209 #define TIMER_CLIPBUFFER_TIME 1000
210 #define TIMER_CLIPBUFFER_FORCE_ID 4
211 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
214 * Styles of text that can be output
217 #define STYLE_HIGHLIGHT 1
219 #define STYLE_HEADER 3
222 * Number of frames of animation in tray activity sequence
224 #define ANIM_FRAMES 8
226 #define DEFAULT_MAX_BUFFER_LINES 200
227 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
228 #define DEFAULT_LOG_FONT_SIZE 8
231 * These values affect the way the log window behaves, they should be read
232 * from a file but for the moment, they are hardcoded here. Some options are
233 * configurable through the UI.
236 /** Indicates whether task bar shows activity animation */
237 BOOL g_bShowActivityAnimation = 1;
239 /** Indicates whether the log window is shown */
240 BOOL g_bShowLogWindow = 1;
242 /** Indicates if the log window appears on the task bar */
243 BOOL g_bShowOnTaskBar = 0;
245 /** Indicates whether closing the log window really just hides it */
246 BOOL g_bCloseHidesWindow = 1;
248 /** Indicates if messages are logged at all */
249 BOOL g_bLogMessages = 1;
251 /** Indicates whether log messages are highlighted */
252 BOOL g_bHighlightMessages = 1;
254 /** Indicates if buffer is limited in size */
255 BOOL g_bLimitBufferSize = 1;
257 /** Maximum number of lines allowed in buffer when limited */
258 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
261 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
263 /** Size of font to use */
264 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
267 /* FIXME: this is a kludge */
269 /** Second (=default, hopefully) Actions file name. */
270 const char * g_default_actions_file = NULL;
272 /** Third (=user, hopefully) Actions file name. */
273 const char * g_user_actions_file = NULL;
275 /** Filter file name. */
276 const char * g_re_filterfile = NULL;
280 /** Trust file name. */
281 const char * g_trustfile = NULL;
283 #endif /* def FEATURE_TRUST */
285 /* FIXME: end kludge */
288 /** Regular expression for detected URLs */
289 #define RE_URL "http:[^ \n\r]*"
293 * Regular expressions that are used to perform highlight in the log window.
295 static struct _Pattern
297 const char *str; /**< The pattern to match. */
298 int style; /**< How to highlight it - one of the STYLE_xxx constants. */
299 regex_t buffer; /**< The compiled pattern to use for matching. Generated from str. */
300 } patterns_to_highlight[] =
303 { RE_URL, STYLE_LINK },
304 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
305 /* interesting text to highlight */
306 { "crunch!", STYLE_HIGHLIGHT },
308 { "referer:", STYLE_HEADER },
309 { "proxy-connection:", STYLE_HEADER },
310 { "proxy-agent:", STYLE_HEADER },
311 { "user-agent:", STYLE_HEADER },
312 { "host:", STYLE_HEADER },
313 { "accept:", STYLE_HEADER },
314 { "accept-encoding:", STYLE_HEADER },
315 { "accept-language:", STYLE_HEADER },
316 { "accept-charset:", STYLE_HEADER },
317 { "accept-ranges:", STYLE_HEADER },
318 { "date:", STYLE_HEADER },
319 { "cache-control:", STYLE_HEADER },
320 { "cache-last-checked:", STYLE_HEADER },
321 { "connection:", STYLE_HEADER },
322 { "content-type", STYLE_HEADER },
323 { "content-length", STYLE_HEADER },
324 { "cookie", STYLE_HEADER },
325 { "last-modified:", STYLE_HEADER },
326 { "pragma:", STYLE_HEADER },
327 { "server:", STYLE_HEADER },
328 { "etag:", STYLE_HEADER },
329 { "expires:", STYLE_HEADER },
330 { "warning:", STYLE_HEADER },
331 /* this is the terminator statement - do not delete! */
340 * Window handle for the log window.
349 /** Critical section used to serialize output to the log window. */
350 static CRITICAL_SECTION g_criticalsection;
352 /** The window handle of the tray icon window. */
353 static HWND g_hwndTray;
355 /** The window handle of the log window. */
356 static HWND g_hwndLogBox;
358 /** The default window procedure for the rich edit control. Set when we
359 subclass this window. */
360 static WNDPROC g_fnLogBox;
362 /** The icons for the activity animation. */
363 static HICON g_hiconAnim[ANIM_FRAMES];
365 /** The icon to use when idle. */
366 static HICON g_hiconIdle;
368 /** The main application icon. */
369 static HICON g_hiconApp;
371 /** The index of the current animation frame. */
372 static int g_nAnimFrame;
374 /** If a timer to call LogClipBuffer() has been started. */
375 static BOOL g_bClipPending = FALSE;
377 /** The version of the rich edit control we're using. */
378 static int g_nRichEditVersion = 0;
384 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
385 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
386 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
387 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
388 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
389 static BOOL InitRichEdit(void);
390 static void LogClipBuffer(void);
391 static void LogCreatePatternMatchingBuffers(void);
392 static void LogDestroyPatternMatchingBuffers(void);
393 static int LogPutStringNoMatch(const char *pszText, int style);
396 /*********************************************************************
398 * Function : InitLogWindow
400 * Description : Initialise the log window.
404 * Returns : Always TRUE (there should be error checking on the resources).
406 *********************************************************************/
407 BOOL InitLogWindow(void)
412 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
413 for (i = 0; i < ANIM_FRAMES; i++)
415 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
417 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
419 /* Create the user interface */
420 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
421 g_hwndTray = CreateTrayWindow(g_hInstance);
422 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
424 /* Create pattern matching buffers (for highlighting */
425 LogCreatePatternMatchingBuffers();
427 /* Create a critical section to protect multi-threaded access to certain things */
428 InitializeCriticalSection(&g_criticalsection);
435 /*********************************************************************
437 * Function : TermLogWindow
439 * Description : Cleanup the logwindow.
445 *********************************************************************/
446 void TermLogWindow(void)
450 LogDestroyPatternMatchingBuffers();
452 TrayDeleteIcon(g_hwndTray, 1);
453 DeleteObject(g_hiconApp);
454 DeleteObject(g_hiconIdle);
455 for (i = 0; i < ANIM_FRAMES; i++)
457 DeleteObject(g_hiconAnim[i]);
463 /*********************************************************************
465 * Function : LogCreatePatternMatchingBuffers
467 * Description : Compile the pattern matching buffers.
473 *********************************************************************/
474 void LogCreatePatternMatchingBuffers(void)
477 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
479 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
484 /*********************************************************************
486 * Function : LogDestroyPatternMatchingBuffers
488 * Description : Free up the pattern matching buffers.
494 *********************************************************************/
495 void LogDestroyPatternMatchingBuffers(void)
498 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
500 regfree(&patterns_to_highlight[i].buffer);
505 /*********************************************************************
507 * Function : LogGetURLUnderCursor
509 * Description : Returns the URL from under the cursor (remember to free it!).
513 * Returns : NULL or a pointer to an URL string.
515 *********************************************************************/
516 char *LogGetURLUnderCursor(void)
518 char *szResult = NULL;
523 DWORD nWordStart = 0;
526 regcomp(&re, RE_URL, REG_ICASE);
528 /* Get the position of the cursor over the text window */
529 GetCursorPos(&ptCursor);
530 ScreenToClient(g_hwndLogBox, &ptCursor);
534 /* Search backwards and fowards to obtain the word that is highlighted */
535 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
536 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
537 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
539 /* Compare the string to the pattern */
540 if (nWordEnd > nWordStart)
545 range.chrg.cpMin = nWordStart;
546 range.chrg.cpMax = nWordEnd;
547 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
548 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
550 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
552 szResult = range.lpstrText;
556 free(range.lpstrText);
566 /*********************************************************************
568 * Function : LogPutString
570 * Description : Inserts text into the logging window. This is really
571 * a regexp aware wrapper function to `LogPutStringNoMatch'.
574 * 1 : pszText = pointer to string going to the log window
576 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
577 * FIXME: this is backwards to the rest of IJB and to common
578 * programming practice. Please use 0 => success instead.
580 *********************************************************************/
581 int LogPutString(const char *pszText)
586 if (pszText == NULL || strlen(pszText) == 0)
596 /* Critical section stops multiple threads doing nasty interactions that
597 * foul up the highlighting and output.
599 EnterCriticalSection(&g_criticalsection);
601 if (g_bHighlightMessages)
605 /* First things first, regexp scan for various things that we would like highlighted */
606 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
608 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
610 char *pszBefore = NULL;
611 char *pszMatch = NULL;
612 char *pszAfter = NULL;
615 /* Split the string up into pieces representing the strings, before
616 at and after the matching pattern
620 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
621 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
622 strncpy(pszBefore, pszText, match.rm_so);
624 if (match.rm_eo < (regoff_t)strlen(pszText))
626 pszAfter = strdup(&pszText[match.rm_eo]);
628 nMatchSize = match.rm_eo - match.rm_so;
629 pszMatch = (char *)malloc(nMatchSize + 1);
630 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
631 pszMatch[nMatchSize] = '\0';
633 /* Recursively call LogPutString */
636 LogPutString(pszBefore);
641 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
646 LogPutString(pszAfter);
656 result = LogPutStringNoMatch(pszText, STYLE_NONE);
659 LeaveCriticalSection(&g_criticalsection);
666 /*********************************************************************
668 * Function : LogPutStringNoMatch
670 * Description : Puts a string into the logging window.
673 * 1 : pszText = pointer to string going to the log window
674 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
676 * Returns : Always 1 => success.
677 * FIXME: this is backwards to the rest of IJB and to common
678 * programming practice. Please use 0 => success instead.
680 *********************************************************************/
681 int LogPutStringNoMatch(const char *pszText, int style)
687 assert(g_hwndLogBox);
688 if (g_hwndLogBox == NULL)
693 /* TODO preserve existing selection */
695 /* Go to the end of the text */
696 nTextLength = GetWindowTextLength(g_hwndLogBox);
697 range.cpMin = nTextLength;
698 range.cpMax = nTextLength;
699 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
701 /* Apply a formatting style */
702 memset(&format, 0, sizeof(format));
703 format.cbSize = sizeof(format);
704 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
705 format.yHeight = (g_nFontSize * 1440) / 72;
706 strcpy(format.szFaceName, g_szFontFaceName);
707 if (style == STYLE_NONE)
710 format.dwEffects |= CFE_AUTOCOLOR;
712 else if (style == STYLE_HEADER)
714 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
716 else if (style == STYLE_HIGHLIGHT)
718 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
720 else if (style == STYLE_LINK)
722 format.dwEffects |= CFE_UNDERLINE;
723 format.crTextColor = RGB(0, 0, 255);
725 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
727 /* Append text to the end */
728 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
730 /* TODO Restore the old selection */
733 if (strchr(pszText, '\n') != NULL)
735 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
738 /* Set the force clip timer going. This timer ensures clipping is done
739 intermittently even when there is a sustained burst of logging
741 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
743 g_bClipPending = TRUE;
751 /*********************************************************************
753 * Function : LogShowActivity
755 * Description : Start the spinner.
761 *********************************************************************/
762 void LogShowActivity(void)
764 /* Start some activity timers */
765 if (g_bShowActivityAnimation)
767 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
768 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
774 /*********************************************************************
776 * Function : LogClipBuffer
778 * Description : Prunes old lines from the log.
779 * This is called occasionally, from a timer callback.
785 *********************************************************************/
786 void LogClipBuffer(void)
788 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
789 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
791 /* Compute the range representing the lines to be deleted */
792 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
793 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
796 range.cpMax = nLastChar;
798 /* TODO get current selection */
800 /* TODO adjust and clip old selection against range to be deleted */
802 /* Select range and erase it (turning off autoscroll to prevent
804 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
805 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
806 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
807 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
809 /* Restore old selection */
815 /*********************************************************************
817 * Function : CreateHiddenLogOwnerWindow
819 * Description : Creates a hidden owner window that stops the log
820 * window appearing in the task bar.
823 * 1 : hInstance = application's instance handle
825 * Returns : Handle to newly created window.
827 *********************************************************************/
828 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
830 static const char *szWndName = "PrivoxyLogOwner";
835 wc.lpfnWndProc = LogOwnerWindowProc;
838 wc.hInstance = hInstance;
841 wc.hbrBackground = 0;
843 wc.lpszClassName = szWndName;
847 hwnd = CreateWindow(szWndName, szWndName,
848 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
849 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
856 /*********************************************************************
858 * Function : LogOwnerWindowProc
860 * Description : Dummy procedure that does nothing special.
863 * 1 : hwnd = window handle
864 * 2 : uMsg = message number
865 * 3 : wParam = first param for this message
866 * 4 : lParam = next param for this message
868 * Returns : Same as `DefWindowProc'.
870 *********************************************************************/
871 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
873 return DefWindowProc(hwnd, uMsg, wParam, lParam);
878 /*********************************************************************
880 * Function : CreateLogWindow
882 * Description : Create the logging window.
885 * 1 : hInstance = application's instance handle
886 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
888 * Returns : Handle to newly created window.
890 *********************************************************************/
891 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
893 static const char *szWndName = "PrivoxyLogWindow";
894 static const char *szWndTitle = "Privoxy";
897 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
901 memset(&wc, 0, sizeof(wc));
902 wc.cbSize = sizeof(wc);
903 wc.style = CS_DBLCLKS;
904 wc.lpfnWndProc = LogWindowProc;
907 wc.hInstance = hInstance;
908 wc.hIcon = g_hiconApp;
910 wc.hbrBackground = 0;
911 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
912 wc.lpszClassName = szWndName;
913 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
914 RegisterClassEx(&wc);
916 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
917 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
918 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
920 /* Now create a child list box */
921 GetClientRect(hwnd, &rcClient);
923 /* Create a rich edit control */
925 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
926 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
927 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
928 hwnd, NULL, hInstance, NULL);
929 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
931 /* Subclass the control to catch certain messages */
932 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
933 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
935 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
936 if (!g_bShowOnTaskBar &&
937 (nCmdShow == SW_SHOWMINIMIZED ||
938 nCmdShow == SW_MINIMIZE ||
939 nCmdShow == SW_SHOWMINNOACTIVE))
944 ShowWindow(hwnd, nCmdShow);
947 GetClientRect(g_hwndLogFrame, &rcClient);
948 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
955 /*********************************************************************
957 * Function : InitRichEdit
959 * Description : Initialise the rich edit control library.
963 * Returns : TRUE => success, FALSE => failure.
964 * FIXME: this is backwards to the rest of IJB and to common
965 * programming practice. Please use 0 => success instead.
967 *********************************************************************/
968 BOOL InitRichEdit(void)
970 static HINSTANCE hInstRichEdit;
971 if (hInstRichEdit == NULL)
973 g_nRichEditVersion = 0;
974 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
977 g_nRichEditVersion = _RICHEDIT_VER;
981 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
984 g_nRichEditVersion = 0x0100;
988 return (hInstRichEdit != NULL) ? TRUE : FALSE;
993 /*********************************************************************
995 * Function : ShowLogWindow
997 * Description : Shows or hides the log window. We will also raise the
998 * window on a show command in case it is buried.
1001 * 1 : bShow = TRUE to show, FALSE to mimize/hide
1005 *********************************************************************/
1006 void ShowLogWindow(BOOL bShow)
1010 SetForegroundWindow(g_hwndLogFrame);
1011 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1014 else if (g_bShowOnTaskBar)
1016 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1020 ShowWindow(g_hwndLogFrame, SW_HIDE);
1025 /*********************************************************************
1027 * Function : EditFile
1029 * Description : Opens the specified setting file for editing.
1030 * FIXME: What if the file has no associated application. Check for return values
1031 * from ShellExecute??
1034 * 1 : filename = filename from the config (aka config.txt) file.
1038 *********************************************************************/
1039 void EditFile(const char *filename)
1043 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1049 /*--------------------------------------------------------------------------*/
1050 /* Windows message handlers */
1051 /*--------------------------------------------------------------------------*/
1054 /*********************************************************************
1056 * Function : OnLogRButtonUp
1058 * Description : Handler for WM_RBUTTONUP messages.
1061 * 1 : nModifier = wParam from mouse message (unused)
1062 * 2 : x = x coordinate of the mouse event
1063 * 3 : y = y coordinate of the mouse event
1067 *********************************************************************/
1068 void OnLogRButtonUp(int nModifier, int x, int y)
1070 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1073 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1075 /* Check if there is a selection */
1077 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1078 if (range.cpMin == range.cpMax)
1080 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1084 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1087 /* Display the popup */
1088 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1095 /*********************************************************************
1097 * Function : OnLogCommand
1099 * Description : Handler for WM_COMMAND messages.
1102 * 1 : nCommand = the command portion of the menu selection event
1106 *********************************************************************/
1107 void OnLogCommand(int nCommand)
1111 case ID_TOGGLE_SHOWWINDOW:
1112 g_bShowLogWindow = !g_bShowLogWindow;
1114 ShowLogWindow(g_bShowLogWindow);
1118 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1122 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1125 case ID_VIEW_CLEARLOG:
1126 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1129 case ID_VIEW_LOGMESSAGES:
1130 g_bLogMessages = !g_bLogMessages;
1131 /* SaveLogSettings(); */
1134 case ID_VIEW_MESSAGEHIGHLIGHTING:
1135 g_bHighlightMessages = !g_bHighlightMessages;
1136 /* SaveLogSettings(); */
1139 case ID_VIEW_LIMITBUFFERSIZE:
1140 g_bLimitBufferSize = !g_bLimitBufferSize;
1141 /* SaveLogSettings(); */
1144 case ID_VIEW_ACTIVITYANIMATION:
1145 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1146 /* SaveLogSettings(); */
1149 #ifdef FEATURE_TOGGLE
1150 /* by haroon - change toggle to its opposite value */
1151 case ID_TOGGLE_ENABLED:
1152 g_bToggleIJB = !g_bToggleIJB;
1155 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1159 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1162 #endif /* def FEATURE_TOGGLE */
1164 case ID_TOOLS_EDITCONFIG:
1165 EditFile(configfile);
1168 case ID_TOOLS_EDITDEFAULTACTIONS:
1169 EditFile(g_default_actions_file);
1172 case ID_TOOLS_EDITUSERACTIONS:
1173 EditFile(g_user_actions_file);
1176 case ID_TOOLS_EDITFILTERS:
1177 EditFile(g_re_filterfile);
1180 #ifdef FEATURE_TRUST
1181 case ID_TOOLS_EDITTRUST:
1182 EditFile(g_trustfile);
1184 #endif /* def FEATURE_TRUST */
1187 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1191 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1194 case ID_HELP_MANUAL:
1195 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1198 case ID_HELP_STATUS:
1199 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1203 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1214 /*********************************************************************
1216 * Function : OnLogInitMenu
1218 * Description : Handler for WM_INITMENU messages. Enable, disable,
1219 * check, and/or uncheck menu options as apropos.
1222 * 1 : hmenu = handle to menu to "make current"
1226 *********************************************************************/
1227 void OnLogInitMenu(HMENU hmenu)
1229 /* Only enable editors if there is a file to edit */
1230 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1231 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1232 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1233 #ifdef FEATURE_TRUST
1234 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1235 #endif /* def FEATURE_TRUST */
1237 /* Check/uncheck options */
1238 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1239 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1240 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1241 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1242 #ifdef FEATURE_TOGGLE
1243 /* by haroon - menu item for Enable toggle on/off */
1244 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1245 #endif /* def FEATURE_TOGGLE */
1246 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1251 /*********************************************************************
1253 * Function : OnLogTimer
1255 * Description : Handler for WM_TIMER messages.
1258 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1262 *********************************************************************/
1263 void OnLogTimer(int nTimer)
1268 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1271 case TIMER_ANIMSTOP_ID:
1273 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1274 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1275 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1278 case TIMER_CLIPBUFFER_ID:
1279 case TIMER_CLIPBUFFER_FORCE_ID:
1281 g_bClipPending = FALSE;
1282 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1283 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1294 /*********************************************************************
1296 * Function : LogRichEditProc
1298 * Description : Window subclass routine handles some events for the rich edit control.
1301 * 1 : hwnd = window handle of the rich edit control
1302 * 2 : uMsg = message number
1303 * 3 : wParam = first param for this message
1304 * 4 : lParam = next param for this message
1306 * Returns : Appropriate M$ window message handler codes.
1308 *********************************************************************/
1309 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1316 pt.x = LOWORD(lParam);
1317 pt.y = HIWORD(lParam);
1318 ClientToScreen(hwnd, &pt);
1319 OnLogRButtonUp(wParam, pt.x, pt.y);
1323 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1328 /*********************************************************************
1330 * Function : LogWindowProc
1332 * Description : Windows call back routine handles events on the log window.
1335 * 1 : hwnd = handle of the logging window
1336 * 2 : uMsg = message number
1337 * 3 : wParam = first param for this message
1338 * 4 : lParam = next param for this message
1340 * Returns : Appropriate M$ window message handler codes.
1342 *********************************************************************/
1343 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1351 /* This is the end - beautiful friend - the end */
1352 DestroyWindow(g_hwndLogBox);
1353 DestroyWindow(g_hwndLogFrame);
1361 g_bShowLogWindow = wParam;
1363 /* Resize the logging window to fit the new frame */
1367 GetClientRect(g_hwndLogFrame, &rc);
1368 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1373 OnLogInitMenu((HMENU) wParam);
1381 OnLogCommand(LOWORD(wParam));
1388 if (g_bCloseHidesWindow)
1390 ShowLogWindow(FALSE);
1395 ShowLogWindow(FALSE);
1401 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1405 #endif /* ndef _WIN_CONSOLE - entire file */