]> git.ipfire.org Git - thirdparty/newt.git/blame - button.c
- handle large values correctly
[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,
6f481af2 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
b3ddfdd7 30/*
31 * return NULL on malloc error.
32 * FIXME: all createButton calls should check for error
33 */
d4109c37 34static 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;
48
49 bu->text = strdup(text);
c3b1b1a7 50 bu->compact = compact;
6fb96a3f 51 co->ops = &buttonOps;
52
c3b1b1a7 53 if (bu->compact) {
6f481af2 54 co->height = 1;
55 co->width = width + 3;
c3b1b1a7 56 } else {
6f481af2 57 co->height = 4;
58 co->width = width + 5;
c3b1b1a7 59 }
60
6fb96a3f 61 co->top = row;
62 co->left = left;
63 co->takesFocus = 1;
993036dd 64 co->isMapped = 0;
6fb96a3f 65
66 newtGotorc(co->top, co->left);
6fb96a3f 67
68 return co;
b3ddfdd7 69
6fb96a3f 70}
71
d4109c37 72newtComponent newtCompactButton(int left, int row, const char * text) {
c3b1b1a7 73 return createButton(left, row, text, 1);
74}
75
d4109c37 76newtComponent newtButton(int left, int row, const char * text) {
c3b1b1a7 77 return createButton(left, row, text, 0);
78}
79
6fb96a3f 80static void buttonDestroy(newtComponent co) {
81 struct button * bu = co->data;
82
83 free(bu->text);
84 free(bu);
85 free(co);
86}
87
8f52cd47 88static void buttonPlace(newtComponent co, int newLeft, int newTop) {
8f52cd47 89 co->top = newTop;
90 co->left = newLeft;
91
e6922740 92 newtGotorc(co->top, co->left);
e6922740 93}
94
6fb96a3f 95static void buttonDraw(newtComponent co) {
96 buttonDrawIt(co, 0, 0);
97}
98
99static void buttonDrawIt(newtComponent co, int active, int pushed) {
100 struct button * bu = co->data;
101
993036dd 102 if (!co->isMapped) return;
103
c3b1b1a7 104 SLsmg_set_color(NEWT_COLORSET_BUTTON);
105
106 if (bu->compact) {
feef2cb5 107 if (!active)
6f481af2 108 SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON);
109 else
110 SLsmg_set_color(NEWT_COLORSET_BUTTON);
111 newtGotorc(co->top+ pushed, co->left + 1 + pushed);
112 SLsmg_write_char('<');
10de8f4a 113 SLsmg_write_string(bu->text);
6f481af2 114 SLsmg_write_char('>');
6fb96a3f 115 } else {
6f481af2 116 if (pushed) {
117 SLsmg_set_color(NEWT_COLORSET_BUTTON);
118 newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0);
119
120 SLsmg_set_color(NEWT_COLORSET_WINDOW);
121 newtClearBox(co->left, co->top, co->width, 1);
122 newtClearBox(co->left, co->top, 1, co->height);
123 } else {
124 newtDrawBox(co->left, co->top, co->width - 1, 3, 1);
125 }
126
127 buttonDrawText(co, active, pushed);
c3b1b1a7 128 }
6fb96a3f 129}
130
131static void buttonDrawText(newtComponent co, int active, int pushed) {
132 struct button * bu = co->data;
133
134 if (pushed) pushed = 1;
135
136 if (active)
6f481af2 137 SLsmg_set_color(NEWT_COLORSET_ACTBUTTON);
6fb96a3f 138 else
6f481af2 139 SLsmg_set_color(NEWT_COLORSET_BUTTON);
6fb96a3f 140
141 newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed);
142 SLsmg_write_char(' ');
10de8f4a 143 SLsmg_write_string(bu->text);
6fb96a3f 144 SLsmg_write_char(' ');
145}
146
45c366b1 147static struct eventResult buttonEvent(newtComponent co,
6f481af2 148 struct event ev) {
6fb96a3f 149 struct eventResult er;
c3b1b1a7 150 struct button * bu = co->data;
6fb96a3f 151
c00a8d1b 152 er.result = ER_IGNORED;
153
c1a075d9 154 if (ev.when == EV_NORMAL) {
6f481af2 155 switch (ev.event) {
156 case EV_FOCUS:
157 buttonDrawIt(co, 1, 0);
158 er.result = ER_SWALLOWED;
159 break;
160
161 case EV_UNFOCUS:
162 buttonDrawIt(co, 0, 0);
163 er.result = ER_SWALLOWED;
164 break;
165
166 case EV_KEYPRESS:
167 if (ev.u.key == ' ' || ev.u.key == '\r') {
168 if (!bu->compact) {
169 /* look pushed */
170 buttonDrawIt(co, 1, 1);
171 newtRefresh();
172 newtDelay(150000);
173 buttonDrawIt(co, 1, 0);
174 newtRefresh();
175 newtDelay(150000);
176 }
177
178 er.result = ER_EXITFORM;
179 } else
180 er.result = ER_IGNORED;
181 break;
182 case EV_MOUSE:
183 if (ev.u.mouse.type == MOUSE_BUTTON_DOWN &&
184 co->top <= ev.u.mouse.y &&
185 co->top + co->height - !bu->compact > ev.u.mouse.y &&
186 co->left <= ev.u.mouse.x &&
187 co->left + co->width - !bu->compact > ev.u.mouse.x) {
188 if (!bu->compact) {
189 buttonDrawIt(co, 1, 1);
190 newtRefresh();
191 newtDelay(150000);
192 buttonDrawIt(co, 1, 0);
193 newtRefresh();
194 newtDelay(150000);
195 }
196 er.result = ER_EXITFORM;
197 }
198 break;
199 }
c00a8d1b 200 }
c1a075d9 201
202 return er;
6fb96a3f 203}