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