]>
Commit | Line | Data |
---|---|---|
139f06bc | 1 | #include <slang.h> |
6fb96a3f | 2 | #include <stdlib.h> |
05130221 | 3 | #include <string.h> |
6fb96a3f | 4 | |
5 | #include "newt.h" | |
6 | #include "newt_pr.h" | |
7 | ||
8 | struct button { | |
9 | char * text; | |
c3b1b1a7 | 10 | int compact; |
6fb96a3f | 11 | }; |
12 | ||
13 | static void buttonDrawIt(newtComponent co, int active, int pushed); | |
14 | static void buttonDrawText(newtComponent co, int active, int pushed); | |
15 | ||
16 | static void buttonDraw(newtComponent c); | |
17 | static void buttonDestroy(newtComponent co); | |
45c366b1 | 18 | static struct eventResult buttonEvent(newtComponent c, |
6f481af2 | 19 | struct event ev); |
8f52cd47 | 20 | static void buttonPlace(newtComponent co, int newLeft, int newTop); |
6fb96a3f | 21 | |
22 | static struct componentOps buttonOps = { | |
23 | buttonDraw, | |
24 | buttonEvent, | |
25 | buttonDestroy, | |
e6922740 | 26 | buttonPlace, |
8f52cd47 | 27 | newtDefaultMappedHandler, |
6fb96a3f | 28 | } ; |
29 | ||
b3ddfdd7 | 30 | /* |
31 | * return NULL on malloc error. | |
32 | * FIXME: all createButton calls should check for error | |
33 | */ | |
d4109c37 | 34 | static newtComponent createButton(int left, int row, const char * text, int compact) { |
6fb96a3f | 35 | newtComponent co; |
36 | struct button * bu; | |
349586bb | 37 | int width = wstrlen(text,-1); |
6fb96a3f | 38 | |
39 | co = malloc(sizeof(*co)); | |
b3ddfdd7 | 40 | if (co == NULL) |
6f481af2 | 41 | return NULL; |
6fb96a3f | 42 | bu = malloc(sizeof(struct button)); |
b3ddfdd7 | 43 | if (bu == NULL) { |
6f481af2 | 44 | free (co); |
45 | return NULL; | |
b3ddfdd7 | 46 | } |
6fb96a3f | 47 | co->data = bu; |
c101e99e | 48 | co->destroyCallback = NULL; |
6fb96a3f | 49 | |
50 | bu->text = strdup(text); | |
c3b1b1a7 | 51 | bu->compact = compact; |
6fb96a3f | 52 | co->ops = &buttonOps; |
53 | ||
c3b1b1a7 | 54 | if (bu->compact) { |
6f481af2 | 55 | co->height = 1; |
56 | co->width = width + 3; | |
c3b1b1a7 | 57 | } else { |
6f481af2 | 58 | co->height = 4; |
59 | co->width = width + 5; | |
c3b1b1a7 | 60 | } |
61 | ||
6fb96a3f | 62 | co->top = row; |
63 | co->left = left; | |
64 | co->takesFocus = 1; | |
993036dd | 65 | co->isMapped = 0; |
6fb96a3f | 66 | |
67 | newtGotorc(co->top, co->left); | |
6fb96a3f | 68 | |
69 | return co; | |
b3ddfdd7 | 70 | |
6fb96a3f | 71 | } |
72 | ||
d4109c37 | 73 | newtComponent newtCompactButton(int left, int row, const char * text) { |
c3b1b1a7 | 74 | return createButton(left, row, text, 1); |
75 | } | |
76 | ||
d4109c37 | 77 | newtComponent newtButton(int left, int row, const char * text) { |
c3b1b1a7 | 78 | return createButton(left, row, text, 0); |
79 | } | |
80 | ||
6fb96a3f | 81 | static void buttonDestroy(newtComponent co) { |
82 | struct button * bu = co->data; | |
83 | ||
84 | free(bu->text); | |
85 | free(bu); | |
86 | free(co); | |
87 | } | |
88 | ||
8f52cd47 | 89 | static void buttonPlace(newtComponent co, int newLeft, int newTop) { |
8f52cd47 | 90 | co->top = newTop; |
91 | co->left = newLeft; | |
92 | ||
e6922740 | 93 | newtGotorc(co->top, co->left); |
e6922740 | 94 | } |
95 | ||
6fb96a3f | 96 | static void buttonDraw(newtComponent co) { |
97 | buttonDrawIt(co, 0, 0); | |
98 | } | |
99 | ||
100 | static void buttonDrawIt(newtComponent co, int active, int pushed) { | |
101 | struct button * bu = co->data; | |
102 | ||
993036dd | 103 | if (!co->isMapped) return; |
104 | ||
c3b1b1a7 | 105 | SLsmg_set_color(NEWT_COLORSET_BUTTON); |
106 | ||
107 | if (bu->compact) { | |
feef2cb5 | 108 | if (!active) |
6f481af2 | 109 | SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON); |
e9d24522 | 110 | else if (SLtt_Use_Ansi_Colors) |
6f481af2 | 111 | SLsmg_set_color(NEWT_COLORSET_BUTTON); |
e9d24522 | 112 | else |
113 | SLsmg_set_color(NEWT_COLORSET_ACTBUTTON); | |
6f481af2 | 114 | newtGotorc(co->top+ pushed, co->left + 1 + pushed); |
115 | SLsmg_write_char('<'); | |
10de8f4a | 116 | SLsmg_write_string(bu->text); |
6f481af2 | 117 | SLsmg_write_char('>'); |
6fb96a3f | 118 | } else { |
6f481af2 | 119 | if (pushed) { |
120 | SLsmg_set_color(NEWT_COLORSET_BUTTON); | |
121 | newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0); | |
122 | ||
123 | SLsmg_set_color(NEWT_COLORSET_WINDOW); | |
124 | newtClearBox(co->left, co->top, co->width, 1); | |
125 | newtClearBox(co->left, co->top, 1, co->height); | |
126 | } else { | |
127 | newtDrawBox(co->left, co->top, co->width - 1, 3, 1); | |
128 | } | |
129 | ||
130 | buttonDrawText(co, active, pushed); | |
c3b1b1a7 | 131 | } |
4e667865 | 132 | /* put cursor at beginning of text for better accessibility */ |
133 | newtGotorc(co->top + (bu->compact ? 0 : 1) + pushed, co->left + 1 + pushed + 1); | |
6fb96a3f | 134 | } |
135 | ||
136 | static void buttonDrawText(newtComponent co, int active, int pushed) { | |
137 | struct button * bu = co->data; | |
138 | ||
139 | if (pushed) pushed = 1; | |
140 | ||
141 | if (active) | |
6f481af2 | 142 | SLsmg_set_color(NEWT_COLORSET_ACTBUTTON); |
6fb96a3f | 143 | else |
6f481af2 | 144 | SLsmg_set_color(NEWT_COLORSET_BUTTON); |
6fb96a3f | 145 | |
146 | newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed); | |
147 | SLsmg_write_char(' '); | |
10de8f4a | 148 | SLsmg_write_string(bu->text); |
6fb96a3f | 149 | SLsmg_write_char(' '); |
150 | } | |
151 | ||
45c366b1 | 152 | static struct eventResult buttonEvent(newtComponent co, |
6f481af2 | 153 | struct event ev) { |
6fb96a3f | 154 | struct eventResult er; |
c3b1b1a7 | 155 | struct button * bu = co->data; |
6fb96a3f | 156 | |
c00a8d1b | 157 | er.result = ER_IGNORED; |
158 | ||
c1a075d9 | 159 | if (ev.when == EV_NORMAL) { |
6f481af2 | 160 | switch (ev.event) { |
161 | case EV_FOCUS: | |
162 | buttonDrawIt(co, 1, 0); | |
163 | er.result = ER_SWALLOWED; | |
164 | break; | |
165 | ||
166 | case EV_UNFOCUS: | |
167 | buttonDrawIt(co, 0, 0); | |
168 | er.result = ER_SWALLOWED; | |
169 | break; | |
170 | ||
171 | case EV_KEYPRESS: | |
172 | if (ev.u.key == ' ' || ev.u.key == '\r') { | |
173 | if (!bu->compact) { | |
174 | /* look pushed */ | |
175 | buttonDrawIt(co, 1, 1); | |
176 | newtRefresh(); | |
177 | newtDelay(150000); | |
178 | buttonDrawIt(co, 1, 0); | |
179 | newtRefresh(); | |
180 | newtDelay(150000); | |
181 | } | |
182 | ||
183 | er.result = ER_EXITFORM; | |
184 | } else | |
185 | er.result = ER_IGNORED; | |
186 | break; | |
187 | case EV_MOUSE: | |
188 | if (ev.u.mouse.type == MOUSE_BUTTON_DOWN && | |
189 | co->top <= ev.u.mouse.y && | |
190 | co->top + co->height - !bu->compact > ev.u.mouse.y && | |
191 | co->left <= ev.u.mouse.x && | |
192 | co->left + co->width - !bu->compact > ev.u.mouse.x) { | |
193 | if (!bu->compact) { | |
194 | buttonDrawIt(co, 1, 1); | |
195 | newtRefresh(); | |
196 | newtDelay(150000); | |
197 | buttonDrawIt(co, 1, 0); | |
198 | newtRefresh(); | |
199 | newtDelay(150000); | |
200 | } | |
201 | er.result = ER_EXITFORM; | |
202 | } | |
203 | break; | |
204 | } | |
c00a8d1b | 205 | } |
c1a075d9 | 206 | |
207 | return er; | |
6fb96a3f | 208 | } |