WINGs: fix possible NULL pointer dereference (Coverity #50197)
[wmaker-crm.git] / WINGs / wtext.c
blobc112fbe8570481b582d7a293e4be516a85444135
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
4 #include "WINGsP.h"
5 #include <ctype.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 #define DO_BLINK 0
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..
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;
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
45 typedef struct _TextBlock {
46 struct _TextBlock *next; /* next text block in linked list */
47 struct _TextBlock *prior; /* prior text block in linked list */
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 */
57 unsigned short used; /* number of chars in this block */
58 unsigned short allocated; /* size of allocation (in chars) */
59 WMColor *color; /* the color */
61 Section *sections; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
64 unsigned short s_begin; /* where the selection begins */
65 unsigned short s_end; /* where it ends */
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;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
82 typedef struct {
83 int y, x, h, w;
84 } myRect;
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 */
90 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
92 WMScroller *vS; /* the vertical scroller */
93 unsigned int vpos; /* the current vertical position */
94 unsigned int prevVpos; /* the previous vertical position */
96 WMScroller *hS; /* the horizontal scroller */
97 unsigned int hpos; /* the current horizontal position */
98 unsigned int prevHpos; /* the previous horizontal position */
100 WMFont *dFont; /* the default font */
101 WMColor *dColor; /* the default color */
102 WMPixmap *dBulletPix; /* the default pixmap for bullets */
104 WMColor *fgColor; /* The current foreground color */
105 WMColor *bgColor; /* The background color */
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 */
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 */
115 WMPoint clicked; /* where in the _document_ was clicked */
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 */
121 TextBlock *firstTextBlock;
122 TextBlock *lastTextBlock;
123 TextBlock *currentTextBlock;
125 WMArray *gfxItems; /* a nice array of graphic items */
127 #if DO_BLINK
128 WMHandlerID timerID; /* for nice twinky-winky */
129 #endif
131 WMAction *parser;
132 WMAction *writer;
133 WMTextDelegate *delegate;
134 Time lastClickTime;
136 WMRulerMargins *margins; /* an array of margins */
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 */
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;
165 WMArray *xdndSourceTypes;
166 WMArray *xdndDestinationTypes;
167 } Text;
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);}
176 #define TYPETEXT 0
178 #if 0
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr, int len)
182 char *s;
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);
191 #endif
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
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 "
218 static void handleEvents(XEvent * event, void *data);
219 static void layOutDocument(Text * tPtr);
220 static void updateScrollers(Text * tPtr);
222 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
224 unsigned int i = 0;
226 for (i = 0; i < tPtr->nMargins; i++) {
228 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
229 return i;
232 return -1;
235 static int newMargin(Text * tPtr, WMRulerMargins * margins)
237 int n;
239 if (!margins) {
240 tPtr->margins[0].retainCount++;
241 return 0;
244 n = getMarginNumber(tPtr, margins);
246 if (n == -1) {
248 if (tPtr->nMargins >= 127) {
249 n = tPtr->nMargins - 1;
250 return n;
253 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
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++;
266 return n;
269 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
271 unsigned short i, w, lw, selected = False, extend = False;
272 myRect sel;
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;
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;
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)) {
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;
299 sel.x = 0;
300 selected = True;
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;
311 if (selected) {
312 selected = False;
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;
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;
324 } else {
326 i = tb->sections[s].begin;
327 lw = 0;
329 if (0 && tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
334 while (++i <= tb->sections[s].end) {
336 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
337 lw += w;
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;
347 if (i > tb->sections[s].end) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
349 return False;
352 _selEnd: rect->x = tb->sections[s].x + lw;
353 lw = 0;
354 while (++i <= tb->sections[s].end) {
356 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
357 lw += w;
359 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
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);
372 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
373 selected = True;
374 break;
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);
387 return selected;
391 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
393 TextBlock *tb;
394 int isFont = False;
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
400 if (font && (!color || underlined == -1))
401 isFont = True;
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
409 if (isFont) {
410 if (!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
414 } else if (underlined != -1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
423 TextBlock *midtb, *otb = tb;
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));
441 if (midtb) {
442 if (underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
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;
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);
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
475 otb->selected = False;
476 otb->used = otb->s_begin;
480 tb = tb->next;
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
486 /* in case the size changed... */
487 if (isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
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;
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
502 static Bool removeSelection(Text * tPtr)
504 TextBlock *tb = NULL;
505 Bool first = False;
507 if (!(tb = tPtr->firstTextBlock))
508 return False;
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;
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;
530 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
531 tb = tPtr->currentTextBlock;
532 continue;
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;
543 tb = tb->next;
545 return True;
548 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
550 TextBlock *hold = tb;
552 if (!tb)
553 return NULL;
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir ? tb->next : tb->prior);
561 if (!tb) {
562 tb = hold;
563 while (tb) {
564 if (!tb->graphic)
565 break;
566 tb = (dir ? tb->prior : tb->next);
570 if (!tb)
571 return NULL;
573 return tb;
576 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
578 if (tPtr->flags.monoFont && tb->graphic) {
579 tb = getFirstNonGraphicBlockFor(tb, *dir);
580 if (!tb)
581 return 0;
583 if (tb->graphic) {
584 tPtr->currentTextBlock = (*dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
590 if (!tb->sections) {
591 layOutDocument(tPtr);
592 return 0;
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;
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;
612 return 1;
615 static void paintText(Text * tPtr)
617 TextBlock *tb;
618 WMFont *font;
619 const char *text;
620 int len, y, c, s, done = False, 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;
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
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);
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
643 done = False;
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;
651 while (tb) {
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
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;
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;
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for (s = 0; s < tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
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);
715 len = tb->sections[s].end - tb->sections[s].begin;
716 text = &(tb->text[tb->sections[s].begin]);
717 y = tb->sections[s].y - tPtr->vpos;
718 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
720 if (!tPtr->flags.monoFont && tb->underlined) {
721 XDrawLine(dpy, tPtr->db, WMColorGC(color),
722 tb->sections[s].x - tPtr->hpos,
723 y + font->y + 1,
724 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
727 tb = (!done ? tb->next : NULL);
730 /* now , show all graphic items that can be viewed */
731 c = WMGetArrayItemCount(tPtr->gfxItems);
732 if (c > 0 && !tPtr->flags.monoFont) {
733 int j, h;
735 for (j = 0; j < c; j++) {
736 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
738 /* if it's not viewable, and mapped, unmap it */
739 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
740 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
742 if (tb->object) {
743 if ((W_VIEW(tb->d.widget))->flags.mapped) {
744 WMUnmapWidget(tb->d.widget);
747 } else {
748 /* if it's viewable, and not mapped, map it */
749 if (tb->object) {
750 W_View *view = W_VIEW(tb->d.widget);
752 if (!view->flags.realized)
753 WMRealizeWidget(tb->d.widget);
754 if (!view->flags.mapped) {
755 XMapWindow(view->screen->display, view->window);
756 XFlush(view->screen->display);
757 view->flags.mapped = 1;
761 if (tb->object) {
762 WMMoveWidget(tb->d.widget,
763 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
764 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
765 h = WMWidgetHeight(tb->d.widget) + 1;
767 } else {
768 WMDrawPixmap(tb->d.pixmap, tPtr->db,
769 tb->sections[0].x - tPtr->hpos,
770 tb->sections[0].y - tPtr->vpos);
771 h = tb->d.pixmap->height + 1;
775 if (tPtr->flags.ownsSelection) {
776 XRectangle rect;
778 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
779 Drawable d = (0 && tb->object ?
780 (WMWidgetView(tb->d.widget))->window : tPtr->db);
782 tb->selected = True;
783 XFillRectangle(dpy, d, tPtr->stippledGC,
784 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
785 rect.x, rect.y, rect.width, rect.height);
789 if (!tPtr->flags.monoFont && tb->underlined) {
790 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
791 tb->sections[0].x - tPtr->hpos,
792 tb->sections[0].y + h - tPtr->vpos,
793 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos);
800 _copy_area:
801 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
802 int y = tPtr->cursor.y - tPtr->vpos;
803 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
804 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
807 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
808 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
810 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
812 if (tPtr->ruler && tPtr->flags.rulerShown)
813 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
817 static void mouseOverObject(Text * tPtr, int x, int y)
819 TextBlock *tb;
820 Bool result = False;
822 x -= tPtr->visible.x;
823 x += tPtr->hpos;
824 y -= tPtr->visible.y;
825 y += tPtr->vpos;
827 if (tPtr->flags.ownsSelection) {
828 if (tPtr->sel.x <= x
829 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
830 tPtr->flags.isOverGraphic = 1;
831 result = True;
835 if (!result) {
836 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
838 if (c < 1)
839 tPtr->flags.isOverGraphic = 0;
841 for (j = 0; j < c; j++) {
842 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
844 if (!tb || !tb->sections) {
845 tPtr->flags.isOverGraphic = 0;
846 return;
849 if (!tb->object) {
850 if (tb->sections[0].x <= x
851 && tb->sections[0].y <= y
852 && tb->sections[0].x + tb->sections[0].w >= x
853 && tb->sections[0].y + tb->d.pixmap->height >= y) {
854 tPtr->flags.isOverGraphic = 3;
855 result = True;
856 break;
863 if (!result)
864 tPtr->flags.isOverGraphic = 0;
866 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
868 XSetWindowAttributes attribs;
869 attribs.cursor = tPtr->view->attribs.cursor;
870 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
874 #if DO_BLINK
876 static void blinkCursor(void *data)
878 Text *tPtr = (Text *) data;
880 if (tPtr->flags.cursorShown) {
881 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
882 } else {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
885 paintText(tPtr);
886 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
888 #endif
890 static void updateCursorPosition(Text * tPtr)
892 TextBlock *tb = NULL;
893 int x, y, h, s;
895 if (tPtr->flags.needsLayOut)
896 layOutDocument(tPtr);
898 if (!(tb = tPtr->currentTextBlock)) {
899 if (!(tb = tPtr->firstTextBlock)) {
900 WMFont *font = tPtr->dFont;
901 tPtr->tpos = 0;
902 tPtr->cursor.h = font->height + abs(font->height - font->y);
904 tPtr->cursor.y = 2;
905 tPtr->cursor.x = 2;
906 return;
910 if (tb->blank) {
911 tPtr->tpos = 0;
912 y = tb->sections[0].y;
913 h = tb->sections[0].h;
914 x = tb->sections[0].x;
916 } else if (tb->graphic) {
917 y = tb->sections[0].y;
918 h = tb->sections[0].h;
919 x = tb->sections[0].x;
920 if (tPtr->tpos == 1)
921 x += tb->sections[0].w;
923 } else {
924 if (tPtr->tpos > tb->used)
925 tPtr->tpos = tb->used;
927 for (s = 0; s < tb->nsections - 1; s++) {
929 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
930 break;
933 y = tb->sections[s]._y;
934 h = tb->sections[s].h;
935 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
936 &tb->text[tb->sections[s].begin],
937 tPtr->tpos - tb->sections[s].begin);
940 tPtr->cursor.y = y;
941 tPtr->cursor.h = h;
942 tPtr->cursor.x = x;
944 /* scroll the bars if the cursor is not visible */
945 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
946 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
947 tPtr->vpos +=
948 (tPtr->cursor.y + tPtr->cursor.h + 10
949 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
950 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
951 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
956 updateScrollers(tPtr);
959 static void cursorToTextPosition(Text * tPtr, int x, int y)
961 TextBlock *tb = NULL;
962 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
963 const char *text;
965 if (tPtr->flags.needsLayOut)
966 layOutDocument(tPtr);
968 y += (tPtr->vpos - tPtr->visible.y);
969 if (y < 0)
970 y = 0;
972 x -= (tPtr->visible.x - 2);
973 if (x < 0)
974 x = 0;
976 /* clicked is relative to document, not window... */
977 tPtr->clicked.x = x;
978 tPtr->clicked.y = y;
980 if (!(tb = tPtr->currentTextBlock)) {
981 if (!(tb = tPtr->firstTextBlock)) {
982 WMFont *font = tPtr->dFont;
983 tPtr->tpos = 0;
984 tPtr->cursor.h = font->height + abs(font->height - font->y);
985 tPtr->cursor.y = 2;
986 tPtr->cursor.x = 2;
987 return;
991 /* first, which direction? Most likely, newly clicked
992 position will be close to previous */
993 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
994 return;
996 s = (dir ? 0 : tb->nsections - 1);
997 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
998 goto _doneV;
1001 /* get the first (or last) section of the TextBlock that
1002 lies about the vertical click point */
1003 done = False;
1004 while (!done && tb) {
1006 if (tPtr->flags.monoFont && tb->graphic) {
1007 if ((dir ? tb->next : tb->prior))
1008 tb = (dir ? tb->next : tb->prior);
1009 continue;
1012 s = (dir ? 0 : tb->nsections - 1);
1013 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1015 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1016 done = True;
1017 } else {
1018 dir ? s++ : s--;
1022 if (!done) {
1023 if ((dir ? tb->next : tb->prior)) {
1024 tb = (dir ? tb->next : tb->prior);
1025 } else {
1026 break; /* goto _doneH; */
1031 if (s < 0 || s >= tb->nsections) {
1032 s = (dir ? tb->nsections - 1 : 0);
1035 _doneV:
1036 /* we have the line, which TextBlock on that line is it? */
1037 pos = (dir ? 0 : tb->sections[s].begin);
1038 if (tPtr->flags.monoFont && tb->graphic) {
1039 TextBlock *hold = tb;
1040 tb = getFirstNonGraphicBlockFor(hold, dir);
1042 if (!tb) {
1043 tPtr->tpos = 0;
1044 tb = hold;
1045 s = 0;
1046 goto _doNothing;
1050 _y = tb->sections[s]._y;
1052 while (tb) {
1054 if (tPtr->flags.monoFont && tb->graphic) {
1055 tb = (dir ? tb->next : tb->prior);
1056 continue;
1059 if (dir) {
1060 if (tb->graphic) {
1061 if (tb->object)
1062 _w = WMWidgetWidth(tb->d.widget) - 5;
1063 else
1064 _w = tb->d.pixmap->width - 5;
1066 if (tb->sections[0].x + _w >= x)
1067 break;
1068 } else {
1069 text = &(tb->text[tb->sections[s].begin]);
1070 len = tb->sections[s].end - tb->sections[s].begin;
1071 _w = WMWidthOfString(tb->d.font, text, len);
1072 if (tb->sections[s].x + _w >= x)
1073 break;
1076 } else {
1077 if (tb->sections[s].x <= x)
1078 break;
1081 if ((dir ? tb->next : tb->prior)) {
1082 TextBlock *nxt = (dir ? tb->next : tb->prior);
1083 if (tPtr->flags.monoFont && nxt->graphic) {
1084 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1085 if (!nxt) {
1086 pos = (dir ? 0 : tb->sections[s].begin);
1087 tPtr->cursor.x = tb->sections[s].x;
1088 goto _doneH;
1092 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1093 /* this must be the last/first on this line. stop */
1094 pos = (dir ? tb->sections[s].end : 0);
1095 tPtr->cursor.x = tb->sections[s].x;
1096 if (!tb->blank) {
1097 if (tb->graphic) {
1098 if (tb->object)
1099 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1100 else
1101 tPtr->cursor.x += tb->d.pixmap->width;
1102 } else if (pos > tb->sections[s].begin) {
1103 tPtr->cursor.x +=
1104 WMWidthOfString(tb->d.font,
1105 &(tb->text[tb->sections[s].begin]),
1106 pos - tb->sections[s].begin);
1109 goto _doneH;
1113 if ((dir ? tb->next : tb->prior)) {
1114 tb = (dir ? tb->next : tb->prior);
1115 } else {
1116 break;
1119 if (tb)
1120 s = (dir ? 0 : tb->nsections - 1);
1123 /* we have said TextBlock, now where within it? */
1124 if (tb) {
1125 if (tb->graphic) {
1126 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1128 tPtr->cursor.x = tb->sections[0].x;
1130 if (x > tPtr->cursor.x + gw / 2) {
1131 pos = 1;
1132 tPtr->cursor.x += gw;
1133 } else {
1134 printf("first %d\n", tb->first);
1135 if (tb->prior) {
1136 if (tb->prior->graphic)
1137 pos = 1;
1138 else
1139 pos = tb->prior->used;
1140 tb = tb->prior;
1141 } else
1142 pos = 0;
1146 s = 0;
1147 goto _doneH;
1149 } else {
1150 WMFont *f = tb->d.font;
1151 len = tb->sections[s].end - tb->sections[s].begin;
1152 text = &(tb->text[tb->sections[s].begin]);
1154 _w = x - tb->sections[s].x;
1155 pos = 0;
1157 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1158 pos++;
1160 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1162 pos += tb->sections[s].begin;
1166 _doneH:
1167 if (tb->graphic) {
1168 tPtr->tpos = (pos <= 1) ? pos : 0;
1169 } else {
1170 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1172 _doNothing:
1173 if (!tb)
1174 printf("...for this app will surely crash :-)\n");
1176 tPtr->currentTextBlock = tb;
1177 tPtr->cursor.h = tb->sections[s].h;
1178 tPtr->cursor.y = tb->sections[s]._y;
1180 /* scroll the bars if the cursor is not visible */
1181 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1182 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1183 tPtr->vpos +=
1184 (tPtr->cursor.y + tPtr->cursor.h + 10
1185 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1186 updateScrollers(tPtr);
1187 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1188 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1189 updateScrollers(tPtr);
1196 static void updateScrollers(Text * tPtr)
1199 if (tPtr->flags.frozen)
1200 return;
1202 if (tPtr->vS) {
1203 if (tPtr->docHeight <= tPtr->visible.h) {
1204 WMSetScrollerParameters(tPtr->vS, 0, 1);
1205 tPtr->vpos = 0;
1206 } else {
1207 float hmax = (float)(tPtr->docHeight);
1208 WMSetScrollerParameters(tPtr->vS,
1209 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1210 (float)tPtr->visible.h / hmax);
1212 } else
1213 tPtr->vpos = 0;
1215 if (tPtr->hS) {
1216 if (tPtr->docWidth <= tPtr->visible.w) {
1217 WMSetScrollerParameters(tPtr->hS, 0, 1);
1218 tPtr->hpos = 0;
1219 } else {
1220 float wmax = (float)(tPtr->docWidth);
1221 WMSetScrollerParameters(tPtr->hS,
1222 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1223 (float)tPtr->visible.w / wmax);
1225 } else
1226 tPtr->hpos = 0;
1229 static void scrollersCallBack(WMWidget * w, void *self)
1231 Text *tPtr = (Text *) self;
1232 Bool scroll = False;
1233 int which;
1235 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1236 return;
1238 if (w == tPtr->vS) {
1239 int height;
1240 height = tPtr->visible.h;
1242 which = WMGetScrollerHitPart(tPtr->vS);
1243 switch (which) {
1245 case WSDecrementLine:
1246 if (tPtr->vpos > 0) {
1247 if (tPtr->vpos > 16)
1248 tPtr->vpos -= 16;
1249 else
1250 tPtr->vpos = 0;
1252 break;
1254 case WSIncrementLine:{
1255 int limit = tPtr->docHeight - height;
1256 if (tPtr->vpos < limit) {
1257 if (tPtr->vpos < limit - 16)
1258 tPtr->vpos += 16;
1259 else
1260 tPtr->vpos = limit;
1263 break;
1265 case WSDecrementPage:
1266 if (((int)tPtr->vpos - (int)height) >= 0)
1267 tPtr->vpos -= height;
1268 else
1269 tPtr->vpos = 0;
1270 break;
1272 case WSIncrementPage:
1273 tPtr->vpos += height;
1274 if (tPtr->vpos > (tPtr->docHeight - height))
1275 tPtr->vpos = tPtr->docHeight - height;
1276 break;
1278 case WSKnob:
1279 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1280 * (float)(tPtr->docHeight - height);
1281 break;
1283 case WSKnobSlot:
1284 case WSNoPart:
1285 break;
1287 scroll = (tPtr->vpos != tPtr->prevVpos);
1288 tPtr->prevVpos = tPtr->vpos;
1291 if (w == tPtr->hS) {
1292 int width = tPtr->visible.w;
1294 which = WMGetScrollerHitPart(tPtr->hS);
1295 switch (which) {
1297 case WSDecrementLine:
1298 if (tPtr->hpos > 0) {
1299 if (tPtr->hpos > 16)
1300 tPtr->hpos -= 16;
1301 else
1302 tPtr->hpos = 0;
1304 break;
1306 case WSIncrementLine:{
1307 int limit = tPtr->docWidth - width;
1308 if (tPtr->hpos < limit) {
1309 if (tPtr->hpos < limit - 16)
1310 tPtr->hpos += 16;
1311 else
1312 tPtr->hpos = limit;
1315 break;
1317 case WSDecrementPage:
1318 if (((int)tPtr->hpos - (int)width) >= 0)
1319 tPtr->hpos -= width;
1320 else
1321 tPtr->hpos = 0;
1322 break;
1324 case WSIncrementPage:
1325 tPtr->hpos += width;
1326 if (tPtr->hpos > (tPtr->docWidth - width))
1327 tPtr->hpos = tPtr->docWidth - width;
1328 break;
1330 case WSKnob:
1331 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1332 * (float)(tPtr->docWidth - width);
1333 break;
1335 case WSKnobSlot:
1336 case WSNoPart:
1337 break;
1339 scroll = (tPtr->hpos != tPtr->prevHpos);
1340 tPtr->prevHpos = tPtr->hpos;
1343 if (scroll) {
1344 updateScrollers(tPtr);
1345 paintText(tPtr);
1349 typedef struct {
1350 TextBlock *tb;
1351 unsigned short begin, end; /* what part of the text block */
1352 } myLineItems;
1354 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1356 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1357 WMFont *font;
1358 const char *text;
1359 TextBlock *tb, *tbsame = NULL;
1361 if (!items || nitems == 0)
1362 return 0;
1364 for (i = 0; i < nitems; i++) {
1365 tb = items[i].tb;
1367 if (tb->graphic) {
1368 if (!tPtr->flags.monoFont) {
1369 if (tb->object) {
1370 WMWidget *wdt = tb->d.widget;
1371 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1372 if (tPtr->flags.alignment != WALeft)
1373 lw += WMWidgetWidth(wdt);
1374 } else {
1375 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1376 if (tPtr->flags.alignment != WALeft)
1377 lw += tb->d.pixmap->width;
1381 } else {
1382 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1383 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1384 max_d = 2;
1385 line_height = WMAX(line_height, font->height + max_d);
1386 text = &(tb->text[items[i].begin]);
1387 len = items[i].end - items[i].begin;
1388 if (tPtr->flags.alignment != WALeft)
1389 lw += WMWidthOfString(font, text, len);
1393 if (tPtr->flags.alignment == WARight) {
1394 j = tPtr->visible.w - lw;
1395 } else if (tPtr->flags.alignment == WACenter) {
1396 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1399 for (i = 0; i < nitems; i++) {
1400 tb = items[i].tb;
1402 if (tbsame == tb) { /* extend it, since it's on same line */
1403 tb->sections[tb->nsections - 1].end = items[i].end;
1404 n = tb->nsections - 1;
1405 } else {
1406 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1407 n = tb->nsections - 1;
1408 tb->sections[n]._y = y + max_d;
1409 tb->sections[n].max_d = max_d;
1410 tb->sections[n].x = x + j;
1411 tb->sections[n].h = line_height;
1412 tb->sections[n].begin = items[i].begin;
1413 tb->sections[n].end = items[i].end;
1416 tb->sections[n].last = (i + 1 == nitems);
1418 if (tb->graphic) {
1419 if (!tPtr->flags.monoFont) {
1420 if (tb->object) {
1421 WMWidget *wdt = tb->d.widget;
1422 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1423 tb->sections[n].w = WMWidgetWidth(wdt);
1424 } else {
1425 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1426 tb->sections[n].w = tb->d.pixmap->width;
1428 x += tb->sections[n].w;
1430 } else {
1431 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1432 len = items[i].end - items[i].begin;
1433 text = &(tb->text[items[i].begin]);
1435 tb->sections[n].y = y + line_height - font->y;
1436 tb->sections[n].w =
1437 WMWidthOfString(font,
1438 &(tb->text[tb->sections[n].begin]),
1439 tb->sections[n].end - tb->sections[n].begin);
1441 x += WMWidthOfString(font, text, len);
1444 tbsame = tb;
1447 return line_height;
1451 static void layOutDocument(Text * tPtr)
1453 TextBlock *tb;
1454 myLineItems *items = NULL;
1455 unsigned int itemsSize = 0, nitems = 0, begin, end;
1456 WMFont *font;
1457 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1458 const char *start = NULL, *mark = NULL;
1460 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1461 return;
1463 assert(tPtr->visible.w > 20);
1465 tPtr->docWidth = tPtr->visible.w;
1466 x = tPtr->margins[tb->marginN].first;
1467 bmargin = tPtr->margins[tb->marginN].body;
1469 /* only partial layOut needed: re-Lay only affected textblocks */
1470 if (tPtr->flags.laidOut) {
1471 tb = tPtr->currentTextBlock;
1473 /* search backwards for textblocks on same line */
1474 while (tb->prior) {
1475 if (!tb->sections || tb->nsections < 1) {
1476 tb = tPtr->firstTextBlock;
1477 tPtr->flags.laidOut = False;
1478 y = 0;
1479 goto _layOut;
1482 if (!tb->prior->sections || tb->prior->nsections < 1) {
1483 tb = tPtr->firstTextBlock;
1484 tPtr->flags.laidOut = False;
1485 y = 0;
1486 goto _layOut;
1489 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1490 break;
1492 tb = tb->prior;
1495 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1496 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1497 tb->prior->sections[tb->prior->nsections - 1].h -
1498 tb->prior->sections[tb->prior->nsections - 1].max_d;
1499 } else {
1500 y = 0;
1504 _layOut:
1505 while (tb) {
1507 if (tb->sections && tb->nsections > 0) {
1508 wfree(tb->sections);
1509 tb->sections = NULL;
1510 tb->nsections = 0;
1513 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1514 TextBlock *next = tb->next;
1515 tPtr->currentTextBlock = tb;
1516 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1517 tb = next;
1518 tb->first = True;
1519 continue;
1522 if (tb->first && tb != tPtr->firstTextBlock) {
1523 y += layOutLine(tPtr, items, nitems, x, y);
1524 x = tPtr->margins[tb->marginN].first;
1525 bmargin = tPtr->margins[tb->marginN].body;
1526 nitems = 0;
1527 lw = 0;
1530 if (tb->graphic) {
1531 if (!tPtr->flags.monoFont) {
1532 if (tb->object)
1533 width = WMWidgetWidth(tb->d.widget);
1534 else
1535 width = tb->d.pixmap->width;
1537 if (width > tPtr->docWidth)
1538 tPtr->docWidth = width;
1540 lw += width;
1541 if (lw >= tPtr->visible.w - x) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 nitems = 0;
1544 x = bmargin;
1545 lw = width;
1548 if (nitems + 1 > itemsSize) {
1549 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1552 items[nitems].tb = tb;
1553 items[nitems].begin = 0;
1554 items[nitems].end = 0;
1555 nitems++;
1558 } else if ((start = tb->text)) {
1559 begin = end = 0;
1560 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1562 while (start) {
1563 mark = strchr(start, ' ');
1564 if (mark) {
1565 end += (int)(mark - start) + 1;
1566 start = mark + 1;
1567 } else {
1568 end += strlen(start);
1569 start = mark;
1572 if (end > tb->used)
1573 end = tb->used;
1575 if (end - begin > 0) {
1577 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1579 /* if it won't fit, char wrap it */
1580 if (width >= tPtr->visible.w) {
1581 char *t = &tb->text[begin];
1582 int l = end - begin, i = 0;
1583 do {
1584 width = WMWidthOfString(font, t, ++i);
1585 } while (width < tPtr->visible.w && i < l);
1586 if (i > 2)
1587 i--;
1588 end = begin + i;
1589 start = &tb->text[end];
1592 lw += width;
1595 if (lw >= tPtr->visible.w - x) {
1596 y += layOutLine(tPtr, items, nitems, x, y);
1597 lw = width;
1598 x = bmargin;
1599 nitems = 0;
1602 if (nitems + 1 > itemsSize) {
1603 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1606 items[nitems].tb = tb;
1607 items[nitems].begin = begin;
1608 items[nitems].end = end;
1609 nitems++;
1611 begin = end;
1615 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1616 if (0 && tPtr->flags.laidOut
1617 && tb->next && tb->next->sections && tb->next->nsections > 0
1618 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1619 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1620 TextBlock *ltb = tPtr->lastTextBlock;
1621 int ly = ltb->sections[ltb->nsections - 1]._y;
1622 int lh = ltb->sections[ltb->nsections - 1].h;
1623 int ss, sd;
1625 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1626 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1628 y += layOutLine(tPtr, items, nitems, x, y);
1629 ss = ly + lh - y;
1630 sd = tPtr->docHeight - y;
1632 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1633 y += tb->next->sections[0]._y - y;
1634 nitems = 0;
1635 printf("nitems%d\n", nitems);
1636 if (ss - sd != 0)
1637 y = tPtr->docHeight + ss - sd;
1639 break;
1640 } else {
1641 tPtr->flags.laidOut = False;
1645 tb = tb->next;
1648 if (nitems > 0)
1649 y += layOutLine(tPtr, items, nitems, x, y);
1651 if (tPtr->docHeight != y + 10) {
1652 tPtr->docHeight = y + 10;
1653 updateScrollers(tPtr);
1656 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1657 XEvent event;
1659 tPtr->flags.horizOnDemand = True;
1660 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1661 event.type = Expose;
1662 handleEvents(&event, (void *)tPtr);
1664 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1665 tPtr->flags.horizOnDemand = False;
1666 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1669 tPtr->flags.laidOut = True;
1671 if (items && itemsSize > 0)
1672 wfree(items);
1676 static void textDidResize(W_ViewDelegate * self, WMView * view)
1678 Text *tPtr = (Text *) view->self;
1679 unsigned short w = tPtr->view->size.width;
1680 unsigned short h = tPtr->view->size.height;
1681 unsigned short rh = 0, vw = 0, rel;
1683 /* Parameter not used, but tell the compiler that it is ok */
1684 (void) self;
1686 rel = (tPtr->flags.relief == WRFlat);
1688 if (tPtr->ruler && tPtr->flags.rulerShown) {
1689 WMMoveWidget(tPtr->ruler, 2, 2);
1690 WMResizeWidget(tPtr->ruler, w - 4, 40);
1691 rh = 40;
1694 if (tPtr->vS) {
1695 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1696 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1697 vw = 20;
1698 WMSetRulerOffset(tPtr->ruler, 22);
1699 } else
1700 WMSetRulerOffset(tPtr->ruler, 2);
1702 if (tPtr->hS) {
1703 if (tPtr->vS) {
1704 WMMoveWidget(tPtr->hS, vw, h - 21);
1705 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1706 } else {
1707 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1708 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1712 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1713 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1714 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1715 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1716 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1717 tPtr->margins[0].right = tPtr->visible.w;
1719 if (tPtr->view->flags.realized) {
1721 if (tPtr->db) {
1722 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1723 tPtr->db = (Pixmap) NULL;
1726 if (tPtr->visible.w < 40)
1727 tPtr->visible.w = 40;
1728 if (tPtr->visible.h < 20)
1729 tPtr->visible.h = 20;
1731 if (!tPtr->db) {
1732 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1733 tPtr->view->window, tPtr->visible.w,
1734 tPtr->visible.h, tPtr->view->screen->depth);
1738 WMThawText(tPtr);
1741 W_ViewDelegate _TextViewDelegate = {
1742 NULL,
1743 NULL,
1744 textDidResize,
1745 NULL,
1746 NULL
1749 #define TEXT_BUFFER_INCR 8
1750 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1752 static void clearText(Text * tPtr)
1754 tPtr->vpos = tPtr->hpos = 0;
1755 tPtr->docHeight = tPtr->docWidth = 0;
1756 tPtr->cursor.x = -23;
1758 if (!tPtr->firstTextBlock)
1759 return;
1761 while (tPtr->currentTextBlock)
1762 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1764 tPtr->firstTextBlock = NULL;
1765 tPtr->currentTextBlock = NULL;
1766 tPtr->lastTextBlock = NULL;
1767 WMEmptyArray(tPtr->gfxItems);
1770 /* possibly remove a single character from the currentTextBlock,
1771 or if there's a selection, remove it...
1772 note that Delete and Backspace are treated differently */
1773 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1775 TextBlock *tb;
1776 Bool back = (Bool) (ksym == XK_BackSpace);
1777 Bool done = 1, wasFirst = 0;
1779 if (!tPtr->flags.editable)
1780 return;
1782 if (!(tb = tPtr->currentTextBlock))
1783 return;
1785 if (tPtr->flags.ownsSelection) {
1786 if (removeSelection(tPtr))
1787 layOutDocument(tPtr);
1788 return;
1791 wasFirst = tb->first;
1792 if (back && tPtr->tpos < 1) {
1793 if (tb->prior) {
1794 if (tb->prior->blank) {
1795 tPtr->currentTextBlock = tb->prior;
1796 WMRemoveTextBlock(tPtr);
1797 tPtr->currentTextBlock = tb;
1798 tb->first = True;
1799 layOutDocument(tPtr);
1800 return;
1801 } else {
1802 if (tb->blank) {
1803 TextBlock *prior = tb->prior;
1804 tPtr->currentTextBlock = tb;
1805 WMRemoveTextBlock(tPtr);
1806 tb = prior;
1807 } else {
1808 tb = tb->prior;
1811 if (tb->graphic)
1812 tPtr->tpos = 1;
1813 else
1814 tPtr->tpos = tb->used;
1816 tPtr->currentTextBlock = tb;
1817 done = 1;
1818 if (wasFirst) {
1819 if (tb->next)
1820 tb->next->first = False;
1821 layOutDocument(tPtr);
1822 return;
1828 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1829 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1830 if (back)
1831 tPtr->tpos--;
1832 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1833 tb->used--;
1834 done = 0;
1837 /* if there are no characters left to back over in the textblock,
1838 but it still has characters to the right of the cursor: */
1839 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1840 || tb->graphic) {
1842 /* no more chars, and it's marked as blank? */
1843 if (tb->blank) {
1844 TextBlock *sibling = (back ? tb->prior : tb->next);
1846 if (tb->used == 0 || tb->graphic)
1847 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1849 if (sibling) {
1850 tPtr->currentTextBlock = sibling;
1851 if (tb->graphic)
1852 tPtr->tpos = (back ? 1 : 0);
1853 else
1854 tPtr->tpos = (back ? sibling->used : 0);
1856 /* no more chars, so mark it as blank */
1857 } else if (tb->used == 0) {
1858 tb->blank = 1;
1859 } else if (tb->graphic) {
1860 Bool hasNext = (tb->next != NULL);
1862 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1863 if (hasNext) {
1864 tPtr->tpos = 0;
1865 } else if (tPtr->currentTextBlock) {
1866 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1868 } else
1869 printf("DEBUG: unaccounted for... catch this!\n");
1872 layOutDocument(tPtr);
1875 static void insertTextInteractively(Text * tPtr, char *text, int len)
1877 TextBlock *tb;
1878 char *newline = NULL;
1880 if (!tPtr->flags.editable) {
1881 return;
1884 if (len < 1 || !text)
1885 return;
1887 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1888 return;
1890 if (tPtr->flags.ownsSelection)
1891 removeSelection(tPtr);
1893 if (tPtr->flags.ignoreNewLine) {
1894 int i;
1895 for (i = 0; i < len; i++) {
1896 if (text[i] == '\n')
1897 text[i] = ' ';
1901 tb = tPtr->currentTextBlock;
1902 if (!tb || tb->graphic) {
1903 tPtr->tpos = 0;
1904 WMAppendTextStream(tPtr, text);
1905 layOutDocument(tPtr);
1906 return;
1909 if ((newline = strchr(text, '\n'))) {
1910 int nlen = (int)(newline - text);
1911 int s = tb->used - tPtr->tpos;
1913 if (!tb->blank && nlen > 0) {
1914 char *save = NULL;
1916 if (s > 0) {
1917 save = wmalloc(s);
1918 memcpy(save, &tb->text[tPtr->tpos], s);
1919 tb->used -= (tb->used - tPtr->tpos);
1921 insertTextInteractively(tPtr, text, nlen);
1922 newline++;
1923 WMAppendTextStream(tPtr, newline);
1924 if (s > 0) {
1925 insertTextInteractively(tPtr, save, s);
1926 wfree(save);
1928 } else {
1929 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1931 unsigned short savePos = tPtr->tpos;
1932 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1933 tb->d.font, tb->color, True,
1934 tb->used - tPtr->tpos);
1936 if (tb->sections[0].end == tPtr->tpos)
1937 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1938 NULL, tb->d.font,
1939 tb->color, True, 0));
1941 tb->used = savePos;
1942 WMAppendTextBlock(tPtr, ntb);
1943 tPtr->tpos = 0;
1945 } else if (tPtr->tpos == tb->used) {
1946 if (tPtr->flags.indentNewLine) {
1947 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1948 " ", tb->d.font,
1949 tb->color, True, 4));
1950 tPtr->tpos = 4;
1951 } else {
1952 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1953 NULL, tb->d.font,
1954 tb->color, True, 0));
1955 tPtr->tpos = 0;
1957 } else if (tPtr->tpos == 0) {
1958 if (tPtr->flags.indentNewLine) {
1959 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1960 " ", tb->d.font,
1961 tb->color, True, 4));
1962 } else {
1963 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1964 NULL, tb->d.font,
1965 tb->color, True, 0));
1967 tPtr->tpos = 0;
1968 if (tPtr->currentTextBlock->next)
1969 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1972 } else {
1973 if (tb->used + len >= tb->allocated) {
1974 tb->allocated = reqBlockSize(tb->used + len);
1975 tb->text = wrealloc(tb->text, tb->allocated);
1978 if (tb->blank) {
1979 memcpy(tb->text, text, len);
1980 tb->used = len;
1981 tPtr->tpos = len;
1982 tb->text[tb->used] = 0;
1983 tb->blank = False;
1985 } else {
1986 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
1987 memmove(&tb->text[tPtr->tpos], text, len);
1988 tb->used += len;
1989 tPtr->tpos += len;
1990 tb->text[tb->used] = 0;
1995 layOutDocument(tPtr);
1998 static void selectRegion(Text * tPtr, int x, int y)
2001 if (x < 0 || y < 0)
2002 return;
2004 y += (tPtr->flags.rulerShown ? 40 : 0);
2005 y += tPtr->vpos;
2006 if (y > 10)
2007 y -= 10; /* the original offset */
2009 x -= tPtr->visible.x - 2;
2010 if (x < 0)
2011 x = 0;
2013 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2014 tPtr->sel.w = abs(tPtr->clicked.x - x);
2015 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2016 tPtr->sel.h = abs(tPtr->clicked.y - y);
2018 tPtr->flags.ownsSelection = True;
2019 paintText(tPtr);
2022 static void releaseSelection(Text * tPtr)
2024 TextBlock *tb = tPtr->firstTextBlock;
2026 while (tb) {
2027 tb->selected = False;
2028 tb = tb->next;
2030 tPtr->flags.ownsSelection = False;
2031 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2033 paintText(tPtr);
2036 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2038 Text *tPtr = view->self;
2039 Display *dpy = tPtr->view->screen->display;
2040 Atom _TARGETS;
2041 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2042 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2043 WMData *data = NULL;
2045 /* Parameter not used, but tell the compiler that it is ok */
2046 (void) selection;
2047 (void) cdata;
2049 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2050 char *text = WMGetTextSelectedStream(tPtr);
2052 if (text) {
2053 data = WMCreateDataWithBytes(text, strlen(text));
2054 WMSetDataFormat(data, TYPETEXT);
2055 wfree(text);
2057 *type = target;
2058 return data;
2059 } else
2060 printf("didn't get it\n");
2062 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2063 if (target == _TARGETS) {
2064 Atom *ptr;
2066 ptr = wmalloc(4 * sizeof(Atom));
2067 ptr[0] = _TARGETS;
2068 ptr[1] = XA_STRING;
2069 ptr[2] = TEXT;
2070 ptr[3] = COMPOUND_TEXT;
2072 data = WMCreateDataWithBytes(ptr, 4 * 4);
2073 WMSetDataFormat(data, 32);
2075 *type = target;
2076 return data;
2079 return NULL;
2082 static void lostHandler(WMView * view, Atom selection, void *cdata)
2084 /* Parameter not used, but tell the compiler that it is ok */
2085 (void) selection;
2086 (void) cdata;
2088 releaseSelection((WMText *) view->self);
2091 static WMSelectionProcs selectionHandler = {
2092 requestHandler, lostHandler, NULL
2095 static void ownershipObserver(void *observerData, WMNotification * notification)
2097 if (observerData != WMGetNotificationClientData(notification))
2098 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2101 static void autoSelectText(Text * tPtr, int clicks)
2103 int x, start;
2104 TextBlock *tb;
2105 char *mark = NULL, behind, ahead;
2107 if (!(tb = tPtr->currentTextBlock))
2108 return;
2110 if (clicks == 2) {
2112 switch (tb->text[tPtr->tpos]) {
2113 case ' ':
2114 return;
2116 case '<': case '>': behind = '<'; ahead = '>'; break;
2117 case '{': case '}': behind = '{'; ahead = '}'; break;
2118 case '[': case ']': behind = '['; ahead = ']'; break;
2120 default:
2121 behind = ahead = ' ';
2124 tPtr->sel.y = tPtr->cursor.y + 5;
2125 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2127 if (tb->graphic) {
2128 tPtr->sel.x = tb->sections[0].x;
2129 tPtr->sel.w = tb->sections[0].w;
2130 } else {
2131 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2133 start = tPtr->tpos;
2134 while (start > 0 && tb->text[start - 1] != behind)
2135 start--;
2137 x = tPtr->cursor.x;
2138 if (tPtr->tpos > start) {
2139 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2141 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2143 if ((mark = strchr(&tb->text[start], ahead))) {
2144 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2145 (int)(mark - &tb->text[start]));
2146 } else if (tb->used > start) {
2147 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2151 } else if (clicks == 3) {
2152 TextBlock *cur = tb;
2154 while (!tb->first) {
2155 tb = tb->prior;
2156 if (tb == NULL) {
2157 #ifndef NDEBUG
2158 wwarning("corrupted list of text blocks in WMText, while searching for first block");
2159 #endif
2160 goto error_select_3clicks;
2163 tPtr->sel.y = tb->sections[0]._y;
2165 tb = cur;
2166 while (tb->next && !tb->next->first) {
2167 tb = tb->next;
2169 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2171 error_select_3clicks:
2172 tPtr->sel.x = 0;
2173 tPtr->sel.w = tPtr->docWidth;
2174 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2177 if (!tPtr->flags.ownsSelection) {
2178 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2179 tPtr->flags.ownsSelection = True;
2181 paintText(tPtr);
2185 # if 0
2186 static void fontChanged(void *observerData, WMNotification * notification)
2188 WMText *tPtr = (WMText *) observerData;
2189 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2190 printf("fontChanged\n");
2192 if (!tPtr || !font)
2193 return;
2195 if (tPtr->flags.ownsSelection)
2196 WMSetTextSelectionFont(tPtr, font);
2198 #endif
2200 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2202 char buffer[64];
2203 KeySym ksym;
2204 int control_pressed = False;
2205 TextBlock *tb = NULL;
2207 if (((XKeyEvent *) event)->state & ControlMask)
2208 control_pressed = True;
2209 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2211 switch (ksym) {
2213 case XK_Home:
2214 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2215 tPtr->tpos = 0;
2216 updateCursorPosition(tPtr);
2217 paintText(tPtr);
2218 break;
2220 case XK_End:
2221 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2222 if (tPtr->currentTextBlock->graphic)
2223 tPtr->tpos = 1;
2224 else
2225 tPtr->tpos = tPtr->currentTextBlock->used;
2227 updateCursorPosition(tPtr);
2228 paintText(tPtr);
2229 break;
2231 case XK_Left:
2232 if (!(tb = tPtr->currentTextBlock))
2233 break;
2234 if (tb->graphic)
2235 goto L_imaGFX;
2237 if (tPtr->tpos == 0) {
2238 L_imaGFX:
2239 if (tb->prior) {
2240 tPtr->currentTextBlock = tb->prior;
2241 if (tPtr->currentTextBlock->graphic)
2242 tPtr->tpos = 1;
2243 else
2244 tPtr->tpos = tPtr->currentTextBlock->used;
2246 if (!tb->first && tPtr->tpos > 0)
2247 tPtr->tpos--;
2248 } else
2249 tPtr->tpos = 0;
2250 } else
2251 tPtr->tpos--;
2252 updateCursorPosition(tPtr);
2253 paintText(tPtr);
2254 break;
2256 case XK_Right:
2257 if (!(tb = tPtr->currentTextBlock))
2258 break;
2259 if (tb->graphic)
2260 goto R_imaGFX;
2261 if (tPtr->tpos == tb->used) {
2262 R_imaGFX:
2263 if (tb->next) {
2264 tPtr->currentTextBlock = tb->next;
2265 tPtr->tpos = 0;
2266 if (!tb->next->first && tb->next->used > 0)
2267 tPtr->tpos++;
2268 } else {
2269 if (tb->graphic)
2270 tPtr->tpos = 1;
2271 else
2272 tPtr->tpos = tb->used;
2274 } else
2275 tPtr->tpos++;
2276 updateCursorPosition(tPtr);
2277 paintText(tPtr);
2278 break;
2280 case XK_Down:
2281 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2282 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2283 paintText(tPtr);
2284 break;
2286 case XK_Up:
2287 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2288 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2289 paintText(tPtr);
2290 break;
2292 case XK_BackSpace:
2293 case XK_Delete:
2294 #ifdef XK_KP_Delete
2295 case XK_KP_Delete:
2296 #endif
2297 deleteTextInteractively(tPtr, ksym);
2298 updateCursorPosition(tPtr);
2299 paintText(tPtr);
2300 break;
2302 case XK_Control_R:
2303 case XK_Control_L:
2304 control_pressed = True;
2305 break;
2307 case XK_Tab:
2308 insertTextInteractively(tPtr, " ", 4);
2309 updateCursorPosition(tPtr);
2310 paintText(tPtr);
2311 break;
2313 case XK_Return:
2314 *buffer = '\n';
2315 default:
2316 if (*buffer != 0 && !control_pressed) {
2317 insertTextInteractively(tPtr, buffer, strlen(buffer));
2318 updateCursorPosition(tPtr);
2319 paintText(tPtr);
2321 } else if (control_pressed && ksym == XK_r) {
2322 Bool i = !tPtr->flags.rulerShown;
2323 WMShowTextRuler(tPtr, i);
2324 tPtr->flags.rulerShown = i;
2325 } else if (control_pressed && *buffer == '\a') {
2326 XBell(tPtr->view->screen->display, 0);
2327 } else {
2328 WMRelayToNextResponder(tPtr->view, event);
2332 if (!control_pressed && tPtr->flags.ownsSelection) {
2333 releaseSelection(tPtr);
2337 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2339 Text *tPtr = (Text *) view->self;
2340 char *text;
2342 /* Parameter not used, but tell the compiler that it is ok */
2343 (void) selection;
2344 (void) target;
2345 (void) timestamp;
2346 (void) cdata;
2348 tPtr->flags.waitingForSelection = 0;
2350 if (data) {
2351 text = (char *)WMDataBytes(data);
2353 if (tPtr->parser) {
2354 (tPtr->parser) (tPtr, (void *)text);
2355 layOutDocument(tPtr);
2356 } else
2357 insertTextInteractively(tPtr, text, strlen(text));
2358 updateCursorPosition(tPtr);
2359 paintText(tPtr);
2361 } else {
2362 int n;
2364 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2366 if (text) {
2367 text[n] = 0;
2368 if (tPtr->parser) {
2369 (tPtr->parser) (tPtr, (void *)text);
2370 layOutDocument(tPtr);
2371 } else
2372 insertTextInteractively(tPtr, text, n);
2373 updateCursorPosition(tPtr);
2374 paintText(tPtr);
2376 XFree(text);
2382 static void handleActionEvents(XEvent * event, void *data)
2384 Text *tPtr = (Text *) data;
2385 Display *dpy = event->xany.display;
2386 KeySym ksym;
2388 switch (event->type) {
2389 case KeyPress:
2390 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2391 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2392 tPtr->flags.extendSelection = True;
2393 return;
2396 if (tPtr->flags.focused) {
2397 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2398 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2399 GrabModeAsync, GrabModeAsync, None,
2400 tPtr->view->screen->invisibleCursor, CurrentTime);
2401 tPtr->flags.pointerGrabbed = True;
2402 handleTextKeyPress(tPtr, event);
2405 break;
2407 case KeyRelease:
2408 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2409 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2410 tPtr->flags.extendSelection = False;
2411 return;
2412 /* end modify flag so selection can be extended */
2414 break;
2416 case MotionNotify:
2418 if (tPtr->flags.pointerGrabbed) {
2419 tPtr->flags.pointerGrabbed = False;
2420 XUngrabPointer(dpy, CurrentTime);
2423 if (tPtr->flags.waitingForSelection)
2424 break;
2426 if ((event->xmotion.state & Button1Mask)) {
2428 if (WMIsDraggingFromView(tPtr->view)) {
2429 WMDragImageFromView(tPtr->view, event);
2430 break;
2433 if (!tPtr->flags.ownsSelection) {
2434 WMCreateSelectionHandler(tPtr->view,
2435 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2436 tPtr->flags.ownsSelection = True;
2438 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2439 break;
2442 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2443 break;
2445 case ButtonPress:
2447 if (tPtr->flags.pointerGrabbed) {
2448 tPtr->flags.pointerGrabbed = False;
2449 XUngrabPointer(dpy, CurrentTime);
2450 break;
2453 if (tPtr->flags.waitingForSelection)
2454 break;
2456 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2457 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2458 return;
2461 if (tPtr->flags.ownsSelection)
2462 releaseSelection(tPtr);
2464 if (event->xbutton.button == Button1) {
2465 TextBlock *tb = tPtr->currentTextBlock;
2467 if (WMIsDoubleClick(event)) {
2469 tPtr->lastClickTime = event->xbutton.time;
2470 if (tb && tb->graphic && !tb->object) {
2471 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2472 char *desc;
2474 desc = wmalloc(tb->used + 1);
2475 memcpy(desc, tb->text, tb->used);
2476 desc[tb->used] = 0;
2477 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2478 wfree(desc);
2480 } else {
2481 autoSelectText(tPtr, 2);
2483 break;
2484 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2485 tPtr->lastClickTime = event->xbutton.time;
2486 autoSelectText(tPtr, 3);
2487 break;
2490 if (!tPtr->flags.focused) {
2491 WMSetFocusToWidget(tPtr);
2492 tPtr->flags.focused = True;
2493 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2495 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2496 WMDragImageFromView(tPtr->view, event);
2497 break;
2500 tPtr->lastClickTime = event->xbutton.time;
2501 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2502 paintText(tPtr);
2505 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2506 WMScrollText(tPtr, 16);
2507 break;
2510 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2511 WMScrollText(tPtr, -16);
2512 break;
2515 if (event->xbutton.button == Button2) {
2516 char *text = NULL;
2517 int n;
2519 if (!tPtr->flags.editable) {
2520 XBell(dpy, 0);
2521 break;
2524 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2525 event->xbutton.time, pasteText, NULL)) {
2527 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2528 tPtr->flags.waitingForSelection = 0;
2530 if (text) {
2531 text[n] = 0;
2533 if (tPtr->parser) {
2534 (tPtr->parser) (tPtr, (void *)text);
2535 layOutDocument(tPtr);
2536 } else
2537 insertTextInteractively(tPtr, text, n);
2539 XFree(text);
2540 #if 0
2541 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2542 (void *)WMInsertTextEvent);
2543 #endif
2544 updateCursorPosition(tPtr);
2545 paintText(tPtr);
2547 } else {
2548 tPtr->flags.waitingForSelection = True;
2551 break;
2554 case ButtonRelease:
2555 if (tPtr->flags.pointerGrabbed) {
2556 tPtr->flags.pointerGrabbed = False;
2557 XUngrabPointer(dpy, CurrentTime);
2558 break;
2561 if (tPtr->flags.waitingForSelection)
2562 break;
2564 if (WMIsDraggingFromView(tPtr->view))
2565 WMDragImageFromView(tPtr->view, event);
2570 static void handleEvents(XEvent * event, void *data)
2572 Text *tPtr = (Text *) data;
2574 switch (event->type) {
2575 case Expose:
2577 if (event->xexpose.count != 0)
2578 break;
2580 if (tPtr->hS) {
2581 if (!(W_VIEW(tPtr->hS))->flags.realized)
2582 WMRealizeWidget(tPtr->hS);
2585 if (tPtr->vS) {
2586 if (!(W_VIEW(tPtr->vS))->flags.realized)
2587 WMRealizeWidget(tPtr->vS);
2590 if (tPtr->ruler) {
2591 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2592 WMRealizeWidget(tPtr->ruler);
2596 if (!tPtr->db)
2597 textDidResize(tPtr->view->delegate, tPtr->view);
2599 paintText(tPtr);
2600 break;
2602 case FocusIn:
2603 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2604 != tPtr->view)
2605 return;
2606 tPtr->flags.focused = True;
2607 #if DO_BLINK
2608 if (tPtr->flags.editable && !tPtr->timerID) {
2609 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2611 #endif
2613 break;
2615 case FocusOut:
2616 tPtr->flags.focused = False;
2617 paintText(tPtr);
2618 #if DO_BLINK
2619 if (tPtr->timerID) {
2620 WMDeleteTimerHandler(tPtr->timerID);
2621 tPtr->timerID = NULL;
2623 #endif
2624 break;
2626 case DestroyNotify:
2627 clearText(tPtr);
2628 if (tPtr->db)
2629 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2630 if (tPtr->gfxItems)
2631 WMEmptyArray(tPtr->gfxItems);
2632 #if DO_BLINK
2633 if (tPtr->timerID)
2634 WMDeleteTimerHandler(tPtr->timerID);
2635 #endif
2636 WMReleaseFont(tPtr->dFont);
2637 WMReleaseColor(tPtr->dColor);
2638 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2639 WMRemoveNotificationObserver(tPtr);
2641 WMFreeArray(tPtr->xdndSourceTypes);
2642 WMFreeArray(tPtr->xdndDestinationTypes);
2644 wfree(tPtr);
2646 break;
2651 static void insertPlainText(Text * tPtr, const char *text)
2653 const char *start, *mark;
2654 void *tb = NULL;
2656 start = text;
2657 while (start) {
2658 mark = strchr(start, '\n');
2659 if (mark) {
2660 tb = WMCreateTextBlockWithText(tPtr,
2661 start, tPtr->dFont,
2662 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2663 start = mark + 1;
2664 tPtr->flags.first = True;
2665 } else {
2666 if (start && strlen(start)) {
2667 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2668 tPtr->dColor, tPtr->flags.first, strlen(start));
2669 } else
2670 tb = NULL;
2671 tPtr->flags.first = False;
2672 start = mark;
2675 if (tPtr->flags.prepend)
2676 WMPrependTextBlock(tPtr, tb);
2677 else
2678 WMAppendTextBlock(tPtr, tb);
2682 static void rulerMoveCallBack(WMWidget * w, void *self)
2684 Text *tPtr = (Text *) self;
2686 /* Parameter not used, but tell the compiler that it is ok */
2687 (void) w;
2689 if (!tPtr)
2690 return;
2691 if (W_CLASS(tPtr) != WC_Text)
2692 return;
2694 paintText(tPtr);
2697 static void rulerReleaseCallBack(WMWidget * w, void *self)
2699 Text *tPtr = (Text *) self;
2701 /* Parameter not used, but tell the compiler that it is ok */
2702 (void) w;
2704 if (!tPtr)
2705 return;
2706 if (W_CLASS(tPtr) != WC_Text)
2707 return;
2709 WMThawText(tPtr);
2710 return;
2713 static WMArray *dropDataTypes(WMView * self)
2715 return ((Text *) self->self)->xdndSourceTypes;
2718 static WMDragOperationType wantedDropOperation(WMView * self)
2720 /* Parameter not used, but tell the compiler that it is ok */
2721 (void) self;
2723 return WDOperationCopy;
2726 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2728 /* Parameter not used, but tell the compiler that it is ok */
2729 (void) self;
2731 return (allowedOperation == WDOperationCopy);
2734 static WMData *fetchDragData(WMView * self, char *type)
2736 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2737 char *desc;
2738 WMData *data;
2740 if (strcmp(type, "text/plain")) {
2741 if (!tb)
2742 return NULL;
2744 desc = wmalloc(tb->used + 1);
2745 memcpy(desc, tb->text, tb->used);
2746 desc[tb->used] = 0;
2747 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2749 wfree(desc);
2751 return data;
2754 return NULL;
2757 static WMDragSourceProcs _DragSourceProcs = {
2758 dropDataTypes,
2759 wantedDropOperation,
2760 NULL,
2761 acceptDropOperation,
2762 NULL,
2763 NULL,
2764 fetchDragData
2767 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2769 /* Parameter not used, but tell the compiler that it is ok */
2770 (void) request;
2771 (void) sourceDataTypes;
2773 return ((Text *) self->self)->xdndDestinationTypes;
2776 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2778 /* Parameter not used, but tell the compiler that it is ok */
2779 (void) self;
2780 (void) request;
2781 (void) sourceDataTypes;
2783 return WDOperationCopy;
2786 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2788 WMText *tPtr = (WMText *) self->self;
2789 WMData *data;
2790 char *colorName;
2791 WMColor *color;
2793 /* Parameter not used, but tell the compiler that it is ok */
2794 (void) operations;
2795 (void) dropLocation;
2797 if (tPtr) {
2799 /* only one required type, implies only one drop data */
2801 /* get application/X-color if any */
2802 data = (WMData *) WMPopFromArray(dropData);
2803 if (data != NULL) {
2804 colorName = (char *)WMDataBytes(data);
2805 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2807 if (color) {
2808 WMSetTextSelectionColor(tPtr, color);
2809 WMReleaseColor(color);
2815 static WMDragDestinationProcs _DragDestinationProcs = {
2816 NULL,
2817 requiredDataTypes,
2818 allowedOperation,
2819 NULL,
2820 performDragOperation,
2821 NULL
2824 static char *getStream(WMText * tPtr, int sel, int array)
2826 TextBlock *tb = NULL;
2827 char *text = NULL;
2828 unsigned long where = 0;
2830 if (!tPtr)
2831 return NULL;
2833 if (!(tb = tPtr->firstTextBlock))
2834 return NULL;
2836 if (tPtr->writer) {
2837 (tPtr->writer) (tPtr, (void *)text);
2838 return text;
2841 tb = tPtr->firstTextBlock;
2842 while (tb) {
2844 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2846 if (!sel || (tb->graphic && tb->selected)) {
2848 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2849 && tb != tPtr->firstTextBlock) {
2850 text = wrealloc(text, where + 1);
2851 text[where++] = '\n';
2854 if (tb->blank)
2855 goto _gSnext;
2857 if (tb->graphic && array) {
2858 text = wrealloc(text, where + 4);
2859 text[where++] = 0xFA;
2860 text[where++] = (tb->used >> 8) & 0x0ff;
2861 text[where++] = tb->used & 0x0ff;
2862 text[where++] = tb->allocated; /* extra info */
2864 text = wrealloc(text, where + tb->used);
2865 memcpy(&text[where], tb->text, tb->used);
2866 where += tb->used;
2868 } else if (sel && tb->selected) {
2870 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2871 text = wrealloc(text, where + 1);
2872 text[where++] = '\n';
2875 if (tb->blank)
2876 goto _gSnext;
2878 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2879 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2880 where += tb->s_end - tb->s_begin;
2885 _gSnext: tb = tb->next;
2888 /* +1 for the end of string, let's be nice */
2889 text = wrealloc(text, where + 1);
2890 text[where] = 0;
2891 return text;
2894 static void releaseStreamObjects(void *data)
2896 if (data)
2897 wfree(data);
2900 static WMArray *getStreamObjects(WMText * tPtr, int sel)
2902 WMArray *array;
2903 WMData *data;
2904 char *stream;
2905 unsigned short len;
2906 char *start, *fa, *desc;
2908 stream = getStream(tPtr, sel, 1);
2909 if (!stream)
2910 return NULL;
2912 array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2914 start = stream;
2915 while (start) {
2917 fa = strchr(start, 0xFA);
2918 if (fa) {
2919 if ((int)(fa - start) > 0) {
2920 desc = start;
2921 desc[(int)(fa - start)] = 0;
2922 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2923 WMSetDataFormat(data, TYPETEXT);
2924 WMAddToArray(array, (void *)data);
2927 len = *(fa + 1) * 0xff + *(fa + 2);
2928 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2929 WMSetDataFormat(data, *(fa + 3));
2930 WMAddToArray(array, (void *)data);
2931 start = fa + len + 4;
2933 } else {
2934 if (start && strlen(start)) {
2935 data = WMCreateDataWithBytes((void *)start, strlen(start));
2936 WMSetDataFormat(data, TYPETEXT);
2937 WMAddToArray(array, (void *)data);
2939 start = fa;
2943 wfree(stream);
2944 return array;
2947 #define XDND_TEXT_DATA_TYPE "text/plain"
2948 #define XDND_COLOR_DATA_TYPE "application/X-color"
2949 static WMArray *getXdndSourceTypeArray(void)
2951 WMArray *types = WMCreateArray(1);
2952 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2953 return types;
2956 static WMArray *getXdndDestinationTypeArray(void)
2958 WMArray *types = WMCreateArray(1);
2959 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2960 return types;
2963 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2965 Text *tPtr;
2966 Display *dpy;
2967 WMScreen *scr;
2968 XGCValues gcv;
2970 tPtr = wmalloc(sizeof(Text));
2971 tPtr->widgetClass = WC_Text;
2972 tPtr->view = W_CreateView(W_VIEW(parent));
2973 if (!tPtr->view) {
2974 perror("could not create text's view\n");
2975 wfree(tPtr);
2976 return NULL;
2979 dpy = tPtr->view->screen->display;
2980 scr = tPtr->view->screen;
2982 tPtr->view->self = tPtr;
2983 tPtr->view->attribs.cursor = scr->textCursor;
2984 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2985 W_ResizeView(tPtr->view, 250, 200);
2987 tPtr->dColor = WMBlackColor(scr);
2988 tPtr->fgColor = WMBlackColor(scr);
2989 tPtr->bgColor = WMWhiteColor(scr);
2990 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2992 gcv.graphics_exposures = False;
2993 gcv.foreground = W_PIXEL(scr->gray);
2994 gcv.background = W_PIXEL(scr->darkGray);
2995 gcv.fill_style = FillStippled;
2996 /* why not use scr->stipple here? */
2997 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2998 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2999 GCForeground | GCBackground | GCStipple
3000 | GCFillStyle | GCGraphicsExposures, &gcv);
3002 tPtr->ruler = NULL;
3003 tPtr->vS = NULL;
3004 tPtr->hS = NULL;
3006 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3008 tPtr->view->delegate = &_TextViewDelegate;
3010 tPtr->delegate = NULL;
3012 #if DO_BLINK
3013 tPtr->timerID = NULL;
3014 #endif
3016 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
3017 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
3019 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
3020 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
3022 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
3024 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3025 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3028 WMArray *types = WMCreateArray(2);
3029 WMAddToArray(types, "application/X-color");
3030 WMAddToArray(types, "application/X-image");
3031 WMRegisterViewForDraggedTypes(tPtr->view, types);
3032 WMFreeArray(types);
3035 /*WMAddNotificationObserver(fontChanged, tPtr,
3036 WMFontPanelDidChangeNotification, tPtr); */
3038 tPtr->firstTextBlock = NULL;
3039 tPtr->lastTextBlock = NULL;
3040 tPtr->currentTextBlock = NULL;
3041 tPtr->tpos = 0;
3043 tPtr->gfxItems = WMCreateArray(4);
3045 tPtr->parser = parser;
3046 tPtr->writer = writer;
3048 tPtr->sel.x = tPtr->sel.y = 2;
3049 tPtr->sel.w = tPtr->sel.h = 0;
3051 tPtr->clicked.x = tPtr->clicked.y = 2;
3053 tPtr->visible.x = tPtr->visible.y = 2;
3054 tPtr->visible.h = tPtr->view->size.height;
3055 tPtr->visible.w = tPtr->view->size.width - 4;
3057 tPtr->cursor.x = -23;
3059 tPtr->docWidth = 0;
3060 tPtr->docHeight = 0;
3061 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3062 tPtr->db = (Pixmap) NULL;
3063 tPtr->bgPixmap = NULL;
3065 tPtr->margins = WMGetRulerMargins(NULL);
3066 tPtr->margins->right = tPtr->visible.w;
3067 tPtr->nMargins = 1;
3069 tPtr->flags.rulerShown = False;
3070 tPtr->flags.monoFont = False;
3071 tPtr->flags.focused = False;
3072 tPtr->flags.editable = True;
3073 tPtr->flags.ownsSelection = False;
3074 tPtr->flags.pointerGrabbed = False;
3075 tPtr->flags.extendSelection = False;
3076 tPtr->flags.frozen = False;
3077 tPtr->flags.cursorShown = True;
3078 tPtr->flags.acceptsGraphic = False;
3079 tPtr->flags.horizOnDemand = False;
3080 tPtr->flags.needsLayOut = False;
3081 tPtr->flags.ignoreNewLine = False;
3082 tPtr->flags.indentNewLine = False;
3083 tPtr->flags.laidOut = False;
3084 tPtr->flags.ownsSelection = False;
3085 tPtr->flags.waitingForSelection = False;
3086 tPtr->flags.prepend = False;
3087 tPtr->flags.isOverGraphic = False;
3088 tPtr->flags.relief = WRSunken;
3089 tPtr->flags.isOverGraphic = 0;
3090 tPtr->flags.alignment = WALeft;
3091 tPtr->flags.first = True;
3093 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3094 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3096 return tPtr;
3099 void WMPrependTextStream(WMText * tPtr, const char *text)
3101 CHECK_CLASS(tPtr, WC_Text);
3103 if (!text) {
3104 if (tPtr->flags.ownsSelection)
3105 releaseSelection(tPtr);
3106 clearText(tPtr);
3107 updateScrollers(tPtr);
3108 return;
3111 tPtr->flags.prepend = True;
3112 if (text && tPtr->parser)
3113 (tPtr->parser) (tPtr, (void *)text);
3114 else
3115 insertPlainText(tPtr, text);
3117 tPtr->flags.needsLayOut = True;
3118 tPtr->tpos = 0;
3119 if (!tPtr->flags.frozen) {
3120 layOutDocument(tPtr);
3124 void WMAppendTextStream(WMText * tPtr, const char *text)
3126 CHECK_CLASS(tPtr, WC_Text);
3128 if (!text) {
3129 if (tPtr->flags.ownsSelection)
3130 releaseSelection(tPtr);
3131 clearText(tPtr);
3132 updateScrollers(tPtr);
3133 return;
3136 tPtr->flags.prepend = False;
3137 if (text && tPtr->parser)
3138 (tPtr->parser) (tPtr, (void *)text);
3139 else
3140 insertPlainText(tPtr, text);
3142 tPtr->flags.needsLayOut = True;
3143 if (tPtr->currentTextBlock) {
3144 if (tPtr->currentTextBlock->graphic)
3145 tPtr->tpos = 1;
3146 else
3147 tPtr->tpos = tPtr->currentTextBlock->used;
3150 if (!tPtr->flags.frozen) {
3151 layOutDocument(tPtr);
3155 char *WMGetTextStream(WMText * tPtr)
3157 CHECK_CLASS(tPtr, WC_Text);
3159 return getStream(tPtr, 0, 0);
3162 char *WMGetTextSelectedStream(WMText * tPtr)
3164 CHECK_CLASS(tPtr, WC_Text);
3166 return getStream(tPtr, 1, 0);
3169 WMArray *WMGetTextObjects(WMText * tPtr)
3171 CHECK_CLASS(tPtr, WC_Text);
3173 return getStreamObjects(tPtr, 0);
3176 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3178 CHECK_CLASS(tPtr, WC_Text);
3180 return getStreamObjects(tPtr, 1);
3183 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3185 CHECK_CLASS(tPtr, WC_Text);
3187 tPtr->delegate = delegate;
3190 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3191 const char *description, WMColor * color,
3192 unsigned short first, unsigned short extraInfo)
3194 TextBlock *tb;
3196 if (!w || !description || !color)
3197 return NULL;
3199 tb = wmalloc(sizeof(TextBlock));
3201 tb->text = wstrdup(description);
3202 tb->used = strlen(description);
3203 tb->blank = False;
3204 tb->d.widget = w;
3205 tb->color = WMRetainColor(color);
3206 tb->marginN = newMargin(tPtr, NULL);
3207 tb->allocated = extraInfo;
3208 tb->first = first;
3209 tb->kanji = False;
3210 tb->graphic = True;
3211 tb->object = True;
3212 tb->underlined = False;
3213 tb->selected = False;
3214 tb->script = 0;
3215 tb->sections = NULL;
3216 tb->nsections = 0;
3217 tb->prior = NULL;
3218 tb->next = NULL;
3220 return tb;
3223 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3224 const char *description, WMColor * color,
3225 unsigned short first, unsigned short extraInfo)
3227 TextBlock *tb;
3229 if (!p || !description || !color)
3230 return NULL;
3232 tb = wmalloc(sizeof(TextBlock));
3234 tb->text = wstrdup(description);
3235 tb->used = strlen(description);
3236 tb->blank = False;
3237 tb->d.pixmap = WMRetainPixmap(p);
3238 tb->color = WMRetainColor(color);
3239 tb->marginN = newMargin(tPtr, NULL);
3240 tb->allocated = extraInfo;
3241 tb->first = first;
3242 tb->kanji = False;
3243 tb->graphic = True;
3244 tb->object = False;
3245 tb->underlined = False;
3246 tb->selected = False;
3247 tb->script = 0;
3248 tb->sections = NULL;
3249 tb->nsections = 0;
3250 tb->prior = NULL;
3251 tb->next = NULL;
3253 return tb;
3256 void *WMCreateTextBlockWithText(WMText * tPtr, const char *text, WMFont * font, WMColor * color,
3257 unsigned short first, unsigned short len)
3259 TextBlock *tb;
3261 if (!font || !color)
3262 return NULL;
3264 tb = wmalloc(sizeof(TextBlock));
3266 tb->allocated = reqBlockSize(len);
3267 tb->text = (char *)wmalloc(tb->allocated);
3269 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3270 *tb->text = ' ';
3271 tb->used = 1;
3272 tb->blank = True;
3273 } else {
3274 memcpy(tb->text, text, len);
3275 tb->used = len;
3276 tb->blank = False;
3278 tb->text[tb->used] = 0;
3280 tb->d.font = WMRetainFont(font);
3281 tb->color = WMRetainColor(color);
3282 tb->marginN = newMargin(tPtr, NULL);
3283 tb->first = first;
3284 tb->kanji = False;
3285 tb->graphic = False;
3286 tb->underlined = False;
3287 tb->selected = False;
3288 tb->script = 0;
3289 tb->sections = NULL;
3290 tb->nsections = 0;
3291 tb->prior = NULL;
3292 tb->next = NULL;
3293 return tb;
3296 void
3297 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3298 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3300 TextBlock *tb = (TextBlock *) vtb;
3301 if (!tb)
3302 return;
3304 tb->first = first;
3305 tb->kanji = kanji;
3306 tb->underlined = underlined;
3307 tb->script = script;
3308 tb->marginN = newMargin(tPtr, margins);
3311 void
3312 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3313 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins *margins)
3315 TextBlock *tb = (TextBlock *) vtb;
3316 if (!tb)
3317 return;
3319 if (first)
3320 *first = tb->first;
3321 if (kanji)
3322 *kanji = tb->kanji;
3323 if (underlined)
3324 *underlined = tb->underlined;
3325 if (script)
3326 *script = tb->script;
3327 if (margins)
3328 *margins = tPtr->margins[tb->marginN];
3331 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3333 TextBlock *tb = (TextBlock *) vtb;
3335 if (!tb)
3336 return;
3338 if (tb->graphic) {
3339 if (tb->object) {
3340 WMWidget *w = tb->d.widget;
3341 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3342 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3343 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3346 WMAddToArray(tPtr->gfxItems, (void *)tb);
3347 tPtr->tpos = 1;
3349 } else {
3350 tPtr->tpos = tb->used;
3353 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3354 tb->next = tb->prior = NULL;
3355 tb->first = True;
3356 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3357 return;
3360 if (!tb->first) {
3361 tb->marginN = tPtr->currentTextBlock->marginN;
3364 tb->next = tPtr->currentTextBlock;
3365 tb->prior = tPtr->currentTextBlock->prior;
3366 if (tPtr->currentTextBlock->prior)
3367 tPtr->currentTextBlock->prior->next = tb;
3369 tPtr->currentTextBlock->prior = tb;
3370 if (!tb->prior)
3371 tPtr->firstTextBlock = tb;
3373 tPtr->currentTextBlock = tb;
3376 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3378 TextBlock *tb = (TextBlock *) vtb;
3380 if (!tb)
3381 return;
3383 if (tb->graphic) {
3384 if (tb->object) {
3385 WMWidget *w = tb->d.widget;
3386 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3387 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3388 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3391 WMAddToArray(tPtr->gfxItems, (void *)tb);
3392 tPtr->tpos = 1;
3394 } else {
3395 tPtr->tpos = tb->used;
3398 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3399 tb->next = tb->prior = NULL;
3400 tb->first = True;
3401 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3402 return;
3405 if (!tb->first) {
3406 tb->marginN = tPtr->currentTextBlock->marginN;
3409 tb->next = tPtr->currentTextBlock->next;
3410 tb->prior = tPtr->currentTextBlock;
3411 if (tPtr->currentTextBlock->next)
3412 tPtr->currentTextBlock->next->prior = tb;
3414 tPtr->currentTextBlock->next = tb;
3416 if (!tb->next)
3417 tPtr->lastTextBlock = tb;
3419 tPtr->currentTextBlock = tb;
3422 void *WMRemoveTextBlock(WMText * tPtr)
3424 TextBlock *tb = NULL;
3426 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3427 return NULL;
3430 tb = tPtr->currentTextBlock;
3431 if (tb->graphic) {
3432 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3434 if (tb->object) {
3435 WMUnmapWidget(tb->d.widget);
3439 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3440 if (tPtr->currentTextBlock->next)
3441 tPtr->currentTextBlock->next->prior = NULL;
3443 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3444 tPtr->currentTextBlock = tPtr->firstTextBlock;
3446 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3447 tPtr->currentTextBlock->prior->next = NULL;
3448 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3449 tPtr->currentTextBlock = tPtr->lastTextBlock;
3450 } else {
3451 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3452 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3453 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3456 return (void *)tb;
3459 #if 0
3460 static void destroyWidget(WMWidget * widget)
3462 WMDestroyWidget(widget);
3463 // -- never do this -- wfree(widget);
3465 #endif
3467 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3469 TextBlock *tb = (TextBlock *) vtb;
3471 /* Parameter not used, but tell the compiler that it is ok */
3472 (void) tPtr;
3474 if (!tb)
3475 return;
3477 if (tb->graphic) {
3478 if (tb->object) {
3479 /* naturally, there's a danger to destroying widgets whose action
3480 * brings us here: ie. press a button to destroy it...
3481 * need to find a safer way. till then... this stays commented out */
3482 /* 5 months later... destroy it 10 seconds after now which should
3483 * be enough time for the widget's action to be completed... :-) */
3484 /* This is a bad assumption. Just destroy the widget here.
3485 * if the caller needs it, it can protect it with W_RetainView()
3486 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3487 WMDestroyWidget(tb->d.widget);
3488 } else {
3489 WMReleasePixmap(tb->d.pixmap);
3491 } else {
3492 WMReleaseFont(tb->d.font);
3495 WMReleaseColor(tb->color);
3496 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3497 if (tb->sections)
3498 wfree(tb->sections);
3499 wfree(tb->text);
3500 wfree(tb);
3503 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3505 if (tPtr->fgColor)
3506 WMReleaseColor(tPtr->fgColor);
3508 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3510 paintText(tPtr);
3513 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3515 if (tPtr->bgColor)
3516 WMReleaseColor(tPtr->bgColor);
3518 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3519 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3521 paintText(tPtr);
3524 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3526 if (tPtr->bgPixmap)
3527 WMReleasePixmap(tPtr->bgPixmap);
3529 if (pixmap)
3530 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3531 else
3532 tPtr->bgPixmap = NULL;
3535 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3537 tPtr->flags.relief = relief;
3538 textDidResize(tPtr->view->delegate, tPtr->view);
3541 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3543 if (shouldhave && !tPtr->hS) {
3544 tPtr->hS = WMCreateScroller(tPtr);
3545 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3546 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3547 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3548 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3549 WMMapWidget(tPtr->hS);
3550 } else if (!shouldhave && tPtr->hS) {
3551 WMUnmapWidget(tPtr->hS);
3552 WMDestroyWidget(tPtr->hS);
3553 tPtr->hS = NULL;
3556 tPtr->hpos = 0;
3557 tPtr->prevHpos = 0;
3558 textDidResize(tPtr->view->delegate, tPtr->view);
3561 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3563 if (shouldhave && !tPtr->ruler) {
3564 tPtr->ruler = WMCreateRuler(tPtr);
3565 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3566 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3567 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3568 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3569 } else if (!shouldhave && tPtr->ruler) {
3570 WMShowTextRuler(tPtr, False);
3571 WMDestroyWidget(tPtr->ruler);
3572 tPtr->ruler = NULL;
3574 textDidResize(tPtr->view->delegate, tPtr->view);
3577 void WMShowTextRuler(WMText * tPtr, Bool show)
3579 if (!tPtr->ruler)
3580 return;
3582 if (tPtr->flags.monoFont)
3583 show = False;
3585 tPtr->flags.rulerShown = show;
3586 if (show) {
3587 WMMapWidget(tPtr->ruler);
3588 } else {
3589 WMUnmapWidget(tPtr->ruler);
3592 textDidResize(tPtr->view->delegate, tPtr->view);
3595 Bool WMGetTextRulerShown(WMText * tPtr)
3597 if (!tPtr->ruler)
3598 return False;
3600 return tPtr->flags.rulerShown;
3603 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3605 if (shouldhave && !tPtr->vS) {
3606 tPtr->vS = WMCreateScroller(tPtr);
3607 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3608 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3609 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3610 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3611 WMMapWidget(tPtr->vS);
3612 } else if (!shouldhave && tPtr->vS) {
3613 WMUnmapWidget(tPtr->vS);
3614 WMDestroyWidget(tPtr->vS);
3615 tPtr->vS = NULL;
3618 tPtr->vpos = 0;
3619 tPtr->prevVpos = 0;
3620 textDidResize(tPtr->view->delegate, tPtr->view);
3623 Bool WMScrollText(WMText * tPtr, int amount)
3625 Bool scroll = False;
3627 if (amount == 0 || !tPtr->view->flags.realized)
3628 return False;
3630 if (amount < 0) {
3631 if (tPtr->vpos > 0) {
3632 if (tPtr->vpos > abs(amount))
3633 tPtr->vpos += amount;
3634 else
3635 tPtr->vpos = 0;
3636 scroll = True;
3638 } else {
3639 int limit = tPtr->docHeight - tPtr->visible.h;
3640 if (tPtr->vpos < limit) {
3641 if (tPtr->vpos < limit - amount)
3642 tPtr->vpos += amount;
3643 else
3644 tPtr->vpos = limit;
3645 scroll = True;
3649 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3650 updateScrollers(tPtr);
3651 paintText(tPtr);
3653 tPtr->prevVpos = tPtr->vpos;
3654 return scroll;
3657 Bool WMPageText(WMText * tPtr, Bool direction)
3659 if (!tPtr->view->flags.realized)
3660 return False;
3662 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3665 void WMSetTextEditable(WMText * tPtr, Bool editable)
3667 tPtr->flags.editable = editable;
3670 int WMGetTextEditable(WMText * tPtr)
3672 return tPtr->flags.editable;
3675 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3677 tPtr->flags.indentNewLine = indent;
3680 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3682 tPtr->flags.ignoreNewLine = ignore;
3685 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3687 return tPtr->flags.ignoreNewLine;
3690 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3692 if (mono) {
3693 if (tPtr->flags.rulerShown)
3694 WMShowTextRuler(tPtr, False);
3695 if (tPtr->flags.alignment != WALeft)
3696 tPtr->flags.alignment = WALeft;
3699 tPtr->flags.monoFont = mono;
3700 textDidResize(tPtr->view->delegate, tPtr->view);
3703 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3705 return tPtr->flags.monoFont;
3708 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3710 if (tPtr->dFont)
3711 WMReleaseFont(tPtr->dFont);
3713 if (font) {
3714 tPtr->dFont = WMRetainFont(font);
3715 } else {
3716 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3720 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3722 return WMRetainFont(tPtr->dFont);
3725 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3727 if (tPtr->dColor)
3728 WMReleaseColor(tPtr->dColor);
3730 if (color) {
3731 tPtr->dColor = WMRetainColor(color);
3732 } else {
3733 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3737 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3739 return tPtr->dColor;
3742 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3744 if (tPtr->flags.monoFont)
3745 tPtr->flags.alignment = WALeft;
3746 else
3747 tPtr->flags.alignment = alignment;
3748 WMThawText(tPtr);
3751 int WMGetTextInsertType(WMText * tPtr)
3753 return tPtr->flags.prepend;
3756 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3758 setSelectionProperty(tPtr, NULL, color, -1);
3761 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3763 TextBlock *tb;
3765 tb = tPtr->currentTextBlock;
3767 if (!tb || !tPtr->flags.ownsSelection)
3768 return NULL;
3770 if (!tb->selected)
3771 return NULL;
3773 return tb->color;
3776 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3778 setSelectionProperty(tPtr, font, NULL, -1);
3781 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3783 TextBlock *tb;
3785 tb = tPtr->currentTextBlock;
3787 if (!tb || !tPtr->flags.ownsSelection)
3788 return NULL;
3790 if (!tb->selected)
3791 return NULL;
3793 if (tb->graphic) {
3794 tb = getFirstNonGraphicBlockFor(tb, 1);
3795 if (!tb)
3796 return NULL;
3798 return (tb->selected ? tb->d.font : NULL);
3801 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3803 /* // check this */
3804 if (underlined != 0 && underlined != 1)
3805 return;
3807 setSelectionProperty(tPtr, NULL, NULL, underlined);
3810 int WMGetTextSelectionUnderlined(WMText * tPtr)
3812 TextBlock *tb;
3814 tb = tPtr->currentTextBlock;
3816 if (!tb || !tPtr->flags.ownsSelection)
3817 return 0;
3819 if (!tb->selected)
3820 return 0;
3822 return tb->underlined;
3825 void WMFreezeText(WMText * tPtr)
3827 tPtr->flags.frozen = True;
3830 void WMThawText(WMText * tPtr)
3832 tPtr->flags.frozen = False;
3834 if (tPtr->flags.monoFont) {
3835 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3836 TextBlock *tb;
3838 /* make sure to unmap widgets no matter where they are */
3839 /* they'll be later remapped if needed by paintText */
3840 for (j = 0; j < c; j++) {
3841 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3842 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3843 WMUnmapWidget(tb->d.widget);
3848 tPtr->flags.laidOut = False;
3849 layOutDocument(tPtr);
3850 updateScrollers(tPtr);
3851 paintText(tPtr);
3852 tPtr->flags.needsLayOut = False;
3856 /* find first occurence of a string */
3857 static const char *mystrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3859 const char *ptr;
3861 if (!haystack || !needle || !end)
3862 return NULL;
3864 for (ptr = haystack; ptr < end; ptr++) {
3865 if (caseSensitive) {
3866 if (*ptr == *needle && !strncmp(ptr, needle, len))
3867 return ptr;
3869 } else {
3870 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3871 return ptr;
3875 return NULL;
3878 /* find last occurence of a string */
3879 static const char *mystrrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3881 const char *ptr;
3883 if (!haystack || !needle || !end)
3884 return NULL;
3886 for (ptr = haystack - 2; ptr > end; ptr--) {
3887 if (caseSensitive) {
3888 if (*ptr == *needle && !strncmp(ptr, needle, len))
3889 return ptr;
3890 } else {
3891 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3892 return ptr;
3896 return NULL;
3899 Bool WMFindInTextStream(WMText * tPtr, const char *needle, Bool direction, Bool caseSensitive)
3901 TextBlock *tb;
3902 const char *mark = NULL;
3903 unsigned short pos;
3905 #if 0
3906 if (!(tb = tPtr->currentTextBlock)) {
3907 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3908 return False;
3910 } else {
3911 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3912 tb = (direction>0) ? tb->next : tb->prior; */
3913 if (tb != tPtr->lastTextBlock)
3914 tb = tb->prior;
3916 #endif
3917 tb = tPtr->currentTextBlock;
3918 pos = tPtr->tpos;
3920 while (tb) {
3921 if (!tb->graphic) {
3923 if (direction > 0) {
3924 if (pos + 1 < tb->used)
3925 pos++;
3927 if (tb->used - pos > 0 && pos > 0) {
3928 mark = mystrstr(&tb->text[pos], needle,
3929 strlen(needle), &tb->text[tb->used], caseSensitive);
3931 } else {
3932 tb = tb->next;
3933 pos = 0;
3934 continue;
3937 } else {
3938 if (pos - 1 > 0)
3939 pos--;
3941 if (pos > 0) {
3942 mark = mystrrstr(&tb->text[pos], needle,
3943 strlen(needle), tb->text, caseSensitive);
3944 } else {
3945 tb = tb->prior;
3946 if (!tb)
3947 return False;
3948 pos = tb->used;
3949 continue;
3953 if (mark) {
3954 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3956 tPtr->tpos = (int)(mark - tb->text);
3957 tPtr->currentTextBlock = tb;
3958 updateCursorPosition(tPtr);
3959 tPtr->sel.y = tPtr->cursor.y + 5;
3960 tPtr->sel.h = tPtr->cursor.h - 10;
3961 tPtr->sel.x = tPtr->cursor.x + 1;
3962 tPtr->sel.w = WMIN(WMWidthOfString(font,
3963 &tb->text[tPtr->tpos], strlen(needle)),
3964 tPtr->docWidth - tPtr->sel.x);
3965 tPtr->flags.ownsSelection = True;
3966 paintText(tPtr);
3968 return True;
3972 tb = (direction > 0) ? tb->next : tb->prior;
3973 if (tb) {
3974 pos = (direction > 0) ? 0 : tb->used;
3978 return False;
3981 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3983 if (!tPtr->flags.ownsSelection)
3984 return False;
3986 removeSelection(tPtr);
3988 if (replacement) {
3989 insertTextInteractively(tPtr, replacement, strlen(replacement));
3990 updateCursorPosition(tPtr);
3991 paintText(tPtr);
3994 return True;