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