Improve dockapp recognition
[wmaker-crm.git] / WINGs / wtext.c
blob01772c4a2f86d49f5dca05808103ca0268ca3a56
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 char *text;
620 int len, y, c, s, done = False, prev_y = -23, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
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 prev_y = tb->sections[s]._y;
717 len = tb->sections[s].end - tb->sections[s].begin;
718 text = &(tb->text[tb->sections[s].begin]);
719 y = tb->sections[s].y - tPtr->vpos;
720 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
722 if (!tPtr->flags.monoFont && tb->underlined) {
723 XDrawLine(dpy, tPtr->db, WMColorGC(color),
724 tb->sections[s].x - tPtr->hpos,
725 y + font->y + 1,
726 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
729 tb = (!done ? tb->next : NULL);
732 /* now , show all graphic items that can be viewed */
733 c = WMGetArrayItemCount(tPtr->gfxItems);
734 if (c > 0 && !tPtr->flags.monoFont) {
735 int j, h;
737 for (j = 0; j < c; j++) {
738 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
740 /* if it's not viewable, and mapped, unmap it */
741 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
742 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
744 if (tb->object) {
745 if ((W_VIEW(tb->d.widget))->flags.mapped) {
746 WMUnmapWidget(tb->d.widget);
749 } else {
750 /* if it's viewable, and not mapped, map it */
751 if (tb->object) {
752 W_View *view = W_VIEW(tb->d.widget);
754 if (!view->flags.realized)
755 WMRealizeWidget(tb->d.widget);
756 if (!view->flags.mapped) {
757 XMapWindow(view->screen->display, view->window);
758 XFlush(view->screen->display);
759 view->flags.mapped = 1;
763 if (tb->object) {
764 WMMoveWidget(tb->d.widget,
765 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
766 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
767 h = WMWidgetHeight(tb->d.widget) + 1;
769 } else {
770 WMDrawPixmap(tb->d.pixmap, tPtr->db,
771 tb->sections[0].x - tPtr->hpos,
772 tb->sections[0].y - tPtr->vpos);
773 h = tb->d.pixmap->height + 1;
777 if (tPtr->flags.ownsSelection) {
778 XRectangle rect;
780 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
781 Drawable d = (0 && tb->object ?
782 (WMWidgetView(tb->d.widget))->window : tPtr->db);
784 tb->selected = True;
785 XFillRectangle(dpy, d, tPtr->stippledGC,
786 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
787 rect.x, rect.y, rect.width, rect.height);
791 if (!tPtr->flags.monoFont && tb->underlined) {
792 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
793 tb->sections[0].x - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos,
795 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
796 tb->sections[0].y + h - tPtr->vpos);
802 _copy_area:
803 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
804 int y = tPtr->cursor.y - tPtr->vpos;
805 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
806 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
809 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
810 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
812 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
814 if (tPtr->ruler && tPtr->flags.rulerShown)
815 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
819 static void mouseOverObject(Text * tPtr, int x, int y)
821 TextBlock *tb;
822 Bool result = False;
824 x -= tPtr->visible.x;
825 x += tPtr->hpos;
826 y -= tPtr->visible.y;
827 y += tPtr->vpos;
829 if (tPtr->flags.ownsSelection) {
830 if (tPtr->sel.x <= x
831 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
832 tPtr->flags.isOverGraphic = 1;
833 result = True;
837 if (!result) {
838 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
840 if (c < 1)
841 tPtr->flags.isOverGraphic = 0;
843 for (j = 0; j < c; j++) {
844 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
846 if (!tb || !tb->sections) {
847 tPtr->flags.isOverGraphic = 0;
848 return;
851 if (!tb->object) {
852 if (tb->sections[0].x <= x
853 && tb->sections[0].y <= y
854 && tb->sections[0].x + tb->sections[0].w >= x
855 && tb->sections[0].y + tb->d.pixmap->height >= y) {
856 tPtr->flags.isOverGraphic = 3;
857 result = True;
858 break;
865 if (!result)
866 tPtr->flags.isOverGraphic = 0;
868 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
870 XSetWindowAttributes attribs;
871 attribs.cursor = tPtr->view->attribs.cursor;
872 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
876 #if DO_BLINK
878 static void blinkCursor(void *data)
880 Text *tPtr = (Text *) data;
882 if (tPtr->flags.cursorShown) {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
884 } else {
885 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
887 paintText(tPtr);
888 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
890 #endif
892 static void updateCursorPosition(Text * tPtr)
894 TextBlock *tb = NULL;
895 int x, y, h, s;
897 if (tPtr->flags.needsLayOut)
898 layOutDocument(tPtr);
900 if (!(tb = tPtr->currentTextBlock)) {
901 if (!(tb = tPtr->firstTextBlock)) {
902 WMFont *font = tPtr->dFont;
903 tPtr->tpos = 0;
904 tPtr->cursor.h = font->height + abs(font->height - font->y);
906 tPtr->cursor.y = 2;
907 tPtr->cursor.x = 2;
908 return;
912 if (tb->blank) {
913 tPtr->tpos = 0;
914 y = tb->sections[0].y;
915 h = tb->sections[0].h;
916 x = tb->sections[0].x;
918 } else if (tb->graphic) {
919 y = tb->sections[0].y;
920 h = tb->sections[0].h;
921 x = tb->sections[0].x;
922 if (tPtr->tpos == 1)
923 x += tb->sections[0].w;
925 } else {
926 if (tPtr->tpos > tb->used)
927 tPtr->tpos = tb->used;
929 for (s = 0; s < tb->nsections - 1; s++) {
931 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
932 break;
935 y = tb->sections[s]._y;
936 h = tb->sections[s].h;
937 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
938 &tb->text[tb->sections[s].begin],
939 tPtr->tpos - tb->sections[s].begin);
942 tPtr->cursor.y = y;
943 tPtr->cursor.h = h;
944 tPtr->cursor.x = x;
946 /* scroll the bars if the cursor is not visible */
947 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
948 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
949 tPtr->vpos +=
950 (tPtr->cursor.y + tPtr->cursor.h + 10
951 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
952 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
953 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
958 updateScrollers(tPtr);
961 static void cursorToTextPosition(Text * tPtr, int x, int y)
963 TextBlock *tb = NULL;
964 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
965 char *text;
967 if (tPtr->flags.needsLayOut)
968 layOutDocument(tPtr);
970 y += (tPtr->vpos - tPtr->visible.y);
971 if (y < 0)
972 y = 0;
974 x -= (tPtr->visible.x - 2);
975 if (x < 0)
976 x = 0;
978 /* clicked is relative to document, not window... */
979 tPtr->clicked.x = x;
980 tPtr->clicked.y = y;
982 if (!(tb = tPtr->currentTextBlock)) {
983 if (!(tb = tPtr->firstTextBlock)) {
984 WMFont *font = tPtr->dFont;
985 tPtr->tpos = 0;
986 tPtr->cursor.h = font->height + abs(font->height - font->y);
987 tPtr->cursor.y = 2;
988 tPtr->cursor.x = 2;
989 return;
993 /* first, which direction? Most likely, newly clicked
994 position will be close to previous */
995 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
996 return;
998 s = (dir ? 0 : tb->nsections - 1);
999 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
1000 goto _doneV;
1003 /* get the first (or last) section of the TextBlock that
1004 lies about the vertical click point */
1005 done = False;
1006 while (!done && tb) {
1008 if (tPtr->flags.monoFont && tb->graphic) {
1009 if ((dir ? tb->next : tb->prior))
1010 tb = (dir ? tb->next : tb->prior);
1011 continue;
1014 s = (dir ? 0 : tb->nsections - 1);
1015 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1017 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1018 done = True;
1019 } else {
1020 dir ? s++ : s--;
1024 if (!done) {
1025 if ((dir ? tb->next : tb->prior)) {
1026 tb = (dir ? tb->next : tb->prior);
1027 } else {
1028 pos = tb->used;
1029 break; /* goto _doneH; */
1034 if (s < 0 || s >= tb->nsections) {
1035 s = (dir ? tb->nsections - 1 : 0);
1038 _doneV:
1039 /* we have the line, which TextBlock on that line is it? */
1040 pos = (dir ? 0 : tb->sections[s].begin);
1041 if (tPtr->flags.monoFont && tb->graphic) {
1042 TextBlock *hold = tb;
1043 tb = getFirstNonGraphicBlockFor(hold, dir);
1045 if (!tb) {
1046 tPtr->tpos = 0;
1047 tb = hold;
1048 s = 0;
1049 goto _doNothing;
1053 if (tb->blank)
1054 _w = 0;
1056 _y = tb->sections[s]._y;
1058 while (tb) {
1060 if (tPtr->flags.monoFont && tb->graphic) {
1061 tb = (dir ? tb->next : tb->prior);
1062 continue;
1065 if (dir) {
1066 if (tb->graphic) {
1067 if (tb->object)
1068 _w = WMWidgetWidth(tb->d.widget) - 5;
1069 else
1070 _w = tb->d.pixmap->width - 5;
1072 if (tb->sections[0].x + _w >= x)
1073 break;
1074 } else {
1075 text = &(tb->text[tb->sections[s].begin]);
1076 len = tb->sections[s].end - tb->sections[s].begin;
1077 _w = WMWidthOfString(tb->d.font, text, len);
1078 if (tb->sections[s].x + _w >= x)
1079 break;
1082 } else {
1083 if (tb->sections[s].x <= x)
1084 break;
1087 if ((dir ? tb->next : tb->prior)) {
1088 TextBlock *nxt = (dir ? tb->next : tb->prior);
1089 if (tPtr->flags.monoFont && nxt->graphic) {
1090 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1091 if (!nxt) {
1092 pos = (dir ? 0 : tb->sections[s].begin);
1093 tPtr->cursor.x = tb->sections[s].x;
1094 goto _doneH;
1098 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1099 /* this must be the last/first on this line. stop */
1100 pos = (dir ? tb->sections[s].end : 0);
1101 tPtr->cursor.x = tb->sections[s].x;
1102 if (!tb->blank) {
1103 if (tb->graphic) {
1104 if (tb->object)
1105 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1106 else
1107 tPtr->cursor.x += tb->d.pixmap->width;
1108 } else if (pos > tb->sections[s].begin) {
1109 tPtr->cursor.x +=
1110 WMWidthOfString(tb->d.font,
1111 &(tb->text[tb->sections[s].begin]),
1112 pos - tb->sections[s].begin);
1115 goto _doneH;
1119 if ((dir ? tb->next : tb->prior)) {
1120 tb = (dir ? tb->next : tb->prior);
1121 } else {
1122 done = True;
1123 break;
1126 if (tb)
1127 s = (dir ? 0 : tb->nsections - 1);
1130 /* we have said TextBlock, now where within it? */
1131 if (tb) {
1132 if (tb->graphic) {
1133 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1135 tPtr->cursor.x = tb->sections[0].x;
1137 if (x > tPtr->cursor.x + gw / 2) {
1138 pos = 1;
1139 tPtr->cursor.x += gw;
1140 } else {
1141 printf("first %d\n", tb->first);
1142 if (tb->prior) {
1143 if (tb->prior->graphic)
1144 pos = 1;
1145 else
1146 pos = tb->prior->used;
1147 tb = tb->prior;
1148 } else
1149 pos = 0;
1153 s = 0;
1154 goto _doneH;
1156 } else {
1157 WMFont *f = tb->d.font;
1158 len = tb->sections[s].end - tb->sections[s].begin;
1159 text = &(tb->text[tb->sections[s].begin]);
1161 _w = x - tb->sections[s].x;
1162 pos = 0;
1164 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1165 pos++;
1167 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1169 pos += tb->sections[s].begin;
1173 _doneH:
1174 if (tb->graphic) {
1175 tPtr->tpos = (pos <= 1) ? pos : 0;
1176 } else {
1177 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1179 _doNothing:
1180 if (!tb)
1181 printf("...for this app will surely crash :-)\n");
1183 tPtr->currentTextBlock = tb;
1184 tPtr->cursor.h = tb->sections[s].h;
1185 tPtr->cursor.y = tb->sections[s]._y;
1187 /* scroll the bars if the cursor is not visible */
1188 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1189 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1190 tPtr->vpos +=
1191 (tPtr->cursor.y + tPtr->cursor.h + 10
1192 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1193 updateScrollers(tPtr);
1194 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1195 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1196 updateScrollers(tPtr);
1203 static void updateScrollers(Text * tPtr)
1206 if (tPtr->flags.frozen)
1207 return;
1209 if (tPtr->vS) {
1210 if (tPtr->docHeight <= tPtr->visible.h) {
1211 WMSetScrollerParameters(tPtr->vS, 0, 1);
1212 tPtr->vpos = 0;
1213 } else {
1214 float hmax = (float)(tPtr->docHeight);
1215 WMSetScrollerParameters(tPtr->vS,
1216 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1217 (float)tPtr->visible.h / hmax);
1219 } else
1220 tPtr->vpos = 0;
1222 if (tPtr->hS) {
1223 if (tPtr->docWidth <= tPtr->visible.w) {
1224 WMSetScrollerParameters(tPtr->hS, 0, 1);
1225 tPtr->hpos = 0;
1226 } else {
1227 float wmax = (float)(tPtr->docWidth);
1228 WMSetScrollerParameters(tPtr->hS,
1229 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1230 (float)tPtr->visible.w / wmax);
1232 } else
1233 tPtr->hpos = 0;
1236 static void scrollersCallBack(WMWidget * w, void *self)
1238 Text *tPtr = (Text *) self;
1239 Bool scroll = False;
1240 int which;
1242 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1243 return;
1245 if (w == tPtr->vS) {
1246 int height;
1247 height = tPtr->visible.h;
1249 which = WMGetScrollerHitPart(tPtr->vS);
1250 switch (which) {
1252 case WSDecrementLine:
1253 if (tPtr->vpos > 0) {
1254 if (tPtr->vpos > 16)
1255 tPtr->vpos -= 16;
1256 else
1257 tPtr->vpos = 0;
1258 scroll = True;
1260 break;
1262 case WSIncrementLine:{
1263 int limit = tPtr->docHeight - height;
1264 if (tPtr->vpos < limit) {
1265 if (tPtr->vpos < limit - 16)
1266 tPtr->vpos += 16;
1267 else
1268 tPtr->vpos = limit;
1269 scroll = True;
1272 break;
1274 case WSDecrementPage:
1275 if (((int)tPtr->vpos - (int)height) >= 0)
1276 tPtr->vpos -= height;
1277 else
1278 tPtr->vpos = 0;
1280 scroll = True;
1281 break;
1283 case WSIncrementPage:
1284 tPtr->vpos += height;
1285 if (tPtr->vpos > (tPtr->docHeight - height))
1286 tPtr->vpos = tPtr->docHeight - height;
1287 scroll = True;
1288 break;
1290 case WSKnob:
1291 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1292 * (float)(tPtr->docHeight - height);
1293 scroll = True;
1294 break;
1296 case WSKnobSlot:
1297 case WSNoPart:
1298 break;
1300 scroll = (tPtr->vpos != tPtr->prevVpos);
1301 tPtr->prevVpos = tPtr->vpos;
1304 if (w == tPtr->hS) {
1305 int width = tPtr->visible.w;
1307 which = WMGetScrollerHitPart(tPtr->hS);
1308 switch (which) {
1310 case WSDecrementLine:
1311 if (tPtr->hpos > 0) {
1312 if (tPtr->hpos > 16)
1313 tPtr->hpos -= 16;
1314 else
1315 tPtr->hpos = 0;
1316 scroll = True;
1318 break;
1320 case WSIncrementLine:{
1321 int limit = tPtr->docWidth - width;
1322 if (tPtr->hpos < limit) {
1323 if (tPtr->hpos < limit - 16)
1324 tPtr->hpos += 16;
1325 else
1326 tPtr->hpos = limit;
1327 scroll = True;
1330 break;
1332 case WSDecrementPage:
1333 if (((int)tPtr->hpos - (int)width) >= 0)
1334 tPtr->hpos -= width;
1335 else
1336 tPtr->hpos = 0;
1338 scroll = True;
1339 break;
1341 case WSIncrementPage:
1342 tPtr->hpos += width;
1343 if (tPtr->hpos > (tPtr->docWidth - width))
1344 tPtr->hpos = tPtr->docWidth - width;
1345 scroll = True;
1346 break;
1348 case WSKnob:
1349 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1350 * (float)(tPtr->docWidth - width);
1351 scroll = True;
1352 break;
1354 case WSKnobSlot:
1355 case WSNoPart:
1356 break;
1358 scroll = (tPtr->hpos != tPtr->prevHpos);
1359 tPtr->prevHpos = tPtr->hpos;
1362 if (scroll) {
1363 updateScrollers(tPtr);
1364 paintText(tPtr);
1368 typedef struct {
1369 TextBlock *tb;
1370 unsigned short begin, end; /* what part of the text block */
1371 } myLineItems;
1373 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1375 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1376 WMFont *font;
1377 char *text;
1378 TextBlock *tb, *tbsame = NULL;
1380 if (!items || nitems == 0)
1381 return 0;
1383 for (i = 0; i < nitems; i++) {
1384 tb = items[i].tb;
1386 if (tb->graphic) {
1387 if (!tPtr->flags.monoFont) {
1388 if (tb->object) {
1389 WMWidget *wdt = tb->d.widget;
1390 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1391 if (tPtr->flags.alignment != WALeft)
1392 lw += WMWidgetWidth(wdt);
1393 } else {
1394 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1395 if (tPtr->flags.alignment != WALeft)
1396 lw += tb->d.pixmap->width;
1400 } else {
1401 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1402 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1403 max_d = 2;
1404 line_height = WMAX(line_height, font->height + max_d);
1405 text = &(tb->text[items[i].begin]);
1406 len = items[i].end - items[i].begin;
1407 if (tPtr->flags.alignment != WALeft)
1408 lw += WMWidthOfString(font, text, len);
1412 if (tPtr->flags.alignment == WARight) {
1413 j = tPtr->visible.w - lw;
1414 } else if (tPtr->flags.alignment == WACenter) {
1415 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1418 for (i = 0; i < nitems; i++) {
1419 tb = items[i].tb;
1421 if (tbsame == tb) { /* extend it, since it's on same line */
1422 tb->sections[tb->nsections - 1].end = items[i].end;
1423 n = tb->nsections - 1;
1424 } else {
1425 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1426 n = tb->nsections - 1;
1427 tb->sections[n]._y = y + max_d;
1428 tb->sections[n].max_d = max_d;
1429 tb->sections[n].x = x + j;
1430 tb->sections[n].h = line_height;
1431 tb->sections[n].begin = items[i].begin;
1432 tb->sections[n].end = items[i].end;
1435 tb->sections[n].last = (i + 1 == nitems);
1437 if (tb->graphic) {
1438 if (!tPtr->flags.monoFont) {
1439 if (tb->object) {
1440 WMWidget *wdt = tb->d.widget;
1441 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1442 tb->sections[n].w = WMWidgetWidth(wdt);
1443 } else {
1444 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1445 tb->sections[n].w = tb->d.pixmap->width;
1447 x += tb->sections[n].w;
1449 } else {
1450 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1451 len = items[i].end - items[i].begin;
1452 text = &(tb->text[items[i].begin]);
1454 tb->sections[n].y = y + line_height - font->y;
1455 tb->sections[n].w =
1456 WMWidthOfString(font,
1457 &(tb->text[tb->sections[n].begin]),
1458 tb->sections[n].end - tb->sections[n].begin);
1460 x += WMWidthOfString(font, text, len);
1463 tbsame = tb;
1466 return line_height;
1470 static void layOutDocument(Text * tPtr)
1472 TextBlock *tb;
1473 myLineItems *items = NULL;
1474 unsigned int itemsSize = 0, nitems = 0, begin, end;
1475 WMFont *font;
1476 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1477 char *start = NULL, *mark = NULL;
1479 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1480 return;
1482 assert(tPtr->visible.w > 20);
1484 tPtr->docWidth = tPtr->visible.w;
1485 x = tPtr->margins[tb->marginN].first;
1486 bmargin = tPtr->margins[tb->marginN].body;
1488 /* only partial layOut needed: re-Lay only affected textblocks */
1489 if (tPtr->flags.laidOut) {
1490 tb = tPtr->currentTextBlock;
1492 /* search backwards for textblocks on same line */
1493 while (tb->prior) {
1494 if (!tb->sections || tb->nsections < 1) {
1495 tb = tPtr->firstTextBlock;
1496 tPtr->flags.laidOut = False;
1497 y = 0;
1498 goto _layOut;
1501 if (!tb->prior->sections || tb->prior->nsections < 1) {
1502 tb = tPtr->firstTextBlock;
1503 tPtr->flags.laidOut = False;
1504 y = 0;
1505 goto _layOut;
1508 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1509 break;
1511 tb = tb->prior;
1514 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1515 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1516 tb->prior->sections[tb->prior->nsections - 1].h -
1517 tb->prior->sections[tb->prior->nsections - 1].max_d;
1518 } else {
1519 y = 0;
1523 _layOut:
1524 while (tb) {
1526 if (tb->sections && tb->nsections > 0) {
1527 wfree(tb->sections);
1528 tb->sections = NULL;
1529 tb->nsections = 0;
1532 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1533 TextBlock *next = tb->next;
1534 tPtr->currentTextBlock = tb;
1535 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1536 tb = next;
1537 tb->first = True;
1538 continue;
1541 if (tb->first && tb != tPtr->firstTextBlock) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 x = tPtr->margins[tb->marginN].first;
1544 bmargin = tPtr->margins[tb->marginN].body;
1545 nitems = 0;
1546 lw = 0;
1549 if (tb->graphic) {
1550 if (!tPtr->flags.monoFont) {
1551 if (tb->object)
1552 width = WMWidgetWidth(tb->d.widget);
1553 else
1554 width = tb->d.pixmap->width;
1556 if (width > tPtr->docWidth)
1557 tPtr->docWidth = width;
1559 lw += width;
1560 if (lw >= tPtr->visible.w - x) {
1561 y += layOutLine(tPtr, items, nitems, x, y);
1562 nitems = 0;
1563 x = bmargin;
1564 lw = width;
1567 if (nitems + 1 > itemsSize) {
1568 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1571 items[nitems].tb = tb;
1572 items[nitems].begin = 0;
1573 items[nitems].end = 0;
1574 nitems++;
1577 } else if ((start = tb->text)) {
1578 begin = end = 0;
1579 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1581 while (start) {
1582 mark = strchr(start, ' ');
1583 if (mark) {
1584 end += (int)(mark - start) + 1;
1585 start = mark + 1;
1586 } else {
1587 end += strlen(start);
1588 start = mark;
1591 if (end > tb->used)
1592 end = tb->used;
1594 if (end - begin > 0) {
1596 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1598 /* if it won't fit, char wrap it */
1599 if (width >= tPtr->visible.w) {
1600 char *t = &tb->text[begin];
1601 int l = end - begin, i = 0;
1602 do {
1603 width = WMWidthOfString(font, t, ++i);
1604 } while (width < tPtr->visible.w && i < l);
1605 if (i > 2)
1606 i--;
1607 end = begin + i;
1608 start = &tb->text[end];
1611 lw += width;
1614 if (lw >= tPtr->visible.w - x) {
1615 y += layOutLine(tPtr, items, nitems, x, y);
1616 lw = width;
1617 x = bmargin;
1618 nitems = 0;
1621 if (nitems + 1 > itemsSize) {
1622 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1625 items[nitems].tb = tb;
1626 items[nitems].begin = begin;
1627 items[nitems].end = end;
1628 nitems++;
1630 begin = end;
1634 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1635 if (0 && tPtr->flags.laidOut
1636 && tb->next && tb->next->sections && tb->next->nsections > 0
1637 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1638 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1639 TextBlock *ltb = tPtr->lastTextBlock;
1640 int ly = ltb->sections[ltb->nsections - 1]._y;
1641 int lh = ltb->sections[ltb->nsections - 1].h;
1642 int ss, sd;
1644 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1645 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1647 y += layOutLine(tPtr, items, nitems, x, y);
1648 ss = ly + lh - y;
1649 sd = tPtr->docHeight - y;
1651 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1652 y += tb->next->sections[0]._y - y;
1653 nitems = 0;
1654 printf("nitems%d\n", nitems);
1655 if (ss - sd != 0)
1656 y = tPtr->docHeight + ss - sd;
1658 break;
1659 } else {
1660 tPtr->flags.laidOut = False;
1664 tb = tb->next;
1667 if (nitems > 0)
1668 y += layOutLine(tPtr, items, nitems, x, y);
1670 if (tPtr->docHeight != y + 10) {
1671 tPtr->docHeight = y + 10;
1672 updateScrollers(tPtr);
1675 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1676 XEvent event;
1678 tPtr->flags.horizOnDemand = True;
1679 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1680 event.type = Expose;
1681 handleEvents(&event, (void *)tPtr);
1683 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1684 tPtr->flags.horizOnDemand = False;
1685 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1688 tPtr->flags.laidOut = True;
1690 if (items && itemsSize > 0)
1691 wfree(items);
1695 static void textDidResize(W_ViewDelegate * self, WMView * view)
1697 Text *tPtr = (Text *) view->self;
1698 unsigned short w = tPtr->view->size.width;
1699 unsigned short h = tPtr->view->size.height;
1700 unsigned short rh = 0, vw = 0, rel;
1702 rel = (tPtr->flags.relief == WRFlat);
1704 if (tPtr->ruler && tPtr->flags.rulerShown) {
1705 WMMoveWidget(tPtr->ruler, 2, 2);
1706 WMResizeWidget(tPtr->ruler, w - 4, 40);
1707 rh = 40;
1710 if (tPtr->vS) {
1711 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1712 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1713 vw = 20;
1714 WMSetRulerOffset(tPtr->ruler, 22);
1715 } else
1716 WMSetRulerOffset(tPtr->ruler, 2);
1718 if (tPtr->hS) {
1719 if (tPtr->vS) {
1720 WMMoveWidget(tPtr->hS, vw, h - 21);
1721 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1722 } else {
1723 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1724 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1728 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1729 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1730 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1731 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1732 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1733 tPtr->margins[0].right = tPtr->visible.w;
1735 if (tPtr->view->flags.realized) {
1737 if (tPtr->db) {
1738 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1739 tPtr->db = (Pixmap) NULL;
1742 if (tPtr->visible.w < 40)
1743 tPtr->visible.w = 40;
1744 if (tPtr->visible.h < 20)
1745 tPtr->visible.h = 20;
1747 if (!tPtr->db) {
1748 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1749 tPtr->view->window, tPtr->visible.w,
1750 tPtr->visible.h, tPtr->view->screen->depth);
1754 WMThawText(tPtr);
1757 W_ViewDelegate _TextViewDelegate = {
1758 NULL,
1759 NULL,
1760 textDidResize,
1761 NULL,
1762 NULL
1765 #define TEXT_BUFFER_INCR 8
1766 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1768 static void clearText(Text * tPtr)
1770 tPtr->vpos = tPtr->hpos = 0;
1771 tPtr->docHeight = tPtr->docWidth = 0;
1772 tPtr->cursor.x = -23;
1774 if (!tPtr->firstTextBlock)
1775 return;
1777 while (tPtr->currentTextBlock)
1778 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1780 tPtr->firstTextBlock = NULL;
1781 tPtr->currentTextBlock = NULL;
1782 tPtr->lastTextBlock = NULL;
1783 WMEmptyArray(tPtr->gfxItems);
1786 /* possibly remove a single character from the currentTextBlock,
1787 or if there's a selection, remove it...
1788 note that Delete and Backspace are treated differently */
1789 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1791 TextBlock *tb;
1792 Bool back = (Bool) (ksym == XK_BackSpace);
1793 Bool done = 1, wasFirst = 0;
1795 if (!tPtr->flags.editable)
1796 return;
1798 if (!(tb = tPtr->currentTextBlock))
1799 return;
1801 if (tPtr->flags.ownsSelection) {
1802 if (removeSelection(tPtr))
1803 layOutDocument(tPtr);
1804 return;
1807 wasFirst = tb->first;
1808 if (back && tPtr->tpos < 1) {
1809 if (tb->prior) {
1810 if (tb->prior->blank) {
1811 tPtr->currentTextBlock = tb->prior;
1812 WMRemoveTextBlock(tPtr);
1813 tPtr->currentTextBlock = tb;
1814 tb->first = True;
1815 layOutDocument(tPtr);
1816 return;
1817 } else {
1818 if (tb->blank) {
1819 TextBlock *prior = tb->prior;
1820 tPtr->currentTextBlock = tb;
1821 WMRemoveTextBlock(tPtr);
1822 tb = prior;
1823 } else {
1824 tb = tb->prior;
1827 if (tb->graphic)
1828 tPtr->tpos = 1;
1829 else
1830 tPtr->tpos = tb->used;
1832 tPtr->currentTextBlock = tb;
1833 done = 1;
1834 if (wasFirst) {
1835 if (tb->next)
1836 tb->next->first = False;
1837 layOutDocument(tPtr);
1838 return;
1844 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1845 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1846 if (back)
1847 tPtr->tpos--;
1848 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1849 tb->used--;
1850 done = 0;
1853 /* if there are no characters left to back over in the textblock,
1854 but it still has characters to the right of the cursor: */
1855 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1856 || tb->graphic) {
1858 /* no more chars, and it's marked as blank? */
1859 if (tb->blank) {
1860 TextBlock *sibling = (back ? tb->prior : tb->next);
1862 if (tb->used == 0 || tb->graphic)
1863 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1865 if (sibling) {
1866 tPtr->currentTextBlock = sibling;
1867 if (tb->graphic)
1868 tPtr->tpos = (back ? 1 : 0);
1869 else
1870 tPtr->tpos = (back ? sibling->used : 0);
1872 /* no more chars, so mark it as blank */
1873 } else if (tb->used == 0) {
1874 tb->blank = 1;
1875 } else if (tb->graphic) {
1876 Bool hasNext = (tb->next != NULL);
1878 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1879 if (hasNext) {
1880 tPtr->tpos = 0;
1881 } else if (tPtr->currentTextBlock) {
1882 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1884 } else
1885 printf("DEBUG: unaccounted for... catch this!\n");
1888 layOutDocument(tPtr);
1891 static void insertTextInteractively(Text * tPtr, char *text, int len)
1893 TextBlock *tb;
1894 char *newline = NULL;
1896 if (!tPtr->flags.editable) {
1897 return;
1900 if (len < 1 || !text)
1901 return;
1903 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1904 return;
1906 if (tPtr->flags.ownsSelection)
1907 removeSelection(tPtr);
1909 if (tPtr->flags.ignoreNewLine) {
1910 int i;
1911 for (i = 0; i < len; i++) {
1912 if (text[i] == '\n')
1913 text[i] = ' ';
1917 tb = tPtr->currentTextBlock;
1918 if (!tb || tb->graphic) {
1919 tPtr->tpos = 0;
1920 WMAppendTextStream(tPtr, text);
1921 layOutDocument(tPtr);
1922 return;
1925 if ((newline = strchr(text, '\n'))) {
1926 int nlen = (int)(newline - text);
1927 int s = tb->used - tPtr->tpos;
1929 if (!tb->blank && nlen > 0) {
1930 char *save = NULL;
1932 if (s > 0) {
1933 save = wmalloc(s);
1934 memcpy(save, &tb->text[tPtr->tpos], s);
1935 tb->used -= (tb->used - tPtr->tpos);
1937 insertTextInteractively(tPtr, text, nlen);
1938 newline++;
1939 WMAppendTextStream(tPtr, newline);
1940 if (s > 0) {
1941 insertTextInteractively(tPtr, save, s);
1942 wfree(save);
1944 } else {
1945 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1947 unsigned short savePos = tPtr->tpos;
1948 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1949 tb->d.font, tb->color, True,
1950 tb->used - tPtr->tpos);
1952 if (tb->sections[0].end == tPtr->tpos)
1953 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1954 NULL, tb->d.font,
1955 tb->color, True, 0));
1957 tb->used = savePos;
1958 WMAppendTextBlock(tPtr, ntb);
1959 tPtr->tpos = 0;
1961 } else if (tPtr->tpos == tb->used) {
1962 if (tPtr->flags.indentNewLine) {
1963 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1964 " ", tb->d.font,
1965 tb->color, True, 4));
1966 tPtr->tpos = 4;
1967 } else {
1968 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1969 NULL, tb->d.font,
1970 tb->color, True, 0));
1971 tPtr->tpos = 0;
1973 } else if (tPtr->tpos == 0) {
1974 if (tPtr->flags.indentNewLine) {
1975 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1976 " ", tb->d.font,
1977 tb->color, True, 4));
1978 } else {
1979 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1980 NULL, tb->d.font,
1981 tb->color, True, 0));
1983 tPtr->tpos = 0;
1984 if (tPtr->currentTextBlock->next)
1985 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1988 } else {
1989 if (tb->used + len >= tb->allocated) {
1990 tb->allocated = reqBlockSize(tb->used + len);
1991 tb->text = wrealloc(tb->text, tb->allocated);
1994 if (tb->blank) {
1995 memcpy(tb->text, text, len);
1996 tb->used = len;
1997 tPtr->tpos = len;
1998 tb->text[tb->used] = 0;
1999 tb->blank = False;
2001 } else {
2002 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
2003 memmove(&tb->text[tPtr->tpos], text, len);
2004 tb->used += len;
2005 tPtr->tpos += len;
2006 tb->text[tb->used] = 0;
2011 layOutDocument(tPtr);
2014 static void selectRegion(Text * tPtr, int x, int y)
2017 if (x < 0 || y < 0)
2018 return;
2020 y += (tPtr->flags.rulerShown ? 40 : 0);
2021 y += tPtr->vpos;
2022 if (y > 10)
2023 y -= 10; /* the original offset */
2025 x -= tPtr->visible.x - 2;
2026 if (x < 0)
2027 x = 0;
2029 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2030 tPtr->sel.w = abs(tPtr->clicked.x - x);
2031 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2032 tPtr->sel.h = abs(tPtr->clicked.y - y);
2034 tPtr->flags.ownsSelection = True;
2035 paintText(tPtr);
2038 static void releaseSelection(Text * tPtr)
2040 TextBlock *tb = tPtr->firstTextBlock;
2042 while (tb) {
2043 tb->selected = False;
2044 tb = tb->next;
2046 tPtr->flags.ownsSelection = False;
2047 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2049 paintText(tPtr);
2052 WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2054 Text *tPtr = view->self;
2055 Display *dpy = tPtr->view->screen->display;
2056 Atom _TARGETS;
2057 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2058 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2059 WMData *data = NULL;
2061 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2062 char *text = WMGetTextSelectedStream(tPtr);
2064 if (text) {
2065 data = WMCreateDataWithBytes(text, strlen(text));
2066 WMSetDataFormat(data, TYPETEXT);
2068 *type = target;
2069 return data;
2070 } else
2071 printf("didn't get it\n");
2073 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2074 if (target == _TARGETS) {
2075 Atom *ptr;
2077 ptr = wmalloc(4 * sizeof(Atom));
2078 ptr[0] = _TARGETS;
2079 ptr[1] = XA_STRING;
2080 ptr[2] = TEXT;
2081 ptr[3] = COMPOUND_TEXT;
2083 data = WMCreateDataWithBytes(ptr, 4 * 4);
2084 WMSetDataFormat(data, 32);
2086 *type = target;
2087 return data;
2090 return NULL;
2093 static void lostHandler(WMView * view, Atom selection, void *cdata)
2095 releaseSelection((WMText *) view->self);
2098 static WMSelectionProcs selectionHandler = {
2099 requestHandler, lostHandler, NULL
2102 static void ownershipObserver(void *observerData, WMNotification * notification)
2104 if (observerData != WMGetNotificationClientData(notification))
2105 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2108 static void autoSelectText(Text * tPtr, int clicks)
2110 int x, start;
2111 TextBlock *tb;
2112 char *mark = NULL, behind, ahead;
2114 if (!(tb = tPtr->currentTextBlock))
2115 return;
2117 if (clicks == 2) {
2119 switch (tb->text[tPtr->tpos]) {
2120 case ' ':
2121 return;
2123 case '<': case '>': behind = '<'; ahead = '>'; break;
2124 case '{': case '}': behind = '{'; ahead = '}'; break;
2125 case '[': case ']': behind = '['; ahead = ']'; break;
2127 default:
2128 behind = ahead = ' ';
2131 tPtr->sel.y = tPtr->cursor.y + 5;
2132 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2134 if (tb->graphic) {
2135 tPtr->sel.x = tb->sections[0].x;
2136 tPtr->sel.w = tb->sections[0].w;
2137 } else {
2138 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2140 start = tPtr->tpos;
2141 while (start > 0 && tb->text[start - 1] != behind)
2142 start--;
2144 x = tPtr->cursor.x;
2145 if (tPtr->tpos > start) {
2146 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2148 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2150 if ((mark = strchr(&tb->text[start], ahead))) {
2151 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2152 (int)(mark - &tb->text[start]));
2153 } else if (tb->used > start) {
2154 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2158 } else if (clicks == 3) {
2159 TextBlock *cur = tb;
2161 while (tb && !tb->first) {
2162 tb = tb->prior;
2164 tPtr->sel.y = tb->sections[0]._y;
2166 tb = cur;
2167 while (tb->next && !tb->next->first) {
2168 tb = tb->next;
2170 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2172 tPtr->sel.x = 0;
2173 tPtr->sel.w = tPtr->docWidth;
2174 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2177 if (!tPtr->flags.ownsSelection) {
2178 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2179 tPtr->flags.ownsSelection = True;
2181 paintText(tPtr);
2185 # if 0
2186 static void fontChanged(void *observerData, WMNotification * notification)
2188 WMText *tPtr = (WMText *) observerData;
2189 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2190 printf("fontChanged\n");
2192 if (!tPtr || !font)
2193 return;
2195 if (tPtr->flags.ownsSelection)
2196 WMSetTextSelectionFont(tPtr, font);
2198 #endif
2200 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2202 char buffer[64];
2203 KeySym ksym;
2204 int control_pressed = False;
2205 TextBlock *tb = NULL;
2207 if (((XKeyEvent *) event)->state & ControlMask)
2208 control_pressed = True;
2209 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2211 switch (ksym) {
2213 case XK_Home:
2214 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2215 tPtr->tpos = 0;
2216 updateCursorPosition(tPtr);
2217 paintText(tPtr);
2218 break;
2220 case XK_End:
2221 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2222 if (tPtr->currentTextBlock->graphic)
2223 tPtr->tpos = 1;
2224 else
2225 tPtr->tpos = tPtr->currentTextBlock->used;
2227 updateCursorPosition(tPtr);
2228 paintText(tPtr);
2229 break;
2231 case XK_Left:
2232 if (!(tb = tPtr->currentTextBlock))
2233 break;
2234 if (tb->graphic)
2235 goto L_imaGFX;
2237 if (tPtr->tpos == 0) {
2238 L_imaGFX:
2239 if (tb->prior) {
2240 tPtr->currentTextBlock = tb->prior;
2241 if (tPtr->currentTextBlock->graphic)
2242 tPtr->tpos = 1;
2243 else
2244 tPtr->tpos = tPtr->currentTextBlock->used;
2246 if (!tb->first && tPtr->tpos > 0)
2247 tPtr->tpos--;
2248 } else
2249 tPtr->tpos = 0;
2250 } else
2251 tPtr->tpos--;
2252 updateCursorPosition(tPtr);
2253 paintText(tPtr);
2254 break;
2256 case XK_Right:
2257 if (!(tb = tPtr->currentTextBlock))
2258 break;
2259 if (tb->graphic)
2260 goto R_imaGFX;
2261 if (tPtr->tpos == tb->used) {
2262 R_imaGFX:
2263 if (tb->next) {
2264 tPtr->currentTextBlock = tb->next;
2265 tPtr->tpos = 0;
2266 if (!tb->next->first && tb->next->used > 0)
2267 tPtr->tpos++;
2268 } else {
2269 if (tb->graphic)
2270 tPtr->tpos = 1;
2271 else
2272 tPtr->tpos = tb->used;
2274 } else
2275 tPtr->tpos++;
2276 updateCursorPosition(tPtr);
2277 paintText(tPtr);
2278 break;
2280 case XK_Down:
2281 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2282 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2283 paintText(tPtr);
2284 break;
2286 case XK_Up:
2287 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2288 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2289 paintText(tPtr);
2290 break;
2292 case XK_BackSpace:
2293 case XK_Delete:
2294 #ifdef XK_KP_Delete
2295 case XK_KP_Delete:
2296 #endif
2297 deleteTextInteractively(tPtr, ksym);
2298 updateCursorPosition(tPtr);
2299 paintText(tPtr);
2300 break;
2302 case XK_Control_R:
2303 case XK_Control_L:
2304 control_pressed = True;
2305 break;
2307 case XK_Tab:
2308 insertTextInteractively(tPtr, " ", 4);
2309 updateCursorPosition(tPtr);
2310 paintText(tPtr);
2311 break;
2313 case XK_Return:
2314 *buffer = '\n';
2315 default:
2316 if (*buffer != 0 && !control_pressed) {
2317 insertTextInteractively(tPtr, buffer, strlen(buffer));
2318 updateCursorPosition(tPtr);
2319 paintText(tPtr);
2321 } else if (control_pressed && ksym == XK_r) {
2322 Bool i = !tPtr->flags.rulerShown;
2323 WMShowTextRuler(tPtr, i);
2324 tPtr->flags.rulerShown = i;
2325 } else if (control_pressed && *buffer == '\a') {
2326 XBell(tPtr->view->screen->display, 0);
2327 } else {
2328 WMRelayToNextResponder(tPtr->view, event);
2332 if (!control_pressed && tPtr->flags.ownsSelection) {
2333 releaseSelection(tPtr);
2337 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2339 Text *tPtr = (Text *) view->self;
2340 char *text;
2342 tPtr->flags.waitingForSelection = 0;
2344 if (data) {
2345 text = (char *)WMDataBytes(data);
2347 if (tPtr->parser) {
2348 (tPtr->parser) (tPtr, (void *)text);
2349 layOutDocument(tPtr);
2350 } else
2351 insertTextInteractively(tPtr, text, strlen(text));
2352 updateCursorPosition(tPtr);
2353 paintText(tPtr);
2355 } else {
2356 int n;
2358 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2360 if (text) {
2361 text[n] = 0;
2362 if (tPtr->parser) {
2363 (tPtr->parser) (tPtr, (void *)text);
2364 layOutDocument(tPtr);
2365 } else
2366 insertTextInteractively(tPtr, text, n);
2367 updateCursorPosition(tPtr);
2368 paintText(tPtr);
2370 XFree(text);
2376 static void handleActionEvents(XEvent * event, void *data)
2378 Text *tPtr = (Text *) data;
2379 Display *dpy = event->xany.display;
2380 KeySym ksym;
2382 switch (event->type) {
2383 case KeyPress:
2384 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2385 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2386 tPtr->flags.extendSelection = True;
2387 return;
2390 if (tPtr->flags.focused) {
2391 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2392 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2393 GrabModeAsync, GrabModeAsync, None,
2394 tPtr->view->screen->invisibleCursor, CurrentTime);
2395 tPtr->flags.pointerGrabbed = True;
2396 handleTextKeyPress(tPtr, event);
2399 break;
2401 case KeyRelease:
2402 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2403 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2404 tPtr->flags.extendSelection = False;
2405 return;
2406 /* end modify flag so selection can be extended */
2408 break;
2410 case MotionNotify:
2412 if (tPtr->flags.pointerGrabbed) {
2413 tPtr->flags.pointerGrabbed = False;
2414 XUngrabPointer(dpy, CurrentTime);
2417 if (tPtr->flags.waitingForSelection)
2418 break;
2420 if ((event->xmotion.state & Button1Mask)) {
2422 if (WMIsDraggingFromView(tPtr->view)) {
2423 WMDragImageFromView(tPtr->view, event);
2424 break;
2427 if (!tPtr->flags.ownsSelection) {
2428 WMCreateSelectionHandler(tPtr->view,
2429 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2430 tPtr->flags.ownsSelection = True;
2432 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2433 break;
2436 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2437 break;
2439 case ButtonPress:
2441 if (tPtr->flags.pointerGrabbed) {
2442 tPtr->flags.pointerGrabbed = False;
2443 XUngrabPointer(dpy, CurrentTime);
2444 break;
2447 if (tPtr->flags.waitingForSelection)
2448 break;
2450 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2451 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2452 return;
2455 if (tPtr->flags.ownsSelection)
2456 releaseSelection(tPtr);
2458 if (event->xbutton.button == Button1) {
2459 TextBlock *tb = tPtr->currentTextBlock;
2461 if (WMIsDoubleClick(event)) {
2463 tPtr->lastClickTime = event->xbutton.time;
2464 if (tb && tb->graphic && !tb->object) {
2465 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2466 char *desc;
2468 desc = wmalloc(tb->used + 1);
2469 memcpy(desc, tb->text, tb->used);
2470 desc[tb->used] = 0;
2471 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2472 wfree(desc);
2474 } else {
2475 autoSelectText(tPtr, 2);
2477 break;
2478 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2479 tPtr->lastClickTime = event->xbutton.time;
2480 autoSelectText(tPtr, 3);
2481 break;
2484 if (!tPtr->flags.focused) {
2485 WMSetFocusToWidget(tPtr);
2486 tPtr->flags.focused = True;
2487 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2489 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2490 WMDragImageFromView(tPtr->view, event);
2491 break;
2494 tPtr->lastClickTime = event->xbutton.time;
2495 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2496 paintText(tPtr);
2499 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2500 WMScrollText(tPtr, 16);
2501 break;
2504 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2505 WMScrollText(tPtr, -16);
2506 break;
2509 if (event->xbutton.button == Button2) {
2510 char *text = NULL;
2511 int n;
2513 if (!tPtr->flags.editable) {
2514 XBell(dpy, 0);
2515 break;
2518 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2519 event->xbutton.time, pasteText, NULL)) {
2521 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2522 tPtr->flags.waitingForSelection = 0;
2524 if (text) {
2525 text[n] = 0;
2527 if (tPtr->parser) {
2528 (tPtr->parser) (tPtr, (void *)text);
2529 layOutDocument(tPtr);
2530 } else
2531 insertTextInteractively(tPtr, text, n);
2533 XFree(text);
2534 #if 0
2535 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2536 (void *)WMInsertTextEvent);
2537 #endif
2538 updateCursorPosition(tPtr);
2539 paintText(tPtr);
2541 } else {
2542 tPtr->flags.waitingForSelection = True;
2545 break;
2548 case ButtonRelease:
2549 if (tPtr->flags.pointerGrabbed) {
2550 tPtr->flags.pointerGrabbed = False;
2551 XUngrabPointer(dpy, CurrentTime);
2552 break;
2555 if (tPtr->flags.waitingForSelection)
2556 break;
2558 if (WMIsDraggingFromView(tPtr->view))
2559 WMDragImageFromView(tPtr->view, event);
2564 static void handleEvents(XEvent * event, void *data)
2566 Text *tPtr = (Text *) data;
2568 switch (event->type) {
2569 case Expose:
2571 if (event->xexpose.count != 0)
2572 break;
2574 if (tPtr->hS) {
2575 if (!(W_VIEW(tPtr->hS))->flags.realized)
2576 WMRealizeWidget(tPtr->hS);
2579 if (tPtr->vS) {
2580 if (!(W_VIEW(tPtr->vS))->flags.realized)
2581 WMRealizeWidget(tPtr->vS);
2584 if (tPtr->ruler) {
2585 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2586 WMRealizeWidget(tPtr->ruler);
2590 if (!tPtr->db)
2591 textDidResize(tPtr->view->delegate, tPtr->view);
2593 paintText(tPtr);
2594 break;
2596 case FocusIn:
2597 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2598 != tPtr->view)
2599 return;
2600 tPtr->flags.focused = True;
2601 #if DO_BLINK
2602 if (tPtr->flags.editable && !tPtr->timerID) {
2603 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2605 #endif
2607 break;
2609 case FocusOut:
2610 tPtr->flags.focused = False;
2611 paintText(tPtr);
2612 #if DO_BLINK
2613 if (tPtr->timerID) {
2614 WMDeleteTimerHandler(tPtr->timerID);
2615 tPtr->timerID = NULL;
2617 #endif
2618 break;
2620 case DestroyNotify:
2621 clearText(tPtr);
2622 if (tPtr->db)
2623 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2624 if (tPtr->gfxItems)
2625 WMEmptyArray(tPtr->gfxItems);
2626 #if DO_BLINK
2627 if (tPtr->timerID)
2628 WMDeleteTimerHandler(tPtr->timerID);
2629 #endif
2630 WMReleaseFont(tPtr->dFont);
2631 WMReleaseColor(tPtr->dColor);
2632 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2633 WMRemoveNotificationObserver(tPtr);
2635 WMFreeArray(tPtr->xdndSourceTypes);
2636 WMFreeArray(tPtr->xdndDestinationTypes);
2638 wfree(tPtr);
2640 break;
2645 static void insertPlainText(Text * tPtr, char *text)
2647 char *start, *mark;
2648 void *tb = NULL;
2650 start = text;
2651 while (start) {
2652 mark = strchr(start, '\n');
2653 if (mark) {
2654 tb = WMCreateTextBlockWithText(tPtr,
2655 start, tPtr->dFont,
2656 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2657 start = mark + 1;
2658 tPtr->flags.first = True;
2659 } else {
2660 if (start && strlen(start)) {
2661 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2662 tPtr->dColor, tPtr->flags.first, strlen(start));
2663 } else
2664 tb = NULL;
2665 tPtr->flags.first = False;
2666 start = mark;
2669 if (tPtr->flags.prepend)
2670 WMPrependTextBlock(tPtr, tb);
2671 else
2672 WMAppendTextBlock(tPtr, tb);
2676 static void rulerMoveCallBack(WMWidget * w, void *self)
2678 Text *tPtr = (Text *) self;
2680 if (!tPtr)
2681 return;
2682 if (W_CLASS(tPtr) != WC_Text)
2683 return;
2685 paintText(tPtr);
2688 static void rulerReleaseCallBack(WMWidget * w, void *self)
2690 Text *tPtr = (Text *) self;
2692 if (!tPtr)
2693 return;
2694 if (W_CLASS(tPtr) != WC_Text)
2695 return;
2697 WMThawText(tPtr);
2698 return;
2701 static WMArray *dropDataTypes(WMView * self)
2703 return ((Text *) self->self)->xdndSourceTypes;
2706 static WMDragOperationType wantedDropOperation(WMView * self)
2708 return WDOperationCopy;
2711 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2713 return (allowedOperation == WDOperationCopy);
2716 static WMData *fetchDragData(WMView * self, char *type)
2718 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2719 char *desc;
2720 WMData *data;
2722 if (strcmp(type, "text/plain")) {
2723 if (!tb)
2724 return NULL;
2726 desc = wmalloc(tb->used + 1);
2727 memcpy(desc, tb->text, tb->used);
2728 desc[tb->used] = 0;
2729 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2731 wfree(desc);
2733 return data;
2736 return NULL;
2739 static WMDragSourceProcs _DragSourceProcs = {
2740 dropDataTypes,
2741 wantedDropOperation,
2742 NULL,
2743 acceptDropOperation,
2744 NULL,
2745 NULL,
2746 fetchDragData
2749 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2751 return ((Text *) self->self)->xdndDestinationTypes;
2754 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2756 return WDOperationCopy;
2759 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2761 WMText *tPtr = (WMText *) self->self;
2762 WMData *data;
2763 char *colorName;
2764 WMColor *color;
2766 if (tPtr) {
2768 /* only one required type, implies only one drop data */
2770 /* get application/X-color if any */
2771 data = (WMData *) WMPopFromArray(dropData);
2772 if (data != NULL) {
2773 colorName = (char *)WMDataBytes(data);
2774 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2776 if (color) {
2777 WMSetTextSelectionColor(tPtr, color);
2778 WMReleaseColor(color);
2784 static WMDragDestinationProcs _DragDestinationProcs = {
2785 NULL,
2786 requiredDataTypes,
2787 allowedOperation,
2788 NULL,
2789 performDragOperation,
2790 NULL
2793 char *getStream(WMText * tPtr, int sel, int array)
2795 TextBlock *tb = NULL;
2796 char *text = NULL;
2797 unsigned long where = 0;
2799 if (!tPtr)
2800 return NULL;
2802 if (!(tb = tPtr->firstTextBlock))
2803 return NULL;
2805 if (tPtr->writer) {
2806 (tPtr->writer) (tPtr, (void *)text);
2807 return text;
2810 tb = tPtr->firstTextBlock;
2811 while (tb) {
2813 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2815 if (!sel || (tb->graphic && tb->selected)) {
2817 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2818 && tb != tPtr->firstTextBlock) {
2819 text = wrealloc(text, where + 1);
2820 text[where++] = '\n';
2823 if (tb->blank)
2824 goto _gSnext;
2826 if (tb->graphic && array) {
2827 text = wrealloc(text, where + 4);
2828 text[where++] = 0xFA;
2829 text[where++] = (tb->used >> 8) & 0x0ff;
2830 text[where++] = tb->used & 0x0ff;
2831 text[where++] = tb->allocated; /* extra info */
2833 text = wrealloc(text, where + tb->used);
2834 memcpy(&text[where], tb->text, tb->used);
2835 where += tb->used;
2837 } else if (sel && tb->selected) {
2839 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2840 text = wrealloc(text, where + 1);
2841 text[where++] = '\n';
2844 if (tb->blank)
2845 goto _gSnext;
2847 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2848 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2849 where += tb->s_end - tb->s_begin;
2854 _gSnext: tb = tb->next;
2857 /* +1 for the end of string, let's be nice */
2858 text = wrealloc(text, where + 1);
2859 text[where] = 0;
2860 return text;
2863 static void releaseStreamObjects(void *data)
2865 if (data)
2866 wfree(data);
2869 WMArray *getStreamObjects(WMText * tPtr, int sel)
2871 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2872 WMData *data;
2873 char *stream;
2874 unsigned short len;
2875 char *start, *fa, *desc;
2877 stream = getStream(tPtr, sel, 1);
2878 if (!stream)
2879 return NULL;
2881 start = stream;
2882 while (start) {
2884 fa = strchr(start, 0xFA);
2885 if (fa) {
2886 if ((int)(fa - start) > 0) {
2887 desc = start;
2888 desc[(int)(fa - start)] = 0;
2889 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2890 WMSetDataFormat(data, TYPETEXT);
2891 WMAddToArray(array, (void *)data);
2894 len = *(fa + 1) * 0xff + *(fa + 2);
2895 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2896 WMSetDataFormat(data, *(fa + 3));
2897 WMAddToArray(array, (void *)data);
2898 start = fa + len + 4;
2900 } else {
2901 if (start && strlen(start)) {
2902 data = WMCreateDataWithBytes((void *)start, strlen(start));
2903 WMSetDataFormat(data, TYPETEXT);
2904 WMAddToArray(array, (void *)data);
2906 start = fa;
2910 wfree(stream);
2911 return array;
2914 #define XDND_TEXT_DATA_TYPE "text/plain"
2915 #define XDND_COLOR_DATA_TYPE "application/X-color"
2916 static WMArray *getXdndSourceTypeArray()
2918 WMArray *types = WMCreateArray(1);
2919 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2920 return types;
2923 static WMArray *getXdndDestinationTypeArray()
2925 WMArray *types = WMCreateArray(1);
2926 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2927 return types;
2930 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2932 Text *tPtr;
2933 Display *dpy;
2934 WMScreen *scr;
2935 XGCValues gcv;
2937 tPtr = wmalloc(sizeof(Text));
2938 memset(tPtr, 0, sizeof(Text));
2939 tPtr->widgetClass = WC_Text;
2940 tPtr->view = W_CreateView(W_VIEW(parent));
2941 if (!tPtr->view) {
2942 perror("could not create text's view\n");
2943 wfree(tPtr);
2944 return NULL;
2947 dpy = tPtr->view->screen->display;
2948 scr = tPtr->view->screen;
2950 tPtr->view->self = tPtr;
2951 tPtr->view->attribs.cursor = scr->textCursor;
2952 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2953 W_ResizeView(tPtr->view, 250, 200);
2955 tPtr->dColor = WMBlackColor(scr);
2956 tPtr->fgColor = WMBlackColor(scr);
2957 tPtr->bgColor = WMWhiteColor(scr);
2958 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2960 gcv.graphics_exposures = False;
2961 gcv.foreground = W_PIXEL(scr->gray);
2962 gcv.background = W_PIXEL(scr->darkGray);
2963 gcv.fill_style = FillStippled;
2964 /* why not use scr->stipple here? */
2965 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2966 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2967 GCForeground | GCBackground | GCStipple
2968 | GCFillStyle | GCGraphicsExposures, &gcv);
2970 tPtr->ruler = NULL;
2971 tPtr->vS = NULL;
2972 tPtr->hS = NULL;
2974 tPtr->dFont = WMSystemFontOfSize(scr, 12);
2976 tPtr->view->delegate = &_TextViewDelegate;
2978 tPtr->delegate = NULL;
2980 #if DO_BLINK
2981 tPtr->timerID = NULL;
2982 #endif
2984 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
2985 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
2987 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
2988 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
2990 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
2992 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
2993 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2996 WMArray *types = WMCreateArray(2);
2997 WMAddToArray(types, "application/X-color");
2998 WMAddToArray(types, "application/X-image");
2999 WMRegisterViewForDraggedTypes(tPtr->view, types);
3002 /*WMAddNotificationObserver(fontChanged, tPtr,
3003 WMFontPanelDidChangeNotification, tPtr); */
3005 tPtr->firstTextBlock = NULL;
3006 tPtr->lastTextBlock = NULL;
3007 tPtr->currentTextBlock = NULL;
3008 tPtr->tpos = 0;
3010 tPtr->gfxItems = WMCreateArray(4);
3012 tPtr->parser = parser;
3013 tPtr->writer = writer;
3015 tPtr->sel.x = tPtr->sel.y = 2;
3016 tPtr->sel.w = tPtr->sel.h = 0;
3018 tPtr->clicked.x = tPtr->clicked.y = 2;
3020 tPtr->visible.x = tPtr->visible.y = 2;
3021 tPtr->visible.h = tPtr->view->size.height;
3022 tPtr->visible.w = tPtr->view->size.width - 4;
3024 tPtr->cursor.x = -23;
3026 tPtr->docWidth = 0;
3027 tPtr->docHeight = 0;
3028 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3029 tPtr->db = (Pixmap) NULL;
3030 tPtr->bgPixmap = NULL;
3032 tPtr->margins = WMGetRulerMargins(NULL);
3033 tPtr->margins->right = tPtr->visible.w;
3034 tPtr->nMargins = 1;
3036 tPtr->flags.rulerShown = False;
3037 tPtr->flags.monoFont = False;
3038 tPtr->flags.focused = False;
3039 tPtr->flags.editable = True;
3040 tPtr->flags.ownsSelection = False;
3041 tPtr->flags.pointerGrabbed = False;
3042 tPtr->flags.extendSelection = False;
3043 tPtr->flags.frozen = False;
3044 tPtr->flags.cursorShown = True;
3045 tPtr->flags.acceptsGraphic = False;
3046 tPtr->flags.horizOnDemand = False;
3047 tPtr->flags.needsLayOut = False;
3048 tPtr->flags.ignoreNewLine = False;
3049 tPtr->flags.indentNewLine = False;
3050 tPtr->flags.laidOut = False;
3051 tPtr->flags.ownsSelection = False;
3052 tPtr->flags.waitingForSelection = False;
3053 tPtr->flags.prepend = False;
3054 tPtr->flags.isOverGraphic = False;
3055 tPtr->flags.relief = WRSunken;
3056 tPtr->flags.isOverGraphic = 0;
3057 tPtr->flags.alignment = WALeft;
3058 tPtr->flags.first = True;
3060 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3061 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3063 return tPtr;
3066 void WMPrependTextStream(WMText * tPtr, char *text)
3068 CHECK_CLASS(tPtr, WC_Text);
3070 if (!text) {
3071 if (tPtr->flags.ownsSelection)
3072 releaseSelection(tPtr);
3073 clearText(tPtr);
3074 updateScrollers(tPtr);
3075 return;
3078 tPtr->flags.prepend = True;
3079 if (text && tPtr->parser)
3080 (tPtr->parser) (tPtr, (void *)text);
3081 else
3082 insertPlainText(tPtr, text);
3084 tPtr->flags.needsLayOut = True;
3085 tPtr->tpos = 0;
3086 if (!tPtr->flags.frozen) {
3087 layOutDocument(tPtr);
3091 void WMAppendTextStream(WMText * tPtr, char *text)
3093 CHECK_CLASS(tPtr, WC_Text);
3095 if (!text) {
3096 if (tPtr->flags.ownsSelection)
3097 releaseSelection(tPtr);
3098 clearText(tPtr);
3099 updateScrollers(tPtr);
3100 return;
3103 tPtr->flags.prepend = False;
3104 if (text && tPtr->parser)
3105 (tPtr->parser) (tPtr, (void *)text);
3106 else
3107 insertPlainText(tPtr, text);
3109 tPtr->flags.needsLayOut = True;
3110 if (tPtr->currentTextBlock) {
3111 if (tPtr->currentTextBlock->graphic)
3112 tPtr->tpos = 1;
3113 else
3114 tPtr->tpos = tPtr->currentTextBlock->used;
3117 if (!tPtr->flags.frozen) {
3118 layOutDocument(tPtr);
3122 char *WMGetTextStream(WMText * tPtr)
3124 CHECK_CLASS(tPtr, WC_Text);
3126 return getStream(tPtr, 0, 0);
3129 char *WMGetTextSelectedStream(WMText * tPtr)
3131 CHECK_CLASS(tPtr, WC_Text);
3133 return getStream(tPtr, 1, 0);
3136 WMArray *WMGetTextObjects(WMText * tPtr)
3138 CHECK_CLASS(tPtr, WC_Text);
3140 return getStreamObjects(tPtr, 0);
3143 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3145 CHECK_CLASS(tPtr, WC_Text);
3147 return getStreamObjects(tPtr, 1);
3150 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3152 CHECK_CLASS(tPtr, WC_Text);
3154 tPtr->delegate = delegate;
3157 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3158 char *description, WMColor * color,
3159 unsigned short first, unsigned short extraInfo)
3161 TextBlock *tb;
3163 if (!w || !description || !color)
3164 return NULL;
3166 tb = wmalloc(sizeof(TextBlock));
3168 tb->text = wstrdup(description);
3169 tb->used = strlen(description);
3170 tb->blank = False;
3171 tb->d.widget = w;
3172 tb->color = WMRetainColor(color);
3173 tb->marginN = newMargin(tPtr, NULL);
3174 tb->allocated = extraInfo;
3175 tb->first = first;
3176 tb->kanji = False;
3177 tb->graphic = True;
3178 tb->object = True;
3179 tb->underlined = False;
3180 tb->selected = False;
3181 tb->script = 0;
3182 tb->sections = NULL;
3183 tb->nsections = 0;
3184 tb->prior = NULL;
3185 tb->next = NULL;
3187 return tb;
3190 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3191 char *description, WMColor * color,
3192 unsigned short first, unsigned short extraInfo)
3194 TextBlock *tb;
3196 if (!p || !description || !color)
3197 return NULL;
3199 tb = wmalloc(sizeof(TextBlock));
3201 tb->text = wstrdup(description);
3202 tb->used = strlen(description);
3203 tb->blank = False;
3204 tb->d.pixmap = WMRetainPixmap(p);
3205 tb->color = WMRetainColor(color);
3206 tb->marginN = newMargin(tPtr, NULL);
3207 tb->allocated = extraInfo;
3208 tb->first = first;
3209 tb->kanji = False;
3210 tb->graphic = True;
3211 tb->object = False;
3212 tb->underlined = False;
3213 tb->selected = False;
3214 tb->script = 0;
3215 tb->sections = NULL;
3216 tb->nsections = 0;
3217 tb->prior = NULL;
3218 tb->next = NULL;
3220 return tb;
3223 void *WMCreateTextBlockWithText(WMText * tPtr, char *text, WMFont * font, WMColor * color,
3224 unsigned short first, unsigned short len)
3226 TextBlock *tb;
3228 if (!font || !color)
3229 return NULL;
3231 tb = wmalloc(sizeof(TextBlock));
3233 tb->allocated = reqBlockSize(len);
3234 tb->text = (char *)wmalloc(tb->allocated);
3235 memset(tb->text, 0, tb->allocated);
3237 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3238 *tb->text = ' ';
3239 tb->used = 1;
3240 tb->blank = True;
3241 } else {
3242 memcpy(tb->text, text, len);
3243 tb->used = len;
3244 tb->blank = False;
3246 tb->text[tb->used] = 0;
3248 tb->d.font = WMRetainFont(font);
3249 tb->color = WMRetainColor(color);
3250 tb->marginN = newMargin(tPtr, NULL);
3251 tb->first = first;
3252 tb->kanji = False;
3253 tb->graphic = False;
3254 tb->underlined = False;
3255 tb->selected = False;
3256 tb->script = 0;
3257 tb->sections = NULL;
3258 tb->nsections = 0;
3259 tb->prior = NULL;
3260 tb->next = NULL;
3261 return tb;
3264 void
3265 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3266 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3268 TextBlock *tb = (TextBlock *) vtb;
3269 if (!tb)
3270 return;
3272 tb->first = first;
3273 tb->kanji = kanji;
3274 tb->underlined = underlined;
3275 tb->script = script;
3276 tb->marginN = newMargin(tPtr, margins);
3279 void
3280 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3281 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins * margins)
3283 TextBlock *tb = (TextBlock *) vtb;
3284 if (!tb)
3285 return;
3287 if (first)
3288 *first = tb->first;
3289 if (kanji)
3290 *kanji = tb->kanji;
3291 if (underlined)
3292 *underlined = tb->underlined;
3293 if (script)
3294 *script = tb->script;
3295 if (margins)
3296 margins = &tPtr->margins[tb->marginN];
3299 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3301 TextBlock *tb = (TextBlock *) vtb;
3303 if (!tb)
3304 return;
3306 if (tb->graphic) {
3307 if (tb->object) {
3308 WMWidget *w = tb->d.widget;
3309 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3310 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3311 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3314 WMAddToArray(tPtr->gfxItems, (void *)tb);
3315 tPtr->tpos = 1;
3317 } else {
3318 tPtr->tpos = tb->used;
3321 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3322 tb->next = tb->prior = NULL;
3323 tb->first = True;
3324 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3325 return;
3328 if (!tb->first) {
3329 tb->marginN = tPtr->currentTextBlock->marginN;
3332 tb->next = tPtr->currentTextBlock;
3333 tb->prior = tPtr->currentTextBlock->prior;
3334 if (tPtr->currentTextBlock->prior)
3335 tPtr->currentTextBlock->prior->next = tb;
3337 tPtr->currentTextBlock->prior = tb;
3338 if (!tb->prior)
3339 tPtr->firstTextBlock = tb;
3341 tPtr->currentTextBlock = tb;
3344 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3346 TextBlock *tb = (TextBlock *) vtb;
3348 if (!tb)
3349 return;
3351 if (tb->graphic) {
3352 if (tb->object) {
3353 WMWidget *w = tb->d.widget;
3354 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3355 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3356 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3359 WMAddToArray(tPtr->gfxItems, (void *)tb);
3360 tPtr->tpos = 1;
3362 } else {
3363 tPtr->tpos = tb->used;
3366 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3367 tb->next = tb->prior = NULL;
3368 tb->first = True;
3369 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3370 return;
3373 if (!tb->first) {
3374 tb->marginN = tPtr->currentTextBlock->marginN;
3377 tb->next = tPtr->currentTextBlock->next;
3378 tb->prior = tPtr->currentTextBlock;
3379 if (tPtr->currentTextBlock->next)
3380 tPtr->currentTextBlock->next->prior = tb;
3382 tPtr->currentTextBlock->next = tb;
3384 if (!tb->next)
3385 tPtr->lastTextBlock = tb;
3387 tPtr->currentTextBlock = tb;
3390 void *WMRemoveTextBlock(WMText * tPtr)
3392 TextBlock *tb = NULL;
3394 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3395 return NULL;
3398 tb = tPtr->currentTextBlock;
3399 if (tb->graphic) {
3400 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3402 if (tb->object) {
3403 WMUnmapWidget(tb->d.widget);
3407 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3408 if (tPtr->currentTextBlock->next)
3409 tPtr->currentTextBlock->next->prior = NULL;
3411 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3412 tPtr->currentTextBlock = tPtr->firstTextBlock;
3414 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3415 tPtr->currentTextBlock->prior->next = NULL;
3416 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3417 tPtr->currentTextBlock = tPtr->lastTextBlock;
3418 } else {
3419 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3420 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3421 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3424 return (void *)tb;
3427 #if 0
3428 static void destroyWidget(WMWidget * widget)
3430 WMDestroyWidget(widget);
3431 // -- never do this -- wfree(widget);
3433 #endif
3435 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3437 TextBlock *tb = (TextBlock *) vtb;
3438 if (!tb)
3439 return;
3441 if (tb->graphic) {
3442 if (tb->object) {
3443 /* naturally, there's a danger to destroying widgets whose action
3444 * brings us here: ie. press a button to destroy it...
3445 * need to find a safer way. till then... this stays commented out */
3446 /* 5 months later... destroy it 10 seconds after now which should
3447 * be enough time for the widget's action to be completed... :-) */
3448 /* This is a bad assumption. Just destroy the widget here.
3449 * if the caller needs it, it can protect it with W_RetainView()
3450 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3451 WMDestroyWidget(tb->d.widget);
3452 } else {
3453 WMReleasePixmap(tb->d.pixmap);
3455 } else {
3456 WMReleaseFont(tb->d.font);
3459 WMReleaseColor(tb->color);
3460 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3461 if (tb->sections)
3462 wfree(tb->sections);
3463 wfree(tb->text);
3464 wfree(tb);
3467 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3469 if (tPtr->fgColor)
3470 WMReleaseColor(tPtr->fgColor);
3472 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3474 paintText(tPtr);
3477 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3479 if (tPtr->bgColor)
3480 WMReleaseColor(tPtr->bgColor);
3482 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3483 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3485 paintText(tPtr);
3488 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3490 if (tPtr->bgPixmap)
3491 WMReleasePixmap(tPtr->bgPixmap);
3493 if (pixmap)
3494 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3495 else
3496 tPtr->bgPixmap = NULL;
3499 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3501 tPtr->flags.relief = relief;
3502 textDidResize(tPtr->view->delegate, tPtr->view);
3505 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3507 if (shouldhave && !tPtr->hS) {
3508 tPtr->hS = WMCreateScroller(tPtr);
3509 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3510 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3511 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3512 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3513 WMMapWidget(tPtr->hS);
3514 } else if (!shouldhave && tPtr->hS) {
3515 WMUnmapWidget(tPtr->hS);
3516 WMDestroyWidget(tPtr->hS);
3517 tPtr->hS = NULL;
3520 tPtr->hpos = 0;
3521 tPtr->prevHpos = 0;
3522 textDidResize(tPtr->view->delegate, tPtr->view);
3525 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3527 if (shouldhave && !tPtr->ruler) {
3528 tPtr->ruler = WMCreateRuler(tPtr);
3529 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3530 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3531 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3532 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3533 } else if (!shouldhave && tPtr->ruler) {
3534 WMShowTextRuler(tPtr, False);
3535 WMDestroyWidget(tPtr->ruler);
3536 tPtr->ruler = NULL;
3538 textDidResize(tPtr->view->delegate, tPtr->view);
3541 void WMShowTextRuler(WMText * tPtr, Bool show)
3543 if (!tPtr->ruler)
3544 return;
3546 if (tPtr->flags.monoFont)
3547 show = False;
3549 tPtr->flags.rulerShown = show;
3550 if (show) {
3551 WMMapWidget(tPtr->ruler);
3552 } else {
3553 WMUnmapWidget(tPtr->ruler);
3556 textDidResize(tPtr->view->delegate, tPtr->view);
3559 Bool WMGetTextRulerShown(WMText * tPtr)
3561 if (!tPtr->ruler)
3562 return False;
3564 return tPtr->flags.rulerShown;
3567 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3569 if (shouldhave && !tPtr->vS) {
3570 tPtr->vS = WMCreateScroller(tPtr);
3571 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3572 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3573 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3574 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3575 WMMapWidget(tPtr->vS);
3576 } else if (!shouldhave && tPtr->vS) {
3577 WMUnmapWidget(tPtr->vS);
3578 WMDestroyWidget(tPtr->vS);
3579 tPtr->vS = NULL;
3582 tPtr->vpos = 0;
3583 tPtr->prevVpos = 0;
3584 textDidResize(tPtr->view->delegate, tPtr->view);
3587 Bool WMScrollText(WMText * tPtr, int amount)
3589 Bool scroll = False;
3591 if (amount == 0 || !tPtr->view->flags.realized)
3592 return False;
3594 if (amount < 0) {
3595 if (tPtr->vpos > 0) {
3596 if (tPtr->vpos > abs(amount))
3597 tPtr->vpos += amount;
3598 else
3599 tPtr->vpos = 0;
3600 scroll = True;
3602 } else {
3603 int limit = tPtr->docHeight - tPtr->visible.h;
3604 if (tPtr->vpos < limit) {
3605 if (tPtr->vpos < limit - amount)
3606 tPtr->vpos += amount;
3607 else
3608 tPtr->vpos = limit;
3609 scroll = True;
3613 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3614 updateScrollers(tPtr);
3615 paintText(tPtr);
3617 tPtr->prevVpos = tPtr->vpos;
3618 return scroll;
3621 Bool WMPageText(WMText * tPtr, Bool direction)
3623 if (!tPtr->view->flags.realized)
3624 return False;
3626 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3629 void WMSetTextEditable(WMText * tPtr, Bool editable)
3631 tPtr->flags.editable = editable;
3634 int WMGetTextEditable(WMText * tPtr)
3636 return tPtr->flags.editable;
3639 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3641 tPtr->flags.indentNewLine = indent;
3644 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3646 tPtr->flags.ignoreNewLine = ignore;
3649 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3651 return tPtr->flags.ignoreNewLine;
3654 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3656 if (mono) {
3657 if (tPtr->flags.rulerShown)
3658 WMShowTextRuler(tPtr, False);
3659 if (tPtr->flags.alignment != WALeft)
3660 tPtr->flags.alignment = WALeft;
3663 tPtr->flags.monoFont = mono;
3664 textDidResize(tPtr->view->delegate, tPtr->view);
3667 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3669 return tPtr->flags.monoFont;
3672 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3674 if (tPtr->dFont)
3675 WMReleaseFont(tPtr->dFont);
3677 if (font) {
3678 tPtr->dFont = WMRetainFont(font);
3679 } else {
3680 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3684 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3686 return WMRetainFont(tPtr->dFont);
3689 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3691 if (tPtr->dColor)
3692 WMReleaseColor(tPtr->dColor);
3694 if (color) {
3695 tPtr->dColor = WMRetainColor(color);
3696 } else {
3697 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3701 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3703 return tPtr->dColor;
3706 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3708 if (tPtr->flags.monoFont)
3709 tPtr->flags.alignment = WALeft;
3710 else
3711 tPtr->flags.alignment = alignment;
3712 WMThawText(tPtr);
3715 int WMGetTextInsertType(WMText * tPtr)
3717 return tPtr->flags.prepend;
3720 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3722 setSelectionProperty(tPtr, NULL, color, -1);
3725 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3727 TextBlock *tb;
3729 tb = tPtr->currentTextBlock;
3731 if (!tb || !tPtr->flags.ownsSelection)
3732 return NULL;
3734 if (!tb->selected)
3735 return NULL;
3737 return tb->color;
3740 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3742 setSelectionProperty(tPtr, font, NULL, -1);
3745 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3747 TextBlock *tb;
3749 tb = tPtr->currentTextBlock;
3751 if (!tb || !tPtr->flags.ownsSelection)
3752 return NULL;
3754 if (!tb->selected)
3755 return NULL;
3757 if (tb->graphic) {
3758 tb = getFirstNonGraphicBlockFor(tb, 1);
3759 if (!tb)
3760 return NULL;
3762 return (tb->selected ? tb->d.font : NULL);
3765 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3767 /* // check this */
3768 if (underlined != 0 && underlined != 1)
3769 return;
3771 setSelectionProperty(tPtr, NULL, NULL, underlined);
3774 int WMGetTextSelectionUnderlined(WMText * tPtr)
3776 TextBlock *tb;
3778 tb = tPtr->currentTextBlock;
3780 if (!tb || !tPtr->flags.ownsSelection)
3781 return 0;
3783 if (!tb->selected)
3784 return 0;
3786 return tb->underlined;
3789 void WMFreezeText(WMText * tPtr)
3791 tPtr->flags.frozen = True;
3794 void WMThawText(WMText * tPtr)
3796 tPtr->flags.frozen = False;
3798 if (tPtr->flags.monoFont) {
3799 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3800 TextBlock *tb;
3802 /* make sure to unmap widgets no matter where they are */
3803 /* they'll be later remapped if needed by paintText */
3804 for (j = 0; j < c; j++) {
3805 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3806 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3807 WMUnmapWidget(tb->d.widget);
3812 tPtr->flags.laidOut = False;
3813 layOutDocument(tPtr);
3814 updateScrollers(tPtr);
3815 paintText(tPtr);
3816 tPtr->flags.needsLayOut = False;
3820 /* find first occurence of a string */
3821 static char *mystrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3823 char *ptr;
3825 if (!haystack || !needle || !end)
3826 return NULL;
3828 for (ptr = haystack; ptr < end; ptr++) {
3829 if (caseSensitive) {
3830 if (*ptr == *needle && !strncmp(ptr, needle, len))
3831 return ptr;
3833 } else {
3834 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3835 return ptr;
3839 return NULL;
3842 /* find last occurence of a string */
3843 static char *mystrrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3845 char *ptr;
3847 if (!haystack || !needle || !end)
3848 return NULL;
3850 for (ptr = haystack - 2; ptr > end; ptr--) {
3851 if (caseSensitive) {
3852 if (*ptr == *needle && !strncmp(ptr, needle, len))
3853 return ptr;
3854 } else {
3855 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3856 return ptr;
3860 return NULL;
3863 Bool WMFindInTextStream(WMText * tPtr, char *needle, Bool direction, Bool caseSensitive)
3865 TextBlock *tb;
3866 char *mark = NULL;
3867 unsigned short pos;
3869 #if 0
3870 if (!(tb = tPtr->currentTextBlock)) {
3871 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3872 return False;
3874 } else {
3875 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3876 tb = (direction>0) ? tb->next : tb->prior; */
3877 if (tb != tPtr->lastTextBlock)
3878 tb = tb->prior;
3880 #endif
3881 tb = tPtr->currentTextBlock;
3882 pos = tPtr->tpos;
3884 while (tb) {
3885 if (!tb->graphic) {
3887 if (direction > 0) {
3888 if (pos + 1 < tb->used)
3889 pos++;
3891 if (tb->used - pos > 0 && pos > 0) {
3892 mark = mystrstr(&tb->text[pos], needle,
3893 strlen(needle), &tb->text[tb->used], caseSensitive);
3895 } else {
3896 tb = tb->next;
3897 pos = 0;
3898 continue;
3901 } else {
3902 if (pos - 1 > 0)
3903 pos--;
3905 if (pos > 0) {
3906 mark = mystrrstr(&tb->text[pos], needle,
3907 strlen(needle), tb->text, caseSensitive);
3908 } else {
3909 tb = tb->prior;
3910 if (!tb)
3911 return False;
3912 pos = tb->used;
3913 continue;
3917 if (mark) {
3918 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3920 tPtr->tpos = (int)(mark - tb->text);
3921 tPtr->currentTextBlock = tb;
3922 updateCursorPosition(tPtr);
3923 tPtr->sel.y = tPtr->cursor.y + 5;
3924 tPtr->sel.h = tPtr->cursor.h - 10;
3925 tPtr->sel.x = tPtr->cursor.x + 1;
3926 tPtr->sel.w = WMIN(WMWidthOfString(font,
3927 &tb->text[tPtr->tpos], strlen(needle)),
3928 tPtr->docWidth - tPtr->sel.x);
3929 tPtr->flags.ownsSelection = True;
3930 paintText(tPtr);
3932 return True;
3936 tb = (direction > 0) ? tb->next : tb->prior;
3937 if (tb) {
3938 pos = (direction > 0) ? 0 : tb->used;
3942 return False;
3945 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3947 if (!tPtr->flags.ownsSelection)
3948 return False;
3950 removeSelection(tPtr);
3952 if (replacement) {
3953 insertTextInteractively(tPtr, replacement, strlen(replacement));
3954 updateCursorPosition(tPtr);
3955 paintText(tPtr);
3958 return True;