1 const char w32log_rcs[] = "$Id: w32log.c,v 1.13 2001/07/19 19:15:14 haroon 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.13 2001/07/19 19:15:14 haroon
36 * - Added a FIXME for EditFile but didn't fix :-)
38 * Revision 1.12 2001/07/13 14:04:59 oes
39 * Removed all #ifdef PCRS
41 * Revision 1.11 2001/06/07 23:08:12 jongfoster
42 * Forward and ACL edit options removed.
44 * Revision 1.10 2001/05/31 21:37:11 jongfoster
45 * GUI changes to rename "permissions file" to "actions file".
47 * Revision 1.9 2001/05/31 17:33:13 oes
51 * Revision 1.8 2001/05/29 09:50:24 jongfoster
52 * Unified blocklist/imagelist/permissionslist.
53 * File format is still under discussion, but the internal changes
56 * Also modified interceptor behaviour:
57 * - We now intercept all URLs beginning with one of the following
58 * prefixes (and *only* these prefixes):
60 * * http://ijbswa.sf.net/config/
61 * * http://ijbswa.sourceforge.net/config/
62 * - New interceptors "home page" - go to http://i.j.b/ to see it.
63 * - Internal changes so that intercepted and fast redirect pages
64 * are not replaced with an image.
65 * - Interceptors now have the option to send a binary page direct
66 * to the client. (i.e. ijb-send-banner uses this)
67 * - Implemented show-url-info interceptor. (Which is why I needed
68 * the above interceptors changes - a typical URL is
69 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
70 * The previous mechanism would not have intercepted that, and
71 * if it had been intercepted then it then it would have replaced
74 * Revision 1.7 2001/05/26 01:26:34 jongfoster
75 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
76 * This #define cannot be set from ./configure - there's no point, it
77 * doesn't work yet. See feature request # 425722
79 * Revision 1.6 2001/05/26 00:31:30 jongfoster
80 * Fixing compiler warning about comparing signed/unsigned.
82 * Revision 1.5 2001/05/26 00:28:36 jongfoster
83 * Automatic reloading of config file.
84 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
85 * Most of the global variables have been moved to a new
86 * struct configuration_spec, accessed through csp->config->globalname
87 * Most of the globals remaining are used by the Win32 GUI.
89 * Revision 1.4 2001/05/22 18:56:28 oes
92 * Revision 1.3 2001/05/20 15:07:54 jongfoster
93 * File is now ignored if _WIN_CONSOLE is defined.
95 * Revision 1.2 2001/05/20 01:21:20 jongfoster
96 * Version 2.9.4 checkin.
97 * - Merged popupfile and cookiefile, and added control over PCRS
98 * filtering, in new "permissionsfile".
99 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
100 * file error you now get a message box (in the Win32 GUI) rather
101 * than the program exiting with no explanation.
102 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
104 * - Removed tabs from "config"
105 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
106 * - Bumped up version number.
108 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
109 * Initial import of version 2.9.3 source tree
112 *********************************************************************/
121 #include <richedit.h>
125 #include "w32taskbar.h"
126 #include "w32rulesdlg.h"
130 #include "miscutil.h"
134 const char w32res_h_rcs[] = W32RES_H_VERSION;
138 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
141 const char w32log_h_rcs[] = W32LOG_H_VERSION;
143 #ifndef _WIN_CONSOLE /* entire file */
146 * Timers and the various durations
148 #define TIMER_ANIM_ID 1
149 #define TIMER_ANIM_TIME 100
150 #define TIMER_ANIMSTOP_ID 2
151 #define TIMER_ANIMSTOP_TIME 1000
152 #define TIMER_CLIPBUFFER_ID 3
153 #define TIMER_CLIPBUFFER_TIME 1000
154 #define TIMER_CLIPBUFFER_FORCE_ID 4
155 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
158 * Styles of text that can be output
161 #define STYLE_HIGHLIGHT 1
163 #define STYLE_HEADER 3
166 * Number of frames of animation in tray activity sequence
168 #define ANIM_FRAMES 8
170 #define DEFAULT_MAX_BUFFER_LINES 200
171 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
172 #define DEFAULT_LOG_FONT_SIZE 8
175 * These values affect the way the log window behaves, they should be read
176 * from a file but for the moment, they are hardcoded here. Some options are
177 * configurable through the UI.
180 /* Indicates whether task bar shows activity animation */
181 BOOL g_bShowActivityAnimation = 1;
183 /* Indicates if the log window appears on the task bar */
184 BOOL g_bShowOnTaskBar = 0;
186 /* Indicates whether closing the log window really just hides it */
187 BOOL g_bCloseHidesWindow = 1;
189 /* Indicates if messages are logged at all */
190 BOOL g_bLogMessages = 1;
192 /* Indicates whether log messages are highlighted */
193 BOOL g_bHighlightMessages = 1;
195 /* Indicates if buffer is limited in size */
196 BOOL g_bLimitBufferSize = 1;
198 /* Maximum number of lines allowed in buffer when limited */
199 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
202 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
204 /* Size of font to use */
205 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
208 /* FIXME: this is a kludge */
210 const char * g_actions_file = NULL;
211 const char * g_re_filterfile = NULL;
213 const char * g_trustfile = NULL;
214 #endif /* def TRUST_FILES */
216 /* FIXME: end kludge */
220 /* Regular expression for detected URLs */
221 #define RE_URL "http:[^ \n\r]*"
224 * Regular expressions that are used to perform highlight in the log window
226 static struct _Pattern
231 } patterns_to_highlight[] =
234 { RE_URL, STYLE_LINK },
235 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
236 /* interesting text to highlight */
237 { "crunch!", STYLE_HIGHLIGHT },
239 { "referer:", STYLE_HEADER },
240 { "proxy-connection:", STYLE_HEADER },
241 { "proxy-agent:", STYLE_HEADER },
242 { "user-agent:", STYLE_HEADER },
243 { "host:", STYLE_HEADER },
244 { "accept:", STYLE_HEADER },
245 { "accept-encoding:", STYLE_HEADER },
246 { "accept-language:", STYLE_HEADER },
247 { "accept-charset:", STYLE_HEADER },
248 { "accept-ranges:", STYLE_HEADER },
249 { "date:", STYLE_HEADER },
250 { "cache-control:", STYLE_HEADER },
251 { "cache-last-checked:", STYLE_HEADER },
252 { "connection:", STYLE_HEADER },
253 { "content-type", STYLE_HEADER },
254 { "content-length", STYLE_HEADER },
255 { "cookie", STYLE_HEADER },
256 { "last-modified:", STYLE_HEADER },
257 { "pragma:", STYLE_HEADER },
258 { "server:", STYLE_HEADER },
259 { "etag:", STYLE_HEADER },
260 { "expires:", STYLE_HEADER },
261 { "warning:", STYLE_HEADER },
262 /* this is the terminator statement - do not delete! */
265 #endif /* def REGEX */
276 static CRITICAL_SECTION g_criticalsection;
277 static HWND g_hwndTray;
278 static HWND g_hwndLogBox;
279 static WNDPROC g_fnLogBox;
280 static HICON g_hiconAnim[ANIM_FRAMES];
281 static HICON g_hiconIdle;
282 static HICON g_hiconApp;
283 static int g_nAnimFrame;
284 static BOOL g_bClipPending = FALSE;
285 static int g_nRichEditVersion = 0;
290 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
291 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
292 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
294 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
295 static BOOL InitRichEdit(void);
296 static void LogClipBuffer(void);
297 static void LogCreatePatternMatchingBuffers(void);
298 static void LogDestroyPatternMatchingBuffers(void);
299 static int LogPutStringNoMatch(const char *pszText, int style);
302 /*********************************************************************
304 * Function : InitLogWindow
306 * Description : Initialise the log window.
310 * Returns : Always TRUE (there should be error checking on the resources).
312 *********************************************************************/
313 BOOL InitLogWindow(void)
318 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
319 for (i = 0; i < ANIM_FRAMES; i++)
321 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
323 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
325 /* Create the user interface */
326 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
327 g_hwndTray = CreateTrayWindow(g_hInstance);
328 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
330 /* Create pattern matching buffers (for highlighting */
331 LogCreatePatternMatchingBuffers();
333 /* Create a critical section to protect multi-threaded access to certain things */
334 InitializeCriticalSection(&g_criticalsection);
341 /*********************************************************************
343 * Function : TermLogWindow
345 * Description : Cleanup the logwindow.
351 *********************************************************************/
352 void TermLogWindow(void)
356 LogDestroyPatternMatchingBuffers();
358 TrayDeleteIcon(g_hwndTray, 1);
359 DeleteObject(g_hiconApp);
360 DeleteObject(g_hiconIdle);
361 for (i = 0; i < ANIM_FRAMES; i++)
363 DeleteObject(g_hiconAnim[i]);
369 /*********************************************************************
371 * Function : LogCreatePatternMatchingBuffers
373 * Description : Compile the pattern matching buffers.
379 *********************************************************************/
380 void LogCreatePatternMatchingBuffers(void)
384 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
386 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
393 /*********************************************************************
395 * Function : LogDestroyPatternMatchingBuffers
397 * Description : Free up the pattern matching buffers.
403 *********************************************************************/
404 void LogDestroyPatternMatchingBuffers(void)
408 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
410 regfree(&patterns_to_highlight[i].buffer);
417 /*********************************************************************
419 * Function : LogGetURLUnderCursor
421 * Description : Returns the URL from under the cursor (remember to free it!).
425 * Returns : NULL or a pointer to an URL string.
427 *********************************************************************/
428 char *LogGetURLUnderCursor(void)
430 char *szResult = NULL;
436 DWORD nWordStart = 0;
439 regcomp(&re, RE_URL, REG_ICASE);
441 /* Get the position of the cursor over the text window */
442 GetCursorPos(&ptCursor);
443 ScreenToClient(g_hwndLogBox, &ptCursor);
447 /* Search backwards and fowards to obtain the word that is highlighted */
448 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
449 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
450 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
452 /* Compare the string to the pattern */
453 if (nWordEnd > nWordStart)
458 range.chrg.cpMin = nWordStart;
459 range.chrg.cpMax = nWordEnd;
460 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
461 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
463 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
465 szResult = range.lpstrText;
469 free(range.lpstrText);
480 /*********************************************************************
482 * Function : LogPutString
484 * Description : Inserts text into the logging window. This is really
485 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
488 * 1 : pszText = pointer to string going to the log window
490 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
491 * FIXME: this is backwards to the rest of IJB and to common
492 * programming practice. Please use 0 => success instead.
494 *********************************************************************/
495 int LogPutString(const char *pszText)
502 if (pszText == NULL || strlen(pszText) == 0)
512 /* Critical section stops multiple threads doing nasty interactions that
513 * foul up the highlighting and output.
515 EnterCriticalSection(&g_criticalsection);
518 if (g_bHighlightMessages)
522 /* First things first, regexp scan for various things that we would like highlighted */
523 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
525 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
527 char *pszBefore = NULL;
528 char *pszMatch = NULL;
529 char *pszAfter = NULL;
532 /* Split the string up into pieces representing the strings, before
533 at and after the matching pattern
537 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
538 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
539 strncpy(pszBefore, pszText, match.rm_so);
541 if (match.rm_eo < (regoff_t)strlen(pszText))
543 pszAfter = strdup(&pszText[match.rm_eo]);
545 nMatchSize = match.rm_eo - match.rm_so;
546 pszMatch = (char *)malloc(nMatchSize + 1);
547 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
548 pszMatch[nMatchSize] = '\0';
550 /* Recursively call LogPutString */
553 LogPutString(pszBefore);
558 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
563 LogPutString(pszAfter);
574 result = LogPutStringNoMatch(pszText, STYLE_NONE);
579 LeaveCriticalSection(&g_criticalsection);
586 /*********************************************************************
588 * Function : LogPutStringNoMatch
590 * Description : Puts a string into the logging window.
593 * 1 : pszText = pointer to string going to the log window
594 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
596 * Returns : Always 1 => success.
597 * FIXME: this is backwards to the rest of IJB and to common
598 * programming practice. Please use 0 => success instead.
600 *********************************************************************/
601 int LogPutStringNoMatch(const char *pszText, int style)
607 assert(g_hwndLogBox);
608 if (g_hwndLogBox == NULL)
613 /* TODO preserve existing selection */
615 /* Go to the end of the text */
616 nTextLength = GetWindowTextLength(g_hwndLogBox);
617 range.cpMin = nTextLength;
618 range.cpMax = nTextLength;
619 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
621 /* Apply a formatting style */
622 memset(&format, 0, sizeof(format));
623 format.cbSize = sizeof(format);
624 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
625 format.yHeight = (g_nFontSize * 1440) / 72;
626 strcpy(format.szFaceName, g_szFontFaceName);
627 if (style == STYLE_NONE)
630 format.dwEffects |= CFE_AUTOCOLOR;
632 else if (style == STYLE_HEADER)
634 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
636 else if (style == STYLE_HIGHLIGHT)
638 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
640 else if (style == STYLE_LINK)
642 format.dwEffects |= CFE_UNDERLINE;
643 format.crTextColor = RGB(0, 0, 255);
645 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
647 /* Append text to the end */
648 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
650 /* TODO Restore the old selection */
653 if (strchr(pszText, '\n') != NULL)
655 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
658 /* Set the force clip timer going. This timer ensures clipping is done
659 intermittently even when there is a sustained burst of logging
661 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
663 g_bClipPending = TRUE;
671 /*********************************************************************
673 * Function : LogShowActivity
675 * Description : Start the spinner.
681 *********************************************************************/
682 void LogShowActivity(void)
684 /* Start some activity timers */
685 if (g_bShowActivityAnimation)
687 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
688 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
694 /*********************************************************************
696 * Function : LogClipBuffer
698 * Description : Prunes old lines from the log.
704 *********************************************************************/
705 void LogClipBuffer(void)
707 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
708 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
710 /* Compute the range representing the lines to be deleted */
711 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
712 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
715 range.cpMax = nLastChar;
717 /* TODO get current selection */
719 /* TODO adjust and clip old selection against range to be deleted */
721 /* Select range and erase it (turning off autoscroll to prevent
723 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
724 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
725 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
726 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
728 /* Restore old selection */
734 /*********************************************************************
736 * Function : CreateHiddenLogOwnerWindow
738 * Description : Creates a hidden owner window that stops the log
739 * window appearing in the task bar.
742 * 1 : hInstance = application's instance handle
744 * Returns : Handle to newly created window.
746 *********************************************************************/
747 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
749 static const char *szWndName = "JunkbusterLogLogOwner";
754 wc.lpfnWndProc = LogOwnerWindowProc;
757 wc.hInstance = hInstance;
760 wc.hbrBackground = 0;
762 wc.lpszClassName = szWndName;
766 hwnd = CreateWindow(szWndName, szWndName,
767 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
768 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
775 /*********************************************************************
777 * Function : LogOwnerWindowProc
779 * Description : Dummy procedure that does nothing special.
782 * 1 : hwnd = window handle
783 * 2 : uMsg = message number
784 * 3 : wParam = first param for this message
785 * 4 : lParam = next param for this message
787 * Returns : Same as `DefWindowProc'.
789 *********************************************************************/
790 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
792 return DefWindowProc(hwnd, uMsg, wParam, lParam);
797 /*********************************************************************
799 * Function : CreateLogWindow
801 * Description : Create the logging window.
804 * 1 : hInstance = application's instance handle
805 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
807 * Returns : Handle to newly created window.
809 *********************************************************************/
810 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
812 static const char *szWndName = "JunkbusterLogWindow";
813 static const char *szWndTitle = "Junkbuster";
816 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
817 HWND hwndChild = NULL;
821 memset(&wc, 0, sizeof(wc));
822 wc.cbSize = sizeof(wc);
823 wc.style = CS_DBLCLKS;
824 wc.lpfnWndProc = LogWindowProc;
827 wc.hInstance = hInstance;
828 wc.hIcon = g_hiconApp;
830 wc.hbrBackground = 0;
831 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
832 wc.lpszClassName = szWndName;
833 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
834 RegisterClassEx(&wc);
836 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
837 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
838 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
840 /* Now create a child list box */
841 GetClientRect(hwnd, &rcClient);
843 /* Create a rich edit control */
845 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
846 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
847 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
848 hwnd, NULL, hInstance, NULL);
849 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
851 /* Subclass the control to catch certain messages */
852 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
853 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
855 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
856 if (!g_bShowOnTaskBar &&
857 (nCmdShow == SW_SHOWMINIMIZED ||
858 nCmdShow == SW_MINIMIZE ||
859 nCmdShow == SW_SHOWMINNOACTIVE))
864 ShowWindow(hwnd, nCmdShow);
867 GetClientRect(g_hwndLogFrame, &rcClient);
868 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
875 /*********************************************************************
877 * Function : InitRichEdit
879 * Description : Initialise the rich edit control library.
883 * Returns : TRUE => success, FALSE => failure.
884 * FIXME: this is backwards to the rest of IJB and to common
885 * programming practice. Please use 0 => success instead.
887 *********************************************************************/
888 BOOL InitRichEdit(void)
890 static HINSTANCE hInstRichEdit;
891 if (hInstRichEdit == NULL)
893 g_nRichEditVersion = 0;
894 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
897 g_nRichEditVersion = _RICHEDIT_VER;
901 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
904 g_nRichEditVersion = 0x0100;
908 return (hInstRichEdit != NULL) ? TRUE : FALSE;
913 /*********************************************************************
915 * Function : ShowLogWindow
917 * Description : Shows or hides the log window. We will also raise the
918 * window on a show command in case it is buried.
921 * 1 : bShow = TRUE to show, FALSE to mimize/hide
925 *********************************************************************/
926 void ShowLogWindow(BOOL bShow)
930 SetForegroundWindow(g_hwndLogFrame);
931 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
933 else if (g_bShowOnTaskBar)
935 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
939 ShowWindow(g_hwndLogFrame, SW_HIDE);
945 /*********************************************************************
947 * Function : EditFile
949 * Description : Opens the specified setting file for editing.
950 * FIXME: What if the file has no associated application. Check for return values
951 * from ShellExecute??
954 * 1 : filename = filename from the config (aka junkbstr.txt) file.
958 *********************************************************************/
959 void EditFile(const char *filename)
963 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
969 /*--------------------------------------------------------------------------*/
970 /* Windows message handlers */
971 /*--------------------------------------------------------------------------*/
974 /*********************************************************************
976 * Function : OnLogRButtonUp
978 * Description : Handler for WM_RBUTTONUP messages.
981 * 1 : nModifier = wParam from mouse message (unused)
982 * 2 : x = x coordinate of the mouse event
983 * 3 : y = y coordinate of the mouse event
987 *********************************************************************/
988 void OnLogRButtonUp(int nModifier, int x, int y)
990 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
993 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
996 #endif /* def WIN_GUI_EDIT */
998 /* Check if there is a selection */
1000 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1001 if (range.cpMin == range.cpMax)
1003 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1007 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1011 /* Check if cursor is over a link */
1012 szURL = LogGetURLUnderCursor();
1016 TCHAR szMenuItemTemplate[1000];
1019 memset(&item, 0, sizeof(item));
1020 item.cbSize = sizeof(item);
1021 item.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1022 item.fType = MFT_STRING;
1023 item.fState = MFS_ENABLED;
1024 item.wID = ID_NEW_BLOCKER;
1026 /* Put the item into the menu */
1027 memset(szMenuItemTemplate, 0, sizeof(szMenuItemTemplate));
1028 LoadString(g_hInstance, IDS_NEW_BLOCKER, szMenuItemTemplate, sizeof(szMenuItemTemplate) / sizeof(szMenuItemTemplate[0]));
1030 szMenuItem = (char *)malloc(strlen(szMenuItemTemplate) + strlen(szURL) + 1);
1031 sprintf(szMenuItem, szMenuItemTemplate, szURL);
1033 item.dwTypeData = szMenuItem;
1034 item.cch = strlen(szMenuItem);
1036 InsertMenuItem(hMenuPopup, 1, TRUE, &item);
1038 SetDefaultRule(szURL);
1042 #endif /* def WIN_GUI_EDIT */
1044 /* Display the popup */
1045 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1052 /*********************************************************************
1054 * Function : OnLogCommand
1056 * Description : Handler for WM_COMMAND messages.
1059 * 1 : nCommand = the command portion of the menu selection event
1063 *********************************************************************/
1064 void OnLogCommand(int nCommand)
1069 ShowLogWindow(TRUE);
1073 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1077 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1080 case ID_VIEW_CLEARLOG:
1081 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1084 case ID_VIEW_LOGMESSAGES:
1085 g_bLogMessages = !g_bLogMessages;
1086 /* SaveLogSettings(); */
1089 case ID_VIEW_MESSAGEHIGHLIGHTING:
1090 g_bHighlightMessages = !g_bHighlightMessages;
1091 /* SaveLogSettings(); */
1094 case ID_VIEW_LIMITBUFFERSIZE:
1095 g_bLimitBufferSize = !g_bLimitBufferSize;
1096 /* SaveLogSettings(); */
1099 case ID_VIEW_ACTIVITYANIMATION:
1100 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1101 /* SaveLogSettings(); */
1105 /* by haroon - change toggle to its opposite value */
1107 g_bToggleIJB = !g_bToggleIJB;
1110 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1114 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1119 case ID_TOOLS_EDITJUNKBUSTER:
1120 EditFile(configfile);
1123 case ID_TOOLS_EDITACTIONS:
1124 EditFile(g_actions_file);
1127 case ID_TOOLS_EDITPERLRE:
1128 EditFile(g_re_filterfile);
1132 case ID_TOOLS_EDITTRUST:
1133 EditFile(g_trustfile);
1135 #endif /* def TRUST_FILES */
1138 case ID_NEW_BLOCKER:
1139 ShowRulesDialog(g_hwndLogFrame);
1141 #endif /* def WIN_GUI_EDIT */
1144 ShellExecute(g_hwndLogFrame, "open", "gpl.html", NULL, NULL, SW_SHOWNORMAL);
1148 ShellExecute(g_hwndLogFrame, "open", "ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1151 case ID_HELP_MANUAL:
1152 ShellExecute(g_hwndLogFrame, "open", "ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1155 case ID_HELP_STATUS:
1156 ShellExecute(g_hwndLogFrame, "open", "Junkbuster Status.URL", NULL, NULL, SW_SHOWNORMAL);
1159 case ID_HELP_ABOUTJUNKBUSTER:
1160 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1171 /*********************************************************************
1173 * Function : OnLogInitMenu
1175 * Description : Handler for WM_INITMENU messages. Enable, disable,
1176 * check, and/or uncheck menu options as apropos.
1179 * 1 : hmenu = handle to menu to "make current"
1183 *********************************************************************/
1184 void OnLogInitMenu(HMENU hmenu)
1186 /* Only enable editors if there is a file to edit */
1187 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1188 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1190 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1191 #endif /* def TRUST_FILES */
1193 /* Check/uncheck options */
1194 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1195 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1196 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1197 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1199 /* by haroon - menu item for Enable toggle on/off */
1200 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1206 /*********************************************************************
1208 * Function : OnLogTimer
1210 * Description : Handler for WM_TIMER messages.
1213 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1217 *********************************************************************/
1218 void OnLogTimer(int nTimer)
1223 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1226 case TIMER_ANIMSTOP_ID:
1228 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1229 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1230 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1233 case TIMER_CLIPBUFFER_ID:
1234 case TIMER_CLIPBUFFER_FORCE_ID:
1236 g_bClipPending = FALSE;
1237 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1238 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1249 /*********************************************************************
1251 * Function : LogRichEditProc
1253 * Description : Window subclass routine handles some events for the rich edit control.
1256 * 1 : hwnd = window handle of the rich edit control
1257 * 2 : uMsg = message number
1258 * 3 : wParam = first param for this message
1259 * 4 : lParam = next param for this message
1261 * Returns : Appropriate M$ window message handler codes.
1263 *********************************************************************/
1264 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1271 pt.x = LOWORD(lParam);
1272 pt.y = HIWORD(lParam);
1273 ClientToScreen(hwnd, &pt);
1274 OnLogRButtonUp(wParam, pt.x, pt.y);
1278 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1283 /*********************************************************************
1285 * Function : LogWindowProc
1287 * Description : Windows call back routine handles events on the log window.
1290 * 1 : hwnd = handle of the logging window
1291 * 2 : uMsg = message number
1292 * 3 : wParam = first param for this message
1293 * 4 : lParam = next param for this message
1295 * Returns : Appropriate M$ window message handler codes.
1297 *********************************************************************/
1298 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1306 /* This is the end - beautiful friend - the end */
1307 DestroyWindow(g_hwndLogBox);
1308 DestroyWindow(g_hwndLogFrame);
1317 /* Resize the logging window to fit the new frame */
1321 GetClientRect(g_hwndLogFrame, &rc);
1322 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1327 OnLogInitMenu((HMENU) wParam);
1335 OnLogCommand(LOWORD(wParam));
1342 if (g_bCloseHidesWindow)
1344 ShowLogWindow(FALSE);
1349 ShowLogWindow(FALSE);
1355 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1359 #endif /* ndef _WIN_CONSOLE - entire file */