smart_menu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 <exec/memory.h>
  61. #include <proto/dos.h>
  62. #include <proto/exec.h>
  63. #include <proto/graphics.h>
  64. #include <proto/intuition.h>
  65. #include <stdio.h>
  66. #include <string.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 FreeItems(struct MenuItem *WindowMenu) {
  99. struct MenuItem *TmpItemPtr1;
  100. struct MenuItem *TmpItemPtr2;
  101. TmpItemPtr1 = WindowMenu;
  102. while (TmpItemPtr1 != NULL) {
  103. // Free and submenu items for this menu item
  104. FreeItems(TmpItemPtr1->SubItem);
  105. // Free the IntuiText and MenuItem for this menu item.
  106. TmpItemPtr2 = TmpItemPtr1;
  107. TmpItemPtr1 = TmpItemPtr1->NextItem;
  108. FreeMem(TmpItemPtr2->ItemFill, sizeof(struct IntuiText));
  109. FreeMem(TmpItemPtr2, sizeof(struct MenuItem));
  110. }
  111. }
  112. /* =============================================================================
  113. * FUNCTION: MenuError
  114. *
  115. * DESCRIPTION:
  116. * This function prints the errot message if there an error is detected by
  117. * the smart menu module.
  118. * It then quits the menu handler, freeing all memory used for the menu.
  119. *
  120. * PARAMETERS:
  121. *
  122. * ErrorText : The error message to display.
  123. *
  124. * RETURN VALUE:
  125. *
  126. * None.
  127. */
  128. static void MenuError(char *ErrorText) {
  129. fprintf(stderr, "%s\n", ErrorText);
  130. MenuQuit();
  131. }
  132. /* =============================================================================
  133. * FUNCTION: AddSubItems
  134. *
  135. * DESCRIPTION:
  136. * This function ads subitems to an item in a menu according to the subitems
  137. * specified in WindowItem.
  138. *
  139. * PARAMETERS:
  140. *
  141. * WindowMenu : The menu item to contain the submenu.
  142. *
  143. * WindowItem : The array of SmartMenuItem specifying the submenu.
  144. *
  145. * TextHeight : THe height of text in the menu.
  146. *
  147. * RETURN VALUE:
  148. *
  149. */
  150. static int AddSubItems(struct MenuItem *WindowMenu,
  151. struct SmartMenuItem *WindowItem, int TextHeight) {
  152. int Length;
  153. int TmpWidth;
  154. int ItemIndex;
  155. int ItemLeft;
  156. int ItemTop;
  157. int ItemWidth;
  158. int ItemHeight;
  159. struct MenuItem *TmpItemPtr1;
  160. struct MenuItem *TmpItemPtr2;
  161. struct IntuiText *ItemText;
  162. ItemLeft = WindowMenu->LeftEdge + WindowMenu->Width;
  163. ItemTop = WindowMenu->Height;
  164. ItemWidth = 0;
  165. ItemHeight = TextHeight;
  166. // Find the width of the submenu as the maximum of the sub menu item
  167. // widths.
  168. ItemIndex = 0;
  169. while (WindowItem[ItemIndex].Text != NULL) {
  170. Length = strlen(WindowItem[ItemIndex].Text);
  171. TmpWidth = TextLength(MenuRPort, WindowItem[ItemIndex].Text, Length);
  172. if (WindowItem[ItemIndex].ComSeq != 0) {
  173. TmpWidth += COMMWIDTH;
  174. TmpWidth += TextLength(MenuRPort, "M", 1);
  175. }
  176. if (TmpWidth > ItemWidth)
  177. ItemWidth = TmpWidth;
  178. ItemIndex++;
  179. }
  180. // Allocate the submenu items and add the to the submenu for the menu item.
  181. TmpItemPtr2 = NULL;
  182. ItemIndex = 0;
  183. while (WindowItem[ItemIndex].Text != NULL) {
  184. if ((TmpItemPtr1 = (struct MenuItem *)AllocMem(sizeof(struct MenuItem),
  185. MEMF_CLEAR)) == NULL) {
  186. MenuError(SMARTMENU_MEMORY_ERROR);
  187. return FALSE;
  188. }
  189. if ((ItemText = (struct IntuiText *)AllocMem(sizeof(struct IntuiText),
  190. MEMF_CLEAR)) == NULL) {
  191. MenuError(SMARTMENU_MEMORY_ERROR);
  192. return FALSE;
  193. }
  194. ItemText->FrontPen = WindowItem->FrontPen;
  195. ItemText->BackPen = WindowItem->BackPen;
  196. ItemText->DrawMode = JAM1;
  197. ItemText->LeftEdge = 0;
  198. ItemText->TopEdge = 0;
  199. ItemText->ITextFont = 0;
  200. ItemText->IText = WindowItem[ItemIndex].Text;
  201. ItemText->NextText = 0;
  202. TmpItemPtr1->NextItem = NULL;
  203. TmpItemPtr1->LeftEdge = ItemLeft;
  204. TmpItemPtr1->TopEdge = ItemTop;
  205. TmpItemPtr1->Width = ItemWidth;
  206. TmpItemPtr1->Height = TextHeight;
  207. if (WindowItem[ItemIndex].ComSeq != 0)
  208. TmpItemPtr1->Flags = ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP;
  209. else
  210. TmpItemPtr1->Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  211. TmpItemPtr1->MutualExclude = 0;
  212. TmpItemPtr1->ItemFill = ItemText;
  213. TmpItemPtr1->SelectFill = NULL;
  214. TmpItemPtr1->Command = WindowItem[ItemIndex].ComSeq;
  215. TmpItemPtr1->SubItem = NULL;
  216. if (WindowMenu->SubItem == NULL)
  217. WindowMenu->SubItem = TmpItemPtr1;
  218. else
  219. TmpItemPtr2->NextItem = TmpItemPtr1;
  220. TmpItemPtr2 = TmpItemPtr1;
  221. ItemTop += TextHeight;
  222. ItemIndex++;
  223. }
  224. return TRUE;
  225. }
  226. /* =============================================================================
  227. * FUNCTION: AddItems
  228. *
  229. * DESCRIPTION:
  230. * This function adds items to an intuition menu as specified in the
  231. * SmartMenu structure for the menu.
  232. *
  233. * PARAMETERS:
  234. *
  235. * WindowMenu : A pointer to the menu to have items added.
  236. *
  237. * WindowItems: The array of SmartMenuItem to add.
  238. *
  239. * TextHeight : The height of text for the menu.
  240. *
  241. * RETURN VALUE:
  242. *
  243. * int : TRUE if the menu items were successfully added.
  244. */
  245. static int AddItems(struct Menu *WindowMenu, struct SmartMenuItem *WindowItem,
  246. int TextHeight) {
  247. int Length;
  248. int TmpWidth;
  249. int ItemIndex;
  250. int ItemLeft;
  251. int ItemTop;
  252. int ItemWidth;
  253. int ItemHeight;
  254. struct MenuItem *TmpItemPtr1;
  255. struct MenuItem *TmpItemPtr2;
  256. struct IntuiText *ItemText;
  257. ItemLeft = 0;
  258. ItemTop = 0;
  259. ItemWidth = 0;
  260. ItemHeight = TextHeight;
  261. // Calculate the width of the menu as the maximum with of all of the items.
  262. ItemIndex = 0;
  263. while (WindowItem[ItemIndex].Text != NULL) {
  264. Length = strlen(WindowItem[ItemIndex].Text);
  265. TmpWidth = TextLength(MenuRPort, WindowItem[ItemIndex].Text, Length);
  266. if (WindowItem[ItemIndex].ComSeq != 0) {
  267. TmpWidth += COMMWIDTH;
  268. TmpWidth += TextLength(MenuRPort, "M", 1);
  269. }
  270. if (TmpWidth > ItemWidth)
  271. ItemWidth = TmpWidth;
  272. ItemIndex++;
  273. }
  274. // Allcoate the menu items ond fill in the data structures in accordance
  275. // with the SmartMenuItem specifications.
  276. TmpItemPtr2 = NULL;
  277. ItemIndex = 0;
  278. while (WindowItem[ItemIndex].Text != NULL) {
  279. if ((TmpItemPtr1 = (struct MenuItem *)AllocMem(sizeof(struct MenuItem),
  280. MEMF_CLEAR)) == NULL) {
  281. MenuError(SMARTMENU_MEMORY_ERROR);
  282. return FALSE;
  283. }
  284. if ((ItemText = (struct IntuiText *)AllocMem(sizeof(struct IntuiText),
  285. MEMF_CLEAR)) == NULL) {
  286. MenuError(SMARTMENU_MEMORY_ERROR);
  287. return FALSE;
  288. }
  289. ItemText->FrontPen = WindowItem->FrontPen;
  290. ItemText->BackPen = WindowItem->BackPen;
  291. ItemText->DrawMode = JAM1;
  292. ItemText->LeftEdge = 0;
  293. ItemText->TopEdge = 0;
  294. ItemText->ITextFont = 0;
  295. ItemText->IText = WindowItem[ItemIndex].Text;
  296. ItemText->NextText = 0;
  297. TmpItemPtr1->NextItem = NULL;
  298. TmpItemPtr1->LeftEdge = ItemLeft;
  299. TmpItemPtr1->TopEdge = ItemTop;
  300. TmpItemPtr1->Width = ItemWidth;
  301. TmpItemPtr1->Height = TextHeight;
  302. if (WindowItem[ItemIndex].ComSeq != 0)
  303. TmpItemPtr1->Flags = ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP;
  304. else
  305. TmpItemPtr1->Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  306. TmpItemPtr1->MutualExclude = 0;
  307. TmpItemPtr1->ItemFill = ItemText;
  308. TmpItemPtr1->SelectFill = NULL;
  309. TmpItemPtr1->Command = WindowItem[ItemIndex].ComSeq;
  310. TmpItemPtr1->SubItem = NULL;
  311. if (WindowMenu->FirstItem == NULL)
  312. WindowMenu->FirstItem = TmpItemPtr1;
  313. else
  314. TmpItemPtr2->NextItem = TmpItemPtr1;
  315. TmpItemPtr2 = TmpItemPtr1;
  316. // Add sub items if required.
  317. if (WindowItem[ItemIndex].SubItem != NULL) {
  318. if (!AddSubItems(TmpItemPtr1, WindowItem[ItemIndex].SubItem, TextHeight))
  319. return FALSE;
  320. }
  321. ItemTop += TextHeight;
  322. ItemIndex++;
  323. }
  324. return TRUE;
  325. }
  326. /* =============================================================================
  327. * Exported functions
  328. */
  329. /* =============================================================================
  330. * FUNCTION: MenuNil
  331. */
  332. void MenuNil(void) {}
  333. /* =============================================================================
  334. * FUNCTION: MenuQuit
  335. */
  336. void MenuQuit(void) {
  337. struct Menu *TmpMenuPtr1;
  338. struct Menu *TmpMenuPtr2;
  339. // Free all memory for menu structures
  340. TmpMenuPtr1 = MenuPtr;
  341. while (TmpMenuPtr1 != NULL) {
  342. FreeItems(TmpMenuPtr1->FirstItem);
  343. TmpMenuPtr2 = TmpMenuPtr1;
  344. TmpMenuPtr1 = TmpMenuPtr1->NextMenu;
  345. FreeMem(TmpMenuPtr2, sizeof(struct Menu));
  346. }
  347. // Remove the menu from the menu strip
  348. ClearMenuStrip(MenuWindow);
  349. // Set the quit flag
  350. Quit = TRUE;
  351. }
  352. /* =============================================================================
  353. * FUNCTION: MakeMenuStructure
  354. */
  355. int MakeMenuStructure(struct Window *MWindow, struct SmartMenu *WindowMenu) {
  356. int Length;
  357. int MenuIndex;
  358. int MenuLeft;
  359. int MenuTop;
  360. int MenuWidth;
  361. int MenuHeight;
  362. struct Menu *TmpMenuPtr1;
  363. struct Menu *TmpMenuPtr2;
  364. struct Screen *WindowScreen;
  365. int TextHeight;
  366. // Store the window for the menu.
  367. MenuWindow = MWindow;
  368. MenuRPort = MenuWindow->RPort;
  369. MenuPtr = NULL;
  370. SmartMenuPtr = WindowMenu;
  371. // Get the font hight for the screen font.
  372. WindowScreen = MenuWindow->WScreen;
  373. TextHeight = WindowScreen->Font->ta_YSize;
  374. // Initialise sizes and positions/
  375. TmpMenuPtr2 = NULL;
  376. MenuIndex = 0;
  377. MenuLeft = TextLength(MenuRPort, " ", 1);
  378. ;
  379. MenuTop = 0;
  380. MenuHeight = TextHeight;
  381. // Create the menu structures
  382. while (WindowMenu[MenuIndex].Text != NULL) {
  383. Length = strlen(WindowMenu[MenuIndex].Text);
  384. MenuWidth = TextLength(MenuRPort, WindowMenu[MenuIndex].Text, Length);
  385. MenuWidth += TextLength(MenuRPort, " ", 1);
  386. if ((TmpMenuPtr1 = (struct Menu *)AllocMem(sizeof(struct Menu),
  387. MEMF_CLEAR)) == NULL) {
  388. MenuError(SMARTMENU_MEMORY_ERROR);
  389. return FALSE;
  390. }
  391. TmpMenuPtr1->NextMenu = NULL;
  392. TmpMenuPtr1->LeftEdge = MenuLeft;
  393. TmpMenuPtr1->TopEdge = MenuTop;
  394. TmpMenuPtr1->Width = MenuWidth;
  395. TmpMenuPtr1->Height = MenuHeight;
  396. TmpMenuPtr1->Flags = MENUENABLED;
  397. TmpMenuPtr1->MenuName = WindowMenu[MenuIndex].Text;
  398. TmpMenuPtr1->FirstItem = NULL;
  399. if (MenuPtr == NULL)
  400. MenuPtr = TmpMenuPtr1;
  401. else
  402. TmpMenuPtr2->NextMenu = TmpMenuPtr1;
  403. TmpMenuPtr2 = TmpMenuPtr1;
  404. if (WindowMenu[MenuIndex].FirstItem != NULL) {
  405. if (!AddItems(TmpMenuPtr1, WindowMenu[MenuIndex].FirstItem, TextHeight))
  406. return FALSE;
  407. }
  408. MenuLeft += MenuWidth;
  409. ;
  410. MenuIndex++;
  411. }
  412. // Set the menu
  413. SetMenuStrip(MenuWindow, MenuPtr);
  414. return TRUE;
  415. }
  416. /* =============================================================================
  417. * FUNCTION: DoMenuSelection
  418. */
  419. void DoMenuSelection(USHORT code) {
  420. USHORT Number;
  421. int MenuNumber;
  422. int ItemNumber;
  423. int SubNumber;
  424. struct MenuItem *Item;
  425. Number = code;
  426. while (Number != MENUNULL) {
  427. Item = ItemAddress(MenuPtr, Number);
  428. MenuNumber = MENUNUM(Number);
  429. ItemNumber = ITEMNUM(Number);
  430. SubNumber = SUBNUM(Number);
  431. if (SubNumber == NOSUB)
  432. SmartMenuPtr[MenuNumber].FirstItem[ItemNumber].SelectFunction();
  433. else
  434. SmartMenuPtr[MenuNumber]
  435. .FirstItem[ItemNumber]
  436. .SubItem[SubNumber]
  437. .SelectFunction();
  438. Number = Item->NextSelect;
  439. }
  440. }