appearanceObserver doesn't create icon again
[wmaker-crm.git] / WPrefs.app / Paths.c
blobed515e5d2e7c2036afc0c60d1a6a29562ba04feb
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "WPrefs.h"
23 #include <unistd.h>
24 #include <assert.h>
26 typedef struct _Panel {
27 WMBox *box;
28 char *sectionName;
30 char *description;
32 CallbackRec callbacks;
34 WMWidget *parent;
36 WMTabView *tabv;
38 WMFrame *pixF;
39 WMList *pixL;
40 WMButton *pixaB;
41 WMButton *pixrB;
43 WMFrame *icoF;
44 WMList *icoL;
45 WMButton *icoaB;
46 WMButton *icorB;
48 WMColor *red;
49 WMColor *black;
50 WMColor *white;
51 WMColor *gray;
52 WMFont *font;
53 } _Panel;
55 #define ICON_FILE "paths"
57 static void addPathToList(WMList * list, int index, char *path)
59 char *fpath = wexpandpath(path);
60 WMListItem *item;
62 item = WMInsertListItem(list, index, path);
64 if (access(fpath, X_OK) != 0) {
65 item->uflags = 1;
67 wfree(fpath);
70 static void showData(_Panel * panel)
72 WMPropList *array, *val;
73 int i;
75 array = GetObjectForKey("IconPath");
76 if (!array || !WMIsPLArray(array)) {
77 if (array)
78 wwarning(_("bad value in option IconPath. Using default path list"));
79 addPathToList(panel->icoL, -1, "~/pixmaps");
80 addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
81 addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
82 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
83 addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
84 addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
85 } else {
86 for (i = 0; i < WMGetPropListItemCount(array); i++) {
87 val = WMGetFromPLArray(array, i);
88 addPathToList(panel->icoL, -1, WMGetFromPLString(val));
92 array = GetObjectForKey("PixmapPath");
93 if (!array || !WMIsPLArray(array)) {
94 if (array)
95 wwarning(_("bad value in option PixmapPath. Using default path list"));
96 addPathToList(panel->pixL, -1, "~/pixmaps");
97 addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
98 addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
99 } else {
100 for (i = 0; i < WMGetPropListItemCount(array); i++) {
101 val = WMGetFromPLArray(array, i);
102 addPathToList(panel->pixL, -1, WMGetFromPLString(val));
107 static void pushButton(WMWidget * w, void *data)
109 _Panel *panel = (_Panel *) data;
110 int i;
112 /* icon paths */
113 if (w == panel->icorB) {
114 i = WMGetListSelectedItemRow(panel->icoL);
116 if (i >= 0)
117 WMRemoveListItem(panel->icoL, i);
120 /* pixmap paths */
121 if (w == panel->pixrB) {
122 i = WMGetListSelectedItemRow(panel->pixL);
124 if (i >= 0)
125 WMRemoveListItem(panel->pixL, i);
129 static void browseForFile(WMWidget * w, void *data)
131 _Panel *panel = (_Panel *) data;
132 WMFilePanel *filePanel;
134 assert(w == panel->icoaB || w == panel->pixaB);
136 filePanel = WMGetOpenPanel(WMWidgetScreen(w));
138 WMSetFilePanelCanChooseFiles(filePanel, False);
140 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/", _("Select directory"), NULL) == True) {
141 char *str = WMGetFilePanelFileName(filePanel);
143 if (str) {
144 int len = strlen(str);
146 /* Remove the trailing '/' except if the path is exactly / */
147 if (len > 1 && str[len - 1] == '/') {
148 str[len - 1] = '\0';
149 len--;
151 if (len > 0) {
152 WMList *lPtr = NULL;
153 int i;
155 if (w == panel->icoaB)
156 lPtr = panel->icoL;
157 else if (w == panel->pixaB)
158 lPtr = panel->pixL;
160 i = WMGetListSelectedItemRow(lPtr);
161 if (i >= 0)
162 i++;
163 addPathToList(lPtr, i, str);
164 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
166 wfree(str);
172 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
174 int width, height, x, y;
175 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
176 WMScreen *scr = WMWidgetScreen(lPtr);
177 Display *dpy = WMScreenDisplay(scr);
178 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
180 width = rect->size.width;
181 height = rect->size.height;
182 x = rect->pos.x;
183 y = rect->pos.y;
185 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
187 if (state & 1) {
188 WMDrawString(scr, d, panel->red, panel->font, x + 4, y, text, strlen(text));
189 } else {
190 WMDrawString(scr, d, panel->black, panel->font, x + 4, y, text, strlen(text));
194 static void storeData(_Panel * panel)
196 WMPropList *list;
197 WMPropList *tmp;
198 int i;
199 char *p;
201 list = WMCreatePLArray(NULL, NULL);
202 for (i = 0; i < WMGetListNumberOfRows(panel->icoL); i++) {
203 p = WMGetListItem(panel->icoL, i)->text;
204 tmp = WMCreatePLString(p);
205 WMAddToPLArray(list, tmp);
207 SetObjectForKey(list, "IconPath");
209 list = WMCreatePLArray(NULL, NULL);
210 for (i = 0; i < WMGetListNumberOfRows(panel->pixL); i++) {
211 p = WMGetListItem(panel->pixL, i)->text;
212 tmp = WMCreatePLString(p);
213 WMAddToPLArray(list, tmp);
215 SetObjectForKey(list, "PixmapPath");
218 static void createPanel(Panel * p)
220 _Panel *panel = (_Panel *) p;
221 WMScreen *scr = WMWidgetScreen(panel->parent);
222 WMTabViewItem *tab;
224 panel->white = WMWhiteColor(scr);
225 panel->black = WMBlackColor(scr);
226 panel->gray = WMGrayColor(scr);
227 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
228 panel->font = WMSystemFontOfSize(scr, 12);
230 panel->box = WMCreateBox(panel->parent);
231 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
233 panel->tabv = WMCreateTabView(panel->box);
234 WMMoveWidget(panel->tabv, 12, 10);
235 WMResizeWidget(panel->tabv, 500, 215);
237 /* icon path */
238 panel->icoF = WMCreateFrame(panel->box);
239 WMSetFrameRelief(panel->icoF, WRFlat);
240 WMResizeWidget(panel->icoF, 230, 210);
242 tab = WMCreateTabViewItemWithIdentifier(0);
243 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
244 WMAddItemInTabView(panel->tabv, tab);
245 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
247 panel->icoL = WMCreateList(panel->icoF);
248 WMResizeWidget(panel->icoL, 480, 147);
249 WMMoveWidget(panel->icoL, 10, 10);
250 WMSetListUserDrawProc(panel->icoL, paintItem);
251 WMHangData(panel->icoL, panel);
253 panel->icoaB = WMCreateCommandButton(panel->icoF);
254 WMResizeWidget(panel->icoaB, 95, 24);
255 WMMoveWidget(panel->icoaB, 293, 165);
256 WMSetButtonText(panel->icoaB, _("Add"));
257 WMSetButtonAction(panel->icoaB, browseForFile, panel);
258 WMSetButtonImagePosition(panel->icoaB, WIPRight);
260 panel->icorB = WMCreateCommandButton(panel->icoF);
261 WMResizeWidget(panel->icorB, 95, 24);
262 WMMoveWidget(panel->icorB, 395, 165);
263 WMSetButtonText(panel->icorB, _("Remove"));
264 WMSetButtonAction(panel->icorB, pushButton, panel);
266 WMMapSubwidgets(panel->icoF);
268 /* pixmap path */
269 panel->pixF = WMCreateFrame(panel->box);
270 WMSetFrameRelief(panel->pixF, WRFlat);
271 WMResizeWidget(panel->pixF, 230, 210);
273 tab = WMCreateTabViewItemWithIdentifier(0);
274 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
275 WMAddItemInTabView(panel->tabv, tab);
276 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
278 panel->pixL = WMCreateList(panel->pixF);
279 WMResizeWidget(panel->pixL, 480, 147);
280 WMMoveWidget(panel->pixL, 10, 10);
281 WMSetListUserDrawProc(panel->pixL, paintItem);
282 WMHangData(panel->pixL, panel);
284 panel->pixaB = WMCreateCommandButton(panel->pixF);
285 WMResizeWidget(panel->pixaB, 95, 24);
286 WMMoveWidget(panel->pixaB, 293, 165);
287 WMSetButtonText(panel->pixaB, _("Add"));
288 WMSetButtonAction(panel->pixaB, browseForFile, panel);
289 WMSetButtonImagePosition(panel->pixaB, WIPRight);
291 panel->pixrB = WMCreateCommandButton(panel->pixF);
292 WMResizeWidget(panel->pixrB, 95, 24);
293 WMMoveWidget(panel->pixrB, 395, 165);
294 WMSetButtonText(panel->pixrB, _("Remove"));
295 WMSetButtonAction(panel->pixrB, pushButton, panel);
297 WMMapSubwidgets(panel->pixF);
299 WMRealizeWidget(panel->box);
300 WMMapSubwidgets(panel->box);
302 showData(panel);
305 Panel *InitPaths(WMScreen * scr, WMWidget * parent)
307 _Panel *panel;
309 panel = wmalloc(sizeof(_Panel));
311 panel->sectionName = _("Search Path Configuration");
313 panel->description = _("Search paths to use when looking for pixmaps\n" "and icons.");
315 panel->parent = parent;
317 panel->callbacks.createWidgets = createPanel;
318 panel->callbacks.updateDomain = storeData;
320 AddSection(panel, ICON_FILE);
322 return panel;