1 const char w32log_rcs[] = "$Id: w32log.c,v 1.28 2008/11/02 14:37:47 ler762 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.28 2008/11/02 14:37:47 ler762
36 * commit the part of the patches I've been using that were written by torford and gjmurphy
37 * [ 1824315 ] Minor code cleanup
38 * [ 1781135 ] Patch - Add clear log, select all, and Accelerators for w32
39 * http://sourceforge.net/tracker/?func=detail&atid=311118&aid=1781135&group_id=11118
40 * The full patch adds control keys A(select all), C(copy) and D(delete all) to the
41 * Privoxy log window menu. Select all and copy work for me without the patch
42 * (albeit without showing the accelerator keys on the menu), so the only part of the
43 * patch I've been using for the last year or so has been the ctrl-d to delete
44 * everything in the Privoxy log window.
46 * Revision 1.27 2006/07/18 14:48:48 david__schmidt
47 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
48 * with what was really the latest development (the v_3_0_branch branch)
50 * Revision 1.25.2.5 2003/04/04 12:48:51 oes
52 * - Made tray menu correctly reflect initial window visibility state
53 * - Hopefully fixed problem where log window contents wasn't visible
54 * until vertical scroll bar was clicked. Thanks to Guy for the fix!
56 * Revision 1.25.2.4 2003/03/11 11:53:59 oes
57 * Cosmetic: Renamed cryptic variable
59 * Revision 1.25.2.3 2002/11/20 14:39:05 oes
60 * Fixed compiler warning
62 * Revision 1.25.2.2 2002/09/25 15:23:10 oes
63 * Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
65 * Revision 1.25.2.1 2002/08/21 17:59:05 oes
66 * - "Show Privoxy Window" now a toggle
67 * - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
69 * Revision 1.25 2002/04/04 00:36:36 gliptak
70 * always use pcre for matching
72 * Revision 1.24 2002/03/31 17:19:00 jongfoster
73 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
75 * Revision 1.23 2002/03/26 22:57:10 jongfoster
76 * Web server name should begin www.
78 * Revision 1.22 2002/03/24 12:48:23 jongfoster
81 * Revision 1.21 2002/03/24 12:07:35 jongfoster
82 * Consistern name for filters file
84 * Revision 1.20 2002/03/24 12:03:47 jongfoster
87 * Revision 1.19 2002/01/17 21:04:17 jongfoster
88 * Replacing hard references to the URL of the config interface
89 * with #defines from project.h
91 * Revision 1.18 2001/11/30 23:37:24 jongfoster
92 * Renaming the Win32 config file to config.txt - this is almost the
93 * same as the corresponding UNIX name "config"
95 * Revision 1.17 2001/11/16 00:46:31 jongfoster
96 * Fixing compiler warnings
98 * Revision 1.16 2001/08/01 19:58:12 jongfoster
99 * Fixing documentation filenames in help menu, and making status
100 * option work without needing the "Junkbuster Status.URL" file.
102 * Revision 1.15 2001/07/30 22:08:36 jongfoster
103 * Tidying up #defines:
104 * - All feature #defines are now of the form FEATURE_xxx
105 * - Permanently turned off WIN_GUI_EDIT
106 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
108 * Revision 1.14 2001/07/29 18:47:05 jongfoster
109 * Adding missing #include "loadcfg.h"
111 * Revision 1.13 2001/07/19 19:15:14 haroon
112 * - Added a FIXME for EditFile but didn't fix :-)
114 * Revision 1.12 2001/07/13 14:04:59 oes
115 * Removed all #ifdef PCRS
117 * Revision 1.11 2001/06/07 23:08:12 jongfoster
118 * Forward and ACL edit options removed.
120 * Revision 1.10 2001/05/31 21:37:11 jongfoster
121 * GUI changes to rename "permissions file" to "actions file".
123 * Revision 1.9 2001/05/31 17:33:13 oes
127 * Revision 1.8 2001/05/29 09:50:24 jongfoster
128 * Unified blocklist/imagelist/permissionslist.
129 * File format is still under discussion, but the internal changes
132 * Also modified interceptor behaviour:
133 * - We now intercept all URLs beginning with one of the following
134 * prefixes (and *only* these prefixes):
136 * * http://ijbswa.sf.net/config/
137 * * http://ijbswa.sourceforge.net/config/
138 * - New interceptors "home page" - go to http://i.j.b/ to see it.
139 * - Internal changes so that intercepted and fast redirect pages
140 * are not replaced with an image.
141 * - Interceptors now have the option to send a binary page direct
142 * to the client. (i.e. ijb-send-banner uses this)
143 * - Implemented show-url-info interceptor. (Which is why I needed
144 * the above interceptors changes - a typical URL is
145 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
146 * The previous mechanism would not have intercepted that, and
147 * if it had been intercepted then it then it would have replaced
150 * Revision 1.7 2001/05/26 01:26:34 jongfoster
151 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
152 * This #define cannot be set from ./configure - there's no point, it
153 * doesn't work yet. See feature request # 425722
155 * Revision 1.6 2001/05/26 00:31:30 jongfoster
156 * Fixing compiler warning about comparing signed/unsigned.
158 * Revision 1.5 2001/05/26 00:28:36 jongfoster
159 * Automatic reloading of config file.
160 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
161 * Most of the global variables have been moved to a new
162 * struct configuration_spec, accessed through csp->config->globalname
163 * Most of the globals remaining are used by the Win32 GUI.
165 * Revision 1.4 2001/05/22 18:56:28 oes
168 * Revision 1.3 2001/05/20 15:07:54 jongfoster
169 * File is now ignored if _WIN_CONSOLE is defined.
171 * Revision 1.2 2001/05/20 01:21:20 jongfoster
172 * Version 2.9.4 checkin.
173 * - Merged popupfile and cookiefile, and added control over PCRS
174 * filtering, in new "permissionsfile".
175 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
176 * file error you now get a message box (in the Win32 GUI) rather
177 * than the program exiting with no explanation.
178 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
180 * - Removed tabs from "config"
181 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
182 * - Bumped up version number.
184 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
185 * Initial import of version 2.9.3 source tree
188 *********************************************************************/
200 #include <richedit.h>
204 #include "w32taskbar.h"
208 #include "miscutil.h"
212 const char w32res_h_rcs[] = W32RES_H_VERSION;
216 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
219 const char w32log_h_rcs[] = W32LOG_H_VERSION;
221 #ifndef _WIN_CONSOLE /* entire file */
224 * Timers and the various durations
226 #define TIMER_ANIM_ID 1
227 #define TIMER_ANIM_TIME 100
228 #define TIMER_ANIMSTOP_ID 2
229 #define TIMER_ANIMSTOP_TIME 1000
230 #define TIMER_CLIPBUFFER_ID 3
231 #define TIMER_CLIPBUFFER_TIME 1000
232 #define TIMER_CLIPBUFFER_FORCE_ID 4
233 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
236 * Styles of text that can be output
239 #define STYLE_HIGHLIGHT 1
241 #define STYLE_HEADER 3
244 * Number of frames of animation in tray activity sequence
246 #define ANIM_FRAMES 8
248 #define DEFAULT_MAX_BUFFER_LINES 200
249 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
250 #define DEFAULT_LOG_FONT_SIZE 8
253 * These values affect the way the log window behaves, they should be read
254 * from a file but for the moment, they are hardcoded here. Some options are
255 * configurable through the UI.
258 /* Indicates whether task bar shows activity animation */
259 BOOL g_bShowActivityAnimation = 1;
261 /* Indicates whether the log window is shown */
262 BOOL g_bShowLogWindow = 1;
264 /* Indicates if the log window appears on the task bar */
265 BOOL g_bShowOnTaskBar = 0;
267 /* Indicates whether closing the log window really just hides it */
268 BOOL g_bCloseHidesWindow = 1;
270 /* Indicates if messages are logged at all */
271 BOOL g_bLogMessages = 1;
273 /* Indicates whether log messages are highlighted */
274 BOOL g_bHighlightMessages = 1;
276 /* Indicates if buffer is limited in size */
277 BOOL g_bLimitBufferSize = 1;
279 /* Maximum number of lines allowed in buffer when limited */
280 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
283 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
285 /* Size of font to use */
286 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
289 /* FIXME: this is a kludge */
291 const char * g_default_actions_file = NULL;
292 const char * g_user_actions_file = NULL;
293 const char * g_re_filterfile = NULL;
295 const char * g_trustfile = NULL;
296 #endif /* def FEATURE_TRUST */
298 /* FIXME: end kludge */
300 /* Regular expression for detected URLs */
301 #define RE_URL "http:[^ \n\r]*"
304 * Regular expressions that are used to perform highlight in the log window
306 static struct _Pattern
311 } patterns_to_highlight[] =
314 { RE_URL, STYLE_LINK },
315 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
316 /* interesting text to highlight */
317 /* see jcc.c crunch_reason for the full list */
318 { "Crunch: Blocked:", STYLE_HIGHLIGHT },
319 { "Crunch: Untrusted", STYLE_HIGHLIGHT },
320 { "Crunch: Redirected:", STYLE_HIGHLIGHT },
321 { "Crunch: DNS failure", STYLE_HIGHLIGHT },
322 { "Crunch: Forwarding failed", STYLE_HIGHLIGHT },
323 { "Crunch: Connection failure", STYLE_HIGHLIGHT },
324 { "Crunch: Out of memory", STYLE_HIGHLIGHT },
325 /* what are all the possible error strings?? */
326 { "Error:", STYLE_HIGHLIGHT },
328 { "referer:", STYLE_HEADER },
329 { "proxy-connection:", STYLE_HEADER },
330 { "proxy-agent:", STYLE_HEADER },
331 { "user-agent:", STYLE_HEADER },
332 { "host:", STYLE_HEADER },
333 { "accept:", STYLE_HEADER },
334 { "accept-encoding:", STYLE_HEADER },
335 { "accept-language:", STYLE_HEADER },
336 { "accept-charset:", STYLE_HEADER },
337 { "accept-ranges:", STYLE_HEADER },
338 { "date:", STYLE_HEADER },
339 { "cache-control:", STYLE_HEADER },
340 { "cache-last-checked:", STYLE_HEADER },
341 { "connection:", STYLE_HEADER },
342 { "content-type", STYLE_HEADER },
343 { "content-length", STYLE_HEADER },
344 { "cookie", STYLE_HEADER },
345 { "last-modified:", STYLE_HEADER },
346 { "pragma:", STYLE_HEADER },
347 { "server:", STYLE_HEADER },
348 { "etag:", STYLE_HEADER },
349 { "expires:", STYLE_HEADER },
350 { "warning:", STYLE_HEADER },
351 /* this is the terminator statement - do not delete! */
364 static CRITICAL_SECTION g_criticalsection;
365 static HWND g_hwndTray;
366 static HWND g_hwndLogBox;
367 static WNDPROC g_fnLogBox;
368 static HICON g_hiconAnim[ANIM_FRAMES];
369 static HICON g_hiconIdle;
370 static int g_nAnimFrame;
371 static BOOL g_bClipPending = FALSE;
372 static int g_nRichEditVersion = 0;
377 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
378 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
379 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
380 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
381 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
382 static BOOL InitRichEdit(void);
383 static void LogClipBuffer(void);
384 static void LogCreatePatternMatchingBuffers(void);
385 static void LogDestroyPatternMatchingBuffers(void);
386 static int LogPutStringNoMatch(const char *pszText, int style);
389 /*********************************************************************
391 * Function : InitLogWindow
393 * Description : Initialise the log window.
397 * Returns : Always TRUE (there should be error checking on the resources).
399 *********************************************************************/
400 BOOL InitLogWindow(void)
405 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
406 for (i = 0; i < ANIM_FRAMES; i++)
408 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
410 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
412 /* Create the user interface */
413 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
414 g_hwndTray = CreateTrayWindow(g_hInstance);
415 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
417 /* Create pattern matching buffers (for highlighting */
418 LogCreatePatternMatchingBuffers();
420 /* Create a critical section to protect multi-threaded access to certain things */
421 InitializeCriticalSection(&g_criticalsection);
428 /*********************************************************************
430 * Function : TermLogWindow
432 * Description : Cleanup the logwindow.
438 *********************************************************************/
439 void TermLogWindow(void)
443 LogDestroyPatternMatchingBuffers();
445 TrayDeleteIcon(g_hwndTray, 1);
446 DeleteObject(g_hiconApp);
447 DeleteObject(g_hiconIdle);
448 for (i = 0; i < ANIM_FRAMES; i++)
450 DeleteObject(g_hiconAnim[i]);
456 /*********************************************************************
458 * Function : LogCreatePatternMatchingBuffers
460 * Description : Compile the pattern matching buffers.
466 *********************************************************************/
467 void LogCreatePatternMatchingBuffers(void)
470 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
472 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
477 /*********************************************************************
479 * Function : LogDestroyPatternMatchingBuffers
481 * Description : Free up the pattern matching buffers.
487 *********************************************************************/
488 void LogDestroyPatternMatchingBuffers(void)
491 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
493 regfree(&patterns_to_highlight[i].buffer);
498 /*********************************************************************
500 * Function : LogGetURLUnderCursor
502 * Description : Returns the URL from under the cursor (remember to free it!).
506 * Returns : NULL or a pointer to an URL string.
508 *********************************************************************/
509 char *LogGetURLUnderCursor(void)
511 char *szResult = NULL;
516 DWORD nWordStart = 0;
519 regcomp(&re, RE_URL, REG_ICASE);
521 /* Get the position of the cursor over the text window */
522 GetCursorPos(&ptCursor);
523 ScreenToClient(g_hwndLogBox, &ptCursor);
527 /* Search backwards and fowards to obtain the word that is highlighted */
528 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
529 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
530 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
532 /* Compare the string to the pattern */
533 if (nWordEnd > nWordStart)
538 range.chrg.cpMin = nWordStart;
539 range.chrg.cpMax = nWordEnd;
540 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
541 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
543 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
545 szResult = range.lpstrText;
549 free(range.lpstrText);
559 /*********************************************************************
561 * Function : LogPutString
563 * Description : Inserts text into the logging window. This is really
564 * a regexp aware wrapper function to `LogPutStringNoMatch'.
567 * 1 : pszText = pointer to string going to the log window
569 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
570 * FIXME: this is backwards to the rest of IJB and to common
571 * programming practice. Please use 0 => success instead.
573 *********************************************************************/
574 int LogPutString(const char *pszText)
579 if (pszText == NULL || strlen(pszText) == 0)
589 /* Critical section stops multiple threads doing nasty interactions that
590 * foul up the highlighting and output.
592 EnterCriticalSection(&g_criticalsection);
594 if (g_bHighlightMessages)
598 /* First things first, regexp scan for various things that we would like highlighted */
599 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
601 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
603 char *pszBefore = NULL;
604 char *pszMatch = NULL;
605 char *pszAfter = NULL;
608 /* Split the string up into pieces representing the strings, before
609 at and after the matching pattern
613 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
614 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
615 strncpy(pszBefore, pszText, match.rm_so);
617 if (match.rm_eo < (regoff_t)strlen(pszText))
619 pszAfter = strdup(&pszText[match.rm_eo]);
621 nMatchSize = match.rm_eo - match.rm_so;
622 pszMatch = (char *)malloc(nMatchSize + 1);
623 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
624 pszMatch[nMatchSize] = '\0';
626 /* Recursively call LogPutString */
629 LogPutString(pszBefore);
634 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
639 LogPutString(pszAfter);
649 result = LogPutStringNoMatch(pszText, STYLE_NONE);
652 LeaveCriticalSection(&g_criticalsection);
659 /*********************************************************************
661 * Function : LogPutStringNoMatch
663 * Description : Puts a string into the logging window.
666 * 1 : pszText = pointer to string going to the log window
667 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
669 * Returns : Always 1 => success.
670 * FIXME: this is backwards to the rest of IJB and to common
671 * programming practice. Please use 0 => success instead.
673 *********************************************************************/
674 int LogPutStringNoMatch(const char *pszText, int style)
680 assert(g_hwndLogBox);
681 if (g_hwndLogBox == NULL)
686 /* TODO preserve existing selection */
688 /* Go to the end of the text */
689 nTextLength = GetWindowTextLength(g_hwndLogBox);
690 range.cpMin = nTextLength;
691 range.cpMax = nTextLength;
692 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
694 /* Apply a formatting style */
695 memset(&format, 0, sizeof(format));
696 format.cbSize = sizeof(format);
697 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
698 format.yHeight = (g_nFontSize * 1440) / 72;
699 strcpy(format.szFaceName, g_szFontFaceName);
700 if (style == STYLE_NONE)
703 format.dwEffects |= CFE_AUTOCOLOR;
705 else if (style == STYLE_HEADER)
707 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
709 else if (style == STYLE_HIGHLIGHT)
711 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
713 else if (style == STYLE_LINK)
715 format.dwEffects |= CFE_UNDERLINE;
716 format.crTextColor = RGB(0, 0, 255);
718 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
720 /* Append text to the end */
721 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
723 /* TODO Restore the old selection */
726 if (strchr(pszText, '\n') != NULL)
728 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
731 /* Set the force clip timer going. This timer ensures clipping is done
732 intermittently even when there is a sustained burst of logging
734 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
736 g_bClipPending = TRUE;
744 /*********************************************************************
746 * Function : LogShowActivity
748 * Description : Start the spinner.
754 *********************************************************************/
755 void LogShowActivity(void)
757 /* Start some activity timers */
758 if (g_bShowActivityAnimation)
760 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
761 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
767 /*********************************************************************
769 * Function : LogClipBuffer
771 * Description : Prunes old lines from the log.
777 *********************************************************************/
778 void LogClipBuffer(void)
780 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
781 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
783 /* Compute the range representing the lines to be deleted */
784 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
785 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
788 range.cpMax = nLastChar;
790 /* TODO get current selection */
792 /* TODO adjust and clip old selection against range to be deleted */
794 /* Select range and erase it (turning off autoscroll to prevent
796 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
797 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
798 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
799 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
801 /* reposition (back to) the end of the log content */
802 range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
804 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
806 /* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
807 SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
814 /*********************************************************************
816 * Function : CreateHiddenLogOwnerWindow
818 * Description : Creates a hidden owner window that stops the log
819 * window appearing in the task bar.
822 * 1 : hInstance = application's instance handle
824 * Returns : Handle to newly created window.
826 *********************************************************************/
827 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
829 static const char *szWndName = "PrivoxyLogOwner";
834 wc.lpfnWndProc = LogOwnerWindowProc;
837 wc.hInstance = hInstance;
840 wc.hbrBackground = 0;
842 wc.lpszClassName = szWndName;
846 hwnd = CreateWindow(szWndName, szWndName,
847 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
848 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
855 /*********************************************************************
857 * Function : LogOwnerWindowProc
859 * Description : Dummy procedure that does nothing special.
862 * 1 : hwnd = window handle
863 * 2 : uMsg = message number
864 * 3 : wParam = first param for this message
865 * 4 : lParam = next param for this message
867 * Returns : Same as `DefWindowProc'.
869 *********************************************************************/
870 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
872 return DefWindowProc(hwnd, uMsg, wParam, lParam);
877 /*********************************************************************
879 * Function : CreateLogWindow
881 * Description : Create the logging window.
884 * 1 : hInstance = application's instance handle
885 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
887 * Returns : Handle to newly created window.
889 *********************************************************************/
890 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
892 static const char *szWndName = "PrivoxyLogWindow";
893 static const char *szWndTitle = "Privoxy";
896 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
900 memset(&wc, 0, sizeof(wc));
901 wc.cbSize = sizeof(wc);
902 wc.style = CS_DBLCLKS;
903 wc.lpfnWndProc = LogWindowProc;
906 wc.hInstance = hInstance;
907 wc.hIcon = g_hiconApp;
909 wc.hbrBackground = 0;
910 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
911 wc.lpszClassName = szWndName;
912 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
913 RegisterClassEx(&wc);
915 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
916 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
917 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
919 /* Now create a child list box */
920 GetClientRect(hwnd, &rcClient);
922 /* Create a rich edit control */
924 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
925 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
926 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
927 hwnd, NULL, hInstance, NULL);
928 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
930 /* Subclass the control to catch certain messages */
931 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
932 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
934 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
935 if (!g_bShowOnTaskBar &&
936 (nCmdShow == SW_SHOWMINIMIZED ||
937 nCmdShow == SW_MINIMIZE ||
938 nCmdShow == SW_SHOWMINNOACTIVE))
940 g_bShowLogWindow = FALSE;
944 ShowWindow(hwnd, nCmdShow);
948 GetClientRect(g_hwndLogFrame, &rcClient);
949 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
956 /*********************************************************************
958 * Function : InitRichEdit
960 * Description : Initialise the rich edit control library.
964 * Returns : TRUE => success, FALSE => failure.
965 * FIXME: this is backwards to the rest of IJB and to common
966 * programming practice. Please use 0 => success instead.
968 *********************************************************************/
969 BOOL InitRichEdit(void)
971 static HINSTANCE hInstRichEdit;
972 if (hInstRichEdit == NULL)
974 g_nRichEditVersion = 0;
975 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
978 g_nRichEditVersion = _RICHEDIT_VER;
982 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
985 g_nRichEditVersion = 0x0100;
989 return (hInstRichEdit != NULL) ? TRUE : FALSE;
994 /*********************************************************************
996 * Function : ShowLogWindow
998 * Description : Shows or hides the log window. We will also raise the
999 * window on a show command in case it is buried.
1002 * 1 : bShow = TRUE to show, FALSE to mimize/hide
1006 *********************************************************************/
1007 void ShowLogWindow(BOOL bShow)
1011 SetForegroundWindow(g_hwndLogFrame);
1012 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1015 else if (g_bShowOnTaskBar)
1017 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1021 ShowWindow(g_hwndLogFrame, SW_HIDE);
1026 /*********************************************************************
1028 * Function : EditFile
1030 * Description : Opens the specified setting file for editing.
1031 * FIXME: What if the file has no associated application. Check for return values
1032 * from ShellExecute??
1035 * 1 : filename = filename from the config (aka config.txt) file.
1039 *********************************************************************/
1040 void EditFile(const char *filename)
1044 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1050 /*--------------------------------------------------------------------------*/
1051 /* Windows message handlers */
1052 /*--------------------------------------------------------------------------*/
1055 /*********************************************************************
1057 * Function : OnLogRButtonUp
1059 * Description : Handler for WM_RBUTTONUP messages.
1062 * 1 : nModifier = wParam from mouse message (unused)
1063 * 2 : x = x coordinate of the mouse event
1064 * 3 : y = y coordinate of the mouse event
1068 *********************************************************************/
1069 void OnLogRButtonUp(int nModifier, int x, int y)
1071 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1074 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1076 /* Check if there is a selection */
1078 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1079 if (range.cpMin == range.cpMax)
1081 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1085 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1088 /* Display the popup */
1089 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1096 /*********************************************************************
1098 * Function : OnLogCommand
1100 * Description : Handler for WM_COMMAND messages.
1103 * 1 : nCommand = the command portion of the menu selection event
1107 *********************************************************************/
1108 void OnLogCommand(int nCommand)
1112 case ID_TOGGLE_SHOWWINDOW:
1113 g_bShowLogWindow = !g_bShowLogWindow;
1115 ShowLogWindow(g_bShowLogWindow);
1119 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1123 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1126 case ID_VIEW_CLEARLOG:
1127 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1130 case ID_VIEW_LOGMESSAGES:
1131 g_bLogMessages = !g_bLogMessages;
1132 /* SaveLogSettings(); */
1135 case ID_VIEW_MESSAGEHIGHLIGHTING:
1136 g_bHighlightMessages = !g_bHighlightMessages;
1137 /* SaveLogSettings(); */
1140 case ID_VIEW_LIMITBUFFERSIZE:
1141 g_bLimitBufferSize = !g_bLimitBufferSize;
1142 /* SaveLogSettings(); */
1145 case ID_VIEW_ACTIVITYANIMATION:
1146 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1147 /* SaveLogSettings(); */
1150 #ifdef FEATURE_TOGGLE
1151 /* by haroon - change toggle to its opposite value */
1152 case ID_TOGGLE_ENABLED:
1153 global_toggle_state = !global_toggle_state;
1154 if (global_toggle_state)
1156 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1160 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1163 #endif /* def FEATURE_TOGGLE */
1165 case ID_TOOLS_EDITCONFIG:
1166 EditFile(configfile);
1169 case ID_TOOLS_EDITDEFAULTACTIONS:
1170 EditFile(g_default_actions_file);
1173 case ID_TOOLS_EDITUSERACTIONS:
1174 EditFile(g_user_actions_file);
1177 case ID_TOOLS_EDITFILTERS:
1178 EditFile(g_re_filterfile);
1181 #ifdef FEATURE_TRUST
1182 case ID_TOOLS_EDITTRUST:
1183 EditFile(g_trustfile);
1185 #endif /* def FEATURE_TRUST */
1188 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1192 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1195 case ID_HELP_MANUAL:
1196 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1199 case ID_HELP_STATUS:
1200 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1204 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1215 /*********************************************************************
1217 * Function : OnLogInitMenu
1219 * Description : Handler for WM_INITMENU messages. Enable, disable,
1220 * check, and/or uncheck menu options as apropos.
1223 * 1 : hmenu = handle to menu to "make current"
1227 *********************************************************************/
1228 void OnLogInitMenu(HMENU hmenu)
1230 /* Only enable editors if there is a file to edit */
1231 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1232 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1233 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1234 #ifdef FEATURE_TRUST
1235 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1236 #endif /* def FEATURE_TRUST */
1238 /* Check/uncheck options */
1239 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1240 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1241 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1242 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1243 #ifdef FEATURE_TOGGLE
1244 /* by haroon - menu item for Enable toggle on/off */
1245 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1246 #endif /* def FEATURE_TOGGLE */
1247 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1252 /*********************************************************************
1254 * Function : OnLogTimer
1256 * Description : Handler for WM_TIMER messages.
1259 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1263 *********************************************************************/
1264 void OnLogTimer(int nTimer)
1269 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1272 case TIMER_ANIMSTOP_ID:
1274 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1275 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1276 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1279 case TIMER_CLIPBUFFER_ID:
1280 case TIMER_CLIPBUFFER_FORCE_ID:
1282 g_bClipPending = FALSE;
1283 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1284 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1295 /*********************************************************************
1297 * Function : LogRichEditProc
1299 * Description : Window subclass routine handles some events for the rich edit control.
1302 * 1 : hwnd = window handle of the rich edit control
1303 * 2 : uMsg = message number
1304 * 3 : wParam = first param for this message
1305 * 4 : lParam = next param for this message
1307 * Returns : Appropriate M$ window message handler codes.
1309 *********************************************************************/
1310 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1317 pt.x = LOWORD(lParam);
1318 pt.y = HIWORD(lParam);
1319 ClientToScreen(hwnd, &pt);
1320 OnLogRButtonUp(wParam, pt.x, pt.y);
1325 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1327 OnLogCommand(ID_VIEW_CLEARLOG);
1332 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1337 /*********************************************************************
1339 * Function : LogWindowProc
1341 * Description : Windows call back routine handles events on the log window.
1344 * 1 : hwnd = handle of the logging window
1345 * 2 : uMsg = message number
1346 * 3 : wParam = first param for this message
1347 * 4 : lParam = next param for this message
1349 * Returns : Appropriate M$ window message handler codes.
1351 *********************************************************************/
1352 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1360 /* This is the end - beautiful friend - the end */
1361 DestroyWindow(g_hwndLogBox);
1362 DestroyWindow(g_hwndLogFrame);
1370 g_bShowLogWindow = wParam;
1372 /* Resize the logging window to fit the new frame */
1376 GetClientRect(g_hwndLogFrame, &rc);
1377 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1382 OnLogInitMenu((HMENU) wParam);
1390 OnLogCommand(LOWORD(wParam));
1397 if (g_bCloseHidesWindow)
1399 ShowLogWindow(FALSE);
1404 ShowLogWindow(FALSE);
1410 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1412 OnLogCommand(ID_VIEW_CLEARLOG);
1418 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1422 #endif /* ndef _WIN_CONSOLE - entire file */