xref: /minix3/lib/libmenu/post.c (revision b618a1ee0b63a120cb1d99249a82c6843ff3e4f4)
1 /*	$NetBSD: post.c,v 1.12 2003/04/19 12:52:39 blymn Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998-1999 Brett Lymn (blymn@baea.com.au, brett_lymn@yahoo.com.au)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: post.c,v 1.12 2003/04/19 12:52:39 blymn Exp $");
31 
32 #include <menu.h>
33 #include <stdlib.h>
34 #include "internals.h"
35 
36 /*
37  * Post the menu to the screen.  Call any defined init routines and then
38  * draw the menu on the screen.
39  */
40 int
post_menu(MENU * menu)41 post_menu(MENU *menu)
42 {
43 	int maxx, maxy, i;
44 
45 	if (menu == NULL)
46 		return E_BAD_ARGUMENT;
47 	if (menu->posted == 1)
48 		return E_POSTED;
49 	if (menu->in_init == 1)
50 		return E_BAD_STATE;
51 	if (menu->items == NULL)
52 		return E_NOT_CONNECTED;
53 	if (*menu->items == NULL)
54 		return E_NOT_CONNECTED;
55 
56 	menu->in_init = 1;
57 
58 	if (menu->menu_init != NULL)
59 		menu->menu_init(menu);
60 	if (menu->item_init != NULL)
61 		menu->item_init(menu);
62 
63 	menu->in_init = 0;
64 
65 	getmaxyx(menu->scrwin, maxy, maxx);
66 	if ((maxx == ERR) || (maxy == ERR)) return E_SYSTEM_ERROR;
67 
68 	if ((menu->cols * menu->max_item_width + menu->cols - 1) > maxx)
69 		return E_NO_ROOM;
70 
71 	if ((menu->opts & O_RADIO) != O_RADIO) {
72 		for (i = 0; i < menu->item_count; i++) {
73 			menu->items[i]->selected = 0;
74 		}
75 	}
76 
77 	menu->posted = 1;
78 	return _menui_draw_menu(menu);
79 
80 }
81 
82 /*
83  * Unpost the menu.  Call any defined termination routines and remove the
84  * menu from the screen.
85  */
86 int
unpost_menu(MENU * menu)87 unpost_menu(MENU *menu)
88 {
89 	if (menu == NULL)
90 		return E_BAD_ARGUMENT;
91 	if (menu->posted != 1)
92 		return E_NOT_POSTED;
93 	if (menu->in_init == 1)
94 		return E_BAD_STATE;
95 	if (menu->item_term != NULL)
96 		menu->item_term(menu);
97 
98 	if (menu->menu_term != NULL)
99 		menu->menu_term(menu);
100 
101 	menu->posted = 0;
102 	werase(menu->scrwin);
103 	wrefresh(menu->scrwin);
104 	return E_OK;
105 }
106 
107 
108