1 const char w32log_rcs[] = "$Id: w32log.c,v 1.12 2001/07/13 14:04:59 oes 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.12 2001/07/13 14:04:59 oes
36 * Removed all #ifdef PCRS
38 * Revision 1.11 2001/06/07 23:08:12 jongfoster
39 * Forward and ACL edit options removed.
41 * Revision 1.10 2001/05/31 21:37:11 jongfoster
42 * GUI changes to rename "permissions file" to "actions file".
44 * Revision 1.9 2001/05/31 17:33:13 oes
48 * Revision 1.8 2001/05/29 09:50:24 jongfoster
49 * Unified blocklist/imagelist/permissionslist.
50 * File format is still under discussion, but the internal changes
53 * Also modified interceptor behaviour:
54 * - We now intercept all URLs beginning with one of the following
55 * prefixes (and *only* these prefixes):
57 * * http://ijbswa.sf.net/config/
58 * * http://ijbswa.sourceforge.net/config/
59 * - New interceptors "home page" - go to http://i.j.b/ to see it.
60 * - Internal changes so that intercepted and fast redirect pages
61 * are not replaced with an image.
62 * - Interceptors now have the option to send a binary page direct
63 * to the client. (i.e. ijb-send-banner uses this)
64 * - Implemented show-url-info interceptor. (Which is why I needed
65 * the above interceptors changes - a typical URL is
66 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
67 * The previous mechanism would not have intercepted that, and
68 * if it had been intercepted then it then it would have replaced
71 * Revision 1.7 2001/05/26 01:26:34 jongfoster
72 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
73 * This #define cannot be set from ./configure - there's no point, it
74 * doesn't work yet. See feature request # 425722
76 * Revision 1.6 2001/05/26 00:31:30 jongfoster
77 * Fixing compiler warning about comparing signed/unsigned.
79 * Revision 1.5 2001/05/26 00:28:36 jongfoster
80 * Automatic reloading of config file.
81 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
82 * Most of the global variables have been moved to a new
83 * struct configuration_spec, accessed through csp->config->globalname
84 * Most of the globals remaining are used by the Win32 GUI.
86 * Revision 1.4 2001/05/22 18:56:28 oes
89 * Revision 1.3 2001/05/20 15:07:54 jongfoster
90 * File is now ignored if _WIN_CONSOLE is defined.
92 * Revision 1.2 2001/05/20 01:21:20 jongfoster
93 * Version 2.9.4 checkin.
94 * - Merged popupfile and cookiefile, and added control over PCRS
95 * filtering, in new "permissionsfile".
96 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
97 * file error you now get a message box (in the Win32 GUI) rather
98 * than the program exiting with no explanation.
99 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
101 * - Removed tabs from "config"
102 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
103 * - Bumped up version number.
105 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
106 * Initial import of version 2.9.3 source tree
109 *********************************************************************/
118 #include <richedit.h>
122 #include "w32taskbar.h"
123 #include "w32rulesdlg.h"
127 #include "miscutil.h"
130 const char w32res_h_rcs[] = W32RES_H_VERSION;
134 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
137 const char w32log_h_rcs[] = W32LOG_H_VERSION;
139 #ifndef _WIN_CONSOLE /* entire file */
142 * Timers and the various durations
144 #define TIMER_ANIM_ID 1
145 #define TIMER_ANIM_TIME 100
146 #define TIMER_ANIMSTOP_ID 2
147 #define TIMER_ANIMSTOP_TIME 1000
148 #define TIMER_CLIPBUFFER_ID 3
149 #define TIMER_CLIPBUFFER_TIME 1000
150 #define TIMER_CLIPBUFFER_FORCE_ID 4
151 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
154 * Styles of text that can be output
157 #define STYLE_HIGHLIGHT 1
159 #define STYLE_HEADER 3
162 * Number of frames of animation in tray activity sequence
164 #define ANIM_FRAMES 8
166 #define DEFAULT_MAX_BUFFER_LINES 200
167 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
168 #define DEFAULT_LOG_FONT_SIZE 8
171 * These values affect the way the log window behaves, they should be read
172 * from a file but for the moment, they are hardcoded here. Some options are
173 * configurable through the UI.
176 /* Indicates whether task bar shows activity animation */
177 BOOL g_bShowActivityAnimation = 1;
179 /* Indicates if the log window appears on the task bar */
180 BOOL g_bShowOnTaskBar = 0;
182 /* Indicates whether closing the log window really just hides it */
183 BOOL g_bCloseHidesWindow = 1;
185 /* Indicates if messages are logged at all */
186 BOOL g_bLogMessages = 1;
188 /* Indicates whether log messages are highlighted */
189 BOOL g_bHighlightMessages = 1;
191 /* Indicates if buffer is limited in size */
192 BOOL g_bLimitBufferSize = 1;
194 /* Maximum number of lines allowed in buffer when limited */
195 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
198 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
200 /* Size of font to use */
201 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
204 /* FIXME: this is a kludge */
206 const char * g_actions_file = NULL;
207 const char * g_re_filterfile = NULL;
209 const char * g_trustfile = NULL;
210 #endif /* def TRUST_FILES */
212 /* FIXME: end kludge */
216 /* Regular expression for detected URLs */
217 #define RE_URL "http:[^ \n\r]*"
220 * Regular expressions that are used to perform highlight in the log window
222 static struct _Pattern
227 } patterns_to_highlight[] =
230 { RE_URL, STYLE_LINK },
231 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
232 /* interesting text to highlight */
233 { "crunch!", STYLE_HIGHLIGHT },
235 { "referer:", STYLE_HEADER },
236 { "proxy-connection:", STYLE_HEADER },
237 { "proxy-agent:", STYLE_HEADER },
238 { "user-agent:", STYLE_HEADER },
239 { "host:", STYLE_HEADER },
240 { "accept:", STYLE_HEADER },
241 { "accept-encoding:", STYLE_HEADER },
242 { "accept-language:", STYLE_HEADER },
243 { "accept-charset:", STYLE_HEADER },
244 { "accept-ranges:", STYLE_HEADER },
245 { "date:", STYLE_HEADER },
246 { "cache-control:", STYLE_HEADER },
247 { "cache-last-checked:", STYLE_HEADER },
248 { "connection:", STYLE_HEADER },
249 { "content-type", STYLE_HEADER },
250 { "content-length", STYLE_HEADER },
251 { "cookie", STYLE_HEADER },
252 { "last-modified:", STYLE_HEADER },
253 { "pragma:", STYLE_HEADER },
254 { "server:", STYLE_HEADER },
255 { "etag:", STYLE_HEADER },
256 { "expires:", STYLE_HEADER },
257 { "warning:", STYLE_HEADER },
258 /* this is the terminator statement - do not delete! */
261 #endif /* def REGEX */
272 static CRITICAL_SECTION g_criticalsection;
273 static HWND g_hwndTray;
274 static HWND g_hwndLogBox;
275 static WNDPROC g_fnLogBox;
276 static HICON g_hiconAnim[ANIM_FRAMES];
277 static HICON g_hiconIdle;
278 static HICON g_hiconApp;
279 static int g_nAnimFrame;
280 static BOOL g_bClipPending = FALSE;
281 static int g_nRichEditVersion = 0;
286 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
287 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
288 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
289 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
290 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
291 static BOOL InitRichEdit(void);
292 static void LogClipBuffer(void);
293 static void LogCreatePatternMatchingBuffers(void);
294 static void LogDestroyPatternMatchingBuffers(void);
295 static int LogPutStringNoMatch(const char *pszText, int style);
298 /*********************************************************************
300 * Function : InitLogWindow
302 * Description : Initialise the log window.
306 * Returns : Always TRUE (there should be error checking on the resources).
308 *********************************************************************/
309 BOOL InitLogWindow(void)
314 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
315 for (i = 0; i < ANIM_FRAMES; i++)
317 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
319 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
321 /* Create the user interface */
322 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
323 g_hwndTray = CreateTrayWindow(g_hInstance);
324 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
326 /* Create pattern matching buffers (for highlighting */
327 LogCreatePatternMatchingBuffers();
329 /* Create a critical section to protect multi-threaded access to certain things */
330 InitializeCriticalSection(&g_criticalsection);
337 /*********************************************************************
339 * Function : TermLogWindow
341 * Description : Cleanup the logwindow.
347 *********************************************************************/
348 void TermLogWindow(void)
352 LogDestroyPatternMatchingBuffers();
354 TrayDeleteIcon(g_hwndTray, 1);
355 DeleteObject(g_hiconApp);
356 DeleteObject(g_hiconIdle);
357 for (i = 0; i < ANIM_FRAMES; i++)
359 DeleteObject(g_hiconAnim[i]);
365 /*********************************************************************
367 * Function : LogCreatePatternMatchingBuffers
369 * Description : Compile the pattern matching buffers.
375 *********************************************************************/
376 void LogCreatePatternMatchingBuffers(void)
380 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
382 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
389 /*********************************************************************
391 * Function : LogDestroyPatternMatchingBuffers
393 * Description : Free up the pattern matching buffers.
399 *********************************************************************/
400 void LogDestroyPatternMatchingBuffers(void)
404 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
406 regfree(&patterns_to_highlight[i].buffer);
413 /*********************************************************************
415 * Function : LogGetURLUnderCursor
417 * Description : Returns the URL from under the cursor (remember to free it!).
421 * Returns : NULL or a pointer to an URL string.
423 *********************************************************************/
424 char *LogGetURLUnderCursor(void)
426 char *szResult = NULL;
432 DWORD nWordStart = 0;
435 regcomp(&re, RE_URL, REG_ICASE);
437 /* Get the position of the cursor over the text window */
438 GetCursorPos(&ptCursor);
439 ScreenToClient(g_hwndLogBox, &ptCursor);
443 /* Search backwards and fowards to obtain the word that is highlighted */
444 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
445 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
446 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
448 /* Compare the string to the pattern */
449 if (nWordEnd > nWordStart)
454 range.chrg.cpMin = nWordStart;
455 range.chrg.cpMax = nWordEnd;
456 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
457 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
459 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
461 szResult = range.lpstrText;
465 free(range.lpstrText);
476 /*********************************************************************
478 * Function : LogPutString
480 * Description : Inserts text into the logging window. This is really
481 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
484 * 1 : pszText = pointer to string going to the log window
486 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
487 * FIXME: this is backwards to the rest of IJB and to common
488 * programming practice. Please use 0 => success instead.
490 *********************************************************************/
491 int LogPutString(const char *pszText)
498 if (pszText == NULL || strlen(pszText) == 0)
508 /* Critical section stops multiple threads doing nasty interactions that
509 * foul up the highlighting and output.
511 EnterCriticalSection(&g_criticalsection);
514 if (g_bHighlightMessages)
518 /* First things first, regexp scan for various things that we would like highlighted */
519 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
521 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
523 char *pszBefore = NULL;
524 char *pszMatch = NULL;
525 char *pszAfter = NULL;
528 /* Split the string up into pieces representing the strings, before
529 at and after the matching pattern
533 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
534 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
535 strncpy(pszBefore, pszText, match.rm_so);
537 if (match.rm_eo < (regoff_t)strlen(pszText))
539 pszAfter = strdup(&pszText[match.rm_eo]);
541 nMatchSize = match.rm_eo - match.rm_so;
542 pszMatch = (char *)malloc(nMatchSize + 1);
543 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
544 pszMatch[nMatchSize] = '\0';
546 /* Recursively call LogPutString */
549 LogPutString(pszBefore);
554 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
559 LogPutString(pszAfter);
570 result = LogPutStringNoMatch(pszText, STYLE_NONE);
575 LeaveCriticalSection(&g_criticalsection);
582 /*********************************************************************
584 * Function : LogPutStringNoMatch
586 * Description : Puts a string into the logging window.
589 * 1 : pszText = pointer to string going to the log window
590 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
592 * Returns : Always 1 => success.
593 * FIXME: this is backwards to the rest of IJB and to common
594 * programming practice. Please use 0 => success instead.
596 *********************************************************************/
597 int LogPutStringNoMatch(const char *pszText, int style)
603 assert(g_hwndLogBox);
604 if (g_hwndLogBox == NULL)
609 /* TODO preserve existing selection */
611 /* Go to the end of the text */
612 nTextLength = GetWindowTextLength(g_hwndLogBox);
613 range.cpMin = nTextLength;
614 range.cpMax = nTextLength;
615 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
617 /* Apply a formatting style */
618 memset(&format, 0, sizeof(format));
619 format.cbSize = sizeof(format);
620 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
621 format.yHeight = (g_nFontSize * 1440) / 72;
622 strcpy(format.szFaceName, g_szFontFaceName);
623 if (style == STYLE_NONE)
626 format.dwEffects |= CFE_AUTOCOLOR;
628 else if (style == STYLE_HEADER)
630 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
632 else if (style == STYLE_HIGHLIGHT)
634 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
636 else if (style == STYLE_LINK)
638 format.dwEffects |= CFE_UNDERLINE;
639 format.crTextColor = RGB(0, 0, 255);
641 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
643 /* Append text to the end */
644 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
646 /* TODO Restore the old selection */
649 if (strchr(pszText, '\n') != NULL)
651 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
654 /* Set the force clip timer going. This timer ensures clipping is done
655 intermittently even when there is a sustained burst of logging
657 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
659 g_bClipPending = TRUE;
667 /*********************************************************************
669 * Function : LogShowActivity
671 * Description : Start the spinner.
677 *********************************************************************/
678 void LogShowActivity(void)
680 /* Start some activity timers */
681 if (g_bShowActivityAnimation)
683 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
684 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
690 /*********************************************************************
692 * Function : LogClipBuffer
694 * Description : Prunes old lines from the log.
700 *********************************************************************/
701 void LogClipBuffer(void)
703 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
704 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
706 /* Compute the range representing the lines to be deleted */
707 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
708 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
711 range.cpMax = nLastChar;
713 /* TODO get current selection */
715 /* TODO adjust and clip old selection against range to be deleted */
717 /* Select range and erase it (turning off autoscroll to prevent
719 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
720 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
721 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
722 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
724 /* Restore old selection */
730 /*********************************************************************
732 * Function : CreateHiddenLogOwnerWindow
734 * Description : Creates a hidden owner window that stops the log
735 * window appearing in the task bar.
738 * 1 : hInstance = application's instance handle
740 * Returns : Handle to newly created window.
742 *********************************************************************/
743 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
745 static const char *szWndName = "JunkbusterLogLogOwner";
750 wc.lpfnWndProc = LogOwnerWindowProc;
753 wc.hInstance = hInstance;
756 wc.hbrBackground = 0;
758 wc.lpszClassName = szWndName;
762 hwnd = CreateWindow(szWndName, szWndName,
763 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
764 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
771 /*********************************************************************
773 * Function : LogOwnerWindowProc
775 * Description : Dummy procedure that does nothing special.
778 * 1 : hwnd = window handle
779 * 2 : uMsg = message number
780 * 3 : wParam = first param for this message
781 * 4 : lParam = next param for this message
783 * Returns : Same as `DefWindowProc'.
785 *********************************************************************/
786 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
788 return DefWindowProc(hwnd, uMsg, wParam, lParam);
793 /*********************************************************************
795 * Function : CreateLogWindow
797 * Description : Create the logging window.
800 * 1 : hInstance = application's instance handle
801 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
803 * Returns : Handle to newly created window.
805 *********************************************************************/
806 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
808 static const char *szWndName = "JunkbusterLogWindow";
809 static const char *szWndTitle = "Junkbuster";
812 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
813 HWND hwndChild = NULL;
817 memset(&wc, 0, sizeof(wc));
818 wc.cbSize = sizeof(wc);
819 wc.style = CS_DBLCLKS;
820 wc.lpfnWndProc = LogWindowProc;
823 wc.hInstance = hInstance;
824 wc.hIcon = g_hiconApp;
826 wc.hbrBackground = 0;
827 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
828 wc.lpszClassName = szWndName;
829 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
830 RegisterClassEx(&wc);
832 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
833 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
834 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
836 /* Now create a child list box */
837 GetClientRect(hwnd, &rcClient);
839 /* Create a rich edit control */
841 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
842 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
843 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
844 hwnd, NULL, hInstance, NULL);
845 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
847 /* Subclass the control to catch certain messages */
848 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
849 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
851 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
852 if (!g_bShowOnTaskBar &&
853 (nCmdShow == SW_SHOWMINIMIZED ||
854 nCmdShow == SW_MINIMIZE ||
855 nCmdShow == SW_SHOWMINNOACTIVE))
860 ShowWindow(hwnd, nCmdShow);
863 GetClientRect(g_hwndLogFrame, &rcClient);
864 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
871 /*********************************************************************
873 * Function : InitRichEdit
875 * Description : Initialise the rich edit control library.
879 * Returns : TRUE => success, FALSE => failure.
880 * FIXME: this is backwards to the rest of IJB and to common
881 * programming practice. Please use 0 => success instead.
883 *********************************************************************/
884 BOOL InitRichEdit(void)
886 static HINSTANCE hInstRichEdit;
887 if (hInstRichEdit == NULL)
889 g_nRichEditVersion = 0;
890 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
893 g_nRichEditVersion = _RICHEDIT_VER;
897 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
900 g_nRichEditVersion = 0x0100;
904 return (hInstRichEdit != NULL) ? TRUE : FALSE;
909 /*********************************************************************
911 * Function : ShowLogWindow
913 * Description : Shows or hides the log window. We will also raise the
914 * window on a show command in case it is buried.
917 * 1 : bShow = TRUE to show, FALSE to mimize/hide
921 *********************************************************************/
922 void ShowLogWindow(BOOL bShow)
926 SetForegroundWindow(g_hwndLogFrame);
927 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
929 else if (g_bShowOnTaskBar)
931 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
935 ShowWindow(g_hwndLogFrame, SW_HIDE);
941 /*********************************************************************
943 * Function : EditFile
945 * Description : Opens the specified setting file for editing.
946 * FIXME: What if the file has no associated application. Check for return values
947 * from ShellExecute??
950 * 1 : filename = filename from the config (aka junkbstr.txt) file.
954 *********************************************************************/
955 void EditFile(const char *filename)
959 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
965 /*--------------------------------------------------------------------------*/
966 /* Windows message handlers */
967 /*--------------------------------------------------------------------------*/
970 /*********************************************************************
972 * Function : OnLogRButtonUp
974 * Description : Handler for WM_RBUTTONUP messages.
977 * 1 : nModifier = wParam from mouse message (unused)
978 * 2 : x = x coordinate of the mouse event
979 * 3 : y = y coordinate of the mouse event
983 *********************************************************************/
984 void OnLogRButtonUp(int nModifier, int x, int y)
986 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
989 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
992 #endif /* def WIN_GUI_EDIT */
994 /* Check if there is a selection */
996 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
997 if (range.cpMin == range.cpMax)
999 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1003 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1007 /* Check if cursor is over a link */
1008 szURL = LogGetURLUnderCursor();
1012 TCHAR szMenuItemTemplate[1000];
1015 memset(&item, 0, sizeof(item));
1016 item.cbSize = sizeof(item);
1017 item.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1018 item.fType = MFT_STRING;
1019 item.fState = MFS_ENABLED;
1020 item.wID = ID_NEW_BLOCKER;
1022 /* Put the item into the menu */
1023 memset(szMenuItemTemplate, 0, sizeof(szMenuItemTemplate));
1024 LoadString(g_hInstance, IDS_NEW_BLOCKER, szMenuItemTemplate, sizeof(szMenuItemTemplate) / sizeof(szMenuItemTemplate[0]));
1026 szMenuItem = (char *)malloc(strlen(szMenuItemTemplate) + strlen(szURL) + 1);
1027 sprintf(szMenuItem, szMenuItemTemplate, szURL);
1029 item.dwTypeData = szMenuItem;
1030 item.cch = strlen(szMenuItem);
1032 InsertMenuItem(hMenuPopup, 1, TRUE, &item);
1034 SetDefaultRule(szURL);
1038 #endif /* def WIN_GUI_EDIT */
1040 /* Display the popup */
1041 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1048 /*********************************************************************
1050 * Function : OnLogCommand
1052 * Description : Handler for WM_COMMAND messages.
1055 * 1 : nCommand = the command portion of the menu selection event
1059 *********************************************************************/
1060 void OnLogCommand(int nCommand)
1065 ShowLogWindow(TRUE);
1069 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1073 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1076 case ID_VIEW_CLEARLOG:
1077 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1080 case ID_VIEW_LOGMESSAGES:
1081 g_bLogMessages = !g_bLogMessages;
1082 /* SaveLogSettings(); */
1085 case ID_VIEW_MESSAGEHIGHLIGHTING:
1086 g_bHighlightMessages = !g_bHighlightMessages;
1087 /* SaveLogSettings(); */
1090 case ID_VIEW_LIMITBUFFERSIZE:
1091 g_bLimitBufferSize = !g_bLimitBufferSize;
1092 /* SaveLogSettings(); */
1095 case ID_VIEW_ACTIVITYANIMATION:
1096 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1097 /* SaveLogSettings(); */
1101 /* by haroon - change toggle to its opposite value */
1103 g_bToggleIJB = !g_bToggleIJB;
1106 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1110 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1115 case ID_TOOLS_EDITJUNKBUSTER:
1116 EditFile(configfile);
1119 case ID_TOOLS_EDITACTIONS:
1120 EditFile(g_actions_file);
1123 case ID_TOOLS_EDITPERLRE:
1124 EditFile(g_re_filterfile);
1128 case ID_TOOLS_EDITTRUST:
1129 EditFile(g_trustfile);
1131 #endif /* def TRUST_FILES */
1134 case ID_NEW_BLOCKER:
1135 ShowRulesDialog(g_hwndLogFrame);
1137 #endif /* def WIN_GUI_EDIT */
1140 ShellExecute(g_hwndLogFrame, "open", "gpl.html", NULL, NULL, SW_SHOWNORMAL);
1144 ShellExecute(g_hwndLogFrame, "open", "ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1147 case ID_HELP_MANUAL:
1148 ShellExecute(g_hwndLogFrame, "open", "ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1151 case ID_HELP_STATUS:
1152 ShellExecute(g_hwndLogFrame, "open", "Junkbuster Status.URL", NULL, NULL, SW_SHOWNORMAL);
1155 case ID_HELP_ABOUTJUNKBUSTER:
1156 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1167 /*********************************************************************
1169 * Function : OnLogInitMenu
1171 * Description : Handler for WM_INITMENU messages. Enable, disable,
1172 * check, and/or uncheck menu options as apropos.
1175 * 1 : hmenu = handle to menu to "make current"
1179 *********************************************************************/
1180 void OnLogInitMenu(HMENU hmenu)
1182 /* Only enable editors if there is a file to edit */
1183 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1184 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1186 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1187 #endif /* def TRUST_FILES */
1189 /* Check/uncheck options */
1190 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1191 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1192 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1193 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1195 /* by haroon - menu item for Enable toggle on/off */
1196 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1202 /*********************************************************************
1204 * Function : OnLogTimer
1206 * Description : Handler for WM_TIMER messages.
1209 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1213 *********************************************************************/
1214 void OnLogTimer(int nTimer)
1219 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1222 case TIMER_ANIMSTOP_ID:
1224 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1225 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1226 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1229 case TIMER_CLIPBUFFER_ID:
1230 case TIMER_CLIPBUFFER_FORCE_ID:
1232 g_bClipPending = FALSE;
1233 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1234 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1245 /*********************************************************************
1247 * Function : LogRichEditProc
1249 * Description : Window subclass routine handles some events for the rich edit control.
1252 * 1 : hwnd = window handle of the rich edit control
1253 * 2 : uMsg = message number
1254 * 3 : wParam = first param for this message
1255 * 4 : lParam = next param for this message
1257 * Returns : Appropriate M$ window message handler codes.
1259 *********************************************************************/
1260 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1267 pt.x = LOWORD(lParam);
1268 pt.y = HIWORD(lParam);
1269 ClientToScreen(hwnd, &pt);
1270 OnLogRButtonUp(wParam, pt.x, pt.y);
1274 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1279 /*********************************************************************
1281 * Function : LogWindowProc
1283 * Description : Windows call back routine handles events on the log window.
1286 * 1 : hwnd = handle of the logging window
1287 * 2 : uMsg = message number
1288 * 3 : wParam = first param for this message
1289 * 4 : lParam = next param for this message
1291 * Returns : Appropriate M$ window message handler codes.
1293 *********************************************************************/
1294 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1302 /* This is the end - beautiful friend - the end */
1303 DestroyWindow(g_hwndLogBox);
1304 DestroyWindow(g_hwndLogFrame);
1313 /* Resize the logging window to fit the new frame */
1317 GetClientRect(g_hwndLogFrame, &rc);
1318 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1323 OnLogInitMenu((HMENU) wParam);
1331 OnLogCommand(LOWORD(wParam));
1338 if (g_bCloseHidesWindow)
1340 ShowLogWindow(FALSE);
1345 ShowLogWindow(FALSE);
1351 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1355 #endif /* ndef _WIN_CONSOLE - entire file */