- Added WMSetButtonImageDimsWhenDisabled() and WMGetButtonEnabled()
[wmaker-crm.git] / WINGs / wtext.c
blobc819a7f3f77b3fda89338d1d33b03049f8c310f8
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 * - verify what happens with XK_return in insertTextInt...
14 * - selection code... selects can be funny if it crosses over. use rect?
15 * - also inspect behaviour for WACenter and WARight
16 * - what if a widget grabs the click... howto say: "pressed me"?
17 * note that WMCreateEventHandler takes one data, but need widget & tPtr
18 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
19 * - check if support for Horizontal Scroll is complete
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 GC stippledGC; /* the GC to overlay selected graphics with */
112 Pixmap db; /* the buffer on which to draw */
113 WMPixmap *bgPixmap; /* the background pixmap */
115 myRect visible; /* the actual rectangle that can be drawn into */
116 myRect cursor; /* the position and (height) of cursor */
117 myRect sel; /* the selection rectangle */
119 WMPoint clicked; /* where in the _document_ was clicked */
121 unsigned short tpos; /* the position in the currentTextBlock */
122 unsigned short docWidth; /* the width of the entire document */
123 unsigned int docHeight; /* the height of the entire document */
125 TextBlock *firstTextBlock;
126 TextBlock *lastTextBlock;
127 TextBlock *currentTextBlock;
129 WMArray *gfxItems; /* a nice array of graphic items */
131 #if DO_BLINK
132 WMHandlerID timerID; /* for nice twinky-winky */
133 #endif
135 WMAction *parser;
136 WMAction *writer;
137 WMTextDelegate *delegate;
138 Time lastClickTime;
140 WMRulerMargins *margins; /* an array of margins */
142 unsigned int nMargins:7; /* the total number of margins in use */
143 struct {
144 unsigned int monoFont:1; /* whether to ignore formats and graphic */
145 unsigned int focused:1; /* whether this instance has input focus */
146 unsigned int editable:1; /* "silly user, you can't edit me" */
147 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
148 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
149 unsigned int extendSelection:1; /* shift-drag to select more regions */
151 unsigned int rulerShown:1; /* whether the ruler is shown or not */
152 unsigned int frozen:1; /* whether screen updates are to be made */
153 unsigned int cursorShown:1; /* whether to show the cursor */
154 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
155 unsigned int horizOnDemand:1;/* if a large image should appear*/
156 unsigned int needsLayOut:1; /* in case of Append/Deletes */
157 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
158 unsigned int indentNewLine:1;/* add " " for a newline typed */
159 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
160 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
161 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
162 WMAlignment alignment:2; /* the alignment for text */
163 WMReliefType relief:3; /* the relief to display with */
164 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
165 unsigned int first:1; /* for plain text parsing, newline? */
166 /* unsigned int RESERVED:1; */
167 } flags;
168 } Text;
171 #define NOTIFY(T,C,N,A) {\
172 WMNotification *notif = WMCreateNotification(N,T,A);\
173 if ((T)->delegate && (T)->delegate->C)\
174 (*(T)->delegate->C)((T)->delegate,notif);\
175 WMPostNotification(notif);\
176 WMReleaseNotification(notif);}
179 #define TYPETEXT 0
181 #if 0
182 /* just to print blocks of text not terminated by \0 */
183 static void
184 output(char *ptr, int len)
186 char *s;
188 s = wmalloc(len+1);
189 memcpy(s, ptr, len);
190 s[len] = 0;
191 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
192 printf("[%s]\n", s);
193 wfree(s);
195 #endif
198 #if DO_BLINK
199 #define CURSOR_BLINK_ON_DELAY 600
200 #define CURSOR_BLINK_OFF_DELAY 400
201 #endif
204 #define STIPPLE_WIDTH 8
205 #define STIPPLE_HEIGHT 8
206 static unsigned char STIPPLE_BITS[] = {
207 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
212 static char *default_bullet[] = {
213 "6 6 4 1",
214 " c None s None", ". c black",
215 "X c white", "o c #808080",
216 " ... ",
217 ".XX.. ",
218 ".XX..o",
219 ".....o",
220 " ...oo",
221 " ooo "};
224 static void handleEvents(XEvent *event, void *data);
225 static void layOutDocument(Text *tPtr);
226 static void updateScrollers(Text *tPtr);
229 static int
230 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
232 unsigned int i=0;
234 for(i=0; i < tPtr->nMargins; i++) {
236 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
237 return i;
240 return -1;
245 static int
246 newMargin(Text *tPtr, WMRulerMargins *margins)
248 int n;
250 if (!margins) {
251 tPtr->margins[0].retainCount++;
252 return 0;
255 n = getMarginNumber(tPtr, margins);
257 if (n == -1) {
259 if(tPtr->nMargins >= 127) {
260 n = tPtr->nMargins-1;
261 return n;
264 tPtr->margins = wrealloc(tPtr->margins,
265 (++tPtr->nMargins)*sizeof(WMRulerMargins));
267 n = tPtr->nMargins-1;
268 tPtr->margins[n].left = margins->left;
269 tPtr->margins[n].first = margins->first;
270 tPtr->margins[n].body = margins->body;
271 tPtr->margins[n].right = margins->right;
272 /* for each tab... */
273 tPtr->margins[n].retainCount = 1;
274 } else {
275 tPtr->margins[n].retainCount++;
278 return n;
281 static Bool
282 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
284 unsigned short i, w, lw, selected = False, extend = False;
285 myRect sel;
288 /* if selection rectangle completely encloses the section */
289 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
290 && (tb->sections[s]._y + tb->sections[s].h
291 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
292 sel.x = 0;
293 sel.w = tPtr->visible.w;
294 selected = extend = True;
296 /* or if it starts on a line and then goes further down */
297 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
298 && (tb->sections[s]._y + tb->sections[s].h
299 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
300 && (tb->sections[s]._y + tb->sections[s].h
301 >= tPtr->visible.y + tPtr->sel.y) ) {
302 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
303 sel.w = tPtr->visible.w;
304 selected = extend = True;
306 /* or if it begins before a line, but ends on it */
307 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
308 && (tb->sections[s]._y + tb->sections[s].h
309 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
310 && (tb->sections[s]._y
311 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
313 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
314 sel.w = tPtr->sel.x + tPtr->sel.w;
315 else
316 sel.w = tPtr->sel.x;
318 sel.x = 0;
319 selected = True;
321 /* or if the selection rectangle lies entirely within a line */
322 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
323 && (tPtr->sel.w >= 2)
324 && (tb->sections[s]._y + tb->sections[s].h
325 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
326 sel.x = tPtr->sel.x;
327 sel.w = tPtr->sel.w;
328 selected = True;
331 if (selected) {
332 selected = False;
334 /* if not within (modified) selection rectangle */
335 if ( tb->sections[s].x > sel.x + sel.w
336 || tb->sections[s].x + tb->sections[s].w < sel.x)
337 return False;
339 if (tb->graphic) {
340 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
341 && tb->sections[s].x >= sel.x) {
342 rect->width = tb->sections[s].w;
343 rect->x = tb->sections[s].x;
344 selected = True;
346 } else {
348 i = tb->sections[s].begin;
349 lw = 0;
351 if (0&& tb->sections[s].x >= sel.x) {
352 tb->s_begin = tb->sections[s].begin;
353 goto _selEnd;
356 while (++i <= tb->sections[s].end) {
358 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
359 lw += w;
361 if (lw + tb->sections[s].x >= sel.x
362 || i == tb->sections[s].end ) {
363 lw -= w;
364 i--;
365 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
366 break;
370 if (i > tb->sections[s].end) {
371 printf("WasSelected: (i > tb->sections[s].end) \n");
372 return False;
375 _selEnd: rect->x = tb->sections[s].x + lw;
376 lw = 0;
377 while(++i <= tb->sections[s].end) {
379 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
380 lw += w;
382 if (lw + rect->x >= sel.x + sel.w
383 || i == tb->sections[s].end ) {
385 if (i != tb->sections[s].end) {
386 lw -= w;
387 i--;
390 rect->width = lw;
391 if (tb->sections[s].last && sel.x + sel.w
392 >= tb->sections[s].x + tb->sections[s].w
393 && extend ) {
394 rect->width += (tPtr->visible.w - rect->x - lw);
397 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
398 selected = True;
399 break;
405 if (selected) {
406 rect->y = tb->sections[s]._y - tPtr->vpos;
407 rect->height = tb->sections[s].h;
408 if(tb->graphic) { printf("DEBUG: graphic s%d h%d\n", s,tb->sections[s].h);}
410 return selected;
414 static void
415 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
417 TextBlock *tb;
418 int isFont=False;
420 tb = tPtr->firstTextBlock;
421 if (!tb || !tPtr->flags.ownsSelection)
422 return;
424 if(font && (!color || underlined==-1))
425 isFont = True;
427 while (tb) {
428 if (tPtr->flags.monoFont || tb->selected) {
430 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
431 || tb->graphic) {
433 if(isFont) {
434 if(!tb->graphic) {
435 WMReleaseFont(tb->d.font);
436 tb->d.font = WMRetainFont(font);
438 } else if(underlined !=-1) {
439 tb->underlined = underlined;
440 } else {
441 WMReleaseColor(tb->color);
442 tb->color = WMRetainColor(color);
445 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
447 TextBlock *midtb, *otb = tb;
449 if(underlined != -1) {
450 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
451 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
452 False, (tb->s_end - tb->s_begin));
453 } else {
454 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
455 &(tb->text[tb->s_begin]),
456 (isFont?font:tb->d.font),
457 (isFont?tb->color:color),
458 False, (tb->s_end - tb->s_begin));
462 if (midtb) {
463 if(underlined != -1) {
464 midtb->underlined = underlined;
465 } else {
466 midtb->underlined = otb->underlined;
469 midtb->selected = !True;
470 midtb->s_begin = 0;
471 midtb->s_end = midtb->used;
472 tPtr->currentTextBlock = tb;
473 WMAppendTextBlock(tPtr, midtb);
474 tb = tPtr->currentTextBlock;
477 if (otb->used - otb->s_end > 0) {
478 TextBlock *ntb;
479 ntb = (TextBlock *)
480 WMCreateTextBlockWithText(tPtr,
481 &(otb->text[otb->s_end]), otb->d.font, otb->color,
482 False, otb->used - otb->s_end);
484 if (ntb) {
485 ntb->underlined = otb->underlined;
486 ntb->selected = False;
487 WMAppendTextBlock(tPtr, ntb);
488 tb = tPtr->currentTextBlock;
492 if (midtb) {
493 tPtr->currentTextBlock = midtb;
496 otb->selected = False;
497 otb->used = otb->s_begin;
501 tb = tb->next;
504 tPtr->flags.needsLayOut = True;
505 WMThawText(tPtr);
507 /* in case the size changed... */
508 if(isFont && tPtr->currentTextBlock) {
509 TextBlock *tb = tPtr->currentTextBlock;
511 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
512 tPtr->sel.y = 3 + tb->sections[0]._y;
513 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
514 tPtr->sel.w = tb->sections[tb->nsections-1].w;
515 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
516 tPtr->sel.x = 0;
518 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
524 static Bool
525 removeSelection(Text *tPtr)
527 TextBlock *tb = NULL;
528 Bool first = False;
530 if (!(tb = tPtr->firstTextBlock))
531 return False;
533 while (tb) {
534 if (tb->selected) {
535 if(!first && !tb->graphic) {
536 WMReleaseFont(tPtr->dFont);
537 tPtr->dFont = WMRetainFont(tb->d.font);
538 first = True;
541 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
542 tPtr->currentTextBlock = tb;
543 if(tb->next) {
544 tPtr->tpos = 0;
545 } else if(tb->prior) {
546 if(tb->prior->graphic)
547 tPtr->tpos = 1;
548 else
549 tPtr->tpos = tb->prior->used;
550 } else tPtr->tpos = 0;
552 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
553 tb = tPtr->currentTextBlock;
554 continue;
556 } else if (tb->s_end <= tb->used) {
557 memmove(&(tb->text[tb->s_begin]),
558 &(tb->text[tb->s_end]), tb->used - tb->s_end);
559 tb->used -= (tb->s_end - tb->s_begin);
560 tb->selected = False;
561 tPtr->tpos = tb->s_begin;
566 tb = tb->next;
568 return True;
571 static TextBlock *
572 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
574 TextBlock *hold = tb;
576 if (!tb)
577 return NULL;
579 while (tb) {
580 if (!tb->graphic)
581 break;
582 tb = (dir? tb->next : tb->prior);
585 if(!tb) {
586 tb = hold;
587 while (tb) {
588 if (!tb->graphic)
589 break;
590 tb = (dir? tb->prior : tb->next);
594 if(!tb)
595 return NULL;
597 return tb;
601 static Bool
602 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
603 TextBlock *tb)
605 if (tPtr->flags.monoFont && tb->graphic) {
606 tb = getFirstNonGraphicBlockFor(tb, *dir);
607 if(!tb)
608 return 0;
610 if (tb->graphic) {
611 tPtr->currentTextBlock =
612 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
613 tPtr->tpos = 0;
614 return 0;
619 if(!tb->sections) {
620 layOutDocument(tPtr);
621 return 0;
624 *dir = !(y <= tb->sections[0].y);
625 if(*dir) {
626 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
627 && (y >= tb->sections[0]._y ) ) {
628 /* if it's on the same line */
629 if(x < tb->sections[0].x)
630 *dir = 0;
632 } else {
633 if ( ( y <= tb->sections[tb->nsections-1]._y
634 + tb->sections[tb->nsections-1].h )
635 && (y >= tb->sections[tb->nsections-1]._y ) ) {
636 /* if it's on the same line */
637 if(x > tb->sections[tb->nsections-1].x)
638 *dir = 1;
642 return 1;
646 static void
647 paintText(Text *tPtr)
649 TextBlock *tb;
650 WMFont *font;
651 GC gc, greyGC;
652 char *text;
653 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
654 WMScreen *scr = tPtr->view->screen;
655 Display *dpy = tPtr->view->screen->display;
656 Window win = tPtr->view->window;
657 WMColor *color;
659 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
660 return;
663 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
664 0, 0, tPtr->visible.w, tPtr->visible.h);
666 if (tPtr->bgPixmap) {
667 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
668 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
669 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
672 if (! (tb = tPtr->currentTextBlock)) {
673 if (! (tb = tPtr->firstTextBlock)) {
674 goto _copy_area;
678 if (tPtr->flags.ownsSelection) {
679 color = WMGrayColor(scr);
680 greyGC = WMColorGC(color);
681 WMReleaseColor(color);
684 done = False;
688 /* first, which direction? Don't waste time looking all over,
689 since the parts to be drawn will most likely be near what
690 was previously drawn */
691 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
692 goto _copy_area;
694 while(tb) {
696 if (tb->graphic && tPtr->flags.monoFont)
697 goto _getSibling;
699 if(dir) {
700 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
701 + tb->sections[tb->nsections-1].h)
702 break;
703 } else {
704 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
705 + tb->sections[tb->nsections-1].h)
706 break;
709 _getSibling:
710 if(dir) {
711 if(tb->next)
712 tb = tb->next;
713 else break;
714 } else {
715 if(tb->prior)
716 tb = tb->prior;
717 else break;
722 /* first, place all text that can be viewed */
723 while (!done && tb) {
726 if (tb->graphic) {
727 tb = tb->next;
728 continue;
731 tb->selected = False;
733 for(s=0; s<tb->nsections && !done; s++) {
735 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
736 done = True;
737 break;
740 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
741 continue;
743 if (tPtr->flags.monoFont) {
744 font = tPtr->dFont;
745 gc = tPtr->fgGC;
746 } else {
747 font = tb->d.font;
748 gc = WMColorGC(tb->color);
751 if (tPtr->flags.ownsSelection) {
752 XRectangle rect;
754 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
755 tb->selected = True;
756 XFillRectangle(dpy, tPtr->db, greyGC,
757 rect.x, rect.y, rect.width, rect.height);
761 prev_y = tb->sections[s]._y;
763 len = tb->sections[s].end - tb->sections[s].begin;
764 text = &(tb->text[tb->sections[s].begin]);
765 y = tb->sections[s].y - tPtr->vpos;
766 WMDrawString(scr, tPtr->db, gc, font,
767 tb->sections[s].x - tPtr->hpos, y, text, len);
769 if (!tPtr->flags.monoFont && tb->underlined) {
770 XDrawLine(dpy, tPtr->db, gc,
771 tb->sections[s].x - tPtr->hpos,
772 y + font->y + 1,
773 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
774 y + font->y + 1);
779 tb = (!done? tb->next : NULL);
783 /* now , show all graphic items that can be viewed */
784 c = WMGetArrayItemCount(tPtr->gfxItems);
785 if (c > 0 && !tPtr->flags.monoFont) {
786 int j, h;
788 for(j=0; j<c; j++) {
789 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
791 /* if it's not viewable, and mapped, unmap it */
792 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
793 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
795 if(tb->object) {
796 if ((W_VIEW(tb->d.widget))->flags.mapped) {
797 WMUnmapWidget(tb->d.widget);
800 } else {
801 /* if it's viewable, and not mapped, map it */
802 if(tb->object) {
803 W_View *view = W_VIEW(tb->d.widget);
805 if (!view->flags.realized)
806 WMRealizeWidget(tb->d.widget);
807 if(!view->flags.mapped) {
808 XMapWindow(view->screen->display, view->window);
809 XFlush(view->screen->display);
810 view->flags.mapped = 1;
814 if(tb->object) {
815 WMMoveWidget(tb->d.widget,
816 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
817 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
818 h = WMWidgetHeight(tb->d.widget) + 1;
820 } else {
821 WMDrawPixmap(tb->d.pixmap, tPtr->db,
822 tb->sections[0].x - tPtr->hpos,
823 tb->sections[0].y - tPtr->vpos);
824 h = tb->d.pixmap->height + 1;
828 if (tPtr->flags.ownsSelection) {
829 XRectangle rect;
831 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
832 Drawable d = (0&&tb->object?
833 (WMWidgetView(tb->d.widget))->window : tPtr->db);
835 tb->selected = True;
836 XFillRectangle(dpy, d, tPtr->stippledGC,
837 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,*/
838 rect.x, rect.y, rect.width, rect.height);
842 if (!tPtr->flags.monoFont && tb->underlined) {
843 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
844 tb->sections[0].x - tPtr->hpos,
845 tb->sections[0].y + h - tPtr->vpos,
846 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
847 tb->sections[0].y + h - tPtr->vpos);
854 _copy_area:
855 if (tPtr->flags.editable && tPtr->flags.cursorShown
856 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
857 int y = tPtr->cursor.y - tPtr->vpos;
858 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
859 tPtr->cursor.x, y,
860 tPtr->cursor.x, y + tPtr->cursor.h);
863 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
864 tPtr->visible.w, tPtr->visible.h,
865 tPtr->visible.x, tPtr->visible.y);
867 W_DrawRelief(scr, win, 0, 0,
868 tPtr->view->size.width, tPtr->view->size.height,
869 tPtr->flags.relief);
871 if (tPtr->ruler && tPtr->flags.rulerShown)
872 XDrawLine(dpy, win, tPtr->fgGC,
873 2, 42, tPtr->view->size.width-4, 42);
877 static void
878 mouseOverObject(Text *tPtr, int x, int y)
880 TextBlock *tb;
881 Bool result = False;
883 x -= tPtr->visible.x;
884 x += tPtr->hpos;
885 y -= tPtr->visible.y;
886 y += tPtr->vpos;
888 if(tPtr->flags.ownsSelection) {
889 if(tPtr->sel.x <= x
890 && tPtr->sel.y <= y
891 && tPtr->sel.x + tPtr->sel.w >= x
892 && tPtr->sel.y + tPtr->sel.h >= y) {
893 tPtr->flags.isOverGraphic = 1;
894 result = True;
899 if(!result) {
900 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
902 if (c<1)
903 tPtr->flags.isOverGraphic = 0;
906 for(j=0; j<c; j++) {
907 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
909 if(!tb || !tb->sections) {
910 tPtr->flags.isOverGraphic = 0;
911 return;
914 if(!tb->object) {
915 if(tb->sections[0].x <= x
916 && tb->sections[0].y <= y
917 && tb->sections[0].x + tb->sections[0].w >= x
918 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
919 tPtr->flags.isOverGraphic = 3;
920 result = True;
921 break;
929 if(!result)
930 tPtr->flags.isOverGraphic = 0;
933 tPtr->view->attribs.cursor = (result?
934 tPtr->view->screen->defaultCursor
935 : tPtr->view->screen->textCursor);
937 XSetWindowAttributes attribs;
938 attribs.cursor = tPtr->view->attribs.cursor;
939 XChangeWindowAttributes(tPtr->view->screen->display,
940 tPtr->view->window, CWCursor,
941 &attribs);
945 #if DO_BLINK
947 static void
948 blinkCursor(void *data)
950 Text *tPtr = (Text*)data;
952 if (tPtr->flags.cursorShown) {
953 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
954 blinkCursor, data);
955 } else {
956 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
957 blinkCursor, data);
959 paintText(tPtr);
960 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
962 #endif
964 static void
965 updateCursorPosition(Text *tPtr)
967 TextBlock *tb = NULL;
968 int x, y, h, s;
970 if(tPtr->flags.needsLayOut)
971 layOutDocument(tPtr);
973 if (! (tb = tPtr->currentTextBlock)) {
974 if (! (tb = tPtr->firstTextBlock)) {
975 WMFont *font = tPtr->dFont;
976 tPtr->tpos = 0;
977 tPtr->cursor.h = font->height + abs(font->height-font->y);
979 tPtr->cursor.y = 2;
980 tPtr->cursor.x = 2;
981 return;
986 if(tb->blank) {
987 tPtr->tpos = 0;
988 y = tb->sections[0].y;
989 h = tb->sections[0].h;
990 x = tb->sections[0].x;
992 } else if(tb->graphic) {
993 y = tb->sections[0].y;
994 h = tb->sections[0].h;
995 x = tb->sections[0].x;
996 if(tPtr->tpos == 1)
997 x += tb->sections[0].w;
999 } else {
1000 if(tPtr->tpos > tb->used)
1001 tPtr->tpos = tb->used;
1003 for(s=0; s<tb->nsections-1; s++) {
1005 if(tPtr->tpos >= tb->sections[s].begin
1006 && tPtr->tpos <= tb->sections[s].end)
1007 break;
1010 y = tb->sections[s]._y;
1011 h = tb->sections[s].h;
1012 x = tb->sections[s].x + WMWidthOfString(
1013 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
1014 &tb->text[tb->sections[s].begin],
1015 tPtr->tpos - tb->sections[s].begin);
1018 tPtr->cursor.y = y;
1019 tPtr->cursor.h = h;
1020 tPtr->cursor.x = x;
1023 /* scroll the bars if the cursor is not visible */
1024 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1025 if(tPtr->cursor.y+tPtr->cursor.h
1026 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1027 tPtr->vpos +=
1028 (tPtr->cursor.y+tPtr->cursor.h+10
1029 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1030 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1031 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1036 updateScrollers(tPtr);
1040 static void
1041 cursorToTextPosition(Text *tPtr, int x, int y)
1043 TextBlock *tb = NULL;
1044 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1045 char *text;
1047 if(tPtr->flags.needsLayOut)
1048 layOutDocument(tPtr);
1050 y += (tPtr->vpos - tPtr->visible.y);
1051 if (y<0)
1052 y = 0;
1054 x -= (tPtr->visible.x - 2);
1055 if (x<0)
1056 x=0;
1058 /* clicked is relative to document, not window... */
1059 tPtr->clicked.x = x;
1060 tPtr->clicked.y = y;
1062 if (! (tb = tPtr->currentTextBlock)) {
1063 if (! (tb = tPtr->firstTextBlock)) {
1064 WMFont *font = tPtr->dFont;
1065 tPtr->tpos = 0;
1066 tPtr->cursor.h = font->height + abs(font->height-font->y);
1067 tPtr->cursor.y = 2;
1068 tPtr->cursor.x = 2;
1069 return;
1073 /* first, which direction? Most likely, newly clicked
1074 position will be close to previous */
1075 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1076 return;
1079 s = (dir? 0 : tb->nsections-1);
1080 if ( y >= tb->sections[s]._y
1081 && y <= tb->sections[s]._y + tb->sections[s].h) {
1082 goto _doneV;
1085 /* get the first (or last) section of the TextBlock that
1086 lies about the vertical click point */
1087 done = False;
1088 while (!done && tb) {
1090 if (tPtr->flags.monoFont && tb->graphic) {
1091 if( (dir?tb->next:tb->prior))
1092 tb = (dir?tb->next:tb->prior);
1093 continue;
1096 s = (dir? 0 : tb->nsections-1);
1097 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1099 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1100 ( y >= tb->sections[s]._y ) ) ) {
1101 done = True;
1102 } else {
1103 dir? s++ : s--;
1107 if (!done) {
1108 if ( (dir? tb->next : tb->prior)) {
1109 tb = (dir ? tb->next : tb->prior);
1110 } else {
1111 pos = tb->used;
1112 break; /* goto _doneH; */
1118 if (s<0 || s>=tb->nsections) {
1119 s = (dir? tb->nsections-1 : 0);
1122 _doneV:
1123 /* we have the line, which TextBlock on that line is it? */
1124 pos = (dir?0:tb->sections[s].begin);
1125 if (tPtr->flags.monoFont && tb->graphic) {
1126 TextBlock *hold = tb;
1127 tb = getFirstNonGraphicBlockFor(hold, dir);
1129 if(!tb) {
1130 tPtr->tpos = 0;
1131 tb = hold;
1132 s = 0;
1133 goto _doNothing;
1138 if(tb->blank)
1139 _w = 0;
1141 _y = tb->sections[s]._y;
1143 while (tb) {
1145 if (tPtr->flags.monoFont && tb->graphic) {
1146 tb = (dir ? tb->next : tb->prior);
1147 continue;
1150 if (dir) {
1151 if (tb->graphic) {
1152 if(tb->object)
1153 _w = WMWidgetWidth(tb->d.widget)-5;
1154 else
1155 _w = tb->d.pixmap->width-5;
1157 if (tb->sections[0].x + _w >= x)
1158 break;
1159 } else {
1160 text = &(tb->text[tb->sections[s].begin]);
1161 len = tb->sections[s].end - tb->sections[s].begin;
1162 _w = WMWidthOfString(tb->d.font, text, len);
1163 if (tb->sections[s].x + _w >= x)
1164 break;
1167 } else {
1168 if (tb->sections[s].x <= x)
1169 break;
1172 if ((dir? tb->next : tb->prior)) {
1173 TextBlock *nxt = (dir? tb->next : tb->prior);
1174 if (tPtr->flags.monoFont && nxt->graphic) {
1175 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1176 if (!nxt) {
1177 pos = (dir?0:tb->sections[s].begin);
1178 tPtr->cursor.x = tb->sections[s].x;
1179 goto _doneH;
1183 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1184 /* this must be the last/first on this line. stop */
1185 pos = (dir? tb->sections[s].end : 0);
1186 tPtr->cursor.x = tb->sections[s].x;
1187 if (!tb->blank) {
1188 if (tb->graphic) {
1189 if(tb->object)
1190 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1191 else
1192 tPtr->cursor.x += tb->d.pixmap->width;
1193 } else if (pos > tb->sections[s].begin) {
1194 tPtr->cursor.x +=
1195 WMWidthOfString(tb->d.font,
1196 &(tb->text[tb->sections[s].begin]),
1197 pos - tb->sections[s].begin);
1200 goto _doneH;
1204 if ( (dir? tb->next : tb->prior)) {
1205 tb = (dir ? tb->next : tb->prior);
1206 } else {
1207 done = True;
1208 break;
1211 if (tb)
1212 s = (dir? 0 : tb->nsections-1);
1215 /* we have said TextBlock, now where within it? */
1216 if (tb) {
1217 if(tb->graphic) {
1218 int gw = (tb->object ?
1219 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1221 tPtr->cursor.x = tb->sections[0].x;
1223 if(x > tPtr->cursor.x + gw/2) {
1224 pos = 1;
1225 tPtr->cursor.x += gw;
1226 } else {
1227 printf("first %d\n", tb->first);
1228 if(tb->prior) {
1229 if(tb->prior->graphic) pos = 1;
1230 else pos = tb->prior->used;
1231 tb = tb->prior;
1232 } else pos = 0;
1236 s = 0;
1237 goto _doneH;
1239 } else {
1240 WMFont *f = tb->d.font;
1241 len = tb->sections[s].end - tb->sections[s].begin;
1242 text = &(tb->text[tb->sections[s].begin]);
1244 _w = x - tb->sections[s].x;
1245 pos = 0;
1247 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1248 pos++;
1250 tPtr->cursor.x = tb->sections[s].x +
1251 (pos? WMWidthOfString(f, text, pos) : 0);
1253 pos += tb->sections[s].begin;
1257 _doneH:
1258 if(tb->graphic) {
1259 tPtr->tpos = (pos<=1)? pos : 0;
1260 } else {
1261 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1263 _doNothing:
1264 if (!tb)
1265 printf("...for this app will surely crash :-)\n");
1267 tPtr->currentTextBlock = tb;
1268 tPtr->cursor.h = tb->sections[s].h;
1269 tPtr->cursor.y = tb->sections[s]._y;
1271 /* scroll the bars if the cursor is not visible */
1272 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1273 if(tPtr->cursor.y+tPtr->cursor.h
1274 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1275 tPtr->vpos +=
1276 (tPtr->cursor.y+tPtr->cursor.h+10
1277 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1278 updateScrollers(tPtr);
1279 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1280 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1281 updateScrollers(tPtr);
1289 static void
1290 updateScrollers(Text *tPtr)
1293 if (tPtr->flags.frozen)
1294 return;
1296 if (tPtr->vS) {
1297 if (tPtr->docHeight <= tPtr->visible.h) {
1298 WMSetScrollerParameters(tPtr->vS, 0, 1);
1299 tPtr->vpos = 0;
1300 } else {
1301 float hmax = (float)(tPtr->docHeight);
1302 WMSetScrollerParameters(tPtr->vS,
1303 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1304 (float)tPtr->visible.h/hmax);
1306 } else tPtr->vpos = 0;
1308 if (tPtr->hS) {
1309 if (tPtr->docWidth <= tPtr->visible.w) {
1310 WMSetScrollerParameters(tPtr->hS, 0, 1);
1311 tPtr->hpos = 0;
1312 } else {
1313 float wmax = (float)(tPtr->docWidth);
1314 WMSetScrollerParameters(tPtr->hS,
1315 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1316 (float)tPtr->visible.w/wmax);
1318 } else tPtr->hpos = 0;
1321 static void
1322 scrollersCallBack(WMWidget *w, void *self)
1324 Text *tPtr = (Text *)self;
1325 Bool scroll = False;
1326 int which;
1328 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1329 return;
1331 if (w == tPtr->vS) {
1332 int height;
1333 height = tPtr->visible.h;
1335 which = WMGetScrollerHitPart(tPtr->vS);
1336 switch(which) {
1338 case WSDecrementLine:
1339 if (tPtr->vpos > 0) {
1340 if (tPtr->vpos>16) tPtr->vpos-=16;
1341 else tPtr->vpos=0;
1342 scroll=True;
1344 break;
1346 case WSIncrementLine: {
1347 int limit = tPtr->docHeight - height;
1348 if (tPtr->vpos < limit) {
1349 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1350 else tPtr->vpos=limit;
1351 scroll = True;
1354 break;
1356 case WSDecrementPage:
1357 if(((int)tPtr->vpos - (int)height) >= 0)
1358 tPtr->vpos -= height;
1359 else
1360 tPtr->vpos = 0;
1362 scroll = True;
1363 break;
1365 case WSIncrementPage:
1366 tPtr->vpos += height;
1367 if (tPtr->vpos > (tPtr->docHeight - height))
1368 tPtr->vpos = tPtr->docHeight - height;
1369 scroll = True;
1370 break;
1373 case WSKnob:
1374 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1375 * (float)(tPtr->docHeight - height);
1376 scroll = True;
1377 break;
1379 case WSKnobSlot:
1380 case WSNoPart:
1381 break;
1383 scroll = (tPtr->vpos != tPtr->prevVpos);
1384 tPtr->prevVpos = tPtr->vpos;
1388 if (w == tPtr->hS) {
1389 int width = tPtr->visible.w;
1391 which = WMGetScrollerHitPart(tPtr->hS);
1392 switch(which) {
1394 case WSDecrementLine:
1395 if (tPtr->hpos > 0) {
1396 if (tPtr->hpos>16) tPtr->hpos-=16;
1397 else tPtr->hpos=0;
1398 scroll=True;
1399 }break;
1401 case WSIncrementLine: {
1402 int limit = tPtr->docWidth - width;
1403 if (tPtr->hpos < limit) {
1404 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1405 else tPtr->hpos=limit;
1406 scroll = True;
1408 }break;
1410 case WSDecrementPage:
1411 if(((int)tPtr->hpos - (int)width) >= 0)
1412 tPtr->hpos -= width;
1413 else
1414 tPtr->hpos = 0;
1416 scroll = True;
1417 break;
1419 case WSIncrementPage:
1420 tPtr->hpos += width;
1421 if (tPtr->hpos > (tPtr->docWidth - width))
1422 tPtr->hpos = tPtr->docWidth - width;
1423 scroll = True;
1424 break;
1427 case WSKnob:
1428 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1429 * (float)(tPtr->docWidth - width);
1430 scroll = True;
1431 break;
1433 case WSKnobSlot:
1434 case WSNoPart:
1435 break;
1437 scroll = (tPtr->hpos != tPtr->prevHpos);
1438 tPtr->prevHpos = tPtr->hpos;
1441 if (scroll) {
1442 updateScrollers(tPtr);
1443 paintText(tPtr);
1449 typedef struct {
1450 TextBlock *tb;
1451 unsigned short begin, end; /* what part of the text block */
1452 } myLineItems;
1455 static int
1456 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1458 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1459 WMFont *font;
1460 char *text;
1461 TextBlock *tb, *tbsame=NULL;
1463 if(!items || nitems == 0)
1464 return 0;
1466 for(i=0; i<nitems; i++) {
1467 tb = items[i].tb;
1469 if (tb->graphic) {
1470 if (!tPtr->flags.monoFont) {
1471 if(tb->object) {
1472 WMWidget *wdt = tb->d.widget;
1473 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1474 if (tPtr->flags.alignment != WALeft)
1475 lw += WMWidgetWidth(wdt);
1476 } else {
1477 line_height = WMAX(line_height,
1478 tb->d.pixmap->height + max_d);
1479 if (tPtr->flags.alignment != WALeft)
1480 lw += tb->d.pixmap->width;
1484 } else {
1485 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1486 max_d = WMAX(max_d, abs(font->height-font->y));
1487 line_height = WMAX(line_height, font->height + max_d);
1488 text = &(tb->text[items[i].begin]);
1489 len = items[i].end - items[i].begin;
1490 if (tPtr->flags.alignment != WALeft)
1491 lw += WMWidthOfString(font, text, len);
1495 if (tPtr->flags.alignment == WARight) {
1496 j = tPtr->visible.w - lw;
1497 } else if (tPtr->flags.alignment == WACenter) {
1498 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1501 for(i=0; i<nitems; i++) {
1502 tb = items[i].tb;
1504 if (tbsame == tb) { /* extend it, since it's on same line */
1505 tb->sections[tb->nsections-1].end = items[i].end;
1506 n = tb->nsections-1;
1507 } else {
1508 tb->sections = wrealloc(tb->sections,
1509 (++tb->nsections)*sizeof(Section));
1510 n = tb->nsections-1;
1511 tb->sections[n]._y = y + max_d;
1512 tb->sections[n].max_d = max_d;
1513 tb->sections[n].x = x+j;
1514 tb->sections[n].h = line_height;
1515 tb->sections[n].begin = items[i].begin;
1516 tb->sections[n].end = items[i].end;
1519 tb->sections[n].last = (i+1 == nitems);
1521 if (tb->graphic) {
1522 if (!tPtr->flags.monoFont) {
1523 if(tb->object) {
1524 WMWidget *wdt = tb->d.widget;
1525 tb->sections[n].y = max_d + y
1526 + line_height - WMWidgetHeight(wdt);
1527 tb->sections[n].w = WMWidgetWidth(wdt);
1528 } else {
1529 tb->sections[n].y = y + line_height
1530 + max_d - tb->d.pixmap->height;
1531 tb->sections[n].w = tb->d.pixmap->width;
1533 x += tb->sections[n].w;
1535 } else {
1536 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1537 len = items[i].end - items[i].begin;
1538 text = &(tb->text[items[i].begin]);
1540 tb->sections[n].y = y+line_height-font->y;
1541 tb->sections[n].w =
1542 WMWidthOfString(font,
1543 &(tb->text[tb->sections[n].begin]),
1544 tb->sections[n].end - tb->sections[n].begin);
1546 x += WMWidthOfString(font, text, len);
1549 tbsame = tb;
1552 return line_height;
1557 static void
1558 layOutDocument(Text *tPtr)
1560 TextBlock *tb;
1561 myLineItems *items = NULL;
1562 unsigned int itemsSize=0, nitems=0, begin, end;
1563 WMFont *font;
1564 unsigned int x, y=0, lw = 0, width=0, bmargin;
1565 char *start=NULL, *mark=NULL;
1567 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1568 return;
1570 assert(tPtr->visible.w > 20);
1572 tPtr->docWidth = tPtr->visible.w;
1573 x = tPtr->margins[tb->marginN].first;
1574 bmargin = tPtr->margins[tb->marginN].body;
1576 /* only partial layOut needed: re-Lay only affected textblocks */
1577 if (tPtr->flags.laidOut) {
1578 tb = tPtr->currentTextBlock;
1580 /* search backwards for textblocks on same line */
1581 while (tb->prior) {
1582 if (!tb->sections || tb->nsections<1) {
1583 tb = tPtr->firstTextBlock;
1584 tPtr->flags.laidOut = False;
1585 y = 0;
1586 goto _layOut;
1589 if(!tb->prior->sections || tb->prior->nsections<1) {
1590 tb = tPtr->firstTextBlock;
1591 tPtr->flags.laidOut = False;
1592 y = 0;
1593 goto _layOut;
1596 if (tb->sections[0]._y !=
1597 tb->prior->sections[tb->prior->nsections-1]._y) {
1598 break;
1600 tb = tb->prior;
1603 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1604 y = tb->prior->sections[tb->prior->nsections-1]._y +
1605 tb->prior->sections[tb->prior->nsections-1].h -
1606 tb->prior->sections[tb->prior->nsections-1].max_d;
1607 } else {
1608 y = 0;
1612 _layOut:
1613 while (tb) {
1615 if (tb->sections && tb->nsections>0) {
1616 wfree(tb->sections);
1617 tb->sections = NULL;
1618 tb->nsections = 0;
1621 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1622 TextBlock *next = tb->next;
1623 tPtr->currentTextBlock = tb;
1624 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1625 tb = next;
1626 tb->first = True;
1627 continue;
1630 if (tb->first && tb != tPtr->firstTextBlock) {
1631 y += layOutLine(tPtr, items, nitems, x, y);
1632 x = tPtr->margins[tb->marginN].first;
1633 bmargin = tPtr->margins[tb->marginN].body;
1634 nitems = 0;
1635 lw = 0;
1638 if (tb->graphic) {
1639 if (!tPtr->flags.monoFont) {
1640 if(tb->object)
1641 width = WMWidgetWidth(tb->d.widget);
1642 else
1643 width = tb->d.pixmap->width;
1645 if (width > tPtr->docWidth)
1646 tPtr->docWidth = width;
1648 lw += width;
1649 if (lw >= tPtr->visible.w - x ) {
1650 y += layOutLine(tPtr, items, nitems, x, y);
1651 nitems = 0;
1652 x = bmargin;
1653 lw = width;
1656 if(nitems + 1> itemsSize) {
1657 items = wrealloc(items,
1658 (++itemsSize)*sizeof(myLineItems));
1661 items[nitems].tb = tb;
1662 items[nitems].begin = 0;
1663 items[nitems].end = 0;
1664 nitems++;
1667 } else if ((start = tb->text)) {
1668 begin = end = 0;
1669 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1671 while (start) {
1672 mark = strchr(start, ' ');
1673 if (mark) {
1674 end += (int)(mark-start)+1;
1675 start = mark+1;
1676 } else {
1677 end += strlen(start);
1678 start = mark;
1681 if (end > tb->used)
1682 end = tb->used;
1684 if (end-begin > 0) {
1686 width = WMWidthOfString(font,
1687 &tb->text[begin], end-begin);
1689 /* if it won't fit, char wrap it */
1690 if (width >= tPtr->visible.w) {
1691 char *t = &tb->text[begin];
1692 int l=end-begin, i=0;
1693 do {
1694 width = WMWidthOfString(font, t, ++i);
1695 } while (width < tPtr->visible.w && i < l);
1696 if(i>2) i--;
1697 end = begin+i;
1698 start = &tb->text[end];
1701 lw += width;
1704 if (lw >= tPtr->visible.w - x) {
1705 y += layOutLine(tPtr, items, nitems, x, y);
1706 lw = width;
1707 x = bmargin;
1708 nitems = 0;
1711 if(nitems + 1 > itemsSize) {
1712 items = wrealloc(items,
1713 (++itemsSize)*sizeof(myLineItems));
1716 items[nitems].tb = tb;
1717 items[nitems].begin = begin;
1718 items[nitems].end = end;
1719 nitems++;
1721 begin = end;
1726 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1727 if(0&&tPtr->flags.laidOut
1728 && tb->next && tb->next->sections && tb->next->nsections>0
1729 && (tPtr->vpos + tPtr->visible.h
1730 < tb->next->sections[0]._y)) {
1731 if(tPtr->lastTextBlock->sections
1732 && tPtr->lastTextBlock->nsections > 0 ) {
1733 TextBlock *ltb = tPtr->lastTextBlock;
1734 int ly = ltb->sections[ltb->nsections-1]._y;
1735 int lh = ltb->sections[ltb->nsections-1].h;
1736 int ss, sd;
1738 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1739 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1741 y += layOutLine(tPtr, items, nitems, x, y);
1742 ss= ly+lh-y;
1743 sd = tPtr->docHeight-y;
1745 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1746 y += tb->next->sections[0]._y-y;
1747 nitems = 0;
1748 printf("nitems%d\n", nitems);
1749 if(ss-sd!=0)
1750 y = tPtr->docHeight+ss-sd;
1752 break;
1753 } else {
1754 tPtr->flags.laidOut = False;
1758 tb = tb->next;
1762 if (nitems > 0)
1763 y += layOutLine(tPtr, items, nitems, x, y);
1765 if (tPtr->docHeight != y+10) {
1766 tPtr->docHeight = y+10;
1767 updateScrollers(tPtr);
1770 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1771 XEvent event;
1773 tPtr->flags.horizOnDemand = True;
1774 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1775 event.type = Expose;
1776 handleEvents(&event, (void *)tPtr);
1778 } else if(tPtr->docWidth <= tPtr->visible.w
1779 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1780 tPtr->flags.horizOnDemand = False;
1781 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1784 tPtr->flags.laidOut = True;
1786 if(items && itemsSize > 0)
1787 wfree(items);
1792 static void
1793 textDidResize(W_ViewDelegate *self, WMView *view)
1795 Text *tPtr = (Text *)view->self;
1796 unsigned short w = tPtr->view->size.width;
1797 unsigned short h = tPtr->view->size.height;
1798 unsigned short rh = 0, vw = 0, rel;
1800 rel = (tPtr->flags.relief == WRFlat);
1802 if (tPtr->ruler && tPtr->flags.rulerShown) {
1803 WMMoveWidget(tPtr->ruler, 2, 2);
1804 WMResizeWidget(tPtr->ruler, w - 4, 40);
1805 rh = 40;
1808 if (tPtr->vS) {
1809 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1810 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1811 vw = 20;
1812 WMSetRulerOffset(tPtr->ruler,22);
1813 } else WMSetRulerOffset(tPtr->ruler, 2);
1815 if (tPtr->hS) {
1816 if (tPtr->vS) {
1817 WMMoveWidget(tPtr->hS, vw, h - 21);
1818 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1819 } else {
1820 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1821 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1825 tPtr->visible.x = (tPtr->vS)?24:4;
1826 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1827 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1828 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1829 tPtr->visible.h -= (tPtr->hS)?20:0;
1830 tPtr->margins[0].right = tPtr->visible.w;
1832 if (tPtr->view->flags.realized) {
1834 if (tPtr->db) {
1835 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1836 tPtr->db = (Pixmap) NULL;
1839 if (tPtr->visible.w < 40)
1840 tPtr->visible.w = 40;
1841 if (tPtr->visible.h < 20)
1842 tPtr->visible.h = 20;
1844 if(!tPtr->db) {
1845 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1846 tPtr->view->window, tPtr->visible.w,
1847 tPtr->visible.h, tPtr->view->screen->depth);
1851 WMThawText(tPtr);
1854 W_ViewDelegate _TextViewDelegate =
1856 NULL,
1857 NULL,
1858 textDidResize,
1859 NULL,
1862 #define TEXT_BUFFER_INCR 8
1863 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1865 static void
1866 clearText(Text *tPtr)
1868 tPtr->vpos = tPtr->hpos = 0;
1869 tPtr->docHeight = tPtr->docWidth = 0;
1870 tPtr->cursor.x = -23;
1872 if (!tPtr->firstTextBlock)
1873 return;
1875 while (tPtr->currentTextBlock)
1876 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1878 tPtr->firstTextBlock = NULL;
1879 tPtr->currentTextBlock = NULL;
1880 tPtr->lastTextBlock = NULL;
1881 WMEmptyArray(tPtr->gfxItems);
1884 /* possibly remove a single character from the currentTextBlock,
1885 or if there's a selection, remove it...
1886 note that Delete and Backspace are treated differently */
1887 static void
1888 deleteTextInteractively(Text *tPtr, KeySym ksym)
1890 TextBlock *tb;
1891 Bool back = (Bool) (ksym == XK_BackSpace);
1892 Bool done = 1, wasFirst = 0;
1894 if (!tPtr->flags.editable)
1895 return;
1897 if ( !(tb = tPtr->currentTextBlock) )
1898 return;
1900 if (tPtr->flags.ownsSelection) {
1901 if(removeSelection(tPtr))
1902 layOutDocument(tPtr);
1903 return;
1906 wasFirst = tb->first;
1907 if (back && tPtr->tpos < 1) {
1908 if (tb->prior) {
1909 if(tb->prior->blank) {
1910 tPtr->currentTextBlock = tb->prior;
1911 WMRemoveTextBlock(tPtr);
1912 tPtr->currentTextBlock = tb;
1913 tb->first = True;
1914 layOutDocument(tPtr);
1915 return;
1916 } else {
1917 if(tb->blank) {
1918 TextBlock *prior = tb->prior;
1919 tPtr->currentTextBlock = tb;
1920 WMRemoveTextBlock(tPtr);
1921 tb = prior;
1922 } else {
1923 tb = tb->prior;
1926 if(tb->graphic)
1927 tPtr->tpos = 1;
1928 else
1929 tPtr->tpos = tb->used;
1931 tPtr->currentTextBlock = tb;
1932 done = 1;
1933 if(wasFirst) {
1934 if(tb->next)
1935 tb->next->first = False;
1936 layOutDocument(tPtr);
1937 return;
1943 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1944 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1945 if (back)
1946 tPtr->tpos--;
1947 memmove(&(tb->text[tPtr->tpos]),
1948 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1949 tb->used--;
1950 done = 0;
1953 /* if there are no characters left to back over in the textblock,
1954 but it still has characters to the right of the cursor: */
1955 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1956 || tb->graphic) {
1958 /* no more chars, and it's marked as blank? */
1959 if(tb->blank) {
1960 TextBlock *sibling = (back? tb->prior : tb->next);
1962 if(tb->used == 0 || tb->graphic)
1963 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1965 if (sibling) {
1966 tPtr->currentTextBlock = sibling;
1967 if(tb->graphic)
1968 tPtr->tpos = (back? 1 : 0);
1969 else
1970 tPtr->tpos = (back? sibling->used : 0);
1972 /* no more chars, so mark it as blank */
1973 } else if(tb->used == 0) {
1974 tb->blank = 1;
1975 } else if(tb->graphic) {
1976 Bool hasNext = (Bool)(tb->next);
1978 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1979 if(hasNext) {
1980 tPtr->tpos = 0;
1981 } else if(tPtr->currentTextBlock) {
1982 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1983 1 : tPtr->currentTextBlock->used);
1985 } else printf("DEBUG: unaccounted for... catch this!\n");
1988 layOutDocument(tPtr);
1992 static void
1993 insertTextInteractively(Text *tPtr, char *text, int len)
1995 TextBlock *tb;
1996 char *newline = NULL;
1998 if (!tPtr->flags.editable) {
1999 return;
2002 if (len < 1 || !text)
2003 return;
2006 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
2007 return;
2010 if (tPtr->flags.ownsSelection)
2011 removeSelection(tPtr);
2014 if (tPtr->flags.ignoreNewLine) {
2015 int i;
2016 for(i=0; i<len; i++) {
2017 if (text[i] == '\n')
2018 text[i] = ' ';
2022 tb = tPtr->currentTextBlock;
2023 if (!tb || tb->graphic) {
2024 tPtr->tpos = 0;
2025 WMAppendTextStream(tPtr, text);
2026 layOutDocument(tPtr);
2027 return;
2030 if ((newline = strchr(text, '\n'))) {
2031 int nlen = (int)(newline-text);
2032 int s = tb->used - tPtr->tpos;
2034 if (!tb->blank && nlen>0) {
2035 char *save;
2037 if (s > 0) {
2038 save = wmalloc(s);
2039 memcpy(save, &tb->text[tPtr->tpos], s);
2040 tb->used -= (tb->used - tPtr->tpos);
2042 insertTextInteractively(tPtr, text, nlen);
2043 newline++;
2044 WMAppendTextStream(tPtr, newline);
2045 if (s>0) {
2046 insertTextInteractively(tPtr, save, s);
2047 wfree(save);
2049 } else {
2050 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2051 && !tb->graphic && tb->text) {
2053 unsigned short savePos = tPtr->tpos;
2054 void *ntb = WMCreateTextBlockWithText(
2055 tPtr, &tb->text[tPtr->tpos],
2056 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2058 if(tb->sections[0].end == tPtr->tpos)
2059 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2060 NULL, tb->d.font, tb->color, True, 0));
2062 tb->used = savePos;
2063 WMAppendTextBlock(tPtr, ntb);
2064 tPtr->tpos = 0;
2066 } else if (tPtr->tpos == tb->used) {
2067 if(tPtr->flags.indentNewLine) {
2068 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2069 " ", tb->d.font, tb->color, True, 4));
2070 tPtr->tpos = 4;
2071 } else {
2072 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2073 NULL, tb->d.font, tb->color, True, 0));
2074 tPtr->tpos = 0;
2076 } else if (tPtr->tpos == 0) {
2077 if(tPtr->flags.indentNewLine) {
2078 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2079 " ", tb->d.font, tb->color, True, 4));
2080 } else {
2081 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2082 NULL, tb->d.font, tb->color, True, 0));
2084 tPtr->tpos = 0;
2085 if(tPtr->currentTextBlock->next)
2086 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2089 } else {
2090 if (tb->used + len >= tb->allocated) {
2091 tb->allocated = reqBlockSize(tb->used+len);
2092 tb->text = wrealloc(tb->text, tb->allocated);
2095 if (tb->blank) {
2096 memcpy(tb->text, text, len);
2097 tb->used = len;
2098 tPtr->tpos = len;
2099 tb->text[tb->used] = 0;
2100 tb->blank = False;
2102 } else {
2103 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2104 tb->used-tPtr->tpos+1);
2105 memmove(&tb->text[tPtr->tpos], text, len);
2106 tb->used += len;
2107 tPtr->tpos += len;
2108 tb->text[tb->used] = 0;
2113 layOutDocument(tPtr);
2117 static void
2118 selectRegion(Text *tPtr, int x, int y)
2121 if (x < 0 || y < 0)
2122 return;
2124 y += (tPtr->flags.rulerShown? 40: 0);
2125 y += tPtr->vpos;
2126 if (y>10)
2127 y -= 10; /* the original offset */
2129 x -= tPtr->visible.x-2;
2130 if (x<0)
2131 x=0;
2133 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2134 tPtr->sel.w = abs(tPtr->clicked.x - x);
2135 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2136 tPtr->sel.h = abs(tPtr->clicked.y - y);
2138 tPtr->flags.ownsSelection = True;
2139 paintText(tPtr);
2143 static void
2144 releaseSelection(Text *tPtr)
2146 TextBlock *tb = tPtr->firstTextBlock;
2148 while(tb) {
2149 tb->selected = False;
2150 tb = tb->next;
2152 tPtr->flags.ownsSelection = False;
2153 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2154 CurrentTime);
2156 paintText(tPtr);
2160 WMData*
2161 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2162 Atom *type)
2164 Text *tPtr = view->self;
2165 Display *dpy = tPtr->view->screen->display;
2166 Atom _TARGETS;
2167 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2168 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2169 WMData *data = NULL;
2172 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2173 char *text = WMGetTextSelectedStream(tPtr);
2175 if (text) {
2176 data = WMCreateDataWithBytes(text, strlen(text));
2177 WMSetDataFormat(data, TYPETEXT);
2179 *type = target;
2180 return data;
2181 } else printf("didn't get it\n");
2183 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2184 if (target == _TARGETS) {
2185 Atom *ptr;
2187 ptr = wmalloc(4 * sizeof(Atom));
2188 ptr[0] = _TARGETS;
2189 ptr[1] = XA_STRING;
2190 ptr[2] = TEXT;
2191 ptr[3] = COMPOUND_TEXT;
2193 data = WMCreateDataWithBytes(ptr, 4*4);
2194 WMSetDataFormat(data, 32);
2196 *type = target;
2197 return data;
2200 return NULL;
2204 static void
2205 lostHandler(WMView *view, Atom selection, void *cdata)
2207 releaseSelection((WMText *)view->self);
2211 static WMSelectionProcs selectionHandler = {
2212 requestHandler, lostHandler, NULL
2216 static void
2217 ownershipObserver(void *observerData, WMNotification *notification)
2219 if (observerData != WMGetNotificationClientData(notification))
2220 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2224 static void
2225 autoSelectText(Text *tPtr, int clicks)
2227 int x, start;
2228 TextBlock *tb;
2229 char *mark = NULL, behind, ahead;
2231 if(!(tb = tPtr->currentTextBlock))
2232 return;
2234 if(clicks == 2) {
2237 switch(tb->text[tPtr->tpos]) {
2238 case ' ': return;
2240 case '<': case '>': behind = '<'; ahead = '>'; break;
2241 case '{': case '}': behind = '{'; ahead = '}'; break;
2242 case '[': case ']': behind = '['; ahead = ']'; break;
2244 default: behind = ahead = ' ';
2247 tPtr->sel.y = tPtr->cursor.y+5;
2248 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2250 if(tb->graphic) {
2251 tPtr->sel.x = tb->sections[0].x;
2252 tPtr->sel.w = tb->sections[0].w;
2253 } else {
2254 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2256 start = tPtr->tpos;
2257 while(start > 0 && tb->text[start-1] != behind)
2258 start--;
2260 x = tPtr->cursor.x;
2261 if(tPtr->tpos > start){
2262 x -= WMWidthOfString(font, &tb->text[start],
2263 tPtr->tpos - start);
2265 tPtr->sel.x = (x<0?0:x)+1;
2267 if((mark = strchr(&tb->text[start], ahead))) {
2268 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2269 (int)(mark - &tb->text[start]));
2270 } else if(tb->used > start) {
2271 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2272 tb->used - start);
2276 } else if(clicks == 3) {
2277 TextBlock *cur = tb;
2279 while(tb && !tb->first) {
2280 tb = tb->prior;
2282 tPtr->sel.y = tb->sections[0]._y;
2284 tb = cur;
2285 while(tb->next && !tb->next->first) {
2286 tb = tb->next;
2288 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2289 + 5 - tPtr->sel.y;
2291 tPtr->sel.x = 0;
2292 tPtr->sel.w = tPtr->docWidth;
2293 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2296 if (!tPtr->flags.ownsSelection) {
2297 WMCreateSelectionHandler(tPtr->view,
2298 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2299 tPtr->flags.ownsSelection = True;
2301 paintText(tPtr);
2306 static void
2307 fontChanged(void *observerData, WMNotification *notification)
2309 WMText *tPtr = (WMText *) observerData;
2310 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2311 printf("fontChanged\n");
2313 if(!tPtr || !font)
2314 return;
2316 if (tPtr->flags.ownsSelection)
2317 WMSetTextSelectionFont(tPtr, font);
2321 static void
2322 handleTextKeyPress(Text *tPtr, XEvent *event)
2324 char buffer[64];
2325 KeySym ksym;
2326 int control_pressed = False;
2327 TextBlock *tb = NULL;
2329 if (((XKeyEvent *) event)->state & ControlMask)
2330 control_pressed = True;
2331 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2333 switch(ksym) {
2335 case XK_Home:
2336 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2337 tPtr->tpos = 0;
2338 updateCursorPosition(tPtr);
2339 paintText(tPtr);
2340 break;
2342 case XK_End:
2343 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2344 if(tPtr->currentTextBlock->graphic)
2345 tPtr->tpos = 1;
2346 else
2347 tPtr->tpos = tPtr->currentTextBlock->used;
2349 updateCursorPosition(tPtr);
2350 paintText(tPtr);
2351 break;
2353 case XK_Left:
2354 if(!(tb = tPtr->currentTextBlock))
2355 break;
2356 if(tb->graphic)
2357 goto L_imaGFX;
2359 if(tPtr->tpos==0) {
2360 L_imaGFX: if(tb->prior) {
2361 tPtr->currentTextBlock = tb->prior;
2362 if(tPtr->currentTextBlock->graphic)
2363 tPtr->tpos = 1;
2364 else
2365 tPtr->tpos = tPtr->currentTextBlock->used;
2367 if(!tb->first && tPtr->tpos > 0)
2368 tPtr->tpos--;
2369 } else tPtr->tpos = 0;
2370 } else tPtr->tpos--;
2371 updateCursorPosition(tPtr);
2372 paintText(tPtr);
2373 break;
2375 case XK_Right:
2376 if(!(tb = tPtr->currentTextBlock))
2377 break;
2378 if(tb->graphic)
2379 goto R_imaGFX;
2380 if(tPtr->tpos == tb->used) {
2381 R_imaGFX: if(tb->next) {
2382 tPtr->currentTextBlock = tb->next;
2383 tPtr->tpos = 0;
2384 if(!tb->next->first && tb->next->used>0)
2385 tPtr->tpos++;
2386 } else {
2387 if(tb->graphic)
2388 tPtr->tpos = 1;
2389 else
2390 tPtr->tpos = tb->used;
2392 } else tPtr->tpos++;
2393 updateCursorPosition(tPtr);
2394 paintText(tPtr);
2395 break;
2397 case XK_Down:
2398 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2399 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2400 paintText(tPtr);
2401 break;
2403 case XK_Up:
2404 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2405 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2406 paintText(tPtr);
2407 break;
2409 case XK_BackSpace:
2410 case XK_Delete:
2411 #ifdef XK_KP_Delete
2412 case XK_KP_Delete:
2413 #endif
2414 deleteTextInteractively(tPtr, ksym);
2415 updateCursorPosition(tPtr);
2416 paintText(tPtr);
2417 break;
2419 case XK_Control_R :
2420 case XK_Control_L :
2421 control_pressed = True;
2422 break;
2424 case XK_Tab:
2425 insertTextInteractively(tPtr, " ", 4);
2426 updateCursorPosition(tPtr);
2427 paintText(tPtr);
2428 break;
2430 case XK_Return:
2431 *buffer = '\n';
2432 default:
2433 if (*buffer != 0 && !control_pressed) {
2434 insertTextInteractively(tPtr, buffer, strlen(buffer));
2435 updateCursorPosition(tPtr);
2436 paintText(tPtr);
2438 } else if (control_pressed && ksym==XK_r) {
2439 Bool i = !tPtr->flags.rulerShown;
2440 WMShowTextRuler(tPtr, i);
2441 tPtr->flags.rulerShown = i;
2443 else if (control_pressed && *buffer == '\a')
2444 XBell(tPtr->view->screen->display, 0);
2445 else
2446 WMRelayToNextResponder(tPtr->view, event);
2449 if (!control_pressed && tPtr->flags.ownsSelection)
2450 releaseSelection(tPtr);
2454 static void
2455 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2456 void *cdata, WMData *data)
2458 Text *tPtr = (Text *)view->self;
2459 char *text;
2461 tPtr->flags.waitingForSelection = 0;
2463 if (data) {
2464 text = (char*)WMDataBytes(data);
2466 if (tPtr->parser) {
2467 (tPtr->parser) (tPtr, (void *) text);
2468 layOutDocument(tPtr);
2469 } else insertTextInteractively(tPtr, text, strlen(text));
2470 updateCursorPosition(tPtr);
2471 paintText(tPtr);
2473 } else {
2474 int n;
2476 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2478 if (text) {
2479 text[n] = 0;
2480 if (tPtr->parser) {
2481 (tPtr->parser) (tPtr, (void *) text);
2482 layOutDocument(tPtr);
2483 } else insertTextInteractively(tPtr, text, n);
2484 updateCursorPosition(tPtr);
2485 paintText(tPtr);
2487 XFree(text);
2495 static void
2496 handleActionEvents(XEvent *event, void *data)
2498 Text *tPtr = (Text *)data;
2499 Display *dpy = event->xany.display;
2500 KeySym ksym;
2503 switch (event->type) {
2504 case KeyPress:
2505 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2506 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2507 tPtr->flags.extendSelection = True;
2508 return;
2511 if (tPtr->flags.focused) {
2512 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2513 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2514 GrabModeAsync, GrabModeAsync, None,
2515 tPtr->view->screen->invisibleCursor, CurrentTime);
2516 tPtr->flags.pointerGrabbed = True;
2517 handleTextKeyPress(tPtr, event);
2519 } break;
2521 case KeyRelease:
2522 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2523 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2524 tPtr->flags.extendSelection = False;
2525 return;
2526 /* end modify flag so selection can be extended */
2528 break;
2531 case MotionNotify:
2533 if (tPtr->flags.pointerGrabbed) {
2534 tPtr->flags.pointerGrabbed = False;
2535 XUngrabPointer(dpy, CurrentTime);
2538 if(tPtr->flags.waitingForSelection)
2539 break;
2541 if ((event->xmotion.state & Button1Mask)) {
2542 TextBlock *tb = tPtr->currentTextBlock;
2544 if(tb && tPtr->flags.isOverGraphic &&
2545 tb->graphic && !tb->object) {
2546 WMSize offs;
2547 WMPixmap *pixmap = tb->d.pixmap;
2548 char *types[2] = {"application/X-image", NULL};
2550 offs.width = 2;
2551 offs.height = 2;
2553 WMDragImageFromView(tPtr->view, pixmap, types,
2554 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2555 offs, event, True);
2558 } else {
2559 if (!tPtr->flags.ownsSelection) {
2560 WMCreateSelectionHandler(tPtr->view,
2561 XA_PRIMARY, event->xbutton.time,
2562 &selectionHandler, NULL);
2563 tPtr->flags.ownsSelection = True;
2566 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2567 break;
2570 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2571 break;
2574 case ButtonPress:
2576 if (tPtr->flags.pointerGrabbed) {
2577 tPtr->flags.pointerGrabbed = False;
2578 XUngrabPointer(dpy, CurrentTime);
2579 break;
2582 if (tPtr->flags.waitingForSelection)
2583 break;
2585 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2586 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2587 return;
2590 if (tPtr->flags.ownsSelection)
2591 releaseSelection(tPtr);
2594 if (event->xbutton.button == Button1) {
2596 if(WMIsDoubleClick(event)) {
2597 TextBlock *tb = tPtr->currentTextBlock;
2599 tPtr->lastClickTime = event->xbutton.time;
2600 if(tb && tb->graphic && !tb->object) {
2601 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2602 char *desc;
2604 desc = wmalloc(tb->used+1);
2605 memcpy(desc, tb->text, tb->used);
2606 desc[tb->used] = 0;
2607 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2608 wfree(desc);
2610 } else {
2611 autoSelectText(tPtr, 2);
2613 break;
2614 } else if(event->xbutton.time - tPtr->lastClickTime
2615 < WINGsConfiguration.doubleClickDelay) {
2616 tPtr->lastClickTime = event->xbutton.time;
2617 autoSelectText(tPtr, 3);
2618 break;
2621 if (!tPtr->flags.focused) {
2622 WMSetFocusToWidget(tPtr);
2623 tPtr->flags.focused = True;
2626 tPtr->lastClickTime = event->xbutton.time;
2627 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2628 paintText(tPtr);
2631 if (event->xbutton.button
2632 == WINGsConfiguration.mouseWheelDown) {
2633 WMScrollText(tPtr, 16);
2634 break;
2637 if (event->xbutton.button
2638 == WINGsConfiguration.mouseWheelUp) {
2639 WMScrollText(tPtr, -16);
2640 break;
2643 if (event->xbutton.button == Button2) {
2644 char *text = NULL;
2645 int n;
2647 if (!tPtr->flags.editable) {
2648 XBell(dpy, 0);
2649 break;
2652 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2653 event->xbutton.time, pasteText, NULL)) {
2655 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2656 tPtr->flags.waitingForSelection = 0;
2658 if (text) {
2659 text[n] = 0;
2661 if (tPtr->parser) {
2662 (tPtr->parser) (tPtr, (void *) text);
2663 layOutDocument(tPtr);
2665 else
2666 insertTextInteractively(tPtr, text, n);
2668 XFree(text);
2669 #if 0
2670 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2671 (void*)WMInsertTextEvent);
2672 #endif
2673 updateCursorPosition(tPtr);
2674 paintText(tPtr);
2676 } else {
2677 tPtr->flags.waitingForSelection = True;
2680 break;
2684 case ButtonRelease:
2685 if (tPtr->flags.pointerGrabbed) {
2686 tPtr->flags.pointerGrabbed = False;
2687 XUngrabPointer(dpy, CurrentTime);
2688 break;
2691 if (tPtr->flags.waitingForSelection)
2692 break;
2698 static void
2699 handleEvents(XEvent *event, void *data)
2701 Text *tPtr = (Text *)data;
2703 switch(event->type) {
2704 case Expose:
2706 if (event->xexpose.count!=0)
2707 break;
2709 if(tPtr->hS) {
2710 if (!(W_VIEW(tPtr->hS))->flags.realized)
2711 WMRealizeWidget(tPtr->hS);
2714 if(tPtr->vS) {
2715 if (!(W_VIEW(tPtr->vS))->flags.realized)
2716 WMRealizeWidget(tPtr->vS);
2719 if(tPtr->ruler) {
2720 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2721 WMRealizeWidget(tPtr->ruler);
2725 if(!tPtr->db)
2726 textDidResize(tPtr->view->delegate, tPtr->view);
2728 paintText(tPtr);
2729 break;
2731 case FocusIn:
2732 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2733 != tPtr->view)
2734 return;
2735 tPtr->flags.focused = True;
2736 #if DO_BLINK
2737 if (tPtr->flags.editable && !tPtr->timerID) {
2738 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2739 blinkCursor, tPtr);
2741 #endif
2743 break;
2745 case FocusOut:
2746 tPtr->flags.focused = False;
2747 paintText(tPtr);
2748 #if DO_BLINK
2749 if (tPtr->timerID) {
2750 WMDeleteTimerHandler(tPtr->timerID);
2751 tPtr->timerID = NULL;
2753 #endif
2754 break;
2757 case DestroyNotify:
2758 clearText(tPtr);
2759 if(tPtr->db)
2760 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2761 if(tPtr->gfxItems)
2762 WMEmptyArray(tPtr->gfxItems);
2763 #if DO_BLINK
2764 if (tPtr->timerID)
2765 WMDeleteTimerHandler(tPtr->timerID);
2766 #endif
2767 WMReleaseFont(tPtr->dFont);
2768 WMReleaseColor(tPtr->dColor);
2769 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2770 WMRemoveNotificationObserver(tPtr);
2772 wfree(tPtr);
2774 break;
2780 static void
2781 insertPlainText(Text *tPtr, char *text)
2783 char *start, *mark;
2784 void *tb = NULL;
2786 start = text;
2787 while (start) {
2788 mark = strchr(start, '\n');
2789 if (mark) {
2790 tb = WMCreateTextBlockWithText(tPtr,
2791 start, tPtr->dFont,
2792 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2793 start = mark+1;
2794 tPtr->flags.first = True;
2795 } else {
2796 if (start && strlen(start)) {
2797 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2798 tPtr->dColor, tPtr->flags.first, strlen(start));
2799 } else tb = NULL;
2800 tPtr->flags.first = False;
2801 start = mark;
2804 if (tPtr->flags.prepend)
2805 WMPrependTextBlock(tPtr, tb);
2806 else
2807 WMAppendTextBlock(tPtr, tb);
2812 static void
2813 rulerMoveCallBack(WMWidget *w, void *self)
2815 Text *tPtr = (Text *)self;
2817 if (!tPtr)
2818 return;
2819 if (W_CLASS(tPtr) != WC_Text)
2820 return;
2822 paintText(tPtr);
2826 static void
2827 rulerReleaseCallBack(WMWidget *w, void *self)
2829 Text *tPtr = (Text *)self;
2831 if (!tPtr)
2832 return;
2833 if (W_CLASS(tPtr) != WC_Text)
2834 return;
2836 WMThawText(tPtr);
2837 return;
2840 static unsigned
2841 draggingSourceOperation(WMView *self, Bool local)
2843 return WDOperationCopy;
2846 static WMData*
2847 fetchDragData(WMView *self, char *type)
2849 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2850 char *desc;
2851 WMData *data;
2853 if (!tb)
2854 return NULL;
2856 printf("type is [%s]\n", type);
2857 desc = wmalloc(tb->used+1);
2858 memcpy(desc, tb->text, tb->used);
2859 desc[tb->used] = 0;
2860 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2862 wfree(desc);
2864 return data;
2868 static WMDragSourceProcs _DragSourceProcs = {
2869 draggingSourceOperation,
2870 NULL,
2871 NULL,
2872 fetchDragData
2876 static unsigned
2877 draggingEntered(WMView *self, WMDraggingInfo *info)
2879 printf("draggingEntered\n");
2880 return WDOperationCopy;
2884 static unsigned
2885 draggingUpdated(WMView *self, WMDraggingInfo *info)
2887 return WDOperationCopy;
2891 static void
2892 draggingExited(WMView *self, WMDraggingInfo *info)
2894 printf("draggingExited\n");
2897 static Bool
2898 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2900 printf("prepareForDragOperation\n");
2901 return True;
2905 char *badbadbad;
2907 static void
2908 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2909 void *cdata, WMData *data)
2911 badbadbad = wstrdup((char *)WMDataBytes(data));
2915 /* when it's done in WINGs, remove this */
2917 Bool
2918 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2920 WMScreen *scr = W_VIEW_SCREEN(view);
2922 if (!WMRequestSelection(scr->dragInfo.destView,
2923 scr->xdndSelectionAtom,
2924 XInternAtom(scr->display, type, False),
2925 scr->dragInfo.timestamp,
2926 receivedData, &scr->dragInfo)) {
2927 wwarning("could not request data for dropped data");
2931 XEvent ev;
2933 ev.type = ClientMessage;
2934 ev.xclient.message_type = scr->xdndFinishedAtom;
2935 ev.xclient.format = 32;
2936 ev.xclient.window = info->destinationWindow;
2937 ev.xclient.data.l[0] = 0;
2938 ev.xclient.data.l[1] = 0;
2939 ev.xclient.data.l[2] = 0;
2940 ev.xclient.data.l[3] = 0;
2941 ev.xclient.data.l[4] = 0;
2943 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2944 XFlush(scr->display);
2946 return True;
2949 static Bool
2950 performDragOperation(WMView *self, WMDraggingInfo *info)
2952 WMColor *color;
2953 WMText *tPtr = (WMText *)self->self;
2955 if (!tPtr)
2956 return True;
2958 requestDroppedData(tPtr->view, info, "application/X-color");
2959 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2960 if(color) {
2961 WMSetTextSelectionColor(tPtr, color);
2962 WMReleaseColor(color);
2967 return True;
2970 static void
2971 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2973 printf("concludeDragOperation\n");
2977 static WMDragDestinationProcs _DragDestinationProcs = {
2978 draggingEntered,
2979 draggingUpdated,
2980 draggingExited,
2981 prepareForDragOperation,
2982 performDragOperation,
2983 concludeDragOperation
2987 char *
2988 getStream(WMText *tPtr, int sel, int array)
2990 TextBlock *tb = NULL;
2991 char *text = NULL;
2992 unsigned long where = 0;
2994 if (!tPtr)
2995 return NULL;
2997 if (!(tb = tPtr->firstTextBlock))
2998 return NULL;
3000 if (tPtr->writer) {
3001 (tPtr->writer) (tPtr, (void *) text);
3002 return text;
3005 tb = tPtr->firstTextBlock;
3006 while (tb) {
3008 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
3010 if (!sel || (tb->graphic && tb->selected)) {
3012 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3013 && tb != tPtr->firstTextBlock) {
3014 text = wrealloc(text, where+1);
3015 text[where++] = '\n';
3018 if(tb->blank)
3019 goto _gSnext;
3021 if(tb->graphic && array) {
3022 text = wrealloc(text, where+4);
3023 text[where++] = 0xFA;
3024 text[where++] = (tb->used>>8)&0x0ff;
3025 text[where++] = tb->used&0x0ff;
3026 text[where++] = tb->allocated; /* extra info */
3028 text = wrealloc(text, where+tb->used);
3029 memcpy(&text[where], tb->text, tb->used);
3030 where += tb->used;
3033 } else if (sel && tb->selected) {
3035 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3036 text = wrealloc(text, where+1);
3037 text[where++] = '\n';
3040 if(tb->blank)
3041 goto _gSnext;
3043 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3044 memcpy(&text[where], &tb->text[tb->s_begin],
3045 tb->s_end - tb->s_begin);
3046 where += tb->s_end - tb->s_begin;
3051 _gSnext:tb = tb->next;
3054 /* +1 for the end of string, let's be nice */
3055 text = wrealloc(text, where+1);
3056 text[where] = 0;
3057 return text;
3061 static void
3062 releaseStreamObjects(void *data)
3064 if(data)
3065 wfree(data);
3068 WMArray *
3069 getStreamObjects(WMText *tPtr, int sel)
3071 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3072 WMData *data;
3073 char *stream;
3074 unsigned short len;
3075 char *start, *fa, *desc;
3077 stream = getStream(tPtr, sel, 1);
3078 if(!stream)
3079 return NULL;
3081 start = stream;
3082 while (start) {
3084 fa = strchr(start, 0xFA);
3085 if (fa) {
3086 if((int)(fa - start)>0) {
3087 desc = start;
3088 desc[(int)(fa - start)] = 0;
3089 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3090 WMSetDataFormat(data, TYPETEXT);
3091 WMAddToArray(array, (void *) data);
3094 len = *(fa+1)*0xff + *(fa+2);
3095 data = WMCreateDataWithBytes((void *)(fa+4), len);
3096 WMSetDataFormat(data, *(fa+3));
3097 WMAddToArray(array, (void *) data);
3098 start = fa + len + 4;
3100 } else {
3101 if (start && strlen(start)) {
3102 data = WMCreateDataWithBytes((void *)start, strlen(start));
3103 WMSetDataFormat(data, TYPETEXT);
3104 WMAddToArray(array, (void *) data);
3106 start = fa;
3110 wfree(stream);
3111 return array;
3115 WMText *
3116 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3118 Text *tPtr;
3119 Display *dpy;
3120 WMScreen *scr;
3121 XGCValues gcv;
3123 tPtr = wmalloc(sizeof(Text));
3124 memset(tPtr, 0, sizeof(Text));
3125 tPtr->widgetClass = WC_Text;
3126 tPtr->view = W_CreateView(W_VIEW(parent));
3127 if (!tPtr->view) {
3128 perror("could not create text's view\n");
3129 wfree(tPtr);
3130 return NULL;
3133 dpy = tPtr->view->screen->display;
3134 scr = tPtr->view->screen;
3136 tPtr->view->self = tPtr;
3137 tPtr->view->attribs.cursor = scr->textCursor;
3138 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3139 W_ResizeView(tPtr->view, 250, 200);
3141 tPtr->dColor = WMWhiteColor(scr);
3142 tPtr->bgGC = WMColorGC(tPtr->dColor);
3143 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3144 WMReleaseColor(tPtr->dColor);
3146 tPtr->dColor = WMBlackColor(scr);
3147 tPtr->fgGC = WMColorGC(tPtr->dColor);
3149 gcv.graphics_exposures = False;
3150 gcv.foreground = W_PIXEL(scr->gray);
3151 gcv.background = W_PIXEL(scr->darkGray);
3152 gcv.fill_style = FillStippled;
3153 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr),
3154 STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
3155 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3156 GCForeground|GCBackground|GCStipple
3157 |GCFillStyle|GCGraphicsExposures, &gcv);
3159 tPtr->ruler = NULL;
3160 tPtr->vS = NULL;
3161 tPtr->hS = NULL;
3163 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(scr, 12));
3165 tPtr->view->delegate = &_TextViewDelegate;
3167 tPtr->delegate = NULL;
3169 #if DO_BLINK
3170 tPtr->timerID = NULL;
3171 #endif
3173 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3174 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3175 handleEvents, tPtr);
3177 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3178 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3179 handleActionEvents, tPtr);
3181 WMAddNotificationObserver(ownershipObserver, tPtr,
3182 WMSelectionOwnerDidChangeNotification,
3183 tPtr);
3185 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3186 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3190 char *types[3] = {"application/X-color", "application/X-image", NULL};
3191 WMRegisterViewForDraggedTypes(tPtr->view, types);
3194 /*WMAddNotificationObserver(fontChanged, tPtr,
3195 WMFontPanelDidChangeNotification, tPtr);*/
3197 tPtr->firstTextBlock = NULL;
3198 tPtr->lastTextBlock = NULL;
3199 tPtr->currentTextBlock = NULL;
3200 tPtr->tpos = 0;
3202 tPtr->gfxItems = WMCreateArray(4);
3204 tPtr->parser = parser;
3205 tPtr->writer = writer;
3207 tPtr->sel.x = tPtr->sel.y = 2;
3208 tPtr->sel.w = tPtr->sel.h = 0;
3210 tPtr->clicked.x = tPtr->clicked.y = 2;
3212 tPtr->visible.x = tPtr->visible.y = 2;
3213 tPtr->visible.h = tPtr->view->size.height;
3214 tPtr->visible.w = tPtr->view->size.width - 4;
3216 tPtr->cursor.x = -23;
3218 tPtr->docWidth = 0;
3219 tPtr->docHeight = 0;
3220 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3221 default_bullet);
3222 tPtr->db = (Pixmap) NULL;
3223 tPtr->bgPixmap = NULL;
3225 tPtr->margins = WMGetRulerMargins(NULL);
3226 tPtr->margins->right = tPtr->visible.w;
3227 tPtr->nMargins = 1;
3229 tPtr->flags.rulerShown = False;
3230 tPtr->flags.monoFont = False;
3231 tPtr->flags.focused = False;
3232 tPtr->flags.editable = True;
3233 tPtr->flags.ownsSelection = False;
3234 tPtr->flags.pointerGrabbed = False;
3235 tPtr->flags.extendSelection = False;
3236 tPtr->flags.frozen = False;
3237 tPtr->flags.cursorShown = True;
3238 tPtr->flags.acceptsGraphic = False;
3239 tPtr->flags.horizOnDemand = False;
3240 tPtr->flags.needsLayOut = False;
3241 tPtr->flags.ignoreNewLine = False;
3242 tPtr->flags.indentNewLine = False;
3243 tPtr->flags.laidOut = False;
3244 tPtr->flags.ownsSelection = False;
3245 tPtr->flags.waitingForSelection = False;
3246 tPtr->flags.prepend = False;
3247 tPtr->flags.isOverGraphic = False;
3248 tPtr->flags.relief = WRSunken;
3249 tPtr->flags.isOverGraphic = 0;
3250 tPtr->flags.alignment = WALeft;
3251 tPtr->flags.first = True;
3253 return tPtr;
3256 void
3257 WMPrependTextStream(WMText *tPtr, char *text)
3259 CHECK_CLASS(tPtr, WC_Text);
3261 if(!text) {
3262 if (tPtr->flags.ownsSelection)
3263 releaseSelection(tPtr);
3264 clearText(tPtr);
3265 updateScrollers(tPtr);
3266 return;
3269 tPtr->flags.prepend = True;
3270 if (text && tPtr->parser)
3271 (tPtr->parser) (tPtr, (void *) text);
3272 else
3273 insertPlainText(tPtr, text);
3275 tPtr->flags.needsLayOut = True;
3276 tPtr->tpos = 0;
3277 if(!tPtr->flags.frozen) {
3278 layOutDocument(tPtr);
3283 void
3284 WMAppendTextStream(WMText *tPtr, char *text)
3286 CHECK_CLASS(tPtr, WC_Text);
3288 if(!text) {
3289 if (tPtr->flags.ownsSelection)
3290 releaseSelection(tPtr);
3291 clearText(tPtr);
3292 updateScrollers(tPtr);
3293 return;
3296 tPtr->flags.prepend = False;
3297 if (text && tPtr->parser)
3298 (tPtr->parser) (tPtr, (void *) text);
3299 else
3300 insertPlainText(tPtr, text);
3302 tPtr->flags.needsLayOut = True;
3303 if(tPtr->currentTextBlock) {
3304 if(tPtr->currentTextBlock->graphic)
3305 tPtr->tpos = 1;
3306 else
3307 tPtr->tpos = tPtr->currentTextBlock->used;
3310 if(!tPtr->flags.frozen) {
3311 layOutDocument(tPtr);
3316 char *
3317 WMGetTextStream(WMText *tPtr)
3319 CHECK_CLASS(tPtr, WC_Text);
3320 return getStream(tPtr, 0, 0);
3323 char *
3324 WMGetTextSelectedStream(WMText *tPtr)
3326 CHECK_CLASS(tPtr, WC_Text);
3327 return getStream(tPtr, 1, 0);
3330 WMArray *
3331 WMGetTextObjects(WMText *tPtr)
3333 CHECK_CLASS(tPtr, WC_Text);
3334 return getStreamObjects(tPtr, 0);
3337 WMArray *
3338 WMGetTextSelectedObjects(WMText *tPtr)
3340 CHECK_CLASS(tPtr, WC_Text);
3341 return getStreamObjects(tPtr, 1);
3345 void
3346 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3348 CHECK_CLASS(tPtr, WC_Text);
3350 tPtr->delegate = delegate;
3354 void *
3355 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3356 char *description, WMColor *color,
3357 unsigned short first, unsigned short extraInfo)
3359 TextBlock *tb;
3361 if (!w || !description || !color)
3362 return NULL;
3364 tb = wmalloc(sizeof(TextBlock));
3365 if (!tb)
3366 return NULL;
3368 tb->text = wstrdup(description);
3369 tb->used = strlen(description);
3370 tb->blank = False;
3371 tb->d.widget = w;
3372 tb->color = WMRetainColor(color);
3373 tb->marginN = newMargin(tPtr, NULL);
3374 tb->allocated = extraInfo;
3375 tb->first = first;
3376 tb->kanji = False;
3377 tb->graphic = True;
3378 tb->object = True;
3379 tb->underlined = False;
3380 tb->selected = False;
3381 tb->script = 0;
3382 tb->sections = NULL;
3383 tb->nsections = 0;
3384 tb->prior = NULL;
3385 tb->next = NULL;
3387 return tb;
3391 void *
3392 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3393 char *description, WMColor *color,
3394 unsigned short first, unsigned short extraInfo)
3396 TextBlock *tb;
3398 if (!p || !description || !color)
3399 return NULL;
3401 tb = wmalloc(sizeof(TextBlock));
3402 if (!tb)
3403 return NULL;
3405 tb->text = wstrdup(description);
3406 tb->used = strlen(description);
3407 tb->blank = False;
3408 tb->d.pixmap = WMRetainPixmap(p);
3409 tb->color = WMRetainColor(color);
3410 tb->marginN = newMargin(tPtr, NULL);
3411 tb->allocated = extraInfo;
3412 tb->first = first;
3413 tb->kanji = False;
3414 tb->graphic = True;
3415 tb->object = False;
3416 tb->underlined = False;
3417 tb->selected = False;
3418 tb->script = 0;
3419 tb->sections = NULL;
3420 tb->nsections = 0;
3421 tb->prior = NULL;
3422 tb->next = NULL;
3424 return tb;
3428 void*
3429 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3430 unsigned short first, unsigned short len)
3432 TextBlock *tb;
3434 if (!font || !color)
3435 return NULL;
3437 tb = wmalloc(sizeof(TextBlock));
3438 if (!tb)
3439 return NULL;
3441 tb->allocated = reqBlockSize(len);
3442 tb->text = (char *)wmalloc(tb->allocated);
3443 memset(tb->text, 0, tb->allocated);
3445 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3446 *tb->text = ' ';
3447 tb->used = 1;
3448 tb->blank = True;
3449 } else {
3450 memcpy(tb->text, text, len);
3451 tb->used = len;
3452 tb->blank = False;
3454 tb->text[tb->used] = 0;
3456 tb->d.font = WMRetainFont(font);
3457 tb->color = WMRetainColor(color);
3458 tb->marginN = newMargin(tPtr, NULL);
3459 tb->first = first;
3460 tb->kanji = False;
3461 tb->graphic = False;
3462 tb->underlined = False;
3463 tb->selected = False;
3464 tb->script = 0;
3465 tb->sections = NULL;
3466 tb->nsections = 0;
3467 tb->prior = NULL;
3468 tb->next = NULL;
3469 return tb;
3472 void
3473 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3474 unsigned int kanji, unsigned int underlined, int script,
3475 WMRulerMargins *margins)
3477 TextBlock *tb = (TextBlock *) vtb;
3478 if (!tb)
3479 return;
3481 tb->first = first;
3482 tb->kanji = kanji;
3483 tb->underlined = underlined;
3484 tb->script = script;
3485 tb->marginN = newMargin(tPtr, margins);
3488 void
3489 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3490 unsigned int *kanji, unsigned int *underlined, int *script,
3491 WMRulerMargins *margins)
3493 TextBlock *tb = (TextBlock *) vtb;
3494 if (!tb)
3495 return;
3497 if (first) *first = tb->first;
3498 if (kanji) *kanji = tb->kanji;
3499 if (underlined) *underlined = tb->underlined;
3500 if (script) *script = tb->script;
3501 if (margins) margins = &tPtr->margins[tb->marginN];
3506 void
3507 WMPrependTextBlock(WMText *tPtr, void *vtb)
3509 TextBlock *tb = (TextBlock *)vtb;
3511 if (!tPtr || !tb)
3512 return;
3514 if (tb->graphic) {
3515 if(tb->object) {
3516 WMWidget *w = tb->d.widget;
3517 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3518 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3519 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3522 WMAddToArray(tPtr->gfxItems, (void *)tb);
3523 tPtr->tpos = 1;
3525 } else {
3526 tPtr->tpos = tb->used;
3529 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3530 tb->next = tb->prior = NULL;
3531 tb->first = True;
3532 tPtr->lastTextBlock = tPtr->firstTextBlock
3533 = tPtr->currentTextBlock = tb;
3534 return;
3537 if(!tb->first) {
3538 tb->marginN = tPtr->currentTextBlock->marginN;
3541 tb->next = tPtr->currentTextBlock;
3542 tb->prior = tPtr->currentTextBlock->prior;
3543 if (tPtr->currentTextBlock->prior)
3544 tPtr->currentTextBlock->prior->next = tb;
3546 tPtr->currentTextBlock->prior = tb;
3547 if (!tb->prior)
3548 tPtr->firstTextBlock = tb;
3550 tPtr->currentTextBlock = tb;
3554 void
3555 WMAppendTextBlock(WMText *tPtr, void *vtb)
3557 TextBlock *tb = (TextBlock *)vtb;
3559 if (!tPtr || !tb)
3560 return;
3562 if (tb->graphic) {
3563 if(tb->object) {
3564 WMWidget *w = tb->d.widget;
3565 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3566 (W_VIEW(w))->attribs.cursor =
3567 tPtr->view->screen->defaultCursor;
3568 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3571 WMAddToArray(tPtr->gfxItems, (void *)tb);
3572 tPtr->tpos = 1;
3574 } else {
3575 tPtr->tpos = tb->used;
3579 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3580 tb->next = tb->prior = NULL;
3581 tb->first = True;
3582 tPtr->lastTextBlock = tPtr->firstTextBlock
3583 = tPtr->currentTextBlock = tb;
3584 return;
3587 if(!tb->first) {
3588 tb->marginN = tPtr->currentTextBlock->marginN;
3591 tb->next = tPtr->currentTextBlock->next;
3592 tb->prior = tPtr->currentTextBlock;
3593 if (tPtr->currentTextBlock->next)
3594 tPtr->currentTextBlock->next->prior = tb;
3596 tPtr->currentTextBlock->next = tb;
3598 if (!tb->next)
3599 tPtr->lastTextBlock = tb;
3601 tPtr->currentTextBlock = tb;
3605 void*
3606 WMRemoveTextBlock(WMText *tPtr)
3608 TextBlock *tb = NULL;
3610 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3611 || !tPtr->currentTextBlock) {
3612 return NULL;
3615 tb = tPtr->currentTextBlock;
3616 if (tb->graphic) {
3617 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3619 if(tb->object) {
3620 WMUnmapWidget(tb->d.widget);
3624 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3625 if (tPtr->currentTextBlock->next)
3626 tPtr->currentTextBlock->next->prior = NULL;
3628 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3629 tPtr->currentTextBlock = tPtr->firstTextBlock;
3631 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3632 tPtr->currentTextBlock->prior->next = NULL;
3633 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3634 tPtr->currentTextBlock = tPtr->lastTextBlock;
3635 } else {
3636 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3637 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3638 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3641 return (void *)tb;
3645 #if 0
3646 static void
3647 destroyWidget(WMWidget *widget)
3649 WMDestroyWidget(widget);
3650 // -- never do this -- wfree(widget);
3652 #endif
3655 void
3656 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3658 TextBlock *tb = (TextBlock *)vtb;
3659 if (!tPtr || !tb)
3660 return;
3662 if (tb->graphic) {
3663 if(tb->object) {
3664 /* naturally, there's a danger to destroying widgets whose action
3665 * brings us here: ie. press a button to destroy it...
3666 * need to find a safer way. till then... this stays commented out */
3667 /* 5 months later... destroy it 10 seconds after now which should
3668 * be enough time for the widget's action to be completed... :-) */
3669 /* This is a bad assumption. Just destroy the widget here.
3670 // if the caller needs it, it can protect it with W_RetainView()
3671 //WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3672 WMDestroyWidget(tb->d.widget);
3673 } else {
3674 WMReleasePixmap(tb->d.pixmap);
3676 } else {
3677 WMReleaseFont(tb->d.font);
3680 WMReleaseColor(tb->color);
3681 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3682 if (tb->sections)
3683 wfree(tb->sections);
3684 wfree(tb->text);
3685 wfree(tb);
3689 void
3690 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3692 if (!tPtr)
3693 return;
3695 if (color)
3696 tPtr->fgGC = WMColorGC(color);
3697 else {
3698 WMColor *color = WMBlackColor(tPtr->view->screen);
3699 tPtr->fgGC = WMColorGC(color);
3700 WMReleaseColor(color);
3703 paintText(tPtr);
3706 void
3707 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3709 if (!tPtr)
3710 return;
3712 if (color) {
3713 tPtr->bgGC = WMColorGC(color);
3714 W_SetViewBackgroundColor(tPtr->view, color);
3715 } else {
3716 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3717 W_SetViewBackgroundColor(tPtr->view,
3718 WMWhiteColor(tPtr->view->screen));
3721 paintText(tPtr);
3724 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3726 if (!tPtr)
3727 return;
3729 if (tPtr->bgPixmap)
3730 WMReleasePixmap(tPtr->bgPixmap);
3732 if (pixmap)
3733 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3734 else
3735 tPtr->bgPixmap = NULL;
3738 void
3739 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3741 if (!tPtr)
3742 return;
3743 tPtr->flags.relief = relief;
3744 textDidResize(tPtr->view->delegate, tPtr->view);
3747 void
3748 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3750 if (!tPtr)
3751 return;
3753 if (shouldhave && !tPtr->hS) {
3754 tPtr->hS = WMCreateScroller(tPtr);
3755 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3756 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3757 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3758 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3759 WMMapWidget(tPtr->hS);
3760 } else if (!shouldhave && tPtr->hS) {
3761 WMUnmapWidget(tPtr->hS);
3762 WMDestroyWidget(tPtr->hS);
3763 tPtr->hS = NULL;
3766 tPtr->hpos = 0;
3767 tPtr->prevHpos = 0;
3768 textDidResize(tPtr->view->delegate, tPtr->view);
3772 void
3773 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3775 if (!tPtr)
3776 return;
3778 if(shouldhave && !tPtr->ruler) {
3779 tPtr->ruler = WMCreateRuler(tPtr);
3780 (W_VIEW(tPtr->ruler))->attribs.cursor =
3781 tPtr->view->screen->defaultCursor;
3782 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3783 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3784 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3785 } else if(!shouldhave && tPtr->ruler) {
3786 WMShowTextRuler(tPtr, False);
3787 WMDestroyWidget(tPtr->ruler);
3788 tPtr->ruler = NULL;
3790 textDidResize(tPtr->view->delegate, tPtr->view);
3793 void
3794 WMShowTextRuler(WMText *tPtr, Bool show)
3796 if(!tPtr)
3797 return;
3798 if(!tPtr->ruler)
3799 return;
3801 if(tPtr->flags.monoFont)
3802 show = False;
3804 tPtr->flags.rulerShown = show;
3805 if(show) {
3806 WMMapWidget(tPtr->ruler);
3807 } else {
3808 WMUnmapWidget(tPtr->ruler);
3811 textDidResize(tPtr->view->delegate, tPtr->view);
3814 Bool
3815 WMGetTextRulerShown(WMText *tPtr)
3817 if(!tPtr)
3818 return 0;
3820 if(!tPtr->ruler)
3821 return 0;
3823 return tPtr->flags.rulerShown;
3827 void
3828 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3830 if (!tPtr)
3831 return;
3833 if (shouldhave && !tPtr->vS) {
3834 tPtr->vS = WMCreateScroller(tPtr);
3835 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3836 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3837 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3838 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3839 WMMapWidget(tPtr->vS);
3840 } else if (!shouldhave && tPtr->vS) {
3841 WMUnmapWidget(tPtr->vS);
3842 WMDestroyWidget(tPtr->vS);
3843 tPtr->vS = NULL;
3846 tPtr->vpos = 0;
3847 tPtr->prevVpos = 0;
3848 textDidResize(tPtr->view->delegate, tPtr->view);
3853 Bool
3854 WMScrollText(WMText *tPtr, int amount)
3856 Bool scroll=False;
3857 if (!tPtr)
3858 return False;
3859 if (amount == 0 || !tPtr->view->flags.realized)
3860 return False;
3862 if (amount < 0) {
3863 if (tPtr->vpos > 0) {
3864 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3865 else tPtr->vpos=0;
3866 scroll=True;
3868 } else {
3869 int limit = tPtr->docHeight - tPtr->visible.h;
3870 if (tPtr->vpos < limit) {
3871 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3872 else tPtr->vpos = limit;
3873 scroll = True;
3877 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3878 updateScrollers(tPtr);
3879 paintText(tPtr);
3881 tPtr->prevVpos = tPtr->vpos;
3882 return scroll;
3885 Bool
3886 WMPageText(WMText *tPtr, Bool direction)
3888 if (!tPtr) return False;
3889 if (!tPtr->view->flags.realized) return False;
3891 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3894 void
3895 WMSetTextEditable(WMText *tPtr, Bool editable)
3897 if (!tPtr)
3898 return;
3899 tPtr->flags.editable = editable;
3902 int
3903 WMGetTextEditable(WMText *tPtr)
3905 if (!tPtr)
3906 return 0;
3907 return tPtr->flags.editable;
3910 void
3911 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3913 if (!tPtr)
3914 return;
3915 tPtr->flags.indentNewLine = indent;
3918 void
3919 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3921 if (!tPtr)
3922 return;
3923 tPtr->flags.ignoreNewLine = ignore;
3926 Bool
3927 WMGetTextIgnoresNewline(WMText *tPtr)
3929 if (!tPtr)
3930 return True;
3931 return tPtr->flags.ignoreNewLine;
3934 void
3935 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3937 if (!tPtr)
3938 return;
3940 if (mono) {
3941 if(tPtr->flags.rulerShown)
3942 WMShowTextRuler(tPtr, False);
3943 if(tPtr->flags.alignment != WALeft)
3944 tPtr->flags.alignment = WALeft;
3947 tPtr->flags.monoFont = mono;
3948 textDidResize(tPtr->view->delegate, tPtr->view);
3951 Bool
3952 WMGetTextUsesMonoFont(WMText *tPtr)
3954 if (!tPtr)
3955 return True;
3956 return tPtr->flags.monoFont;
3960 void
3961 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3963 if (!tPtr)
3964 return;
3966 WMReleaseFont(tPtr->dFont);
3967 if (font)
3968 tPtr->dFont = WMRetainFont(font);
3969 else
3970 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3973 WMFont *
3974 WMGetTextDefaultFont(WMText *tPtr)
3976 if (!tPtr)
3977 return NULL;
3978 else
3979 return WMRetainFont(tPtr->dFont);
3982 void
3983 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3985 if (!tPtr)
3986 return;
3988 WMReleaseColor(tPtr->dColor);
3989 if (color)
3990 tPtr->dColor = WMRetainColor(color);
3991 else
3992 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3995 WMColor *
3996 WMGetTextDefaultColor(WMText *tPtr)
3998 if (!tPtr)
3999 return NULL;
4000 else
4001 return WMRetainColor(tPtr->dColor);
4004 void
4005 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
4007 if (!tPtr)
4008 return;
4009 if(tPtr->flags.monoFont)
4010 tPtr->flags.alignment = WALeft;
4011 else
4012 tPtr->flags.alignment = alignment;
4013 WMThawText(tPtr);
4016 int
4017 WMGetTextInsertType(WMText *tPtr)
4019 if (!tPtr)
4020 return 0;
4021 return tPtr->flags.prepend;
4025 void
4026 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
4028 if (!tPtr || !color)
4029 return;
4031 setSelectionProperty(tPtr, NULL, color, -1);
4034 WMColor *
4035 WMGetTextSelectionColor(WMText *tPtr)
4037 TextBlock *tb;
4039 if (!tPtr)
4040 return NULL;
4042 tb = tPtr->currentTextBlock;
4044 if (!tb || !tPtr->flags.ownsSelection)
4045 return NULL;
4047 if(!tb->selected)
4048 return NULL;
4050 return tb->color;
4054 void
4055 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4057 if (!tPtr || !font)
4058 return;
4060 setSelectionProperty(tPtr, font, NULL, -1) ;
4063 WMFont *
4064 WMGetTextSelectionFont(WMText *tPtr)
4066 TextBlock *tb;
4068 if (!tPtr)
4069 return NULL;
4071 tb = tPtr->currentTextBlock;
4073 if (!tb || !tPtr->flags.ownsSelection)
4074 return NULL;
4076 if(!tb->selected)
4077 return NULL;
4079 if(tb->graphic) {
4080 tb = getFirstNonGraphicBlockFor(tb, 1);
4081 if(!tb)
4082 return NULL;
4084 return (tb->selected ? tb->d.font : NULL);
4088 void
4089 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4091 if (!tPtr || (underlined!=0 && underlined !=1))
4092 return;
4094 setSelectionProperty(tPtr, NULL, NULL, underlined);
4098 int
4099 WMGetTextSelectionUnderlined(WMText *tPtr)
4101 TextBlock *tb;
4103 if (!tPtr)
4104 return 0;
4106 tb = tPtr->currentTextBlock;
4108 if (!tb || !tPtr->flags.ownsSelection)
4109 return 0;
4111 if(!tb->selected)
4112 return 0;
4114 return tb->underlined;
4118 void
4119 WMFreezeText(WMText *tPtr)
4121 if (!tPtr)
4122 return;
4124 tPtr->flags.frozen = True;
4128 void
4129 WMThawText(WMText *tPtr)
4131 if (!tPtr)
4132 return;
4134 tPtr->flags.frozen = False;
4136 if(tPtr->flags.monoFont) {
4137 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4138 TextBlock *tb;
4140 /* make sure to unmap widgets no matter where they are */
4141 /* they'll be later remapped if needed by paintText */
4142 for(j=0; j<c; j++) {
4143 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4144 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4145 WMUnmapWidget(tb->d.widget);
4151 tPtr->flags.laidOut = False;
4152 layOutDocument(tPtr);
4153 updateScrollers(tPtr);
4154 paintText(tPtr);
4155 tPtr->flags.needsLayOut = False;
4159 /* find first occurence of a string */
4160 static char *
4161 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4162 Bool caseSensitive)
4164 char *ptr;
4166 if(!haystack || !needle || !end)
4167 return NULL;
4169 for (ptr = haystack; ptr < end; ptr++) {
4170 if(caseSensitive) {
4171 if (*ptr == *needle && !strncmp(ptr, needle, len))
4172 return ptr;
4174 } else {
4175 if (tolower(*ptr) == tolower(*needle) &&
4176 !strncasecmp(ptr, needle, len))
4177 return ptr;
4181 return NULL;
4184 /* find last occurence of a string */
4185 static char *
4186 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4187 Bool caseSensitive)
4189 char *ptr;
4191 if(!haystack || !needle || !end)
4192 return NULL;
4194 for (ptr = haystack-2; ptr > end; ptr--) {
4195 if(caseSensitive) {
4196 if (*ptr == *needle && !strncmp(ptr, needle, len))
4197 return ptr;
4198 } else {
4199 if (tolower(*ptr) == tolower(*needle) &&
4200 !strncasecmp(ptr, needle, len))
4201 return ptr;
4205 return NULL;
4209 Bool
4210 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4211 Bool caseSensitive)
4213 TextBlock *tb;
4214 char *mark=NULL;
4215 unsigned short pos;
4217 if (!tPtr || !needle)
4218 return False;
4220 #if 0
4221 if (! (tb = tPtr->currentTextBlock)) {
4222 if (! (tb = ( (direction > 0) ?
4223 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4224 return False;
4226 } else {
4227 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4228 tb = (direction>0) ? tb->next : tb->prior; */
4229 if(tb != tPtr->lastTextBlock)
4230 tb = tb->prior;
4232 #endif
4233 tb = tPtr->currentTextBlock;
4234 pos = tPtr->tpos;
4237 while(tb) {
4238 if (!tb->graphic) {
4240 if(direction > 0) {
4241 if(pos+1 < tb->used)
4242 pos++;
4244 if(tb->used - pos> 0 && pos > 0) {
4245 mark = mystrstr(&tb->text[pos], needle,
4246 strlen(needle), &tb->text[tb->used], caseSensitive);
4248 } else {
4249 tb = tb->next;
4250 pos = 0;
4251 continue;
4254 } else {
4255 if(pos-1 > 0)
4256 pos--;
4258 if(pos > 0) {
4259 mark = mystrrstr(&tb->text[pos], needle,
4260 strlen(needle), tb->text, caseSensitive);
4261 } else {
4262 tb = tb->prior;
4263 if(!tb)
4264 return False;
4265 pos = tb->used;
4266 continue;
4271 if(mark) {
4272 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4274 tPtr->tpos = (int)(mark - tb->text);
4275 tPtr->currentTextBlock = tb;
4276 updateCursorPosition(tPtr);
4277 tPtr->sel.y = tPtr->cursor.y+5;
4278 tPtr->sel.h = tPtr->cursor.h-10;
4279 tPtr->sel.x = tPtr->cursor.x +1;
4280 tPtr->sel.w = WMIN(WMWidthOfString(font,
4281 &tb->text[tPtr->tpos], strlen(needle)),
4282 tPtr->docWidth - tPtr->sel.x);
4283 tPtr->flags.ownsSelection = True;
4284 paintText(tPtr);
4286 return True;
4290 tb = (direction>0) ? tb->next : tb->prior;
4291 if(tb) {
4292 pos = (direction>0) ? 0 : tb->used;
4296 return False;
4300 Bool
4301 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4303 if (!tPtr)
4304 return False;
4306 if (!tPtr->flags.ownsSelection)
4307 return False;
4309 removeSelection(tPtr);
4311 if(replacement) {
4312 insertTextInteractively(tPtr, replacement, strlen(replacement));
4313 updateCursorPosition(tPtr);
4314 paintText(tPtr);
4317 return True;