]>
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, |
6fb96a3f | 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 | ||
d4109c37 | 30 | static newtComponent createButton(int left, int row, const char * text, int compact) { |
6fb96a3f | 31 | newtComponent co; |
32 | struct button * bu; | |
33 | ||
34 | co = malloc(sizeof(*co)); | |
35 | bu = malloc(sizeof(struct button)); | |
36 | co->data = bu; | |
37 | ||
38 | bu->text = strdup(text); | |
c3b1b1a7 | 39 | bu->compact = compact; |
6fb96a3f | 40 | co->ops = &buttonOps; |
41 | ||
c3b1b1a7 | 42 | if (bu->compact) { |
43 | co->height = 1; | |
44 | co->width = strlen(text) + 3; | |
45 | } else { | |
46 | co->height = 4; | |
47 | co->width = strlen(text) + 5; | |
48 | } | |
49 | ||
6fb96a3f | 50 | co->top = row; |
51 | co->left = left; | |
52 | co->takesFocus = 1; | |
993036dd | 53 | co->isMapped = 0; |
6fb96a3f | 54 | |
55 | newtGotorc(co->top, co->left); | |
6fb96a3f | 56 | |
57 | return co; | |
58 | } | |
59 | ||
d4109c37 | 60 | newtComponent newtCompactButton(int left, int row, const char * text) { |
c3b1b1a7 | 61 | return createButton(left, row, text, 1); |
62 | } | |
63 | ||
d4109c37 | 64 | newtComponent newtButton(int left, int row, const char * text) { |
c3b1b1a7 | 65 | return createButton(left, row, text, 0); |
66 | } | |
67 | ||
6fb96a3f | 68 | static void buttonDestroy(newtComponent co) { |
69 | struct button * bu = co->data; | |
70 | ||
71 | free(bu->text); | |
72 | free(bu); | |
73 | free(co); | |
74 | } | |
75 | ||
8f52cd47 | 76 | static void buttonPlace(newtComponent co, int newLeft, int newTop) { |
8f52cd47 | 77 | co->top = newTop; |
78 | co->left = newLeft; | |
79 | ||
e6922740 | 80 | newtGotorc(co->top, co->left); |
e6922740 | 81 | } |
82 | ||
6fb96a3f | 83 | static void buttonDraw(newtComponent co) { |
84 | buttonDrawIt(co, 0, 0); | |
85 | } | |
86 | ||
87 | static void buttonDrawIt(newtComponent co, int active, int pushed) { | |
88 | struct button * bu = co->data; | |
89 | ||
993036dd | 90 | if (!co->isMapped) return; |
91 | ||
c3b1b1a7 | 92 | SLsmg_set_color(NEWT_COLORSET_BUTTON); |
93 | ||
94 | if (bu->compact) { | |
45f6c4fd | 95 | if (active) |
c3b1b1a7 | 96 | SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON); |
97 | else | |
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('>'); | |
6fb96a3f | 103 | } else { |
c3b1b1a7 | 104 | if (pushed) { |
105 | SLsmg_set_color(NEWT_COLORSET_BUTTON); | |
106 | newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0); | |
107 | ||
9a22c91e | 108 | SLsmg_set_color(NEWT_COLORSET_WINDOW); |
c3b1b1a7 | 109 | newtClearBox(co->left, co->top, co->width, 1); |
110 | newtClearBox(co->left, co->top, 1, co->height); | |
111 | } else { | |
112 | newtDrawBox(co->left, co->top, co->width - 1, 3, 1); | |
113 | } | |
6fb96a3f | 114 | |
c3b1b1a7 | 115 | buttonDrawText(co, active, pushed); |
116 | } | |
6fb96a3f | 117 | } |
118 | ||
119 | static void buttonDrawText(newtComponent co, int active, int pushed) { | |
120 | struct button * bu = co->data; | |
121 | ||
122 | if (pushed) pushed = 1; | |
123 | ||
124 | if (active) | |
c3b1b1a7 | 125 | SLsmg_set_color(NEWT_COLORSET_ACTBUTTON); |
6fb96a3f | 126 | else |
c3b1b1a7 | 127 | SLsmg_set_color(NEWT_COLORSET_BUTTON); |
6fb96a3f | 128 | |
129 | newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed); | |
130 | SLsmg_write_char(' '); | |
131 | SLsmg_write_string(bu->text); | |
132 | SLsmg_write_char(' '); | |
133 | } | |
134 | ||
45c366b1 | 135 | static struct eventResult buttonEvent(newtComponent co, |
6fb96a3f | 136 | struct event ev) { |
137 | struct eventResult er; | |
c3b1b1a7 | 138 | struct button * bu = co->data; |
6fb96a3f | 139 | |
c1a075d9 | 140 | if (ev.when == EV_NORMAL) { |
141 | switch (ev.event) { | |
142 | case EV_FOCUS: | |
6fb96a3f | 143 | buttonDrawIt(co, 1, 0); |
c1a075d9 | 144 | er.result = ER_SWALLOWED; |
145 | break; | |
146 | ||
147 | case EV_UNFOCUS: | |
148 | buttonDrawIt(co, 0, 0); | |
149 | er.result = ER_SWALLOWED; | |
150 | break; | |
151 | ||
152 | case EV_KEYPRESS: | |
153 | if (ev.u.key == ' ' || ev.u.key == '\r') { | |
c3b1b1a7 | 154 | if (!bu->compact) { |
155 | /* look pushed */ | |
156 | buttonDrawIt(co, 1, 1); | |
157 | newtRefresh(); | |
24a2ad22 | 158 | newtDelay(150000); |
c3b1b1a7 | 159 | buttonDrawIt(co, 1, 0); |
160 | newtRefresh(); | |
24a2ad22 | 161 | newtDelay(150000); |
c3b1b1a7 | 162 | } |
c1a075d9 | 163 | |
164 | er.result = ER_EXITFORM; | |
45f6c4fd | 165 | } else |
c1a075d9 | 166 | er.result = ER_IGNORED; |
167 | break; | |
45f6c4fd | 168 | case EV_MOUSE: |
33b9b6eb | 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) { | |
174 | if (!bu->compact) { | |
175 | buttonDrawIt(co, 1, 1); | |
176 | newtRefresh(); | |
177 | newtDelay(150000); | |
178 | buttonDrawIt(co, 1, 0); | |
179 | newtRefresh(); | |
180 | newtDelay(150000); | |
181 | } | |
182 | er.result = ER_EXITFORM; | |
183 | } | |
45f6c4fd | 184 | break; |
c1a075d9 | 185 | } |
45f6c4fd | 186 | } else |
c1a075d9 | 187 | er.result = ER_IGNORED; |
188 | ||
189 | return er; | |
6fb96a3f | 190 | } |