Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wtext.c
1
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
3
4 #include "WINGsP.h"
5 #include <ctype.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
8
9 #define DO_BLINK 0
10
11 /* TODO:
12 * - verify what happens with XK_return in insertTextInt...
13 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
22 */
23
24 /* a Section is a section of a TextBlock that describes what parts
25 of a TextBlock has been laid out on which "line"...
26 o this greatly aids redraw, scroll and selection.
27 o this is created during layoutLine, but may be later modified.
28 o there may be many Sections per TextBlock, hence the array */
29 typedef struct {
30 unsigned int x, y; /* where to draw it from */
31 unsigned short w, h; /* its width and height */
32 unsigned short begin; /* where the layout begins */
33 unsigned short end; /* where it ends */
34 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
35 unsigned short last:1; /* is it the last section on a "line"? */
36 unsigned int _y:31; /* the "line" it and other textblocks are on */
37 } Section;
38
39 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
40 o text for the block, color and font
41 o or a pointer to the pixmap
42 o OR a pointer to the widget and the (text) description for its graphic
43 */
44
45 typedef struct _TextBlock {
46 struct _TextBlock *next; /* next text block in linked list */
47 struct _TextBlock *prior; /* prior text block in linked list */
48
49 char *text; /* pointer to text (could be kanji) */
50 /* or to the object's description */
51 union {
52 WMFont *font; /* the font */
53 WMWidget *widget; /* the embedded widget */
54 WMPixmap *pixmap; /* the pixmap */
55 } d; /* description */
56
57 unsigned short used; /* number of chars in this block */
58 unsigned short allocated; /* size of allocation (in chars) */
59 WMColor *color; /* the color */
60
61 Section *sections; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
63
64 unsigned short s_begin; /* where the selection begins */
65 unsigned short s_end; /* where it ends */
66
67 unsigned int first:1; /* first TextBlock in paragraph */
68 unsigned int blank:1; /* ie. blank paragraph */
69 unsigned int kanji:1; /* is of 16-bit characters or not */
70 unsigned int graphic:1; /* graphic or text: text=0 */
71 unsigned int object:1; /* embedded object or pixmap */
72 unsigned int underlined:1; /* underlined or not */
73 unsigned int selected:1; /* selected or not */
74 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
75 int script:8; /* script in points: negative for subscript */
76 unsigned int marginN:8; /* which of the margins in the tPtr to use */
77 unsigned int nClicks:2; /* single, double, triple clicks */
78 unsigned int RESERVED:7;
79 } TextBlock;
80
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
82 typedef struct {
83 int y, x, h, w;
84 } myRect;
85
86 typedef struct W_Text {
87 W_Class widgetClass; /* the class number of this widget */
88 W_View *view; /* the view referring to this instance */
89
90 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
91
92 WMScroller *vS; /* the vertical scroller */
93 unsigned int vpos; /* the current vertical position */
94 unsigned int prevVpos; /* the previous vertical position */
95
96 WMScroller *hS; /* the horizontal scroller */
97 unsigned int hpos; /* the current horizontal position */
98 unsigned int prevHpos; /* the previous horizontal position */
99
100 WMFont *dFont; /* the default font */
101 WMColor *dColor; /* the default color */
102 WMPixmap *dBulletPix; /* the default pixmap for bullets */
103
104 WMColor *fgColor; /* The current foreground color */
105 WMColor *bgColor; /* The background color */
106
107 GC stippledGC; /* the GC to overlay selected graphics with */
108 Pixmap db; /* the buffer on which to draw */
109 WMPixmap *bgPixmap; /* the background pixmap */
110
111 myRect visible; /* the actual rectangle that can be drawn into */
112 myRect cursor; /* the position and (height) of cursor */
113 myRect sel; /* the selection rectangle */
114
115 WMPoint clicked; /* where in the _document_ was clicked */
116
117 unsigned short tpos; /* the position in the currentTextBlock */
118 unsigned short docWidth; /* the width of the entire document */
119 unsigned int docHeight; /* the height of the entire document */
120
121 TextBlock *firstTextBlock;
122 TextBlock *lastTextBlock;
123 TextBlock *currentTextBlock;
124
125 WMArray *gfxItems; /* a nice array of graphic items */
126
127 #if DO_BLINK
128 WMHandlerID timerID; /* for nice twinky-winky */
129 #endif
130
131 WMAction *parser;
132 WMAction *writer;
133 WMTextDelegate *delegate;
134 Time lastClickTime;
135
136 WMRulerMargins *margins; /* an array of margins */
137
138 unsigned int nMargins:7; /* the total number of margins in use */
139 struct {
140 unsigned int monoFont:1; /* whether to ignore formats and graphic */
141 unsigned int focused:1; /* whether this instance has input focus */
142 unsigned int editable:1; /* "silly user, you can't edit me" */
143 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
144 unsigned int pointerGrabbed:1; /* "heh, gib me pointer" */
145 unsigned int extendSelection:1; /* shift-drag to select more regions */
146
147 unsigned int rulerShown:1; /* whether the ruler is shown or not */
148 unsigned int frozen:1; /* whether screen updates are to be made */
149 unsigned int cursorShown:1; /* whether to show the cursor */
150 unsigned int acceptsGraphic:1; /* accept graphic when dropped */
151 unsigned int horizOnDemand:1; /* if a large image should appear */
152 unsigned int needsLayOut:1; /* in case of Append/Deletes */
153 unsigned int ignoreNewLine:1; /* turn it into a ' ' in streams > 1 */
154 unsigned int indentNewLine:1; /* add " " for a newline typed */
155 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
156 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
157 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
158 WMAlignment alignment:2; /* the alignment for text */
159 WMReliefType relief:3; /* the relief to display with */
160 unsigned int isOverGraphic:2; /* the mouse is over a graphic */
161 unsigned int first:1; /* for plain text parsing, newline? */
162 /* unsigned int RESERVED:1; */
163 } flags;
164
165 WMArray *xdndSourceTypes;
166 WMArray *xdndDestinationTypes;
167 } Text;
168
169 #define NOTIFY(T,C,N,A) {\
170 WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
175
176 #define TYPETEXT 0
177
178 #if 0
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr, int len)
181 {
182 char *s;
183
184 s = wmalloc(len + 1);
185 memcpy(s, ptr, len);
186 s[len] = 0;
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
188 printf("[%s]\n", s);
189 wfree(s);
190 }
191 #endif
192
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
197
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
202 };
203
204 static char *default_bullet[] = {
205 "6 6 4 1",
206 " c None s None",
207 ". c black",
208 "X c white",
209 "o c #808080",
210 " ... ",
211 ".XX.. ",
212 ".XX..o",
213 ".....o",
214 " ...oo",
215 " ooo "
216 };
217
218 static void handleEvents(XEvent * event, void *data);
219 static void layOutDocument(Text * tPtr);
220 static void updateScrollers(Text * tPtr);
221
222 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
223 {
224 unsigned int i = 0;
225
226 for (i = 0; i < tPtr->nMargins; i++) {
227
228 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
229 return i;
230 }
231
232 return -1;
233 }
234
235 static int newMargin(Text * tPtr, WMRulerMargins * margins)
236 {
237 int n;
238
239 if (!margins) {
240 tPtr->margins[0].retainCount++;
241 return 0;
242 }
243
244 n = getMarginNumber(tPtr, margins);
245
246 if (n == -1) {
247
248 if (tPtr->nMargins >= 127) {
249 n = tPtr->nMargins - 1;
250 return n;
251 }
252
253 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
254
255 n = tPtr->nMargins - 1;
256 tPtr->margins[n].left = margins->left;
257 tPtr->margins[n].first = margins->first;
258 tPtr->margins[n].body = margins->body;
259 tPtr->margins[n].right = margins->right;
260 /* for each tab... */
261 tPtr->margins[n].retainCount = 1;
262 } else {
263 tPtr->margins[n].retainCount++;
264 }
265
266 return n;
267 }
268
269 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
270 {
271 unsigned short i, w, lw, selected = False, extend = False;
272 myRect sel;
273
274 /* if selection rectangle completely encloses the section */
275 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
276 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
277 sel.x = 0;
278 sel.w = tPtr->visible.w;
279 selected = extend = True;
280
281 /* or if it starts on a line and then goes further down */
282 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
283 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
284 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y)) {
285 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
286 sel.w = tPtr->visible.w;
287 selected = extend = True;
288
289 /* or if it begins before a line, but ends on it */
290 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
291 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
292 && (tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
293
294 if (1 || tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
295 sel.w = tPtr->sel.x + tPtr->sel.w;
296 else
297 sel.w = tPtr->sel.x;
298
299 sel.x = 0;
300 selected = True;
301
302 /* or if the selection rectangle lies entirely within a line */
303 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
304 && (tPtr->sel.w >= 2)
305 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
306 sel.x = tPtr->sel.x;
307 sel.w = tPtr->sel.w;
308 selected = True;
309 }
310
311 if (selected) {
312 selected = False;
313
314 /* if not within (modified) selection rectangle */
315 if (tb->sections[s].x > sel.x + sel.w || tb->sections[s].x + tb->sections[s].w < sel.x)
316 return False;
317
318 if (tb->graphic) {
319 if (tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w && tb->sections[s].x >= sel.x) {
320 rect->width = tb->sections[s].w;
321 rect->x = tb->sections[s].x;
322 selected = True;
323 }
324 } else {
325
326 i = tb->sections[s].begin;
327 lw = 0;
328
329 if (0 && tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
332 }
333
334 while (++i <= tb->sections[s].end) {
335
336 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
337 lw += w;
338
339 if (lw + tb->sections[s].x >= sel.x || i == tb->sections[s].end) {
340 lw -= w;
341 i--;
342 tb->s_begin = (tb->selected ? WMIN(tb->s_begin, i) : i);
343 break;
344 }
345 }
346
347 if (i > tb->sections[s].end) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
349 return False;
350 }
351
352 _selEnd: rect->x = tb->sections[s].x + lw;
353 lw = 0;
354 while (++i <= tb->sections[s].end) {
355
356 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
357 lw += w;
358
359 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
360
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
364 }
365
366 rect->width = lw;
367 if (tb->sections[s].last && sel.x + sel.w
368 >= tb->sections[s].x + tb->sections[s].w && extend) {
369 rect->width += (tPtr->visible.w - rect->x - lw);
370 }
371
372 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
373 selected = True;
374 break;
375 }
376 }
377 }
378 }
379
380 if (selected) {
381 rect->y = tb->sections[s]._y - tPtr->vpos;
382 rect->height = tb->sections[s].h;
383 if (tb->graphic) {
384 printf("DEBUG: graphic s%d h%d\n", s, tb->sections[s].h);
385 }
386 }
387 return selected;
388
389 }
390
391 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
392 {
393 TextBlock *tb;
394 int isFont = False;
395
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
399
400 if (font && (!color || underlined == -1))
401 isFont = True;
402
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
405
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
408
409 if (isFont) {
410 if (!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
413 }
414 } else if (underlined != -1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
419 }
420
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
422
423 TextBlock *midtb, *otb = tb;
424
425 if (underlined != -1) {
426 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
427 &(tb->text[tb->s_begin]),
428 tb->d.font, tb->color,
429 False,
430 (tb->s_end - tb->s_begin));
431 } else {
432 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
433 &(tb->text[tb->s_begin]),
434 (isFont ? font : tb->d.
435 font),
436 (isFont ? tb->
437 color : color), False,
438 (tb->s_end - tb->s_begin));
439 }
440
441 if (midtb) {
442 if (underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
446 }
447
448 midtb->selected = !True;
449 midtb->s_begin = 0;
450 midtb->s_end = midtb->used;
451 tPtr->currentTextBlock = tb;
452 WMAppendTextBlock(tPtr, midtb);
453 tb = tPtr->currentTextBlock;
454 }
455
456 if (otb->used - otb->s_end > 0) {
457 TextBlock *ntb;
458 ntb = (TextBlock *)
459 WMCreateTextBlockWithText(tPtr,
460 &(otb->text[otb->s_end]), otb->d.font,
461 otb->color, False, otb->used - otb->s_end);
462
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
468 }
469 }
470
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
473 }
474
475 otb->selected = False;
476 otb->used = otb->s_begin;
477 }
478 }
479
480 tb = tb->next;
481 }
482
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
485
486 /* in case the size changed... */
487 if (isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
489
490 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
491 tPtr->sel.y = 3 + tb->sections[0]._y;
492 tPtr->sel.h = tb->sections[tb->nsections - 1]._y - tb->sections[0]._y;
493 tPtr->sel.w = tb->sections[tb->nsections - 1].w;
494 if (tb->sections[tb->nsections - 1]._y != tb->sections[0]._y) {
495 tPtr->sel.x = 0;
496 }
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
498 }
499
500 }
501
502 static Bool removeSelection(Text * tPtr)
503 {
504 TextBlock *tb = NULL;
505 Bool first = False;
506
507 if (!(tb = tPtr->firstTextBlock))
508 return False;
509
510 while (tb) {
511 if (tb->selected) {
512 if (!first && !tb->graphic) {
513 WMReleaseFont(tPtr->dFont);
514 tPtr->dFont = WMRetainFont(tb->d.font);
515 first = True;
516 }
517
518 if ((tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
519 tPtr->currentTextBlock = tb;
520 if (tb->next) {
521 tPtr->tpos = 0;
522 } else if (tb->prior) {
523 if (tb->prior->graphic)
524 tPtr->tpos = 1;
525 else
526 tPtr->tpos = tb->prior->used;
527 } else
528 tPtr->tpos = 0;
529
530 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
531 tb = tPtr->currentTextBlock;
532 continue;
533
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]), &(tb->text[tb->s_end]), tb->used - tb->s_end);
536 tb->used -= (tb->s_end - tb->s_begin);
537 tb->selected = False;
538 tPtr->tpos = tb->s_begin;
539 }
540
541 }
542
543 tb = tb->next;
544 }
545 return True;
546 }
547
548 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
549 {
550 TextBlock *hold = tb;
551
552 if (!tb)
553 return NULL;
554
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir ? tb->next : tb->prior);
559 }
560
561 if (!tb) {
562 tb = hold;
563 while (tb) {
564 if (!tb->graphic)
565 break;
566 tb = (dir ? tb->prior : tb->next);
567 }
568 }
569
570 if (!tb)
571 return NULL;
572
573 return tb;
574 }
575
576 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
577 {
578 if (tPtr->flags.monoFont && tb->graphic) {
579 tb = getFirstNonGraphicBlockFor(tb, *dir);
580 if (!tb)
581 return 0;
582
583 if (tb->graphic) {
584 tPtr->currentTextBlock = (dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
587 }
588 }
589
590 if (!tb->sections) {
591 layOutDocument(tPtr);
592 return 0;
593 }
594
595 *dir = !(y <= tb->sections[0].y);
596 if (*dir) {
597 if ((y <= tb->sections[0]._y + tb->sections[0].h)
598 && (y >= tb->sections[0]._y)) {
599 /* if it's on the same line */
600 if (x < tb->sections[0].x)
601 *dir = 0;
602 }
603 } else {
604 if ((y <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
605 && (y >= tb->sections[tb->nsections - 1]._y)) {
606 /* if it's on the same line */
607 if (x > tb->sections[tb->nsections - 1].x)
608 *dir = 1;
609 }
610 }
611
612 return 1;
613 }
614
615 static void paintText(Text * tPtr)
616 {
617 TextBlock *tb;
618 WMFont *font;
619 char *text;
620 int len, y, c, s, done = False, prev_y = -23, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
625
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
628
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
630
631 if (tPtr->bgPixmap) {
632 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
633 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
634 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
635 }
636
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
640 }
641 }
642
643 done = False;
644
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
649 goto _copy_area;
650
651 while (tb) {
652
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
655
656 if (dir) {
657 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
658 break;
659 } else {
660 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
661 break;
662 }
663
664 _getSibling:
665 if (dir) {
666 if (tb->next)
667 tb = tb->next;
668 else
669 break;
670 } else {
671 if (tb->prior)
672 tb = tb->prior;
673 else
674 break;
675 }
676 }
677
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
683 }
684
685 tb->selected = False;
686
687 for (s = 0; s < tb->nsections && !done; s++) {
688
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
692 }
693
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
696
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
703 }
704
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
707
708 if (sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
711 rect.x, rect.y, rect.width, rect.height);
712 }
713 }
714
715 prev_y = tb->sections[s]._y;
716
717 len = tb->sections[s].end - tb->sections[s].begin;
718 text = &(tb->text[tb->sections[s].begin]);
719 y = tb->sections[s].y - tPtr->vpos;
720 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
721
722 if (!tPtr->flags.monoFont && tb->underlined) {
723 XDrawLine(dpy, tPtr->db, WMColorGC(color),
724 tb->sections[s].x - tPtr->hpos,
725 y + font->y + 1,
726 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
727 }
728 }
729 tb = (!done ? tb->next : NULL);
730 }
731
732 /* now , show all graphic items that can be viewed */
733 c = WMGetArrayItemCount(tPtr->gfxItems);
734 if (c > 0 && !tPtr->flags.monoFont) {
735 int j, h;
736
737 for (j = 0; j < c; j++) {
738 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
739
740 /* if it's not viewable, and mapped, unmap it */
741 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
742 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
743
744 if (tb->object) {
745 if ((W_VIEW(tb->d.widget))->flags.mapped) {
746 WMUnmapWidget(tb->d.widget);
747 }
748 }
749 } else {
750 /* if it's viewable, and not mapped, map it */
751 if (tb->object) {
752 W_View *view = W_VIEW(tb->d.widget);
753
754 if (!view->flags.realized)
755 WMRealizeWidget(tb->d.widget);
756 if (!view->flags.mapped) {
757 XMapWindow(view->screen->display, view->window);
758 XFlush(view->screen->display);
759 view->flags.mapped = 1;
760 }
761 }
762
763 if (tb->object) {
764 WMMoveWidget(tb->d.widget,
765 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
766 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
767 h = WMWidgetHeight(tb->d.widget) + 1;
768
769 } else {
770 WMDrawPixmap(tb->d.pixmap, tPtr->db,
771 tb->sections[0].x - tPtr->hpos,
772 tb->sections[0].y - tPtr->vpos);
773 h = tb->d.pixmap->height + 1;
774
775 }
776
777 if (tPtr->flags.ownsSelection) {
778 XRectangle rect;
779
780 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
781 Drawable d = (0 && tb->object ?
782 (WMWidgetView(tb->d.widget))->window : tPtr->db);
783
784 tb->selected = True;
785 XFillRectangle(dpy, d, tPtr->stippledGC,
786 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
787 rect.x, rect.y, rect.width, rect.height);
788 }
789 }
790
791 if (!tPtr->flags.monoFont && tb->underlined) {
792 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
793 tb->sections[0].x - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos,
795 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
796 tb->sections[0].y + h - tPtr->vpos);
797 }
798 }
799 }
800 }
801
802 _copy_area:
803 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
804 int y = tPtr->cursor.y - tPtr->vpos;
805 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
806 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
807 }
808
809 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
810 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
811
812 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
813
814 if (tPtr->ruler && tPtr->flags.rulerShown)
815 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
816
817 }
818
819 static void mouseOverObject(Text * tPtr, int x, int y)
820 {
821 TextBlock *tb;
822 Bool result = False;
823
824 x -= tPtr->visible.x;
825 x += tPtr->hpos;
826 y -= tPtr->visible.y;
827 y += tPtr->vpos;
828
829 if (tPtr->flags.ownsSelection) {
830 if (tPtr->sel.x <= x
831 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
832 tPtr->flags.isOverGraphic = 1;
833 result = True;
834 }
835 }
836
837 if (!result) {
838 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
839
840 if (c < 1)
841 tPtr->flags.isOverGraphic = 0;
842
843 for (j = 0; j < c; j++) {
844 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
845
846 if (!tb || !tb->sections) {
847 tPtr->flags.isOverGraphic = 0;
848 return;
849 }
850
851 if (!tb->object) {
852 if (tb->sections[0].x <= x
853 && tb->sections[0].y <= y
854 && tb->sections[0].x + tb->sections[0].w >= x
855 && tb->sections[0].y + tb->d.pixmap->height >= y) {
856 tPtr->flags.isOverGraphic = 3;
857 result = True;
858 break;
859 }
860 }
861 }
862
863 }
864
865 if (!result)
866 tPtr->flags.isOverGraphic = 0;
867
868 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
869 {
870 XSetWindowAttributes attribs;
871 attribs.cursor = tPtr->view->attribs.cursor;
872 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
873 }
874 }
875
876 #if DO_BLINK
877
878 static void blinkCursor(void *data)
879 {
880 Text *tPtr = (Text *) data;
881
882 if (tPtr->flags.cursorShown) {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
884 } else {
885 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
886 }
887 paintText(tPtr);
888 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
889 }
890 #endif
891
892 static void updateCursorPosition(Text * tPtr)
893 {
894 TextBlock *tb = NULL;
895 int x, y, h, s;
896
897 if (tPtr->flags.needsLayOut)
898 layOutDocument(tPtr);
899
900 if (!(tb = tPtr->currentTextBlock)) {
901 if (!(tb = tPtr->firstTextBlock)) {
902 WMFont *font = tPtr->dFont;
903 tPtr->tpos = 0;
904 tPtr->cursor.h = font->height + abs(font->height - font->y);
905
906 tPtr->cursor.y = 2;
907 tPtr->cursor.x = 2;
908 return;
909 }
910 }
911
912 if (tb->blank) {
913 tPtr->tpos = 0;
914 y = tb->sections[0].y;
915 h = tb->sections[0].h;
916 x = tb->sections[0].x;
917
918 } else if (tb->graphic) {
919 y = tb->sections[0].y;
920 h = tb->sections[0].h;
921 x = tb->sections[0].x;
922 if (tPtr->tpos == 1)
923 x += tb->sections[0].w;
924
925 } else {
926 if (tPtr->tpos > tb->used)
927 tPtr->tpos = tb->used;
928
929 for (s = 0; s < tb->nsections - 1; s++) {
930
931 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
932 break;
933 }
934
935 y = tb->sections[s]._y;
936 h = tb->sections[s].h;
937 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
938 &tb->text[tb->sections[s].begin],
939 tPtr->tpos - tb->sections[s].begin);
940 }
941
942 tPtr->cursor.y = y;
943 tPtr->cursor.h = h;
944 tPtr->cursor.x = x;
945
946 /* scroll the bars if the cursor is not visible */
947 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
948 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
949 tPtr->vpos +=
950 (tPtr->cursor.y + tPtr->cursor.h + 10
951 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
952 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
953 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
954 }
955
956 }
957
958 updateScrollers(tPtr);
959 }
960
961 static void cursorToTextPosition(Text * tPtr, int x, int y)
962 {
963 TextBlock *tb = NULL;
964 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
965 char *text;
966
967 if (tPtr->flags.needsLayOut)
968 layOutDocument(tPtr);
969
970 y += (tPtr->vpos - tPtr->visible.y);
971 if (y < 0)
972 y = 0;
973
974 x -= (tPtr->visible.x - 2);
975 if (x < 0)
976 x = 0;
977
978 /* clicked is relative to document, not window... */
979 tPtr->clicked.x = x;
980 tPtr->clicked.y = y;
981
982 if (!(tb = tPtr->currentTextBlock)) {
983 if (!(tb = tPtr->firstTextBlock)) {
984 WMFont *font = tPtr->dFont;
985 tPtr->tpos = 0;
986 tPtr->cursor.h = font->height + abs(font->height - font->y);
987 tPtr->cursor.y = 2;
988 tPtr->cursor.x = 2;
989 return;
990 }
991 }
992
993 /* first, which direction? Most likely, newly clicked
994 position will be close to previous */
995 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
996 return;
997
998 s = (dir ? 0 : tb->nsections - 1);
999 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
1000 goto _doneV;
1001 }
1002
1003 /* get the first (or last) section of the TextBlock that
1004 lies about the vertical click point */
1005 done = False;
1006 while (!done && tb) {
1007
1008 if (tPtr->flags.monoFont && tb->graphic) {
1009 if ((dir ? tb->next : tb->prior))
1010 tb = (dir ? tb->next : tb->prior);
1011 continue;
1012 }
1013
1014 s = (dir ? 0 : tb->nsections - 1);
1015 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1016
1017 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1018 done = True;
1019 } else {
1020 dir ? s++ : s--;
1021 }
1022 }
1023
1024 if (!done) {
1025 if ((dir ? tb->next : tb->prior)) {
1026 tb = (dir ? tb->next : tb->prior);
1027 } else {
1028 pos = tb->used;
1029 break; /* goto _doneH; */
1030 }
1031 }
1032 }
1033
1034 if (s < 0 || s >= tb->nsections) {
1035 s = (dir ? tb->nsections - 1 : 0);
1036 }
1037
1038 _doneV:
1039 /* we have the line, which TextBlock on that line is it? */
1040 pos = (dir ? 0 : tb->sections[s].begin);
1041 if (tPtr->flags.monoFont && tb->graphic) {
1042 TextBlock *hold = tb;
1043 tb = getFirstNonGraphicBlockFor(hold, dir);
1044
1045 if (!tb) {
1046 tPtr->tpos = 0;
1047 tb = hold;
1048 s = 0;
1049 goto _doNothing;
1050 }
1051 }
1052
1053 if (tb->blank)
1054 _w = 0;
1055
1056 _y = tb->sections[s]._y;
1057
1058 while (tb) {
1059
1060 if (tPtr->flags.monoFont && tb->graphic) {
1061 tb = (dir ? tb->next : tb->prior);
1062 continue;
1063 }
1064
1065 if (dir) {
1066 if (tb->graphic) {
1067 if (tb->object)
1068 _w = WMWidgetWidth(tb->d.widget) - 5;
1069 else
1070 _w = tb->d.pixmap->width - 5;
1071
1072 if (tb->sections[0].x + _w >= x)
1073 break;
1074 } else {
1075 text = &(tb->text[tb->sections[s].begin]);
1076 len = tb->sections[s].end - tb->sections[s].begin;
1077 _w = WMWidthOfString(tb->d.font, text, len);
1078 if (tb->sections[s].x + _w >= x)
1079 break;
1080
1081 }
1082 } else {
1083 if (tb->sections[s].x <= x)
1084 break;
1085 }
1086
1087 if ((dir ? tb->next : tb->prior)) {
1088 TextBlock *nxt = (dir ? tb->next : tb->prior);
1089 if (tPtr->flags.monoFont && nxt->graphic) {
1090 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1091 if (!nxt) {
1092 pos = (dir ? 0 : tb->sections[s].begin);
1093 tPtr->cursor.x = tb->sections[s].x;
1094 goto _doneH;
1095 }
1096 }
1097
1098 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1099 /* this must be the last/first on this line. stop */
1100 pos = (dir ? tb->sections[s].end : 0);
1101 tPtr->cursor.x = tb->sections[s].x;
1102 if (!tb->blank) {
1103 if (tb->graphic) {
1104 if (tb->object)
1105 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1106 else
1107 tPtr->cursor.x += tb->d.pixmap->width;
1108 } else if (pos > tb->sections[s].begin) {
1109 tPtr->cursor.x +=
1110 WMWidthOfString(tb->d.font,
1111 &(tb->text[tb->sections[s].begin]),
1112 pos - tb->sections[s].begin);
1113 }
1114 }
1115 goto _doneH;
1116 }
1117 }
1118
1119 if ((dir ? tb->next : tb->prior)) {
1120 tb = (dir ? tb->next : tb->prior);
1121 } else {
1122 done = True;
1123 break;
1124 }
1125
1126 if (tb)
1127 s = (dir ? 0 : tb->nsections - 1);
1128 }
1129
1130 /* we have said TextBlock, now where within it? */
1131 if (tb) {
1132 if (tb->graphic) {
1133 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1134
1135 tPtr->cursor.x = tb->sections[0].x;
1136
1137 if (x > tPtr->cursor.x + gw / 2) {
1138 pos = 1;
1139 tPtr->cursor.x += gw;
1140 } else {
1141 printf("first %d\n", tb->first);
1142 if (tb->prior) {
1143 if (tb->prior->graphic)
1144 pos = 1;
1145 else
1146 pos = tb->prior->used;
1147 tb = tb->prior;
1148 } else
1149 pos = 0;
1150
1151 }
1152
1153 s = 0;
1154 goto _doneH;
1155
1156 } else {
1157 WMFont *f = tb->d.font;
1158 len = tb->sections[s].end - tb->sections[s].begin;
1159 text = &(tb->text[tb->sections[s].begin]);
1160
1161 _w = x - tb->sections[s].x;
1162 pos = 0;
1163
1164 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1165 pos++;
1166
1167 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1168
1169 pos += tb->sections[s].begin;
1170 }
1171 }
1172
1173 _doneH:
1174 if (tb->graphic) {
1175 tPtr->tpos = (pos <= 1) ? pos : 0;
1176 } else {
1177 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1178 }
1179 _doNothing:
1180 if (!tb)
1181 printf("...for this app will surely crash :-)\n");
1182
1183 tPtr->currentTextBlock = tb;
1184 tPtr->cursor.h = tb->sections[s].h;
1185 tPtr->cursor.y = tb->sections[s]._y;
1186
1187 /* scroll the bars if the cursor is not visible */
1188 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1189 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1190 tPtr->vpos +=
1191 (tPtr->cursor.y + tPtr->cursor.h + 10
1192 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1193 updateScrollers(tPtr);
1194 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1195 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1196 updateScrollers(tPtr);
1197 }
1198
1199 }
1200
1201 }
1202
1203 static void updateScrollers(Text * tPtr)
1204 {
1205
1206 if (tPtr->flags.frozen)
1207 return;
1208
1209 if (tPtr->vS) {
1210 if (tPtr->docHeight <= tPtr->visible.h) {
1211 WMSetScrollerParameters(tPtr->vS, 0, 1);
1212 tPtr->vpos = 0;
1213 } else {
1214 float hmax = (float)(tPtr->docHeight);
1215 WMSetScrollerParameters(tPtr->vS,
1216 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1217 (float)tPtr->visible.h / hmax);
1218 }
1219 } else
1220 tPtr->vpos = 0;
1221
1222 if (tPtr->hS) {
1223 if (tPtr->docWidth <= tPtr->visible.w) {
1224 WMSetScrollerParameters(tPtr->hS, 0, 1);
1225 tPtr->hpos = 0;
1226 } else {
1227 float wmax = (float)(tPtr->docWidth);
1228 WMSetScrollerParameters(tPtr->hS,
1229 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1230 (float)tPtr->visible.w / wmax);
1231 }
1232 } else
1233 tPtr->hpos = 0;
1234 }
1235
1236 static void scrollersCallBack(WMWidget * w, void *self)
1237 {
1238 Text *tPtr = (Text *) self;
1239 Bool scroll = False;
1240 int which;
1241
1242 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1243 return;
1244
1245 if (w == tPtr->vS) {
1246 int height;
1247 height = tPtr->visible.h;
1248
1249 which = WMGetScrollerHitPart(tPtr->vS);
1250 switch (which) {
1251
1252 case WSDecrementLine:
1253 if (tPtr->vpos > 0) {
1254 if (tPtr->vpos > 16)
1255 tPtr->vpos -= 16;
1256 else
1257 tPtr->vpos = 0;
1258 scroll = True;
1259 }
1260 break;
1261
1262 case WSIncrementLine:{
1263 int limit = tPtr->docHeight - height;
1264 if (tPtr->vpos < limit) {
1265 if (tPtr->vpos < limit - 16)
1266 tPtr->vpos += 16;
1267 else
1268 tPtr->vpos = limit;
1269 scroll = True;
1270 }
1271 }
1272 break;
1273
1274 case WSDecrementPage:
1275 if (((int)tPtr->vpos - (int)height) >= 0)
1276 tPtr->vpos -= height;
1277 else
1278 tPtr->vpos = 0;
1279
1280 scroll = True;
1281 break;
1282
1283 case WSIncrementPage:
1284 tPtr->vpos += height;
1285 if (tPtr->vpos > (tPtr->docHeight - height))
1286 tPtr->vpos = tPtr->docHeight - height;
1287 scroll = True;
1288 break;
1289
1290 case WSKnob:
1291 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1292 * (float)(tPtr->docHeight - height);
1293 scroll = True;
1294 break;
1295
1296 case WSKnobSlot:
1297 case WSNoPart:
1298 break;
1299 }
1300 scroll = (tPtr->vpos != tPtr->prevVpos);
1301 tPtr->prevVpos = tPtr->vpos;
1302 }
1303
1304 if (w == tPtr->hS) {
1305 int width = tPtr->visible.w;
1306
1307 which = WMGetScrollerHitPart(tPtr->hS);
1308 switch (which) {
1309
1310 case WSDecrementLine:
1311 if (tPtr->hpos > 0) {
1312 if (tPtr->hpos > 16)
1313 tPtr->hpos -= 16;
1314 else
1315 tPtr->hpos = 0;
1316 scroll = True;
1317 }
1318 break;
1319
1320 case WSIncrementLine:{
1321 int limit = tPtr->docWidth - width;
1322 if (tPtr->hpos < limit) {
1323 if (tPtr->hpos < limit - 16)
1324 tPtr->hpos += 16;
1325 else
1326 tPtr->hpos = limit;
1327 scroll = True;
1328 }
1329 }
1330 break;
1331
1332 case WSDecrementPage:
1333 if (((int)tPtr->hpos - (int)width) >= 0)
1334 tPtr->hpos -= width;
1335 else
1336 tPtr->hpos = 0;
1337
1338 scroll = True;
1339 break;
1340
1341 case WSIncrementPage:
1342 tPtr->hpos += width;
1343 if (tPtr->hpos > (tPtr->docWidth - width))
1344 tPtr->hpos = tPtr->docWidth - width;
1345 scroll = True;
1346 break;
1347
1348 case WSKnob:
1349 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1350 * (float)(tPtr->docWidth - width);
1351 scroll = True;
1352 break;
1353
1354 case WSKnobSlot:
1355 case WSNoPart:
1356 break;
1357 }
1358 scroll = (tPtr->hpos != tPtr->prevHpos);
1359 tPtr->prevHpos = tPtr->hpos;
1360 }
1361
1362 if (scroll) {
1363 updateScrollers(tPtr);
1364 paintText(tPtr);
1365 }
1366 }
1367
1368 typedef struct {
1369 TextBlock *tb;
1370 unsigned short begin, end; /* what part of the text block */
1371 } myLineItems;
1372
1373 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1374 {
1375 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1376 WMFont *font;
1377 char *text;
1378 TextBlock *tb, *tbsame = NULL;
1379
1380 if (!items || nitems == 0)
1381 return 0;
1382
1383 for (i = 0; i < nitems; i++) {
1384 tb = items[i].tb;
1385
1386 if (tb->graphic) {
1387 if (!tPtr->flags.monoFont) {
1388 if (tb->object) {
1389 WMWidget *wdt = tb->d.widget;
1390 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1391 if (tPtr->flags.alignment != WALeft)
1392 lw += WMWidgetWidth(wdt);
1393 } else {
1394 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1395 if (tPtr->flags.alignment != WALeft)
1396 lw += tb->d.pixmap->width;
1397 }
1398 }
1399
1400 } else {
1401 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1402 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1403 max_d = 2;
1404 line_height = WMAX(line_height, font->height + max_d);
1405 text = &(tb->text[items[i].begin]);
1406 len = items[i].end - items[i].begin;
1407 if (tPtr->flags.alignment != WALeft)
1408 lw += WMWidthOfString(font, text, len);
1409 }
1410 }
1411
1412 if (tPtr->flags.alignment == WARight) {
1413 j = tPtr->visible.w - lw;
1414 } else if (tPtr->flags.alignment == WACenter) {
1415 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1416 }
1417
1418 for (i = 0; i < nitems; i++) {
1419 tb = items[i].tb;
1420
1421 if (tbsame == tb) { /* extend it, since it's on same line */
1422 tb->sections[tb->nsections - 1].end = items[i].end;
1423 n = tb->nsections - 1;
1424 } else {
1425 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1426 n = tb->nsections - 1;
1427 tb->sections[n]._y = y + max_d;
1428 tb->sections[n].max_d = max_d;
1429 tb->sections[n].x = x + j;
1430 tb->sections[n].h = line_height;
1431 tb->sections[n].begin = items[i].begin;
1432 tb->sections[n].end = items[i].end;
1433 }
1434
1435 tb->sections[n].last = (i + 1 == nitems);
1436
1437 if (tb->graphic) {
1438 if (!tPtr->flags.monoFont) {
1439 if (tb->object) {
1440 WMWidget *wdt = tb->d.widget;
1441 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1442 tb->sections[n].w = WMWidgetWidth(wdt);
1443 } else {
1444 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1445 tb->sections[n].w = tb->d.pixmap->width;
1446 }
1447 x += tb->sections[n].w;
1448 }
1449 } else {
1450 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1451 len = items[i].end - items[i].begin;
1452 text = &(tb->text[items[i].begin]);
1453
1454 tb->sections[n].y = y + line_height - font->y;
1455 tb->sections[n].w =
1456 WMWidthOfString(font,
1457 &(tb->text[tb->sections[n].begin]),
1458 tb->sections[n].end - tb->sections[n].begin);
1459
1460 x += WMWidthOfString(font, text, len);
1461 }
1462
1463 tbsame = tb;
1464 }
1465
1466 return line_height;
1467
1468 }
1469
1470 static void layOutDocument(Text * tPtr)
1471 {
1472 TextBlock *tb;
1473 myLineItems *items = NULL;
1474 unsigned int itemsSize = 0, nitems = 0, begin, end;
1475 WMFont *font;
1476 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1477 char *start = NULL, *mark = NULL;
1478
1479 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1480 return;
1481
1482 assert(tPtr->visible.w > 20);
1483
1484 tPtr->docWidth = tPtr->visible.w;
1485 x = tPtr->margins[tb->marginN].first;
1486 bmargin = tPtr->margins[tb->marginN].body;
1487
1488 /* only partial layOut needed: re-Lay only affected textblocks */
1489 if (tPtr->flags.laidOut) {
1490 tb = tPtr->currentTextBlock;
1491
1492 /* search backwards for textblocks on same line */
1493 while (tb->prior) {
1494 if (!tb->sections || tb->nsections < 1) {
1495 tb = tPtr->firstTextBlock;
1496 tPtr->flags.laidOut = False;
1497 y = 0;
1498 goto _layOut;
1499 }
1500
1501 if (!tb->prior->sections || tb->prior->nsections < 1) {
1502 tb = tPtr->firstTextBlock;
1503 tPtr->flags.laidOut = False;
1504 y = 0;
1505 goto _layOut;
1506 }
1507
1508 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1509 break;
1510 }
1511 tb = tb->prior;
1512 }
1513
1514 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1515 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1516 tb->prior->sections[tb->prior->nsections - 1].h -
1517 tb->prior->sections[tb->prior->nsections - 1].max_d;
1518 } else {
1519 y = 0;
1520 }
1521 }
1522
1523 _layOut:
1524 while (tb) {
1525
1526 if (tb->sections && tb->nsections > 0) {
1527 wfree(tb->sections);
1528 tb->sections = NULL;
1529 tb->nsections = 0;
1530 }
1531
1532 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1533 TextBlock *next = tb->next;
1534 tPtr->currentTextBlock = tb;
1535 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1536 tb = next;
1537 tb->first = True;
1538 continue;
1539 }
1540
1541 if (tb->first && tb != tPtr->firstTextBlock) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 x = tPtr->margins[tb->marginN].first;
1544 bmargin = tPtr->margins[tb->marginN].body;
1545 nitems = 0;
1546 lw = 0;
1547 }
1548
1549 if (tb->graphic) {
1550 if (!tPtr->flags.monoFont) {
1551 if (tb->object)
1552 width = WMWidgetWidth(tb->d.widget);
1553 else
1554 width = tb->d.pixmap->width;
1555
1556 if (width > tPtr->docWidth)
1557 tPtr->docWidth = width;
1558
1559 lw += width;
1560 if (lw >= tPtr->visible.w - x) {
1561 y += layOutLine(tPtr, items, nitems, x, y);
1562 nitems = 0;
1563 x = bmargin;
1564 lw = width;
1565 }
1566
1567 if (nitems + 1 > itemsSize) {
1568 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1569 }
1570
1571 items[nitems].tb = tb;
1572 items[nitems].begin = 0;
1573 items[nitems].end = 0;
1574 nitems++;
1575 }
1576
1577 } else if ((start = tb->text)) {
1578 begin = end = 0;
1579 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1580
1581 while (start) {
1582 mark = strchr(start, ' ');
1583 if (mark) {
1584 end += (int)(mark - start) + 1;
1585 start = mark + 1;
1586 } else {
1587 end += strlen(start);
1588 start = mark;
1589 }
1590
1591 if (end > tb->used)
1592 end = tb->used;
1593
1594 if (end - begin > 0) {
1595
1596 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1597
1598 /* if it won't fit, char wrap it */
1599 if (width >= tPtr->visible.w) {
1600 char *t = &tb->text[begin];
1601 int l = end - begin, i = 0;
1602 do {
1603 width = WMWidthOfString(font, t, ++i);
1604 } while (width < tPtr->visible.w && i < l);
1605 if (i > 2)
1606 i--;
1607 end = begin + i;
1608 start = &tb->text[end];
1609 }
1610
1611 lw += width;
1612 }
1613
1614 if (lw >= tPtr->visible.w - x) {
1615 y += layOutLine(tPtr, items, nitems, x, y);
1616 lw = width;
1617 x = bmargin;
1618 nitems = 0;
1619 }
1620
1621 if (nitems + 1 > itemsSize) {
1622 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1623 }
1624
1625 items[nitems].tb = tb;
1626 items[nitems].begin = begin;
1627 items[nitems].end = end;
1628 nitems++;
1629
1630 begin = end;
1631 }
1632 }
1633
1634 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1635 if (0 && tPtr->flags.laidOut
1636 && tb->next && tb->next->sections && tb->next->nsections > 0
1637 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1638 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1639 TextBlock *ltb = tPtr->lastTextBlock;
1640 int ly = ltb->sections[ltb->nsections - 1]._y;
1641 int lh = ltb->sections[ltb->nsections - 1].h;
1642 int ss, sd;
1643
1644 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1645 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1646
1647 y += layOutLine(tPtr, items, nitems, x, y);
1648 ss = ly + lh - y;
1649 sd = tPtr->docHeight - y;
1650
1651 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1652 y += tb->next->sections[0]._y - y;
1653 nitems = 0;
1654 printf("nitems%d\n", nitems);
1655 if (ss - sd != 0)
1656 y = tPtr->docHeight + ss - sd;
1657
1658 break;
1659 } else {
1660 tPtr->flags.laidOut = False;
1661 }
1662 }
1663
1664 tb = tb->next;
1665 }
1666
1667 if (nitems > 0)
1668 y += layOutLine(tPtr, items, nitems, x, y);
1669
1670 if (tPtr->docHeight != y + 10) {
1671 tPtr->docHeight = y + 10;
1672 updateScrollers(tPtr);
1673 }
1674
1675 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1676 XEvent event;
1677
1678 tPtr->flags.horizOnDemand = True;
1679 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1680 event.type = Expose;
1681 handleEvents(&event, (void *)tPtr);
1682
1683 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1684 tPtr->flags.horizOnDemand = False;
1685 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1686 }
1687
1688 tPtr->flags.laidOut = True;
1689
1690 if (items && itemsSize > 0)
1691 wfree(items);
1692
1693 }
1694
1695 static void textDidResize(W_ViewDelegate * self, WMView * view)
1696 {
1697 Text *tPtr = (Text *) view->self;
1698 unsigned short w = tPtr->view->size.width;
1699 unsigned short h = tPtr->view->size.height;
1700 unsigned short rh = 0, vw = 0, rel;
1701
1702 rel = (tPtr->flags.relief == WRFlat);
1703
1704 if (tPtr->ruler && tPtr->flags.rulerShown) {
1705 WMMoveWidget(tPtr->ruler, 2, 2);
1706 WMResizeWidget(tPtr->ruler, w - 4, 40);
1707 rh = 40;
1708 }
1709
1710 if (tPtr->vS) {
1711 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1712 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1713 vw = 20;
1714 WMSetRulerOffset(tPtr->ruler, 22);
1715 } else
1716 WMSetRulerOffset(tPtr->ruler, 2);
1717
1718 if (tPtr->hS) {
1719 if (tPtr->vS) {
1720 WMMoveWidget(tPtr->hS, vw, h - 21);
1721 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1722 } else {
1723 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1724 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1725 }
1726 }
1727
1728 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1729 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1730 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1731 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1732 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1733 tPtr->margins[0].right = tPtr->visible.w;
1734
1735 if (tPtr->view->flags.realized) {
1736
1737 if (tPtr->db) {
1738 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1739 tPtr->db = (Pixmap) NULL;
1740 }
1741
1742 if (tPtr->visible.w < 40)
1743 tPtr->visible.w = 40;
1744 if (tPtr->visible.h < 20)
1745 tPtr->visible.h = 20;
1746
1747 if (!tPtr->db) {
1748 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1749 tPtr->view->window, tPtr->visible.w,
1750 tPtr->visible.h, tPtr->view->screen->depth);
1751 }
1752 }
1753
1754 WMThawText(tPtr);
1755 }
1756
1757 W_ViewDelegate _TextViewDelegate = {
1758 NULL,
1759 NULL,
1760 textDidResize,
1761 NULL,
1762 };
1763
1764 #define TEXT_BUFFER_INCR 8
1765 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1766
1767 static void clearText(Text * tPtr)
1768 {
1769 tPtr->vpos = tPtr->hpos = 0;
1770 tPtr->docHeight = tPtr->docWidth = 0;
1771 tPtr->cursor.x = -23;
1772
1773 if (!tPtr->firstTextBlock)
1774 return;
1775
1776 while (tPtr->currentTextBlock)
1777 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1778
1779 tPtr->firstTextBlock = NULL;
1780 tPtr->currentTextBlock = NULL;
1781 tPtr->lastTextBlock = NULL;
1782 WMEmptyArray(tPtr->gfxItems);
1783 }
1784
1785 /* possibly remove a single character from the currentTextBlock,
1786 or if there's a selection, remove it...
1787 note that Delete and Backspace are treated differently */
1788 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1789 {
1790 TextBlock *tb;
1791 Bool back = (Bool) (ksym == XK_BackSpace);
1792 Bool done = 1, wasFirst = 0;
1793
1794 if (!tPtr->flags.editable)
1795 return;
1796
1797 if (!(tb = tPtr->currentTextBlock))
1798 return;
1799
1800 if (tPtr->flags.ownsSelection) {
1801 if (removeSelection(tPtr))
1802 layOutDocument(tPtr);
1803 return;
1804 }
1805
1806 wasFirst = tb->first;
1807 if (back && tPtr->tpos < 1) {
1808 if (tb->prior) {
1809 if (tb->prior->blank) {
1810 tPtr->currentTextBlock = tb->prior;
1811 WMRemoveTextBlock(tPtr);
1812 tPtr->currentTextBlock = tb;
1813 tb->first = True;
1814 layOutDocument(tPtr);
1815 return;
1816 } else {
1817 if (tb->blank) {
1818 TextBlock *prior = tb->prior;
1819 tPtr->currentTextBlock = tb;
1820 WMRemoveTextBlock(tPtr);
1821 tb = prior;
1822 } else {
1823 tb = tb->prior;
1824 }
1825
1826 if (tb->graphic)
1827 tPtr->tpos = 1;
1828 else
1829 tPtr->tpos = tb->used;
1830
1831 tPtr->currentTextBlock = tb;
1832 done = 1;
1833 if (wasFirst) {
1834 if (tb->next)
1835 tb->next->first = False;
1836 layOutDocument(tPtr);
1837 return;
1838 }
1839 }
1840 }
1841 }
1842
1843 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1844 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1845 if (back)
1846 tPtr->tpos--;
1847 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1848 tb->used--;
1849 done = 0;
1850 }
1851
1852 /* if there are no characters left to back over in the textblock,
1853 but it still has characters to the right of the cursor: */
1854 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1855 || tb->graphic) {
1856
1857 /* no more chars, and it's marked as blank? */
1858 if (tb->blank) {
1859 TextBlock *sibling = (back ? tb->prior : tb->next);
1860
1861 if (tb->used == 0 || tb->graphic)
1862 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1863
1864 if (sibling) {
1865 tPtr->currentTextBlock = sibling;
1866 if (tb->graphic)
1867 tPtr->tpos = (back ? 1 : 0);
1868 else
1869 tPtr->tpos = (back ? sibling->used : 0);
1870 }
1871 /* no more chars, so mark it as blank */
1872 } else if (tb->used == 0) {
1873 tb->blank = 1;
1874 } else if (tb->graphic) {
1875 Bool hasNext = (tb->next != NULL);
1876
1877 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1878 if (hasNext) {
1879 tPtr->tpos = 0;
1880 } else if (tPtr->currentTextBlock) {
1881 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1882 }
1883 } else
1884 printf("DEBUG: unaccounted for... catch this!\n");
1885 }
1886
1887 layOutDocument(tPtr);
1888 }
1889
1890 static void insertTextInteractively(Text * tPtr, char *text, int len)
1891 {
1892 TextBlock *tb;
1893 char *newline = NULL;
1894
1895 if (!tPtr->flags.editable) {
1896 return;
1897 }
1898
1899 if (len < 1 || !text)
1900 return;
1901
1902 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1903 return;
1904
1905 if (tPtr->flags.ownsSelection)
1906 removeSelection(tPtr);
1907
1908 if (tPtr->flags.ignoreNewLine) {
1909 int i;
1910 for (i = 0; i < len; i++) {
1911 if (text[i] == '\n')
1912 text[i] = ' ';
1913 }
1914 }
1915
1916 tb = tPtr->currentTextBlock;
1917 if (!tb || tb->graphic) {
1918 tPtr->tpos = 0;
1919 WMAppendTextStream(tPtr, text);
1920 layOutDocument(tPtr);
1921 return;
1922 }
1923
1924 if ((newline = strchr(text, '\n'))) {
1925 int nlen = (int)(newline - text);
1926 int s = tb->used - tPtr->tpos;
1927
1928 if (!tb->blank && nlen > 0) {
1929 char *save = NULL;
1930
1931 if (s > 0) {
1932 save = wmalloc(s);
1933 memcpy(save, &tb->text[tPtr->tpos], s);
1934 tb->used -= (tb->used - tPtr->tpos);
1935 }
1936 insertTextInteractively(tPtr, text, nlen);
1937 newline++;
1938 WMAppendTextStream(tPtr, newline);
1939 if (s > 0) {
1940 insertTextInteractively(tPtr, save, s);
1941 wfree(save);
1942 }
1943 } else {
1944 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1945
1946 unsigned short savePos = tPtr->tpos;
1947 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1948 tb->d.font, tb->color, True,
1949 tb->used - tPtr->tpos);
1950
1951 if (tb->sections[0].end == tPtr->tpos)
1952 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1953 NULL, tb->d.font,
1954 tb->color, True, 0));
1955
1956 tb->used = savePos;
1957 WMAppendTextBlock(tPtr, ntb);
1958 tPtr->tpos = 0;
1959
1960 } else if (tPtr->tpos == tb->used) {
1961 if (tPtr->flags.indentNewLine) {
1962 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1963 " ", tb->d.font,
1964 tb->color, True, 4));
1965 tPtr->tpos = 4;
1966 } else {
1967 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1968 NULL, tb->d.font,
1969 tb->color, True, 0));
1970 tPtr->tpos = 0;
1971 }
1972 } else if (tPtr->tpos == 0) {
1973 if (tPtr->flags.indentNewLine) {
1974 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1975 " ", tb->d.font,
1976 tb->color, True, 4));
1977 } else {
1978 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1979 NULL, tb->d.font,
1980 tb->color, True, 0));
1981 }
1982 tPtr->tpos = 0;
1983 if (tPtr->currentTextBlock->next)
1984 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1985 }
1986 }
1987 } else {
1988 if (tb->used + len >= tb->allocated) {
1989 tb->allocated = reqBlockSize(tb->used + len);
1990 tb->text = wrealloc(tb->text, tb->allocated);
1991 }
1992
1993 if (tb->blank) {
1994 memcpy(tb->text, text, len);
1995 tb->used = len;
1996 tPtr->tpos = len;
1997 tb->text[tb->used] = 0;
1998 tb->blank = False;
1999
2000 } else {
2001 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
2002 memmove(&tb->text[tPtr->tpos], text, len);
2003 tb->used += len;
2004 tPtr->tpos += len;
2005 tb->text[tb->used] = 0;
2006 }
2007
2008 }
2009
2010 layOutDocument(tPtr);
2011 }
2012
2013 static void selectRegion(Text * tPtr, int x, int y)
2014 {
2015
2016 if (x < 0 || y < 0)
2017 return;
2018
2019 y += (tPtr->flags.rulerShown ? 40 : 0);
2020 y += tPtr->vpos;
2021 if (y > 10)
2022 y -= 10; /* the original offset */
2023
2024 x -= tPtr->visible.x - 2;
2025 if (x < 0)
2026 x = 0;
2027
2028 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2029 tPtr->sel.w = abs(tPtr->clicked.x - x);
2030 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2031 tPtr->sel.h = abs(tPtr->clicked.y - y);
2032
2033 tPtr->flags.ownsSelection = True;
2034 paintText(tPtr);
2035 }
2036
2037 static void releaseSelection(Text * tPtr)
2038 {
2039 TextBlock *tb = tPtr->firstTextBlock;
2040
2041 while (tb) {
2042 tb->selected = False;
2043 tb = tb->next;
2044 }
2045 tPtr->flags.ownsSelection = False;
2046 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2047
2048 paintText(tPtr);
2049 }
2050
2051 WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2052 {
2053 Text *tPtr = view->self;
2054 Display *dpy = tPtr->view->screen->display;
2055 Atom _TARGETS;
2056 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2057 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2058 WMData *data = NULL;
2059
2060 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2061 char *text = WMGetTextSelectedStream(tPtr);
2062
2063 if (text) {
2064 data = WMCreateDataWithBytes(text, strlen(text));
2065 WMSetDataFormat(data, TYPETEXT);
2066 }
2067 *type = target;
2068 return data;
2069 } else
2070 printf("didn't get it\n");
2071
2072 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2073 if (target == _TARGETS) {
2074 Atom *ptr;
2075
2076 ptr = wmalloc(4 * sizeof(Atom));
2077 ptr[0] = _TARGETS;
2078 ptr[1] = XA_STRING;
2079 ptr[2] = TEXT;
2080 ptr[3] = COMPOUND_TEXT;
2081
2082 data = WMCreateDataWithBytes(ptr, 4 * 4);
2083 WMSetDataFormat(data, 32);
2084
2085 *type = target;
2086 return data;
2087 }
2088
2089 return NULL;
2090 }
2091
2092 static void lostHandler(WMView * view, Atom selection, void *cdata)
2093 {
2094 releaseSelection((WMText *) view->self);
2095 }
2096
2097 static WMSelectionProcs selectionHandler = {
2098 requestHandler, lostHandler, NULL
2099 };
2100
2101 static void ownershipObserver(void *observerData, WMNotification * notification)
2102 {
2103 if (observerData != WMGetNotificationClientData(notification))
2104 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2105 }
2106
2107 static void autoSelectText(Text * tPtr, int clicks)
2108 {
2109 int x, start;
2110 TextBlock *tb;
2111 char *mark = NULL, behind, ahead;
2112
2113 if (!(tb = tPtr->currentTextBlock))
2114 return;
2115
2116 if (clicks == 2) {
2117
2118 switch (tb->text[tPtr->tpos]) {
2119 case ' ':
2120 return;
2121 /*
2122 case '<': case '>': behind = '<'; ahead = '>'; break;
2123 case '{': case '}': behind = '{'; ahead = '}'; break;
2124 case '[': case ']': behind = '['; ahead = ']'; break;
2125 */
2126 default:
2127 behind = ahead = ' ';
2128 }
2129
2130 tPtr->sel.y = tPtr->cursor.y + 5;
2131 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2132
2133 if (tb->graphic) {
2134 tPtr->sel.x = tb->sections[0].x;
2135 tPtr->sel.w = tb->sections[0].w;
2136 } else {
2137 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2138
2139 start = tPtr->tpos;
2140 while (start > 0 && tb->text[start - 1] != behind)
2141 start--;
2142
2143 x = tPtr->cursor.x;
2144 if (tPtr->tpos > start) {
2145 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2146 }
2147 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2148
2149 if ((mark = strchr(&tb->text[start], ahead))) {
2150 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2151 (int)(mark - &tb->text[start]));
2152 } else if (tb->used > start) {
2153 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2154 }
2155 }
2156
2157 } else if (clicks == 3) {
2158 TextBlock *cur = tb;
2159
2160 while (tb && !tb->first) {
2161 tb = tb->prior;
2162 }
2163 tPtr->sel.y = tb->sections[0]._y;
2164
2165 tb = cur;
2166 while (tb->next && !tb->next->first) {
2167 tb = tb->next;
2168 }
2169 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2170
2171 tPtr->sel.x = 0;
2172 tPtr->sel.w = tPtr->docWidth;
2173 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2174 }
2175
2176 if (!tPtr->flags.ownsSelection) {
2177 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2178 tPtr->flags.ownsSelection = True;
2179 }
2180 paintText(tPtr);
2181
2182 }
2183
2184 # if 0
2185 static void fontChanged(void *observerData, WMNotification * notification)
2186 {
2187 WMText *tPtr = (WMText *) observerData;
2188 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2189 printf("fontChanged\n");
2190
2191 if (!tPtr || !font)
2192 return;
2193
2194 if (tPtr->flags.ownsSelection)
2195 WMSetTextSelectionFont(tPtr, font);
2196 }
2197 #endif
2198
2199 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2200 {
2201 char buffer[64];
2202 KeySym ksym;
2203 int control_pressed = False;
2204 TextBlock *tb = NULL;
2205
2206 if (((XKeyEvent *) event)->state & ControlMask)
2207 control_pressed = True;
2208 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2209
2210 switch (ksym) {
2211
2212 case XK_Home:
2213 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2214 tPtr->tpos = 0;
2215 updateCursorPosition(tPtr);
2216 paintText(tPtr);
2217 break;
2218
2219 case XK_End:
2220 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2221 if (tPtr->currentTextBlock->graphic)
2222 tPtr->tpos = 1;
2223 else
2224 tPtr->tpos = tPtr->currentTextBlock->used;
2225 }
2226 updateCursorPosition(tPtr);
2227 paintText(tPtr);
2228 break;
2229
2230 case XK_Left:
2231 if (!(tb = tPtr->currentTextBlock))
2232 break;
2233 if (tb->graphic)
2234 goto L_imaGFX;
2235
2236 if (tPtr->tpos == 0) {
2237 L_imaGFX:
2238 if (tb->prior) {
2239 tPtr->currentTextBlock = tb->prior;
2240 if (tPtr->currentTextBlock->graphic)
2241 tPtr->tpos = 1;
2242 else
2243 tPtr->tpos = tPtr->currentTextBlock->used;
2244
2245 if (!tb->first && tPtr->tpos > 0)
2246 tPtr->tpos--;
2247 } else
2248 tPtr->tpos = 0;
2249 } else
2250 tPtr->tpos--;
2251 updateCursorPosition(tPtr);
2252 paintText(tPtr);
2253 break;
2254
2255 case XK_Right:
2256 if (!(tb = tPtr->currentTextBlock))
2257 break;
2258 if (tb->graphic)
2259 goto R_imaGFX;
2260 if (tPtr->tpos == tb->used) {
2261 R_imaGFX:
2262 if (tb->next) {
2263 tPtr->currentTextBlock = tb->next;
2264 tPtr->tpos = 0;
2265 if (!tb->next->first && tb->next->used > 0)
2266 tPtr->tpos++;
2267 } else {
2268 if (tb->graphic)
2269 tPtr->tpos = 1;
2270 else
2271 tPtr->tpos = tb->used;
2272 }
2273 } else
2274 tPtr->tpos++;
2275 updateCursorPosition(tPtr);
2276 paintText(tPtr);
2277 break;
2278
2279 case XK_Down:
2280 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2281 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2282 paintText(tPtr);
2283 break;
2284
2285 case XK_Up:
2286 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2287 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2288 paintText(tPtr);
2289 break;
2290
2291 case XK_BackSpace:
2292 case XK_Delete:
2293 #ifdef XK_KP_Delete
2294 case XK_KP_Delete:
2295 #endif
2296 deleteTextInteractively(tPtr, ksym);
2297 updateCursorPosition(tPtr);
2298 paintText(tPtr);
2299 break;
2300
2301 case XK_Control_R:
2302 case XK_Control_L:
2303 control_pressed = True;
2304 break;
2305
2306 case XK_Tab:
2307 insertTextInteractively(tPtr, " ", 4);
2308 updateCursorPosition(tPtr);
2309 paintText(tPtr);
2310 break;
2311
2312 case XK_Return:
2313 *buffer = '\n';
2314 default:
2315 if (*buffer != 0 && !control_pressed) {
2316 insertTextInteractively(tPtr, buffer, strlen(buffer));
2317 updateCursorPosition(tPtr);
2318 paintText(tPtr);
2319
2320 } else if (control_pressed && ksym == XK_r) {
2321 Bool i = !tPtr->flags.rulerShown;
2322 WMShowTextRuler(tPtr, i);
2323 tPtr->flags.rulerShown = i;
2324 } else if (control_pressed && *buffer == '\a') {
2325 XBell(tPtr->view->screen->display, 0);
2326 } else {
2327 WMRelayToNextResponder(tPtr->view, event);
2328 }
2329 }
2330
2331 if (!control_pressed && tPtr->flags.ownsSelection) {
2332 releaseSelection(tPtr);
2333 }
2334 }
2335
2336 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2337 {
2338 Text *tPtr = (Text *) view->self;
2339 char *text;
2340
2341 tPtr->flags.waitingForSelection = 0;
2342
2343 if (data) {
2344 text = (char *)WMDataBytes(data);
2345
2346 if (tPtr->parser) {
2347 (tPtr->parser) (tPtr, (void *)text);
2348 layOutDocument(tPtr);
2349 } else
2350 insertTextInteractively(tPtr, text, strlen(text));
2351 updateCursorPosition(tPtr);
2352 paintText(tPtr);
2353
2354 } else {
2355 int n;
2356
2357 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2358
2359 if (text) {
2360 text[n] = 0;
2361 if (tPtr->parser) {
2362 (tPtr->parser) (tPtr, (void *)text);
2363 layOutDocument(tPtr);
2364 } else
2365 insertTextInteractively(tPtr, text, n);
2366 updateCursorPosition(tPtr);
2367 paintText(tPtr);
2368
2369 XFree(text);
2370 }
2371 }
2372
2373 }
2374
2375 static void handleActionEvents(XEvent * event, void *data)
2376 {
2377 Text *tPtr = (Text *) data;
2378 Display *dpy = event->xany.display;
2379 KeySym ksym;
2380
2381 switch (event->type) {
2382 case KeyPress:
2383 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2384 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2385 tPtr->flags.extendSelection = True;
2386 return;
2387 }
2388
2389 if (tPtr->flags.focused) {
2390 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2391 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2392 GrabModeAsync, GrabModeAsync, None,
2393 tPtr->view->screen->invisibleCursor, CurrentTime);
2394 tPtr->flags.pointerGrabbed = True;
2395 handleTextKeyPress(tPtr, event);
2396
2397 }
2398 break;
2399
2400 case KeyRelease:
2401 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2402 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2403 tPtr->flags.extendSelection = False;
2404 return;
2405 /* end modify flag so selection can be extended */
2406 }
2407 break;
2408
2409 case MotionNotify:
2410
2411 if (tPtr->flags.pointerGrabbed) {
2412 tPtr->flags.pointerGrabbed = False;
2413 XUngrabPointer(dpy, CurrentTime);
2414 }
2415
2416 if (tPtr->flags.waitingForSelection)
2417 break;
2418
2419 if ((event->xmotion.state & Button1Mask)) {
2420
2421 if (WMIsDraggingFromView(tPtr->view)) {
2422 WMDragImageFromView(tPtr->view, event);
2423 break;
2424 }
2425
2426 if (!tPtr->flags.ownsSelection) {
2427 WMCreateSelectionHandler(tPtr->view,
2428 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2429 tPtr->flags.ownsSelection = True;
2430 }
2431 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2432 break;
2433 }
2434
2435 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2436 break;
2437
2438 case ButtonPress:
2439
2440 if (tPtr->flags.pointerGrabbed) {
2441 tPtr->flags.pointerGrabbed = False;
2442 XUngrabPointer(dpy, CurrentTime);
2443 break;
2444 }
2445
2446 if (tPtr->flags.waitingForSelection)
2447 break;
2448
2449 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2450 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2451 return;
2452 }
2453
2454 if (tPtr->flags.ownsSelection)
2455 releaseSelection(tPtr);
2456
2457 if (event->xbutton.button == Button1) {
2458 TextBlock *tb = tPtr->currentTextBlock;
2459
2460 if (WMIsDoubleClick(event)) {
2461
2462 tPtr->lastClickTime = event->xbutton.time;
2463 if (tb && tb->graphic && !tb->object) {
2464 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2465 char *desc;
2466
2467 desc = wmalloc(tb->used + 1);
2468 memcpy(desc, tb->text, tb->used);
2469 desc[tb->used] = 0;
2470 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2471 wfree(desc);
2472 }
2473 } else {
2474 autoSelectText(tPtr, 2);
2475 }
2476 break;
2477 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2478 tPtr->lastClickTime = event->xbutton.time;
2479 autoSelectText(tPtr, 3);
2480 break;
2481 }
2482
2483 if (!tPtr->flags.focused) {
2484 WMSetFocusToWidget(tPtr);
2485 tPtr->flags.focused = True;
2486 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2487
2488 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2489 WMDragImageFromView(tPtr->view, event);
2490 break;
2491 }
2492
2493 tPtr->lastClickTime = event->xbutton.time;
2494 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2495 paintText(tPtr);
2496 }
2497
2498 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2499 WMScrollText(tPtr, 16);
2500 break;
2501 }
2502
2503 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2504 WMScrollText(tPtr, -16);
2505 break;
2506 }
2507
2508 if (event->xbutton.button == Button2) {
2509 char *text = NULL;
2510 int n;
2511
2512 if (!tPtr->flags.editable) {
2513 XBell(dpy, 0);
2514 break;
2515 }
2516
2517 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2518 event->xbutton.time, pasteText, NULL)) {
2519
2520 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2521 tPtr->flags.waitingForSelection = 0;
2522
2523 if (text) {
2524 text[n] = 0;
2525
2526 if (tPtr->parser) {
2527 (tPtr->parser) (tPtr, (void *)text);
2528 layOutDocument(tPtr);
2529 } else
2530 insertTextInteractively(tPtr, text, n);
2531
2532 XFree(text);
2533 #if 0
2534 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2535 (void *)WMInsertTextEvent);
2536 #endif
2537 updateCursorPosition(tPtr);
2538 paintText(tPtr);
2539
2540 } else {
2541 tPtr->flags.waitingForSelection = True;
2542 }
2543 }
2544 break;
2545 }
2546
2547 case ButtonRelease:
2548 if (tPtr->flags.pointerGrabbed) {
2549 tPtr->flags.pointerGrabbed = False;
2550 XUngrabPointer(dpy, CurrentTime);
2551 break;
2552 }
2553
2554 if (tPtr->flags.waitingForSelection)
2555 break;
2556
2557 if (WMIsDraggingFromView(tPtr->view))
2558 WMDragImageFromView(tPtr->view, event);
2559 }
2560
2561 }
2562
2563 static void handleEvents(XEvent * event, void *data)
2564 {
2565 Text *tPtr = (Text *) data;
2566
2567 switch (event->type) {
2568 case Expose:
2569
2570 if (event->xexpose.count != 0)
2571 break;
2572
2573 if (tPtr->hS) {
2574 if (!(W_VIEW(tPtr->hS))->flags.realized)
2575 WMRealizeWidget(tPtr->hS);
2576 }
2577
2578 if (tPtr->vS) {
2579 if (!(W_VIEW(tPtr->vS))->flags.realized)
2580 WMRealizeWidget(tPtr->vS);
2581 }
2582
2583 if (tPtr->ruler) {
2584 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2585 WMRealizeWidget(tPtr->ruler);
2586
2587 }
2588
2589 if (!tPtr->db)
2590 textDidResize(tPtr->view->delegate, tPtr->view);
2591
2592 paintText(tPtr);
2593 break;
2594
2595 case FocusIn:
2596 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2597 != tPtr->view)
2598 return;
2599 tPtr->flags.focused = True;
2600 #if DO_BLINK
2601 if (tPtr->flags.editable && !tPtr->timerID) {
2602 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2603 }
2604 #endif
2605
2606 break;
2607
2608 case FocusOut:
2609 tPtr->flags.focused = False;
2610 paintText(tPtr);
2611 #if DO_BLINK
2612 if (tPtr->timerID) {
2613 WMDeleteTimerHandler(tPtr->timerID);
2614 tPtr->timerID = NULL;
2615 }
2616 #endif
2617 break;
2618
2619 case DestroyNotify:
2620 clearText(tPtr);
2621 if (tPtr->db)
2622 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2623 if (tPtr->gfxItems)
2624 WMEmptyArray(tPtr->gfxItems);
2625 #if DO_BLINK
2626 if (tPtr->timerID)
2627 WMDeleteTimerHandler(tPtr->timerID);
2628 #endif
2629 WMReleaseFont(tPtr->dFont);
2630 WMReleaseColor(tPtr->dColor);
2631 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2632 WMRemoveNotificationObserver(tPtr);
2633
2634 WMFreeArray(tPtr->xdndSourceTypes);
2635 WMFreeArray(tPtr->xdndDestinationTypes);
2636
2637 wfree(tPtr);
2638
2639 break;
2640
2641 }
2642 }
2643
2644 static void insertPlainText(Text * tPtr, char *text)
2645 {
2646 char *start, *mark;
2647 void *tb = NULL;
2648
2649 start = text;
2650 while (start) {
2651 mark = strchr(start, '\n');
2652 if (mark) {
2653 tb = WMCreateTextBlockWithText(tPtr,
2654 start, tPtr->dFont,
2655 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2656 start = mark + 1;
2657 tPtr->flags.first = True;
2658 } else {
2659 if (start && strlen(start)) {
2660 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2661 tPtr->dColor, tPtr->flags.first, strlen(start));
2662 } else
2663 tb = NULL;
2664 tPtr->flags.first = False;
2665 start = mark;
2666 }
2667
2668 if (tPtr->flags.prepend)
2669 WMPrependTextBlock(tPtr, tb);
2670 else
2671 WMAppendTextBlock(tPtr, tb);
2672 }
2673 }
2674
2675 static void rulerMoveCallBack(WMWidget * w, void *self)
2676 {
2677 Text *tPtr = (Text *) self;
2678
2679 if (!tPtr)
2680 return;
2681 if (W_CLASS(tPtr) != WC_Text)
2682 return;
2683
2684 paintText(tPtr);
2685 }
2686
2687 static void rulerReleaseCallBack(WMWidget * w, void *self)
2688 {
2689 Text *tPtr = (Text *) self;
2690
2691 if (!tPtr)
2692 return;
2693 if (W_CLASS(tPtr) != WC_Text)
2694 return;
2695
2696 WMThawText(tPtr);
2697 return;
2698 }
2699
2700 static WMArray *dropDataTypes(WMView * self)
2701 {
2702 return ((Text *) self->self)->xdndSourceTypes;
2703 }
2704
2705 static WMDragOperationType wantedDropOperation(WMView * self)
2706 {
2707 return WDOperationCopy;
2708 }
2709
2710 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2711 {
2712 return (allowedOperation == WDOperationCopy);
2713 }
2714
2715 static WMData *fetchDragData(WMView * self, char *type)
2716 {
2717 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2718 char *desc;
2719 WMData *data;
2720
2721 if (strcmp(type, "text/plain")) {
2722 if (!tb)
2723 return NULL;
2724
2725 desc = wmalloc(tb->used + 1);
2726 memcpy(desc, tb->text, tb->used);
2727 desc[tb->used] = 0;
2728 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2729
2730 wfree(desc);
2731
2732 return data;
2733 }
2734
2735 return NULL;
2736 }
2737
2738 static WMDragSourceProcs _DragSourceProcs = {
2739 dropDataTypes,
2740 wantedDropOperation,
2741 NULL,
2742 acceptDropOperation,
2743 NULL,
2744 NULL,
2745 fetchDragData
2746 };
2747
2748 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2749 {
2750 return ((Text *) self->self)->xdndDestinationTypes;
2751 }
2752
2753 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2754 {
2755 return WDOperationCopy;
2756 }
2757
2758 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2759 {
2760 WMText *tPtr = (WMText *) self->self;
2761 WMData *data;
2762 char *colorName;
2763 WMColor *color;
2764
2765 if (tPtr) {
2766
2767 /* only one required type, implies only one drop data */
2768
2769 /* get application/X-color if any */
2770 data = (WMData *) WMPopFromArray(dropData);
2771 if (data != NULL) {
2772 colorName = (char *)WMDataBytes(data);
2773 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2774
2775 if (color) {
2776 WMSetTextSelectionColor(tPtr, color);
2777 WMReleaseColor(color);
2778 }
2779 }
2780 }
2781 }
2782
2783 static WMDragDestinationProcs _DragDestinationProcs = {
2784 NULL,
2785 requiredDataTypes,
2786 allowedOperation,
2787 NULL,
2788 performDragOperation,
2789 NULL
2790 };
2791
2792 char *getStream(WMText * tPtr, int sel, int array)
2793 {
2794 TextBlock *tb = NULL;
2795 char *text = NULL;
2796 unsigned long where = 0;
2797
2798 if (!tPtr)
2799 return NULL;
2800
2801 if (!(tb = tPtr->firstTextBlock))
2802 return NULL;
2803
2804 if (tPtr->writer) {
2805 (tPtr->writer) (tPtr, (void *)text);
2806 return text;
2807 }
2808
2809 tb = tPtr->firstTextBlock;
2810 while (tb) {
2811
2812 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2813
2814 if (!sel || (tb->graphic && tb->selected)) {
2815
2816 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2817 && tb != tPtr->firstTextBlock) {
2818 text = wrealloc(text, where + 1);
2819 text[where++] = '\n';
2820 }
2821
2822 if (tb->blank)
2823 goto _gSnext;
2824
2825 if (tb->graphic && array) {
2826 text = wrealloc(text, where + 4);
2827 text[where++] = 0xFA;
2828 text[where++] = (tb->used >> 8) & 0x0ff;
2829 text[where++] = tb->used & 0x0ff;
2830 text[where++] = tb->allocated; /* extra info */
2831 }
2832 text = wrealloc(text, where + tb->used);
2833 memcpy(&text[where], tb->text, tb->used);
2834 where += tb->used;
2835
2836 } else if (sel && tb->selected) {
2837
2838 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2839 text = wrealloc(text, where + 1);
2840 text[where++] = '\n';
2841 }
2842
2843 if (tb->blank)
2844 goto _gSnext;
2845
2846 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2847 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2848 where += tb->s_end - tb->s_begin;
2849
2850 }
2851
2852 }
2853 _gSnext: tb = tb->next;
2854 }
2855
2856 /* +1 for the end of string, let's be nice */
2857 text = wrealloc(text, where + 1);
2858 text[where] = 0;
2859 return text;
2860 }
2861
2862 static void releaseStreamObjects(void *data)
2863 {
2864 if (data)
2865 wfree(data);
2866 }
2867
2868 WMArray *getStreamObjects(WMText * tPtr, int sel)
2869 {
2870 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2871 WMData *data;
2872 char *stream;
2873 unsigned short len;
2874 char *start, *fa, *desc;
2875
2876 stream = getStream(tPtr, sel, 1);
2877 if (!stream)
2878 return NULL;
2879
2880 start = stream;
2881 while (start) {
2882
2883 fa = strchr(start, 0xFA);
2884 if (fa) {
2885 if ((int)(fa - start) > 0) {
2886 desc = start;
2887 desc[(int)(fa - start)] = 0;
2888 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2889 WMSetDataFormat(data, TYPETEXT);
2890 WMAddToArray(array, (void *)data);
2891 }
2892
2893 len = *(fa + 1) * 0xff + *(fa + 2);
2894 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2895 WMSetDataFormat(data, *(fa + 3));
2896 WMAddToArray(array, (void *)data);
2897 start = fa + len + 4;
2898
2899 } else {
2900 if (start && strlen(start)) {
2901 data = WMCreateDataWithBytes((void *)start, strlen(start));
2902 WMSetDataFormat(data, TYPETEXT);
2903 WMAddToArray(array, (void *)data);
2904 }
2905 start = fa;
2906 }
2907 }
2908
2909 wfree(stream);
2910 return array;
2911 }
2912
2913 #define XDND_TEXT_DATA_TYPE "text/plain"
2914 #define XDND_COLOR_DATA_TYPE "application/X-color"
2915 static WMArray *getXdndSourceTypeArray()
2916 {
2917 WMArray *types = WMCreateArray(1);
2918 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2919 return types;
2920 }
2921
2922 static WMArray *getXdndDestinationTypeArray()
2923 {
2924 WMArray *types = WMCreateArray(1);
2925 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2926 return types;
2927 }
2928
2929 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2930 {
2931 Text *tPtr;
2932 Display *dpy;
2933 WMScreen *scr;
2934 XGCValues gcv;
2935
2936 tPtr = wmalloc(sizeof(Text));
2937 memset(tPtr, 0, sizeof(Text));
2938 tPtr->widgetClass = WC_Text;
2939 tPtr->view = W_CreateView(W_VIEW(parent));
2940 if (!tPtr->view) {
2941 perror("could not create text's view\n");
2942 wfree(tPtr);
2943 return NULL;
2944 }
2945
2946 dpy = tPtr->view->screen->display;
2947 scr = tPtr->view->screen;
2948
2949 tPtr->view->self = tPtr;
2950 tPtr->view->attribs.cursor = scr->textCursor;
2951 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2952 W_ResizeView(tPtr->view, 250, 200);
2953
2954 tPtr->dColor = WMBlackColor(scr);
2955 tPtr->fgColor = WMBlackColor(scr);
2956 tPtr->bgColor = WMWhiteColor(scr);
2957 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2958
2959 gcv.graphics_exposures = False;
2960 gcv.foreground = W_PIXEL(scr->gray);
2961 gcv.background = W_PIXEL(scr->darkGray);
2962 gcv.fill_style = FillStippled;
2963 /* why not use scr->stipple here? */
2964 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2965 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2966 GCForeground | GCBackground | GCStipple
2967 | GCFillStyle | GCGraphicsExposures, &gcv);
2968
2969 tPtr->ruler = NULL;
2970 tPtr->vS = NULL;
2971 tPtr->hS = NULL;
2972
2973 tPtr->dFont = WMSystemFontOfSize(scr, 12);
2974
2975 tPtr->view->delegate = &_TextViewDelegate;
2976
2977 tPtr->delegate = NULL;
2978
2979 #if DO_BLINK
2980 tPtr->timerID = NULL;
2981 #endif
2982
2983 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
2984 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
2985
2986 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
2987 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
2988
2989 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
2990
2991 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
2992 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2993
2994 {
2995 WMArray *types = WMCreateArray(2);
2996 WMAddToArray(types, "application/X-color");
2997 WMAddToArray(types, "application/X-image");
2998 WMRegisterViewForDraggedTypes(tPtr->view, types);
2999 }
3000
3001 /*WMAddNotificationObserver(fontChanged, tPtr,
3002 WMFontPanelDidChangeNotification, tPtr); */
3003
3004 tPtr->firstTextBlock = NULL;
3005 tPtr->lastTextBlock = NULL;
3006 tPtr->currentTextBlock = NULL;
3007 tPtr->tpos = 0;
3008
3009 tPtr->gfxItems = WMCreateArray(4);
3010
3011 tPtr->parser = parser;
3012 tPtr->writer = writer;
3013
3014 tPtr->sel.x = tPtr->sel.y = 2;
3015 tPtr->sel.w = tPtr->sel.h = 0;
3016
3017 tPtr->clicked.x = tPtr->clicked.y = 2;
3018
3019 tPtr->visible.x = tPtr->visible.y = 2;
3020 tPtr->visible.h = tPtr->view->size.height;
3021 tPtr->visible.w = tPtr->view->size.width - 4;
3022
3023 tPtr->cursor.x = -23;
3024
3025 tPtr->docWidth = 0;
3026 tPtr->docHeight = 0;
3027 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3028 tPtr->db = (Pixmap) NULL;
3029 tPtr->bgPixmap = NULL;
3030
3031 tPtr->margins = WMGetRulerMargins(NULL);
3032 tPtr->margins->right = tPtr->visible.w;
3033 tPtr->nMargins = 1;
3034
3035 tPtr->flags.rulerShown = False;
3036 tPtr->flags.monoFont = False;
3037 tPtr->flags.focused = False;
3038 tPtr->flags.editable = True;
3039 tPtr->flags.ownsSelection = False;
3040 tPtr->flags.pointerGrabbed = False;
3041 tPtr->flags.extendSelection = False;
3042 tPtr->flags.frozen = False;
3043 tPtr->flags.cursorShown = True;
3044 tPtr->flags.acceptsGraphic = False;
3045 tPtr->flags.horizOnDemand = False;
3046 tPtr->flags.needsLayOut = False;
3047 tPtr->flags.ignoreNewLine = False;
3048 tPtr->flags.indentNewLine = False;
3049 tPtr->flags.laidOut = False;
3050 tPtr->flags.ownsSelection = False;
3051 tPtr->flags.waitingForSelection = False;
3052 tPtr->flags.prepend = False;
3053 tPtr->flags.isOverGraphic = False;
3054 tPtr->flags.relief = WRSunken;
3055 tPtr->flags.isOverGraphic = 0;
3056 tPtr->flags.alignment = WALeft;
3057 tPtr->flags.first = True;
3058
3059 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3060 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3061
3062 return tPtr;
3063 }
3064
3065 void WMPrependTextStream(WMText * tPtr, char *text)
3066 {
3067 CHECK_CLASS(tPtr, WC_Text);
3068
3069 if (!text) {
3070 if (tPtr->flags.ownsSelection)
3071 releaseSelection(tPtr);
3072 clearText(tPtr);
3073 updateScrollers(tPtr);
3074 return;
3075 }
3076
3077 tPtr->flags.prepend = True;
3078 if (text && tPtr->parser)
3079 (tPtr->parser) (tPtr, (void *)text);
3080 else
3081 insertPlainText(tPtr, text);
3082
3083 tPtr->flags.needsLayOut = True;
3084 tPtr->tpos = 0;
3085 if (!tPtr->flags.frozen) {
3086 layOutDocument(tPtr);
3087 }
3088 }
3089
3090 void WMAppendTextStream(WMText * tPtr, char *text)
3091 {
3092 CHECK_CLASS(tPtr, WC_Text);
3093
3094 if (!text) {
3095 if (tPtr->flags.ownsSelection)
3096 releaseSelection(tPtr);
3097 clearText(tPtr);
3098 updateScrollers(tPtr);
3099 return;
3100 }
3101
3102 tPtr->flags.prepend = False;
3103 if (text && tPtr->parser)
3104 (tPtr->parser) (tPtr, (void *)text);
3105 else
3106 insertPlainText(tPtr, text);
3107
3108 tPtr->flags.needsLayOut = True;
3109 if (tPtr->currentTextBlock) {
3110 if (tPtr->currentTextBlock->graphic)
3111 tPtr->tpos = 1;
3112 else
3113 tPtr->tpos = tPtr->currentTextBlock->used;
3114 }
3115
3116 if (!tPtr->flags.frozen) {
3117 layOutDocument(tPtr);
3118 }
3119 }
3120
3121 char *WMGetTextStream(WMText * tPtr)
3122 {
3123 CHECK_CLASS(tPtr, WC_Text);
3124
3125 return getStream(tPtr, 0, 0);
3126 }
3127
3128 char *WMGetTextSelectedStream(WMText * tPtr)
3129 {
3130 CHECK_CLASS(tPtr, WC_Text);
3131
3132 return getStream(tPtr, 1, 0);
3133 }
3134
3135 WMArray *WMGetTextObjects(WMText * tPtr)
3136 {
3137 CHECK_CLASS(tPtr, WC_Text);
3138
3139 return getStreamObjects(tPtr, 0);
3140 }
3141
3142 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3143 {
3144 CHECK_CLASS(tPtr, WC_Text);
3145
3146 return getStreamObjects(tPtr, 1);
3147 }
3148
3149 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3150 {
3151 CHECK_CLASS(tPtr, WC_Text);
3152
3153 tPtr->delegate = delegate;
3154 }
3155
3156 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3157 char *description, WMColor * color,
3158 unsigned short first, unsigned short extraInfo)
3159 {
3160 TextBlock *tb;
3161
3162 if (!w || !description || !color)
3163 return NULL;
3164
3165 tb = wmalloc(sizeof(TextBlock));
3166
3167 tb->text = wstrdup(description);
3168 tb->used = strlen(description);
3169 tb->blank = False;
3170 tb->d.widget = w;
3171 tb->color = WMRetainColor(color);
3172 tb->marginN = newMargin(tPtr, NULL);
3173 tb->allocated = extraInfo;
3174 tb->first = first;
3175 tb->kanji = False;
3176 tb->graphic = True;
3177 tb->object = True;
3178 tb->underlined = False;
3179 tb->selected = False;
3180 tb->script = 0;
3181 tb->sections = NULL;
3182 tb->nsections = 0;
3183 tb->prior = NULL;
3184 tb->next = NULL;
3185
3186 return tb;
3187 }
3188
3189 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3190 char *description, WMColor * color,
3191 unsigned short first, unsigned short extraInfo)
3192 {
3193 TextBlock *tb;
3194
3195 if (!p || !description || !color)
3196 return NULL;
3197
3198 tb = wmalloc(sizeof(TextBlock));
3199
3200 tb->text = wstrdup(description);
3201 tb->used = strlen(description);
3202 tb->blank = False;
3203 tb->d.pixmap = WMRetainPixmap(p);
3204 tb->color = WMRetainColor(color);
3205 tb->marginN = newMargin(tPtr, NULL);
3206 tb->allocated = extraInfo;
3207 tb->first = first;
3208 tb->kanji = False;
3209 tb->graphic = True;
3210 tb->object = False;
3211 tb->underlined = False;
3212 tb->selected = False;
3213 tb->script = 0;
3214 tb->sections = NULL;
3215 tb->nsections = 0;
3216 tb->prior = NULL;
3217 tb->next = NULL;
3218
3219 return tb;
3220 }
3221
3222 void *WMCreateTextBlockWithText(WMText * tPtr, char *text, WMFont * font, WMColor * color,
3223 unsigned short first, unsigned short len)
3224 {
3225 TextBlock *tb;
3226
3227 if (!font || !color)
3228 return NULL;
3229
3230 tb = wmalloc(sizeof(TextBlock));
3231
3232 tb->allocated = reqBlockSize(len);
3233 tb->text = (char *)wmalloc(tb->allocated);
3234 memset(tb->text, 0, tb->allocated);
3235
3236 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3237 *tb->text = ' ';
3238 tb->used = 1;
3239 tb->blank = True;
3240 } else {
3241 memcpy(tb->text, text, len);
3242 tb->used = len;
3243 tb->blank = False;
3244 }
3245 tb->text[tb->used] = 0;
3246
3247 tb->d.font = WMRetainFont(font);
3248 tb->color = WMRetainColor(color);
3249 tb->marginN = newMargin(tPtr, NULL);
3250 tb->first = first;
3251 tb->kanji = False;
3252 tb->graphic = False;
3253 tb->underlined = False;
3254 tb->selected = False;
3255 tb->script = 0;
3256 tb->sections = NULL;
3257 tb->nsections = 0;
3258 tb->prior = NULL;
3259 tb->next = NULL;
3260 return tb;
3261 }
3262
3263 void
3264 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3265 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3266 {
3267 TextBlock *tb = (TextBlock *) vtb;
3268 if (!tb)
3269 return;
3270
3271 tb->first = first;
3272 tb->kanji = kanji;
3273 tb->underlined = underlined;
3274 tb->script = script;
3275 tb->marginN = newMargin(tPtr, margins);
3276 }
3277
3278 void
3279 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3280 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins * margins)
3281 {
3282 TextBlock *tb = (TextBlock *) vtb;
3283 if (!tb)
3284 return;
3285
3286 if (first)
3287 *first = tb->first;
3288 if (kanji)
3289 *kanji = tb->kanji;
3290 if (underlined)
3291 *underlined = tb->underlined;
3292 if (script)
3293 *script = tb->script;
3294 if (margins)
3295 margins = &tPtr->margins[tb->marginN];
3296 }
3297
3298 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3299 {
3300 TextBlock *tb = (TextBlock *) vtb;
3301
3302 if (!tb)
3303 return;
3304
3305 if (tb->graphic) {
3306 if (tb->object) {
3307 WMWidget *w = tb->d.widget;
3308 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3309 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3310 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3311 }
3312 }
3313 WMAddToArray(tPtr->gfxItems, (void *)tb);
3314 tPtr->tpos = 1;
3315
3316 } else {
3317 tPtr->tpos = tb->used;
3318 }
3319
3320 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3321 tb->next = tb->prior = NULL;
3322 tb->first = True;
3323 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3324 return;
3325 }
3326
3327 if (!tb->first) {
3328 tb->marginN = tPtr->currentTextBlock->marginN;
3329 }
3330
3331 tb->next = tPtr->currentTextBlock;
3332 tb->prior = tPtr->currentTextBlock->prior;
3333 if (tPtr->currentTextBlock->prior)
3334 tPtr->currentTextBlock->prior->next = tb;
3335
3336 tPtr->currentTextBlock->prior = tb;
3337 if (!tb->prior)
3338 tPtr->firstTextBlock = tb;
3339
3340 tPtr->currentTextBlock = tb;
3341 }
3342
3343 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3344 {
3345 TextBlock *tb = (TextBlock *) vtb;
3346
3347 if (!tb)
3348 return;
3349
3350 if (tb->graphic) {
3351 if (tb->object) {
3352 WMWidget *w = tb->d.widget;
3353 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3354 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3355 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3356 }
3357 }
3358 WMAddToArray(tPtr->gfxItems, (void *)tb);
3359 tPtr->tpos = 1;
3360
3361 } else {
3362 tPtr->tpos = tb->used;
3363 }
3364
3365 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3366 tb->next = tb->prior = NULL;
3367 tb->first = True;
3368 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3369 return;
3370 }
3371
3372 if (!tb->first) {
3373 tb->marginN = tPtr->currentTextBlock->marginN;
3374 }
3375
3376 tb->next = tPtr->currentTextBlock->next;
3377 tb->prior = tPtr->currentTextBlock;
3378 if (tPtr->currentTextBlock->next)
3379 tPtr->currentTextBlock->next->prior = tb;
3380
3381 tPtr->currentTextBlock->next = tb;
3382
3383 if (!tb->next)
3384 tPtr->lastTextBlock = tb;
3385
3386 tPtr->currentTextBlock = tb;
3387 }
3388
3389 void *WMRemoveTextBlock(WMText * tPtr)
3390 {
3391 TextBlock *tb = NULL;
3392
3393 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3394 return NULL;
3395 }
3396
3397 tb = tPtr->currentTextBlock;
3398 if (tb->graphic) {
3399 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3400
3401 if (tb->object) {
3402 WMUnmapWidget(tb->d.widget);
3403 }
3404 }
3405
3406 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3407 if (tPtr->currentTextBlock->next)
3408 tPtr->currentTextBlock->next->prior = NULL;
3409
3410 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3411 tPtr->currentTextBlock = tPtr->firstTextBlock;
3412
3413 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3414 tPtr->currentTextBlock->prior->next = NULL;
3415 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3416 tPtr->currentTextBlock = tPtr->lastTextBlock;
3417 } else {
3418 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3419 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3420 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3421 }
3422
3423 return (void *)tb;
3424 }
3425
3426 #if 0
3427 static void destroyWidget(WMWidget * widget)
3428 {
3429 WMDestroyWidget(widget);
3430 // -- never do this -- wfree(widget);
3431 }
3432 #endif
3433
3434 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3435 {
3436 TextBlock *tb = (TextBlock *) vtb;
3437 if (!tb)
3438 return;
3439
3440 if (tb->graphic) {
3441 if (tb->object) {
3442 /* naturally, there's a danger to destroying widgets whose action
3443 * brings us here: ie. press a button to destroy it...
3444 * need to find a safer way. till then... this stays commented out */
3445 /* 5 months later... destroy it 10 seconds after now which should
3446 * be enough time for the widget's action to be completed... :-) */
3447 /* This is a bad assumption. Just destroy the widget here.
3448 * if the caller needs it, it can protect it with W_RetainView()
3449 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3450 WMDestroyWidget(tb->d.widget);
3451 } else {
3452 WMReleasePixmap(tb->d.pixmap);
3453 }
3454 } else {
3455 WMReleaseFont(tb->d.font);
3456 }
3457
3458 WMReleaseColor(tb->color);
3459 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3460 if (tb->sections)
3461 wfree(tb->sections);
3462 wfree(tb->text);
3463 wfree(tb);
3464 }
3465
3466 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3467 {
3468 if (tPtr->fgColor)
3469 WMReleaseColor(tPtr->fgColor);
3470
3471 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3472
3473 paintText(tPtr);
3474 }
3475
3476 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3477 {
3478 if (tPtr->bgColor)
3479 WMReleaseColor(tPtr->bgColor);
3480
3481 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3482 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3483
3484 paintText(tPtr);
3485 }
3486
3487 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3488 {
3489 if (tPtr->bgPixmap)
3490 WMReleasePixmap(tPtr->bgPixmap);
3491
3492 if (pixmap)
3493 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3494 else
3495 tPtr->bgPixmap = NULL;
3496 }
3497
3498 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3499 {
3500 tPtr->flags.relief = relief;
3501 textDidResize(tPtr->view->delegate, tPtr->view);
3502 }
3503
3504 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3505 {
3506 if (shouldhave && !tPtr->hS) {
3507 tPtr->hS = WMCreateScroller(tPtr);
3508 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3509 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3510 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3511 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3512 WMMapWidget(tPtr->hS);
3513 } else if (!shouldhave && tPtr->hS) {
3514 WMUnmapWidget(tPtr->hS);
3515 WMDestroyWidget(tPtr->hS);
3516 tPtr->hS = NULL;
3517 }
3518
3519 tPtr->hpos = 0;
3520 tPtr->prevHpos = 0;
3521 textDidResize(tPtr->view->delegate, tPtr->view);
3522 }
3523
3524 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3525 {
3526 if (shouldhave && !tPtr->ruler) {
3527 tPtr->ruler = WMCreateRuler(tPtr);
3528 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3529 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3530 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3531 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3532 } else if (!shouldhave && tPtr->ruler) {
3533 WMShowTextRuler(tPtr, False);
3534 WMDestroyWidget(tPtr->ruler);
3535 tPtr->ruler = NULL;
3536 }
3537 textDidResize(tPtr->view->delegate, tPtr->view);
3538 }
3539
3540 void WMShowTextRuler(WMText * tPtr, Bool show)
3541 {
3542 if (!tPtr->ruler)
3543 return;
3544
3545 if (tPtr->flags.monoFont)
3546 show = False;
3547
3548 tPtr->flags.rulerShown = show;
3549 if (show) {
3550 WMMapWidget(tPtr->ruler);
3551 } else {
3552 WMUnmapWidget(tPtr->ruler);
3553 }
3554
3555 textDidResize(tPtr->view->delegate, tPtr->view);
3556 }
3557
3558 Bool WMGetTextRulerShown(WMText * tPtr)
3559 {
3560 if (!tPtr->ruler)
3561 return False;
3562
3563 return tPtr->flags.rulerShown;
3564 }
3565
3566 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3567 {
3568 if (shouldhave && !tPtr->vS) {
3569 tPtr->vS = WMCreateScroller(tPtr);
3570 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3571 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3572 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3573 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3574 WMMapWidget(tPtr->vS);
3575 } else if (!shouldhave && tPtr->vS) {
3576 WMUnmapWidget(tPtr->vS);
3577 WMDestroyWidget(tPtr->vS);
3578 tPtr->vS = NULL;
3579 }
3580
3581 tPtr->vpos = 0;
3582 tPtr->prevVpos = 0;
3583 textDidResize(tPtr->view->delegate, tPtr->view);
3584 }
3585
3586 Bool WMScrollText(WMText * tPtr, int amount)
3587 {
3588 Bool scroll = False;
3589
3590 if (amount == 0 || !tPtr->view->flags.realized)
3591 return False;
3592
3593 if (amount < 0) {
3594 if (tPtr->vpos > 0) {
3595 if (tPtr->vpos > abs(amount))
3596 tPtr->vpos += amount;
3597 else
3598 tPtr->vpos = 0;
3599 scroll = True;
3600 }
3601 } else {
3602 int limit = tPtr->docHeight - tPtr->visible.h;
3603 if (tPtr->vpos < limit) {
3604 if (tPtr->vpos < limit - amount)
3605 tPtr->vpos += amount;
3606 else
3607 tPtr->vpos = limit;
3608 scroll = True;
3609 }
3610 }
3611
3612 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3613 updateScrollers(tPtr);
3614 paintText(tPtr);
3615 }
3616 tPtr->prevVpos = tPtr->vpos;
3617 return scroll;
3618 }
3619
3620 Bool WMPageText(WMText * tPtr, Bool direction)
3621 {
3622 if (!tPtr->view->flags.realized)
3623 return False;
3624
3625 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3626 }
3627
3628 void WMSetTextEditable(WMText * tPtr, Bool editable)
3629 {
3630 tPtr->flags.editable = editable;
3631 }
3632
3633 int WMGetTextEditable(WMText * tPtr)
3634 {
3635 return tPtr->flags.editable;
3636 }
3637
3638 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3639 {
3640 tPtr->flags.indentNewLine = indent;
3641 }
3642
3643 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3644 {
3645 tPtr->flags.ignoreNewLine = ignore;
3646 }
3647
3648 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3649 {
3650 return tPtr->flags.ignoreNewLine;