File: fontOp.c

package info (click to toggle)
xpaint 2.5.1-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,488 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (290 lines) | stat: -rw-r--r-- 7,396 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* +-------------------------------------------------------------------+ */
/* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
/* |                                                                   | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.  There is no           | */
/* | representations about the suitability of this software for        | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.                                              | */
/* |                                                                   | */
/* +-------------------------------------------------------------------+ */

/* $Id: fontOp.c,v 1.3 1996/04/19 08:56:26 torsten Exp $ */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include "xpaint.h"
#include "Paint.h"
#include "graphic.h"
#include "misc.h"
#include "ops.h"

static Atom targetAtom, selectionAtom;

#define	DELAY	500

typedef struct {
    XtIntervalId id;
    Boolean typing;
    Boolean state;
    int startX, startY, curX, curY, height;
    XFontStruct *fi;
    int insertX, insertY, insertSX, insertSY, insertH;
    int zoom;
    Widget w;
    char *sptr;
    int maxStrLen;
    char minPos;
    char *str;
    Drawable drawable;
    GC gc, gcx;
} LocalInfo;

static void addString(Widget, LocalInfo *, int, char *);

static void 
cursor(LocalInfo * l, Boolean flag)
{
    if (l->w == None)
	return;
    if (flag) {
	XDrawLine(XtDisplay(l->w), XtWindow(l->w), l->gcx,
	    l->insertX, l->insertY, l->insertX, l->insertY + l->insertH);
	l->state = !l->state;
    } else if (l->state) {
	XDrawLine(XtDisplay(l->w), XtWindow(l->w), l->gcx,
	    l->insertX, l->insertY, l->insertX, l->insertY + l->insertH);
	l->state = False;
    }
}

static void 
flash(LocalInfo * l)
{
    cursor(l, True);
    l->id = XtAppAddTimeOut(XtWidgetToApplicationContext(l->w),
		      DELAY, (XtTimerCallbackProc) flash, (XtPointer) l);
}

static void 
gotSelection(Widget w, LocalInfo * l, Atom * selection, Atom * type,
	     XtPointer value, unsigned long *len, int *format)
{
    if (!l->typing)
	return;

    if (len == 0 || value == NULL)
	return;

    switch (*type) {
    case XA_STRING:
	addString(w, l, *len, (char *) value);
	break;
    }
    XtFree((XtPointer) value);
}

static void 
press(Widget w, LocalInfo * l, XButtonEvent * event, OpInfo * info)
{
    if (event->button == Button1) {
	cursor(l, False);

	XtVaGetValues(w, XtNfont, &l->fi, NULL);
	if (l->fi == NULL)
	    return;

	l->w = w;
	l->height = l->fi->ascent + l->fi->descent;
	l->startX = l->curX = info->x;
	l->startY = l->curY = info->y;
	l->curY = l->curY - l->height / 2;
	l->typing = True;

	if (info->surface == opWindow) {
	    l->insertH = l->height * info->zoom;
	    l->insertY = event->y - l->insertH / 2;
	    l->insertX = event->x;
	    l->insertSX = event->x;
	}
	l->sptr = l->str;

	if (l->id == (XtIntervalId) NULL)
	    flash(l);

	l->zoom = info->zoom;
	l->gc = info->first_gc;
	l->minPos = 0;

	UndoStartPoint(w, info, l->curX, l->curY);
	if (info->surface == opPixmap)
	    l->drawable = info->drawable;
    } else if (event->button == Button2) {
	XtGetSelectionValue(w, selectionAtom, targetAtom,
			    (XtSelectionCallbackProc) gotSelection, (XtPointer) l, event->time);
    }
}

static void 
key(Widget w, LocalInfo * l, XKeyEvent * event, OpInfo * info)
{
    char buf[21];
    KeySym keysym;
    int len;

    if (l->w == None)
	return;

    if ((len = XLookupString(event, buf, sizeof(buf) - 1, &keysym, NULL)) == 0)
	return;

    l->zoom = info->zoom;
    l->gc = info->first_gc;
    l->drawable = info->drawable;
    addString(w, l, len, buf);
}

