Autoarrange icons after unhiding an application that has miniaturized
[wmaker-crm.git] / WINGs / wtext.c
blob6bd0219b3259722499aaf544b5cfbd342abd6aa6
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;
573 static TextBlock*
574 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
576 TextBlock *hold = tb;
578 if (!tb)
579 return NULL;
581 while (tb) {
582 if (!tb->graphic)
583 break;
584 tb = (dir? tb->next : tb->prior);
587 if(!tb) {
588 tb = hold;
589 while (tb) {
590 if (!tb->graphic)
591 break;
592 tb = (dir? tb->prior : tb->next);
596 if(!tb)
597 return NULL;
599 return tb;
603 static Bool
604 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
605 TextBlock *tb)
607 if (tPtr->flags.monoFont && tb->graphic) {
608 tb = getFirstNonGraphicBlockFor(tb, *dir);
609 if(!tb)
610 return 0;
612 if (tb->graphic) {
613 tPtr->currentTextBlock =
614 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
615 tPtr->tpos = 0;
616 return 0;
621 if(!tb->sections) {
622 layOutDocument(tPtr);
623 return 0;
626 *dir = !(y <= tb->sections[0].y);
627 if(*dir) {
628 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
629 && (y >= tb->sections[0]._y ) ) {
630 /* if it's on the same line */
631 if(x < tb->sections[0].x)
632 *dir = 0;
634 } else {
635 if ( ( y <= tb->sections[tb->nsections-1]._y
636 + tb->sections[tb->nsections-1].h )
637 && (y >= tb->sections[tb->nsections-1]._y ) ) {
638 /* if it's on the same line */
639 if(x > tb->sections[tb->nsections-1].x)
640 *dir = 1;
644 return 1;
648 static void
649 paintText(Text *tPtr)
651 TextBlock *tb;
652 WMFont *font;
653 char *text;
654 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
655 WMScreen *scr = tPtr->view->screen;
656 Display *dpy = tPtr->view->screen->display;
657 Window win = tPtr->view->window;
658 WMColor *color;
660 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
661 return;
664 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0,
665 tPtr->visible.w, tPtr->visible.h);
667 if (tPtr->bgPixmap) {
668 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
669 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
670 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
673 if (! (tb = tPtr->currentTextBlock)) {
674 if (! (tb = tPtr->firstTextBlock)) {
675 goto _copy_area;
679 done = False;
683 /* first, which direction? Don't waste time looking all over,
684 since the parts to be drawn will most likely be near what
685 was previously drawn */
686 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
687 goto _copy_area;
689 while(tb) {
691 if (tb->graphic && tPtr->flags.monoFont)
692 goto _getSibling;
694 if(dir) {
695 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
696 + tb->sections[tb->nsections-1].h)
697 break;
698 } else {
699 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
700 + tb->sections[tb->nsections-1].h)
701 break;
704 _getSibling:
705 if(dir) {
706 if(tb->next)
707 tb = tb->next;
708 else break;
709 } else {
710 if(tb->prior)
711 tb = tb->prior;
712 else break;
717 /* first, place all text that can be viewed */
718 while (!done && tb) {
719 if (tb->graphic) {
720 tb = tb->next;
721 continue;
724 tb->selected = False;
726 for(s=0; s<tb->nsections && !done; s++) {
728 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
729 done = True;
730 break;
733 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
734 continue;
736 if (tPtr->flags.monoFont) {
737 font = tPtr->dFont;
738 color = tPtr->fgColor;
739 } else {
740 font = tb->d.font;
741 color = tb->color;
744 if (tPtr->flags.ownsSelection) {
745 XRectangle rect;
747 if (sectionWasSelected(tPtr, tb, &rect, s)) {
748 tb->selected = True;
749 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
750 rect.x, rect.y, rect.width, rect.height);
754 prev_y = tb->sections[s]._y;
756 len = tb->sections[s].end - tb->sections[s].begin;
757 text = &(tb->text[tb->sections[s].begin]);
758 y = tb->sections[s].y - tPtr->vpos;
759 WMDrawString(scr, tPtr->db, color, font,
760 tb->sections[s].x - tPtr->hpos, y, text, len);
762 if (!tPtr->flags.monoFont && tb->underlined) {
763 XDrawLine(dpy, tPtr->db, WMColorGC(color),
764 tb->sections[s].x - tPtr->hpos,
765 y + font->y + 1,
766 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
767 y + font->y + 1);
770 tb = (!done? tb->next : NULL);
773 /* now , show all graphic items that can be viewed */
774 c = WMGetArrayItemCount(tPtr->gfxItems);
775 if (c > 0 && !tPtr->flags.monoFont) {
776 int j, h;
778 for(j=0; j<c; j++) {
779 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
781 /* if it's not viewable, and mapped, unmap it */
782 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
783 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
785 if(tb->object) {
786 if ((W_VIEW(tb->d.widget))->flags.mapped) {
787 WMUnmapWidget(tb->d.widget);
790 } else {
791 /* if it's viewable, and not mapped, map it */
792 if(tb->object) {
793 W_View *view = W_VIEW(tb->d.widget);
795 if (!view->flags.realized)
796 WMRealizeWidget(tb->d.widget);
797 if(!view->flags.mapped) {
798 XMapWindow(view->screen->display, view->window);
799 XFlush(view->screen->display);
800 view->flags.mapped = 1;
804 if(tb->object) {
805 WMMoveWidget(tb->d.widget,
806 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
807 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
808 h = WMWidgetHeight(tb->d.widget) + 1;
810 } else {
811 WMDrawPixmap(tb->d.pixmap, tPtr->db,
812 tb->sections[0].x - tPtr->hpos,
813 tb->sections[0].y - tPtr->vpos);
814 h = tb->d.pixmap->height + 1;
818 if (tPtr->flags.ownsSelection) {
819 XRectangle rect;
821 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
822 Drawable d = (0&&tb->object?
823 (WMWidgetView(tb->d.widget))->window : tPtr->db);
825 tb->selected = True;
826 XFillRectangle(dpy, d, tPtr->stippledGC,
827 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,*/
828 rect.x, rect.y, rect.width, rect.height);
832 if (!tPtr->flags.monoFont && tb->underlined) {
833 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
834 tb->sections[0].x - tPtr->hpos,
835 tb->sections[0].y + h - tPtr->vpos,
836 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
837 tb->sections[0].y + h - tPtr->vpos);
844 _copy_area:
845 if (tPtr->flags.editable && tPtr->flags.cursorShown
846 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
847 int y = tPtr->cursor.y - tPtr->vpos;
848 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
849 tPtr->cursor.x, y,
850 tPtr->cursor.x, y + tPtr->cursor.h);
853 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
854 tPtr->visible.w, tPtr->visible.h,
855 tPtr->visible.x, tPtr->visible.y);
857 W_DrawRelief(scr, win, 0, 0,
858 tPtr->view->size.width, tPtr->view->size.height,
859 tPtr->flags.relief);
861 if (tPtr->ruler && tPtr->flags.rulerShown)
862 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor),
863 2, 42, tPtr->view->size.width-4, 42);
867 static void
868 mouseOverObject(Text *tPtr, int x, int y)
870 TextBlock *tb;
871 Bool result = False;
873 x -= tPtr->visible.x;
874 x += tPtr->hpos;
875 y -= tPtr->visible.y;
876 y += tPtr->vpos;
878 if(tPtr->flags.ownsSelection) {
879 if(tPtr->sel.x <= x
880 && tPtr->sel.y <= y
881 && tPtr->sel.x + tPtr->sel.w >= x
882 && tPtr->sel.y + tPtr->sel.h >= y) {
883 tPtr->flags.isOverGraphic = 1;
884 result = True;
889 if(!result) {
890 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
892 if (c<1)
893 tPtr->flags.isOverGraphic = 0;
896 for(j=0; j<c; j++) {
897 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
899 if(!tb || !tb->sections) {
900 tPtr->flags.isOverGraphic = 0;
901 return;
904 if(!tb->object) {
905 if(tb->sections[0].x <= x
906 && tb->sections[0].y <= y
907 && tb->sections[0].x + tb->sections[0].w >= x
908 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
909 tPtr->flags.isOverGraphic = 3;
910 result = True;
911 break;
919 if(!result)
920 tPtr->flags.isOverGraphic = 0;
923 tPtr->view->attribs.cursor = (result?
924 tPtr->view->screen->defaultCursor
925 : tPtr->view->screen->textCursor);
927 XSetWindowAttributes attribs;
928 attribs.cursor = tPtr->view->attribs.cursor;
929 XChangeWindowAttributes(tPtr->view->screen->display,
930 tPtr->view->window, CWCursor,
931 &attribs);
935 #if DO_BLINK
937 static void
938 blinkCursor(void *data)
940 Text *tPtr = (Text*)data;
942 if (tPtr->flags.cursorShown) {
943 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
944 blinkCursor, data);
945 } else {
946 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
947 blinkCursor, data);
949 paintText(tPtr);
950 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
952 #endif
954 static void
955 updateCursorPosition(Text *tPtr)
957 TextBlock *tb = NULL;
958 int x, y, h, s;
960 if(tPtr->flags.needsLayOut)
961 layOutDocument(tPtr);
963 if (! (tb = tPtr->currentTextBlock)) {
964 if (! (tb = tPtr->firstTextBlock)) {
965 WMFont *font = tPtr->dFont;
966 tPtr->tpos = 0;
967 tPtr->cursor.h = font->height + abs(font->height-font->y);
969 tPtr->cursor.y = 2;
970 tPtr->cursor.x = 2;
971 return;
976 if(tb->blank) {
977 tPtr->tpos = 0;
978 y = tb->sections[0].y;
979 h = tb->sections[0].h;
980 x = tb->sections[0].x;
982 } else if(tb->graphic) {
983 y = tb->sections[0].y;
984 h = tb->sections[0].h;
985 x = tb->sections[0].x;
986 if(tPtr->tpos == 1)
987 x += tb->sections[0].w;
989 } else {
990 if(tPtr->tpos > tb->used)
991 tPtr->tpos = tb->used;
993 for(s=0; s<tb->nsections-1; s++) {
995 if(tPtr->tpos >= tb->sections[s].begin
996 && tPtr->tpos <= tb->sections[s].end)
997 break;
1000 y = tb->sections[s]._y;
1001 h = tb->sections[s].h;
1002 x = tb->sections[s].x + WMWidthOfString(
1003 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
1004 &tb->text[tb->sections[s].begin],
1005 tPtr->tpos - tb->sections[s].begin);
1008 tPtr->cursor.y = y;
1009 tPtr->cursor.h = h;
1010 tPtr->cursor.x = x;
1013 /* scroll the bars if the cursor is not visible */
1014 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1015 if(tPtr->cursor.y+tPtr->cursor.h
1016 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1017 tPtr->vpos +=
1018 (tPtr->cursor.y+tPtr->cursor.h+10
1019 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1020 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1021 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1026 updateScrollers(tPtr);
1030 static void
1031 cursorToTextPosition(Text *tPtr, int x, int y)
1033 TextBlock *tb = NULL;
1034 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1035 char *text;
1037 if(tPtr->flags.needsLayOut)
1038 layOutDocument(tPtr);
1040 y += (tPtr->vpos - tPtr->visible.y);
1041 if (y<0)
1042 y = 0;
1044 x -= (tPtr->visible.x - 2);
1045 if (x<0)
1046 x=0;
1048 /* clicked is relative to document, not window... */
1049 tPtr->clicked.x = x;
1050 tPtr->clicked.y = y;
1052 if (! (tb = tPtr->currentTextBlock)) {
1053 if (! (tb = tPtr->firstTextBlock)) {
1054 WMFont *font = tPtr->dFont;
1055 tPtr->tpos = 0;
1056 tPtr->cursor.h = font->height + abs(font->height-font->y);
1057 tPtr->cursor.y = 2;
1058 tPtr->cursor.x = 2;
1059 return;
1063 /* first, which direction? Most likely, newly clicked
1064 position will be close to previous */
1065 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1066 return;
1069 s = (dir? 0 : tb->nsections-1);
1070 if ( y >= tb->sections[s]._y
1071 && y <= tb->sections[s]._y + tb->sections[s].h) {
1072 goto _doneV;
1075 /* get the first (or last) section of the TextBlock that
1076 lies about the vertical click point */
1077 done = False;
1078 while (!done && tb) {
1080 if (tPtr->flags.monoFont && tb->graphic) {
1081 if( (dir?tb->next:tb->prior))
1082 tb = (dir?tb->next:tb->prior);
1083 continue;
1086 s = (dir? 0 : tb->nsections-1);
1087 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1089 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1090 ( y >= tb->sections[s]._y ) ) ) {
1091 done = True;
1092 } else {
1093 dir? s++ : s--;
1097 if (!done) {
1098 if ( (dir? tb->next : tb->prior)) {
1099 tb = (dir ? tb->next : tb->prior);
1100 } else {
1101 pos = tb->used;
1102 break; /* goto _doneH; */
1108 if (s<0 || s>=tb->nsections) {
1109 s = (dir? tb->nsections-1 : 0);
1112 _doneV:
1113 /* we have the line, which TextBlock on that line is it? */
1114 pos = (dir?0:tb->sections[s].begin);
1115 if (tPtr->flags.monoFont && tb->graphic) {
1116 TextBlock *hold = tb;
1117 tb = getFirstNonGraphicBlockFor(hold, dir);
1119 if(!tb) {
1120 tPtr->tpos = 0;
1121 tb = hold;
1122 s = 0;
1123 goto _doNothing;
1128 if(tb->blank)
1129 _w = 0;
1131 _y = tb->sections[s]._y;
1133 while (tb) {
1135 if (tPtr->flags.monoFont && tb->graphic) {
1136 tb = (dir ? tb->next : tb->prior);
1137 continue;
1140 if (dir) {
1141 if (tb->graphic) {
1142 if(tb->object)
1143 _w = WMWidgetWidth(tb->d.widget)-5;
1144 else
1145 _w = tb->d.pixmap->width-5;
1147 if (tb->sections[0].x + _w >= x)
1148 break;
1149 } else {
1150 text = &(tb->text[tb->sections[s].begin]);
1151 len = tb->sections[s].end - tb->sections[s].begin;
1152 _w = WMWidthOfString(tb->d.font, text, len);
1153 if (tb->sections[s].x + _w >= x)
1154 break;
1157 } else {
1158 if (tb->sections[s].x <= x)
1159 break;
1162 if ((dir? tb->next : tb->prior)) {
1163 TextBlock *nxt = (dir? tb->next : tb->prior);
1164 if (tPtr->flags.monoFont && nxt->graphic) {
1165 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1166 if (!nxt) {
1167 pos = (dir?0:tb->sections[s].begin);
1168 tPtr->cursor.x = tb->sections[s].x;
1169 goto _doneH;
1173 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1174 /* this must be the last/first on this line. stop */
1175 pos = (dir? tb->sections[s].end : 0);
1176 tPtr->cursor.x = tb->sections[s].x;
1177 if (!tb->blank) {
1178 if (tb->graphic) {
1179 if(tb->object)
1180 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1181 else
1182 tPtr->cursor.x += tb->d.pixmap->width;
1183 } else if (pos > tb->sections[s].begin) {
1184 tPtr->cursor.x +=
1185 WMWidthOfString(tb->d.font,
1186 &(tb->text[tb->sections[s].begin]),
1187 pos - tb->sections[s].begin);
1190 goto _doneH;
1194 if ( (dir? tb->next : tb->prior)) {
1195 tb = (dir ? tb->next : tb->prior);
1196 } else {
1197 done = True;
1198 break;
1201 if (tb)
1202 s = (dir? 0 : tb->nsections-1);
1205 /* we have said TextBlock, now where within it? */
1206 if (tb) {
1207 if(tb->graphic) {
1208 int gw = (tb->object ?
1209 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1211 tPtr->cursor.x = tb->sections[0].x;
1213 if(x > tPtr->cursor.x + gw/2) {
1214 pos = 1;
1215 tPtr->cursor.x += gw;
1216 } else {
1217 printf("first %d\n", tb->first);
1218 if(tb->prior) {
1219 if(tb->prior->graphic) pos = 1;
1220 else pos = tb->prior->used;
1221 tb = tb->prior;
1222 } else pos = 0;
1226 s = 0;
1227 goto _doneH;
1229 } else {
1230 WMFont *f = tb->d.font;
1231 len = tb->sections[s].end - tb->sections[s].begin;
1232 text = &(tb->text[tb->sections[s].begin]);
1234 _w = x - tb->sections[s].x;
1235 pos = 0;
1237 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1238 pos++;
1240 tPtr->cursor.x = tb->sections[s].x +
1241 (pos? WMWidthOfString(f, text, pos) : 0);
1243 pos += tb->sections[s].begin;
1247 _doneH:
1248 if(tb->graphic) {
1249 tPtr->tpos = (pos<=1)? pos : 0;
1250 } else {
1251 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1253 _doNothing:
1254 if (!tb)
1255 printf("...for this app will surely crash :-)\n");
1257 tPtr->currentTextBlock = tb;
1258 tPtr->cursor.h = tb->sections[s].h;
1259 tPtr->cursor.y = tb->sections[s]._y;
1261 /* scroll the bars if the cursor is not visible */
1262 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1263 if(tPtr->cursor.y+tPtr->cursor.h
1264 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1265 tPtr->vpos +=
1266 (tPtr->cursor.y+tPtr->cursor.h+10
1267 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1268 updateScrollers(tPtr);
1269 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1270 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1271 updateScrollers(tPtr);
1279 static void
1280 updateScrollers(Text *tPtr)
1283 if (tPtr->flags.frozen)
1284 return;
1286 if (tPtr->vS) {
1287 if (tPtr->docHeight <= tPtr->visible.h) {
1288 WMSetScrollerParameters(tPtr->vS, 0, 1);
1289 tPtr->vpos = 0;
1290 } else {
1291 float hmax = (float)(tPtr->docHeight);
1292 WMSetScrollerParameters(tPtr->vS,
1293 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1294 (float)tPtr->visible.h/hmax);
1296 } else tPtr->vpos = 0;
1298 if (tPtr->hS) {
1299 if (tPtr->docWidth <= tPtr->visible.w) {
1300 WMSetScrollerParameters(tPtr->hS, 0, 1);
1301 tPtr->hpos = 0;
1302 } else {
1303 float wmax = (float)(tPtr->docWidth);
1304 WMSetScrollerParameters(tPtr->hS,
1305 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1306 (float)tPtr->visible.w/wmax);
1308 } else tPtr->hpos = 0;
1311 static void
1312 scrollersCallBack(WMWidget *w, void *self)
1314 Text *tPtr = (Text *)self;
1315 Bool scroll = False;
1316 int which;
1318 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1319 return;
1321 if (w == tPtr->vS) {
1322 int height;
1323 height = tPtr->visible.h;
1325 which = WMGetScrollerHitPart(tPtr->vS);
1326 switch(which) {
1328 case WSDecrementLine:
1329 if (tPtr->vpos > 0) {
1330 if (tPtr->vpos>16) tPtr->vpos-=16;
1331 else tPtr->vpos=0;
1332 scroll=True;
1334 break;
1336 case WSIncrementLine: {
1337 int limit = tPtr->docHeight - height;
1338 if (tPtr->vpos < limit) {
1339 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1340 else tPtr->vpos=limit;
1341 scroll = True;
1344 break;
1346 case WSDecrementPage:
1347 if(((int)tPtr->vpos - (int)height) >= 0)
1348 tPtr->vpos -= height;
1349 else
1350 tPtr->vpos = 0;
1352 scroll = True;
1353 break;
1355 case WSIncrementPage:
1356 tPtr->vpos += height;
1357 if (tPtr->vpos > (tPtr->docHeight - height))
1358 tPtr->vpos = tPtr->docHeight - height;
1359 scroll = True;
1360 break;
1363 case WSKnob:
1364 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1365 * (float)(tPtr->docHeight - height);
1366 scroll = True;
1367 break;
1369 case WSKnobSlot:
1370 case WSNoPart:
1371 break;
1373 scroll = (tPtr->vpos != tPtr->prevVpos);
1374 tPtr->prevVpos = tPtr->vpos;
1378 if (w == tPtr->hS) {
1379 int width = tPtr->visible.w;
1381 which = WMGetScrollerHitPart(tPtr->hS);
1382 switch(which) {
1384 case WSDecrementLine:
1385 if (tPtr->hpos > 0) {
1386 if (tPtr->hpos>16) tPtr->hpos-=16;
1387 else tPtr->hpos=0;
1388 scroll=True;
1389 }break;
1391 case WSIncrementLine: {
1392 int limit = tPtr->docWidth - width;
1393 if (tPtr->hpos < limit) {
1394 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1395 else tPtr->hpos=limit;
1396 scroll = True;
1398 }break;
1400 case WSDecrementPage:
1401 if(((int)tPtr->hpos - (int)width) >= 0)
1402 tPtr->hpos -= width;
1403 else
1404 tPtr->hpos = 0;
1406 scroll = True;
1407 break;
1409 case WSIncrementPage:
1410 tPtr->hpos += width;
1411 if (tPtr->hpos > (tPtr->docWidth - width))
1412 tPtr->hpos = tPtr->docWidth - width;
1413 scroll = True;
1414 break;
1417 case WSKnob:
1418 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1419 * (float)(tPtr->docWidth - width);
1420 scroll = True;
1421 break;
1423 case WSKnobSlot:
1424 case WSNoPart:
1425 break;
1427 scroll = (tPtr->hpos != tPtr->prevHpos);
1428 tPtr->prevHpos = tPtr->hpos;
1431 if (scroll) {
1432 updateScrollers(tPtr);
1433 paintText(tPtr);
1439 typedef struct {
1440 TextBlock *tb;
1441 unsigned short begin, end; /* what part of the text block */
1442 } myLineItems;
1445 static int
1446 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1448 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1449 WMFont *font;
1450 char *text;
1451 TextBlock *tb, *tbsame=NULL;
1453 if(!items || nitems == 0)
1454 return 0;
1456 for(i=0; i<nitems; i++) {
1457 tb = items[i].tb;
1459 if (tb->graphic) {
1460 if (!tPtr->flags.monoFont) {
1461 if(tb->object) {
1462 WMWidget *wdt = tb->d.widget;
1463 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1464 if (tPtr->flags.alignment != WALeft)
1465 lw += WMWidgetWidth(wdt);
1466 } else {
1467 line_height = WMAX(line_height,
1468 tb->d.pixmap->height + max_d);
1469 if (tPtr->flags.alignment != WALeft)
1470 lw += tb->d.pixmap->width;
1474 } else {
1475 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1476 /*max_d = WMAX(max_d, abs(font->height-font->y));*/
1477 max_d = 2;
1478 line_height = WMAX(line_height, font->height + max_d);
1479 text = &(tb->text[items[i].begin]);
1480 len = items[i].end - items[i].begin;
1481 if (tPtr->flags.alignment != WALeft)
1482 lw += WMWidthOfString(font, text, len);
1486 if (tPtr->flags.alignment == WARight) {
1487 j = tPtr->visible.w - lw;
1488 } else if (tPtr->flags.alignment == WACenter) {
1489 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1492 for(i=0; i<nitems; i++) {
1493 tb = items[i].tb;
1495 if (tbsame == tb) { /* extend it, since it's on same line */
1496 tb->sections[tb->nsections-1].end = items[i].end;
1497 n = tb->nsections-1;
1498 } else {
1499 tb->sections = wrealloc(tb->sections,
1500 (++tb->nsections)*sizeof(Section));
1501 n = tb->nsections-1;
1502 tb->sections[n]._y = y + max_d;
1503 tb->sections[n].max_d = max_d;
1504 tb->sections[n].x = x+j;
1505 tb->sections[n].h = line_height;
1506 tb->sections[n].begin = items[i].begin;
1507 tb->sections[n].end = items[i].end;
1510 tb->sections[n].last = (i+1 == nitems);
1512 if (tb->graphic) {
1513 if (!tPtr->flags.monoFont) {
1514 if(tb->object) {
1515 WMWidget *wdt = tb->d.widget;
1516 tb->sections[n].y = max_d + y
1517 + line_height - WMWidgetHeight(wdt);
1518 tb->sections[n].w = WMWidgetWidth(wdt);
1519 } else {
1520 tb->sections[n].y = y + line_height
1521 + max_d - tb->d.pixmap->height;
1522 tb->sections[n].w = tb->d.pixmap->width;
1524 x += tb->sections[n].w;
1526 } else {
1527 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1528 len = items[i].end - items[i].begin;
1529 text = &(tb->text[items[i].begin]);
1531 tb->sections[n].y = y+line_height-font->y;
1532 tb->sections[n].w =
1533 WMWidthOfString(font,
1534 &(tb->text[tb->sections[n].begin]),
1535 tb->sections[n].end - tb->sections[n].begin);
1537 x += WMWidthOfString(font, text, len);
1540 tbsame = tb;
1543 return line_height;
1548 static void
1549 layOutDocument(Text *tPtr)
1551 TextBlock *tb;
1552 myLineItems *items = NULL;
1553 unsigned int itemsSize=0, nitems=0, begin, end;
1554 WMFont *font;
1555 unsigned int x, y=0, lw = 0, width=0, bmargin;
1556 char *start=NULL, *mark=NULL;
1558 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1559 return;
1561 assert(tPtr->visible.w > 20);
1563 tPtr->docWidth = tPtr->visible.w;
1564 x = tPtr->margins[tb->marginN].first;
1565 bmargin = tPtr->margins[tb->marginN].body;
1567 /* only partial layOut needed: re-Lay only affected textblocks */
1568 if (tPtr->flags.laidOut) {
1569 tb = tPtr->currentTextBlock;
1571 /* search backwards for textblocks on same line */
1572 while (tb->prior) {
1573 if (!tb->sections || tb->nsections<1) {
1574 tb = tPtr->firstTextBlock;
1575 tPtr->flags.laidOut = False;
1576 y = 0;
1577 goto _layOut;
1580 if(!tb->prior->sections || tb->prior->nsections<1) {
1581 tb = tPtr->firstTextBlock;
1582 tPtr->flags.laidOut = False;
1583 y = 0;
1584 goto _layOut;
1587 if (tb->sections[0]._y !=
1588 tb->prior->sections[tb->prior->nsections-1]._y) {
1589 break;
1591 tb = tb->prior;
1594 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1595 y = tb->prior->sections[tb->prior->nsections-1]._y +
1596 tb->prior->sections[tb->prior->nsections-1].h -
1597 tb->prior->sections[tb->prior->nsections-1].max_d;
1598 } else {
1599 y = 0;
1603 _layOut:
1604 while (tb) {
1606 if (tb->sections && tb->nsections>0) {
1607 wfree(tb->sections);
1608 tb->sections = NULL;
1609 tb->nsections = 0;
1612 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1613 TextBlock *next = tb->next;
1614 tPtr->currentTextBlock = tb;
1615 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1616 tb = next;
1617 tb->first = True;
1618 continue;
1621 if (tb->first && tb != tPtr->firstTextBlock) {
1622 y += layOutLine(tPtr, items, nitems, x, y);
1623 x = tPtr->margins[tb->marginN].first;
1624 bmargin = tPtr->margins[tb->marginN].body;
1625 nitems = 0;
1626 lw = 0;
1629 if (tb->graphic) {
1630 if (!tPtr->flags.monoFont) {
1631 if(tb->object)
1632 width = WMWidgetWidth(tb->d.widget);
1633 else
1634 width = tb->d.pixmap->width;
1636 if (width > tPtr->docWidth)
1637 tPtr->docWidth = width;
1639 lw += width;
1640 if (lw >= tPtr->visible.w - x ) {
1641 y += layOutLine(tPtr, items, nitems, x, y);
1642 nitems = 0;
1643 x = bmargin;
1644 lw = width;
1647 if(nitems + 1> itemsSize) {
1648 items = wrealloc(items,
1649 (++itemsSize)*sizeof(myLineItems));
1652 items[nitems].tb = tb;
1653 items[nitems].begin = 0;
1654 items[nitems].end = 0;
1655 nitems++;
1658 } else if ((start = tb->text)) {
1659 begin = end = 0;
1660 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1662 while (start) {
1663 mark = strchr(start, ' ');
1664 if (mark) {
1665 end += (int)(mark-start)+1;
1666 start = mark+1;
1667 } else {
1668 end += strlen(start);
1669 start = mark;
1672 if (end > tb->used)
1673 end = tb->used;
1675 if (end-begin > 0) {
1677 width = WMWidthOfString(font,
1678 &tb->text[begin], end-begin);
1680 /* if it won't fit, char wrap it */
1681 if (width >= tPtr->visible.w) {
1682 char *t = &tb->text[begin];
1683 int l=end-begin, i=0;
1684 do {
1685 width = WMWidthOfString(font, t, ++i);
1686 } while (width < tPtr->visible.w && i < l);
1687 if(i>2) i--;
1688 end = begin+i;
1689 start = &tb->text[end];
1692 lw += width;
1695 if (lw >= tPtr->visible.w - x) {
1696 y += layOutLine(tPtr, items, nitems, x, y);
1697 lw = width;
1698 x = bmargin;
1699 nitems = 0;
1702 if(nitems + 1 > itemsSize) {
1703 items = wrealloc(items,
1704 (++itemsSize)*sizeof(myLineItems));
1707 items[nitems].tb = tb;
1708 items[nitems].begin = begin;
1709 items[nitems].end = end;
1710 nitems++;
1712 begin = end;
1717 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1718 if(0&&tPtr->flags.laidOut
1719 && tb->next && tb->next->sections && tb->next->nsections>0
1720 && (tPtr->vpos + tPtr->visible.h
1721 < tb->next->sections[0]._y)) {
1722 if(tPtr->lastTextBlock->sections
1723 && tPtr->lastTextBlock->nsections > 0 ) {
1724 TextBlock *ltb = tPtr->lastTextBlock;
1725 int ly = ltb->sections[ltb->nsections-1]._y;
1726 int lh = ltb->sections[ltb->nsections-1].h;
1727 int ss, sd;
1729 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1730 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1732 y += layOutLine(tPtr, items, nitems, x, y);
1733 ss= ly+lh-y;
1734 sd = tPtr->docHeight-y;
1736 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1737 y += tb->next->sections[0]._y-y;
1738 nitems = 0;
1739 printf("nitems%d\n", nitems);
1740 if(ss-sd!=0)
1741 y = tPtr->docHeight+ss-sd;
1743 break;
1744 } else {
1745 tPtr->flags.laidOut = False;
1749 tb = tb->next;
1753 if (nitems > 0)
1754 y += layOutLine(tPtr, items, nitems, x, y);
1756 if (tPtr->docHeight != y+10) {
1757 tPtr->docHeight = y+10;
1758 updateScrollers(tPtr);
1761 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1762 XEvent event;
1764 tPtr->flags.horizOnDemand = True;
1765 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1766 event.type = Expose;
1767 handleEvents(&event, (void *)tPtr);
1769 } else if(tPtr->docWidth <= tPtr->visible.w
1770 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1771 tPtr->flags.horizOnDemand = False;
1772 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1775 tPtr->flags.laidOut = True;
1777 if(items && itemsSize > 0)
1778 wfree(items);
1783 static void
1784 textDidResize(W_ViewDelegate *self, WMView *view)
1786 Text *tPtr = (Text *)view->self;
1787 unsigned short w = tPtr->view->size.width;
1788 unsigned short h = tPtr->view->size.height;
1789 unsigned short rh = 0, vw = 0, rel;
1791 rel = (tPtr->flags.relief == WRFlat);
1793 if (tPtr->ruler && tPtr->flags.rulerShown) {
1794 WMMoveWidget(tPtr->ruler, 2, 2);
1795 WMResizeWidget(tPtr->ruler, w - 4, 40);
1796 rh = 40;
1799 if (tPtr->vS) {
1800 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1801 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1802 vw = 20;
1803 WMSetRulerOffset(tPtr->ruler,22);
1804 } else WMSetRulerOffset(tPtr->ruler, 2);
1806 if (tPtr->hS) {
1807 if (tPtr->vS) {
1808 WMMoveWidget(tPtr->hS, vw, h - 21);
1809 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1810 } else {
1811 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1812 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1816 tPtr->visible.x = (tPtr->vS)?24:4;
1817 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1818 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1819 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1820 tPtr->visible.h -= (tPtr->hS)?20:0;
1821 tPtr->margins[0].right = tPtr->visible.w;
1823 if (tPtr->view->flags.realized) {
1825 if (tPtr->db) {
1826 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1827 tPtr->db = (Pixmap) NULL;
1830 if (tPtr->visible.w < 40)
1831 tPtr->visible.w = 40;
1832 if (tPtr->visible.h < 20)
1833 tPtr->visible.h = 20;
1835 if(!tPtr->db) {
1836 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1837 tPtr->view->window, tPtr->visible.w,
1838 tPtr->visible.h, tPtr->view->screen->depth);
1842 WMThawText(tPtr);
1845 W_ViewDelegate _TextViewDelegate =
1847 NULL,
1848 NULL,
1849 textDidResize,
1850 NULL,
1853 #define TEXT_BUFFER_INCR 8
1854 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1856 static void
1857 clearText(Text *tPtr)
1859 tPtr->vpos = tPtr->hpos = 0;
1860 tPtr->docHeight = tPtr->docWidth = 0;
1861 tPtr->cursor.x = -23;
1863 if (!tPtr->firstTextBlock)
1864 return;
1866 while (tPtr->currentTextBlock)
1867 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1869 tPtr->firstTextBlock = NULL;
1870 tPtr->currentTextBlock = NULL;
1871 tPtr->lastTextBlock = NULL;
1872 WMEmptyArray(tPtr->gfxItems);
1875 /* possibly remove a single character from the currentTextBlock,
1876 or if there's a selection, remove it...
1877 note that Delete and Backspace are treated differently */
1878 static void
1879 deleteTextInteractively(Text *tPtr, KeySym ksym)
1881 TextBlock *tb;
1882 Bool back = (Bool) (ksym == XK_BackSpace);
1883 Bool done = 1, wasFirst = 0;
1885 if (!tPtr->flags.editable)
1886 return;
1888 if ( !(tb = tPtr->currentTextBlock) )
1889 return;
1891 if (tPtr->flags.ownsSelection) {
1892 if(removeSelection(tPtr))
1893 layOutDocument(tPtr);
1894 return;
1897 wasFirst = tb->first;
1898 if (back && tPtr->tpos < 1) {
1899 if (tb->prior) {
1900 if(tb->prior->blank) {
1901 tPtr->currentTextBlock = tb->prior;
1902 WMRemoveTextBlock(tPtr);
1903 tPtr->currentTextBlock = tb;
1904 tb->first = True;
1905 layOutDocument(tPtr);
1906 return;
1907 } else {
1908 if(tb->blank) {
1909 TextBlock *prior = tb->prior;
1910 tPtr->currentTextBlock = tb;
1911 WMRemoveTextBlock(tPtr);
1912 tb = prior;
1913 } else {
1914 tb = tb->prior;
1917 if(tb->graphic)
1918 tPtr->tpos = 1;
1919 else
1920 tPtr->tpos = tb->used;
1922 tPtr->currentTextBlock = tb;
1923 done = 1;
1924 if(wasFirst) {
1925 if(tb->next)
1926 tb->next->first = False;
1927 layOutDocument(tPtr);
1928 return;
1934 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1935 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1936 if (back)
1937 tPtr->tpos--;
1938 memmove(&(tb->text[tPtr->tpos]),
1939 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1940 tb->used--;
1941 done = 0;
1944 /* if there are no characters left to back over in the textblock,
1945 but it still has characters to the right of the cursor: */
1946 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1947 || tb->graphic) {
1949 /* no more chars, and it's marked as blank? */
1950 if(tb->blank) {
1951 TextBlock *sibling = (back? tb->prior : tb->next);
1953 if(tb->used == 0 || tb->graphic)
1954 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1956 if (sibling) {
1957 tPtr->currentTextBlock = sibling;
1958 if(tb->graphic)
1959 tPtr->tpos = (back? 1 : 0);
1960 else
1961 tPtr->tpos = (back? sibling->used : 0);
1963 /* no more chars, so mark it as blank */
1964 } else if(tb->used == 0) {
1965 tb->blank = 1;
1966 } else if(tb->graphic) {
1967 Bool hasNext = (Bool)(tb->next);
1969 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1970 if(hasNext) {
1971 tPtr->tpos = 0;
1972 } else if(tPtr->currentTextBlock) {
1973 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1974 1 : tPtr->currentTextBlock->used);
1976 } else printf("DEBUG: unaccounted for... catch this!\n");
1979 layOutDocument(tPtr);
1983 static void
1984 insertTextInteractively(Text *tPtr, char *text, int len)
1986 TextBlock *tb;
1987 char *newline = NULL;
1989 if (!tPtr->flags.editable) {
1990 return;
1993 if (len < 1 || !text)
1994 return;
1997 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1998 return;
2001 if (tPtr->flags.ownsSelection)
2002 removeSelection(tPtr);
2005 if (tPtr->flags.ignoreNewLine) {
2006 int i;
2007 for(i=0; i<len; i++) {
2008 if (text[i] == '\n')
2009 text[i] = ' ';
2013 tb = tPtr->currentTextBlock;
2014 if (!tb || tb->graphic) {
2015 tPtr->tpos = 0;
2016 WMAppendTextStream(tPtr, text);
2017 layOutDocument(tPtr);
2018 return;
2021 if ((newline = strchr(text, '\n'))) {
2022 int nlen = (int)(newline-text);
2023 int s = tb->used - tPtr->tpos;
2025 if (!tb->blank && nlen>0) {
2026 char *save;
2028 if (s > 0) {
2029 save = wmalloc(s);
2030 memcpy(save, &tb->text[tPtr->tpos], s);
2031 tb->used -= (tb->used - tPtr->tpos);
2033 insertTextInteractively(tPtr, text, nlen);
2034 newline++;
2035 WMAppendTextStream(tPtr, newline);
2036 if (s>0) {
2037 insertTextInteractively(tPtr, save, s);
2038 wfree(save);
2040 } else {
2041 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2042 && !tb->graphic && tb->text) {
2044 unsigned short savePos = tPtr->tpos;
2045 void *ntb = WMCreateTextBlockWithText(
2046 tPtr, &tb->text[tPtr->tpos],
2047 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2049 if(tb->sections[0].end == tPtr->tpos)
2050 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2051 NULL, tb->d.font, tb->color, True, 0));
2053 tb->used = savePos;
2054 WMAppendTextBlock(tPtr, ntb);
2055 tPtr->tpos = 0;
2057 } else if (tPtr->tpos == tb->used) {
2058 if(tPtr->flags.indentNewLine) {
2059 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2060 " ", tb->d.font, tb->color, True, 4));
2061 tPtr->tpos = 4;
2062 } else {
2063 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2064 NULL, tb->d.font, tb->color, True, 0));
2065 tPtr->tpos = 0;
2067 } else if (tPtr->tpos == 0) {
2068 if(tPtr->flags.indentNewLine) {
2069 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2070 " ", tb->d.font, tb->color, True, 4));
2071 } else {
2072 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2073 NULL, tb->d.font, tb->color, True, 0));
2075 tPtr->tpos = 0;
2076 if(tPtr->currentTextBlock->next)
2077 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2080 } else {
2081 if (tb->used + len >= tb->allocated) {
2082 tb->allocated = reqBlockSize(tb->used+len);
2083 tb->text = wrealloc(tb->text, tb->allocated);
2086 if (tb->blank) {
2087 memcpy(tb->text, text, len);
2088 tb->used = len;
2089 tPtr->tpos = len;
2090 tb->text[tb->used] = 0;
2091 tb->blank = False;
2093 } else {
2094 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2095 tb->used-tPtr->tpos+1);
2096 memmove(&tb->text[tPtr->tpos], text, len);
2097 tb->used += len;
2098 tPtr->tpos += len;
2099 tb->text[tb->used] = 0;
2104 layOutDocument(tPtr);
2108 static void
2109 selectRegion(Text *tPtr, int x, int y)
2112 if (x < 0 || y < 0)
2113 return;
2115 y += (tPtr->flags.rulerShown? 40: 0);
2116 y += tPtr->vpos;
2117 if (y>10)
2118 y -= 10; /* the original offset */
2120 x -= tPtr->visible.x-2;
2121 if (x<0)
2122 x=0;
2124 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2125 tPtr->sel.w = abs(tPtr->clicked.x - x);
2126 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2127 tPtr->sel.h = abs(tPtr->clicked.y - y);
2129 tPtr->flags.ownsSelection = True;
2130 paintText(tPtr);
2134 static void
2135 releaseSelection(Text *tPtr)
2137 TextBlock *tb = tPtr->firstTextBlock;
2139 while(tb) {
2140 tb->selected = False;
2141 tb = tb->next;
2143 tPtr->flags.ownsSelection = False;
2144 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2145 CurrentTime);
2147 paintText(tPtr);
2151 WMData*
2152 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2153 Atom *type)
2155 Text *tPtr = view->self;
2156 Display *dpy = tPtr->view->screen->display;
2157 Atom _TARGETS;
2158 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2159 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2160 WMData *data = NULL;
2163 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2164 char *text = WMGetTextSelectedStream(tPtr);
2166 if (text) {
2167 data = WMCreateDataWithBytes(text, strlen(text));
2168 WMSetDataFormat(data, TYPETEXT);
2170 *type = target;
2171 return data;
2172 } else printf("didn't get it\n");
2174 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2175 if (target == _TARGETS) {
2176 Atom *ptr;
2178 ptr = wmalloc(4 * sizeof(Atom));
2179 ptr[0] = _TARGETS;
2180 ptr[1] = XA_STRING;
2181 ptr[2] = TEXT;
2182 ptr[3] = COMPOUND_TEXT;
2184 data = WMCreateDataWithBytes(ptr, 4*4);
2185 WMSetDataFormat(data, 32);
2187 *type = target;
2188 return data;
2191 return NULL;
2195 static void
2196 lostHandler(WMView *view, Atom selection, void *cdata)
2198 releaseSelection((WMText *)view->self);
2202 static WMSelectionProcs selectionHandler = {
2203 requestHandler, lostHandler, NULL
2207 static void
2208 ownershipObserver(void *observerData, WMNotification *notification)
2210 if (observerData != WMGetNotificationClientData(notification))
2211 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2215 static void
2216 autoSelectText(Text *tPtr, int clicks)
2218 int x, start;
2219 TextBlock *tb;
2220 char *mark = NULL, behind, ahead;
2222 if(!(tb = tPtr->currentTextBlock))
2223 return;
2225 if(clicks == 2) {
2228 switch(tb->text[tPtr->tpos]) {
2229 case ' ': return;
2231 case '<': case '>': behind = '<'; ahead = '>'; break;
2232 case '{': case '}': behind = '{'; ahead = '}'; break;
2233 case '[': case ']': behind = '['; ahead = ']'; break;
2235 default: behind = ahead = ' ';
2238 tPtr->sel.y = tPtr->cursor.y+5;
2239 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2241 if(tb->graphic) {
2242 tPtr->sel.x = tb->sections[0].x;
2243 tPtr->sel.w = tb->sections[0].w;
2244 } else {
2245 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2247 start = tPtr->tpos;
2248 while(start > 0 && tb->text[start-1] != behind)
2249 start--;
2251 x = tPtr->cursor.x;
2252 if(tPtr->tpos > start){
2253 x -= WMWidthOfString(font, &tb->text[start],
2254 tPtr->tpos - start);
2256 tPtr->sel.x = (x<0?0:x)+1;
2258 if((mark = strchr(&tb->text[start], ahead))) {
2259 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2260 (int)(mark - &tb->text[start]));
2261 } else if(tb->used > start) {
2262 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2263 tb->used - start);
2267 } else if(clicks == 3) {
2268 TextBlock *cur = tb;
2270 while(tb && !tb->first) {
2271 tb = tb->prior;
2273 tPtr->sel.y = tb->sections[0]._y;
2275 tb = cur;
2276 while(tb->next && !tb->next->first) {
2277 tb = tb->next;
2279 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2280 + 5 - tPtr->sel.y;
2282 tPtr->sel.x = 0;
2283 tPtr->sel.w = tPtr->docWidth;
2284 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2287 if (!tPtr->flags.ownsSelection) {
2288 WMCreateSelectionHandler(tPtr->view,
2289 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2290 tPtr->flags.ownsSelection = True;
2292 paintText(tPtr);
2297 static void
2298 fontChanged(void *observerData, WMNotification *notification)
2300 WMText *tPtr = (WMText *) observerData;
2301 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2302 printf("fontChanged\n");
2304 if(!tPtr || !font)
2305 return;
2307 if (tPtr->flags.ownsSelection)
2308 WMSetTextSelectionFont(tPtr, font);
2312 static void
2313 handleTextKeyPress(Text *tPtr, XEvent *event)
2315 char buffer[64];
2316 KeySym ksym;
2317 int control_pressed = False;
2318 TextBlock *tb = NULL;
2320 if (((XKeyEvent *) event)->state & ControlMask)
2321 control_pressed = True;
2322 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2324 switch(ksym) {
2326 case XK_Home:
2327 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2328 tPtr->tpos = 0;
2329 updateCursorPosition(tPtr);
2330 paintText(tPtr);
2331 break;
2333 case XK_End:
2334 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2335 if(tPtr->currentTextBlock->graphic)
2336 tPtr->tpos = 1;
2337 else
2338 tPtr->tpos = tPtr->currentTextBlock->used;
2340 updateCursorPosition(tPtr);
2341 paintText(tPtr);
2342 break;
2344 case XK_Left:
2345 if(!(tb = tPtr->currentTextBlock))
2346 break;
2347 if(tb->graphic)
2348 goto L_imaGFX;
2350 if(tPtr->tpos==0) {
2351 L_imaGFX:
2352 if(tb->prior) {
2353 tPtr->currentTextBlock = tb->prior;
2354 if(tPtr->currentTextBlock->graphic)
2355 tPtr->tpos = 1;
2356 else
2357 tPtr->tpos = tPtr->currentTextBlock->used;
2359 if(!tb->first && tPtr->tpos > 0)
2360 tPtr->tpos--;
2361 } else tPtr->tpos = 0;
2362 } else tPtr->tpos--;
2363 updateCursorPosition(tPtr);
2364 paintText(tPtr);
2365 break;
2367 case XK_Right:
2368 if(!(tb = tPtr->currentTextBlock))
2369 break;
2370 if(tb->graphic)
2371 goto R_imaGFX;
2372 if(tPtr->tpos == tb->used) {
2373 R_imaGFX:
2374 if(tb->next) {
2375 tPtr->currentTextBlock = tb->next;
2376 tPtr->tpos = 0;
2377 if(!tb->next->first && tb->next->used>0)
2378 tPtr->tpos++;
2379 } else {
2380 if(tb->graphic)
2381 tPtr->tpos = 1;
2382 else
2383 tPtr->tpos = tb->used;
2385 } else tPtr->tpos++;
2386 updateCursorPosition(tPtr);
2387 paintText(tPtr);
2388 break;
2390 case XK_Down:
2391 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2392 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2393 paintText(tPtr);
2394 break;
2396 case XK_Up:
2397 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2398 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2399 paintText(tPtr);
2400 break;
2402 case XK_BackSpace:
2403 case XK_Delete:
2404 #ifdef XK_KP_Delete
2405 case XK_KP_Delete:
2406 #endif
2407 deleteTextInteractively(tPtr, ksym);
2408 updateCursorPosition(tPtr);
2409 paintText(tPtr);
2410 break;
2412 case XK_Control_R :
2413 case XK_Control_L :
2414 control_pressed = True;
2415 break;
2417 case XK_Tab:
2418 insertTextInteractively(tPtr, " ", 4);
2419 updateCursorPosition(tPtr);
2420 paintText(tPtr);
2421 break;
2423 case XK_Return:
2424 *buffer = '\n';
2425 default:
2426 if (*buffer != 0 && !control_pressed) {
2427 insertTextInteractively(tPtr, buffer, strlen(buffer));
2428 updateCursorPosition(tPtr);
2429 paintText(tPtr);
2431 } else if (control_pressed && ksym==XK_r) {
2432 Bool i = !tPtr->flags.rulerShown;
2433 WMShowTextRuler(tPtr, i);
2434 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);
2442 if (!control_pressed && tPtr->flags.ownsSelection) {
2443 releaseSelection(tPtr);
2448 static void
2449 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2450 void *cdata, WMData *data)
2452 Text *tPtr = (Text *)view->self;
2453 char *text;
2455 tPtr->flags.waitingForSelection = 0;
2457 if (data) {
2458 text = (char*)WMDataBytes(data);
2460 if (tPtr->parser) {
2461 (tPtr->parser) (tPtr, (void *) text);
2462 layOutDocument(tPtr);
2463 } else insertTextInteractively(tPtr, text, strlen(text));
2464 updateCursorPosition(tPtr);
2465 paintText(tPtr);
2467 } else {
2468 int n;
2470 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2472 if (text) {
2473 text[n] = 0;
2474 if (tPtr->parser) {
2475 (tPtr->parser) (tPtr, (void *) text);
2476 layOutDocument(tPtr);
2477 } else insertTextInteractively(tPtr, text, n);
2478 updateCursorPosition(tPtr);
2479 paintText(tPtr);
2481 XFree(text);
2489 static void
2490 handleActionEvents(XEvent *event, void *data)
2492 Text *tPtr = (Text *)data;
2493 Display *dpy = event->xany.display;
2494 KeySym ksym;
2497 switch (event->type) {
2498 case KeyPress:
2499 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2500 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2501 tPtr->flags.extendSelection = True;
2502 return;
2505 if (tPtr->flags.focused) {
2506 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2507 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2508 GrabModeAsync, GrabModeAsync, None,
2509 tPtr->view->screen->invisibleCursor, CurrentTime);
2510 tPtr->flags.pointerGrabbed = True;
2511 handleTextKeyPress(tPtr, event);
2513 } break;
2515 case KeyRelease:
2516 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2517 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2518 tPtr->flags.extendSelection = False;
2519 return;
2520 /* end modify flag so selection can be extended */
2522 break;
2525 case MotionNotify:
2527 if (tPtr->flags.pointerGrabbed) {
2528 tPtr->flags.pointerGrabbed = False;
2529 XUngrabPointer(dpy, CurrentTime);
2532 if(tPtr->flags.waitingForSelection)
2533 break;
2535 if ((event->xmotion.state & Button1Mask)) {
2536 TextBlock *tb = tPtr->currentTextBlock;
2538 if(tb && tPtr->flags.isOverGraphic &&
2539 tb->graphic && !tb->object) {
2540 WMSize offs;
2541 WMPixmap *pixmap = tb->d.pixmap;
2542 char *types[2] = {"application/X-image", NULL};
2544 offs.width = 2;
2545 offs.height = 2;
2547 WMDragImageFromView(tPtr->view, pixmap, types,
2548 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2549 offs, event, True);
2552 } else {
2553 if (!tPtr->flags.ownsSelection) {
2554 WMCreateSelectionHandler(tPtr->view,
2555 XA_PRIMARY, event->xbutton.time,
2556 &selectionHandler, NULL);
2557 tPtr->flags.ownsSelection = True;
2560 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2561 break;
2564 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2565 break;
2568 case ButtonPress:
2570 if (tPtr->flags.pointerGrabbed) {
2571 tPtr->flags.pointerGrabbed = False;
2572 XUngrabPointer(dpy, CurrentTime);
2573 break;
2576 if (tPtr->flags.waitingForSelection)
2577 break;
2579 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2580 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2581 return;
2584 if (tPtr->flags.ownsSelection)
2585 releaseSelection(tPtr);
2588 if (event->xbutton.button == Button1) {
2590 if(WMIsDoubleClick(event)) {
2591 TextBlock *tb = tPtr->currentTextBlock;
2593 tPtr->lastClickTime = event->xbutton.time;
2594 if(tb && tb->graphic && !tb->object) {
2595 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2596 char *desc;
2598 desc = wmalloc(tb->used+1);
2599 memcpy(desc, tb->text, tb->used);
2600 desc[tb->used] = 0;
2601 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2602 wfree(desc);
2604 } else {
2605 autoSelectText(tPtr, 2);
2607 break;
2608 } else if(event->xbutton.time - tPtr->lastClickTime
2609 < WINGsConfiguration.doubleClickDelay) {
2610 tPtr->lastClickTime = event->xbutton.time;
2611 autoSelectText(tPtr, 3);
2612 break;
2615 if (!tPtr->flags.focused) {
2616 WMSetFocusToWidget(tPtr);
2617 tPtr->flags.focused = True;
2620 tPtr->lastClickTime = event->xbutton.time;
2621 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2622 paintText(tPtr);
2625 if (event->xbutton.button
2626 == WINGsConfiguration.mouseWheelDown) {
2627 WMScrollText(tPtr, 16);
2628 break;
2631 if (event->xbutton.button
2632 == WINGsConfiguration.mouseWheelUp) {
2633 WMScrollText(tPtr, -16);
2634 break;
2637 if (event->xbutton.button == Button2) {
2638 char *text = NULL;
2639 int n;
2641 if (!tPtr->flags.editable) {
2642 XBell(dpy, 0);
2643 break;
2646 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2647 event->xbutton.time, pasteText, NULL)) {
2649 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2650 tPtr->flags.waitingForSelection = 0;
2652 if (text) {
2653 text[n] = 0;
2655 if (tPtr->parser) {
2656 (tPtr->parser) (tPtr, (void *) text);
2657 layOutDocument(tPtr);
2659 else
2660 insertTextInteractively(tPtr, text, n);
2662 XFree(text);
2663 #if 0
2664 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2665 (void*)WMInsertTextEvent);
2666 #endif
2667 updateCursorPosition(tPtr);
2668 paintText(tPtr);
2670 } else {
2671 tPtr->flags.waitingForSelection = True;
2674 break;
2678 case ButtonRelease:
2679 if (tPtr->flags.pointerGrabbed) {
2680 tPtr->flags.pointerGrabbed = False;
2681 XUngrabPointer(dpy, CurrentTime);
2682 break;
2685 if (tPtr->flags.waitingForSelection)
2686 break;
2692 static void
2693 handleEvents(XEvent *event, void *data)
2695 Text *tPtr = (Text *)data;
2697 switch(event->type) {
2698 case Expose:
2700 if (event->xexpose.count!=0)
2701 break;
2703 if(tPtr->hS) {
2704 if (!(W_VIEW(tPtr->hS))->flags.realized)
2705 WMRealizeWidget(tPtr->hS);
2708 if(tPtr->vS) {
2709 if (!(W_VIEW(tPtr->vS))->flags.realized)
2710 WMRealizeWidget(tPtr->vS);
2713 if(tPtr->ruler) {
2714 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2715 WMRealizeWidget(tPtr->ruler);
2719 if(!tPtr->db)
2720 textDidResize(tPtr->view->delegate, tPtr->view);
2722 paintText(tPtr);
2723 break;
2725 case FocusIn:
2726 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2727 != tPtr->view)
2728 return;
2729 tPtr->flags.focused = True;
2730 #if DO_BLINK
2731 if (tPtr->flags.editable && !tPtr->timerID) {
2732 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2733 blinkCursor, tPtr);
2735 #endif
2737 break;
2739 case FocusOut:
2740 tPtr->flags.focused = False;
2741 paintText(tPtr);
2742 #if DO_BLINK
2743 if (tPtr->timerID) {
2744 WMDeleteTimerHandler(tPtr->timerID);
2745 tPtr->timerID = NULL;
2747 #endif
2748 break;
2751 case DestroyNotify:
2752 clearText(tPtr);
2753 if(tPtr->db)
2754 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2755 if(tPtr->gfxItems)
2756 WMEmptyArray(tPtr->gfxItems);
2757 #if DO_BLINK
2758 if (tPtr->timerID)
2759 WMDeleteTimerHandler(tPtr->timerID);
2760 #endif
2761 WMReleaseFont(tPtr->dFont);
2762 WMReleaseColor(tPtr->dColor);
2763 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2764 WMRemoveNotificationObserver(tPtr);
2766 wfree(tPtr);
2768 break;
2774 static void
2775 insertPlainText(Text *tPtr, char *text)
2777 char *start, *mark;
2778 void *tb = NULL;
2780 start = text;
2781 while (start) {
2782 mark = strchr(start, '\n');
2783 if (mark) {
2784 tb = WMCreateTextBlockWithText(tPtr,
2785 start, tPtr->dFont,
2786 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2787 start = mark+1;
2788 tPtr->flags.first = True;
2789 } else {
2790 if (start && strlen(start)) {
2791 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2792 tPtr->dColor, tPtr->flags.first, strlen(start));
2793 } else tb = NULL;
2794 tPtr->flags.first = False;
2795 start = mark;
2798 if (tPtr->flags.prepend)
2799 WMPrependTextBlock(tPtr, tb);
2800 else
2801 WMAppendTextBlock(tPtr, tb);
2806 static void
2807 rulerMoveCallBack(WMWidget *w, void *self)
2809 Text *tPtr = (Text *)self;
2811 if (!tPtr)
2812 return;
2813 if (W_CLASS(tPtr) != WC_Text)
2814 return;
2816 paintText(tPtr);
2820 static void
2821 rulerReleaseCallBack(WMWidget *w, void *self)
2823 Text *tPtr = (Text *)self;
2825 if (!tPtr)
2826 return;
2827 if (W_CLASS(tPtr) != WC_Text)
2828 return;
2830 WMThawText(tPtr);
2831 return;
2834 static unsigned
2835 draggingSourceOperation(WMView *self, Bool local)
2837 return WDOperationCopy;
2840 static WMData*
2841 fetchDragData(WMView *self, char *type)
2843 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2844 char *desc;
2845 WMData *data;
2847 if (!tb)
2848 return NULL;
2850 printf("type is [%s]\n", type);
2851 desc = wmalloc(tb->used+1);
2852 memcpy(desc, tb->text, tb->used);
2853 desc[tb->used] = 0;
2854 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2856 wfree(desc);
2858 return data;
2862 static WMDragSourceProcs _DragSourceProcs = {
2863 draggingSourceOperation,
2864 NULL,
2865 NULL,
2866 fetchDragData
2870 static unsigned
2871 draggingEntered(WMView *self, WMDraggingInfo *info)
2873 printf("draggingEntered\n");
2874 return WDOperationCopy;
2878 static unsigned
2879 draggingUpdated(WMView *self, WMDraggingInfo *info)
2881 return WDOperationCopy;
2885 static void
2886 draggingExited(WMView *self, WMDraggingInfo *info)
2888 printf("draggingExited\n");
2891 static Bool
2892 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2894 printf("prepareForDragOperation\n");
2895 return True;
2899 char *badbadbad;
2901 static void
2902 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2903 void *cdata, WMData *data)
2905 badbadbad = wstrdup((char *)WMDataBytes(data));
2909 /* when it's done in WINGs, remove this */
2911 Bool
2912 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2914 WMScreen *scr = W_VIEW_SCREEN(view);
2916 if (!WMRequestSelection(scr->dragInfo.destView,
2917 scr->xdndSelectionAtom,
2918 XInternAtom(scr->display, type, False),
2919 scr->dragInfo.timestamp,
2920 receivedData, &scr->dragInfo)) {
2921 wwarning("could not request data for dropped data");
2925 XEvent ev;
2927 ev.type = ClientMessage;
2928 ev.xclient.message_type = scr->xdndFinishedAtom;
2929 ev.xclient.format = 32;
2930 ev.xclient.window = info->destinationWindow;
2931 ev.xclient.data.l[0] = 0;
2932 ev.xclient.data.l[1] = 0;
2933 ev.xclient.data.l[2] = 0;
2934 ev.xclient.data.l[3] = 0;
2935 ev.xclient.data.l[4] = 0;
2937 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2938 XFlush(scr->display);
2940 return True;
2943 static Bool
2944 performDragOperation(WMView *self, WMDraggingInfo *info)
2946 WMColor *color;
2947 WMText *tPtr = (WMText *)self->self;
2949 if (!tPtr)
2950 return True;
2952 requestDroppedData(tPtr->view, info, "application/X-color");
2953 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2954 if(color) {
2955 WMSetTextSelectionColor(tPtr, color);
2956 WMReleaseColor(color);
2961 return True;
2964 static void
2965 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2967 printf("concludeDragOperation\n");
2971 static WMDragDestinationProcs _DragDestinationProcs = {
2972 draggingEntered,
2973 draggingUpdated,
2974 draggingExited,
2975 prepareForDragOperation,
2976 performDragOperation,
2977 concludeDragOperation
2981 char *
2982 getStream(WMText *tPtr, int sel, int array)
2984 TextBlock *tb = NULL;
2985 char *text = NULL;
2986 unsigned long where = 0;
2988 if (!tPtr)
2989 return NULL;
2991 if (!(tb = tPtr->firstTextBlock))
2992 return NULL;
2994 if (tPtr->writer) {
2995 (tPtr->writer) (tPtr, (void *) text);
2996 return text;
2999 tb = tPtr->firstTextBlock;
3000 while (tb) {
3002 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
3004 if (!sel || (tb->graphic && tb->selected)) {
3006 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3007 && tb != tPtr->firstTextBlock) {
3008 text = wrealloc(text, where+1);
3009 text[where++] = '\n';
3012 if(tb->blank)
3013 goto _gSnext;
3015 if(tb->graphic && array) {
3016 text = wrealloc(text, where+4);
3017 text[where++] = 0xFA;
3018 text[where++] = (tb->used>>8)&0x0ff;
3019 text[where++] = tb->used&0x0ff;
3020 text[where++] = tb->allocated; /* extra info */
3022 text = wrealloc(text, where+tb->used);
3023 memcpy(&text[where], tb->text, tb->used);
3024 where += tb->used;
3027 } else if (sel && tb->selected) {
3029 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3030 text = wrealloc(text, where+1);
3031 text[where++] = '\n';
3034 if(tb->blank)
3035 goto _gSnext;
3037 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3038 memcpy(&text[where], &tb->text[tb->s_begin],
3039 tb->s_end - tb->s_begin);
3040 where += tb->s_end - tb->s_begin;
3045 _gSnext:tb = tb->next;
3048 /* +1 for the end of string, let's be nice */
3049 text = wrealloc(text, where+1);
3050 text[where] = 0;
3051 return text;
3055 static void
3056 releaseStreamObjects(void *data)
3058 if(data)
3059 wfree(data);
3062 WMArray *
3063 getStreamObjects(WMText *tPtr, int sel)
3065 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3066 WMData *data;
3067 char *stream;
3068 unsigned short len;
3069 char *start, *fa, *desc;
3071 stream = getStream(tPtr, sel, 1);
3072 if(!stream)
3073 return NULL;
3075 start = stream;
3076 while (start) {
3078 fa = strchr(start, 0xFA);
3079 if (fa) {
3080 if((int)(fa - start)>0) {
3081 desc = start;
3082 desc[(int)(fa - start)] = 0;
3083 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3084 WMSetDataFormat(data, TYPETEXT);
3085 WMAddToArray(array, (void *) data);
3088 len = *(fa+1)*0xff + *(fa+2);
3089 data = WMCreateDataWithBytes((void *)(fa+4), len);
3090 WMSetDataFormat(data, *(fa+3));
3091 WMAddToArray(array, (void *) data);
3092 start = fa + len + 4;
3094 } else {
3095 if (start && strlen(start)) {
3096 data = WMCreateDataWithBytes((void *)start, strlen(start));
3097 WMSetDataFormat(data, TYPETEXT);
3098 WMAddToArray(array, (void *) data);
3100 start = fa;
3104 wfree(stream);
3105 return array;
3109 WMText *
3110 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3112 Text *tPtr;
3113 Display *dpy;
3114 WMScreen *scr;
3115 XGCValues gcv;
3117 tPtr = wmalloc(sizeof(Text));
3118 memset(tPtr, 0, sizeof(Text));
3119 tPtr->widgetClass = WC_Text;
3120 tPtr->view = W_CreateView(W_VIEW(parent));
3121 if (!tPtr->view) {
3122 perror("could not create text's view\n");
3123 wfree(tPtr);
3124 return NULL;
3127 dpy = tPtr->view->screen->display;
3128 scr = tPtr->view->screen;
3130 tPtr->view->self = tPtr;
3131 tPtr->view->attribs.cursor = scr->textCursor;
3132 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3133 W_ResizeView(tPtr->view, 250, 200);
3135 tPtr->dColor = WMBlackColor(scr);
3136 tPtr->fgColor = WMBlackColor(scr);
3137 tPtr->bgColor = WMWhiteColor(scr);
3138 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3140 gcv.graphics_exposures = False;
3141 gcv.foreground = W_PIXEL(scr->gray);
3142 gcv.background = W_PIXEL(scr->darkGray);
3143 gcv.fill_style = FillStippled;
3144 /* why not use scr->stipple here? */
3145 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS,
3146 STIPPLE_WIDTH, STIPPLE_HEIGHT);
3147 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3148 GCForeground|GCBackground|GCStipple
3149 |GCFillStyle|GCGraphicsExposures, &gcv);
3151 tPtr->ruler = NULL;
3152 tPtr->vS = NULL;
3153 tPtr->hS = NULL;
3155 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3157 tPtr->view->delegate = &_TextViewDelegate;
3159 tPtr->delegate = NULL;
3161 #if DO_BLINK
3162 tPtr->timerID = NULL;
3163 #endif
3165 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3166 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3167 handleEvents, tPtr);
3169 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3170 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3171 handleActionEvents, tPtr);
3173 WMAddNotificationObserver(ownershipObserver, tPtr,
3174 WMSelectionOwnerDidChangeNotification,
3175 tPtr);
3177 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3178 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3182 char *types[3] = {"application/X-color", "application/X-image", NULL};
3183 WMRegisterViewForDraggedTypes(tPtr->view, types);
3186 /*WMAddNotificationObserver(fontChanged, tPtr,
3187 WMFontPanelDidChangeNotification, tPtr);*/
3189 tPtr->firstTextBlock = NULL;
3190 tPtr->lastTextBlock = NULL;
3191 tPtr->currentTextBlock = NULL;
3192 tPtr->tpos = 0;
3194 tPtr->gfxItems = WMCreateArray(4);
3196 tPtr->parser = parser;
3197 tPtr->writer = writer;
3199 tPtr->sel.x = tPtr->sel.y = 2;
3200 tPtr->sel.w = tPtr->sel.h = 0;
3202 tPtr->clicked.x = tPtr->clicked.y = 2;
3204 tPtr->visible.x = tPtr->visible.y = 2;
3205 tPtr->visible.h = tPtr->view->size.height;
3206 tPtr->visible.w = tPtr->view->size.width - 4;
3208 tPtr->cursor.x = -23;
3210 tPtr->docWidth = 0;
3211 tPtr->docHeight = 0;
3212 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3213 default_bullet);
3214 tPtr->db = (Pixmap) NULL;
3215 tPtr->bgPixmap = NULL;
3217 tPtr->margins = WMGetRulerMargins(NULL);
3218 tPtr->margins->right = tPtr->visible.w;
3219 tPtr->nMargins = 1;
3221 tPtr->flags.rulerShown = False;
3222 tPtr->flags.monoFont = False;
3223 tPtr->flags.focused = False;
3224 tPtr->flags.editable = True;
3225 tPtr->flags.ownsSelection = False;
3226 tPtr->flags.pointerGrabbed = False;
3227 tPtr->flags.extendSelection = False;
3228 tPtr->flags.frozen = False;
3229 tPtr->flags.cursorShown = True;
3230 tPtr->flags.acceptsGraphic = False;
3231 tPtr->flags.horizOnDemand = False;
3232 tPtr->flags.needsLayOut = False;
3233 tPtr->flags.ignoreNewLine = False;
3234 tPtr->flags.indentNewLine = False;
3235 tPtr->flags.laidOut = False;
3236 tPtr->flags.ownsSelection = False;
3237 tPtr->flags.waitingForSelection = False;
3238 tPtr->flags.prepend = False;
3239 tPtr->flags.isOverGraphic = False;
3240 tPtr->flags.relief = WRSunken;
3241 tPtr->flags.isOverGraphic = 0;
3242 tPtr->flags.alignment = WALeft;
3243 tPtr->flags.first = True;
3245 return tPtr;
3248 void
3249 WMPrependTextStream(WMText *tPtr, char *text)
3251 CHECK_CLASS(tPtr, WC_Text);
3253 if(!text) {
3254 if (tPtr->flags.ownsSelection)
3255 releaseSelection(tPtr);
3256 clearText(tPtr);
3257 updateScrollers(tPtr);
3258 return;
3261 tPtr->flags.prepend = True;
3262 if (text && tPtr->parser)
3263 (tPtr->parser) (tPtr, (void *) text);
3264 else
3265 insertPlainText(tPtr, text);
3267 tPtr->flags.needsLayOut = True;
3268 tPtr->tpos = 0;
3269 if(!tPtr->flags.frozen) {
3270 layOutDocument(tPtr);
3275 void
3276 WMAppendTextStream(WMText *tPtr, char *text)
3278 CHECK_CLASS(tPtr, WC_Text);
3280 if(!text) {
3281 if (tPtr->flags.ownsSelection)
3282 releaseSelection(tPtr);
3283 clearText(tPtr);
3284 updateScrollers(tPtr);
3285 return;
3288 tPtr->flags.prepend = False;
3289 if (text && tPtr->parser)
3290 (tPtr->parser) (tPtr, (void *) text);
3291 else
3292 insertPlainText(tPtr, text);
3294 tPtr->flags.needsLayOut = True;
3295 if(tPtr->currentTextBlock) {
3296 if(tPtr->currentTextBlock->graphic)
3297 tPtr->tpos = 1;
3298 else
3299 tPtr->tpos = tPtr->currentTextBlock->used;
3302 if(!tPtr->flags.frozen) {
3303 layOutDocument(tPtr);
3308 char*
3309 WMGetTextStream(WMText *tPtr)
3311 CHECK_CLASS(tPtr, WC_Text);
3313 return getStream(tPtr, 0, 0);
3317 char*
3318 WMGetTextSelectedStream(WMText *tPtr)
3320 CHECK_CLASS(tPtr, WC_Text);
3322 return getStream(tPtr, 1, 0);
3326 WMArray*
3327 WMGetTextObjects(WMText *tPtr)
3329 CHECK_CLASS(tPtr, WC_Text);
3331 return getStreamObjects(tPtr, 0);
3334 WMArray*
3335 WMGetTextSelectedObjects(WMText *tPtr)
3337 CHECK_CLASS(tPtr, WC_Text);
3339 return getStreamObjects(tPtr, 1);
3343 void
3344 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3346 CHECK_CLASS(tPtr, WC_Text);
3348 tPtr->delegate = delegate;
3352 void*
3353 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3354 char *description, WMColor *color,
3355 unsigned short first, unsigned short extraInfo)
3357 TextBlock *tb;
3359 if (!w || !description || !color)
3360 return NULL;
3362 tb = wmalloc(sizeof(TextBlock));
3364 tb->text = wstrdup(description);
3365 tb->used = strlen(description);
3366 tb->blank = False;
3367 tb->d.widget = w;
3368 tb->color = WMRetainColor(color);
3369 tb->marginN = newMargin(tPtr, NULL);
3370 tb->allocated = extraInfo;
3371 tb->first = first;
3372 tb->kanji = False;
3373 tb->graphic = True;
3374 tb->object = True;
3375 tb->underlined = False;
3376 tb->selected = False;
3377 tb->script = 0;
3378 tb->sections = NULL;
3379 tb->nsections = 0;
3380 tb->prior = NULL;
3381 tb->next = NULL;
3383 return tb;
3387 void*
3388 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3389 char *description, WMColor *color,
3390 unsigned short first, unsigned short extraInfo)
3392 TextBlock *tb;
3394 if (!p || !description || !color)
3395 return NULL;
3397 tb = wmalloc(sizeof(TextBlock));
3399 tb->text = wstrdup(description);
3400 tb->used = strlen(description);
3401 tb->blank = False;
3402 tb->d.pixmap = WMRetainPixmap(p);
3403 tb->color = WMRetainColor(color);
3404 tb->marginN = newMargin(tPtr, NULL);
3405 tb->allocated = extraInfo;
3406 tb->first = first;
3407 tb->kanji = False;
3408 tb->graphic = True;
3409 tb->object = False;
3410 tb->underlined = False;
3411 tb->selected = False;
3412 tb->script = 0;
3413 tb->sections = NULL;
3414 tb->nsections = 0;
3415 tb->prior = NULL;
3416 tb->next = NULL;
3418 return tb;
3422 void*
3423 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3424 unsigned short first, unsigned short len)
3426 TextBlock *tb;
3428 if (!font || !color)
3429 return NULL;
3431 tb = wmalloc(sizeof(TextBlock));
3433 tb->allocated = reqBlockSize(len);
3434 tb->text = (char *)wmalloc(tb->allocated);
3435 memset(tb->text, 0, tb->allocated);
3437 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3438 *tb->text = ' ';
3439 tb->used = 1;
3440 tb->blank = True;
3441 } else {
3442 memcpy(tb->text, text, len);
3443 tb->used = len;
3444 tb->blank = False;
3446 tb->text[tb->used] = 0;
3448 tb->d.font = WMRetainFont(font);
3449 tb->color = WMRetainColor(color);
3450 tb->marginN = newMargin(tPtr, NULL);
3451 tb->first = first;
3452 tb->kanji = False;
3453 tb->graphic = False;
3454 tb->underlined = False;
3455 tb->selected = False;
3456 tb->script = 0;
3457 tb->sections = NULL;
3458 tb->nsections = 0;
3459 tb->prior = NULL;
3460 tb->next = NULL;
3461 return tb;
3465 void
3466 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3467 unsigned int kanji, unsigned int underlined, int script,
3468 WMRulerMargins *margins)
3470 TextBlock *tb = (TextBlock *) vtb;
3471 if (!tb)
3472 return;
3474 tb->first = first;
3475 tb->kanji = kanji;
3476 tb->underlined = underlined;
3477 tb->script = script;
3478 tb->marginN = newMargin(tPtr, margins);
3482 void
3483 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3484 unsigned int *kanji, unsigned int *underlined, int *script,
3485 WMRulerMargins *margins)
3487 TextBlock *tb = (TextBlock *) vtb;
3488 if (!tb)
3489 return;
3491 if (first) *first = tb->first;
3492 if (kanji) *kanji = tb->kanji;
3493 if (underlined) *underlined = tb->underlined;
3494 if (script) *script = tb->script;
3495 if (margins) margins = &tPtr->margins[tb->marginN];
3499 void
3500 WMPrependTextBlock(WMText *tPtr, void *vtb)
3502 TextBlock *tb = (TextBlock *)vtb;
3504 if (!tb)
3505 return;
3507 if (tb->graphic) {
3508 if(tb->object) {
3509 WMWidget *w = tb->d.widget;
3510 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3511 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3512 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3515 WMAddToArray(tPtr->gfxItems, (void *)tb);
3516 tPtr->tpos = 1;
3518 } else {
3519 tPtr->tpos = tb->used;
3522 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3523 tb->next = tb->prior = NULL;
3524 tb->first = True;
3525 tPtr->lastTextBlock = tPtr->firstTextBlock
3526 = tPtr->currentTextBlock = tb;
3527 return;
3530 if(!tb->first) {
3531 tb->marginN = tPtr->currentTextBlock->marginN;
3534 tb->next = tPtr->currentTextBlock;
3535 tb->prior = tPtr->currentTextBlock->prior;
3536 if (tPtr->currentTextBlock->prior)
3537 tPtr->currentTextBlock->prior->next = tb;
3539 tPtr->currentTextBlock->prior = tb;
3540 if (!tb->prior)
3541 tPtr->firstTextBlock = tb;
3543 tPtr->currentTextBlock = tb;
3547 void
3548 WMAppendTextBlock(WMText *tPtr, void *vtb)
3550 TextBlock *tb = (TextBlock *)vtb;
3552 if (!tb)
3553 return;
3555 if (tb->graphic) {
3556 if(tb->object) {
3557 WMWidget *w = tb->d.widget;
3558 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3559 (W_VIEW(w))->attribs.cursor =
3560 tPtr->view->screen->defaultCursor;
3561 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3564 WMAddToArray(tPtr->gfxItems, (void *)tb);
3565 tPtr->tpos = 1;
3567 } else {
3568 tPtr->tpos = tb->used;
3572 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3573 tb->next = tb->prior = NULL;
3574 tb->first = True;
3575 tPtr->lastTextBlock = tPtr->firstTextBlock
3576 = tPtr->currentTextBlock = tb;
3577 return;
3580 if(!tb->first) {
3581 tb->marginN = tPtr->currentTextBlock->marginN;
3584 tb->next = tPtr->currentTextBlock->next;
3585 tb->prior = tPtr->currentTextBlock;
3586 if (tPtr->currentTextBlock->next)
3587 tPtr->currentTextBlock->next->prior = tb;
3589 tPtr->currentTextBlock->next = tb;
3591 if (!tb->next)
3592 tPtr->lastTextBlock = tb;
3594 tPtr->currentTextBlock = tb;
3598 void*
3599 WMRemoveTextBlock(WMText *tPtr)
3601 TextBlock *tb = NULL;
3603 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock ||
3604 !tPtr->currentTextBlock) {
3605 return NULL;
3608 tb = tPtr->currentTextBlock;
3609 if (tb->graphic) {
3610 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3612 if(tb->object) {
3613 WMUnmapWidget(tb->d.widget);
3617 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3618 if (tPtr->currentTextBlock->next)
3619 tPtr->currentTextBlock->next->prior = NULL;
3621 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3622 tPtr->currentTextBlock = tPtr->firstTextBlock;
3624 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3625 tPtr->currentTextBlock->prior->next = NULL;
3626 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3627 tPtr->currentTextBlock = tPtr->lastTextBlock;
3628 } else {
3629 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3630 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3631 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3634 return (void *)tb;
3638 #if 0
3639 static void
3640 destroyWidget(WMWidget *widget)
3642 WMDestroyWidget(widget);
3643 // -- never do this -- wfree(widget);
3645 #endif
3648 void
3649 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3651 TextBlock *tb = (TextBlock *)vtb;
3652 if (!tb)
3653 return;
3655 if (tb->graphic) {
3656 if(tb->object) {
3657 /* naturally, there's a danger to destroying widgets whose action
3658 * brings us here: ie. press a button to destroy it...
3659 * need to find a safer way. till then... this stays commented out */
3660 /* 5 months later... destroy it 10 seconds after now which should
3661 * be enough time for the widget's action to be completed... :-) */
3662 /* This is a bad assumption. Just destroy the widget here.
3663 * if the caller needs it, it can protect it with W_RetainView()
3664 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3665 WMDestroyWidget(tb->d.widget);
3666 } else {
3667 WMReleasePixmap(tb->d.pixmap);
3669 } else {
3670 WMReleaseFont(tb->d.font);
3673 WMReleaseColor(tb->color);
3674 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3675 if (tb->sections)
3676 wfree(tb->sections);
3677 wfree(tb->text);
3678 wfree(tb);
3682 void
3683 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3685 if (tPtr->fgColor)
3686 WMReleaseColor(tPtr->fgColor);
3688 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3690 paintText(tPtr);
3694 void
3695 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3697 if (tPtr->bgColor)
3698 WMReleaseColor(tPtr->bgColor);
3700 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3701 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3703 paintText(tPtr);
3707 void
3708 WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3710 if (tPtr->bgPixmap)
3711 WMReleasePixmap(tPtr->bgPixmap);
3713 if (pixmap)
3714 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3715 else
3716 tPtr->bgPixmap = NULL;
3720 void
3721 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3723 tPtr->flags.relief = relief;
3724 textDidResize(tPtr->view->delegate, tPtr->view);
3728 void
3729 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3731 if (shouldhave && !tPtr->hS) {
3732 tPtr->hS = WMCreateScroller(tPtr);
3733 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3734 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3735 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3736 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3737 WMMapWidget(tPtr->hS);
3738 } else if (!shouldhave && tPtr->hS) {
3739 WMUnmapWidget(tPtr->hS);
3740 WMDestroyWidget(tPtr->hS);
3741 tPtr->hS = NULL;
3744 tPtr->hpos = 0;
3745 tPtr->prevHpos = 0;
3746 textDidResize(tPtr->view->delegate, tPtr->view);
3750 void
3751 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3753 if(shouldhave && !tPtr->ruler) {
3754 tPtr->ruler = WMCreateRuler(tPtr);
3755 (W_VIEW(tPtr->ruler))->attribs.cursor =
3756 tPtr->view->screen->defaultCursor;
3757 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3758 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3759 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3760 } else if(!shouldhave && tPtr->ruler) {
3761 WMShowTextRuler(tPtr, False);
3762 WMDestroyWidget(tPtr->ruler);
3763 tPtr->ruler = NULL;
3765 textDidResize(tPtr->view->delegate, tPtr->view);
3768 void
3769 WMShowTextRuler(WMText *tPtr, Bool show)
3771 if(!tPtr->ruler)
3772 return;
3774 if(tPtr->flags.monoFont)
3775 show = False;
3777 tPtr->flags.rulerShown = show;
3778 if(show) {
3779 WMMapWidget(tPtr->ruler);
3780 } else {
3781 WMUnmapWidget(tPtr->ruler);
3784 textDidResize(tPtr->view->delegate, tPtr->view);
3788 Bool
3789 WMGetTextRulerShown(WMText *tPtr)
3791 if(!tPtr->ruler)
3792 return False;
3794 return tPtr->flags.rulerShown;
3798 void
3799 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3801 if (shouldhave && !tPtr->vS) {
3802 tPtr->vS = WMCreateScroller(tPtr);
3803 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3804 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3805 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3806 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3807 WMMapWidget(tPtr->vS);
3808 } else if (!shouldhave && tPtr->vS) {
3809 WMUnmapWidget(tPtr->vS);
3810 WMDestroyWidget(tPtr->vS);
3811 tPtr->vS = NULL;
3814 tPtr->vpos = 0;
3815 tPtr->prevVpos = 0;
3816 textDidResize(tPtr->view->delegate, tPtr->view);
3820 Bool
3821 WMScrollText(WMText *tPtr, int amount)
3823 Bool scroll=False;
3825 if (amount == 0 || !tPtr->view->flags.realized)
3826 return False;
3828 if (amount < 0) {
3829 if (tPtr->vpos > 0) {
3830 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3831 else tPtr->vpos=0;
3832 scroll=True;
3834 } else {
3835 int limit = tPtr->docHeight - tPtr->visible.h;
3836 if (tPtr->vpos < limit) {
3837 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3838 else tPtr->vpos = limit;
3839 scroll = True;
3843 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3844 updateScrollers(tPtr);
3845 paintText(tPtr);
3847 tPtr->prevVpos = tPtr->vpos;
3848 return scroll;
3852 Bool
3853 WMPageText(WMText *tPtr, Bool direction)
3855 if (!tPtr->view->flags.realized)
3856 return False;
3858 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3862 void
3863 WMSetTextEditable(WMText *tPtr, Bool editable)
3865 tPtr->flags.editable = editable;
3870 WMGetTextEditable(WMText *tPtr)
3872 return tPtr->flags.editable;
3875 void
3876 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3878 tPtr->flags.indentNewLine = indent;
3881 void
3882 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3884 tPtr->flags.ignoreNewLine = ignore;
3887 Bool
3888 WMGetTextIgnoresNewline(WMText *tPtr)
3890 return tPtr->flags.ignoreNewLine;
3893 void
3894 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3896 if (mono) {
3897 if(tPtr->flags.rulerShown)
3898 WMShowTextRuler(tPtr, False);
3899 if(tPtr->flags.alignment != WALeft)
3900 tPtr->flags.alignment = WALeft;
3903 tPtr->flags.monoFont = mono;
3904 textDidResize(tPtr->view->delegate, tPtr->view);
3907 Bool
3908 WMGetTextUsesMonoFont(WMText *tPtr)
3910 return tPtr->flags.monoFont;
3914 void
3915 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3917 if (tPtr->dFont)
3918 WMReleaseFont(tPtr->dFont);
3920 if (font) {
3921 tPtr->dFont = WMRetainFont(font);
3922 } else {
3923 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3928 WMFont*
3929 WMGetTextDefaultFont(WMText *tPtr)
3931 return WMRetainFont(tPtr->dFont);
3935 void
3936 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3938 if (tPtr->dColor)
3939 WMReleaseColor(tPtr->dColor);
3941 if (color) {
3942 tPtr->dColor = WMRetainColor(color);
3943 } else {
3944 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3949 WMColor*
3950 WMGetTextDefaultColor(WMText *tPtr)
3952 return tPtr->dColor;
3956 void
3957 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3959 if(tPtr->flags.monoFont)
3960 tPtr->flags.alignment = WALeft;
3961 else
3962 tPtr->flags.alignment = alignment;
3963 WMThawText(tPtr);
3968 WMGetTextInsertType(WMText *tPtr)
3970 return tPtr->flags.prepend;
3974 void
3975 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3977 setSelectionProperty(tPtr, NULL, color, -1);
3981 WMColor*
3982 WMGetTextSelectionColor(WMText *tPtr)
3984 TextBlock *tb;
3986 tb = tPtr->currentTextBlock;
3988 if (!tb || !tPtr->flags.ownsSelection)
3989 return NULL;
3991 if(!tb->selected)
3992 return NULL;
3994 return tb->color;
3998 void
3999 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4001 setSelectionProperty(tPtr, font, NULL, -1) ;
4005 WMFont*
4006 WMGetTextSelectionFont(WMText *tPtr)
4008 TextBlock *tb;
4010 tb = tPtr->currentTextBlock;
4012 if (!tb || !tPtr->flags.ownsSelection)
4013 return NULL;
4015 if(!tb->selected)
4016 return NULL;
4018 if(tb->graphic) {
4019 tb = getFirstNonGraphicBlockFor(tb, 1);
4020 if(!tb)
4021 return NULL;
4023 return (tb->selected ? tb->d.font : NULL);
4027 void
4028 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4030 // check this
4031 if (underlined!=0 && underlined!=1)
4032 return;
4034 setSelectionProperty(tPtr, NULL, NULL, underlined);
4039 WMGetTextSelectionUnderlined(WMText *tPtr)
4041 TextBlock *tb;
4043 tb = tPtr->currentTextBlock;
4045 if (!tb || !tPtr->flags.ownsSelection)
4046 return 0;
4048 if(!tb->selected)
4049 return 0;
4051 return tb->underlined;
4055 void
4056 WMFreezeText(WMText *tPtr)
4058 tPtr->flags.frozen = True;
4062 void
4063 WMThawText(WMText *tPtr)
4065 tPtr->flags.frozen = False;
4067 if(tPtr->flags.monoFont) {
4068 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4069 TextBlock *tb;
4071 /* make sure to unmap widgets no matter where they are */
4072 /* they'll be later remapped if needed by paintText */
4073 for(j=0; j<c; j++) {
4074 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4075 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4076 WMUnmapWidget(tb->d.widget);
4082 tPtr->flags.laidOut = False;
4083 layOutDocument(tPtr);
4084 updateScrollers(tPtr);
4085 paintText(tPtr);
4086 tPtr->flags.needsLayOut = False;
4090 /* find first occurence of a string */
4091 static char *
4092 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4093 Bool caseSensitive)
4095 char *ptr;
4097 if(!haystack || !needle || !end)
4098 return NULL;
4100 for (ptr = haystack; ptr < end; ptr++) {
4101 if(caseSensitive) {
4102 if (*ptr == *needle && !strncmp(ptr, needle, len))
4103 return ptr;
4105 } else {
4106 if (tolower(*ptr) == tolower(*needle) &&
4107 !strncasecmp(ptr, needle, len))
4108 return ptr;
4112 return NULL;
4115 /* find last occurence of a string */
4116 static char *
4117 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4118 Bool caseSensitive)
4120 char *ptr;
4122 if(!haystack || !needle || !end)
4123 return NULL;
4125 for (ptr = haystack-2; ptr > end; ptr--) {
4126 if(caseSensitive) {
4127 if (*ptr == *needle && !strncmp(ptr, needle, len))
4128 return ptr;
4129 } else {
4130 if (tolower(*ptr) == tolower(*needle) &&
4131 !strncasecmp(ptr, needle, len))
4132 return ptr;
4136 return NULL;
4140 Bool
4141 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4142 Bool caseSensitive)
4144 TextBlock *tb;
4145 char *mark=NULL;
4146 unsigned short pos;
4149 #if 0
4150 if (! (tb = tPtr->currentTextBlock)) {
4151 if (! (tb = ( (direction > 0) ?
4152 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4153 return False;
4155 } else {
4156 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4157 tb = (direction>0) ? tb->next : tb->prior; */
4158 if(tb != tPtr->lastTextBlock)
4159 tb = tb->prior;
4161 #endif
4162 tb = tPtr->currentTextBlock;
4163 pos = tPtr->tpos;
4166 while(tb) {
4167 if (!tb->graphic) {
4169 if(direction > 0) {
4170 if(pos+1 < tb->used)
4171 pos++;
4173 if(tb->used - pos> 0 && pos > 0) {
4174 mark = mystrstr(&tb->text[pos], needle,
4175 strlen(needle), &tb->text[tb->used], caseSensitive);
4177 } else {
4178 tb = tb->next;
4179 pos = 0;
4180 continue;
4183 } else {
4184 if(pos-1 > 0)
4185 pos--;
4187 if(pos > 0) {
4188 mark = mystrrstr(&tb->text[pos], needle,
4189 strlen(needle), tb->text, caseSensitive);
4190 } else {
4191 tb = tb->prior;
4192 if(!tb)
4193 return False;
4194 pos = tb->used;
4195 continue;
4200 if(mark) {
4201 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4203 tPtr->tpos = (int)(mark - tb->text);
4204 tPtr->currentTextBlock = tb;
4205 updateCursorPosition(tPtr);
4206 tPtr->sel.y = tPtr->cursor.y+5;
4207 tPtr->sel.h = tPtr->cursor.h-10;
4208 tPtr->sel.x = tPtr->cursor.x +1;
4209 tPtr->sel.w = WMIN(WMWidthOfString(font,
4210 &tb->text[tPtr->tpos], strlen(needle)),
4211 tPtr->docWidth - tPtr->sel.x);
4212 tPtr->flags.ownsSelection = True;
4213 paintText(tPtr);
4215 return True;
4219 tb = (direction>0) ? tb->next : tb->prior;
4220 if(tb) {
4221 pos = (direction>0) ? 0 : tb->used;
4225 return False;
4229 Bool
4230 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4232 if (!tPtr->flags.ownsSelection)
4233 return False;
4235 removeSelection(tPtr);
4237 if(replacement) {
4238 insertTextInteractively(tPtr, replacement, strlen(replacement));
4239 updateCursorPosition(tPtr);
4240 paintText(tPtr);
4243 return True;