- Replaced all free() with wfree() where appropriate
[wmaker-crm.git] / WINGs / wtext.c
blob53e3efbd1e085d285ff323443eb021c5a0280566
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 (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;
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;
1434 TextBlock *tbsame=NULL;
1436 if(!items || nitems == 0)
1437 return 0;
1439 for(i=0; i<nitems; i++) {
1440 tb = items[i].tb;
1442 if (tb->graphic) {
1443 if (!tPtr->flags.monoFont) {
1444 if(tb->object) {
1445 WMWidget *wdt = tb->d.widget;
1446 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1447 if (tPtr->flags.alignment != WALeft)
1448 lw += WMWidgetWidth(wdt);
1449 } else {
1450 line_height = WMAX(line_height,
1451 tb->d.pixmap->height + max_d);
1452 if (tPtr->flags.alignment != WALeft)
1453 lw += tb->d.pixmap->width;
1457 } else {
1458 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1459 max_d = WMAX(max_d, abs(font->height-font->y));
1460 line_height = WMAX(line_height, font->height + max_d);
1461 text = &(tb->text[items[i].begin]);
1462 len = items[i].end - items[i].begin;
1463 if (tPtr->flags.alignment != WALeft)
1464 lw += WMWidthOfString(font, text, len);
1468 if (tPtr->flags.alignment == WARight) {
1469 j = tPtr->visible.w - lw;
1470 } else if (tPtr->flags.alignment == WACenter) {
1471 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1474 for(i=0; i<nitems; i++) {
1475 tb = items[i].tb;
1477 if (tbsame == tb) { /* extend it, since it's on same line */
1478 tb->sections[tb->nsections-1].end = items[i].end;
1479 n = tb->nsections-1;
1480 } else {
1481 tb->sections = wrealloc(tb->sections,
1482 (++tb->nsections)*sizeof(Section));
1483 n = tb->nsections-1;
1484 tb->sections[n]._y = y + max_d;
1485 tb->sections[n].max_d = max_d;
1486 tb->sections[n].x = x+j;
1487 tb->sections[n].h = line_height;
1488 tb->sections[n].begin = items[i].begin;
1489 tb->sections[n].end = items[i].end;
1492 tb->sections[n].last = (i+1 == nitems);
1494 if (tb->graphic) {
1495 if (!tPtr->flags.monoFont) {
1496 if(tb->object) {
1497 WMWidget *wdt = tb->d.widget;
1498 tb->sections[n].y = max_d + y
1499 + line_height - WMWidgetHeight(wdt);
1500 tb->sections[n].w = WMWidgetWidth(wdt);
1501 } else {
1502 tb->sections[n].y = y + line_height
1503 + max_d - tb->d.pixmap->height;
1504 tb->sections[n].w = tb->d.pixmap->width;
1506 x += tb->sections[n].w;
1508 } else {
1509 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1510 len = items[i].end - items[i].begin;
1511 text = &(tb->text[items[i].begin]);
1513 tb->sections[n].y = y+line_height-font->y;
1514 tb->sections[n].w =
1515 WMWidthOfString(font,
1516 &(tb->text[tb->sections[n].begin]),
1517 tb->sections[n].end - tb->sections[n].begin);
1519 x += WMWidthOfString(font, text, len);
1522 tbsame = tb;
1525 return line_height;
1530 static void
1531 layOutDocument(Text *tPtr)
1533 TextBlock *tb;
1534 myLineItems *items = NULL;
1535 unsigned int itemsSize=0, nitems=0, begin, end;
1536 WMFont *font;
1537 unsigned int x, y=0, lw = 0, width=0, bmargin;
1538 char *start=NULL, *mark=NULL;
1540 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1541 return;
1543 tPtr->docWidth = tPtr->visible.w;
1544 x = tPtr->margins[tb->marginN].first;
1545 bmargin = tPtr->margins[tb->marginN].body;
1547 /* only partial layOut needed: re-Lay only affected textblocks */
1548 if (tPtr->flags.laidOut) {
1549 tb = tPtr->currentTextBlock;
1551 /* search backwards for textblocks on same line */
1552 while (tb->prior) {
1553 if (!tb->sections || tb->nsections<1) {
1554 tb = tPtr->firstTextBlock;
1555 tPtr->flags.laidOut = False;
1556 y = 0;
1557 goto _layOut;
1560 if(!tb->prior->sections || tb->prior->nsections<1) {
1561 tb = tPtr->firstTextBlock;
1562 tPtr->flags.laidOut = False;
1563 y = 0;
1564 goto _layOut;
1567 if (tb->sections[0]._y !=
1568 tb->prior->sections[tb->prior->nsections-1]._y) {
1569 break;
1571 tb = tb->prior;
1574 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1575 y = tb->prior->sections[tb->prior->nsections-1]._y +
1576 tb->prior->sections[tb->prior->nsections-1].h -
1577 tb->prior->sections[tb->prior->nsections-1].max_d;
1578 } else {
1579 y = 0;
1583 _layOut:
1584 while (tb) {
1586 if (tb->sections && tb->nsections>0) {
1587 wfree(tb->sections);
1588 tb->sections = NULL;
1589 tb->nsections = 0;
1592 if (tb->blank && tb->next && !tb->next->first) {
1593 TextBlock *next = tb->next;
1594 tPtr->currentTextBlock = tb;
1595 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1596 tb = next;
1597 tb->first = True;
1598 continue;
1601 if (tb->first && tb != tPtr->firstTextBlock) {
1602 y += layOutLine(tPtr, items, nitems, x, y);
1603 x = tPtr->margins[tb->marginN].first;
1604 bmargin = tPtr->margins[tb->marginN].body;
1605 nitems = 0;
1606 lw = 0;
1609 if (tb->graphic) {
1610 if (!tPtr->flags.monoFont) {
1611 if(tb->object)
1612 width = WMWidgetWidth(tb->d.widget);
1613 else
1614 width = tb->d.pixmap->width;
1616 if (width > tPtr->docWidth)
1617 tPtr->docWidth = width;
1619 lw += width;
1620 if (lw >= tPtr->visible.w - x ) {
1621 y += layOutLine(tPtr, items, nitems, x, y);
1622 nitems = 0;
1623 x = bmargin;
1624 lw = width;
1627 if(nitems + 1> itemsSize) {
1628 printf("realloc %d nitems\n", nitems);
1629 items = wrealloc(items,
1630 (++itemsSize)*sizeof(myLineItems));
1633 items[nitems].tb = tb;
1634 items[nitems].begin = 0;
1635 items[nitems].end = 0;
1636 nitems++;
1639 } else if ((start = tb->text)) {
1640 begin = end = 0;
1641 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1643 while (start) {
1644 mark = strchr(start, ' ');
1645 if (mark) {
1646 end += (int)(mark-start)+1;
1647 start = mark+1;
1648 } else {
1649 end += strlen(start);
1650 start = mark;
1653 if (end > tb->used)
1654 end = tb->used;
1656 if (end-begin > 0) {
1658 width = WMWidthOfString(font,
1659 &tb->text[begin], end-begin);
1661 /* if it won't fit, break it up */
1662 if (width > tPtr->visible.w) {
1663 char *t = &tb->text[begin];
1664 int l=end-begin, i=0;
1665 printf("%d > %d\n", width, tPtr->visible.w);
1666 do {
1667 width = WMWidthOfString(font, t, ++i);
1668 } while (width < tPtr->visible.w && i < l);
1669 end = begin+i;
1670 if (start)
1671 start -= l-i;
1674 lw += width;
1677 if (lw >= tPtr->visible.w - x) {
1678 y += layOutLine(tPtr, items, nitems, x, y);
1679 lw = width;
1680 x = bmargin;
1681 nitems = 0;
1684 if(nitems + 1 > itemsSize) {
1685 items = wrealloc(items,
1686 (++itemsSize)*sizeof(myLineItems));
1689 items[nitems].tb = tb;
1690 items[nitems].begin = begin;
1691 items[nitems].end = end;
1692 nitems++;
1694 begin = end;
1699 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1700 if(0&&tPtr->flags.laidOut
1701 && tb->next && tb->next->sections && tb->next->nsections>0
1702 && (tPtr->vpos + tPtr->visible.h
1703 < tb->next->sections[0]._y)) {
1704 if(tPtr->lastTextBlock->sections
1705 && tPtr->lastTextBlock->nsections > 0 ) {
1706 TextBlock *ltb = tPtr->lastTextBlock;
1707 int ly = ltb->sections[ltb->nsections-1]._y;
1708 int lh = ltb->sections[ltb->nsections-1].h;
1709 int ss, sd;
1711 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1712 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1714 y += layOutLine(tPtr, items, nitems, x, y);
1715 ss= ly+lh-y;
1716 sd = tPtr->docHeight-y;
1718 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1719 y += tb->next->sections[0]._y-y;
1720 nitems = 0;
1721 printf("nitems%d\n", nitems);
1722 if(ss-sd!=0)
1723 y = tPtr->docHeight+ss-sd;
1725 break;
1726 } else {
1727 tPtr->flags.laidOut = False;
1731 tb = tb->next;
1735 if (nitems > 0)
1736 y += layOutLine(tPtr, items, nitems, x, y);
1738 if (tPtr->docHeight != y+10) {
1739 tPtr->docHeight = y+10;
1740 updateScrollers(tPtr);
1743 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1744 XEvent event;
1746 tPtr->flags.horizOnDemand = True;
1747 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1748 event.type = Expose;
1749 handleEvents(&event, (void *)tPtr);
1751 } else if(tPtr->docWidth <= tPtr->visible.w
1752 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1753 tPtr->flags.horizOnDemand = False;
1754 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1757 tPtr->flags.laidOut = True;
1759 if(items && itemsSize > 0)
1760 wfree(items);
1764 static void
1765 textDidResize(W_ViewDelegate *self, WMView *view)
1767 Text *tPtr = (Text *)view->self;
1768 unsigned short w = tPtr->view->size.width;
1769 unsigned short h = tPtr->view->size.height;
1770 unsigned short rh = 0, vw = 0, rel;
1772 rel = (tPtr->flags.relief == WRFlat);
1774 if (tPtr->ruler && tPtr->flags.rulerShown) {
1775 WMMoveWidget(tPtr->ruler, 2, 2);
1776 WMResizeWidget(tPtr->ruler, w - 4, 40);
1777 rh = 40;
1780 if (tPtr->vS) {
1781 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1782 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1783 vw = 20;
1784 WMSetRulerOffset(tPtr->ruler,22);
1785 } else WMSetRulerOffset(tPtr->ruler, 2);
1787 if (tPtr->hS) {
1788 if (tPtr->vS) {
1789 WMMoveWidget(tPtr->hS, vw, h - 21);
1790 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1791 } else {
1792 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1793 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1797 tPtr->visible.x = (tPtr->vS)?24:4;
1798 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1799 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1800 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1801 tPtr->visible.h -= (tPtr->hS)?20:0;
1802 tPtr->margins[0].right = tPtr->visible.w;
1804 if (tPtr->view->flags.realized) {
1806 if (tPtr->db) {
1807 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1808 tPtr->db = (Pixmap) NULL;
1811 if (tPtr->visible.w < 40)
1812 tPtr->visible.w = 40;
1813 if (tPtr->visible.h < 20)
1814 tPtr->visible.h = 20;
1816 if(!tPtr->db) {
1817 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1818 tPtr->view->window, tPtr->visible.w,
1819 tPtr->visible.h, tPtr->view->screen->depth);
1823 WMThawText(tPtr);
1826 W_ViewDelegate _TextViewDelegate =
1828 NULL,
1829 NULL,
1830 textDidResize,
1831 NULL,
1834 /* nice, divisble-by-16 blocks */
1835 static inline unsigned short
1836 reqBlockSize(unsigned short requested)
1838 return requested + 16 - (requested%16);
1842 static void
1843 clearText(Text *tPtr)
1845 tPtr->vpos = tPtr->hpos = 0;
1846 tPtr->docHeight = tPtr->docWidth = 0;
1847 updateScrollers(tPtr);
1849 if (!tPtr->firstTextBlock)
1850 return;
1852 while (tPtr->currentTextBlock)
1853 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1855 tPtr->firstTextBlock = NULL;
1856 tPtr->currentTextBlock = NULL;
1857 tPtr->lastTextBlock = NULL;
1858 WMEmptyArray(tPtr->gfxItems);
1861 static void
1862 deleteTextInteractively(Text *tPtr, KeySym ksym)
1864 TextBlock *tb;
1865 Bool back = (Bool) (ksym == XK_BackSpace);
1866 Bool done = 1;
1867 Bool wasFirst = 0;
1869 if (!tPtr->flags.editable) {
1870 XBell(tPtr->view->screen->display, 0);
1871 return;
1874 if ( !(tb = tPtr->currentTextBlock) )
1875 return;
1877 if (tPtr->flags.ownsSelection) {
1878 if(removeSelection(tPtr))
1879 layOutDocument(tPtr);
1880 return;
1883 wasFirst = tb->first;
1884 if (back && tPtr->tpos < 1) {
1885 if (tb->prior) {
1886 if(tb->prior->blank) {
1887 tPtr->currentTextBlock = tb->prior;
1888 WMRemoveTextBlock(tPtr);
1889 tPtr->currentTextBlock = tb;
1890 tb->first = True;
1891 layOutDocument(tPtr);
1892 return;
1893 } else {
1894 if(tb->blank) {
1895 TextBlock *prior = tb->prior;
1896 tPtr->currentTextBlock = tb;
1897 WMRemoveTextBlock(tPtr);
1898 tb = prior;
1899 } else {
1900 tb = tb->prior;
1903 tPtr->tpos = tb->used;
1904 tPtr->currentTextBlock = tb;
1905 done = 1;
1906 if(wasFirst) {
1907 if(tb->next)
1908 tb->next->first = False;
1909 layOutDocument(tPtr);
1910 return;
1916 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1917 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1918 if (back)
1919 tPtr->tpos--;
1920 memmove(&(tb->text[tPtr->tpos]),
1921 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1922 tb->used--;
1923 done = 0;
1926 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1927 || tb->graphic) {
1929 TextBlock *sibling = (back? tb->prior : tb->next);
1931 if(tb->used == 0 || tb->graphic)
1932 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1934 if (sibling) {
1935 tPtr->currentTextBlock = sibling;
1936 tPtr->tpos = (back? sibling->used : 0);
1940 layOutDocument(tPtr);
1944 static void
1945 insertTextInteractively(Text *tPtr, char *text, int len)
1947 TextBlock *tb;
1948 char *newline = NULL;
1950 if (!tPtr->flags.editable) {
1951 XBell(tPtr->view->screen->display, 0);
1952 return;
1955 if (len < 1 || !text)
1956 return;
1959 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1960 return;
1963 if (tPtr->flags.ownsSelection)
1964 removeSelection(tPtr);
1967 if (tPtr->flags.ignoreNewLine) {
1968 int i;
1969 for(i=0; i<len; i++) {
1970 if (text[i] == '\n')
1971 text[i] = ' ';
1975 tb = tPtr->currentTextBlock;
1976 if (!tb || tb->graphic) {
1977 text[len] = 0;
1978 WMAppendTextStream(tPtr, text);
1979 tPtr->tpos = tPtr->currentTextBlock->used;
1980 layOutDocument(tPtr);
1981 return;
1984 if ((newline = strchr(text, '\n'))) {
1985 int nlen = (int)(newline-text);
1986 int s = tb->used - tPtr->tpos;
1987 char save[s];
1988 if (!tb->blank && nlen>0) {
1989 if (s > 0) {
1990 memcpy(save, &tb->text[tPtr->tpos], s);
1991 tb->used -= (tb->used - tPtr->tpos);
1993 insertTextInteractively(tPtr, text, nlen);
1994 newline++;
1995 WMAppendTextStream(tPtr, newline);
1996 if (s>0)
1997 insertTextInteractively(tPtr, save, s);
1999 } else {
2000 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2001 && !tb->graphic && tb->text) {
2003 void *ntb = WMCreateTextBlockWithText(
2004 tPtr, &tb->text[tPtr->tpos],
2005 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2006 tb->used = tPtr->tpos;
2007 WMAppendTextBlock(tPtr, ntb);
2008 tPtr->tpos = 0;
2010 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2011 if(tPtr->flags.indentNewLine) {
2012 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2013 " ", tb->d.font, tb->color, True, 4));
2014 tPtr->tpos = 4;
2015 } else {
2016 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2017 NULL, tb->d.font, tb->color, True, 0));
2018 tPtr->tpos = 0;
2023 } else {
2024 if (tb->used + len >= tb->allocated) {
2025 tb->allocated = reqBlockSize(tb->used+len);
2026 tb->text = wrealloc(tb->text, tb->allocated);
2029 if (tb->blank) {
2030 memcpy(tb->text, text, len);
2031 tb->used = len;
2032 tPtr->tpos = len;
2033 tb->text[tb->used] = 0;
2034 tb->blank = False;
2036 } else {
2037 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2038 tb->used-tPtr->tpos+1);
2039 memmove(&tb->text[tPtr->tpos], text, len);
2040 tb->used += len;
2041 tPtr->tpos += len;
2042 tb->text[tb->used] = 0;
2047 layOutDocument(tPtr);
2051 static void
2052 selectRegion(Text *tPtr, int x, int y)
2055 if (x < 0 || y < 0)
2056 return;
2058 y += (tPtr->flags.rulerShown? 40: 0);
2059 y += tPtr->vpos;
2060 if (y>10)
2061 y -= 10; /* the original offset */
2063 x -= tPtr->visible.x-2;
2064 if (x<0)
2065 x=0;
2067 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2068 tPtr->sel.w = abs(tPtr->clicked.x - x);
2069 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2070 tPtr->sel.h = abs(tPtr->clicked.y - y);
2072 tPtr->flags.ownsSelection = True;
2073 paintText(tPtr);
2077 static void
2078 releaseSelection(Text *tPtr)
2080 TextBlock *tb = tPtr->firstTextBlock;
2082 while(tb) {
2083 tb->selected = False;
2084 tb = tb->next;
2086 tPtr->flags.ownsSelection = False;
2087 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2088 CurrentTime);
2090 paintText(tPtr);
2094 WMData*
2095 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2096 Atom *type)
2098 Text *tPtr = view->self;
2099 Display *dpy = tPtr->view->screen->display;
2100 Atom _TARGETS;
2101 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2102 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2103 WMData *data = NULL;
2106 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2107 char *text = WMGetTextSelectedStream(tPtr);
2109 if (text) {
2110 printf("got text [%s]\n", text);
2111 data = WMCreateDataWithBytes(text, strlen(text));
2112 WMSetDataFormat(data, TYPETEXT);
2114 *type = target;
2115 return data;
2116 } else printf("didn't get it\n");
2118 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2119 if (target == _TARGETS) {
2120 Atom *ptr;
2122 ptr = wmalloc(4 * sizeof(Atom));
2123 ptr[0] = _TARGETS;
2124 ptr[1] = XA_STRING;
2125 ptr[2] = TEXT;
2126 ptr[3] = COMPOUND_TEXT;
2128 data = WMCreateDataWithBytes(ptr, 4*4);
2129 WMSetDataFormat(data, 32);
2131 *type = target;
2132 return data;
2135 return NULL;
2138 static void
2139 lostHandler(WMView *view, Atom selection, void *cdata)
2141 releaseSelection((WMText *)view->self);
2144 static WMSelectionProcs selectionHandler = {
2145 requestHandler, lostHandler, NULL
2149 static void
2150 ownershipObserver(void *observerData, WMNotification *notification)
2152 if (observerData != WMGetNotificationClientData(notification))
2153 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2157 static void
2158 fontChanged(void *observerData, WMNotification *notification)
2160 WMText *tPtr = (WMText *) observerData;
2161 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2162 printf("fontChanged\n");
2164 if(!tPtr || !font)
2165 return;
2167 if (tPtr->flags.ownsSelection)
2168 WMSetTextSelectionFont(tPtr, font);
2172 static void
2173 handleTextKeyPress(Text *tPtr, XEvent *event)
2175 char buffer[2];
2176 KeySym ksym;
2177 int control_pressed = False;
2178 TextBlock *tb = NULL;
2180 if (((XKeyEvent *) event)->state & ControlMask)
2181 control_pressed = True;
2182 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2184 switch(ksym) {
2186 case XK_Left:
2187 if(!(tb = tPtr->currentTextBlock))
2188 break;
2189 if(tb->graphic)
2190 goto L_imaGFX;
2192 if(tPtr->tpos==0) {
2193 L_imaGFX: if(tb->prior) {
2194 tPtr->currentTextBlock = tb->prior;
2195 tPtr->tpos = tPtr->currentTextBlock->used -1;
2196 } else tPtr->tpos = 0;
2197 } else tPtr->tpos--;
2198 updateCursorPosition(tPtr);
2199 paintText(tPtr);
2200 break;
2202 case XK_Right:
2203 if(!(tb = tPtr->currentTextBlock))
2204 break;
2205 if(tb->graphic)
2206 goto R_imaGFX;
2207 if(tPtr->tpos == tb->used) {
2208 R_imaGFX: if(tb->next) {
2209 tPtr->currentTextBlock = tb->next;
2210 tPtr->tpos = 1;
2211 } else tPtr->tpos = tb->used;
2212 } else tPtr->tpos++;
2213 updateCursorPosition(tPtr);
2214 paintText(tPtr);
2215 break;
2217 case XK_Down:
2218 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2219 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2220 paintText(tPtr);
2221 break;
2223 case XK_Up:
2224 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2225 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2226 paintText(tPtr);
2227 break;
2229 case XK_BackSpace:
2230 case XK_Delete:
2231 case XK_KP_Delete:
2232 deleteTextInteractively(tPtr, ksym);
2233 updateCursorPosition(tPtr);
2234 paintText(tPtr);
2235 break;
2237 case XK_Control_R :
2238 case XK_Control_L :
2239 control_pressed = True;
2240 break;
2242 case XK_Tab:
2243 insertTextInteractively(tPtr, " ", 4);
2244 updateCursorPosition(tPtr);
2245 paintText(tPtr);
2246 break;
2248 case XK_Return:
2249 buffer[0] = '\n';
2250 default:
2251 if (buffer[0] != 0 && !control_pressed) {
2252 insertTextInteractively(tPtr, buffer, 1);
2253 updateCursorPosition(tPtr);
2254 paintText(tPtr);
2256 } else if (control_pressed && ksym==XK_r) {
2257 Bool i = !tPtr->flags.rulerShown;
2258 WMShowTextRuler(tPtr, i);
2259 tPtr->flags.rulerShown = i;
2261 else if (control_pressed && buffer[0] == '\a')
2262 XBell(tPtr->view->screen->display, 0);
2265 if (!control_pressed && tPtr->flags.ownsSelection)
2266 releaseSelection(tPtr);
2269 static void
2270 handleWidgetPress(XEvent *event, void *data)
2272 TextBlock *tb = (TextBlock *)data;
2273 Text *tPtr;
2274 WMWidget *w;
2276 if (!tb)
2277 return;
2279 tPtr = (Text*)w;
2280 tPtr->currentTextBlock = tb;
2281 tPtr->flags.isOverGraphic = 2;
2282 tPtr->tpos = 0;
2283 output(tb->text, tb->used);
2284 #if 0
2285 if (!tPtr->flags.focused) {
2286 WMSetFocusToWidget(tPtr);
2287 tPtr->flags.focused = True;
2289 #endif
2293 static void
2294 handleActionEvents(XEvent *event, void *data)
2296 Text *tPtr = (Text *)data;
2297 Display *dpy = event->xany.display;
2298 KeySym ksym;
2301 switch (event->type) {
2302 case KeyPress:
2303 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2304 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2305 tPtr->flags.extendSelection = True;
2306 return;
2309 if (tPtr->flags.focused) {
2310 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2311 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2312 GrabModeAsync, GrabModeAsync, None,
2313 tPtr->view->screen->invisibleCursor, CurrentTime);
2314 tPtr->flags.pointerGrabbed = True;
2315 handleTextKeyPress(tPtr, event);
2317 } break;
2319 case KeyRelease:
2320 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2321 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2322 tPtr->flags.extendSelection = False;
2323 return;
2324 /* end modify flag so selection can be extended */
2326 break;
2329 case MotionNotify:
2331 if (tPtr->flags.pointerGrabbed) {
2332 tPtr->flags.pointerGrabbed = False;
2333 XUngrabPointer(dpy, CurrentTime);
2336 if(tPtr->flags.waitingForSelection)
2337 break;
2339 if ((event->xmotion.state & Button1Mask)) {
2340 if (!tPtr->flags.ownsSelection) {
2341 WMCreateSelectionHandler(tPtr->view,
2342 XA_PRIMARY, event->xbutton.time,
2343 &selectionHandler, NULL);
2344 tPtr->flags.ownsSelection = True;
2346 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2347 break;
2350 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2351 break;
2354 case ButtonPress:
2356 if (tPtr->flags.pointerGrabbed) {
2357 tPtr->flags.pointerGrabbed = False;
2358 XUngrabPointer(dpy, CurrentTime);
2359 break;
2362 if (tPtr->flags.waitingForSelection)
2363 break;
2365 if (tPtr->flags.extendSelection) {
2366 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2367 return;
2371 if (event->xbutton.button == Button1) {
2373 if(WMIsDoubleClick(event)) {
2374 TextBlock *tb = tPtr->currentTextBlock;
2376 if(tb && tb->graphic && !tb->object) {
2377 char desc[tb->used+1];
2378 memcpy(desc, tb->text, tb->used);
2379 desc[tb->used] = 0;
2380 if(tPtr->delegate) {
2381 if(tPtr->delegate->didDoubleClickOnPicture)
2382 (*tPtr->delegate->didDoubleClickOnPicture)
2383 (tPtr->delegate, desc);
2385 } else {
2386 autoSelectText(tPtr, 2);
2388 tPtr->lastClickTime = event->xbutton.time;
2389 break;
2390 } else if(event->xbutton.time - tPtr->lastClickTime
2391 < WINGsConfiguration.doubleClickDelay) {
2392 autoSelectText(tPtr, 3);
2393 break;
2396 if (!tPtr->flags.focused) {
2397 WMSetFocusToWidget(tPtr);
2398 tPtr->flags.focused = True;
2401 if (tPtr->flags.ownsSelection)
2402 releaseSelection(tPtr);
2404 tPtr->lastClickTime = event->xbutton.time;
2405 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2406 paintText(tPtr);
2409 if (event->xbutton.button
2410 == WINGsConfiguration.mouseWheelDown) {
2411 WMScrollText(tPtr, 16);
2412 break;
2415 if (event->xbutton.button
2416 == WINGsConfiguration.mouseWheelUp) {
2417 WMScrollText(tPtr, -16);
2418 break;
2421 if (event->xbutton.button == Button2) {
2422 char *text = NULL;
2423 int n;
2425 if (!tPtr->flags.editable) {
2426 XBell(dpy, 0);
2427 break;
2430 #if 0
2431 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2432 event->xbutton.time, pasteText, NULL)) {
2433 #endif
2436 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2438 if (text) {
2439 text[n] = 0;
2441 if (tPtr->parser)
2442 (tPtr->parser) (tPtr, (void *) text);
2443 else
2444 insertTextInteractively(tPtr, text, n);
2446 XFree(text);
2447 #if 0
2448 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2449 (void*)WMInsertTextEvent);
2450 #endif
2452 } else {
2453 tPtr->flags.waitingForSelection = True;
2456 break;
2460 case ButtonRelease:
2462 if (tPtr->flags.pointerGrabbed) {
2463 tPtr->flags.pointerGrabbed = False;
2464 XUngrabPointer(dpy, CurrentTime);
2465 break;
2468 if (tPtr->flags.waitingForSelection)
2469 break;
2475 static void
2476 handleEvents(XEvent *event, void *data)
2478 Text *tPtr = (Text *)data;
2480 switch(event->type) {
2481 case Expose:
2483 if (event->xexpose.count!=0)
2484 break;
2486 if(tPtr->hS) {
2487 if (!(W_VIEW(tPtr->hS))->flags.realized)
2488 WMRealizeWidget(tPtr->hS);
2491 if(tPtr->vS) {
2492 if (!(W_VIEW(tPtr->vS))->flags.realized)
2493 WMRealizeWidget(tPtr->vS);
2496 if(tPtr->ruler) {
2497 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2498 WMRealizeWidget(tPtr->ruler);
2502 if(!tPtr->db)
2503 textDidResize(tPtr->view->delegate, tPtr->view);
2505 paintText(tPtr);
2506 break;
2508 case FocusIn:
2509 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2510 != tPtr->view)
2511 return;
2512 tPtr->flags.focused = True;
2513 #if DO_BLINK
2514 if (tPtr->flags.editable && !tPtr->timerID) {
2515 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2516 blinkCursor, tPtr);
2518 #endif
2520 break;
2522 case FocusOut:
2523 tPtr->flags.focused = False;
2524 paintText(tPtr);
2525 #if DO_BLINK
2526 if (tPtr->timerID) {
2527 WMDeleteTimerHandler(tPtr->timerID);
2528 tPtr->timerID = NULL;
2530 #endif
2531 break;
2534 case DestroyNotify:
2535 clearText(tPtr);
2536 if(tPtr->hS)
2537 WMDestroyWidget(tPtr->hS);
2538 if(tPtr->vS)
2539 WMDestroyWidget(tPtr->vS);
2540 if(tPtr->ruler)
2541 WMDestroyWidget(tPtr->ruler);
2542 if(tPtr->db)
2543 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2544 if(tPtr->gfxItems)
2545 WMEmptyArray(tPtr->gfxItems);
2546 #if DO_BLINK
2547 if (tPtr->timerID)
2548 WMDeleteTimerHandler(tPtr->timerID);
2549 #endif
2550 WMReleaseFont(tPtr->dFont);
2551 WMReleaseColor(tPtr->dColor);
2552 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2553 WMRemoveNotificationObserver(tPtr);
2555 wfree(tPtr);
2557 break;
2563 static void
2564 insertPlainText(Text *tPtr, char *text)
2566 char *start, *mark;
2567 void *tb = NULL;
2569 start = text;
2570 while (start) {
2571 mark = strchr(start, '\n');
2572 if (mark) {
2573 tb = WMCreateTextBlockWithText(tPtr,
2574 start, tPtr->dFont,
2575 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2576 start = mark+1;
2577 tPtr->flags.first = True;
2578 } else {
2579 if (start && strlen(start)) {
2580 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2581 tPtr->dColor, tPtr->flags.first, strlen(start));
2582 } else tb = NULL;
2583 tPtr->flags.first = False;
2584 start = mark;
2587 if (tPtr->flags.prepend)
2588 WMPrependTextBlock(tPtr, tb);
2589 else
2590 WMAppendTextBlock(tPtr, tb);
2592 return;
2597 static void
2598 rulerMoveCallBack(WMWidget *w, void *self)
2600 Text *tPtr = (Text *)self;
2601 if (!tPtr)
2602 return;
2603 if (W_CLASS(tPtr) != WC_Text)
2604 return;
2606 paintText(tPtr);
2610 static void
2611 rulerReleaseCallBack(WMWidget *w, void *self)
2613 Text *tPtr = (Text *)self;
2614 if (!tPtr)
2615 return;
2616 if (W_CLASS(tPtr) != WC_Text)
2617 return;
2619 WMThawText(tPtr);
2620 return;
2624 static unsigned
2625 draggingEntered(WMView *self, WMDraggingInfo *info)
2627 printf("draggingEntered\n");
2628 return WDOperationCopy;
2632 static unsigned
2633 draggingUpdated(WMView *self, WMDraggingInfo *info)
2635 return WDOperationCopy;
2639 static void
2640 draggingExited(WMView *self, WMDraggingInfo *info)
2642 printf("draggingExited\n");
2645 static Bool
2646 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2648 printf("prepareForDragOperation\n");
2649 return True;
2653 char *badbadbad;
2655 static void
2656 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2657 void *cdata, WMData *data)
2659 badbadbad = wstrdup((char *)WMDataBytes(data));
2663 /* when it's done in WINGs, remove this */
2665 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2667 WMScreen *scr = W_VIEW_SCREEN(view);
2669 if (!WMRequestSelection(scr->dragInfo.destView,
2670 scr->xdndSelectionAtom,
2671 XInternAtom(scr->display, type, False),
2672 scr->dragInfo.timestamp,
2673 receivedData, &scr->dragInfo)) {
2674 wwarning("could not request data for dropped data");
2679 XEvent ev;
2681 ev.type = ClientMessage;
2682 ev.xclient.message_type = scr->xdndFinishedAtom;
2683 ev.xclient.format = 32;
2684 ev.xclient.window = info->destinationWindow;
2685 ev.xclient.data.l[0] = 0;
2686 ev.xclient.data.l[1] = 0;
2687 ev.xclient.data.l[2] = 0;
2688 ev.xclient.data.l[3] = 0;
2689 ev.xclient.data.l[4] = 0;
2691 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2692 XFlush(scr->display);
2694 return True;
2697 static Bool
2698 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2700 WMColor *color;
2701 WMText *tPtr = (WMText *)self->self;
2703 if (!tPtr)
2704 return True;
2706 requestDroppedData(tPtr->view, info, "application/X-color");
2707 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2708 if(color) {
2709 WMSetTextSelectionColor(tPtr, color);
2710 WMReleaseColor(color);
2715 return True;
2718 static void
2719 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2721 printf("concludeDragOperation\n");
2725 static WMDragDestinationProcs _DragDestinationProcs = {
2726 draggingEntered,
2727 draggingUpdated,
2728 draggingExited,
2729 prepareForDragOperation,
2730 performDragOperation,
2731 concludeDragOperation
2735 char *
2736 getStream(WMText *tPtr, int sel, int array)
2738 TextBlock *tb = NULL;
2739 char *text = NULL;
2740 unsigned long where = 0;
2742 if (!tPtr)
2743 return NULL;
2745 if (!(tb = tPtr->firstTextBlock))
2746 return NULL;
2748 if (tPtr->writer) {
2749 (tPtr->writer) (tPtr, (void *) text);
2750 return text;
2753 tb = tPtr->firstTextBlock;
2754 while (tb) {
2756 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2758 if (!sel || (tb->graphic && tb->selected)) {
2760 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2761 && tb != tPtr->firstTextBlock) {
2762 text = wrealloc(text, where+1);
2763 text[where++] = '\n';
2766 if(tb->blank)
2767 goto _gSnext;
2769 if(tb->graphic && array) {
2770 text = wrealloc(text, where+4);
2771 text[where++] = 0xFA;
2772 text[where++] = (tb->used>>8)&0x0ff;
2773 text[where++] = tb->used&0x0ff;
2774 text[where++] = tb->allocated; /* extra info */
2776 text = wrealloc(text, where+tb->used);
2777 memcpy(&text[where], tb->text, tb->used);
2778 where += tb->used;
2781 } else if (sel && tb->selected) {
2783 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2784 && tb != tPtr->firstTextBlock) {
2785 text = wrealloc(text, where+1);
2786 text[where++] = '\n';
2789 if(tb->blank)
2790 goto _gSnext;
2792 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2793 memcpy(&text[where], &tb->text[tb->s_begin],
2794 tb->s_end - tb->s_begin);
2795 where += tb->s_end - tb->s_begin;
2800 _gSnext:tb = tb->next;
2803 /* +1 for the end of string, let's be nice */
2804 text = wrealloc(text, where+1);
2805 text[where] = 0;
2806 return text;
2810 static void
2811 releaseStreamObjects(void *data)
2813 if(data)
2814 wfree(data);
2817 WMArray *
2818 getStreamObjects(WMText *tPtr, int sel)
2820 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2821 WMData *data;
2822 char *stream;
2823 unsigned short len;
2824 char *start, *fa, *desc;
2826 stream = getStream(tPtr, sel, 1);
2827 if(!stream)
2828 return NULL;
2830 start = stream;
2831 while (start) {
2833 fa = strchr(start, 0xFA);
2834 if (fa) {
2835 if((int)(fa - start)>0) {
2836 desc = start;
2837 desc[(int)(fa - start)] = 0;
2838 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2839 WMSetDataFormat(data, TYPETEXT);
2840 WMAddToArray(array, (void *) data);
2843 len = *(fa+1)*0xff + *(fa+2);
2844 data = WMCreateDataWithBytes((void *)(fa+4), len);
2845 WMSetDataFormat(data, *(fa+3));
2846 WMAddToArray(array, (void *) data);
2847 start = fa + len + 4;
2849 } else {
2850 if (start && strlen(start)) {
2851 data = WMCreateDataWithBytes((void *)start, strlen(start));
2852 WMSetDataFormat(data, TYPETEXT);
2853 WMAddToArray(array, (void *) data);
2855 start = fa;
2859 wfree(stream);
2860 return array;
2861 WMFreeArray(array);
2865 WMText *
2866 WMCreateTextForDocumentType(WMWidget *parent,
2867 WMAction *parser, WMAction *writer)
2869 Text *tPtr = wmalloc(sizeof(Text));
2870 if (!tPtr) {
2871 printf("could not create text widget\n");
2872 return NULL;
2876 memset(tPtr, 0, sizeof(Text));
2877 tPtr->widgetClass = WC_Text;
2878 tPtr->view = W_CreateView(W_VIEW(parent));
2879 if (!tPtr->view) {
2880 perror("could not create text's view\n");
2881 wfree(tPtr);
2882 return NULL;
2884 tPtr->view->self = tPtr;
2885 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2886 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2887 W_ResizeView(tPtr->view, 250, 200);
2889 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2890 tPtr->bgGC = WMColorGC(tPtr->dColor);
2891 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2892 WMReleaseColor(tPtr->dColor);
2894 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2895 tPtr->fgGC = WMColorGC(tPtr->dColor);
2897 tPtr->ruler = NULL;
2898 tPtr->vS = NULL;
2899 tPtr->hS = NULL;
2901 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2903 tPtr->view->delegate = &_TextViewDelegate;
2905 tPtr->delegate = NULL;
2907 #if DO_BLINK
2908 tPtr->timerID = NULL;
2909 #endif
2911 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2912 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2913 handleEvents, tPtr);
2915 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2916 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2917 handleActionEvents, tPtr);
2919 WMAddNotificationObserver(ownershipObserver, tPtr,
2920 "_lostOwnership", tPtr);
2922 if(0){
2923 char *types[2] = {"application/X-color", NULL};
2924 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2925 WMRegisterViewForDraggedTypes(tPtr->view, types);
2928 WMAddNotificationObserver(fontChanged, tPtr,
2929 "WMFontPanelDidChangeNotification", tPtr);
2931 tPtr->firstTextBlock = NULL;
2932 tPtr->lastTextBlock = NULL;
2933 tPtr->currentTextBlock = NULL;
2934 tPtr->tpos = 0;
2936 tPtr->gfxItems = WMCreateArray(4);
2938 tPtr->parser = parser;
2939 tPtr->writer = writer;
2941 tPtr->sel.x = tPtr->sel.y = 2;
2942 tPtr->sel.w = tPtr->sel.h = 0;
2944 tPtr->clicked.x = tPtr->clicked.y = 2;
2946 tPtr->visible.x = tPtr->visible.y = 2;
2947 tPtr->visible.h = tPtr->view->size.height;
2948 tPtr->visible.w = tPtr->view->size.width - 4;
2950 tPtr->cursor.x = -23;
2952 tPtr->docWidth = 0;
2953 tPtr->docHeight = 0;
2954 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2955 default_bullet);
2956 tPtr->db = (Pixmap) NULL;
2958 tPtr->margins = WMGetRulerMargins(NULL);
2959 tPtr->margins->right = tPtr->visible.w;
2960 tPtr->nMargins = 1;
2962 tPtr->flags.rulerShown = False;
2963 tPtr->flags.monoFont = False;
2964 tPtr->flags.focused = False;
2965 tPtr->flags.editable = True;
2966 tPtr->flags.ownsSelection = False;
2967 tPtr->flags.pointerGrabbed = False;
2968 tPtr->flags.extendSelection = False;
2969 tPtr->flags.frozen = False;
2970 tPtr->flags.cursorShown = True;
2971 tPtr->flags.acceptsGraphic = False;
2972 tPtr->flags.horizOnDemand = False;
2973 tPtr->flags.needsLayOut = False;
2974 tPtr->flags.ignoreNewLine = False;
2975 tPtr->flags.indentNewLine = False;
2976 tPtr->flags.laidOut = False;
2977 tPtr->flags.waitingForSelection = False;
2978 tPtr->flags.prepend = False;
2979 tPtr->flags.isOverGraphic = False;
2980 tPtr->flags.relief = WRSunken;
2981 tPtr->flags.isOverGraphic = 0;
2982 tPtr->flags.alignment = WALeft;
2983 tPtr->flags.first = True;
2985 return tPtr;
2988 void
2989 WMPrependTextStream(WMText *tPtr, char *text)
2991 CHECK_CLASS(tPtr, WC_Text);
2993 if(!text) {
2994 if(tPtr->flags.ownsSelection)
2995 releaseSelection(tPtr);
2996 else
2997 clearText(tPtr);
2998 return;
3001 tPtr->flags.prepend = True;
3002 if (text && tPtr->parser)
3003 (tPtr->parser) (tPtr, (void *) text);
3004 else
3005 insertPlainText(tPtr, text);
3007 tPtr->flags.needsLayOut = True;
3011 void
3012 WMAppendTextStream(WMText *tPtr, char *text)
3014 CHECK_CLASS(tPtr, WC_Text);
3016 if(!text) {
3017 if(tPtr->flags.ownsSelection)
3018 releaseSelection(tPtr);
3019 else
3020 clearText(tPtr);
3021 return;
3024 tPtr->flags.prepend = False;
3025 if (text && tPtr->parser)
3026 (tPtr->parser) (tPtr, (void *) text);
3027 else
3028 insertPlainText(tPtr, text);
3030 tPtr->flags.needsLayOut = True;
3035 char *
3036 WMGetTextStream(WMText *tPtr)
3038 CHECK_CLASS(tPtr, WC_Text);
3039 return getStream(tPtr, 0, 0);
3042 char *
3043 WMGetTextSelectedStream(WMText *tPtr)
3045 CHECK_CLASS(tPtr, WC_Text);
3046 return getStream(tPtr, 1, 0);
3049 WMArray *
3050 WMGetTextObjects(WMText *tPtr)
3052 CHECK_CLASS(tPtr, WC_Text);
3053 return getStreamObjects(tPtr, 0);
3056 WMArray *
3057 WMGetTextSelectedObjects(WMText *tPtr)
3059 CHECK_CLASS(tPtr, WC_Text);
3060 return getStreamObjects(tPtr, 1);
3064 void
3065 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3067 CHECK_CLASS(tPtr, WC_Text);
3069 tPtr->delegate = delegate;
3073 void *
3074 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3075 char *description, WMColor *color,
3076 unsigned short first, unsigned short extraInfo)
3078 TextBlock *tb;
3080 if (!w || !description || !color)
3081 return NULL;
3083 tb = wmalloc(sizeof(TextBlock));
3084 if (!tb)
3085 return NULL;
3087 tb->text = wstrdup(description);
3088 tb->used = strlen(description);
3089 tb->blank = False;
3090 tb->d.widget = w;
3091 tb->color = WMRetainColor(color);
3092 tb->marginN = newMargin(tPtr, NULL);
3093 tb->allocated = extraInfo;
3094 tb->first = first;
3095 tb->kanji = False;
3096 tb->graphic = True;
3097 tb->object = True;
3098 tb->underlined = False;
3099 tb->selected = False;
3100 tb->script = 0;
3101 tb->sections = NULL;
3102 tb->nsections = 0;
3103 tb->prior = NULL;
3104 tb->next = NULL;
3106 return tb;
3110 void *
3111 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3112 char *description, WMColor *color,
3113 unsigned short first, unsigned short extraInfo)
3115 TextBlock *tb;
3117 if (!p || !description || !color)
3118 return NULL;
3120 tb = wmalloc(sizeof(TextBlock));
3121 if (!tb)
3122 return NULL;
3124 tb->text = wstrdup(description);
3125 tb->used = strlen(description);
3126 tb->blank = False;
3127 tb->d.pixmap = WMRetainPixmap(p);
3128 tb->color = WMRetainColor(color);
3129 tb->marginN = newMargin(tPtr, NULL);
3130 tb->allocated = extraInfo;
3131 tb->first = first;
3132 tb->kanji = False;
3133 tb->graphic = True;
3134 tb->object = False;
3135 tb->underlined = False;
3136 tb->selected = False;
3137 tb->script = 0;
3138 tb->sections = NULL;
3139 tb->nsections = 0;
3140 tb->prior = NULL;
3141 tb->next = NULL;
3143 return tb;
3146 void *
3147 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3148 unsigned short first, unsigned short len)
3150 TextBlock *tb;
3152 if (!font || !color)
3153 return NULL;
3155 tb = wmalloc(sizeof(TextBlock));
3156 if (!tb)
3157 return NULL;
3159 tb->allocated = reqBlockSize(len);
3160 tb->text = (char *)wmalloc(tb->allocated);
3161 memset(tb->text, 0, tb->allocated);
3163 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3164 *tb->text = ' ';
3165 tb->used = 1;
3166 tb->blank = True;
3167 } else {
3168 memcpy(tb->text, text, len);
3169 tb->used = len;
3170 tb->blank = False;
3172 tb->text[tb->used] = 0;
3174 tb->d.font = WMRetainFont(font);
3175 tb->color = WMRetainColor(color);
3176 tb->marginN = newMargin(tPtr, NULL);
3177 tb->first = first;
3178 tb->kanji = False;
3179 tb->graphic = False;
3180 tb->underlined = False;
3181 tb->selected = False;
3182 tb->script = 0;
3183 tb->sections = NULL;
3184 tb->nsections = 0;
3185 tb->prior = NULL;
3186 tb->next = NULL;
3187 return tb;
3190 void
3191 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3192 unsigned int kanji, unsigned int underlined, int script,
3193 WMRulerMargins *margins)
3195 TextBlock *tb = (TextBlock *) vtb;
3196 if (!tb)
3197 return;
3199 tb->first = first;
3200 tb->kanji = kanji;
3201 tb->underlined = underlined;
3202 tb->script = script;
3203 tb->marginN = newMargin(tPtr, margins);
3206 void
3207 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3208 unsigned int *kanji, unsigned int *underlined, int *script,
3209 WMRulerMargins *margins)
3211 TextBlock *tb = (TextBlock *) vtb;
3212 if (!tb)
3213 return;
3215 if (first) *first = tb->first;
3216 if (kanji) *kanji = tb->kanji;
3217 if (underlined) *underlined = tb->underlined;
3218 if (script) *script = tb->script;
3219 if (margins) margins = &tPtr->margins[tb->marginN];
3224 void
3225 WMPrependTextBlock(WMText *tPtr, void *vtb)
3227 TextBlock *tb = (TextBlock *)vtb;
3229 if (!tPtr || !tb)
3230 return;
3232 if (tb->graphic) {
3233 if(tb->object) {
3234 WMWidget *w = tb->d.widget;
3235 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3236 handleWidgetPress, tb);
3237 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3238 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3239 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3242 WMAddToArray(tPtr->gfxItems, (void *)tb);
3243 tPtr->tpos = 0;
3244 } else tPtr->tpos = tb->used;
3246 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3247 tb->next = tb->prior = NULL;
3248 tb->first = True;
3249 tPtr->lastTextBlock = tPtr->firstTextBlock
3250 = tPtr->currentTextBlock = tb;
3251 return;
3254 if(!tb->first) {
3255 tb->marginN = tPtr->currentTextBlock->marginN;
3258 tb->next = tPtr->currentTextBlock;
3259 tb->prior = tPtr->currentTextBlock->prior;
3260 if (tPtr->currentTextBlock->prior)
3261 tPtr->currentTextBlock->prior->next = tb;
3263 tPtr->currentTextBlock->prior = tb;
3264 if (!tb->prior)
3265 tPtr->firstTextBlock = tb;
3267 tPtr->currentTextBlock = tb;
3271 void
3272 WMAppendTextBlock(WMText *tPtr, void *vtb)
3274 TextBlock *tb = (TextBlock *)vtb;
3276 if (!tPtr || !tb)
3277 return;
3279 if (tb->graphic) {
3280 if(tb->object) {
3281 WMWidget *w = tb->d.widget;
3282 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3283 handleWidgetPress, tb);
3284 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3285 (W_VIEW(w))->attribs.cursor =
3286 tPtr->view->screen->defaultCursor;
3287 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3290 WMAddToArray(tPtr->gfxItems, (void *)tb);
3291 tPtr->tpos = 0;
3292 } else tPtr->tpos = tb->used;
3294 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3295 tb->next = tb->prior = NULL;
3296 tb->first = True;
3297 tPtr->lastTextBlock = tPtr->firstTextBlock
3298 = tPtr->currentTextBlock = tb;
3299 return;
3302 if(!tb->first) {
3303 tb->marginN = tPtr->currentTextBlock->marginN;
3306 tb->next = tPtr->currentTextBlock->next;
3307 tb->prior = tPtr->currentTextBlock;
3308 if (tPtr->currentTextBlock->next)
3309 tPtr->currentTextBlock->next->prior = tb;
3311 tPtr->currentTextBlock->next = tb;
3313 if (!tb->next)
3314 tPtr->lastTextBlock = tb;
3316 tPtr->currentTextBlock = tb;
3319 void *
3320 WMRemoveTextBlock(WMText *tPtr)
3322 TextBlock *tb = NULL;
3324 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3325 || !tPtr->currentTextBlock) {
3326 printf("cannot remove non existent TextBlock!\b");
3327 return NULL;
3330 tb = tPtr->currentTextBlock;
3331 if (tb->graphic) {
3332 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3334 if(tb->object) {
3335 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3336 handleWidgetPress, tb);
3337 WMUnmapWidget(tb->d.widget);
3341 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3342 if (tPtr->currentTextBlock->next)
3343 tPtr->currentTextBlock->next->prior = NULL;
3345 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3346 tPtr->currentTextBlock = tPtr->firstTextBlock;
3348 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3349 tPtr->currentTextBlock->prior->next = NULL;
3350 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3351 tPtr->currentTextBlock = tPtr->lastTextBlock;
3352 } else {
3353 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3354 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3355 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3358 return (void *)tb;
3361 void
3362 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3364 TextBlock *tb = (TextBlock *)vtb;
3365 if (!tPtr || !tb)
3366 return;
3368 if (tb->graphic) {
3369 if(tb->object) {
3370 /* naturally, there's a danger to destroying
3371 widgets whose action brings us here:
3372 ie. press a button to destroy it... need to
3373 find a safer way. till then... this stays commented out */
3374 /* WMDestroyWidget(tb->d.widget);
3375 wfree(tb->d.widget); */
3376 tb->d.widget = NULL;
3377 } else {
3378 WMReleasePixmap(tb->d.pixmap);
3379 tb->d.pixmap = NULL;
3381 } else {
3382 WMReleaseFont(tb->d.font);
3385 WMReleaseColor(tb->color);
3386 if (tb->sections && tb->nsections > 0)
3387 wfree(tb->sections);
3388 wfree(tb->text);
3389 wfree(tb);
3390 tb = NULL;
3395 void
3396 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3398 if (!tPtr)
3399 return;
3401 if (color)
3402 tPtr->fgGC = WMColorGC(color);
3403 else
3404 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3406 paintText(tPtr);
3409 void
3410 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3412 if (!tPtr)
3413 return;
3415 if (color) {
3416 tPtr->bgGC = WMColorGC(color);
3417 W_SetViewBackgroundColor(tPtr->view, color);
3418 } else {
3419 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3420 W_SetViewBackgroundColor(tPtr->view,
3421 WMWhiteColor(tPtr->view->screen));
3424 paintText(tPtr);
3427 void
3428 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3430 if (!tPtr)
3431 return;
3432 tPtr->flags.relief = relief;
3433 textDidResize(tPtr->view->delegate, tPtr->view);
3436 void
3437 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3439 if (!tPtr)
3440 return;
3442 if (shouldhave && !tPtr->hS) {
3443 tPtr->hS = WMCreateScroller(tPtr);
3444 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3445 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3446 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3447 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3448 WMMapWidget(tPtr->hS);
3449 } else if (!shouldhave && tPtr->hS) {
3450 WMUnmapWidget(tPtr->hS);
3451 WMDestroyWidget(tPtr->hS);
3452 tPtr->hS = NULL;
3455 tPtr->hpos = 0;
3456 tPtr->prevHpos = 0;
3457 textDidResize(tPtr->view->delegate, tPtr->view);
3461 void
3462 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3464 if (!tPtr)
3465 return;
3467 if(shouldhave && !tPtr->ruler) {
3468 tPtr->ruler = WMCreateRuler(tPtr);
3469 (W_VIEW(tPtr->ruler))->attribs.cursor =
3470 tPtr->view->screen->defaultCursor;
3471 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3472 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3473 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3474 } else if(!shouldhave && tPtr->ruler) {
3475 WMShowTextRuler(tPtr, False);
3476 WMDestroyWidget(tPtr->ruler);
3477 tPtr->ruler = NULL;
3479 textDidResize(tPtr->view->delegate, tPtr->view);
3482 void
3483 WMShowTextRuler(WMText *tPtr, Bool show)
3485 if(!tPtr)
3486 return;
3487 if(!tPtr->ruler)
3488 return;
3490 if(tPtr->flags.monoFont)
3491 show = False;
3493 tPtr->flags.rulerShown = show;
3494 if(show) {
3495 WMMapWidget(tPtr->ruler);
3496 } else {
3497 WMUnmapWidget(tPtr->ruler);
3500 textDidResize(tPtr->view->delegate, tPtr->view);
3503 Bool
3504 WMGetTextRulerShown(WMText *tPtr)
3506 if(!tPtr)
3507 return 0;
3509 if(!tPtr->ruler)
3510 return 0;
3512 return tPtr->flags.rulerShown;
3516 void
3517 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3519 if (!tPtr)
3520 return;
3522 if (shouldhave && !tPtr->vS) {
3523 tPtr->vS = WMCreateScroller(tPtr);
3524 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3525 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3526 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3527 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3528 WMMapWidget(tPtr->vS);
3529 } else if (!shouldhave && tPtr->vS) {
3530 WMUnmapWidget(tPtr->vS);
3531 WMDestroyWidget(tPtr->vS);
3532 tPtr->vS = NULL;
3535 tPtr->vpos = 0;
3536 tPtr->prevVpos = 0;
3537 textDidResize(tPtr->view->delegate, tPtr->view);
3542 Bool
3543 WMScrollText(WMText *tPtr, int amount)
3545 Bool scroll=False;
3546 if (!tPtr)
3547 return False;
3548 if (amount == 0 || !tPtr->view->flags.realized)
3549 return False;
3551 if (amount < 0) {
3552 if (tPtr->vpos > 0) {
3553 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3554 else tPtr->vpos=0;
3555 scroll=True;
3556 } } else {
3557 int limit = tPtr->docHeight - tPtr->visible.h;
3558 if (tPtr->vpos < limit) {
3559 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3560 else tPtr->vpos = limit;
3561 scroll = True;
3562 } }
3564 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3565 updateScrollers(tPtr);
3566 paintText(tPtr);
3568 tPtr->prevVpos = tPtr->vpos;
3569 return scroll;
3572 Bool
3573 WMPageText(WMText *tPtr, Bool direction)
3575 if (!tPtr) return False;
3576 if (!tPtr->view->flags.realized) return False;
3578 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3581 void
3582 WMSetTextEditable(WMText *tPtr, Bool editable)
3584 if (!tPtr)
3585 return;
3586 tPtr->flags.editable = editable;
3589 int
3590 WMGetTextEditable(WMText *tPtr)
3592 if (!tPtr)
3593 return 0;
3594 return tPtr->flags.editable;
3597 void
3598 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3600 if (!tPtr)
3601 return;
3602 tPtr->flags.indentNewLine = indent;
3605 void
3606 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3608 if (!tPtr)
3609 return;
3610 // tPtr->flags.ignoreNewLine = ignore;
3613 Bool
3614 WMGetTextIgnoresNewline(WMText *tPtr)
3616 if (!tPtr)
3617 return True;
3618 return tPtr->flags.ignoreNewLine;
3621 void
3622 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3624 if (!tPtr)
3625 return;
3626 if (mono && tPtr->flags.rulerShown)
3627 WMShowTextRuler(tPtr, False);
3629 tPtr->flags.monoFont = mono;
3630 WMThawText(tPtr);
3633 Bool
3634 WMGetTextUsesMonoFont(WMText *tPtr)
3636 if (!tPtr)
3637 return True;
3638 return tPtr->flags.monoFont;
3642 void
3643 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3645 if (!tPtr)
3646 return;
3648 WMReleaseFont(tPtr->dFont);
3649 if (font)
3650 tPtr->dFont = WMRetainFont(font);
3651 else
3652 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3655 WMFont *
3656 WMGetTextDefaultFont(WMText *tPtr)
3658 if (!tPtr)
3659 return NULL;
3660 else
3661 return tPtr->dFont;
3664 void
3665 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3667 if (!tPtr)
3668 return;
3669 tPtr->flags.alignment = alignment;
3670 WMThawText(tPtr);
3673 int
3674 WMGetTextInsertType(WMText *tPtr)
3676 if (!tPtr)
3677 return 0;
3678 return tPtr->flags.prepend;
3682 void
3683 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3685 if (!tPtr || !color)
3686 return;
3688 setSelectionProperty(tPtr, NULL, color, -1);
3691 void
3692 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3694 if (!tPtr || !font)
3695 return;
3697 setSelectionProperty(tPtr, font, NULL, -1) ;
3700 void
3701 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3703 if (!tPtr || (underlined!=0 && underlined !=1))
3704 return;
3706 setSelectionProperty(tPtr, NULL, NULL, underlined);
3712 void
3713 WMFreezeText(WMText *tPtr)
3715 if (!tPtr)
3716 return;
3718 tPtr->flags.frozen = True;
3722 void
3723 WMThawText(WMText *tPtr)
3725 if (!tPtr)
3726 return;
3728 tPtr->flags.frozen = False;
3730 if(tPtr->flags.monoFont) {
3731 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3732 TextBlock *tb;
3734 /* make sure to unmap widgets no matter where they are */
3735 /* they'll be later remapped if needed by paintText */
3736 for(j=0; j<c; j++) {
3737 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3738 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3739 WMUnmapWidget(tb->d.widget);
3745 tPtr->flags.laidOut = False;
3746 layOutDocument(tPtr);
3747 updateScrollers(tPtr);
3748 paintText(tPtr);
3749 tPtr->flags.needsLayOut = False;
3754 static char *
3755 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
3756 Bool caseSensitive)
3758 char *ptr;
3760 if(!haystack || !needle)
3761 return NULL;
3763 for (ptr = haystack-2; ptr > end; ptr--) {
3764 if(caseSensitive) {
3765 if (*ptr == *needle && !strncmp(ptr, needle, len))
3766 return ptr;
3767 } else {
3768 if (tolower(*ptr) == tolower(*needle) &&
3769 strncasecmp(ptr, needle, len))
3770 return ptr;
3774 return NULL;
3778 Bool
3779 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3780 Bool caseSensitive)
3782 TextBlock *tb;
3783 char *mark;
3784 unsigned short pos;
3786 if (!tPtr || !needle)
3787 return False;
3789 if (! (tb = tPtr->currentTextBlock)) {
3790 if (! (tb = ( (direction > 0) ?
3791 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3792 return False;
3794 } else {
3795 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3796 tb = (direction>0) ? tb->next : tb->prior; */
3797 if(tb != tPtr->lastTextBlock)
3798 tb = tb->prior;
3802 while(tb) {
3803 if (!tb->graphic) {
3804 pos = tPtr->tpos;
3805 if(pos+1 < tb->used)
3806 pos++;
3808 if(tb->used - pos> 0 && pos > 0) {
3809 char tmp = tb->text[tb->used];
3810 tb->text[tb->used] = 0;
3812 output(&tb->text[pos], tb->used - pos);
3813 if(direction > 0)
3814 mark = strstr(&tb->text[pos], needle);
3815 else
3816 mark = mystrrstr(&tb->text[pos], needle,
3817 strlen(needle), tb->text, caseSensitive);
3819 tb->text[tb->used] = tmp;
3821 } else {
3822 return False;
3825 if(mark) {
3826 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3828 tPtr->tpos = (int)(mark - tb->text);
3829 tPtr->currentTextBlock = tb;
3830 updateCursorPosition(tPtr);
3831 tPtr->sel.y = tPtr->cursor.y+5;
3832 tPtr->sel.h = tPtr->cursor.h-10;
3833 tPtr->sel.x = tPtr->cursor.x +1;
3834 tPtr->sel.w = WMIN(WMWidthOfString(font,
3835 &tb->text[tPtr->tpos], strlen(needle)),
3836 tPtr->docWidth - tPtr->sel.x);
3837 tPtr->flags.ownsSelection = True;
3838 paintText(tPtr);
3840 return True;
3844 tb = (direction>0) ? tb->next : tb->prior;
3845 pos = 0;
3848 return False;