Skip no_appicon apps in the application icon list
[wmaker-crm.git] / WINGs / wtext.c
blob827d19a821ee4d8fdcf560136bfb25f418b2ed79
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, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
631 if (tPtr->bgPixmap) {
632 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
633 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
634 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
643 done = False;
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
649 goto _copy_area;
651 while (tb) {
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
656 if (dir) {
657 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
658 break;
659 } else {
660 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
661 break;
664 _getSibling:
665 if (dir) {
666 if (tb->next)
667 tb = tb->next;
668 else
669 break;
670 } else {
671 if (tb->prior)
672 tb = tb->prior;
673 else
674 break;
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for (s = 0; s < tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
708 if (sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
711 rect.x, rect.y, rect.width, rect.height);
715 len = tb->sections[s].end - tb->sections[s].begin;
716 text = &(tb->text[tb->sections[s].begin]);
717 y = tb->sections[s].y - tPtr->vpos;
718 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
720 if (!tPtr->flags.monoFont && tb->underlined) {
721 XDrawLine(dpy, tPtr->db, WMColorGC(color),
722 tb->sections[s].x - tPtr->hpos,
723 y + font->y + 1,
724 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
727 tb = (!done ? tb->next : NULL);
730 /* now , show all graphic items that can be viewed */
731 c = WMGetArrayItemCount(tPtr->gfxItems);
732 if (c > 0 && !tPtr->flags.monoFont) {
733 int j, h;
735 for (j = 0; j < c; j++) {
736 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
738 /* if it's not viewable, and mapped, unmap it */
739 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
740 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
742 if (tb->object) {
743 if ((W_VIEW(tb->d.widget))->flags.mapped) {
744 WMUnmapWidget(tb->d.widget);
747 } else {
748 /* if it's viewable, and not mapped, map it */
749 if (tb->object) {
750 W_View *view = W_VIEW(tb->d.widget);
752 if (!view->flags.realized)
753 WMRealizeWidget(tb->d.widget);
754 if (!view->flags.mapped) {
755 XMapWindow(view->screen->display, view->window);
756 XFlush(view->screen->display);
757 view->flags.mapped = 1;
761 if (tb->object) {
762 WMMoveWidget(tb->d.widget,
763 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
764 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
765 h = WMWidgetHeight(tb->d.widget) + 1;
767 } else {
768 WMDrawPixmap(tb->d.pixmap, tPtr->db,
769 tb->sections[0].x - tPtr->hpos,
770 tb->sections[0].y - tPtr->vpos);
771 h = tb->d.pixmap->height + 1;
775 if (tPtr->flags.ownsSelection) {
776 XRectangle rect;
778 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
779 Drawable d = (0 && tb->object ?
780 (WMWidgetView(tb->d.widget))->window : tPtr->db);
782 tb->selected = True;
783 XFillRectangle(dpy, d, tPtr->stippledGC,
784 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
785 rect.x, rect.y, rect.width, rect.height);
789 if (!tPtr->flags.monoFont && tb->underlined) {
790 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
791 tb->sections[0].x - tPtr->hpos,
792 tb->sections[0].y + h - tPtr->vpos,
793 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos);
800 _copy_area:
801 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
802 int y = tPtr->cursor.y - tPtr->vpos;
803 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
804 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
807 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
808 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
810 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
812 if (tPtr->ruler && tPtr->flags.rulerShown)
813 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
817 static void mouseOverObject(Text * tPtr, int x, int y)
819 TextBlock *tb;
820 Bool result = False;
822 x -= tPtr->visible.x;
823 x += tPtr->hpos;
824 y -= tPtr->visible.y;
825 y += tPtr->vpos;
827 if (tPtr->flags.ownsSelection) {
828 if (tPtr->sel.x <= x
829 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
830 tPtr->flags.isOverGraphic = 1;
831 result = True;
835 if (!result) {
836 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
838 if (c < 1)
839 tPtr->flags.isOverGraphic = 0;
841 for (j = 0; j < c; j++) {
842 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
844 if (!tb || !tb->sections) {
845 tPtr->flags.isOverGraphic = 0;
846 return;
849 if (!tb->object) {
850 if (tb->sections[0].x <= x
851 && tb->sections[0].y <= y
852 && tb->sections[0].x + tb->sections[0].w >= x
853 && tb->sections[0].y + tb->d.pixmap->height >= y) {
854 tPtr->flags.isOverGraphic = 3;
855 result = True;
856 break;
863 if (!result)
864 tPtr->flags.isOverGraphic = 0;
866 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
868 XSetWindowAttributes attribs;
869 attribs.cursor = tPtr->view->attribs.cursor;
870 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
874 #if DO_BLINK
876 static void blinkCursor(void *data)
878 Text *tPtr = (Text *) data;
880 if (tPtr->flags.cursorShown) {
881 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
882 } else {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
885 paintText(tPtr);
886 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
888 #endif
890 static void updateCursorPosition(Text * tPtr)
892 TextBlock *tb = NULL;
893 int x, y, h, s;
895 if (tPtr->flags.needsLayOut)
896 layOutDocument(tPtr);
898 if (!(tb = tPtr->currentTextBlock)) {
899 if (!(tb = tPtr->firstTextBlock)) {
900 WMFont *font = tPtr->dFont;
901 tPtr->tpos = 0;
902 tPtr->cursor.h = font->height + abs(font->height - font->y);
904 tPtr->cursor.y = 2;
905 tPtr->cursor.x = 2;
906 return;
910 if (tb->blank) {
911 tPtr->tpos = 0;
912 y = tb->sections[0].y;
913 h = tb->sections[0].h;
914 x = tb->sections[0].x;
916 } else if (tb->graphic) {
917 y = tb->sections[0].y;
918 h = tb->sections[0].h;
919 x = tb->sections[0].x;
920 if (tPtr->tpos == 1)
921 x += tb->sections[0].w;
923 } else {
924 if (tPtr->tpos > tb->used)
925 tPtr->tpos = tb->used;
927 for (s = 0; s < tb->nsections - 1; s++) {
929 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
930 break;
933 y = tb->sections[s]._y;
934 h = tb->sections[s].h;
935 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
936 &tb->text[tb->sections[s].begin],
937 tPtr->tpos - tb->sections[s].begin);
940 tPtr->cursor.y = y;
941 tPtr->cursor.h = h;
942 tPtr->cursor.x = x;
944 /* scroll the bars if the cursor is not visible */
945 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
946 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
947 tPtr->vpos +=
948 (tPtr->cursor.y + tPtr->cursor.h + 10
949 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
950 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
951 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
956 updateScrollers(tPtr);
959 static void cursorToTextPosition(Text * tPtr, int x, int y)
961 TextBlock *tb = NULL;
962 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
963 char *text;
965 if (tPtr->flags.needsLayOut)
966 layOutDocument(tPtr);
968 y += (tPtr->vpos - tPtr->visible.y);
969 if (y < 0)
970 y = 0;
972 x -= (tPtr->visible.x - 2);
973 if (x < 0)
974 x = 0;
976 /* clicked is relative to document, not window... */
977 tPtr->clicked.x = x;
978 tPtr->clicked.y = y;
980 if (!(tb = tPtr->currentTextBlock)) {
981 if (!(tb = tPtr->firstTextBlock)) {
982 WMFont *font = tPtr->dFont;
983 tPtr->tpos = 0;
984 tPtr->cursor.h = font->height + abs(font->height - font->y);
985 tPtr->cursor.y = 2;
986 tPtr->cursor.x = 2;
987 return;
991 /* first, which direction? Most likely, newly clicked
992 position will be close to previous */
993 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
994 return;
996 s = (dir ? 0 : tb->nsections - 1);
997 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
998 goto _doneV;
1001 /* get the first (or last) section of the TextBlock that
1002 lies about the vertical click point */
1003 done = False;
1004 while (!done && tb) {
1006 if (tPtr->flags.monoFont && tb->graphic) {
1007 if ((dir ? tb->next : tb->prior))
1008 tb = (dir ? tb->next : tb->prior);
1009 continue;
1012 s = (dir ? 0 : tb->nsections - 1);
1013 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1015 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1016 done = True;
1017 } else {
1018 dir ? s++ : s--;
1022 if (!done) {
1023 if ((dir ? tb->next : tb->prior)) {
1024 tb = (dir ? tb->next : tb->prior);
1025 } else {
1026 pos = tb->used;
1027 break; /* goto _doneH; */
1032 if (s < 0 || s >= tb->nsections) {
1033 s = (dir ? tb->nsections - 1 : 0);
1036 _doneV:
1037 /* we have the line, which TextBlock on that line is it? */
1038 pos = (dir ? 0 : tb->sections[s].begin);
1039 if (tPtr->flags.monoFont && tb->graphic) {
1040 TextBlock *hold = tb;
1041 tb = getFirstNonGraphicBlockFor(hold, dir);
1043 if (!tb) {
1044 tPtr->tpos = 0;
1045 tb = hold;
1046 s = 0;
1047 goto _doNothing;
1051 if (tb->blank)
1052 _w = 0;
1054 _y = tb->sections[s]._y;
1056 while (tb) {
1058 if (tPtr->flags.monoFont && tb->graphic) {
1059 tb = (dir ? tb->next : tb->prior);
1060 continue;
1063 if (dir) {
1064 if (tb->graphic) {
1065 if (tb->object)
1066 _w = WMWidgetWidth(tb->d.widget) - 5;
1067 else
1068 _w = tb->d.pixmap->width - 5;
1070 if (tb->sections[0].x + _w >= x)
1071 break;
1072 } else {
1073 text = &(tb->text[tb->sections[s].begin]);
1074 len = tb->sections[s].end - tb->sections[s].begin;
1075 _w = WMWidthOfString(tb->d.font, text, len);
1076 if (tb->sections[s].x + _w >= x)
1077 break;
1080 } else {
1081 if (tb->sections[s].x <= x)
1082 break;
1085 if ((dir ? tb->next : tb->prior)) {
1086 TextBlock *nxt = (dir ? tb->next : tb->prior);
1087 if (tPtr->flags.monoFont && nxt->graphic) {
1088 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1089 if (!nxt) {
1090 pos = (dir ? 0 : tb->sections[s].begin);
1091 tPtr->cursor.x = tb->sections[s].x;
1092 goto _doneH;
1096 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1097 /* this must be the last/first on this line. stop */
1098 pos = (dir ? tb->sections[s].end : 0);
1099 tPtr->cursor.x = tb->sections[s].x;
1100 if (!tb->blank) {
1101 if (tb->graphic) {
1102 if (tb->object)
1103 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1104 else
1105 tPtr->cursor.x += tb->d.pixmap->width;
1106 } else if (pos > tb->sections[s].begin) {
1107 tPtr->cursor.x +=
1108 WMWidthOfString(tb->d.font,
1109 &(tb->text[tb->sections[s].begin]),
1110 pos - tb->sections[s].begin);
1113 goto _doneH;
1117 if ((dir ? tb->next : tb->prior)) {
1118 tb = (dir ? tb->next : tb->prior);
1119 } else {
1120 done = True;
1121 break;
1124 if (tb)
1125 s = (dir ? 0 : tb->nsections - 1);
1128 /* we have said TextBlock, now where within it? */
1129 if (tb) {
1130 if (tb->graphic) {
1131 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1133 tPtr->cursor.x = tb->sections[0].x;
1135 if (x > tPtr->cursor.x + gw / 2) {
1136 pos = 1;
1137 tPtr->cursor.x += gw;
1138 } else {
1139 printf("first %d\n", tb->first);
1140 if (tb->prior) {
1141 if (tb->prior->graphic)
1142 pos = 1;
1143 else
1144 pos = tb->prior->used;
1145 tb = tb->prior;
1146 } else
1147 pos = 0;
1151 s = 0;
1152 goto _doneH;
1154 } else {
1155 WMFont *f = tb->d.font;
1156 len = tb->sections[s].end - tb->sections[s].begin;
1157 text = &(tb->text[tb->sections[s].begin]);
1159 _w = x - tb->sections[s].x;
1160 pos = 0;
1162 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1163 pos++;
1165 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1167 pos += tb->sections[s].begin;
1171 _doneH:
1172 if (tb->graphic) {
1173 tPtr->tpos = (pos <= 1) ? pos : 0;
1174 } else {
1175 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1177 _doNothing:
1178 if (!tb)
1179 printf("...for this app will surely crash :-)\n");
1181 tPtr->currentTextBlock = tb;
1182 tPtr->cursor.h = tb->sections[s].h;
1183 tPtr->cursor.y = tb->sections[s]._y;
1185 /* scroll the bars if the cursor is not visible */
1186 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1187 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1188 tPtr->vpos +=
1189 (tPtr->cursor.y + tPtr->cursor.h + 10
1190 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1191 updateScrollers(tPtr);
1192 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1193 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1194 updateScrollers(tPtr);
1201 static void updateScrollers(Text * tPtr)
1204 if (tPtr->flags.frozen)
1205 return;
1207 if (tPtr->vS) {
1208 if (tPtr->docHeight <= tPtr->visible.h) {
1209 WMSetScrollerParameters(tPtr->vS, 0, 1);
1210 tPtr->vpos = 0;
1211 } else {
1212 float hmax = (float)(tPtr->docHeight);
1213 WMSetScrollerParameters(tPtr->vS,
1214 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1215 (float)tPtr->visible.h / hmax);
1217 } else
1218 tPtr->vpos = 0;
1220 if (tPtr->hS) {
1221 if (tPtr->docWidth <= tPtr->visible.w) {
1222 WMSetScrollerParameters(tPtr->hS, 0, 1);
1223 tPtr->hpos = 0;
1224 } else {
1225 float wmax = (float)(tPtr->docWidth);
1226 WMSetScrollerParameters(tPtr->hS,
1227 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1228 (float)tPtr->visible.w / wmax);
1230 } else
1231 tPtr->hpos = 0;
1234 static void scrollersCallBack(WMWidget * w, void *self)
1236 Text *tPtr = (Text *) self;
1237 Bool scroll = False;
1238 int which;
1240 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1241 return;
1243 if (w == tPtr->vS) {
1244 int height;
1245 height = tPtr->visible.h;
1247 which = WMGetScrollerHitPart(tPtr->vS);
1248 switch (which) {
1250 case WSDecrementLine:
1251 if (tPtr->vpos > 0) {
1252 if (tPtr->vpos > 16)
1253 tPtr->vpos -= 16;
1254 else
1255 tPtr->vpos = 0;
1256 scroll = True;
1258 break;
1260 case WSIncrementLine:{
1261 int limit = tPtr->docHeight - height;
1262 if (tPtr->vpos < limit) {
1263 if (tPtr->vpos < limit - 16)
1264 tPtr->vpos += 16;
1265 else
1266 tPtr->vpos = limit;
1267 scroll = True;
1270 break;
1272 case WSDecrementPage:
1273 if (((int)tPtr->vpos - (int)height) >= 0)
1274 tPtr->vpos -= height;
1275 else
1276 tPtr->vpos = 0;
1278 scroll = True;
1279 break;
1281 case WSIncrementPage:
1282 tPtr->vpos += height;
1283 if (tPtr->vpos > (tPtr->docHeight - height))
1284 tPtr->vpos = tPtr->docHeight - height;
1285 scroll = True;
1286 break;
1288 case WSKnob:
1289 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1290 * (float)(tPtr->docHeight - height);
1291 scroll = True;
1292 break;
1294 case WSKnobSlot:
1295 case WSNoPart:
1296 break;
1298 scroll = (tPtr->vpos != tPtr->prevVpos);
1299 tPtr->prevVpos = tPtr->vpos;
1302 if (w == tPtr->hS) {
1303 int width = tPtr->visible.w;
1305 which = WMGetScrollerHitPart(tPtr->hS);
1306 switch (which) {
1308 case WSDecrementLine:
1309 if (tPtr->hpos > 0) {
1310 if (tPtr->hpos > 16)
1311 tPtr->hpos -= 16;
1312 else
1313 tPtr->hpos = 0;
1314 scroll = True;
1316 break;
1318 case WSIncrementLine:{
1319 int limit = tPtr->docWidth - width;
1320 if (tPtr->hpos < limit) {
1321 if (tPtr->hpos < limit - 16)
1322 tPtr->hpos += 16;
1323 else
1324 tPtr->hpos = limit;
1325 scroll = True;
1328 break;
1330 case WSDecrementPage:
1331 if (((int)tPtr->hpos - (int)width) >= 0)
1332 tPtr->hpos -= width;
1333 else
1334 tPtr->hpos = 0;
1336 scroll = True;
1337 break;
1339 case WSIncrementPage:
1340 tPtr->hpos += width;
1341 if (tPtr->hpos > (tPtr->docWidth - width))
1342 tPtr->hpos = tPtr->docWidth - width;
1343 scroll = True;
1344 break;
1346 case WSKnob:
1347 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1348 * (float)(tPtr->docWidth - width);
1349 scroll = True;
1350 break;
1352 case WSKnobSlot:
1353 case WSNoPart:
1354 break;
1356 scroll = (tPtr->hpos != tPtr->prevHpos);
1357 tPtr->prevHpos = tPtr->hpos;
1360 if (scroll) {
1361 updateScrollers(tPtr);
1362 paintText(tPtr);
1366 typedef struct {
1367 TextBlock *tb;
1368 unsigned short begin, end; /* what part of the text block */
1369 } myLineItems;
1371 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1373 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1374 WMFont *font;
1375 char *text;
1376 TextBlock *tb, *tbsame = NULL;
1378 if (!items || nitems == 0)
1379 return 0;
1381 for (i = 0; i < nitems; i++) {
1382 tb = items[i].tb;
1384 if (tb->graphic) {
1385 if (!tPtr->flags.monoFont) {
1386 if (tb->object) {
1387 WMWidget *wdt = tb->d.widget;
1388 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1389 if (tPtr->flags.alignment != WALeft)
1390 lw += WMWidgetWidth(wdt);
1391 } else {
1392 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1393 if (tPtr->flags.alignment != WALeft)
1394 lw += tb->d.pixmap->width;
1398 } else {
1399 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1400 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1401 max_d = 2;
1402 line_height = WMAX(line_height, font->height + max_d);
1403 text = &(tb->text[items[i].begin]);
1404 len = items[i].end - items[i].begin;
1405 if (tPtr->flags.alignment != WALeft)
1406 lw += WMWidthOfString(font, text, len);
1410 if (tPtr->flags.alignment == WARight) {
1411 j = tPtr->visible.w - lw;
1412 } else if (tPtr->flags.alignment == WACenter) {
1413 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1416 for (i = 0; i < nitems; i++) {
1417 tb = items[i].tb;
1419 if (tbsame == tb) { /* extend it, since it's on same line */
1420 tb->sections[tb->nsections - 1].end = items[i].end;
1421 n = tb->nsections - 1;
1422 } else {
1423 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1424 n = tb->nsections - 1;
1425 tb->sections[n]._y = y + max_d;
1426 tb->sections[n].max_d = max_d;
1427 tb->sections[n].x = x + j;
1428 tb->sections[n].h = line_height;
1429 tb->sections[n].begin = items[i].begin;
1430 tb->sections[n].end = items[i].end;
1433 tb->sections[n].last = (i + 1 == nitems);
1435 if (tb->graphic) {
1436 if (!tPtr->flags.monoFont) {
1437 if (tb->object) {
1438 WMWidget *wdt = tb->d.widget;
1439 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1440 tb->sections[n].w = WMWidgetWidth(wdt);
1441 } else {
1442 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1443 tb->sections[n].w = tb->d.pixmap->width;
1445 x += tb->sections[n].w;
1447 } else {
1448 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1449 len = items[i].end - items[i].begin;
1450 text = &(tb->text[items[i].begin]);
1452 tb->sections[n].y = y + line_height - font->y;
1453 tb->sections[n].w =
1454 WMWidthOfString(font,
1455 &(tb->text[tb->sections[n].begin]),
1456 tb->sections[n].end - tb->sections[n].begin);
1458 x += WMWidthOfString(font, text, len);
1461 tbsame = tb;
1464 return line_height;
1468 static void layOutDocument(Text * tPtr)
1470 TextBlock *tb;
1471 myLineItems *items = NULL;
1472 unsigned int itemsSize = 0, nitems = 0, begin, end;
1473 WMFont *font;
1474 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1475 char *start = NULL, *mark = NULL;
1477 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1478 return;
1480 assert(tPtr->visible.w > 20);
1482 tPtr->docWidth = tPtr->visible.w;
1483 x = tPtr->margins[tb->marginN].first;
1484 bmargin = tPtr->margins[tb->marginN].body;
1486 /* only partial layOut needed: re-Lay only affected textblocks */
1487 if (tPtr->flags.laidOut) {
1488 tb = tPtr->currentTextBlock;
1490 /* search backwards for textblocks on same line */
1491 while (tb->prior) {
1492 if (!tb->sections || tb->nsections < 1) {
1493 tb = tPtr->firstTextBlock;
1494 tPtr->flags.laidOut = False;
1495 y = 0;
1496 goto _layOut;
1499 if (!tb->prior->sections || tb->prior->nsections < 1) {
1500 tb = tPtr->firstTextBlock;
1501 tPtr->flags.laidOut = False;
1502 y = 0;
1503 goto _layOut;
1506 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1507 break;
1509 tb = tb->prior;
1512 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1513 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1514 tb->prior->sections[tb->prior->nsections - 1].h -
1515 tb->prior->sections[tb->prior->nsections - 1].max_d;
1516 } else {
1517 y = 0;
1521 _layOut:
1522 while (tb) {
1524 if (tb->sections && tb->nsections > 0) {
1525 wfree(tb->sections);
1526 tb->sections = NULL;
1527 tb->nsections = 0;
1530 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1531 TextBlock *next = tb->next;
1532 tPtr->currentTextBlock = tb;
1533 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1534 tb = next;
1535 tb->first = True;
1536 continue;
1539 if (tb->first && tb != tPtr->firstTextBlock) {
1540 y += layOutLine(tPtr, items, nitems, x, y);
1541 x = tPtr->margins[tb->marginN].first;
1542 bmargin = tPtr->margins[tb->marginN].body;
1543 nitems = 0;
1544 lw = 0;
1547 if (tb->graphic) {
1548 if (!tPtr->flags.monoFont) {
1549 if (tb->object)
1550 width = WMWidgetWidth(tb->d.widget);
1551 else
1552 width = tb->d.pixmap->width;
1554 if (width > tPtr->docWidth)
1555 tPtr->docWidth = width;
1557 lw += width;
1558 if (lw >= tPtr->visible.w - x) {
1559 y += layOutLine(tPtr, items, nitems, x, y);
1560 nitems = 0;
1561 x = bmargin;
1562 lw = width;
1565 if (nitems + 1 > itemsSize) {
1566 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1569 items[nitems].tb = tb;
1570 items[nitems].begin = 0;
1571 items[nitems].end = 0;
1572 nitems++;
1575 } else if ((start = tb->text)) {
1576 begin = end = 0;
1577 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1579 while (start) {
1580 mark = strchr(start, ' ');
1581 if (mark) {
1582 end += (int)(mark - start) + 1;
1583 start = mark + 1;
1584 } else {
1585 end += strlen(start);
1586 start = mark;
1589 if (end > tb->used)
1590 end = tb->used;
1592 if (end - begin > 0) {
1594 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1596 /* if it won't fit, char wrap it */
1597 if (width >= tPtr->visible.w) {
1598 char *t = &tb->text[begin];
1599 int l = end - begin, i = 0;
1600 do {
1601 width = WMWidthOfString(font, t, ++i);
1602 } while (width < tPtr->visible.w && i < l);
1603 if (i > 2)
1604 i--;
1605 end = begin + i;
1606 start = &tb->text[end];
1609 lw += width;
1612 if (lw >= tPtr->visible.w - x) {
1613 y += layOutLine(tPtr, items, nitems, x, y);
1614 lw = width;
1615 x = bmargin;
1616 nitems = 0;
1619 if (nitems + 1 > itemsSize) {
1620 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1623 items[nitems].tb = tb;
1624 items[nitems].begin = begin;
1625 items[nitems].end = end;
1626 nitems++;
1628 begin = end;
1632 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1633 if (0 && tPtr->flags.laidOut
1634 && tb->next && tb->next->sections && tb->next->nsections > 0
1635 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1636 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1637 TextBlock *ltb = tPtr->lastTextBlock;
1638 int ly = ltb->sections[ltb->nsections - 1]._y;
1639 int lh = ltb->sections[ltb->nsections - 1].h;
1640 int ss, sd;
1642 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1643 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1645 y += layOutLine(tPtr, items, nitems, x, y);
1646 ss = ly + lh - y;
1647 sd = tPtr->docHeight - y;
1649 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1650 y += tb->next->sections[0]._y - y;
1651 nitems = 0;
1652 printf("nitems%d\n", nitems);
1653 if (ss - sd != 0)
1654 y = tPtr->docHeight + ss - sd;
1656 break;
1657 } else {
1658 tPtr->flags.laidOut = False;
1662 tb = tb->next;
1665 if (nitems > 0)
1666 y += layOutLine(tPtr, items, nitems, x, y);
1668 if (tPtr->docHeight != y + 10) {
1669 tPtr->docHeight = y + 10;
1670 updateScrollers(tPtr);
1673 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1674 XEvent event;
1676 tPtr->flags.horizOnDemand = True;
1677 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1678 event.type = Expose;
1679 handleEvents(&event, (void *)tPtr);
1681 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1682 tPtr->flags.horizOnDemand = False;
1683 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1686 tPtr->flags.laidOut = True;
1688 if (items && itemsSize > 0)
1689 wfree(items);
1693 static void textDidResize(W_ViewDelegate * self, WMView * view)
1695 Text *tPtr = (Text *) view->self;
1696 unsigned short w = tPtr->view->size.width;
1697 unsigned short h = tPtr->view->size.height;
1698 unsigned short rh = 0, vw = 0, rel;
1700 rel = (tPtr->flags.relief == WRFlat);
1702 if (tPtr->ruler && tPtr->flags.rulerShown) {
1703 WMMoveWidget(tPtr->ruler, 2, 2);
1704 WMResizeWidget(tPtr->ruler, w - 4, 40);
1705 rh = 40;
1708 if (tPtr->vS) {
1709 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1710 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1711 vw = 20;
1712 WMSetRulerOffset(tPtr->ruler, 22);
1713 } else
1714 WMSetRulerOffset(tPtr->ruler, 2);
1716 if (tPtr->hS) {
1717 if (tPtr->vS) {
1718 WMMoveWidget(tPtr->hS, vw, h - 21);
1719 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1720 } else {
1721 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1722 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1726 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1727 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1728 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1729 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1730 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1731 tPtr->margins[0].right = tPtr->visible.w;
1733 if (tPtr->view->flags.realized) {
1735 if (tPtr->db) {
1736 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1737 tPtr->db = (Pixmap) NULL;
1740 if (tPtr->visible.w < 40)
1741 tPtr->visible.w = 40;
1742 if (tPtr->visible.h < 20)
1743 tPtr->visible.h = 20;
1745 if (!tPtr->db) {
1746 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1747 tPtr->view->window, tPtr->visible.w,
1748 tPtr->visible.h, tPtr->view->screen->depth);
1752 WMThawText(tPtr);
1755 W_ViewDelegate _TextViewDelegate = {
1756 NULL,
1757 NULL,
1758 textDidResize,
1759 NULL,
1760 NULL
1763 #define TEXT_BUFFER_INCR 8
1764 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1766 static void clearText(Text * tPtr)
1768 tPtr->vpos = tPtr->hpos = 0;
1769 tPtr->docHeight = tPtr->docWidth = 0;
1770 tPtr->cursor.x = -23;
1772 if (!tPtr->firstTextBlock)
1773 return;
1775 while (tPtr->currentTextBlock)
1776 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1778 tPtr->firstTextBlock = NULL;
1779 tPtr->currentTextBlock = NULL;
1780 tPtr->lastTextBlock = NULL;
1781 WMEmptyArray(tPtr->gfxItems);
1784 /* possibly remove a single character from the currentTextBlock,
1785 or if there's a selection, remove it...
1786 note that Delete and Backspace are treated differently */
1787 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1789 TextBlock *tb;
1790 Bool back = (Bool) (ksym == XK_BackSpace);
1791 Bool done = 1, wasFirst = 0;
1793 if (!tPtr->flags.editable)
1794 return;
1796 if (!(tb = tPtr->currentTextBlock))
1797 return;
1799 if (tPtr->flags.ownsSelection) {
1800 if (removeSelection(tPtr))
1801 layOutDocument(tPtr);
1802 return;
1805 wasFirst = tb->first;
1806 if (back && tPtr->tpos < 1) {
1807 if (tb->prior) {
1808 if (tb->prior->blank) {
1809 tPtr->currentTextBlock = tb->prior;
1810 WMRemoveTextBlock(tPtr);
1811 tPtr->currentTextBlock = tb;
1812 tb->first = True;
1813 layOutDocument(tPtr);
1814 return;
1815 } else {
1816 if (tb->blank) {
1817 TextBlock *prior = tb->prior;
1818 tPtr->currentTextBlock = tb;
1819 WMRemoveTextBlock(tPtr);
1820 tb = prior;
1821 } else {
1822 tb = tb->prior;
1825 if (tb->graphic)
1826 tPtr->tpos = 1;
1827 else
1828 tPtr->tpos = tb->used;
1830 tPtr->currentTextBlock = tb;
1831 done = 1;
1832 if (wasFirst) {
1833 if (tb->next)
1834 tb->next->first = False;
1835 layOutDocument(tPtr);
1836 return;
1842 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1843 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1844 if (back)
1845 tPtr->tpos--;
1846 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1847 tb->used--;
1848 done = 0;
1851 /* if there are no characters left to back over in the textblock,
1852 but it still has characters to the right of the cursor: */
1853 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1854 || tb->graphic) {
1856 /* no more chars, and it's marked as blank? */
1857 if (tb->blank) {
1858 TextBlock *sibling = (back ? tb->prior : tb->next);
1860 if (tb->used == 0 || tb->graphic)
1861 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1863 if (sibling) {
1864 tPtr->currentTextBlock = sibling;
1865 if (tb->graphic)
1866 tPtr->tpos = (back ? 1 : 0);
1867 else
1868 tPtr->tpos = (back ? sibling->used : 0);
1870 /* no more chars, so mark it as blank */
1871 } else if (tb->used == 0) {
1872 tb->blank = 1;
1873 } else if (tb->graphic) {
1874 Bool hasNext = (tb->next != NULL);
1876 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1877 if (hasNext) {
1878 tPtr->tpos = 0;
1879 } else if (tPtr->currentTextBlock) {
1880 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1882 } else
1883 printf("DEBUG: unaccounted for... catch this!\n");
1886 layOutDocument(tPtr);
1889 static void insertTextInteractively(Text * tPtr, char *text, int len)
1891 TextBlock *tb;
1892 char *newline = NULL;
1894 if (!tPtr->flags.editable) {
1895 return;
1898 if (len < 1 || !text)
1899 return;
1901 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1902 return;
1904 if (tPtr->flags.ownsSelection)
1905 removeSelection(tPtr);
1907 if (tPtr->flags.ignoreNewLine) {
1908 int i;
1909 for (i = 0; i < len; i++) {
1910 if (text[i] == '\n')
1911 text[i] = ' ';
1915 tb = tPtr->currentTextBlock;
1916 if (!tb || tb->graphic) {
1917 tPtr->tpos = 0;
1918 WMAppendTextStream(tPtr, text);
1919 layOutDocument(tPtr);
1920 return;
1923 if ((newline = strchr(text, '\n'))) {
1924 int nlen = (int)(newline - text);
1925 int s = tb->used - tPtr->tpos;
1927 if (!tb->blank && nlen > 0) {
1928 char *save = NULL;
1930 if (s > 0) {
1931 save = wmalloc(s);
1932 memcpy(save, &tb->text[tPtr->tpos], s);
1933 tb->used -= (tb->used - tPtr->tpos);
1935 insertTextInteractively(tPtr, text, nlen);
1936 newline++;
1937 WMAppendTextStream(tPtr, newline);
1938 if (s > 0) {
1939 insertTextInteractively(tPtr, save, s);
1940 wfree(save);
1942 } else {
1943 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1945 unsigned short savePos = tPtr->tpos;
1946 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1947 tb->d.font, tb->color, True,
1948 tb->used - tPtr->tpos);
1950 if (tb->sections[0].end == tPtr->tpos)
1951 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1952 NULL, tb->d.font,
1953 tb->color, True, 0));
1955 tb->used = savePos;
1956 WMAppendTextBlock(tPtr, ntb);
1957 tPtr->tpos = 0;
1959 } else if (tPtr->tpos == tb->used) {
1960 if (tPtr->flags.indentNewLine) {
1961 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1962 " ", tb->d.font,
1963 tb->color, True, 4));
1964 tPtr->tpos = 4;
1965 } else {
1966 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1967 NULL, tb->d.font,
1968 tb->color, True, 0));
1969 tPtr->tpos = 0;
1971 } else if (tPtr->tpos == 0) {
1972 if (tPtr->flags.indentNewLine) {
1973 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1974 " ", tb->d.font,
1975 tb->color, True, 4));
1976 } else {
1977 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1978 NULL, tb->d.font,
1979 tb->color, True, 0));
1981 tPtr->tpos = 0;
1982 if (tPtr->currentTextBlock->next)
1983 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1986 } else {
1987 if (tb->used + len >= tb->allocated) {
1988 tb->allocated = reqBlockSize(tb->used + len);
1989 tb->text = wrealloc(tb->text, tb->allocated);
1992 if (tb->blank) {
1993 memcpy(tb->text, text, len);
1994 tb->used = len;
1995 tPtr->tpos = len;
1996 tb->text[tb->used] = 0;
1997 tb->blank = False;
1999 } else {
2000 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
2001 memmove(&tb->text[tPtr->tpos], text, len);
2002 tb->used += len;
2003 tPtr->tpos += len;
2004 tb->text[tb->used] = 0;
2009 layOutDocument(tPtr);
2012 static void selectRegion(Text * tPtr, int x, int y)
2015 if (x < 0 || y < 0)
2016 return;
2018 y += (tPtr->flags.rulerShown ? 40 : 0);
2019 y += tPtr->vpos;
2020 if (y > 10)
2021 y -= 10; /* the original offset */
2023 x -= tPtr->visible.x - 2;
2024 if (x < 0)
2025 x = 0;
2027 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2028 tPtr->sel.w = abs(tPtr->clicked.x - x);
2029 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2030 tPtr->sel.h = abs(tPtr->clicked.y - y);
2032 tPtr->flags.ownsSelection = True;
2033 paintText(tPtr);
2036 static void releaseSelection(Text * tPtr)
2038 TextBlock *tb = tPtr->firstTextBlock;
2040 while (tb) {
2041 tb->selected = False;
2042 tb = tb->next;
2044 tPtr->flags.ownsSelection = False;
2045 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2047 paintText(tPtr);
2050 WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2052 Text *tPtr = view->self;
2053 Display *dpy = tPtr->view->screen->display;
2054 Atom _TARGETS;
2055 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2056 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2057 WMData *data = NULL;
2059 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2060 char *text = WMGetTextSelectedStream(tPtr);
2062 if (text) {
2063 data = WMCreateDataWithBytes(text, strlen(text));
2064 WMSetDataFormat(data, TYPETEXT);
2066 *type = target;
2067 return data;
2068 } else
2069 printf("didn't get it\n");
2071 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2072 if (target == _TARGETS) {
2073 Atom *ptr;
2075 ptr = wmalloc(4 * sizeof(Atom));
2076 ptr[0] = _TARGETS;
2077 ptr[1] = XA_STRING;
2078 ptr[2] = TEXT;
2079 ptr[3] = COMPOUND_TEXT;
2081 data = WMCreateDataWithBytes(ptr, 4 * 4);
2082 WMSetDataFormat(data, 32);
2084 *type = target;
2085 return data;
2088 return NULL;
2091 static void lostHandler(WMView * view, Atom selection, void *cdata)
2093 releaseSelection((WMText *) view->self);
2096 static WMSelectionProcs selectionHandler = {
2097 requestHandler, lostHandler, NULL
2100 static void ownershipObserver(void *observerData, WMNotification * notification)
2102 if (observerData != WMGetNotificationClientData(notification))
2103 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2106 static void autoSelectText(Text * tPtr, int clicks)
2108 int x, start;
2109 TextBlock *tb;
2110 char *mark = NULL, behind, ahead;
2112 if (!(tb = tPtr->currentTextBlock))
2113 return;
2115 if (clicks == 2) {
2117 switch (tb->text[tPtr->tpos]) {
2118 case ' ':
2119 return;
2121 case '<': case '>': behind = '<'; ahead = '>'; break;
2122 case '{': case '}': behind = '{'; ahead = '}'; break;
2123 case '[': case ']': behind = '['; ahead = ']'; break;
2125 default:
2126 behind = ahead = ' ';
2129 tPtr->sel.y = tPtr->cursor.y + 5;
2130 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2132 if (tb->graphic) {
2133 tPtr->sel.x = tb->sections[0].x;
2134 tPtr->sel.w = tb->sections[0].w;
2135 } else {
2136 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2138 start = tPtr->tpos;
2139 while (start > 0 && tb->text[start - 1] != behind)
2140 start--;
2142 x = tPtr->cursor.x;
2143 if (tPtr->tpos > start) {
2144 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2146 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2148 if ((mark = strchr(&tb->text[start], ahead))) {
2149 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2150 (int)(mark - &tb->text[start]));
2151 } else if (tb->used > start) {
2152 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2156 } else if (clicks == 3) {
2157 TextBlock *cur = tb;
2159 while (tb && !tb->first) {
2160 tb = tb->prior;
2162 tPtr->sel.y = tb->sections[0]._y;
2164 tb = cur;
2165 while (tb->next && !tb->next->first) {
2166 tb = tb->next;
2168 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2170 tPtr->sel.x = 0;
2171 tPtr->sel.w = tPtr->docWidth;
2172 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2175 if (!tPtr->flags.ownsSelection) {
2176 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2177 tPtr->flags.ownsSelection = True;
2179 paintText(tPtr);
2183 # if 0
2184 static void fontChanged(void *observerData, WMNotification * notification)
2186 WMText *tPtr = (WMText *) observerData;
2187 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2188 printf("fontChanged\n");
2190 if (!tPtr || !font)
2191 return;
2193 if (tPtr->flags.ownsSelection)
2194 WMSetTextSelectionFont(tPtr, font);
2196 #endif
2198 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2200 char buffer[64];
2201 KeySym ksym;
2202 int control_pressed = False;
2203 TextBlock *tb = NULL;
2205 if (((XKeyEvent *) event)->state & ControlMask)
2206 control_pressed = True;
2207 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2209 switch (ksym) {
2211 case XK_Home:
2212 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2213 tPtr->tpos = 0;
2214 updateCursorPosition(tPtr);
2215 paintText(tPtr);
2216 break;
2218 case XK_End:
2219 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2220 if (tPtr->currentTextBlock->graphic)
2221 tPtr->tpos = 1;
2222 else
2223 tPtr->tpos = tPtr->currentTextBlock->used;
2225 updateCursorPosition(tPtr);
2226 paintText(tPtr);
2227 break;
2229 case XK_Left:
2230 if (!(tb = tPtr->currentTextBlock))
2231 break;
2232 if (tb->graphic)
2233 goto L_imaGFX;
2235 if (tPtr->tpos == 0) {
2236 L_imaGFX:
2237 if (tb->prior) {
2238 tPtr->currentTextBlock = tb->prior;
2239 if (tPtr->currentTextBlock->graphic)
2240 tPtr->tpos = 1;
2241 else
2242 tPtr->tpos = tPtr->currentTextBlock->used;
2244 if (!tb->first && tPtr->tpos > 0)
2245 tPtr->tpos--;
2246 } else
2247 tPtr->tpos = 0;
2248 } else
2249 tPtr->tpos--;
2250 updateCursorPosition(tPtr);
2251 paintText(tPtr);
2252 break;
2254 case XK_Right:
2255 if (!(tb = tPtr->currentTextBlock))
2256 break;
2257 if (tb->graphic)
2258 goto R_imaGFX;
2259 if (tPtr->tpos == tb->used) {
2260 R_imaGFX:
2261 if (tb->next) {
2262 tPtr->currentTextBlock = tb->next;
2263 tPtr->tpos = 0;
2264 if (!tb->next->first && tb->next->used > 0)
2265 tPtr->tpos++;
2266 } else {
2267 if (tb->graphic)
2268 tPtr->tpos = 1;
2269 else
2270 tPtr->tpos = tb->used;
2272 } else
2273 tPtr->tpos++;
2274 updateCursorPosition(tPtr);
2275 paintText(tPtr);
2276 break;
2278 case XK_Down:
2279 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2280 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2281 paintText(tPtr);
2282 break;
2284 case XK_Up:
2285 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2286 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2287 paintText(tPtr);
2288 break;
2290 case XK_BackSpace:
2291 case XK_Delete:
2292 #ifdef XK_KP_Delete
2293 case XK_KP_Delete:
2294 #endif
2295 deleteTextInteractively(tPtr, ksym);
2296 updateCursorPosition(tPtr);
2297 paintText(tPtr);
2298 break;
2300 case XK_Control_R:
2301 case XK_Control_L:
2302 control_pressed = True;
2303 break;
2305 case XK_Tab:
2306 insertTextInteractively(tPtr, " ", 4);
2307 updateCursorPosition(tPtr);
2308 paintText(tPtr);
2309 break;
2311 case XK_Return:
2312 *buffer = '\n';
2313 default:
2314 if (*buffer != 0 && !control_pressed) {
2315 insertTextInteractively(tPtr, buffer, strlen(buffer));
2316 updateCursorPosition(tPtr);
2317 paintText(tPtr);
2319 } else if (control_pressed && ksym == XK_r) {
2320 Bool i = !tPtr->flags.rulerShown;
2321 WMShowTextRuler(tPtr, i);
2322 tPtr->flags.rulerShown = i;
2323 } else if (control_pressed && *buffer == '\a') {
2324 XBell(tPtr->view->screen->display, 0);
2325 } else {
2326 WMRelayToNextResponder(tPtr->view, event);
2330 if (!control_pressed && tPtr->flags.ownsSelection) {
2331 releaseSelection(tPtr);
2335 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2337 Text *tPtr = (Text *) view->self;
2338 char *text;
2340 tPtr->flags.waitingForSelection = 0;
2342 if (data) {
2343 text = (char *)WMDataBytes(data);
2345 if (tPtr->parser) {
2346 (tPtr->parser) (tPtr, (void *)text);
2347 layOutDocument(tPtr);
2348 } else
2349 insertTextInteractively(tPtr, text, strlen(text));
2350 updateCursorPosition(tPtr);
2351 paintText(tPtr);
2353 } else {
2354 int n;
2356 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2358 if (text) {
2359 text[n] = 0;
2360 if (tPtr->parser) {
2361 (tPtr->parser) (tPtr, (void *)text);
2362 layOutDocument(tPtr);
2363 } else
2364 insertTextInteractively(tPtr, text, n);
2365 updateCursorPosition(tPtr);
2366 paintText(tPtr);
2368 XFree(text);
2374 static void handleActionEvents(XEvent * event, void *data)
2376 Text *tPtr = (Text *) data;
2377 Display *dpy = event->xany.display;
2378 KeySym ksym;
2380 switch (event->type) {
2381 case KeyPress:
2382 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2383 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2384 tPtr->flags.extendSelection = True;
2385 return;
2388 if (tPtr->flags.focused) {
2389 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2390 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2391 GrabModeAsync, GrabModeAsync, None,
2392 tPtr->view->screen->invisibleCursor, CurrentTime);
2393 tPtr->flags.pointerGrabbed = True;
2394 handleTextKeyPress(tPtr, event);
2397 break;
2399 case KeyRelease:
2400 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2401 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2402 tPtr->flags.extendSelection = False;
2403 return;
2404 /* end modify flag so selection can be extended */
2406 break;
2408 case MotionNotify:
2410 if (tPtr->flags.pointerGrabbed) {
2411 tPtr->flags.pointerGrabbed = False;
2412 XUngrabPointer(dpy, CurrentTime);
2415 if (tPtr->flags.waitingForSelection)
2416 break;
2418 if ((event->xmotion.state & Button1Mask)) {
2420 if (WMIsDraggingFromView(tPtr->view)) {
2421 WMDragImageFromView(tPtr->view, event);
2422 break;
2425 if (!tPtr->flags.ownsSelection) {
2426 WMCreateSelectionHandler(tPtr->view,
2427 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2428 tPtr->flags.ownsSelection = True;
2430 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2431 break;
2434 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2435 break;
2437 case ButtonPress:
2439 if (tPtr->flags.pointerGrabbed) {
2440 tPtr->flags.pointerGrabbed = False;
2441 XUngrabPointer(dpy, CurrentTime);
2442 break;
2445 if (tPtr->flags.waitingForSelection)
2446 break;
2448 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2449 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2450 return;
2453 if (tPtr->flags.ownsSelection)
2454 releaseSelection(tPtr);
2456 if (event->xbutton.button == Button1) {
2457 TextBlock *tb = tPtr->currentTextBlock;
2459 if (WMIsDoubleClick(event)) {
2461 tPtr->lastClickTime = event->xbutton.time;
2462 if (tb && tb->graphic && !tb->object) {
2463 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2464 char *desc;
2466 desc = wmalloc(tb->used + 1);
2467 memcpy(desc, tb->text, tb->used);
2468 desc[tb->used] = 0;
2469 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2470 wfree(desc);
2472 } else {
2473 autoSelectText(tPtr, 2);
2475 break;
2476 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2477 tPtr->lastClickTime = event->xbutton.time;
2478 autoSelectText(tPtr, 3);
2479 break;
2482 if (!tPtr->flags.focused) {
2483 WMSetFocusToWidget(tPtr);
2484 tPtr->flags.focused = True;
2485 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2487 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2488 WMDragImageFromView(tPtr->view, event);
2489 break;
2492 tPtr->lastClickTime = event->xbutton.time;
2493 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2494 paintText(tPtr);
2497 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2498 WMScrollText(tPtr, 16);
2499 break;
2502 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2503 WMScrollText(tPtr, -16);
2504 break;
2507 if (event->xbutton.button == Button2) {
2508 char *text = NULL;
2509 int n;
2511 if (!tPtr->flags.editable) {
2512 XBell(dpy, 0);
2513 break;
2516 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2517 event->xbutton.time, pasteText, NULL)) {
2519 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2520 tPtr->flags.waitingForSelection = 0;
2522 if (text) {
2523 text[n] = 0;
2525 if (tPtr->parser) {
2526 (tPtr->parser) (tPtr, (void *)text);
2527 layOutDocument(tPtr);
2528 } else
2529 insertTextInteractively(tPtr, text, n);
2531 XFree(text);
2532 #if 0
2533 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2534 (void *)WMInsertTextEvent);
2535 #endif
2536 updateCursorPosition(tPtr);
2537 paintText(tPtr);
2539 } else {
2540 tPtr->flags.waitingForSelection = True;
2543 break;
2546 case ButtonRelease:
2547 if (tPtr->flags.pointerGrabbed) {
2548 tPtr->flags.pointerGrabbed = False;
2549 XUngrabPointer(dpy, CurrentTime);
2550 break;
2553 if (tPtr->flags.waitingForSelection)
2554 break;
2556 if (WMIsDraggingFromView(tPtr->view))
2557 WMDragImageFromView(tPtr->view, event);
2562 static void handleEvents(XEvent * event, void *data)
2564 Text *tPtr = (Text *) data;
2566 switch (event->type) {
2567 case Expose:
2569 if (event->xexpose.count != 0)
2570 break;
2572 if (tPtr->hS) {
2573 if (!(W_VIEW(tPtr->hS))->flags.realized)
2574 WMRealizeWidget(tPtr->hS);
2577 if (tPtr->vS) {
2578 if (!(W_VIEW(tPtr->vS))->flags.realized)
2579 WMRealizeWidget(tPtr->vS);
2582 if (tPtr->ruler) {
2583 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2584 WMRealizeWidget(tPtr->ruler);
2588 if (!tPtr->db)
2589 textDidResize(tPtr->view->delegate, tPtr->view);
2591 paintText(tPtr);
2592 break;
2594 case FocusIn:
2595 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2596 != tPtr->view)
2597 return;
2598 tPtr->flags.focused = True;
2599 #if DO_BLINK
2600 if (tPtr->flags.editable && !tPtr->timerID) {
2601 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2603 #endif
2605 break;
2607 case FocusOut:
2608 tPtr->flags.focused = False;
2609 paintText(tPtr);
2610 #if DO_BLINK
2611 if (tPtr->timerID) {
2612 WMDeleteTimerHandler(tPtr->timerID);
2613 tPtr->timerID = NULL;
2615 #endif
2616 break;
2618 case DestroyNotify:
2619 clearText(tPtr);
2620 if (tPtr->db)
2621 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2622 if (tPtr->gfxItems)
2623 WMEmptyArray(tPtr->gfxItems);
2624 #if DO_BLINK
2625 if (tPtr->timerID)
2626 WMDeleteTimerHandler(tPtr->timerID);
2627 #endif
2628 WMReleaseFont(tPtr->dFont);
2629 WMReleaseColor(tPtr->dColor);
2630 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2631 WMRemoveNotificationObserver(tPtr);
2633 WMFreeArray(tPtr->xdndSourceTypes);
2634 WMFreeArray(tPtr->xdndDestinationTypes);
2636 wfree(tPtr);
2638 break;
2643 static void insertPlainText(Text * tPtr, char *text)
2645 char *start, *mark;
2646 void *tb = NULL;
2648 start = text;
2649 while (start) {
2650 mark = strchr(start, '\n');
2651 if (mark) {
2652 tb = WMCreateTextBlockWithText(tPtr,
2653 start, tPtr->dFont,
2654 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2655 start = mark + 1;
2656 tPtr->flags.first = True;
2657 } else {
2658 if (start && strlen(start)) {
2659 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2660 tPtr->dColor, tPtr->flags.first, strlen(start));
2661 } else
2662 tb = NULL;
2663 tPtr->flags.first = False;
2664 start = mark;
2667 if (tPtr->flags.prepend)
2668 WMPrependTextBlock(tPtr, tb);
2669 else
2670 WMAppendTextBlock(tPtr, tb);
2674 static void rulerMoveCallBack(WMWidget * w, void *self)
2676 Text *tPtr = (Text *) self;
2678 if (!tPtr)
2679 return;
2680 if (W_CLASS(tPtr) != WC_Text)
2681 return;
2683 paintText(tPtr);
2686 static void rulerReleaseCallBack(WMWidget * w, void *self)
2688 Text *tPtr = (Text *) self;
2690 if (!tPtr)
2691 return;
2692 if (W_CLASS(tPtr) != WC_Text)
2693 return;
2695 WMThawText(tPtr);
2696 return;
2699 static WMArray *dropDataTypes(WMView * self)
2701 return ((Text *) self->self)->xdndSourceTypes;
2704 static WMDragOperationType wantedDropOperation(WMView * self)
2706 return WDOperationCopy;
2709 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2711 return (allowedOperation == WDOperationCopy);
2714 static WMData *fetchDragData(WMView * self, char *type)
2716 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2717 char *desc;
2718 WMData *data;
2720 if (strcmp(type, "text/plain")) {
2721 if (!tb)
2722 return NULL;
2724 desc = wmalloc(tb->used + 1);
2725 memcpy(desc, tb->text, tb->used);
2726 desc[tb->used] = 0;
2727 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2729 wfree(desc);
2731 return data;
2734 return NULL;
2737 static WMDragSourceProcs _DragSourceProcs = {
2738 dropDataTypes,
2739 wantedDropOperation,
2740 NULL,
2741 acceptDropOperation,
2742 NULL,
2743 NULL,
2744 fetchDragData
2747 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2749 return ((Text *) self->self)->xdndDestinationTypes;
2752 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2754 return WDOperationCopy;
2757 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2759 WMText *tPtr = (WMText *) self->self;
2760 WMData *data;
2761 char *colorName;
2762 WMColor *color;
2764 if (tPtr) {
2766 /* only one required type, implies only one drop data */
2768 /* get application/X-color if any */
2769 data = (WMData *) WMPopFromArray(dropData);
2770 if (data != NULL) {
2771 colorName = (char *)WMDataBytes(data);
2772 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2774 if (color) {
2775 WMSetTextSelectionColor(tPtr, color);
2776 WMReleaseColor(color);
2782 static WMDragDestinationProcs _DragDestinationProcs = {
2783 NULL,
2784 requiredDataTypes,
2785 allowedOperation,
2786 NULL,
2787 performDragOperation,
2788 NULL
2791 char *getStream(WMText * tPtr, int sel, int array)
2793 TextBlock *tb = NULL;
2794 char *text = NULL;
2795 unsigned long where = 0;
2797 if (!tPtr)
2798 return NULL;
2800 if (!(tb = tPtr->firstTextBlock))
2801 return NULL;
2803 if (tPtr->writer) {
2804 (tPtr->writer) (tPtr, (void *)text);
2805 return text;
2808 tb = tPtr->firstTextBlock;
2809 while (tb) {
2811 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2813 if (!sel || (tb->graphic && tb->selected)) {
2815 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2816 && tb != tPtr->firstTextBlock) {
2817 text = wrealloc(text, where + 1);
2818 text[where++] = '\n';
2821 if (tb->blank)
2822 goto _gSnext;
2824 if (tb->graphic && array) {
2825 text = wrealloc(text, where + 4);
2826 text[where++] = 0xFA;
2827 text[where++] = (tb->used >> 8) & 0x0ff;
2828 text[where++] = tb->used & 0x0ff;
2829 text[where++] = tb->allocated; /* extra info */
2831 text = wrealloc(text, where + tb->used);
2832 memcpy(&text[where], tb->text, tb->used);
2833 where += tb->used;
2835 } else if (sel && tb->selected) {
2837 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2838 text = wrealloc(text, where + 1);
2839 text[where++] = '\n';
2842 if (tb->blank)
2843 goto _gSnext;
2845 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2846 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2847 where += tb->s_end - tb->s_begin;
2852 _gSnext: tb = tb->next;
2855 /* +1 for the end of string, let's be nice */
2856 text = wrealloc(text, where + 1);
2857 text[where] = 0;
2858 return text;
2861 static void releaseStreamObjects(void *data)
2863 if (data)
2864 wfree(data);
2867 WMArray *getStreamObjects(WMText * tPtr, int sel)
2869 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2870 WMData *data;
2871 char *stream;
2872 unsigned short len;
2873 char *start, *fa, *desc;
2875 stream = getStream(tPtr, sel, 1);
2876 if (!stream)
2877 return NULL;
2879 start = stream;
2880 while (start) {
2882 fa = strchr(start, 0xFA);
2883 if (fa) {
2884 if ((int)(fa - start) > 0) {
2885 desc = start;
2886 desc[(int)(fa - start)] = 0;
2887 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2888 WMSetDataFormat(data, TYPETEXT);
2889 WMAddToArray(array, (void *)data);
2892 len = *(fa + 1) * 0xff + *(fa + 2);
2893 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2894 WMSetDataFormat(data, *(fa + 3));
2895 WMAddToArray(array, (void *)data);
2896 start = fa + len + 4;
2898 } else {
2899 if (start && strlen(start)) {
2900 data = WMCreateDataWithBytes((void *)start, strlen(start));
2901 WMSetDataFormat(data, TYPETEXT);
2902 WMAddToArray(array, (void *)data);
2904 start = fa;
2908 wfree(stream);
2909 return array;
2912 #define XDND_TEXT_DATA_TYPE "text/plain"
2913 #define XDND_COLOR_DATA_TYPE "application/X-color"
2914 static WMArray *getXdndSourceTypeArray()
2916 WMArray *types = WMCreateArray(1);
2917 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2918 return types;
2921 static WMArray *getXdndDestinationTypeArray()
2923 WMArray *types = WMCreateArray(1);
2924 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2925 return types;
2928 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2930 Text *tPtr;
2931 Display *dpy;
2932 WMScreen *scr;
2933 XGCValues gcv;
2935 tPtr = wmalloc(sizeof(Text));
2936 tPtr->widgetClass = WC_Text;
2937 tPtr->view = W_CreateView(W_VIEW(parent));
2938 if (!tPtr->view) {
2939 perror("could not create text's view\n");
2940 wfree(tPtr);
2941 return NULL;
2944 dpy = tPtr->view->screen->display;
2945 scr = tPtr->view->screen;
2947 tPtr->view->self = tPtr;
2948 tPtr->view->attribs.cursor = scr->textCursor;
2949 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2950 W_ResizeView(tPtr->view, 250, 200);
2952 tPtr->dColor = WMBlackColor(scr);
2953 tPtr->fgColor = WMBlackColor(scr);
2954 tPtr->bgColor = WMWhiteColor(scr);
2955 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2957 gcv.graphics_exposures = False;
2958 gcv.foreground = W_PIXEL(scr->gray);
2959 gcv.background = W_PIXEL(scr->darkGray);
2960 gcv.fill_style = FillStippled;
2961 /* why not use scr->stipple here? */
2962 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2963 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2964 GCForeground | GCBackground | GCStipple
2965 | GCFillStyle | GCGraphicsExposures, &gcv);
2967 tPtr->ruler = NULL;
2968 tPtr->vS = NULL;
2969 tPtr->hS = NULL;
2971 tPtr->dFont = WMSystemFontOfSize(scr, 12);
2973 tPtr->view->delegate = &_TextViewDelegate;
2975 tPtr->delegate = NULL;
2977 #if DO_BLINK
2978 tPtr->timerID = NULL;
2979 #endif
2981 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
2982 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
2984 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
2985 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
2987 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
2989 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
2990 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2993 WMArray *types = WMCreateArray(2);
2994 WMAddToArray(types, "application/X-color");
2995 WMAddToArray(types, "application/X-image");
2996 WMRegisterViewForDraggedTypes(tPtr->view, types);
2999 /*WMAddNotificationObserver(fontChanged, tPtr,
3000 WMFontPanelDidChangeNotification, tPtr); */
3002 tPtr->firstTextBlock = NULL;
3003 tPtr->lastTextBlock = NULL;
3004 tPtr->currentTextBlock = NULL;
3005 tPtr->tpos = 0;
3007 tPtr->gfxItems = WMCreateArray(4);
3009 tPtr->parser = parser;
3010 tPtr->writer = writer;
3012 tPtr->sel.x = tPtr->sel.y = 2;
3013 tPtr->sel.w = tPtr->sel.h = 0;
3015 tPtr->clicked.x = tPtr->clicked.y = 2;
3017 tPtr->visible.x = tPtr->visible.y = 2;
3018 tPtr->visible.h = tPtr->view->size.height;
3019 tPtr->visible.w = tPtr->view->size.width - 4;
3021 tPtr->cursor.x = -23;
3023 tPtr->docWidth = 0;
3024 tPtr->docHeight = 0;
3025 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3026 tPtr->db = (Pixmap) NULL;
3027 tPtr->bgPixmap = NULL;
3029 tPtr->margins = WMGetRulerMargins(NULL);
3030 tPtr->margins->right = tPtr->visible.w;
3031 tPtr->nMargins = 1;
3033 tPtr->flags.rulerShown = False;
3034 tPtr->flags.monoFont = False;
3035 tPtr->flags.focused = False;
3036 tPtr->flags.editable = True;
3037 tPtr->flags.ownsSelection = False;
3038 tPtr->flags.pointerGrabbed = False;
3039 tPtr->flags.extendSelection = False;
3040 tPtr->flags.frozen = False;
3041 tPtr->flags.cursorShown = True;
3042 tPtr->flags.acceptsGraphic = False;
3043 tPtr->flags.horizOnDemand = False;
3044 tPtr->flags.needsLayOut = False;
3045 tPtr->flags.ignoreNewLine = False;
3046 tPtr->flags.indentNewLine = False;
3047 tPtr->flags.laidOut = False;
3048 tPtr->flags.ownsSelection = False;
3049 tPtr->flags.waitingForSelection = False;
3050 tPtr->flags.prepend = False;
3051 tPtr->flags.isOverGraphic = False;
3052 tPtr->flags.relief = WRSunken;
3053 tPtr->flags.isOverGraphic = 0;
3054 tPtr->flags.alignment = WALeft;
3055 tPtr->flags.first = True;
3057 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3058 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3060 return tPtr;
3063 void WMPrependTextStream(WMText * tPtr, char *text)
3065 CHECK_CLASS(tPtr, WC_Text);
3067 if (!text) {
3068 if (tPtr->flags.ownsSelection)
3069 releaseSelection(tPtr);
3070 clearText(tPtr);
3071 updateScrollers(tPtr);
3072 return;
3075 tPtr->flags.prepend = True;
3076 if (text && tPtr->parser)
3077 (tPtr->parser) (tPtr, (void *)text);
3078 else
3079 insertPlainText(tPtr, text);
3081 tPtr->flags.needsLayOut = True;
3082 tPtr->tpos = 0;
3083 if (!tPtr->flags.frozen) {
3084 layOutDocument(tPtr);
3088 void WMAppendTextStream(WMText * tPtr, char *text)
3090 CHECK_CLASS(tPtr, WC_Text);
3092 if (!text) {
3093 if (tPtr->flags.ownsSelection)
3094 releaseSelection(tPtr);
3095 clearText(tPtr);
3096 updateScrollers(tPtr);
3097 return;
3100 tPtr->flags.prepend = False;
3101 if (text && tPtr->parser)
3102 (tPtr->parser) (tPtr, (void *)text);
3103 else
3104 insertPlainText(tPtr, text);
3106 tPtr->flags.needsLayOut = True;
3107 if (tPtr->currentTextBlock) {
3108 if (tPtr->currentTextBlock->graphic)
3109 tPtr->tpos = 1;
3110 else
3111 tPtr->tpos = tPtr->currentTextBlock->used;
3114 if (!tPtr->flags.frozen) {
3115 layOutDocument(tPtr);
3119 char *WMGetTextStream(WMText * tPtr)
3121 CHECK_CLASS(tPtr, WC_Text);
3123 return getStream(tPtr, 0, 0);
3126 char *WMGetTextSelectedStream(WMText * tPtr)
3128 CHECK_CLASS(tPtr, WC_Text);
3130 return getStream(tPtr, 1, 0);
3133 WMArray *WMGetTextObjects(WMText * tPtr)
3135 CHECK_CLASS(tPtr, WC_Text);
3137 return getStreamObjects(tPtr, 0);
3140 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3142 CHECK_CLASS(tPtr, WC_Text);
3144 return getStreamObjects(tPtr, 1);
3147 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3149 CHECK_CLASS(tPtr, WC_Text);
3151 tPtr->delegate = delegate;
3154 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3155 char *description, WMColor * color,
3156 unsigned short first, unsigned short extraInfo)
3158 TextBlock *tb;
3160 if (!w || !description || !color)
3161 return NULL;
3163 tb = wmalloc(sizeof(TextBlock));
3165 tb->text = wstrdup(description);
3166 tb->used = strlen(description);
3167 tb->blank = False;
3168 tb->d.widget = w;
3169 tb->color = WMRetainColor(color);
3170 tb->marginN = newMargin(tPtr, NULL);
3171 tb->allocated = extraInfo;
3172 tb->first = first;
3173 tb->kanji = False;
3174 tb->graphic = True;
3175 tb->object = True;
3176 tb->underlined = False;
3177 tb->selected = False;
3178 tb->script = 0;
3179 tb->sections = NULL;
3180 tb->nsections = 0;
3181 tb->prior = NULL;
3182 tb->next = NULL;
3184 return tb;
3187 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3188 char *description, WMColor * color,
3189 unsigned short first, unsigned short extraInfo)
3191 TextBlock *tb;
3193 if (!p || !description || !color)
3194 return NULL;
3196 tb = wmalloc(sizeof(TextBlock));
3198 tb->text = wstrdup(description);
3199 tb->used = strlen(description);
3200 tb->blank = False;
3201 tb->d.pixmap = WMRetainPixmap(p);
3202 tb->color = WMRetainColor(color);
3203 tb->marginN = newMargin(tPtr, NULL);
3204 tb->allocated = extraInfo;
3205 tb->first = first;
3206 tb->kanji = False;
3207 tb->graphic = True;
3208 tb->object = False;
3209 tb->underlined = False;
3210 tb->selected = False;
3211 tb->script = 0;
3212 tb->sections = NULL;
3213 tb->nsections = 0;
3214 tb->prior = NULL;
3215 tb->next = NULL;
3217 return tb;
3220 void *WMCreateTextBlockWithText(WMText * tPtr, char *text, WMFont * font, WMColor * color,
3221 unsigned short first, unsigned short len)
3223 TextBlock *tb;
3225 if (!font || !color)
3226 return NULL;
3228 tb = wmalloc(sizeof(TextBlock));
3230 tb->allocated = reqBlockSize(len);
3231 tb->text = (char *)wmalloc(tb->allocated);
3233 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3234 *tb->text = ' ';
3235 tb->used = 1;
3236 tb->blank = True;
3237 } else {
3238 memcpy(tb->text, text, len);
3239 tb->used = len;
3240 tb->blank = False;
3242 tb->text[tb->used] = 0;
3244 tb->d.font = WMRetainFont(font);
3245 tb->color = WMRetainColor(color);
3246 tb->marginN = newMargin(tPtr, NULL);
3247 tb->first = first;
3248 tb->kanji = False;
3249 tb->graphic = False;
3250 tb->underlined = False;
3251 tb->selected = False;
3252 tb->script = 0;
3253 tb->sections = NULL;
3254 tb->nsections = 0;
3255 tb->prior = NULL;
3256 tb->next = NULL;
3257 return tb;
3260 void
3261 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3262 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3264 TextBlock *tb = (TextBlock *) vtb;
3265 if (!tb)
3266 return;
3268 tb->first = first;
3269 tb->kanji = kanji;
3270 tb->underlined = underlined;
3271 tb->script = script;
3272 tb->marginN = newMargin(tPtr, margins);
3275 void
3276 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3277 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins * margins)
3279 TextBlock *tb = (TextBlock *) vtb;
3280 if (!tb)
3281 return;
3283 if (first)
3284 *first = tb->first;
3285 if (kanji)
3286 *kanji = tb->kanji;
3287 if (underlined)
3288 *underlined = tb->underlined;
3289 if (script)
3290 *script = tb->script;
3291 if (margins)
3292 margins = &tPtr->margins[tb->marginN];
3295 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3297 TextBlock *tb = (TextBlock *) vtb;
3299 if (!tb)
3300 return;
3302 if (tb->graphic) {
3303 if (tb->object) {
3304 WMWidget *w = tb->d.widget;
3305 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3306 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3307 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3310 WMAddToArray(tPtr->gfxItems, (void *)tb);
3311 tPtr->tpos = 1;
3313 } else {
3314 tPtr->tpos = tb->used;
3317 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3318 tb->next = tb->prior = NULL;
3319 tb->first = True;
3320 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3321 return;
3324 if (!tb->first) {
3325 tb->marginN = tPtr->currentTextBlock->marginN;
3328 tb->next = tPtr->currentTextBlock;
3329 tb->prior = tPtr->currentTextBlock->prior;
3330 if (tPtr->currentTextBlock->prior)
3331 tPtr->currentTextBlock->prior->next = tb;
3333 tPtr->currentTextBlock->prior = tb;
3334 if (!tb->prior)
3335 tPtr->firstTextBlock = tb;
3337 tPtr->currentTextBlock = tb;
3340 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3342 TextBlock *tb = (TextBlock *) vtb;
3344 if (!tb)
3345 return;
3347 if (tb->graphic) {
3348 if (tb->object) {
3349 WMWidget *w = tb->d.widget;
3350 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3351 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3352 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3355 WMAddToArray(tPtr->gfxItems, (void *)tb);
3356 tPtr->tpos = 1;
3358 } else {
3359 tPtr->tpos = tb->used;
3362 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3363 tb->next = tb->prior = NULL;
3364 tb->first = True;
3365 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3366 return;
3369 if (!tb->first) {
3370 tb->marginN = tPtr->currentTextBlock->marginN;
3373 tb->next = tPtr->currentTextBlock->next;
3374 tb->prior = tPtr->currentTextBlock;
3375 if (tPtr->currentTextBlock->next)
3376 tPtr->currentTextBlock->next->prior = tb;
3378 tPtr->currentTextBlock->next = tb;
3380 if (!tb->next)
3381 tPtr->lastTextBlock = tb;
3383 tPtr->currentTextBlock = tb;
3386 void *WMRemoveTextBlock(WMText * tPtr)
3388 TextBlock *tb = NULL;
3390 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3391 return NULL;
3394 tb = tPtr->currentTextBlock;
3395 if (tb->graphic) {
3396 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3398 if (tb->object) {
3399 WMUnmapWidget(tb->d.widget);
3403 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3404 if (tPtr->currentTextBlock->next)
3405 tPtr->currentTextBlock->next->prior = NULL;
3407 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3408 tPtr->currentTextBlock = tPtr->firstTextBlock;
3410 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3411 tPtr->currentTextBlock->prior->next = NULL;
3412 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3413 tPtr->currentTextBlock = tPtr->lastTextBlock;
3414 } else {
3415 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3416 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3417 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3420 return (void *)tb;
3423 #if 0
3424 static void destroyWidget(WMWidget * widget)
3426 WMDestroyWidget(widget);
3427 // -- never do this -- wfree(widget);
3429 #endif
3431 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3433 TextBlock *tb = (TextBlock *) vtb;
3434 if (!tb)
3435 return;
3437 if (tb->graphic) {
3438 if (tb->object) {
3439 /* naturally, there's a danger to destroying widgets whose action
3440 * brings us here: ie. press a button to destroy it...
3441 * need to find a safer way. till then... this stays commented out */
3442 /* 5 months later... destroy it 10 seconds after now which should
3443 * be enough time for the widget's action to be completed... :-) */
3444 /* This is a bad assumption. Just destroy the widget here.
3445 * if the caller needs it, it can protect it with W_RetainView()
3446 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3447 WMDestroyWidget(tb->d.widget);
3448 } else {
3449 WMReleasePixmap(tb->d.pixmap);
3451 } else {
3452 WMReleaseFont(tb->d.font);
3455 WMReleaseColor(tb->color);
3456 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3457 if (tb->sections)
3458 wfree(tb->sections);
3459 wfree(tb->text);
3460 wfree(tb);
3463 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3465 if (tPtr->fgColor)
3466 WMReleaseColor(tPtr->fgColor);
3468 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3470 paintText(tPtr);
3473 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3475 if (tPtr->bgColor)
3476 WMReleaseColor(tPtr->bgColor);
3478 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3479 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3481 paintText(tPtr);
3484 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3486 if (tPtr->bgPixmap)
3487 WMReleasePixmap(tPtr->bgPixmap);
3489 if (pixmap)
3490 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3491 else
3492 tPtr->bgPixmap = NULL;
3495 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3497 tPtr->flags.relief = relief;
3498 textDidResize(tPtr->view->delegate, tPtr->view);
3501 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3503 if (shouldhave && !tPtr->hS) {
3504 tPtr->hS = WMCreateScroller(tPtr);
3505 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3506 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3507 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3508 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3509 WMMapWidget(tPtr->hS);
3510 } else if (!shouldhave && tPtr->hS) {
3511 WMUnmapWidget(tPtr->hS);
3512 WMDestroyWidget(tPtr->hS);
3513 tPtr->hS = NULL;
3516 tPtr->hpos = 0;
3517 tPtr->prevHpos = 0;
3518 textDidResize(tPtr->view->delegate, tPtr->view);
3521 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3523 if (shouldhave && !tPtr->ruler) {
3524 tPtr->ruler = WMCreateRuler(tPtr);
3525 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3526 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3527 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3528 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3529 } else if (!shouldhave && tPtr->ruler) {
3530 WMShowTextRuler(tPtr, False);
3531 WMDestroyWidget(tPtr->ruler);
3532 tPtr->ruler = NULL;
3534 textDidResize(tPtr->view->delegate, tPtr->view);
3537 void WMShowTextRuler(WMText * tPtr, Bool show)
3539 if (!tPtr->ruler)
3540 return;
3542 if (tPtr->flags.monoFont)
3543 show = False;
3545 tPtr->flags.rulerShown = show;
3546 if (show) {
3547 WMMapWidget(tPtr->ruler);
3548 } else {
3549 WMUnmapWidget(tPtr->ruler);
3552 textDidResize(tPtr->view->delegate, tPtr->view);
3555 Bool WMGetTextRulerShown(WMText * tPtr)
3557 if (!tPtr->ruler)
3558 return False;
3560 return tPtr->flags.rulerShown;
3563 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3565 if (shouldhave && !tPtr->vS) {
3566 tPtr->vS = WMCreateScroller(tPtr);
3567 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3568 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3569 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3570 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3571 WMMapWidget(tPtr->vS);
3572 } else if (!shouldhave && tPtr->vS) {
3573 WMUnmapWidget(tPtr->vS);
3574 WMDestroyWidget(tPtr->vS);
3575 tPtr->vS = NULL;
3578 tPtr->vpos = 0;
3579 tPtr->prevVpos = 0;
3580 textDidResize(tPtr->view->delegate, tPtr->view);
3583 Bool WMScrollText(WMText * tPtr, int amount)
3585 Bool scroll = False;
3587 if (amount == 0 || !tPtr->view->flags.realized)
3588 return False;
3590 if (amount < 0) {
3591 if (tPtr->vpos > 0) {
3592 if (tPtr->vpos > abs(amount))
3593 tPtr->vpos += amount;
3594 else
3595 tPtr->vpos = 0;
3596 scroll = True;
3598 } else {
3599 int limit = tPtr->docHeight - tPtr->visible.h;
3600 if (tPtr->vpos < limit) {
3601 if (tPtr->vpos < limit - amount)
3602 tPtr->vpos += amount;
3603 else
3604 tPtr->vpos = limit;
3605 scroll = True;
3609 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3610 updateScrollers(tPtr);
3611 paintText(tPtr);
3613 tPtr->prevVpos = tPtr->vpos;
3614 return scroll;
3617 Bool WMPageText(WMText * tPtr, Bool direction)
3619 if (!tPtr->view->flags.realized)
3620 return False;
3622 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3625 void WMSetTextEditable(WMText * tPtr, Bool editable)
3627 tPtr->flags.editable = editable;
3630 int WMGetTextEditable(WMText * tPtr)
3632 return tPtr->flags.editable;
3635 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3637 tPtr->flags.indentNewLine = indent;
3640 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3642 tPtr->flags.ignoreNewLine = ignore;
3645 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3647 return tPtr->flags.ignoreNewLine;
3650 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3652 if (mono) {
3653 if (tPtr->flags.rulerShown)
3654 WMShowTextRuler(tPtr, False);
3655 if (tPtr->flags.alignment != WALeft)
3656 tPtr->flags.alignment = WALeft;
3659 tPtr->flags.monoFont = mono;
3660 textDidResize(tPtr->view->delegate, tPtr->view);
3663 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3665 return tPtr->flags.monoFont;
3668 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3670 if (tPtr->dFont)
3671 WMReleaseFont(tPtr->dFont);
3673 if (font) {
3674 tPtr->dFont = WMRetainFont(font);
3675 } else {
3676 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3680 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3682 return WMRetainFont(tPtr->dFont);
3685 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3687 if (tPtr->dColor)
3688 WMReleaseColor(tPtr->dColor);
3690 if (color) {
3691 tPtr->dColor = WMRetainColor(color);
3692 } else {
3693 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3697 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3699 return tPtr->dColor;
3702 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3704 if (tPtr->flags.monoFont)
3705 tPtr->flags.alignment = WALeft;
3706 else
3707 tPtr->flags.alignment = alignment;
3708 WMThawText(tPtr);
3711 int WMGetTextInsertType(WMText * tPtr)
3713 return tPtr->flags.prepend;
3716 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3718 setSelectionProperty(tPtr, NULL, color, -1);
3721 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3723 TextBlock *tb;
3725 tb = tPtr->currentTextBlock;
3727 if (!tb || !tPtr->flags.ownsSelection)
3728 return NULL;
3730 if (!tb->selected)
3731 return NULL;
3733 return tb->color;
3736 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3738 setSelectionProperty(tPtr, font, NULL, -1);
3741 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3743 TextBlock *tb;
3745 tb = tPtr->currentTextBlock;
3747 if (!tb || !tPtr->flags.ownsSelection)
3748 return NULL;
3750 if (!tb->selected)
3751 return NULL;
3753 if (tb->graphic) {
3754 tb = getFirstNonGraphicBlockFor(tb, 1);
3755 if (!tb)
3756 return NULL;
3758 return (tb->selected ? tb->d.font : NULL);
3761 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3763 /* // check this */
3764 if (underlined != 0 && underlined != 1)
3765 return;
3767 setSelectionProperty(tPtr, NULL, NULL, underlined);
3770 int WMGetTextSelectionUnderlined(WMText * tPtr)
3772 TextBlock *tb;
3774 tb = tPtr->currentTextBlock;
3776 if (!tb || !tPtr->flags.ownsSelection)
3777 return 0;
3779 if (!tb->selected)
3780 return 0;
3782 return tb->underlined;
3785 void WMFreezeText(WMText * tPtr)
3787 tPtr->flags.frozen = True;
3790 void WMThawText(WMText * tPtr)
3792 tPtr->flags.frozen = False;
3794 if (tPtr->flags.monoFont) {
3795 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3796 TextBlock *tb;
3798 /* make sure to unmap widgets no matter where they are */
3799 /* they'll be later remapped if needed by paintText */
3800 for (j = 0; j < c; j++) {
3801 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3802 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3803 WMUnmapWidget(tb->d.widget);
3808 tPtr->flags.laidOut = False;
3809 layOutDocument(tPtr);
3810 updateScrollers(tPtr);
3811 paintText(tPtr);
3812 tPtr->flags.needsLayOut = False;
3816 /* find first occurence of a string */
3817 static char *mystrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3819 char *ptr;
3821 if (!haystack || !needle || !end)
3822 return NULL;
3824 for (ptr = haystack; ptr < end; ptr++) {
3825 if (caseSensitive) {
3826 if (*ptr == *needle && !strncmp(ptr, needle, len))
3827 return ptr;
3829 } else {
3830 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3831 return ptr;
3835 return NULL;
3838 /* find last occurence of a string */
3839 static char *mystrrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3841 char *ptr;
3843 if (!haystack || !needle || !end)
3844 return NULL;
3846 for (ptr = haystack - 2; ptr > end; ptr--) {
3847 if (caseSensitive) {
3848 if (*ptr == *needle && !strncmp(ptr, needle, len))
3849 return ptr;
3850 } else {
3851 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3852 return ptr;
3856 return NULL;
3859 Bool WMFindInTextStream(WMText * tPtr, char *needle, Bool direction, Bool caseSensitive)
3861 TextBlock *tb;
3862 char *mark = NULL;
3863 unsigned short pos;
3865 #if 0
3866 if (!(tb = tPtr->currentTextBlock)) {
3867 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3868 return False;
3870 } else {
3871 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3872 tb = (direction>0) ? tb->next : tb->prior; */
3873 if (tb != tPtr->lastTextBlock)
3874 tb = tb->prior;
3876 #endif
3877 tb = tPtr->currentTextBlock;
3878 pos = tPtr->tpos;
3880 while (tb) {
3881 if (!tb->graphic) {
3883 if (direction > 0) {
3884 if (pos + 1 < tb->used)
3885 pos++;
3887 if (tb->used - pos > 0 && pos > 0) {
3888 mark = mystrstr(&tb->text[pos], needle,
3889 strlen(needle), &tb->text[tb->used], caseSensitive);
3891 } else {
3892 tb = tb->next;
3893 pos = 0;
3894 continue;
3897 } else {
3898 if (pos - 1 > 0)
3899 pos--;
3901 if (pos > 0) {
3902 mark = mystrrstr(&tb->text[pos], needle,
3903 strlen(needle), tb->text, caseSensitive);
3904 } else {
3905 tb = tb->prior;
3906 if (!tb)
3907 return False;
3908 pos = tb->used;
3909 continue;
3913 if (mark) {
3914 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3916 tPtr->tpos = (int)(mark - tb->text);
3917 tPtr->currentTextBlock = tb;
3918 updateCursorPosition(tPtr);
3919 tPtr->sel.y = tPtr->cursor.y + 5;
3920 tPtr->sel.h = tPtr->cursor.h - 10;
3921 tPtr->sel.x = tPtr->cursor.x + 1;
3922 tPtr->sel.w = WMIN(WMWidthOfString(font,
3923 &tb->text[tPtr->tpos], strlen(needle)),
3924 tPtr->docWidth - tPtr->sel.x);
3925 tPtr->flags.ownsSelection = True;
3926 paintText(tPtr);
3928 return True;
3932 tb = (direction > 0) ? tb->next : tb->prior;
3933 if (tb) {
3934 pos = (direction > 0) ? 0 : tb->used;
3938 return False;
3941 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3943 if (!tPtr->flags.ownsSelection)
3944 return False;
3946 removeSelection(tPtr);
3948 if (replacement) {
3949 insertTextInteractively(tPtr, replacement, strlen(replacement));
3950 updateCursorPosition(tPtr);
3951 paintText(tPtr);
3954 return True;