Smart_menu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* =============================================================================
  2. * PROGRAM: CODE LIBRARY
  3. * FILENAME: smart_menu.c
  4. *
  5. * DESCRIPTION:
  6. * This module provides a set of functions to make menu handling for
  7. * Intuition easier.
  8. * A much simpler set of data structures for simple text menu are used.
  9. *
  10. * =============================================================================
  11. * COPYRIGHT:
  12. *
  13. * Copyright (c) 1995, 2004, Julian Olds
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. *
  20. * . Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. *
  23. * . Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * The name of the author may not be used to endorse or promote products
  28. * derived from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  40. * THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * =============================================================================
  43. * EXPORTED VARIABLES
  44. *
  45. * Quit : This is set to true for the quit event.
  46. *
  47. * =============================================================================
  48. * EXPORTED FUNCTIONS
  49. *
  50. * MenuNil : The Null action event handler for menu items that do nothing.
  51. * MenuQuit : The default Quit application menu handler. Sets the Quit flag.
  52. * This should also be called when the application exits if it is not
  53. * used to handle the Quit menu item.
  54. * MakeMenuStructure : Makes the Intuition menu structure and assigns the menu
  55. * into a window.
  56. * DoMenuSelection : Handles the menu selection events.
  57. *
  58. * =============================================================================
  59. */
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <exec/memory.h>
  63. #include <proto/intuition.h>
  64. #include <proto/graphics.h>
  65. #include <proto/exec.h>
  66. #include <proto/dos.h>
  67. #include "smart_menu.h"
  68. /* =============================================================================
  69. * Exported variables
  70. */
  71. int Quit;
  72. /* =============================================================================
  73. * Local variables
  74. */
  75. #define MenuLeftIndent 4
  76. static struct Menu *MenuPtr;
  77. static struct SmartMenu *SmartMenuPtr;
  78. static struct Window *MenuWindow;
  79. static struct RastPort *MenuRPort;
  80. static char *SMARTMENU_MEMORY_ERROR = "SmartMenu: AllocMem failed.";
  81. /* =============================================================================
  82. * Local functions
  83. */
  84. /* =============================================================================
  85. * FUNCTION: FreeItems
  86. *
  87. * DESCRIPTION:
  88. * This function frees the items created of an Intuition Menu.
  89. *
  90. * PARAMETERS:
  91. *
  92. * WindowMenu : The menuto be freed.
  93. *
  94. * RETURN VALUE:
  95. *
  96. * None.
  97. */
  98. void
  99. FreeItems(
  100. struct MenuItem *WindowMenu)
  101. {
  102. struct MenuItem *TmpItemPtr1;
  103. struct MenuItem *TmpItemPtr2;
  104. TmpItemPtr1 = WindowMenu;
  105. while (TmpItemPtr1 != NULL)
  106. {
  107. // Free and submenu items for this menu item
  108. FreeItems(TmpItemPtr1->SubItem);
  109. // Free the IntuiText and MenuItem for this menu item.
  110. TmpItemPtr2 = TmpItemPtr1;
  111. TmpItemPtr1 = TmpItemPtr1->NextItem;
  112. FreeMem(TmpItemPtr2->ItemFill, sizeof(struct IntuiText));
  113. FreeMem(TmpItemPtr2, sizeof(struct MenuItem));
  114. }
  115. }
  116. /* =============================================================================
  117. * FUNCTION: MenuError
  118. *
  119. * DESCRIPTION:
  120. * This function prints the errot message if there an error is detected by
  121. * the smart menu module.
  122. * It then quits the menu handler, freeing all memory used for the menu.
  123. *
  124. * PARAMETERS:
  125. *
  126. * ErrorText : The error message to display.
  127. *
  128. * RETURN VALUE:
  129. *
  130. * None.
  131. */
  132. static void
  133. MenuError(
  134. char *ErrorText)
  135. {
  136. fprintf(stderr, "%s\n", ErrorText);
  137. MenuQuit();
  138. }
  139. /* =============================================================================
  140. * FUNCTION: AddSubItems
  141. *
  142. * DESCRIPTION:
  143. * This function ads subitems to an item in a menu according to the subitems
  144. * specified in WindowItem.
  145. *
  146. * PARAMETERS:
  147. *
  148. * WindowMenu : The menu item to contain the submenu.
  149. *
  150. * WindowItem : The array of SmartMenuItem specifying the submenu.
  151. *
  152. * TextHeight : THe height of text in the menu.
  153. *
  154. * RETURN VALUE:
  155. *
  156. */
  157. static int
  158. AddSubItems(
  159. struct MenuItem *WindowMenu,
  160. struct SmartMenuItem *WindowItem,
  161. int TextHeight)
  162. {
  163. int Length;
  164. int TmpWidth;
  165. int ItemIndex;
  166. int ItemLeft;
  167. int ItemTop;
  168. int ItemWidth;
  169. int ItemHeight;
  170. struct MenuItem *TmpItemPtr1;
  171. struct MenuItem *TmpItemPtr2;
  172. struct IntuiText *ItemText;
  173. ItemLeft = WindowMenu->LeftEdge + WindowMenu->Width;
  174. ItemTop = WindowMenu->Height;
  175. ItemWidth = 0;
  176. ItemHeight = TextHeight;
  177. // Find the width of the submenu as the maximum of the sub menu item
  178. // widths.
  179. ItemIndex = 0;
  180. while (WindowItem[ItemIndex].Text != NULL)
  181. {
  182. Length = strlen(WindowItem[ItemIndex].Text);
  183. TmpWidth = TextLength(MenuRPort, WindowItem[ItemIndex].Text, Length);
  184. if (WindowItem[ItemIndex].ComSeq != 0)
  185. {
  186. TmpWidth += COMMWIDTH;
  187. TmpWidth += TextLength(MenuRPort, "M", 1);
  188. }
  189. if (TmpWidth > ItemWidth)
  190. {
  191. ItemWidth = TmpWidth;
  192. }
  193. ItemIndex++;
  194. }
  195. // Allocate the submenu items and add the to the submenu for the menu item.
  196. TmpItemPtr2 = NULL;
  197. ItemIndex = 0;
  198. while (WindowItem[ItemIndex].Text != NULL)
  199. {
  200. if ((TmpItemPtr1 = (struct MenuItem *)
  201. AllocMem(sizeof(struct MenuItem), MEMF_CLEAR)) == NULL)
  202. {
  203. MenuError(SMARTMENU_MEMORY_ERROR);
  204. return FALSE;
  205. }
  206. if ((ItemText = (struct IntuiText *)
  207. AllocMem(sizeof(struct IntuiText), MEMF_CLEAR)) == NULL)
  208. {
  209. MenuError(SMARTMENU_MEMORY_ERROR);
  210. return FALSE;
  211. }
  212. ItemText->FrontPen = WindowItem->FrontPen;
  213. ItemText->BackPen = WindowItem->BackPen;
  214. ItemText->DrawMode = JAM1;
  215. ItemText->LeftEdge = 0;
  216. ItemText->TopEdge = 0;
  217. ItemText->ITextFont = 0;
  218. ItemText->IText = WindowItem[ItemIndex].Text;
  219. ItemText->NextText = 0;
  220. TmpItemPtr1->NextItem = NULL;
  221. TmpItemPtr1->LeftEdge = ItemLeft;
  222. TmpItemPtr1->TopEdge = ItemTop;
  223. TmpItemPtr1->Width = ItemWidth;
  224. TmpItemPtr1->Height = TextHeight;
  225. if (WindowItem[ItemIndex].ComSeq != 0)
  226. {
  227. TmpItemPtr1->Flags = ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP;
  228. }
  229. else
  230. {
  231. TmpItemPtr1->Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  232. }
  233. TmpItemPtr1->MutualExclude = 0;
  234. TmpItemPtr1->ItemFill = ItemText;
  235. TmpItemPtr1->SelectFill = NULL;
  236. TmpItemPtr1->Command = WindowItem[ItemIndex].ComSeq;
  237. TmpItemPtr1->SubItem = NULL;
  238. if (WindowMenu->SubItem == NULL)
  239. {
  240. WindowMenu->SubItem = TmpItemPtr1;
  241. }
  242. else
  243. {
  244. TmpItemPtr2->NextItem = TmpItemPtr1;
  245. }
  246. TmpItemPtr2 = TmpItemPtr1;
  247. ItemTop += TextHeight;
  248. ItemIndex++;
  249. }
  250. return TRUE;
  251. }
  252. /* =============================================================================
  253. * FUNCTION: AddItems
  254. *
  255. * DESCRIPTION:
  256. * This function adds items to an intuition menu as specified in the
  257. * SmartMenu structure for the menu.
  258. *
  259. * PARAMETERS:
  260. *
  261. * WindowMenu : A pointer to the menu to have items added.
  262. *
  263. * WindowItems: The array of SmartMenuItem to add.
  264. *
  265. * TextHeight : The height of text for the menu.
  266. *
  267. * RETURN VALUE:
  268. *
  269. * int : TRUE if the menu items were successfully added.
  270. */
  271. static int
  272. AddItems(
  273. struct Menu *WindowMenu,
  274. struct SmartMenuItem *WindowItem,
  275. int TextHeight)
  276. {
  277. int Length;
  278. int TmpWidth;
  279. int ItemIndex;
  280. int ItemLeft;
  281. int ItemTop;
  282. int ItemWidth;
  283. int ItemHeight;
  284. struct MenuItem *TmpItemPtr1;
  285. struct MenuItem *TmpItemPtr2;
  286. struct IntuiText *ItemText;
  287. ItemLeft = 0;
  288. ItemTop = 0;
  289. ItemWidth = 0;
  290. ItemHeight = TextHeight;
  291. // Calculate the width of the menu as the maximum with of all of the items.
  292. ItemIndex = 0;
  293. while (WindowItem[ItemIndex].Text != NULL)
  294. {
  295. Length = strlen(WindowItem[ItemIndex].Text);
  296. TmpWidth = TextLength(MenuRPort, WindowItem[ItemIndex].Text, Length);
  297. if (WindowItem[ItemIndex].ComSeq != 0)
  298. {
  299. TmpWidth += COMMWIDTH;
  300. TmpWidth += TextLength(MenuRPort, "M", 1);
  301. }
  302. if (TmpWidth > ItemWidth)
  303. {
  304. ItemWidth = TmpWidth;
  305. }
  306. ItemIndex++;
  307. }
  308. // Allcoate the menu items ond fill in the data structures in accordance
  309. // with the SmartMenuItem specifications.
  310. TmpItemPtr2 = NULL;
  311. ItemIndex = 0;
  312. while (WindowItem[ItemIndex].Text != NULL)
  313. {
  314. if ((TmpItemPtr1 = (struct MenuItem *)
  315. AllocMem(sizeof(struct MenuItem), MEMF_CLEAR)) == NULL)
  316. {
  317. MenuError(SMARTMENU_MEMORY_ERROR);
  318. return FALSE;
  319. }
  320. if ((ItemText = (struct IntuiText *)
  321. AllocMem(sizeof(struct IntuiText), MEMF_CLEAR)) == NULL)
  322. {
  323. MenuError(SMARTMENU_MEMORY_ERROR);
  324. return FALSE;
  325. }
  326. ItemText->FrontPen = WindowItem->FrontPen;
  327. ItemText->BackPen = WindowItem->BackPen;
  328. ItemText->DrawMode = JAM1;
  329. ItemText->LeftEdge = 0;
  330. ItemText->TopEdge = 0;
  331. ItemText->ITextFont = 0;
  332. ItemText->IText = WindowItem[ItemIndex].Text;
  333. ItemText->NextText = 0;
  334. TmpItemPtr1->NextItem = NULL;
  335. TmpItemPtr1->LeftEdge = ItemLeft;
  336. TmpItemPtr1->TopEdge = ItemTop;
  337. TmpItemPtr1->Width = ItemWidth;
  338. TmpItemPtr1->Height = TextHeight;
  339. if (WindowItem[ItemIndex].ComSeq != 0)
  340. {
  341. TmpItemPtr1->Flags = ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP;
  342. }
  343. else
  344. {
  345. TmpItemPtr1->Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  346. }
  347. TmpItemPtr1->MutualExclude = 0;
  348. TmpItemPtr1->ItemFill = ItemText;
  349. TmpItemPtr1->SelectFill = NULL;
  350. TmpItemPtr1->Command = WindowItem[ItemIndex].ComSeq;
  351. TmpItemPtr1->SubItem = NULL;
  352. if (WindowMenu->FirstItem == NULL)
  353. {
  354. WindowMenu->FirstItem = TmpItemPtr1;
  355. }
  356. else
  357. {
  358. TmpItemPtr2->NextItem = TmpItemPtr1;
  359. }
  360. TmpItemPtr2 = TmpItemPtr1;
  361. // Add sub items if required.
  362. if (WindowItem[ItemIndex].SubItem != NULL)
  363. {
  364. if (!AddSubItems(
  365. TmpItemPtr1,
  366. WindowItem[ItemIndex].SubItem,
  367. TextHeight))
  368. {
  369. return FALSE;
  370. }
  371. }
  372. ItemTop += TextHeight;
  373. ItemIndex++;
  374. }
  375. return TRUE;
  376. }
  377. /* =============================================================================
  378. * Exported functions
  379. */
  380. /* =============================================================================
  381. * FUNCTION: MenuNil
  382. */
  383. void
  384. MenuNil(void)
  385. {
  386. }
  387. /* =============================================================================
  388. * FUNCTION: MenuQuit
  389. */
  390. void
  391. MenuQuit(void)
  392. {
  393. struct Menu *TmpMenuPtr1;
  394. struct Menu *TmpMenuPtr2;
  395. // Free all memory for menu structures
  396. TmpMenuPtr1 = MenuPtr;
  397. while (TmpMenuPtr1 != NULL)
  398. {
  399. FreeItems(TmpMenuPtr1->FirstItem);
  400. TmpMenuPtr2 = TmpMenuPtr1;
  401. TmpMenuPtr1 = TmpMenuPtr1->NextMenu;
  402. FreeMem(TmpMenuPtr2, sizeof(struct Menu));
  403. }
  404. // Remove the menu from the menu strip
  405. ClearMenuStrip(MenuWindow);
  406. // Set the quit flag
  407. Quit = TRUE;
  408. }
  409. /* =============================================================================
  410. * FUNCTION: MakeMenuStructure
  411. */
  412. int
  413. MakeMenuStructure(
  414. struct Window *MWindow,
  415. struct SmartMenu *WindowMenu)
  416. {
  417. int Length;
  418. int MenuIndex;
  419. int MenuLeft;
  420. int MenuTop;
  421. int MenuWidth;
  422. int MenuHeight;
  423. struct Menu *TmpMenuPtr1;
  424. struct Menu *TmpMenuPtr2;
  425. struct Screen *WindowScreen;
  426. int TextHeight;
  427. // Store the window for the menu.
  428. MenuWindow = MWindow;
  429. MenuRPort = MenuWindow->RPort;
  430. MenuPtr = NULL;
  431. SmartMenuPtr = WindowMenu;
  432. // Get the font hight for the screen font.
  433. WindowScreen = MenuWindow->WScreen;
  434. TextHeight = WindowScreen->Font->ta_YSize;
  435. // Initialise sizes and positions/
  436. TmpMenuPtr2 = NULL;
  437. MenuIndex = 0;
  438. MenuLeft = TextLength(MenuRPort, " ", 1);;
  439. MenuTop = 0;
  440. MenuHeight = TextHeight;
  441. // Create the menu structures
  442. while (WindowMenu[MenuIndex].Text != NULL)
  443. {
  444. Length = strlen(WindowMenu[MenuIndex].Text);
  445. MenuWidth = TextLength(MenuRPort, WindowMenu[MenuIndex].Text, Length);
  446. MenuWidth += TextLength(MenuRPort, " ", 1);
  447. if ((TmpMenuPtr1 = (struct Menu *)
  448. AllocMem(sizeof(struct Menu), MEMF_CLEAR)) == NULL)
  449. {
  450. MenuError(SMARTMENU_MEMORY_ERROR);
  451. return FALSE;
  452. }
  453. TmpMenuPtr1->NextMenu = NULL;
  454. TmpMenuPtr1->LeftEdge = MenuLeft;
  455. TmpMenuPtr1->TopEdge = MenuTop;
  456. TmpMenuPtr1->Width = MenuWidth;
  457. TmpMenuPtr1->Height = MenuHeight;
  458. TmpMenuPtr1->Flags = MENUENABLED;
  459. TmpMenuPtr1->MenuName = WindowMenu[MenuIndex].Text;
  460. TmpMenuPtr1->FirstItem = NULL;
  461. if (MenuPtr == NULL)
  462. {
  463. MenuPtr = TmpMenuPtr1;
  464. }
  465. else
  466. {
  467. TmpMenuPtr2->NextMenu = TmpMenuPtr1;
  468. }
  469. TmpMenuPtr2 = TmpMenuPtr1;
  470. if (WindowMenu[MenuIndex].FirstItem != NULL)
  471. {
  472. if (!AddItems(
  473. TmpMenuPtr1,
  474. WindowMenu[MenuIndex].FirstItem,
  475. TextHeight))
  476. {
  477. return FALSE;
  478. }
  479. }
  480. MenuLeft += MenuWidth;;
  481. MenuIndex++;
  482. }
  483. // Set the menu
  484. SetMenuStrip(MenuWindow, MenuPtr);
  485. return TRUE;
  486. }
  487. /* =============================================================================
  488. * FUNCTION: DoMenuSelection
  489. */
  490. void
  491. DoMenuSelection(
  492. USHORT code)
  493. {
  494. USHORT Number;
  495. int MenuNumber;
  496. int ItemNumber;
  497. int SubNumber;
  498. struct MenuItem *Item;
  499. Number = code;
  500. while (Number != MENUNULL)
  501. {
  502. Item = ItemAddress(MenuPtr, Number);
  503. MenuNumber = MENUNUM(Number);
  504. ItemNumber = ITEMNUM(Number);
  505. SubNumber = SUBNUM(Number);
  506. if (SubNumber == NOSUB)
  507. {
  508. SmartMenuPtr[MenuNumber].FirstItem[ItemNumber].SelectFunction();
  509. }
  510. else
  511. {
  512. SmartMenuPtr[MenuNumber].FirstItem[ItemNumber].
  513. SubItem[SubNumber].SelectFunction();
  514. }
  515. Number = Item->NextSelect;
  516. }
  517. }