summaryrefslogtreecommitdiffstats
path: root/ui/ncurses/nc-menu.h
blob: 4639c5537bd38d00d16115eb472aa4951acd17a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  Copyright (C) 2009 Sony Computer Entertainment Inc.
 *  Copyright 2009 Sony Corp.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if !defined(_PB_NC_MENU_H)
#define _PB_NC_MENU_H

#include <assert.h>

#include <linux/input.h> /* This must be included before ncurses.h */
#if defined HAVE_NCURSESW_MENU_H
#  include <ncursesw/menu.h>
#elif defined HAVE_NCURSES_MENU_H
#  include <ncurses/menu.h>
#elif defined HAVE_MENU_H
#  include <menu.h>
#else
#  error "Curses menu.h not found."
#endif

#include "log/log.h"
#include "types/types.h"
#include "nc-scr.h"

struct pmenu;

/**
 * struct pmenu_item - Hold the state of a single menu item.
 * @i_sig: Signature for callback type checking, should be pmenu_item_sig.
 * @nci: The ncurses menu item instance for this item.
 */

struct pmenu_item {
	enum pb_nc_sig i_sig;
	ITEM *nci;
	struct pmenu *pmenu;
	void *data;
	void (*on_edit)(struct pmenu_item *item);
	int (*on_execute)(struct pmenu_item *item);
};

struct pmenu_item *pmenu_item_alloc(struct pmenu *menu);
struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
	unsigned int index, const char *name);
int pmenu_item_replace(struct pmenu_item *i, const char *name);
void pmenu_item_delete(struct pmenu_item *item);

static inline struct pmenu_item *pmenu_item_from_arg(void *arg)
{
	struct pmenu_item *item = (struct pmenu_item *)arg;

	assert(item->i_sig == pb_item_sig);
	return item;
}

static inline struct cui_opt_data *cod_from_item(struct pmenu_item *item)
{
	return item->data;
}

static inline struct pmenu_item *pmenu_item_init(struct pmenu *menu,
	unsigned int index, const char *name)
{
	return pmenu_item_setup(menu, pmenu_item_alloc(menu), index, name);
}

/**
 * struct pmenu - Data structure defining complete menu.
 * @insert_pt: Index in nc item array.
 * @ncm: The ncurses menu instance for this menu.
 */

struct pmenu {
	struct nc_scr scr;
	MENU *ncm;
	ITEM **items;
	unsigned int item_count;
	unsigned int insert_pt;
	int (*hot_key)(struct pmenu *menu, struct pmenu_item *item, int c);
	void (*on_exit)(struct pmenu *menu);
	void (*on_new)(struct pmenu *menu);
};

struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
	void (*on_exit)(struct pmenu *));
int pmenu_setup(struct pmenu *menu);
void pmenu_delete(struct pmenu *menu);
unsigned int pmenu_grow(struct pmenu *menu, unsigned int count);
int pmenu_remove(struct pmenu *menu, struct pmenu_item *item);
struct pmenu_item *pmenu_find_selected(struct pmenu *menu);

/* convenience routines */

int pmenu_exit_cb(struct pmenu_item *item);

static inline struct pmenu *pmenu_from_scr(struct nc_scr *scr)
{
	struct pmenu *pmenu;

	assert(scr->sig == pb_pmenu_sig);
	pmenu = (struct pmenu *)((char *)scr
		- (size_t)&((struct pmenu *)0)->scr);
	assert(pmenu->scr.sig == pb_pmenu_sig);

	return pmenu;
}

/* debug routines */

static inline void pmenu_dump_item(const ITEM *item)
{
	pb_debug("%p %s\n", item, (item ? item->name.str : "(null)"));
}

static inline void pmenu_dump_items(ITEM *const *items, unsigned int count)
{
	unsigned int i;

	for (i = 0; i < count; i++)
		pb_debug("%u: %p %s\n", i, items[i],
			(items[i] ? items[i]->name.str : "(null)"));
}

#endif
OpenPOWER on IntegriCloud