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