]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/xmark.c
finalize -3
[1ts-debian.git] / zephyr / zwgc / xmark.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It is one of the source files comprising zwgc, the Zephyr WindowGram
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #if (!defined(lint) && !defined(SABER))
15 static char rcsid_xmark_c[] = "$Id$";
16 #endif
17
18 #include <zephyr/mit-copyright.h>
19
20 #include <sysdep.h>
21
22 #ifndef X_DISPLAY_MISSING
23
24 #include <X11/X.h>
25 #include <X11/Xlib.h>
26 #include "X_gram.h"
27 #include "X_fonts.h"
28 #include "xmark.h"
29 #include "new_string.h"
30
31 int markblock[3] = { -1 , -1 , -1 };
32 int markchar[3] = { -1 , -1 , -1 };
33 int markpixel[3] = { -1 , -1 , -1 };
34 x_gram *markgram = NULL;
35
36 int oldblock[2] = { -1 , -1 };
37 int oldpixel[2] = { -1 , -1 };
38 x_gram *oldgram = NULL;
39
40 #define xmarkValid() \
41    ((markgram) && \
42    (STARTBLOCK != -1) && (ENDBLOCK != -1) && \
43    (STARTCHAR != -1) && (ENDCHAR != -1) && \
44    (STARTPIXEL != -1) && (ENDPIXEL != -1))
45
46 void xmarkSetBound(gram,x,y,which)
47      x_gram *gram;
48      int x,y;
49      int which;
50 {
51    int i,xofs,yofs;
52    XFontStruct *font;
53    xblock *xb;
54    unsigned char *s;
55
56 #ifdef MARK_DEBUG
57 #define RETURN \
58    if ((oldblock[which] != markblock[which]) || \
59        (oldpixel[which] != markpixel[which])) { \
60       printf("----- SetBound:\noldblock[%d]=%d,   oldpixel[%d]=%d\nmarkblock[%d]=%d, markpixel[%d]=%d\n-----", \
61              which,oldblock[which],which,oldpixel[which], \
62              which,markblock[which],which,markpixel[which]); \
63    } \
64    return
65 #else
66 #define RETURN return
67 #endif
68
69    if (markgram != gram) {
70       xmarkClear();
71       markgram = gram;
72    } else if (which < XMARK_TEMP_BOUND) {
73       oldblock[which]=markblock[which];
74       oldpixel[which]=markpixel[which];
75    }
76
77    /* Start at the top, fastforward to first span not too high. */
78    for (i=0,xb=gram->blocks ;
79         (i<gram->numblocks) && (xb->y2 < y) ;
80         i++,xb++) ;
81
82    /* the point is after the end */
83    if (i==gram->numblocks) {
84       markblock[which]=i;
85       markchar[which]=0;
86       markpixel[which]=0;
87       RETURN;
88    }
89
90    /* is the point before the beginning of the line? */
91    if (x <= xb->x1) {
92       markblock[which]=i;
93       markchar[which]=0;
94       markpixel[which]=0;
95       RETURN;
96    }
97
98    /* is the point in the nether space between this line and the last? */
99    if (y < xb->y1) {
100       markblock[which]=i;
101       markchar[which]=0;
102       markpixel[which]=0;
103       RETURN;
104    }
105
106    for (yofs=xb->y1;(i<gram->numblocks) && (xb->y1 == yofs);i++,xb++) {
107
108       if (x <= xb->x2) {
109          markblock[which]=i;
110
111          xofs=xb->x1;
112          if ((x < xofs) || (y < xb->y1)) {
113             markchar[which]=0;
114             RETURN;
115          }
116          font=get_fontst_from_fid(xb->fid);
117          for (i=0,s=(unsigned char *)((gram->text)+(xb->strindex));
118               xofs<x && i<xb->strlen;
119               i++,s++) {
120              /* if font->per_char is NULL, then we should use min_bounds */
121              short usewidth = font->per_char ? font->per_char[*s - font->min_char_or_byte2].width : font->min_bounds.width;
122            if (x <= (xofs+=usewidth)) {
123               markchar[which]=i;
124               markpixel[which]=xofs - xb->x1 - usewidth;
125               RETURN;
126            }
127          }
128       }
129    }
130
131    /* The endpoint is after the end of the block if the loop ends */
132    markblock[which]=i;
133    markchar[which]=0;
134    markpixel[which]=0;
135    RETURN;
136 }
137
138 /* needs both bounds to be valid (!= -1) */
139 static int xmarkNearest(x,y)
140      int x,y;
141 {
142    int middle;
143
144    xmarkSetBound(markgram,x,y,XMARK_TEMP_BOUND);
145    middle=(ENDBLOCK+STARTBLOCK)/2;
146
147    if (markblock[XMARK_TEMP_BOUND] < middle)
148      return(XMARK_START_BOUND);
149    else if (markblock[XMARK_TEMP_BOUND] > middle)
150      return(XMARK_END_BOUND);
151    else {
152       middle=(ENDCHAR+STARTCHAR)/2;
153       if (markchar[XMARK_TEMP_BOUND] < middle)
154         return(XMARK_START_BOUND);
155       else
156         return(XMARK_END_BOUND);
157    }
158 }
159
160 void xmarkExpose(dpy,w,gram,b1,p1,b2,p2)
161      Display *dpy;
162      Window w;
163      x_gram *gram;
164      unsigned int b1,p1,b2,p2;
165 {
166 #define swap(x,y) temp=(x); (x)=(y); (y)=temp
167    int i,temp;
168    XEvent event;
169 #define expose (event.xexpose)
170
171    if ((b1==-1) || (p1==-1) || (b2==-1) || (p2==-1)) return;
172
173    if ((b1 > b2) || ((b1 == b2) && (p1 > p2))) {
174       swap(b1,b2);
175       swap(p1,p2);
176    }
177
178 #if defined(_IBMR2) && !defined(__GNUC__) && defined(RS6000_OPT_BUG)
179    /* A version of the AIX 3.1 RS/6000 C compiler needs this to prevent
180       a core dump in the loop below. */
181    &b1;
182 #endif
183
184    expose.type=Expose;
185    expose.display=dpy;
186    expose.window=w;
187
188    for (i=b1;i<=b2;i++) {
189       if (b1==b2) {
190          expose.x=gram->blocks[i].x1+p1;
191          expose.y=gram->blocks[i].y1;
192          expose.width=p2-p1;
193          expose.height=gram->blocks[i].y2-gram->blocks[i].y1;
194          expose.count=0;
195       } else if (i==b1) {
196          expose.x=gram->blocks[i].x1+p1;
197          expose.y=gram->blocks[i].y1;
198          expose.width=gram->blocks[i].x2-p1;
199          expose.height=gram->blocks[i].y2-gram->blocks[i].y1;
200          expose.count=b2-i;
201       } else if (i==b2) {
202          expose.x=gram->blocks[i].x1;
203          expose.y=gram->blocks[i].y1;
204          expose.width=p2;
205          expose.height=gram->blocks[i].y2-gram->blocks[i].y1;
206          expose.count=b2-i;
207       } else {
208          expose.x=gram->blocks[i].x1;
209          expose.y=gram->blocks[i].y1;
210          expose.width=gram->blocks[i].x2-gram->blocks[i].x1;
211          expose.height=gram->blocks[i].y2-gram->blocks[i].y1;
212          expose.count=b2-i;
213       }
214
215 #ifdef MARK_DEBUG
216    if (expose.width && expose.height) {
217       printf("---- markExpose:\nb1=%d p1=%d b2=%d p2=%d\n",b1,p1,b2,p2);
218       printf("x=%d y=%d w=%d h=%d\n-----",
219              expose.x,expose.y,expose.width,expose.height);
220    }
221 #endif
222       if ((expose.width && expose.height) || (expose.count == 0))
223         XSendEvent(dpy,w,True,ExposureMask,&event);
224    }
225 }
226
227 /* Public functions: */
228
229 void xmarkRedraw(dpy,w,gram,range)
230      Display *dpy;
231      Window w;
232      x_gram *gram;
233      int range;
234 {
235 #define ob1 ((unsigned int) oldblock[XMARK_START_BOUND])
236 #define ob2 ((unsigned int) oldblock[XMARK_END_BOUND])
237 #define nb1 ((unsigned int) markblock[XMARK_START_BOUND])
238 #define nb2 ((unsigned int) markblock[XMARK_END_BOUND])
239 #define op1 ((unsigned int) oldpixel[XMARK_START_BOUND])
240 #define op2 ((unsigned int) oldpixel[XMARK_END_BOUND])
241 #define np1 ((unsigned int) markpixel[XMARK_START_BOUND])
242 #define np2 ((unsigned int) markpixel[XMARK_END_BOUND])
243
244    if (range==XMARK_REDRAW_CURRENT) {
245       if (!markgram) return;
246       xmarkExpose(dpy,w,gram,nb1,np1,nb2,np2);
247    } else if (range==XMARK_REDRAW_OLD) {
248       if (!oldgram) return;
249       xmarkExpose(dpy,w,gram,ob1,op1,ob2,op2);
250    } else if (range==XMARK_REDRAW_START) {
251       if (!markgram) return;
252       xmarkExpose(dpy,w,gram,ob1,op1,nb1,np1);
253    } else if (range==XMARK_REDRAW_END) {
254       if (!markgram) return;
255       xmarkExpose(dpy,w,gram,ob2,op2,nb2,np2);
256    }
257 #ifdef DEBUG
258      else {
259         printf("xmarkRedraw:  This shouldn't happen!\n");
260      }
261 #endif
262 }
263
264 /* needs both bounds to be valid (!= -1) */
265 int xmarkSecond()
266 {
267    if (STARTBLOCK > ENDBLOCK)
268      return(XMARK_START_BOUND);
269    else if (STARTBLOCK < ENDBLOCK)
270      return(XMARK_END_BOUND);
271    else {
272       if (STARTCHAR > ENDCHAR)
273         return(XMARK_START_BOUND);
274       else if (STARTCHAR < ENDCHAR)
275         return(XMARK_END_BOUND);
276       else
277         return(XMARK_END_BOUND);
278    }
279 }
280
281 void xmarkClear()
282 {
283    oldblock[0]=markblock[0];
284    oldblock[1]=markblock[1];
285    oldpixel[0]=markpixel[0];
286    oldpixel[1]=markpixel[1];
287    oldgram=markgram;
288
289    markblock[0] = -1;
290    markblock[1] = -1;
291    markchar[0] = -1;
292    markchar[1] = -1;
293    markpixel[0] = -1;
294    markpixel[1] = -1;
295    markgram=NULL;
296 }
297
298 int xmarkExtendFromFirst(gram,x,y)
299      x_gram *gram;
300      int x,y;
301 {
302    if (markgram != gram) {
303       xmarkClear();
304       markgram = gram;
305    }
306
307    if (STARTBLOCK == -1) {
308       xmarkStart(gram,x,y);
309       xmarkEnd(gram,x,y);
310       return(XMARK_REDRAW_CURRENT);
311    } else if (ENDBLOCK == -1) {
312       xmarkEnd(gram,x,y);
313       return(XMARK_REDRAW_CURRENT);
314    } else {
315       xmarkSetBound(gram,x,y,XMARK_END_BOUND);
316       return(XMARK_REDRAW_END);
317    }
318 }
319
320 int xmarkExtendFromNearest(gram,x,y)
321      x_gram *gram;
322      int x,y;
323 {
324    int bound;
325
326    if (markgram != gram) {
327       xmarkClear();
328       markgram = gram;
329    }
330
331    if (STARTBLOCK == -1) {
332       xmarkStart(gram,x,y);
333       xmarkEnd(gram,x,y);
334       return(XMARK_REDRAW_CURRENT);
335    } else if (ENDBLOCK == -1) {
336       xmarkEnd(gram,x,y);
337       return(XMARK_REDRAW_CURRENT);
338    } else {
339       xmarkSetBound(gram,x,y,bound=xmarkNearest(x,y));
340       return(bound==XMARK_START_BOUND?XMARK_REDRAW_START:XMARK_REDRAW_END);
341    }
342 }
343
344 char *xmarkGetText()
345 {
346     int i, index, len;
347     int last_y = -1;
348     string temp;
349     string text_so_far = string_Copy("");
350     char *text = markgram->text;
351     int startblock,endblock,startchar,endchar;
352
353     if (xmarkValid()) {
354        if (xmarkSecond() == XMARK_END_BOUND) {
355           startblock=STARTBLOCK;
356           endblock=ENDBLOCK;
357           startchar=STARTCHAR;
358           endchar=ENDCHAR;
359        } else {
360           startblock=ENDBLOCK;
361           endblock=STARTBLOCK;
362           startchar=ENDCHAR;
363           endchar=STARTCHAR;
364        }
365
366        for (i=startblock; i<=endblock; i++) {
367           if (last_y != -1 && last_y != markgram->blocks[i].y)
368             text_so_far = string_Concat2(text_so_far, "\n");
369           index = markgram->blocks[i].strindex;
370           len = markgram->blocks[i].strlen;
371           if (startblock == endblock)
372             temp = string_CreateFromData(text+index+startchar,
373                                          endchar-startchar);
374           else if (i==startblock)
375             temp = string_CreateFromData(text+index+startchar,len-startchar);
376           else if (i==endblock)
377             temp = string_CreateFromData(text+index,endchar);
378           else
379             temp = string_CreateFromData(text+index,len);
380           text_so_far = string_Concat2(text_so_far, temp);
381           free(temp);
382           last_y = markgram->blocks[i].y;
383        }
384     }
385
386     return(text_so_far);
387 }
388
389 #endif /* X_DISPLAY_MISSING */
390