13
static void buttonDrawIt(newtComponent co, int active, int pushed);
14
static void buttonDrawText(newtComponent co, int active, int pushed);
16
static void buttonDraw(newtComponent c);
17
static void buttonDestroy(newtComponent co);
18
static struct eventResult buttonEvent(newtComponent c,
20
static void buttonPlace(newtComponent co, int newLeft, int newTop);
22
static struct componentOps buttonOps = {
27
newtDefaultMappedHandler,
30
static newtComponent createButton(int left, int row, const char * text, int compact) {
34
co = malloc(sizeof(*co));
35
bu = malloc(sizeof(struct button));
38
bu->text = strdup(text);
39
bu->compact = compact;
44
co->width = strlen(text) + 3;
47
co->width = strlen(text) + 5;
55
newtGotorc(co->top, co->left);
60
newtComponent newtCompactButton(int left, int row, const char * text) {
61
return createButton(left, row, text, 1);
64
newtComponent newtButton(int left, int row, const char * text) {
65
return createButton(left, row, text, 0);
68
static void buttonDestroy(newtComponent co) {
69
struct button * bu = co->data;
76
static void buttonPlace(newtComponent co, int newLeft, int newTop) {
80
newtGotorc(co->top, co->left);
83
static void buttonDraw(newtComponent co) {
84
buttonDrawIt(co, 0, 0);
87
static void buttonDrawIt(newtComponent co, int active, int pushed) {
88
struct button * bu = co->data;
90
if (!co->isMapped) return;
92
SLsmg_set_color(NEWT_COLORSET_BUTTON);
96
SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON);
98
SLsmg_set_color(NEWT_COLORSET_BUTTON);
99
newtGotorc(co->top+ pushed, co->left + 1 + pushed);
100
SLsmg_write_char('<');
101
SLsmg_write_string(bu->text);
102
SLsmg_write_char('>');
105
SLsmg_set_color(NEWT_COLORSET_BUTTON);
106
newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0);
108
SLsmg_set_color(NEWT_COLORSET_WINDOW);
109
newtClearBox(co->left, co->top, co->width, 1);
110
newtClearBox(co->left, co->top, 1, co->height);
112
newtDrawBox(co->left, co->top, co->width - 1, 3, 1);
115
buttonDrawText(co, active, pushed);
119
static void buttonDrawText(newtComponent co, int active, int pushed) {
120
struct button * bu = co->data;
122
if (pushed) pushed = 1;
125
SLsmg_set_color(NEWT_COLORSET_ACTBUTTON);
127
SLsmg_set_color(NEWT_COLORSET_BUTTON);
129
newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed);
130
SLsmg_write_char(' ');
131
SLsmg_write_string(bu->text);
132
SLsmg_write_char(' ');
135
static struct eventResult buttonEvent(newtComponent co,
137
struct eventResult er;
138
struct button * bu = co->data;
140
if (ev.when == EV_NORMAL) {
143
buttonDrawIt(co, 1, 0);
144
er.result = ER_SWALLOWED;
148
buttonDrawIt(co, 0, 0);
149
er.result = ER_SWALLOWED;
153
if (ev.u.key == ' ' || ev.u.key == '\r') {
156
buttonDrawIt(co, 1, 1);
159
buttonDrawIt(co, 1, 0);
164
er.result = ER_EXITFORM;
166
er.result = ER_IGNORED;
169
if (ev.u.mouse.type == MOUSE_BUTTON_DOWN &&
170
co->top <= ev.u.mouse.y &&
171
co->top + co->height - !bu->compact > ev.u.mouse.y &&
172
co->left <= ev.u.mouse.x &&
173
co->left + co->width - !bu->compact > ev.u.mouse.x) {
175
buttonDrawIt(co, 1, 1);
178
buttonDrawIt(co, 1, 0);
182
er.result = ER_EXITFORM;
187
er.result = ER_IGNORED;