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