2 static char Xrcsid
[] = "$XConsortium: MenuButton.c,v 1.13 89/12/11 14:57:40 kit Exp $";
6 * Copyright 1989 Massachusetts Institute of Technology
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of M.I.T. not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. M.I.T. makes no representations about the
15 * suitability of this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
18 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
20 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 /***********************************************************************
32 ***********************************************************************/
35 * MenuButton.c - Source code for MenuButton widget.
37 * This is the source code for the Athena MenuButton widget.
38 * It is intended to provide an easy method of activating pulldown menus.
42 * By: Chris D. Peterson
44 * kit@expo.lcs.mit.edu
48 #include <X11/IntrinsicP.h>
49 #include <X11/StringDefs.h>
51 #include <./Xaw3_1XawInit.h>
52 #include <./Xaw3_1MenuButtoP.h>
54 static void Realize();
55 static void PopupMenu();
57 #define superclass ((CommandWidgetClass)&commandClassRec)
59 static char defaultTranslations
[] =
60 "<EnterWindow>: highlight() \n\
61 <LeaveWindow>: reset() \n\
62 <BtnDown>: reset() PopupMenu() ";
64 /****************************************************************
66 * Full class record constant
68 ****************************************************************/
72 #define offset(field) XtOffset(MenuButtonWidget, field)
73 static XtResource resources
[] = {
75 XtNmenuName
, XtCMenuName
, XtRString
, sizeof(String
),
76 offset(menu_button
.menu_name
), XtRString
, (caddr_t
)"menu"},
80 static XtActionsRec actionsList
[] =
82 {"PopupMenu", PopupMenu
}
85 /* %%% Export the actions list as a hack until an Intrinsics interface
86 * allows applications to call action procedures. Used in xmh: button.c
87 * Donna Converse, 7 July, 1989. To be removed before R4 public release.
89 XtActionList xaw_mbutton_actions_list
= actionsList
;
92 MenuButtonClassRec menuButtonClassRec
= {
94 (WidgetClass
) superclass
, /* superclass */
95 "MenuButton", /* class_name */
96 sizeof(MenuButtonRec
), /* size */
97 XawInitializeWidgetSet
, /* class_initialize */
98 NULL
, /* class_part_initialize */
99 FALSE
, /* class_inited */
100 NULL
, /* initialize */
101 NULL
, /* initialize_hook */
102 Realize
, /* realize */
103 actionsList
, /* actions */
104 XtNumber(actionsList
), /* num_actions */
105 resources
, /* resources */
106 XtNumber(resources
), /* resource_count */
107 NULLQUARK
, /* xrm_class */
108 FALSE
, /* compress_motion */
109 TRUE
, /* compress_exposure */
110 TRUE
, /* compress_enterleave */
111 FALSE
, /* visible_interest */
113 XtInheritResize
, /* resize */
114 XtInheritExpose
, /* expose */
115 NULL
, /* set_values */
116 NULL
, /* set_values_hook */
117 XtInheritSetValuesAlmost
, /* set_values_almost */
118 NULL
, /* get_values_hook */
119 NULL
, /* accept_focus */
120 XtVersion
, /* version */
121 NULL
, /* callback_private */
122 defaultTranslations
, /* tm_table */
123 XtInheritQueryGeometry
, /* query_geometry */
124 XtInheritDisplayAccelerator
, /* display_accelerator */
126 }, /* CoreClass fields initialization */
128 XtInheritChangeSensitive
/* change_sensitive */
129 }, /* SimpleClass fields initialization */
131 0, /* field not used */
132 }, /* LabelClass fields initialization */
134 0, /* field not used */
135 }, /* CommandClass fields initialization */
137 0, /* field not used */
138 }, /* MenuButtonClass fields initialization */
141 /* for public consumption */
142 WidgetClass menuButtonWidgetClass
= (WidgetClass
) &menuButtonClassRec
;
144 /****************************************************************
148 ****************************************************************/
152 Realize(w
, mask
, attrs
)
155 XSetWindowAttributes
*attrs
;
157 (*superclass
->core_class
.realize
) (w
, mask
, attrs
);
159 /* We have a window now. Register a grab. */
161 XGrabButton( XtDisplay(w
), AnyButton
, AnyModifier
, XtWindow(w
),
162 TRUE
, ButtonPressMask
|ButtonReleaseMask
,
163 GrabModeAsync
, GrabModeAsync
, None
, None
);
168 PopupMenu(w
, event
, params
, num_params
)
172 Cardinal
* num_params
;
174 MenuButtonWidget mbw
= (MenuButtonWidget
) w
;
178 int menu_x
, menu_y
, menu_width
, menu_height
, button_width
, button_height
;
179 Position button_x
, button_y
;
182 while(temp
!= NULL
) {
183 menu
= XtNameToWidget(temp
, mbw
->menu_button
.menu_name
);
185 temp
= XtParent(temp
);
191 char error_buf
[BUFSIZ
];
192 sprintf(error_buf
, "MenuButton: %s %s.",
193 "Could not find menu widget named", mbw
->menu_button
.menu_name
);
194 XtAppWarning(XtWidgetToApplicationContext(w
), error_buf
);
197 if (!XtIsRealized(menu
))
198 XtRealizeWidget(menu
);
200 menu_width
= menu
->core
.width
+ 2 * menu
->core
.border_width
;
201 button_width
= w
->core
.width
+ 2 * w
->core
.border_width
;
202 button_height
= w
->core
.height
+ 2 * w
->core
.border_width
;
204 menu_height
= menu
->core
.height
+ 2 * menu
->core
.border_width
;
206 XtTranslateCoords(w
, 0, 0, &button_x
, &button_y
);
208 menu_y
= button_y
+ button_height
;
213 int scr_width
= WidthOfScreen(XtScreen(menu
));
214 if (menu_x
+ menu_width
> scr_width
)
215 menu_x
= scr_width
- menu_width
;
221 int scr_height
= HeightOfScreen(XtScreen(menu
));
222 if (menu_y
+ menu_height
> scr_height
)
223 menu_y
= scr_height
- menu_height
;
227 XtSetArg(arglist
[num_args
], XtNx
, menu_x
); num_args
++;
228 XtSetArg(arglist
[num_args
], XtNy
, menu_y
); num_args
++;
229 XtSetValues(menu
, arglist
, num_args
);
231 XtPopupSpringLoaded(menu
);