~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to lefty/ws/x11/gtext.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma prototyped
 
2
/* Lefteris Koutsofios - AT&T Bell Laboratories */
 
3
 
 
4
#include "common.h"
 
5
#include "g.h"
 
6
#include "gcommon.h"
 
7
#include "mem.h"
 
8
 
 
9
#define WTU widget->u.t
 
10
 
 
11
int GTcreatewidget (Gwidget_t *parent, Gwidget_t *widget,
 
12
        int attrn, Gwattr_t *attrp) {
 
13
    PIXsize_t ps;
 
14
    char *s;
 
15
    int ai;
 
16
    XColor c;
 
17
    int color;
 
18
 
 
19
    static XawTextSelectType sarray[] = {
 
20
        XawselectLine, XawselectNull
 
21
    };
 
22
 
 
23
    if (!parent) {
 
24
        Gerr (POS, G_ERRNOPARENTWIDGET);
 
25
        return -1;
 
26
    }
 
27
    WTU->func = NULL;
 
28
    ps.x = ps.y = MINTWSIZE;
 
29
    s = "oneline";
 
30
    RESETARGS;
 
31
    for (ai = 0; ai < attrn; ai++) {
 
32
        switch (attrp[ai].id) {
 
33
        case G_ATTRSIZE:
 
34
            GETSIZE (attrp[ai].u.s, ps, MINTWSIZE);
 
35
            break;
 
36
        case G_ATTRBORDERWIDTH:
 
37
            ADD2ARGS (XtNborderWidth, attrp[ai].u.i);
 
38
            break;
 
39
        case G_ATTRTEXT:
 
40
            ADD2ARGS (XtNstring, attrp[ai].u.t);
 
41
            break;
 
42
        case G_ATTRAPPENDTEXT:
 
43
            Gerr (POS, G_ERRCANNOTSETATTR1, "appendtext");
 
44
            return -1;
 
45
        case G_ATTRMODE:
 
46
            s = attrp[ai].u.t;
 
47
            break;
 
48
        case G_ATTRCOLOR:
 
49
            color = attrp[ai].u.c.index;
 
50
            if (color != 0 && color != 1) {
 
51
                Gerr (POS, G_ERRBADCOLORINDEX, color);
 
52
                return -1;
 
53
            }
 
54
            c.red = attrp[ai].u.c.r * 257;
 
55
            c.green = attrp[ai].u.c.g * 257;
 
56
            c.blue = attrp[ai].u.c.b * 257;
 
57
            if (XAllocColor (
 
58
                Gdisplay, DefaultColormap (Gdisplay, Gscreenn), &c
 
59
            ))
 
60
                if (color == 0)
 
61
                    ADD2ARGS (XtNbackground, c.pixel);
 
62
                else
 
63
                    ADD2ARGS (XtNforeground, c.pixel);
 
64
            break;
 
65
        case G_ATTRWINDOWID:
 
66
            Gerr (POS, G_ERRCANNOTSETATTR1, "windowid");
 
67
            return -1;
 
68
        case G_ATTRNEWLINECB:
 
69
            WTU->func = (Gtwnewlinecb) attrp[ai].u.func;
 
70
            break;
 
71
        case G_ATTRUSERDATA:
 
72
            widget->udata = attrp[ai].u.u;
 
73
            break;
 
74
        default:
 
75
            Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
 
76
            return -1;
 
77
        }
 
78
    }
 
79
    ADD2ARGS (XtNwidth, ps.x);
 
80
    ADD2ARGS (XtNheight, ps.y);
 
81
    if (Strcmp ("oneline", s) == 0)
 
82
        ADD2ARGS (XtNeditType, XawtextAppend);
 
83
    else if (Strcmp ("input", s) == 0 || Strcmp ("select", s) == 0)
 
84
        ADD2ARGS (XtNeditType, XawtextEdit);
 
85
    else if (Strcmp ("output", s) == 0)
 
86
        ADD2ARGS (XtNeditType, XawtextRead);
 
87
    else {
 
88
        Gerr (POS, G_ERRBADATTRVALUE, s);
 
89
        return -1;
 
90
    }
 
91
    ADD2ARGS (XtNscrollHorizontal, XawtextScrollWhenNeeded);
 
92
    ADD2ARGS (XtNscrollVertical, XawtextScrollWhenNeeded);
 
93
    if (!(widget->w = XtCreateWidget ("ascii", asciiTextWidgetClass,
 
94
            parent->w, argp, argn))) {
 
95
        Gerr (POS, G_ERRCANNOTCREATEWIDGET);
 
96
        return -1;
 
97
    }
 
98
    if (Strcmp ("oneline", s) == 0 || Strcmp ("input", s) == 0)
 
99
    XtOverrideTranslations (widget->w, Gtweoltable);
 
100
    else if (Strcmp ("select", s) == 0)
 
101
        XawTextSetSelectionArray (widget->w, sarray);
 
102
    Glazymanage (widget->w);
 
103
    return 0;
 
104
}
 
