various table widget updates
[wmaker-crm.git] / WINGs / wtext.c
blob2f144f2206b86a8ce31550638943a6f3c77ee45f
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 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - assess danger of destroying widgets whose actions link to other pages
20 * - Tabs now are simply replaced by 4 spaces...
21 * - redo blink code to reduce paint event... use pixmap buffer...
22 * - add paragraph support (full) and '\n' code in getStream..
26 /* a Section is a section of a TextBlock that describes what parts
27 of a TextBlock has been laid out on which "line"...
28 o this greatly aids redraw, scroll and selection.
29 o this is created during layoutLine, but may be later modified.
30 o there may be many Sections per TextBlock, hence the array */
31 typedef struct {
32 unsigned int x, y; /* where to draw it from */
33 unsigned short w, h; /* its width and height */
34 unsigned short begin; /* where the layout begins */
35 unsigned short end ; /* where it ends */
36 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
37 unsigned short last:1; /* is it the last section on a "line"? */
38 unsigned int _y:31; /* the "line" it and other textblocks are on */
39 } Section;
42 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
43 o text for the block, color and font
44 o or a pointer to the pixmap
45 o OR a pointer to the widget and the (text) description for its graphic
48 typedef struct _TextBlock {
49 struct _TextBlock *next; /* next text block in linked list */
50 struct _TextBlock *prior; /* prior text block in linked list */
52 char *text; /* pointer to text (could be kanji) */
53 /* or to the object's description */
54 union {
55 WMFont *font; /* the font */
56 WMWidget *widget; /* the embedded widget */
57 WMPixmap *pixmap; /* the pixmap */
58 } d; /* description */
60 unsigned short used; /* number of chars in this block */
61 unsigned short allocated; /* size of allocation (in chars) */
62 WMColor *color; /* the color */
64 Section *sections; /* the region for layouts (a growable array) */
65 /* an _array_! of size _nsections_ */
67 unsigned short s_begin; /* where the selection begins */
68 unsigned short s_end; /* where it ends */
70 unsigned int first:1; /* first TextBlock in paragraph */
71 unsigned int blank:1; /* ie. blank paragraph */
72 unsigned int kanji:1; /* is of 16-bit characters or not */
73 unsigned int graphic:1; /* graphic or text: text=0 */
74 unsigned int object:1; /* embedded object or pixmap */
75 unsigned int underlined:1; /* underlined or not */
76 unsigned int selected:1; /* selected or not */
77 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
78 int script:8; /* script in points: negative for subscript */
79 unsigned int marginN:8; /* which of the margins in the tPtr to use */
80 unsigned int nClicks:2; /* single, double, triple clicks */
81 unsigned int RESERVED:7;
82 } TextBlock;
85 /* I'm lazy: visible.h vs. visible.size.height :-) */
86 typedef struct {
87 int y, x, h, w;
88 } myRect;
91 typedef struct W_Text {
92 W_Class widgetClass; /* the class number of this widget */
93 W_View *view; /* the view referring to this instance */
95 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
97 WMScroller *vS; /* the vertical scroller */
98 unsigned int vpos; /* the current vertical position */
99 unsigned int prevVpos; /* the previous vertical position */
101 WMScroller *hS; /* the horizontal scroller */
102 unsigned int hpos; /* the current horizontal position */
103 unsigned int prevHpos; /* the previous horizontal position */
105 WMFont *dFont; /* the default font */
106 WMColor *dColor; /* the default color */
107 WMPixmap *dBulletPix; /* the default pixmap for bullets */
109 GC bgGC; /* the background GC to draw with */
110 GC fgGC; /* the foreground GC to draw with */
111 Pixmap db; /* the buffer on which to draw */
112 WMPixmap *bgPixmap; /* the background pixmap */
114 myRect visible; /* the actual rectangle that can be drawn into */
115 myRect cursor; /* the position and (height) of cursor */
116 myRect sel; /* the selection rectangle */
118 WMPoint clicked; /* where in the _document_ was clicked */
120 unsigned short tpos; /* the position in the currentTextBlock */
121 unsigned short docWidth; /* the width of the entire document */
122 unsigned int docHeight; /* the height of the entire document */
124 TextBlock *firstTextBlock;
125 TextBlock *lastTextBlock;
126 TextBlock *currentTextBlock;
128 WMArray *gfxItems; /* a nice array of graphic items */
130 #if DO_BLINK
131 WMHandlerID timerID; /* for nice twinky-winky */
132 #endif
134 WMAction *parser;
135 WMAction *writer;
136 WMTextDelegate *delegate;
137 Time lastClickTime;
139 WMRulerMargins *margins; /* an array of margins */
141 unsigned int nMargins:7; /* the total number of margins in use */
142 struct {
143 unsigned int monoFont:1; /* whether to ignore formats and graphic */
144 unsigned int focused:1; /* whether this instance has input focus */
145 unsigned int editable:1; /* "silly user, you can't edit me" */
146 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
147 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
148 unsigned int extendSelection:1; /* shift-drag to select more regions */
150 unsigned int rulerShown:1; /* whether the ruler is shown or not */
151 unsigned int frozen:1; /* whether screen updates are to be made */
152 unsigned int cursorShown:1; /* whether to show the cursor */
153 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
154 unsigned int horizOnDemand:1;/* if a large image should appear*/
155 unsigned int needsLayOut:1; /* in case of Append/Deletes */
156 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
157 unsigned int indentNewLine:1;/* add " " for a newline typed */
158 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
159 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
160 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
161 WMAlignment alignment:2; /* the alignment for text */
162 WMReliefType relief:3; /* the relief to display with */
163 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
164 unsigned int first:1; /* for plain text parsing, newline? */
165 /* unsigned int RESERVED:1; */
166 } flags;
167 } Text;
170 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
177 #define TYPETEXT 0
179 static void
180 output(char *ptr, int len)
182 char s[len+1];
183 memcpy(s, ptr, len);
184 s[len] = 0;
185 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
186 printf("[%s]\n", s);
190 #if DO_BLINK
191 #define CURSOR_BLINK_ON_DELAY 600
192 #define CURSOR_BLINK_OFF_DELAY 400
193 #endif
195 static char *default_bullet[] = {
196 "6 6 4 1",
197 " c None s None", ". c black",
198 "X c white", "o c #808080",
199 " ... ",
200 ".XX.. ",
201 ".XX..o",
202 ".....o",
203 " ...oo",
204 " ooo "};
206 static void handleEvents(XEvent *event, void *data);
207 static void layOutDocument(Text *tPtr);
208 static void updateScrollers(Text *tPtr);
211 static int
212 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
214 unsigned int i=0;
216 for(i=0; i < tPtr->nMargins; i++) {
218 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
219 return i;
222 return -1;
227 static int
228 newMargin(Text *tPtr, WMRulerMargins *margins)
230 int n;
232 if (!margins) {
233 tPtr->margins[0].retainCount++;
234 return 0;
237 n = getMarginNumber(tPtr, margins);
239 if (n == -1) {
241 if(tPtr->nMargins >= 127) {
242 n = tPtr->nMargins-1;
243 return n;
246 tPtr->margins = wrealloc(tPtr->margins,
247 (++tPtr->nMargins)*sizeof(WMRulerMargins));
249 n = tPtr->nMargins-1;
250 tPtr->margins[n].left = margins->left;
251 tPtr->margins[n].first = margins->first;
252 tPtr->margins[n].body = margins->body;
253 tPtr->margins[n].right = margins->right;
254 /* for each tab... */
255 tPtr->margins[n].retainCount = 1;
256 } else {
257 tPtr->margins[n].retainCount++;
260 return n;
263 static Bool
264 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
266 unsigned short i, w, lw, selected = False, extend = False;
267 myRect sel;
270 /* if selection rectangle completely encloses the section */
271 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
272 && (tb->sections[s]._y + tb->sections[s].h
273 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
274 sel.x = 0;
275 sel.w = tPtr->visible.w;
276 selected = extend = True;
278 /* or if it starts on a line and then goes further down */
279 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
280 && (tb->sections[s]._y + tb->sections[s].h
281 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
282 && (tb->sections[s]._y + tb->sections[s].h
283 >= tPtr->visible.y + tPtr->sel.y) ) {
284 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
285 sel.w = tPtr->visible.w;
286 selected = extend = True;
288 /* or if it begins before a line, but ends on it */
289 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
290 && (tb->sections[s]._y + tb->sections[s].h
291 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
292 && (tb->sections[s]._y
293 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
295 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
296 sel.w = tPtr->sel.x + tPtr->sel.w;
297 else
298 sel.w = tPtr->sel.x;
300 sel.x = 0;
301 selected = True;
303 /* or if the selection rectangle lies entirely within a line */
304 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
305 && (tPtr->sel.w >= 2)
306 && (tb->sections[s]._y + tb->sections[s].h
307 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
308 sel.x = tPtr->sel.x;
309 sel.w = tPtr->sel.w;
310 selected = True;
313 if (selected) {
314 selected = False;
316 /* if not within (modified) selection rectangle */
317 if ( tb->sections[s].x > sel.x + sel.w
318 || tb->sections[s].x + tb->sections[s].w < sel.x)
319 return False;
321 if (tb->graphic) {
322 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
323 && tb->sections[s].x >= sel.x) {
324 rect->width = tb->sections[s].w;
325 rect->x = tb->sections[s].x;
326 selected = True;
328 } else {
330 i = tb->sections[s].begin;
331 lw = 0;
333 if (0&& tb->sections[s].x >= sel.x) {
334 tb->s_begin = tb->sections[s].begin;
335 goto _selEnd;
338 while (++i <= tb->sections[s].end) {
340 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
341 lw += w;
343 if (lw + tb->sections[s].x >= sel.x
344 || i == tb->sections[s].end ) {
345 lw -= w;
346 i--;
347 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
348 break;
352 if (i > tb->sections[s].end) {
353 printf("WasSelected: (i > tb->sections[s].end) \n");
354 return False;
357 _selEnd: rect->x = tb->sections[s].x + lw;
358 lw = 0;
359 while(++i <= tb->sections[s].end) {
361 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
362 lw += w;
364 if (lw + rect->x >= sel.x + sel.w
365 || i == tb->sections[s].end ) {
367 if (i != tb->sections[s].end) {
368 lw -= w;
369 i--;
372 rect->width = lw;
373 if (tb->sections[s].last && sel.x + sel.w
374 >= tb->sections[s].x + tb->sections[s].w
375 && extend ) {
376 rect->width += (tPtr->visible.w - rect->x - lw);
379 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
380 selected = True;
381 break;
382 } } } }
384 if (selected) {
385 rect->y = tb->sections[s]._y - tPtr->vpos;
386 rect->height = tb->sections[s].h;
387 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
389 return selected;
393 static void
394 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
396 TextBlock *tb;
397 int isFont=False;
399 tb = tPtr->firstTextBlock;
400 if (!tb || !tPtr->flags.ownsSelection)
401 return;
403 if(font && (!color || underlined==-1))
404 isFont = True;
406 while (tb) {
407 if (tPtr->flags.monoFont || tb->selected) {
409 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
410 || tb->graphic) {
412 if(isFont) {
413 if(!tb->graphic) {
414 WMReleaseFont(tb->d.font);
415 tb->d.font = WMRetainFont(font);
417 } else if(underlined !=-1) {
418 tb->underlined = underlined;
419 } else {
420 WMReleaseColor(tb->color);
421 tb->color = WMRetainColor(color);
424 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
426 TextBlock *midtb, *otb = tb;
428 if(underlined != -1) {
429 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
430 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
431 False, (tb->s_end - tb->s_begin));
432 } else {
433 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
434 &(tb->text[tb->s_begin]),
435 (isFont?font:tb->d.font),
436 (isFont?tb->color:color),
437 False, (tb->s_end - tb->s_begin));
441 if (midtb) {
442 if(underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
448 midtb->selected = !True;
449 midtb->s_begin = 0;
450 midtb->s_end = midtb->used;
451 tPtr->currentTextBlock = tb;
452 WMAppendTextBlock(tPtr, midtb);
453 tb = tPtr->currentTextBlock;
456 if (otb->used - otb->s_end > 0) {
457 TextBlock *ntb;
458 ntb = (TextBlock *)
459 WMCreateTextBlockWithText(tPtr,
460 &(otb->text[otb->s_end]), otb->d.font, otb->color,
461 False, otb->used - otb->s_end);
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
475 otb->selected = False;
476 otb->used = otb->s_begin;
480 tb = tb->next;
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
486 /* in case the size changed... */
487 if(isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
490 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
491 tPtr->sel.y = 3 + tb->sections[0]._y;
492 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
493 tPtr->sel.w = tb->sections[tb->nsections-1].w;
494 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
495 tPtr->sel.x = 0;
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
503 static Bool
504 removeSelection(Text *tPtr)
506 TextBlock *tb = NULL;
507 Bool first = False;
509 if (!(tb = tPtr->firstTextBlock))
510 return False;
512 while (tb) {
513 if (tb->selected) {
514 if(!first && !tb->graphic) {
515 WMReleaseFont(tPtr->dFont);
516 tPtr->dFont = WMRetainFont(tb->d.font);
517 first = True;
520 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
521 tPtr->currentTextBlock = tb;
522 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
523 tb = tPtr->currentTextBlock;
524 if (tb)
525 tPtr->tpos = 0;
526 continue;
528 } else if (tb->s_end <= tb->used) {
529 memmove(&(tb->text[tb->s_begin]),
530 &(tb->text[tb->s_end]), tb->used - tb->s_end);
531 tb->used -= (tb->s_end - tb->s_begin);
532 tb->selected = False;
533 tPtr->tpos = tb->s_begin;
538 tb = tb->next;
540 return True;
543 static TextBlock *
544 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
546 TextBlock *hold = tb;
548 if (!tb)
549 return NULL;
551 while (tb) {
552 if (!tb->graphic)
553 break;
554 tb = (dir? tb->next : tb->prior);
557 if(!tb) {
558 tb = hold;
559 while (tb) {
560 if (!tb->graphic)
561 break;
562 tb = (dir? tb->prior : tb->next);
566 if(!tb)
567 return NULL;
569 return tb;
573 static Bool
574 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
575 TextBlock *tb)
577 if (tPtr->flags.monoFont && tb->graphic) {
578 tb = getFirstNonGraphicBlockFor(tb, *dir);
579 if(!tb)
580 return 0;
582 if (tb->graphic) {
583 tPtr->currentTextBlock =
584 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
591 if(!tb->sections)
592 layOutDocument(tPtr);
593 if(!tb->sections)
594 return 0;
596 *dir = !(y <= tb->sections[0].y);
597 if(*dir) {
598 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
599 && (y >= tb->sections[0]._y ) ) {
600 /* if it's on the same line */
601 if(x < tb->sections[0].x)
602 *dir = 0;
604 } else {
605 if ( ( y <= tb->sections[tb->nsections-1]._y
606 + tb->sections[tb->nsections-1].h )
607 && (y >= tb->sections[tb->nsections-1]._y ) ) {
608 /* if it's on the same line */
609 if(x > tb->sections[tb->nsections-1].x)
610 *dir = 1;
614 return 1;
618 static void
619 paintText(Text *tPtr)
621 TextBlock *tb;
622 WMFont *font;
623 GC gc, greyGC;
624 char *text;
625 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
626 WMScreen *scr = tPtr->view->screen;
627 Display *dpy = tPtr->view->screen->display;
628 Window win = tPtr->view->window;
630 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
631 return;
633 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
634 0, 0, tPtr->visible.w, tPtr->visible.h);
636 if (tPtr->bgPixmap) {
637 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
638 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
639 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
642 if (! (tb = tPtr->currentTextBlock)) {
643 if (! (tb = tPtr->firstTextBlock)) {
644 goto _copy_area;
648 if (tPtr->flags.ownsSelection)
649 greyGC = WMColorGC(WMGrayColor(scr));
651 done = False;
655 /* first, which direction? Don't waste time looking all over,
656 since the parts to be drawn will most likely be near what
657 was previously drawn */
658 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
659 goto _copy_area;
661 while(tb) {
663 if (tb->graphic && tPtr->flags.monoFont)
664 goto _getSibling;
666 if(dir) {
667 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
668 + tb->sections[tb->nsections-1].h)
669 break;
670 } else {
671 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
672 + tb->sections[tb->nsections-1].h)
673 break;
676 _getSibling:
677 if(dir) {
678 if(tb->next)
679 tb = tb->next;
680 else break;
681 } else {
682 if(tb->prior)
683 tb = tb->prior;
684 else break;
689 /* first, place all text that can be viewed */
690 while (!done && tb) {
692 /* paragraph diagnostic
693 if(tb->blank) {tb->text[0] = 'F'; } */
695 if (tb->graphic) {
696 tb = tb->next;
697 continue;
700 tb->selected = False;
702 for(s=0; s<tb->nsections && !done; s++) {
704 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
705 done = True;
706 break;
709 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
710 continue;
712 if (tPtr->flags.monoFont) {
713 font = tPtr->dFont;
714 gc = tPtr->fgGC;
715 } else {
716 font = tb->d.font;
717 gc = WMColorGC(tb->color);
720 if (tPtr->flags.ownsSelection) {
721 XRectangle rect;
723 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
724 tb->selected = True;
725 XFillRectangle(dpy, tPtr->db, greyGC,
726 rect.x, rect.y, rect.width, rect.height);
730 prev_y = tb->sections[s]._y;
732 len = tb->sections[s].end - tb->sections[s].begin;
733 text = &(tb->text[tb->sections[s].begin]);
734 y = tb->sections[s].y - tPtr->vpos;
735 WMDrawString(scr, tPtr->db, gc, font,
736 tb->sections[s].x - tPtr->hpos, y, text, len);
738 if (!tPtr->flags.monoFont && tb->underlined) {
739 XDrawLine(dpy, tPtr->db, gc,
740 tb->sections[s].x - tPtr->hpos,
741 y + font->y + 1,
742 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
743 y + font->y + 1);
748 tb = (!done? tb->next : NULL);
752 /* now , show all graphic items that can be viewed */
753 c = WMGetArrayItemCount(tPtr->gfxItems);
754 if (c > 0 && !tPtr->flags.monoFont) {
755 int j, h;
757 for(j=0; j<c; j++) {
758 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
760 /* if it's not viewable, and mapped, unmap it */
761 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
762 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
764 if(tb->object) {
765 if ((W_VIEW(tb->d.widget))->flags.mapped) {
766 WMUnmapWidget(tb->d.widget);
769 } else {
770 /* if it's viewable, and not mapped, map it */
771 if(tb->object) {
772 W_View *view = W_VIEW(tb->d.widget);
774 if (!view->flags.realized)
775 WMRealizeWidget(tb->d.widget);
776 if(!view->flags.mapped) {
777 XMapWindow(view->screen->display, view->window);
778 XFlush(view->screen->display);
779 view->flags.mapped = 1;
783 if (tPtr->flags.ownsSelection) {
784 XRectangle rect;
786 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
787 tb->selected = True;
788 XFillRectangle(dpy, tPtr->db, greyGC,
789 rect.x, rect.y, rect.width, rect.height);
793 if(tb->object) {
794 WMMoveWidget(tb->d.widget,
795 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
796 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
797 h = WMWidgetHeight(tb->d.widget) + 1;
799 } else {
800 WMDrawPixmap(tb->d.pixmap, tPtr->db,
801 tb->sections[0].x - tPtr->hpos,
802 tb->sections[0].y - tPtr->vpos);
803 h = tb->d.pixmap->height + 1;
807 if (!tPtr->flags.monoFont && tb->underlined) {
808 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
809 tb->sections[0].x - tPtr->hpos,
810 tb->sections[0].y + h - tPtr->vpos,
811 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
812 tb->sections[0].y + h - tPtr->vpos);
813 } } } }
816 _copy_area:
817 if (tPtr->flags.editable && tPtr->flags.cursorShown
818 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
819 int y = tPtr->cursor.y - tPtr->vpos;
820 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
821 tPtr->cursor.x, y,
822 tPtr->cursor.x, y + tPtr->cursor.h);
825 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
826 tPtr->visible.w, tPtr->visible.h,
827 tPtr->visible.x, tPtr->visible.y);
829 W_DrawRelief(scr, win, 0, 0,
830 tPtr->view->size.width, tPtr->view->size.height,
831 tPtr->flags.relief);
833 if (tPtr->ruler && tPtr->flags.rulerShown)
834 XDrawLine(dpy, win, tPtr->fgGC,
835 2, 42, tPtr->view->size.width-4, 42);
839 static void
840 mouseOverObject(Text *tPtr, int x, int y)
842 TextBlock *tb;
843 Bool result = False;
845 x -= tPtr->visible.x;
846 x += tPtr->hpos;
847 y -= tPtr->visible.y;
848 y += tPtr->vpos;
850 if(tPtr->flags.ownsSelection) {
851 if(tPtr->sel.x <= x
852 && tPtr->sel.y <= y
853 && tPtr->sel.x + tPtr->sel.w >= x
854 && tPtr->sel.y + tPtr->sel.h >= y) {
855 tPtr->flags.isOverGraphic = 1;
856 result = True;
861 if(!result) {
862 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
864 if (c<1)
865 tPtr->flags.isOverGraphic = 0;
868 for(j=0; j<c; j++) {
869 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
871 if(!tb || !tb->sections) {
872 tPtr->flags.isOverGraphic = 0;
873 return;
876 if(!tb->object) {
877 if(tb->sections[0].x <= x
878 && tb->sections[0].y <= y
879 && tb->sections[0].x + tb->sections[0].w >= x
880 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
881 tPtr->flags.isOverGraphic = 3;
882 result = True;
883 break;
891 if(!result)
892 tPtr->flags.isOverGraphic = 0;
895 tPtr->view->attribs.cursor = (result?
896 tPtr->view->screen->defaultCursor
897 : tPtr->view->screen->textCursor);
899 XSetWindowAttributes attribs;
900 attribs.cursor = tPtr->view->attribs.cursor;
901 XChangeWindowAttributes(tPtr->view->screen->display,
902 tPtr->view->window, CWCursor,
903 &attribs);
907 #if DO_BLINK
909 static void
910 blinkCursor(void *data)
912 Text *tPtr = (Text*)data;
914 if (tPtr->flags.cursorShown) {
915 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
916 blinkCursor, data);
917 } else {
918 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
919 blinkCursor, data);
921 paintText(tPtr);
922 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
924 #endif
926 static void
927 updateCursorPosition(Text *tPtr)
929 TextBlock *tb = NULL;
930 int x, y, h, s;
932 if(tPtr->flags.needsLayOut)
933 layOutDocument(tPtr);
935 if (! (tb = tPtr->currentTextBlock)) {
936 if (! (tb = tPtr->firstTextBlock)) {
937 tPtr->tpos = 0;
938 tPtr->cursor.h = tPtr->dFont->height;
939 tPtr->cursor.y = 2;
940 tPtr->cursor.x = 2;
941 return;
946 if(tb->blank) {
947 tPtr->tpos = 0;
948 y = tb->sections[0].y;
949 h = tb->sections[0].h;
950 x = tb->sections[0].x;
952 } else if(tb->graphic) {
953 y = tb->sections[0].y;
954 h = tb->sections[0].h;
955 x = tb->sections[0].x;
957 } else {
958 if(tPtr->tpos > tb->used)
959 tPtr->tpos = tb->used;
961 for(s=0; s<tb->nsections-1; s++) {
963 if(tPtr->tpos >= tb->sections[s].begin
964 && tPtr->tpos <= tb->sections[s].end)
965 break;
968 y = tb->sections[s]._y;
969 h = tb->sections[s].h;
970 x = tb->sections[s].x + WMWidthOfString(
971 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
972 &tb->text[tb->sections[s].begin],
973 tPtr->tpos - tb->sections[s].begin);
976 tPtr->cursor.y = y;
977 tPtr->cursor.h = h;
978 tPtr->cursor.x = x;
981 /* scroll the bars if the cursor is not visible */
982 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
983 if(tPtr->cursor.y+tPtr->cursor.h
984 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
985 tPtr->vpos +=
986 (tPtr->cursor.y+tPtr->cursor.h+10
987 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
988 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
989 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
994 updateScrollers(tPtr);
998 static void
999 cursorToTextPosition(Text *tPtr, int x, int y)
1001 TextBlock *tb = NULL;
1002 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1003 char *text;
1005 if(tPtr->flags.needsLayOut)
1006 layOutDocument(tPtr);
1008 y += (tPtr->vpos - tPtr->visible.y);
1009 if (y<0)
1010 y = 0;
1012 x -= (tPtr->visible.x - 2);
1013 if (x<0)
1014 x=0;
1016 /* clicked is relative to document, not window... */
1017 tPtr->clicked.x = x;
1018 tPtr->clicked.y = y;
1020 if (! (tb = tPtr->currentTextBlock)) {
1021 if (! (tb = tPtr->firstTextBlock)) {
1022 tPtr->tpos = 0;
1023 tPtr->cursor.h = tPtr->dFont->height;
1024 tPtr->cursor.y = 2;
1025 tPtr->cursor.x = 2;
1026 return;
1030 /* first, which direction? Most likely, newly clicked
1031 position will be close to previous */
1032 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1033 return;
1036 s = (dir? 0 : tb->nsections-1);
1037 if ( y >= tb->sections[s]._y
1038 && y <= tb->sections[s]._y + tb->sections[s].h) {
1039 goto _doneV;
1042 /* get the first (or last) section of the TextBlock that
1043 lies about the vertical click point */
1044 done = False;
1045 while (!done && tb) {
1047 if (tPtr->flags.monoFont && tb->graphic) {
1048 if( (dir?tb->next:tb->prior))
1049 tb = (dir?tb->next:tb->prior);
1050 continue;
1053 s = (dir? 0 : tb->nsections-1);
1054 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1056 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1057 ( y >= tb->sections[s]._y ) ) ) {
1058 done = True;
1059 } else {
1060 dir? s++ : s--;
1064 if (!done) {
1065 if ( (dir? tb->next : tb->prior)) {
1066 tb = (dir ? tb->next : tb->prior);
1067 } else {
1068 pos = tb->used;
1069 break; /* goto _doneH; */
1075 if (s<0 || s>=tb->nsections) {
1076 s = (dir? tb->nsections-1 : 0);
1079 _doneV:
1080 /* we have the line, which TextBlock on that line is it? */
1081 pos = (dir?0:tb->sections[s].begin);
1082 if (tPtr->flags.monoFont && tb->graphic) {
1083 TextBlock *hold = tb;
1084 tb = getFirstNonGraphicBlockFor(hold, dir);
1086 if(!tb) {
1087 tPtr->tpos = 0;
1088 tb = hold;
1089 s = 0;
1090 goto _doNothing;
1095 if(tb->blank)
1096 _w = 0;
1098 _y = tb->sections[s]._y;
1100 while (tb) {
1102 if (tPtr->flags.monoFont && tb->graphic) {
1103 tb = (dir ? tb->next : tb->prior);
1104 continue;
1107 if (dir) {
1108 if (tb->graphic) {
1109 if(tb->object)
1110 _w = WMWidgetWidth(tb->d.widget)-5;
1111 else
1112 _w = tb->d.pixmap->width-5;
1114 if (tb->sections[0].x + _w >= x)
1115 break;
1116 } else {
1117 text = &(tb->text[tb->sections[s].begin]);
1118 len = tb->sections[s].end - tb->sections[s].begin;
1119 _w = WMWidthOfString(tb->d.font, text, len);
1120 if (tb->sections[s].x + _w >= x)
1121 break;
1124 } else {
1125 if (tb->sections[s].x <= x)
1126 break;
1129 if ((dir? tb->next : tb->prior)) {
1130 TextBlock *nxt = (dir? tb->next : tb->prior);
1131 if (tPtr->flags.monoFont && nxt->graphic) {
1132 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1133 if (!nxt) {
1134 pos = (dir?0:tb->sections[s].begin);
1135 tPtr->cursor.x = tb->sections[s].x;
1136 goto _doneH;
1140 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1141 /* this must be the last/first on this line. stop */
1142 pos = (dir? tb->sections[s].end : 0);
1143 tPtr->cursor.x = tb->sections[s].x;
1144 if (!tb->blank) {
1145 if (tb->graphic) {
1146 if(tb->object)
1147 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1148 else
1149 tPtr->cursor.x += tb->d.pixmap->width;
1150 } else if (pos > tb->sections[s].begin) {
1151 tPtr->cursor.x +=
1152 WMWidthOfString(tb->d.font,
1153 &(tb->text[tb->sections[s].begin]),
1154 pos - tb->sections[s].begin);
1157 goto _doneH;
1161 if ( (dir? tb->next : tb->prior)) {
1162 tb = (dir ? tb->next : tb->prior);
1163 } else {
1164 done = True;
1165 break;
1168 if (tb)
1169 s = (dir? 0 : tb->nsections-1);
1172 /* we have said TextBlock, now where within it? */
1173 if (tb) {
1174 if(tb->graphic) {
1175 int gw = (tb->object ?
1176 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1178 tPtr->cursor.x = tb->sections[0].x;
1180 if(x > tPtr->cursor.x + gw/2) {
1181 pos = 1;
1182 printf("here x%d: %d\n", x, tPtr->cursor.x + gw/2);
1183 tPtr->cursor.x += gw;
1184 } else pos = 0;
1186 s = 0;
1187 goto _doneH;
1189 } else {
1190 WMFont *f = tb->d.font;
1191 len = tb->sections[s].end - tb->sections[s].begin;
1192 text = &(tb->text[tb->sections[s].begin]);
1194 _w = x - tb->sections[s].x;
1195 pos = 0;
1197 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1198 pos++;
1200 tPtr->cursor.x = tb->sections[s].x +
1201 (pos? WMWidthOfString(f, text, pos) : 0);
1203 pos += tb->sections[s].begin;
1207 _doneH:
1208 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1209 _doNothing:
1210 if (!tb)
1211 printf("...for this app will surely crash :-)\n");
1213 tPtr->currentTextBlock = tb;
1214 tPtr->cursor.h = tb->sections[s].h;
1215 tPtr->cursor.y = tb->sections[s]._y;
1217 /* scroll the bars if the cursor is not visible */
1218 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1219 if(tPtr->cursor.y+tPtr->cursor.h
1220 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1221 tPtr->vpos +=
1222 (tPtr->cursor.y+tPtr->cursor.h+10
1223 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1224 updateScrollers(tPtr);
1225 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1226 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1227 updateScrollers(tPtr);
1235 static void
1236 updateScrollers(Text *tPtr)
1239 if (tPtr->flags.frozen)
1240 return;
1242 if (tPtr->vS) {
1243 if (tPtr->docHeight < tPtr->visible.h) {
1244 WMSetScrollerParameters(tPtr->vS, 0, 1);
1245 tPtr->vpos = 0;
1246 } else {
1247 float hmax = (float)(tPtr->docHeight);
1248 WMSetScrollerParameters(tPtr->vS,
1249 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1250 (float)tPtr->visible.h/hmax);
1252 } else tPtr->vpos = 0;
1254 if (tPtr->hS) {
1255 if (tPtr->docWidth < tPtr->visible.w) {
1256 WMSetScrollerParameters(tPtr->hS, 0, 1);
1257 tPtr->hpos = 0;
1258 } else {
1259 float wmax = (float)(tPtr->docWidth);
1260 WMSetScrollerParameters(tPtr->hS,
1261 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1262 (float)tPtr->visible.w/wmax);
1264 } else tPtr->hpos = 0;
1267 static void
1268 scrollersCallBack(WMWidget *w, void *self)
1270 Text *tPtr = (Text *)self;
1271 Bool scroll = False;
1272 int which;
1274 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1275 return;
1277 if (w == tPtr->vS) {
1278 int height;
1279 height = tPtr->visible.h;
1281 which = WMGetScrollerHitPart(tPtr->vS);
1282 switch(which) {
1284 case WSDecrementLine:
1285 if (tPtr->vpos > 0) {
1286 if (tPtr->vpos>16) tPtr->vpos-=16;
1287 else tPtr->vpos=0;
1288 scroll=True;
1290 break;
1292 case WSIncrementLine: {
1293 int limit = tPtr->docHeight - height;
1294 if (tPtr->vpos < limit) {
1295 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1296 else tPtr->vpos=limit;
1297 scroll = True;
1300 break;
1302 case WSDecrementPage:
1303 if(((int)tPtr->vpos - (int)height) >= 0)
1304 tPtr->vpos -= height;
1305 else
1306 tPtr->vpos = 0;
1308 scroll = True;
1309 break;
1311 case WSIncrementPage:
1312 tPtr->vpos += height;
1313 if (tPtr->vpos > (tPtr->docHeight - height))
1314 tPtr->vpos = tPtr->docHeight - height;
1315 scroll = True;
1316 break;
1319 case WSKnob:
1320 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1321 * (float)(tPtr->docHeight - height);
1322 scroll = True;
1323 break;
1325 case WSKnobSlot:
1326 case WSNoPart:
1327 break;
1329 scroll = (tPtr->vpos != tPtr->prevVpos);
1330 tPtr->prevVpos = tPtr->vpos;
1334 if (w == tPtr->hS) {
1335 int width = tPtr->visible.w;
1337 which = WMGetScrollerHitPart(tPtr->hS);
1338 switch(which) {
1340 case WSDecrementLine:
1341 if (tPtr->hpos > 0) {
1342 if (tPtr->hpos>16) tPtr->hpos-=16;
1343 else tPtr->hpos=0;
1344 scroll=True;
1345 }break;
1347 case WSIncrementLine: {
1348 int limit = tPtr->docWidth - width;
1349 if (tPtr->hpos < limit) {
1350 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1351 else tPtr->hpos=limit;
1352 scroll = True;
1353 }}break;
1355 case WSDecrementPage:
1356 if(((int)tPtr->hpos - (int)width) >= 0)
1357 tPtr->hpos -= width;
1358 else
1359 tPtr->hpos = 0;
1361 scroll = True;
1362 break;
1364 case WSIncrementPage:
1365 tPtr->hpos += width;
1366 if (tPtr->hpos > (tPtr->docWidth - width))
1367 tPtr->hpos = tPtr->docWidth - width;
1368 scroll = True;
1369 break;
1372 case WSKnob:
1373 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1374 * (float)(tPtr->docWidth - width);
1375 scroll = True;
1376 break;
1378 case WSKnobSlot:
1379 case WSNoPart:
1380 break;
1382 scroll = (tPtr->hpos != tPtr->prevHpos);
1383 tPtr->prevHpos = tPtr->hpos;
1386 if (scroll) {
1387 updateScrollers(tPtr);
1388 paintText(tPtr);
1394 typedef struct {
1395 TextBlock *tb;
1396 unsigned short begin, end; /* what part of the text block */
1397 } myLineItems;
1400 static int
1401 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1403 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1404 WMFont *font;
1405 char *text;
1406 TextBlock *tb, *tbsame=NULL;
1408 if(!items || nitems == 0)
1409 return 0;
1411 for(i=0; i<nitems; i++) {
1412 tb = items[i].tb;
1414 if (tb->graphic) {
1415 if (!tPtr->flags.monoFont) {
1416 if(tb->object) {
1417 WMWidget *wdt = tb->d.widget;
1418 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1419 if (tPtr->flags.alignment != WALeft)
1420 lw += WMWidgetWidth(wdt);
1421 } else {
1422 line_height = WMAX(line_height,
1423 tb->d.pixmap->height + max_d);
1424 if (tPtr->flags.alignment != WALeft)
1425 lw += tb->d.pixmap->width;
1429 } else {
1430 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1431 max_d = WMAX(max_d, abs(font->height-font->y));
1432 line_height = WMAX(line_height, font->height + max_d);
1433 text = &(tb->text[items[i].begin]);
1434 len = items[i].end - items[i].begin;
1435 if (tPtr->flags.alignment != WALeft)
1436 lw += WMWidthOfString(font, text, len);
1440 if (tPtr->flags.alignment == WARight) {
1441 j = tPtr->visible.w - lw;
1442 } else if (tPtr->flags.alignment == WACenter) {
1443 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1446 for(i=0; i<nitems; i++) {
1447 tb = items[i].tb;
1449 if (tbsame == tb) { /* extend it, since it's on same line */
1450 tb->sections[tb->nsections-1].end = items[i].end;
1451 n = tb->nsections-1;
1452 } else {
1453 tb->sections = wrealloc(tb->sections,
1454 (++tb->nsections)*sizeof(Section));
1455 n = tb->nsections-1;
1456 tb->sections[n]._y = y + max_d;
1457 tb->sections[n].max_d = max_d;
1458 tb->sections[n].x = x+j;
1459 tb->sections[n].h = line_height;
1460 tb->sections[n].begin = items[i].begin;
1461 tb->sections[n].end = items[i].end;
1464 tb->sections[n].last = (i+1 == nitems);
1466 if (tb->graphic) {
1467 if (!tPtr->flags.monoFont) {
1468 if(tb->object) {
1469 WMWidget *wdt = tb->d.widget;
1470 tb->sections[n].y = max_d + y
1471 + line_height - WMWidgetHeight(wdt);
1472 tb->sections[n].w = WMWidgetWidth(wdt);
1473 } else {
1474 tb->sections[n].y = y + line_height
1475 + max_d - tb->d.pixmap->height;
1476 tb->sections[n].w = tb->d.pixmap->width;
1478 x += tb->sections[n].w;
1480 } else {
1481 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1482 len = items[i].end - items[i].begin;
1483 text = &(tb->text[items[i].begin]);
1485 tb->sections[n].y = y+line_height-font->y;
1486 tb->sections[n].w =
1487 WMWidthOfString(font,
1488 &(tb->text[tb->sections[n].begin]),
1489 tb->sections[n].end - tb->sections[n].begin);
1491 x += WMWidthOfString(font, text, len);
1494 tbsame = tb;
1497 return line_height;
1502 static void
1503 layOutDocument(Text *tPtr)
1505 TextBlock *tb;
1506 myLineItems *items = NULL;
1507 unsigned int itemsSize=0, nitems=0, begin, end;
1508 WMFont *font;
1509 unsigned int x, y=0, lw = 0, width=0, bmargin;
1510 char *start=NULL, *mark=NULL;
1512 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1513 return;
1515 assert(tPtr->visible.w > 20);
1517 tPtr->docWidth = tPtr->visible.w;
1518 x = tPtr->margins[tb->marginN].first;
1519 bmargin = tPtr->margins[tb->marginN].body;
1521 /* only partial layOut needed: re-Lay only affected textblocks */
1522 if (tPtr->flags.laidOut) {
1523 tb = tPtr->currentTextBlock;
1525 /* search backwards for textblocks on same line */
1526 while (tb->prior) {
1527 if (!tb->sections || tb->nsections<1) {
1528 tb = tPtr->firstTextBlock;
1529 tPtr->flags.laidOut = False;
1530 y = 0;
1531 goto _layOut;
1534 if(!tb->prior->sections || tb->prior->nsections<1) {
1535 tb = tPtr->firstTextBlock;
1536 tPtr->flags.laidOut = False;
1537 y = 0;
1538 goto _layOut;
1541 if (tb->sections[0]._y !=
1542 tb->prior->sections[tb->prior->nsections-1]._y) {
1543 break;
1545 tb = tb->prior;
1548 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1549 y = tb->prior->sections[tb->prior->nsections-1]._y +
1550 tb->prior->sections[tb->prior->nsections-1].h -
1551 tb->prior->sections[tb->prior->nsections-1].max_d;
1552 } else {
1553 y = 0;
1557 _layOut:
1558 while (tb) {
1560 if (tb->sections && tb->nsections>0) {
1561 wfree(tb->sections);
1562 tb->sections = NULL;
1563 tb->nsections = 0;
1566 if (tb->blank && tb->next && !tb->next->first) {
1567 TextBlock *next = tb->next;
1568 tPtr->currentTextBlock = tb;
1569 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1570 tb = next;
1571 tb->first = True;
1572 continue;
1575 if (tb->first && tb != tPtr->firstTextBlock) {
1576 y += layOutLine(tPtr, items, nitems, x, y);
1577 x = tPtr->margins[tb->marginN].first;
1578 bmargin = tPtr->margins[tb->marginN].body;
1579 nitems = 0;
1580 lw = 0;
1583 if (tb->graphic) {
1584 if (!tPtr->flags.monoFont) {
1585 if(tb->object)
1586 width = WMWidgetWidth(tb->d.widget);
1587 else
1588 width = tb->d.pixmap->width;
1590 if (width > tPtr->docWidth)
1591 tPtr->docWidth = width;
1593 lw += width;
1594 if (lw >= tPtr->visible.w - x ) {
1595 y += layOutLine(tPtr, items, nitems, x, y);
1596 nitems = 0;
1597 x = bmargin;
1598 lw = width;
1601 if(nitems + 1> itemsSize) {
1602 items = wrealloc(items,
1603 (++itemsSize)*sizeof(myLineItems));
1606 items[nitems].tb = tb;
1607 items[nitems].begin = 0;
1608 items[nitems].end = 0;
1609 nitems++;
1612 } else if ((start = tb->text)) {
1613 begin = end = 0;
1614 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1616 while (start) {
1617 mark = strchr(start, ' ');
1618 if (mark) {
1619 end += (int)(mark-start)+1;
1620 start = mark+1;
1621 } else {
1622 end += strlen(start);
1623 start = mark;
1626 if (end > tb->used)
1627 end = tb->used;
1629 if (end-begin > 0) {
1631 width = WMWidthOfString(font,
1632 &tb->text[begin], end-begin);
1634 /* if it won't fit, char wrap it */
1635 if (width >= tPtr->visible.w) {
1636 char *t = &tb->text[begin];
1637 int l=end-begin, i=0;
1638 do {
1639 width = WMWidthOfString(font, t, ++i);
1640 } while (width < tPtr->visible.w && i < l);
1641 if(i>2) i--;
1642 end = begin+i;
1643 start = &tb->text[end];
1646 lw += width;
1649 if (lw >= tPtr->visible.w - x) {
1650 y += layOutLine(tPtr, items, nitems, x, y);
1651 lw = width;
1652 x = bmargin;
1653 nitems = 0;
1656 if(nitems + 1 > itemsSize) {
1657 items = wrealloc(items,
1658 (++itemsSize)*sizeof(myLineItems));
1661 items[nitems].tb = tb;
1662 items[nitems].begin = begin;
1663 items[nitems].end = end;
1664 nitems++;
1666 begin = end;
1671 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1672 if(0&&tPtr->flags.laidOut
1673 && tb->next && tb->next->sections && tb->next->nsections>0
1674 && (tPtr->vpos + tPtr->visible.h
1675 < tb->next->sections[0]._y)) {
1676 if(tPtr->lastTextBlock->sections
1677 && tPtr->lastTextBlock->nsections > 0 ) {
1678 TextBlock *ltb = tPtr->lastTextBlock;
1679 int ly = ltb->sections[ltb->nsections-1]._y;
1680 int lh = ltb->sections[ltb->nsections-1].h;
1681 int ss, sd;
1683 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1684 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1686 y += layOutLine(tPtr, items, nitems, x, y);
1687 ss= ly+lh-y;
1688 sd = tPtr->docHeight-y;
1690 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1691 y += tb->next->sections[0]._y-y;
1692 nitems = 0;
1693 printf("nitems%d\n", nitems);
1694 if(ss-sd!=0)
1695 y = tPtr->docHeight+ss-sd;
1697 break;
1698 } else {
1699 tPtr->flags.laidOut = False;
1703 tb = tb->next;
1707 if (nitems > 0)
1708 y += layOutLine(tPtr, items, nitems, x, y);
1710 if (tPtr->docHeight != y+10) {
1711 tPtr->docHeight = y+10;
1712 updateScrollers(tPtr);
1715 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1716 XEvent event;
1718 tPtr->flags.horizOnDemand = True;
1719 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1720 event.type = Expose;
1721 handleEvents(&event, (void *)tPtr);
1723 } else if(tPtr->docWidth <= tPtr->visible.w
1724 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1725 tPtr->flags.horizOnDemand = False;
1726 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1729 tPtr->flags.laidOut = True;
1731 if(items && itemsSize > 0)
1732 wfree(items);
1734 if (W_VIEW_REALIZED(tPtr->view) && W_VIEW_MAPPED(tPtr->view))
1735 paintText(tPtr);
1739 static void
1740 textDidResize(W_ViewDelegate *self, WMView *view)
1742 Text *tPtr = (Text *)view->self;
1743 unsigned short w = tPtr->view->size.width;
1744 unsigned short h = tPtr->view->size.height;
1745 unsigned short rh = 0, vw = 0, rel;
1747 rel = (tPtr->flags.relief == WRFlat);
1749 if (tPtr->ruler && tPtr->flags.rulerShown) {
1750 WMMoveWidget(tPtr->ruler, 2, 2);
1751 WMResizeWidget(tPtr->ruler, w - 4, 40);
1752 rh = 40;
1755 if (tPtr->vS) {
1756 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1757 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1758 vw = 20;
1759 WMSetRulerOffset(tPtr->ruler,22);
1760 } else WMSetRulerOffset(tPtr->ruler, 2);
1762 if (tPtr->hS) {
1763 if (tPtr->vS) {
1764 WMMoveWidget(tPtr->hS, vw, h - 21);
1765 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1766 } else {
1767 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1768 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1772 tPtr->visible.x = (tPtr->vS)?24:4;
1773 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1774 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1775 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1776 tPtr->visible.h -= (tPtr->hS)?20:0;
1777 tPtr->margins[0].right = tPtr->visible.w;
1779 if (tPtr->view->flags.realized) {
1781 if (tPtr->db) {
1782 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1783 tPtr->db = (Pixmap) NULL;
1786 if (tPtr->visible.w < 40)
1787 tPtr->visible.w = 40;
1788 if (tPtr->visible.h < 20)
1789 tPtr->visible.h = 20;
1791 if(!tPtr->db) {
1792 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1793 tPtr->view->window, tPtr->visible.w,
1794 tPtr->visible.h, tPtr->view->screen->depth);
1798 WMThawText(tPtr);
1801 W_ViewDelegate _TextViewDelegate =
1803 NULL,
1804 NULL,
1805 textDidResize,
1806 NULL,
1809 /* nice, divisble-by-16 blocks */
1810 static inline unsigned short
1811 reqBlockSize(unsigned short requested)
1813 return requested + 16 - (requested%16);
1817 static void
1818 clearText(Text *tPtr)
1820 tPtr->vpos = tPtr->hpos = 0;
1821 tPtr->docHeight = tPtr->docWidth = 0;
1822 tPtr->cursor.x = -23;
1824 if (!tPtr->firstTextBlock)
1825 return;
1827 while (tPtr->currentTextBlock)
1828 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1830 tPtr->firstTextBlock = NULL;
1831 tPtr->currentTextBlock = NULL;
1832 tPtr->lastTextBlock = NULL;
1833 WMEmptyArray(tPtr->gfxItems);
1836 static void
1837 deleteTextInteractively(Text *tPtr, KeySym ksym)
1839 TextBlock *tb;
1840 Bool back = (Bool) (ksym == XK_BackSpace);
1841 Bool done = 1;
1842 Bool wasFirst = 0;
1844 if (!tPtr->flags.editable) {
1845 XBell(tPtr->view->screen->display, 0);
1846 return;
1849 if ( !(tb = tPtr->currentTextBlock) )
1850 return;
1852 if (tPtr->flags.ownsSelection) {
1853 if(removeSelection(tPtr))
1854 layOutDocument(tPtr);
1855 return;
1858 wasFirst = tb->first;
1859 if (back && tPtr->tpos < 1) {
1860 if (tb->prior) {
1861 if(tb->prior->blank) {
1862 tPtr->currentTextBlock = tb->prior;
1863 WMRemoveTextBlock(tPtr);
1864 tPtr->currentTextBlock = tb;
1865 tb->first = True;
1866 layOutDocument(tPtr);
1867 return;
1868 } else {
1869 if(tb->blank) {
1870 TextBlock *prior = tb->prior;
1871 tPtr->currentTextBlock = tb;
1872 WMRemoveTextBlock(tPtr);
1873 tb = prior;
1874 } else {
1875 tb = tb->prior;
1878 tPtr->tpos = tb->used;
1879 tPtr->currentTextBlock = tb;
1880 done = 1;
1881 if(wasFirst) {
1882 if(tb->next)
1883 tb->next->first = False;
1884 layOutDocument(tPtr);
1885 return;
1891 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1892 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1893 if (back)
1894 tPtr->tpos--;
1895 memmove(&(tb->text[tPtr->tpos]),
1896 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1897 tb->used--;
1898 done = 0;
1901 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1902 || tb->graphic) {
1904 TextBlock *sibling = (back? tb->prior : tb->next);
1906 if(tb->used == 0 || tb->graphic)
1907 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1909 if (sibling) {
1910 tPtr->currentTextBlock = sibling;
1911 tPtr->tpos = (back? sibling->used : 0);
1915 layOutDocument(tPtr);
1919 static void
1920 insertTextInteractively(Text *tPtr, char *text, int len)
1922 TextBlock *tb;
1923 char *newline = NULL;
1925 if (!tPtr->flags.editable) {
1926 XBell(tPtr->view->screen->display, 0);
1927 return;
1930 if (len < 1 || !text)
1931 return;
1934 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1935 return;
1938 if (tPtr->flags.ownsSelection)
1939 removeSelection(tPtr);
1942 if (tPtr->flags.ignoreNewLine) {
1943 int i;
1944 for(i=0; i<len; i++) {
1945 if (text[i] == '\n')
1946 text[i] = ' ';
1950 tb = tPtr->currentTextBlock;
1951 if (!tb || tb->graphic) {
1952 tPtr->tpos = 0;
1953 WMAppendTextStream(tPtr, text);
1954 layOutDocument(tPtr);
1955 return;
1958 if ((newline = strchr(text, '\n'))) {
1959 int nlen = (int)(newline-text);
1960 int s = tb->used - tPtr->tpos;
1961 char save[s];
1962 if (!tb->blank && nlen>0) {
1963 if (s > 0) {
1964 memcpy(save, &tb->text[tPtr->tpos], s);
1965 tb->used -= (tb->used - tPtr->tpos);
1967 insertTextInteractively(tPtr, text, nlen);
1968 newline++;
1969 WMAppendTextStream(tPtr, newline);
1970 if (s>0)
1971 insertTextInteractively(tPtr, save, s);
1973 } else {
1974 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1975 && !tb->graphic && tb->text) {
1977 void *ntb = WMCreateTextBlockWithText(
1978 tPtr, &tb->text[tPtr->tpos],
1979 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1980 tb->used = tPtr->tpos;
1981 WMAppendTextBlock(tPtr, ntb);
1982 tPtr->tpos = 0;
1984 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
1985 if(tPtr->flags.indentNewLine) {
1986 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1987 " ", tb->d.font, tb->color, True, 4));
1988 tPtr->tpos = 4;
1989 } else {
1990 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1991 NULL, tb->d.font, tb->color, True, 0));
1992 tPtr->tpos = 0;
1997 } else {
1998 if (tb->used + len >= tb->allocated) {
1999 tb->allocated = reqBlockSize(tb->used+len);
2000 tb->text = wrealloc(tb->text, tb->allocated);
2003 if (tb->blank) {
2004 memcpy(tb->text, text, len);
2005 tb->used = len;
2006 tPtr->tpos = len;
2007 tb->text[tb->used] = 0;
2008 tb->blank = False;
2010 } else {
2011 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2012 tb->used-tPtr->tpos+1);
2013 memmove(&tb->text[tPtr->tpos], text, len);
2014 tb->used += len;
2015 tPtr->tpos += len;
2016 tb->text[tb->used] = 0;
2021 layOutDocument(tPtr);
2025 static void
2026 selectRegion(Text *tPtr, int x, int y)
2029 if (x < 0 || y < 0)
2030 return;
2032 y += (tPtr->flags.rulerShown? 40: 0);
2033 y += tPtr->vpos;
2034 if (y>10)
2035 y -= 10; /* the original offset */
2037 x -= tPtr->visible.x-2;
2038 if (x<0)
2039 x=0;
2041 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2042 tPtr->sel.w = abs(tPtr->clicked.x - x);
2043 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2044 tPtr->sel.h = abs(tPtr->clicked.y - y);
2046 tPtr->flags.ownsSelection = True;
2047 paintText(tPtr);
2051 static void
2052 releaseSelection(Text *tPtr)
2054 TextBlock *tb = tPtr->firstTextBlock;
2056 while(tb) {
2057 tb->selected = False;
2058 tb = tb->next;
2060 tPtr->flags.ownsSelection = False;
2061 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2062 CurrentTime);
2064 paintText(tPtr);
2068 WMData*
2069 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2070 Atom *type)
2072 Text *tPtr = view->self;
2073 Display *dpy = tPtr->view->screen->display;
2074 Atom _TARGETS;
2075 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2076 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2077 WMData *data = NULL;
2080 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2081 char *text = WMGetTextSelectedStream(tPtr);
2083 if (text) {
2084 data = WMCreateDataWithBytes(text, strlen(text));
2085 WMSetDataFormat(data, TYPETEXT);
2087 *type = target;
2088 return data;
2089 } else printf("didn't get it\n");
2091 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2092 if (target == _TARGETS) {
2093 Atom *ptr;
2095 ptr = wmalloc(4 * sizeof(Atom));
2096 ptr[0] = _TARGETS;
2097 ptr[1] = XA_STRING;
2098 ptr[2] = TEXT;
2099 ptr[3] = COMPOUND_TEXT;
2101 data = WMCreateDataWithBytes(ptr, 4*4);
2102 WMSetDataFormat(data, 32);
2104 *type = target;
2105 return data;
2108 return NULL;
2111 static void
2112 lostHandler(WMView *view, Atom selection, void *cdata)
2114 releaseSelection((WMText *)view->self);
2117 static WMSelectionProcs selectionHandler = {
2118 requestHandler, lostHandler, NULL
2122 static void
2123 ownershipObserver(void *observerData, WMNotification *notification)
2125 if (observerData != WMGetNotificationClientData(notification))
2126 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2129 static void
2130 autoSelectText(Text *tPtr, int clicks)
2132 int x, start;
2133 TextBlock *tb;
2134 char *mark = NULL, behind, ahead;
2136 if(!(tb = tPtr->currentTextBlock))
2137 return;
2139 if(clicks == 2) {
2142 switch(tb->text[tPtr->tpos]) {
2143 case ' ': return;
2145 case '<': case '>': behind = '<'; ahead = '>'; break;
2146 case '{': case '}': behind = '{'; ahead = '}'; break;
2147 case '[': case ']': behind = '['; ahead = ']'; break;
2149 default: behind = ahead = ' ';
2152 tPtr->sel.y = tPtr->cursor.y+5;
2153 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2155 if(tb->graphic) {
2156 tPtr->sel.x = tb->sections[0].x;
2157 tPtr->sel.w = tb->sections[0].w;
2158 } else {
2159 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2161 start = tPtr->tpos;
2162 while(start > 0 && tb->text[start-1] != behind)
2163 start--;
2165 x = tPtr->cursor.x;
2166 if(tPtr->tpos > start){
2167 x -= WMWidthOfString(font, &tb->text[start],
2168 tPtr->tpos - start);
2170 tPtr->sel.x = (x<0?0:x)+1;
2172 if((mark = strchr(&tb->text[start], ahead))) {
2173 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2174 (int)(mark - &tb->text[start]));
2175 } else if(tb->used > start) {
2176 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2177 tb->used - start);
2181 } else if(clicks == 3) {
2182 TextBlock *cur = tb;
2184 while(tb && !tb->first) {
2185 tb = tb->prior;
2187 tPtr->sel.y = tb->sections[0]._y;
2189 tb = cur;
2190 while(tb->next && !tb->next->first) {
2191 tb = tb->next;
2193 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2194 + 5 - tPtr->sel.y;
2196 tPtr->sel.x = 0;
2197 tPtr->sel.w = tPtr->docWidth;
2198 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2201 if (!tPtr->flags.ownsSelection) {
2202 WMCreateSelectionHandler(tPtr->view,
2203 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2204 tPtr->flags.ownsSelection = True;
2206 paintText(tPtr);
2211 static void
2212 fontChanged(void *observerData, WMNotification *notification)
2214 WMText *tPtr = (WMText *) observerData;
2215 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2216 printf("fontChanged\n");
2218 if(!tPtr || !font)
2219 return;
2221 if (tPtr->flags.ownsSelection)
2222 WMSetTextSelectionFont(tPtr, font);
2226 static void
2227 handleTextKeyPress(Text *tPtr, XEvent *event)
2229 char buffer[2];
2230 KeySym ksym;
2231 int control_pressed = False;
2232 TextBlock *tb = NULL;
2234 if (((XKeyEvent *) event)->state & ControlMask)
2235 control_pressed = True;
2236 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2238 switch(ksym) {
2240 case XK_Left:
2241 if(!(tb = tPtr->currentTextBlock))
2242 break;
2243 if(tb->graphic)
2244 goto L_imaGFX;
2246 if(tPtr->tpos==0) {
2247 L_imaGFX: if(tb->prior) {
2248 tPtr->currentTextBlock = tb->prior;
2249 tPtr->tpos = tPtr->currentTextBlock->used -1;
2250 } else tPtr->tpos = 0;
2251 } else tPtr->tpos--;
2252 updateCursorPosition(tPtr);
2253 paintText(tPtr);
2254 break;
2256 case XK_Right:
2257 if(!(tb = tPtr->currentTextBlock))
2258 break;
2259 if(tb->graphic)
2260 goto R_imaGFX;
2261 if(tPtr->tpos == tb->used) {
2262 R_imaGFX: if(tb->next) {
2263 tPtr->currentTextBlock = tb->next;
2264 tPtr->tpos = 1;
2265 } else tPtr->tpos = tb->used;
2266 } else tPtr->tpos++;
2267 updateCursorPosition(tPtr);
2268 paintText(tPtr);
2269 break;
2271 case XK_Down:
2272 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2273 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2274 paintText(tPtr);
2275 break;
2277 case XK_Up:
2278 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2279 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2280 paintText(tPtr);
2281 break;
2283 case XK_BackSpace:
2284 case XK_Delete:
2285 #ifdef XK_KP_Delete
2286 case XK_KP_Delete:
2287 #endif
2288 deleteTextInteractively(tPtr, ksym);
2289 updateCursorPosition(tPtr);
2290 paintText(tPtr);
2291 break;
2293 case XK_Control_R :
2294 case XK_Control_L :
2295 control_pressed = True;
2296 break;
2298 case XK_Tab:
2299 insertTextInteractively(tPtr, " ", 4);
2300 updateCursorPosition(tPtr);
2301 paintText(tPtr);
2302 break;
2304 case XK_Return:
2305 buffer[0] = '\n';
2306 default:
2307 if (buffer[0] != 0 && !control_pressed) {
2308 insertTextInteractively(tPtr, buffer, 1);
2309 updateCursorPosition(tPtr);
2310 paintText(tPtr);
2312 } else if (control_pressed && ksym==XK_r) {
2313 Bool i = !tPtr->flags.rulerShown;
2314 WMShowTextRuler(tPtr, i);
2315 tPtr->flags.rulerShown = i;
2317 else if (control_pressed && buffer[0] == '\a')
2318 XBell(tPtr->view->screen->display, 0);
2321 if (!control_pressed && tPtr->flags.ownsSelection)
2322 releaseSelection(tPtr);
2326 static void
2327 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2328 void *cdata, WMData *data)
2330 Text *tPtr = (Text *)view->self;
2331 char *text;
2333 tPtr->flags.waitingForSelection = 0;
2335 if (data) {
2336 text = (char*)WMDataBytes(data);
2338 if (tPtr->parser) {
2339 (tPtr->parser) (tPtr, (void *) text);
2340 layOutDocument(tPtr);
2341 } else insertTextInteractively(tPtr, text, strlen(text));
2342 updateCursorPosition(tPtr);
2343 paintText(tPtr);
2345 } else {
2346 int n;
2348 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2350 if (text) {
2351 text[n] = 0;
2352 if (tPtr->parser) {
2353 (tPtr->parser) (tPtr, (void *) text);
2354 layOutDocument(tPtr);
2355 } else insertTextInteractively(tPtr, text, n);
2356 updateCursorPosition(tPtr);
2357 paintText(tPtr);
2359 XFree(text);
2367 static void
2368 handleActionEvents(XEvent *event, void *data)
2370 Text *tPtr = (Text *)data;
2371 Display *dpy = event->xany.display;
2372 KeySym ksym;
2375 switch (event->type) {
2376 case KeyPress:
2377 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2378 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2379 tPtr->flags.extendSelection = True;
2380 return;
2383 if (tPtr->flags.focused) {
2384 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2385 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2386 GrabModeAsync, GrabModeAsync, None,
2387 tPtr->view->screen->invisibleCursor, CurrentTime);
2388 tPtr->flags.pointerGrabbed = True;
2389 handleTextKeyPress(tPtr, event);
2391 } break;
2393 case KeyRelease:
2394 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2395 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2396 tPtr->flags.extendSelection = False;
2397 return;
2398 /* end modify flag so selection can be extended */
2400 break;
2403 case MotionNotify:
2405 if (tPtr->flags.pointerGrabbed) {
2406 tPtr->flags.pointerGrabbed = False;
2407 XUngrabPointer(dpy, CurrentTime);
2410 if(tPtr->flags.waitingForSelection)
2411 break;
2413 if ((event->xmotion.state & Button1Mask)) {
2414 TextBlock *tb = tPtr->currentTextBlock;
2416 if(tPtr->flags.isOverGraphic && tb && tb->graphic && !tb->object) {
2417 WMSize offs;
2418 WMPixmap *pixmap = tb->d.pixmap;
2419 char *types[2] = {"application/X-image", NULL};
2421 offs.width = 2;
2422 offs.height = 2;
2424 WMDragImageFromView(tPtr->view, pixmap, types,
2425 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2426 offs, event, True);
2429 } else {
2430 if (!tPtr->flags.ownsSelection) {
2431 WMCreateSelectionHandler(tPtr->view,
2432 XA_PRIMARY, event->xbutton.time,
2433 &selectionHandler, NULL);
2434 tPtr->flags.ownsSelection = True;
2437 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2438 break;
2441 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2442 break;
2445 case ButtonPress:
2447 if (tPtr->flags.pointerGrabbed) {
2448 tPtr->flags.pointerGrabbed = False;
2449 XUngrabPointer(dpy, CurrentTime);
2450 break;
2453 if (tPtr->flags.waitingForSelection)
2454 break;
2456 if (tPtr->flags.extendSelection) {
2457 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2458 return;
2461 if (tPtr->flags.ownsSelection)
2462 releaseSelection(tPtr);
2465 if (event->xbutton.button == Button1) {
2467 if(WMIsDoubleClick(event)) {
2468 TextBlock *tb = tPtr->currentTextBlock;
2470 tPtr->lastClickTime = event->xbutton.time;
2471 if(tb && tb->graphic && !tb->object) {
2472 char desc[tb->used+1];
2473 memcpy(desc, tb->text, tb->used);
2474 desc[tb->used] = 0;
2475 if(tPtr->delegate) {
2476 if(tPtr->delegate->didDoubleClickOnPicture)
2477 (*tPtr->delegate->didDoubleClickOnPicture)
2478 (tPtr->delegate, desc);
2480 } else {
2481 autoSelectText(tPtr, 2);
2483 break;
2484 } else if(event->xbutton.time - tPtr->lastClickTime
2485 < WINGsConfiguration.doubleClickDelay) {
2486 tPtr->lastClickTime = event->xbutton.time;
2487 autoSelectText(tPtr, 3);
2488 break;
2491 if (!tPtr->flags.focused) {
2492 WMSetFocusToWidget(tPtr);
2493 tPtr->flags.focused = True;
2496 tPtr->lastClickTime = event->xbutton.time;
2497 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2498 paintText(tPtr);
2501 if (event->xbutton.button
2502 == WINGsConfiguration.mouseWheelDown) {
2503 WMScrollText(tPtr, 16);
2504 break;
2507 if (event->xbutton.button
2508 == WINGsConfiguration.mouseWheelUp) {
2509 WMScrollText(tPtr, -16);
2510 break;
2513 if (event->xbutton.button == Button2) {
2514 char *text = NULL;
2515 int n;
2517 if (!tPtr->flags.editable) {
2518 XBell(dpy, 0);
2519 break;
2522 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2523 event->xbutton.time, pasteText, NULL)) {
2525 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2526 tPtr->flags.waitingForSelection = 0;
2528 if (text) {
2529 text[n] = 0;
2531 if (tPtr->parser) {
2532 (tPtr->parser) (tPtr, (void *) text);
2533 layOutDocument(tPtr);
2535 else
2536 insertTextInteractively(tPtr, text, n);
2538 XFree(text);
2539 #if 0
2540 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2541 (void*)WMInsertTextEvent);
2542 #endif
2543 updateCursorPosition(tPtr);
2544 paintText(tPtr);
2546 } else {
2547 tPtr->flags.waitingForSelection = True;
2550 break;
2554 case ButtonRelease:
2555 if (tPtr->flags.pointerGrabbed) {
2556 tPtr->flags.pointerGrabbed = False;
2557 XUngrabPointer(dpy, CurrentTime);
2558 break;
2561 if (tPtr->flags.waitingForSelection)
2562 break;
2568 static void
2569 handleEvents(XEvent *event, void *data)
2571 Text *tPtr = (Text *)data;
2573 switch(event->type) {
2574 case Expose:
2576 if (event->xexpose.count!=0)
2577 break;
2579 if(tPtr->hS) {
2580 if (!(W_VIEW(tPtr->hS))->flags.realized)
2581 WMRealizeWidget(tPtr->hS);
2584 if(tPtr->vS) {
2585 if (!(W_VIEW(tPtr->vS))->flags.realized)
2586 WMRealizeWidget(tPtr->vS);
2589 if(tPtr->ruler) {
2590 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2591 WMRealizeWidget(tPtr->ruler);
2595 if(!tPtr->db)
2596 textDidResize(tPtr->view->delegate, tPtr->view);
2598 paintText(tPtr);
2599 break;
2601 case FocusIn:
2602 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2603 != tPtr->view)
2604 return;
2605 tPtr->flags.focused = True;
2606 #if DO_BLINK
2607 if (tPtr->flags.editable && !tPtr->timerID) {
2608 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2609 blinkCursor, tPtr);
2611 #endif
2613 break;
2615 case FocusOut:
2616 tPtr->flags.focused = False;
2617 paintText(tPtr);
2618 #if DO_BLINK
2619 if (tPtr->timerID) {
2620 WMDeleteTimerHandler(tPtr->timerID);
2621 tPtr->timerID = NULL;
2623 #endif
2624 break;
2627 case DestroyNotify:
2628 clearText(tPtr);
2629 if(tPtr->db)
2630 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2631 if(tPtr->gfxItems)
2632 WMEmptyArray(tPtr->gfxItems);
2633 #if DO_BLINK
2634 if (tPtr->timerID)
2635 WMDeleteTimerHandler(tPtr->timerID);
2636 #endif
2637 WMReleaseFont(tPtr->dFont);
2638 WMReleaseColor(tPtr->dColor);
2639 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2640 WMRemoveNotificationObserver(tPtr);
2642 wfree(tPtr);
2644 break;
2650 static void
2651 insertPlainText(Text *tPtr, char *text)
2653 char *start, *mark;
2654 void *tb = NULL;
2656 start = text;
2657 while (start) {
2658 mark = strchr(start, '\n');
2659 if (mark) {
2660 tb = WMCreateTextBlockWithText(tPtr,
2661 start, tPtr->dFont,
2662 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2663 start = mark+1;
2664 tPtr->flags.first = True;
2665 } else {
2666 if (start && strlen(start)) {
2667 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2668 tPtr->dColor, tPtr->flags.first, strlen(start));
2669 } else tb = NULL;
2670 tPtr->flags.first = False;
2671 start = mark;
2674 if (tPtr->flags.prepend)
2675 WMPrependTextBlock(tPtr, tb);
2676 else
2677 WMAppendTextBlock(tPtr, tb);
2682 static void
2683 rulerMoveCallBack(WMWidget *w, void *self)
2685 Text *tPtr = (Text *)self;
2687 if (!tPtr)
2688 return;
2689 if (W_CLASS(tPtr) != WC_Text)
2690 return;
2692 paintText(tPtr);
2696 static void
2697 rulerReleaseCallBack(WMWidget *w, void *self)
2699 Text *tPtr = (Text *)self;
2701 if (!tPtr)
2702 return;
2703 if (W_CLASS(tPtr) != WC_Text)
2704 return;
2706 WMThawText(tPtr);
2707 return;
2710 static unsigned
2711 draggingSourceOperation(WMView *self, Bool local)
2713 return WDOperationCopy;
2716 static WMData*
2717 fetchDragData(WMView *self, char *type)
2719 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2720 char *desc;
2721 WMData *data;
2723 if (!tb)
2724 return NULL;
2726 printf("type is [%s]\n", type);
2727 desc = wmalloc(tb->used+1);
2728 memcpy(desc, tb->text, tb->used);
2729 desc[tb->used] = 0;
2730 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2732 wfree(desc);
2734 return data;
2738 static WMDragSourceProcs _DragSourceProcs = {
2739 draggingSourceOperation,
2740 NULL,
2741 NULL,
2742 fetchDragData
2746 static unsigned
2747 draggingEntered(WMView *self, WMDraggingInfo *info)
2749 printf("draggingEntered\n");
2750 return WDOperationCopy;
2754 static unsigned
2755 draggingUpdated(WMView *self, WMDraggingInfo *info)
2757 return WDOperationCopy;
2761 static void
2762 draggingExited(WMView *self, WMDraggingInfo *info)
2764 printf("draggingExited\n");
2767 static Bool
2768 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2770 printf("prepareForDragOperation\n");
2771 return True;
2775 char *badbadbad;
2777 static void
2778 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2779 void *cdata, WMData *data)
2781 badbadbad = wstrdup((char *)WMDataBytes(data));
2785 /* when it's done in WINGs, remove this */
2787 Bool
2788 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2790 WMScreen *scr = W_VIEW_SCREEN(view);
2792 if (!WMRequestSelection(scr->dragInfo.destView,
2793 scr->xdndSelectionAtom,
2794 XInternAtom(scr->display, type, False),
2795 scr->dragInfo.timestamp,
2796 receivedData, &scr->dragInfo)) {
2797 wwarning("could not request data for dropped data");
2801 XEvent ev;
2803 ev.type = ClientMessage;
2804 ev.xclient.message_type = scr->xdndFinishedAtom;
2805 ev.xclient.format = 32;
2806 ev.xclient.window = info->destinationWindow;
2807 ev.xclient.data.l[0] = 0;
2808 ev.xclient.data.l[1] = 0;
2809 ev.xclient.data.l[2] = 0;
2810 ev.xclient.data.l[3] = 0;
2811 ev.xclient.data.l[4] = 0;
2813 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2814 XFlush(scr->display);
2816 return True;
2819 static Bool
2820 performDragOperation(WMView *self, WMDraggingInfo *info)
2822 WMColor *color;
2823 WMText *tPtr = (WMText *)self->self;
2825 if (!tPtr)
2826 return True;
2828 requestDroppedData(tPtr->view, info, "application/X-color");
2829 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2830 if(color) {
2831 WMSetTextSelectionColor(tPtr, color);
2832 WMReleaseColor(color);
2837 return True;
2840 static void
2841 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2843 printf("concludeDragOperation\n");
2847 static WMDragDestinationProcs _DragDestinationProcs = {
2848 draggingEntered,
2849 draggingUpdated,
2850 draggingExited,
2851 prepareForDragOperation,
2852 performDragOperation,
2853 concludeDragOperation
2857 char *
2858 getStream(WMText *tPtr, int sel, int array)
2860 TextBlock *tb = NULL;
2861 char *text = NULL;
2862 unsigned long where = 0;
2864 if (!tPtr)
2865 return NULL;
2867 if (!(tb = tPtr->firstTextBlock))
2868 return NULL;
2870 if (tPtr->writer) {
2871 (tPtr->writer) (tPtr, (void *) text);
2872 return text;
2875 tb = tPtr->firstTextBlock;
2876 while (tb) {
2878 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2880 if (!sel || (tb->graphic && tb->selected)) {
2882 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2883 && tb != tPtr->firstTextBlock) {
2884 text = wrealloc(text, where+1);
2885 text[where++] = '\n';
2888 if(tb->blank)
2889 goto _gSnext;
2891 if(tb->graphic && array) {
2892 text = wrealloc(text, where+4);
2893 text[where++] = 0xFA;
2894 text[where++] = (tb->used>>8)&0x0ff;
2895 text[where++] = tb->used&0x0ff;
2896 text[where++] = tb->allocated; /* extra info */
2898 text = wrealloc(text, where+tb->used);
2899 memcpy(&text[where], tb->text, tb->used);
2900 where += tb->used;
2903 } else if (sel && tb->selected) {
2905 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2906 text = wrealloc(text, where+1);
2907 text[where++] = '\n';
2910 if(tb->blank)
2911 goto _gSnext;
2913 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2914 memcpy(&text[where], &tb->text[tb->s_begin],
2915 tb->s_end - tb->s_begin);
2916 where += tb->s_end - tb->s_begin;
2921 _gSnext:tb = tb->next;
2924 /* +1 for the end of string, let's be nice */
2925 text = wrealloc(text, where+1);
2926 text[where] = 0;
2927 return text;
2931 static void
2932 releaseStreamObjects(void *data)
2934 if(data)
2935 wfree(data);
2938 WMArray *
2939 getStreamObjects(WMText *tPtr, int sel)
2941 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2942 WMData *data;
2943 char *stream;
2944 unsigned short len;
2945 char *start, *fa, *desc;
2947 stream = getStream(tPtr, sel, 1);
2948 if(!stream)
2949 return NULL;
2951 start = stream;
2952 while (start) {
2954 fa = strchr(start, 0xFA);
2955 if (fa) {
2956 if((int)(fa - start)>0) {
2957 desc = start;
2958 desc[(int)(fa - start)] = 0;
2959 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2960 WMSetDataFormat(data, TYPETEXT);
2961 WMAddToArray(array, (void *) data);
2964 len = *(fa+1)*0xff + *(fa+2);
2965 data = WMCreateDataWithBytes((void *)(fa+4), len);
2966 WMSetDataFormat(data, *(fa+3));
2967 WMAddToArray(array, (void *) data);
2968 start = fa + len + 4;
2970 } else {
2971 if (start && strlen(start)) {
2972 data = WMCreateDataWithBytes((void *)start, strlen(start));
2973 WMSetDataFormat(data, TYPETEXT);
2974 WMAddToArray(array, (void *) data);
2976 start = fa;
2980 wfree(stream);
2981 return array;
2985 WMText *
2986 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
2988 Text *tPtr;
2990 tPtr = wmalloc(sizeof(Text));
2991 memset(tPtr, 0, sizeof(Text));
2992 tPtr->widgetClass = WC_Text;
2993 tPtr->view = W_CreateView(W_VIEW(parent));
2994 if (!tPtr->view) {
2995 perror("could not create text's view\n");
2996 wfree(tPtr);
2997 return NULL;
2999 tPtr->view->self = tPtr;
3000 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
3001 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3002 W_ResizeView(tPtr->view, 250, 200);
3004 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
3005 tPtr->bgGC = WMColorGC(tPtr->dColor);
3006 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3007 WMReleaseColor(tPtr->dColor);
3009 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3010 tPtr->fgGC = WMColorGC(tPtr->dColor);
3012 tPtr->ruler = NULL;
3013 tPtr->vS = NULL;
3014 tPtr->hS = NULL;
3016 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3018 tPtr->view->delegate = &_TextViewDelegate;
3020 tPtr->delegate = NULL;
3022 #if DO_BLINK
3023 tPtr->timerID = NULL;
3024 #endif
3026 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3027 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3028 handleEvents, tPtr);
3030 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3031 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3032 handleActionEvents, tPtr);
3034 WMAddNotificationObserver(ownershipObserver, tPtr,
3035 "_lostOwnership", tPtr);
3037 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3038 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3042 char *types[3] = {"application/X-color", "application/X-image", NULL};
3043 WMRegisterViewForDraggedTypes(tPtr->view, types);
3046 WMAddNotificationObserver(fontChanged, tPtr,
3047 "WMFontPanelDidChangeNotification", tPtr);
3049 tPtr->firstTextBlock = NULL;
3050 tPtr->lastTextBlock = NULL;
3051 tPtr->currentTextBlock = NULL;
3052 tPtr->tpos = 0;
3054 tPtr->gfxItems = WMCreateArray(4);
3056 tPtr->parser = parser;
3057 tPtr->writer = writer;
3059 tPtr->sel.x = tPtr->sel.y = 2;
3060 tPtr->sel.w = tPtr->sel.h = 0;
3062 tPtr->clicked.x = tPtr->clicked.y = 2;
3064 tPtr->visible.x = tPtr->visible.y = 2;
3065 tPtr->visible.h = tPtr->view->size.height;
3066 tPtr->visible.w = tPtr->view->size.width - 4;
3068 tPtr->cursor.x = -23;
3070 tPtr->docWidth = 0;
3071 tPtr->docHeight = 0;
3072 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3073 default_bullet);
3074 tPtr->db = (Pixmap) NULL;
3075 tPtr->bgPixmap = NULL;
3077 tPtr->margins = WMGetRulerMargins(NULL);
3078 tPtr->margins->right = tPtr->visible.w;
3079 tPtr->nMargins = 1;
3081 tPtr->flags.rulerShown = False;
3082 tPtr->flags.monoFont = False;
3083 tPtr->flags.focused = False;
3084 tPtr->flags.editable = True;
3085 tPtr->flags.ownsSelection = False;
3086 tPtr->flags.pointerGrabbed = False;
3087 tPtr->flags.extendSelection = False;
3088 tPtr->flags.frozen = False;
3089 tPtr->flags.cursorShown = True;
3090 tPtr->flags.acceptsGraphic = False;
3091 tPtr->flags.horizOnDemand = False;
3092 tPtr->flags.needsLayOut = False;
3093 tPtr->flags.ignoreNewLine = False;
3094 tPtr->flags.indentNewLine = False;
3095 tPtr->flags.laidOut = False;
3096 tPtr->flags.waitingForSelection = False;
3097 tPtr->flags.prepend = False;
3098 tPtr->flags.isOverGraphic = False;
3099 tPtr->flags.relief = WRSunken;
3100 tPtr->flags.isOverGraphic = 0;
3101 tPtr->flags.alignment = WALeft;
3102 tPtr->flags.first = True;
3104 return tPtr;
3107 void
3108 WMPrependTextStream(WMText *tPtr, char *text)
3110 CHECK_CLASS(tPtr, WC_Text);
3112 if(!text) {
3113 if (tPtr->flags.ownsSelection)
3114 releaseSelection(tPtr);
3115 clearText(tPtr);
3116 updateScrollers(tPtr);
3117 return;
3120 tPtr->flags.prepend = True;
3121 if (text && tPtr->parser)
3122 (tPtr->parser) (tPtr, (void *) text);
3123 else
3124 insertPlainText(tPtr, text);
3126 tPtr->flags.needsLayOut = True;
3127 tPtr->tpos = 0;
3128 if(!tPtr->flags.frozen) {
3129 layOutDocument(tPtr);
3134 void
3135 WMAppendTextStream(WMText *tPtr, char *text)
3137 CHECK_CLASS(tPtr, WC_Text);
3139 if(!text) {
3140 if (tPtr->flags.ownsSelection)
3141 releaseSelection(tPtr);
3142 clearText(tPtr);
3143 updateScrollers(tPtr);
3144 return;
3147 tPtr->flags.prepend = False;
3148 if (text && tPtr->parser)
3149 (tPtr->parser) (tPtr, (void *) text);
3150 else
3151 insertPlainText(tPtr, text);
3153 tPtr->flags.needsLayOut = True;
3154 if(tPtr->currentTextBlock)
3155 tPtr->tpos = tPtr->currentTextBlock->used;
3157 if(!tPtr->flags.frozen) {
3158 layOutDocument(tPtr);
3163 char *
3164 WMGetTextStream(WMText *tPtr)
3166 CHECK_CLASS(tPtr, WC_Text);
3167 return getStream(tPtr, 0, 0);
3170 char *
3171 WMGetTextSelectedStream(WMText *tPtr)
3173 CHECK_CLASS(tPtr, WC_Text);
3174 return getStream(tPtr, 1, 0);
3177 WMArray *
3178 WMGetTextObjects(WMText *tPtr)
3180 CHECK_CLASS(tPtr, WC_Text);
3181 return getStreamObjects(tPtr, 0);
3184 WMArray *
3185 WMGetTextSelectedObjects(WMText *tPtr)
3187 CHECK_CLASS(tPtr, WC_Text);
3188 return getStreamObjects(tPtr, 1);
3192 void
3193 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3195 CHECK_CLASS(tPtr, WC_Text);
3197 tPtr->delegate = delegate;
3201 void *
3202 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3203 char *description, WMColor *color,
3204 unsigned short first, unsigned short extraInfo)
3206 TextBlock *tb;
3208 if (!w || !description || !color)
3209 return NULL;
3211 tb = wmalloc(sizeof(TextBlock));
3212 if (!tb)
3213 return NULL;
3215 tb->text = wstrdup(description);
3216 tb->used = strlen(description);
3217 tb->blank = False;
3218 tb->d.widget = w;
3219 tb->color = WMRetainColor(color);
3220 tb->marginN = newMargin(tPtr, NULL);
3221 tb->allocated = extraInfo;
3222 tb->first = first;
3223 tb->kanji = False;
3224 tb->graphic = True;
3225 tb->object = True;
3226 tb->underlined = False;
3227 tb->selected = False;
3228 tb->script = 0;
3229 tb->sections = NULL;
3230 tb->nsections = 0;
3231 tb->prior = NULL;
3232 tb->next = NULL;
3234 return tb;
3238 void *
3239 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3240 char *description, WMColor *color,
3241 unsigned short first, unsigned short extraInfo)
3243 TextBlock *tb;
3245 if (!p || !description || !color)
3246 return NULL;
3248 tb = wmalloc(sizeof(TextBlock));
3249 if (!tb)
3250 return NULL;
3252 tb->text = wstrdup(description);
3253 tb->used = strlen(description);
3254 tb->blank = False;
3255 tb->d.pixmap = WMRetainPixmap(p);
3256 tb->color = WMRetainColor(color);
3257 tb->marginN = newMargin(tPtr, NULL);
3258 tb->allocated = extraInfo;
3259 tb->first = first;
3260 tb->kanji = False;
3261 tb->graphic = True;
3262 tb->object = False;
3263 tb->underlined = False;
3264 tb->selected = False;
3265 tb->script = 0;
3266 tb->sections = NULL;
3267 tb->nsections = 0;
3268 tb->prior = NULL;
3269 tb->next = NULL;
3271 return tb;
3275 void*
3276 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3277 unsigned short first, unsigned short len)
3279 TextBlock *tb;
3281 if (!font || !color)
3282 return NULL;
3284 tb = wmalloc(sizeof(TextBlock));
3285 if (!tb)
3286 return NULL;
3288 tb->allocated = reqBlockSize(len);
3289 tb->text = (char *)wmalloc(tb->allocated);
3290 memset(tb->text, 0, tb->allocated);
3292 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3293 *tb->text = ' ';
3294 tb->used = 1;
3295 tb->blank = True;
3296 } else {
3297 memcpy(tb->text, text, len);
3298 tb->used = len;
3299 tb->blank = False;
3301 tb->text[tb->used] = 0;
3303 tb->d.font = WMRetainFont(font);
3304 tb->color = WMRetainColor(color);
3305 tb->marginN = newMargin(tPtr, NULL);
3306 tb->first = first;
3307 tb->kanji = False;
3308 tb->graphic = False;
3309 tb->underlined = False;
3310 tb->selected = False;
3311 tb->script = 0;
3312 tb->sections = NULL;
3313 tb->nsections = 0;
3314 tb->prior = NULL;
3315 tb->next = NULL;
3316 return tb;
3319 void
3320 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3321 unsigned int kanji, unsigned int underlined, int script,
3322 WMRulerMargins *margins)
3324 TextBlock *tb = (TextBlock *) vtb;
3325 if (!tb)
3326 return;
3328 tb->first = first;
3329 tb->kanji = kanji;
3330 tb->underlined = underlined;
3331 tb->script = script;
3332 tb->marginN = newMargin(tPtr, margins);
3335 void
3336 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3337 unsigned int *kanji, unsigned int *underlined, int *script,
3338 WMRulerMargins *margins)
3340 TextBlock *tb = (TextBlock *) vtb;
3341 if (!tb)
3342 return;
3344 if (first) *first = tb->first;
3345 if (kanji) *kanji = tb->kanji;
3346 if (underlined) *underlined = tb->underlined;
3347 if (script) *script = tb->script;
3348 if (margins) margins = &tPtr->margins[tb->marginN];
3353 void
3354 WMPrependTextBlock(WMText *tPtr, void *vtb)
3356 TextBlock *tb = (TextBlock *)vtb;
3358 if (!tPtr || !tb)
3359 return;
3361 if (tb->graphic) {
3362 if(tb->object) {
3363 WMWidget *w = tb->d.widget;
3364 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3365 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3366 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3369 WMAddToArray(tPtr->gfxItems, (void *)tb);
3370 tPtr->tpos = 0;
3371 } else tPtr->tpos = tb->used;
3373 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3374 tb->next = tb->prior = NULL;
3375 tb->first = True;
3376 tPtr->lastTextBlock = tPtr->firstTextBlock
3377 = tPtr->currentTextBlock = tb;
3378 return;
3381 if(!tb->first) {
3382 tb->marginN = tPtr->currentTextBlock->marginN;
3385 tb->next = tPtr->currentTextBlock;
3386 tb->prior = tPtr->currentTextBlock->prior;
3387 if (tPtr->currentTextBlock->prior)
3388 tPtr->currentTextBlock->prior->next = tb;
3390 tPtr->currentTextBlock->prior = tb;
3391 if (!tb->prior)
3392 tPtr->firstTextBlock = tb;
3394 tPtr->currentTextBlock = tb;
3398 void
3399 WMAppendTextBlock(WMText *tPtr, void *vtb)
3401 TextBlock *tb = (TextBlock *)vtb;
3403 if (!tPtr || !tb)
3404 return;
3406 if (tb->graphic) {
3407 if(tb->object) {
3408 WMWidget *w = tb->d.widget;
3409 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3410 (W_VIEW(w))->attribs.cursor =
3411 tPtr->view->screen->defaultCursor;
3412 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3415 WMAddToArray(tPtr->gfxItems, (void *)tb);
3416 tPtr->tpos = 0;
3417 } else tPtr->tpos = tb->used;
3419 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3420 tb->next = tb->prior = NULL;
3421 tb->first = True;
3422 tPtr->lastTextBlock = tPtr->firstTextBlock
3423 = tPtr->currentTextBlock = tb;
3424 return;
3427 if(!tb->first) {
3428 tb->marginN = tPtr->currentTextBlock->marginN;
3431 tb->next = tPtr->currentTextBlock->next;
3432 tb->prior = tPtr->currentTextBlock;
3433 if (tPtr->currentTextBlock->next)
3434 tPtr->currentTextBlock->next->prior = tb;
3436 tPtr->currentTextBlock->next = tb;
3438 if (!tb->next)
3439 tPtr->lastTextBlock = tb;
3441 tPtr->currentTextBlock = tb;
3444 void *
3445 WMRemoveTextBlock(WMText *tPtr)
3447 TextBlock *tb = NULL;
3449 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3450 || !tPtr->currentTextBlock) {
3451 /* printf("cannot remove non existent TextBlock!\n"); */
3452 return NULL;
3455 tb = tPtr->currentTextBlock;
3456 if (tb->graphic) {
3457 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3459 if(tb->object) {
3460 WMUnmapWidget(tb->d.widget);
3464 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3465 if (tPtr->currentTextBlock->next)
3466 tPtr->currentTextBlock->next->prior = NULL;
3468 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3469 tPtr->currentTextBlock = tPtr->firstTextBlock;
3471 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3472 tPtr->currentTextBlock->prior->next = NULL;
3473 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3474 tPtr->currentTextBlock = tPtr->lastTextBlock;
3475 } else {
3476 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3477 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3478 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3481 return (void *)tb;
3484 void
3485 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3487 TextBlock *tb = (TextBlock *)vtb;
3488 if (!tPtr || !tb)
3489 return;
3491 if (tb->graphic) {
3492 if(tb->object) {
3493 /* naturally, there's a danger to destroying
3494 widgets whose action brings us here:
3495 ie. press a button to destroy it... need to
3496 find a safer way. till then... this stays commented out */
3497 /* WMDestroyWidget(tb->d.widget);
3498 wfree(tb->d.widget); */
3499 tb->d.widget = NULL;
3500 } else {
3501 WMReleasePixmap(tb->d.pixmap);
3502 tb->d.pixmap = NULL;
3504 } else {
3505 WMReleaseFont(tb->d.font);
3508 WMReleaseColor(tb->color);
3509 if (tb->sections && tb->nsections > 0)
3510 wfree(tb->sections);
3511 wfree(tb->text);
3512 wfree(tb);
3513 tb = NULL;
3518 void
3519 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3521 if (!tPtr)
3522 return;
3524 if (color)
3525 tPtr->fgGC = WMColorGC(color);
3526 else
3527 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3529 paintText(tPtr);
3532 void
3533 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3535 if (!tPtr)
3536 return;
3538 if (color) {
3539 tPtr->bgGC = WMColorGC(color);
3540 W_SetViewBackgroundColor(tPtr->view, color);
3541 } else {
3542 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3543 W_SetViewBackgroundColor(tPtr->view,
3544 WMWhiteColor(tPtr->view->screen));
3547 paintText(tPtr);
3550 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3552 if (!tPtr)
3553 return;
3555 if (tPtr->bgPixmap)
3556 WMReleasePixmap(tPtr->bgPixmap);
3558 if (pixmap)
3559 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3560 else
3561 tPtr->bgPixmap = NULL;
3564 void
3565 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3567 if (!tPtr)
3568 return;
3569 tPtr->flags.relief = relief;
3570 textDidResize(tPtr->view->delegate, tPtr->view);
3573 void
3574 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3576 if (!tPtr)
3577 return;
3579 if (shouldhave && !tPtr->hS) {
3580 tPtr->hS = WMCreateScroller(tPtr);
3581 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3582 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3583 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3584 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3585 WMMapWidget(tPtr->hS);
3586 } else if (!shouldhave && tPtr->hS) {
3587 WMUnmapWidget(tPtr->hS);
3588 WMDestroyWidget(tPtr->hS);
3589 tPtr->hS = NULL;
3592 tPtr->hpos = 0;
3593 tPtr->prevHpos = 0;
3594 textDidResize(tPtr->view->delegate, tPtr->view);
3598 void
3599 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3601 if (!tPtr)
3602 return;
3604 if(shouldhave && !tPtr->ruler) {
3605 tPtr->ruler = WMCreateRuler(tPtr);
3606 (W_VIEW(tPtr->ruler))->attribs.cursor =
3607 tPtr->view->screen->defaultCursor;
3608 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3609 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3610 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3611 } else if(!shouldhave && tPtr->ruler) {
3612 WMShowTextRuler(tPtr, False);
3613 WMDestroyWidget(tPtr->ruler);
3614 tPtr->ruler = NULL;
3616 textDidResize(tPtr->view->delegate, tPtr->view);
3619 void
3620 WMShowTextRuler(WMText *tPtr, Bool show)
3622 if(!tPtr)
3623 return;
3624 if(!tPtr->ruler)
3625 return;
3627 if(tPtr->flags.monoFont)
3628 show = False;
3630 tPtr->flags.rulerShown = show;
3631 if(show) {
3632 WMMapWidget(tPtr->ruler);
3633 } else {
3634 WMUnmapWidget(tPtr->ruler);
3637 textDidResize(tPtr->view->delegate, tPtr->view);
3640 Bool
3641 WMGetTextRulerShown(WMText *tPtr)
3643 if(!tPtr)
3644 return 0;
3646 if(!tPtr->ruler)
3647 return 0;
3649 return tPtr->flags.rulerShown;
3653 void
3654 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3656 if (!tPtr)
3657 return;
3659 if (shouldhave && !tPtr->vS) {
3660 tPtr->vS = WMCreateScroller(tPtr);
3661 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3662 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3663 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3664 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3665 WMMapWidget(tPtr->vS);
3666 } else if (!shouldhave && tPtr->vS) {
3667 WMUnmapWidget(tPtr->vS);
3668 WMDestroyWidget(tPtr->vS);
3669 tPtr->vS = NULL;
3672 tPtr->vpos = 0;
3673 tPtr->prevVpos = 0;
3674 textDidResize(tPtr->view->delegate, tPtr->view);
3679 Bool
3680 WMScrollText(WMText *tPtr, int amount)
3682 Bool scroll=False;
3683 if (!tPtr)
3684 return False;
3685 if (amount == 0 || !tPtr->view->flags.realized)
3686 return False;
3688 if (amount < 0) {
3689 if (tPtr->vpos > 0) {
3690 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3691 else tPtr->vpos=0;
3692 scroll=True;
3693 } } else {
3694 int limit = tPtr->docHeight - tPtr->visible.h;
3695 if (tPtr->vpos < limit) {
3696 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3697 else tPtr->vpos = limit;
3698 scroll = True;
3699 } }
3701 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3702 updateScrollers(tPtr);
3703 paintText(tPtr);
3705 tPtr->prevVpos = tPtr->vpos;
3706 return scroll;
3709 Bool
3710 WMPageText(WMText *tPtr, Bool direction)
3712 if (!tPtr) return False;
3713 if (!tPtr->view->flags.realized) return False;
3715 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3718 void
3719 WMSetTextEditable(WMText *tPtr, Bool editable)
3721 if (!tPtr)
3722 return;
3723 tPtr->flags.editable = editable;
3726 int
3727 WMGetTextEditable(WMText *tPtr)
3729 if (!tPtr)
3730 return 0;
3731 return tPtr->flags.editable;
3734 void
3735 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3737 if (!tPtr)
3738 return;
3739 tPtr->flags.indentNewLine = indent;
3742 void
3743 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3745 if (!tPtr)
3746 return;
3747 tPtr->flags.ignoreNewLine = ignore;
3750 Bool
3751 WMGetTextIgnoresNewline(WMText *tPtr)
3753 if (!tPtr)
3754 return True;
3755 return tPtr->flags.ignoreNewLine;
3758 void
3759 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3761 if (!tPtr)
3762 return;
3764 if (mono) {
3765 if(tPtr->flags.rulerShown)
3766 WMShowTextRuler(tPtr, False);
3767 if(tPtr->flags.alignment != WALeft)
3768 tPtr->flags.alignment = WALeft;
3771 tPtr->flags.monoFont = mono;
3772 textDidResize(tPtr->view->delegate, tPtr->view);
3775 Bool
3776 WMGetTextUsesMonoFont(WMText *tPtr)
3778 if (!tPtr)
3779 return True;
3780 return tPtr->flags.monoFont;
3784 void
3785 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3787 if (!tPtr)
3788 return;
3790 WMReleaseFont(tPtr->dFont);
3791 if (font)
3792 tPtr->dFont = WMRetainFont(font);
3793 else
3794 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3797 WMFont *
3798 WMGetTextDefaultFont(WMText *tPtr)
3800 if (!tPtr)
3801 return NULL;
3802 else
3803 return WMRetainFont(tPtr->dFont);
3806 void
3807 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3809 if (!tPtr)
3810 return;
3812 WMReleaseColor(tPtr->dColor);
3813 if (color)
3814 tPtr->dColor = WMRetainColor(color);
3815 else
3816 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3819 WMColor *
3820 WMGetTextDefaultColor(WMText *tPtr)
3822 if (!tPtr)
3823 return NULL;
3824 else
3825 return WMRetainColor(tPtr->dColor);
3828 void
3829 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3831 if (!tPtr)
3832 return;
3833 if(tPtr->flags.monoFont)
3834 tPtr->flags.alignment = WALeft;
3835 else
3836 tPtr->flags.alignment = alignment;
3837 WMThawText(tPtr);
3840 int
3841 WMGetTextInsertType(WMText *tPtr)
3843 if (!tPtr)
3844 return 0;
3845 return tPtr->flags.prepend;
3849 void
3850 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3852 if (!tPtr || !color)
3853 return;
3855 setSelectionProperty(tPtr, NULL, color, -1);
3858 WMColor *
3859 WMGetTextSelectionColor(WMText *tPtr)
3861 TextBlock *tb;
3863 if (!tPtr)
3864 return NULL;
3866 tb = tPtr->currentTextBlock;
3868 if (!tb || !tPtr->flags.ownsSelection)
3869 return NULL;
3871 if(!tb->selected)
3872 return NULL;
3874 return tb->color;
3878 void
3879 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3881 if (!tPtr || !font)
3882 return;
3884 setSelectionProperty(tPtr, font, NULL, -1) ;
3887 WMFont *
3888 WMGetTextSelectionFont(WMText *tPtr)
3890 TextBlock *tb;
3892 if (!tPtr)
3893 return NULL;
3895 tb = tPtr->currentTextBlock;
3897 if (!tb || !tPtr->flags.ownsSelection)
3898 return NULL;
3900 if(!tb->selected)
3901 return NULL;
3903 if(tb->graphic) {
3904 tb = getFirstNonGraphicBlockFor(tb, 1);
3905 if(!tb)
3906 return NULL;
3908 return (tb->selected ? tb->d.font : NULL);
3912 void
3913 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3915 if (!tPtr || (underlined!=0 && underlined !=1))
3916 return;
3918 setSelectionProperty(tPtr, NULL, NULL, underlined);
3922 int
3923 WMGetTextSelectionUnderlined(WMText *tPtr)
3925 TextBlock *tb;
3927 if (!tPtr)
3928 return 0;
3930 tb = tPtr->currentTextBlock;
3932 if (!tb || !tPtr->flags.ownsSelection)
3933 return 0;
3935 if(!tb->selected)
3936 return 0;
3938 return tb->underlined;
3942 void
3943 WMFreezeText(WMText *tPtr)
3945 if (!tPtr)
3946 return;
3948 tPtr->flags.frozen = True;
3952 void
3953 WMThawText(WMText *tPtr)
3955 if (!tPtr)
3956 return;
3958 tPtr->flags.frozen = False;
3960 if(tPtr->flags.monoFont) {
3961 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3962 TextBlock *tb;
3964 /* make sure to unmap widgets no matter where they are */
3965 /* they'll be later remapped if needed by paintText */
3966 for(j=0; j<c; j++) {
3967 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3968 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3969 WMUnmapWidget(tb->d.widget);
3975 tPtr->flags.laidOut = False;
3976 layOutDocument(tPtr);
3977 updateScrollers(tPtr);
3978 paintText(tPtr);
3979 tPtr->flags.needsLayOut = False;
3983 /* find first occurence of a string */
3984 static char *
3985 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
3986 Bool caseSensitive)
3988 char *ptr;
3990 if(!haystack || !needle || !end)
3991 return NULL;
3993 for (ptr = haystack; ptr < end; ptr++) {
3994 if(caseSensitive) {
3995 if (*ptr == *needle && !strncmp(ptr, needle, len))
3996 return ptr;
3998 } else {
3999 if (tolower(*ptr) == tolower(*needle) &&
4000 !strncasecmp(ptr, needle, len))
4001 return ptr;
4005 return NULL;
4008 /* find last occurence of a string */
4009 static char *
4010 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4011 Bool caseSensitive)
4013 char *ptr;
4015 if(!haystack || !needle || !end)
4016 return NULL;
4018 for (ptr = haystack-2; ptr > end; ptr--) {
4019 if(caseSensitive) {
4020 if (*ptr == *needle && !strncmp(ptr, needle, len))
4021 return ptr;
4022 } else {
4023 if (tolower(*ptr) == tolower(*needle) &&
4024 !strncasecmp(ptr, needle, len))
4025 return ptr;
4029 return NULL;
4033 Bool
4034 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4035 Bool caseSensitive)
4037 TextBlock *tb;
4038 char *mark=NULL;
4039 unsigned short pos;
4041 if (!tPtr || !needle)
4042 return False;
4044 #if 0
4045 if (! (tb = tPtr->currentTextBlock)) {
4046 if (! (tb = ( (direction > 0) ?
4047 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4048 return False;
4050 } else {
4051 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4052 tb = (direction>0) ? tb->next : tb->prior; */
4053 if(tb != tPtr->lastTextBlock)
4054 tb = tb->prior;
4056 #endif
4057 tb = tPtr->currentTextBlock;
4058 pos = tPtr->tpos;
4061 while(tb) {
4062 if (!tb->graphic) {
4064 if(direction > 0) {
4065 if(pos+1 < tb->used)
4066 pos++;
4068 if(tb->used - pos> 0 && pos > 0) {
4069 mark = mystrstr(&tb->text[pos], needle,
4070 strlen(needle), &tb->text[tb->used], caseSensitive);
4072 } else {
4073 tb = tb->next;
4074 pos = 0;
4075 continue;
4078 } else {
4079 if(pos-1 > 0)
4080 pos--;
4082 if(pos > 0) {
4083 mark = mystrrstr(&tb->text[pos], needle,
4084 strlen(needle), tb->text, caseSensitive);
4085 } else {
4086 tb = tb->prior;
4087 if(!tb)
4088 return False;
4089 pos = tb->used;
4090 continue;
4095 if(mark) {
4096 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4098 tPtr->tpos = (int)(mark - tb->text);
4099 tPtr->currentTextBlock = tb;
4100 updateCursorPosition(tPtr);
4101 tPtr->sel.y = tPtr->cursor.y+5;
4102 tPtr->sel.h = tPtr->cursor.h-10;
4103 tPtr->sel.x = tPtr->cursor.x +1;
4104 tPtr->sel.w = WMIN(WMWidthOfString(font,
4105 &tb->text[tPtr->tpos], strlen(needle)),
4106 tPtr->docWidth - tPtr->sel.x);
4107 tPtr->flags.ownsSelection = True;
4108 paintText(tPtr);
4110 return True;
4114 tb = (direction>0) ? tb->next : tb->prior;
4115 if(tb) {
4116 pos = (direction>0) ? 0 : tb->used;
4120 return False;
4124 Bool
4125 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4127 if (!tPtr)
4128 return False;
4130 if (!tPtr->flags.ownsSelection)
4131 return False;
4133 removeSelection(tPtr);
4135 if(replacement) {
4136 insertTextInteractively(tPtr, replacement, strlen(replacement));
4137 updateCursorPosition(tPtr);
4138 paintText(tPtr);
4141 return True;