1 /*
2   $Header: /cvsroot/lesstif/lesstif/test/Xm/misc/test8.c,v 1.4 2002/04/17 16:22:01 amai Exp $
3 ** accelerators
4 */
5 
6 #include <stdlib.h>
7 #include <stdio.h>
8 
9 #include <X11/Xatom.h>
10 #include <X11/Intrinsic.h>
11 #include <X11/Shell.h>
12 #include <X11/keysym.h>
13 
14 #include <Xm/Xm.h>
15 #include <Xm/DialogS.h>
16 #include <Xm/Form.h>
17 #include <Xm/Label.h>
18 #include <Xm/MessageB.h>
19 #include <Xm/PushB.h>
20 #include <Xm/RowColumn.h>
21 #include <Xm/TextF.h>
22 #include <Xm/SeparatoG.h>
23 #include <Xm/ToggleBG.h>
24 #include <stdio.h>
25 
26 Widget shell = (Widget) NULL;
27 Widget quitb = (Widget) NULL;
28 
29 /* Structure used to pass details of the accelerator to the event handler */
30 typedef struct {
31 	KeyCode         keycode;
32 	Modifiers       modifiers;
33 	Widget          pushbutton;
34 }               accel_key_t, *accel_key_p;
35 
36 void            handle_accel();
37 
38 void
install_accelerator(shell,keysym,modifiers,pushbutton)39 install_accelerator(shell, keysym, modifiers, pushbutton)
40 	Widget          shell;
41 	KeySym          keysym;
42 	Modifiers       modifiers;
43 	Widget          pushbutton;
44 {
45 	KeyCode         keycode;
46 	accel_key_p     accel;
47 
48 	/* Convert keysym to keycode and set up passive grab on shell */
49 	keycode = XKeysymToKeycode(XtDisplay(shell), keysym);
50 	if (keycode == 0)
51 		return;		/* with error message */
52 	XtGrabKey(shell, keycode, modifiers, False, GrabModeAsync,
53 		  GrabModeAsync);
54 
55 	/* Set up event handler on shell */
56 	accel = (accel_key_p) XtMalloc(sizeof(accel_key_t));
57 	accel->keycode = keycode;
58 	accel->modifiers = modifiers;
59 	accel->pushbutton = pushbutton;
60 	XtAddEventHandler(shell, KeyPressMask, False, handle_accel,
61 			  (XtPointer) accel);
62 }
63 
64 void
handle_accel(widget,client_data,event,cont)65 handle_accel(widget, client_data, event, cont)
66 	Widget          widget;
67 	XtPointer       client_data;
68 	XEvent         *event;
69 	Boolean        *cont;
70 {
71 	/* Check that we have the right key combination */
72 	accel_key_p     accel = (accel_key_p) client_data;
73 	if ((event->xkey.state == accel->modifiers)
74 	    && (event->xkey.keycode == accel->keycode))
75 		XtCallActionProc(accel->pushbutton, "ArmAndActivate", event,
76 				 NULL, 0);
77 }
78 
create_shell(display,app_name,app_argc,app_argv)79 void create_shell (display, app_name, app_argc, app_argv)
80 Display *display;
81 char *app_name;
82 int app_argc;
83 char **app_argv;
84 {
85 	Widget template = (Widget) NULL;
86 	Widget sep = (Widget) NULL;
87 	Widget form = (Widget) NULL;
88 	Widget radiob = (Widget) NULL;
89 	Widget radio_1 = (Widget) NULL;
90 	Widget radio_2 = (Widget) NULL;
91 	Widget radio_3 = (Widget) NULL;
92 	Widget label_1 = (Widget) NULL;
93 	Widget text = (Widget) NULL;
94 	Widget label_2 = (Widget) NULL;
95 	Widget okb = (Widget) NULL;
96 	Widget children[4];      /* Children to manage */
97 	Arg al[64];                    /* Arg List */
98 	register int ac = 0;           /* Arg Count */
99 	XmString xmstring;
100 
101 	/* Create a pretty standard sort of dialog */
102 	XtSetArg(al[ac], XmNallowShellResize, TRUE); ac++;
103 	XtSetArg(al[ac], XmNtitle, "Keyboard Accelerators"); ac++;
104 	XtSetArg(al[ac], XmNargc, app_argc); ac++;
105 	XtSetArg(al[ac], XmNargv, app_argv); ac++;
106 	shell = XtAppCreateShell ( app_name, "XApplication", applicationShellWidgetClass, display, al, ac );
107 	ac = 0;
108 	XtSetArg(al[ac], XmNautoUnmanage, FALSE); ac++;
109 	XtSetArg(al[ac], XmNdialogType, XmDIALOG_TEMPLATE); ac++;
110 	template = XmCreateMessageBox ( shell, "template", al, ac );
111 	ac = 0;
112 	sep = XmMessageBoxGetChild ( template, XmDIALOG_SEPARATOR );
113 	XtSetArg(al[ac], XmNautoUnmanage, FALSE); ac++;
114 	XtSetArg(al[ac], XmNhorizontalSpacing, 5); ac++;
115 	XtSetArg(al[ac], XmNverticalSpacing, 5); ac++;
116 	form = XmCreateForm ( template, "form", al, ac );
117 	ac = 0;
118 	radiob = XmCreateRadioBox ( form, "radiob", al, ac );
119 	radio_1 = XmCreateToggleButtonGadget ( radiob, "radio_1", al, ac );
120 	radio_2 = XmCreateToggleButtonGadget ( radiob, "radio_2", al, ac );
121 	radio_3 = XmCreateToggleButtonGadget ( radiob, "radio_3", al, ac );
122 	label_1 = XmCreateLabel ( form, "label_1", al, ac );
123 	text = XmCreateTextField ( form, "text", al, ac );
124 	xmstring = XmStringCreateLtoR("Quit button can be activated by Ctrl-Shift-Q\nanywhere in the dialog", (XmStringCharSet)XmFONTLIST_DEFAULT_TAG);
125 	XtSetArg(al[ac], XmNlabelString, xmstring); ac++;
126 	label_2 = XmCreateLabel ( form, "label_2", al, ac );
127 	ac = 0;
128 	XmStringFree ( xmstring);
129 	xmstring = XmStringCreateLtoR("OK", (XmStringCharSet)XmFONTLIST_DEFAULT_TAG);
130 	XtSetArg(al[ac], XmNlabelString, xmstring); ac++;
131 	okb = XmCreatePushButton ( template, "okb", al, ac );
132 	ac = 0;
133 	XmStringFree ( xmstring);
134 	xmstring = XmStringCreateLtoR("Quit", (XmStringCharSet)XmFONTLIST_DEFAULT_TAG);
135 	XtSetArg(al[ac], XmNlabelString, xmstring); ac++;
136 	quitb = XmCreatePushButton ( template, "quitb", al, ac );
137 	ac = 0;
138 	XmStringFree ( xmstring);
139 
140 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
141 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
142 	XtSetValues ( radiob,al, ac );
143 	ac = 0;
144 
145 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
146 	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
147 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
148 	XtSetArg(al[ac], XmNleftWidget, radiob); ac++;
149 	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_NONE); ac++;
150 	XtSetValues ( label_1,al, ac );
151 	ac = 0;
152 
153 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
154 	XtSetArg(al[ac], XmNtopWidget, label_1); ac++;
155 	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
156 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); ac++;
157 	XtSetArg(al[ac], XmNleftOffset, 0); ac++;
158 	XtSetArg(al[ac], XmNleftWidget, label_1); ac++;
159 	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_NONE); ac++;
160 	XtSetValues ( text,al, ac );
161 	ac = 0;
162 
163 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
164 	XtSetArg(al[ac], XmNtopWidget, radiob); ac++;
165 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
166 	XtSetValues ( label_2,al, ac );
167 	ac = 0;
168 	children[ac++] = radio_1;
169 	children[ac++] = radio_2;
170 	children[ac++] = radio_3;
171 	XtManageChildren(children, ac);
172 	ac = 0;
173 	children[ac++] = radiob;
174 	children[ac++] = label_1;
175 	children[ac++] = text;
176 	children[ac++] = label_2;
177 	XtManageChildren(children, ac);
178 	ac = 0;
179 	children[ac++] = form;
180 	children[ac++] = okb;
181 	children[ac++] = quitb;
182 	XtManageChildren(children, ac);
183 	ac = 0;
184 	XtManageChild ( template);
185 }
186 
187 
188 
189 XtAppContext app_context;
190 Display *display;       /*  Display             */
191 
main(argc,argv)192 int main (argc,argv)
193 int    argc;
194 char            **argv;
195 {
196   XtSetLanguageProc ( (XtAppContext) NULL, (XtLanguageProc) NULL, (XtPointer) NULL );
197   XtToolkitInitialize ();
198   app_context = XtCreateApplicationContext ();
199   display = XtOpenDisplay (app_context, NULL, argv[0], "XApplication",
200 			   NULL, 0, &argc, argv);
201   if (!display)
202     {
203       printf("%s: can't open display, exiting...\n", argv[0]);
204       exit (-1);
205     }
206   /* Create the dialog */
207   create_shell ( display, argv[0], argc, argv );
208 
209   /* Install the accelerator */
210   install_accelerator (shell, XK_q, ControlMask | ShiftMask, quitb);
211   XtRealizeWidget (shell);
212 
213 {
214     static XtWidgetGeometry Expected[] = {
215    CWWidth | CWHeight            ,  428,  398,  295,  196, 0,0,0, /* template */
216    CWWidth | CWHeight | CWX | CWY,    0,  148,  295,    2, 0,0,0, /* Separator */
217    CWWidth | CWHeight | CWX | CWY,   11,   11,  273,  127, 0,0,0, /* form */
218    CWWidth | CWHeight | CWX | CWY,    5,    5,   73,   87, 0,0,0, /* radiob */
219    CWWidth | CWHeight | CWX | CWY,    3,    3,   67,   25, 0,0,0, /* radio_1 */
220    CWWidth | CWHeight | CWX | CWY,    3,   31,   67,   25, 0,0,0, /* radio_2 */
221    CWWidth | CWHeight | CWX | CWY,    3,   59,   67,   25, 0,0,0, /* radio_3 */
222    CWWidth | CWHeight | CWX | CWY,   83,    5,   46,   17, 0,0,0, /* label_1 */
223    CWWidth | CWHeight | CWX | CWY,   83,   27,  138,   31, 0,0,0, /* text */
224    CWWidth | CWHeight | CWX | CWY,    5,   97,  268,   30, 0,0,0, /* label_2 */
225    CWWidth | CWHeight | CWX | CWY,   11,  160,   36,   25, 0,0,0, /* okb */
226    CWWidth | CWHeight | CWX | CWY,  248,  160,   36,   25, 0,0,0, /* quitb */
227 };
228 /* toplevel should be replaced with to correct applicationShell */
229 PrintDetails(shell, Expected);
230 }
231 LessTifTestMainLoop(shell);
232   exit (0);
233 }
234 
235