fixed arrow navigation quirks and color mem leaks
[wmaker-crm.git] / WINGs / wtext.c
blobd5a78ffa13b3c45cc0924e0bbc4991fe66b8e28d
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
5 #include "WINGsP.h"
6 #include <ctype.h>
7 #include <X11/keysym.h>
8 #include <X11/Xatom.h>
10 #define DO_BLINK 0
12 /* TODO:
13 * - verify what happens with XK_return in insertTextInt...
14 * - selection code... selects can be funny if it crosses over. use rect?
15 * - also inspect behaviour for WACenter and WARight
16 * - what if a widget grabs the click... howto say: "pressed me"?
17 * note that WMCreateEventHandler takes one data, but need widget & tPtr
18 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
19 * - check if support for Horizontal Scroll is complete
20 * - assess danger of destroying widgets whose actions link to other pages
21 * - Tabs now are simply replaced by 4 spaces...
22 * - redo blink code to reduce paint event... use pixmap buffer...
23 * - add paragraph support (full) and '\n' code in getStream..
27 /* a Section is a section of a TextBlock that describes what parts
28 of a TextBlock has been laid out on which "line"...
29 o this greatly aids redraw, scroll and selection.
30 o this is created during layoutLine, but may be later modified.
31 o there may be many Sections per TextBlock, hence the array */
32 typedef struct {
33 unsigned int x, y; /* where to draw it from */
34 unsigned short w, h; /* its width and height */
35 unsigned short begin; /* where the layout begins */
36 unsigned short end ; /* where it ends */
37 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
38 unsigned short last:1; /* is it the last section on a "line"? */
39 unsigned int _y:31; /* the "line" it and other textblocks are on */
40 } Section;
43 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
44 o text for the block, color and font
45 o or a pointer to the pixmap
46 o OR a pointer to the widget and the (text) description for its graphic
49 typedef struct _TextBlock {
50 struct _TextBlock *next; /* next text block in linked list */
51 struct _TextBlock *prior; /* prior text block in linked list */
53 char *text; /* pointer to text (could be kanji) */
54 /* or to the object's description */
55 union {
56 WMFont *font; /* the font */
57 WMWidget *widget; /* the embedded widget */
58 WMPixmap *pixmap; /* the pixmap */
59 } d; /* description */
61 unsigned short used; /* number of chars in this block */
62 unsigned short allocated; /* size of allocation (in chars) */
63 WMColor *color; /* the color */
65 Section *sections; /* the region for layouts (a growable array) */
66 /* an _array_! of size _nsections_ */
68 unsigned short s_begin; /* where the selection begins */
69 unsigned short s_end; /* where it ends */
71 unsigned int first:1; /* first TextBlock in paragraph */
72 unsigned int blank:1; /* ie. blank paragraph */
73 unsigned int kanji:1; /* is of 16-bit characters or not */
74 unsigned int graphic:1; /* graphic or text: text=0 */
75 unsigned int object:1; /* embedded object or pixmap */
76 unsigned int underlined:1; /* underlined or not */
77 unsigned int selected:1; /* selected or not */
78 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
79 int script:8; /* script in points: negative for subscript */
80 unsigned int marginN:8; /* which of the margins in the tPtr to use */
81 unsigned int nClicks:2; /* single, double, triple clicks */
82 unsigned int RESERVED:7;
83 } TextBlock;
86 /* I'm lazy: visible.h vs. visible.size.height :-) */
87 typedef struct {
88 int y, x, h, w;
89 } myRect;
92 typedef struct W_Text {
93 W_Class widgetClass; /* the class number of this widget */
94 W_View *view; /* the view referring to this instance */
96 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
98 WMScroller *vS; /* the vertical scroller */
99 unsigned int vpos; /* the current vertical position */
100 unsigned int prevVpos; /* the previous vertical position */
102 WMScroller *hS; /* the horizontal scroller */
103 unsigned int hpos; /* the current horizontal position */
104 unsigned int prevHpos; /* the previous horizontal position */
106 WMFont *dFont; /* the default font */
107 WMColor *dColor; /* the default color */
108 WMPixmap *dBulletPix; /* the default pixmap for bullets */
110 GC bgGC; /* the background GC to draw with */
111 GC fgGC; /* the foreground GC to draw with */
112 Pixmap db; /* the buffer on which to draw */
113 WMPixmap *bgPixmap; /* the background pixmap */
115 myRect visible; /* the actual rectangle that can be drawn into */
116 myRect cursor; /* the position and (height) of cursor */
117 myRect sel; /* the selection rectangle */
119 WMPoint clicked; /* where in the _document_ was clicked */
121 unsigned short tpos; /* the position in the currentTextBlock */
122 unsigned short docWidth; /* the width of the entire document */
123 unsigned int docHeight; /* the height of the entire document */
125 TextBlock *firstTextBlock;
126 TextBlock *lastTextBlock;
127 TextBlock *currentTextBlock;
129 WMArray *gfxItems; /* a nice array of graphic items */
131 #if DO_BLINK
132 WMHandlerID timerID; /* for nice twinky-winky */
133 #endif
135 WMAction *parser;
136 WMAction *writer;
137 WMTextDelegate *delegate;
138 Time lastClickTime;
140 WMRulerMargins *margins; /* an array of margins */
142 unsigned int nMargins:7; /* the total number of margins in use */
143 struct {
144 unsigned int monoFont:1; /* whether to ignore formats and graphic */
145 unsigned int focused:1; /* whether this instance has input focus */
146 unsigned int editable:1; /* "silly user, you can't edit me" */
147 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
148 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
149 unsigned int extendSelection:1; /* shift-drag to select more regions */
151 unsigned int rulerShown:1; /* whether the ruler is shown or not */
152 unsigned int frozen:1; /* whether screen updates are to be made */
153 unsigned int cursorShown:1; /* whether to show the cursor */
154 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
155 unsigned int horizOnDemand:1;/* if a large image should appear*/
156 unsigned int needsLayOut:1; /* in case of Append/Deletes */
157 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
158 unsigned int indentNewLine:1;/* add " " for a newline typed */
159 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
160 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
161 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
162 WMAlignment alignment:2; /* the alignment for text */
163 WMReliefType relief:3; /* the relief to display with */
164 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
165 unsigned int first:1; /* for plain text parsing, newline? */
166 /* unsigned int RESERVED:1; */
167 } flags;
168 } Text;
171 #define NOTIFY(T,C,N,A) {\
172 WMNotification *notif = WMCreateNotification(N,T,A);\
173 if ((T)->delegate && (T)->delegate->C)\
174 (*(T)->delegate->C)((T)->delegate,notif);\
175 WMPostNotification(notif);\
176 WMReleaseNotification(notif);}
179 #define TYPETEXT 0
181 /* just to print blocks of text not terminated by \0 */
182 static void
183 output(char *ptr, int len)
185 char s[len+1];
186 memcpy(s, ptr, len);
187 s[len] = 0;
188 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
189 printf("[%s]\n", s);
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
198 static char *default_bullet[] = {
199 "6 6 4 1",
200 " c None s None", ". c black",
201 "X c white", "o c #808080",
202 " ... ",
203 ".XX.. ",
204 ".XX..o",
205 ".....o",
206 " ...oo",
207 " ooo "};
209 static void handleEvents(XEvent *event, void *data);
210 static void layOutDocument(Text *tPtr);
211 static void updateScrollers(Text *tPtr);
214 static int
215 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
217 unsigned int i=0;
219 for(i=0; i < tPtr->nMargins; i++) {
221 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
222 return i;
225 return -1;
230 static int
231 newMargin(Text *tPtr, WMRulerMargins *margins)
233 int n;
235 if (!margins) {
236 tPtr->margins[0].retainCount++;
237 return 0;
240 n = getMarginNumber(tPtr, margins);
242 if (n == -1) {
244 if(tPtr->nMargins >= 127) {
245 n = tPtr->nMargins-1;
246 return n;
249 tPtr->margins = wrealloc(tPtr->margins,
250 (++tPtr->nMargins)*sizeof(WMRulerMargins));
252 n = tPtr->nMargins-1;
253 tPtr->margins[n].left = margins->left;
254 tPtr->margins[n].first = margins->first;
255 tPtr->margins[n].body = margins->body;
256 tPtr->margins[n].right = margins->right;
257 /* for each tab... */
258 tPtr->margins[n].retainCount = 1;
259 } else {
260 tPtr->margins[n].retainCount++;
263 return n;
266 static Bool
267 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
269 unsigned short i, w, lw, selected = False, extend = False;
270 myRect sel;
273 /* if selection rectangle completely encloses the section */
274 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
275 && (tb->sections[s]._y + tb->sections[s].h
276 <= 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
284 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
285 && (tb->sections[s]._y + tb->sections[s].h
286 >= tPtr->visible.y + tPtr->sel.y) ) {
287 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
288 sel.w = tPtr->visible.w;
289 selected = extend = True;
291 /* or if it begins before a line, but ends on it */
292 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
293 && (tb->sections[s]._y + tb->sections[s].h
294 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
295 && (tb->sections[s]._y
296 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
298 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
299 sel.w = tPtr->sel.x + tPtr->sel.w;
300 else
301 sel.w = tPtr->sel.x;
303 sel.x = 0;
304 selected = True;
306 /* or if the selection rectangle lies entirely within a line */
307 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
308 && (tPtr->sel.w >= 2)
309 && (tb->sections[s]._y + tb->sections[s].h
310 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
311 sel.x = tPtr->sel.x;
312 sel.w = tPtr->sel.w;
313 selected = True;
316 if (selected) {
317 selected = False;
319 /* if not within (modified) selection rectangle */
320 if ( tb->sections[s].x > sel.x + sel.w
321 || tb->sections[s].x + tb->sections[s].w < sel.x)
322 return False;
324 if (tb->graphic) {
325 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
326 && tb->sections[s].x >= sel.x) {
327 rect->width = tb->sections[s].w;
328 rect->x = tb->sections[s].x;
329 selected = True;
331 } else {
333 i = tb->sections[s].begin;
334 lw = 0;
336 if (0&& tb->sections[s].x >= sel.x) {
337 tb->s_begin = tb->sections[s].begin;
338 goto _selEnd;
341 while (++i <= tb->sections[s].end) {
343 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
344 lw += w;
346 if (lw + tb->sections[s].x >= sel.x
347 || i == tb->sections[s].end ) {
348 lw -= w;
349 i--;
350 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
351 break;
355 if (i > tb->sections[s].end) {
356 printf("WasSelected: (i > tb->sections[s].end) \n");
357 return False;
360 _selEnd: rect->x = tb->sections[s].x + lw;
361 lw = 0;
362 while(++i <= tb->sections[s].end) {
364 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
365 lw += w;
367 if (lw + rect->x >= sel.x + sel.w
368 || i == tb->sections[s].end ) {
370 if (i != tb->sections[s].end) {
371 lw -= w;
372 i--;
375 rect->width = lw;
376 if (tb->sections[s].last && sel.x + sel.w
377 >= tb->sections[s].x + tb->sections[s].w
378 && extend ) {
379 rect->width += (tPtr->visible.w - rect->x - lw);
382 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
383 selected = True;
384 break;
390 if (selected) {
391 rect->y = tb->sections[s]._y - tPtr->vpos;
392 rect->height = tb->sections[s].h;
393 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
395 return selected;
399 static void
400 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
402 TextBlock *tb;
403 int isFont=False;
405 tb = tPtr->firstTextBlock;
406 if (!tb || !tPtr->flags.ownsSelection)
407 return;
409 if(font && (!color || underlined==-1))
410 isFont = True;
412 while (tb) {
413 if (tPtr->flags.monoFont || tb->selected) {
415 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
416 || tb->graphic) {
418 if(isFont) {
419 if(!tb->graphic) {
420 WMReleaseFont(tb->d.font);
421 tb->d.font = WMRetainFont(font);
423 } else if(underlined !=-1) {
424 tb->underlined = underlined;
425 } else {
426 WMReleaseColor(tb->color);
427 tb->color = WMRetainColor(color);
430 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
432 TextBlock *midtb, *otb = tb;
434 if(underlined != -1) {
435 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
436 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
437 False, (tb->s_end - tb->s_begin));
438 } else {
439 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
440 &(tb->text[tb->s_begin]),
441 (isFont?font:tb->d.font),
442 (isFont?tb->color:color),
443 False, (tb->s_end - tb->s_begin));
447 if (midtb) {
448 if(underlined != -1) {
449 midtb->underlined = underlined;
450 } else {
451 midtb->underlined = otb->underlined;
454 midtb->selected = !True;
455 midtb->s_begin = 0;
456 midtb->s_end = midtb->used;
457 tPtr->currentTextBlock = tb;
458 WMAppendTextBlock(tPtr, midtb);
459 tb = tPtr->currentTextBlock;
462 if (otb->used - otb->s_end > 0) {
463 TextBlock *ntb;
464 ntb = (TextBlock *)
465 WMCreateTextBlockWithText(tPtr,
466 &(otb->text[otb->s_end]), otb->d.font, otb->color,
467 False, otb->used - otb->s_end);
469 if (ntb) {
470 ntb->underlined = otb->underlined;
471 ntb->selected = False;
472 WMAppendTextBlock(tPtr, ntb);
473 tb = tPtr->currentTextBlock;
477 if (midtb) {
478 tPtr->currentTextBlock = midtb;
481 otb->selected = False;
482 otb->used = otb->s_begin;
486 tb = tb->next;
489 tPtr->flags.needsLayOut = True;
490 WMThawText(tPtr);
492 /* in case the size changed... */
493 if(isFont && tPtr->currentTextBlock) {
494 TextBlock *tb = tPtr->currentTextBlock;
496 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
497 tPtr->sel.y = 3 + tb->sections[0]._y;
498 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
499 tPtr->sel.w = tb->sections[tb->nsections-1].w;
500 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
501 tPtr->sel.x = 0;
503 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
509 static Bool
510 removeSelection(Text *tPtr)
512 TextBlock *tb = NULL;
513 Bool first = False;
515 if (!(tb = tPtr->firstTextBlock))
516 return False;
518 while (tb) {
519 if (tb->selected) {
520 if(!first && !tb->graphic) {
521 WMReleaseFont(tPtr->dFont);
522 tPtr->dFont = WMRetainFont(tb->d.font);
523 first = True;
526 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
527 tPtr->currentTextBlock = tb;
528 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
529 tb = tPtr->currentTextBlock;
530 if (tb)
531 tPtr->tpos = 0;
532 continue;
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]),
536 &(tb->text[tb->s_end]), tb->used - tb->s_end);
537 tb->used -= (tb->s_end - tb->s_begin);
538 tb->selected = False;
539 tPtr->tpos = tb->s_begin;
544 tb = tb->next;
546 return True;
549 static TextBlock *
550 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
552 TextBlock *hold = tb;
554 if (!tb)
555 return NULL;
557 while (tb) {
558 if (!tb->graphic)
559 break;
560 tb = (dir? tb->next : tb->prior);
563 if(!tb) {
564 tb = hold;
565 while (tb) {
566 if (!tb->graphic)
567 break;
568 tb = (dir? tb->prior : tb->next);
572 if(!tb)
573 return NULL;
575 return tb;
579 static Bool
580 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
581 TextBlock *tb)
583 if (tPtr->flags.monoFont && tb->graphic) {
584 tb = getFirstNonGraphicBlockFor(tb, *dir);
585 if(!tb)
586 return 0;
588 if (tb->graphic) {
589 tPtr->currentTextBlock =
590 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
591 tPtr->tpos = 0;
592 return 0;
597 if(!tb->sections)
598 layOutDocument(tPtr);
599 if(!tb->sections)
600 return 0;
602 *dir = !(y <= tb->sections[0].y);
603 if(*dir) {
604 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
605 && (y >= tb->sections[0]._y ) ) {
606 /* if it's on the same line */
607 if(x < tb->sections[0].x)
608 *dir = 0;
610 } else {
611 if ( ( y <= tb->sections[tb->nsections-1]._y
612 + tb->sections[tb->nsections-1].h )
613 && (y >= tb->sections[tb->nsections-1]._y ) ) {
614 /* if it's on the same line */
615 if(x > tb->sections[tb->nsections-1].x)
616 *dir = 1;
620 return 1;
624 static void
625 paintText(Text *tPtr)
627 TextBlock *tb;
628 WMFont *font;
629 GC gc, greyGC;
630 char *text;
631 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
632 WMScreen *scr = tPtr->view->screen;
633 Display *dpy = tPtr->view->screen->display;
634 Window win = tPtr->view->window;
635 WMColor *color;
637 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
638 return;
641 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
642 0, 0, tPtr->visible.w, tPtr->visible.h);
644 if (tPtr->bgPixmap) {
645 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
646 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
647 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
650 if (! (tb = tPtr->currentTextBlock)) {
651 if (! (tb = tPtr->firstTextBlock)) {
652 goto _copy_area;
656 if (tPtr->flags.ownsSelection) {
657 color = WMGrayColor(scr);
658 greyGC = WMColorGC(color);
659 WMReleaseColor(color);
662 done = False;
666 /* first, which direction? Don't waste time looking all over,
667 since the parts to be drawn will most likely be near what
668 was previously drawn */
669 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
670 goto _copy_area;
672 while(tb) {
674 if (tb->graphic && tPtr->flags.monoFont)
675 goto _getSibling;
677 if(dir) {
678 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
679 + tb->sections[tb->nsections-1].h)
680 break;
681 } else {
682 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
683 + tb->sections[tb->nsections-1].h)
684 break;
687 _getSibling:
688 if(dir) {
689 if(tb->next)
690 tb = tb->next;
691 else break;
692 } else {
693 if(tb->prior)
694 tb = tb->prior;
695 else break;
700 /* first, place all text that can be viewed */
701 while (!done && tb) {
703 /* paragraph diagnostic
704 if(tb->blank) {tb->text[0] = 'F'; } */
706 if (tb->graphic) {
707 tb = tb->next;
708 continue;
711 tb->selected = False;
713 for(s=0; s<tb->nsections && !done; s++) {
715 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
716 done = True;
717 break;
720 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
721 continue;
723 if (tPtr->flags.monoFont) {
724 font = tPtr->dFont;
725 gc = tPtr->fgGC;
726 } else {
727 font = tb->d.font;
728 gc = WMColorGC(tb->color);
731 if (tPtr->flags.ownsSelection) {
732 XRectangle rect;
734 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
735 tb->selected = True;
736 XFillRectangle(dpy, tPtr->db, greyGC,
737 rect.x, rect.y, rect.width, rect.height);
741 prev_y = tb->sections[s]._y;
743 len = tb->sections[s].end - tb->sections[s].begin;
744 text = &(tb->text[tb->sections[s].begin]);
745 y = tb->sections[s].y - tPtr->vpos;
746 WMDrawString(scr, tPtr->db, gc, font,
747 tb->sections[s].x - tPtr->hpos, y, text, len);
749 if (!tPtr->flags.monoFont && tb->underlined) {
750 XDrawLine(dpy, tPtr->db, gc,
751 tb->sections[s].x - tPtr->hpos,
752 y + font->y + 1,
753 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
754 y + font->y + 1);
759 tb = (!done? tb->next : NULL);
763 /* now , show all graphic items that can be viewed */
764 c = WMGetArrayItemCount(tPtr->gfxItems);
765 if (c > 0 && !tPtr->flags.monoFont) {
766 int j, h;
768 for(j=0; j<c; j++) {
769 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
771 /* if it's not viewable, and mapped, unmap it */
772 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
773 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
775 if(tb->object) {
776 if ((W_VIEW(tb->d.widget))->flags.mapped) {
777 WMUnmapWidget(tb->d.widget);
780 } else {
781 /* if it's viewable, and not mapped, map it */
782 if(tb->object) {
783 W_View *view = W_VIEW(tb->d.widget);
785 if (!view->flags.realized)
786 WMRealizeWidget(tb->d.widget);
787 if(!view->flags.mapped) {
788 XMapWindow(view->screen->display, view->window);
789 XFlush(view->screen->display);
790 view->flags.mapped = 1;
794 if (tPtr->flags.ownsSelection) {
795 XRectangle rect;
797 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
798 tb->selected = True;
799 XFillRectangle(dpy, tPtr->db, greyGC,
800 rect.x, rect.y, rect.width, rect.height);
804 if(tb->object) {
805 WMMoveWidget(tb->d.widget,
806 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
807 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
808 h = WMWidgetHeight(tb->d.widget) + 1;
810 } else {
811 WMDrawPixmap(tb->d.pixmap, tPtr->db,
812 tb->sections[0].x - tPtr->hpos,
813 tb->sections[0].y - tPtr->vpos);
814 h = tb->d.pixmap->height + 1;
818 if (!tPtr->flags.monoFont && tb->underlined) {
819 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
820 tb->sections[0].x - tPtr->hpos,
821 tb->sections[0].y + h - tPtr->vpos,
822 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
823 tb->sections[0].y + h - tPtr->vpos);
830 _copy_area:
831 if (tPtr->flags.editable && tPtr->flags.cursorShown
832 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
833 int y = tPtr->cursor.y - tPtr->vpos;
834 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
835 tPtr->cursor.x, y,
836 tPtr->cursor.x, y + tPtr->cursor.h);
839 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
840 tPtr->visible.w, tPtr->visible.h,
841 tPtr->visible.x, tPtr->visible.y);
843 W_DrawRelief(scr, win, 0, 0,
844 tPtr->view->size.width, tPtr->view->size.height,
845 tPtr->flags.relief);
847 if (tPtr->ruler && tPtr->flags.rulerShown)
848 XDrawLine(dpy, win, tPtr->fgGC,
849 2, 42, tPtr->view->size.width-4, 42);
853 static void
854 mouseOverObject(Text *tPtr, int x, int y)
856 TextBlock *tb;
857 Bool result = False;
859 x -= tPtr->visible.x;
860 x += tPtr->hpos;
861 y -= tPtr->visible.y;
862 y += tPtr->vpos;
864 if(tPtr->flags.ownsSelection) {
865 if(tPtr->sel.x <= x
866 && tPtr->sel.y <= y
867 && tPtr->sel.x + tPtr->sel.w >= x
868 && tPtr->sel.y + tPtr->sel.h >= y) {
869 tPtr->flags.isOverGraphic = 1;
870 result = True;
875 if(!result) {
876 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
878 if (c<1)
879 tPtr->flags.isOverGraphic = 0;
882 for(j=0; j<c; j++) {
883 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
885 if(!tb || !tb->sections) {
886 tPtr->flags.isOverGraphic = 0;
887 return;
890 if(!tb->object) {
891 if(tb->sections[0].x <= x
892 && tb->sections[0].y <= y
893 && tb->sections[0].x + tb->sections[0].w >= x
894 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
895 tPtr->flags.isOverGraphic = 3;
896 result = True;
897 break;
905 if(!result)
906 tPtr->flags.isOverGraphic = 0;
909 tPtr->view->attribs.cursor = (result?
910 tPtr->view->screen->defaultCursor
911 : tPtr->view->screen->textCursor);
913 XSetWindowAttributes attribs;
914 attribs.cursor = tPtr->view->attribs.cursor;
915 XChangeWindowAttributes(tPtr->view->screen->display,
916 tPtr->view->window, CWCursor,
917 &attribs);
921 #if DO_BLINK
923 static void
924 blinkCursor(void *data)
926 Text *tPtr = (Text*)data;
928 if (tPtr->flags.cursorShown) {
929 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
930 blinkCursor, data);
931 } else {
932 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
933 blinkCursor, data);
935 paintText(tPtr);
936 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
938 #endif
940 static void
941 updateCursorPosition(Text *tPtr)
943 TextBlock *tb = NULL;
944 int x, y, h, s;
946 if(tPtr->flags.needsLayOut)
947 layOutDocument(tPtr);
949 if (! (tb = tPtr->currentTextBlock)) {
950 if (! (tb = tPtr->firstTextBlock)) {
951 WMFont *font = tPtr->dFont;
952 tPtr->tpos = 0;
953 tPtr->cursor.h = font->height + abs(font->height-font->y);
955 tPtr->cursor.y = 2;
956 tPtr->cursor.x = 2;
957 return;
962 if(tb->blank) {
963 tPtr->tpos = 0;
964 y = tb->sections[0].y;
965 h = tb->sections[0].h;
966 x = tb->sections[0].x;
968 } else if(tb->graphic) {
969 y = tb->sections[0].y;
970 h = tb->sections[0].h;
971 x = tb->sections[0].x;
973 } else {
974 if(tPtr->tpos > tb->used)
975 tPtr->tpos = tb->used;
977 for(s=0; s<tb->nsections-1; s++) {
979 if(tPtr->tpos >= tb->sections[s].begin
980 && tPtr->tpos <= tb->sections[s].end)
981 break;
984 y = tb->sections[s]._y;
985 h = tb->sections[s].h;
986 x = tb->sections[s].x + WMWidthOfString(
987 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
988 &tb->text[tb->sections[s].begin],
989 tPtr->tpos - tb->sections[s].begin);
992 tPtr->cursor.y = y;
993 tPtr->cursor.h = h;
994 tPtr->cursor.x = x;
997 /* scroll the bars if the cursor is not visible */
998 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
999 if(tPtr->cursor.y+tPtr->cursor.h
1000 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1001 tPtr->vpos +=
1002 (tPtr->cursor.y+tPtr->cursor.h+10
1003 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1004 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1005 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1010 updateScrollers(tPtr);
1014 static void
1015 cursorToTextPosition(Text *tPtr, int x, int y)
1017 TextBlock *tb = NULL;
1018 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1019 char *text;
1021 if(tPtr->flags.needsLayOut)
1022 layOutDocument(tPtr);
1024 y += (tPtr->vpos - tPtr->visible.y);
1025 if (y<0)
1026 y = 0;
1028 x -= (tPtr->visible.x - 2);
1029 if (x<0)
1030 x=0;
1032 /* clicked is relative to document, not window... */
1033 tPtr->clicked.x = x;
1034 tPtr->clicked.y = y;
1036 if (! (tb = tPtr->currentTextBlock)) {
1037 if (! (tb = tPtr->firstTextBlock)) {
1038 WMFont *font = tPtr->dFont;
1039 tPtr->tpos = 0;
1040 tPtr->cursor.h = font->height + abs(font->height-font->y);
1041 tPtr->cursor.y = 2;
1042 tPtr->cursor.x = 2;
1043 return;
1047 /* first, which direction? Most likely, newly clicked
1048 position will be close to previous */
1049 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1050 return;
1053 s = (dir? 0 : tb->nsections-1);
1054 if ( y >= tb->sections[s]._y
1055 && y <= tb->sections[s]._y + tb->sections[s].h) {
1056 goto _doneV;
1059 /* get the first (or last) section of the TextBlock that
1060 lies about the vertical click point */
1061 done = False;
1062 while (!done && tb) {
1064 if (tPtr->flags.monoFont && tb->graphic) {
1065 if( (dir?tb->next:tb->prior))
1066 tb = (dir?tb->next:tb->prior);
1067 continue;
1070 s = (dir? 0 : tb->nsections-1);
1071 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1073 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1074 ( y >= tb->sections[s]._y ) ) ) {
1075 done = True;
1076 } else {
1077 dir? s++ : s--;
1081 if (!done) {
1082 if ( (dir? tb->next : tb->prior)) {
1083 tb = (dir ? tb->next : tb->prior);
1084 } else {
1085 pos = tb->used;
1086 break; /* goto _doneH; */
1092 if (s<0 || s>=tb->nsections) {
1093 s = (dir? tb->nsections-1 : 0);
1096 _doneV:
1097 /* we have the line, which TextBlock on that line is it? */
1098 pos = (dir?0:tb->sections[s].begin);
1099 if (tPtr->flags.monoFont && tb->graphic) {
1100 TextBlock *hold = tb;
1101 tb = getFirstNonGraphicBlockFor(hold, dir);
1103 if(!tb) {
1104 tPtr->tpos = 0;
1105 tb = hold;
1106 s = 0;
1107 goto _doNothing;
1112 if(tb->blank)
1113 _w = 0;
1115 _y = tb->sections[s]._y;
1117 while (tb) {
1119 if (tPtr->flags.monoFont && tb->graphic) {
1120 tb = (dir ? tb->next : tb->prior);
1121 continue;
1124 if (dir) {
1125 if (tb->graphic) {
1126 if(tb->object)
1127 _w = WMWidgetWidth(tb->d.widget)-5;
1128 else
1129 _w = tb->d.pixmap->width-5;
1131 if (tb->sections[0].x + _w >= x)
1132 break;
1133 } else {
1134 text = &(tb->text[tb->sections[s].begin]);
1135 len = tb->sections[s].end - tb->sections[s].begin;
1136 _w = WMWidthOfString(tb->d.font, text, len);
1137 if (tb->sections[s].x + _w >= x)
1138 break;
1141 } else {
1142 if (tb->sections[s].x <= x)
1143 break;
1146 if ((dir? tb->next : tb->prior)) {
1147 TextBlock *nxt = (dir? tb->next : tb->prior);
1148 if (tPtr->flags.monoFont && nxt->graphic) {
1149 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1150 if (!nxt) {
1151 pos = (dir?0:tb->sections[s].begin);
1152 tPtr->cursor.x = tb->sections[s].x;
1153 goto _doneH;
1157 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1158 /* this must be the last/first on this line. stop */
1159 pos = (dir? tb->sections[s].end : 0);
1160 tPtr->cursor.x = tb->sections[s].x;
1161 if (!tb->blank) {
1162 if (tb->graphic) {
1163 if(tb->object)
1164 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1165 else
1166 tPtr->cursor.x += tb->d.pixmap->width;
1167 } else if (pos > tb->sections[s].begin) {
1168 tPtr->cursor.x +=
1169 WMWidthOfString(tb->d.font,
1170 &(tb->text[tb->sections[s].begin]),
1171 pos - tb->sections[s].begin);
1174 goto _doneH;
1178 if ( (dir? tb->next : tb->prior)) {
1179 tb = (dir ? tb->next : tb->prior);
1180 } else {
1181 done = True;
1182 break;
1185 if (tb)
1186 s = (dir? 0 : tb->nsections-1);
1189 /* we have said TextBlock, now where within it? */
1190 if (tb) {
1191 if(tb->graphic) {
1192 int gw = (tb->object ?
1193 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1195 tPtr->cursor.x = tb->sections[0].x;
1197 if(x > tPtr->cursor.x + gw/2) {
1198 pos = 1;
1199 printf("here x%d: %d\n", x, tPtr->cursor.x + gw/2);
1200 tPtr->cursor.x += gw;
1201 } else pos = 0;
1203 s = 0;
1204 goto _doneH;
1206 } else {
1207 WMFont *f = tb->d.font;
1208 len = tb->sections[s].end - tb->sections[s].begin;
1209 text = &(tb->text[tb->sections[s].begin]);
1211 _w = x - tb->sections[s].x;
1212 pos = 0;
1214 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1215 pos++;
1217 tPtr->cursor.x = tb->sections[s].x +
1218 (pos? WMWidthOfString(f, text, pos) : 0);
1220 pos += tb->sections[s].begin;
1224 _doneH:
1225 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1226 _doNothing:
1227 if (!tb)
1228 printf("...for this app will surely crash :-)\n");
1230 tPtr->currentTextBlock = tb;
1231 tPtr->cursor.h = tb->sections[s].h;
1232 tPtr->cursor.y = tb->sections[s]._y;
1234 /* scroll the bars if the cursor is not visible */
1235 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1236 if(tPtr->cursor.y+tPtr->cursor.h
1237 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1238 tPtr->vpos +=
1239 (tPtr->cursor.y+tPtr->cursor.h+10
1240 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1241 updateScrollers(tPtr);
1242 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1243 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1244 updateScrollers(tPtr);
1252 static void
1253 updateScrollers(Text *tPtr)
1256 if (tPtr->flags.frozen)
1257 return;
1259 if (tPtr->vS) {
1260 if (tPtr->docHeight < tPtr->visible.h) {
1261 WMSetScrollerParameters(tPtr->vS, 0, 1);
1262 tPtr->vpos = 0;
1263 } else {
1264 float hmax = (float)(tPtr->docHeight);
1265 WMSetScrollerParameters(tPtr->vS,
1266 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1267 (float)tPtr->visible.h/hmax);
1269 } else tPtr->vpos = 0;
1271 if (tPtr->hS) {
1272 if (tPtr->docWidth < tPtr->visible.w) {
1273 WMSetScrollerParameters(tPtr->hS, 0, 1);
1274 tPtr->hpos = 0;
1275 } else {
1276 float wmax = (float)(tPtr->docWidth);
1277 WMSetScrollerParameters(tPtr->hS,
1278 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1279 (float)tPtr->visible.w/wmax);
1281 } else tPtr->hpos = 0;
1284 static void
1285 scrollersCallBack(WMWidget *w, void *self)
1287 Text *tPtr = (Text *)self;
1288 Bool scroll = False;
1289 int which;
1291 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1292 return;
1294 if (w == tPtr->vS) {
1295 int height;
1296 height = tPtr->visible.h;
1298 which = WMGetScrollerHitPart(tPtr->vS);
1299 switch(which) {
1301 case WSDecrementLine:
1302 if (tPtr->vpos > 0) {
1303 if (tPtr->vpos>16) tPtr->vpos-=16;
1304 else tPtr->vpos=0;
1305 scroll=True;
1307 break;
1309 case WSIncrementLine: {
1310 int limit = tPtr->docHeight - height;
1311 if (tPtr->vpos < limit) {
1312 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1313 else tPtr->vpos=limit;
1314 scroll = True;
1317 break;
1319 case WSDecrementPage:
1320 if(((int)tPtr->vpos - (int)height) >= 0)
1321 tPtr->vpos -= height;
1322 else
1323 tPtr->vpos = 0;
1325 scroll = True;
1326 break;
1328 case WSIncrementPage:
1329 tPtr->vpos += height;
1330 if (tPtr->vpos > (tPtr->docHeight - height))
1331 tPtr->vpos = tPtr->docHeight - height;
1332 scroll = True;
1333 break;
1336 case WSKnob:
1337 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1338 * (float)(tPtr->docHeight - height);
1339 scroll = True;
1340 break;
1342 case WSKnobSlot:
1343 case WSNoPart:
1344 break;
1346 scroll = (tPtr->vpos != tPtr->prevVpos);
1347 tPtr->prevVpos = tPtr->vpos;
1351 if (w == tPtr->hS) {
1352 int width = tPtr->visible.w;
1354 which = WMGetScrollerHitPart(tPtr->hS);
1355 switch(which) {
1357 case WSDecrementLine:
1358 if (tPtr->hpos > 0) {
1359 if (tPtr->hpos>16) tPtr->hpos-=16;
1360 else tPtr->hpos=0;
1361 scroll=True;
1362 }break;
1364 case WSIncrementLine: {
1365 int limit = tPtr->docWidth - width;
1366 if (tPtr->hpos < limit) {
1367 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1368 else tPtr->hpos=limit;
1369 scroll = True;
1371 }break;
1373 case WSDecrementPage:
1374 if(((int)tPtr->hpos - (int)width) >= 0)
1375 tPtr->hpos -= width;
1376 else
1377 tPtr->hpos = 0;
1379 scroll = True;
1380 break;
1382 case WSIncrementPage:
1383 tPtr->hpos += width;
1384 if (tPtr->hpos > (tPtr->docWidth - width))
1385 tPtr->hpos = tPtr->docWidth - width;
1386 scroll = True;
1387 break;
1390 case WSKnob:
1391 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1392 * (float)(tPtr->docWidth - width);
1393 scroll = True;
1394 break;
1396 case WSKnobSlot:
1397 case WSNoPart:
1398 break;
1400 scroll = (tPtr->hpos != tPtr->prevHpos);
1401 tPtr->prevHpos = tPtr->hpos;
1404 if (scroll) {
1405 updateScrollers(tPtr);
1406 paintText(tPtr);
1412 typedef struct {
1413 TextBlock *tb;
1414 unsigned short begin, end; /* what part of the text block */
1415 } myLineItems;
1418 static int
1419 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1421 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1422 WMFont *font;
1423 char *text;
1424 TextBlock *tb, *tbsame=NULL;
1426 if(!items || nitems == 0)
1427 return 0;
1429 for(i=0; i<nitems; i++) {
1430 tb = items[i].tb;
1432 if (tb->graphic) {
1433 if (!tPtr->flags.monoFont) {
1434 if(tb->object) {
1435 WMWidget *wdt = tb->d.widget;
1436 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1437 if (tPtr->flags.alignment != WALeft)
1438 lw += WMWidgetWidth(wdt);
1439 } else {
1440 line_height = WMAX(line_height,
1441 tb->d.pixmap->height + max_d);
1442 if (tPtr->flags.alignment != WALeft)
1443 lw += tb->d.pixmap->width;
1447 } else {
1448 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1449 max_d = WMAX(max_d, abs(font->height-font->y));
1450 line_height = WMAX(line_height, font->height + max_d);
1451 text = &(tb->text[items[i].begin]);
1452 len = items[i].end - items[i].begin;
1453 if (tPtr->flags.alignment != WALeft)
1454 lw += WMWidthOfString(font, text, len);
1458 if (tPtr->flags.alignment == WARight) {
1459 j = tPtr->visible.w - lw;
1460 } else if (tPtr->flags.alignment == WACenter) {
1461 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1464 for(i=0; i<nitems; i++) {
1465 tb = items[i].tb;
1467 if (tbsame == tb) { /* extend it, since it's on same line */
1468 tb->sections[tb->nsections-1].end = items[i].end;
1469 n = tb->nsections-1;
1470 } else {
1471 tb->sections = wrealloc(tb->sections,
1472 (++tb->nsections)*sizeof(Section));
1473 n = tb->nsections-1;
1474 tb->sections[n]._y = y + max_d;
1475 tb->sections[n].max_d = max_d;
1476 tb->sections[n].x = x+j;
1477 tb->sections[n].h = line_height;
1478 tb->sections[n].begin = items[i].begin;
1479 tb->sections[n].end = items[i].end;
1482 tb->sections[n].last = (i+1 == nitems);
1484 if (tb->graphic) {
1485 if (!tPtr->flags.monoFont) {
1486 if(tb->object) {
1487 WMWidget *wdt = tb->d.widget;
1488 tb->sections[n].y = max_d + y
1489 + line_height - WMWidgetHeight(wdt);
1490 tb->sections[n].w = WMWidgetWidth(wdt);
1491 } else {
1492 tb->sections[n].y = y + line_height
1493 + max_d - tb->d.pixmap->height;
1494 tb->sections[n].w = tb->d.pixmap->width;
1496 x += tb->sections[n].w;
1498 } else {
1499 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1500 len = items[i].end - items[i].begin;
1501 text = &(tb->text[items[i].begin]);
1503 tb->sections[n].y = y+line_height-font->y;
1504 tb->sections[n].w =
1505 WMWidthOfString(font,
1506 &(tb->text[tb->sections[n].begin]),
1507 tb->sections[n].end - tb->sections[n].begin);
1509 x += WMWidthOfString(font, text, len);
1512 tbsame = tb;
1515 return line_height;
1520 static void
1521 layOutDocument(Text *tPtr)
1523 TextBlock *tb;
1524 myLineItems *items = NULL;
1525 unsigned int itemsSize=0, nitems=0, begin, end;
1526 WMFont *font;
1527 unsigned int x, y=0, lw = 0, width=0, bmargin;
1528 char *start=NULL, *mark=NULL;
1530 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1531 return;
1533 assert(tPtr->visible.w > 20);
1535 tPtr->docWidth = tPtr->visible.w;
1536 x = tPtr->margins[tb->marginN].first;
1537 bmargin = tPtr->margins[tb->marginN].body;
1539 /* only partial layOut needed: re-Lay only affected textblocks */
1540 if (tPtr->flags.laidOut) {
1541 tb = tPtr->currentTextBlock;
1543 /* search backwards for textblocks on same line */
1544 while (tb->prior) {
1545 if (!tb->sections || tb->nsections<1) {
1546 tb = tPtr->firstTextBlock;
1547 tPtr->flags.laidOut = False;
1548 y = 0;
1549 goto _layOut;
1552 if(!tb->prior->sections || tb->prior->nsections<1) {
1553 tb = tPtr->firstTextBlock;
1554 tPtr->flags.laidOut = False;
1555 y = 0;
1556 goto _layOut;
1559 if (tb->sections[0]._y !=
1560 tb->prior->sections[tb->prior->nsections-1]._y) {
1561 break;
1563 tb = tb->prior;
1566 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1567 y = tb->prior->sections[tb->prior->nsections-1]._y +
1568 tb->prior->sections[tb->prior->nsections-1].h -
1569 tb->prior->sections[tb->prior->nsections-1].max_d;
1570 } else {
1571 y = 0;
1575 _layOut:
1576 while (tb) {
1578 if (tb->sections && tb->nsections>0) {
1579 wfree(tb->sections);
1580 tb->sections = NULL;
1581 tb->nsections = 0;
1584 if (tb->blank && tb->next && !tb->next->first) {
1585 TextBlock *next = tb->next;
1586 tPtr->currentTextBlock = tb;
1587 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1588 tb = next;
1589 tb->first = True;
1590 continue;
1593 if (tb->first && tb != tPtr->firstTextBlock) {
1594 y += layOutLine(tPtr, items, nitems, x, y);
1595 x = tPtr->margins[tb->marginN].first;
1596 bmargin = tPtr->margins[tb->marginN].body;
1597 nitems = 0;
1598 lw = 0;
1601 if (tb->graphic) {
1602 if (!tPtr->flags.monoFont) {
1603 if(tb->object)
1604 width = WMWidgetWidth(tb->d.widget);
1605 else
1606 width = tb->d.pixmap->width;
1608 if (width > tPtr->docWidth)
1609 tPtr->docWidth = width;
1611 lw += width;
1612 if (lw >= tPtr->visible.w - x ) {
1613 y += layOutLine(tPtr, items, nitems, x, y);
1614 nitems = 0;
1615 x = bmargin;
1616 lw = width;
1619 if(nitems + 1> itemsSize) {
1620 items = wrealloc(items,
1621 (++itemsSize)*sizeof(myLineItems));
1624 items[nitems].tb = tb;
1625 items[nitems].begin = 0;
1626 items[nitems].end = 0;
1627 nitems++;
1630 } else if ((start = tb->text)) {
1631 begin = end = 0;
1632 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1634 while (start) {
1635 mark = strchr(start, ' ');
1636 if (mark) {
1637 end += (int)(mark-start)+1;
1638 start = mark+1;
1639 } else {
1640 end += strlen(start);
1641 start = mark;
1644 if (end > tb->used)
1645 end = tb->used;
1647 if (end-begin > 0) {
1649 width = WMWidthOfString(font,
1650 &tb->text[begin], end-begin);
1652 /* if it won't fit, char wrap it */
1653 if (width >= tPtr->visible.w) {
1654 char *t = &tb->text[begin];
1655 int l=end-begin, i=0;
1656 do {
1657 width = WMWidthOfString(font, t, ++i);
1658 } while (width < tPtr->visible.w && i < l);
1659 if(i>2) i--;
1660 end = begin+i;
1661 start = &tb->text[end];
1664 lw += width;
1667 if (lw >= tPtr->visible.w - x) {
1668 y += layOutLine(tPtr, items, nitems, x, y);
1669 lw = width;
1670 x = bmargin;
1671 nitems = 0;
1674 if(nitems + 1 > itemsSize) {
1675 items = wrealloc(items,
1676 (++itemsSize)*sizeof(myLineItems));
1679 items[nitems].tb = tb;
1680 items[nitems].begin = begin;
1681 items[nitems].end = end;
1682 nitems++;
1684 begin = end;
1689 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1690 if(0&&tPtr->flags.laidOut
1691 && tb->next && tb->next->sections && tb->next->nsections>0
1692 && (tPtr->vpos + tPtr->visible.h
1693 < tb->next->sections[0]._y)) {
1694 if(tPtr->lastTextBlock->sections
1695 && tPtr->lastTextBlock->nsections > 0 ) {
1696 TextBlock *ltb = tPtr->lastTextBlock;
1697 int ly = ltb->sections[ltb->nsections-1]._y;
1698 int lh = ltb->sections[ltb->nsections-1].h;
1699 int ss, sd;
1701 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1702 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1704 y += layOutLine(tPtr, items, nitems, x, y);
1705 ss= ly+lh-y;
1706 sd = tPtr->docHeight-y;
1708 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1709 y += tb->next->sections[0]._y-y;
1710 nitems = 0;
1711 printf("nitems%d\n", nitems);
1712 if(ss-sd!=0)
1713 y = tPtr->docHeight+ss-sd;
1715 break;
1716 } else {
1717 tPtr->flags.laidOut = False;
1721 tb = tb->next;
1725 if (nitems > 0)
1726 y += layOutLine(tPtr, items, nitems, x, y);
1728 if (tPtr->docHeight != y+10) {
1729 tPtr->docHeight = y+10;
1730 updateScrollers(tPtr);
1733 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1734 XEvent event;
1736 tPtr->flags.horizOnDemand = True;
1737 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1738 event.type = Expose;
1739 handleEvents(&event, (void *)tPtr);
1741 } else if(tPtr->docWidth <= tPtr->visible.w
1742 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1743 tPtr->flags.horizOnDemand = False;
1744 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1747 tPtr->flags.laidOut = True;
1749 if(items && itemsSize > 0)
1750 wfree(items);
1755 static void
1756 textDidResize(W_ViewDelegate *self, WMView *view)
1758 Text *tPtr = (Text *)view->self;
1759 unsigned short w = tPtr->view->size.width;
1760 unsigned short h = tPtr->view->size.height;
1761 unsigned short rh = 0, vw = 0, rel;
1763 rel = (tPtr->flags.relief == WRFlat);
1765 if (tPtr->ruler && tPtr->flags.rulerShown) {
1766 WMMoveWidget(tPtr->ruler, 2, 2);
1767 WMResizeWidget(tPtr->ruler, w - 4, 40);
1768 rh = 40;
1771 if (tPtr->vS) {
1772 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1773 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1774 vw = 20;
1775 WMSetRulerOffset(tPtr->ruler,22);
1776 } else WMSetRulerOffset(tPtr->ruler, 2);
1778 if (tPtr->hS) {
1779 if (tPtr->vS) {
1780 WMMoveWidget(tPtr->hS, vw, h - 21);
1781 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1782 } else {
1783 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1784 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1788 tPtr->visible.x = (tPtr->vS)?24:4;
1789 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1790 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1791 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1792 tPtr->visible.h -= (tPtr->hS)?20:0;
1793 tPtr->margins[0].right = tPtr->visible.w;
1795 if (tPtr->view->flags.realized) {
1797 if (tPtr->db) {
1798 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1799 tPtr->db = (Pixmap) NULL;
1802 if (tPtr->visible.w < 40)
1803 tPtr->visible.w = 40;
1804 if (tPtr->visible.h < 20)
1805 tPtr->visible.h = 20;
1807 if(!tPtr->db) {
1808 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1809 tPtr->view->window, tPtr->visible.w,
1810 tPtr->visible.h, tPtr->view->screen->depth);
1814 WMThawText(tPtr);
1817 W_ViewDelegate _TextViewDelegate =
1819 NULL,
1820 NULL,
1821 textDidResize,
1822 NULL,
1825 #define TEXT_BUFFER_INCR 8
1826 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1828 static void
1829 clearText(Text *tPtr)
1831 tPtr->vpos = tPtr->hpos = 0;
1832 tPtr->docHeight = tPtr->docWidth = 0;
1833 tPtr->cursor.x = -23;
1835 if (!tPtr->firstTextBlock)
1836 return;
1838 while (tPtr->currentTextBlock)
1839 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1841 tPtr->firstTextBlock = NULL;
1842 tPtr->currentTextBlock = NULL;
1843 tPtr->lastTextBlock = NULL;
1844 WMEmptyArray(tPtr->gfxItems);
1847 /* possibly remove a single character from the currentTextBlock,
1848 or if there's a selection, remove it...
1849 note that Delete and Backspace are treated differently */
1850 static void
1851 deleteTextInteractively(Text *tPtr, KeySym ksym)
1853 TextBlock *tb;
1854 Bool back = (Bool) (ksym == XK_BackSpace);
1855 Bool done = 1, wasFirst = 0;
1857 if (!tPtr->flags.editable)
1858 return;
1860 if ( !(tb = tPtr->currentTextBlock) )
1861 return;
1863 if (tPtr->flags.ownsSelection) {
1864 if(removeSelection(tPtr))
1865 layOutDocument(tPtr);
1866 return;
1869 wasFirst = tb->first;
1870 if (back && tPtr->tpos < 1) {
1871 if (tb->prior) {
1872 if(tb->prior->blank) {
1873 tPtr->currentTextBlock = tb->prior;
1874 WMRemoveTextBlock(tPtr);
1875 tPtr->currentTextBlock = tb;
1876 tb->first = True;
1877 layOutDocument(tPtr);
1878 return;
1879 } else {
1880 if(tb->blank) {
1881 TextBlock *prior = tb->prior;
1882 tPtr->currentTextBlock = tb;
1883 WMRemoveTextBlock(tPtr);
1884 tb = prior;
1885 } else {
1886 tb = tb->prior;
1889 tPtr->tpos = tb->used;
1890 tPtr->currentTextBlock = tb;
1891 done = 1;
1892 if(wasFirst) {
1893 if(tb->next)
1894 tb->next->first = False;
1895 layOutDocument(tPtr);
1896 return;
1902 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1903 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1904 if (back)
1905 tPtr->tpos--;
1906 memmove(&(tb->text[tPtr->tpos]),
1907 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1908 tb->used--;
1909 done = 0;
1912 /* if there are no characters left to back over in the textblock,
1913 but it still has characters to the right of the cursor: */
1914 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1915 || tb->graphic) {
1917 /* no more chars, and it's marked as blank? */
1918 if(tb->blank) {
1919 TextBlock *sibling = (back? tb->prior : tb->next);
1921 if(tb->used == 0 || tb->graphic)
1922 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1924 if (sibling) {
1925 tPtr->currentTextBlock = sibling;
1926 tPtr->tpos = (back? sibling->used : 0);
1928 /* no more chars, so mark it as blank */
1929 } else if(tb->used == 0)
1930 tb->blank = 1;
1933 layOutDocument(tPtr);
1937 static void
1938 insertTextInteractively(Text *tPtr, char *text, int len)
1940 TextBlock *tb;
1941 char *newline = NULL;
1943 if (!tPtr->flags.editable) {
1944 return;
1947 if (len < 1 || !text)
1948 return;
1951 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1952 return;
1955 if (tPtr->flags.ownsSelection)
1956 removeSelection(tPtr);
1959 if (tPtr->flags.ignoreNewLine) {
1960 int i;
1961 for(i=0; i<len; i++) {
1962 if (text[i] == '\n')
1963 text[i] = ' ';
1967 tb = tPtr->currentTextBlock;
1968 if (!tb || tb->graphic) {
1969 tPtr->tpos = 0;
1970 WMAppendTextStream(tPtr, text);
1971 layOutDocument(tPtr);
1972 return;
1975 if ((newline = strchr(text, '\n'))) {
1976 int nlen = (int)(newline-text);
1977 int s = tb->used - tPtr->tpos;
1978 char save[s];
1979 if (!tb->blank && nlen>0) {
1980 if (s > 0) {
1981 memcpy(save, &tb->text[tPtr->tpos], s);
1982 tb->used -= (tb->used - tPtr->tpos);
1984 insertTextInteractively(tPtr, text, nlen);
1985 newline++;
1986 WMAppendTextStream(tPtr, newline);
1987 if (s>0)
1988 insertTextInteractively(tPtr, save, s);
1990 } else {
1991 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1992 && !tb->graphic && tb->text) {
1994 void *ntb = WMCreateTextBlockWithText(
1995 tPtr, &tb->text[tPtr->tpos],
1996 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1997 tb->used = tPtr->tpos;
1998 WMAppendTextBlock(tPtr, ntb);
1999 tPtr->tpos = 0;
2001 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2002 if(tPtr->flags.indentNewLine) {
2003 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2004 " ", tb->d.font, tb->color, True, 4));
2005 tPtr->tpos = 4;
2006 } else {
2007 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2008 NULL, tb->d.font, tb->color, True, 0));
2009 tPtr->tpos = 0;
2014 } else {
2015 if (tb->used + len >= tb->allocated) {
2016 tb->allocated = reqBlockSize(tb->used+len);
2017 tb->text = wrealloc(tb->text, tb->allocated);
2020 if (tb->blank) {
2021 memcpy(tb->text, text, len);
2022 tb->used = len;
2023 tPtr->tpos = len;
2024 tb->text[tb->used] = 0;
2025 tb->blank = False;
2027 } else {
2028 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2029 tb->used-tPtr->tpos+1);
2030 memmove(&tb->text[tPtr->tpos], text, len);
2031 tb->used += len;
2032 tPtr->tpos += len;
2033 tb->text[tb->used] = 0;
2038 layOutDocument(tPtr);
2042 static void
2043 selectRegion(Text *tPtr, int x, int y)
2046 if (x < 0 || y < 0)
2047 return;
2049 y += (tPtr->flags.rulerShown? 40: 0);
2050 y += tPtr->vpos;
2051 if (y>10)
2052 y -= 10; /* the original offset */
2054 x -= tPtr->visible.x-2;
2055 if (x<0)
2056 x=0;
2058 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2059 tPtr->sel.w = abs(tPtr->clicked.x - x);
2060 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2061 tPtr->sel.h = abs(tPtr->clicked.y - y);
2063 tPtr->flags.ownsSelection = True;
2064 paintText(tPtr);
2068 static void
2069 releaseSelection(Text *tPtr)
2071 TextBlock *tb = tPtr->firstTextBlock;
2073 while(tb) {
2074 tb->selected = False;
2075 tb = tb->next;
2077 tPtr->flags.ownsSelection = False;
2078 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2079 CurrentTime);
2081 paintText(tPtr);
2085 WMData*
2086 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2087 Atom *type)
2089 Text *tPtr = view->self;
2090 Display *dpy = tPtr->view->screen->display;
2091 Atom _TARGETS;
2092 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2093 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2094 WMData *data = NULL;
2097 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2098 char *text = WMGetTextSelectedStream(tPtr);
2100 if (text) {
2101 data = WMCreateDataWithBytes(text, strlen(text));
2102 WMSetDataFormat(data, TYPETEXT);
2104 *type = target;
2105 return data;
2106 } else printf("didn't get it\n");
2108 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2109 if (target == _TARGETS) {
2110 Atom *ptr;
2112 ptr = wmalloc(4 * sizeof(Atom));
2113 ptr[0] = _TARGETS;
2114 ptr[1] = XA_STRING;
2115 ptr[2] = TEXT;
2116 ptr[3] = COMPOUND_TEXT;
2118 data = WMCreateDataWithBytes(ptr, 4*4);
2119 WMSetDataFormat(data, 32);
2121 *type = target;
2122 return data;
2125 return NULL;
2128 static void
2129 lostHandler(WMView *view, Atom selection, void *cdata)
2131 releaseSelection((WMText *)view->self);
2134 static WMSelectionProcs selectionHandler = {
2135 requestHandler, lostHandler, NULL
2139 static void
2140 ownershipObserver(void *observerData, WMNotification *notification)
2142 if (observerData != WMGetNotificationClientData(notification))
2143 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2146 static void
2147 autoSelectText(Text *tPtr, int clicks)
2149 int x, start;
2150 TextBlock *tb;
2151 char *mark = NULL, behind, ahead;
2153 if(!(tb = tPtr->currentTextBlock))
2154 return;
2156 if(clicks == 2) {
2159 switch(tb->text[tPtr->tpos]) {
2160 case ' ': return;
2162 case '<': case '>': behind = '<'; ahead = '>'; break;
2163 case '{': case '}': behind = '{'; ahead = '}'; break;
2164 case '[': case ']': behind = '['; ahead = ']'; break;
2166 default: behind = ahead = ' ';
2169 tPtr->sel.y = tPtr->cursor.y+5;
2170 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2172 if(tb->graphic) {
2173 tPtr->sel.x = tb->sections[0].x;
2174 tPtr->sel.w = tb->sections[0].w;
2175 } else {
2176 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2178 start = tPtr->tpos;
2179 while(start > 0 && tb->text[start-1] != behind)
2180 start--;
2182 x = tPtr->cursor.x;
2183 if(tPtr->tpos > start){
2184 x -= WMWidthOfString(font, &tb->text[start],
2185 tPtr->tpos - start);
2187 tPtr->sel.x = (x<0?0:x)+1;
2189 if((mark = strchr(&tb->text[start], ahead))) {
2190 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2191 (int)(mark - &tb->text[start]));
2192 } else if(tb->used > start) {
2193 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2194 tb->used - start);
2198 } else if(clicks == 3) {
2199 TextBlock *cur = tb;
2201 while(tb && !tb->first) {
2202 tb = tb->prior;
2204 tPtr->sel.y = tb->sections[0]._y;
2206 tb = cur;
2207 while(tb->next && !tb->next->first) {
2208 tb = tb->next;
2210 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2211 + 5 - tPtr->sel.y;
2213 tPtr->sel.x = 0;
2214 tPtr->sel.w = tPtr->docWidth;
2215 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2218 if (!tPtr->flags.ownsSelection) {
2219 WMCreateSelectionHandler(tPtr->view,
2220 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2221 tPtr->flags.ownsSelection = True;
2223 paintText(tPtr);
2228 static void
2229 fontChanged(void *observerData, WMNotification *notification)
2231 WMText *tPtr = (WMText *) observerData;
2232 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2233 printf("fontChanged\n");
2235 if(!tPtr || !font)
2236 return;
2238 if (tPtr->flags.ownsSelection)
2239 WMSetTextSelectionFont(tPtr, font);
2243 static void
2244 handleTextKeyPress(Text *tPtr, XEvent *event)
2246 char buffer[64];
2247 KeySym ksym;
2248 int control_pressed = False;
2249 TextBlock *tb = NULL;
2251 if (((XKeyEvent *) event)->state & ControlMask)
2252 control_pressed = True;
2253 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2255 switch(ksym) {
2257 case XK_Left:
2258 if(!(tb = tPtr->currentTextBlock))
2259 break;
2260 if(tb->graphic)
2261 goto L_imaGFX;
2263 if(tPtr->tpos==0) {
2264 L_imaGFX: if(tb->prior) {
2265 tPtr->currentTextBlock = tb->prior;
2266 tPtr->tpos = tPtr->currentTextBlock->used;
2267 if(!tb->first && tPtr->tpos > 0)
2268 tPtr->tpos--;
2269 } else tPtr->tpos = 0;
2270 } else tPtr->tpos--;
2271 updateCursorPosition(tPtr);
2272 paintText(tPtr);
2273 break;
2275 case XK_Right:
2276 if(!(tb = tPtr->currentTextBlock))
2277 break;
2278 if(tb->graphic)
2279 goto R_imaGFX;
2280 if(tPtr->tpos == tb->used) {
2281 R_imaGFX: if(tb->next) {
2282 tPtr->currentTextBlock = tb->next;
2283 tPtr->tpos = 0;
2284 if(!tb->next->first && tb->next->used>0)
2285 tPtr->tpos++;
2286 } else tPtr->tpos = tb->used;
2287 } else tPtr->tpos++;
2288 updateCursorPosition(tPtr);
2289 paintText(tPtr);
2290 break;
2292 case XK_Down:
2293 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2294 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2295 paintText(tPtr);
2296 break;
2298 case XK_Up:
2299 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2300 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2301 paintText(tPtr);
2302 break;
2304 case XK_BackSpace:
2305 case XK_Delete:
2306 #ifdef XK_KP_Delete
2307 case XK_KP_Delete:
2308 #endif
2309 deleteTextInteractively(tPtr, ksym);
2310 updateCursorPosition(tPtr);
2311 paintText(tPtr);
2312 break;
2314 case XK_Control_R :
2315 case XK_Control_L :
2316 control_pressed = True;
2317 break;
2319 case XK_Tab:
2320 insertTextInteractively(tPtr, " ", 4);
2321 updateCursorPosition(tPtr);
2322 paintText(tPtr);
2323 break;
2325 case XK_Return:
2326 *buffer = '\n';
2327 default:
2328 if (*buffer != 0 && !control_pressed) {
2329 insertTextInteractively(tPtr, buffer, strlen(buffer));
2330 updateCursorPosition(tPtr);
2331 paintText(tPtr);
2333 } else if (control_pressed && ksym==XK_r) {
2334 Bool i = !tPtr->flags.rulerShown;
2335 WMShowTextRuler(tPtr, i);
2336 tPtr->flags.rulerShown = i;
2338 else if (control_pressed && *buffer == '\a')
2339 XBell(tPtr->view->screen->display, 0);
2340 else
2341 WMRelayToNextResponder(tPtr->view, event);
2344 if (!control_pressed && tPtr->flags.ownsSelection)
2345 releaseSelection(tPtr);
2349 static void
2350 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2351 void *cdata, WMData *data)
2353 Text *tPtr = (Text *)view->self;
2354 char *text;
2356 tPtr->flags.waitingForSelection = 0;
2358 if (data) {
2359 text = (char*)WMDataBytes(data);
2361 if (tPtr->parser) {
2362 (tPtr->parser) (tPtr, (void *) text);
2363 layOutDocument(tPtr);
2364 } else insertTextInteractively(tPtr, text, strlen(text));
2365 updateCursorPosition(tPtr);
2366 paintText(tPtr);
2368 } else {
2369 int n;
2371 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2373 if (text) {
2374 text[n] = 0;
2375 if (tPtr->parser) {
2376 (tPtr->parser) (tPtr, (void *) text);
2377 layOutDocument(tPtr);
2378 } else insertTextInteractively(tPtr, text, n);
2379 updateCursorPosition(tPtr);
2380 paintText(tPtr);
2382 XFree(text);
2390 static void
2391 handleActionEvents(XEvent *event, void *data)
2393 Text *tPtr = (Text *)data;
2394 Display *dpy = event->xany.display;
2395 KeySym ksym;
2398 switch (event->type) {
2399 case KeyPress:
2400 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2401 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2402 tPtr->flags.extendSelection = True;
2403 return;
2406 if (tPtr->flags.focused) {
2407 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2408 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2409 GrabModeAsync, GrabModeAsync, None,
2410 tPtr->view->screen->invisibleCursor, CurrentTime);
2411 tPtr->flags.pointerGrabbed = True;
2412 handleTextKeyPress(tPtr, event);
2414 } break;
2416 case KeyRelease:
2417 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2418 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2419 tPtr->flags.extendSelection = False;
2420 return;
2421 /* end modify flag so selection can be extended */
2423 break;
2426 case MotionNotify:
2428 if (tPtr->flags.pointerGrabbed) {
2429 tPtr->flags.pointerGrabbed = False;
2430 XUngrabPointer(dpy, CurrentTime);
2433 if(tPtr->flags.waitingForSelection)
2434 break;
2436 if ((event->xmotion.state & Button1Mask)) {
2437 TextBlock *tb = tPtr->currentTextBlock;
2439 if(tb && tPtr->flags.isOverGraphic &&
2440 tb->graphic && !tb->object) {
2441 WMSize offs;
2442 WMPixmap *pixmap = tb->d.pixmap;
2443 char *types[2] = {"application/X-image", NULL};
2445 offs.width = 2;
2446 offs.height = 2;
2448 WMDragImageFromView(tPtr->view, pixmap, types,
2449 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2450 offs, event, True);
2453 } else {
2454 if (!tPtr->flags.ownsSelection) {
2455 WMCreateSelectionHandler(tPtr->view,
2456 XA_PRIMARY, event->xbutton.time,
2457 &selectionHandler, NULL);
2458 tPtr->flags.ownsSelection = True;
2461 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2462 break;
2465 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2466 break;
2469 case ButtonPress:
2471 if (tPtr->flags.pointerGrabbed) {
2472 tPtr->flags.pointerGrabbed = False;
2473 XUngrabPointer(dpy, CurrentTime);
2474 break;
2477 if (tPtr->flags.waitingForSelection)
2478 break;
2480 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2481 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2482 return;
2485 if (tPtr->flags.ownsSelection)
2486 releaseSelection(tPtr);
2489 if (event->xbutton.button == Button1) {
2491 if(WMIsDoubleClick(event)) {
2492 TextBlock *tb = tPtr->currentTextBlock;
2494 tPtr->lastClickTime = event->xbutton.time;
2495 if(tb && tb->graphic && !tb->object) {
2496 char desc[tb->used+1];
2497 memcpy(desc, tb->text, tb->used);
2498 desc[tb->used] = 0;
2499 if(tPtr->delegate) {
2500 if(tPtr->delegate->didDoubleClickOnPicture)
2501 (*tPtr->delegate->didDoubleClickOnPicture)
2502 (tPtr->delegate, desc);
2504 } else {
2505 autoSelectText(tPtr, 2);
2507 break;
2508 } else if(event->xbutton.time - tPtr->lastClickTime
2509 < WINGsConfiguration.doubleClickDelay) {
2510 tPtr->lastClickTime = event->xbutton.time;
2511 autoSelectText(tPtr, 3);
2512 break;
2515 if (!tPtr->flags.focused) {
2516 WMSetFocusToWidget(tPtr);
2517 tPtr->flags.focused = True;
2520 tPtr->lastClickTime = event->xbutton.time;
2521 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2522 paintText(tPtr);
2525 if (event->xbutton.button
2526 == WINGsConfiguration.mouseWheelDown) {
2527 WMScrollText(tPtr, 16);
2528 break;
2531 if (event->xbutton.button
2532 == WINGsConfiguration.mouseWheelUp) {
2533 WMScrollText(tPtr, -16);
2534 break;
2537 if (event->xbutton.button == Button2) {
2538 char *text = NULL;
2539 int n;
2541 if (!tPtr->flags.editable) {
2542 XBell(dpy, 0);
2543 break;
2546 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2547 event->xbutton.time, pasteText, NULL)) {
2549 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2550 tPtr->flags.waitingForSelection = 0;
2552 if (text) {
2553 text[n] = 0;
2555 if (tPtr->parser) {
2556 (tPtr->parser) (tPtr, (void *) text);
2557 layOutDocument(tPtr);
2559 else
2560 insertTextInteractively(tPtr, text, n);
2562 XFree(text);
2563 #if 0
2564 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2565 (void*)WMInsertTextEvent);
2566 #endif
2567 updateCursorPosition(tPtr);
2568 paintText(tPtr);
2570 } else {
2571 tPtr->flags.waitingForSelection = True;
2574 break;
2578 case ButtonRelease:
2579 if (tPtr->flags.pointerGrabbed) {
2580 tPtr->flags.pointerGrabbed = False;
2581 XUngrabPointer(dpy, CurrentTime);
2582 break;
2585 if (tPtr->flags.waitingForSelection)
2586 break;
2592 static void
2593 handleEvents(XEvent *event, void *data)
2595 Text *tPtr = (Text *)data;
2597 switch(event->type) {
2598 case Expose:
2600 if (event->xexpose.count!=0)
2601 break;
2603 if(tPtr->hS) {
2604 if (!(W_VIEW(tPtr->hS))->flags.realized)
2605 WMRealizeWidget(tPtr->hS);
2608 if(tPtr->vS) {
2609 if (!(W_VIEW(tPtr->vS))->flags.realized)
2610 WMRealizeWidget(tPtr->vS);
2613 if(tPtr->ruler) {
2614 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2615 WMRealizeWidget(tPtr->ruler);
2619 if(!tPtr->db)
2620 textDidResize(tPtr->view->delegate, tPtr->view);
2622 paintText(tPtr);
2623 break;
2625 case FocusIn:
2626 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2627 != tPtr->view)
2628 return;
2629 tPtr->flags.focused = True;
2630 #if DO_BLINK
2631 if (tPtr->flags.editable && !tPtr->timerID) {
2632 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2633 blinkCursor, tPtr);
2635 #endif
2637 break;
2639 case FocusOut:
2640 tPtr->flags.focused = False;
2641 paintText(tPtr);
2642 #if DO_BLINK
2643 if (tPtr->timerID) {
2644 WMDeleteTimerHandler(tPtr->timerID);
2645 tPtr->timerID = NULL;
2647 #endif
2648 break;
2651 case DestroyNotify:
2652 clearText(tPtr);
2653 if(tPtr->db)
2654 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2655 if(tPtr->gfxItems)
2656 WMEmptyArray(tPtr->gfxItems);
2657 #if DO_BLINK
2658 if (tPtr->timerID)
2659 WMDeleteTimerHandler(tPtr->timerID);
2660 #endif
2661 WMReleaseFont(tPtr->dFont);
2662 WMReleaseColor(tPtr->dColor);
2663 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2664 WMRemoveNotificationObserver(tPtr);
2666 wfree(tPtr);
2668 break;
2674 static void
2675 insertPlainText(Text *tPtr, char *text)
2677 char *start, *mark;
2678 void *tb = NULL;
2680 start = text;
2681 while (start) {
2682 mark = strchr(start, '\n');
2683 if (mark) {
2684 tb = WMCreateTextBlockWithText(tPtr,
2685 start, tPtr->dFont,
2686 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2687 start = mark+1;
2688 tPtr->flags.first = True;
2689 } else {
2690 if (start && strlen(start)) {
2691 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2692 tPtr->dColor, tPtr->flags.first, strlen(start));
2693 } else tb = NULL;
2694 tPtr->flags.first = False;
2695 start = mark;
2698 if (tPtr->flags.prepend)
2699 WMPrependTextBlock(tPtr, tb);
2700 else
2701 WMAppendTextBlock(tPtr, tb);
2706 static void
2707 rulerMoveCallBack(WMWidget *w, void *self)
2709 Text *tPtr = (Text *)self;
2711 if (!tPtr)
2712 return;
2713 if (W_CLASS(tPtr) != WC_Text)
2714 return;
2716 paintText(tPtr);
2720 static void
2721 rulerReleaseCallBack(WMWidget *w, void *self)
2723 Text *tPtr = (Text *)self;
2725 if (!tPtr)
2726 return;
2727 if (W_CLASS(tPtr) != WC_Text)
2728 return;
2730 WMThawText(tPtr);
2731 return;
2734 static unsigned
2735 draggingSourceOperation(WMView *self, Bool local)
2737 return WDOperationCopy;
2740 static WMData*
2741 fetchDragData(WMView *self, char *type)
2743 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2744 char *desc;
2745 WMData *data;
2747 if (!tb)
2748 return NULL;
2750 printf("type is [%s]\n", type);
2751 desc = wmalloc(tb->used+1);
2752 memcpy(desc, tb->text, tb->used);
2753 desc[tb->used] = 0;
2754 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2756 wfree(desc);
2758 return data;
2762 static WMDragSourceProcs _DragSourceProcs = {
2763 draggingSourceOperation,
2764 NULL,
2765 NULL,
2766 fetchDragData
2770 static unsigned
2771 draggingEntered(WMView *self, WMDraggingInfo *info)
2773 printf("draggingEntered\n");
2774 return WDOperationCopy;
2778 static unsigned
2779 draggingUpdated(WMView *self, WMDraggingInfo *info)
2781 return WDOperationCopy;
2785 static void
2786 draggingExited(WMView *self, WMDraggingInfo *info)
2788 printf("draggingExited\n");
2791 static Bool
2792 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2794 printf("prepareForDragOperation\n");
2795 return True;
2799 char *badbadbad;
2801 static void
2802 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2803 void *cdata, WMData *data)
2805 badbadbad = wstrdup((char *)WMDataBytes(data));
2809 /* when it's done in WINGs, remove this */
2811 Bool
2812 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2814 WMScreen *scr = W_VIEW_SCREEN(view);
2816 if (!WMRequestSelection(scr->dragInfo.destView,
2817 scr->xdndSelectionAtom,
2818 XInternAtom(scr->display, type, False),
2819 scr->dragInfo.timestamp,
2820 receivedData, &scr->dragInfo)) {
2821 wwarning("could not request data for dropped data");
2825 XEvent ev;
2827 ev.type = ClientMessage;
2828 ev.xclient.message_type = scr->xdndFinishedAtom;
2829 ev.xclient.format = 32;
2830 ev.xclient.window = info->destinationWindow;
2831 ev.xclient.data.l[0] = 0;
2832 ev.xclient.data.l[1] = 0;
2833 ev.xclient.data.l[2] = 0;
2834 ev.xclient.data.l[3] = 0;
2835 ev.xclient.data.l[4] = 0;
2837 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2838 XFlush(scr->display);
2840 return True;
2843 static Bool
2844 performDragOperation(WMView *self, WMDraggingInfo *info)
2846 WMColor *color;
2847 WMText *tPtr = (WMText *)self->self;
2849 if (!tPtr)
2850 return True;
2852 requestDroppedData(tPtr->view, info, "application/X-color");
2853 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2854 if(color) {
2855 WMSetTextSelectionColor(tPtr, color);
2856 WMReleaseColor(color);
2861 return True;
2864 static void
2865 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2867 printf("concludeDragOperation\n");
2871 static WMDragDestinationProcs _DragDestinationProcs = {
2872 draggingEntered,
2873 draggingUpdated,
2874 draggingExited,
2875 prepareForDragOperation,
2876 performDragOperation,
2877 concludeDragOperation
2881 char *
2882 getStream(WMText *tPtr, int sel, int array)
2884 TextBlock *tb = NULL;
2885 char *text = NULL;
2886 unsigned long where = 0;
2888 if (!tPtr)
2889 return NULL;
2891 if (!(tb = tPtr->firstTextBlock))
2892 return NULL;
2894 if (tPtr->writer) {
2895 (tPtr->writer) (tPtr, (void *) text);
2896 return text;
2899 tb = tPtr->firstTextBlock;
2900 while (tb) {
2902 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2904 if (!sel || (tb->graphic && tb->selected)) {
2906 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2907 && tb != tPtr->firstTextBlock) {
2908 text = wrealloc(text, where+1);
2909 text[where++] = '\n';
2912 if(tb->blank)
2913 goto _gSnext;
2915 if(tb->graphic && array) {
2916 text = wrealloc(text, where+4);
2917 text[where++] = 0xFA;
2918 text[where++] = (tb->used>>8)&0x0ff;
2919 text[where++] = tb->used&0x0ff;
2920 text[where++] = tb->allocated; /* extra info */
2922 text = wrealloc(text, where+tb->used);
2923 memcpy(&text[where], tb->text, tb->used);
2924 where += tb->used;
2927 } else if (sel && tb->selected) {
2929 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2930 text = wrealloc(text, where+1);
2931 text[where++] = '\n';
2934 if(tb->blank)
2935 goto _gSnext;
2937 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2938 memcpy(&text[where], &tb->text[tb->s_begin],
2939 tb->s_end - tb->s_begin);
2940 where += tb->s_end - tb->s_begin;
2945 _gSnext:tb = tb->next;
2948 /* +1 for the end of string, let's be nice */
2949 text = wrealloc(text, where+1);
2950 text[where] = 0;
2951 return text;
2955 static void
2956 releaseStreamObjects(void *data)
2958 if(data)
2959 wfree(data);
2962 WMArray *
2963 getStreamObjects(WMText *tPtr, int sel)
2965 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2966 WMData *data;
2967 char *stream;
2968 unsigned short len;
2969 char *start, *fa, *desc;
2971 stream = getStream(tPtr, sel, 1);
2972 if(!stream)
2973 return NULL;
2975 start = stream;
2976 while (start) {
2978 fa = strchr(start, 0xFA);
2979 if (fa) {
2980 if((int)(fa - start)>0) {
2981 desc = start;
2982 desc[(int)(fa - start)] = 0;
2983 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2984 WMSetDataFormat(data, TYPETEXT);
2985 WMAddToArray(array, (void *) data);
2988 len = *(fa+1)*0xff + *(fa+2);
2989 data = WMCreateDataWithBytes((void *)(fa+4), len);
2990 WMSetDataFormat(data, *(fa+3));
2991 WMAddToArray(array, (void *) data);
2992 start = fa + len + 4;
2994 } else {
2995 if (start && strlen(start)) {
2996 data = WMCreateDataWithBytes((void *)start, strlen(start));
2997 WMSetDataFormat(data, TYPETEXT);
2998 WMAddToArray(array, (void *) data);
3000 start = fa;
3004 wfree(stream);
3005 return array;
3009 WMText *
3010 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3012 Text *tPtr;
3014 tPtr = wmalloc(sizeof(Text));
3015 memset(tPtr, 0, sizeof(Text));
3016 tPtr->widgetClass = WC_Text;
3017 tPtr->view = W_CreateView(W_VIEW(parent));
3018 if (!tPtr->view) {
3019 perror("could not create text's view\n");
3020 wfree(tPtr);
3021 return NULL;
3023 tPtr->view->self = tPtr;
3024 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
3025 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3026 W_ResizeView(tPtr->view, 250, 200);
3028 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
3029 tPtr->bgGC = WMColorGC(tPtr->dColor);
3030 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3031 WMReleaseColor(tPtr->dColor);
3033 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3034 tPtr->fgGC = WMColorGC(tPtr->dColor);
3036 tPtr->ruler = NULL;
3037 tPtr->vS = NULL;
3038 tPtr->hS = NULL;
3040 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3042 tPtr->view->delegate = &_TextViewDelegate;
3044 tPtr->delegate = NULL;
3046 #if DO_BLINK
3047 tPtr->timerID = NULL;
3048 #endif
3050 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3051 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3052 handleEvents, tPtr);
3054 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3055 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3056 handleActionEvents, tPtr);
3058 WMAddNotificationObserver(ownershipObserver, tPtr,
3059 "_lostOwnership", tPtr);
3061 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3062 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3066 char *types[3] = {"application/X-color", "application/X-image", NULL};
3067 WMRegisterViewForDraggedTypes(tPtr->view, types);
3070 WMAddNotificationObserver(fontChanged, tPtr,
3071 "WMFontPanelDidChangeNotification", tPtr);
3073 tPtr->firstTextBlock = NULL;
3074 tPtr->lastTextBlock = NULL;
3075 tPtr->currentTextBlock = NULL;
3076 tPtr->tpos = 0;
3078 tPtr->gfxItems = WMCreateArray(4);
3080 tPtr->parser = parser;
3081 tPtr->writer = writer;
3083 tPtr->sel.x = tPtr->sel.y = 2;
3084 tPtr->sel.w = tPtr->sel.h = 0;
3086 tPtr->clicked.x = tPtr->clicked.y = 2;
3088 tPtr->visible.x = tPtr->visible.y = 2;
3089 tPtr->visible.h = tPtr->view->size.height;
3090 tPtr->visible.w = tPtr->view->size.width - 4;
3092 tPtr->cursor.x = -23;
3094 tPtr->docWidth = 0;
3095 tPtr->docHeight = 0;
3096 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3097 default_bullet);
3098 tPtr->db = (Pixmap) NULL;
3099 tPtr->bgPixmap = NULL;
3101 tPtr->margins = WMGetRulerMargins(NULL);
3102 tPtr->margins->right = tPtr->visible.w;
3103 tPtr->nMargins = 1;
3105 tPtr->flags.rulerShown = False;
3106 tPtr->flags.monoFont = False;
3107 tPtr->flags.focused = False;
3108 tPtr->flags.editable = True;
3109 tPtr->flags.ownsSelection = False;
3110 tPtr->flags.pointerGrabbed = False;
3111 tPtr->flags.extendSelection = False;
3112 tPtr->flags.frozen = False;
3113 tPtr->flags.cursorShown = True;
3114 tPtr->flags.acceptsGraphic = False;
3115 tPtr->flags.horizOnDemand = False;
3116 tPtr->flags.needsLayOut = False;
3117 tPtr->flags.ignoreNewLine = False;
3118 tPtr->flags.indentNewLine = False;
3119 tPtr->flags.laidOut = False;
3120 tPtr->flags.ownsSelection = False;
3121 tPtr->flags.waitingForSelection = False;
3122 tPtr->flags.prepend = False;
3123 tPtr->flags.isOverGraphic = False;
3124 tPtr->flags.relief = WRSunken;
3125 tPtr->flags.isOverGraphic = 0;
3126 tPtr->flags.alignment = WALeft;
3127 tPtr->flags.first = True;
3129 return tPtr;
3132 void
3133 WMPrependTextStream(WMText *tPtr, char *text)
3135 CHECK_CLASS(tPtr, WC_Text);
3137 if(!text) {
3138 if (tPtr->flags.ownsSelection)
3139 releaseSelection(tPtr);
3140 clearText(tPtr);
3141 updateScrollers(tPtr);
3142 return;
3145 tPtr->flags.prepend = True;
3146 if (text && tPtr->parser)
3147 (tPtr->parser) (tPtr, (void *) text);
3148 else
3149 insertPlainText(tPtr, text);
3151 tPtr->flags.needsLayOut = True;
3152 tPtr->tpos = 0;
3153 if(!tPtr->flags.frozen) {
3154 layOutDocument(tPtr);
3159 void
3160 WMAppendTextStream(WMText *tPtr, char *text)
3162 CHECK_CLASS(tPtr, WC_Text);
3164 if(!text) {
3165 if (tPtr->flags.ownsSelection)
3166 releaseSelection(tPtr);
3167 clearText(tPtr);
3168 updateScrollers(tPtr);
3169 return;
3172 tPtr->flags.prepend = False;
3173 if (text && tPtr->parser)
3174 (tPtr->parser) (tPtr, (void *) text);
3175 else
3176 insertPlainText(tPtr, text);
3178 tPtr->flags.needsLayOut = True;
3179 if(tPtr->currentTextBlock)
3180 tPtr->tpos = tPtr->currentTextBlock->used;
3182 if(!tPtr->flags.frozen) {
3183 layOutDocument(tPtr);
3188 char *
3189 WMGetTextStream(WMText *tPtr)
3191 CHECK_CLASS(tPtr, WC_Text);
3192 return getStream(tPtr, 0, 0);
3195 char *
3196 WMGetTextSelectedStream(WMText *tPtr)
3198 CHECK_CLASS(tPtr, WC_Text);
3199 return getStream(tPtr, 1, 0);
3202 WMArray *
3203 WMGetTextObjects(WMText *tPtr)
3205 CHECK_CLASS(tPtr, WC_Text);
3206 return getStreamObjects(tPtr, 0);
3209 WMArray *
3210 WMGetTextSelectedObjects(WMText *tPtr)
3212 CHECK_CLASS(tPtr, WC_Text);
3213 return getStreamObjects(tPtr, 1);
3217 void
3218 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3220 CHECK_CLASS(tPtr, WC_Text);
3222 tPtr->delegate = delegate;
3226 void *
3227 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3228 char *description, WMColor *color,
3229 unsigned short first, unsigned short extraInfo)
3231 TextBlock *tb;
3233 if (!w || !description || !color)
3234 return NULL;
3236 tb = wmalloc(sizeof(TextBlock));
3237 if (!tb)
3238 return NULL;
3240 tb->text = wstrdup(description);
3241 tb->used = strlen(description);
3242 tb->blank = False;
3243 tb->d.widget = w;
3244 tb->color = WMRetainColor(color);
3245 tb->marginN = newMargin(tPtr, NULL);
3246 tb->allocated = extraInfo;
3247 tb->first = first;
3248 tb->kanji = False;
3249 tb->graphic = True;
3250 tb->object = True;
3251 tb->underlined = False;
3252 tb->selected = False;
3253 tb->script = 0;
3254 tb->sections = NULL;
3255 tb->nsections = 0;
3256 tb->prior = NULL;
3257 tb->next = NULL;
3259 return tb;
3263 void *
3264 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3265 char *description, WMColor *color,
3266 unsigned short first, unsigned short extraInfo)
3268 TextBlock *tb;
3270 if (!p || !description || !color)
3271 return NULL;
3273 tb = wmalloc(sizeof(TextBlock));
3274 if (!tb)
3275 return NULL;
3277 tb->text = wstrdup(description);
3278 tb->used = strlen(description);
3279 tb->blank = False;
3280 tb->d.pixmap = WMRetainPixmap(p);
3281 tb->color = WMRetainColor(color);
3282 tb->marginN = newMargin(tPtr, NULL);
3283 tb->allocated = extraInfo;
3284 tb->first = first;
3285 tb->kanji = False;
3286 tb->graphic = True;
3287 tb->object = False;
3288 tb->underlined = False;
3289 tb->selected = False;
3290 tb->script = 0;
3291 tb->sections = NULL;
3292 tb->nsections = 0;
3293 tb->prior = NULL;
3294 tb->next = NULL;
3296 return tb;
3300 void*
3301 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3302 unsigned short first, unsigned short len)
3304 TextBlock *tb;
3306 if (!font || !color)
3307 return NULL;
3309 tb = wmalloc(sizeof(TextBlock));
3310 if (!tb)
3311 return NULL;
3313 tb->allocated = reqBlockSize(len);
3314 tb->text = (char *)wmalloc(tb->allocated);
3315 memset(tb->text, 0, tb->allocated);
3317 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3318 *tb->text = ' ';
3319 tb->used = 1;
3320 tb->blank = True;
3321 } else {
3322 memcpy(tb->text, text, len);
3323 tb->used = len;
3324 tb->blank = False;
3326 tb->text[tb->used] = 0;
3328 tb->d.font = WMRetainFont(font);
3329 tb->color = WMRetainColor(color);
3330 tb->marginN = newMargin(tPtr, NULL);
3331 tb->first = first;
3332 tb->kanji = False;
3333 tb->graphic = False;
3334 tb->underlined = False;
3335 tb->selected = False;
3336 tb->script = 0;
3337 tb->sections = NULL;
3338 tb->nsections = 0;
3339 tb->prior = NULL;
3340 tb->next = NULL;
3341 return tb;
3344 void
3345 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3346 unsigned int kanji, unsigned int underlined, int script,
3347 WMRulerMargins *margins)
3349 TextBlock *tb = (TextBlock *) vtb;
3350 if (!tb)
3351 return;
3353 tb->first = first;
3354 tb->kanji = kanji;
3355 tb->underlined = underlined;
3356 tb->script = script;
3357 tb->marginN = newMargin(tPtr, margins);
3360 void
3361 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3362 unsigned int *kanji, unsigned int *underlined, int *script,
3363 WMRulerMargins *margins)
3365 TextBlock *tb = (TextBlock *) vtb;
3366 if (!tb)
3367 return;
3369 if (first) *first = tb->first;
3370 if (kanji) *kanji = tb->kanji;
3371 if (underlined) *underlined = tb->underlined;
3372 if (script) *script = tb->script;
3373 if (margins) margins = &tPtr->margins[tb->marginN];
3378 void
3379 WMPrependTextBlock(WMText *tPtr, void *vtb)
3381 TextBlock *tb = (TextBlock *)vtb;
3383 if (!tPtr || !tb)
3384 return;
3386 if (tb->graphic) {
3387 if(tb->object) {
3388 WMWidget *w = tb->d.widget;
3389 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3390 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3391 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3394 WMAddToArray(tPtr->gfxItems, (void *)tb);
3395 tPtr->tpos = 0;
3396 } else tPtr->tpos = tb->used;
3398 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3399 tb->next = tb->prior = NULL;
3400 tb->first = True;
3401 tPtr->lastTextBlock = tPtr->firstTextBlock
3402 = tPtr->currentTextBlock = tb;
3403 return;
3406 if(!tb->first) {
3407 tb->marginN = tPtr->currentTextBlock->marginN;
3410 tb->next = tPtr->currentTextBlock;
3411 tb->prior = tPtr->currentTextBlock->prior;
3412 if (tPtr->currentTextBlock->prior)
3413 tPtr->currentTextBlock->prior->next = tb;
3415 tPtr->currentTextBlock->prior = tb;
3416 if (!tb->prior)
3417 tPtr->firstTextBlock = tb;
3419 tPtr->currentTextBlock = tb;
3423 void
3424 WMAppendTextBlock(WMText *tPtr, void *vtb)
3426 TextBlock *tb = (TextBlock *)vtb;
3428 if (!tPtr || !tb)
3429 return;
3431 if (tb->graphic) {
3432 if(tb->object) {
3433 WMWidget *w = tb->d.widget;
3434 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3435 (W_VIEW(w))->attribs.cursor =
3436 tPtr->view->screen->defaultCursor;
3437 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3440 WMAddToArray(tPtr->gfxItems, (void *)tb);
3441 tPtr->tpos = 0;
3442 } else tPtr->tpos = tb->used;
3444 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3445 tb->next = tb->prior = NULL;
3446 tb->first = True;
3447 tPtr->lastTextBlock = tPtr->firstTextBlock
3448 = tPtr->currentTextBlock = tb;
3449 return;
3452 if(!tb->first) {
3453 tb->marginN = tPtr->currentTextBlock->marginN;
3456 tb->next = tPtr->currentTextBlock->next;
3457 tb->prior = tPtr->currentTextBlock;
3458 if (tPtr->currentTextBlock->next)
3459 tPtr->currentTextBlock->next->prior = tb;
3461 tPtr->currentTextBlock->next = tb;
3463 if (!tb->next)
3464 tPtr->lastTextBlock = tb;
3466 tPtr->currentTextBlock = tb;
3469 void *
3470 WMRemoveTextBlock(WMText *tPtr)
3472 TextBlock *tb = NULL;
3474 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3475 || !tPtr->currentTextBlock) {
3476 /* printf("cannot remove non existent TextBlock!\n"); */
3477 return NULL;
3480 tb = tPtr->currentTextBlock;
3481 if (tb->graphic) {
3482 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3484 if(tb->object) {
3485 WMUnmapWidget(tb->d.widget);
3489 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3490 if (tPtr->currentTextBlock->next)
3491 tPtr->currentTextBlock->next->prior = NULL;
3493 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3494 tPtr->currentTextBlock = tPtr->firstTextBlock;
3496 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3497 tPtr->currentTextBlock->prior->next = NULL;
3498 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3499 tPtr->currentTextBlock = tPtr->lastTextBlock;
3500 } else {
3501 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3502 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3503 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3506 return (void *)tb;
3509 void
3510 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3512 TextBlock *tb = (TextBlock *)vtb;
3513 if (!tPtr || !tb)
3514 return;
3516 if (tb->graphic) {
3517 if(tb->object) {
3518 /* naturally, there's a danger to destroying
3519 widgets whose action brings us here:
3520 ie. press a button to destroy it... need to
3521 find a safer way. till then... this stays commented out */
3522 /* WMDestroyWidget(tb->d.widget);
3523 wfree(tb->d.widget); */
3524 tb->d.widget = NULL;
3525 } else {
3526 WMReleasePixmap(tb->d.pixmap);
3527 tb->d.pixmap = NULL;
3529 } else {
3530 WMReleaseFont(tb->d.font);
3533 WMReleaseColor(tb->color);
3534 if (tb->sections && tb->nsections > 0)
3535 wfree(tb->sections);
3536 wfree(tb->text);
3537 wfree(tb);
3538 tb = NULL;
3543 void
3544 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3546 if (!tPtr)
3547 return;
3549 if (color)
3550 tPtr->fgGC = WMColorGC(color);
3551 else {
3552 WMColor *color = WMBlackColor(tPtr->view->screen);
3553 tPtr->fgGC = WMColorGC(color);
3554 WMReleaseColor(color);
3557 paintText(tPtr);
3560 void
3561 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3563 if (!tPtr)
3564 return;
3566 if (color) {
3567 tPtr->bgGC = WMColorGC(color);
3568 W_SetViewBackgroundColor(tPtr->view, color);
3569 } else {
3570 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3571 W_SetViewBackgroundColor(tPtr->view,
3572 WMWhiteColor(tPtr->view->screen));
3575 paintText(tPtr);
3578 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3580 if (!tPtr)
3581 return;
3583 if (tPtr->bgPixmap)
3584 WMReleasePixmap(tPtr->bgPixmap);
3586 if (pixmap)
3587 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3588 else
3589 tPtr->bgPixmap = NULL;
3592 void
3593 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3595 if (!tPtr)
3596 return;
3597 tPtr->flags.relief = relief;
3598 textDidResize(tPtr->view->delegate, tPtr->view);
3601 void
3602 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3604 if (!tPtr)
3605 return;
3607 if (shouldhave && !tPtr->hS) {
3608 tPtr->hS = WMCreateScroller(tPtr);
3609 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3610 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3611 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3612 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3613 WMMapWidget(tPtr->hS);
3614 } else if (!shouldhave && tPtr->hS) {
3615 WMUnmapWidget(tPtr->hS);
3616 WMDestroyWidget(tPtr->hS);
3617 tPtr->hS = NULL;
3620 tPtr->hpos = 0;
3621 tPtr->prevHpos = 0;
3622 textDidResize(tPtr->view->delegate, tPtr->view);
3626 void
3627 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3629 if (!tPtr)
3630 return;
3632 if(shouldhave && !tPtr->ruler) {
3633 tPtr->ruler = WMCreateRuler(tPtr);
3634 (W_VIEW(tPtr->ruler))->attribs.cursor =
3635 tPtr->view->screen->defaultCursor;
3636 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3637 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3638 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3639 } else if(!shouldhave && tPtr->ruler) {
3640 WMShowTextRuler(tPtr, False);
3641 WMDestroyWidget(tPtr->ruler);
3642 tPtr->ruler = NULL;
3644 textDidResize(tPtr->view->delegate, tPtr->view);
3647 void
3648 WMShowTextRuler(WMText *tPtr, Bool show)
3650 if(!tPtr)
3651 return;
3652 if(!tPtr->ruler)
3653 return;
3655 if(tPtr->flags.monoFont)
3656 show = False;
3658 tPtr->flags.rulerShown = show;
3659 if(show) {
3660 WMMapWidget(tPtr->ruler);
3661 } else {
3662 WMUnmapWidget(tPtr->ruler);
3665 textDidResize(tPtr->view->delegate, tPtr->view);
3668 Bool
3669 WMGetTextRulerShown(WMText *tPtr)
3671 if(!tPtr)
3672 return 0;
3674 if(!tPtr->ruler)
3675 return 0;
3677 return tPtr->flags.rulerShown;
3681 void
3682 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3684 if (!tPtr)
3685 return;
3687 if (shouldhave && !tPtr->vS) {
3688 tPtr->vS = WMCreateScroller(tPtr);
3689 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3690 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3691 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3692 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3693 WMMapWidget(tPtr->vS);
3694 } else if (!shouldhave && tPtr->vS) {
3695 WMUnmapWidget(tPtr->vS);
3696 WMDestroyWidget(tPtr->vS);
3697 tPtr->vS = NULL;
3700 tPtr->vpos = 0;
3701 tPtr->prevVpos = 0;
3702 textDidResize(tPtr->view->delegate, tPtr->view);
3707 Bool
3708 WMScrollText(WMText *tPtr, int amount)
3710 Bool scroll=False;
3711 if (!tPtr)
3712 return False;
3713 if (amount == 0 || !tPtr->view->flags.realized)
3714 return False;
3716 if (amount < 0) {
3717 if (tPtr->vpos > 0) {
3718 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3719 else tPtr->vpos=0;
3720 scroll=True;
3722 } else {
3723 int limit = tPtr->docHeight - tPtr->visible.h;
3724 if (tPtr->vpos < limit) {
3725 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3726 else tPtr->vpos = limit;
3727 scroll = True;
3731 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3732 updateScrollers(tPtr);
3733 paintText(tPtr);
3735 tPtr->prevVpos = tPtr->vpos;
3736 return scroll;
3739 Bool
3740 WMPageText(WMText *tPtr, Bool direction)
3742 if (!tPtr) return False;
3743 if (!tPtr->view->flags.realized) return False;
3745 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3748 void
3749 WMSetTextEditable(WMText *tPtr, Bool editable)
3751 if (!tPtr)
3752 return;
3753 tPtr->flags.editable = editable;
3756 int
3757 WMGetTextEditable(WMText *tPtr)
3759 if (!tPtr)
3760 return 0;
3761 return tPtr->flags.editable;
3764 void
3765 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3767 if (!tPtr)
3768 return;
3769 tPtr->flags.indentNewLine = indent;
3772 void
3773 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3775 if (!tPtr)
3776 return;
3777 tPtr->flags.ignoreNewLine = ignore;
3780 Bool
3781 WMGetTextIgnoresNewline(WMText *tPtr)
3783 if (!tPtr)
3784 return True;
3785 return tPtr->flags.ignoreNewLine;
3788 void
3789 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3791 if (!tPtr)
3792 return;
3794 if (mono) {
3795 if(tPtr->flags.rulerShown)
3796 WMShowTextRuler(tPtr, False);
3797 if(tPtr->flags.alignment != WALeft)
3798 tPtr->flags.alignment = WALeft;
3801 tPtr->flags.monoFont = mono;
3802 textDidResize(tPtr->view->delegate, tPtr->view);
3805 Bool
3806 WMGetTextUsesMonoFont(WMText *tPtr)
3808 if (!tPtr)
3809 return True;
3810 return tPtr->flags.monoFont;
3814 void
3815 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3817 if (!tPtr)
3818 return;
3820 WMReleaseFont(tPtr->dFont);
3821 if (font)
3822 tPtr->dFont = WMRetainFont(font);
3823 else
3824 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3827 WMFont *
3828 WMGetTextDefaultFont(WMText *tPtr)
3830 if (!tPtr)
3831 return NULL;
3832 else
3833 return WMRetainFont(tPtr->dFont);
3836 void
3837 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3839 if (!tPtr)
3840 return;
3842 WMReleaseColor(tPtr->dColor);
3843 if (color)
3844 tPtr->dColor = WMRetainColor(color);
3845 else
3846 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3849 WMColor *
3850 WMGetTextDefaultColor(WMText *tPtr)
3852 if (!tPtr)
3853 return NULL;
3854 else
3855 return WMRetainColor(tPtr->dColor);
3858 void
3859 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3861 if (!tPtr)
3862 return;
3863 if(tPtr->flags.monoFont)
3864 tPtr->flags.alignment = WALeft;
3865 else
3866 tPtr->flags.alignment = alignment;
3867 WMThawText(tPtr);
3870 int
3871 WMGetTextInsertType(WMText *tPtr)
3873 if (!tPtr)
3874 return 0;
3875 return tPtr->flags.prepend;
3879 void
3880 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3882 if (!tPtr || !color)
3883 return;
3885 setSelectionProperty(tPtr, NULL, color, -1);
3888 WMColor *
3889 WMGetTextSelectionColor(WMText *tPtr)
3891 TextBlock *tb;
3893 if (!tPtr)
3894 return NULL;
3896 tb = tPtr->currentTextBlock;
3898 if (!tb || !tPtr->flags.ownsSelection)
3899 return NULL;
3901 if(!tb->selected)
3902 return NULL;
3904 return tb->color;
3908 void
3909 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3911 if (!tPtr || !font)
3912 return;
3914 setSelectionProperty(tPtr, font, NULL, -1) ;
3917 WMFont *
3918 WMGetTextSelectionFont(WMText *tPtr)
3920 TextBlock *tb;
3922 if (!tPtr)
3923 return NULL;
3925 tb = tPtr->currentTextBlock;
3927 if (!tb || !tPtr->flags.ownsSelection)
3928 return NULL;
3930 if(!tb->selected)
3931 return NULL;
3933 if(tb->graphic) {
3934 tb = getFirstNonGraphicBlockFor(tb, 1);
3935 if(!tb)
3936 return NULL;
3938 return (tb->selected ? tb->d.font : NULL);
3942 void
3943 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3945 if (!tPtr || (underlined!=0 && underlined !=1))
3946 return;
3948 setSelectionProperty(tPtr, NULL, NULL, underlined);
3952 int
3953 WMGetTextSelectionUnderlined(WMText *tPtr)
3955 TextBlock *tb;
3957 if (!tPtr)
3958 return 0;
3960 tb = tPtr->currentTextBlock;
3962 if (!tb || !tPtr->flags.ownsSelection)
3963 return 0;
3965 if(!tb->selected)
3966 return 0;
3968 return tb->underlined;
3972 void
3973 WMFreezeText(WMText *tPtr)
3975 if (!tPtr)
3976 return;
3978 tPtr->flags.frozen = True;
3982 void
3983 WMThawText(WMText *tPtr)
3985 if (!tPtr)
3986 return;
3988 tPtr->flags.frozen = False;
3990 if(tPtr->flags.monoFont) {
3991 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3992 TextBlock *tb;
3994 /* make sure to unmap widgets no matter where they are */
3995 /* they'll be later remapped if needed by paintText */
3996 for(j=0; j<c; j++) {
3997 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3998 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3999 WMUnmapWidget(tb->d.widget);
4005 tPtr->flags.laidOut = False;
4006 layOutDocument(tPtr);
4007 updateScrollers(tPtr);
4008 paintText(tPtr);
4009 tPtr->flags.needsLayOut = False;
4013 /* find first occurence of a string */
4014 static char *
4015 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4016 Bool caseSensitive)
4018 char *ptr;
4020 if(!haystack || !needle || !end)
4021 return NULL;
4023 for (ptr = haystack; ptr < end; ptr++) {
4024 if(caseSensitive) {
4025 if (*ptr == *needle && !strncmp(ptr, needle, len))
4026 return ptr;
4028 } else {
4029 if (tolower(*ptr) == tolower(*needle) &&
4030 !strncasecmp(ptr, needle, len))
4031 return ptr;
4035 return NULL;
4038 /* find last occurence of a string */
4039 static char *
4040 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4041 Bool caseSensitive)
4043 char *ptr;
4045 if(!haystack || !needle || !end)
4046 return NULL;
4048 for (ptr = haystack-2; ptr > end; ptr--) {
4049 if(caseSensitive) {
4050 if (*ptr == *needle && !strncmp(ptr, needle, len))
4051 return ptr;
4052 } else {
4053 if (tolower(*ptr) == tolower(*needle) &&
4054 !strncasecmp(ptr, needle, len))
4055 return ptr;
4059 return NULL;
4063 Bool
4064 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4065 Bool caseSensitive)
4067 TextBlock *tb;
4068 char *mark=NULL;
4069 unsigned short pos;
4071 if (!tPtr || !needle)
4072 return False;
4074 #if 0
4075 if (! (tb = tPtr->currentTextBlock)) {
4076 if (! (tb = ( (direction > 0) ?
4077 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4078 return False;
4080 } else {
4081 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4082 tb = (direction>0) ? tb->next : tb->prior; */
4083 if(tb != tPtr->lastTextBlock)
4084 tb = tb->prior;
4086 #endif
4087 tb = tPtr->currentTextBlock;
4088 pos = tPtr->tpos;
4091 while(tb) {
4092 if (!tb->graphic) {
4094 if(direction > 0) {
4095 if(pos+1 < tb->used)
4096 pos++;
4098 if(tb->used - pos> 0 && pos > 0) {
4099 mark = mystrstr(&tb->text[pos], needle,
4100 strlen(needle), &tb->text[tb->used], caseSensitive);
4102 } else {
4103 tb = tb->next;
4104 pos = 0;
4105 continue;
4108 } else {
4109 if(pos-1 > 0)
4110 pos--;
4112 if(pos > 0) {
4113 mark = mystrrstr(&tb->text[pos], needle,
4114 strlen(needle), tb->text, caseSensitive);
4115 } else {
4116 tb = tb->prior;
4117 if(!tb)
4118 return False;
4119 pos = tb->used;
4120 continue;
4125 if(mark) {
4126 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4128 tPtr->tpos = (int)(mark - tb->text);
4129 tPtr->currentTextBlock = tb;
4130 updateCursorPosition(tPtr);
4131 tPtr->sel.y = tPtr->cursor.y+5;
4132 tPtr->sel.h = tPtr->cursor.h-10;
4133 tPtr->sel.x = tPtr->cursor.x +1;
4134 tPtr->sel.w = WMIN(WMWidthOfString(font,
4135 &tb->text[tPtr->tpos], strlen(needle)),
4136 tPtr->docWidth - tPtr->sel.x);
4137 tPtr->flags.ownsSelection = True;
4138 paintText(tPtr);
4140 return True;
4144 tb = (direction>0) ? tb->next : tb->prior;
4145 if(tb) {
4146 pos = (direction>0) ? 0 : tb->used;
4150 return False;
4154 Bool
4155 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4157 if (!tPtr)
4158 return False;
4160 if (!tPtr->flags.ownsSelection)
4161 return False;
4163 removeSelection(tPtr);
4165 if(replacement) {
4166 insertTextInteractively(tPtr, replacement, strlen(replacement));
4167 updateCursorPosition(tPtr);
4168 paintText(tPtr);
4171 return True;