FindInTextStream completed for forward, backward, case (in)sensitive searches
[wmaker-crm.git] / WINGs / wtext.c
blobdefbb27a3c65c54ccf6a1f012bfa128e70797709
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 * - FIX wrap... long lines that don't fit are not char wrapped yet.
14 * - hrm... something to do with already having tbs...
15 * - selection code... selects can be funny if it crosses over. use rect?
16 * - also inspect behaviour for WACenter and WARight
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 unsigned int y;
88 unsigned int x;
89 unsigned int h;
90 unsigned int w;
91 } myRect;
94 typedef struct W_Text {
95 W_Class widgetClass; /* the class number of this widget */
96 W_View *view; /* the view referring to this instance */
98 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
100 WMScroller *vS; /* the vertical scroller */
101 unsigned int vpos; /* the current vertical position */
102 unsigned int prevVpos; /* the previous vertical position */
104 WMScroller *hS; /* the horizontal scroller */
105 unsigned int hpos; /* the current horizontal position */
106 unsigned int prevHpos; /* the previous horizontal position */
108 WMFont *dFont; /* the default font */
109 WMColor *dColor; /* the default color */
110 WMPixmap *dBulletPix; /* the default pixmap for bullets */
112 GC bgGC; /* the background GC to draw with */
113 GC fgGC; /* the foreground GC to draw with */
114 Pixmap db; /* the buffer on which to draw */
116 myRect visible; /* the actual rectangle that can be drawn into */
117 myRect cursor; /* the position and (height) of cursor */
118 myRect sel; /* the selection rectangle */
120 WMPoint clicked; /* where in the _document_ was clicked */
122 unsigned short tpos; /* the position in the currentTextBlock */
123 unsigned short docWidth; /* the width of the entire document */
124 unsigned int docHeight; /* the height of the entire document */
126 TextBlock *firstTextBlock;
127 TextBlock *lastTextBlock;
128 TextBlock *currentTextBlock;
130 WMArray *gfxItems; /* a nice array of graphic items */
132 #if DO_BLINK
133 WMHandlerID timerID; /* for nice twinky-winky */
134 #endif
136 WMAction *parser;
137 WMAction *writer;
138 WMTextDelegate *delegate;
139 Time lastClickTime;
141 WMRulerMargins *margins; /* an array of margins */
143 unsigned int nMargins:7; /* the total number of margins in use */
144 struct {
145 unsigned int monoFont:1; /* whether to ignore formats and graphic */
146 unsigned int focused:1; /* whether this instance has input focus */
147 unsigned int editable:1; /* "silly user, you can't edit me" */
148 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
149 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
150 unsigned int extendSelection:1; /* shift-drag to select more regions */
152 unsigned int rulerShown:1; /* whether the ruler is shown or not */
153 unsigned int frozen:1; /* whether screen updates are to be made */
154 unsigned int cursorShown:1; /* whether to show the cursor */
155 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
156 unsigned int horizOnDemand:1;/* if a large image should appear*/
157 unsigned int needsLayOut:1; /* in case of Append/Deletes */
158 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
159 unsigned int indentNewLine:1;/* add " " for a newline typed */
160 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
161 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
162 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
163 WMAlignment alignment:2; /* the alignment for text */
164 WMReliefType relief:3; /* the relief to display with */
165 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
166 unsigned int first:1; /* for plain text parsing, newline? */
167 /* unsigned int RESERVED:1; */
168 } flags;
169 } Text;
172 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
173 if ((T)->delegate && (T)->delegate->C)\
174 (*(T)->delegate->C)((T)->delegate,notif);\
175 WMPostNotification(notif);\
176 WMReleaseNotification(notif);}
179 #define TYPETEXT 0
181 static void
182 output(char *ptr, int len)
184 char s[len+1];
185 memcpy(s, ptr, len);
186 s[len] = 0;
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
188 printf("[%s]\n", s);
192 #if DO_BLINK
193 #define CURSOR_BLINK_ON_DELAY 600
194 #define CURSOR_BLINK_OFF_DELAY 400
195 #endif
197 static char *default_bullet[] = {
198 "6 6 4 1",
199 " c None s None", ". c black",
200 "X c white", "o c #808080",
201 " ... ",
202 ".XX.. ",
203 ".XX..o",
204 ".....o",
205 " ...oo",
206 " ooo "};
208 static void handleEvents(XEvent *event, void *data);
209 static void layOutDocument(Text *tPtr);
210 static void updateScrollers(Text *tPtr);
213 static int
214 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
216 unsigned int i=0;
218 for(i=0; i < tPtr->nMargins; i++) {
220 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
221 return i;
224 return -1;
229 static int
230 newMargin(Text *tPtr, WMRulerMargins *margins)
232 int n;
234 if (!margins) {
235 tPtr->margins[0].retainCount++;
236 return 0;
239 n = getMarginNumber(tPtr, margins);
241 if (n == -1) {
243 tPtr->margins = wrealloc(tPtr->margins,
244 (++tPtr->nMargins)*sizeof(WMRulerMargins));
246 n = tPtr->nMargins-1;
247 tPtr->margins[n].left = margins->left;
248 tPtr->margins[n].first = margins->first;
249 tPtr->margins[n].body = margins->body;
250 tPtr->margins[n].right = margins->right;
251 /* for each tab... */
252 tPtr->margins[n].retainCount = 1;
253 } else {
254 tPtr->margins[n].retainCount++;
257 return n;
260 static Bool
261 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
263 unsigned short i, w, lw, selected = False, extend = False;
264 myRect sel;
267 /* if selection rectangle completely encloses the section */
268 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
269 && (tb->sections[s]._y + tb->sections[s].h
270 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
271 sel.x = 0;
272 sel.w = tPtr->visible.w;
273 selected = extend = True;
275 /* or if it starts on a line and then goes further down */
276 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
277 && (tb->sections[s]._y + tb->sections[s].h
278 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
279 && (tb->sections[s]._y + tb->sections[s].h
280 >= tPtr->visible.y + tPtr->sel.y) ) {
281 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
282 sel.w = tPtr->visible.w;
283 selected = extend = True;
285 /* or if it begins before a line, but ends on it */
286 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
287 && (tb->sections[s]._y + tb->sections[s].h
288 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
289 && (tb->sections[s]._y
290 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
292 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
293 sel.w = tPtr->sel.x + tPtr->sel.w;
294 else
295 sel.w = tPtr->sel.x;
297 sel.x = 0;
298 selected = True;
300 /* or if the selection rectangle lies entirely within a line */
301 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
302 && (tPtr->sel.w >= 2)
303 && (tb->sections[s]._y + tb->sections[s].h
304 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
305 sel.x = tPtr->sel.x;
306 sel.w = tPtr->sel.w;
307 selected = True;
310 if (selected) {
311 selected = False;
313 /* if not within (modified) selection rectangle */
314 if ( tb->sections[s].x > sel.x + sel.w
315 || tb->sections[s].x + tb->sections[s].w < sel.x)
316 return False;
318 if (tb->graphic) {
319 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
320 && tb->sections[s].x >= sel.x) {
321 rect->width = tb->sections[s].w;
322 rect->x = tb->sections[s].x;
323 selected = True;
325 } else {
327 i = tb->sections[s].begin;
328 lw = 0;
330 if (0&& tb->sections[s].x >= sel.x) {
331 tb->s_begin = tb->sections[s].begin;
332 goto _selEnd;
335 while (++i <= tb->sections[s].end) {
337 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
338 lw += w;
340 if (lw + tb->sections[s].x >= sel.x
341 || i == tb->sections[s].end ) {
342 lw -= w;
343 i--;
344 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
345 break;
349 if (i > tb->sections[s].end) {
350 printf("WasSelected: (i > tb->sections[s].end) \n");
351 return False;
354 _selEnd: rect->x = tb->sections[s].x + lw;
355 lw = 0;
356 while(++i <= tb->sections[s].end) {
358 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
359 lw += w;
361 if (lw + rect->x >= sel.x + sel.w
362 || i == tb->sections[s].end ) {
364 if (i != tb->sections[s].end) {
365 lw -= w;
366 i--;
369 rect->width = lw;
370 if (tb->sections[s].last && sel.x + sel.w
371 >= tb->sections[s].x + tb->sections[s].w
372 && extend ) {
373 rect->width += (tPtr->visible.w - rect->x - lw);
376 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
377 selected = True;
378 break;
379 } } } }
381 if (selected) {
382 rect->y = tb->sections[s]._y - tPtr->vpos;
383 rect->height = tb->sections[s].h;
384 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
386 return selected;
390 static void
391 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
393 TextBlock *tb;
394 int isFont=False;
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
400 if(font && (!color || underlined==-1))
401 isFont = True;
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
409 if(isFont) {
410 if(!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
414 } else if(underlined !=-1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
423 TextBlock *midtb, *otb = tb;
425 if(underlined != -1) {
426 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
427 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
428 False, (tb->s_end - tb->s_begin));
429 } else {
430 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
431 &(tb->text[tb->s_begin]),
432 (isFont?font:tb->d.font),
433 (isFont?tb->color:color),
434 False, (tb->s_end - tb->s_begin));
438 if (midtb) {
439 if(underlined != -1) {
440 midtb->underlined = underlined;
441 } else {
442 midtb->underlined = otb->underlined;
445 midtb->selected = !True;
446 midtb->s_begin = 0;
447 midtb->s_end = midtb->used;
448 tPtr->currentTextBlock = tb;
449 WMAppendTextBlock(tPtr, midtb);
450 tb = tPtr->currentTextBlock;
453 if (otb->used - otb->s_end > 0) {
454 TextBlock *ntb;
455 ntb = (TextBlock *)
456 WMCreateTextBlockWithText(tPtr,
457 &(otb->text[otb->s_end]), otb->d.font, otb->color,
458 False, otb->used - otb->s_end);
460 if (ntb) {
461 ntb->underlined = otb->underlined;
462 ntb->selected = False;
463 WMAppendTextBlock(tPtr, ntb);
464 tb = tPtr->currentTextBlock;
468 if (midtb) {
469 tPtr->currentTextBlock = midtb;
472 otb->selected = False;
473 otb->used = otb->s_begin;
477 tb = tb->next;
480 tPtr->flags.needsLayOut = True;
481 WMThawText(tPtr);
483 /* in case the size changed... */
484 if(isFont && tPtr->currentTextBlock) {
485 TextBlock *tb = tPtr->currentTextBlock;
487 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
488 tPtr->sel.y = 3 + tb->sections[0]._y;
489 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
490 tPtr->sel.w = tb->sections[tb->nsections-1].w;
491 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
492 tPtr->sel.x = 0;
494 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
500 static Bool
501 removeSelection(Text *tPtr)
503 TextBlock *tb = NULL;
504 Bool first = False;
506 if (!(tb = tPtr->firstTextBlock))
507 return False;
509 while (tb) {
510 if (tb->selected) {
511 if(!first && !tb->graphic) {
512 WMReleaseFont(tPtr->dFont);
513 tPtr->dFont = WMRetainFont(tb->d.font);
514 first = True;
517 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
518 tPtr->currentTextBlock = tb;
519 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
520 tb = tPtr->currentTextBlock;
521 if (tb)
522 tPtr->tpos = 0;
523 continue;
525 } else if (tb->s_end <= tb->used) {
526 memmove(&(tb->text[tb->s_begin]),
527 &(tb->text[tb->s_end]), tb->used - tb->s_end);
528 tb->used -= (tb->s_end - tb->s_begin);
529 tb->selected = False;
530 tPtr->tpos = tb->s_begin;
535 tb = tb->next;
537 return True;
540 static TextBlock *
541 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
543 TextBlock *hold = tb;
545 if (!tb)
546 return NULL;
548 while (tb) {
549 if (!tb->graphic)
550 break;
551 tb = (dir? tb->next : tb->prior);
554 if(!tb) {
555 tb = hold;
556 while (tb) {
557 if (!tb->graphic)
558 break;
559 tb = (dir? tb->prior : tb->next);
563 if(!tb)
564 return NULL;
566 return tb;
570 static Bool
571 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
572 TextBlock *tb)
574 if (tPtr->flags.monoFont && tb->graphic) {
575 tb = getFirstNonGraphicBlockFor(tb, *dir);
576 if(!tb)
577 return 0;
579 if (tb->graphic) {
580 tPtr->currentTextBlock =
581 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
582 tPtr->tpos = 0;
583 return 0;
587 *dir = !(y <= tb->sections[0].y);
588 if(*dir) {
589 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
590 && (y >= tb->sections[0]._y ) ) {
591 /* if it's on the same line */
592 if(x < tb->sections[0].x)
593 *dir = 0;
595 } else {
596 if ( ( y <= tb->sections[tb->nsections-1]._y
597 + tb->sections[tb->nsections-1].h )
598 && (y >= tb->sections[tb->nsections-1]._y ) ) {
599 /* if it's on the same line */
600 if(x > tb->sections[tb->nsections-1].x)
601 *dir = 1;
605 return 1;
609 static void
610 paintText(Text *tPtr)
612 TextBlock *tb;
613 WMFont *font;
614 GC gc, greyGC;
615 char *text;
616 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
617 WMScreen *scr = tPtr->view->screen;
618 Display *dpy = tPtr->view->screen->display;
619 Window win = tPtr->view->window;
621 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
622 return;
624 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
625 0, 0, tPtr->visible.w, tPtr->visible.h);
627 if (! (tb = tPtr->currentTextBlock)) {
628 if (! (tb = tPtr->firstTextBlock)) {
629 goto _copy_area;
633 if (tPtr->flags.ownsSelection)
634 greyGC = WMColorGC(WMGrayColor(scr));
636 done = False;
640 /* first, which direction? Don't waste time looking all over,
641 since the parts to be drawn will most likely be near what
642 was previously drawn */
643 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
644 goto _copy_area;
646 while(tb) {
648 if (tb->graphic && tPtr->flags.monoFont)
649 goto _getSibling;
651 if(dir) {
652 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
653 + tb->sections[tb->nsections-1].h)
654 break;
655 } else {
656 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
657 + tb->sections[tb->nsections-1].h)
658 break;
661 _getSibling:
662 if(dir) {
663 if(tb->next)
664 tb = tb->next;
665 else break;
666 } else {
667 if(tb->prior)
668 tb = tb->prior;
669 else break;
674 /* first, place all text that can be viewed */
675 while (!done && tb) {
677 /* paragraph diagnostic
678 if(tb->blank) {tb->text[0] = 'F'; } */
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for(s=0; s<tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 gc = tPtr->fgGC;
700 } else {
701 font = tb->d.font;
702 gc = WMColorGC(tb->color);
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
708 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, greyGC,
711 rect.x, rect.y, rect.width, rect.height);
715 prev_y = tb->sections[s]._y;
717 len = tb->sections[s].end - tb->sections[s].begin;
718 text = &(tb->text[tb->sections[s].begin]);
719 y = tb->sections[s].y - tPtr->vpos;
720 WMDrawString(scr, tPtr->db, gc, font,
721 tb->sections[s].x - tPtr->hpos, y, text, len);
723 if (!tPtr->flags.monoFont && tb->underlined) {
724 XDrawLine(dpy, tPtr->db, gc,
725 tb->sections[s].x - tPtr->hpos,
726 y + font->y + 1,
727 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
728 y + font->y + 1);
733 tb = (!done? tb->next : NULL);
737 /* now , show all graphic items that can be viewed */
738 c = WMGetArrayItemCount(tPtr->gfxItems);
739 if (c > 0 && !tPtr->flags.monoFont) {
740 int j, h;
742 for(j=0; j<c; j++) {
743 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
745 /* if it's not viewable, and mapped, unmap it */
746 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
747 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
749 if(tb->object) {
750 if ((W_VIEW(tb->d.widget))->flags.mapped) {
751 WMUnmapWidget(tb->d.widget);
754 } else {
755 /* if it's viewable, and not mapped, map it */
756 if(tb->object) {
757 W_View *view = W_VIEW(tb->d.widget);
759 if (!view->flags.realized)
760 WMRealizeWidget(tb->d.widget);
761 if(!view->flags.mapped) {
762 XMapWindow(view->screen->display, view->window);
763 XFlush(view->screen->display);
764 view->flags.mapped = 1;
768 if (tPtr->flags.ownsSelection) {
769 XRectangle rect;
771 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
772 tb->selected = True;
773 XFillRectangle(dpy, tPtr->db, greyGC,
774 rect.x, rect.y, rect.width, rect.height);
778 if(tb->object) {
779 WMMoveWidget(tb->d.widget,
780 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
781 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
782 h = WMWidgetHeight(tb->d.widget) + 1;
784 } else {
785 WMDrawPixmap(tb->d.pixmap, tPtr->db,
786 tb->sections[0].x - tPtr->hpos,
787 tb->sections[0].y - tPtr->vpos);
788 h = tb->d.pixmap->height + 1;
792 if (!tPtr->flags.monoFont && tb->underlined) {
793 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
794 tb->sections[0].x - tPtr->hpos,
795 tb->sections[0].y + h - tPtr->vpos,
796 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
797 tb->sections[0].y + h - tPtr->vpos);
798 } } } }
801 _copy_area:
802 if (tPtr->flags.editable && tPtr->flags.cursorShown
803 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
804 int y = tPtr->cursor.y - tPtr->vpos;
805 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
806 tPtr->cursor.x, y,
807 tPtr->cursor.x, y + tPtr->cursor.h);
810 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
811 tPtr->visible.w, tPtr->visible.h,
812 tPtr->visible.x, tPtr->visible.y);
814 W_DrawRelief(scr, win, 0, 0,
815 tPtr->view->size.width, tPtr->view->size.height,
816 tPtr->flags.relief);
818 if (tPtr->ruler && tPtr->flags.rulerShown)
819 XDrawLine(dpy, win, tPtr->fgGC,
820 2, 42, tPtr->view->size.width-4, 42);
824 static void
825 mouseOverObject(Text *tPtr, int x, int y)
827 TextBlock *tb;
828 Bool result = False;
830 x -= tPtr->visible.x;
831 x += tPtr->hpos;
832 y -= tPtr->visible.y;
833 y += tPtr->vpos;
835 if(tPtr->flags.ownsSelection) {
836 if(tPtr->sel.x <= x
837 && tPtr->sel.y <= y
838 && tPtr->sel.x + tPtr->sel.w >= x
839 && tPtr->sel.y + tPtr->sel.h >= y) {
840 tPtr->flags.isOverGraphic = 1;
841 result = True;
846 if(!result) {
847 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
849 if (c<1)
850 tPtr->flags.isOverGraphic = 0;
853 for(j=0; j<c; j++) {
854 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
856 if(!tb || !tb->sections) {
857 tPtr->flags.isOverGraphic = 0;
858 return;
861 if(!tb->object) {
862 if(tb->sections[0].x <= x
863 && tb->sections[0].y <= y
864 && tb->sections[0].x + tb->sections[0].w >= x
865 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
866 tPtr->flags.isOverGraphic = 3;
867 result = True;
868 break;
876 if(!result)
877 tPtr->flags.isOverGraphic = 0;
880 tPtr->view->attribs.cursor = (result?
881 tPtr->view->screen->defaultCursor
882 : tPtr->view->screen->textCursor);
884 XSetWindowAttributes attribs;
885 attribs.cursor = tPtr->view->attribs.cursor;
886 XChangeWindowAttributes(tPtr->view->screen->display,
887 tPtr->view->window, CWCursor,
888 &attribs);
892 #if DO_BLINK
894 static void
895 blinkCursor(void *data)
897 Text *tPtr = (Text*)data;
899 if (tPtr->flags.cursorShown) {
900 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
901 blinkCursor, data);
902 } else {
903 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
904 blinkCursor, data);
906 paintText(tPtr);
907 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
909 #endif
911 static void
912 updateCursorPosition(Text *tPtr)
914 TextBlock *tb = NULL;
915 int x, y, h, s;
917 if(tPtr->flags.needsLayOut)
918 layOutDocument(tPtr);
920 if (! (tb = tPtr->currentTextBlock)) {
921 if (! (tb = tPtr->firstTextBlock)) {
922 tPtr->tpos = 0;
923 tPtr->cursor.h = tPtr->dFont->height;
924 tPtr->cursor.y = 2;
925 tPtr->cursor.x = 2;
926 return;
931 if(tb->blank) {
932 tPtr->tpos = 0;
933 y = tb->sections[0].y;
934 h = tb->sections[0].h;
935 x = tb->sections[0].x;
937 } else if(tb->graphic) {
938 y = tb->sections[0].y;
939 h = tb->sections[0].h;
940 x = tb->sections[0].x;
942 } else {
943 if(tPtr->tpos > tb->used)
944 tPtr->tpos = tb->used;
946 for(s=0; s<tb->nsections-1; s++) {
948 if(tPtr->tpos >= tb->sections[s].begin
949 && tPtr->tpos <= tb->sections[s].end)
950 break;
953 y = tb->sections[s]._y;
954 h = tb->sections[s].h;
955 x = tb->sections[s].x + WMWidthOfString(
956 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
957 &tb->text[tb->sections[s].begin],
958 tPtr->tpos - tb->sections[s].begin);
961 tPtr->cursor.y = y;
962 tPtr->cursor.h = h;
963 tPtr->cursor.x = x;
966 /* scroll the bars if the cursor is not visible */
967 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
968 if(tPtr->cursor.y+tPtr->cursor.h
969 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
970 tPtr->vpos +=
971 (tPtr->cursor.y+tPtr->cursor.h+10
972 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
973 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
974 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
979 updateScrollers(tPtr);
983 static void
984 cursorToTextPosition(Text *tPtr, int x, int y)
986 TextBlock *tb = NULL;
987 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
988 char *text;
990 if(tPtr->flags.needsLayOut)
991 layOutDocument(tPtr);
993 y += (tPtr->vpos - tPtr->visible.y);
994 if (y<0)
995 y = 0;
997 x -= (tPtr->visible.x - 2);
998 if (x<0)
999 x=0;
1001 /* clicked is relative to document, not window... */
1002 tPtr->clicked.x = x;
1003 tPtr->clicked.y = y;
1005 if (! (tb = tPtr->currentTextBlock)) {
1006 if (! (tb = tPtr->firstTextBlock)) {
1007 tPtr->tpos = 0;
1008 tPtr->cursor.h = tPtr->dFont->height;
1009 tPtr->cursor.y = 2;
1010 tPtr->cursor.x = 2;
1011 return;
1015 /* first, which direction? Most likely, newly clicked
1016 position will be close to previous */
1017 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1018 return;
1021 s = (dir? 0 : tb->nsections-1);
1022 if ( y >= tb->sections[s]._y
1023 && y <= tb->sections[s]._y + tb->sections[s].h) {
1024 goto _doneV;
1027 /* get the first (or last) section of the TextBlock that
1028 lies about the vertical click point */
1029 done = False;
1030 while (!done && tb) {
1032 if (tPtr->flags.monoFont && tb->graphic) {
1033 if( (dir?tb->next:tb->prior))
1034 tb = (dir?tb->next:tb->prior);
1035 continue;
1038 s = (dir? 0 : tb->nsections-1);
1039 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1041 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1042 ( y >= tb->sections[s]._y ) ) ) {
1043 done = True;
1044 } else {
1045 dir? s++ : s--;
1049 if (!done) {
1050 if ( (dir? tb->next : tb->prior)) {
1051 tb = (dir ? tb->next : tb->prior);
1052 } else {
1053 pos = tb->used;
1054 break; /* goto _doneH; */
1060 if (s<0 || s>=tb->nsections) {
1061 s = (dir? tb->nsections-1 : 0);
1064 _doneV:
1065 /* we have the line, which TextBlock on that line is it? */
1066 pos = (dir?0:tb->sections[s].begin);
1067 if (tPtr->flags.monoFont && tb->graphic) {
1068 TextBlock *hold = tb;
1069 tb = getFirstNonGraphicBlockFor(hold, dir);
1071 if(!tb) {
1072 tPtr->tpos = 0;
1073 tb = hold;
1074 s = 0;
1075 goto _doNothing;
1080 if(tb->blank)
1081 _w = 0;
1083 _y = tb->sections[s]._y;
1085 while (tb) {
1087 if (tPtr->flags.monoFont && tb->graphic) {
1088 tb = (dir ? tb->next : tb->prior);
1089 continue;
1092 if (dir) {
1093 if (tb->graphic) {
1094 if(tb->object)
1095 _w = WMWidgetWidth(tb->d.widget)-5;
1096 else
1097 _w = tb->d.pixmap->width-5;
1098 } else {
1099 text = &(tb->text[tb->sections[s].begin]);
1100 len = tb->sections[s].end - tb->sections[s].begin;
1101 _w = WMWidthOfString(tb->d.font, text, len);
1102 if (tb->sections[s].x + _w >= x)
1103 break;
1106 } else {
1107 if (tb->sections[s].x <= x)
1108 break;
1111 if ((dir? tb->next : tb->prior)) {
1112 TextBlock *nxt = (dir? tb->next : tb->prior);
1113 if (tPtr->flags.monoFont && nxt->graphic) {
1114 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1115 if (!nxt) {
1116 pos = (dir?0:tb->sections[s].begin);
1117 tPtr->cursor.x = tb->sections[s].x;
1118 goto _doneH;
1122 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1123 /* this must be the last/first on this line. stop */
1124 pos = (dir? tb->sections[s].end : 0);
1125 tPtr->cursor.x = tb->sections[s].x;
1126 if (!tb->blank) {
1127 if (tb->graphic) {
1128 if(tb->object)
1129 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1130 else
1131 tPtr->cursor.x += tb->d.pixmap->width;
1132 } else if (pos > tb->sections[s].begin) {
1133 tPtr->cursor.x +=
1134 WMWidthOfString(tb->d.font,
1135 &(tb->text[tb->sections[s].begin]),
1136 pos - tb->sections[s].begin);
1139 goto _doneH;
1143 if ( (dir? tb->next : tb->prior)) {
1144 tb = (dir ? tb->next : tb->prior);
1145 } else {
1146 done = True;
1147 break;
1150 if (tb)
1151 s = (dir? 0 : tb->nsections-1);
1154 /* we have said TextBlock, now where within it? */
1155 if (tb && !tb->graphic) {
1156 WMFont *f = tb->d.font;
1157 len = tb->sections[s].end - tb->sections[s].begin;
1158 text = &(tb->text[tb->sections[s].begin]);
1160 _w = x - tb->sections[s].x;
1161 pos = 0;
1163 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1164 pos++;
1166 tPtr->cursor.x = tb->sections[s].x +
1167 (pos? WMWidthOfString(f, text, pos) : 0);
1169 pos += tb->sections[s].begin;
1170 _doneH:
1171 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1174 _doNothing:
1175 if (!tb)
1176 printf("...for this app will surely crash :-)\n");
1178 tPtr->currentTextBlock = tb;
1179 tPtr->cursor.h = tb->sections[s].h;
1180 tPtr->cursor.y = tb->sections[s]._y;
1182 /* scroll the bars if the cursor is not visible */
1183 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1184 if(tPtr->cursor.y+tPtr->cursor.h
1185 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1186 tPtr->vpos +=
1187 (tPtr->cursor.y+tPtr->cursor.h+10
1188 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1189 updateScrollers(tPtr);
1190 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1191 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1192 updateScrollers(tPtr);
1199 static void
1200 autoSelectText(Text *tPtr, int clicks)
1202 int x, start;
1203 TextBlock *tb;
1204 char *mark = NULL, behind, ahead;
1206 if(!(tb = tPtr->currentTextBlock))
1207 return;
1209 if(clicks == 2) {
1212 switch(tb->text[tPtr->tpos]) {
1213 case ' ': return;
1215 case '<': case '>': behind = '<'; ahead = '>'; break;
1216 case '{': case '}': behind = '{'; ahead = '}'; break;
1217 case '[': case ']': behind = '['; ahead = ']'; break;
1219 default: behind = ahead = ' ';
1222 tPtr->sel.y = tPtr->cursor.y+5;
1223 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
1225 if(tb->graphic) {
1226 tPtr->sel.x = tb->sections[0].x;
1227 tPtr->sel.w = tb->sections[0].w;
1228 } else {
1229 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1231 start = tPtr->tpos;
1232 while(start > 0 && tb->text[start-1] != behind)
1233 start--;
1235 x = tPtr->cursor.x;
1236 if(tPtr->tpos > start){
1237 x -= WMWidthOfString(font, &tb->text[start],
1238 tPtr->tpos - start);
1240 tPtr->sel.x = (x<0?0:x)+1;
1242 if((mark = strchr(&tb->text[start], ahead))) {
1243 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1244 (int)(mark - &tb->text[start]));
1245 } else if(tb->used > start) {
1246 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1247 tb->used - start);
1251 } else if(clicks == 3) {
1252 TextBlock *cur = tb;
1254 while(tb && !tb->first) {
1255 tb = tb->prior;
1257 tPtr->sel.y = tb->sections[0]._y;
1259 tb = cur;
1260 while(tb->next && !tb->next->first) {
1261 tb = tb->next;
1263 tPtr->sel.h = tb->sections[tb->nsections-1]._y
1264 + 5 - tPtr->sel.y;
1266 tPtr->sel.x = 0;
1267 tPtr->sel.w = tPtr->docWidth;
1268 tPtr->clicked.x = 0; /* only for now, fix sel. code */
1271 tPtr->flags.ownsSelection = True;
1272 paintText(tPtr);
1277 static void
1278 updateScrollers(Text *tPtr)
1281 if (tPtr->flags.frozen)
1282 return;
1284 if (tPtr->vS) {
1285 if (tPtr->docHeight < tPtr->visible.h) {
1286 WMSetScrollerParameters(tPtr->vS, 0, 1);
1287 tPtr->vpos = 0;
1288 } else {
1289 float hmax = (float)(tPtr->docHeight);
1290 WMSetScrollerParameters(tPtr->vS,
1291 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1292 (float)tPtr->visible.h/hmax);
1294 } else tPtr->vpos = 0;
1296 if (tPtr->hS) {
1297 if (tPtr->docWidth < tPtr->visible.w) {
1298 WMSetScrollerParameters(tPtr->hS, 0, 1);
1299 tPtr->hpos = 0;
1300 } else {
1301 float wmax = (float)(tPtr->docWidth);
1302 WMSetScrollerParameters(tPtr->hS,
1303 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1304 (float)tPtr->visible.w/wmax);
1306 } else tPtr->hpos = 0;
1309 static void
1310 scrollersCallBack(WMWidget *w, void *self)
1312 Text *tPtr = (Text *)self;
1313 Bool scroll = False;
1314 int which;
1316 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1317 return;
1319 if (w == tPtr->vS) {
1320 int height;
1321 height = tPtr->visible.h;
1323 which = WMGetScrollerHitPart(tPtr->vS);
1324 switch(which) {
1326 case WSDecrementLine:
1327 if (tPtr->vpos > 0) {
1328 if (tPtr->vpos>16) tPtr->vpos-=16;
1329 else tPtr->vpos=0;
1330 scroll=True;
1332 break;
1334 case WSIncrementLine: {
1335 int limit = tPtr->docHeight - height;
1336 if (tPtr->vpos < limit) {
1337 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1338 else tPtr->vpos=limit;
1339 scroll = True;
1342 break;
1344 case WSDecrementPage:
1345 if(((int)tPtr->vpos - (int)height) >= 0)
1346 tPtr->vpos -= height;
1347 else
1348 tPtr->vpos = 0;
1350 scroll = True;
1351 break;
1353 case WSIncrementPage:
1354 tPtr->vpos += height;
1355 if (tPtr->vpos > (tPtr->docHeight - height))
1356 tPtr->vpos = tPtr->docHeight - height;
1357 scroll = True;
1358 break;
1361 case WSKnob:
1362 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1363 * (float)(tPtr->docHeight - height);
1364 scroll = True;
1365 break;
1367 case WSKnobSlot:
1368 case WSNoPart:
1369 break;
1371 scroll = (tPtr->vpos != tPtr->prevVpos);
1372 tPtr->prevVpos = tPtr->vpos;
1376 if (w == tPtr->hS) {
1377 int width = tPtr->visible.w;
1379 which = WMGetScrollerHitPart(tPtr->hS);
1380 switch(which) {
1382 case WSDecrementLine:
1383 if (tPtr->hpos > 0) {
1384 if (tPtr->hpos>16) tPtr->hpos-=16;
1385 else tPtr->hpos=0;
1386 scroll=True;
1387 }break;
1389 case WSIncrementLine: {
1390 int limit = tPtr->docWidth - width;
1391 if (tPtr->hpos < limit) {
1392 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1393 else tPtr->hpos=limit;
1394 scroll = True;
1395 }}break;
1397 case WSDecrementPage:
1398 if(((int)tPtr->hpos - (int)width) >= 0)
1399 tPtr->hpos -= width;
1400 else
1401 tPtr->hpos = 0;
1403 scroll = True;
1404 break;
1406 case WSIncrementPage:
1407 tPtr->hpos += width;
1408 if (tPtr->hpos > (tPtr->docWidth - width))
1409 tPtr->hpos = tPtr->docWidth - width;
1410 scroll = True;
1411 break;
1414 case WSKnob:
1415 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1416 * (float)(tPtr->docWidth - width);
1417 scroll = True;
1418 break;
1420 case WSKnobSlot:
1421 case WSNoPart:
1422 break;
1424 scroll = (tPtr->hpos != tPtr->prevHpos);
1425 tPtr->prevHpos = tPtr->hpos;
1428 if (scroll) {
1429 updateScrollers(tPtr);
1430 paintText(tPtr);
1436 typedef struct {
1437 TextBlock *tb;
1438 unsigned short begin, end; /* what part of the text block */
1439 } myLineItems;
1442 static int
1443 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1445 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1446 WMFont *font;
1447 char *text;
1448 TextBlock *tb, *tbsame=NULL;
1450 if(!items || nitems == 0)
1451 return 0;
1453 for(i=0; i<nitems; i++) {
1454 tb = items[i].tb;
1456 if (tb->graphic) {
1457 if (!tPtr->flags.monoFont) {
1458 if(tb->object) {
1459 WMWidget *wdt = tb->d.widget;
1460 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1461 if (tPtr->flags.alignment != WALeft)
1462 lw += WMWidgetWidth(wdt);
1463 } else {
1464 line_height = WMAX(line_height,
1465 tb->d.pixmap->height + max_d);
1466 if (tPtr->flags.alignment != WALeft)
1467 lw += tb->d.pixmap->width;
1471 } else {
1472 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1473 max_d = WMAX(max_d, abs(font->height-font->y));
1474 line_height = WMAX(line_height, font->height + max_d);
1475 text = &(tb->text[items[i].begin]);
1476 len = items[i].end - items[i].begin;
1477 if (tPtr->flags.alignment != WALeft)
1478 lw += WMWidthOfString(font, text, len);
1482 if (tPtr->flags.alignment == WARight) {
1483 j = tPtr->visible.w - lw;
1484 } else if (tPtr->flags.alignment == WACenter) {
1485 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1488 for(i=0; i<nitems; i++) {
1489 tb = items[i].tb;
1491 if (tbsame == tb) { /* extend it, since it's on same line */
1492 tb->sections[tb->nsections-1].end = items[i].end;
1493 n = tb->nsections-1;
1494 } else {
1495 tb->sections = wrealloc(tb->sections,
1496 (++tb->nsections)*sizeof(Section));
1497 n = tb->nsections-1;
1498 tb->sections[n]._y = y + max_d;
1499 tb->sections[n].max_d = max_d;
1500 tb->sections[n].x = x+j;
1501 tb->sections[n].h = line_height;
1502 tb->sections[n].begin = items[i].begin;
1503 tb->sections[n].end = items[i].end;
1506 tb->sections[n].last = (i+1 == nitems);
1508 if (tb->graphic) {
1509 if (!tPtr->flags.monoFont) {
1510 if(tb->object) {
1511 WMWidget *wdt = tb->d.widget;
1512 tb->sections[n].y = max_d + y
1513 + line_height - WMWidgetHeight(wdt);
1514 tb->sections[n].w = WMWidgetWidth(wdt);
1515 } else {
1516 tb->sections[n].y = y + line_height
1517 + max_d - tb->d.pixmap->height;
1518 tb->sections[n].w = tb->d.pixmap->width;
1520 x += tb->sections[n].w;
1522 } else {
1523 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1524 len = items[i].end - items[i].begin;
1525 text = &(tb->text[items[i].begin]);
1527 tb->sections[n].y = y+line_height-font->y;
1528 tb->sections[n].w =
1529 WMWidthOfString(font,
1530 &(tb->text[tb->sections[n].begin]),
1531 tb->sections[n].end - tb->sections[n].begin);
1533 x += WMWidthOfString(font, text, len);
1536 tbsame = tb;
1539 return line_height;
1544 static void
1545 layOutDocument(Text *tPtr)
1547 TextBlock *tb;
1548 myLineItems *items = NULL;
1549 unsigned int itemsSize=0, nitems=0, begin, end;
1550 WMFont *font;
1551 unsigned int x, y=0, lw = 0, width=0, bmargin;
1552 char *start=NULL, *mark=NULL;
1554 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1555 return;
1557 tPtr->docWidth = tPtr->visible.w;
1558 x = tPtr->margins[tb->marginN].first;
1559 bmargin = tPtr->margins[tb->marginN].body;
1561 /* only partial layOut needed: re-Lay only affected textblocks */
1562 if (tPtr->flags.laidOut) {
1563 tb = tPtr->currentTextBlock;
1565 /* search backwards for textblocks on same line */
1566 while (tb->prior) {
1567 if (!tb->sections || tb->nsections<1) {
1568 tb = tPtr->firstTextBlock;
1569 tPtr->flags.laidOut = False;
1570 y = 0;
1571 goto _layOut;
1574 if(!tb->prior->sections || tb->prior->nsections<1) {
1575 tb = tPtr->firstTextBlock;
1576 tPtr->flags.laidOut = False;
1577 y = 0;
1578 goto _layOut;
1581 if (tb->sections[0]._y !=
1582 tb->prior->sections[tb->prior->nsections-1]._y) {
1583 break;
1585 tb = tb->prior;
1588 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1589 y = tb->prior->sections[tb->prior->nsections-1]._y +
1590 tb->prior->sections[tb->prior->nsections-1].h -
1591 tb->prior->sections[tb->prior->nsections-1].max_d;
1592 } else {
1593 y = 0;
1597 _layOut:
1598 while (tb) {
1600 if (tb->sections && tb->nsections>0) {
1601 wfree(tb->sections);
1602 tb->sections = NULL;
1603 tb->nsections = 0;
1606 if (tb->blank && tb->next && !tb->next->first) {
1607 TextBlock *next = tb->next;
1608 tPtr->currentTextBlock = tb;
1609 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1610 tb = next;
1611 tb->first = True;
1612 continue;
1615 if (tb->first && tb != tPtr->firstTextBlock) {
1616 y += layOutLine(tPtr, items, nitems, x, y);
1617 x = tPtr->margins[tb->marginN].first;
1618 bmargin = tPtr->margins[tb->marginN].body;
1619 nitems = 0;
1620 lw = 0;
1623 if (tb->graphic) {
1624 if (!tPtr->flags.monoFont) {
1625 if(tb->object)
1626 width = WMWidgetWidth(tb->d.widget);
1627 else
1628 width = tb->d.pixmap->width;
1630 if (width > tPtr->docWidth)
1631 tPtr->docWidth = width;
1633 lw += width;
1634 if (lw >= tPtr->visible.w - x ) {
1635 y += layOutLine(tPtr, items, nitems, x, y);
1636 nitems = 0;
1637 x = bmargin;
1638 lw = width;
1641 if(nitems + 1> itemsSize) {
1642 items = wrealloc(items,
1643 (++itemsSize)*sizeof(myLineItems));
1646 items[nitems].tb = tb;
1647 items[nitems].begin = 0;
1648 items[nitems].end = 0;
1649 nitems++;
1652 } else if ((start = tb->text)) {
1653 begin = end = 0;
1654 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1656 while (start) {
1657 mark = strchr(start, ' ');
1658 if (mark) {
1659 end += (int)(mark-start)+1;
1660 start = mark+1;
1661 } else {
1662 end += strlen(start);
1663 start = mark;
1666 if (end > tb->used)
1667 end = tb->used;
1669 if (end-begin > 0) {
1671 width = WMWidthOfString(font,
1672 &tb->text[begin], end-begin);
1674 /* if it won't fit, break it up */
1675 if (width > tPtr->visible.w) {
1676 char *t = &tb->text[begin];
1677 int l=end-begin, i=0;
1678 do {
1679 width = WMWidthOfString(font, t, ++i);
1680 } while (width < tPtr->visible.w && i < l);
1681 end = begin+i;
1682 if (start)
1683 start -= l-i;
1686 lw += width;
1689 if (lw >= tPtr->visible.w - x) {
1690 y += layOutLine(tPtr, items, nitems, x, y);
1691 lw = width;
1692 x = bmargin;
1693 nitems = 0;
1696 if(nitems + 1 > itemsSize) {
1697 items = wrealloc(items,
1698 (++itemsSize)*sizeof(myLineItems));
1701 items[nitems].tb = tb;
1702 items[nitems].begin = begin;
1703 items[nitems].end = end;
1704 nitems++;
1706 begin = end;
1711 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1712 if(0&&tPtr->flags.laidOut
1713 && tb->next && tb->next->sections && tb->next->nsections>0
1714 && (tPtr->vpos + tPtr->visible.h
1715 < tb->next->sections[0]._y)) {
1716 if(tPtr->lastTextBlock->sections
1717 && tPtr->lastTextBlock->nsections > 0 ) {
1718 TextBlock *ltb = tPtr->lastTextBlock;
1719 int ly = ltb->sections[ltb->nsections-1]._y;
1720 int lh = ltb->sections[ltb->nsections-1].h;
1721 int ss, sd;
1723 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1724 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1726 y += layOutLine(tPtr, items, nitems, x, y);
1727 ss= ly+lh-y;
1728 sd = tPtr->docHeight-y;
1730 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1731 y += tb->next->sections[0]._y-y;
1732 nitems = 0;
1733 printf("nitems%d\n", nitems);
1734 if(ss-sd!=0)
1735 y = tPtr->docHeight+ss-sd;
1737 break;
1738 } else {
1739 tPtr->flags.laidOut = False;
1743 tb = tb->next;
1747 if (nitems > 0)
1748 y += layOutLine(tPtr, items, nitems, x, y);
1750 if (tPtr->docHeight != y+10) {
1751 tPtr->docHeight = y+10;
1752 updateScrollers(tPtr);
1755 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1756 XEvent event;
1758 tPtr->flags.horizOnDemand = True;
1759 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1760 event.type = Expose;
1761 handleEvents(&event, (void *)tPtr);
1763 } else if(tPtr->docWidth <= tPtr->visible.w
1764 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1765 tPtr->flags.horizOnDemand = False;
1766 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1769 tPtr->flags.laidOut = True;
1771 if(items && itemsSize > 0)
1772 wfree(items);
1776 static void
1777 textDidResize(W_ViewDelegate *self, WMView *view)
1779 Text *tPtr = (Text *)view->self;
1780 unsigned short w = tPtr->view->size.width;
1781 unsigned short h = tPtr->view->size.height;
1782 unsigned short rh = 0, vw = 0, rel;
1784 rel = (tPtr->flags.relief == WRFlat);
1786 if (tPtr->ruler && tPtr->flags.rulerShown) {
1787 WMMoveWidget(tPtr->ruler, 2, 2);
1788 WMResizeWidget(tPtr->ruler, w - 4, 40);
1789 rh = 40;
1792 if (tPtr->vS) {
1793 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1794 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1795 vw = 20;
1796 WMSetRulerOffset(tPtr->ruler,22);
1797 } else WMSetRulerOffset(tPtr->ruler, 2);
1799 if (tPtr->hS) {
1800 if (tPtr->vS) {
1801 WMMoveWidget(tPtr->hS, vw, h - 21);
1802 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1803 } else {
1804 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1805 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1809 tPtr->visible.x = (tPtr->vS)?24:4;
1810 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1811 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1812 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1813 tPtr->visible.h -= (tPtr->hS)?20:0;
1814 tPtr->margins[0].right = tPtr->visible.w;
1816 if (tPtr->view->flags.realized) {
1818 if (tPtr->db) {
1819 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1820 tPtr->db = (Pixmap) NULL;
1823 if (tPtr->visible.w < 40)
1824 tPtr->visible.w = 40;
1825 if (tPtr->visible.h < 20)
1826 tPtr->visible.h = 20;
1828 if(!tPtr->db) {
1829 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1830 tPtr->view->window, tPtr->visible.w,
1831 tPtr->visible.h, tPtr->view->screen->depth);
1835 WMThawText(tPtr);
1838 W_ViewDelegate _TextViewDelegate =
1840 NULL,
1841 NULL,
1842 textDidResize,
1843 NULL,
1846 /* nice, divisble-by-16 blocks */
1847 static inline unsigned short
1848 reqBlockSize(unsigned short requested)
1850 return requested + 16 - (requested%16);
1854 static void
1855 clearText(Text *tPtr)
1857 tPtr->vpos = tPtr->hpos = 0;
1858 tPtr->docHeight = tPtr->docWidth = 0;
1860 if (!tPtr->firstTextBlock)
1861 return;
1863 while (tPtr->currentTextBlock)
1864 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1866 tPtr->firstTextBlock = NULL;
1867 tPtr->currentTextBlock = NULL;
1868 tPtr->lastTextBlock = NULL;
1869 WMEmptyArray(tPtr->gfxItems);
1872 static void
1873 deleteTextInteractively(Text *tPtr, KeySym ksym)
1875 TextBlock *tb;
1876 Bool back = (Bool) (ksym == XK_BackSpace);
1877 Bool done = 1;
1878 Bool wasFirst = 0;
1880 if (!tPtr->flags.editable) {
1881 XBell(tPtr->view->screen->display, 0);
1882 return;
1885 if ( !(tb = tPtr->currentTextBlock) )
1886 return;
1888 if (tPtr->flags.ownsSelection) {
1889 if(removeSelection(tPtr))
1890 layOutDocument(tPtr);
1891 return;
1894 wasFirst = tb->first;
1895 if (back && tPtr->tpos < 1) {
1896 if (tb->prior) {
1897 if(tb->prior->blank) {
1898 tPtr->currentTextBlock = tb->prior;
1899 WMRemoveTextBlock(tPtr);
1900 tPtr->currentTextBlock = tb;
1901 tb->first = True;
1902 layOutDocument(tPtr);
1903 return;
1904 } else {
1905 if(tb->blank) {
1906 TextBlock *prior = tb->prior;
1907 tPtr->currentTextBlock = tb;
1908 WMRemoveTextBlock(tPtr);
1909 tb = prior;
1910 } else {
1911 tb = tb->prior;
1914 tPtr->tpos = tb->used;
1915 tPtr->currentTextBlock = tb;
1916 done = 1;
1917 if(wasFirst) {
1918 if(tb->next)
1919 tb->next->first = False;
1920 layOutDocument(tPtr);
1921 return;
1927 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1928 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1929 if (back)
1930 tPtr->tpos--;
1931 memmove(&(tb->text[tPtr->tpos]),
1932 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1933 tb->used--;
1934 done = 0;
1937 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1938 || tb->graphic) {
1940 TextBlock *sibling = (back? tb->prior : tb->next);
1942 if(tb->used == 0 || tb->graphic)
1943 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1945 if (sibling) {
1946 tPtr->currentTextBlock = sibling;
1947 tPtr->tpos = (back? sibling->used : 0);
1951 layOutDocument(tPtr);
1955 static void
1956 insertTextInteractively(Text *tPtr, char *text, int len)
1958 TextBlock *tb;
1959 char *newline = NULL;
1961 if (!tPtr->flags.editable) {
1962 XBell(tPtr->view->screen->display, 0);
1963 return;
1966 if (len < 1 || !text)
1967 return;
1970 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1971 return;
1974 if (tPtr->flags.ownsSelection)
1975 removeSelection(tPtr);
1978 if (tPtr->flags.ignoreNewLine) {
1979 int i;
1980 for(i=0; i<len; i++) {
1981 if (text[i] == '\n')
1982 text[i] = ' ';
1986 tb = tPtr->currentTextBlock;
1987 if (!tb || tb->graphic) {
1988 tPtr->tpos = 0;
1989 WMAppendTextStream(tPtr, text);
1990 layOutDocument(tPtr);
1991 return;
1994 if ((newline = strchr(text, '\n'))) {
1995 int nlen = (int)(newline-text);
1996 int s = tb->used - tPtr->tpos;
1997 char save[s];
1998 if (!tb->blank && nlen>0) {
1999 if (s > 0) {
2000 memcpy(save, &tb->text[tPtr->tpos], s);
2001 tb->used -= (tb->used - tPtr->tpos);
2003 insertTextInteractively(tPtr, text, nlen);
2004 newline++;
2005 WMAppendTextStream(tPtr, newline);
2006 if (s>0)
2007 insertTextInteractively(tPtr, save, s);
2009 } else {
2010 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2011 && !tb->graphic && tb->text) {
2013 void *ntb = WMCreateTextBlockWithText(
2014 tPtr, &tb->text[tPtr->tpos],
2015 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2016 tb->used = tPtr->tpos;
2017 WMAppendTextBlock(tPtr, ntb);
2018 tPtr->tpos = 0;
2020 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2021 if(tPtr->flags.indentNewLine) {
2022 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2023 " ", tb->d.font, tb->color, True, 4));
2024 tPtr->tpos = 4;
2025 } else {
2026 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2027 NULL, tb->d.font, tb->color, True, 0));
2028 tPtr->tpos = 0;
2033 } else {
2034 if (tb->used + len >= tb->allocated) {
2035 tb->allocated = reqBlockSize(tb->used+len);
2036 tb->text = wrealloc(tb->text, tb->allocated);
2039 if (tb->blank) {
2040 memcpy(tb->text, text, len);
2041 tb->used = len;
2042 tPtr->tpos = len;
2043 tb->text[tb->used] = 0;
2044 tb->blank = False;
2046 } else {
2047 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2048 tb->used-tPtr->tpos+1);
2049 memmove(&tb->text[tPtr->tpos], text, len);
2050 tb->used += len;
2051 tPtr->tpos += len;
2052 tb->text[tb->used] = 0;
2057 layOutDocument(tPtr);
2061 static void
2062 selectRegion(Text *tPtr, int x, int y)
2065 if (x < 0 || y < 0)
2066 return;
2068 y += (tPtr->flags.rulerShown? 40: 0);
2069 y += tPtr->vpos;
2070 if (y>10)
2071 y -= 10; /* the original offset */
2073 x -= tPtr->visible.x-2;
2074 if (x<0)
2075 x=0;
2077 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2078 tPtr->sel.w = abs(tPtr->clicked.x - x);
2079 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2080 tPtr->sel.h = abs(tPtr->clicked.y - y);
2082 tPtr->flags.ownsSelection = True;
2083 paintText(tPtr);
2087 static void
2088 releaseSelection(Text *tPtr)
2090 TextBlock *tb = tPtr->firstTextBlock;
2092 while(tb) {
2093 tb->selected = False;
2094 tb = tb->next;
2096 tPtr->flags.ownsSelection = False;
2097 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2098 CurrentTime);
2100 paintText(tPtr);
2104 WMData*
2105 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2106 Atom *type)
2108 Text *tPtr = view->self;
2109 Display *dpy = tPtr->view->screen->display;
2110 Atom _TARGETS;
2111 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2112 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2113 WMData *data = NULL;
2116 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2117 char *text = WMGetTextSelectedStream(tPtr);
2119 if (text) {
2120 printf("got text [%s]\n", text);
2121 data = WMCreateDataWithBytes(text, strlen(text));
2122 WMSetDataFormat(data, TYPETEXT);
2124 *type = target;
2125 return data;
2126 } else printf("didn't get it\n");
2128 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2129 if (target == _TARGETS) {
2130 Atom *ptr;
2132 ptr = wmalloc(4 * sizeof(Atom));
2133 ptr[0] = _TARGETS;
2134 ptr[1] = XA_STRING;
2135 ptr[2] = TEXT;
2136 ptr[3] = COMPOUND_TEXT;
2138 data = WMCreateDataWithBytes(ptr, 4*4);
2139 WMSetDataFormat(data, 32);
2141 *type = target;
2142 return data;
2145 return NULL;
2148 static void
2149 lostHandler(WMView *view, Atom selection, void *cdata)
2151 releaseSelection((WMText *)view->self);
2154 static WMSelectionProcs selectionHandler = {
2155 requestHandler, lostHandler, NULL
2159 static void
2160 ownershipObserver(void *observerData, WMNotification *notification)
2162 if (observerData != WMGetNotificationClientData(notification))
2163 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2167 static void
2168 fontChanged(void *observerData, WMNotification *notification)
2170 WMText *tPtr = (WMText *) observerData;
2171 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2172 printf("fontChanged\n");
2174 if(!tPtr || !font)
2175 return;
2177 if (tPtr->flags.ownsSelection)
2178 WMSetTextSelectionFont(tPtr, font);
2182 static void
2183 handleTextKeyPress(Text *tPtr, XEvent *event)
2185 char buffer[2];
2186 KeySym ksym;
2187 int control_pressed = False;
2188 TextBlock *tb = NULL;
2190 if (((XKeyEvent *) event)->state & ControlMask)
2191 control_pressed = True;
2192 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2194 switch(ksym) {
2196 case XK_Left:
2197 if(!(tb = tPtr->currentTextBlock))
2198 break;
2199 if(tb->graphic)
2200 goto L_imaGFX;
2202 if(tPtr->tpos==0) {
2203 L_imaGFX: if(tb->prior) {
2204 tPtr->currentTextBlock = tb->prior;
2205 tPtr->tpos = tPtr->currentTextBlock->used -1;
2206 } else tPtr->tpos = 0;
2207 } else tPtr->tpos--;
2208 updateCursorPosition(tPtr);
2209 paintText(tPtr);
2210 break;
2212 case XK_Right:
2213 if(!(tb = tPtr->currentTextBlock))
2214 break;
2215 if(tb->graphic)
2216 goto R_imaGFX;
2217 if(tPtr->tpos == tb->used) {
2218 R_imaGFX: if(tb->next) {
2219 tPtr->currentTextBlock = tb->next;
2220 tPtr->tpos = 1;
2221 } else tPtr->tpos = tb->used;
2222 } else tPtr->tpos++;
2223 updateCursorPosition(tPtr);
2224 paintText(tPtr);
2225 break;
2227 case XK_Down:
2228 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2229 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2230 paintText(tPtr);
2231 break;
2233 case XK_Up:
2234 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2235 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2236 paintText(tPtr);
2237 break;
2239 case XK_BackSpace:
2240 case XK_Delete:
2241 case XK_KP_Delete:
2242 deleteTextInteractively(tPtr, ksym);
2243 updateCursorPosition(tPtr);
2244 paintText(tPtr);
2245 break;
2247 case XK_Control_R :
2248 case XK_Control_L :
2249 control_pressed = True;
2250 break;
2252 case XK_Tab:
2253 insertTextInteractively(tPtr, " ", 4);
2254 updateCursorPosition(tPtr);
2255 paintText(tPtr);
2256 break;
2258 case XK_Return:
2259 buffer[0] = '\n';
2260 default:
2261 if (buffer[0] != 0 && !control_pressed) {
2262 insertTextInteractively(tPtr, buffer, 1);
2263 updateCursorPosition(tPtr);
2264 paintText(tPtr);
2266 } else if (control_pressed && ksym==XK_r) {
2267 Bool i = !tPtr->flags.rulerShown;
2268 WMShowTextRuler(tPtr, i);
2269 tPtr->flags.rulerShown = i;
2271 else if (control_pressed && buffer[0] == '\a')
2272 XBell(tPtr->view->screen->display, 0);
2275 if (!control_pressed && tPtr->flags.ownsSelection)
2276 ;//releaseSelection(tPtr);
2279 static void
2280 handleWidgetPress(XEvent *event, void *data)
2282 TextBlock *tb = (TextBlock *)data;
2283 Text *tPtr;
2284 WMWidget *w;
2286 if (!tb)
2287 return;
2289 tPtr = (Text*)w;
2290 tPtr->currentTextBlock = tb;
2291 tPtr->flags.isOverGraphic = 2;
2292 tPtr->tpos = 0;
2293 output(tb->text, tb->used);
2294 #if 0
2295 if (!tPtr->flags.focused) {
2296 WMSetFocusToWidget(tPtr);
2297 tPtr->flags.focused = True;
2299 #endif
2303 static void
2304 handleActionEvents(XEvent *event, void *data)
2306 Text *tPtr = (Text *)data;
2307 Display *dpy = event->xany.display;
2308 KeySym ksym;
2311 switch (event->type) {
2312 case KeyPress:
2313 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2314 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2315 tPtr->flags.extendSelection = True;
2316 return;
2319 if (tPtr->flags.focused) {
2320 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2321 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2322 GrabModeAsync, GrabModeAsync, None,
2323 tPtr->view->screen->invisibleCursor, CurrentTime);
2324 tPtr->flags.pointerGrabbed = True;
2325 handleTextKeyPress(tPtr, event);
2327 } break;
2329 case KeyRelease:
2330 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2331 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2332 tPtr->flags.extendSelection = False;
2333 return;
2334 /* end modify flag so selection can be extended */
2336 break;
2339 case MotionNotify:
2341 if (tPtr->flags.pointerGrabbed) {
2342 tPtr->flags.pointerGrabbed = False;
2343 XUngrabPointer(dpy, CurrentTime);
2346 if(tPtr->flags.waitingForSelection)
2347 break;
2349 if ((event->xmotion.state & Button1Mask)) {
2350 if (!tPtr->flags.ownsSelection) {
2351 WMCreateSelectionHandler(tPtr->view,
2352 XA_PRIMARY, event->xbutton.time,
2353 &selectionHandler, NULL);
2354 tPtr->flags.ownsSelection = True;
2356 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2357 break;
2360 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2361 break;
2364 case ButtonPress:
2366 if (tPtr->flags.pointerGrabbed) {
2367 tPtr->flags.pointerGrabbed = False;
2368 XUngrabPointer(dpy, CurrentTime);
2369 break;
2372 if (tPtr->flags.waitingForSelection)
2373 break;
2375 if (tPtr->flags.extendSelection) {
2376 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2377 return;
2381 if (event->xbutton.button == Button1) {
2383 if(WMIsDoubleClick(event)) {
2384 TextBlock *tb = tPtr->currentTextBlock;
2386 if(tb && tb->graphic && !tb->object) {
2387 char desc[tb->used+1];
2388 memcpy(desc, tb->text, tb->used);
2389 desc[tb->used] = 0;
2390 if(tPtr->delegate) {
2391 if(tPtr->delegate->didDoubleClickOnPicture)
2392 (*tPtr->delegate->didDoubleClickOnPicture)
2393 (tPtr->delegate, desc);
2395 } else {
2396 autoSelectText(tPtr, 2);
2398 tPtr->lastClickTime = event->xbutton.time;
2399 break;
2400 } else if(event->xbutton.time - tPtr->lastClickTime
2401 < WINGsConfiguration.doubleClickDelay) {
2402 autoSelectText(tPtr, 3);
2403 break;
2406 if (!tPtr->flags.focused) {
2407 WMSetFocusToWidget(tPtr);
2408 tPtr->flags.focused = True;
2411 if (tPtr->flags.ownsSelection)
2412 releaseSelection(tPtr);
2414 tPtr->lastClickTime = event->xbutton.time;
2415 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2416 paintText(tPtr);
2419 if (event->xbutton.button
2420 == WINGsConfiguration.mouseWheelDown) {
2421 WMScrollText(tPtr, 16);
2422 break;
2425 if (event->xbutton.button
2426 == WINGsConfiguration.mouseWheelUp) {
2427 WMScrollText(tPtr, -16);
2428 break;
2431 if (event->xbutton.button == Button2) {
2432 char *text = NULL;
2433 int n;
2435 if (!tPtr->flags.editable) {
2436 XBell(dpy, 0);
2437 break;
2440 #if 0
2441 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2442 event->xbutton.time, pasteText, NULL)) {
2443 #endif
2446 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2448 if (text) {
2449 text[n] = 0;
2451 if (tPtr->parser)
2452 (tPtr->parser) (tPtr, (void *) text);
2453 else
2454 insertTextInteractively(tPtr, text, n);
2456 XFree(text);
2457 #if 0
2458 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2459 (void*)WMInsertTextEvent);
2460 #endif
2462 } else {
2463 tPtr->flags.waitingForSelection = True;
2466 break;
2470 case ButtonRelease:
2472 if (tPtr->flags.pointerGrabbed) {
2473 tPtr->flags.pointerGrabbed = False;
2474 XUngrabPointer(dpy, CurrentTime);
2475 break;
2478 if (tPtr->flags.waitingForSelection)
2479 break;
2485 static void
2486 handleEvents(XEvent *event, void *data)
2488 Text *tPtr = (Text *)data;
2490 switch(event->type) {
2491 case Expose:
2493 if (event->xexpose.count!=0)
2494 break;
2496 if(tPtr->hS) {
2497 if (!(W_VIEW(tPtr->hS))->flags.realized)
2498 WMRealizeWidget(tPtr->hS);
2501 if(tPtr->vS) {
2502 if (!(W_VIEW(tPtr->vS))->flags.realized)
2503 WMRealizeWidget(tPtr->vS);
2506 if(tPtr->ruler) {
2507 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2508 WMRealizeWidget(tPtr->ruler);
2512 if(!tPtr->db)
2513 textDidResize(tPtr->view->delegate, tPtr->view);
2515 paintText(tPtr);
2516 break;
2518 case FocusIn:
2519 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2520 != tPtr->view)
2521 return;
2522 tPtr->flags.focused = True;
2523 #if DO_BLINK
2524 if (tPtr->flags.editable && !tPtr->timerID) {
2525 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2526 blinkCursor, tPtr);
2528 #endif
2530 break;
2532 case FocusOut:
2533 tPtr->flags.focused = False;
2534 paintText(tPtr);
2535 #if DO_BLINK
2536 if (tPtr->timerID) {
2537 WMDeleteTimerHandler(tPtr->timerID);
2538 tPtr->timerID = NULL;
2540 #endif
2541 break;
2544 case DestroyNotify:
2545 clearText(tPtr);
2546 if(tPtr->db)
2547 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2548 if(tPtr->gfxItems)
2549 WMEmptyArray(tPtr->gfxItems);
2550 #if DO_BLINK
2551 if (tPtr->timerID)
2552 WMDeleteTimerHandler(tPtr->timerID);
2553 #endif
2554 WMReleaseFont(tPtr->dFont);
2555 WMReleaseColor(tPtr->dColor);
2556 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2557 WMRemoveNotificationObserver(tPtr);
2559 wfree(tPtr);
2561 break;
2567 static void
2568 insertPlainText(Text *tPtr, char *text)
2570 char *start, *mark;
2571 void *tb = NULL;
2573 start = text;
2574 while (start) {
2575 mark = strchr(start, '\n');
2576 if (mark) {
2577 tb = WMCreateTextBlockWithText(tPtr,
2578 start, tPtr->dFont,
2579 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2580 start = mark+1;
2581 tPtr->flags.first = True;
2582 } else {
2583 if (start && strlen(start)) {
2584 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2585 tPtr->dColor, tPtr->flags.first, strlen(start));
2586 } else tb = NULL;
2587 tPtr->flags.first = False;
2588 start = mark;
2591 if (tPtr->flags.prepend)
2592 WMPrependTextBlock(tPtr, tb);
2593 else
2594 WMAppendTextBlock(tPtr, tb);
2596 return;
2601 static void
2602 rulerMoveCallBack(WMWidget *w, void *self)
2604 Text *tPtr = (Text *)self;
2605 if (!tPtr)
2606 return;
2607 if (W_CLASS(tPtr) != WC_Text)
2608 return;
2610 paintText(tPtr);
2614 static void
2615 rulerReleaseCallBack(WMWidget *w, void *self)
2617 Text *tPtr = (Text *)self;
2618 if (!tPtr)
2619 return;
2620 if (W_CLASS(tPtr) != WC_Text)
2621 return;
2623 WMThawText(tPtr);
2624 return;
2628 static unsigned
2629 draggingEntered(WMView *self, WMDraggingInfo *info)
2631 printf("draggingEntered\n");
2632 return WDOperationCopy;
2636 static unsigned
2637 draggingUpdated(WMView *self, WMDraggingInfo *info)
2639 return WDOperationCopy;
2643 static void
2644 draggingExited(WMView *self, WMDraggingInfo *info)
2646 printf("draggingExited\n");
2649 static Bool
2650 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2652 printf("prepareForDragOperation\n");
2653 return True;
2657 char *badbadbad;
2659 static void
2660 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2661 void *cdata, WMData *data)
2663 badbadbad = wstrdup((char *)WMDataBytes(data));
2667 /* when it's done in WINGs, remove this */
2669 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2671 WMScreen *scr = W_VIEW_SCREEN(view);
2673 if (!WMRequestSelection(scr->dragInfo.destView,
2674 scr->xdndSelectionAtom,
2675 XInternAtom(scr->display, type, False),
2676 scr->dragInfo.timestamp,
2677 receivedData, &scr->dragInfo)) {
2678 wwarning("could not request data for dropped data");
2683 XEvent ev;
2685 ev.type = ClientMessage;
2686 ev.xclient.message_type = scr->xdndFinishedAtom;
2687 ev.xclient.format = 32;
2688 ev.xclient.window = info->destinationWindow;
2689 ev.xclient.data.l[0] = 0;
2690 ev.xclient.data.l[1] = 0;
2691 ev.xclient.data.l[2] = 0;
2692 ev.xclient.data.l[3] = 0;
2693 ev.xclient.data.l[4] = 0;
2695 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2696 XFlush(scr->display);
2698 return True;
2701 static Bool
2702 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2704 WMColor *color;
2705 WMText *tPtr = (WMText *)self->self;
2707 if (!tPtr)
2708 return True;
2710 requestDroppedData(tPtr->view, info, "application/X-color");
2711 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2712 if(color) {
2713 WMSetTextSelectionColor(tPtr, color);
2714 WMReleaseColor(color);
2719 return True;
2722 static void
2723 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2725 printf("concludeDragOperation\n");
2729 static WMDragDestinationProcs _DragDestinationProcs = {
2730 draggingEntered,
2731 draggingUpdated,
2732 draggingExited,
2733 prepareForDragOperation,
2734 performDragOperation,
2735 concludeDragOperation
2739 char *
2740 getStream(WMText *tPtr, int sel, int array)
2742 TextBlock *tb = NULL;
2743 char *text = NULL;
2744 unsigned long where = 0;
2746 if (!tPtr)
2747 return NULL;
2749 if (!(tb = tPtr->firstTextBlock))
2750 return NULL;
2752 if (tPtr->writer) {
2753 (tPtr->writer) (tPtr, (void *) text);
2754 return text;
2757 tb = tPtr->firstTextBlock;
2758 while (tb) {
2760 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2762 if (!sel || (tb->graphic && tb->selected)) {
2764 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2765 && tb != tPtr->firstTextBlock) {
2766 text = wrealloc(text, where+1);
2767 text[where++] = '\n';
2770 if(tb->blank)
2771 goto _gSnext;
2773 if(tb->graphic && array) {
2774 text = wrealloc(text, where+4);
2775 text[where++] = 0xFA;
2776 text[where++] = (tb->used>>8)&0x0ff;
2777 text[where++] = tb->used&0x0ff;
2778 text[where++] = tb->allocated; /* extra info */
2780 text = wrealloc(text, where+tb->used);
2781 memcpy(&text[where], tb->text, tb->used);
2782 where += tb->used;
2785 } else if (sel && tb->selected) {
2787 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2788 && tb != tPtr->firstTextBlock) {
2789 text = wrealloc(text, where+1);
2790 text[where++] = '\n';
2793 if(tb->blank)
2794 goto _gSnext;
2796 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2797 memcpy(&text[where], &tb->text[tb->s_begin],
2798 tb->s_end - tb->s_begin);
2799 where += tb->s_end - tb->s_begin;
2804 _gSnext:tb = tb->next;
2807 /* +1 for the end of string, let's be nice */
2808 text = wrealloc(text, where+1);
2809 text[where] = 0;
2810 return text;
2814 static void
2815 releaseStreamObjects(void *data)
2817 if(data)
2818 wfree(data);
2821 WMArray *
2822 getStreamObjects(WMText *tPtr, int sel)
2824 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2825 WMData *data;
2826 char *stream;
2827 unsigned short len;
2828 char *start, *fa, *desc;
2830 stream = getStream(tPtr, sel, 1);
2831 if(!stream)
2832 return NULL;
2834 start = stream;
2835 while (start) {
2837 fa = strchr(start, 0xFA);
2838 if (fa) {
2839 if((int)(fa - start)>0) {
2840 desc = start;
2841 desc[(int)(fa - start)] = 0;
2842 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2843 WMSetDataFormat(data, TYPETEXT);
2844 WMAddToArray(array, (void *) data);
2847 len = *(fa+1)*0xff + *(fa+2);
2848 data = WMCreateDataWithBytes((void *)(fa+4), len);
2849 WMSetDataFormat(data, *(fa+3));
2850 WMAddToArray(array, (void *) data);
2851 start = fa + len + 4;
2853 } else {
2854 if (start && strlen(start)) {
2855 data = WMCreateDataWithBytes((void *)start, strlen(start));
2856 WMSetDataFormat(data, TYPETEXT);
2857 WMAddToArray(array, (void *) data);
2859 start = fa;
2863 wfree(stream);
2864 return array;
2865 WMFreeArray(array);
2869 WMText *
2870 WMCreateTextForDocumentType(WMWidget *parent,
2871 WMAction *parser, WMAction *writer)
2873 Text *tPtr = wmalloc(sizeof(Text));
2874 if (!tPtr) {
2875 printf("could not create text widget\n");
2876 return NULL;
2880 memset(tPtr, 0, sizeof(Text));
2881 tPtr->widgetClass = WC_Text;
2882 tPtr->view = W_CreateView(W_VIEW(parent));
2883 if (!tPtr->view) {
2884 perror("could not create text's view\n");
2885 wfree(tPtr);
2886 return NULL;
2888 tPtr->view->self = tPtr;
2889 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2890 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2891 W_ResizeView(tPtr->view, 250, 200);
2893 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2894 tPtr->bgGC = WMColorGC(tPtr->dColor);
2895 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2896 WMReleaseColor(tPtr->dColor);
2898 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2899 tPtr->fgGC = WMColorGC(tPtr->dColor);
2901 tPtr->ruler = NULL;
2902 tPtr->vS = NULL;
2903 tPtr->hS = NULL;
2905 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2907 tPtr->view->delegate = &_TextViewDelegate;
2909 tPtr->delegate = NULL;
2911 #if DO_BLINK
2912 tPtr->timerID = NULL;
2913 #endif
2915 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2916 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2917 handleEvents, tPtr);
2919 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2920 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2921 handleActionEvents, tPtr);
2923 WMAddNotificationObserver(ownershipObserver, tPtr,
2924 "_lostOwnership", tPtr);
2926 if(0){
2927 char *types[2] = {"application/X-color", NULL};
2928 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2929 WMRegisterViewForDraggedTypes(tPtr->view, types);
2932 WMAddNotificationObserver(fontChanged, tPtr,
2933 "WMFontPanelDidChangeNotification", tPtr);
2935 tPtr->firstTextBlock = NULL;
2936 tPtr->lastTextBlock = NULL;
2937 tPtr->currentTextBlock = NULL;
2938 tPtr->tpos = 0;
2940 tPtr->gfxItems = WMCreateArray(4);
2942 tPtr->parser = parser;
2943 tPtr->writer = writer;
2945 tPtr->sel.x = tPtr->sel.y = 2;
2946 tPtr->sel.w = tPtr->sel.h = 0;
2948 tPtr->clicked.x = tPtr->clicked.y = 2;
2950 tPtr->visible.x = tPtr->visible.y = 2;
2951 tPtr->visible.h = tPtr->view->size.height;
2952 tPtr->visible.w = tPtr->view->size.width - 4;
2954 tPtr->cursor.x = -23;
2956 tPtr->docWidth = 0;
2957 tPtr->docHeight = 0;
2958 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2959 default_bullet);
2960 tPtr->db = (Pixmap) NULL;
2962 tPtr->margins = WMGetRulerMargins(NULL);
2963 tPtr->margins->right = tPtr->visible.w;
2964 tPtr->nMargins = 1;
2966 tPtr->flags.rulerShown = False;
2967 tPtr->flags.monoFont = False;
2968 tPtr->flags.focused = False;
2969 tPtr->flags.editable = True;
2970 tPtr->flags.ownsSelection = False;
2971 tPtr->flags.pointerGrabbed = False;
2972 tPtr->flags.extendSelection = False;
2973 tPtr->flags.frozen = False;
2974 tPtr->flags.cursorShown = True;
2975 tPtr->flags.acceptsGraphic = False;
2976 tPtr->flags.horizOnDemand = False;
2977 tPtr->flags.needsLayOut = False;
2978 tPtr->flags.ignoreNewLine = False;
2979 tPtr->flags.indentNewLine = False;
2980 tPtr->flags.laidOut = False;
2981 tPtr->flags.waitingForSelection = False;
2982 tPtr->flags.prepend = False;
2983 tPtr->flags.isOverGraphic = False;
2984 tPtr->flags.relief = WRSunken;
2985 tPtr->flags.isOverGraphic = 0;
2986 tPtr->flags.alignment = WALeft;
2987 tPtr->flags.first = True;
2989 return tPtr;
2992 void
2993 WMPrependTextStream(WMText *tPtr, char *text)
2995 CHECK_CLASS(tPtr, WC_Text);
2997 if(!text) {
2998 if(tPtr->flags.ownsSelection)
2999 releaseSelection(tPtr);
3000 else {
3001 clearText(tPtr);
3002 updateScrollers(tPtr);
3004 return;
3007 tPtr->flags.prepend = True;
3008 if (text && tPtr->parser)
3009 (tPtr->parser) (tPtr, (void *) text);
3010 else
3011 insertPlainText(tPtr, text);
3013 tPtr->flags.needsLayOut = True;
3014 tPtr->tpos = 0;
3018 void
3019 WMAppendTextStream(WMText *tPtr, char *text)
3021 CHECK_CLASS(tPtr, WC_Text);
3023 if(!text) {
3024 if(tPtr->flags.ownsSelection)
3025 releaseSelection(tPtr);
3026 else {
3027 clearText(tPtr);
3028 updateScrollers(tPtr);
3030 return;
3033 tPtr->flags.prepend = False;
3034 if (text && tPtr->parser)
3035 (tPtr->parser) (tPtr, (void *) text);
3036 else
3037 insertPlainText(tPtr, text);
3039 tPtr->flags.needsLayOut = True;
3040 if(tPtr->currentTextBlock)
3041 tPtr->tpos = tPtr->currentTextBlock->used;
3047 char *
3048 WMGetTextStream(WMText *tPtr)
3050 CHECK_CLASS(tPtr, WC_Text);
3051 return getStream(tPtr, 0, 0);
3054 char *
3055 WMGetTextSelectedStream(WMText *tPtr)
3057 CHECK_CLASS(tPtr, WC_Text);
3058 return getStream(tPtr, 1, 0);
3061 WMArray *
3062 WMGetTextObjects(WMText *tPtr)
3064 CHECK_CLASS(tPtr, WC_Text);
3065 return getStreamObjects(tPtr, 0);
3068 WMArray *
3069 WMGetTextSelectedObjects(WMText *tPtr)
3071 CHECK_CLASS(tPtr, WC_Text);
3072 return getStreamObjects(tPtr, 1);
3076 void
3077 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3079 CHECK_CLASS(tPtr, WC_Text);
3081 tPtr->delegate = delegate;
3085 void *
3086 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3087 char *description, WMColor *color,
3088 unsigned short first, unsigned short extraInfo)
3090 TextBlock *tb;
3092 if (!w || !description || !color)
3093 return NULL;
3095 tb = wmalloc(sizeof(TextBlock));
3096 if (!tb)
3097 return NULL;
3099 tb->text = wstrdup(description);
3100 tb->used = strlen(description);
3101 tb->blank = False;
3102 tb->d.widget = w;
3103 tb->color = WMRetainColor(color);
3104 tb->marginN = newMargin(tPtr, NULL);
3105 tb->allocated = extraInfo;
3106 tb->first = first;
3107 tb->kanji = False;
3108 tb->graphic = True;
3109 tb->object = True;
3110 tb->underlined = False;
3111 tb->selected = False;
3112 tb->script = 0;
3113 tb->sections = NULL;
3114 tb->nsections = 0;
3115 tb->prior = NULL;
3116 tb->next = NULL;
3118 return tb;
3122 void *
3123 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3124 char *description, WMColor *color,
3125 unsigned short first, unsigned short extraInfo)
3127 TextBlock *tb;
3129 if (!p || !description || !color)
3130 return NULL;
3132 tb = wmalloc(sizeof(TextBlock));
3133 if (!tb)
3134 return NULL;
3136 tb->text = wstrdup(description);
3137 tb->used = strlen(description);
3138 tb->blank = False;
3139 tb->d.pixmap = WMRetainPixmap(p);
3140 tb->color = WMRetainColor(color);
3141 tb->marginN = newMargin(tPtr, NULL);
3142 tb->allocated = extraInfo;
3143 tb->first = first;
3144 tb->kanji = False;
3145 tb->graphic = True;
3146 tb->object = False;
3147 tb->underlined = False;
3148 tb->selected = False;
3149 tb->script = 0;
3150 tb->sections = NULL;
3151 tb->nsections = 0;
3152 tb->prior = NULL;
3153 tb->next = NULL;
3155 return tb;
3158 void *
3159 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3160 unsigned short first, unsigned short len)
3162 TextBlock *tb;
3164 if (!font || !color)
3165 return NULL;
3167 tb = wmalloc(sizeof(TextBlock));
3168 if (!tb)
3169 return NULL;
3171 tb->allocated = reqBlockSize(len);
3172 tb->text = (char *)wmalloc(tb->allocated);
3173 memset(tb->text, 0, tb->allocated);
3175 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3176 *tb->text = ' ';
3177 tb->used = 1;
3178 tb->blank = True;
3179 } else {
3180 memcpy(tb->text, text, len);
3181 tb->used = len;
3182 tb->blank = False;
3184 tb->text[tb->used] = 0;
3186 tb->d.font = WMRetainFont(font);
3187 tb->color = WMRetainColor(color);
3188 tb->marginN = newMargin(tPtr, NULL);
3189 tb->first = first;
3190 tb->kanji = False;
3191 tb->graphic = False;
3192 tb->underlined = False;
3193 tb->selected = False;
3194 tb->script = 0;
3195 tb->sections = NULL;
3196 tb->nsections = 0;
3197 tb->prior = NULL;
3198 tb->next = NULL;
3199 return tb;
3202 void
3203 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3204 unsigned int kanji, unsigned int underlined, int script,
3205 WMRulerMargins *margins)
3207 TextBlock *tb = (TextBlock *) vtb;
3208 if (!tb)
3209 return;
3211 tb->first = first;
3212 tb->kanji = kanji;
3213 tb->underlined = underlined;
3214 tb->script = script;
3215 tb->marginN = newMargin(tPtr, margins);
3218 void
3219 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3220 unsigned int *kanji, unsigned int *underlined, int *script,
3221 WMRulerMargins *margins)
3223 TextBlock *tb = (TextBlock *) vtb;
3224 if (!tb)
3225 return;
3227 if (first) *first = tb->first;
3228 if (kanji) *kanji = tb->kanji;
3229 if (underlined) *underlined = tb->underlined;
3230 if (script) *script = tb->script;
3231 if (margins) margins = &tPtr->margins[tb->marginN];
3236 void
3237 WMPrependTextBlock(WMText *tPtr, void *vtb)
3239 TextBlock *tb = (TextBlock *)vtb;
3241 if (!tPtr || !tb)
3242 return;
3244 if (tb->graphic) {
3245 if(tb->object) {
3246 WMWidget *w = tb->d.widget;
3247 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3248 handleWidgetPress, tb);
3249 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3250 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3251 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3254 WMAddToArray(tPtr->gfxItems, (void *)tb);
3255 tPtr->tpos = 0;
3256 } else tPtr->tpos = tb->used;
3258 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3259 tb->next = tb->prior = NULL;
3260 tb->first = True;
3261 tPtr->lastTextBlock = tPtr->firstTextBlock
3262 = tPtr->currentTextBlock = tb;
3263 return;
3266 if(!tb->first) {
3267 tb->marginN = tPtr->currentTextBlock->marginN;
3270 tb->next = tPtr->currentTextBlock;
3271 tb->prior = tPtr->currentTextBlock->prior;
3272 if (tPtr->currentTextBlock->prior)
3273 tPtr->currentTextBlock->prior->next = tb;
3275 tPtr->currentTextBlock->prior = tb;
3276 if (!tb->prior)
3277 tPtr->firstTextBlock = tb;
3279 tPtr->currentTextBlock = tb;
3283 void
3284 WMAppendTextBlock(WMText *tPtr, void *vtb)
3286 TextBlock *tb = (TextBlock *)vtb;
3288 if (!tPtr || !tb)
3289 return;
3291 if (tb->graphic) {
3292 if(tb->object) {
3293 WMWidget *w = tb->d.widget;
3294 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3295 handleWidgetPress, tb);
3296 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3297 (W_VIEW(w))->attribs.cursor =
3298 tPtr->view->screen->defaultCursor;
3299 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3302 WMAddToArray(tPtr->gfxItems, (void *)tb);
3303 tPtr->tpos = 0;
3304 } else tPtr->tpos = tb->used;
3306 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3307 tb->next = tb->prior = NULL;
3308 tb->first = True;
3309 tPtr->lastTextBlock = tPtr->firstTextBlock
3310 = tPtr->currentTextBlock = tb;
3311 return;
3314 if(!tb->first) {
3315 tb->marginN = tPtr->currentTextBlock->marginN;
3318 tb->next = tPtr->currentTextBlock->next;
3319 tb->prior = tPtr->currentTextBlock;
3320 if (tPtr->currentTextBlock->next)
3321 tPtr->currentTextBlock->next->prior = tb;
3323 tPtr->currentTextBlock->next = tb;
3325 if (!tb->next)
3326 tPtr->lastTextBlock = tb;
3328 tPtr->currentTextBlock = tb;
3331 void *
3332 WMRemoveTextBlock(WMText *tPtr)
3334 TextBlock *tb = NULL;
3336 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3337 || !tPtr->currentTextBlock) {
3338 printf("cannot remove non existent TextBlock!\b");
3339 return NULL;
3342 tb = tPtr->currentTextBlock;
3343 if (tb->graphic) {
3344 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3346 if(tb->object) {
3347 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3348 handleWidgetPress, tb);
3349 WMUnmapWidget(tb->d.widget);
3353 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3354 if (tPtr->currentTextBlock->next)
3355 tPtr->currentTextBlock->next->prior = NULL;
3357 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3358 tPtr->currentTextBlock = tPtr->firstTextBlock;
3360 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3361 tPtr->currentTextBlock->prior->next = NULL;
3362 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3363 tPtr->currentTextBlock = tPtr->lastTextBlock;
3364 } else {
3365 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3366 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3367 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3370 return (void *)tb;
3373 void
3374 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3376 TextBlock *tb = (TextBlock *)vtb;
3377 if (!tPtr || !tb)
3378 return;
3380 if (tb->graphic) {
3381 if(tb->object) {
3382 /* naturally, there's a danger to destroying
3383 widgets whose action brings us here:
3384 ie. press a button to destroy it... need to
3385 find a safer way. till then... this stays commented out */
3386 /* WMDestroyWidget(tb->d.widget);
3387 wfree(tb->d.widget); */
3388 tb->d.widget = NULL;
3389 } else {
3390 WMReleasePixmap(tb->d.pixmap);
3391 tb->d.pixmap = NULL;
3393 } else {
3394 WMReleaseFont(tb->d.font);
3397 WMReleaseColor(tb->color);
3398 if (tb->sections && tb->nsections > 0)
3399 wfree(tb->sections);
3400 wfree(tb->text);
3401 wfree(tb);
3402 tb = NULL;
3407 void
3408 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3410 if (!tPtr)
3411 return;
3413 if (color)
3414 tPtr->fgGC = WMColorGC(color);
3415 else
3416 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3418 paintText(tPtr);
3421 void
3422 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3424 if (!tPtr)
3425 return;
3427 if (color) {
3428 tPtr->bgGC = WMColorGC(color);
3429 W_SetViewBackgroundColor(tPtr->view, color);
3430 } else {
3431 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3432 W_SetViewBackgroundColor(tPtr->view,
3433 WMWhiteColor(tPtr->view->screen));
3436 paintText(tPtr);
3439 void
3440 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3442 if (!tPtr)
3443 return;
3444 tPtr->flags.relief = relief;
3445 textDidResize(tPtr->view->delegate, tPtr->view);
3448 void
3449 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3451 if (!tPtr)
3452 return;
3454 if (shouldhave && !tPtr->hS) {
3455 tPtr->hS = WMCreateScroller(tPtr);
3456 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3457 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3458 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3459 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3460 WMMapWidget(tPtr->hS);
3461 } else if (!shouldhave && tPtr->hS) {
3462 WMUnmapWidget(tPtr->hS);
3463 WMDestroyWidget(tPtr->hS);
3464 tPtr->hS = NULL;
3467 tPtr->hpos = 0;
3468 tPtr->prevHpos = 0;
3469 textDidResize(tPtr->view->delegate, tPtr->view);
3473 void
3474 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3476 if (!tPtr)
3477 return;
3479 if(shouldhave && !tPtr->ruler) {
3480 tPtr->ruler = WMCreateRuler(tPtr);
3481 (W_VIEW(tPtr->ruler))->attribs.cursor =
3482 tPtr->view->screen->defaultCursor;
3483 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3484 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3485 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3486 } else if(!shouldhave && tPtr->ruler) {
3487 WMShowTextRuler(tPtr, False);
3488 WMDestroyWidget(tPtr->ruler);
3489 tPtr->ruler = NULL;
3491 textDidResize(tPtr->view->delegate, tPtr->view);
3494 void
3495 WMShowTextRuler(WMText *tPtr, Bool show)
3497 if(!tPtr)
3498 return;
3499 if(!tPtr->ruler)
3500 return;
3502 if(tPtr->flags.monoFont)
3503 show = False;
3505 tPtr->flags.rulerShown = show;
3506 if(show) {
3507 WMMapWidget(tPtr->ruler);
3508 } else {
3509 WMUnmapWidget(tPtr->ruler);
3512 textDidResize(tPtr->view->delegate, tPtr->view);
3515 Bool
3516 WMGetTextRulerShown(WMText *tPtr)
3518 if(!tPtr)
3519 return 0;
3521 if(!tPtr->ruler)
3522 return 0;
3524 return tPtr->flags.rulerShown;
3528 void
3529 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3531 if (!tPtr)
3532 return;
3534 if (shouldhave && !tPtr->vS) {
3535 tPtr->vS = WMCreateScroller(tPtr);
3536 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3537 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3538 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3539 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3540 WMMapWidget(tPtr->vS);
3541 } else if (!shouldhave && tPtr->vS) {
3542 WMUnmapWidget(tPtr->vS);
3543 WMDestroyWidget(tPtr->vS);
3544 tPtr->vS = NULL;
3547 tPtr->vpos = 0;
3548 tPtr->prevVpos = 0;
3549 textDidResize(tPtr->view->delegate, tPtr->view);
3554 Bool
3555 WMScrollText(WMText *tPtr, int amount)
3557 Bool scroll=False;
3558 if (!tPtr)
3559 return False;
3560 if (amount == 0 || !tPtr->view->flags.realized)
3561 return False;
3563 if (amount < 0) {
3564 if (tPtr->vpos > 0) {
3565 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3566 else tPtr->vpos=0;
3567 scroll=True;
3568 } } else {
3569 int limit = tPtr->docHeight - tPtr->visible.h;
3570 if (tPtr->vpos < limit) {
3571 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3572 else tPtr->vpos = limit;
3573 scroll = True;
3574 } }
3576 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3577 updateScrollers(tPtr);
3578 paintText(tPtr);
3580 tPtr->prevVpos = tPtr->vpos;
3581 return scroll;
3584 Bool
3585 WMPageText(WMText *tPtr, Bool direction)
3587 if (!tPtr) return False;
3588 if (!tPtr->view->flags.realized) return False;
3590 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3593 void
3594 WMSetTextEditable(WMText *tPtr, Bool editable)
3596 if (!tPtr)
3597 return;
3598 tPtr->flags.editable = editable;
3601 int
3602 WMGetTextEditable(WMText *tPtr)
3604 if (!tPtr)
3605 return 0;
3606 return tPtr->flags.editable;
3609 void
3610 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3612 if (!tPtr)
3613 return;
3614 tPtr->flags.indentNewLine = indent;
3617 void
3618 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3620 if (!tPtr)
3621 return;
3622 tPtr->flags.ignoreNewLine = ignore;
3625 Bool
3626 WMGetTextIgnoresNewline(WMText *tPtr)
3628 if (!tPtr)
3629 return True;
3630 return tPtr->flags.ignoreNewLine;
3633 void
3634 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3636 if (!tPtr)
3637 return;
3639 if (mono) {
3640 if(tPtr->flags.rulerShown)
3641 WMShowTextRuler(tPtr, False);
3642 if(tPtr->flags.alignment != WALeft)
3643 tPtr->flags.alignment = WALeft;
3646 tPtr->flags.monoFont = mono;
3647 textDidResize(tPtr->view->delegate, tPtr->view);
3650 Bool
3651 WMGetTextUsesMonoFont(WMText *tPtr)
3653 if (!tPtr)
3654 return True;
3655 return tPtr->flags.monoFont;
3659 void
3660 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3662 if (!tPtr)
3663 return;
3665 WMReleaseFont(tPtr->dFont);
3666 if (font)
3667 tPtr->dFont = WMRetainFont(font);
3668 else
3669 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3672 WMFont *
3673 WMGetTextDefaultFont(WMText *tPtr)
3675 if (!tPtr)
3676 return NULL;
3677 else
3678 return WMRetainFont(tPtr->dFont);
3681 void
3682 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3684 if (!tPtr)
3685 return;
3687 WMReleaseColor(tPtr->dColor);
3688 if (color)
3689 tPtr->dColor = WMRetainColor(color);
3690 else
3691 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3694 WMColor *
3695 WMGetTextDefaultColor(WMText *tPtr)
3697 if (!tPtr)
3698 return NULL;
3699 else
3700 return WMRetainColor(tPtr->dColor);
3703 void
3704 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3706 if (!tPtr)
3707 return;
3708 if(tPtr->flags.monoFont)
3709 tPtr->flags.alignment = WALeft;
3710 else
3711 tPtr->flags.alignment = alignment;
3712 WMThawText(tPtr);
3715 int
3716 WMGetTextInsertType(WMText *tPtr)
3718 if (!tPtr)
3719 return 0;
3720 return tPtr->flags.prepend;
3724 void
3725 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3727 if (!tPtr || !color)
3728 return;
3730 setSelectionProperty(tPtr, NULL, color, -1);
3733 WMColor *
3734 WMGetTextSelectionColor(WMText *tPtr)
3736 TextBlock *tb;
3738 if (!tPtr)
3739 return NULL;
3741 tb = tPtr->currentTextBlock;
3743 if (!tb || !tPtr->flags.ownsSelection)
3744 return NULL;
3746 if(!tb->selected)
3747 return NULL;
3749 return tb->color;
3753 void
3754 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3756 if (!tPtr || !font)
3757 return;
3759 setSelectionProperty(tPtr, font, NULL, -1) ;
3762 WMFont *
3763 WMGetTextSelectionFont(WMText *tPtr)
3765 TextBlock *tb;
3767 if (!tPtr)
3768 return NULL;
3770 tb = tPtr->currentTextBlock;
3772 if (!tb || !tPtr->flags.ownsSelection)
3773 return NULL;
3775 if(!tb->selected)
3776 return NULL;
3778 if(tb->graphic) {
3779 tb = getFirstNonGraphicBlockFor(tb, 1);
3780 if(!tb)
3781 return NULL;
3783 return (tb->selected ? tb->d.font : NULL);
3787 void
3788 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3790 if (!tPtr || (underlined!=0 && underlined !=1))
3791 return;
3793 setSelectionProperty(tPtr, NULL, NULL, underlined);
3797 int
3798 WMGetTextSelectionUnderlined(WMText *tPtr)
3800 TextBlock *tb;
3802 if (!tPtr)
3803 return 0;
3805 tb = tPtr->currentTextBlock;
3807 if (!tb || !tPtr->flags.ownsSelection)
3808 return 0;
3810 if(!tb->selected)
3811 return 0;
3813 return tb->underlined;
3817 void
3818 WMFreezeText(WMText *tPtr)
3820 if (!tPtr)
3821 return;
3823 tPtr->flags.frozen = True;
3827 void
3828 WMThawText(WMText *tPtr)
3830 if (!tPtr)
3831 return;
3833 tPtr->flags.frozen = False;
3835 if(tPtr->flags.monoFont) {
3836 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3837 TextBlock *tb;
3839 /* make sure to unmap widgets no matter where they are */
3840 /* they'll be later remapped if needed by paintText */
3841 for(j=0; j<c; j++) {
3842 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3843 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3844 WMUnmapWidget(tb->d.widget);
3850 tPtr->flags.laidOut = False;
3851 layOutDocument(tPtr);
3852 updateScrollers(tPtr);
3853 paintText(tPtr);
3854 tPtr->flags.needsLayOut = False;
3858 /* find first occurence of a string */
3859 static char *
3860 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
3861 Bool caseSensitive)
3863 char *ptr;
3865 if(!haystack || !needle || !end)
3866 return NULL;
3868 for (ptr = haystack; ptr < end; ptr++) {
3869 if(caseSensitive) {
3870 if (*ptr == *needle && !strncmp(ptr, needle, len))
3871 return ptr;
3873 } else {
3874 if (tolower(*ptr) == tolower(*needle) &&
3875 !strncasecmp(ptr, needle, len))
3876 return ptr;
3880 return NULL;
3883 /* find last occurence of a string */
3884 static char *
3885 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
3886 Bool caseSensitive)
3888 char *ptr;
3890 if(!haystack || !needle || !end)
3891 return NULL;
3893 for (ptr = haystack-2; ptr > end; ptr--) {
3894 if(caseSensitive) {
3895 if (*ptr == *needle && !strncmp(ptr, needle, len))
3896 return ptr;
3897 } else {
3898 if (tolower(*ptr) == tolower(*needle) &&
3899 !strncasecmp(ptr, needle, len))
3900 return ptr;
3904 return NULL;
3908 Bool
3909 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3910 Bool caseSensitive)
3912 TextBlock *tb;
3913 char *mark=NULL;
3914 unsigned short pos;
3916 if (!tPtr || !needle)
3917 return False;
3919 #if 0
3920 if (! (tb = tPtr->currentTextBlock)) {
3921 if (! (tb = ( (direction > 0) ?
3922 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3923 return False;
3925 } else {
3926 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3927 tb = (direction>0) ? tb->next : tb->prior; */
3928 if(tb != tPtr->lastTextBlock)
3929 tb = tb->prior;
3931 #endif
3932 tb = tPtr->currentTextBlock;
3933 pos = tPtr->tpos;
3936 while(tb) {
3937 if (!tb->graphic) {
3939 if(direction > 0) {
3940 if(pos+1 < tb->used)
3941 pos++;
3943 if(tb->used - pos> 0 && pos > 0) {
3944 mark = mystrstr(&tb->text[pos], needle,
3945 strlen(needle), &tb->text[tb->used], caseSensitive);
3947 } else {
3948 tb = tb->next;
3949 pos = 0;
3950 continue;
3953 } else {
3954 if(pos-1 > 0)
3955 pos--;
3957 if(pos > 0) {
3958 mark = mystrrstr(&tb->text[pos], needle,
3959 strlen(needle), tb->text, caseSensitive);
3960 } else {
3961 tb = tb->prior;
3962 if(!tb)
3963 return False;
3964 pos = tb->used;
3965 continue;
3970 if(mark) {
3971 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3973 tPtr->tpos = (int)(mark - tb->text);
3974 tPtr->currentTextBlock = tb;
3975 updateCursorPosition(tPtr);
3976 tPtr->sel.y = tPtr->cursor.y+5;
3977 tPtr->sel.h = tPtr->cursor.h-10;
3978 tPtr->sel.x = tPtr->cursor.x +1;
3979 tPtr->sel.w = WMIN(WMWidthOfString(font,
3980 &tb->text[tPtr->tpos], strlen(needle)),
3981 tPtr->docWidth - tPtr->sel.x);
3982 tPtr->flags.ownsSelection = True;
3983 paintText(tPtr);
3985 return True;
3989 tb = (direction>0) ? tb->next : tb->prior;
3990 if(tb) {
3991 pos = (direction>0) ? 0 : tb->used;
3995 return False;