WPrefs: fix memory leak when storing the list of texture in Appearence panel (Coverit...
[wmaker-crm.git] / WPrefs.app / Paths.c
blob6aa7ef57a86d932c8ed263fe386f4d309c968966
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, const 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;
153 int i;
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;
162 i = WMGetListSelectedItemRow(lPtr);
163 if (i >= 0)
164 i++;
165 addPathToList(lPtr, i, str);
166 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
168 error_unknown_widget:
169 wfree(str);
174 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
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;
182 /* Parameter not used, but tell the compiler that it is ok */
183 (void) index;
185 width = rect->size.width;
186 height = rect->size.height;
187 x = rect->pos.x;
188 y = rect->pos.y;
190 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
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));
199 static void storeData(_Panel * panel)
201 WMPropList *list;
202 WMPropList *tmp;
203 int i;
204 char *p;
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);
212 SetObjectForKey(list, "IconPath");
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);
220 SetObjectForKey(list, "PixmapPath");
223 static void createPanel(Panel * p)
225 _Panel *panel = (_Panel *) p;
226 WMScreen *scr = WMWidgetScreen(panel->parent);
227 WMTabViewItem *tab;
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);
235 panel->box = WMCreateBox(panel->parent);
236 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
238 panel->tabv = WMCreateTabView(panel->box);
239 WMMoveWidget(panel->tabv, 12, 10);
240 WMResizeWidget(panel->tabv, 500, 215);
242 /* icon path */
243 panel->icoF = WMCreateFrame(panel->box);
244 WMSetFrameRelief(panel->icoF, WRFlat);
245 WMResizeWidget(panel->icoF, 230, 210);
247 tab = WMCreateTabViewItemWithIdentifier(0);
248 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
249 WMAddItemInTabView(panel->tabv, tab);
250 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
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);
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);
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);
271 WMMapSubwidgets(panel->icoF);
273 /* pixmap path */
274 panel->pixF = WMCreateFrame(panel->box);
275 WMSetFrameRelief(panel->pixF, WRFlat);
276 WMResizeWidget(panel->pixF, 230, 210);
278 tab = WMCreateTabViewItemWithIdentifier(0);
279 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
280 WMAddItemInTabView(panel->tabv, tab);
281 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
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);
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);
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);
302 WMMapSubwidgets(panel->pixF);
304 WMRealizeWidget(panel->box);
305 WMMapSubwidgets(panel->box);
307 showData(panel);
310 Panel *InitPaths(WMWidget *parent)
312 _Panel *panel;
314 panel = wmalloc(sizeof(_Panel));
316 panel->sectionName = _("Search Path Configuration");
318 panel->description = _("Search paths to use when looking for pixmaps\n" "and icons.");
320 panel->parent = parent;
322 panel->callbacks.createWidgets = createPanel;
323 panel->callbacks.updateDomain = storeData;
325 AddSection(panel, ICON_FILE);
327 return panel;