added support for Getting and Setting Selection Fonts/Colors/Underline...
[wmaker-crm.git] / WINGs / wtext.c
blobb81eeef48caabdec356b2eadfd7739c5269418ec
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
11 /* TODO:
12 * - FIX wrap... long lines that don't fit are not char wrapped yet.
13 * - hrm... something to do with already having tbs...
14 * - selection code... selects can be funny if it crosses over. use rect?
15 * - also inspect behaviour for WACenter and WARight
16 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
17 * - check if support for Horizontal Scroll is complete
18 * - assess danger of destroying widgets whose actions link to other pages
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
25 /* a Section is a section of a TextBlock that describes what parts
26 of a TextBlock has been laid out on which "line"...
27 o this greatly aids redraw, scroll and selection.
28 o this is created during layoutLine, but may be later modified.
29 o there may be many Sections per TextBlock, hence the array */
30 typedef struct {
31 unsigned int x, y; /* where to draw it from */
32 unsigned short w, h; /* its width and height */
33 unsigned short begin; /* where the layout begins */
34 unsigned short end ; /* where it ends */
35 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
36 unsigned short last:1; /* is it the last section on a "line"? */
37 unsigned int _y:31; /* the "line" it and other textblocks are on */
38 } Section;
41 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
42 o text for the block, color and font
43 o or a pointer to the pixmap
44 o OR a pointer to the widget and the (text) description for its graphic
47 typedef struct _TextBlock {
48 struct _TextBlock *next; /* next text block in linked list */
49 struct _TextBlock *prior; /* prior text block in linked list */
51 char *text; /* pointer to text (could be kanji) */
52 /* or to the object's description */
53 union {
54 WMFont *font; /* the font */
55 WMWidget *widget; /* the embedded widget */
56 WMPixmap *pixmap; /* the pixmap */
57 } d; /* description */
59 unsigned short used; /* number of chars in this block */
60 unsigned short allocated; /* size of allocation (in chars) */
61 WMColor *color; /* the color */
63 Section *sections; /* the region for layouts (a growable array) */
64 /* an _array_! of size _nsections_ */
66 unsigned short s_begin; /* where the selection begins */
67 unsigned short s_end; /* where it ends */
69 unsigned int first:1; /* first TextBlock in paragraph */
70 unsigned int blank:1; /* ie. blank paragraph */
71 unsigned int kanji:1; /* is of 16-bit characters or not */
72 unsigned int graphic:1; /* graphic or text: text=0 */
73 unsigned int object:1; /* embedded object or pixmap */
74 unsigned int underlined:1; /* underlined or not */
75 unsigned int selected:1; /* selected or not */
76 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
77 int script:8; /* script in points: negative for subscript */
78 unsigned int marginN:8; /* which of the margins in the tPtr to use */
79 unsigned int nClicks:2; /* single, double, triple clicks */
80 unsigned int RESERVED:7;
81 } TextBlock;
84 /* I'm lazy: visible.h vs. visible.size.height :-) */
85 typedef struct {
86 unsigned int y;
87 unsigned int x;
88 unsigned int h;
89 unsigned int w;
90 } myRect;
93 typedef struct W_Text {
94 W_Class widgetClass; /* the class number of this widget */
95 W_View *view; /* the view referring to this instance */
97 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
99 WMScroller *vS; /* the vertical scroller */
100 unsigned int vpos; /* the current vertical position */
101 unsigned int prevVpos; /* the previous vertical position */
103 WMScroller *hS; /* the horizontal scroller */
104 unsigned int hpos; /* the current horizontal position */
105 unsigned int prevHpos; /* the previous horizontal position */
107 WMFont *dFont; /* the default font */
108 WMColor *dColor; /* the default color */
109 WMPixmap *dBulletPix; /* the default pixmap for bullets */
111 GC bgGC; /* the background GC to draw with */
112 GC fgGC; /* the foreground GC to draw with */
113 Pixmap db; /* the buffer on which to draw */
115 myRect visible; /* the actual rectangle that can be drawn into */
116 myRect cursor; /* the position and (height) of cursor */
117 myRect sel; /* the selection rectangle */
119 WMPoint clicked; /* where in the _document_ was clicked */
121 unsigned short tpos; /* the position in the currentTextBlock */
122 unsigned short docWidth; /* the width of the entire document */
123 unsigned int docHeight; /* the height of the entire document */
125 TextBlock *firstTextBlock;
126 TextBlock *lastTextBlock;
127 TextBlock *currentTextBlock;
129 WMArray *gfxItems; /* a nice array of graphic items */
131 #if DO_BLINK
132 WMHandlerID timerID; /* for nice twinky-winky */
133 #endif
135 WMAction *parser;
136 WMAction *writer;
137 WMTextDelegate *delegate;
138 Time lastClickTime;
140 WMRulerMargins *margins; /* an array of margins */
142 unsigned int nMargins:7; /* the total number of margins in use */
143 struct {
144 unsigned int monoFont:1; /* whether to ignore formats and graphic */
145 unsigned int focused:1; /* whether this instance has input focus */
146 unsigned int editable:1; /* "silly user, you can't edit me" */
147 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
148 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
149 unsigned int extendSelection:1; /* shift-drag to select more regions */
151 unsigned int rulerShown:1; /* whether the ruler is shown or not */
152 unsigned int frozen:1; /* whether screen updates are to be made */
153 unsigned int cursorShown:1; /* whether to show the cursor */
154 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
155 unsigned int horizOnDemand:1;/* if a large image should appear*/
156 unsigned int needsLayOut:1; /* in case of Append/Deletes */
157 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
158 unsigned int indentNewLine:1;/* add " " for a newline typed */
159 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
160 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
161 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
162 WMAlignment alignment:2; /* the alignment for text */
163 WMReliefType relief:3; /* the relief to display with */
164 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
165 unsigned int first:1; /* for plain text parsing, newline? */
166 /* unsigned int RESERVED:1; */
167 } flags;
168 } Text;
171 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
172 if ((T)->delegate && (T)->delegate->C)\
173 (*(T)->delegate->C)((T)->delegate,notif);\
174 WMPostNotification(notif);\
175 WMReleaseNotification(notif);}
178 #define TYPETEXT 0
180 static void
181 output(char *ptr, int len)
183 char s[len+1];
184 memcpy(s, ptr, len);
185 s[len] = 0;
186 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
187 printf("[%s]\n", s);
191 #if DO_BLINK
192 #define CURSOR_BLINK_ON_DELAY 600
193 #define CURSOR_BLINK_OFF_DELAY 400
194 #endif
196 static char *default_bullet[] = {
197 "6 6 4 1",
198 " c None s None", ". c black",
199 "X c white", "o c #808080",
200 " ... ",
201 ".XX.. ",
202 ".XX..o",
203 ".....o",
204 " ...oo",
205 " ooo "};
207 static void handleEvents(XEvent *event, void *data);
208 static void layOutDocument(Text *tPtr);
209 static void updateScrollers(Text *tPtr);
212 static int
213 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
215 unsigned int i=0;
217 for(i=0; i < tPtr->nMargins; i++) {
219 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
220 return i;
223 return -1;
228 static int
229 newMargin(Text *tPtr, WMRulerMargins *margins)
231 int n;
233 if (!margins) {
234 tPtr->margins[0].retainCount++;
235 return 0;
238 n = getMarginNumber(tPtr, margins);
240 if (n == -1) {
242 tPtr->margins = wrealloc(tPtr->margins,
243 (++tPtr->nMargins)*sizeof(WMRulerMargins));
245 n = tPtr->nMargins-1;
246 tPtr->margins[n].left = margins->left;
247 tPtr->margins[n].first = margins->first;
248 tPtr->margins[n].body = margins->body;
249 tPtr->margins[n].right = margins->right;
250 /* for each tab... */
251 tPtr->margins[n].retainCount = 1;
252 } else {
253 tPtr->margins[n].retainCount++;
256 return n;
259 static Bool
260 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
262 unsigned short i, w, lw, selected = False, extend = False;
263 myRect sel;
266 /* if selection rectangle completely encloses the section */
267 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
268 && (tb->sections[s]._y + tb->sections[s].h
269 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
270 sel.x = 0;
271 sel.w = tPtr->visible.w;
272 selected = extend = True;
274 /* or if it starts on a line and then goes further down */
275 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
276 && (tb->sections[s]._y + tb->sections[s].h
277 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
278 && (tb->sections[s]._y + tb->sections[s].h
279 >= tPtr->visible.y + tPtr->sel.y) ) {
280 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
281 sel.w = tPtr->visible.w;
282 selected = extend = True;
284 /* or if it begins before a line, but ends on it */
285 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
286 && (tb->sections[s]._y + tb->sections[s].h
287 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
288 && (tb->sections[s]._y
289 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
291 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
292 sel.w = tPtr->sel.x + tPtr->sel.w;
293 else
294 sel.w = tPtr->sel.x;
296 sel.x = 0;
297 selected = True;
299 /* or if the selection rectangle lies entirely within a line */
300 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
301 && (tPtr->sel.w >= 2)
302 && (tb->sections[s]._y + tb->sections[s].h
303 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
304 sel.x = tPtr->sel.x;
305 sel.w = tPtr->sel.w;
306 selected = True;
309 if (selected) {
310 selected = False;
312 /* if not within (modified) selection rectangle */
313 if ( tb->sections[s].x > sel.x + sel.w
314 || tb->sections[s].x + tb->sections[s].w < sel.x)
315 return False;
317 if (tb->graphic) {
318 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
319 && tb->sections[s].x >= sel.x) {
320 rect->width = tb->sections[s].w;
321 rect->x = tb->sections[s].x;
322 selected = True;
324 } else {
326 i = tb->sections[s].begin;
327 lw = 0;
329 if (0&& tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
334 while (++i <= tb->sections[s].end) {
336 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
337 lw += w;
339 if (lw + tb->sections[s].x >= sel.x
340 || i == tb->sections[s].end ) {
341 lw -= w;
342 i--;
343 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
344 break;
348 if (i > tb->sections[s].end) {
349 printf("WasSelected: (i > tb->sections[s].end) \n");
350 return False;
353 _selEnd: rect->x = tb->sections[s].x + lw;
354 lw = 0;
355 while(++i <= tb->sections[s].end) {
357 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
358 lw += w;
360 if (lw + rect->x >= sel.x + sel.w
361 || i == tb->sections[s].end ) {
363 if (i != tb->sections[s].end) {
364 lw -= w;
365 i--;
368 rect->width = lw;
369 if (tb->sections[s].last && sel.x + sel.w
370 >= tb->sections[s].x + tb->sections[s].w
371 && extend ) {
372 rect->width += (tPtr->visible.w - rect->x - lw);
375 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
376 selected = True;
377 break;
378 } } } }
380 if (selected) {
381 rect->y = tb->sections[s]._y - tPtr->vpos;
382 rect->height = tb->sections[s].h;
383 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
385 return selected;
389 static void
390 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
392 TextBlock *tb;
393 int isFont=False;
395 tb = tPtr->firstTextBlock;
396 if (!tb || !tPtr->flags.ownsSelection)
397 return;
399 if(font && (!color || underlined==-1))
400 isFont = True;
402 while (tb) {
403 if (tPtr->flags.monoFont || tb->selected) {
405 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
406 || tb->graphic) {
408 if(isFont) {
409 if(!tb->graphic) {
410 WMReleaseFont(tb->d.font);
411 tb->d.font = WMRetainFont(font);
413 } else if(underlined !=-1) {
414 tb->underlined = underlined;
415 } else {
416 WMReleaseColor(tb->color);
417 tb->color = WMRetainColor(color);
420 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
422 TextBlock *midtb, *otb = tb;
424 if(underlined != -1) {
425 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
426 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
427 False, (tb->s_end - tb->s_begin));
428 } else {
429 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
430 &(tb->text[tb->s_begin]),
431 (isFont?font:tb->d.font),
432 (isFont?tb->color:color),
433 False, (tb->s_end - tb->s_begin));
437 if (midtb) {
438 if(underlined != -1) {
439 midtb->underlined = underlined;
440 } else {
441 midtb->underlined = otb->underlined;
444 midtb->selected = !True;
445 midtb->s_begin = 0;
446 midtb->s_end = midtb->used;
447 tPtr->currentTextBlock = tb;
448 WMAppendTextBlock(tPtr, midtb);
449 tb = tPtr->currentTextBlock;
452 if (otb->used - otb->s_end > 0) {
453 TextBlock *ntb;
454 ntb = (TextBlock *)
455 WMCreateTextBlockWithText(tPtr,
456 &(otb->text[otb->s_end]), otb->d.font, otb->color,
457 False, otb->used - otb->s_end);
459 if (ntb) {
460 ntb->underlined = otb->underlined;
461 ntb->selected = False;
462 WMAppendTextBlock(tPtr, ntb);
463 tb = tPtr->currentTextBlock;
467 if (midtb) {
468 tPtr->currentTextBlock = midtb;
471 otb->selected = False;
472 otb->used = otb->s_begin;
476 tb = tb->next;
479 tPtr->flags.needsLayOut = True;
480 WMThawText(tPtr);
482 /* in case the size changed... */
483 if(isFont && tPtr->currentTextBlock) {
484 TextBlock *tb = tPtr->currentTextBlock;
486 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
487 tPtr->sel.y = 3 + tb->sections[0]._y;
488 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
489 tPtr->sel.w = tb->sections[tb->nsections-1].w;
490 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
491 tPtr->sel.x = 0;
493 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
499 static Bool
500 removeSelection(Text *tPtr)
502 TextBlock *tb = NULL;
503 Bool first = False;
505 if (!(tb = tPtr->firstTextBlock))
506 return False;
508 while (tb) {
509 if (tb->selected) {
510 if(!first && !tb->graphic) {
511 WMReleaseFont(tPtr->dFont);
512 tPtr->dFont = WMRetainFont(tb->d.font);
513 first = True;
516 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
517 tPtr->currentTextBlock = tb;
518 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
519 tb = tPtr->currentTextBlock;
520 if (tb)
521 tPtr->tpos = 0;
522 continue;
524 } else if (tb->s_end <= tb->used) {
525 memmove(&(tb->text[tb->s_begin]),
526 &(tb->text[tb->s_end]), tb->used - tb->s_end);
527 tb->used -= (tb->s_end - tb->s_begin);
528 tb->selected = False;
529 tPtr->tpos = tb->s_begin;
534 tb = tb->next;
536 return True;
539 static TextBlock *
540 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
542 TextBlock *hold = tb;
544 if (!tb)
545 return NULL;
547 while (tb) {
548 if (!tb->graphic)
549 break;
550 tb = (dir? tb->next : tb->prior);
553 if(!tb) {
554 tb = hold;
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir? tb->prior : tb->next);
562 if(!tb)
563 return NULL;
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)
576 return 0;
578 if (tb->graphic) {
579 tPtr->currentTextBlock =
580 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
581 tPtr->tpos = 0;
582 return 0;
586 *dir = !(y <= tb->sections[0].y);
587 if(*dir) {
588 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
589 && (y >= tb->sections[0]._y ) ) {
590 /* if it's on the same line */
591 if(x < tb->sections[0].x)
592 *dir = 0;
594 } else {
595 if ( ( y <= tb->sections[tb->nsections-1]._y
596 + tb->sections[tb->nsections-1].h )
597 && (y >= tb->sections[tb->nsections-1]._y ) ) {
598 /* if it's on the same line */
599 if(x > tb->sections[tb->nsections-1].x)
600 *dir = 1;
604 return 1;
608 static void
609 paintText(Text *tPtr)
611 TextBlock *tb;
612 WMFont *font;
613 GC gc, greyGC;
614 char *text;
615 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
616 WMScreen *scr = tPtr->view->screen;
617 Display *dpy = tPtr->view->screen->display;
618 Window win = tPtr->view->window;
620 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
621 return;
623 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
624 0, 0, tPtr->visible.w, tPtr->visible.h);
626 if (! (tb = tPtr->currentTextBlock)) {
627 if (! (tb = tPtr->firstTextBlock)) {
628 goto _copy_area;
632 if (tPtr->flags.ownsSelection)
633 greyGC = WMColorGC(WMGrayColor(scr));
635 done = False;
639 /* first, which direction? Don't waste time looking all over,
640 since the parts to be drawn will most likely be near what
641 was previously drawn */
642 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
643 goto _copy_area;
645 while(tb) {
647 if (tb->graphic && tPtr->flags.monoFont)
648 goto _getSibling;
650 if(dir) {
651 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
652 + tb->sections[tb->nsections-1].h)
653 break;
654 } else {
655 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
656 + tb->sections[tb->nsections-1].h)
657 break;
660 _getSibling:
661 if(dir) {
662 if(tb->next)
663 tb = tb->next;
664 else break;
665 } else {
666 if(tb->prior)
667 tb = tb->prior;
668 else break;
673 /* first, place all text that can be viewed */
674 while (!done && tb) {
676 /* paragraph diagnostic
677 if(tb->blank) {tb->text[0] = 'F'; } */
679 if (tb->graphic) {
680 tb = tb->next;
681 continue;
684 tb->selected = False;
686 for(s=0; s<tb->nsections && !done; s++) {
688 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
689 done = True;
690 break;
693 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
694 continue;
696 if (tPtr->flags.monoFont) {
697 font = tPtr->dFont;
698 gc = tPtr->fgGC;
699 } else {
700 font = tb->d.font;
701 gc = WMColorGC(tb->color);
704 if (tPtr->flags.ownsSelection) {
705 XRectangle rect;
707 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
708 tb->selected = True;
709 XFillRectangle(dpy, tPtr->db, greyGC,
710 rect.x, rect.y, rect.width, rect.height);
714 prev_y = tb->sections[s]._y;
716 len = tb->sections[s].end - tb->sections[s].begin;
717 text = &(tb->text[tb->sections[s].begin]);
718 y = tb->sections[s].y - tPtr->vpos;
719 WMDrawString(scr, tPtr->db, gc, font,
720 tb->sections[s].x - tPtr->hpos, y, text, len);
722 if (!tPtr->flags.monoFont && tb->underlined) {
723 XDrawLine(dpy, tPtr->db, gc,
724 tb->sections[s].x - tPtr->hpos,
725 y + font->y + 1,
726 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
727 y + font->y + 1);
732 tb = (!done? tb->next : NULL);
736 /* now , show all graphic items that can be viewed */
737 c = WMGetArrayItemCount(tPtr->gfxItems);
738 if (c > 0 && !tPtr->flags.monoFont) {
739 int j, h;
741 for(j=0; j<c; j++) {
742 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
744 /* if it's not viewable, and mapped, unmap it */
745 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
746 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
748 if(tb->object) {
749 if ((W_VIEW(tb->d.widget))->flags.mapped) {
750 WMUnmapWidget(tb->d.widget);
753 } else {
754 /* if it's viewable, and not mapped, map it */
755 if(tb->object) {
756 W_View *view = W_VIEW(tb->d.widget);
758 if (!view->flags.realized)
759 WMRealizeWidget(tb->d.widget);
760 if(!view->flags.mapped) {
761 XMapWindow(view->screen->display, view->window);
762 XFlush(view->screen->display);
763 view->flags.mapped = 1;
767 if (tPtr->flags.ownsSelection) {
768 XRectangle rect;
770 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
771 tb->selected = True;
772 XFillRectangle(dpy, tPtr->db, greyGC,
773 rect.x, rect.y, rect.width, rect.height);
777 if(tb->object) {
778 WMMoveWidget(tb->d.widget,
779 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
780 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
781 h = WMWidgetHeight(tb->d.widget) + 1;
783 } else {
784 WMDrawPixmap(tb->d.pixmap, tPtr->db,
785 tb->sections[0].x - tPtr->hpos,
786 tb->sections[0].y - tPtr->vpos);
787 h = tb->d.pixmap->height + 1;
791 if (!tPtr->flags.monoFont && tb->underlined) {
792 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
793 tb->sections[0].x - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos,
795 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
796 tb->sections[0].y + h - tPtr->vpos);
797 } } } }
800 _copy_area:
801 if (tPtr->flags.editable && tPtr->flags.cursorShown
802 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
803 int y = tPtr->cursor.y - tPtr->vpos;
804 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
805 tPtr->cursor.x, y,
806 tPtr->cursor.x, y + tPtr->cursor.h);
809 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
810 tPtr->visible.w, tPtr->visible.h,
811 tPtr->visible.x, tPtr->visible.y);
813 W_DrawRelief(scr, win, 0, 0,
814 tPtr->view->size.width, tPtr->view->size.height,
815 tPtr->flags.relief);
817 if (tPtr->ruler && tPtr->flags.rulerShown)
818 XDrawLine(dpy, win, tPtr->fgGC,
819 2, 42, tPtr->view->size.width-4, 42);
823 static void
824 mouseOverObject(Text *tPtr, int x, int y)
826 TextBlock *tb;
827 Bool result = False;
829 x -= tPtr->visible.x;
830 x += tPtr->hpos;
831 y -= tPtr->visible.y;
832 y += tPtr->vpos;
834 if(tPtr->flags.ownsSelection) {
835 if(tPtr->sel.x <= x
836 && tPtr->sel.y <= y
837 && tPtr->sel.x + tPtr->sel.w >= x
838 && tPtr->sel.y + tPtr->sel.h >= y) {
839 tPtr->flags.isOverGraphic = 1;
840 result = True;
845 if(!result) {
846 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
848 if (c<1)
849 tPtr->flags.isOverGraphic = 0;
852 for(j=0; j<c; j++) {
853 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
855 if(!tb || !tb->sections) {
856 tPtr->flags.isOverGraphic = 0;
857 return;
860 if(!tb->object) {
861 if(tb->sections[0].x <= x
862 && tb->sections[0].y <= y
863 && tb->sections[0].x + tb->sections[0].w >= x
864 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
865 tPtr->flags.isOverGraphic = 3;
866 result = True;
867 break;
875 if(!result)
876 tPtr->flags.isOverGraphic = 0;
879 tPtr->view->attribs.cursor = (result?
880 tPtr->view->screen->defaultCursor
881 : tPtr->view->screen->textCursor);
883 XSetWindowAttributes attribs;
884 attribs.cursor = tPtr->view->attribs.cursor;
885 XChangeWindowAttributes(tPtr->view->screen->display,
886 tPtr->view->window, CWCursor,
887 &attribs);
891 #if DO_BLINK
893 static void
894 blinkCursor(void *data)
896 Text *tPtr = (Text*)data;
898 if (tPtr->flags.cursorShown) {
899 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
900 blinkCursor, data);
901 } else {
902 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
903 blinkCursor, data);
905 paintText(tPtr);
906 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
908 #endif
910 static void
911 updateCursorPosition(Text *tPtr)
913 TextBlock *tb = NULL;
914 int x, y, h, s;
916 if(tPtr->flags.needsLayOut)
917 layOutDocument(tPtr);
919 if (! (tb = tPtr->currentTextBlock)) {
920 if (! (tb = tPtr->firstTextBlock)) {
921 tPtr->tpos = 0;
922 tPtr->cursor.h = tPtr->dFont->height;
923 tPtr->cursor.y = 2;
924 tPtr->cursor.x = 2;
925 return;
930 if(tb->blank) {
931 tPtr->tpos = 0;
932 y = tb->sections[0].y;
933 h = tb->sections[0].h;
934 x = tb->sections[0].x;
936 } else if(tb->graphic) {
937 y = tb->sections[0].y;
938 h = tb->sections[0].h;
939 x = tb->sections[0].x;
941 } else {
942 if(tPtr->tpos > tb->used)
943 tPtr->tpos = tb->used;
945 for(s=0; s<tb->nsections-1; s++) {
947 if(tPtr->tpos >= tb->sections[s].begin
948 && tPtr->tpos <= tb->sections[s].end)
949 break;
952 y = tb->sections[s]._y;
953 h = tb->sections[s].h;
954 x = tb->sections[s].x + WMWidthOfString(
955 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
956 &tb->text[tb->sections[s].begin],
957 tPtr->tpos - tb->sections[s].begin);
960 tPtr->cursor.y = y;
961 tPtr->cursor.h = h;
962 tPtr->cursor.x = x;
965 /* scroll the bars if the cursor is not visible */
966 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
967 if(tPtr->cursor.y+tPtr->cursor.h
968 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
969 tPtr->vpos +=
970 (tPtr->cursor.y+tPtr->cursor.h+10
971 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
972 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
973 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
978 updateScrollers(tPtr);
982 static void
983 cursorToTextPosition(Text *tPtr, int x, int y)
985 TextBlock *tb = NULL;
986 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
987 char *text;
989 if(tPtr->flags.needsLayOut)
990 layOutDocument(tPtr);
992 y += (tPtr->vpos - tPtr->visible.y);
993 if (y<0)
994 y = 0;
996 x -= (tPtr->visible.x - 2);
997 if (x<0)
998 x=0;
1000 /* clicked is relative to document, not window... */
1001 tPtr->clicked.x = x;
1002 tPtr->clicked.y = y;
1004 if (! (tb = tPtr->currentTextBlock)) {
1005 if (! (tb = tPtr->firstTextBlock)) {
1006 tPtr->tpos = 0;
1007 tPtr->cursor.h = tPtr->dFont->height;
1008 tPtr->cursor.y = 2;
1009 tPtr->cursor.x = 2;
1010 return;
1014 /* first, which direction? Most likely, newly clicked
1015 position will be close to previous */
1016 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1017 return;
1020 s = (dir? 0 : tb->nsections-1);
1021 if ( y >= tb->sections[s]._y
1022 && y <= tb->sections[s]._y + tb->sections[s].h) {
1023 goto _doneV;
1026 /* get the first (or last) section of the TextBlock that
1027 lies about the vertical click point */
1028 done = False;
1029 while (!done && tb) {
1031 if (tPtr->flags.monoFont && tb->graphic) {
1032 if( (dir?tb->next:tb->prior))
1033 tb = (dir?tb->next:tb->prior);
1034 continue;
1037 s = (dir? 0 : tb->nsections-1);
1038 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1040 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1041 ( y >= tb->sections[s]._y ) ) ) {
1042 done = True;
1043 } else {
1044 dir? s++ : s--;
1048 if (!done) {
1049 if ( (dir? tb->next : tb->prior)) {
1050 tb = (dir ? tb->next : tb->prior);
1051 } else {
1052 pos = tb->used;
1053 break; /* goto _doneH; */
1059 if (s<0 || s>=tb->nsections) {
1060 s = (dir? tb->nsections-1 : 0);
1063 _doneV:
1064 /* we have the line, which TextBlock on that line is it? */
1065 pos = (dir?0:tb->sections[s].begin);
1066 if (tPtr->flags.monoFont && tb->graphic) {
1067 TextBlock *hold = tb;
1068 tb = getFirstNonGraphicBlockFor(hold, dir);
1070 if(!tb) {
1071 tPtr->tpos = 0;
1072 tb = hold;
1073 s = 0;
1074 goto _doNothing;
1079 if(tb->blank)
1080 _w = 0;
1082 _y = tb->sections[s]._y;
1084 while (tb) {
1086 if (tPtr->flags.monoFont && tb->graphic) {
1087 tb = (dir ? tb->next : tb->prior);
1088 continue;
1091 if (dir) {
1092 if (tb->graphic) {
1093 if(tb->object)
1094 _w = WMWidgetWidth(tb->d.widget)-5;
1095 else
1096 _w = tb->d.pixmap->width-5;
1097 } else {
1098 text = &(tb->text[tb->sections[s].begin]);
1099 len = tb->sections[s].end - tb->sections[s].begin;
1100 _w = WMWidthOfString(tb->d.font, text, len);
1101 if (tb->sections[s].x + _w >= x)
1102 break;
1105 } else {
1106 if (tb->sections[s].x <= x)
1107 break;
1110 if ((dir? tb->next : tb->prior)) {
1111 TextBlock *nxt = (dir? tb->next : tb->prior);
1112 if (tPtr->flags.monoFont && nxt->graphic) {
1113 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1114 if (!nxt) {
1115 pos = (dir?0:tb->sections[s].begin);
1116 tPtr->cursor.x = tb->sections[s].x;
1117 goto _doneH;
1121 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1122 /* this must be the last/first on this line. stop */
1123 pos = (dir? tb->sections[s].end : 0);
1124 tPtr->cursor.x = tb->sections[s].x;
1125 if (!tb->blank) {
1126 if (tb->graphic) {
1127 if(tb->object)
1128 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1129 else
1130 tPtr->cursor.x += tb->d.pixmap->width;
1131 } else if (pos > tb->sections[s].begin) {
1132 tPtr->cursor.x +=
1133 WMWidthOfString(tb->d.font,
1134 &(tb->text[tb->sections[s].begin]),
1135 pos - tb->sections[s].begin);
1138 goto _doneH;
1142 if ( (dir? tb->next : tb->prior)) {
1143 tb = (dir ? tb->next : tb->prior);
1144 } else {
1145 done = True;
1146 break;
1149 if (tb)
1150 s = (dir? 0 : tb->nsections-1);
1153 /* we have said TextBlock, now where within it? */
1154 if (tb && !tb->graphic) {
1155 WMFont *f = tb->d.font;
1156 len = tb->sections[s].end - tb->sections[s].begin;
1157 text = &(tb->text[tb->sections[s].begin]);
1159 _w = x - tb->sections[s].x;
1160 pos = 0;
1162 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1163 pos++;
1165 tPtr->cursor.x = tb->sections[s].x +
1166 (pos? WMWidthOfString(f, text, pos) : 0);
1168 pos += tb->sections[s].begin;
1169 _doneH:
1170 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1173 _doNothing:
1174 if (!tb)
1175 printf("...for this app will surely crash :-)\n");
1177 tPtr->currentTextBlock = tb;
1178 tPtr->cursor.h = tb->sections[s].h;
1179 tPtr->cursor.y = tb->sections[s]._y;
1181 /* scroll the bars if the cursor is not visible */
1182 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1183 if(tPtr->cursor.y+tPtr->cursor.h
1184 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1185 tPtr->vpos +=
1186 (tPtr->cursor.y+tPtr->cursor.h+10
1187 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1188 updateScrollers(tPtr);
1189 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1190 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1191 updateScrollers(tPtr);
1198 static void
1199 autoSelectText(Text *tPtr, int clicks)
1201 int x, start;
1202 TextBlock *tb;
1203 char *mark = NULL, behind, ahead;
1205 if(!(tb = tPtr->currentTextBlock))
1206 return;
1208 if(clicks == 2) {
1211 switch(tb->text[tPtr->tpos]) {
1212 case ' ': return;
1214 case '<': case '>': behind = '<'; ahead = '>'; break;
1215 case '{': case '}': behind = '{'; ahead = '}'; break;
1216 case '[': case ']': behind = '['; ahead = ']'; break;
1218 default: behind = ahead = ' ';
1221 tPtr->sel.y = tPtr->cursor.y+5;
1222 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
1224 if(tb->graphic) {
1225 tPtr->sel.x = tb->sections[0].x;
1226 tPtr->sel.w = tb->sections[0].w;
1227 } else {
1228 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1230 start = tPtr->tpos;
1231 while(start > 0 && tb->text[start-1] != behind)
1232 start--;
1234 x = tPtr->cursor.x;
1235 if(tPtr->tpos > start){
1236 x -= WMWidthOfString(font, &tb->text[start],
1237 tPtr->tpos - start);
1239 tPtr->sel.x = (x<0?0:x)+1;
1241 if((mark = strchr(&tb->text[start], ahead))) {
1242 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1243 (int)(mark - &tb->text[start]));
1244 } else if(tb->used > start) {
1245 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1246 tb->used - start);
1250 } else if(clicks == 3) {
1251 TextBlock *cur = tb;
1253 while(tb && !tb->first) {
1254 tb = tb->prior;
1256 tPtr->sel.y = tb->sections[0]._y;
1258 tb = cur;
1259 while(tb->next && !tb->next->first) {
1260 tb = tb->next;
1262 tPtr->sel.h = tb->sections[tb->nsections-1]._y
1263 + 5 - tPtr->sel.y;
1265 tPtr->sel.x = 0;
1266 tPtr->sel.w = tPtr->docWidth;
1267 tPtr->clicked.x = 0; /* only for now, fix sel. code */
1270 tPtr->flags.ownsSelection = True;
1271 paintText(tPtr);
1276 static void
1277 updateScrollers(Text *tPtr)
1280 if (tPtr->flags.frozen)
1281 return;
1283 if (tPtr->vS) {
1284 if (tPtr->docHeight < tPtr->visible.h) {
1285 WMSetScrollerParameters(tPtr->vS, 0, 1);
1286 tPtr->vpos = 0;
1287 } else {
1288 float hmax = (float)(tPtr->docHeight);
1289 WMSetScrollerParameters(tPtr->vS,
1290 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1291 (float)tPtr->visible.h/hmax);
1293 } else tPtr->vpos = 0;
1295 if (tPtr->hS) {
1296 if (tPtr->docWidth < tPtr->visible.w) {
1297 WMSetScrollerParameters(tPtr->hS, 0, 1);
1298 tPtr->hpos = 0;
1299 } else {
1300 float wmax = (float)(tPtr->docWidth);
1301 WMSetScrollerParameters(tPtr->hS,
1302 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1303 (float)tPtr->visible.w/wmax);
1305 } else tPtr->hpos = 0;
1308 static void
1309 scrollersCallBack(WMWidget *w, void *self)
1311 Text *tPtr = (Text *)self;
1312 Bool scroll = False;
1313 int which;
1315 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1316 return;
1318 if (w == tPtr->vS) {
1319 int height;
1320 height = tPtr->visible.h;
1322 which = WMGetScrollerHitPart(tPtr->vS);
1323 switch(which) {
1325 case WSDecrementLine:
1326 if (tPtr->vpos > 0) {
1327 if (tPtr->vpos>16) tPtr->vpos-=16;
1328 else tPtr->vpos=0;
1329 scroll=True;
1331 break;
1333 case WSIncrementLine: {
1334 int limit = tPtr->docHeight - height;
1335 if (tPtr->vpos < limit) {
1336 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1337 else tPtr->vpos=limit;
1338 scroll = True;
1341 break;
1343 case WSDecrementPage:
1344 if(((int)tPtr->vpos - (int)height) >= 0)
1345 tPtr->vpos -= height;
1346 else
1347 tPtr->vpos = 0;
1349 scroll = True;
1350 break;
1352 case WSIncrementPage:
1353 tPtr->vpos += height;
1354 if (tPtr->vpos > (tPtr->docHeight - height))
1355 tPtr->vpos = tPtr->docHeight - height;
1356 scroll = True;
1357 break;
1360 case WSKnob:
1361 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1362 * (float)(tPtr->docHeight - height);
1363 scroll = True;
1364 break;
1366 case WSKnobSlot:
1367 case WSNoPart:
1368 break;
1370 scroll = (tPtr->vpos != tPtr->prevVpos);
1371 tPtr->prevVpos = tPtr->vpos;
1375 if (w == tPtr->hS) {
1376 int width = tPtr->visible.w;
1378 which = WMGetScrollerHitPart(tPtr->hS);
1379 switch(which) {
1381 case WSDecrementLine:
1382 if (tPtr->hpos > 0) {
1383 if (tPtr->hpos>16) tPtr->hpos-=16;
1384 else tPtr->hpos=0;
1385 scroll=True;
1386 }break;
1388 case WSIncrementLine: {
1389 int limit = tPtr->docWidth - width;
1390 if (tPtr->hpos < limit) {
1391 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1392 else tPtr->hpos=limit;
1393 scroll = True;
1394 }}break;
1396 case WSDecrementPage:
1397 if(((int)tPtr->hpos - (int)width) >= 0)
1398 tPtr->hpos -= width;
1399 else
1400 tPtr->hpos = 0;
1402 scroll = True;
1403 break;
1405 case WSIncrementPage:
1406 tPtr->hpos += width;
1407 if (tPtr->hpos > (tPtr->docWidth - width))
1408 tPtr->hpos = tPtr->docWidth - width;
1409 scroll = True;
1410 break;
1413 case WSKnob:
1414 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1415 * (float)(tPtr->docWidth - width);
1416 scroll = True;
1417 break;
1419 case WSKnobSlot:
1420 case WSNoPart:
1421 break;
1423 scroll = (tPtr->hpos != tPtr->prevHpos);
1424 tPtr->prevHpos = tPtr->hpos;
1427 if (scroll) {
1428 updateScrollers(tPtr);
1429 paintText(tPtr);
1435 typedef struct {
1436 TextBlock *tb;
1437 unsigned short begin, end; /* what part of the text block */
1438 } myLineItems;
1441 static int
1442 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1444 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1445 WMFont *font;
1446 char *text;
1447 TextBlock *tb, *tbsame=NULL;
1449 if(!items || nitems == 0)
1450 return 0;
1452 for(i=0; i<nitems; i++) {
1453 tb = items[i].tb;
1455 if (tb->graphic) {
1456 if (!tPtr->flags.monoFont) {
1457 if(tb->object) {
1458 WMWidget *wdt = tb->d.widget;
1459 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1460 if (tPtr->flags.alignment != WALeft)
1461 lw += WMWidgetWidth(wdt);
1462 } else {
1463 line_height = WMAX(line_height,
1464 tb->d.pixmap->height + max_d);
1465 if (tPtr->flags.alignment != WALeft)
1466 lw += tb->d.pixmap->width;
1470 } else {
1471 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1472 max_d = WMAX(max_d, abs(font->height-font->y));
1473 line_height = WMAX(line_height, font->height + max_d);
1474 text = &(tb->text[items[i].begin]);
1475 len = items[i].end - items[i].begin;
1476 if (tPtr->flags.alignment != WALeft)
1477 lw += WMWidthOfString(font, text, len);
1481 if (tPtr->flags.alignment == WARight) {
1482 j = tPtr->visible.w - lw;
1483 } else if (tPtr->flags.alignment == WACenter) {
1484 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1487 for(i=0; i<nitems; i++) {
1488 tb = items[i].tb;
1490 if (tbsame == tb) { /* extend it, since it's on same line */
1491 tb->sections[tb->nsections-1].end = items[i].end;
1492 n = tb->nsections-1;
1493 } else {
1494 tb->sections = wrealloc(tb->sections,
1495 (++tb->nsections)*sizeof(Section));
1496 n = tb->nsections-1;
1497 tb->sections[n]._y = y + max_d;
1498 tb->sections[n].max_d = max_d;
1499 tb->sections[n].x = x+j;
1500 tb->sections[n].h = line_height;
1501 tb->sections[n].begin = items[i].begin;
1502 tb->sections[n].end = items[i].end;
1505 tb->sections[n].last = (i+1 == nitems);
1507 if (tb->graphic) {
1508 if (!tPtr->flags.monoFont) {
1509 if(tb->object) {
1510 WMWidget *wdt = tb->d.widget;
1511 tb->sections[n].y = max_d + y
1512 + line_height - WMWidgetHeight(wdt);
1513 tb->sections[n].w = WMWidgetWidth(wdt);
1514 } else {
1515 tb->sections[n].y = y + line_height
1516 + max_d - tb->d.pixmap->height;
1517 tb->sections[n].w = tb->d.pixmap->width;
1519 x += tb->sections[n].w;
1521 } else {
1522 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1523 len = items[i].end - items[i].begin;
1524 text = &(tb->text[items[i].begin]);
1526 tb->sections[n].y = y+line_height-font->y;
1527 tb->sections[n].w =
1528 WMWidthOfString(font,
1529 &(tb->text[tb->sections[n].begin]),
1530 tb->sections[n].end - tb->sections[n].begin);
1532 x += WMWidthOfString(font, text, len);
1535 tbsame = tb;
1538 return line_height;
1543 static void
1544 layOutDocument(Text *tPtr)
1546 TextBlock *tb;
1547 myLineItems *items = NULL;
1548 unsigned int itemsSize=0, nitems=0, begin, end;
1549 WMFont *font;
1550 unsigned int x, y=0, lw = 0, width=0, bmargin;
1551 char *start=NULL, *mark=NULL;
1553 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1554 return;
1556 tPtr->docWidth = tPtr->visible.w;
1557 x = tPtr->margins[tb->marginN].first;
1558 bmargin = tPtr->margins[tb->marginN].body;
1560 /* only partial layOut needed: re-Lay only affected textblocks */
1561 if (tPtr->flags.laidOut) {
1562 tb = tPtr->currentTextBlock;
1564 /* search backwards for textblocks on same line */
1565 while (tb->prior) {
1566 if (!tb->sections || tb->nsections<1) {
1567 tb = tPtr->firstTextBlock;
1568 tPtr->flags.laidOut = False;
1569 y = 0;
1570 goto _layOut;
1573 if(!tb->prior->sections || tb->prior->nsections<1) {
1574 tb = tPtr->firstTextBlock;
1575 tPtr->flags.laidOut = False;
1576 y = 0;
1577 goto _layOut;
1580 if (tb->sections[0]._y !=
1581 tb->prior->sections[tb->prior->nsections-1]._y) {
1582 break;
1584 tb = tb->prior;
1587 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1588 y = tb->prior->sections[tb->prior->nsections-1]._y +
1589 tb->prior->sections[tb->prior->nsections-1].h -
1590 tb->prior->sections[tb->prior->nsections-1].max_d;
1591 } else {
1592 y = 0;
1596 _layOut:
1597 while (tb) {
1599 if (tb->sections && tb->nsections>0) {
1600 wfree(tb->sections);
1601 tb->sections = NULL;
1602 tb->nsections = 0;
1605 if (tb->blank && tb->next && !tb->next->first) {
1606 TextBlock *next = tb->next;
1607 tPtr->currentTextBlock = tb;
1608 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1609 tb = next;
1610 tb->first = True;
1611 continue;
1614 if (tb->first && tb != tPtr->firstTextBlock) {
1615 y += layOutLine(tPtr, items, nitems, x, y);
1616 x = tPtr->margins[tb->marginN].first;
1617 bmargin = tPtr->margins[tb->marginN].body;
1618 nitems = 0;
1619 lw = 0;
1622 if (tb->graphic) {
1623 if (!tPtr->flags.monoFont) {
1624 if(tb->object)
1625 width = WMWidgetWidth(tb->d.widget);
1626 else
1627 width = tb->d.pixmap->width;
1629 if (width > tPtr->docWidth)
1630 tPtr->docWidth = width;
1632 lw += width;
1633 if (lw >= tPtr->visible.w - x ) {
1634 y += layOutLine(tPtr, items, nitems, x, y);
1635 nitems = 0;
1636 x = bmargin;
1637 lw = width;
1640 if(nitems + 1> itemsSize) {
1641 items = wrealloc(items,
1642 (++itemsSize)*sizeof(myLineItems));
1645 items[nitems].tb = tb;
1646 items[nitems].begin = 0;
1647 items[nitems].end = 0;
1648 nitems++;
1651 } else if ((start = tb->text)) {
1652 begin = end = 0;
1653 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1655 while (start) {
1656 mark = strchr(start, ' ');
1657 if (mark) {
1658 end += (int)(mark-start)+1;
1659 start = mark+1;
1660 } else {
1661 end += strlen(start);
1662 start = mark;
1665 if (end > tb->used)
1666 end = tb->used;
1668 if (end-begin > 0) {
1670 width = WMWidthOfString(font,
1671 &tb->text[begin], end-begin);
1673 /* if it won't fit, break it up */
1674 if (width > tPtr->visible.w) {
1675 char *t = &tb->text[begin];
1676 int l=end-begin, i=0;
1677 do {
1678 width = WMWidthOfString(font, t, ++i);
1679 } while (width < tPtr->visible.w && i < l);
1680 end = begin+i;
1681 if (start)
1682 start -= l-i;
1685 lw += width;
1688 if (lw >= tPtr->visible.w - x) {
1689 y += layOutLine(tPtr, items, nitems, x, y);
1690 lw = width;
1691 x = bmargin;
1692 nitems = 0;
1695 if(nitems + 1 > itemsSize) {
1696 items = wrealloc(items,
1697 (++itemsSize)*sizeof(myLineItems));
1700 items[nitems].tb = tb;
1701 items[nitems].begin = begin;
1702 items[nitems].end = end;
1703 nitems++;
1705 begin = end;
1710 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1711 if(0&&tPtr->flags.laidOut
1712 && tb->next && tb->next->sections && tb->next->nsections>0
1713 && (tPtr->vpos + tPtr->visible.h
1714 < tb->next->sections[0]._y)) {
1715 if(tPtr->lastTextBlock->sections
1716 && tPtr->lastTextBlock->nsections > 0 ) {
1717 TextBlock *ltb = tPtr->lastTextBlock;
1718 int ly = ltb->sections[ltb->nsections-1]._y;
1719 int lh = ltb->sections[ltb->nsections-1].h;
1720 int ss, sd;
1722 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1723 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1725 y += layOutLine(tPtr, items, nitems, x, y);
1726 ss= ly+lh-y;
1727 sd = tPtr->docHeight-y;
1729 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1730 y += tb->next->sections[0]._y-y;
1731 nitems = 0;
1732 printf("nitems%d\n", nitems);
1733 if(ss-sd!=0)
1734 y = tPtr->docHeight+ss-sd;
1736 break;
1737 } else {
1738 tPtr->flags.laidOut = False;
1742 tb = tb->next;
1746 if (nitems > 0)
1747 y += layOutLine(tPtr, items, nitems, x, y);
1749 if (tPtr->docHeight != y+10) {
1750 tPtr->docHeight = y+10;
1751 updateScrollers(tPtr);
1754 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1755 XEvent event;
1757 tPtr->flags.horizOnDemand = True;
1758 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1759 event.type = Expose;
1760 handleEvents(&event, (void *)tPtr);
1762 } else if(tPtr->docWidth <= tPtr->visible.w
1763 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1764 tPtr->flags.horizOnDemand = False;
1765 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1768 tPtr->flags.laidOut = True;
1770 if(items && itemsSize > 0)
1771 wfree(items);
1775 static void
1776 textDidResize(W_ViewDelegate *self, WMView *view)
1778 Text *tPtr = (Text *)view->self;
1779 unsigned short w = tPtr->view->size.width;
1780 unsigned short h = tPtr->view->size.height;
1781 unsigned short rh = 0, vw = 0, rel;
1783 rel = (tPtr->flags.relief == WRFlat);
1785 if (tPtr->ruler && tPtr->flags.rulerShown) {
1786 WMMoveWidget(tPtr->ruler, 2, 2);
1787 WMResizeWidget(tPtr->ruler, w - 4, 40);
1788 rh = 40;
1791 if (tPtr->vS) {
1792 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1793 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1794 vw = 20;
1795 WMSetRulerOffset(tPtr->ruler,22);
1796 } else WMSetRulerOffset(tPtr->ruler, 2);
1798 if (tPtr->hS) {
1799 if (tPtr->vS) {
1800 WMMoveWidget(tPtr->hS, vw, h - 21);
1801 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1802 } else {
1803 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1804 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1808 tPtr->visible.x = (tPtr->vS)?24:4;
1809 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1810 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1811 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1812 tPtr->visible.h -= (tPtr->hS)?20:0;
1813 tPtr->margins[0].right = tPtr->visible.w;
1815 if (tPtr->view->flags.realized) {
1817 if (tPtr->db) {
1818 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1819 tPtr->db = (Pixmap) NULL;
1822 if (tPtr->visible.w < 40)
1823 tPtr->visible.w = 40;
1824 if (tPtr->visible.h < 20)
1825 tPtr->visible.h = 20;
1827 if(!tPtr->db) {
1828 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1829 tPtr->view->window, tPtr->visible.w,
1830 tPtr->visible.h, tPtr->view->screen->depth);
1834 WMThawText(tPtr);
1837 W_ViewDelegate _TextViewDelegate =
1839 NULL,
1840 NULL,
1841 textDidResize,
1842 NULL,
1845 /* nice, divisble-by-16 blocks */
1846 static inline unsigned short
1847 reqBlockSize(unsigned short requested)
1849 return requested + 16 - (requested%16);
1853 static void
1854 clearText(Text *tPtr)
1856 tPtr->vpos = tPtr->hpos = 0;
1857 tPtr->docHeight = tPtr->docWidth = 0;
1859 if (!tPtr->firstTextBlock)
1860 return;
1862 while (tPtr->currentTextBlock)
1863 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1865 tPtr->firstTextBlock = NULL;
1866 tPtr->currentTextBlock = NULL;
1867 tPtr->lastTextBlock = NULL;
1868 WMEmptyArray(tPtr->gfxItems);
1871 static void
1872 deleteTextInteractively(Text *tPtr, KeySym ksym)
1874 TextBlock *tb;
1875 Bool back = (Bool) (ksym == XK_BackSpace);
1876 Bool done = 1;
1877 Bool wasFirst = 0;
1879 if (!tPtr->flags.editable) {
1880 XBell(tPtr->view->screen->display, 0);
1881 return;
1884 if ( !(tb = tPtr->currentTextBlock) )
1885 return;
1887 if (tPtr->flags.ownsSelection) {
1888 if(removeSelection(tPtr))
1889 layOutDocument(tPtr);
1890 return;
1893 wasFirst = tb->first;
1894 if (back && tPtr->tpos < 1) {
1895 if (tb->prior) {
1896 if(tb->prior->blank) {
1897 tPtr->currentTextBlock = tb->prior;
1898 WMRemoveTextBlock(tPtr);
1899 tPtr->currentTextBlock = tb;
1900 tb->first = True;
1901 layOutDocument(tPtr);
1902 return;
1903 } else {
1904 if(tb->blank) {
1905 TextBlock *prior = tb->prior;
1906 tPtr->currentTextBlock = tb;
1907 WMRemoveTextBlock(tPtr);
1908 tb = prior;
1909 } else {
1910 tb = tb->prior;
1913 tPtr->tpos = tb->used;
1914 tPtr->currentTextBlock = tb;
1915 done = 1;
1916 if(wasFirst) {
1917 if(tb->next)
1918 tb->next->first = False;
1919 layOutDocument(tPtr);
1920 return;
1926 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1927 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1928 if (back)
1929 tPtr->tpos--;
1930 memmove(&(tb->text[tPtr->tpos]),
1931 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1932 tb->used--;
1933 done = 0;
1936 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1937 || tb->graphic) {
1939 TextBlock *sibling = (back? tb->prior : tb->next);
1941 if(tb->used == 0 || tb->graphic)
1942 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1944 if (sibling) {
1945 tPtr->currentTextBlock = sibling;
1946 tPtr->tpos = (back? sibling->used : 0);
1950 layOutDocument(tPtr);
1954 static void
1955 insertTextInteractively(Text *tPtr, char *text, int len)
1957 TextBlock *tb;
1958 char *newline = NULL;
1960 if (!tPtr->flags.editable) {
1961 XBell(tPtr->view->screen->display, 0);
1962 return;
1965 if (len < 1 || !text)
1966 return;
1969 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1970 return;
1973 if (tPtr->flags.ownsSelection)
1974 removeSelection(tPtr);
1977 if (tPtr->flags.ignoreNewLine) {
1978 int i;
1979 for(i=0; i<len; i++) {
1980 if (text[i] == '\n')
1981 text[i] = ' ';
1985 tb = tPtr->currentTextBlock;
1986 if (!tb || tb->graphic) {
1987 tPtr->tpos = 0;
1988 WMAppendTextStream(tPtr, text);
1989 layOutDocument(tPtr);
1990 return;
1993 if ((newline = strchr(text, '\n'))) {
1994 int nlen = (int)(newline-text);
1995 int s = tb->used - tPtr->tpos;
1996 char save[s];
1997 if (!tb->blank && nlen>0) {
1998 if (s > 0) {
1999 memcpy(save, &tb->text[tPtr->tpos], s);
2000 tb->used -= (tb->used - tPtr->tpos);
2002 insertTextInteractively(tPtr, text, nlen);
2003 newline++;
2004 WMAppendTextStream(tPtr, newline);
2005 if (s>0)
2006 insertTextInteractively(tPtr, save, s);
2008 } else {
2009 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2010 && !tb->graphic && tb->text) {
2012 void *ntb = WMCreateTextBlockWithText(
2013 tPtr, &tb->text[tPtr->tpos],
2014 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2015 tb->used = tPtr->tpos;
2016 WMAppendTextBlock(tPtr, ntb);
2017 tPtr->tpos = 0;
2019 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2020 if(tPtr->flags.indentNewLine) {
2021 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2022 " ", tb->d.font, tb->color, True, 4));
2023 tPtr->tpos = 4;
2024 } else {
2025 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2026 NULL, tb->d.font, tb->color, True, 0));
2027 tPtr->tpos = 0;
2032 } else {
2033 if (tb->used + len >= tb->allocated) {
2034 tb->allocated = reqBlockSize(tb->used+len);
2035 tb->text = wrealloc(tb->text, tb->allocated);
2038 if (tb->blank) {
2039 memcpy(tb->text, text, len);
2040 tb->used = len;
2041 tPtr->tpos = len;
2042 tb->text[tb->used] = 0;
2043 tb->blank = False;
2045 } else {
2046 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2047 tb->used-tPtr->tpos+1);
2048 memmove(&tb->text[tPtr->tpos], text, len);
2049 tb->used += len;
2050 tPtr->tpos += len;
2051 tb->text[tb->used] = 0;
2056 layOutDocument(tPtr);
2060 static void
2061 selectRegion(Text *tPtr, int x, int y)
2064 if (x < 0 || y < 0)
2065 return;
2067 y += (tPtr->flags.rulerShown? 40: 0);
2068 y += tPtr->vpos;
2069 if (y>10)
2070 y -= 10; /* the original offset */
2072 x -= tPtr->visible.x-2;
2073 if (x<0)
2074 x=0;
2076 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2077 tPtr->sel.w = abs(tPtr->clicked.x - x);
2078 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2079 tPtr->sel.h = abs(tPtr->clicked.y - y);
2081 tPtr->flags.ownsSelection = True;
2082 paintText(tPtr);
2086 static void
2087 releaseSelection(Text *tPtr)
2089 TextBlock *tb = tPtr->firstTextBlock;
2091 while(tb) {
2092 tb->selected = False;
2093 tb = tb->next;
2095 tPtr->flags.ownsSelection = False;
2096 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2097 CurrentTime);
2099 paintText(tPtr);
2103 WMData*
2104 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2105 Atom *type)
2107 Text *tPtr = view->self;
2108 Display *dpy = tPtr->view->screen->display;
2109 Atom _TARGETS;
2110 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2111 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2112 WMData *data = NULL;
2115 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2116 char *text = WMGetTextSelectedStream(tPtr);
2118 if (text) {
2119 printf("got text [%s]\n", text);
2120 data = WMCreateDataWithBytes(text, strlen(text));
2121 WMSetDataFormat(data, TYPETEXT);
2123 *type = target;
2124 return data;
2125 } else printf("didn't get it\n");
2127 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2128 if (target == _TARGETS) {
2129 Atom *ptr;
2131 ptr = wmalloc(4 * sizeof(Atom));
2132 ptr[0] = _TARGETS;
2133 ptr[1] = XA_STRING;
2134 ptr[2] = TEXT;
2135 ptr[3] = COMPOUND_TEXT;
2137 data = WMCreateDataWithBytes(ptr, 4*4);
2138 WMSetDataFormat(data, 32);
2140 *type = target;
2141 return data;
2144 return NULL;
2147 static void
2148 lostHandler(WMView *view, Atom selection, void *cdata)
2150 releaseSelection((WMText *)view->self);
2153 static WMSelectionProcs selectionHandler = {
2154 requestHandler, lostHandler, NULL
2158 static void
2159 ownershipObserver(void *observerData, WMNotification *notification)
2161 if (observerData != WMGetNotificationClientData(notification))
2162 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2166 static void
2167 fontChanged(void *observerData, WMNotification *notification)
2169 WMText *tPtr = (WMText *) observerData;
2170 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2171 printf("fontChanged\n");
2173 if(!tPtr || !font)
2174 return;
2176 if (tPtr->flags.ownsSelection)
2177 WMSetTextSelectionFont(tPtr, font);
2181 static void
2182 handleTextKeyPress(Text *tPtr, XEvent *event)
2184 char buffer[2];
2185 KeySym ksym;
2186 int control_pressed = False;
2187 TextBlock *tb = NULL;
2189 if (((XKeyEvent *) event)->state & ControlMask)
2190 control_pressed = True;
2191 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2193 switch(ksym) {
2195 case XK_Left:
2196 if(!(tb = tPtr->currentTextBlock))
2197 break;
2198 if(tb->graphic)
2199 goto L_imaGFX;
2201 if(tPtr->tpos==0) {
2202 L_imaGFX: if(tb->prior) {
2203 tPtr->currentTextBlock = tb->prior;
2204 tPtr->tpos = tPtr->currentTextBlock->used -1;
2205 } else tPtr->tpos = 0;
2206 } else tPtr->tpos--;
2207 updateCursorPosition(tPtr);
2208 paintText(tPtr);
2209 break;
2211 case XK_Right:
2212 if(!(tb = tPtr->currentTextBlock))
2213 break;
2214 if(tb->graphic)
2215 goto R_imaGFX;
2216 if(tPtr->tpos == tb->used) {
2217 R_imaGFX: if(tb->next) {
2218 tPtr->currentTextBlock = tb->next;
2219 tPtr->tpos = 1;
2220 } else tPtr->tpos = tb->used;
2221 } else tPtr->tpos++;
2222 updateCursorPosition(tPtr);
2223 paintText(tPtr);
2224 break;
2226 case XK_Down:
2227 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2228 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2229 paintText(tPtr);
2230 break;
2232 case XK_Up:
2233 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2234 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2235 paintText(tPtr);
2236 break;
2238 case XK_BackSpace:
2239 case XK_Delete:
2240 case XK_KP_Delete:
2241 deleteTextInteractively(tPtr, ksym);
2242 updateCursorPosition(tPtr);
2243 paintText(tPtr);
2244 break;
2246 case XK_Control_R :
2247 case XK_Control_L :
2248 control_pressed = True;
2249 break;
2251 case XK_Tab:
2252 insertTextInteractively(tPtr, " ", 4);
2253 updateCursorPosition(tPtr);
2254 paintText(tPtr);
2255 break;
2257 case XK_Return:
2258 buffer[0] = '\n';
2259 default:
2260 if (buffer[0] != 0 && !control_pressed) {
2261 insertTextInteractively(tPtr, buffer, 1);
2262 updateCursorPosition(tPtr);
2263 paintText(tPtr);
2265 } else if (control_pressed && ksym==XK_r) {
2266 Bool i = !tPtr->flags.rulerShown;
2267 WMShowTextRuler(tPtr, i);
2268 tPtr->flags.rulerShown = i;
2270 else if (control_pressed && buffer[0] == '\a')
2271 XBell(tPtr->view->screen->display, 0);
2274 if (!control_pressed && tPtr->flags.ownsSelection)
2275 releaseSelection(tPtr);
2278 static void
2279 handleWidgetPress(XEvent *event, void *data)
2281 TextBlock *tb = (TextBlock *)data;
2282 Text *tPtr;
2283 WMWidget *w;
2285 if (!tb)
2286 return;
2288 tPtr = (Text*)w;
2289 tPtr->currentTextBlock = tb;
2290 tPtr->flags.isOverGraphic = 2;
2291 tPtr->tpos = 0;
2292 output(tb->text, tb->used);
2293 #if 0
2294 if (!tPtr->flags.focused) {
2295 WMSetFocusToWidget(tPtr);
2296 tPtr->flags.focused = True;
2298 #endif
2302 static void
2303 handleActionEvents(XEvent *event, void *data)
2305 Text *tPtr = (Text *)data;
2306 Display *dpy = event->xany.display;
2307 KeySym ksym;
2310 switch (event->type) {
2311 case KeyPress:
2312 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2313 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2314 tPtr->flags.extendSelection = True;
2315 return;
2318 if (tPtr->flags.focused) {
2319 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2320 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2321 GrabModeAsync, GrabModeAsync, None,
2322 tPtr->view->screen->invisibleCursor, CurrentTime);
2323 tPtr->flags.pointerGrabbed = True;
2324 handleTextKeyPress(tPtr, event);
2326 } break;
2328 case KeyRelease:
2329 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2330 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2331 tPtr->flags.extendSelection = False;
2332 return;
2333 /* end modify flag so selection can be extended */
2335 break;
2338 case MotionNotify:
2340 if (tPtr->flags.pointerGrabbed) {
2341 tPtr->flags.pointerGrabbed = False;
2342 XUngrabPointer(dpy, CurrentTime);
2345 if(tPtr->flags.waitingForSelection)
2346 break;
2348 if ((event->xmotion.state & Button1Mask)) {
2349 if (!tPtr->flags.ownsSelection) {
2350 WMCreateSelectionHandler(tPtr->view,
2351 XA_PRIMARY, event->xbutton.time,
2352 &selectionHandler, NULL);
2353 tPtr->flags.ownsSelection = True;
2355 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2356 break;
2359 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2360 break;
2363 case ButtonPress:
2365 if (tPtr->flags.pointerGrabbed) {
2366 tPtr->flags.pointerGrabbed = False;
2367 XUngrabPointer(dpy, CurrentTime);
2368 break;
2371 if (tPtr->flags.waitingForSelection)
2372 break;
2374 if (tPtr->flags.extendSelection) {
2375 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2376 return;
2380 if (event->xbutton.button == Button1) {
2382 if(WMIsDoubleClick(event)) {
2383 TextBlock *tb = tPtr->currentTextBlock;
2385 if(tb && tb->graphic && !tb->object) {
2386 char desc[tb->used+1];
2387 memcpy(desc, tb->text, tb->used);
2388 desc[tb->used] = 0;
2389 if(tPtr->delegate) {
2390 if(tPtr->delegate->didDoubleClickOnPicture)
2391 (*tPtr->delegate->didDoubleClickOnPicture)
2392 (tPtr->delegate, desc);
2394 } else {
2395 autoSelectText(tPtr, 2);
2397 tPtr->lastClickTime = event->xbutton.time;
2398 break;
2399 } else if(event->xbutton.time - tPtr->lastClickTime
2400 < WINGsConfiguration.doubleClickDelay) {
2401 autoSelectText(tPtr, 3);
2402 break;
2405 if (!tPtr->flags.focused) {
2406 WMSetFocusToWidget(tPtr);
2407 tPtr->flags.focused = True;
2410 if (tPtr->flags.ownsSelection)
2411 releaseSelection(tPtr);
2413 tPtr->lastClickTime = event->xbutton.time;
2414 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2415 paintText(tPtr);
2418 if (event->xbutton.button
2419 == WINGsConfiguration.mouseWheelDown) {
2420 WMScrollText(tPtr, 16);
2421 break;
2424 if (event->xbutton.button
2425 == WINGsConfiguration.mouseWheelUp) {
2426 WMScrollText(tPtr, -16);
2427 break;
2430 if (event->xbutton.button == Button2) {
2431 char *text = NULL;
2432 int n;
2434 if (!tPtr->flags.editable) {
2435 XBell(dpy, 0);
2436 break;
2439 #if 0
2440 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2441 event->xbutton.time, pasteText, NULL)) {
2442 #endif
2445 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2447 if (text) {
2448 text[n] = 0;
2450 if (tPtr->parser)
2451 (tPtr->parser) (tPtr, (void *) text);
2452 else
2453 insertTextInteractively(tPtr, text, n);
2455 XFree(text);
2456 #if 0
2457 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2458 (void*)WMInsertTextEvent);
2459 #endif
2461 } else {
2462 tPtr->flags.waitingForSelection = True;
2465 break;
2469 case ButtonRelease:
2471 if (tPtr->flags.pointerGrabbed) {
2472 tPtr->flags.pointerGrabbed = False;
2473 XUngrabPointer(dpy, CurrentTime);
2474 break;
2477 if (tPtr->flags.waitingForSelection)
2478 break;
2484 static void
2485 handleEvents(XEvent *event, void *data)
2487 Text *tPtr = (Text *)data;
2489 switch(event->type) {
2490 case Expose:
2492 if (event->xexpose.count!=0)
2493 break;
2495 if(tPtr->hS) {
2496 if (!(W_VIEW(tPtr->hS))->flags.realized)
2497 WMRealizeWidget(tPtr->hS);
2500 if(tPtr->vS) {
2501 if (!(W_VIEW(tPtr->vS))->flags.realized)
2502 WMRealizeWidget(tPtr->vS);
2505 if(tPtr->ruler) {
2506 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2507 WMRealizeWidget(tPtr->ruler);
2511 if(!tPtr->db)
2512 textDidResize(tPtr->view->delegate, tPtr->view);
2514 paintText(tPtr);
2515 break;
2517 case FocusIn:
2518 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2519 != tPtr->view)
2520 return;
2521 tPtr->flags.focused = True;
2522 #if DO_BLINK
2523 if (tPtr->flags.editable && !tPtr->timerID) {
2524 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2525 blinkCursor, tPtr);
2527 #endif
2529 break;
2531 case FocusOut:
2532 tPtr->flags.focused = False;
2533 paintText(tPtr);
2534 #if DO_BLINK
2535 if (tPtr->timerID) {
2536 WMDeleteTimerHandler(tPtr->timerID);
2537 tPtr->timerID = NULL;
2539 #endif
2540 break;
2543 case DestroyNotify:
2544 clearText(tPtr);
2545 if(tPtr->db)
2546 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2547 if(tPtr->gfxItems)
2548 WMEmptyArray(tPtr->gfxItems);
2549 #if DO_BLINK
2550 if (tPtr->timerID)
2551 WMDeleteTimerHandler(tPtr->timerID);
2552 #endif
2553 WMReleaseFont(tPtr->dFont);
2554 WMReleaseColor(tPtr->dColor);
2555 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2556 WMRemoveNotificationObserver(tPtr);
2558 wfree(tPtr);
2560 break;
2566 static void
2567 insertPlainText(Text *tPtr, char *text)
2569 char *start, *mark;
2570 void *tb = NULL;
2572 start = text;
2573 while (start) {
2574 mark = strchr(start, '\n');
2575 if (mark) {
2576 tb = WMCreateTextBlockWithText(tPtr,
2577 start, tPtr->dFont,
2578 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2579 start = mark+1;
2580 tPtr->flags.first = True;
2581 } else {
2582 if (start && strlen(start)) {
2583 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2584 tPtr->dColor, tPtr->flags.first, strlen(start));
2585 } else tb = NULL;
2586 tPtr->flags.first = False;
2587 start = mark;
2590 if (tPtr->flags.prepend)
2591 WMPrependTextBlock(tPtr, tb);
2592 else
2593 WMAppendTextBlock(tPtr, tb);
2595 return;
2600 static void
2601 rulerMoveCallBack(WMWidget *w, void *self)
2603 Text *tPtr = (Text *)self;
2604 if (!tPtr)
2605 return;
2606 if (W_CLASS(tPtr) != WC_Text)
2607 return;
2609 paintText(tPtr);
2613 static void
2614 rulerReleaseCallBack(WMWidget *w, void *self)
2616 Text *tPtr = (Text *)self;
2617 if (!tPtr)
2618 return;
2619 if (W_CLASS(tPtr) != WC_Text)
2620 return;
2622 WMThawText(tPtr);
2623 return;
2627 static unsigned
2628 draggingEntered(WMView *self, WMDraggingInfo *info)
2630 printf("draggingEntered\n");
2631 return WDOperationCopy;
2635 static unsigned
2636 draggingUpdated(WMView *self, WMDraggingInfo *info)
2638 return WDOperationCopy;
2642 static void
2643 draggingExited(WMView *self, WMDraggingInfo *info)
2645 printf("draggingExited\n");
2648 static Bool
2649 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2651 printf("prepareForDragOperation\n");
2652 return True;
2656 char *badbadbad;
2658 static void
2659 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2660 void *cdata, WMData *data)
2662 badbadbad = wstrdup((char *)WMDataBytes(data));
2666 /* when it's done in WINGs, remove this */
2668 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2670 WMScreen *scr = W_VIEW_SCREEN(view);
2672 if (!WMRequestSelection(scr->dragInfo.destView,
2673 scr->xdndSelectionAtom,
2674 XInternAtom(scr->display, type, False),
2675 scr->dragInfo.timestamp,
2676 receivedData, &scr->dragInfo)) {
2677 wwarning("could not request data for dropped data");
2682 XEvent ev;
2684 ev.type = ClientMessage;
2685 ev.xclient.message_type = scr->xdndFinishedAtom;
2686 ev.xclient.format = 32;
2687 ev.xclient.window = info->destinationWindow;
2688 ev.xclient.data.l[0] = 0;
2689 ev.xclient.data.l[1] = 0;
2690 ev.xclient.data.l[2] = 0;
2691 ev.xclient.data.l[3] = 0;
2692 ev.xclient.data.l[4] = 0;
2694 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2695 XFlush(scr->display);
2697 return True;
2700 static Bool
2701 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2703 WMColor *color;
2704 WMText *tPtr = (WMText *)self->self;
2706 if (!tPtr)
2707 return True;
2709 requestDroppedData(tPtr->view, info, "application/X-color");
2710 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2711 if(color) {
2712 WMSetTextSelectionColor(tPtr, color);
2713 WMReleaseColor(color);
2718 return True;
2721 static void
2722 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2724 printf("concludeDragOperation\n");
2728 static WMDragDestinationProcs _DragDestinationProcs = {
2729 draggingEntered,
2730 draggingUpdated,
2731 draggingExited,
2732 prepareForDragOperation,
2733 performDragOperation,
2734 concludeDragOperation
2738 char *
2739 getStream(WMText *tPtr, int sel, int array)
2741 TextBlock *tb = NULL;
2742 char *text = NULL;
2743 unsigned long where = 0;
2745 if (!tPtr)
2746 return NULL;
2748 if (!(tb = tPtr->firstTextBlock))
2749 return NULL;
2751 if (tPtr->writer) {
2752 (tPtr->writer) (tPtr, (void *) text);
2753 return text;
2756 tb = tPtr->firstTextBlock;
2757 while (tb) {
2759 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2761 if (!sel || (tb->graphic && tb->selected)) {
2763 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2764 && tb != tPtr->firstTextBlock) {
2765 text = wrealloc(text, where+1);
2766 text[where++] = '\n';
2769 if(tb->blank)
2770 goto _gSnext;
2772 if(tb->graphic && array) {
2773 text = wrealloc(text, where+4);
2774 text[where++] = 0xFA;
2775 text[where++] = (tb->used>>8)&0x0ff;
2776 text[where++] = tb->used&0x0ff;
2777 text[where++] = tb->allocated; /* extra info */
2779 text = wrealloc(text, where+tb->used);
2780 memcpy(&text[where], tb->text, tb->used);
2781 where += tb->used;
2784 } else if (sel && tb->selected) {
2786 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2787 && tb != tPtr->firstTextBlock) {
2788 text = wrealloc(text, where+1);
2789 text[where++] = '\n';
2792 if(tb->blank)
2793 goto _gSnext;
2795 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2796 memcpy(&text[where], &tb->text[tb->s_begin],
2797 tb->s_end - tb->s_begin);
2798 where += tb->s_end - tb->s_begin;
2803 _gSnext:tb = tb->next;
2806 /* +1 for the end of string, let's be nice */
2807 text = wrealloc(text, where+1);
2808 text[where] = 0;
2809 return text;
2813 static void
2814 releaseStreamObjects(void *data)
2816 if(data)
2817 wfree(data);
2820 WMArray *
2821 getStreamObjects(WMText *tPtr, int sel)
2823 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2824 WMData *data;
2825 char *stream;
2826 unsigned short len;
2827 char *start, *fa, *desc;
2829 stream = getStream(tPtr, sel, 1);
2830 if(!stream)
2831 return NULL;
2833 start = stream;
2834 while (start) {
2836 fa = strchr(start, 0xFA);
2837 if (fa) {
2838 if((int)(fa - start)>0) {
2839 desc = start;
2840 desc[(int)(fa - start)] = 0;
2841 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2842 WMSetDataFormat(data, TYPETEXT);
2843 WMAddToArray(array, (void *) data);
2846 len = *(fa+1)*0xff + *(fa+2);
2847 data = WMCreateDataWithBytes((void *)(fa+4), len);
2848 WMSetDataFormat(data, *(fa+3));
2849 WMAddToArray(array, (void *) data);
2850 start = fa + len + 4;
2852 } else {
2853 if (start && strlen(start)) {
2854 data = WMCreateDataWithBytes((void *)start, strlen(start));
2855 WMSetDataFormat(data, TYPETEXT);
2856 WMAddToArray(array, (void *) data);
2858 start = fa;
2862 wfree(stream);
2863 return array;
2864 WMFreeArray(array);
2868 WMText *
2869 WMCreateTextForDocumentType(WMWidget *parent,
2870 WMAction *parser, WMAction *writer)
2872 Text *tPtr = wmalloc(sizeof(Text));
2873 if (!tPtr) {
2874 printf("could not create text widget\n");
2875 return NULL;
2879 memset(tPtr, 0, sizeof(Text));
2880 tPtr->widgetClass = WC_Text;
2881 tPtr->view = W_CreateView(W_VIEW(parent));
2882 if (!tPtr->view) {
2883 perror("could not create text's view\n");
2884 wfree(tPtr);
2885 return NULL;
2887 tPtr->view->self = tPtr;
2888 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2889 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2890 W_ResizeView(tPtr->view, 250, 200);
2892 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2893 tPtr->bgGC = WMColorGC(tPtr->dColor);
2894 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2895 WMReleaseColor(tPtr->dColor);
2897 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2898 tPtr->fgGC = WMColorGC(tPtr->dColor);
2900 tPtr->ruler = NULL;
2901 tPtr->vS = NULL;
2902 tPtr->hS = NULL;
2904 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2906 tPtr->view->delegate = &_TextViewDelegate;
2908 tPtr->delegate = NULL;
2910 #if DO_BLINK
2911 tPtr->timerID = NULL;
2912 #endif
2914 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2915 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2916 handleEvents, tPtr);
2918 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2919 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2920 handleActionEvents, tPtr);
2922 WMAddNotificationObserver(ownershipObserver, tPtr,
2923 "_lostOwnership", tPtr);
2925 if(0){
2926 char *types[2] = {"application/X-color", NULL};
2927 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2928 WMRegisterViewForDraggedTypes(tPtr->view, types);
2931 WMAddNotificationObserver(fontChanged, tPtr,
2932 "WMFontPanelDidChangeNotification", tPtr);
2934 tPtr->firstTextBlock = NULL;
2935 tPtr->lastTextBlock = NULL;
2936 tPtr->currentTextBlock = NULL;
2937 tPtr->tpos = 0;
2939 tPtr->gfxItems = WMCreateArray(4);
2941 tPtr->parser = parser;
2942 tPtr->writer = writer;
2944 tPtr->sel.x = tPtr->sel.y = 2;
2945 tPtr->sel.w = tPtr->sel.h = 0;
2947 tPtr->clicked.x = tPtr->clicked.y = 2;
2949 tPtr->visible.x = tPtr->visible.y = 2;
2950 tPtr->visible.h = tPtr->view->size.height;
2951 tPtr->visible.w = tPtr->view->size.width - 4;
2953 tPtr->cursor.x = -23;
2955 tPtr->docWidth = 0;
2956 tPtr->docHeight = 0;
2957 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2958 default_bullet);
2959 tPtr->db = (Pixmap) NULL;
2961 tPtr->margins = WMGetRulerMargins(NULL);
2962 tPtr->margins->right = tPtr->visible.w;
2963 tPtr->nMargins = 1;
2965 tPtr->flags.rulerShown = False;
2966 tPtr->flags.monoFont = False;
2967 tPtr->flags.focused = False;
2968 tPtr->flags.editable = True;
2969 tPtr->flags.ownsSelection = False;
2970 tPtr->flags.pointerGrabbed = False;
2971 tPtr->flags.extendSelection = False;
2972 tPtr->flags.frozen = False;
2973 tPtr->flags.cursorShown = True;
2974 tPtr->flags.acceptsGraphic = False;
2975 tPtr->flags.horizOnDemand = False;
2976 tPtr->flags.needsLayOut = False;
2977 tPtr->flags.ignoreNewLine = False;
2978 tPtr->flags.indentNewLine = False;
2979 tPtr->flags.laidOut = False;
2980 tPtr->flags.waitingForSelection = False;
2981 tPtr->flags.prepend = False;
2982 tPtr->flags.isOverGraphic = False;
2983 tPtr->flags.relief = WRSunken;
2984 tPtr->flags.isOverGraphic = 0;
2985 tPtr->flags.alignment = WALeft;
2986 tPtr->flags.first = True;
2988 return tPtr;
2991 void
2992 WMPrependTextStream(WMText *tPtr, char *text)
2994 CHECK_CLASS(tPtr, WC_Text);
2996 if(!text) {
2997 if(tPtr->flags.ownsSelection)
2998 releaseSelection(tPtr);
2999 else {
3000 clearText(tPtr);
3001 updateScrollers(tPtr);
3003 return;
3006 tPtr->flags.prepend = True;
3007 if (text && tPtr->parser)
3008 (tPtr->parser) (tPtr, (void *) text);
3009 else
3010 insertPlainText(tPtr, text);
3012 tPtr->flags.needsLayOut = True;
3013 tPtr->tpos = 0;
3017 void
3018 WMAppendTextStream(WMText *tPtr, char *text)
3020 CHECK_CLASS(tPtr, WC_Text);
3022 if(!text) {
3023 if(tPtr->flags.ownsSelection)
3024 releaseSelection(tPtr);
3025 else {
3026 clearText(tPtr);
3027 updateScrollers(tPtr);
3029 return;
3032 tPtr->flags.prepend = False;
3033 if (text && tPtr->parser)
3034 (tPtr->parser) (tPtr, (void *) text);
3035 else
3036 insertPlainText(tPtr, text);
3038 tPtr->flags.needsLayOut = True;
3039 if(tPtr->currentTextBlock)
3040 tPtr->tpos = tPtr->currentTextBlock->used;
3046 char *
3047 WMGetTextStream(WMText *tPtr)
3049 CHECK_CLASS(tPtr, WC_Text);
3050 return getStream(tPtr, 0, 0);
3053 char *
3054 WMGetTextSelectedStream(WMText *tPtr)
3056 CHECK_CLASS(tPtr, WC_Text);
3057 return getStream(tPtr, 1, 0);
3060 WMArray *
3061 WMGetTextObjects(WMText *tPtr)
3063 CHECK_CLASS(tPtr, WC_Text);
3064 return getStreamObjects(tPtr, 0);
3067 WMArray *
3068 WMGetTextSelectedObjects(WMText *tPtr)
3070 CHECK_CLASS(tPtr, WC_Text);
3071 return getStreamObjects(tPtr, 1);
3075 void
3076 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3078 CHECK_CLASS(tPtr, WC_Text);
3080 tPtr->delegate = delegate;
3084 void *
3085 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3086 char *description, WMColor *color,
3087 unsigned short first, unsigned short extraInfo)
3089 TextBlock *tb;
3091 if (!w || !description || !color)
3092 return NULL;
3094 tb = wmalloc(sizeof(TextBlock));
3095 if (!tb)
3096 return NULL;
3098 tb->text = wstrdup(description);
3099 tb->used = strlen(description);
3100 tb->blank = False;
3101 tb->d.widget = w;
3102 tb->color = WMRetainColor(color);
3103 tb->marginN = newMargin(tPtr, NULL);
3104 tb->allocated = extraInfo;
3105 tb->first = first;
3106 tb->kanji = False;
3107 tb->graphic = True;
3108 tb->object = True;
3109 tb->underlined = False;
3110 tb->selected = False;
3111 tb->script = 0;
3112 tb->sections = NULL;
3113 tb->nsections = 0;
3114 tb->prior = NULL;
3115 tb->next = NULL;
3117 return tb;
3121 void *
3122 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3123 char *description, WMColor *color,
3124 unsigned short first, unsigned short extraInfo)
3126 TextBlock *tb;
3128 if (!p || !description || !color)
3129 return NULL;
3131 tb = wmalloc(sizeof(TextBlock));
3132 if (!tb)
3133 return NULL;
3135 tb->text = wstrdup(description);
3136 tb->used = strlen(description);
3137 tb->blank = False;
3138 tb->d.pixmap = WMRetainPixmap(p);
3139 tb->color = WMRetainColor(color);
3140 tb->marginN = newMargin(tPtr, NULL);
3141 tb->allocated = extraInfo;
3142 tb->first = first;
3143 tb->kanji = False;
3144 tb->graphic = True;
3145 tb->object = False;
3146 tb->underlined = False;
3147 tb->selected = False;
3148 tb->script = 0;
3149 tb->sections = NULL;
3150 tb->nsections = 0;
3151 tb->prior = NULL;
3152 tb->next = NULL;
3154 return tb;
3157 void *
3158 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3159 unsigned short first, unsigned short len)
3161 TextBlock *tb;
3163 if (!font || !color)
3164 return NULL;
3166 tb = wmalloc(sizeof(TextBlock));
3167 if (!tb)
3168 return NULL;
3170 tb->allocated = reqBlockSize(len);
3171 tb->text = (char *)wmalloc(tb->allocated);
3172 memset(tb->text, 0, tb->allocated);
3174 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3175 *tb->text = ' ';
3176 tb->used = 1;
3177 tb->blank = True;
3178 } else {
3179 memcpy(tb->text, text, len);
3180 tb->used = len;
3181 tb->blank = False;
3183 tb->text[tb->used] = 0;
3185 tb->d.font = WMRetainFont(font);
3186 tb->color = WMRetainColor(color);
3187 tb->marginN = newMargin(tPtr, NULL);
3188 tb->first = first;
3189 tb->kanji = False;
3190 tb->graphic = False;
3191 tb->underlined = False;
3192 tb->selected = False;
3193 tb->script = 0;
3194 tb->sections = NULL;
3195 tb->nsections = 0;
3196 tb->prior = NULL;
3197 tb->next = NULL;
3198 return tb;
3201 void
3202 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3203 unsigned int kanji, unsigned int underlined, int script,
3204 WMRulerMargins *margins)
3206 TextBlock *tb = (TextBlock *) vtb;
3207 if (!tb)
3208 return;
3210 tb->first = first;
3211 tb->kanji = kanji;
3212 tb->underlined = underlined;
3213 tb->script = script;
3214 tb->marginN = newMargin(tPtr, margins);
3217 void
3218 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3219 unsigned int *kanji, unsigned int *underlined, int *script,
3220 WMRulerMargins *margins)
3222 TextBlock *tb = (TextBlock *) vtb;
3223 if (!tb)
3224 return;
3226 if (first) *first = tb->first;
3227 if (kanji) *kanji = tb->kanji;
3228 if (underlined) *underlined = tb->underlined;
3229 if (script) *script = tb->script;
3230 if (margins) margins = &tPtr->margins[tb->marginN];
3235 void
3236 WMPrependTextBlock(WMText *tPtr, void *vtb)
3238 TextBlock *tb = (TextBlock *)vtb;
3240 if (!tPtr || !tb)
3241 return;
3243 if (tb->graphic) {
3244 if(tb->object) {
3245 WMWidget *w = tb->d.widget;
3246 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3247 handleWidgetPress, tb);
3248 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3249 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3250 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3253 WMAddToArray(tPtr->gfxItems, (void *)tb);
3254 tPtr->tpos = 0;
3255 } else tPtr->tpos = tb->used;
3257 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3258 tb->next = tb->prior = NULL;
3259 tb->first = True;
3260 tPtr->lastTextBlock = tPtr->firstTextBlock
3261 = tPtr->currentTextBlock = tb;
3262 return;
3265 if(!tb->first) {
3266 tb->marginN = tPtr->currentTextBlock->marginN;
3269 tb->next = tPtr->currentTextBlock;
3270 tb->prior = tPtr->currentTextBlock->prior;
3271 if (tPtr->currentTextBlock->prior)
3272 tPtr->currentTextBlock->prior->next = tb;
3274 tPtr->currentTextBlock->prior = tb;
3275 if (!tb->prior)
3276 tPtr->firstTextBlock = tb;
3278 tPtr->currentTextBlock = tb;
3282 void
3283 WMAppendTextBlock(WMText *tPtr, void *vtb)
3285 TextBlock *tb = (TextBlock *)vtb;
3287 if (!tPtr || !tb)
3288 return;
3290 if (tb->graphic) {
3291 if(tb->object) {
3292 WMWidget *w = tb->d.widget;
3293 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3294 handleWidgetPress, tb);
3295 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3296 (W_VIEW(w))->attribs.cursor =
3297 tPtr->view->screen->defaultCursor;
3298 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3301 WMAddToArray(tPtr->gfxItems, (void *)tb);
3302 tPtr->tpos = 0;
3303 } else tPtr->tpos = tb->used;
3305 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3306 tb->next = tb->prior = NULL;
3307 tb->first = True;
3308 tPtr->lastTextBlock = tPtr->firstTextBlock
3309 = tPtr->currentTextBlock = tb;
3310 return;
3313 if(!tb->first) {
3314 tb->marginN = tPtr->currentTextBlock->marginN;
3317 tb->next = tPtr->currentTextBlock->next;
3318 tb->prior = tPtr->currentTextBlock;
3319 if (tPtr->currentTextBlock->next)
3320 tPtr->currentTextBlock->next->prior = tb;
3322 tPtr->currentTextBlock->next = tb;
3324 if (!tb->next)
3325 tPtr->lastTextBlock = tb;
3327 tPtr->currentTextBlock = tb;
3330 void *
3331 WMRemoveTextBlock(WMText *tPtr)
3333 TextBlock *tb = NULL;
3335 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3336 || !tPtr->currentTextBlock) {
3337 printf("cannot remove non existent TextBlock!\b");
3338 return NULL;
3341 tb = tPtr->currentTextBlock;
3342 if (tb->graphic) {
3343 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3345 if(tb->object) {
3346 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3347 handleWidgetPress, tb);
3348 WMUnmapWidget(tb->d.widget);
3352 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3353 if (tPtr->currentTextBlock->next)
3354 tPtr->currentTextBlock->next->prior = NULL;
3356 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3357 tPtr->currentTextBlock = tPtr->firstTextBlock;
3359 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3360 tPtr->currentTextBlock->prior->next = NULL;
3361 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3362 tPtr->currentTextBlock = tPtr->lastTextBlock;
3363 } else {
3364 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3365 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3366 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3369 return (void *)tb;
3372 void
3373 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3375 TextBlock *tb = (TextBlock *)vtb;
3376 if (!tPtr || !tb)
3377 return;
3379 if (tb->graphic) {
3380 if(tb->object) {
3381 /* naturally, there's a danger to destroying
3382 widgets whose action brings us here:
3383 ie. press a button to destroy it... need to
3384 find a safer way. till then... this stays commented out */
3385 /* WMDestroyWidget(tb->d.widget);
3386 wfree(tb->d.widget); */
3387 tb->d.widget = NULL;
3388 } else {
3389 WMReleasePixmap(tb->d.pixmap);
3390 tb->d.pixmap = NULL;
3392 } else {
3393 WMReleaseFont(tb->d.font);
3396 WMReleaseColor(tb->color);
3397 if (tb->sections && tb->nsections > 0)
3398 wfree(tb->sections);
3399 wfree(tb->text);
3400 wfree(tb);
3401 tb = NULL;
3406 void
3407 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3409 if (!tPtr)
3410 return;
3412 if (color)
3413 tPtr->fgGC = WMColorGC(color);
3414 else
3415 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3417 paintText(tPtr);
3420 void
3421 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3423 if (!tPtr)
3424 return;
3426 if (color) {
3427 tPtr->bgGC = WMColorGC(color);
3428 W_SetViewBackgroundColor(tPtr->view, color);
3429 } else {
3430 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3431 W_SetViewBackgroundColor(tPtr->view,
3432 WMWhiteColor(tPtr->view->screen));
3435 paintText(tPtr);
3438 void
3439 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3441 if (!tPtr)
3442 return;
3443 tPtr->flags.relief = relief;
3444 textDidResize(tPtr->view->delegate, tPtr->view);
3447 void
3448 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3450 if (!tPtr)
3451 return;
3453 if (shouldhave && !tPtr->hS) {
3454 tPtr->hS = WMCreateScroller(tPtr);
3455 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3456 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3457 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3458 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3459 WMMapWidget(tPtr->hS);
3460 } else if (!shouldhave && tPtr->hS) {
3461 WMUnmapWidget(tPtr->hS);
3462 WMDestroyWidget(tPtr->hS);
3463 tPtr->hS = NULL;
3466 tPtr->hpos = 0;
3467 tPtr->prevHpos = 0;
3468 textDidResize(tPtr->view->delegate, tPtr->view);
3472 void
3473 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3475 if (!tPtr)
3476 return;
3478 if(shouldhave && !tPtr->ruler) {
3479 tPtr->ruler = WMCreateRuler(tPtr);
3480 (W_VIEW(tPtr->ruler))->attribs.cursor =
3481 tPtr->view->screen->defaultCursor;
3482 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3483 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3484 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3485 } else if(!shouldhave && tPtr->ruler) {
3486 WMShowTextRuler(tPtr, False);
3487 WMDestroyWidget(tPtr->ruler);
3488 tPtr->ruler = NULL;
3490 textDidResize(tPtr->view->delegate, tPtr->view);
3493 void
3494 WMShowTextRuler(WMText *tPtr, Bool show)
3496 if(!tPtr)
3497 return;
3498 if(!tPtr->ruler)
3499 return;
3501 if(tPtr->flags.monoFont)
3502 show = False;
3504 tPtr->flags.rulerShown = show;
3505 if(show) {
3506 WMMapWidget(tPtr->ruler);
3507 } else {
3508 WMUnmapWidget(tPtr->ruler);
3511 textDidResize(tPtr->view->delegate, tPtr->view);
3514 Bool
3515 WMGetTextRulerShown(WMText *tPtr)
3517 if(!tPtr)
3518 return 0;
3520 if(!tPtr->ruler)
3521 return 0;
3523 return tPtr->flags.rulerShown;
3527 void
3528 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3530 if (!tPtr)
3531 return;
3533 if (shouldhave && !tPtr->vS) {
3534 tPtr->vS = WMCreateScroller(tPtr);
3535 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3536 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3537 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3538 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3539 WMMapWidget(tPtr->vS);
3540 } else if (!shouldhave && tPtr->vS) {
3541 WMUnmapWidget(tPtr->vS);
3542 WMDestroyWidget(tPtr->vS);
3543 tPtr->vS = NULL;
3546 tPtr->vpos = 0;
3547 tPtr->prevVpos = 0;
3548 textDidResize(tPtr->view->delegate, tPtr->view);
3553 Bool
3554 WMScrollText(WMText *tPtr, int amount)
3556 Bool scroll=False;
3557 if (!tPtr)
3558 return False;
3559 if (amount == 0 || !tPtr->view->flags.realized)
3560 return False;
3562 if (amount < 0) {
3563 if (tPtr->vpos > 0) {
3564 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3565 else tPtr->vpos=0;
3566 scroll=True;
3567 } } else {
3568 int limit = tPtr->docHeight - tPtr->visible.h;
3569 if (tPtr->vpos < limit) {
3570 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3571 else tPtr->vpos = limit;
3572 scroll = True;
3573 } }
3575 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3576 updateScrollers(tPtr);
3577 paintText(tPtr);
3579 tPtr->prevVpos = tPtr->vpos;
3580 return scroll;
3583 Bool
3584 WMPageText(WMText *tPtr, Bool direction)
3586 if (!tPtr) return False;
3587 if (!tPtr->view->flags.realized) return False;
3589 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3592 void
3593 WMSetTextEditable(WMText *tPtr, Bool editable)
3595 if (!tPtr)
3596 return;
3597 tPtr->flags.editable = editable;
3600 int
3601 WMGetTextEditable(WMText *tPtr)
3603 if (!tPtr)
3604 return 0;
3605 return tPtr->flags.editable;
3608 void
3609 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3611 if (!tPtr)
3612 return;
3613 tPtr->flags.indentNewLine = indent;
3616 void
3617 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3619 if (!tPtr)
3620 return;
3621 tPtr->flags.ignoreNewLine = ignore;
3624 Bool
3625 WMGetTextIgnoresNewline(WMText *tPtr)
3627 if (!tPtr)
3628 return True;
3629 return tPtr->flags.ignoreNewLine;
3632 void
3633 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3635 if (!tPtr)
3636 return;
3638 if (mono) {
3639 if(tPtr->flags.rulerShown)
3640 WMShowTextRuler(tPtr, False);
3641 if(tPtr->flags.alignment != WALeft)
3642 tPtr->flags.alignment = WALeft;
3645 tPtr->flags.monoFont = mono;
3646 textDidResize(tPtr->view->delegate, tPtr->view);
3649 Bool
3650 WMGetTextUsesMonoFont(WMText *tPtr)
3652 if (!tPtr)
3653 return True;
3654 return tPtr->flags.monoFont;
3658 void
3659 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3661 if (!tPtr)
3662 return;
3664 WMReleaseFont(tPtr->dFont);
3665 if (font)
3666 tPtr->dFont = WMRetainFont(font);
3667 else
3668 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3671 WMFont *
3672 WMGetTextDefaultFont(WMText *tPtr)
3674 if (!tPtr)
3675 return NULL;
3676 else
3677 return WMRetainFont(tPtr->dFont);
3680 void
3681 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3683 if (!tPtr)
3684 return;
3686 WMReleaseColor(tPtr->dColor);
3687 if (color)
3688 tPtr->dColor = WMRetainColor(color);
3689 else
3690 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3693 WMColor *
3694 WMGetTextDefaultColor(WMText *tPtr)
3696 if (!tPtr)
3697 return NULL;
3698 else
3699 return WMRetainColor(tPtr->dColor);
3702 void
3703 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3705 if (!tPtr)
3706 return;
3707 if(tPtr->flags.monoFont)
3708 tPtr->flags.alignment = WALeft;
3709 else
3710 tPtr->flags.alignment = alignment;
3711 WMThawText(tPtr);
3714 int
3715 WMGetTextInsertType(WMText *tPtr)
3717 if (!tPtr)
3718 return 0;
3719 return tPtr->flags.prepend;
3723 void
3724 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3726 if (!tPtr || !color)
3727 return;
3729 setSelectionProperty(tPtr, NULL, color, -1);
3732 WMColor *
3733 WMGetTextSelectionColor(WMText *tPtr)
3735 TextBlock *tb;
3737 if (!tPtr)
3738 return NULL;
3740 tb = tPtr->currentTextBlock;
3742 if (!tb || !tPtr->flags.ownsSelection)
3743 return NULL;
3745 if(!tb->selected)
3746 return NULL;
3748 return tb->color;
3752 void
3753 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3755 if (!tPtr || !font)
3756 return;
3758 setSelectionProperty(tPtr, font, NULL, -1) ;
3761 WMFont *
3762 WMGetTextSelectionFont(WMText *tPtr)
3764 TextBlock *tb;
3766 if (!tPtr)
3767 return NULL;
3769 tb = tPtr->currentTextBlock;
3771 if (!tb || !tPtr->flags.ownsSelection)
3772 return NULL;
3774 if(!tb->selected)
3775 return NULL;
3777 if(tb->graphic) {
3778 tb = getFirstNonGraphicBlockFor(tb, 1);
3779 if(!tb)
3780 return NULL;
3782 return (tb->selected ? tb->d.font : NULL);
3786 void
3787 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3789 if (!tPtr || (underlined!=0 && underlined !=1))
3790 return;
3792 setSelectionProperty(tPtr, NULL, NULL, underlined);
3796 int
3797 WMGetTextSelectionUnderlined(WMText *tPtr)
3799 TextBlock *tb;
3801 if (!tPtr)
3802 return 0;
3804 tb = tPtr->currentTextBlock;
3806 if (!tb || !tPtr->flags.ownsSelection)
3807 return 0;
3809 if(!tb->selected)
3810 return 0;
3812 return tb->underlined;
3816 void
3817 WMFreezeText(WMText *tPtr)
3819 if (!tPtr)
3820 return;
3822 tPtr->flags.frozen = True;
3826 void
3827 WMThawText(WMText *tPtr)
3829 if (!tPtr)
3830 return;
3832 tPtr->flags.frozen = False;
3834 if(tPtr->flags.monoFont) {
3835 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3836 TextBlock *tb;
3838 /* make sure to unmap widgets no matter where they are */
3839 /* they'll be later remapped if needed by paintText */
3840 for(j=0; j<c; j++) {
3841 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3842 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3843 WMUnmapWidget(tb->d.widget);
3849 tPtr->flags.laidOut = False;
3850 layOutDocument(tPtr);
3851 updateScrollers(tPtr);
3852 paintText(tPtr);
3853 tPtr->flags.needsLayOut = False;
3858 static char *
3859 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
3860 Bool caseSensitive)
3862 char *ptr;
3864 if(!haystack || !needle)
3865 return NULL;
3867 for (ptr = haystack-2; ptr > end; ptr--) {
3868 if(caseSensitive) {
3869 if (*ptr == *needle && !strncmp(ptr, needle, len))
3870 return ptr;
3871 } else {
3872 if (tolower(*ptr) == tolower(*needle) &&
3873 strncasecmp(ptr, needle, len))
3874 return ptr;
3878 return NULL;
3882 Bool
3883 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3884 Bool caseSensitive)
3886 TextBlock *tb;
3887 char *mark;
3888 unsigned short pos;
3890 if (!tPtr || !needle)
3891 return False;
3893 if (! (tb = tPtr->currentTextBlock)) {
3894 if (! (tb = ( (direction > 0) ?
3895 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3896 return False;
3898 } else {
3899 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3900 tb = (direction>0) ? tb->next : tb->prior; */
3901 if(tb != tPtr->lastTextBlock)
3902 tb = tb->prior;
3906 while(tb) {
3907 if (!tb->graphic) {
3908 pos = tPtr->tpos;
3909 if(pos+1 < tb->used)
3910 pos++;
3912 if(tb->used - pos> 0 && pos > 0) {
3913 char tmp = tb->text[tb->used];
3914 tb->text[tb->used] = 0;
3916 output(&tb->text[pos], tb->used - pos);
3917 if(direction > 0)
3918 mark = strstr(&tb->text[pos], needle);
3919 else
3920 mark = mystrrstr(&tb->text[pos], needle,
3921 strlen(needle), tb->text, caseSensitive);
3923 tb->text[tb->used] = tmp;
3925 } else {
3926 return False;
3929 if(mark) {
3930 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3932 tPtr->tpos = (int)(mark - tb->text);
3933 tPtr->currentTextBlock = tb;
3934 updateCursorPosition(tPtr);
3935 tPtr->sel.y = tPtr->cursor.y+5;
3936 tPtr->sel.h = tPtr->cursor.h-10;
3937 tPtr->sel.x = tPtr->cursor.x +1;
3938 tPtr->sel.w = WMIN(WMWidthOfString(font,
3939 &tb->text[tPtr->tpos], strlen(needle)),
3940 tPtr->docWidth - tPtr->sel.x);
3941 tPtr->flags.ownsSelection = True;
3942 paintText(tPtr);
3944 return True;
3948 tb = (direction>0) ? tb->next : tb->prior;
3949 pos = 0;
3952 return False;