Fix problem with title bar
[fsviewer.git] / src / FSInspector.c
blob9ff4384df725b301f5722a0c7fb077884d924bad
1 /* FSInspector.c */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <X11/keysym.h>
8 #include "FSViewer.h"
9 #include "FSUtils.h"
11 #define INITIALIZED_PANEL (1<<0)
12 #define FSI_WIDTH 272
13 #define FSI_HEIGHT 418
14 #define MF_X 5
15 #define MF_Y 5
16 #define NUM_PANEL 5/* 6 */
17 #define INIT_PANEL 0
19 enum
21 AttribPanel = 0,
22 IconPanel = 1,
23 ViewPanel = 2,
24 EditPanel = 3,
25 ExePanel = 4,
26 /* ExtnPanel = 5, */
29 typedef struct _FSInspector {
30 WMWindow *win;
32 WMFrame *mainF;
33 WMFrame *infoF;
34 WMPopUpButton *popupMenu;
36 WMLabel *nameLabel;
37 WMLabel *pathLabel;
39 Bool isOpen;
41 FileInfo *fileInfo;
43 FSViewer *fsViewer;
45 Panel *panel[NUM_PANEL];
46 Panel *currentPanel;
48 } _FSInspector;
51 typedef struct _FSInspector FSInspector;
52 static FSInspector *fsInspector;
54 static void
55 FSEnableInspectorPanels(FSInspector *fsInspector)
57 char *pathname;
58 struct stat *st;
60 st = (struct stat *) wmalloc(sizeof(struct stat));
62 pathname = (char *) wmalloc(strlen(fsInspector->fileInfo->path)+
63 strlen(fsInspector->fileInfo->name)+1);
64 strcpy(pathname, fsInspector->fileInfo->path);
65 if (fsInspector->fileInfo->fileType != ROOT)
66 strcat(pathname, fsInspector->fileInfo->name);
68 /* get information of a file represented by pathname */
69 if (stat(pathname, st) == -1)
70 wwarning(_("%s %d: Stat Error"), __FILE__, __LINE__);
72 if (S_ISDIR(st->st_mode))
74 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, EditPanel, False);
75 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, ViewPanel, False);
76 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, ExePanel, False);
77 /* WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, ExtnPanel, False); */
79 else
81 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, EditPanel, True);
82 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, ViewPanel, True);
83 if (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
85 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu,
86 ExePanel, True);
87 /* WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, */
88 /* ExtnPanel, True); */
90 else
92 WMSetPopUpButtonItemEnabled(fsInspector->popupMenu,
93 ExePanel, False);
94 /* WMSetPopUpButtonItemEnabled(fsInspector->popupMenu, */
95 /* ExtnPanel, False); */
99 if (st)
101 free(st);
102 st = NULL;
105 if (pathname)
106 free(pathname);
109 static void
110 FSUpdateInspectorFileInfoDisplay(FSInspector *fsInspector)
112 char *buf;
113 RColor color;
114 WMPixmap *pixmap;
116 color.red = 0xae;
117 color.green = 0xaa; /* aa ?*/
118 color.blue = 0xae;
119 color.alpha = 0;
121 if (fsInspector->fileInfo->fileType != ROOT)
122 WMSetLabelText(fsInspector->nameLabel, fsInspector->fileInfo->name);
123 else
125 char *tmp;
127 tmp = FSNodeName();
128 WMSetLabelText(fsInspector->nameLabel, tmp);
130 /* why FS... */
131 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(fsInspector->win),
132 fsInspector->fileInfo->imgName,
133 &color);
134 WMSetLabelImage(fsInspector->nameLabel, pixmap);
136 buf = (char *) wmalloc(strlen(_("Path: ")) +
137 strlen(fsInspector->fileInfo->path)+1);
138 sprintf(buf, _("Path: %s"), fsInspector->fileInfo->path);
139 WMSetLabelText(fsInspector->pathLabel, buf);
141 if (buf)
142 free(buf);
146 static void
147 FSCopyInspectorFileInfo(FileInfo *fileInfo)
149 fsInspector->fileInfo->name =
150 (char *) wrealloc(fsInspector->fileInfo->name,
151 strlen(fileInfo->name)+1);
152 strcpy(fsInspector->fileInfo->name, fileInfo->name);
154 fsInspector->fileInfo->path =
155 (char *) wrealloc(fsInspector->fileInfo->path,
156 strlen(fileInfo->path)+1);
157 strcpy(fsInspector->fileInfo->path, fileInfo->path);
159 fsInspector->fileInfo->extn =
160 (char *) wrealloc(fsInspector->fileInfo->extn,
161 strlen(fileInfo->extn)+1);
162 strcpy(fsInspector->fileInfo->extn, fileInfo->extn);
164 fsInspector->fileInfo->abbrev =
165 (char *) wrealloc(fsInspector->fileInfo->abbrev,
166 strlen(fileInfo->abbrev)+1);
167 strcpy(fsInspector->fileInfo->abbrev, fileInfo->abbrev);
169 fsInspector->fileInfo->imgName =
170 (char *) wrealloc(fsInspector->fileInfo->imgName,
171 strlen(fileInfo->imgName)+1);
172 strcpy(fsInspector->fileInfo->imgName, fileInfo->imgName);
174 fsInspector->fileInfo->fileType = fileInfo->fileType;
177 static void
178 FSHideInspectorPanel(Panel *panel)
180 PanelRec *rec = (PanelRec*)panel;
182 WMUnmapWidget(rec->frame);
185 static void
186 FSShowInspectorPanel(Panel *panel)
188 PanelRec *rec = (PanelRec*)panel;
190 if (!(rec->callbacks.flags & INITIALIZED_PANEL)) {
191 (*rec->callbacks.createWidgets)(panel);
192 rec->callbacks.flags |= INITIALIZED_PANEL;
195 WMSetWindowTitle(fsInspector->win, rec->sectionName);
197 (*rec->callbacks.updateDisplay)(panel);
198 WMMapWidget(rec->frame);
201 void
202 FSCloseInspectorWindow(WMWidget *w, void *data)
204 fsInspector->isOpen = False;
205 WMUnmapWidget((WMWindow *) w);
208 void
209 FSUpdateInspectorWindow(FileInfo *fileInfo)
211 if (fsInspector->isOpen)
213 fsInspector->currentPanel = fsInspector->panel[AttribPanel];
214 WMSetPopUpButtonSelectedItem(fsInspector->popupMenu , AttribPanel);
215 /* WMMapWidget(fsInspector->win); */
216 FSCopyInspectorFileInfo(fileInfo);
217 FSUpdateInspectorFileInfoDisplay(fsInspector);
218 FSEnableInspectorPanels(fsInspector);
219 FSShowInspectorPanel(fsInspector->currentPanel);
220 fsInspector->isOpen = True;
224 void
225 FSShowInspectorWindow(WMScreen *scr, FileInfo *fileInfo)
227 fsInspector->currentPanel = fsInspector->panel[AttribPanel];
228 WMSetPopUpButtonSelectedItem(fsInspector->popupMenu , AttribPanel);
229 WMMapWidget(fsInspector->win);
230 FSCopyInspectorFileInfo(fileInfo);
231 FSUpdateInspectorFileInfoDisplay(fsInspector);
232 FSEnableInspectorPanels(fsInspector);
233 FSShowInspectorPanel(fsInspector->currentPanel);
234 fsInspector->isOpen = True;
237 void
238 FSHideInspectorWindow(WMScreen *scr)
240 FSCloseInspectorWindow(fsInspector->win, NULL);
243 static void
244 FSInspectorPopupAction(WMWidget *self, void *data)
246 int i = WMGetPopUpButtonSelectedItem((WMPopUpButton *)self);
248 if (fsInspector->currentPanel != fsInspector->panel[i])
250 FSHideInspectorPanel(fsInspector->currentPanel);
251 fsInspector->currentPanel = fsInspector->panel[i];
252 FSShowInspectorPanel(fsInspector->panel[i]);
256 static void
257 FSCreateInspectorWindow()
259 WMScreen *scr = FSGetFSViewerScreen(fsInspector->fsViewer);
260 WMFont *font;
262 fsInspector->win = WMCreateWindow(scr, "FSViewer Inspector");
263 WMResizeWidget(fsInspector->win, FSI_WIDTH, FSI_HEIGHT);
264 WMSetWindowTitle(fsInspector->win, _("FSViewer Inspector"));
265 WMSetWindowCloseAction(fsInspector->win, FSCloseInspectorWindow, NULL);
266 WMSetWindowMaxSize(fsInspector->win, FSI_WIDTH, FSI_HEIGHT);
267 WMSetWindowMinSize(fsInspector->win, FSI_WIDTH, FSI_HEIGHT);
269 /* mainF */
270 fsInspector->mainF = WMCreateFrame(fsInspector->win);
271 WMResizeWidget(fsInspector->mainF, FSI_WIDTH+10, FSI_HEIGHT+10);
272 WMMoveWidget(fsInspector->mainF, -MF_X, -MF_Y);
273 WMSetFrameRelief(fsInspector->mainF, WRFlat);
275 font = WMCreateFont(WMWidgetScreen(fsInspector->win),
276 "-*-helvetica-medium-r-*-*-18-*-*-*-*-*-*-*");
277 if (!font)
278 font = WMBoldSystemFontOfSize(WMWidgetScreen(fsInspector->win), 18);
280 fsInspector->popupMenu = WMCreatePopUpButton(fsInspector->mainF);
281 WMResizeWidget(fsInspector->popupMenu, 112, 20);
282 WMMoveWidget(fsInspector->popupMenu, ((FSI_WIDTH-112)/2)+MF_X, 8+MF_Y);
283 WMSetPopUpButtonText(fsInspector->popupMenu, _("Select"));
284 WMAddPopUpButtonItem(fsInspector->popupMenu, _("Attributes"));
285 WMAddPopUpButtonItem(fsInspector->popupMenu, _("Icon"));
286 WMAddPopUpButtonItem(fsInspector->popupMenu, _("Viewer"));
287 WMAddPopUpButtonItem(fsInspector->popupMenu, _("Editor"));
288 WMAddPopUpButtonItem(fsInspector->popupMenu, _("Executable"));
289 /* WMAddPopUpButtonItem(fsInspector->popupMenu, "File Extensions"); */
290 WMSetPopUpButtonAction(fsInspector->popupMenu, FSInspectorPopupAction,
291 fsInspector);
292 WMSetPopUpButtonSelectedItem(fsInspector->popupMenu, INIT_PANEL);
294 fsInspector->infoF = WMCreateFrame(fsInspector->mainF);
295 WMResizeWidget(fsInspector->infoF, FSI_WIDTH+10, 93);
296 WMMoveWidget(fsInspector->infoF, 0, 36+MF_Y);
297 WMSetFrameRelief(fsInspector->infoF, WRGroove);
299 fsInspector->nameLabel = WMCreateLabel(fsInspector->infoF);
300 WMResizeWidget(fsInspector->nameLabel, FSI_WIDTH-10, 55);
301 WMMoveWidget(fsInspector->nameLabel, 8+MF_X, 8);
302 WMSetLabelFont(fsInspector->nameLabel, font);
303 WMSetLabelImagePosition(fsInspector->nameLabel, WIPLeft);
304 WMSetLabelTextAlignment(fsInspector->nameLabel, WALeft);
305 WMReleaseFont(font);
307 fsInspector->pathLabel = WMCreateLabel(fsInspector->infoF);
308 WMResizeWidget(fsInspector->pathLabel, FSI_WIDTH-10, 30);
309 WMMoveWidget(fsInspector->pathLabel, 8, 60);
311 WMMapSubwidgets(fsInspector->win);
313 fsInspector->panel[AttribPanel] = InitAttribs(fsInspector->win,
314 fsInspector->fileInfo);
315 fsInspector->panel[IconPanel] = InitIcon(fsInspector->win,
316 fsInspector->fsViewer,
317 fsInspector->fileInfo,
318 0, 138);
319 fsInspector->panel[ViewPanel] = InitViewer(fsInspector->win,
320 fsInspector->fileInfo);
321 fsInspector->panel[EditPanel] = InitEditor(fsInspector->win,
322 fsInspector->fileInfo);
323 fsInspector->panel[ExePanel] = InitExecutable(fsInspector->win,
324 fsInspector->fileInfo);
325 /* fsInspector->panel[ExtnPanel] = InitExtn(fsInspector->win, */
326 /* fsInspector->fsViewer, */
327 /* fsInspector->fileInfo); */
331 void
332 FSInitInspector(FSViewer *fsViewer)
335 XWMHints *hints;
336 GNUstepWMAttributes attributes;
338 if (!(fsInspector = (FSInspector *) malloc(sizeof(FSInspector))))
340 wfatal(_("Unable to allocate memory for FSInspector %s %d\n"),
341 __FILE__, __LINE__);
343 memset(fsInspector, 0, sizeof(FSInspector));
345 fsInspector->fileInfo = FSCreateFileInfo();
346 fsInspector->fsViewer = fsViewer;
347 FSCreateInspectorWindow();
349 WMRealizeWidget(fsInspector->win);
350 FSAddWindow(fsInspector->fsViewer, WMWidgetXID(fsInspector->win));
351 fsInspector->isOpen = False;