1 /* Paths.c- pixmap/icon paths
2  *
3  *  WPrefs - Window Maker Preferences Program
4  *
5  *  Copyright (c) 1998-2003 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.
11  *
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.
16  *
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.
20  */
21 
22 #include "WPrefs.h"
23 #include <unistd.h>
24 #include <assert.h>
25 
26 typedef struct _Panel {
27 	WMBox *box;
28 	char *sectionName;
29 
30 	char *description;
31 
32 	CallbackRec callbacks;
33 
34 	WMWidget *parent;
35 
36 	WMTabView *tabv;
37 
38 	WMFrame *pixF;
39 	WMList *pixL;
40 	WMButton *pixaB;
41 	WMButton *pixrB;
42 
43 	WMFrame *icoF;
44 	WMList *icoL;
45 	WMButton *icoaB;
46 	WMButton *icorB;
47 
48 	WMColor *red;
49 	WMColor *black;
50 	WMColor *white;
51 	WMColor *gray;
52 	WMFont *font;
53 } _Panel;
54 
55 #define ICON_FILE	"paths"
56 
addPathToList(WMList * list,int index,const char * path)57 static void addPathToList(WMList * list, int index, const char *path)
58 {
59 	char *fpath = wexpandpath(path);
60 	WMListItem *item;
61 
62 	item = WMInsertListItem(list, index, path);
63 
64 	if (access(fpath, X_OK) != 0) {
65 		item->uflags = 1;
66 	}
67 	wfree(fpath);
68 }
69 
showData(_Panel * panel)70 static void showData(_Panel * panel)
71 {
72 	WMPropList *array, *val;
73 	int i;
74 
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, PREFIX "/share/pixmaps");
82 		addPathToList(panel->icoL, -1, PREFIX "/share/WindowMaker/Icons");
83 		addPathToList(panel->icoL, -1, PREFIX "/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));
89 		}
90 	}
91 
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, PREFIX "/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));
103 		}
104 	}
105 }
106 
pushButton(WMWidget * w,void * data)107 static void pushButton(WMWidget * w, void *data)
108 {
109 	_Panel *panel = (_Panel *) data;
110 	int i;
111 
112 	/* icon paths */
113 	if (w == panel->icorB) {
114 		i = WMGetListSelectedItemRow(panel->icoL);
115 
116 		if (i >= 0)
117 			WMRemoveListItem(panel->icoL, i);
118 	}
119 
120 	/* pixmap paths */
121 	if (w == panel->pixrB) {
122 		i = WMGetListSelectedItemRow(panel->pixL);
123 
124 		if (i >= 0)
125 			WMRemoveListItem(panel->pixL, i);
126 	}
127 }
128 
browseForFile(WMWidget * w,void * data)129 static void browseForFile(WMWidget * w, void *data)
130 {
131 	_Panel *panel = (_Panel *) data;
132 	WMFilePanel *filePanel;
133 
134 	assert(w == panel->icoaB || w == panel->pixaB);
135 
136 	filePanel = WMGetOpenPanel(WMWidgetScreen(w));
137 
138 	WMSetFilePanelCanChooseFiles(filePanel, False);
139 
140 	if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/", _("Select directory"), NULL) == True) {
141 		char *str = WMGetFilePanelFileName(filePanel);
142 
143 		if (str) {
144 			int len = strlen(str);
145 
146 			/* Remove the trailing '/' except if the path is exactly / */
147 			if (len > 1 && str[len - 1] == '/') {
148 				str[len - 1] = '\0';
149 				len--;
150 			}
151 			if (len > 0) {
152 				WMList *lPtr;
153 				int i;
154 
155 				if (w == panel->icoaB)
156 					lPtr = panel->icoL;
157 				else if (w == panel->pixaB)
158 					lPtr = panel->pixL;
159 				else
160 					goto error_unknown_widget;
161 
162 				i = WMGetListSelectedItemRow(lPtr);
163 				if (i >= 0)
164 					i++;
165 				addPathToList(lPtr, i, str);
166 				WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
167 			}
168 		error_unknown_widget:
169 			wfree(str);
170 		}
171 	}
172 }
173 
paintItem(WMList * lPtr,int index,Drawable d,char * text,int state,WMRect * rect)174 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
175 {
176 	int width, height, x, y;
177 	_Panel *panel = (_Panel *) WMGetHangedData(lPtr);
178 	WMScreen *scr = WMWidgetScreen(lPtr);
179 	Display *dpy = WMScreenDisplay(scr);
180 	WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
181 
182 	/* Parameter not used, but tell the compiler that it is ok */
183 	(void) index;
184 
185 	width = rect->size.width;
186 	height = rect->size.height;
187 	x = rect->pos.x;
188 	y = rect->pos.y;
189 
190 	XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
191 
192 	if (state & 1) {
193 		WMDrawString(scr, d, panel->red, panel->font, x + 4, y, text, strlen(text));
194 	} else {
195 		WMDrawString(scr, d, panel->black, panel->font, x + 4, y, text, strlen(text));
196 	}
197 }
198 
storeData(_Panel * panel)199 static void storeData(_Panel * panel)
200 {
201 	WMPropList *list;
202 	WMPropList *tmp;
203 	int i;
204 	char *p;
205 
206 	list = WMCreatePLArray(NULL, NULL);
207 	for (i = 0; i < WMGetListNumberOfRows(panel->icoL); i++) {
208 		p = WMGetListItem(panel->icoL, i)->text;
209 		tmp = WMCreatePLString(p);
210 		WMAddToPLArray(list, tmp);
211 	}
212 	SetObjectForKey(list, "IconPath");
213 
214 	list = WMCreatePLArray(NULL, NULL);
215 	for (i = 0; i < WMGetListNumberOfRows(panel->pixL); i++) {
216 		p = WMGetListItem(panel->pixL, i)->text;
217 		tmp = WMCreatePLString(p);
218 		WMAddToPLArray(list, tmp);
219 	}
220 	SetObjectForKey(list, "PixmapPath");
221 }
222 
createPanel(Panel * p)223 static void createPanel(Panel * p)
224 {
225 	_Panel *panel = (_Panel *) p;
226 	WMScreen *scr = WMWidgetScreen(panel->parent);
227 	WMTabViewItem *tab;
228 
229 	panel->white = WMWhiteColor(scr);
230 	panel->black = WMBlackColor(scr);
231 	panel->gray = WMGrayColor(scr);
232 	panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
233 	panel->font = WMSystemFontOfSize(scr, 12);
234 
235 	panel->box = WMCreateBox(panel->parent);
236 	WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
237 
238 	panel->tabv = WMCreateTabView(panel->box);
239 	WMMoveWidget(panel->tabv, 12, 10);
240 	WMResizeWidget(panel->tabv, 500, 215);
241 
242 	/* icon path */
243 	panel->icoF = WMCreateFrame(panel->box);
244 	WMSetFrameRelief(panel->icoF, WRFlat);
245 	WMResizeWidget(panel->icoF, 230, 210);
246 
247 	tab = WMCreateTabViewItemWithIdentifier(0);
248 	WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
249 	WMAddItemInTabView(panel->tabv, tab);
250 	WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
251 
252 	panel->icoL = WMCreateList(panel->icoF);
253 	WMResizeWidget(panel->icoL, 480, 147);
254 	WMMoveWidget(panel->icoL, 10, 10);
255 	WMSetListUserDrawProc(panel->icoL, paintItem);
256 	WMHangData(panel->icoL, panel);
257 
258 	panel->icoaB = WMCreateCommandButton(panel->icoF);
259 	WMResizeWidget(panel->icoaB, 95, 24);
260 	WMMoveWidget(panel->icoaB, 293, 165);
261 	WMSetButtonText(panel->icoaB, _("Add"));
262 	WMSetButtonAction(panel->icoaB, browseForFile, panel);
263 	WMSetButtonImagePosition(panel->icoaB, WIPRight);
264 
265 	panel->icorB = WMCreateCommandButton(panel->icoF);
266 	WMResizeWidget(panel->icorB, 95, 24);
267 	WMMoveWidget(panel->icorB, 395, 165);
268 	WMSetButtonText(panel->icorB, _("Remove"));
269 	WMSetButtonAction(panel->icorB, pushButton, panel);
270 
271 	WMMapSubwidgets(panel->icoF);
272 
273 	/* pixmap path */
274 	panel->pixF = WMCreateFrame(panel->box);
275 	WMSetFrameRelief(panel->pixF, WRFlat);
276 	WMResizeWidget(panel->pixF, 230, 210);
277 
278 	tab = WMCreateTabViewItemWithIdentifier(0);
279 	WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
280 	WMAddItemInTabView(panel->tabv, tab);
281 	WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
282 
283 	panel->pixL = WMCreateList(panel->pixF);
284 	WMResizeWidget(panel->pixL, 480, 147);
285 	WMMoveWidget(panel->pixL, 10, 10);
286 	WMSetListUserDrawProc(panel->pixL, paintItem);
287 	WMHangData(panel->pixL, panel);
288 
289 	panel->pixaB = WMCreateCommandButton(panel->pixF);
290 	WMResizeWidget(panel->pixaB, 95, 24);
291 	WMMoveWidget(panel->pixaB, 293, 165);
292 	WMSetButtonText(panel->pixaB, _("Add"));
293 	WMSetButtonAction(panel->pixaB, browseForFile, panel);
294 	WMSetButtonImagePosition(panel->pixaB, WIPRight);
295 
296 	panel->pixrB = WMCreateCommandButton(panel->pixF);
297 	WMResizeWidget(panel->pixrB, 95, 24);
298 	WMMoveWidget(panel->pixrB, 395, 165);
299 	WMSetButtonText(panel->pixrB, _("Remove"));
300 	WMSetButtonAction(panel->pixrB, pushButton, panel);
301 
302 	WMMapSubwidgets(panel->pixF);
303 
304 	WMRealizeWidget(panel->box);
305 	WMMapSubwidgets(panel->box);
306 
307 	showData(panel);
308 }
309 
InitPaths(WMWidget * parent)310 Panel *InitPaths(WMWidget *parent)
311 {
312 	_Panel *panel;
313 
314 	panel = wmalloc(sizeof(_Panel));
315 
316 	panel->sectionName = _("Search Path Configuration");
317 
318 	panel->description = _("Search paths to use when looking for pixmaps\n" "and icons.");
319 
320 	panel->parent = parent;
321 
322 	panel->callbacks.createWidgets = createPanel;
323 	panel->callbacks.updateDomain = storeData;
324 
325 	AddSection(panel, ICON_FILE);
326 
327 	return panel;
328 }
329