- removed unnecessary include of WINGsP.h in some places
[wmaker-crm.git] / WINGs / wtext.c
blob6ed438026057210f2f63923a8f18556142111a0e
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 max_d = 2;
1477 line_height = WMAX(line_height, font->height + max_d);
1478 text = &(tb->text[items[i].begin]);
1479 len = items[i].end - items[i].begin;
1480 if (tPtr->flags.alignment != WALeft)
1481 lw += WMWidthOfString(font, text, len);
1485 if (tPtr->flags.alignment == WARight) {
1486 j = tPtr->visible.w - lw;
1487 } else if (tPtr->flags.alignment == WACenter) {
1488 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1491 for(i=0; i<nitems; i++) {
1492 tb = items[i].tb;
1494 if (tbsame == tb) { /* extend it, since it's on same line */
1495 tb->sections[tb->nsections-1].end = items[i].end;
1496 n = tb->nsections-1;
1497 } else {
1498 tb->sections = wrealloc(tb->sections,
1499 (++tb->nsections)*sizeof(Section));
1500 n = tb->nsections-1;
1501 tb->sections[n]._y = y + max_d;
1502 tb->sections[n].max_d = max_d;
1503 tb->sections[n].x = x+j;
1504 tb->sections[n].h = line_height;
1505 tb->sections[n].begin = items[i].begin;
1506 tb->sections[n].end = items[i].end;
1509 tb->sections[n].last = (i+1 == nitems);
1511 if (tb->graphic) {
1512 if (!tPtr->flags.monoFont) {
1513 if(tb->object) {
1514 WMWidget *wdt = tb->d.widget;
1515 tb->sections[n].y = max_d + y
1516 + line_height - WMWidgetHeight(wdt);
1517 tb->sections[n].w = WMWidgetWidth(wdt);
1518 } else {
1519 tb->sections[n].y = y + line_height
1520 + max_d - tb->d.pixmap->height;
1521 tb->sections[n].w = tb->d.pixmap->width;
1523 x += tb->sections[n].w;
1525 } else {
1526 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1527 len = items[i].end - items[i].begin;
1528 text = &(tb->text[items[i].begin]);
1530 tb->sections[n].y = y+line_height-font->y;
1531 tb->sections[n].w =
1532 WMWidthOfString(font,
1533 &(tb->text[tb->sections[n].begin]),
1534 tb->sections[n].end - tb->sections[n].begin);
1536 x += WMWidthOfString(font, text, len);
1539 tbsame = tb;
1542 return line_height;
1547 static void
1548 layOutDocument(Text *tPtr)
1550 TextBlock *tb;
1551 myLineItems *items = NULL;
1552 unsigned int itemsSize=0, nitems=0, begin, end;
1553 WMFont *font;
1554 unsigned int x, y=0, lw = 0, width=0, bmargin;
1555 char *start=NULL, *mark=NULL;
1557 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1558 return;
1560 assert(tPtr->visible.w > 20);
1562 tPtr->docWidth = tPtr->visible.w;
1563 x = tPtr->margins[tb->marginN].first;
1564 bmargin = tPtr->margins[tb->marginN].body;
1566 /* only partial layOut needed: re-Lay only affected textblocks */
1567 if (tPtr->flags.laidOut) {
1568 tb = tPtr->currentTextBlock;
1570 /* search backwards for textblocks on same line */
1571 while (tb->prior) {
1572 if (!tb->sections || tb->nsections<1) {
1573 tb = tPtr->firstTextBlock;
1574 tPtr->flags.laidOut = False;
1575 y = 0;
1576 goto _layOut;
1579 if(!tb->prior->sections || tb->prior->nsections<1) {
1580 tb = tPtr->firstTextBlock;
1581 tPtr->flags.laidOut = False;
1582 y = 0;
1583 goto _layOut;
1586 if (tb->sections[0]._y !=
1587 tb->prior->sections[tb->prior->nsections-1]._y) {
1588 break;
1590 tb = tb->prior;
1593 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1594 y = tb->prior->sections[tb->prior->nsections-1]._y +
1595 tb->prior->sections[tb->prior->nsections-1].h -
1596 tb->prior->sections[tb->prior->nsections-1].max_d;
1597 } else {
1598 y = 0;
1602 _layOut:
1603 while (tb) {
1605 if (tb->sections && tb->nsections>0) {
1606 wfree(tb->sections);
1607 tb->sections = NULL;
1608 tb->nsections = 0;
1611 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1612 TextBlock *next = tb->next;
1613 tPtr->currentTextBlock = tb;
1614 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1615 tb = next;
1616 tb->first = True;
1617 continue;
1620 if (tb->first && tb != tPtr->firstTextBlock) {
1621 y += layOutLine(tPtr, items, nitems, x, y);
1622 x = tPtr->margins[tb->marginN].first;
1623 bmargin = tPtr->margins[tb->marginN].body;
1624 nitems = 0;
1625 lw = 0;
1628 if (tb->graphic) {
1629 if (!tPtr->flags.monoFont) {
1630 if(tb->object)
1631 width = WMWidgetWidth(tb->d.widget);
1632 else
1633 width = tb->d.pixmap->width;
1635 if (width > tPtr->docWidth)
1636 tPtr->docWidth = width;
1638 lw += width;
1639 if (lw >= tPtr->visible.w - x ) {
1640 y += layOutLine(tPtr, items, nitems, x, y);
1641 nitems = 0;
1642 x = bmargin;
1643 lw = width;
1646 if(nitems + 1> itemsSize) {
1647 items = wrealloc(items,
1648 (++itemsSize)*sizeof(myLineItems));
1651 items[nitems].tb = tb;
1652 items[nitems].begin = 0;
1653 items[nitems].end = 0;
1654 nitems++;
1657 } else if ((start = tb->text)) {
1658 begin = end = 0;
1659 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1661 while (start) {
1662 mark = strchr(start, ' ');
1663 if (mark) {
1664 end += (int)(mark-start)+1;
1665 start = mark+1;
1666 } else {
1667 end += strlen(start);
1668 start = mark;
1671 if (end > tb->used)
1672 end = tb->used;
1674 if (end-begin > 0) {
1676 width = WMWidthOfString(font,
1677 &tb->text[begin], end-begin);
1679 /* if it won't fit, char wrap it */
1680 if (width >= tPtr->visible.w) {
1681 char *t = &tb->text[begin];
1682 int l=end-begin, i=0;
1683 do {
1684 width = WMWidthOfString(font, t, ++i);
1685 } while (width < tPtr->visible.w && i < l);
1686 if(i>2) i--;
1687 end = begin+i;
1688 start = &tb->text[end];
1691 lw += width;
1694 if (lw >= tPtr->visible.w - x) {
1695 y += layOutLine(tPtr, items, nitems, x, y);
1696 lw = width;
1697 x = bmargin;
1698 nitems = 0;
1701 if(nitems + 1 > itemsSize) {
1702 items = wrealloc(items,
1703 (++itemsSize)*sizeof(myLineItems));
1706 items[nitems].tb = tb;
1707 items[nitems].begin = begin;
1708 items[nitems].end = end;
1709 nitems++;
1711 begin = end;
1716 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1717 if(0&&tPtr->flags.laidOut
1718 && tb->next && tb->next->sections && tb->next->nsections>0
1719 && (tPtr->vpos + tPtr->visible.h
1720 < tb->next->sections[0]._y)) {
1721 if(tPtr->lastTextBlock->sections
1722 && tPtr->lastTextBlock->nsections > 0 ) {
1723 TextBlock *ltb = tPtr->lastTextBlock;
1724 int ly = ltb->sections[ltb->nsections-1]._y;
1725 int lh = ltb->sections[ltb->nsections-1].h;
1726 int ss, sd;
1728 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1729 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1731 y += layOutLine(tPtr, items, nitems, x, y);
1732 ss= ly+lh-y;
1733 sd = tPtr->docHeight-y;
1735 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1736 y += tb->next->sections[0]._y-y;
1737 nitems = 0;
1738 printf("nitems%d\n", nitems);
1739 if(ss-sd!=0)
1740 y = tPtr->docHeight+ss-sd;
1742 break;
1743 } else {
1744 tPtr->flags.laidOut = False;
1748 tb = tb->next;
1752 if (nitems > 0)
1753 y += layOutLine(tPtr, items, nitems, x, y);
1755 if (tPtr->docHeight != y+10) {
1756 tPtr->docHeight = y+10;
1757 updateScrollers(tPtr);
1760 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1761 XEvent event;
1763 tPtr->flags.horizOnDemand = True;
1764 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1765 event.type = Expose;
1766 handleEvents(&event, (void *)tPtr);
1768 } else if(tPtr->docWidth <= tPtr->visible.w
1769 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1770 tPtr->flags.horizOnDemand = False;
1771 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1774 tPtr->flags.laidOut = True;
1776 if(items && itemsSize > 0)
1777 wfree(items);
1782 static void
1783 textDidResize(W_ViewDelegate *self, WMView *view)
1785 Text *tPtr = (Text *)view->self;
1786 unsigned short w = tPtr->view->size.width;
1787 unsigned short h = tPtr->view->size.height;
1788 unsigned short rh = 0, vw = 0, rel;
1790 rel = (tPtr->flags.relief == WRFlat);
1792 if (tPtr->ruler && tPtr->flags.rulerShown) {
1793 WMMoveWidget(tPtr->ruler, 2, 2);
1794 WMResizeWidget(tPtr->ruler, w - 4, 40);
1795 rh = 40;
1798 if (tPtr->vS) {
1799 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1800 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1801 vw = 20;
1802 WMSetRulerOffset(tPtr->ruler,22);
1803 } else WMSetRulerOffset(tPtr->ruler, 2);
1805 if (tPtr->hS) {
1806 if (tPtr->vS) {
1807 WMMoveWidget(tPtr->hS, vw, h - 21);
1808 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1809 } else {
1810 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1811 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1815 tPtr->visible.x = (tPtr->vS)?24:4;
1816 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1817 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1818 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1819 tPtr->visible.h -= (tPtr->hS)?20:0;
1820 tPtr->margins[0].right = tPtr->visible.w;
1822 if (tPtr->view->flags.realized) {
1824 if (tPtr->db) {
1825 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1826 tPtr->db = (Pixmap) NULL;
1829 if (tPtr->visible.w < 40)
1830 tPtr->visible.w = 40;
1831 if (tPtr->visible.h < 20)
1832 tPtr->visible.h = 20;
1834 if(!tPtr->db) {
1835 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1836 tPtr->view->window, tPtr->visible.w,
1837 tPtr->visible.h, tPtr->view->screen->depth);
1841 WMThawText(tPtr);
1844 W_ViewDelegate _TextViewDelegate =
1846 NULL,
1847 NULL,
1848 textDidResize,
1849 NULL,
1852 #define TEXT_BUFFER_INCR 8
1853 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1855 static void
1856 clearText(Text *tPtr)
1858 tPtr->vpos = tPtr->hpos = 0;
1859 tPtr->docHeight = tPtr->docWidth = 0;
1860 tPtr->cursor.x = -23;
1862 if (!tPtr->firstTextBlock)
1863 return;
1865 while (tPtr->currentTextBlock)
1866 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1868 tPtr->firstTextBlock = NULL;
1869 tPtr->currentTextBlock = NULL;
1870 tPtr->lastTextBlock = NULL;
1871 WMEmptyArray(tPtr->gfxItems);
1874 /* possibly remove a single character from the currentTextBlock,
1875 or if there's a selection, remove it...
1876 note that Delete and Backspace are treated differently */
1877 static void
1878 deleteTextInteractively(Text *tPtr, KeySym ksym)
1880 TextBlock *tb;
1881 Bool back = (Bool) (ksym == XK_BackSpace);
1882 Bool done = 1, wasFirst = 0;
1884 if (!tPtr->flags.editable)
1885 return;
1887 if ( !(tb = tPtr->currentTextBlock) )
1888 return;
1890 if (tPtr->flags.ownsSelection) {
1891 if(removeSelection(tPtr))
1892 layOutDocument(tPtr);
1893 return;
1896 wasFirst = tb->first;
1897 if (back && tPtr->tpos < 1) {
1898 if (tb->prior) {
1899 if(tb->prior->blank) {
1900 tPtr->currentTextBlock = tb->prior;
1901 WMRemoveTextBlock(tPtr);
1902 tPtr->currentTextBlock = tb;
1903 tb->first = True;
1904 layOutDocument(tPtr);
1905 return;
1906 } else {
1907 if(tb->blank) {
1908 TextBlock *prior = tb->prior;
1909 tPtr->currentTextBlock = tb;
1910 WMRemoveTextBlock(tPtr);
1911 tb = prior;
1912 } else {
1913 tb = tb->prior;
1916 if(tb->graphic)
1917 tPtr->tpos = 1;
1918 else
1919 tPtr->tpos = tb->used;
1921 tPtr->currentTextBlock = tb;
1922 done = 1;
1923 if(wasFirst) {
1924 if(tb->next)
1925 tb->next->first = False;
1926 layOutDocument(tPtr);
1927 return;
1933 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1934 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1935 if (back)
1936 tPtr->tpos--;
1937 memmove(&(tb->text[tPtr->tpos]),
1938 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1939 tb->used--;
1940 done = 0;
1943 /* if there are no characters left to back over in the textblock,
1944 but it still has characters to the right of the cursor: */
1945 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1946 || tb->graphic) {
1948 /* no more chars, and it's marked as blank? */
1949 if(tb->blank) {
1950 TextBlock *sibling = (back? tb->prior : tb->next);
1952 if(tb->used == 0 || tb->graphic)
1953 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1955 if (sibling) {
1956 tPtr->currentTextBlock = sibling;
1957 if(tb->graphic)
1958 tPtr->tpos = (back? 1 : 0);
1959 else
1960 tPtr->tpos = (back? sibling->used : 0);
1962 /* no more chars, so mark it as blank */
1963 } else if(tb->used == 0) {
1964 tb->blank = 1;
1965 } else if(tb->graphic) {
1966 Bool hasNext = (Bool)(tb->next);
1968 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1969 if(hasNext) {
1970 tPtr->tpos = 0;
1971 } else if(tPtr->currentTextBlock) {
1972 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1973 1 : tPtr->currentTextBlock->used);
1975 } else printf("DEBUG: unaccounted for... catch this!\n");
1978 layOutDocument(tPtr);
1982 static void
1983 insertTextInteractively(Text *tPtr, char *text, int len)
1985 TextBlock *tb;
1986 char *newline = NULL;
1988 if (!tPtr->flags.editable) {
1989 return;
1992 if (len < 1 || !text)
1993 return;
1996 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1997 return;
2000 if (tPtr->flags.ownsSelection)
2001 removeSelection(tPtr);
2004 if (tPtr->flags.ignoreNewLine) {
2005 int i;
2006 for(i=0; i<len; i++) {
2007 if (text[i] == '\n')
2008 text[i] = ' ';
2012 tb = tPtr->currentTextBlock;
2013 if (!tb || tb->graphic) {
2014 tPtr->tpos = 0;
2015 WMAppendTextStream(tPtr, text);
2016 layOutDocument(tPtr);
2017 return;
2020 if ((newline = strchr(text, '\n'))) {
2021 int nlen = (int)(newline-text);
2022 int s = tb->used - tPtr->tpos;
2024 if (!tb->blank && nlen>0) {
2025 char *save;
2027 if (s > 0) {
2028 save = wmalloc(s);
2029 memcpy(save, &tb->text[tPtr->tpos], s);
2030 tb->used -= (tb->used - tPtr->tpos);
2032 insertTextInteractively(tPtr, text, nlen);
2033 newline++;
2034 WMAppendTextStream(tPtr, newline);
2035 if (s>0) {
2036 insertTextInteractively(tPtr, save, s);
2037 wfree(save);
2039 } else {
2040 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2041 && !tb->graphic && tb->text) {
2043 unsigned short savePos = tPtr->tpos;
2044 void *ntb = WMCreateTextBlockWithText(
2045 tPtr, &tb->text[tPtr->tpos],
2046 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2048 if(tb->sections[0].end == tPtr->tpos)
2049 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2050 NULL, tb->d.font, tb->color, True, 0));
2052 tb->used = savePos;
2053 WMAppendTextBlock(tPtr, ntb);
2054 tPtr->tpos = 0;
2056 } else if (tPtr->tpos == tb->used) {
2057 if(tPtr->flags.indentNewLine) {
2058 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2059 " ", tb->d.font, tb->color, True, 4));
2060 tPtr->tpos = 4;
2061 } else {
2062 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2063 NULL, tb->d.font, tb->color, True, 0));
2064 tPtr->tpos = 0;
2066 } else if (tPtr->tpos == 0) {
2067 if(tPtr->flags.indentNewLine) {
2068 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2069 " ", tb->d.font, tb->color, True, 4));
2070 } else {
2071 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2072 NULL, tb->d.font, tb->color, True, 0));
2074 tPtr->tpos = 0;
2075 if(tPtr->currentTextBlock->next)
2076 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2079 } else {
2080 if (tb->used + len >= tb->allocated) {
2081 tb->allocated = reqBlockSize(tb->used+len);
2082 tb->text = wrealloc(tb->text, tb->allocated);
2085 if (tb->blank) {
2086 memcpy(tb->text, text, len);
2087 tb->used = len;
2088 tPtr->tpos = len;
2089 tb->text[tb->used] = 0;
2090 tb->blank = False;
2092 } else {
2093 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2094 tb->used-tPtr->tpos+1);
2095 memmove(&tb->text[tPtr->tpos], text, len);
2096 tb->used += len;
2097 tPtr->tpos += len;
2098 tb->text[tb->used] = 0;
2103 layOutDocument(tPtr);
2107 static void
2108 selectRegion(Text *tPtr, int x, int y)
2111 if (x < 0 || y < 0)
2112 return;
2114 y += (tPtr->flags.rulerShown? 40: 0);
2115 y += tPtr->vpos;
2116 if (y>10)
2117 y -= 10; /* the original offset */
2119 x -= tPtr->visible.x-2;
2120 if (x<0)
2121 x=0;
2123 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2124 tPtr->sel.w = abs(tPtr->clicked.x - x);
2125 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2126 tPtr->sel.h = abs(tPtr->clicked.y - y);
2128 tPtr->flags.ownsSelection = True;
2129 paintText(tPtr);
2133 static void
2134 releaseSelection(Text *tPtr)
2136 TextBlock *tb = tPtr->firstTextBlock;
2138 while(tb) {
2139 tb->selected = False;
2140 tb = tb->next;
2142 tPtr->flags.ownsSelection = False;
2143 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2144 CurrentTime);
2146 paintText(tPtr);
2150 WMData*
2151 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2152 Atom *type)
2154 Text *tPtr = view->self;
2155 Display *dpy = tPtr->view->screen->display;
2156 Atom _TARGETS;
2157 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2158 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2159 WMData *data = NULL;
2162 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2163 char *text = WMGetTextSelectedStream(tPtr);
2165 if (text) {
2166 data = WMCreateDataWithBytes(text, strlen(text));
2167 WMSetDataFormat(data, TYPETEXT);
2169 *type = target;
2170 return data;
2171 } else printf("didn't get it\n");
2173 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2174 if (target == _TARGETS) {
2175 Atom *ptr;
2177 ptr = wmalloc(4 * sizeof(Atom));
2178 ptr[0] = _TARGETS;
2179 ptr[1] = XA_STRING;
2180 ptr[2] = TEXT;
2181 ptr[3] = COMPOUND_TEXT;
2183 data = WMCreateDataWithBytes(ptr, 4*4);
2184 WMSetDataFormat(data, 32);
2186 *type = target;
2187 return data;
2190 return NULL;
2194 static void
2195 lostHandler(WMView *view, Atom selection, void *cdata)
2197 releaseSelection((WMText *)view->self);
2201 static WMSelectionProcs selectionHandler = {
2202 requestHandler, lostHandler, NULL
2206 static void
2207 ownershipObserver(void *observerData, WMNotification *notification)
2209 if (observerData != WMGetNotificationClientData(notification))
2210 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2214 static void
2215 autoSelectText(Text *tPtr, int clicks)
2217 int x, start;
2218 TextBlock *tb;
2219 char *mark = NULL, behind, ahead;
2221 if(!(tb = tPtr->currentTextBlock))
2222 return;
2224 if(clicks == 2) {
2227 switch(tb->text[tPtr->tpos]) {
2228 case ' ': return;
2230 case '<': case '>': behind = '<'; ahead = '>'; break;
2231 case '{': case '}': behind = '{'; ahead = '}'; break;
2232 case '[': case ']': behind = '['; ahead = ']'; break;
2234 default: behind = ahead = ' ';
2237 tPtr->sel.y = tPtr->cursor.y+5;
2238 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2240 if(tb->graphic) {
2241 tPtr->sel.x = tb->sections[0].x;
2242 tPtr->sel.w = tb->sections[0].w;
2243 } else {
2244 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2246 start = tPtr->tpos;
2247 while(start > 0 && tb->text[start-1] != behind)
2248 start--;
2250 x = tPtr->cursor.x;
2251 if(tPtr->tpos > start){
2252 x -= WMWidthOfString(font, &tb->text[start],
2253 tPtr->tpos - start);
2255 tPtr->sel.x = (x<0?0:x)+1;
2257 if((mark = strchr(&tb->text[start], ahead))) {
2258 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2259 (int)(mark - &tb->text[start]));
2260 } else if(tb->used > start) {
2261 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2262 tb->used - start);
2266 } else if(clicks == 3) {
2267 TextBlock *cur = tb;
2269 while(tb && !tb->first) {
2270 tb = tb->prior;
2272 tPtr->sel.y = tb->sections[0]._y;
2274 tb = cur;
2275 while(tb->next && !tb->next->first) {
2276 tb = tb->next;
2278 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2279 + 5 - tPtr->sel.y;
2281 tPtr->sel.x = 0;
2282 tPtr->sel.w = tPtr->docWidth;
2283 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2286 if (!tPtr->flags.ownsSelection) {
2287 WMCreateSelectionHandler(tPtr->view,
2288 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2289 tPtr->flags.ownsSelection = True;
2291 paintText(tPtr);
2296 static void
2297 fontChanged(void *observerData, WMNotification *notification)
2299 WMText *tPtr = (WMText *) observerData;
2300 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2301 printf("fontChanged\n");
2303 if(!tPtr || !font)
2304 return;
2306 if (tPtr->flags.ownsSelection)
2307 WMSetTextSelectionFont(tPtr, font);
2311 static void
2312 handleTextKeyPress(Text *tPtr, XEvent *event)
2314 char buffer[64];
2315 KeySym ksym;
2316 int control_pressed = False;
2317 TextBlock *tb = NULL;
2319 if (((XKeyEvent *) event)->state & ControlMask)
2320 control_pressed = True;
2321 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2323 switch(ksym) {
2325 case XK_Home:
2326 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2327 tPtr->tpos = 0;
2328 updateCursorPosition(tPtr);
2329 paintText(tPtr);
2330 break;
2332 case XK_End:
2333 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2334 if(tPtr->currentTextBlock->graphic)
2335 tPtr->tpos = 1;
2336 else
2337 tPtr->tpos = tPtr->currentTextBlock->used;
2339 updateCursorPosition(tPtr);
2340 paintText(tPtr);
2341 break;
2343 case XK_Left:
2344 if(!(tb = tPtr->currentTextBlock))
2345 break;
2346 if(tb->graphic)
2347 goto L_imaGFX;
2349 if(tPtr->tpos==0) {
2350 L_imaGFX:
2351 if(tb->prior) {
2352 tPtr->currentTextBlock = tb->prior;
2353 if(tPtr->currentTextBlock->graphic)
2354 tPtr->tpos = 1;
2355 else
2356 tPtr->tpos = tPtr->currentTextBlock->used;
2358 if(!tb->first && tPtr->tpos > 0)
2359 tPtr->tpos--;
2360 } else tPtr->tpos = 0;
2361 } else tPtr->tpos--;
2362 updateCursorPosition(tPtr);
2363 paintText(tPtr);
2364 break;
2366 case XK_Right:
2367 if(!(tb = tPtr->currentTextBlock))
2368 break;
2369 if(tb->graphic)
2370 goto R_imaGFX;
2371 if(tPtr->tpos == tb->used) {
2372 R_imaGFX:
2373 if(tb->next) {
2374 tPtr->currentTextBlock = tb->next;
2375 tPtr->tpos = 0;
2376 if(!tb->next->first && tb->next->used>0)
2377 tPtr->tpos++;
2378 } else {
2379 if(tb->graphic)
2380 tPtr->tpos = 1;
2381 else
2382 tPtr->tpos = tb->used;
2384 } else tPtr->tpos++;
2385 updateCursorPosition(tPtr);
2386 paintText(tPtr);
2387 break;
2389 case XK_Down:
2390 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2391 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2392 paintText(tPtr);
2393 break;
2395 case XK_Up:
2396 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2397 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2398 paintText(tPtr);
2399 break;
2401 case XK_BackSpace:
2402 case XK_Delete:
2403 #ifdef XK_KP_Delete
2404 case XK_KP_Delete:
2405 #endif
2406 deleteTextInteractively(tPtr, ksym);
2407 updateCursorPosition(tPtr);
2408 paintText(tPtr);
2409 break;
2411 case XK_Control_R :
2412 case XK_Control_L :
2413 control_pressed = True;
2414 break;
2416 case XK_Tab:
2417 insertTextInteractively(tPtr, " ", 4);
2418 updateCursorPosition(tPtr);
2419 paintText(tPtr);
2420 break;
2422 case XK_Return:
2423 *buffer = '\n';
2424 default:
2425 if (*buffer != 0 && !control_pressed) {
2426 insertTextInteractively(tPtr, buffer, strlen(buffer));
2427 updateCursorPosition(tPtr);
2428 paintText(tPtr);
2430 } else if (control_pressed && ksym==XK_r) {
2431 Bool i = !tPtr->flags.rulerShown;
2432 WMShowTextRuler(tPtr, i);
2433 tPtr->flags.rulerShown = i;
2435 else if (control_pressed && *buffer == '\a')
2436 XBell(tPtr->view->screen->display, 0);
2437 else
2438 WMRelayToNextResponder(tPtr->view, event);
2441 if (!control_pressed && tPtr->flags.ownsSelection)
2442 releaseSelection(tPtr);
2446 static void
2447 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2448 void *cdata, WMData *data)
2450 Text *tPtr = (Text *)view->self;
2451 char *text;
2453 tPtr->flags.waitingForSelection = 0;
2455 if (data) {
2456 text = (char*)WMDataBytes(data);
2458 if (tPtr->parser) {
2459 (tPtr->parser) (tPtr, (void *) text);
2460 layOutDocument(tPtr);
2461 } else insertTextInteractively(tPtr, text, strlen(text));
2462 updateCursorPosition(tPtr);
2463 paintText(tPtr);
2465 } else {
2466 int n;
2468 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2470 if (text) {
2471 text[n] = 0;
2472 if (tPtr->parser) {
2473 (tPtr->parser) (tPtr, (void *) text);
2474 layOutDocument(tPtr);
2475 } else insertTextInteractively(tPtr, text, n);
2476 updateCursorPosition(tPtr);
2477 paintText(tPtr);
2479 XFree(text);
2487 static void
2488 handleActionEvents(XEvent *event, void *data)
2490 Text *tPtr = (Text *)data;
2491 Display *dpy = event->xany.display;
2492 KeySym ksym;
2495 switch (event->type) {
2496 case KeyPress:
2497 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2498 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2499 tPtr->flags.extendSelection = True;
2500 return;
2503 if (tPtr->flags.focused) {
2504 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2505 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2506 GrabModeAsync, GrabModeAsync, None,
2507 tPtr->view->screen->invisibleCursor, CurrentTime);
2508 tPtr->flags.pointerGrabbed = True;
2509 handleTextKeyPress(tPtr, event);
2511 } break;
2513 case KeyRelease:
2514 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2515 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2516 tPtr->flags.extendSelection = False;
2517 return;
2518 /* end modify flag so selection can be extended */
2520 break;
2523 case MotionNotify:
2525 if (tPtr->flags.pointerGrabbed) {
2526 tPtr->flags.pointerGrabbed = False;
2527 XUngrabPointer(dpy, CurrentTime);
2530 if(tPtr->flags.waitingForSelection)
2531 break;
2533 if ((event->xmotion.state & Button1Mask)) {
2534 TextBlock *tb = tPtr->currentTextBlock;
2536 if(tb && tPtr->flags.isOverGraphic &&
2537 tb->graphic && !tb->object) {
2538 WMSize offs;
2539 WMPixmap *pixmap = tb->d.pixmap;
2540 char *types[2] = {"application/X-image", NULL};
2542 offs.width = 2;
2543 offs.height = 2;
2545 WMDragImageFromView(tPtr->view, pixmap, types,
2546 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2547 offs, event, True);
2550 } else {
2551 if (!tPtr->flags.ownsSelection) {
2552 WMCreateSelectionHandler(tPtr->view,
2553 XA_PRIMARY, event->xbutton.time,
2554 &selectionHandler, NULL);
2555 tPtr->flags.ownsSelection = True;
2558 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2559 break;
2562 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2563 break;
2566 case ButtonPress:
2568 if (tPtr->flags.pointerGrabbed) {
2569 tPtr->flags.pointerGrabbed = False;
2570 XUngrabPointer(dpy, CurrentTime);
2571 break;
2574 if (tPtr->flags.waitingForSelection)
2575 break;
2577 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2578 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2579 return;
2582 if (tPtr->flags.ownsSelection)
2583 releaseSelection(tPtr);
2586 if (event->xbutton.button == Button1) {
2588 if(WMIsDoubleClick(event)) {
2589 TextBlock *tb = tPtr->currentTextBlock;
2591 tPtr->lastClickTime = event->xbutton.time;
2592 if(tb && tb->graphic && !tb->object) {
2593 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2594 char *desc;
2596 desc = wmalloc(tb->used+1);
2597 memcpy(desc, tb->text, tb->used);
2598 desc[tb->used] = 0;
2599 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2600 wfree(desc);
2602 } else {
2603 autoSelectText(tPtr, 2);
2605 break;
2606 } else if(event->xbutton.time - tPtr->lastClickTime
2607 < WINGsConfiguration.doubleClickDelay) {
2608 tPtr->lastClickTime = event->xbutton.time;
2609 autoSelectText(tPtr, 3);
2610 break;
2613 if (!tPtr->flags.focused) {
2614 WMSetFocusToWidget(tPtr);
2615 tPtr->flags.focused = True;
2618 tPtr->lastClickTime = event->xbutton.time;
2619 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2620 paintText(tPtr);
2623 if (event->xbutton.button
2624 == WINGsConfiguration.mouseWheelDown) {
2625 WMScrollText(tPtr, 16);
2626 break;
2629 if (event->xbutton.button
2630 == WINGsConfiguration.mouseWheelUp) {
2631 WMScrollText(tPtr, -16);
2632 break;
2635 if (event->xbutton.button == Button2) {
2636 char *text = NULL;
2637 int n;
2639 if (!tPtr->flags.editable) {
2640 XBell(dpy, 0);
2641 break;
2644 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2645 event->xbutton.time, pasteText, NULL)) {
2647 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2648 tPtr->flags.waitingForSelection = 0;
2650 if (text) {
2651 text[n] = 0;
2653 if (tPtr->parser) {
2654 (tPtr->parser) (tPtr, (void *) text);
2655 layOutDocument(tPtr);
2657 else
2658 insertTextInteractively(tPtr, text, n);
2660 XFree(text);
2661 #if 0
2662 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2663 (void*)WMInsertTextEvent);
2664 #endif
2665 updateCursorPosition(tPtr);
2666 paintText(tPtr);
2668 } else {
2669 tPtr->flags.waitingForSelection = True;
2672 break;
2676 case ButtonRelease:
2677 if (tPtr->flags.pointerGrabbed) {
2678 tPtr->flags.pointerGrabbed = False;
2679 XUngrabPointer(dpy, CurrentTime);
2680 break;
2683 if (tPtr->flags.waitingForSelection)
2684 break;
2690 static void
2691 handleEvents(XEvent *event, void *data)
2693 Text *tPtr = (Text *)data;
2695 switch(event->type) {
2696 case Expose:
2698 if (event->xexpose.count!=0)
2699 break;
2701 if(tPtr->hS) {
2702 if (!(W_VIEW(tPtr->hS))->flags.realized)
2703 WMRealizeWidget(tPtr->hS);
2706 if(tPtr->vS) {
2707 if (!(W_VIEW(tPtr->vS))->flags.realized)
2708 WMRealizeWidget(tPtr->vS);
2711 if(tPtr->ruler) {
2712 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2713 WMRealizeWidget(tPtr->ruler);
2717 if(!tPtr->db)
2718 textDidResize(tPtr->view->delegate, tPtr->view);
2720 paintText(tPtr);
2721 break;
2723 case FocusIn:
2724 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2725 != tPtr->view)
2726 return;
2727 tPtr->flags.focused = True;
2728 #if DO_BLINK
2729 if (tPtr->flags.editable && !tPtr->timerID) {
2730 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2731 blinkCursor, tPtr);
2733 #endif
2735 break;
2737 case FocusOut:
2738 tPtr->flags.focused = False;
2739 paintText(tPtr);
2740 #if DO_BLINK
2741 if (tPtr->timerID) {
2742 WMDeleteTimerHandler(tPtr->timerID);
2743 tPtr->timerID = NULL;
2745 #endif
2746 break;
2749 case DestroyNotify:
2750 clearText(tPtr);
2751 if(tPtr->db)
2752 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2753 if(tPtr->gfxItems)
2754 WMEmptyArray(tPtr->gfxItems);
2755 #if DO_BLINK
2756 if (tPtr->timerID)
2757 WMDeleteTimerHandler(tPtr->timerID);
2758 #endif
2759 WMReleaseFont(tPtr->dFont);
2760 WMReleaseColor(tPtr->dColor);
2761 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2762 WMRemoveNotificationObserver(tPtr);
2764 wfree(tPtr);
2766 break;
2772 static void
2773 insertPlainText(Text *tPtr, char *text)
2775 char *start, *mark;
2776 void *tb = NULL;
2778 start = text;
2779 while (start) {
2780 mark = strchr(start, '\n');
2781 if (mark) {
2782 tb = WMCreateTextBlockWithText(tPtr,
2783 start, tPtr->dFont,
2784 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2785 start = mark+1;
2786 tPtr->flags.first = True;
2787 } else {
2788 if (start && strlen(start)) {
2789 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2790 tPtr->dColor, tPtr->flags.first, strlen(start));
2791 } else tb = NULL;
2792 tPtr->flags.first = False;
2793 start = mark;
2796 if (tPtr->flags.prepend)
2797 WMPrependTextBlock(tPtr, tb);
2798 else
2799 WMAppendTextBlock(tPtr, tb);
2804 static void
2805 rulerMoveCallBack(WMWidget *w, void *self)
2807 Text *tPtr = (Text *)self;
2809 if (!tPtr)
2810 return;
2811 if (W_CLASS(tPtr) != WC_Text)
2812 return;
2814 paintText(tPtr);
2818 static void
2819 rulerReleaseCallBack(WMWidget *w, void *self)
2821 Text *tPtr = (Text *)self;
2823 if (!tPtr)
2824 return;
2825 if (W_CLASS(tPtr) != WC_Text)
2826 return;
2828 WMThawText(tPtr);
2829 return;
2832 static unsigned
2833 draggingSourceOperation(WMView *self, Bool local)
2835 return WDOperationCopy;
2838 static WMData*
2839 fetchDragData(WMView *self, char *type)
2841 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2842 char *desc;
2843 WMData *data;
2845 if (!tb)
2846 return NULL;
2848 printf("type is [%s]\n", type);
2849 desc = wmalloc(tb->used+1);
2850 memcpy(desc, tb->text, tb->used);
2851 desc[tb->used] = 0;
2852 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2854 wfree(desc);
2856 return data;
2860 static WMDragSourceProcs _DragSourceProcs = {
2861 draggingSourceOperation,
2862 NULL,
2863 NULL,
2864 fetchDragData
2868 static unsigned
2869 draggingEntered(WMView *self, WMDraggingInfo *info)
2871 printf("draggingEntered\n");
2872 return WDOperationCopy;
2876 static unsigned
2877 draggingUpdated(WMView *self, WMDraggingInfo *info)
2879 return WDOperationCopy;
2883 static void
2884 draggingExited(WMView *self, WMDraggingInfo *info)
2886 printf("draggingExited\n");
2889 static Bool
2890 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2892 printf("prepareForDragOperation\n");
2893 return True;
2897 char *badbadbad;
2899 static void
2900 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2901 void *cdata, WMData *data)
2903 badbadbad = wstrdup((char *)WMDataBytes(data));
2907 /* when it's done in WINGs, remove this */
2909 Bool
2910 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2912 WMScreen *scr = W_VIEW_SCREEN(view);
2914 if (!WMRequestSelection(scr->dragInfo.destView,
2915 scr->xdndSelectionAtom,
2916 XInternAtom(scr->display, type, False),
2917 scr->dragInfo.timestamp,
2918 receivedData, &scr->dragInfo)) {
2919 wwarning("could not request data for dropped data");
2923 XEvent ev;
2925 ev.type = ClientMessage;
2926 ev.xclient.message_type = scr->xdndFinishedAtom;
2927 ev.xclient.format = 32;
2928 ev.xclient.window = info->destinationWindow;
2929 ev.xclient.data.l[0] = 0;
2930 ev.xclient.data.l[1] = 0;
2931 ev.xclient.data.l[2] = 0;
2932 ev.xclient.data.l[3] = 0;
2933 ev.xclient.data.l[4] = 0;
2935 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2936 XFlush(scr->display);
2938 return True;
2941 static Bool
2942 performDragOperation(WMView *self, WMDraggingInfo *info)
2944 WMColor *color;
2945 WMText *tPtr = (WMText *)self->self;
2947 if (!tPtr)
2948 return True;
2950 requestDroppedData(tPtr->view, info, "application/X-color");
2951 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2952 if(color) {
2953 WMSetTextSelectionColor(tPtr, color);
2954 WMReleaseColor(color);
2959 return True;
2962 static void
2963 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2965 printf("concludeDragOperation\n");
2969 static WMDragDestinationProcs _DragDestinationProcs = {
2970 draggingEntered,
2971 draggingUpdated,
2972 draggingExited,
2973 prepareForDragOperation,
2974 performDragOperation,
2975 concludeDragOperation
2979 char *
2980 getStream(WMText *tPtr, int sel, int array)
2982 TextBlock *tb = NULL;
2983 char *text = NULL;
2984 unsigned long where = 0;
2986 if (!tPtr)
2987 return NULL;
2989 if (!(tb = tPtr->firstTextBlock))
2990 return NULL;
2992 if (tPtr->writer) {
2993 (tPtr->writer) (tPtr, (void *) text);
2994 return text;
2997 tb = tPtr->firstTextBlock;
2998 while (tb) {
3000 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
3002 if (!sel || (tb->graphic && tb->selected)) {
3004 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3005 && tb != tPtr->firstTextBlock) {
3006 text = wrealloc(text, where+1);
3007 text[where++] = '\n';
3010 if(tb->blank)
3011 goto _gSnext;
3013 if(tb->graphic && array) {
3014 text = wrealloc(text, where+4);
3015 text[where++] = 0xFA;
3016 text[where++] = (tb->used>>8)&0x0ff;
3017 text[where++] = tb->used&0x0ff;
3018 text[where++] = tb->allocated; /* extra info */
3020 text = wrealloc(text, where+tb->used);
3021 memcpy(&text[where], tb->text, tb->used);
3022 where += tb->used;
3025 } else if (sel && tb->selected) {
3027 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3028 text = wrealloc(text, where+1);
3029 text[where++] = '\n';
3032 if(tb->blank)
3033 goto _gSnext;
3035 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3036 memcpy(&text[where], &tb->text[tb->s_begin],
3037 tb->s_end - tb->s_begin);
3038 where += tb->s_end - tb->s_begin;
3043 _gSnext:tb = tb->next;
3046 /* +1 for the end of string, let's be nice */
3047 text = wrealloc(text, where+1);
3048 text[where] = 0;
3049 return text;
3053 static void
3054 releaseStreamObjects(void *data)
3056 if(data)
3057 wfree(data);
3060 WMArray *
3061 getStreamObjects(WMText *tPtr, int sel)
3063 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3064 WMData *data;
3065 char *stream;
3066 unsigned short len;
3067 char *start, *fa, *desc;
3069 stream = getStream(tPtr, sel, 1);
3070 if(!stream)
3071 return NULL;
3073 start = stream;
3074 while (start) {
3076 fa = strchr(start, 0xFA);
3077 if (fa) {
3078 if((int)(fa - start)>0) {
3079 desc = start;
3080 desc[(int)(fa - start)] = 0;
3081 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3082 WMSetDataFormat(data, TYPETEXT);
3083 WMAddToArray(array, (void *) data);
3086 len = *(fa+1)*0xff + *(fa+2);
3087 data = WMCreateDataWithBytes((void *)(fa+4), len);
3088 WMSetDataFormat(data, *(fa+3));
3089 WMAddToArray(array, (void *) data);
3090 start = fa + len + 4;
3092 } else {
3093 if (start && strlen(start)) {
3094 data = WMCreateDataWithBytes((void *)start, strlen(start));
3095 WMSetDataFormat(data, TYPETEXT);
3096 WMAddToArray(array, (void *) data);
3098 start = fa;
3102 wfree(stream);
3103 return array;
3107 WMText *
3108 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3110 Text *tPtr;
3111 Display *dpy;
3112 WMScreen *scr;
3113 XGCValues gcv;
3115 tPtr = wmalloc(sizeof(Text));
3116 memset(tPtr, 0, sizeof(Text));
3117 tPtr->widgetClass = WC_Text;
3118 tPtr->view = W_CreateView(W_VIEW(parent));
3119 if (!tPtr->view) {
3120 perror("could not create text's view\n");
3121 wfree(tPtr);
3122 return NULL;
3125 dpy = tPtr->view->screen->display;
3126 scr = tPtr->view->screen;
3128 tPtr->view->self = tPtr;
3129 tPtr->view->attribs.cursor = scr->textCursor;
3130 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3131 W_ResizeView(tPtr->view, 250, 200);
3133 tPtr->dColor = WMBlackColor(scr);
3134 tPtr->fgColor = WMBlackColor(scr);
3135 tPtr->bgColor = WMWhiteColor(scr);
3136 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3138 gcv.graphics_exposures = False;
3139 gcv.foreground = W_PIXEL(scr->gray);
3140 gcv.background = W_PIXEL(scr->darkGray);
3141 gcv.fill_style = FillStippled;
3142 /* why not use scr->stipple here? */
3143 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS,
3144 STIPPLE_WIDTH, STIPPLE_HEIGHT);
3145 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3146 GCForeground|GCBackground|GCStipple
3147 |GCFillStyle|GCGraphicsExposures, &gcv);
3149 tPtr->ruler = NULL;
3150 tPtr->vS = NULL;
3151 tPtr->hS = NULL;
3153 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3155 tPtr->view->delegate = &_TextViewDelegate;
3157 tPtr->delegate = NULL;
3159 #if DO_BLINK
3160 tPtr->timerID = NULL;
3161 #endif
3163 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3164 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3165 handleEvents, tPtr);
3167 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3168 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3169 handleActionEvents, tPtr);
3171 WMAddNotificationObserver(ownershipObserver, tPtr,
3172 WMSelectionOwnerDidChangeNotification,
3173 tPtr);
3175 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3176 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3180 char *types[3] = {"application/X-color", "application/X-image", NULL};
3181 WMRegisterViewForDraggedTypes(tPtr->view, types);
3184 /*WMAddNotificationObserver(fontChanged, tPtr,
3185 WMFontPanelDidChangeNotification, tPtr);*/
3187 tPtr->firstTextBlock = NULL;
3188 tPtr->lastTextBlock = NULL;
3189 tPtr->currentTextBlock = NULL;
3190 tPtr->tpos = 0;
3192 tPtr->gfxItems = WMCreateArray(4);
3194 tPtr->parser = parser;
3195 tPtr->writer = writer;
3197 tPtr->sel.x = tPtr->sel.y = 2;
3198 tPtr->sel.w = tPtr->sel.h = 0;
3200 tPtr->clicked.x = tPtr->clicked.y = 2;
3202 tPtr->visible.x = tPtr->visible.y = 2;
3203 tPtr->visible.h = tPtr->view->size.height;
3204 tPtr->visible.w = tPtr->view->size.width - 4;
3206 tPtr->cursor.x = -23;
3208 tPtr->docWidth = 0;
3209 tPtr->docHeight = 0;
3210 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3211 default_bullet);
3212 tPtr->db = (Pixmap) NULL;
3213 tPtr->bgPixmap = NULL;
3215 tPtr->margins = WMGetRulerMargins(NULL);
3216 tPtr->margins->right = tPtr->visible.w;
3217 tPtr->nMargins = 1;
3219 tPtr->flags.rulerShown = False;
3220 tPtr->flags.monoFont = False;
3221 tPtr->flags.focused = False;
3222 tPtr->flags.editable = True;
3223 tPtr->flags.ownsSelection = False;
3224 tPtr->flags.pointerGrabbed = False;
3225 tPtr->flags.extendSelection = False;
3226 tPtr->flags.frozen = False;
3227 tPtr->flags.cursorShown = True;
3228 tPtr->flags.acceptsGraphic = False;
3229 tPtr->flags.horizOnDemand = False;
3230 tPtr->flags.needsLayOut = False;
3231 tPtr->flags.ignoreNewLine = False;
3232 tPtr->flags.indentNewLine = False;
3233 tPtr->flags.laidOut = False;
3234 tPtr->flags.ownsSelection = False;
3235 tPtr->flags.waitingForSelection = False;
3236 tPtr->flags.prepend = False;
3237 tPtr->flags.isOverGraphic = False;
3238 tPtr->flags.relief = WRSunken;
3239 tPtr->flags.isOverGraphic = 0;
3240 tPtr->flags.alignment = WALeft;
3241 tPtr->flags.first = True;
3243 return tPtr;
3246 void
3247 WMPrependTextStream(WMText *tPtr, char *text)
3249 CHECK_CLASS(tPtr, WC_Text);
3251 if(!text) {
3252 if (tPtr->flags.ownsSelection)
3253 releaseSelection(tPtr);
3254 clearText(tPtr);
3255 updateScrollers(tPtr);
3256 return;
3259 tPtr->flags.prepend = True;
3260 if (text && tPtr->parser)
3261 (tPtr->parser) (tPtr, (void *) text);
3262 else
3263 insertPlainText(tPtr, text);
3265 tPtr->flags.needsLayOut = True;
3266 tPtr->tpos = 0;
3267 if(!tPtr->flags.frozen) {
3268 layOutDocument(tPtr);
3273 void
3274 WMAppendTextStream(WMText *tPtr, char *text)
3276 CHECK_CLASS(tPtr, WC_Text);
3278 if(!text) {
3279 if (tPtr->flags.ownsSelection)
3280 releaseSelection(tPtr);
3281 clearText(tPtr);
3282 updateScrollers(tPtr);
3283 return;
3286 tPtr->flags.prepend = False;
3287 if (text && tPtr->parser)
3288 (tPtr->parser) (tPtr, (void *) text);
3289 else
3290 insertPlainText(tPtr, text);
3292 tPtr->flags.needsLayOut = True;
3293 if(tPtr->currentTextBlock) {
3294 if(tPtr->currentTextBlock->graphic)
3295 tPtr->tpos = 1;
3296 else
3297 tPtr->tpos = tPtr->currentTextBlock->used;
3300 if(!tPtr->flags.frozen) {
3301 layOutDocument(tPtr);
3306 char *
3307 WMGetTextStream(WMText *tPtr)
3309 CHECK_CLASS(tPtr, WC_Text);
3310 return getStream(tPtr, 0, 0);
3313 char *
3314 WMGetTextSelectedStream(WMText *tPtr)
3316 CHECK_CLASS(tPtr, WC_Text);
3317 return getStream(tPtr, 1, 0);
3320 WMArray *
3321 WMGetTextObjects(WMText *tPtr)
3323 CHECK_CLASS(tPtr, WC_Text);
3324 return getStreamObjects(tPtr, 0);
3327 WMArray *
3328 WMGetTextSelectedObjects(WMText *tPtr)
3330 CHECK_CLASS(tPtr, WC_Text);
3331 return getStreamObjects(tPtr, 1);
3335 void
3336 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3338 CHECK_CLASS(tPtr, WC_Text);
3340 tPtr->delegate = delegate;
3344 void *
3345 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3346 char *description, WMColor *color,
3347 unsigned short first, unsigned short extraInfo)
3349 TextBlock *tb;
3351 if (!w || !description || !color)
3352 return NULL;
3354 tb = wmalloc(sizeof(TextBlock));
3355 if (!tb)
3356 return NULL;
3358 tb->text = wstrdup(description);
3359 tb->used = strlen(description);
3360 tb->blank = False;
3361 tb->d.widget = w;
3362 tb->color = WMRetainColor(color);
3363 tb->marginN = newMargin(tPtr, NULL);
3364 tb->allocated = extraInfo;
3365 tb->first = first;
3366 tb->kanji = False;
3367 tb->graphic = True;
3368 tb->object = True;
3369 tb->underlined = False;
3370 tb->selected = False;
3371 tb->script = 0;
3372 tb->sections = NULL;
3373 tb->nsections = 0;
3374 tb->prior = NULL;
3375 tb->next = NULL;
3377 return tb;
3381 void *
3382 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3383 char *description, WMColor *color,
3384 unsigned short first, unsigned short extraInfo)
3386 TextBlock *tb;
3388 if (!p || !description || !color)
3389 return NULL;
3391 tb = wmalloc(sizeof(TextBlock));
3392 if (!tb)
3393 return NULL;
3395 tb->text = wstrdup(description);
3396 tb->used = strlen(description);
3397 tb->blank = False;
3398 tb->d.pixmap = WMRetainPixmap(p);
3399 tb->color = WMRetainColor(color);
3400 tb->marginN = newMargin(tPtr, NULL);
3401 tb->allocated = extraInfo;
3402 tb->first = first;
3403 tb->kanji = False;
3404 tb->graphic = True;
3405 tb->object = False;
3406 tb->underlined = False;
3407 tb->selected = False;
3408 tb->script = 0;
3409 tb->sections = NULL;
3410 tb->nsections = 0;
3411 tb->prior = NULL;
3412 tb->next = NULL;
3414 return tb;
3418 void*
3419 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3420 unsigned short first, unsigned short len)
3422 TextBlock *tb;
3424 if (!font || !color)
3425 return NULL;
3427 tb = wmalloc(sizeof(TextBlock));
3428 if (!tb)
3429 return NULL;
3431 tb->allocated = reqBlockSize(len);
3432 tb->text = (char *)wmalloc(tb->allocated);
3433 memset(tb->text, 0, tb->allocated);
3435 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3436 *tb->text = ' ';
3437 tb->used = 1;
3438 tb->blank = True;
3439 } else {
3440 memcpy(tb->text, text, len);
3441 tb->used = len;
3442 tb->blank = False;
3444 tb->text[tb->used] = 0;
3446 tb->d.font = WMRetainFont(font);
3447 tb->color = WMRetainColor(color);
3448 tb->marginN = newMargin(tPtr, NULL);
3449 tb->first = first;
3450 tb->kanji = False;
3451 tb->graphic = False;
3452 tb->underlined = False;
3453 tb->selected = False;
3454 tb->script = 0;
3455 tb->sections = NULL;
3456 tb->nsections = 0;
3457 tb->prior = NULL;
3458 tb->next = NULL;
3459 return tb;
3462 void
3463 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3464 unsigned int kanji, unsigned int underlined, int script,
3465 WMRulerMargins *margins)
3467 TextBlock *tb = (TextBlock *) vtb;
3468 if (!tb)
3469 return;
3471 tb->first = first;
3472 tb->kanji = kanji;
3473 tb->underlined = underlined;
3474 tb->script = script;
3475 tb->marginN = newMargin(tPtr, margins);
3478 void
3479 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3480 unsigned int *kanji, unsigned int *underlined, int *script,
3481 WMRulerMargins *margins)
3483 TextBlock *tb = (TextBlock *) vtb;
3484 if (!tb)
3485 return;
3487 if (first) *first = tb->first;
3488 if (kanji) *kanji = tb->kanji;
3489 if (underlined) *underlined = tb->underlined;
3490 if (script) *script = tb->script;
3491 if (margins) margins = &tPtr->margins[tb->marginN];
3496 void
3497 WMPrependTextBlock(WMText *tPtr, void *vtb)
3499 TextBlock *tb = (TextBlock *)vtb;
3501 if (!tPtr || !tb)
3502 return;
3504 if (tb->graphic) {
3505 if(tb->object) {
3506 WMWidget *w = tb->d.widget;
3507 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3508 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3509 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3512 WMAddToArray(tPtr->gfxItems, (void *)tb);
3513 tPtr->tpos = 1;
3515 } else {
3516 tPtr->tpos = tb->used;
3519 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3520 tb->next = tb->prior = NULL;
3521 tb->first = True;
3522 tPtr->lastTextBlock = tPtr->firstTextBlock
3523 = tPtr->currentTextBlock = tb;
3524 return;
3527 if(!tb->first) {
3528 tb->marginN = tPtr->currentTextBlock->marginN;
3531 tb->next = tPtr->currentTextBlock;
3532 tb->prior = tPtr->currentTextBlock->prior;
3533 if (tPtr->currentTextBlock->prior)
3534 tPtr->currentTextBlock->prior->next = tb;
3536 tPtr->currentTextBlock->prior = tb;
3537 if (!tb->prior)
3538 tPtr->firstTextBlock = tb;
3540 tPtr->currentTextBlock = tb;
3544 void
3545 WMAppendTextBlock(WMText *tPtr, void *vtb)
3547 TextBlock *tb = (TextBlock *)vtb;
3549 if (!tPtr || !tb)
3550 return;
3552 if (tb->graphic) {
3553 if(tb->object) {
3554 WMWidget *w = tb->d.widget;
3555 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3556 (W_VIEW(w))->attribs.cursor =
3557 tPtr->view->screen->defaultCursor;
3558 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3561 WMAddToArray(tPtr->gfxItems, (void *)tb);
3562 tPtr->tpos = 1;
3564 } else {
3565 tPtr->tpos = tb->used;
3569 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3570 tb->next = tb->prior = NULL;
3571 tb->first = True;
3572 tPtr->lastTextBlock = tPtr->firstTextBlock
3573 = tPtr->currentTextBlock = tb;
3574 return;
3577 if(!tb->first) {
3578 tb->marginN = tPtr->currentTextBlock->marginN;
3581 tb->next = tPtr->currentTextBlock->next;
3582 tb->prior = tPtr->currentTextBlock;
3583 if (tPtr->currentTextBlock->next)
3584 tPtr->currentTextBlock->next->prior = tb;
3586 tPtr->currentTextBlock->next = tb;
3588 if (!tb->next)
3589 tPtr->lastTextBlock = tb;
3591 tPtr->currentTextBlock = tb;
3595 void*
3596 WMRemoveTextBlock(WMText *tPtr)
3598 TextBlock *tb = NULL;
3600 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3601 || !tPtr->currentTextBlock) {
3602 return NULL;
3605 tb = tPtr->currentTextBlock;
3606 if (tb->graphic) {
3607 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3609 if(tb->object) {
3610 WMUnmapWidget(tb->d.widget);
3614 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3615 if (tPtr->currentTextBlock->next)
3616 tPtr->currentTextBlock->next->prior = NULL;
3618 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3619 tPtr->currentTextBlock = tPtr->firstTextBlock;
3621 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3622 tPtr->currentTextBlock->prior->next = NULL;
3623 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3624 tPtr->currentTextBlock = tPtr->lastTextBlock;
3625 } else {
3626 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3627 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3628 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3631 return (void *)tb;
3635 #if 0
3636 static void
3637 destroyWidget(WMWidget *widget)
3639 WMDestroyWidget(widget);
3640 // -- never do this -- wfree(widget);
3642 #endif
3645 void
3646 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3648 TextBlock *tb = (TextBlock *)vtb;
3649 if (!tPtr || !tb)
3650 return;
3652 if (tb->graphic) {
3653 if(tb->object) {
3654 /* naturally, there's a danger to destroying widgets whose action
3655 * brings us here: ie. press a button to destroy it...
3656 * need to find a safer way. till then... this stays commented out */
3657 /* 5 months later... destroy it 10 seconds after now which should
3658 * be enough time for the widget's action to be completed... :-) */
3659 /* This is a bad assumption. Just destroy the widget here.
3660 * if the caller needs it, it can protect it with W_RetainView()
3661 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3662 WMDestroyWidget(tb->d.widget);
3663 } else {
3664 WMReleasePixmap(tb->d.pixmap);
3666 } else {
3667 WMReleaseFont(tb->d.font);
3670 WMReleaseColor(tb->color);
3671 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3672 if (tb->sections)
3673 wfree(tb->sections);
3674 wfree(tb->text);
3675 wfree(tb);
3679 void
3680 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3682 if (!tPtr)
3683 return;
3685 WMReleaseColor(tPtr->fgColor);
3686 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3688 paintText(tPtr);
3691 void
3692 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3694 if (!tPtr)
3695 return;
3697 WMReleaseColor(tPtr->bgColor);
3698 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3699 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3701 paintText(tPtr);
3704 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3706 if (!tPtr)
3707 return;
3709 if (tPtr->bgPixmap)
3710 WMReleasePixmap(tPtr->bgPixmap);
3712 if (pixmap)
3713 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3714 else
3715 tPtr->bgPixmap = NULL;
3718 void
3719 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3721 if (!tPtr)
3722 return;
3723 tPtr->flags.relief = relief;
3724 textDidResize(tPtr->view->delegate, tPtr->view);
3727 void
3728 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3730 if (!tPtr)
3731 return;
3733 if (shouldhave && !tPtr->hS) {
3734 tPtr->hS = WMCreateScroller(tPtr);
3735 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3736 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3737 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3738 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3739 WMMapWidget(tPtr->hS);
3740 } else if (!shouldhave && tPtr->hS) {
3741 WMUnmapWidget(tPtr->hS);
3742 WMDestroyWidget(tPtr->hS);
3743 tPtr->hS = NULL;
3746 tPtr->hpos = 0;
3747 tPtr->prevHpos = 0;
3748 textDidResize(tPtr->view->delegate, tPtr->view);
3752 void
3753 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3755 if (!tPtr)
3756 return;
3758 if(shouldhave && !tPtr->ruler) {
3759 tPtr->ruler = WMCreateRuler(tPtr);
3760 (W_VIEW(tPtr->ruler))->attribs.cursor =
3761 tPtr->view->screen->defaultCursor;
3762 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3763 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3764 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3765 } else if(!shouldhave && tPtr->ruler) {
3766 WMShowTextRuler(tPtr, False);
3767 WMDestroyWidget(tPtr->ruler);
3768 tPtr->ruler = NULL;
3770 textDidResize(tPtr->view->delegate, tPtr->view);
3773 void
3774 WMShowTextRuler(WMText *tPtr, Bool show)
3776 if(!tPtr)
3777 return;
3778 if(!tPtr->ruler)
3779 return;
3781 if(tPtr->flags.monoFont)
3782 show = False;
3784 tPtr->flags.rulerShown = show;
3785 if(show) {
3786 WMMapWidget(tPtr->ruler);
3787 } else {
3788 WMUnmapWidget(tPtr->ruler);
3791 textDidResize(tPtr->view->delegate, tPtr->view);
3794 Bool
3795 WMGetTextRulerShown(WMText *tPtr)
3797 if(!tPtr)
3798 return 0;
3800 if(!tPtr->ruler)
3801 return 0;
3803 return tPtr->flags.rulerShown;
3807 void
3808 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3810 if (!tPtr)
3811 return;
3813 if (shouldhave && !tPtr->vS) {
3814 tPtr->vS = WMCreateScroller(tPtr);
3815 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3816 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3817 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3818 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3819 WMMapWidget(tPtr->vS);
3820 } else if (!shouldhave && tPtr->vS) {
3821 WMUnmapWidget(tPtr->vS);
3822 WMDestroyWidget(tPtr->vS);
3823 tPtr->vS = NULL;
3826 tPtr->vpos = 0;
3827 tPtr->prevVpos = 0;
3828 textDidResize(tPtr->view->delegate, tPtr->view);
3833 Bool
3834 WMScrollText(WMText *tPtr, int amount)
3836 Bool scroll=False;
3837 if (!tPtr)
3838 return False;
3839 if (amount == 0 || !tPtr->view->flags.realized)
3840 return False;
3842 if (amount < 0) {
3843 if (tPtr->vpos > 0) {
3844 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3845 else tPtr->vpos=0;
3846 scroll=True;
3848 } else {
3849 int limit = tPtr->docHeight - tPtr->visible.h;
3850 if (tPtr->vpos < limit) {
3851 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3852 else tPtr->vpos = limit;
3853 scroll = True;
3857 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3858 updateScrollers(tPtr);
3859 paintText(tPtr);
3861 tPtr->prevVpos = tPtr->vpos;
3862 return scroll;
3865 Bool
3866 WMPageText(WMText *tPtr, Bool direction)
3868 if (!tPtr) return False;
3869 if (!tPtr->view->flags.realized) return False;
3871 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3874 void
3875 WMSetTextEditable(WMText *tPtr, Bool editable)
3877 if (!tPtr)
3878 return;
3879 tPtr->flags.editable = editable;
3882 int
3883 WMGetTextEditable(WMText *tPtr)
3885 if (!tPtr)
3886 return 0;
3887 return tPtr->flags.editable;
3890 void
3891 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3893 if (!tPtr)
3894 return;
3895 tPtr->flags.indentNewLine = indent;
3898 void
3899 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3901 if (!tPtr)
3902 return;
3903 tPtr->flags.ignoreNewLine = ignore;
3906 Bool
3907 WMGetTextIgnoresNewline(WMText *tPtr)
3909 if (!tPtr)
3910 return True;
3911 return tPtr->flags.ignoreNewLine;
3914 void
3915 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3917 if (!tPtr)
3918 return;
3920 if (mono) {
3921 if(tPtr->flags.rulerShown)
3922 WMShowTextRuler(tPtr, False);
3923 if(tPtr->flags.alignment != WALeft)
3924 tPtr->flags.alignment = WALeft;
3927 tPtr->flags.monoFont = mono;
3928 textDidResize(tPtr->view->delegate, tPtr->view);
3931 Bool
3932 WMGetTextUsesMonoFont(WMText *tPtr)
3934 if (!tPtr)
3935 return True;
3936 return tPtr->flags.monoFont;
3940 void
3941 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3943 if (!tPtr)
3944 return;
3946 WMReleaseFont(tPtr->dFont);
3947 if (font)
3948 tPtr->dFont = WMRetainFont(font);
3949 else
3950 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3953 WMFont *
3954 WMGetTextDefaultFont(WMText *tPtr)
3956 if (!tPtr)
3957 return NULL;
3958 else
3959 return WMRetainFont(tPtr->dFont);
3962 void
3963 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3965 if (!tPtr)
3966 return;
3968 WMReleaseColor(tPtr->dColor);
3969 if (color)
3970 tPtr->dColor = WMRetainColor(color);
3971 else
3972 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3975 WMColor *
3976 WMGetTextDefaultColor(WMText *tPtr)
3978 if (!tPtr)
3979 return NULL;
3980 else
3981 return WMRetainColor(tPtr->dColor);
3984 void
3985 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3987 if (!tPtr)
3988 return;
3989 if(tPtr->flags.monoFont)
3990 tPtr->flags.alignment = WALeft;
3991 else
3992 tPtr->flags.alignment = alignment;
3993 WMThawText(tPtr);
3996 int
3997 WMGetTextInsertType(WMText *tPtr)
3999 if (!tPtr)
4000 return 0;
4001 return tPtr->flags.prepend;
4005 void
4006 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
4008 if (!tPtr || !color)
4009 return;
4011 setSelectionProperty(tPtr, NULL, color, -1);
4014 WMColor *
4015 WMGetTextSelectionColor(WMText *tPtr)
4017 TextBlock *tb;
4019 if (!tPtr)
4020 return NULL;
4022 tb = tPtr->currentTextBlock;
4024 if (!tb || !tPtr->flags.ownsSelection)
4025 return NULL;
4027 if(!tb->selected)
4028 return NULL;
4030 return tb->color;
4034 void
4035 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4037 if (!tPtr || !font)
4038 return;
4040 setSelectionProperty(tPtr, font, NULL, -1) ;
4043 WMFont *
4044 WMGetTextSelectionFont(WMText *tPtr)
4046 TextBlock *tb;
4048 if (!tPtr)
4049 return NULL;
4051 tb = tPtr->currentTextBlock;
4053 if (!tb || !tPtr->flags.ownsSelection)
4054 return NULL;
4056 if(!tb->selected)
4057 return NULL;
4059 if(tb->graphic) {
4060 tb = getFirstNonGraphicBlockFor(tb, 1);
4061 if(!tb)
4062 return NULL;
4064 return (tb->selected ? tb->d.font : NULL);
4068 void
4069 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4071 if (!tPtr || (underlined!=0 && underlined !=1))
4072 return;
4074 setSelectionProperty(tPtr, NULL, NULL, underlined);
4078 int
4079 WMGetTextSelectionUnderlined(WMText *tPtr)
4081 TextBlock *tb;
4083 if (!tPtr)
4084 return 0;
4086 tb = tPtr->currentTextBlock;
4088 if (!tb || !tPtr->flags.ownsSelection)
4089 return 0;
4091 if(!tb->selected)
4092 return 0;
4094 return tb->underlined;
4098 void
4099 WMFreezeText(WMText *tPtr)
4101 if (!tPtr)
4102 return;
4104 tPtr->flags.frozen = True;
4108 void
4109 WMThawText(WMText *tPtr)
4111 if (!tPtr)
4112 return;
4114 tPtr->flags.frozen = False;
4116 if(tPtr->flags.monoFont) {
4117 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4118 TextBlock *tb;
4120 /* make sure to unmap widgets no matter where they are */
4121 /* they'll be later remapped if needed by paintText */
4122 for(j=0; j<c; j++) {
4123 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4124 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4125 WMUnmapWidget(tb->d.widget);
4131 tPtr->flags.laidOut = False;
4132 layOutDocument(tPtr);
4133 updateScrollers(tPtr);
4134 paintText(tPtr);
4135 tPtr->flags.needsLayOut = False;
4139 /* find first occurence of a string */
4140 static char *
4141 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4142 Bool caseSensitive)
4144 char *ptr;
4146 if(!haystack || !needle || !end)
4147 return NULL;
4149 for (ptr = haystack; ptr < end; ptr++) {
4150 if(caseSensitive) {
4151 if (*ptr == *needle && !strncmp(ptr, needle, len))
4152 return ptr;
4154 } else {
4155 if (tolower(*ptr) == tolower(*needle) &&
4156 !strncasecmp(ptr, needle, len))
4157 return ptr;
4161 return NULL;
4164 /* find last occurence of a string */
4165 static char *
4166 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4167 Bool caseSensitive)
4169 char *ptr;
4171 if(!haystack || !needle || !end)
4172 return NULL;
4174 for (ptr = haystack-2; ptr > end; ptr--) {
4175 if(caseSensitive) {
4176 if (*ptr == *needle && !strncmp(ptr, needle, len))
4177 return ptr;
4178 } else {
4179 if (tolower(*ptr) == tolower(*needle) &&
4180 !strncasecmp(ptr, needle, len))
4181 return ptr;
4185 return NULL;
4189 Bool
4190 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4191 Bool caseSensitive)
4193 TextBlock *tb;
4194 char *mark=NULL;
4195 unsigned short pos;
4197 if (!tPtr || !needle)
4198 return False;
4200 #if 0
4201 if (! (tb = tPtr->currentTextBlock)) {
4202 if (! (tb = ( (direction > 0) ?
4203 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4204 return False;
4206 } else {
4207 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4208 tb = (direction>0) ? tb->next : tb->prior; */
4209 if(tb != tPtr->lastTextBlock)
4210 tb = tb->prior;
4212 #endif
4213 tb = tPtr->currentTextBlock;
4214 pos = tPtr->tpos;
4217 while(tb) {
4218 if (!tb->graphic) {
4220 if(direction > 0) {
4221 if(pos+1 < tb->used)
4222 pos++;
4224 if(tb->used - pos> 0 && pos > 0) {
4225 mark = mystrstr(&tb->text[pos], needle,
4226 strlen(needle), &tb->text[tb->used], caseSensitive);
4228 } else {
4229 tb = tb->next;
4230 pos = 0;
4231 continue;
4234 } else {
4235 if(pos-1 > 0)
4236 pos--;
4238 if(pos > 0) {
4239 mark = mystrrstr(&tb->text[pos], needle,
4240 strlen(needle), tb->text, caseSensitive);
4241 } else {
4242 tb = tb->prior;
4243 if(!tb)
4244 return False;
4245 pos = tb->used;
4246 continue;
4251 if(mark) {
4252 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4254 tPtr->tpos = (int)(mark - tb->text);
4255 tPtr->currentTextBlock = tb;
4256 updateCursorPosition(tPtr);
4257 tPtr->sel.y = tPtr->cursor.y+5;
4258 tPtr->sel.h = tPtr->cursor.h-10;
4259 tPtr->sel.x = tPtr->cursor.x +1;
4260 tPtr->sel.w = WMIN(WMWidthOfString(font,
4261 &tb->text[tPtr->tpos], strlen(needle)),
4262 tPtr->docWidth - tPtr->sel.x);
4263 tPtr->flags.ownsSelection = True;
4264 paintText(tPtr);
4266 return True;
4270 tb = (direction>0) ? tb->next : tb->prior;
4271 if(tb) {
4272 pos = (direction>0) ? 0 : tb->used;
4276 return False;
4280 Bool
4281 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4283 if (!tPtr)
4284 return False;
4286 if (!tPtr->flags.ownsSelection)
4287 return False;
4289 removeSelection(tPtr);
4291 if(replacement) {
4292 insertTextInteractively(tPtr, replacement, strlen(replacement));
4293 updateCursorPosition(tPtr);
4294 paintText(tPtr);
4297 return True;