- API change in WINGs for WMDraw*String().
[wmaker-crm.git] / WPrefs.app / Paths.c
blob68dd8fe869c08adca4989d50415324a886693004
1 /* Paths.c- pixmap/icon paths
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998-2002 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 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 WMFont *font;
53 } _Panel;
57 #define ICON_FILE "paths"
60 static void
61 addPathToList(WMList *list, int index, char *path)
63 char *fpath = wexpandpath(path);
64 WMListItem *item;
66 item = WMInsertListItem(list, index, path);
68 if (access(fpath, X_OK)!=0) {
69 item->uflags = 1;
71 wfree(fpath);
75 static void
76 showData(_Panel *panel)
78 WMPropList *array, *val;
79 int i;
81 array = GetObjectForKey("IconPath");
82 if (!array || !WMIsPLArray(array)) {
83 if (array)
84 wwarning(_("bad value in option IconPath. Using default path list"));
85 addPathToList(panel->icoL, -1, "~/pixmaps");
86 addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
87 addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
88 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
89 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
90 addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
91 } else {
92 for (i=0; i<WMGetPropListItemCount(array); i++) {
93 val = WMGetFromPLArray(array, i);
94 addPathToList(panel->icoL, -1, WMGetFromPLString(val));
98 array = GetObjectForKey("PixmapPath");
99 if (!array || !WMIsPLArray(array)) {
100 if (array)
101 wwarning(_("bad value in option PixmapPath. Using default path list"));
102 addPathToList(panel->pixL, -1, "~/pixmaps");
103 addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
104 addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
105 } else {
106 for (i=0; i<WMGetPropListItemCount(array); i++) {
107 val = WMGetFromPLArray(array, i);
108 addPathToList(panel->pixL, -1, WMGetFromPLString(val));
114 static void
115 pushButton(WMWidget *w, void *data)
117 _Panel *panel = (_Panel*)data;
118 int i;
120 /* icon paths */
121 if (w == panel->icorB) {
122 i = WMGetListSelectedItemRow(panel->icoL);
124 if (i>=0)
125 WMRemoveListItem(panel->icoL, i);
128 /* pixmap paths */
129 if (w == panel->pixrB) {
130 i = WMGetListSelectedItemRow(panel->pixL);
132 if (i>=0)
133 WMRemoveListItem(panel->pixL, i);
138 static void
139 browseForFile(WMWidget *w, void *data)
141 _Panel *panel = (_Panel*)data;
142 WMFilePanel *filePanel;
144 filePanel = WMGetOpenPanel(WMWidgetScreen(w));
146 WMSetFilePanelCanChooseFiles(filePanel, False);
148 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/",
149 _("Select directory"), NULL) == True) {
150 char *str = WMGetFilePanelFileName(filePanel);
152 if (str) {
153 int len = strlen(str);
155 /* Remove the trailing '/' except if the path is exactly / */
156 if (len > 1 && str[len-1] == '/') {
157 str[len-1] = '\0';
158 len--;
160 if (len > 0) {
161 WMList *lPtr;
162 int i;
164 if (w == panel->icoaB)
165 lPtr = panel->icoL;
166 else if (w == panel->pixaB)
167 lPtr = panel->pixL;
169 i = WMGetListSelectedItemRow(lPtr);
170 if (i >= 0) i++;
171 addPathToList(lPtr, i, str);
172 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
174 wfree(str);
182 static void
183 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
184 WMRect *rect)
186 int width, height, x, y;
187 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
188 WMScreen *scr = WMWidgetScreen(lPtr);
189 Display *dpy = WMScreenDisplay(scr);
191 width = rect->size.width;
192 height = rect->size.height;
193 x = rect->pos.x;
194 y = rect->pos.y;
196 if (state & WLDSSelected)
197 XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width,
198 height);
199 else
200 XClearArea(dpy, d, x, y, width, height, False);
202 if (state & 1) {
203 WMDrawString(scr, d, panel->red, panel->font, x+4, y, text, strlen(text));
204 } else {
205 WMDrawString(scr, d, panel->black, panel->font, x+4, y, text, strlen(text));
211 static void
212 storeData(_Panel *panel)
214 WMPropList *list;
215 WMPropList *tmp;
216 int i;
217 char *p;
219 list = WMCreatePLArray(NULL, NULL);
220 for (i=0; i<WMGetListNumberOfRows(panel->icoL); i++) {
221 p = WMGetListItem(panel->icoL, i)->text;
222 tmp = WMCreatePLString(p);
223 WMAddToPLArray(list, tmp);
225 SetObjectForKey(list, "IconPath");
227 list = WMCreatePLArray(NULL, NULL);
228 for (i=0; i<WMGetListNumberOfRows(panel->pixL); i++) {
229 p = WMGetListItem(panel->pixL, i)->text;
230 tmp = WMCreatePLString(p);
231 WMAddToPLArray(list, tmp);
233 SetObjectForKey(list, "PixmapPath");
238 static void
239 createPanel(Panel *p)
241 _Panel *panel = (_Panel*)p;
242 WMScreen *scr = WMWidgetScreen(panel->parent);
243 WMTabViewItem *tab;
245 panel->white = WMWhiteColor(scr);
246 panel->black = WMBlackColor(scr);
247 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
248 panel->font = WMSystemFontOfSize(scr, 12);
250 panel->box = WMCreateBox(panel->parent);
251 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
254 panel->tabv = WMCreateTabView(panel->box);
255 WMMoveWidget(panel->tabv, 12, 10);
256 WMResizeWidget(panel->tabv, 500, 215);
260 /* icon path */
261 panel->icoF = WMCreateFrame(panel->box);
262 WMSetFrameRelief(panel->icoF, WRFlat);
263 WMResizeWidget(panel->icoF, 230, 210);
265 tab = WMCreateTabViewItemWithIdentifier(0);
266 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
267 WMAddItemInTabView(panel->tabv, tab);
268 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
270 panel->icoL = WMCreateList(panel->icoF);
271 WMResizeWidget(panel->icoL, 480, 147);
272 WMMoveWidget(panel->icoL, 10, 10);
273 WMSetListUserDrawProc(panel->icoL, paintItem);
274 WMHangData(panel->icoL, panel);
276 panel->icoaB = WMCreateCommandButton(panel->icoF);
277 WMResizeWidget(panel->icoaB, 95, 24);
278 WMMoveWidget(panel->icoaB, 293, 165);
279 WMSetButtonText(panel->icoaB, _("Add"));
280 WMSetButtonAction(panel->icoaB, browseForFile, panel);
281 WMSetButtonImagePosition(panel->icoaB, WIPRight);
283 panel->icorB = WMCreateCommandButton(panel->icoF);
284 WMResizeWidget(panel->icorB, 95, 24);
285 WMMoveWidget(panel->icorB, 395, 165);
286 WMSetButtonText(panel->icorB, _("Remove"));
287 WMSetButtonAction(panel->icorB, pushButton, panel);
289 WMMapSubwidgets(panel->icoF);
291 /* pixmap path */
292 panel->pixF = WMCreateFrame(panel->box);
293 WMSetFrameRelief(panel->pixF, WRFlat);
294 WMResizeWidget(panel->pixF, 230, 210);
296 tab = WMCreateTabViewItemWithIdentifier(0);
297 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
298 WMAddItemInTabView(panel->tabv, tab);
299 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
301 panel->pixL = WMCreateList(panel->pixF);
302 WMResizeWidget(panel->pixL, 480, 147);
303 WMMoveWidget(panel->pixL, 10, 10);
304 WMSetListUserDrawProc(panel->pixL, paintItem);
305 WMHangData(panel->pixL, panel);
307 panel->pixaB = WMCreateCommandButton(panel->pixF);
308 WMResizeWidget(panel->pixaB, 95, 24);
309 WMMoveWidget(panel->pixaB, 293, 165);
310 WMSetButtonText(panel->pixaB, _("Add"));
311 WMSetButtonAction(panel->pixaB, browseForFile, panel);
312 WMSetButtonImagePosition(panel->pixaB, WIPRight);
314 panel->pixrB = WMCreateCommandButton(panel->pixF);
315 WMResizeWidget(panel->pixrB, 95, 24);
316 WMMoveWidget(panel->pixrB, 395, 165);
317 WMSetButtonText(panel->pixrB, _("Remove"));
318 WMSetButtonAction(panel->pixrB, pushButton, panel);
321 WMMapSubwidgets(panel->pixF);
323 WMRealizeWidget(panel->box);
324 WMMapSubwidgets(panel->box);
326 showData(panel);
331 Panel*
332 InitPaths(WMScreen *scr, WMWidget *parent)
334 _Panel *panel;
336 panel = wmalloc(sizeof(_Panel));
337 memset(panel, 0, sizeof(_Panel));
339 panel->sectionName = _("Search Path Configuration");
341 panel->description = _("Search paths to use when looking for pixmaps\n"
342 "and icons.");
344 panel->parent = parent;
346 panel->callbacks.createWidgets = createPanel;
347 panel->callbacks.updateDomain = storeData;
349 AddSection(panel, ICON_FILE);
351 return panel;