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