xref: /netbsd-src/lib/libmenu/post.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: post.c,v 1.8 2001/06/13 10:45:59 wiz 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 <menu.h>
30 #include <stdlib.h>
31 #include "internals.h"
32 
33 /*
34  * Post the menu to the screen.  Call any defined init routines and then
35  * draw the menu on the screen.
36  */
37 int
38 post_menu(MENU *menu)
39 {
40 	int maxx, maxy, i;
41 
42 	if (menu == NULL)
43 		return E_BAD_ARGUMENT;
44 	if (menu->posted == 1)
45 		return E_POSTED;
46 	if (menu->in_init == 1)
47 		return E_BAD_STATE;
48 	if (menu->items == NULL)
49 		return E_NOT_CONNECTED;
50 	if (*menu->items == NULL)
51 		return E_NOT_CONNECTED;
52 	if (menu->menu_win == NULL)
53 		return E_BAD_ARGUMENT;
54 
55 	menu->in_init = 1;
56 	menu->cur_item = 0; /* reset current item in case it was set before */
57 	menu->top_row = 0; /* and the top row too */
58 	if (menu->pattern != NULL) { /* and the pattern buffer....sigh */
59 		free(menu->pattern);
60 		menu->plen = 0;
61 		menu->match_len = 0;
62 	}
63 
64 	if (menu->menu_init != NULL)
65 		menu->menu_init(menu);
66 	if (menu->item_init != NULL)
67 		menu->item_init(menu);
68 
69 	menu->in_init = 0;
70 
71 	if (menu->menu_subwin == NULL) {
72 		menu->we_created = 1;
73 		menu->menu_subwin = derwin(menu->menu_win, menu->rows,
74 					   menu->cols * menu->max_item_width
75 					   + menu->cols, 0, 0);
76 		if (menu->menu_subwin == NULL) {
77 			menu->we_created = 0;
78 			return E_SYSTEM_ERROR;
79 		}
80 	}
81 
82 	getmaxyx(menu->menu_subwin, maxy, maxx);
83 	if ((maxx == ERR) || (maxy == ERR)) return E_SYSTEM_ERROR;
84 
85 	if ((menu->cols * menu->max_item_width + menu->cols - 1) > maxx)
86 		return E_NO_ROOM;
87 
88 	for (i = 0; i < menu->item_count; i++) {
89 		menu->items[i]->selected = 0;
90 	}
91 
92 	menu->posted = 1;
93 	return _menui_draw_menu(menu);
94 
95 }
96 
97 /*
98  * Unpost the menu.  Call any defined termination routines and remove the
99  * menu from the screen.
100  */
101 int
102 unpost_menu(MENU *menu)
103 {
104 	if (menu == NULL)
105 		return E_BAD_ARGUMENT;
106 	if (menu->posted != 1)
107 		return E_NOT_POSTED;
108 	if (menu->in_init == 1)
109 		return E_BAD_STATE;
110 	if (menu->menu_subwin == NULL)
111 		return E_SYSTEM_ERROR;
112 	if (menu->menu_win == NULL)
113 		return E_SYSTEM_ERROR;
114 
115 	if (menu->item_term != NULL)
116 		menu->item_term(menu);
117 
118 	if (menu->menu_term != NULL)
119 		menu->menu_term(menu);
120 
121 	menu->posted = 0;
122 	werase(menu->menu_subwin);
123 	wrefresh(menu->menu_subwin);
124 	if (menu->we_created == 1) {
125 		delwin(menu->menu_subwin);
126 		menu->menu_subwin = NULL;
127 	}
128 	wrefresh(menu->menu_win);
129 	return E_OK;
130 }
131 
132 
133