105
 
 
106
int GTsetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) {
 
107
    PIXsize_t ps;
 
108
    XawTextBlock tb;
 
109
    int ai, li;
 
110
    XColor c;
 
111
    int color;
 
112
 
 
113
    RESETARGS;
 
114
    for (ai = 0; ai < attrn; ai++) {
 
115
        switch (attrp[ai].id) {
 
116
        case G_ATTRSIZE:
 
117
            GETSIZE (attrp[ai].u.s, ps, MINTWSIZE);
 
118
            ADD2ARGS (XtNwidth, ps.x);
 
119
            ADD2ARGS (XtNheight, ps.y);
 
120
            break;
 
121
        case G_ATTRBORDERWIDTH:
 
122
            ADD2ARGS (XtNborderWidth, attrp[ai].u.i);
 
123
            break;
 
124
        case G_ATTRTEXT:
 
125
            ADD2ARGS (XtNstring, attrp[ai].u.t);
 
126
            break;
 
127
        case G_ATTRAPPENDTEXT:
 
128
            XawTextSetInsertionPoint (widget->w, 327670000);
 
129
            li = XawTextGetInsertionPoint (widget->w);
 
130
            tb.firstPos = 0, tb.length = strlen (attrp[ai].u.t);
 
131
            tb.ptr = attrp[ai].u.t, tb.format = FMT8BIT;
 
132
            XawTextReplace (widget->w, li, li, &tb);
 
133
            li = XawTextGetInsertionPoint (widget->w);
 
134
            tb.firstPos = 0, tb.length = 1;
 
135
            tb.ptr = "\n", tb.format = FMT8BIT;
 
136
            XawTextReplace (widget->w, li, li, &tb);
 
137
            break;
 
138
        case G_ATTRMODE:
 
139
            if (Strcmp ("oneline", attrp[ai].u.t) == 0)
 
140
                ADD2ARGS (XtNeditType, XawtextAppend);
 
141
            else if (Strcmp ("input", attrp[ai].u.t) == 0)
 
142
                ADD2ARGS (XtNeditType, XawtextEdit);
 
143
            else if (Strcmp ("output", attrp[ai].u.t) == 0)
 
144
                ADD2ARGS (XtNeditType, XawtextRead);
 
145
            else {
 
146
                Gerr (POS, G_ERRBADATTRVALUE, attrp[ai].u.t);
 
147
                return -1;
 
148
            }
 
149
            break;
 
150
        case G_ATTRCOLOR:
 
151
            color = attrp[ai].u.c.index;
 
152
            if (color != 0 && color != 1) {
 
153
                Gerr (POS, G_ERRBADCOLORINDEX, color);
 
154
                return -1;
 
155
            }
 
156
            c.red = attrp[ai].u.c.r * 257;
 
157
            c.green = attrp[ai].u.c.g * 257;
 
158
            c.blue = attrp[ai].u.c.b * 257;
 
159
            if (XAllocColor (
 
160
                Gdisplay, DefaultColormap (Gdisplay, Gscreenn), &c
 
161
            ))
 
162
                if (color == 0)
 
163
                    ADD2ARGS (XtNbackground, c.pixel);
 
164
                else
 
165
                    ADD2ARGS (XtNforeground, c.pixel);
 
166
            break;
 
167
        case G_ATTRWINDOWID:
 
168
            Gerr (POS, G_ERRCANNOTSETATTR2, "windowid");
 
169
            return -1;
 
170
        case G_ATTRNEWLINECB:
 
171
            WTU->func = (Gtwnewlinecb) attrp[ai].u.func;
 
172
            break;
 
173
        case G_ATTRUSERDATA:
 
174
            widget->udata = attrp[ai].u.u;
 
175
            break;
 
176
        default:
 
177
            Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
 
178
            return -1;
 
179
        }
 
180
    }
 
181
    XtSetValues (widget->w, argp, argn);
 
182
    return 0;
 
183
}
 
