WINGs: Small assorted formatting changes
[wmaker-crm.git] / WINGs / wtext.c
blobece4b3982eeeff2c78622993fbba723617eb0465
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
4 #include "WINGsP.h"
5 #include <ctype.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 #define DO_BLINK 0
11 /* TODO:
12 * - verify what happens with XK_return in insertTextInt...
13 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
24 /* a Section is a section of a TextBlock that describes what parts
25 of a TextBlock has been laid out on which "line"...
26 o this greatly aids redraw, scroll and selection.
27 o this is created during layoutLine, but may be later modified.
28 o there may be many Sections per TextBlock, hence the array */
29 typedef struct {
30 unsigned int x, y; /* where to draw it from */
31 unsigned short w, h; /* its width and height */
32 unsigned short begin; /* where the layout begins */
33 unsigned short end; /* where it ends */
34 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
35 unsigned short last:1; /* is it the last section on a "line"? */
36 unsigned int _y:31; /* the "line" it and other textblocks are on */
37 } Section;
39 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
40 o text for the block, color and font
41 o or a pointer to the pixmap
42 o OR a pointer to the widget and the (text) description for its graphic
45 typedef struct _TextBlock {
46 struct _TextBlock *next; /* next text block in linked list */
47 struct _TextBlock *prior; /* prior text block in linked list */
49 char *text; /* pointer to text (could be kanji) */
50 /* or to the object's description */
51 union {
52 WMFont *font; /* the font */
53 WMWidget *widget; /* the embedded widget */
54 WMPixmap *pixmap; /* the pixmap */
55 } d; /* description */
57 unsigned short used; /* number of chars in this block */
58 unsigned short allocated; /* size of allocation (in chars) */
59 WMColor *color; /* the color */
61 Section *sections; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
64 unsigned short s_begin; /* where the selection begins */
65 unsigned short s_end; /* where it ends */
67 unsigned int first:1; /* first TextBlock in paragraph */
68 unsigned int blank:1; /* ie. blank paragraph */
69 unsigned int kanji:1; /* is of 16-bit characters or not */
70 unsigned int graphic:1; /* graphic or text: text=0 */
71 unsigned int object:1; /* embedded object or pixmap */
72 unsigned int underlined:1; /* underlined or not */
73 unsigned int selected:1; /* selected or not */
74 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
75 int script:8; /* script in points: negative for subscript */
76 unsigned int marginN:8; /* which of the margins in the tPtr to use */
77 unsigned int nClicks:2; /* single, double, triple clicks */
78 unsigned int RESERVED:7;
79 } TextBlock;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
82 typedef struct {
83 int y, x, h, w;
84 } myRect;
86 typedef struct W_Text {
87 W_Class widgetClass; /* the class number of this widget */
88 W_View *view; /* the view referring to this instance */
90 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
92 WMScroller *vS; /* the vertical scroller */
93 unsigned int vpos; /* the current vertical position */
94 unsigned int prevVpos; /* the previous vertical position */
96 WMScroller *hS; /* the horizontal scroller */
97 unsigned int hpos; /* the current horizontal position */
98 unsigned int prevHpos; /* the previous horizontal position */
100 WMFont *dFont; /* the default font */
101 WMColor *dColor; /* the default color */
102 WMPixmap *dBulletPix; /* the default pixmap for bullets */
104 WMColor *fgColor; /* The current foreground color */
105 WMColor *bgColor; /* The background color */
107 GC stippledGC; /* the GC to overlay selected graphics with */
108 Pixmap db; /* the buffer on which to draw */
109 WMPixmap *bgPixmap; /* the background pixmap */
111 myRect visible; /* the actual rectangle that can be drawn into */
112 myRect cursor; /* the position and (height) of cursor */
113 myRect sel; /* the selection rectangle */
115 WMPoint clicked; /* where in the _document_ was clicked */
117 unsigned short tpos; /* the position in the currentTextBlock */
118 unsigned short docWidth; /* the width of the entire document */
119 unsigned int docHeight; /* the height of the entire document */
121 TextBlock *firstTextBlock;
122 TextBlock *lastTextBlock;
123 TextBlock *currentTextBlock;
125 WMArray *gfxItems; /* a nice array of graphic items */
127 #if DO_BLINK
128 WMHandlerID timerID; /* for nice twinky-winky */
129 #endif
131 WMAction *parser;
132 WMAction *writer;
133 WMTextDelegate *delegate;
134 Time lastClickTime;
136 WMRulerMargins *margins; /* an array of margins */
138 unsigned int nMargins:7; /* the total number of margins in use */
139 struct {
140 unsigned int monoFont:1; /* whether to ignore formats and graphic */
141 unsigned int focused:1; /* whether this instance has input focus */
142 unsigned int editable:1; /* "silly user, you can't edit me" */
143 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
144 unsigned int pointerGrabbed:1; /* "heh, gib me pointer" */
145 unsigned int extendSelection:1; /* shift-drag to select more regions */
147 unsigned int rulerShown:1; /* whether the ruler is shown or not */
148 unsigned int frozen:1; /* whether screen updates are to be made */
149 unsigned int cursorShown:1; /* whether to show the cursor */
150 unsigned int acceptsGraphic:1; /* accept graphic when dropped */
151 unsigned int horizOnDemand:1; /* if a large image should appear */
152 unsigned int needsLayOut:1; /* in case of Append/Deletes */
153 unsigned int ignoreNewLine:1; /* turn it into a ' ' in streams > 1 */
154 unsigned int indentNewLine:1; /* add " " for a newline typed */
155 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
156 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
157 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
158 WMAlignment alignment:2; /* the alignment for text */
159 WMReliefType relief:3; /* the relief to display with */
160 unsigned int isOverGraphic:2; /* the mouse is over a graphic */
161 unsigned int first:1; /* for plain text parsing, newline? */
162 /* unsigned int RESERVED:1; */
163 } flags;
165 WMArray *xdndSourceTypes;
166 WMArray *xdndDestinationTypes;
167 } Text;
169 #define NOTIFY(T,C,N,A) {\
170 WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
176 #define TYPETEXT 0
178 #if 0
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr, int len)
182 char *s;
184 s = wmalloc(len + 1);
185 memcpy(s, ptr, len);
186 s[len] = 0;
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
188 printf("[%s]\n", s);
189 wfree(s);
191 #endif
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
204 static char *default_bullet[] = {
205 "6 6 4 1",
206 " c None s None",
207 ". c black",
208 "X c white",
209 "o c #808080",
210 " ... ",
211 ".XX.. ",
212 ".XX..o",
213 ".....o",
214 " ...oo",
215 " ooo "
218 static void handleEvents(XEvent * event, void *data);
219 static void layOutDocument(Text * tPtr);
220 static void updateScrollers(Text * tPtr);
222 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
224 unsigned int i = 0;
226 for (i = 0; i < tPtr->nMargins; i++) {
228 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
229 return i;
232 return -1;
235 static int newMargin(Text * tPtr, WMRulerMargins * margins)
237 int n;
239 if (!margins) {
240 tPtr->margins[0].retainCount++;
241 return 0;
244 n = getMarginNumber(tPtr, margins);
246 if (n == -1) {
248 if (tPtr->nMargins >= 127) {
249 n = tPtr->nMargins - 1;
250 return n;
253 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
255 n = tPtr->nMargins - 1;
256 tPtr->margins[n].left = margins->left;
257 tPtr->margins[n].first = margins->first;
258 tPtr->margins[n].body = margins->body;
259 tPtr->margins[n].right = margins->right;
260 /* for each tab... */
261 tPtr->margins[n].retainCount = 1;
262 } else {
263 tPtr->margins[n].retainCount++;
266 return n;
269 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
271 unsigned short i, w, lw, selected = False, extend = False;
272 myRect sel;
274 /* if selection rectangle completely encloses the section */
275 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
276 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
277 sel.x = 0;
278 sel.w = tPtr->visible.w;
279 selected = extend = True;
281 /* or if it starts on a line and then goes further down */
282 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
283 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
284 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y)) {
285 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
286 sel.w = tPtr->visible.w;
287 selected = extend = True;
289 /* or if it begins before a line, but ends on it */
290 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
291 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
292 && (tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
294 if (1 || tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
295 sel.w = tPtr->sel.x + tPtr->sel.w;
296 else
297 sel.w = tPtr->sel.x;
299 sel.x = 0;
300 selected = True;
302 /* or if the selection rectangle lies entirely within a line */
303 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
304 && (tPtr->sel.w >= 2)
305 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
306 sel.x = tPtr->sel.x;
307 sel.w = tPtr->sel.w;
308 selected = True;
311 if (selected) {
312 selected = False;
314 /* if not within (modified) selection rectangle */
315 if (tb->sections[s].x > sel.x + sel.w || tb->sections[s].x + tb->sections[s].w < sel.x)
316 return False;
318 if (tb->graphic) {
319 if (tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w && tb->sections[s].x >= sel.x) {
320 rect->width = tb->sections[s].w;
321 rect->x = tb->sections[s].x;
322 selected = True;
324 } else {
326 i = tb->sections[s].begin;
327 lw = 0;
329 if (0 && tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
334 while (++i <= tb->sections[s].end) {
336 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
337 lw += w;
339 if (lw + tb->sections[s].x >= sel.x || i == tb->sections[s].end) {
340 lw -= w;
341 i--;
342 tb->s_begin = (tb->selected ? WMIN(tb->s_begin, i) : i);
343 break;
347 if (i > tb->sections[s].end) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
349 return False;
352 _selEnd: rect->x = tb->sections[s].x + lw;
353 lw = 0;
354 while (++i <= tb->sections[s].end) {
356 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
357 lw += w;
359 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
366 rect->width = lw;
367 if (tb->sections[s].last && sel.x + sel.w
368 >= tb->sections[s].x + tb->sections[s].w && extend) {
369 rect->width += (tPtr->visible.w - rect->x - lw);
372 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
373 selected = True;
374 break;
380 if (selected) {
381 rect->y = tb->sections[s]._y - tPtr->vpos;
382 rect->height = tb->sections[s].h;
383 if (tb->graphic) {
384 printf("DEBUG: graphic s%d h%d\n", s, tb->sections[s].h);
387 return selected;
391 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
393 TextBlock *tb;
394 int isFont = False;
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
400 if (font && (!color || underlined == -1))
401 isFont = True;
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
409 if (isFont) {
410 if (!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
414 } else if (underlined != -1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
423 TextBlock *midtb, *otb = tb;
425 if (underlined != -1) {
426 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
427 &(tb->text[tb->s_begin]),
428 tb->d.font, tb->color,
429 False,
430 (tb->s_end - tb->s_begin));
431 } else {
432 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
433 &(tb->text[tb->s_begin]),
434 (isFont ? font : tb->d.
435 font),
436 (isFont ? tb->
437 color : color), False,
438 (tb->s_end - tb->s_begin));
441 if (midtb) {
442 if (underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
448 midtb->selected = !True;
449 midtb->s_begin = 0;
450 midtb->s_end = midtb->used;
451 tPtr->currentTextBlock = tb;
452 WMAppendTextBlock(tPtr, midtb);
453 tb = tPtr->currentTextBlock;
456 if (otb->used - otb->s_end > 0) {
457 TextBlock *ntb;
458 ntb = (TextBlock *)
459 WMCreateTextBlockWithText(tPtr,
460 &(otb->text[otb->s_end]), otb->d.font,
461 otb->color, False, otb->used - otb->s_end);
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
475 otb->selected = False;
476 otb->used = otb->s_begin;
480 tb = tb->next;
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
486 /* in case the size changed... */
487 if (isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
490 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
491 tPtr->sel.y = 3 + tb->sections[0]._y;
492 tPtr->sel.h = tb->sections[tb->nsections - 1]._y - tb->sections[0]._y;
493 tPtr->sel.w = tb->sections[tb->nsections - 1].w;
494 if (tb->sections[tb->nsections - 1]._y != tb->sections[0]._y) {
495 tPtr->sel.x = 0;
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
502 static Bool removeSelection(Text * tPtr)
504 TextBlock *tb = NULL;
505 Bool first = False;
507 if (!(tb = tPtr->firstTextBlock))
508 return False;
510 while (tb) {
511 if (tb->selected) {
512 if (!first && !tb->graphic) {
513 WMReleaseFont(tPtr->dFont);
514 tPtr->dFont = WMRetainFont(tb->d.font);
515 first = True;
518 if ((tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
519 tPtr->currentTextBlock = tb;
520 if (tb->next) {
521 tPtr->tpos = 0;
522 } else if (tb->prior) {
523 if (tb->prior->graphic)
524 tPtr->tpos = 1;
525 else
526 tPtr->tpos = tb->prior->used;
527 } else
528 tPtr->tpos = 0;
530 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
531 tb = tPtr->currentTextBlock;
532 continue;
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]), &(tb->text[tb->s_end]), tb->used - tb->s_end);
536 tb->used -= (tb->s_end - tb->s_begin);
537 tb->selected = False;
538 tPtr->tpos = tb->s_begin;
543 tb = tb->next;
545 return True;
548 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
550 TextBlock *hold = tb;
552 if (!tb)
553 return NULL;
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir ? tb->next : tb->prior);
561 if (!tb) {
562 tb = hold;
563 while (tb) {
564 if (!tb->graphic)
565 break;
566 tb = (dir ? tb->prior : tb->next);
570 if (!tb)
571 return NULL;
573 return tb;
576 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
578 if (tPtr->flags.monoFont && tb->graphic) {
579 tb = getFirstNonGraphicBlockFor(tb, *dir);
580 if (!tb)
581 return 0;
583 if (tb->graphic) {
584 tPtr->currentTextBlock = (dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
590 if (!tb->sections) {
591 layOutDocument(tPtr);
592 return 0;
595 *dir = !(y <= tb->sections[0].y);
596 if (*dir) {
597 if ((y <= tb->sections[0]._y + tb->sections[0].h)
598 && (y >= tb->sections[0]._y)) {
599 /* if it's on the same line */
600 if (x < tb->sections[0].x)
601 *dir = 0;
603 } else {
604 if ((y <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
605 && (y >= tb->sections[tb->nsections - 1]._y)) {
606 /* if it's on the same line */
607 if (x > tb->sections[tb->nsections - 1].x)
608 *dir = 1;
612 return 1;
615 static void paintText(Text * tPtr)
617 TextBlock *tb;
618 WMFont *font;
619 char *text;
620 int len, y, c, s, done = False, prev_y = -23, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
631 if (tPtr->bgPixmap) {
632 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
633 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
634 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
643 done = False;
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
649 goto _copy_area;
651 while (tb) {
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
656 if (dir) {
657 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
658 break;
659 } else {
660 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
661 break;
664 _getSibling:
665 if (dir) {
666 if (tb->next)
667 tb = tb->next;
668 else
669 break;
670 } else {
671 if (tb->prior)
672 tb = tb->prior;
673 else
674 break;
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for (s = 0; s < tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
708 if (sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
711 rect.x, rect.y, rect.width, rect.height);
715 prev_y = tb->sections[s]._y;
717 len = tb->sections[s].end - tb->sections[s].begin;
718 text = &(tb->text[tb->sections[s].begin]);
719 y = tb->sections[s].y - tPtr->vpos;
720 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
722 if (!tPtr->flags.monoFont && tb->underlined) {
723 XDrawLine(dpy, tPtr->db, WMColorGC(color),
724 tb->sections[s].x - tPtr->hpos,
725 y + font->y + 1,
726 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
729 tb = (!done ? tb->next : NULL);
732 /* now , show all graphic items that can be viewed */
733 c = WMGetArrayItemCount(tPtr->gfxItems);
734 if (c > 0 && !tPtr->flags.monoFont) {
735 int j, h;
737 for (j = 0; j < c; j++) {
738 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
740 /* if it's not viewable, and mapped, unmap it */
741 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
742 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
744 if (tb->object) {
745 if ((W_VIEW(tb->d.widget))->flags.mapped) {
746 WMUnmapWidget(tb->d.widget);
749 } else {
750 /* if it's viewable, and not mapped, map it */
751 if (tb->object) {
752 W_View *view = W_VIEW(tb->d.widget);
754 if (!view->flags.realized)
755 WMRealizeWidget(tb->d.widget);
756 if (!view->flags.mapped) {
757 XMapWindow(view->screen->display, view->window);
758 XFlush(view->screen->display);
759 view->flags.mapped = 1;
763 if (tb->object) {
764 WMMoveWidget(tb->d.widget,
765 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
766 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
767 h = WMWidgetHeight(tb->d.widget) + 1;
769 } else {
770 WMDrawPixmap(tb->d.pixmap, tPtr->db,
771 tb->sections[0].x - tPtr->hpos,
772 tb->sections[0].y - tPtr->vpos);
773 h = tb->d.pixmap->height + 1;
777 if (tPtr->flags.ownsSelection) {
778 XRectangle rect;
780 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
781 Drawable d = (0 && tb->object ?
782 (WMWidgetView(tb->d.widget))->window : tPtr->db);
784 tb->selected = True;
785 XFillRectangle(dpy, d, tPtr->stippledGC,
786 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
787 rect.x, rect.y, rect.width, rect.height);
791 if (!tPtr->flags.monoFont && tb->underlined) {
792 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
793 tb->sections[0].x - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos,
795 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
796 tb->sections[0].y + h - tPtr->vpos);
802 _copy_area:
803 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
804 int y = tPtr->cursor.y - tPtr->vpos;
805 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
806 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
809 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
810 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
812 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
814 if (tPtr->ruler && tPtr->flags.rulerShown)
815 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
819 static void mouseOverObject(Text * tPtr, int x, int y)
821 TextBlock *tb;
822 Bool result = False;
824 x -= tPtr->visible.x;
825 x += tPtr->hpos;
826 y -= tPtr->visible.y;
827 y += tPtr->vpos;
829 if (tPtr->flags.ownsSelection) {
830 if (tPtr->sel.x <= x
831 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
832 tPtr->flags.isOverGraphic = 1;
833 result = True;
837 if (!result) {
838 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
840 if (c < 1)
841 tPtr->flags.isOverGraphic = 0;
843 for (j = 0; j < c; j++) {
844 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
846 if (!tb || !tb->sections) {
847 tPtr->flags.isOverGraphic = 0;
848 return;
851 if (!tb->object) {
852 if (tb->sections[0].x <= x
853 && tb->sections[0].y <= y
854 && tb->sections[0].x + tb->sections[0].w >= x
855 && tb->sections[0].y + tb->d.pixmap->height >= y) {
856 tPtr->flags.isOverGraphic = 3;
857 result = True;
858 break;
865 if (!result)
866 tPtr->flags.isOverGraphic = 0;
868 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
870 XSetWindowAttributes attribs;
871 attribs.cursor = tPtr->view->attribs.cursor;
872 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
876 #if DO_BLINK
878 static void blinkCursor(void *data)
880 Text *tPtr = (Text *) data;
882 if (tPtr->flags.cursorShown) {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
884 } else {
885 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
887 paintText(tPtr);
888 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
890 #endif
892 static void updateCursorPosition(Text * tPtr)
894 TextBlock *tb = NULL;
895 int x, y, h, s;
897 if (tPtr->flags.needsLayOut)
898 layOutDocument(tPtr);
900 if (!(tb = tPtr->currentTextBlock)) {
901 if (!(tb = tPtr->firstTextBlock)) {
902 WMFont *font = tPtr->dFont;
903 tPtr->tpos = 0;
904 tPtr->cursor.h = font->height + abs(font->height - font->y);
906 tPtr->cursor.y = 2;
907 tPtr->cursor.x = 2;
908 return;
912 if (tb->blank) {
913 tPtr->tpos = 0;
914 y = tb->sections[0].y;
915 h = tb->sections[0].h;
916 x = tb->sections[0].x;
918 } else if (tb->graphic) {
919 y = tb->sections[0].y;
920 h = tb->sections[0].h;
921 x = tb->sections[0].x;
922 if (tPtr->tpos == 1)
923 x += tb->sections[0].w;
925 } else {
926 if (tPtr->tpos > tb->used)
927 tPtr->tpos = tb->used;
929 for (s = 0; s < tb->nsections - 1; s++) {
931 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
932 break;
935 y = tb->sections[s]._y;
936 h = tb->sections[s].h;
937 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
938 &tb->text[tb->sections[s].begin],
939 tPtr->tpos - tb->sections[s].begin);
942 tPtr->cursor.y = y;
943 tPtr->cursor.h = h;
944 tPtr->cursor.x = x;
946 /* scroll the bars if the cursor is not visible */
947 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
948 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
949 tPtr->vpos +=
950 (tPtr->cursor.y + tPtr->cursor.h + 10
951 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
952 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
953 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
958 updateScrollers(tPtr);
961 static void cursorToTextPosition(Text * tPtr, int x, int y)
963 TextBlock *tb = NULL;
964 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
965 char *text;
967 if (tPtr->flags.needsLayOut)
968 layOutDocument(tPtr);
970 y += (tPtr->vpos - tPtr->visible.y);
971 if (y < 0)
972 y = 0;
974 x -= (tPtr->visible.x - 2);
975 if (x < 0)
976 x = 0;
978 /* clicked is relative to document, not window... */
979 tPtr->clicked.x = x;
980 tPtr->clicked.y = y;
982 if (!(tb = tPtr->currentTextBlock)) {
983 if (!(tb = tPtr->firstTextBlock)) {
984 WMFont *font = tPtr->dFont;
985 tPtr->tpos = 0;
986 tPtr->cursor.h = font->height + abs(font->height - font->y);
987 tPtr->cursor.y = 2;
988 tPtr->cursor.x = 2;
989 return;
993 /* first, which direction? Most likely, newly clicked
994 position will be close to previous */
995 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
996 return;
998 s = (dir ? 0 : tb->nsections - 1);
999 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
1000 goto _doneV;
1003 /* get the first (or last) section of the TextBlock that
1004 lies about the vertical click point */
1005 done = False;
1006 while (!done && tb) {
1008 if (tPtr->flags.monoFont && tb->graphic) {
1009 if ((dir ? tb->next : tb->prior))
1010 tb = (dir ? tb->next : tb->prior);
1011 continue;
1014 s = (dir ? 0 : tb->nsections - 1);
1015 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1017 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1018 done = True;
1019 } else {
1020 dir ? s++ : s--;
1024 if (!done) {
1025 if ((dir ? tb->next : tb->prior)) {
1026 tb = (dir ? tb->next : tb->prior);
1027 } else {
1028 pos = tb->used;
1029 break; /* goto _doneH; */
1034 if (s < 0 || s >= tb->nsections) {
1035 s = (dir ? tb->nsections - 1 : 0);
1038 _doneV:
1039 /* we have the line, which TextBlock on that line is it? */
1040 pos = (dir ? 0 : tb->sections[s].begin);
1041 if (tPtr->flags.monoFont && tb->graphic) {
1042 TextBlock *hold = tb;
1043 tb = getFirstNonGraphicBlockFor(hold, dir);
1045 if (!tb) {
1046 tPtr->tpos = 0;
1047 tb = hold;
1048 s = 0;
1049 goto _doNothing;
1053 if (tb->blank)
1054 _w = 0;
1056 _y = tb->sections[s]._y;
1058 while (tb) {
1060 if (tPtr->flags.monoFont && tb->graphic) {
1061 tb = (dir ? tb->next : tb->prior);
1062 continue;
1065 if (dir) {
1066 if (tb->graphic) {
1067 if (tb->object)
1068 _w = WMWidgetWidth(tb->d.widget) - 5;
1069 else
1070 _w = tb->d.pixmap->width - 5;
1072 if (tb->sections[0].x + _w >= x)
1073 break;
1074 } else {
1075 text = &(tb->text[tb->sections[s].begin]);
1076 len = tb->sections[s].end - tb->sections[s].begin;
1077 _w = WMWidthOfString(tb->d.font, text, len);
1078 if (tb->sections[s].x + _w >= x)
1079 break;
1082 } else {
1083 if (tb->sections[s].x <= x)
1084 break;
1087 if ((dir ? tb->next : tb->prior)) {
1088 TextBlock *nxt = (dir ? tb->next : tb->prior);
1089 if (tPtr->flags.monoFont && nxt->graphic) {
1090 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1091 if (!nxt) {
1092 pos = (dir ? 0 : tb->sections[s].begin);
1093 tPtr->cursor.x = tb->sections[s].x;
1094 goto _doneH;
1098 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1099 /* this must be the last/first on this line. stop */
1100 pos = (dir ? tb->sections[s].end : 0);
1101 tPtr->cursor.x = tb->sections[s].x;
1102 if (!tb->blank) {
1103 if (tb->graphic) {
1104 if (tb->object)
1105 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1106 else
1107 tPtr->cursor.x += tb->d.pixmap->width;
1108 } else if (pos > tb->sections[s].begin) {
1109 tPtr->cursor.x +=
1110 WMWidthOfString(tb->d.font,
1111 &(tb->text[tb->sections[s].begin]),
1112 pos - tb->sections[s].begin);
1115 goto _doneH;
1119 if ((dir ? tb->next : tb->prior)) {
1120 tb = (dir ? tb->next : tb->prior);
1121 } else {
1122 done = True;
1123 break;
1126 if (tb)
1127 s = (dir ? 0 : tb->nsections - 1);
1130 /* we have said TextBlock, now where within it? */
1131 if (tb) {
1132 if (tb->graphic) {
1133 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1135 tPtr->cursor.x = tb->sections[0].x;
1137 if (x > tPtr->cursor.x + gw / 2) {
1138 pos = 1;
1139 tPtr->cursor.x += gw;
1140 } else {
1141 printf("first %d\n", tb->first);
1142 if (tb->prior) {
1143 if (tb->prior->graphic)
1144 pos = 1;
1145 else
1146 pos = tb->prior->used;
1147 tb = tb->prior;
1148 } else
1149 pos = 0;
1153 s = 0;
1154 goto _doneH;
1156 } else {
1157 WMFont *f = tb->d.font;
1158 len = tb->sections[s].end - tb->sections[s].begin;
1159 text = &(tb->text[tb->sections[s].begin]);
1161 _w = x - tb->sections[s].x;
1162 pos = 0;
1164 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1165 pos++;
1167 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1169 pos += tb->sections[s].begin;
1173 _doneH:
1174 if (tb->graphic) {
1175 tPtr->tpos = (pos <= 1) ? pos : 0;
1176 } else {
1177 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1179 _doNothing:
1180 if (!tb)
1181 printf("...for this app will surely crash :-)\n");
1183 tPtr->currentTextBlock = tb;
1184 tPtr->cursor.h = tb->sections[s].h;
1185 tPtr->cursor.y = tb->sections[s]._y;
1187 /* scroll the bars if the cursor is not visible */
1188 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1189 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1190 tPtr->vpos +=
1191 (tPtr->cursor.y + tPtr->cursor.h + 10
1192 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1193 updateScrollers(tPtr);
1194 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1195 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1196 updateScrollers(tPtr);
1203 static void updateScrollers(Text * tPtr)
1206 if (tPtr->flags.frozen)
1207 return;
1209 if (tPtr->vS) {
1210 if (tPtr->docHeight <= tPtr->visible.h) {
1211 WMSetScrollerParameters(tPtr->vS, 0, 1);
1212 tPtr->vpos = 0;
1213 } else {
1214 float hmax = (float)(tPtr->docHeight);
1215 WMSetScrollerParameters(tPtr->vS,
1216 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1217 (float)tPtr->visible.h / hmax);
1219 } else
1220 tPtr->vpos = 0;
1222 if (tPtr->hS) {
1223 if (tPtr->docWidth <= tPtr->visible.w) {
1224 WMSetScrollerParameters(tPtr->hS, 0, 1);
1225 tPtr->hpos = 0;
1226 } else {
1227 float wmax = (float)(tPtr->docWidth);
1228 WMSetScrollerParameters(tPtr->hS,
1229 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1230 (float)tPtr->visible.w / wmax);
1232 } else
1233 tPtr->hpos = 0;
1236 static void scrollersCallBack(WMWidget * w, void *self)
1238 Text *tPtr = (Text *) self;
1239 Bool scroll = False;
1240 int which;
1242 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1243 return;
1245 if (w == tPtr->vS) {
1246 int height;
1247 height = tPtr->visible.h;
1249 which = WMGetScrollerHitPart(tPtr->vS);
1250 switch (which) {
1252 case WSDecrementLine:
1253 if (tPtr->vpos > 0) {
1254 if (tPtr->vpos > 16)
1255 tPtr->vpos -= 16;
1256 else
1257 tPtr->vpos = 0;
1258 scroll = True;
1260 break;
1262 case WSIncrementLine:{
1263 int limit = tPtr->docHeight - height;
1264 if (tPtr->vpos < limit) {
1265 if (tPtr->vpos < limit - 16)
1266 tPtr->vpos += 16;
1267 else
1268 tPtr->vpos = limit;
1269 scroll = True;
1272 break;
1274 case WSDecrementPage:
1275 if (((int)tPtr->vpos - (int)height) >= 0)
1276 tPtr->vpos -= height;
1277 else
1278 tPtr->vpos = 0;
1280 scroll = True;
1281 break;
1283 case WSIncrementPage:
1284 tPtr->vpos += height;
1285 if (tPtr->vpos > (tPtr->docHeight - height))
1286 tPtr->vpos = tPtr->docHeight - height;
1287 scroll = True;
1288 break;
1290 case WSKnob:
1291 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1292 * (float)(tPtr->docHeight - height);
1293 scroll = True;
1294 break;
1296 case WSKnobSlot:
1297 case WSNoPart:
1298 break;
1300 scroll = (tPtr->vpos != tPtr->prevVpos);
1301 tPtr->prevVpos = tPtr->vpos;
1304 if (w == tPtr->hS) {
1305 int width = tPtr->visible.w;
1307 which = WMGetScrollerHitPart(tPtr->hS);
1308 switch (which) {
1310 case WSDecrementLine:
1311 if (tPtr->hpos > 0) {
1312 if (tPtr->hpos > 16)
1313 tPtr->hpos -= 16;
1314 else
1315 tPtr->hpos = 0;
1316 scroll = True;
1318 break;
1320 case WSIncrementLine:{
1321 int limit = tPtr->docWidth - width;
1322 if (tPtr->hpos < limit) {
1323 if (tPtr->hpos < limit - 16)
1324 tPtr->hpos += 16;
1325 else
1326 tPtr->hpos = limit;
1327 scroll = True;
1330 break;
1332 case WSDecrementPage:
1333 if (((int)tPtr->hpos - (int)width) >= 0)
1334 tPtr->hpos -= width;
1335 else
1336 tPtr->hpos = 0;
1338 scroll = True;
1339 break;
1341 case WSIncrementPage:
1342 tPtr->hpos += width;
1343 if (tPtr->hpos > (tPtr->docWidth - width))
1344 tPtr->hpos = tPtr->docWidth - width;
1345 scroll = True;
1346 break;
1348 case WSKnob:
1349 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1350 * (float)(tPtr->docWidth - width);
1351 scroll = True;
1352 break;
1354 case WSKnobSlot:
1355 case WSNoPart:
1356 break;
1358 scroll = (tPtr->hpos != tPtr->prevHpos);
1359 tPtr->prevHpos = tPtr->hpos;
1362 if (scroll) {
1363 updateScrollers(tPtr);
1364 paintText(tPtr);
1368 typedef struct {
1369 TextBlock *tb;
1370 unsigned short begin, end; /* what part of the text block */
1371 } myLineItems;
1373 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1375 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1376 WMFont *font;
1377 char *text;
1378 TextBlock *tb, *tbsame = NULL;
1380 if (!items || nitems == 0)
1381 return 0;
1383 for (i = 0; i < nitems; i++) {
1384 tb = items[i].tb;
1386 if (tb->graphic) {
1387 if (!tPtr->flags.monoFont) {
1388 if (tb->object) {
1389 WMWidget *wdt = tb->d.widget;
1390 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1391 if (tPtr->flags.alignment != WALeft)
1392 lw += WMWidgetWidth(wdt);
1393 } else {
1394 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1395 if (tPtr->flags.alignment != WALeft)
1396 lw += tb->d.pixmap->width;
1400 } else {
1401 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1402 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1403 max_d = 2;
1404 line_height = WMAX(line_height, font->height + max_d);
1405 text = &(tb->text[items[i].begin]);
1406 len = items[i].end - items[i].begin;
1407 if (tPtr->flags.alignment != WALeft)
1408 lw += WMWidthOfString(font, text, len);
1412 if (tPtr->flags.alignment == WARight) {
1413 j = tPtr->visible.w - lw;
1414 } else if (tPtr->flags.alignment == WACenter) {
1415 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1418 for (i = 0; i < nitems; i++) {
1419 tb = items[i].tb;
1421 if (tbsame == tb) { /* extend it, since it's on same line */
1422 tb->sections[tb->nsections - 1].end = items[i].end;
1423 n = tb->nsections - 1;
1424 } else {
1425 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1426 n = tb->nsections - 1;
1427 tb->sections[n]._y = y + max_d;
1428 tb->sections[n].max_d = max_d;
1429 tb->sections[n].x = x + j;
1430 tb->sections[n].h = line_height;
1431 tb->sections[n].begin = items[i].begin;
1432 tb->sections[n].end = items[i].end;
1435 tb->sections[n].last = (i + 1 == nitems);
1437 if (tb->graphic) {
1438 if (!tPtr->flags.monoFont) {
1439 if (tb->object) {
1440 WMWidget *wdt = tb->d.widget;
1441 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1442 tb->sections[n].w = WMWidgetWidth(wdt);
1443 } else {
1444 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1445 tb->sections[n].w = tb->d.pixmap->width;
1447 x += tb->sections[n].w;
1449 } else {
1450 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1451 len = items[i].end - items[i].begin;
1452 text = &(tb->text[items[i].begin]);
1454 tb->sections[n].y = y + line_height - font->y;
1455 tb->sections[n].w =
1456 WMWidthOfString(font,
1457 &(tb->text[tb->sections[n].begin]),
1458 tb->sections[n].end - tb->sections[n].begin);
1460 x += WMWidthOfString(font, text, len);
1463 tbsame = tb;
1466 return line_height;
1470 static void layOutDocument(Text * tPtr)
1472 TextBlock *tb;
1473 myLineItems *items = NULL;
1474 unsigned int itemsSize = 0, nitems = 0, begin, end;
1475 WMFont *font;
1476 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1477 char *start = NULL, *mark = NULL;
1479 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1480 return;
1482 assert(tPtr->visible.w > 20);
1484 tPtr->docWidth = tPtr->visible.w;
1485 x = tPtr->margins[tb->marginN].first;
1486 bmargin = tPtr->margins[tb->marginN].body;
1488 /* only partial layOut needed: re-Lay only affected textblocks */
1489 if (tPtr->flags.laidOut) {
1490 tb = tPtr->currentTextBlock;
1492 /* search backwards for textblocks on same line */
1493 while (tb->prior) {
1494 if (!tb->sections || tb->nsections < 1) {
1495 tb = tPtr->firstTextBlock;
1496 tPtr->flags.laidOut = False;
1497 y = 0;
1498 goto _layOut;
1501 if (!tb->prior->sections || tb->prior->nsections < 1) {
1502 tb = tPtr->firstTextBlock;
1503 tPtr->flags.laidOut = False;
1504 y = 0;
1505 goto _layOut;
1508 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1509 break;
1511 tb = tb->prior;
1514 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1515 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1516 tb->prior->sections[tb->prior->nsections - 1].h -
1517 tb->prior->sections[tb->prior->nsections - 1].max_d;
1518 } else {
1519 y = 0;
1523 _layOut:
1524 while (tb) {
1526 if (tb->sections && tb->nsections > 0) {
1527 wfree(tb->sections);
1528 tb->sections = NULL;
1529 tb->nsections = 0;
1532 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1533 TextBlock *next = tb->next;
1534 tPtr->currentTextBlock = tb;
1535 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1536 tb = next;
1537 tb->first = True;
1538 continue;
1541 if (tb->first && tb != tPtr->firstTextBlock) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 x = tPtr->margins[tb->marginN].first;
1544 bmargin = tPtr->margins[tb->marginN].body;
1545 nitems = 0;
1546 lw = 0;
1549 if (tb->graphic) {
1550 if (!tPtr->flags.monoFont) {
1551 if (tb->object)
1552 width = WMWidgetWidth(tb->d.widget);
1553 else
1554 width = tb->d.pixmap->width;
1556 if (width > tPtr->docWidth)
1557 tPtr->docWidth = width;
1559 lw += width;
1560 if (lw >= tPtr->visible.w - x) {
1561 y += layOutLine(tPtr, items, nitems, x, y);
1562 nitems = 0;
1563 x = bmargin;
1564 lw = width;
1567 if (nitems + 1 > itemsSize) {
1568 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1571 items[nitems].tb = tb;
1572 items[nitems].begin = 0;
1573 items[nitems].end = 0;
1574 nitems++;
1577 } else if ((start = tb->text)) {
1578 begin = end = 0;
1579 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1581 while (start) {
1582 mark = strchr(start, ' ');
1583 if (mark) {
1584 end += (int)(mark - start) + 1;
1585 start = mark + 1;
1586 } else {
1587 end += strlen(start);
1588 start = mark;
1591 if (end > tb->used)
1592 end = tb->used;
1594 if (end - begin > 0) {
1596 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1598 /* if it won't fit, char wrap it */
1599 if (width >= tPtr->visible.w) {
1600 char *t = &tb->text[begin];
1601 int l = end - begin, i = 0;
1602 do {
1603 width = WMWidthOfString(font, t, ++i);
1604 } while (width < tPtr->visible.w && i < l);
1605 if (i > 2)
1606 i--;
1607 end = begin + i;
1608 start = &tb->text[end];
1611 lw += width;
1614 if (lw >= tPtr->visible.w - x) {
1615 y += layOutLine(tPtr, items, nitems, x, y);
1616 lw = width;
1617 x = bmargin;
1618 nitems = 0;
1621 if (nitems + 1 > itemsSize) {
1622 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1625 items[nitems].tb = tb;
1626 items[nitems].begin = begin;
1627 items[nitems].end = end;
1628 nitems++;
1630 begin = end;
1634 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1635 if (0 && tPtr->flags.laidOut
1636 && tb->next && tb->next->sections && tb->next->nsections > 0
1637 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1638 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1639 TextBlock *ltb = tPtr->lastTextBlock;
1640 int ly = ltb->sections[ltb->nsections - 1]._y;
1641 int lh = ltb->sections[ltb->nsections - 1].h;
1642 int ss, sd;
1644 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1645 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1647 y += layOutLine(tPtr, items, nitems, x, y);
1648 ss = ly + lh - y;
1649 sd = tPtr->docHeight - y;
1651 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1652 y += tb->next->sections[0]._y - y;
1653 nitems = 0;
1654 printf("nitems%d\n", nitems);
1655 if (ss - sd != 0)
1656 y = tPtr->docHeight + ss - sd;
1658 break;
1659 } else {
1660 tPtr->flags.laidOut = False;
1664 tb = tb->next;
1667 if (nitems > 0)
1668 y += layOutLine(tPtr, items, nitems, x, y);
1670 if (tPtr->docHeight != y + 10) {
1671 tPtr->docHeight = y + 10;
1672 updateScrollers(tPtr);
1675 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1676 XEvent event;
1678 tPtr->flags.horizOnDemand = True;
1679 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1680 event.type = Expose;
1681 handleEvents(&event, (void *)tPtr);
1683 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1684 tPtr->flags.horizOnDemand = False;
1685 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1688 tPtr->flags.laidOut = True;
1690 if (items && itemsSize > 0)
1691 wfree(items);
1695 static void textDidResize(W_ViewDelegate * self, WMView * view)
1697 Text *tPtr = (Text *) view->self;
1698 unsigned short w = tPtr->view->size.width;
1699 unsigned short h = tPtr->view->size.height;
1700 unsigned short rh = 0, vw = 0, rel;
1702 rel = (tPtr->flags.relief == WRFlat);
1704 if (tPtr->ruler && tPtr->flags.rulerShown) {
1705 WMMoveWidget(tPtr->ruler, 2, 2);
1706 WMResizeWidget(tPtr->ruler, w - 4, 40);
1707 rh = 40;
1710 if (tPtr->vS) {
1711 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1712 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1713 vw = 20;
1714 WMSetRulerOffset(tPtr->ruler, 22);
1715 } else
1716 WMSetRulerOffset(tPtr->ruler, 2);
1718 if (tPtr->hS) {
1719 if (tPtr->vS) {
1720 WMMoveWidget(tPtr->hS, vw, h - 21);
1721 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1722 } else {
1723 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1724 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1728 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1729 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1730 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1731 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1732 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1733 tPtr->margins[0].right = tPtr->visible.w;
1735 if (tPtr->view->flags.realized) {
1737 if (tPtr->db) {
1738 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1739 tPtr->db = (Pixmap) NULL;
1742 if (tPtr->visible.w < 40)
1743 tPtr->visible.w = 40;
1744 if (tPtr->visible.h < 20)
1745 tPtr->visible.h = 20;
1747 if (!tPtr->db) {
1748 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1749 tPtr->view->window, tPtr->visible.w,
1750 tPtr->visible.h, tPtr->view->screen->depth);
1754 WMThawText(tPtr);
1757 W_ViewDelegate _TextViewDelegate = {
1758 NULL,
1759 NULL,
1760 textDidResize,
1761 NULL,
1762 NULL
1765 #define TEXT_BUFFER_INCR 8
1766 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1768 static void clearText(Text * tPtr)
1770 tPtr->vpos = tPtr->hpos = 0;
1771 tPtr->docHeight = tPtr->docWidth = 0;
1772 tPtr->cursor.x = -23;
1774 if (!tPtr->firstTextBlock)
1775 return;
1777 while (tPtr->currentTextBlock)
1778 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1780 tPtr->firstTextBlock = NULL;
1781 tPtr->currentTextBlock = NULL;
1782 tPtr->lastTextBlock = NULL;
1783 WMEmptyArray(tPtr->gfxItems);
1786 /* possibly remove a single character from the currentTextBlock,
1787 or if there's a selection, remove it...
1788 note that Delete and Backspace are treated differently */
1789 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1791 TextBlock *tb;
1792 Bool back = (Bool) (ksym == XK_BackSpace);
1793 Bool done = 1, wasFirst = 0;
1795 if (!tPtr->flags.editable)
1796 return;
1798 if (!(tb = tPtr->currentTextBlock))
1799 return;
1801 if (tPtr->flags.ownsSelection) {
1802 if (removeSelection(tPtr))
1803 layOutDocument(tPtr);
1804 return;
1807 wasFirst = tb->first;
1808 if (back && tPtr->tpos < 1) {
1809 if (tb->prior) {
1810 if (tb->prior->blank) {
1811 tPtr->currentTextBlock = tb->prior;
1812 WMRemoveTextBlock(tPtr);
1813 tPtr->currentTextBlock = tb;
1814 tb->first = True;
1815 layOutDocument(tPtr);
1816 return;
1817 } else {
1818 if (tb->blank) {
1819 TextBlock *prior = tb->prior;
1820 tPtr->currentTextBlock = tb;
1821 WMRemoveTextBlock(tPtr);
1822 tb = prior;
1823 } else {
1824 tb = tb->prior;
1827 if (tb->graphic)
1828 tPtr->tpos = 1;
1829 else
1830 tPtr->tpos = tb->used;
1832 tPtr->currentTextBlock = tb;
1833 done = 1;
1834 if (wasFirst) {
1835 if (tb->next)
1836 tb->next->first = False;
1837 layOutDocument(tPtr);
1838 return;
1844 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1845 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1846 if (back)
1847 tPtr->tpos--;
1848 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1849 tb->used--;
1850 done = 0;
1853 /* if there are no characters left to back over in the textblock,
1854 but it still has characters to the right of the cursor: */
1855 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1856 || tb->graphic) {
1858 /* no more chars, and it's marked as blank? */
1859 if (tb->blank) {
1860 TextBlock *sibling = (back ? tb->prior : tb->next);
1862 if (tb->used == 0 || tb->graphic)
1863 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1865 if (sibling) {
1866 tPtr->currentTextBlock = sibling;
1867 if (tb->graphic)
1868 tPtr->tpos = (back ? 1 : 0);
1869 else
1870 tPtr->tpos = (back ? sibling->used : 0);
1872 /* no more chars, so mark it as blank */
1873 } else if (tb->used == 0) {
1874 tb->blank = 1;
1875 } else if (tb->graphic) {
1876 Bool hasNext = (tb->next != NULL);
1878 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1879 if (hasNext) {
1880 tPtr->tpos = 0;
1881 } else if (tPtr->currentTextBlock) {
1882 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1884 } else
1885 printf("DEBUG: unaccounted for... catch this!\n");
1888 layOutDocument(tPtr);
1891 static void insertTextInteractively(Text * tPtr, char *text, int len)
1893 TextBlock *tb;
1894 char *newline = NULL;
1896 if (!tPtr->flags.editable) {
1897 return;
1900 if (len < 1 || !text)
1901 return;
1903 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1904 return;
1906 if (tPtr->flags.ownsSelection)
1907 removeSelection(tPtr);
1909 if (tPtr->flags.ignoreNewLine) {
1910 int i;
1911 for (i = 0; i < len; i++) {
1912 if (text[i] == '\n')
1913 text[i] = ' ';
1917 tb = tPtr->currentTextBlock;
1918 if (!tb || tb->graphic) {
1919 tPtr->tpos = 0;
1920 WMAppendTextStream(tPtr, text);
1921 layOutDocument(tPtr);
1922 return;
1925 if ((newline = strchr(text, '\n'))) {
1926 int nlen = (int)(newline - text);
1927 int s = tb->used - tPtr->tpos;
1929 if (!tb->blank && nlen > 0) {
1930 char *save = NULL;
1932 if (s > 0) {
1933 save = wmalloc(s);
1934 memcpy(save, &tb->text[tPtr->tpos], s);
1935 tb->used -= (tb->used - tPtr->tpos);
1937 insertTextInteractively(tPtr, text, nlen);
1938 newline++;
1939 WMAppendTextStream(tPtr, newline);
1940 if (s > 0) {
1941 insertTextInteractively(tPtr, save, s);
1942 wfree(save);
1944 } else {
1945 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1947 unsigned short savePos = tPtr->tpos;
1948 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1949 tb->d.font, tb->color, True,
1950 tb->used - tPtr->tpos);
1952 if (tb->sections[0].end == tPtr->tpos)
1953 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1954 NULL, tb->d.font,
1955 tb->color, True, 0));
1957 tb->used = savePos;
1958 WMAppendTextBlock(tPtr, ntb);
1959 tPtr->tpos = 0;
1961 } else if (tPtr->tpos == tb->used) {
1962 if (tPtr->flags.indentNewLine) {
1963 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1964 " ", tb->d.font,
1965 tb->color, True, 4));
1966 tPtr->tpos = 4;
1967 } else {
1968 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1969 NULL, tb->d.font,
1970 tb->color, True, 0));
1971 tPtr->tpos = 0;
1973 } else if (tPtr->tpos == 0) {
1974 if (tPtr->flags.indentNewLine) {
1975 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1976 " ", tb->d.font,
1977 tb->color, True, 4));
1978 } else {
1979 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1980 NULL, tb->d.font,
1981 tb->color, True, 0));
1983 tPtr->tpos = 0;
1984 if (tPtr->currentTextBlock->next)
1985 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1988 } else {
1989 if (tb->used + len >= tb->allocated) {
1990 tb->allocated = reqBlockSize(tb->used + len);
1991 tb->text = wrealloc(tb->text, tb->allocated);
1994 if (tb->blank) {
1995 memcpy(tb->text, text, len);
1996 tb->used = len;
1997 tPtr->tpos = len;
1998 tb->text[tb->used] = 0;
1999 tb->blank = False;
2001 } else {
2002 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
2003 memmove(&tb->text[tPtr->tpos], text, len);
2004 tb->used += len;
2005 tPtr->tpos += len;
2006 tb->text[tb->used] = 0;
2011 layOutDocument(tPtr);
2014 static void selectRegion(Text * tPtr, int x, int y)
2017 if (x < 0 || y < 0)
2018 return;
2020 y += (tPtr->flags.rulerShown ? 40 : 0);
2021 y += tPtr->vpos;
2022 if (y > 10)
2023 y -= 10; /* the original offset */
2025 x -= tPtr->visible.x - 2;
2026 if (x < 0)
2027 x = 0;
2029 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2030 tPtr->sel.w = abs(tPtr->clicked.x - x);
2031 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2032 tPtr->sel.h = abs(tPtr->clicked.y - y);
2034 tPtr->flags.ownsSelection = True;
2035 paintText(tPtr);
2038 static void releaseSelection(Text * tPtr)
2040 TextBlock *tb = tPtr->firstTextBlock;
2042 while (tb) {
2043 tb->selected = False;
2044 tb = tb->next;
2046 tPtr->flags.ownsSelection = False;
2047 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2049 paintText(tPtr);
2052 WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2054 Text *tPtr = view->self;
2055 Display *dpy = tPtr->view->screen->display;
2056 Atom _TARGETS;
2057 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2058 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2059 WMData *data = NULL;
2061 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2062 char *text = WMGetTextSelectedStream(tPtr);
2064 if (text) {
2065 data = WMCreateDataWithBytes(text, strlen(text));
2066 WMSetDataFormat(data, TYPETEXT);
2068 *type = target;
2069 return data;
2070 } else
2071 printf("didn't get it\n");
2073 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2074 if (target == _TARGETS) {
2075 Atom *ptr;
2077 ptr = wmalloc(4 * sizeof(Atom));
2078 ptr[0] = _TARGETS;
2079 ptr[1] = XA_STRING;
2080 ptr[2] = TEXT;
2081 ptr[3] = COMPOUND_TEXT;
2083 data = WMCreateDataWithBytes(ptr, 4 * 4);
2084 WMSetDataFormat(data, 32);
2086 *type = target;
2087 return data;
2090 return NULL;
2093 static void lostHandler(WMView * view, Atom selection, void *cdata)
2095 releaseSelection((WMText *) view->self);
2098 static WMSelectionProcs selectionHandler = {
2099 requestHandler, lostHandler, NULL
2102 static void ownershipObserver(void *observerData, WMNotification * notification)
2104 if (observerData != WMGetNotificationClientData(notification))
2105 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2108 static void autoSelectText(Text * tPtr, int clicks)
2110 int x, start;
2111 TextBlock *tb;
2112 char *mark = NULL, behind, ahead;
2114 if (!(tb = tPtr->currentTextBlock))
2115 return;
2117 if (clicks == 2) {
2119 switch (tb->text[tPtr->tpos]) {
2120 case ' ':
2121 return;
2123 case '<': case '>': behind = '<'; ahead = '>'; break;
2124 case '{': case '}': behind = '{'; ahead = '}'; break;
2125 case '[': case ']': behind = '['; ahead = ']'; break;
2127 default:
2128 behind = ahead = ' ';
2131 tPtr->sel.y = tPtr->cursor.y + 5;
2132 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2134 if (tb->graphic) {
2135 tPtr->sel.x = tb->sections[0].x;
2136 tPtr->sel.w = tb->sections[0].w;
2137 } else {
2138 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2140 start = tPtr->tpos;
2141 while (start > 0 && tb->text[start - 1] != behind)
2142 start--;
2144 x = tPtr->cursor.x;
2145 if (tPtr->tpos > start) {
2146 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2148 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2150 if ((mark = strchr(&tb->text[start], ahead))) {
2151 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2152 (int)(mark - &tb->text[start]));
2153 } else if (tb->used > start) {
2154 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2158 } else if (clicks == 3) {
2159 TextBlock *cur = tb;
2161 while (tb && !tb->first) {
2162 tb = tb->prior;
2164 tPtr->sel.y = tb->sections[0]._y;
2166 tb = cur;
2167 while (tb->next && !tb->next->first) {
2168 tb = tb->next;
2170 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2172 tPtr->sel.x = 0;
2173 tPtr->sel.w = tPtr->docWidth;
2174 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2177 if (!tPtr->flags.ownsSelection) {
2178 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2179 tPtr->flags.ownsSelection = True;
2181 paintText(tPtr);
2185 # if 0
2186 static void fontChanged(void *observerData, WMNotification * notification)
2188 WMText *tPtr = (WMText *) observerData;
2189 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2190 printf("fontChanged\n");
2192 if (!tPtr || !font)
2193 return;
2195 if (tPtr->flags.ownsSelection)
2196 WMSetTextSelectionFont(tPtr, font);
2198 #endif
2200 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2202 char buffer[64];
2203 KeySym ksym;
2204 int control_pressed = False;
2205 TextBlock *tb = NULL;
2207 if (((XKeyEvent *) event)->state & ControlMask)
2208 control_pressed = True;
2209 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2211 switch (ksym) {
2213 case XK_Home:
2214 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2215 tPtr->tpos = 0;
2216 updateCursorPosition(tPtr);
2217 paintText(tPtr);
2218 break;
2220 case XK_End:
2221 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2222 if (tPtr->currentTextBlock->graphic)
2223 tPtr->tpos = 1;
2224 else
2225 tPtr->tpos = tPtr->currentTextBlock->used;
2227 updateCursorPosition(tPtr);
2228 paintText(tPtr);
2229 break;
2231 case XK_Left:
2232 if (!(tb = tPtr->currentTextBlock))
2233 break;
2234 if (tb->graphic)
2235 goto L_imaGFX;
2237 if (tPtr->tpos == 0) {
2238 L_imaGFX:
2239 if (tb->prior) {
2240 tPtr->currentTextBlock = tb->prior;
2241 if (tPtr->currentTextBlock->graphic)
2242 tPtr->tpos = 1;
2243 else
2244 tPtr->tpos = tPtr->currentTextBlock->used;
2246 if (!tb->first && tPtr->tpos > 0)
2247 tPtr->tpos--;
2248 } else
2249 tPtr->tpos = 0;
2250 } else
2251 tPtr->tpos--;
2252 updateCursorPosition(tPtr);
2253 paintText(tPtr);
2254 break;
2256 case XK_Right:
2257 if (!(tb = tPtr->currentTextBlock))
2258 break;
2259 if (tb->graphic)
2260 goto R_imaGFX;
2261 if (tPtr->tpos == tb->used) {
2262 R_imaGFX:
2263 if (tb->next) {
2264 tPtr->currentTextBlock = tb->next;
2265 tPtr->tpos = 0;
2266 if (!tb->next->first && tb->next->used > 0)
2267 tPtr->tpos++;
2268 } else {
2269 if (tb->graphic)
2270 tPtr->tpos = 1;
2271 else
2272 tPtr->tpos = tb->used;
2274 } else
2275 tPtr->tpos++;
2276 updateCursorPosition(tPtr);
2277 paintText(tPtr);
2278 break;
2280 case XK_Down:
2281 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2282 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2283 paintText(tPtr);
2284 break;
2286 case XK_Up:
2287 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2288 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2289 paintText(tPtr);
2290 break;
2292 case XK_BackSpace:
2293 case XK_Delete:
2294 #ifdef XK_KP_Delete
2295 case XK_KP_Delete:
2296 #endif
2297 deleteTextInteractively(tPtr, ksym);
2298 updateCursorPosition(tPtr);
2299 paintText(tPtr);
2300 break;
2302 case XK_Control_R:
2303 case XK_Control_L:
2304 control_pressed = True;
2305 break;
2307 case XK_Tab:
2308 insertTextInteractively(tPtr, " ", 4);
2309 updateCursorPosition(tPtr);
2310 paintText(tPtr);
2311 break;
2313 case XK_Return:
2314 *buffer = '\n';
2315 default:
2316 if (*buffer != 0 && !control_pressed) {
2317 insertTextInteractively(tPtr, buffer, strlen(buffer));
2318 updateCursorPosition(tPtr);
2319 paintText(tPtr);
2321 } else if (control_pressed && ksym == XK_r) {
2322 Bool i = !tPtr->flags.rulerShown;
2323 WMShowTextRuler(tPtr, i);
2324 tPtr->flags.rulerShown = i;
2325 } else if (control_pressed && *buffer == '\a') {
2326 XBell(tPtr->view->screen->display, 0);
2327 } else {
2328 WMRelayToNextResponder(tPtr->view, event);
2332 if (!control_pressed && tPtr->flags.ownsSelection) {
2333 releaseSelection(tPtr);
2337 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2339 Text *tPtr = (Text *) view->self;
2340 char *text;
2342 tPtr->flags.waitingForSelection = 0;
2344 if (data) {
2345 text = (char *)WMDataBytes(data);
2347 if (tPtr->parser) {
2348 (tPtr->parser) (tPtr, (void *)text);
2349 layOutDocument(tPtr);
2350 } else
2351 insertTextInteractively(tPtr, text, strlen(text));
2352 updateCursorPosition(tPtr);
2353 paintText(tPtr);
2355 } else {
2356 int n;
2358 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2360 if (text) {
2361 text[n] = 0;
2362 if (tPtr->parser) {
2363 (tPtr->parser) (tPtr, (void *)text);
2364 layOutDocument(tPtr);
2365 } else
2366 insertTextInteractively(tPtr, text, n);
2367 updateCursorPosition(tPtr);
2368 paintText(tPtr);
2370 XFree(text);
2376 static void handleActionEvents(XEvent * event, void *data)
2378 Text *tPtr = (Text *) data;
2379 Display *dpy = event->xany.display;
2380 KeySym ksym;
2382 switch (event->type) {
2383 case KeyPress:
2384 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2385 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2386 tPtr->flags.extendSelection = True;
2387 return;
2390 if (tPtr->flags.focused) {
2391 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2392 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2393 GrabModeAsync, GrabModeAsync, None,
2394 tPtr->view->screen->invisibleCursor, CurrentTime);
2395 tPtr->flags.pointerGrabbed = True;
2396 handleTextKeyPress(tPtr, event);
2399 break;
2401 case KeyRelease:
2402 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2403 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2404 tPtr->flags.extendSelection = False;
2405 return;
2406 /* end modify flag so selection can be extended */
2408 break;
2410 case MotionNotify:
2412 if (tPtr->flags.pointerGrabbed) {
2413 tPtr->flags.pointerGrabbed = False;
2414 XUngrabPointer(dpy, CurrentTime);
2417 if (tPtr->flags.waitingForSelection)
2418 break;
2420 if ((event->xmotion.state & Button1Mask)) {
2422 if (WMIsDraggingFromView(tPtr->view)) {
2423 WMDragImageFromView(tPtr->view, event);
2424 break;
2427 if (!tPtr->flags.ownsSelection) {
2428 WMCreateSelectionHandler(tPtr->view,
2429 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2430 tPtr->flags.ownsSelection = True;
2432 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2433 break;
2436 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2437 break;
2439 case ButtonPress:
2441 if (tPtr->flags.pointerGrabbed) {
2442 tPtr->flags.pointerGrabbed = False;
2443 XUngrabPointer(dpy, CurrentTime);
2444 break;
2447 if (tPtr->flags.waitingForSelection)
2448 break;
2450 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2451 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2452 return;
2455 if (tPtr->flags.ownsSelection)
2456 releaseSelection(tPtr);
2458 if (event->xbutton.button == Button1) {
2459 TextBlock *tb = tPtr->currentTextBlock;
2461 if (WMIsDoubleClick(event)) {
2463 tPtr->lastClickTime = event->xbutton.time;
2464 if (tb && tb->graphic && !tb->object) {
2465 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2466 char *desc;
2468 desc = wmalloc(tb->used + 1);
2469 memcpy(desc, tb->text, tb->used);
2470 desc[tb->used] = 0;
2471 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2472 wfree(desc);
2474 } else {
2475 autoSelectText(tPtr, 2);
2477 break;
2478 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2479 tPtr->lastClickTime = event->xbutton.time;
2480 autoSelectText(tPtr, 3);
2481 break;
2484 if (!tPtr->flags.focused) {
2485 WMSetFocusToWidget(tPtr);
2486 tPtr->flags.focused = True;
2487 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2489 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2490 WMDragImageFromView(tPtr->view, event);
2491 break;
2494 tPtr->lastClickTime = event->xbutton.time;
2495 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2496 paintText(tPtr);
2499 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2500 WMScrollText(tPtr, 16);
2501 break;
2504 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2505 WMScrollText(tPtr, -16);
2506 break;
2509 if (event->xbutton.button == Button2) {
2510 char *text = NULL;
2511 int n;
2513 if (!tPtr->flags.editable) {
2514 XBell(dpy, 0);
2515 break;
2518 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2519 event->xbutton.time, pasteText, NULL)) {
2521 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2522 tPtr->flags.waitingForSelection = 0;
2524 if (text) {
2525 text[n] = 0;
2527 if (tPtr->parser) {
2528 (tPtr->parser) (tPtr, (void *)text);
2529 layOutDocument(tPtr);
2530 } else
2531 insertTextInteractively(tPtr, text, n);
2533 XFree(text);
2534 #if 0
2535 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2536 (void *)WMInsertTextEvent);
2537 #endif
2538 updateCursorPosition(tPtr);
2539 paintText(tPtr);
2541 } else {
2542 tPtr->flags.waitingForSelection = True;
2545 break;
2548 case ButtonRelease:
2549 if (tPtr->flags.pointerGrabbed) {
2550 tPtr->flags.pointerGrabbed = False;
2551 XUngrabPointer(dpy, CurrentTime);
2552 break;
2555 if (tPtr->flags.waitingForSelection)
2556 break;
2558 if (WMIsDraggingFromView(tPtr->view))
2559 WMDragImageFromView(tPtr->view, event);
2564 static void handleEvents(XEvent * event, void *data)
2566 Text *tPtr = (Text *) data;
2568 switch (event->type) {
2569 case Expose:
2571 if (event->xexpose.count != 0)
2572 break;
2574 if (tPtr->hS) {
2575 if (!(W_VIEW(tPtr->hS))->flags.realized)
2576 WMRealizeWidget(tPtr->hS);
2579 if (tPtr->vS) {
2580 if (!(W_VIEW(tPtr->vS))->flags.realized)
2581 WMRealizeWidget(tPtr->vS);
2584 if (tPtr->ruler) {
2585 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2586 WMRealizeWidget(tPtr->ruler);
2590 if (!tPtr->db)
2591 textDidResize(tPtr->view->delegate, tPtr->view);
2593 paintText(tPtr);
2594 break;
2596 case FocusIn:
2597 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2598 != tPtr->view)
2599 return;
2600 tPtr->flags.focused = True;
2601 #if DO_BLINK
2602 if (tPtr->flags.editable && !tPtr->timerID) {
2603 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2605 #endif
2607 break;
2609 case FocusOut:
2610 tPtr->flags.focused = False;
2611 paintText(tPtr);
2612 #if DO_BLINK
2613 if (tPtr->timerID) {
2614 WMDeleteTimerHandler(tPtr->timerID);
2615 tPtr->timerID = NULL;
2617 #endif
2618 break;
2620 case DestroyNotify:
2621 clearText(tPtr);
2622 if (tPtr->db)
2623 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2624 if (tPtr->gfxItems)
2625 WMEmptyArray(tPtr->gfxItems);
2626 #if DO_BLINK
2627 if (tPtr->timerID)
2628 WMDeleteTimerHandler(tPtr->timerID);
2629 #endif
2630 WMReleaseFont(tPtr->dFont);
2631 WMReleaseColor(tPtr->dColor);
2632 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2633 WMRemoveNotificationObserver(tPtr);
2635 WMFreeArray(tPtr->xdndSourceTypes);
2636 WMFreeArray(tPtr->xdndDestinationTypes);
2638 wfree(tPtr);
2640 break;
2645 static void insertPlainText(Text * tPtr, char *text)
2647 char *start, *mark;
2648 void *tb = NULL;
2650 start = text;
2651 while (start) {
2652 mark = strchr(start, '\n');
2653 if (mark) {
2654 tb = WMCreateTextBlockWithText(tPtr,
2655 start, tPtr->dFont,
2656 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2657 start = mark + 1;
2658 tPtr->flags.first = True;
2659 } else {
2660 if (start && strlen(start)) {
2661 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2662 tPtr->dColor, tPtr->flags.first, strlen(start));
2663 } else
2664 tb = NULL;
2665 tPtr->flags.first = False;
2666 start = mark;
2669 if (tPtr->flags.prepend)
2670 WMPrependTextBlock(tPtr, tb);
2671 else
2672 WMAppendTextBlock(tPtr, tb);
2676 static void rulerMoveCallBack(WMWidget * w, void *self)
2678 Text *tPtr = (Text *) self;
2680 if (!tPtr)
2681 return;
2682 if (W_CLASS(tPtr) != WC_Text)
2683 return;
2685 paintText(tPtr);
2688 static void rulerReleaseCallBack(WMWidget * w, void *self)
2690 Text *tPtr = (Text *) self;
2692 if (!tPtr)
2693 return;
2694 if (W_CLASS(tPtr) != WC_Text)
2695 return;
2697 WMThawText(tPtr);
2698 return;
2701 static WMArray *dropDataTypes(WMView * self)
2703 return ((Text *) self->self)->xdndSourceTypes;
2706 static WMDragOperationType wantedDropOperation(WMView * self)
2708 return WDOperationCopy;
2711 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2713 return (allowedOperation == WDOperationCopy);
2716 static WMData *fetchDragData(WMView * self, char *type)
2718 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2719 char *desc;
2720 WMData *data;
2722 if (strcmp(type, "text/plain")) {
2723 if (!tb)
2724 return NULL;
2726 desc = wmalloc(tb->used + 1);
2727 memcpy(desc, tb->text, tb->used);
2728 desc[tb->used] = 0;
2729 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2731 wfree(desc);
2733 return data;
2736 return NULL;
2739 static WMDragSourceProcs _DragSourceProcs = {
2740 dropDataTypes,
2741 wantedDropOperation,
2742 NULL,
2743 acceptDropOperation,
2744 NULL,
2745 NULL,
2746 fetchDragData
2749 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2751 return ((Text *) self->self)->xdndDestinationTypes;
2754 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2756 return WDOperationCopy;
2759 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2761 WMText *tPtr = (WMText *) self->self;
2762 WMData *data;
2763 char *colorName;
2764 WMColor *color;
2766 if (tPtr) {
2768 /* only one required type, implies only one drop data */
2770 /* get application/X-color if any */
2771 data = (WMData *) WMPopFromArray(dropData);
2772 if (data != NULL) {
2773 colorName = (char *)WMDataBytes(data);
2774 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2776 if (color) {
2777 WMSetTextSelectionColor(tPtr, color);
2778 WMReleaseColor(color);
2784 static WMDragDestinationProcs _DragDestinationProcs = {
2785 NULL,
2786 requiredDataTypes,
2787 allowedOperation,
2788 NULL,
2789 performDragOperation,
2790 NULL
2793 char *getStream(WMText * tPtr, int sel, int array)
2795 TextBlock *tb = NULL;
2796 char *text = NULL;
2797 unsigned long where = 0;
2799 if (!tPtr)
2800 return NULL;
2802 if (!(tb = tPtr->firstTextBlock))
2803 return NULL;
2805 if (tPtr->writer) {
2806 (tPtr->writer) (tPtr, (void *)text);
2807 return text;
2810 tb = tPtr->firstTextBlock;
2811 while (tb) {
2813 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2815 if (!sel || (tb->graphic && tb->selected)) {
2817 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2818 && tb != tPtr->firstTextBlock) {
2819 text = wrealloc(text, where + 1);
2820 text[where++] = '\n';
2823 if (tb->blank)
2824 goto _gSnext;
2826 if (tb->graphic && array) {
2827 text = wrealloc(text, where + 4);
2828 text[where++] = 0xFA;
2829 text[where++] = (tb->used >> 8) & 0x0ff;
2830 text[where++] = tb->used & 0x0ff;
2831 text[where++] = tb->allocated; /* extra info */
2833 text = wrealloc(text, where + tb->used);
2834 memcpy(&text[where], tb->text, tb->used);
2835 where += tb->used;
2837 } else if (sel && tb->selected) {
2839 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2840 text = wrealloc(text, where + 1);
2841 text[where++] = '\n';
2844 if (tb->blank)
2845 goto _gSnext;
2847 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2848 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2849 where += tb->s_end - tb->s_begin;
2854 _gSnext: tb = tb->next;
2857 /* +1 for the end of string, let's be nice */
2858 text = wrealloc(text, where + 1);
2859 text[where] = 0;
2860 return text;
2863 static void releaseStreamObjects(void *data)
2865 if (data)
2866 wfree(data);
2869 WMArray *getStreamObjects(WMText * tPtr, int sel)
2871 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2872 WMData *data;
2873 char *stream;
2874 unsigned short len;
2875 char *start, *fa, *desc;
2877 stream = getStream(tPtr, sel, 1);
2878 if (!stream)
2879 return NULL;
2881 start = stream;
2882 while (start) {
2884 fa = strchr(start, 0xFA);
2885 if (fa) {
2886 if ((int)(fa - start) > 0) {
2887 desc = start;
2888 desc[(int)(fa - start)] = 0;
2889 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2890 WMSetDataFormat(data, TYPETEXT);
2891 WMAddToArray(array, (void *)data);
2894 len = *(fa + 1) * 0xff + *(fa + 2);
2895 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2896 WMSetDataFormat(data, *(fa + 3));
2897 WMAddToArray(array, (void *)data);
2898 start = fa + len + 4;
2900 } else {
2901 if (start && strlen(start)) {
2902 data = WMCreateDataWithBytes((void *)start, strlen(start));
2903 WMSetDataFormat(data, TYPETEXT);
2904 WMAddToArray(array, (void *)data);
2906 start = fa;
2910 wfree(stream);
2911 return array;
2914 #define XDND_TEXT_DATA_TYPE "text/plain"
2915 #define XDND_COLOR_DATA_TYPE "application/X-color"
2916 static WMArray *getXdndSourceTypeArray()
2918 WMArray *types = WMCreateArray(1);
2919 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2920 return types;
2923 static WMArray *getXdndDestinationTypeArray()
2925 WMArray *types = WMCreateArray(1);
2926 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2927 return types;
2930 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2932 Text *tPtr;
2933 Display *dpy;
2934 WMScreen *scr;
2935 XGCValues gcv;
2937 tPtr = wmalloc(sizeof(Text));
2938 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);
3235 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3236 *tb->text = ' ';
3237 tb->used = 1;
3238 tb->blank = True;
3239 } else {
3240 memcpy(tb->text, text, len);
3241 tb->used = len;
3242 tb->blank = False;
3244 tb->text[tb->used] = 0;
3246 tb->d.font = WMRetainFont(font);
3247 tb->color = WMRetainColor(color);
3248 tb->marginN = newMargin(tPtr, NULL);
3249 tb->first = first;
3250 tb->kanji = False;
3251 tb->graphic = False;
3252 tb->underlined = False;
3253 tb->selected = False;
3254 tb->script = 0;
3255 tb->sections = NULL;
3256 tb->nsections = 0;
3257 tb->prior = NULL;
3258 tb->next = NULL;
3259 return tb;
3262 void
3263 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3264 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3266 TextBlock *tb = (TextBlock *) vtb;
3267 if (!tb)
3268 return;
3270 tb->first = first;
3271 tb->kanji = kanji;
3272 tb->underlined = underlined;
3273 tb->script = script;
3274 tb->marginN = newMargin(tPtr, margins);
3277 void
3278 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3279 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins * margins)
3281 TextBlock *tb = (TextBlock *) vtb;
3282 if (!tb)
3283 return;
3285 if (first)
3286 *first = tb->first;
3287 if (kanji)
3288 *kanji = tb->kanji;
3289 if (underlined)
3290 *underlined = tb->underlined;
3291 if (script)
3292 *script = tb->script;
3293 if (margins)
3294 margins = &tPtr->margins[tb->marginN];
3297 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3299 TextBlock *tb = (TextBlock *) vtb;
3301 if (!tb)
3302 return;
3304 if (tb->graphic) {
3305 if (tb->object) {
3306 WMWidget *w = tb->d.widget;
3307 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3308 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3309 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3312 WMAddToArray(tPtr->gfxItems, (void *)tb);
3313 tPtr->tpos = 1;
3315 } else {
3316 tPtr->tpos = tb->used;
3319 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3320 tb->next = tb->prior = NULL;
3321 tb->first = True;
3322 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3323 return;
3326 if (!tb->first) {
3327 tb->marginN = tPtr->currentTextBlock->marginN;
3330 tb->next = tPtr->currentTextBlock;
3331 tb->prior = tPtr->currentTextBlock->prior;
3332 if (tPtr->currentTextBlock->prior)
3333 tPtr->currentTextBlock->prior->next = tb;
3335 tPtr->currentTextBlock->prior = tb;
3336 if (!tb->prior)
3337 tPtr->firstTextBlock = tb;
3339 tPtr->currentTextBlock = tb;
3342 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3344 TextBlock *tb = (TextBlock *) vtb;
3346 if (!tb)
3347 return;
3349 if (tb->graphic) {
3350 if (tb->object) {
3351 WMWidget *w = tb->d.widget;
3352 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3353 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3354 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3357 WMAddToArray(tPtr->gfxItems, (void *)tb);
3358 tPtr->tpos = 1;
3360 } else {
3361 tPtr->tpos = tb->used;
3364 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3365 tb->next = tb->prior = NULL;
3366 tb->first = True;
3367 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3368 return;
3371 if (!tb->first) {
3372 tb->marginN = tPtr->currentTextBlock->marginN;
3375 tb->next = tPtr->currentTextBlock->next;
3376 tb->prior = tPtr->currentTextBlock;
3377 if (tPtr->currentTextBlock->next)
3378 tPtr->currentTextBlock->next->prior = tb;
3380 tPtr->currentTextBlock->next = tb;
3382 if (!tb->next)
3383 tPtr->lastTextBlock = tb;
3385 tPtr->currentTextBlock = tb;
3388 void *WMRemoveTextBlock(WMText * tPtr)
3390 TextBlock *tb = NULL;
3392 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3393 return NULL;
3396 tb = tPtr->currentTextBlock;
3397 if (tb->graphic) {
3398 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3400 if (tb->object) {
3401 WMUnmapWidget(tb->d.widget);
3405 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3406 if (tPtr->currentTextBlock->next)
3407 tPtr->currentTextBlock->next->prior = NULL;
3409 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3410 tPtr->currentTextBlock = tPtr->firstTextBlock;
3412 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3413 tPtr->currentTextBlock->prior->next = NULL;
3414 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3415 tPtr->currentTextBlock = tPtr->lastTextBlock;
3416 } else {
3417 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3418 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3419 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3422 return (void *)tb;
3425 #if 0
3426 static void destroyWidget(WMWidget * widget)
3428 WMDestroyWidget(widget);
3429 // -- never do this -- wfree(widget);
3431 #endif
3433 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3435 TextBlock *tb = (TextBlock *) vtb;
3436 if (!tb)
3437 return;
3439 if (tb->graphic) {
3440 if (tb->object) {
3441 /* naturally, there's a danger to destroying widgets whose action
3442 * brings us here: ie. press a button to destroy it...
3443 * need to find a safer way. till then... this stays commented out */
3444 /* 5 months later... destroy it 10 seconds after now which should
3445 * be enough time for the widget's action to be completed... :-) */
3446 /* This is a bad assumption. Just destroy the widget here.
3447 * if the caller needs it, it can protect it with W_RetainView()
3448 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3449 WMDestroyWidget(tb->d.widget);
3450 } else {
3451 WMReleasePixmap(tb->d.pixmap);
3453 } else {
3454 WMReleaseFont(tb->d.font);
3457 WMReleaseColor(tb->color);
3458 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3459 if (tb->sections)
3460 wfree(tb->sections);
3461 wfree(tb->text);
3462 wfree(tb);
3465 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3467 if (tPtr->fgColor)
3468 WMReleaseColor(tPtr->fgColor);
3470 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3472 paintText(tPtr);
3475 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3477 if (tPtr->bgColor)
3478 WMReleaseColor(tPtr->bgColor);
3480 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3481 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3483 paintText(tPtr);
3486 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3488 if (tPtr->bgPixmap)
3489 WMReleasePixmap(tPtr->bgPixmap);
3491 if (pixmap)
3492 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3493 else
3494 tPtr->bgPixmap = NULL;
3497 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3499 tPtr->flags.relief = relief;
3500 textDidResize(tPtr->view->delegate, tPtr->view);
3503 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3505 if (shouldhave && !tPtr->hS) {
3506 tPtr->hS = WMCreateScroller(tPtr);
3507 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3508 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3509 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3510 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3511 WMMapWidget(tPtr->hS);
3512 } else if (!shouldhave && tPtr->hS) {
3513 WMUnmapWidget(tPtr->hS);
3514 WMDestroyWidget(tPtr->hS);
3515 tPtr->hS = NULL;
3518 tPtr->hpos = 0;
3519 tPtr->prevHpos = 0;
3520 textDidResize(tPtr->view->delegate, tPtr->view);
3523 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3525 if (shouldhave && !tPtr->ruler) {
3526 tPtr->ruler = WMCreateRuler(tPtr);
3527 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3528 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3529 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3530 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3531 } else if (!shouldhave && tPtr->ruler) {
3532 WMShowTextRuler(tPtr, False);
3533 WMDestroyWidget(tPtr->ruler);
3534 tPtr->ruler = NULL;
3536 textDidResize(tPtr->view->delegate, tPtr->view);
3539 void WMShowTextRuler(WMText * tPtr, Bool show)
3541 if (!tPtr->ruler)
3542 return;
3544 if (tPtr->flags.monoFont)
3545 show = False;
3547 tPtr->flags.rulerShown = show;
3548 if (show) {
3549 WMMapWidget(tPtr->ruler);
3550 } else {
3551 WMUnmapWidget(tPtr->ruler);
3554 textDidResize(tPtr->view->delegate, tPtr->view);
3557 Bool WMGetTextRulerShown(WMText * tPtr)
3559 if (!tPtr->ruler)
3560 return False;
3562 return tPtr->flags.rulerShown;
3565 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3567 if (shouldhave && !tPtr->vS) {
3568 tPtr->vS = WMCreateScroller(tPtr);
3569 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3570 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3571 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3572 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3573 WMMapWidget(tPtr->vS);
3574 } else if (!shouldhave && tPtr->vS) {
3575 WMUnmapWidget(tPtr->vS);
3576 WMDestroyWidget(tPtr->vS);
3577 tPtr->vS = NULL;
3580 tPtr->vpos = 0;
3581 tPtr->prevVpos = 0;
3582 textDidResize(tPtr->view->delegate, tPtr->view);
3585 Bool WMScrollText(WMText * tPtr, int amount)
3587 Bool scroll = False;
3589 if (amount == 0 || !tPtr->view->flags.realized)
3590 return False;
3592 if (amount < 0) {
3593 if (tPtr->vpos > 0) {
3594 if (tPtr->vpos > abs(amount))
3595 tPtr->vpos += amount;
3596 else
3597 tPtr->vpos = 0;
3598 scroll = True;
3600 } else {
3601 int limit = tPtr->docHeight - tPtr->visible.h;
3602 if (tPtr->vpos < limit) {
3603 if (tPtr->vpos < limit - amount)
3604 tPtr->vpos += amount;
3605 else
3606 tPtr->vpos = limit;
3607 scroll = True;
3611 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3612 updateScrollers(tPtr);
3613 paintText(tPtr);
3615 tPtr->prevVpos = tPtr->vpos;
3616 return scroll;
3619 Bool WMPageText(WMText * tPtr, Bool direction)
3621 if (!tPtr->view->flags.realized)
3622 return False;
3624 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3627 void WMSetTextEditable(WMText * tPtr, Bool editable)
3629 tPtr->flags.editable = editable;
3632 int WMGetTextEditable(WMText * tPtr)
3634 return tPtr->flags.editable;
3637 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3639 tPtr->flags.indentNewLine = indent;
3642 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3644 tPtr->flags.ignoreNewLine = ignore;
3647 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3649 return tPtr->flags.ignoreNewLine;
3652 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3654 if (mono) {
3655 if (tPtr->flags.rulerShown)
3656 WMShowTextRuler(tPtr, False);
3657 if (tPtr->flags.alignment != WALeft)
3658 tPtr->flags.alignment = WALeft;
3661 tPtr->flags.monoFont = mono;
3662 textDidResize(tPtr->view->delegate, tPtr->view);
3665 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3667 return tPtr->flags.monoFont;
3670 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3672 if (tPtr->dFont)
3673 WMReleaseFont(tPtr->dFont);
3675 if (font) {
3676 tPtr->dFont = WMRetainFont(font);
3677 } else {
3678 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3682 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3684 return WMRetainFont(tPtr->dFont);
3687 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3689 if (tPtr->dColor)
3690 WMReleaseColor(tPtr->dColor);
3692 if (color) {
3693 tPtr->dColor = WMRetainColor(color);
3694 } else {
3695 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3699 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3701 return tPtr->dColor;
3704 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3706 if (tPtr->flags.monoFont)
3707 tPtr->flags.alignment = WALeft;
3708 else
3709 tPtr->flags.alignment = alignment;
3710 WMThawText(tPtr);
3713 int WMGetTextInsertType(WMText * tPtr)
3715 return tPtr->flags.prepend;
3718 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3720 setSelectionProperty(tPtr, NULL, color, -1);
3723 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3725 TextBlock *tb;
3727 tb = tPtr->currentTextBlock;
3729 if (!tb || !tPtr->flags.ownsSelection)
3730 return NULL;
3732 if (!tb->selected)
3733 return NULL;
3735 return tb->color;
3738 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3740 setSelectionProperty(tPtr, font, NULL, -1);
3743 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3745 TextBlock *tb;
3747 tb = tPtr->currentTextBlock;
3749 if (!tb || !tPtr->flags.ownsSelection)
3750 return NULL;
3752 if (!tb->selected)
3753 return NULL;
3755 if (tb->graphic) {
3756 tb = getFirstNonGraphicBlockFor(tb, 1);
3757 if (!tb)
3758 return NULL;
3760 return (tb->selected ? tb->d.font : NULL);
3763 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3765 /* // check this */
3766 if (underlined != 0 && underlined != 1)
3767 return;
3769 setSelectionProperty(tPtr, NULL, NULL, underlined);
3772 int WMGetTextSelectionUnderlined(WMText * tPtr)
3774 TextBlock *tb;
3776 tb = tPtr->currentTextBlock;
3778 if (!tb || !tPtr->flags.ownsSelection)
3779 return 0;
3781 if (!tb->selected)
3782 return 0;
3784 return tb->underlined;
3787 void WMFreezeText(WMText * tPtr)
3789 tPtr->flags.frozen = True;
3792 void WMThawText(WMText * tPtr)
3794 tPtr->flags.frozen = False;
3796 if (tPtr->flags.monoFont) {
3797 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3798 TextBlock *tb;
3800 /* make sure to unmap widgets no matter where they are */
3801 /* they'll be later remapped if needed by paintText */
3802 for (j = 0; j < c; j++) {
3803 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3804 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3805 WMUnmapWidget(tb->d.widget);
3810 tPtr->flags.laidOut = False;
3811 layOutDocument(tPtr);
3812 updateScrollers(tPtr);
3813 paintText(tPtr);
3814 tPtr->flags.needsLayOut = False;
3818 /* find first occurence of a string */
3819 static char *mystrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3821 char *ptr;
3823 if (!haystack || !needle || !end)
3824 return NULL;
3826 for (ptr = haystack; ptr < end; ptr++) {
3827 if (caseSensitive) {
3828 if (*ptr == *needle && !strncmp(ptr, needle, len))
3829 return ptr;
3831 } else {
3832 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3833 return ptr;
3837 return NULL;
3840 /* find last occurence of a string */
3841 static char *mystrrstr(char *haystack, char *needle, unsigned short len, char *end, Bool caseSensitive)
3843 char *ptr;
3845 if (!haystack || !needle || !end)
3846 return NULL;
3848 for (ptr = haystack - 2; ptr > end; ptr--) {
3849 if (caseSensitive) {
3850 if (*ptr == *needle && !strncmp(ptr, needle, len))
3851 return ptr;
3852 } else {
3853 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3854 return ptr;
3858 return NULL;
3861 Bool WMFindInTextStream(WMText * tPtr, char *needle, Bool direction, Bool caseSensitive)
3863 TextBlock *tb;
3864 char *mark = NULL;
3865 unsigned short pos;
3867 #if 0
3868 if (!(tb = tPtr->currentTextBlock)) {
3869 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3870 return False;
3872 } else {
3873 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3874 tb = (direction>0) ? tb->next : tb->prior; */
3875 if (tb != tPtr->lastTextBlock)
3876 tb = tb->prior;
3878 #endif
3879 tb = tPtr->currentTextBlock;
3880 pos = tPtr->tpos;
3882 while (tb) {
3883 if (!tb->graphic) {
3885 if (direction > 0) {
3886 if (pos + 1 < tb->used)
3887 pos++;
3889 if (tb->used - pos > 0 && pos > 0) {
3890 mark = mystrstr(&tb->text[pos], needle,
3891 strlen(needle), &tb->text[tb->used], caseSensitive);
3893 } else {
3894 tb = tb->next;
3895 pos = 0;
3896 continue;
3899 } else {
3900 if (pos - 1 > 0)
3901 pos--;
3903 if (pos > 0) {
3904 mark = mystrrstr(&tb->text[pos], needle,
3905 strlen(needle), tb->text, caseSensitive);
3906 } else {
3907 tb = tb->prior;
3908 if (!tb)
3909 return False;
3910 pos = tb->used;
3911 continue;
3915 if (mark) {
3916 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3918 tPtr->tpos = (int)(mark - tb->text);
3919 tPtr->currentTextBlock = tb;
3920 updateCursorPosition(tPtr);
3921 tPtr->sel.y = tPtr->cursor.y + 5;
3922 tPtr->sel.h = tPtr->cursor.h - 10;
3923 tPtr->sel.x = tPtr->cursor.x + 1;
3924 tPtr->sel.w = WMIN(WMWidthOfString(font,
3925 &tb->text[tPtr->tpos], strlen(needle)),
3926 tPtr->docWidth - tPtr->sel.x);
3927 tPtr->flags.ownsSelection = True;
3928 paintText(tPtr);
3930 return True;
3934 tb = (direction > 0) ? tb->next : tb->prior;
3935 if (tb) {
3936 pos = (direction > 0) ? 0 : tb->used;
3940 return False;
3943 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3945 if (!tPtr->flags.ownsSelection)
3946 return False;
3948 removeSelection(tPtr);
3950 if (replacement) {
3951 insertTextInteractively(tPtr, replacement, strlen(replacement));
3952 updateCursorPosition(tPtr);
3953 paintText(tPtr);
3956 return True;