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