Some menu entry beautification
[wmaker-crm.git] / WPrefs.app / Paths.c
blob0d9f14519eabfab2653cd6ff7b1abb7ab069124c
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 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 free(fpath);
75 static void
76 showData(_Panel *panel)
78 proplist_t array, val;
79 int i;
81 array = GetObjectForKey("IconPath");
82 if (!array || !PLIsArray(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<PLGetNumberOfElements(array); i++) {
93 val = PLGetArrayElement(array, i);
94 addPathToList(panel->icoL, -1, PLGetString(val));
98 array = GetObjectForKey("PixmapPath");
99 if (!array || !PLIsArray(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<PLGetNumberOfElements(array); i++) {
107 val = PLGetArrayElement(array, i);
108 addPathToList(panel->pixL, -1, PLGetString(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->win, "/",
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 free(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, WMColorGC(panel->red), panel->font, x+4, y,
204 text, strlen(text));
205 } else {
206 WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+4, y,
207 text, strlen(text));
213 static void
214 storeData(_Panel *panel)
216 proplist_t list;
217 proplist_t tmp;
218 int i;
219 char *p;
221 list = PLMakeArrayFromElements(NULL, NULL);
222 for (i=0; i<WMGetListNumberOfRows(panel->icoL); i++) {
223 p = WMGetListItem(panel->icoL, i)->text;
224 tmp = PLMakeString(p);
225 PLAppendArrayElement(list, tmp);
227 SetObjectForKey(list, "IconPath");
229 list = PLMakeArrayFromElements(NULL, NULL);
230 for (i=0; i<WMGetListNumberOfRows(panel->pixL); i++) {
231 p = WMGetListItem(panel->pixL, i)->text;
232 tmp = PLMakeString(p);
233 PLAppendArrayElement(list, tmp);
235 SetObjectForKey(list, "PixmapPath");
240 static void
241 createPanel(Panel *p)
243 _Panel *panel = (_Panel*)p;
244 WMScreen *scr = WMWidgetScreen(panel->win);
245 WMTabViewItem *tab;
247 panel->white = WMWhiteColor(scr);
248 panel->black = WMBlackColor(scr);
249 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
250 panel->font = WMSystemFontOfSize(scr, 12);
252 panel->frame = WMCreateFrame(panel->win);
253 WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
254 WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
256 panel->tabv = WMCreateTabView(panel->frame);
257 WMMoveWidget(panel->tabv, 12, 10);
258 WMResizeWidget(panel->tabv, 500, 215);
262 /* icon path */
263 panel->icoF = WMCreateFrame(panel->frame);
264 WMSetFrameRelief(panel->icoF, WRFlat);
265 WMResizeWidget(panel->icoF, 230, 210);
267 tab = WMCreateTabViewItemWithIdentifier(0);
268 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
269 WMAddItemInTabView(panel->tabv, tab);
270 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
272 panel->icoL = WMCreateList(panel->icoF);
273 WMResizeWidget(panel->icoL, 480, 147);
274 WMMoveWidget(panel->icoL, 10, 10);
275 WMSetListUserDrawProc(panel->icoL, paintItem);
276 WMHangData(panel->icoL, panel);
278 panel->icoaB = WMCreateCommandButton(panel->icoF);
279 WMResizeWidget(panel->icoaB, 95, 24);
280 WMMoveWidget(panel->icoaB, 293, 165);
281 WMSetButtonText(panel->icoaB, _("Add"));
282 WMSetButtonAction(panel->icoaB, browseForFile, panel);
283 WMSetButtonImagePosition(panel->icoaB, WIPRight);
285 panel->icorB = WMCreateCommandButton(panel->icoF);
286 WMResizeWidget(panel->icorB, 95, 24);
287 WMMoveWidget(panel->icorB, 395, 165);
288 WMSetButtonText(panel->icorB, _("Remove"));
289 WMSetButtonAction(panel->icorB, pushButton, panel);
291 WMMapSubwidgets(panel->icoF);
293 /* pixmap path */
294 panel->pixF = WMCreateFrame(panel->frame);
295 WMSetFrameRelief(panel->pixF, WRFlat);
296 WMResizeWidget(panel->pixF, 230, 210);
298 tab = WMCreateTabViewItemWithIdentifier(0);
299 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
300 WMAddItemInTabView(panel->tabv, tab);
301 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
303 panel->pixL = WMCreateList(panel->pixF);
304 WMResizeWidget(panel->pixL, 480, 147);
305 WMMoveWidget(panel->pixL, 10, 10);
306 WMSetListUserDrawProc(panel->pixL, paintItem);
307 WMHangData(panel->pixL, panel);
309 panel->pixaB = WMCreateCommandButton(panel->pixF);
310 WMResizeWidget(panel->pixaB, 95, 24);
311 WMMoveWidget(panel->pixaB, 293, 165);
312 WMSetButtonText(panel->pixaB, _("Add"));
313 WMSetButtonAction(panel->pixaB, browseForFile, panel);
314 WMSetButtonImagePosition(panel->pixaB, WIPRight);
316 panel->pixrB = WMCreateCommandButton(panel->pixF);
317 WMResizeWidget(panel->pixrB, 95, 24);
318 WMMoveWidget(panel->pixrB, 395, 165);
319 WMSetButtonText(panel->pixrB, _("Remove"));
320 WMSetButtonAction(panel->pixrB, pushButton, panel);
323 WMMapSubwidgets(panel->pixF);
325 WMRealizeWidget(panel->frame);
326 WMMapSubwidgets(panel->frame);
328 showData(panel);
333 Panel*
334 InitPaths(WMScreen *scr, WMWindow *win)
336 _Panel *panel;
338 panel = wmalloc(sizeof(_Panel));
339 memset(panel, 0, sizeof(_Panel));
341 panel->sectionName = _("Search Path Configuration");
343 panel->description = _("Search paths to use when looking for pixmaps\n"
344 "and icons.");
346 panel->win = win;
348 panel->callbacks.createWidgets = createPanel;
349 panel->callbacks.updateDomain = storeData;
351 AddSection(panel, ICON_FILE);
353 return panel;