1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
#ifndef lint
static char rcsId[]="$Header: /usr/local/rcs/Newt/libxutil/RCS/menu.c,v 2.1 1997/02/11 02:23:27 newt Exp newt $";
#endif
/*****
* menu.c : X11/Motif menu creation/convenience routines
*
* This file Version: $Revision: 2.1 $
*
* Creation date: 06/15/1994
* Last modification: $Date: 1997/02/11 02:23:27 $
* By: $Author: newt $
* Current State: $State: Exp $
*
* Author: koen
*
* Copyright (C) 1994-1997 by Ripley Software Development
* All Rights Reserved
*
* This file is part of the XmHTML Widget Library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* History:
* iEdit
* XntHelp
*****/
/*****
* Revision Log:
* $Log: menu.c,v $
* Revision 2.1 1997/02/11 02:23:27 newt
* Migration into eXweb
*
*****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* X11 includes */
#include <X11/Xlib.h>
#include <X11/Xos.h>
/* Motif includes */
#include <Xm/CascadeBG.h>
#include <Xm/CascadeB.h>
#include <Xm/MenuShell.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Xm/SeparatoG.h>
#include <Xm/ToggleB.h>
#include <Xm/ToggleBG.h>
#include <Xm/Xm.h>
/* Local includes */
#include "menu.h"
/*****
* Name: MenuBuild
* Return Type: Widget
* Description: multiple menu type creation routine
* In:
* parent: parent for this menu
* type: type of desired menu, see below
* title: title for this menu
* mnemonic: shortcut key for this menu
* items: menu descriptions, see menu.h
* Returns:
* On success, the created menu. Can exit the application if the menu
* specification is invalid
* Notes:
* - This function is a modified version of the buildMenu function
* in The Motif Programming Manual, Volume 6 in the O'Reilly X Window
* System Series.
* - The menu types supported are: XmMENU_PULLDOWN, XmMENU_OPTION and
* XmMENU_POPUP
* - The class of the menu buttons can be:
* xmCascadeButtonGadgetClass, xmLabelGadgetClass, xmPushButtonGadgetClass,
* xmSeparatorGadgetClass, xmToggleButtonGadgetClass and all of their Widget
* counterparts.
*****/
Widget
MenuBuild(Widget parent, int type, String title, KeySym mnemonic,
MenuItem *items)
{
int i, argc = 0;
Widget menu, cascade, widget;
String resource;
char menu_name[512];
Arg args[5];
sprintf(menu_name,"%sMenu", title);
/* create appropriate RowColum container */
switch(type)
{
case XmMENU_PULLDOWN:
menu = XmCreatePulldownMenu(parent, menu_name, NULL, 0);
break;
case XmMENU_OPTION:
strcat(menu_name, "_Option");
menu = XmCreatePulldownMenu(parent, menu_name, NULL, 0);
break;
case XmMENU_POPUP:
strcat(menu_name, "_Popup");
menu = XmCreatePopupMenu(parent, menu_name, NULL, 0);
break;
default:
return(NULL);
}
/* create menu triggerer */
if(type == XmMENU_PULLDOWN)
{
cascade = XtVaCreateManagedWidget(title,
xmCascadeButtonGadgetClass, parent,
XmNsubMenuId, menu,
NULL);
if(mnemonic != None)
{
XtVaSetValues(cascade,
XmNmnemonic, mnemonic,
NULL);
}
}
else if(type == XmMENU_OPTION)
{
argc = 0;
XtSetArg(args[argc], XmNsubMenuId, menu); argc++;
cascade = XmCreateOptionMenu(parent, title, args, argc);
}
/* add all menu items */
for(i = 0; items[i].name != NULL; i++)
{
/* check for pull-rights */
if(items[i].subitems)
{
if(type == XmMENU_OPTION)
{
fprintf(stderr, "MenuBuild:\n"
"\tsubmenus in option menus are not allowed.\n");
return(NULL);
}
else
widget = MenuBuild(menu, XmMENU_PULLDOWN,
items[i].name, items[i].mnemonic,
items[i].subitems);
}
else
widget = XtCreateManagedWidget(items[i].name,
*items[i].class, menu, NULL, 0);
/* configure the new menu item */
if(items[i].class != &xmSeparatorGadgetClass &&
items[i].class != &xmSeparatorWidgetClass)
{
argc = 0;
/* callback? */
if(items[i].callback)
{
if(items[i].class == &xmToggleButtonWidgetClass ||
items[i].class == &xmToggleButtonGadgetClass)
resource = XmNvalueChangedCallback;
else
resource = XmNactivateCallback;
XtAddCallback(widget, resource,
items[i].callback, items[i].client_data);
}
/* do we have an mnemonic for this item? */
if(items[i].mnemonic != None)
{
XtSetArg(args[argc], XmNmnemonic, items[i].mnemonic);
argc++;
}
/* initial state? */
if(items[i].class == &xmToggleButtonWidgetClass ||
items[i].class == &xmToggleButtonGadgetClass)
resource = XmNset;
else
resource = XmNsensitive;
XtSetArg(args[argc], resource, items[i].sensitive); argc++;
/* userData? */
if(items[i].id != (XtPointer)-1)
{
XtSetArg(args[argc], XmNuserData, items[i].id);
argc++;
}
/* set them */
if(argc)
XtSetValues(widget, args, argc);
}
}
return(type == XmMENU_POPUP ? menu : cascade);
}
/*****
* Name: MenuBuildShared
* Return Type: Widget
* Description: creates a ``floating'' menu suitable for sharing between
* multiple menubars
* In:
* parent: parent for this menu;
* type: type of menu to be created;
* title: name of this menu;
* items: items for this menu;
* Returns:
* the created menu.
*****/
Widget
MenuBuildShared(Widget parent, int type, String title, MenuItem *items)
{
int i, argc = 0;
Widget menu, widget;
String resource;
char menu_name[512];
Arg args[5];
sprintf(menu_name,"%sMenu", title);
/* create appropriate RowColum container */
switch(type)
{
case XmMENU_PULLDOWN:
menu = XmCreatePulldownMenu(parent, menu_name, NULL, 0);
break;
case XmMENU_OPTION:
strcat(menu_name, "_Option");
menu = XmCreatePulldownMenu(parent, menu_name, NULL, 0);
break;
case XmMENU_POPUP:
strcat(menu_name, "_Popup");
menu = XmCreatePopupMenu(parent, menu_name, NULL, 0);
break;
default:
return(NULL);
}
/* add all menu items */
for(i = 0; items[i].name != NULL; i++)
{
/* check for pull-rights */
if(items[i].subitems)
{
if(type == XmMENU_OPTION)
{
fprintf(stderr, "MenuBuildShared:\n"
"\tsubmenus in option menus are not allowed.\n");
return(NULL);
}
else
widget = MenuBuild(menu, XmMENU_PULLDOWN,
items[i].name, items[i].mnemonic,
items[i].subitems);
}
else
widget = XtCreateManagedWidget(items[i].name,
*items[i].class, menu, NULL, 0);
/* configure the new menu item */
if(items[i].class != &xmSeparatorGadgetClass &&
items[i].class != &xmSeparatorWidgetClass)
{
argc = 0;
/* callback? */
if(items[i].callback)
{
if(items[i].class == &xmToggleButtonWidgetClass ||
items[i].class == &xmToggleButtonGadgetClass)
resource = XmNvalueChangedCallback;
else
resource = XmNactivateCallback;
XtAddCallback(widget, resource,
items[i].callback, items[i].client_data);
}
/* do we have an mnemonic for this item? */
if(items[i].mnemonic != None)
{
XtSetArg(args[argc], XmNmnemonic, items[i].mnemonic);
argc++;
}
/* initial state? */
if(items[i].class == &xmToggleButtonWidgetClass ||
items[i].class == &xmToggleButtonGadgetClass)
resource = XmNset;
else
resource = XmNsensitive;
XtSetArg(args[argc], resource, items[i].sensitive); argc++;
/* userData? */
if(items[i].id != (XtPointer)-1)
{
XtSetArg(args[argc], XmNuserData, items[i].id);
argc++;
}
/* set them */
if(argc)
XtSetValues(widget, args, argc);
}
}
return(menu);
}
/*****
* Name: MenuAddShared
* Return Type: Widget
* Description: creates a menubar entry and adds the given menu to it.
* In:
* parent: parent for this menu
* type: type of desired menu, see below
* title: title for this menu
* mnemonic: shortcut key for this menu
* items: menu items;
* Returns:
* The new menubar entry when a pulldown or optionmenu was requested, NULL
* otherwise.
*****/
Widget
MenuAddShared(Widget parent, int type, String title, KeySym mnemonic,
Widget items)
{
Arg args[2];
int argc = 0;
/* create menu triggerer */
switch(type)
{
case XmMENU_PULLDOWN:
XtSetArg(args[argc], XmNsubMenuId, items); argc++;
if(mnemonic != None)
{
XtSetArg(args[argc], XmNmnemonic, mnemonic);
argc++;
}
return(XmCreateCascadeButtonGadget(parent, title, args, argc));
case XmMENU_OPTION:
XtSetArg(args[argc], XmNsubMenuId, items); argc++;
return(XmCreateOptionMenu(parent, title, args, argc));
default:
fprintf(stderr, "MenuAddShared: unknown menu type!\n");
return(NULL);
}
}
/**********
***** Common menu convenience routines
**********/
/*****
* Name: MenuGetChildren
* Return Type: void
* Description: Get the children of a menu.
* In:
* menu: menu of which to get the children
* children: children of menu. This elemenent is filled upon return.
* num_children: number of elements in children. This element is filled
* upon return.
* Returns:
* nothing
*****/
void
MenuGetChildren(Widget menu, WidgetList *children, int *num_children)
{
Widget foo = NULL;
*children = NULL;
*num_children = 0;
if (XtIsSubclass(menu, xmCascadeButtonWidgetClass) ||
XtIsSubclass(menu, xmCascadeButtonGadgetClass))
{
XtVaGetValues(menu, XmNsubMenuId, &foo, NULL);
XtVaGetValues(foo,
XmNchildren, &*children,
XmNnumChildren, &*num_children,
NULL);
}
else
if ((XmIsRowColumn(menu)) == 1)
{
XtVaGetValues(menu,
XmNchildren, &*children,
XmNnumChildren, &*num_children,
NULL);
}
else
{
fprintf(stderr, "MenuGetChildren:\n"
"\tcan't retrieve children: menu is of unknown class.\n");
return;
}
}
/*****
* Name: MenuFindButtonById
* Return Type: Widget
* Description: Find a widget identified by Id in a pointer array of widgets
* In:
* menu: list of widgets to search
* id: id of widget to find
* Returns:
* widget found or NULL on failure.
*****/
Widget
MenuFindButtonById(Widget menu, int id)
{
register WidgetList child_ptr;
WidgetList childs;
int num_childs;
MenuGetChildren(menu, &childs, &num_childs);
for(child_ptr = childs; child_ptr != NULL; child_ptr++)
{
if((MenuButtonGetId(*child_ptr)) == id)
return(*child_ptr);
}
return(NULL); /* not found */
}
/*****
* Name: MenuFindButtonByName
* Return Type: Widget
* Description: Find a named widget in a pointer array of widgets
* In:
* wlist: list of widgets to search
* name: name of widget to find
* Returns:
* widget found or NULL on failure.
*****/
Widget
MenuFindButtonByName(WidgetList wlist, String name)
{
WidgetList child_ptr;
Widget child = NULL;
for(child_ptr = wlist; child_ptr != NULL; child_ptr++)
{
child = *child_ptr;
if (strcmp(XtName(child),name) == 0)
return(child);
}
return(NULL); /* not found */
}
/*****
* Name: MenuButtonGetId
* Return Type: int
* Description: This routine finds out what number a menubutton has by
* examining the XmNuserData field of the widget
* In:
* button: button for which to fetch the userData value
* Returns:
* value of the XmNuserData resource
*****/
int
MenuButtonGetId(Widget button)
{
int w_num = -1;
XtVaGetValues(button,
XmNuserData, &w_num,
NULL);
return(w_num);
}
/*****
* Name: MenuIsToggleMenu
* Return Type: void
* Description: Make a menu with togglebuttons act as a radiobox
* In:
* menu: menu of which to set state
* Returns:
* nothing
*****/
void
MenuIsToggleMenu(Widget menu)
{
Widget foo = NULL;
if (XtIsSubclass(menu, xmCascadeButtonWidgetClass) ||
XtIsSubclass(menu, xmCascadeButtonGadgetClass))
{
XtVaGetValues(menu,
XmNsubMenuId, &foo,
NULL);
XtVaSetValues(foo,
XmNradioBehavior, True,
XmNradioAlwaysOne, True,
NULL);
}
else
{
fprintf(stderr, "MenuToggleSet:\n\tmenu %s is no subclass "
"of CascadeButton (internal error).\n", XtName(menu));
}
}
/**********
***** Option menu specific convenience routines
**********/
/*****
* Name: MenuOptionSelectItem
* Return Type: void
* Description: selects the given option menu button;
* In:
* menu: menu of which to select a menu item
* id: id of menu item to set
* Returns:
* nothing
*****/
void
MenuOptionSelectItem(Widget menu, int id)
{
Widget option_menu = NULL;
Widget pb;
/* Get all children and number of items contained */
XtVaGetValues(menu,
XmNsubMenuId, &option_menu,
NULL);
/* sanity check */
if(!option_menu)
{
fprintf(stderr, "MenuSelectOption:\n"
"\tcan't select requested menu: menu has no children.\n");
return;
}
/* get correct menu entry */
if((pb = MenuFindButtonById(menu, id)) == NULL)
{
fprintf(stderr, "MenuSelectOption:\n"
"\tcan't select requested menu: requested id out of range.\n");
return;
}
/* Select it */
XtVaSetValues(option_menu,
XmNmenuHistory, pb,
NULL);
}
/*****
* Name: MenuOptionSetSensitivity
* Return Type: void
* Description: Set the sensitivity state of an item in a option menu
* In:
* menu:option menu id
* id: id of menu item of which the state is to be set.
* Zero based index.
* state: state of the item.
* Returns:
* nothing
*****/
void
MenuOptionSetSensitivity(Widget menu, int id, int state)
{
Widget pb;
/* get correct menu entry */
if((pb = MenuFindButtonById(menu, id)) == NULL)
{
fprintf(stderr, "MenuOptionSetSensitivity:\n"
"\tcan't select requested menu: requested id out of range.\n");
return;
}
XtSetSensitive(pb, state);
}
/*****
* Name: MenuOptionGetSelected
* Return Type: int
* Description: Return the id of the selected option menu item
* In:
* menu: Option menu of which to return the selected menu item
* Returns:
* the id (XmNuserData field) of the selected menu item. -1 upon failure.
*****/
int
MenuOptionGetSelected(Widget menu)
{
int item_no;
Widget item;
XtVaGetValues(menu,
XmNmenuHistory, &item,
NULL);
if(!item)
{
fprintf(stderr, "MenuOptionGetSelected:\n"
"\tcan't retrieve selected item in option menu: XtVaGetValues "
"returned NULL.\n");
return(-1);
}
XtVaGetValues(item,
XmNuserData, &item_no,
NULL);
return(item_no);
}
/**********
***** Toggle menu specific convenience routines
**********/
/*****
* Name: MenuToggleSelected
* Return Type: Boolean
* Description: See if a toggle button is checked
* In:
* toggle: toggle button widget to check
* Returns:
* selection state of toggle
*****/
Boolean
MenuToggleSelected(Widget toggle)
{
Boolean value = False;
XtVaGetValues(toggle,
XmNset, &value,
NULL);
return(value);
}
/*****
* Name: MenuToggleSetState
* Return Type: void
* Description: sets the selection state of a togglebutton.
* In:
* menu: menu of which an item has to be set;
* id: id of menubutton to be set;
* state: new toggle state;
* Returns:
* nothing.
*****/
void
MenuToggleSetState(Widget menu, int id, Boolean state)
{
Widget toggle;
/* get correct menu entry */
if((toggle = MenuFindButtonById(menu, id)) == NULL)
{
fprintf(stderr, "MenuToggleSetState:\n"
"\tcan't select requested menu: requested id out of range.\n");
return;
}
XtVaSetValues(toggle, XmNset, state, NULL);
}
/*****
* Name: MenuToggleGetState
* Return Type: void
* Description: gets the selection state of a togglebutton.
* In:
* menu: menu for which an item has to be checked;
* id: id of menubutton to be checked;
* Returns:
* False when it's not selected, True otherwise.
*****/
Boolean
MenuToggleGetState(Widget menu, int id)
{
Widget toggle;
Boolean value = False;
/* get correct menu entry */
if((toggle = MenuFindButtonById(menu, id)) == NULL)
{
fprintf(stderr, "MenuToggleSetState:\n"
"\tcan't get requested menu: requested id out of range.\n");
return(False);
}
XtVaGetValues(toggle,
XmNset, &value,
NULL);
return(value);
}
|