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