1 /*****************************************************************************
2  ** File          : buttons.c                                               **
3  ** Purpose       : Initialise and Realise button dialogs                   **
4  ** Author        : Edward Groenendaal                                      **
5  ** Date          : April 1991                                              **
6  ** Documentation : Xdtm Design Folder                                      **
7  ** Related Files :                                                         **
8  ** Changes       : 28-11-91, Edward Groenendaal                            **
9  **                 Merged 1.8 and 2.0b to make this file                   **
10  **                 18-04-92, Edward Groenendaal                            **
11  **                 Added the #if NeedFunctionPrototypes stuff              **
12  **                 June 20, 1992, Ramon Santiago                           **
13  **                 Changed all XtCreate calls to XtVaCreate calls.         **
14  **                 Changed all caddr_t to XtPointer.                       **
15  **                 Removed some lint.                                      **
16  *****************************************************************************/
17 
18 #include "xdtm.h"
19 #include "menus.h"
20 
21 #include <stdlib.h>
22 
23 #include <X11/Shell.h>
24 #include <X11/Xaw/Label.h>
25 #include <X11/Xaw/Command.h>
26 #include <X11/Xaw/Viewport.h>
27 #include "Xedw/XedwForm.h"
28 #include "Xedw/XedwList.h"
29 
30 /* external and forward functions definitions */
31 #if NeedFunctionPrototypes
32 private int buttoniconcmp(const void *, const void *);
33 extern void copyQueryResult(Widget, Boolean, XtPointer);
34 extern String getfilename(String);
35 extern void moveQueryResult(Widget, Boolean, XtPointer);
36 extern void realize_dialog(Widget, Widget, XtGrabKind, XtEventHandler, XtPointer);
37 extern void setscroll(Widget, double);
38 extern void trashQueryResult(Widget, Boolean, XtPointer);
39 #else
40 private int buttoniconcmp();
41 extern void copyQueryResult();
42 extern String getfilename();
43 extern void moveQueryResult();
44 extern void realize_dialog();
45 extern void setscroll();
46 extern void trashQueryResult();
47 #endif
48 
49 /* Widgets */
50 
51 private Widget buttonpopup;   /* For confirmation of move/copy/trash buttons */
52 private Widget buttonform;
53 private Widget buttonlabel1;
54 private Widget buttonlabel2;
55 private Widget buttonyes;
56 private Widget buttonno;
57 private Widget fileview;
58 private Widget filelist;
59 
60 /*****************************************************************************
61  *                             init_button                                   *
62  *****************************************************************************/
init_button(top)63 public void init_button(top)
64 Widget top;
65 {
66   /* initialise the widget which display's a list of filenames with a message
67    * and a couple of buttons.
68    */
69 
70   Dimension height, space;
71   XFontStruct *font;
72 
73   buttonpopup =
74     XtVaCreatePopupShell(
75         "Trash/Move/Copy",
76 	transientShellWidgetClass,
77 	top,
78 	    NULL ) ;
79 
80   buttonform =
81     XtVaCreateManagedWidget(
82         "buttonform",
83 	xedwFormWidgetClass,
84 	buttonpopup,
85 	    NULL ) ;
86 
87   buttonlabel1 =
88     XtVaCreateManagedWidget(
89         "buttonlabel1",
90 	labelWidgetClass,
91 	buttonform,
92 	    XtNjustify, XtJustifyCenter,
93 	    XtNfullWidth,          True,
94 	    XtNborderWidth,           0,
95 	    NULL ) ;
96 
97   buttonlabel2 =
98     XtVaCreateManagedWidget(
99         "buttonlabel2",
100 	labelWidgetClass,
101 	buttonform,
102 	    XtNjustify, XtJustifyCenter,
103 	    XtNfullWidth,          True,
104 	    XtNborderWidth,           0,
105 	    XtNfromVert,   buttonlabel1,
106 	    NULL ) ;
107 
108   fileview =
109     XtVaCreateManagedWidget(
110         "fileview",
111 	viewportWidgetClass,
112 	buttonform,
113             XtNfromVert, buttonlabel2,
114 	    XtNfullWidth,        True,
115 	    XtNforceBars,        True,
116 	    XtNallowVert,        True,
117 	    NULL ) ;
118 
119   filelist =
120     XtVaCreateManagedWidget(
121         "filelist",
122 	xedwListWidgetClass,
123 	fileview,
124 	    XtNdefaultColumns,  1,
125 	    XtNforceColumns, True,
126 	    XtNrowSpacing,      4,
127 	    XtNtranslations, XtParseTranslationTable(""),
128 	    NULL ) ;
129 
130   /* Get font height from filelist, then set fileview to be 5 times that
131    * size.
132    */
133 
134   XtVaGetValues(
135       filelist,
136           XtNfont, &font,
137 	  XtNrowSpacing, &space,
138 	  NULL ) ;
139 
140   height = (font->max_bounds.ascent +
141            font->max_bounds.descent +
142 	   space) * 5;
143 
144   XtVaSetValues(
145       fileview,
146           XtNheight, height,
147 	  NULL ) ;
148 
149   buttonyes =
150     XtVaCreateManagedWidget(
151         "buttonyes",
152 	commandWidgetClass,
153 	buttonform,
154 	    XtNfromVert,       fileview,
155 	    XtNjustify, XtJustifyCenter,
156 	    NULL ) ;
157 
158   buttonno =
159     XtVaCreateManagedWidget(
160         "buttonyes",
161 	commandWidgetClass,
162 	buttonform,
163 	    XtNfromVert,       fileview,
164 	    XtNfromHoriz,     buttonyes,
165 	    XtNjustify, XtJustifyCenter,
166 	    NULL ) ;
167 
168 }
169 
170 /*****************************************************************************
171  *                              WM_destroy_button_dialog                     *
172  ****************************************************************************/
173 #if NeedFunctionPrototypes
WM_destroy_button_dialog(Widget w,XtPointer client_data,XEvent * event,Boolean * dispatch)174 private void WM_destroy_button_dialog(Widget w,
175 				      XtPointer client_data,
176 				      XEvent *event,
177 				      Boolean *dispatch)
178 #else
179 private void WM_destroy_button_dialog(w, client_data, event, dispatch)
180      Widget w;
181      XtPointer client_data;
182      XEvent *event;
183      Boolean *dispatch;
184 #endif
185 {
186     extern Atom protocols[2]; /* defined in dialogs.c */
187 
188     if (event->xclient.message_type == protocols[1] &&
189 	event->xclient.data.l[0] == protocols[0])
190     /* the widget got a kill signal */
191     {
192 	/* call callbacks on Cancel button */
193 	XtCallCallbacks(buttonno, XtNcallback, NULL);
194     }
195 }
196 
197 /*****************************************************************************
198  *                                button_dialog                              *
199  *****************************************************************************/
button_dialog(type,list)200 public void button_dialog(type, list)
201 Cardinal type;
202 XedwListReturnStruct *list;
203 {
204   /* Set the resources of the dialog to the type of dialog required,
205    * the type is either Trash, Copy or Move then popup the dialog.
206    *
207    * - Takes the type of dialog, and the list of files to display.
208    */
209 
210   XedwListReturnStruct *tmp;
211   XedwList **buttonlist;
212   Cardinal i, n;
213   private String CancelButtonLabel = "Cancel";
214 
215   /* Count items in linked list */
216   n = 0;
217   tmp = list;
218   while (tmp != NULL) {
219     tmp = tmp->next;
220     n++;
221   }
222 
223   /* Allocate an array of XedwList* of that size */
224   buttonlist = (XedwList**) XtMalloc (sizeof(XedwList*) * (n+1));
225 
226   /* Put Strings from linked list into array, using NULL icons */
227   for (i = 0; i < n; i++) {
228     buttonlist[i] = XtNew(XedwList);
229     buttonlist[i]->string = list->string;
230     list = list->next;
231   }
232   buttonlist[i] = NULL;
233 
234   /* Sort the list */
235   qsort((char*)buttonlist, n, sizeof(buttonlist[0]), buttoniconcmp);
236 
237   /* Reset view to top */
238 
239   setscroll(fileview, 0.0);
240 
241   switch (type) {
242   case Trash:
243     {
244       private String TrashTitle        = "Delete File(s)";
245       private String TrashLabel1       = "Delete these files";
246       private String TrashLabel2       = "from current directory?";
247       private String TrashButtonLabel  = "Delete";
248 
249       /* dialog title */
250 
251       XtVaSetValues(
252 	  buttonpopup,
253 	      XtNtitle, TrashTitle,
254 	      NULL ) ;
255 
256       /* label */
257 
258       XtVaSetValues(
259           buttonlabel1,
260 	      XtNlabel, TrashLabel1,
261 	      NULL ) ;
262 
263       /* label2 */
264 
265       XtVaSetValues(
266           buttonlabel2,
267 	      XtNlabel, TrashLabel2,
268 	      NULL ) ;
269 
270       /* file list */
271 
272       XtVaSetValues(
273           filelist,
274 	      XtNlongest,           0,
275 	      XtNnumberStrings,     0,
276 	      XtNxedwList, buttonlist,
277 	      NULL ) ;
278 
279       /* button1 */
280       XtVaSetValues(
281           buttonyes,
282 	      XtNlabel, TrashButtonLabel,
283 	      NULL ) ;
284       /* button2 */
285       XtVaSetValues(
286           buttonno,
287 	      XtNlabel, CancelButtonLabel,
288 	      NULL ) ;
289 
290       XtAddCallback(buttonno, XtNcallback, (XtCallbackProc)trashQueryResult,
291 		    (XtPointer)False);
292       XtAddCallback(buttonyes, XtNcallback, (XtCallbackProc)trashQueryResult,
293 		    (XtPointer)True);
294       realize_dialog(buttonpopup, NULL, XtGrabNonexclusive,
295 		     (XtEventHandler)WM_destroy_button_dialog, NULL);
296       break;
297     }
298   case Copy:
299     {
300       private String CopyTitle        = "Copy File(s)";
301       private String CopyLabel1       = "Copy these files";
302       private String CopyLabel2       = "to current directory?";
303       private String CopyButtonLabel  = "Copy";
304 
305       /* dialog title */
306 
307       XtVaSetValues(
308 	  buttonpopup,
309 	      XtNtitle, CopyTitle,
310 	      NULL ) ;
311 
312       /* label */
313 
314       XtVaSetValues(
315           buttonlabel1,
316 	      XtNlabel, CopyLabel1,
317 	      NULL ) ;
318 
319       /* label2 */
320 
321       XtVaSetValues( buttonlabel2, XtNlabel, CopyLabel2, NULL ) ;
322 
323       /* file list */
324 
325       XtVaSetValues(
326           filelist,
327 	      XtNlongest,           0,
328 	      XtNnumberStrings,     0,
329 	      XtNxedwList, buttonlist,
330 	      NULL ) ;
331 
332       /* button1 */
333       XtVaSetValues(
334           buttonyes,
335 	  XtNlabel, CopyButtonLabel,
336 	  NULL ) ;
337 
338       /* button2 */
339       XtVaSetValues(
340 	  buttonno,
341 	  XtNlabel, CancelButtonLabel,
342 	  NULL ) ;
343 
344       XtAddCallback(buttonno, XtNcallback,  (XtCallbackProc)copyQueryResult,
345 		    (XtPointer)False);
346       XtAddCallback(buttonyes, XtNcallback, (XtCallbackProc)copyQueryResult,
347 		    (XtPointer)True);
348       realize_dialog(buttonpopup, NULL, XtGrabNonexclusive,
349 		     (XtEventHandler)WM_destroy_button_dialog, NULL);
350       break;
351     }
352   case Move:
353     {
354       private String MoveTitle        = "Move File(s)";
355       private String MoveLabel1       = "Move these files";
356       private String MoveLabel2       = "to current directory?";
357       private String MoveButtonLabel  = "Move";
358 
359       /* dialog title */
360 
361       XtVaSetValues(
362 	  buttonpopup,
363 	      XtNtitle, MoveTitle,
364 	      NULL ) ;
365 
366       /* label */
367 
368       XtVaSetValues(
369           buttonlabel1,
370 	      XtNlabel, MoveLabel1,
371 	      NULL ) ;
372 
373       /* label2 */
374 
375       XtVaSetValues(
376           buttonlabel2,
377 	      XtNlabel, MoveLabel2,
378 	      NULL ) ;
379 
380       /* file list */
381 
382       XtVaSetValues(
383           filelist,
384 	      XtNlongest,           0,
385 	      XtNnumberStrings,     0,
386 	      XtNxedwList, buttonlist,
387 	      NULL ) ;
388 
389       /* button1 */
390       XtVaSetValues(
391           buttonyes,
392 	      XtNlabel, MoveButtonLabel,
393 	      NULL ) ;
394 
395       /* button2 */
396       XtVaSetValues(
397           buttonno,
398 	      XtNlabel, CancelButtonLabel,
399 	      NULL ) ;
400 
401       XtAddCallback(buttonno, XtNcallback,  (XtCallbackProc)moveQueryResult,
402 		    (XtPointer)False);
403       XtAddCallback(buttonyes, XtNcallback, (XtCallbackProc)moveQueryResult,
404 		    (XtPointer)True);
405       realize_dialog(buttonpopup, NULL, XtGrabNonexclusive,
406 		     (XtEventHandler)WM_destroy_button_dialog, NULL);
407       break;
408     }
409 
410   default:
411     fprintf(stderr, "Unrecognised button dialog request\n");
412     break;
413   }
414 
415 }
416 
417 /*****************************************************************************
418  *                             destroy_button_dialog                         *
419  *****************************************************************************/
destroy_button_dialog()420 public void destroy_button_dialog()
421 {
422   /* Popdown and destroy the callback lists of the dialog */
423   XedwList **list;
424   int i = 0;
425 
426   XtPopdown(buttonpopup);
427 
428   /* Free the allocated filelist */
429   XtVaGetValues(filelist, XtNxedwList, &list, NULL);
430   if (list) {
431       while (list[i]) XtFree((char *)list[i++]);
432       XtFree((char *)list);
433   }
434 
435   XtRemoveAllCallbacks(buttonyes, XtNcallback);
436   XtRemoveAllCallbacks(buttonno, XtNcallback);
437 
438 }
439 
440 /*****************************************************************************
441  *                               buttoniconcmp                               *
442  *****************************************************************************/
443 #if NeedFunctionPrototypes
buttoniconcmp(const void * ip1,const void * ip2)444 private int buttoniconcmp(const void *ip1, const void *ip2)
445 #else
446 private int buttoniconcmp(ip1, ip2)
447 void *ip1;
448 void *ip2;
449 #endif
450 {
451   /* compare the strings of 2 XedwList's */
452 
453   return (strcmp((*(XedwList **)ip1)->string,
454 		 (*(XedwList **)ip2)->string));
455 }
456