Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / Paths.c
1 /* Paths.c- pixmap/icon paths
2  *
3  *  WPrefs - Window Maker Preferences Program
4  *
5  *  Copyright (c) 1998-2003 Alfredo K. Kojima
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20  *  USA.
21  */
22
23 #include "WPrefs.h"
24 #include <unistd.h>
25
26 typedef struct _Panel {
27         WMBox *box;
28         char *sectionName;
29
30         char *description;
31
32         CallbackRec callbacks;
33
34         WMWidget *parent;
35
36         WMTabView *tabv;
37
38         WMFrame *pixF;
39         WMList *pixL;
40         WMButton *pixaB;
41         WMButton *pixrB;
42
43         WMFrame *icoF;
44         WMList *icoL;
45         WMButton *icoaB;
46         WMButton *icorB;
47
48         WMColor *red;
49         WMColor *black;
50         WMColor *white;
51         WMColor *gray;
52         WMFont *font;
53 } _Panel;
54
55 #define ICON_FILE       "paths"
56
57 static void addPathToList(WMList * list, int index, char *path)
58 {
59         char *fpath = wexpandpath(path);
60         WMListItem *item;
61
62         item = WMInsertListItem(list, index, path);
63
64         if (access(fpath, X_OK) != 0) {
65                 item->uflags = 1;
66         }
67         wfree(fpath);
68 }
69
70 static void showData(_Panel * panel)
71 {
72         WMPropList *array, *val;
73         int i;
74
75         array = GetObjectForKey("IconPath");
76         if (!array || !WMIsPLArray(array)) {
77                 if (array)
78                         wwarning(_("bad value in option IconPath. Using default path list"));
79                 addPathToList(panel->icoL, -1, "~/pixmaps");
80                 addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
81                 addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
82                 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
83                 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
84                 addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
85         } else {
86                 for (i = 0; i < WMGetPropListItemCount(array); i++) {
87                         val = WMGetFromPLArray(array, i);
88                         addPathToList(panel->icoL, -1, WMGetFromPLString(val));
89                 }
90         }
91
92         array = GetObjectForKey("PixmapPath");
93         if (!array || !WMIsPLArray(array)) {
94                 if (array)
95                         wwarning(_("bad value in option PixmapPath. Using default path list"));
96                 addPathToList(panel->pixL, -1, "~/pixmaps");
97                 addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
98                 addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
99         } else {
100                 for (i = 0; i < WMGetPropListItemCount(array); i++) {
101                         val = WMGetFromPLArray(array, i);
102                         addPathToList(panel->pixL, -1, WMGetFromPLString(val));
103                 }
104         }
105 }
106
107 static void pushButton(WMWidget * w, void *data)
108 {
109         _Panel *panel = (_Panel *) data;
110         int i;
111
112         /* icon paths */
113         if (w == panel->icorB) {
114                 i = WMGetListSelectedItemRow(panel->icoL);
115
116                 if (i >= 0)
117                         WMRemoveListItem(panel->icoL, i);
118         }
119
120         /* pixmap paths */
121         if (w == panel->pixrB) {
122                 i = WMGetListSelectedItemRow(panel->pixL);
123
124                 if (i >= 0)
125                         WMRemoveListItem(panel->pixL, i);
126         }
127 }
128
129 static void browseForFile(WMWidget * w, void *data)
130 {
131         _Panel *panel = (_Panel *) data;
132         WMFilePanel *filePanel;
133
134         filePanel = WMGetOpenPanel(WMWidgetScreen(w));
135
136         WMSetFilePanelCanChooseFiles(filePanel, False);
137
138         if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/", _("Select directory"), NULL) == True) {
139                 char *str = WMGetFilePanelFileName(filePanel);
140
141                 if (str) {
142                         int len = strlen(str);
143
144                         /* Remove the trailing '/' except if the path is exactly / */
145                         if (len > 1 && str[len - 1] == '/') {
146                                 str[len - 1] = '\0';
147                                 len--;
148                         }
149                         if (len > 0) {
150                                 WMList *lPtr;
151                                 int i;
152
153                                 if (w == panel->icoaB)
154                                         lPtr = panel->icoL;
155                                 else if (w == panel->pixaB)
156                                         lPtr = panel->pixL;
157
158                                 i = WMGetListSelectedItemRow(lPtr);
159                                 if (i >= 0)
160                                         i++;
161                                 addPathToList(lPtr, i, str);
162                                 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
163
164                                 wfree(str);
165                         }
166                 }
167         }
168 }
169
170 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
171 {
172         int width, height, x, y;
173         _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
174         WMScreen *scr = WMWidgetScreen(lPtr);
175         Display *dpy = WMScreenDisplay(scr);
176         WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
177
178         width = rect->size.width;
179         height = rect->size.height;
180         x = rect->pos.x;
181         y = rect->pos.y;
182
183         XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
184
185         if (state & 1) {
186                 WMDrawString(scr, d, panel->red, panel->font, x + 4, y, text, strlen(text));
187         } else {
188                 WMDrawString(scr, d, panel->black, panel->font, x + 4, y, text, strlen(text));
189         }
190 }
191
192 static void storeData(_Panel * panel)
193 {
194         WMPropList *list;
195         WMPropList *tmp;
196         int i;
197         char *p;
198
199         list = WMCreatePLArray(NULL, NULL);
200         for (i = 0; i < WMGetListNumberOfRows(panel->icoL); i++) {
201                 p = WMGetListItem(panel->icoL, i)->text;
202                 tmp = WMCreatePLString(p);
203                 WMAddToPLArray(list, tmp);
204         }
205         SetObjectForKey(list, "IconPath");
206
207         list = WMCreatePLArray(NULL, NULL);
208         for (i = 0; i < WMGetListNumberOfRows(panel->pixL); i++) {
209                 p = WMGetListItem(panel->pixL, i)->text;
210                 tmp = WMCreatePLString(p);
211                 WMAddToPLArray(list, tmp);
212         }
213         SetObjectForKey(list, "PixmapPath");
214 }
215
216 static void createPanel(Panel * p)
217 {
218         _Panel *panel = (_Panel *) p;
219         WMScreen *scr = WMWidgetScreen(panel->parent);
220         WMTabViewItem *tab;
221
222         panel->white = WMWhiteColor(scr);
223         panel->black = WMBlackColor(scr);
224         panel->gray = WMGrayColor(scr);
225         panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
226         panel->font = WMSystemFontOfSize(scr, 12);
227
228         panel->box = WMCreateBox(panel->parent);
229         WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
230
231         panel->tabv = WMCreateTabView(panel->box);
232         WMMoveWidget(panel->tabv, 12, 10);
233         WMResizeWidget(panel->tabv, 500, 215);
234
235         /* icon path */
236         panel->icoF = WMCreateFrame(panel->box);
237         WMSetFrameRelief(panel->icoF, WRFlat);
238         WMResizeWidget(panel->icoF, 230, 210);
239
240         tab = WMCreateTabViewItemWithIdentifier(0);
241         WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
242         WMAddItemInTabView(panel->tabv, tab);
243         WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
244
245         panel->icoL = WMCreateList(panel->icoF);
246         WMResizeWidget(panel->icoL, 480, 147);
247         WMMoveWidget(panel->icoL, 10, 10);
248         WMSetListUserDrawProc(panel->icoL, paintItem);
249         WMHangData(panel->icoL, panel);
250
251         panel->icoaB = WMCreateCommandButton(panel->icoF);
252         WMResizeWidget(panel->icoaB, 95, 24);
253         WMMoveWidget(panel->icoaB, 293, 165);
254         WMSetButtonText(panel->icoaB, _("Add"));
255         WMSetButtonAction(panel->icoaB, browseForFile, panel);
256         WMSetButtonImagePosition(panel->icoaB, WIPRight);
257
258         panel->icorB = WMCreateCommandButton(panel->icoF);
259         WMResizeWidget(panel->icorB, 95, 24);
260         WMMoveWidget(panel->icorB, 395, 165);
261         WMSetButtonText(panel->icorB, _("Remove"));
262         WMSetButtonAction(panel->icorB, pushButton, panel);
263
264         WMMapSubwidgets(panel->icoF);
265
266         /* pixmap path */
267         panel->pixF = WMCreateFrame(panel->box);
268         WMSetFrameRelief(panel->pixF, WRFlat);
269         WMResizeWidget(panel->pixF, 230, 210);
270
271         tab = WMCreateTabViewItemWithIdentifier(0);
272         WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
273         WMAddItemInTabView(panel->tabv, tab);
274         WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
275
276         panel->pixL = WMCreateList(panel->pixF);
277         WMResizeWidget(panel->pixL, 480, 147);
278         WMMoveWidget(panel->pixL, 10, 10);
279         WMSetListUserDrawProc(panel->pixL, paintItem);
280         WMHangData(panel->pixL, panel);
281
282         panel->pixaB = WMCreateCommandButton(panel->pixF);
283         WMResizeWidget(panel->pixaB, 95, 24);
284         WMMoveWidget(panel->pixaB, 293, 165);
285         WMSetButtonText(panel->pixaB, _("Add"));
286         WMSetButtonAction(panel->pixaB, browseForFile, panel);
287         WMSetButtonImagePosition(panel->pixaB, WIPRight);
288
289         panel->pixrB = WMCreateCommandButton(panel->pixF);
290         WMResizeWidget(panel->pixrB, 95, 24);
291         WMMoveWidget(panel->pixrB, 395, 165);
292         WMSetButtonText(panel->pixrB, _("Remove"));
293         WMSetButtonAction(panel->pixrB, pushButton, panel);
294
295         WMMapSubwidgets(panel->pixF);
296
297         WMRealizeWidget(panel->box);
298         WMMapSubwidgets(panel->box);
299
300         showData(panel);
301 }
302
303 Panel *InitPaths(WMScreen * scr, WMWidget * parent)
304 {
305         _Panel *panel;
306
307         panel = wmalloc(sizeof(_Panel));
308         memset(panel, 0, sizeof(_Panel));
309
310         panel->sectionName = _("Search Path Configuration");
311
312         panel->description = _("Search paths to use when looking for pixmaps\n" "and icons.");
313
314         panel->parent = parent;
315
316         panel->callbacks.createWidgets = createPanel;
317         panel->callbacks.updateDomain = storeData;
318
319         AddSection(panel, ICON_FILE);
320
321         return panel;
322 }