1 /* $Header: /cvsroot/lesstif/lesstif/test/Xm/rowcolumn/test53.c,v 1.4 2001/05/15 14:46:10 amai Exp $ */
2 
3 #include <stdlib.h>
4 #include <stdio.h>
5 
6 #include <Xm/XmP.h>
7 #include <Xm/FormP.h>
8 #include <Xm/RowColumnP.h>
9 #include <Xm/CascadeBP.h>
10 #include <Xm/CascadeBGP.h>
11 #include <Xm/PushBP.h>
12 #include <Xm/PushBGP.h>
13 #include <Xm/RepType.h>
14 #include <Xm/SeparatoGP.h>
15 #include <Xm/MenuShellP.h>
16 #include <Xm/MainWP.h>
17 
18 
19 /* This test demonstrates a problem with NEWMENUS and attaching submenus
20    after creation with tearoff enabled.  Run once normally, and once with
21    -xrm "*tearOffModel: tear_off_enabled".  Notice that the menu order is
22    different(!?) and that there's no submenu for the one of the
23    cascadebuttons
24  */
25 
26 #define RC_Armed(w)	(((XmRowColumnWidget)(w))->row_column.armed)
27 
28 
29 Widget toplevel, mainw, menubar, filepane;
30 Widget button, button1, button2, sep;
31 Widget saveAsPane, button3, button4, button5;
32 Widget button6, button7, button8, button9, editpane;
33 Widget button10, button11, sub_popup_pane;
34 Widget inclPane, button12, button13, button14;
35 Widget one, two, three;
36 Widget optpane;
37 Widget opt1, opt2;
38 Widget ms1, ms2;
39 Widget wa, md, mp, mo, option;
40 Widget popup;
41 #if 0
42 Widget	mb;
43 #endif
44 
45 void
PrintPostedFrom(Widget w)46 PrintPostedFrom(Widget w)
47 {
48     Widget pf = XmGetPostedFromWidget(w);
49 
50     printf("XmGetPostedFromWidget(%s)=%s\n",
51 	   XtName(w),
52 	   pf ? XtName(pf) : "(null)");
53 }
54 
55 void
PopdownCB(Widget w,XtPointer client_data,XtPointer call_data)56 PopdownCB(Widget w, XtPointer client_data, XtPointer call_data)
57 {
58     printf("popdown %s\n", XtName(w));
59 }
60 
61 void
PopupCB(Widget w,XtPointer client_data,XtPointer call_data)62 PopupCB(Widget w, XtPointer client_data, XtPointer call_data)
63 {
64     printf("popup %s\n", XtName(w));
65 }
66 
67 void
MapCB(Widget w,XtPointer client_data,XtPointer call_data)68 MapCB(Widget w, XtPointer client_data, XtPointer call_data)
69 {
70     printf("RC_CascadeBtn(%s)=%p, armed=%x\n", XtName(w), RC_CascadeBtn(w), (int)RC_Armed(w));
71 }
72 
73 void
DisarmCB(Widget w,XtPointer client_data,XtPointer call_data)74 DisarmCB(Widget w, XtPointer client_data, XtPointer call_data)
75 {
76     printf("disarm\n");
77 }
78 
79 void
PushCB(Widget w,XtPointer client_data,XtPointer call_data)80 PushCB(Widget w, XtPointer client_data, XtPointer call_data)
81 {
82     Widget mp = (Widget)client_data;
83 
84     if (XmIsTraversable(wa))
85 	printf("(wa) IS TRAVERSABLE\n");
86     else
87 	printf("(wa) IS NOT TRAVERSABLE\n");
88     if (_XmIsNavigable(wa))
89 	printf("(wa) IS NAVIGABLE\n");
90     else
91 	printf("(wa) IS NOT NAVIGABLE\n");
92 #if 0
93     if (XmIsTraversable(mb))
94 	printf("(mb) IS TRAVERSABLE\n");
95     else
96 	printf("(mb) IS NOT TRAVERSABLE\n");
97     if (_XmIsNavigable(mb))
98 	printf("(mb) IS NAVIGABLE\n");
99     else
100 	printf("(mb) IS NOT NAVIGABLE\n");
101 #endif
102     if (XmIsTraversable(md))
103 	printf("(md) IS TRAVERSABLE\n");
104     else
105 	printf("(md) IS NOT TRAVERSABLE\n");
106     if (_XmIsNavigable(md))
107 	printf("(md) IS NAVIGABLE\n");
108     else
109 	printf("(md) IS NOT NAVIGABLE\n");
110     if (XmIsTraversable(mp))
111 	printf("(mp) IS TRAVERSABLE\n");
112     else
113 	printf("(mp) IS NOT TRAVERSABLE\n");
114     if (_XmIsNavigable(mp))
115 	printf("(mp) IS NAVIGABLE\n");
116     else
117 	printf("(mp) IS NOT NAVIGABLE\n");
118     if (XmIsTraversable(option))
119 	printf("(option) IS TRAVERSABLE\n");
120     else
121 	printf("(option) IS NOT TRAVERSABLE\n");
122     if (_XmIsNavigable(option))
123 	printf("(option) IS NAVIGABLE\n");
124     else
125 	printf("(option) IS NOT NAVIGABLE\n");
126 
127     printf("\n");
128 
129     PrintPostedFrom(mp);
130 }
131 
132 void
buttonPressEventHandler(Widget w,XtPointer client_data,XEvent * e,Boolean * cont)133 buttonPressEventHandler(Widget w,
134 			XtPointer client_data,
135 			XEvent *e,
136 			Boolean *cont)
137 {
138     XButtonEvent *be = (XButtonEvent*)e;
139 
140     if (1/*be->button == 3*/)
141     {
142 	XmMenuPosition(popup, be);
143 
144 	XtManageChild(popup);
145     }
146 }
147 
148 void
buttonPopup(Widget w,XEvent * event,String * params,Cardinal * num_params)149 buttonPopup(Widget w, XEvent *event, String *params, Cardinal *num_params)
150 {
151     XButtonEvent *be = (XButtonEvent*)event;
152 
153     printf("Widget: %s : BUTTON3!\n", XtName(w));
154 
155     XmMenuPosition(popup, be);
156 
157     XtManageChild(popup);
158 }
159 
160 
161 static char but_trans[] = "\
162 #override <Btn3Down> : buttonPopup()\n";
163 
164 static XtActionsRec app_actions[] = {
165 	{ "buttonPopup", buttonPopup }
166 };
167 
168 
GXAttachMenu(Widget submenu,Widget parent,int position)169 void GXAttachMenu(Widget submenu, Widget parent, int position)
170 /*-
171    attach SUBMENU to PARENT menu's POSITIONth cascade button (starts at 0)
172 -*/
173 {
174     Cardinal kidcnt;
175     WidgetList *kidlist;
176     int i, cnt;
177     WidgetClass class;
178 
179     /* get list of children */
180     XtVaGetValues(parent,
181 		  XtNchildren, &kidlist,
182 		  XtNnumChildren, &kidcnt,
183 		  NULL);
184     if ((position > kidcnt) || (position < 0))
185 	return;			/* verify range */
186 
187     /* search for POSITIONth cascade button */
188     for (i = 0, cnt = -1; i < kidcnt; i++) {
189 	class = XtClass((Widget) kidlist[i]);
190 	if (class == xmCascadeButtonWidgetClass)
191 	    cnt++;
192 	if (cnt == position) {	/* set submenuid & return */
193 	    XtVaSetValues((Widget) kidlist[i],
194 			  XmNsubMenuId, submenu,
195 			  NULL);
196 	    return;
197 	}
198     }
199     return;			/* failed to find match */
200 }
201 
202 
203 int
main(int argc,char ** argv)204 main(int argc, char **argv)
205 {
206     XtAppContext theApp;
207     Arg args[6];
208     int ac;
209     Dimension mmt, mmb, mml, mmr, mmw, mmh, st, ht;
210 
211     XmRepTypeInstallTearOffModelConverter();
212 
213     toplevel = XtVaAppInitialize(&theApp, "mainW", NULL, 0,
214 				 &argc, argv, NULL, NULL);
215 
216     XtAppAddActions(theApp, app_actions, XtNumber(app_actions));
217     XtSetArg(args[0], XtNtranslations, XtParseTranslationTable(but_trans));
218     mainw = XmCreateMainWindow(toplevel, "MainWindow", args, 1);
219     XtManageChild(mainw);
220 
221     printf("after creation:\n");
222 
223     wa = XtVaCreateWidget("work_area", xmRowColumnWidgetClass, mainw,
224 			  XmNrowColumnType, XmWORK_AREA,
225 			  NULL);
226 #if 0
227 /* this causes core dumps with Motif-1.2 on Solaris */
228     PrintPostedFrom(wa);
229 #endif
230 
231 #if 0
232     mb = XtVaCreateWidget("menu_bar", xmRowColumnWidgetClass, mainw,
233 			  XmNrowColumnType, XmMENU_BAR,
234 			  NULL);
235 
236     PrintPostedFrom(mb);
237 #endif
238 
239     ms1 = XtVaCreatePopupShell("ms1", xmMenuShellWidgetClass, mainw,
240 			       XmNwidth, 1,
241 			       XmNheight, 1,
242 			       NULL);
243 
244 
245     md = XtVaCreateWidget("menu_pulldown", xmRowColumnWidgetClass, ms1,
246 			  XmNrowColumnType, XmMENU_PULLDOWN,
247 			  NULL);
248 
249     PrintPostedFrom(md);
250 
251     ms2 = XtVaCreatePopupShell("ms2", xmMenuShellWidgetClass, mainw,
252 			       XmNwidth, 1,
253 			       XmNheight, 1,
254 			       NULL);
255 
256     mp = XtVaCreateWidget("menu_popup", xmRowColumnWidgetClass, ms2,
257 			  XmNrowColumnType, XmMENU_POPUP,
258 			  NULL);
259 
260     PrintPostedFrom(mp);
261 
262     mo = XtVaCreateWidget("menu_option", xmRowColumnWidgetClass, mainw,
263 			  XmNrowColumnType, XmMENU_OPTION,
264 			  NULL);
265 
266     PrintPostedFrom(mo);
267 
268     printf("\n");
269 
270     menubar = XmCreateMenuBar(mainw,
271 			      "menuBar",
272 			      NULL,0);
273 
274     filepane = XmCreatePulldownMenu(menubar,
275 				    "file_pane",
276 				    NULL,0);
277 
278     button = XtVaCreateManagedWidget("File",
279 				     xmCascadeButtonWidgetClass,
280 				     menubar,
281 				     XmNsubMenuId, filepane,
282 				     NULL);
283   XtVaGetValues(button,
284 		XmNmarginTop, &mmt, XmNmarginBottom, &mmb,
285 		XmNmarginLeft, &mml, XmNmarginRight, &mmr,
286 		XmNmarginWidth, &mmw, XmNmarginHeight, &mmh,
287 		XmNshadowThickness, &st, XmNhighlightThickness, &ht,
288 		NULL);
289 printf("%d %d %d %d %d %d %d %d\n",
290 	mmt, mmb, mml, mmr, mmw, mmh, st, ht);
291 
292 
293     editpane = XmCreatePulldownMenu(menubar,
294 				    "pane2",
295 				    NULL, 0);
296 
297     button6 = XtVaCreateManagedWidget("Edit",
298 				      xmCascadeButtonWidgetClass,
299 				      menubar,
300 				      XmNsubMenuId, editpane,
301 				      NULL);
302 
303     button7 = XtVaCreateManagedWidget("Cut",
304 				      xmPushButtonWidgetClass,
305 				      editpane,
306 				      NULL);
307 
308     button12 = XtVaCreateManagedWidget("Include",
309 				       xmCascadeButtonWidgetClass,
310 				       filepane,
311 				       XmNsubMenuId, NULL,
312 				       NULL);
313     inclPane = XmCreatePulldownMenu(filepane,
314 				    "incl_pane",
315 				    NULL, 0);
316 
317 
318 
319     button3 = XtVaCreateManagedWidget("Save As",
320 				      xmCascadeButtonWidgetClass,
321 				      filepane,
322 				      XmNsubMenuId, NULL,
323 				      NULL);
324 
325     saveAsPane = XmCreatePulldownMenu(filepane,
326 				      "save_as_pane",
327 				      NULL, 0);
328 
329 
330     button1 = XtVaCreateManagedWidget("Open",
331 				      xmPushButtonWidgetClass,
332 				      filepane,
333 				      NULL);
334 
335     XtAddCallback(XtParent(filepane), XmNpopdownCallback, PopdownCB, NULL);
336 
337     XtAddCallback(XtParent(filepane), XmNpopupCallback, PopupCB, NULL);
338 
339     XtAddCallback(filepane, XmNmapCallback, MapCB, NULL);
340 
341     XtAddCallback(button1, XmNactivateCallback, PushCB, (Widget)filepane);
342 
343     XtAddCallback(button1, XmNdisarmCallback, DisarmCB, NULL);
344 
345     sep = XtVaCreateManagedWidget("sep",
346 				  xmSeparatorGadgetClass,
347                                   filepane,
348                                   NULL);
349 
350     button2 = XtVaCreateManagedWidget("Close",
351 				      xmPushButtonWidgetClass,
352 				      filepane,
353 				      NULL);
354 
355 
356 
357     button13 = XtVaCreateManagedWidget("fig",
358 				       xmPushButtonWidgetClass,
359 				       inclPane,
360 				       NULL);
361 
362     button14 = XtVaCreateManagedWidget("pic",
363 				       xmPushButtonWidgetClass,
364 				       inclPane,
365 				       NULL);
366 
367 
368     XtAddCallback(XtParent(saveAsPane), XmNpopupCallback, PopupCB, NULL);
369 
370     XtAddCallback(XtParent(saveAsPane), XmNpopdownCallback, PopdownCB, NULL);
371 
372     XtAddCallback(saveAsPane, XmNmapCallback, MapCB, NULL);
373 
374 
375     button4 = XtVaCreateManagedWidget("MS Word",
376 				      xmPushButtonWidgetClass,
377 				      saveAsPane,
378 				      NULL);
379 
380     button5 = XtVaCreateManagedWidget("LaTeX",
381 				      xmPushButtonWidgetClass,
382 				      saveAsPane,
383 				      NULL);
384 
385     XtAddCallback(button5, XmNactivateCallback, PushCB, (Widget)saveAsPane);
386 
387     XtManageChild(menubar);
388 
389     one = XtVaCreateManagedWidget("form", xmFormWidgetClass, mainw,
390 				  NULL);
391 
392     optpane = XmCreatePulldownMenu(mainw, "optpane", NULL, 0);
393 
394     opt1 = XtVaCreateManagedWidget("true",
395 				   xmPushButtonWidgetClass,
396 				   optpane,
397 				   NULL);
398 
399     opt2 = XtVaCreateManagedWidget("false",
400 				   xmPushButtonWidgetClass,
401 				   optpane,
402 				   NULL);
403 
404     ac = 0;
405     XtSetArg(args[ac], XmNsubMenuId, optpane); ac++;
406     XtSetArg(args[ac], XmNlabelString, XmStringCreateLocalized("Guess:")); ac++;
407     XtSetArg(args[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
408     XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
409     XtSetArg(args[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
410     XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
411 
412     option = XmCreateOptionMenu(one, "option", args, ac);
413 
414     XtManageChild(option);
415 
416   XtVaGetValues(XmOptionButtonGadget(option),
417 		XmNmarginTop, &mmt, XmNmarginBottom, &mmb,
418 		XmNmarginLeft, &mml, XmNmarginRight, &mmr,
419 		XmNmarginWidth, &mmw, XmNmarginHeight, &mmh,
420 		XmNshadowThickness, &st, XmNhighlightThickness, &ht,
421 		NULL);
422 printf("%d %d %d %d %d %d %d %d %d %d\n",
423 	mmt, mmb, mml, mmr, mmw, mmh, st, ht, XtWidth(option), XtHeight(option));
424 
425     XtAddCallback(opt1, XmNactivateCallback, PushCB, (Widget)option);
426 
427     two = XtVaCreateManagedWidget("option", xmPushButtonWidgetClass, one,
428 				  XmNtopAttachment, XmATTACH_WIDGET,
429 				  XmNtopWidget, option,
430 				  XmNbottomAttachment, XmATTACH_FORM,
431 				  XmNleftAttachment, XmATTACH_NONE,
432 				  XmNrightAttachment, XmATTACH_FORM,
433 				  NULL);
434 
435     XtAddCallback(two, XmNactivateCallback, PushCB, (Widget)option);
436     XtAddCallback(two, XmNactivateCallback, PushCB, (Widget)optpane);
437 
438     three = XtVaCreateManagedWidget("menu", xmPushButtonWidgetClass, one,
439 				    XmNtopAttachment, XmATTACH_WIDGET,
440 				    XmNtopWidget, option,
441 				    XmNbottomAttachment, XmATTACH_FORM,
442 				    XmNleftAttachment, XmATTACH_FORM,
443 				    XmNrightAttachment, XmATTACH_WIDGET,
444 				    XmNrightWidget, two,
445 				    NULL);
446 
447     XtAddEventHandler(two, KeyPressMask, False,
448 		      buttonPressEventHandler, NULL);
449 
450 #if 0
451     XtSetArg(args[0], XmNmenuPost, /* "<Btn3Down>"*/"<Button3>");
452 #endif
453     popup = XmCreatePopupMenu(one, "popup", args, 1);
454 
455 #if 1
456 #endif
457 
458     button8 = XtVaCreateManagedWidget("button8",
459 				      xmPushButtonWidgetClass,
460 				      popup,
461 				      NULL);
462 
463     XtAddCallback(button8, XmNactivateCallback, PushCB, (Widget)popup);
464 
465     sub_popup_pane = XmCreatePulldownMenu(two,
466 					 "save_as_pane",
467 					 NULL, 0);
468 
469     button10 = XtVaCreateManagedWidget("button10", xmPushButtonWidgetClass, sub_popup_pane,
470 				       NULL);
471 
472     XtAddCallback(button10, XmNactivateCallback, PushCB, (Widget)sub_popup_pane);
473 
474     button11 = XtVaCreateManagedWidget("button11", xmPushButtonWidgetClass, sub_popup_pane,
475 				       NULL);
476 
477     button9 = XtVaCreateManagedWidget("button9", xmCascadeButtonWidgetClass, popup,
478 				      XmNsubMenuId, sub_popup_pane,
479 				      NULL);
480 
481     XtAddCallback(three, XmNactivateCallback, PushCB, (Widget)menubar);
482     XtAddCallback(three, XmNactivateCallback, PushCB, (Widget)filepane);
483     XtAddCallback(three, XmNactivateCallback, PushCB, (Widget)saveAsPane);
484 
485     GXAttachMenu(inclPane,filepane,0);
486     GXAttachMenu(saveAsPane,filepane,1);
487 
488     XmMainWindowSetAreas(mainw, menubar, NULL, NULL, NULL, one);
489 
490     XtRealizeWidget(toplevel);
491 
492   XtVaGetValues(XmOptionButtonGadget(option),
493 		XmNmarginTop, &mmt, XmNmarginBottom, &mmb,
494 		XmNmarginLeft, &mml, XmNmarginRight, &mmr,
495 		XmNmarginWidth, &mmw, XmNmarginHeight, &mmh,
496 		XmNshadowThickness, &st, XmNhighlightThickness, &ht,
497 		NULL);
498 printf("%d %d %d %d %d %d %d %d %d %d\n",
499 	mmt, mmb, mml, mmr, mmw, mmh, st, ht, XtWidth(option), XtHeight(option));
500 
501 
502 
503 
504 {
505     static XtWidgetGeometry Expected[] = {
506    CWWidth | CWHeight            ,    0,    0,  116,   91, 0,0,0, /* MainWindow */
507    CWWidth | CWHeight | CWX | CWY,    3,    3,    4,   25, 0,0,0, /* OptionLabel */
508    CWWidth | CWHeight | CWX | CWY,   10,    3,   84,   25, 0,0,0, /* OptionButton */
509    CWWidth | CWHeight | CWX | CWY,    0,    0,  116,   31, 0,0,0, /* menuBar */
510    CWWidth | CWHeight | CWX | CWY,    5,    5,   40,   21, 0,0,0, /* File */
511    CWWidth | CWHeight | CWX | CWY,   45,    5,   40,   21, 0,0,0, /* Edit */
512    CWWidth | CWHeight | CWX | CWY,    0,   31,  116,   60, 0,0,0, /* form */
513    CWWidth | CWHeight | CWX | CWY,    0,    0,  116,   35, 0,0,0, /* option */
514    CWWidth | CWHeight | CWX | CWY,    3,    3,   40,   29, 0,0,0, /* OptionLabel */
515    CWWidth | CWHeight | CWX | CWY,   46,    3,   67,   29, 0,0,0, /* OptionButton */
516    CWWidth | CWHeight | CWX | CWY,   68,   35,   48,   25, 0,0,0, /* option */
517    CWWidth | CWHeight | CWX | CWY,    0,   35,   68,   25, 0,0,0, /* menu */
518 };
519 /* toplevel should be replaced with to correct applicationShell */
520 PrintDetails(toplevel, Expected);
521 }
522 LessTifTestMainLoop(toplevel);
523     /*
524     XtAppMainLoop(theApp);
525     */
526 
527     exit(0);
528 }
529 
530