Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wtext.c
blobf032ca6dfe8ca9d2815e4ad179b9eee91c371aa2
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
4 #include "WINGsP.h"
5 #include <ctype.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 #define DO_BLINK 0
11 /* TODO:
12 * - verify what happens with XK_return in insertTextInt...
13 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
24 /* a Section is a section of a TextBlock that describes what parts
25 of a TextBlock has been laid out on which "line"...
26 o this greatly aids redraw, scroll and selection.
27 o this is created during layoutLine, but may be later modified.
28 o there may be many Sections per TextBlock, hence the array */
29 typedef struct {
30 unsigned int x, y; /* where to draw it from */
31 unsigned short w, h; /* its width and height */
32 unsigned short begin; /* where the layout begins */
33 unsigned short end; /* where it ends */
34 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
35 unsigned short last:1; /* is it the last section on a "line"? */
36 unsigned int _y:31; /* the "line" it and other textblocks are on */
37 } Section;
39 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
40 o text for the block, color and font
41 o or a pointer to the pixmap
42 o OR a pointer to the widget and the (text) description for its graphic
45 typedef struct _TextBlock {
46 struct _TextBlock *next; /* next text block in linked list */
47 struct _TextBlock *prior; /* prior text block in linked list */
49 char *text; /* pointer to text (could be kanji) */
50 /* or to the object's description */
51 union {
52 WMFont *font; /* the font */
53 WMWidget *widget; /* the embedded widget */
54 WMPixmap *pixmap; /* the pixmap */
55 } d; /* description */
57 unsigned short used; /* number of chars in this block */
58 unsigned short allocated; /* size of allocation (in chars) */
59 WMColor *color; /* the color */
61 Section *sections; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
64 unsigned short s_begin; /* where the selection begins */
65 unsigned short s_end; /* where it ends */
67 unsigned int first:1; /* first TextBlock in paragraph */
68 unsigned int blank:1; /* ie. blank paragraph */
69 unsigned int kanji:1; /* is of 16-bit characters or not */
70 unsigned int graphic:1; /* graphic or text: text=0 */
71 unsigned int object:1; /* embedded object or pixmap */
72 unsigned int underlined:1; /* underlined or not */
73 unsigned int selected:1; /* selected or not */
74 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
75 int script:8; /* script in points: negative for subscript */
76 unsigned int marginN:8; /* which of the margins in the tPtr to use */
77 unsigned int nClicks:2; /* single, double, triple clicks */
78 unsigned int RESERVED:7;
79 } TextBlock;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
82 typedef struct {
83 int y, x, h, w;
84 } myRect;
86 typedef struct W_Text {
87 W_Class widgetClass; /* the class number of this widget */
88 W_View *view; /* the view referring to this instance */
90 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
92 WMScroller *vS; /* the vertical scroller */
93 unsigned int vpos; /* the current vertical position */
94 unsigned int prevVpos; /* the previous vertical position */
96 WMScroller *hS; /* the horizontal scroller */
97 unsigned int hpos; /* the current horizontal position */
98 unsigned int prevHpos; /* the previous horizontal position */
100 WMFont *dFont; /* the default font */
101 WMColor *dColor; /* the default color */
102 WMPixmap *dBulletPix; /* the default pixmap for bullets */
104 WMColor *fgColor; /* The current foreground color */
105 WMColor *bgColor; /* The background color */
107 GC stippledGC; /* the GC to overlay selected graphics with */
108 Pixmap db; /* the buffer on which to draw */
109 WMPixmap *bgPixmap; /* the background pixmap */
111 myRect visible; /* the actual rectangle that can be drawn into */
112 myRect cursor; /* the position and (height) of cursor */
113 myRect sel; /* the selection rectangle */
115 WMPoint clicked; /* where in the _document_ was clicked */
117 unsigned short tpos; /* the position in the currentTextBlock */
118 unsigned short docWidth; /* the width of the entire document */
119 unsigned int docHeight; /* the height of the entire document */
121 TextBlock *firstTextBlock;
122 TextBlock *lastTextBlock;
123 TextBlock *currentTextBlock;
125 WMArray *gfxItems; /* a nice array of graphic items */
127 #if DO_BLINK
128 WMHandlerID timerID; /* for nice twinky-winky */
129 #endif
131 WMAction *parser;
132 WMAction *writer;
133 WMTextDelegate *delegate;
134 Time lastClickTime;
136 WMRulerMargins *margins; /* an array of margins */
138 unsigned int nMargins:7; /* the total number of margins in use */
139 struct {
140 unsigned int monoFont:1; /* whether to ignore formats and graphic */
141 unsigned int focused:1; /* whether this instance has input focus */
142 unsigned int editable:1; /* "silly user, you can't edit me" */
143 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
144 unsigned int pointerGrabbed:1; /* "heh, gib me pointer" */
145 unsigned int extendSelection:1; /* shift-drag to select more regions */
147 unsigned int rulerShown:1; /* whether the ruler is shown or not */
148 unsigned int frozen:1; /* whether screen updates are to be made */
149 unsigned int cursorShown:1; /* whether to show the cursor */
150 unsigned int acceptsGraphic:1; /* accept graphic when dropped */
151 unsigned int horizOnDemand:1; /* if a large image should appear */
152 unsigned int needsLayOut:1; /* in case of Append/Deletes */
153 unsigned int ignoreNewLine:1; /* turn it into a ' ' in streams > 1 */
154 unsigned int indentNewLine:1; /* add " " for a newline typed */
155 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
156 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
157 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
158 WMAlignment alignment:2; /* the alignment for text */
159 WMReliefType relief:3; /* the relief to display with */
160 unsigned int isOverGraphic:2; /* the mouse is over a graphic */
161 unsigned int first:1; /* for plain text parsing, newline? */
162 /* unsigned int RESERVED:1; */
163 } flags;
165 WMArray *xdndSourceTypes;
166 WMArray *xdndDestinationTypes;
167 } Text;
169 #define NOTIFY(T,C,N,A) {\
170 WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
176 #define TYPETEXT 0
178 #if 0
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr, int len)
182 char *s;
184 s = wmalloc(len + 1);
185 memcpy(s, ptr, len);
186 s[len] = 0;
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
188 printf("[%s]\n", s);
189 wfree(s);
191 #endif
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
204 static char *default_bullet[] = {
205 "6 6 4 1",
206 " c None s None",
207 ". c black",
208 "X c white",
209 "o c #808080",
210 " ... ",
211 ".XX.. ",
212 ".XX..o",
213 ".....o",
214 " ...oo",
215 " ooo "
218 static void handleEvents(XEvent * event, void *data);
219 static void layOutDocument(Text * tPtr);
220 static void updateScrollers(Text * tPtr);
222 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
224 unsigned int i = 0;
226 for (i = 0; i < tPtr->nMargins; i++) {
228 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
229 return i;
232 return -1;
235 static int newMargin(Text * tPtr, WMRulerMargins * margins)
237 int n;
239 if (!margins) {
240 tPtr->margins[0].retainCount++;
241 return 0;
244 n = getMarginNumber(tPtr, margins);
246 if (n == -1) {
248 if (tPtr->nMargins >= 127) {
249 n = tPtr->nMargins - 1;
250 return n;
253 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
255 n = tPtr->nMargins - 1;
256 tPtr->margins[n].left = margins->left;
257 tPtr->margins[n].first = margins->first;
258 tPtr->margins[n].body = margins->body;
259 tPtr->margins[n].right = margins->right;
260 /* for each tab... */
261 tPtr->margins[n].retainCount = 1;
262 } else {
263 tPtr->margins[n].retainCount++;
266 return n;
269 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
271 unsigned short i, w, lw, selected = False, extend = False;
272 myRect sel;
274 /* if selection rectangle completely encloses the section */
275 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
276 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
277 sel.x = 0;
278 sel.w = tPtr->visible.w;
279 selected = extend = True;
281 /* or if it starts on a line and then goes further down */
282 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
283 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
284 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y)) {
285 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
286 sel.w = tPtr->visible.w;
287 selected = extend = True;
289 /* or if it begins before a line, but ends on it */
290 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
291 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
292 && (tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
294 if (1 || tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
295 sel.w = tPtr->sel.x + tPtr->sel.w;
296 else
297 sel.w = tPtr->sel.x;
299 sel.x = 0;
300 selected = True;
302 /* or if the selection rectangle lies entirely within a line */
303 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
304 && (tPtr->sel.w >= 2)
305 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
306 sel.x = tPtr->sel.x;
307 sel.w = tPtr->sel.w;
308 selected = True;
311 if (selected) {
312 selected = False;
314 /* if not within (modified) selection rectangle */
315 if (tb->sections[s].x > sel.x + sel.w || tb->sections[s].x + tb->sections[s].w < sel.x)
316 return False;
318 if (tb->graphic) {
319 if (tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w && tb->sections[s].x >= sel.x) {
320 rect->width = tb->sections[s].w;
321 rect->x = tb->sections[s].x;
322 selected = True;
324 } else {
326 i = tb->sections[s].begin;
327 lw = 0;
329 if (0 && tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
334 while (++i <= tb->sections[s].end) {
336 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
337 lw += w;
339 if (lw + tb->sections[s].x >= sel.x || i == tb->sections[s].end) {
340 lw -= w;
341 i--;
342 tb->s_begin = (tb->selected ? WMIN(tb->s_begin, i) : i);
343 break;
347 if (i > tb->sections[s].end) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
349 return False;
352 _selEnd: rect->x = tb->sections[s].x + lw;
353 lw = 0;
354 while (++i <= tb->sections[s].end) {
356 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
357 lw += w;
359 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
366 rect->width = lw;
367 if (tb->sections[s].last && sel.x + sel.w
368 >= tb->sections[s].x + tb->sections[s].w && extend) {
369 rect->width += (tPtr->visible.w - rect->x - lw);
372 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
373 selected = True;
374 break;
380 if (selected) {
381 rect->y = tb->sections[s]._y - tPtr->vpos;
382 rect->height = tb->sections[s].h;
383 if (tb->graphic) {
384 printf("DEBUG: graphic s%d h%d\n", s, tb->sections[s].h);
387 return selected;
391 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
393 TextBlock *tb;
394 int isFont = False;
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
400 if (font && (!color || underlined == -1))
401 isFont = True;
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
409 if (isFont) {
410 if (!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
414 } else if (underlined != -1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
423 TextBlock *midtb, *otb = tb;
425 if (underlined != -1) {
426 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
427 &(tb->text[tb->s_begin]),
428 tb->d.font, tb->color,
429 False,
430 (tb->s_end - tb->s_begin));
431 } else {
432 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
433 &(tb->text[tb->s_begin]),
434 (isFont ? font : tb->d.
435 font),
436 (isFont ? tb->
437 color : color), False,
438 (tb->s_end - tb->s_begin));
441 if (midtb) {
442 if (underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
448 midtb->selected = !True;
449 midtb->s_begin = 0;
450 midtb->s_end = midtb->used;
451 tPtr->currentTextBlock = tb;
452 WMAppendTextBlock(tPtr, midtb);
453 tb = tPtr->currentTextBlock;
456 if (otb->used - otb->s_end > 0) {
457 TextBlock *ntb;
458 ntb = (TextBlock *)
459 WMCreateTextBlockWithText(tPtr,
460 &(otb->text[otb->s_end]), otb->d.font,
461 otb->color, False, otb->used - otb->s_end);
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
475 otb->selected = False;
476 otb->used = otb->s_begin;
480 tb = tb->next;
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
486 /* in case the size changed... */
487 if (isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
490 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
491 tPtr->sel.y = 3 + tb->sections[0]._y;
492 tPtr->sel.h = tb->sections[tb->nsections - 1]._y - tb->sections[0]._y;
493 tPtr->sel.w = tb->sections[tb->nsections - 1].w;
494 if (tb->sections[tb->nsections - 1]._y != tb->sections[0]._y) {
495 tPtr->sel.x = 0;
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
502 static Bool removeSelection(Text * tPtr)
504 TextBlock *tb = NULL;
505 Bool first = False;
507 if (!(tb = tPtr->firstTextBlock))
508 return False;
510 while (tb) {
511 if (tb->selected) {
512 if (!first && !tb->graphic) {
513 WMReleaseFont(tPtr->dFont);
514 tPtr->dFont = WMRetainFont(tb->d.font);
515 first = True;
518 if ((tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
519 tPtr->currentTextBlock = tb;
520 if (tb->next) {
521 tPtr->tpos = 0;
522 } else if (tb->prior) {
523 if (tb->prior->graphic)
524 tPtr->tpos = 1;
525 else
526 tPtr->tpos = tb->prior->used;
527 } else
528 tPtr->tpos = 0;
530 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
531 tb = tPtr->currentTextBlock;
532 continue;
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]), &(tb->text[tb->s_end]), tb->used - tb->s_end);
536 tb->used -= (tb->s_end - tb->s_begin);
537 tb->selected = False;
538 tPtr->tpos = tb->s_begin;
543 tb = tb->next;
545 return True;
548 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
550 TextBlock *hold = tb;
552 if (!tb)
553 return NULL;
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir ? tb->next : tb->prior);
561 if (!tb) {
562 tb = hold;
563 while (tb) {
564 if (!tb->graphic)
565 break;
566 tb = (dir ? tb->prior : tb->next);
570 if (!tb)
571 return NULL;
573 return tb;
576 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
578 if (tPtr->flags.monoFont && tb->graphic) {
579 tb = getFirstNonGraphicBlockFor(tb, *dir);
580 if (!tb)
581 return 0;
583 if (tb->graphic) {
584 tPtr->currentTextBlock = (dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
590 if (!tb->sections) {
591 layOutDocument(tPtr);
592 return 0;
595 *dir = !(y <= tb->sections[0].y);
596 if (*dir) {
597 if ((y <= tb->sections[0]._y + tb->sections[0].h)
598 && (y >= tb->sections[0]._y)) {
599 /* if it's on the same line */
600 if (x < tb->sections[0].x)
601 *dir = 0;
603 } else {
604 if ((y <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
605 && (y >= tb->sections[tb->nsections - 1]._y)) {
606 /* if it's on the same line */
607 if (x > tb->sections[tb->nsections - 1].x)
608 *dir = 1;
612 return 1;
615 static void paintText(Text * tPtr)
617 TextBlock *tb;
618 WMFont *font;
619 char *text;
620 int len, y, c, s, done = False, prev_y = -23, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
631 if (tPtr->bgPixmap) {
632 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
633 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
634 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
643 done = False;
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
649 goto _copy_area;
651 while (tb) {
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
656 if (dir) {
657 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
658 break;
659 } else {
660 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
661 break;
664 _getSibling:
665 if (dir) {
666 if (tb->next)
667 tb = tb->next;
668 else
669 break;
670 } else {
671 if (tb->prior)
672 tb = tb->prior;
673 else
674 break;
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for (s = 0; s < tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
708 if (sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
711 rect.x, rect.y, rect.width, rect.height);
715 prev_y = tb->sections[s]._y;
717 len = tb->sections[s].end - tb->sections[s].begin;
718 text = &(tb->text[tb->sections[s].begin]);
719 y = tb->sections[s].y - tPtr->vpos;
720 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
722 if (!tPtr->flags.monoFont && tb->underlined) {
723 XDrawLine(dpy, tPtr->db, WMColorGC(color),
724 tb->sections[s].x - tPtr->hpos,
725 y + font->y + 1,
726 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
729 tb = (!done ? tb->next : NULL);
732 /* now , show all graphic items that can be viewed */
733 c = WMGetArrayItemCount(tPtr->gfxItems);
734 if (c > 0 && !tPtr->flags.monoFont) {
735 int j, h;
737 for (j = 0; j < c; j++) {
738 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
740 /* if it's not viewable, and mapped, unmap it */
741 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
742 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
744 if (tb->object) {
745 if ((W_VIEW(tb->d.widget))->flags.mapped) {
746 WMUnmapWidget(tb->d.widget);
749 } else {
750 /* if it's viewable, and not mapped, map it */
751 if (tb->object) {
752 W_View *view = W_VIEW(tb->d.widget);
754 if (!view->flags.realized)
755 WMRealizeWidget(tb->d.widget);
756 if (!view->flags.mapped) {
757 XMapWindow(view->screen->display, view->window);
758 XFlush(view->screen->display);
759 view->flags.mapped = 1;
763 if (tb->object) {
764 WMMoveWidget(tb->d.widget,
765 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
766 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
767 h = WMWidgetHeight(tb->d.widget) + 1;
769 } else {
770 WMDrawPixmap(tb->d.pixmap, tPtr->db,
771 tb->sections[0].x - tPtr->hpos,
772 tb->sections[0].y - tPtr->vpos);
773 h = tb->d.pixmap->height + 1;
777 if (tPtr->flags.ownsSelection) {
778 XRectangle rect;
780 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
781 Drawable d = (0 && tb->object ?
782 (WMWidgetView(tb->d.widget))->window : tPtr->db);
784 tb->selected = True;
785 XFillRectangle(dpy, d, tPtr->stippledGC,
786 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
787 rect.x, rect.y, rect.width, rect.height);
791 if (!tPtr->flags.monoFont && tb->underlined) {
792 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
793 tb->sections[0].x - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos,
795 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
796 tb->sections[0].y + h - tPtr->vpos);
802 _copy_area:
803 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
804 int y = tPtr->cursor.y - tPtr->vpos;
805 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
806 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
809 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
810 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
812 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
814 if (tPtr->ruler && tPtr->flags.rulerShown)
815 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
819 static void mouseOverObject(Text * tPtr, int x, int y)
821 TextBlock *tb;
822 Bool result = False;
824 x -= tPtr->visible.x;
825 x += tPtr->hpos;
826 y -= tPtr->visible.y;
827 y += tPtr->vpos;
829 if (tPtr->flags.ownsSelection) {
830 if (tPtr->sel.x <= x
831 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
832 tPtr->flags.isOverGraphic = 1;
833 result = True;
837 if (!result) {
838 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
840 if (c < 1)
841 tPtr->flags.isOverGraphic = 0;
843 for (j = 0; j < c; j++) {
844 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
846 if (!tb || !tb->sections) {
847 tPtr->flags.isOverGraphic = 0;
848 return;
851 if (!tb->object) {
852 if (tb->sections[0].x <= x
853 && tb->sections[0].y <= y
854 && tb->sections[0].x + tb->sections[0].w >= x
855 && tb->sections[0].y + tb->d.pixmap->height >= y) {
856 tPtr->flags.isOverGraphic = 3;
857 result = True;
858 break;
865 if (!result)
866 tPtr->flags.isOverGraphic = 0;
868 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
870 XSetWindowAttributes attribs;
871 attribs.cursor = tPtr->view->attribs.cursor;
872 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
876 #if DO_BLINK
878 static void blinkCursor(void *data)
880 Text *tPtr = (Text *) data;
882 if (tPtr->flags.cursorShown) {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
884 } else {
885 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
887 paintText(tPtr);
888 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
890 #endif
892 static void updateCursorPosition(Text * tPtr)
894 TextBlock *tb = NULL;
895 int x, y, h, s;
897 if (tPtr->flags.needsLayOut)
898 layOutDocument(tPtr);
900 if (!(tb = tPtr->currentTextBlock)) {
901 if (!(tb = tPtr->firstTextBlock)) {
902 WMFont *font = tPtr->dFont;
903 tPtr->tpos = 0;
904 tPtr->cursor.h = font->height + abs(font->height - font->y);
906 tPtr->cursor.y = 2;
907 tPtr->cursor.x = 2;
908 return;
912 if (tb->blank) {
913 tPtr->tpos = 0;
914 y = tb->sections[0].y;
915 h = tb->sections[0].h;
916 x = tb->sections[0].x;
918 } else if (tb->graphic) {
919 y = tb->sections[0].y;
920 h = tb->sections[0].h;
921 x = tb->sections[0].x;
922 if (tPtr->tpos == 1)
923 x += tb->sections[0].w;
925 } else {
926 if (tPtr->tpos > tb->used)
927 tPtr->tpos = tb->used;
929 for (s = 0; s < tb->nsections - 1; s++) {
931 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
932 break;
935 y = tb->sections[s]._y;
936 h = tb->sections[s].h;
937 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
938 &tb->text[tb->sections[s].begin],
939 tPtr->tpos - tb->sections[s].begin);
942 tPtr->cursor.y = y;
943 tPtr->cursor.h = h;
944 tPtr->cursor.x = x;
946 /* scroll the bars if the cursor is not visible */
947 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
948 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
949 tPtr->vpos +=
950 (tPtr->cursor.y + tPtr->cursor.h + 10
951 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
952 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
953 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
958 updateScrollers(tPtr);
961 static void cursorToTextPosition(Text * tPtr, int x, int y)
963 TextBlock *tb = NULL;
964 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
965 char *text;
967 if (tPtr->flags.needsLayOut)
968 layOutDocument(tPtr);
970 y += (tPtr->vpos - tPtr->visible.y);
971 if (y < 0)
972 y = 0;
974 x -= (tPtr->visible.x - 2);
975 if (x < 0)
976 x = 0;
978 /* clicked is relative to document, not window... */
979 tPtr->clicked.x = x;
980 tPtr->clicked.y = y;
982 if (!(tb = tPtr->currentTextBlock)) {
983 if (!(tb = tPtr->firstTextBlock)) {
984 WMFont *font = tPtr->dFont;
985 tPtr->tpos = 0;
986 tPtr->cursor.h = font->height + abs(font->height - font->y);
987 tPtr->cursor.y = 2;
988 tPtr->cursor.x = 2;
989 return;
993 /* first, which direction? Most likely, newly clicked
994 position will be close to previous */
995 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
996 return;
998 s = (dir ? 0 : tb->nsections - 1);
999 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
1000 goto _doneV;
1003 /* get the first (or last) section of the TextBlock that
1004 lies about the vertical click point */
1005 done = False;
1006 while (!done && tb) {
1008 if (tPtr->flags.monoFont && tb->graphic) {
1009 if ((dir ? tb->next : tb->prior))
1010 tb = (dir ? tb->next : tb->prior);
1011 continue;
1014 s = (dir ? 0 : tb->nsections - 1);
1015 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1017 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1018 done = True;
1019 } else {
1020 dir ? s++ : s--;
1024 if (!done) {
1025 if ((dir ? tb->next : tb->prior)) {
1026 tb = (dir ? tb->next : tb->prior);
1027 } else {
1028 pos = tb->used;
1029 break; /* goto _doneH; */
1034 if (s < 0 || s >= tb->nsections) {
1035 s = (dir ? tb->nsections - 1 : 0);
1038 _doneV:
1039 /* we have the line, which TextBlock on that line is it? */
1040 pos = (dir ? 0 : tb->sections[s].begin);
1041 if (tPtr->flags.monoFont && tb->graphic) {
1042 TextBlock *hold = tb;
1043 tb = getFirstNonGraphicBlockFor(hold, dir);
1045 if (!tb) {
1046 tPtr->tpos = 0;
1047 tb = hold;
1048 s = 0;
1049 goto _doNothing;
1053 if (tb->blank)
1054 _w = 0;
1056 _y = tb->sections[s]._y;
1058 while (tb) {
1060 if (tPtr->flags.monoFont && tb->graphic) {
1061 tb = (dir ? tb->next : tb->prior);
1062 continue;
1065 if (dir) {
1066 if (tb->graphic) {
1067 if (tb->object)
1068 _w = WMWidgetWidth(tb->d.widget) - 5;
1069 else
1070 _w = tb->d.pixmap->width - 5;
1072 if (tb->sections[0].x + _w >= x)
1073 break;
1074 } else {
1075 text = &(tb->text[tb->sections[s].begin]);
1076 len = tb->sections[s].end - tb->sections[s].begin;
1077 _w = WMWidthOfString(tb->d.font, text, len);
1078 if (tb->sections[s].x + _w >= x)
1079 break;
1082 } else {
1083 if (tb->sections[s].x <= x)
1084 break;
1087 if ((dir ? tb->next : tb->prior)) {
1088 TextBlock *nxt = (dir ? tb->next : tb->prior);
1089 if (tPtr->flags.monoFont && nxt->graphic) {
1090 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1091 if (!nxt) {
1092 pos = (dir ? 0 : tb->sections[s].begin);
1093 tPtr->cursor.x = tb->sections[s].x;
1094 goto _doneH;
1098 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1099 /* this must be the last/first on this line. stop */
1100 pos = (dir ? tb->sections[s].end : 0);
1101 tPtr->cursor.x = tb->sections[s].x;
1102 if (!tb->blank) {
1103 if (tb->graphic) {
1104 if (tb->object)
1105 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1106 else
1107 tPtr->cursor.x += tb->d.pixmap->width;
1108 } else if (pos > tb->sections[s].begin) {
1109 tPtr->cursor.x +=
1110 WMWidthOfString(tb->d.font,
1111 &(tb->text[tb->sections[s].begin]),
1112 pos - tb->sections[s].begin);
1115 goto _doneH;
1119 if ((dir ? tb->next : tb->prior)) {
1120 tb = (dir ? tb->next : tb->prior);
1121 } else {
1122 done = True;
1123 break;
1126 if (tb)
1127 s = (dir ? 0 : tb->nsections - 1);
1130 /* we have said TextBlock, now where within it? */
1131 if (tb) {
1132 if (tb->graphic) {
1133 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1135 tPtr->cursor.x = tb->sections[0].x;
1137 if (x > tPtr->cursor.x + gw / 2) {
1138 pos = 1;
1139 tPtr->cursor.x += gw;
1140 } else {
1141 printf("first %d\n", tb->first);
1142 if (tb->prior) {
1143 if (tb->prior->graphic)
1144 pos = 1;
1145 else
1146 pos = tb->prior->used;
1147 tb = tb->prior;
1148 } else
1149 pos = 0;
1153 s = 0;
1154 goto _doneH;
1156 } else {
1157 WMFont *f = tb->d.font;
1158 len = tb->sections[s].end - tb->sections[s].begin;
1159 text = &(tb->text[tb->sections[s].begin]);
1161 _w = x - tb->sections[s].x;
1162 pos = 0;
1164 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1165 pos++;
1167 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1169 pos += tb->sections[s].begin;
1173 _doneH:
1174 if (tb->graphic) {
1175 tPtr->tpos = (pos <= 1) ? pos : 0;
1176 } else {
1177 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1179 _doNothing:
1180 if (!tb)
1181 printf("...for this app will surely crash :-)\n");
1183 tPtr->currentTextBlock = tb;
1184 tPtr->cursor.h = tb->sections[s].h;
1185 tPtr->cursor.y = tb->sections[s]._y;
1187 /* scroll the bars if the cursor is not visible */
1188 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1189 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1190 tPtr->vpos +=
1191 (tPtr->cursor.y + tPtr->cursor.h + 10
1192 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1193 updateScrollers(tPtr);
1194 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1195 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1196 updateScrollers(tPtr);
1203 static void updateScrollers(Text * tPtr)
1206 if (tPtr->flags.frozen)
1207 return;
1209 if (tPtr->vS) {
1210 if (tPtr->docHeight <= tPtr->visible.h) {
1211 WMSetScrollerParameters(tPtr->vS, 0, 1);
1212 tPtr->vpos = 0;
1213 } else {
1214 float hmax = (float)(tPtr->docHeight);
1215 WMSetScrollerParameters(tPtr->vS,
1216 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1217 (float)tPtr->visible.h / hmax);
1219 } else
1220 tPtr->vpos = 0;
1222 if (tPtr->hS) {
1223 if (tPtr->docWidth <= tPtr->visible.w) {
1224 WMSetScrollerParameters(tPtr->hS, 0, 1);
1225 tPtr->hpos = 0;
1226 } else {
1227 float wmax = (float)(tPtr->docWidth);
1228 WMSetScrollerParameters(tPtr->hS,
1229 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1230 (float)tPtr->visible.w / wmax);
1232 } else
1233 tPtr->hpos = 0;
1236 static void scrollersCallBack(WMWidget * w, void *self)
1238 Text *tPtr = (Text *) self;
1239 Bool scroll = False;
1240 int which;
1242 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1243 return;
1245 if (w == tPtr->vS) {
1246 int height;
1247 height = tPtr->visible.h;
1249 which = WMGetScrollerHitPart(tPtr->vS);
1250 switch (which) {
1252 case WSDecrementLine:
1253 if (tPtr->vpos > 0) {
1254 if (tPtr->vpos > 16)
1255 tPtr->vpos -= 16;
1256 else
1257 tPtr->vpos = 0;
1258 scroll = True;
1260 break;
1262 case WSIncrementLine:{
1263 int limit = tPtr->docHeight - height;
1264 if (tPtr->vpos < limit) {
1265 if (tPtr->vpos < limit - 16)
1266 tPtr->vpos += 16;
1267 else
1268 tPtr->vpos = limit;
1269 scroll = True;
1272 break;
1274 case WSDecrementPage:
1275 if (((int)tPtr->vpos - (int)height) >= 0)
1276 tPtr->vpos -= height;
1277 else
1278 tPtr->vpos = 0;
1280 scroll = True;
1281 break;
1283 case WSIncrementPage:
1284 tPtr->vpos += height;
1285 if (tPtr->vpos > (tPtr->docHeight - height))
1286 tPtr->vpos = tPtr->docHeight - height;
1287 scroll = True;
1288 break;
1290 case WSKnob:
1291 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1292 * (float)(tPtr->docHeight - height);
1293 scroll = True;
1294 break;
1296 case WSKnobSlot:
1297 case WSNoPart:
1298 break;
1300 scroll = (tPtr->vpos != tPtr->prevVpos);
1301 tPtr->prevVpos = tPtr->vpos;
1304 if (w == tPtr->hS) {
1305 int width = tPtr->visible.w;
1307 which = WMGetScrollerHitPart(tPtr->hS);
1308 switch (which) {
1310 case WSDecrementLine:
1311 if (tPtr->hpos > 0) {
1312 if (tPtr->hpos > 16)
1313 tPtr->hpos -= 16;
1314 else
1315 tPtr->hpos = 0;
1316 scroll = True;
1318 break;
1320 case WSIncrementLine:{
1321 int limit = tPtr->docWidth - width;
1322 if (tPtr->hpos < limit) {
1323 if (tPtr->hpos < limit - 16)
1324 tPtr->hpos += 16;
1325 else
1326 tPtr->hpos = limit;
1327 scroll = True;
1330 break;
1332 case WSDecrementPage:
1333 if (((int)tPtr->hpos - (int)width) >= 0)
1334 tPtr->hpos -= width;
1335 else
1336 tPtr->hpos = 0;
1338 scroll = True;
1339 break;
1341 case WSIncrementPage:
1342 tPtr->hpos += width;
1343 if (tPtr->hpos > (tPtr->docWidth - width))
1344 tPtr->hpos = tPtr->docWidth - width;
1345 scroll = True;
1346 break;
1348 case WSKnob:
1349 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1350 * (float)(tPtr->docWidth - width);
1351 scroll = True;
1352 break;
1354 case WSKnobSlot:
1355 case WSNoPart:
1356 break;
1358 scroll = (tPtr->hpos != tPtr->prevHpos);
1359 tPtr->prevHpos = tPtr->hpos;
1362 if (scroll) {
1363 updateScrollers(tPtr);
1364 paintText(tPtr);
1368 typedef struct {
1369 TextBlock *tb;
1370 unsigned short begin, end; /* what part of the text block */
1371 } myLineItems;
1373 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1375 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1376 WMFont *font;
1377 char *text;
1378 TextBlock *tb, *tbsame = NULL;
1380 if (!items || nitems == 0)
1381 return 0;
1383 for (i = 0; i < nitems; i++) {
1384 tb = items[i].tb;
1386 if (tb->graphic) {
1387 if (!tPtr->flags.monoFont) {
1388 if (tb->object) {
1389 WMWidget *wdt = tb->d.widget;
1390 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1391 if (tPtr->flags.alignment != WALeft)
1392 lw += WMWidgetWidth(wdt);
1393 } else {
1394 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1395 if (tPtr->flags.alignment != WALeft)
1396 lw += tb->d.pixmap->width;
1400 } else {
1401 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1402 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1403 max_d = 2;
1404 line_height = WMAX(line_height, font->height + max_d);
1405 text = &(tb->text[items[i].begin]);
1406 len = items[i].end - items[i].begin;
1407 if (tPtr->flags.alignment != WALeft)
1408 lw += WMWidthOfString(font, text, len);
1412 if (tPtr->flags.alignment == WARight) {
1413 j = tPtr->visible.w - lw;
1414 } else if (tPtr->flags.alignment == WACenter) {
1415 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1418 for (i = 0; i < nitems; i++) {
1419 tb = items[i].tb;
1421 if (tbsame == tb) { /* extend it, since it's on same line */
1422 tb->sections[tb->nsections - 1].end = items[i].end;
1423 n = tb->nsections - 1;
1424 } else {
1425 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1426 n = tb->nsections - 1;
1427 tb->sections[n]._y = y + max_d;
1428 tb->sections[n].max_d = max_d;
1429 tb->sections[n].x = x + j;
1430 tb->sections[n].h = line_height;
1431 tb->sections[n].begin = items[i].begin;
1432 tb->sections[n].end = items[i].end;
1435 tb->sections[n].last = (i + 1 == nitems);
1437 if (tb->graphic) {
1438 if (!tPtr->flags.monoFont) {
1439 if (tb->object) {
1440 WMWidget *wdt = tb->d.widget;
1441 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1442 tb->sections[n].w = WMWidgetWidth(wdt);
1443 } else {
1444 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1445 tb->sections[n].w = tb->d.pixmap->width;
1447 x += tb->sections[n].w;
1449 } else {
1450 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1451 len = items[i].end - items[i].begin;
1452 text = &(tb->text[items[i].begin]);
1454 tb->sections[n].y = y + line_height - font->y;
1455 tb->sections[n].w =
1456 WMWidthOfString(font,
1457 &(tb->text[tb->sections[n].begin]),
1458 tb->sections[n].end - tb->sections[n].begin);
1460 x += WMWidthOfString(font, text, len);
1463 tbsame = tb;
1466 return line_height;
1470 static void layOutDocument(Text * tPtr)
1472 TextBlock *tb;
1473 myLineItems *items = NULL;
1474 unsigned int itemsSize = 0, nitems = 0, begin, end;
1475 WMFont *font;
1476 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1477 char *start = NULL, *mark = NULL;
1479 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1480 return;
1482 assert(tPtr->visible.w > 20);
1484 tPtr->docWidth = tPtr->visible.w;
1485 x = tPtr->margins[tb->marginN].first;
1486 bmargin = tPtr->margins[tb->marginN].body;
1488 /* only partial layOut needed: re-Lay only affected textblocks */
1489 if (tPtr->flags.laidOut) {
1490 tb = tPtr->currentTextBlock;
1492 /* search backwards for textblocks on same line */
1493 while (tb->prior) {
1494 if (!tb->sections || tb->nsections < 1) {
1495 tb = tPtr->firstTextBlock;
1496 tPtr->flags.laidOut = False;
1497 y = 0;
1498 goto _layOut;
1501 if (!tb->prior->sections || tb->prior->nsections < 1) {
1502 tb = tPtr->firstTextBlock;
1503 tPtr->flags.laidOut = False;
1504 y = 0;
1505 goto _layOut;
1508 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1509 break;
1511 tb = tb->prior;
1514 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1515 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1516 tb->prior->sections[tb->prior->nsections - 1].h -
1517 tb->prior->sections[tb->prior->nsections - 1].max_d;
1518 } else {
1519 y = 0;
1523 _layOut:
1524 while (tb) {
1526 if (tb->sections && tb->nsections > 0) {
1527 wfree(tb->sections);
1528 tb->sections = NULL;
1529 tb->nsections = 0;
1532 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1533 TextBlock *next = tb->next;
1534 tPtr->currentTextBlock = tb;
1535 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1536 tb = next;
1537 tb->first = True;
1538 continue;
1541 if (tb->first && tb != tPtr->firstTextBlock) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 x = tPtr->margins[tb->marginN].first;
1544 bmargin = tPtr->margins[tb->marginN].body;
1545 nitems = 0;
1546 lw = 0;
1549 if (tb->graphic) {
1550 if (!tPtr->flags.monoFont) {
1551 if (tb->object)
1552 width = WMWidgetWidth(tb->d.widget);
1553 else
1554 width = tb->d.pixmap->width;
1556 if (width > tPtr->docWidth)
1557 tPtr->docWidth = width;
1559 lw += width;
1560 if (lw >= tPtr->visible.w - x) {
1561 y += layOutLine(tPtr, items, nitems, x, y);
1562 nitems = 0;
1563 x = bmargin;
1564 lw = width;
1567 if (nitems + 1 > itemsSize) {
1568 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1571 items[nitems].tb = tb;
1572 items[nitems].begin = 0;
1573 items[nitems].end = 0;
1574 nitems++;
1577 } else if ((start = tb->text)) {
1578 begin = end = 0;
1579 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1581 while (start) {
1582 mark = strchr(start, ' ');
1583 if (mark) {
1584 end += (int)(mark - start) + 1;
1585 start = mark + 1;
1586 } else {
1587 end += strlen(start);
1588 start = mark;
1591 if (end > tb->used)
1592 end = tb->used;
1594 if (end - begin > 0) {
1596 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1598 /* if it won't fit, char wrap it */
1599 if (width >= tPtr->visible.w) {
1600 char *t = &tb->text[begin];
1601 int l = end - begin, i = 0;
1602 do {
1603 width = WMWidthOfString(font, t, ++i);
1604 } while (width < tPtr->visible.w && i < l);
1605 if (i > 2)
1606 i--;
1607 end = begin + i;
1608 start = &tb->text[end];
1611 lw += width;
1614 if (lw >= tPtr->visible.w - x) {
1615 y += layOutLine(tPtr, items, nitems, x, y);
1616 lw = width;
1617 x = bmargin;
1618 nitems = 0;
1621 if (nitems + 1 > itemsSize) {
1622 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1625 items[nitems].tb = tb;
1626 items[nitems].begin = begin;
1627 items[nitems].end = end;
1628 nitems++;
1630 begin = end;
1634 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1635 if (0 && tPtr->flags.laidOut
1636 && tb->next && tb->next->sections && tb->next->nsections > 0
1637 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1638 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1639 TextBlock *ltb = tPtr->lastTextBlock;
1640 int ly = ltb->sections[ltb->nsections - 1]._y;
1641 int lh = ltb->sections[ltb->nsections - 1].h;
1642 int ss, sd;
1644 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1645 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1647 y += layOutLine(tPtr, items, nitems, x, y);
1648 ss = ly + lh - y;
1649 sd = tPtr->docHeight - y;
1651 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1652 y += tb->next->sections[0]._y - y;
1653 nitems = 0;
1654 printf("nitems%d\n", nitems);
1655 if (ss - sd != 0)
1656 y = tPtr->docHeight + ss - sd;
1658 break;
1659 } else {
1660 tPtr->flags.laidOut = False;
1664 tb = tb->next;
1667 if (nitems > 0)
1668 y += layOutLine(tPtr, items, nitems, x, y);
1670 if (tPtr->docHeight != y + 10) {
1671 tPtr->docHeight = y + 10;
1672 updateScrollers(tPtr);
1675 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1676 XEvent event;
1678 tPtr->flags.horizOnDemand = True;
1679 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1680 event.type = Expose;
1681 handleEvents(&event, (void *)tPtr);
1683 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1684 tPtr->flags.horizOnDemand = False;
1685 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1688 tPtr->flags.laidOut = True;
1690 if (items && itemsSize > 0)
1691 wfree(items);
1695 static void textDidResize(W_ViewDelegate * self, WMView * view)
1697 Text *tPtr = (Text *) view->self;
1698 unsigned short w = tPtr->view->size.width;
1699 unsigned short h = tPtr->view->size.height;
1700 unsigned short rh = 0, vw = 0, rel;
1702 rel = (tPtr->flags.relief == WRFlat);
1704 if (tPtr->ruler && tPtr->flags.rulerShown) {
1705 WMMoveWidget(tPtr->ruler, 2, 2);
1706 WMResizeWidget(tPtr->ruler, w - 4, 40);
1707 rh = 40;
1710 if (tPtr->vS) {
1711 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1712 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1713 vw = 20;
1714 WMSetRulerOffset(tPtr->ruler, 22);
1715 } else
1716 WMSetRulerOffset(tPtr->ruler, 2);
1718 if (tPtr->hS) {
1719 if (tPtr->vS) {
1720 WMMoveWidget(tPtr->hS, vw, h - 21);
1721 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1722 } else {
1723 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1724 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1728 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1729 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1730 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1731 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1732 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1733 tPtr->margins[0].right = tPtr->visible.w;
1735 if (tPtr->view->flags.realized) {
1737 if (tPtr->db) {
1738 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1739 tPtr->db = (Pixmap) NULL;
1742 if (tPtr->visible.w < 40)
1743 tPtr->visible.w = 40;
1744 if (tPtr->visible.h < 20)
1745 tPtr->visible.h = 20;
1747 if (!tPtr->db) {
1748 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1749 tPtr->view->window, tPtr->visible.w,
1750 tPtr->visible.h, tPtr->view->screen->depth);
1754 WMThawText(tPtr);
1757 W_ViewDelegate _TextViewDelegate = {
1758 NULL,
1759 NULL,
1760 textDidResize,
1761 NULL,
1764 #define TEXT_BUFFER_INCR 8
1765 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1767 static void clearText(Text * tPtr)
1769 tPtr->vpos = tPtr->hpos = 0;
1770 tPtr->docHeight = tPtr->docWidth = 0;
1771 tPtr->cursor.x = -23;
1773 if (!tPtr->firstTextBlock)
1774 return;
1776 while (tPtr->currentTextBlock)
1777 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1779 tPtr->firstTextBlock = NULL;
1780 tPtr->currentTextBlock = NULL;
1781 tPtr->lastTextBlock = NULL;
1782 WMEmptyArray(tPtr->gfxItems);
1785 /* possibly remove a single character from the currentTextBlock,
1786 or if there's a selection, remove it...
1787 note that Delete and Backspace are treated differently */
1788 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1790 TextBlock *tb;
1791 Bool back = (Bool) (ksym == XK_BackSpace);
1792 Bool done = 1, wasFirst = 0;
1794 if (!tPtr->flags.editable)
1795 return;
1797 if (!(tb = tPtr->currentTextBlock))
1798 return;
1800 if (tPtr->flags.ownsSelection) {
1801 if (removeSelection(tPtr))
1802 layOutDocument(tPtr);
1803 return;
1806 wasFirst = tb->first;
1807 if (back && tPtr->tpos < 1) {
1808 if (tb->prior) {
1809 if (tb->prior->blank) {
1810 tPtr->currentTextBlock = tb->prior;
1811 WMRemoveTextBlock(tPtr);
1812 tPtr->currentTextBlock = tb;
1813 tb->first = True;
1814 layOutDocument(tPtr);
1815 return;
1816 } else {
1817 if (tb->blank) {
1818 TextBlock *prior = tb->prior;
1819 tPtr->currentTextBlock = tb;
1820 WMRemoveTextBlock(tPtr);
1821 tb = prior;
1822 } else {
1823 tb = tb->prior;
1826 if (tb->graphic)
1827 tPtr->tpos = 1;
1828 else
1829 tPtr->tpos = tb->used;
1831 tPtr->currentTextBlock = tb;
1832 done = 1;
1833 if (wasFirst) {
1834 if (tb->next)
1835 tb->next->first = False;
1836 layOutDocument(tPtr);
1837 return;
1843 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1844 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1845 if (back)
1846 tPtr->tpos--;
1847 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1848 tb->used--;
1849 done = 0;
1852 /* if there are no characters left to back over in the textblock,
1853 but it still has characters to the right of the cursor: */
1854 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1855 || tb->graphic) {
1857 /* no more chars, and it's marked as blank? */
1858 if (tb->blank) {
1859 TextBlock *sibling = (back ? tb->prior : tb->next);
1861 if (tb->used == 0 || tb->graphic)
1862 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1864 if (sibling) {
1865 tPtr->currentTextBlock = sibling;
1866 if (tb->graphic)
1867 tPtr->tpos = (back ? 1 : 0);
1868 else
1869 tPtr->tpos = (back ? sibling->used : 0);
1871 /* no more chars, so mark it as blank */
1872 } else if (tb->used == 0) {
1873 tb->blank = 1;
1874 } else if (tb->graphic) {
1875 Bool hasNext = (tb->next != NULL);
1877 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1878 if (hasNext) {
1879 tPtr->tpos = 0;
1880 } else if (tPtr->currentTextBlock) {
1881 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1883 } else
1884 printf("DEBUG: unaccounted for... catch this!\n");
1887 layOutDocument(tPtr);
1890 static void insertTextInteractively(Text * tPtr, char *text, int len)
1892 TextBlock *tb;
1893 char *newline = NULL;
1895 if (!tPtr->flags.editable) {
1896 return;
1899 if (len < 1 || !text)
1900 return;
1902 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1903 return;
1905 if (tPtr->flags.ownsSelection)
1906 removeSelection(tPtr);
1908 if (tPtr->flags.ignoreNewLine) {
1909 int i;
1910 for (i = 0; i < len; i++) {
1911 if (text[i] == '\n')
1912 text[i] = ' ';
1916 tb = tPtr->currentTextBlock;
1917 if (!tb || tb->graphic) {
1918 tPtr->tpos = 0;
1919 WMAppendTextStream(tPtr, text);
1920 layOutDocument(tPtr);
1921 return;
1924 if ((newline = strchr(text, '\n'))) {
1925 int nlen = (int)(newline - text);
1926 int s = tb->used - tPtr->tpos;
1928 if (!tb->blank && nlen > 0) {
1929 char *save = NULL;
1931 if (s > 0) {
1932 save = wmalloc(s);
1933 memcpy(save, &tb->text[tPtr->tpos], s);
1934 tb->used -= (tb->used - tPtr->tpos);
1936 insertTextInteractively(tPtr, text, nlen);
1937 newline++;
1938 WMAppendTextStream(tPtr, newline);
1939 if (s > 0) {
1940 insertTextInteractively(tPtr, save, s);
1941 wfree(save);
1943 } else {
1944 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1946 unsigned short savePos = tPtr->tpos;
1947 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1948 tb->d.font, tb->color, True,
1949 tb->used - tPtr->tpos);
1951 if (tb->sections[0].end == tPtr->tpos)
1952 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1953 NULL, tb->d.font,
1954 tb->color, True, 0));
1956 tb->used = savePos;
1957 WMAppendTextBlock(tPtr, ntb);
1958 tPtr->tpos = 0;
1960 } else if (tPtr->tpos == tb->used) {
1961 if (tPtr->flags.indentNewLine) {
1962 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1963 " ", tb->d.font,
1964 tb->color, True, 4));
1965 tPtr->tpos = 4;
1966 } else {
1967 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1968 NULL, tb->d.font,
1969 tb->color, True, 0));
1970 tPtr->tpos = 0;
1972 } else if (tPtr->tpos == 0) {
1973 if (tPtr->flags.indentNewLine) {
1974 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1975 " ", tb->d.font,
1976 tb->color, True, 4));
1977 } else {
1978 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1979 NULL, tb->d.font,
1980 tb->color, True, 0));
1982 tPtr->tpos = 0;
1983 if (tPtr->currentTextBlock->next)
1984 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1987 } else {
1988 if (tb->used + len >= tb->allocated) {
1989 tb->allocated = reqBlockSize(tb->used + len);
1990 tb->text = wrealloc(tb->text, tb->allocated);
1993 if (tb->blank) {
1994 memcpy(tb->text, text, len);
1995 tb->used = len;
1996 tPtr->tpos = len;
1997 tb->text[tb->used] = 0;
1998 tb->blank = False;
2000 } else {
2001 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
2002 memmove(&tb->text[tPtr->tpos], text, len);
2003 tb->used += len;
2004 tPtr->tpos += len;
2005 tb->text[tb->used] = 0;
2010 layOutDocument(tPtr);
2013 static void selectRegion(Text * tPtr, int x, int y)
2016 if (x < 0 || y < 0)
2017 return;
2019 y += (tPtr->flags.rulerShown ? 40 : 0);
2020 y += tPtr->vpos;
2021 if (y > 10)
2022 y -= 10; /* the original offset */
2024 x -= tPtr->visible.x - 2;
2025 if (x < 0)
2026 x = 0;
2028 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2029 tPtr->sel.w = abs(tPtr->clicked.x - x);
2030 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2031 tPtr->sel.h = abs(tPtr->clicked.y - y);
2033 tPtr->flags.ownsSelection = True;
2034 paintText(tPtr);
2037 static void releaseSelection(Text * tPtr)
2039 TextBlock *tb = tPtr->firstTextBlock;
2041 while (tb) {
2042 tb->selected = False;
2043 tb = tb->next;
2045 tPtr->flags.ownsSelection = False;
2046 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2048 paintText(tPtr);
2051 WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2053 Text *tPtr = view->self;
2054 Display *dpy = tPtr->view->screen->display;
2055 Atom _TARGETS;
2056 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2057 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2058 WMData *data = NULL;
2060 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2061 char *text = WMGetTextSelectedStream(tPtr);
2063 if (text) {
2064 data = WMCreateDataWithBytes(text, strlen(text));
2065 WMSetDataFormat(data, TYPETEXT);
2067 *type = target;
2068 return data;
2069 } else
2070 printf("didn't get it\n");
2072 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2073 if (target == _TARGETS) {
2074 Atom *ptr;
2076 ptr = wmalloc(4 * sizeof(Atom));
2077 ptr[0] = _TARGETS;
2078 ptr[1] = XA_STRING;
2079 ptr[2] = TEXT;
2080 ptr[3] = COMPOUND_TEXT;
2082 data = WMCreateDataWithBytes(ptr, 4 * 4);
2083 WMSetDataFormat(data, 32);
2085 *type = target;
2086 return data;
2089 return NULL;
2092 static void lostHandler(WMView * view, Atom selection, void *cdata)
2094 releaseSelection((WMText *) view->self);
2097 static WMSelectionProcs selectionHandler = {
2098 requestHandler, lostHandler, NULL
2101 static void ownershipObserver(void *observerData, WMNotification * notification)
2103 if (observerData != WMGetNotificationClientData(notification))
2104 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2107 static void autoSelectText(Text * tPtr, int clicks)
2109 int x, start;
2110 TextBlock *tb;
2111 char *mark = NULL, behind, ahead;
2113 if (!(tb = tPtr->currentTextBlock))
2114 return;
2116 if (clicks == 2) {
2118 switch (tb->text[tPtr->tpos]) {
2119 case ' ':
2120 return;
2122 case '<': case '>': behind = '<'; ahead = '>'; break;
2123 case '{': case '}': behind = '{'; ahead = '}'; break;
2124 case '[': case ']': behind = '['; ahead = ']'; break;
2126 default:
2127 behind = ahead = ' ';
2130 tPtr->sel.y = tPtr->cursor.y + 5;
2131 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2133 if (tb->graphic) {
2134 tPtr->sel.x = tb->sections[0].x;
2135 tPtr->sel.w = tb->sections[0].w;
2136 } else {
2137 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2139 start = tPtr->tpos;
2140 while (start > 0 && tb->text[start - 1] != behind)
2141 start--;
2143 x = tPtr->cursor.x;
2144 if (tPtr->tpos > start) {
2145 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2147 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2149 if ((mark = strchr(&tb->text[start], ahead))) {
2150 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2151 (int)(mark - &tb->text[start]));
2152 } else if (tb->used > start) {
2153 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2157 } else if (clicks == 3) {
2158 TextBlock *cur = tb;
2160 while (tb && !tb->first) {
2161 tb = tb->prior;
2163 tPtr->sel.y = tb->sections[0]._y;
2165 tb = cur;
2166 while (tb->next && !tb->next->first) {
2167 tb = tb->next;
2169 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2171 tPtr->sel.x = 0;
2172 tPtr->sel.w = tPtr->docWidth;
2173 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2176 if (!tPtr->flags.ownsSelection) {
2177 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2178 tPtr->flags.ownsSelection = True;
2180 paintText(tPtr);
2184 # if 0
2185 static void fontChanged(void *observerData, WMNotification * notification)
2187 WMText *tPtr = (WMText *) observerData;
2188 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2189 printf("fontChanged\n");
2191 if (!tPtr || !font)
2192 return;
2194 if (tPtr->flags.ownsSelection)
2195 WMSetTextSelectionFont(tPtr, font);
2197 #endif
2199 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2201 char buffer[64];
2202 KeySym ksym;
2203 int control_pressed = False;
2204 TextBlock *tb = NULL;
2206 if (((XKeyEvent *) event)->state & ControlMask)
2207 control_pressed = True;
2208 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2210 switch (ksym) {
2212 case XK_Home:
2213 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2214 tPtr->tpos = 0;
2215 updateCursorPosition(tPtr);
2216 paintText(tPtr);
2217 break;
2219 case XK_End:
2220 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2221 if (tPtr->currentTextBlock->graphic)
2222 tPtr->tpos = 1;
2223 else
2224 tPtr->tpos = tPtr->currentTextBlock->used;
2226 updateCursorPosition(tPtr);
2227 paintText(tPtr);
2228 break;
2230 case XK_Left:
2231 if (!(tb = tPtr->currentTextBlock))
2232 break;
2233 if (tb->graphic)
2234 goto L_imaGFX;
2236 if (tPtr->tpos == 0) {
2237 L_imaGFX:
2238 if (tb->prior) {
2239 tPtr->currentTextBlock = tb->prior;
2240 if (tPtr->currentTextBlock->graphic)
2241 tPtr->tpos = 1;
2242 else
2243 tPtr->tpos = tPtr->currentTextBlock->used;
2245 if (!tb->first && tPtr->tpos > 0)
2246 tPtr->tpos--;
2247 } else
2248 tPtr->tpos = 0;
2249 } else
2250 tPtr->tpos--;
2251 updateCursorPosition(tPtr);
2252 paintText(tPtr);
2253 break;
2255 case XK_Right:
2256 if (!(tb = tPtr->currentTextBlock))
2257 break;
2258 if (tb->graphic)
2259 goto R_imaGFX;
2260 if (tPtr->tpos == tb->used) {
2261 R_imaGFX:
2262 if (tb->next) {
2263 tPtr->currentTextBlock = tb->next;
2264 tPtr->tpos = 0;
2265 if (!tb->next->first && tb->next->used > 0)
2266 tPtr->tpos++;
2267 } else {
2268 if (tb->graphic)
2269 tPtr->tpos = 1;
2270 else
2271 tPtr->tpos = tb->used;
2273 } else
2274 tPtr->tpos++;
2275 updateCursorPosition(tPtr);
2276 paintText(tPtr);
2277 break;
2279 case XK_Down:
2280 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2281 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2282 paintText(tPtr);
2283 break;
2285 case XK_Up:
2286 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2287 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2288 paintText(tPtr);
2289 break;
2291 case XK_BackSpace:
2292 case XK_Delete:
2293 #ifdef XK_KP_Delete
2294 case XK_KP_Delete:
2295 #endif
2296 deleteTextInteractively(tPtr, ksym);
2297 updateCursorPosition(tPtr);
2298 paintText(tPtr);
2299 break;
2301 case XK_Control_R:
2302 case XK_Control_L:
2303 control_pressed = True;
2304 break;
2306 case XK_Tab:
2307 insertTextInteractively(tPtr, " ", 4);
2308 updateCursorPosition(tPtr);
2309 paintText(tPtr);
2310 break;
2312 case XK_Return:
2313 *buffer = '\n';
2314 default:
2315 if (*buffer != 0 && !control_pressed) {
2316 insertTextInteractively(tPtr, buffer, strlen(buffer));
2317 updateCursorPosition(tPtr);
2318 paintText(tPtr);
2320 } else if (control_pressed && ksym == XK_r) {
2321 Bool i = !tPtr->flags.rulerShown;
2322 WMShowTextRuler(tPtr, i);
2323 tPtr->flags.rulerShown = i;
2324 } else if (control_pressed && *buffer == '\a') {
2325 XBell(tPtr->view->screen->display, 0);
2326 } else {
2327 WMRelayToNextResponder(tPtr->view, event);
2331 if (!control_pressed && tPtr->flags.ownsSelection) {
2332 releaseSelection(tPtr);
2336 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2338 Text *tPtr = (Text *) view->self;
2339 char *text;
2341 tPtr->flags.waitingForSelection = 0;
2343 if (data) {
2344 text = (char *)WMDataBytes(data);
2346 if (tPtr->parser) {
2347 (tPtr->parser) (tPtr, (void *)text);
2348 layOutDocument(tPtr);
2349 } else
2350 insertTextInteractively(tPtr, text, strlen(text));
2351 updateCursorPosition(tPtr);
2352 paintText(tPtr);
2354 } else {
2355 int n;
2357 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2359 if (text) {
2360 text[n] = 0;
2361 if (tPtr->parser) {
2362 (tPtr->parser) (tPtr, (void *)text);
2363 layOutDocument(tPtr);
2364 } else
2365 insertTextInteractively(tPtr, text, n);
2366 updateCursorPosition(tPtr);
2367 paintText(tPtr);
2369 XFree(text);
2375 static void handleActionEvents(XEvent * event, void *data)
2377 Text *tPtr = (Text *) data;
2378 Display *dpy = event->xany.display;
2379 KeySym ksym;
2381 switch (event->type) {
2382 case KeyPress:
2383 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2384 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2385 tPtr->flags.extendSelection = True;
2386 return;
2389 if (tPtr->flags.focused) {
2390 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2391 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2392 GrabModeAsync, GrabModeAsync, None,
2393 tPtr->view->screen->invisibleCursor, CurrentTime);
2394 tPtr->flags.pointerGrabbed = True;
2395 handleTextKeyPress(tPtr, event);
2398 break;
2400 case KeyRelease:
2401 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2402 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2403 tPtr->flags.extendSelection = False;
2404 return;
2405 /* end modify flag so selection can be extended */
2407 break;
2409 case MotionNotify:
2411 if (tPtr->flags.pointerGrabbed) {
2412 tPtr->flags.pointerGrabbed = False;
2413 XUngrabPointer(dpy, CurrentTime);
2416 if (tPtr->flags.waitingForSelection)
2417 break;
2419 if ((event->xmotion.state & Button1Mask)) {
2421 if (WMIsDraggingFromView(tPtr->view)) {
2422 WMDragImageFromView(tPtr->view, event);
2423 break;
2426 if (!tPtr->flags.ownsSelection) {
2427 WMCreateSelectionHandler(tPtr->view,
2428 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2429 tPtr->flags.ownsSelection = True;
2431 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2432 break;
2435 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2436 break;
2438 case ButtonPress:
2440 if (tPtr->flags.pointerGrabbed) {
2441 tPtr->flags.pointerGrabbed = False;
2442 XUngrabPointer(dpy, CurrentTime);
2443 break;
2446 if (tPtr->flags.waitingForSelection)
2447 break;
2449 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2450 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2451 return;
2454 if (tPtr->flags.ownsSelection)
2455 releaseSelection(tPtr);
2457 if (event->xbutton.button == Button1) {
2458 TextBlock *tb = tPtr->currentTextBlock;
2460 if (WMIsDoubleClick(event)) {
2462 tPtr->lastClickTime = event->xbutton.time;
2463 if (tb && tb->graphic && !tb->object) {
2464 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2465 char *desc;
2467 desc = wmalloc(tb->used + 1);
2468 memcpy(desc, tb->text, tb->used);
2469 desc[tb->used] = 0;
2470 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2471 wfree(desc);
2473 } else {
2474 autoSelectText(tPtr, 2);
2476 break;
2477 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2478 tPtr->lastClickTime = event->xbutton.time;
2479 autoSelectText(tPtr, 3);
2480 break;
2483 if (!tPtr->flags.focused) {
2484 WMSetFocusToWidget(tPtr);
2485 tPtr->flags.focused = True;
2486 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2488 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2489 WMDragImageFromView(tPtr->view, event);
2490 break;
2493 tPtr->lastClickTime = event->xbutton.time;
2494 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2495 paintText(tPtr);
2498 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2499 WMScrollText(tPtr, 16);
2500 break;
2503 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2504 WMScrollText(tPtr, -16);
2505 break;
2508 if (event->xbutton.button == Button2) {
2509 char *text = NULL;
2510 int n;
2512 if (!tPtr->flags.editable) {
2513 XBell(dpy, 0);
2514 break;
2517 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2518 event->xbutton.time, pasteText, NULL)) {
2520 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2521 tPtr->flags.waitingForSelection = 0;
2523 if (text) {
2524 text[n] = 0;
2526 if (tPtr->parser) {
2527 (tPtr->parser) (tPtr, (void *)text);
2528 layOutDocument(tPtr);
2529 } else
2530 insertTextInteractively(tPtr, text, n);
2532 XFree(text);
2533 #if 0
2534 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2535 (void *)WMInsertTextEvent);
2536 #endif
2537 updateCursorPosition(tPtr);
2538 paintText(tPtr);
2540 } else {
2541 tPtr->flags.waitingForSelection = True;
2544 break;
2547 case ButtonRelease:
2548 if (tPtr->flags.pointerGrabbed) {
2549 tPtr->flags.pointerGrabbed = False;
2550 XUngrabPointer(dpy, CurrentTime);
2551 break;
2554 if (tPtr->flags.waitingForSelection)
2555 break;
2557 if (WMIsDraggingFromView(tPtr->view))
2558 WMDragImageFromView(tPtr->view, event);
2563 static void handleEvents(XEvent * event, void *data)
2565 Text *tPtr = (Text *) data;
2567 switch (event->type) {
2568 case Expose:
2570 if (event->xexpose.count != 0)
2571 break;
2573 if (tPtr->hS) {
2574 if (!(W_VIEW(tPtr->hS))->flags.realized)
2575 WMRealizeWidget(tPtr->hS);
2578 if (tPtr->vS) {
2579 if (!(W_VIEW(tPtr->vS))->flags.realized)
2580 WMRealizeWidget(tPtr->vS);
2583 if (tPtr->ruler) {
2584 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2585 WMRealizeWidget(tPtr->ruler);
2589 if (!tPtr->db)
2590 textDidResize(tPtr->view->delegate, tPtr->view);
2592 paintText(tPtr);
2593 break;
2595 case FocusIn:
2596 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2597 != tPtr->view)
2598 return;
2599 tPtr->flags.focused = True;
2600 #if DO_BLINK
2601 if (tPtr->flags.editable && !tPtr->timerID) {
2602 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2604 #endif
2606 break;
2608 case FocusOut:
2609 tPtr->flags.focused = False;
2610 paintText(tPtr);
2611 #if DO_BLINK
2612 if (tPtr->timerID) {
2613 WMDeleteTimerHandler(tPtr->timerID);
2614 tPtr->timerID = NULL;
2616 #endif
2617 break;
2619 case DestroyNotify:
2620 clearText(tPtr);
2621 if (tPtr->db)
2622 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2623 if (tPtr->gfxItems)
2624 WMEmptyArray(tPtr->gfxItems);
2625 #if DO_BLINK
2626 if (tPtr->timerID)
2627 WMDeleteTimerHandler(tPtr->timerID);
2628 #endif
2629 WMReleaseFont(tPtr->dFont);
2630 WMReleaseColor(tPtr->dColor);
2631 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2632 WMRemoveNotificationObserver(tPtr);
2634 WMFreeArray(tPtr->xdndSourceTypes);
2635 WMFreeArray(tPtr->xdndDestinationTypes);
2637 wfree(tPtr);
2639 break;
2644 static void insertPlainText(Text * tPtr, char *text)
2646 char *start, *mark;
2647 void *tb = NULL;
2649 start = text;
2650 while (start) {
2651 mark = strchr(start, '\n');
2652 if (mark) {
2653 tb = WMCreateTextBlockWithText(tPtr,
2654 start, tPtr->dFont,
2655 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2656 start = mark + 1;
2657 tPtr->flags.first = True;
2658 } else {
2659 if (start && strlen(start)) {
2660 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2661 tPtr->dColor, tPtr->flags.first, strlen(start));
2662 } else
2663 tb = NULL;
2664 tPtr->flags.first = False;
2665 start = mark;
2668 if (tPtr->flags.prepend)
2669 WMPrependTextBlock(tPtr, tb);
2670 else
2671 WMAppendTextBlock(tPtr, tb);
2675 static void rulerMoveCallBack(WMWidget * w, void *self)
2677 Text *tPtr = (Text *) self;
2679 if (!tPtr)
2680 return;
2681 if (W_CLASS(tPtr) != WC_Text)
2682 return;
2684 paintText(tPtr);
2687 static void rulerReleaseCallBack(WMWidget * w, void *self)
2689 Text *tPtr = (Text *) self;
2691 if (!tPtr)
2692 return;
2693 if (W_CLASS(tPtr) != WC_Text)
2694 return;
2696 WMThawText(tPtr);
2697 return;
2700 static WMArray *dropDataTypes(WMView * self)
2702 return ((Text *) self->self)->xdndSourceTypes;
2705 static WMDragOperationType wantedDropOperation(WMView * self)
2707 return WDOperationCopy;
2710 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2712 return (allowedOperation == WDOperationCopy);
2715 static WMData *fetchDragData(WMView * self, char *type)
2717 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2718 char *desc;
2719 WMData *data;
2721 if (strcmp(type, "text/plain")) {
2722 if (!tb)
2723 return NULL;
2725 desc = wmalloc(tb->used + 1);
2726 memcpy(desc, tb->text, tb->used);
2727 desc[tb->used] = 0;
2728 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2730 wfree(desc);
2732 return data;
2735 return NULL;
2738 static WMDragSourceProcs _DragSourceProcs = {
2739 dropDataTypes,
2740 wantedDropOperation,
2741 NULL,
2742 acceptDropOperation,
2743 NULL,
2744 NULL,
2745 fetchDragData
2748 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2750 return ((Text *) self->self)->xdndDestinationTypes;
2753 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2755 return WDOperationCopy;
2758 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2760 WMText *tPtr = (WMText *) self->self;
2761 WMData *data;
2762 char *colorName;
2763 WMColor *color;
2765 if (tPtr) {
2767 /* only one required type, implies only one drop data */
2769 /* get application/X-color if any */
2770 data = (WMData *) WMPopFromArray(dropData);
2771 if (data != NULL) {
2772 colorName = (char *)WMDataBytes(data);
2773 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2775 if (color) {
2776 WMSetTextSelectionColor(tPtr, color);
2777 WMReleaseColor(color);
2783 static WMDragDestinationProcs _DragDestinationProcs = {
2784 NULL,
2785 requiredDataTypes,
2786 allowedOperation,
2787 NULL,
2788 performDragOperation,
2789 NULL
2792 char *getStream(WMText * tPtr, int sel, int array)
2794 TextBlock *tb = NULL;
2795 char *text = NULL;
2796 unsigned long where = 0;
2798 if (!tPtr)
2799 return NULL;
2801 if (!(tb = tPtr->firstTextBlock))
2802 return NULL;
2804 if (tPtr->writer) {
2805 (tPtr->writer) (tPtr, (void *)text);
2806 return text;
2809 tb = tPtr->firstTextBlock;
2810 while (tb) {
2812 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2814 if (!sel || (tb->graphic && tb->selected)) {
2816 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2817 && tb != tPtr->firstTextBlock) {
2818 text = wrealloc(text, where + 1);
2819 text[where++] = '\n';
2822 if (tb->blank)
2823 goto _gSnext;
2825 if (tb->graphic && array) {
2826 text = wrealloc(text, where + 4);
2827 text[where++] = 0xFA;
2828 text[where++] = (tb->used >> 8) & 0x0ff;
2829 text[where++] = tb->used & 0x0ff;
2830 text[where++] = tb->allocated; /* extra info */
2832 text = wrealloc(text, where + tb->used);
2833 memcpy(&text[where], tb->text, tb->used);
2834 where += tb->used;
2836 } else if (sel && tb->selected) {
2838 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2839 text = wrealloc(text, where + 1);
2840 text[where++] = '\n';
2843 if (tb->blank)
2844 goto _gSnext;
2846 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2847 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2848 where += tb->s_end - tb->s_begin;
2853 _gSnext: tb = tb->next;
2856 /* +1 for the end of string, let's be nice */
2857 text = wrealloc(text, where + 1);
2858 text[where] = 0;
2859 return text;
2862 static void releaseStreamObjects(void *data)
2864 if (data)
2865 wfree(data);
2868 WMArray *getStreamObjects(WMText * tPtr, int sel)
2870 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2871 WMData *data;
2872 char *stream;
2873 unsigned short len;
2874 char *start, *fa, *desc;
2876 stream = getStream(tPtr, sel, 1);
2877 if (!stream)
2878 return NULL;
2880 start = stream;
2881 while (start) {
2883 fa = strchr(start, 0xFA);
2884 if (fa) {
2885 if ((int)(fa - start) > 0) {
2886 desc = start;
2887 desc[(int)(fa - start)] = 0;
2888 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2889 WMSetDataFormat(data, TYPETEXT);
2890 WMAddToArray(array, (void *)data);
2893 len = *(fa + 1) * 0xff + *(fa + 2);
2894 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2895 WMSetDataFormat(data, *(fa + 3));
2896 WMAddToArray(array, (void *)data);
2897 start = fa + len + 4;
2899 } else {
2900 if (start && strlen(start)) {
2901 data = WMCreateDataWithBytes((void *)start, strlen(start));
2902 WMSetDataFormat(data, TYPETEXT);
2903 WMAddToArray(array, (void *)data);
2905 start = fa;
2909 wfree(stream);
2910 return array;
2913 #define XDND_TEXT_DATA_TYPE "text/plain"
2914 #define XDND_COLOR_DATA_TYPE "application/X-color"
2915 static WMArray *getXdndSourceTypeArray()
2917 WMArray *types = WMCreateArray(1);
2918 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2919 return types;
2922 static WMArray *getXdndDestinationTypeArray()
2924 WMArray *types = WMCreateArray(1);
2925 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2926 return types;
2929 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2931 Text *tPtr;
2932 Display *dpy;
2933 WMScreen *scr;
2934 XGCValues gcv;
2936 tPtr = wmalloc(sizeof(Text));
2937 memset(tPtr, 0, sizeof(Text));
2938 tPtr->widgetClass = WC_Text;
2939 tPtr->view = W_CreateView(W_VIEW(parent));
2940 if (!tPtr->view) {
2941 perror("could not create text's view\n");
2942 wfree(tPtr);
2943 return NULL;
2946 dpy = tPtr->view->screen->display;
2947 scr = tPtr->view->screen;
2949 tPtr->view->self = tPtr;
2950 tPtr->view->attribs.cursor = scr->textCursor;
2951 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2952 W_ResizeView(tPtr->view, 250, 200);
2954 tPtr->dColor = WMBlackColor(scr);
2955 tPtr->fgColor = WMBlackColor(scr);
2956 tPtr->bgColor = WMWhiteColor(scr);
2957 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2959 gcv.graphics_exposures = False;
2960 gcv.foreground = W_PIXEL(scr->gray);
2961 gcv.background = W_PIXEL(scr->darkGray);
2962 gcv.fill_style = FillStippled;
2963 /* why not use scr->stipple here? */
2964 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2965 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2966 GCForeground | GCBackground | GCStipple
2967 | GCFillStyle | GCGraphicsExposures, &gcv);
2969 tPtr->ruler = NULL;
2970 tPtr->vS = NULL;
2971 tPtr->hS = NULL;
2973 tPtr->dFont = WMSystemFontOfSize(scr, 12);
2975 tPtr->view->delegate = &_TextViewDelegate;
2977 tPtr->delegate = NULL;
2979 #if DO_BLINK
2980 tPtr->timerID = NULL;
2981 #endif
2983 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
2984 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
2986 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
2987 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
2989 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
2991 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
2992 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2995 WMArray *types = WMCreateArray(2);
2996 WMAddToArray(types, "application/X-color");
2997 WMAddToArray(types, "application/X-image");
2998 WMRegisterViewForDraggedTypes(tPtr->view, types);
3001 /*WMAddNotificationObserver(fontChanged, tPtr,
3002 WMFontPanelDidChangeNotification, tPtr); */
3004 tPtr->firstTextBlock = NULL;
3005 tPtr->lastTextBlock = NULL;
3006 tPtr->currentTextBlock = NULL;
3007 tPtr->tpos = 0;
3009 tPtr->gfxItems = WMCreateArray(4);
3011 tPtr->parser = parser;
3012 tPtr->writer = writer;
3014 tPtr->sel.x = tPtr->sel.y = 2;
3015 tPtr->sel.w = tPtr->sel.h = 0;
3017 tPtr->clicked.x = tPtr->clicked.y = 2;
3019 tPtr->visible.x = tPtr->visible.y = 2;
3020 tPtr->visible.h = tPtr->view->size.height;
3021 tPtr->visible.w = tPtr->view->size.width - 4;
3023 tPtr->cursor.x = -23;
3025 tPtr->docWidth = 0;
3026 tPtr->docHeight = 0;
3027 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3028 tPtr->db = (Pixmap) NULL;
3029 tPtr->bgPixmap = NULL;
3031 tPtr->margins = WMGetRulerMargins(NULL);
3032 tPtr->margins->right = tPtr->visible.w;
3033 tPtr->nMargins = 1;
3035 tPtr->flags.rulerShown = False;
3036 tPtr->flags.monoFont = False;
3037 tPtr->flags.focused = False;
3038 tPtr->flags.editable = True;
3039 tPtr->flags.ownsSelection = False;
3040 tPtr->flags.pointerGrabbed = False;
3041 tPtr->flags.extendSelection = False;
3042 tPtr->flags.frozen = False;
3043 tPtr->flags.cursorShown = True;
3044 tPtr->flags.acceptsGraphic = False;
3045 tPtr->flags.horizOnDemand = False;
3046 tPtr->flags.needsLayOut = False;
3047 tPtr->flags.ignoreNewLine = False;
3048 tPtr->flags.indentNewLine = False;
3049 tPtr->flags.laidOut = False;
3050 tPtr->flags.ownsSelection = False;
3051 tPtr->flags.waitingForSelection = False;
3052 tPtr->flags.prepend = False;
3053 tPtr->flags.isOverGraphic = False;
3054 tPtr->flags.relief = WRSunken;
3055 tPtr->flags.isOverGraphic = 0;
3056 tPtr->flags.alignment = WALeft;
3057 tPtr->flags.first = True;
3059 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3060 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3062 return tPtr;
3065 void WMPrependTextStream(WMText * tPtr, char *text)
3067 CHECK_CLASS(tPtr, WC_Text);
3069 if (!text) {
3070 if (tPtr->flags.ownsSelection)
3071 releaseSelection(tPtr);
3072 clearText(tPtr);
3073 updateScrollers(tPtr);
3074 return;
3077 tPtr->flags.prepend = True;
3078 if (text && tPtr->parser)
3079 (tPtr->parser) (tPtr, (void *)text);
3080 else
3081 insertPlainText(tPtr, text);
3083 tPtr->flags.needsLayOut = True;
3084 tPtr->tpos = 0;
3085 if (!tPtr->flags.frozen) {
3086 layOutDocument(tPtr);
3090 void WMAppendTextStream(WMText * tPtr, char *text)
3092 CHECK_CLASS(tPtr, WC_Text);
3094 if (!text) {
3095 if (tPtr->flags.ownsSelection)
3096 releaseSelection(tPtr);
3097 clearText(tPtr);
3098 updateScrollers(tPtr);
3099 return;
3102 tPtr->flags.prepend = False;
3103 if (text && tPtr->parser)
3104 (tPtr->parser) (tPtr, (void *)text);
3105 else
3106 insertPlainText(tPtr, text);
3108 tPtr->flags.needsLayOut = True;
3109 if (tPtr->currentTextBlock) {
3110 if (tPtr->currentTextBlock->graphic)
3111 tPtr->tpos = 1;
3112 else
3113 tPtr->tpos = tPtr->currentTextBlock->used;
3116 if (!tPtr->flags.frozen) {
3117 layOutDocument(tPtr);
3121 char *WMGetTextStream(WMText * tPtr)
3123 CHECK_CLASS(tPtr, WC_Text);
3125 return getStream(tPtr, 0, 0);
3128 char *WMGetTextSelectedStream(WMText * tPtr)
3130 CHECK_CLASS(tPtr, WC_Text);
3132 return getStream(tPtr, 1, 0);
3135 WMArray *WMGetTextObjects(WMText * tPtr)
3137 CHECK_CLASS(tPtr, WC_Text);
3139 return getStreamObjects(tPtr, 0);
3142 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3144 CHECK_CLASS(tPtr, WC_Text);
3146 return getStreamObjects(tPtr, 1);
3149 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3151 CHECK_CLASS(tPtr, WC_Text);
3153 tPtr->delegate = delegate;
3156 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3157 char *description, WMColor * color,
3158 unsigned short first, unsigned short extraInfo)
3160 TextBlock *tb;
3162 if (!w || !description || !color)
3163 return NULL;
3165 tb = wmalloc(sizeof(TextBlock));
3167 tb->text = wstrdup(description);
3168 tb->used = strlen(description);
3169 tb->blank = False;
3170 tb->d.widget = w;
3171 tb->color = WMRetainColor(color);
3172 tb->marginN = newMargin(tPtr, NULL);
3173 tb->allocated = extraInfo;
3174 tb->first = first;
3175 tb->kanji = False;
3176 tb->graphic = True;
3177 tb->object = True;
3178 tb->underlined = False;
3179 tb->selected = False;
3180 tb->script = 0;
3181 tb->sections = NULL;
3182 tb->nsections = 0;
3183 tb->prior = NULL;
3184 tb->next = NULL;
3186 return tb;
3189 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3190 char *description, WMColor * color,
3191 unsigned short first, unsigned short extraInfo)
3193 TextBlock *tb;
3195 if (!p || !description || !color)
3196 return NULL;
3198 tb = wmalloc(sizeof(TextBlock));
3200 tb->text = wstrdup(description);
3201 tb->used = strlen(description);
3202 tb->blank = False;
3203 tb->d.pixmap = WMRetainPixmap(p);
3204 tb->color = WMRetainColor(color);
3205 tb->marginN = newMargin(tPtr, NULL);
3206 tb->allocated = extraInfo;
3207 tb->first = first;
3208 tb->kanji = False;
3209 tb->graphic = True;
3210 tb->object = False;
3211 tb->underlined = False;
3212 tb->selected = False;
3213 tb->script = 0;
3214 tb->sections = NULL;
3215 tb->nsections = 0;
3216 tb->prior = NULL;
3217 tb->next = NULL;
3219 return tb;
3222 void *WMCreateTextBlockWithText(WMText * tPtr, char *text, WMFont * font, WMColor * color,
3223 unsigned short first, unsigned short len)
3225 TextBlock *tb;
3227 if (!font || !color)
3228 return NULL;
3230 tb = wmalloc(sizeof(TextBlock));
3232 tb->allocated = reqBlockSize(len);
3233 tb->text = (char *)wmalloc(tb->allocated);
3234 memset(tb->text, 0, tb->allocated);
3236 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3237 *tb->text = ' ';
3238 tb->used = 1;
3239 tb->blank = True;
3240 } else {
3241 memcpy(tb->text, text, len);
3242 tb->used = len;
3243 tb->blank = False;
3245 tb->text[tb->used] = 0;
3247 tb->d.font = WMRetainFont(font);
3248 tb->color = WMRetainColor(color);
3249 tb->marginN = newMargin(tPtr, NULL);
3250 tb->first = first;
3251 tb->kanji = False;
3252 tb->graphic = False;
3253 tb->underlined = False;
3254 tb->selected = False;
3255 tb->script = 0;
3256 tb->sections = NULL;
3257 tb->nsections = 0;
3258 tb->prior = NULL;
3259 tb->next = NULL;
3260 return tb;
3263 void
3264 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3265 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3267 TextBlock *tb = (TextBlock *) vtb;
3268 if (!tb)
3269 return;
3271 tb->first = first;
3272 tb->kanji = kanji;
3273 tb->underlined = underlined;
3274 tb->script = script;
3275 tb->marginN = newMargin(tPtr, margins);
3278 void
3279 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3280 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins * margins)
3282 TextBlock *tb = (TextBlock *) vtb;
3283 if (!tb)
3284 return;
3286 if (first)
3287 *first = tb->first;
3288 if (kanji)
3289 *kanji = tb->kanji;
3290 if (underlined)
3291 *underlined = tb->underlined;
3292 if (script)
3293 *script = tb->script;
3294 if (margins)
3295 margins = &tPtr->margins[tb->marginN];
3298 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3300 TextBlock *tb = (TextBlock *) vtb;
3302 if (!tb)
3303 return;
3305 if (tb->graphic) {
3306 if (tb->object) {
3307 WMWidget *w = tb->d.widget;
3308 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3309 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3310 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3313 WMAddToArray(tPtr->gfxItems, (void *)tb);
3314 tPtr->tpos = 1;
3316 } else {
3317 tPtr->tpos = tb->used;
3320 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3321 tb->next = tb->prior = NULL;
3322 tb->first = True;
3323 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3324 return;
3327 if (!tb->first) {
3328 tb->marginN = tPtr->currentTextBlock->marginN;
3331 tb->next = tPtr->currentTextBlock;
3332 tb->prior = tPtr->currentTextBlock->prior;
3333 if (tPtr->currentTextBlock->prior)
3334 tPtr->currentTextBlock->prior->next = tb;
3336 tPtr->currentTextBlock->prior = tb;
3337 if (!tb->prior)
3338 tPtr->firstTextBlock = tb;
3340 tPtr->currentTextBlock = tb;
3343 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3345 TextBlock *tb = (TextBlock *) vtb;
3347 if (!tb)
3348 return;
3350 if (tb->graphic) {
3351 if (tb->object) {
3352 WMWidget *w = tb->d.widget;
3353 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3354 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3355 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3358 WMAddToArray(tPtr->gfxItems, (void *)tb);
3359 tPtr->tpos = 1;
3361 } else {
3362 tPtr->tpos = tb->used;
3365 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3366 tb->next = tb->prior = NULL;
3367 tb->first = True;
3368 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3369 return;
3372 if (!tb->first) {
3373 tb->marginN = tPtr->currentTextBlock->marginN;
3376 tb->next = tPtr->currentTextBlock->next;
3377 tb->prior = tPtr->currentTextBlock;
3378 if (tPtr->currentTextBlock->next)
3379 tPtr->currentTextBlock->next->prior = tb;
3381 tPtr->currentTextBlock->next = tb;
3383 if (!tb->next)
3384 tPtr->lastTextBlock = tb;
3386 tPtr->currentTextBlock = tb;
3389 void *WMRemoveTextBlock(WMText * tPtr)
3391 TextBlock *tb = NULL;
3393 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3394 return NULL;
3397 tb = tPtr->currentTextBlock;
3398 if (tb->graphic) {
3399 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3401 if (tb->object) {
3402 WMUnmapWidget(tb->d.widget);
3406 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3407 if (tPtr->currentTextBlock->next)
3408 tPtr->currentTextBlock->next->prior = NULL;
3410 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3411 tPtr->currentTextBlock = tPtr->firstTextBlock;
3413 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3414 tPtr->currentTextBlock->prior->next = NULL;
3415 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3416 tPtr->currentTextBlock = tPtr->lastTextBlock;
3417 } else {
3418 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3419 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3420 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3423 return (void *)tb;
3426 #if 0
3427 static void destroyWidget(WMWidget * widget)
3429 WMDestroyWidget(widget);
3430 // -- never do this -- wfree(widget);
3432 #endif
3434 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3436 TextBlock *tb = (TextBlock *) vtb;
3437 if (!tb)
3438 return;
3440 if (tb->graphic) {
3441 if (tb->object) {
3442 /* naturally, there's a danger to destroying widgets whose action
3443 * brings us here: ie. press a button to destroy it...
3444 * need to find a safer way. till then... this stays commented out */
3445 /* 5 months later... destroy it 10 seconds after now which should
3446 * be enough time for the widget's action to be completed... :-) */
3447 /* This is a bad assumption. Just destroy the widget here.
3448 * if the caller needs it, it can protect it with W_RetainView()
3449 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3450 WMDestroyWidget(tb->d.widget);
3451 } else {
3452 WMReleasePixmap(tb->d.pixmap);
3454 } else {
3455 WMReleaseFont(tb->d.font);
3458 WMReleaseColor(tb->color);
3459 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3460 if (tb->sections)
3461 wfree(tb->sections);
3462 wfree(tb->text);
3463 wfree(tb);
3466 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3468 if (tPtr->fgColor)
3469 WMReleaseColor(tPtr->fgColor);
3471 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3473 paintText(tPtr);
3476 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3478 if (tPtr->bgColor)
3479 WMReleaseColor(tPtr->bgColor);
3481 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3482 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3484 paintText(tPtr);
3487 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3489 if (tPtr->bgPixmap)
3490 WMReleasePixmap(tPtr->bgPixmap);
3492 if (pixmap)
3493 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3494 else
3495 tPtr->bgPixmap = NULL;
3498 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3500 tPtr->flags.relief = relief;
3501 textDidResize(tPtr->view->delegate, tPtr->view);
3504 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3506 if (shouldhave && !tPtr->hS) {
3507 tPtr->hS = WMCreateScroller(tPtr);
3508 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3509 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3510 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3511 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3512 WMMapWidget(tPtr->hS);
3513 } else if (!shouldhave && tPtr->hS) {
3514 WMUnmapWidget(tPtr->hS);
3515 WMDestroyWidget(tPtr->hS);
3516 tPtr->hS = NULL;
3519 tPtr->hpos = 0;
3520 tPtr->prevHpos = 0;
3521 textDidResize(tPtr->view->delegate, tPtr->view);
3524 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3526 if (shouldhave && !tPtr->ruler) {
3527 tPtr->ruler = WMCreateRuler(tPtr);
3528 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3529 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3530 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3531 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3532 } else if (!shouldhave && tPtr->ruler) {
3533 WMShowTextRuler(tPtr, False);
3534 WMDestroyWidget(tPtr->ruler);
3535 tPtr->ruler = NULL;
3537 textDidResize(tPtr->view->delegate, tPtr->view);
3540 void WMShowTextRuler(WMText * tPtr, Bool show)
3542 if (!tPtr->ruler)
3543 return;
3545 if (tPtr->flags.monoFont)
3546 show = False;
3548 tPtr->flags.rulerShown = show;
3549 if (show) {
3550 WMMapWidget(tPtr->ruler);
3551 } else {
3552 WMUnmapWidget(tPtr->ruler);
3555 textDidResize(tPtr->view->delegate, tPtr->view);
3558 Bool WMGetTextRulerShown(WMText * tPtr)
3560 if (!tPtr->ruler)
3561 return False;
3563 return tPtr->flags.rulerShown;
3566 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3568 if (shouldhave && !tPtr->vS) {
3569 tPtr->vS = WMCreateScroller(tPtr);
3570 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3571 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3572 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3573 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3574 WMMapWidget(tPtr->vS);
3575 } else if (!shouldhave && tPtr->vS) {
3576 WMUnmapWidget(tPtr->vS);
3577 WMDestroyWidget(tPtr->vS);
3578 tPtr->vS = NULL;
3581 tPtr->vpos = 0;
3582 tPtr->prevVpos = 0;
3583 textDidResize(tPtr->view->delegate, tPtr->view);
3586 Bool WMScrollText(WMText * tPtr, int amount)
3588 Bool scroll = False;
3590 if (amount == 0 || !tPtr->view->flags.realized)
3591 return False;
3593 if (amount < 0) {
3594 if (tPtr->vpos > 0) {
3595 if (tPtr->vpos > abs(amount))
3596 tPtr->vpos += amount;
3597 else
3598 tPtr->vpos = 0;
3599 scroll = True;
3601 } else {
3602 int limit = tPtr->docHeight - tPtr->visible.h;
3603 if (tPtr->vpos < limit) {
3604 if (tPtr->vpos < limit - amount)
3605 tPtr->vpos += amount;
3606 else
3607 tPtr->vpos = limit;
3608 scroll = True;
3612 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3613 updateScrollers(tPtr);
3614 paintText(tPtr);
3616 tPtr->prevVpos = tPtr->vpos;
3617 return scroll;
3620 Bool WMPageText(WMText * tPtr, Bool direction)
3622 if (!tPtr->view->flags.realized)
3623 return False;
3625 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3628 void WMSetTextEditable(WMText * tPtr, Bool editable)
3630 tPtr->flags.editable = editable;
3633 int WMGetTextEditable(WMText * tPtr)
3635 return tPtr->flags.editable;
3638 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3640 tPtr->flags.indentNewLine = indent;
3643 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3645 tPtr->flags.ignoreNewLine = ignore;
3648 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3650 return tPtr->flags.ignoreNewLine;
3653 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3655 if (mono) {
3656 if (tPtr->flags.rulerShown)
3657 WMShowTextRuler(tPtr, False);
3658 if (tPtr->flags.alignment != WALeft)
3659 tPtr->flags.alignment = WALeft;
3662 tPtr->flags.monoFont = mono;
3663 textDidResize(tPtr->view->delegate, tPtr->view);
3666 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3668 return tPtr->flags.monoFont;
3671 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3673 if (tPtr->dFont)
3674 WMReleaseFont(tPtr->dFont);
3676 if (font) {
3677 tPtr->dFont = WMRetainFont(font);
3678 } else {
3679 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3683 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3685 return WMRetainFont(tPtr->dFont);
3688 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3690 if (tPtr->dColor)
3691 WMReleaseColor(tPtr->dColor);
3693 if (color) {
3694 tPtr->dColor = WMRetainColor(color);
3695 } else {
3696 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3700 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3702 return tPtr->dColor;
3705 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3707 if (tPtr->flags.monoFont)
3708 tPtr->flags.alignment = WALeft;
3709 else
3710 tPtr->flags.alignment = alignment;
3711 WMThawText(tPtr);
3714 int WMGetTextInsertType(WMText * tPtr)
3716 return tPtr->flags.prepend;
3719 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3721 setSelectionProperty(tPtr, NULL, color, -1);
3724 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3726 TextBlock *tb;
3728 tb = tPtr->currentTextBlock;
3730 if (!tb || !tPtr->flags.ownsSelection)
3731 return NULL;
3733 if (!tb->selected)
3734 return NULL;
3736 return tb->color;
3739 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3741 setSelectionProperty(tPtr, font, NULL, -1);
3744 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3746 TextBlock *tb;
3748 tb = tPtr->currentTextBlock;
3750 if (!tb || !tPtr->flags.ownsSelection)
3751 return NULL;
3753 if (!tb->selected)
3754 return NULL;
3756 if (tb->graphic) {
3757 tb = getFirstNonGraphicBlockFor(tb, 1);
3758 if (!tb)
3759 return NULL;
3761 return (tb->selected ? tb->d.font : NULL);
3764 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3766 /* // check this */
3767 if (underlined != 0 && underlined != 1)
3768 return;
3770 setSelectionProperty(tPtr, NULL, NULL, underlined);
3773 int WMGetTextSelectionUnderlined(WMText * tPtr)
3775 TextBlock *tb;
3777 tb = tPtr->currentTextBlock;
3779 if (!tb || !tPtr->flags.ownsSelection)
3780 return 0;
3782 if (!tb->selected)
3783 return 0;
3785 return tb->underlined;
3788 void WMFreezeText(WMText * tPtr)
3790 tPtr->flags.frozen = True;
3793 void WMThawText(WMText * tPtr)
3795 tPtr->flags.frozen = False;
3797 if (tPtr->flags.monoFont) {
3798 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3799 TextBlock *tb;
3801 /* make sure to unmap widgets no matter where they are */
3802 /* they'll be later remapped if needed by paintText */
3803 for (j = 0; j < c; j++) {
3804 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3805 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3806 WMUnmapWidget(tb->d.widget);
3811 tPtr->flags.laidOut = False;
3812 layOutDocument(tPtr);
3813 updateScrollers(tPtr);
3814 paintText(tPtr);
3815 tPtr->flags.needsLayOut = False;
3819 /* find first occurence of a string */
3820 static char *mystrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3822 char *ptr;
3824 if (!haystack || !needle || !end)
3825 return NULL;
3827 for (ptr = haystack; ptr < end; ptr++) {
3828 if (caseSensitive) {
3829 if (*ptr == *needle && !strncmp(ptr, needle, len))
3830 return ptr;
3832 } else {
3833 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3834 return ptr;
3838 return NULL;
3841 /* find last occurence of a string */
3842 static char *mystrrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3844 char *ptr;
3846 if (!haystack || !needle || !end)
3847 return NULL;
3849 for (ptr = haystack - 2; ptr > end; ptr--) {
3850 if (caseSensitive) {
3851 if (*ptr == *needle && !strncmp(ptr, needle, len))
3852 return ptr;
3853 } else {
3854 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3855 return ptr;
3859 return NULL;
3862 Bool WMFindInTextStream(WMText * tPtr, char *needle, Bool direction, Bool caseSensitive)
3864 TextBlock *tb;
3865 char *mark = NULL;
3866 unsigned short pos;
3868 #if 0
3869 if (!(tb = tPtr->currentTextBlock)) {
3870 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3871 return False;
3873 } else {
3874 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3875 tb = (direction>0) ? tb->next : tb->prior; */
3876 if (tb != tPtr->lastTextBlock)
3877 tb = tb->prior;
3879 #endif
3880 tb = tPtr->currentTextBlock;
3881 pos = tPtr->tpos;
3883 while (tb) {
3884 if (!tb->graphic) {
3886 if (direction > 0) {
3887 if (pos + 1 < tb->used)
3888 pos++;
3890 if (tb->used - pos > 0 && pos > 0) {
3891 mark = mystrstr(&tb->text[pos], needle,
3892 strlen(needle), &tb->text[tb->used], caseSensitive);
3894 } else {
3895 tb = tb->next;
3896 pos = 0;
3897 continue;
3900 } else {
3901 if (pos - 1 > 0)
3902 pos--;
3904 if (pos > 0) {
3905 mark = mystrrstr(&tb->text[pos], needle,
3906 strlen(needle), tb->text, caseSensitive);
3907 } else {
3908 tb = tb->prior;
3909 if (!tb)
3910 return False;
3911 pos = tb->used;
3912 continue;
3916 if (mark) {
3917 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3919 tPtr->tpos = (int)(mark - tb->text);
3920 tPtr->currentTextBlock = tb;
3921 updateCursorPosition(tPtr);
3922 tPtr->sel.y = tPtr->cursor.y + 5;
3923 tPtr->sel.h = tPtr->cursor.h - 10;
3924 tPtr->sel.x = tPtr->cursor.x + 1;
3925 tPtr->sel.w = WMIN(WMWidthOfString(font,
3926 &tb->text[tPtr->tpos], strlen(needle)),
3927 tPtr->docWidth - tPtr->sel.x);
3928 tPtr->flags.ownsSelection = True;
3929 paintText(tPtr);
3931 return True;
3935 tb = (direction > 0) ? tb->next : tb->prior;
3936 if (tb) {
3937 pos = (direction > 0) ? 0 : tb->used;
3941 return False;
3944 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3946 if (!tPtr->flags.ownsSelection)
3947 return False;
3949 removeSelection(tPtr);
3951 if (replacement) {
3952 insertTextInteractively(tPtr, replacement, strlen(replacement));
3953 updateCursorPosition(tPtr);
3954 paintText(tPtr);
3957 return True;