- fixed a bug with setting initial path in browsers.
[wmaker-crm.git] / WPrefs.app / Paths.c
blob33134777efb7bb0a8c242e5c8862ffc8edb666fa
1 /* Paths.c- pixmap/icon paths
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998 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.
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 WMFrame *frame;
29 char *sectionName;
31 char *description;
33 CallbackRec callbacks;
35 WMWindow *win;
37 WMFrame *pixF;
38 WMList *pixL;
39 WMButton *pixaB;
40 WMButton *pixrB;
42 WMFrame *icoF;
43 WMList *icoL;
44 WMButton *icoaB;
45 WMButton *icorB;
47 WMColor *red;
48 WMColor *black;
49 WMColor *white;
50 WMFont *font;
51 } _Panel;
55 #define ICON_FILE "paths"
58 static void
59 addPathToList(WMList *list, int index, char *path)
61 char *fpath = wexpandpath(path);
62 WMListItem *item;
64 item = WMInsertListItem(list, index, path);
66 if (access(fpath, X_OK)!=0) {
67 item->uflags = 1;
69 free(fpath);
73 static void
74 showData(_Panel *panel)
76 proplist_t array, val;
77 int i;
79 array = GetObjectForKey("IconPath");
80 if (!array || !PLIsArray(array)) {
81 if (array)
82 wwarning(_("bad value in option IconPath. Using default path list"));
83 addPathToList(panel->icoL, -1, "~/pixmaps");
84 addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
85 addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
86 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
87 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
88 addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
89 } else {
90 for (i=0; i<PLGetNumberOfElements(array); i++) {
91 val = PLGetArrayElement(array, i);
92 addPathToList(panel->icoL, -1, PLGetString(val));
96 array = GetObjectForKey("PixmapPath");
97 if (!array || !PLIsArray(array)) {
98 if (array)
99 wwarning(_("bad value in option PixmapPath. Using default path list"));
100 addPathToList(panel->pixL, -1, "~/pixmaps");
101 addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
102 addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
103 } else {
104 for (i=0; i<PLGetNumberOfElements(array); i++) {
105 val = PLGetArrayElement(array, i);
106 addPathToList(panel->pixL, -1, PLGetString(val));
112 static void
113 pushButton(WMWidget *w, void *data)
115 _Panel *panel = (_Panel*)data;
116 int i;
118 /* icon paths */
119 if (w == panel->icorB) {
120 i = WMGetListSelectedItemRow(panel->icoL);
122 if (i>=0)
123 WMRemoveListItem(panel->icoL, i);
126 /* pixmap paths */
127 if (w == panel->pixrB) {
128 i = WMGetListSelectedItemRow(panel->pixL);
130 if (i>=0)
131 WMRemoveListItem(panel->pixL, i);
136 static void
137 browseForFile(WMWidget *w, void *data)
139 _Panel *panel = (_Panel*)data;
140 WMFilePanel *filePanel;
142 filePanel = WMGetOpenPanel(WMWidgetScreen(w));
144 WMSetFilePanelCanChooseFiles(filePanel, False);
146 if (WMRunModalFilePanelForDirectory(filePanel, panel->win, "/",
147 _("Select path"), NULL) == True) {
148 char *str = WMGetFilePanelFileName(filePanel);
150 if (str) {
151 int len = strlen(str);
153 /* Remove the trailing '/' except if the path is exactly / */
154 if (len > 1 && str[len-1] == '/') {
155 str[len-1] = '\0';
156 len--;
158 if (len > 0) {
159 WMList *lPtr;
160 int i;
162 if (w == panel->icoaB)
163 lPtr = panel->icoL;
164 else if (w == panel->pixaB)
165 lPtr = panel->pixL;
167 i = WMGetListSelectedItemRow(lPtr);
168 if (i >= 0) i++;
169 addPathToList(lPtr, i, str);
170 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
172 free(str);
180 static void
181 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
182 WMRect *rect)
184 int width, height, x, y;
185 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
186 WMScreen *scr = WMWidgetScreen(lPtr);
187 Display *dpy = WMScreenDisplay(scr);
189 width = rect->size.width;
190 height = rect->size.height;
191 x = rect->pos.x;
192 y = rect->pos.y;
194 if (state & WLDSSelected)
195 XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width,
196 height);
197 else
198 XClearArea(dpy, d, x, y, width, height, False);
200 if (state & 1) {
201 WMDrawString(scr, d, WMColorGC(panel->red), panel->font, x+4, y,
202 text, strlen(text));
203 } else {
204 WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+4, y,
205 text, strlen(text));
211 static void
212 storeData(_Panel *panel)
214 proplist_t list;
215 proplist_t tmp;
216 int i;
217 char *p;
219 list = PLMakeArrayFromElements(NULL, NULL);
220 for (i=0; i<WMGetListNumberOfRows(panel->icoL); i++) {
221 p = WMGetListItem(panel->icoL, i)->text;
222 tmp = PLMakeString(p);
223 PLAppendArrayElement(list, tmp);
225 SetObjectForKey(list, "IconPath");
227 list = PLMakeArrayFromElements(NULL, NULL);
228 for (i=0; i<WMGetListNumberOfRows(panel->pixL); i++) {
229 p = WMGetListItem(panel->pixL, i)->text;
230 tmp = PLMakeString(p);
231 PLAppendArrayElement(list, tmp);
233 SetObjectForKey(list, "PixmapPath");
238 static void
239 createPanel(Panel *p)
241 _Panel *panel = (_Panel*)p;
242 WMScreen *scr = WMWidgetScreen(panel->win);
244 panel->white = WMWhiteColor(scr);
245 panel->black = WMBlackColor(scr);
246 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
247 panel->font = WMSystemFontOfSize(scr, 12);
249 panel->frame = WMCreateFrame(panel->win);
250 WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
251 WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
253 /* icon path */
254 panel->icoF = WMCreateFrame(panel->frame);
255 WMResizeWidget(panel->icoF, 230, 210);
256 WMMoveWidget(panel->icoF, 25, 10);
257 WMSetFrameTitle(panel->icoF, _("Icon Search Paths"));
259 panel->icoL = WMCreateList(panel->icoF);
260 WMResizeWidget(panel->icoL, 200, 150);
261 WMMoveWidget(panel->icoL, 15, 20);
262 WMSetListUserDrawProc(panel->icoL, paintItem);
263 WMHangData(panel->icoL, panel);
265 panel->icoaB = WMCreateCommandButton(panel->icoF);
266 WMResizeWidget(panel->icoaB, 90, 24);
267 WMMoveWidget(panel->icoaB, 125, 176);
268 WMSetButtonText(panel->icoaB, _("Add"));
269 WMSetButtonAction(panel->icoaB, browseForFile, panel);
270 WMSetButtonImagePosition(panel->icoaB, WIPRight);
272 panel->icorB = WMCreateCommandButton(panel->icoF);
273 WMResizeWidget(panel->icorB, 90, 24);
274 WMMoveWidget(panel->icorB, 15, 176);
275 WMSetButtonText(panel->icorB, _("Remove"));
276 WMSetButtonAction(panel->icorB, pushButton, panel);
278 WMMapSubwidgets(panel->icoF);
280 /* pixmap path */
281 panel->pixF = WMCreateFrame(panel->frame);
282 WMResizeWidget(panel->pixF, 230, 210);
283 WMMoveWidget(panel->pixF, 270, 10);
284 WMSetFrameTitle(panel->pixF, _("Pixmap Search Paths"));
286 panel->pixL = WMCreateList(panel->pixF);
287 WMResizeWidget(panel->pixL, 200, 150);
288 WMMoveWidget(panel->pixL, 15, 20);
289 WMSetListUserDrawProc(panel->pixL, paintItem);
290 WMHangData(panel->pixL, panel);
292 panel->pixaB = WMCreateCommandButton(panel->pixF);
293 WMResizeWidget(panel->pixaB, 90, 24);
294 WMMoveWidget(panel->pixaB, 125, 176);
295 WMSetButtonText(panel->pixaB, _("Add"));
296 WMSetButtonAction(panel->pixaB, browseForFile, panel);
297 WMSetButtonImagePosition(panel->pixaB, WIPRight);
299 panel->pixrB = WMCreateCommandButton(panel->pixF);
300 WMResizeWidget(panel->pixrB, 90, 24);
301 WMMoveWidget(panel->pixrB, 15, 176);
302 WMSetButtonText(panel->pixrB, _("Remove"));
303 WMSetButtonAction(panel->pixrB, pushButton, panel);
306 WMMapSubwidgets(panel->pixF);
308 WMRealizeWidget(panel->frame);
309 WMMapSubwidgets(panel->frame);
311 showData(panel);
316 Panel*
317 InitPaths(WMScreen *scr, WMWindow *win)
319 _Panel *panel;
321 panel = wmalloc(sizeof(_Panel));
322 memset(panel, 0, sizeof(_Panel));
324 panel->sectionName = _("Search Path Configuration");
326 panel->description = _("Search paths to use when looking for pixmaps\n"
327 "and icons.");
329 panel->win = win;
331 panel->callbacks.createWidgets = createPanel;
332 panel->callbacks.updateDomain = storeData;
334 AddSection(panel, ICON_FILE);
336 return panel;