Initial revision
[wmaker-crm.git] / WPrefs.app / Paths.c
blobb49c0f7b1615f3a3aba34d22ad1e521922ec528b
1 /* Paths.c- pixmap/icon paths
2 *
3 * WPrefs - WindowMaker 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 CallbackRec callbacks;
33 WMWindow *win;
35 WMFrame *pixF;
36 WMList *pixL;
37 WMButton *pixaB;
38 WMButton *pixrB;
39 WMTextField *pixT;
41 WMFrame *icoF;
42 WMList *icoL;
43 WMButton *icoaB;
44 WMButton *icorB;
45 WMTextField *icoT;
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->icoaB) {
120 char *text = WMGetTextFieldText(panel->icoT);
122 if (text && strlen(text) > 0) {
123 i = WMGetListSelectedItemRow(panel->icoL);
124 if (i >= 0) i++;
125 addPathToList(panel->icoL, i, text);
126 WMSetListBottomPosition(panel->icoL,
127 WMGetListNumberOfRows(panel->icoL));
129 if (text)
130 free(text);
132 WMSetTextFieldText(panel->icoT, NULL);
133 } else if (w == panel->icorB) {
134 i = WMGetListSelectedItemRow(panel->icoL);
136 if (i>=0)
137 WMRemoveListItem(panel->icoL, i);
140 /* pixmap paths */
141 if (w == panel->pixaB) {
142 char *text = WMGetTextFieldText(panel->pixT);
144 if (text && strlen(text) > 0) {
145 i = WMGetListSelectedItemRow(panel->pixL);
146 if (i >= 0) i++;
147 addPathToList(panel->pixL, i, text);
148 WMSetListBottomPosition(panel->pixL,
149 WMGetListNumberOfRows(panel->pixL));
151 if (text)
152 free(text);
153 WMSetTextFieldText(panel->pixT, NULL);
154 } else if (w == panel->pixrB) {
155 i = WMGetListSelectedItemRow(panel->pixL);
157 if (i>=0)
158 WMRemoveListItem(panel->pixL, i);
163 static void
164 textEditedObserver(void *observerData, WMNotification *notification)
166 _Panel *panel = (_Panel*)observerData;
168 switch ((int)WMGetNotificationClientData(notification)) {
169 case WMReturnTextMovement:
170 if (WMGetNotificationObject(notification) == panel->icoT)
171 WMPerformButtonClick(panel->icoaB);
172 else
173 WMPerformButtonClick(panel->pixaB);
174 break;
176 case WMIllegalTextMovement:
177 if (WMGetNotificationObject(notification) == panel->icoT) {
178 WMSetButtonImage(panel->icoaB, NULL);
179 WMSetButtonImage(panel->icoaB, NULL);
180 } else {
181 WMSetButtonImage(panel->pixaB, NULL);
182 WMSetButtonImage(panel->pixaB, NULL);
184 break;
189 static void
190 textBeginObserver(void *observerData, WMNotification *notification)
192 _Panel *panel = (_Panel*)observerData;
193 WMScreen *scr = WMWidgetScreen(panel->win);
194 WMPixmap *arrow1 = WMGetSystemPixmap(scr, WSIReturnArrow);
195 WMPixmap *arrow2 = WMGetSystemPixmap(scr, WSIHighlightedReturnArrow);
197 if (WMGetNotificationObject(notification)==panel->icoT) {
198 WMSetButtonImage(panel->icoaB, arrow1);
199 WMSetButtonAltImage(panel->icoaB, arrow2);
200 } else {
201 WMSetButtonImage(panel->pixaB, arrow1);
202 WMSetButtonAltImage(panel->pixaB, arrow2);
208 static void
209 listClick(WMWidget *w, void *data)
211 _Panel *panel = (_Panel*)data;
212 char *t;
214 if (w == panel->icoL) {
215 t = WMGetListSelectedItem(panel->icoL)->text;
216 WMSetTextFieldText(panel->icoT, t);
217 } else {
218 t = WMGetListSelectedItem(panel->pixL)->text;
219 WMSetTextFieldText(panel->pixT, t);
225 static void
226 paintItem(WMList *lPtr, Drawable d, char *text, int state, WMRect *rect)
228 int width, height, x, y;
229 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
230 WMScreen *scr = WMWidgetScreen(lPtr);
231 Display *dpy = WMScreenDisplay(scr);
233 width = rect->size.width;
234 height = rect->size.height;
235 x = rect->pos.x;
236 y = rect->pos.y;
238 if (state & WLDSSelected)
239 XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width,
240 height);
241 else
242 XClearArea(dpy, d, x, y, width, height, False);
244 if (state & 1) {
245 WMDrawString(scr, d, WMColorGC(panel->red), panel->font, x+4, y,
246 text, strlen(text));
247 } else {
248 WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+4, y,
249 text, strlen(text));
255 static void
256 storeData(_Panel *panel)
258 proplist_t list;
259 proplist_t tmp;
260 int i;
261 char *p;
263 list = PLMakeArrayFromElements(NULL, NULL);
264 for (i=0; i<WMGetListNumberOfRows(panel->icoL); i++) {
265 p = WMGetListItem(panel->icoL, i)->text;
266 tmp = PLMakeString(p);
267 PLAppendArrayElement(list, tmp);
269 SetObjectForKey(list, "IconPath");
271 list = PLMakeArrayFromElements(NULL, NULL);
272 for (i=0; i<WMGetListNumberOfRows(panel->pixL); i++) {
273 p = WMGetListItem(panel->pixL, i)->text;
274 tmp = PLMakeString(p);
275 PLAppendArrayElement(list, tmp);
277 SetObjectForKey(list, "PixmapPath");
282 static void
283 createPanel(Panel *p)
285 _Panel *panel = (_Panel*)p;
286 WMScreen *scr = WMWidgetScreen(panel->win);
288 panel->white = WMWhiteColor(scr);
289 panel->black = WMBlackColor(scr);
290 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
291 panel->font = WMSystemFontOfSize(scr, 12);
293 panel->frame = WMCreateFrame(panel->win);
294 WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
295 WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
297 /* icon path */
298 panel->icoF = WMCreateFrame(panel->frame);
299 WMResizeWidget(panel->icoF, 230, 210);
300 WMMoveWidget(panel->icoF, 25, 10);
301 WMSetFrameTitle(panel->icoF, _("Icon Search Paths"));
303 panel->icoL = WMCreateList(panel->icoF);
304 WMResizeWidget(panel->icoL, 200, 120);
305 WMMoveWidget(panel->icoL, 15, 20);
306 WMSetListAction(panel->icoL, listClick, panel);
307 WMSetListUserDrawProc(panel->icoL, paintItem);
308 WMHangData(panel->icoL, panel);
310 panel->icoaB = WMCreateCommandButton(panel->icoF);
311 WMResizeWidget(panel->icoaB, 90, 24);
312 WMMoveWidget(panel->icoaB, 125, 145);
313 WMSetButtonText(panel->icoaB, _("Add"));
314 WMSetButtonAction(panel->icoaB, pushButton, panel);
315 WMSetButtonImagePosition(panel->icoaB, WIPRight);
317 panel->icorB = WMCreateCommandButton(panel->icoF);
318 WMResizeWidget(panel->icorB, 90, 24);
319 WMMoveWidget(panel->icorB, 15, 145);
320 WMSetButtonText(panel->icorB, _("Remove"));
321 WMSetButtonAction(panel->icorB, pushButton, panel);
323 panel->icoT = WMCreateTextField(panel->icoF);
324 WMResizeWidget(panel->icoT, 200, 20);
325 WMMoveWidget(panel->icoT, 15, 175);
326 WMAddNotificationObserver(textEditedObserver, panel,
327 WMTextDidEndEditingNotification, panel->icoT);
328 WMAddNotificationObserver(textBeginObserver, panel,
329 WMTextDidBeginEditingNotification, panel->icoT);
331 WMMapSubwidgets(panel->icoF);
333 /* pixmap path */
334 panel->pixF = WMCreateFrame(panel->frame);
335 WMResizeWidget(panel->pixF, 230, 210);
336 WMMoveWidget(panel->pixF, 270, 10);
337 WMSetFrameTitle(panel->pixF, _("Pixmap Search Paths"));
339 panel->pixL = WMCreateList(panel->pixF);
340 WMResizeWidget(panel->pixL, 200, 120);
341 WMMoveWidget(panel->pixL, 15, 20);
342 WMSetListAction(panel->pixL, listClick, panel);
343 WMSetListUserDrawProc(panel->pixL, paintItem);
344 WMHangData(panel->pixL, panel);
346 panel->pixaB = WMCreateCommandButton(panel->pixF);
347 WMResizeWidget(panel->pixaB, 90, 24);
348 WMMoveWidget(panel->pixaB, 125, 145);
349 WMSetButtonText(panel->pixaB, _("Add"));
350 WMSetButtonAction(panel->pixaB, pushButton, panel);
351 WMSetButtonImagePosition(panel->pixaB, WIPRight);
353 panel->pixrB = WMCreateCommandButton(panel->pixF);
354 WMResizeWidget(panel->pixrB, 90, 24);
355 WMMoveWidget(panel->pixrB, 15, 145);
356 WMSetButtonText(panel->pixrB, _("Remove"));
357 WMSetButtonAction(panel->pixrB, pushButton, panel);
359 panel->pixT= WMCreateTextField(panel->pixF);
360 WMResizeWidget(panel->pixT, 200, 20);
361 WMMoveWidget(panel->pixT, 15, 175);
362 WMAddNotificationObserver(textEditedObserver, panel,
363 WMTextDidEndEditingNotification, panel->pixT);
364 WMAddNotificationObserver(textBeginObserver, panel,
365 WMTextDidBeginEditingNotification, panel->pixT);
367 WMMapSubwidgets(panel->pixF);
369 WMRealizeWidget(panel->frame);
370 WMMapSubwidgets(panel->frame);
372 showData(panel);
377 Panel*
378 InitPaths(WMScreen *scr, WMWindow *win)
380 _Panel *panel;
382 panel = wmalloc(sizeof(_Panel));
383 memset(panel, 0, sizeof(_Panel));
385 panel->sectionName = _("Search Path Configuration");
387 panel->win = win;
389 panel->callbacks.createWidgets = createPanel;
390 panel->callbacks.updateDomain = storeData;
392 AddSection(panel, ICON_FILE);
394 return panel;