WmMenu.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. /*
  24. * (c) Copyright 1989, 1990, 1991, 1992, 1993 OPEN SOFTWARE FOUNDATION, INC.
  25. * ALL RIGHTS RESERVED
  26. */
  27. /*
  28. * Motif Release 1.2.3
  29. */
  30. /*
  31. * (c) Copyright 1987, 1988, 1989, 1990 HEWLETT-PACKARD COMPANY */
  32. /*
  33. * (c) Copyright 1987, 1988 DIGITAL EQUIPMENT CORPORATION */
  34. /*
  35. * (c) Copyright 1988 MASSACHUSETTS INSTITUTE OF TECHNOLOGY */
  36. /*
  37. * Included Files:
  38. */
  39. #include "WmGlobal.h"
  40. #include "WmCEvent.h"
  41. #include "WmResource.h"
  42. #include "WmResParse.h"
  43. #include <stdio.h>
  44. #include <X11/Shell.h>
  45. #include <Xm/Xm.h>
  46. #include <Xm/XmP.h>
  47. #include <Xm/CascadeB.h>
  48. #include <Xm/CascadeBG.h>
  49. #include <Xm/Label.h>
  50. #include <Xm/LabelG.h>
  51. #include <Xm/MenuShell.h>
  52. #include <Xm/PushB.h>
  53. #include <Xm/PushBG.h>
  54. #include <Xm/RowColumn.h>
  55. #include <Xm/RowColumnP.h>
  56. #include <Xm/Separator.h>
  57. #include <Xm/SeparatoG.h>
  58. #define SHELL_NAME "menu"
  59. #define SEPARATOR_NAME "separator"
  60. #define TITLE_NAME "title_name"
  61. #define CASCADE_BTN_NAME "cascadebutton"
  62. #define PUSH_BTN_NAME "pushbutton"
  63. #define CHILDREN_CACHE 22
  64. #define MENU_BUTTON_INC 5
  65. /*
  66. * include extern functions
  67. */
  68. #include "WmMenu.h"
  69. #include "WmCDecor.h"
  70. #include "WmColormap.h"
  71. #include "WmEvent.h"
  72. #include "WmFunction.h"
  73. #include "WmIconBox.h"
  74. #include "WmImage.h"
  75. #include "WmError.h"
  76. #include "WmWrkspace.h"
  77. static void UnmapCallback (Widget w, XtPointer client_data,
  78. XtPointer call_data);
  79. static MenuItem *DuplicateMenuItems (MenuItem *menuItems);
  80. /*************************************<->*************************************
  81. *
  82. * MakeMenu (menuName, initialContext, accelContext, moreMenuItems,
  83. * fSystemMenu)
  84. *
  85. *
  86. * Description:
  87. * -----------
  88. * This function makes a menu widget.
  89. *
  90. *
  91. * Inputs:
  92. * ------
  93. * menuName = name of the top-level menu pane for the menu
  94. * initialContext = initial context for menuitem sensitivity
  95. * accelContext = accelerator context
  96. * moreMenuItems = additional menuitems for custom menu.
  97. * fSystemMenu = TRUE iff the menu is a client system menu.
  98. *
  99. *
  100. * Outputs:
  101. * -------
  102. * Return = pointer to a MenuSpec structure with updated currentContext,
  103. * menuWidget, and menuButtons members.
  104. *
  105. *
  106. * Comments:
  107. * --------
  108. * If moreMenuItems is nonNULL, a custom MenuSpec will be created, with
  109. * menuItem member pointing to moreMenuItems. The menuItems for the
  110. * standard MenuSpec of the same name and the moreMenuItems list will be
  111. * used to create menubuttons, and the menu widget will be separate from
  112. * any existing standard menu widget.
  113. *
  114. * When the client is destroyed, this custom MenuSpec, its menuItem and
  115. * menuButton lists, and its menu widget should be freed.
  116. *
  117. *************************************<->***********************************/
  118. MenuSpec *MakeMenu (WmScreenData *pSD,
  119. String menuName, Context initialContext,
  120. Context accelContext, MenuItem *moreMenuItems,
  121. Boolean fSystemMenu)
  122. {
  123. unsigned int n;
  124. MenuSpec *menuSpec;
  125. MenuSpec *newMenuSpec;
  126. MenuItem *menuItem;
  127. KeySpec *accelKeySpec;
  128. if ((menuName == NULL) || (pSD == NULL))
  129. {
  130. return (NULL);
  131. }
  132. /*
  133. * Look for the menu specification:
  134. */
  135. menuSpec = pSD->menuSpecs;
  136. while (menuSpec)
  137. {
  138. if ((menuSpec->name != NULL) && !strcmp (menuSpec->name, menuName))
  139. /* Found the menu pane. */
  140. {
  141. break;
  142. }
  143. menuSpec = menuSpec->nextMenuSpec;
  144. }
  145. if (menuSpec == NULL)
  146. /* the menuSpecs list is exhausted */
  147. {
  148. MWarning(((char *)GETMESSAGE(48, 1, "Menu specification %s not found\n")), menuName);
  149. return (NULL);
  150. }
  151. /*
  152. * The top-level menu pane specification was found.
  153. * Adjust the menu accelerator context?
  154. */
  155. if (fSystemMenu)
  156. {
  157. accelContext = 0;
  158. }
  159. else if (accelContext & F_CONTEXT_ROOT)
  160. /* root context accelerators apply everywhere */
  161. {
  162. accelContext = F_CONTEXT_ALL;
  163. }
  164. /*
  165. * If making a custom menu, create a custom copy of the specification with
  166. * which to build the custom menu.
  167. * Otherwise, if the menu widget exists, possibly modify the accelerator
  168. * contexts and return the specification.
  169. */
  170. if (moreMenuItems != NULL)
  171. {
  172. if ((newMenuSpec = (MenuSpec *) XtMalloc (sizeof (MenuSpec))) == NULL)
  173. /* Handle insufficent memory */
  174. {
  175. MWarning(((char *)GETMESSAGE(48, 2, "Insufficient memory for menu %s\n")), menuName);
  176. return (NULL);
  177. }
  178. newMenuSpec->name = NULL; /* distinguishes this as custom */
  179. newMenuSpec->whichButton = SELECT_BUTTON;
  180. newMenuSpec->height = 0;
  181. newMenuSpec->menuItems = menuSpec->menuItems; /* temporary */
  182. newMenuSpec->accelContext = menuSpec->accelContext;
  183. newMenuSpec->accelKeySpecs = NULL;
  184. newMenuSpec->nextMenuSpec = NULL;
  185. menuSpec = newMenuSpec;
  186. }
  187. else if (menuSpec->menuWidget)
  188. {
  189. /*
  190. * OR the accelContext into the accelerators, if necessary.
  191. */
  192. if (accelContext != (menuSpec->accelContext & accelContext))
  193. {
  194. menuSpec->accelContext |= accelContext;
  195. accelKeySpec = menuSpec->accelKeySpecs;
  196. while (accelKeySpec)
  197. {
  198. accelKeySpec->context |= accelContext;
  199. accelKeySpec = accelKeySpec->nextKeySpec;
  200. }
  201. }
  202. return (menuSpec);
  203. }
  204. /*
  205. * We have a menu specification with which to build the menu.
  206. * Set the initial and accelerator contexts -- they are needed within
  207. * CreateMenuWidget.
  208. */
  209. menuSpec->currentContext = initialContext;
  210. menuSpec->accelContext = accelContext;
  211. /*
  212. * Scan the toplevel MenuSpec and create its initial menuButtons array
  213. * if any of its items will need to be included. This array will be
  214. * created or enlarged within CreateMenuWidget below if necessary.
  215. */
  216. n = 0;
  217. menuItem = menuSpec->menuItems;
  218. while (menuItem)
  219. {
  220. if ((menuItem->greyedContext) || (menuItem->mgtMask))
  221. {
  222. n++;
  223. }
  224. menuItem = menuItem->nextMenuItem;
  225. }
  226. menuItem = moreMenuItems;
  227. while (menuItem)
  228. {
  229. if ((menuItem->greyedContext) || (menuItem->mgtMask))
  230. {
  231. n++;
  232. }
  233. menuItem = menuItem->nextMenuItem;
  234. }
  235. if (n)
  236. {
  237. if ((menuSpec->menuButtons =
  238. (MenuButton *) XtMalloc (n * sizeof(MenuButton))) == NULL)
  239. /* insufficent memory */
  240. {
  241. MWarning(((char *)GETMESSAGE(48, 3, "Insufficient memory for menu %s\n")), menuName);
  242. return (NULL);
  243. }
  244. menuSpec->menuButtonSize = n;
  245. }
  246. else
  247. {
  248. menuSpec->menuButtons = NULL;
  249. menuSpec->menuButtonSize = 0;
  250. }
  251. menuSpec->menuButtonCount = 0;
  252. /*
  253. * Create a PopupShell widget as a child of the workspace manager widget
  254. * and a PopupMenu as a child of the shell.
  255. * Fill the PopupMenu with the menu items.
  256. */
  257. menuSpec->menuWidget = CREATE_MENU_WIDGET (pSD, pCD, menuName,
  258. pSD->screenTopLevelW,
  259. TRUE, menuSpec, moreMenuItems);
  260. if (menuSpec->menuWidget == NULL)
  261. {
  262. /*
  263. * Could not make the top-level menu pane.
  264. */
  265. return (NULL);
  266. }
  267. /*
  268. _XmSetPopupMenuClick(menuSpec->menuWidget, False);
  269. */
  270. /* Return the top MenuSpec */
  271. return (menuSpec);
  272. } /* END OF FUNCTION MakeMenu */
  273. /*************************************<->***********************************/
  274. void CheckTerminalSeparator(MenuSpec *menuSpec, Widget buttonWidget, Boolean manage)
  275. {
  276. CompositeWidget cw;
  277. WidgetList children;
  278. Cardinal wPos;
  279. if ((menuSpec == NULL) || (menuSpec->menuWidget == NULL))
  280. {
  281. return;
  282. }
  283. cw = (CompositeWidget)menuSpec->menuWidget;
  284. children = cw->composite.children;
  285. for (wPos = 0; wPos < cw->composite.num_children; wPos++)
  286. {
  287. if((Widget)children[wPos] == buttonWidget)
  288. {
  289. break;
  290. }
  291. }
  292. if(wPos > 0 &&
  293. XtClass((Widget) children[wPos -1]) == xmSeparatorGadgetClass)
  294. {
  295. if(manage)
  296. {
  297. if (!(XtIsManaged((Widget)children[wPos -1])))
  298. {
  299. XtManageChild((Widget)children[wPos -1]);
  300. }
  301. }
  302. else
  303. {
  304. if (XtIsManaged((Widget)children[wPos -1]))
  305. {
  306. XtUnmanageChild((Widget)children[wPos -1]);
  307. }
  308. }
  309. }
  310. } /* END OF FUNCTION CheckTerminalSeparator */
  311. /*************************************<->*************************************
  312. *
  313. * DuplicateMenuItems (menuItems)
  314. *
  315. *
  316. *
  317. * Description:
  318. * -----------
  319. * This function creates an indentical duplicate of the given menuItems
  320. * list.
  321. *
  322. *
  323. * Inputs:
  324. * ------
  325. * menuItems = the linked list of menuItems to duplicate
  326. *
  327. *
  328. * Outputs:
  329. * -------
  330. * Return = pointer to a new MenuItems list, identical to the original
  331. *
  332. *
  333. * Comments:
  334. * --------
  335. *
  336. *************************************<->***********************************/
  337. static MenuItem *
  338. DuplicateMenuItems (MenuItem *menuItems)
  339. {
  340. MenuItem *newMenuItem = NULL, *returnMenuItem = NULL, *curMenuItem;
  341. for (curMenuItem = menuItems;
  342. curMenuItem != (MenuItem *) NULL;
  343. curMenuItem = curMenuItem->nextMenuItem)
  344. {
  345. /* If its the first one ... */
  346. if (newMenuItem == (MenuItem *) NULL)
  347. {
  348. newMenuItem = (MenuItem *)XtMalloc(sizeof(MenuItem));
  349. returnMenuItem = newMenuItem;
  350. }
  351. else /* ... otherwise, get the next menuItem. */
  352. {
  353. newMenuItem->nextMenuItem =
  354. (MenuItem *)XtMalloc(sizeof(MenuItem));
  355. newMenuItem = newMenuItem->nextMenuItem;
  356. }
  357. newMenuItem->labelType = curMenuItem->labelType;
  358. if (curMenuItem->label != (String) NULL)
  359. newMenuItem->label = XtNewString(curMenuItem->label);
  360. else
  361. newMenuItem->label = NULL;
  362. newMenuItem->labelBitmapIndex = curMenuItem->labelBitmapIndex;
  363. newMenuItem->mnemonic = curMenuItem->mnemonic;
  364. newMenuItem->accelState = curMenuItem->accelState;
  365. newMenuItem->accelKeyCode = curMenuItem->accelKeyCode;
  366. if (curMenuItem->accelText != (String) NULL)
  367. newMenuItem->accelText = XtNewString(curMenuItem->accelText);
  368. else
  369. newMenuItem->accelText = NULL;
  370. newMenuItem->wmFunction = curMenuItem->wmFunction;
  371. if ((curMenuItem->wmFunction == F_Send_Msg)
  372. || (curMenuItem->wmFunction == F_Set_Context)
  373. /*
  374. * NOTE: For now, in dtwm this function is used only
  375. * to copy the FrontPanel menu. So, we know that
  376. * curMenuItem->wmFuncArgs isn't going anywhere,
  377. * so it's safe to simply point at it. If at some
  378. * point it becomes possible that curMenuItem->wmFuncArgs
  379. * can go away, we'll need to make a (deep) copy of
  380. * the WmActionArg. 11/20/96
  381. */
  382. || (curMenuItem->wmFunction == F_Action)
  383. )
  384. newMenuItem->wmFuncArgs = curMenuItem->wmFuncArgs;
  385. else if (curMenuItem->wmFuncArgs != (String) NULL)
  386. newMenuItem->wmFuncArgs = XtNewString(curMenuItem->wmFuncArgs);
  387. else
  388. newMenuItem->wmFuncArgs = NULL;
  389. newMenuItem->greyedContext = curMenuItem->greyedContext;
  390. newMenuItem->mgtMask = curMenuItem->mgtMask;
  391. newMenuItem->nextMenuItem = (MenuItem *) NULL;
  392. }
  393. return(returnMenuItem);
  394. }
  395. /*************************************<->*************************************
  396. *
  397. * DuplicateMenuSpec (menuSpec)
  398. *
  399. *
  400. *
  401. * Description:
  402. * -----------
  403. * This function creates an indentical duplicate of the given menuSpec.
  404. * The menuItems list in the menuSpec is also duplicated.
  405. *
  406. *
  407. * Inputs:
  408. * ------
  409. * menuSpec = the menuSpec to duplicate
  410. *
  411. *
  412. * Outputs:
  413. * -------
  414. * Return = pointer to a new MenuSpec structure with the same field
  415. * values as the original
  416. *
  417. *
  418. * Comments:
  419. * --------
  420. * A new MenuSpec structure is allocated. Most of he fields of the new
  421. * structure are set to the same values as the passed in menuSpec.
  422. * There are some differences between the two final structures.
  423. * One difference: any fields related to push buttons and other
  424. * widgets are left blank in the new MenuSpec to be filled in later.
  425. *
  426. *************************************<->***********************************/
  427. MenuSpec *
  428. DuplicateMenuSpec (MenuSpec *menuSpec)
  429. {
  430. MenuSpec *newMenuSpec;
  431. if ((newMenuSpec = (MenuSpec *) XtMalloc (sizeof (MenuSpec))) == NULL)
  432. /* Handle insufficent memory */
  433. {
  434. Warning((char *)GETMESSAGE(48, 9,
  435. "Insufficient memory for menu specification\n"));
  436. return (NULL);
  437. }
  438. newMenuSpec->name = XtNewString(menuSpec->name);
  439. newMenuSpec->currentContext = menuSpec->currentContext;
  440. newMenuSpec->menuWidget = (Widget) NULL;
  441. newMenuSpec->whichButton = menuSpec->whichButton;
  442. newMenuSpec->height = menuSpec->height;
  443. newMenuSpec->menuItems = DuplicateMenuItems(menuSpec->menuItems);
  444. newMenuSpec->menuButtons = (MenuButton *) NULL;
  445. newMenuSpec->menuButtonSize = 0;
  446. newMenuSpec->menuButtonCount = 0;
  447. newMenuSpec->accelContext = menuSpec->accelContext;
  448. newMenuSpec->accelKeySpecs = (KeySpec *) NULL;
  449. newMenuSpec->nextMenuSpec = (MenuSpec *) NULL;
  450. return(newMenuSpec);
  451. }
  452. /*************************************<->*************************************
  453. *
  454. * static Boolean
  455. * AdjustPBs (menuSpec, pCD, newContext)
  456. *
  457. *
  458. * Description:
  459. * -----------
  460. * This procedure adjusts menu PushButton sensitivities and manage/unmanaged
  461. * status for a toplevel menu.
  462. *
  463. *
  464. * Inputs:
  465. * ------
  466. * menuSpec = nonNULL toplevel menu specification with gadget
  467. * pCD = client data
  468. * newContext = context that the menu is to be posted under.
  469. *
  470. *
  471. * Outputs:
  472. * -------
  473. * menuSpec = menu specification with modifications
  474. * Return = TRUE iff at least one menu item changed manage status.
  475. *
  476. *
  477. * Comments:
  478. * --------
  479. * Adjusts PushButton sensitivity according to context and function type.
  480. * Manages/Unmanages PushButtons according to clientFunction resource.
  481. *
  482. *************************************<->***********************************/
  483. static Boolean AdjustPBs (MenuSpec *menuSpec, ClientData *pCD,
  484. Context newContext)
  485. {
  486. MenuButton *menuButton;
  487. MenuItem *menuItem;
  488. int msgc;
  489. unsigned int n;
  490. long *pMsg;
  491. Boolean fSupported;
  492. Boolean fChangeManaged = FALSE;
  493. /*
  494. * Set PushButton sensitivity.
  495. * Set f.send_msg button sensitivity according to context and client
  496. * message list. Adjust other button sensitivities only for context.
  497. */
  498. /* check for bad input value - shouldn't happen. */
  499. if (menuSpec == NULL) return (FALSE);
  500. for (n = 0, menuButton = menuSpec->menuButtons;
  501. n < menuSpec->menuButtonCount;
  502. n++, menuButton++)
  503. {
  504. menuItem = menuButton->menuItem;
  505. if (menuItem->wmFunction == F_Send_Msg)
  506. /* f.send_msg button: set according to context and message. */
  507. {
  508. if ((newContext & menuItem->greyedContext) ||
  509. !(pCD && pCD->mwmMessagesCount && pCD->mwmMessages))
  510. /* insensitive context or empty client message list */
  511. {
  512. XtSetSensitive (menuButton->buttonWidget, FALSE);
  513. }
  514. else
  515. /*
  516. * Have a context sensitive f.send_msg item and a client with a
  517. * nonempty message list. Set sensitive only if the message is
  518. * supported by this client. Otherwise set insensitive.
  519. */
  520. {
  521. msgc = pCD->mwmMessagesCount;
  522. pMsg = pCD->mwmMessages;
  523. fSupported = FALSE;
  524. while (msgc--)
  525. /* scan nonempty message list */
  526. {
  527. if (*pMsg == (long) menuItem->wmFuncArgs)
  528. /* found match */
  529. {
  530. fSupported = TRUE;
  531. break;
  532. }
  533. pMsg++; /* next message in list */
  534. }
  535. XtSetSensitive (menuButton->buttonWidget, fSupported);
  536. }
  537. }
  538. else
  539. /*
  540. * Non f.send_msg button:
  541. * Adjust sensitivity according to context.
  542. * Manage/Unmanage according to clientFunction.
  543. */
  544. {
  545. if (menuSpec->currentContext & menuItem->greyedContext)
  546. /* button is currently insensitive */
  547. {
  548. if (!(newContext & menuItem->greyedContext))
  549. /* insensitive -> sensitive */
  550. {
  551. XtSetSensitive (menuButton->buttonWidget, TRUE);
  552. }
  553. }
  554. else
  555. /* button is currently sensitive */
  556. {
  557. if (newContext & menuItem->greyedContext)
  558. /* sensitive -> insensitive */
  559. {
  560. XtSetSensitive (menuButton->buttonWidget, FALSE);
  561. }
  562. }
  563. if (menuItem->wmFunction == F_Remove)
  564. {
  565. /*
  566. * Only allow remove from workspace if the client
  567. * is in more than one workspace
  568. */
  569. fSupported = (pCD && (pCD->numInhabited > 1));
  570. XtSetSensitive (menuButton->buttonWidget, fSupported);
  571. }
  572. if ((menuItem->mgtMask) && pCD)
  573. /* PushButton might not apply */
  574. {
  575. if ((pCD->clientFunctions & menuItem->mgtMask & MWM_MGT_MASK) ||
  576. (pCD->dtwmFunctions & menuItem->mgtMask & DTWM_MGT_MASK))
  577. /* function applies -- manage it */
  578. {
  579. if (!menuButton->managed)
  580. /* unmanaged -> managed */
  581. {
  582. XtManageChild (menuButton->buttonWidget);
  583. menuButton->managed = TRUE;
  584. fChangeManaged = TRUE;
  585. if (n == menuSpec->menuButtonCount - 1)
  586. {
  587. /*
  588. * last item, if it has a separator before
  589. * it, manage the separator
  590. */
  591. CheckTerminalSeparator(menuSpec,
  592. menuButton->buttonWidget,
  593. True);
  594. }
  595. }
  596. }
  597. else
  598. /* function does not apply -- unmanage it */
  599. {
  600. if (menuButton->managed)
  601. /* managed -> unmanaged */
  602. {
  603. XtUnmanageChild (menuButton->buttonWidget);
  604. menuButton->managed = FALSE;
  605. fChangeManaged = TRUE;
  606. if (n == menuSpec->menuButtonCount - 1)
  607. {
  608. /*
  609. * last item, if it has a separator before
  610. * it, unmanage the separator
  611. */
  612. CheckTerminalSeparator(menuSpec,
  613. menuButton->buttonWidget,
  614. False);
  615. }
  616. }
  617. }
  618. }
  619. else if (!menuButton->managed)
  620. /* unmanaged PushButton applies */
  621. {
  622. XtManageChild (menuButton->buttonWidget);
  623. menuButton->managed = TRUE;
  624. fChangeManaged = TRUE;
  625. }
  626. }
  627. }
  628. return (fChangeManaged);
  629. } /* END OF FUNCTION AdjustPBs */
  630. /*************************************<->*************************************
  631. *
  632. * static Boolean
  633. * SavePBInfo (topMenuSpec, menuItem, itemW)
  634. *
  635. *
  636. * Description:
  637. * -----------
  638. * Fills a MenuButton structure for a PushButton.
  639. * If necessary, mallocs or reallocs the menuButtons array in the toplevel
  640. * MenuSpec.
  641. *
  642. *
  643. * Inputs:
  644. * ------
  645. * topMenuSpec = pointer to toplevel MenuSpec structure
  646. * menuItem = pointer to PushButton MenuItem structure
  647. * itemW = PushButton gadget
  648. * topMenuSpec->menuButtons[]
  649. * topMenuSpec->menuButtonSize
  650. * topMenuSpec->menuButtonCount
  651. *
  652. *
  653. * Outputs:
  654. * -------
  655. * Return = FALSE iff insufficient memory for malloc or realloc
  656. * or bad input value forces exit.
  657. * topMenuSpec->menuButtons[]
  658. * topMenuSpec->menuButtonSize
  659. * topMenuSpec->menuButtonCount
  660. *
  661. *
  662. * Comments:
  663. * --------
  664. * The initial managed status of PushButtons is TRUE.
  665. *
  666. *************************************<->***********************************/
  667. static Boolean SavePBInfo (MenuSpec *topMenuSpec, MenuItem *menuItem,
  668. Widget itemW)
  669. {
  670. MenuButton *menuButton;
  671. /* check for bad input value - shouldn't happen. */
  672. if (topMenuSpec == NULL) return (FALSE);
  673. if (topMenuSpec->menuButtonSize == 0)
  674. /* need to create array */
  675. {
  676. topMenuSpec->menuButtonSize = MENU_BUTTON_INC;
  677. topMenuSpec->menuButtons =
  678. (MenuButton *) XtMalloc (MENU_BUTTON_INC * sizeof(MenuButton));
  679. }
  680. else if (topMenuSpec->menuButtonCount == topMenuSpec->menuButtonSize)
  681. /* need larger array */
  682. {
  683. topMenuSpec->menuButtonSize += MENU_BUTTON_INC;
  684. topMenuSpec->menuButtons = (MenuButton *)
  685. XtRealloc ((char*)topMenuSpec->menuButtons,
  686. topMenuSpec->menuButtonSize * sizeof(MenuButton));
  687. }
  688. if (topMenuSpec->menuButtons == NULL)
  689. /* insufficent memory */
  690. {
  691. topMenuSpec->menuButtonSize = 0;
  692. topMenuSpec->menuButtonCount = 0;
  693. return (FALSE);
  694. }
  695. menuButton = &(topMenuSpec->menuButtons[topMenuSpec->menuButtonCount]);
  696. topMenuSpec->menuButtonCount++;
  697. menuButton->menuItem = menuItem;
  698. menuButton->buttonWidget = itemW;
  699. menuButton->managed = TRUE;
  700. return (TRUE);
  701. }
  702. /*************************************<->*************************************
  703. *
  704. * CreateMenuWidget (pSD, menuName, parent, fTopLevelPane, topMenuSpec,
  705. * moreMenuItems)
  706. *
  707. *
  708. * Description:
  709. * -----------
  710. * Creates a MenuShell as a child of the specified parent widget, and a
  711. * PopupMenu or PulldownMenu as a child of the shell. Fill the menu with
  712. * the named menupane items.
  713. *
  714. *
  715. * Inputs:
  716. * ------
  717. * pSD ---------- pointer to screen data
  718. * menuName ----- the name of the menu specification to be used to create
  719. * the menu widget.
  720. * parent -------- parent of popup shell
  721. * fTopLevelPane - TRUE iff the menupane is a top level one
  722. * topMenuSpec --- pointer to the top menu specification.
  723. * moreMenuItems - pointer to additional menu items for custom menu.
  724. *
  725. *
  726. * Outputs:
  727. * -------
  728. * Return = created PopupMenu or PulldownMenu widget, or NULL.
  729. *
  730. *
  731. * Comments:
  732. * --------
  733. * We attach a popdowncallback to the menu to set wmGD.menuActive to NULL,
  734. * allowing us to not dispatch key events separately from the toolkit
  735. * dispatcher.
  736. *
  737. *************************************<->***********************************/
  738. typedef struct _StrList
  739. {
  740. XmString string;
  741. struct _StrList *next;
  742. } StrList;
  743. Widget CreateMenuWidget (WmScreenData *pSD,
  744. String menuName, Widget parent,
  745. Boolean fTopLevelPane, MenuSpec *topMenuSpec,
  746. MenuItem *moreMenuItems)
  747. {
  748. int i, n;
  749. Arg sepArgs[1];
  750. Arg args[10];
  751. MenuSpec *menuSpec = (MenuSpec *)NULL;
  752. MenuItem *menuItem;
  753. Widget menuShellW;
  754. Widget menuW;
  755. Widget subMenuW;
  756. Widget children[CHILDREN_CACHE];
  757. Pixmap labelPixmap;
  758. KeySpec *accelKeySpec;
  759. Dimension menuHeight;
  760. Boolean fUseTitleSep = False;
  761. StrList *stringsToFree = NULL, *sPtr;
  762. XmString tmpstr;
  763. #ifndef IBM_151913
  764. Screen *scr;
  765. #endif
  766. /* check for bad input values. */
  767. if ((menuName == NULL) || (pSD == NULL))
  768. {
  769. return (NULL);
  770. }
  771. /*
  772. * Find the menu pane specifications for menuName.
  773. * The top-level menu specification is passed as an argument (it may
  774. * be custom). A submenu specification must be found and might not exist.
  775. * Return NULL if a submenu specification is not found.
  776. */
  777. if (fTopLevelPane)
  778. {
  779. menuSpec = topMenuSpec;
  780. }
  781. else
  782. {
  783. menuSpec = pSD->menuSpecs;
  784. while (menuSpec)
  785. {
  786. if ((menuSpec->name != NULL) && !strcmp (menuSpec->name, menuName))
  787. {
  788. break; /* found menuName's specification */
  789. }
  790. menuSpec = menuSpec->nextMenuSpec; /* keep looking */
  791. }
  792. }
  793. if (menuSpec == NULL)
  794. /* (submenu) specification not found */
  795. {
  796. MWarning(((char *)GETMESSAGE(48, 4, "Menu specification %s not found\n")), menuName);
  797. return (NULL);
  798. }
  799. /*
  800. * If menuSpec is marked, we have menu recursion => fail.
  801. * Otherwise, mark it.
  802. */
  803. if (menuSpec->currentContext & CR_MENU_MARK) /* marked? */
  804. /* menu recursion */
  805. {
  806. MWarning(((char *)GETMESSAGE(48, 5, "Menu recursion detected for %s\n")), menuName);
  807. return (NULL);
  808. }
  809. menuSpec->currentContext |= CR_MENU_MARK; /* no, mark it */
  810. /*
  811. * Create a PopupShell widget.
  812. * If the parent of the specified parent ("grandparent") is a MenuShell
  813. * widget, then use the grandparent as the parent of the PopupShell.
  814. * Otherwise, use the specified parent.
  815. */
  816. i = 0;
  817. XtSetArg (args[i], XmNwidth, (XtArgVal) 5); i++;
  818. XtSetArg (args[i], XmNheight, (XtArgVal) 5); i++;
  819. XtSetArg (args[i], XmNallowShellResize, (XtArgVal) TRUE); i++;
  820. XtSetArg (args[i], XtNoverrideRedirect, (XtArgVal) TRUE); i++;
  821. XtSetArg (args[i], XtNdepth,
  822. (XtArgVal) DefaultDepth(XtDisplay(parent), pSD->screen)); i++;
  823. XtSetArg (args[i], XtNscreen,
  824. (XtArgVal) ScreenOfDisplay(XtDisplay(parent), pSD->screen)); i++;
  825. if ((XtParent (parent) != NULL) && XmIsMenuShell (XtParent (parent)))
  826. {
  827. parent = XtParent (parent);
  828. }
  829. menuShellW = XtCreatePopupShell (SHELL_NAME, xmMenuShellWidgetClass,
  830. parent, (ArgList) args, i);
  831. /*
  832. * Create a RowColumn widget as a child of the shell for the menu pane.
  833. * If the menu pane is top-level, create a popup menu for it and attach
  834. * the unmap callback to it.
  835. * Otherwise, create a pulldown menu for it.
  836. */
  837. i = 0;
  838. XtSetArg (args[i], XmNborderWidth, (XtArgVal) 0); i++;
  839. XtSetArg (args[i], XmNwhichButton, (XtArgVal) SELECT_BUTTON); i++;
  840. XtSetArg (args[i], XmNadjustMargin, (XtArgVal) TRUE); i++;
  841. if (fTopLevelPane)
  842. {
  843. XtSetArg (args[i], XmNrowColumnType, (XtArgVal) XmMENU_POPUP); i++;
  844. XtSetArg (args[i], XmNpopupEnabled, (XtArgVal) TRUE); i++;
  845. menuW = XtCreateWidget (menuName, xmRowColumnWidgetClass, menuShellW,
  846. (ArgList) args, i);
  847. XtAddCallback (menuW, XmNunmapCallback, UnmapCallback,
  848. (XtPointer) menuSpec);
  849. }
  850. else
  851. {
  852. XtSetArg (args[i], XmNrowColumnType, (XtArgVal) XmMENU_PULLDOWN); i++;
  853. menuW = XtCreateWidget (menuName, xmRowColumnWidgetClass, menuShellW,
  854. (ArgList) args, i);
  855. }
  856. /*
  857. * Create the specified menu entries as children of the menupane.
  858. * Menus may contain the following widgets:
  859. *
  860. * Label
  861. * Separator
  862. * CascadeButton
  863. * PushButton
  864. *
  865. * Add separator gadgets around menu titles.
  866. */
  867. XtSetArg (sepArgs[0], XmNseparatorType, (XtArgVal) XmDOUBLE_LINE);
  868. n = 0;
  869. menuItem = menuSpec->menuItems;
  870. if ((menuItem == NULL) && (moreMenuItems != NULL))
  871. /* handle custom menu with empty standard specification */
  872. {
  873. menuSpec->menuItems = menuItem = moreMenuItems;
  874. moreMenuItems = NULL;
  875. }
  876. while (menuItem)
  877. {
  878. i = 0;
  879. if (menuItem->wmFunction == F_Separator)
  880. /*
  881. * Add a Separator gadget for a menu separator.
  882. * An immediately following title will not have a top separator.
  883. */
  884. {
  885. {
  886. children[n] =
  887. XmCreateSeparatorGadget (menuW, SEPARATOR_NAME,
  888. (ArgList)NULL, 0);
  889. fUseTitleSep = FALSE;
  890. }
  891. } /* F_Separator */
  892. else
  893. /*
  894. * We will use one of:
  895. *
  896. * Label
  897. * CascadeButton
  898. * PushButton
  899. */
  900. {
  901. /*
  902. * Construct the label
  903. */
  904. if ((menuItem->labelType == XmPIXMAP) &&
  905. (labelPixmap =
  906. MakeCachedLabelPixmap (pSD, menuW,
  907. menuItem->labelBitmapIndex)))
  908. {
  909. XtSetArg (args[i], XmNlabelType, (XtArgVal) XmPIXMAP); i++;
  910. XtSetArg (args[i], XmNlabelPixmap, (XtArgVal) labelPixmap); i++;
  911. XtSetArg (args[i], XmNlabelInsensitivePixmap,
  912. (XtArgVal) labelPixmap); i++;
  913. }
  914. else
  915. {
  916. XtSetArg (args[i], XmNlabelType, (XtArgVal) XmSTRING); i++;
  917. XtSetArg (args[i], XmNlabelString, (XtArgVal)
  918. (tmpstr = XmStringCreateLocalized(menuItem->label))); i++;
  919. sPtr = (StrList *) XtMalloc(sizeof(StrList));
  920. if (sPtr == NULL)
  921. {
  922. MWarning(((char *)GETMESSAGE(48, 2, "Insufficient memory for menu %s\n")), menuName);
  923. return (NULL);
  924. }
  925. else
  926. {
  927. sPtr->string = tmpstr;
  928. sPtr->next = stringsToFree;
  929. stringsToFree = sPtr;
  930. }
  931. }
  932. if (menuItem->wmFunction == F_Title)
  933. /*
  934. * Add a centered Label gadget for a menu title.
  935. * Include separators above and below the title.
  936. * Don't include the top one if the title is the first pane item
  937. * or immediately follows a user-supplied separator.
  938. */
  939. {
  940. if (fUseTitleSep)
  941. {
  942. children[n] =
  943. XmCreateSeparatorGadget (menuW, SEPARATOR_NAME,
  944. sepArgs, 1); n++;
  945. }
  946. XtSetArg (args[i], XmNalignment, XmALIGNMENT_CENTER); i++;
  947. children[n] = XmCreateLabelGadget (menuW, TITLE_NAME,
  948. (ArgList) args, i); n++;
  949. children[n] = XmCreateSeparatorGadget (menuW, SEPARATOR_NAME,
  950. sepArgs, 1);
  951. /*
  952. * A following title will have both separators.
  953. */
  954. fUseTitleSep = TRUE;
  955. }
  956. else
  957. /*
  958. * We will use one of:
  959. *
  960. * CascadeButton
  961. * PushButton
  962. *
  963. * Both support mnemonics; only PushButtons support accelerators.
  964. */
  965. {
  966. /*
  967. * Align text on the left.
  968. * Set any mnemonic text.
  969. */
  970. XtSetArg (args[i], XmNalignment, XmALIGNMENT_BEGINNING); i++;
  971. if (menuItem->mnemonic)
  972. {
  973. XtSetArg (args[i], XmNmnemonic,
  974. (XtArgVal) menuItem->mnemonic); i++;
  975. }
  976. if (menuItem->wmFunction == F_Menu)
  977. /*
  978. * Create a PopupShell and PulldownMenu for a submenu (the
  979. * menushells are linked together).
  980. * Create a CascadeButton Widget
  981. * The submenu widget is attached to the CascadeButton gadget
  982. * using the subMenuId resource.
  983. * Make the CascadeButton insensitive if the submenu cannot be
  984. * created.
  985. */
  986. {
  987. subMenuW = CREATE_MENU_WIDGET (pSD, pCD,
  988. menuItem->wmFuncArgs, menuW,
  989. FALSE, topMenuSpec,
  990. (MenuItem *)NULL);
  991. if (subMenuW)
  992. /*
  993. * Attach submenu to cascade button.
  994. */
  995. {
  996. XtSetArg (args[i], XmNsubMenuId, (XtArgVal) subMenuW);
  997. i++;
  998. children[n] = XmCreateCascadeButtonGadget (menuW,
  999. CASCADE_BTN_NAME, (ArgList) args, i);
  1000. }
  1001. else
  1002. /*
  1003. * Unable to create submenupane: make the entry insensitive.
  1004. */
  1005. {
  1006. children[n] = XmCreateCascadeButtonGadget (menuW,
  1007. CASCADE_BTN_NAME, (ArgList) args, i);
  1008. XtSetSensitive (children[n], FALSE);
  1009. }
  1010. /*
  1011. * A following title will have both separators.
  1012. */
  1013. fUseTitleSep = TRUE;
  1014. }
  1015. else
  1016. /*
  1017. * Create a PushButton gadget.
  1018. */
  1019. {
  1020. /*
  1021. * If an accelerator is specified, set acceleratorText,
  1022. * then create an accelerator KeySpec and insert it at the
  1023. * head of the toplevel MenuSpec's list.
  1024. */
  1025. if (menuItem->accelText)
  1026. {
  1027. XtSetArg (args[i], XmNacceleratorText, (XtArgVal)
  1028. (tmpstr = XmStringCreateLocalized(menuItem->accelText))); i++;
  1029. sPtr = (StrList *) XtMalloc(sizeof(StrList));
  1030. if (sPtr == NULL)
  1031. {
  1032. MWarning(((char *)GETMESSAGE(48, 2, "Insufficient memory for menu %s\n")), menuName);
  1033. return (NULL);
  1034. }
  1035. else
  1036. {
  1037. sPtr->string = tmpstr;
  1038. sPtr->next = stringsToFree;
  1039. stringsToFree = sPtr;
  1040. }
  1041. if ((accelKeySpec = (KeySpec *)
  1042. XtMalloc (sizeof (KeySpec ))) == NULL)
  1043. /* Handle insufficent memory */
  1044. {
  1045. MWarning (((char *)GETMESSAGE(48, 6, "Insufficient memory for menu %s\n")),
  1046. menuName);
  1047. menuSpec->currentContext &= ~CR_MENU_MARK;
  1048. return (NULL);
  1049. }
  1050. accelKeySpec->state = menuItem->accelState;
  1051. accelKeySpec->keycode = menuItem->accelKeyCode;
  1052. accelKeySpec->context = topMenuSpec->accelContext;
  1053. accelKeySpec->subContext = 0;
  1054. accelKeySpec->wmFunction = menuItem->wmFunction;
  1055. accelKeySpec->wmFuncArgs = menuItem->wmFuncArgs;
  1056. accelKeySpec->nextKeySpec = topMenuSpec->accelKeySpecs;
  1057. topMenuSpec->accelKeySpecs = accelKeySpec;
  1058. }
  1059. children[n] = XmCreatePushButtonGadget (menuW,
  1060. PUSH_BTN_NAME, (ArgList) args, i);
  1061. /*
  1062. * Set sensitivity. Initially we only consider the context
  1063. * of the top level menupane.
  1064. */
  1065. if (menuItem->greyedContext & topMenuSpec->currentContext)
  1066. /* insensitive button in this context*/
  1067. {
  1068. XtSetSensitive (children[n], FALSE);
  1069. }
  1070. else
  1071. /* sensitive button in this context*/
  1072. {
  1073. XtSetSensitive (children[n], TRUE);
  1074. }
  1075. /*
  1076. * If necessary, fill a menuButtons element for this
  1077. * PushButton. Malloc or Realloc the array if necessary.
  1078. */
  1079. if ((menuItem->greyedContext) || (menuItem->mgtMask))
  1080. {
  1081. if (!SavePBInfo (topMenuSpec, menuItem, children[n]))
  1082. {
  1083. MWarning(((char *)GETMESSAGE(48, 7, "Insufficient memory for menu %s\n")),
  1084. menuName);
  1085. menuSpec->currentContext &= ~CR_MENU_MARK;
  1086. return (NULL);
  1087. }
  1088. }
  1089. /*
  1090. * Set up the function callback.
  1091. * A following title will have both separators.
  1092. */
  1093. XtAddCallback (children[n], XmNactivateCallback,
  1094. (XtCallbackProc)ActivateCallback,
  1095. (XtPointer) menuItem);
  1096. fUseTitleSep = TRUE;
  1097. }
  1098. }
  1099. }
  1100. /*
  1101. * Increment the children array count if we actually
  1102. * created a new child.
  1103. */
  1104. n++;
  1105. /*
  1106. * Next menu item: handle custom items and full children[].
  1107. */
  1108. menuItem = menuItem->nextMenuItem;
  1109. if ((menuItem == NULL) && (moreMenuItems != NULL))
  1110. {
  1111. menuSpec->menuItems = menuItem = moreMenuItems;
  1112. moreMenuItems = NULL;
  1113. }
  1114. if (n >= CHILDREN_CACHE - 2) /* leave room for title separators */
  1115. {
  1116. XtManageChildren (children, n);
  1117. n = 0;
  1118. }
  1119. }
  1120. if (n > 0)
  1121. {
  1122. XtManageChildren (children, n);
  1123. }
  1124. /*
  1125. * Get the initial height of the top level menu pane shell.
  1126. * The actual height will change according to clientFunctions.
  1127. */
  1128. if (fTopLevelPane)
  1129. {
  1130. i = 0;
  1131. XtSetArg (args[i], XtNheight, &menuHeight); i++;
  1132. XtGetValues (menuW, (ArgList)args, i);
  1133. topMenuSpec->height = (unsigned int) menuHeight;
  1134. }
  1135. #ifndef IBM_151913
  1136. /*
  1137. * Check if the menu that's been created is higher than the screen.
  1138. * If it is, force it to wrap. Taken straight from the 1.1 fix.
  1139. */
  1140. i = 0;
  1141. XtSetArg (args[i], XtNheight, &menuHeight); i++;
  1142. XtGetValues (menuW, (ArgList)args, i);
  1143. scr = XtScreen (menuW);
  1144. if (menuHeight > (Dimension)scr->height) {
  1145. i = 0;
  1146. XtSetArg (args[i], XmNresizeHeight, (XtArgVal) FALSE); i++;
  1147. XtSetArg (args[i], XmNpacking, (XtArgVal) XmPACK_TIGHT); i++;
  1148. XtSetArg (args[i], XmNorientation, (XtArgVal) XmVERTICAL); i++;
  1149. XtSetArg (args[i], XmNheight, scr->height); i++;
  1150. XtSetValues (menuW, (ArgList)args, i);
  1151. }
  1152. #endif /* IBM_151913 */
  1153. /* free the string that may have been created earlier. */
  1154. for (sPtr = stringsToFree; sPtr != NULL; )
  1155. {
  1156. stringsToFree = stringsToFree->next;
  1157. XmStringFree(sPtr->string);
  1158. XtFree((char *)sPtr);
  1159. sPtr = stringsToFree;
  1160. }
  1161. /* Unmark the menu specification and return. */
  1162. menuSpec->currentContext &= ~CR_MENU_MARK;
  1163. return (menuW);
  1164. } /* END OF FUNCTION CreateMenuWidget */
  1165. /*************************************<->*************************************
  1166. *
  1167. * PostMenu (menuSpec, pCD, x, y, button, newContext, flags, passedInEvent)
  1168. *
  1169. *
  1170. * Description:
  1171. * -----------
  1172. * This function is used to post a menu at a particular location.
  1173. *
  1174. *
  1175. * Inputs:
  1176. * ------
  1177. * menuSpec = menu specification
  1178. * pCD = client data
  1179. * x,y = position to post the menu if (flags & POST_AT_XY) set
  1180. * button = button number posting the menu or NoButton (WmGlobal.h) if
  1181. * posted by a key
  1182. * newContext = context that the menu is to be posted under.
  1183. * flags = POST_AT_XY bit set iff x,y are valid, else compute from pCD
  1184. * POST_TRAVERSAL_ON bit set if set traversal on
  1185. *
  1186. * Outputs:
  1187. * -------
  1188. * menuSpec = menu specification with modifications
  1189. * wmGD.menuClient = pCD
  1190. * wmGD.menuActive = menuSpec
  1191. *
  1192. *
  1193. * Comments:
  1194. * --------
  1195. * Accepts x,y only if POST_AT_XY flag bit set. Otherwise, computes from pCD.
  1196. * Adjusts PushButton sensitivity according to context and function type.
  1197. * Manages/Unmanages PushButtons according to clientFunction resource.
  1198. * Sets traversal on if button==NoButton or POST_TRAVERSAL_ON flag bit set.
  1199. *
  1200. *************************************<->***********************************/
  1201. void PostMenu (MenuSpec *menuSpec, ClientData *pCD, int x, int y, unsigned int button, Context newContext, long flags, XEvent *passedInEvent)
  1202. {
  1203. int i;
  1204. Arg args[3];
  1205. unsigned int whichButton;
  1206. Dimension menuHeight;
  1207. XButtonPressedEvent event;
  1208. Window saveWindow;
  1209. Display *saveDisplay;
  1210. if ((menuSpec == NULL) || (menuSpec->menuWidget == NULL))
  1211. {
  1212. return;
  1213. }
  1214. /*
  1215. * Don't post a menu from an icon in the iconbox if the
  1216. * icon is not visible
  1217. */
  1218. if((newContext == F_SUBCONTEXT_IB_WICON ||
  1219. newContext == F_SUBCONTEXT_IB_IICON) &&
  1220. (!(IconVisible(pCD))))
  1221. {
  1222. return;
  1223. }
  1224. /*
  1225. * Set grabContext to be used in GrabWin when no event is passed
  1226. * to GrabWin.
  1227. */
  1228. wmGD.grabContext = newContext;
  1229. /*
  1230. * Adjust PushButton sensitivity and manage/unmanage status.
  1231. * If the manage status of the system menu has changed,
  1232. * then get the height of the top level menu pane shell and
  1233. * cache it in its MenuSpec.
  1234. *
  1235. * Also...
  1236. * Adjust the tear off control. If we are posting this menu from
  1237. * a client then force the tear off to be disabled. NOTE: This must
  1238. * be done after wmGD.menuClient has been set.
  1239. * Since turning off the tear-off control could result in a height
  1240. * change, we may need to remeasure things. (CR 9316)
  1241. */
  1242. if(pCD && pCD->clientFlags & ICON_BOX)
  1243. {
  1244. newContext |= F_CONTEXT_ICONBOX;
  1245. }
  1246. if (AdjustPBs (menuSpec, pCD, newContext))
  1247. {
  1248. i = 0;
  1249. XtSetArg (args[i], XtNheight, &menuHeight); i++;
  1250. XtGetValues (menuSpec->menuWidget, (ArgList)args, i);
  1251. menuSpec->height = (unsigned int) menuHeight;
  1252. }
  1253. menuSpec->currentContext = newContext;
  1254. /*
  1255. * Adjust the whichButton resource if necessary.
  1256. * Use SELECT_BUTTON for NoButton.
  1257. */
  1258. whichButton = (button == NoButton) ? SELECT_BUTTON : button;
  1259. if (whichButton != menuSpec->whichButton)
  1260. {
  1261. i = 0;
  1262. XtSetArg (args[i], XmNwhichButton, (XtArgVal) whichButton); i++;
  1263. XtSetValues (menuSpec->menuWidget, args, i);
  1264. menuSpec->whichButton = whichButton;
  1265. }
  1266. /*
  1267. * Determine the position of the popup menu.
  1268. * Compute position if necessary (system menu).
  1269. */
  1270. if (pCD && !(flags & POST_AT_XY))
  1271. /* compute the position */
  1272. {
  1273. GetSystemMenuPosition (pCD, &x, &y, menuSpec->height, newContext);
  1274. }
  1275. event.x_root = x;
  1276. event.y_root = y;
  1277. XmMenuPosition (menuSpec->menuWidget, &event);
  1278. wmGD.menuClient = pCD;
  1279. wmGD.menuActive = menuSpec; /* set to NULL within UnmapCallback() */
  1280. /*
  1281. * Post the menu by managing its top-level RowColumn.
  1282. *
  1283. * First dispatch the event to set the time stamp in the toolkit
  1284. */
  1285. if(passedInEvent)
  1286. {
  1287. saveWindow = passedInEvent->xany.window;
  1288. saveDisplay = passedInEvent->xany.display;
  1289. passedInEvent->xany.window = 0;
  1290. passedInEvent->xany.display = XtDisplay(menuSpec->menuWidget);
  1291. XtDispatchEvent(passedInEvent);
  1292. passedInEvent->xany.window = saveWindow;
  1293. passedInEvent->xany.display = saveDisplay;
  1294. /* If menu posted by ButtonPress/ButtonRelease, release grabs. */
  1295. if ((passedInEvent->type == ButtonPress) ||
  1296. (passedInEvent->type == ButtonRelease))
  1297. XUngrabPointer(passedInEvent->xany.display,
  1298. passedInEvent->xbutton.time);
  1299. }
  1300. XtManageChild (menuSpec->menuWidget);
  1301. /*
  1302. * set the traversal state.
  1303. */
  1304. if ((button == NoButton) || (flags & POST_TRAVERSAL_ON))
  1305. /* turn traversal on */
  1306. {
  1307. TraversalOn (menuSpec);
  1308. }
  1309. else
  1310. /* turn traversal off */
  1311. {
  1312. TraversalOff (menuSpec);
  1313. }
  1314. } /* END OF FUNCTION PostMenu */
  1315. /*************************************<->*************************************
  1316. *
  1317. * UnpostMenu (menuSpec)
  1318. *
  1319. *
  1320. * Description:
  1321. * -----------
  1322. * This function is used to unpost a menu.
  1323. *
  1324. *
  1325. * Inputs:
  1326. * ------
  1327. * menuSpec = menu specification
  1328. *
  1329. * Outputs:
  1330. * -------
  1331. * None.
  1332. *
  1333. *
  1334. * Comments:
  1335. * --------
  1336. * wmGD.menuActive and wmGD.menuUnpostKey are set to NULL within
  1337. * UnmapCallback.
  1338. *
  1339. *************************************<->***********************************/
  1340. void UnpostMenu (MenuSpec *menuSpec)
  1341. {
  1342. if (menuSpec && (menuSpec->menuWidget))
  1343. /*
  1344. * Unpost the menu by unmanaging its top-level RowColumn.
  1345. */
  1346. {
  1347. XtUnmanageChild (menuSpec->menuWidget);
  1348. ForceColormapFocus (ACTIVE_PSD, ACTIVE_PSD->colormapFocus);
  1349. }
  1350. } /* END OF FUNCTION UnpostMenu */
  1351. /*************************************<->*************************************
  1352. *
  1353. * ActivateCallback (w, client_data, call_data)
  1354. *
  1355. *
  1356. * Description:
  1357. * -----------
  1358. * This function is called whenever a menu item is selected.
  1359. *
  1360. *
  1361. * Inputs:
  1362. * ------
  1363. * w = menubuttonWidget
  1364. * client_data = pointer to menu button's MenuItem structure
  1365. * call_data = not used
  1366. * wmGD.menuClient = pointer to client's ClientData structure
  1367. *
  1368. *
  1369. * Outputs:
  1370. * -------
  1371. * None.
  1372. *
  1373. *
  1374. * Comments:
  1375. * --------
  1376. * None.
  1377. *
  1378. *************************************<->***********************************/
  1379. void ActivateCallback (Widget w, caddr_t client_data, caddr_t call_data)
  1380. {
  1381. WmScreenData *pSD;
  1382. /* set active screen */
  1383. pSD = GetScreenForWindow (XtWindow(w));
  1384. if (pSD) SetActiveScreen (pSD);
  1385. ((MenuItem *)client_data)->wmFunction (
  1386. ((MenuItem *)client_data)->wmFuncArgs, wmGD.menuClient, NULL);
  1387. } /* END OF FUNCTION ActivateCallback */
  1388. /*************************************<->*************************************
  1389. *
  1390. * UnmapCallback (w, client_data, call_data)
  1391. *
  1392. *
  1393. * Description:
  1394. * -----------
  1395. * This function is called whenever a toplevel RowColumn is unmapped.
  1396. *
  1397. *
  1398. * Inputs:
  1399. * ------
  1400. * w =
  1401. * client_data = not used
  1402. * call_data = not used
  1403. * wmGD.gadgetClient = last client with depressed client
  1404. *
  1405. *
  1406. * Outputs:
  1407. * -------
  1408. * wmGD.menuActive = NULL
  1409. * wmGD.menuUnpostKeySpec = NULL
  1410. * wmGD.checkHotspot = FALSE
  1411. *
  1412. *
  1413. * Comments:
  1414. * --------
  1415. * None.
  1416. *
  1417. *************************************<->***********************************/
  1418. static void UnmapCallback (Widget w, XtPointer client_data,
  1419. XtPointer call_data)
  1420. {
  1421. wmGD.menuActive = NULL;
  1422. wmGD.menuUnpostKeySpec = NULL;
  1423. wmGD.checkHotspot = FALSE;
  1424. if (wmGD.gadgetClient)
  1425. {
  1426. PopGadgetOut(wmGD.gadgetClient, FRAME_SYSTEM);
  1427. }
  1428. ForceColormapFocus (ACTIVE_PSD, ACTIVE_PSD->colormapFocus);
  1429. PullExposureEvents();
  1430. } /* END OF FUNCTION UnmapCallback */
  1431. /*************************************<->*************************************
  1432. *
  1433. * MWarning (message)
  1434. *
  1435. *
  1436. * Description:
  1437. * -----------
  1438. * This function lists a message to stderr.
  1439. *
  1440. *
  1441. * Inputs:
  1442. * ------
  1443. * format = pointer to a format string
  1444. * message = pointer to a message string
  1445. *
  1446. *************************************<->***********************************/
  1447. void MWarning (char *format, char *message)
  1448. {
  1449. if (strlen(format) + strlen(message) < (size_t) MAXWMPATH)
  1450. {
  1451. char pch[MAXWMPATH+1];
  1452. sprintf (pch, format, message);
  1453. Warning (pch);
  1454. }
  1455. } /* END OF FUNCTION MWarning */
  1456. /*************************************<->*************************************
  1457. *
  1458. * TraversalOff (menuSpec)
  1459. *
  1460. *
  1461. * Description:
  1462. * -----------
  1463. * This function turns menu traversal off.
  1464. *
  1465. *
  1466. * Inputs:
  1467. * ------
  1468. * menuSpec = menu specification
  1469. *
  1470. *
  1471. * Outputs:
  1472. * -------
  1473. * None.
  1474. *
  1475. *
  1476. * Comments:
  1477. * --------
  1478. * None.
  1479. *
  1480. *************************************<->***********************************/
  1481. void TraversalOff (MenuSpec *menuSpec)
  1482. {
  1483. if (menuSpec && (menuSpec->menuWidget))
  1484. {
  1485. /* function pointer */
  1486. (*((XmRowColumnWidgetClass) XtClass (menuSpec->menuWidget))
  1487. ->row_column_class.menuProcedures)
  1488. /* argument list */
  1489. (XmMENU_TRAVERSAL, menuSpec->menuWidget, False, NULL, NULL);
  1490. }
  1491. } /* END OF FUNCTION TraversalOff */
  1492. /*************************************<->*************************************
  1493. *
  1494. * TraversalOn (menuSpec)
  1495. *
  1496. *
  1497. * Description:
  1498. * -----------
  1499. * This function turns menu traversal on.
  1500. *
  1501. *
  1502. * Inputs:
  1503. * ------
  1504. * menuSpec = menu specification
  1505. *
  1506. *
  1507. * Outputs:
  1508. * -------
  1509. * None.
  1510. *
  1511. *
  1512. * Comments:
  1513. * --------
  1514. * None.
  1515. *
  1516. *************************************<->***********************************/
  1517. void TraversalOn (MenuSpec *menuSpec)
  1518. {
  1519. if (menuSpec && (menuSpec->menuWidget))
  1520. {
  1521. /* function pointer */
  1522. (*((XmRowColumnWidgetClass) XtClass (menuSpec->menuWidget))
  1523. ->row_column_class.menuProcedures)
  1524. /*argument list */
  1525. (XmMENU_TRAVERSAL, menuSpec->menuWidget, True, NULL, NULL);
  1526. }
  1527. } /* END OF FUNCTION TraversalOn */
  1528. /*************************************<->*************************************
  1529. *
  1530. * FreeCustomMenuSpec (menuSpec)
  1531. *
  1532. *
  1533. * Description:
  1534. * -----------
  1535. * This procedure destroys a custom MenuSpec structure and its associated
  1536. * menu widget, menuItems list, menuButtons array, and menu accelerator list.
  1537. *
  1538. *
  1539. * Inputs:
  1540. * ------
  1541. * menuSpec = MenuSpec structure
  1542. *
  1543. *
  1544. * Outputs:
  1545. * -------
  1546. * None.
  1547. *
  1548. *
  1549. * Comments:
  1550. * --------
  1551. * Assumes that a MenuSpec is custom iff its name is NULL.
  1552. *
  1553. * Assumes that ParseWmFuncStr() has parsed a menu item's function
  1554. * argument only for F_Exec and F_Menu. If it is used for other functions,
  1555. * be sure to include them here!
  1556. *
  1557. *************************************<->***********************************/
  1558. void FreeCustomMenuSpec (MenuSpec *menuSpec)
  1559. {
  1560. MenuItem *menuItem;
  1561. MenuItem *nextMenuItem;
  1562. KeySpec *accelKeySpec;
  1563. KeySpec *nextAccelKeySpec;
  1564. if ((menuSpec == NULL) || (menuSpec->name != NULL))
  1565. /* we only destroy custom menus! */
  1566. {
  1567. return;
  1568. }
  1569. /*
  1570. * Fix for CR 5450 - If the custom menu is the same as wmGD.menuActive, call
  1571. * the UnmapCallback directly to clean things up. Since
  1572. * the menu is going to be destroyed, this callback will
  1573. * not get called, leaving MWM in a failure state.
  1574. */
  1575. if (wmGD.menuActive == menuSpec)
  1576. UnmapCallback((Widget)NULL, (caddr_t)NULL, (caddr_t)NULL);
  1577. /*
  1578. * End fix for CR 5450
  1579. */
  1580. menuItem = menuSpec->menuItems;
  1581. while (menuItem)
  1582. {
  1583. nextMenuItem = menuItem->nextMenuItem;
  1584. FreeMenuItem (menuItem);
  1585. menuItem = nextMenuItem;
  1586. }
  1587. if (menuSpec->menuButtons)
  1588. {
  1589. XtFree ((char *)menuSpec->menuButtons);
  1590. }
  1591. accelKeySpec = menuSpec->accelKeySpecs;
  1592. while (accelKeySpec)
  1593. {
  1594. nextAccelKeySpec = accelKeySpec->nextKeySpec;
  1595. XtFree ((char *)accelKeySpec);
  1596. accelKeySpec = nextAccelKeySpec;
  1597. }
  1598. if (menuSpec->menuWidget)
  1599. /* destroy all children of the menu's MenuShell parent */
  1600. {
  1601. XtDestroyWidget (XtParent(menuSpec->menuWidget));
  1602. }
  1603. XtFree ((char *)menuSpec);
  1604. } /* END OF FUNCTION FreeCustomMenuSpec */