Fix problem with title bar
[fsviewer.git] / src / viewInspector.c
blob4309597d3206bce41393fd4def0a579a9ec37050
1 /* viewInspector.c */
3 #include <WINGs/WINGsP.h> /* for: scr->normalFont */
5 #include "FSViewer.h"
6 #include "FSUtils.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, "viewer");
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, "viewer", 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 else {
155 WMSetTextFieldText(panel->execField, "");
158 if(tmp)
159 free(tmp);
160 if(icon)
161 free(icon);
162 if(exec)
163 free(exec);
166 static void
167 buttonClick(WMWidget *self, void *data)
169 Panel *panel = (Panel *)data;
171 if ((WMButton *)self == panel->okBtn)
173 storeData(panel);
174 showData(panel);
175 WMSetButtonEnabled(panel->revertBtn, True);
177 else if ((WMButton *)self == panel->revertBtn)
179 displayDefault(panel);
180 WMSetButtonEnabled(panel->revertBtn, False);
182 else if ((WMButton *)self == panel->appBtn)
184 char *exeStr = NULL;
185 WMListItem *item = NULL;
187 item = WMGetListSelectedItem(panel->appList);
188 exeStr = WMGetTextFieldText(panel->execField);
190 if (item && strcmp("", exeStr))
192 char *exec = NULL;
193 char *path = NULL;
195 path = GetPathnameFromPathName(panel->fileInfo->path,
196 panel->fileInfo->name);
198 exec = FSParseExecString(path, exeStr);
199 execCommand(exec);
201 if (path)
202 free(path);
203 if (exec)
204 free(exec);
209 static void
210 createAppList(Panel *panel)
212 panel->appList = WMCreateList(panel->frame);
214 WMMoveWidget(panel->appList, 8, 36);
215 WMResizeWidget(panel->appList, 180, 115);
216 WMSetListAction(panel->appList, appListClick, panel);
219 static void
220 createClickLabel(Panel *panel)
222 WMLabel *l;
224 l = WMCreateLabel(panel->frame);
225 WMSetLabelWraps(l, 1);
226 WMResizeWidget(l, WIDTH-8, LABEL_HEIGHT*2);
227 WMMoveWidget(l, 4, 0);
228 WMSetLabelText(l, _("Click button to edit selected document"));
229 /* WMSetLabelTextAlignment(l, WACenter); */
230 WMSetLabelRelief(l, WRFlat);
231 WMSetLabelTextColor(l, WMDarkGrayColor(WMWidgetScreen(panel->win)));
235 static void
236 createDefaultLabels(Panel *panel)
238 WMLabel *l;
239 WMScreen *scr;
240 int tw;
242 l = WMCreateLabel(panel->frame);
243 WMSetLabelText(l, _("Default:"));
244 scr = WMWidgetScreen(panel->win);
245 tw = WMWidthOfString(scr->normalFont, WMGetLabelText(l),
246 strlen(WMGetLabelText(l)));
247 WMResizeWidget(l, tw+4, LABEL_HEIGHT);
248 WMMoveWidget(l, 4, 182);
249 WMSetLabelRelief(l, WRFlat);
250 WMSetLabelTextColor(l, WMDarkGrayColor(WMWidgetScreen(panel->win)));
251 /* Position of "defaultLabel" is adjusted by width of "l"
252 to make i18n support easier for translators */
253 panel->defaultLabel = WMCreateLabel(panel->frame);
254 /* Initilalization without text */
255 WMSetLabelText(panel->defaultLabel,"");
256 WMResizeWidget(panel->defaultLabel, WIDTH, LABEL_HEIGHT);
257 /* x-Position should be: 4 + width("Default:") + 4 */
258 WMMoveWidget(panel->defaultLabel, 8+tw, 182);
259 WMSetLabelRelief(panel->defaultLabel, WRFlat);
262 static void
263 createSetDefaultLabels(Panel *panel)
265 WMLabel *l;
267 l = WMCreateLabel(panel->frame);
268 WMSetLabelWraps(l, 1);
269 WMResizeWidget(l, WIDTH-8, LABEL_HEIGHT*2);
270 WMMoveWidget(l, 4, 206);
271 WMSetLabelText(l, _("Click \"Set Default\" to set default "\
272 "application for all documents with this extension."));
273 WMSetLabelTextAlignment(l, WALeft);
274 WMSetLabelRelief(l, WRFlat);
275 WMSetLabelTextColor(l, WMDarkGrayColor(WMWidgetScreen(panel->win)));
279 static void
280 createExecField(Panel *panel)
282 panel->execField = WMCreateTextField(panel->frame);
283 WMMoveWidget(panel->execField, 8, 159);
284 WMResizeWidget(panel->execField, 256, 18);
287 static void
288 createButtons(Panel *panel)
290 panel->revertBtn = WMCreateCommandButton(panel->frame);
291 WMMoveWidget(panel->revertBtn, 16, HEIGHT-24);
292 WMResizeWidget(panel->revertBtn, 115, 24);
293 WMSetButtonText(panel->revertBtn, _("Revert"));
294 WMSetButtonAction(panel->revertBtn, buttonClick, panel);
295 WMSetButtonEnabled(panel->revertBtn, False);
297 panel->okBtn = WMCreateCommandButton(panel->frame);
298 WMMoveWidget(panel->okBtn, 140, HEIGHT-24);
299 WMResizeWidget(panel->okBtn, 115, 24);
300 WMSetButtonText(panel->okBtn, _("Set Default"));
301 WMSetButtonImage(panel->okBtn,
302 WMGetSystemPixmap(WMWidgetScreen(panel->win),
303 WSIReturnArrow));
304 WMSetButtonAltImage(panel->okBtn,
305 WMGetSystemPixmap(WMWidgetScreen(panel->win),
306 WSIHighlightedReturnArrow));
307 WMSetButtonImagePosition(panel->okBtn, WIPRight);
308 WMSetButtonEnabled(panel->okBtn, True);
309 WMSetButtonAction(panel->okBtn, buttonClick, panel);
311 panel->appBtn = WMCreateCommandButton(panel->frame);
312 WMMoveWidget(panel->appBtn, 196, 59); /* 36 + (115/2) - 68/2 = 59 */
313 WMResizeWidget(panel->appBtn, 68, 68);
314 WMSetButtonEnabled(panel->appBtn, True);
315 WMSetButtonImagePosition(panel->appBtn, WIPImageOnly);
316 WMSetButtonAction(panel->appBtn, buttonClick, panel);
317 WMCreateEventHandler(WMWidgetView(panel->appBtn), ButtonPressMask,
318 handleAppBtnEvents, panel);
321 static void
322 createPanel(Panel *p)
324 _Panel *panel = (_Panel*)p;
325 panel->frame = WMCreateFrame(panel->win);
327 WMResizeWidget(panel->frame, WIDTH, HEIGHT);
328 WMMoveWidget(panel->frame, 0, 138);
329 WMSetFrameRelief(panel->frame, WRFlat);
331 createSetDefaultLabels(panel);
332 createAppList(panel);
333 createDefaultLabels(panel);
334 createClickLabel(panel);
335 createExecField(panel);
336 createButtons(panel);
338 WMRealizeWidget(panel->frame);
339 WMMapSubwidgets(panel->frame);
343 Panel*
344 InitViewer(WMWindow *win, FileInfo *fileInfo)
346 _Panel *panel;
348 panel = wmalloc(sizeof(_Panel));
349 memset(panel, 0, sizeof(_Panel));
351 panel->sectionName = (char *) wmalloc(strlen(_("Viewer Inspector"))+1);
352 strcpy(panel->sectionName, _("Viewer Inspector"));
354 panel->win = win;
356 panel->callbacks.createWidgets = createPanel;
357 panel->callbacks.updateDomain = storeData;
358 panel->callbacks.updateDisplay = showData;
360 panel->fileInfo = fileInfo;
362 return panel;