static void 
addString(Widget w, LocalInfo * l, int len, char *buf)
{
    int i, width;
    XRectangle rect;

    if (len != 0)
	cursor(l, False);
    for (i = 0; i < len; i++) {
	if (l->sptr == l->str + l->maxStrLen - 5) {
	    int delta = l->sptr - l->str;
	    l->maxStrLen += 128;
	    l->str = (char *) XtRealloc((XtPointer) l->str, l->maxStrLen);
	    l->sptr = l->str + delta;
	}
	if (buf[i] == '\n' || buf[i] == '\r') {
	    l->curX = l->startX;
	    l->curY += l->height + 2;
	    l->insertX = l->insertSX;
	    l->insertY += l->insertH + 2 * l->zoom;

	    *l->sptr++ = '\n';
	} else if (buf[i] == 0x08 || buf[i] == 0x7f) {
	    if (l->sptr - l->str > l->minPos) {
		l->sptr--;
		if (*l->sptr == '\n') {
		    char *cp;
		    *l->sptr = '\0';
		    if ((cp = strrchr(l->str, '\n')) == NULL)
			cp = l->str;
		    else
			cp++;
		    width = XTextWidth(l->fi, cp, strlen(cp));
		    l->curX = l->startX + width;
		    l->curY -= l->height + 2;
		    l->insertX = (l->startX + width) * l->zoom;
		    l->insertY -= l->insertH + 2 * l->zoom;
		} else {
		    width = XTextWidth(l->fi, l->sptr, 1);
		    l->curX -= width;
		    l->insertX -= width * l->zoom;

		    /*
		    **  Now undraw the character.
		     */
		    XYtoRECT(l->curX, l->curY, l->curX + width, l->curY + l->height, &rect);
		    PwUpdateFromLast(w, &rect);
		}
	    }
	} else {
	    if (l->zoom == 1)
		XDrawString(XtDisplay(w), XtWindow(w), l->gc,
			    l->curX, l->curY + l->fi->ascent, &buf[i], 1);
	    XDrawString(XtDisplay(w), l->drawable, l->gc,
			l->curX, l->curY + l->fi->ascent, &buf[i], 1);
	    width = XTextWidth(l->fi, &buf[i], 1);

	    XYtoRECT(l->curX, l->curY, l->curX + width, l->curY + l->height, &rect);
	    PwUpdate(w, &rect, False);

	    l->curX += width;
	    l->insertX += width * l->zoom;

	    UndoGrow(w, l->curX, l->curY + l->height);
	    UndoGrow(w, l->curX, l->curY + l->height);

	    *l->sptr++ = buf[i];
	}
    }
}

/*
**  Those public functions
 */
void *
FontAdd(Widget w)
{
    static int inited = False;
    LocalInfo *l = (LocalInfo *) XtMalloc(sizeof(LocalInfo));

    if (!inited) {
	selectionAtom = XA_PRIMARY;
	targetAtom = XA_STRING;
    }
    l->id = (XtIntervalId) NULL;
    l->w = None;
    l->typing = False;
    l->state = False;

    l->maxStrLen = 128;
    l->str = (char *) XtMalloc(l->maxStrLen);
    l->gcx = GetGCX(w);

    OpAddEventHandler(w, opPixmap | opWindow, ButtonPressMask, FALSE,
		      (OpEventProc) press, l);
    OpAddEventHandler(w, opPixmap, KeyPressMask, FALSE, (OpEventProc) key, l);

    SetIBeamCursor(w);

    return l;
}
void 
FontRemove(Widget w, void *p)
{
    LocalInfo *l = (LocalInfo *) p;

    OpRemoveEventHandler(w, opPixmap | opWindow, ButtonPressMask, FALSE,
			 (OpEventProc) press, p);
    OpRemoveEventHandler(w, opPixmap, KeyPressMask, FALSE, (OpEventProc) key, p);

    if (l->id != (XtIntervalId) NULL)
	XtRemoveTimeOut(l->id);

    cursor(l, False);

    XtFree((XtPointer) l->str);
    XtFree((XtPointer) l);
}
void 
FontChanged(Widget w)
{
    LocalInfo *l;

    if (CurrentOp->add != FontAdd)
	return;

    l = (LocalInfo *) GraphicGetData(w);

    XtVaGetValues(w, XtNfont, &l->fi, NULL);
    if (l->fi == NULL)
	return;

    cursor(l, False);
    l->height = l->fi->ascent + l->fi->descent;

    l->minPos = l->sptr - l->str;
    l->insertH = l->height * l->zoom;
}