Address some sparse warnings
[wmaker-crm.git] / WPrefs.app / Paths.c
blobdcffd5376b2e201fa7161b07f7ef95fa554125d4
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.
23 #include "WPrefs.h"
24 #include <unistd.h>
25 #include <assert.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;
56 #define ICON_FILE "paths"
58 static void addPathToList(WMList * list, int index, char *path)
60 char *fpath = wexpandpath(path);
61 WMListItem *item;
63 item = WMInsertListItem(list, index, path);
65 if (access(fpath, X_OK) != 0) {
66 item->uflags = 1;
68 wfree(fpath);
71 static void showData(_Panel * panel)
73 WMPropList *array, *val;
74 int i;
76 array = GetObjectForKey("IconPath");
77 if (!array || !WMIsPLArray(array)) {
78 if (array)
79 wwarning(_("bad value in option IconPath. Using default path list"));
80 addPathToList(panel->icoL, -1, "~/pixmaps");
81 addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
82 addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
83 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
84 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
85 addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
86 } else {
87 for (i = 0; i < WMGetPropListItemCount(array); i++) {
88 val = WMGetFromPLArray(array, i);
89 addPathToList(panel->icoL, -1, WMGetFromPLString(val));
93 array = GetObjectForKey("PixmapPath");
94 if (!array || !WMIsPLArray(array)) {
95 if (array)
96 wwarning(_("bad value in option PixmapPath. Using default path list"));
97 addPathToList(panel->pixL, -1, "~/pixmaps");
98 addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
99 addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
100 } else {
101 for (i = 0; i < WMGetPropListItemCount(array); i++) {
102 val = WMGetFromPLArray(array, i);
103 addPathToList(panel->pixL, -1, WMGetFromPLString(val));
108 static void pushButton(WMWidget * w, void *data)
110 _Panel *panel = (_Panel *) data;
111 int i;
113 /* icon paths */
114 if (w == panel->icorB) {
115 i = WMGetListSelectedItemRow(panel->icoL);
117 if (i >= 0)
118 WMRemoveListItem(panel->icoL, i);
121 /* pixmap paths */
122 if (w == panel->pixrB) {
123 i = WMGetListSelectedItemRow(panel->pixL);
125 if (i >= 0)
126 WMRemoveListItem(panel->pixL, i);
130 static void browseForFile(WMWidget * w, void *data)
132 _Panel *panel = (_Panel *) data;
133 WMFilePanel *filePanel;
135 assert(w == panel->icoaB || w == panel->pixaB);
137 filePanel = WMGetOpenPanel(WMWidgetScreen(w));
139 WMSetFilePanelCanChooseFiles(filePanel, False);
141 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/", _("Select directory"), NULL) == True) {
142 char *str = WMGetFilePanelFileName(filePanel);
144 if (str) {
145 int len = strlen(str);
147 /* Remove the trailing '/' except if the path is exactly / */
148 if (len > 1 && str[len - 1] == '/') {
149 str[len - 1] = '\0';
150 len--;
152 if (len > 0) {
153 WMList *lPtr = NULL;
154 int i;
156 if (w == panel->icoaB)
157 lPtr = panel->icoL;
158 else if (w == panel->pixaB)
159 lPtr = panel->pixL;
161 i = WMGetListSelectedItemRow(lPtr);
162 if (i >= 0)
163 i++;
164 addPathToList(lPtr, i, str);
165 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
167 wfree(str);
173 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
175 int width, height, x, y;
176 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
177 WMScreen *scr = WMWidgetScreen(lPtr);
178 Display *dpy = WMScreenDisplay(scr);
179 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
181 width = rect->size.width;
182 height = rect->size.height;
183 x = rect->pos.x;
184 y = rect->pos.y;
186 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
188 if (state & 1) {
189 WMDrawString(scr, d, panel->red, panel->font, x + 4, y, text, strlen(text));
190 } else {
191 WMDrawString(scr, d, panel->black, panel->font, x + 4, y, text, strlen(text));
195 static void storeData(_Panel * panel)
197 WMPropList *list;
198 WMPropList *tmp;
199 int i;
200 char *p;
202 list = WMCreatePLArray(NULL, NULL);
203 for (i = 0; i < WMGetListNumberOfRows(panel->icoL); i++) {
204 p = WMGetListItem(panel->icoL, i)->text;
205 tmp = WMCreatePLString(p);
206 WMAddToPLArray(list, tmp);
208 SetObjectForKey(list, "IconPath");
210 list = WMCreatePLArray(NULL, NULL);
211 for (i = 0; i < WMGetListNumberOfRows(panel->pixL); i++) {
212 p = WMGetListItem(panel->pixL, i)->text;
213 tmp = WMCreatePLString(p);
214 WMAddToPLArray(list, tmp);
216 SetObjectForKey(list, "PixmapPath");
219 static void createPanel(Panel * p)
221 _Panel *panel = (_Panel *) p;
222 WMScreen *scr = WMWidgetScreen(panel->parent);
223 WMTabViewItem *tab;
225 panel->white = WMWhiteColor(scr);
226 panel->black = WMBlackColor(scr);
227 panel->gray = WMGrayColor(scr);
228 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
229 panel->font = WMSystemFontOfSize(scr, 12);
231 panel->box = WMCreateBox(panel->parent);
232 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
234 panel->tabv = WMCreateTabView(panel->box);
235 WMMoveWidget(panel->tabv, 12, 10);
236 WMResizeWidget(panel->tabv, 500, 215);
238 /* icon path */
239 panel->icoF = WMCreateFrame(panel->box);
240 WMSetFrameRelief(panel->icoF, WRFlat);
241 WMResizeWidget(panel->icoF, 230, 210);
243 tab = WMCreateTabViewItemWithIdentifier(0);
244 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
245 WMAddItemInTabView(panel->tabv, tab);
246 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
248 panel->icoL = WMCreateList(panel->icoF);
249 WMResizeWidget(panel->icoL, 480, 147);
250 WMMoveWidget(panel->icoL, 10, 10);
251 WMSetListUserDrawProc(panel->icoL, paintItem);
252 WMHangData(panel->icoL, panel);
254 panel->icoaB = WMCreateCommandButton(panel->icoF);
255 WMResizeWidget(panel->icoaB, 95, 24);
256 WMMoveWidget(panel->icoaB, 293, 165);
257 WMSetButtonText(panel->icoaB, _("Add"));
258 WMSetButtonAction(panel->icoaB, browseForFile, panel);
259 WMSetButtonImagePosition(panel->icoaB, WIPRight);
261 panel->icorB = WMCreateCommandButton(panel->icoF);
262 WMResizeWidget(panel->icorB, 95, 24);
263 WMMoveWidget(panel->icorB, 395, 165);
264 WMSetButtonText(panel->icorB, _("Remove"));
265 WMSetButtonAction(panel->icorB, pushButton, panel);
267 WMMapSubwidgets(panel->icoF);
269 /* pixmap path */
270 panel->pixF = WMCreateFrame(panel->box);
271 WMSetFrameRelief(panel->pixF, WRFlat);
272 WMResizeWidget(panel->pixF, 230, 210);
274 tab = WMCreateTabViewItemWithIdentifier(0);
275 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
276 WMAddItemInTabView(panel->tabv, tab);
277 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
279 panel->pixL = WMCreateList(panel->pixF);
280 WMResizeWidget(panel->pixL, 480, 147);
281 WMMoveWidget(panel->pixL, 10, 10);
282 WMSetListUserDrawProc(panel->pixL, paintItem);
283 WMHangData(panel->pixL, panel);
285 panel->pixaB = WMCreateCommandButton(panel->pixF);
286 WMResizeWidget(panel->pixaB, 95, 24);
287 WMMoveWidget(panel->pixaB, 293, 165);
288 WMSetButtonText(panel->pixaB, _("Add"));
289 WMSetButtonAction(panel->pixaB, browseForFile, panel);
290 WMSetButtonImagePosition(panel->pixaB, WIPRight);
292 panel->pixrB = WMCreateCommandButton(panel->pixF);
293 WMResizeWidget(panel->pixrB, 95, 24);
294 WMMoveWidget(panel->pixrB, 395, 165);
295 WMSetButtonText(panel->pixrB, _("Remove"));
296 WMSetButtonAction(panel->pixrB, pushButton, panel);
298 WMMapSubwidgets(panel->pixF);
300 WMRealizeWidget(panel->box);
301 WMMapSubwidgets(panel->box);
303 showData(panel);
306 Panel *InitPaths(WMScreen * scr, WMWidget * parent)
308 _Panel *panel;
310 panel = wmalloc(sizeof(_Panel));
311 memset(panel, 0, sizeof(_Panel));
313 panel->sectionName = _("Search Path Configuration");
315 panel->description = _("Search paths to use when looking for pixmaps\n" "and icons.");
317 panel->parent = parent;
319 panel->callbacks.createWidgets = createPanel;
320 panel->callbacks.updateDomain = storeData;
322 AddSection(panel, ICON_FILE);
324 return panel;