wmaker: remove useless null pointer check (Coverity #109612)
[wmaker-crm.git] / WINGs / wtext.c
blob93090e4087487b99dc1a1df181cf7a37c7049bc6
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 array[4];
2066 array[0] = _TARGETS;
2067 array[1] = XA_STRING;
2068 array[2] = TEXT;
2069 array[3] = COMPOUND_TEXT;
2071 data = WMCreateDataWithBytes(&array, sizeof(array));
2072 WMSetDataFormat(data, 32);
2074 *type = target;
2075 return data;
2078 return NULL;
2081 static void lostHandler(WMView * view, Atom selection, void *cdata)
2083 /* Parameter not used, but tell the compiler that it is ok */
2084 (void) selection;
2085 (void) cdata;
2087 releaseSelection((WMText *) view->self);
2090 static WMSelectionProcs selectionHandler = {
2091 requestHandler, lostHandler, NULL
2094 static void ownershipObserver(void *observerData, WMNotification * notification)
2096 if (observerData != WMGetNotificationClientData(notification))
2097 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2100 static void autoSelectText(Text * tPtr, int clicks)
2102 int x, start;
2103 TextBlock *tb;
2104 char *mark = NULL, behind, ahead;
2106 if (!(tb = tPtr->currentTextBlock))
2107 return;
2109 if (clicks == 2) {
2111 switch (tb->text[tPtr->tpos]) {
2112 case ' ':
2113 return;
2115 case '<': case '>': behind = '<'; ahead = '>'; break;
2116 case '{': case '}': behind = '{'; ahead = '}'; break;
2117 case '[': case ']': behind = '['; ahead = ']'; break;
2119 default:
2120 behind = ahead = ' ';
2123 tPtr->sel.y = tPtr->cursor.y + 5;
2124 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2126 if (tb->graphic) {
2127 tPtr->sel.x = tb->sections[0].x;
2128 tPtr->sel.w = tb->sections[0].w;
2129 } else {
2130 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2132 start = tPtr->tpos;
2133 while (start > 0 && tb->text[start - 1] != behind)
2134 start--;
2136 x = tPtr->cursor.x;
2137 if (tPtr->tpos > start) {
2138 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2140 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2142 if ((mark = strchr(&tb->text[start], ahead))) {
2143 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2144 (int)(mark - &tb->text[start]));
2145 } else if (tb->used > start) {
2146 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2150 } else if (clicks == 3) {
2151 TextBlock *cur = tb;
2153 while (!tb->first) {
2154 tb = tb->prior;
2155 if (tb == NULL) {
2156 #ifndef NDEBUG
2157 wwarning("corrupted list of text blocks in WMText, while searching for first block");
2158 #endif
2159 goto error_select_3clicks;
2162 tPtr->sel.y = tb->sections[0]._y;
2164 tb = cur;
2165 while (tb->next && !tb->next->first) {
2166 tb = tb->next;
2168 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2170 error_select_3clicks:
2171 tPtr->sel.x = 0;
2172 tPtr->sel.w = tPtr->docWidth;
2173 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2176 if (!tPtr->flags.ownsSelection) {
2177 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2178 tPtr->flags.ownsSelection = True;
2180 paintText(tPtr);
2184 # if 0
2185 static void fontChanged(void *observerData, WMNotification * notification)
2187 WMText *tPtr = (WMText *) observerData;
2188 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2189 printf("fontChanged\n");
2191 if (!tPtr || !font)
2192 return;
2194 if (tPtr->flags.ownsSelection)
2195 WMSetTextSelectionFont(tPtr, font);
2197 #endif
2199 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2201 char buffer[64];
2202 KeySym ksym;
2203 int control_pressed = False;
2204 TextBlock *tb = NULL;
2206 if (((XKeyEvent *) event)->state & ControlMask)
2207 control_pressed = True;
2208 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2210 switch (ksym) {
2212 case XK_Home:
2213 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2214 tPtr->tpos = 0;
2215 updateCursorPosition(tPtr);
2216 paintText(tPtr);
2217 break;
2219 case XK_End:
2220 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2221 if (tPtr->currentTextBlock->graphic)
2222 tPtr->tpos = 1;
2223 else
2224 tPtr->tpos = tPtr->currentTextBlock->used;
2226 updateCursorPosition(tPtr);
2227 paintText(tPtr);
2228 break;
2230 case XK_Left:
2231 if (!(tb = tPtr->currentTextBlock))
2232 break;
2234 if (tb->graphic || tPtr->tpos == 0) {
2235 if (tb->prior) {
2236 tPtr->currentTextBlock = tb->prior;
2237 if (tPtr->currentTextBlock->graphic)
2238 tPtr->tpos = 1;
2239 else
2240 tPtr->tpos = tPtr->currentTextBlock->used;
2242 if (!tb->first && tPtr->tpos > 0)
2243 tPtr->tpos--;
2244 } else
2245 tPtr->tpos = 0;
2246 } else
2247 tPtr->tpos--;
2248 updateCursorPosition(tPtr);
2249 paintText(tPtr);
2250 break;
2252 case XK_Right:
2253 if (!(tb = tPtr->currentTextBlock))
2254 break;
2255 if (tb->graphic || tPtr->tpos == tb->used) {
2256 if (tb->next) {
2257 tPtr->currentTextBlock = tb->next;
2258 tPtr->tpos = 0;
2259 if (!tb->next->first && tb->next->used > 0)
2260 tPtr->tpos++;
2261 } else {
2262 if (tb->graphic)
2263 tPtr->tpos = 1;
2264 else
2265 tPtr->tpos = tb->used;
2267 } else
2268 tPtr->tpos++;
2269 updateCursorPosition(tPtr);
2270 paintText(tPtr);
2271 break;
2273 case XK_Down:
2274 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2275 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2276 paintText(tPtr);
2277 break;
2279 case XK_Up:
2280 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2281 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2282 paintText(tPtr);
2283 break;
2285 case XK_BackSpace:
2286 case XK_Delete:
2287 #ifdef XK_KP_Delete
2288 case XK_KP_Delete:
2289 #endif
2290 deleteTextInteractively(tPtr, ksym);
2291 updateCursorPosition(tPtr);
2292 paintText(tPtr);
2293 break;
2295 case XK_Control_R:
2296 case XK_Control_L:
2297 control_pressed = True;
2298 break;
2300 case XK_Tab:
2301 insertTextInteractively(tPtr, " ", 4);
2302 updateCursorPosition(tPtr);
2303 paintText(tPtr);
2304 break;
2306 case XK_Return:
2307 *buffer = '\n';
2308 default:
2309 if (*buffer != 0 && !control_pressed) {
2310 insertTextInteractively(tPtr, buffer, strlen(buffer));
2311 updateCursorPosition(tPtr);
2312 paintText(tPtr);
2314 } else if (control_pressed && ksym == XK_r) {
2315 Bool i = !tPtr->flags.rulerShown;
2316 WMShowTextRuler(tPtr, i);
2317 tPtr->flags.rulerShown = i;
2318 } else if (control_pressed && *buffer == '\a') {
2319 XBell(tPtr->view->screen->display, 0);
2320 } else {
2321 WMRelayToNextResponder(tPtr->view, event);
2325 if (!control_pressed && tPtr->flags.ownsSelection) {
2326 releaseSelection(tPtr);
2330 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2332 Text *tPtr = (Text *) view->self;
2333 char *text;
2335 /* Parameter not used, but tell the compiler that it is ok */
2336 (void) selection;
2337 (void) target;
2338 (void) timestamp;
2339 (void) cdata;
2341 tPtr->flags.waitingForSelection = 0;
2343 if (data) {
2344 text = (char *)WMDataBytes(data);
2346 if (tPtr->parser) {
2347 (tPtr->parser) (tPtr, (void *)text);
2348 layOutDocument(tPtr);
2349 } else
2350 insertTextInteractively(tPtr, text, strlen(text));
2351 updateCursorPosition(tPtr);
2352 paintText(tPtr);
2354 } else {
2355 int n;
2357 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2359 if (text) {
2360 text[n] = 0;
2361 if (tPtr->parser) {
2362 (tPtr->parser) (tPtr, (void *)text);
2363 layOutDocument(tPtr);
2364 } else
2365 insertTextInteractively(tPtr, text, n);
2366 updateCursorPosition(tPtr);
2367 paintText(tPtr);
2369 XFree(text);
2375 static void handleActionEvents(XEvent * event, void *data)
2377 Text *tPtr = (Text *) data;
2378 Display *dpy = event->xany.display;
2379 KeySym ksym;
2381 switch (event->type) {
2382 case KeyPress:
2383 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2384 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2385 tPtr->flags.extendSelection = True;
2386 return;
2389 if (tPtr->flags.focused) {
2390 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2391 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2392 GrabModeAsync, GrabModeAsync, None,
2393 tPtr->view->screen->invisibleCursor, CurrentTime);
2394 tPtr->flags.pointerGrabbed = True;
2395 handleTextKeyPress(tPtr, event);
2398 break;
2400 case KeyRelease:
2401 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2402 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2403 tPtr->flags.extendSelection = False;
2404 return;
2405 /* end modify flag so selection can be extended */
2407 break;
2409 case MotionNotify:
2411 if (tPtr->flags.pointerGrabbed) {
2412 tPtr->flags.pointerGrabbed = False;
2413 XUngrabPointer(dpy, CurrentTime);
2416 if (tPtr->flags.waitingForSelection)
2417 break;
2419 if ((event->xmotion.state & Button1Mask)) {
2421 if (WMIsDraggingFromView(tPtr->view)) {
2422 WMDragImageFromView(tPtr->view, event);
2423 break;
2426 if (!tPtr->flags.ownsSelection) {
2427 WMCreateSelectionHandler(tPtr->view,
2428 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2429 tPtr->flags.ownsSelection = True;
2431 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2432 break;
2435 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2436 break;
2438 case ButtonPress:
2440 if (tPtr->flags.pointerGrabbed) {
2441 tPtr->flags.pointerGrabbed = False;
2442 XUngrabPointer(dpy, CurrentTime);
2443 break;
2446 if (tPtr->flags.waitingForSelection)
2447 break;
2449 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2450 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2451 return;
2454 if (tPtr->flags.ownsSelection)
2455 releaseSelection(tPtr);
2457 if (event->xbutton.button == Button1) {
2458 TextBlock *tb = tPtr->currentTextBlock;
2460 if (WMIsDoubleClick(event)) {
2462 tPtr->lastClickTime = event->xbutton.time;
2463 if (tb && tb->graphic && !tb->object) {
2464 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2465 char *desc;
2467 desc = wmalloc(tb->used + 1);
2468 memcpy(desc, tb->text, tb->used);
2469 desc[tb->used] = 0;
2470 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2471 wfree(desc);
2473 } else {
2474 autoSelectText(tPtr, 2);
2476 break;
2477 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2478 tPtr->lastClickTime = event->xbutton.time;
2479 autoSelectText(tPtr, 3);
2480 break;
2483 if (!tPtr->flags.focused) {
2484 WMSetFocusToWidget(tPtr);
2485 tPtr->flags.focused = True;
2486 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2488 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2489 WMDragImageFromView(tPtr->view, event);
2490 break;
2493 tPtr->lastClickTime = event->xbutton.time;
2494 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2495 paintText(tPtr);
2498 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2499 WMScrollText(tPtr, 16);
2500 break;
2503 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2504 WMScrollText(tPtr, -16);
2505 break;
2508 if (event->xbutton.button == Button2) {
2509 char *text = NULL;
2510 int n;
2512 if (!tPtr->flags.editable) {
2513 XBell(dpy, 0);
2514 break;
2517 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2518 event->xbutton.time, pasteText, NULL)) {
2520 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2521 tPtr->flags.waitingForSelection = 0;
2523 if (text) {
2524 text[n] = 0;
2526 if (tPtr->parser) {
2527 (tPtr->parser) (tPtr, (void *)text);
2528 layOutDocument(tPtr);
2529 } else
2530 insertTextInteractively(tPtr, text, n);
2532 XFree(text);
2533 #if 0
2534 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2535 (void *)WMInsertTextEvent);
2536 #endif
2537 updateCursorPosition(tPtr);
2538 paintText(tPtr);
2540 } else {
2541 tPtr->flags.waitingForSelection = True;
2544 break;
2547 case ButtonRelease:
2548 if (tPtr->flags.pointerGrabbed) {
2549 tPtr->flags.pointerGrabbed = False;
2550 XUngrabPointer(dpy, CurrentTime);
2551 break;
2554 if (tPtr->flags.waitingForSelection)
2555 break;
2557 if (WMIsDraggingFromView(tPtr->view))
2558 WMDragImageFromView(tPtr->view, event);
2563 static void handleEvents(XEvent * event, void *data)
2565 Text *tPtr = (Text *) data;
2567 switch (event->type) {
2568 case Expose:
2570 if (event->xexpose.count != 0)
2571 break;
2573 if (tPtr->hS) {
2574 if (!(W_VIEW(tPtr->hS))->flags.realized)
2575 WMRealizeWidget(tPtr->hS);
2578 if (tPtr->vS) {
2579 if (!(W_VIEW(tPtr->vS))->flags.realized)
2580 WMRealizeWidget(tPtr->vS);
2583 if (tPtr->ruler) {
2584 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2585 WMRealizeWidget(tPtr->ruler);
2589 if (!tPtr->db)
2590 textDidResize(tPtr->view->delegate, tPtr->view);
2592 paintText(tPtr);
2593 break;
2595 case FocusIn:
2596 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2597 != tPtr->view)
2598 return;
2599 tPtr->flags.focused = True;
2600 #if DO_BLINK
2601 if (tPtr->flags.editable && !tPtr->timerID) {
2602 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2604 #endif
2606 break;
2608 case FocusOut:
2609 tPtr->flags.focused = False;
2610 paintText(tPtr);
2611 #if DO_BLINK
2612 if (tPtr->timerID) {
2613 WMDeleteTimerHandler(tPtr->timerID);
2614 tPtr->timerID = NULL;
2616 #endif
2617 break;
2619 case DestroyNotify:
2620 clearText(tPtr);
2621 if (tPtr->db)
2622 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2623 if (tPtr->gfxItems)
2624 WMEmptyArray(tPtr->gfxItems);
2625 #if DO_BLINK
2626 if (tPtr->timerID)
2627 WMDeleteTimerHandler(tPtr->timerID);
2628 #endif
2629 WMReleaseFont(tPtr->dFont);
2630 WMReleaseColor(tPtr->dColor);
2631 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2632 WMRemoveNotificationObserver(tPtr);
2634 WMFreeArray(tPtr->xdndSourceTypes);
2635 WMFreeArray(tPtr->xdndDestinationTypes);
2637 wfree(tPtr);
2639 break;
2644 static void insertPlainText(Text * tPtr, const char *text)
2646 const char *start, *mark;
2647 void *tb = NULL;
2649 start = text;
2650 while (start) {
2651 mark = strchr(start, '\n');
2652 if (mark) {
2653 tb = WMCreateTextBlockWithText(tPtr,
2654 start, tPtr->dFont,
2655 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2656 start = mark + 1;
2657 tPtr->flags.first = True;
2658 } else {
2659 if (start && strlen(start)) {
2660 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2661 tPtr->dColor, tPtr->flags.first, strlen(start));
2662 } else
2663 tb = NULL;
2664 tPtr->flags.first = False;
2665 start = mark;
2668 if (tPtr->flags.prepend)
2669 WMPrependTextBlock(tPtr, tb);
2670 else
2671 WMAppendTextBlock(tPtr, tb);
2675 static void rulerMoveCallBack(WMWidget * w, void *self)
2677 Text *tPtr = (Text *) self;
2679 /* Parameter not used, but tell the compiler that it is ok */
2680 (void) w;
2682 if (!tPtr)
2683 return;
2684 if (W_CLASS(tPtr) != WC_Text)
2685 return;
2687 paintText(tPtr);
2690 static void rulerReleaseCallBack(WMWidget * w, void *self)
2692 Text *tPtr = (Text *) self;
2694 /* Parameter not used, but tell the compiler that it is ok */
2695 (void) w;
2697 if (!tPtr)
2698 return;
2699 if (W_CLASS(tPtr) != WC_Text)
2700 return;
2702 WMThawText(tPtr);
2703 return;
2706 static WMArray *dropDataTypes(WMView * self)
2708 return ((Text *) self->self)->xdndSourceTypes;
2711 static WMDragOperationType wantedDropOperation(WMView * self)
2713 /* Parameter not used, but tell the compiler that it is ok */
2714 (void) self;
2716 return WDOperationCopy;
2719 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2721 /* Parameter not used, but tell the compiler that it is ok */
2722 (void) self;
2724 return (allowedOperation == WDOperationCopy);
2727 static WMData *fetchDragData(WMView * self, char *type)
2729 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2730 char *desc;
2731 WMData *data;
2733 if (strcmp(type, "text/plain")) {
2734 if (!tb)
2735 return NULL;
2737 desc = wmalloc(tb->used + 1);
2738 memcpy(desc, tb->text, tb->used);
2739 desc[tb->used] = 0;
2740 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2742 wfree(desc);
2744 return data;
2747 return NULL;
2750 static WMDragSourceProcs _DragSourceProcs = {
2751 dropDataTypes,
2752 wantedDropOperation,
2753 NULL,
2754 acceptDropOperation,
2755 NULL,
2756 NULL,
2757 fetchDragData
2760 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2762 /* Parameter not used, but tell the compiler that it is ok */
2763 (void) request;
2764 (void) sourceDataTypes;
2766 return ((Text *) self->self)->xdndDestinationTypes;
2769 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2771 /* Parameter not used, but tell the compiler that it is ok */
2772 (void) self;
2773 (void) request;
2774 (void) sourceDataTypes;
2776 return WDOperationCopy;
2779 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2781 WMText *tPtr = (WMText *) self->self;
2782 WMData *data;
2783 char *colorName;
2784 WMColor *color;
2786 /* Parameter not used, but tell the compiler that it is ok */
2787 (void) operations;
2788 (void) dropLocation;
2790 if (tPtr) {
2792 /* only one required type, implies only one drop data */
2794 /* get application/X-color if any */
2795 data = (WMData *) WMPopFromArray(dropData);
2796 if (data != NULL) {
2797 colorName = (char *)WMDataBytes(data);
2798 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2800 if (color) {
2801 WMSetTextSelectionColor(tPtr, color);
2802 WMReleaseColor(color);
2808 static WMDragDestinationProcs _DragDestinationProcs = {
2809 NULL,
2810 requiredDataTypes,
2811 allowedOperation,
2812 NULL,
2813 performDragOperation,
2814 NULL
2817 static char *getStream(WMText * tPtr, int sel, int array)
2819 TextBlock *tb = NULL;
2820 char *text = NULL;
2821 unsigned long where = 0;
2823 if (!tPtr)
2824 return NULL;
2826 if (!(tb = tPtr->firstTextBlock))
2827 return NULL;
2829 if (tPtr->writer) {
2830 (tPtr->writer) (tPtr, (void *)text);
2831 return text;
2834 tb = tPtr->firstTextBlock;
2835 while (tb) {
2837 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2839 if (!sel || (tb->graphic && tb->selected)) {
2841 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2842 && tb != tPtr->firstTextBlock) {
2843 text = wrealloc(text, where + 1);
2844 text[where++] = '\n';
2847 if (tb->blank)
2848 goto _gSnext;
2850 if (tb->graphic && array) {
2851 text = wrealloc(text, where + 4);
2852 text[where++] = 0xFA;
2853 text[where++] = (tb->used >> 8) & 0x0ff;
2854 text[where++] = tb->used & 0x0ff;
2855 text[where++] = tb->allocated; /* extra info */
2857 text = wrealloc(text, where + tb->used);
2858 memcpy(&text[where], tb->text, tb->used);
2859 where += tb->used;
2861 } else if (sel && tb->selected) {
2863 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2864 text = wrealloc(text, where + 1);
2865 text[where++] = '\n';
2868 if (tb->blank)
2869 goto _gSnext;
2871 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2872 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2873 where += tb->s_end - tb->s_begin;
2878 _gSnext: tb = tb->next;
2881 /* +1 for the end of string, let's be nice */
2882 text = wrealloc(text, where + 1);
2883 text[where] = 0;
2884 return text;
2887 static void releaseStreamObjects(void *data)
2889 if (data)
2890 wfree(data);
2893 static WMArray *getStreamObjects(WMText * tPtr, int sel)
2895 WMArray *array;
2896 WMData *data;
2897 char *stream;
2898 unsigned short len;
2899 char *start, *fa, *desc;
2901 stream = getStream(tPtr, sel, 1);
2902 if (!stream)
2903 return NULL;
2905 array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2907 start = stream;
2908 while (start) {
2910 fa = strchr(start, 0xFA);
2911 if (fa) {
2912 if ((int)(fa - start) > 0) {
2913 desc = start;
2914 desc[(int)(fa - start)] = 0;
2915 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2916 WMSetDataFormat(data, TYPETEXT);
2917 WMAddToArray(array, (void *)data);
2920 len = *(fa + 1) * 0xff + *(fa + 2);
2921 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2922 WMSetDataFormat(data, *(fa + 3));
2923 WMAddToArray(array, (void *)data);
2924 start = fa + len + 4;
2926 } else {
2927 if (start && strlen(start)) {
2928 data = WMCreateDataWithBytes((void *)start, strlen(start));
2929 WMSetDataFormat(data, TYPETEXT);
2930 WMAddToArray(array, (void *)data);
2932 start = fa;
2936 wfree(stream);
2937 return array;
2940 #define XDND_TEXT_DATA_TYPE "text/plain"
2941 #define XDND_COLOR_DATA_TYPE "application/X-color"
2942 static WMArray *getXdndSourceTypeArray(void)
2944 WMArray *types = WMCreateArray(1);
2945 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2946 return types;
2949 static WMArray *getXdndDestinationTypeArray(void)
2951 WMArray *types = WMCreateArray(1);
2952 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2953 return types;
2956 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2958 Text *tPtr;
2959 Display *dpy;
2960 WMScreen *scr;
2961 XGCValues gcv;
2963 tPtr = wmalloc(sizeof(Text));
2964 tPtr->widgetClass = WC_Text;
2965 tPtr->view = W_CreateView(W_VIEW(parent));
2966 if (!tPtr->view) {
2967 perror("could not create text's view\n");
2968 wfree(tPtr);
2969 return NULL;
2972 dpy = tPtr->view->screen->display;
2973 scr = tPtr->view->screen;
2975 tPtr->view->self = tPtr;
2976 tPtr->view->attribs.cursor = scr->textCursor;
2977 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2978 W_ResizeView(tPtr->view, 250, 200);
2980 tPtr->dColor = WMBlackColor(scr);
2981 tPtr->fgColor = WMBlackColor(scr);
2982 tPtr->bgColor = WMWhiteColor(scr);
2983 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2985 gcv.graphics_exposures = False;
2986 gcv.foreground = W_PIXEL(scr->gray);
2987 gcv.background = W_PIXEL(scr->darkGray);
2988 gcv.fill_style = FillStippled;
2989 /* why not use scr->stipple here? */
2990 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2991 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2992 GCForeground | GCBackground | GCStipple
2993 | GCFillStyle | GCGraphicsExposures, &gcv);
2995 tPtr->ruler = NULL;
2996 tPtr->vS = NULL;
2997 tPtr->hS = NULL;
2999 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3001 tPtr->view->delegate = &_TextViewDelegate;
3003 tPtr->delegate = NULL;
3005 #if DO_BLINK
3006 tPtr->timerID = NULL;
3007 #endif
3009 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
3010 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
3012 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
3013 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
3015 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
3017 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3018 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3021 WMArray *types = WMCreateArray(2);
3022 WMAddToArray(types, "application/X-color");
3023 WMAddToArray(types, "application/X-image");
3024 WMRegisterViewForDraggedTypes(tPtr->view, types);
3025 WMFreeArray(types);
3028 /*WMAddNotificationObserver(fontChanged, tPtr,
3029 WMFontPanelDidChangeNotification, tPtr); */
3031 tPtr->firstTextBlock = NULL;
3032 tPtr->lastTextBlock = NULL;
3033 tPtr->currentTextBlock = NULL;
3034 tPtr->tpos = 0;
3036 tPtr->gfxItems = WMCreateArray(4);
3038 tPtr->parser = parser;
3039 tPtr->writer = writer;
3041 tPtr->sel.x = tPtr->sel.y = 2;
3042 tPtr->sel.w = tPtr->sel.h = 0;
3044 tPtr->clicked.x = tPtr->clicked.y = 2;
3046 tPtr->visible.x = tPtr->visible.y = 2;
3047 tPtr->visible.h = tPtr->view->size.height;
3048 tPtr->visible.w = tPtr->view->size.width - 4;
3050 tPtr->cursor.x = -23;
3052 tPtr->docWidth = 0;
3053 tPtr->docHeight = 0;
3054 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3055 tPtr->db = (Pixmap) NULL;
3056 tPtr->bgPixmap = NULL;
3058 tPtr->margins = WMGetRulerMargins(NULL);
3059 tPtr->margins->right = tPtr->visible.w;
3060 tPtr->nMargins = 1;
3062 tPtr->flags.rulerShown = False;
3063 tPtr->flags.monoFont = False;
3064 tPtr->flags.focused = False;
3065 tPtr->flags.editable = True;
3066 tPtr->flags.ownsSelection = False;
3067 tPtr->flags.pointerGrabbed = False;
3068 tPtr->flags.extendSelection = False;
3069 tPtr->flags.frozen = False;
3070 tPtr->flags.cursorShown = True;
3071 tPtr->flags.acceptsGraphic = False;
3072 tPtr->flags.horizOnDemand = False;
3073 tPtr->flags.needsLayOut = False;
3074 tPtr->flags.ignoreNewLine = False;
3075 tPtr->flags.indentNewLine = False;
3076 tPtr->flags.laidOut = False;
3077 tPtr->flags.ownsSelection = False;
3078 tPtr->flags.waitingForSelection = False;
3079 tPtr->flags.prepend = False;
3080 tPtr->flags.isOverGraphic = False;
3081 tPtr->flags.relief = WRSunken;
3082 tPtr->flags.isOverGraphic = 0;
3083 tPtr->flags.alignment = WALeft;
3084 tPtr->flags.first = True;
3086 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3087 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3089 return tPtr;
3092 void WMPrependTextStream(WMText * tPtr, const char *text)
3094 CHECK_CLASS(tPtr, WC_Text);
3096 if (!text) {
3097 if (tPtr->flags.ownsSelection)
3098 releaseSelection(tPtr);
3099 clearText(tPtr);
3100 updateScrollers(tPtr);
3101 return;
3104 tPtr->flags.prepend = True;
3105 if (text && tPtr->parser)
3106 (tPtr->parser) (tPtr, (void *)text);
3107 else
3108 insertPlainText(tPtr, text);
3110 tPtr->flags.needsLayOut = True;
3111 tPtr->tpos = 0;
3112 if (!tPtr->flags.frozen) {
3113 layOutDocument(tPtr);
3117 void WMAppendTextStream(WMText * tPtr, const char *text)
3119 CHECK_CLASS(tPtr, WC_Text);
3121 if (!text) {
3122 if (tPtr->flags.ownsSelection)
3123 releaseSelection(tPtr);
3124 clearText(tPtr);
3125 updateScrollers(tPtr);
3126 return;
3129 tPtr->flags.prepend = False;
3130 if (text && tPtr->parser)
3131 (tPtr->parser) (tPtr, (void *)text);
3132 else
3133 insertPlainText(tPtr, text);
3135 tPtr->flags.needsLayOut = True;
3136 if (tPtr->currentTextBlock) {
3137 if (tPtr->currentTextBlock->graphic)
3138 tPtr->tpos = 1;
3139 else
3140 tPtr->tpos = tPtr->currentTextBlock->used;
3143 if (!tPtr->flags.frozen) {
3144 layOutDocument(tPtr);
3148 char *WMGetTextStream(WMText * tPtr)
3150 CHECK_CLASS(tPtr, WC_Text);
3152 return getStream(tPtr, 0, 0);
3155 char *WMGetTextSelectedStream(WMText * tPtr)
3157 CHECK_CLASS(tPtr, WC_Text);
3159 return getStream(tPtr, 1, 0);
3162 WMArray *WMGetTextObjects(WMText * tPtr)
3164 CHECK_CLASS(tPtr, WC_Text);
3166 return getStreamObjects(tPtr, 0);
3169 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3171 CHECK_CLASS(tPtr, WC_Text);
3173 return getStreamObjects(tPtr, 1);
3176 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3178 CHECK_CLASS(tPtr, WC_Text);
3180 tPtr->delegate = delegate;
3183 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3184 const char *description, WMColor * color,
3185 unsigned short first, unsigned short extraInfo)
3187 TextBlock *tb;
3189 if (!w || !description || !color)
3190 return NULL;
3192 tb = wmalloc(sizeof(TextBlock));
3194 tb->text = wstrdup(description);
3195 tb->used = strlen(description);
3196 tb->blank = False;
3197 tb->d.widget = w;
3198 tb->color = WMRetainColor(color);
3199 tb->marginN = newMargin(tPtr, NULL);
3200 tb->allocated = extraInfo;
3201 tb->first = first;
3202 tb->kanji = False;
3203 tb->graphic = True;
3204 tb->object = True;
3205 tb->underlined = False;
3206 tb->selected = False;
3207 tb->script = 0;
3208 tb->sections = NULL;
3209 tb->nsections = 0;
3210 tb->prior = NULL;
3211 tb->next = NULL;
3213 return tb;
3216 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3217 const char *description, WMColor * color,
3218 unsigned short first, unsigned short extraInfo)
3220 TextBlock *tb;
3222 if (!p || !description || !color)
3223 return NULL;
3225 tb = wmalloc(sizeof(TextBlock));
3227 tb->text = wstrdup(description);
3228 tb->used = strlen(description);
3229 tb->blank = False;
3230 tb->d.pixmap = WMRetainPixmap(p);
3231 tb->color = WMRetainColor(color);
3232 tb->marginN = newMargin(tPtr, NULL);
3233 tb->allocated = extraInfo;
3234 tb->first = first;
3235 tb->kanji = False;
3236 tb->graphic = True;
3237 tb->object = False;
3238 tb->underlined = False;
3239 tb->selected = False;
3240 tb->script = 0;
3241 tb->sections = NULL;
3242 tb->nsections = 0;
3243 tb->prior = NULL;
3244 tb->next = NULL;
3246 return tb;
3249 void *WMCreateTextBlockWithText(WMText * tPtr, const char *text, WMFont * font, WMColor * color,
3250 unsigned short first, unsigned short len)
3252 TextBlock *tb;
3254 if (!font || !color)
3255 return NULL;
3257 tb = wmalloc(sizeof(TextBlock));
3259 tb->allocated = reqBlockSize(len);
3260 tb->text = (char *)wmalloc(tb->allocated);
3262 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3263 *tb->text = ' ';
3264 tb->used = 1;
3265 tb->blank = True;
3266 } else {
3267 memcpy(tb->text, text, len);
3268 tb->used = len;
3269 tb->blank = False;
3271 tb->text[tb->used] = 0;
3273 tb->d.font = WMRetainFont(font);
3274 tb->color = WMRetainColor(color);
3275 tb->marginN = newMargin(tPtr, NULL);
3276 tb->first = first;
3277 tb->kanji = False;
3278 tb->graphic = False;
3279 tb->underlined = False;
3280 tb->selected = False;
3281 tb->script = 0;
3282 tb->sections = NULL;
3283 tb->nsections = 0;
3284 tb->prior = NULL;
3285 tb->next = NULL;
3286 return tb;
3289 void
3290 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3291 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3293 TextBlock *tb = (TextBlock *) vtb;
3294 if (!tb)
3295 return;
3297 tb->first = first;
3298 tb->kanji = kanji;
3299 tb->underlined = underlined;
3300 tb->script = script;
3301 tb->marginN = newMargin(tPtr, margins);
3304 void
3305 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3306 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins *margins)
3308 TextBlock *tb = (TextBlock *) vtb;
3309 if (!tb)
3310 return;
3312 if (first)
3313 *first = tb->first;
3314 if (kanji)
3315 *kanji = tb->kanji;
3316 if (underlined)
3317 *underlined = tb->underlined;
3318 if (script)
3319 *script = tb->script;
3320 if (margins)
3321 *margins = tPtr->margins[tb->marginN];
3324 static int prepareTextBlock(WMText *tPtr, TextBlock *tb)
3326 if (tb->graphic) {
3327 if (tb->object) {
3328 WMWidget *w = tb->d.widget;
3329 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3330 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3331 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3334 WMAddToArray(tPtr->gfxItems, (void *)tb);
3335 tPtr->tpos = 1;
3337 } else {
3338 tPtr->tpos = tb->used;
3341 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3342 tb->next = tb->prior = NULL;
3343 tb->first = True;
3344 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3345 return 0;
3348 if (!tb->first) {
3349 tb->marginN = tPtr->currentTextBlock->marginN;
3352 return 1;
3356 void WMPrependTextBlock(WMText *tPtr, void *vtb)
3358 TextBlock *tb = (TextBlock *) vtb;
3360 if (!tb || !prepareTextBlock(tPtr, tb))
3361 return;
3363 tb->next = tPtr->currentTextBlock;
3364 tb->prior = tPtr->currentTextBlock->prior;
3365 if (tPtr->currentTextBlock->prior)
3366 tPtr->currentTextBlock->prior->next = tb;
3368 tPtr->currentTextBlock->prior = tb;
3369 if (!tb->prior)
3370 tPtr->firstTextBlock = tb;
3372 tPtr->currentTextBlock = tb;
3375 void WMAppendTextBlock(WMText *tPtr, void *vtb)
3377 TextBlock *tb = (TextBlock *) vtb;
3379 if (!tb || !prepareTextBlock(tPtr, tb))
3380 return;
3382 tb->next = tPtr->currentTextBlock->next;
3383 tb->prior = tPtr->currentTextBlock;
3384 if (tPtr->currentTextBlock->next)
3385 tPtr->currentTextBlock->next->prior = tb;
3387 tPtr->currentTextBlock->next = tb;
3389 if (!tb->next)
3390 tPtr->lastTextBlock = tb;
3392 tPtr->currentTextBlock = tb;
3395 void *WMRemoveTextBlock(WMText * tPtr)
3397 TextBlock *tb = NULL;
3399 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3400 return NULL;
3403 tb = tPtr->currentTextBlock;
3404 if (tb->graphic) {
3405 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3407 if (tb->object) {
3408 WMUnmapWidget(tb->d.widget);
3412 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3413 if (tPtr->currentTextBlock->next)
3414 tPtr->currentTextBlock->next->prior = NULL;
3416 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3417 tPtr->currentTextBlock = tPtr->firstTextBlock;
3419 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3420 tPtr->currentTextBlock->prior->next = NULL;
3421 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3422 tPtr->currentTextBlock = tPtr->lastTextBlock;
3423 } else {
3424 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3425 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3426 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3429 return (void *)tb;
3432 #if 0
3433 static void destroyWidget(WMWidget * widget)
3435 WMDestroyWidget(widget);
3436 // -- never do this -- wfree(widget);
3438 #endif
3440 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3442 TextBlock *tb = (TextBlock *) vtb;
3444 /* Parameter not used, but tell the compiler that it is ok */
3445 (void) tPtr;
3447 if (!tb)
3448 return;
3450 if (tb->graphic) {
3451 if (tb->object) {
3452 /* naturally, there's a danger to destroying widgets whose action
3453 * brings us here: ie. press a button to destroy it...
3454 * need to find a safer way. till then... this stays commented out */
3455 /* 5 months later... destroy it 10 seconds after now which should
3456 * be enough time for the widget's action to be completed... :-) */
3457 /* This is a bad assumption. Just destroy the widget here.
3458 * if the caller needs it, it can protect it with W_RetainView()
3459 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3460 WMDestroyWidget(tb->d.widget);
3461 } else {
3462 WMReleasePixmap(tb->d.pixmap);
3464 } else {
3465 WMReleaseFont(tb->d.font);
3468 WMReleaseColor(tb->color);
3469 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3470 if (tb->sections)
3471 wfree(tb->sections);
3472 wfree(tb->text);
3473 wfree(tb);
3476 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3478 if (tPtr->fgColor)
3479 WMReleaseColor(tPtr->fgColor);
3481 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3483 paintText(tPtr);
3486 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3488 if (tPtr->bgColor)
3489 WMReleaseColor(tPtr->bgColor);
3491 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3492 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3494 paintText(tPtr);
3497 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3499 if (tPtr->bgPixmap)
3500 WMReleasePixmap(tPtr->bgPixmap);
3502 if (pixmap)
3503 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3504 else
3505 tPtr->bgPixmap = NULL;
3508 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3510 tPtr->flags.relief = relief;
3511 textDidResize(tPtr->view->delegate, tPtr->view);
3514 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3516 if (shouldhave && !tPtr->hS) {
3517 tPtr->hS = WMCreateScroller(tPtr);
3518 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3519 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3520 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3521 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3522 WMMapWidget(tPtr->hS);
3523 } else if (!shouldhave && tPtr->hS) {
3524 WMUnmapWidget(tPtr->hS);
3525 WMDestroyWidget(tPtr->hS);
3526 tPtr->hS = NULL;
3529 tPtr->hpos = 0;
3530 tPtr->prevHpos = 0;
3531 textDidResize(tPtr->view->delegate, tPtr->view);
3534 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3536 if (shouldhave && !tPtr->ruler) {
3537 tPtr->ruler = WMCreateRuler(tPtr);
3538 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3539 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3540 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3541 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3542 } else if (!shouldhave && tPtr->ruler) {
3543 WMShowTextRuler(tPtr, False);
3544 WMDestroyWidget(tPtr->ruler);
3545 tPtr->ruler = NULL;
3547 textDidResize(tPtr->view->delegate, tPtr->view);
3550 void WMShowTextRuler(WMText * tPtr, Bool show)
3552 if (!tPtr->ruler)
3553 return;
3555 if (tPtr->flags.monoFont)
3556 show = False;
3558 tPtr->flags.rulerShown = show;
3559 if (show) {
3560 WMMapWidget(tPtr->ruler);
3561 } else {
3562 WMUnmapWidget(tPtr->ruler);
3565 textDidResize(tPtr->view->delegate, tPtr->view);
3568 Bool WMGetTextRulerShown(WMText * tPtr)
3570 if (!tPtr->ruler)
3571 return False;
3573 return tPtr->flags.rulerShown;
3576 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3578 if (shouldhave && !tPtr->vS) {
3579 tPtr->vS = WMCreateScroller(tPtr);
3580 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3581 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3582 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3583 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3584 WMMapWidget(tPtr->vS);
3585 } else if (!shouldhave && tPtr->vS) {
3586 WMUnmapWidget(tPtr->vS);
3587 WMDestroyWidget(tPtr->vS);
3588 tPtr->vS = NULL;
3591 tPtr->vpos = 0;
3592 tPtr->prevVpos = 0;
3593 textDidResize(tPtr->view->delegate, tPtr->view);
3596 Bool WMScrollText(WMText * tPtr, int amount)
3598 Bool scroll = False;
3600 if (amount == 0 || !tPtr->view->flags.realized)
3601 return False;
3603 if (amount < 0) {
3604 if (tPtr->vpos > 0) {
3605 if (tPtr->vpos > abs(amount))
3606 tPtr->vpos += amount;
3607 else
3608 tPtr->vpos = 0;
3609 scroll = True;
3611 } else {
3612 int limit = tPtr->docHeight - tPtr->visible.h;
3613 if (tPtr->vpos < limit) {
3614 if (tPtr->vpos < limit - amount)
3615 tPtr->vpos += amount;
3616 else
3617 tPtr->vpos = limit;
3618 scroll = True;
3622 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3623 updateScrollers(tPtr);
3624 paintText(tPtr);
3626 tPtr->prevVpos = tPtr->vpos;
3627 return scroll;
3630 Bool WMPageText(WMText * tPtr, Bool direction)
3632 if (!tPtr->view->flags.realized)
3633 return False;
3635 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3638 void WMSetTextEditable(WMText * tPtr, Bool editable)
3640 tPtr->flags.editable = editable;
3643 int WMGetTextEditable(WMText * tPtr)
3645 return tPtr->flags.editable;
3648 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3650 tPtr->flags.indentNewLine = indent;
3653 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3655 tPtr->flags.ignoreNewLine = ignore;
3658 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3660 return tPtr->flags.ignoreNewLine;
3663 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3665 if (mono) {
3666 if (tPtr->flags.rulerShown)
3667 WMShowTextRuler(tPtr, False);
3668 if (tPtr->flags.alignment != WALeft)
3669 tPtr->flags.alignment = WALeft;
3672 tPtr->flags.monoFont = mono;
3673 textDidResize(tPtr->view->delegate, tPtr->view);
3676 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3678 return tPtr->flags.monoFont;
3681 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3683 if (tPtr->dFont)
3684 WMReleaseFont(tPtr->dFont);
3686 if (font) {
3687 tPtr->dFont = WMRetainFont(font);
3688 } else {
3689 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3693 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3695 return WMRetainFont(tPtr->dFont);
3698 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3700 if (tPtr->dColor)
3701 WMReleaseColor(tPtr->dColor);
3703 if (color) {
3704 tPtr->dColor = WMRetainColor(color);
3705 } else {
3706 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3710 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3712 return tPtr->dColor;
3715 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3717 if (tPtr->flags.monoFont)
3718 tPtr->flags.alignment = WALeft;
3719 else
3720 tPtr->flags.alignment = alignment;
3721 WMThawText(tPtr);
3724 int WMGetTextInsertType(WMText * tPtr)
3726 return tPtr->flags.prepend;
3729 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3731 setSelectionProperty(tPtr, NULL, color, -1);
3734 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3736 TextBlock *tb;
3738 tb = tPtr->currentTextBlock;
3740 if (!tb || !tPtr->flags.ownsSelection)
3741 return NULL;
3743 if (!tb->selected)
3744 return NULL;
3746 return tb->color;
3749 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3751 setSelectionProperty(tPtr, font, NULL, -1);
3754 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3756 TextBlock *tb;
3758 tb = tPtr->currentTextBlock;
3760 if (!tb || !tPtr->flags.ownsSelection)
3761 return NULL;
3763 if (!tb->selected)
3764 return NULL;
3766 if (tb->graphic) {
3767 tb = getFirstNonGraphicBlockFor(tb, 1);
3768 if (!tb)
3769 return NULL;
3771 return (tb->selected ? tb->d.font : NULL);
3774 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3776 /* // check this */
3777 if (underlined != 0 && underlined != 1)
3778 return;
3780 setSelectionProperty(tPtr, NULL, NULL, underlined);
3783 int WMGetTextSelectionUnderlined(WMText * tPtr)
3785 TextBlock *tb;
3787 tb = tPtr->currentTextBlock;
3789 if (!tb || !tPtr->flags.ownsSelection)
3790 return 0;
3792 if (!tb->selected)
3793 return 0;
3795 return tb->underlined;
3798 void WMFreezeText(WMText * tPtr)
3800 tPtr->flags.frozen = True;
3803 void WMThawText(WMText * tPtr)
3805 tPtr->flags.frozen = False;
3807 if (tPtr->flags.monoFont) {
3808 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3809 TextBlock *tb;
3811 /* make sure to unmap widgets no matter where they are */
3812 /* they'll be later remapped if needed by paintText */
3813 for (j = 0; j < c; j++) {
3814 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3815 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3816 WMUnmapWidget(tb->d.widget);
3821 tPtr->flags.laidOut = False;
3822 layOutDocument(tPtr);
3823 updateScrollers(tPtr);
3824 paintText(tPtr);
3825 tPtr->flags.needsLayOut = False;
3829 /* find first occurence of a string */
3830 static const char *mystrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3832 const char *ptr;
3834 if (!haystack || !needle || !end)
3835 return NULL;
3837 for (ptr = haystack; ptr < end; ptr++) {
3838 if (caseSensitive) {
3839 if (*ptr == *needle && !strncmp(ptr, needle, len))
3840 return ptr;
3842 } else {
3843 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3844 return ptr;
3848 return NULL;
3851 /* find last occurence of a string */
3852 static const char *mystrrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3854 const char *ptr;
3856 if (!haystack || !needle || !end)
3857 return NULL;
3859 for (ptr = haystack - 2; ptr > end; ptr--) {
3860 if (caseSensitive) {
3861 if (*ptr == *needle && !strncmp(ptr, needle, len))
3862 return ptr;
3863 } else {
3864 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3865 return ptr;
3869 return NULL;
3872 Bool WMFindInTextStream(WMText * tPtr, const char *needle, Bool direction, Bool caseSensitive)
3874 TextBlock *tb;
3875 const char *mark = NULL;
3876 unsigned short pos;
3878 #if 0
3879 if (!(tb = tPtr->currentTextBlock)) {
3880 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3881 return False;
3883 } else {
3884 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3885 tb = (direction>0) ? tb->next : tb->prior; */
3886 if (tb != tPtr->lastTextBlock)
3887 tb = tb->prior;
3889 #endif
3890 tb = tPtr->currentTextBlock;
3891 pos = tPtr->tpos;
3893 while (tb) {
3894 if (!tb->graphic) {
3896 if (direction > 0) {
3897 if (pos + 1 < tb->used)
3898 pos++;
3900 if (tb->used - pos > 0 && pos > 0) {
3901 mark = mystrstr(&tb->text[pos], needle,
3902 strlen(needle), &tb->text[tb->used], caseSensitive);
3904 } else {
3905 tb = tb->next;
3906 pos = 0;
3907 continue;
3910 } else {
3911 if (pos - 1 > 0)
3912 pos--;
3914 if (pos > 0) {
3915 mark = mystrrstr(&tb->text[pos], needle,
3916 strlen(needle), tb->text, caseSensitive);
3917 } else {
3918 tb = tb->prior;
3919 if (!tb)
3920 return False;
3921 pos = tb->used;
3922 continue;
3926 if (mark) {
3927 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3929 tPtr->tpos = (int)(mark - tb->text);
3930 tPtr->currentTextBlock = tb;
3931 updateCursorPosition(tPtr);
3932 tPtr->sel.y = tPtr->cursor.y + 5;
3933 tPtr->sel.h = tPtr->cursor.h - 10;
3934 tPtr->sel.x = tPtr->cursor.x + 1;
3935 tPtr->sel.w = WMIN(WMWidthOfString(font,
3936 &tb->text[tPtr->tpos], strlen(needle)),
3937 tPtr->docWidth - tPtr->sel.x);
3938 tPtr->flags.ownsSelection = True;
3939 paintText(tPtr);
3941 return True;
3945 tb = (direction > 0) ? tb->next : tb->prior;
3946 if (tb) {
3947 pos = (direction > 0) ? 0 : tb->used;
3951 return False;
3954 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3956 if (!tPtr->flags.ownsSelection)
3957 return False;
3959 removeSelection(tPtr);
3961 if (replacement) {
3962 insertTextInteractively(tPtr, replacement, strlen(replacement));
3963 updateCursorPosition(tPtr);
3964 paintText(tPtr);
3967 return True;