added responder chain alike stuff for relaying kbd events from widget to widget
[wmaker-crm.git] / WINGs / wtext.c
blob535fdfcc641a77e970e77d69cdbeb03965d6fc6c
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 #define reqBlockSize(requested) (requested + 16 - (requested%16))
1813 static void
1814 clearText(Text *tPtr)
1816 tPtr->vpos = tPtr->hpos = 0;
1817 tPtr->docHeight = tPtr->docWidth = 0;
1818 tPtr->cursor.x = -23;
1820 if (!tPtr->firstTextBlock)
1821 return;
1823 while (tPtr->currentTextBlock)
1824 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1826 tPtr->firstTextBlock = NULL;
1827 tPtr->currentTextBlock = NULL;
1828 tPtr->lastTextBlock = NULL;
1829 WMEmptyArray(tPtr->gfxItems);
1832 static void
1833 deleteTextInteractively(Text *tPtr, KeySym ksym)
1835 TextBlock *tb;
1836 Bool back = (Bool) (ksym == XK_BackSpace);
1837 Bool done = 1;
1838 Bool wasFirst = 0;
1840 if (!tPtr->flags.editable) {
1841 XBell(tPtr->view->screen->display, 0);
1842 return;
1845 if ( !(tb = tPtr->currentTextBlock) )
1846 return;
1848 if (tPtr->flags.ownsSelection) {
1849 if(removeSelection(tPtr))
1850 layOutDocument(tPtr);
1851 return;
1854 wasFirst = tb->first;
1855 if (back && tPtr->tpos < 1) {
1856 if (tb->prior) {
1857 if(tb->prior->blank) {
1858 tPtr->currentTextBlock = tb->prior;
1859 WMRemoveTextBlock(tPtr);
1860 tPtr->currentTextBlock = tb;
1861 tb->first = True;
1862 layOutDocument(tPtr);
1863 return;
1864 } else {
1865 if(tb->blank) {
1866 TextBlock *prior = tb->prior;
1867 tPtr->currentTextBlock = tb;
1868 WMRemoveTextBlock(tPtr);
1869 tb = prior;
1870 } else {
1871 tb = tb->prior;
1874 tPtr->tpos = tb->used;
1875 tPtr->currentTextBlock = tb;
1876 done = 1;
1877 if(wasFirst) {
1878 if(tb->next)
1879 tb->next->first = False;
1880 layOutDocument(tPtr);
1881 return;
1887 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1888 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1889 if (back)
1890 tPtr->tpos--;
1891 memmove(&(tb->text[tPtr->tpos]),
1892 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1893 tb->used--;
1894 done = 0;
1897 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1898 || tb->graphic) {
1900 TextBlock *sibling = (back? tb->prior : tb->next);
1902 if(tb->used == 0 || tb->graphic)
1903 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1905 if (sibling) {
1906 tPtr->currentTextBlock = sibling;
1907 tPtr->tpos = (back? sibling->used : 0);
1911 layOutDocument(tPtr);
1915 static void
1916 insertTextInteractively(Text *tPtr, char *text, int len)
1918 TextBlock *tb;
1919 char *newline = NULL;
1921 if (!tPtr->flags.editable) {
1922 XBell(tPtr->view->screen->display, 0);
1923 return;
1926 if (len < 1 || !text)
1927 return;
1930 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1931 return;
1934 if (tPtr->flags.ownsSelection)
1935 removeSelection(tPtr);
1938 if (tPtr->flags.ignoreNewLine) {
1939 int i;
1940 for(i=0; i<len; i++) {
1941 if (text[i] == '\n')
1942 text[i] = ' ';
1946 tb = tPtr->currentTextBlock;
1947 if (!tb || tb->graphic) {
1948 tPtr->tpos = 0;
1949 WMAppendTextStream(tPtr, text);
1950 layOutDocument(tPtr);
1951 return;
1954 if ((newline = strchr(text, '\n'))) {
1955 int nlen = (int)(newline-text);
1956 int s = tb->used - tPtr->tpos;
1957 char save[s];
1958 if (!tb->blank && nlen>0) {
1959 if (s > 0) {
1960 memcpy(save, &tb->text[tPtr->tpos], s);
1961 tb->used -= (tb->used - tPtr->tpos);
1963 insertTextInteractively(tPtr, text, nlen);
1964 newline++;
1965 WMAppendTextStream(tPtr, newline);
1966 if (s>0)
1967 insertTextInteractively(tPtr, save, s);
1969 } else {
1970 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1971 && !tb->graphic && tb->text) {
1973 void *ntb = WMCreateTextBlockWithText(
1974 tPtr, &tb->text[tPtr->tpos],
1975 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1976 tb->used = tPtr->tpos;
1977 WMAppendTextBlock(tPtr, ntb);
1978 tPtr->tpos = 0;
1980 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
1981 if(tPtr->flags.indentNewLine) {
1982 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1983 " ", tb->d.font, tb->color, True, 4));
1984 tPtr->tpos = 4;
1985 } else {
1986 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1987 NULL, tb->d.font, tb->color, True, 0));
1988 tPtr->tpos = 0;
1993 } else {
1994 if (tb->used + len >= tb->allocated) {
1995 tb->allocated = reqBlockSize(tb->used+len);
1996 tb->text = wrealloc(tb->text, tb->allocated);
1999 if (tb->blank) {
2000 memcpy(tb->text, text, len);
2001 tb->used = len;
2002 tPtr->tpos = len;
2003 tb->text[tb->used] = 0;
2004 tb->blank = False;
2006 } else {
2007 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2008 tb->used-tPtr->tpos+1);
2009 memmove(&tb->text[tPtr->tpos], text, len);
2010 tb->used += len;
2011 tPtr->tpos += len;
2012 tb->text[tb->used] = 0;
2017 layOutDocument(tPtr);
2021 static void
2022 selectRegion(Text *tPtr, int x, int y)
2025 if (x < 0 || y < 0)
2026 return;
2028 y += (tPtr->flags.rulerShown? 40: 0);
2029 y += tPtr->vpos;
2030 if (y>10)
2031 y -= 10; /* the original offset */
2033 x -= tPtr->visible.x-2;
2034 if (x<0)
2035 x=0;
2037 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2038 tPtr->sel.w = abs(tPtr->clicked.x - x);
2039 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2040 tPtr->sel.h = abs(tPtr->clicked.y - y);
2042 tPtr->flags.ownsSelection = True;
2043 paintText(tPtr);
2047 static void
2048 releaseSelection(Text *tPtr)
2050 TextBlock *tb = tPtr->firstTextBlock;
2052 while(tb) {
2053 tb->selected = False;
2054 tb = tb->next;
2056 tPtr->flags.ownsSelection = False;
2057 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2058 CurrentTime);
2060 paintText(tPtr);
2064 WMData*
2065 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2066 Atom *type)
2068 Text *tPtr = view->self;
2069 Display *dpy = tPtr->view->screen->display;
2070 Atom _TARGETS;
2071 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2072 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2073 WMData *data = NULL;
2076 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2077 char *text = WMGetTextSelectedStream(tPtr);
2079 if (text) {
2080 data = WMCreateDataWithBytes(text, strlen(text));
2081 WMSetDataFormat(data, TYPETEXT);
2083 *type = target;
2084 return data;
2085 } else printf("didn't get it\n");
2087 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2088 if (target == _TARGETS) {
2089 Atom *ptr;
2091 ptr = wmalloc(4 * sizeof(Atom));
2092 ptr[0] = _TARGETS;
2093 ptr[1] = XA_STRING;
2094 ptr[2] = TEXT;
2095 ptr[3] = COMPOUND_TEXT;
2097 data = WMCreateDataWithBytes(ptr, 4*4);
2098 WMSetDataFormat(data, 32);
2100 *type = target;
2101 return data;
2104 return NULL;
2107 static void
2108 lostHandler(WMView *view, Atom selection, void *cdata)
2110 releaseSelection((WMText *)view->self);
2113 static WMSelectionProcs selectionHandler = {
2114 requestHandler, lostHandler, NULL
2118 static void
2119 ownershipObserver(void *observerData, WMNotification *notification)
2121 if (observerData != WMGetNotificationClientData(notification))
2122 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2125 static void
2126 autoSelectText(Text *tPtr, int clicks)
2128 int x, start;
2129 TextBlock *tb;
2130 char *mark = NULL, behind, ahead;
2132 if(!(tb = tPtr->currentTextBlock))
2133 return;
2135 if(clicks == 2) {
2138 switch(tb->text[tPtr->tpos]) {
2139 case ' ': return;
2141 case '<': case '>': behind = '<'; ahead = '>'; break;
2142 case '{': case '}': behind = '{'; ahead = '}'; break;
2143 case '[': case ']': behind = '['; ahead = ']'; break;
2145 default: behind = ahead = ' ';
2148 tPtr->sel.y = tPtr->cursor.y+5;
2149 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2151 if(tb->graphic) {
2152 tPtr->sel.x = tb->sections[0].x;
2153 tPtr->sel.w = tb->sections[0].w;
2154 } else {
2155 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2157 start = tPtr->tpos;
2158 while(start > 0 && tb->text[start-1] != behind)
2159 start--;
2161 x = tPtr->cursor.x;
2162 if(tPtr->tpos > start){
2163 x -= WMWidthOfString(font, &tb->text[start],
2164 tPtr->tpos - start);
2166 tPtr->sel.x = (x<0?0:x)+1;
2168 if((mark = strchr(&tb->text[start], ahead))) {
2169 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2170 (int)(mark - &tb->text[start]));
2171 } else if(tb->used > start) {
2172 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2173 tb->used - start);
2177 } else if(clicks == 3) {
2178 TextBlock *cur = tb;
2180 while(tb && !tb->first) {
2181 tb = tb->prior;
2183 tPtr->sel.y = tb->sections[0]._y;
2185 tb = cur;
2186 while(tb->next && !tb->next->first) {
2187 tb = tb->next;
2189 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2190 + 5 - tPtr->sel.y;
2192 tPtr->sel.x = 0;
2193 tPtr->sel.w = tPtr->docWidth;
2194 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2197 if (!tPtr->flags.ownsSelection) {
2198 WMCreateSelectionHandler(tPtr->view,
2199 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2200 tPtr->flags.ownsSelection = True;
2202 paintText(tPtr);
2207 static void
2208 fontChanged(void *observerData, WMNotification *notification)
2210 WMText *tPtr = (WMText *) observerData;
2211 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2212 printf("fontChanged\n");
2214 if(!tPtr || !font)
2215 return;
2217 if (tPtr->flags.ownsSelection)
2218 WMSetTextSelectionFont(tPtr, font);
2222 static void
2223 handleTextKeyPress(Text *tPtr, XEvent *event)
2225 char buffer[2];
2226 KeySym ksym;
2227 int control_pressed = False;
2228 TextBlock *tb = NULL;
2230 if (((XKeyEvent *) event)->state & ControlMask)
2231 control_pressed = True;
2232 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2234 switch(ksym) {
2236 case XK_Left:
2237 if(!(tb = tPtr->currentTextBlock))
2238 break;
2239 if(tb->graphic)
2240 goto L_imaGFX;
2242 if(tPtr->tpos==0) {
2243 L_imaGFX: if(tb->prior) {
2244 tPtr->currentTextBlock = tb->prior;
2245 tPtr->tpos = tPtr->currentTextBlock->used -1;
2246 } else tPtr->tpos = 0;
2247 } else tPtr->tpos--;
2248 updateCursorPosition(tPtr);
2249 paintText(tPtr);
2250 break;
2252 case XK_Right:
2253 if(!(tb = tPtr->currentTextBlock))
2254 break;
2255 if(tb->graphic)
2256 goto R_imaGFX;
2257 if(tPtr->tpos == tb->used) {
2258 R_imaGFX: if(tb->next) {
2259 tPtr->currentTextBlock = tb->next;
2260 tPtr->tpos = 1;
2261 } else tPtr->tpos = tb->used;
2262 } else tPtr->tpos++;
2263 updateCursorPosition(tPtr);
2264 paintText(tPtr);
2265 break;
2267 case XK_Down:
2268 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2269 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2270 paintText(tPtr);
2271 break;
2273 case XK_Up:
2274 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2275 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2276 paintText(tPtr);
2277 break;
2279 case XK_BackSpace:
2280 case XK_Delete:
2281 #ifdef XK_KP_Delete
2282 case XK_KP_Delete:
2283 #endif
2284 deleteTextInteractively(tPtr, ksym);
2285 updateCursorPosition(tPtr);
2286 paintText(tPtr);
2287 break;
2289 case XK_Control_R :
2290 case XK_Control_L :
2291 control_pressed = True;
2292 break;
2294 case XK_Tab:
2295 insertTextInteractively(tPtr, " ", 4);
2296 updateCursorPosition(tPtr);
2297 paintText(tPtr);
2298 break;
2300 case XK_Return:
2301 buffer[0] = '\n';
2302 default:
2303 if (buffer[0] != 0 && !control_pressed) {
2304 insertTextInteractively(tPtr, buffer, 1);
2305 updateCursorPosition(tPtr);
2306 paintText(tPtr);
2308 } else if (control_pressed && ksym==XK_r) {
2309 Bool i = !tPtr->flags.rulerShown;
2310 WMShowTextRuler(tPtr, i);
2311 tPtr->flags.rulerShown = i;
2313 else if (control_pressed && buffer[0] == '\a')
2314 XBell(tPtr->view->screen->display, 0);
2315 else
2316 WMRelayToNextResponder(tPtr->view, event);
2319 if (!control_pressed && tPtr->flags.ownsSelection)
2320 releaseSelection(tPtr);
2324 static void
2325 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2326 void *cdata, WMData *data)
2328 Text *tPtr = (Text *)view->self;
2329 char *text;
2331 tPtr->flags.waitingForSelection = 0;
2333 if (data) {
2334 text = (char*)WMDataBytes(data);
2336 if (tPtr->parser) {
2337 (tPtr->parser) (tPtr, (void *) text);
2338 layOutDocument(tPtr);
2339 } else insertTextInteractively(tPtr, text, strlen(text));
2340 updateCursorPosition(tPtr);
2341 paintText(tPtr);
2343 } else {
2344 int n;
2346 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2348 if (text) {
2349 text[n] = 0;
2350 if (tPtr->parser) {
2351 (tPtr->parser) (tPtr, (void *) text);
2352 layOutDocument(tPtr);
2353 } else insertTextInteractively(tPtr, text, n);
2354 updateCursorPosition(tPtr);
2355 paintText(tPtr);
2357 XFree(text);
2365 static void
2366 handleActionEvents(XEvent *event, void *data)
2368 Text *tPtr = (Text *)data;
2369 Display *dpy = event->xany.display;
2370 KeySym ksym;
2373 switch (event->type) {
2374 case KeyPress:
2375 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2376 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2377 tPtr->flags.extendSelection = True;
2378 return;
2381 if (tPtr->flags.focused) {
2382 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2383 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2384 GrabModeAsync, GrabModeAsync, None,
2385 tPtr->view->screen->invisibleCursor, CurrentTime);
2386 tPtr->flags.pointerGrabbed = True;
2387 handleTextKeyPress(tPtr, event);
2389 } break;
2391 case KeyRelease:
2392 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2393 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2394 tPtr->flags.extendSelection = False;
2395 return;
2396 /* end modify flag so selection can be extended */
2398 break;
2401 case MotionNotify:
2403 if (tPtr->flags.pointerGrabbed) {
2404 tPtr->flags.pointerGrabbed = False;
2405 XUngrabPointer(dpy, CurrentTime);
2408 if(tPtr->flags.waitingForSelection)
2409 break;
2411 if ((event->xmotion.state & Button1Mask)) {
2412 TextBlock *tb = tPtr->currentTextBlock;
2414 if(tPtr->flags.isOverGraphic && tb && tb->graphic && !tb->object) {
2415 WMSize offs;
2416 WMPixmap *pixmap = tb->d.pixmap;
2417 char *types[2] = {"application/X-image", NULL};
2419 offs.width = 2;
2420 offs.height = 2;
2422 WMDragImageFromView(tPtr->view, pixmap, types,
2423 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2424 offs, event, True);
2427 } else {
2428 if (!tPtr->flags.ownsSelection) {
2429 WMCreateSelectionHandler(tPtr->view,
2430 XA_PRIMARY, event->xbutton.time,
2431 &selectionHandler, NULL);
2432 tPtr->flags.ownsSelection = True;
2435 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2436 break;
2439 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2440 break;
2443 case ButtonPress:
2445 if (tPtr->flags.pointerGrabbed) {
2446 tPtr->flags.pointerGrabbed = False;
2447 XUngrabPointer(dpy, CurrentTime);
2448 break;
2451 if (tPtr->flags.waitingForSelection)
2452 break;
2454 if (tPtr->flags.extendSelection) {
2455 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2456 return;
2459 if (tPtr->flags.ownsSelection)
2460 releaseSelection(tPtr);
2463 if (event->xbutton.button == Button1) {
2465 if(WMIsDoubleClick(event)) {
2466 TextBlock *tb = tPtr->currentTextBlock;
2468 tPtr->lastClickTime = event->xbutton.time;
2469 if(tb && tb->graphic && !tb->object) {
2470 char desc[tb->used+1];
2471 memcpy(desc, tb->text, tb->used);
2472 desc[tb->used] = 0;
2473 if(tPtr->delegate) {
2474 if(tPtr->delegate->didDoubleClickOnPicture)
2475 (*tPtr->delegate->didDoubleClickOnPicture)
2476 (tPtr->delegate, desc);
2478 } else {
2479 autoSelectText(tPtr, 2);
2481 break;
2482 } else if(event->xbutton.time - tPtr->lastClickTime
2483 < WINGsConfiguration.doubleClickDelay) {
2484 tPtr->lastClickTime = event->xbutton.time;
2485 autoSelectText(tPtr, 3);
2486 break;
2489 if (!tPtr->flags.focused) {
2490 WMSetFocusToWidget(tPtr);
2491 tPtr->flags.focused = True;
2494 tPtr->lastClickTime = event->xbutton.time;
2495 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2496 paintText(tPtr);
2499 if (event->xbutton.button
2500 == WINGsConfiguration.mouseWheelDown) {
2501 WMScrollText(tPtr, 16);
2502 break;
2505 if (event->xbutton.button
2506 == WINGsConfiguration.mouseWheelUp) {
2507 WMScrollText(tPtr, -16);
2508 break;
2511 if (event->xbutton.button == Button2) {
2512 char *text = NULL;
2513 int n;
2515 if (!tPtr->flags.editable) {
2516 XBell(dpy, 0);
2517 break;
2520 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2521 event->xbutton.time, pasteText, NULL)) {
2523 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2524 tPtr->flags.waitingForSelection = 0;
2526 if (text) {
2527 text[n] = 0;
2529 if (tPtr->parser) {
2530 (tPtr->parser) (tPtr, (void *) text);
2531 layOutDocument(tPtr);
2533 else
2534 insertTextInteractively(tPtr, text, n);
2536 XFree(text);
2537 #if 0
2538 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2539 (void*)WMInsertTextEvent);
2540 #endif
2541 updateCursorPosition(tPtr);
2542 paintText(tPtr);
2544 } else {
2545 tPtr->flags.waitingForSelection = True;
2548 break;
2552 case ButtonRelease:
2553 if (tPtr->flags.pointerGrabbed) {
2554 tPtr->flags.pointerGrabbed = False;
2555 XUngrabPointer(dpy, CurrentTime);
2556 break;
2559 if (tPtr->flags.waitingForSelection)
2560 break;
2566 static void
2567 handleEvents(XEvent *event, void *data)
2569 Text *tPtr = (Text *)data;
2571 switch(event->type) {
2572 case Expose:
2574 if (event->xexpose.count!=0)
2575 break;
2577 if(tPtr->hS) {
2578 if (!(W_VIEW(tPtr->hS))->flags.realized)
2579 WMRealizeWidget(tPtr->hS);
2582 if(tPtr->vS) {
2583 if (!(W_VIEW(tPtr->vS))->flags.realized)
2584 WMRealizeWidget(tPtr->vS);
2587 if(tPtr->ruler) {
2588 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2589 WMRealizeWidget(tPtr->ruler);
2593 if(!tPtr->db)
2594 textDidResize(tPtr->view->delegate, tPtr->view);
2596 paintText(tPtr);
2597 break;
2599 case FocusIn:
2600 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2601 != tPtr->view)
2602 return;
2603 tPtr->flags.focused = True;
2604 #if DO_BLINK
2605 if (tPtr->flags.editable && !tPtr->timerID) {
2606 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2607 blinkCursor, tPtr);
2609 #endif
2611 break;
2613 case FocusOut:
2614 tPtr->flags.focused = False;
2615 paintText(tPtr);
2616 #if DO_BLINK
2617 if (tPtr->timerID) {
2618 WMDeleteTimerHandler(tPtr->timerID);
2619 tPtr->timerID = NULL;
2621 #endif
2622 break;
2625 case DestroyNotify:
2626 clearText(tPtr);
2627 if(tPtr->db)
2628 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2629 if(tPtr->gfxItems)
2630 WMEmptyArray(tPtr->gfxItems);
2631 #if DO_BLINK
2632 if (tPtr->timerID)
2633 WMDeleteTimerHandler(tPtr->timerID);
2634 #endif
2635 WMReleaseFont(tPtr->dFont);
2636 WMReleaseColor(tPtr->dColor);
2637 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2638 WMRemoveNotificationObserver(tPtr);
2640 wfree(tPtr);
2642 break;
2648 static void
2649 insertPlainText(Text *tPtr, char *text)
2651 char *start, *mark;
2652 void *tb = NULL;
2654 start = text;
2655 while (start) {
2656 mark = strchr(start, '\n');
2657 if (mark) {
2658 tb = WMCreateTextBlockWithText(tPtr,
2659 start, tPtr->dFont,
2660 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2661 start = mark+1;
2662 tPtr->flags.first = True;
2663 } else {
2664 if (start && strlen(start)) {
2665 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2666 tPtr->dColor, tPtr->flags.first, strlen(start));
2667 } else tb = NULL;
2668 tPtr->flags.first = False;
2669 start = mark;
2672 if (tPtr->flags.prepend)
2673 WMPrependTextBlock(tPtr, tb);
2674 else
2675 WMAppendTextBlock(tPtr, tb);
2680 static void
2681 rulerMoveCallBack(WMWidget *w, void *self)
2683 Text *tPtr = (Text *)self;
2685 if (!tPtr)
2686 return;
2687 if (W_CLASS(tPtr) != WC_Text)
2688 return;
2690 paintText(tPtr);
2694 static void
2695 rulerReleaseCallBack(WMWidget *w, void *self)
2697 Text *tPtr = (Text *)self;
2699 if (!tPtr)
2700 return;
2701 if (W_CLASS(tPtr) != WC_Text)
2702 return;
2704 WMThawText(tPtr);
2705 return;
2708 static unsigned
2709 draggingSourceOperation(WMView *self, Bool local)
2711 return WDOperationCopy;
2714 static WMData*
2715 fetchDragData(WMView *self, char *type)
2717 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2718 char *desc;
2719 WMData *data;
2721 if (!tb)
2722 return NULL;
2724 printf("type is [%s]\n", type);
2725 desc = wmalloc(tb->used+1);
2726 memcpy(desc, tb->text, tb->used);
2727 desc[tb->used] = 0;
2728 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2730 wfree(desc);
2732 return data;
2736 static WMDragSourceProcs _DragSourceProcs = {
2737 draggingSourceOperation,
2738 NULL,
2739 NULL,
2740 fetchDragData
2744 static unsigned
2745 draggingEntered(WMView *self, WMDraggingInfo *info)
2747 printf("draggingEntered\n");
2748 return WDOperationCopy;
2752 static unsigned
2753 draggingUpdated(WMView *self, WMDraggingInfo *info)
2755 return WDOperationCopy;
2759 static void
2760 draggingExited(WMView *self, WMDraggingInfo *info)
2762 printf("draggingExited\n");
2765 static Bool
2766 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2768 printf("prepareForDragOperation\n");
2769 return True;
2773 char *badbadbad;
2775 static void
2776 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2777 void *cdata, WMData *data)
2779 badbadbad = wstrdup((char *)WMDataBytes(data));
2783 /* when it's done in WINGs, remove this */
2785 Bool
2786 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2788 WMScreen *scr = W_VIEW_SCREEN(view);
2790 if (!WMRequestSelection(scr->dragInfo.destView,
2791 scr->xdndSelectionAtom,
2792 XInternAtom(scr->display, type, False),
2793 scr->dragInfo.timestamp,
2794 receivedData, &scr->dragInfo)) {
2795 wwarning("could not request data for dropped data");
2799 XEvent ev;
2801 ev.type = ClientMessage;
2802 ev.xclient.message_type = scr->xdndFinishedAtom;
2803 ev.xclient.format = 32;
2804 ev.xclient.window = info->destinationWindow;
2805 ev.xclient.data.l[0] = 0;
2806 ev.xclient.data.l[1] = 0;
2807 ev.xclient.data.l[2] = 0;
2808 ev.xclient.data.l[3] = 0;
2809 ev.xclient.data.l[4] = 0;
2811 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2812 XFlush(scr->display);
2814 return True;
2817 static Bool
2818 performDragOperation(WMView *self, WMDraggingInfo *info)
2820 WMColor *color;
2821 WMText *tPtr = (WMText *)self->self;
2823 if (!tPtr)
2824 return True;
2826 requestDroppedData(tPtr->view, info, "application/X-color");
2827 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2828 if(color) {
2829 WMSetTextSelectionColor(tPtr, color);
2830 WMReleaseColor(color);
2835 return True;
2838 static void
2839 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2841 printf("concludeDragOperation\n");
2845 static WMDragDestinationProcs _DragDestinationProcs = {
2846 draggingEntered,
2847 draggingUpdated,
2848 draggingExited,
2849 prepareForDragOperation,
2850 performDragOperation,
2851 concludeDragOperation
2855 char *
2856 getStream(WMText *tPtr, int sel, int array)
2858 TextBlock *tb = NULL;
2859 char *text = NULL;
2860 unsigned long where = 0;
2862 if (!tPtr)
2863 return NULL;
2865 if (!(tb = tPtr->firstTextBlock))
2866 return NULL;
2868 if (tPtr->writer) {
2869 (tPtr->writer) (tPtr, (void *) text);
2870 return text;
2873 tb = tPtr->firstTextBlock;
2874 while (tb) {
2876 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2878 if (!sel || (tb->graphic && tb->selected)) {
2880 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2881 && tb != tPtr->firstTextBlock) {
2882 text = wrealloc(text, where+1);
2883 text[where++] = '\n';
2886 if(tb->blank)
2887 goto _gSnext;
2889 if(tb->graphic && array) {
2890 text = wrealloc(text, where+4);
2891 text[where++] = 0xFA;
2892 text[where++] = (tb->used>>8)&0x0ff;
2893 text[where++] = tb->used&0x0ff;
2894 text[where++] = tb->allocated; /* extra info */
2896 text = wrealloc(text, where+tb->used);
2897 memcpy(&text[where], tb->text, tb->used);
2898 where += tb->used;
2901 } else if (sel && tb->selected) {
2903 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2904 text = wrealloc(text, where+1);
2905 text[where++] = '\n';
2908 if(tb->blank)
2909 goto _gSnext;
2911 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2912 memcpy(&text[where], &tb->text[tb->s_begin],
2913 tb->s_end - tb->s_begin);
2914 where += tb->s_end - tb->s_begin;
2919 _gSnext:tb = tb->next;
2922 /* +1 for the end of string, let's be nice */
2923 text = wrealloc(text, where+1);
2924 text[where] = 0;
2925 return text;
2929 static void
2930 releaseStreamObjects(void *data)
2932 if(data)
2933 wfree(data);
2936 WMArray *
2937 getStreamObjects(WMText *tPtr, int sel)
2939 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2940 WMData *data;
2941 char *stream;
2942 unsigned short len;
2943 char *start, *fa, *desc;
2945 stream = getStream(tPtr, sel, 1);
2946 if(!stream)
2947 return NULL;
2949 start = stream;
2950 while (start) {
2952 fa = strchr(start, 0xFA);
2953 if (fa) {
2954 if((int)(fa - start)>0) {
2955 desc = start;
2956 desc[(int)(fa - start)] = 0;
2957 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2958 WMSetDataFormat(data, TYPETEXT);
2959 WMAddToArray(array, (void *) data);
2962 len = *(fa+1)*0xff + *(fa+2);
2963 data = WMCreateDataWithBytes((void *)(fa+4), len);
2964 WMSetDataFormat(data, *(fa+3));
2965 WMAddToArray(array, (void *) data);
2966 start = fa + len + 4;
2968 } else {
2969 if (start && strlen(start)) {
2970 data = WMCreateDataWithBytes((void *)start, strlen(start));
2971 WMSetDataFormat(data, TYPETEXT);
2972 WMAddToArray(array, (void *) data);
2974 start = fa;
2978 wfree(stream);
2979 return array;
2983 WMText *
2984 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
2986 Text *tPtr;
2988 tPtr = wmalloc(sizeof(Text));
2989 memset(tPtr, 0, sizeof(Text));
2990 tPtr->widgetClass = WC_Text;
2991 tPtr->view = W_CreateView(W_VIEW(parent));
2992 if (!tPtr->view) {
2993 perror("could not create text's view\n");
2994 wfree(tPtr);
2995 return NULL;
2997 tPtr->view->self = tPtr;
2998 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2999 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3000 W_ResizeView(tPtr->view, 250, 200);
3002 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
3003 tPtr->bgGC = WMColorGC(tPtr->dColor);
3004 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3005 WMReleaseColor(tPtr->dColor);
3007 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3008 tPtr->fgGC = WMColorGC(tPtr->dColor);
3010 tPtr->ruler = NULL;
3011 tPtr->vS = NULL;
3012 tPtr->hS = NULL;
3014 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3016 tPtr->view->delegate = &_TextViewDelegate;
3018 tPtr->delegate = NULL;
3020 #if DO_BLINK
3021 tPtr->timerID = NULL;
3022 #endif
3024 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3025 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3026 handleEvents, tPtr);
3028 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3029 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3030 handleActionEvents, tPtr);
3032 WMAddNotificationObserver(ownershipObserver, tPtr,
3033 "_lostOwnership", tPtr);
3035 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3036 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3040 char *types[3] = {"application/X-color", "application/X-image", NULL};
3041 WMRegisterViewForDraggedTypes(tPtr->view, types);
3044 WMAddNotificationObserver(fontChanged, tPtr,
3045 "WMFontPanelDidChangeNotification", tPtr);
3047 tPtr->firstTextBlock = NULL;
3048 tPtr->lastTextBlock = NULL;
3049 tPtr->currentTextBlock = NULL;
3050 tPtr->tpos = 0;
3052 tPtr->gfxItems = WMCreateArray(4);
3054 tPtr->parser = parser;
3055 tPtr->writer = writer;
3057 tPtr->sel.x = tPtr->sel.y = 2;
3058 tPtr->sel.w = tPtr->sel.h = 0;
3060 tPtr->clicked.x = tPtr->clicked.y = 2;
3062 tPtr->visible.x = tPtr->visible.y = 2;
3063 tPtr->visible.h = tPtr->view->size.height;
3064 tPtr->visible.w = tPtr->view->size.width - 4;
3066 tPtr->cursor.x = -23;
3068 tPtr->docWidth = 0;
3069 tPtr->docHeight = 0;
3070 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3071 default_bullet);
3072 tPtr->db = (Pixmap) NULL;
3073 tPtr->bgPixmap = NULL;
3075 tPtr->margins = WMGetRulerMargins(NULL);
3076 tPtr->margins->right = tPtr->visible.w;
3077 tPtr->nMargins = 1;
3079 tPtr->flags.rulerShown = False;
3080 tPtr->flags.monoFont = False;
3081 tPtr->flags.focused = False;
3082 tPtr->flags.editable = True;
3083 tPtr->flags.ownsSelection = False;
3084 tPtr->flags.pointerGrabbed = False;
3085 tPtr->flags.extendSelection = False;
3086 tPtr->flags.frozen = False;
3087 tPtr->flags.cursorShown = True;
3088 tPtr->flags.acceptsGraphic = False;
3089 tPtr->flags.horizOnDemand = False;
3090 tPtr->flags.needsLayOut = False;
3091 tPtr->flags.ignoreNewLine = False;
3092 tPtr->flags.indentNewLine = False;
3093 tPtr->flags.laidOut = False;
3094 tPtr->flags.waitingForSelection = False;
3095 tPtr->flags.prepend = False;
3096 tPtr->flags.isOverGraphic = False;
3097 tPtr->flags.relief = WRSunken;
3098 tPtr->flags.isOverGraphic = 0;
3099 tPtr->flags.alignment = WALeft;
3100 tPtr->flags.first = True;
3102 return tPtr;
3105 void
3106 WMPrependTextStream(WMText *tPtr, char *text)
3108 CHECK_CLASS(tPtr, WC_Text);
3110 if(!text) {
3111 if (tPtr->flags.ownsSelection)
3112 releaseSelection(tPtr);
3113 clearText(tPtr);
3114 updateScrollers(tPtr);
3115 return;
3118 tPtr->flags.prepend = True;
3119 if (text && tPtr->parser)
3120 (tPtr->parser) (tPtr, (void *) text);
3121 else
3122 insertPlainText(tPtr, text);
3124 tPtr->flags.needsLayOut = True;
3125 tPtr->tpos = 0;
3126 if(!tPtr->flags.frozen) {
3127 layOutDocument(tPtr);
3132 void
3133 WMAppendTextStream(WMText *tPtr, char *text)
3135 CHECK_CLASS(tPtr, WC_Text);
3137 if(!text) {
3138 if (tPtr->flags.ownsSelection)
3139 releaseSelection(tPtr);
3140 clearText(tPtr);
3141 updateScrollers(tPtr);
3142 return;
3145 tPtr->flags.prepend = False;
3146 if (text && tPtr->parser)
3147 (tPtr->parser) (tPtr, (void *) text);
3148 else
3149 insertPlainText(tPtr, text);
3151 tPtr->flags.needsLayOut = True;
3152 if(tPtr->currentTextBlock)
3153 tPtr->tpos = tPtr->currentTextBlock->used;
3155 if(!tPtr->flags.frozen) {
3156 layOutDocument(tPtr);
3161 char *
3162 WMGetTextStream(WMText *tPtr)
3164 CHECK_CLASS(tPtr, WC_Text);
3165 return getStream(tPtr, 0, 0);
3168 char *
3169 WMGetTextSelectedStream(WMText *tPtr)
3171 CHECK_CLASS(tPtr, WC_Text);
3172 return getStream(tPtr, 1, 0);
3175 WMArray *
3176 WMGetTextObjects(WMText *tPtr)
3178 CHECK_CLASS(tPtr, WC_Text);
3179 return getStreamObjects(tPtr, 0);
3182 WMArray *
3183 WMGetTextSelectedObjects(WMText *tPtr)
3185 CHECK_CLASS(tPtr, WC_Text);
3186 return getStreamObjects(tPtr, 1);
3190 void
3191 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3193 CHECK_CLASS(tPtr, WC_Text);
3195 tPtr->delegate = delegate;
3199 void *
3200 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3201 char *description, WMColor *color,
3202 unsigned short first, unsigned short extraInfo)
3204 TextBlock *tb;
3206 if (!w || !description || !color)
3207 return NULL;
3209 tb = wmalloc(sizeof(TextBlock));
3210 if (!tb)
3211 return NULL;
3213 tb->text = wstrdup(description);
3214 tb->used = strlen(description);
3215 tb->blank = False;
3216 tb->d.widget = w;
3217 tb->color = WMRetainColor(color);
3218 tb->marginN = newMargin(tPtr, NULL);
3219 tb->allocated = extraInfo;
3220 tb->first = first;
3221 tb->kanji = False;
3222 tb->graphic = True;
3223 tb->object = True;
3224 tb->underlined = False;
3225 tb->selected = False;
3226 tb->script = 0;
3227 tb->sections = NULL;
3228 tb->nsections = 0;
3229 tb->prior = NULL;
3230 tb->next = NULL;
3232 return tb;
3236 void *
3237 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3238 char *description, WMColor *color,
3239 unsigned short first, unsigned short extraInfo)
3241 TextBlock *tb;
3243 if (!p || !description || !color)
3244 return NULL;
3246 tb = wmalloc(sizeof(TextBlock));
3247 if (!tb)
3248 return NULL;
3250 tb->text = wstrdup(description);
3251 tb->used = strlen(description);
3252 tb->blank = False;
3253 tb->d.pixmap = WMRetainPixmap(p);
3254 tb->color = WMRetainColor(color);
3255 tb->marginN = newMargin(tPtr, NULL);
3256 tb->allocated = extraInfo;
3257 tb->first = first;
3258 tb->kanji = False;
3259 tb->graphic = True;
3260 tb->object = False;
3261 tb->underlined = False;
3262 tb->selected = False;
3263 tb->script = 0;
3264 tb->sections = NULL;
3265 tb->nsections = 0;
3266 tb->prior = NULL;
3267 tb->next = NULL;
3269 return tb;
3273 void*
3274 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3275 unsigned short first, unsigned short len)
3277 TextBlock *tb;
3279 if (!font || !color)
3280 return NULL;
3282 tb = wmalloc(sizeof(TextBlock));
3283 if (!tb)
3284 return NULL;
3286 tb->allocated = reqBlockSize(len);
3287 tb->text = (char *)wmalloc(tb->allocated);
3288 memset(tb->text, 0, tb->allocated);
3290 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3291 *tb->text = ' ';
3292 tb->used = 1;
3293 tb->blank = True;
3294 } else {
3295 memcpy(tb->text, text, len);
3296 tb->used = len;
3297 tb->blank = False;
3299 tb->text[tb->used] = 0;
3301 tb->d.font = WMRetainFont(font);
3302 tb->color = WMRetainColor(color);
3303 tb->marginN = newMargin(tPtr, NULL);
3304 tb->first = first;
3305 tb->kanji = False;
3306 tb->graphic = False;
3307 tb->underlined = False;
3308 tb->selected = False;
3309 tb->script = 0;
3310 tb->sections = NULL;
3311 tb->nsections = 0;
3312 tb->prior = NULL;
3313 tb->next = NULL;
3314 return tb;
3317 void
3318 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3319 unsigned int kanji, unsigned int underlined, int script,
3320 WMRulerMargins *margins)
3322 TextBlock *tb = (TextBlock *) vtb;
3323 if (!tb)
3324 return;
3326 tb->first = first;
3327 tb->kanji = kanji;
3328 tb->underlined = underlined;
3329 tb->script = script;
3330 tb->marginN = newMargin(tPtr, margins);
3333 void
3334 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3335 unsigned int *kanji, unsigned int *underlined, int *script,
3336 WMRulerMargins *margins)
3338 TextBlock *tb = (TextBlock *) vtb;
3339 if (!tb)
3340 return;
3342 if (first) *first = tb->first;
3343 if (kanji) *kanji = tb->kanji;
3344 if (underlined) *underlined = tb->underlined;
3345 if (script) *script = tb->script;
3346 if (margins) margins = &tPtr->margins[tb->marginN];
3351 void
3352 WMPrependTextBlock(WMText *tPtr, void *vtb)
3354 TextBlock *tb = (TextBlock *)vtb;
3356 if (!tPtr || !tb)
3357 return;
3359 if (tb->graphic) {
3360 if(tb->object) {
3361 WMWidget *w = tb->d.widget;
3362 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3363 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3364 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3367 WMAddToArray(tPtr->gfxItems, (void *)tb);
3368 tPtr->tpos = 0;
3369 } else tPtr->tpos = tb->used;
3371 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3372 tb->next = tb->prior = NULL;
3373 tb->first = True;
3374 tPtr->lastTextBlock = tPtr->firstTextBlock
3375 = tPtr->currentTextBlock = tb;
3376 return;
3379 if(!tb->first) {
3380 tb->marginN = tPtr->currentTextBlock->marginN;
3383 tb->next = tPtr->currentTextBlock;
3384 tb->prior = tPtr->currentTextBlock->prior;
3385 if (tPtr->currentTextBlock->prior)
3386 tPtr->currentTextBlock->prior->next = tb;
3388 tPtr->currentTextBlock->prior = tb;
3389 if (!tb->prior)
3390 tPtr->firstTextBlock = tb;
3392 tPtr->currentTextBlock = tb;
3396 void
3397 WMAppendTextBlock(WMText *tPtr, void *vtb)
3399 TextBlock *tb = (TextBlock *)vtb;
3401 if (!tPtr || !tb)
3402 return;
3404 if (tb->graphic) {
3405 if(tb->object) {
3406 WMWidget *w = tb->d.widget;
3407 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3408 (W_VIEW(w))->attribs.cursor =
3409 tPtr->view->screen->defaultCursor;
3410 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3413 WMAddToArray(tPtr->gfxItems, (void *)tb);
3414 tPtr->tpos = 0;
3415 } else tPtr->tpos = tb->used;
3417 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3418 tb->next = tb->prior = NULL;
3419 tb->first = True;
3420 tPtr->lastTextBlock = tPtr->firstTextBlock
3421 = tPtr->currentTextBlock = tb;
3422 return;
3425 if(!tb->first) {
3426 tb->marginN = tPtr->currentTextBlock->marginN;
3429 tb->next = tPtr->currentTextBlock->next;
3430 tb->prior = tPtr->currentTextBlock;
3431 if (tPtr->currentTextBlock->next)
3432 tPtr->currentTextBlock->next->prior = tb;
3434 tPtr->currentTextBlock->next = tb;
3436 if (!tb->next)
3437 tPtr->lastTextBlock = tb;
3439 tPtr->currentTextBlock = tb;
3442 void *
3443 WMRemoveTextBlock(WMText *tPtr)
3445 TextBlock *tb = NULL;
3447 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3448 || !tPtr->currentTextBlock) {
3449 /* printf("cannot remove non existent TextBlock!\n"); */
3450 return NULL;
3453 tb = tPtr->currentTextBlock;
3454 if (tb->graphic) {
3455 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3457 if(tb->object) {
3458 WMUnmapWidget(tb->d.widget);
3462 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3463 if (tPtr->currentTextBlock->next)
3464 tPtr->currentTextBlock->next->prior = NULL;
3466 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3467 tPtr->currentTextBlock = tPtr->firstTextBlock;
3469 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3470 tPtr->currentTextBlock->prior->next = NULL;
3471 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3472 tPtr->currentTextBlock = tPtr->lastTextBlock;
3473 } else {
3474 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3475 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3476 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3479 return (void *)tb;
3482 void
3483 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3485 TextBlock *tb = (TextBlock *)vtb;
3486 if (!tPtr || !tb)
3487 return;
3489 if (tb->graphic) {
3490 if(tb->object) {
3491 /* naturally, there's a danger to destroying
3492 widgets whose action brings us here:
3493 ie. press a button to destroy it... need to
3494 find a safer way. till then... this stays commented out */
3495 /* WMDestroyWidget(tb->d.widget);
3496 wfree(tb->d.widget); */
3497 tb->d.widget = NULL;
3498 } else {
3499 WMReleasePixmap(tb->d.pixmap);
3500 tb->d.pixmap = NULL;
3502 } else {
3503 WMReleaseFont(tb->d.font);
3506 WMReleaseColor(tb->color);
3507 if (tb->sections && tb->nsections > 0)
3508 wfree(tb->sections);
3509 wfree(tb->text);
3510 wfree(tb);
3511 tb = NULL;
3516 void
3517 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3519 if (!tPtr)
3520 return;
3522 if (color)
3523 tPtr->fgGC = WMColorGC(color);
3524 else
3525 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3527 paintText(tPtr);
3530 void
3531 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3533 if (!tPtr)
3534 return;
3536 if (color) {
3537 tPtr->bgGC = WMColorGC(color);
3538 W_SetViewBackgroundColor(tPtr->view, color);
3539 } else {
3540 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3541 W_SetViewBackgroundColor(tPtr->view,
3542 WMWhiteColor(tPtr->view->screen));
3545 paintText(tPtr);
3548 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3550 if (!tPtr)
3551 return;
3553 if (tPtr->bgPixmap)
3554 WMReleasePixmap(tPtr->bgPixmap);
3556 if (pixmap)
3557 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3558 else
3559 tPtr->bgPixmap = NULL;
3562 void
3563 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3565 if (!tPtr)
3566 return;
3567 tPtr->flags.relief = relief;
3568 textDidResize(tPtr->view->delegate, tPtr->view);
3571 void
3572 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3574 if (!tPtr)
3575 return;
3577 if (shouldhave && !tPtr->hS) {
3578 tPtr->hS = WMCreateScroller(tPtr);
3579 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3580 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3581 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3582 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3583 WMMapWidget(tPtr->hS);
3584 } else if (!shouldhave && tPtr->hS) {
3585 WMUnmapWidget(tPtr->hS);
3586 WMDestroyWidget(tPtr->hS);
3587 tPtr->hS = NULL;
3590 tPtr->hpos = 0;
3591 tPtr->prevHpos = 0;
3592 textDidResize(tPtr->view->delegate, tPtr->view);
3596 void
3597 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3599 if (!tPtr)
3600 return;
3602 if(shouldhave && !tPtr->ruler) {
3603 tPtr->ruler = WMCreateRuler(tPtr);
3604 (W_VIEW(tPtr->ruler))->attribs.cursor =
3605 tPtr->view->screen->defaultCursor;
3606 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3607 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3608 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3609 } else if(!shouldhave && tPtr->ruler) {
3610 WMShowTextRuler(tPtr, False);
3611 WMDestroyWidget(tPtr->ruler);
3612 tPtr->ruler = NULL;
3614 textDidResize(tPtr->view->delegate, tPtr->view);
3617 void
3618 WMShowTextRuler(WMText *tPtr, Bool show)
3620 if(!tPtr)
3621 return;
3622 if(!tPtr->ruler)
3623 return;
3625 if(tPtr->flags.monoFont)
3626 show = False;
3628 tPtr->flags.rulerShown = show;
3629 if(show) {
3630 WMMapWidget(tPtr->ruler);
3631 } else {
3632 WMUnmapWidget(tPtr->ruler);
3635 textDidResize(tPtr->view->delegate, tPtr->view);
3638 Bool
3639 WMGetTextRulerShown(WMText *tPtr)
3641 if(!tPtr)
3642 return 0;
3644 if(!tPtr->ruler)
3645 return 0;
3647 return tPtr->flags.rulerShown;
3651 void
3652 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3654 if (!tPtr)
3655 return;
3657 if (shouldhave && !tPtr->vS) {
3658 tPtr->vS = WMCreateScroller(tPtr);
3659 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3660 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3661 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3662 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3663 WMMapWidget(tPtr->vS);
3664 } else if (!shouldhave && tPtr->vS) {
3665 WMUnmapWidget(tPtr->vS);
3666 WMDestroyWidget(tPtr->vS);
3667 tPtr->vS = NULL;
3670 tPtr->vpos = 0;
3671 tPtr->prevVpos = 0;
3672 textDidResize(tPtr->view->delegate, tPtr->view);
3677 Bool
3678 WMScrollText(WMText *tPtr, int amount)
3680 Bool scroll=False;
3681 if (!tPtr)
3682 return False;
3683 if (amount == 0 || !tPtr->view->flags.realized)
3684 return False;
3686 if (amount < 0) {
3687 if (tPtr->vpos > 0) {
3688 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3689 else tPtr->vpos=0;
3690 scroll=True;
3691 } } else {
3692 int limit = tPtr->docHeight - tPtr->visible.h;
3693 if (tPtr->vpos < limit) {
3694 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3695 else tPtr->vpos = limit;
3696 scroll = True;
3697 } }
3699 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3700 updateScrollers(tPtr);
3701 paintText(tPtr);
3703 tPtr->prevVpos = tPtr->vpos;
3704 return scroll;
3707 Bool
3708 WMPageText(WMText *tPtr, Bool direction)
3710 if (!tPtr) return False;
3711 if (!tPtr->view->flags.realized) return False;
3713 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3716 void
3717 WMSetTextEditable(WMText *tPtr, Bool editable)
3719 if (!tPtr)
3720 return;
3721 tPtr->flags.editable = editable;
3724 int
3725 WMGetTextEditable(WMText *tPtr)
3727 if (!tPtr)
3728 return 0;
3729 return tPtr->flags.editable;
3732 void
3733 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3735 if (!tPtr)
3736 return;
3737 tPtr->flags.indentNewLine = indent;
3740 void
3741 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3743 if (!tPtr)
3744 return;
3745 tPtr->flags.ignoreNewLine = ignore;
3748 Bool
3749 WMGetTextIgnoresNewline(WMText *tPtr)
3751 if (!tPtr)
3752 return True;
3753 return tPtr->flags.ignoreNewLine;
3756 void
3757 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3759 if (!tPtr)
3760 return;
3762 if (mono) {
3763 if(tPtr->flags.rulerShown)
3764 WMShowTextRuler(tPtr, False);
3765 if(tPtr->flags.alignment != WALeft)
3766 tPtr->flags.alignment = WALeft;
3769 tPtr->flags.monoFont = mono;
3770 textDidResize(tPtr->view->delegate, tPtr->view);
3773 Bool
3774 WMGetTextUsesMonoFont(WMText *tPtr)
3776 if (!tPtr)
3777 return True;
3778 return tPtr->flags.monoFont;
3782 void
3783 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3785 if (!tPtr)
3786 return;
3788 WMReleaseFont(tPtr->dFont);
3789 if (font)
3790 tPtr->dFont = WMRetainFont(font);
3791 else
3792 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3795 WMFont *
3796 WMGetTextDefaultFont(WMText *tPtr)
3798 if (!tPtr)
3799 return NULL;
3800 else
3801 return WMRetainFont(tPtr->dFont);
3804 void
3805 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3807 if (!tPtr)
3808 return;
3810 WMReleaseColor(tPtr->dColor);
3811 if (color)
3812 tPtr->dColor = WMRetainColor(color);
3813 else
3814 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3817 WMColor *
3818 WMGetTextDefaultColor(WMText *tPtr)
3820 if (!tPtr)
3821 return NULL;
3822 else
3823 return WMRetainColor(tPtr->dColor);
3826 void
3827 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3829 if (!tPtr)
3830 return;
3831 if(tPtr->flags.monoFont)
3832 tPtr->flags.alignment = WALeft;
3833 else
3834 tPtr->flags.alignment = alignment;
3835 WMThawText(tPtr);
3838 int
3839 WMGetTextInsertType(WMText *tPtr)
3841 if (!tPtr)
3842 return 0;
3843 return tPtr->flags.prepend;
3847 void
3848 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3850 if (!tPtr || !color)
3851 return;
3853 setSelectionProperty(tPtr, NULL, color, -1);
3856 WMColor *
3857 WMGetTextSelectionColor(WMText *tPtr)
3859 TextBlock *tb;
3861 if (!tPtr)
3862 return NULL;
3864 tb = tPtr->currentTextBlock;
3866 if (!tb || !tPtr->flags.ownsSelection)
3867 return NULL;
3869 if(!tb->selected)
3870 return NULL;
3872 return tb->color;
3876 void
3877 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3879 if (!tPtr || !font)
3880 return;
3882 setSelectionProperty(tPtr, font, NULL, -1) ;
3885 WMFont *
3886 WMGetTextSelectionFont(WMText *tPtr)
3888 TextBlock *tb;
3890 if (!tPtr)
3891 return NULL;
3893 tb = tPtr->currentTextBlock;
3895 if (!tb || !tPtr->flags.ownsSelection)
3896 return NULL;
3898 if(!tb->selected)
3899 return NULL;
3901 if(tb->graphic) {
3902 tb = getFirstNonGraphicBlockFor(tb, 1);
3903 if(!tb)
3904 return NULL;
3906 return (tb->selected ? tb->d.font : NULL);
3910 void
3911 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3913 if (!tPtr || (underlined!=0 && underlined !=1))
3914 return;
3916 setSelectionProperty(tPtr, NULL, NULL, underlined);
3920 int
3921 WMGetTextSelectionUnderlined(WMText *tPtr)
3923 TextBlock *tb;
3925 if (!tPtr)
3926 return 0;
3928 tb = tPtr->currentTextBlock;
3930 if (!tb || !tPtr->flags.ownsSelection)
3931 return 0;
3933 if(!tb->selected)
3934 return 0;
3936 return tb->underlined;
3940 void
3941 WMFreezeText(WMText *tPtr)
3943 if (!tPtr)
3944 return;
3946 tPtr->flags.frozen = True;
3950 void
3951 WMThawText(WMText *tPtr)
3953 if (!tPtr)
3954 return;
3956 tPtr->flags.frozen = False;
3958 if(tPtr->flags.monoFont) {
3959 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3960 TextBlock *tb;
3962 /* make sure to unmap widgets no matter where they are */
3963 /* they'll be later remapped if needed by paintText */
3964 for(j=0; j<c; j++) {
3965 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3966 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3967 WMUnmapWidget(tb->d.widget);
3973 tPtr->flags.laidOut = False;
3974 layOutDocument(tPtr);
3975 updateScrollers(tPtr);
3976 paintText(tPtr);
3977 tPtr->flags.needsLayOut = False;
3981 /* find first occurence of a string */
3982 static char *
3983 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
3984 Bool caseSensitive)
3986 char *ptr;
3988 if(!haystack || !needle || !end)
3989 return NULL;
3991 for (ptr = haystack; ptr < end; ptr++) {
3992 if(caseSensitive) {
3993 if (*ptr == *needle && !strncmp(ptr, needle, len))
3994 return ptr;
3996 } else {
3997 if (tolower(*ptr) == tolower(*needle) &&
3998 !strncasecmp(ptr, needle, len))
3999 return ptr;
4003 return NULL;
4006 /* find last occurence of a string */
4007 static char *
4008 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4009 Bool caseSensitive)
4011 char *ptr;
4013 if(!haystack || !needle || !end)
4014 return NULL;
4016 for (ptr = haystack-2; ptr > end; ptr--) {
4017 if(caseSensitive) {
4018 if (*ptr == *needle && !strncmp(ptr, needle, len))
4019 return ptr;
4020 } else {
4021 if (tolower(*ptr) == tolower(*needle) &&
4022 !strncasecmp(ptr, needle, len))
4023 return ptr;
4027 return NULL;
4031 Bool
4032 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4033 Bool caseSensitive)
4035 TextBlock *tb;
4036 char *mark=NULL;
4037 unsigned short pos;
4039 if (!tPtr || !needle)
4040 return False;
4042 #if 0
4043 if (! (tb = tPtr->currentTextBlock)) {
4044 if (! (tb = ( (direction > 0) ?
4045 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4046 return False;
4048 } else {
4049 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4050 tb = (direction>0) ? tb->next : tb->prior; */
4051 if(tb != tPtr->lastTextBlock)
4052 tb = tb->prior;
4054 #endif
4055 tb = tPtr->currentTextBlock;
4056 pos = tPtr->tpos;
4059 while(tb) {
4060 if (!tb->graphic) {
4062 if(direction > 0) {
4063 if(pos+1 < tb->used)
4064 pos++;
4066 if(tb->used - pos> 0 && pos > 0) {
4067 mark = mystrstr(&tb->text[pos], needle,
4068 strlen(needle), &tb->text[tb->used], caseSensitive);
4070 } else {
4071 tb = tb->next;
4072 pos = 0;
4073 continue;
4076 } else {
4077 if(pos-1 > 0)
4078 pos--;
4080 if(pos > 0) {
4081 mark = mystrrstr(&tb->text[pos], needle,
4082 strlen(needle), tb->text, caseSensitive);
4083 } else {
4084 tb = tb->prior;
4085 if(!tb)
4086 return False;
4087 pos = tb->used;
4088 continue;
4093 if(mark) {
4094 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4096 tPtr->tpos = (int)(mark - tb->text);
4097 tPtr->currentTextBlock = tb;
4098 updateCursorPosition(tPtr);
4099 tPtr->sel.y = tPtr->cursor.y+5;
4100 tPtr->sel.h = tPtr->cursor.h-10;
4101 tPtr->sel.x = tPtr->cursor.x +1;
4102 tPtr->sel.w = WMIN(WMWidthOfString(font,
4103 &tb->text[tPtr->tpos], strlen(needle)),
4104 tPtr->docWidth - tPtr->sel.x);
4105 tPtr->flags.ownsSelection = True;
4106 paintText(tPtr);
4108 return True;
4112 tb = (direction>0) ? tb->next : tb->prior;
4113 if(tb) {
4114 pos = (direction>0) ? 0 : tb->used;
4118 return False;
4122 Bool
4123 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4125 if (!tPtr)
4126 return False;
4128 if (!tPtr->flags.ownsSelection)
4129 return False;
4131 removeSelection(tPtr);
4133 if(replacement) {
4134 insertTextInteractively(tPtr, replacement, strlen(replacement));
4135 updateCursorPosition(tPtr);
4136 paintText(tPtr);
4139 return True;