1 const char w32log_rcs[] = "$Id: w32log.c,v 1.31 2009/03/07 17:58:02 fabiankeil 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-2009 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.31 2009/03/07 17:58:02 fabiankeil
36 * Fix two mingw32-only buffer overflows. Note that triggering
37 * them requires control over the configuration file in which
38 * case all bets are off anyway.
40 * Revision 1.30 2009/01/01 15:09:23 ler762
41 * Change the Windows taskbar icon when privoxy is toggled off.
43 * Revision 1.29 2008/12/20 15:27:40 ler762
44 * The crunch log message format changed, so update the strings to highlight
47 * Revision 1.28 2008/11/02 14:37:47 ler762
48 * commit the part of the patches I've been using that were written by torford and gjmurphy
49 * [ 1824315 ] Minor code cleanup
50 * [ 1781135 ] Patch - Add clear log, select all, and Accelerators for w32
51 * http://sourceforge.net/tracker/?func=detail&atid=311118&aid=1781135&group_id=11118
52 * The full patch adds control keys A(select all), C(copy) and D(delete all) to the
53 * Privoxy log window menu. Select all and copy work for me without the patch
54 * (albeit without showing the accelerator keys on the menu), so the only part of the
55 * patch I've been using for the last year or so has been the ctrl-d to delete
56 * everything in the Privoxy log window.
58 * Revision 1.27 2006/07/18 14:48:48 david__schmidt
59 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
60 * with what was really the latest development (the v_3_0_branch branch)
62 * Revision 1.25.2.5 2003/04/04 12:48:51 oes
64 * - Made tray menu correctly reflect initial window visibility state
65 * - Hopefully fixed problem where log window contents wasn't visible
66 * until vertical scroll bar was clicked. Thanks to Guy for the fix!
68 * Revision 1.25.2.4 2003/03/11 11:53:59 oes
69 * Cosmetic: Renamed cryptic variable
71 * Revision 1.25.2.3 2002/11/20 14:39:05 oes
72 * Fixed compiler warning
74 * Revision 1.25.2.2 2002/09/25 15:23:10 oes
75 * Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
77 * Revision 1.25.2.1 2002/08/21 17:59:05 oes
78 * - "Show Privoxy Window" now a toggle
79 * - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
81 * Revision 1.25 2002/04/04 00:36:36 gliptak
82 * always use pcre for matching
84 * Revision 1.24 2002/03/31 17:19:00 jongfoster
85 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
87 * Revision 1.23 2002/03/26 22:57:10 jongfoster
88 * Web server name should begin www.
90 * Revision 1.22 2002/03/24 12:48:23 jongfoster
93 * Revision 1.21 2002/03/24 12:07:35 jongfoster
94 * Consistern name for filters file
96 * Revision 1.20 2002/03/24 12:03:47 jongfoster
99 * Revision 1.19 2002/01/17 21:04:17 jongfoster
100 * Replacing hard references to the URL of the config interface
101 * with #defines from project.h
103 * Revision 1.18 2001/11/30 23:37:24 jongfoster
104 * Renaming the Win32 config file to config.txt - this is almost the
105 * same as the corresponding UNIX name "config"
107 * Revision 1.17 2001/11/16 00:46:31 jongfoster
108 * Fixing compiler warnings
110 * Revision 1.16 2001/08/01 19:58:12 jongfoster
111 * Fixing documentation filenames in help menu, and making status
112 * option work without needing the "Junkbuster Status.URL" file.
114 * Revision 1.15 2001/07/30 22:08:36 jongfoster
115 * Tidying up #defines:
116 * - All feature #defines are now of the form FEATURE_xxx
117 * - Permanently turned off WIN_GUI_EDIT
118 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
120 * Revision 1.14 2001/07/29 18:47:05 jongfoster
121 * Adding missing #include "loadcfg.h"
123 * Revision 1.13 2001/07/19 19:15:14 haroon
124 * - Added a FIXME for EditFile but didn't fix :-)
126 * Revision 1.12 2001/07/13 14:04:59 oes
127 * Removed all #ifdef PCRS
129 * Revision 1.11 2001/06/07 23:08:12 jongfoster
130 * Forward and ACL edit options removed.
132 * Revision 1.10 2001/05/31 21:37:11 jongfoster
133 * GUI changes to rename "permissions file" to "actions file".
135 * Revision 1.9 2001/05/31 17:33:13 oes
139 * Revision 1.8 2001/05/29 09:50:24 jongfoster
140 * Unified blocklist/imagelist/permissionslist.
141 * File format is still under discussion, but the internal changes
144 * Also modified interceptor behaviour:
145 * - We now intercept all URLs beginning with one of the following
146 * prefixes (and *only* these prefixes):
148 * * http://ijbswa.sf.net/config/
149 * * http://ijbswa.sourceforge.net/config/
150 * - New interceptors "home page" - go to http://i.j.b/ to see it.
151 * - Internal changes so that intercepted and fast redirect pages
152 * are not replaced with an image.
153 * - Interceptors now have the option to send a binary page direct
154 * to the client. (i.e. ijb-send-banner uses this)
155 * - Implemented show-url-info interceptor. (Which is why I needed
156 * the above interceptors changes - a typical URL is
157 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
158 * The previous mechanism would not have intercepted that, and
159 * if it had been intercepted then it then it would have replaced
162 * Revision 1.7 2001/05/26 01:26:34 jongfoster
163 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
164 * This #define cannot be set from ./configure - there's no point, it
165 * doesn't work yet. See feature request # 425722
167 * Revision 1.6 2001/05/26 00:31:30 jongfoster
168 * Fixing compiler warning about comparing signed/unsigned.
170 * Revision 1.5 2001/05/26 00:28:36 jongfoster
171 * Automatic reloading of config file.
172 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
173 * Most of the global variables have been moved to a new
174 * struct configuration_spec, accessed through csp->config->globalname
175 * Most of the globals remaining are used by the Win32 GUI.
177 * Revision 1.4 2001/05/22 18:56:28 oes
180 * Revision 1.3 2001/05/20 15:07:54 jongfoster
181 * File is now ignored if _WIN_CONSOLE is defined.
183 * Revision 1.2 2001/05/20 01:21:20 jongfoster
184 * Version 2.9.4 checkin.
185 * - Merged popupfile and cookiefile, and added control over PCRS
186 * filtering, in new "permissionsfile".
187 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
188 * file error you now get a message box (in the Win32 GUI) rather
189 * than the program exiting with no explanation.
190 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
192 * - Removed tabs from "config"
193 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
194 * - Bumped up version number.
196 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
197 * Initial import of version 2.9.3 source tree
200 *********************************************************************/
212 #include <richedit.h>
216 #include "w32taskbar.h"
220 #include "miscutil.h"
224 const char w32res_h_rcs[] = W32RES_H_VERSION;
228 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
231 const char w32log_h_rcs[] = W32LOG_H_VERSION;
233 #ifndef _WIN_CONSOLE /* entire file */
236 * Timers and the various durations
238 #define TIMER_ANIM_ID 1
239 #define TIMER_ANIM_TIME 100
240 #define TIMER_ANIMSTOP_ID 2
241 #define TIMER_ANIMSTOP_TIME 1000
242 #define TIMER_CLIPBUFFER_ID 3
243 #define TIMER_CLIPBUFFER_TIME 1000
244 #define TIMER_CLIPBUFFER_FORCE_ID 4
245 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
248 * Styles of text that can be output
251 #define STYLE_HIGHLIGHT 1
253 #define STYLE_HEADER 3
256 * Number of frames of animation in tray activity sequence
258 #define ANIM_FRAMES 8
260 #define DEFAULT_MAX_BUFFER_LINES 200
261 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
262 #define DEFAULT_LOG_FONT_SIZE 8
265 * These values affect the way the log window behaves, they should be read
266 * from a file but for the moment, they are hardcoded here. Some options are
267 * configurable through the UI.
270 /* Indicates whether task bar shows activity animation */
271 BOOL g_bShowActivityAnimation = 1;
273 /* Indicates whether the log window is shown */
274 BOOL g_bShowLogWindow = 1;
276 /* Indicates if the log window appears on the task bar */
277 BOOL g_bShowOnTaskBar = 0;
279 /* Indicates whether closing the log window really just hides it */
280 BOOL g_bCloseHidesWindow = 1;
282 /* Indicates if messages are logged at all */
283 BOOL g_bLogMessages = 1;
285 /* Indicates whether log messages are highlighted */
286 BOOL g_bHighlightMessages = 1;
288 /* Indicates if buffer is limited in size */
289 BOOL g_bLimitBufferSize = 1;
291 /* Maximum number of lines allowed in buffer when limited */
292 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
295 char g_szFontFaceName[32] = DEFAULT_LOG_FONT_NAME;
297 /* Size of font to use */
298 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
301 /* FIXME: this is a kludge */
303 const char * g_default_actions_file = NULL;
304 const char * g_user_actions_file = NULL;
305 const char * g_re_filterfile = NULL;
307 const char * g_trustfile = NULL;
308 #endif /* def FEATURE_TRUST */
310 /* FIXME: end kludge */
312 /* Regular expression for detected URLs */
313 #define RE_URL "http:[^ \n\r]*"
316 * Regular expressions that are used to perform highlight in the log window
318 static struct _Pattern
323 } patterns_to_highlight[] =
326 { RE_URL, STYLE_LINK },
327 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
328 /* interesting text to highlight */
329 /* see jcc.c crunch_reason for the full list */
330 { "Crunch: Blocked:", STYLE_HIGHLIGHT },
331 { "Crunch: Untrusted", STYLE_HIGHLIGHT },
332 { "Crunch: Redirected:", STYLE_HIGHLIGHT },
333 { "Crunch: DNS failure", STYLE_HIGHLIGHT },
334 { "Crunch: Forwarding failed", STYLE_HIGHLIGHT },
335 { "Crunch: Connection failure", STYLE_HIGHLIGHT },
336 { "Crunch: Out of memory", STYLE_HIGHLIGHT },
337 /* what are all the possible error strings?? */
338 { "Error:", STYLE_HIGHLIGHT },
340 { "referer:", STYLE_HEADER },
341 { "proxy-connection:", STYLE_HEADER },
342 { "proxy-agent:", STYLE_HEADER },
343 { "user-agent:", STYLE_HEADER },
344 { "host:", STYLE_HEADER },
345 { "accept:", STYLE_HEADER },
346 { "accept-encoding:", STYLE_HEADER },
347 { "accept-language:", STYLE_HEADER },
348 { "accept-charset:", STYLE_HEADER },
349 { "accept-ranges:", STYLE_HEADER },
350 { "date:", STYLE_HEADER },
351 { "cache-control:", STYLE_HEADER },
352 { "cache-last-checked:", STYLE_HEADER },
353 { "connection:", STYLE_HEADER },
354 { "content-type", STYLE_HEADER },
355 { "content-length", STYLE_HEADER },
356 { "cookie", STYLE_HEADER },
357 { "last-modified:", STYLE_HEADER },
358 { "pragma:", STYLE_HEADER },
359 { "server:", STYLE_HEADER },
360 { "etag:", STYLE_HEADER },
361 { "expires:", STYLE_HEADER },
362 { "warning:", STYLE_HEADER },
363 /* this is the terminator statement - do not delete! */
376 static CRITICAL_SECTION g_criticalsection;
377 static HWND g_hwndTray;
378 static HWND g_hwndLogBox;
379 static WNDPROC g_fnLogBox;
380 static HICON g_hiconAnim[ANIM_FRAMES];
381 static HICON g_hiconIdle;
382 static HICON g_hiconOff;
383 static int g_nAnimFrame;
384 static BOOL g_bClipPending = FALSE;
385 static int g_nRichEditVersion = 0;
390 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
391 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
392 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
393 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
394 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
395 static BOOL InitRichEdit(void);
396 static void LogClipBuffer(void);
397 static void LogCreatePatternMatchingBuffers(void);
398 static void LogDestroyPatternMatchingBuffers(void);
399 static int LogPutStringNoMatch(const char *pszText, int style);
400 static void SetIdleIcon(void);
403 /*********************************************************************
405 * Function : InitLogWindow
407 * Description : Initialise the log window.
411 * Returns : Always TRUE (there should be error checking on the resources).
413 *********************************************************************/
414 BOOL InitLogWindow(void)
419 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
420 g_hiconOff = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_OFF));
421 for (i = 0; i < ANIM_FRAMES; i++)
423 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
425 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
427 /* Create the user interface */
428 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
429 g_hwndTray = CreateTrayWindow(g_hInstance);
430 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
432 /* Create pattern matching buffers (for highlighting */
433 LogCreatePatternMatchingBuffers();
435 /* Create a critical section to protect multi-threaded access to certain things */
436 InitializeCriticalSection(&g_criticalsection);
443 /*********************************************************************
445 * Function : TermLogWindow
447 * Description : Cleanup the logwindow.
453 *********************************************************************/
454 void TermLogWindow(void)
458 LogDestroyPatternMatchingBuffers();
460 TrayDeleteIcon(g_hwndTray, 1);
461 DeleteObject(g_hiconApp);
462 DeleteObject(g_hiconIdle);
463 DeleteObject(g_hiconOff);
464 for (i = 0; i < ANIM_FRAMES; i++)
466 DeleteObject(g_hiconAnim[i]);
472 /*********************************************************************
474 * Function : LogCreatePatternMatchingBuffers
476 * Description : Compile the pattern matching buffers.
482 *********************************************************************/
483 void LogCreatePatternMatchingBuffers(void)
486 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
488 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
493 /*********************************************************************
495 * Function : LogDestroyPatternMatchingBuffers
497 * Description : Free up the pattern matching buffers.
503 *********************************************************************/
504 void LogDestroyPatternMatchingBuffers(void)
507 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
509 regfree(&patterns_to_highlight[i].buffer);
514 /*********************************************************************
516 * Function : LogGetURLUnderCursor
518 * Description : Returns the URL from under the cursor (remember to free it!).
522 * Returns : NULL or a pointer to an URL string.
524 *********************************************************************/
525 char *LogGetURLUnderCursor(void)
527 char *szResult = NULL;
532 DWORD nWordStart = 0;
535 regcomp(&re, RE_URL, REG_ICASE);
537 /* Get the position of the cursor over the text window */
538 GetCursorPos(&ptCursor);
539 ScreenToClient(g_hwndLogBox, &ptCursor);
543 /* Search backwards and fowards to obtain the word that is highlighted */
544 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
545 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
546 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
548 /* Compare the string to the pattern */
549 if (nWordEnd > nWordStart)
554 range.chrg.cpMin = nWordStart;
555 range.chrg.cpMax = nWordEnd;
556 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
557 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
559 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
561 szResult = range.lpstrText;
565 free(range.lpstrText);
575 /*********************************************************************
577 * Function : LogPutString
579 * Description : Inserts text into the logging window. This is really
580 * a regexp aware wrapper function to `LogPutStringNoMatch'.
583 * 1 : pszText = pointer to string going to the log window
585 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
586 * FIXME: this is backwards to the rest of IJB and to common
587 * programming practice. Please use 0 => success instead.
589 *********************************************************************/
590 int LogPutString(const char *pszText)
595 if (pszText == NULL || strlen(pszText) == 0)
605 /* Critical section stops multiple threads doing nasty interactions that
606 * foul up the highlighting and output.
608 EnterCriticalSection(&g_criticalsection);
610 if (g_bHighlightMessages)
614 /* First things first, regexp scan for various things that we would like highlighted */
615 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
617 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
619 char *pszBefore = NULL;
620 char *pszMatch = NULL;
621 char *pszAfter = NULL;
624 /* Split the string up into pieces representing the strings, before
625 at and after the matching pattern
629 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
630 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
631 strncpy(pszBefore, pszText, match.rm_so);
633 if (match.rm_eo < (regoff_t)strlen(pszText))
635 pszAfter = strdup(&pszText[match.rm_eo]);
637 nMatchSize = match.rm_eo - match.rm_so;
638 pszMatch = (char *)malloc(nMatchSize + 1);
639 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
640 pszMatch[nMatchSize] = '\0';
642 /* Recursively call LogPutString */
645 LogPutString(pszBefore);
650 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
655 LogPutString(pszAfter);
665 result = LogPutStringNoMatch(pszText, STYLE_NONE);
668 LeaveCriticalSection(&g_criticalsection);
675 /*********************************************************************
677 * Function : LogPutStringNoMatch
679 * Description : Puts a string into the logging window.
682 * 1 : pszText = pointer to string going to the log window
683 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
685 * Returns : Always 1 => success.
686 * FIXME: this is backwards to the rest of IJB and to common
687 * programming practice. Please use 0 => success instead.
689 *********************************************************************/
690 int LogPutStringNoMatch(const char *pszText, int style)
696 assert(g_hwndLogBox);
697 if (g_hwndLogBox == NULL)
702 /* TODO preserve existing selection */
704 /* Go to the end of the text */
705 nTextLength = GetWindowTextLength(g_hwndLogBox);
706 range.cpMin = nTextLength;
707 range.cpMax = nTextLength;
708 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
710 /* Apply a formatting style */
711 memset(&format, 0, sizeof(format));
712 format.cbSize = sizeof(format);
713 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
714 format.yHeight = (g_nFontSize * 1440) / 72;
715 strlcpy(format.szFaceName, g_szFontFaceName, sizeof(format.szFaceName));
716 if (style == STYLE_NONE)
719 format.dwEffects |= CFE_AUTOCOLOR;
721 else if (style == STYLE_HEADER)
723 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
725 else if (style == STYLE_HIGHLIGHT)
727 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
729 else if (style == STYLE_LINK)
731 format.dwEffects |= CFE_UNDERLINE;
732 format.crTextColor = RGB(0, 0, 255);
734 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
736 /* Append text to the end */
737 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
739 /* TODO Restore the old selection */
742 if (strchr(pszText, '\n') != NULL)
744 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
747 /* Set the force clip timer going. This timer ensures clipping is done
748 intermittently even when there is a sustained burst of logging
750 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
752 g_bClipPending = TRUE;
760 /*********************************************************************
762 * Function : LogShowActivity
764 * Description : Start the spinner.
770 *********************************************************************/
771 void LogShowActivity(void)
773 /* Start some activity timers */
774 if (g_bShowActivityAnimation)
776 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
777 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
783 /*********************************************************************
785 * Function : LogClipBuffer
787 * Description : Prunes old lines from the log.
793 *********************************************************************/
794 void LogClipBuffer(void)
796 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
797 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
799 /* Compute the range representing the lines to be deleted */
800 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
801 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
804 range.cpMax = nLastChar;
806 /* TODO get current selection */
808 /* TODO adjust and clip old selection against range to be deleted */
810 /* Select range and erase it (turning off autoscroll to prevent
812 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
813 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
814 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
815 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
817 /* reposition (back to) the end of the log content */
818 range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
820 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
822 /* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
823 SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
830 /*********************************************************************
832 * Function : CreateHiddenLogOwnerWindow
834 * Description : Creates a hidden owner window that stops the log
835 * window appearing in the task bar.
838 * 1 : hInstance = application's instance handle
840 * Returns : Handle to newly created window.
842 *********************************************************************/
843 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
845 static const char *szWndName = "PrivoxyLogOwner";
850 wc.lpfnWndProc = LogOwnerWindowProc;
853 wc.hInstance = hInstance;
854 wc.hIcon = g_hiconApp;
856 wc.hbrBackground = 0;
858 wc.lpszClassName = szWndName;
862 hwnd = CreateWindow(szWndName, szWndName,
863 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
864 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
871 /*********************************************************************
873 * Function : LogOwnerWindowProc
875 * Description : Dummy procedure that does nothing special.
878 * 1 : hwnd = window handle
879 * 2 : uMsg = message number
880 * 3 : wParam = first param for this message
881 * 4 : lParam = next param for this message
883 * Returns : Same as `DefWindowProc'.
885 *********************************************************************/
886 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
888 return DefWindowProc(hwnd, uMsg, wParam, lParam);
893 /*********************************************************************
895 * Function : CreateLogWindow
897 * Description : Create the logging window.
900 * 1 : hInstance = application's instance handle
901 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
903 * Returns : Handle to newly created window.
905 *********************************************************************/
906 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
908 static const char *szWndName = "PrivoxyLogWindow";
909 static const char *szWndTitle = "Privoxy";
912 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
916 memset(&wc, 0, sizeof(wc));
917 wc.cbSize = sizeof(wc);
918 wc.style = CS_DBLCLKS;
919 wc.lpfnWndProc = LogWindowProc;
922 wc.hInstance = hInstance;
923 wc.hIcon = g_hiconApp;
925 wc.hbrBackground = 0;
926 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
927 wc.lpszClassName = szWndName;
928 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
929 RegisterClassEx(&wc);
931 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
932 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
933 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
935 /* Now create a child list box */
936 GetClientRect(hwnd, &rcClient);
938 /* Create a rich edit control */
940 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
941 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
942 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
943 hwnd, NULL, hInstance, NULL);
944 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
946 /* Subclass the control to catch certain messages */
947 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
948 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
950 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
951 if (!g_bShowOnTaskBar &&
952 (nCmdShow == SW_SHOWMINIMIZED ||
953 nCmdShow == SW_MINIMIZE ||
954 nCmdShow == SW_SHOWMINNOACTIVE))
956 g_bShowLogWindow = FALSE;
960 ShowWindow(hwnd, nCmdShow);
964 GetClientRect(g_hwndLogFrame, &rcClient);
965 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
972 /*********************************************************************
974 * Function : InitRichEdit
976 * Description : Initialise the rich edit control library.
980 * Returns : TRUE => success, FALSE => failure.
981 * FIXME: this is backwards to the rest of IJB and to common
982 * programming practice. Please use 0 => success instead.
984 *********************************************************************/
985 BOOL InitRichEdit(void)
987 static HINSTANCE hInstRichEdit;
988 if (hInstRichEdit == NULL)
990 g_nRichEditVersion = 0;
991 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
994 g_nRichEditVersion = _RICHEDIT_VER;
998 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
1001 g_nRichEditVersion = 0x0100;
1005 return (hInstRichEdit != NULL) ? TRUE : FALSE;
1010 /*********************************************************************
1012 * Function : ShowLogWindow
1014 * Description : Shows or hides the log window. We will also raise the
1015 * window on a show command in case it is buried.
1018 * 1 : bShow = TRUE to show, FALSE to mimize/hide
1022 *********************************************************************/
1023 void ShowLogWindow(BOOL bShow)
1027 SetForegroundWindow(g_hwndLogFrame);
1028 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1031 else if (g_bShowOnTaskBar)
1033 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1037 ShowWindow(g_hwndLogFrame, SW_HIDE);
1042 /*********************************************************************
1044 * Function : EditFile
1046 * Description : Opens the specified setting file for editing.
1047 * FIXME: What if the file has no associated application. Check for return values
1048 * from ShellExecute??
1051 * 1 : filename = filename from the config (aka config.txt) file.
1055 *********************************************************************/
1056 void EditFile(const char *filename)
1060 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1066 /*--------------------------------------------------------------------------*/
1067 /* Windows message handlers */
1068 /*--------------------------------------------------------------------------*/
1071 /*********************************************************************
1073 * Function : OnLogRButtonUp
1075 * Description : Handler for WM_RBUTTONUP messages.
1078 * 1 : nModifier = wParam from mouse message (unused)
1079 * 2 : x = x coordinate of the mouse event
1080 * 3 : y = y coordinate of the mouse event
1084 *********************************************************************/
1085 void OnLogRButtonUp(int nModifier, int x, int y)
1087 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1090 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1092 /* Check if there is a selection */
1094 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1095 if (range.cpMin == range.cpMax)
1097 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1101 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1104 /* Display the popup */
1105 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1112 /*********************************************************************
1114 * Function : OnLogCommand
1116 * Description : Handler for WM_COMMAND messages.
1119 * 1 : nCommand = the command portion of the menu selection event
1123 *********************************************************************/
1124 void OnLogCommand(int nCommand)
1128 case ID_TOGGLE_SHOWWINDOW:
1129 g_bShowLogWindow = !g_bShowLogWindow;
1131 ShowLogWindow(g_bShowLogWindow);
1135 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1139 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1142 case ID_VIEW_CLEARLOG:
1143 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1146 case ID_VIEW_LOGMESSAGES:
1147 g_bLogMessages = !g_bLogMessages;
1148 /* SaveLogSettings(); */
1151 case ID_VIEW_MESSAGEHIGHLIGHTING:
1152 g_bHighlightMessages = !g_bHighlightMessages;
1153 /* SaveLogSettings(); */
1156 case ID_VIEW_LIMITBUFFERSIZE:
1157 g_bLimitBufferSize = !g_bLimitBufferSize;
1158 /* SaveLogSettings(); */
1161 case ID_VIEW_ACTIVITYANIMATION:
1162 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1163 /* SaveLogSettings(); */
1166 #ifdef FEATURE_TOGGLE
1167 /* by haroon - change toggle to its opposite value */
1168 case ID_TOGGLE_ENABLED:
1169 global_toggle_state = !global_toggle_state;
1170 if (global_toggle_state)
1172 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1176 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1180 #endif /* def FEATURE_TOGGLE */
1182 case ID_TOOLS_EDITCONFIG:
1183 EditFile(configfile);
1186 case ID_TOOLS_EDITDEFAULTACTIONS:
1187 EditFile(g_default_actions_file);
1190 case ID_TOOLS_EDITUSERACTIONS:
1191 EditFile(g_user_actions_file);
1194 case ID_TOOLS_EDITFILTERS:
1195 EditFile(g_re_filterfile);
1198 #ifdef FEATURE_TRUST
1199 case ID_TOOLS_EDITTRUST:
1200 EditFile(g_trustfile);
1202 #endif /* def FEATURE_TRUST */
1205 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1209 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1212 case ID_HELP_MANUAL:
1213 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1216 case ID_HELP_STATUS:
1217 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1221 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1232 /*********************************************************************
1234 * Function : OnLogInitMenu
1236 * Description : Handler for WM_INITMENU messages. Enable, disable,
1237 * check, and/or uncheck menu options as apropos.
1240 * 1 : hmenu = handle to menu to "make current"
1244 *********************************************************************/
1245 void OnLogInitMenu(HMENU hmenu)
1247 /* Only enable editors if there is a file to edit */
1248 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1249 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1250 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1251 #ifdef FEATURE_TRUST
1252 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1253 #endif /* def FEATURE_TRUST */
1255 /* Check/uncheck options */
1256 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1257 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1258 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1259 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1260 #ifdef FEATURE_TOGGLE
1261 /* by haroon - menu item for Enable toggle on/off */
1262 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1263 #endif /* def FEATURE_TOGGLE */
1264 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1269 /*********************************************************************
1271 * Function : OnLogTimer
1273 * Description : Handler for WM_TIMER messages.
1276 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1280 *********************************************************************/
1281 void OnLogTimer(int nTimer)
1286 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1289 case TIMER_ANIMSTOP_ID:
1292 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1293 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1296 case TIMER_CLIPBUFFER_ID:
1297 case TIMER_CLIPBUFFER_FORCE_ID:
1299 g_bClipPending = FALSE;
1300 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1301 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1312 /*********************************************************************
1314 * Function : SetIdleIcon
1316 * Description : Sets the tray icon to either idle or off
1322 *********************************************************************/
1325 #ifdef FEATURE_TOGGLE
1326 if (!global_toggle_state)
1328 TraySetIcon(g_hwndTray, 1, g_hiconOff);
1329 /* log_error(LOG_LEVEL_INFO, "Privoxy OFF icon selected."); */
1332 #endif /* def FEATURE_TOGGLE */
1333 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1337 /*********************************************************************
1339 * Function : LogRichEditProc
1341 * Description : Window subclass routine handles some events for the rich edit control.
1344 * 1 : hwnd = window handle of the rich edit control
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 LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1359 pt.x = LOWORD(lParam);
1360 pt.y = HIWORD(lParam);
1361 ClientToScreen(hwnd, &pt);
1362 OnLogRButtonUp(wParam, pt.x, pt.y);
1367 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1369 OnLogCommand(ID_VIEW_CLEARLOG);
1374 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1379 /*********************************************************************
1381 * Function : LogWindowProc
1383 * Description : Windows call back routine handles events on the log window.
1386 * 1 : hwnd = handle of the logging window
1387 * 2 : uMsg = message number
1388 * 3 : wParam = first param for this message
1389 * 4 : lParam = next param for this message
1391 * Returns : Appropriate M$ window message handler codes.
1393 *********************************************************************/
1394 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1402 /* This is the end - beautiful friend - the end */
1403 DestroyWindow(g_hwndLogBox);
1404 DestroyWindow(g_hwndLogFrame);
1412 g_bShowLogWindow = wParam;
1414 /* Resize the logging window to fit the new frame */
1418 GetClientRect(g_hwndLogFrame, &rc);
1419 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1424 OnLogInitMenu((HMENU) wParam);
1432 OnLogCommand(LOWORD(wParam));
1439 if (g_bCloseHidesWindow)
1441 ShowLogWindow(FALSE);
1446 ShowLogWindow(FALSE);
1452 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1454 OnLogCommand(ID_VIEW_CLEARLOG);
1460 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1464 #endif /* ndef _WIN_CONSOLE - entire file */