Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / Paths.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* Paths.c- pixmap/icon paths
6830b057 dan2004-10-12 21:28:27 +00002 *
9af1c6c4 dan1998-10-21 14:43:47 +00003 * WPrefs - Window Maker Preferences Program
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1998-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * 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
18 * along with this program; if not, write to the Free Software
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
9d2e6ef9 scottc1998-09-29 22:36:29 +000023#include "WPrefs.h"
24#include <unistd.h>
25
26typedef struct _Panel {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020027 WMBox *box;
28 char *sectionName;
d3f29988 kojima1999-04-25 01:49:46 +000029
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020030 char *description;
6830b057 dan2004-10-12 21:28:27 +000031
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020032 CallbackRec callbacks;
9d2e6ef9 scottc1998-09-29 22:36:29 +000033
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020034 WMWidget *parent;
6830b057 dan2004-10-12 21:28:27 +000035
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036 WMTabView *tabv;
9d2e6ef9 scottc1998-09-29 22:36:29 +000037
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020038 WMFrame *pixF;
39 WMList *pixL;
40 WMButton *pixaB;
41 WMButton *pixrB;
9d2e6ef9 scottc1998-09-29 22:36:29 +000042
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020043 WMFrame *icoF;
44 WMList *icoL;
45 WMButton *icoaB;
46 WMButton *icorB;
9d2e6ef9 scottc1998-09-29 22:36:29 +000047
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020048 WMColor *red;
49 WMColor *black;
50 WMColor *white;
51 WMColor *gray;
52 WMFont *font;
9d2e6ef9 scottc1998-09-29 22:36:29 +000053} _Panel;
54
9d2e6ef9 scottc1998-09-29 22:36:29 +000055#define ICON_FILE "paths"
56
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020057static void addPathToList(WMList * list, int index, char *path)
9d2e6ef9 scottc1998-09-29 22:36:29 +000058{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020059 char *fpath = wexpandpath(path);
60 WMListItem *item;
9d2e6ef9 scottc1998-09-29 22:36:29 +000061
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020062 item = WMInsertListItem(list, index, path);
9d2e6ef9 scottc1998-09-29 22:36:29 +000063
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020064 if (access(fpath, X_OK) != 0) {
65 item->uflags = 1;
66 }
67 wfree(fpath);
9d2e6ef9 scottc1998-09-29 22:36:29 +000068}
69
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020070static void showData(_Panel * panel)
9d2e6ef9 scottc1998-09-29 22:36:29 +000071{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020072 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, "/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));
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, "/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));
103 }
104 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000105}
106
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200107static void pushButton(WMWidget * w, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200109 _Panel *panel = (_Panel *) data;
110 int i;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000111
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200112 /* icon paths */
113 if (w == panel->icorB) {
114 i = WMGetListSelectedItemRow(panel->icoL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000115
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200116 if (i >= 0)
117 WMRemoveListItem(panel->icoL, i);
118 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000119
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200120 /* pixmap paths */
121 if (w == panel->pixrB) {
122 i = WMGetListSelectedItemRow(panel->pixL);
6fc9e2a2 dan1999-05-04 23:14:47 +0000123
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200124 if (i >= 0)
125 WMRemoveListItem(panel->pixL, i);
126 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000127}
128
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200129static void browseForFile(WMWidget * w, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000130{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200131 _Panel *panel = (_Panel *) data;
132 WMFilePanel *filePanel;
133
134 filePanel = WMGetOpenPanel(WMWidgetScreen(w));
135
136 WMSetFilePanelCanChooseFiles(filePanel, False);
137
138 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, "/", _("Select directory"), NULL) == True) {
139 char *str = WMGetFilePanelFileName(filePanel);
140
141 if (str) {
142 int len = strlen(str);
143
144 /* Remove the trailing '/' except if the path is exactly / */
145 if (len > 1 && str[len - 1] == '/') {
146 str[len - 1] = '\0';
147 len--;
148 }
149 if (len > 0) {
150 WMList *lPtr;
151 int i;
152
153 if (w == panel->icoaB)
154 lPtr = panel->icoL;
155 else if (w == panel->pixaB)
156 lPtr = panel->pixL;
157
158 i = WMGetListSelectedItemRow(lPtr);
159 if (i >= 0)
160 i++;
161 addPathToList(lPtr, i, str);
162 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
163
164 wfree(str);
165 }
166 }
167 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000168}
169
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000171{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200172 int width, height, x, y;
173 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
174 WMScreen *scr = WMWidgetScreen(lPtr);
175 Display *dpy = WMScreenDisplay(scr);
176 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
177
178 width = rect->size.width;
179 height = rect->size.height;
180 x = rect->pos.x;
181 y = rect->pos.y;
182
183 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
184
185 if (state & 1) {
186 WMDrawString(scr, d, panel->red, panel->font, x + 4, y, text, strlen(text));
187 } else {
188 WMDrawString(scr, d, panel->black, panel->font, x + 4, y, text, strlen(text));
189 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000190}
191
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200192static void storeData(_Panel * panel)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000193{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200194 WMPropList *list;
195 WMPropList *tmp;
196 int i;
197 char *p;
198
199 list = WMCreatePLArray(NULL, NULL);
200 for (i = 0; i < WMGetListNumberOfRows(panel->icoL); i++) {
201 p = WMGetListItem(panel->icoL, i)->text;
202 tmp = WMCreatePLString(p);
203 WMAddToPLArray(list, tmp);
204 }
205 SetObjectForKey(list, "IconPath");
206
207 list = WMCreatePLArray(NULL, NULL);
208 for (i = 0; i < WMGetListNumberOfRows(panel->pixL); i++) {
209 p = WMGetListItem(panel->pixL, i)->text;
210 tmp = WMCreatePLString(p);
211 WMAddToPLArray(list, tmp);
212 }
213 SetObjectForKey(list, "PixmapPath");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000214}
215
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200216static void createPanel(Panel * p)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000217{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200218 _Panel *panel = (_Panel *) p;
219 WMScreen *scr = WMWidgetScreen(panel->parent);
220 WMTabViewItem *tab;
221
222 panel->white = WMWhiteColor(scr);
223 panel->black = WMBlackColor(scr);
224 panel->gray = WMGrayColor(scr);
225 panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
226 panel->font = WMSystemFontOfSize(scr, 12);
227
228 panel->box = WMCreateBox(panel->parent);
229 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
230
231 panel->tabv = WMCreateTabView(panel->box);
232 WMMoveWidget(panel->tabv, 12, 10);
233 WMResizeWidget(panel->tabv, 500, 215);
234
235 /* icon path */
236 panel->icoF = WMCreateFrame(panel->box);
237 WMSetFrameRelief(panel->icoF, WRFlat);
238 WMResizeWidget(panel->icoF, 230, 210);
239
240 tab = WMCreateTabViewItemWithIdentifier(0);
241 WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
242 WMAddItemInTabView(panel->tabv, tab);
243 WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
244
245 panel->icoL = WMCreateList(panel->icoF);
246 WMResizeWidget(panel->icoL, 480, 147);
247 WMMoveWidget(panel->icoL, 10, 10);
248 WMSetListUserDrawProc(panel->icoL, paintItem);
249 WMHangData(panel->icoL, panel);
250
251 panel->icoaB = WMCreateCommandButton(panel->icoF);
252 WMResizeWidget(panel->icoaB, 95, 24);
253 WMMoveWidget(panel->icoaB, 293, 165);
254 WMSetButtonText(panel->icoaB, _("Add"));
255 WMSetButtonAction(panel->icoaB, browseForFile, panel);
256 WMSetButtonImagePosition(panel->icoaB, WIPRight);
257
258 panel->icorB = WMCreateCommandButton(panel->icoF);
259 WMResizeWidget(panel->icorB, 95, 24);
260 WMMoveWidget(panel->icorB, 395, 165);
261 WMSetButtonText(panel->icorB, _("Remove"));
262 WMSetButtonAction(panel->icorB, pushButton, panel);
263
264 WMMapSubwidgets(panel->icoF);
265
266 /* pixmap path */
267 panel->pixF = WMCreateFrame(panel->box);
268 WMSetFrameRelief(panel->pixF, WRFlat);
269 WMResizeWidget(panel->pixF, 230, 210);
270
271 tab = WMCreateTabViewItemWithIdentifier(0);
272 WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
273 WMAddItemInTabView(panel->tabv, tab);
274 WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
275
276 panel->pixL = WMCreateList(panel->pixF);
277 WMResizeWidget(panel->pixL, 480, 147);
278 WMMoveWidget(panel->pixL, 10, 10);
279 WMSetListUserDrawProc(panel->pixL, paintItem);
280 WMHangData(panel->pixL, panel);
281
282 panel->pixaB = WMCreateCommandButton(panel->pixF);
283 WMResizeWidget(panel->pixaB, 95, 24);
284 WMMoveWidget(panel->pixaB, 293, 165);
285 WMSetButtonText(panel->pixaB, _("Add"));
286 WMSetButtonAction(panel->pixaB, browseForFile, panel);
287 WMSetButtonImagePosition(panel->pixaB, WIPRight);
288
289 panel->pixrB = WMCreateCommandButton(panel->pixF);
290 WMResizeWidget(panel->pixrB, 95, 24);
291 WMMoveWidget(panel->pixrB, 395, 165);
292 WMSetButtonText(panel->pixrB, _("Remove"));
293 WMSetButtonAction(panel->pixrB, pushButton, panel);
294
295 WMMapSubwidgets(panel->pixF);
296
297 WMRealizeWidget(panel->box);
298 WMMapSubwidgets(panel->box);
299
300 showData(panel);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000301}
302
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200303Panel *InitPaths(WMScreen * scr, WMWidget * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000304{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200305 _Panel *panel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000306
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200307 panel = wmalloc(sizeof(_Panel));
308 memset(panel, 0, sizeof(_Panel));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000309
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200310 panel->sectionName = _("Search Path Configuration");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000311
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200312 panel->description = _("Search paths to use when looking for pixmaps\n" "and icons.");
d3f29988 kojima1999-04-25 01:49:46 +0000313
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200314 panel->parent = parent;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000315
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200316 panel->callbacks.createWidgets = createPanel;
317 panel->callbacks.updateDomain = storeData;
6830b057 dan2004-10-12 21:28:27 +0000318
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200319 AddSection(panel, ICON_FILE);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000320
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200321 return panel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000322}