Initial repo
[fsviewer.git] / src / editInspector.c
blob899d5ff6e7470e9d512925e4b1f8092652daa75a
1 /* editInspector.c */
3 #include "FSViewer.h"
4 #include "FSUtils.h"
6 /* for: scr->normalFont */
7 #include <WINGs/WINGsP.h>
9 #define WIDTH 272
10 #define HEIGHT 272
11 #define LABEL_HEIGHT 16
12 #define LABEL_WIDTH 48
14 typedef struct _Panel {
15 WMFrame *frame;
16 char *sectionName;
18 CallbackRec callbacks;
20 WMWindow *win;
22 WMLabel *defaultLabel;
24 WMList *appList;
25 WMTextField *execField;
27 WMButton *okBtn;
28 WMButton *revertBtn;
29 WMButton *appBtn;
31 FileInfo *fileInfo;
33 } _Panel;
35 static void
36 displayDefault(_Panel *panel)
38 char *extn = NULL;
39 char *exec = NULL;
41 exec = FSGetStringForNameKey(panel->fileInfo->extn, "editor");
43 if(exec)
45 WMSetLabelText(panel->defaultLabel, exec);
46 WMSetTextFieldText(panel->execField, exec);
48 else
50 WMSetLabelText(panel->defaultLabel, "");
51 WMSetTextFieldText(panel->execField, "");
54 WMSetButtonImagePosition(panel->appBtn, WIPNoImage);
55 WMSelectListItem(panel->appList, -1);
57 if(extn)
58 free(extn);
59 if(exec)
60 free(exec);
64 static void
65 showData(_Panel *panel)
67 WMPropList* array = NULL;
69 WMClearList(panel->appList);
71 array = FSGetUDObjectForKey(defaultsDB, "EXE");
72 if(array && WMIsPLArray(array))
74 int numElem, i;
76 numElem = WMGetPropListItemCount(array);
77 for(i = 0; i < numElem; i++)
78 WMAddListItem(panel->appList,
79 WMGetPropListDescription(WMGetFromPLArray(array, i), False));
81 WMSortListItems(panel->appList);
83 displayDefault(panel);
87 static void
88 storeData(_Panel *panel)
90 char *exec;
92 exec = WMGetTextFieldText(panel->execField);
94 if(exec)
96 WMPropList* array = NULL;
98 FSSetStringForNameKey(panel->fileInfo->extn, "editor", exec);
99 array = FSGetUDObjectForKey(defaultsDB, "EXTN");
100 if(array && WMIsPLArray(array))
101 InsertArrayElement(array, WMCreatePLString(panel->fileInfo->extn));
102 free(exec);
106 static void
107 handleAppBtnEvents(XEvent *event, void *data)
109 /* if(WMIsDoubleClick(event)) */
110 /* printf("Double Click\n"); */
113 static void
114 appListClick(WMWidget *self, void *data)
116 char *tmp = NULL;
117 char *icon = NULL;
118 char *exec = NULL;
119 char *selected = NULL;
120 Panel *panel = (Panel *) data;
122 selected = ((WMListItem *)WMGetListSelectedItem(panel->appList))->text;
124 tmp = FSGetStringForNameKey(selected, "icon");
125 icon = LocateImage(tmp);
126 exec = FSGetStringForNameKey(selected, "exec");
128 if(icon)
130 RColor color;
131 WMPixmap *pixmap;
133 color.red = 0xae;
134 color.green = 0xaa;
135 color.blue = 0xae;
136 color.alpha = 0;
137 /* FS.. */
138 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win),
139 icon, &color);
140 if(pixmap)
142 WMSetButtonImagePosition(panel->appBtn, WIPImageOnly);
143 WMSetButtonImage(panel->appBtn, pixmap);
145 else
146 WMSetButtonImagePosition(panel->appBtn, WIPNoImage);
148 else
149 WMSetButtonImagePosition(panel->appBtn, WIPNoImage);
151 if(exec)
152 WMSetTextFieldText(panel->execField, exec);
154 if(tmp)
155 free(tmp);
156 if(icon)
157 free(icon);
158 if(exec)
159 free(exec);
162 static void
163 buttonClick(WMWidget *self, void *data)
165 Panel *panel = (Panel *)data;
167 if ((WMButton *)self == panel->okBtn)
169 storeData(panel);
170 showData(panel);
171 WMSetButtonEnabled(panel->revertBtn, True);
173 else if ((WMButton *)self == panel->revertBtn)
175 displayDefault(panel);
176 WMSetButtonEnabled(panel->revertBtn, False);
178 else if ((WMButton *)self == panel->appBtn)
180 char *exeStr = NULL;
181 WMListItem *item = NULL;
183 item = WMGetListSelectedItem(panel->appList);
184 exeStr = WMGetTextFieldText(panel->execField);
186 if( item && strcmp("", exeStr) )
188 char *exec = NULL;
189 char *path = NULL;
191 path = GetPathnameFromPathName(panel->fileInfo->path,
192 panel->fileInfo->name);
194 exec = FSParseExecString(path, exeStr);
195 execCommand(exec);
197 if(path)
198 free(path);
199 if(exec)
200 free(exec);
205 static void
206 createAppList(Panel *panel)
208 panel->appList = WMCreateList(panel->frame);
210 WMMoveWidget(panel->appList, 8, 36);
211 WMResizeWidget(panel->appList, 180, 115);
212 WMSetListAction(panel->appList, appListClick, panel);
215 static void
216 createClickLabel(Panel *panel)
218 WMLabel *l;
220 l = WMCreateLabel(panel->frame);
221 WMSetLabelWraps(l, True);
222 WMResizeWidget(l, WIDTH-8, LABEL_HEIGHT*2);
223 WMMoveWidget(l, 4, 0);
224 WMSetLabelText(l, _("Click button to edit selected document"));
225 /* WMSetLabelTextAlignment(l, WACenter); */
226 WMSetLabelRelief(l, WRFlat);
227 WMSetLabelTextColor(l, WMDarkGrayColor(WMWidgetScreen(panel->win)));
231 static void
232 createDefaultLabels(Panel *panel)
234 WMLabel *l;
235 WMScreen *scr;
236 int tw;
238 l = WMCreateLabel(panel->frame);
239 WMSetLabelText(l, _("Default:"));
240 scr = WMWidgetScreen(panel->win);
241 tw = WMWidthOfString(scr->normalFont, WMGetLabelText(l),
242 strlen(WMGetLabelText(l)));
243 WMResizeWidget(l, tw+4, LABEL_HEIGHT);
244 WMMoveWidget(l, 4, 182);
245 WMSetLabelRelief(l, WRFlat);
246 WMSetLabelTextColor(l, WMDarkGrayColor(WMWidgetScreen(panel->win)));
248 panel->defaultLabel = WMCreateLabel(panel->frame);
249 WMResizeWidget(panel->defaultLabel, WIDTH, LABEL_HEIGHT);
250 /* x-Position should be: 4 + width("Default:") + 4 */
251 WMMoveWidget(panel->defaultLabel, 8+tw, 182);
252 WMSetLabelRelief(panel->defaultLabel, WRFlat);
256 static void
257 createSetDefaultLabels(Panel *panel)
259 WMLabel *l;
261 l = WMCreateLabel(panel->frame);
262 WMSetLabelWraps(l, True);
263 WMResizeWidget(l, WIDTH-8, LABEL_HEIGHT*2);
264 WMMoveWidget(l, 4, 206);
265 WMSetLabelText(l, _("Click \"Set Default\" to set default "\
266 "application for all documents with this extension."));
267 WMSetLabelTextAlignment(l, WALeft);
268 WMSetLabelRelief(l, WRFlat);
269 WMSetLabelTextColor(l, WMDarkGrayColor(WMWidgetScreen(panel->win)));
273 static void
274 createExecField(Panel *panel)
276 panel->execField = WMCreateTextField(panel->frame);
277 WMMoveWidget(panel->execField, 8, 159);
278 WMResizeWidget(panel->execField, 256, 18);
281 static void
282 createButtons(Panel *panel)
285 panel->revertBtn = WMCreateCommandButton(panel->frame);
286 WMMoveWidget(panel->revertBtn, 16, HEIGHT-24);
287 WMResizeWidget(panel->revertBtn, 115, 24);
288 WMSetButtonText(panel->revertBtn, _("Revert"));
289 WMSetButtonAction(panel->revertBtn, buttonClick, panel);
290 WMSetButtonEnabled(panel->revertBtn, False);
292 panel->okBtn = WMCreateCommandButton(panel->frame);
293 WMMoveWidget(panel->okBtn, 140, HEIGHT-24);
294 WMResizeWidget(panel->okBtn, 115, 24);
295 WMSetButtonText(panel->okBtn, _("Set Default"));
296 WMSetButtonImage(panel->okBtn,
297 WMGetSystemPixmap(WMWidgetScreen(panel->win),
298 WSIReturnArrow));
299 WMSetButtonAltImage(panel->okBtn,
300 WMGetSystemPixmap(WMWidgetScreen(panel->win),
301 WSIHighlightedReturnArrow));
302 WMSetButtonImagePosition(panel->okBtn, WIPRight);
303 WMSetButtonEnabled(panel->okBtn, True);
304 WMSetButtonAction(panel->okBtn, buttonClick, panel);
306 panel->appBtn = WMCreateCommandButton(panel->frame);
307 WMMoveWidget(panel->appBtn, 196, 59); /* 36 + (115/2) - 68/2 = 59 */
308 WMResizeWidget(panel->appBtn, 68, 68);
309 WMSetButtonEnabled(panel->appBtn, True);
310 WMSetButtonAction(panel->appBtn, buttonClick, panel);
311 WMSetButtonImagePosition(panel->appBtn, WIPImageOnly);
312 WMCreateEventHandler(WMWidgetView(panel->appBtn), ButtonPressMask,
313 handleAppBtnEvents, panel);
316 static void
317 createPanel(Panel *p)
319 _Panel *panel = (_Panel*)p;
320 panel->frame = WMCreateFrame(panel->win);
322 WMResizeWidget(panel->frame, WIDTH, HEIGHT);
323 WMMoveWidget(panel->frame, 0, 138);
324 WMSetFrameRelief(panel->frame, WRFlat);
326 createSetDefaultLabels(panel);
327 createAppList(panel);
328 createDefaultLabels(panel);
329 createClickLabel(panel);
330 createExecField(panel);
331 createButtons(panel);
333 WMRealizeWidget(panel->frame);
334 WMMapSubwidgets(panel->frame);
338 Panel*
339 InitEditor(WMWindow *win, FileInfo *fileInfo)
341 _Panel *panel;
343 panel = wmalloc(sizeof(_Panel));
344 memset(panel, 0, sizeof(_Panel));
346 panel->sectionName = (char *) wmalloc(strlen(_("Editor Inspector"))+1);
347 strcpy(panel->sectionName, _("Editor Inspector"));
349 panel->win = win;
351 panel->callbacks.createWidgets = createPanel;
352 panel->callbacks.updateDomain = storeData;
353 panel->callbacks.updateDisplay = showData;
355 panel->fileInfo = fileInfo;
357 return panel;