13 static void buttonDrawIt(newtComponent co
, int active
, int pushed
);
14 static void buttonDrawText(newtComponent co
, int active
, int pushed
);
16 static void buttonDraw(newtComponent c
);
17 static void buttonDestroy(newtComponent co
);
18 static struct eventResult
buttonEvent(newtComponent c
,
20 static void buttonPlace(newtComponent co
, int newLeft
, int newTop
);
22 static struct componentOps buttonOps
= {
27 newtDefaultMappedHandler
,
31 * return NULL on malloc error.
32 * FIXME: all createButton calls should check for error
34 static newtComponent
createButton(int left
, int row
, const char * text
, int compact
) {
37 int width
= wstrlen(text
,-1);
39 co
= malloc(sizeof(*co
));
42 bu
= malloc(sizeof(struct button
));
48 co
->destroyCallback
= NULL
;
50 bu
->text
= strdup(text
);
51 bu
->compact
= compact
;
56 co
->width
= width
+ 3;
59 co
->width
= width
+ 5;
67 newtGotorc(co
->top
, co
->left
);
73 newtComponent
newtCompactButton(int left
, int row
, const char * text
) {
74 return createButton(left
, row
, text
, 1);
77 newtComponent
newtButton(int left
, int row
, const char * text
) {
78 return createButton(left
, row
, text
, 0);
81 static void buttonDestroy(newtComponent co
) {
82 struct button
* bu
= co
->data
;
89 static void buttonPlace(newtComponent co
, int newLeft
, int newTop
) {
93 newtGotorc(co
->top
, co
->left
);
96 static void buttonDraw(newtComponent co
) {
97 buttonDrawIt(co
, 0, 0);
100 static void buttonDrawIt(newtComponent co
, int active
, int pushed
) {
101 struct button
* bu
= co
->data
;
103 if (!co
->isMapped
) return;
105 SLsmg_set_color(NEWT_COLORSET_BUTTON
);
109 SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON
);
110 else if (SLtt_Use_Ansi_Colors
)
111 SLsmg_set_color(NEWT_COLORSET_BUTTON
);
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('>');
120 SLsmg_set_color(NEWT_COLORSET_BUTTON
);
121 newtDrawBox(co
->left
+ 1, co
->top
+ 1, co
->width
- 1, 3, 0);
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
);
127 newtDrawBox(co
->left
, co
->top
, co
->width
- 1, 3, 1);
130 buttonDrawText(co
, active
, pushed
);
132 /* put cursor at beginning of text for better accessibility */
133 newtGotorc(co
->top
+ (bu
->compact
? 0 : 1) + pushed
, co
->left
+ 1 + pushed
+ 1);
136 static void buttonDrawText(newtComponent co
, int active
, int pushed
) {
137 struct button
* bu
= co
->data
;
139 if (pushed
) pushed
= 1;
142 SLsmg_set_color(NEWT_COLORSET_ACTBUTTON
);
144 SLsmg_set_color(NEWT_COLORSET_BUTTON
);
146 newtGotorc(co
->top
+ 1 + pushed
, co
->left
+ 1 + pushed
);
147 SLsmg_write_char(' ');
148 SLsmg_write_string(bu
->text
);
149 SLsmg_write_char(' ');
152 static struct eventResult
buttonEvent(newtComponent co
,
154 struct eventResult er
;
155 struct button
* bu
= co
->data
;
157 er
.result
= ER_IGNORED
;
159 if (ev
.when
== EV_NORMAL
) {
162 buttonDrawIt(co
, 1, 0);
163 er
.result
= ER_SWALLOWED
;
167 buttonDrawIt(co
, 0, 0);
168 er
.result
= ER_SWALLOWED
;
172 if (ev
.u
.key
== ' ' || ev
.u
.key
== '\r') {
175 buttonDrawIt(co
, 1, 1);
178 buttonDrawIt(co
, 1, 0);
183 er
.result
= ER_EXITFORM
;
185 er
.result
= ER_IGNORED
;
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
) {
194 buttonDrawIt(co
, 1, 1);
197 buttonDrawIt(co
, 1, 0);
201 er
.result
= ER_EXITFORM
;