MenuButton.c

Go to the documentation of this file.
00001 /* $XConsortium: MenuButton.c,v 1.21 95/06/26 20:35:12 kaleb Exp $ */
00002 
00003 /*
00004 Copyright (c) 1989, 1994  X Consortium
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a copy
00007 of this software and associated documentation files (the "Software"), to deal
00008 in the Software without restriction, including without limitation the rights
00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, subject to the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of the X Consortium shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from the X Consortium.
00026  *
00027  */
00028 
00029 
00030 /***********************************************************************
00031  *
00032  * MenuButton Widget
00033  *
00034  ***********************************************************************/
00035 
00036 /*
00037  * MenuButton.c - Source code for MenuButton widget.
00038  *
00039  * This is the source code for the Athena MenuButton widget.
00040  * It is intended to provide an easy method of activating pulldown menus.
00041  *
00042  * Date:    May 2, 1989
00043  *
00044  * By:      Chris D. Peterson
00045  *          MIT X Consortium 
00046  *          kit@expo.lcs.mit.edu
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  * Full class record constant
00069  *
00070  ****************************************************************/
00071 
00072 /* Private Data */
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,           /* superclass             */    
00090     "MenuButton",                       /* class_name             */
00091     sizeof(MenuButtonRec),              /* size                   */
00092     ClassInitialize,                    /* class_initialize       */
00093     NULL,                               /* class_part_initialize  */
00094     FALSE,                              /* class_inited           */
00095     NULL,                               /* initialize             */
00096     NULL,                               /* initialize_hook        */
00097     XtInheritRealize,                   /* realize                */
00098     actionsList,                        /* actions                */
00099     XtNumber(actionsList),              /* num_actions            */
00100     resources,                          /* resources              */
00101     XtNumber(resources),                /* resource_count         */
00102     NULLQUARK,                          /* xrm_class              */
00103     FALSE,                              /* compress_motion        */
00104     TRUE,                               /* compress_exposure      */
00105     TRUE,                               /* compress_enterleave    */
00106     FALSE,                              /* visible_interest       */
00107     NULL,                               /* destroy                */
00108     XtInheritResize,                    /* resize                 */
00109     XtInheritExpose,                    /* expose                 */
00110     NULL,                               /* set_values             */
00111     NULL,                               /* set_values_hook        */
00112     XtInheritSetValuesAlmost,           /* set_values_almost      */
00113     NULL,                               /* get_values_hook        */
00114     NULL,                               /* accept_focus           */
00115     XtVersion,                          /* version                */
00116     NULL,                               /* callback_private       */
00117     defaultTranslations,                /* tm_table               */
00118     XtInheritQueryGeometry,             /* query_geometry         */
00119     XtInheritDisplayAccelerator,        /* display_accelerator    */
00120     NULL                                /* extension              */
00121   },  /* CoreClass fields initialization */
00122   {
00123     XtInheritChangeSensitive            /* change_sensitive       */ 
00124   },  /* SimpleClass fields initialization */
00125   {
00126     XtInheritXaw3dShadowDraw,           /* shadowdraw           */
00127   },  /* ThreeDClass fields initialization */
00128   {
00129     0,                                     /* field not used    */
00130   },  /* LabelClass fields initialization */
00131   {
00132     0,                                     /* field not used    */
00133   },  /* CommandClass fields initialization */
00134   {
00135     0,                                     /* field not used    */
00136   }  /* MenuButtonClass fields initialization */
00137 };
00138 
00139   /* for public consumption */
00140 WidgetClass menuButtonWidgetClass = (WidgetClass) &menuButtonClassRec;
00141 
00142 /****************************************************************
00143  *
00144  * Private Procedures
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 /* ARGSUSED */
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 

Generated on Sun Mar 4 15:03:55 2007 for Scilab [trunk] by  doxygen 1.5.1