- Test of log
[wmaker-crm.git] / WINGs / wtext.c
blobe02b1ac61fb16a3de24295cc84ee877c99dd5a4d
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 * - 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 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 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 (!tPtr->flags.monoFont && 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 (!tPtr->flags.monoFont && 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;
849 for(j=0; j<c; j++) {
850 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
852 if(!tb || !tb->sections) {
853 tPtr->flags.isOverGraphic = 0;
854 return;
857 if(!tb->object) {
858 if(tb->sections[0].x <= x
859 && tb->sections[0].y <= y
860 && tb->sections[0].x + tb->sections[0].w >= x
861 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
862 tPtr->flags.isOverGraphic = 3;
863 result = True;
864 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;
1317 break;
1319 case WSIncrementLine: {
1320 int limit = tPtr->docHeight - height;
1321 if (tPtr->vpos < limit) {
1322 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1323 else tPtr->vpos=limit;
1324 scroll = True;
1327 break;
1329 case WSDecrementPage:
1330 if(((int)tPtr->vpos - (int)height) >= 0)
1331 tPtr->vpos -= height;
1332 else
1333 tPtr->vpos = 0;
1335 scroll = True;
1336 break;
1338 case WSIncrementPage:
1339 tPtr->vpos += height;
1340 if (tPtr->vpos > (tPtr->docHeight - height))
1341 tPtr->vpos = tPtr->docHeight - height;
1342 scroll = True;
1343 break;
1346 case WSKnob:
1347 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1348 * (float)(tPtr->docHeight - height);
1349 scroll = True;
1350 break;
1352 case WSKnobSlot:
1353 case WSNoPart:
1354 break;
1356 scroll = (tPtr->vpos != tPtr->prevVpos);
1357 tPtr->prevVpos = tPtr->vpos;
1361 if (w == tPtr->hS) {
1362 int width = tPtr->visible.w;
1364 which = WMGetScrollerHitPart(tPtr->hS);
1365 switch(which) {
1367 case WSDecrementLine:
1368 if (tPtr->hpos > 0) {
1369 if (tPtr->hpos>16) tPtr->hpos-=16;
1370 else tPtr->hpos=0;
1371 scroll=True;
1372 }break;
1374 case WSIncrementLine: {
1375 int limit = tPtr->docWidth - width;
1376 if (tPtr->hpos < limit) {
1377 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1378 else tPtr->hpos=limit;
1379 scroll = True;
1380 }}break;
1382 case WSDecrementPage:
1383 if(((int)tPtr->hpos - (int)width) >= 0)
1384 tPtr->hpos -= width;
1385 else
1386 tPtr->hpos = 0;
1388 scroll = True;
1389 break;
1391 case WSIncrementPage:
1392 tPtr->hpos += width;
1393 if (tPtr->hpos > (tPtr->docWidth - width))
1394 tPtr->hpos = tPtr->docWidth - width;
1395 scroll = True;
1396 break;
1399 case WSKnob:
1400 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1401 * (float)(tPtr->docWidth - width);
1402 scroll = True;
1403 break;
1405 case WSKnobSlot:
1406 case WSNoPart:
1407 break;
1409 scroll = (tPtr->hpos != tPtr->prevHpos);
1410 tPtr->prevHpos = tPtr->hpos;
1413 if (scroll) {
1414 updateScrollers(tPtr);
1415 paintText(tPtr);
1421 typedef struct {
1422 TextBlock *tb;
1423 unsigned short begin, end; /* what part of the text block */
1424 } myLineItems;
1427 static int
1428 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1430 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1431 WMFont *font;
1432 char *text;
1433 TextBlock *tb, *tbsame=NULL;
1435 if(!items || nitems == 0)
1436 return 0;
1438 for(i=0; i<nitems; i++) {
1439 tb = items[i].tb;
1441 if (tb->graphic) {
1442 if (!tPtr->flags.monoFont) {
1443 if(tb->object) {
1444 WMWidget *wdt = tb->d.widget;
1445 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1446 if (tPtr->flags.alignment != WALeft)
1447 lw += WMWidgetWidth(wdt);
1448 } else {
1449 line_height = WMAX(line_height,
1450 tb->d.pixmap->height + max_d);
1451 if (tPtr->flags.alignment != WALeft)
1452 lw += tb->d.pixmap->width;
1456 } else {
1457 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1458 max_d = WMAX(max_d, abs(font->height-font->y));
1459 line_height = WMAX(line_height, font->height + max_d);
1460 text = &(tb->text[items[i].begin]);
1461 len = items[i].end - items[i].begin;
1462 if (tPtr->flags.alignment != WALeft)
1463 lw += WMWidthOfString(font, text, len);
1467 if (tPtr->flags.alignment == WARight) {
1468 j = tPtr->visible.w - lw;
1469 } else if (tPtr->flags.alignment == WACenter) {
1470 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1473 for(i=0; i<nitems; i++) {
1474 tb = items[i].tb;
1476 if (tbsame == tb) { /* extend it, since it's on same line */
1477 tb->sections[tb->nsections-1].end = items[i].end;
1478 n = tb->nsections-1;
1479 } else {
1480 tb->sections = wrealloc(tb->sections,
1481 (++tb->nsections)*sizeof(Section));
1482 n = tb->nsections-1;
1483 tb->sections[n]._y = y + max_d;
1484 tb->sections[n].max_d = max_d;
1485 tb->sections[n].x = x+j;
1486 tb->sections[n].h = line_height;
1487 tb->sections[n].begin = items[i].begin;
1488 tb->sections[n].end = items[i].end;
1491 tb->sections[n].last = (i+1 == nitems);
1493 if (tb->graphic) {
1494 if (!tPtr->flags.monoFont) {
1495 if(tb->object) {
1496 WMWidget *wdt = tb->d.widget;
1497 tb->sections[n].y = max_d + y
1498 + line_height - WMWidgetHeight(wdt);
1499 tb->sections[n].w = WMWidgetWidth(wdt);
1500 } else {
1501 tb->sections[n].y = y + line_height
1502 + max_d - tb->d.pixmap->height;
1503 tb->sections[n].w = tb->d.pixmap->width;
1505 x += tb->sections[n].w;
1507 } else {
1508 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1509 len = items[i].end - items[i].begin;
1510 text = &(tb->text[items[i].begin]);
1512 tb->sections[n].y = y+line_height-font->y;
1513 tb->sections[n].w =
1514 WMWidthOfString(font,
1515 &(tb->text[tb->sections[n].begin]),
1516 tb->sections[n].end - tb->sections[n].begin);
1518 x += WMWidthOfString(font, text, len);
1521 tbsame = tb;
1524 return line_height;
1529 static void
1530 layOutDocument(Text *tPtr)
1532 TextBlock *tb;
1533 myLineItems *items = NULL;
1534 unsigned int itemsSize=0, nitems=0, begin, end;
1535 WMFont *font;
1536 unsigned int x, y=0, lw = 0, width=0, bmargin;
1537 char *start=NULL, *mark=NULL;
1539 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1540 return;
1542 tPtr->docWidth = tPtr->visible.w;
1543 x = tPtr->margins[tb->marginN].first;
1544 bmargin = tPtr->margins[tb->marginN].body;
1546 /* only partial layOut needed: re-Lay only affected textblocks */
1547 if (tPtr->flags.laidOut) {
1548 tb = tPtr->currentTextBlock;
1550 /* search backwards for textblocks on same line */
1551 while (tb->prior) {
1552 if (!tb->sections || tb->nsections<1) {
1553 tb = tPtr->firstTextBlock;
1554 tPtr->flags.laidOut = False;
1555 y = 0;
1556 goto _layOut;
1559 if(!tb->prior->sections || tb->prior->nsections<1) {
1560 tb = tPtr->firstTextBlock;
1561 tPtr->flags.laidOut = False;
1562 y = 0;
1563 goto _layOut;
1566 if (tb->sections[0]._y !=
1567 tb->prior->sections[tb->prior->nsections-1]._y) {
1568 break;
1570 tb = tb->prior;
1573 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1574 y = tb->prior->sections[tb->prior->nsections-1]._y +
1575 tb->prior->sections[tb->prior->nsections-1].h -
1576 tb->prior->sections[tb->prior->nsections-1].max_d;
1577 } else {
1578 y = 0;
1582 _layOut:
1583 while (tb) {
1585 if (tb->sections && tb->nsections>0) {
1586 wfree(tb->sections);
1587 tb->sections = NULL;
1588 tb->nsections = 0;
1591 if (tb->blank && tb->next && !tb->next->first) {
1592 TextBlock *next = tb->next;
1593 tPtr->currentTextBlock = tb;
1594 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1595 tb = next;
1596 tb->first = True;
1597 continue;
1600 if (tb->first && tb != tPtr->firstTextBlock) {
1601 y += layOutLine(tPtr, items, nitems, x, y);
1602 x = tPtr->margins[tb->marginN].first;
1603 bmargin = tPtr->margins[tb->marginN].body;
1604 nitems = 0;
1605 lw = 0;
1608 if (tb->graphic) {
1609 if (!tPtr->flags.monoFont) {
1610 if(tb->object)
1611 width = WMWidgetWidth(tb->d.widget);
1612 else
1613 width = tb->d.pixmap->width;
1615 if (width > tPtr->docWidth)
1616 tPtr->docWidth = width;
1618 lw += width;
1619 if (lw >= tPtr->visible.w - x ) {
1620 y += layOutLine(tPtr, items, nitems, x, y);
1621 nitems = 0;
1622 x = bmargin;
1623 lw = width;
1626 if(nitems + 1> itemsSize) {
1627 items = wrealloc(items,
1628 (++itemsSize)*sizeof(myLineItems));
1631 items[nitems].tb = tb;
1632 items[nitems].begin = 0;
1633 items[nitems].end = 0;
1634 nitems++;
1637 } else if ((start = tb->text)) {
1638 begin = end = 0;
1639 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1641 while (start) {
1642 mark = strchr(start, ' ');
1643 if (mark) {
1644 end += (int)(mark-start)+1;
1645 start = mark+1;
1646 } else {
1647 end += strlen(start);
1648 start = mark;
1651 if (end > tb->used)
1652 end = tb->used;
1654 if (end-begin > 0) {
1656 width = WMWidthOfString(font,
1657 &tb->text[begin], end-begin);
1659 /* if it won't fit, break it up */
1660 if (width > tPtr->visible.w) {
1661 char *t = &tb->text[begin];
1662 int l=end-begin, i=0;
1663 do {
1664 width = WMWidthOfString(font, t, ++i);
1665 } while (width < tPtr->visible.w && i < l);
1666 end = begin+i;
1667 if (start)
1668 start -= l-i;
1671 lw += width;
1674 if (lw >= tPtr->visible.w - x) {
1675 y += layOutLine(tPtr, items, nitems, x, y);
1676 lw = width;
1677 x = bmargin;
1678 nitems = 0;
1681 if(nitems + 1 > itemsSize) {
1682 items = wrealloc(items,
1683 (++itemsSize)*sizeof(myLineItems));
1686 items[nitems].tb = tb;
1687 items[nitems].begin = begin;
1688 items[nitems].end = end;
1689 nitems++;
1691 begin = end;
1696 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1697 if(0&&tPtr->flags.laidOut
1698 && tb->next && tb->next->sections && tb->next->nsections>0
1699 && (tPtr->vpos + tPtr->visible.h
1700 < tb->next->sections[0]._y)) {
1701 if(tPtr->lastTextBlock->sections
1702 && tPtr->lastTextBlock->nsections > 0 ) {
1703 TextBlock *ltb = tPtr->lastTextBlock;
1704 int ly = ltb->sections[ltb->nsections-1]._y;
1705 int lh = ltb->sections[ltb->nsections-1].h;
1706 int ss, sd;
1708 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1709 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1711 y += layOutLine(tPtr, items, nitems, x, y);
1712 ss= ly+lh-y;
1713 sd = tPtr->docHeight-y;
1715 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1716 y += tb->next->sections[0]._y-y;
1717 nitems = 0;
1718 printf("nitems%d\n", nitems);
1719 if(ss-sd!=0)
1720 y = tPtr->docHeight+ss-sd;
1722 break;
1723 } else {
1724 tPtr->flags.laidOut = False;
1728 tb = tb->next;
1732 if (nitems > 0)
1733 y += layOutLine(tPtr, items, nitems, x, y);
1735 if (tPtr->docHeight != y+10) {
1736 tPtr->docHeight = y+10;
1737 updateScrollers(tPtr);
1740 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1741 XEvent event;
1743 tPtr->flags.horizOnDemand = True;
1744 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1745 event.type = Expose;
1746 handleEvents(&event, (void *)tPtr);
1748 } else if(tPtr->docWidth <= tPtr->visible.w
1749 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1750 tPtr->flags.horizOnDemand = False;
1751 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1754 tPtr->flags.laidOut = True;
1756 if(items && itemsSize > 0)
1757 wfree(items);
1761 static void
1762 textDidResize(W_ViewDelegate *self, WMView *view)
1764 Text *tPtr = (Text *)view->self;
1765 unsigned short w = tPtr->view->size.width;
1766 unsigned short h = tPtr->view->size.height;
1767 unsigned short rh = 0, vw = 0, rel;
1769 rel = (tPtr->flags.relief == WRFlat);
1771 if (tPtr->ruler && tPtr->flags.rulerShown) {
1772 WMMoveWidget(tPtr->ruler, 2, 2);
1773 WMResizeWidget(tPtr->ruler, w - 4, 40);
1774 rh = 40;
1777 if (tPtr->vS) {
1778 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1779 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1780 vw = 20;
1781 WMSetRulerOffset(tPtr->ruler,22);
1782 } else WMSetRulerOffset(tPtr->ruler, 2);
1784 if (tPtr->hS) {
1785 if (tPtr->vS) {
1786 WMMoveWidget(tPtr->hS, vw, h - 21);
1787 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1788 } else {
1789 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1790 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1794 tPtr->visible.x = (tPtr->vS)?24:4;
1795 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1796 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1797 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1798 tPtr->visible.h -= (tPtr->hS)?20:0;
1799 tPtr->margins[0].right = tPtr->visible.w;
1801 if (tPtr->view->flags.realized) {
1803 if (tPtr->db) {
1804 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1805 tPtr->db = (Pixmap) NULL;
1808 if (tPtr->visible.w < 40)
1809 tPtr->visible.w = 40;
1810 if (tPtr->visible.h < 20)
1811 tPtr->visible.h = 20;
1813 if(!tPtr->db) {
1814 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1815 tPtr->view->window, tPtr->visible.w,
1816 tPtr->visible.h, tPtr->view->screen->depth);
1820 WMThawText(tPtr);
1823 W_ViewDelegate _TextViewDelegate =
1825 NULL,
1826 NULL,
1827 textDidResize,
1828 NULL,
1831 /* nice, divisble-by-16 blocks */
1832 static inline unsigned short
1833 reqBlockSize(unsigned short requested)
1835 return requested + 16 - (requested%16);
1839 static void
1840 clearText(Text *tPtr)
1842 tPtr->vpos = tPtr->hpos = 0;
1843 tPtr->docHeight = tPtr->docWidth = 0;
1845 if (!tPtr->firstTextBlock)
1846 return;
1848 while (tPtr->currentTextBlock)
1849 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1851 tPtr->firstTextBlock = NULL;
1852 tPtr->currentTextBlock = NULL;
1853 tPtr->lastTextBlock = NULL;
1854 WMEmptyArray(tPtr->gfxItems);
1857 static void
1858 deleteTextInteractively(Text *tPtr, KeySym ksym)
1860 TextBlock *tb;
1861 Bool back = (Bool) (ksym == XK_BackSpace);
1862 Bool done = 1;
1863 Bool wasFirst = 0;
1865 if (!tPtr->flags.editable) {
1866 XBell(tPtr->view->screen->display, 0);
1867 return;
1870 if ( !(tb = tPtr->currentTextBlock) )
1871 return;
1873 if (tPtr->flags.ownsSelection) {
1874 if(removeSelection(tPtr))
1875 layOutDocument(tPtr);
1876 return;
1879 wasFirst = tb->first;
1880 if (back && tPtr->tpos < 1) {
1881 if (tb->prior) {
1882 if(tb->prior->blank) {
1883 tPtr->currentTextBlock = tb->prior;
1884 WMRemoveTextBlock(tPtr);
1885 tPtr->currentTextBlock = tb;
1886 tb->first = True;
1887 layOutDocument(tPtr);
1888 return;
1889 } else {
1890 if(tb->blank) {
1891 TextBlock *prior = tb->prior;
1892 tPtr->currentTextBlock = tb;
1893 WMRemoveTextBlock(tPtr);
1894 tb = prior;
1895 } else {
1896 tb = tb->prior;
1899 tPtr->tpos = tb->used;
1900 tPtr->currentTextBlock = tb;
1901 done = 1;
1902 if(wasFirst) {
1903 if(tb->next)
1904 tb->next->first = False;
1905 layOutDocument(tPtr);
1906 return;
1912 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1913 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1914 if (back)
1915 tPtr->tpos--;
1916 memmove(&(tb->text[tPtr->tpos]),
1917 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1918 tb->used--;
1919 done = 0;
1922 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1923 || tb->graphic) {
1925 TextBlock *sibling = (back? tb->prior : tb->next);
1927 if(tb->used == 0 || tb->graphic)
1928 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1930 if (sibling) {
1931 tPtr->currentTextBlock = sibling;
1932 tPtr->tpos = (back? sibling->used : 0);
1936 layOutDocument(tPtr);
1940 static void
1941 insertTextInteractively(Text *tPtr, char *text, int len)
1943 TextBlock *tb;
1944 char *newline = NULL;
1946 if (!tPtr->flags.editable) {
1947 XBell(tPtr->view->screen->display, 0);
1948 return;
1951 if (len < 1 || !text)
1952 return;
1955 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1956 return;
1959 if (tPtr->flags.ownsSelection)
1960 removeSelection(tPtr);
1963 if (tPtr->flags.ignoreNewLine) {
1964 int i;
1965 for(i=0; i<len; i++) {
1966 if (text[i] == '\n')
1967 text[i] = ' ';
1971 tb = tPtr->currentTextBlock;
1972 if (!tb || tb->graphic) {
1973 tPtr->tpos = 0;
1974 WMAppendTextStream(tPtr, text);
1975 layOutDocument(tPtr);
1976 return;
1979 if ((newline = strchr(text, '\n'))) {
1980 int nlen = (int)(newline-text);
1981 int s = tb->used - tPtr->tpos;
1982 char save[s];
1983 if (!tb->blank && nlen>0) {
1984 if (s > 0) {
1985 memcpy(save, &tb->text[tPtr->tpos], s);
1986 tb->used -= (tb->used - tPtr->tpos);
1988 insertTextInteractively(tPtr, text, nlen);
1989 newline++;
1990 WMAppendTextStream(tPtr, newline);
1991 if (s>0)
1992 insertTextInteractively(tPtr, save, s);
1994 } else {
1995 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1996 && !tb->graphic && tb->text) {
1998 void *ntb = WMCreateTextBlockWithText(
1999 tPtr, &tb->text[tPtr->tpos],
2000 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2001 tb->used = tPtr->tpos;
2002 WMAppendTextBlock(tPtr, ntb);
2003 tPtr->tpos = 0;
2005 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2006 if(tPtr->flags.indentNewLine) {
2007 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2008 " ", tb->d.font, tb->color, True, 4));
2009 tPtr->tpos = 4;
2010 } else {
2011 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2012 NULL, tb->d.font, tb->color, True, 0));
2013 tPtr->tpos = 0;
2018 } else {
2019 if (tb->used + len >= tb->allocated) {
2020 tb->allocated = reqBlockSize(tb->used+len);
2021 tb->text = wrealloc(tb->text, tb->allocated);
2024 if (tb->blank) {
2025 memcpy(tb->text, text, len);
2026 tb->used = len;
2027 tPtr->tpos = len;
2028 tb->text[tb->used] = 0;
2029 tb->blank = False;
2031 } else {
2032 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2033 tb->used-tPtr->tpos+1);
2034 memmove(&tb->text[tPtr->tpos], text, len);
2035 tb->used += len;
2036 tPtr->tpos += len;
2037 tb->text[tb->used] = 0;
2042 layOutDocument(tPtr);
2046 static void
2047 selectRegion(Text *tPtr, int x, int y)
2050 if (x < 0 || y < 0)
2051 return;
2053 y += (tPtr->flags.rulerShown? 40: 0);
2054 y += tPtr->vpos;
2055 if (y>10)
2056 y -= 10; /* the original offset */
2058 x -= tPtr->visible.x-2;
2059 if (x<0)
2060 x=0;
2062 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2063 tPtr->sel.w = abs(tPtr->clicked.x - x);
2064 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2065 tPtr->sel.h = abs(tPtr->clicked.y - y);
2067 tPtr->flags.ownsSelection = True;
2068 paintText(tPtr);
2072 static void
2073 releaseSelection(Text *tPtr)
2075 TextBlock *tb = tPtr->firstTextBlock;
2077 while(tb) {
2078 tb->selected = False;
2079 tb = tb->next;
2081 tPtr->flags.ownsSelection = False;
2082 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2083 CurrentTime);
2085 paintText(tPtr);
2089 WMData*
2090 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2091 Atom *type)
2093 Text *tPtr = view->self;
2094 Display *dpy = tPtr->view->screen->display;
2095 Atom _TARGETS;
2096 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2097 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2098 WMData *data = NULL;
2101 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2102 char *text = WMGetTextSelectedStream(tPtr);
2104 if (text) {
2105 printf("got text [%s]\n", text);
2106 data = WMCreateDataWithBytes(text, strlen(text));
2107 WMSetDataFormat(data, TYPETEXT);
2109 *type = target;
2110 return data;
2111 } else printf("didn't get it\n");
2113 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2114 if (target == _TARGETS) {
2115 Atom *ptr;
2117 ptr = wmalloc(4 * sizeof(Atom));
2118 ptr[0] = _TARGETS;
2119 ptr[1] = XA_STRING;
2120 ptr[2] = TEXT;
2121 ptr[3] = COMPOUND_TEXT;
2123 data = WMCreateDataWithBytes(ptr, 4*4);
2124 WMSetDataFormat(data, 32);
2126 *type = target;
2127 return data;
2130 return NULL;
2133 static void
2134 lostHandler(WMView *view, Atom selection, void *cdata)
2136 releaseSelection((WMText *)view->self);
2139 static WMSelectionProcs selectionHandler = {
2140 requestHandler, lostHandler, NULL
2144 static void
2145 ownershipObserver(void *observerData, WMNotification *notification)
2147 if (observerData != WMGetNotificationClientData(notification))
2148 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2152 static void
2153 fontChanged(void *observerData, WMNotification *notification)
2155 WMText *tPtr = (WMText *) observerData;
2156 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2157 printf("fontChanged\n");
2159 if(!tPtr || !font)
2160 return;
2162 if (tPtr->flags.ownsSelection)
2163 WMSetTextSelectionFont(tPtr, font);
2167 static void
2168 handleTextKeyPress(Text *tPtr, XEvent *event)
2170 char buffer[2];
2171 KeySym ksym;
2172 int control_pressed = False;
2173 TextBlock *tb = NULL;
2175 if (((XKeyEvent *) event)->state & ControlMask)
2176 control_pressed = True;
2177 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2179 switch(ksym) {
2181 case XK_Left:
2182 if(!(tb = tPtr->currentTextBlock))
2183 break;
2184 if(tb->graphic)
2185 goto L_imaGFX;
2187 if(tPtr->tpos==0) {
2188 L_imaGFX: if(tb->prior) {
2189 tPtr->currentTextBlock = tb->prior;
2190 tPtr->tpos = tPtr->currentTextBlock->used -1;
2191 } else tPtr->tpos = 0;
2192 } else tPtr->tpos--;
2193 updateCursorPosition(tPtr);
2194 paintText(tPtr);
2195 break;
2197 case XK_Right:
2198 if(!(tb = tPtr->currentTextBlock))
2199 break;
2200 if(tb->graphic)
2201 goto R_imaGFX;
2202 if(tPtr->tpos == tb->used) {
2203 R_imaGFX: if(tb->next) {
2204 tPtr->currentTextBlock = tb->next;
2205 tPtr->tpos = 1;
2206 } else tPtr->tpos = tb->used;
2207 } else tPtr->tpos++;
2208 updateCursorPosition(tPtr);
2209 paintText(tPtr);
2210 break;
2212 case XK_Down:
2213 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2214 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2215 paintText(tPtr);
2216 break;
2218 case XK_Up:
2219 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2220 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2221 paintText(tPtr);
2222 break;
2224 case XK_BackSpace:
2225 case XK_Delete:
2226 case XK_KP_Delete:
2227 deleteTextInteractively(tPtr, ksym);
2228 updateCursorPosition(tPtr);
2229 paintText(tPtr);
2230 break;
2232 case XK_Control_R :
2233 case XK_Control_L :
2234 control_pressed = True;
2235 break;
2237 case XK_Tab:
2238 insertTextInteractively(tPtr, " ", 4);
2239 updateCursorPosition(tPtr);
2240 paintText(tPtr);
2241 break;
2243 case XK_Return:
2244 buffer[0] = '\n';
2245 default:
2246 if (buffer[0] != 0 && !control_pressed) {
2247 insertTextInteractively(tPtr, buffer, 1);
2248 updateCursorPosition(tPtr);
2249 paintText(tPtr);
2251 } else if (control_pressed && ksym==XK_r) {
2252 Bool i = !tPtr->flags.rulerShown;
2253 WMShowTextRuler(tPtr, i);
2254 tPtr->flags.rulerShown = i;
2256 else if (control_pressed && buffer[0] == '\a')
2257 XBell(tPtr->view->screen->display, 0);
2260 if (!control_pressed && tPtr->flags.ownsSelection)
2261 releaseSelection(tPtr);
2264 static void
2265 handleWidgetPress(XEvent *event, void *data)
2267 TextBlock *tb = (TextBlock *)data;
2268 Text *tPtr;
2269 WMWidget *w;
2271 if (!tb)
2272 return;
2274 tPtr = (Text*)w;
2275 tPtr->currentTextBlock = tb;
2276 tPtr->flags.isOverGraphic = 2;
2277 tPtr->tpos = 0;
2278 output(tb->text, tb->used);
2279 #if 0
2280 if (!tPtr->flags.focused) {
2281 WMSetFocusToWidget(tPtr);
2282 tPtr->flags.focused = True;
2284 #endif
2288 static void
2289 handleActionEvents(XEvent *event, void *data)
2291 Text *tPtr = (Text *)data;
2292 Display *dpy = event->xany.display;
2293 KeySym ksym;
2296 switch (event->type) {
2297 case KeyPress:
2298 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2299 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2300 tPtr->flags.extendSelection = True;
2301 return;
2304 if (tPtr->flags.focused) {
2305 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2306 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2307 GrabModeAsync, GrabModeAsync, None,
2308 tPtr->view->screen->invisibleCursor, CurrentTime);
2309 tPtr->flags.pointerGrabbed = True;
2310 handleTextKeyPress(tPtr, event);
2312 } break;
2314 case KeyRelease:
2315 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2316 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2317 tPtr->flags.extendSelection = False;
2318 return;
2319 /* end modify flag so selection can be extended */
2321 break;
2324 case MotionNotify:
2326 if (tPtr->flags.pointerGrabbed) {
2327 tPtr->flags.pointerGrabbed = False;
2328 XUngrabPointer(dpy, CurrentTime);
2331 if(tPtr->flags.waitingForSelection)
2332 break;
2334 if ((event->xmotion.state & Button1Mask)) {
2335 if (!tPtr->flags.ownsSelection) {
2336 WMCreateSelectionHandler(tPtr->view,
2337 XA_PRIMARY, event->xbutton.time,
2338 &selectionHandler, NULL);
2339 tPtr->flags.ownsSelection = True;
2341 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2342 break;
2345 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2346 break;
2349 case ButtonPress:
2351 if (tPtr->flags.pointerGrabbed) {
2352 tPtr->flags.pointerGrabbed = False;
2353 XUngrabPointer(dpy, CurrentTime);
2354 break;
2357 if (tPtr->flags.waitingForSelection)
2358 break;
2360 if (tPtr->flags.extendSelection) {
2361 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2362 return;
2366 if (event->xbutton.button == Button1) {
2368 if(WMIsDoubleClick(event)) {
2369 TextBlock *tb = tPtr->currentTextBlock;
2371 if(tb && tb->graphic && !tb->object) {
2372 char desc[tb->used+1];
2373 memcpy(desc, tb->text, tb->used);
2374 desc[tb->used] = 0;
2375 if(tPtr->delegate) {
2376 if(tPtr->delegate->didDoubleClickOnPicture)
2377 (*tPtr->delegate->didDoubleClickOnPicture)
2378 (tPtr->delegate, desc);
2380 } else {
2381 autoSelectText(tPtr, 2);
2383 tPtr->lastClickTime = event->xbutton.time;
2384 break;
2385 } else if(event->xbutton.time - tPtr->lastClickTime
2386 < WINGsConfiguration.doubleClickDelay) {
2387 autoSelectText(tPtr, 3);
2388 break;
2391 if (!tPtr->flags.focused) {
2392 WMSetFocusToWidget(tPtr);
2393 tPtr->flags.focused = True;
2396 if (tPtr->flags.ownsSelection)
2397 releaseSelection(tPtr);
2399 tPtr->lastClickTime = event->xbutton.time;
2400 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2401 paintText(tPtr);
2404 if (event->xbutton.button
2405 == WINGsConfiguration.mouseWheelDown) {
2406 WMScrollText(tPtr, 16);
2407 break;
2410 if (event->xbutton.button
2411 == WINGsConfiguration.mouseWheelUp) {
2412 WMScrollText(tPtr, -16);
2413 break;
2416 if (event->xbutton.button == Button2) {
2417 char *text = NULL;
2418 int n;
2420 if (!tPtr->flags.editable) {
2421 XBell(dpy, 0);
2422 break;
2425 #if 0
2426 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2427 event->xbutton.time, pasteText, NULL)) {
2428 #endif
2431 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2433 if (text) {
2434 text[n] = 0;
2436 if (tPtr->parser)
2437 (tPtr->parser) (tPtr, (void *) text);
2438 else
2439 insertTextInteractively(tPtr, text, n);
2441 XFree(text);
2442 #if 0
2443 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2444 (void*)WMInsertTextEvent);
2445 #endif
2447 } else {
2448 tPtr->flags.waitingForSelection = True;
2451 break;
2455 case ButtonRelease:
2457 if (tPtr->flags.pointerGrabbed) {
2458 tPtr->flags.pointerGrabbed = False;
2459 XUngrabPointer(dpy, CurrentTime);
2460 break;
2463 if (tPtr->flags.waitingForSelection)
2464 break;
2470 static void
2471 handleEvents(XEvent *event, void *data)
2473 Text *tPtr = (Text *)data;
2475 switch(event->type) {
2476 case Expose:
2478 if (event->xexpose.count!=0)
2479 break;
2481 if(tPtr->hS) {
2482 if (!(W_VIEW(tPtr->hS))->flags.realized)
2483 WMRealizeWidget(tPtr->hS);
2486 if(tPtr->vS) {
2487 if (!(W_VIEW(tPtr->vS))->flags.realized)
2488 WMRealizeWidget(tPtr->vS);
2491 if(tPtr->ruler) {
2492 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2493 WMRealizeWidget(tPtr->ruler);
2497 if(!tPtr->db)
2498 textDidResize(tPtr->view->delegate, tPtr->view);
2500 paintText(tPtr);
2501 break;
2503 case FocusIn:
2504 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2505 != tPtr->view)
2506 return;
2507 tPtr->flags.focused = True;
2508 #if DO_BLINK
2509 if (tPtr->flags.editable && !tPtr->timerID) {
2510 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2511 blinkCursor, tPtr);
2513 #endif
2515 break;
2517 case FocusOut:
2518 tPtr->flags.focused = False;
2519 paintText(tPtr);
2520 #if DO_BLINK
2521 if (tPtr->timerID) {
2522 WMDeleteTimerHandler(tPtr->timerID);
2523 tPtr->timerID = NULL;
2525 #endif
2526 break;
2529 case DestroyNotify:
2530 clearText(tPtr);
2531 if(tPtr->db)
2532 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2533 if(tPtr->gfxItems)
2534 WMEmptyArray(tPtr->gfxItems);
2535 #if DO_BLINK
2536 if (tPtr->timerID)
2537 WMDeleteTimerHandler(tPtr->timerID);
2538 #endif
2539 WMReleaseFont(tPtr->dFont);
2540 WMReleaseColor(tPtr->dColor);
2541 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2542 WMRemoveNotificationObserver(tPtr);
2544 wfree(tPtr);
2546 break;
2552 static void
2553 insertPlainText(Text *tPtr, char *text)
2555 char *start, *mark;
2556 void *tb = NULL;
2558 start = text;
2559 while (start) {
2560 mark = strchr(start, '\n');
2561 if (mark) {
2562 tb = WMCreateTextBlockWithText(tPtr,
2563 start, tPtr->dFont,
2564 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2565 start = mark+1;
2566 tPtr->flags.first = True;
2567 } else {
2568 if (start && strlen(start)) {
2569 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2570 tPtr->dColor, tPtr->flags.first, strlen(start));
2571 } else tb = NULL;
2572 tPtr->flags.first = False;
2573 start = mark;
2576 if (tPtr->flags.prepend)
2577 WMPrependTextBlock(tPtr, tb);
2578 else
2579 WMAppendTextBlock(tPtr, tb);
2581 return;
2586 static void
2587 rulerMoveCallBack(WMWidget *w, void *self)
2589 Text *tPtr = (Text *)self;
2590 if (!tPtr)
2591 return;
2592 if (W_CLASS(tPtr) != WC_Text)
2593 return;
2595 paintText(tPtr);
2599 static void
2600 rulerReleaseCallBack(WMWidget *w, void *self)
2602 Text *tPtr = (Text *)self;
2603 if (!tPtr)
2604 return;
2605 if (W_CLASS(tPtr) != WC_Text)
2606 return;
2608 WMThawText(tPtr);
2609 return;
2613 static unsigned
2614 draggingEntered(WMView *self, WMDraggingInfo *info)
2616 printf("draggingEntered\n");
2617 return WDOperationCopy;
2621 static unsigned
2622 draggingUpdated(WMView *self, WMDraggingInfo *info)
2624 return WDOperationCopy;
2628 static void
2629 draggingExited(WMView *self, WMDraggingInfo *info)
2631 printf("draggingExited\n");
2634 static Bool
2635 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2637 printf("prepareForDragOperation\n");
2638 return True;
2642 char *badbadbad;
2644 static void
2645 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2646 void *cdata, WMData *data)
2648 badbadbad = wstrdup((char *)WMDataBytes(data));
2652 /* when it's done in WINGs, remove this */
2654 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2656 WMScreen *scr = W_VIEW_SCREEN(view);
2658 if (!WMRequestSelection(scr->dragInfo.destView,
2659 scr->xdndSelectionAtom,
2660 XInternAtom(scr->display, type, False),
2661 scr->dragInfo.timestamp,
2662 receivedData, &scr->dragInfo)) {
2663 wwarning("could not request data for dropped data");
2668 XEvent ev;
2670 ev.type = ClientMessage;
2671 ev.xclient.message_type = scr->xdndFinishedAtom;
2672 ev.xclient.format = 32;
2673 ev.xclient.window = info->destinationWindow;
2674 ev.xclient.data.l[0] = 0;
2675 ev.xclient.data.l[1] = 0;
2676 ev.xclient.data.l[2] = 0;
2677 ev.xclient.data.l[3] = 0;
2678 ev.xclient.data.l[4] = 0;
2680 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2681 XFlush(scr->display);
2683 return True;
2686 static Bool
2687 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2689 WMColor *color;
2690 WMText *tPtr = (WMText *)self->self;
2692 if (!tPtr)
2693 return True;
2695 requestDroppedData(tPtr->view, info, "application/X-color");
2696 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2697 if(color) {
2698 WMSetTextSelectionColor(tPtr, color);
2699 WMReleaseColor(color);
2704 return True;
2707 static void
2708 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2710 printf("concludeDragOperation\n");
2714 static WMDragDestinationProcs _DragDestinationProcs = {
2715 draggingEntered,
2716 draggingUpdated,
2717 draggingExited,
2718 prepareForDragOperation,
2719 performDragOperation,
2720 concludeDragOperation
2724 char *
2725 getStream(WMText *tPtr, int sel, int array)
2727 TextBlock *tb = NULL;
2728 char *text = NULL;
2729 unsigned long where = 0;
2731 if (!tPtr)
2732 return NULL;
2734 if (!(tb = tPtr->firstTextBlock))
2735 return NULL;
2737 if (tPtr->writer) {
2738 (tPtr->writer) (tPtr, (void *) text);
2739 return text;
2742 tb = tPtr->firstTextBlock;
2743 while (tb) {
2745 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2747 if (!sel || (tb->graphic && tb->selected)) {
2749 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2750 && tb != tPtr->firstTextBlock) {
2751 text = wrealloc(text, where+1);
2752 text[where++] = '\n';
2755 if(tb->blank)
2756 goto _gSnext;
2758 if(tb->graphic && array) {
2759 text = wrealloc(text, where+4);
2760 text[where++] = 0xFA;
2761 text[where++] = (tb->used>>8)&0x0ff;
2762 text[where++] = tb->used&0x0ff;
2763 text[where++] = tb->allocated; /* extra info */
2765 text = wrealloc(text, where+tb->used);
2766 memcpy(&text[where], tb->text, tb->used);
2767 where += tb->used;
2770 } else if (sel && tb->selected) {
2772 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2773 && tb != tPtr->firstTextBlock) {
2774 text = wrealloc(text, where+1);
2775 text[where++] = '\n';
2778 if(tb->blank)
2779 goto _gSnext;
2781 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2782 memcpy(&text[where], &tb->text[tb->s_begin],
2783 tb->s_end - tb->s_begin);
2784 where += tb->s_end - tb->s_begin;
2789 _gSnext:tb = tb->next;
2792 /* +1 for the end of string, let's be nice */
2793 text = wrealloc(text, where+1);
2794 text[where] = 0;
2795 return text;
2799 static void
2800 releaseStreamObjects(void *data)
2802 if(data)
2803 wfree(data);
2806 WMArray *
2807 getStreamObjects(WMText *tPtr, int sel)
2809 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2810 WMData *data;
2811 char *stream;
2812 unsigned short len;
2813 char *start, *fa, *desc;
2815 stream = getStream(tPtr, sel, 1);
2816 if(!stream)
2817 return NULL;
2819 start = stream;
2820 while (start) {
2822 fa = strchr(start, 0xFA);
2823 if (fa) {
2824 if((int)(fa - start)>0) {
2825 desc = start;
2826 desc[(int)(fa - start)] = 0;
2827 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2828 WMSetDataFormat(data, TYPETEXT);
2829 WMAddToArray(array, (void *) data);
2832 len = *(fa+1)*0xff + *(fa+2);
2833 data = WMCreateDataWithBytes((void *)(fa+4), len);
2834 WMSetDataFormat(data, *(fa+3));
2835 WMAddToArray(array, (void *) data);
2836 start = fa + len + 4;
2838 } else {
2839 if (start && strlen(start)) {
2840 data = WMCreateDataWithBytes((void *)start, strlen(start));
2841 WMSetDataFormat(data, TYPETEXT);
2842 WMAddToArray(array, (void *) data);
2844 start = fa;
2848 wfree(stream);
2849 return array;
2850 WMFreeArray(array);
2854 WMText *
2855 WMCreateTextForDocumentType(WMWidget *parent,
2856 WMAction *parser, WMAction *writer)
2858 Text *tPtr = wmalloc(sizeof(Text));
2859 if (!tPtr) {
2860 printf("could not create text widget\n");
2861 return NULL;
2865 memset(tPtr, 0, sizeof(Text));
2866 tPtr->widgetClass = WC_Text;
2867 tPtr->view = W_CreateView(W_VIEW(parent));
2868 if (!tPtr->view) {
2869 perror("could not create text's view\n");
2870 wfree(tPtr);
2871 return NULL;
2873 tPtr->view->self = tPtr;
2874 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2875 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2876 W_ResizeView(tPtr->view, 250, 200);
2878 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2879 tPtr->bgGC = WMColorGC(tPtr->dColor);
2880 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2881 WMReleaseColor(tPtr->dColor);
2883 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2884 tPtr->fgGC = WMColorGC(tPtr->dColor);
2886 tPtr->ruler = NULL;
2887 tPtr->vS = NULL;
2888 tPtr->hS = NULL;
2890 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2892 tPtr->view->delegate = &_TextViewDelegate;
2894 tPtr->delegate = NULL;
2896 #if DO_BLINK
2897 tPtr->timerID = NULL;
2898 #endif
2900 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2901 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2902 handleEvents, tPtr);
2904 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2905 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2906 handleActionEvents, tPtr);
2908 WMAddNotificationObserver(ownershipObserver, tPtr,
2909 "_lostOwnership", tPtr);
2911 if(0){
2912 char *types[2] = {"application/X-color", NULL};
2913 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2914 WMRegisterViewForDraggedTypes(tPtr->view, types);
2917 WMAddNotificationObserver(fontChanged, tPtr,
2918 "WMFontPanelDidChangeNotification", tPtr);
2920 tPtr->firstTextBlock = NULL;
2921 tPtr->lastTextBlock = NULL;
2922 tPtr->currentTextBlock = NULL;
2923 tPtr->tpos = 0;
2925 tPtr->gfxItems = WMCreateArray(4);
2927 tPtr->parser = parser;
2928 tPtr->writer = writer;
2930 tPtr->sel.x = tPtr->sel.y = 2;
2931 tPtr->sel.w = tPtr->sel.h = 0;
2933 tPtr->clicked.x = tPtr->clicked.y = 2;
2935 tPtr->visible.x = tPtr->visible.y = 2;
2936 tPtr->visible.h = tPtr->view->size.height;
2937 tPtr->visible.w = tPtr->view->size.width - 4;
2939 tPtr->cursor.x = -23;
2941 tPtr->docWidth = 0;
2942 tPtr->docHeight = 0;
2943 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2944 default_bullet);
2945 tPtr->db = (Pixmap) NULL;
2947 tPtr->margins = WMGetRulerMargins(NULL);
2948 tPtr->margins->right = tPtr->visible.w;
2949 tPtr->nMargins = 1;
2951 tPtr->flags.rulerShown = False;
2952 tPtr->flags.monoFont = False;
2953 tPtr->flags.focused = False;
2954 tPtr->flags.editable = True;
2955 tPtr->flags.ownsSelection = False;
2956 tPtr->flags.pointerGrabbed = False;
2957 tPtr->flags.extendSelection = False;
2958 tPtr->flags.frozen = False;
2959 tPtr->flags.cursorShown = True;
2960 tPtr->flags.acceptsGraphic = False;
2961 tPtr->flags.horizOnDemand = False;
2962 tPtr->flags.needsLayOut = False;
2963 tPtr->flags.ignoreNewLine = False;
2964 tPtr->flags.indentNewLine = False;
2965 tPtr->flags.laidOut = False;
2966 tPtr->flags.waitingForSelection = False;
2967 tPtr->flags.prepend = False;
2968 tPtr->flags.isOverGraphic = False;
2969 tPtr->flags.relief = WRSunken;
2970 tPtr->flags.isOverGraphic = 0;
2971 tPtr->flags.alignment = WALeft;
2972 tPtr->flags.first = True;
2974 return tPtr;
2977 void
2978 WMPrependTextStream(WMText *tPtr, char *text)
2980 CHECK_CLASS(tPtr, WC_Text);
2982 if(!text) {
2983 if(tPtr->flags.ownsSelection)
2984 releaseSelection(tPtr);
2985 else {
2986 clearText(tPtr);
2987 updateScrollers(tPtr);
2989 return;
2992 tPtr->flags.prepend = True;
2993 if (text && tPtr->parser)
2994 (tPtr->parser) (tPtr, (void *) text);
2995 else
2996 insertPlainText(tPtr, text);
2998 tPtr->flags.needsLayOut = True;
2999 tPtr->tpos = 0;
3003 void
3004 WMAppendTextStream(WMText *tPtr, char *text)
3006 CHECK_CLASS(tPtr, WC_Text);
3008 if(!text) {
3009 if(tPtr->flags.ownsSelection)
3010 releaseSelection(tPtr);
3011 else {
3012 clearText(tPtr);
3013 updateScrollers(tPtr);
3015 return;
3018 tPtr->flags.prepend = False;
3019 if (text && tPtr->parser)
3020 (tPtr->parser) (tPtr, (void *) text);
3021 else
3022 insertPlainText(tPtr, text);
3024 tPtr->flags.needsLayOut = True;
3025 if(tPtr->currentTextBlock)
3026 tPtr->tpos = tPtr->currentTextBlock->used;
3032 char *
3033 WMGetTextStream(WMText *tPtr)
3035 CHECK_CLASS(tPtr, WC_Text);
3036 return getStream(tPtr, 0, 0);
3039 char *
3040 WMGetTextSelectedStream(WMText *tPtr)
3042 CHECK_CLASS(tPtr, WC_Text);
3043 return getStream(tPtr, 1, 0);
3046 WMArray *
3047 WMGetTextObjects(WMText *tPtr)
3049 CHECK_CLASS(tPtr, WC_Text);
3050 return getStreamObjects(tPtr, 0);
3053 WMArray *
3054 WMGetTextSelectedObjects(WMText *tPtr)
3056 CHECK_CLASS(tPtr, WC_Text);
3057 return getStreamObjects(tPtr, 1);
3061 void
3062 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3064 CHECK_CLASS(tPtr, WC_Text);
3066 tPtr->delegate = delegate;
3070 void *
3071 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3072 char *description, WMColor *color,
3073 unsigned short first, unsigned short extraInfo)
3075 TextBlock *tb;
3077 if (!w || !description || !color)
3078 return NULL;
3080 tb = wmalloc(sizeof(TextBlock));
3081 if (!tb)
3082 return NULL;
3084 tb->text = wstrdup(description);
3085 tb->used = strlen(description);
3086 tb->blank = False;
3087 tb->d.widget = w;
3088 tb->color = WMRetainColor(color);
3089 tb->marginN = newMargin(tPtr, NULL);
3090 tb->allocated = extraInfo;
3091 tb->first = first;
3092 tb->kanji = False;
3093 tb->graphic = True;
3094 tb->object = True;
3095 tb->underlined = False;
3096 tb->selected = False;
3097 tb->script = 0;
3098 tb->sections = NULL;
3099 tb->nsections = 0;
3100 tb->prior = NULL;
3101 tb->next = NULL;
3103 return tb;
3107 void *
3108 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3109 char *description, WMColor *color,
3110 unsigned short first, unsigned short extraInfo)
3112 TextBlock *tb;
3114 if (!p || !description || !color)
3115 return NULL;
3117 tb = wmalloc(sizeof(TextBlock));
3118 if (!tb)
3119 return NULL;
3121 tb->text = wstrdup(description);
3122 tb->used = strlen(description);
3123 tb->blank = False;
3124 tb->d.pixmap = WMRetainPixmap(p);
3125 tb->color = WMRetainColor(color);
3126 tb->marginN = newMargin(tPtr, NULL);
3127 tb->allocated = extraInfo;
3128 tb->first = first;
3129 tb->kanji = False;
3130 tb->graphic = True;
3131 tb->object = False;
3132 tb->underlined = False;
3133 tb->selected = False;
3134 tb->script = 0;
3135 tb->sections = NULL;
3136 tb->nsections = 0;
3137 tb->prior = NULL;
3138 tb->next = NULL;
3140 return tb;
3143 void *
3144 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3145 unsigned short first, unsigned short len)
3147 TextBlock *tb;
3149 if (!font || !color)
3150 return NULL;
3152 tb = wmalloc(sizeof(TextBlock));
3153 if (!tb)
3154 return NULL;
3156 tb->allocated = reqBlockSize(len);
3157 tb->text = (char *)wmalloc(tb->allocated);
3158 memset(tb->text, 0, tb->allocated);
3160 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3161 *tb->text = ' ';
3162 tb->used = 1;
3163 tb->blank = True;
3164 } else {
3165 memcpy(tb->text, text, len);
3166 tb->used = len;
3167 tb->blank = False;
3169 tb->text[tb->used] = 0;
3171 tb->d.font = WMRetainFont(font);
3172 tb->color = WMRetainColor(color);
3173 tb->marginN = newMargin(tPtr, NULL);
3174 tb->first = first;
3175 tb->kanji = False;
3176 tb->graphic = False;
3177 tb->underlined = False;
3178 tb->selected = False;
3179 tb->script = 0;
3180 tb->sections = NULL;
3181 tb->nsections = 0;
3182 tb->prior = NULL;
3183 tb->next = NULL;
3184 return tb;
3187 void
3188 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3189 unsigned int kanji, unsigned int underlined, int script,
3190 WMRulerMargins *margins)
3192 TextBlock *tb = (TextBlock *) vtb;
3193 if (!tb)
3194 return;
3196 tb->first = first;
3197 tb->kanji = kanji;
3198 tb->underlined = underlined;
3199 tb->script = script;
3200 tb->marginN = newMargin(tPtr, margins);
3203 void
3204 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3205 unsigned int *kanji, unsigned int *underlined, int *script,
3206 WMRulerMargins *margins)
3208 TextBlock *tb = (TextBlock *) vtb;
3209 if (!tb)
3210 return;
3212 if (first) *first = tb->first;
3213 if (kanji) *kanji = tb->kanji;
3214 if (underlined) *underlined = tb->underlined;
3215 if (script) *script = tb->script;
3216 if (margins) margins = &tPtr->margins[tb->marginN];
3221 void
3222 WMPrependTextBlock(WMText *tPtr, void *vtb)
3224 TextBlock *tb = (TextBlock *)vtb;
3226 if (!tPtr || !tb)
3227 return;
3229 if (tb->graphic) {
3230 if(tb->object) {
3231 WMWidget *w = tb->d.widget;
3232 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3233 handleWidgetPress, tb);
3234 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3235 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3236 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3239 WMAddToArray(tPtr->gfxItems, (void *)tb);
3240 tPtr->tpos = 0;
3241 } else tPtr->tpos = tb->used;
3243 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3244 tb->next = tb->prior = NULL;
3245 tb->first = True;
3246 tPtr->lastTextBlock = tPtr->firstTextBlock
3247 = tPtr->currentTextBlock = tb;
3248 return;
3251 if(!tb->first) {
3252 tb->marginN = tPtr->currentTextBlock->marginN;
3255 tb->next = tPtr->currentTextBlock;
3256 tb->prior = tPtr->currentTextBlock->prior;
3257 if (tPtr->currentTextBlock->prior)
3258 tPtr->currentTextBlock->prior->next = tb;
3260 tPtr->currentTextBlock->prior = tb;
3261 if (!tb->prior)
3262 tPtr->firstTextBlock = tb;
3264 tPtr->currentTextBlock = tb;
3268 void
3269 WMAppendTextBlock(WMText *tPtr, void *vtb)
3271 TextBlock *tb = (TextBlock *)vtb;
3273 if (!tPtr || !tb)
3274 return;
3276 if (tb->graphic) {
3277 if(tb->object) {
3278 WMWidget *w = tb->d.widget;
3279 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3280 handleWidgetPress, tb);
3281 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3282 (W_VIEW(w))->attribs.cursor =
3283 tPtr->view->screen->defaultCursor;
3284 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3287 WMAddToArray(tPtr->gfxItems, (void *)tb);
3288 tPtr->tpos = 0;
3289 } else tPtr->tpos = tb->used;
3291 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3292 tb->next = tb->prior = NULL;
3293 tb->first = True;
3294 tPtr->lastTextBlock = tPtr->firstTextBlock
3295 = tPtr->currentTextBlock = tb;
3296 return;
3299 if(!tb->first) {
3300 tb->marginN = tPtr->currentTextBlock->marginN;
3303 tb->next = tPtr->currentTextBlock->next;
3304 tb->prior = tPtr->currentTextBlock;
3305 if (tPtr->currentTextBlock->next)
3306 tPtr->currentTextBlock->next->prior = tb;
3308 tPtr->currentTextBlock->next = tb;
3310 if (!tb->next)
3311 tPtr->lastTextBlock = tb;
3313 tPtr->currentTextBlock = tb;
3316 void *
3317 WMRemoveTextBlock(WMText *tPtr)
3319 TextBlock *tb = NULL;
3321 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3322 || !tPtr->currentTextBlock) {
3323 printf("cannot remove non existent TextBlock!\b");
3324 return NULL;
3327 tb = tPtr->currentTextBlock;
3328 if (tb->graphic) {
3329 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3331 if(tb->object) {
3332 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3333 handleWidgetPress, tb);
3334 WMUnmapWidget(tb->d.widget);
3338 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3339 if (tPtr->currentTextBlock->next)
3340 tPtr->currentTextBlock->next->prior = NULL;
3342 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3343 tPtr->currentTextBlock = tPtr->firstTextBlock;
3345 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3346 tPtr->currentTextBlock->prior->next = NULL;
3347 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3348 tPtr->currentTextBlock = tPtr->lastTextBlock;
3349 } else {
3350 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3351 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3352 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3355 return (void *)tb;
3358 void
3359 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3361 TextBlock *tb = (TextBlock *)vtb;
3362 if (!tPtr || !tb)
3363 return;
3365 if (tb->graphic) {
3366 if(tb->object) {
3367 /* naturally, there's a danger to destroying
3368 widgets whose action brings us here:
3369 ie. press a button to destroy it... need to
3370 find a safer way. till then... this stays commented out */
3371 /* WMDestroyWidget(tb->d.widget);
3372 wfree(tb->d.widget); */
3373 tb->d.widget = NULL;
3374 } else {
3375 WMReleasePixmap(tb->d.pixmap);
3376 tb->d.pixmap = NULL;
3378 } else {
3379 WMReleaseFont(tb->d.font);
3382 WMReleaseColor(tb->color);
3383 if (tb->sections && tb->nsections > 0)
3384 wfree(tb->sections);
3385 wfree(tb->text);
3386 wfree(tb);
3387 tb = NULL;
3392 void
3393 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3395 if (!tPtr)
3396 return;
3398 if (color)
3399 tPtr->fgGC = WMColorGC(color);
3400 else
3401 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3403 paintText(tPtr);
3406 void
3407 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3409 if (!tPtr)
3410 return;
3412 if (color) {
3413 tPtr->bgGC = WMColorGC(color);
3414 W_SetViewBackgroundColor(tPtr->view, color);
3415 } else {
3416 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3417 W_SetViewBackgroundColor(tPtr->view,
3418 WMWhiteColor(tPtr->view->screen));
3421 paintText(tPtr);
3424 void
3425 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3427 if (!tPtr)
3428 return;
3429 tPtr->flags.relief = relief;
3430 textDidResize(tPtr->view->delegate, tPtr->view);
3433 void
3434 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3436 if (!tPtr)
3437 return;
3439 if (shouldhave && !tPtr->hS) {
3440 tPtr->hS = WMCreateScroller(tPtr);
3441 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3442 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3443 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3444 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3445 WMMapWidget(tPtr->hS);
3446 } else if (!shouldhave && tPtr->hS) {
3447 WMUnmapWidget(tPtr->hS);
3448 WMDestroyWidget(tPtr->hS);
3449 tPtr->hS = NULL;
3452 tPtr->hpos = 0;
3453 tPtr->prevHpos = 0;
3454 textDidResize(tPtr->view->delegate, tPtr->view);
3458 void
3459 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3461 if (!tPtr)
3462 return;
3464 if(shouldhave && !tPtr->ruler) {
3465 tPtr->ruler = WMCreateRuler(tPtr);
3466 (W_VIEW(tPtr->ruler))->attribs.cursor =
3467 tPtr->view->screen->defaultCursor;
3468 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3469 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3470 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3471 } else if(!shouldhave && tPtr->ruler) {
3472 WMShowTextRuler(tPtr, False);
3473 WMDestroyWidget(tPtr->ruler);
3474 tPtr->ruler = NULL;
3476 textDidResize(tPtr->view->delegate, tPtr->view);
3479 void
3480 WMShowTextRuler(WMText *tPtr, Bool show)
3482 if(!tPtr)
3483 return;
3484 if(!tPtr->ruler)
3485 return;
3487 if(tPtr->flags.monoFont)
3488 show = False;
3490 tPtr->flags.rulerShown = show;
3491 if(show) {
3492 WMMapWidget(tPtr->ruler);
3493 } else {
3494 WMUnmapWidget(tPtr->ruler);
3497 textDidResize(tPtr->view->delegate, tPtr->view);
3500 Bool
3501 WMGetTextRulerShown(WMText *tPtr)
3503 if(!tPtr)
3504 return 0;
3506 if(!tPtr->ruler)
3507 return 0;
3509 return tPtr->flags.rulerShown;
3513 void
3514 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3516 if (!tPtr)
3517 return;
3519 if (shouldhave && !tPtr->vS) {
3520 tPtr->vS = WMCreateScroller(tPtr);
3521 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3522 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3523 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3524 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3525 WMMapWidget(tPtr->vS);
3526 } else if (!shouldhave && tPtr->vS) {
3527 WMUnmapWidget(tPtr->vS);
3528 WMDestroyWidget(tPtr->vS);
3529 tPtr->vS = NULL;
3532 tPtr->vpos = 0;
3533 tPtr->prevVpos = 0;
3534 textDidResize(tPtr->view->delegate, tPtr->view);
3539 Bool
3540 WMScrollText(WMText *tPtr, int amount)
3542 Bool scroll=False;
3543 if (!tPtr)
3544 return False;
3545 if (amount == 0 || !tPtr->view->flags.realized)
3546 return False;
3548 if (amount < 0) {
3549 if (tPtr->vpos > 0) {
3550 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3551 else tPtr->vpos=0;
3552 scroll=True;
3553 } } else {
3554 int limit = tPtr->docHeight - tPtr->visible.h;
3555 if (tPtr->vpos < limit) {
3556 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3557 else tPtr->vpos = limit;
3558 scroll = True;
3559 } }
3561 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3562 updateScrollers(tPtr);
3563 paintText(tPtr);
3565 tPtr->prevVpos = tPtr->vpos;
3566 return scroll;
3569 Bool
3570 WMPageText(WMText *tPtr, Bool direction)
3572 if (!tPtr) return False;
3573 if (!tPtr->view->flags.realized) return False;
3575 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3578 void
3579 WMSetTextEditable(WMText *tPtr, Bool editable)
3581 if (!tPtr)
3582 return;
3583 tPtr->flags.editable = editable;
3586 int
3587 WMGetTextEditable(WMText *tPtr)
3589 if (!tPtr)
3590 return 0;
3591 return tPtr->flags.editable;
3594 void
3595 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3597 if (!tPtr)
3598 return;
3599 tPtr->flags.indentNewLine = indent;
3602 void
3603 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3605 if (!tPtr)
3606 return;
3607 // tPtr->flags.ignoreNewLine = ignore;
3610 Bool
3611 WMGetTextIgnoresNewline(WMText *tPtr)
3613 if (!tPtr)
3614 return True;
3615 return tPtr->flags.ignoreNewLine;
3618 void
3619 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3621 if (!tPtr)
3622 return;
3624 if (mono) {
3625 if(tPtr->flags.rulerShown)
3626 WMShowTextRuler(tPtr, False);
3627 if(tPtr->flags.alignment != WALeft)
3628 tPtr->flags.alignment = WALeft;
3631 tPtr->flags.monoFont = mono;
3632 textDidResize(tPtr->view->delegate, tPtr->view);
3635 Bool
3636 WMGetTextUsesMonoFont(WMText *tPtr)
3638 if (!tPtr)
3639 return True;
3640 return tPtr->flags.monoFont;
3644 void
3645 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3647 if (!tPtr)
3648 return;
3650 WMReleaseFont(tPtr->dFont);
3651 if (font)
3652 tPtr->dFont = WMRetainFont(font);
3653 else
3654 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3657 WMFont *
3658 WMGetTextDefaultFont(WMText *tPtr)
3660 if (!tPtr)
3661 return NULL;
3662 else
3663 return WMRetainFont(tPtr->dFont);
3666 void
3667 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3669 if (!tPtr)
3670 return;
3672 WMReleaseColor(tPtr->dColor);
3673 if (color)
3674 tPtr->dColor = WMRetainColor(color);
3675 else
3676 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3679 WMColor *
3680 WMGetTextDefaultColor(WMText *tPtr)
3682 if (!tPtr)
3683 return NULL;
3684 else
3685 return WMRetainColor(tPtr->dColor);
3688 void
3689 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3691 if (!tPtr)
3692 return;
3693 if(tPtr->flags.monoFont)
3694 tPtr->flags.alignment = WALeft;
3695 else
3696 tPtr->flags.alignment = alignment;
3697 WMThawText(tPtr);
3700 int
3701 WMGetTextInsertType(WMText *tPtr)
3703 if (!tPtr)
3704 return 0;
3705 return tPtr->flags.prepend;
3709 void
3710 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3712 if (!tPtr || !color)
3713 return;
3715 setSelectionProperty(tPtr, NULL, color, -1);
3718 void
3719 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3721 if (!tPtr || !font)
3722 return;
3724 setSelectionProperty(tPtr, font, NULL, -1) ;
3727 void
3728 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3730 if (!tPtr || (underlined!=0 && underlined !=1))
3731 return;
3733 setSelectionProperty(tPtr, NULL, NULL, underlined);
3739 void
3740 WMFreezeText(WMText *tPtr)
3742 if (!tPtr)
3743 return;
3745 tPtr->flags.frozen = True;
3749 void
3750 WMThawText(WMText *tPtr)
3752 if (!tPtr)
3753 return;
3755 tPtr->flags.frozen = False;
3757 if(tPtr->flags.monoFont) {
3758 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3759 TextBlock *tb;
3761 /* make sure to unmap widgets no matter where they are */
3762 /* they'll be later remapped if needed by paintText */
3763 for(j=0; j<c; j++) {
3764 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3765 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3766 WMUnmapWidget(tb->d.widget);
3772 tPtr->flags.laidOut = False;
3773 layOutDocument(tPtr);
3774 updateScrollers(tPtr);
3775 paintText(tPtr);
3776 tPtr->flags.needsLayOut = False;
3781 static char *
3782 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
3783 Bool caseSensitive)
3785 char *ptr;
3787 if(!haystack || !needle)
3788 return NULL;
3790 for (ptr = haystack-2; ptr > end; ptr--) {
3791 if(caseSensitive) {
3792 if (*ptr == *needle && !strncmp(ptr, needle, len))
3793 return ptr;
3794 } else {
3795 if (tolower(*ptr) == tolower(*needle) &&
3796 strncasecmp(ptr, needle, len))
3797 return ptr;
3801 return NULL;
3805 Bool
3806 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3807 Bool caseSensitive)
3809 TextBlock *tb;
3810 char *mark;
3811 unsigned short pos;
3813 if (!tPtr || !needle)
3814 return False;
3816 if (! (tb = tPtr->currentTextBlock)) {
3817 if (! (tb = ( (direction > 0) ?
3818 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3819 return False;
3821 } else {
3822 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3823 tb = (direction>0) ? tb->next : tb->prior; */
3824 if(tb != tPtr->lastTextBlock)
3825 tb = tb->prior;
3829 while(tb) {
3830 if (!tb->graphic) {
3831 pos = tPtr->tpos;
3832 if(pos+1 < tb->used)
3833 pos++;
3835 if(tb->used - pos> 0 && pos > 0) {
3836 char tmp = tb->text[tb->used];
3837 tb->text[tb->used] = 0;
3839 output(&tb->text[pos], tb->used - pos);
3840 if(direction > 0)
3841 mark = strstr(&tb->text[pos], needle);
3842 else
3843 mark = mystrrstr(&tb->text[pos], needle,
3844 strlen(needle), tb->text, caseSensitive);
3846 tb->text[tb->used] = tmp;
3848 } else {
3849 return False;
3852 if(mark) {
3853 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3855 tPtr->tpos = (int)(mark - tb->text);
3856 tPtr->currentTextBlock = tb;
3857 updateCursorPosition(tPtr);
3858 tPtr->sel.y = tPtr->cursor.y+5;
3859 tPtr->sel.h = tPtr->cursor.h-10;
3860 tPtr->sel.x = tPtr->cursor.x +1;
3861 tPtr->sel.w = WMIN(WMWidthOfString(font,
3862 &tb->text[tPtr->tpos], strlen(needle)),
3863 tPtr->docWidth - tPtr->sel.x);
3864 tPtr->flags.ownsSelection = True;
3865 paintText(tPtr);
3867 return True;
3871 tb = (direction>0) ? tb->next : tb->prior;
3872 pos = 0;
3875 return False;