Debian Codegraph @

demos/unsupported/aicon/aicon.c

raw
1 /* $XConsortium: aicon.c /main/5 1995/07/15 20:47:16 drk $ */
2 /*
3 * Motif
4 *
5 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
6 *
7 * These libraries and programs are free software; you can
8 * redistribute them and/or modify them under the terms of the GNU
9 * Lesser General Public License as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * These libraries and programs are distributed in the hope that
14 * they will be useful, but WITHOUT ANY WARRANTY; without even the
15 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU Lesser General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with these librararies and programs; if not, write
21 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
22 * Floor, Boston, MA 02110-1301 USA
23 *
24 */
25 /*
26 * HISTORY
27 */
28 #include <stdlib.h>
29 #include <Xm/XmAll.h>
30 #include <Xmd/AdjView.h>
31 #include <Xmd/RegEdit.h>
32 #include <Xmd/Help.h>
33 #ifdef SYS_DIR
34 #include <sys/dir.h>
35 #else
36 #ifdef NDIR
37 #include <ndir.h>
38 #else
39 #include <dirent.h>
40 #endif
41 #endif
42
43 #define APP_CLASS "XmdAIcon"
44
45
46
47 typedef struct {
48 String icon_dir;
49 String valid_extensions;
50 } ApplicationData, *ApplicationDataPtr;
51 static ApplicationData AppData;
52
53 /* no need for a prefix, this is not exported */
54 #define NiconDir "iconDir"
55 #define CIconDir "IconDir"
56 #define NvalidExtensions "validExtensions"
57 #define CValidExtensions "ValidExtensions"
58
59 static XtResource resources[] = {
60 { NiconDir, CIconDir, XmRString, sizeof(String),
61 XtOffset(ApplicationDataPtr, icon_dir), XmRString, "." },
62 { NvalidExtensions, CValidExtensions, XmRString, sizeof(String),
63 XtOffset(ApplicationDataPtr, valid_extensions), XmRString,
64 "xbm,xpm,bm,pm" },
65 };
66
67 /*
68 * Fallbacks: keep it minimum
69 */
70 static String fallbacks[] = {
71 "*help_manager.helpFile: aicon",
72 "aicon.geometry:700x500",
73 NULL
74 };
75
76
77 static void
78 HelpCB (Widget w, XtPointer client_data, XtPointer call_data)
79 {
80 static Widget help_widget = NULL ;
81
82 if (!help_widget)
83 help_widget = XmdCreateHelpDialog(w, "help_manager", NULL, 0);
84
85 XtManageChild(help_widget);
86 }
87
88 static void
89 DefCB(
90 Widget widget,
91 XtPointer tag,
92 XtPointer data)
93 {
94 XmContainerSelectCallbackStruct *cbs =
95 (XmContainerSelectCallbackStruct*)data;
96 Pixmap pix ;
97 Window root;
98 int x,y;
99 unsigned int loc_width, loc_height, border_width, loc_depth;
100
101 XtVaGetValues(cbs->selected_items[0], XmNlargeIconPixmap, &pix, NULL);
102 if (pix != XmUNSPECIFIED_PIXMAP) {
103 XGetGeometry(XtDisplay(widget), pix, &root, &x, &y,
104 &loc_width, &loc_height, &border_width, &loc_depth);
105 if (loc_depth == DefaultDepthOfScreen(XtScreen(widget))) {
106 XSetWindowBackgroundPixmap(XtDisplay(widget),
107 DefaultRootWindow(XtDisplay(widget)),
108 pix);
109 XClearWindow(XtDisplay(widget),
110 DefaultRootWindow(XtDisplay(widget)));
111 }
112 }
113
114 }
115 static void
116 AppModalDialogWarning (w, str)
117 Widget w;
118 String str;
119 {
120 static Widget warning_dialog = NULL ;
121 XmString message_string ;
122
123 if (!warning_dialog) {
124 warning_dialog = XmCreateWarningDialog (w, "Huh ho", NULL, 0);
125 XtAddCallback (warning_dialog, XmNhelpCallback, HelpCB, NULL);
126 }
127
128 message_string = XmStringGenerate((XtPointer) str,
129 NULL, XmCHARSET_TEXT, NULL);
130 XtVaSetValues(warning_dialog, XmNmessageString, message_string, NULL);
131 XmStringFree(message_string);
132
133 XtManageChild(warning_dialog);
134 }
135
136
137
138
139 static void
140 PopulateContainer(cont, dir)
141 Widget cont ;
142 String dir ;
143 {
144 char icon_fullname[256] ;
145 #if defined (SYS_DIR) || defined (NDIR)
146 struct direct *item; /* entry in directory */
147 #else
148 struct dirent *item; /* entry in directory */
149 #endif
150 static Widget * icong = NULL ;
151 static int n_icong = 0;
152 Cardinal num_children, i;
153 Widget * children ;
154 int dir_len ;
155 DIR *dirp;
156
157 dir_len = strlen(dir) ;
158 /* let's remove the FSB added * pattern */
159 if (dir[dir_len-1] == '*') dir[--dir_len] = '\0';
160
161 /* try to open the icons directory */
162 if (!(dirp = opendir (dir))) {
163 char s[256] ;
164 sprintf(s, "Cannot open iconDir: %s\n", dir);
165 AppModalDialogWarning(cont, s);
166 return;
167 }
168
169 /* first destroy current set of icons (if any) */
170 XtVaGetValues(cont, XmNnumChildren, &num_children,
171 XmNchildren, &children, NULL);
172 XtUnmanageChildren(children, num_children);
173 for (i=0; i < num_children; i++) XtDestroyWidget(children[i]);
174
175 strcpy(icon_fullname, dir);
176 icon_fullname[dir_len] = '/' ;
177
178 /* turn Container (back) to CELLS. Also ask for natural cell sizes */
179 XtVaSetValues (cont, XmNspatialStyle, XmCELLS,
180 XmNlargeCellWidth, 0, XmNlargeCellHeight, 0, NULL);
181
182 /* read one entry each time through the loop */
183 for (item = readdir (dirp), i = 0; item != NULL;
184 item = readdir (dirp)) {
185
186 /* reset the fullname every time */
187 icon_fullname[dir_len+1] = '\0' ;
188 strcat(icon_fullname, item->d_name);
189
190 if ((!strcmp (item->d_name, ".")) ||
191 (!strcmp (item->d_name, "..")))
192 continue ;
193
194 /* check the extension only if ALL is not part of it */
195 if (!strstr(AppData.valid_extensions, "ALL")) {
196 /* the extension is defined by anything after the last . */
197 String extension = strrchr(item->d_name, '.');
198 /* if no . or . is last char of the file name, invalid */
199 if (!extension || extension[1] == '\0') continue ;
200 /* if the extension is in not the list, invalid */
201 if (!strstr(AppData.valid_extensions, extension+1)) continue;
202 }
203
204 if (i >= n_icong) {
205 n_icong += 500 ;
206 icong = (Widget*)XtRealloc((char*)icong,
207 n_icong * sizeof(Widget*));
208 }
209 icong[i] = XtVaCreateWidget(item->d_name, xmIconGadgetClass, cont,
210 XtVaTypedArg, XmNlargeIconPixmap, XmRString,
211 icon_fullname, strlen(icon_fullname)+1, NULL);
212 i++;
213 }
214
215 XtManageChildren(icong, i);
216
217 /* turn Container back to NONE */
218 XtVaSetValues (cont, XmNspatialStyle, XmNONE, NULL);
219 }
220
221
222 static void QuitCB (w, client_data, call_data)
223 Widget w; /* widget id */
224 XtPointer client_data; /* data from application */
225 XtPointer call_data; /* data from widget class */
226 {
227 exit(0);
228 }
229 static void UnMapCB (w, client_data, call_data)
230 Widget w; /* widget id */
231 XtPointer client_data; /* data from application */
232 XtPointer call_data; /* data from widget class */
233 {
234 XtUnmanageChild(w);
235 }
236
237 static void OKCB (w, client_data, call_data)
238 Widget w; /* widget id */
239 XtPointer client_data; /* data from application */
240 XtPointer call_data; /* data from widget class */
241 {
242 Widget cont = *(Widget*)client_data ;
243 String dir ;
244 unsigned char path_mode ;
245
246 /* in CDE mode, the FilterText is useless, we have to look at the
247 Directory text */
248 XtVaGetValues(w, XmNpathMode, &path_mode, NULL);
249 if (path_mode != XmPATH_MODE_RELATIVE)
250 dir = XmTextGetString(XtNameToWidget(w, "FilterText"));
251 else
252 dir = XmTextGetString(XtNameToWidget(w, "DirText"));
253
254 PopulateContainer(cont, dir);
255 }
256
257 static void OpenCB (w, client_data, call_data)
258 Widget w; /* widget id */
259 XtPointer client_data; /* data from application */
260 XtPointer call_data; /* data from widget class */
261 {
262 static Widget fsb_box = NULL ;
263
264 if (!fsb_box) {
265 Arg args[2];
266 Cardinal n;
267 XmString dir ;
268
269 dir = XmStringGenerate((XtPointer) AppData.icon_dir,
270 NULL, XmCHARSET_TEXT, NULL);
271 n = 0;
272 XtSetArg (args[n], XmNdirectory, dir); n++;
273 fsb_box = XmCreateFileSelectionDialog (w, "Open Icon Dir",
274 args, n);
275 XtAddCallback (fsb_box, XmNhelpCallback, HelpCB, NULL);
276 XmStringFree(dir);
277 XtAddCallback (fsb_box, XmNokCallback, OKCB, client_data);
278 XtAddCallback (fsb_box, XmNcancelCallback, UnMapCB, client_data);
279 XtUnmanageChild(XtNameToWidget(fsb_box, "Selection"));
280 XtUnmanageChild(XtNameToWidget(fsb_box, "Text"));
281 }
282
283 XtManageChild (fsb_box);
284 }
285
286 static Widget CreateMainWindowWithMenuBar(toplevel, client_data)
287 Widget toplevel ;
288 XtPointer client_data ;
289 {
290 Widget main_window, menu_bar, menu_pane, cascade, button ;
291
292 Arg args[20];
293 Cardinal n;
294
295
296 /* Create MainWindow.
297 */
298 n = 0;
299 main_window = XmCreateMainWindow (toplevel, "main_window", args, n);
300 XtManageChild (main_window);
301
302
303 /* Create MenuBar in MainWindow.
304 */
305 n = 0;
306 menu_bar = XmCreateMenuBar (main_window, "menu_bar", args, n);
307 XtManageChild (menu_bar);
308
309 /* Create "File" PulldownMenu.
310 */
311 n = 0;
312 menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, n);
313
314 n = 0;
315 button = XmCreatePushButton (menu_pane, "Open...", args, n);
316 XtManageChild (button);
317 XtAddCallback (button, XmNactivateCallback, OpenCB, client_data);
318 n = 0;
319 button = XmCreatePushButton (menu_pane, "Exit", args, n);
320 XtManageChild (button);
321 XtAddCallback (button, XmNactivateCallback, QuitCB, NULL);
322
323 n = 0;
324 XtSetArg (args[n], XmNsubMenuId, menu_pane); n++;
325 cascade = XmCreateCascadeButton (menu_bar, "File", args, n);
326 XtManageChild (cascade);
327
328
329 /* Create "Help" PulldownMenu.
330 */
331 n = 0;
332 menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane3", args, n);
333
334 n = 0;
335 button = XmCreatePushButton (menu_pane, "Overview", args, n);
336 XtManageChild (button);
337 XtAddCallback (button, XmNactivateCallback, HelpCB, NULL);
338
339 n = 0;
340 XtSetArg (args[n], XmNsubMenuId, menu_pane); n++;
341 cascade = XmCreateCascadeButton (menu_bar, "Help", args, n);
342 XtManageChild (cascade);
343 n = 0;
344 XtSetArg (args[n], XmNmenuHelpWidget, cascade); n++;
345 XtSetValues (menu_bar, args, n);
346
347 return main_window ;
348 }
349
350
351
352
353 int
354 main(int argc, char **argv)
355 {
356
357 XtAppContext app_context;
358 Widget toplevel, sw, cont, mainw ;
359
360 XtSetLanguageProc(NULL, NULL, NULL);
361 toplevel = XtVaAppInitialize(&app_context, APP_CLASS, NULL, 0,
362 &argc, argv, fallbacks, NULL);
363
364 XtGetApplicationResources(toplevel,
365 (XtPointer)&AppData,
366 resources,
367 XtNumber(resources),
368 NULL,
369 0);
370
371 XmdRegisterEditres(toplevel) ;
372
373 mainw = CreateMainWindowWithMenuBar(toplevel, &cont);
374 XtManageChild(mainw);
375
376 /* we want a scrolledwindow because the container window
377 can potentially be huge */
378 sw = XtVaCreateManagedWidget("sw", xmScrolledWindowWidgetClass, mainw,
379 XmNscrollingPolicy, XmAUTOMATIC, NULL);
380
381 cont = XtVaCreateManagedWidget("cont", xmContainerWidgetClass, sw,
382 XmNspatialResizeModel, XmGROW_BALANCED, NULL);
383 XtAddCallback(cont, XmNdefaultActionCallback, DefCB, NULL);
384
385 /* setup our clipwindow/workwindow constraint stuff */
386 XmdSWAdjustView (sw) ;
387
388 XtRealizeWidget(toplevel);
389
390 PopulateContainer(cont, AppData.icon_dir);
391
392 XtAppMainLoop(app_context);
393
394 return 0; /* make compiler happy */
395 }