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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <slang.h>
#include <stdlib.h>
#include <string.h>
#include "newt.h"
#include "newt_pr.h"
struct button {
char * text;
char bgColor;
int compact;
};
static void buttonDrawIt(newtComponent co, int active, int pushed);
static void buttonDrawText(newtComponent co, int active, int pushed);
static void buttonDraw(newtComponent c);
static void buttonDestroy(newtComponent co);
static struct eventResult buttonEvent(newtComponent c,
struct event ev);
static void buttonPlace(newtComponent co);
static const struct componentOps buttonOps = {
buttonDraw,
buttonEvent,
buttonDestroy,
buttonPlace,
} ;
static newtComponent createButton(int left, int row, const char * text, int compact) {
newtComponent co;
struct button * bu;
co = malloc(sizeof(*co));
bu = malloc(sizeof(struct button));
co->data = bu;
bu->text = strdup(text);
bu->compact = compact;
co->ops = &buttonOps;
if (bu->compact) {
co->height = 1;
co->width = strlen(text) + 3;
} else {
co->height = 4;
co->width = strlen(text) + 5;
}
co->top = row;
co->left = left;
co->takesFocus = 1;
newtGotorc(co->top, co->left);
bu->bgColor = -1;
return co;
}
newtComponent newtCompactButton(int left, int row, const char * text) {
return createButton(left, row, text, 1);
}
newtComponent newtButton(int left, int row, const char * text) {
return createButton(left, row, text, 0);
}
static void buttonDestroy(newtComponent co) {
struct button * bu = co->data;
free(bu->text);
free(bu);
free(co);
}
static void buttonPlace(newtComponent co) {
struct button * bu = co->data;
newtGotorc(co->top, co->left);
bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF;
}
static void buttonDraw(newtComponent co) {
buttonDrawIt(co, 0, 0);
}
static void buttonDrawIt(newtComponent co, int active, int pushed) {
struct button * bu = co->data;
if (bu->bgColor == -1) {
newtGotorc(co->top, co->left);
bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF;
}
SLsmg_set_color(NEWT_COLORSET_BUTTON);
if (bu->compact) {
if (active)
SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON);
else
SLsmg_set_color(NEWT_COLORSET_BUTTON);
newtGotorc(co->top+ pushed, co->left + 1 + pushed);
SLsmg_write_char('<');
SLsmg_write_string(bu->text);
SLsmg_write_char('>');
} else {
if (pushed) {
SLsmg_set_color(NEWT_COLORSET_BUTTON);
newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0);
SLsmg_set_color(bu->bgColor);
newtClearBox(co->left, co->top, co->width, 1);
newtClearBox(co->left, co->top, 1, co->height);
} else {
newtDrawBox(co->left, co->top, co->width - 1, 3, 1);
}
buttonDrawText(co, active, pushed);
}
}
static void buttonDrawText(newtComponent co, int active, int pushed) {
struct button * bu = co->data;
if (pushed) pushed = 1;
if (active)
SLsmg_set_color(NEWT_COLORSET_ACTBUTTON);
else
SLsmg_set_color(NEWT_COLORSET_BUTTON);
newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed);
SLsmg_write_char(' ');
SLsmg_write_string(bu->text);
SLsmg_write_char(' ');
}
static struct eventResult buttonEvent(newtComponent co,
struct event ev) {
struct eventResult er;
struct button * bu = co->data;
if (ev.when == EV_NORMAL) {
switch (ev.event) {
case EV_FOCUS:
buttonDrawIt(co, 1, 0);
er.result = ER_SWALLOWED;
break;
case EV_UNFOCUS:
buttonDrawIt(co, 0, 0);
er.result = ER_SWALLOWED;
break;
case EV_KEYPRESS:
if (ev.u.key == ' ' || ev.u.key == '\r') {
if (!bu->compact) {
/* look pushed */
buttonDrawIt(co, 1, 1);
newtRefresh();
newtDelay(150000);
buttonDrawIt(co, 1, 0);
newtRefresh();
newtDelay(150000);
}
er.result = ER_EXITFORM;
} else
er.result = ER_IGNORED;
break;
}
} else
er.result = ER_IGNORED;
return er;
}
|