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;
45
co->width = strwidth(text) + 3;
47
co->width = strlen(text) + 3;
52
co->width = strwidth(text) + 5;
54
co->width = strlen(text) + 5;
63
newtGotorc(co->top, co->left);
68
newtComponent newtCompactButton(int left, int row, const char * text) {
69
return createButton(left, row, text, 1);
72
newtComponent newtButton(int left, int row, const char * text) {
73
return createButton(left, row, text, 0);
76
static void buttonDestroy(newtComponent co) {
77
struct button * bu = co->data;
84
static void buttonPlace(newtComponent co, int newLeft, int newTop) {
88
newtGotorc(co->top, co->left);
91
static void buttonDraw(newtComponent co) {
92
buttonDrawIt(co, 0, 0);
95
static void buttonDrawIt(newtComponent co, int active, int pushed) {
96
struct button * bu = co->data;
98
if (!co->isMapped) return;
100
newtColor(NEWT_COLORSET_BUTTON);
104
newtColor(NEWT_COLORSET_COMPACTBUTTON);
106
newtColor(NEWT_COLORSET_BUTTON);
107
newtGotorc(co->top+ pushed, co->left + 1 + pushed);
108
SLsmg_write_char('<');
109
SLsmg_write_string(bu->text);
110
SLsmg_write_char('>');
113
newtColor(NEWT_COLORSET_BUTTON);
114
newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0);
116
newtColor(NEWT_COLORSET_WINDOW);
117
newtClearBox(co->left, co->top, co->width, 1);
118
newtClearBox(co->left, co->top, 1, co->height);
120
newtDrawBox(co->left, co->top, co->width - 1, 3, 1);
123
buttonDrawText(co, active, pushed);
127
static void buttonDrawText(newtComponent co, int active, int pushed) {
128
struct button * bu = co->data;
130
if (pushed) pushed = 1;
133
newtColor(NEWT_COLORSET_ACTBUTTON);
135
newtColor(NEWT_COLORSET_BUTTON);
137
newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed);
138
SLsmg_write_char(' ');
139
SLsmg_write_string(bu->text);
140
SLsmg_write_char(' ');
143
static struct eventResult buttonEvent(newtComponent co,
145
struct eventResult er;
146
struct button * bu = co->data;
148
if (ev.when == EV_NORMAL) {
151
buttonDrawIt(co, 1, 0);
152
er.result = ER_SWALLOWED;
156
buttonDrawIt(co, 0, 0);
157
er.result = ER_SWALLOWED;
161
if (ev.u.key == ' ' || ev.u.key == '\r') {
164
buttonDrawIt(co, 1, 1);
167
buttonDrawIt(co, 1, 0);
172
er.result = ER_EXITFORM;
174
er.result = ER_IGNORED;
177
if (ev.u.mouse.type == MOUSE_BUTTON_DOWN &&
178
co->top <= ev.u.mouse.y &&
179
co->top + co->height - !bu->compact > ev.u.mouse.y &&
180
co->left <= ev.u.mouse.x &&
181
co->left + co->width - !bu->compact > ev.u.mouse.x) {
183
buttonDrawIt(co, 1, 1);
186
buttonDrawIt(co, 1, 0);
190
er.result = ER_EXITFORM;
195
er.result = ER_IGNORED;