1 const char w32log_rcs[] = "$Id: w32log.c,v 1.19 2002/01/17 21:04:17 jongfoster 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://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.19 2002/01/17 21:04:17 jongfoster
36 * Replacing hard references to the URL of the config interface
37 * with #defines from project.h
39 * Revision 1.18 2001/11/30 23:37:24 jongfoster
40 * Renaming the Win32 config file to config.txt - this is almost the
41 * same as the corresponding UNIX name "config"
43 * Revision 1.17 2001/11/16 00:46:31 jongfoster
44 * Fixing compiler warnings
46 * Revision 1.16 2001/08/01 19:58:12 jongfoster
47 * Fixing documentation filenames in help menu, and making status
48 * option work without needing the "Junkbuster Status.URL" file.
50 * Revision 1.15 2001/07/30 22:08:36 jongfoster
51 * Tidying up #defines:
52 * - All feature #defines are now of the form FEATURE_xxx
53 * - Permanently turned off WIN_GUI_EDIT
54 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
56 * Revision 1.14 2001/07/29 18:47:05 jongfoster
57 * Adding missing #include "loadcfg.h"
59 * Revision 1.13 2001/07/19 19:15:14 haroon
60 * - Added a FIXME for EditFile but didn't fix :-)
62 * Revision 1.12 2001/07/13 14:04:59 oes
63 * Removed all #ifdef PCRS
65 * Revision 1.11 2001/06/07 23:08:12 jongfoster
66 * Forward and ACL edit options removed.
68 * Revision 1.10 2001/05/31 21:37:11 jongfoster
69 * GUI changes to rename "permissions file" to "actions file".
71 * Revision 1.9 2001/05/31 17:33:13 oes
75 * Revision 1.8 2001/05/29 09:50:24 jongfoster
76 * Unified blocklist/imagelist/permissionslist.
77 * File format is still under discussion, but the internal changes
80 * Also modified interceptor behaviour:
81 * - We now intercept all URLs beginning with one of the following
82 * prefixes (and *only* these prefixes):
84 * * http://ijbswa.sf.net/config/
85 * * http://ijbswa.sourceforge.net/config/
86 * - New interceptors "home page" - go to http://i.j.b/ to see it.
87 * - Internal changes so that intercepted and fast redirect pages
88 * are not replaced with an image.
89 * - Interceptors now have the option to send a binary page direct
90 * to the client. (i.e. ijb-send-banner uses this)
91 * - Implemented show-url-info interceptor. (Which is why I needed
92 * the above interceptors changes - a typical URL is
93 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
94 * The previous mechanism would not have intercepted that, and
95 * if it had been intercepted then it then it would have replaced
98 * Revision 1.7 2001/05/26 01:26:34 jongfoster
99 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
100 * This #define cannot be set from ./configure - there's no point, it
101 * doesn't work yet. See feature request # 425722
103 * Revision 1.6 2001/05/26 00:31:30 jongfoster
104 * Fixing compiler warning about comparing signed/unsigned.
106 * Revision 1.5 2001/05/26 00:28:36 jongfoster
107 * Automatic reloading of config file.
108 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
109 * Most of the global variables have been moved to a new
110 * struct configuration_spec, accessed through csp->config->globalname
111 * Most of the globals remaining are used by the Win32 GUI.
113 * Revision 1.4 2001/05/22 18:56:28 oes
116 * Revision 1.3 2001/05/20 15:07:54 jongfoster
117 * File is now ignored if _WIN_CONSOLE is defined.
119 * Revision 1.2 2001/05/20 01:21:20 jongfoster
120 * Version 2.9.4 checkin.
121 * - Merged popupfile and cookiefile, and added control over PCRS
122 * filtering, in new "permissionsfile".
123 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
124 * file error you now get a message box (in the Win32 GUI) rather
125 * than the program exiting with no explanation.
126 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
128 * - Removed tabs from "config"
129 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
130 * - Bumped up version number.
132 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
133 * Initial import of version 2.9.3 source tree
136 *********************************************************************/
145 #include <richedit.h>
149 #include "w32taskbar.h"
153 #include "miscutil.h"
157 const char w32res_h_rcs[] = W32RES_H_VERSION;
161 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
164 const char w32log_h_rcs[] = W32LOG_H_VERSION;
166 #ifndef _WIN_CONSOLE /* entire file */
169 * Timers and the various durations
171 #define TIMER_ANIM_ID 1
172 #define TIMER_ANIM_TIME 100
173 #define TIMER_ANIMSTOP_ID 2
174 #define TIMER_ANIMSTOP_TIME 1000
175 #define TIMER_CLIPBUFFER_ID 3
176 #define TIMER_CLIPBUFFER_TIME 1000
177 #define TIMER_CLIPBUFFER_FORCE_ID 4
178 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
181 * Styles of text that can be output
184 #define STYLE_HIGHLIGHT 1
186 #define STYLE_HEADER 3
189 * Number of frames of animation in tray activity sequence
191 #define ANIM_FRAMES 8
193 #define DEFAULT_MAX_BUFFER_LINES 200
194 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
195 #define DEFAULT_LOG_FONT_SIZE 8
198 * These values affect the way the log window behaves, they should be read
199 * from a file but for the moment, they are hardcoded here. Some options are
200 * configurable through the UI.
203 /* Indicates whether task bar shows activity animation */
204 BOOL g_bShowActivityAnimation = 1;
206 /* Indicates if the log window appears on the task bar */
207 BOOL g_bShowOnTaskBar = 0;
209 /* Indicates whether closing the log window really just hides it */
210 BOOL g_bCloseHidesWindow = 1;
212 /* Indicates if messages are logged at all */
213 BOOL g_bLogMessages = 1;
215 /* Indicates whether log messages are highlighted */
216 BOOL g_bHighlightMessages = 1;
218 /* Indicates if buffer is limited in size */
219 BOOL g_bLimitBufferSize = 1;
221 /* Maximum number of lines allowed in buffer when limited */
222 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
225 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
227 /* Size of font to use */
228 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
231 /* FIXME: this is a kludge */
233 const char * g_actions_file = NULL;
234 const char * g_re_filterfile = NULL;
236 const char * g_trustfile = NULL;
237 #endif /* def FEATURE_TRUST */
239 /* FIXME: end kludge */
243 /* Regular expression for detected URLs */
244 #define RE_URL "http:[^ \n\r]*"
247 * Regular expressions that are used to perform highlight in the log window
249 static struct _Pattern
254 } patterns_to_highlight[] =
257 { RE_URL, STYLE_LINK },
258 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
259 /* interesting text to highlight */
260 { "crunch!", STYLE_HIGHLIGHT },
262 { "referer:", STYLE_HEADER },
263 { "proxy-connection:", STYLE_HEADER },
264 { "proxy-agent:", STYLE_HEADER },
265 { "user-agent:", STYLE_HEADER },
266 { "host:", STYLE_HEADER },
267 { "accept:", STYLE_HEADER },
268 { "accept-encoding:", STYLE_HEADER },
269 { "accept-language:", STYLE_HEADER },
270 { "accept-charset:", STYLE_HEADER },
271 { "accept-ranges:", STYLE_HEADER },
272 { "date:", STYLE_HEADER },
273 { "cache-control:", STYLE_HEADER },
274 { "cache-last-checked:", STYLE_HEADER },
275 { "connection:", STYLE_HEADER },
276 { "content-type", STYLE_HEADER },
277 { "content-length", STYLE_HEADER },
278 { "cookie", STYLE_HEADER },
279 { "last-modified:", STYLE_HEADER },
280 { "pragma:", STYLE_HEADER },
281 { "server:", STYLE_HEADER },
282 { "etag:", STYLE_HEADER },
283 { "expires:", STYLE_HEADER },
284 { "warning:", STYLE_HEADER },
285 /* this is the terminator statement - do not delete! */
288 #endif /* def REGEX */
299 static CRITICAL_SECTION g_criticalsection;
300 static HWND g_hwndTray;
301 static HWND g_hwndLogBox;
302 static WNDPROC g_fnLogBox;
303 static HICON g_hiconAnim[ANIM_FRAMES];
304 static HICON g_hiconIdle;
305 static HICON g_hiconApp;
306 static int g_nAnimFrame;
307 static BOOL g_bClipPending = FALSE;
308 static int g_nRichEditVersion = 0;
313 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
314 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
315 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
316 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
317 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
318 static BOOL InitRichEdit(void);
319 static void LogClipBuffer(void);
320 static void LogCreatePatternMatchingBuffers(void);
321 static void LogDestroyPatternMatchingBuffers(void);
322 static int LogPutStringNoMatch(const char *pszText, int style);
325 /*********************************************************************
327 * Function : InitLogWindow
329 * Description : Initialise the log window.
333 * Returns : Always TRUE (there should be error checking on the resources).
335 *********************************************************************/
336 BOOL InitLogWindow(void)
341 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
342 for (i = 0; i < ANIM_FRAMES; i++)
344 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
346 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
348 /* Create the user interface */
349 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
350 g_hwndTray = CreateTrayWindow(g_hInstance);
351 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
353 /* Create pattern matching buffers (for highlighting */
354 LogCreatePatternMatchingBuffers();
356 /* Create a critical section to protect multi-threaded access to certain things */
357 InitializeCriticalSection(&g_criticalsection);
364 /*********************************************************************
366 * Function : TermLogWindow
368 * Description : Cleanup the logwindow.
374 *********************************************************************/
375 void TermLogWindow(void)
379 LogDestroyPatternMatchingBuffers();
381 TrayDeleteIcon(g_hwndTray, 1);
382 DeleteObject(g_hiconApp);
383 DeleteObject(g_hiconIdle);
384 for (i = 0; i < ANIM_FRAMES; i++)
386 DeleteObject(g_hiconAnim[i]);
392 /*********************************************************************
394 * Function : LogCreatePatternMatchingBuffers
396 * Description : Compile the pattern matching buffers.
402 *********************************************************************/
403 void LogCreatePatternMatchingBuffers(void)
407 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
409 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
416 /*********************************************************************
418 * Function : LogDestroyPatternMatchingBuffers
420 * Description : Free up the pattern matching buffers.
426 *********************************************************************/
427 void LogDestroyPatternMatchingBuffers(void)
431 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
433 regfree(&patterns_to_highlight[i].buffer);
440 /*********************************************************************
442 * Function : LogGetURLUnderCursor
444 * Description : Returns the URL from under the cursor (remember to free it!).
448 * Returns : NULL or a pointer to an URL string.
450 *********************************************************************/
451 char *LogGetURLUnderCursor(void)
453 char *szResult = NULL;
459 DWORD nWordStart = 0;
462 regcomp(&re, RE_URL, REG_ICASE);
464 /* Get the position of the cursor over the text window */
465 GetCursorPos(&ptCursor);
466 ScreenToClient(g_hwndLogBox, &ptCursor);
470 /* Search backwards and fowards to obtain the word that is highlighted */
471 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
472 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
473 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
475 /* Compare the string to the pattern */
476 if (nWordEnd > nWordStart)
481 range.chrg.cpMin = nWordStart;
482 range.chrg.cpMax = nWordEnd;
483 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
484 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
486 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
488 szResult = range.lpstrText;
492 free(range.lpstrText);
503 /*********************************************************************
505 * Function : LogPutString
507 * Description : Inserts text into the logging window. This is really
508 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
511 * 1 : pszText = pointer to string going to the log window
513 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
514 * FIXME: this is backwards to the rest of IJB and to common
515 * programming practice. Please use 0 => success instead.
517 *********************************************************************/
518 int LogPutString(const char *pszText)
525 if (pszText == NULL || strlen(pszText) == 0)
535 /* Critical section stops multiple threads doing nasty interactions that
536 * foul up the highlighting and output.
538 EnterCriticalSection(&g_criticalsection);
541 if (g_bHighlightMessages)
545 /* First things first, regexp scan for various things that we would like highlighted */
546 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
548 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
550 char *pszBefore = NULL;
551 char *pszMatch = NULL;
552 char *pszAfter = NULL;
555 /* Split the string up into pieces representing the strings, before
556 at and after the matching pattern
560 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
561 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
562 strncpy(pszBefore, pszText, match.rm_so);
564 if (match.rm_eo < (regoff_t)strlen(pszText))
566 pszAfter = strdup(&pszText[match.rm_eo]);
568 nMatchSize = match.rm_eo - match.rm_so;
569 pszMatch = (char *)malloc(nMatchSize + 1);
570 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
571 pszMatch[nMatchSize] = '\0';
573 /* Recursively call LogPutString */
576 LogPutString(pszBefore);
581 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
586 LogPutString(pszAfter);
597 result = LogPutStringNoMatch(pszText, STYLE_NONE);
602 LeaveCriticalSection(&g_criticalsection);
609 /*********************************************************************
611 * Function : LogPutStringNoMatch
613 * Description : Puts a string into the logging window.
616 * 1 : pszText = pointer to string going to the log window
617 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
619 * Returns : Always 1 => success.
620 * FIXME: this is backwards to the rest of IJB and to common
621 * programming practice. Please use 0 => success instead.
623 *********************************************************************/
624 int LogPutStringNoMatch(const char *pszText, int style)
630 assert(g_hwndLogBox);
631 if (g_hwndLogBox == NULL)
636 /* TODO preserve existing selection */
638 /* Go to the end of the text */
639 nTextLength = GetWindowTextLength(g_hwndLogBox);
640 range.cpMin = nTextLength;
641 range.cpMax = nTextLength;
642 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
644 /* Apply a formatting style */
645 memset(&format, 0, sizeof(format));
646 format.cbSize = sizeof(format);
647 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
648 format.yHeight = (g_nFontSize * 1440) / 72;
649 strcpy(format.szFaceName, g_szFontFaceName);
650 if (style == STYLE_NONE)
653 format.dwEffects |= CFE_AUTOCOLOR;
655 else if (style == STYLE_HEADER)
657 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
659 else if (style == STYLE_HIGHLIGHT)
661 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
663 else if (style == STYLE_LINK)
665 format.dwEffects |= CFE_UNDERLINE;
666 format.crTextColor = RGB(0, 0, 255);
668 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
670 /* Append text to the end */
671 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
673 /* TODO Restore the old selection */
676 if (strchr(pszText, '\n') != NULL)
678 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
681 /* Set the force clip timer going. This timer ensures clipping is done
682 intermittently even when there is a sustained burst of logging
684 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
686 g_bClipPending = TRUE;
694 /*********************************************************************
696 * Function : LogShowActivity
698 * Description : Start the spinner.
704 *********************************************************************/
705 void LogShowActivity(void)
707 /* Start some activity timers */
708 if (g_bShowActivityAnimation)
710 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
711 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
717 /*********************************************************************
719 * Function : LogClipBuffer
721 * Description : Prunes old lines from the log.
727 *********************************************************************/
728 void LogClipBuffer(void)
730 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
731 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
733 /* Compute the range representing the lines to be deleted */
734 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
735 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
738 range.cpMax = nLastChar;
740 /* TODO get current selection */
742 /* TODO adjust and clip old selection against range to be deleted */
744 /* Select range and erase it (turning off autoscroll to prevent
746 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
747 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
748 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
749 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
751 /* Restore old selection */
757 /*********************************************************************
759 * Function : CreateHiddenLogOwnerWindow
761 * Description : Creates a hidden owner window that stops the log
762 * window appearing in the task bar.
765 * 1 : hInstance = application's instance handle
767 * Returns : Handle to newly created window.
769 *********************************************************************/
770 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
772 static const char *szWndName = "PrivoxyLogOwner";
777 wc.lpfnWndProc = LogOwnerWindowProc;
780 wc.hInstance = hInstance;
783 wc.hbrBackground = 0;
785 wc.lpszClassName = szWndName;
789 hwnd = CreateWindow(szWndName, szWndName,
790 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
791 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
798 /*********************************************************************
800 * Function : LogOwnerWindowProc
802 * Description : Dummy procedure that does nothing special.
805 * 1 : hwnd = window handle
806 * 2 : uMsg = message number
807 * 3 : wParam = first param for this message
808 * 4 : lParam = next param for this message
810 * Returns : Same as `DefWindowProc'.
812 *********************************************************************/
813 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
815 return DefWindowProc(hwnd, uMsg, wParam, lParam);
820 /*********************************************************************
822 * Function : CreateLogWindow
824 * Description : Create the logging window.
827 * 1 : hInstance = application's instance handle
828 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
830 * Returns : Handle to newly created window.
832 *********************************************************************/
833 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
835 static const char *szWndName = "PrivoxyLogWindow";
836 static const char *szWndTitle = "Privoxy";
839 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
843 memset(&wc, 0, sizeof(wc));
844 wc.cbSize = sizeof(wc);
845 wc.style = CS_DBLCLKS;
846 wc.lpfnWndProc = LogWindowProc;
849 wc.hInstance = hInstance;
850 wc.hIcon = g_hiconApp;
852 wc.hbrBackground = 0;
853 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
854 wc.lpszClassName = szWndName;
855 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
856 RegisterClassEx(&wc);
858 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
859 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
860 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
862 /* Now create a child list box */
863 GetClientRect(hwnd, &rcClient);
865 /* Create a rich edit control */
867 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
868 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
869 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
870 hwnd, NULL, hInstance, NULL);
871 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
873 /* Subclass the control to catch certain messages */
874 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
875 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
877 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
878 if (!g_bShowOnTaskBar &&
879 (nCmdShow == SW_SHOWMINIMIZED ||
880 nCmdShow == SW_MINIMIZE ||
881 nCmdShow == SW_SHOWMINNOACTIVE))
886 ShowWindow(hwnd, nCmdShow);
889 GetClientRect(g_hwndLogFrame, &rcClient);
890 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
897 /*********************************************************************
899 * Function : InitRichEdit
901 * Description : Initialise the rich edit control library.
905 * Returns : TRUE => success, FALSE => failure.
906 * FIXME: this is backwards to the rest of IJB and to common
907 * programming practice. Please use 0 => success instead.
909 *********************************************************************/
910 BOOL InitRichEdit(void)
912 static HINSTANCE hInstRichEdit;
913 if (hInstRichEdit == NULL)
915 g_nRichEditVersion = 0;
916 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
919 g_nRichEditVersion = _RICHEDIT_VER;
923 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
926 g_nRichEditVersion = 0x0100;
930 return (hInstRichEdit != NULL) ? TRUE : FALSE;
935 /*********************************************************************
937 * Function : ShowLogWindow
939 * Description : Shows or hides the log window. We will also raise the
940 * window on a show command in case it is buried.
943 * 1 : bShow = TRUE to show, FALSE to mimize/hide
947 *********************************************************************/
948 void ShowLogWindow(BOOL bShow)
952 SetForegroundWindow(g_hwndLogFrame);
953 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
955 else if (g_bShowOnTaskBar)
957 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
961 ShowWindow(g_hwndLogFrame, SW_HIDE);
967 /*********************************************************************
969 * Function : EditFile
971 * Description : Opens the specified setting file for editing.
972 * FIXME: What if the file has no associated application. Check for return values
973 * from ShellExecute??
976 * 1 : filename = filename from the config (aka config.txt) file.
980 *********************************************************************/
981 void EditFile(const char *filename)
985 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
991 /*--------------------------------------------------------------------------*/
992 /* Windows message handlers */
993 /*--------------------------------------------------------------------------*/
996 /*********************************************************************
998 * Function : OnLogRButtonUp
1000 * Description : Handler for WM_RBUTTONUP messages.
1003 * 1 : nModifier = wParam from mouse message (unused)
1004 * 2 : x = x coordinate of the mouse event
1005 * 3 : y = y coordinate of the mouse event
1009 *********************************************************************/
1010 void OnLogRButtonUp(int nModifier, int x, int y)
1012 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1015 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1017 /* Check if there is a selection */
1019 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1020 if (range.cpMin == range.cpMax)
1022 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1026 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1029 /* Display the popup */
1030 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1037 /*********************************************************************
1039 * Function : OnLogCommand
1041 * Description : Handler for WM_COMMAND messages.
1044 * 1 : nCommand = the command portion of the menu selection event
1048 *********************************************************************/
1049 void OnLogCommand(int nCommand)
1054 ShowLogWindow(TRUE);
1058 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1062 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1065 case ID_VIEW_CLEARLOG:
1066 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1069 case ID_VIEW_LOGMESSAGES:
1070 g_bLogMessages = !g_bLogMessages;
1071 /* SaveLogSettings(); */
1074 case ID_VIEW_MESSAGEHIGHLIGHTING:
1075 g_bHighlightMessages = !g_bHighlightMessages;
1076 /* SaveLogSettings(); */
1079 case ID_VIEW_LIMITBUFFERSIZE:
1080 g_bLimitBufferSize = !g_bLimitBufferSize;
1081 /* SaveLogSettings(); */
1084 case ID_VIEW_ACTIVITYANIMATION:
1085 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1086 /* SaveLogSettings(); */
1089 #ifdef FEATURE_TOGGLE
1090 /* by haroon - change toggle to its opposite value */
1091 case ID_TOGGLE_ENABLED:
1092 g_bToggleIJB = !g_bToggleIJB;
1095 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1099 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1102 #endif /* def FEATURE_TOGGLE */
1104 case ID_TOOLS_EDITCONFIG:
1105 EditFile(configfile);
1108 case ID_TOOLS_EDITACTIONS:
1109 EditFile(g_actions_file);
1112 case ID_TOOLS_EDITPERLRE:
1113 EditFile(g_re_filterfile);
1116 #ifdef FEATURE_TRUST
1117 case ID_TOOLS_EDITTRUST:
1118 EditFile(g_trustfile);
1120 #endif /* def FEATURE_TRUST */
1123 ShellExecute(g_hwndLogFrame, "open", "doc/gpl.html", NULL, NULL, SW_SHOWNORMAL);
1127 ShellExecute(g_hwndLogFrame, "open", "doc/ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1130 case ID_HELP_MANUAL:
1131 ShellExecute(g_hwndLogFrame, "open", "doc/ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1134 case ID_HELP_STATUS:
1135 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1139 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1150 /*********************************************************************
1152 * Function : OnLogInitMenu
1154 * Description : Handler for WM_INITMENU messages. Enable, disable,
1155 * check, and/or uncheck menu options as apropos.
1158 * 1 : hmenu = handle to menu to "make current"
1162 *********************************************************************/
1163 void OnLogInitMenu(HMENU hmenu)
1165 /* Only enable editors if there is a file to edit */
1166 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1167 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1168 #ifdef FEATURE_TRUST
1169 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1170 #endif /* def FEATURE_TRUST */
1172 /* Check/uncheck options */
1173 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1174 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1175 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1176 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1177 #ifdef FEATURE_TOGGLE
1178 /* by haroon - menu item for Enable toggle on/off */
1179 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1180 #endif /* def FEATURE_TOGGLE */
1185 /*********************************************************************
1187 * Function : OnLogTimer
1189 * Description : Handler for WM_TIMER messages.
1192 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1196 *********************************************************************/
1197 void OnLogTimer(int nTimer)
1202 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1205 case TIMER_ANIMSTOP_ID:
1207 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1208 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1209 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1212 case TIMER_CLIPBUFFER_ID:
1213 case TIMER_CLIPBUFFER_FORCE_ID:
1215 g_bClipPending = FALSE;
1216 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1217 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1228 /*********************************************************************
1230 * Function : LogRichEditProc
1232 * Description : Window subclass routine handles some events for the rich edit control.
1235 * 1 : hwnd = window handle of the rich edit control
1236 * 2 : uMsg = message number
1237 * 3 : wParam = first param for this message
1238 * 4 : lParam = next param for this message
1240 * Returns : Appropriate M$ window message handler codes.
1242 *********************************************************************/
1243 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1250 pt.x = LOWORD(lParam);
1251 pt.y = HIWORD(lParam);
1252 ClientToScreen(hwnd, &pt);
1253 OnLogRButtonUp(wParam, pt.x, pt.y);
1257 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1262 /*********************************************************************
1264 * Function : LogWindowProc
1266 * Description : Windows call back routine handles events on the log window.
1269 * 1 : hwnd = handle of the logging window
1270 * 2 : uMsg = message number
1271 * 3 : wParam = first param for this message
1272 * 4 : lParam = next param for this message
1274 * Returns : Appropriate M$ window message handler codes.
1276 *********************************************************************/
1277 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1285 /* This is the end - beautiful friend - the end */
1286 DestroyWindow(g_hwndLogBox);
1287 DestroyWindow(g_hwndLogFrame);
1296 /* Resize the logging window to fit the new frame */
1300 GetClientRect(g_hwndLogFrame, &rc);
1301 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1306 OnLogInitMenu((HMENU) wParam);
1314 OnLogCommand(LOWORD(wParam));
1321 if (g_bCloseHidesWindow)
1323 ShowLogWindow(FALSE);
1328 ShowLogWindow(FALSE);
1334 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1338 #endif /* ndef _WIN_CONSOLE - entire file */