1 const char w32log_rcs[] = "$Id: w32log.c,v 1.30 2009/01/01 15:09:23 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-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.30 2009/01/01 15:09:23 ler762
36 * Change the Windows taskbar icon when privoxy is toggled off.
38 * Revision 1.29 2008/12/20 15:27:40 ler762
39 * The crunch log message format changed, so update the strings to highlight
42 * Revision 1.28 2008/11/02 14:37:47 ler762
43 * commit the part of the patches I've been using that were written by torford and gjmurphy
44 * [ 1824315 ] Minor code cleanup
45 * [ 1781135 ] Patch - Add clear log, select all, and Accelerators for w32
46 * http://sourceforge.net/tracker/?func=detail&atid=311118&aid=1781135&group_id=11118
47 * The full patch adds control keys A(select all), C(copy) and D(delete all) to the
48 * Privoxy log window menu. Select all and copy work for me without the patch
49 * (albeit without showing the accelerator keys on the menu), so the only part of the
50 * patch I've been using for the last year or so has been the ctrl-d to delete
51 * everything in the Privoxy log window.
53 * Revision 1.27 2006/07/18 14:48:48 david__schmidt
54 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
55 * with what was really the latest development (the v_3_0_branch branch)
57 * Revision 1.25.2.5 2003/04/04 12:48:51 oes
59 * - Made tray menu correctly reflect initial window visibility state
60 * - Hopefully fixed problem where log window contents wasn't visible
61 * until vertical scroll bar was clicked. Thanks to Guy for the fix!
63 * Revision 1.25.2.4 2003/03/11 11:53:59 oes
64 * Cosmetic: Renamed cryptic variable
66 * Revision 1.25.2.3 2002/11/20 14:39:05 oes
67 * Fixed compiler warning
69 * Revision 1.25.2.2 2002/09/25 15:23:10 oes
70 * Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
72 * Revision 1.25.2.1 2002/08/21 17:59:05 oes
73 * - "Show Privoxy Window" now a toggle
74 * - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
76 * Revision 1.25 2002/04/04 00:36:36 gliptak
77 * always use pcre for matching
79 * Revision 1.24 2002/03/31 17:19:00 jongfoster
80 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
82 * Revision 1.23 2002/03/26 22:57:10 jongfoster
83 * Web server name should begin www.
85 * Revision 1.22 2002/03/24 12:48:23 jongfoster
88 * Revision 1.21 2002/03/24 12:07:35 jongfoster
89 * Consistern name for filters file
91 * Revision 1.20 2002/03/24 12:03:47 jongfoster
94 * Revision 1.19 2002/01/17 21:04:17 jongfoster
95 * Replacing hard references to the URL of the config interface
96 * with #defines from project.h
98 * Revision 1.18 2001/11/30 23:37:24 jongfoster
99 * Renaming the Win32 config file to config.txt - this is almost the
100 * same as the corresponding UNIX name "config"
102 * Revision 1.17 2001/11/16 00:46:31 jongfoster
103 * Fixing compiler warnings
105 * Revision 1.16 2001/08/01 19:58:12 jongfoster
106 * Fixing documentation filenames in help menu, and making status
107 * option work without needing the "Junkbuster Status.URL" file.
109 * Revision 1.15 2001/07/30 22:08:36 jongfoster
110 * Tidying up #defines:
111 * - All feature #defines are now of the form FEATURE_xxx
112 * - Permanently turned off WIN_GUI_EDIT
113 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
115 * Revision 1.14 2001/07/29 18:47:05 jongfoster
116 * Adding missing #include "loadcfg.h"
118 * Revision 1.13 2001/07/19 19:15:14 haroon
119 * - Added a FIXME for EditFile but didn't fix :-)
121 * Revision 1.12 2001/07/13 14:04:59 oes
122 * Removed all #ifdef PCRS
124 * Revision 1.11 2001/06/07 23:08:12 jongfoster
125 * Forward and ACL edit options removed.
127 * Revision 1.10 2001/05/31 21:37:11 jongfoster
128 * GUI changes to rename "permissions file" to "actions file".
130 * Revision 1.9 2001/05/31 17:33:13 oes
134 * Revision 1.8 2001/05/29 09:50:24 jongfoster
135 * Unified blocklist/imagelist/permissionslist.
136 * File format is still under discussion, but the internal changes
139 * Also modified interceptor behaviour:
140 * - We now intercept all URLs beginning with one of the following
141 * prefixes (and *only* these prefixes):
143 * * http://ijbswa.sf.net/config/
144 * * http://ijbswa.sourceforge.net/config/
145 * - New interceptors "home page" - go to http://i.j.b/ to see it.
146 * - Internal changes so that intercepted and fast redirect pages
147 * are not replaced with an image.
148 * - Interceptors now have the option to send a binary page direct
149 * to the client. (i.e. ijb-send-banner uses this)
150 * - Implemented show-url-info interceptor. (Which is why I needed
151 * the above interceptors changes - a typical URL is
152 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
153 * The previous mechanism would not have intercepted that, and
154 * if it had been intercepted then it then it would have replaced
157 * Revision 1.7 2001/05/26 01:26:34 jongfoster
158 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
159 * This #define cannot be set from ./configure - there's no point, it
160 * doesn't work yet. See feature request # 425722
162 * Revision 1.6 2001/05/26 00:31:30 jongfoster
163 * Fixing compiler warning about comparing signed/unsigned.
165 * Revision 1.5 2001/05/26 00:28:36 jongfoster
166 * Automatic reloading of config file.
167 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
168 * Most of the global variables have been moved to a new
169 * struct configuration_spec, accessed through csp->config->globalname
170 * Most of the globals remaining are used by the Win32 GUI.
172 * Revision 1.4 2001/05/22 18:56:28 oes
175 * Revision 1.3 2001/05/20 15:07:54 jongfoster
176 * File is now ignored if _WIN_CONSOLE is defined.
178 * Revision 1.2 2001/05/20 01:21:20 jongfoster
179 * Version 2.9.4 checkin.
180 * - Merged popupfile and cookiefile, and added control over PCRS
181 * filtering, in new "permissionsfile".
182 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
183 * file error you now get a message box (in the Win32 GUI) rather
184 * than the program exiting with no explanation.
185 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
187 * - Removed tabs from "config"
188 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
189 * - Bumped up version number.
191 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
192 * Initial import of version 2.9.3 source tree
195 *********************************************************************/
207 #include <richedit.h>
211 #include "w32taskbar.h"
215 #include "miscutil.h"
219 const char w32res_h_rcs[] = W32RES_H_VERSION;
223 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
226 const char w32log_h_rcs[] = W32LOG_H_VERSION;
228 #ifndef _WIN_CONSOLE /* entire file */
231 * Timers and the various durations
233 #define TIMER_ANIM_ID 1
234 #define TIMER_ANIM_TIME 100
235 #define TIMER_ANIMSTOP_ID 2
236 #define TIMER_ANIMSTOP_TIME 1000
237 #define TIMER_CLIPBUFFER_ID 3
238 #define TIMER_CLIPBUFFER_TIME 1000
239 #define TIMER_CLIPBUFFER_FORCE_ID 4
240 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
243 * Styles of text that can be output
246 #define STYLE_HIGHLIGHT 1
248 #define STYLE_HEADER 3
251 * Number of frames of animation in tray activity sequence
253 #define ANIM_FRAMES 8
255 #define DEFAULT_MAX_BUFFER_LINES 200
256 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
257 #define DEFAULT_LOG_FONT_SIZE 8
260 * These values affect the way the log window behaves, they should be read
261 * from a file but for the moment, they are hardcoded here. Some options are
262 * configurable through the UI.
265 /* Indicates whether task bar shows activity animation */
266 BOOL g_bShowActivityAnimation = 1;
268 /* Indicates whether the log window is shown */
269 BOOL g_bShowLogWindow = 1;
271 /* Indicates if the log window appears on the task bar */
272 BOOL g_bShowOnTaskBar = 0;
274 /* Indicates whether closing the log window really just hides it */
275 BOOL g_bCloseHidesWindow = 1;
277 /* Indicates if messages are logged at all */
278 BOOL g_bLogMessages = 1;
280 /* Indicates whether log messages are highlighted */
281 BOOL g_bHighlightMessages = 1;
283 /* Indicates if buffer is limited in size */
284 BOOL g_bLimitBufferSize = 1;
286 /* Maximum number of lines allowed in buffer when limited */
287 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
290 char g_szFontFaceName[32] = DEFAULT_LOG_FONT_NAME;
292 /* Size of font to use */
293 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
296 /* FIXME: this is a kludge */
298 const char * g_default_actions_file = NULL;
299 const char * g_user_actions_file = NULL;
300 const char * g_re_filterfile = NULL;
302 const char * g_trustfile = NULL;
303 #endif /* def FEATURE_TRUST */
305 /* FIXME: end kludge */
307 /* Regular expression for detected URLs */
308 #define RE_URL "http:[^ \n\r]*"
311 * Regular expressions that are used to perform highlight in the log window
313 static struct _Pattern
318 } patterns_to_highlight[] =
321 { RE_URL, STYLE_LINK },
322 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
323 /* interesting text to highlight */
324 /* see jcc.c crunch_reason for the full list */
325 { "Crunch: Blocked:", STYLE_HIGHLIGHT },
326 { "Crunch: Untrusted", STYLE_HIGHLIGHT },
327 { "Crunch: Redirected:", STYLE_HIGHLIGHT },
328 { "Crunch: DNS failure", STYLE_HIGHLIGHT },
329 { "Crunch: Forwarding failed", STYLE_HIGHLIGHT },
330 { "Crunch: Connection failure", STYLE_HIGHLIGHT },
331 { "Crunch: Out of memory", STYLE_HIGHLIGHT },
332 /* what are all the possible error strings?? */
333 { "Error:", STYLE_HIGHLIGHT },
335 { "referer:", STYLE_HEADER },
336 { "proxy-connection:", STYLE_HEADER },
337 { "proxy-agent:", STYLE_HEADER },
338 { "user-agent:", STYLE_HEADER },
339 { "host:", STYLE_HEADER },
340 { "accept:", STYLE_HEADER },
341 { "accept-encoding:", STYLE_HEADER },
342 { "accept-language:", STYLE_HEADER },
343 { "accept-charset:", STYLE_HEADER },
344 { "accept-ranges:", STYLE_HEADER },
345 { "date:", STYLE_HEADER },
346 { "cache-control:", STYLE_HEADER },
347 { "cache-last-checked:", STYLE_HEADER },
348 { "connection:", STYLE_HEADER },
349 { "content-type", STYLE_HEADER },
350 { "content-length", STYLE_HEADER },
351 { "cookie", STYLE_HEADER },
352 { "last-modified:", STYLE_HEADER },
353 { "pragma:", STYLE_HEADER },
354 { "server:", STYLE_HEADER },
355 { "etag:", STYLE_HEADER },
356 { "expires:", STYLE_HEADER },
357 { "warning:", STYLE_HEADER },
358 /* this is the terminator statement - do not delete! */
371 static CRITICAL_SECTION g_criticalsection;
372 static HWND g_hwndTray;
373 static HWND g_hwndLogBox;
374 static WNDPROC g_fnLogBox;
375 static HICON g_hiconAnim[ANIM_FRAMES];
376 static HICON g_hiconIdle;
377 static HICON g_hiconOff;
378 static int g_nAnimFrame;
379 static BOOL g_bClipPending = FALSE;
380 static int g_nRichEditVersion = 0;
385 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
386 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
387 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
388 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
389 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
390 static BOOL InitRichEdit(void);
391 static void LogClipBuffer(void);
392 static void LogCreatePatternMatchingBuffers(void);
393 static void LogDestroyPatternMatchingBuffers(void);
394 static int LogPutStringNoMatch(const char *pszText, int style);
395 static void SetIdleIcon(void);
398 /*********************************************************************
400 * Function : InitLogWindow
402 * Description : Initialise the log window.
406 * Returns : Always TRUE (there should be error checking on the resources).
408 *********************************************************************/
409 BOOL InitLogWindow(void)
414 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
415 g_hiconOff = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_OFF));
416 for (i = 0; i < ANIM_FRAMES; i++)
418 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
420 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
422 /* Create the user interface */
423 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
424 g_hwndTray = CreateTrayWindow(g_hInstance);
425 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
427 /* Create pattern matching buffers (for highlighting */
428 LogCreatePatternMatchingBuffers();
430 /* Create a critical section to protect multi-threaded access to certain things */
431 InitializeCriticalSection(&g_criticalsection);
438 /*********************************************************************
440 * Function : TermLogWindow
442 * Description : Cleanup the logwindow.
448 *********************************************************************/
449 void TermLogWindow(void)
453 LogDestroyPatternMatchingBuffers();
455 TrayDeleteIcon(g_hwndTray, 1);
456 DeleteObject(g_hiconApp);
457 DeleteObject(g_hiconIdle);
458 DeleteObject(g_hiconOff);
459 for (i = 0; i < ANIM_FRAMES; i++)
461 DeleteObject(g_hiconAnim[i]);
467 /*********************************************************************
469 * Function : LogCreatePatternMatchingBuffers
471 * Description : Compile the pattern matching buffers.
477 *********************************************************************/
478 void LogCreatePatternMatchingBuffers(void)
481 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
483 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
488 /*********************************************************************
490 * Function : LogDestroyPatternMatchingBuffers
492 * Description : Free up the pattern matching buffers.
498 *********************************************************************/
499 void LogDestroyPatternMatchingBuffers(void)
502 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
504 regfree(&patterns_to_highlight[i].buffer);
509 /*********************************************************************
511 * Function : LogGetURLUnderCursor
513 * Description : Returns the URL from under the cursor (remember to free it!).
517 * Returns : NULL or a pointer to an URL string.
519 *********************************************************************/
520 char *LogGetURLUnderCursor(void)
522 char *szResult = NULL;
527 DWORD nWordStart = 0;
530 regcomp(&re, RE_URL, REG_ICASE);
532 /* Get the position of the cursor over the text window */
533 GetCursorPos(&ptCursor);
534 ScreenToClient(g_hwndLogBox, &ptCursor);
538 /* Search backwards and fowards to obtain the word that is highlighted */
539 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
540 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
541 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
543 /* Compare the string to the pattern */
544 if (nWordEnd > nWordStart)
549 range.chrg.cpMin = nWordStart;
550 range.chrg.cpMax = nWordEnd;
551 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
552 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
554 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
556 szResult = range.lpstrText;
560 free(range.lpstrText);
570 /*********************************************************************
572 * Function : LogPutString
574 * Description : Inserts text into the logging window. This is really
575 * a regexp aware wrapper function to `LogPutStringNoMatch'.
578 * 1 : pszText = pointer to string going to the log window
580 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
581 * FIXME: this is backwards to the rest of IJB and to common
582 * programming practice. Please use 0 => success instead.
584 *********************************************************************/
585 int LogPutString(const char *pszText)
590 if (pszText == NULL || strlen(pszText) == 0)
600 /* Critical section stops multiple threads doing nasty interactions that
601 * foul up the highlighting and output.
603 EnterCriticalSection(&g_criticalsection);
605 if (g_bHighlightMessages)
609 /* First things first, regexp scan for various things that we would like highlighted */
610 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
612 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
614 char *pszBefore = NULL;
615 char *pszMatch = NULL;
616 char *pszAfter = NULL;
619 /* Split the string up into pieces representing the strings, before
620 at and after the matching pattern
624 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
625 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
626 strncpy(pszBefore, pszText, match.rm_so);
628 if (match.rm_eo < (regoff_t)strlen(pszText))
630 pszAfter = strdup(&pszText[match.rm_eo]);
632 nMatchSize = match.rm_eo - match.rm_so;
633 pszMatch = (char *)malloc(nMatchSize + 1);
634 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
635 pszMatch[nMatchSize] = '\0';
637 /* Recursively call LogPutString */
640 LogPutString(pszBefore);
645 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
650 LogPutString(pszAfter);
660 result = LogPutStringNoMatch(pszText, STYLE_NONE);
663 LeaveCriticalSection(&g_criticalsection);
670 /*********************************************************************
672 * Function : LogPutStringNoMatch
674 * Description : Puts a string into the logging window.
677 * 1 : pszText = pointer to string going to the log window
678 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
680 * Returns : Always 1 => success.
681 * FIXME: this is backwards to the rest of IJB and to common
682 * programming practice. Please use 0 => success instead.
684 *********************************************************************/
685 int LogPutStringNoMatch(const char *pszText, int style)
691 assert(g_hwndLogBox);
692 if (g_hwndLogBox == NULL)
697 /* TODO preserve existing selection */
699 /* Go to the end of the text */
700 nTextLength = GetWindowTextLength(g_hwndLogBox);
701 range.cpMin = nTextLength;
702 range.cpMax = nTextLength;
703 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
705 /* Apply a formatting style */
706 memset(&format, 0, sizeof(format));
707 format.cbSize = sizeof(format);
708 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
709 format.yHeight = (g_nFontSize * 1440) / 72;
710 strlcpy(format.szFaceName, g_szFontFaceName, sizeof(format.szFaceName));
711 if (style == STYLE_NONE)
714 format.dwEffects |= CFE_AUTOCOLOR;
716 else if (style == STYLE_HEADER)
718 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
720 else if (style == STYLE_HIGHLIGHT)
722 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
724 else if (style == STYLE_LINK)
726 format.dwEffects |= CFE_UNDERLINE;
727 format.crTextColor = RGB(0, 0, 255);
729 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
731 /* Append text to the end */
732 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
734 /* TODO Restore the old selection */
737 if (strchr(pszText, '\n') != NULL)
739 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
742 /* Set the force clip timer going. This timer ensures clipping is done
743 intermittently even when there is a sustained burst of logging
745 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
747 g_bClipPending = TRUE;
755 /*********************************************************************
757 * Function : LogShowActivity
759 * Description : Start the spinner.
765 *********************************************************************/
766 void LogShowActivity(void)
768 /* Start some activity timers */
769 if (g_bShowActivityAnimation)
771 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
772 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
778 /*********************************************************************
780 * Function : LogClipBuffer
782 * Description : Prunes old lines from the log.
788 *********************************************************************/
789 void LogClipBuffer(void)
791 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
792 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
794 /* Compute the range representing the lines to be deleted */
795 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
796 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
799 range.cpMax = nLastChar;
801 /* TODO get current selection */
803 /* TODO adjust and clip old selection against range to be deleted */
805 /* Select range and erase it (turning off autoscroll to prevent
807 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
808 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
809 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
810 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
812 /* reposition (back to) the end of the log content */
813 range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
815 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
817 /* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
818 SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
825 /*********************************************************************
827 * Function : CreateHiddenLogOwnerWindow
829 * Description : Creates a hidden owner window that stops the log
830 * window appearing in the task bar.
833 * 1 : hInstance = application's instance handle
835 * Returns : Handle to newly created window.
837 *********************************************************************/
838 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
840 static const char *szWndName = "PrivoxyLogOwner";
845 wc.lpfnWndProc = LogOwnerWindowProc;
848 wc.hInstance = hInstance;
851 wc.hbrBackground = 0;
853 wc.lpszClassName = szWndName;
857 hwnd = CreateWindow(szWndName, szWndName,
858 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
859 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
866 /*********************************************************************
868 * Function : LogOwnerWindowProc
870 * Description : Dummy procedure that does nothing special.
873 * 1 : hwnd = window handle
874 * 2 : uMsg = message number
875 * 3 : wParam = first param for this message
876 * 4 : lParam = next param for this message
878 * Returns : Same as `DefWindowProc'.
880 *********************************************************************/
881 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
883 return DefWindowProc(hwnd, uMsg, wParam, lParam);
888 /*********************************************************************
890 * Function : CreateLogWindow
892 * Description : Create the logging window.
895 * 1 : hInstance = application's instance handle
896 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
898 * Returns : Handle to newly created window.
900 *********************************************************************/
901 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
903 static const char *szWndName = "PrivoxyLogWindow";
904 static const char *szWndTitle = "Privoxy";
907 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
911 memset(&wc, 0, sizeof(wc));
912 wc.cbSize = sizeof(wc);
913 wc.style = CS_DBLCLKS;
914 wc.lpfnWndProc = LogWindowProc;
917 wc.hInstance = hInstance;
918 wc.hIcon = g_hiconApp;
920 wc.hbrBackground = 0;
921 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
922 wc.lpszClassName = szWndName;
923 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
924 RegisterClassEx(&wc);
926 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
927 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
928 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
930 /* Now create a child list box */
931 GetClientRect(hwnd, &rcClient);
933 /* Create a rich edit control */
935 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
936 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
937 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
938 hwnd, NULL, hInstance, NULL);
939 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
941 /* Subclass the control to catch certain messages */
942 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
943 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
945 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
946 if (!g_bShowOnTaskBar &&
947 (nCmdShow == SW_SHOWMINIMIZED ||
948 nCmdShow == SW_MINIMIZE ||
949 nCmdShow == SW_SHOWMINNOACTIVE))
951 g_bShowLogWindow = FALSE;
955 ShowWindow(hwnd, nCmdShow);
959 GetClientRect(g_hwndLogFrame, &rcClient);
960 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
967 /*********************************************************************
969 * Function : InitRichEdit
971 * Description : Initialise the rich edit control library.
975 * Returns : TRUE => success, FALSE => failure.
976 * FIXME: this is backwards to the rest of IJB and to common
977 * programming practice. Please use 0 => success instead.
979 *********************************************************************/
980 BOOL InitRichEdit(void)
982 static HINSTANCE hInstRichEdit;
983 if (hInstRichEdit == NULL)
985 g_nRichEditVersion = 0;
986 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
989 g_nRichEditVersion = _RICHEDIT_VER;
993 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
996 g_nRichEditVersion = 0x0100;
1000 return (hInstRichEdit != NULL) ? TRUE : FALSE;
1005 /*********************************************************************
1007 * Function : ShowLogWindow
1009 * Description : Shows or hides the log window. We will also raise the
1010 * window on a show command in case it is buried.
1013 * 1 : bShow = TRUE to show, FALSE to mimize/hide
1017 *********************************************************************/
1018 void ShowLogWindow(BOOL bShow)
1022 SetForegroundWindow(g_hwndLogFrame);
1023 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1026 else if (g_bShowOnTaskBar)
1028 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1032 ShowWindow(g_hwndLogFrame, SW_HIDE);
1037 /*********************************************************************
1039 * Function : EditFile
1041 * Description : Opens the specified setting file for editing.
1042 * FIXME: What if the file has no associated application. Check for return values
1043 * from ShellExecute??
1046 * 1 : filename = filename from the config (aka config.txt) file.
1050 *********************************************************************/
1051 void EditFile(const char *filename)
1055 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1061 /*--------------------------------------------------------------------------*/
1062 /* Windows message handlers */
1063 /*--------------------------------------------------------------------------*/
1066 /*********************************************************************
1068 * Function : OnLogRButtonUp
1070 * Description : Handler for WM_RBUTTONUP messages.
1073 * 1 : nModifier = wParam from mouse message (unused)
1074 * 2 : x = x coordinate of the mouse event
1075 * 3 : y = y coordinate of the mouse event
1079 *********************************************************************/
1080 void OnLogRButtonUp(int nModifier, int x, int y)
1082 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1085 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1087 /* Check if there is a selection */
1089 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1090 if (range.cpMin == range.cpMax)
1092 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1096 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1099 /* Display the popup */
1100 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1107 /*********************************************************************
1109 * Function : OnLogCommand
1111 * Description : Handler for WM_COMMAND messages.
1114 * 1 : nCommand = the command portion of the menu selection event
1118 *********************************************************************/
1119 void OnLogCommand(int nCommand)
1123 case ID_TOGGLE_SHOWWINDOW:
1124 g_bShowLogWindow = !g_bShowLogWindow;
1126 ShowLogWindow(g_bShowLogWindow);
1130 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1134 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1137 case ID_VIEW_CLEARLOG:
1138 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1141 case ID_VIEW_LOGMESSAGES:
1142 g_bLogMessages = !g_bLogMessages;
1143 /* SaveLogSettings(); */
1146 case ID_VIEW_MESSAGEHIGHLIGHTING:
1147 g_bHighlightMessages = !g_bHighlightMessages;
1148 /* SaveLogSettings(); */
1151 case ID_VIEW_LIMITBUFFERSIZE:
1152 g_bLimitBufferSize = !g_bLimitBufferSize;
1153 /* SaveLogSettings(); */
1156 case ID_VIEW_ACTIVITYANIMATION:
1157 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1158 /* SaveLogSettings(); */
1161 #ifdef FEATURE_TOGGLE
1162 /* by haroon - change toggle to its opposite value */
1163 case ID_TOGGLE_ENABLED:
1164 global_toggle_state = !global_toggle_state;
1165 if (global_toggle_state)
1167 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1171 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1175 #endif /* def FEATURE_TOGGLE */
1177 case ID_TOOLS_EDITCONFIG:
1178 EditFile(configfile);
1181 case ID_TOOLS_EDITDEFAULTACTIONS:
1182 EditFile(g_default_actions_file);
1185 case ID_TOOLS_EDITUSERACTIONS:
1186 EditFile(g_user_actions_file);
1189 case ID_TOOLS_EDITFILTERS:
1190 EditFile(g_re_filterfile);
1193 #ifdef FEATURE_TRUST
1194 case ID_TOOLS_EDITTRUST:
1195 EditFile(g_trustfile);
1197 #endif /* def FEATURE_TRUST */
1200 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1204 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1207 case ID_HELP_MANUAL:
1208 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1211 case ID_HELP_STATUS:
1212 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1216 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1227 /*********************************************************************
1229 * Function : OnLogInitMenu
1231 * Description : Handler for WM_INITMENU messages. Enable, disable,
1232 * check, and/or uncheck menu options as apropos.
1235 * 1 : hmenu = handle to menu to "make current"
1239 *********************************************************************/
1240 void OnLogInitMenu(HMENU hmenu)
1242 /* Only enable editors if there is a file to edit */
1243 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1244 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1245 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1246 #ifdef FEATURE_TRUST
1247 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1248 #endif /* def FEATURE_TRUST */
1250 /* Check/uncheck options */
1251 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1252 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1253 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1254 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1255 #ifdef FEATURE_TOGGLE
1256 /* by haroon - menu item for Enable toggle on/off */
1257 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1258 #endif /* def FEATURE_TOGGLE */
1259 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1264 /*********************************************************************
1266 * Function : OnLogTimer
1268 * Description : Handler for WM_TIMER messages.
1271 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1275 *********************************************************************/
1276 void OnLogTimer(int nTimer)
1281 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1284 case TIMER_ANIMSTOP_ID:
1287 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1288 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1291 case TIMER_CLIPBUFFER_ID:
1292 case TIMER_CLIPBUFFER_FORCE_ID:
1294 g_bClipPending = FALSE;
1295 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1296 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1307 /*********************************************************************
1309 * Function : SetIdleIcon
1311 * Description : Sets the tray icon to either idle or off
1317 *********************************************************************/
1320 #ifdef FEATURE_TOGGLE
1321 if (!global_toggle_state)
1323 TraySetIcon(g_hwndTray, 1, g_hiconOff);
1324 /* log_error(LOG_LEVEL_INFO, "Privoxy OFF icon selected."); */
1327 #endif /* def FEATURE_TOGGLE */
1328 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1332 /*********************************************************************
1334 * Function : LogRichEditProc
1336 * Description : Window subclass routine handles some events for the rich edit control.
1339 * 1 : hwnd = window handle of the rich edit control
1340 * 2 : uMsg = message number
1341 * 3 : wParam = first param for this message
1342 * 4 : lParam = next param for this message
1344 * Returns : Appropriate M$ window message handler codes.
1346 *********************************************************************/
1347 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1354 pt.x = LOWORD(lParam);
1355 pt.y = HIWORD(lParam);
1356 ClientToScreen(hwnd, &pt);
1357 OnLogRButtonUp(wParam, pt.x, pt.y);
1362 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1364 OnLogCommand(ID_VIEW_CLEARLOG);
1369 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1374 /*********************************************************************
1376 * Function : LogWindowProc
1378 * Description : Windows call back routine handles events on the log window.
1381 * 1 : hwnd = handle of the logging window
1382 * 2 : uMsg = message number
1383 * 3 : wParam = first param for this message
1384 * 4 : lParam = next param for this message
1386 * Returns : Appropriate M$ window message handler codes.
1388 *********************************************************************/
1389 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1397 /* This is the end - beautiful friend - the end */
1398 DestroyWindow(g_hwndLogBox);
1399 DestroyWindow(g_hwndLogFrame);
1407 g_bShowLogWindow = wParam;
1409 /* Resize the logging window to fit the new frame */
1413 GetClientRect(g_hwndLogFrame, &rc);
1414 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1419 OnLogInitMenu((HMENU) wParam);
1427 OnLogCommand(LOWORD(wParam));
1434 if (g_bCloseHidesWindow)
1436 ShowLogWindow(FALSE);
1441 ShowLogWindow(FALSE);
1447 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1449 OnLogCommand(ID_VIEW_CLEARLOG);
1455 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1459 #endif /* ndef _WIN_CONSOLE - entire file */