184
 
 
185
int GTgetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) {
 
186
    Dimension width, height;
 
187
    XawTextEditType mode;
 
188
    XawTextBlock tb;
 
189
    Widget w;
 
190
    int rtn, ai;
 
191
    long fi, li;
 
192
 
 
193
    for (ai = 0; ai < attrn; ai++) {
 
194
        RESETARGS;
 
195
        switch (attrp[ai].id) {
 
196
        case G_ATTRSIZE:
 
197
            ADD2ARGS (XtNwidth, &width);
 
198
            ADD2ARGS (XtNheight, &height);
 
199
            XtGetValues (widget->w, argp, argn);
 
200
            attrp[ai].u.s.x = width, attrp[ai].u.s.y = height;
 
201
            break;
 
202
        case G_ATTRBORDERWIDTH:
 
203
            ADD2ARGS (XtNborderWidth, &width);
 
204
            XtGetValues (widget->w, argp, argn);
 
205
            attrp[ai].u.i = width;
 
206
            break;
 
207
        case G_ATTRTEXT:
 
208
            w = XawTextGetSource (widget->w);
 
209
            tb.firstPos = 0, tb.ptr = NULL, tb.format = FMT8BIT, tb.length = 0;
 
210
            rtn = XawTextSourceRead (w, 0, &tb, 30000);
 
211
            if (rtn > Gbufn + 1) {
 
212
                Gbufp = Marraygrow (Gbufp, (long) (rtn + 1) * BUFSIZE);
 
213
                Gbufn = rtn + 1;
 
214
            }
 
215
            for (Gbufi = 0; Gbufi < rtn; Gbufi++)
 
216
                Gbufp[Gbufi] = tb.ptr[Gbufi];
 
217
            Gbufp[Gbufi++] = '\000';
 
218
            attrp[ai].u.t = &Gbufp[0];
 
219
            break;
 
220
        case G_ATTRAPPENDTEXT:
 
221
            Gerr (POS, G_ERRCANNOTGETATTR, "appendtext");
 
222
            return -1;
 
223
        case G_ATTRSELECTION:
 
224
            w = XawTextGetSource (widget->w);
 
225
            XawTextGetSelectionPos (widget->w, &fi, &li);
 
226
            tb.firstPos = 0, tb.ptr = NULL, tb.format = FMT8BIT, tb.length = 0;
 
227
            rtn = XawTextSourceRead (w, fi, &tb, li - fi);
 
228
            if (li - fi > Gbufn + 1) {
 
229
                Gbufp = Marraygrow (Gbufp, (long) (rtn + 1) * BUFSIZE);
 
230
                Gbufn = rtn + 1;
 
231
            }
 
232
            for (Gbufi = 0; Gbufi < li - fi; Gbufi++)
 
233
                Gbufp[Gbufi] = tb.ptr[Gbufi];
 
234
            Gbufp[Gbufi++] = '\000';
 
235
            attrp[ai].u.t = &Gbufp[0];
 
236
            break;
 
237
        case G_ATTRMODE:
 
238
            ADD2ARGS (XtNeditType, &mode);
 
239
            XtGetValues (widget->w, argp, argn);
 
240
            if (mode == XawtextAppend)
 
241
                attrp[ai].u.t = "oneline";
 
242
            else if (mode == XawtextEdit)
 
243
                attrp[ai].u.t = "input";
 
244
            else if (mode == XawtextRead)
 
245
                attrp[ai].u.t = "output";
 
246
            else {
 
247
                panic (POS, "GTgetwidgetattr", "unexpected text mode");
 
248
                return -1;
 
249
            }
 
250
            break;
 
251
        case G_ATTRWINDOWID:
 
252
            sprintf (&Gbufp[0], "0x%lx", XtWindow (widget->w));
 
253
            attrp[ai].u.t = &Gbufp[0];
 
254
            break;
 
255
        case G_ATTRNEWLINECB:
 
256
            attrp[ai].u.func = WTU->func;
 
257
            break;
 
258
        case G_ATTRUSERDATA:
 
259
            attrp[ai].u.u = widget->udata;
 
260
            break;
 
261
        default:
 
262
            Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
 
263
            return -1;
 
264
        }
 
265
    }
 
266
    return 0;
 
267
}
 
268
 
 
269
int GTdestroywidget (Gwidget_t *widget) {
 
270
    XtDestroyWidget (widget->w);
 
271
    return 0;
 
272
}
 
273
 
 
274
/* used for one line text input widgets */
 
275
void Gtweolaction (Widget w, XEvent *evp, char **app, unsigned int *anp) {
 
276
    Gwidget_t *widget;
 
277
    Widget ww;
 
278
    XawTextBlock tb;
 
279
    int ret, fi, li, n, i;
 
280
 
 
281
    widget = findwidget ((unsigned long) w, G_TEXTWIDGET);
 
282
    li = XawTextGetInsertionPoint (w) - 1;
 
283
    ww = XawTextGetSource (w);
 
284
    tb.firstPos = 0, tb.ptr = "\n", tb.format = FMT8BIT, tb.length = 1;
 
285
    fi = XawTextSourceSearch (ww, li, XawsdLeft, &tb);
 
286
    if (fi == XawTextSearchError)
 
287
        fi = 0;
 
288
    else
 
289
        fi++;
 
290
    n = li - fi;
 
291
    Gbufp[(Gbufi = 0)] = '\000';
 
292
    while (Gbufi != n) {
 
293
        ret = XawTextSourceRead (ww, fi, &tb, n - Gbufi) - fi;
 
294
        for (i = 0; i < ret; i++)
 
295
            Gbufp[Gbufi++] = tb.ptr[i];
 
296
    }
 
297
    Gbufp[Gbufi] = '\000';
 
298
    if (n >= 0 && WTU->func)
 
299
        (*WTU->func) (widget - &Gwidgets[0], Gbufp);
 
300
}