Fix two mingw32-only buffer overflows. Note that triggering
[privoxy.git] / w32log.c
1 const char w32log_rcs[] = "$Id: w32log.c,v 1.30 2009/01/01 15:09:23 ler762 Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/w32log.c,v $
5  *
6  * Purpose     :  Functions for creating and destroying the log window,
7  *                ouputting strings, processing messages and so on.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2009 members of
10  *                the Privoxy team.  http://www.privoxy.org/
11  *
12  *                Written by and Copyright (C) 1999 Adam Lock
13  *                <locka@iol.ie>
14  *
15  *                This program is free software; you can redistribute it 
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  * Revisions   :
34  *    $Log: w32log.c,v $
35  *    Revision 1.30  2009/01/01 15:09:23  ler762
36  *    Change the Windows taskbar icon when privoxy is toggled off.
37  *
38  *    Revision 1.29  2008/12/20 15:27:40  ler762
39  *    The crunch log message format changed, so update the strings to highlight
40  *    in the log window.
41  *
42  *    Revision 1.28  2008/11/02 14:37:47  ler762
43  *    commit the part of the patches I've been using that were written by torford and gjmurphy
44  *      [ 1824315 ] Minor code cleanup
45  *      [ 1781135 ] Patch - Add clear log, select all, and Accelerators for w32
46  *        http://sourceforge.net/tracker/?func=detail&atid=311118&aid=1781135&group_id=11118
47  *    The full patch adds control keys A(select all), C(copy) and D(delete all) to the
48  *    Privoxy log window menu.  Select all and copy work for me without the patch
49  *    (albeit without showing the accelerator keys on the menu), so the only part of the
50  *    patch I've been using for the last year or so has been the ctrl-d to delete
51  *    everything in the Privoxy log window.
52  *
53  *    Revision 1.27  2006/07/18 14:48:48  david__schmidt
54  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
55  *    with what was really the latest development (the v_3_0_branch branch)
56  *
57  *    Revision 1.25.2.5  2003/04/04 12:48:51  oes
58  *    Fixed bug #711865:
59  *     - Made tray menu correctly reflect initial window visibility state
60  *     - Hopefully fixed problem where log window contents wasn't visible
61  *       until vertical scroll bar was clicked. Thanks to Guy for the fix!
62  *
63  *    Revision 1.25.2.4  2003/03/11 11:53:59  oes
64  *    Cosmetic: Renamed cryptic variable
65  *
66  *    Revision 1.25.2.3  2002/11/20 14:39:05  oes
67  *    Fixed compiler warning
68  *
69  *    Revision 1.25.2.2  2002/09/25 15:23:10  oes
70  *    Uncheck the "Show Privoxy Window" taskbar menu item when window gets minimized. Fixes bug #606804
71  *
72  *    Revision 1.25.2.1  2002/08/21 17:59:05  oes
73  *     - "Show Privoxy Window" now a toggle
74  *     - Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
75  *
76  *    Revision 1.25  2002/04/04 00:36:36  gliptak
77  *    always use pcre for matching
78  *
79  *    Revision 1.24  2002/03/31 17:19:00  jongfoster
80  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
81  *
82  *    Revision 1.23  2002/03/26 22:57:10  jongfoster
83  *    Web server name should begin www.
84  *
85  *    Revision 1.22  2002/03/24 12:48:23  jongfoster
86  *    Fixing doc links
87  *
88  *    Revision 1.21  2002/03/24 12:07:35  jongfoster
89  *    Consistern name for filters file
90  *
91  *    Revision 1.20  2002/03/24 12:03:47  jongfoster
92  *    Name change
93  *
94  *    Revision 1.19  2002/01/17 21:04:17  jongfoster
95  *    Replacing hard references to the URL of the config interface
96  *    with #defines from project.h
97  *
98  *    Revision 1.18  2001/11/30 23:37:24  jongfoster
99  *    Renaming the Win32 config file to config.txt - this is almost the
100  *    same as the corresponding UNIX name "config"
101  *
102  *    Revision 1.17  2001/11/16 00:46:31  jongfoster
103  *    Fixing compiler warnings
104  *
105  *    Revision 1.16  2001/08/01 19:58:12  jongfoster
106  *    Fixing documentation filenames in help menu, and making status
107  *    option work without needing the "Junkbuster Status.URL" file.
108  *
109  *    Revision 1.15  2001/07/30 22:08:36  jongfoster
110  *    Tidying up #defines:
111  *    - All feature #defines are now of the form FEATURE_xxx
112  *    - Permanently turned off WIN_GUI_EDIT
113  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
114  *
115  *    Revision 1.14  2001/07/29 18:47:05  jongfoster
116  *    Adding missing #include "loadcfg.h"
117  *
118  *    Revision 1.13  2001/07/19 19:15:14  haroon
119  *    - Added a FIXME for EditFile but didn't fix :-)
120  *
121  *    Revision 1.12  2001/07/13 14:04:59  oes
122  *    Removed all #ifdef PCRS
123  *
124  *    Revision 1.11  2001/06/07 23:08:12  jongfoster
125  *    Forward and ACL edit options removed.
126  *
127  *    Revision 1.10  2001/05/31 21:37:11  jongfoster
128  *    GUI changes to rename "permissions file" to "actions file".
129  *
130  *    Revision 1.9  2001/05/31 17:33:13  oes
131  *
132  *    CRLF -> LF
133  *
134  *    Revision 1.8  2001/05/29 09:50:24  jongfoster
135  *    Unified blocklist/imagelist/permissionslist.
136  *    File format is still under discussion, but the internal changes
137  *    are (mostly) done.
138  *
139  *    Also modified interceptor behaviour:
140  *    - We now intercept all URLs beginning with one of the following
141  *      prefixes (and *only* these prefixes):
142  *        * http://i.j.b/
143  *        * http://ijbswa.sf.net/config/
144  *        * http://ijbswa.sourceforge.net/config/
145  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
146  *    - Internal changes so that intercepted and fast redirect pages
147  *      are not replaced with an image.
148  *    - Interceptors now have the option to send a binary page direct
149  *      to the client. (i.e. ijb-send-banner uses this)
150  *    - Implemented show-url-info interceptor.  (Which is why I needed
151  *      the above interceptors changes - a typical URL is
152  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
153  *      The previous mechanism would not have intercepted that, and
154  *      if it had been intercepted then it then it would have replaced
155  *      it with an image.)
156  *
157  *    Revision 1.7  2001/05/26 01:26:34  jongfoster
158  *    New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
159  *    This #define cannot be set from ./configure - there's no point, it
160  *    doesn't work yet.  See feature request # 425722
161  *
162  *    Revision 1.6  2001/05/26 00:31:30  jongfoster
163  *    Fixing compiler warning about comparing signed/unsigned.
164  *
165  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
166  *    Automatic reloading of config file.
167  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
168  *    Most of the global variables have been moved to a new
169  *    struct configuration_spec, accessed through csp->config->globalname
170  *    Most of the globals remaining are used by the Win32 GUI.
171  *
172  *    Revision 1.4  2001/05/22 18:56:28  oes
173  *    CRLF -> LF
174  *
175  *    Revision 1.3  2001/05/20 15:07:54  jongfoster
176  *    File is now ignored if _WIN_CONSOLE is defined.
177  *
178  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
179  *    Version 2.9.4 checkin.
180  *    - Merged popupfile and cookiefile, and added control over PCRS
181  *      filtering, in new "permissionsfile".
182  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
183  *      file error you now get a message box (in the Win32 GUI) rather
184  *      than the program exiting with no explanation.
185  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
186  *      skipping.
187  *    - Removed tabs from "config"
188  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
189  *    - Bumped up version number.
190  *
191  *    Revision 1.1.1.1  2001/05/15 13:59:07  oes
192  *    Initial import of version 2.9.3 source tree
193  *
194  *
195  *********************************************************************/
196 \f
197
198 #include "config.h"
199
200 #include <assert.h>
201 #include <stdio.h>
202
203 #ifndef STRICT
204 #define STRICT
205 #endif
206 #include <windows.h>
207 #include <richedit.h>
208
209 #include "project.h"
210 #include "w32log.h"
211 #include "w32taskbar.h"
212 #include "win32.h"
213 #include "w32res.h"
214 #include "jcc.h"
215 #include "miscutil.h"
216 #include "errlog.h"
217 #include "loadcfg.h"
218
219 const char w32res_h_rcs[] = W32RES_H_VERSION;
220
221 #ifdef __MINGW32__
222 #include "cygwin.h"
223 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
224 #endif
225
226 const char w32log_h_rcs[] = W32LOG_H_VERSION;
227
228 #ifndef _WIN_CONSOLE /* entire file */
229
230 /*
231  * Timers and the various durations
232  */
233 #define TIMER_ANIM_ID               1
234 #define TIMER_ANIM_TIME             100
235 #define TIMER_ANIMSTOP_ID           2
236 #define TIMER_ANIMSTOP_TIME         1000
237 #define TIMER_CLIPBUFFER_ID         3
238 #define TIMER_CLIPBUFFER_TIME       1000
239 #define TIMER_CLIPBUFFER_FORCE_ID   4
240 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
241
242 /*
243  * Styles of text that can be output
244  */
245 #define STYLE_NONE      0
246 #define STYLE_HIGHLIGHT 1
247 #define STYLE_LINK      2
248 #define STYLE_HEADER    3
249
250 /*
251  * Number of frames of animation in tray activity sequence
252  */
253 #define ANIM_FRAMES 8
254
255 #define DEFAULT_MAX_BUFFER_LINES    200
256 #define DEFAULT_LOG_FONT_NAME       "MS Sans Serif"
257 #define DEFAULT_LOG_FONT_SIZE       8
258
259 /*
260  * These values affect the way the log window behaves, they should be read
261  * from a file but for the moment, they are hardcoded here. Some options are
262  * configurable through the UI.
263  */
264
265 /* Indicates whether task bar shows activity animation */
266 BOOL g_bShowActivityAnimation = 1;
267
268 /* Indicates whether the log window is shown */
269 BOOL g_bShowLogWindow = 1;
270
271 /* Indicates if the log window appears on the task bar */
272 BOOL g_bShowOnTaskBar = 0;
273
274 /* Indicates whether closing the log window really just hides it */
275 BOOL g_bCloseHidesWindow = 1;
276
277 /* Indicates if messages are logged at all */
278 BOOL g_bLogMessages = 1;
279
280 /* Indicates whether log messages are highlighted */
281 BOOL g_bHighlightMessages = 1;
282
283 /* Indicates if buffer is limited in size */
284 BOOL g_bLimitBufferSize = 1;
285
286 /* Maximum number of lines allowed in buffer when limited */
287 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
288
289 /* Font to use */
290 char g_szFontFaceName[32] = DEFAULT_LOG_FONT_NAME;
291
292 /* Size of font to use */
293 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
294
295
296 /* FIXME: this is a kludge */
297
298 const char * g_default_actions_file = NULL;
299 const char * g_user_actions_file = NULL;
300 const char * g_re_filterfile = NULL;
301 #ifdef FEATURE_TRUST
302 const char * g_trustfile = NULL;
303 #endif /* def FEATURE_TRUST */
304
305 /* FIXME: end kludge */
306
307 /* Regular expression for detected URLs */
308 #define RE_URL "http:[^ \n\r]*"
309
310 /*
311  * Regular expressions that are used to perform highlight in the log window
312  */
313 static struct _Pattern
314 {
315    const char *str;
316    int style;
317    regex_t buffer;
318 } patterns_to_highlight[] =
319 {
320    /* url headers */
321    { RE_URL,                STYLE_LINK },
322 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
323    /* interesting text to highlight */
324    /*   see jcc.c crunch_reason for the full list */
325    { "Crunch: Blocked:",            STYLE_HIGHLIGHT },
326    { "Crunch: Untrusted",           STYLE_HIGHLIGHT },
327    { "Crunch: Redirected:",         STYLE_HIGHLIGHT },
328    { "Crunch: DNS failure",         STYLE_HIGHLIGHT },
329    { "Crunch: Forwarding failed",   STYLE_HIGHLIGHT },
330    { "Crunch: Connection failure",  STYLE_HIGHLIGHT },
331    { "Crunch: Out of memory",       STYLE_HIGHLIGHT },
332    /* what are all the possible error strings?? */
333    { "Error:",                      STYLE_HIGHLIGHT },
334    /* http headers */
335    { "referer:",            STYLE_HEADER },
336    { "proxy-connection:",   STYLE_HEADER },
337    { "proxy-agent:",        STYLE_HEADER },
338    { "user-agent:",         STYLE_HEADER },
339    { "host:",               STYLE_HEADER },
340    { "accept:",             STYLE_HEADER },
341    { "accept-encoding:",    STYLE_HEADER },
342    { "accept-language:",    STYLE_HEADER },
343    { "accept-charset:",     STYLE_HEADER },
344    { "accept-ranges:",      STYLE_HEADER },
345    { "date:",               STYLE_HEADER },
346    { "cache-control:",      STYLE_HEADER },
347    { "cache-last-checked:", STYLE_HEADER },
348    { "connection:",         STYLE_HEADER },
349    { "content-type",        STYLE_HEADER },
350    { "content-length",      STYLE_HEADER },
351    { "cookie",              STYLE_HEADER },
352    { "last-modified:",      STYLE_HEADER },
353    { "pragma:",             STYLE_HEADER },
354    { "server:",             STYLE_HEADER },
355    { "etag:",               STYLE_HEADER },
356    { "expires:",            STYLE_HEADER },
357    { "warning:",            STYLE_HEADER },
358    /* this is the terminator statement - do not delete! */
359    { NULL,                  STYLE_NONE }
360 };
361
362 /*
363  * Public variables
364  */
365 HWND g_hwndLogFrame;
366 HICON g_hiconApp;
367
368 /*
369  * Private variables
370  */
371 static CRITICAL_SECTION g_criticalsection;
372 static HWND g_hwndTray;
373 static HWND g_hwndLogBox;
374 static WNDPROC g_fnLogBox;
375 static HICON g_hiconAnim[ANIM_FRAMES];
376 static HICON g_hiconIdle;
377 static HICON g_hiconOff;
378 static int g_nAnimFrame;
379 static BOOL g_bClipPending = FALSE;
380 static int g_nRichEditVersion = 0;
381
382 /*
383  * Private functions
384  */
385 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
386 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
387 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
388 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
389 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
390 static BOOL InitRichEdit(void);
391 static void LogClipBuffer(void);
392 static void LogCreatePatternMatchingBuffers(void);
393 static void LogDestroyPatternMatchingBuffers(void);
394 static int LogPutStringNoMatch(const char *pszText, int style);
395 static void SetIdleIcon(void);
396
397
398 /*********************************************************************
399  *
400  * Function    :  InitLogWindow
401  *
402  * Description :  Initialise the log window.
403  *
404  * Parameters  :  None
405  *
406  * Returns     :  Always TRUE (there should be error checking on the resources).
407  *
408  *********************************************************************/
409 BOOL InitLogWindow(void)
410 {
411    int i;
412
413    /* Load the icons */
414    g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
415    g_hiconOff  = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_OFF));
416    for (i = 0; i < ANIM_FRAMES; i++)
417    {
418       g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
419    }
420    g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
421
422    /* Create the user interface */
423    g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
424    g_hwndTray = CreateTrayWindow(g_hInstance);
425    TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
426
427    /* Create pattern matching buffers (for highlighting */
428    LogCreatePatternMatchingBuffers();
429
430    /* Create a critical section to protect multi-threaded access to certain things */
431    InitializeCriticalSection(&g_criticalsection);
432
433    return TRUE;
434
435 }
436
437
438 /*********************************************************************
439  *
440  * Function    :  TermLogWindow
441  *
442  * Description :  Cleanup the logwindow.
443  *
444  * Parameters  :  None
445  *
446  * Returns     :  N/A
447  *
448  *********************************************************************/
449 void TermLogWindow(void)
450 {
451    int i;
452
453    LogDestroyPatternMatchingBuffers();
454
455    TrayDeleteIcon(g_hwndTray, 1);
456    DeleteObject(g_hiconApp);
457    DeleteObject(g_hiconIdle);
458    DeleteObject(g_hiconOff);
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    strlcpy(format.szFaceName, g_szFontFaceName, sizeof(format.szFaceName));
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  *
784  * Parameters  :  None
785  *
786  * Returns     :  N/A
787  *
788  *********************************************************************/
789 void LogClipBuffer(void)
790 {
791    int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
792    if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
793    {
794       /* Compute the range representing the lines to be deleted */
795       LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
796       LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
797       CHARRANGE range;
798       range.cpMin = 0;
799       range.cpMax = nLastChar;
800
801       /* TODO get current selection */
802
803       /* TODO adjust and clip old selection against range to be deleted */
804
805       /* Select range and erase it (turning off autoscroll to prevent
806          nasty scrolling) */
807       SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
808       SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
809       SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
810       SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
811
812       /* reposition (back to) the end of the log content */
813       range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
814       range.cpMax = -1;
815       SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
816  
817       /* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
818       SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
819  
820    }
821
822 }                                        
823
824
825 /*********************************************************************
826  *
827  * Function    :  CreateHiddenLogOwnerWindow
828  *
829  * Description :  Creates a hidden owner window that stops the log
830  *                window appearing in the task bar.
831  *
832  * Parameters  :
833  *          1  :  hInstance = application's instance handle
834  *
835  * Returns     :  Handle to newly created window.
836  *
837  *********************************************************************/
838 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
839 {
840    static const char *szWndName = "PrivoxyLogOwner";
841    WNDCLASS wc;
842    HWND hwnd;
843
844    wc.style          = 0;
845    wc.lpfnWndProc    = LogOwnerWindowProc;
846    wc.cbClsExtra     = 0;
847    wc.cbWndExtra     = 0;
848    wc.hInstance      = hInstance;
849    wc.hIcon          = 0;
850    wc.hCursor        = 0;
851    wc.hbrBackground  = 0;
852    wc.lpszMenuName   = 0;
853    wc.lpszClassName  = szWndName;
854
855    RegisterClass(&wc);
856
857    hwnd = CreateWindow(szWndName, szWndName,
858       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
859       CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
860
861    return hwnd;
862
863 }
864
865
866 /*********************************************************************
867  *
868  * Function    :  LogOwnerWindowProc
869  *
870  * Description :  Dummy procedure that does nothing special.
871  *
872  * Parameters  :
873  *          1  :  hwnd = window handle
874  *          2  :  uMsg = message number
875  *          3  :  wParam = first param for this message
876  *          4  :  lParam = next param for this message
877  *
878  * Returns     :  Same as `DefWindowProc'.
879  *
880  *********************************************************************/
881 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
882 {
883    return DefWindowProc(hwnd, uMsg, wParam, lParam);
884
885 }
886
887
888 /*********************************************************************
889  *
890  * Function    :  CreateLogWindow
891  *
892  * Description :  Create the logging window.
893  *
894  * Parameters  :
895  *          1  :  hInstance = application's instance handle
896  *          2  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
897  *
898  * Returns     :  Handle to newly created window.
899  *
900  *********************************************************************/
901 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
902 {
903    static const char *szWndName = "PrivoxyLogWindow";
904    static const char *szWndTitle = "Privoxy";
905
906    HWND hwnd = NULL;
907    HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
908    RECT rcClient;
909    WNDCLASSEX wc;
910
911    memset(&wc, 0, sizeof(wc));
912    wc.cbSize         = sizeof(wc);
913    wc.style          = CS_DBLCLKS;
914    wc.lpfnWndProc    = LogWindowProc;
915    wc.cbClsExtra     = 0;
916    wc.cbWndExtra     = 0;
917    wc.hInstance      = hInstance;
918    wc.hIcon          = g_hiconApp;
919    wc.hCursor        = 0;
920    wc.hbrBackground  = 0;
921    wc.lpszMenuName   = MAKEINTRESOURCE(IDR_LOGVIEW);
922    wc.lpszClassName  = szWndName;
923    wc.hbrBackground  = GetStockObject(WHITE_BRUSH);
924    RegisterClassEx(&wc);
925
926    hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
927       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
928       CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
929
930    /* Now create a child list box */
931    GetClientRect(hwnd, &rcClient);
932
933    /* Create a rich edit control */
934    InitRichEdit();
935    g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
936       ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
937       rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
938       hwnd, NULL, hInstance, NULL);
939 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
940
941    /* Subclass the control to catch certain messages */
942    g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
943    SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
944
945    /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
946    if (!g_bShowOnTaskBar &&
947          (nCmdShow == SW_SHOWMINIMIZED ||
948           nCmdShow == SW_MINIMIZE ||
949           nCmdShow == SW_SHOWMINNOACTIVE))
950    {
951       g_bShowLogWindow = FALSE;
952       nCmdShow = SW_HIDE;
953    }
954
955    ShowWindow(hwnd, nCmdShow);
956    UpdateWindow(hwnd);
957
958
959    GetClientRect(g_hwndLogFrame, &rcClient);
960    SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
961
962    return hwnd;
963
964 }
965
966
967 /*********************************************************************
968  *
969  * Function    :  InitRichEdit
970  *
971  * Description :  Initialise the rich edit control library.
972  *
973  * Parameters  :  None
974  *
975  * Returns     :  TRUE => success, FALSE => failure.
976  *                FIXME: this is backwards to the rest of IJB and to common
977  *                programming practice.  Please use 0 => success instead.
978  *
979  *********************************************************************/
980 BOOL InitRichEdit(void)
981 {
982    static HINSTANCE hInstRichEdit;
983    if (hInstRichEdit == NULL)
984    {
985       g_nRichEditVersion = 0;
986       hInstRichEdit = LoadLibraryA("RICHED20.DLL");
987       if (hInstRichEdit)
988       {
989          g_nRichEditVersion = _RICHEDIT_VER;
990       }
991       else
992       {
993          hInstRichEdit = LoadLibraryA("RICHED32.DLL");
994          if (hInstRichEdit)
995          {
996             g_nRichEditVersion = 0x0100;
997          }
998       }
999    }
1000    return (hInstRichEdit != NULL) ? TRUE : FALSE;
1001
1002 }
1003
1004
1005 /*********************************************************************
1006  *
1007  * Function    :  ShowLogWindow
1008  *
1009  * Description :  Shows or hides the log window.  We will also raise the
1010  *                window on a show command in case it is buried.
1011  *
1012  * Parameters  :
1013  *          1  :  bShow = TRUE to show, FALSE to mimize/hide
1014  *
1015  * Returns     :  N/A
1016  *
1017  *********************************************************************/
1018 void ShowLogWindow(BOOL bShow)
1019 {
1020    if (bShow)
1021    {
1022       SetForegroundWindow(g_hwndLogFrame);
1023       SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
1024       
1025    }
1026    else if (g_bShowOnTaskBar)
1027    {
1028       ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
1029    }
1030    else
1031    {
1032       ShowWindow(g_hwndLogFrame, SW_HIDE);
1033    }
1034 }
1035
1036
1037 /*********************************************************************
1038  *
1039  * Function    :  EditFile
1040  *
1041  * Description :  Opens the specified setting file for editing.
1042  * FIXME: What if the file has no associated application. Check for return values
1043 *        from ShellExecute??
1044  *
1045  * Parameters  :
1046  *          1  :  filename = filename from the config (aka config.txt) file.
1047  *
1048  * Returns     :  N/A
1049  *
1050  *********************************************************************/
1051 void EditFile(const char *filename)
1052 {
1053    if (filename)
1054    {
1055       ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
1056    }
1057
1058 }
1059
1060
1061 /*--------------------------------------------------------------------------*/
1062 /* Windows message handlers                                                 */
1063 /*--------------------------------------------------------------------------*/
1064
1065
1066 /*********************************************************************
1067  *
1068  * Function    :  OnLogRButtonUp
1069  *
1070  * Description :  Handler for WM_RBUTTONUP messages.
1071  *
1072  * Parameters  :
1073  *          1  :  nModifier = wParam from mouse message (unused)
1074  *          2  :  x = x coordinate of the mouse event
1075  *          3  :  y = y coordinate of the mouse event
1076  *
1077  * Returns     :  N/A
1078  *
1079  *********************************************************************/
1080 void OnLogRButtonUp(int nModifier, int x, int y)
1081 {
1082    HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1083    if (hMenu != NULL)
1084    {
1085       HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1086
1087       /* Check if there is a selection */
1088       CHARRANGE range;
1089       SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1090       if (range.cpMin == range.cpMax)
1091       {
1092          EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1093       }
1094       else
1095       {
1096          EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1097       }
1098
1099       /* Display the popup */
1100       TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1101       DestroyMenu(hMenu);
1102    }
1103
1104 }
1105
1106
1107 /*********************************************************************
1108  *
1109  * Function    :  OnLogCommand
1110  *
1111  * Description :  Handler for WM_COMMAND messages.
1112  *
1113  * Parameters  :
1114  *          1  :  nCommand = the command portion of the menu selection event
1115  *
1116  * Returns     :  N/A
1117  *
1118  *********************************************************************/
1119 void OnLogCommand(int nCommand)
1120 {
1121    switch (nCommand)
1122    {
1123       case ID_TOGGLE_SHOWWINDOW:
1124          g_bShowLogWindow = !g_bShowLogWindow;
1125
1126          ShowLogWindow(g_bShowLogWindow);
1127          break;
1128
1129       case ID_FILE_EXIT:
1130          PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1131          break;
1132
1133       case ID_EDIT_COPY:
1134          SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1135          break;
1136
1137       case ID_VIEW_CLEARLOG:
1138          SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1139          break;
1140
1141       case ID_VIEW_LOGMESSAGES:
1142          g_bLogMessages = !g_bLogMessages;
1143          /* SaveLogSettings(); */
1144          break;
1145
1146       case ID_VIEW_MESSAGEHIGHLIGHTING:
1147          g_bHighlightMessages = !g_bHighlightMessages;
1148          /* SaveLogSettings(); */
1149          break;
1150
1151       case ID_VIEW_LIMITBUFFERSIZE:
1152          g_bLimitBufferSize = !g_bLimitBufferSize;
1153          /* SaveLogSettings(); */
1154          break;
1155
1156       case ID_VIEW_ACTIVITYANIMATION:
1157          g_bShowActivityAnimation = !g_bShowActivityAnimation;
1158          /* SaveLogSettings(); */
1159          break;
1160
1161 #ifdef FEATURE_TOGGLE
1162       /* by haroon - change toggle to its opposite value */
1163       case ID_TOGGLE_ENABLED:
1164          global_toggle_state = !global_toggle_state;
1165          if (global_toggle_state)
1166          {
1167             log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1168          }
1169          else
1170          {
1171             log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1172          }
1173          SetIdleIcon();
1174          break;
1175 #endif /* def FEATURE_TOGGLE */
1176
1177       case ID_TOOLS_EDITCONFIG:
1178          EditFile(configfile);
1179          break;
1180
1181       case ID_TOOLS_EDITDEFAULTACTIONS:
1182          EditFile(g_default_actions_file);
1183          break;
1184
1185       case ID_TOOLS_EDITUSERACTIONS:
1186          EditFile(g_user_actions_file);
1187          break;
1188
1189       case ID_TOOLS_EDITFILTERS:
1190          EditFile(g_re_filterfile);
1191          break;
1192
1193 #ifdef FEATURE_TRUST
1194       case ID_TOOLS_EDITTRUST:
1195          EditFile(g_trustfile);
1196          break;
1197 #endif /* def FEATURE_TRUST */
1198
1199       case ID_HELP_GPL:
1200          ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
1201          break;
1202
1203       case ID_HELP_FAQ:
1204          ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
1205          break;
1206
1207       case ID_HELP_MANUAL:
1208          ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
1209          break;
1210
1211       case ID_HELP_STATUS:
1212          ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1213          break;
1214
1215       case ID_HELP_ABOUT:
1216          MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1217          break;
1218
1219       default:
1220          /* DO NOTHING */
1221          break;
1222    }
1223
1224 }
1225
1226
1227 /*********************************************************************
1228  *
1229  * Function    :  OnLogInitMenu
1230  *
1231  * Description :  Handler for WM_INITMENU messages.  Enable, disable,
1232  *                check, and/or uncheck menu options as apropos.
1233  *
1234  * Parameters  :
1235  *          1  :  hmenu = handle to menu to "make current"
1236  *
1237  * Returns     :  N/A
1238  *
1239  *********************************************************************/
1240 void OnLogInitMenu(HMENU hmenu)
1241 {
1242    /* Only enable editors if there is a file to edit */
1243    EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1244    EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1245    EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1246 #ifdef FEATURE_TRUST
1247    EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1248 #endif /* def FEATURE_TRUST */
1249
1250    /* Check/uncheck options */
1251    CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1252    CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1253    CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1254    CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1255 #ifdef FEATURE_TOGGLE
1256    /* by haroon - menu item for Enable toggle on/off */
1257    CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1258 #endif /* def FEATURE_TOGGLE */
1259    CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1260
1261 }
1262
1263
1264 /*********************************************************************
1265  *
1266  * Function    :  OnLogTimer
1267  *
1268  * Description :  Handler for WM_TIMER messages.
1269  *
1270  * Parameters  :
1271  *          1  :  nTimer = timer id (animation start/stop or clip buffer)
1272  *
1273  * Returns     :  N/A
1274  *
1275  *********************************************************************/
1276 void OnLogTimer(int nTimer)
1277 {
1278    switch (nTimer)
1279    {
1280       case TIMER_ANIM_ID:
1281          TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1282          break;
1283
1284       case TIMER_ANIMSTOP_ID:
1285          g_nAnimFrame = 0;
1286          SetIdleIcon();
1287          KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1288          KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1289          break;
1290
1291       case TIMER_CLIPBUFFER_ID:
1292       case TIMER_CLIPBUFFER_FORCE_ID:
1293          LogClipBuffer();
1294          g_bClipPending = FALSE;
1295          KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1296          KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1297          break;
1298
1299       default:
1300          /* DO NOTHING */
1301          break;
1302    }
1303
1304 }
1305
1306
1307 /*********************************************************************
1308  *
1309  * Function    :  SetIdleIcon
1310  *
1311  * Description :  Sets the tray icon to either idle or off
1312  *
1313  * Parameters  :  none
1314  *
1315  * Returns     :  N/A
1316  *
1317  *********************************************************************/
1318 void SetIdleIcon()
1319 {
1320 #ifdef FEATURE_TOGGLE
1321          if (!global_toggle_state)
1322          {
1323             TraySetIcon(g_hwndTray, 1, g_hiconOff);
1324             /* log_error(LOG_LEVEL_INFO, "Privoxy OFF icon selected."); */
1325          }
1326          else
1327 #endif /* def FEATURE_TOGGLE */
1328          TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1329 }
1330
1331
1332 /*********************************************************************
1333  *
1334  * Function    :  LogRichEditProc
1335  *
1336  * Description :  Window subclass routine handles some events for the rich edit control.
1337  *
1338  * Parameters  :
1339  *          1  :  hwnd = window handle of the rich edit control
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 LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1348 {
1349    switch (uMsg)
1350    {
1351       case WM_RBUTTONUP:
1352       {
1353          POINT pt;
1354          pt.x = LOWORD(lParam);
1355          pt.y = HIWORD(lParam);
1356          ClientToScreen(hwnd, &pt);
1357          OnLogRButtonUp(wParam, pt.x, pt.y);
1358          return 0;
1359       }
1360       case WM_CHAR:
1361       {
1362          if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1363          {
1364              OnLogCommand(ID_VIEW_CLEARLOG);
1365              return 0;
1366          }
1367       }
1368    }
1369    return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1370
1371 }
1372
1373
1374 /*********************************************************************
1375  *
1376  * Function    :  LogWindowProc
1377  *
1378  * Description :  Windows call back routine handles events on the log window.
1379  *
1380  * Parameters  :
1381  *          1  :  hwnd = handle of the logging window
1382  *          2  :  uMsg = message number
1383  *          3  :  wParam = first param for this message
1384  *          4  :  lParam = next param for this message
1385  *
1386  * Returns     :  Appropriate M$ window message handler codes.
1387  *
1388  *********************************************************************/
1389 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1390 {
1391    switch (uMsg)
1392    {
1393       case WM_CREATE:
1394          return 0;
1395
1396       case WM_CLOSE:
1397          /* This is the end - beautiful friend - the end */
1398          DestroyWindow(g_hwndLogBox);
1399          DestroyWindow(g_hwndLogFrame);
1400          return 0;
1401
1402       case WM_DESTROY:
1403          PostQuitMessage(0);
1404          return 0;
1405
1406       case WM_SHOWWINDOW:
1407          g_bShowLogWindow = wParam;
1408       case WM_SIZE:
1409          /* Resize the logging window to fit the new frame */
1410          if (g_hwndLogBox)
1411          {
1412             RECT rc;
1413             GetClientRect(g_hwndLogFrame, &rc);
1414             SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1415          }
1416          return 0;
1417
1418       case WM_INITMENU:
1419          OnLogInitMenu((HMENU) wParam);
1420          return 0;
1421
1422       case WM_TIMER:
1423          OnLogTimer(wParam);
1424          return 0;
1425
1426       case WM_COMMAND:
1427          OnLogCommand(LOWORD(wParam));
1428          return 0;
1429
1430       case WM_SYSCOMMAND:
1431          switch (wParam)
1432          {
1433             case SC_CLOSE:
1434                if (g_bCloseHidesWindow)
1435                {
1436                   ShowLogWindow(FALSE);
1437                   return 0;
1438                }
1439                break;
1440             case SC_MINIMIZE:
1441                ShowLogWindow(FALSE);
1442                return 0;
1443          }
1444          break;
1445
1446       case WM_CHAR:
1447          if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1448          {
1449              OnLogCommand(ID_VIEW_CLEARLOG);
1450              return 0;
1451          }
1452          break;
1453    }
1454
1455    return DefWindowProc(hwnd, uMsg, wParam, lParam);
1456
1457 }
1458
1459 #endif /* ndef _WIN_CONSOLE - entire file */
1460
1461 /*
1462   Local Variables:
1463   tab-width: 3
1464   end:
1465 */