2
0

MenuBar.C 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: MenuBar.C /main/6 1997/06/03 16:12:28 mgreess $ */
  24. /*
  25. *+SNOTICE
  26. *
  27. * RESTRICTED CONFIDENTIAL INFORMATION:
  28. *
  29. * The information in this document is subject to special
  30. * restrictions in a confidential disclosure agreement bertween
  31. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  32. * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
  33. * Sun's specific written approval. This documment and all copies
  34. * and derivative works thereof must be returned or destroyed at
  35. * Sun's request.
  36. *
  37. * Copyright 1993, 1994, 1995 Sun Microsystems, Inc. All rights reserved.
  38. *
  39. *+ENOTICE
  40. */
  41. ///////////////////////////////////////////////////////////////////////////////
  42. //////////////////////////////////////////////////////////////////////////////
  43. // This example code is from the book:
  44. //
  45. // Object-Oriented Programming with C++ and OSF/Motif
  46. // by
  47. // Douglas Young
  48. // Prentice Hall, 1992
  49. // ISBN 0-13-630252-1
  50. //
  51. // Copyright 1991 by Prentice Hall
  52. // All Rights Reserved
  53. //
  54. // Permission to use, copy, modify, and distribute this software for
  55. // any purpose except publication and without fee is hereby granted, provided
  56. // that the above copyright notice appear in all copies of the software.
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //////////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////
  60. // MenuBar.C: A menu bar whose panes support items
  61. // that execute Cmd's
  62. //////////////////////////////////////////////////////////
  63. ////////////////////////////////////////////////////////////
  64. // MODIFIED TO SUPPORT SUBMENUS - not described in Book
  65. ///////////////////////////////////////////////////////////
  66. #include "Application.h"
  67. #include "MenuBar.h"
  68. #include "Cmd.h"
  69. #include "CmdList.h"
  70. #include "ToggleButtonInterface.h"
  71. #include "ButtonInterface.h"
  72. #include "Help.hh"
  73. #include <Xm/RowColumn.h>
  74. #include <Xm/CascadeB.h>
  75. #include <Xm/Separator.h>
  76. #include <Xm/PushB.h>
  77. #include <Xm/Label.h>
  78. extern "C" {
  79. extern XtPointer _XmStringUngenerate (
  80. XmString string,
  81. XmStringTag tag,
  82. XmTextType tag_type,
  83. XmTextType output_type);
  84. }
  85. MenuBar::MenuBar ( Widget parent, char *name, unsigned char type )
  86. : UIComponent ( name )
  87. {
  88. // Base widget is a Motif menu bar widget
  89. if(parent == NULL)
  90. return;
  91. if( type == XmMENU_POPUP) {
  92. Arg args[1];
  93. int i = 0;
  94. XtSetArg(args[i], XmNwhichButton, theApplication->bMenuButton()); i++;
  95. _w = XmCreatePopupMenu( parent, _name, args, i );
  96. // XtVaSetValues(_w, XmNwhichButton, theApplication->bMenuButton(), NULL);
  97. } else {
  98. type = XmMENU_BAR; // Force it to menu bar
  99. _w = XmCreateMenuBar ( parent, _name, NULL, 0 );
  100. }
  101. printHelpId("_w", _w);
  102. /* install callback */
  103. // XtAddCallback(_w, XmNhelpCallback, HelpCB, helpId);
  104. installDestroyHandler();
  105. }
  106. Widget MenuBar::addCommands ( Widget *menuBarCascade, CmdList *list, Boolean help, unsigned char type )
  107. {
  108. return( createPulldown ( _w, list, menuBarCascade, help, type ) );
  109. }
  110. Widget MenuBar::addCommands ( CmdList *list, Boolean help, unsigned char type )
  111. {
  112. return( createPulldown ( _w, list, help, type ) );
  113. }
  114. Widget
  115. MenuBar::createPulldown (
  116. Widget parent,
  117. CmdList *list,
  118. Widget *cascade,
  119. Boolean help ,
  120. unsigned char type)
  121. {
  122. int i;
  123. Widget pulldown;
  124. Arg args2[2];
  125. char * helpId;
  126. XmString label_str;
  127. if(type != XmMENU_POPUP)
  128. type = XmMENU_BAR;
  129. // Create a pulldown menu pane for this list of commands
  130. if( type == XmMENU_BAR) {
  131. pulldown = XmCreatePulldownMenu( parent,
  132. (char *) list->name(), NULL, 0 );
  133. label_str = XmStringCreateLocalized(list->getLabel());
  134. XtVaSetValues(pulldown,
  135. XmNlabelString, label_str,
  136. NULL);
  137. printHelpId("pulldown", pulldown);
  138. /* install callback */
  139. // XtAddCallback(pulldown, XmNhelpCallback, HelpCB, helpId);
  140. // Each entry in the menu bar must have a cascade button
  141. // from which the user can pull down the pane
  142. if (cascade != NULL && *cascade != NULL)
  143. XtVaSetValues (*cascade, XmNsubMenuId, pulldown,
  144. XmNlabelString, label_str,
  145. NULL);
  146. else {
  147. XtSetArg(args2[0], XmNsubMenuId, pulldown);
  148. *cascade = XtCreateWidget ( list->name(),
  149. xmCascadeButtonWidgetClass,
  150. parent,
  151. args2, 1);
  152. XtVaSetValues(*cascade,
  153. XmNlabelString, label_str,
  154. NULL);
  155. if (help) {
  156. XtSetArg (args2[0], XmNmenuHelpWidget, *cascade);
  157. XtSetValues (parent, args2, 1);
  158. }
  159. XtManageChild ( *cascade );
  160. printHelpId("cascade", *cascade);
  161. /* install callback */
  162. // Install callbacks for each of the
  163. // pulldown menus so we can get
  164. // On Item help for them.
  165. helpId = XtName(*cascade);
  166. if (helpId == "Mailbox") {
  167. XtAddCallback(*cascade, XmNhelpCallback,
  168. HelpCB, (void *)DTMAILCONTAINERMENUID);
  169. } else if (helpId == "Edit") {
  170. XtAddCallback(*cascade, XmNhelpCallback,
  171. HelpCB, (void *)DTMAILEDITMENUID);
  172. } else if (helpId == "Message") {
  173. XtAddCallback(*cascade, XmNhelpCallback,
  174. HelpCB, (void *)DTMAILMESSAGEMENUID);
  175. } else if (helpId == "Attachments") {
  176. XtAddCallback(*cascade, XmNhelpCallback,
  177. HelpCB, (void *)DTMAILATTACHMENUID);
  178. } else if (helpId == "View") {
  179. XtAddCallback(*cascade, XmNhelpCallback,
  180. HelpCB, (void *)DTMAILVIEWMENUID);
  181. } else if (helpId == "Compose") {
  182. XtAddCallback(*cascade, XmNhelpCallback,
  183. HelpCB, (void *)DTMAILCOMPOSEMENUID);
  184. }
  185. }
  186. } else
  187. pulldown = _w;
  188. // Loop through the cmdList, creating a menu
  189. // entry for each command.
  190. Widget *head_wl, *wl;
  191. Cardinal num_wl = 0;
  192. head_wl = wl = new Widget[ list->size() ];
  193. for ( i = 0; i < list->size(); i++)
  194. {
  195. if(!strcmp((*list)[i]->className(), "CmdList")) {
  196. Widget pane = createPulldown(pulldown,
  197. (CmdList*) (*list)[i], FALSE, XmMENU_BAR);
  198. ((CmdList *)(*list)[i])->setPaneWidget(pane);
  199. label_str = XmStringCreateLocalized(((CmdList *)(*list)[i])->getLabel());
  200. XtVaSetValues(pane,
  201. XmNlabelString, label_str,
  202. NULL);
  203. } else {
  204. if ( !strcmp((*list)[i]->className(),"SeparatorCmd")) {
  205. *(wl++) = XtCreateWidget ( (*list)[i]->name(),
  206. xmSeparatorWidgetClass,
  207. pulldown,
  208. NULL, 0);
  209. } else if (!strcmp((*list)[i]->className(),"ToggleButtonCmd")) {
  210. CmdInterface *ci;
  211. ci = new ToggleButtonInterface(pulldown, (*list)[i] );
  212. *(wl++) = ci->baseWidget();
  213. } else if(!strcmp((*list)[i]->className(),"LabelCmd")) {
  214. label_str = XmStringCreateLocalized((*list)[i]->getLabel());
  215. *(wl++) = XtVaCreateWidget ( (*list)[i]->name(),
  216. xmLabelWidgetClass,
  217. pulldown,
  218. XmNlabelString, label_str,
  219. NULL);
  220. } else {
  221. CmdInterface *ci;
  222. ci = new ButtonInterface ( pulldown, (*list)[i] );
  223. *(wl++) = ci->baseWidget();
  224. }
  225. num_wl++;
  226. }
  227. }
  228. XtManageChildren ( head_wl, num_wl );
  229. delete [] head_wl;
  230. return(pulldown);
  231. }
  232. Widget
  233. MenuBar::createPulldown (
  234. Widget parent,
  235. CmdList *list,
  236. Boolean help ,
  237. unsigned char type)
  238. {
  239. int i;
  240. Widget pulldown, cascade;
  241. Arg args2[2];
  242. char * helpId;
  243. XmString label_str;
  244. if(type != XmMENU_POPUP)
  245. type = XmMENU_BAR;
  246. // Create a pulldown menu pane for this list of commands
  247. if( type == XmMENU_BAR) {
  248. pulldown = XmCreatePulldownMenu( parent,
  249. (char *) list->name(), NULL, 0 );
  250. label_str = XmStringCreateLocalized(list->getLabel());
  251. XtVaSetValues(pulldown,
  252. XmNlabelString, label_str,
  253. NULL);
  254. printHelpId("pulldown", pulldown);
  255. /* install callback */
  256. // XtAddCallback(pulldown, XmNhelpCallback, HelpCB, helpId);
  257. // Each entry in the menu bar must have a cascade button
  258. // from which the user can pull down the pane
  259. XtSetArg(args2[0], XmNsubMenuId, pulldown);
  260. cascade = XtCreateWidget ( list->name(),
  261. xmCascadeButtonWidgetClass,
  262. parent,
  263. args2, 1);
  264. XtVaSetValues(cascade,
  265. XmNlabelString, label_str,
  266. NULL);
  267. if (help) {
  268. XtSetArg (args2[0], XmNmenuHelpWidget, cascade);
  269. XtSetValues (parent, args2, 1);
  270. }
  271. XtManageChild ( cascade );
  272. printHelpId("cascade", cascade);
  273. /* install callback */
  274. // Install callbacks for each of the
  275. // pulldown menus so we can get
  276. // On Item help for them.
  277. helpId = XtName(cascade);
  278. if (helpId == "Mailbox") {
  279. XtAddCallback(cascade, XmNhelpCallback,
  280. HelpCB, (void *)DTMAILCONTAINERMENUID);
  281. } else if (helpId == "Edit") {
  282. XtAddCallback(cascade, XmNhelpCallback,
  283. HelpCB, (void *)DTMAILEDITMENUID);
  284. } else if (helpId == "Message") {
  285. XtAddCallback(cascade, XmNhelpCallback,
  286. HelpCB, (void *)DTMAILMESSAGEMENUID);
  287. } else if (helpId == "Attachments") {
  288. XtAddCallback(cascade, XmNhelpCallback,
  289. HelpCB, (void *)DTMAILATTACHMENUID);
  290. } else if (helpId == "View") {
  291. XtAddCallback(cascade, XmNhelpCallback,
  292. HelpCB, (void *)DTMAILVIEWMENUID);
  293. } else if (helpId == "Compose") {
  294. XtAddCallback(cascade, XmNhelpCallback,
  295. HelpCB, (void *)DTMAILCOMPOSEMENUID);
  296. }
  297. } else
  298. pulldown = _w;
  299. // Loop through the cmdList, creating a menu
  300. // entry for each command.
  301. for ( i = 0; i < list->size(); i++)
  302. {
  303. if(!strcmp((*list)[i]->className(), "CmdList")) {
  304. Widget pane = createPulldown(pulldown,
  305. (CmdList*) (*list)[i], FALSE, XmMENU_BAR);
  306. ((CmdList *)(*list)[i])->setPaneWidget(pane);
  307. label_str = XmStringCreateLocalized((*list)[i]->getLabel());
  308. XtVaSetValues(pane,
  309. XmNlabelString, label_str,
  310. NULL);
  311. } else {
  312. if ( !strcmp((*list)[i]->className(),"SeparatorCmd")) {
  313. XtCreateManagedWidget ( (*list)[i]->name(),
  314. xmSeparatorWidgetClass,
  315. pulldown,
  316. NULL, 0);
  317. } else if (!strcmp((*list)[i]->className(),"ToggleButtonCmd")) {
  318. CmdInterface *ci;
  319. ci = new ToggleButtonInterface(pulldown, (*list)[i]);
  320. ci->manage();
  321. } else if(!strcmp((*list)[i]->className(),"LabelCmd")) {
  322. Widget _i18n;
  323. Arg _args[1];
  324. XmString xms;
  325. _i18n = XtCreateManagedWidget(
  326. (*list)[i]->name(),
  327. xmLabelWidgetClass,
  328. pulldown,
  329. NULL, 0
  330. );
  331. xms = XmStringCreateLocalized((char*) (*list)[i]->getLabel());
  332. XtSetArg( _args[0], XmNlabelString, xms );
  333. XtSetValues( _i18n, _args, 1 );
  334. XmStringFree(xms);
  335. } else {
  336. CmdInterface *ci;
  337. ci = new ButtonInterface ( pulldown, (*list)[i] );
  338. ci->manage();
  339. }
  340. }
  341. }
  342. return(pulldown);
  343. }
  344. // SR - Added to handle dynamic menus
  345. Widget
  346. MenuBar::addCommands(
  347. Widget pulldown,
  348. CmdList *new_list
  349. )
  350. {
  351. // if(isValidMenuPane(pulldown) == FALSE)
  352. // return NULL;
  353. int i;
  354. XtArgVal num_children;
  355. WidgetList children;
  356. Boolean haveNoSeparator;
  357. int newItemIndex, numPBUnmanaged, tmpPBUnmanaged;
  358. XtVaGetValues(pulldown,
  359. XmNnumChildren, &num_children,
  360. NULL);
  361. children = (WidgetList)XtMalloc(sizeof(Widget) * num_children);
  362. XtVaGetValues(pulldown,
  363. XmNchildren, &children,
  364. NULL);
  365. // Handle the creation or management of the Separator.
  366. haveNoSeparator = TRUE;
  367. numPBUnmanaged = 0;
  368. for (i=0; (i < num_children); i++) {
  369. Widget wid = (Widget) children[i];
  370. if (XtIsSubclass(wid, xmSeparatorWidgetClass)) {
  371. XtManageChild(wid);
  372. haveNoSeparator = FALSE;
  373. }
  374. else if (XtIsSubclass(wid, xmPushButtonWidgetClass)) {
  375. if (!XtIsManaged(wid)) { // If widget is unmanaged
  376. numPBUnmanaged++;
  377. }
  378. }
  379. }
  380. if (haveNoSeparator) {
  381. XtCreateManagedWidget ("Separator",
  382. xmSeparatorWidgetClass,
  383. pulldown,
  384. NULL, 0);
  385. haveNoSeparator = FALSE;
  386. }
  387. // Now handle the pushButton case
  388. newItemIndex = 0;
  389. tmpPBUnmanaged = 0;
  390. // Loop through the cmdList, creating a menu
  391. // entry for each command.
  392. for (newItemIndex = 0; newItemIndex < new_list->size(); newItemIndex++) {
  393. tmpPBUnmanaged = numPBUnmanaged;
  394. if (numPBUnmanaged > 0) { // If there exists unmanaged PBs
  395. for (i = 0;
  396. (i < num_children) &&
  397. (tmpPBUnmanaged == numPBUnmanaged);
  398. i++) {
  399. Widget wid = (Widget) children[i];
  400. if (XtIsSubclass(wid, xmPushButtonWidgetClass)) {
  401. if (!XtIsManaged(wid)) {
  402. // If widget is unmanaged
  403. // Set its label to be the newItemIndex widget's.
  404. // Manage it.
  405. // Bump up newItemIndex
  406. XtVaSetValues(wid,
  407. XmNlabelString, XmStringCreateLocalized(
  408. (char *) (*new_list)[newItemIndex]->getLabel()),
  409. NULL);
  410. XtManageChild(wid);
  411. numPBUnmanaged--;
  412. }
  413. }
  414. }
  415. }
  416. else { // No unmanaged push buttons available
  417. CmdInterface *ci;
  418. ci = new ButtonInterface (
  419. pulldown, (*new_list)[newItemIndex]
  420. );
  421. ci->manage();
  422. }
  423. }
  424. return(pulldown);
  425. }
  426. void
  427. MenuBar::removeOnlyCommands(
  428. Widget pulldown,
  429. CmdList *redundant_list
  430. )
  431. {
  432. // if(isValidMenuPane(pulldown) == FALSE)
  433. // return;
  434. int i, j;
  435. XtArgVal num_children;
  436. WidgetList children;
  437. XtVaGetValues(pulldown,
  438. XmNnumChildren, &num_children,
  439. NULL);
  440. children = (WidgetList)XtMalloc(sizeof(Widget) * num_children);
  441. XtVaGetValues(pulldown,
  442. XmNchildren, &children,
  443. NULL);
  444. // Loop through widget list. Destroy those widgets that map to those
  445. // in the redundant list.
  446. for (i=0; i<num_children; i++) {
  447. Widget wid = (Widget) children[i];
  448. if (XtIsSubclass(wid, xmPushButtonWidgetClass)) {
  449. for (j=0; j<redundant_list->size(); j++) {
  450. char name[200];
  451. ButtonInterface::mapName((*redundant_list)[j]->name(), name);
  452. if (strcmp(XtName(wid), name) == 0) {
  453. // The redundant item has been found.
  454. XtUnmanageChild(wid);
  455. }
  456. }
  457. }
  458. }
  459. }
  460. void
  461. MenuBar::removeCommands(
  462. Widget pulldown,
  463. CmdList *redundant_list
  464. )
  465. {
  466. // if(isValidMenuPane(pulldown) == FALSE)
  467. // return;
  468. int i, j;
  469. XtArgVal num_children;
  470. WidgetList children;
  471. XtVaGetValues(pulldown,
  472. XmNnumChildren, &num_children,
  473. NULL);
  474. children = (WidgetList)XtMalloc(sizeof(Widget) * num_children);
  475. XtVaGetValues(pulldown,
  476. XmNchildren, &children,
  477. NULL);
  478. // Loop through widget list. Destroy those widgets that map to those
  479. // in the redundant list.
  480. for (i=0; i<num_children; i++) {
  481. Widget wid = (Widget) children[i];
  482. if (XtIsSubclass(wid, xmSeparatorWidgetClass)) {
  483. XtUnmanageChild(wid);
  484. }
  485. else if (XtIsSubclass(wid, xmPushButtonWidgetClass)) {
  486. for (j=0; j<redundant_list->size(); j++) {
  487. XmString str=NULL;
  488. String label=NULL;
  489. XtVaGetValues(wid, XmNlabelString, &str, NULL);
  490. if (str == NULL) continue;
  491. label = NULL;
  492. label = (char *) _XmStringUngenerate(
  493. str, NULL,
  494. XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
  495. XmStringFree(str);
  496. if (label == NULL) continue;
  497. if (strcmp(label, (*redundant_list)[j]->getLabel()) == 0) {
  498. // The redundant item has been found.
  499. XtUnmanageChild(wid);
  500. }
  501. XtFree(label);
  502. }
  503. }
  504. }
  505. }
  506. void
  507. MenuBar::addCommand(
  508. Widget pulldown,
  509. Cmd *cmd
  510. )
  511. {
  512. // if(isValidMenuPane(pulldown) == FALSE)
  513. // return;
  514. CmdInterface *ci;
  515. ci = new ButtonInterface ( pulldown, cmd);
  516. ci->manage();
  517. }
  518. void
  519. MenuBar::changeLabel(
  520. Widget pulldown,
  521. int index,
  522. char * name
  523. )
  524. {
  525. // if(isValidMenuPane(pulldown) == FALSE)
  526. // return;
  527. int managed_widgets, i;
  528. XtArgVal num_children;
  529. WidgetList children;
  530. XmString label;
  531. Widget wid;
  532. XtVaGetValues(pulldown,
  533. XmNnumChildren, &num_children,
  534. NULL);
  535. children = (WidgetList)XtMalloc(sizeof(Widget) * num_children);
  536. XtVaGetValues(pulldown,
  537. XmNchildren, &children,
  538. NULL);
  539. // Some widgets may be unmanaged, so find the real index
  540. for (managed_widgets=0, i=0;
  541. managed_widgets <= index && i < num_children; i++) {
  542. wid = (Widget) children[i];
  543. if (XtIsManaged(wid)) managed_widgets++;
  544. }
  545. if (--i >= num_children) return;
  546. wid = (Widget) children[i];
  547. label = XmStringCreateLocalized(name);
  548. XtVaSetValues(wid,
  549. XmNlabelString, label,
  550. NULL);
  551. XmStringFree(label);
  552. }
  553. void
  554. MenuBar::changeLabel(Widget pulldown,
  555. const char * button_name,
  556. const char * label)
  557. {
  558. // if(isValidMenuPane(pulldown) == FALSE)
  559. // return;
  560. // Locate the appropriate widget in the list.
  561. //
  562. XtArgVal num_children;
  563. char wid_name[200];
  564. XmString label_string = XmStringCreateLocalized((char *)label);
  565. ButtonInterface::mapName(button_name, wid_name);
  566. XtVaGetValues(pulldown,
  567. XmNnumChildren, &num_children,
  568. NULL);
  569. Widget * children = new Widget[num_children];
  570. XtVaGetValues(pulldown,
  571. XmNchildren, &children,
  572. NULL);
  573. for (int wid = 0; wid < num_children; wid++) {
  574. if (strcmp(XtName(children[wid]), wid_name) == 0) {
  575. if (XtIsManaged(children[wid]))
  576. XtVaSetValues(children[wid],
  577. XmNlabelString, label_string,
  578. NULL);
  579. }
  580. }
  581. XmStringFree(label_string);
  582. }
  583. void
  584. MenuBar::rotateLabels(
  585. Widget pulldown,
  586. int startindex,
  587. int endindex
  588. )
  589. {
  590. // if(isValidMenuPane(pulldown) == FALSE)
  591. // return;
  592. int num_managed_wids=0, i, j, num_to_change;
  593. XtArgVal num_children;
  594. WidgetList children;
  595. XmString label, endlabel;
  596. Widget prevwid, wid;
  597. if (startindex < 0 || endindex < 0)
  598. return;
  599. XtVaGetValues(pulldown,
  600. XmNnumChildren, &num_children,
  601. NULL);
  602. if (startindex >= num_children || endindex >= num_children)
  603. return;
  604. num_to_change = endindex - startindex;
  605. if (num_children < num_to_change || num_to_change == 0)
  606. return;
  607. children = (WidgetList)XtMalloc(sizeof(Widget) * num_children);
  608. XtVaGetValues(pulldown,
  609. XmNchildren, &children,
  610. NULL);
  611. // Some of the widgets may be unmanaged: find the first managed widget
  612. for (i = startindex; i < num_children; i++) {
  613. if (XtIsManaged(children[i]))
  614. break;
  615. startindex++;
  616. }
  617. if (startindex == num_children)
  618. return;
  619. // Find the real endindex
  620. endindex = startindex+1;
  621. while (endindex < num_children) {
  622. if (XtIsManaged(children[endindex]))
  623. num_managed_wids++;
  624. if (num_managed_wids == num_to_change)
  625. // We have found the endindex at this point
  626. break;
  627. endindex++;
  628. }
  629. if (endindex == num_children)
  630. return;
  631. wid = (Widget) children[endindex];
  632. XtVaGetValues(wid,
  633. XmNlabelString, &label,
  634. NULL);
  635. endlabel = XmStringCopy(label);
  636. j = i = endindex;
  637. while (i > startindex) {
  638. do {
  639. if (--j < startindex) break;
  640. prevwid = (Widget) children[j];
  641. } while (!XtIsManaged(prevwid));
  642. XtVaGetValues(prevwid,
  643. XmNlabelString, &label,
  644. NULL);
  645. XtVaSetValues(wid,
  646. XmNlabelString, label,
  647. NULL);
  648. i = j;
  649. wid = (Widget) children[i];
  650. }
  651. wid = (Widget) children[startindex];
  652. XtVaSetValues(wid,
  653. XmNlabelString, endlabel,
  654. NULL);
  655. XmStringFree(endlabel);
  656. }
  657. #ifdef DEAD_WOOD
  658. Boolean
  659. MenuBar::isValidMenuPane(Widget w)
  660. {
  661. Boolean retval = FALSE;
  662. Widget parent = w;
  663. while(parent && parent != _w)
  664. parent = XtParent(parent);
  665. if(parent == _w)
  666. retval = TRUE;
  667. return retval;
  668. }
  669. #endif /* DEAD_WOOD */
  670. void
  671. MenuBar::removeCommand(
  672. Widget pulldown,
  673. int index
  674. )
  675. {
  676. //if(isValidMenuPane(pulldown) == FALSE)
  677. // return;
  678. int managed_widgets, i;
  679. XtArgVal num_children;
  680. WidgetList children;
  681. XtVaGetValues(pulldown,
  682. XmNnumChildren, &num_children,
  683. NULL);
  684. children = (WidgetList)XtMalloc(sizeof(Widget) * num_children);
  685. XtVaGetValues(pulldown,
  686. XmNchildren, &children,
  687. NULL);
  688. // Some widgets may be unmanaged, so find the real index
  689. for (managed_widgets=0, i=0;
  690. managed_widgets <= index && i < num_children; i++) {
  691. Widget wid = (Widget) children[i];
  692. if (XtIsManaged(wid)) managed_widgets++;
  693. }
  694. if (--i < num_children)
  695. XtUnmanageChild(children[i]);
  696. }