]> git.ipfire.org Git - thirdparty/newt.git/blob - button.c
install python modules to purelib and platlib
[thirdparty/newt.git] / button.c
1 #include <slang.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "newt.h"
6 #include "newt_pr.h"
7
8 struct button {
9 char * text;
10 int compact;
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);
18 static struct eventResult buttonEvent(newtComponent c,
19 struct event ev);
20 static void buttonPlace(newtComponent co, int newLeft, int newTop);
21
22 static struct componentOps buttonOps = {
23 buttonDraw,
24 buttonEvent,
25 buttonDestroy,
26 buttonPlace,
27 newtDefaultMappedHandler,
28 } ;
29
30 /*
31 * return NULL on malloc error.
32 * FIXME: all createButton calls should check for error
33 */
34 static newtComponent createButton(int left, int row, const char * text, int compact) {
35 newtComponent co;
36 struct button * bu;
37 int width = wstrlen(text,-1);
38
39 co = malloc(sizeof(*co));
40 if (co == NULL)
41 return NULL;
42 bu = malloc(sizeof(struct button));
43 if (bu == NULL) {
44 free (co);
45 return NULL;
46 }
47 co->data = bu;
48 co->destroyCallback = NULL;
49
50 bu->text = strdup(text);
51 bu->compact = compact;
52 co->ops = &buttonOps;
53
54 if (bu->compact) {
55 co->height = 1;
56 co->width = width + 3;
57 } else {
58 co->height = 4;
59 co->width = width + 5;
60 }
61
62 co->top = row;
63 co->left = left;
64 co->takesFocus = 1;
65 co->isMapped = 0;
66
67 newtGotorc(co->top, co->left);
68
69 return co;
70
71 }
72
73 newtComponent newtCompactButton(int left, int row, const char * text) {
74 return createButton(left, row, text, 1);
75 }
76
77 newtComponent newtButton(int left, int row, const char * text) {
78 return createButton(left, row, text, 0);
79 }
80
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
89 static void buttonPlace(newtComponent co, int newLeft, int newTop) {
90 co->top = newTop;
91 co->left = newLeft;
92
93 newtGotorc(co->top, co->left);
94 }
95
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
103 if (!co->isMapped) return;
104
105 SLsmg_set_color(NEWT_COLORSET_BUTTON);
106
107 if (bu->compact) {
108 if (!active)
109 SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON);
110 else if (SLtt_Use_Ansi_Colors)
111 SLsmg_set_color(NEWT_COLORSET_BUTTON);
112 else
113 SLsmg_set_color(NEWT_COLORSET_ACTBUTTON);
114 newtGotorc(co->top+ pushed, co->left + 1 + pushed);
115 SLsmg_write_char('<');
116 SLsmg_write_string(bu->text);
117 SLsmg_write_char('>');
118 } else {
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);
131 }
132 /* put cursor at beginning of text for better accessibility */
133 newtGotorc(co->top + (bu->compact ? 0 : 1) + pushed, co->left + 1 + pushed + 1);
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)
142 SLsmg_set_color(NEWT_COLORSET_ACTBUTTON);
143 else
144 SLsmg_set_color(NEWT_COLORSET_BUTTON);
145
146 newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed);
147 SLsmg_write_char(' ');
148 SLsmg_write_string(bu->text);
149 SLsmg_write_char(' ');
150 }
151
152 static struct eventResult buttonEvent(newtComponent co,
153 struct event ev) {
154 struct eventResult er;
155 struct button * bu = co->data;
156
157 er.result = ER_IGNORED;
158
159 if (ev.when == EV_NORMAL) {
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 }
205 }
206
207 return er;
208 }