1 const char w32log_rcs[] = "$Id: w32log.c,v 1.27 2006/07/18 14:48:48 david__schmidt 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.27 2006/07/18 14:48:48 david__schmidt
36 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
37 * with what was really the latest development (the v_3_0_branch branch)
39 * Revision 1.25.2.5 2003/04/04 12:48:51 oes
41 * - Made tray menu correctly reflect initial window visibility state
42 * - Hopefully fixed problem where log window contents wasn't visible
43 * until vertical scroll bar was clicked. Thanks to Guy for the fix!
45 * Revision 1.25.2.4 2003/03/11 11:53:59 oes
46 * Cosmetic: Renamed cryptic variable
48 * Revision 1.25.2.3 2002/11/20 14:39:05 oes
49 * Fixed compiler warning
51 * Revision 1.25.2.2 2002/09/25 15:23:10 oes
52 * Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
54 * Revision 1.25.2.1 2002/08/21 17:59:05 oes
55 * - "Show Privoxy Window" now a toggle
56 * - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
58 * Revision 1.25 2002/04/04 00:36:36 gliptak
59 * always use pcre for matching
61 * Revision 1.24 2002/03/31 17:19:00 jongfoster
62 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
64 * Revision 1.23 2002/03/26 22:57:10 jongfoster
65 * Web server name should begin www.
67 * Revision 1.22 2002/03/24 12:48:23 jongfoster
70 * Revision 1.21 2002/03/24 12:07:35 jongfoster
71 * Consistern name for filters file
73 * Revision 1.20 2002/03/24 12:03:47 jongfoster
76 * Revision 1.19 2002/01/17 21:04:17 jongfoster
77 * Replacing hard references to the URL of the config interface
78 * with #defines from project.h
80 * Revision 1.18 2001/11/30 23:37:24 jongfoster
81 * Renaming the Win32 config file to config.txt - this is almost the
82 * same as the corresponding UNIX name "config"
84 * Revision 1.17 2001/11/16 00:46:31 jongfoster
85 * Fixing compiler warnings
87 * Revision 1.16 2001/08/01 19:58:12 jongfoster
88 * Fixing documentation filenames in help menu, and making status
89 * option work without needing the "Junkbuster Status.URL" file.
91 * Revision 1.15 2001/07/30 22:08:36 jongfoster
92 * Tidying up #defines:
93 * - All feature #defines are now of the form FEATURE_xxx
94 * - Permanently turned off WIN_GUI_EDIT
95 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
97 * Revision 1.14 2001/07/29 18:47:05 jongfoster
98 * Adding missing #include "loadcfg.h"
100 * Revision 1.13 2001/07/19 19:15:14 haroon
101 * - Added a FIXME for EditFile but didn't fix :-)
103 * Revision 1.12 2001/07/13 14:04:59 oes
104 * Removed all #ifdef PCRS
106 * Revision 1.11 2001/06/07 23:08:12 jongfoster
107 * Forward and ACL edit options removed.
109 * Revision 1.10 2001/05/31 21:37:11 jongfoster
110 * GUI changes to rename "permissions file" to "actions file".
112 * Revision 1.9 2001/05/31 17:33:13 oes
116 * Revision 1.8 2001/05/29 09:50:24 jongfoster
117 * Unified blocklist/imagelist/permissionslist.
118 * File format is still under discussion, but the internal changes
121 * Also modified interceptor behaviour:
122 * - We now intercept all URLs beginning with one of the following
123 * prefixes (and *only* these prefixes):
125 * * http://ijbswa.sf.net/config/
126 * * http://ijbswa.sourceforge.net/config/
127 * - New interceptors "home page" - go to http://i.j.b/ to see it.
128 * - Internal changes so that intercepted and fast redirect pages
129 * are not replaced with an image.
130 * - Interceptors now have the option to send a binary page direct
131 * to the client. (i.e. ijb-send-banner uses this)
132 * - Implemented show-url-info interceptor. (Which is why I needed
133 * the above interceptors changes - a typical URL is
134 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
135 * The previous mechanism would not have intercepted that, and
136 * if it had been intercepted then it then it would have replaced
139 * Revision 1.7 2001/05/26 01:26:34 jongfoster
140 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
141 * This #define cannot be set from ./configure - there's no point, it
142 * doesn't work yet. See feature request # 425722
144 * Revision 1.6 2001/05/26 00:31:30 jongfoster
145 * Fixing compiler warning about comparing signed/unsigned.
147 * Revision 1.5 2001/05/26 00:28:36 jongfoster
148 * Automatic reloading of config file.
149 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
150 * Most of the global variables have been moved to a new
151 * struct configuration_spec, accessed through csp->config->globalname
152 * Most of the globals remaining are used by the Win32 GUI.
154 * Revision 1.4 2001/05/22 18:56:28 oes
157 * Revision 1.3 2001/05/20 15:07:54 jongfoster
158 * File is now ignored if _WIN_CONSOLE is defined.
160 * Revision 1.2 2001/05/20 01:21:20 jongfoster
161 * Version 2.9.4 checkin.
162 * - Merged popupfile and cookiefile, and added control over PCRS
163 * filtering, in new "permissionsfile".
164 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
165 * file error you now get a message box (in the Win32 GUI) rather
166 * than the program exiting with no explanation.
167 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
169 * - Removed tabs from "config"
170 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
171 * - Bumped up version number.
173 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
174 * Initial import of version 2.9.3 source tree
177 *********************************************************************/
189 #include <richedit.h>
193 #include "w32taskbar.h"
197 #include "miscutil.h"
201 const char w32res_h_rcs[] = W32RES_H_VERSION;
205 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
208 const char w32log_h_rcs[] = W32LOG_H_VERSION;
210 #ifndef _WIN_CONSOLE /* entire file */
213 * Timers and the various durations
215 #define TIMER_ANIM_ID 1
216 #define TIMER_ANIM_TIME 100
217 #define TIMER_ANIMSTOP_ID 2
218 #define TIMER_ANIMSTOP_TIME 1000
219 #define TIMER_CLIPBUFFER_ID 3
220 #define TIMER_CLIPBUFFER_TIME 1000
221 #define TIMER_CLIPBUFFER_FORCE_ID 4
222 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
225 * Styles of text that can be output
228 #define STYLE_HIGHLIGHT 1
230 #define STYLE_HEADER 3
233 * Number of frames of animation in tray activity sequence
235 #define ANIM_FRAMES 8
237 #define DEFAULT_MAX_BUFFER_LINES 200
238 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
239 #define DEFAULT_LOG_FONT_SIZE 8
242 * These values affect the way the log window behaves, they should be read
243 * from a file but for the moment, they are hardcoded here. Some options are
244 * configurable through the UI.
247 /* Indicates whether task bar shows activity animation */
248 BOOL g_bShowActivityAnimation = 1;
250 /* Indicates whether the log window is shown */
251 BOOL g_bShowLogWindow = 1;
253 /* Indicates if the log window appears on the task bar */
254 BOOL g_bShowOnTaskBar = 0;
256 /* Indicates whether closing the log window really just hides it */
257 BOOL g_bCloseHidesWindow = 1;
259 /* Indicates if messages are logged at all */
260 BOOL g_bLogMessages = 1;
262 /* Indicates whether log messages are highlighted */
263 BOOL g_bHighlightMessages = 1;
265 /* Indicates if buffer is limited in size */
266 BOOL g_bLimitBufferSize = 1;
268 /* Maximum number of lines allowed in buffer when limited */
269 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
272 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
274 /* Size of font to use */
275 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
278 /* FIXME: this is a kludge */
280 const char * g_default_actions_file = NULL;
281 const char * g_user_actions_file = NULL;
282 const char * g_re_filterfile = NULL;
284 const char * g_trustfile = NULL;
285 #endif /* def FEATURE_TRUST */
287 /* FIXME: end kludge */
289 /* Regular expression for detected URLs */
290 #define RE_URL "http:[^ \n\r]*"
293 * Regular expressions that are used to perform highlight in the log window
295 static struct _Pattern
300 } patterns_to_highlight[] =
303 { RE_URL, STYLE_LINK },
304 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
305 /* interesting text to highlight */
306 { "crunch!", STYLE_HIGHLIGHT },
308 { "referer:", STYLE_HEADER },
309 { "proxy-connection:", STYLE_HEADER },
310 { "proxy-agent:", STYLE_HEADER },
311 { "user-agent:", STYLE_HEADER },
312 { "host:", STYLE_HEADER },
313 { "accept:", STYLE_HEADER },
314 { "accept-encoding:", STYLE_HEADER },
315 { "accept-language:", STYLE_HEADER },
316 { "accept-charset:", STYLE_HEADER },
317 { "accept-ranges:", STYLE_HEADER },
318 { "date:", STYLE_HEADER },
319 { "cache-control:", STYLE_HEADER },
320 { "cache-last-checked:", STYLE_HEADER },
321 { "connection:", STYLE_HEADER },
322 { "content-type", STYLE_HEADER },
323 { "content-length", STYLE_HEADER },
324 { "cookie", STYLE_HEADER },
325 { "last-modified:", STYLE_HEADER },
326 { "pragma:", STYLE_HEADER },
327 { "server:", STYLE_HEADER },
328 { "etag:", STYLE_HEADER },
329 { "expires:", STYLE_HEADER },
330 { "warning:", STYLE_HEADER },
331 /* this is the terminator statement - do not delete! */
344 static CRITICAL_SECTION g_criticalsection;
345 static HWND g_hwndTray;
346 static HWND g_hwndLogBox;
347 static WNDPROC g_fnLogBox;
348 static HICON g_hiconAnim[ANIM_FRAMES];
349 static HICON g_hiconIdle;
350 static int g_nAnimFrame;
351 static BOOL g_bClipPending = FALSE;
352 static int g_nRichEditVersion = 0;
357 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
358 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
359 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
360 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
361 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
362 static BOOL InitRichEdit(void);
363 static void LogClipBuffer(void);
364 static void LogCreatePatternMatchingBuffers(void);
365 static void LogDestroyPatternMatchingBuffers(void);
366 static int LogPutStringNoMatch(const char *pszText, int style);
369 /*********************************************************************
371 * Function : InitLogWindow
373 * Description : Initialise the log window.
377 * Returns : Always TRUE (there should be error checking on the resources).
379 *********************************************************************/
380 BOOL InitLogWindow(void)
385 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
386 for (i = 0; i < ANIM_FRAMES; i++)
388 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
390 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
392 /* Create the user interface */
393 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
394 g_hwndTray = CreateTrayWindow(g_hInstance);
395 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
397 /* Create pattern matching buffers (for highlighting */
398 LogCreatePatternMatchingBuffers();
400 /* Create a critical section to protect multi-threaded access to certain things */
401 InitializeCriticalSection(&g_criticalsection);
408 /*********************************************************************
410 * Function : TermLogWindow
412 * Description : Cleanup the logwindow.
418 *********************************************************************/
419 void TermLogWindow(void)
423 LogDestroyPatternMatchingBuffers();
425 TrayDeleteIcon(g_hwndTray, 1);
426 DeleteObject(g_hiconApp);
427 DeleteObject(g_hiconIdle);
428 for (i = 0; i < ANIM_FRAMES; i++)
430 DeleteObject(g_hiconAnim[i]);
436 /*********************************************************************
438 * Function : LogCreatePatternMatchingBuffers
440 * Description : Compile the pattern matching buffers.
446 *********************************************************************/
447 void LogCreatePatternMatchingBuffers(void)
450 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
452 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
457 /*********************************************************************
459 * Function : LogDestroyPatternMatchingBuffers
461 * Description : Free up the pattern matching buffers.
467 *********************************************************************/
468 void LogDestroyPatternMatchingBuffers(void)
471 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
473 regfree(&patterns_to_highlight[i].buffer);
478 /*********************************************************************
480 * Function : LogGetURLUnderCursor
482 * Description : Returns the URL from under the cursor (remember to free it!).
486 * Returns : NULL or a pointer to an URL string.
488 *********************************************************************/
489 char *LogGetURLUnderCursor(void)
491 char *szResult = NULL;
496 DWORD nWordStart = 0;
499 regcomp(&re, RE_URL, REG_ICASE);
501 /* Get the position of the cursor over the text window */
502 GetCursorPos(&ptCursor);
503 ScreenToClient(g_hwndLogBox, &ptCursor);
507 /* Search backwards and fowards to obtain the word that is highlighted */
508 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
509 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
510 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
512 /* Compare the string to the pattern */
513 if (nWordEnd > nWordStart)
518 range.chrg.cpMin = nWordStart;
519 range.chrg.cpMax = nWordEnd;
520 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
521 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
523 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
525 szResult = range.lpstrText;
529 free(range.lpstrText);
539 /*********************************************************************
541 * Function : LogPutString
543 * Description : Inserts text into the logging window. This is really
544 * a regexp aware wrapper function to `LogPutStringNoMatch'.
547 * 1 : pszText = pointer to string going to the log window
549 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
550 * FIXME: this is backwards to the rest of IJB and to common
551 * programming practice. Please use 0 => success instead.
553 *********************************************************************/
554 int LogPutString(const char *pszText)
559 if (pszText == NULL || strlen(pszText) == 0)
569 /* Critical section stops multiple threads doing nasty interactions that
570 * foul up the highlighting and output.
572 EnterCriticalSection(&g_criticalsection);
574 if (g_bHighlightMessages)
578 /* First things first, regexp scan for various things that we would like highlighted */
579 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
581 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
583 char *pszBefore = NULL;
584 char *pszMatch = NULL;
585 char *pszAfter = NULL;
588 /* Split the string up into pieces representing the strings, before
589 at and after the matching pattern
593 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
594 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
595 strncpy(pszBefore, pszText, match.rm_so);
597 if (match.rm_eo < (regoff_t)strlen(pszText))
599 pszAfter = strdup(&pszText[match.rm_eo]);
601 nMatchSize = match.rm_eo - match.rm_so;
602 pszMatch = (char *)malloc(nMatchSize + 1);
603 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
604 pszMatch[nMatchSize] = '\0';
606 /* Recursively call LogPutString */
609 LogPutString(pszBefore);
614 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
619 LogPutString(pszAfter);
629 result = LogPutStringNoMatch(pszText, STYLE_NONE);
632 LeaveCriticalSection(&g_criticalsection);
639 /*********************************************************************
641 * Function : LogPutStringNoMatch
643 * Description : Puts a string into the logging window.
646 * 1 : pszText = pointer to string going to the log window
647 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
649 * Returns : Always 1 => success.
650 * FIXME: this is backwards to the rest of IJB and to common
651 * programming practice. Please use 0 => success instead.
653 *********************************************************************/
654 int LogPutStringNoMatch(const char *pszText, int style)
660 assert(g_hwndLogBox);
661 if (g_hwndLogBox == NULL)
666 /* TODO preserve existing selection */
668 /* Go to the end of the text */
669 nTextLength = GetWindowTextLength(g_hwndLogBox);
670 range.cpMin = nTextLength;
671 range.cpMax = nTextLength;
672 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
674 /* Apply a formatting style */
675 memset(&format, 0, sizeof(format));
676 format.cbSize = sizeof(format);
677 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
678 format.yHeight = (g_nFontSize * 1440) / 72;
679 strcpy(format.szFaceName, g_szFontFaceName);
680 if (style == STYLE_NONE)
683 format.dwEffects |= CFE_AUTOCOLOR;
685 else if (style == STYLE_HEADER)
687 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
689 else if (style == STYLE_HIGHLIGHT)
691 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
693 else if (style == STYLE_LINK)
695 format.dwEffects |= CFE_UNDERLINE;
696 format.crTextColor = RGB(0, 0, 255);
698 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
700 /* Append text to the end */
701 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
703 /* TODO Restore the old selection */
706 if (strchr(pszText, '\n') != NULL)
708 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
711 /* Set the force clip timer going. This timer ensures clipping is done
712 intermittently even when there is a sustained burst of logging
714 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
716 g_bClipPending = TRUE;
724 /*********************************************************************
726 * Function : LogShowActivity
728 * Description : Start the spinner.
734 *********************************************************************/
735 void LogShowActivity(void)
737 /* Start some activity timers */
738 if (g_bShowActivityAnimation)
740 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
741 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
747 /*********************************************************************
749 * Function : LogClipBuffer
751 * Description : Prunes old lines from the log.
757 *********************************************************************/
758 void LogClipBuffer(void)
760 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
761 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
763 /* Compute the range representing the lines to be deleted */
764 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
765 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
768 range.cpMax = nLastChar;
770 /* TODO get current selection */
772 /* TODO adjust and clip old selection against range to be deleted */
774 /* Select range and erase it (turning off autoscroll to prevent
776 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
777 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
778 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
779 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
781 /* reposition (back to) the end of the log content */
782 range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
784 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
786 /* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
787 SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
794 /*********************************************************************
796 * Function : CreateHiddenLogOwnerWindow
798 * Description : Creates a hidden owner window that stops the log
799 * window appearing in the task bar.
802 * 1 : hInstance = application's instance handle
804 * Returns : Handle to newly created window.
806 *********************************************************************/
807 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
809 static const char *szWndName = "PrivoxyLogOwner";
814 wc.lpfnWndProc = LogOwnerWindowProc;
817 wc.hInstance = hInstance;
820 wc.hbrBackground = 0;
822 wc.lpszClassName = szWndName;
826 hwnd = CreateWindow(szWndName, szWndName,
827 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
828 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
835 /*********************************************************************
837 * Function : LogOwnerWindowProc
839 * Description : Dummy procedure that does nothing special.
842 * 1 : hwnd = window handle
843 * 2 : uMsg = message number
844 * 3 : wParam = first param for this message
845 * 4 : lParam = next param for this message
847 * Returns : Same as `DefWindowProc'.
849 *********************************************************************/
850 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
852 return DefWindowProc(hwnd, uMsg, wParam, lParam);
857 /*********************************************************************
859 * Function : CreateLogWindow
861 * Description : Create the logging window.
864 * 1 : hInstance = application's instance handle
865 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
867 * Returns : Handle to newly created window.
869 *********************************************************************/
870 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
872 static const char *szWndName = "PrivoxyLogWindow";
873 static const char *szWndTitle = "Privoxy";
876 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
880 memset(&wc, 0, sizeof(wc));
881 wc.cbSize = sizeof(wc);
882 wc.style = CS_DBLCLKS;
883 wc.lpfnWndProc = LogWindowProc;
886 wc.hInstance = hInstance;
887 wc.hIcon = g_hiconApp;
889 wc.hbrBackground = 0;
890 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
891 wc.lpszClassName = szWndName;
892 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
893 RegisterClassEx(&wc);
895 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
896 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
897 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
899 /* Now create a child list box */
900 GetClientRect(hwnd, &rcClient);
902 /* Create a rich edit control */
904 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
905 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
906 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
907 hwnd, NULL, hInstance, NULL);
908 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
910 /* Subclass the control to catch certain messages */
911 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
912 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
914 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
915 if (!g_bShowOnTaskBar &&
916 (nCmdShow == SW_SHOWMINIMIZED ||
917 nCmdShow == SW_MINIMIZE ||
918 nCmdShow == SW_SHOWMINNOACTIVE))
920 g_bShowLogWindow = FALSE;
924 ShowWindow(hwnd, nCmdShow);
928 GetClientRect(g_hwndLogFrame, &rcClient);
929 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
936 /*********************************************************************
938 * Function : InitRichEdit
940 * Description : Initialise the rich edit control library.
944 * Returns : TRUE => success, FALSE => failure.
945 * FIXME: this is backwards to the rest of IJB and to common
946 * programming practice. Please use 0 => success instead.
948 *********************************************************************/
949 BOOL InitRichEdit(void)
951 static HINSTANCE hInstRichEdit;
952 if (hInstRichEdit == NULL)
954 g_nRichEditVersion = 0;
955 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
958 g_nRichEditVersion = _RICHEDIT_VER;
962 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
965 g_nRichEditVersion = 0x0100;
969 return (hInstRichEdit != NULL) ? TRUE : FALSE;
974 /*********************************************************************
976 * Function : ShowLogWindow
978 * Description : Shows or hides the log window. We will also raise the
979 * window on a show command in case it is buried.
982 * 1 : bShow = TRUE to show, FALSE to mimize/hide
986 *********************************************************************/
987 void ShowLogWindow(BOOL bShow)
991 SetForegroundWindow(g_hwndLogFrame);
992 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
995 else if (g_bShowOnTaskBar)
997 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1001 ShowWindow(g_hwndLogFrame, SW_HIDE);
1006 /*********************************************************************
1008 * Function : EditFile
1010 * Description : Opens the specified setting file for editing.
1011 * FIXME: What if the file has no associated application. Check for return values
1012 * from ShellExecute??
1015 * 1 : filename = filename from the config (aka config.txt) file.
1019 *********************************************************************/
1020 void EditFile(const char *filename)
1024 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1030 /*--------------------------------------------------------------------------*/
1031 /* Windows message handlers */
1032 /*--------------------------------------------------------------------------*/
1035 /*********************************************************************
1037 * Function : OnLogRButtonUp
1039 * Description : Handler for WM_RBUTTONUP messages.
1042 * 1 : nModifier = wParam from mouse message (unused)
1043 * 2 : x = x coordinate of the mouse event
1044 * 3 : y = y coordinate of the mouse event
1048 *********************************************************************/
1049 void OnLogRButtonUp(int nModifier, int x, int y)
1051 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1054 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1056 /* Check if there is a selection */
1058 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1059 if (range.cpMin == range.cpMax)
1061 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1065 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1068 /* Display the popup */
1069 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1076 /*********************************************************************
1078 * Function : OnLogCommand
1080 * Description : Handler for WM_COMMAND messages.
1083 * 1 : nCommand = the command portion of the menu selection event
1087 *********************************************************************/
1088 void OnLogCommand(int nCommand)
1092 case ID_TOGGLE_SHOWWINDOW:
1093 g_bShowLogWindow = !g_bShowLogWindow;
1095 ShowLogWindow(g_bShowLogWindow);
1099 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1103 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1106 case ID_VIEW_CLEARLOG:
1107 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1110 case ID_VIEW_LOGMESSAGES:
1111 g_bLogMessages = !g_bLogMessages;
1112 /* SaveLogSettings(); */
1115 case ID_VIEW_MESSAGEHIGHLIGHTING:
1116 g_bHighlightMessages = !g_bHighlightMessages;
1117 /* SaveLogSettings(); */
1120 case ID_VIEW_LIMITBUFFERSIZE:
1121 g_bLimitBufferSize = !g_bLimitBufferSize;
1122 /* SaveLogSettings(); */
1125 case ID_VIEW_ACTIVITYANIMATION:
1126 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1127 /* SaveLogSettings(); */
1130 #ifdef FEATURE_TOGGLE
1131 /* by haroon - change toggle to its opposite value */
1132 case ID_TOGGLE_ENABLED:
1133 global_toggle_state = !global_toggle_state;
1134 if (global_toggle_state)
1136 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1140 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1143 #endif /* def FEATURE_TOGGLE */
1145 case ID_TOOLS_EDITCONFIG:
1146 EditFile(configfile);
1149 case ID_TOOLS_EDITDEFAULTACTIONS:
1150 EditFile(g_default_actions_file);
1153 case ID_TOOLS_EDITUSERACTIONS:
1154 EditFile(g_user_actions_file);
1157 case ID_TOOLS_EDITFILTERS:
1158 EditFile(g_re_filterfile);
1161 #ifdef FEATURE_TRUST
1162 case ID_TOOLS_EDITTRUST:
1163 EditFile(g_trustfile);
1165 #endif /* def FEATURE_TRUST */
1168 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1172 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1175 case ID_HELP_MANUAL:
1176 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1179 case ID_HELP_STATUS:
1180 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1184 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1195 /*********************************************************************
1197 * Function : OnLogInitMenu
1199 * Description : Handler for WM_INITMENU messages. Enable, disable,
1200 * check, and/or uncheck menu options as apropos.
1203 * 1 : hmenu = handle to menu to "make current"
1207 *********************************************************************/
1208 void OnLogInitMenu(HMENU hmenu)
1210 /* Only enable editors if there is a file to edit */
1211 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1212 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1213 EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1214 #ifdef FEATURE_TRUST
1215 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1216 #endif /* def FEATURE_TRUST */
1218 /* Check/uncheck options */
1219 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1220 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1221 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1222 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1223 #ifdef FEATURE_TOGGLE
1224 /* by haroon - menu item for Enable toggle on/off */
1225 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1226 #endif /* def FEATURE_TOGGLE */
1227 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1232 /*********************************************************************
1234 * Function : OnLogTimer
1236 * Description : Handler for WM_TIMER messages.
1239 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1243 *********************************************************************/
1244 void OnLogTimer(int nTimer)
1249 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1252 case TIMER_ANIMSTOP_ID:
1254 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1255 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1256 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1259 case TIMER_CLIPBUFFER_ID:
1260 case TIMER_CLIPBUFFER_FORCE_ID:
1262 g_bClipPending = FALSE;
1263 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1264 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1275 /*********************************************************************
1277 * Function : LogRichEditProc
1279 * Description : Window subclass routine handles some events for the rich edit control.
1282 * 1 : hwnd = window handle of the rich edit control
1283 * 2 : uMsg = message number
1284 * 3 : wParam = first param for this message
1285 * 4 : lParam = next param for this message
1287 * Returns : Appropriate M$ window message handler codes.
1289 *********************************************************************/
1290 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1297 pt.x = LOWORD(lParam);
1298 pt.y = HIWORD(lParam);
1299 ClientToScreen(hwnd, &pt);
1300 OnLogRButtonUp(wParam, pt.x, pt.y);
1305 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1307 OnLogCommand(ID_VIEW_CLEARLOG);
1312 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1317 /*********************************************************************
1319 * Function : LogWindowProc
1321 * Description : Windows call back routine handles events on the log window.
1324 * 1 : hwnd = handle of the logging window
1325 * 2 : uMsg = message number
1326 * 3 : wParam = first param for this message
1327 * 4 : lParam = next param for this message
1329 * Returns : Appropriate M$ window message handler codes.
1331 *********************************************************************/
1332 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1340 /* This is the end - beautiful friend - the end */
1341 DestroyWindow(g_hwndLogBox);
1342 DestroyWindow(g_hwndLogFrame);
1350 g_bShowLogWindow = wParam;
1352 /* Resize the logging window to fit the new frame */
1356 GetClientRect(g_hwndLogFrame, &rc);
1357 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1362 OnLogInitMenu((HMENU) wParam);
1370 OnLogCommand(LOWORD(wParam));
1377 if (g_bCloseHidesWindow)
1379 ShowLogWindow(FALSE);
1384 ShowLogWindow(FALSE);
1390 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1392 OnLogCommand(ID_VIEW_CLEARLOG);
1398 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1402 #endif /* ndef _WIN_CONSOLE - entire file */