]>
Commit | Line | Data |
---|---|---|
6fb96a3f | 1 | #include <slang/slang.h> |
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; | |
10 | char bgColor; | |
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); |
20 | ||
21 | static struct componentOps buttonOps = { | |
22 | buttonDraw, | |
23 | buttonEvent, | |
24 | buttonDestroy, | |
25 | } ; | |
26 | ||
27 | newtComponent newtButton(int left, int row, char * text) { | |
28 | newtComponent co; | |
29 | struct button * bu; | |
30 | ||
31 | co = malloc(sizeof(*co)); | |
32 | bu = malloc(sizeof(struct button)); | |
33 | co->data = bu; | |
34 | ||
35 | bu->text = strdup(text); | |
36 | co->ops = &buttonOps; | |
37 | ||
38 | co->height = 4; | |
39 | co->width = strlen(text) + 5; | |
40 | co->top = row; | |
41 | co->left = left; | |
42 | co->takesFocus = 1; | |
43 | ||
44 | newtGotorc(co->top, co->left); | |
45 | bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF; | |
46 | ||
47 | return co; | |
48 | } | |
49 | ||
50 | static void buttonDestroy(newtComponent co) { | |
51 | struct button * bu = co->data; | |
52 | ||
53 | free(bu->text); | |
54 | free(bu); | |
55 | free(co); | |
56 | } | |
57 | ||
58 | static void buttonDraw(newtComponent co) { | |
59 | buttonDrawIt(co, 0, 0); | |
60 | } | |
61 | ||
62 | static void buttonDrawIt(newtComponent co, int active, int pushed) { | |
63 | struct button * bu = co->data; | |
64 | ||
65 | SLsmg_set_color(COLORSET_BUTTON); | |
66 | if (pushed) { | |
67 | SLsmg_set_color(COLORSET_BUTTON); | |
68 | newtDrawBox(co->left + 1, co->top + 1, co->width - 1, 3, 0); | |
69 | ||
70 | SLsmg_set_color(bu->bgColor); | |
71 | newtClearBox(co->left, co->top, co->width, 1); | |
72 | newtClearBox(co->left, co->top, 1, co->height); | |
73 | } else { | |
74 | newtDrawBox(co->left, co->top, co->width - 1, 3, 1); | |
75 | } | |
76 | ||
77 | buttonDrawText(co, active, pushed); | |
78 | } | |
79 | ||
80 | static void buttonDrawText(newtComponent co, int active, int pushed) { | |
81 | struct button * bu = co->data; | |
82 | ||
83 | if (pushed) pushed = 1; | |
84 | ||
85 | if (active) | |
86 | SLsmg_set_color(COLORSET_ACTBUTTON); | |
87 | else | |
88 | SLsmg_set_color(COLORSET_BUTTON); | |
89 | ||
90 | newtGotorc(co->top + 1 + pushed, co->left + 1 + pushed); | |
91 | SLsmg_write_char(' '); | |
92 | SLsmg_write_string(bu->text); | |
93 | SLsmg_write_char(' '); | |
94 | } | |
95 | ||
45c366b1 | 96 | static struct eventResult buttonEvent(newtComponent co, |
6fb96a3f | 97 | struct event ev) { |
98 | struct eventResult er; | |
99 | ||
c1a075d9 | 100 | if (ev.when == EV_NORMAL) { |
101 | switch (ev.event) { | |
102 | case EV_FOCUS: | |
6fb96a3f | 103 | buttonDrawIt(co, 1, 0); |
c1a075d9 | 104 | er.result = ER_SWALLOWED; |
105 | break; | |
106 | ||
107 | case EV_UNFOCUS: | |
108 | buttonDrawIt(co, 0, 0); | |
109 | er.result = ER_SWALLOWED; | |
110 | break; | |
111 | ||
112 | case EV_KEYPRESS: | |
113 | if (ev.u.key == ' ' || ev.u.key == '\r') { | |
114 | /* look pushed */ | |
115 | buttonDrawIt(co, 1, 1); | |
116 | newtRefresh(); | |
117 | newtDelay(300000); | |
118 | buttonDrawIt(co, 1, 0); | |
119 | newtRefresh(); | |
120 | newtDelay(300000); | |
121 | ||
122 | er.result = ER_EXITFORM; | |
123 | } else | |
124 | er.result = ER_IGNORED; | |
125 | break; | |
126 | } | |
127 | } else | |
128 | er.result = ER_IGNORED; | |
129 | ||
130 | return er; | |
6fb96a3f | 131 | } |