m_post.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /****************************************************************************
  2. * Copyright (c) 1998-2003,2004 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Juergen Pfeifer, 1995,1997 *
  30. ****************************************************************************/
  31. /***************************************************************************
  32. * Module m_post *
  33. * Write or erase menus from associated subwindows *
  34. ***************************************************************************/
  35. #include "menu.priv.h"
  36. MODULE_ID("$Id: m_post.c,v 1.26 2004/12/25 23:57:04 tom Exp $")
  37. /*---------------------------------------------------------------------------
  38. | Facility : libnmenu
  39. | Function : void _nc_Post_Item(MENU *menu, ITEM *item)
  40. |
  41. | Description : Draw the item in the menus window at the current
  42. | window position
  43. |
  44. | Return Values : -
  45. +--------------------------------------------------------------------------*/
  46. NCURSES_EXPORT(void)
  47. _nc_Post_Item(const MENU * menu, const ITEM * item)
  48. {
  49. int i;
  50. chtype ch;
  51. int item_x, item_y;
  52. int count = 0;
  53. bool isfore = FALSE, isback = FALSE, isgrey = FALSE;
  54. int name_len;
  55. int desc_len;
  56. assert(menu->win);
  57. getyx(menu->win, item_y, item_x);
  58. /* We need a marker iff
  59. - it is a onevalued menu and it is the current item
  60. - or it has a selection value
  61. */
  62. wattron(menu->win, menu->back);
  63. if (item->value || (item == menu->curitem))
  64. {
  65. if (menu->marklen)
  66. {
  67. /* In a multi selection menu we use the fore attribute
  68. for a selected marker that is not the current one.
  69. This improves visualization of the menu, because now
  70. always the 'normal' marker denotes the current
  71. item. */
  72. if (!(menu->opt & O_ONEVALUE) && item->value && item != menu->curitem)
  73. {
  74. wattron(menu->win, menu->fore);
  75. isfore = TRUE;
  76. }
  77. waddstr(menu->win, menu->mark);
  78. if (isfore)
  79. {
  80. wattron(menu->win, menu->fore);
  81. isfore = FALSE;
  82. }
  83. }
  84. }
  85. else /* otherwise we have to wipe out the marker area */
  86. for (ch = ' ', i = menu->marklen; i > 0; i--)
  87. waddch(menu->win, ch);
  88. wattroff(menu->win, menu->back);
  89. count += menu->marklen;
  90. /* First we have to calculate the attribute depending on selectability
  91. and selection status
  92. */
  93. if (!(item->opt & O_SELECTABLE))
  94. {
  95. wattron(menu->win, menu->grey);
  96. isgrey = TRUE;
  97. }
  98. else
  99. {
  100. if (item->value || item == menu->curitem)
  101. {
  102. wattron(menu->win, menu->fore);
  103. isfore = TRUE;
  104. }
  105. else
  106. {
  107. wattron(menu->win, menu->back);
  108. isback = TRUE;
  109. }
  110. }
  111. waddnstr(menu->win, item->name.str, item->name.length);
  112. name_len = _nc_Calculate_Text_Width(&(item->name));
  113. for (ch = ' ', i = menu->namelen - name_len; i > 0; i--)
  114. {
  115. waddch(menu->win, ch);
  116. }
  117. count += menu->namelen;
  118. /* Show description if required and available */
  119. if ((menu->opt & O_SHOWDESC) && menu->desclen > 0)
  120. {
  121. int m = menu->spc_desc / 2;
  122. int cy = -1, cx = -1;
  123. for (ch = ' ', i = 0; i < menu->spc_desc; i++)
  124. {
  125. if (i == m)
  126. {
  127. waddch(menu->win, menu->pad);
  128. getyx(menu->win, cy, cx);
  129. }
  130. else
  131. waddch(menu->win, ch);
  132. }
  133. if (item->description.length)
  134. waddnstr(menu->win, item->description.str, item->description.length);
  135. desc_len = _nc_Calculate_Text_Width(&(item->description));
  136. for (ch = ' ', i = menu->desclen - desc_len; i > 0; i--)
  137. {
  138. waddch(menu->win, ch);
  139. }
  140. count += menu->desclen + menu->spc_desc;
  141. if (menu->spc_rows > 1)
  142. {
  143. int j, k, ncy, ncx;
  144. assert(cx >= 0 && cy >= 0);
  145. getyx(menu->win, ncy, ncx);
  146. if (isgrey)
  147. wattroff(menu->win, menu->grey);
  148. else if (isfore)
  149. wattroff(menu->win, menu->fore);
  150. wattron(menu->win, menu->back);
  151. for (j = 1; j < menu->spc_rows; j++)
  152. {
  153. if ((item_y + j) < getmaxy(menu->win))
  154. {
  155. wmove(menu->win, item_y + j, item_x);
  156. for (k = 0; k < count; k++)
  157. waddch(menu->win, ' ');
  158. }
  159. if ((cy + j) < getmaxy(menu->win))
  160. mvwaddch(menu->win, cy + j, cx - 1, menu->pad);
  161. }
  162. wmove(menu->win, ncy, ncx);
  163. if (!isback)
  164. wattroff(menu->win, menu->back);
  165. }
  166. }
  167. /* Remove attributes */
  168. if (isfore)
  169. wattroff(menu->win, menu->fore);
  170. if (isback)
  171. wattroff(menu->win, menu->back);
  172. if (isgrey)
  173. wattroff(menu->win, menu->grey);
  174. }
  175. /*---------------------------------------------------------------------------
  176. | Facility : libnmenu
  177. | Function : void _nc_Draw_Menu(const MENU *)
  178. |
  179. | Description : Display the menu in its windows
  180. |
  181. | Return Values : -
  182. +--------------------------------------------------------------------------*/
  183. NCURSES_EXPORT(void)
  184. _nc_Draw_Menu(const MENU * menu)
  185. {
  186. ITEM *item = menu->items[0];
  187. ITEM *lasthor, *lastvert;
  188. ITEM *hitem;
  189. int y = 0;
  190. chtype s_bkgd;
  191. assert(item && menu->win);
  192. s_bkgd = getbkgd(menu->win);
  193. wbkgdset(menu->win, menu->back);
  194. werase(menu->win);
  195. wbkgdset(menu->win, s_bkgd);
  196. lastvert = (menu->opt & O_NONCYCLIC) ? (ITEM *) 0 : item;
  197. do
  198. {
  199. wmove(menu->win, y, 0);
  200. hitem = item;
  201. lasthor = (menu->opt & O_NONCYCLIC) ? (ITEM *) 0 : hitem;
  202. do
  203. {
  204. _nc_Post_Item(menu, hitem);
  205. wattron(menu->win, menu->back);
  206. if (((hitem = hitem->right) != lasthor) && hitem)
  207. {
  208. int i, j, cy, cx;
  209. chtype ch = ' ';
  210. getyx(menu->win, cy, cx);
  211. for (j = 0; j < menu->spc_rows; j++)
  212. {
  213. wmove(menu->win, cy + j, cx);
  214. for (i = 0; i < menu->spc_cols; i++)
  215. {
  216. waddch(menu->win, ch);
  217. }
  218. }
  219. wmove(menu->win, cy, cx + menu->spc_cols);
  220. }
  221. }
  222. while (hitem && (hitem != lasthor));
  223. wattroff(menu->win, menu->back);
  224. item = item->down;
  225. y += menu->spc_rows;
  226. }
  227. while (item && (item != lastvert));
  228. }
  229. /*---------------------------------------------------------------------------
  230. | Facility : libnmenu
  231. | Function : int post_menu(MENU *)
  232. |
  233. | Description : Post a menu to the screen. This makes it visible.
  234. |
  235. | Return Values : E_OK - success
  236. | E_BAD_ARGUMENT - not a valid menu pointer
  237. | E_SYSTEM_ERROR - error in lower layers
  238. | E_NOT_CONNECTED - No items connected to menu
  239. | E_BAD_STATE - Menu in userexit routine
  240. | E_POSTED - Menu already posted
  241. +--------------------------------------------------------------------------*/
  242. NCURSES_EXPORT(int)
  243. post_menu(MENU * menu)
  244. {
  245. T((T_CALLED("post_menu(%p)"), menu));
  246. if (!menu)
  247. RETURN(E_BAD_ARGUMENT);
  248. if (menu->status & _IN_DRIVER)
  249. RETURN(E_BAD_STATE);
  250. if (menu->status & _POSTED)
  251. RETURN(E_POSTED);
  252. if (menu->items && *(menu->items))
  253. {
  254. int y;
  255. int h = 1 + menu->spc_rows * (menu->rows - 1);
  256. WINDOW *win = Get_Menu_Window(menu);
  257. int maxy = getmaxy(win);
  258. if ((menu->win = newpad(h, menu->width)))
  259. {
  260. y = (maxy >= h) ? h : maxy;
  261. if (y >= menu->height)
  262. y = menu->height;
  263. if (!(menu->sub = subpad(menu->win, y, menu->width, 0, 0)))
  264. RETURN(E_SYSTEM_ERROR);
  265. }
  266. else
  267. RETURN(E_SYSTEM_ERROR);
  268. if (menu->status & _LINK_NEEDED)
  269. _nc_Link_Items(menu);
  270. }
  271. else
  272. RETURN(E_NOT_CONNECTED);
  273. menu->status |= _POSTED;
  274. if (!(menu->opt & O_ONEVALUE))
  275. {
  276. ITEM **items;
  277. for (items = menu->items; *items; items++)
  278. {
  279. (*items)->value = FALSE;
  280. }
  281. }
  282. _nc_Draw_Menu(menu);
  283. Call_Hook(menu, menuinit);
  284. Call_Hook(menu, iteminit);
  285. _nc_Show_Menu(menu);
  286. RETURN(E_OK);
  287. }
  288. /*---------------------------------------------------------------------------
  289. | Facility : libnmenu
  290. | Function : int unpost_menu(MENU *)
  291. |
  292. | Description : Detach menu from screen
  293. |
  294. | Return Values : E_OK - success
  295. | E_BAD_ARGUMENT - not a valid menu pointer
  296. | E_BAD_STATE - menu in userexit routine
  297. | E_NOT_POSTED - menu is not posted
  298. +--------------------------------------------------------------------------*/
  299. NCURSES_EXPORT(int)
  300. unpost_menu(MENU * menu)
  301. {
  302. WINDOW *win;
  303. T((T_CALLED("unpost_menu(%p)"), menu));
  304. if (!menu)
  305. RETURN(E_BAD_ARGUMENT);
  306. if (menu->status & _IN_DRIVER)
  307. RETURN(E_BAD_STATE);
  308. if (!(menu->status & _POSTED))
  309. RETURN(E_NOT_POSTED);
  310. Call_Hook(menu, itemterm);
  311. Call_Hook(menu, menuterm);
  312. win = Get_Menu_Window(menu);
  313. werase(win);
  314. wsyncup(win);
  315. assert(menu->sub);
  316. delwin(menu->sub);
  317. menu->sub = (WINDOW *)0;
  318. assert(menu->win);
  319. delwin(menu->win);
  320. menu->win = (WINDOW *)0;
  321. menu->status &= ~_POSTED;
  322. RETURN(E_OK);
  323. }
  324. /* m_post.c ends here */