changed indentation to use spaces only
[wmaker-crm.git] / WPrefs.app / Paths.c
blob8a72d4aaaeeb23f1c120139008e3a0884b87d729
1 /* Paths.c- pixmap/icon paths
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
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.
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.
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.
24 #include "WPrefs.h"
25 #include <unistd.h>
27 typedef struct _Panel {
28 WMBox *box;
29 char *sectionName;
31 char *description;
33 CallbackRec callbacks;
35 WMWidget *parent;
37 WMTabView *tabv;
39 WMFrame *pixF;
40 WMList *pixL;
41 WMButton *pixaB;
42 WMButton *pixrB;
44 WMFrame *icoF;
45 WMList *icoL;
46 WMButton *icoaB;
47 WMButton *icorB;
49 WMColor *red;
50 WMColor *black;
51 WMColor *white;
52 WMColor *gray;
53 WMFont *font;
54 } _Panel;
58 #define ICON_FILE "paths"
61 static void
62 addPathToList(WMList *list, int index, char *path)
64 char *fpath = wexpandpath(path);
65 WMListItem *item;
67 item = WMInsertListItem(list, index, path);
69 if (access(fpath, X_OK)!=0) {
70 item->uflags = 1;
72 wfree(fpath);
76 static void
77 showData(_Panel *panel)
79 WMPropList *array, *val;
80 int i;
82 array = GetObjectForKey("IconPath");
83 if (!array || !WMIsPLArray(array)) {
84 if (array)
85 wwarning(_("bad value in option IconPath. Using default path list"));
86 addPathToList(panel->icoL, -1, "~/pixmaps");
87 addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
88 addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
89 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
90 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
91 addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
92 } else {
93 for (i=0; i<WMGetPropListItemCount(array); i++) {
94 val = WMGetFromPLArray(array, i);
95 addPathToList(panel->icoL, -1, WMGetFromPLString(val));
99 array = GetObjectForKey("PixmapPath");
100 if (!array || !WMIsPLArray(array)) {
101 if (array)
102 wwarning(_("bad value in option PixmapPath. Using default path list"));
103 addPathToList(panel->pixL, -1, "~/pixmaps");
104 addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
105 addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
106 } else {
107 for (i=0; i<WMGetPropListItemCount(array); i++) {
108 val = WMGetFromPLArray(array, i);
109 addPathToList(panel->pixL, -1, WMGetFromPLString(val));
115 static void
116 pushButton(WMWidget *w, void *data)
118 _Panel *panel = (_Panel*)data;
119 int i;
121 /* icon paths */
122 if (w == panel->icorB) {
123 i = WMGetListSelectedItemRow(panel->icoL);
125 if (i>=0)
126 WMRemoveListItem(panel->icoL, i);
129 /* pixmap paths */
130 if (w == panel->pixrB) {
131 i = WMGetListSelectedItemRow(panel->pixL);
133 if (i>=0)
134 WMRemoveListItem(panel->pixL, i);
139 static void
140 browseForFile(WMWidget *w, void *data)
142 _Panel *panel = (_Panel*)data;
143 WMFilePanel *filePanel;
145 filePanel = WMGetOpenPanel(WMWidgetScreen(w));
147 WMSetFilePanelCanChooseFiles(filePanel, False);
149 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/",
150 _("Select directory"), NULL) == True) {
151 char *str = WMGetFilePanelFileName(filePanel);
153 if (str) {
154 int len = strlen(str);
156 /* Remove the trailing '/' except if the path is exactly / */
157 if (len > 1 && str[len-1] == '/') {
158 str[len-1] = '\0';
159 len--;
161 if (len > 0) {
162 WMList *lPtr;
163 int i;
165 if (w == panel->icoaB)
166 lPtr = panel->icoL;
167 else if (w == panel->pixaB)
168 lPtr = panel->pixL;
170 i = WMGetListSelectedItemRow(lPtr);
171 if (i >= 0) i++;
172 addPathToList(lPtr, i, str);
173 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
175 wfree(str);
183 static void
184 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state, WMRect *rect)
186 int width, height, x, y;
187 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
188 WMScreen *scr = WMWidgetScreen(lPtr);
189 Display *dpy = WMScreenDisplay(scr);
190 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
192 width = rect->size.width;
193 height = rect->size.height;
194 x = rect->pos.x;
195 y = rect->pos.y;
197 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
199 if (state & 1) {
200 WMDrawString(scr, d, panel->red, panel->font, x+4, y, text, strlen(text));
201 } else {
202 WMDrawString(scr, d, panel->black, panel->font, x+4, y, text, strlen(text));
208 static void
209 storeData(_Panel *panel)
211 WMPropList *list;
212 WMPropList *tmp;
213 int i;
214 char *p;
216 list = WMCreatePLArray(NULL, NULL);
217 for (i=0; i<WMGetListNumberOfRows(panel->icoL); i++) {
218 p = WMGetListItem(panel->icoL, i)->text;
219 tmp = WMCreatePLString(p);
220 WMAddToPLArray(list, tmp);
222 SetObjectForKey(list, "IconPath");
224 list = WMCreatePLArray(NULL, NULL);
225 for (i=0; i<WMGetListNumberOfRows(panel->pixL); i++) {
226 p = WMGetListItem(panel->pixL, i)->text;
227 tmp = WMCreatePLString(p);
228 WMAddToPLArray(list, tmp);
230 SetObjectForKey(list, "PixmapPath");
235 static void
236 createPanel(Panel *p)
238 _Panel *panel = (_Panel*)p;
239 WMScreen *scr = WMWidgetScreen(panel->parent);
240 WMTabViewItem *tab;
242 panel->white = WMWhiteColor(scr);
243 panel->black = WMBlackColor(scr);
244 panel->gray = WMGrayColor(scr);
245 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
246 panel->font = WMSystemFontOfSize(scr, 12);
248 panel->box = WMCreateBox(panel->parent);
249 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
252 panel->tabv = WMCreateTabView(panel->box);
253 WMMoveWidget(panel->tabv, 12, 10);
254 WMResizeWidget(panel->tabv, 500, 215);
258 /* icon path */
259 panel->icoF = WMCreateFrame(panel->box);
260 WMSetFrameRelief(panel->icoF, WRFlat);
261 WMResizeWidget(panel->icoF, 230, 210);
263 tab = WMCreateTabViewItemWithIdentifier(0);
264 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
265 WMAddItemInTabView(panel->tabv, tab);
266 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
268 panel->icoL = WMCreateList(panel->icoF);
269 WMResizeWidget(panel->icoL, 480, 147);
270 WMMoveWidget(panel->icoL, 10, 10);
271 WMSetListUserDrawProc(panel->icoL, paintItem);
272 WMHangData(panel->icoL, panel);
274 panel->icoaB = WMCreateCommandButton(panel->icoF);
275 WMResizeWidget(panel->icoaB, 95, 24);
276 WMMoveWidget(panel->icoaB, 293, 165);
277 WMSetButtonText(panel->icoaB, _("Add"));
278 WMSetButtonAction(panel->icoaB, browseForFile, panel);
279 WMSetButtonImagePosition(panel->icoaB, WIPRight);
281 panel->icorB = WMCreateCommandButton(panel->icoF);
282 WMResizeWidget(panel->icorB, 95, 24);
283 WMMoveWidget(panel->icorB, 395, 165);
284 WMSetButtonText(panel->icorB, _("Remove"));
285 WMSetButtonAction(panel->icorB, pushButton, panel);
287 WMMapSubwidgets(panel->icoF);
289 /* pixmap path */
290 panel->pixF = WMCreateFrame(panel->box);
291 WMSetFrameRelief(panel->pixF, WRFlat);
292 WMResizeWidget(panel->pixF, 230, 210);
294 tab = WMCreateTabViewItemWithIdentifier(0);
295 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
296 WMAddItemInTabView(panel->tabv, tab);
297 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
299 panel->pixL = WMCreateList(panel->pixF);
300 WMResizeWidget(panel->pixL, 480, 147);
301 WMMoveWidget(panel->pixL, 10, 10);
302 WMSetListUserDrawProc(panel->pixL, paintItem);
303 WMHangData(panel->pixL, panel);
305 panel->pixaB = WMCreateCommandButton(panel->pixF);
306 WMResizeWidget(panel->pixaB, 95, 24);
307 WMMoveWidget(panel->pixaB, 293, 165);
308 WMSetButtonText(panel->pixaB, _("Add"));
309 WMSetButtonAction(panel->pixaB, browseForFile, panel);
310 WMSetButtonImagePosition(panel->pixaB, WIPRight);
312 panel->pixrB = WMCreateCommandButton(panel->pixF);
313 WMResizeWidget(panel->pixrB, 95, 24);
314 WMMoveWidget(panel->pixrB, 395, 165);
315 WMSetButtonText(panel->pixrB, _("Remove"));
316 WMSetButtonAction(panel->pixrB, pushButton, panel);
319 WMMapSubwidgets(panel->pixF);
321 WMRealizeWidget(panel->box);
322 WMMapSubwidgets(panel->box);
324 showData(panel);
329 Panel*
330 InitPaths(WMScreen *scr, WMWidget *parent)
332 _Panel *panel;
334 panel = wmalloc(sizeof(_Panel));
335 memset(panel, 0, sizeof(_Panel));
337 panel->sectionName = _("Search Path Configuration");
339 panel->description = _("Search paths to use when looking for pixmaps\n"
340 "and icons.");
342 panel->parent = parent;
344 panel->callbacks.createWidgets = createPanel;
345 panel->callbacks.updateDomain = storeData;
347 AddSection(panel, ICON_FILE);
349 return panel;