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