- Enabled filtering banners by size rather than URL
[privoxy.git] / w32rulesdlg.c
1 const char w32rulesdlg_rcs[] = "$Id: w32rulesdlg.c,v 1.1.1.1 2001/05/15 13:59:08 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/w32rulesdlg.c,v $
5  *
6  * Purpose     :  A dialog to allow GUI editing of the rules.
7  *                Unfinished.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
10  *                IJBSWA team.  http://ijbswa.sourceforge.net
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: w32rulesdlg.c,v $
35  *    Revision 1.1.1.1  2001/05/15 13:59:08  oes
36  *    Initial import of version 2.9.3 source tree
37  *
38  *
39  *********************************************************************/
40 \f
41 #include "config.h"
42
43 #include <stdio.h>
44
45 #include <windows.h>
46 #include <commctrl.h>
47
48 #include "w32res.h"
49 #include "w32rulesdlg.h"
50 #include "win32.h"
51
52 #ifdef __MINGW32__
53 #include "cygwin.h"
54 #endif
55
56 const char w32rulesdlg_h_rcs[] = W32RULESDLG_H_VERSION;
57 \r
58 #ifndef _WIN_CONSOLE /* entire file */\r
59
60 const int nSmallIconWidth = 16;
61 const int nSmallIconHeight = 16;
62
63 static BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
64
65 static HIMAGELIST g_hImageList = NULL;
66 static char *g_pszDefaultRule;
67 static BOOL g_bDirty = FALSE;
68
69
70
71 /*********************************************************************
72  *
73  * Function    :  ShowRulesDialog
74  *
75  * Description :  (Please fill me in!)
76  *
77  * Parameters  :
78  *          1  :  hwndParent = (what?)
79  *
80  * Returns     :  (Please fill me in!)
81  *
82  *********************************************************************/
83 int ShowRulesDialog(HWND hwndParent)
84 {
85    DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_RULES), hwndParent, DialogProc);
86    return TRUE;
87
88 }
89
90
91 /*********************************************************************
92  *
93  * Function    :  SetDefaultRule
94  *
95  * Description :  (Please fill me in!)
96  *
97  * Parameters  :
98  *          1  :  pszRule = (what?)
99  *
100  * Returns     :  N/A
101  *
102  *********************************************************************/
103 void SetDefaultRule(const char *pszRule)
104 {
105    if (pszRule == NULL)
106    {
107       if (g_pszDefaultRule)
108       {
109          free(g_pszDefaultRule);
110          g_pszDefaultRule = NULL;
111       }
112    }
113    else
114    {
115       g_pszDefaultRule = strdup(pszRule);
116    }
117
118 }
119
120
121 #define IMAGE_ALLOW 0
122 #define IMAGE_DENY  1
123
124 /*********************************************************************
125  *
126  * Function    :  InsertRule
127  *
128  * Description :  (Please fill me in!)
129  *
130  * Parameters  :
131  *          1  :  hwndListView = (what?)
132  *          2  :  pszRule = (what?)
133  *          3  :  bAllow = (what?)
134  *
135  * Returns     :  N/A
136  *
137  *********************************************************************/
138 static void InsertRule(HWND hwndListView, const char *pszRule, BOOL bAllow)
139 {
140    LVITEM item;
141    item.mask = LVIF_TEXT | LVIF_IMAGE;
142    item.pszText = (char *)pszRule;
143    item.iItem = ListView_GetItemCount(hwndListView) + 1;
144    item.iSubItem = 0;
145    item.iImage = bAllow ? IMAGE_ALLOW : IMAGE_DENY;
146    ListView_InsertItem(hwndListView, &item);
147    /* TODO add subitem for whether the rule is always or never */
148
149 }
150
151
152 /*********************************************************************
153  *
154  * Function    :  SetDirty
155  *
156  * Description :  (Please fill me in!)
157  *
158  * Parameters  :
159  *          1  :  bDirty = (what?)
160  *
161  * Returns     :  N/A
162  *
163  *********************************************************************/
164 static void SetDirty(BOOL bDirty)
165 {
166    g_bDirty = bDirty;
167    /* TODO Change some values */
168
169 }
170
171
172 /*********************************************************************
173  *
174  * Function    :  OnInitDialog
175  *
176  * Description :  (Please fill me in!)
177  *
178  * Parameters  :
179  *          1  :  hwndDlg = (what?)
180  *
181  * Returns     :  N/A
182  *
183  *********************************************************************/
184 static void OnInitDialog(HWND hwndDlg)
185 {
186    LVCOLUMN aCols[2];
187    HWND hwndListView;
188    RECT rcListView;
189    int cx;
190
191    if (g_hImageList == NULL)
192    {
193       /* Create image list and add icons */
194       HICON hIconDeny = LoadImage(g_hInstance, MAKEINTRESOURCE(IDI_DENYRULE), IMAGE_ICON, nSmallIconWidth, nSmallIconHeight, 0);
195       HICON hIconAllow = LoadImage(g_hInstance, MAKEINTRESOURCE(IDI_ALLOWRULE), IMAGE_ICON, nSmallIconWidth, nSmallIconHeight, 0);
196       g_hImageList = ImageList_Create(nSmallIconWidth, nSmallIconHeight, ILC_COLOR | ILC_MASK, 0, 10);
197       ImageList_AddIcon(g_hImageList, hIconAllow);
198       ImageList_AddIcon(g_hImageList, hIconDeny);
199    }
200
201    /* Set the default rule value if there is one */
202    if (g_pszDefaultRule)
203    {
204       SetDlgItemText(hwndDlg, IDC_NEW, g_pszDefaultRule);
205       SetDefaultRule(NULL);
206    }
207
208    /* Initialise the list view */
209    hwndListView = GetDlgItem(hwndDlg, IDC_RULES);
210    ListView_SetImageList(hwndListView, g_hImageList, LVSIL_SMALL);
211    GetClientRect(hwndListView, &rcListView);
212    cx = rcListView.right - rcListView.left;
213    aCols[0].mask = LVCF_TEXT | LVCF_WIDTH;
214    aCols[0].pszText = "Rule";
215    aCols[0].cx = (70 * cx) / 100;
216    aCols[1].mask = LVCF_TEXT | LVCF_WIDTH;
217    aCols[1].pszText = "Applies when";
218    aCols[1].cx = cx - aCols[0].cx;
219    ListView_InsertColumn(hwndListView, 0, &aCols[0]);
220    ListView_InsertColumn(hwndListView, 1, &aCols[1]);
221
222    /* Read and add rules to the list */
223    /* TODO */
224    InsertRule(hwndListView, "Test rule 1", TRUE);
225    InsertRule(hwndListView, "Test rule 2", TRUE);
226    InsertRule(hwndListView, "Test rule 3", FALSE);
227    InsertRule(hwndListView, "Test rule 4", FALSE);
228
229 }
230
231
232 /*********************************************************************
233  *
234  * Function    :  GetFirstSelectedItem
235  *
236  * Description :  (Please fill me in!)
237  *
238  * Parameters  :
239  *          1  :  hwndDlg = (what?)
240  *
241  * Returns     :  (Please fill me in!)
242  *
243  *********************************************************************/
244 static int GetFirstSelectedItem(HWND hwndDlg)
245 {
246    /* Check for selected items */
247    HWND hwndListView = GetDlgItem(hwndDlg, IDC_RULES);
248    int nItem = -1;
249    do
250    {
251       nItem = ListView_GetNextItem(hwndListView, nItem, LVNI_SELECTED);
252       if (nItem >= 0)
253       {
254          return nItem;
255       }
256    } while (nItem >= 0);
257    return -1;
258
259 }
260
261
262 /*********************************************************************
263  *
264  * Function    :  OnRulesItemChanged
265  *
266  * Description :  (Please fill me in!)
267  *
268  * Parameters  :
269  *          1  :  hwndDlg = (what?)
270  *
271  * Returns     :  N/A
272  *
273  *********************************************************************/
274 static void OnRulesItemChanged(HWND hwndDlg)
275 {
276    int nItem = GetFirstSelectedItem(hwndDlg);
277    HWND hwndListView = GetDlgItem(hwndDlg, IDC_RULES);
278    int nItems = ListView_GetItemCount(hwndListView);
279    BOOL bHaveSelection = (nItem >= 0) ? TRUE : FALSE;
280    BOOL bMoveUp = (bHaveSelection && nItem > 0) ? TRUE : FALSE;
281    BOOL bMoveDown = (bHaveSelection && nItem < nItems - 1) ? TRUE : FALSE;
282
283    /* Enable/disable buttons */
284    EnableWindow(GetDlgItem(hwndDlg, IDC_DELETE), bHaveSelection);
285    EnableWindow(GetDlgItem(hwndDlg, IDC_MOVEUP), bMoveUp);
286    EnableWindow(GetDlgItem(hwndDlg, IDC_MOVEDOWN), bMoveDown);
287
288 }
289
290
291 /*********************************************************************
292  *
293  * Function    :  MoveRules
294  *
295  * Description :  (Please fill me in!)
296  *
297  * Parameters  :
298  *          1  :  hwndDlg = (what?)
299  *          2  :  bMoveUp = (what?)
300  *
301  * Returns     :  N/A
302  *
303  *********************************************************************/
304 static void MoveRules(HWND hwndDlg, BOOL bMoveUp)
305 {
306 }
307
308
309 /*********************************************************************
310  *
311  * Function    :  OnMoveRuleUpClick
312  *
313  * Description :  (Please fill me in!)
314  *
315  * Parameters  :
316  *          1  :  hwndDlg = (what?)
317  *
318  * Returns     :  N/A
319  *
320  *********************************************************************/
321 static void OnMoveRuleUpClick(HWND hwndDlg)
322 {
323 }
324
325
326 /*********************************************************************
327  *
328  * Function    :  OnMoveRuleDownClick
329  *
330  * Description :  (Please fill me in!)
331  *
332  * Parameters  :
333  *          1  :  hwndDlg = (what?)
334  *
335  * Returns     :  N/A
336  *
337  *********************************************************************/
338 static void OnMoveRuleDownClick(HWND hwndDlg)
339 {
340 }
341
342
343 /*********************************************************************
344  *
345  * Function    :  OnCreateRuleClick
346  *
347  * Description :  (Please fill me in!)
348  *
349  * Parameters  :
350  *          1  :  hwndDlg = (what?)
351  *
352  * Returns     :  N/A
353  *
354  *********************************************************************/
355 static void OnCreateRuleClick(HWND hwndDlg)
356 {
357 }
358
359
360 /*********************************************************************
361  *
362  * Function    :  OnDeleteRuleClick
363  *
364  * Description :  (Please fill me in!)
365  *
366  * Parameters  :
367  *          1  :  hwndDlg = (what?)
368  *
369  * Returns     :  N/A
370  *
371  *********************************************************************/
372 static void OnDeleteRuleClick(HWND hwndDlg)
373 {
374    /* Get selection and remove it */
375    int nItem = GetFirstSelectedItem(hwndDlg);
376    if (nItem >= 0)
377    {
378       LVITEM item;
379       HWND hwndListView = GetDlgItem(hwndDlg, IDC_RULES);
380       item.mask = LVIF_PARAM;
381       item.iItem = nItem;
382       item.iSubItem = 0;
383       ListView_GetItem(hwndListView, &item);
384       /* TODO erase data stored with item */
385       ListView_DeleteItem(hwndListView, nItem);
386    }
387
388 }
389
390
391 /*********************************************************************
392  *
393  * Function    :  OnCommand
394  *
395  * Description :  (Please fill me in!)
396  *
397  * Parameters  :
398  *          1  :  hwndDlg = (what?)
399  *          2  :  nCommand = (what?)
400  *          3  :  nNotifyCode = (what?)
401  *          4  :  hwndItem = (what?)
402  *
403  * Returns     :  N/A
404  *
405  *********************************************************************/
406 static void OnCommand(HWND hwndDlg, int nCommand, int nNotifyCode, HWND hwndItem)
407 {
408    switch (nCommand)
409    {
410       case IDCANCEL:
411       case IDC_SAVE:
412          EndDialog(hwndDlg, IDOK);
413          break;
414       case IDC_CREATE:
415          if (nNotifyCode == BN_CLICKED)
416          {
417             OnCreateRuleClick(hwndDlg);
418          }
419          break;
420       case IDC_DELETE:
421          if (nNotifyCode == BN_CLICKED)
422          {
423             OnDeleteRuleClick(hwndDlg);
424          }
425          break;
426       case IDC_MOVEUP:
427          if (nNotifyCode == BN_CLICKED)
428          {
429             OnMoveRuleUpClick(hwndDlg);
430          }
431          break;
432       case IDC_MOVEDOWN:
433          if (nNotifyCode == BN_CLICKED)
434          {
435             OnMoveRuleDownClick(hwndDlg);
436          }
437          break;
438    }
439
440 }
441
442
443 /*********************************************************************
444  *
445  * Function    :  OnNotify
446  *
447  * Description :  (Please fill me in!)
448  *
449  * Parameters  :
450  *          1  :  hwndDlg = (what?)
451  *          2  :  nIdCtrl = (what?)
452  *          3  :  pnmh = (what?)
453  *
454  * Returns     :  N/A
455  *
456  *********************************************************************/
457 static void OnNotify(HWND hwndDlg, int nIdCtrl, LPNMHDR pnmh)
458 {
459    switch (nIdCtrl)
460    {
461       case IDC_RULES:
462          switch (pnmh->code)
463          {
464             case LVN_ITEMCHANGED:
465                OnRulesItemChanged(hwndDlg);
466                break;
467          }
468          break;
469    }
470
471 }
472
473
474 /*********************************************************************
475  *
476  * Function    :  OnDestroy
477  *
478  * Description :  (Please fill me in!)
479  *
480  * Parameters  :
481  *          1  :  hwndDlg = (what?)
482  *
483  * Returns     :  N/A
484  *
485  *********************************************************************/
486 static void OnDestroy(HWND hwndDlg)
487 {
488    /* TODO any destruction cleanup */
489
490 }
491
492
493 /*********************************************************************
494  *
495  * Function    :  DialogProc
496  *
497  * Description :  (Please fill me in!)
498  *
499  * Parameters  :
500  *          1  :  hwndDlg = (what?)
501  *          2  :  uMsg = (what?)
502  *          3  :  wParam = (what?)
503  *          4  :  lParam = (what?)
504  *
505  * Returns     :  (Please fill me in!)
506  *
507  *********************************************************************/
508 static BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
509 {
510    switch (uMsg)
511    {
512       case WM_INITDIALOG:
513          OnInitDialog(hwndDlg);
514          return TRUE;
515
516       case WM_DESTROY:
517          OnDestroy(hwndDlg);
518          return TRUE;
519
520       case WM_COMMAND:
521          OnCommand(hwndDlg, LOWORD(wParam), HIWORD(wParam), (HWND) lParam);
522          break;
523
524       case WM_NOTIFY:
525          OnNotify(hwndDlg, (int) wParam, (LPNMHDR) lParam);
526          break;
527    }
528    return FALSE;
529
530 }
531
532 #endif /* ndef _WIN_CONSOLE - entire file */\r
533
534 /*
535   Local Variables:
536   tab-width: 3
537   end:
538 */