2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: DialogBox.c /main/9 1996/05/14 15:26:42 drk $
25 * (c) Copyright 1996 Digital Equipment Corporation.
26 * (c) Copyright 1990,1996 Hewlett-Packard Company.
27 * (c) Copyright 1996 International Business Machines Corp.
28 * (c) Copyright 1996 Sun Microsystems, Inc.
29 * (c) Copyright 1996 Novell, Inc.
30 * (c) Copyright 1996 FUJITSU LIMITED.
31 * (c) Copyright 1996 Hitachi.
34 /**---------------------------------------------------------------------
38 *** project: Motif Widgets
40 *** description: Source code for DtDialogBox class.
42 ***-------------------------------------------------------------------*/
45 /*-------------------------------------------------------------
50 #include <Xm/DialogS.h>
51 #include <Xm/PushBG.h>
52 #include <Xm/SeparatoG.h>
53 #include <Dt/DialogBoxP.h>
54 #include <Dt/MacrosP.h>
55 #include <Dt/DtMsgsP.h>
56 #include "DtWidgetI.h"
59 * Private functions borrowed from Motif.
61 extern void _XmSetThickness(Widget widget, int offset, XrmValue *value);
63 /*-------------------------------------------------------------
65 **-------------------------------------------------------------
68 extern Widget _DtCreateDialogBox(
73 extern Widget __DtCreateDialogBoxDialog(
78 extern Widget _DtDialogBoxGetButton(
81 extern Widget _DtDialogBoxGetWorkArea(
84 #define Max(x, y) (((x) > (y)) ? (x) : (y))
86 /******** Static Function Declarations ********/
88 static void ClassInitialize( void ) ;
89 static void ClassPartInitialize(
91 static void Initialize(
96 static Boolean SetValues(
100 static void ChangeManaged(
102 static void InsertChild(
104 static void DeleteChild(
106 static void ConstraintInitialize(
117 static void CreateChildren(
118 DtDialogBoxWidget new,
120 Widget bottom_widget) ;
121 static void ButtonCallback(
123 XtPointer client_data,
124 XtPointer call_data) ;
126 /******** End Static Function Declarations ********/
129 /*-------------------------------------------------------------
130 ** Forward Declarations
134 #define CENTER_POS 50
137 #define WARN_CHILD_TYPE _DtMsgDialogBox_0000
138 #define WARN_BUTTON_CHILD _DtMsgDialogBox_0001
140 extern void _DtRegisterNewConverters ();
143 /*-------------------------------------------------------------
144 ** Translations and Actions
149 /*-------------------------------------------------------------
153 /* Define offset macros.
155 #define DB_Offset(field) \
156 XtOffset (DtDialogBoxWidget, dialog_box.field)
157 #define DBC_Offset(field) \
158 XtOffset (DtDialogBoxConstraintPtr, dialog_box_constraint.field)
162 static XtResource resources[] =
166 XmCMinimizeButtons, XmRBoolean, sizeof (Boolean),
167 DB_Offset (minimize_buttons), XmRImmediate, (XtPointer) False
171 XmCButtonCount, XmRCardinal, sizeof (Cardinal),
172 DB_Offset (button_count), XmRImmediate, (XtPointer) 4
175 XmNbuttonLabelStrings,
176 XmCButtonLabelStrings, XmRXmStringTable, sizeof (XtPointer),
177 DB_Offset (button_label_strings), XmRStringTable, NULL
181 XmCCallback, XmRCallback, sizeof (XtCallbackList),
182 DB_Offset (callback), XmRImmediate, (XtPointer) NULL
186 XmCShadowThickness, XmRHorizontalDimension, sizeof(Dimension),
187 XtOffsetOf (struct _XmManagerRec, manager.shadow_thickness),
188 XmRCallProc, (XtPointer) _XmSetThickness
192 /* Constraint Resources
194 static XtResource constraints[] =
198 XmCChildType, XmRChildType, sizeof (unsigned char),
199 DBC_Offset (child_type), XmRImmediate, (XtPointer) XmWORK_AREA
208 /*-------------------------------------------------------------
211 DtDialogBoxClassRec dtDialogBoxClassRec =
216 (WidgetClass) &xmFormClassRec, /* superclass */
217 "DtDialogBox", /* class_name */
218 sizeof (DtDialogBoxRec), /* widget_size */
219 ClassInitialize, /* class_initialize */
220 ClassPartInitialize, /* class_part_initialize*/
221 False, /* class_inited */
222 (XtInitProc) Initialize, /* initialize */
223 NULL, /* initialize_hook */
224 XtInheritRealize, /* realize */
227 resources, /* resources */
228 XtNumber (resources), /* num_resources */
229 NULLQUARK, /* xrm_class */
230 True, /* compress_motion */
231 False, /* compress_exposure */
232 True, /* compress_enterleave */
233 False, /* visible_interest */
234 Destroy, /* destroy */
235 XtInheritResize, /* resize */
236 XtInheritExpose, /* expose */
237 (XtSetValuesFunc) SetValues, /* set_values */
238 NULL, /* set_values_hook */
239 XtInheritSetValuesAlmost, /* set_values_almost */
240 NULL, /* get_values_hook */
241 NULL, /* accept_focus */
242 XtVersion, /* version */
243 NULL, /* callback private */
244 XtInheritTranslations, /* tm_table */
245 XtInheritQueryGeometry, /* query_geometry */
246 NULL, /* display_accelerator */
247 NULL, /* extension */
253 XtInheritGeometryManager, /* geometry_manager */
254 XtInheritChangeManaged, /* change_managed */
255 InsertChild, /* insert_child */
256 DeleteChild, /* delete_child */
257 NULL, /* extension */
263 constraints, /* constraint_resources */
264 XtNumber (constraints), /* num_constraint_resources */
265 sizeof (DtDialogBoxConstraintRec),/* constraint_record */
266 (XtInitProc) ConstraintInitialize,/* constraint_initialize */
267 NULL, /* constraint_destroy */
268 NULL, /* constraint_set_values */
269 NULL, /* extension */
275 XtInheritTranslations, /* default_translations */
276 NULL, /* get_resources */
277 0, /* num_get_resources */
278 NULL, /* get_cont_resources */
279 0, /* num_get_cont_resources */
280 XmInheritParentProcess, /* parent_process */
281 NULL, /* extension */
284 /* XmBulletinBoard Part
287 True, /* always_install_accelerators*/
288 NULL, /* geo_matrix_create */
289 XmInheritFocusMovedProc, /* focus_moved_proc */
290 NULL, /* extension */
296 NULL, /* extension */
302 (XtWidgetProc) CreateChildren, /* create_children */
303 NULL, /* configure_children */
304 GetSize, /* get_size */
305 NULL, /* button_callback */
306 NULL, /* extension */
311 WidgetClass dtDialogBoxWidgetClass = (WidgetClass) &dtDialogBoxClassRec;
315 /*-------------------------------------------------------------
317 **-------------------------------------------------------------
321 /*-------------------------------------------------------------
323 **-------------------------------------------------------------
327 /*-------------------------------------------------------------
329 **-------------------------------------------------------------
332 /*-------------------------------------------------------------
334 ** Initialize widget class.
337 ClassInitialize( void )
339 _DtRegisterNewConverters ();
344 /*-------------------------------------------------------------
345 ** ClassPartInitialize
346 ** Initialize widget class part.
352 DtDialogBoxWidgetClass mc = (DtDialogBoxWidgetClass) wc;
353 DtDialogBoxWidgetClass super = (DtDialogBoxWidgetClass)
356 mc->bulletin_board_class.always_install_accelerators = True;
358 /* Propagate class procs for subclasses.
360 if (C_CreateChildren (mc) == (XtWidgetProc) _XtInherit)
361 C_CreateChildren (mc) = C_CreateChildren (super);
363 if (C_ConfigureChildren (mc) == (XtWidgetProc) _XtInherit)
364 C_ConfigureChildren (mc) = C_ConfigureChildren (super);
366 if (C_GetSize (mc) == (DlgGetSizeProc) _XtInherit)
367 C_GetSize (mc) = C_GetSize (super);
369 if (C_ButtonCallback (mc) == (XtCallbackProc) _XtInherit)
370 C_ButtonCallback (mc) = C_ButtonCallback (super);
376 /*-------------------------------------------------------------
378 ** Initialize a new widget instance.
385 DtDialogBoxWidget new = (DtDialogBoxWidget) new_w;
386 XtWidgetProc create_children;
388 if (M_ButtonCount (new) > 0)
389 M_Button (new) = (Widget *)
390 XtMalloc (sizeof (Widget) * M_ButtonCount (new));
392 M_Button (new) = NULL;
394 M_WorkArea (new) = NULL;
396 create_children = C_CreateChildren((DtDialogBoxWidgetClass) XtClass(new));
398 (*create_children) ((Widget)new);
400 M_ButtonLabelStrings (new) = NULL;
402 M_MarginWidth (new) = M_ShadowThickness (new);
403 M_MarginHeight (new) = M_ShadowThickness (new);
407 /*-------------------------------------------------------------
409 ** Release resources allocated for widget instance.
415 DtDialogBoxWidget mgr = (DtDialogBoxWidget) w;
417 /* Free button pointers.
419 if (M_Button (mgr) != NULL)
420 XtFree ((char *)M_Button (mgr));
425 /*-------------------------------------------------------------
427 ** Handle changes in resource data.
435 Boolean redraw_flag = False;
437 /* Superclass does all the work so far; checks for redisplay, etc.
440 return (redraw_flag);
445 /*-------------------------------------------------------------
447 **-------------------------------------------------------------
450 /*-------------------------------------------------------------
452 ** Handle change in set of managed children.
458 DtDialogBoxWidget mgr = (DtDialogBoxWidget) manager;
459 Dimension w = M_Width (mgr),
461 DlgGetSizeProc get_size;
463 /* Compute desired size.
466 get_size = C_GetSize((DtDialogBoxWidgetClass) XtClass(mgr));
467 resize = XtCoreProc(manager, resize);
469 (*get_size) ((Widget)mgr, 0, 0, 0, 0, &w, &h);
471 /* check for resize policy if not first time ?? */
473 /* Try to change size to fit children
475 if (w != M_Width (mgr) || h != M_Height (mgr))
477 switch (XtMakeResizeRequest ((Widget) mgr, w, h, &w, &h))
479 case XtGeometryAlmost:
480 XtMakeResizeRequest ((Widget) mgr, w, h,
489 /* Set positions and sizes of children.
496 /*-------------------------------------------------------------
504 DtDialogBoxConstraint constraint = (DtDialogBoxConstraint)
505 M_DialogBoxConstraint (child);
506 DtDialogBoxWidget w = (DtDialogBoxWidget) XtParent (child);
507 XmManagerWidgetClass mc = (XmManagerWidgetClass)xmManagerWidgetClass;
508 Dimension s_t = M_ShadowThickness (w);
509 Arg al[20]; /* arg list */
510 register int ac; /* arg count */
511 XtWidgetProc insert_child;
514 insert_child = mc->composite_class.insert_child;
516 (*insert_child) (child);
518 if (constraint->child_type == XmWORK_AREA)
520 if (! M_WorkArea (w))
522 M_WorkArea (w) = child;
524 XtSetArg (al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
525 XtSetArg (al[ac], XmNtopOffset, s_t); ac++;
526 XtSetArg (al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
527 XtSetArg (al[ac], XmNleftOffset, s_t); ac++;
528 XtSetArg (al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
529 XtSetArg (al[ac], XmNrightOffset, s_t); ac++;
530 XtSetArg (al[ac], XmNbottomAttachment, XmATTACH_WIDGET); ac++;
531 XtSetArg (al[ac], XmNbottomWidget, M_Separator (w)); ac++;
532 XtSetArg (al[ac], XmNbottomOffset, 0); ac++;
533 XtSetValues (child, al, ac);
540 /*-------------------------------------------------------------
548 DtDialogBoxWidget w = (DtDialogBoxWidget) XtParent (child);
549 XmManagerWidgetClass mc = (XmManagerWidgetClass)xmManagerWidgetClass;
550 XtWidgetProc delete_child;
553 delete_child = mc->composite_class.delete_child;
555 (*delete_child) (child);
557 if (child == M_Separator (w))
558 M_Separator (w) = NULL;
559 else if (child == M_WorkArea (w))
560 M_WorkArea (w) = NULL;
562 /* button children ?? */
567 /*-------------------------------------------------------------
569 **-------------------------------------------------------------
571 /*-------------------------------------------------------------
572 ** ConstraintInitialize
576 ConstraintInitialize(
580 DtDialogBoxWidget mgr = (DtDialogBoxWidget) XtParent (new);
581 DtDialogBoxConstraint constraint = M_DialogBoxConstraint (new);
583 /* Validate child type.
585 if (constraint->child_type != XmWORK_AREA &&
586 constraint->child_type != XmSEPARATOR &&
587 constraint->child_type != XmBUTTON)
589 XmeWarning (new, WARN_CHILD_TYPE);
590 if (! M_WorkArea (mgr))
592 constraint->child_type = XmWORK_AREA;
598 /*-------------------------------------------------------------
600 **-------------------------------------------------------------
603 /* All inherited from superclass.
608 /*-------------------------------------------------------------
610 **-------------------------------------------------------------
613 /*-------------------------------------------------------------------------
615 ** Calculate desired size based on children.
627 DtDialogBoxWidget mgr = (DtDialogBoxWidget) manager;
628 Widget work_area = M_WorkArea (mgr),
629 separator = M_Separator (mgr);
630 Widget * button = M_Button (mgr);
631 register int button_count = M_ButtonCount (mgr),
633 Boolean min_btns = M_MinimizeButtons (mgr);
635 offset = OFFSET, pad,
636 max_w = 0, max_h = 0,
637 s_t = M_ShadowThickness (mgr);
638 XtWidgetGeometry request, reply;
643 if (work_area && !wa_w && !wa_h)
645 request.request_mode = CWWidth | CWHeight;
646 XtQueryGeometry (work_area, &request, &reply);
651 sep_h = (separator) ? P_Height (separator) : 0;
653 /* Get button panel size.
656 if ((bp_w == 0) && (bp_h == 0) && !min_btns)
658 request.request_mode = CWWidth | CWHeight;
659 for (i = 0; i < button_count; i++)
661 pad = (i == 0) ? 0 : 4 * s_t;
662 XtQueryGeometry (button[i], &request, &reply);
663 max_w = Max ((unsigned)max_w, reply.width + pad);
664 max_h = Max ((unsigned)max_h, reply.height + pad);
673 bp_w = (button_count * max_w) + ((button_count + 1) * offset);
674 bp_h = sep_h + max_h + (2 * offset);
677 else if ((bp_w == 0) && (bp_h == 0) && min_btns)
679 request.request_mode = CWWidth | CWHeight;
680 for (i = 0; i < button_count; i++)
682 XtQueryGeometry (button[i], &request, &reply);
684 max_h = Max (max_h, reply.height);
687 bp_w += (button_count + 1) * offset;
688 bp_h = sep_h + max_h + (2 * offset);
691 /* Set width and height.
693 *w = Max (wa_w, bp_w);
694 *h = wa_h + sep_h + bp_h;
699 /*-------------------------------------------------------------
701 ** Create resource and value labels and text.
705 DtDialogBoxWidget new,
707 Widget bottom_widget )
711 Widget * button = M_Button (new);
712 int count = M_ButtonCount (new),
713 i, l_p, r_p, off_p, b_delta, offset = 0;
714 XmStringTable string = M_ButtonLabelStrings (new);
715 Boolean min_btns = M_MinimizeButtons (new);
716 Dimension s_t = M_ShadowThickness (new);
717 char button_name[100];
719 Arg al[20]; /* arg list */
720 register int ac; /* arg count */
722 /* Compute position factors.
725 b_delta = (100 - off_p) / count;
726 l_p = (100 - (count * b_delta) + off_p) / 2;
727 r_p = l_p + b_delta - off_p;
728 /* s_t = Max (2, s_t); */
732 for (i = 0; i < count; i++)
735 XtSetArg (al[ac], XmNdefaultButtonShadowThickness,Max(2U,s_t)/2U);
737 XtSetArg (al[ac], XmNshadowThickness, s_t); ac++;
738 XtSetArg (al[ac], XmNhighlightThickness, s_t); ac++;
741 XtSetArg (al[ac], XmNleftAttachment,
742 XmATTACH_POSITION); ac++;
743 XtSetArg (al[ac], XmNleftPosition, l_p); ac++;
744 XtSetArg (al[ac], XmNleftOffset, offset); ac++;
745 XtSetArg (al[ac], XmNrightAttachment,
746 XmATTACH_POSITION); ac++;
747 XtSetArg (al[ac], XmNrightPosition, r_p); ac++;
748 XtSetArg (al[ac], XmNrightOffset, offset); ac++;
754 XtSetArg (al[ac], XmNleftAttachment,
755 XmATTACH_FORM); ac++;
756 XtSetArg (al[ac], XmNleftOffset, OFFSET); ac++;
758 else if (i == count -1)
760 XtSetArg (al[ac], XmNrightAttachment,
761 XmATTACH_FORM); ac++;
762 XtSetArg (al[ac], XmNrightOffset, OFFSET); ac++;
766 XtSetArg (al[ac], XmNleftAttachment,
767 XmATTACH_WIDGET); ac++;
768 XtSetArg (al[ac], XmNleftWidget, button[i-1]);
770 XtSetArg (al[ac], XmNleftOffset, OFFSET);
775 XtSetArg (al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
776 XtSetArg (al[ac], XmNbottomOffset, OFFSET + offset); ac++;
779 XtSetArg (al[ac], XmNlabelString, string[i]); ac++;
781 XtSetArg (al[ac], XmNchildType, XmBUTTON); ac++;
782 sprintf (button_name, "%s_%d", M_Name (new), i+1);
783 btn = XmCreatePushButtonGadget ((Widget)new, button_name, al, ac);
785 XtAddCallback (btn, XmNactivateCallback,
786 (XtCallbackProc)ButtonCallback,
787 (XtPointer)((long)(i+1)));
794 ref_widget = button[0];
795 new->bulletin_board.default_button = button[0];
800 XtSetArg (al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
801 XtSetArg (al[ac], XmNleftOffset, 0); ac++;
802 XtSetArg (al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
803 XtSetArg (al[ac], XmNrightOffset, 0); ac++;
804 XtSetArg (al[ac], XmNbottomAttachment, XmATTACH_WIDGET); ac++;
805 XtSetArg (al[ac], XmNbottomWidget, ref_widget); ac++;
806 XtSetArg (al[ac], XmNbottomOffset, OFFSET); ac++;
807 XtSetArg (al[ac], XmNhighlightThickness, 0); ac++;
808 XtSetArg (al[ac], XmNchildType, XmSEPARATOR); ac++;
809 sep = XmCreateSeparatorGadget ((Widget)new, "separator", al, ac);
811 M_Separator (new) = sep;
815 /*-------------------------------------------------------------
821 XtPointer client_data,
822 XtPointer call_data )
824 DtDialogBoxWidget mgr = (DtDialogBoxWidget) XtParent (g);
825 XtCallbackList cb_list = M_Callback (mgr);
826 XmAnyCallbackStruct * b_cb_data =
827 (XmAnyCallbackStruct *) call_data;
828 DtDialogBoxCallbackStruct cb_data;
832 cb_data.reason = XmCR_DIALOG_BUTTON;
833 cb_data.event = b_cb_data->event;
834 cb_data.button_position = (int) client_data;
836 XtCallCallbackList ((Widget) mgr, cb_list, &cb_data);
842 /*-------------------------------------------------------------
843 ** Public Entry Points
844 **-------------------------------------------------------------
847 /*-------------------------------------------------------------
848 ** _DtCreateDialogBox
849 ** Create a new DtDialogBox instance.
850 **-------------------------------------------------------------
859 return (XtCreateWidget (name, dtDialogBoxWidgetClass,
860 parent, arglist, argcount));
864 /*-------------------------------------------------------------
865 ** __DtCreateDialogBoxDialog
866 ** Create a DialogShell and a new DialogBox instance;
867 ** return the DialogBox widget;
868 **-------------------------------------------------------------
871 __DtCreateDialogBoxDialog(
877 Widget ds; /* DialogShell */
878 Arg ds_args[10]; /* arglist for shell */
879 Cardinal ds_n; /* argcount for shell */
880 Widget db; /* new sb widget */
883 /* create DialogShell parent
886 XtSetArg (ds_args[ds_n], XmNallowShellResize, True); ds_n++;
887 ds = XtCreatePopupShell (name, xmDialogShellWidgetClass,
888 ds_p, ds_args, ds_n);
890 /* create DialogBox, free args, return
892 db = XtCreateWidget (name, dtDialogBoxWidgetClass,
899 /*-------------------------------------------------------------
900 ** _DtDialogBoxGetButton
901 ** Return DialogBox button.
902 **-------------------------------------------------------------
905 _DtDialogBoxGetButton(
909 DtDialogBoxWidget mgr = (DtDialogBoxWidget) w;
911 Widget * button = M_Button (mgr);
915 if (index >= M_ButtonCount (mgr))
916 XmeWarning ((Widget)mgr, WARN_BUTTON_CHILD);
918 rtn_w = button[index];
924 /*-------------------------------------------------------------
925 ** _DtDialogBoxGetWorkArea
926 ** Return DialogBox work area;
927 **-------------------------------------------------------------
930 _DtDialogBoxGetWorkArea(
933 DtDialogBoxWidget mgr = (DtDialogBoxWidget) w;
934 Widget rtn_w = M_WorkArea (mgr);