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