xref: /netbsd/lib/libmenu/post.c (revision c4a72b64)
1 /*	$NetBSD: post.c,v 1.10 2002/07/29 13:03:51 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 <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 
53 	menu->in_init = 1;
54 	menu->cur_item = 0; /* reset current item in case it was set before */
55 	menu->top_row = 0; /* and the top row too */
56 	if (menu->pattern != NULL) { /* and the pattern buffer....sigh */
57 		free(menu->pattern);
58 		menu->plen = 0;
59 		menu->match_len = 0;
60 	}
61 
62 	if (menu->menu_init != NULL)
63 		menu->menu_init(menu);
64 	if (menu->item_init != NULL)
65 		menu->item_init(menu);
66 
67 	menu->in_init = 0;
68 
69 	getmaxyx(menu->scrwin, maxy, maxx);
70 	if ((maxx == ERR) || (maxy == ERR)) return E_SYSTEM_ERROR;
71 
72 	if ((menu->cols * menu->max_item_width + menu->cols - 1) > maxx)
73 		return E_NO_ROOM;
74 
75 	if ((menu->opts & O_RADIO) != O_RADIO) {
76 		for (i = 0; i < menu->item_count; i++) {
77 			menu->items[i]->selected = 0;
78 		}
79 	}
80 
81 	menu->posted = 1;
82 	return _menui_draw_menu(menu);
83 
84 }
85 
86 /*
87  * Unpost the menu.  Call any defined termination routines and remove the
88  * menu from the screen.
89  */
90 int
91 unpost_menu(MENU *menu)
92 {
93 	if (menu == NULL)
94 		return E_BAD_ARGUMENT;
95 	if (menu->posted != 1)
96 		return E_NOT_POSTED;
97 	if (menu->in_init == 1)
98 		return E_BAD_STATE;
99 	if (menu->item_term != NULL)
100 		menu->item_term(menu);
101 
102 	if (menu->menu_term != NULL)
103 		menu->menu_term(menu);
104 
105 	menu->posted = 0;
106 	werase(menu->scrwin);
107 	wrefresh(menu->scrwin);
108 	return E_OK;
109 }
110 
111 
112