00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include <stdio.h>
00050 #include <X11/IntrinsicP.h>
00051 #include <X11/StringDefs.h>
00052
00053 #include <X11/Xaw3d/XawInit.h>
00054 #include <X11/Xaw3d/MenuButtoP.h>
00055
00056 static void ClassInitialize();
00057 static void PopupMenu();
00058
00059 #define superclass ((CommandWidgetClass)&commandClassRec)
00060
00061 static char defaultTranslations[] =
00062 "<EnterWindow>: highlight()\n\
00063 <LeaveWindow>: reset()\n\
00064 Any<BtnDown>: reset() PopupMenu()";
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #define offset(field) XtOffsetOf(MenuButtonRec, field)
00075 static XtResource resources[] = {
00076 {
00077 XtNmenuName, XtCMenuName, XtRString, sizeof(String),
00078 offset(menu_button.menu_name), XtRString, (XtPointer)"menu"},
00079 };
00080 #undef offset
00081
00082 static XtActionsRec actionsList[] =
00083 {
00084 {"PopupMenu", PopupMenu}
00085 };
00086
00087 MenuButtonClassRec menuButtonClassRec = {
00088 {
00089 (WidgetClass) superclass,
00090 "MenuButton",
00091 sizeof(MenuButtonRec),
00092 ClassInitialize,
00093 NULL,
00094 FALSE,
00095 NULL,
00096 NULL,
00097 XtInheritRealize,
00098 actionsList,
00099 XtNumber(actionsList),
00100 resources,
00101 XtNumber(resources),
00102 NULLQUARK,
00103 FALSE,
00104 TRUE,
00105 TRUE,
00106 FALSE,
00107 NULL,
00108 XtInheritResize,
00109 XtInheritExpose,
00110 NULL,
00111 NULL,
00112 XtInheritSetValuesAlmost,
00113 NULL,
00114 NULL,
00115 XtVersion,
00116 NULL,
00117 defaultTranslations,
00118 XtInheritQueryGeometry,
00119 XtInheritDisplayAccelerator,
00120 NULL
00121 },
00122 {
00123 XtInheritChangeSensitive
00124 },
00125 {
00126 XtInheritXaw3dShadowDraw,
00127 },
00128 {
00129 0,
00130 },
00131 {
00132 0,
00133 },
00134 {
00135 0,
00136 }
00137 };
00138
00139
00140 WidgetClass menuButtonWidgetClass = (WidgetClass) &menuButtonClassRec;
00141
00142
00143
00144
00145
00146
00147
00148 static void ClassInitialize()
00149 {
00150 XawInitializeWidgetSet();
00151 XtRegisterGrabAction(PopupMenu, True,
00152 (unsigned int)(ButtonPressMask | ButtonReleaseMask),
00153 GrabModeAsync, GrabModeAsync);
00154 }
00155
00156
00157 static void
00158 PopupMenu(w, event, params, num_params)
00159 Widget w;
00160 XEvent * event;
00161 String * params;
00162 Cardinal * num_params;
00163 {
00164 MenuButtonWidget mbw = (MenuButtonWidget) w;
00165 Widget menu = NULL, temp;
00166 Arg arglist[2];
00167 Cardinal num_args;
00168 int menu_x, menu_y, menu_width, menu_height, button_height;
00169 Position button_x, button_y;
00170
00171 temp = w;
00172 while(temp != NULL) {
00173 menu = XtNameToWidget(temp, mbw->menu_button.menu_name);
00174 if (menu == NULL)
00175 temp = XtParent(temp);
00176 else
00177 break;
00178 }
00179
00180 if (menu == NULL) {
00181 char error_buf[BUFSIZ];
00182 (void) sprintf(error_buf, "MenuButton: %s %s.",
00183 "Could not find menu widget named", mbw->menu_button.menu_name);
00184 XtAppWarning(XtWidgetToApplicationContext(w), error_buf);
00185 return;
00186 }
00187 if (!XtIsRealized(menu))
00188 XtRealizeWidget(menu);
00189
00190 menu_width = menu->core.width + 2 * menu->core.border_width;
00191 button_height = w->core.height + 2 * w->core.border_width;
00192 menu_height = menu->core.height + 2 * menu->core.border_width;
00193
00194 XtTranslateCoords(w, 0, 0, &button_x, &button_y);
00195 menu_x = button_x;
00196 menu_y = button_y + button_height;
00197
00198 if (menu_x >= 0) {
00199 int scr_width = WidthOfScreen(XtScreen(menu));
00200 if (menu_x + menu_width > scr_width)
00201 menu_x = scr_width - menu_width;
00202 }
00203 if (menu_x < 0)
00204 menu_x = 0;
00205
00206 if (menu_y >= 0) {
00207 int scr_height = HeightOfScreen(XtScreen(menu));
00208 if (menu_y + menu_height > scr_height)
00209 menu_y = scr_height - menu_height;
00210 }
00211 if (menu_y < 0)
00212 menu_y = 0;
00213
00214 num_args = 0;
00215 XtSetArg(arglist[num_args], XtNx, menu_x); num_args++;
00216 XtSetArg(arglist[num_args], XtNy, menu_y); num_args++;
00217 XtSetValues(menu, arglist, num_args);
00218
00219 XtPopupSpringLoaded(menu);
00220 }
00221