Cosmetic and very minor fix of markup.
[privoxy.git] / w32log.c
1 const char w32log_rcs[] = "$Id: w32log.c,v 1.25.2.3 2002/11/20 14:39:05 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/Attic/w32log.c,v $
5  *
6  * Purpose     :  Functions for creating and destroying the log window,
7  *                ouputting strings, processing messages and so on.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2002 members of
10  *                the Privoxy team.  http://www.privoxy.org/
11  *
12  *                Written by and Copyright (C) 1999 Adam Lock
13  *                <locka@iol.ie>
14  *
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.
20  *
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.
26  *
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.
32  *
33  * Revisions   :
34  *    $Log: w32log.c,v $
35  *    Revision 1.25.2.3  2002/11/20 14:39:05  oes
36  *    Fixed compiler warning
37  *
38  *    Revision 1.25.2.2  2002/09/25 15:23:10  oes
39  *    Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
40  *
41  *    Revision 1.25.2.1  2002/08/21 17:59:05  oes
42  *     - "Show Privoxy Window" now a toggle
43  *     - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
44  *
45  *    Revision 1.25  2002/04/04 00:36:36  gliptak
46  *    always use pcre for matching
47  *
48  *    Revision 1.24  2002/03/31 17:19:00  jongfoster
49  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
50  *
51  *    Revision 1.23  2002/03/26 22:57:10  jongfoster
52  *    Web server name should begin www.
53  *
54  *    Revision 1.22  2002/03/24 12:48:23  jongfoster
55  *    Fixing doc links
56  *
57  *    Revision 1.21  2002/03/24 12:07:35  jongfoster
58  *    Consistern name for filters file
59  *
60  *    Revision 1.20  2002/03/24 12:03:47  jongfoster
61  *    Name change
62  *
63  *    Revision 1.19  2002/01/17 21:04:17  jongfoster
64  *    Replacing hard references to the URL of the config interface
65  *    with #defines from project.h
66  *
67  *    Revision 1.18  2001/11/30 23:37:24  jongfoster
68  *    Renaming the Win32 config file to config.txt - this is almost the
69  *    same as the corresponding UNIX name "config"
70  *
71  *    Revision 1.17  2001/11/16 00:46:31  jongfoster
72  *    Fixing compiler warnings
73  *
74  *    Revision 1.16  2001/08/01 19:58:12  jongfoster
75  *    Fixing documentation filenames in help menu, and making status
76  *    option work without needing the "Junkbuster Status.URL" file.
77  *
78  *    Revision 1.15  2001/07/30 22:08:36  jongfoster
79  *    Tidying up #defines:
80  *    - All feature #defines are now of the form FEATURE_xxx
81  *    - Permanently turned off WIN_GUI_EDIT
82  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
83  *
84  *    Revision 1.14  2001/07/29 18:47:05  jongfoster
85  *    Adding missing #include "loadcfg.h"
86  *
87  *    Revision 1.13  2001/07/19 19:15:14  haroon
88  *    - Added a FIXME for EditFile but didn't fix :-)
89  *
90  *    Revision 1.12  2001/07/13 14:04:59  oes
91  *    Removed all #ifdef PCRS
92  *
93  *    Revision 1.11  2001/06/07 23:08:12  jongfoster
94  *    Forward and ACL edit options removed.
95  *
96  *    Revision 1.10  2001/05/31 21:37:11  jongfoster
97  *    GUI changes to rename "permissions file" to "actions file".
98  *
99  *    Revision 1.9  2001/05/31 17:33:13  oes
100  *
101  *    CRLF -> LF
102  *
103  *    Revision 1.8  2001/05/29 09:50:24  jongfoster
104  *    Unified blocklist/imagelist/permissionslist.
105  *    File format is still under discussion, but the internal changes
106  *    are (mostly) done.
107  *
108  *    Also modified interceptor behaviour:
109  *    - We now intercept all URLs beginning with one of the following
110  *      prefixes (and *only* these prefixes):
111  *        * http://i.j.b/
112  *        * http://ijbswa.sf.net/config/
113  *        * http://ijbswa.sourceforge.net/config/
114  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
115  *    - Internal changes so that intercepted and fast redirect pages
116  *      are not replaced with an image.
117  *    - Interceptors now have the option to send a binary page direct
118  *      to the client. (i.e. ijb-send-banner uses this)
119  *    - Implemented show-url-info interceptor.  (Which is why I needed
120  *      the above interceptors changes - a typical URL is
121  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
122  *      The previous mechanism would not have intercepted that, and
123  *      if it had been intercepted then it then it would have replaced
124  *      it with an image.)
125  *
126  *    Revision 1.7  2001/05/26 01:26:34  jongfoster
127  *    New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
128  *    This #define cannot be set from ./configure - there's no point, it
129  *    doesn't work yet.  See feature request # 425722
130  *
131  *    Revision 1.6  2001/05/26 00:31:30  jongfoster
132  *    Fixing compiler warning about comparing signed/unsigned.
133  *
134  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
135  *    Automatic reloading of config file.
136  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
137  *    Most of the global variables have been moved to a new
138  *    struct configuration_spec, accessed through csp->config->globalname
139  *    Most of the globals remaining are used by the Win32 GUI.
140  *
141  *    Revision 1.4  2001/05/22 18:56:28  oes
142  *    CRLF -> LF
143  *
144  *    Revision 1.3  2001/05/20 15:07:54  jongfoster
145  *    File is now ignored if _WIN_CONSOLE is defined.
146  *
147  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
148  *    Version 2.9.4 checkin.
149  *    - Merged popupfile and cookiefile, and added control over PCRS
150  *      filtering, in new "permissionsfile".
151  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
152  *      file error you now get a message box (in the Win32 GUI) rather
153  *      than the program exiting with no explanation.
154  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
155  *      skipping.
156  *    - Removed tabs from "config"
157  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
158  *    - Bumped up version number.
159  *
160  *    Revision 1.1.1.1  2001/05/15 13:59:07  oes
161  *    Initial import of version 2.9.3 source tree
162  *
163  *
164  *********************************************************************/
165 \f
166
167 #include "config.h"
168
169 #include <assert.h>
170 #include <stdio.h>
171
172 #ifndef STRICT
173 #define STRICT
174 #endif
175 #include <windows.h>
176 #include <richedit.h>
177
178 #include "project.h"
179 #include "w32log.h"
180 #include "w32taskbar.h"
181 #include "win32.h"
182 #include "w32res.h"
183 #include "jcc.h"
184 #include "miscutil.h"
185 #include "errlog.h"
186 #include "loadcfg.h"
187
188 const char w32res_h_rcs[] = W32RES_H_VERSION;
189
190 #ifdef __MINGW32__
191 #include "cygwin.h"
192 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
193 #endif
194
195 const char w32log_h_rcs[] = W32LOG_H_VERSION;
196
197 #ifndef _WIN_CONSOLE /* entire file */
198
199 /*
200  * Timers and the various durations
201  */
202 #define TIMER_ANIM_ID               1
203 #define TIMER_ANIM_TIME             100
204 #define TIMER_ANIMSTOP_ID           2
205 #define TIMER_ANIMSTOP_TIME         1000
206 #define TIMER_CLIPBUFFER_ID         3
207 #define TIMER_CLIPBUFFER_TIME       1000
208 #define TIMER_CLIPBUFFER_FORCE_ID   4
209 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
210
211 /*
212  * Styles of text that can be output
213  */
214 #define STYLE_NONE      0
215 #define STYLE_HIGHLIGHT 1
216 #define STYLE_LINK      2
217 #define STYLE_HEADER    3
218
219 /*
220  * Number of frames of animation in tray activity sequence
221  */
222 #define ANIM_FRAMES 8
223
224 #define DEFAULT_MAX_BUFFER_LINES    200
225 #define DEFAULT_LOG_FONT_NAME       "MS Sans Serif"
226 #define DEFAULT_LOG_FONT_SIZE       8
227
228 /*
229  * These values affect the way the log window behaves, they should be read
230  * from a file but for the moment, they are hardcoded here. Some options are
231  * configurable through the UI.
232  */
233
234 /* Indicates whether task bar shows activity animation */
235 BOOL g_bShowActivityAnimation = 1;
236
237 /* Indicates whether the log window is shown */
238 BOOL g_bShowLogWindow = 1;
239
240 /* Indicates if the log window appears on the task bar */
241 BOOL g_bShowOnTaskBar = 0;
242
243 /* Indicates whether closing the log window really just hides it */
244 BOOL g_bCloseHidesWindow = 1;
245
246 /* Indicates if messages are logged at all */
247 BOOL g_bLogMessages = 1;
248
249 /* Indicates whether log messages are highlighted */
250 BOOL g_bHighlightMessages = 1;
251
252 /* Indicates if buffer is limited in size */
253 BOOL g_bLimitBufferSize = 1;
254
255 /* Maximum number of lines allowed in buffer when limited */
256 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
257
258 /* Font to use */
259 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
260
261 /* Size of font to use */
262 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
263
264
265 /* FIXME: this is a kludge */
266
267 const char * g_default_actions_file = NULL;
268 const char * g_user_actions_file = NULL;
269 const char * g_re_filterfile = NULL;
270 #ifdef FEATURE_TRUST
271 const char * g_trustfile = NULL;
272 #endif /* def FEATURE_TRUST */
273
274 /* FIXME: end kludge */
275
276 /* Regular expression for detected URLs */
277 #define RE_URL "http:[^ \n\r]*"
278
279 /*
280  * Regular expressions that are used to perform highlight in the log window
281  */
282 static struct _Pattern
283 {
284    const char *str;
285    int style;
286    regex_t buffer;
287 } patterns_to_highlight[] =
288 {
289    /* url headers */
290    { RE_URL,                STYLE_LINK },
291 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
292    /* interesting text to highlight */
293    { "crunch!",           STYLE_HIGHLIGHT },
294    /* http headers */
295    { "referer:",            STYLE_HEADER },
296    { "proxy-connection:",   STYLE_HEADER },
297    { "proxy-agent:",        STYLE_HEADER },
298    { "user-agent:",         STYLE_HEADER },
299    { "host:",               STYLE_HEADER },
300    { "accept:",             STYLE_HEADER },
301    { "accept-encoding:",    STYLE_HEADER },
302    { "accept-language:",    STYLE_HEADER },
303    { "accept-charset:",     STYLE_HEADER },
304    { "accept-ranges:",      STYLE_HEADER },
305    { "date:",               STYLE_HEADER },
306    { "cache-control:",      STYLE_HEADER },
307    { "cache-last-checked:", STYLE_HEADER },
308    { "connection:",         STYLE_HEADER },
309    { "content-type",        STYLE_HEADER },
310    { "content-length",      STYLE_HEADER },
311    { "cookie",              STYLE_HEADER },
312    { "last-modified:",      STYLE_HEADER },
313    { "pragma:",             STYLE_HEADER },
314    { "server:",             STYLE_HEADER },
315    { "etag:",               STYLE_HEADER },
316    { "expires:",            STYLE_HEADER },
317    { "warning:",            STYLE_HEADER },
318    /* this is the terminator statement - do not delete! */
319    { NULL,                  STYLE_NONE }
320 };
321
322 /*
323  * Public variables
324  */
325 HWND g_hwndLogFrame;
326 HICON g_hiconApp;
327
328 /*
329  * Private variables
330  */
331 static CRITICAL_SECTION g_criticalsection;
332 static HWND g_hwndTray;
333 static HWND g_hwndLogBox;
334 static WNDPROC g_fnLogBox;
335 static HICON g_hiconAnim[ANIM_FRAMES];
336 static HICON g_hiconIdle;
337 static int g_nAnimFrame;
338 static BOOL g_bClipPending = FALSE;
339 static int g_nRichEditVersion = 0;
340
341 /*
342  * Private functions
343  */
344 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
345 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
346 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
347 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
348 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
349 static BOOL InitRichEdit(void);
350 static void LogClipBuffer(void);
351 static void LogCreatePatternMatchingBuffers(void);
352 static void LogDestroyPatternMatchingBuffers(void);
353 static int LogPutStringNoMatch(const char *pszText, int style);
354
355
356 /*********************************************************************
357  *
358  * Function    :  InitLogWindow
359  *
360  * Description :  Initialise the log window.
361  *
362  * Parameters  :  None
363  *
364  * Returns     :  Always TRUE (there should be error checking on the resources).
365  *
366  *********************************************************************/
367 BOOL InitLogWindow(void)
368 {
369    int i;
370
371    /* Load the icons */
372    g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
373    for (i = 0; i < ANIM_FRAMES; i++)
374    {
375       g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
376    }
377    g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
378
379    /* Create the user interface */
380    g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
381    g_hwndTray = CreateTrayWindow(g_hInstance);
382    TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
383
384    /* Create pattern matching buffers (for highlighting */
385    LogCreatePatternMatchingBuffers();
386
387    /* Create a critical section to protect multi-threaded access to certain things */
388    InitializeCriticalSection(&g_criticalsection);
389
390    return TRUE;
391
392 }
393
394
395 /*********************************************************************
396  *
397  * Function    :  TermLogWindow
398  *
399  * Description :  Cleanup the logwindow.
400  *
401  * Parameters  :  None
402  *
403  * Returns     :  N/A
404  *
405  *********************************************************************/
406 void TermLogWindow(void)
407 {
408    int i;
409
410    LogDestroyPatternMatchingBuffers();
411
412    TrayDeleteIcon(g_hwndTray, 1);
413    DeleteObject(g_hiconApp);
414    DeleteObject(g_hiconIdle);
415    for (i = 0; i < ANIM_FRAMES; i++)
416    {
417       DeleteObject(g_hiconAnim[i]);
418    }
419
420 }
421
422
423 /*********************************************************************
424  *
425  * Function    :  LogCreatePatternMatchingBuffers
426  *
427  * Description :  Compile the pattern matching buffers.
428  *
429  * Parameters  :  None
430  *
431  * Returns     :  N/A
432  *
433  *********************************************************************/
434 void LogCreatePatternMatchingBuffers(void)
435 {
436    int i;
437    for (i = 0; patterns_to_highlight[i].str != NULL; i++)
438    {
439       regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
440    }
441 }
442
443
444 /*********************************************************************
445  *
446  * Function    :  LogDestroyPatternMatchingBuffers
447  *
448  * Description :  Free up the pattern matching buffers.
449  *
450  * Parameters  :  None
451  *
452  * Returns     :  N/A
453  *
454  *********************************************************************/
455 void LogDestroyPatternMatchingBuffers(void)
456 {
457    int i;
458    for (i = 0; patterns_to_highlight[i].str != NULL; i++)
459    {
460       regfree(&patterns_to_highlight[i].buffer);
461    }
462 }
463
464
465 /*********************************************************************
466  *
467  * Function    :  LogGetURLUnderCursor
468  *
469  * Description :  Returns the URL from under the cursor (remember to free it!).
470  *
471  * Parameters  :  None
472  *
473  * Returns     :  NULL or a pointer to an URL string.
474  *
475  *********************************************************************/
476 char *LogGetURLUnderCursor(void)
477 {
478    char *szResult = NULL;
479    regex_t re;
480    POINT ptCursor;
481    POINTL ptl;
482    DWORD nPos;
483    DWORD nWordStart = 0;
484    DWORD nWordEnd = 0;
485
486    regcomp(&re, RE_URL, REG_ICASE);
487
488    /* Get the position of the cursor over the text window */
489    GetCursorPos(&ptCursor);
490    ScreenToClient(g_hwndLogBox, &ptCursor);
491    ptl.x = ptCursor.x;
492    ptl.y = ptCursor.y;
493
494    /* Search backwards and fowards to obtain the word that is highlighted */
495    nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
496    nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
497    nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
498
499    /* Compare the string to the pattern */
500    if (nWordEnd > nWordStart)
501    {
502       TEXTRANGE range;
503       regmatch_t match;
504
505       range.chrg.cpMin = nWordStart;
506       range.chrg.cpMax = nWordEnd;
507       range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
508       SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
509
510       if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
511       {
512          szResult = range.lpstrText;
513       }
514       else
515       {
516          free(range.lpstrText);
517       }
518
519       regfree(&re);
520    }
521    return szResult;
522
523 }
524
525
526 /*********************************************************************
527  *
528  * Function    :  LogPutString
529  *
530  * Description :  Inserts text into the logging window.  This is really
531  *                a regexp aware wrapper function to `LogPutStringNoMatch'.
532  *
533  * Parameters  :
534  *          1  :  pszText = pointer to string going to the log window
535  *
536  * Returns     :  1 => success, else the return code from `LogPutStringNoMatch'.
537  *                FIXME: this is backwards to the rest of IJB and to common
538  *                programming practice.  Please use 0 => success instead.
539  *
540  *********************************************************************/
541 int LogPutString(const char *pszText)
542 {
543    int i;
544    int result = 0;
545
546    if (pszText == NULL || strlen(pszText) == 0)
547    {
548       return 1;
549    }
550
551    if (!g_bLogMessages)
552    {
553       return 1;
554    }
555
556    /* Critical section stops multiple threads doing nasty interactions that
557     * foul up the highlighting and output.
558     */
559    EnterCriticalSection(&g_criticalsection);
560
561    if (g_bHighlightMessages)
562    {
563       regmatch_t match;
564
565       /* First things first, regexp scan for various things that we would like highlighted */
566       for (i = 0; patterns_to_highlight[i].str != NULL; i++)
567       {
568          if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
569          {
570             char *pszBefore = NULL;
571             char *pszMatch = NULL;
572             char *pszAfter = NULL;
573             int nMatchSize;
574
575             /* Split the string up into pieces representing the strings, before
576                at and after the matching pattern
577              */
578             if (match.rm_so > 0)
579             {
580                pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
581                memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
582                strncpy(pszBefore, pszText, match.rm_so);
583             }
584             if (match.rm_eo < (regoff_t)strlen(pszText))
585             {
586                pszAfter = strdup(&pszText[match.rm_eo]);
587             }
588             nMatchSize = match.rm_eo - match.rm_so;
589             pszMatch = (char *)malloc(nMatchSize + 1);
590             strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
591             pszMatch[nMatchSize] = '\0';
592
593             /* Recursively call LogPutString */
594             if (pszBefore)
595             {
596                LogPutString(pszBefore);
597                free(pszBefore);
598             }
599             if (pszMatch)
600             {
601                LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
602                free(pszMatch);
603             }
604             if (pszAfter)
605             {
606                LogPutString(pszAfter);
607                free(pszAfter);
608             }
609
610             result = 1;
611             goto end;
612          }
613       }
614    }
615
616    result = LogPutStringNoMatch(pszText, STYLE_NONE);
617
618 end:
619    LeaveCriticalSection(&g_criticalsection);
620
621    return result;
622
623 }
624
625
626 /*********************************************************************
627  *
628  * Function    :  LogPutStringNoMatch
629  *
630  * Description :  Puts a string into the logging window.
631  *
632  * Parameters  :
633  *          1  :  pszText = pointer to string going to the log window
634  *          2  :  style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
635  *
636  * Returns     :  Always 1 => success.
637  *                FIXME: this is backwards to the rest of IJB and to common
638  *                programming practice.  Please use 0 => success instead.
639  *
640  *********************************************************************/
641 int LogPutStringNoMatch(const char *pszText, int style)
642 {
643    CHARRANGE range;
644    CHARFORMAT format;
645    int nTextLength;
646
647    assert(g_hwndLogBox);
648    if (g_hwndLogBox == NULL)
649    {
650       return 1;
651    }
652
653    /* TODO preserve existing selection */
654
655    /* Go to the end of the text */
656    nTextLength = GetWindowTextLength(g_hwndLogBox);
657    range.cpMin = nTextLength;
658    range.cpMax = nTextLength;
659    SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
660
661    /* Apply a formatting style */
662    memset(&format, 0, sizeof(format));
663    format.cbSize = sizeof(format);
664    format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
665    format.yHeight = (g_nFontSize * 1440) / 72;
666    strcpy(format.szFaceName, g_szFontFaceName);
667    if (style == STYLE_NONE)
668    {
669       /* DO NOTHING */
670       format.dwEffects |= CFE_AUTOCOLOR;
671    }
672    else if (style == STYLE_HEADER)
673    {
674       format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
675    }
676    else if (style == STYLE_HIGHLIGHT)
677    {
678       format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
679    }
680    else if (style == STYLE_LINK)
681    {
682       format.dwEffects |= CFE_UNDERLINE;
683       format.crTextColor = RGB(0, 0, 255);
684    }
685    SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
686
687    /* Append text to the end */
688    SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
689
690    /* TODO Restore the old selection */
691
692    /* Purge buffer */
693    if (strchr(pszText, '\n') != NULL)
694    {
695       SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
696       if (!g_bClipPending)
697       {
698          /* Set the force clip timer going. This timer ensures clipping is done
699             intermittently even when there is a sustained burst of logging
700          */
701          SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
702       }
703       g_bClipPending = TRUE;
704    }
705
706    return 1;
707
708 }
709
710
711 /*********************************************************************
712  *
713  * Function    :  LogShowActivity
714  *
715  * Description :  Start the spinner.
716  *
717  * Parameters  :  None
718  *
719  * Returns     :  N/A
720  *
721  *********************************************************************/
722 void LogShowActivity(void)
723 {
724    /* Start some activity timers */
725    if (g_bShowActivityAnimation)
726    {
727       SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
728       SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
729    }
730
731 }
732
733
734 /*********************************************************************
735  *
736  * Function    :  LogClipBuffer
737  *
738  * Description :  Prunes old lines from the log.
739  *
740  * Parameters  :  None
741  *
742  * Returns     :  N/A
743  *
744  *********************************************************************/
745 void LogClipBuffer(void)
746 {
747    int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
748    if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
749    {
750       /* Compute the range representing the lines to be deleted */
751       LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
752       LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
753       CHARRANGE range;
754       range.cpMin = 0;
755       range.cpMax = nLastChar;
756
757       /* TODO get current selection */
758
759       /* TODO adjust and clip old selection against range to be deleted */
760
761       /* Select range and erase it (turning off autoscroll to prevent
762          nasty scrolling) */
763       SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
764       SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
765       SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
766       SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
767
768       /* Restore old selection */
769    }
770
771 }
772
773
774 /*********************************************************************
775  *
776  * Function    :  CreateHiddenLogOwnerWindow
777  *
778  * Description :  Creates a hidden owner window that stops the log
779  *                window appearing in the task bar.
780  *
781  * Parameters  :
782  *          1  :  hInstance = application's instance handle
783  *
784  * Returns     :  Handle to newly created window.
785  *
786  *********************************************************************/
787 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
788 {
789    static const char *szWndName = "PrivoxyLogOwner";
790    WNDCLASS wc;
791    HWND hwnd;
792
793    wc.style          = 0;
794    wc.lpfnWndProc    = LogOwnerWindowProc;
795    wc.cbClsExtra     = 0;
796    wc.cbWndExtra     = 0;
797    wc.hInstance      = hInstance;
798    wc.hIcon          = 0;
799    wc.hCursor        = 0;
800    wc.hbrBackground  = 0;
801    wc.lpszMenuName   = 0;
802    wc.lpszClassName  = szWndName;
803
804    RegisterClass(&wc);
805
806    hwnd = CreateWindow(szWndName, szWndName,
807       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
808       CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
809
810    return hwnd;
811
812 }
813
814
815 /*********************************************************************
816  *
817  * Function    :  LogOwnerWindowProc
818  *
819  * Description :  Dummy procedure that does nothing special.
820  *
821  * Parameters  :
822  *          1  :  hwnd = window handle
823  *          2  :  uMsg = message number
824  *          3  :  wParam = first param for this message
825  *          4  :  lParam = next param for this message
826  *
827  * Returns     :  Same as `DefWindowProc'.
828  *
829  *********************************************************************/
830 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
831 {
832    return DefWindowProc(hwnd, uMsg, wParam, lParam);
833
834 }
835
836
837 /*********************************************************************
838  *
839  * Function    :  CreateLogWindow
840  *
841  * Description :  Create the logging window.
842  *
843  * Parameters  :
844  *          1  :  hInstance = application's instance handle
845  *          2  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
846  *
847  * Returns     :  Handle to newly created window.
848  *
849  *********************************************************************/
850 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
851 {
852    static const char *szWndName = "PrivoxyLogWindow";
853    static const char *szWndTitle = "Privoxy";
854
855    HWND hwnd = NULL;
856    HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
857    RECT rcClient;
858    WNDCLASSEX wc;
859
860    memset(&wc, 0, sizeof(wc));
861    wc.cbSize         = sizeof(wc);
862    wc.style          = CS_DBLCLKS;
863    wc.lpfnWndProc    = LogWindowProc;
864    wc.cbClsExtra     = 0;
865    wc.cbWndExtra     = 0;
866    wc.hInstance      = hInstance;
867    wc.hIcon          = g_hiconApp;
868    wc.hCursor        = 0;
869    wc.hbrBackground  = 0;
870    wc.lpszMenuName   = MAKEINTRESOURCE(IDR_LOGVIEW);
871    wc.lpszClassName  = szWndName;
872    wc.hbrBackground  = GetStockObject(WHITE_BRUSH);
873    RegisterClassEx(&wc);
874
875    hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
876       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
877       CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
878
879    /* Now create a child list box */
880    GetClientRect(hwnd, &rcClient);
881
882    /* Create a rich edit control */
883    InitRichEdit();
884    g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
885       ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
886       rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
887       hwnd, NULL, hInstance, NULL);
888 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
889
890    /* Subclass the control to catch certain messages */
891    g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
892    SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
893
894    /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
895    if (!g_bShowOnTaskBar &&
896          (nCmdShow == SW_SHOWMINIMIZED ||
897           nCmdShow == SW_MINIMIZE ||
898           nCmdShow == SW_SHOWMINNOACTIVE))
899    {
900       nCmdShow = SW_HIDE;
901    }
902
903    ShowWindow(hwnd, nCmdShow);
904    UpdateWindow(hwnd);
905
906    GetClientRect(g_hwndLogFrame, &rcClient);
907    SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
908
909    return hwnd;
910
911 }
912
913
914 /*********************************************************************
915  *
916  * Function    :  InitRichEdit
917  *
918  * Description :  Initialise the rich edit control library.
919  *
920  * Parameters  :  None
921  *
922  * Returns     :  TRUE => success, FALSE => failure.
923  *                FIXME: this is backwards to the rest of IJB and to common
924  *                programming practice.  Please use 0 => success instead.
925  *
926  *********************************************************************/
927 BOOL InitRichEdit(void)
928 {
929    static HINSTANCE hInstRichEdit;
930    if (hInstRichEdit == NULL)
931    {
932       g_nRichEditVersion = 0;
933       hInstRichEdit = LoadLibraryA("RICHED20.DLL");
934       if (hInstRichEdit)
935       {
936          g_nRichEditVersion = _RICHEDIT_VER;
937       }
938       else
939       {
940          hInstRichEdit = LoadLibraryA("RICHED32.DLL");
941          if (hInstRichEdit)
942          {
943             g_nRichEditVersion = 0x0100;
944          }
945       }
946    }
947    return (hInstRichEdit != NULL) ? TRUE : FALSE;
948
949 }
950
951
952 /*********************************************************************
953  *
954  * Function    :  ShowLogWindow
955  *
956  * Description :  Shows or hides the log window.  We will also raise the
957  *                window on a show command in case it is buried.
958  *
959  * Parameters  :
960  *          1  :  bShow = TRUE to show, FALSE to mimize/hide
961  *
962  * Returns     :  N/A
963  *
964  *********************************************************************/
965 void ShowLogWindow(BOOL bShow)
966 {
967    if (bShow)
968    {
969       SetForegroundWindow(g_hwndLogFrame);
970       SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
971       
972    }
973    else if (g_bShowOnTaskBar)
974    {
975       ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
976    }
977    else
978    {
979       ShowWindow(g_hwndLogFrame, SW_HIDE);
980    }
981 }
982
983
984 /*********************************************************************
985  *
986  * Function    :  EditFile
987  *
988  * Description :  Opens the specified setting file for editing.
989  * FIXME: What if the file has no associated application. Check for return values
990 *        from ShellExecute??
991  *
992  * Parameters  :
993  *          1  :  filename = filename from the config (aka config.txt) file.
994  *
995  * Returns     :  N/A
996  *
997  *********************************************************************/
998 void EditFile(const char *filename)
999 {
1000    if (filename)
1001    {
1002       ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1003    }
1004
1005 }
1006
1007
1008 /*--------------------------------------------------------------------------*/
1009 /* Windows message handlers                                                 */
1010 /*--------------------------------------------------------------------------*/
1011
1012
1013 /*********************************************************************
1014  *
1015  * Function    :  OnLogRButtonUp
1016  *
1017  * Description :  Handler for WM_RBUTTONUP messages.
1018  *
1019  * Parameters  :
1020  *          1  :  nModifier = wParam from mouse message (unused)
1021  *          2  :  x = x coordinate of the mouse event
1022  *          3  :  y = y coordinate of the mouse event
1023  *
1024  * Returns     :  N/A
1025  *
1026  *********************************************************************/
1027 void OnLogRButtonUp(int nModifier, int x, int y)
1028 {
1029    HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1030    if (hMenu != NULL)
1031    {
1032       HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1033
1034       /* Check if there is a selection */
1035       CHARRANGE range;
1036       SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1037       if (range.cpMin == range.cpMax)
1038       {
1039          EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1040       }
1041       else
1042       {
1043          EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1044       }
1045
1046       /* Display the popup */
1047       TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1048       DestroyMenu(hMenu);
1049    }
1050
1051 }
1052
1053
1054 /*********************************************************************
1055  *
1056  * Function    :  OnLogCommand
1057  *
1058  * Description :  Handler for WM_COMMAND messages.
1059  *
1060  * Parameters  :
1061  *          1  :  nCommand = the command portion of the menu selection event
1062  *
1063  * Returns     :  N/A
1064  *
1065  *********************************************************************/
1066 void OnLogCommand(int nCommand)
1067 {
1068    switch (nCommand)
1069    {
1070       case ID_TOGGLE_SHOWWINDOW:
1071          g_bShowLogWindow = !g_bShowLogWindow;
1072
1073          ShowLogWindow(g_bShowLogWindow);
1074          break;
1075
1076       case ID_FILE_EXIT:
1077          PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1078          break;
1079
1080       case ID_EDIT_COPY:
1081          SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1082          break;
1083
1084       case ID_VIEW_CLEARLOG:
1085          SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1086          break;
1087
1088       case ID_VIEW_LOGMESSAGES:
1089          g_bLogMessages = !g_bLogMessages;
1090          /* SaveLogSettings(); */
1091          break;
1092
1093       case ID_VIEW_MESSAGEHIGHLIGHTING:
1094          g_bHighlightMessages = !g_bHighlightMessages;
1095          /* SaveLogSettings(); */
1096          break;
1097
1098       case ID_VIEW_LIMITBUFFERSIZE:
1099          g_bLimitBufferSize = !g_bLimitBufferSize;
1100          /* SaveLogSettings(); */
1101          break;
1102
1103       case ID_VIEW_ACTIVITYANIMATION:
1104          g_bShowActivityAnimation = !g_bShowActivityAnimation;
1105          /* SaveLogSettings(); */
1106          break;
1107
1108 #ifdef FEATURE_TOGGLE
1109       /* by haroon - change toggle to its opposite value */
1110       case ID_TOGGLE_ENABLED:
1111          global_toggle_state = !global_toggle_state;
1112          if (global_toggle_state)
1113          {
1114             log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1115          }
1116          else
1117          {
1118             log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1119          }
1120          break;
1121 #endif /* def FEATURE_TOGGLE */
1122
1123       case ID_TOOLS_EDITCONFIG:
1124          EditFile(configfile);
1125          break;
1126
1127       case ID_TOOLS_EDITDEFAULTACTIONS:
1128          EditFile(g_default_actions_file);
1129          break;
1130
1131       case ID_TOOLS_EDITUSERACTIONS:
1132          EditFile(g_user_actions_file);
1133          break;
1134
1135       case ID_TOOLS_EDITFILTERS:
1136          EditFile(g_re_filterfile);
1137          break;
1138
1139 #ifdef FEATURE_TRUST
1140       case ID_TOOLS_EDITTRUST:
1141          EditFile(g_trustfile);
1142          break;
1143 #endif /* def FEATURE_TRUST */
1144
1145       case ID_HELP_GPL:
1146          ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1147          break;
1148
1149       case ID_HELP_FAQ:
1150          ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1151          break;
1152
1153       case ID_HELP_MANUAL:
1154          ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1155          break;
1156
1157       case ID_HELP_STATUS:
1158          ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1159          break;
1160
1161       case ID_HELP_ABOUT:
1162          MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1163          break;
1164
1165       default:
1166          /* DO NOTHING */
1167          break;
1168    }
1169
1170 }
1171
1172
1173 /*********************************************************************
1174  *
1175  * Function    :  OnLogInitMenu
1176  *
1177  * Description :  Handler for WM_INITMENU messages.  Enable, disable,
1178  *                check, and/or uncheck menu options as apropos.
1179  *
1180  * Parameters  :
1181  *          1  :  hmenu = handle to menu to "make current"
1182  *
1183  * Returns     :  N/A
1184  *
1185  *********************************************************************/
1186 void OnLogInitMenu(HMENU hmenu)
1187 {
1188    /* Only enable editors if there is a file to edit */
1189    EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1190    EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1191    EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1192 #ifdef FEATURE_TRUST
1193    EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1194 #endif /* def FEATURE_TRUST */
1195
1196    /* Check/uncheck options */
1197    CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1198    CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1199    CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1200    CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1201 #ifdef FEATURE_TOGGLE
1202    /* by haroon - menu item for Enable toggle on/off */
1203    CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1204 #endif /* def FEATURE_TOGGLE */
1205    CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1206
1207 }
1208
1209
1210 /*********************************************************************
1211  *
1212  * Function    :  OnLogTimer
1213  *
1214  * Description :  Handler for WM_TIMER messages.
1215  *
1216  * Parameters  :
1217  *          1  :  nTimer = timer id (animation start/stop or clip buffer)
1218  *
1219  * Returns     :  N/A
1220  *
1221  *********************************************************************/
1222 void OnLogTimer(int nTimer)
1223 {
1224    switch (nTimer)
1225    {
1226       case TIMER_ANIM_ID:
1227          TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1228          break;
1229
1230       case TIMER_ANIMSTOP_ID:
1231          g_nAnimFrame = 0;
1232          TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1233          KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1234          KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1235          break;
1236
1237       case TIMER_CLIPBUFFER_ID:
1238       case TIMER_CLIPBUFFER_FORCE_ID:
1239          LogClipBuffer();
1240          g_bClipPending = FALSE;
1241          KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1242          KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1243          break;
1244
1245       default:
1246          /* DO NOTHING */
1247          break;
1248    }
1249
1250 }
1251
1252
1253 /*********************************************************************
1254  *
1255  * Function    :  LogRichEditProc
1256  *
1257  * Description :  Window subclass routine handles some events for the rich edit control.
1258  *
1259  * Parameters  :
1260  *          1  :  hwnd = window handle of the rich edit control
1261  *          2  :  uMsg = message number
1262  *          3  :  wParam = first param for this message
1263  *          4  :  lParam = next param for this message
1264  *
1265  * Returns     :  Appropriate M$ window message handler codes.
1266  *
1267  *********************************************************************/
1268 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1269 {
1270    switch (uMsg)
1271    {
1272       case WM_RBUTTONUP:
1273       {
1274          POINT pt;
1275          pt.x = LOWORD(lParam);
1276          pt.y = HIWORD(lParam);
1277          ClientToScreen(hwnd, &pt);
1278          OnLogRButtonUp(wParam, pt.x, pt.y);
1279       }
1280       return 0;
1281    }
1282    return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1283
1284 }
1285
1286
1287 /*********************************************************************
1288  *
1289  * Function    :  LogWindowProc
1290  *
1291  * Description :  Windows call back routine handles events on the log window.
1292  *
1293  * Parameters  :
1294  *          1  :  hwnd = handle of the logging window
1295  *          2  :  uMsg = message number
1296  *          3  :  wParam = first param for this message
1297  *          4  :  lParam = next param for this message
1298  *
1299  * Returns     :  Appropriate M$ window message handler codes.
1300  *
1301  *********************************************************************/
1302 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1303 {
1304    switch (uMsg)
1305    {
1306       case WM_CREATE:
1307          return 0;
1308
1309       case WM_CLOSE:
1310          /* This is the end - beautiful friend - the end */
1311          DestroyWindow(g_hwndLogBox);
1312          DestroyWindow(g_hwndLogFrame);
1313          return 0;
1314
1315       case WM_DESTROY:
1316          PostQuitMessage(0);
1317          return 0;
1318
1319       case WM_SHOWWINDOW:
1320          g_bShowLogWindow = wParam;
1321       case WM_SIZE:
1322          /* Resize the logging window to fit the new frame */
1323          if (g_hwndLogBox)
1324          {
1325             RECT rc;
1326             GetClientRect(g_hwndLogFrame, &rc);
1327             SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1328          }
1329          return 0;
1330
1331       case WM_INITMENU:
1332          OnLogInitMenu((HMENU) wParam);
1333          return 0;
1334
1335       case WM_TIMER:
1336          OnLogTimer(wParam);
1337          return 0;
1338
1339       case WM_COMMAND:
1340          OnLogCommand(LOWORD(wParam));
1341          return 0;
1342
1343       case WM_SYSCOMMAND:
1344          switch (wParam)
1345          {
1346             case SC_CLOSE:
1347                if (g_bCloseHidesWindow)
1348                {
1349                   ShowLogWindow(FALSE);
1350                   return 0;
1351                }
1352                break;
1353             case SC_MINIMIZE:
1354                ShowLogWindow(FALSE);
1355                return 0;
1356          }
1357          break;
1358    }
1359
1360    return DefWindowProc(hwnd, uMsg, wParam, lParam);
1361
1362 }
1363
1364 #endif /* ndef _WIN_CONSOLE - entire file */
1365
1366 /*
1367   Local Variables:
1368   tab-width: 3
1369   end:
1370 */