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

« back to all changes in this revision

Viewing changes to button.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2005-03-22 12:44:37 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050322124437-nuhl0pqjcijjno9z
Tags: 0.51.6-20ubuntu3
Add Xhosa translation (thanks, Adi Attar).

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