WMReplaceTextSelection is now implemented
[wmaker-crm.git] / WINGs / wtext.c
blob39e111dce041e8068325b11068f7c398a1a211af
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
5 #include "WINGsP.h"
6 #include <ctype.h>
7 #include <X11/keysym.h>
8 #include <X11/Xatom.h>
10 #define DO_BLINK 0
12 /* TODO:
13 * - FIX wrap... long lines that don't fit are not char wrapped yet.
14 * - hrm... something to do with already having tbs...
15 * - selection code... selects can be funny if it crosses over. use rect?
16 * - also inspect behaviour for WACenter and WARight
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - assess danger of destroying widgets whose actions link to other pages
20 * - Tabs now are simply replaced by 4 spaces...
21 * - redo blink code to reduce paint event... use pixmap buffer...
22 * - add paragraph support (full) and '\n' code in getStream..
26 /* a Section is a section of a TextBlock that describes what parts
27 of a TextBlock has been laid out on which "line"...
28 o this greatly aids redraw, scroll and selection.
29 o this is created during layoutLine, but may be later modified.
30 o there may be many Sections per TextBlock, hence the array */
31 typedef struct {
32 unsigned int x, y; /* where to draw it from */
33 unsigned short w, h; /* its width and height */
34 unsigned short begin; /* where the layout begins */
35 unsigned short end ; /* where it ends */
36 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
37 unsigned short last:1; /* is it the last section on a "line"? */
38 unsigned int _y:31; /* the "line" it and other textblocks are on */
39 } Section;
42 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
43 o text for the block, color and font
44 o or a pointer to the pixmap
45 o OR a pointer to the widget and the (text) description for its graphic
48 typedef struct _TextBlock {
49 struct _TextBlock *next; /* next text block in linked list */
50 struct _TextBlock *prior; /* prior text block in linked list */
52 char *text; /* pointer to text (could be kanji) */
53 /* or to the object's description */
54 union {
55 WMFont *font; /* the font */
56 WMWidget *widget; /* the embedded widget */
57 WMPixmap *pixmap; /* the pixmap */
58 } d; /* description */
60 unsigned short used; /* number of chars in this block */
61 unsigned short allocated; /* size of allocation (in chars) */
62 WMColor *color; /* the color */
64 Section *sections; /* the region for layouts (a growable array) */
65 /* an _array_! of size _nsections_ */
67 unsigned short s_begin; /* where the selection begins */
68 unsigned short s_end; /* where it ends */
70 unsigned int first:1; /* first TextBlock in paragraph */
71 unsigned int blank:1; /* ie. blank paragraph */
72 unsigned int kanji:1; /* is of 16-bit characters or not */
73 unsigned int graphic:1; /* graphic or text: text=0 */
74 unsigned int object:1; /* embedded object or pixmap */
75 unsigned int underlined:1; /* underlined or not */
76 unsigned int selected:1; /* selected or not */
77 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
78 int script:8; /* script in points: negative for subscript */
79 unsigned int marginN:8; /* which of the margins in the tPtr to use */
80 unsigned int nClicks:2; /* single, double, triple clicks */
81 unsigned int RESERVED:7;
82 } TextBlock;
85 /* I'm lazy: visible.h vs. visible.size.height :-) */
86 typedef struct {
87 int y, x, h, w;
88 } myRect;
91 typedef struct W_Text {
92 W_Class widgetClass; /* the class number of this widget */
93 W_View *view; /* the view referring to this instance */
95 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
97 WMScroller *vS; /* the vertical scroller */
98 unsigned int vpos; /* the current vertical position */
99 unsigned int prevVpos; /* the previous vertical position */
101 WMScroller *hS; /* the horizontal scroller */
102 unsigned int hpos; /* the current horizontal position */
103 unsigned int prevHpos; /* the previous horizontal position */
105 WMFont *dFont; /* the default font */
106 WMColor *dColor; /* the default color */
107 WMPixmap *dBulletPix; /* the default pixmap for bullets */
109 GC bgGC; /* the background GC to draw with */
110 GC fgGC; /* the foreground GC to draw with */
111 Pixmap db; /* the buffer on which to draw */
113 myRect visible; /* the actual rectangle that can be drawn into */
114 myRect cursor; /* the position and (height) of cursor */
115 myRect sel; /* the selection rectangle */
117 WMPoint clicked; /* where in the _document_ was clicked */
119 unsigned short tpos; /* the position in the currentTextBlock */
120 unsigned short docWidth; /* the width of the entire document */
121 unsigned int docHeight; /* the height of the entire document */
123 TextBlock *firstTextBlock;
124 TextBlock *lastTextBlock;
125 TextBlock *currentTextBlock;
127 WMArray *gfxItems; /* a nice array of graphic items */
129 #if DO_BLINK
130 WMHandlerID timerID; /* for nice twinky-winky */
131 #endif
133 WMAction *parser;
134 WMAction *writer;
135 WMTextDelegate *delegate;
136 Time lastClickTime;
138 WMRulerMargins *margins; /* an array of margins */
140 unsigned int nMargins:7; /* the total number of margins in use */
141 struct {
142 unsigned int monoFont:1; /* whether to ignore formats and graphic */
143 unsigned int focused:1; /* whether this instance has input focus */
144 unsigned int editable:1; /* "silly user, you can't edit me" */
145 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
146 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
147 unsigned int extendSelection:1; /* shift-drag to select more regions */
149 unsigned int rulerShown:1; /* whether the ruler is shown or not */
150 unsigned int frozen:1; /* whether screen updates are to be made */
151 unsigned int cursorShown:1; /* whether to show the cursor */
152 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
153 unsigned int horizOnDemand:1;/* if a large image should appear*/
154 unsigned int needsLayOut:1; /* in case of Append/Deletes */
155 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
156 unsigned int indentNewLine:1;/* add " " for a newline typed */
157 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
158 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
159 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
160 WMAlignment alignment:2; /* the alignment for text */
161 WMReliefType relief:3; /* the relief to display with */
162 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
163 unsigned int first:1; /* for plain text parsing, newline? */
164 /* unsigned int RESERVED:1; */
165 } flags;
166 } Text;
169 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
170 if ((T)->delegate && (T)->delegate->C)\
171 (*(T)->delegate->C)((T)->delegate,notif);\
172 WMPostNotification(notif);\
173 WMReleaseNotification(notif);}
176 #define TYPETEXT 0
178 static void
179 output(char *ptr, int len)
181 char s[len+1];
182 memcpy(s, ptr, len);
183 s[len] = 0;
184 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
185 printf("[%s]\n", s);
189 #if DO_BLINK
190 #define CURSOR_BLINK_ON_DELAY 600
191 #define CURSOR_BLINK_OFF_DELAY 400
192 #endif
194 static char *default_bullet[] = {
195 "6 6 4 1",
196 " c None s None", ". c black",
197 "X c white", "o c #808080",
198 " ... ",
199 ".XX.. ",
200 ".XX..o",
201 ".....o",
202 " ...oo",
203 " ooo "};
205 static void handleEvents(XEvent *event, void *data);
206 static void layOutDocument(Text *tPtr);
207 static void updateScrollers(Text *tPtr);
210 static int
211 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
213 unsigned int i=0;
215 for(i=0; i < tPtr->nMargins; i++) {
217 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
218 return i;
221 return -1;
226 static int
227 newMargin(Text *tPtr, WMRulerMargins *margins)
229 int n;
231 if (!margins) {
232 tPtr->margins[0].retainCount++;
233 return 0;
236 n = getMarginNumber(tPtr, margins);
238 if (n == -1) {
240 tPtr->margins = wrealloc(tPtr->margins,
241 (++tPtr->nMargins)*sizeof(WMRulerMargins));
243 n = tPtr->nMargins-1;
244 tPtr->margins[n].left = margins->left;
245 tPtr->margins[n].first = margins->first;
246 tPtr->margins[n].body = margins->body;
247 tPtr->margins[n].right = margins->right;
248 /* for each tab... */
249 tPtr->margins[n].retainCount = 1;
250 } else {
251 tPtr->margins[n].retainCount++;
254 return n;
257 static Bool
258 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
260 unsigned short i, w, lw, selected = False, extend = False;
261 myRect sel;
264 /* if selection rectangle completely encloses the section */
265 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
266 && (tb->sections[s]._y + tb->sections[s].h
267 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
268 sel.x = 0;
269 sel.w = tPtr->visible.w;
270 selected = extend = True;
272 /* or if it starts on a line and then goes further down */
273 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
274 && (tb->sections[s]._y + tb->sections[s].h
275 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
276 && (tb->sections[s]._y + tb->sections[s].h
277 >= tPtr->visible.y + tPtr->sel.y) ) {
278 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
279 sel.w = tPtr->visible.w;
280 selected = extend = True;
282 /* or if it begins before a line, but ends on it */
283 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
284 && (tb->sections[s]._y + tb->sections[s].h
285 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
286 && (tb->sections[s]._y
287 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
289 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
290 sel.w = tPtr->sel.x + tPtr->sel.w;
291 else
292 sel.w = tPtr->sel.x;
294 sel.x = 0;
295 selected = True;
297 /* or if the selection rectangle lies entirely within a line */
298 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
299 && (tPtr->sel.w >= 2)
300 && (tb->sections[s]._y + tb->sections[s].h
301 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
302 sel.x = tPtr->sel.x;
303 sel.w = tPtr->sel.w;
304 selected = True;
307 if (selected) {
308 selected = False;
310 /* if not within (modified) selection rectangle */
311 if ( tb->sections[s].x > sel.x + sel.w
312 || tb->sections[s].x + tb->sections[s].w < sel.x)
313 return False;
315 if (tb->graphic) {
316 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
317 && tb->sections[s].x >= sel.x) {
318 rect->width = tb->sections[s].w;
319 rect->x = tb->sections[s].x;
320 selected = True;
322 } else {
324 i = tb->sections[s].begin;
325 lw = 0;
327 if (0&& tb->sections[s].x >= sel.x) {
328 tb->s_begin = tb->sections[s].begin;
329 goto _selEnd;
332 while (++i <= tb->sections[s].end) {
334 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
335 lw += w;
337 if (lw + tb->sections[s].x >= sel.x
338 || i == tb->sections[s].end ) {
339 lw -= w;
340 i--;
341 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
342 break;
346 if (i > tb->sections[s].end) {
347 printf("WasSelected: (i > tb->sections[s].end) \n");
348 return False;
351 _selEnd: rect->x = tb->sections[s].x + lw;
352 lw = 0;
353 while(++i <= tb->sections[s].end) {
355 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
356 lw += w;
358 if (lw + rect->x >= sel.x + sel.w
359 || i == tb->sections[s].end ) {
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
366 rect->width = lw;
367 if (tb->sections[s].last && sel.x + sel.w
368 >= tb->sections[s].x + tb->sections[s].w
369 && extend ) {
370 rect->width += (tPtr->visible.w - rect->x - lw);
373 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
374 selected = True;
375 break;
376 } } } }
378 if (selected) {
379 rect->y = tb->sections[s]._y - tPtr->vpos;
380 rect->height = tb->sections[s].h;
381 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
383 return selected;
387 static void
388 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
390 TextBlock *tb;
391 int isFont=False;
393 tb = tPtr->firstTextBlock;
394 if (!tb || !tPtr->flags.ownsSelection)
395 return;
397 if(font && (!color || underlined==-1))
398 isFont = True;
400 while (tb) {
401 if (tPtr->flags.monoFont || tb->selected) {
403 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
404 || tb->graphic) {
406 if(isFont) {
407 if(!tb->graphic) {
408 WMReleaseFont(tb->d.font);
409 tb->d.font = WMRetainFont(font);
411 } else if(underlined !=-1) {
412 tb->underlined = underlined;
413 } else {
414 WMReleaseColor(tb->color);
415 tb->color = WMRetainColor(color);
418 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
420 TextBlock *midtb, *otb = tb;
422 if(underlined != -1) {
423 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
424 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
425 False, (tb->s_end - tb->s_begin));
426 } else {
427 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
428 &(tb->text[tb->s_begin]),
429 (isFont?font:tb->d.font),
430 (isFont?tb->color:color),
431 False, (tb->s_end - tb->s_begin));
435 if (midtb) {
436 if(underlined != -1) {
437 midtb->underlined = underlined;
438 } else {
439 midtb->underlined = otb->underlined;
442 midtb->selected = !True;
443 midtb->s_begin = 0;
444 midtb->s_end = midtb->used;
445 tPtr->currentTextBlock = tb;
446 WMAppendTextBlock(tPtr, midtb);
447 tb = tPtr->currentTextBlock;
450 if (otb->used - otb->s_end > 0) {
451 TextBlock *ntb;
452 ntb = (TextBlock *)
453 WMCreateTextBlockWithText(tPtr,
454 &(otb->text[otb->s_end]), otb->d.font, otb->color,
455 False, otb->used - otb->s_end);
457 if (ntb) {
458 ntb->underlined = otb->underlined;
459 ntb->selected = False;
460 WMAppendTextBlock(tPtr, ntb);
461 tb = tPtr->currentTextBlock;
465 if (midtb) {
466 tPtr->currentTextBlock = midtb;
469 otb->selected = False;
470 otb->used = otb->s_begin;
474 tb = tb->next;
477 tPtr->flags.needsLayOut = True;
478 WMThawText(tPtr);
480 /* in case the size changed... */
481 if(isFont && tPtr->currentTextBlock) {
482 TextBlock *tb = tPtr->currentTextBlock;
484 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
485 tPtr->sel.y = 3 + tb->sections[0]._y;
486 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
487 tPtr->sel.w = tb->sections[tb->nsections-1].w;
488 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
489 tPtr->sel.x = 0;
491 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
497 static Bool
498 removeSelection(Text *tPtr)
500 TextBlock *tb = NULL;
501 Bool first = False;
503 if (!(tb = tPtr->firstTextBlock))
504 return False;
506 while (tb) {
507 if (tb->selected) {
508 if(!first && !tb->graphic) {
509 WMReleaseFont(tPtr->dFont);
510 tPtr->dFont = WMRetainFont(tb->d.font);
511 first = True;
514 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
515 tPtr->currentTextBlock = tb;
516 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
517 tb = tPtr->currentTextBlock;
518 if (tb)
519 tPtr->tpos = 0;
520 continue;
522 } else if (tb->s_end <= tb->used) {
523 memmove(&(tb->text[tb->s_begin]),
524 &(tb->text[tb->s_end]), tb->used - tb->s_end);
525 tb->used -= (tb->s_end - tb->s_begin);
526 tb->selected = False;
527 tPtr->tpos = tb->s_begin;
532 tb = tb->next;
534 return True;
537 static TextBlock *
538 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
540 TextBlock *hold = tb;
542 if (!tb)
543 return NULL;
545 while (tb) {
546 if (!tb->graphic)
547 break;
548 tb = (dir? tb->next : tb->prior);
551 if(!tb) {
552 tb = hold;
553 while (tb) {
554 if (!tb->graphic)
555 break;
556 tb = (dir? tb->prior : tb->next);
560 if(!tb)
561 return NULL;
563 return tb;
567 static Bool
568 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
569 TextBlock *tb)
571 if (tPtr->flags.monoFont && tb->graphic) {
572 tb = getFirstNonGraphicBlockFor(tb, *dir);
573 if(!tb)
574 return 0;
576 if (tb->graphic) {
577 tPtr->currentTextBlock =
578 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
579 tPtr->tpos = 0;
580 return 0;
584 *dir = !(y <= tb->sections[0].y);
585 if(*dir) {
586 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
587 && (y >= tb->sections[0]._y ) ) {
588 /* if it's on the same line */
589 if(x < tb->sections[0].x)
590 *dir = 0;
592 } else {
593 if ( ( y <= tb->sections[tb->nsections-1]._y
594 + tb->sections[tb->nsections-1].h )
595 && (y >= tb->sections[tb->nsections-1]._y ) ) {
596 /* if it's on the same line */
597 if(x > tb->sections[tb->nsections-1].x)
598 *dir = 1;
602 return 1;
606 static void
607 paintText(Text *tPtr)
609 TextBlock *tb;
610 WMFont *font;
611 GC gc, greyGC;
612 char *text;
613 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
614 WMScreen *scr = tPtr->view->screen;
615 Display *dpy = tPtr->view->screen->display;
616 Window win = tPtr->view->window;
618 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
619 return;
621 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
622 0, 0, tPtr->visible.w, tPtr->visible.h);
624 if (! (tb = tPtr->currentTextBlock)) {
625 if (! (tb = tPtr->firstTextBlock)) {
626 goto _copy_area;
630 if (tPtr->flags.ownsSelection)
631 greyGC = WMColorGC(WMGrayColor(scr));
633 done = False;
637 /* first, which direction? Don't waste time looking all over,
638 since the parts to be drawn will most likely be near what
639 was previously drawn */
640 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
641 goto _copy_area;
643 while(tb) {
645 if (tb->graphic && tPtr->flags.monoFont)
646 goto _getSibling;
648 if(dir) {
649 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
650 + tb->sections[tb->nsections-1].h)
651 break;
652 } else {
653 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
654 + tb->sections[tb->nsections-1].h)
655 break;
658 _getSibling:
659 if(dir) {
660 if(tb->next)
661 tb = tb->next;
662 else break;
663 } else {
664 if(tb->prior)
665 tb = tb->prior;
666 else break;
671 /* first, place all text that can be viewed */
672 while (!done && tb) {
674 /* paragraph diagnostic
675 if(tb->blank) {tb->text[0] = 'F'; } */
677 if (tb->graphic) {
678 tb = tb->next;
679 continue;
682 tb->selected = False;
684 for(s=0; s<tb->nsections && !done; s++) {
686 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
687 done = True;
688 break;
691 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
692 continue;
694 if (tPtr->flags.monoFont) {
695 font = tPtr->dFont;
696 gc = tPtr->fgGC;
697 } else {
698 font = tb->d.font;
699 gc = WMColorGC(tb->color);
702 if (tPtr->flags.ownsSelection) {
703 XRectangle rect;
705 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
706 tb->selected = True;
707 XFillRectangle(dpy, tPtr->db, greyGC,
708 rect.x, rect.y, rect.width, rect.height);
712 prev_y = tb->sections[s]._y;
714 len = tb->sections[s].end - tb->sections[s].begin;
715 text = &(tb->text[tb->sections[s].begin]);
716 y = tb->sections[s].y - tPtr->vpos;
717 WMDrawString(scr, tPtr->db, gc, font,
718 tb->sections[s].x - tPtr->hpos, y, text, len);
720 if (!tPtr->flags.monoFont && tb->underlined) {
721 XDrawLine(dpy, tPtr->db, gc,
722 tb->sections[s].x - tPtr->hpos,
723 y + font->y + 1,
724 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
725 y + font->y + 1);
730 tb = (!done? tb->next : NULL);
734 /* now , show all graphic items that can be viewed */
735 c = WMGetArrayItemCount(tPtr->gfxItems);
736 if (c > 0 && !tPtr->flags.monoFont) {
737 int j, h;
739 for(j=0; j<c; j++) {
740 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
742 /* if it's not viewable, and mapped, unmap it */
743 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
744 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
746 if(tb->object) {
747 if ((W_VIEW(tb->d.widget))->flags.mapped) {
748 WMUnmapWidget(tb->d.widget);
751 } else {
752 /* if it's viewable, and not mapped, map it */
753 if(tb->object) {
754 W_View *view = W_VIEW(tb->d.widget);
756 if (!view->flags.realized)
757 WMRealizeWidget(tb->d.widget);
758 if(!view->flags.mapped) {
759 XMapWindow(view->screen->display, view->window);
760 XFlush(view->screen->display);
761 view->flags.mapped = 1;
765 if (tPtr->flags.ownsSelection) {
766 XRectangle rect;
768 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
769 tb->selected = True;
770 XFillRectangle(dpy, tPtr->db, greyGC,
771 rect.x, rect.y, rect.width, rect.height);
775 if(tb->object) {
776 WMMoveWidget(tb->d.widget,
777 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
778 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
779 h = WMWidgetHeight(tb->d.widget) + 1;
781 } else {
782 WMDrawPixmap(tb->d.pixmap, tPtr->db,
783 tb->sections[0].x - tPtr->hpos,
784 tb->sections[0].y - tPtr->vpos);
785 h = tb->d.pixmap->height + 1;
789 if (!tPtr->flags.monoFont && tb->underlined) {
790 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
791 tb->sections[0].x - tPtr->hpos,
792 tb->sections[0].y + h - tPtr->vpos,
793 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos);
795 } } } }
798 _copy_area:
799 if (tPtr->flags.editable && tPtr->flags.cursorShown
800 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
801 int y = tPtr->cursor.y - tPtr->vpos;
802 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
803 tPtr->cursor.x, y,
804 tPtr->cursor.x, y + tPtr->cursor.h);
807 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
808 tPtr->visible.w, tPtr->visible.h,
809 tPtr->visible.x, tPtr->visible.y);
811 W_DrawRelief(scr, win, 0, 0,
812 tPtr->view->size.width, tPtr->view->size.height,
813 tPtr->flags.relief);
815 if (tPtr->ruler && tPtr->flags.rulerShown)
816 XDrawLine(dpy, win, tPtr->fgGC,
817 2, 42, tPtr->view->size.width-4, 42);
821 static void
822 mouseOverObject(Text *tPtr, int x, int y)
824 TextBlock *tb;
825 Bool result = False;
827 x -= tPtr->visible.x;
828 x += tPtr->hpos;
829 y -= tPtr->visible.y;
830 y += tPtr->vpos;
832 if(tPtr->flags.ownsSelection) {
833 if(tPtr->sel.x <= x
834 && tPtr->sel.y <= y
835 && tPtr->sel.x + tPtr->sel.w >= x
836 && tPtr->sel.y + tPtr->sel.h >= y) {
837 tPtr->flags.isOverGraphic = 1;
838 result = True;
843 if(!result) {
844 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
846 if (c<1)
847 tPtr->flags.isOverGraphic = 0;
850 for(j=0; j<c; j++) {
851 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
853 if(!tb || !tb->sections) {
854 tPtr->flags.isOverGraphic = 0;
855 return;
858 if(!tb->object) {
859 if(tb->sections[0].x <= x
860 && tb->sections[0].y <= y
861 && tb->sections[0].x + tb->sections[0].w >= x
862 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
863 tPtr->flags.isOverGraphic = 3;
864 result = True;
865 break;
873 if(!result)
874 tPtr->flags.isOverGraphic = 0;
877 tPtr->view->attribs.cursor = (result?
878 tPtr->view->screen->defaultCursor
879 : tPtr->view->screen->textCursor);
881 XSetWindowAttributes attribs;
882 attribs.cursor = tPtr->view->attribs.cursor;
883 XChangeWindowAttributes(tPtr->view->screen->display,
884 tPtr->view->window, CWCursor,
885 &attribs);
889 #if DO_BLINK
891 static void
892 blinkCursor(void *data)
894 Text *tPtr = (Text*)data;
896 if (tPtr->flags.cursorShown) {
897 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
898 blinkCursor, data);
899 } else {
900 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
901 blinkCursor, data);
903 paintText(tPtr);
904 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
906 #endif
908 static void
909 updateCursorPosition(Text *tPtr)
911 TextBlock *tb = NULL;
912 int x, y, h, s;
914 if(tPtr->flags.needsLayOut)
915 layOutDocument(tPtr);
917 if (! (tb = tPtr->currentTextBlock)) {
918 if (! (tb = tPtr->firstTextBlock)) {
919 tPtr->tpos = 0;
920 tPtr->cursor.h = tPtr->dFont->height;
921 tPtr->cursor.y = 2;
922 tPtr->cursor.x = 2;
923 return;
928 if(tb->blank) {
929 tPtr->tpos = 0;
930 y = tb->sections[0].y;
931 h = tb->sections[0].h;
932 x = tb->sections[0].x;
934 } else if(tb->graphic) {
935 y = tb->sections[0].y;
936 h = tb->sections[0].h;
937 x = tb->sections[0].x;
939 } else {
940 if(tPtr->tpos > tb->used)
941 tPtr->tpos = tb->used;
943 for(s=0; s<tb->nsections-1; s++) {
945 if(tPtr->tpos >= tb->sections[s].begin
946 && tPtr->tpos <= tb->sections[s].end)
947 break;
950 y = tb->sections[s]._y;
951 h = tb->sections[s].h;
952 x = tb->sections[s].x + WMWidthOfString(
953 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
954 &tb->text[tb->sections[s].begin],
955 tPtr->tpos - tb->sections[s].begin);
958 tPtr->cursor.y = y;
959 tPtr->cursor.h = h;
960 tPtr->cursor.x = x;
963 /* scroll the bars if the cursor is not visible */
964 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
965 if(tPtr->cursor.y+tPtr->cursor.h
966 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
967 tPtr->vpos +=
968 (tPtr->cursor.y+tPtr->cursor.h+10
969 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
970 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
971 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
976 updateScrollers(tPtr);
980 static void
981 cursorToTextPosition(Text *tPtr, int x, int y)
983 TextBlock *tb = NULL;
984 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
985 char *text;
987 if(tPtr->flags.needsLayOut)
988 layOutDocument(tPtr);
990 y += (tPtr->vpos - tPtr->visible.y);
991 if (y<0)
992 y = 0;
994 x -= (tPtr->visible.x - 2);
995 if (x<0)
996 x=0;
998 /* clicked is relative to document, not window... */
999 tPtr->clicked.x = x;
1000 tPtr->clicked.y = y;
1002 if (! (tb = tPtr->currentTextBlock)) {
1003 if (! (tb = tPtr->firstTextBlock)) {
1004 tPtr->tpos = 0;
1005 tPtr->cursor.h = tPtr->dFont->height;
1006 tPtr->cursor.y = 2;
1007 tPtr->cursor.x = 2;
1008 return;
1012 /* first, which direction? Most likely, newly clicked
1013 position will be close to previous */
1014 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1015 return;
1018 s = (dir? 0 : tb->nsections-1);
1019 if ( y >= tb->sections[s]._y
1020 && y <= tb->sections[s]._y + tb->sections[s].h) {
1021 goto _doneV;
1024 /* get the first (or last) section of the TextBlock that
1025 lies about the vertical click point */
1026 done = False;
1027 while (!done && tb) {
1029 if (tPtr->flags.monoFont && tb->graphic) {
1030 if( (dir?tb->next:tb->prior))
1031 tb = (dir?tb->next:tb->prior);
1032 continue;
1035 s = (dir? 0 : tb->nsections-1);
1036 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1038 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1039 ( y >= tb->sections[s]._y ) ) ) {
1040 done = True;
1041 } else {
1042 dir? s++ : s--;
1046 if (!done) {
1047 if ( (dir? tb->next : tb->prior)) {
1048 tb = (dir ? tb->next : tb->prior);
1049 } else {
1050 pos = tb->used;
1051 break; /* goto _doneH; */
1057 if (s<0 || s>=tb->nsections) {
1058 s = (dir? tb->nsections-1 : 0);
1061 _doneV:
1062 /* we have the line, which TextBlock on that line is it? */
1063 pos = (dir?0:tb->sections[s].begin);
1064 if (tPtr->flags.monoFont && tb->graphic) {
1065 TextBlock *hold = tb;
1066 tb = getFirstNonGraphicBlockFor(hold, dir);
1068 if(!tb) {
1069 tPtr->tpos = 0;
1070 tb = hold;
1071 s = 0;
1072 goto _doNothing;
1077 if(tb->blank)
1078 _w = 0;
1080 _y = tb->sections[s]._y;
1082 while (tb) {
1084 if (tPtr->flags.monoFont && tb->graphic) {
1085 tb = (dir ? tb->next : tb->prior);
1086 continue;
1089 if (dir) {
1090 if (tb->graphic) {
1091 if(tb->object)
1092 _w = WMWidgetWidth(tb->d.widget)-5;
1093 else
1094 _w = tb->d.pixmap->width-5;
1095 } else {
1096 text = &(tb->text[tb->sections[s].begin]);
1097 len = tb->sections[s].end - tb->sections[s].begin;
1098 _w = WMWidthOfString(tb->d.font, text, len);
1099 if (tb->sections[s].x + _w >= x)
1100 break;
1103 } else {
1104 if (tb->sections[s].x <= x)
1105 break;
1108 if ((dir? tb->next : tb->prior)) {
1109 TextBlock *nxt = (dir? tb->next : tb->prior);
1110 if (tPtr->flags.monoFont && nxt->graphic) {
1111 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1112 if (!nxt) {
1113 pos = (dir?0:tb->sections[s].begin);
1114 tPtr->cursor.x = tb->sections[s].x;
1115 goto _doneH;
1119 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1120 /* this must be the last/first on this line. stop */
1121 pos = (dir? tb->sections[s].end : 0);
1122 tPtr->cursor.x = tb->sections[s].x;
1123 if (!tb->blank) {
1124 if (tb->graphic) {
1125 if(tb->object)
1126 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1127 else
1128 tPtr->cursor.x += tb->d.pixmap->width;
1129 } else if (pos > tb->sections[s].begin) {
1130 tPtr->cursor.x +=
1131 WMWidthOfString(tb->d.font,
1132 &(tb->text[tb->sections[s].begin]),
1133 pos - tb->sections[s].begin);
1136 goto _doneH;
1140 if ( (dir? tb->next : tb->prior)) {
1141 tb = (dir ? tb->next : tb->prior);
1142 } else {
1143 done = True;
1144 break;
1147 if (tb)
1148 s = (dir? 0 : tb->nsections-1);
1151 /* we have said TextBlock, now where within it? */
1152 if (tb && !tb->graphic) {
1153 WMFont *f = tb->d.font;
1154 len = tb->sections[s].end - tb->sections[s].begin;
1155 text = &(tb->text[tb->sections[s].begin]);
1157 _w = x - tb->sections[s].x;
1158 pos = 0;
1160 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1161 pos++;
1163 tPtr->cursor.x = tb->sections[s].x +
1164 (pos? WMWidthOfString(f, text, pos) : 0);
1166 pos += tb->sections[s].begin;
1167 _doneH:
1168 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1171 _doNothing:
1172 if (!tb)
1173 printf("...for this app will surely crash :-)\n");
1175 tPtr->currentTextBlock = tb;
1176 tPtr->cursor.h = tb->sections[s].h;
1177 tPtr->cursor.y = tb->sections[s]._y;
1179 /* scroll the bars if the cursor is not visible */
1180 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1181 if(tPtr->cursor.y+tPtr->cursor.h
1182 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1183 tPtr->vpos +=
1184 (tPtr->cursor.y+tPtr->cursor.h+10
1185 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1186 updateScrollers(tPtr);
1187 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1188 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1189 updateScrollers(tPtr);
1196 static void
1197 autoSelectText(Text *tPtr, int clicks)
1199 int x, start;
1200 TextBlock *tb;
1201 char *mark = NULL, behind, ahead;
1203 if(!(tb = tPtr->currentTextBlock))
1204 return;
1206 if(clicks == 2) {
1209 switch(tb->text[tPtr->tpos]) {
1210 case ' ': return;
1212 case '<': case '>': behind = '<'; ahead = '>'; break;
1213 case '{': case '}': behind = '{'; ahead = '}'; break;
1214 case '[': case ']': behind = '['; ahead = ']'; break;
1216 default: behind = ahead = ' ';
1219 tPtr->sel.y = tPtr->cursor.y+5;
1220 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
1222 if(tb->graphic) {
1223 tPtr->sel.x = tb->sections[0].x;
1224 tPtr->sel.w = tb->sections[0].w;
1225 } else {
1226 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1228 start = tPtr->tpos;
1229 while(start > 0 && tb->text[start-1] != behind)
1230 start--;
1232 x = tPtr->cursor.x;
1233 if(tPtr->tpos > start){
1234 x -= WMWidthOfString(font, &tb->text[start],
1235 tPtr->tpos - start);
1237 tPtr->sel.x = (x<0?0:x)+1;
1239 if((mark = strchr(&tb->text[start], ahead))) {
1240 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1241 (int)(mark - &tb->text[start]));
1242 } else if(tb->used > start) {
1243 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1244 tb->used - start);
1248 } else if(clicks == 3) {
1249 TextBlock *cur = tb;
1251 while(tb && !tb->first) {
1252 tb = tb->prior;
1254 tPtr->sel.y = tb->sections[0]._y;
1256 tb = cur;
1257 while(tb->next && !tb->next->first) {
1258 tb = tb->next;
1260 tPtr->sel.h = tb->sections[tb->nsections-1]._y
1261 + 5 - tPtr->sel.y;
1263 tPtr->sel.x = 0;
1264 tPtr->sel.w = tPtr->docWidth;
1265 tPtr->clicked.x = 0; /* only for now, fix sel. code */
1268 tPtr->flags.ownsSelection = True;
1269 paintText(tPtr);
1274 static void
1275 updateScrollers(Text *tPtr)
1278 if (tPtr->flags.frozen)
1279 return;
1281 if (tPtr->vS) {
1282 if (tPtr->docHeight < tPtr->visible.h) {
1283 WMSetScrollerParameters(tPtr->vS, 0, 1);
1284 tPtr->vpos = 0;
1285 } else {
1286 float hmax = (float)(tPtr->docHeight);
1287 WMSetScrollerParameters(tPtr->vS,
1288 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1289 (float)tPtr->visible.h/hmax);
1291 } else tPtr->vpos = 0;
1293 if (tPtr->hS) {
1294 if (tPtr->docWidth < tPtr->visible.w) {
1295 WMSetScrollerParameters(tPtr->hS, 0, 1);
1296 tPtr->hpos = 0;
1297 } else {
1298 float wmax = (float)(tPtr->docWidth);
1299 WMSetScrollerParameters(tPtr->hS,
1300 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1301 (float)tPtr->visible.w/wmax);
1303 } else tPtr->hpos = 0;
1306 static void
1307 scrollersCallBack(WMWidget *w, void *self)
1309 Text *tPtr = (Text *)self;
1310 Bool scroll = False;
1311 int which;
1313 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1314 return;
1316 if (w == tPtr->vS) {
1317 int height;
1318 height = tPtr->visible.h;
1320 which = WMGetScrollerHitPart(tPtr->vS);
1321 switch(which) {
1323 case WSDecrementLine:
1324 if (tPtr->vpos > 0) {
1325 if (tPtr->vpos>16) tPtr->vpos-=16;
1326 else tPtr->vpos=0;
1327 scroll=True;
1329 break;
1331 case WSIncrementLine: {
1332 int limit = tPtr->docHeight - height;
1333 if (tPtr->vpos < limit) {
1334 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1335 else tPtr->vpos=limit;
1336 scroll = True;
1339 break;
1341 case WSDecrementPage:
1342 if(((int)tPtr->vpos - (int)height) >= 0)
1343 tPtr->vpos -= height;
1344 else
1345 tPtr->vpos = 0;
1347 scroll = True;
1348 break;
1350 case WSIncrementPage:
1351 tPtr->vpos += height;
1352 if (tPtr->vpos > (tPtr->docHeight - height))
1353 tPtr->vpos = tPtr->docHeight - height;
1354 scroll = True;
1355 break;
1358 case WSKnob:
1359 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1360 * (float)(tPtr->docHeight - height);
1361 scroll = True;
1362 break;
1364 case WSKnobSlot:
1365 case WSNoPart:
1366 break;
1368 scroll = (tPtr->vpos != tPtr->prevVpos);
1369 tPtr->prevVpos = tPtr->vpos;
1373 if (w == tPtr->hS) {
1374 int width = tPtr->visible.w;
1376 which = WMGetScrollerHitPart(tPtr->hS);
1377 switch(which) {
1379 case WSDecrementLine:
1380 if (tPtr->hpos > 0) {
1381 if (tPtr->hpos>16) tPtr->hpos-=16;
1382 else tPtr->hpos=0;
1383 scroll=True;
1384 }break;
1386 case WSIncrementLine: {
1387 int limit = tPtr->docWidth - width;
1388 if (tPtr->hpos < limit) {
1389 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1390 else tPtr->hpos=limit;
1391 scroll = True;
1392 }}break;
1394 case WSDecrementPage:
1395 if(((int)tPtr->hpos - (int)width) >= 0)
1396 tPtr->hpos -= width;
1397 else
1398 tPtr->hpos = 0;
1400 scroll = True;
1401 break;
1403 case WSIncrementPage:
1404 tPtr->hpos += width;
1405 if (tPtr->hpos > (tPtr->docWidth - width))
1406 tPtr->hpos = tPtr->docWidth - width;
1407 scroll = True;
1408 break;
1411 case WSKnob:
1412 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1413 * (float)(tPtr->docWidth - width);
1414 scroll = True;
1415 break;
1417 case WSKnobSlot:
1418 case WSNoPart:
1419 break;
1421 scroll = (tPtr->hpos != tPtr->prevHpos);
1422 tPtr->prevHpos = tPtr->hpos;
1425 if (scroll) {
1426 updateScrollers(tPtr);
1427 paintText(tPtr);
1433 typedef struct {
1434 TextBlock *tb;
1435 unsigned short begin, end; /* what part of the text block */
1436 } myLineItems;
1439 static int
1440 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1442 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1443 WMFont *font;
1444 char *text;
1445 TextBlock *tb, *tbsame=NULL;
1447 if(!items || nitems == 0)
1448 return 0;
1450 for(i=0; i<nitems; i++) {
1451 tb = items[i].tb;
1453 if (tb->graphic) {
1454 if (!tPtr->flags.monoFont) {
1455 if(tb->object) {
1456 WMWidget *wdt = tb->d.widget;
1457 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1458 if (tPtr->flags.alignment != WALeft)
1459 lw += WMWidgetWidth(wdt);
1460 } else {
1461 line_height = WMAX(line_height,
1462 tb->d.pixmap->height + max_d);
1463 if (tPtr->flags.alignment != WALeft)
1464 lw += tb->d.pixmap->width;
1468 } else {
1469 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1470 max_d = WMAX(max_d, abs(font->height-font->y));
1471 line_height = WMAX(line_height, font->height + max_d);
1472 text = &(tb->text[items[i].begin]);
1473 len = items[i].end - items[i].begin;
1474 if (tPtr->flags.alignment != WALeft)
1475 lw += WMWidthOfString(font, text, len);
1479 if (tPtr->flags.alignment == WARight) {
1480 j = tPtr->visible.w - lw;
1481 } else if (tPtr->flags.alignment == WACenter) {
1482 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1485 for(i=0; i<nitems; i++) {
1486 tb = items[i].tb;
1488 if (tbsame == tb) { /* extend it, since it's on same line */
1489 tb->sections[tb->nsections-1].end = items[i].end;
1490 n = tb->nsections-1;
1491 } else {
1492 tb->sections = wrealloc(tb->sections,
1493 (++tb->nsections)*sizeof(Section));
1494 n = tb->nsections-1;
1495 tb->sections[n]._y = y + max_d;
1496 tb->sections[n].max_d = max_d;
1497 tb->sections[n].x = x+j;
1498 tb->sections[n].h = line_height;
1499 tb->sections[n].begin = items[i].begin;
1500 tb->sections[n].end = items[i].end;
1503 tb->sections[n].last = (i+1 == nitems);
1505 if (tb->graphic) {
1506 if (!tPtr->flags.monoFont) {
1507 if(tb->object) {
1508 WMWidget *wdt = tb->d.widget;
1509 tb->sections[n].y = max_d + y
1510 + line_height - WMWidgetHeight(wdt);
1511 tb->sections[n].w = WMWidgetWidth(wdt);
1512 } else {
1513 tb->sections[n].y = y + line_height
1514 + max_d - tb->d.pixmap->height;
1515 tb->sections[n].w = tb->d.pixmap->width;
1517 x += tb->sections[n].w;
1519 } else {
1520 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1521 len = items[i].end - items[i].begin;
1522 text = &(tb->text[items[i].begin]);
1524 tb->sections[n].y = y+line_height-font->y;
1525 tb->sections[n].w =
1526 WMWidthOfString(font,
1527 &(tb->text[tb->sections[n].begin]),
1528 tb->sections[n].end - tb->sections[n].begin);
1530 x += WMWidthOfString(font, text, len);
1533 tbsame = tb;
1536 return line_height;
1541 static void
1542 layOutDocument(Text *tPtr)
1544 TextBlock *tb;
1545 myLineItems *items = NULL;
1546 unsigned int itemsSize=0, nitems=0, begin, end;
1547 WMFont *font;
1548 unsigned int x, y=0, lw = 0, width=0, bmargin;
1549 char *start=NULL, *mark=NULL;
1551 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1552 return;
1554 assert(tPtr->visible.w > 20);
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, char wrap it */
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 if(i>2) i--;
1681 end = begin+i;
1682 start = &tb->text[end];
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;
1858 tPtr->cursor.x = -23;
1860 if (!tPtr->firstTextBlock)
1861 return;
1863 while (tPtr->currentTextBlock)
1864 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1866 tPtr->firstTextBlock = NULL;
1867 tPtr->currentTextBlock = NULL;
1868 tPtr->lastTextBlock = NULL;
1869 WMEmptyArray(tPtr->gfxItems);
1872 static void
1873 deleteTextInteractively(Text *tPtr, KeySym ksym)
1875 TextBlock *tb;
1876 Bool back = (Bool) (ksym == XK_BackSpace);
1877 Bool done = 1;
1878 Bool wasFirst = 0;
1880 if (!tPtr->flags.editable) {
1881 XBell(tPtr->view->screen->display, 0);
1882 return;
1885 if ( !(tb = tPtr->currentTextBlock) )
1886 return;
1888 if (tPtr->flags.ownsSelection) {
1889 if(removeSelection(tPtr))
1890 layOutDocument(tPtr);
1891 return;
1894 wasFirst = tb->first;
1895 if (back && tPtr->tpos < 1) {
1896 if (tb->prior) {
1897 if(tb->prior->blank) {
1898 tPtr->currentTextBlock = tb->prior;
1899 WMRemoveTextBlock(tPtr);
1900 tPtr->currentTextBlock = tb;
1901 tb->first = True;
1902 layOutDocument(tPtr);
1903 return;
1904 } else {
1905 if(tb->blank) {
1906 TextBlock *prior = tb->prior;
1907 tPtr->currentTextBlock = tb;
1908 WMRemoveTextBlock(tPtr);
1909 tb = prior;
1910 } else {
1911 tb = tb->prior;
1914 tPtr->tpos = tb->used;
1915 tPtr->currentTextBlock = tb;
1916 done = 1;
1917 if(wasFirst) {
1918 if(tb->next)
1919 tb->next->first = False;
1920 layOutDocument(tPtr);
1921 return;
1927 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1928 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1929 if (back)
1930 tPtr->tpos--;
1931 memmove(&(tb->text[tPtr->tpos]),
1932 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1933 tb->used--;
1934 done = 0;
1937 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1938 || tb->graphic) {
1940 TextBlock *sibling = (back? tb->prior : tb->next);
1942 if(tb->used == 0 || tb->graphic)
1943 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1945 if (sibling) {
1946 tPtr->currentTextBlock = sibling;
1947 tPtr->tpos = (back? sibling->used : 0);
1951 layOutDocument(tPtr);
1955 static void
1956 insertTextInteractively(Text *tPtr, char *text, int len)
1958 TextBlock *tb;
1959 char *newline = NULL;
1961 if (!tPtr->flags.editable) {
1962 XBell(tPtr->view->screen->display, 0);
1963 return;
1966 if (len < 1 || !text)
1967 return;
1970 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1971 return;
1974 if (tPtr->flags.ownsSelection)
1975 removeSelection(tPtr);
1978 if (tPtr->flags.ignoreNewLine) {
1979 int i;
1980 for(i=0; i<len; i++) {
1981 if (text[i] == '\n')
1982 text[i] = ' ';
1986 tb = tPtr->currentTextBlock;
1987 if (!tb || tb->graphic) {
1988 tPtr->tpos = 0;
1989 WMAppendTextStream(tPtr, text);
1990 layOutDocument(tPtr);
1991 return;
1994 if ((newline = strchr(text, '\n'))) {
1995 int nlen = (int)(newline-text);
1996 int s = tb->used - tPtr->tpos;
1997 char save[s];
1998 if (!tb->blank && nlen>0) {
1999 if (s > 0) {
2000 memcpy(save, &tb->text[tPtr->tpos], s);
2001 tb->used -= (tb->used - tPtr->tpos);
2003 insertTextInteractively(tPtr, text, nlen);
2004 newline++;
2005 WMAppendTextStream(tPtr, newline);
2006 if (s>0)
2007 insertTextInteractively(tPtr, save, s);
2009 } else {
2010 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2011 && !tb->graphic && tb->text) {
2013 void *ntb = WMCreateTextBlockWithText(
2014 tPtr, &tb->text[tPtr->tpos],
2015 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2016 tb->used = tPtr->tpos;
2017 WMAppendTextBlock(tPtr, ntb);
2018 tPtr->tpos = 0;
2020 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2021 if(tPtr->flags.indentNewLine) {
2022 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2023 " ", tb->d.font, tb->color, True, 4));
2024 tPtr->tpos = 4;
2025 } else {
2026 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2027 NULL, tb->d.font, tb->color, True, 0));
2028 tPtr->tpos = 0;
2033 } else {
2034 if (tb->used + len >= tb->allocated) {
2035 tb->allocated = reqBlockSize(tb->used+len);
2036 tb->text = wrealloc(tb->text, tb->allocated);
2039 if (tb->blank) {
2040 memcpy(tb->text, text, len);
2041 tb->used = len;
2042 tPtr->tpos = len;
2043 tb->text[tb->used] = 0;
2044 tb->blank = False;
2046 } else {
2047 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2048 tb->used-tPtr->tpos+1);
2049 memmove(&tb->text[tPtr->tpos], text, len);
2050 tb->used += len;
2051 tPtr->tpos += len;
2052 tb->text[tb->used] = 0;
2057 layOutDocument(tPtr);
2061 static void
2062 selectRegion(Text *tPtr, int x, int y)
2065 if (x < 0 || y < 0)
2066 return;
2068 y += (tPtr->flags.rulerShown? 40: 0);
2069 y += tPtr->vpos;
2070 if (y>10)
2071 y -= 10; /* the original offset */
2073 x -= tPtr->visible.x-2;
2074 if (x<0)
2075 x=0;
2077 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2078 tPtr->sel.w = abs(tPtr->clicked.x - x);
2079 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2080 tPtr->sel.h = abs(tPtr->clicked.y - y);
2082 tPtr->flags.ownsSelection = True;
2083 paintText(tPtr);
2087 static void
2088 releaseSelection(Text *tPtr)
2090 TextBlock *tb = tPtr->firstTextBlock;
2092 while(tb) {
2093 tb->selected = False;
2094 tb = tb->next;
2096 tPtr->flags.ownsSelection = False;
2097 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2098 CurrentTime);
2100 paintText(tPtr);
2104 WMData*
2105 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2106 Atom *type)
2108 Text *tPtr = view->self;
2109 Display *dpy = tPtr->view->screen->display;
2110 Atom _TARGETS;
2111 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2112 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2113 WMData *data = NULL;
2116 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2117 char *text = WMGetTextSelectedStream(tPtr);
2119 if (text) {
2120 printf("got text [%s]\n", text);
2121 data = WMCreateDataWithBytes(text, strlen(text));
2122 WMSetDataFormat(data, TYPETEXT);
2124 *type = target;
2125 return data;
2126 } else printf("didn't get it\n");
2128 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2129 if (target == _TARGETS) {
2130 Atom *ptr;
2132 ptr = wmalloc(4 * sizeof(Atom));
2133 ptr[0] = _TARGETS;
2134 ptr[1] = XA_STRING;
2135 ptr[2] = TEXT;
2136 ptr[3] = COMPOUND_TEXT;
2138 data = WMCreateDataWithBytes(ptr, 4*4);
2139 WMSetDataFormat(data, 32);
2141 *type = target;
2142 return data;
2145 return NULL;
2148 static void
2149 lostHandler(WMView *view, Atom selection, void *cdata)
2151 releaseSelection((WMText *)view->self);
2154 static WMSelectionProcs selectionHandler = {
2155 requestHandler, lostHandler, NULL
2159 static void
2160 ownershipObserver(void *observerData, WMNotification *notification)
2162 if (observerData != WMGetNotificationClientData(notification))
2163 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2167 static void
2168 fontChanged(void *observerData, WMNotification *notification)
2170 WMText *tPtr = (WMText *) observerData;
2171 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2172 printf("fontChanged\n");
2174 if(!tPtr || !font)
2175 return;
2177 if (tPtr->flags.ownsSelection)
2178 WMSetTextSelectionFont(tPtr, font);
2182 static void
2183 handleTextKeyPress(Text *tPtr, XEvent *event)
2185 char buffer[2];
2186 KeySym ksym;
2187 int control_pressed = False;
2188 TextBlock *tb = NULL;
2190 if (((XKeyEvent *) event)->state & ControlMask)
2191 control_pressed = True;
2192 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
2194 switch(ksym) {
2196 case XK_Left:
2197 if(!(tb = tPtr->currentTextBlock))
2198 break;
2199 if(tb->graphic)
2200 goto L_imaGFX;
2202 if(tPtr->tpos==0) {
2203 L_imaGFX: if(tb->prior) {
2204 tPtr->currentTextBlock = tb->prior;
2205 tPtr->tpos = tPtr->currentTextBlock->used -1;
2206 } else tPtr->tpos = 0;
2207 } else tPtr->tpos--;
2208 updateCursorPosition(tPtr);
2209 paintText(tPtr);
2210 break;
2212 case XK_Right:
2213 if(!(tb = tPtr->currentTextBlock))
2214 break;
2215 if(tb->graphic)
2216 goto R_imaGFX;
2217 if(tPtr->tpos == tb->used) {
2218 R_imaGFX: if(tb->next) {
2219 tPtr->currentTextBlock = tb->next;
2220 tPtr->tpos = 1;
2221 } else tPtr->tpos = tb->used;
2222 } else tPtr->tpos++;
2223 updateCursorPosition(tPtr);
2224 paintText(tPtr);
2225 break;
2227 case XK_Down:
2228 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2229 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2230 paintText(tPtr);
2231 break;
2233 case XK_Up:
2234 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2235 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2236 paintText(tPtr);
2237 break;
2239 case XK_BackSpace:
2240 case XK_Delete:
2241 case XK_KP_Delete:
2242 deleteTextInteractively(tPtr, ksym);
2243 updateCursorPosition(tPtr);
2244 paintText(tPtr);
2245 break;
2247 case XK_Control_R :
2248 case XK_Control_L :
2249 control_pressed = True;
2250 break;
2252 case XK_Tab:
2253 insertTextInteractively(tPtr, " ", 4);
2254 updateCursorPosition(tPtr);
2255 paintText(tPtr);
2256 break;
2258 case XK_Return:
2259 buffer[0] = '\n';
2260 default:
2261 if (buffer[0] != 0 && !control_pressed) {
2262 insertTextInteractively(tPtr, buffer, 1);
2263 updateCursorPosition(tPtr);
2264 paintText(tPtr);
2266 } else if (control_pressed && ksym==XK_r) {
2267 Bool i = !tPtr->flags.rulerShown;
2268 WMShowTextRuler(tPtr, i);
2269 tPtr->flags.rulerShown = i;
2271 else if (control_pressed && buffer[0] == '\a')
2272 XBell(tPtr->view->screen->display, 0);
2275 if (!control_pressed && tPtr->flags.ownsSelection)
2276 ;//releaseSelection(tPtr);
2279 static void
2280 handleWidgetPress(XEvent *event, void *data)
2282 TextBlock *tb = (TextBlock *)data;
2283 Text *tPtr;
2284 WMWidget *w;
2286 if (!tb)
2287 return;
2289 tPtr = (Text*)w;
2290 tPtr->currentTextBlock = tb;
2291 tPtr->flags.isOverGraphic = 2;
2292 tPtr->tpos = 0;
2293 output(tb->text, tb->used);
2294 #if 0
2295 if (!tPtr->flags.focused) {
2296 WMSetFocusToWidget(tPtr);
2297 tPtr->flags.focused = True;
2299 #endif
2303 static void
2304 handleActionEvents(XEvent *event, void *data)
2306 Text *tPtr = (Text *)data;
2307 Display *dpy = event->xany.display;
2308 KeySym ksym;
2311 switch (event->type) {
2312 case KeyPress:
2313 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2314 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2315 tPtr->flags.extendSelection = True;
2316 return;
2319 if (tPtr->flags.focused) {
2320 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2321 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2322 GrabModeAsync, GrabModeAsync, None,
2323 tPtr->view->screen->invisibleCursor, CurrentTime);
2324 tPtr->flags.pointerGrabbed = True;
2325 handleTextKeyPress(tPtr, event);
2327 } break;
2329 case KeyRelease:
2330 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2331 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2332 tPtr->flags.extendSelection = False;
2333 return;
2334 /* end modify flag so selection can be extended */
2336 break;
2339 case MotionNotify:
2341 if (tPtr->flags.pointerGrabbed) {
2342 tPtr->flags.pointerGrabbed = False;
2343 XUngrabPointer(dpy, CurrentTime);
2346 if(tPtr->flags.waitingForSelection)
2347 break;
2349 if ((event->xmotion.state & Button1Mask)) {
2350 if (!tPtr->flags.ownsSelection) {
2351 WMCreateSelectionHandler(tPtr->view,
2352 XA_PRIMARY, event->xbutton.time,
2353 &selectionHandler, NULL);
2354 tPtr->flags.ownsSelection = True;
2356 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2357 break;
2360 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2361 break;
2364 case ButtonPress:
2366 if (tPtr->flags.pointerGrabbed) {
2367 tPtr->flags.pointerGrabbed = False;
2368 XUngrabPointer(dpy, CurrentTime);
2369 break;
2372 if (tPtr->flags.waitingForSelection)
2373 break;
2375 if (tPtr->flags.extendSelection) {
2376 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2377 return;
2381 if (event->xbutton.button == Button1) {
2383 if(WMIsDoubleClick(event)) {
2384 TextBlock *tb = tPtr->currentTextBlock;
2386 if(tb && tb->graphic && !tb->object) {
2387 char desc[tb->used+1];
2388 memcpy(desc, tb->text, tb->used);
2389 desc[tb->used] = 0;
2390 if(tPtr->delegate) {
2391 if(tPtr->delegate->didDoubleClickOnPicture)
2392 (*tPtr->delegate->didDoubleClickOnPicture)
2393 (tPtr->delegate, desc);
2395 } else {
2396 autoSelectText(tPtr, 2);
2398 tPtr->lastClickTime = event->xbutton.time;
2399 break;
2400 } else if(event->xbutton.time - tPtr->lastClickTime
2401 < WINGsConfiguration.doubleClickDelay) {
2402 autoSelectText(tPtr, 3);
2403 break;
2406 if (!tPtr->flags.focused) {
2407 WMSetFocusToWidget(tPtr);
2408 tPtr->flags.focused = True;
2411 if (tPtr->flags.ownsSelection)
2412 releaseSelection(tPtr);
2414 tPtr->lastClickTime = event->xbutton.time;
2415 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2416 paintText(tPtr);
2419 if (event->xbutton.button
2420 == WINGsConfiguration.mouseWheelDown) {
2421 WMScrollText(tPtr, 16);
2422 break;
2425 if (event->xbutton.button
2426 == WINGsConfiguration.mouseWheelUp) {
2427 WMScrollText(tPtr, -16);
2428 break;
2431 if (event->xbutton.button == Button2) {
2432 char *text = NULL;
2433 int n;
2435 if (!tPtr->flags.editable) {
2436 XBell(dpy, 0);
2437 break;
2440 #if 0
2441 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2442 event->xbutton.time, pasteText, NULL)) {
2443 #endif
2446 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2448 if (text) {
2449 text[n] = 0;
2451 if (tPtr->parser)
2452 (tPtr->parser) (tPtr, (void *) text);
2453 else
2454 insertTextInteractively(tPtr, text, n);
2456 XFree(text);
2457 #if 0
2458 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2459 (void*)WMInsertTextEvent);
2460 #endif
2462 } else {
2463 tPtr->flags.waitingForSelection = True;
2466 break;
2470 case ButtonRelease:
2472 if (tPtr->flags.pointerGrabbed) {
2473 tPtr->flags.pointerGrabbed = False;
2474 XUngrabPointer(dpy, CurrentTime);
2475 break;
2478 if (tPtr->flags.waitingForSelection)
2479 break;
2485 static void
2486 handleEvents(XEvent *event, void *data)
2488 Text *tPtr = (Text *)data;
2490 switch(event->type) {
2491 case Expose:
2493 if (event->xexpose.count!=0)
2494 break;
2496 if(tPtr->hS) {
2497 if (!(W_VIEW(tPtr->hS))->flags.realized)
2498 WMRealizeWidget(tPtr->hS);
2501 if(tPtr->vS) {
2502 if (!(W_VIEW(tPtr->vS))->flags.realized)
2503 WMRealizeWidget(tPtr->vS);
2506 if(tPtr->ruler) {
2507 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2508 WMRealizeWidget(tPtr->ruler);
2512 if(!tPtr->db)
2513 textDidResize(tPtr->view->delegate, tPtr->view);
2515 paintText(tPtr);
2516 break;
2518 case FocusIn:
2519 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2520 != tPtr->view)
2521 return;
2522 tPtr->flags.focused = True;
2523 #if DO_BLINK
2524 if (tPtr->flags.editable && !tPtr->timerID) {
2525 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2526 blinkCursor, tPtr);
2528 #endif
2530 break;
2532 case FocusOut:
2533 tPtr->flags.focused = False;
2534 paintText(tPtr);
2535 #if DO_BLINK
2536 if (tPtr->timerID) {
2537 WMDeleteTimerHandler(tPtr->timerID);
2538 tPtr->timerID = NULL;
2540 #endif
2541 break;
2544 case DestroyNotify:
2545 clearText(tPtr);
2546 if(tPtr->db)
2547 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2548 if(tPtr->gfxItems)
2549 WMEmptyArray(tPtr->gfxItems);
2550 #if DO_BLINK
2551 if (tPtr->timerID)
2552 WMDeleteTimerHandler(tPtr->timerID);
2553 #endif
2554 WMReleaseFont(tPtr->dFont);
2555 WMReleaseColor(tPtr->dColor);
2556 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2557 WMRemoveNotificationObserver(tPtr);
2559 wfree(tPtr);
2561 break;
2567 static void
2568 insertPlainText(Text *tPtr, char *text)
2570 char *start, *mark;
2571 void *tb = NULL;
2573 start = text;
2574 while (start) {
2575 mark = strchr(start, '\n');
2576 if (mark) {
2577 tb = WMCreateTextBlockWithText(tPtr,
2578 start, tPtr->dFont,
2579 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2580 start = mark+1;
2581 tPtr->flags.first = True;
2582 } else {
2583 if (start && strlen(start)) {
2584 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2585 tPtr->dColor, tPtr->flags.first, strlen(start));
2586 } else tb = NULL;
2587 tPtr->flags.first = False;
2588 start = mark;
2591 if (tPtr->flags.prepend)
2592 WMPrependTextBlock(tPtr, tb);
2593 else
2594 WMAppendTextBlock(tPtr, tb);
2596 return;
2601 static void
2602 rulerMoveCallBack(WMWidget *w, void *self)
2604 Text *tPtr = (Text *)self;
2605 if (!tPtr)
2606 return;
2607 if (W_CLASS(tPtr) != WC_Text)
2608 return;
2610 paintText(tPtr);
2614 static void
2615 rulerReleaseCallBack(WMWidget *w, void *self)
2617 Text *tPtr = (Text *)self;
2618 if (!tPtr)
2619 return;
2620 if (W_CLASS(tPtr) != WC_Text)
2621 return;
2623 WMThawText(tPtr);
2624 return;
2628 static unsigned
2629 draggingEntered(WMView *self, WMDraggingInfo *info)
2631 printf("draggingEntered\n");
2632 return WDOperationCopy;
2636 static unsigned
2637 draggingUpdated(WMView *self, WMDraggingInfo *info)
2639 return WDOperationCopy;
2643 static void
2644 draggingExited(WMView *self, WMDraggingInfo *info)
2646 printf("draggingExited\n");
2649 static Bool
2650 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2652 printf("prepareForDragOperation\n");
2653 return True;
2657 char *badbadbad;
2659 static void
2660 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2661 void *cdata, WMData *data)
2663 badbadbad = wstrdup((char *)WMDataBytes(data));
2667 /* when it's done in WINGs, remove this */
2669 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2671 WMScreen *scr = W_VIEW_SCREEN(view);
2673 if (!WMRequestSelection(scr->dragInfo.destView,
2674 scr->xdndSelectionAtom,
2675 XInternAtom(scr->display, type, False),
2676 scr->dragInfo.timestamp,
2677 receivedData, &scr->dragInfo)) {
2678 wwarning("could not request data for dropped data");
2683 XEvent ev;
2685 ev.type = ClientMessage;
2686 ev.xclient.message_type = scr->xdndFinishedAtom;
2687 ev.xclient.format = 32;
2688 ev.xclient.window = info->destinationWindow;
2689 ev.xclient.data.l[0] = 0;
2690 ev.xclient.data.l[1] = 0;
2691 ev.xclient.data.l[2] = 0;
2692 ev.xclient.data.l[3] = 0;
2693 ev.xclient.data.l[4] = 0;
2695 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2696 XFlush(scr->display);
2698 return True;
2701 static Bool
2702 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2704 WMColor *color;
2705 WMText *tPtr = (WMText *)self->self;
2707 if (!tPtr)
2708 return True;
2710 requestDroppedData(tPtr->view, info, "application/X-color");
2711 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2712 if(color) {
2713 WMSetTextSelectionColor(tPtr, color);
2714 WMReleaseColor(color);
2719 return True;
2722 static void
2723 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2725 printf("concludeDragOperation\n");
2729 static WMDragDestinationProcs _DragDestinationProcs = {
2730 draggingEntered,
2731 draggingUpdated,
2732 draggingExited,
2733 prepareForDragOperation,
2734 performDragOperation,
2735 concludeDragOperation
2739 char *
2740 getStream(WMText *tPtr, int sel, int array)
2742 TextBlock *tb = NULL;
2743 char *text = NULL;
2744 unsigned long where = 0;
2746 if (!tPtr)
2747 return NULL;
2749 if (!(tb = tPtr->firstTextBlock))
2750 return NULL;
2752 if (tPtr->writer) {
2753 (tPtr->writer) (tPtr, (void *) text);
2754 return text;
2757 tb = tPtr->firstTextBlock;
2758 while (tb) {
2760 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2762 if (!sel || (tb->graphic && tb->selected)) {
2764 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2765 && tb != tPtr->firstTextBlock) {
2766 text = wrealloc(text, where+1);
2767 text[where++] = '\n';
2770 if(tb->blank)
2771 goto _gSnext;
2773 if(tb->graphic && array) {
2774 text = wrealloc(text, where+4);
2775 text[where++] = 0xFA;
2776 text[where++] = (tb->used>>8)&0x0ff;
2777 text[where++] = tb->used&0x0ff;
2778 text[where++] = tb->allocated; /* extra info */
2780 text = wrealloc(text, where+tb->used);
2781 memcpy(&text[where], tb->text, tb->used);
2782 where += tb->used;
2785 } else if (sel && tb->selected) {
2787 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2788 && tb != tPtr->firstTextBlock) {
2789 text = wrealloc(text, where+1);
2790 text[where++] = '\n';
2793 if(tb->blank)
2794 goto _gSnext;
2796 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2797 memcpy(&text[where], &tb->text[tb->s_begin],
2798 tb->s_end - tb->s_begin);
2799 where += tb->s_end - tb->s_begin;
2804 _gSnext:tb = tb->next;
2807 /* +1 for the end of string, let's be nice */
2808 text = wrealloc(text, where+1);
2809 text[where] = 0;
2810 return text;
2814 static void
2815 releaseStreamObjects(void *data)
2817 if(data)
2818 wfree(data);
2821 WMArray *
2822 getStreamObjects(WMText *tPtr, int sel)
2824 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2825 WMData *data;
2826 char *stream;
2827 unsigned short len;
2828 char *start, *fa, *desc;
2830 stream = getStream(tPtr, sel, 1);
2831 if(!stream)
2832 return NULL;
2834 start = stream;
2835 while (start) {
2837 fa = strchr(start, 0xFA);
2838 if (fa) {
2839 if((int)(fa - start)>0) {
2840 desc = start;
2841 desc[(int)(fa - start)] = 0;
2842 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2843 WMSetDataFormat(data, TYPETEXT);
2844 WMAddToArray(array, (void *) data);
2847 len = *(fa+1)*0xff + *(fa+2);
2848 data = WMCreateDataWithBytes((void *)(fa+4), len);
2849 WMSetDataFormat(data, *(fa+3));
2850 WMAddToArray(array, (void *) data);
2851 start = fa + len + 4;
2853 } else {
2854 if (start && strlen(start)) {
2855 data = WMCreateDataWithBytes((void *)start, strlen(start));
2856 WMSetDataFormat(data, TYPETEXT);
2857 WMAddToArray(array, (void *) data);
2859 start = fa;
2863 wfree(stream);
2864 return array;
2865 WMFreeArray(array);
2869 WMText *
2870 WMCreateTextForDocumentType(WMWidget *parent,
2871 WMAction *parser, WMAction *writer)
2873 Text *tPtr = wmalloc(sizeof(Text));
2874 if (!tPtr) {
2875 printf("could not create text widget\n");
2876 return NULL;
2880 memset(tPtr, 0, sizeof(Text));
2881 tPtr->widgetClass = WC_Text;
2882 tPtr->view = W_CreateView(W_VIEW(parent));
2883 if (!tPtr->view) {
2884 perror("could not create text's view\n");
2885 wfree(tPtr);
2886 return NULL;
2888 tPtr->view->self = tPtr;
2889 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2890 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2891 W_ResizeView(tPtr->view, 250, 200);
2893 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2894 tPtr->bgGC = WMColorGC(tPtr->dColor);
2895 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2896 WMReleaseColor(tPtr->dColor);
2898 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2899 tPtr->fgGC = WMColorGC(tPtr->dColor);
2901 tPtr->ruler = NULL;
2902 tPtr->vS = NULL;
2903 tPtr->hS = NULL;
2905 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2907 tPtr->view->delegate = &_TextViewDelegate;
2909 tPtr->delegate = NULL;
2911 #if DO_BLINK
2912 tPtr->timerID = NULL;
2913 #endif
2915 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2916 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2917 handleEvents, tPtr);
2919 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2920 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2921 handleActionEvents, tPtr);
2923 WMAddNotificationObserver(ownershipObserver, tPtr,
2924 "_lostOwnership", tPtr);
2926 if(0){
2927 char *types[2] = {"application/X-color", NULL};
2928 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2929 WMRegisterViewForDraggedTypes(tPtr->view, types);
2932 WMAddNotificationObserver(fontChanged, tPtr,
2933 "WMFontPanelDidChangeNotification", tPtr);
2935 tPtr->firstTextBlock = NULL;
2936 tPtr->lastTextBlock = NULL;
2937 tPtr->currentTextBlock = NULL;
2938 tPtr->tpos = 0;
2940 tPtr->gfxItems = WMCreateArray(4);
2942 tPtr->parser = parser;
2943 tPtr->writer = writer;
2945 tPtr->sel.x = tPtr->sel.y = 2;
2946 tPtr->sel.w = tPtr->sel.h = 0;
2948 tPtr->clicked.x = tPtr->clicked.y = 2;
2950 tPtr->visible.x = tPtr->visible.y = 2;
2951 tPtr->visible.h = tPtr->view->size.height;
2952 tPtr->visible.w = tPtr->view->size.width - 4;
2954 tPtr->cursor.x = -23;
2956 tPtr->docWidth = 0;
2957 tPtr->docHeight = 0;
2958 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2959 default_bullet);
2960 tPtr->db = (Pixmap) NULL;
2962 tPtr->margins = WMGetRulerMargins(NULL);
2963 tPtr->margins->right = tPtr->visible.w;
2964 tPtr->nMargins = 1;
2966 tPtr->flags.rulerShown = False;
2967 tPtr->flags.monoFont = False;
2968 tPtr->flags.focused = False;
2969 tPtr->flags.editable = True;
2970 tPtr->flags.ownsSelection = False;
2971 tPtr->flags.pointerGrabbed = False;
2972 tPtr->flags.extendSelection = False;
2973 tPtr->flags.frozen = False;
2974 tPtr->flags.cursorShown = True;
2975 tPtr->flags.acceptsGraphic = False;
2976 tPtr->flags.horizOnDemand = False;
2977 tPtr->flags.needsLayOut = False;
2978 tPtr->flags.ignoreNewLine = False;
2979 tPtr->flags.indentNewLine = False;
2980 tPtr->flags.laidOut = False;
2981 tPtr->flags.waitingForSelection = False;
2982 tPtr->flags.prepend = False;
2983 tPtr->flags.isOverGraphic = False;
2984 tPtr->flags.relief = WRSunken;
2985 tPtr->flags.isOverGraphic = 0;
2986 tPtr->flags.alignment = WALeft;
2987 tPtr->flags.first = True;
2989 return tPtr;
2992 void
2993 WMPrependTextStream(WMText *tPtr, char *text)
2995 CHECK_CLASS(tPtr, WC_Text);
2997 if(!text) {
2998 if(tPtr->flags.ownsSelection)
2999 releaseSelection(tPtr);
3000 else {
3001 clearText(tPtr);
3002 updateScrollers(tPtr);
3004 return;
3007 tPtr->flags.prepend = True;
3008 if (text && tPtr->parser)
3009 (tPtr->parser) (tPtr, (void *) text);
3010 else
3011 insertPlainText(tPtr, text);
3013 tPtr->flags.needsLayOut = True;
3014 tPtr->tpos = 0;
3018 void
3019 WMAppendTextStream(WMText *tPtr, char *text)
3021 CHECK_CLASS(tPtr, WC_Text);
3023 if(!text) {
3024 if(tPtr->flags.ownsSelection)
3025 releaseSelection(tPtr);
3026 else {
3027 clearText(tPtr);
3028 updateScrollers(tPtr);
3030 return;
3033 tPtr->flags.prepend = False;
3034 if (text && tPtr->parser)
3035 (tPtr->parser) (tPtr, (void *) text);
3036 else
3037 insertPlainText(tPtr, text);
3039 tPtr->flags.needsLayOut = True;
3040 if(tPtr->currentTextBlock)
3041 tPtr->tpos = tPtr->currentTextBlock->used;
3047 char *
3048 WMGetTextStream(WMText *tPtr)
3050 CHECK_CLASS(tPtr, WC_Text);
3051 return getStream(tPtr, 0, 0);
3054 char *
3055 WMGetTextSelectedStream(WMText *tPtr)
3057 CHECK_CLASS(tPtr, WC_Text);
3058 return getStream(tPtr, 1, 0);
3061 WMArray *
3062 WMGetTextObjects(WMText *tPtr)
3064 CHECK_CLASS(tPtr, WC_Text);
3065 return getStreamObjects(tPtr, 0);
3068 WMArray *
3069 WMGetTextSelectedObjects(WMText *tPtr)
3071 CHECK_CLASS(tPtr, WC_Text);
3072 return getStreamObjects(tPtr, 1);
3076 void
3077 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3079 CHECK_CLASS(tPtr, WC_Text);
3081 tPtr->delegate = delegate;
3085 void *
3086 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3087 char *description, WMColor *color,
3088 unsigned short first, unsigned short extraInfo)
3090 TextBlock *tb;
3092 if (!w || !description || !color)
3093 return NULL;
3095 tb = wmalloc(sizeof(TextBlock));
3096 if (!tb)
3097 return NULL;
3099 tb->text = wstrdup(description);
3100 tb->used = strlen(description);
3101 tb->blank = False;
3102 tb->d.widget = w;
3103 tb->color = WMRetainColor(color);
3104 tb->marginN = newMargin(tPtr, NULL);
3105 tb->allocated = extraInfo;
3106 tb->first = first;
3107 tb->kanji = False;
3108 tb->graphic = True;
3109 tb->object = True;
3110 tb->underlined = False;
3111 tb->selected = False;
3112 tb->script = 0;
3113 tb->sections = NULL;
3114 tb->nsections = 0;
3115 tb->prior = NULL;
3116 tb->next = NULL;
3118 return tb;
3122 void *
3123 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3124 char *description, WMColor *color,
3125 unsigned short first, unsigned short extraInfo)
3127 TextBlock *tb;
3129 if (!p || !description || !color)
3130 return NULL;
3132 tb = wmalloc(sizeof(TextBlock));
3133 if (!tb)
3134 return NULL;
3136 tb->text = wstrdup(description);
3137 tb->used = strlen(description);
3138 tb->blank = False;
3139 tb->d.pixmap = WMRetainPixmap(p);
3140 tb->color = WMRetainColor(color);
3141 tb->marginN = newMargin(tPtr, NULL);
3142 tb->allocated = extraInfo;
3143 tb->first = first;
3144 tb->kanji = False;
3145 tb->graphic = True;
3146 tb->object = False;
3147 tb->underlined = False;
3148 tb->selected = False;
3149 tb->script = 0;
3150 tb->sections = NULL;
3151 tb->nsections = 0;
3152 tb->prior = NULL;
3153 tb->next = NULL;
3155 return tb;
3158 void *
3159 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3160 unsigned short first, unsigned short len)
3162 TextBlock *tb;
3164 if (!font || !color)
3165 return NULL;
3167 tb = wmalloc(sizeof(TextBlock));
3168 if (!tb)
3169 return NULL;
3171 tb->allocated = reqBlockSize(len);
3172 tb->text = (char *)wmalloc(tb->allocated);
3173 memset(tb->text, 0, tb->allocated);
3175 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3176 *tb->text = ' ';
3177 tb->used = 1;
3178 tb->blank = True;
3179 } else {
3180 memcpy(tb->text, text, len);
3181 tb->used = len;
3182 tb->blank = False;
3184 tb->text[tb->used] = 0;
3186 tb->d.font = WMRetainFont(font);
3187 tb->color = WMRetainColor(color);
3188 tb->marginN = newMargin(tPtr, NULL);
3189 tb->first = first;
3190 tb->kanji = False;
3191 tb->graphic = False;
3192 tb->underlined = False;
3193 tb->selected = False;
3194 tb->script = 0;
3195 tb->sections = NULL;
3196 tb->nsections = 0;
3197 tb->prior = NULL;
3198 tb->next = NULL;
3199 return tb;
3202 void
3203 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3204 unsigned int kanji, unsigned int underlined, int script,
3205 WMRulerMargins *margins)
3207 TextBlock *tb = (TextBlock *) vtb;
3208 if (!tb)
3209 return;
3211 tb->first = first;
3212 tb->kanji = kanji;
3213 tb->underlined = underlined;
3214 tb->script = script;
3215 tb->marginN = newMargin(tPtr, margins);
3218 void
3219 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3220 unsigned int *kanji, unsigned int *underlined, int *script,
3221 WMRulerMargins *margins)
3223 TextBlock *tb = (TextBlock *) vtb;
3224 if (!tb)
3225 return;
3227 if (first) *first = tb->first;
3228 if (kanji) *kanji = tb->kanji;
3229 if (underlined) *underlined = tb->underlined;
3230 if (script) *script = tb->script;
3231 if (margins) margins = &tPtr->margins[tb->marginN];
3236 void
3237 WMPrependTextBlock(WMText *tPtr, void *vtb)
3239 TextBlock *tb = (TextBlock *)vtb;
3241 if (!tPtr || !tb)
3242 return;
3244 if (tb->graphic) {
3245 if(tb->object) {
3246 WMWidget *w = tb->d.widget;
3247 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3248 handleWidgetPress, tb);
3249 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3250 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3251 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3254 WMAddToArray(tPtr->gfxItems, (void *)tb);
3255 tPtr->tpos = 0;
3256 } else tPtr->tpos = tb->used;
3258 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3259 tb->next = tb->prior = NULL;
3260 tb->first = True;
3261 tPtr->lastTextBlock = tPtr->firstTextBlock
3262 = tPtr->currentTextBlock = tb;
3263 return;
3266 if(!tb->first) {
3267 tb->marginN = tPtr->currentTextBlock->marginN;
3270 tb->next = tPtr->currentTextBlock;
3271 tb->prior = tPtr->currentTextBlock->prior;
3272 if (tPtr->currentTextBlock->prior)
3273 tPtr->currentTextBlock->prior->next = tb;
3275 tPtr->currentTextBlock->prior = tb;
3276 if (!tb->prior)
3277 tPtr->firstTextBlock = tb;
3279 tPtr->currentTextBlock = tb;
3283 void
3284 WMAppendTextBlock(WMText *tPtr, void *vtb)
3286 TextBlock *tb = (TextBlock *)vtb;
3288 if (!tPtr || !tb)
3289 return;
3291 if (tb->graphic) {
3292 if(tb->object) {
3293 WMWidget *w = tb->d.widget;
3294 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3295 handleWidgetPress, tb);
3296 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3297 (W_VIEW(w))->attribs.cursor =
3298 tPtr->view->screen->defaultCursor;
3299 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3302 WMAddToArray(tPtr->gfxItems, (void *)tb);
3303 tPtr->tpos = 0;
3304 } else tPtr->tpos = tb->used;
3306 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3307 tb->next = tb->prior = NULL;
3308 tb->first = True;
3309 tPtr->lastTextBlock = tPtr->firstTextBlock
3310 = tPtr->currentTextBlock = tb;
3311 return;
3314 if(!tb->first) {
3315 tb->marginN = tPtr->currentTextBlock->marginN;
3318 tb->next = tPtr->currentTextBlock->next;
3319 tb->prior = tPtr->currentTextBlock;
3320 if (tPtr->currentTextBlock->next)
3321 tPtr->currentTextBlock->next->prior = tb;
3323 tPtr->currentTextBlock->next = tb;
3325 if (!tb->next)
3326 tPtr->lastTextBlock = tb;
3328 tPtr->currentTextBlock = tb;
3331 void *
3332 WMRemoveTextBlock(WMText *tPtr)
3334 TextBlock *tb = NULL;
3336 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3337 || !tPtr->currentTextBlock) {
3338 printf("cannot remove non existent TextBlock!\b");
3339 return NULL;
3342 tb = tPtr->currentTextBlock;
3343 if (tb->graphic) {
3344 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3346 if(tb->object) {
3347 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3348 handleWidgetPress, tb);
3349 WMUnmapWidget(tb->d.widget);
3353 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3354 if (tPtr->currentTextBlock->next)
3355 tPtr->currentTextBlock->next->prior = NULL;
3357 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3358 tPtr->currentTextBlock = tPtr->firstTextBlock;
3360 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3361 tPtr->currentTextBlock->prior->next = NULL;
3362 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3363 tPtr->currentTextBlock = tPtr->lastTextBlock;
3364 } else {
3365 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3366 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3367 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3370 return (void *)tb;
3373 void
3374 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3376 TextBlock *tb = (TextBlock *)vtb;
3377 if (!tPtr || !tb)
3378 return;
3380 if (tb->graphic) {
3381 if(tb->object) {
3382 /* naturally, there's a danger to destroying
3383 widgets whose action brings us here:
3384 ie. press a button to destroy it... need to
3385 find a safer way. till then... this stays commented out */
3386 /* WMDestroyWidget(tb->d.widget);
3387 wfree(tb->d.widget); */
3388 tb->d.widget = NULL;
3389 } else {
3390 WMReleasePixmap(tb->d.pixmap);
3391 tb->d.pixmap = NULL;
3393 } else {
3394 WMReleaseFont(tb->d.font);
3397 WMReleaseColor(tb->color);
3398 if (tb->sections && tb->nsections > 0)
3399 wfree(tb->sections);
3400 wfree(tb->text);
3401 wfree(tb);
3402 tb = NULL;
3407 void
3408 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3410 if (!tPtr)
3411 return;
3413 if (color)
3414 tPtr->fgGC = WMColorGC(color);
3415 else
3416 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3418 paintText(tPtr);
3421 void
3422 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3424 if (!tPtr)
3425 return;
3427 if (color) {
3428 tPtr->bgGC = WMColorGC(color);
3429 W_SetViewBackgroundColor(tPtr->view, color);
3430 } else {
3431 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3432 W_SetViewBackgroundColor(tPtr->view,
3433 WMWhiteColor(tPtr->view->screen));
3436 paintText(tPtr);
3439 void
3440 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3442 if (!tPtr)
3443 return;
3444 tPtr->flags.relief = relief;
3445 textDidResize(tPtr->view->delegate, tPtr->view);
3448 void
3449 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3451 if (!tPtr)
3452 return;
3454 if (shouldhave && !tPtr->hS) {
3455 tPtr->hS = WMCreateScroller(tPtr);
3456 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3457 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3458 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3459 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3460 WMMapWidget(tPtr->hS);
3461 } else if (!shouldhave && tPtr->hS) {
3462 WMUnmapWidget(tPtr->hS);
3463 WMDestroyWidget(tPtr->hS);
3464 tPtr->hS = NULL;
3467 tPtr->hpos = 0;
3468 tPtr->prevHpos = 0;
3469 textDidResize(tPtr->view->delegate, tPtr->view);
3473 void
3474 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3476 if (!tPtr)
3477 return;
3479 if(shouldhave && !tPtr->ruler) {
3480 tPtr->ruler = WMCreateRuler(tPtr);
3481 (W_VIEW(tPtr->ruler))->attribs.cursor =
3482 tPtr->view->screen->defaultCursor;
3483 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3484 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3485 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3486 } else if(!shouldhave && tPtr->ruler) {
3487 WMShowTextRuler(tPtr, False);
3488 WMDestroyWidget(tPtr->ruler);
3489 tPtr->ruler = NULL;
3491 textDidResize(tPtr->view->delegate, tPtr->view);
3494 void
3495 WMShowTextRuler(WMText *tPtr, Bool show)
3497 if(!tPtr)
3498 return;
3499 if(!tPtr->ruler)
3500 return;
3502 if(tPtr->flags.monoFont)
3503 show = False;
3505 tPtr->flags.rulerShown = show;
3506 if(show) {
3507 WMMapWidget(tPtr->ruler);
3508 } else {
3509 WMUnmapWidget(tPtr->ruler);
3512 textDidResize(tPtr->view->delegate, tPtr->view);
3515 Bool
3516 WMGetTextRulerShown(WMText *tPtr)
3518 if(!tPtr)
3519 return 0;
3521 if(!tPtr->ruler)
3522 return 0;
3524 return tPtr->flags.rulerShown;
3528 void
3529 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3531 if (!tPtr)
3532 return;
3534 if (shouldhave && !tPtr->vS) {
3535 tPtr->vS = WMCreateScroller(tPtr);
3536 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3537 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3538 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3539 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3540 WMMapWidget(tPtr->vS);
3541 } else if (!shouldhave && tPtr->vS) {
3542 WMUnmapWidget(tPtr->vS);
3543 WMDestroyWidget(tPtr->vS);
3544 tPtr->vS = NULL;
3547 tPtr->vpos = 0;
3548 tPtr->prevVpos = 0;
3549 textDidResize(tPtr->view->delegate, tPtr->view);
3554 Bool
3555 WMScrollText(WMText *tPtr, int amount)
3557 Bool scroll=False;
3558 if (!tPtr)
3559 return False;
3560 if (amount == 0 || !tPtr->view->flags.realized)
3561 return False;
3563 if (amount < 0) {
3564 if (tPtr->vpos > 0) {
3565 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3566 else tPtr->vpos=0;
3567 scroll=True;
3568 } } else {
3569 int limit = tPtr->docHeight - tPtr->visible.h;
3570 if (tPtr->vpos < limit) {
3571 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3572 else tPtr->vpos = limit;
3573 scroll = True;
3574 } }
3576 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3577 updateScrollers(tPtr);
3578 paintText(tPtr);
3580 tPtr->prevVpos = tPtr->vpos;
3581 return scroll;
3584 Bool
3585 WMPageText(WMText *tPtr, Bool direction)
3587 if (!tPtr) return False;
3588 if (!tPtr->view->flags.realized) return False;
3590 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3593 void
3594 WMSetTextEditable(WMText *tPtr, Bool editable)
3596 if (!tPtr)
3597 return;
3598 tPtr->flags.editable = editable;
3601 int
3602 WMGetTextEditable(WMText *tPtr)
3604 if (!tPtr)
3605 return 0;
3606 return tPtr->flags.editable;
3609 void
3610 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3612 if (!tPtr)
3613 return;
3614 tPtr->flags.indentNewLine = indent;
3617 void
3618 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3620 if (!tPtr)
3621 return;
3622 tPtr->flags.ignoreNewLine = ignore;
3625 Bool
3626 WMGetTextIgnoresNewline(WMText *tPtr)
3628 if (!tPtr)
3629 return True;
3630 return tPtr->flags.ignoreNewLine;
3633 void
3634 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3636 if (!tPtr)
3637 return;
3639 if (mono) {
3640 if(tPtr->flags.rulerShown)
3641 WMShowTextRuler(tPtr, False);
3642 if(tPtr->flags.alignment != WALeft)
3643 tPtr->flags.alignment = WALeft;
3646 tPtr->flags.monoFont = mono;
3647 textDidResize(tPtr->view->delegate, tPtr->view);
3650 Bool
3651 WMGetTextUsesMonoFont(WMText *tPtr)
3653 if (!tPtr)
3654 return True;
3655 return tPtr->flags.monoFont;
3659 void
3660 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3662 if (!tPtr)
3663 return;
3665 WMReleaseFont(tPtr->dFont);
3666 if (font)
3667 tPtr->dFont = WMRetainFont(font);
3668 else
3669 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3672 WMFont *
3673 WMGetTextDefaultFont(WMText *tPtr)
3675 if (!tPtr)
3676 return NULL;
3677 else
3678 return WMRetainFont(tPtr->dFont);
3681 void
3682 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3684 if (!tPtr)
3685 return;
3687 WMReleaseColor(tPtr->dColor);
3688 if (color)
3689 tPtr->dColor = WMRetainColor(color);
3690 else
3691 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3694 WMColor *
3695 WMGetTextDefaultColor(WMText *tPtr)
3697 if (!tPtr)
3698 return NULL;
3699 else
3700 return WMRetainColor(tPtr->dColor);
3703 void
3704 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3706 if (!tPtr)
3707 return;
3708 if(tPtr->flags.monoFont)
3709 tPtr->flags.alignment = WALeft;
3710 else
3711 tPtr->flags.alignment = alignment;
3712 WMThawText(tPtr);
3715 int
3716 WMGetTextInsertType(WMText *tPtr)
3718 if (!tPtr)
3719 return 0;
3720 return tPtr->flags.prepend;
3724 void
3725 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3727 if (!tPtr || !color)
3728 return;
3730 setSelectionProperty(tPtr, NULL, color, -1);
3733 WMColor *
3734 WMGetTextSelectionColor(WMText *tPtr)
3736 TextBlock *tb;
3738 if (!tPtr)
3739 return NULL;
3741 tb = tPtr->currentTextBlock;
3743 if (!tb || !tPtr->flags.ownsSelection)
3744 return NULL;
3746 if(!tb->selected)
3747 return NULL;
3749 return tb->color;
3753 void
3754 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3756 if (!tPtr || !font)
3757 return;
3759 setSelectionProperty(tPtr, font, NULL, -1) ;
3762 WMFont *
3763 WMGetTextSelectionFont(WMText *tPtr)
3765 TextBlock *tb;
3767 if (!tPtr)
3768 return NULL;
3770 tb = tPtr->currentTextBlock;
3772 if (!tb || !tPtr->flags.ownsSelection)
3773 return NULL;
3775 if(!tb->selected)
3776 return NULL;
3778 if(tb->graphic) {
3779 tb = getFirstNonGraphicBlockFor(tb, 1);
3780 if(!tb)
3781 return NULL;
3783 return (tb->selected ? tb->d.font : NULL);
3787 void
3788 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
3790 if (!tPtr || (underlined!=0 && underlined !=1))
3791 return;
3793 setSelectionProperty(tPtr, NULL, NULL, underlined);
3797 int
3798 WMGetTextSelectionUnderlined(WMText *tPtr)
3800 TextBlock *tb;
3802 if (!tPtr)
3803 return 0;
3805 tb = tPtr->currentTextBlock;
3807 if (!tb || !tPtr->flags.ownsSelection)
3808 return 0;
3810 if(!tb->selected)
3811 return 0;
3813 return tb->underlined;
3817 void
3818 WMFreezeText(WMText *tPtr)
3820 if (!tPtr)
3821 return;
3823 tPtr->flags.frozen = True;
3827 void
3828 WMThawText(WMText *tPtr)
3830 if (!tPtr)
3831 return;
3833 tPtr->flags.frozen = False;
3835 if(tPtr->flags.monoFont) {
3836 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3837 TextBlock *tb;
3839 /* make sure to unmap widgets no matter where they are */
3840 /* they'll be later remapped if needed by paintText */
3841 for(j=0; j<c; j++) {
3842 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3843 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3844 WMUnmapWidget(tb->d.widget);
3850 tPtr->flags.laidOut = False;
3851 layOutDocument(tPtr);
3852 updateScrollers(tPtr);
3853 paintText(tPtr);
3854 tPtr->flags.needsLayOut = False;
3858 /* find first occurence of a string */
3859 static char *
3860 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
3861 Bool caseSensitive)
3863 char *ptr;
3865 if(!haystack || !needle || !end)
3866 return NULL;
3868 for (ptr = haystack; ptr < end; ptr++) {
3869 if(caseSensitive) {
3870 if (*ptr == *needle && !strncmp(ptr, needle, len))
3871 return ptr;
3873 } else {
3874 if (tolower(*ptr) == tolower(*needle) &&
3875 !strncasecmp(ptr, needle, len))
3876 return ptr;
3880 return NULL;
3883 /* find last occurence of a string */
3884 static char *
3885 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
3886 Bool caseSensitive)
3888 char *ptr;
3890 if(!haystack || !needle || !end)
3891 return NULL;
3893 for (ptr = haystack-2; ptr > end; ptr--) {
3894 if(caseSensitive) {
3895 if (*ptr == *needle && !strncmp(ptr, needle, len))
3896 return ptr;
3897 } else {
3898 if (tolower(*ptr) == tolower(*needle) &&
3899 !strncasecmp(ptr, needle, len))
3900 return ptr;
3904 return NULL;
3908 Bool
3909 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3910 Bool caseSensitive)
3912 TextBlock *tb;
3913 char *mark=NULL;
3914 unsigned short pos;
3916 if (!tPtr || !needle)
3917 return False;
3919 #if 0
3920 if (! (tb = tPtr->currentTextBlock)) {
3921 if (! (tb = ( (direction > 0) ?
3922 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3923 return False;
3925 } else {
3926 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3927 tb = (direction>0) ? tb->next : tb->prior; */
3928 if(tb != tPtr->lastTextBlock)
3929 tb = tb->prior;
3931 #endif
3932 tb = tPtr->currentTextBlock;
3933 pos = tPtr->tpos;
3936 while(tb) {
3937 if (!tb->graphic) {
3939 if(direction > 0) {
3940 if(pos+1 < tb->used)
3941 pos++;
3943 if(tb->used - pos> 0 && pos > 0) {
3944 mark = mystrstr(&tb->text[pos], needle,
3945 strlen(needle), &tb->text[tb->used], caseSensitive);
3947 } else {
3948 tb = tb->next;
3949 pos = 0;
3950 continue;
3953 } else {
3954 if(pos-1 > 0)
3955 pos--;
3957 if(pos > 0) {
3958 mark = mystrrstr(&tb->text[pos], needle,
3959 strlen(needle), tb->text, caseSensitive);
3960 } else {
3961 tb = tb->prior;
3962 if(!tb)
3963 return False;
3964 pos = tb->used;
3965 continue;
3970 if(mark) {
3971 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3973 tPtr->tpos = (int)(mark - tb->text);
3974 tPtr->currentTextBlock = tb;
3975 updateCursorPosition(tPtr);
3976 tPtr->sel.y = tPtr->cursor.y+5;
3977 tPtr->sel.h = tPtr->cursor.h-10;
3978 tPtr->sel.x = tPtr->cursor.x +1;
3979 tPtr->sel.w = WMIN(WMWidthOfString(font,
3980 &tb->text[tPtr->tpos], strlen(needle)),
3981 tPtr->docWidth - tPtr->sel.x);
3982 tPtr->flags.ownsSelection = True;
3983 paintText(tPtr);
3985 return True;
3989 tb = (direction>0) ? tb->next : tb->prior;
3990 if(tb) {
3991 pos = (direction>0) ? 0 : tb->used;
3995 return False;
3999 Bool
4000 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4002 if (!tPtr)
4003 return False;
4005 if (!tPtr->flags.ownsSelection)
4006 return False;
4008 removeSelection(tPtr);
4010 if(replacement) {
4011 insertTextInteractively(tPtr, replacement, strlen(replacement));
4012 updateCursorPosition(tPtr);
4013 paintText(tPtr);
4016 return True;