- put back wmksize(), wmkrange() and wmkpoint() as functions instead of macros
[wmaker-crm.git] / WINGs / wtext.c
blobb1d086fe90c52973e1f23da0f415894c12d4cf91
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 if(!tb->sections)
622 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;
2203 static void
2204 lostHandler(WMView *view, Atom selection, void *cdata)
2206 releaseSelection((WMText *)view->self);
2209 static WMSelectionProcs selectionHandler = {
2210 requestHandler, lostHandler, NULL
2214 static void
2215 ownershipObserver(void *observerData, WMNotification *notification)
2217 if (observerData != WMGetNotificationClientData(notification))
2218 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2221 static void
2222 autoSelectText(Text *tPtr, int clicks)
2224 int x, start;
2225 TextBlock *tb;
2226 char *mark = NULL, behind, ahead;
2228 if(!(tb = tPtr->currentTextBlock))
2229 return;
2231 if(clicks == 2) {
2234 switch(tb->text[tPtr->tpos]) {
2235 case ' ': return;
2237 case '<': case '>': behind = '<'; ahead = '>'; break;
2238 case '{': case '}': behind = '{'; ahead = '}'; break;
2239 case '[': case ']': behind = '['; ahead = ']'; break;
2241 default: behind = ahead = ' ';
2244 tPtr->sel.y = tPtr->cursor.y+5;
2245 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2247 if(tb->graphic) {
2248 tPtr->sel.x = tb->sections[0].x;
2249 tPtr->sel.w = tb->sections[0].w;
2250 } else {
2251 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2253 start = tPtr->tpos;
2254 while(start > 0 && tb->text[start-1] != behind)
2255 start--;
2257 x = tPtr->cursor.x;
2258 if(tPtr->tpos > start){
2259 x -= WMWidthOfString(font, &tb->text[start],
2260 tPtr->tpos - start);
2262 tPtr->sel.x = (x<0?0:x)+1;
2264 if((mark = strchr(&tb->text[start], ahead))) {
2265 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2266 (int)(mark - &tb->text[start]));
2267 } else if(tb->used > start) {
2268 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2269 tb->used - start);
2273 } else if(clicks == 3) {
2274 TextBlock *cur = tb;
2276 while(tb && !tb->first) {
2277 tb = tb->prior;
2279 tPtr->sel.y = tb->sections[0]._y;
2281 tb = cur;
2282 while(tb->next && !tb->next->first) {
2283 tb = tb->next;
2285 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2286 + 5 - tPtr->sel.y;
2288 tPtr->sel.x = 0;
2289 tPtr->sel.w = tPtr->docWidth;
2290 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2293 if (!tPtr->flags.ownsSelection) {
2294 WMCreateSelectionHandler(tPtr->view,
2295 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2296 tPtr->flags.ownsSelection = True;
2298 paintText(tPtr);
2303 static void
2304 fontChanged(void *observerData, WMNotification *notification)
2306 WMText *tPtr = (WMText *) observerData;
2307 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2308 printf("fontChanged\n");
2310 if(!tPtr || !font)
2311 return;
2313 if (tPtr->flags.ownsSelection)
2314 WMSetTextSelectionFont(tPtr, font);
2318 static void
2319 handleTextKeyPress(Text *tPtr, XEvent *event)
2321 char buffer[64];
2322 KeySym ksym;
2323 int control_pressed = False;
2324 TextBlock *tb = NULL;
2326 if (((XKeyEvent *) event)->state & ControlMask)
2327 control_pressed = True;
2328 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2330 switch(ksym) {
2332 case XK_Home:
2333 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2334 tPtr->tpos = 0;
2335 updateCursorPosition(tPtr);
2336 paintText(tPtr);
2337 break;
2339 case XK_End:
2340 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2341 if(tPtr->currentTextBlock->graphic)
2342 tPtr->tpos = 1;
2343 else
2344 tPtr->tpos = tPtr->currentTextBlock->used;
2346 updateCursorPosition(tPtr);
2347 paintText(tPtr);
2348 break;
2350 case XK_Left:
2351 if(!(tb = tPtr->currentTextBlock))
2352 break;
2353 if(tb->graphic)
2354 goto L_imaGFX;
2356 if(tPtr->tpos==0) {
2357 L_imaGFX: if(tb->prior) {
2358 tPtr->currentTextBlock = tb->prior;
2359 if(tPtr->currentTextBlock->graphic)
2360 tPtr->tpos = 1;
2361 else
2362 tPtr->tpos = tPtr->currentTextBlock->used;
2364 if(!tb->first && tPtr->tpos > 0)
2365 tPtr->tpos--;
2366 } else tPtr->tpos = 0;
2367 } else tPtr->tpos--;
2368 updateCursorPosition(tPtr);
2369 paintText(tPtr);
2370 break;
2372 case XK_Right:
2373 if(!(tb = tPtr->currentTextBlock))
2374 break;
2375 if(tb->graphic)
2376 goto R_imaGFX;
2377 if(tPtr->tpos == tb->used) {
2378 R_imaGFX: if(tb->next) {
2379 tPtr->currentTextBlock = tb->next;
2380 tPtr->tpos = 0;
2381 if(!tb->next->first && tb->next->used>0)
2382 tPtr->tpos++;
2383 } else {
2384 if(tb->graphic)
2385 tPtr->tpos = 1;
2386 else
2387 tPtr->tpos = tb->used;
2389 } else tPtr->tpos++;
2390 updateCursorPosition(tPtr);
2391 paintText(tPtr);
2392 break;
2394 case XK_Down:
2395 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2396 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2397 paintText(tPtr);
2398 break;
2400 case XK_Up:
2401 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2402 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2403 paintText(tPtr);
2404 break;
2406 case XK_BackSpace:
2407 case XK_Delete:
2408 #ifdef XK_KP_Delete
2409 case XK_KP_Delete:
2410 #endif
2411 deleteTextInteractively(tPtr, ksym);
2412 updateCursorPosition(tPtr);
2413 paintText(tPtr);
2414 break;
2416 case XK_Control_R :
2417 case XK_Control_L :
2418 control_pressed = True;
2419 break;
2421 case XK_Tab:
2422 insertTextInteractively(tPtr, " ", 4);
2423 updateCursorPosition(tPtr);
2424 paintText(tPtr);
2425 break;
2427 case XK_Return:
2428 *buffer = '\n';
2429 default:
2430 if (*buffer != 0 && !control_pressed) {
2431 insertTextInteractively(tPtr, buffer, strlen(buffer));
2432 updateCursorPosition(tPtr);
2433 paintText(tPtr);
2435 } else if (control_pressed && ksym==XK_r) {
2436 Bool i = !tPtr->flags.rulerShown;
2437 WMShowTextRuler(tPtr, i);
2438 tPtr->flags.rulerShown = i;
2440 else if (control_pressed && *buffer == '\a')
2441 XBell(tPtr->view->screen->display, 0);
2442 else
2443 WMRelayToNextResponder(tPtr->view, event);
2446 if (!control_pressed && tPtr->flags.ownsSelection)
2447 releaseSelection(tPtr);
2451 static void
2452 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2453 void *cdata, WMData *data)
2455 Text *tPtr = (Text *)view->self;
2456 char *text;
2458 tPtr->flags.waitingForSelection = 0;
2460 if (data) {
2461 text = (char*)WMDataBytes(data);
2463 if (tPtr->parser) {
2464 (tPtr->parser) (tPtr, (void *) text);
2465 layOutDocument(tPtr);
2466 } else insertTextInteractively(tPtr, text, strlen(text));
2467 updateCursorPosition(tPtr);
2468 paintText(tPtr);
2470 } else {
2471 int n;
2473 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2475 if (text) {
2476 text[n] = 0;
2477 if (tPtr->parser) {
2478 (tPtr->parser) (tPtr, (void *) text);
2479 layOutDocument(tPtr);
2480 } else insertTextInteractively(tPtr, text, n);
2481 updateCursorPosition(tPtr);
2482 paintText(tPtr);
2484 XFree(text);
2492 static void
2493 handleActionEvents(XEvent *event, void *data)
2495 Text *tPtr = (Text *)data;
2496 Display *dpy = event->xany.display;
2497 KeySym ksym;
2500 switch (event->type) {
2501 case KeyPress:
2502 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2503 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2504 tPtr->flags.extendSelection = True;
2505 return;
2508 if (tPtr->flags.focused) {
2509 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2510 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2511 GrabModeAsync, GrabModeAsync, None,
2512 tPtr->view->screen->invisibleCursor, CurrentTime);
2513 tPtr->flags.pointerGrabbed = True;
2514 handleTextKeyPress(tPtr, event);
2516 } break;
2518 case KeyRelease:
2519 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2520 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2521 tPtr->flags.extendSelection = False;
2522 return;
2523 /* end modify flag so selection can be extended */
2525 break;
2528 case MotionNotify:
2530 if (tPtr->flags.pointerGrabbed) {
2531 tPtr->flags.pointerGrabbed = False;
2532 XUngrabPointer(dpy, CurrentTime);
2535 if(tPtr->flags.waitingForSelection)
2536 break;
2538 if ((event->xmotion.state & Button1Mask)) {
2539 TextBlock *tb = tPtr->currentTextBlock;
2541 if(tb && tPtr->flags.isOverGraphic &&
2542 tb->graphic && !tb->object) {
2543 WMSize offs;
2544 WMPixmap *pixmap = tb->d.pixmap;
2545 char *types[2] = {"application/X-image", NULL};
2547 offs.width = 2;
2548 offs.height = 2;
2550 WMDragImageFromView(tPtr->view, pixmap, types,
2551 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2552 offs, event, True);
2555 } else {
2556 if (!tPtr->flags.ownsSelection) {
2557 WMCreateSelectionHandler(tPtr->view,
2558 XA_PRIMARY, event->xbutton.time,
2559 &selectionHandler, NULL);
2560 tPtr->flags.ownsSelection = True;
2563 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2564 break;
2567 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2568 break;
2571 case ButtonPress:
2573 if (tPtr->flags.pointerGrabbed) {
2574 tPtr->flags.pointerGrabbed = False;
2575 XUngrabPointer(dpy, CurrentTime);
2576 break;
2579 if (tPtr->flags.waitingForSelection)
2580 break;
2582 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2583 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2584 return;
2587 if (tPtr->flags.ownsSelection)
2588 releaseSelection(tPtr);
2591 if (event->xbutton.button == Button1) {
2593 if(WMIsDoubleClick(event)) {
2594 TextBlock *tb = tPtr->currentTextBlock;
2596 tPtr->lastClickTime = event->xbutton.time;
2597 if(tb && tb->graphic && !tb->object) {
2598 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2599 char *desc;
2601 desc = wmalloc(tb->used+1);
2602 memcpy(desc, tb->text, tb->used);
2603 desc[tb->used] = 0;
2604 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2605 wfree(desc);
2607 } else {
2608 autoSelectText(tPtr, 2);
2610 break;
2611 } else if(event->xbutton.time - tPtr->lastClickTime
2612 < WINGsConfiguration.doubleClickDelay) {
2613 tPtr->lastClickTime = event->xbutton.time;
2614 autoSelectText(tPtr, 3);
2615 break;
2618 if (!tPtr->flags.focused) {
2619 WMSetFocusToWidget(tPtr);
2620 tPtr->flags.focused = True;
2623 tPtr->lastClickTime = event->xbutton.time;
2624 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2625 paintText(tPtr);
2628 if (event->xbutton.button
2629 == WINGsConfiguration.mouseWheelDown) {
2630 WMScrollText(tPtr, 16);
2631 break;
2634 if (event->xbutton.button
2635 == WINGsConfiguration.mouseWheelUp) {
2636 WMScrollText(tPtr, -16);
2637 break;
2640 if (event->xbutton.button == Button2) {
2641 char *text = NULL;
2642 int n;
2644 if (!tPtr->flags.editable) {
2645 XBell(dpy, 0);
2646 break;
2649 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2650 event->xbutton.time, pasteText, NULL)) {
2652 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2653 tPtr->flags.waitingForSelection = 0;
2655 if (text) {
2656 text[n] = 0;
2658 if (tPtr->parser) {
2659 (tPtr->parser) (tPtr, (void *) text);
2660 layOutDocument(tPtr);
2662 else
2663 insertTextInteractively(tPtr, text, n);
2665 XFree(text);
2666 #if 0
2667 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2668 (void*)WMInsertTextEvent);
2669 #endif
2670 updateCursorPosition(tPtr);
2671 paintText(tPtr);
2673 } else {
2674 tPtr->flags.waitingForSelection = True;
2677 break;
2681 case ButtonRelease:
2682 if (tPtr->flags.pointerGrabbed) {
2683 tPtr->flags.pointerGrabbed = False;
2684 XUngrabPointer(dpy, CurrentTime);
2685 break;
2688 if (tPtr->flags.waitingForSelection)
2689 break;
2695 static void
2696 handleEvents(XEvent *event, void *data)
2698 Text *tPtr = (Text *)data;
2700 switch(event->type) {
2701 case Expose:
2703 if (event->xexpose.count!=0)
2704 break;
2706 if(tPtr->hS) {
2707 if (!(W_VIEW(tPtr->hS))->flags.realized)
2708 WMRealizeWidget(tPtr->hS);
2711 if(tPtr->vS) {
2712 if (!(W_VIEW(tPtr->vS))->flags.realized)
2713 WMRealizeWidget(tPtr->vS);
2716 if(tPtr->ruler) {
2717 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2718 WMRealizeWidget(tPtr->ruler);
2722 if(!tPtr->db)
2723 textDidResize(tPtr->view->delegate, tPtr->view);
2725 paintText(tPtr);
2726 break;
2728 case FocusIn:
2729 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2730 != tPtr->view)
2731 return;
2732 tPtr->flags.focused = True;
2733 #if DO_BLINK
2734 if (tPtr->flags.editable && !tPtr->timerID) {
2735 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2736 blinkCursor, tPtr);
2738 #endif
2740 break;
2742 case FocusOut:
2743 tPtr->flags.focused = False;
2744 paintText(tPtr);
2745 #if DO_BLINK
2746 if (tPtr->timerID) {
2747 WMDeleteTimerHandler(tPtr->timerID);
2748 tPtr->timerID = NULL;
2750 #endif
2751 break;
2754 case DestroyNotify:
2755 clearText(tPtr);
2756 if(tPtr->db)
2757 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2758 if(tPtr->gfxItems)
2759 WMEmptyArray(tPtr->gfxItems);
2760 #if DO_BLINK
2761 if (tPtr->timerID)
2762 WMDeleteTimerHandler(tPtr->timerID);
2763 #endif
2764 WMReleaseFont(tPtr->dFont);
2765 WMReleaseColor(tPtr->dColor);
2766 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2767 WMRemoveNotificationObserver(tPtr);
2769 wfree(tPtr);
2771 break;
2777 static void
2778 insertPlainText(Text *tPtr, char *text)
2780 char *start, *mark;
2781 void *tb = NULL;
2783 start = text;
2784 while (start) {
2785 mark = strchr(start, '\n');
2786 if (mark) {
2787 tb = WMCreateTextBlockWithText(tPtr,
2788 start, tPtr->dFont,
2789 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2790 start = mark+1;
2791 tPtr->flags.first = True;
2792 } else {
2793 if (start && strlen(start)) {
2794 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2795 tPtr->dColor, tPtr->flags.first, strlen(start));
2796 } else tb = NULL;
2797 tPtr->flags.first = False;
2798 start = mark;
2801 if (tPtr->flags.prepend)
2802 WMPrependTextBlock(tPtr, tb);
2803 else
2804 WMAppendTextBlock(tPtr, tb);
2809 static void
2810 rulerMoveCallBack(WMWidget *w, void *self)
2812 Text *tPtr = (Text *)self;
2814 if (!tPtr)
2815 return;
2816 if (W_CLASS(tPtr) != WC_Text)
2817 return;
2819 paintText(tPtr);
2823 static void
2824 rulerReleaseCallBack(WMWidget *w, void *self)
2826 Text *tPtr = (Text *)self;
2828 if (!tPtr)
2829 return;
2830 if (W_CLASS(tPtr) != WC_Text)
2831 return;
2833 WMThawText(tPtr);
2834 return;
2837 static unsigned
2838 draggingSourceOperation(WMView *self, Bool local)
2840 return WDOperationCopy;
2843 static WMData*
2844 fetchDragData(WMView *self, char *type)
2846 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2847 char *desc;
2848 WMData *data;
2850 if (!tb)
2851 return NULL;
2853 printf("type is [%s]\n", type);
2854 desc = wmalloc(tb->used+1);
2855 memcpy(desc, tb->text, tb->used);
2856 desc[tb->used] = 0;
2857 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2859 wfree(desc);
2861 return data;
2865 static WMDragSourceProcs _DragSourceProcs = {
2866 draggingSourceOperation,
2867 NULL,
2868 NULL,
2869 fetchDragData
2873 static unsigned
2874 draggingEntered(WMView *self, WMDraggingInfo *info)
2876 printf("draggingEntered\n");
2877 return WDOperationCopy;
2881 static unsigned
2882 draggingUpdated(WMView *self, WMDraggingInfo *info)
2884 return WDOperationCopy;
2888 static void
2889 draggingExited(WMView *self, WMDraggingInfo *info)
2891 printf("draggingExited\n");
2894 static Bool
2895 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2897 printf("prepareForDragOperation\n");
2898 return True;
2902 char *badbadbad;
2904 static void
2905 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2906 void *cdata, WMData *data)
2908 badbadbad = wstrdup((char *)WMDataBytes(data));
2912 /* when it's done in WINGs, remove this */
2914 Bool
2915 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2917 WMScreen *scr = W_VIEW_SCREEN(view);
2919 if (!WMRequestSelection(scr->dragInfo.destView,
2920 scr->xdndSelectionAtom,
2921 XInternAtom(scr->display, type, False),
2922 scr->dragInfo.timestamp,
2923 receivedData, &scr->dragInfo)) {
2924 wwarning("could not request data for dropped data");
2928 XEvent ev;
2930 ev.type = ClientMessage;
2931 ev.xclient.message_type = scr->xdndFinishedAtom;
2932 ev.xclient.format = 32;
2933 ev.xclient.window = info->destinationWindow;
2934 ev.xclient.data.l[0] = 0;
2935 ev.xclient.data.l[1] = 0;
2936 ev.xclient.data.l[2] = 0;
2937 ev.xclient.data.l[3] = 0;
2938 ev.xclient.data.l[4] = 0;
2940 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2941 XFlush(scr->display);
2943 return True;
2946 static Bool
2947 performDragOperation(WMView *self, WMDraggingInfo *info)
2949 WMColor *color;
2950 WMText *tPtr = (WMText *)self->self;
2952 if (!tPtr)
2953 return True;
2955 requestDroppedData(tPtr->view, info, "application/X-color");
2956 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2957 if(color) {
2958 WMSetTextSelectionColor(tPtr, color);
2959 WMReleaseColor(color);
2964 return True;
2967 static void
2968 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2970 printf("concludeDragOperation\n");
2974 static WMDragDestinationProcs _DragDestinationProcs = {
2975 draggingEntered,
2976 draggingUpdated,
2977 draggingExited,
2978 prepareForDragOperation,
2979 performDragOperation,
2980 concludeDragOperation
2984 char *
2985 getStream(WMText *tPtr, int sel, int array)
2987 TextBlock *tb = NULL;
2988 char *text = NULL;
2989 unsigned long where = 0;
2991 if (!tPtr)
2992 return NULL;
2994 if (!(tb = tPtr->firstTextBlock))
2995 return NULL;
2997 if (tPtr->writer) {
2998 (tPtr->writer) (tPtr, (void *) text);
2999 return text;
3002 tb = tPtr->firstTextBlock;
3003 while (tb) {
3005 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
3007 if (!sel || (tb->graphic && tb->selected)) {
3009 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3010 && tb != tPtr->firstTextBlock) {
3011 text = wrealloc(text, where+1);
3012 text[where++] = '\n';
3015 if(tb->blank)
3016 goto _gSnext;
3018 if(tb->graphic && array) {
3019 text = wrealloc(text, where+4);
3020 text[where++] = 0xFA;
3021 text[where++] = (tb->used>>8)&0x0ff;
3022 text[where++] = tb->used&0x0ff;
3023 text[where++] = tb->allocated; /* extra info */
3025 text = wrealloc(text, where+tb->used);
3026 memcpy(&text[where], tb->text, tb->used);
3027 where += tb->used;
3030 } else if (sel && tb->selected) {
3032 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3033 text = wrealloc(text, where+1);
3034 text[where++] = '\n';
3037 if(tb->blank)
3038 goto _gSnext;
3040 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3041 memcpy(&text[where], &tb->text[tb->s_begin],
3042 tb->s_end - tb->s_begin);
3043 where += tb->s_end - tb->s_begin;
3048 _gSnext:tb = tb->next;
3051 /* +1 for the end of string, let's be nice */
3052 text = wrealloc(text, where+1);
3053 text[where] = 0;
3054 return text;
3058 static void
3059 releaseStreamObjects(void *data)
3061 if(data)
3062 wfree(data);
3065 WMArray *
3066 getStreamObjects(WMText *tPtr, int sel)
3068 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3069 WMData *data;
3070 char *stream;
3071 unsigned short len;
3072 char *start, *fa, *desc;
3074 stream = getStream(tPtr, sel, 1);
3075 if(!stream)
3076 return NULL;
3078 start = stream;
3079 while (start) {
3081 fa = strchr(start, 0xFA);
3082 if (fa) {
3083 if((int)(fa - start)>0) {
3084 desc = start;
3085 desc[(int)(fa - start)] = 0;
3086 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3087 WMSetDataFormat(data, TYPETEXT);
3088 WMAddToArray(array, (void *) data);
3091 len = *(fa+1)*0xff + *(fa+2);
3092 data = WMCreateDataWithBytes((void *)(fa+4), len);
3093 WMSetDataFormat(data, *(fa+3));
3094 WMAddToArray(array, (void *) data);
3095 start = fa + len + 4;
3097 } else {
3098 if (start && strlen(start)) {
3099 data = WMCreateDataWithBytes((void *)start, strlen(start));
3100 WMSetDataFormat(data, TYPETEXT);
3101 WMAddToArray(array, (void *) data);
3103 start = fa;
3107 wfree(stream);
3108 return array;
3112 WMText *
3113 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3115 Text *tPtr;
3116 Display *dpy;
3117 WMScreen *scr;
3118 XGCValues gcv;
3120 tPtr = wmalloc(sizeof(Text));
3121 memset(tPtr, 0, sizeof(Text));
3122 tPtr->widgetClass = WC_Text;
3123 tPtr->view = W_CreateView(W_VIEW(parent));
3124 if (!tPtr->view) {
3125 perror("could not create text's view\n");
3126 wfree(tPtr);
3127 return NULL;
3130 dpy = tPtr->view->screen->display;
3131 scr = tPtr->view->screen;
3133 tPtr->view->self = tPtr;
3134 tPtr->view->attribs.cursor = scr->textCursor;
3135 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3136 W_ResizeView(tPtr->view, 250, 200);
3138 tPtr->dColor = WMWhiteColor(scr);
3139 tPtr->bgGC = WMColorGC(tPtr->dColor);
3140 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3141 WMReleaseColor(tPtr->dColor);
3143 tPtr->dColor = WMBlackColor(scr);
3144 tPtr->fgGC = WMColorGC(tPtr->dColor);
3146 gcv.graphics_exposures = False;
3147 gcv.foreground = W_PIXEL(scr->gray);
3148 gcv.background = W_PIXEL(scr->darkGray);
3149 gcv.fill_style = FillStippled;
3150 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr),
3151 STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
3152 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3153 GCForeground|GCBackground|GCStipple
3154 |GCFillStyle|GCGraphicsExposures, &gcv);
3156 tPtr->ruler = NULL;
3157 tPtr->vS = NULL;
3158 tPtr->hS = NULL;
3160 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(scr, 12));
3162 tPtr->view->delegate = &_TextViewDelegate;
3164 tPtr->delegate = NULL;
3166 #if DO_BLINK
3167 tPtr->timerID = NULL;
3168 #endif
3170 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3171 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3172 handleEvents, tPtr);
3174 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3175 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3176 handleActionEvents, tPtr);
3178 WMAddNotificationObserver(ownershipObserver, tPtr,
3179 "_lostOwnership", tPtr);
3181 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3182 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3186 char *types[3] = {"application/X-color", "application/X-image", NULL};
3187 WMRegisterViewForDraggedTypes(tPtr->view, types);
3190 WMAddNotificationObserver(fontChanged, tPtr,
3191 "WMFontPanelDidChangeNotification", tPtr);
3193 tPtr->firstTextBlock = NULL;
3194 tPtr->lastTextBlock = NULL;
3195 tPtr->currentTextBlock = NULL;
3196 tPtr->tpos = 0;
3198 tPtr->gfxItems = WMCreateArray(4);
3200 tPtr->parser = parser;
3201 tPtr->writer = writer;
3203 tPtr->sel.x = tPtr->sel.y = 2;
3204 tPtr->sel.w = tPtr->sel.h = 0;
3206 tPtr->clicked.x = tPtr->clicked.y = 2;
3208 tPtr->visible.x = tPtr->visible.y = 2;
3209 tPtr->visible.h = tPtr->view->size.height;
3210 tPtr->visible.w = tPtr->view->size.width - 4;
3212 tPtr->cursor.x = -23;
3214 tPtr->docWidth = 0;
3215 tPtr->docHeight = 0;
3216 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3217 default_bullet);
3218 tPtr->db = (Pixmap) NULL;
3219 tPtr->bgPixmap = NULL;
3221 tPtr->margins = WMGetRulerMargins(NULL);
3222 tPtr->margins->right = tPtr->visible.w;
3223 tPtr->nMargins = 1;
3225 tPtr->flags.rulerShown = False;
3226 tPtr->flags.monoFont = False;
3227 tPtr->flags.focused = False;
3228 tPtr->flags.editable = True;
3229 tPtr->flags.ownsSelection = False;
3230 tPtr->flags.pointerGrabbed = False;
3231 tPtr->flags.extendSelection = False;
3232 tPtr->flags.frozen = False;
3233 tPtr->flags.cursorShown = True;
3234 tPtr->flags.acceptsGraphic = False;
3235 tPtr->flags.horizOnDemand = False;
3236 tPtr->flags.needsLayOut = False;
3237 tPtr->flags.ignoreNewLine = False;
3238 tPtr->flags.indentNewLine = False;
3239 tPtr->flags.laidOut = False;
3240 tPtr->flags.ownsSelection = False;
3241 tPtr->flags.waitingForSelection = False;
3242 tPtr->flags.prepend = False;
3243 tPtr->flags.isOverGraphic = False;
3244 tPtr->flags.relief = WRSunken;
3245 tPtr->flags.isOverGraphic = 0;
3246 tPtr->flags.alignment = WALeft;
3247 tPtr->flags.first = True;
3249 return tPtr;
3252 void
3253 WMPrependTextStream(WMText *tPtr, char *text)
3255 CHECK_CLASS(tPtr, WC_Text);
3257 if(!text) {
3258 if (tPtr->flags.ownsSelection)
3259 releaseSelection(tPtr);
3260 clearText(tPtr);
3261 updateScrollers(tPtr);
3262 return;
3265 tPtr->flags.prepend = True;
3266 if (text && tPtr->parser)
3267 (tPtr->parser) (tPtr, (void *) text);
3268 else
3269 insertPlainText(tPtr, text);
3271 tPtr->flags.needsLayOut = True;
3272 tPtr->tpos = 0;
3273 if(!tPtr->flags.frozen) {
3274 layOutDocument(tPtr);
3279 void
3280 WMAppendTextStream(WMText *tPtr, char *text)
3282 CHECK_CLASS(tPtr, WC_Text);
3284 if(!text) {
3285 if (tPtr->flags.ownsSelection)
3286 releaseSelection(tPtr);
3287 clearText(tPtr);
3288 updateScrollers(tPtr);
3289 return;
3292 tPtr->flags.prepend = False;
3293 if (text && tPtr->parser)
3294 (tPtr->parser) (tPtr, (void *) text);
3295 else
3296 insertPlainText(tPtr, text);
3298 tPtr->flags.needsLayOut = True;
3299 if(tPtr->currentTextBlock) {
3300 if(tPtr->currentTextBlock->graphic)
3301 tPtr->tpos = 1;
3302 else
3303 tPtr->tpos = tPtr->currentTextBlock->used;
3306 if(!tPtr->flags.frozen) {
3307 layOutDocument(tPtr);
3312 char *
3313 WMGetTextStream(WMText *tPtr)
3315 CHECK_CLASS(tPtr, WC_Text);
3316 return getStream(tPtr, 0, 0);
3319 char *
3320 WMGetTextSelectedStream(WMText *tPtr)
3322 CHECK_CLASS(tPtr, WC_Text);
3323 return getStream(tPtr, 1, 0);
3326 WMArray *
3327 WMGetTextObjects(WMText *tPtr)
3329 CHECK_CLASS(tPtr, WC_Text);
3330 return getStreamObjects(tPtr, 0);
3333 WMArray *
3334 WMGetTextSelectedObjects(WMText *tPtr)
3336 CHECK_CLASS(tPtr, WC_Text);
3337 return getStreamObjects(tPtr, 1);
3341 void
3342 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3344 CHECK_CLASS(tPtr, WC_Text);
3346 tPtr->delegate = delegate;
3350 void *
3351 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3352 char *description, WMColor *color,
3353 unsigned short first, unsigned short extraInfo)
3355 TextBlock *tb;
3357 if (!w || !description || !color)
3358 return NULL;
3360 tb = wmalloc(sizeof(TextBlock));
3361 if (!tb)
3362 return NULL;
3364 tb->text = wstrdup(description);
3365 tb->used = strlen(description);
3366 tb->blank = False;
3367 tb->d.widget = w;
3368 tb->color = WMRetainColor(color);
3369 tb->marginN = newMargin(tPtr, NULL);
3370 tb->allocated = extraInfo;
3371 tb->first = first;
3372 tb->kanji = False;
3373 tb->graphic = True;
3374 tb->object = True;
3375 tb->underlined = False;
3376 tb->selected = False;
3377 tb->script = 0;
3378 tb->sections = NULL;
3379 tb->nsections = 0;
3380 tb->prior = NULL;
3381 tb->next = NULL;
3383 return tb;
3387 void *
3388 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3389 char *description, WMColor *color,
3390 unsigned short first, unsigned short extraInfo)
3392 TextBlock *tb;
3394 if (!p || !description || !color)
3395 return NULL;
3397 tb = wmalloc(sizeof(TextBlock));
3398 if (!tb)
3399 return NULL;
3401 tb->text = wstrdup(description);
3402 tb->used = strlen(description);
3403 tb->blank = False;
3404 tb->d.pixmap = WMRetainPixmap(p);
3405 tb->color = WMRetainColor(color);
3406 tb->marginN = newMargin(tPtr, NULL);
3407 tb->allocated = extraInfo;
3408 tb->first = first;
3409 tb->kanji = False;
3410 tb->graphic = True;
3411 tb->object = False;
3412 tb->underlined = False;
3413 tb->selected = False;
3414 tb->script = 0;
3415 tb->sections = NULL;
3416 tb->nsections = 0;
3417 tb->prior = NULL;
3418 tb->next = NULL;
3420 return tb;
3424 void*
3425 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3426 unsigned short first, unsigned short len)
3428 TextBlock *tb;
3430 if (!font || !color)
3431 return NULL;
3433 tb = wmalloc(sizeof(TextBlock));
3434 if (!tb)
3435 return NULL;
3437 tb->allocated = reqBlockSize(len);
3438 tb->text = (char *)wmalloc(tb->allocated);
3439 memset(tb->text, 0, tb->allocated);
3441 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3442 *tb->text = ' ';
3443 tb->used = 1;
3444 tb->blank = True;
3445 } else {
3446 memcpy(tb->text, text, len);
3447 tb->used = len;
3448 tb->blank = False;
3450 tb->text[tb->used] = 0;
3452 tb->d.font = WMRetainFont(font);
3453 tb->color = WMRetainColor(color);
3454 tb->marginN = newMargin(tPtr, NULL);
3455 tb->first = first;
3456 tb->kanji = False;
3457 tb->graphic = False;
3458 tb->underlined = False;
3459 tb->selected = False;
3460 tb->script = 0;
3461 tb->sections = NULL;
3462 tb->nsections = 0;
3463 tb->prior = NULL;
3464 tb->next = NULL;
3465 return tb;
3468 void
3469 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3470 unsigned int kanji, unsigned int underlined, int script,
3471 WMRulerMargins *margins)
3473 TextBlock *tb = (TextBlock *) vtb;
3474 if (!tb)
3475 return;
3477 tb->first = first;
3478 tb->kanji = kanji;
3479 tb->underlined = underlined;
3480 tb->script = script;
3481 tb->marginN = newMargin(tPtr, margins);
3484 void
3485 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3486 unsigned int *kanji, unsigned int *underlined, int *script,
3487 WMRulerMargins *margins)
3489 TextBlock *tb = (TextBlock *) vtb;
3490 if (!tb)
3491 return;
3493 if (first) *first = tb->first;
3494 if (kanji) *kanji = tb->kanji;
3495 if (underlined) *underlined = tb->underlined;
3496 if (script) *script = tb->script;
3497 if (margins) margins = &tPtr->margins[tb->marginN];
3502 void
3503 WMPrependTextBlock(WMText *tPtr, void *vtb)
3505 TextBlock *tb = (TextBlock *)vtb;
3507 if (!tPtr || !tb)
3508 return;
3510 if (tb->graphic) {
3511 if(tb->object) {
3512 WMWidget *w = tb->d.widget;
3513 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3514 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3515 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3518 WMAddToArray(tPtr->gfxItems, (void *)tb);
3519 tPtr->tpos = 1;
3521 } else {
3522 tPtr->tpos = tb->used;
3525 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3526 tb->next = tb->prior = NULL;
3527 tb->first = True;
3528 tPtr->lastTextBlock = tPtr->firstTextBlock
3529 = tPtr->currentTextBlock = tb;
3530 return;
3533 if(!tb->first) {
3534 tb->marginN = tPtr->currentTextBlock->marginN;
3537 tb->next = tPtr->currentTextBlock;
3538 tb->prior = tPtr->currentTextBlock->prior;
3539 if (tPtr->currentTextBlock->prior)
3540 tPtr->currentTextBlock->prior->next = tb;
3542 tPtr->currentTextBlock->prior = tb;
3543 if (!tb->prior)
3544 tPtr->firstTextBlock = tb;
3546 tPtr->currentTextBlock = tb;
3550 void
3551 WMAppendTextBlock(WMText *tPtr, void *vtb)
3553 TextBlock *tb = (TextBlock *)vtb;
3555 if (!tPtr || !tb)
3556 return;
3558 if (tb->graphic) {
3559 if(tb->object) {
3560 WMWidget *w = tb->d.widget;
3561 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3562 (W_VIEW(w))->attribs.cursor =
3563 tPtr->view->screen->defaultCursor;
3564 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3567 WMAddToArray(tPtr->gfxItems, (void *)tb);
3568 tPtr->tpos = 1;
3570 } else {
3571 tPtr->tpos = tb->used;
3575 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3576 tb->next = tb->prior = NULL;
3577 tb->first = True;
3578 tPtr->lastTextBlock = tPtr->firstTextBlock
3579 = tPtr->currentTextBlock = tb;
3580 return;
3583 if(!tb->first) {
3584 tb->marginN = tPtr->currentTextBlock->marginN;
3587 tb->next = tPtr->currentTextBlock->next;
3588 tb->prior = tPtr->currentTextBlock;
3589 if (tPtr->currentTextBlock->next)
3590 tPtr->currentTextBlock->next->prior = tb;
3592 tPtr->currentTextBlock->next = tb;
3594 if (!tb->next)
3595 tPtr->lastTextBlock = tb;
3597 tPtr->currentTextBlock = tb;
3600 void *
3601 WMRemoveTextBlock(WMText *tPtr)
3603 TextBlock *tb = NULL;
3605 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3606 || !tPtr->currentTextBlock) {
3607 return NULL;
3610 tb = tPtr->currentTextBlock;
3611 if (tb->graphic) {
3612 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3614 if(tb->object) {
3615 WMUnmapWidget(tb->d.widget);
3619 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3620 if (tPtr->currentTextBlock->next)
3621 tPtr->currentTextBlock->next->prior = NULL;
3623 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3624 tPtr->currentTextBlock = tPtr->firstTextBlock;
3626 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3627 tPtr->currentTextBlock->prior->next = NULL;
3628 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3629 tPtr->currentTextBlock = tPtr->lastTextBlock;
3630 } else {
3631 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3632 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3633 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3636 return (void *)tb;
3640 static void
3641 destroyWidget(WMWidget *widget)
3643 if(!widget)
3644 return;
3646 WMDestroyWidget(widget);
3647 wfree(widget);
3650 void
3651 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3653 TextBlock *tb = (TextBlock *)vtb;
3654 if (!tPtr || !tb)
3655 return;
3657 if (tb->graphic) {
3658 if(tb->object) {
3659 /* naturally, there's a danger to destroying widgets whose action
3660 brings us here: ie. press a button to destroy it...
3661 need to find a safer way. till then... this stays commented out */
3662 /* 5 months later... destroy it 10 seconds after now which should be
3663 enough time for the widget's action to be completed... :-) */
3664 WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);
3665 tb->d.widget = NULL;
3667 } else {
3668 WMReleasePixmap(tb->d.pixmap);
3669 tb->d.pixmap = NULL;
3671 } else {
3672 WMReleaseFont(tb->d.font);
3675 WMReleaseColor(tb->color);
3676 if (tb->sections && tb->nsections > 0)
3677 wfree(tb->sections);
3678 wfree(tb->text);
3679 wfree(tb);
3680 tb = NULL;
3685 void
3686 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3688 if (!tPtr)
3689 return;
3691 if (color)
3692 tPtr->fgGC = WMColorGC(color);
3693 else {
3694 WMColor *color = WMBlackColor(tPtr->view->screen);
3695 tPtr->fgGC = WMColorGC(color);
3696 WMReleaseColor(color);
3699 paintText(tPtr);
3702 void
3703 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3705 if (!tPtr)
3706 return;
3708 if (color) {
3709 tPtr->bgGC = WMColorGC(color);
3710 W_SetViewBackgroundColor(tPtr->view, color);
3711 } else {
3712 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3713 W_SetViewBackgroundColor(tPtr->view,
3714 WMWhiteColor(tPtr->view->screen));
3717 paintText(tPtr);
3720 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3722 if (!tPtr)
3723 return;
3725 if (tPtr->bgPixmap)
3726 WMReleasePixmap(tPtr->bgPixmap);
3728 if (pixmap)
3729 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3730 else
3731 tPtr->bgPixmap = NULL;
3734 void
3735 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3737 if (!tPtr)
3738 return;
3739 tPtr->flags.relief = relief;
3740 textDidResize(tPtr->view->delegate, tPtr->view);
3743 void
3744 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3746 if (!tPtr)
3747 return;
3749 if (shouldhave && !tPtr->hS) {
3750 tPtr->hS = WMCreateScroller(tPtr);
3751 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3752 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3753 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3754 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3755 WMMapWidget(tPtr->hS);
3756 } else if (!shouldhave && tPtr->hS) {
3757 WMUnmapWidget(tPtr->hS);
3758 WMDestroyWidget(tPtr->hS);
3759 tPtr->hS = NULL;
3762 tPtr->hpos = 0;
3763 tPtr->prevHpos = 0;
3764 textDidResize(tPtr->view->delegate, tPtr->view);
3768 void
3769 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3771 if (!tPtr)
3772 return;
3774 if(shouldhave && !tPtr->ruler) {
3775 tPtr->ruler = WMCreateRuler(tPtr);
3776 (W_VIEW(tPtr->ruler))->attribs.cursor =
3777 tPtr->view->screen->defaultCursor;
3778 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3779 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3780 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3781 } else if(!shouldhave && tPtr->ruler) {
3782 WMShowTextRuler(tPtr, False);
3783 WMDestroyWidget(tPtr->ruler);
3784 tPtr->ruler = NULL;
3786 textDidResize(tPtr->view->delegate, tPtr->view);
3789 void
3790 WMShowTextRuler(WMText *tPtr, Bool show)
3792 if(!tPtr)
3793 return;
3794 if(!tPtr->ruler)
3795 return;
3797 if(tPtr->flags.monoFont)
3798 show = False;
3800 tPtr->flags.rulerShown = show;
3801 if(show) {
3802 WMMapWidget(tPtr->ruler);
3803 } else {
3804 WMUnmapWidget(tPtr->ruler);
3807 textDidResize(tPtr->view->delegate, tPtr->view);
3810 Bool
3811 WMGetTextRulerShown(WMText *tPtr)
3813 if(!tPtr)
3814 return 0;
3816 if(!tPtr->ruler)
3817 return 0;
3819 return tPtr->flags.rulerShown;
3823 void
3824 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3826 if (!tPtr)
3827 return;
3829 if (shouldhave && !tPtr->vS) {
3830 tPtr->vS = WMCreateScroller(tPtr);
3831 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3832 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3833 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3834 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3835 WMMapWidget(tPtr->vS);
3836 } else if (!shouldhave && tPtr->vS) {
3837 WMUnmapWidget(tPtr->vS);
3838 WMDestroyWidget(tPtr->vS);
3839 tPtr->vS = NULL;
3842 tPtr->vpos = 0;
3843 tPtr->prevVpos = 0;
3844 textDidResize(tPtr->view->delegate, tPtr->view);
3849 Bool
3850 WMScrollText(WMText *tPtr, int amount)
3852 Bool scroll=False;
3853 if (!tPtr)
3854 return False;
3855 if (amount == 0 || !tPtr->view->flags.realized)
3856 return False;
3858 if (amount < 0) {
3859 if (tPtr->vpos > 0) {
3860 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3861 else tPtr->vpos=0;
3862 scroll=True;
3864 } else {
3865 int limit = tPtr->docHeight - tPtr->visible.h;
3866 if (tPtr->vpos < limit) {
3867 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3868 else tPtr->vpos = limit;
3869 scroll = True;
3873 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3874 updateScrollers(tPtr);
3875 paintText(tPtr);
3877 tPtr->prevVpos = tPtr->vpos;
3878 return scroll;
3881 Bool
3882 WMPageText(WMText *tPtr, Bool direction)
3884 if (!tPtr) return False;
3885 if (!tPtr->view->flags.realized) return False;
3887 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3890 void
3891 WMSetTextEditable(WMText *tPtr, Bool editable)
3893 if (!tPtr)
3894 return;
3895 tPtr->flags.editable = editable;
3898 int
3899 WMGetTextEditable(WMText *tPtr)
3901 if (!tPtr)
3902 return 0;
3903 return tPtr->flags.editable;
3906 void
3907 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3909 if (!tPtr)
3910 return;
3911 tPtr->flags.indentNewLine = indent;
3914 void
3915 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3917 if (!tPtr)
3918 return;
3919 tPtr->flags.ignoreNewLine = ignore;
3922 Bool
3923 WMGetTextIgnoresNewline(WMText *tPtr)
3925 if (!tPtr)
3926 return True;
3927 return tPtr->flags.ignoreNewLine;
3930 void
3931 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3933 if (!tPtr)
3934 return;
3936 if (mono) {
3937 if(tPtr->flags.rulerShown)
3938 WMShowTextRuler(tPtr, False);
3939 if(tPtr->flags.alignment != WALeft)
3940 tPtr->flags.alignment = WALeft;
3943 tPtr->flags.monoFont = mono;
3944 textDidResize(tPtr->view->delegate, tPtr->view);
3947 Bool
3948 WMGetTextUsesMonoFont(WMText *tPtr)
3950 if (!tPtr)
3951 return True;
3952 return tPtr->flags.monoFont;
3956 void
3957 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3959 if (!tPtr)
3960 return;
3962 WMReleaseFont(tPtr->dFont);
3963 if (font)
3964 tPtr->dFont = WMRetainFont(font);
3965 else
3966 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3969 WMFont *
3970 WMGetTextDefaultFont(WMText *tPtr)
3972 if (!tPtr)
3973 return NULL;
3974 else
3975 return WMRetainFont(tPtr->dFont);
3978 void
3979 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3981 if (!tPtr)
3982 return;
3984 WMReleaseColor(tPtr->dColor);
3985 if (color)
3986 tPtr->dColor = WMRetainColor(color);
3987 else
3988 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3991 WMColor *
3992 WMGetTextDefaultColor(WMText *tPtr)
3994 if (!tPtr)
3995 return NULL;
3996 else
3997 return WMRetainColor(tPtr->dColor);
4000 void
4001 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
4003 if (!tPtr)
4004 return;
4005 if(tPtr->flags.monoFont)
4006 tPtr->flags.alignment = WALeft;
4007 else
4008 tPtr->flags.alignment = alignment;
4009 WMThawText(tPtr);
4012 int
4013 WMGetTextInsertType(WMText *tPtr)
4015 if (!tPtr)
4016 return 0;
4017 return tPtr->flags.prepend;
4021 void
4022 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
4024 if (!tPtr || !color)
4025 return;
4027 setSelectionProperty(tPtr, NULL, color, -1);
4030 WMColor *
4031 WMGetTextSelectionColor(WMText *tPtr)
4033 TextBlock *tb;
4035 if (!tPtr)
4036 return NULL;
4038 tb = tPtr->currentTextBlock;
4040 if (!tb || !tPtr->flags.ownsSelection)
4041 return NULL;
4043 if(!tb->selected)
4044 return NULL;
4046 return tb->color;
4050 void
4051 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4053 if (!tPtr || !font)
4054 return;
4056 setSelectionProperty(tPtr, font, NULL, -1) ;
4059 WMFont *
4060 WMGetTextSelectionFont(WMText *tPtr)
4062 TextBlock *tb;
4064 if (!tPtr)
4065 return NULL;
4067 tb = tPtr->currentTextBlock;
4069 if (!tb || !tPtr->flags.ownsSelection)
4070 return NULL;
4072 if(!tb->selected)
4073 return NULL;
4075 if(tb->graphic) {
4076 tb = getFirstNonGraphicBlockFor(tb, 1);
4077 if(!tb)
4078 return NULL;
4080 return (tb->selected ? tb->d.font : NULL);
4084 void
4085 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4087 if (!tPtr || (underlined!=0 && underlined !=1))
4088 return;
4090 setSelectionProperty(tPtr, NULL, NULL, underlined);
4094 int
4095 WMGetTextSelectionUnderlined(WMText *tPtr)
4097 TextBlock *tb;
4099 if (!tPtr)
4100 return 0;
4102 tb = tPtr->currentTextBlock;
4104 if (!tb || !tPtr->flags.ownsSelection)
4105 return 0;
4107 if(!tb->selected)
4108 return 0;
4110 return tb->underlined;
4114 void
4115 WMFreezeText(WMText *tPtr)
4117 if (!tPtr)
4118 return;
4120 tPtr->flags.frozen = True;
4124 void
4125 WMThawText(WMText *tPtr)
4127 if (!tPtr)
4128 return;
4130 tPtr->flags.frozen = False;
4132 if(tPtr->flags.monoFont) {
4133 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4134 TextBlock *tb;
4136 /* make sure to unmap widgets no matter where they are */
4137 /* they'll be later remapped if needed by paintText */
4138 for(j=0; j<c; j++) {
4139 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4140 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4141 WMUnmapWidget(tb->d.widget);
4147 tPtr->flags.laidOut = False;
4148 layOutDocument(tPtr);
4149 updateScrollers(tPtr);
4150 paintText(tPtr);
4151 tPtr->flags.needsLayOut = False;
4155 /* find first occurence of a string */
4156 static char *
4157 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4158 Bool caseSensitive)
4160 char *ptr;
4162 if(!haystack || !needle || !end)
4163 return NULL;
4165 for (ptr = haystack; ptr < end; ptr++) {
4166 if(caseSensitive) {
4167 if (*ptr == *needle && !strncmp(ptr, needle, len))
4168 return ptr;
4170 } else {
4171 if (tolower(*ptr) == tolower(*needle) &&
4172 !strncasecmp(ptr, needle, len))
4173 return ptr;
4177 return NULL;
4180 /* find last occurence of a string */
4181 static char *
4182 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4183 Bool caseSensitive)
4185 char *ptr;
4187 if(!haystack || !needle || !end)
4188 return NULL;
4190 for (ptr = haystack-2; ptr > end; ptr--) {
4191 if(caseSensitive) {
4192 if (*ptr == *needle && !strncmp(ptr, needle, len))
4193 return ptr;
4194 } else {
4195 if (tolower(*ptr) == tolower(*needle) &&
4196 !strncasecmp(ptr, needle, len))
4197 return ptr;
4201 return NULL;
4205 Bool
4206 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4207 Bool caseSensitive)
4209 TextBlock *tb;
4210 char *mark=NULL;
4211 unsigned short pos;
4213 if (!tPtr || !needle)
4214 return False;
4216 #if 0
4217 if (! (tb = tPtr->currentTextBlock)) {
4218 if (! (tb = ( (direction > 0) ?
4219 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4220 return False;
4222 } else {
4223 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4224 tb = (direction>0) ? tb->next : tb->prior; */
4225 if(tb != tPtr->lastTextBlock)
4226 tb = tb->prior;
4228 #endif
4229 tb = tPtr->currentTextBlock;
4230 pos = tPtr->tpos;
4233 while(tb) {
4234 if (!tb->graphic) {
4236 if(direction > 0) {
4237 if(pos+1 < tb->used)
4238 pos++;
4240 if(tb->used - pos> 0 && pos > 0) {
4241 mark = mystrstr(&tb->text[pos], needle,
4242 strlen(needle), &tb->text[tb->used], caseSensitive);
4244 } else {
4245 tb = tb->next;
4246 pos = 0;
4247 continue;
4250 } else {
4251 if(pos-1 > 0)
4252 pos--;
4254 if(pos > 0) {
4255 mark = mystrrstr(&tb->text[pos], needle,
4256 strlen(needle), tb->text, caseSensitive);
4257 } else {
4258 tb = tb->prior;
4259 if(!tb)
4260 return False;
4261 pos = tb->used;
4262 continue;
4267 if(mark) {
4268 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4270 tPtr->tpos = (int)(mark - tb->text);
4271 tPtr->currentTextBlock = tb;
4272 updateCursorPosition(tPtr);
4273 tPtr->sel.y = tPtr->cursor.y+5;
4274 tPtr->sel.h = tPtr->cursor.h-10;
4275 tPtr->sel.x = tPtr->cursor.x +1;
4276 tPtr->sel.w = WMIN(WMWidthOfString(font,
4277 &tb->text[tPtr->tpos], strlen(needle)),
4278 tPtr->docWidth - tPtr->sel.x);
4279 tPtr->flags.ownsSelection = True;
4280 paintText(tPtr);
4282 return True;
4286 tb = (direction>0) ? tb->next : tb->prior;
4287 if(tb) {
4288 pos = (direction>0) ? 0 : tb->used;
4292 return False;
4296 Bool
4297 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4299 if (!tPtr)
4300 return False;
4302 if (!tPtr->flags.ownsSelection)
4303 return False;
4305 removeSelection(tPtr);
4307 if(replacement) {
4308 insertTextInteractively(tPtr, replacement, strlen(replacement));
4309 updateCursorPosition(tPtr);
4310 paintText(tPtr);
4313 return True;