- API change in WINGs for WMDraw*String().
[wmaker-crm.git] / WINGs / wtext.c
blob8f14268cc31fc616b527f9b6a599ddd80e993034
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 WMColor *fgColor; /* The current foreground color */
110 WMColor *bgColor; /* The background color */
112 GC stippledGC; /* the GC to overlay selected graphics with */
113 Pixmap db; /* the buffer on which to draw */
114 WMPixmap *bgPixmap; /* the background pixmap */
116 myRect visible; /* the actual rectangle that can be drawn into */
117 myRect cursor; /* the position and (height) of cursor */
118 myRect sel; /* the selection rectangle */
120 WMPoint clicked; /* where in the _document_ was clicked */
122 unsigned short tpos; /* the position in the currentTextBlock */
123 unsigned short docWidth; /* the width of the entire document */
124 unsigned int docHeight; /* the height of the entire document */
126 TextBlock *firstTextBlock;
127 TextBlock *lastTextBlock;
128 TextBlock *currentTextBlock;
130 WMArray *gfxItems; /* a nice array of graphic items */
132 #if DO_BLINK
133 WMHandlerID timerID; /* for nice twinky-winky */
134 #endif
136 WMAction *parser;
137 WMAction *writer;
138 WMTextDelegate *delegate;
139 Time lastClickTime;
141 WMRulerMargins *margins; /* an array of margins */
143 unsigned int nMargins:7; /* the total number of margins in use */
144 struct {
145 unsigned int monoFont:1; /* whether to ignore formats and graphic */
146 unsigned int focused:1; /* whether this instance has input focus */
147 unsigned int editable:1; /* "silly user, you can't edit me" */
148 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
149 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
150 unsigned int extendSelection:1; /* shift-drag to select more regions */
152 unsigned int rulerShown:1; /* whether the ruler is shown or not */
153 unsigned int frozen:1; /* whether screen updates are to be made */
154 unsigned int cursorShown:1; /* whether to show the cursor */
155 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
156 unsigned int horizOnDemand:1;/* if a large image should appear*/
157 unsigned int needsLayOut:1; /* in case of Append/Deletes */
158 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
159 unsigned int indentNewLine:1;/* add " " for a newline typed */
160 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
161 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
162 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
163 WMAlignment alignment:2; /* the alignment for text */
164 WMReliefType relief:3; /* the relief to display with */
165 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
166 unsigned int first:1; /* for plain text parsing, newline? */
167 /* unsigned int RESERVED:1; */
168 } flags;
169 } Text;
172 #define NOTIFY(T,C,N,A) {\
173 WMNotification *notif = WMCreateNotification(N,T,A);\
174 if ((T)->delegate && (T)->delegate->C)\
175 (*(T)->delegate->C)((T)->delegate,notif);\
176 WMPostNotification(notif);\
177 WMReleaseNotification(notif);}
180 #define TYPETEXT 0
182 #if 0
183 /* just to print blocks of text not terminated by \0 */
184 static void
185 output(char *ptr, int len)
187 char *s;
189 s = wmalloc(len+1);
190 memcpy(s, ptr, len);
191 s[len] = 0;
192 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
193 printf("[%s]\n", s);
194 wfree(s);
196 #endif
199 #if DO_BLINK
200 #define CURSOR_BLINK_ON_DELAY 600
201 #define CURSOR_BLINK_OFF_DELAY 400
202 #endif
205 #define STIPPLE_WIDTH 8
206 #define STIPPLE_HEIGHT 8
207 static unsigned char STIPPLE_BITS[] = {
208 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
213 static char *default_bullet[] = {
214 "6 6 4 1",
215 " c None s None", ". c black",
216 "X c white", "o c #808080",
217 " ... ",
218 ".XX.. ",
219 ".XX..o",
220 ".....o",
221 " ...oo",
222 " ooo "};
225 static void handleEvents(XEvent *event, void *data);
226 static void layOutDocument(Text *tPtr);
227 static void updateScrollers(Text *tPtr);
230 static int
231 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
233 unsigned int i=0;
235 for(i=0; i < tPtr->nMargins; i++) {
237 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
238 return i;
241 return -1;
246 static int
247 newMargin(Text *tPtr, WMRulerMargins *margins)
249 int n;
251 if (!margins) {
252 tPtr->margins[0].retainCount++;
253 return 0;
256 n = getMarginNumber(tPtr, margins);
258 if (n == -1) {
260 if(tPtr->nMargins >= 127) {
261 n = tPtr->nMargins-1;
262 return n;
265 tPtr->margins = wrealloc(tPtr->margins,
266 (++tPtr->nMargins)*sizeof(WMRulerMargins));
268 n = tPtr->nMargins-1;
269 tPtr->margins[n].left = margins->left;
270 tPtr->margins[n].first = margins->first;
271 tPtr->margins[n].body = margins->body;
272 tPtr->margins[n].right = margins->right;
273 /* for each tab... */
274 tPtr->margins[n].retainCount = 1;
275 } else {
276 tPtr->margins[n].retainCount++;
279 return n;
282 static Bool
283 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
285 unsigned short i, w, lw, selected = False, extend = False;
286 myRect sel;
289 /* if selection rectangle completely encloses the section */
290 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
291 && (tb->sections[s]._y + tb->sections[s].h
292 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
293 sel.x = 0;
294 sel.w = tPtr->visible.w;
295 selected = extend = True;
297 /* or if it starts on a line and then goes further down */
298 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
299 && (tb->sections[s]._y + tb->sections[s].h
300 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
301 && (tb->sections[s]._y + tb->sections[s].h
302 >= tPtr->visible.y + tPtr->sel.y) ) {
303 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
304 sel.w = tPtr->visible.w;
305 selected = extend = True;
307 /* or if it begins before a line, but ends on it */
308 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
309 && (tb->sections[s]._y + tb->sections[s].h
310 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
311 && (tb->sections[s]._y
312 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
314 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
315 sel.w = tPtr->sel.x + tPtr->sel.w;
316 else
317 sel.w = tPtr->sel.x;
319 sel.x = 0;
320 selected = True;
322 /* or if the selection rectangle lies entirely within a line */
323 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
324 && (tPtr->sel.w >= 2)
325 && (tb->sections[s]._y + tb->sections[s].h
326 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
327 sel.x = tPtr->sel.x;
328 sel.w = tPtr->sel.w;
329 selected = True;
332 if (selected) {
333 selected = False;
335 /* if not within (modified) selection rectangle */
336 if ( tb->sections[s].x > sel.x + sel.w
337 || tb->sections[s].x + tb->sections[s].w < sel.x)
338 return False;
340 if (tb->graphic) {
341 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
342 && tb->sections[s].x >= sel.x) {
343 rect->width = tb->sections[s].w;
344 rect->x = tb->sections[s].x;
345 selected = True;
347 } else {
349 i = tb->sections[s].begin;
350 lw = 0;
352 if (0&& tb->sections[s].x >= sel.x) {
353 tb->s_begin = tb->sections[s].begin;
354 goto _selEnd;
357 while (++i <= tb->sections[s].end) {
359 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
360 lw += w;
362 if (lw + tb->sections[s].x >= sel.x
363 || i == tb->sections[s].end ) {
364 lw -= w;
365 i--;
366 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
367 break;
371 if (i > tb->sections[s].end) {
372 printf("WasSelected: (i > tb->sections[s].end) \n");
373 return False;
376 _selEnd: rect->x = tb->sections[s].x + lw;
377 lw = 0;
378 while(++i <= tb->sections[s].end) {
380 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
381 lw += w;
383 if (lw + rect->x >= sel.x + sel.w
384 || i == tb->sections[s].end ) {
386 if (i != tb->sections[s].end) {
387 lw -= w;
388 i--;
391 rect->width = lw;
392 if (tb->sections[s].last && sel.x + sel.w
393 >= tb->sections[s].x + tb->sections[s].w
394 && extend ) {
395 rect->width += (tPtr->visible.w - rect->x - lw);
398 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
399 selected = True;
400 break;
406 if (selected) {
407 rect->y = tb->sections[s]._y - tPtr->vpos;
408 rect->height = tb->sections[s].h;
409 if(tb->graphic) { printf("DEBUG: graphic s%d h%d\n", s,tb->sections[s].h);}
411 return selected;
415 static void
416 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
418 TextBlock *tb;
419 int isFont=False;
421 tb = tPtr->firstTextBlock;
422 if (!tb || !tPtr->flags.ownsSelection)
423 return;
425 if(font && (!color || underlined==-1))
426 isFont = True;
428 while (tb) {
429 if (tPtr->flags.monoFont || tb->selected) {
431 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
432 || tb->graphic) {
434 if(isFont) {
435 if(!tb->graphic) {
436 WMReleaseFont(tb->d.font);
437 tb->d.font = WMRetainFont(font);
439 } else if(underlined !=-1) {
440 tb->underlined = underlined;
441 } else {
442 WMReleaseColor(tb->color);
443 tb->color = WMRetainColor(color);
446 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
448 TextBlock *midtb, *otb = tb;
450 if(underlined != -1) {
451 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
452 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
453 False, (tb->s_end - tb->s_begin));
454 } else {
455 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
456 &(tb->text[tb->s_begin]),
457 (isFont?font:tb->d.font),
458 (isFont?tb->color:color),
459 False, (tb->s_end - tb->s_begin));
463 if (midtb) {
464 if(underlined != -1) {
465 midtb->underlined = underlined;
466 } else {
467 midtb->underlined = otb->underlined;
470 midtb->selected = !True;
471 midtb->s_begin = 0;
472 midtb->s_end = midtb->used;
473 tPtr->currentTextBlock = tb;
474 WMAppendTextBlock(tPtr, midtb);
475 tb = tPtr->currentTextBlock;
478 if (otb->used - otb->s_end > 0) {
479 TextBlock *ntb;
480 ntb = (TextBlock *)
481 WMCreateTextBlockWithText(tPtr,
482 &(otb->text[otb->s_end]), otb->d.font, otb->color,
483 False, otb->used - otb->s_end);
485 if (ntb) {
486 ntb->underlined = otb->underlined;
487 ntb->selected = False;
488 WMAppendTextBlock(tPtr, ntb);
489 tb = tPtr->currentTextBlock;
493 if (midtb) {
494 tPtr->currentTextBlock = midtb;
497 otb->selected = False;
498 otb->used = otb->s_begin;
502 tb = tb->next;
505 tPtr->flags.needsLayOut = True;
506 WMThawText(tPtr);
508 /* in case the size changed... */
509 if(isFont && tPtr->currentTextBlock) {
510 TextBlock *tb = tPtr->currentTextBlock;
512 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
513 tPtr->sel.y = 3 + tb->sections[0]._y;
514 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
515 tPtr->sel.w = tb->sections[tb->nsections-1].w;
516 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
517 tPtr->sel.x = 0;
519 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
525 static Bool
526 removeSelection(Text *tPtr)
528 TextBlock *tb = NULL;
529 Bool first = False;
531 if (!(tb = tPtr->firstTextBlock))
532 return False;
534 while (tb) {
535 if (tb->selected) {
536 if(!first && !tb->graphic) {
537 WMReleaseFont(tPtr->dFont);
538 tPtr->dFont = WMRetainFont(tb->d.font);
539 first = True;
542 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
543 tPtr->currentTextBlock = tb;
544 if(tb->next) {
545 tPtr->tpos = 0;
546 } else if(tb->prior) {
547 if(tb->prior->graphic)
548 tPtr->tpos = 1;
549 else
550 tPtr->tpos = tb->prior->used;
551 } else tPtr->tpos = 0;
553 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
554 tb = tPtr->currentTextBlock;
555 continue;
557 } else if (tb->s_end <= tb->used) {
558 memmove(&(tb->text[tb->s_begin]),
559 &(tb->text[tb->s_end]), tb->used - tb->s_end);
560 tb->used -= (tb->s_end - tb->s_begin);
561 tb->selected = False;
562 tPtr->tpos = tb->s_begin;
567 tb = tb->next;
569 return True;
572 static TextBlock *
573 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
575 TextBlock *hold = tb;
577 if (!tb)
578 return NULL;
580 while (tb) {
581 if (!tb->graphic)
582 break;
583 tb = (dir? tb->next : tb->prior);
586 if(!tb) {
587 tb = hold;
588 while (tb) {
589 if (!tb->graphic)
590 break;
591 tb = (dir? tb->prior : tb->next);
595 if(!tb)
596 return NULL;
598 return tb;
602 static Bool
603 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
604 TextBlock *tb)
606 if (tPtr->flags.monoFont && tb->graphic) {
607 tb = getFirstNonGraphicBlockFor(tb, *dir);
608 if(!tb)
609 return 0;
611 if (tb->graphic) {
612 tPtr->currentTextBlock =
613 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
614 tPtr->tpos = 0;
615 return 0;
620 if(!tb->sections) {
621 layOutDocument(tPtr);
622 return 0;
625 *dir = !(y <= tb->sections[0].y);
626 if(*dir) {
627 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
628 && (y >= tb->sections[0]._y ) ) {
629 /* if it's on the same line */
630 if(x < tb->sections[0].x)
631 *dir = 0;
633 } else {
634 if ( ( y <= tb->sections[tb->nsections-1]._y
635 + tb->sections[tb->nsections-1].h )
636 && (y >= tb->sections[tb->nsections-1]._y ) ) {
637 /* if it's on the same line */
638 if(x > tb->sections[tb->nsections-1].x)
639 *dir = 1;
643 return 1;
647 static void
648 paintText(Text *tPtr)
650 TextBlock *tb;
651 WMFont *font;
652 char *text;
653 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
654 WMScreen *scr = tPtr->view->screen;
655 Display *dpy = tPtr->view->screen->display;
656 Window win = tPtr->view->window;
657 WMColor *color;
659 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
660 return;
663 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0,
664 tPtr->visible.w, tPtr->visible.h);
666 if (tPtr->bgPixmap) {
667 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
668 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
669 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
672 if (! (tb = tPtr->currentTextBlock)) {
673 if (! (tb = tPtr->firstTextBlock)) {
674 goto _copy_area;
678 done = False;
682 /* first, which direction? Don't waste time looking all over,
683 since the parts to be drawn will most likely be near what
684 was previously drawn */
685 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
686 goto _copy_area;
688 while(tb) {
690 if (tb->graphic && tPtr->flags.monoFont)
691 goto _getSibling;
693 if(dir) {
694 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
695 + tb->sections[tb->nsections-1].h)
696 break;
697 } else {
698 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
699 + tb->sections[tb->nsections-1].h)
700 break;
703 _getSibling:
704 if(dir) {
705 if(tb->next)
706 tb = tb->next;
707 else break;
708 } else {
709 if(tb->prior)
710 tb = tb->prior;
711 else break;
716 /* first, place all text that can be viewed */
717 while (!done && tb) {
718 if (tb->graphic) {
719 tb = tb->next;
720 continue;
723 tb->selected = False;
725 for(s=0; s<tb->nsections && !done; s++) {
727 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
728 done = True;
729 break;
732 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
733 continue;
735 if (tPtr->flags.monoFont) {
736 font = tPtr->dFont;
737 color = tPtr->fgColor;
738 } else {
739 font = tb->d.font;
740 color = tb->color;
743 if (tPtr->flags.ownsSelection) {
744 XRectangle rect;
746 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
747 tb->selected = True;
748 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
749 rect.x, rect.y, rect.width, rect.height);
753 prev_y = tb->sections[s]._y;
755 len = tb->sections[s].end - tb->sections[s].begin;
756 text = &(tb->text[tb->sections[s].begin]);
757 y = tb->sections[s].y - tPtr->vpos;
758 WMDrawString(scr, tPtr->db, color, font,
759 tb->sections[s].x - tPtr->hpos, y, text, len);
761 if (!tPtr->flags.monoFont && tb->underlined) {
762 XDrawLine(dpy, tPtr->db, WMColorGC(color),
763 tb->sections[s].x - tPtr->hpos,
764 y + font->y + 1,
765 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
766 y + font->y + 1);
769 tb = (!done? tb->next : NULL);
772 /* now , show all graphic items that can be viewed */
773 c = WMGetArrayItemCount(tPtr->gfxItems);
774 if (c > 0 && !tPtr->flags.monoFont) {
775 int j, h;
777 for(j=0; j<c; j++) {
778 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
780 /* if it's not viewable, and mapped, unmap it */
781 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
782 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
784 if(tb->object) {
785 if ((W_VIEW(tb->d.widget))->flags.mapped) {
786 WMUnmapWidget(tb->d.widget);
789 } else {
790 /* if it's viewable, and not mapped, map it */
791 if(tb->object) {
792 W_View *view = W_VIEW(tb->d.widget);
794 if (!view->flags.realized)
795 WMRealizeWidget(tb->d.widget);
796 if(!view->flags.mapped) {
797 XMapWindow(view->screen->display, view->window);
798 XFlush(view->screen->display);
799 view->flags.mapped = 1;
803 if(tb->object) {
804 WMMoveWidget(tb->d.widget,
805 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
806 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
807 h = WMWidgetHeight(tb->d.widget) + 1;
809 } else {
810 WMDrawPixmap(tb->d.pixmap, tPtr->db,
811 tb->sections[0].x - tPtr->hpos,
812 tb->sections[0].y - tPtr->vpos);
813 h = tb->d.pixmap->height + 1;
817 if (tPtr->flags.ownsSelection) {
818 XRectangle rect;
820 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
821 Drawable d = (0&&tb->object?
822 (WMWidgetView(tb->d.widget))->window : tPtr->db);
824 tb->selected = True;
825 XFillRectangle(dpy, d, tPtr->stippledGC,
826 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,*/
827 rect.x, rect.y, rect.width, rect.height);
831 if (!tPtr->flags.monoFont && tb->underlined) {
832 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
833 tb->sections[0].x - tPtr->hpos,
834 tb->sections[0].y + h - tPtr->vpos,
835 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
836 tb->sections[0].y + h - tPtr->vpos);
843 _copy_area:
844 if (tPtr->flags.editable && tPtr->flags.cursorShown
845 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
846 int y = tPtr->cursor.y - tPtr->vpos;
847 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
848 tPtr->cursor.x, y,
849 tPtr->cursor.x, y + tPtr->cursor.h);
852 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
853 tPtr->visible.w, tPtr->visible.h,
854 tPtr->visible.x, tPtr->visible.y);
856 W_DrawRelief(scr, win, 0, 0,
857 tPtr->view->size.width, tPtr->view->size.height,
858 tPtr->flags.relief);
860 if (tPtr->ruler && tPtr->flags.rulerShown)
861 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor),
862 2, 42, tPtr->view->size.width-4, 42);
866 static void
867 mouseOverObject(Text *tPtr, int x, int y)
869 TextBlock *tb;
870 Bool result = False;
872 x -= tPtr->visible.x;
873 x += tPtr->hpos;
874 y -= tPtr->visible.y;
875 y += tPtr->vpos;
877 if(tPtr->flags.ownsSelection) {
878 if(tPtr->sel.x <= x
879 && tPtr->sel.y <= y
880 && tPtr->sel.x + tPtr->sel.w >= x
881 && tPtr->sel.y + tPtr->sel.h >= y) {
882 tPtr->flags.isOverGraphic = 1;
883 result = True;
888 if(!result) {
889 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
891 if (c<1)
892 tPtr->flags.isOverGraphic = 0;
895 for(j=0; j<c; j++) {
896 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
898 if(!tb || !tb->sections) {
899 tPtr->flags.isOverGraphic = 0;
900 return;
903 if(!tb->object) {
904 if(tb->sections[0].x <= x
905 && tb->sections[0].y <= y
906 && tb->sections[0].x + tb->sections[0].w >= x
907 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
908 tPtr->flags.isOverGraphic = 3;
909 result = True;
910 break;
918 if(!result)
919 tPtr->flags.isOverGraphic = 0;
922 tPtr->view->attribs.cursor = (result?
923 tPtr->view->screen->defaultCursor
924 : tPtr->view->screen->textCursor);
926 XSetWindowAttributes attribs;
927 attribs.cursor = tPtr->view->attribs.cursor;
928 XChangeWindowAttributes(tPtr->view->screen->display,
929 tPtr->view->window, CWCursor,
930 &attribs);
934 #if DO_BLINK
936 static void
937 blinkCursor(void *data)
939 Text *tPtr = (Text*)data;
941 if (tPtr->flags.cursorShown) {
942 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
943 blinkCursor, data);
944 } else {
945 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
946 blinkCursor, data);
948 paintText(tPtr);
949 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
951 #endif
953 static void
954 updateCursorPosition(Text *tPtr)
956 TextBlock *tb = NULL;
957 int x, y, h, s;
959 if(tPtr->flags.needsLayOut)
960 layOutDocument(tPtr);
962 if (! (tb = tPtr->currentTextBlock)) {
963 if (! (tb = tPtr->firstTextBlock)) {
964 WMFont *font = tPtr->dFont;
965 tPtr->tpos = 0;
966 tPtr->cursor.h = font->height + abs(font->height-font->y);
968 tPtr->cursor.y = 2;
969 tPtr->cursor.x = 2;
970 return;
975 if(tb->blank) {
976 tPtr->tpos = 0;
977 y = tb->sections[0].y;
978 h = tb->sections[0].h;
979 x = tb->sections[0].x;
981 } else if(tb->graphic) {
982 y = tb->sections[0].y;
983 h = tb->sections[0].h;
984 x = tb->sections[0].x;
985 if(tPtr->tpos == 1)
986 x += tb->sections[0].w;
988 } else {
989 if(tPtr->tpos > tb->used)
990 tPtr->tpos = tb->used;
992 for(s=0; s<tb->nsections-1; s++) {
994 if(tPtr->tpos >= tb->sections[s].begin
995 && tPtr->tpos <= tb->sections[s].end)
996 break;
999 y = tb->sections[s]._y;
1000 h = tb->sections[s].h;
1001 x = tb->sections[s].x + WMWidthOfString(
1002 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
1003 &tb->text[tb->sections[s].begin],
1004 tPtr->tpos - tb->sections[s].begin);
1007 tPtr->cursor.y = y;
1008 tPtr->cursor.h = h;
1009 tPtr->cursor.x = x;
1012 /* scroll the bars if the cursor is not visible */
1013 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1014 if(tPtr->cursor.y+tPtr->cursor.h
1015 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1016 tPtr->vpos +=
1017 (tPtr->cursor.y+tPtr->cursor.h+10
1018 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1019 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1020 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1025 updateScrollers(tPtr);
1029 static void
1030 cursorToTextPosition(Text *tPtr, int x, int y)
1032 TextBlock *tb = NULL;
1033 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1034 char *text;
1036 if(tPtr->flags.needsLayOut)
1037 layOutDocument(tPtr);
1039 y += (tPtr->vpos - tPtr->visible.y);
1040 if (y<0)
1041 y = 0;
1043 x -= (tPtr->visible.x - 2);
1044 if (x<0)
1045 x=0;
1047 /* clicked is relative to document, not window... */
1048 tPtr->clicked.x = x;
1049 tPtr->clicked.y = y;
1051 if (! (tb = tPtr->currentTextBlock)) {
1052 if (! (tb = tPtr->firstTextBlock)) {
1053 WMFont *font = tPtr->dFont;
1054 tPtr->tpos = 0;
1055 tPtr->cursor.h = font->height + abs(font->height-font->y);
1056 tPtr->cursor.y = 2;
1057 tPtr->cursor.x = 2;
1058 return;
1062 /* first, which direction? Most likely, newly clicked
1063 position will be close to previous */
1064 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1065 return;
1068 s = (dir? 0 : tb->nsections-1);
1069 if ( y >= tb->sections[s]._y
1070 && y <= tb->sections[s]._y + tb->sections[s].h) {
1071 goto _doneV;
1074 /* get the first (or last) section of the TextBlock that
1075 lies about the vertical click point */
1076 done = False;
1077 while (!done && tb) {
1079 if (tPtr->flags.monoFont && tb->graphic) {
1080 if( (dir?tb->next:tb->prior))
1081 tb = (dir?tb->next:tb->prior);
1082 continue;
1085 s = (dir? 0 : tb->nsections-1);
1086 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1088 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1089 ( y >= tb->sections[s]._y ) ) ) {
1090 done = True;
1091 } else {
1092 dir? s++ : s--;
1096 if (!done) {
1097 if ( (dir? tb->next : tb->prior)) {
1098 tb = (dir ? tb->next : tb->prior);
1099 } else {
1100 pos = tb->used;
1101 break; /* goto _doneH; */
1107 if (s<0 || s>=tb->nsections) {
1108 s = (dir? tb->nsections-1 : 0);
1111 _doneV:
1112 /* we have the line, which TextBlock on that line is it? */
1113 pos = (dir?0:tb->sections[s].begin);
1114 if (tPtr->flags.monoFont && tb->graphic) {
1115 TextBlock *hold = tb;
1116 tb = getFirstNonGraphicBlockFor(hold, dir);
1118 if(!tb) {
1119 tPtr->tpos = 0;
1120 tb = hold;
1121 s = 0;
1122 goto _doNothing;
1127 if(tb->blank)
1128 _w = 0;
1130 _y = tb->sections[s]._y;
1132 while (tb) {
1134 if (tPtr->flags.monoFont && tb->graphic) {
1135 tb = (dir ? tb->next : tb->prior);
1136 continue;
1139 if (dir) {
1140 if (tb->graphic) {
1141 if(tb->object)
1142 _w = WMWidgetWidth(tb->d.widget)-5;
1143 else
1144 _w = tb->d.pixmap->width-5;
1146 if (tb->sections[0].x + _w >= x)
1147 break;
1148 } else {
1149 text = &(tb->text[tb->sections[s].begin]);
1150 len = tb->sections[s].end - tb->sections[s].begin;
1151 _w = WMWidthOfString(tb->d.font, text, len);
1152 if (tb->sections[s].x + _w >= x)
1153 break;
1156 } else {
1157 if (tb->sections[s].x <= x)
1158 break;
1161 if ((dir? tb->next : tb->prior)) {
1162 TextBlock *nxt = (dir? tb->next : tb->prior);
1163 if (tPtr->flags.monoFont && nxt->graphic) {
1164 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1165 if (!nxt) {
1166 pos = (dir?0:tb->sections[s].begin);
1167 tPtr->cursor.x = tb->sections[s].x;
1168 goto _doneH;
1172 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1173 /* this must be the last/first on this line. stop */
1174 pos = (dir? tb->sections[s].end : 0);
1175 tPtr->cursor.x = tb->sections[s].x;
1176 if (!tb->blank) {
1177 if (tb->graphic) {
1178 if(tb->object)
1179 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1180 else
1181 tPtr->cursor.x += tb->d.pixmap->width;
1182 } else if (pos > tb->sections[s].begin) {
1183 tPtr->cursor.x +=
1184 WMWidthOfString(tb->d.font,
1185 &(tb->text[tb->sections[s].begin]),
1186 pos - tb->sections[s].begin);
1189 goto _doneH;
1193 if ( (dir? tb->next : tb->prior)) {
1194 tb = (dir ? tb->next : tb->prior);
1195 } else {
1196 done = True;
1197 break;
1200 if (tb)
1201 s = (dir? 0 : tb->nsections-1);
1204 /* we have said TextBlock, now where within it? */
1205 if (tb) {
1206 if(tb->graphic) {
1207 int gw = (tb->object ?
1208 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1210 tPtr->cursor.x = tb->sections[0].x;
1212 if(x > tPtr->cursor.x + gw/2) {
1213 pos = 1;
1214 tPtr->cursor.x += gw;
1215 } else {
1216 printf("first %d\n", tb->first);
1217 if(tb->prior) {
1218 if(tb->prior->graphic) pos = 1;
1219 else pos = tb->prior->used;
1220 tb = tb->prior;
1221 } else pos = 0;
1225 s = 0;
1226 goto _doneH;
1228 } else {
1229 WMFont *f = tb->d.font;
1230 len = tb->sections[s].end - tb->sections[s].begin;
1231 text = &(tb->text[tb->sections[s].begin]);
1233 _w = x - tb->sections[s].x;
1234 pos = 0;
1236 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1237 pos++;
1239 tPtr->cursor.x = tb->sections[s].x +
1240 (pos? WMWidthOfString(f, text, pos) : 0);
1242 pos += tb->sections[s].begin;
1246 _doneH:
1247 if(tb->graphic) {
1248 tPtr->tpos = (pos<=1)? pos : 0;
1249 } else {
1250 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1252 _doNothing:
1253 if (!tb)
1254 printf("...for this app will surely crash :-)\n");
1256 tPtr->currentTextBlock = tb;
1257 tPtr->cursor.h = tb->sections[s].h;
1258 tPtr->cursor.y = tb->sections[s]._y;
1260 /* scroll the bars if the cursor is not visible */
1261 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1262 if(tPtr->cursor.y+tPtr->cursor.h
1263 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1264 tPtr->vpos +=
1265 (tPtr->cursor.y+tPtr->cursor.h+10
1266 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1267 updateScrollers(tPtr);
1268 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1269 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1270 updateScrollers(tPtr);
1278 static void
1279 updateScrollers(Text *tPtr)
1282 if (tPtr->flags.frozen)
1283 return;
1285 if (tPtr->vS) {
1286 if (tPtr->docHeight <= tPtr->visible.h) {
1287 WMSetScrollerParameters(tPtr->vS, 0, 1);
1288 tPtr->vpos = 0;
1289 } else {
1290 float hmax = (float)(tPtr->docHeight);
1291 WMSetScrollerParameters(tPtr->vS,
1292 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1293 (float)tPtr->visible.h/hmax);
1295 } else tPtr->vpos = 0;
1297 if (tPtr->hS) {
1298 if (tPtr->docWidth <= tPtr->visible.w) {
1299 WMSetScrollerParameters(tPtr->hS, 0, 1);
1300 tPtr->hpos = 0;
1301 } else {
1302 float wmax = (float)(tPtr->docWidth);
1303 WMSetScrollerParameters(tPtr->hS,
1304 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1305 (float)tPtr->visible.w/wmax);
1307 } else tPtr->hpos = 0;
1310 static void
1311 scrollersCallBack(WMWidget *w, void *self)
1313 Text *tPtr = (Text *)self;
1314 Bool scroll = False;
1315 int which;
1317 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1318 return;
1320 if (w == tPtr->vS) {
1321 int height;
1322 height = tPtr->visible.h;
1324 which = WMGetScrollerHitPart(tPtr->vS);
1325 switch(which) {
1327 case WSDecrementLine:
1328 if (tPtr->vpos > 0) {
1329 if (tPtr->vpos>16) tPtr->vpos-=16;
1330 else tPtr->vpos=0;
1331 scroll=True;
1333 break;
1335 case WSIncrementLine: {
1336 int limit = tPtr->docHeight - height;
1337 if (tPtr->vpos < limit) {
1338 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1339 else tPtr->vpos=limit;
1340 scroll = True;
1343 break;
1345 case WSDecrementPage:
1346 if(((int)tPtr->vpos - (int)height) >= 0)
1347 tPtr->vpos -= height;
1348 else
1349 tPtr->vpos = 0;
1351 scroll = True;
1352 break;
1354 case WSIncrementPage:
1355 tPtr->vpos += height;
1356 if (tPtr->vpos > (tPtr->docHeight - height))
1357 tPtr->vpos = tPtr->docHeight - height;
1358 scroll = True;
1359 break;
1362 case WSKnob:
1363 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1364 * (float)(tPtr->docHeight - height);
1365 scroll = True;
1366 break;
1368 case WSKnobSlot:
1369 case WSNoPart:
1370 break;
1372 scroll = (tPtr->vpos != tPtr->prevVpos);
1373 tPtr->prevVpos = tPtr->vpos;
1377 if (w == tPtr->hS) {
1378 int width = tPtr->visible.w;
1380 which = WMGetScrollerHitPart(tPtr->hS);
1381 switch(which) {
1383 case WSDecrementLine:
1384 if (tPtr->hpos > 0) {
1385 if (tPtr->hpos>16) tPtr->hpos-=16;
1386 else tPtr->hpos=0;
1387 scroll=True;
1388 }break;
1390 case WSIncrementLine: {
1391 int limit = tPtr->docWidth - width;
1392 if (tPtr->hpos < limit) {
1393 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1394 else tPtr->hpos=limit;
1395 scroll = True;
1397 }break;
1399 case WSDecrementPage:
1400 if(((int)tPtr->hpos - (int)width) >= 0)
1401 tPtr->hpos -= width;
1402 else
1403 tPtr->hpos = 0;
1405 scroll = True;
1406 break;
1408 case WSIncrementPage:
1409 tPtr->hpos += width;
1410 if (tPtr->hpos > (tPtr->docWidth - width))
1411 tPtr->hpos = tPtr->docWidth - width;
1412 scroll = True;
1413 break;
1416 case WSKnob:
1417 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1418 * (float)(tPtr->docWidth - width);
1419 scroll = True;
1420 break;
1422 case WSKnobSlot:
1423 case WSNoPart:
1424 break;
1426 scroll = (tPtr->hpos != tPtr->prevHpos);
1427 tPtr->prevHpos = tPtr->hpos;
1430 if (scroll) {
1431 updateScrollers(tPtr);
1432 paintText(tPtr);
1438 typedef struct {
1439 TextBlock *tb;
1440 unsigned short begin, end; /* what part of the text block */
1441 } myLineItems;
1444 static int
1445 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1447 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1448 WMFont *font;
1449 char *text;
1450 TextBlock *tb, *tbsame=NULL;
1452 if(!items || nitems == 0)
1453 return 0;
1455 for(i=0; i<nitems; i++) {
1456 tb = items[i].tb;
1458 if (tb->graphic) {
1459 if (!tPtr->flags.monoFont) {
1460 if(tb->object) {
1461 WMWidget *wdt = tb->d.widget;
1462 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1463 if (tPtr->flags.alignment != WALeft)
1464 lw += WMWidgetWidth(wdt);
1465 } else {
1466 line_height = WMAX(line_height,
1467 tb->d.pixmap->height + max_d);
1468 if (tPtr->flags.alignment != WALeft)
1469 lw += tb->d.pixmap->width;
1473 } else {
1474 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1475 max_d = WMAX(max_d, abs(font->height-font->y));
1476 line_height = WMAX(line_height, font->height + max_d);
1477 text = &(tb->text[items[i].begin]);
1478 len = items[i].end - items[i].begin;
1479 if (tPtr->flags.alignment != WALeft)
1480 lw += WMWidthOfString(font, text, len);
1484 if (tPtr->flags.alignment == WARight) {
1485 j = tPtr->visible.w - lw;
1486 } else if (tPtr->flags.alignment == WACenter) {
1487 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1490 for(i=0; i<nitems; i++) {
1491 tb = items[i].tb;
1493 if (tbsame == tb) { /* extend it, since it's on same line */
1494 tb->sections[tb->nsections-1].end = items[i].end;
1495 n = tb->nsections-1;
1496 } else {
1497 tb->sections = wrealloc(tb->sections,
1498 (++tb->nsections)*sizeof(Section));
1499 n = tb->nsections-1;
1500 tb->sections[n]._y = y + max_d;
1501 tb->sections[n].max_d = max_d;
1502 tb->sections[n].x = x+j;
1503 tb->sections[n].h = line_height;
1504 tb->sections[n].begin = items[i].begin;
1505 tb->sections[n].end = items[i].end;
1508 tb->sections[n].last = (i+1 == nitems);
1510 if (tb->graphic) {
1511 if (!tPtr->flags.monoFont) {
1512 if(tb->object) {
1513 WMWidget *wdt = tb->d.widget;
1514 tb->sections[n].y = max_d + y
1515 + line_height - WMWidgetHeight(wdt);
1516 tb->sections[n].w = WMWidgetWidth(wdt);
1517 } else {
1518 tb->sections[n].y = y + line_height
1519 + max_d - tb->d.pixmap->height;
1520 tb->sections[n].w = tb->d.pixmap->width;
1522 x += tb->sections[n].w;
1524 } else {
1525 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1526 len = items[i].end - items[i].begin;
1527 text = &(tb->text[items[i].begin]);
1529 tb->sections[n].y = y+line_height-font->y;
1530 tb->sections[n].w =
1531 WMWidthOfString(font,
1532 &(tb->text[tb->sections[n].begin]),
1533 tb->sections[n].end - tb->sections[n].begin);
1535 x += WMWidthOfString(font, text, len);
1538 tbsame = tb;
1541 return line_height;
1546 static void
1547 layOutDocument(Text *tPtr)
1549 TextBlock *tb;
1550 myLineItems *items = NULL;
1551 unsigned int itemsSize=0, nitems=0, begin, end;
1552 WMFont *font;
1553 unsigned int x, y=0, lw = 0, width=0, bmargin;
1554 char *start=NULL, *mark=NULL;
1556 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1557 return;
1559 assert(tPtr->visible.w > 20);
1561 tPtr->docWidth = tPtr->visible.w;
1562 x = tPtr->margins[tb->marginN].first;
1563 bmargin = tPtr->margins[tb->marginN].body;
1565 /* only partial layOut needed: re-Lay only affected textblocks */
1566 if (tPtr->flags.laidOut) {
1567 tb = tPtr->currentTextBlock;
1569 /* search backwards for textblocks on same line */
1570 while (tb->prior) {
1571 if (!tb->sections || tb->nsections<1) {
1572 tb = tPtr->firstTextBlock;
1573 tPtr->flags.laidOut = False;
1574 y = 0;
1575 goto _layOut;
1578 if(!tb->prior->sections || tb->prior->nsections<1) {
1579 tb = tPtr->firstTextBlock;
1580 tPtr->flags.laidOut = False;
1581 y = 0;
1582 goto _layOut;
1585 if (tb->sections[0]._y !=
1586 tb->prior->sections[tb->prior->nsections-1]._y) {
1587 break;
1589 tb = tb->prior;
1592 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1593 y = tb->prior->sections[tb->prior->nsections-1]._y +
1594 tb->prior->sections[tb->prior->nsections-1].h -
1595 tb->prior->sections[tb->prior->nsections-1].max_d;
1596 } else {
1597 y = 0;
1601 _layOut:
1602 while (tb) {
1604 if (tb->sections && tb->nsections>0) {
1605 wfree(tb->sections);
1606 tb->sections = NULL;
1607 tb->nsections = 0;
1610 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1611 TextBlock *next = tb->next;
1612 tPtr->currentTextBlock = tb;
1613 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1614 tb = next;
1615 tb->first = True;
1616 continue;
1619 if (tb->first && tb != tPtr->firstTextBlock) {
1620 y += layOutLine(tPtr, items, nitems, x, y);
1621 x = tPtr->margins[tb->marginN].first;
1622 bmargin = tPtr->margins[tb->marginN].body;
1623 nitems = 0;
1624 lw = 0;
1627 if (tb->graphic) {
1628 if (!tPtr->flags.monoFont) {
1629 if(tb->object)
1630 width = WMWidgetWidth(tb->d.widget);
1631 else
1632 width = tb->d.pixmap->width;
1634 if (width > tPtr->docWidth)
1635 tPtr->docWidth = width;
1637 lw += width;
1638 if (lw >= tPtr->visible.w - x ) {
1639 y += layOutLine(tPtr, items, nitems, x, y);
1640 nitems = 0;
1641 x = bmargin;
1642 lw = width;
1645 if(nitems + 1> itemsSize) {
1646 items = wrealloc(items,
1647 (++itemsSize)*sizeof(myLineItems));
1650 items[nitems].tb = tb;
1651 items[nitems].begin = 0;
1652 items[nitems].end = 0;
1653 nitems++;
1656 } else if ((start = tb->text)) {
1657 begin = end = 0;
1658 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1660 while (start) {
1661 mark = strchr(start, ' ');
1662 if (mark) {
1663 end += (int)(mark-start)+1;
1664 start = mark+1;
1665 } else {
1666 end += strlen(start);
1667 start = mark;
1670 if (end > tb->used)
1671 end = tb->used;
1673 if (end-begin > 0) {
1675 width = WMWidthOfString(font,
1676 &tb->text[begin], end-begin);
1678 /* if it won't fit, char wrap it */
1679 if (width >= tPtr->visible.w) {
1680 char *t = &tb->text[begin];
1681 int l=end-begin, i=0;
1682 do {
1683 width = WMWidthOfString(font, t, ++i);
1684 } while (width < tPtr->visible.w && i < l);
1685 if(i>2) i--;
1686 end = begin+i;
1687 start = &tb->text[end];
1690 lw += width;
1693 if (lw >= tPtr->visible.w - x) {
1694 y += layOutLine(tPtr, items, nitems, x, y);
1695 lw = width;
1696 x = bmargin;
1697 nitems = 0;
1700 if(nitems + 1 > itemsSize) {
1701 items = wrealloc(items,
1702 (++itemsSize)*sizeof(myLineItems));
1705 items[nitems].tb = tb;
1706 items[nitems].begin = begin;
1707 items[nitems].end = end;
1708 nitems++;
1710 begin = end;
1715 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1716 if(0&&tPtr->flags.laidOut
1717 && tb->next && tb->next->sections && tb->next->nsections>0
1718 && (tPtr->vpos + tPtr->visible.h
1719 < tb->next->sections[0]._y)) {
1720 if(tPtr->lastTextBlock->sections
1721 && tPtr->lastTextBlock->nsections > 0 ) {
1722 TextBlock *ltb = tPtr->lastTextBlock;
1723 int ly = ltb->sections[ltb->nsections-1]._y;
1724 int lh = ltb->sections[ltb->nsections-1].h;
1725 int ss, sd;
1727 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1728 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1730 y += layOutLine(tPtr, items, nitems, x, y);
1731 ss= ly+lh-y;
1732 sd = tPtr->docHeight-y;
1734 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1735 y += tb->next->sections[0]._y-y;
1736 nitems = 0;
1737 printf("nitems%d\n", nitems);
1738 if(ss-sd!=0)
1739 y = tPtr->docHeight+ss-sd;
1741 break;
1742 } else {
1743 tPtr->flags.laidOut = False;
1747 tb = tb->next;
1751 if (nitems > 0)
1752 y += layOutLine(tPtr, items, nitems, x, y);
1754 if (tPtr->docHeight != y+10) {
1755 tPtr->docHeight = y+10;
1756 updateScrollers(tPtr);
1759 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1760 XEvent event;
1762 tPtr->flags.horizOnDemand = True;
1763 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1764 event.type = Expose;
1765 handleEvents(&event, (void *)tPtr);
1767 } else if(tPtr->docWidth <= tPtr->visible.w
1768 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1769 tPtr->flags.horizOnDemand = False;
1770 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1773 tPtr->flags.laidOut = True;
1775 if(items && itemsSize > 0)
1776 wfree(items);
1781 static void
1782 textDidResize(W_ViewDelegate *self, WMView *view)
1784 Text *tPtr = (Text *)view->self;
1785 unsigned short w = tPtr->view->size.width;
1786 unsigned short h = tPtr->view->size.height;
1787 unsigned short rh = 0, vw = 0, rel;
1789 rel = (tPtr->flags.relief == WRFlat);
1791 if (tPtr->ruler && tPtr->flags.rulerShown) {
1792 WMMoveWidget(tPtr->ruler, 2, 2);
1793 WMResizeWidget(tPtr->ruler, w - 4, 40);
1794 rh = 40;
1797 if (tPtr->vS) {
1798 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1799 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1800 vw = 20;
1801 WMSetRulerOffset(tPtr->ruler,22);
1802 } else WMSetRulerOffset(tPtr->ruler, 2);
1804 if (tPtr->hS) {
1805 if (tPtr->vS) {
1806 WMMoveWidget(tPtr->hS, vw, h - 21);
1807 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1808 } else {
1809 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1810 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1814 tPtr->visible.x = (tPtr->vS)?24:4;
1815 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1816 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1817 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1818 tPtr->visible.h -= (tPtr->hS)?20:0;
1819 tPtr->margins[0].right = tPtr->visible.w;
1821 if (tPtr->view->flags.realized) {
1823 if (tPtr->db) {
1824 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1825 tPtr->db = (Pixmap) NULL;
1828 if (tPtr->visible.w < 40)
1829 tPtr->visible.w = 40;
1830 if (tPtr->visible.h < 20)
1831 tPtr->visible.h = 20;
1833 if(!tPtr->db) {
1834 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1835 tPtr->view->window, tPtr->visible.w,
1836 tPtr->visible.h, tPtr->view->screen->depth);
1840 WMThawText(tPtr);
1843 W_ViewDelegate _TextViewDelegate =
1845 NULL,
1846 NULL,
1847 textDidResize,
1848 NULL,
1851 #define TEXT_BUFFER_INCR 8
1852 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1854 static void
1855 clearText(Text *tPtr)
1857 tPtr->vpos = tPtr->hpos = 0;
1858 tPtr->docHeight = tPtr->docWidth = 0;
1859 tPtr->cursor.x = -23;
1861 if (!tPtr->firstTextBlock)
1862 return;
1864 while (tPtr->currentTextBlock)
1865 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1867 tPtr->firstTextBlock = NULL;
1868 tPtr->currentTextBlock = NULL;
1869 tPtr->lastTextBlock = NULL;
1870 WMEmptyArray(tPtr->gfxItems);
1873 /* possibly remove a single character from the currentTextBlock,
1874 or if there's a selection, remove it...
1875 note that Delete and Backspace are treated differently */
1876 static void
1877 deleteTextInteractively(Text *tPtr, KeySym ksym)
1879 TextBlock *tb;
1880 Bool back = (Bool) (ksym == XK_BackSpace);
1881 Bool done = 1, wasFirst = 0;
1883 if (!tPtr->flags.editable)
1884 return;
1886 if ( !(tb = tPtr->currentTextBlock) )
1887 return;
1889 if (tPtr->flags.ownsSelection) {
1890 if(removeSelection(tPtr))
1891 layOutDocument(tPtr);
1892 return;
1895 wasFirst = tb->first;
1896 if (back && tPtr->tpos < 1) {
1897 if (tb->prior) {
1898 if(tb->prior->blank) {
1899 tPtr->currentTextBlock = tb->prior;
1900 WMRemoveTextBlock(tPtr);
1901 tPtr->currentTextBlock = tb;
1902 tb->first = True;
1903 layOutDocument(tPtr);
1904 return;
1905 } else {
1906 if(tb->blank) {
1907 TextBlock *prior = tb->prior;
1908 tPtr->currentTextBlock = tb;
1909 WMRemoveTextBlock(tPtr);
1910 tb = prior;
1911 } else {
1912 tb = tb->prior;
1915 if(tb->graphic)
1916 tPtr->tpos = 1;
1917 else
1918 tPtr->tpos = tb->used;
1920 tPtr->currentTextBlock = tb;
1921 done = 1;
1922 if(wasFirst) {
1923 if(tb->next)
1924 tb->next->first = False;
1925 layOutDocument(tPtr);
1926 return;
1932 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1933 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1934 if (back)
1935 tPtr->tpos--;
1936 memmove(&(tb->text[tPtr->tpos]),
1937 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1938 tb->used--;
1939 done = 0;
1942 /* if there are no characters left to back over in the textblock,
1943 but it still has characters to the right of the cursor: */
1944 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1945 || tb->graphic) {
1947 /* no more chars, and it's marked as blank? */
1948 if(tb->blank) {
1949 TextBlock *sibling = (back? tb->prior : tb->next);
1951 if(tb->used == 0 || tb->graphic)
1952 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1954 if (sibling) {
1955 tPtr->currentTextBlock = sibling;
1956 if(tb->graphic)
1957 tPtr->tpos = (back? 1 : 0);
1958 else
1959 tPtr->tpos = (back? sibling->used : 0);
1961 /* no more chars, so mark it as blank */
1962 } else if(tb->used == 0) {
1963 tb->blank = 1;
1964 } else if(tb->graphic) {
1965 Bool hasNext = (Bool)(tb->next);
1967 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1968 if(hasNext) {
1969 tPtr->tpos = 0;
1970 } else if(tPtr->currentTextBlock) {
1971 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1972 1 : tPtr->currentTextBlock->used);
1974 } else printf("DEBUG: unaccounted for... catch this!\n");
1977 layOutDocument(tPtr);
1981 static void
1982 insertTextInteractively(Text *tPtr, char *text, int len)
1984 TextBlock *tb;
1985 char *newline = NULL;
1987 if (!tPtr->flags.editable) {
1988 return;
1991 if (len < 1 || !text)
1992 return;
1995 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1996 return;
1999 if (tPtr->flags.ownsSelection)
2000 removeSelection(tPtr);
2003 if (tPtr->flags.ignoreNewLine) {
2004 int i;
2005 for(i=0; i<len; i++) {
2006 if (text[i] == '\n')
2007 text[i] = ' ';
2011 tb = tPtr->currentTextBlock;
2012 if (!tb || tb->graphic) {
2013 tPtr->tpos = 0;
2014 WMAppendTextStream(tPtr, text);
2015 layOutDocument(tPtr);
2016 return;
2019 if ((newline = strchr(text, '\n'))) {
2020 int nlen = (int)(newline-text);
2021 int s = tb->used - tPtr->tpos;
2023 if (!tb->blank && nlen>0) {
2024 char *save;
2026 if (s > 0) {
2027 save = wmalloc(s);
2028 memcpy(save, &tb->text[tPtr->tpos], s);
2029 tb->used -= (tb->used - tPtr->tpos);
2031 insertTextInteractively(tPtr, text, nlen);
2032 newline++;
2033 WMAppendTextStream(tPtr, newline);
2034 if (s>0) {
2035 insertTextInteractively(tPtr, save, s);
2036 wfree(save);
2038 } else {
2039 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2040 && !tb->graphic && tb->text) {
2042 unsigned short savePos = tPtr->tpos;
2043 void *ntb = WMCreateTextBlockWithText(
2044 tPtr, &tb->text[tPtr->tpos],
2045 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2047 if(tb->sections[0].end == tPtr->tpos)
2048 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2049 NULL, tb->d.font, tb->color, True, 0));
2051 tb->used = savePos;
2052 WMAppendTextBlock(tPtr, ntb);
2053 tPtr->tpos = 0;
2055 } else if (tPtr->tpos == tb->used) {
2056 if(tPtr->flags.indentNewLine) {
2057 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2058 " ", tb->d.font, tb->color, True, 4));
2059 tPtr->tpos = 4;
2060 } else {
2061 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2062 NULL, tb->d.font, tb->color, True, 0));
2063 tPtr->tpos = 0;
2065 } else if (tPtr->tpos == 0) {
2066 if(tPtr->flags.indentNewLine) {
2067 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2068 " ", tb->d.font, tb->color, True, 4));
2069 } else {
2070 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2071 NULL, tb->d.font, tb->color, True, 0));
2073 tPtr->tpos = 0;
2074 if(tPtr->currentTextBlock->next)
2075 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2078 } else {
2079 if (tb->used + len >= tb->allocated) {
2080 tb->allocated = reqBlockSize(tb->used+len);
2081 tb->text = wrealloc(tb->text, tb->allocated);
2084 if (tb->blank) {
2085 memcpy(tb->text, text, len);
2086 tb->used = len;
2087 tPtr->tpos = len;
2088 tb->text[tb->used] = 0;
2089 tb->blank = False;
2091 } else {
2092 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2093 tb->used-tPtr->tpos+1);
2094 memmove(&tb->text[tPtr->tpos], text, len);
2095 tb->used += len;
2096 tPtr->tpos += len;
2097 tb->text[tb->used] = 0;
2102 layOutDocument(tPtr);
2106 static void
2107 selectRegion(Text *tPtr, int x, int y)
2110 if (x < 0 || y < 0)
2111 return;
2113 y += (tPtr->flags.rulerShown? 40: 0);
2114 y += tPtr->vpos;
2115 if (y>10)
2116 y -= 10; /* the original offset */
2118 x -= tPtr->visible.x-2;
2119 if (x<0)
2120 x=0;
2122 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2123 tPtr->sel.w = abs(tPtr->clicked.x - x);
2124 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2125 tPtr->sel.h = abs(tPtr->clicked.y - y);
2127 tPtr->flags.ownsSelection = True;
2128 paintText(tPtr);
2132 static void
2133 releaseSelection(Text *tPtr)
2135 TextBlock *tb = tPtr->firstTextBlock;
2137 while(tb) {
2138 tb->selected = False;
2139 tb = tb->next;
2141 tPtr->flags.ownsSelection = False;
2142 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2143 CurrentTime);
2145 paintText(tPtr);
2149 WMData*
2150 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2151 Atom *type)
2153 Text *tPtr = view->self;
2154 Display *dpy = tPtr->view->screen->display;
2155 Atom _TARGETS;
2156 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2157 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2158 WMData *data = NULL;
2161 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2162 char *text = WMGetTextSelectedStream(tPtr);
2164 if (text) {
2165 data = WMCreateDataWithBytes(text, strlen(text));
2166 WMSetDataFormat(data, TYPETEXT);
2168 *type = target;
2169 return data;
2170 } else printf("didn't get it\n");
2172 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2173 if (target == _TARGETS) {
2174 Atom *ptr;
2176 ptr = wmalloc(4 * sizeof(Atom));
2177 ptr[0] = _TARGETS;
2178 ptr[1] = XA_STRING;
2179 ptr[2] = TEXT;
2180 ptr[3] = COMPOUND_TEXT;
2182 data = WMCreateDataWithBytes(ptr, 4*4);
2183 WMSetDataFormat(data, 32);
2185 *type = target;
2186 return data;
2189 return NULL;
2193 static void
2194 lostHandler(WMView *view, Atom selection, void *cdata)
2196 releaseSelection((WMText *)view->self);
2200 static WMSelectionProcs selectionHandler = {
2201 requestHandler, lostHandler, NULL
2205 static void
2206 ownershipObserver(void *observerData, WMNotification *notification)
2208 if (observerData != WMGetNotificationClientData(notification))
2209 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2213 static void
2214 autoSelectText(Text *tPtr, int clicks)
2216 int x, start;
2217 TextBlock *tb;
2218 char *mark = NULL, behind, ahead;
2220 if(!(tb = tPtr->currentTextBlock))
2221 return;
2223 if(clicks == 2) {
2226 switch(tb->text[tPtr->tpos]) {
2227 case ' ': return;
2229 case '<': case '>': behind = '<'; ahead = '>'; break;
2230 case '{': case '}': behind = '{'; ahead = '}'; break;
2231 case '[': case ']': behind = '['; ahead = ']'; break;
2233 default: behind = ahead = ' ';
2236 tPtr->sel.y = tPtr->cursor.y+5;
2237 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2239 if(tb->graphic) {
2240 tPtr->sel.x = tb->sections[0].x;
2241 tPtr->sel.w = tb->sections[0].w;
2242 } else {
2243 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2245 start = tPtr->tpos;
2246 while(start > 0 && tb->text[start-1] != behind)
2247 start--;
2249 x = tPtr->cursor.x;
2250 if(tPtr->tpos > start){
2251 x -= WMWidthOfString(font, &tb->text[start],
2252 tPtr->tpos - start);
2254 tPtr->sel.x = (x<0?0:x)+1;
2256 if((mark = strchr(&tb->text[start], ahead))) {
2257 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2258 (int)(mark - &tb->text[start]));
2259 } else if(tb->used > start) {
2260 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2261 tb->used - start);
2265 } else if(clicks == 3) {
2266 TextBlock *cur = tb;
2268 while(tb && !tb->first) {
2269 tb = tb->prior;
2271 tPtr->sel.y = tb->sections[0]._y;
2273 tb = cur;
2274 while(tb->next && !tb->next->first) {
2275 tb = tb->next;
2277 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2278 + 5 - tPtr->sel.y;
2280 tPtr->sel.x = 0;
2281 tPtr->sel.w = tPtr->docWidth;
2282 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2285 if (!tPtr->flags.ownsSelection) {
2286 WMCreateSelectionHandler(tPtr->view,
2287 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2288 tPtr->flags.ownsSelection = True;
2290 paintText(tPtr);
2295 static void
2296 fontChanged(void *observerData, WMNotification *notification)
2298 WMText *tPtr = (WMText *) observerData;
2299 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2300 printf("fontChanged\n");
2302 if(!tPtr || !font)
2303 return;
2305 if (tPtr->flags.ownsSelection)
2306 WMSetTextSelectionFont(tPtr, font);
2310 static void
2311 handleTextKeyPress(Text *tPtr, XEvent *event)
2313 char buffer[64];
2314 KeySym ksym;
2315 int control_pressed = False;
2316 TextBlock *tb = NULL;
2318 if (((XKeyEvent *) event)->state & ControlMask)
2319 control_pressed = True;
2320 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2322 switch(ksym) {
2324 case XK_Home:
2325 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2326 tPtr->tpos = 0;
2327 updateCursorPosition(tPtr);
2328 paintText(tPtr);
2329 break;
2331 case XK_End:
2332 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2333 if(tPtr->currentTextBlock->graphic)
2334 tPtr->tpos = 1;
2335 else
2336 tPtr->tpos = tPtr->currentTextBlock->used;
2338 updateCursorPosition(tPtr);
2339 paintText(tPtr);
2340 break;
2342 case XK_Left:
2343 if(!(tb = tPtr->currentTextBlock))
2344 break;
2345 if(tb->graphic)
2346 goto L_imaGFX;
2348 if(tPtr->tpos==0) {
2349 L_imaGFX: if(tb->prior) {
2350 tPtr->currentTextBlock = tb->prior;
2351 if(tPtr->currentTextBlock->graphic)
2352 tPtr->tpos = 1;
2353 else
2354 tPtr->tpos = tPtr->currentTextBlock->used;
2356 if(!tb->first && tPtr->tpos > 0)
2357 tPtr->tpos--;
2358 } else tPtr->tpos = 0;
2359 } else tPtr->tpos--;
2360 updateCursorPosition(tPtr);
2361 paintText(tPtr);
2362 break;
2364 case XK_Right:
2365 if(!(tb = tPtr->currentTextBlock))
2366 break;
2367 if(tb->graphic)
2368 goto R_imaGFX;
2369 if(tPtr->tpos == tb->used) {
2370 R_imaGFX: if(tb->next) {
2371 tPtr->currentTextBlock = tb->next;
2372 tPtr->tpos = 0;
2373 if(!tb->next->first && tb->next->used>0)
2374 tPtr->tpos++;
2375 } else {
2376 if(tb->graphic)
2377 tPtr->tpos = 1;
2378 else
2379 tPtr->tpos = tb->used;
2381 } else tPtr->tpos++;
2382 updateCursorPosition(tPtr);
2383 paintText(tPtr);
2384 break;
2386 case XK_Down:
2387 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2388 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2389 paintText(tPtr);
2390 break;
2392 case XK_Up:
2393 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2394 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2395 paintText(tPtr);
2396 break;
2398 case XK_BackSpace:
2399 case XK_Delete:
2400 #ifdef XK_KP_Delete
2401 case XK_KP_Delete:
2402 #endif
2403 deleteTextInteractively(tPtr, ksym);
2404 updateCursorPosition(tPtr);
2405 paintText(tPtr);
2406 break;
2408 case XK_Control_R :
2409 case XK_Control_L :
2410 control_pressed = True;
2411 break;
2413 case XK_Tab:
2414 insertTextInteractively(tPtr, " ", 4);
2415 updateCursorPosition(tPtr);
2416 paintText(tPtr);
2417 break;
2419 case XK_Return:
2420 *buffer = '\n';
2421 default:
2422 if (*buffer != 0 && !control_pressed) {
2423 insertTextInteractively(tPtr, buffer, strlen(buffer));
2424 updateCursorPosition(tPtr);
2425 paintText(tPtr);
2427 } else if (control_pressed && ksym==XK_r) {
2428 Bool i = !tPtr->flags.rulerShown;
2429 WMShowTextRuler(tPtr, i);
2430 tPtr->flags.rulerShown = i;
2432 else if (control_pressed && *buffer == '\a')
2433 XBell(tPtr->view->screen->display, 0);
2434 else
2435 WMRelayToNextResponder(tPtr->view, event);
2438 if (!control_pressed && tPtr->flags.ownsSelection)
2439 releaseSelection(tPtr);
2443 static void
2444 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2445 void *cdata, WMData *data)
2447 Text *tPtr = (Text *)view->self;
2448 char *text;
2450 tPtr->flags.waitingForSelection = 0;
2452 if (data) {
2453 text = (char*)WMDataBytes(data);
2455 if (tPtr->parser) {
2456 (tPtr->parser) (tPtr, (void *) text);
2457 layOutDocument(tPtr);
2458 } else insertTextInteractively(tPtr, text, strlen(text));
2459 updateCursorPosition(tPtr);
2460 paintText(tPtr);
2462 } else {
2463 int n;
2465 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2467 if (text) {
2468 text[n] = 0;
2469 if (tPtr->parser) {
2470 (tPtr->parser) (tPtr, (void *) text);
2471 layOutDocument(tPtr);
2472 } else insertTextInteractively(tPtr, text, n);
2473 updateCursorPosition(tPtr);
2474 paintText(tPtr);
2476 XFree(text);
2484 static void
2485 handleActionEvents(XEvent *event, void *data)
2487 Text *tPtr = (Text *)data;
2488 Display *dpy = event->xany.display;
2489 KeySym ksym;
2492 switch (event->type) {
2493 case KeyPress:
2494 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2495 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2496 tPtr->flags.extendSelection = True;
2497 return;
2500 if (tPtr->flags.focused) {
2501 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2502 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2503 GrabModeAsync, GrabModeAsync, None,
2504 tPtr->view->screen->invisibleCursor, CurrentTime);
2505 tPtr->flags.pointerGrabbed = True;
2506 handleTextKeyPress(tPtr, event);
2508 } break;
2510 case KeyRelease:
2511 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2512 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2513 tPtr->flags.extendSelection = False;
2514 return;
2515 /* end modify flag so selection can be extended */
2517 break;
2520 case MotionNotify:
2522 if (tPtr->flags.pointerGrabbed) {
2523 tPtr->flags.pointerGrabbed = False;
2524 XUngrabPointer(dpy, CurrentTime);
2527 if(tPtr->flags.waitingForSelection)
2528 break;
2530 if ((event->xmotion.state & Button1Mask)) {
2531 TextBlock *tb = tPtr->currentTextBlock;
2533 if(tb && tPtr->flags.isOverGraphic &&
2534 tb->graphic && !tb->object) {
2535 WMSize offs;
2536 WMPixmap *pixmap = tb->d.pixmap;
2537 char *types[2] = {"application/X-image", NULL};
2539 offs.width = 2;
2540 offs.height = 2;
2542 WMDragImageFromView(tPtr->view, pixmap, types,
2543 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2544 offs, event, True);
2547 } else {
2548 if (!tPtr->flags.ownsSelection) {
2549 WMCreateSelectionHandler(tPtr->view,
2550 XA_PRIMARY, event->xbutton.time,
2551 &selectionHandler, NULL);
2552 tPtr->flags.ownsSelection = True;
2555 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2556 break;
2559 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2560 break;
2563 case ButtonPress:
2565 if (tPtr->flags.pointerGrabbed) {
2566 tPtr->flags.pointerGrabbed = False;
2567 XUngrabPointer(dpy, CurrentTime);
2568 break;
2571 if (tPtr->flags.waitingForSelection)
2572 break;
2574 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2575 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2576 return;
2579 if (tPtr->flags.ownsSelection)
2580 releaseSelection(tPtr);
2583 if (event->xbutton.button == Button1) {
2585 if(WMIsDoubleClick(event)) {
2586 TextBlock *tb = tPtr->currentTextBlock;
2588 tPtr->lastClickTime = event->xbutton.time;
2589 if(tb && tb->graphic && !tb->object) {
2590 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2591 char *desc;
2593 desc = wmalloc(tb->used+1);
2594 memcpy(desc, tb->text, tb->used);
2595 desc[tb->used] = 0;
2596 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2597 wfree(desc);
2599 } else {
2600 autoSelectText(tPtr, 2);
2602 break;
2603 } else if(event->xbutton.time - tPtr->lastClickTime
2604 < WINGsConfiguration.doubleClickDelay) {
2605 tPtr->lastClickTime = event->xbutton.time;
2606 autoSelectText(tPtr, 3);
2607 break;
2610 if (!tPtr->flags.focused) {
2611 WMSetFocusToWidget(tPtr);
2612 tPtr->flags.focused = True;
2615 tPtr->lastClickTime = event->xbutton.time;
2616 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2617 paintText(tPtr);
2620 if (event->xbutton.button
2621 == WINGsConfiguration.mouseWheelDown) {
2622 WMScrollText(tPtr, 16);
2623 break;
2626 if (event->xbutton.button
2627 == WINGsConfiguration.mouseWheelUp) {
2628 WMScrollText(tPtr, -16);
2629 break;
2632 if (event->xbutton.button == Button2) {
2633 char *text = NULL;
2634 int n;
2636 if (!tPtr->flags.editable) {
2637 XBell(dpy, 0);
2638 break;
2641 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2642 event->xbutton.time, pasteText, NULL)) {
2644 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2645 tPtr->flags.waitingForSelection = 0;
2647 if (text) {
2648 text[n] = 0;
2650 if (tPtr->parser) {
2651 (tPtr->parser) (tPtr, (void *) text);
2652 layOutDocument(tPtr);
2654 else
2655 insertTextInteractively(tPtr, text, n);
2657 XFree(text);
2658 #if 0
2659 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2660 (void*)WMInsertTextEvent);
2661 #endif
2662 updateCursorPosition(tPtr);
2663 paintText(tPtr);
2665 } else {
2666 tPtr->flags.waitingForSelection = True;
2669 break;
2673 case ButtonRelease:
2674 if (tPtr->flags.pointerGrabbed) {
2675 tPtr->flags.pointerGrabbed = False;
2676 XUngrabPointer(dpy, CurrentTime);
2677 break;
2680 if (tPtr->flags.waitingForSelection)
2681 break;
2687 static void
2688 handleEvents(XEvent *event, void *data)
2690 Text *tPtr = (Text *)data;
2692 switch(event->type) {
2693 case Expose:
2695 if (event->xexpose.count!=0)
2696 break;
2698 if(tPtr->hS) {
2699 if (!(W_VIEW(tPtr->hS))->flags.realized)
2700 WMRealizeWidget(tPtr->hS);
2703 if(tPtr->vS) {
2704 if (!(W_VIEW(tPtr->vS))->flags.realized)
2705 WMRealizeWidget(tPtr->vS);
2708 if(tPtr->ruler) {
2709 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2710 WMRealizeWidget(tPtr->ruler);
2714 if(!tPtr->db)
2715 textDidResize(tPtr->view->delegate, tPtr->view);
2717 paintText(tPtr);
2718 break;
2720 case FocusIn:
2721 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2722 != tPtr->view)
2723 return;
2724 tPtr->flags.focused = True;
2725 #if DO_BLINK
2726 if (tPtr->flags.editable && !tPtr->timerID) {
2727 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2728 blinkCursor, tPtr);
2730 #endif
2732 break;
2734 case FocusOut:
2735 tPtr->flags.focused = False;
2736 paintText(tPtr);
2737 #if DO_BLINK
2738 if (tPtr->timerID) {
2739 WMDeleteTimerHandler(tPtr->timerID);
2740 tPtr->timerID = NULL;
2742 #endif
2743 break;
2746 case DestroyNotify:
2747 clearText(tPtr);
2748 if(tPtr->db)
2749 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2750 if(tPtr->gfxItems)
2751 WMEmptyArray(tPtr->gfxItems);
2752 #if DO_BLINK
2753 if (tPtr->timerID)
2754 WMDeleteTimerHandler(tPtr->timerID);
2755 #endif
2756 WMReleaseFont(tPtr->dFont);
2757 WMReleaseColor(tPtr->dColor);
2758 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2759 WMRemoveNotificationObserver(tPtr);
2761 wfree(tPtr);
2763 break;
2769 static void
2770 insertPlainText(Text *tPtr, char *text)
2772 char *start, *mark;
2773 void *tb = NULL;
2775 start = text;
2776 while (start) {
2777 mark = strchr(start, '\n');
2778 if (mark) {
2779 tb = WMCreateTextBlockWithText(tPtr,
2780 start, tPtr->dFont,
2781 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2782 start = mark+1;
2783 tPtr->flags.first = True;
2784 } else {
2785 if (start && strlen(start)) {
2786 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2787 tPtr->dColor, tPtr->flags.first, strlen(start));
2788 } else tb = NULL;
2789 tPtr->flags.first = False;
2790 start = mark;
2793 if (tPtr->flags.prepend)
2794 WMPrependTextBlock(tPtr, tb);
2795 else
2796 WMAppendTextBlock(tPtr, tb);
2801 static void
2802 rulerMoveCallBack(WMWidget *w, void *self)
2804 Text *tPtr = (Text *)self;
2806 if (!tPtr)
2807 return;
2808 if (W_CLASS(tPtr) != WC_Text)
2809 return;
2811 paintText(tPtr);
2815 static void
2816 rulerReleaseCallBack(WMWidget *w, void *self)
2818 Text *tPtr = (Text *)self;
2820 if (!tPtr)
2821 return;
2822 if (W_CLASS(tPtr) != WC_Text)
2823 return;
2825 WMThawText(tPtr);
2826 return;
2829 static unsigned
2830 draggingSourceOperation(WMView *self, Bool local)
2832 return WDOperationCopy;
2835 static WMData*
2836 fetchDragData(WMView *self, char *type)
2838 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2839 char *desc;
2840 WMData *data;
2842 if (!tb)
2843 return NULL;
2845 printf("type is [%s]\n", type);
2846 desc = wmalloc(tb->used+1);
2847 memcpy(desc, tb->text, tb->used);
2848 desc[tb->used] = 0;
2849 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2851 wfree(desc);
2853 return data;
2857 static WMDragSourceProcs _DragSourceProcs = {
2858 draggingSourceOperation,
2859 NULL,
2860 NULL,
2861 fetchDragData
2865 static unsigned
2866 draggingEntered(WMView *self, WMDraggingInfo *info)
2868 printf("draggingEntered\n");
2869 return WDOperationCopy;
2873 static unsigned
2874 draggingUpdated(WMView *self, WMDraggingInfo *info)
2876 return WDOperationCopy;
2880 static void
2881 draggingExited(WMView *self, WMDraggingInfo *info)
2883 printf("draggingExited\n");
2886 static Bool
2887 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2889 printf("prepareForDragOperation\n");
2890 return True;
2894 char *badbadbad;
2896 static void
2897 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2898 void *cdata, WMData *data)
2900 badbadbad = wstrdup((char *)WMDataBytes(data));
2904 /* when it's done in WINGs, remove this */
2906 Bool
2907 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2909 WMScreen *scr = W_VIEW_SCREEN(view);
2911 if (!WMRequestSelection(scr->dragInfo.destView,
2912 scr->xdndSelectionAtom,
2913 XInternAtom(scr->display, type, False),
2914 scr->dragInfo.timestamp,
2915 receivedData, &scr->dragInfo)) {
2916 wwarning("could not request data for dropped data");
2920 XEvent ev;
2922 ev.type = ClientMessage;
2923 ev.xclient.message_type = scr->xdndFinishedAtom;
2924 ev.xclient.format = 32;
2925 ev.xclient.window = info->destinationWindow;
2926 ev.xclient.data.l[0] = 0;
2927 ev.xclient.data.l[1] = 0;
2928 ev.xclient.data.l[2] = 0;
2929 ev.xclient.data.l[3] = 0;
2930 ev.xclient.data.l[4] = 0;
2932 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2933 XFlush(scr->display);
2935 return True;
2938 static Bool
2939 performDragOperation(WMView *self, WMDraggingInfo *info)
2941 WMColor *color;
2942 WMText *tPtr = (WMText *)self->self;
2944 if (!tPtr)
2945 return True;
2947 requestDroppedData(tPtr->view, info, "application/X-color");
2948 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2949 if(color) {
2950 WMSetTextSelectionColor(tPtr, color);
2951 WMReleaseColor(color);
2956 return True;
2959 static void
2960 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2962 printf("concludeDragOperation\n");
2966 static WMDragDestinationProcs _DragDestinationProcs = {
2967 draggingEntered,
2968 draggingUpdated,
2969 draggingExited,
2970 prepareForDragOperation,
2971 performDragOperation,
2972 concludeDragOperation
2976 char *
2977 getStream(WMText *tPtr, int sel, int array)
2979 TextBlock *tb = NULL;
2980 char *text = NULL;
2981 unsigned long where = 0;
2983 if (!tPtr)
2984 return NULL;
2986 if (!(tb = tPtr->firstTextBlock))
2987 return NULL;
2989 if (tPtr->writer) {
2990 (tPtr->writer) (tPtr, (void *) text);
2991 return text;
2994 tb = tPtr->firstTextBlock;
2995 while (tb) {
2997 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2999 if (!sel || (tb->graphic && tb->selected)) {
3001 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3002 && tb != tPtr->firstTextBlock) {
3003 text = wrealloc(text, where+1);
3004 text[where++] = '\n';
3007 if(tb->blank)
3008 goto _gSnext;
3010 if(tb->graphic && array) {
3011 text = wrealloc(text, where+4);
3012 text[where++] = 0xFA;
3013 text[where++] = (tb->used>>8)&0x0ff;
3014 text[where++] = tb->used&0x0ff;
3015 text[where++] = tb->allocated; /* extra info */
3017 text = wrealloc(text, where+tb->used);
3018 memcpy(&text[where], tb->text, tb->used);
3019 where += tb->used;
3022 } else if (sel && tb->selected) {
3024 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3025 text = wrealloc(text, where+1);
3026 text[where++] = '\n';
3029 if(tb->blank)
3030 goto _gSnext;
3032 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3033 memcpy(&text[where], &tb->text[tb->s_begin],
3034 tb->s_end - tb->s_begin);
3035 where += tb->s_end - tb->s_begin;
3040 _gSnext:tb = tb->next;
3043 /* +1 for the end of string, let's be nice */
3044 text = wrealloc(text, where+1);
3045 text[where] = 0;
3046 return text;
3050 static void
3051 releaseStreamObjects(void *data)
3053 if(data)
3054 wfree(data);
3057 WMArray *
3058 getStreamObjects(WMText *tPtr, int sel)
3060 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3061 WMData *data;
3062 char *stream;
3063 unsigned short len;
3064 char *start, *fa, *desc;
3066 stream = getStream(tPtr, sel, 1);
3067 if(!stream)
3068 return NULL;
3070 start = stream;
3071 while (start) {
3073 fa = strchr(start, 0xFA);
3074 if (fa) {
3075 if((int)(fa - start)>0) {
3076 desc = start;
3077 desc[(int)(fa - start)] = 0;
3078 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3079 WMSetDataFormat(data, TYPETEXT);
3080 WMAddToArray(array, (void *) data);
3083 len = *(fa+1)*0xff + *(fa+2);
3084 data = WMCreateDataWithBytes((void *)(fa+4), len);
3085 WMSetDataFormat(data, *(fa+3));
3086 WMAddToArray(array, (void *) data);
3087 start = fa + len + 4;
3089 } else {
3090 if (start && strlen(start)) {
3091 data = WMCreateDataWithBytes((void *)start, strlen(start));
3092 WMSetDataFormat(data, TYPETEXT);
3093 WMAddToArray(array, (void *) data);
3095 start = fa;
3099 wfree(stream);
3100 return array;
3104 WMText *
3105 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3107 Text *tPtr;
3108 Display *dpy;
3109 WMScreen *scr;
3110 XGCValues gcv;
3112 tPtr = wmalloc(sizeof(Text));
3113 memset(tPtr, 0, sizeof(Text));
3114 tPtr->widgetClass = WC_Text;
3115 tPtr->view = W_CreateView(W_VIEW(parent));
3116 if (!tPtr->view) {
3117 perror("could not create text's view\n");
3118 wfree(tPtr);
3119 return NULL;
3122 dpy = tPtr->view->screen->display;
3123 scr = tPtr->view->screen;
3125 tPtr->view->self = tPtr;
3126 tPtr->view->attribs.cursor = scr->textCursor;
3127 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3128 W_ResizeView(tPtr->view, 250, 200);
3130 tPtr->dColor = WMBlackColor(scr);
3131 tPtr->fgColor = WMBlackColor(scr);
3132 tPtr->bgColor = WMWhiteColor(scr);
3133 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3135 gcv.graphics_exposures = False;
3136 gcv.foreground = W_PIXEL(scr->gray);
3137 gcv.background = W_PIXEL(scr->darkGray);
3138 gcv.fill_style = FillStippled;
3139 /* why not use scr->stipple here? */
3140 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS,
3141 STIPPLE_WIDTH, STIPPLE_HEIGHT);
3142 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3143 GCForeground|GCBackground|GCStipple
3144 |GCFillStyle|GCGraphicsExposures, &gcv);
3146 tPtr->ruler = NULL;
3147 tPtr->vS = NULL;
3148 tPtr->hS = NULL;
3150 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3152 tPtr->view->delegate = &_TextViewDelegate;
3154 tPtr->delegate = NULL;
3156 #if DO_BLINK
3157 tPtr->timerID = NULL;
3158 #endif
3160 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3161 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3162 handleEvents, tPtr);
3164 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3165 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3166 handleActionEvents, tPtr);
3168 WMAddNotificationObserver(ownershipObserver, tPtr,
3169 WMSelectionOwnerDidChangeNotification,
3170 tPtr);
3172 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3173 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3177 char *types[3] = {"application/X-color", "application/X-image", NULL};
3178 WMRegisterViewForDraggedTypes(tPtr->view, types);
3181 /*WMAddNotificationObserver(fontChanged, tPtr,
3182 WMFontPanelDidChangeNotification, tPtr);*/
3184 tPtr->firstTextBlock = NULL;
3185 tPtr->lastTextBlock = NULL;
3186 tPtr->currentTextBlock = NULL;
3187 tPtr->tpos = 0;
3189 tPtr->gfxItems = WMCreateArray(4);
3191 tPtr->parser = parser;
3192 tPtr->writer = writer;
3194 tPtr->sel.x = tPtr->sel.y = 2;
3195 tPtr->sel.w = tPtr->sel.h = 0;
3197 tPtr->clicked.x = tPtr->clicked.y = 2;
3199 tPtr->visible.x = tPtr->visible.y = 2;
3200 tPtr->visible.h = tPtr->view->size.height;
3201 tPtr->visible.w = tPtr->view->size.width - 4;
3203 tPtr->cursor.x = -23;
3205 tPtr->docWidth = 0;
3206 tPtr->docHeight = 0;
3207 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3208 default_bullet);
3209 tPtr->db = (Pixmap) NULL;
3210 tPtr->bgPixmap = NULL;
3212 tPtr->margins = WMGetRulerMargins(NULL);
3213 tPtr->margins->right = tPtr->visible.w;
3214 tPtr->nMargins = 1;
3216 tPtr->flags.rulerShown = False;
3217 tPtr->flags.monoFont = False;
3218 tPtr->flags.focused = False;
3219 tPtr->flags.editable = True;
3220 tPtr->flags.ownsSelection = False;
3221 tPtr->flags.pointerGrabbed = False;
3222 tPtr->flags.extendSelection = False;
3223 tPtr->flags.frozen = False;
3224 tPtr->flags.cursorShown = True;
3225 tPtr->flags.acceptsGraphic = False;
3226 tPtr->flags.horizOnDemand = False;
3227 tPtr->flags.needsLayOut = False;
3228 tPtr->flags.ignoreNewLine = False;
3229 tPtr->flags.indentNewLine = False;
3230 tPtr->flags.laidOut = False;
3231 tPtr->flags.ownsSelection = False;
3232 tPtr->flags.waitingForSelection = False;
3233 tPtr->flags.prepend = False;
3234 tPtr->flags.isOverGraphic = False;
3235 tPtr->flags.relief = WRSunken;
3236 tPtr->flags.isOverGraphic = 0;
3237 tPtr->flags.alignment = WALeft;
3238 tPtr->flags.first = True;
3240 return tPtr;
3243 void
3244 WMPrependTextStream(WMText *tPtr, char *text)
3246 CHECK_CLASS(tPtr, WC_Text);
3248 if(!text) {
3249 if (tPtr->flags.ownsSelection)
3250 releaseSelection(tPtr);
3251 clearText(tPtr);
3252 updateScrollers(tPtr);
3253 return;
3256 tPtr->flags.prepend = True;
3257 if (text && tPtr->parser)
3258 (tPtr->parser) (tPtr, (void *) text);
3259 else
3260 insertPlainText(tPtr, text);
3262 tPtr->flags.needsLayOut = True;
3263 tPtr->tpos = 0;
3264 if(!tPtr->flags.frozen) {
3265 layOutDocument(tPtr);
3270 void
3271 WMAppendTextStream(WMText *tPtr, char *text)
3273 CHECK_CLASS(tPtr, WC_Text);
3275 if(!text) {
3276 if (tPtr->flags.ownsSelection)
3277 releaseSelection(tPtr);
3278 clearText(tPtr);
3279 updateScrollers(tPtr);
3280 return;
3283 tPtr->flags.prepend = False;
3284 if (text && tPtr->parser)
3285 (tPtr->parser) (tPtr, (void *) text);
3286 else
3287 insertPlainText(tPtr, text);
3289 tPtr->flags.needsLayOut = True;
3290 if(tPtr->currentTextBlock) {
3291 if(tPtr->currentTextBlock->graphic)
3292 tPtr->tpos = 1;
3293 else
3294 tPtr->tpos = tPtr->currentTextBlock->used;
3297 if(!tPtr->flags.frozen) {
3298 layOutDocument(tPtr);
3303 char *
3304 WMGetTextStream(WMText *tPtr)
3306 CHECK_CLASS(tPtr, WC_Text);
3307 return getStream(tPtr, 0, 0);
3310 char *
3311 WMGetTextSelectedStream(WMText *tPtr)
3313 CHECK_CLASS(tPtr, WC_Text);
3314 return getStream(tPtr, 1, 0);
3317 WMArray *
3318 WMGetTextObjects(WMText *tPtr)
3320 CHECK_CLASS(tPtr, WC_Text);
3321 return getStreamObjects(tPtr, 0);
3324 WMArray *
3325 WMGetTextSelectedObjects(WMText *tPtr)
3327 CHECK_CLASS(tPtr, WC_Text);
3328 return getStreamObjects(tPtr, 1);
3332 void
3333 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3335 CHECK_CLASS(tPtr, WC_Text);
3337 tPtr->delegate = delegate;
3341 void *
3342 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3343 char *description, WMColor *color,
3344 unsigned short first, unsigned short extraInfo)
3346 TextBlock *tb;
3348 if (!w || !description || !color)
3349 return NULL;
3351 tb = wmalloc(sizeof(TextBlock));
3352 if (!tb)
3353 return NULL;
3355 tb->text = wstrdup(description);
3356 tb->used = strlen(description);
3357 tb->blank = False;
3358 tb->d.widget = w;
3359 tb->color = WMRetainColor(color);
3360 tb->marginN = newMargin(tPtr, NULL);
3361 tb->allocated = extraInfo;
3362 tb->first = first;
3363 tb->kanji = False;
3364 tb->graphic = True;
3365 tb->object = True;
3366 tb->underlined = False;
3367 tb->selected = False;
3368 tb->script = 0;
3369 tb->sections = NULL;
3370 tb->nsections = 0;
3371 tb->prior = NULL;
3372 tb->next = NULL;
3374 return tb;
3378 void *
3379 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3380 char *description, WMColor *color,
3381 unsigned short first, unsigned short extraInfo)
3383 TextBlock *tb;
3385 if (!p || !description || !color)
3386 return NULL;
3388 tb = wmalloc(sizeof(TextBlock));
3389 if (!tb)
3390 return NULL;
3392 tb->text = wstrdup(description);
3393 tb->used = strlen(description);
3394 tb->blank = False;
3395 tb->d.pixmap = WMRetainPixmap(p);
3396 tb->color = WMRetainColor(color);
3397 tb->marginN = newMargin(tPtr, NULL);
3398 tb->allocated = extraInfo;
3399 tb->first = first;
3400 tb->kanji = False;
3401 tb->graphic = True;
3402 tb->object = False;
3403 tb->underlined = False;
3404 tb->selected = False;
3405 tb->script = 0;
3406 tb->sections = NULL;
3407 tb->nsections = 0;
3408 tb->prior = NULL;
3409 tb->next = NULL;
3411 return tb;
3415 void*
3416 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3417 unsigned short first, unsigned short len)
3419 TextBlock *tb;
3421 if (!font || !color)
3422 return NULL;
3424 tb = wmalloc(sizeof(TextBlock));
3425 if (!tb)
3426 return NULL;
3428 tb->allocated = reqBlockSize(len);
3429 tb->text = (char *)wmalloc(tb->allocated);
3430 memset(tb->text, 0, tb->allocated);
3432 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3433 *tb->text = ' ';
3434 tb->used = 1;
3435 tb->blank = True;
3436 } else {
3437 memcpy(tb->text, text, len);
3438 tb->used = len;
3439 tb->blank = False;
3441 tb->text[tb->used] = 0;
3443 tb->d.font = WMRetainFont(font);
3444 tb->color = WMRetainColor(color);
3445 tb->marginN = newMargin(tPtr, NULL);
3446 tb->first = first;
3447 tb->kanji = False;
3448 tb->graphic = False;
3449 tb->underlined = False;
3450 tb->selected = False;
3451 tb->script = 0;
3452 tb->sections = NULL;
3453 tb->nsections = 0;
3454 tb->prior = NULL;
3455 tb->next = NULL;
3456 return tb;
3459 void
3460 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3461 unsigned int kanji, unsigned int underlined, int script,
3462 WMRulerMargins *margins)
3464 TextBlock *tb = (TextBlock *) vtb;
3465 if (!tb)
3466 return;
3468 tb->first = first;
3469 tb->kanji = kanji;
3470 tb->underlined = underlined;
3471 tb->script = script;
3472 tb->marginN = newMargin(tPtr, margins);
3475 void
3476 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3477 unsigned int *kanji, unsigned int *underlined, int *script,
3478 WMRulerMargins *margins)
3480 TextBlock *tb = (TextBlock *) vtb;
3481 if (!tb)
3482 return;
3484 if (first) *first = tb->first;
3485 if (kanji) *kanji = tb->kanji;
3486 if (underlined) *underlined = tb->underlined;
3487 if (script) *script = tb->script;
3488 if (margins) margins = &tPtr->margins[tb->marginN];
3493 void
3494 WMPrependTextBlock(WMText *tPtr, void *vtb)
3496 TextBlock *tb = (TextBlock *)vtb;
3498 if (!tPtr || !tb)
3499 return;
3501 if (tb->graphic) {
3502 if(tb->object) {
3503 WMWidget *w = tb->d.widget;
3504 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3505 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3506 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3509 WMAddToArray(tPtr->gfxItems, (void *)tb);
3510 tPtr->tpos = 1;
3512 } else {
3513 tPtr->tpos = tb->used;
3516 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3517 tb->next = tb->prior = NULL;
3518 tb->first = True;
3519 tPtr->lastTextBlock = tPtr->firstTextBlock
3520 = tPtr->currentTextBlock = tb;
3521 return;
3524 if(!tb->first) {
3525 tb->marginN = tPtr->currentTextBlock->marginN;
3528 tb->next = tPtr->currentTextBlock;
3529 tb->prior = tPtr->currentTextBlock->prior;
3530 if (tPtr->currentTextBlock->prior)
3531 tPtr->currentTextBlock->prior->next = tb;
3533 tPtr->currentTextBlock->prior = tb;
3534 if (!tb->prior)
3535 tPtr->firstTextBlock = tb;
3537 tPtr->currentTextBlock = tb;
3541 void
3542 WMAppendTextBlock(WMText *tPtr, void *vtb)
3544 TextBlock *tb = (TextBlock *)vtb;
3546 if (!tPtr || !tb)
3547 return;
3549 if (tb->graphic) {
3550 if(tb->object) {
3551 WMWidget *w = tb->d.widget;
3552 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3553 (W_VIEW(w))->attribs.cursor =
3554 tPtr->view->screen->defaultCursor;
3555 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3558 WMAddToArray(tPtr->gfxItems, (void *)tb);
3559 tPtr->tpos = 1;
3561 } else {
3562 tPtr->tpos = tb->used;
3566 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3567 tb->next = tb->prior = NULL;
3568 tb->first = True;
3569 tPtr->lastTextBlock = tPtr->firstTextBlock
3570 = tPtr->currentTextBlock = tb;
3571 return;
3574 if(!tb->first) {
3575 tb->marginN = tPtr->currentTextBlock->marginN;
3578 tb->next = tPtr->currentTextBlock->next;
3579 tb->prior = tPtr->currentTextBlock;
3580 if (tPtr->currentTextBlock->next)
3581 tPtr->currentTextBlock->next->prior = tb;
3583 tPtr->currentTextBlock->next = tb;
3585 if (!tb->next)
3586 tPtr->lastTextBlock = tb;
3588 tPtr->currentTextBlock = tb;
3592 void*
3593 WMRemoveTextBlock(WMText *tPtr)
3595 TextBlock *tb = NULL;
3597 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3598 || !tPtr->currentTextBlock) {
3599 return NULL;
3602 tb = tPtr->currentTextBlock;
3603 if (tb->graphic) {
3604 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3606 if(tb->object) {
3607 WMUnmapWidget(tb->d.widget);
3611 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3612 if (tPtr->currentTextBlock->next)
3613 tPtr->currentTextBlock->next->prior = NULL;
3615 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3616 tPtr->currentTextBlock = tPtr->firstTextBlock;
3618 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3619 tPtr->currentTextBlock->prior->next = NULL;
3620 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3621 tPtr->currentTextBlock = tPtr->lastTextBlock;
3622 } else {
3623 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3624 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3625 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3628 return (void *)tb;
3632 #if 0
3633 static void
3634 destroyWidget(WMWidget *widget)
3636 WMDestroyWidget(widget);
3637 // -- never do this -- wfree(widget);
3639 #endif
3642 void
3643 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3645 TextBlock *tb = (TextBlock *)vtb;
3646 if (!tPtr || !tb)
3647 return;
3649 if (tb->graphic) {
3650 if(tb->object) {
3651 /* naturally, there's a danger to destroying widgets whose action
3652 * brings us here: ie. press a button to destroy it...
3653 * need to find a safer way. till then... this stays commented out */
3654 /* 5 months later... destroy it 10 seconds after now which should
3655 * be enough time for the widget's action to be completed... :-) */
3656 /* This is a bad assumption. Just destroy the widget here.
3657 * if the caller needs it, it can protect it with W_RetainView()
3658 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3659 WMDestroyWidget(tb->d.widget);
3660 } else {
3661 WMReleasePixmap(tb->d.pixmap);
3663 } else {
3664 WMReleaseFont(tb->d.font);
3667 WMReleaseColor(tb->color);
3668 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3669 if (tb->sections)
3670 wfree(tb->sections);
3671 wfree(tb->text);
3672 wfree(tb);
3676 void
3677 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3679 if (!tPtr)
3680 return;
3682 WMReleaseColor(tPtr->fgColor);
3683 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3685 paintText(tPtr);
3688 void
3689 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3691 if (!tPtr)
3692 return;
3694 WMReleaseColor(tPtr->bgColor);
3695 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3696 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3698 paintText(tPtr);
3701 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3703 if (!tPtr)
3704 return;
3706 if (tPtr->bgPixmap)
3707 WMReleasePixmap(tPtr->bgPixmap);
3709 if (pixmap)
3710 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3711 else
3712 tPtr->bgPixmap = NULL;
3715 void
3716 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3718 if (!tPtr)
3719 return;
3720 tPtr->flags.relief = relief;
3721 textDidResize(tPtr->view->delegate, tPtr->view);
3724 void
3725 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3727 if (!tPtr)
3728 return;
3730 if (shouldhave && !tPtr->hS) {
3731 tPtr->hS = WMCreateScroller(tPtr);
3732 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3733 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3734 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3735 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3736 WMMapWidget(tPtr->hS);
3737 } else if (!shouldhave && tPtr->hS) {
3738 WMUnmapWidget(tPtr->hS);
3739 WMDestroyWidget(tPtr->hS);
3740 tPtr->hS = NULL;
3743 tPtr->hpos = 0;
3744 tPtr->prevHpos = 0;
3745 textDidResize(tPtr->view->delegate, tPtr->view);
3749 void
3750 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3752 if (!tPtr)
3753 return;
3755 if(shouldhave && !tPtr->ruler) {
3756 tPtr->ruler = WMCreateRuler(tPtr);
3757 (W_VIEW(tPtr->ruler))->attribs.cursor =
3758 tPtr->view->screen->defaultCursor;
3759 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3760 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3761 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3762 } else if(!shouldhave && tPtr->ruler) {
3763 WMShowTextRuler(tPtr, False);
3764 WMDestroyWidget(tPtr->ruler);
3765 tPtr->ruler = NULL;
3767 textDidResize(tPtr->view->delegate, tPtr->view);
3770 void
3771 WMShowTextRuler(WMText *tPtr, Bool show)
3773 if(!tPtr)
3774 return;
3775 if(!tPtr->ruler)
3776 return;
3778 if(tPtr->flags.monoFont)
3779 show = False;
3781 tPtr->flags.rulerShown = show;
3782 if(show) {
3783 WMMapWidget(tPtr->ruler);
3784 } else {
3785 WMUnmapWidget(tPtr->ruler);
3788 textDidResize(tPtr->view->delegate, tPtr->view);
3791 Bool
3792 WMGetTextRulerShown(WMText *tPtr)
3794 if(!tPtr)
3795 return 0;
3797 if(!tPtr->ruler)
3798 return 0;
3800 return tPtr->flags.rulerShown;
3804 void
3805 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3807 if (!tPtr)
3808 return;
3810 if (shouldhave && !tPtr->vS) {
3811 tPtr->vS = WMCreateScroller(tPtr);
3812 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3813 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3814 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3815 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3816 WMMapWidget(tPtr->vS);
3817 } else if (!shouldhave && tPtr->vS) {
3818 WMUnmapWidget(tPtr->vS);
3819 WMDestroyWidget(tPtr->vS);
3820 tPtr->vS = NULL;
3823 tPtr->vpos = 0;
3824 tPtr->prevVpos = 0;
3825 textDidResize(tPtr->view->delegate, tPtr->view);
3830 Bool
3831 WMScrollText(WMText *tPtr, int amount)
3833 Bool scroll=False;
3834 if (!tPtr)
3835 return False;
3836 if (amount == 0 || !tPtr->view->flags.realized)
3837 return False;
3839 if (amount < 0) {
3840 if (tPtr->vpos > 0) {
3841 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3842 else tPtr->vpos=0;
3843 scroll=True;
3845 } else {
3846 int limit = tPtr->docHeight - tPtr->visible.h;
3847 if (tPtr->vpos < limit) {
3848 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3849 else tPtr->vpos = limit;
3850 scroll = True;
3854 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3855 updateScrollers(tPtr);
3856 paintText(tPtr);
3858 tPtr->prevVpos = tPtr->vpos;
3859 return scroll;
3862 Bool
3863 WMPageText(WMText *tPtr, Bool direction)
3865 if (!tPtr) return False;
3866 if (!tPtr->view->flags.realized) return False;
3868 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3871 void
3872 WMSetTextEditable(WMText *tPtr, Bool editable)
3874 if (!tPtr)
3875 return;
3876 tPtr->flags.editable = editable;
3879 int
3880 WMGetTextEditable(WMText *tPtr)
3882 if (!tPtr)
3883 return 0;
3884 return tPtr->flags.editable;
3887 void
3888 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3890 if (!tPtr)
3891 return;
3892 tPtr->flags.indentNewLine = indent;
3895 void
3896 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3898 if (!tPtr)
3899 return;
3900 tPtr->flags.ignoreNewLine = ignore;
3903 Bool
3904 WMGetTextIgnoresNewline(WMText *tPtr)
3906 if (!tPtr)
3907 return True;
3908 return tPtr->flags.ignoreNewLine;
3911 void
3912 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3914 if (!tPtr)
3915 return;
3917 if (mono) {
3918 if(tPtr->flags.rulerShown)
3919 WMShowTextRuler(tPtr, False);
3920 if(tPtr->flags.alignment != WALeft)
3921 tPtr->flags.alignment = WALeft;
3924 tPtr->flags.monoFont = mono;
3925 textDidResize(tPtr->view->delegate, tPtr->view);
3928 Bool
3929 WMGetTextUsesMonoFont(WMText *tPtr)
3931 if (!tPtr)
3932 return True;
3933 return tPtr->flags.monoFont;
3937 void
3938 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3940 if (!tPtr)
3941 return;
3943 WMReleaseFont(tPtr->dFont);
3944 if (font)
3945 tPtr->dFont = WMRetainFont(font);
3946 else
3947 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3950 WMFont *
3951 WMGetTextDefaultFont(WMText *tPtr)
3953 if (!tPtr)
3954 return NULL;
3955 else
3956 return WMRetainFont(tPtr->dFont);
3959 void
3960 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3962 if (!tPtr)
3963 return;
3965 WMReleaseColor(tPtr->dColor);
3966 if (color)
3967 tPtr->dColor = WMRetainColor(color);
3968 else
3969 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3972 WMColor *
3973 WMGetTextDefaultColor(WMText *tPtr)
3975 if (!tPtr)
3976 return NULL;
3977 else
3978 return WMRetainColor(tPtr->dColor);
3981 void
3982 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3984 if (!tPtr)
3985 return;
3986 if(tPtr->flags.monoFont)
3987 tPtr->flags.alignment = WALeft;
3988 else
3989 tPtr->flags.alignment = alignment;
3990 WMThawText(tPtr);
3993 int
3994 WMGetTextInsertType(WMText *tPtr)
3996 if (!tPtr)
3997 return 0;
3998 return tPtr->flags.prepend;
4002 void
4003 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
4005 if (!tPtr || !color)
4006 return;
4008 setSelectionProperty(tPtr, NULL, color, -1);
4011 WMColor *
4012 WMGetTextSelectionColor(WMText *tPtr)
4014 TextBlock *tb;
4016 if (!tPtr)
4017 return NULL;
4019 tb = tPtr->currentTextBlock;
4021 if (!tb || !tPtr->flags.ownsSelection)
4022 return NULL;
4024 if(!tb->selected)
4025 return NULL;
4027 return tb->color;
4031 void
4032 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4034 if (!tPtr || !font)
4035 return;
4037 setSelectionProperty(tPtr, font, NULL, -1) ;
4040 WMFont *
4041 WMGetTextSelectionFont(WMText *tPtr)
4043 TextBlock *tb;
4045 if (!tPtr)
4046 return NULL;
4048 tb = tPtr->currentTextBlock;
4050 if (!tb || !tPtr->flags.ownsSelection)
4051 return NULL;
4053 if(!tb->selected)
4054 return NULL;
4056 if(tb->graphic) {
4057 tb = getFirstNonGraphicBlockFor(tb, 1);
4058 if(!tb)
4059 return NULL;
4061 return (tb->selected ? tb->d.font : NULL);
4065 void
4066 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4068 if (!tPtr || (underlined!=0 && underlined !=1))
4069 return;
4071 setSelectionProperty(tPtr, NULL, NULL, underlined);
4075 int
4076 WMGetTextSelectionUnderlined(WMText *tPtr)
4078 TextBlock *tb;
4080 if (!tPtr)
4081 return 0;
4083 tb = tPtr->currentTextBlock;
4085 if (!tb || !tPtr->flags.ownsSelection)
4086 return 0;
4088 if(!tb->selected)
4089 return 0;
4091 return tb->underlined;
4095 void
4096 WMFreezeText(WMText *tPtr)
4098 if (!tPtr)
4099 return;
4101 tPtr->flags.frozen = True;
4105 void
4106 WMThawText(WMText *tPtr)
4108 if (!tPtr)
4109 return;
4111 tPtr->flags.frozen = False;
4113 if(tPtr->flags.monoFont) {
4114 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4115 TextBlock *tb;
4117 /* make sure to unmap widgets no matter where they are */
4118 /* they'll be later remapped if needed by paintText */
4119 for(j=0; j<c; j++) {
4120 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4121 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4122 WMUnmapWidget(tb->d.widget);
4128 tPtr->flags.laidOut = False;
4129 layOutDocument(tPtr);
4130 updateScrollers(tPtr);
4131 paintText(tPtr);
4132 tPtr->flags.needsLayOut = False;
4136 /* find first occurence of a string */
4137 static char *
4138 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4139 Bool caseSensitive)
4141 char *ptr;
4143 if(!haystack || !needle || !end)
4144 return NULL;
4146 for (ptr = haystack; ptr < end; ptr++) {
4147 if(caseSensitive) {
4148 if (*ptr == *needle && !strncmp(ptr, needle, len))
4149 return ptr;
4151 } else {
4152 if (tolower(*ptr) == tolower(*needle) &&
4153 !strncasecmp(ptr, needle, len))
4154 return ptr;
4158 return NULL;
4161 /* find last occurence of a string */
4162 static char *
4163 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4164 Bool caseSensitive)
4166 char *ptr;
4168 if(!haystack || !needle || !end)
4169 return NULL;
4171 for (ptr = haystack-2; ptr > end; ptr--) {
4172 if(caseSensitive) {
4173 if (*ptr == *needle && !strncmp(ptr, needle, len))
4174 return ptr;
4175 } else {
4176 if (tolower(*ptr) == tolower(*needle) &&
4177 !strncasecmp(ptr, needle, len))
4178 return ptr;
4182 return NULL;
4186 Bool
4187 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4188 Bool caseSensitive)
4190 TextBlock *tb;
4191 char *mark=NULL;
4192 unsigned short pos;
4194 if (!tPtr || !needle)
4195 return False;
4197 #if 0
4198 if (! (tb = tPtr->currentTextBlock)) {
4199 if (! (tb = ( (direction > 0) ?
4200 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4201 return False;
4203 } else {
4204 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4205 tb = (direction>0) ? tb->next : tb->prior; */
4206 if(tb != tPtr->lastTextBlock)
4207 tb = tb->prior;
4209 #endif
4210 tb = tPtr->currentTextBlock;
4211 pos = tPtr->tpos;
4214 while(tb) {
4215 if (!tb->graphic) {
4217 if(direction > 0) {
4218 if(pos+1 < tb->used)
4219 pos++;
4221 if(tb->used - pos> 0 && pos > 0) {
4222 mark = mystrstr(&tb->text[pos], needle,
4223 strlen(needle), &tb->text[tb->used], caseSensitive);
4225 } else {
4226 tb = tb->next;
4227 pos = 0;
4228 continue;
4231 } else {
4232 if(pos-1 > 0)
4233 pos--;
4235 if(pos > 0) {
4236 mark = mystrrstr(&tb->text[pos], needle,
4237 strlen(needle), tb->text, caseSensitive);
4238 } else {
4239 tb = tb->prior;
4240 if(!tb)
4241 return False;
4242 pos = tb->used;
4243 continue;
4248 if(mark) {
4249 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4251 tPtr->tpos = (int)(mark - tb->text);
4252 tPtr->currentTextBlock = tb;
4253 updateCursorPosition(tPtr);
4254 tPtr->sel.y = tPtr->cursor.y+5;
4255 tPtr->sel.h = tPtr->cursor.h-10;
4256 tPtr->sel.x = tPtr->cursor.x +1;
4257 tPtr->sel.w = WMIN(WMWidthOfString(font,
4258 &tb->text[tPtr->tpos], strlen(needle)),
4259 tPtr->docWidth - tPtr->sel.x);
4260 tPtr->flags.ownsSelection = True;
4261 paintText(tPtr);
4263 return True;
4267 tb = (direction>0) ? tb->next : tb->prior;
4268 if(tb) {
4269 pos = (direction>0) ? 0 : tb->used;
4273 return False;
4277 Bool
4278 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4280 if (!tPtr)
4281 return False;
4283 if (!tPtr->flags.ownsSelection)
4284 return False;
4286 removeSelection(tPtr);
4288 if(replacement) {
4289 insertTextInteractively(tPtr, replacement, strlen(replacement));
4290 updateCursorPosition(tPtr);
4291 paintText(tPtr);
4294 return True;