1 const char w32log_rcs[] = "$Id: w32log.c,v 1.18 2001/11/30 23:37:24 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 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
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.18 2001/11/30 23:37:24 jongfoster
36 * Renaming the Win32 config file to config.txt - this is almost the
37 * same as the corresponding UNIX name "config"
39 * Revision 1.17 2001/11/16 00:46:31 jongfoster
40 * Fixing compiler warnings
42 * Revision 1.16 2001/08/01 19:58:12 jongfoster
43 * Fixing documentation filenames in help menu, and making status
44 * option work without needing the "Junkbuster Status.URL" file.
46 * Revision 1.15 2001/07/30 22:08:36 jongfoster
47 * Tidying up #defines:
48 * - All feature #defines are now of the form FEATURE_xxx
49 * - Permanently turned off WIN_GUI_EDIT
50 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
52 * Revision 1.14 2001/07/29 18:47:05 jongfoster
53 * Adding missing #include "loadcfg.h"
55 * Revision 1.13 2001/07/19 19:15:14 haroon
56 * - Added a FIXME for EditFile but didn't fix :-)
58 * Revision 1.12 2001/07/13 14:04:59 oes
59 * Removed all #ifdef PCRS
61 * Revision 1.11 2001/06/07 23:08:12 jongfoster
62 * Forward and ACL edit options removed.
64 * Revision 1.10 2001/05/31 21:37:11 jongfoster
65 * GUI changes to rename "permissions file" to "actions file".
67 * Revision 1.9 2001/05/31 17:33:13 oes
71 * Revision 1.8 2001/05/29 09:50:24 jongfoster
72 * Unified blocklist/imagelist/permissionslist.
73 * File format is still under discussion, but the internal changes
76 * Also modified interceptor behaviour:
77 * - We now intercept all URLs beginning with one of the following
78 * prefixes (and *only* these prefixes):
80 * * http://ijbswa.sf.net/config/
81 * * http://ijbswa.sourceforge.net/config/
82 * - New interceptors "home page" - go to http://i.j.b/ to see it.
83 * - Internal changes so that intercepted and fast redirect pages
84 * are not replaced with an image.
85 * - Interceptors now have the option to send a binary page direct
86 * to the client. (i.e. ijb-send-banner uses this)
87 * - Implemented show-url-info interceptor. (Which is why I needed
88 * the above interceptors changes - a typical URL is
89 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
90 * The previous mechanism would not have intercepted that, and
91 * if it had been intercepted then it then it would have replaced
94 * Revision 1.7 2001/05/26 01:26:34 jongfoster
95 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
96 * This #define cannot be set from ./configure - there's no point, it
97 * doesn't work yet. See feature request # 425722
99 * Revision 1.6 2001/05/26 00:31:30 jongfoster
100 * Fixing compiler warning about comparing signed/unsigned.
102 * Revision 1.5 2001/05/26 00:28:36 jongfoster
103 * Automatic reloading of config file.
104 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
105 * Most of the global variables have been moved to a new
106 * struct configuration_spec, accessed through csp->config->globalname
107 * Most of the globals remaining are used by the Win32 GUI.
109 * Revision 1.4 2001/05/22 18:56:28 oes
112 * Revision 1.3 2001/05/20 15:07:54 jongfoster
113 * File is now ignored if _WIN_CONSOLE is defined.
115 * Revision 1.2 2001/05/20 01:21:20 jongfoster
116 * Version 2.9.4 checkin.
117 * - Merged popupfile and cookiefile, and added control over PCRS
118 * filtering, in new "permissionsfile".
119 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
120 * file error you now get a message box (in the Win32 GUI) rather
121 * than the program exiting with no explanation.
122 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
124 * - Removed tabs from "config"
125 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
126 * - Bumped up version number.
128 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
129 * Initial import of version 2.9.3 source tree
132 *********************************************************************/
141 #include <richedit.h>
145 #include "w32taskbar.h"
149 #include "miscutil.h"
153 const char w32res_h_rcs[] = W32RES_H_VERSION;
157 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
160 const char w32log_h_rcs[] = W32LOG_H_VERSION;
162 #ifndef _WIN_CONSOLE /* entire file */
165 * Timers and the various durations
167 #define TIMER_ANIM_ID 1
168 #define TIMER_ANIM_TIME 100
169 #define TIMER_ANIMSTOP_ID 2
170 #define TIMER_ANIMSTOP_TIME 1000
171 #define TIMER_CLIPBUFFER_ID 3
172 #define TIMER_CLIPBUFFER_TIME 1000
173 #define TIMER_CLIPBUFFER_FORCE_ID 4
174 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
177 * Styles of text that can be output
180 #define STYLE_HIGHLIGHT 1
182 #define STYLE_HEADER 3
185 * Number of frames of animation in tray activity sequence
187 #define ANIM_FRAMES 8
189 #define DEFAULT_MAX_BUFFER_LINES 200
190 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
191 #define DEFAULT_LOG_FONT_SIZE 8
194 * These values affect the way the log window behaves, they should be read
195 * from a file but for the moment, they are hardcoded here. Some options are
196 * configurable through the UI.
199 /* Indicates whether task bar shows activity animation */
200 BOOL g_bShowActivityAnimation = 1;
202 /* Indicates if the log window appears on the task bar */
203 BOOL g_bShowOnTaskBar = 0;
205 /* Indicates whether closing the log window really just hides it */
206 BOOL g_bCloseHidesWindow = 1;
208 /* Indicates if messages are logged at all */
209 BOOL g_bLogMessages = 1;
211 /* Indicates whether log messages are highlighted */
212 BOOL g_bHighlightMessages = 1;
214 /* Indicates if buffer is limited in size */
215 BOOL g_bLimitBufferSize = 1;
217 /* Maximum number of lines allowed in buffer when limited */
218 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
221 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
223 /* Size of font to use */
224 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
227 /* FIXME: this is a kludge */
229 const char * g_actions_file = NULL;
230 const char * g_re_filterfile = NULL;
232 const char * g_trustfile = NULL;
233 #endif /* def FEATURE_TRUST */
235 /* FIXME: end kludge */
239 /* Regular expression for detected URLs */
240 #define RE_URL "http:[^ \n\r]*"
243 * Regular expressions that are used to perform highlight in the log window
245 static struct _Pattern
250 } patterns_to_highlight[] =
253 { RE_URL, STYLE_LINK },
254 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
255 /* interesting text to highlight */
256 { "crunch!", STYLE_HIGHLIGHT },
258 { "referer:", STYLE_HEADER },
259 { "proxy-connection:", STYLE_HEADER },
260 { "proxy-agent:", STYLE_HEADER },
261 { "user-agent:", STYLE_HEADER },
262 { "host:", STYLE_HEADER },
263 { "accept:", STYLE_HEADER },
264 { "accept-encoding:", STYLE_HEADER },
265 { "accept-language:", STYLE_HEADER },
266 { "accept-charset:", STYLE_HEADER },
267 { "accept-ranges:", STYLE_HEADER },
268 { "date:", STYLE_HEADER },
269 { "cache-control:", STYLE_HEADER },
270 { "cache-last-checked:", STYLE_HEADER },
271 { "connection:", STYLE_HEADER },
272 { "content-type", STYLE_HEADER },
273 { "content-length", STYLE_HEADER },
274 { "cookie", STYLE_HEADER },
275 { "last-modified:", STYLE_HEADER },
276 { "pragma:", STYLE_HEADER },
277 { "server:", STYLE_HEADER },
278 { "etag:", STYLE_HEADER },
279 { "expires:", STYLE_HEADER },
280 { "warning:", STYLE_HEADER },
281 /* this is the terminator statement - do not delete! */
284 #endif /* def REGEX */
295 static CRITICAL_SECTION g_criticalsection;
296 static HWND g_hwndTray;
297 static HWND g_hwndLogBox;
298 static WNDPROC g_fnLogBox;
299 static HICON g_hiconAnim[ANIM_FRAMES];
300 static HICON g_hiconIdle;
301 static HICON g_hiconApp;
302 static int g_nAnimFrame;
303 static BOOL g_bClipPending = FALSE;
304 static int g_nRichEditVersion = 0;
309 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
310 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
311 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
312 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
313 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
314 static BOOL InitRichEdit(void);
315 static void LogClipBuffer(void);
316 static void LogCreatePatternMatchingBuffers(void);
317 static void LogDestroyPatternMatchingBuffers(void);
318 static int LogPutStringNoMatch(const char *pszText, int style);
321 /*********************************************************************
323 * Function : InitLogWindow
325 * Description : Initialise the log window.
329 * Returns : Always TRUE (there should be error checking on the resources).
331 *********************************************************************/
332 BOOL InitLogWindow(void)
337 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
338 for (i = 0; i < ANIM_FRAMES; i++)
340 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
342 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
344 /* Create the user interface */
345 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
346 g_hwndTray = CreateTrayWindow(g_hInstance);
347 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
349 /* Create pattern matching buffers (for highlighting */
350 LogCreatePatternMatchingBuffers();
352 /* Create a critical section to protect multi-threaded access to certain things */
353 InitializeCriticalSection(&g_criticalsection);
360 /*********************************************************************
362 * Function : TermLogWindow
364 * Description : Cleanup the logwindow.
370 *********************************************************************/
371 void TermLogWindow(void)
375 LogDestroyPatternMatchingBuffers();
377 TrayDeleteIcon(g_hwndTray, 1);
378 DeleteObject(g_hiconApp);
379 DeleteObject(g_hiconIdle);
380 for (i = 0; i < ANIM_FRAMES; i++)
382 DeleteObject(g_hiconAnim[i]);
388 /*********************************************************************
390 * Function : LogCreatePatternMatchingBuffers
392 * Description : Compile the pattern matching buffers.
398 *********************************************************************/
399 void LogCreatePatternMatchingBuffers(void)
403 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
405 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
412 /*********************************************************************
414 * Function : LogDestroyPatternMatchingBuffers
416 * Description : Free up the pattern matching buffers.
422 *********************************************************************/
423 void LogDestroyPatternMatchingBuffers(void)
427 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
429 regfree(&patterns_to_highlight[i].buffer);
436 /*********************************************************************
438 * Function : LogGetURLUnderCursor
440 * Description : Returns the URL from under the cursor (remember to free it!).
444 * Returns : NULL or a pointer to an URL string.
446 *********************************************************************/
447 char *LogGetURLUnderCursor(void)
449 char *szResult = NULL;
455 DWORD nWordStart = 0;
458 regcomp(&re, RE_URL, REG_ICASE);
460 /* Get the position of the cursor over the text window */
461 GetCursorPos(&ptCursor);
462 ScreenToClient(g_hwndLogBox, &ptCursor);
466 /* Search backwards and fowards to obtain the word that is highlighted */
467 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
468 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
469 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
471 /* Compare the string to the pattern */
472 if (nWordEnd > nWordStart)
477 range.chrg.cpMin = nWordStart;
478 range.chrg.cpMax = nWordEnd;
479 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
480 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
482 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
484 szResult = range.lpstrText;
488 free(range.lpstrText);
499 /*********************************************************************
501 * Function : LogPutString
503 * Description : Inserts text into the logging window. This is really
504 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
507 * 1 : pszText = pointer to string going to the log window
509 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
510 * FIXME: this is backwards to the rest of IJB and to common
511 * programming practice. Please use 0 => success instead.
513 *********************************************************************/
514 int LogPutString(const char *pszText)
521 if (pszText == NULL || strlen(pszText) == 0)
531 /* Critical section stops multiple threads doing nasty interactions that
532 * foul up the highlighting and output.
534 EnterCriticalSection(&g_criticalsection);
537 if (g_bHighlightMessages)
541 /* First things first, regexp scan for various things that we would like highlighted */
542 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
544 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
546 char *pszBefore = NULL;
547 char *pszMatch = NULL;
548 char *pszAfter = NULL;
551 /* Split the string up into pieces representing the strings, before
552 at and after the matching pattern
556 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
557 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
558 strncpy(pszBefore, pszText, match.rm_so);
560 if (match.rm_eo < (regoff_t)strlen(pszText))
562 pszAfter = strdup(&pszText[match.rm_eo]);
564 nMatchSize = match.rm_eo - match.rm_so;
565 pszMatch = (char *)malloc(nMatchSize + 1);
566 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
567 pszMatch[nMatchSize] = '\0';
569 /* Recursively call LogPutString */
572 LogPutString(pszBefore);
577 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
582 LogPutString(pszAfter);
593 result = LogPutStringNoMatch(pszText, STYLE_NONE);
598 LeaveCriticalSection(&g_criticalsection);
605 /*********************************************************************
607 * Function : LogPutStringNoMatch
609 * Description : Puts a string into the logging window.
612 * 1 : pszText = pointer to string going to the log window
613 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
615 * Returns : Always 1 => success.
616 * FIXME: this is backwards to the rest of IJB and to common
617 * programming practice. Please use 0 => success instead.
619 *********************************************************************/
620 int LogPutStringNoMatch(const char *pszText, int style)
626 assert(g_hwndLogBox);
627 if (g_hwndLogBox == NULL)
632 /* TODO preserve existing selection */
634 /* Go to the end of the text */
635 nTextLength = GetWindowTextLength(g_hwndLogBox);
636 range.cpMin = nTextLength;
637 range.cpMax = nTextLength;
638 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
640 /* Apply a formatting style */
641 memset(&format, 0, sizeof(format));
642 format.cbSize = sizeof(format);
643 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
644 format.yHeight = (g_nFontSize * 1440) / 72;
645 strcpy(format.szFaceName, g_szFontFaceName);
646 if (style == STYLE_NONE)
649 format.dwEffects |= CFE_AUTOCOLOR;
651 else if (style == STYLE_HEADER)
653 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
655 else if (style == STYLE_HIGHLIGHT)
657 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
659 else if (style == STYLE_LINK)
661 format.dwEffects |= CFE_UNDERLINE;
662 format.crTextColor = RGB(0, 0, 255);
664 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
666 /* Append text to the end */
667 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
669 /* TODO Restore the old selection */
672 if (strchr(pszText, '\n') != NULL)
674 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
677 /* Set the force clip timer going. This timer ensures clipping is done
678 intermittently even when there is a sustained burst of logging
680 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
682 g_bClipPending = TRUE;
690 /*********************************************************************
692 * Function : LogShowActivity
694 * Description : Start the spinner.
700 *********************************************************************/
701 void LogShowActivity(void)
703 /* Start some activity timers */
704 if (g_bShowActivityAnimation)
706 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
707 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
713 /*********************************************************************
715 * Function : LogClipBuffer
717 * Description : Prunes old lines from the log.
723 *********************************************************************/
724 void LogClipBuffer(void)
726 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
727 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
729 /* Compute the range representing the lines to be deleted */
730 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
731 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
734 range.cpMax = nLastChar;
736 /* TODO get current selection */
738 /* TODO adjust and clip old selection against range to be deleted */
740 /* Select range and erase it (turning off autoscroll to prevent
742 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
743 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
744 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
745 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
747 /* Restore old selection */
753 /*********************************************************************
755 * Function : CreateHiddenLogOwnerWindow
757 * Description : Creates a hidden owner window that stops the log
758 * window appearing in the task bar.
761 * 1 : hInstance = application's instance handle
763 * Returns : Handle to newly created window.
765 *********************************************************************/
766 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
768 static const char *szWndName = "JunkbusterLogLogOwner";
773 wc.lpfnWndProc = LogOwnerWindowProc;
776 wc.hInstance = hInstance;
779 wc.hbrBackground = 0;
781 wc.lpszClassName = szWndName;
785 hwnd = CreateWindow(szWndName, szWndName,
786 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
787 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
794 /*********************************************************************
796 * Function : LogOwnerWindowProc
798 * Description : Dummy procedure that does nothing special.
801 * 1 : hwnd = window handle
802 * 2 : uMsg = message number
803 * 3 : wParam = first param for this message
804 * 4 : lParam = next param for this message
806 * Returns : Same as `DefWindowProc'.
808 *********************************************************************/
809 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
811 return DefWindowProc(hwnd, uMsg, wParam, lParam);
816 /*********************************************************************
818 * Function : CreateLogWindow
820 * Description : Create the logging window.
823 * 1 : hInstance = application's instance handle
824 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
826 * Returns : Handle to newly created window.
828 *********************************************************************/
829 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
831 static const char *szWndName = "JunkbusterLogWindow";
832 static const char *szWndTitle = "Junkbuster";
835 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
839 memset(&wc, 0, sizeof(wc));
840 wc.cbSize = sizeof(wc);
841 wc.style = CS_DBLCLKS;
842 wc.lpfnWndProc = LogWindowProc;
845 wc.hInstance = hInstance;
846 wc.hIcon = g_hiconApp;
848 wc.hbrBackground = 0;
849 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
850 wc.lpszClassName = szWndName;
851 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
852 RegisterClassEx(&wc);
854 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
855 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
856 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
858 /* Now create a child list box */
859 GetClientRect(hwnd, &rcClient);
861 /* Create a rich edit control */
863 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
864 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
865 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
866 hwnd, NULL, hInstance, NULL);
867 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
869 /* Subclass the control to catch certain messages */
870 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
871 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
873 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
874 if (!g_bShowOnTaskBar &&
875 (nCmdShow == SW_SHOWMINIMIZED ||
876 nCmdShow == SW_MINIMIZE ||
877 nCmdShow == SW_SHOWMINNOACTIVE))
882 ShowWindow(hwnd, nCmdShow);
885 GetClientRect(g_hwndLogFrame, &rcClient);
886 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
893 /*********************************************************************
895 * Function : InitRichEdit
897 * Description : Initialise the rich edit control library.
901 * Returns : TRUE => success, FALSE => failure.
902 * FIXME: this is backwards to the rest of IJB and to common
903 * programming practice. Please use 0 => success instead.
905 *********************************************************************/
906 BOOL InitRichEdit(void)
908 static HINSTANCE hInstRichEdit;
909 if (hInstRichEdit == NULL)
911 g_nRichEditVersion = 0;
912 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
915 g_nRichEditVersion = _RICHEDIT_VER;
919 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
922 g_nRichEditVersion = 0x0100;
926 return (hInstRichEdit != NULL) ? TRUE : FALSE;
931 /*********************************************************************
933 * Function : ShowLogWindow
935 * Description : Shows or hides the log window. We will also raise the
936 * window on a show command in case it is buried.
939 * 1 : bShow = TRUE to show, FALSE to mimize/hide
943 *********************************************************************/
944 void ShowLogWindow(BOOL bShow)
948 SetForegroundWindow(g_hwndLogFrame);
949 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
951 else if (g_bShowOnTaskBar)
953 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
957 ShowWindow(g_hwndLogFrame, SW_HIDE);
963 /*********************************************************************
965 * Function : EditFile
967 * Description : Opens the specified setting file for editing.
968 * FIXME: What if the file has no associated application. Check for return values
969 * from ShellExecute??
972 * 1 : filename = filename from the config (aka config.txt) file.
976 *********************************************************************/
977 void EditFile(const char *filename)
981 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
987 /*--------------------------------------------------------------------------*/
988 /* Windows message handlers */
989 /*--------------------------------------------------------------------------*/
992 /*********************************************************************
994 * Function : OnLogRButtonUp
996 * Description : Handler for WM_RBUTTONUP messages.
999 * 1 : nModifier = wParam from mouse message (unused)
1000 * 2 : x = x coordinate of the mouse event
1001 * 3 : y = y coordinate of the mouse event
1005 *********************************************************************/
1006 void OnLogRButtonUp(int nModifier, int x, int y)
1008 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1011 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1013 /* Check if there is a selection */
1015 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1016 if (range.cpMin == range.cpMax)
1018 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1022 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1025 /* Display the popup */
1026 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1033 /*********************************************************************
1035 * Function : OnLogCommand
1037 * Description : Handler for WM_COMMAND messages.
1040 * 1 : nCommand = the command portion of the menu selection event
1044 *********************************************************************/
1045 void OnLogCommand(int nCommand)
1050 ShowLogWindow(TRUE);
1054 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1058 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1061 case ID_VIEW_CLEARLOG:
1062 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1065 case ID_VIEW_LOGMESSAGES:
1066 g_bLogMessages = !g_bLogMessages;
1067 /* SaveLogSettings(); */
1070 case ID_VIEW_MESSAGEHIGHLIGHTING:
1071 g_bHighlightMessages = !g_bHighlightMessages;
1072 /* SaveLogSettings(); */
1075 case ID_VIEW_LIMITBUFFERSIZE:
1076 g_bLimitBufferSize = !g_bLimitBufferSize;
1077 /* SaveLogSettings(); */
1080 case ID_VIEW_ACTIVITYANIMATION:
1081 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1082 /* SaveLogSettings(); */
1085 #ifdef FEATURE_TOGGLE
1086 /* by haroon - change toggle to its opposite value */
1088 g_bToggleIJB = !g_bToggleIJB;
1091 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1095 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1098 #endif /* def FEATURE_TOGGLE */
1100 case ID_TOOLS_EDITJUNKBUSTER:
1101 EditFile(configfile);
1104 case ID_TOOLS_EDITACTIONS:
1105 EditFile(g_actions_file);
1108 case ID_TOOLS_EDITPERLRE:
1109 EditFile(g_re_filterfile);
1112 #ifdef FEATURE_TRUST
1113 case ID_TOOLS_EDITTRUST:
1114 EditFile(g_trustfile);
1116 #endif /* def FEATURE_TRUST */
1119 ShellExecute(g_hwndLogFrame, "open", "doc/gpl.html", NULL, NULL, SW_SHOWNORMAL);
1123 ShellExecute(g_hwndLogFrame, "open", "doc/ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1126 case ID_HELP_MANUAL:
1127 ShellExecute(g_hwndLogFrame, "open", "doc/ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1130 case ID_HELP_STATUS:
1131 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1134 case ID_HELP_ABOUTJUNKBUSTER:
1135 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1146 /*********************************************************************
1148 * Function : OnLogInitMenu
1150 * Description : Handler for WM_INITMENU messages. Enable, disable,
1151 * check, and/or uncheck menu options as apropos.
1154 * 1 : hmenu = handle to menu to "make current"
1158 *********************************************************************/
1159 void OnLogInitMenu(HMENU hmenu)
1161 /* Only enable editors if there is a file to edit */
1162 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1163 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1164 #ifdef FEATURE_TRUST
1165 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1166 #endif /* def FEATURE_TRUST */
1168 /* Check/uncheck options */
1169 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1170 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1171 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1172 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1173 #ifdef FEATURE_TOGGLE
1174 /* by haroon - menu item for Enable toggle on/off */
1175 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1176 #endif /* def FEATURE_TOGGLE */
1181 /*********************************************************************
1183 * Function : OnLogTimer
1185 * Description : Handler for WM_TIMER messages.
1188 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1192 *********************************************************************/
1193 void OnLogTimer(int nTimer)
1198 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1201 case TIMER_ANIMSTOP_ID:
1203 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1204 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1205 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1208 case TIMER_CLIPBUFFER_ID:
1209 case TIMER_CLIPBUFFER_FORCE_ID:
1211 g_bClipPending = FALSE;
1212 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1213 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1224 /*********************************************************************
1226 * Function : LogRichEditProc
1228 * Description : Window subclass routine handles some events for the rich edit control.
1231 * 1 : hwnd = window handle of the rich edit control
1232 * 2 : uMsg = message number
1233 * 3 : wParam = first param for this message
1234 * 4 : lParam = next param for this message
1236 * Returns : Appropriate M$ window message handler codes.
1238 *********************************************************************/
1239 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1246 pt.x = LOWORD(lParam);
1247 pt.y = HIWORD(lParam);
1248 ClientToScreen(hwnd, &pt);
1249 OnLogRButtonUp(wParam, pt.x, pt.y);
1253 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1258 /*********************************************************************
1260 * Function : LogWindowProc
1262 * Description : Windows call back routine handles events on the log window.
1265 * 1 : hwnd = handle of the logging window
1266 * 2 : uMsg = message number
1267 * 3 : wParam = first param for this message
1268 * 4 : lParam = next param for this message
1270 * Returns : Appropriate M$ window message handler codes.
1272 *********************************************************************/
1273 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1281 /* This is the end - beautiful friend - the end */
1282 DestroyWindow(g_hwndLogBox);
1283 DestroyWindow(g_hwndLogFrame);
1292 /* Resize the logging window to fit the new frame */
1296 GetClientRect(g_hwndLogFrame, &rc);
1297 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1302 OnLogInitMenu((HMENU) wParam);
1310 OnLogCommand(LOWORD(wParam));
1317 if (g_bCloseHidesWindow)
1319 ShowLogWindow(FALSE);
1324 ShowLogWindow(FALSE);
1330 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1334 #endif /* ndef _WIN_CONSOLE - entire file */