Update Armenian translation
[wmaker-crm.git] / WINGs / wtext.c
blob1e15b9855616d2d5434fcff2a1536b1ae31e6580
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
4 #include "WINGsP.h"
5 #include <ctype.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 #define DO_BLINK 0
11 /* TODO:
12 * - verify what happens with XK_return in insertTextInt...
13 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
24 /* a Section is a section of a TextBlock that describes what parts
25 of a TextBlock has been laid out on which "line"...
26 o this greatly aids redraw, scroll and selection.
27 o this is created during layoutLine, but may be later modified.
28 o there may be many Sections per TextBlock, hence the array */
29 typedef struct {
30 unsigned int x, y; /* where to draw it from */
31 unsigned short w, h; /* its width and height */
32 unsigned short begin; /* where the layout begins */
33 unsigned short end; /* where it ends */
34 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
35 unsigned short last:1; /* is it the last section on a "line"? */
36 unsigned int _y:31; /* the "line" it and other textblocks are on */
37 } Section;
39 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
40 o text for the block, color and font
41 o or a pointer to the pixmap
42 o OR a pointer to the widget and the (text) description for its graphic
45 typedef struct _TextBlock {
46 struct _TextBlock *next; /* next text block in linked list */
47 struct _TextBlock *prior; /* prior text block in linked list */
49 char *text; /* pointer to text (could be kanji) */
50 /* or to the object's description */
51 union {
52 WMFont *font; /* the font */
53 WMWidget *widget; /* the embedded widget */
54 WMPixmap *pixmap; /* the pixmap */
55 } d; /* description */
57 unsigned short used; /* number of chars in this block */
58 unsigned short allocated; /* size of allocation (in chars) */
59 WMColor *color; /* the color */
61 Section *sections; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
64 unsigned short s_begin; /* where the selection begins */
65 unsigned short s_end; /* where it ends */
67 unsigned int first:1; /* first TextBlock in paragraph */
68 unsigned int blank:1; /* ie. blank paragraph */
69 unsigned int kanji:1; /* is of 16-bit characters or not */
70 unsigned int graphic:1; /* graphic or text: text=0 */
71 unsigned int object:1; /* embedded object or pixmap */
72 unsigned int underlined:1; /* underlined or not */
73 unsigned int selected:1; /* selected or not */
74 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
75 int script:8; /* script in points: negative for subscript */
76 unsigned int marginN:8; /* which of the margins in the tPtr to use */
77 unsigned int nClicks:2; /* single, double, triple clicks */
78 unsigned int RESERVED:7;
79 } TextBlock;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
82 typedef struct {
83 int y, x, h, w;
84 } myRect;
86 typedef struct W_Text {
87 W_Class widgetClass; /* the class number of this widget */
88 W_View *view; /* the view referring to this instance */
90 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
92 WMScroller *vS; /* the vertical scroller */
93 unsigned int vpos; /* the current vertical position */
94 unsigned int prevVpos; /* the previous vertical position */
96 WMScroller *hS; /* the horizontal scroller */
97 unsigned int hpos; /* the current horizontal position */
98 unsigned int prevHpos; /* the previous horizontal position */
100 WMFont *dFont; /* the default font */
101 WMColor *dColor; /* the default color */
102 WMPixmap *dBulletPix; /* the default pixmap for bullets */
104 WMColor *fgColor; /* The current foreground color */
105 WMColor *bgColor; /* The background color */
107 GC stippledGC; /* the GC to overlay selected graphics with */
108 Pixmap db; /* the buffer on which to draw */
109 WMPixmap *bgPixmap; /* the background pixmap */
111 myRect visible; /* the actual rectangle that can be drawn into */
112 myRect cursor; /* the position and (height) of cursor */
113 myRect sel; /* the selection rectangle */
115 WMPoint clicked; /* where in the _document_ was clicked */
117 unsigned short tpos; /* the position in the currentTextBlock */
118 unsigned short docWidth; /* the width of the entire document */
119 unsigned int docHeight; /* the height of the entire document */
121 TextBlock *firstTextBlock;
122 TextBlock *lastTextBlock;
123 TextBlock *currentTextBlock;
125 WMArray *gfxItems; /* a nice array of graphic items */
127 #if DO_BLINK
128 WMHandlerID timerID; /* for nice twinky-winky */
129 #endif
131 WMAction *parser;
132 WMAction *writer;
133 WMTextDelegate *delegate;
134 Time lastClickTime;
136 WMRulerMargins *margins; /* an array of margins */
138 unsigned int nMargins:7; /* the total number of margins in use */
139 struct {
140 unsigned int monoFont:1; /* whether to ignore formats and graphic */
141 unsigned int focused:1; /* whether this instance has input focus */
142 unsigned int editable:1; /* "silly user, you can't edit me" */
143 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
144 unsigned int pointerGrabbed:1; /* "heh, gib me pointer" */
145 unsigned int extendSelection:1; /* shift-drag to select more regions */
147 unsigned int rulerShown:1; /* whether the ruler is shown or not */
148 unsigned int frozen:1; /* whether screen updates are to be made */
149 unsigned int cursorShown:1; /* whether to show the cursor */
150 unsigned int acceptsGraphic:1; /* accept graphic when dropped */
151 unsigned int horizOnDemand:1; /* if a large image should appear */
152 unsigned int needsLayOut:1; /* in case of Append/Deletes */
153 unsigned int ignoreNewLine:1; /* turn it into a ' ' in streams > 1 */
154 unsigned int indentNewLine:1; /* add " " for a newline typed */
155 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
156 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
157 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
158 WMAlignment alignment:2; /* the alignment for text */
159 WMReliefType relief:3; /* the relief to display with */
160 unsigned int isOverGraphic:2; /* the mouse is over a graphic */
161 unsigned int first:1; /* for plain text parsing, newline? */
162 /* unsigned int RESERVED:1; */
163 } flags;
165 WMArray *xdndSourceTypes;
166 WMArray *xdndDestinationTypes;
167 } Text;
169 #define NOTIFY(T,C,N,A) {\
170 WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
176 #define TYPETEXT 0
178 #if 0
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr, int len)
182 char *s;
184 s = wmalloc(len + 1);
185 memcpy(s, ptr, len);
186 s[len] = 0;
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
188 printf("[%s]\n", s);
189 wfree(s);
191 #endif
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
204 static char *default_bullet[] = {
205 "6 6 4 1",
206 " c None s None",
207 ". c black",
208 "X c white",
209 "o c #808080",
210 " ... ",
211 ".XX.. ",
212 ".XX..o",
213 ".....o",
214 " ...oo",
215 " ooo "
218 static void handleEvents(XEvent * event, void *data);
219 static void layOutDocument(Text * tPtr);
220 static void updateScrollers(Text * tPtr);
222 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
224 unsigned int i = 0;
226 for (i = 0; i < tPtr->nMargins; i++) {
228 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
229 return i;
232 return -1;
235 static int newMargin(Text * tPtr, WMRulerMargins * margins)
237 int n;
239 if (!margins) {
240 tPtr->margins[0].retainCount++;
241 return 0;
244 n = getMarginNumber(tPtr, margins);
246 if (n == -1) {
248 if (tPtr->nMargins >= 127) {
249 n = tPtr->nMargins - 1;
250 return n;
253 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
255 n = tPtr->nMargins - 1;
256 tPtr->margins[n].left = margins->left;
257 tPtr->margins[n].first = margins->first;
258 tPtr->margins[n].body = margins->body;
259 tPtr->margins[n].right = margins->right;
260 /* for each tab... */
261 tPtr->margins[n].retainCount = 1;
262 } else {
263 tPtr->margins[n].retainCount++;
266 return n;
269 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
271 unsigned short i, w, lw, selected = False, extend = False;
272 myRect sel;
274 /* if selection rectangle completely encloses the section */
275 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
276 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
277 sel.x = 0;
278 sel.w = tPtr->visible.w;
279 selected = extend = True;
281 /* or if it starts on a line and then goes further down */
282 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
283 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
284 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y)) {
285 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
286 sel.w = tPtr->visible.w;
287 selected = extend = True;
289 /* or if it begins before a line, but ends on it */
290 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
291 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
292 && (tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
294 if (1 || tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
295 sel.w = tPtr->sel.x + tPtr->sel.w;
296 else
297 sel.w = tPtr->sel.x;
299 sel.x = 0;
300 selected = True;
302 /* or if the selection rectangle lies entirely within a line */
303 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
304 && (tPtr->sel.w >= 2)
305 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
306 sel.x = tPtr->sel.x;
307 sel.w = tPtr->sel.w;
308 selected = True;
311 if (selected) {
312 selected = False;
314 /* if not within (modified) selection rectangle */
315 if (tb->sections[s].x > sel.x + sel.w || tb->sections[s].x + tb->sections[s].w < sel.x)
316 return False;
318 if (tb->graphic) {
319 if (tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w && tb->sections[s].x >= sel.x) {
320 rect->width = tb->sections[s].w;
321 rect->x = tb->sections[s].x;
322 selected = True;
324 } else {
326 i = tb->sections[s].begin;
327 lw = 0;
329 if (0 && tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
334 while (++i <= tb->sections[s].end) {
336 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
337 lw += w;
339 if (lw + tb->sections[s].x >= sel.x || i == tb->sections[s].end) {
340 lw -= w;
341 i--;
342 tb->s_begin = (tb->selected ? WMIN(tb->s_begin, i) : i);
343 break;
347 if (i > tb->sections[s].end) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
349 return False;
352 _selEnd: rect->x = tb->sections[s].x + lw;
353 lw = 0;
354 while (++i <= tb->sections[s].end) {
356 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
357 lw += w;
359 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
366 rect->width = lw;
367 if (tb->sections[s].last && sel.x + sel.w
368 >= tb->sections[s].x + tb->sections[s].w && extend) {
369 rect->width += (tPtr->visible.w - rect->x - lw);
372 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
373 selected = True;
374 break;
380 if (selected) {
381 rect->y = tb->sections[s]._y - tPtr->vpos;
382 rect->height = tb->sections[s].h;
383 if (tb->graphic) {
384 printf("DEBUG: graphic s%d h%d\n", s, tb->sections[s].h);
387 return selected;
391 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
393 TextBlock *tb;
394 int isFont = False;
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
400 if (font && (!color || underlined == -1))
401 isFont = True;
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
409 if (isFont) {
410 if (!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
414 } else if (underlined != -1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
423 TextBlock *midtb, *otb = tb;
425 if (underlined != -1) {
426 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
427 &(tb->text[tb->s_begin]),
428 tb->d.font, tb->color,
429 False,
430 (tb->s_end - tb->s_begin));
431 } else {
432 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
433 &(tb->text[tb->s_begin]),
434 (isFont ? font : tb->d.
435 font),
436 (isFont ? tb->
437 color : color), False,
438 (tb->s_end - tb->s_begin));
441 if (midtb) {
442 if (underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
448 midtb->selected = !True;
449 midtb->s_begin = 0;
450 midtb->s_end = midtb->used;
451 tPtr->currentTextBlock = tb;
452 WMAppendTextBlock(tPtr, midtb);
453 tb = tPtr->currentTextBlock;
456 if (otb->used - otb->s_end > 0) {
457 TextBlock *ntb;
458 ntb = (TextBlock *)
459 WMCreateTextBlockWithText(tPtr,
460 &(otb->text[otb->s_end]), otb->d.font,
461 otb->color, False, otb->used - otb->s_end);
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
475 otb->selected = False;
476 otb->used = otb->s_begin;
480 tb = tb->next;
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
486 /* in case the size changed... */
487 if (isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
490 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
491 tPtr->sel.y = 3 + tb->sections[0]._y;
492 tPtr->sel.h = tb->sections[tb->nsections - 1]._y - tb->sections[0]._y;
493 tPtr->sel.w = tb->sections[tb->nsections - 1].w;
494 if (tb->sections[tb->nsections - 1]._y != tb->sections[0]._y) {
495 tPtr->sel.x = 0;
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
502 static Bool removeSelection(Text * tPtr)
504 TextBlock *tb = NULL;
505 Bool first = False;
507 if (!(tb = tPtr->firstTextBlock))
508 return False;
510 while (tb) {
511 if (tb->selected) {
512 if (!first && !tb->graphic) {
513 WMReleaseFont(tPtr->dFont);
514 tPtr->dFont = WMRetainFont(tb->d.font);
515 first = True;
518 if ((tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
519 tPtr->currentTextBlock = tb;
520 if (tb->next) {
521 tPtr->tpos = 0;
522 } else if (tb->prior) {
523 if (tb->prior->graphic)
524 tPtr->tpos = 1;
525 else
526 tPtr->tpos = tb->prior->used;
527 } else
528 tPtr->tpos = 0;
530 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
531 tb = tPtr->currentTextBlock;
532 continue;
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]), &(tb->text[tb->s_end]), tb->used - tb->s_end);
536 tb->used -= (tb->s_end - tb->s_begin);
537 tb->selected = False;
538 tPtr->tpos = tb->s_begin;
543 tb = tb->next;
545 return True;
548 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
550 TextBlock *hold = tb;
552 if (!tb)
553 return NULL;
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir ? tb->next : tb->prior);
561 if (!tb) {
562 tb = hold;
563 while (tb) {
564 if (!tb->graphic)
565 break;
566 tb = (dir ? tb->prior : tb->next);
570 if (!tb)
571 return NULL;
573 return tb;
576 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
578 if (tPtr->flags.monoFont && tb->graphic) {
579 tb = getFirstNonGraphicBlockFor(tb, *dir);
580 if (!tb)
581 return 0;
583 if (tb->graphic) {
584 tPtr->currentTextBlock = (*dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
590 if (!tb->sections) {
591 layOutDocument(tPtr);
592 return 0;
595 *dir = !(y <= tb->sections[0].y);
596 if (*dir) {
597 if ((y <= tb->sections[0]._y + tb->sections[0].h)
598 && (y >= tb->sections[0]._y)) {
599 /* if it's on the same line */
600 if (x < tb->sections[0].x)
601 *dir = 0;
603 } else {
604 if ((y <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
605 && (y >= tb->sections[tb->nsections - 1]._y)) {
606 /* if it's on the same line */
607 if (x > tb->sections[tb->nsections - 1].x)
608 *dir = 1;
612 return 1;
615 static void paintText(Text * tPtr)
617 TextBlock *tb;
618 WMFont *font;
619 const char *text;
620 int len, y, c, s, done = False, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
631 if (tPtr->bgPixmap) {
632 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
633 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
634 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
643 done = False;
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
649 goto _copy_area;
651 while (tb) {
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
656 if (dir) {
657 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
658 break;
659 } else {
660 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
661 break;
664 _getSibling:
665 if (dir) {
666 if (tb->next)
667 tb = tb->next;
668 else
669 break;
670 } else {
671 if (tb->prior)
672 tb = tb->prior;
673 else
674 break;
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for (s = 0; s < tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
708 if (sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
711 rect.x, rect.y, rect.width, rect.height);
715 len = tb->sections[s].end - tb->sections[s].begin;
716 text = &(tb->text[tb->sections[s].begin]);
717 y = tb->sections[s].y - tPtr->vpos;
718 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
720 if (!tPtr->flags.monoFont && tb->underlined) {
721 XDrawLine(dpy, tPtr->db, WMColorGC(color),
722 tb->sections[s].x - tPtr->hpos,
723 y + font->y + 1,
724 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
727 tb = (!done ? tb->next : NULL);
730 /* now , show all graphic items that can be viewed */
731 c = WMGetArrayItemCount(tPtr->gfxItems);
732 if (c > 0 && !tPtr->flags.monoFont) {
733 int j, h;
735 for (j = 0; j < c; j++) {
736 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
738 /* if it's not viewable, and mapped, unmap it */
739 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
740 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
742 if (tb->object) {
743 if ((W_VIEW(tb->d.widget))->flags.mapped) {
744 WMUnmapWidget(tb->d.widget);
747 } else {
748 /* if it's viewable, and not mapped, map it */
749 if (tb->object) {
750 W_View *view = W_VIEW(tb->d.widget);
752 if (!view->flags.realized)
753 WMRealizeWidget(tb->d.widget);
754 if (!view->flags.mapped) {
755 XMapWindow(view->screen->display, view->window);
756 XFlush(view->screen->display);
757 view->flags.mapped = 1;
761 if (tb->object) {
762 WMMoveWidget(tb->d.widget,
763 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
764 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
765 h = WMWidgetHeight(tb->d.widget) + 1;
767 } else {
768 WMDrawPixmap(tb->d.pixmap, tPtr->db,
769 tb->sections[0].x - tPtr->hpos,
770 tb->sections[0].y - tPtr->vpos);
771 h = tb->d.pixmap->height + 1;
775 if (tPtr->flags.ownsSelection) {
776 XRectangle rect;
778 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
779 Drawable d = (0 && tb->object ?
780 (WMWidgetView(tb->d.widget))->window : tPtr->db);
782 tb->selected = True;
783 XFillRectangle(dpy, d, tPtr->stippledGC,
784 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
785 rect.x, rect.y, rect.width, rect.height);
789 if (!tPtr->flags.monoFont && tb->underlined) {
790 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
791 tb->sections[0].x - tPtr->hpos,
792 tb->sections[0].y + h - tPtr->vpos,
793 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos);
800 _copy_area:
801 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
802 int y = tPtr->cursor.y - tPtr->vpos;
803 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
804 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
807 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
808 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
810 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
812 if (tPtr->ruler && tPtr->flags.rulerShown)
813 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
817 static void mouseOverObject(Text * tPtr, int x, int y)
819 TextBlock *tb;
820 Bool result = False;
822 x -= tPtr->visible.x;
823 x += tPtr->hpos;
824 y -= tPtr->visible.y;
825 y += tPtr->vpos;
827 if (tPtr->flags.ownsSelection) {
828 if (tPtr->sel.x <= x
829 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
830 tPtr->flags.isOverGraphic = 1;
831 result = True;
835 if (!result) {
836 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
838 if (c < 1)
839 tPtr->flags.isOverGraphic = 0;
841 for (j = 0; j < c; j++) {
842 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
844 if (!tb || !tb->sections) {
845 tPtr->flags.isOverGraphic = 0;
846 return;
849 if (!tb->object) {
850 if (tb->sections[0].x <= x
851 && tb->sections[0].y <= y
852 && tb->sections[0].x + tb->sections[0].w >= x
853 && tb->sections[0].y + tb->d.pixmap->height >= y) {
854 tPtr->flags.isOverGraphic = 3;
855 result = True;
856 break;
863 if (!result)
864 tPtr->flags.isOverGraphic = 0;
866 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
868 XSetWindowAttributes attribs;
869 attribs.cursor = tPtr->view->attribs.cursor;
870 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
874 #if DO_BLINK
876 static void blinkCursor(void *data)
878 Text *tPtr = (Text *) data;
880 if (tPtr->flags.cursorShown) {
881 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
882 } else {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
885 paintText(tPtr);
886 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
888 #endif
890 static void updateCursorPosition(Text * tPtr)
892 TextBlock *tb = NULL;
893 int x, y, h, s;
895 if (tPtr->flags.needsLayOut)
896 layOutDocument(tPtr);
898 if (!(tb = tPtr->currentTextBlock)) {
899 if (!(tb = tPtr->firstTextBlock)) {
900 WMFont *font = tPtr->dFont;
901 tPtr->tpos = 0;
902 tPtr->cursor.h = font->height + abs(font->height - font->y);
904 tPtr->cursor.y = 2;
905 tPtr->cursor.x = 2;
906 return;
910 if (tb->blank) {
911 tPtr->tpos = 0;
912 y = tb->sections[0].y;
913 h = tb->sections[0].h;
914 x = tb->sections[0].x;
916 } else if (tb->graphic) {
917 y = tb->sections[0].y;
918 h = tb->sections[0].h;
919 x = tb->sections[0].x;
920 if (tPtr->tpos == 1)
921 x += tb->sections[0].w;
923 } else {
924 if (tPtr->tpos > tb->used)
925 tPtr->tpos = tb->used;
927 for (s = 0; s < tb->nsections - 1; s++) {
929 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
930 break;
933 y = tb->sections[s]._y;
934 h = tb->sections[s].h;
935 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
936 &tb->text[tb->sections[s].begin],
937 tPtr->tpos - tb->sections[s].begin);
940 tPtr->cursor.y = y;
941 tPtr->cursor.h = h;
942 tPtr->cursor.x = x;
944 /* scroll the bars if the cursor is not visible */
945 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
946 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
947 tPtr->vpos +=
948 (tPtr->cursor.y + tPtr->cursor.h + 10
949 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
950 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
951 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
956 updateScrollers(tPtr);
959 static void cursorToTextPosition(Text * tPtr, int x, int y)
961 TextBlock *tb = NULL;
962 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
963 const char *text;
965 if (tPtr->flags.needsLayOut)
966 layOutDocument(tPtr);
968 y += (tPtr->vpos - tPtr->visible.y);
969 if (y < 0)
970 y = 0;
972 x -= (tPtr->visible.x - 2);
973 if (x < 0)
974 x = 0;
976 /* clicked is relative to document, not window... */
977 tPtr->clicked.x = x;
978 tPtr->clicked.y = y;
980 if (!(tb = tPtr->currentTextBlock)) {
981 if (!(tb = tPtr->firstTextBlock)) {
982 WMFont *font = tPtr->dFont;
983 tPtr->tpos = 0;
984 tPtr->cursor.h = font->height + abs(font->height - font->y);
985 tPtr->cursor.y = 2;
986 tPtr->cursor.x = 2;
987 return;
991 /* first, which direction? Most likely, newly clicked
992 position will be close to previous */
993 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
994 return;
996 s = (dir ? 0 : tb->nsections - 1);
997 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
998 goto _doneV;
1001 /* get the first (or last) section of the TextBlock that
1002 lies about the vertical click point */
1003 done = False;
1004 while (!done && tb) {
1006 if (tPtr->flags.monoFont && tb->graphic) {
1007 if ((dir ? tb->next : tb->prior))
1008 tb = (dir ? tb->next : tb->prior);
1009 continue;
1012 s = (dir ? 0 : tb->nsections - 1);
1013 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1015 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1016 done = True;
1017 } else {
1018 dir ? s++ : s--;
1022 if (!done) {
1023 if ((dir ? tb->next : tb->prior)) {
1024 tb = (dir ? tb->next : tb->prior);
1025 } else {
1026 break; /* goto _doneH; */
1031 if (s < 0 || s >= tb->nsections) {
1032 s = (dir ? tb->nsections - 1 : 0);
1035 _doneV:
1036 /* we have the line, which TextBlock on that line is it? */
1037 pos = (dir ? 0 : tb->sections[s].begin);
1038 if (tPtr->flags.monoFont && tb->graphic) {
1039 TextBlock *hold = tb;
1040 tb = getFirstNonGraphicBlockFor(hold, dir);
1042 if (!tb) {
1043 tPtr->tpos = 0;
1044 tb = hold;
1045 s = 0;
1046 goto _doNothing;
1050 _y = tb->sections[s]._y;
1052 while (tb) {
1054 if (tPtr->flags.monoFont && tb->graphic) {
1055 tb = (dir ? tb->next : tb->prior);
1056 continue;
1059 if (dir) {
1060 if (tb->graphic) {
1061 if (tb->object)
1062 _w = WMWidgetWidth(tb->d.widget) - 5;
1063 else
1064 _w = tb->d.pixmap->width - 5;
1066 if (tb->sections[0].x + _w >= x)
1067 break;
1068 } else {
1069 text = &(tb->text[tb->sections[s].begin]);
1070 len = tb->sections[s].end - tb->sections[s].begin;
1071 _w = WMWidthOfString(tb->d.font, text, len);
1072 if (tb->sections[s].x + _w >= x)
1073 break;
1076 } else {
1077 if (tb->sections[s].x <= x)
1078 break;
1081 if ((dir ? tb->next : tb->prior)) {
1082 TextBlock *nxt = (dir ? tb->next : tb->prior);
1083 if (tPtr->flags.monoFont && nxt->graphic) {
1084 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1085 if (!nxt) {
1086 pos = (dir ? 0 : tb->sections[s].begin);
1087 tPtr->cursor.x = tb->sections[s].x;
1088 goto _doneH;
1092 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1093 /* this must be the last/first on this line. stop */
1094 pos = (dir ? tb->sections[s].end : 0);
1095 tPtr->cursor.x = tb->sections[s].x;
1096 if (!tb->blank) {
1097 if (tb->graphic) {
1098 if (tb->object)
1099 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1100 else
1101 tPtr->cursor.x += tb->d.pixmap->width;
1102 } else if (pos > tb->sections[s].begin) {
1103 tPtr->cursor.x +=
1104 WMWidthOfString(tb->d.font,
1105 &(tb->text[tb->sections[s].begin]),
1106 pos - tb->sections[s].begin);
1109 goto _doneH;
1113 if ((dir ? tb->next : tb->prior)) {
1114 tb = (dir ? tb->next : tb->prior);
1115 } else {
1116 break;
1119 if (tb)
1120 s = (dir ? 0 : tb->nsections - 1);
1123 /* we have said TextBlock, now where within it? */
1124 if (tb) {
1125 if (tb->graphic) {
1126 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1128 tPtr->cursor.x = tb->sections[0].x;
1130 if (x > tPtr->cursor.x + gw / 2) {
1131 pos = 1;
1132 tPtr->cursor.x += gw;
1133 } else {
1134 printf("first %d\n", tb->first);
1135 if (tb->prior) {
1136 if (tb->prior->graphic)
1137 pos = 1;
1138 else
1139 pos = tb->prior->used;
1140 tb = tb->prior;
1141 } else
1142 pos = 0;
1146 s = 0;
1147 goto _doneH;
1149 } else {
1150 WMFont *f = tb->d.font;
1151 len = tb->sections[s].end - tb->sections[s].begin;
1152 text = &(tb->text[tb->sections[s].begin]);
1154 _w = x - tb->sections[s].x;
1155 pos = 0;
1157 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1158 pos++;
1160 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1162 pos += tb->sections[s].begin;
1166 _doneH:
1167 if (tb->graphic) {
1168 tPtr->tpos = (pos <= 1) ? pos : 0;
1169 } else {
1170 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1172 _doNothing:
1173 if (!tb)
1174 printf("...for this app will surely crash :-)\n");
1176 tPtr->currentTextBlock = tb;
1177 tPtr->cursor.h = tb->sections[s].h;
1178 tPtr->cursor.y = tb->sections[s]._y;
1180 /* scroll the bars if the cursor is not visible */
1181 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1182 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1183 tPtr->vpos +=
1184 (tPtr->cursor.y + tPtr->cursor.h + 10
1185 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1186 updateScrollers(tPtr);
1187 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1188 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1189 updateScrollers(tPtr);
1196 static void updateScrollers(Text * tPtr)
1199 if (tPtr->flags.frozen)
1200 return;
1202 if (tPtr->vS) {
1203 if (tPtr->docHeight <= tPtr->visible.h) {
1204 WMSetScrollerParameters(tPtr->vS, 0, 1);
1205 tPtr->vpos = 0;
1206 } else {
1207 float hmax = (float)(tPtr->docHeight);
1208 WMSetScrollerParameters(tPtr->vS,
1209 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1210 (float)tPtr->visible.h / hmax);
1212 } else
1213 tPtr->vpos = 0;
1215 if (tPtr->hS) {
1216 if (tPtr->docWidth <= tPtr->visible.w) {
1217 WMSetScrollerParameters(tPtr->hS, 0, 1);
1218 tPtr->hpos = 0;
1219 } else {
1220 float wmax = (float)(tPtr->docWidth);
1221 WMSetScrollerParameters(tPtr->hS,
1222 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1223 (float)tPtr->visible.w / wmax);
1225 } else
1226 tPtr->hpos = 0;
1229 static void scrollersCallBack(WMWidget * w, void *self)
1231 Text *tPtr = (Text *) self;
1232 Bool scroll = False;
1233 int which;
1235 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1236 return;
1238 if (w == tPtr->vS) {
1239 int height;
1240 height = tPtr->visible.h;
1242 which = WMGetScrollerHitPart(tPtr->vS);
1243 switch (which) {
1245 case WSDecrementLine:
1246 if (tPtr->vpos > 0) {
1247 if (tPtr->vpos > 16)
1248 tPtr->vpos -= 16;
1249 else
1250 tPtr->vpos = 0;
1252 break;
1254 case WSIncrementLine:{
1255 int limit = tPtr->docHeight - height;
1256 if (tPtr->vpos < limit) {
1257 if (tPtr->vpos < limit - 16)
1258 tPtr->vpos += 16;
1259 else
1260 tPtr->vpos = limit;
1263 break;
1265 case WSDecrementPage:
1266 if (((int)tPtr->vpos - (int)height) >= 0)
1267 tPtr->vpos -= height;
1268 else
1269 tPtr->vpos = 0;
1270 break;
1272 case WSIncrementPage:
1273 tPtr->vpos += height;
1274 if (tPtr->vpos > (tPtr->docHeight - height))
1275 tPtr->vpos = tPtr->docHeight - height;
1276 break;
1278 case WSKnob:
1279 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1280 * (float)(tPtr->docHeight - height);
1281 break;
1283 case WSKnobSlot:
1284 case WSNoPart:
1285 break;
1287 scroll = (tPtr->vpos != tPtr->prevVpos);
1288 tPtr->prevVpos = tPtr->vpos;
1291 if (w == tPtr->hS) {
1292 int width = tPtr->visible.w;
1294 which = WMGetScrollerHitPart(tPtr->hS);
1295 switch (which) {
1297 case WSDecrementLine:
1298 if (tPtr->hpos > 0) {
1299 if (tPtr->hpos > 16)
1300 tPtr->hpos -= 16;
1301 else
1302 tPtr->hpos = 0;
1304 break;
1306 case WSIncrementLine:{
1307 int limit = tPtr->docWidth - width;
1308 if (tPtr->hpos < limit) {
1309 if (tPtr->hpos < limit - 16)
1310 tPtr->hpos += 16;
1311 else
1312 tPtr->hpos = limit;
1315 break;
1317 case WSDecrementPage:
1318 if (((int)tPtr->hpos - (int)width) >= 0)
1319 tPtr->hpos -= width;
1320 else
1321 tPtr->hpos = 0;
1322 break;
1324 case WSIncrementPage:
1325 tPtr->hpos += width;
1326 if (tPtr->hpos > (tPtr->docWidth - width))
1327 tPtr->hpos = tPtr->docWidth - width;
1328 break;
1330 case WSKnob:
1331 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1332 * (float)(tPtr->docWidth - width);
1333 break;
1335 case WSKnobSlot:
1336 case WSNoPart:
1337 break;
1339 scroll = (tPtr->hpos != tPtr->prevHpos);
1340 tPtr->prevHpos = tPtr->hpos;
1343 if (scroll) {
1344 updateScrollers(tPtr);
1345 paintText(tPtr);
1349 typedef struct {
1350 TextBlock *tb;
1351 unsigned short begin, end; /* what part of the text block */
1352 } myLineItems;
1354 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1356 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1357 WMFont *font;
1358 const char *text;
1359 TextBlock *tb, *tbsame = NULL;
1361 if (!items || nitems == 0)
1362 return 0;
1364 for (i = 0; i < nitems; i++) {
1365 tb = items[i].tb;
1367 if (tb->graphic) {
1368 if (!tPtr->flags.monoFont) {
1369 if (tb->object) {
1370 WMWidget *wdt = tb->d.widget;
1371 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1372 if (tPtr->flags.alignment != WALeft)
1373 lw += WMWidgetWidth(wdt);
1374 } else {
1375 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1376 if (tPtr->flags.alignment != WALeft)
1377 lw += tb->d.pixmap->width;
1381 } else {
1382 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1383 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1384 max_d = 2;
1385 line_height = WMAX(line_height, font->height + max_d);
1386 text = &(tb->text[items[i].begin]);
1387 len = items[i].end - items[i].begin;
1388 if (tPtr->flags.alignment != WALeft)
1389 lw += WMWidthOfString(font, text, len);
1393 if (tPtr->flags.alignment == WARight) {
1394 j = tPtr->visible.w - lw;
1395 } else if (tPtr->flags.alignment == WACenter) {
1396 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1399 for (i = 0; i < nitems; i++) {
1400 tb = items[i].tb;
1402 if (tbsame == tb) { /* extend it, since it's on same line */
1403 tb->sections[tb->nsections - 1].end = items[i].end;
1404 n = tb->nsections - 1;
1405 } else {
1406 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1407 n = tb->nsections - 1;
1408 tb->sections[n]._y = y + max_d;
1409 tb->sections[n].max_d = max_d;
1410 tb->sections[n].x = x + j;
1411 tb->sections[n].h = line_height;
1412 tb->sections[n].begin = items[i].begin;
1413 tb->sections[n].end = items[i].end;
1416 tb->sections[n].last = (i + 1 == nitems);
1418 if (tb->graphic) {
1419 if (!tPtr->flags.monoFont) {
1420 if (tb->object) {
1421 WMWidget *wdt = tb->d.widget;
1422 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1423 tb->sections[n].w = WMWidgetWidth(wdt);
1424 } else {
1425 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1426 tb->sections[n].w = tb->d.pixmap->width;
1428 x += tb->sections[n].w;
1430 } else {
1431 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1432 len = items[i].end - items[i].begin;
1433 text = &(tb->text[items[i].begin]);
1435 tb->sections[n].y = y + line_height - font->y;
1436 tb->sections[n].w =
1437 WMWidthOfString(font,
1438 &(tb->text[tb->sections[n].begin]),
1439 tb->sections[n].end - tb->sections[n].begin);
1441 x += WMWidthOfString(font, text, len);
1444 tbsame = tb;
1447 return line_height;
1451 static void layOutDocument(Text * tPtr)
1453 TextBlock *tb;
1454 myLineItems *items = NULL;
1455 unsigned int itemsSize = 0, nitems = 0, begin, end;
1456 WMFont *font;
1457 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1458 const char *start = NULL, *mark = NULL;
1460 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1461 return;
1463 assert(tPtr->visible.w > 20);
1465 tPtr->docWidth = tPtr->visible.w;
1466 x = tPtr->margins[tb->marginN].first;
1467 bmargin = tPtr->margins[tb->marginN].body;
1469 /* only partial layOut needed: re-Lay only affected textblocks */
1470 if (tPtr->flags.laidOut) {
1471 tb = tPtr->currentTextBlock;
1473 /* search backwards for textblocks on same line */
1474 while (tb->prior) {
1475 if (!tb->sections || tb->nsections < 1) {
1476 tb = tPtr->firstTextBlock;
1477 tPtr->flags.laidOut = False;
1478 y = 0;
1479 goto _layOut;
1482 if (!tb->prior->sections || tb->prior->nsections < 1) {
1483 tb = tPtr->firstTextBlock;
1484 tPtr->flags.laidOut = False;
1485 y = 0;
1486 goto _layOut;
1489 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1490 break;
1492 tb = tb->prior;
1495 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1496 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1497 tb->prior->sections[tb->prior->nsections - 1].h -
1498 tb->prior->sections[tb->prior->nsections - 1].max_d;
1499 } else {
1500 y = 0;
1504 _layOut:
1505 while (tb) {
1507 if (tb->sections && tb->nsections > 0) {
1508 wfree(tb->sections);
1509 tb->sections = NULL;
1510 tb->nsections = 0;
1513 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1514 TextBlock *next = tb->next;
1515 tPtr->currentTextBlock = tb;
1516 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1517 tb = next;
1518 tb->first = True;
1519 continue;
1522 if (tb->first && tb != tPtr->firstTextBlock) {
1523 y += layOutLine(tPtr, items, nitems, x, y);
1524 x = tPtr->margins[tb->marginN].first;
1525 bmargin = tPtr->margins[tb->marginN].body;
1526 nitems = 0;
1527 lw = 0;
1530 if (tb->graphic) {
1531 if (!tPtr->flags.monoFont) {
1532 if (tb->object)
1533 width = WMWidgetWidth(tb->d.widget);
1534 else
1535 width = tb->d.pixmap->width;
1537 if (width > tPtr->docWidth)
1538 tPtr->docWidth = width;
1540 lw += width;
1541 if (lw >= tPtr->visible.w - x) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 nitems = 0;
1544 x = bmargin;
1545 lw = width;
1548 if (nitems + 1 > itemsSize) {
1549 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1552 items[nitems].tb = tb;
1553 items[nitems].begin = 0;
1554 items[nitems].end = 0;
1555 nitems++;
1558 } else if ((start = tb->text)) {
1559 begin = end = 0;
1560 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1562 while (start) {
1563 mark = strchr(start, ' ');
1564 if (mark) {
1565 end += (int)(mark - start) + 1;
1566 start = mark + 1;
1567 } else {
1568 end += strlen(start);
1569 start = mark;
1572 if (end > tb->used)
1573 end = tb->used;
1575 if (end - begin > 0) {
1577 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1579 /* if it won't fit, char wrap it */
1580 if (width >= tPtr->visible.w) {
1581 char *t = &tb->text[begin];
1582 int l = end - begin, i = 0;
1583 do {
1584 width = WMWidthOfString(font, t, ++i);
1585 } while (width < tPtr->visible.w && i < l);
1586 if (i > 2)
1587 i--;
1588 end = begin + i;
1589 start = &tb->text[end];
1592 lw += width;
1595 if (lw >= tPtr->visible.w - x) {
1596 y += layOutLine(tPtr, items, nitems, x, y);
1597 lw = width;
1598 x = bmargin;
1599 nitems = 0;
1602 if (nitems + 1 > itemsSize) {
1603 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1606 items[nitems].tb = tb;
1607 items[nitems].begin = begin;
1608 items[nitems].end = end;
1609 nitems++;
1611 begin = end;
1615 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1616 if (0 && tPtr->flags.laidOut
1617 && tb->next && tb->next->sections && tb->next->nsections > 0
1618 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1619 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1620 TextBlock *ltb = tPtr->lastTextBlock;
1621 int ly = ltb->sections[ltb->nsections - 1]._y;
1622 int lh = ltb->sections[ltb->nsections - 1].h;
1623 int ss, sd;
1625 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1626 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1628 y += layOutLine(tPtr, items, nitems, x, y);
1629 ss = ly + lh - y;
1630 sd = tPtr->docHeight - y;
1632 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1633 y += tb->next->sections[0]._y - y;
1634 nitems = 0;
1635 printf("nitems%d\n", nitems);
1636 if (ss - sd != 0)
1637 y = tPtr->docHeight + ss - sd;
1639 break;
1640 } else {
1641 tPtr->flags.laidOut = False;
1645 tb = tb->next;
1648 if (nitems > 0)
1649 y += layOutLine(tPtr, items, nitems, x, y);
1651 if (tPtr->docHeight != y + 10) {
1652 tPtr->docHeight = y + 10;
1653 updateScrollers(tPtr);
1656 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1657 XEvent event;
1659 tPtr->flags.horizOnDemand = True;
1660 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1661 event.type = Expose;
1662 handleEvents(&event, (void *)tPtr);
1664 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1665 tPtr->flags.horizOnDemand = False;
1666 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1669 tPtr->flags.laidOut = True;
1671 if (items && itemsSize > 0)
1672 wfree(items);
1676 static void textDidResize(W_ViewDelegate * self, WMView * view)
1678 Text *tPtr = (Text *) view->self;
1679 unsigned short w = tPtr->view->size.width;
1680 unsigned short h = tPtr->view->size.height;
1681 unsigned short rh = 0, vw = 0, rel;
1683 /* Parameter not used, but tell the compiler that it is ok */
1684 (void) self;
1686 rel = (tPtr->flags.relief == WRFlat);
1688 if (tPtr->ruler && tPtr->flags.rulerShown) {
1689 WMMoveWidget(tPtr->ruler, 2, 2);
1690 WMResizeWidget(tPtr->ruler, w - 4, 40);
1691 rh = 40;
1694 if (tPtr->vS) {
1695 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1696 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1697 vw = 20;
1698 WMSetRulerOffset(tPtr->ruler, 22);
1699 } else
1700 WMSetRulerOffset(tPtr->ruler, 2);
1702 if (tPtr->hS) {
1703 if (tPtr->vS) {
1704 WMMoveWidget(tPtr->hS, vw, h - 21);
1705 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1706 } else {
1707 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1708 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1712 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1713 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1714 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1715 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1716 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1717 tPtr->margins[0].right = tPtr->visible.w;
1719 if (tPtr->view->flags.realized) {
1721 if (tPtr->db) {
1722 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1723 tPtr->db = (Pixmap) NULL;
1726 if (tPtr->visible.w < 40)
1727 tPtr->visible.w = 40;
1728 if (tPtr->visible.h < 20)
1729 tPtr->visible.h = 20;
1731 if (!tPtr->db) {
1732 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1733 tPtr->view->window, tPtr->visible.w,
1734 tPtr->visible.h, tPtr->view->screen->depth);
1738 WMThawText(tPtr);
1741 W_ViewDelegate _TextViewDelegate = {
1742 NULL,
1743 NULL,
1744 textDidResize,
1745 NULL,
1746 NULL
1749 #define TEXT_BUFFER_INCR 8
1750 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1752 static void clearText(Text * tPtr)
1754 tPtr->vpos = tPtr->hpos = 0;
1755 tPtr->docHeight = tPtr->docWidth = 0;
1756 tPtr->cursor.x = -23;
1758 if (!tPtr->firstTextBlock)
1759 return;
1761 while (tPtr->currentTextBlock)
1762 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1764 tPtr->firstTextBlock = NULL;
1765 tPtr->currentTextBlock = NULL;
1766 tPtr->lastTextBlock = NULL;
1767 WMEmptyArray(tPtr->gfxItems);
1770 /* possibly remove a single character from the currentTextBlock,
1771 or if there's a selection, remove it...
1772 note that Delete and Backspace are treated differently */
1773 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1775 TextBlock *tb;
1776 Bool back = (Bool) (ksym == XK_BackSpace);
1777 Bool done = 1, wasFirst = 0;
1779 if (!tPtr->flags.editable)
1780 return;
1782 if (!(tb = tPtr->currentTextBlock))
1783 return;
1785 if (tPtr->flags.ownsSelection) {
1786 if (removeSelection(tPtr))
1787 layOutDocument(tPtr);
1788 return;
1791 wasFirst = tb->first;
1792 if (back && tPtr->tpos < 1) {
1793 if (tb->prior) {
1794 if (tb->prior->blank) {
1795 tPtr->currentTextBlock = tb->prior;
1796 WMRemoveTextBlock(tPtr);
1797 tPtr->currentTextBlock = tb;
1798 tb->first = True;
1799 layOutDocument(tPtr);
1800 return;
1801 } else {
1802 if (tb->blank) {
1803 TextBlock *prior = tb->prior;
1804 tPtr->currentTextBlock = tb;
1805 WMRemoveTextBlock(tPtr);
1806 tb = prior;
1807 } else {
1808 tb = tb->prior;
1811 if (tb->graphic)
1812 tPtr->tpos = 1;
1813 else
1814 tPtr->tpos = tb->used;
1816 tPtr->currentTextBlock = tb;
1817 done = 1;
1818 if (wasFirst) {
1819 if (tb->next)
1820 tb->next->first = False;
1821 layOutDocument(tPtr);
1822 return;
1828 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1829 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1830 if (back)
1831 tPtr->tpos--;
1832 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1833 tb->used--;
1834 done = 0;
1837 /* if there are no characters left to back over in the textblock,
1838 but it still has characters to the right of the cursor: */
1839 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1840 || tb->graphic) {
1842 /* no more chars, and it's marked as blank? */
1843 if (tb->blank) {
1844 TextBlock *sibling = (back ? tb->prior : tb->next);
1846 if (tb->used == 0 || tb->graphic)
1847 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1849 if (sibling) {
1850 tPtr->currentTextBlock = sibling;
1851 if (tb->graphic)
1852 tPtr->tpos = (back ? 1 : 0);
1853 else
1854 tPtr->tpos = (back ? sibling->used : 0);
1856 /* no more chars, so mark it as blank */
1857 } else if (tb->used == 0) {
1858 tb->blank = 1;
1859 } else if (tb->graphic) {
1860 Bool hasNext = (tb->next != NULL);
1862 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1863 if (hasNext) {
1864 tPtr->tpos = 0;
1865 } else if (tPtr->currentTextBlock) {
1866 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1868 } else
1869 printf("DEBUG: unaccounted for... catch this!\n");
1872 layOutDocument(tPtr);
1875 static void insertTextInteractively(Text * tPtr, char *text, int len)
1877 TextBlock *tb;
1878 char *newline = NULL;
1880 if (!tPtr->flags.editable) {
1881 return;
1884 if (len < 1 || !text)
1885 return;
1887 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1888 return;
1890 if (tPtr->flags.ownsSelection)
1891 removeSelection(tPtr);
1893 if (tPtr->flags.ignoreNewLine) {
1894 int i;
1895 for (i = 0; i < len; i++) {
1896 if (text[i] == '\n')
1897 text[i] = ' ';
1901 tb = tPtr->currentTextBlock;
1902 if (!tb || tb->graphic) {
1903 tPtr->tpos = 0;
1904 WMAppendTextStream(tPtr, text);
1905 layOutDocument(tPtr);
1906 return;
1909 if ((newline = strchr(text, '\n'))) {
1910 int nlen = (int)(newline - text);
1911 int s = tb->used - tPtr->tpos;
1913 if (!tb->blank && nlen > 0) {
1914 char *save = NULL;
1916 if (s > 0) {
1917 save = wmalloc(s);
1918 memcpy(save, &tb->text[tPtr->tpos], s);
1919 tb->used -= (tb->used - tPtr->tpos);
1921 insertTextInteractively(tPtr, text, nlen);
1922 newline++;
1923 WMAppendTextStream(tPtr, newline);
1924 if (s > 0) {
1925 insertTextInteractively(tPtr, save, s);
1926 wfree(save);
1928 } else {
1929 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1931 unsigned short savePos = tPtr->tpos;
1932 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1933 tb->d.font, tb->color, True,
1934 tb->used - tPtr->tpos);
1936 if (tb->sections[0].end == tPtr->tpos)
1937 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1938 NULL, tb->d.font,
1939 tb->color, True, 0));
1941 tb->used = savePos;
1942 WMAppendTextBlock(tPtr, ntb);
1943 tPtr->tpos = 0;
1945 } else if (tPtr->tpos == tb->used) {
1946 if (tPtr->flags.indentNewLine) {
1947 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1948 " ", tb->d.font,
1949 tb->color, True, 4));
1950 tPtr->tpos = 4;
1951 } else {
1952 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1953 NULL, tb->d.font,
1954 tb->color, True, 0));
1955 tPtr->tpos = 0;
1957 } else if (tPtr->tpos == 0) {
1958 if (tPtr->flags.indentNewLine) {
1959 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1960 " ", tb->d.font,
1961 tb->color, True, 4));
1962 } else {
1963 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1964 NULL, tb->d.font,
1965 tb->color, True, 0));
1967 tPtr->tpos = 0;
1968 if (tPtr->currentTextBlock->next)
1969 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1972 } else {
1973 if (tb->used + len >= tb->allocated) {
1974 tb->allocated = reqBlockSize(tb->used + len);
1975 tb->text = wrealloc(tb->text, tb->allocated);
1978 if (tb->blank) {
1979 memcpy(tb->text, text, len);
1980 tb->used = len;
1981 tPtr->tpos = len;
1982 tb->text[tb->used] = 0;
1983 tb->blank = False;
1985 } else {
1986 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
1987 memmove(&tb->text[tPtr->tpos], text, len);
1988 tb->used += len;
1989 tPtr->tpos += len;
1990 tb->text[tb->used] = 0;
1995 layOutDocument(tPtr);
1998 static void selectRegion(Text * tPtr, int x, int y)
2001 if (x < 0 || y < 0)
2002 return;
2004 y += (tPtr->flags.rulerShown ? 40 : 0);
2005 y += tPtr->vpos;
2006 if (y > 10)
2007 y -= 10; /* the original offset */
2009 x -= tPtr->visible.x - 2;
2010 if (x < 0)
2011 x = 0;
2013 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2014 tPtr->sel.w = abs(tPtr->clicked.x - x);
2015 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2016 tPtr->sel.h = abs(tPtr->clicked.y - y);
2018 tPtr->flags.ownsSelection = True;
2019 paintText(tPtr);
2022 static void releaseSelection(Text * tPtr)
2024 TextBlock *tb = tPtr->firstTextBlock;
2026 while (tb) {
2027 tb->selected = False;
2028 tb = tb->next;
2030 tPtr->flags.ownsSelection = False;
2031 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2033 paintText(tPtr);
2036 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2038 Text *tPtr = view->self;
2039 Display *dpy = tPtr->view->screen->display;
2040 Atom _TARGETS;
2041 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2042 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2043 WMData *data = NULL;
2045 /* Parameter not used, but tell the compiler that it is ok */
2046 (void) selection;
2047 (void) cdata;
2049 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2050 char *text = WMGetTextSelectedStream(tPtr);
2052 if (text) {
2053 data = WMCreateDataWithBytes(text, strlen(text));
2054 WMSetDataFormat(data, TYPETEXT);
2055 wfree(text);
2057 *type = target;
2058 return data;
2059 } else
2060 printf("didn't get it\n");
2062 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2063 if (target == _TARGETS) {
2064 Atom array[4];
2066 array[0] = _TARGETS;
2067 array[1] = XA_STRING;
2068 array[2] = TEXT;
2069 array[3] = COMPOUND_TEXT;
2071 data = WMCreateDataWithBytes(&array, sizeof(array));
2072 WMSetDataFormat(data, 32);
2074 *type = target;
2075 return data;
2078 return NULL;
2081 static void lostHandler(WMView * view, Atom selection, void *cdata)
2083 /* Parameter not used, but tell the compiler that it is ok */
2084 (void) selection;
2085 (void) cdata;
2087 releaseSelection((WMText *) view->self);
2090 static WMSelectionProcs selectionHandler = {
2091 requestHandler, lostHandler, NULL
2094 static void ownershipObserver(void *observerData, WMNotification * notification)
2096 if (observerData != WMGetNotificationClientData(notification))
2097 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2100 static void autoSelectText(Text * tPtr, int clicks)
2102 int x, start;
2103 TextBlock *tb;
2104 char *mark = NULL, behind, ahead;
2106 if (!(tb = tPtr->currentTextBlock))
2107 return;
2109 if (clicks == 2) {
2111 switch (tb->text[tPtr->tpos]) {
2112 case ' ':
2113 return;
2115 case '<': case '>': behind = '<'; ahead = '>'; break;
2116 case '{': case '}': behind = '{'; ahead = '}'; break;
2117 case '[': case ']': behind = '['; ahead = ']'; break;
2119 default:
2120 behind = ahead = ' ';
2123 tPtr->sel.y = tPtr->cursor.y + 5;
2124 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2126 if (tb->graphic) {
2127 tPtr->sel.x = tb->sections[0].x;
2128 tPtr->sel.w = tb->sections[0].w;
2129 } else {
2130 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2132 start = tPtr->tpos;
2133 while (start > 0 && tb->text[start - 1] != behind)
2134 start--;
2136 x = tPtr->cursor.x;
2137 if (tPtr->tpos > start) {
2138 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2140 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2142 if ((mark = strchr(&tb->text[start], ahead))) {
2143 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2144 (int)(mark - &tb->text[start]));
2145 } else if (tb->used > start) {
2146 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2150 } else if (clicks == 3) {
2151 TextBlock *cur = tb;
2153 while (!tb->first) {
2154 tb = tb->prior;
2155 if (tb == NULL) {
2156 #ifndef NDEBUG
2157 wwarning("corrupted list of text blocks in WMText, while searching for first block");
2158 #endif
2159 goto error_select_3clicks;
2162 tPtr->sel.y = tb->sections[0]._y;
2164 tb = cur;
2165 while (tb->next && !tb->next->first) {
2166 tb = tb->next;
2168 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2170 error_select_3clicks:
2171 tPtr->sel.x = 0;
2172 tPtr->sel.w = tPtr->docWidth;
2173 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2176 if (!tPtr->flags.ownsSelection) {
2177 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2178 tPtr->flags.ownsSelection = True;
2180 paintText(tPtr);
2184 # if 0
2185 static void fontChanged(void *observerData, WMNotification * notification)
2187 WMText *tPtr = (WMText *) observerData;
2188 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2189 printf("fontChanged\n");
2191 if (!tPtr || !font)
2192 return;
2194 if (tPtr->flags.ownsSelection)
2195 WMSetTextSelectionFont(tPtr, font);
2197 #endif
2199 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2201 char buffer[64];
2202 KeySym ksym;
2203 int control_pressed = False;
2204 TextBlock *tb = NULL;
2206 if (((XKeyEvent *) event)->state & ControlMask)
2207 control_pressed = True;
2208 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2210 switch (ksym) {
2212 case XK_Home:
2213 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2214 tPtr->tpos = 0;
2215 updateCursorPosition(tPtr);
2216 paintText(tPtr);
2217 break;
2219 case XK_End:
2220 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2221 if (tPtr->currentTextBlock->graphic)
2222 tPtr->tpos = 1;
2223 else
2224 tPtr->tpos = tPtr->currentTextBlock->used;
2226 updateCursorPosition(tPtr);
2227 paintText(tPtr);
2228 break;
2230 case XK_Left:
2231 if (!(tb = tPtr->currentTextBlock))
2232 break;
2234 if (tb->graphic || tPtr->tpos == 0) {
2235 if (tb->prior) {
2236 tPtr->currentTextBlock = tb->prior;
2237 if (tPtr->currentTextBlock->graphic)
2238 tPtr->tpos = 1;
2239 else
2240 tPtr->tpos = tPtr->currentTextBlock->used;
2242 if (!tb->first && tPtr->tpos > 0)
2243 tPtr->tpos--;
2244 } else
2245 tPtr->tpos = 0;
2246 } else
2247 tPtr->tpos--;
2248 updateCursorPosition(tPtr);
2249 paintText(tPtr);
2250 break;
2252 case XK_Right:
2253 if (!(tb = tPtr->currentTextBlock))
2254 break;
2255 if (tb->graphic || tPtr->tpos == tb->used) {
2256 if (tb->next) {
2257 tPtr->currentTextBlock = tb->next;
2258 tPtr->tpos = 0;
2259 if (!tb->next->first && tb->next->used > 0)
2260 tPtr->tpos++;
2261 } else {
2262 if (tb->graphic)
2263 tPtr->tpos = 1;
2264 else
2265 tPtr->tpos = tb->used;
2267 } else
2268 tPtr->tpos++;
2269 updateCursorPosition(tPtr);
2270 paintText(tPtr);
2271 break;
2273 case XK_Down:
2274 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2275 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2276 paintText(tPtr);
2277 break;
2279 case XK_Up:
2280 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2281 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2282 paintText(tPtr);
2283 break;
2285 case XK_BackSpace:
2286 case XK_Delete:
2287 #ifdef XK_KP_Delete
2288 case XK_KP_Delete:
2289 #endif
2290 deleteTextInteractively(tPtr, ksym);
2291 updateCursorPosition(tPtr);
2292 paintText(tPtr);
2293 break;
2295 case XK_Control_R:
2296 case XK_Control_L:
2297 control_pressed = True;
2298 break;
2300 case XK_Tab:
2301 insertTextInteractively(tPtr, " ", 4);
2302 updateCursorPosition(tPtr);
2303 paintText(tPtr);
2304 break;
2306 case XK_Return:
2307 *buffer = '\n';
2308 /* FALLTHRU */
2309 default:
2310 if (*buffer != 0 && !control_pressed) {
2311 insertTextInteractively(tPtr, buffer, strlen(buffer));
2312 updateCursorPosition(tPtr);
2313 paintText(tPtr);
2315 } else if (control_pressed && ksym == XK_r) {
2316 Bool i = !tPtr->flags.rulerShown;
2317 WMShowTextRuler(tPtr, i);
2318 tPtr->flags.rulerShown = i;
2319 } else if (control_pressed && *buffer == '\a') {
2320 XBell(tPtr->view->screen->display, 0);
2321 } else {
2322 WMRelayToNextResponder(tPtr->view, event);
2326 if (!control_pressed && tPtr->flags.ownsSelection) {
2327 releaseSelection(tPtr);
2331 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2333 Text *tPtr = (Text *) view->self;
2334 char *text;
2336 /* Parameter not used, but tell the compiler that it is ok */
2337 (void) selection;
2338 (void) target;
2339 (void) timestamp;
2340 (void) cdata;
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 /* FALLTHRU */
2549 case ButtonRelease:
2550 if (tPtr->flags.pointerGrabbed) {
2551 tPtr->flags.pointerGrabbed = False;
2552 XUngrabPointer(dpy, CurrentTime);
2553 break;
2556 if (tPtr->flags.waitingForSelection)
2557 break;
2559 if (WMIsDraggingFromView(tPtr->view))
2560 WMDragImageFromView(tPtr->view, event);
2565 static void handleEvents(XEvent * event, void *data)
2567 Text *tPtr = (Text *) data;
2569 switch (event->type) {
2570 case Expose:
2572 if (event->xexpose.count != 0)
2573 break;
2575 if (tPtr->hS) {
2576 if (!(W_VIEW(tPtr->hS))->flags.realized)
2577 WMRealizeWidget(tPtr->hS);
2580 if (tPtr->vS) {
2581 if (!(W_VIEW(tPtr->vS))->flags.realized)
2582 WMRealizeWidget(tPtr->vS);
2585 if (tPtr->ruler) {
2586 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2587 WMRealizeWidget(tPtr->ruler);
2591 if (!tPtr->db)
2592 textDidResize(tPtr->view->delegate, tPtr->view);
2594 paintText(tPtr);
2595 break;
2597 case FocusIn:
2598 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2599 != tPtr->view)
2600 return;
2601 tPtr->flags.focused = True;
2602 #if DO_BLINK
2603 if (tPtr->flags.editable && !tPtr->timerID) {
2604 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2606 #endif
2608 break;
2610 case FocusOut:
2611 tPtr->flags.focused = False;
2612 paintText(tPtr);
2613 #if DO_BLINK
2614 if (tPtr->timerID) {
2615 WMDeleteTimerHandler(tPtr->timerID);
2616 tPtr->timerID = NULL;
2618 #endif
2619 break;
2621 case DestroyNotify:
2622 clearText(tPtr);
2623 if (tPtr->db)
2624 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2625 if (tPtr->gfxItems)
2626 WMEmptyArray(tPtr->gfxItems);
2627 #if DO_BLINK
2628 if (tPtr->timerID)
2629 WMDeleteTimerHandler(tPtr->timerID);
2630 #endif
2631 WMReleaseFont(tPtr->dFont);
2632 WMReleaseColor(tPtr->dColor);
2633 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2634 WMRemoveNotificationObserver(tPtr);
2636 WMFreeArray(tPtr->xdndSourceTypes);
2637 WMFreeArray(tPtr->xdndDestinationTypes);
2639 wfree(tPtr);
2641 break;
2646 static void insertPlainText(Text * tPtr, const char *text)
2648 const char *start, *mark;
2649 void *tb = NULL;
2651 start = text;
2652 while (start) {
2653 mark = strchr(start, '\n');
2654 if (mark) {
2655 tb = WMCreateTextBlockWithText(tPtr,
2656 start, tPtr->dFont,
2657 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2658 start = mark + 1;
2659 tPtr->flags.first = True;
2660 } else {
2661 if (start && strlen(start)) {
2662 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2663 tPtr->dColor, tPtr->flags.first, strlen(start));
2664 } else
2665 tb = NULL;
2666 tPtr->flags.first = False;
2667 start = mark;
2670 if (tPtr->flags.prepend)
2671 WMPrependTextBlock(tPtr, tb);
2672 else
2673 WMAppendTextBlock(tPtr, tb);
2677 static void rulerMoveCallBack(WMWidget * w, void *self)
2679 Text *tPtr = (Text *) self;
2681 /* Parameter not used, but tell the compiler that it is ok */
2682 (void) w;
2684 if (!tPtr)
2685 return;
2686 if (W_CLASS(tPtr) != WC_Text)
2687 return;
2689 paintText(tPtr);
2692 static void rulerReleaseCallBack(WMWidget * w, void *self)
2694 Text *tPtr = (Text *) self;
2696 /* Parameter not used, but tell the compiler that it is ok */
2697 (void) w;
2699 if (!tPtr)
2700 return;
2701 if (W_CLASS(tPtr) != WC_Text)
2702 return;
2704 WMThawText(tPtr);
2705 return;
2708 static WMArray *dropDataTypes(WMView * self)
2710 return ((Text *) self->self)->xdndSourceTypes;
2713 static WMDragOperationType wantedDropOperation(WMView * self)
2715 /* Parameter not used, but tell the compiler that it is ok */
2716 (void) self;
2718 return WDOperationCopy;
2721 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2723 /* Parameter not used, but tell the compiler that it is ok */
2724 (void) self;
2726 return (allowedOperation == WDOperationCopy);
2729 static WMData *fetchDragData(WMView * self, char *type)
2731 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2732 char *desc;
2733 WMData *data;
2735 if (strcmp(type, "text/plain")) {
2736 if (!tb)
2737 return NULL;
2739 desc = wmalloc(tb->used + 1);
2740 memcpy(desc, tb->text, tb->used);
2741 desc[tb->used] = 0;
2742 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2744 wfree(desc);
2746 return data;
2749 return NULL;
2752 static WMDragSourceProcs _DragSourceProcs = {
2753 dropDataTypes,
2754 wantedDropOperation,
2755 NULL,
2756 acceptDropOperation,
2757 NULL,
2758 NULL,
2759 fetchDragData
2762 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2764 /* Parameter not used, but tell the compiler that it is ok */
2765 (void) request;
2766 (void) sourceDataTypes;
2768 return ((Text *) self->self)->xdndDestinationTypes;
2771 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2773 /* Parameter not used, but tell the compiler that it is ok */
2774 (void) self;
2775 (void) request;
2776 (void) sourceDataTypes;
2778 return WDOperationCopy;
2781 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2783 WMText *tPtr = (WMText *) self->self;
2784 WMData *data;
2785 char *colorName;
2786 WMColor *color;
2788 /* Parameter not used, but tell the compiler that it is ok */
2789 (void) operations;
2790 (void) dropLocation;
2792 if (tPtr) {
2794 /* only one required type, implies only one drop data */
2796 /* get application/X-color if any */
2797 data = (WMData *) WMPopFromArray(dropData);
2798 if (data != NULL) {
2799 colorName = (char *)WMDataBytes(data);
2800 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2802 if (color) {
2803 WMSetTextSelectionColor(tPtr, color);
2804 WMReleaseColor(color);
2810 static WMDragDestinationProcs _DragDestinationProcs = {
2811 NULL,
2812 requiredDataTypes,
2813 allowedOperation,
2814 NULL,
2815 performDragOperation,
2816 NULL
2819 static char *getStream(WMText * tPtr, int sel, int array)
2821 TextBlock *tb = NULL;
2822 char *text = NULL;
2823 unsigned long where = 0;
2825 if (!tPtr)
2826 return NULL;
2828 if (!(tb = tPtr->firstTextBlock))
2829 return NULL;
2831 if (tPtr->writer) {
2832 (tPtr->writer) (tPtr, (void *)text);
2833 return text;
2836 tb = tPtr->firstTextBlock;
2837 while (tb) {
2839 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2841 if (!sel || (tb->graphic && tb->selected)) {
2843 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2844 && tb != tPtr->firstTextBlock) {
2845 text = wrealloc(text, where + 1);
2846 text[where++] = '\n';
2849 if (tb->blank)
2850 goto _gSnext;
2852 if (tb->graphic && array) {
2853 text = wrealloc(text, where + 4);
2854 text[where++] = 0xFA;
2855 text[where++] = (tb->used >> 8) & 0x0ff;
2856 text[where++] = tb->used & 0x0ff;
2857 text[where++] = tb->allocated; /* extra info */
2859 text = wrealloc(text, where + tb->used);
2860 memcpy(&text[where], tb->text, tb->used);
2861 where += tb->used;
2863 } else if (sel && tb->selected) {
2865 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2866 text = wrealloc(text, where + 1);
2867 text[where++] = '\n';
2870 if (tb->blank)
2871 goto _gSnext;
2873 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2874 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2875 where += tb->s_end - tb->s_begin;
2880 _gSnext: tb = tb->next;
2883 /* +1 for the end of string, let's be nice */
2884 text = wrealloc(text, where + 1);
2885 text[where] = 0;
2886 return text;
2889 static void releaseStreamObjects(void *data)
2891 if (data)
2892 wfree(data);
2895 static WMArray *getStreamObjects(WMText * tPtr, int sel)
2897 WMArray *array;
2898 WMData *data;
2899 char *stream;
2900 unsigned short len;
2901 char *start, *fa, *desc;
2903 stream = getStream(tPtr, sel, 1);
2904 if (!stream)
2905 return NULL;
2907 array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2909 start = stream;
2910 while (start) {
2912 fa = strchr(start, 0xFA);
2913 if (fa) {
2914 if ((int)(fa - start) > 0) {
2915 desc = start;
2916 desc[(int)(fa - start)] = 0;
2917 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2918 WMSetDataFormat(data, TYPETEXT);
2919 WMAddToArray(array, (void *)data);
2922 len = *(fa + 1) * 0xff + *(fa + 2);
2923 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2924 WMSetDataFormat(data, *(fa + 3));
2925 WMAddToArray(array, (void *)data);
2926 start = fa + len + 4;
2928 } else {
2929 if (start && strlen(start)) {
2930 data = WMCreateDataWithBytes((void *)start, strlen(start));
2931 WMSetDataFormat(data, TYPETEXT);
2932 WMAddToArray(array, (void *)data);
2934 start = fa;
2938 wfree(stream);
2939 return array;
2942 #define XDND_TEXT_DATA_TYPE "text/plain"
2943 #define XDND_COLOR_DATA_TYPE "application/X-color"
2944 static WMArray *getXdndSourceTypeArray(void)
2946 WMArray *types = WMCreateArray(1);
2947 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2948 return types;
2951 static WMArray *getXdndDestinationTypeArray(void)
2953 WMArray *types = WMCreateArray(1);
2954 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2955 return types;
2958 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2960 Text *tPtr;
2961 Display *dpy;
2962 WMScreen *scr;
2963 XGCValues gcv;
2965 tPtr = wmalloc(sizeof(Text));
2966 tPtr->widgetClass = WC_Text;
2967 tPtr->view = W_CreateView(W_VIEW(parent));
2968 if (!tPtr->view) {
2969 perror("could not create text's view\n");
2970 wfree(tPtr);
2971 return NULL;
2974 dpy = tPtr->view->screen->display;
2975 scr = tPtr->view->screen;
2977 tPtr->view->self = tPtr;
2978 tPtr->view->attribs.cursor = scr->textCursor;
2979 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2980 W_ResizeView(tPtr->view, 250, 200);
2982 tPtr->dColor = WMBlackColor(scr);
2983 tPtr->fgColor = WMBlackColor(scr);
2984 tPtr->bgColor = WMWhiteColor(scr);
2985 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2987 gcv.graphics_exposures = False;
2988 gcv.foreground = W_PIXEL(scr->gray);
2989 gcv.background = W_PIXEL(scr->darkGray);
2990 gcv.fill_style = FillStippled;
2991 /* why not use scr->stipple here? */
2992 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2993 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2994 GCForeground | GCBackground | GCStipple
2995 | GCFillStyle | GCGraphicsExposures, &gcv);
2997 tPtr->ruler = NULL;
2998 tPtr->vS = NULL;
2999 tPtr->hS = NULL;
3001 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3003 tPtr->view->delegate = &_TextViewDelegate;
3005 tPtr->delegate = NULL;
3007 #if DO_BLINK
3008 tPtr->timerID = NULL;
3009 #endif
3011 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
3012 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
3014 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
3015 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
3017 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
3019 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3020 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3023 WMArray *types = WMCreateArray(2);
3024 WMAddToArray(types, "application/X-color");
3025 WMAddToArray(types, "application/X-image");
3026 WMRegisterViewForDraggedTypes(tPtr->view, types);
3027 WMFreeArray(types);
3030 /*WMAddNotificationObserver(fontChanged, tPtr,
3031 WMFontPanelDidChangeNotification, tPtr); */
3033 tPtr->firstTextBlock = NULL;
3034 tPtr->lastTextBlock = NULL;
3035 tPtr->currentTextBlock = NULL;
3036 tPtr->tpos = 0;
3038 tPtr->gfxItems = WMCreateArray(4);
3040 tPtr->parser = parser;
3041 tPtr->writer = writer;
3043 tPtr->sel.x = tPtr->sel.y = 2;
3044 tPtr->sel.w = tPtr->sel.h = 0;
3046 tPtr->clicked.x = tPtr->clicked.y = 2;
3048 tPtr->visible.x = tPtr->visible.y = 2;
3049 tPtr->visible.h = tPtr->view->size.height;
3050 tPtr->visible.w = tPtr->view->size.width - 4;
3052 tPtr->cursor.x = -23;
3054 tPtr->docWidth = 0;
3055 tPtr->docHeight = 0;
3056 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3057 tPtr->db = (Pixmap) NULL;
3058 tPtr->bgPixmap = NULL;
3060 tPtr->margins = WMGetRulerMargins(NULL);
3061 tPtr->margins->right = tPtr->visible.w;
3062 tPtr->nMargins = 1;
3064 tPtr->flags.rulerShown = False;
3065 tPtr->flags.monoFont = False;
3066 tPtr->flags.focused = False;
3067 tPtr->flags.editable = True;
3068 tPtr->flags.ownsSelection = False;
3069 tPtr->flags.pointerGrabbed = False;
3070 tPtr->flags.extendSelection = False;
3071 tPtr->flags.frozen = False;
3072 tPtr->flags.cursorShown = True;
3073 tPtr->flags.acceptsGraphic = False;
3074 tPtr->flags.horizOnDemand = False;
3075 tPtr->flags.needsLayOut = False;
3076 tPtr->flags.ignoreNewLine = False;
3077 tPtr->flags.indentNewLine = False;
3078 tPtr->flags.laidOut = False;
3079 tPtr->flags.ownsSelection = False;
3080 tPtr->flags.waitingForSelection = False;
3081 tPtr->flags.prepend = False;
3082 tPtr->flags.isOverGraphic = False;
3083 tPtr->flags.relief = WRSunken;
3084 tPtr->flags.isOverGraphic = 0;
3085 tPtr->flags.alignment = WALeft;
3086 tPtr->flags.first = True;
3088 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3089 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3091 return tPtr;
3094 void WMPrependTextStream(WMText * tPtr, const char *text)
3096 CHECK_CLASS(tPtr, WC_Text);
3098 if (!text) {
3099 if (tPtr->flags.ownsSelection)
3100 releaseSelection(tPtr);
3101 clearText(tPtr);
3102 updateScrollers(tPtr);
3103 return;
3106 tPtr->flags.prepend = True;
3107 if (text && tPtr->parser)
3108 (tPtr->parser) (tPtr, (void *)text);
3109 else
3110 insertPlainText(tPtr, text);
3112 tPtr->flags.needsLayOut = True;
3113 tPtr->tpos = 0;
3114 if (!tPtr->flags.frozen) {
3115 layOutDocument(tPtr);
3119 void WMAppendTextStream(WMText * tPtr, const char *text)
3121 CHECK_CLASS(tPtr, WC_Text);
3123 if (!text) {
3124 if (tPtr->flags.ownsSelection)
3125 releaseSelection(tPtr);
3126 clearText(tPtr);
3127 updateScrollers(tPtr);
3128 return;
3131 tPtr->flags.prepend = False;
3132 if (text && tPtr->parser)
3133 (tPtr->parser) (tPtr, (void *)text);
3134 else
3135 insertPlainText(tPtr, text);
3137 tPtr->flags.needsLayOut = True;
3138 if (tPtr->currentTextBlock) {
3139 if (tPtr->currentTextBlock->graphic)
3140 tPtr->tpos = 1;
3141 else
3142 tPtr->tpos = tPtr->currentTextBlock->used;
3145 if (!tPtr->flags.frozen) {
3146 layOutDocument(tPtr);
3150 char *WMGetTextStream(WMText * tPtr)
3152 CHECK_CLASS(tPtr, WC_Text);
3154 return getStream(tPtr, 0, 0);
3157 char *WMGetTextSelectedStream(WMText * tPtr)
3159 CHECK_CLASS(tPtr, WC_Text);
3161 return getStream(tPtr, 1, 0);
3164 WMArray *WMGetTextObjects(WMText * tPtr)
3166 CHECK_CLASS(tPtr, WC_Text);
3168 return getStreamObjects(tPtr, 0);
3171 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3173 CHECK_CLASS(tPtr, WC_Text);
3175 return getStreamObjects(tPtr, 1);
3178 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3180 CHECK_CLASS(tPtr, WC_Text);
3182 tPtr->delegate = delegate;
3185 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3186 const char *description, WMColor * color,
3187 unsigned short first, unsigned short extraInfo)
3189 TextBlock *tb;
3191 if (!w || !description || !color)
3192 return NULL;
3194 tb = wmalloc(sizeof(TextBlock));
3196 tb->text = wstrdup(description);
3197 tb->used = strlen(description);
3198 tb->blank = False;
3199 tb->d.widget = w;
3200 tb->color = WMRetainColor(color);
3201 tb->marginN = newMargin(tPtr, NULL);
3202 tb->allocated = extraInfo;
3203 tb->first = first;
3204 tb->kanji = False;
3205 tb->graphic = True;
3206 tb->object = True;
3207 tb->underlined = False;
3208 tb->selected = False;
3209 tb->script = 0;
3210 tb->sections = NULL;
3211 tb->nsections = 0;
3212 tb->prior = NULL;
3213 tb->next = NULL;
3215 return tb;
3218 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3219 const char *description, WMColor * color,
3220 unsigned short first, unsigned short extraInfo)
3222 TextBlock *tb;
3224 if (!p || !description || !color)
3225 return NULL;
3227 tb = wmalloc(sizeof(TextBlock));
3229 tb->text = wstrdup(description);
3230 tb->used = strlen(description);
3231 tb->blank = False;
3232 tb->d.pixmap = WMRetainPixmap(p);
3233 tb->color = WMRetainColor(color);
3234 tb->marginN = newMargin(tPtr, NULL);
3235 tb->allocated = extraInfo;
3236 tb->first = first;
3237 tb->kanji = False;
3238 tb->graphic = True;
3239 tb->object = False;
3240 tb->underlined = False;
3241 tb->selected = False;
3242 tb->script = 0;
3243 tb->sections = NULL;
3244 tb->nsections = 0;
3245 tb->prior = NULL;
3246 tb->next = NULL;
3248 return tb;
3251 void *WMCreateTextBlockWithText(WMText * tPtr, const char *text, WMFont * font, WMColor * color,
3252 unsigned short first, unsigned short len)
3254 TextBlock *tb;
3256 if (!font || !color)
3257 return NULL;
3259 tb = wmalloc(sizeof(TextBlock));
3261 tb->allocated = reqBlockSize(len);
3262 tb->text = (char *)wmalloc(tb->allocated);
3264 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3265 *tb->text = ' ';
3266 tb->used = 1;
3267 tb->blank = True;
3268 } else {
3269 memcpy(tb->text, text, len);
3270 tb->used = len;
3271 tb->blank = False;
3273 tb->text[tb->used] = 0;
3275 tb->d.font = WMRetainFont(font);
3276 tb->color = WMRetainColor(color);
3277 tb->marginN = newMargin(tPtr, NULL);
3278 tb->first = first;
3279 tb->kanji = False;
3280 tb->graphic = False;
3281 tb->underlined = False;
3282 tb->selected = False;
3283 tb->script = 0;
3284 tb->sections = NULL;
3285 tb->nsections = 0;
3286 tb->prior = NULL;
3287 tb->next = NULL;
3288 return tb;
3291 void
3292 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3293 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3295 TextBlock *tb = (TextBlock *) vtb;
3296 if (!tb)
3297 return;
3299 tb->first = first;
3300 tb->kanji = kanji;
3301 tb->underlined = underlined;
3302 tb->script = script;
3303 tb->marginN = newMargin(tPtr, margins);
3306 void
3307 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3308 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins *margins)
3310 TextBlock *tb = (TextBlock *) vtb;
3311 if (!tb)
3312 return;
3314 if (first)
3315 *first = tb->first;
3316 if (kanji)
3317 *kanji = tb->kanji;
3318 if (underlined)
3319 *underlined = tb->underlined;
3320 if (script)
3321 *script = tb->script;
3322 if (margins)
3323 *margins = tPtr->margins[tb->marginN];
3326 static int prepareTextBlock(WMText *tPtr, TextBlock *tb)
3328 if (tb->graphic) {
3329 if (tb->object) {
3330 WMWidget *w = tb->d.widget;
3331 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3332 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3333 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3336 WMAddToArray(tPtr->gfxItems, (void *)tb);
3337 tPtr->tpos = 1;
3339 } else {
3340 tPtr->tpos = tb->used;
3343 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3344 tb->next = tb->prior = NULL;
3345 tb->first = True;
3346 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3347 return 0;
3350 if (!tb->first) {
3351 tb->marginN = tPtr->currentTextBlock->marginN;
3354 return 1;
3358 void WMPrependTextBlock(WMText *tPtr, void *vtb)
3360 TextBlock *tb = (TextBlock *) vtb;
3362 if (!tb || !prepareTextBlock(tPtr, tb))
3363 return;
3365 tb->next = tPtr->currentTextBlock;
3366 tb->prior = tPtr->currentTextBlock->prior;
3367 if (tPtr->currentTextBlock->prior)
3368 tPtr->currentTextBlock->prior->next = tb;
3370 tPtr->currentTextBlock->prior = tb;
3371 if (!tb->prior)
3372 tPtr->firstTextBlock = tb;
3374 tPtr->currentTextBlock = tb;
3377 void WMAppendTextBlock(WMText *tPtr, void *vtb)
3379 TextBlock *tb = (TextBlock *) vtb;
3381 if (!tb || !prepareTextBlock(tPtr, tb))
3382 return;
3384 tb->next = tPtr->currentTextBlock->next;
3385 tb->prior = tPtr->currentTextBlock;
3386 if (tPtr->currentTextBlock->next)
3387 tPtr->currentTextBlock->next->prior = tb;
3389 tPtr->currentTextBlock->next = tb;
3391 if (!tb->next)
3392 tPtr->lastTextBlock = tb;
3394 tPtr->currentTextBlock = tb;
3397 void *WMRemoveTextBlock(WMText * tPtr)
3399 TextBlock *tb = NULL;
3401 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3402 return NULL;
3405 tb = tPtr->currentTextBlock;
3406 if (tb->graphic) {
3407 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3409 if (tb->object) {
3410 WMUnmapWidget(tb->d.widget);
3414 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3415 if (tPtr->currentTextBlock->next)
3416 tPtr->currentTextBlock->next->prior = NULL;
3418 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3419 tPtr->currentTextBlock = tPtr->firstTextBlock;
3421 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3422 tPtr->currentTextBlock->prior->next = NULL;
3423 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3424 tPtr->currentTextBlock = tPtr->lastTextBlock;
3425 } else {
3426 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3427 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3428 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3431 return (void *)tb;
3434 #if 0
3435 static void destroyWidget(WMWidget * widget)
3437 WMDestroyWidget(widget);
3438 // -- never do this -- wfree(widget);
3440 #endif
3442 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3444 TextBlock *tb = (TextBlock *) vtb;
3446 /* Parameter not used, but tell the compiler that it is ok */
3447 (void) tPtr;
3449 if (!tb)
3450 return;
3452 if (tb->graphic) {
3453 if (tb->object) {
3454 /* naturally, there's a danger to destroying widgets whose action
3455 * brings us here: ie. press a button to destroy it...
3456 * need to find a safer way. till then... this stays commented out */
3457 /* 5 months later... destroy it 10 seconds after now which should
3458 * be enough time for the widget's action to be completed... :-) */
3459 /* This is a bad assumption. Just destroy the widget here.
3460 * if the caller needs it, it can protect it with W_RetainView()
3461 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3462 WMDestroyWidget(tb->d.widget);
3463 } else {
3464 WMReleasePixmap(tb->d.pixmap);
3466 } else {
3467 WMReleaseFont(tb->d.font);
3470 WMReleaseColor(tb->color);
3471 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3472 if (tb->sections)
3473 wfree(tb->sections);
3474 wfree(tb->text);
3475 wfree(tb);
3478 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3480 if (tPtr->fgColor)
3481 WMReleaseColor(tPtr->fgColor);
3483 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3485 paintText(tPtr);
3488 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3490 if (tPtr->bgColor)
3491 WMReleaseColor(tPtr->bgColor);
3493 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3494 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3496 paintText(tPtr);
3499 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3501 if (tPtr->bgPixmap)
3502 WMReleasePixmap(tPtr->bgPixmap);
3504 if (pixmap)
3505 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3506 else
3507 tPtr->bgPixmap = NULL;
3510 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3512 tPtr->flags.relief = relief;
3513 textDidResize(tPtr->view->delegate, tPtr->view);
3516 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3518 if (shouldhave && !tPtr->hS) {
3519 tPtr->hS = WMCreateScroller(tPtr);
3520 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3521 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3522 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3523 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3524 WMMapWidget(tPtr->hS);
3525 } else if (!shouldhave && tPtr->hS) {
3526 WMUnmapWidget(tPtr->hS);
3527 WMDestroyWidget(tPtr->hS);
3528 tPtr->hS = NULL;
3531 tPtr->hpos = 0;
3532 tPtr->prevHpos = 0;
3533 textDidResize(tPtr->view->delegate, tPtr->view);
3536 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3538 if (shouldhave && !tPtr->ruler) {
3539 tPtr->ruler = WMCreateRuler(tPtr);
3540 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3541 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3542 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3543 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3544 } else if (!shouldhave && tPtr->ruler) {
3545 WMShowTextRuler(tPtr, False);
3546 WMDestroyWidget(tPtr->ruler);
3547 tPtr->ruler = NULL;
3549 textDidResize(tPtr->view->delegate, tPtr->view);
3552 void WMShowTextRuler(WMText * tPtr, Bool show)
3554 if (!tPtr->ruler)
3555 return;
3557 if (tPtr->flags.monoFont)
3558 show = False;
3560 tPtr->flags.rulerShown = show;
3561 if (show) {
3562 WMMapWidget(tPtr->ruler);
3563 } else {
3564 WMUnmapWidget(tPtr->ruler);
3567 textDidResize(tPtr->view->delegate, tPtr->view);
3570 Bool WMGetTextRulerShown(WMText * tPtr)
3572 if (!tPtr->ruler)
3573 return False;
3575 return tPtr->flags.rulerShown;
3578 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3580 if (shouldhave && !tPtr->vS) {
3581 tPtr->vS = WMCreateScroller(tPtr);
3582 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3583 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3584 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3585 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3586 WMMapWidget(tPtr->vS);
3587 } else if (!shouldhave && tPtr->vS) {
3588 WMUnmapWidget(tPtr->vS);
3589 WMDestroyWidget(tPtr->vS);
3590 tPtr->vS = NULL;
3593 tPtr->vpos = 0;
3594 tPtr->prevVpos = 0;
3595 textDidResize(tPtr->view->delegate, tPtr->view);
3598 Bool WMScrollText(WMText * tPtr, int amount)
3600 Bool scroll = False;
3602 if (amount == 0 || !tPtr->view->flags.realized)
3603 return False;
3605 if (amount < 0) {
3606 if (tPtr->vpos > 0) {
3607 if (tPtr->vpos > abs(amount))
3608 tPtr->vpos += amount;
3609 else
3610 tPtr->vpos = 0;
3611 scroll = True;
3613 } else {
3614 int limit = tPtr->docHeight - tPtr->visible.h;
3615 if (tPtr->vpos < limit) {
3616 if (tPtr->vpos < limit - amount)
3617 tPtr->vpos += amount;
3618 else
3619 tPtr->vpos = limit;
3620 scroll = True;
3624 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3625 updateScrollers(tPtr);
3626 paintText(tPtr);
3628 tPtr->prevVpos = tPtr->vpos;
3629 return scroll;
3632 Bool WMPageText(WMText * tPtr, Bool direction)
3634 if (!tPtr->view->flags.realized)
3635 return False;
3637 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3640 void WMSetTextEditable(WMText * tPtr, Bool editable)
3642 tPtr->flags.editable = editable;
3645 int WMGetTextEditable(WMText * tPtr)
3647 return tPtr->flags.editable;
3650 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3652 tPtr->flags.indentNewLine = indent;
3655 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3657 tPtr->flags.ignoreNewLine = ignore;
3660 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3662 return tPtr->flags.ignoreNewLine;
3665 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3667 if (mono) {
3668 if (tPtr->flags.rulerShown)
3669 WMShowTextRuler(tPtr, False);
3670 if (tPtr->flags.alignment != WALeft)
3671 tPtr->flags.alignment = WALeft;
3674 tPtr->flags.monoFont = mono;
3675 textDidResize(tPtr->view->delegate, tPtr->view);
3678 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3680 return tPtr->flags.monoFont;
3683 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3685 if (tPtr->dFont)
3686 WMReleaseFont(tPtr->dFont);
3688 if (font) {
3689 tPtr->dFont = WMRetainFont(font);
3690 } else {
3691 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3695 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3697 return WMRetainFont(tPtr->dFont);
3700 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3702 if (tPtr->dColor)
3703 WMReleaseColor(tPtr->dColor);
3705 if (color) {
3706 tPtr->dColor = WMRetainColor(color);
3707 } else {
3708 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3712 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3714 return tPtr->dColor;
3717 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3719 if (tPtr->flags.monoFont)
3720 tPtr->flags.alignment = WALeft;
3721 else
3722 tPtr->flags.alignment = alignment;
3723 WMThawText(tPtr);
3726 int WMGetTextInsertType(WMText * tPtr)
3728 return tPtr->flags.prepend;
3731 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3733 setSelectionProperty(tPtr, NULL, color, -1);
3736 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3738 TextBlock *tb;
3740 tb = tPtr->currentTextBlock;
3742 if (!tb || !tPtr->flags.ownsSelection)
3743 return NULL;
3745 if (!tb->selected)
3746 return NULL;
3748 return tb->color;
3751 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3753 setSelectionProperty(tPtr, font, NULL, -1);
3756 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3758 TextBlock *tb;
3760 tb = tPtr->currentTextBlock;
3762 if (!tb || !tPtr->flags.ownsSelection)
3763 return NULL;
3765 if (!tb->selected)
3766 return NULL;
3768 if (tb->graphic) {
3769 tb = getFirstNonGraphicBlockFor(tb, 1);
3770 if (!tb)
3771 return NULL;
3773 return (tb->selected ? tb->d.font : NULL);
3776 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3778 /* // check this */
3779 if (underlined != 0 && underlined != 1)
3780 return;
3782 setSelectionProperty(tPtr, NULL, NULL, underlined);
3785 int WMGetTextSelectionUnderlined(WMText * tPtr)
3787 TextBlock *tb;
3789 tb = tPtr->currentTextBlock;
3791 if (!tb || !tPtr->flags.ownsSelection)
3792 return 0;
3794 if (!tb->selected)
3795 return 0;
3797 return tb->underlined;
3800 void WMFreezeText(WMText * tPtr)
3802 tPtr->flags.frozen = True;
3805 void WMThawText(WMText * tPtr)
3807 tPtr->flags.frozen = False;
3809 if (tPtr->flags.monoFont) {
3810 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3811 TextBlock *tb;
3813 /* make sure to unmap widgets no matter where they are */
3814 /* they'll be later remapped if needed by paintText */
3815 for (j = 0; j < c; j++) {
3816 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3817 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3818 WMUnmapWidget(tb->d.widget);
3823 tPtr->flags.laidOut = False;
3824 layOutDocument(tPtr);
3825 updateScrollers(tPtr);
3826 paintText(tPtr);
3827 tPtr->flags.needsLayOut = False;
3831 /* find first occurence of a string */
3832 static const char *mystrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3834 const char *ptr;
3836 if (!haystack || !needle || !end)
3837 return NULL;
3839 for (ptr = haystack; ptr < end; ptr++) {
3840 if (caseSensitive) {
3841 if (*ptr == *needle && !strncmp(ptr, needle, len))
3842 return ptr;
3844 } else {
3845 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3846 return ptr;
3850 return NULL;
3853 /* find last occurence of a string */
3854 static const char *mystrrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3856 const char *ptr;
3858 if (!haystack || !needle || !end)
3859 return NULL;
3861 for (ptr = haystack - 2; ptr > end; ptr--) {
3862 if (caseSensitive) {
3863 if (*ptr == *needle && !strncmp(ptr, needle, len))
3864 return ptr;
3865 } else {
3866 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3867 return ptr;
3871 return NULL;
3874 Bool WMFindInTextStream(WMText * tPtr, const char *needle, Bool direction, Bool caseSensitive)
3876 TextBlock *tb;
3877 const char *mark = NULL;
3878 unsigned short pos;
3880 #if 0
3881 if (!(tb = tPtr->currentTextBlock)) {
3882 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3883 return False;
3885 } else {
3886 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3887 tb = (direction>0) ? tb->next : tb->prior; */
3888 if (tb != tPtr->lastTextBlock)
3889 tb = tb->prior;
3891 #endif
3892 tb = tPtr->currentTextBlock;
3893 pos = tPtr->tpos;
3895 while (tb) {
3896 if (!tb->graphic) {
3898 if (direction > 0) {
3899 if (pos + 1 < tb->used)
3900 pos++;
3902 if (tb->used - pos > 0 && pos > 0) {
3903 mark = mystrstr(&tb->text[pos], needle,
3904 strlen(needle), &tb->text[tb->used], caseSensitive);
3906 } else {
3907 tb = tb->next;
3908 pos = 0;
3909 continue;
3912 } else {
3913 if (pos - 1 > 0)
3914 pos--;
3916 if (pos > 0) {
3917 mark = mystrrstr(&tb->text[pos], needle,
3918 strlen(needle), tb->text, caseSensitive);
3919 } else {
3920 tb = tb->prior;
3921 if (!tb)
3922 return False;
3923 pos = tb->used;
3924 continue;
3928 if (mark) {
3929 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3931 tPtr->tpos = (int)(mark - tb->text);
3932 tPtr->currentTextBlock = tb;
3933 updateCursorPosition(tPtr);
3934 tPtr->sel.y = tPtr->cursor.y + 5;
3935 tPtr->sel.h = tPtr->cursor.h - 10;
3936 tPtr->sel.x = tPtr->cursor.x + 1;
3937 tPtr->sel.w = WMIN(WMWidthOfString(font,
3938 &tb->text[tPtr->tpos], strlen(needle)),
3939 tPtr->docWidth - tPtr->sel.x);
3940 tPtr->flags.ownsSelection = True;
3941 paintText(tPtr);
3943 return True;
3947 tb = (direction > 0) ? tb->next : tb->prior;
3948 if (tb) {
3949 pos = (direction > 0) ? 0 : tb->used;
3953 return False;
3956 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3958 if (!tPtr->flags.ownsSelection)
3959 return False;
3961 removeSelection(tPtr);
3963 if (replacement) {
3964 insertTextInteractively(tPtr, replacement, strlen(replacement));
3965 updateCursorPosition(tPtr);
3966 paintText(tPtr);
3969 return True;