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