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