lotsa new features
[wmaker-crm.git] / WINGs / wtext.c
blobaf128e08230cde6f386ed85b50f35cf79d5986e9
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
5 #include "WINGsP.h"
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 #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 * - FIX html parser: 1. <b>foo</i> should STILL BE BOLD!
20 * - 2. " foo > bar " should not confuse it.
21 * - assess danger of destroying widgets whose actions link to other pages
22 * - Tabs now are simply replaced by 4 spaces...
23 * - redo blink code to reduce paint event... use pixmap buffer...
24 * - add paragraph support (full) and '\n' code in getStream..
28 /* a Section is a section of a TextBlock that describes what parts
29 of a TextBlock has been laid out on which "line"...
30 o this greatly aids redraw, scroll and selection.
31 o this is created during layoutLine, but may be later modified.
32 o there may be many Sections per TextBlock, hence the array */
33 typedef struct {
34 unsigned int x, y; /* where to draw it from */
35 unsigned short w, h; /* its width and height */
36 unsigned short begin; /* where the layout begins */
37 unsigned short end ; /* where it ends */
38 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
39 unsigned short last:1; /* is it the last section on a "line"? */
40 unsigned int _y:31; /* the "line" it and other textblocks are on */
41 } Section;
44 /* a TextBlock is a doubly-linked list of TextBlocks containing:
45 o text for the block, color and font
46 o or a pointer to the pixmap
47 o OR a pointer to the widget and the (text) description for its graphic
50 typedef struct _TextBlock {
51 struct _TextBlock *next; /* next text block in linked list */
52 struct _TextBlock *prior; /* prior text block in linked list */
54 char *text; /* pointer to text (could be kanji) */
55 /* or to the object's description */
56 union {
57 WMFont *font; /* the font */
58 WMWidget *widget; /* the embedded widget */
59 WMPixmap *pixmap; /* the pixmap */
60 } d; /* description */
62 unsigned short used; /* number of chars in this block */
63 unsigned short allocated; /* size of allocation (in chars) */
64 WMColor *color; /* the color */
66 Section *sections; /* the region for layouts (a growable array) */
67 /* an _array_! of size _nsections_ */
69 unsigned short s_begin; /* where the selection begins */
70 unsigned short s_end; /* where it ends */
72 unsigned int first:1; /* first TextBlock in paragraph */
73 unsigned int blank:1; /* ie. blank paragraph */
74 unsigned int kanji:1; /* is of 16-bit characters or not */
75 unsigned int graphic:1; /* graphic or text: text=0 */
76 unsigned int object:1; /* embedded object or pixmap */
77 unsigned int underlined:1; /* underlined or not */
78 unsigned int selected:1; /* selected or not */
79 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
80 int script:8; /* script in points: negative for subscript */
81 unsigned int marginN:8; /* which of the margins in the tPtr to use */
82 unsigned int nClicks:2; /* single, double, triple clicks */
83 unsigned int RESERVED:7;
84 } TextBlock;
87 /* I'm lazy: visible.h vs. visible.size.height :-) */
88 typedef struct {
89 unsigned int y;
90 unsigned int x;
91 unsigned int h;
92 unsigned int w;
93 } myRect;
96 typedef struct W_Text {
97 W_Class widgetClass; /* the class number of this widget */
98 W_View *view; /* the view referring to this instance */
100 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
102 WMScroller *vS; /* the vertical scroller */
103 unsigned int vpos; /* the current vertical position */
104 unsigned int prevVpos; /* the previous vertical position */
106 WMScroller *hS; /* the horizontal scroller */
107 unsigned int hpos; /* the current horizontal position */
108 unsigned int prevHpos; /* the previous horizontal position */
110 WMFont *dFont; /* the default font */
111 WMColor *dColor; /* the default color */
112 WMPixmap *dBulletPix; /* the default pixmap for bullets */
114 GC bgGC; /* the background GC to draw with */
115 GC fgGC; /* the foreground GC to draw with */
116 Pixmap db; /* the buffer on which to draw */
118 myRect visible; /* the actual rectangle that can be drawn into */
119 myRect cursor; /* the position and (height) of cursor */
120 myRect sel; /* the selection rectangle */
122 WMPoint clicked; /* where in the _document_ was clicked */
124 unsigned short tpos; /* the position in the currentTextBlock */
125 unsigned short docWidth; /* the width of the entire document */
126 unsigned int docHeight; /* the height of the entire document */
128 TextBlock *firstTextBlock;
129 TextBlock *lastTextBlock;
130 TextBlock *currentTextBlock;
132 WMArray *gfxItems; /* a nice array of graphic items */
134 #if DO_BLINK
135 WMHandlerID timerID; /* for nice twinky-winky */
136 #endif
138 WMAction *parser;
139 WMAction *writer;
140 WMTextDelegate *delegate;
141 Time lastClickTime;
143 WMRulerMargins *margins; /* an array of margins */
145 unsigned int nMargins:8; /* the total number of margins in use */
146 struct {
147 unsigned int monoFont:1; /* whether to ignore formats and graphic */
148 unsigned int focused:1; /* whether this instance has input focus */
149 unsigned int editable:1; /* "silly user, you can't edit me" */
150 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
151 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
152 unsigned int extendSelection:1; /* shift-drag to select more regions */
154 unsigned int rulerShown:1; /* whether the ruler is shown or not */
155 unsigned int frozen:1; /* whether screen updates are to be made */
156 unsigned int cursorShown:1; /* whether to show the cursor */
157 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
158 unsigned int horizOnDemand:1;/* if a large image should appear*/
159 unsigned int needsLayOut:1; /* in case of Append/Deletes */
160 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
161 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
162 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
163 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
164 WMAlignment alignment:2; /* the alignment for text */
165 WMReliefType relief:3; /* the relief to display with */
166 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
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 tb = hold;
565 return tb;
569 static Bool
570 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
571 TextBlock *tb)
573 if (tPtr->flags.monoFont && tb->graphic) {
574 tb = getFirstNonGraphicBlockFor(tb, *dir);
575 if (tb->graphic) {
576 tPtr->currentTextBlock =
577 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
578 tPtr->tpos = 0;
579 return 0;
583 *dir = !(y <= tb->sections[0].y);
584 if(*dir) {
585 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
586 && (y >= tb->sections[0]._y ) ) {
587 /* if it's on the same line */
588 if(x < tb->sections[0].x)
589 *dir = 0;
591 } else {
592 if ( ( y <= tb->sections[tb->nsections-1]._y
593 + tb->sections[tb->nsections-1].h )
594 && (y >= tb->sections[tb->nsections-1]._y ) ) {
595 /* if it's on the same line */
596 if(x > tb->sections[tb->nsections-1].x)
597 *dir = 1;
601 return 1;
605 static void
606 paintText(Text *tPtr)
608 TextBlock *tb;
609 WMFont *font;
610 GC gc, greyGC;
611 char *text;
612 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
613 WMScreen *scr = tPtr->view->screen;
614 Display *dpy = tPtr->view->screen->display;
615 Window win = tPtr->view->window;
617 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
618 return;
620 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
621 0, 0, tPtr->visible.w, tPtr->visible.h);
623 if (! (tb = tPtr->currentTextBlock)) {
624 if (! (tb = tPtr->firstTextBlock)) {
625 goto _copy_area;
629 if (tPtr->flags.ownsSelection)
630 greyGC = WMColorGC(WMGrayColor(scr));
632 done = False;
636 /* first, which direction? Don't waste time looking all over,
637 since the parts to be drawn will most likely be near what
638 was previously drawn */
639 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
640 goto _copy_area;
642 while(tb) {
644 if (tb->graphic && tPtr->flags.monoFont)
645 goto _getSibling;
647 if(dir) {
648 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
649 + tb->sections[tb->nsections-1].h)
650 break;
651 } else {
652 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
653 + tb->sections[tb->nsections-1].h)
654 break;
657 _getSibling:
658 if(dir) {
659 if(tb->next)
660 tb = tb->next;
661 else break;
662 } else {
663 if(tb->prior)
664 tb = tb->prior;
665 else break;
670 /* first, place all text that can be viewed */
671 while (!done && tb) {
673 /* paragraph diagnostic
674 if(tb->blank) {tb->text[0] = 'F'; } */
676 if (tb->graphic) {
677 tb = tb->next;
678 continue;
681 tb->selected = False;
683 for(s=0; s<tb->nsections && !done; s++) {
685 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
686 done = True;
687 break;
690 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
691 continue;
693 if (tPtr->flags.monoFont) {
694 font = tPtr->dFont;
695 gc = tPtr->fgGC;
696 } else {
697 font = tb->d.font;
698 gc = WMColorGC(tb->color);
701 if (tPtr->flags.ownsSelection) {
702 XRectangle rect;
704 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
705 tb->selected = True;
706 XFillRectangle(dpy, tPtr->db, greyGC,
707 rect.x, rect.y, rect.width, rect.height);
711 prev_y = tb->sections[s]._y;
713 len = tb->sections[s].end - tb->sections[s].begin;
714 text = &(tb->text[tb->sections[s].begin]);
715 y = tb->sections[s].y - tPtr->vpos;
716 WMDrawString(scr, tPtr->db, gc, font,
717 tb->sections[s].x - tPtr->hpos, y, text, len);
719 if (tb->underlined) {
720 XDrawLine(dpy, tPtr->db, gc,
721 tb->sections[s].x - tPtr->hpos,
722 y + font->y + 1,
723 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
724 y + font->y + 1);
729 tb = (!done? tb->next : NULL);
733 /* now , show all graphic items that can be viewed */
734 c = WMGetArrayItemCount(tPtr->gfxItems);
735 if (c > 0 && !tPtr->flags.monoFont) {
736 int j, h;
738 for(j=0; j<c; j++) {
739 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
741 /* if it's not viewable, and mapped, unmap it */
742 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
743 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
745 if(tb->object) {
746 if ((W_VIEW(tb->d.widget))->flags.mapped) {
747 WMUnmapWidget(tb->d.widget);
750 } else {
751 /* if it's viewable, and not mapped, map it */
752 if(tb->object) {
753 W_View *view = W_VIEW(tb->d.widget);
755 if (!view->flags.realized)
756 WMRealizeWidget(tb->d.widget);
757 if(!view->flags.mapped) {
758 XMapWindow(view->screen->display, view->window);
759 XFlush(view->screen->display);
760 view->flags.mapped = 1;
764 if (tPtr->flags.ownsSelection) {
765 XRectangle rect;
767 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
768 tb->selected = True;
769 XFillRectangle(dpy, tPtr->db, greyGC,
770 rect.x, rect.y, rect.width, rect.height);
774 if(tb->object) {
775 WMMoveWidget(tb->d.widget,
776 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
777 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
778 h = WMWidgetHeight(tb->d.widget) + 1;
780 } else {
781 WMDrawPixmap(tb->d.pixmap, tPtr->db,
782 tb->sections[0].x - tPtr->hpos,
783 tb->sections[0].y - tPtr->vpos);
784 h = tb->d.pixmap->height + 1;
788 if (tb->underlined) {
789 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
790 tb->sections[0].x - tPtr->hpos,
791 tb->sections[0].y + h - tPtr->vpos,
792 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
793 tb->sections[0].y + h - tPtr->vpos);
794 } } } }
797 _copy_area:
798 if (tPtr->flags.editable && tPtr->flags.cursorShown
799 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
800 int y = tPtr->cursor.y - tPtr->vpos;
801 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
802 tPtr->cursor.x, y,
803 tPtr->cursor.x, y + tPtr->cursor.h);
806 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
807 tPtr->visible.w, tPtr->visible.h,
808 tPtr->visible.x, tPtr->visible.y);
810 W_DrawRelief(scr, win, 0, 0,
811 tPtr->view->size.width, tPtr->view->size.height,
812 tPtr->flags.relief);
814 if (tPtr->ruler && tPtr->flags.rulerShown)
815 XDrawLine(dpy, win, tPtr->fgGC,
816 2, 42, tPtr->view->size.width-4, 42);
820 static void
821 mouseOverObject(Text *tPtr, int x, int y)
823 TextBlock *tb;
824 Bool result = False;
826 x -= tPtr->visible.x;
827 x += tPtr->hpos;
828 y -= tPtr->visible.y;
829 y += tPtr->vpos;
831 if(tPtr->flags.ownsSelection) {
832 if(tPtr->sel.x <= x
833 && tPtr->sel.y <= y
834 && tPtr->sel.x + tPtr->sel.w >= x
835 && tPtr->sel.y + tPtr->sel.h >= y) {
836 tPtr->flags.isOverGraphic = 1;
837 result = True;
842 if(!result) {
843 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
845 if (c<1) {
846 tPtr->flags.isOverGraphic = 0;
847 return;
850 for(j=0; j<c; j++) {
851 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
853 if(!tb || !tb->sections) {
854 tPtr->flags.isOverGraphic = 0;
855 return;
858 if(!tb->object) {
859 if(tb->sections[0].x <= x
860 && tb->sections[0].y <= y
861 && tb->sections[0].x + tb->sections[0].w >= x
862 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
863 tPtr->flags.isOverGraphic = 3;
864 result = True;
865 break;
872 if(!result)
873 tPtr->flags.isOverGraphic = 0;
876 tPtr->view->attribs.cursor = (result?
877 tPtr->view->screen->defaultCursor
878 : tPtr->view->screen->textCursor);
880 XSetWindowAttributes attribs;
881 attribs.cursor = tPtr->view->attribs.cursor;
882 XChangeWindowAttributes(tPtr->view->screen->display,
883 tPtr->view->window, CWCursor,
884 &attribs);
888 #if DO_BLINK
890 static void
891 blinkCursor(void *data)
893 Text *tPtr = (Text*)data;
895 if (tPtr->flags.cursorShown) {
896 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
897 blinkCursor, data);
898 } else {
899 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
900 blinkCursor, data);
902 paintText(tPtr);
903 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
905 #endif
907 static void
908 updateCursorPosition(Text *tPtr)
910 TextBlock *tb = NULL;
911 int x, y, h, s;
913 if(tPtr->flags.needsLayOut)
914 layOutDocument(tPtr);
916 if (! (tb = tPtr->currentTextBlock)) {
917 if (! (tb = tPtr->firstTextBlock)) {
918 tPtr->tpos = 0;
919 tPtr->cursor.h = tPtr->dFont->height;
920 tPtr->cursor.y = 2;
921 tPtr->cursor.x = 2;
922 return;
927 if(tb->blank) {
928 tPtr->tpos = 0;
929 y = tb->sections[0].y;
930 h = tb->sections[0].h;
931 x = tb->sections[0].x;
933 } else if(tb->graphic) {
934 y = tb->sections[0].y;
935 h = tb->sections[0].h;
936 x = tb->sections[0].x;
938 } else {
939 if(tPtr->tpos > tb->used)
940 tPtr->tpos = tb->used;
942 for(s=0; s<tb->nsections-1; s++) {
944 if(tPtr->tpos >= tb->sections[s].begin
945 && tPtr->tpos <= tb->sections[s].end)
946 break;
949 y = tb->sections[s]._y;
950 h = tb->sections[s].h;
951 x = tb->sections[s].x + WMWidthOfString(
952 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
953 &tb->text[tb->sections[s].begin],
954 tPtr->tpos - tb->sections[s].begin);
957 tPtr->cursor.y = y;
958 tPtr->cursor.h = h;
959 tPtr->cursor.x = x;
962 /* scroll the bars if the cursor is not visible */
963 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
964 if(tPtr->cursor.y+tPtr->cursor.h
965 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
966 tPtr->vpos +=
967 (tPtr->cursor.y+tPtr->cursor.h+10
968 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
969 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
970 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
975 updateScrollers(tPtr);
979 static void
980 cursorToTextPosition(Text *tPtr, int x, int y)
982 TextBlock *tb = NULL;
983 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
984 char *text;
986 if(tPtr->flags.needsLayOut)
987 layOutDocument(tPtr);
989 y += (tPtr->vpos - tPtr->visible.y);
990 if (y<0)
991 y = 0;
993 x -= (tPtr->visible.x - 2);
994 if (x<0)
995 x=0;
997 /* clicked is relative to document, not window... */
998 tPtr->clicked.x = x;
999 tPtr->clicked.y = y;
1001 if (! (tb = tPtr->currentTextBlock)) {
1002 if (! (tb = tPtr->firstTextBlock)) {
1003 tPtr->tpos = 0;
1004 tPtr->cursor.h = tPtr->dFont->height;
1005 tPtr->cursor.y = 2;
1006 tPtr->cursor.x = 2;
1007 return;
1011 /* first, which direction? Most likely, newly clicked
1012 position will be close to previous */
1013 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1014 return;
1017 s = (dir? 0 : tb->nsections-1);
1018 if ( y >= tb->sections[s]._y
1019 && y <= tb->sections[s]._y + tb->sections[s].h) {
1020 goto _doneV;
1023 /* get the first (or last) section of the TextBlock that
1024 lies about the vertical click point */
1025 done = False;
1026 while (!done && tb) {
1028 if (tPtr->flags.monoFont && tb->graphic) {
1029 if( (dir?tb->next:tb->prior))
1030 tb = (dir?tb->next:tb->prior);
1031 continue;
1034 s = (dir? 0 : tb->nsections-1);
1035 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1037 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1038 ( y >= tb->sections[s]._y ) ) ) {
1039 done = True;
1040 } else {
1041 dir? s++ : s--;
1045 if (!done) {
1046 if ( (dir? tb->next : tb->prior)) {
1047 tb = (dir ? tb->next : tb->prior);
1048 } else {
1049 pos = tb->used;
1050 break; /* goto _doneH; */
1056 if (s<0 || s>=tb->nsections) {
1057 s = (dir? tb->nsections-1 : 0);
1060 _doneV:
1061 /* we have the line, which TextBlock on that line is it? */
1062 pos = (dir?0:tb->sections[s].begin);
1063 if (tPtr->flags.monoFont && tb->graphic)
1064 tb = getFirstNonGraphicBlockFor(tb, dir);
1066 if(tb->blank)
1067 _w = 0;
1069 _y = tb->sections[s]._y;
1071 while (tb) {
1073 if (tPtr->flags.monoFont && tb->graphic) {
1074 tb = (dir ? tb->next : tb->prior);
1075 continue;
1078 if (dir) {
1079 if (tb->graphic) {
1080 if(tb->object)
1081 _w = WMWidgetWidth(tb->d.widget)-5;
1082 else
1083 _w = tb->d.pixmap->width-5;
1084 } else {
1085 text = &(tb->text[tb->sections[s].begin]);
1086 len = tb->sections[s].end - tb->sections[s].begin;
1087 _w = WMWidthOfString(tb->d.font, text, len);
1088 if (tb->sections[s].x + _w >= x)
1089 break;
1092 } else {
1093 if (tb->sections[s].x <= x)
1094 break;
1097 if ((dir? tb->next : tb->prior)) {
1098 TextBlock *nxt = (dir? tb->next : tb->prior);
1099 if (tPtr->flags.monoFont && nxt->graphic) {
1100 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1101 if (!nxt) {
1102 pos = (dir?0:tb->sections[s].begin);
1103 tPtr->cursor.x = tb->sections[s].x;
1104 goto _doneH;
1108 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1109 /* this must be the last/first on this line. stop */
1110 pos = (dir? tb->sections[s].end : 0);
1111 tPtr->cursor.x = tb->sections[s].x;
1112 if (!tb->blank) {
1113 if (tb->graphic) {
1114 if(tb->object)
1115 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1116 else
1117 tPtr->cursor.x += tb->d.pixmap->width;
1118 } else if (pos > tb->sections[s].begin) {
1119 tPtr->cursor.x +=
1120 WMWidthOfString(tb->d.font,
1121 &(tb->text[tb->sections[s].begin]),
1122 pos - tb->sections[s].begin);
1125 goto _doneH;
1129 if ( (dir? tb->next : tb->prior)) {
1130 tb = (dir ? tb->next : tb->prior);
1131 } else {
1132 done = True;
1133 break;
1136 if (tb)
1137 s = (dir? 0 : tb->nsections-1);
1140 /* we have said TextBlock, now where within it? */
1141 if (tb && !tb->graphic) {
1142 WMFont *f = tb->d.font;
1143 len = tb->sections[s].end - tb->sections[s].begin;
1144 text = &(tb->text[tb->sections[s].begin]);
1146 _w = x - tb->sections[s].x;
1147 pos = 0;
1149 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1150 pos++;
1152 tPtr->cursor.x = tb->sections[s].x +
1153 (pos? WMWidthOfString(f, text, pos) : 0);
1155 pos += tb->sections[s].begin;
1156 _doneH:
1157 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1160 if (!tb)
1161 printf("...for this app will surely crash :-)\n");
1163 tPtr->currentTextBlock = tb;
1164 tPtr->cursor.h = tb->sections[s].h;
1165 tPtr->cursor.y = tb->sections[s]._y;
1167 /* scroll the bars if the cursor is not visible */
1168 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1169 if(tPtr->cursor.y+tPtr->cursor.h
1170 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1171 tPtr->vpos +=
1172 (tPtr->cursor.y+tPtr->cursor.h+10
1173 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1174 updateScrollers(tPtr);
1175 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1176 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1177 updateScrollers(tPtr);
1184 static void
1185 autoSelectText(Text *tPtr, int clicks)
1187 int x, start;
1188 TextBlock *tb;
1189 char *mark = NULL, behind, ahead;
1191 if(!(tb = tPtr->currentTextBlock))
1192 return;
1194 if(clicks == 2) {
1197 switch(tb->text[tPtr->tpos]) {
1198 case ' ': return;
1200 case '<': case '>': behind = '<'; ahead = '>'; break;
1201 case '{': case '}': behind = '{'; ahead = '}'; break;
1202 case '[': case ']': behind = '['; ahead = ']'; break;
1204 default: behind = ahead = ' ';
1207 tPtr->sel.y = tPtr->cursor.y+5;
1208 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
1210 if(tb->graphic) {
1211 tPtr->sel.x = tb->sections[0].x;
1212 tPtr->sel.w = tb->sections[0].w;
1213 } else {
1214 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1216 start = tPtr->tpos;
1217 while(start > 0 && tb->text[start-1] != behind)
1218 start--;
1220 x = tPtr->cursor.x;
1221 if(tPtr->tpos > start){
1222 x -= WMWidthOfString(font, &tb->text[start],
1223 tPtr->tpos - start);
1225 tPtr->sel.x = (x<0?0:x)+1;
1227 if((mark = strchr(&tb->text[start], ahead))) {
1228 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1229 (int)(mark - &tb->text[start]));
1230 } else if(tb->used > start) {
1231 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1232 tb->used - start);
1236 } else if(clicks == 3) {
1237 TextBlock *cur = tb;
1239 while(tb && !tb->first) {
1240 tb = tb->prior;
1242 tPtr->sel.y = tb->sections[0]._y;
1244 tb = cur;
1245 while(tb->next && !tb->next->first) {
1246 tb = tb->next;
1248 tPtr->sel.h = tb->sections[tb->nsections-1]._y
1249 + 5 - tPtr->sel.y;
1251 tPtr->sel.x = 0;
1252 tPtr->sel.w = tPtr->docWidth;
1253 tPtr->clicked.x = 0; /* only for now, fix sel. code */
1256 tPtr->flags.ownsSelection = True;
1257 paintText(tPtr);
1262 static void
1263 updateScrollers(Text *tPtr)
1266 if (tPtr->flags.frozen)
1267 return;
1269 if (tPtr->vS) {
1270 if (tPtr->docHeight < tPtr->visible.h) {
1271 WMSetScrollerParameters(tPtr->vS, 0, 1);
1272 tPtr->vpos = 0;
1273 } else {
1274 float hmax = (float)(tPtr->docHeight);
1275 WMSetScrollerParameters(tPtr->vS,
1276 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1277 (float)tPtr->visible.h/hmax);
1279 } else tPtr->vpos = 0;
1281 if (tPtr->hS) {
1282 if (tPtr->docWidth < tPtr->visible.w) {
1283 WMSetScrollerParameters(tPtr->hS, 0, 1);
1284 tPtr->hpos = 0;
1285 } else {
1286 float wmax = (float)(tPtr->docWidth);
1287 WMSetScrollerParameters(tPtr->hS,
1288 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1289 (float)tPtr->visible.w/wmax);
1291 } else tPtr->hpos = 0;
1294 static void
1295 scrollersCallBack(WMWidget *w, void *self)
1297 Text *tPtr = (Text *)self;
1298 Bool scroll = False;
1299 int which;
1301 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1302 return;
1304 if (w == tPtr->vS) {
1305 int height;
1306 height = tPtr->visible.h;
1308 which = WMGetScrollerHitPart(tPtr->vS);
1309 switch(which) {
1311 case WSDecrementLine:
1312 if (tPtr->vpos > 0) {
1313 if (tPtr->vpos>16) tPtr->vpos-=16;
1314 else tPtr->vpos=0;
1315 scroll=True;
1316 }break;
1318 case WSIncrementLine: {
1319 int limit = tPtr->docHeight - height;
1320 if (tPtr->vpos < limit) {
1321 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1322 else tPtr->vpos=limit;
1323 scroll = True;
1324 }}break;
1326 case WSDecrementPage:
1327 if(((int)tPtr->vpos - (int)height) >= 0)
1328 tPtr->vpos -= height;
1329 else
1330 tPtr->vpos = 0;
1332 scroll = True;
1333 break;
1335 case WSIncrementPage:
1336 tPtr->vpos += height;
1337 if (tPtr->vpos > (tPtr->docHeight - height))
1338 tPtr->vpos = tPtr->docHeight - height;
1339 scroll = True;
1340 break;
1343 case WSKnob:
1344 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1345 * (float)(tPtr->docHeight - height);
1346 scroll = True;
1347 break;
1349 case WSKnobSlot:
1350 case WSNoPart:
1351 break;
1353 scroll = (tPtr->vpos != tPtr->prevVpos);
1354 tPtr->prevVpos = tPtr->vpos;
1358 if (w == tPtr->hS) {
1359 int width = tPtr->visible.w;
1361 which = WMGetScrollerHitPart(tPtr->hS);
1362 switch(which) {
1364 case WSDecrementLine:
1365 if (tPtr->hpos > 0) {
1366 if (tPtr->hpos>16) tPtr->hpos-=16;
1367 else tPtr->hpos=0;
1368 scroll=True;
1369 }break;
1371 case WSIncrementLine: {
1372 int limit = tPtr->docWidth - width;
1373 if (tPtr->hpos < limit) {
1374 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1375 else tPtr->hpos=limit;
1376 scroll = True;
1377 }}break;
1379 case WSDecrementPage:
1380 if(((int)tPtr->hpos - (int)width) >= 0)
1381 tPtr->hpos -= width;
1382 else
1383 tPtr->hpos = 0;
1385 scroll = True;
1386 break;
1388 case WSIncrementPage:
1389 tPtr->hpos += width;
1390 if (tPtr->hpos > (tPtr->docWidth - width))
1391 tPtr->hpos = tPtr->docWidth - width;
1392 scroll = True;
1393 break;
1396 case WSKnob:
1397 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1398 * (float)(tPtr->docWidth - width);
1399 scroll = True;
1400 break;
1402 case WSKnobSlot:
1403 case WSNoPart:
1404 break;
1406 scroll = (tPtr->hpos != tPtr->prevHpos);
1407 tPtr->prevHpos = tPtr->hpos;
1410 if (scroll) {
1411 updateScrollers(tPtr);
1412 paintText(tPtr);
1418 typedef struct {
1419 TextBlock *tb;
1420 unsigned short begin, end; /* what part of the text block */
1421 } myLineItems;
1424 static int
1425 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1427 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1428 WMFont *font;
1429 char *text;
1430 TextBlock *tb;
1431 TextBlock *tbsame=NULL;
1433 if(!items || nitems == 0)
1434 return 0;
1436 for(i=0; i<nitems; i++) {
1437 tb = items[i].tb;
1439 if (tb->graphic) {
1440 if (!tPtr->flags.monoFont) {
1441 if(tb->object) {
1442 WMWidget *wdt = tb->d.widget;
1443 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1444 if (tPtr->flags.alignment != WALeft)
1445 lw += WMWidgetWidth(wdt);
1446 } else {
1447 line_height = WMAX(line_height,
1448 tb->d.pixmap->height + max_d);
1449 if (tPtr->flags.alignment != WALeft)
1450 lw += tb->d.pixmap->width;
1454 } else {
1455 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1456 max_d = WMAX(max_d, abs(font->height-font->y));
1457 line_height = WMAX(line_height, font->height + max_d);
1458 text = &(tb->text[items[i].begin]);
1459 len = items[i].end - items[i].begin;
1460 if (tPtr->flags.alignment != WALeft)
1461 lw += WMWidthOfString(font, text, len);
1465 if (tPtr->flags.alignment == WARight) {
1466 j = tPtr->visible.w - lw;
1467 } else if (tPtr->flags.alignment == WACenter) {
1468 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1471 for(i=0; i<nitems; i++) {
1472 tb = items[i].tb;
1474 if (tbsame == tb) { /* extend it, since it's on same line */
1475 tb->sections[tb->nsections-1].end = items[i].end;
1476 n = tb->nsections-1;
1477 } else {
1478 tb->sections = wrealloc(tb->sections,
1479 (++tb->nsections)*sizeof(Section));
1480 n = tb->nsections-1;
1481 tb->sections[n]._y = y + max_d;
1482 tb->sections[n].max_d = max_d;
1483 tb->sections[n].x = x+j;
1484 tb->sections[n].h = line_height;
1485 tb->sections[n].begin = items[i].begin;
1486 tb->sections[n].end = items[i].end;
1489 tb->sections[n].last = (i+1 == nitems);
1491 if (tb->graphic) {
1492 if (!tPtr->flags.monoFont) {
1493 if(tb->object) {
1494 WMWidget *wdt = tb->d.widget;
1495 tb->sections[n].y = max_d + y
1496 + line_height - WMWidgetHeight(wdt);
1497 tb->sections[n].w = WMWidgetWidth(wdt);
1498 } else {
1499 tb->sections[n].y = y + line_height
1500 + max_d - tb->d.pixmap->height;
1501 tb->sections[n].w = tb->d.pixmap->width;
1503 x += tb->sections[n].w;
1505 } else {
1506 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1507 len = items[i].end - items[i].begin;
1508 text = &(tb->text[items[i].begin]);
1510 tb->sections[n].y = y+line_height-font->y;
1511 tb->sections[n].w =
1512 WMWidthOfString(font,
1513 &(tb->text[tb->sections[n].begin]),
1514 tb->sections[n].end - tb->sections[n].begin);
1516 x += WMWidthOfString(font, text, len);
1519 tbsame = tb;
1522 return line_height;
1527 static void
1528 layOutDocument(Text *tPtr)
1530 TextBlock *tb;
1531 myLineItems *items = NULL;
1532 unsigned int itemsSize=0, nitems=0, begin, end;
1533 WMFont *font;
1534 unsigned int x, y=0, lw = 0, width=0, bmargin;
1535 char *start=NULL, *mark=NULL;
1537 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1538 return;
1540 tPtr->docWidth = tPtr->visible.w;
1541 x = tPtr->margins[tb->marginN].first;
1542 bmargin = tPtr->margins[tb->marginN].body;
1544 /* only partial layOut needed: re-Lay only affected textblocks */
1545 if (tPtr->flags.laidOut) {
1546 tb = tPtr->currentTextBlock;
1548 /* search backwards for textblocks on same line */
1549 while (tb->prior) {
1550 if (!tb->sections || tb->nsections<1) {
1551 tb = tPtr->firstTextBlock;
1552 tPtr->flags.laidOut = False;
1553 y = 0;
1554 goto _layOut;
1557 if(!tb->prior->sections || tb->prior->nsections<1) {
1558 tb = tPtr->firstTextBlock;
1559 tPtr->flags.laidOut = False;
1560 y = 0;
1561 goto _layOut;
1564 if (tb->sections[0]._y !=
1565 tb->prior->sections[tb->prior->nsections-1]._y) {
1566 break;
1568 tb = tb->prior;
1571 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1572 y = tb->prior->sections[tb->prior->nsections-1]._y +
1573 tb->prior->sections[tb->prior->nsections-1].h -
1574 tb->prior->sections[tb->prior->nsections-1].max_d;
1575 } else {
1576 y = 0;
1580 _layOut:
1581 while (tb) {
1583 if (tb->sections && tb->nsections>0) {
1584 wfree(tb->sections);
1585 tb->sections = NULL;
1586 tb->nsections = 0;
1589 if (tb->blank && tb->next && !tb->next->first) {
1590 TextBlock *next = tb->next;
1591 tPtr->currentTextBlock = tb;
1592 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1593 tb = next;
1594 tb->first = True;
1595 continue;
1598 if (tb->first && tb != tPtr->firstTextBlock) {
1599 y += layOutLine(tPtr, items, nitems, x, y);
1600 x = tPtr->margins[tb->marginN].first;
1601 bmargin = tPtr->margins[tb->marginN].body;
1602 nitems = 0;
1603 lw = 0;
1606 if (tb->graphic) {
1607 if (!tPtr->flags.monoFont) {
1608 if(tb->object)
1609 width = WMWidgetWidth(tb->d.widget);
1610 else
1611 width = tb->d.pixmap->width;
1613 if (width > tPtr->docWidth)
1614 tPtr->docWidth = width;
1616 lw += width;
1617 if (lw >= tPtr->visible.w - x ) {
1618 y += layOutLine(tPtr, items, nitems, x, y);
1619 nitems = 0;
1620 x = bmargin;
1621 lw = width;
1624 if(nitems + 1> itemsSize) {
1625 items = wrealloc(items,
1626 (++itemsSize)*sizeof(myLineItems));
1629 items[nitems].tb = tb;
1630 items[nitems].begin = 0;
1631 items[nitems].end = 0;
1632 nitems++;
1635 } else if ((start = tb->text)) {
1636 begin = end = 0;
1637 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1639 while (start) {
1640 mark = strchr(start, ' ');
1641 if (mark) {
1642 end += (int)(mark-start)+1;
1643 start = mark+1;
1644 } else {
1645 end += strlen(start);
1646 start = mark;
1649 if (end > tb->used)
1650 end = tb->used;
1652 if (end-begin > 0) {
1654 width = WMWidthOfString(font,
1655 &tb->text[begin], end-begin);
1657 /* if it won't fit, break it up */
1658 if (width > tPtr->visible.w) {
1659 char *t = &tb->text[begin];
1660 int l=end-begin, i=0;
1661 do {
1662 width = WMWidthOfString(font, t, ++i);
1663 } while (width < tPtr->visible.w && i < l);
1664 end = begin+i;
1665 if (start)
1666 start -= l-i;
1669 lw += width;
1672 if (lw >= tPtr->visible.w - x) {
1673 y += layOutLine(tPtr, items, nitems, x, y);
1674 lw = width;
1675 x = bmargin;
1676 nitems = 0;
1679 if(nitems + 1 > itemsSize) {
1680 items = wrealloc(items,
1681 (++itemsSize)*sizeof(myLineItems));
1684 items[nitems].tb = tb;
1685 items[nitems].begin = begin;
1686 items[nitems].end = end;
1687 nitems++;
1689 begin = end;
1694 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1695 if(0&&tPtr->flags.laidOut
1696 && tb->next && tb->next->sections && tb->next->nsections>0
1697 && (tPtr->vpos + tPtr->visible.h
1698 < tb->next->sections[0]._y)) {
1699 if(tPtr->lastTextBlock->sections
1700 && tPtr->lastTextBlock->nsections > 0 ) {
1701 TextBlock *ltb = tPtr->lastTextBlock;
1702 int ly = ltb->sections[ltb->nsections-1]._y;
1703 int lh = ltb->sections[ltb->nsections-1].h;
1704 int ss, sd;
1706 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1707 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1709 y += layOutLine(tPtr, items, nitems, x, y);
1710 ss= ly+lh-y;
1711 sd = tPtr->docHeight-y;
1713 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1714 y += tb->next->sections[0]._y-y;
1715 nitems = 0;
1716 printf("nitems%d\n", nitems);
1717 if(ss-sd!=0)
1718 y = tPtr->docHeight+ss-sd;
1720 break;
1721 } else {
1722 tPtr->flags.laidOut = False;
1726 tb = tb->next;
1730 if (nitems > 0)
1731 y += layOutLine(tPtr, items, nitems, x, y);
1733 if (tPtr->docHeight != y+10) {
1734 tPtr->docHeight = y+10;
1735 updateScrollers(tPtr);
1738 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1739 XEvent event;
1741 tPtr->flags.horizOnDemand = True;
1742 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1743 event.type = Expose;
1744 handleEvents(&event, (void *)tPtr);
1746 } else if(tPtr->docWidth <= tPtr->visible.w
1747 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1748 tPtr->flags.horizOnDemand = False;
1749 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1752 tPtr->flags.laidOut = True;
1754 if(items && itemsSize > 0)
1755 wfree(items);
1759 static void
1760 textDidResize(W_ViewDelegate *self, WMView *view)
1762 Text *tPtr = (Text *)view->self;
1763 unsigned short w = tPtr->view->size.width;
1764 unsigned short h = tPtr->view->size.height;
1765 unsigned short rh = 0, vw = 0, rel;
1767 rel = (tPtr->flags.relief == WRFlat);
1769 if (tPtr->ruler && tPtr->flags.rulerShown) {
1770 WMMoveWidget(tPtr->ruler, 2, 2);
1771 WMResizeWidget(tPtr->ruler, w - 4, 40);
1772 rh = 40;
1775 if (tPtr->vS) {
1776 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1777 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1778 vw = 20;
1779 WMSetRulerOffset(tPtr->ruler,22);
1780 } else WMSetRulerOffset(tPtr->ruler, 2);
1782 if (tPtr->hS) {
1783 if (tPtr->vS) {
1784 WMMoveWidget(tPtr->hS, vw, h - 21);
1785 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1786 } else {
1787 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1788 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1792 tPtr->visible.x = (tPtr->vS)?24:4;
1793 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1794 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1795 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1796 tPtr->visible.h -= (tPtr->hS)?20:0;
1797 tPtr->margins[0].right = tPtr->visible.w;
1799 if (tPtr->view->flags.realized) {
1801 if (tPtr->db) {
1802 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1803 tPtr->db = (Pixmap) NULL;
1806 if (tPtr->visible.w < 40)
1807 tPtr->visible.w = 40;
1808 if (tPtr->visible.h < 20)
1809 tPtr->visible.h = 20;
1811 if(!tPtr->db) {
1812 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1813 tPtr->view->window, tPtr->visible.w,
1814 tPtr->visible.h, tPtr->view->screen->depth);
1818 WMThawText(tPtr);
1821 W_ViewDelegate _TextViewDelegate =
1823 NULL,
1824 NULL,
1825 textDidResize,
1826 NULL,
1829 /* nice, divisble-by-16 blocks */
1830 static inline unsigned short
1831 reqBlockSize(unsigned short requested)
1833 return requested + 16 - (requested%16);
1837 static void
1838 clearText(Text *tPtr)
1840 if (!tPtr->firstTextBlock)
1841 return;
1843 while (tPtr->currentTextBlock)
1844 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1846 tPtr->firstTextBlock = NULL;
1847 tPtr->currentTextBlock = NULL;
1848 tPtr->lastTextBlock = NULL;
1851 static void
1852 deleteTextInteractively(Text *tPtr, KeySym ksym)
1854 TextBlock *tb;
1855 Bool back = (Bool) (ksym == XK_BackSpace);
1856 Bool done = 1;
1857 Bool wasFirst = 0;
1859 if (!tPtr->flags.editable) {
1860 XBell(tPtr->view->screen->display, 0);
1861 return;
1864 if ( !(tb = tPtr->currentTextBlock) )
1865 return;
1867 if (tPtr->flags.ownsSelection) {
1868 if(removeSelection(tPtr))
1869 layOutDocument(tPtr);
1870 return;
1873 wasFirst = tb->first;
1874 if (back && tPtr->tpos < 1) {
1875 if (tb->prior) {
1876 if(tb->prior->blank) {
1877 tPtr->currentTextBlock = tb->prior;
1878 WMRemoveTextBlock(tPtr);
1879 tPtr->currentTextBlock = tb;
1880 tb->first = True;
1881 layOutDocument(tPtr);
1882 return;
1883 } else {
1884 if(tb->blank) {
1885 TextBlock *prior = tb->prior;
1886 tPtr->currentTextBlock = tb;
1887 WMRemoveTextBlock(tPtr);
1888 tb = prior;
1889 } else {
1890 tb = tb->prior;
1893 tPtr->tpos = tb->used;
1894 tPtr->currentTextBlock = tb;
1895 done = 1;
1896 if(wasFirst) {
1897 if(tb->next)
1898 tb->next->first = False;
1899 layOutDocument(tPtr);
1900 return;
1906 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1907 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1908 if (back)
1909 tPtr->tpos--;
1910 memmove(&(tb->text[tPtr->tpos]),
1911 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1912 tb->used--;
1913 done = 0;
1916 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1917 || tb->graphic) {
1919 TextBlock *sibling = (back? tb->prior : tb->next);
1921 if(tb->used == 0 || tb->graphic)
1922 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1924 if (sibling) {
1925 tPtr->currentTextBlock = sibling;
1926 tPtr->tpos = (back? sibling->used : 0);
1930 layOutDocument(tPtr);
1934 static void
1935 insertTextInteractively(Text *tPtr, char *text, int len)
1937 TextBlock *tb;
1938 char *newline = NULL;
1940 if (!tPtr->flags.editable) {
1941 XBell(tPtr->view->screen->display, 0);
1942 return;
1945 if (len < 1 || !text)
1946 return;
1949 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1950 return;
1952 if (tPtr->flags.ownsSelection)
1953 removeSelection(tPtr);
1956 if (tPtr->flags.ignoreNewLine) {
1957 int i;
1958 for(i=0; i<len; i++) {
1959 if (text[i] == '\n')
1960 text[i] = ' ';
1964 tb = tPtr->currentTextBlock;
1965 if (!tb || tb->graphic) {
1966 text[len] = 0;
1967 WMAppendTextStream(tPtr, text);
1968 tPtr->tpos = tPtr->currentTextBlock->used;
1969 layOutDocument(tPtr);
1970 return;
1973 if ((newline = strchr(text, '\n'))) {
1974 int nlen = (int)(newline-text);
1975 int s = tb->used - tPtr->tpos;
1976 char save[s];
1977 if (!tb->blank && nlen>0) {
1978 if (s > 0) {
1979 memcpy(save, &tb->text[tPtr->tpos], s);
1980 tb->used -= (tb->used - tPtr->tpos);
1982 text[nlen] = 0;
1983 insertTextInteractively(tPtr, text, nlen);
1984 newline++;
1985 WMAppendTextStream(tPtr, newline);
1986 if (s>0)
1987 insertTextInteractively(tPtr, save, s);
1989 } else {
1990 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1991 && !tb->graphic && tb->text) {
1993 void *ntb = WMCreateTextBlockWithText(
1994 tPtr, &tb->text[tPtr->tpos],
1995 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1996 tb->used = tPtr->tpos;
1997 WMAppendTextBlock(tPtr, ntb);
1998 tPtr->tpos = 0;
2000 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2001 void *ntb = WMCreateTextBlockWithText(tPtr,
2002 NULL, tb->d.font, tb->color, True, 0);
2003 WMAppendTextBlock(tPtr, ntb);
2004 tPtr->tpos = 0;
2008 } else {
2009 if (tb->used + len >= tb->allocated) {
2010 tb->allocated = reqBlockSize(tb->used+len);
2011 tb->text = wrealloc(tb->text, tb->allocated);
2014 if (tb->blank) {
2015 memcpy(tb->text, text, len);
2016 tb->used = len;
2017 tPtr->tpos = len;
2018 tb->text[tb->used] = 0;
2019 tb->blank = False;
2021 } else {
2022 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2023 tb->used-tPtr->tpos+1);
2024 memmove(&tb->text[tPtr->tpos], text, len);
2025 tb->used += len;
2026 tPtr->tpos += len;
2027 tb->text[tb->used] = 0;
2032 layOutDocument(tPtr);
2036 static void
2037 selectRegion(Text *tPtr, int x, int y)
2040 if (x < 0 || y < 0)
2041 return;
2043 y += (tPtr->flags.rulerShown? 40: 0);
2044 y += tPtr->vpos;
2045 if (y>10)
2046 y -= 10; /* the original offset */
2048 x -= tPtr->visible.x-2;
2049 if (x<0)
2050 x=0;
2052 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2053 tPtr->sel.w = abs(tPtr->clicked.x - x);
2054 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2055 tPtr->sel.h = abs(tPtr->clicked.y - y);
2057 tPtr->flags.ownsSelection = True;
2058 paintText(tPtr);
2062 static void
2063 releaseSelection(Text *tPtr)
2065 TextBlock *tb = tPtr->firstTextBlock;
2067 while(tb) {
2068 tb->selected = False;
2069 tb = tb->next;
2071 tPtr->flags.ownsSelection = False;
2072 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2073 CurrentTime);
2075 paintText(tPtr);
2079 WMData*
2080 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2081 Atom *type)
2083 Text *tPtr = view->self;
2084 Display *dpy = tPtr->view->screen->display;
2085 Atom _TARGETS;
2086 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2087 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2088 WMData *data = NULL;
2091 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2092 char *text = WMGetTextSelectedStream(tPtr);
2094 if (text) {
2095 printf("got text [%s]\n", text);
2096 data = WMCreateDataWithBytes(text, strlen(text));
2097 WMSetDataFormat(data, TYPETEXT);
2099 *type = target;
2100 return data;
2101 } else printf("didn't get it\n");
2103 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2104 if (target == _TARGETS) {
2105 Atom *ptr;
2107 ptr = wmalloc(4 * sizeof(Atom));
2108 ptr[0] = _TARGETS;
2109 ptr[1] = XA_STRING;
2110 ptr[2] = TEXT;
2111 ptr[3] = COMPOUND_TEXT;
2113 data = WMCreateDataWithBytes(ptr, 4*4);
2114 WMSetDataFormat(data, 32);
2116 *type = target;
2117 return data;
2120 return NULL;
2123 static void
2124 lostHandler(WMView *view, Atom selection, void *cdata)
2126 releaseSelection((WMText *)view->self);
2129 static WMSelectionProcs selectionHandler = {
2130 requestHandler, lostHandler, NULL
2134 static void
2135 ownershipObserver(void *observerData, WMNotification *notification)
2137 if (observerData != WMGetNotificationClientData(notification))
2138 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2142 static void
2143 fontChanged(void *observerData, WMNotification *notification)
2145 WMText *tPtr = (WMText *) observerData;
2146 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2147 printf("fontChanged\n");
2149 if(!tPtr || !font)
2150 return;
2152 if (tPtr->flags.ownsSelection)
2153 WMSetTextSelectionFont(tPtr, font);
2157 static void
2158 handleTextKeyPress(Text *tPtr, XEvent *event)
2160 char buffer[2];
2161 KeySym ksym;
2162 int control_pressed = False;
2163 TextBlock *tb = NULL;
2165 if (((XKeyEvent *) event)->state & ControlMask)
2166 control_pressed = True;
2167 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2169 switch(ksym) {
2171 case XK_Left:
2172 if(!(tb = tPtr->currentTextBlock))
2173 break;
2174 if(tb->graphic)
2175 goto L_imaGFX;
2177 if(tPtr->tpos==0) {
2178 L_imaGFX: if(tb->prior) {
2179 tPtr->currentTextBlock = tb->prior;
2180 tPtr->tpos = tPtr->currentTextBlock->used -1;
2181 } else tPtr->tpos = 0;
2182 } else tPtr->tpos--;
2183 updateCursorPosition(tPtr);
2184 paintText(tPtr);
2185 break;
2187 case XK_Right:
2188 if(!(tb = tPtr->currentTextBlock))
2189 break;
2190 if(tb->graphic)
2191 goto R_imaGFX;
2192 if(tPtr->tpos == tb->used) {
2193 R_imaGFX: if(tb->next) {
2194 tPtr->currentTextBlock = tb->next;
2195 tPtr->tpos = 1;
2196 } else tPtr->tpos = tb->used;
2197 } else tPtr->tpos++;
2198 updateCursorPosition(tPtr);
2199 paintText(tPtr);
2200 break;
2202 case XK_Down:
2203 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2204 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2205 paintText(tPtr);
2206 break;
2208 case XK_Up:
2209 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2210 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2211 paintText(tPtr);
2212 break;
2214 case XK_BackSpace:
2215 case XK_Delete:
2216 case XK_KP_Delete:
2217 deleteTextInteractively(tPtr, ksym);
2218 updateCursorPosition(tPtr);
2219 paintText(tPtr);
2220 break;
2222 case XK_Control_R :
2223 case XK_Control_L :
2224 control_pressed = True;
2225 break;
2227 case XK_Tab:
2228 insertTextInteractively(tPtr, " ", 4);
2229 updateCursorPosition(tPtr);
2230 paintText(tPtr);
2231 break;
2233 case XK_Return:
2234 buffer[0] = '\n';
2235 default:
2236 if (buffer[0] != 0 && !control_pressed) {
2237 insertTextInteractively(tPtr, buffer, 1);
2238 updateCursorPosition(tPtr);
2239 paintText(tPtr);
2241 } else if (control_pressed && ksym==XK_r) {
2242 Bool i = !tPtr->flags.rulerShown;
2243 WMShowTextRuler(tPtr, i);
2244 tPtr->flags.rulerShown = i;
2246 else if (control_pressed && buffer[0] == '\a')
2247 XBell(tPtr->view->screen->display, 0);
2250 if (!control_pressed && tPtr->flags.ownsSelection)
2251 releaseSelection(tPtr);
2254 static void
2255 handleWidgetPress(XEvent *event, void *data)
2257 TextBlock *tb = (TextBlock *)data;
2258 Text *tPtr;
2259 WMWidget *w;
2261 if (!tb)
2262 return;
2264 tPtr = (Text*)w;
2265 tPtr->currentTextBlock = tb;
2266 tPtr->flags.isOverGraphic = 2;
2267 tPtr->tpos = 0;
2268 output(tb->text, tb->used);
2269 #if 0
2270 if (!tPtr->flags.focused) {
2271 WMSetFocusToWidget(tPtr);
2272 tPtr->flags.focused = True;
2274 #endif
2278 static void
2279 handleActionEvents(XEvent *event, void *data)
2281 Text *tPtr = (Text *)data;
2282 Display *dpy = event->xany.display;
2283 KeySym ksym;
2286 switch (event->type) {
2287 case KeyPress:
2288 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2289 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2290 tPtr->flags.extendSelection = True;
2291 return;
2294 if (tPtr->flags.focused) {
2295 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2296 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2297 GrabModeAsync, GrabModeAsync, None,
2298 tPtr->view->screen->invisibleCursor, CurrentTime);
2299 tPtr->flags.pointerGrabbed = True;
2300 handleTextKeyPress(tPtr, event);
2302 } break;
2304 case KeyRelease:
2305 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2306 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2307 tPtr->flags.extendSelection = False;
2308 return;
2309 /* end modify flag so selection can be extended */
2311 break;
2314 case MotionNotify:
2316 if (tPtr->flags.pointerGrabbed) {
2317 tPtr->flags.pointerGrabbed = False;
2318 XUngrabPointer(dpy, CurrentTime);
2321 if(tPtr->flags.waitingForSelection)
2322 break;
2324 if ((event->xmotion.state & Button1Mask)) {
2325 if (!tPtr->flags.ownsSelection) {
2326 WMCreateSelectionHandler(tPtr->view,
2327 XA_PRIMARY, event->xbutton.time,
2328 &selectionHandler, NULL);
2329 tPtr->flags.ownsSelection = True;
2331 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2332 break;
2335 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2336 break;
2339 case ButtonPress:
2341 if (tPtr->flags.pointerGrabbed) {
2342 tPtr->flags.pointerGrabbed = False;
2343 XUngrabPointer(dpy, CurrentTime);
2344 break;
2347 if (tPtr->flags.waitingForSelection)
2348 break;
2350 if (tPtr->flags.extendSelection) {
2351 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2352 return;
2356 if (event->xbutton.button == Button1) {
2358 if(WMIsDoubleClick(event)) {
2359 TextBlock *tb = tPtr->currentTextBlock;
2361 if(tb && tb->graphic && !tb->object) {
2362 char desc[tb->used+1];
2363 memcpy(desc, tb->text, tb->used);
2364 desc[tb->used] = 0;
2365 if(tPtr->delegate) {
2366 if(tPtr->delegate->didDoubleClickOnPicture)
2367 (*tPtr->delegate->didDoubleClickOnPicture)
2368 (tPtr->delegate, desc);
2370 } else {
2371 autoSelectText(tPtr, 2);
2373 tPtr->lastClickTime = event->xbutton.time;
2374 break;
2375 } else if(event->xbutton.time - tPtr->lastClickTime
2376 < WINGsConfiguration.doubleClickDelay) {
2377 autoSelectText(tPtr, 3);
2378 break;
2381 if (!tPtr->flags.focused) {
2382 WMSetFocusToWidget(tPtr);
2383 tPtr->flags.focused = True;
2386 if (tPtr->flags.ownsSelection)
2387 releaseSelection(tPtr);
2389 tPtr->lastClickTime = event->xbutton.time;
2390 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2391 paintText(tPtr);
2394 if (event->xbutton.button
2395 == WINGsConfiguration.mouseWheelDown) {
2396 WMScrollText(tPtr, -16);
2397 break;
2400 if (event->xbutton.button
2401 == WINGsConfiguration.mouseWheelUp) {
2402 WMScrollText(tPtr, 16);
2403 break;
2406 if (event->xbutton.button == Button2) {
2407 char *text = NULL;
2408 int n;
2410 if (!tPtr->flags.editable) {
2411 XBell(dpy, 0);
2412 break;
2415 #if 0
2416 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2417 event->xbutton.time, pasteText, NULL)) {
2418 #endif
2421 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2423 if (text) {
2424 text[n] = 0;
2426 if (tPtr->parser)
2427 (tPtr->parser) (tPtr, (void *) text);
2428 else
2429 insertTextInteractively(tPtr, text, n);
2431 XFree(text);
2432 #if 0
2433 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2434 (void*)WMInsertTextEvent);
2435 #endif
2437 } else {
2438 tPtr->flags.waitingForSelection = True;
2441 break;
2445 case ButtonRelease:
2447 if (tPtr->flags.pointerGrabbed) {
2448 tPtr->flags.pointerGrabbed = False;
2449 XUngrabPointer(dpy, CurrentTime);
2450 break;
2453 if (tPtr->flags.waitingForSelection)
2454 break;
2460 static void
2461 handleEvents(XEvent *event, void *data)
2463 Text *tPtr = (Text *)data;
2465 switch(event->type) {
2466 case Expose:
2468 if (event->xexpose.count!=0)
2469 break;
2471 if(tPtr->hS) {
2472 if (!(W_VIEW(tPtr->hS))->flags.realized)
2473 WMRealizeWidget(tPtr->hS);
2476 if(tPtr->vS) {
2477 if (!(W_VIEW(tPtr->vS))->flags.realized)
2478 WMRealizeWidget(tPtr->vS);
2481 if(tPtr->ruler) {
2482 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2483 WMRealizeWidget(tPtr->ruler);
2487 if(!tPtr->db)
2488 textDidResize(tPtr->view->delegate, tPtr->view);
2490 paintText(tPtr);
2491 break;
2493 case FocusIn:
2494 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2495 != tPtr->view)
2496 return;
2497 tPtr->flags.focused = True;
2498 #if DO_BLINK
2499 if (tPtr->flags.editable && !tPtr->timerID) {
2500 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2501 blinkCursor, tPtr);
2503 #endif
2505 break;
2507 case FocusOut:
2508 tPtr->flags.focused = False;
2509 paintText(tPtr);
2510 #if DO_BLINK
2511 if (tPtr->timerID) {
2512 WMDeleteTimerHandler(tPtr->timerID);
2513 tPtr->timerID = NULL;
2515 #endif
2516 break;
2519 case DestroyNotify:
2520 clearText(tPtr);
2521 if(tPtr->hS)
2522 WMDestroyWidget(tPtr->hS);
2523 if(tPtr->vS)
2524 WMDestroyWidget(tPtr->vS);
2525 if(tPtr->ruler)
2526 WMDestroyWidget(tPtr->ruler);
2527 if(tPtr->db)
2528 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2529 if(tPtr->gfxItems)
2530 WMFreeArray(tPtr->gfxItems);
2531 #if DO_BLINK
2532 if (tPtr->timerID)
2533 WMDeleteTimerHandler(tPtr->timerID);
2534 #endif
2535 WMReleaseFont(tPtr->dFont);
2536 WMReleaseColor(tPtr->dColor);
2537 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2538 WMRemoveNotificationObserver(tPtr);
2540 wfree(tPtr);
2542 break;
2548 static void
2549 insertText(WMText *tPtr, char *stream)
2551 char *start, *mark;
2552 void *tb = NULL;
2554 if (!stream) {
2555 clearText(tPtr);
2556 return;
2559 start = stream;
2560 while (start) {
2561 mark = strchr(start, '\n');
2562 if (mark) {
2563 tb = WMCreateTextBlockWithText(tPtr,
2564 start, tPtr->dFont,
2565 tPtr->dColor, True, (int)(mark-start));
2566 start = mark+1;
2567 } else {
2568 if (start && strlen(start)) {
2569 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2570 tPtr->dColor, False, strlen(start));
2571 } else tb = NULL;
2572 start = mark;
2575 if (tPtr->flags.prepend)
2576 WMPrependTextBlock(tPtr, tb);
2577 else
2578 WMAppendTextBlock(tPtr, tb);
2580 return;
2585 static void
2586 rulerMoveCallBack(WMWidget *w, void *self)
2588 Text *tPtr = (Text *)self;
2589 if (!tPtr)
2590 return;
2591 if (W_CLASS(tPtr) != WC_Text)
2592 return;
2594 paintText(tPtr);
2598 static void
2599 rulerReleaseCallBack(WMWidget *w, void *self)
2601 Text *tPtr = (Text *)self;
2602 if (!tPtr)
2603 return;
2604 if (W_CLASS(tPtr) != WC_Text)
2605 return;
2607 WMThawText(tPtr);
2608 return;
2612 static unsigned
2613 draggingEntered(WMView *self, WMDraggingInfo *info)
2615 printf("draggingEntered\n");
2616 return WDOperationCopy;
2620 static unsigned
2621 draggingUpdated(WMView *self, WMDraggingInfo *info)
2623 return WDOperationCopy;
2627 static void
2628 draggingExited(WMView *self, WMDraggingInfo *info)
2630 printf("draggingExited\n");
2633 static Bool
2634 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2636 printf("prepareForDragOperation\n");
2637 return True;
2641 char *badbadbad;
2643 static void
2644 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2645 void *cdata, WMData *data)
2647 badbadbad = wstrdup((char *)WMDataBytes(data));
2651 /* when it's done in WINGs, remove this */
2653 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2655 WMScreen *scr = W_VIEW_SCREEN(view);
2657 if (!WMRequestSelection(scr->dragInfo.destView,
2658 scr->xdndSelectionAtom,
2659 XInternAtom(scr->display, type, False),
2660 scr->dragInfo.timestamp,
2661 receivedData, &scr->dragInfo)) {
2662 wwarning("could not request data for dropped data");
2667 XEvent ev;
2669 ev.type = ClientMessage;
2670 ev.xclient.message_type = scr->xdndFinishedAtom;
2671 ev.xclient.format = 32;
2672 ev.xclient.window = info->destinationWindow;
2673 ev.xclient.data.l[0] = 0;
2674 ev.xclient.data.l[1] = 0;
2675 ev.xclient.data.l[2] = 0;
2676 ev.xclient.data.l[3] = 0;
2677 ev.xclient.data.l[4] = 0;
2679 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2680 XFlush(scr->display);
2682 return True;
2685 static Bool
2686 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2688 WMColor *color;
2689 WMText *tPtr = (WMText *)self->self;
2691 if (!tPtr)
2692 return True;
2694 requestDroppedData(tPtr->view, info, "application/X-color");
2695 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2696 if(color) {
2697 WMSetTextSelectionColor(tPtr, color);
2698 WMReleaseColor(color);
2703 return True;
2706 static void
2707 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2709 printf("concludeDragOperation\n");
2713 static WMDragDestinationProcs _DragDestinationProcs = {
2714 draggingEntered,
2715 draggingUpdated,
2716 draggingExited,
2717 prepareForDragOperation,
2718 performDragOperation,
2719 concludeDragOperation
2723 char *
2724 getStream(WMText *tPtr, int sel, int array)
2726 TextBlock *tb = NULL;
2727 char *text = NULL;
2728 unsigned long where = 0;
2730 if (!tPtr)
2731 return NULL;
2733 if (!(tb = tPtr->firstTextBlock))
2734 return NULL;
2736 if (tPtr->writer) {
2737 (tPtr->writer) (tPtr, (void *) text);
2738 return text;
2741 tb = tPtr->firstTextBlock;
2742 while (tb) {
2744 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2746 if (!sel || (tb->graphic && tb->selected)) {
2748 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2749 && tb != tPtr->firstTextBlock) {
2750 text = wrealloc(text, where+1);
2751 text[where++] = '\n';
2754 if(tb->blank)
2755 goto _gSnext;
2757 if(tb->graphic && array) {
2758 text = wrealloc(text, where+4);
2759 text[where++] = 0xFA;
2760 text[where++] = (tb->used>>8)&0x0ff;
2761 text[where++] = tb->used&0x0ff;
2762 text[where++] = tb->allocated; /* extra info */
2764 text = wrealloc(text, where+tb->used);
2765 memcpy(&text[where], tb->text, tb->used);
2766 where += tb->used;
2769 } else if (sel && tb->selected) {
2771 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2772 && tb != tPtr->firstTextBlock) {
2773 text = wrealloc(text, where+1);
2774 text[where++] = '\n';
2777 if(tb->blank)
2778 goto _gSnext;
2780 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2781 memcpy(&text[where], &tb->text[tb->s_begin],
2782 tb->s_end - tb->s_begin);
2783 where += tb->s_end - tb->s_begin;
2788 _gSnext:tb = tb->next;
2791 /* +1 for the end of string, let's be nice */
2792 text = wrealloc(text, where+1);
2793 text[where] = 0;
2794 return text;
2798 static void
2799 releaseStreamObjects(void *data)
2801 if(data)
2802 wfree(data);
2805 WMArray *
2806 getStreamObjects(WMText *tPtr, int sel)
2808 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2809 WMData *data;
2810 char *stream;
2811 unsigned short len;
2812 char *start, *fa, *desc;
2814 stream = getStream(tPtr, sel, 1);
2815 if(!stream)
2816 return NULL;
2818 start = stream;
2819 while (start) {
2821 fa = strchr(start, 0xFA);
2822 if (fa) {
2823 if((int)(fa - start)>0) {
2824 desc = start;
2825 desc[(int)(fa - start)] = 0;
2826 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2827 WMSetDataFormat(data, TYPETEXT);
2828 WMAddToArray(array, (void *) data);
2831 len = *(fa+1)*0xff + *(fa+2);
2832 data = WMCreateDataWithBytes((void *)(fa+4), len);
2833 WMSetDataFormat(data, *(fa+3));
2834 WMAddToArray(array, (void *) data);
2835 start = fa + len + 4;
2837 } else {
2838 if (start && strlen(start)) {
2839 data = WMCreateDataWithBytes((void *)start, strlen(start));
2840 WMSetDataFormat(data, TYPETEXT);
2841 WMAddToArray(array, (void *) data);
2843 start = fa;
2847 wfree(stream);
2848 return array;
2849 WMFreeArray(array);
2853 WMText *
2854 WMCreateTextForDocumentType(WMWidget *parent,
2855 WMAction *parser, WMAction *writer)
2857 Text *tPtr = wmalloc(sizeof(Text));
2858 if (!tPtr) {
2859 printf("could not create text widget\n");
2860 return NULL;
2864 memset(tPtr, 0, sizeof(Text));
2865 tPtr->widgetClass = WC_Text;
2866 tPtr->view = W_CreateView(W_VIEW(parent));
2867 if (!tPtr->view) {
2868 perror("could not create text's view\n");
2869 free(tPtr);
2870 return NULL;
2872 tPtr->view->self = tPtr;
2873 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2874 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2875 W_ResizeView(tPtr->view, 250, 200);
2877 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2878 tPtr->bgGC = WMColorGC(tPtr->dColor);
2879 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2880 WMReleaseColor(tPtr->dColor);
2882 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2883 tPtr->fgGC = WMColorGC(tPtr->dColor);
2885 tPtr->ruler = NULL;
2886 tPtr->vS = NULL;
2887 tPtr->hS = NULL;
2889 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2891 tPtr->view->delegate = &_TextViewDelegate;
2893 tPtr->delegate = NULL;
2895 #if DO_BLINK
2896 tPtr->timerID = NULL;
2897 #endif
2899 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2900 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2901 handleEvents, tPtr);
2903 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2904 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2905 handleActionEvents, tPtr);
2907 WMAddNotificationObserver(ownershipObserver, tPtr,
2908 "_lostOwnership", tPtr);
2910 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2912 char *types[2] = {"application/X-color", NULL};
2913 WMRegisterViewForDraggedTypes(tPtr->view, types);
2916 WMAddNotificationObserver(fontChanged, tPtr,
2917 "WMFontPanelDidChangeNotification", tPtr);
2919 tPtr->firstTextBlock = NULL;
2920 tPtr->lastTextBlock = NULL;
2921 tPtr->currentTextBlock = NULL;
2922 tPtr->tpos = 0;
2924 tPtr->gfxItems = WMCreateArray(4);
2926 tPtr->parser = parser;
2927 tPtr->writer = writer;
2929 tPtr->sel.x = tPtr->sel.y = 2;
2930 tPtr->sel.w = tPtr->sel.h = 0;
2932 tPtr->clicked.x = tPtr->clicked.y = 2;
2934 tPtr->visible.x = tPtr->visible.y = 2;
2935 tPtr->visible.h = tPtr->view->size.height;
2936 tPtr->visible.w = tPtr->view->size.width - 4;
2938 tPtr->cursor.x = -23;
2940 tPtr->docWidth = 0;
2941 tPtr->docHeight = 0;
2942 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2943 default_bullet);
2944 tPtr->db = (Pixmap) NULL;
2946 tPtr->margins = WMGetRulerMargins(NULL);
2947 tPtr->margins->right = tPtr->visible.w;
2948 tPtr->nMargins = 1;
2950 tPtr->flags.rulerShown = False;
2951 tPtr->flags.monoFont = False;
2952 tPtr->flags.focused = False;
2953 tPtr->flags.editable = True;
2954 tPtr->flags.ownsSelection = False;
2955 tPtr->flags.pointerGrabbed = False;
2956 tPtr->flags.extendSelection = False;
2957 tPtr->flags.frozen = False;
2958 tPtr->flags.cursorShown = True;
2959 tPtr->flags.acceptsGraphic = False;
2960 tPtr->flags.horizOnDemand = False;
2961 tPtr->flags.needsLayOut = False;
2962 tPtr->flags.ignoreNewLine = False;
2963 tPtr->flags.laidOut = False;
2964 tPtr->flags.waitingForSelection = False;
2965 tPtr->flags.prepend = False;
2966 tPtr->flags.isOverGraphic = False;
2967 tPtr->flags.relief = WRSunken;
2968 tPtr->flags.isOverGraphic = 0;
2969 tPtr->flags.alignment = WALeft;
2971 return tPtr;
2974 void
2975 WMPrependTextStream(WMText *tPtr, char *text)
2977 CHECK_CLASS(tPtr, WC_Text);
2979 if(!text) {
2980 if(tPtr->flags.ownsSelection)
2981 releaseSelection(tPtr);
2982 else
2983 clearText(tPtr);
2986 tPtr->flags.prepend = True;
2987 if (text && tPtr->parser)
2988 (tPtr->parser) (tPtr, (void *) text);
2989 else
2990 insertText(tPtr, text);
2992 tPtr->flags.needsLayOut = True;
2996 void
2997 WMAppendTextStream(WMText *tPtr, char *text)
2999 CHECK_CLASS(tPtr, WC_Text);
3001 if(!text) {
3002 if(tPtr->flags.ownsSelection)
3003 releaseSelection(tPtr);
3004 else
3005 clearText(tPtr);
3008 tPtr->flags.prepend = False;
3009 if (text && tPtr->parser)
3010 (tPtr->parser) (tPtr, (void *) text);
3011 else
3012 insertText(tPtr, text);
3014 tPtr->flags.needsLayOut = True;
3019 char *
3020 WMGetTextStream(WMText *tPtr)
3022 CHECK_CLASS(tPtr, WC_Text);
3023 return getStream(tPtr, 0, 0);
3026 char *
3027 WMGetTextSelectedStream(WMText *tPtr)
3029 CHECK_CLASS(tPtr, WC_Text);
3030 return getStream(tPtr, 1, 0);
3033 WMArray *
3034 WMGetTextObjects(WMText *tPtr)
3036 CHECK_CLASS(tPtr, WC_Text);
3037 return getStreamObjects(tPtr, 0);
3040 WMArray *
3041 WMGetTextSelectedObjects(WMText *tPtr)
3043 CHECK_CLASS(tPtr, WC_Text);
3044 return getStreamObjects(tPtr, 1);
3048 void
3049 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3051 CHECK_CLASS(tPtr, WC_Text);
3053 tPtr->delegate = delegate;
3057 void *
3058 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3059 char *description, WMColor *color,
3060 unsigned short first, unsigned short extraInfo)
3062 TextBlock *tb;
3063 unsigned short len;
3065 if (!w || !description || !color)
3066 return NULL;
3068 tb = wmalloc(sizeof(TextBlock));
3069 if (!tb)
3070 return NULL;
3072 len = strlen(description);
3073 tb->text = (char *)wmalloc(len);
3074 memset(tb->text, 0, len);
3075 memcpy(tb->text, description, len);
3076 tb->used = len;
3077 tb->text[tb->used] = 0;
3078 tb->blank = False;
3079 tb->d.widget = w;
3080 tb->color = WMRetainColor(color);
3081 tb->marginN = newMargin(tPtr, NULL);
3082 tb->allocated = extraInfo;
3083 tb->first = first;
3084 tb->kanji = False;
3085 tb->graphic = True;
3086 tb->object = True;
3087 tb->underlined = False;
3088 tb->selected = False;
3089 tb->script = 0;
3090 tb->sections = NULL;
3091 tb->nsections = 0;
3092 tb->prior = NULL;
3093 tb->next = NULL;
3095 return tb;
3099 void *
3100 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3101 char *description, WMColor *color,
3102 unsigned short first, unsigned short extraInfo)
3104 TextBlock *tb;
3105 unsigned short len;
3107 if (!p || !description || !color)
3108 return NULL;
3110 tb = wmalloc(sizeof(TextBlock));
3111 if (!tb)
3112 return NULL;
3114 len = strlen(description);
3115 tb->text = (char *)wmalloc(len);
3116 memset(tb->text, 0, len);
3117 memcpy(tb->text, description, len);
3118 tb->used = len;
3119 tb->text[tb->used] = 0;
3120 tb->blank = False;
3121 tb->d.pixmap = WMRetainPixmap(p);
3122 tb->color = WMRetainColor(color);
3123 tb->marginN = newMargin(tPtr, NULL);
3124 tb->allocated = extraInfo;
3125 tb->first = first;
3126 tb->kanji = False;
3127 tb->graphic = True;
3128 tb->object = False;
3129 tb->underlined = False;
3130 tb->selected = False;
3131 tb->script = 0;
3132 tb->sections = NULL;
3133 tb->nsections = 0;
3134 tb->prior = NULL;
3135 tb->next = NULL;
3137 return tb;
3140 void *
3141 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3142 unsigned short first, unsigned short len)
3144 TextBlock *tb;
3146 if (!font || !color)
3147 return NULL;
3149 tb = wmalloc(sizeof(TextBlock));
3150 if (!tb)
3151 return NULL;
3153 tb->allocated = reqBlockSize(len);
3154 tb->text = (char *)wmalloc(tb->allocated);
3155 memset(tb->text, 0, tb->allocated);
3157 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3158 *tb->text = ' ';
3159 tb->used = 1;
3160 tb->blank = True;
3161 } else {
3162 memcpy(tb->text, text, len);
3163 tb->used = len;
3164 tb->blank = False;
3166 tb->text[tb->used] = 0;
3168 tb->d.font = WMRetainFont(font);
3169 tb->color = WMRetainColor(color);
3170 tb->marginN = newMargin(tPtr, NULL);
3171 tb->first = first;
3172 tb->kanji = False;
3173 tb->graphic = False;
3174 tb->underlined = False;
3175 tb->selected = False;
3176 tb->script = 0;
3177 tb->sections = NULL;
3178 tb->nsections = 0;
3179 tb->prior = NULL;
3180 tb->next = NULL;
3181 return tb;
3184 void
3185 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3186 unsigned int kanji, unsigned int underlined, int script,
3187 WMRulerMargins *margins)
3189 TextBlock *tb = (TextBlock *) vtb;
3190 if (!tb)
3191 return;
3193 tb->first = first;
3194 tb->kanji = kanji;
3195 tb->underlined = underlined;
3196 tb->script = script;
3197 tb->marginN = newMargin(tPtr, margins);
3200 void
3201 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3202 unsigned int *kanji, unsigned int *underlined, int *script,
3203 WMRulerMargins *margins)
3205 TextBlock *tb = (TextBlock *) vtb;
3206 if (!tb)
3207 return;
3209 if (first) *first = tb->first;
3210 if (kanji) *kanji = tb->kanji;
3211 if (underlined) *underlined = tb->underlined;
3212 if (script) *script = tb->script;
3213 if (margins) margins = &tPtr->margins[tb->marginN];
3218 void
3219 WMPrependTextBlock(WMText *tPtr, void *vtb)
3221 TextBlock *tb = (TextBlock *)vtb;
3223 if (!tPtr || !tb)
3224 return;
3226 if (tb->graphic) {
3227 if(tb->object) {
3228 WMWidget *w = tb->d.widget;
3229 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3230 handleWidgetPress, tb);
3231 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3232 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3233 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3236 WMAddToArray(tPtr->gfxItems, (void *)tb);
3237 tPtr->tpos = 0;
3238 } else tPtr->tpos = tb->used;
3240 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3241 tb->next = tb->prior = NULL;
3242 tb->first = True;
3243 tPtr->lastTextBlock = tPtr->firstTextBlock
3244 = tPtr->currentTextBlock = tb;
3245 return;
3248 if(!tb->first) {
3249 tb->marginN = tPtr->currentTextBlock->marginN;
3252 tb->next = tPtr->currentTextBlock;
3253 tb->prior = tPtr->currentTextBlock->prior;
3254 if (tPtr->currentTextBlock->prior)
3255 tPtr->currentTextBlock->prior->next = tb;
3257 tPtr->currentTextBlock->prior = tb;
3258 if (!tb->prior)
3259 tPtr->firstTextBlock = tb;
3261 tPtr->currentTextBlock = tb;
3265 void
3266 WMAppendTextBlock(WMText *tPtr, void *vtb)
3268 TextBlock *tb = (TextBlock *)vtb;
3270 if (!tPtr || !tb)
3271 return;
3273 if (tb->graphic) {
3274 if(tb->object) {
3275 WMWidget *w = tb->d.widget;
3276 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3277 handleWidgetPress, tb);
3278 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3279 (W_VIEW(w))->attribs.cursor =
3280 tPtr->view->screen->defaultCursor;
3281 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3284 WMAddToArray(tPtr->gfxItems, (void *)tb);
3285 tPtr->tpos = 0;
3286 } else tPtr->tpos = tb->used;
3288 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3289 tb->next = tb->prior = NULL;
3290 tb->first = True;
3291 tPtr->lastTextBlock = tPtr->firstTextBlock
3292 = tPtr->currentTextBlock = tb;
3293 return;
3296 if(!tb->first) {
3297 tb->marginN = tPtr->currentTextBlock->marginN;
3300 tb->next = tPtr->currentTextBlock->next;
3301 tb->prior = tPtr->currentTextBlock;
3302 if (tPtr->currentTextBlock->next)
3303 tPtr->currentTextBlock->next->prior = tb;
3305 tPtr->currentTextBlock->next = tb;
3307 if (!tb->next)
3308 tPtr->lastTextBlock = tb;
3310 tPtr->currentTextBlock = tb;
3313 void *
3314 WMRemoveTextBlock(WMText *tPtr)
3316 TextBlock *tb = NULL;
3318 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3319 || !tPtr->currentTextBlock) {
3320 printf("cannot remove non existent TextBlock!\b");
3321 return tb;
3324 tb = tPtr->currentTextBlock;
3325 if (tb->graphic) {
3326 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3328 if(tb->object) {
3329 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3330 handleWidgetPress, tb);
3331 WMUnmapWidget(tb->d.widget);
3335 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3336 if (tPtr->currentTextBlock->next)
3337 tPtr->currentTextBlock->next->prior = NULL;
3339 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3340 tPtr->currentTextBlock = tPtr->firstTextBlock;
3342 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3343 tPtr->currentTextBlock->prior->next = NULL;
3344 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3345 tPtr->currentTextBlock = tPtr->lastTextBlock;
3346 } else {
3347 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3348 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3349 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3352 return (void *)tb;
3355 void
3356 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3358 TextBlock *tb = (TextBlock *)vtb;
3359 if (!tPtr || !tb)
3360 return;
3362 if (tb->graphic) {
3363 if(tb->object) {
3364 /* naturally, there's a danger to destroying
3365 widgets whose action brings us here:
3366 ie. press a button to destroy it... need to
3367 find a safer way. till then... this stays commented out */
3368 /* WMDestroyWidget(tb->d.widget);
3369 wfree(tb->d.widget); */
3370 tb->d.widget = NULL;
3371 } else {
3372 WMReleasePixmap(tb->d.pixmap);
3373 tb->d.pixmap = NULL;
3375 } else {
3376 WMReleaseFont(tb->d.font);
3379 WMReleaseColor(tb->color);
3380 if (tb->sections && tb->nsections > 0)
3381 wfree(tb->sections);
3382 wfree(tb->text);
3383 wfree(tb);
3384 tb = NULL;
3389 void
3390 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3392 if (!tPtr)
3393 return;
3395 if (color)
3396 tPtr->fgGC = WMColorGC(color);
3397 else
3398 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3400 paintText(tPtr);
3403 void
3404 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3406 if (!tPtr)
3407 return;
3409 if (color) {
3410 tPtr->bgGC = WMColorGC(color);
3411 W_SetViewBackgroundColor(tPtr->view, color);
3412 } else {
3413 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3414 W_SetViewBackgroundColor(tPtr->view,
3415 WMWhiteColor(tPtr->view->screen));
3418 paintText(tPtr);
3421 void
3422 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3424 if (!tPtr)
3425 return;
3426 tPtr->flags.relief = relief;
3427 textDidResize(tPtr->view->delegate, tPtr->view);
3430 void
3431 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3433 if (!tPtr)
3434 return;
3436 if (shouldhave && !tPtr->hS) {
3437 tPtr->hS = WMCreateScroller(tPtr);
3438 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3439 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3440 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3441 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3442 WMMapWidget(tPtr->hS);
3443 } else if (!shouldhave && tPtr->hS) {
3444 WMUnmapWidget(tPtr->hS);
3445 WMDestroyWidget(tPtr->hS);
3446 tPtr->hS = NULL;
3449 tPtr->hpos = 0;
3450 tPtr->prevHpos = 0;
3451 textDidResize(tPtr->view->delegate, tPtr->view);
3455 void
3456 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3458 if (!tPtr)
3459 return;
3461 if(shouldhave && !tPtr->ruler) {
3462 tPtr->ruler = WMCreateRuler(tPtr);
3463 (W_VIEW(tPtr->ruler))->attribs.cursor =
3464 tPtr->view->screen->defaultCursor;
3465 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3466 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3467 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3468 } else if(!shouldhave && tPtr->ruler) {
3469 WMShowTextRuler(tPtr, False);
3470 WMDestroyWidget(tPtr->ruler);
3471 tPtr->ruler = NULL;
3473 textDidResize(tPtr->view->delegate, tPtr->view);
3476 void
3477 WMShowTextRuler(WMText *tPtr, Bool show)
3479 if(!tPtr)
3480 return;
3481 if(!tPtr->ruler)
3482 return;
3484 if(tPtr->flags.monoFont)
3485 show = False;
3487 tPtr->flags.rulerShown = show;
3488 if(show) {
3489 WMMapWidget(tPtr->ruler);
3490 } else {
3491 WMUnmapWidget(tPtr->ruler);
3494 textDidResize(tPtr->view->delegate, tPtr->view);
3497 Bool
3498 WMGetTextRulerShown(WMText *tPtr)
3500 if(!tPtr)
3501 return 0;
3503 if(!tPtr->ruler)
3504 return 0;
3506 return tPtr->flags.rulerShown;
3510 void
3511 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3513 if (!tPtr)
3514 return;
3516 if (shouldhave && !tPtr->vS) {
3517 tPtr->vS = WMCreateScroller(tPtr);
3518 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3519 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3520 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3521 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3522 WMMapWidget(tPtr->vS);
3523 } else if (!shouldhave && tPtr->vS) {
3524 WMUnmapWidget(tPtr->vS);
3525 WMDestroyWidget(tPtr->vS);
3526 tPtr->vS = NULL;
3529 tPtr->vpos = 0;
3530 tPtr->prevVpos = 0;
3531 textDidResize(tPtr->view->delegate, tPtr->view);
3536 Bool
3537 WMScrollText(WMText *tPtr, int amount)
3539 Bool scroll=False;
3540 if (!tPtr)
3541 return False;
3542 if (amount == 0 || !tPtr->view->flags.realized)
3543 return False;
3545 if (amount < 0) {
3546 if (tPtr->vpos > 0) {
3547 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3548 else tPtr->vpos=0;
3549 scroll=True;
3550 } } else {
3551 int limit = tPtr->docHeight - tPtr->visible.h;
3552 if (tPtr->vpos < limit) {
3553 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3554 else tPtr->vpos = limit;
3555 scroll = True;
3556 } }
3558 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3559 updateScrollers(tPtr);
3560 paintText(tPtr);
3562 tPtr->prevVpos = tPtr->vpos;
3563 return scroll;
3566 Bool
3567 WMPageText(WMText *tPtr, Bool direction)
3569 if (!tPtr) return False;
3570 if (!tPtr->view->flags.realized) return False;
3572 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3575 void
3576 WMSetTextEditable(WMText *tPtr, Bool editable)
3578 if (!tPtr)
3579 return;
3580 tPtr->flags.editable = editable;
3583 int
3584 WMGetTextEditable(WMText *tPtr)
3586 if (!tPtr)
3587 return 0;
3588 return tPtr->flags.editable;
3591 void
3592 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3594 if (!tPtr)
3595 return;
3596 tPtr->flags.ignoreNewLine = ignore;
3599 Bool
3600 WMGetTextIgnoresNewline(WMText *tPtr)
3602 if (!tPtr)
3603 return True;
3604 return tPtr->flags.ignoreNewLine;
3607 void
3608 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3610 if (!tPtr)
3611 return;
3612 if (mono && tPtr->flags.rulerShown)
3613 WMShowTextRuler(tPtr, False);
3615 tPtr->flags.monoFont = mono;
3616 WMThawText(tPtr);
3619 Bool
3620 WMGetTextUsesMonoFont(WMText *tPtr)
3622 if (!tPtr)
3623 return True;
3624 return tPtr->flags.monoFont;
3628 void
3629 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3631 if (!tPtr)
3632 return;
3634 WMReleaseFont(tPtr->dFont);
3635 if (font)
3636 tPtr->dFont = WMRetainFont(font);
3637 else
3638 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3641 WMFont *
3642 WMGetTextDefaultFont(WMText *tPtr)
3644 if (!tPtr)
3645 return NULL;
3646 else
3647 return tPtr->dFont;
3650 void
3651 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3653 if (!tPtr)
3654 return;
3655 tPtr->flags.alignment = alignment;
3656 WMThawText(tPtr);
3659 int
3660 WMGetTextInsertType(WMText *tPtr)
3662 if (!tPtr)
3663 return 0;
3664 return tPtr->flags.prepend;
3668 void
3669 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3671 if (!tPtr || !color)
3672 return;
3674 setSelectionProperty(tPtr, NULL, color, -1);
3677 void
3678 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3680 if (!tPtr || !font)
3681 return;
3683 setSelectionProperty(tPtr, font, NULL, -1) ;
3686 void
3687 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3689 if (!tPtr || (underlined!=0 && underlined !=1))
3690 return;
3692 setSelectionProperty(tPtr, NULL, NULL, underlined);
3698 void
3699 WMFreezeText(WMText *tPtr)
3701 if (!tPtr)
3702 return;
3704 tPtr->flags.frozen = True;
3708 void
3709 WMThawText(WMText *tPtr)
3711 if (!tPtr)
3712 return;
3714 tPtr->flags.frozen = False;
3716 if(tPtr->flags.monoFont) {
3717 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3718 TextBlock *tb;
3720 /* make sure to unmap widgets no matter where they are */
3721 /* they'll be later remapped if needed by paintText */
3722 for(j=0; j<c; j++) {
3723 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3724 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3725 WMUnmapWidget(tb->d.widget);
3731 tPtr->flags.laidOut = False;
3732 layOutDocument(tPtr);
3733 updateScrollers(tPtr);
3734 paintText(tPtr);
3735 tPtr->flags.needsLayOut = False;
3740 static char *
3741 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
3742 Bool caseSensitive)
3744 char *ptr;
3746 if(!haystack || !needle)
3747 return NULL;
3749 for (ptr = haystack-2; ptr > end; ptr--) {
3750 if(caseSensitive) {
3751 if (*ptr == *needle && !strncmp(ptr, needle, len))
3752 return ptr;
3753 } else {
3754 if (tolower(*ptr) == tolower(*needle) &&
3755 strncasecmp(ptr, needle, len))
3756 return ptr;
3760 return NULL;
3764 Bool
3765 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3766 Bool caseSensitive)
3768 TextBlock *tb;
3769 char *mark;
3770 unsigned short pos;
3772 if (!tPtr || !needle)
3773 return False;
3775 if (! (tb = tPtr->currentTextBlock)) {
3776 if (! (tb = ( (direction > 0) ?
3777 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3778 return False;
3780 } else {
3781 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3782 tb = (direction>0) ? tb->next : tb->prior; */
3783 if(tb != tPtr->lastTextBlock)
3784 tb = tb->prior;
3788 while(tb) {
3789 if (!tb->graphic) {
3790 pos = tPtr->tpos;
3791 if(pos+1 < tb->used)
3792 pos++;
3794 if(tb->used - pos> 0 && pos > 0) {
3795 char tmp = tb->text[tb->used];
3796 tb->text[tb->used] = 0;
3798 output(&tb->text[pos], tb->used - pos);
3799 if(direction > 0)
3800 mark = strstr(&tb->text[pos], needle);
3801 else
3802 mark = mystrrstr(&tb->text[pos], needle,
3803 strlen(needle), tb->text, caseSensitive);
3805 tb->text[tb->used] = tmp;
3807 } else {
3808 return False;
3811 if(mark) {
3812 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3814 tPtr->tpos = (int)(mark - tb->text);
3815 tPtr->currentTextBlock = tb;
3816 updateCursorPosition(tPtr);
3817 tPtr->sel.y = tPtr->cursor.y+5;
3818 tPtr->sel.h = tPtr->cursor.h-10;
3819 tPtr->sel.x = tPtr->cursor.x +1;
3820 tPtr->sel.w = WMIN(WMWidthOfString(font,
3821 &tb->text[tPtr->tpos], strlen(needle)),
3822 tPtr->docWidth - tPtr->sel.x);
3823 tPtr->flags.ownsSelection = True;
3824 paintText(tPtr);
3826 return True;
3830 tb = (direction>0) ? tb->next : tb->prior;
3831 pos = 0;
3834 return False;