1 const char w32log_rcs[] = "$Id: w32log.c,v 1.1 2001/05/13 21:57:07 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/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.
36 *********************************************************************/
49 #include "w32taskbar.h"
50 #include "w32rulesdlg.h"
57 const char w32res_h_rcs[] = W32RES_H_VERSION;
61 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
64 const char w32log_h_rcs[] = W32LOG_H_VERSION;
67 * Timers and the various durations
69 #define TIMER_ANIM_ID 1
70 #define TIMER_ANIM_TIME 100
71 #define TIMER_ANIMSTOP_ID 2
72 #define TIMER_ANIMSTOP_TIME 1000
73 #define TIMER_CLIPBUFFER_ID 3
74 #define TIMER_CLIPBUFFER_TIME 1000
75 #define TIMER_CLIPBUFFER_FORCE_ID 4
76 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
79 * Styles of text that can be output
82 #define STYLE_HIGHLIGHT 1
84 #define STYLE_HEADER 3
87 * Number of frames of animation in tray activity sequence
91 #define DEFAULT_MAX_BUFFER_LINES 200
92 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
93 #define DEFAULT_LOG_FONT_SIZE 8
96 * These values affect the way the log window behaves, they should be read
97 * from a file but for the moment, they are hardcoded here. Some options are
98 * configurable through the UI.
101 /* Indicates whether task bar shows activity animation */
102 BOOL g_bShowActivityAnimation = 1;
104 /* Indicates if the log window appears on the task bar */
105 BOOL g_bShowOnTaskBar = 0;
107 /* Indicates whether closing the log window really just hides it */
108 BOOL g_bCloseHidesWindow = 1;
110 /* Indicates if messages are logged at all */
111 BOOL g_bLogMessages = 1;
113 /* Indicates whether log messages are highlighted */
114 BOOL g_bHighlightMessages = 1;
116 /* Indicates if buffer is limited in size */
117 BOOL g_bLimitBufferSize = 1;
119 /* Maximum number of lines allowed in buffer when limited */
120 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
123 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
125 /* Size of font to use */
126 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
130 /* Regular expression for detected URLs */
131 #define RE_URL "http:[^ \n\r]*"
134 * Regular expressions that are used to perform highlight in the log window
136 static struct _Pattern
141 } patterns_to_highlight[] =
144 { RE_URL, STYLE_LINK },
145 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
146 /* interesting text to highlight */
147 { "crunch!", STYLE_HIGHLIGHT },
149 { "referer:", STYLE_HEADER },
150 { "proxy-connection:", STYLE_HEADER },
151 { "proxy-agent:", STYLE_HEADER },
152 { "user-agent:", STYLE_HEADER },
153 { "host:", STYLE_HEADER },
154 { "accept:", STYLE_HEADER },
155 { "accept-encoding:", STYLE_HEADER },
156 { "accept-language:", STYLE_HEADER },
157 { "accept-charset:", STYLE_HEADER },
158 { "accept-ranges:", STYLE_HEADER },
159 { "date:", STYLE_HEADER },
160 { "cache-control:", STYLE_HEADER },
161 { "cache-last-checked:", STYLE_HEADER },
162 { "connection:", STYLE_HEADER },
163 { "content-type", STYLE_HEADER },
164 { "content-length", STYLE_HEADER },
165 { "cookie", STYLE_HEADER },
166 { "last-modified:", STYLE_HEADER },
167 { "pragma:", STYLE_HEADER },
168 { "server:", STYLE_HEADER },
169 { "etag:", STYLE_HEADER },
170 { "expires:", STYLE_HEADER },
171 { "warning:", STYLE_HEADER },
172 /* this is the terminator statement - do not delete! */
175 #endif /* def REGEX */
186 static CRITICAL_SECTION g_criticalsection;
187 static HWND g_hwndTray;
188 static HWND g_hwndLogBox;
189 static WNDPROC g_fnLogBox;
190 static HICON g_hiconAnim[ANIM_FRAMES];
191 static HICON g_hiconIdle;
192 static HICON g_hiconApp;
193 static int g_nAnimFrame;
194 static BOOL g_bClipPending = FALSE;
195 static int g_nRichEditVersion = 0;
200 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
201 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
202 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
203 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
204 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
205 static BOOL InitRichEdit(void);
206 static void LogClipBuffer(void);
207 static void LogCreatePatternMatchingBuffers(void);
208 static void LogDestroyPatternMatchingBuffers(void);
209 static int LogPutStringNoMatch(const char *pszText, int style);
212 /*********************************************************************
214 * Function : InitLogWindow
216 * Description : Initialise the log window.
220 * Returns : Always TRUE (there should be error checking on the resources).
222 *********************************************************************/
223 BOOL InitLogWindow(void)
228 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
229 for (i = 0; i < ANIM_FRAMES; i++)
231 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
233 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
235 /* Create the user interface */
236 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
237 g_hwndTray = CreateTrayWindow(g_hInstance);
238 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
240 /* Create pattern matching buffers (for highlighting */
241 LogCreatePatternMatchingBuffers();
243 /* Create a critical section to protect multi-threaded access to certain things */
244 InitializeCriticalSection(&g_criticalsection);
251 /*********************************************************************
253 * Function : TermLogWindow
255 * Description : Cleanup the logwindow.
261 *********************************************************************/
262 void TermLogWindow(void)
266 LogDestroyPatternMatchingBuffers();
268 TrayDeleteIcon(g_hwndTray, 1);
269 DeleteObject(g_hiconApp);
270 DeleteObject(g_hiconIdle);
271 for (i = 0; i < ANIM_FRAMES; i++)
273 DeleteObject(g_hiconAnim[i]);
279 /*********************************************************************
281 * Function : LogCreatePatternMatchingBuffers
283 * Description : Compile the pattern matching buffers.
289 *********************************************************************/
290 void LogCreatePatternMatchingBuffers(void)
294 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
296 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
303 /*********************************************************************
305 * Function : LogDestroyPatternMatchingBuffers
307 * Description : Free up the pattern matching buffers.
313 *********************************************************************/
314 void LogDestroyPatternMatchingBuffers(void)
318 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
320 regfree(&patterns_to_highlight[i].buffer);
327 /*********************************************************************
329 * Function : LogGetURLUnderCursor
331 * Description : Returns the URL from under the cursor (remember to free it!).
335 * Returns : NULL or a pointer to an URL string.
337 *********************************************************************/
338 char *LogGetURLUnderCursor(void)
340 char *szResult = NULL;
346 DWORD nWordStart = 0;
349 regcomp(&re, RE_URL, REG_ICASE);
351 /* Get the position of the cursor over the text window */
352 GetCursorPos(&ptCursor);
353 ScreenToClient(g_hwndLogBox, &ptCursor);
357 /* Search backwards and fowards to obtain the word that is highlighted */
358 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
359 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
360 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
362 /* Compare the string to the pattern */
363 if (nWordEnd > nWordStart)
368 range.chrg.cpMin = nWordStart;
369 range.chrg.cpMax = nWordEnd;
370 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
371 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
373 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
375 szResult = range.lpstrText;
379 free(range.lpstrText);
390 /*********************************************************************
392 * Function : LogPutString
394 * Description : Inserts text into the logging window. This is really
395 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
398 * 1 : pszText = pointer to string going to the log window
400 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
401 * FIXME: this is backwards to the rest of IJB and to common
402 * programming practice. Please use 0 => success instead.
404 *********************************************************************/
405 int LogPutString(const char *pszText)
412 if (pszText == NULL || strlen(pszText) == 0)
422 /* Critical section stops multiple threads doing nasty interactions that
423 * foul up the highlighting and output.
425 EnterCriticalSection(&g_criticalsection);
428 if (g_bHighlightMessages)
432 /* First things first, regexp scan for various things that we would like highlighted */
433 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
435 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
437 char *pszBefore = NULL;
438 char *pszMatch = NULL;
439 char *pszAfter = NULL;
442 /* Split the string up into pieces representing the strings, before
443 at and after the matching pattern
447 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
448 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
449 strncpy(pszBefore, pszText, match.rm_so);
451 if (match.rm_eo < strlen(pszText))
453 pszAfter = strdup(&pszText[match.rm_eo]);
455 nMatchSize = match.rm_eo - match.rm_so;
456 pszMatch = (char *)malloc(nMatchSize + 1);
457 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
458 pszMatch[nMatchSize] = '\0';
460 /* Recursively call LogPutString */
463 LogPutString(pszBefore);
468 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
473 LogPutString(pszAfter);
484 result = LogPutStringNoMatch(pszText, STYLE_NONE);
489 LeaveCriticalSection(&g_criticalsection);
496 /*********************************************************************
498 * Function : LogPutStringNoMatch
500 * Description : Puts a string into the logging window.
503 * 1 : pszText = pointer to string going to the log window
504 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
506 * Returns : Always 1 => success.
507 * FIXME: this is backwards to the rest of IJB and to common
508 * programming practice. Please use 0 => success instead.
510 *********************************************************************/
511 int LogPutStringNoMatch(const char *pszText, int style)
517 assert(g_hwndLogBox);
518 if (g_hwndLogBox == NULL)
523 /* TODO preserve existing selection */
525 /* Go to the end of the text */
526 nTextLength = GetWindowTextLength(g_hwndLogBox);
527 range.cpMin = nTextLength;
528 range.cpMax = nTextLength;
529 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
531 /* Apply a formatting style */
532 memset(&format, 0, sizeof(format));
533 format.cbSize = sizeof(format);
534 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
535 format.yHeight = (g_nFontSize * 1440) / 72;
536 strcpy(format.szFaceName, g_szFontFaceName);
537 if (style == STYLE_NONE)
540 format.dwEffects |= CFE_AUTOCOLOR;
542 else if (style == STYLE_HEADER)
544 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
546 else if (style == STYLE_HIGHLIGHT)
548 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
550 else if (style == STYLE_LINK)
552 format.dwEffects |= CFE_UNDERLINE;
553 format.crTextColor = RGB(0, 0, 255);
555 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
557 /* Append text to the end */
558 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
560 /* TODO Restore the old selection */
563 if (strchr(pszText, '\n') != NULL)
565 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
568 /* Set the force clip timer going. This timer ensures clipping is done
569 intermittently even when there is a sustained burst of logging
571 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
573 g_bClipPending = TRUE;
581 /*********************************************************************
583 * Function : LogShowActivity
585 * Description : Start the spinner.
591 *********************************************************************/
592 void LogShowActivity(void)
594 /* Start some activity timers */
595 if (g_bShowActivityAnimation)
597 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
598 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
604 /*********************************************************************
606 * Function : LogClipBuffer
608 * Description : Prunes old lines from the log.
614 *********************************************************************/
615 void LogClipBuffer(void)
617 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
618 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
620 /* Compute the range representing the lines to be deleted */
621 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
622 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
625 range.cpMax = nLastChar;
627 /* TODO get current selection */
629 /* TODO adjust and clip old selection against range to be deleted */
631 /* Select range and erase it (turning off autoscroll to prevent
633 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
634 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
635 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
636 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
638 /* Restore old selection */
644 /*********************************************************************
646 * Function : CreateHiddenLogOwnerWindow
648 * Description : Creates a hidden owner window that stops the log
649 * window appearing in the task bar.
652 * 1 : hInstance = application's instance handle
654 * Returns : Handle to newly created window.
656 *********************************************************************/
657 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
659 static const char *szWndName = "JunkbusterLogLogOwner";
664 wc.lpfnWndProc = LogOwnerWindowProc;
667 wc.hInstance = hInstance;
670 wc.hbrBackground = 0;
672 wc.lpszClassName = szWndName;
676 hwnd = CreateWindow(szWndName, szWndName,
677 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
678 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
685 /*********************************************************************
687 * Function : LogOwnerWindowProc
689 * Description : Dummy procedure that does nothing special.
692 * 1 : hwnd = window handle
693 * 2 : uMsg = message number
694 * 3 : wParam = first param for this message
695 * 4 : lParam = next param for this message
697 * Returns : Same as `DefWindowProc'.
699 *********************************************************************/
700 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
702 return DefWindowProc(hwnd, uMsg, wParam, lParam);
707 /*********************************************************************
709 * Function : CreateLogWindow
711 * Description : Create the logging window.
714 * 1 : hInstance = application's instance handle
715 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
717 * Returns : Handle to newly created window.
719 *********************************************************************/
720 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
722 static const char *szWndName = "JunkbusterLogWindow";
723 static const char *szWndTitle = "Junkbuster";
726 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
727 HWND hwndChild = NULL;
731 memset(&wc, 0, sizeof(wc));
732 wc.cbSize = sizeof(wc);
733 wc.style = CS_DBLCLKS;
734 wc.lpfnWndProc = LogWindowProc;
737 wc.hInstance = hInstance;
738 wc.hIcon = g_hiconApp;
740 wc.hbrBackground = 0;
741 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
742 wc.lpszClassName = szWndName;
743 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
744 RegisterClassEx(&wc);
746 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
747 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
748 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
750 /* Now create a child list box */
751 GetClientRect(hwnd, &rcClient);
753 /* Create a rich edit control */
755 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
756 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
757 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
758 hwnd, NULL, hInstance, NULL);
759 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
761 /* Subclass the control to catch certain messages */
762 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
763 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
765 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
766 if (!g_bShowOnTaskBar &&
767 (nCmdShow == SW_SHOWMINIMIZED ||
768 nCmdShow == SW_MINIMIZE ||
769 nCmdShow == SW_SHOWMINNOACTIVE))
774 ShowWindow(hwnd, nCmdShow);
777 GetClientRect(g_hwndLogFrame, &rcClient);
778 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
785 /*********************************************************************
787 * Function : InitRichEdit
789 * Description : Initialise the rich edit control library.
793 * Returns : TRUE => success, FALSE => failure.
794 * FIXME: this is backwards to the rest of IJB and to common
795 * programming practice. Please use 0 => success instead.
797 *********************************************************************/
798 BOOL InitRichEdit(void)
800 static HINSTANCE hInstRichEdit;
801 if (hInstRichEdit == NULL)
803 g_nRichEditVersion = 0;
804 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
807 g_nRichEditVersion = _RICHEDIT_VER;
811 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
814 g_nRichEditVersion = 0x0100;
818 return (hInstRichEdit != NULL) ? TRUE : FALSE;
823 /*********************************************************************
825 * Function : ShowLogWindow
827 * Description : Shows or hides the log window. We will also raise the
828 * window on a show command in case it is buried.
831 * 1 : bShow = TRUE to show, FALSE to mimize/hide
835 *********************************************************************/
836 void ShowLogWindow(BOOL bShow)
840 SetForegroundWindow(g_hwndLogFrame);
841 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
843 else if (g_bShowOnTaskBar)
845 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
849 ShowWindow(g_hwndLogFrame, SW_HIDE);
855 /*********************************************************************
857 * Function : EditFile
859 * Description : Opens the specified setting file for editing.
862 * 1 : filename = filename from the config (aka junkbstr.txt) file.
866 *********************************************************************/
867 void EditFile(const char *filename)
871 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
877 /*--------------------------------------------------------------------------*/
878 /* Windows message handlers */
879 /*--------------------------------------------------------------------------*/
882 /*********************************************************************
884 * Function : OnLogRButtonUp
886 * Description : Handler for WM_RBUTTONUP messages.
889 * 1 : nModifier = wParam from mouse message (unused)
890 * 2 : x = x coordinate of the mouse event
891 * 3 : y = y coordinate of the mouse event
895 *********************************************************************/
896 void OnLogRButtonUp(int nModifier, int x, int y)
898 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
901 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
904 /* Check if there is a selection */
906 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
907 if (range.cpMin == range.cpMax)
909 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
913 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
916 /* Check if cursor is over a link */
917 szURL = LogGetURLUnderCursor();
921 TCHAR szMenuItemTemplate[1000];
924 memset(&item, 0, sizeof(item));
925 item.cbSize = sizeof(item);
926 item.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
927 item.fType = MFT_STRING;
928 item.fState = MFS_ENABLED;
929 item.wID = ID_NEW_BLOCKER;
931 /* Put the item into the menu */
932 memset(szMenuItemTemplate, 0, sizeof(szMenuItemTemplate));
933 LoadString(g_hInstance, IDS_NEW_BLOCKER, szMenuItemTemplate, sizeof(szMenuItemTemplate) / sizeof(szMenuItemTemplate[0]));
935 szMenuItem = (char *)malloc(strlen(szMenuItemTemplate) + strlen(szURL) + 1);
936 sprintf(szMenuItem, szMenuItemTemplate, szURL);
938 item.dwTypeData = szMenuItem;
939 item.cch = strlen(szMenuItem);
941 InsertMenuItem(hMenuPopup, 1, TRUE, &item);
943 SetDefaultRule(szURL);
948 /* Display the popup */
949 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
956 /*********************************************************************
958 * Function : OnLogCommand
960 * Description : Handler for WM_COMMAND messages.
963 * 1 : nCommand = the command portion of the menu selection event
967 *********************************************************************/
968 void OnLogCommand(int nCommand)
977 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
981 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
984 case ID_VIEW_CLEARLOG:
985 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
988 case ID_VIEW_LOGMESSAGES:
989 g_bLogMessages = !g_bLogMessages;
990 /* SaveLogSettings(); */
993 case ID_VIEW_MESSAGEHIGHLIGHTING:
994 g_bHighlightMessages = !g_bHighlightMessages;
995 /* SaveLogSettings(); */
998 case ID_VIEW_LIMITBUFFERSIZE:
999 g_bLimitBufferSize = !g_bLimitBufferSize;
1000 /* SaveLogSettings(); */
1003 case ID_VIEW_ACTIVITYANIMATION:
1004 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1005 /* SaveLogSettings(); */
1009 /* by haroon - change toggle to its opposite value */
1011 g_bToggleIJB = !g_bToggleIJB;
1014 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1018 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1023 case ID_RELOAD_CONFIG:
1029 log_error(LOG_LEVEL_ERROR, "load_config encountered a problem! You should probably restart IJB.");
1033 log_error(LOG_LEVEL_INFO, "Configuration has been reloaded.");
1037 case ID_TOOLS_EDITJUNKBUSTER:
1038 EditFile(configfile);
1041 case ID_TOOLS_EDITBLOCKERS:
1042 EditFile(blockfile);
1045 case ID_TOOLS_EDITCOOKIES:
1046 EditFile(cookiefile);
1049 case ID_TOOLS_EDITFORWARD:
1050 EditFile(forwardfile);
1054 case ID_TOOLS_EDITACLS:
1057 #endif /* def ACL_FILES */
1059 #ifdef USE_IMAGE_LIST
1060 case ID_TOOLS_EDITIMAGE:
1061 EditFile(imagefile);
1063 #endif /* def USE_IMAGE_LIST */
1066 case ID_TOOLS_EDITPERLRE:
1067 EditFile(re_filterfile);
1072 case ID_TOOLS_EDITPOPUPS:
1073 EditFile(popupfile);
1075 #endif /* def KILLPOPUPS */
1078 case ID_TOOLS_EDITTRUST:
1079 EditFile(trustfile);
1081 #endif /* def TRUST_FILES */
1083 case ID_NEW_BLOCKER:
1084 ShowRulesDialog(g_hwndLogFrame);
1088 ShellExecute(g_hwndLogFrame, "open", "gpl.html", NULL, NULL, SW_SHOWNORMAL);
1092 ShellExecute(g_hwndLogFrame, "open", "ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1095 case ID_HELP_MANUAL:
1096 ShellExecute(g_hwndLogFrame, "open", "ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1099 case ID_HELP_STATUS:
1100 ShellExecute(g_hwndLogFrame, "open", "Junkbuster Status.URL", NULL, NULL, SW_SHOWNORMAL);
1103 case ID_HELP_ABOUTJUNKBUSTER:
1104 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1115 /*********************************************************************
1117 * Function : OnLogInitMenu
1119 * Description : Handler for WM_INITMENU messages. Enable, disable,
1120 * check, and/or uncheck menu options as apropos.
1123 * 1 : hmenu = handle to menu to "make current"
1127 *********************************************************************/
1128 void OnLogInitMenu(HMENU hmenu)
1130 /* Only enable editors if there is a file to edit */
1131 EnableMenuItem(hmenu, ID_TOOLS_EDITCOOKIES, MF_BYCOMMAND | (cookiefile ? MF_ENABLED : MF_GRAYED));
1132 EnableMenuItem(hmenu, ID_TOOLS_EDITBLOCKERS, MF_BYCOMMAND | (blockfile ? MF_ENABLED : MF_GRAYED));
1133 EnableMenuItem(hmenu, ID_TOOLS_EDITFORWARD, MF_BYCOMMAND | (forwardfile ? MF_ENABLED : MF_GRAYED));
1135 EnableMenuItem(hmenu, ID_TOOLS_EDITACLS, MF_BYCOMMAND | (aclfile ? MF_ENABLED : MF_GRAYED));
1136 #endif /* def ACL_FILES */
1137 #ifdef USE_IMAGE_LIST
1138 EnableMenuItem(hmenu, ID_TOOLS_EDITIMAGE, MF_BYCOMMAND | (imagefile ? MF_ENABLED : MF_GRAYED));
1139 #endif /* def USE_IMAGE_LIST */
1141 EnableMenuItem(hmenu, ID_TOOLS_EDITPOPUPS, MF_BYCOMMAND | (popupfile ? MF_ENABLED : MF_GRAYED));
1142 #endif /* def KILLPOPUPS */
1144 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (re_filterfile ? MF_ENABLED : MF_GRAYED));
1147 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (trustfile ? MF_ENABLED : MF_GRAYED));
1148 #endif /* def TRUST_FILES */
1150 /* Check/uncheck options */
1151 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1152 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1153 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1154 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1156 /* by haroon - menu item for Enable toggle on/off */
1157 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1163 /*********************************************************************
1165 * Function : OnLogTimer
1167 * Description : Handler for WM_TIMER messages.
1170 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1174 *********************************************************************/
1175 void OnLogTimer(int nTimer)
1180 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1183 case TIMER_ANIMSTOP_ID:
1185 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1186 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1187 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1190 case TIMER_CLIPBUFFER_ID:
1191 case TIMER_CLIPBUFFER_FORCE_ID:
1193 g_bClipPending = FALSE;
1194 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1195 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1206 /*********************************************************************
1208 * Function : LogRichEditProc
1210 * Description : Window subclass routine handles some events for the rich edit control.
1213 * 1 : hwnd = window handle of the rich edit control
1214 * 2 : uMsg = message number
1215 * 3 : wParam = first param for this message
1216 * 4 : lParam = next param for this message
1218 * Returns : Appropriate M$ window message handler codes.
1220 *********************************************************************/
1221 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1228 pt.x = LOWORD(lParam);
1229 pt.y = HIWORD(lParam);
1230 ClientToScreen(hwnd, &pt);
1231 OnLogRButtonUp(wParam, pt.x, pt.y);
1235 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1240 /*********************************************************************
1242 * Function : LogWindowProc
1244 * Description : Windows call back routine handles events on the log window.
1247 * 1 : hwnd = handle of the logging window
1248 * 2 : uMsg = message number
1249 * 3 : wParam = first param for this message
1250 * 4 : lParam = next param for this message
1252 * Returns : Appropriate M$ window message handler codes.
1254 *********************************************************************/
1255 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1263 /* This is the end - beautiful friend - the end */
1264 DestroyWindow(g_hwndLogBox);
1265 DestroyWindow(g_hwndLogFrame);
1274 /* Resize the logging window to fit the new frame */
1278 GetClientRect(g_hwndLogFrame, &rc);
1279 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1284 OnLogInitMenu((HMENU) wParam);
1292 OnLogCommand(LOWORD(wParam));
1299 if (g_bCloseHidesWindow)
1301 ShowLogWindow(FALSE);
1306 ShowLogWindow(FALSE);
1312 return DefWindowProc(hwnd, uMsg, wParam, lParam);