~ubuntu-branches/ubuntu/dapper/newt/dapper-security

« back to all changes in this revision

Viewing changes to button.c

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2002-03-31 09:38:18 UTC
  • Revision ID: james.westby@ubuntu.com-20020331093818-t3cla7103r07qnyw
Tags: upstream-0.50.17
ImportĀ upstreamĀ versionĀ 0.50.17

Show diffs side-by-side

added added

removed removed

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