xref: /netbsd/lib/libmenu/post.c (revision bf9ec67e)
1 /*	$NetBSD: post.c,v 1.9 2002/02/04 13:02:06 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 	for (i = 0; i < menu->item_count; i++) {
76 		menu->items[i]->selected = 0;
77 	}
78 
79 	menu->posted = 1;
80 	return _menui_draw_menu(menu);
81 
82 }
83 
84 /*
85  * Unpost the menu.  Call any defined termination routines and remove the
86  * menu from the screen.
87  */
88 int
89 unpost_menu(MENU *menu)
90 {
91 	if (menu == NULL)
92 		return E_BAD_ARGUMENT;
93 	if (menu->posted != 1)
94 		return E_NOT_POSTED;
95 	if (menu->in_init == 1)
96 		return E_BAD_STATE;
97 	if (menu->item_term != NULL)
98 		menu->item_term(menu);
99 
100 	if (menu->menu_term != NULL)
101 		menu->menu_term(menu);
102 
103 	menu->posted = 0;
104 	werase(menu->scrwin);
105 	wrefresh(menu->scrwin);
106 	return E_OK;
107 }
108 
109 
110