now when a button click "links" to another page, instead of it
[wmaker-crm.git] / WINGs / wtext.c
blobd99bfc78b48c011db3639196c1d6c4d1e23c93ca
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 /* just to print blocks of text not terminated by \0 */
182 static void
183 output(char *ptr, int len)
185 char s[len+1];
186 memcpy(s, ptr, len);
187 s[len] = 0;
188 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
189 printf("[%s]\n", s);
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
199 #define STIPPLE_WIDTH 8
200 #define STIPPLE_HEIGHT 8
201 static unsigned char STIPPLE_BITS[] = {
202 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
207 static char *default_bullet[] = {
208 "6 6 4 1",
209 " c None s None", ". c black",
210 "X c white", "o c #808080",
211 " ... ",
212 ".XX.. ",
213 ".XX..o",
214 ".....o",
215 " ...oo",
216 " ooo "};
219 static void handleEvents(XEvent *event, void *data);
220 static void layOutDocument(Text *tPtr);
221 static void updateScrollers(Text *tPtr);
224 static int
225 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
227 unsigned int i=0;
229 for(i=0; i < tPtr->nMargins; i++) {
231 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
232 return i;
235 return -1;
240 static int
241 newMargin(Text *tPtr, WMRulerMargins *margins)
243 int n;
245 if (!margins) {
246 tPtr->margins[0].retainCount++;
247 return 0;
250 n = getMarginNumber(tPtr, margins);
252 if (n == -1) {
254 if(tPtr->nMargins >= 127) {
255 n = tPtr->nMargins-1;
256 return n;
259 tPtr->margins = wrealloc(tPtr->margins,
260 (++tPtr->nMargins)*sizeof(WMRulerMargins));
262 n = tPtr->nMargins-1;
263 tPtr->margins[n].left = margins->left;
264 tPtr->margins[n].first = margins->first;
265 tPtr->margins[n].body = margins->body;
266 tPtr->margins[n].right = margins->right;
267 /* for each tab... */
268 tPtr->margins[n].retainCount = 1;
269 } else {
270 tPtr->margins[n].retainCount++;
273 return n;
276 static Bool
277 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
279 unsigned short i, w, lw, selected = False, extend = False;
280 myRect sel;
283 /* if selection rectangle completely encloses the section */
284 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
285 && (tb->sections[s]._y + tb->sections[s].h
286 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
287 sel.x = 0;
288 sel.w = tPtr->visible.w;
289 selected = extend = True;
291 /* or if it starts on a line and then goes further down */
292 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
293 && (tb->sections[s]._y + tb->sections[s].h
294 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
295 && (tb->sections[s]._y + tb->sections[s].h
296 >= tPtr->visible.y + tPtr->sel.y) ) {
297 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
298 sel.w = tPtr->visible.w;
299 selected = extend = True;
301 /* or if it begins before a line, but ends on it */
302 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
303 && (tb->sections[s]._y + tb->sections[s].h
304 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
305 && (tb->sections[s]._y
306 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
308 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
309 sel.w = tPtr->sel.x + tPtr->sel.w;
310 else
311 sel.w = tPtr->sel.x;
313 sel.x = 0;
314 selected = True;
316 /* or if the selection rectangle lies entirely within a line */
317 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
318 && (tPtr->sel.w >= 2)
319 && (tb->sections[s]._y + tb->sections[s].h
320 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
321 sel.x = tPtr->sel.x;
322 sel.w = tPtr->sel.w;
323 selected = True;
326 if (selected) {
327 selected = False;
329 /* if not within (modified) selection rectangle */
330 if ( tb->sections[s].x > sel.x + sel.w
331 || tb->sections[s].x + tb->sections[s].w < sel.x)
332 return False;
334 if (tb->graphic) {
335 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
336 && tb->sections[s].x >= sel.x) {
337 rect->width = tb->sections[s].w;
338 rect->x = tb->sections[s].x;
339 selected = True;
341 } else {
343 i = tb->sections[s].begin;
344 lw = 0;
346 if (0&& tb->sections[s].x >= sel.x) {
347 tb->s_begin = tb->sections[s].begin;
348 goto _selEnd;
351 while (++i <= tb->sections[s].end) {
353 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
354 lw += w;
356 if (lw + tb->sections[s].x >= sel.x
357 || i == tb->sections[s].end ) {
358 lw -= w;
359 i--;
360 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
361 break;
365 if (i > tb->sections[s].end) {
366 printf("WasSelected: (i > tb->sections[s].end) \n");
367 return False;
370 _selEnd: rect->x = tb->sections[s].x + lw;
371 lw = 0;
372 while(++i <= tb->sections[s].end) {
374 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
375 lw += w;
377 if (lw + rect->x >= sel.x + sel.w
378 || i == tb->sections[s].end ) {
380 if (i != tb->sections[s].end) {
381 lw -= w;
382 i--;
385 rect->width = lw;
386 if (tb->sections[s].last && sel.x + sel.w
387 >= tb->sections[s].x + tb->sections[s].w
388 && extend ) {
389 rect->width += (tPtr->visible.w - rect->x - lw);
392 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
393 selected = True;
394 break;
400 if (selected) {
401 rect->y = tb->sections[s]._y - tPtr->vpos;
402 rect->height = tb->sections[s].h;
403 if(tb->graphic) { printf("DEBUG: graphic s%d h%d\n", s,tb->sections[s].h);}
405 return selected;
409 static void
410 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
412 TextBlock *tb;
413 int isFont=False;
415 tb = tPtr->firstTextBlock;
416 if (!tb || !tPtr->flags.ownsSelection)
417 return;
419 if(font && (!color || underlined==-1))
420 isFont = True;
422 while (tb) {
423 if (tPtr->flags.monoFont || tb->selected) {
425 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
426 || tb->graphic) {
428 if(isFont) {
429 if(!tb->graphic) {
430 WMReleaseFont(tb->d.font);
431 tb->d.font = WMRetainFont(font);
433 } else if(underlined !=-1) {
434 tb->underlined = underlined;
435 } else {
436 WMReleaseColor(tb->color);
437 tb->color = WMRetainColor(color);
440 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
442 TextBlock *midtb, *otb = tb;
444 if(underlined != -1) {
445 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
446 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
447 False, (tb->s_end - tb->s_begin));
448 } else {
449 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
450 &(tb->text[tb->s_begin]),
451 (isFont?font:tb->d.font),
452 (isFont?tb->color:color),
453 False, (tb->s_end - tb->s_begin));
457 if (midtb) {
458 if(underlined != -1) {
459 midtb->underlined = underlined;
460 } else {
461 midtb->underlined = otb->underlined;
464 midtb->selected = !True;
465 midtb->s_begin = 0;
466 midtb->s_end = midtb->used;
467 tPtr->currentTextBlock = tb;
468 WMAppendTextBlock(tPtr, midtb);
469 tb = tPtr->currentTextBlock;
472 if (otb->used - otb->s_end > 0) {
473 TextBlock *ntb;
474 ntb = (TextBlock *)
475 WMCreateTextBlockWithText(tPtr,
476 &(otb->text[otb->s_end]), otb->d.font, otb->color,
477 False, otb->used - otb->s_end);
479 if (ntb) {
480 ntb->underlined = otb->underlined;
481 ntb->selected = False;
482 WMAppendTextBlock(tPtr, ntb);
483 tb = tPtr->currentTextBlock;
487 if (midtb) {
488 tPtr->currentTextBlock = midtb;
491 otb->selected = False;
492 otb->used = otb->s_begin;
496 tb = tb->next;
499 tPtr->flags.needsLayOut = True;
500 WMThawText(tPtr);
502 /* in case the size changed... */
503 if(isFont && tPtr->currentTextBlock) {
504 TextBlock *tb = tPtr->currentTextBlock;
506 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
507 tPtr->sel.y = 3 + tb->sections[0]._y;
508 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
509 tPtr->sel.w = tb->sections[tb->nsections-1].w;
510 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
511 tPtr->sel.x = 0;
513 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
519 static Bool
520 removeSelection(Text *tPtr)
522 TextBlock *tb = NULL;
523 Bool first = False;
525 if (!(tb = tPtr->firstTextBlock))
526 return False;
528 while (tb) {
529 if (tb->selected) {
530 if(!first && !tb->graphic) {
531 WMReleaseFont(tPtr->dFont);
532 tPtr->dFont = WMRetainFont(tb->d.font);
533 first = True;
536 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
537 tPtr->currentTextBlock = tb;
538 if(tb->next) {
539 tPtr->tpos = 0;
540 } else if(tb->prior) {
541 if(tb->prior->graphic)
542 tPtr->tpos = 1;
543 else
544 tPtr->tpos = tb->prior->used;
545 } else tPtr->tpos = 0;
547 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
548 tb = tPtr->currentTextBlock;
549 continue;
551 } else if (tb->s_end <= tb->used) {
552 memmove(&(tb->text[tb->s_begin]),
553 &(tb->text[tb->s_end]), tb->used - tb->s_end);
554 tb->used -= (tb->s_end - tb->s_begin);
555 tb->selected = False;
556 tPtr->tpos = tb->s_begin;
561 tb = tb->next;
563 return True;
566 static TextBlock *
567 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
569 TextBlock *hold = tb;
571 if (!tb)
572 return NULL;
574 while (tb) {
575 if (!tb->graphic)
576 break;
577 tb = (dir? tb->next : tb->prior);
580 if(!tb) {
581 tb = hold;
582 while (tb) {
583 if (!tb->graphic)
584 break;
585 tb = (dir? tb->prior : tb->next);
589 if(!tb)
590 return NULL;
592 return tb;
596 static Bool
597 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
598 TextBlock *tb)
600 if (tPtr->flags.monoFont && tb->graphic) {
601 tb = getFirstNonGraphicBlockFor(tb, *dir);
602 if(!tb)
603 return 0;
605 if (tb->graphic) {
606 tPtr->currentTextBlock =
607 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
608 tPtr->tpos = 0;
609 return 0;
614 if(!tb->sections)
615 layOutDocument(tPtr);
616 if(!tb->sections)
617 return 0;
619 *dir = !(y <= tb->sections[0].y);
620 if(*dir) {
621 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
622 && (y >= tb->sections[0]._y ) ) {
623 /* if it's on the same line */
624 if(x < tb->sections[0].x)
625 *dir = 0;
627 } else {
628 if ( ( y <= tb->sections[tb->nsections-1]._y
629 + tb->sections[tb->nsections-1].h )
630 && (y >= tb->sections[tb->nsections-1]._y ) ) {
631 /* if it's on the same line */
632 if(x > tb->sections[tb->nsections-1].x)
633 *dir = 1;
637 return 1;
641 static void
642 paintText(Text *tPtr)
644 TextBlock *tb;
645 WMFont *font;
646 GC gc, greyGC;
647 char *text;
648 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
649 WMScreen *scr = tPtr->view->screen;
650 Display *dpy = tPtr->view->screen->display;
651 Window win = tPtr->view->window;
652 WMColor *color;
654 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
655 return;
658 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
659 0, 0, tPtr->visible.w, tPtr->visible.h);
661 if (tPtr->bgPixmap) {
662 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
663 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
664 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
667 if (! (tb = tPtr->currentTextBlock)) {
668 if (! (tb = tPtr->firstTextBlock)) {
669 goto _copy_area;
673 if (tPtr->flags.ownsSelection) {
674 color = WMGrayColor(scr);
675 greyGC = WMColorGC(color);
676 WMReleaseColor(color);
679 done = False;
683 /* first, which direction? Don't waste time looking all over,
684 since the parts to be drawn will most likely be near what
685 was previously drawn */
686 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
687 goto _copy_area;
689 while(tb) {
691 if (tb->graphic && tPtr->flags.monoFont)
692 goto _getSibling;
694 if(dir) {
695 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
696 + tb->sections[tb->nsections-1].h)
697 break;
698 } else {
699 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
700 + tb->sections[tb->nsections-1].h)
701 break;
704 _getSibling:
705 if(dir) {
706 if(tb->next)
707 tb = tb->next;
708 else break;
709 } else {
710 if(tb->prior)
711 tb = tb->prior;
712 else break;
717 /* first, place all text that can be viewed */
718 while (!done && tb) {
721 if (tb->graphic) {
722 tb = tb->next;
723 continue;
726 tb->selected = False;
728 for(s=0; s<tb->nsections && !done; s++) {
730 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
731 done = True;
732 break;
735 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
736 continue;
738 if (tPtr->flags.monoFont) {
739 font = tPtr->dFont;
740 gc = tPtr->fgGC;
741 } else {
742 font = tb->d.font;
743 gc = WMColorGC(tb->color);
746 if (tPtr->flags.ownsSelection) {
747 XRectangle rect;
749 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
750 tb->selected = True;
751 XFillRectangle(dpy, tPtr->db, greyGC,
752 rect.x, rect.y, rect.width, rect.height);
756 prev_y = tb->sections[s]._y;
758 len = tb->sections[s].end - tb->sections[s].begin;
759 text = &(tb->text[tb->sections[s].begin]);
760 y = tb->sections[s].y - tPtr->vpos;
761 WMDrawString(scr, tPtr->db, gc, font,
762 tb->sections[s].x - tPtr->hpos, y, text, len);
764 if (!tPtr->flags.monoFont && tb->underlined) {
765 XDrawLine(dpy, tPtr->db, gc,
766 tb->sections[s].x - tPtr->hpos,
767 y + font->y + 1,
768 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
769 y + font->y + 1);
774 tb = (!done? tb->next : NULL);
778 /* now , show all graphic items that can be viewed */
779 c = WMGetArrayItemCount(tPtr->gfxItems);
780 if (c > 0 && !tPtr->flags.monoFont) {
781 int j, h;
783 for(j=0; j<c; j++) {
784 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
786 /* if it's not viewable, and mapped, unmap it */
787 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
788 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
790 if(tb->object) {
791 if ((W_VIEW(tb->d.widget))->flags.mapped) {
792 WMUnmapWidget(tb->d.widget);
795 } else {
796 /* if it's viewable, and not mapped, map it */
797 if(tb->object) {
798 W_View *view = W_VIEW(tb->d.widget);
800 if (!view->flags.realized)
801 WMRealizeWidget(tb->d.widget);
802 if(!view->flags.mapped) {
803 XMapWindow(view->screen->display, view->window);
804 XFlush(view->screen->display);
805 view->flags.mapped = 1;
809 if(tb->object) {
810 WMMoveWidget(tb->d.widget,
811 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
812 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
813 h = WMWidgetHeight(tb->d.widget) + 1;
815 } else {
816 WMDrawPixmap(tb->d.pixmap, tPtr->db,
817 tb->sections[0].x - tPtr->hpos,
818 tb->sections[0].y - tPtr->vpos);
819 h = tb->d.pixmap->height + 1;
823 if (tPtr->flags.ownsSelection) {
824 XRectangle rect;
826 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
827 Drawable d = (0&&tb->object?
828 (WMWidgetView(tb->d.widget))->window : tPtr->db);
830 tb->selected = True;
831 XFillRectangle(dpy, d, tPtr->stippledGC,
832 //XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,
833 rect.x, rect.y, rect.width, rect.height);
837 if (!tPtr->flags.monoFont && tb->underlined) {
838 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
839 tb->sections[0].x - tPtr->hpos,
840 tb->sections[0].y + h - tPtr->vpos,
841 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
842 tb->sections[0].y + h - tPtr->vpos);
849 _copy_area:
850 if (tPtr->flags.editable && tPtr->flags.cursorShown
851 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
852 int y = tPtr->cursor.y - tPtr->vpos;
853 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
854 tPtr->cursor.x, y,
855 tPtr->cursor.x, y + tPtr->cursor.h);
858 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
859 tPtr->visible.w, tPtr->visible.h,
860 tPtr->visible.x, tPtr->visible.y);
862 W_DrawRelief(scr, win, 0, 0,
863 tPtr->view->size.width, tPtr->view->size.height,
864 tPtr->flags.relief);
866 if (tPtr->ruler && tPtr->flags.rulerShown)
867 XDrawLine(dpy, win, tPtr->fgGC,
868 2, 42, tPtr->view->size.width-4, 42);
872 static void
873 mouseOverObject(Text *tPtr, int x, int y)
875 TextBlock *tb;
876 Bool result = False;
878 x -= tPtr->visible.x;
879 x += tPtr->hpos;
880 y -= tPtr->visible.y;
881 y += tPtr->vpos;
883 if(tPtr->flags.ownsSelection) {
884 if(tPtr->sel.x <= x
885 && tPtr->sel.y <= y
886 && tPtr->sel.x + tPtr->sel.w >= x
887 && tPtr->sel.y + tPtr->sel.h >= y) {
888 tPtr->flags.isOverGraphic = 1;
889 result = True;
894 if(!result) {
895 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
897 if (c<1)
898 tPtr->flags.isOverGraphic = 0;
901 for(j=0; j<c; j++) {
902 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
904 if(!tb || !tb->sections) {
905 tPtr->flags.isOverGraphic = 0;
906 return;
909 if(!tb->object) {
910 if(tb->sections[0].x <= x
911 && tb->sections[0].y <= y
912 && tb->sections[0].x + tb->sections[0].w >= x
913 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
914 tPtr->flags.isOverGraphic = 3;
915 result = True;
916 break;
924 if(!result)
925 tPtr->flags.isOverGraphic = 0;
928 tPtr->view->attribs.cursor = (result?
929 tPtr->view->screen->defaultCursor
930 : tPtr->view->screen->textCursor);
932 XSetWindowAttributes attribs;
933 attribs.cursor = tPtr->view->attribs.cursor;
934 XChangeWindowAttributes(tPtr->view->screen->display,
935 tPtr->view->window, CWCursor,
936 &attribs);
940 #if DO_BLINK
942 static void
943 blinkCursor(void *data)
945 Text *tPtr = (Text*)data;
947 if (tPtr->flags.cursorShown) {
948 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
949 blinkCursor, data);
950 } else {
951 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
952 blinkCursor, data);
954 paintText(tPtr);
955 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
957 #endif
959 static void
960 updateCursorPosition(Text *tPtr)
962 TextBlock *tb = NULL;
963 int x, y, h, s;
965 if(tPtr->flags.needsLayOut)
966 layOutDocument(tPtr);
968 if (! (tb = tPtr->currentTextBlock)) {
969 if (! (tb = tPtr->firstTextBlock)) {
970 WMFont *font = tPtr->dFont;
971 tPtr->tpos = 0;
972 tPtr->cursor.h = font->height + abs(font->height-font->y);
974 tPtr->cursor.y = 2;
975 tPtr->cursor.x = 2;
976 return;
981 if(tb->blank) {
982 tPtr->tpos = 0;
983 y = tb->sections[0].y;
984 h = tb->sections[0].h;
985 x = tb->sections[0].x;
987 } else if(tb->graphic) {
988 y = tb->sections[0].y;
989 h = tb->sections[0].h;
990 x = tb->sections[0].x;
991 if(tPtr->tpos == 1)
992 x += tb->sections[0].w;
994 } else {
995 if(tPtr->tpos > tb->used)
996 tPtr->tpos = tb->used;
998 for(s=0; s<tb->nsections-1; s++) {
1000 if(tPtr->tpos >= tb->sections[s].begin
1001 && tPtr->tpos <= tb->sections[s].end)
1002 break;
1005 y = tb->sections[s]._y;
1006 h = tb->sections[s].h;
1007 x = tb->sections[s].x + WMWidthOfString(
1008 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
1009 &tb->text[tb->sections[s].begin],
1010 tPtr->tpos - tb->sections[s].begin);
1013 tPtr->cursor.y = y;
1014 tPtr->cursor.h = h;
1015 tPtr->cursor.x = x;
1018 /* scroll the bars if the cursor is not visible */
1019 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1020 if(tPtr->cursor.y+tPtr->cursor.h
1021 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1022 tPtr->vpos +=
1023 (tPtr->cursor.y+tPtr->cursor.h+10
1024 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1025 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1026 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1031 updateScrollers(tPtr);
1035 static void
1036 cursorToTextPosition(Text *tPtr, int x, int y)
1038 TextBlock *tb = NULL;
1039 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1040 char *text;
1042 if(tPtr->flags.needsLayOut)
1043 layOutDocument(tPtr);
1045 y += (tPtr->vpos - tPtr->visible.y);
1046 if (y<0)
1047 y = 0;
1049 x -= (tPtr->visible.x - 2);
1050 if (x<0)
1051 x=0;
1053 /* clicked is relative to document, not window... */
1054 tPtr->clicked.x = x;
1055 tPtr->clicked.y = y;
1057 if (! (tb = tPtr->currentTextBlock)) {
1058 if (! (tb = tPtr->firstTextBlock)) {
1059 WMFont *font = tPtr->dFont;
1060 tPtr->tpos = 0;
1061 tPtr->cursor.h = font->height + abs(font->height-font->y);
1062 tPtr->cursor.y = 2;
1063 tPtr->cursor.x = 2;
1064 return;
1068 /* first, which direction? Most likely, newly clicked
1069 position will be close to previous */
1070 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1071 return;
1074 s = (dir? 0 : tb->nsections-1);
1075 if ( y >= tb->sections[s]._y
1076 && y <= tb->sections[s]._y + tb->sections[s].h) {
1077 goto _doneV;
1080 /* get the first (or last) section of the TextBlock that
1081 lies about the vertical click point */
1082 done = False;
1083 while (!done && tb) {
1085 if (tPtr->flags.monoFont && tb->graphic) {
1086 if( (dir?tb->next:tb->prior))
1087 tb = (dir?tb->next:tb->prior);
1088 continue;
1091 s = (dir? 0 : tb->nsections-1);
1092 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1094 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1095 ( y >= tb->sections[s]._y ) ) ) {
1096 done = True;
1097 } else {
1098 dir? s++ : s--;
1102 if (!done) {
1103 if ( (dir? tb->next : tb->prior)) {
1104 tb = (dir ? tb->next : tb->prior);
1105 } else {
1106 pos = tb->used;
1107 break; /* goto _doneH; */
1113 if (s<0 || s>=tb->nsections) {
1114 s = (dir? tb->nsections-1 : 0);
1117 _doneV:
1118 /* we have the line, which TextBlock on that line is it? */
1119 pos = (dir?0:tb->sections[s].begin);
1120 if (tPtr->flags.monoFont && tb->graphic) {
1121 TextBlock *hold = tb;
1122 tb = getFirstNonGraphicBlockFor(hold, dir);
1124 if(!tb) {
1125 tPtr->tpos = 0;
1126 tb = hold;
1127 s = 0;
1128 goto _doNothing;
1133 if(tb->blank)
1134 _w = 0;
1136 _y = tb->sections[s]._y;
1138 while (tb) {
1140 if (tPtr->flags.monoFont && tb->graphic) {
1141 tb = (dir ? tb->next : tb->prior);
1142 continue;
1145 if (dir) {
1146 if (tb->graphic) {
1147 if(tb->object)
1148 _w = WMWidgetWidth(tb->d.widget)-5;
1149 else
1150 _w = tb->d.pixmap->width-5;
1152 if (tb->sections[0].x + _w >= x)
1153 break;
1154 } else {
1155 text = &(tb->text[tb->sections[s].begin]);
1156 len = tb->sections[s].end - tb->sections[s].begin;
1157 _w = WMWidthOfString(tb->d.font, text, len);
1158 if (tb->sections[s].x + _w >= x)
1159 break;
1162 } else {
1163 if (tb->sections[s].x <= x)
1164 break;
1167 if ((dir? tb->next : tb->prior)) {
1168 TextBlock *nxt = (dir? tb->next : tb->prior);
1169 if (tPtr->flags.monoFont && nxt->graphic) {
1170 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1171 if (!nxt) {
1172 pos = (dir?0:tb->sections[s].begin);
1173 tPtr->cursor.x = tb->sections[s].x;
1174 goto _doneH;
1178 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1179 /* this must be the last/first on this line. stop */
1180 pos = (dir? tb->sections[s].end : 0);
1181 tPtr->cursor.x = tb->sections[s].x;
1182 if (!tb->blank) {
1183 if (tb->graphic) {
1184 if(tb->object)
1185 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1186 else
1187 tPtr->cursor.x += tb->d.pixmap->width;
1188 } else if (pos > tb->sections[s].begin) {
1189 tPtr->cursor.x +=
1190 WMWidthOfString(tb->d.font,
1191 &(tb->text[tb->sections[s].begin]),
1192 pos - tb->sections[s].begin);
1195 goto _doneH;
1199 if ( (dir? tb->next : tb->prior)) {
1200 tb = (dir ? tb->next : tb->prior);
1201 } else {
1202 done = True;
1203 break;
1206 if (tb)
1207 s = (dir? 0 : tb->nsections-1);
1210 /* we have said TextBlock, now where within it? */
1211 if (tb) {
1212 if(tb->graphic) {
1213 int gw = (tb->object ?
1214 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1216 tPtr->cursor.x = tb->sections[0].x;
1218 if(x > tPtr->cursor.x + gw/2) {
1219 pos = 1;
1220 tPtr->cursor.x += gw;
1221 } else {
1222 printf("first %d\n", tb->first);
1223 if(tb->prior) {
1224 if(tb->prior->graphic) pos = 1;
1225 else pos = tb->prior->used;
1226 tb = tb->prior;
1227 } else pos = 0;
1231 s = 0;
1232 goto _doneH;
1234 } else {
1235 WMFont *f = tb->d.font;
1236 len = tb->sections[s].end - tb->sections[s].begin;
1237 text = &(tb->text[tb->sections[s].begin]);
1239 _w = x - tb->sections[s].x;
1240 pos = 0;
1242 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1243 pos++;
1245 tPtr->cursor.x = tb->sections[s].x +
1246 (pos? WMWidthOfString(f, text, pos) : 0);
1248 pos += tb->sections[s].begin;
1252 _doneH:
1253 if(tb->graphic) {
1254 tPtr->tpos = (pos<=1)? pos : 0;
1255 } else {
1256 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1258 _doNothing:
1259 if (!tb)
1260 printf("...for this app will surely crash :-)\n");
1262 tPtr->currentTextBlock = tb;
1263 tPtr->cursor.h = tb->sections[s].h;
1264 tPtr->cursor.y = tb->sections[s]._y;
1266 /* scroll the bars if the cursor is not visible */
1267 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1268 if(tPtr->cursor.y+tPtr->cursor.h
1269 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1270 tPtr->vpos +=
1271 (tPtr->cursor.y+tPtr->cursor.h+10
1272 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1273 updateScrollers(tPtr);
1274 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1275 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1276 updateScrollers(tPtr);
1284 static void
1285 updateScrollers(Text *tPtr)
1288 if (tPtr->flags.frozen)
1289 return;
1291 if (tPtr->vS) {
1292 if (tPtr->docHeight < tPtr->visible.h) {
1293 WMSetScrollerParameters(tPtr->vS, 0, 1);
1294 tPtr->vpos = 0;
1295 } else {
1296 float hmax = (float)(tPtr->docHeight);
1297 WMSetScrollerParameters(tPtr->vS,
1298 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1299 (float)tPtr->visible.h/hmax);
1301 } else tPtr->vpos = 0;
1303 if (tPtr->hS) {
1304 if (tPtr->docWidth < tPtr->visible.w) {
1305 WMSetScrollerParameters(tPtr->hS, 0, 1);
1306 tPtr->hpos = 0;
1307 } else {
1308 float wmax = (float)(tPtr->docWidth);
1309 WMSetScrollerParameters(tPtr->hS,
1310 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1311 (float)tPtr->visible.w/wmax);
1313 } else tPtr->hpos = 0;
1316 static void
1317 scrollersCallBack(WMWidget *w, void *self)
1319 Text *tPtr = (Text *)self;
1320 Bool scroll = False;
1321 int which;
1323 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1324 return;
1326 if (w == tPtr->vS) {
1327 int height;
1328 height = tPtr->visible.h;
1330 which = WMGetScrollerHitPart(tPtr->vS);
1331 switch(which) {
1333 case WSDecrementLine:
1334 if (tPtr->vpos > 0) {
1335 if (tPtr->vpos>16) tPtr->vpos-=16;
1336 else tPtr->vpos=0;
1337 scroll=True;
1339 break;
1341 case WSIncrementLine: {
1342 int limit = tPtr->docHeight - height;
1343 if (tPtr->vpos < limit) {
1344 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1345 else tPtr->vpos=limit;
1346 scroll = True;
1349 break;
1351 case WSDecrementPage:
1352 if(((int)tPtr->vpos - (int)height) >= 0)
1353 tPtr->vpos -= height;
1354 else
1355 tPtr->vpos = 0;
1357 scroll = True;
1358 break;
1360 case WSIncrementPage:
1361 tPtr->vpos += height;
1362 if (tPtr->vpos > (tPtr->docHeight - height))
1363 tPtr->vpos = tPtr->docHeight - height;
1364 scroll = True;
1365 break;
1368 case WSKnob:
1369 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1370 * (float)(tPtr->docHeight - height);
1371 scroll = True;
1372 break;
1374 case WSKnobSlot:
1375 case WSNoPart:
1376 break;
1378 scroll = (tPtr->vpos != tPtr->prevVpos);
1379 tPtr->prevVpos = tPtr->vpos;
1383 if (w == tPtr->hS) {
1384 int width = tPtr->visible.w;
1386 which = WMGetScrollerHitPart(tPtr->hS);
1387 switch(which) {
1389 case WSDecrementLine:
1390 if (tPtr->hpos > 0) {
1391 if (tPtr->hpos>16) tPtr->hpos-=16;
1392 else tPtr->hpos=0;
1393 scroll=True;
1394 }break;
1396 case WSIncrementLine: {
1397 int limit = tPtr->docWidth - width;
1398 if (tPtr->hpos < limit) {
1399 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1400 else tPtr->hpos=limit;
1401 scroll = True;
1403 }break;
1405 case WSDecrementPage:
1406 if(((int)tPtr->hpos - (int)width) >= 0)
1407 tPtr->hpos -= width;
1408 else
1409 tPtr->hpos = 0;
1411 scroll = True;
1412 break;
1414 case WSIncrementPage:
1415 tPtr->hpos += width;
1416 if (tPtr->hpos > (tPtr->docWidth - width))
1417 tPtr->hpos = tPtr->docWidth - width;
1418 scroll = True;
1419 break;
1422 case WSKnob:
1423 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1424 * (float)(tPtr->docWidth - width);
1425 scroll = True;
1426 break;
1428 case WSKnobSlot:
1429 case WSNoPart:
1430 break;
1432 scroll = (tPtr->hpos != tPtr->prevHpos);
1433 tPtr->prevHpos = tPtr->hpos;
1436 if (scroll) {
1437 updateScrollers(tPtr);
1438 paintText(tPtr);
1444 typedef struct {
1445 TextBlock *tb;
1446 unsigned short begin, end; /* what part of the text block */
1447 } myLineItems;
1450 static int
1451 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1453 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1454 WMFont *font;
1455 char *text;
1456 TextBlock *tb, *tbsame=NULL;
1458 if(!items || nitems == 0)
1459 return 0;
1461 for(i=0; i<nitems; i++) {
1462 tb = items[i].tb;
1464 if (tb->graphic) {
1465 if (!tPtr->flags.monoFont) {
1466 if(tb->object) {
1467 WMWidget *wdt = tb->d.widget;
1468 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1469 if (tPtr->flags.alignment != WALeft)
1470 lw += WMWidgetWidth(wdt);
1471 } else {
1472 line_height = WMAX(line_height,
1473 tb->d.pixmap->height + max_d);
1474 if (tPtr->flags.alignment != WALeft)
1475 lw += tb->d.pixmap->width;
1479 } else {
1480 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1481 max_d = WMAX(max_d, abs(font->height-font->y));
1482 line_height = WMAX(line_height, font->height + max_d);
1483 text = &(tb->text[items[i].begin]);
1484 len = items[i].end - items[i].begin;
1485 if (tPtr->flags.alignment != WALeft)
1486 lw += WMWidthOfString(font, text, len);
1490 if (tPtr->flags.alignment == WARight) {
1491 j = tPtr->visible.w - lw;
1492 } else if (tPtr->flags.alignment == WACenter) {
1493 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1496 for(i=0; i<nitems; i++) {
1497 tb = items[i].tb;
1499 if (tbsame == tb) { /* extend it, since it's on same line */
1500 tb->sections[tb->nsections-1].end = items[i].end;
1501 n = tb->nsections-1;
1502 } else {
1503 tb->sections = wrealloc(tb->sections,
1504 (++tb->nsections)*sizeof(Section));
1505 n = tb->nsections-1;
1506 tb->sections[n]._y = y + max_d;
1507 tb->sections[n].max_d = max_d;
1508 tb->sections[n].x = x+j;
1509 tb->sections[n].h = line_height;
1510 tb->sections[n].begin = items[i].begin;
1511 tb->sections[n].end = items[i].end;
1514 tb->sections[n].last = (i+1 == nitems);
1516 if (tb->graphic) {
1517 if (!tPtr->flags.monoFont) {
1518 if(tb->object) {
1519 WMWidget *wdt = tb->d.widget;
1520 tb->sections[n].y = max_d + y
1521 + line_height - WMWidgetHeight(wdt);
1522 tb->sections[n].w = WMWidgetWidth(wdt);
1523 } else {
1524 tb->sections[n].y = y + line_height
1525 + max_d - tb->d.pixmap->height;
1526 tb->sections[n].w = tb->d.pixmap->width;
1528 x += tb->sections[n].w;
1530 } else {
1531 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1532 len = items[i].end - items[i].begin;
1533 text = &(tb->text[items[i].begin]);
1535 tb->sections[n].y = y+line_height-font->y;
1536 tb->sections[n].w =
1537 WMWidthOfString(font,
1538 &(tb->text[tb->sections[n].begin]),
1539 tb->sections[n].end - tb->sections[n].begin);
1541 x += WMWidthOfString(font, text, len);
1544 tbsame = tb;
1547 return line_height;
1552 static void
1553 layOutDocument(Text *tPtr)
1555 TextBlock *tb;
1556 myLineItems *items = NULL;
1557 unsigned int itemsSize=0, nitems=0, begin, end;
1558 WMFont *font;
1559 unsigned int x, y=0, lw = 0, width=0, bmargin;
1560 char *start=NULL, *mark=NULL;
1562 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1563 return;
1565 assert(tPtr->visible.w > 20);
1567 tPtr->docWidth = tPtr->visible.w;
1568 x = tPtr->margins[tb->marginN].first;
1569 bmargin = tPtr->margins[tb->marginN].body;
1571 /* only partial layOut needed: re-Lay only affected textblocks */
1572 if (tPtr->flags.laidOut) {
1573 tb = tPtr->currentTextBlock;
1575 /* search backwards for textblocks on same line */
1576 while (tb->prior) {
1577 if (!tb->sections || tb->nsections<1) {
1578 tb = tPtr->firstTextBlock;
1579 tPtr->flags.laidOut = False;
1580 y = 0;
1581 goto _layOut;
1584 if(!tb->prior->sections || tb->prior->nsections<1) {
1585 tb = tPtr->firstTextBlock;
1586 tPtr->flags.laidOut = False;
1587 y = 0;
1588 goto _layOut;
1591 if (tb->sections[0]._y !=
1592 tb->prior->sections[tb->prior->nsections-1]._y) {
1593 break;
1595 tb = tb->prior;
1598 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1599 y = tb->prior->sections[tb->prior->nsections-1]._y +
1600 tb->prior->sections[tb->prior->nsections-1].h -
1601 tb->prior->sections[tb->prior->nsections-1].max_d;
1602 } else {
1603 y = 0;
1607 _layOut:
1608 while (tb) {
1610 if (tb->sections && tb->nsections>0) {
1611 wfree(tb->sections);
1612 tb->sections = NULL;
1613 tb->nsections = 0;
1616 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1617 TextBlock *next = tb->next;
1618 tPtr->currentTextBlock = tb;
1619 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1620 tb = next;
1621 tb->first = True;
1622 continue;
1625 if (tb->first && tb != tPtr->firstTextBlock) {
1626 y += layOutLine(tPtr, items, nitems, x, y);
1627 x = tPtr->margins[tb->marginN].first;
1628 bmargin = tPtr->margins[tb->marginN].body;
1629 nitems = 0;
1630 lw = 0;
1633 if (tb->graphic) {
1634 if (!tPtr->flags.monoFont) {
1635 if(tb->object)
1636 width = WMWidgetWidth(tb->d.widget);
1637 else
1638 width = tb->d.pixmap->width;
1640 if (width > tPtr->docWidth)
1641 tPtr->docWidth = width;
1643 lw += width;
1644 if (lw >= tPtr->visible.w - x ) {
1645 y += layOutLine(tPtr, items, nitems, x, y);
1646 nitems = 0;
1647 x = bmargin;
1648 lw = width;
1651 if(nitems + 1> itemsSize) {
1652 items = wrealloc(items,
1653 (++itemsSize)*sizeof(myLineItems));
1656 items[nitems].tb = tb;
1657 items[nitems].begin = 0;
1658 items[nitems].end = 0;
1659 nitems++;
1662 } else if ((start = tb->text)) {
1663 begin = end = 0;
1664 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1666 while (start) {
1667 mark = strchr(start, ' ');
1668 if (mark) {
1669 end += (int)(mark-start)+1;
1670 start = mark+1;
1671 } else {
1672 end += strlen(start);
1673 start = mark;
1676 if (end > tb->used)
1677 end = tb->used;
1679 if (end-begin > 0) {
1681 width = WMWidthOfString(font,
1682 &tb->text[begin], end-begin);
1684 /* if it won't fit, char wrap it */
1685 if (width >= tPtr->visible.w) {
1686 char *t = &tb->text[begin];
1687 int l=end-begin, i=0;
1688 do {
1689 width = WMWidthOfString(font, t, ++i);
1690 } while (width < tPtr->visible.w && i < l);
1691 if(i>2) i--;
1692 end = begin+i;
1693 start = &tb->text[end];
1696 lw += width;
1699 if (lw >= tPtr->visible.w - x) {
1700 y += layOutLine(tPtr, items, nitems, x, y);
1701 lw = width;
1702 x = bmargin;
1703 nitems = 0;
1706 if(nitems + 1 > itemsSize) {
1707 items = wrealloc(items,
1708 (++itemsSize)*sizeof(myLineItems));
1711 items[nitems].tb = tb;
1712 items[nitems].begin = begin;
1713 items[nitems].end = end;
1714 nitems++;
1716 begin = end;
1721 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1722 if(0&&tPtr->flags.laidOut
1723 && tb->next && tb->next->sections && tb->next->nsections>0
1724 && (tPtr->vpos + tPtr->visible.h
1725 < tb->next->sections[0]._y)) {
1726 if(tPtr->lastTextBlock->sections
1727 && tPtr->lastTextBlock->nsections > 0 ) {
1728 TextBlock *ltb = tPtr->lastTextBlock;
1729 int ly = ltb->sections[ltb->nsections-1]._y;
1730 int lh = ltb->sections[ltb->nsections-1].h;
1731 int ss, sd;
1733 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1734 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1736 y += layOutLine(tPtr, items, nitems, x, y);
1737 ss= ly+lh-y;
1738 sd = tPtr->docHeight-y;
1740 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1741 y += tb->next->sections[0]._y-y;
1742 nitems = 0;
1743 printf("nitems%d\n", nitems);
1744 if(ss-sd!=0)
1745 y = tPtr->docHeight+ss-sd;
1747 break;
1748 } else {
1749 tPtr->flags.laidOut = False;
1753 tb = tb->next;
1757 if (nitems > 0)
1758 y += layOutLine(tPtr, items, nitems, x, y);
1760 if (tPtr->docHeight != y+10) {
1761 tPtr->docHeight = y+10;
1762 updateScrollers(tPtr);
1765 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1766 XEvent event;
1768 tPtr->flags.horizOnDemand = True;
1769 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1770 event.type = Expose;
1771 handleEvents(&event, (void *)tPtr);
1773 } else if(tPtr->docWidth <= tPtr->visible.w
1774 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1775 tPtr->flags.horizOnDemand = False;
1776 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1779 tPtr->flags.laidOut = True;
1781 if(items && itemsSize > 0)
1782 wfree(items);
1787 static void
1788 textDidResize(W_ViewDelegate *self, WMView *view)
1790 Text *tPtr = (Text *)view->self;
1791 unsigned short w = tPtr->view->size.width;
1792 unsigned short h = tPtr->view->size.height;
1793 unsigned short rh = 0, vw = 0, rel;
1795 rel = (tPtr->flags.relief == WRFlat);
1797 if (tPtr->ruler && tPtr->flags.rulerShown) {
1798 WMMoveWidget(tPtr->ruler, 2, 2);
1799 WMResizeWidget(tPtr->ruler, w - 4, 40);
1800 rh = 40;
1803 if (tPtr->vS) {
1804 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1805 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1806 vw = 20;
1807 WMSetRulerOffset(tPtr->ruler,22);
1808 } else WMSetRulerOffset(tPtr->ruler, 2);
1810 if (tPtr->hS) {
1811 if (tPtr->vS) {
1812 WMMoveWidget(tPtr->hS, vw, h - 21);
1813 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1814 } else {
1815 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1816 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1820 tPtr->visible.x = (tPtr->vS)?24:4;
1821 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1822 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1823 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1824 tPtr->visible.h -= (tPtr->hS)?20:0;
1825 tPtr->margins[0].right = tPtr->visible.w;
1827 if (tPtr->view->flags.realized) {
1829 if (tPtr->db) {
1830 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1831 tPtr->db = (Pixmap) NULL;
1834 if (tPtr->visible.w < 40)
1835 tPtr->visible.w = 40;
1836 if (tPtr->visible.h < 20)
1837 tPtr->visible.h = 20;
1839 if(!tPtr->db) {
1840 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1841 tPtr->view->window, tPtr->visible.w,
1842 tPtr->visible.h, tPtr->view->screen->depth);
1846 WMThawText(tPtr);
1849 W_ViewDelegate _TextViewDelegate =
1851 NULL,
1852 NULL,
1853 textDidResize,
1854 NULL,
1857 #define TEXT_BUFFER_INCR 8
1858 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1860 static void
1861 clearText(Text *tPtr)
1863 tPtr->vpos = tPtr->hpos = 0;
1864 tPtr->docHeight = tPtr->docWidth = 0;
1865 tPtr->cursor.x = -23;
1867 if (!tPtr->firstTextBlock)
1868 return;
1870 while (tPtr->currentTextBlock)
1871 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1873 tPtr->firstTextBlock = NULL;
1874 tPtr->currentTextBlock = NULL;
1875 tPtr->lastTextBlock = NULL;
1876 WMEmptyArray(tPtr->gfxItems);
1879 /* possibly remove a single character from the currentTextBlock,
1880 or if there's a selection, remove it...
1881 note that Delete and Backspace are treated differently */
1882 static void
1883 deleteTextInteractively(Text *tPtr, KeySym ksym)
1885 TextBlock *tb;
1886 Bool back = (Bool) (ksym == XK_BackSpace);
1887 Bool done = 1, wasFirst = 0;
1889 if (!tPtr->flags.editable)
1890 return;
1892 if ( !(tb = tPtr->currentTextBlock) )
1893 return;
1895 if (tPtr->flags.ownsSelection) {
1896 if(removeSelection(tPtr))
1897 layOutDocument(tPtr);
1898 return;
1901 wasFirst = tb->first;
1902 if (back && tPtr->tpos < 1) {
1903 if (tb->prior) {
1904 if(tb->prior->blank) {
1905 tPtr->currentTextBlock = tb->prior;
1906 WMRemoveTextBlock(tPtr);
1907 tPtr->currentTextBlock = tb;
1908 tb->first = True;
1909 layOutDocument(tPtr);
1910 return;
1911 } else {
1912 if(tb->blank) {
1913 TextBlock *prior = tb->prior;
1914 tPtr->currentTextBlock = tb;
1915 WMRemoveTextBlock(tPtr);
1916 tb = prior;
1917 } else {
1918 tb = tb->prior;
1921 if(tb->graphic)
1922 tPtr->tpos = 1;
1923 else
1924 tPtr->tpos = tb->used;
1926 tPtr->currentTextBlock = tb;
1927 done = 1;
1928 if(wasFirst) {
1929 if(tb->next)
1930 tb->next->first = False;
1931 layOutDocument(tPtr);
1932 return;
1938 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1939 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1940 if (back)
1941 tPtr->tpos--;
1942 memmove(&(tb->text[tPtr->tpos]),
1943 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1944 tb->used--;
1945 done = 0;
1948 /* if there are no characters left to back over in the textblock,
1949 but it still has characters to the right of the cursor: */
1950 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1951 || tb->graphic) {
1953 /* no more chars, and it's marked as blank? */
1954 if(tb->blank) {
1955 TextBlock *sibling = (back? tb->prior : tb->next);
1957 if(tb->used == 0 || tb->graphic)
1958 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1960 if (sibling) {
1961 tPtr->currentTextBlock = sibling;
1962 if(tb->graphic)
1963 tPtr->tpos = (back? 1 : 0);
1964 else
1965 tPtr->tpos = (back? sibling->used : 0);
1967 /* no more chars, so mark it as blank */
1968 } else if(tb->used == 0) {
1969 tb->blank = 1;
1970 } else if(tb->graphic) {
1971 Bool hasNext = (Bool)(tb->next);
1973 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1974 if(hasNext) {
1975 tPtr->tpos = 0;
1976 } else if(tPtr->currentTextBlock) {
1977 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1978 1 : tPtr->currentTextBlock->used);
1980 } else printf("DEBUG: unaccounted for... catch this!\n");
1983 layOutDocument(tPtr);
1987 static void
1988 insertTextInteractively(Text *tPtr, char *text, int len)
1990 TextBlock *tb;
1991 char *newline = NULL;
1993 if (!tPtr->flags.editable) {
1994 return;
1997 if (len < 1 || !text)
1998 return;
2001 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
2002 return;
2005 if (tPtr->flags.ownsSelection)
2006 removeSelection(tPtr);
2009 if (tPtr->flags.ignoreNewLine) {
2010 int i;
2011 for(i=0; i<len; i++) {
2012 if (text[i] == '\n')
2013 text[i] = ' ';
2017 tb = tPtr->currentTextBlock;
2018 if (!tb || tb->graphic) {
2019 tPtr->tpos = 0;
2020 WMAppendTextStream(tPtr, text);
2021 layOutDocument(tPtr);
2022 return;
2025 if ((newline = strchr(text, '\n'))) {
2026 int nlen = (int)(newline-text);
2027 int s = tb->used - tPtr->tpos;
2028 char save[s];
2029 if (!tb->blank && nlen>0) {
2030 if (s > 0) {
2031 memcpy(save, &tb->text[tPtr->tpos], s);
2032 tb->used -= (tb->used - tPtr->tpos);
2034 insertTextInteractively(tPtr, text, nlen);
2035 newline++;
2036 WMAppendTextStream(tPtr, newline);
2037 if (s>0)
2038 insertTextInteractively(tPtr, save, s);
2040 } else {
2041 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2042 && !tb->graphic && tb->text) {
2044 void *ntb = WMCreateTextBlockWithText(
2045 tPtr, &tb->text[tPtr->tpos],
2046 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2047 tb->used = tPtr->tpos;
2048 WMAppendTextBlock(tPtr, ntb);
2049 tPtr->tpos = 0;
2051 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
2052 if(tPtr->flags.indentNewLine) {
2053 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2054 " ", tb->d.font, tb->color, True, 4));
2055 tPtr->tpos = 4;
2056 } else {
2057 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2058 NULL, tb->d.font, tb->color, True, 0));
2059 tPtr->tpos = 0;
2064 } else {
2065 if (tb->used + len >= tb->allocated) {
2066 tb->allocated = reqBlockSize(tb->used+len);
2067 tb->text = wrealloc(tb->text, tb->allocated);
2070 if (tb->blank) {
2071 memcpy(tb->text, text, len);
2072 tb->used = len;
2073 tPtr->tpos = len;
2074 tb->text[tb->used] = 0;
2075 tb->blank = False;
2077 } else {
2078 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2079 tb->used-tPtr->tpos+1);
2080 memmove(&tb->text[tPtr->tpos], text, len);
2081 tb->used += len;
2082 tPtr->tpos += len;
2083 tb->text[tb->used] = 0;
2088 layOutDocument(tPtr);
2092 static void
2093 selectRegion(Text *tPtr, int x, int y)
2096 if (x < 0 || y < 0)
2097 return;
2099 y += (tPtr->flags.rulerShown? 40: 0);
2100 y += tPtr->vpos;
2101 if (y>10)
2102 y -= 10; /* the original offset */
2104 x -= tPtr->visible.x-2;
2105 if (x<0)
2106 x=0;
2108 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2109 tPtr->sel.w = abs(tPtr->clicked.x - x);
2110 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2111 tPtr->sel.h = abs(tPtr->clicked.y - y);
2113 tPtr->flags.ownsSelection = True;
2114 paintText(tPtr);
2118 static void
2119 releaseSelection(Text *tPtr)
2121 TextBlock *tb = tPtr->firstTextBlock;
2123 while(tb) {
2124 tb->selected = False;
2125 tb = tb->next;
2127 tPtr->flags.ownsSelection = False;
2128 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2129 CurrentTime);
2131 paintText(tPtr);
2135 WMData*
2136 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2137 Atom *type)
2139 Text *tPtr = view->self;
2140 Display *dpy = tPtr->view->screen->display;
2141 Atom _TARGETS;
2142 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2143 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2144 WMData *data = NULL;
2147 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2148 char *text = WMGetTextSelectedStream(tPtr);
2150 if (text) {
2151 data = WMCreateDataWithBytes(text, strlen(text));
2152 WMSetDataFormat(data, TYPETEXT);
2154 *type = target;
2155 return data;
2156 } else printf("didn't get it\n");
2158 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2159 if (target == _TARGETS) {
2160 Atom *ptr;
2162 ptr = wmalloc(4 * sizeof(Atom));
2163 ptr[0] = _TARGETS;
2164 ptr[1] = XA_STRING;
2165 ptr[2] = TEXT;
2166 ptr[3] = COMPOUND_TEXT;
2168 data = WMCreateDataWithBytes(ptr, 4*4);
2169 WMSetDataFormat(data, 32);
2171 *type = target;
2172 return data;
2175 return NULL;
2178 static void
2179 lostHandler(WMView *view, Atom selection, void *cdata)
2181 releaseSelection((WMText *)view->self);
2184 static WMSelectionProcs selectionHandler = {
2185 requestHandler, lostHandler, NULL
2189 static void
2190 ownershipObserver(void *observerData, WMNotification *notification)
2192 if (observerData != WMGetNotificationClientData(notification))
2193 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2196 static void
2197 autoSelectText(Text *tPtr, int clicks)
2199 int x, start;
2200 TextBlock *tb;
2201 char *mark = NULL, behind, ahead;
2203 if(!(tb = tPtr->currentTextBlock))
2204 return;
2206 if(clicks == 2) {
2209 switch(tb->text[tPtr->tpos]) {
2210 case ' ': return;
2212 case '<': case '>': behind = '<'; ahead = '>'; break;
2213 case '{': case '}': behind = '{'; ahead = '}'; break;
2214 case '[': case ']': behind = '['; ahead = ']'; break;
2216 default: behind = ahead = ' ';
2219 tPtr->sel.y = tPtr->cursor.y+5;
2220 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2222 if(tb->graphic) {
2223 tPtr->sel.x = tb->sections[0].x;
2224 tPtr->sel.w = tb->sections[0].w;
2225 } else {
2226 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2228 start = tPtr->tpos;
2229 while(start > 0 && tb->text[start-1] != behind)
2230 start--;
2232 x = tPtr->cursor.x;
2233 if(tPtr->tpos > start){
2234 x -= WMWidthOfString(font, &tb->text[start],
2235 tPtr->tpos - start);
2237 tPtr->sel.x = (x<0?0:x)+1;
2239 if((mark = strchr(&tb->text[start], ahead))) {
2240 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2241 (int)(mark - &tb->text[start]));
2242 } else if(tb->used > start) {
2243 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2244 tb->used - start);
2248 } else if(clicks == 3) {
2249 TextBlock *cur = tb;
2251 while(tb && !tb->first) {
2252 tb = tb->prior;
2254 tPtr->sel.y = tb->sections[0]._y;
2256 tb = cur;
2257 while(tb->next && !tb->next->first) {
2258 tb = tb->next;
2260 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2261 + 5 - tPtr->sel.y;
2263 tPtr->sel.x = 0;
2264 tPtr->sel.w = tPtr->docWidth;
2265 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2268 if (!tPtr->flags.ownsSelection) {
2269 WMCreateSelectionHandler(tPtr->view,
2270 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2271 tPtr->flags.ownsSelection = True;
2273 paintText(tPtr);
2278 static void
2279 fontChanged(void *observerData, WMNotification *notification)
2281 WMText *tPtr = (WMText *) observerData;
2282 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2283 printf("fontChanged\n");
2285 if(!tPtr || !font)
2286 return;
2288 if (tPtr->flags.ownsSelection)
2289 WMSetTextSelectionFont(tPtr, font);
2293 static void
2294 handleTextKeyPress(Text *tPtr, XEvent *event)
2296 char buffer[64];
2297 KeySym ksym;
2298 int control_pressed = False;
2299 TextBlock *tb = NULL;
2301 if (((XKeyEvent *) event)->state & ControlMask)
2302 control_pressed = True;
2303 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2305 switch(ksym) {
2307 case XK_Home:
2308 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2309 tPtr->tpos = 0;
2310 updateCursorPosition(tPtr);
2311 paintText(tPtr);
2312 break;
2314 case XK_End:
2315 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2316 if(tPtr->currentTextBlock->graphic)
2317 tPtr->tpos = 1;
2318 else
2319 tPtr->tpos = tPtr->currentTextBlock->used;
2321 updateCursorPosition(tPtr);
2322 paintText(tPtr);
2323 break;
2325 case XK_Left:
2326 if(!(tb = tPtr->currentTextBlock))
2327 break;
2328 if(tb->graphic)
2329 goto L_imaGFX;
2331 if(tPtr->tpos==0) {
2332 L_imaGFX: if(tb->prior) {
2333 tPtr->currentTextBlock = tb->prior;
2334 if(tPtr->currentTextBlock->graphic)
2335 tPtr->tpos = 1;
2336 else
2337 tPtr->tpos = tPtr->currentTextBlock->used;
2339 if(!tb->first && tPtr->tpos > 0)
2340 tPtr->tpos--;
2341 } else tPtr->tpos = 0;
2342 } else tPtr->tpos--;
2343 updateCursorPosition(tPtr);
2344 paintText(tPtr);
2345 break;
2347 case XK_Right:
2348 if(!(tb = tPtr->currentTextBlock))
2349 break;
2350 if(tb->graphic)
2351 goto R_imaGFX;
2352 if(tPtr->tpos == tb->used) {
2353 R_imaGFX: if(tb->next) {
2354 tPtr->currentTextBlock = tb->next;
2355 tPtr->tpos = 0;
2356 if(!tb->next->first && tb->next->used>0)
2357 tPtr->tpos++;
2358 } else {
2359 if(tb->graphic)
2360 tPtr->tpos = 1;
2361 else
2362 tPtr->tpos = tb->used;
2364 } else tPtr->tpos++;
2365 updateCursorPosition(tPtr);
2366 paintText(tPtr);
2367 break;
2369 case XK_Down:
2370 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2371 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2372 paintText(tPtr);
2373 break;
2375 case XK_Up:
2376 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2377 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2378 paintText(tPtr);
2379 break;
2381 case XK_BackSpace:
2382 case XK_Delete:
2383 #ifdef XK_KP_Delete
2384 case XK_KP_Delete:
2385 #endif
2386 deleteTextInteractively(tPtr, ksym);
2387 updateCursorPosition(tPtr);
2388 paintText(tPtr);
2389 break;
2391 case XK_Control_R :
2392 case XK_Control_L :
2393 control_pressed = True;
2394 break;
2396 case XK_Tab:
2397 insertTextInteractively(tPtr, " ", 4);
2398 updateCursorPosition(tPtr);
2399 paintText(tPtr);
2400 break;
2402 case XK_Return:
2403 *buffer = '\n';
2404 default:
2405 if (*buffer != 0 && !control_pressed) {
2406 insertTextInteractively(tPtr, buffer, strlen(buffer));
2407 updateCursorPosition(tPtr);
2408 paintText(tPtr);
2410 } else if (control_pressed && ksym==XK_r) {
2411 Bool i = !tPtr->flags.rulerShown;
2412 WMShowTextRuler(tPtr, i);
2413 tPtr->flags.rulerShown = i;
2415 else if (control_pressed && *buffer == '\a')
2416 XBell(tPtr->view->screen->display, 0);
2417 else
2418 WMRelayToNextResponder(tPtr->view, event);
2421 if (!control_pressed && tPtr->flags.ownsSelection)
2422 releaseSelection(tPtr);
2426 static void
2427 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2428 void *cdata, WMData *data)
2430 Text *tPtr = (Text *)view->self;
2431 char *text;
2433 tPtr->flags.waitingForSelection = 0;
2435 if (data) {
2436 text = (char*)WMDataBytes(data);
2438 if (tPtr->parser) {
2439 (tPtr->parser) (tPtr, (void *) text);
2440 layOutDocument(tPtr);
2441 } else insertTextInteractively(tPtr, text, strlen(text));
2442 updateCursorPosition(tPtr);
2443 paintText(tPtr);
2445 } else {
2446 int n;
2448 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2450 if (text) {
2451 text[n] = 0;
2452 if (tPtr->parser) {
2453 (tPtr->parser) (tPtr, (void *) text);
2454 layOutDocument(tPtr);
2455 } else insertTextInteractively(tPtr, text, n);
2456 updateCursorPosition(tPtr);
2457 paintText(tPtr);
2459 XFree(text);
2467 static void
2468 handleActionEvents(XEvent *event, void *data)
2470 Text *tPtr = (Text *)data;
2471 Display *dpy = event->xany.display;
2472 KeySym ksym;
2475 switch (event->type) {
2476 case KeyPress:
2477 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2478 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2479 tPtr->flags.extendSelection = True;
2480 return;
2483 if (tPtr->flags.focused) {
2484 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2485 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2486 GrabModeAsync, GrabModeAsync, None,
2487 tPtr->view->screen->invisibleCursor, CurrentTime);
2488 tPtr->flags.pointerGrabbed = True;
2489 handleTextKeyPress(tPtr, event);
2491 } break;
2493 case KeyRelease:
2494 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2495 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2496 tPtr->flags.extendSelection = False;
2497 return;
2498 /* end modify flag so selection can be extended */
2500 break;
2503 case MotionNotify:
2505 if (tPtr->flags.pointerGrabbed) {
2506 tPtr->flags.pointerGrabbed = False;
2507 XUngrabPointer(dpy, CurrentTime);
2510 if(tPtr->flags.waitingForSelection)
2511 break;
2513 if ((event->xmotion.state & Button1Mask)) {
2514 TextBlock *tb = tPtr->currentTextBlock;
2516 if(tb && tPtr->flags.isOverGraphic &&
2517 tb->graphic && !tb->object) {
2518 WMSize offs;
2519 WMPixmap *pixmap = tb->d.pixmap;
2520 char *types[2] = {"application/X-image", NULL};
2522 offs.width = 2;
2523 offs.height = 2;
2525 WMDragImageFromView(tPtr->view, pixmap, types,
2526 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2527 offs, event, True);
2530 } else {
2531 if (!tPtr->flags.ownsSelection) {
2532 WMCreateSelectionHandler(tPtr->view,
2533 XA_PRIMARY, event->xbutton.time,
2534 &selectionHandler, NULL);
2535 tPtr->flags.ownsSelection = True;
2538 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2539 break;
2542 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2543 break;
2546 case ButtonPress:
2548 if (tPtr->flags.pointerGrabbed) {
2549 tPtr->flags.pointerGrabbed = False;
2550 XUngrabPointer(dpy, CurrentTime);
2551 break;
2554 if (tPtr->flags.waitingForSelection)
2555 break;
2557 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2558 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2559 return;
2562 if (tPtr->flags.ownsSelection)
2563 releaseSelection(tPtr);
2566 if (event->xbutton.button == Button1) {
2568 if(WMIsDoubleClick(event)) {
2569 TextBlock *tb = tPtr->currentTextBlock;
2571 tPtr->lastClickTime = event->xbutton.time;
2572 if(tb && tb->graphic && !tb->object) {
2573 char desc[tb->used+1];
2574 memcpy(desc, tb->text, tb->used);
2575 desc[tb->used] = 0;
2576 if(tPtr->delegate) {
2577 if(tPtr->delegate->didDoubleClickOnPicture)
2578 (*tPtr->delegate->didDoubleClickOnPicture)
2579 (tPtr->delegate, desc);
2581 } else {
2582 autoSelectText(tPtr, 2);
2584 break;
2585 } else if(event->xbutton.time - tPtr->lastClickTime
2586 < WINGsConfiguration.doubleClickDelay) {
2587 tPtr->lastClickTime = event->xbutton.time;
2588 autoSelectText(tPtr, 3);
2589 break;
2592 if (!tPtr->flags.focused) {
2593 WMSetFocusToWidget(tPtr);
2594 tPtr->flags.focused = True;
2597 tPtr->lastClickTime = event->xbutton.time;
2598 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2599 paintText(tPtr);
2602 if (event->xbutton.button
2603 == WINGsConfiguration.mouseWheelDown) {
2604 WMScrollText(tPtr, 16);
2605 break;
2608 if (event->xbutton.button
2609 == WINGsConfiguration.mouseWheelUp) {
2610 WMScrollText(tPtr, -16);
2611 break;
2614 if (event->xbutton.button == Button2) {
2615 char *text = NULL;
2616 int n;
2618 if (!tPtr->flags.editable) {
2619 XBell(dpy, 0);
2620 break;
2623 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2624 event->xbutton.time, pasteText, NULL)) {
2626 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2627 tPtr->flags.waitingForSelection = 0;
2629 if (text) {
2630 text[n] = 0;
2632 if (tPtr->parser) {
2633 (tPtr->parser) (tPtr, (void *) text);
2634 layOutDocument(tPtr);
2636 else
2637 insertTextInteractively(tPtr, text, n);
2639 XFree(text);
2640 #if 0
2641 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2642 (void*)WMInsertTextEvent);
2643 #endif
2644 updateCursorPosition(tPtr);
2645 paintText(tPtr);
2647 } else {
2648 tPtr->flags.waitingForSelection = True;
2651 break;
2655 case ButtonRelease:
2656 if (tPtr->flags.pointerGrabbed) {
2657 tPtr->flags.pointerGrabbed = False;
2658 XUngrabPointer(dpy, CurrentTime);
2659 break;
2662 if (tPtr->flags.waitingForSelection)
2663 break;
2669 static void
2670 handleEvents(XEvent *event, void *data)
2672 Text *tPtr = (Text *)data;
2674 switch(event->type) {
2675 case Expose:
2677 if (event->xexpose.count!=0)
2678 break;
2680 if(tPtr->hS) {
2681 if (!(W_VIEW(tPtr->hS))->flags.realized)
2682 WMRealizeWidget(tPtr->hS);
2685 if(tPtr->vS) {
2686 if (!(W_VIEW(tPtr->vS))->flags.realized)
2687 WMRealizeWidget(tPtr->vS);
2690 if(tPtr->ruler) {
2691 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2692 WMRealizeWidget(tPtr->ruler);
2696 if(!tPtr->db)
2697 textDidResize(tPtr->view->delegate, tPtr->view);
2699 paintText(tPtr);
2700 break;
2702 case FocusIn:
2703 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2704 != tPtr->view)
2705 return;
2706 tPtr->flags.focused = True;
2707 #if DO_BLINK
2708 if (tPtr->flags.editable && !tPtr->timerID) {
2709 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2710 blinkCursor, tPtr);
2712 #endif
2714 break;
2716 case FocusOut:
2717 tPtr->flags.focused = False;
2718 paintText(tPtr);
2719 #if DO_BLINK
2720 if (tPtr->timerID) {
2721 WMDeleteTimerHandler(tPtr->timerID);
2722 tPtr->timerID = NULL;
2724 #endif
2725 break;
2728 case DestroyNotify:
2729 clearText(tPtr);
2730 if(tPtr->db)
2731 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2732 if(tPtr->gfxItems)
2733 WMEmptyArray(tPtr->gfxItems);
2734 #if DO_BLINK
2735 if (tPtr->timerID)
2736 WMDeleteTimerHandler(tPtr->timerID);
2737 #endif
2738 WMReleaseFont(tPtr->dFont);
2739 WMReleaseColor(tPtr->dColor);
2740 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2741 WMRemoveNotificationObserver(tPtr);
2743 wfree(tPtr);
2745 break;
2751 static void
2752 insertPlainText(Text *tPtr, char *text)
2754 char *start, *mark;
2755 void *tb = NULL;
2757 start = text;
2758 while (start) {
2759 mark = strchr(start, '\n');
2760 if (mark) {
2761 tb = WMCreateTextBlockWithText(tPtr,
2762 start, tPtr->dFont,
2763 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2764 start = mark+1;
2765 tPtr->flags.first = True;
2766 } else {
2767 if (start && strlen(start)) {
2768 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2769 tPtr->dColor, tPtr->flags.first, strlen(start));
2770 } else tb = NULL;
2771 tPtr->flags.first = False;
2772 start = mark;
2775 if (tPtr->flags.prepend)
2776 WMPrependTextBlock(tPtr, tb);
2777 else
2778 WMAppendTextBlock(tPtr, tb);
2783 static void
2784 rulerMoveCallBack(WMWidget *w, void *self)
2786 Text *tPtr = (Text *)self;
2788 if (!tPtr)
2789 return;
2790 if (W_CLASS(tPtr) != WC_Text)
2791 return;
2793 paintText(tPtr);
2797 static void
2798 rulerReleaseCallBack(WMWidget *w, void *self)
2800 Text *tPtr = (Text *)self;
2802 if (!tPtr)
2803 return;
2804 if (W_CLASS(tPtr) != WC_Text)
2805 return;
2807 WMThawText(tPtr);
2808 return;
2811 static unsigned
2812 draggingSourceOperation(WMView *self, Bool local)
2814 return WDOperationCopy;
2817 static WMData*
2818 fetchDragData(WMView *self, char *type)
2820 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2821 char *desc;
2822 WMData *data;
2824 if (!tb)
2825 return NULL;
2827 printf("type is [%s]\n", type);
2828 desc = wmalloc(tb->used+1);
2829 memcpy(desc, tb->text, tb->used);
2830 desc[tb->used] = 0;
2831 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2833 wfree(desc);
2835 return data;
2839 static WMDragSourceProcs _DragSourceProcs = {
2840 draggingSourceOperation,
2841 NULL,
2842 NULL,
2843 fetchDragData
2847 static unsigned
2848 draggingEntered(WMView *self, WMDraggingInfo *info)
2850 printf("draggingEntered\n");
2851 return WDOperationCopy;
2855 static unsigned
2856 draggingUpdated(WMView *self, WMDraggingInfo *info)
2858 return WDOperationCopy;
2862 static void
2863 draggingExited(WMView *self, WMDraggingInfo *info)
2865 printf("draggingExited\n");
2868 static Bool
2869 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2871 printf("prepareForDragOperation\n");
2872 return True;
2876 char *badbadbad;
2878 static void
2879 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2880 void *cdata, WMData *data)
2882 badbadbad = wstrdup((char *)WMDataBytes(data));
2886 /* when it's done in WINGs, remove this */
2888 Bool
2889 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2891 WMScreen *scr = W_VIEW_SCREEN(view);
2893 if (!WMRequestSelection(scr->dragInfo.destView,
2894 scr->xdndSelectionAtom,
2895 XInternAtom(scr->display, type, False),
2896 scr->dragInfo.timestamp,
2897 receivedData, &scr->dragInfo)) {
2898 wwarning("could not request data for dropped data");
2902 XEvent ev;
2904 ev.type = ClientMessage;
2905 ev.xclient.message_type = scr->xdndFinishedAtom;
2906 ev.xclient.format = 32;
2907 ev.xclient.window = info->destinationWindow;
2908 ev.xclient.data.l[0] = 0;
2909 ev.xclient.data.l[1] = 0;
2910 ev.xclient.data.l[2] = 0;
2911 ev.xclient.data.l[3] = 0;
2912 ev.xclient.data.l[4] = 0;
2914 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2915 XFlush(scr->display);
2917 return True;
2920 static Bool
2921 performDragOperation(WMView *self, WMDraggingInfo *info)
2923 WMColor *color;
2924 WMText *tPtr = (WMText *)self->self;
2926 if (!tPtr)
2927 return True;
2929 requestDroppedData(tPtr->view, info, "application/X-color");
2930 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2931 if(color) {
2932 WMSetTextSelectionColor(tPtr, color);
2933 WMReleaseColor(color);
2938 return True;
2941 static void
2942 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2944 printf("concludeDragOperation\n");
2948 static WMDragDestinationProcs _DragDestinationProcs = {
2949 draggingEntered,
2950 draggingUpdated,
2951 draggingExited,
2952 prepareForDragOperation,
2953 performDragOperation,
2954 concludeDragOperation
2958 char *
2959 getStream(WMText *tPtr, int sel, int array)
2961 TextBlock *tb = NULL;
2962 char *text = NULL;
2963 unsigned long where = 0;
2965 if (!tPtr)
2966 return NULL;
2968 if (!(tb = tPtr->firstTextBlock))
2969 return NULL;
2971 if (tPtr->writer) {
2972 (tPtr->writer) (tPtr, (void *) text);
2973 return text;
2976 tb = tPtr->firstTextBlock;
2977 while (tb) {
2979 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2981 if (!sel || (tb->graphic && tb->selected)) {
2983 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2984 && tb != tPtr->firstTextBlock) {
2985 text = wrealloc(text, where+1);
2986 text[where++] = '\n';
2989 if(tb->blank)
2990 goto _gSnext;
2992 if(tb->graphic && array) {
2993 text = wrealloc(text, where+4);
2994 text[where++] = 0xFA;
2995 text[where++] = (tb->used>>8)&0x0ff;
2996 text[where++] = tb->used&0x0ff;
2997 text[where++] = tb->allocated; /* extra info */
2999 text = wrealloc(text, where+tb->used);
3000 memcpy(&text[where], tb->text, tb->used);
3001 where += tb->used;
3004 } else if (sel && tb->selected) {
3006 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3007 text = wrealloc(text, where+1);
3008 text[where++] = '\n';
3011 if(tb->blank)
3012 goto _gSnext;
3014 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3015 memcpy(&text[where], &tb->text[tb->s_begin],
3016 tb->s_end - tb->s_begin);
3017 where += tb->s_end - tb->s_begin;
3022 _gSnext:tb = tb->next;
3025 /* +1 for the end of string, let's be nice */
3026 text = wrealloc(text, where+1);
3027 text[where] = 0;
3028 return text;
3032 static void
3033 releaseStreamObjects(void *data)
3035 if(data)
3036 wfree(data);
3039 WMArray *
3040 getStreamObjects(WMText *tPtr, int sel)
3042 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3043 WMData *data;
3044 char *stream;
3045 unsigned short len;
3046 char *start, *fa, *desc;
3048 stream = getStream(tPtr, sel, 1);
3049 if(!stream)
3050 return NULL;
3052 start = stream;
3053 while (start) {
3055 fa = strchr(start, 0xFA);
3056 if (fa) {
3057 if((int)(fa - start)>0) {
3058 desc = start;
3059 desc[(int)(fa - start)] = 0;
3060 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3061 WMSetDataFormat(data, TYPETEXT);
3062 WMAddToArray(array, (void *) data);
3065 len = *(fa+1)*0xff + *(fa+2);
3066 data = WMCreateDataWithBytes((void *)(fa+4), len);
3067 WMSetDataFormat(data, *(fa+3));
3068 WMAddToArray(array, (void *) data);
3069 start = fa + len + 4;
3071 } else {
3072 if (start && strlen(start)) {
3073 data = WMCreateDataWithBytes((void *)start, strlen(start));
3074 WMSetDataFormat(data, TYPETEXT);
3075 WMAddToArray(array, (void *) data);
3077 start = fa;
3081 wfree(stream);
3082 return array;
3086 WMText *
3087 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3089 Text *tPtr;
3090 Display *dpy;
3091 WMScreen *scr;
3092 XGCValues gcv;
3094 tPtr = wmalloc(sizeof(Text));
3095 memset(tPtr, 0, sizeof(Text));
3096 tPtr->widgetClass = WC_Text;
3097 tPtr->view = W_CreateView(W_VIEW(parent));
3098 if (!tPtr->view) {
3099 perror("could not create text's view\n");
3100 wfree(tPtr);
3101 return NULL;
3104 tPtr->view->self = tPtr;
3105 tPtr->view->attribs.cursor = scr->textCursor;
3106 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3107 W_ResizeView(tPtr->view, 250, 200);
3109 dpy = tPtr->view->screen->display;
3110 scr = tPtr->view->screen;
3112 tPtr->dColor = WMWhiteColor(scr);
3113 tPtr->bgGC = WMColorGC(tPtr->dColor);
3114 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3115 WMReleaseColor(tPtr->dColor);
3117 tPtr->dColor = WMBlackColor(scr);
3118 tPtr->fgGC = WMColorGC(tPtr->dColor);
3120 gcv.graphics_exposures = False;
3121 gcv.foreground = W_PIXEL(scr->gray);
3122 gcv.background = W_PIXEL(scr->darkGray);
3123 gcv.fill_style = FillStippled;
3124 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr),
3125 STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
3126 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3127 GCForeground|GCBackground|GCStipple
3128 |GCFillStyle|GCGraphicsExposures, &gcv);
3130 tPtr->ruler = NULL;
3131 tPtr->vS = NULL;
3132 tPtr->hS = NULL;
3134 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(scr, 12));
3136 tPtr->view->delegate = &_TextViewDelegate;
3138 tPtr->delegate = NULL;
3140 #if DO_BLINK
3141 tPtr->timerID = NULL;
3142 #endif
3144 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3145 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3146 handleEvents, tPtr);
3148 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3149 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3150 handleActionEvents, tPtr);
3152 WMAddNotificationObserver(ownershipObserver, tPtr,
3153 "_lostOwnership", tPtr);
3155 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3156 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3160 char *types[3] = {"application/X-color", "application/X-image", NULL};
3161 WMRegisterViewForDraggedTypes(tPtr->view, types);
3164 WMAddNotificationObserver(fontChanged, tPtr,
3165 "WMFontPanelDidChangeNotification", tPtr);
3167 tPtr->firstTextBlock = NULL;
3168 tPtr->lastTextBlock = NULL;
3169 tPtr->currentTextBlock = NULL;
3170 tPtr->tpos = 0;
3172 tPtr->gfxItems = WMCreateArray(4);
3174 tPtr->parser = parser;
3175 tPtr->writer = writer;
3177 tPtr->sel.x = tPtr->sel.y = 2;
3178 tPtr->sel.w = tPtr->sel.h = 0;
3180 tPtr->clicked.x = tPtr->clicked.y = 2;
3182 tPtr->visible.x = tPtr->visible.y = 2;
3183 tPtr->visible.h = tPtr->view->size.height;
3184 tPtr->visible.w = tPtr->view->size.width - 4;
3186 tPtr->cursor.x = -23;
3188 tPtr->docWidth = 0;
3189 tPtr->docHeight = 0;
3190 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3191 default_bullet);
3192 tPtr->db = (Pixmap) NULL;
3193 tPtr->bgPixmap = NULL;
3195 tPtr->margins = WMGetRulerMargins(NULL);
3196 tPtr->margins->right = tPtr->visible.w;
3197 tPtr->nMargins = 1;
3199 tPtr->flags.rulerShown = False;
3200 tPtr->flags.monoFont = False;
3201 tPtr->flags.focused = False;
3202 tPtr->flags.editable = True;
3203 tPtr->flags.ownsSelection = False;
3204 tPtr->flags.pointerGrabbed = False;
3205 tPtr->flags.extendSelection = False;
3206 tPtr->flags.frozen = False;
3207 tPtr->flags.cursorShown = True;
3208 tPtr->flags.acceptsGraphic = False;
3209 tPtr->flags.horizOnDemand = False;
3210 tPtr->flags.needsLayOut = False;
3211 tPtr->flags.ignoreNewLine = False;
3212 tPtr->flags.indentNewLine = False;
3213 tPtr->flags.laidOut = False;
3214 tPtr->flags.ownsSelection = False;
3215 tPtr->flags.waitingForSelection = False;
3216 tPtr->flags.prepend = False;
3217 tPtr->flags.isOverGraphic = False;
3218 tPtr->flags.relief = WRSunken;
3219 tPtr->flags.isOverGraphic = 0;
3220 tPtr->flags.alignment = WALeft;
3221 tPtr->flags.first = True;
3223 return tPtr;
3226 void
3227 WMPrependTextStream(WMText *tPtr, char *text)
3229 CHECK_CLASS(tPtr, WC_Text);
3231 if(!text) {
3232 if (tPtr->flags.ownsSelection)
3233 releaseSelection(tPtr);
3234 clearText(tPtr);
3235 updateScrollers(tPtr);
3236 return;
3239 tPtr->flags.prepend = True;
3240 if (text && tPtr->parser)
3241 (tPtr->parser) (tPtr, (void *) text);
3242 else
3243 insertPlainText(tPtr, text);
3245 tPtr->flags.needsLayOut = True;
3246 tPtr->tpos = 0;
3247 if(!tPtr->flags.frozen) {
3248 layOutDocument(tPtr);
3253 void
3254 WMAppendTextStream(WMText *tPtr, char *text)
3256 CHECK_CLASS(tPtr, WC_Text);
3258 if(!text) {
3259 if (tPtr->flags.ownsSelection)
3260 releaseSelection(tPtr);
3261 clearText(tPtr);
3262 updateScrollers(tPtr);
3263 return;
3266 tPtr->flags.prepend = False;
3267 if (text && tPtr->parser)
3268 (tPtr->parser) (tPtr, (void *) text);
3269 else
3270 insertPlainText(tPtr, text);
3272 tPtr->flags.needsLayOut = True;
3273 if(tPtr->currentTextBlock) {
3274 if(tPtr->currentTextBlock->graphic)
3275 tPtr->tpos = 1;
3276 else
3277 tPtr->tpos = tPtr->currentTextBlock->used;
3280 if(!tPtr->flags.frozen) {
3281 layOutDocument(tPtr);
3286 char *
3287 WMGetTextStream(WMText *tPtr)
3289 CHECK_CLASS(tPtr, WC_Text);
3290 return getStream(tPtr, 0, 0);
3293 char *
3294 WMGetTextSelectedStream(WMText *tPtr)
3296 CHECK_CLASS(tPtr, WC_Text);
3297 return getStream(tPtr, 1, 0);
3300 WMArray *
3301 WMGetTextObjects(WMText *tPtr)
3303 CHECK_CLASS(tPtr, WC_Text);
3304 return getStreamObjects(tPtr, 0);
3307 WMArray *
3308 WMGetTextSelectedObjects(WMText *tPtr)
3310 CHECK_CLASS(tPtr, WC_Text);
3311 return getStreamObjects(tPtr, 1);
3315 void
3316 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3318 CHECK_CLASS(tPtr, WC_Text);
3320 tPtr->delegate = delegate;
3324 void *
3325 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3326 char *description, WMColor *color,
3327 unsigned short first, unsigned short extraInfo)
3329 TextBlock *tb;
3331 if (!w || !description || !color)
3332 return NULL;
3334 tb = wmalloc(sizeof(TextBlock));
3335 if (!tb)
3336 return NULL;
3338 tb->text = wstrdup(description);
3339 tb->used = strlen(description);
3340 tb->blank = False;
3341 tb->d.widget = w;
3342 tb->color = WMRetainColor(color);
3343 tb->marginN = newMargin(tPtr, NULL);
3344 tb->allocated = extraInfo;
3345 tb->first = first;
3346 tb->kanji = False;
3347 tb->graphic = True;
3348 tb->object = True;
3349 tb->underlined = False;
3350 tb->selected = False;
3351 tb->script = 0;
3352 tb->sections = NULL;
3353 tb->nsections = 0;
3354 tb->prior = NULL;
3355 tb->next = NULL;
3357 return tb;
3361 void *
3362 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3363 char *description, WMColor *color,
3364 unsigned short first, unsigned short extraInfo)
3366 TextBlock *tb;
3368 if (!p || !description || !color)
3369 return NULL;
3371 tb = wmalloc(sizeof(TextBlock));
3372 if (!tb)
3373 return NULL;
3375 tb->text = wstrdup(description);
3376 tb->used = strlen(description);
3377 tb->blank = False;
3378 tb->d.pixmap = WMRetainPixmap(p);
3379 tb->color = WMRetainColor(color);
3380 tb->marginN = newMargin(tPtr, NULL);
3381 tb->allocated = extraInfo;
3382 tb->first = first;
3383 tb->kanji = False;
3384 tb->graphic = True;
3385 tb->object = False;
3386 tb->underlined = False;
3387 tb->selected = False;
3388 tb->script = 0;
3389 tb->sections = NULL;
3390 tb->nsections = 0;
3391 tb->prior = NULL;
3392 tb->next = NULL;
3394 return tb;
3398 void*
3399 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3400 unsigned short first, unsigned short len)
3402 TextBlock *tb;
3404 if (!font || !color)
3405 return NULL;
3407 tb = wmalloc(sizeof(TextBlock));
3408 if (!tb)
3409 return NULL;
3411 tb->allocated = reqBlockSize(len);
3412 tb->text = (char *)wmalloc(tb->allocated);
3413 memset(tb->text, 0, tb->allocated);
3415 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3416 *tb->text = ' ';
3417 tb->used = 1;
3418 tb->blank = True;
3419 } else {
3420 memcpy(tb->text, text, len);
3421 tb->used = len;
3422 tb->blank = False;
3424 tb->text[tb->used] = 0;
3426 tb->d.font = WMRetainFont(font);
3427 tb->color = WMRetainColor(color);
3428 tb->marginN = newMargin(tPtr, NULL);
3429 tb->first = first;
3430 tb->kanji = False;
3431 tb->graphic = False;
3432 tb->underlined = False;
3433 tb->selected = False;
3434 tb->script = 0;
3435 tb->sections = NULL;
3436 tb->nsections = 0;
3437 tb->prior = NULL;
3438 tb->next = NULL;
3439 return tb;
3442 void
3443 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3444 unsigned int kanji, unsigned int underlined, int script,
3445 WMRulerMargins *margins)
3447 TextBlock *tb = (TextBlock *) vtb;
3448 if (!tb)
3449 return;
3451 tb->first = first;
3452 tb->kanji = kanji;
3453 tb->underlined = underlined;
3454 tb->script = script;
3455 tb->marginN = newMargin(tPtr, margins);
3458 void
3459 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3460 unsigned int *kanji, unsigned int *underlined, int *script,
3461 WMRulerMargins *margins)
3463 TextBlock *tb = (TextBlock *) vtb;
3464 if (!tb)
3465 return;
3467 if (first) *first = tb->first;
3468 if (kanji) *kanji = tb->kanji;
3469 if (underlined) *underlined = tb->underlined;
3470 if (script) *script = tb->script;
3471 if (margins) margins = &tPtr->margins[tb->marginN];
3476 void
3477 WMPrependTextBlock(WMText *tPtr, void *vtb)
3479 TextBlock *tb = (TextBlock *)vtb;
3481 if (!tPtr || !tb)
3482 return;
3484 if (tb->graphic) {
3485 if(tb->object) {
3486 WMWidget *w = tb->d.widget;
3487 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3488 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3489 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3492 WMAddToArray(tPtr->gfxItems, (void *)tb);
3493 tPtr->tpos = 0;
3494 } else {
3495 if(tb->graphic)
3496 tPtr->tpos = 1;
3497 else
3498 tPtr->tpos = tb->used;
3501 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3502 tb->next = tb->prior = NULL;
3503 tb->first = True;
3504 tPtr->lastTextBlock = tPtr->firstTextBlock
3505 = tPtr->currentTextBlock = tb;
3506 return;
3509 if(!tb->first) {
3510 tb->marginN = tPtr->currentTextBlock->marginN;
3513 tb->next = tPtr->currentTextBlock;
3514 tb->prior = tPtr->currentTextBlock->prior;
3515 if (tPtr->currentTextBlock->prior)
3516 tPtr->currentTextBlock->prior->next = tb;
3518 tPtr->currentTextBlock->prior = tb;
3519 if (!tb->prior)
3520 tPtr->firstTextBlock = tb;
3522 tPtr->currentTextBlock = tb;
3526 void
3527 WMAppendTextBlock(WMText *tPtr, void *vtb)
3529 TextBlock *tb = (TextBlock *)vtb;
3531 if (!tPtr || !tb)
3532 return;
3534 if (tb->graphic) {
3535 if(tb->object) {
3536 WMWidget *w = tb->d.widget;
3537 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3538 (W_VIEW(w))->attribs.cursor =
3539 tPtr->view->screen->defaultCursor;
3540 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3543 WMAddToArray(tPtr->gfxItems, (void *)tb);
3544 tPtr->tpos = 0;
3546 } else {
3547 if(tb->graphic)
3548 tPtr->tpos = 1;
3549 else
3550 tPtr->tpos = tb->used;
3554 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3555 tb->next = tb->prior = NULL;
3556 tb->first = True;
3557 tPtr->lastTextBlock = tPtr->firstTextBlock
3558 = tPtr->currentTextBlock = tb;
3559 return;
3562 if(!tb->first) {
3563 tb->marginN = tPtr->currentTextBlock->marginN;
3566 tb->next = tPtr->currentTextBlock->next;
3567 tb->prior = tPtr->currentTextBlock;
3568 if (tPtr->currentTextBlock->next)
3569 tPtr->currentTextBlock->next->prior = tb;
3571 tPtr->currentTextBlock->next = tb;
3573 if (!tb->next)
3574 tPtr->lastTextBlock = tb;
3576 tPtr->currentTextBlock = tb;
3579 void *
3580 WMRemoveTextBlock(WMText *tPtr)
3582 TextBlock *tb = NULL;
3584 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3585 || !tPtr->currentTextBlock) {
3586 return NULL;
3589 tb = tPtr->currentTextBlock;
3590 if (tb->graphic) {
3591 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3593 if(tb->object) {
3594 WMUnmapWidget(tb->d.widget);
3598 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3599 if (tPtr->currentTextBlock->next)
3600 tPtr->currentTextBlock->next->prior = NULL;
3602 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3603 tPtr->currentTextBlock = tPtr->firstTextBlock;
3605 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3606 tPtr->currentTextBlock->prior->next = NULL;
3607 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3608 tPtr->currentTextBlock = tPtr->lastTextBlock;
3609 } else {
3610 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3611 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3612 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3615 return (void *)tb;
3619 static void
3620 destroyWidget(WMWidget *widget)
3622 if(!widget)
3623 return;
3625 WMDestroyWidget(widget);
3626 wfree(widget);
3629 void
3630 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3632 TextBlock *tb = (TextBlock *)vtb;
3633 if (!tPtr || !tb)
3634 return;
3636 if (tb->graphic) {
3637 if(tb->object) {
3638 /* naturally, there's a danger to destroying widgets whose action
3639 brings us here: ie. press a button to destroy it...
3640 need to find a safer way. till then... this stays commented out */
3641 /* 5 months later... destroy it 10 seconds after now which should be
3642 enough time for the widget's action to be completed... :-) */
3643 WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);
3644 tb->d.widget = NULL;
3646 } else {
3647 WMReleasePixmap(tb->d.pixmap);
3648 tb->d.pixmap = NULL;
3650 } else {
3651 WMReleaseFont(tb->d.font);
3654 WMReleaseColor(tb->color);
3655 if (tb->sections && tb->nsections > 0)
3656 wfree(tb->sections);
3657 wfree(tb->text);
3658 wfree(tb);
3659 tb = NULL;
3664 void
3665 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3667 if (!tPtr)
3668 return;
3670 if (color)
3671 tPtr->fgGC = WMColorGC(color);
3672 else {
3673 WMColor *color = WMBlackColor(tPtr->view->screen);
3674 tPtr->fgGC = WMColorGC(color);
3675 WMReleaseColor(color);
3678 paintText(tPtr);
3681 void
3682 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3684 if (!tPtr)
3685 return;
3687 if (color) {
3688 tPtr->bgGC = WMColorGC(color);
3689 W_SetViewBackgroundColor(tPtr->view, color);
3690 } else {
3691 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3692 W_SetViewBackgroundColor(tPtr->view,
3693 WMWhiteColor(tPtr->view->screen));
3696 paintText(tPtr);
3699 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3701 if (!tPtr)
3702 return;
3704 if (tPtr->bgPixmap)
3705 WMReleasePixmap(tPtr->bgPixmap);
3707 if (pixmap)
3708 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3709 else
3710 tPtr->bgPixmap = NULL;
3713 void
3714 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3716 if (!tPtr)
3717 return;
3718 tPtr->flags.relief = relief;
3719 textDidResize(tPtr->view->delegate, tPtr->view);
3722 void
3723 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3725 if (!tPtr)
3726 return;
3728 if (shouldhave && !tPtr->hS) {
3729 tPtr->hS = WMCreateScroller(tPtr);
3730 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3731 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3732 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3733 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3734 WMMapWidget(tPtr->hS);
3735 } else if (!shouldhave && tPtr->hS) {
3736 WMUnmapWidget(tPtr->hS);
3737 WMDestroyWidget(tPtr->hS);
3738 tPtr->hS = NULL;
3741 tPtr->hpos = 0;
3742 tPtr->prevHpos = 0;
3743 textDidResize(tPtr->view->delegate, tPtr->view);
3747 void
3748 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3750 if (!tPtr)
3751 return;
3753 if(shouldhave && !tPtr->ruler) {
3754 tPtr->ruler = WMCreateRuler(tPtr);
3755 (W_VIEW(tPtr->ruler))->attribs.cursor =
3756 tPtr->view->screen->defaultCursor;
3757 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3758 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3759 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3760 } else if(!shouldhave && tPtr->ruler) {
3761 WMShowTextRuler(tPtr, False);
3762 WMDestroyWidget(tPtr->ruler);
3763 tPtr->ruler = NULL;
3765 textDidResize(tPtr->view->delegate, tPtr->view);
3768 void
3769 WMShowTextRuler(WMText *tPtr, Bool show)
3771 if(!tPtr)
3772 return;
3773 if(!tPtr->ruler)
3774 return;
3776 if(tPtr->flags.monoFont)
3777 show = False;
3779 tPtr->flags.rulerShown = show;
3780 if(show) {
3781 WMMapWidget(tPtr->ruler);
3782 } else {
3783 WMUnmapWidget(tPtr->ruler);
3786 textDidResize(tPtr->view->delegate, tPtr->view);
3789 Bool
3790 WMGetTextRulerShown(WMText *tPtr)
3792 if(!tPtr)
3793 return 0;
3795 if(!tPtr->ruler)
3796 return 0;
3798 return tPtr->flags.rulerShown;
3802 void
3803 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3805 if (!tPtr)
3806 return;
3808 if (shouldhave && !tPtr->vS) {
3809 tPtr->vS = WMCreateScroller(tPtr);
3810 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3811 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3812 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3813 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3814 WMMapWidget(tPtr->vS);
3815 } else if (!shouldhave && tPtr->vS) {
3816 WMUnmapWidget(tPtr->vS);
3817 WMDestroyWidget(tPtr->vS);
3818 tPtr->vS = NULL;
3821 tPtr->vpos = 0;
3822 tPtr->prevVpos = 0;
3823 textDidResize(tPtr->view->delegate, tPtr->view);
3828 Bool
3829 WMScrollText(WMText *tPtr, int amount)
3831 Bool scroll=False;
3832 if (!tPtr)
3833 return False;
3834 if (amount == 0 || !tPtr->view->flags.realized)
3835 return False;
3837 if (amount < 0) {
3838 if (tPtr->vpos > 0) {
3839 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3840 else tPtr->vpos=0;
3841 scroll=True;
3843 } else {
3844 int limit = tPtr->docHeight - tPtr->visible.h;
3845 if (tPtr->vpos < limit) {
3846 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3847 else tPtr->vpos = limit;
3848 scroll = True;
3852 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3853 updateScrollers(tPtr);
3854 paintText(tPtr);
3856 tPtr->prevVpos = tPtr->vpos;
3857 return scroll;
3860 Bool
3861 WMPageText(WMText *tPtr, Bool direction)
3863 if (!tPtr) return False;
3864 if (!tPtr->view->flags.realized) return False;
3866 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3869 void
3870 WMSetTextEditable(WMText *tPtr, Bool editable)
3872 if (!tPtr)
3873 return;
3874 tPtr->flags.editable = editable;
3877 int
3878 WMGetTextEditable(WMText *tPtr)
3880 if (!tPtr)
3881 return 0;
3882 return tPtr->flags.editable;
3885 void
3886 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3888 if (!tPtr)
3889 return;
3890 tPtr->flags.indentNewLine = indent;
3893 void
3894 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3896 if (!tPtr)
3897 return;
3898 tPtr->flags.ignoreNewLine = ignore;
3901 Bool
3902 WMGetTextIgnoresNewline(WMText *tPtr)
3904 if (!tPtr)
3905 return True;
3906 return tPtr->flags.ignoreNewLine;
3909 void
3910 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3912 if (!tPtr)
3913 return;
3915 if (mono) {
3916 if(tPtr->flags.rulerShown)
3917 WMShowTextRuler(tPtr, False);
3918 if(tPtr->flags.alignment != WALeft)
3919 tPtr->flags.alignment = WALeft;
3922 tPtr->flags.monoFont = mono;
3923 textDidResize(tPtr->view->delegate, tPtr->view);
3926 Bool
3927 WMGetTextUsesMonoFont(WMText *tPtr)
3929 if (!tPtr)
3930 return True;
3931 return tPtr->flags.monoFont;
3935 void
3936 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3938 if (!tPtr)
3939 return;
3941 WMReleaseFont(tPtr->dFont);
3942 if (font)
3943 tPtr->dFont = WMRetainFont(font);
3944 else
3945 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3948 WMFont *
3949 WMGetTextDefaultFont(WMText *tPtr)
3951 if (!tPtr)
3952 return NULL;
3953 else
3954 return WMRetainFont(tPtr->dFont);
3957 void
3958 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3960 if (!tPtr)
3961 return;
3963 WMReleaseColor(tPtr->dColor);
3964 if (color)
3965 tPtr->dColor = WMRetainColor(color);
3966 else
3967 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3970 WMColor *
3971 WMGetTextDefaultColor(WMText *tPtr)
3973 if (!tPtr)
3974 return NULL;
3975 else
3976 return WMRetainColor(tPtr->dColor);
3979 void
3980 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3982 if (!tPtr)
3983 return;
3984 if(tPtr->flags.monoFont)
3985 tPtr->flags.alignment = WALeft;
3986 else
3987 tPtr->flags.alignment = alignment;
3988 WMThawText(tPtr);
3991 int
3992 WMGetTextInsertType(WMText *tPtr)
3994 if (!tPtr)
3995 return 0;
3996 return tPtr->flags.prepend;
4000 void
4001 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
4003 if (!tPtr || !color)
4004 return;
4006 setSelectionProperty(tPtr, NULL, color, -1);
4009 WMColor *
4010 WMGetTextSelectionColor(WMText *tPtr)
4012 TextBlock *tb;
4014 if (!tPtr)
4015 return NULL;
4017 tb = tPtr->currentTextBlock;
4019 if (!tb || !tPtr->flags.ownsSelection)
4020 return NULL;
4022 if(!tb->selected)
4023 return NULL;
4025 return tb->color;
4029 void
4030 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4032 if (!tPtr || !font)
4033 return;
4035 setSelectionProperty(tPtr, font, NULL, -1) ;
4038 WMFont *
4039 WMGetTextSelectionFont(WMText *tPtr)
4041 TextBlock *tb;
4043 if (!tPtr)
4044 return NULL;
4046 tb = tPtr->currentTextBlock;
4048 if (!tb || !tPtr->flags.ownsSelection)
4049 return NULL;
4051 if(!tb->selected)
4052 return NULL;
4054 if(tb->graphic) {
4055 tb = getFirstNonGraphicBlockFor(tb, 1);
4056 if(!tb)
4057 return NULL;
4059 return (tb->selected ? tb->d.font : NULL);
4063 void
4064 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4066 if (!tPtr || (underlined!=0 && underlined !=1))
4067 return;
4069 setSelectionProperty(tPtr, NULL, NULL, underlined);
4073 int
4074 WMGetTextSelectionUnderlined(WMText *tPtr)
4076 TextBlock *tb;
4078 if (!tPtr)
4079 return 0;
4081 tb = tPtr->currentTextBlock;
4083 if (!tb || !tPtr->flags.ownsSelection)
4084 return 0;
4086 if(!tb->selected)
4087 return 0;
4089 return tb->underlined;
4093 void
4094 WMFreezeText(WMText *tPtr)
4096 if (!tPtr)
4097 return;
4099 tPtr->flags.frozen = True;
4103 void
4104 WMThawText(WMText *tPtr)
4106 if (!tPtr)
4107 return;
4109 tPtr->flags.frozen = False;
4111 if(tPtr->flags.monoFont) {
4112 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4113 TextBlock *tb;
4115 /* make sure to unmap widgets no matter where they are */
4116 /* they'll be later remapped if needed by paintText */
4117 for(j=0; j<c; j++) {
4118 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4119 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4120 WMUnmapWidget(tb->d.widget);
4126 tPtr->flags.laidOut = False;
4127 layOutDocument(tPtr);
4128 updateScrollers(tPtr);
4129 paintText(tPtr);
4130 tPtr->flags.needsLayOut = False;
4134 /* find first occurence of a string */
4135 static char *
4136 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4137 Bool caseSensitive)
4139 char *ptr;
4141 if(!haystack || !needle || !end)
4142 return NULL;
4144 for (ptr = haystack; ptr < end; ptr++) {
4145 if(caseSensitive) {
4146 if (*ptr == *needle && !strncmp(ptr, needle, len))
4147 return ptr;
4149 } else {
4150 if (tolower(*ptr) == tolower(*needle) &&
4151 !strncasecmp(ptr, needle, len))
4152 return ptr;
4156 return NULL;
4159 /* find last occurence of a string */
4160 static char *
4161 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4162 Bool caseSensitive)
4164 char *ptr;
4166 if(!haystack || !needle || !end)
4167 return NULL;
4169 for (ptr = haystack-2; ptr > end; ptr--) {
4170 if(caseSensitive) {
4171 if (*ptr == *needle && !strncmp(ptr, needle, len))
4172 return ptr;
4173 } else {
4174 if (tolower(*ptr) == tolower(*needle) &&
4175 !strncasecmp(ptr, needle, len))
4176 return ptr;
4180 return NULL;
4184 Bool
4185 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4186 Bool caseSensitive)
4188 TextBlock *tb;
4189 char *mark=NULL;
4190 unsigned short pos;
4192 if (!tPtr || !needle)
4193 return False;
4195 #if 0
4196 if (! (tb = tPtr->currentTextBlock)) {
4197 if (! (tb = ( (direction > 0) ?
4198 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4199 return False;
4201 } else {
4202 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4203 tb = (direction>0) ? tb->next : tb->prior; */
4204 if(tb != tPtr->lastTextBlock)
4205 tb = tb->prior;
4207 #endif
4208 tb = tPtr->currentTextBlock;
4209 pos = tPtr->tpos;
4212 while(tb) {
4213 if (!tb->graphic) {
4215 if(direction > 0) {
4216 if(pos+1 < tb->used)
4217 pos++;
4219 if(tb->used - pos> 0 && pos > 0) {
4220 mark = mystrstr(&tb->text[pos], needle,
4221 strlen(needle), &tb->text[tb->used], caseSensitive);
4223 } else {
4224 tb = tb->next;
4225 pos = 0;
4226 continue;
4229 } else {
4230 if(pos-1 > 0)
4231 pos--;
4233 if(pos > 0) {
4234 mark = mystrrstr(&tb->text[pos], needle,
4235 strlen(needle), tb->text, caseSensitive);
4236 } else {
4237 tb = tb->prior;
4238 if(!tb)
4239 return False;
4240 pos = tb->used;
4241 continue;
4246 if(mark) {
4247 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4249 tPtr->tpos = (int)(mark - tb->text);
4250 tPtr->currentTextBlock = tb;
4251 updateCursorPosition(tPtr);
4252 tPtr->sel.y = tPtr->cursor.y+5;
4253 tPtr->sel.h = tPtr->cursor.h-10;
4254 tPtr->sel.x = tPtr->cursor.x +1;
4255 tPtr->sel.w = WMIN(WMWidthOfString(font,
4256 &tb->text[tPtr->tpos], strlen(needle)),
4257 tPtr->docWidth - tPtr->sel.x);
4258 tPtr->flags.ownsSelection = True;
4259 paintText(tPtr);
4261 return True;
4265 tb = (direction>0) ? tb->next : tb->prior;
4266 if(tb) {
4267 pos = (direction>0) ? 0 : tb->used;
4271 return False;
4275 Bool
4276 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4278 if (!tPtr)
4279 return False;
4281 if (!tPtr->flags.ownsSelection)
4282 return False;
4284 removeSelection(tPtr);
4286 if(replacement) {
4287 insertTextInteractively(tPtr, replacement, strlen(replacement));
4288 updateCursorPosition(tPtr);
4289 paintText(tPtr);
4292 return True;