- WMGetTextDefaultColor() won't retain the returned color anymore, and you
[wmaker-crm.git] / WINGs / wtext.c
blob9cbc7e7ab66603e46f76cabfa2f434c99d382847
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;
2436 else if (control_pressed && *buffer == '\a')
2437 XBell(tPtr->view->screen->display, 0);
2438 else
2439 WMRelayToNextResponder(tPtr->view, event);
2442 if (!control_pressed && tPtr->flags.ownsSelection)
2443 releaseSelection(tPtr);
2447 static void
2448 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2449 void *cdata, WMData *data)
2451 Text *tPtr = (Text *)view->self;
2452 char *text;
2454 tPtr->flags.waitingForSelection = 0;
2456 if (data) {
2457 text = (char*)WMDataBytes(data);
2459 if (tPtr->parser) {
2460 (tPtr->parser) (tPtr, (void *) text);
2461 layOutDocument(tPtr);
2462 } else insertTextInteractively(tPtr, text, strlen(text));
2463 updateCursorPosition(tPtr);
2464 paintText(tPtr);
2466 } else {
2467 int n;
2469 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2471 if (text) {
2472 text[n] = 0;
2473 if (tPtr->parser) {
2474 (tPtr->parser) (tPtr, (void *) text);
2475 layOutDocument(tPtr);
2476 } else insertTextInteractively(tPtr, text, n);
2477 updateCursorPosition(tPtr);
2478 paintText(tPtr);
2480 XFree(text);
2488 static void
2489 handleActionEvents(XEvent *event, void *data)
2491 Text *tPtr = (Text *)data;
2492 Display *dpy = event->xany.display;
2493 KeySym ksym;
2496 switch (event->type) {
2497 case KeyPress:
2498 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2499 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2500 tPtr->flags.extendSelection = True;
2501 return;
2504 if (tPtr->flags.focused) {
2505 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2506 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2507 GrabModeAsync, GrabModeAsync, None,
2508 tPtr->view->screen->invisibleCursor, CurrentTime);
2509 tPtr->flags.pointerGrabbed = True;
2510 handleTextKeyPress(tPtr, event);
2512 } break;
2514 case KeyRelease:
2515 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2516 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2517 tPtr->flags.extendSelection = False;
2518 return;
2519 /* end modify flag so selection can be extended */
2521 break;
2524 case MotionNotify:
2526 if (tPtr->flags.pointerGrabbed) {
2527 tPtr->flags.pointerGrabbed = False;
2528 XUngrabPointer(dpy, CurrentTime);
2531 if(tPtr->flags.waitingForSelection)
2532 break;
2534 if ((event->xmotion.state & Button1Mask)) {
2535 TextBlock *tb = tPtr->currentTextBlock;
2537 if(tb && tPtr->flags.isOverGraphic &&
2538 tb->graphic && !tb->object) {
2539 WMSize offs;
2540 WMPixmap *pixmap = tb->d.pixmap;
2541 char *types[2] = {"application/X-image", NULL};
2543 offs.width = 2;
2544 offs.height = 2;
2546 WMDragImageFromView(tPtr->view, pixmap, types,
2547 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2548 offs, event, True);
2551 } else {
2552 if (!tPtr->flags.ownsSelection) {
2553 WMCreateSelectionHandler(tPtr->view,
2554 XA_PRIMARY, event->xbutton.time,
2555 &selectionHandler, NULL);
2556 tPtr->flags.ownsSelection = True;
2559 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2560 break;
2563 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2564 break;
2567 case ButtonPress:
2569 if (tPtr->flags.pointerGrabbed) {
2570 tPtr->flags.pointerGrabbed = False;
2571 XUngrabPointer(dpy, CurrentTime);
2572 break;
2575 if (tPtr->flags.waitingForSelection)
2576 break;
2578 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2579 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2580 return;
2583 if (tPtr->flags.ownsSelection)
2584 releaseSelection(tPtr);
2587 if (event->xbutton.button == Button1) {
2589 if(WMIsDoubleClick(event)) {
2590 TextBlock *tb = tPtr->currentTextBlock;
2592 tPtr->lastClickTime = event->xbutton.time;
2593 if(tb && tb->graphic && !tb->object) {
2594 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2595 char *desc;
2597 desc = wmalloc(tb->used+1);
2598 memcpy(desc, tb->text, tb->used);
2599 desc[tb->used] = 0;
2600 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2601 wfree(desc);
2603 } else {
2604 autoSelectText(tPtr, 2);
2606 break;
2607 } else if(event->xbutton.time - tPtr->lastClickTime
2608 < WINGsConfiguration.doubleClickDelay) {
2609 tPtr->lastClickTime = event->xbutton.time;
2610 autoSelectText(tPtr, 3);
2611 break;
2614 if (!tPtr->flags.focused) {
2615 WMSetFocusToWidget(tPtr);
2616 tPtr->flags.focused = True;
2619 tPtr->lastClickTime = event->xbutton.time;
2620 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2621 paintText(tPtr);
2624 if (event->xbutton.button
2625 == WINGsConfiguration.mouseWheelDown) {
2626 WMScrollText(tPtr, 16);
2627 break;
2630 if (event->xbutton.button
2631 == WINGsConfiguration.mouseWheelUp) {
2632 WMScrollText(tPtr, -16);
2633 break;
2636 if (event->xbutton.button == Button2) {
2637 char *text = NULL;
2638 int n;
2640 if (!tPtr->flags.editable) {
2641 XBell(dpy, 0);
2642 break;
2645 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2646 event->xbutton.time, pasteText, NULL)) {
2648 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2649 tPtr->flags.waitingForSelection = 0;
2651 if (text) {
2652 text[n] = 0;
2654 if (tPtr->parser) {
2655 (tPtr->parser) (tPtr, (void *) text);
2656 layOutDocument(tPtr);
2658 else
2659 insertTextInteractively(tPtr, text, n);
2661 XFree(text);
2662 #if 0
2663 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2664 (void*)WMInsertTextEvent);
2665 #endif
2666 updateCursorPosition(tPtr);
2667 paintText(tPtr);
2669 } else {
2670 tPtr->flags.waitingForSelection = True;
2673 break;
2677 case ButtonRelease:
2678 if (tPtr->flags.pointerGrabbed) {
2679 tPtr->flags.pointerGrabbed = False;
2680 XUngrabPointer(dpy, CurrentTime);
2681 break;
2684 if (tPtr->flags.waitingForSelection)
2685 break;
2691 static void
2692 handleEvents(XEvent *event, void *data)
2694 Text *tPtr = (Text *)data;
2696 switch(event->type) {
2697 case Expose:
2699 if (event->xexpose.count!=0)
2700 break;
2702 if(tPtr->hS) {
2703 if (!(W_VIEW(tPtr->hS))->flags.realized)
2704 WMRealizeWidget(tPtr->hS);
2707 if(tPtr->vS) {
2708 if (!(W_VIEW(tPtr->vS))->flags.realized)
2709 WMRealizeWidget(tPtr->vS);
2712 if(tPtr->ruler) {
2713 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2714 WMRealizeWidget(tPtr->ruler);
2718 if(!tPtr->db)
2719 textDidResize(tPtr->view->delegate, tPtr->view);
2721 paintText(tPtr);
2722 break;
2724 case FocusIn:
2725 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2726 != tPtr->view)
2727 return;
2728 tPtr->flags.focused = True;
2729 #if DO_BLINK
2730 if (tPtr->flags.editable && !tPtr->timerID) {
2731 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2732 blinkCursor, tPtr);
2734 #endif
2736 break;
2738 case FocusOut:
2739 tPtr->flags.focused = False;
2740 paintText(tPtr);
2741 #if DO_BLINK
2742 if (tPtr->timerID) {
2743 WMDeleteTimerHandler(tPtr->timerID);
2744 tPtr->timerID = NULL;
2746 #endif
2747 break;
2750 case DestroyNotify:
2751 clearText(tPtr);
2752 if(tPtr->db)
2753 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2754 if(tPtr->gfxItems)
2755 WMEmptyArray(tPtr->gfxItems);
2756 #if DO_BLINK
2757 if (tPtr->timerID)
2758 WMDeleteTimerHandler(tPtr->timerID);
2759 #endif
2760 WMReleaseFont(tPtr->dFont);
2761 WMReleaseColor(tPtr->dColor);
2762 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2763 WMRemoveNotificationObserver(tPtr);
2765 wfree(tPtr);
2767 break;
2773 static void
2774 insertPlainText(Text *tPtr, char *text)
2776 char *start, *mark;
2777 void *tb = NULL;
2779 start = text;
2780 while (start) {
2781 mark = strchr(start, '\n');
2782 if (mark) {
2783 tb = WMCreateTextBlockWithText(tPtr,
2784 start, tPtr->dFont,
2785 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2786 start = mark+1;
2787 tPtr->flags.first = True;
2788 } else {
2789 if (start && strlen(start)) {
2790 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2791 tPtr->dColor, tPtr->flags.first, strlen(start));
2792 } else tb = NULL;
2793 tPtr->flags.first = False;
2794 start = mark;
2797 if (tPtr->flags.prepend)
2798 WMPrependTextBlock(tPtr, tb);
2799 else
2800 WMAppendTextBlock(tPtr, tb);
2805 static void
2806 rulerMoveCallBack(WMWidget *w, void *self)
2808 Text *tPtr = (Text *)self;
2810 if (!tPtr)
2811 return;
2812 if (W_CLASS(tPtr) != WC_Text)
2813 return;
2815 paintText(tPtr);
2819 static void
2820 rulerReleaseCallBack(WMWidget *w, void *self)
2822 Text *tPtr = (Text *)self;
2824 if (!tPtr)
2825 return;
2826 if (W_CLASS(tPtr) != WC_Text)
2827 return;
2829 WMThawText(tPtr);
2830 return;
2833 static unsigned
2834 draggingSourceOperation(WMView *self, Bool local)
2836 return WDOperationCopy;
2839 static WMData*
2840 fetchDragData(WMView *self, char *type)
2842 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2843 char *desc;
2844 WMData *data;
2846 if (!tb)
2847 return NULL;
2849 printf("type is [%s]\n", type);
2850 desc = wmalloc(tb->used+1);
2851 memcpy(desc, tb->text, tb->used);
2852 desc[tb->used] = 0;
2853 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2855 wfree(desc);
2857 return data;
2861 static WMDragSourceProcs _DragSourceProcs = {
2862 draggingSourceOperation,
2863 NULL,
2864 NULL,
2865 fetchDragData
2869 static unsigned
2870 draggingEntered(WMView *self, WMDraggingInfo *info)
2872 printf("draggingEntered\n");
2873 return WDOperationCopy;
2877 static unsigned
2878 draggingUpdated(WMView *self, WMDraggingInfo *info)
2880 return WDOperationCopy;
2884 static void
2885 draggingExited(WMView *self, WMDraggingInfo *info)
2887 printf("draggingExited\n");
2890 static Bool
2891 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2893 printf("prepareForDragOperation\n");
2894 return True;
2898 char *badbadbad;
2900 static void
2901 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2902 void *cdata, WMData *data)
2904 badbadbad = wstrdup((char *)WMDataBytes(data));
2908 /* when it's done in WINGs, remove this */
2910 Bool
2911 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2913 WMScreen *scr = W_VIEW_SCREEN(view);
2915 if (!WMRequestSelection(scr->dragInfo.destView,
2916 scr->xdndSelectionAtom,
2917 XInternAtom(scr->display, type, False),
2918 scr->dragInfo.timestamp,
2919 receivedData, &scr->dragInfo)) {
2920 wwarning("could not request data for dropped data");
2924 XEvent ev;
2926 ev.type = ClientMessage;
2927 ev.xclient.message_type = scr->xdndFinishedAtom;
2928 ev.xclient.format = 32;
2929 ev.xclient.window = info->destinationWindow;
2930 ev.xclient.data.l[0] = 0;
2931 ev.xclient.data.l[1] = 0;
2932 ev.xclient.data.l[2] = 0;
2933 ev.xclient.data.l[3] = 0;
2934 ev.xclient.data.l[4] = 0;
2936 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2937 XFlush(scr->display);
2939 return True;
2942 static Bool
2943 performDragOperation(WMView *self, WMDraggingInfo *info)
2945 WMColor *color;
2946 WMText *tPtr = (WMText *)self->self;
2948 if (!tPtr)
2949 return True;
2951 requestDroppedData(tPtr->view, info, "application/X-color");
2952 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2953 if(color) {
2954 WMSetTextSelectionColor(tPtr, color);
2955 WMReleaseColor(color);
2960 return True;
2963 static void
2964 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2966 printf("concludeDragOperation\n");
2970 static WMDragDestinationProcs _DragDestinationProcs = {
2971 draggingEntered,
2972 draggingUpdated,
2973 draggingExited,
2974 prepareForDragOperation,
2975 performDragOperation,
2976 concludeDragOperation
2980 char *
2981 getStream(WMText *tPtr, int sel, int array)
2983 TextBlock *tb = NULL;
2984 char *text = NULL;
2985 unsigned long where = 0;
2987 if (!tPtr)
2988 return NULL;
2990 if (!(tb = tPtr->firstTextBlock))
2991 return NULL;
2993 if (tPtr->writer) {
2994 (tPtr->writer) (tPtr, (void *) text);
2995 return text;
2998 tb = tPtr->firstTextBlock;
2999 while (tb) {
3001 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
3003 if (!sel || (tb->graphic && tb->selected)) {
3005 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3006 && tb != tPtr->firstTextBlock) {
3007 text = wrealloc(text, where+1);
3008 text[where++] = '\n';
3011 if(tb->blank)
3012 goto _gSnext;
3014 if(tb->graphic && array) {
3015 text = wrealloc(text, where+4);
3016 text[where++] = 0xFA;
3017 text[where++] = (tb->used>>8)&0x0ff;
3018 text[where++] = tb->used&0x0ff;
3019 text[where++] = tb->allocated; /* extra info */
3021 text = wrealloc(text, where+tb->used);
3022 memcpy(&text[where], tb->text, tb->used);
3023 where += tb->used;
3026 } else if (sel && tb->selected) {
3028 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3029 text = wrealloc(text, where+1);
3030 text[where++] = '\n';
3033 if(tb->blank)
3034 goto _gSnext;
3036 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3037 memcpy(&text[where], &tb->text[tb->s_begin],
3038 tb->s_end - tb->s_begin);
3039 where += tb->s_end - tb->s_begin;
3044 _gSnext:tb = tb->next;
3047 /* +1 for the end of string, let's be nice */
3048 text = wrealloc(text, where+1);
3049 text[where] = 0;
3050 return text;
3054 static void
3055 releaseStreamObjects(void *data)
3057 if(data)
3058 wfree(data);
3061 WMArray *
3062 getStreamObjects(WMText *tPtr, int sel)
3064 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3065 WMData *data;
3066 char *stream;
3067 unsigned short len;
3068 char *start, *fa, *desc;
3070 stream = getStream(tPtr, sel, 1);
3071 if(!stream)
3072 return NULL;
3074 start = stream;
3075 while (start) {
3077 fa = strchr(start, 0xFA);
3078 if (fa) {
3079 if((int)(fa - start)>0) {
3080 desc = start;
3081 desc[(int)(fa - start)] = 0;
3082 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3083 WMSetDataFormat(data, TYPETEXT);
3084 WMAddToArray(array, (void *) data);
3087 len = *(fa+1)*0xff + *(fa+2);
3088 data = WMCreateDataWithBytes((void *)(fa+4), len);
3089 WMSetDataFormat(data, *(fa+3));
3090 WMAddToArray(array, (void *) data);
3091 start = fa + len + 4;
3093 } else {
3094 if (start && strlen(start)) {
3095 data = WMCreateDataWithBytes((void *)start, strlen(start));
3096 WMSetDataFormat(data, TYPETEXT);
3097 WMAddToArray(array, (void *) data);
3099 start = fa;
3103 wfree(stream);
3104 return array;
3108 WMText *
3109 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3111 Text *tPtr;
3112 Display *dpy;
3113 WMScreen *scr;
3114 XGCValues gcv;
3116 tPtr = wmalloc(sizeof(Text));
3117 memset(tPtr, 0, sizeof(Text));
3118 tPtr->widgetClass = WC_Text;
3119 tPtr->view = W_CreateView(W_VIEW(parent));
3120 if (!tPtr->view) {
3121 perror("could not create text's view\n");
3122 wfree(tPtr);
3123 return NULL;
3126 dpy = tPtr->view->screen->display;
3127 scr = tPtr->view->screen;
3129 tPtr->view->self = tPtr;
3130 tPtr->view->attribs.cursor = scr->textCursor;
3131 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3132 W_ResizeView(tPtr->view, 250, 200);
3134 tPtr->dColor = WMBlackColor(scr);
3135 tPtr->fgColor = WMBlackColor(scr);
3136 tPtr->bgColor = WMWhiteColor(scr);
3137 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3139 gcv.graphics_exposures = False;
3140 gcv.foreground = W_PIXEL(scr->gray);
3141 gcv.background = W_PIXEL(scr->darkGray);
3142 gcv.fill_style = FillStippled;
3143 /* why not use scr->stipple here? */
3144 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS,
3145 STIPPLE_WIDTH, STIPPLE_HEIGHT);
3146 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3147 GCForeground|GCBackground|GCStipple
3148 |GCFillStyle|GCGraphicsExposures, &gcv);
3150 tPtr->ruler = NULL;
3151 tPtr->vS = NULL;
3152 tPtr->hS = NULL;
3154 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3156 tPtr->view->delegate = &_TextViewDelegate;
3158 tPtr->delegate = NULL;
3160 #if DO_BLINK
3161 tPtr->timerID = NULL;
3162 #endif
3164 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3165 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3166 handleEvents, tPtr);
3168 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3169 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3170 handleActionEvents, tPtr);
3172 WMAddNotificationObserver(ownershipObserver, tPtr,
3173 WMSelectionOwnerDidChangeNotification,
3174 tPtr);
3176 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3177 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3181 char *types[3] = {"application/X-color", "application/X-image", NULL};
3182 WMRegisterViewForDraggedTypes(tPtr->view, types);
3185 /*WMAddNotificationObserver(fontChanged, tPtr,
3186 WMFontPanelDidChangeNotification, tPtr);*/
3188 tPtr->firstTextBlock = NULL;
3189 tPtr->lastTextBlock = NULL;
3190 tPtr->currentTextBlock = NULL;
3191 tPtr->tpos = 0;
3193 tPtr->gfxItems = WMCreateArray(4);
3195 tPtr->parser = parser;
3196 tPtr->writer = writer;
3198 tPtr->sel.x = tPtr->sel.y = 2;
3199 tPtr->sel.w = tPtr->sel.h = 0;
3201 tPtr->clicked.x = tPtr->clicked.y = 2;
3203 tPtr->visible.x = tPtr->visible.y = 2;
3204 tPtr->visible.h = tPtr->view->size.height;
3205 tPtr->visible.w = tPtr->view->size.width - 4;
3207 tPtr->cursor.x = -23;
3209 tPtr->docWidth = 0;
3210 tPtr->docHeight = 0;
3211 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3212 default_bullet);
3213 tPtr->db = (Pixmap) NULL;
3214 tPtr->bgPixmap = NULL;
3216 tPtr->margins = WMGetRulerMargins(NULL);
3217 tPtr->margins->right = tPtr->visible.w;
3218 tPtr->nMargins = 1;
3220 tPtr->flags.rulerShown = False;
3221 tPtr->flags.monoFont = False;
3222 tPtr->flags.focused = False;
3223 tPtr->flags.editable = True;
3224 tPtr->flags.ownsSelection = False;
3225 tPtr->flags.pointerGrabbed = False;
3226 tPtr->flags.extendSelection = False;
3227 tPtr->flags.frozen = False;
3228 tPtr->flags.cursorShown = True;
3229 tPtr->flags.acceptsGraphic = False;
3230 tPtr->flags.horizOnDemand = False;
3231 tPtr->flags.needsLayOut = False;
3232 tPtr->flags.ignoreNewLine = False;
3233 tPtr->flags.indentNewLine = False;
3234 tPtr->flags.laidOut = False;
3235 tPtr->flags.ownsSelection = False;
3236 tPtr->flags.waitingForSelection = False;
3237 tPtr->flags.prepend = False;
3238 tPtr->flags.isOverGraphic = False;
3239 tPtr->flags.relief = WRSunken;
3240 tPtr->flags.isOverGraphic = 0;
3241 tPtr->flags.alignment = WALeft;
3242 tPtr->flags.first = True;
3244 return tPtr;
3247 void
3248 WMPrependTextStream(WMText *tPtr, char *text)
3250 CHECK_CLASS(tPtr, WC_Text);
3252 if(!text) {
3253 if (tPtr->flags.ownsSelection)
3254 releaseSelection(tPtr);
3255 clearText(tPtr);
3256 updateScrollers(tPtr);
3257 return;
3260 tPtr->flags.prepend = True;
3261 if (text && tPtr->parser)
3262 (tPtr->parser) (tPtr, (void *) text);
3263 else
3264 insertPlainText(tPtr, text);
3266 tPtr->flags.needsLayOut = True;
3267 tPtr->tpos = 0;
3268 if(!tPtr->flags.frozen) {
3269 layOutDocument(tPtr);
3274 void
3275 WMAppendTextStream(WMText *tPtr, char *text)
3277 CHECK_CLASS(tPtr, WC_Text);
3279 if(!text) {
3280 if (tPtr->flags.ownsSelection)
3281 releaseSelection(tPtr);
3282 clearText(tPtr);
3283 updateScrollers(tPtr);
3284 return;
3287 tPtr->flags.prepend = False;
3288 if (text && tPtr->parser)
3289 (tPtr->parser) (tPtr, (void *) text);
3290 else
3291 insertPlainText(tPtr, text);
3293 tPtr->flags.needsLayOut = True;
3294 if(tPtr->currentTextBlock) {
3295 if(tPtr->currentTextBlock->graphic)
3296 tPtr->tpos = 1;
3297 else
3298 tPtr->tpos = tPtr->currentTextBlock->used;
3301 if(!tPtr->flags.frozen) {
3302 layOutDocument(tPtr);
3307 char*
3308 WMGetTextStream(WMText *tPtr)
3310 CHECK_CLASS(tPtr, WC_Text);
3312 return getStream(tPtr, 0, 0);
3316 char*
3317 WMGetTextSelectedStream(WMText *tPtr)
3319 CHECK_CLASS(tPtr, WC_Text);
3321 return getStream(tPtr, 1, 0);
3325 WMArray*
3326 WMGetTextObjects(WMText *tPtr)
3328 CHECK_CLASS(tPtr, WC_Text);
3330 return getStreamObjects(tPtr, 0);
3333 WMArray*
3334 WMGetTextSelectedObjects(WMText *tPtr)
3336 CHECK_CLASS(tPtr, WC_Text);
3338 return getStreamObjects(tPtr, 1);
3342 void
3343 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3345 CHECK_CLASS(tPtr, WC_Text);
3347 tPtr->delegate = delegate;
3351 void*
3352 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3353 char *description, WMColor *color,
3354 unsigned short first, unsigned short extraInfo)
3356 TextBlock *tb;
3358 if (!w || !description || !color)
3359 return NULL;
3361 tb = wmalloc(sizeof(TextBlock));
3363 tb->text = wstrdup(description);
3364 tb->used = strlen(description);
3365 tb->blank = False;
3366 tb->d.widget = w;
3367 tb->color = WMRetainColor(color);
3368 tb->marginN = newMargin(tPtr, NULL);
3369 tb->allocated = extraInfo;
3370 tb->first = first;
3371 tb->kanji = False;
3372 tb->graphic = True;
3373 tb->object = True;
3374 tb->underlined = False;
3375 tb->selected = False;
3376 tb->script = 0;
3377 tb->sections = NULL;
3378 tb->nsections = 0;
3379 tb->prior = NULL;
3380 tb->next = NULL;
3382 return tb;
3386 void*
3387 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3388 char *description, WMColor *color,
3389 unsigned short first, unsigned short extraInfo)
3391 TextBlock *tb;
3393 if (!p || !description || !color)
3394 return NULL;
3396 tb = wmalloc(sizeof(TextBlock));
3398 tb->text = wstrdup(description);
3399 tb->used = strlen(description);
3400 tb->blank = False;
3401 tb->d.pixmap = WMRetainPixmap(p);
3402 tb->color = WMRetainColor(color);
3403 tb->marginN = newMargin(tPtr, NULL);
3404 tb->allocated = extraInfo;
3405 tb->first = first;
3406 tb->kanji = False;
3407 tb->graphic = True;
3408 tb->object = False;
3409 tb->underlined = False;
3410 tb->selected = False;
3411 tb->script = 0;
3412 tb->sections = NULL;
3413 tb->nsections = 0;
3414 tb->prior = NULL;
3415 tb->next = NULL;
3417 return tb;
3421 void*
3422 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3423 unsigned short first, unsigned short len)
3425 TextBlock *tb;
3427 if (!font || !color)
3428 return NULL;
3430 tb = wmalloc(sizeof(TextBlock));
3432 tb->allocated = reqBlockSize(len);
3433 tb->text = (char *)wmalloc(tb->allocated);
3434 memset(tb->text, 0, tb->allocated);
3436 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3437 *tb->text = ' ';
3438 tb->used = 1;
3439 tb->blank = True;
3440 } else {
3441 memcpy(tb->text, text, len);
3442 tb->used = len;
3443 tb->blank = False;
3445 tb->text[tb->used] = 0;
3447 tb->d.font = WMRetainFont(font);
3448 tb->color = WMRetainColor(color);
3449 tb->marginN = newMargin(tPtr, NULL);
3450 tb->first = first;
3451 tb->kanji = False;
3452 tb->graphic = False;
3453 tb->underlined = False;
3454 tb->selected = False;
3455 tb->script = 0;
3456 tb->sections = NULL;
3457 tb->nsections = 0;
3458 tb->prior = NULL;
3459 tb->next = NULL;
3460 return tb;
3464 void
3465 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3466 unsigned int kanji, unsigned int underlined, int script,
3467 WMRulerMargins *margins)
3469 TextBlock *tb = (TextBlock *) vtb;
3470 if (!tb)
3471 return;
3473 tb->first = first;
3474 tb->kanji = kanji;
3475 tb->underlined = underlined;
3476 tb->script = script;
3477 tb->marginN = newMargin(tPtr, margins);
3481 void
3482 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3483 unsigned int *kanji, unsigned int *underlined, int *script,
3484 WMRulerMargins *margins)
3486 TextBlock *tb = (TextBlock *) vtb;
3487 if (!tb)
3488 return;
3490 if (first) *first = tb->first;
3491 if (kanji) *kanji = tb->kanji;
3492 if (underlined) *underlined = tb->underlined;
3493 if (script) *script = tb->script;
3494 if (margins) margins = &tPtr->margins[tb->marginN];
3498 void
3499 WMPrependTextBlock(WMText *tPtr, void *vtb)
3501 TextBlock *tb = (TextBlock *)vtb;
3503 if (!tb)
3504 return;
3506 if (tb->graphic) {
3507 if(tb->object) {
3508 WMWidget *w = tb->d.widget;
3509 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3510 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3511 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3514 WMAddToArray(tPtr->gfxItems, (void *)tb);
3515 tPtr->tpos = 1;
3517 } else {
3518 tPtr->tpos = tb->used;
3521 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3522 tb->next = tb->prior = NULL;
3523 tb->first = True;
3524 tPtr->lastTextBlock = tPtr->firstTextBlock
3525 = tPtr->currentTextBlock = tb;
3526 return;
3529 if(!tb->first) {
3530 tb->marginN = tPtr->currentTextBlock->marginN;
3533 tb->next = tPtr->currentTextBlock;
3534 tb->prior = tPtr->currentTextBlock->prior;
3535 if (tPtr->currentTextBlock->prior)
3536 tPtr->currentTextBlock->prior->next = tb;
3538 tPtr->currentTextBlock->prior = tb;
3539 if (!tb->prior)
3540 tPtr->firstTextBlock = tb;
3542 tPtr->currentTextBlock = tb;
3546 void
3547 WMAppendTextBlock(WMText *tPtr, void *vtb)
3549 TextBlock *tb = (TextBlock *)vtb;
3551 if (!tb)
3552 return;
3554 if (tb->graphic) {
3555 if(tb->object) {
3556 WMWidget *w = tb->d.widget;
3557 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3558 (W_VIEW(w))->attribs.cursor =
3559 tPtr->view->screen->defaultCursor;
3560 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3563 WMAddToArray(tPtr->gfxItems, (void *)tb);
3564 tPtr->tpos = 1;
3566 } else {
3567 tPtr->tpos = tb->used;
3571 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3572 tb->next = tb->prior = NULL;
3573 tb->first = True;
3574 tPtr->lastTextBlock = tPtr->firstTextBlock
3575 = tPtr->currentTextBlock = tb;
3576 return;
3579 if(!tb->first) {
3580 tb->marginN = tPtr->currentTextBlock->marginN;
3583 tb->next = tPtr->currentTextBlock->next;
3584 tb->prior = tPtr->currentTextBlock;
3585 if (tPtr->currentTextBlock->next)
3586 tPtr->currentTextBlock->next->prior = tb;
3588 tPtr->currentTextBlock->next = tb;
3590 if (!tb->next)
3591 tPtr->lastTextBlock = tb;
3593 tPtr->currentTextBlock = tb;
3597 void*
3598 WMRemoveTextBlock(WMText *tPtr)
3600 TextBlock *tb = NULL;
3602 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock ||
3603 !tPtr->currentTextBlock) {
3604 return NULL;
3607 tb = tPtr->currentTextBlock;
3608 if (tb->graphic) {
3609 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3611 if(tb->object) {
3612 WMUnmapWidget(tb->d.widget);
3616 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3617 if (tPtr->currentTextBlock->next)
3618 tPtr->currentTextBlock->next->prior = NULL;
3620 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3621 tPtr->currentTextBlock = tPtr->firstTextBlock;
3623 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3624 tPtr->currentTextBlock->prior->next = NULL;
3625 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3626 tPtr->currentTextBlock = tPtr->lastTextBlock;
3627 } else {
3628 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3629 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3630 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3633 return (void *)tb;
3637 #if 0
3638 static void
3639 destroyWidget(WMWidget *widget)
3641 WMDestroyWidget(widget);
3642 // -- never do this -- wfree(widget);
3644 #endif
3647 void
3648 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3650 TextBlock *tb = (TextBlock *)vtb;
3651 if (!tb)
3652 return;
3654 if (tb->graphic) {
3655 if(tb->object) {
3656 /* naturally, there's a danger to destroying widgets whose action
3657 * brings us here: ie. press a button to destroy it...
3658 * need to find a safer way. till then... this stays commented out */
3659 /* 5 months later... destroy it 10 seconds after now which should
3660 * be enough time for the widget's action to be completed... :-) */
3661 /* This is a bad assumption. Just destroy the widget here.
3662 * if the caller needs it, it can protect it with W_RetainView()
3663 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3664 WMDestroyWidget(tb->d.widget);
3665 } else {
3666 WMReleasePixmap(tb->d.pixmap);
3668 } else {
3669 WMReleaseFont(tb->d.font);
3672 WMReleaseColor(tb->color);
3673 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3674 if (tb->sections)
3675 wfree(tb->sections);
3676 wfree(tb->text);
3677 wfree(tb);
3681 void
3682 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3684 if (tPtr->fgColor)
3685 WMReleaseColor(tPtr->fgColor);
3687 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3689 paintText(tPtr);
3693 void
3694 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3696 if (tPtr->bgColor)
3697 WMReleaseColor(tPtr->bgColor);
3699 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3700 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3702 paintText(tPtr);
3706 void
3707 WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3709 if (tPtr->bgPixmap)
3710 WMReleasePixmap(tPtr->bgPixmap);
3712 if (pixmap)
3713 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3714 else
3715 tPtr->bgPixmap = NULL;
3719 void
3720 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3722 tPtr->flags.relief = relief;
3723 textDidResize(tPtr->view->delegate, tPtr->view);
3727 void
3728 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3730 if (shouldhave && !tPtr->hS) {
3731 tPtr->hS = WMCreateScroller(tPtr);
3732 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3733 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3734 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3735 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3736 WMMapWidget(tPtr->hS);
3737 } else if (!shouldhave && tPtr->hS) {
3738 WMUnmapWidget(tPtr->hS);
3739 WMDestroyWidget(tPtr->hS);
3740 tPtr->hS = NULL;
3743 tPtr->hpos = 0;
3744 tPtr->prevHpos = 0;
3745 textDidResize(tPtr->view->delegate, tPtr->view);
3749 void
3750 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3752 if(shouldhave && !tPtr->ruler) {
3753 tPtr->ruler = WMCreateRuler(tPtr);
3754 (W_VIEW(tPtr->ruler))->attribs.cursor =
3755 tPtr->view->screen->defaultCursor;
3756 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3757 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3758 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3759 } else if(!shouldhave && tPtr->ruler) {
3760 WMShowTextRuler(tPtr, False);
3761 WMDestroyWidget(tPtr->ruler);
3762 tPtr->ruler = NULL;
3764 textDidResize(tPtr->view->delegate, tPtr->view);
3767 void
3768 WMShowTextRuler(WMText *tPtr, Bool show)
3770 if(!tPtr->ruler)
3771 return;
3773 if(tPtr->flags.monoFont)
3774 show = False;
3776 tPtr->flags.rulerShown = show;
3777 if(show) {
3778 WMMapWidget(tPtr->ruler);
3779 } else {
3780 WMUnmapWidget(tPtr->ruler);
3783 textDidResize(tPtr->view->delegate, tPtr->view);
3787 Bool
3788 WMGetTextRulerShown(WMText *tPtr)
3790 if(!tPtr->ruler)
3791 return False;
3793 return tPtr->flags.rulerShown;
3797 void
3798 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3800 if (shouldhave && !tPtr->vS) {
3801 tPtr->vS = WMCreateScroller(tPtr);
3802 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3803 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3804 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3805 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3806 WMMapWidget(tPtr->vS);
3807 } else if (!shouldhave && tPtr->vS) {
3808 WMUnmapWidget(tPtr->vS);
3809 WMDestroyWidget(tPtr->vS);
3810 tPtr->vS = NULL;
3813 tPtr->vpos = 0;
3814 tPtr->prevVpos = 0;
3815 textDidResize(tPtr->view->delegate, tPtr->view);
3819 Bool
3820 WMScrollText(WMText *tPtr, int amount)
3822 Bool scroll=False;
3824 if (amount == 0 || !tPtr->view->flags.realized)
3825 return False;
3827 if (amount < 0) {
3828 if (tPtr->vpos > 0) {
3829 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3830 else tPtr->vpos=0;
3831 scroll=True;
3833 } else {
3834 int limit = tPtr->docHeight - tPtr->visible.h;
3835 if (tPtr->vpos < limit) {
3836 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3837 else tPtr->vpos = limit;
3838 scroll = True;
3842 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3843 updateScrollers(tPtr);
3844 paintText(tPtr);
3846 tPtr->prevVpos = tPtr->vpos;
3847 return scroll;
3851 Bool
3852 WMPageText(WMText *tPtr, Bool direction)
3854 if (!tPtr->view->flags.realized)
3855 return False;
3857 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3861 void
3862 WMSetTextEditable(WMText *tPtr, Bool editable)
3864 tPtr->flags.editable = editable;
3869 WMGetTextEditable(WMText *tPtr)
3871 return tPtr->flags.editable;
3874 void
3875 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3877 tPtr->flags.indentNewLine = indent;
3880 void
3881 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3883 tPtr->flags.ignoreNewLine = ignore;
3886 Bool
3887 WMGetTextIgnoresNewline(WMText *tPtr)
3889 return tPtr->flags.ignoreNewLine;
3892 void
3893 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3895 if (mono) {
3896 if(tPtr->flags.rulerShown)
3897 WMShowTextRuler(tPtr, False);
3898 if(tPtr->flags.alignment != WALeft)
3899 tPtr->flags.alignment = WALeft;
3902 tPtr->flags.monoFont = mono;
3903 textDidResize(tPtr->view->delegate, tPtr->view);
3906 Bool
3907 WMGetTextUsesMonoFont(WMText *tPtr)
3909 return tPtr->flags.monoFont;
3913 void
3914 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3916 if (tPtr->dFont)
3917 WMReleaseFont(tPtr->dFont);
3919 if (font) {
3920 tPtr->dFont = WMRetainFont(font);
3921 } else {
3922 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3927 WMFont*
3928 WMGetTextDefaultFont(WMText *tPtr)
3930 return WMRetainFont(tPtr->dFont);
3934 void
3935 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3937 if (tPtr->dColor)
3938 WMReleaseColor(tPtr->dColor);
3940 if (color) {
3941 tPtr->dColor = WMRetainColor(color);
3942 } else {
3943 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3948 WMColor*
3949 WMGetTextDefaultColor(WMText *tPtr)
3951 return tPtr->dColor;
3955 void
3956 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3958 if(tPtr->flags.monoFont)
3959 tPtr->flags.alignment = WALeft;
3960 else
3961 tPtr->flags.alignment = alignment;
3962 WMThawText(tPtr);
3967 WMGetTextInsertType(WMText *tPtr)
3969 return tPtr->flags.prepend;
3973 void
3974 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3976 setSelectionProperty(tPtr, NULL, color, -1);
3980 WMColor*
3981 WMGetTextSelectionColor(WMText *tPtr)
3983 TextBlock *tb;
3985 tb = tPtr->currentTextBlock;
3987 if (!tb || !tPtr->flags.ownsSelection)
3988 return NULL;
3990 if(!tb->selected)
3991 return NULL;
3993 return tb->color;
3997 void
3998 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4000 setSelectionProperty(tPtr, font, NULL, -1) ;
4004 WMFont*
4005 WMGetTextSelectionFont(WMText *tPtr)
4007 TextBlock *tb;
4009 tb = tPtr->currentTextBlock;
4011 if (!tb || !tPtr->flags.ownsSelection)
4012 return NULL;
4014 if(!tb->selected)
4015 return NULL;
4017 if(tb->graphic) {
4018 tb = getFirstNonGraphicBlockFor(tb, 1);
4019 if(!tb)
4020 return NULL;
4022 return (tb->selected ? tb->d.font : NULL);
4026 void
4027 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4029 // check this
4030 if (underlined!=0 && underlined!=1)
4031 return;
4033 setSelectionProperty(tPtr, NULL, NULL, underlined);
4038 WMGetTextSelectionUnderlined(WMText *tPtr)
4040 TextBlock *tb;
4042 tb = tPtr->currentTextBlock;
4044 if (!tb || !tPtr->flags.ownsSelection)
4045 return 0;
4047 if(!tb->selected)
4048 return 0;
4050 return tb->underlined;
4054 void
4055 WMFreezeText(WMText *tPtr)
4057 tPtr->flags.frozen = True;
4061 void
4062 WMThawText(WMText *tPtr)
4064 tPtr->flags.frozen = False;
4066 if(tPtr->flags.monoFont) {
4067 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4068 TextBlock *tb;
4070 /* make sure to unmap widgets no matter where they are */
4071 /* they'll be later remapped if needed by paintText */
4072 for(j=0; j<c; j++) {
4073 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4074 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4075 WMUnmapWidget(tb->d.widget);
4081 tPtr->flags.laidOut = False;
4082 layOutDocument(tPtr);
4083 updateScrollers(tPtr);
4084 paintText(tPtr);
4085 tPtr->flags.needsLayOut = False;
4089 /* find first occurence of a string */
4090 static char *
4091 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4092 Bool caseSensitive)
4094 char *ptr;
4096 if(!haystack || !needle || !end)
4097 return NULL;
4099 for (ptr = haystack; ptr < end; ptr++) {
4100 if(caseSensitive) {
4101 if (*ptr == *needle && !strncmp(ptr, needle, len))
4102 return ptr;
4104 } else {
4105 if (tolower(*ptr) == tolower(*needle) &&
4106 !strncasecmp(ptr, needle, len))
4107 return ptr;
4111 return NULL;
4114 /* find last occurence of a string */
4115 static char *
4116 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4117 Bool caseSensitive)
4119 char *ptr;
4121 if(!haystack || !needle || !end)
4122 return NULL;
4124 for (ptr = haystack-2; ptr > end; ptr--) {
4125 if(caseSensitive) {
4126 if (*ptr == *needle && !strncmp(ptr, needle, len))
4127 return ptr;
4128 } else {
4129 if (tolower(*ptr) == tolower(*needle) &&
4130 !strncasecmp(ptr, needle, len))
4131 return ptr;
4135 return NULL;
4139 Bool
4140 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4141 Bool caseSensitive)
4143 TextBlock *tb;
4144 char *mark=NULL;
4145 unsigned short pos;
4148 #if 0
4149 if (! (tb = tPtr->currentTextBlock)) {
4150 if (! (tb = ( (direction > 0) ?
4151 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4152 return False;
4154 } else {
4155 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4156 tb = (direction>0) ? tb->next : tb->prior; */
4157 if(tb != tPtr->lastTextBlock)
4158 tb = tb->prior;
4160 #endif
4161 tb = tPtr->currentTextBlock;
4162 pos = tPtr->tpos;
4165 while(tb) {
4166 if (!tb->graphic) {
4168 if(direction > 0) {
4169 if(pos+1 < tb->used)
4170 pos++;
4172 if(tb->used - pos> 0 && pos > 0) {
4173 mark = mystrstr(&tb->text[pos], needle,
4174 strlen(needle), &tb->text[tb->used], caseSensitive);
4176 } else {
4177 tb = tb->next;
4178 pos = 0;
4179 continue;
4182 } else {
4183 if(pos-1 > 0)
4184 pos--;
4186 if(pos > 0) {
4187 mark = mystrrstr(&tb->text[pos], needle,
4188 strlen(needle), tb->text, caseSensitive);
4189 } else {
4190 tb = tb->prior;
4191 if(!tb)
4192 return False;
4193 pos = tb->used;
4194 continue;
4199 if(mark) {
4200 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4202 tPtr->tpos = (int)(mark - tb->text);
4203 tPtr->currentTextBlock = tb;
4204 updateCursorPosition(tPtr);
4205 tPtr->sel.y = tPtr->cursor.y+5;
4206 tPtr->sel.h = tPtr->cursor.h-10;
4207 tPtr->sel.x = tPtr->cursor.x +1;
4208 tPtr->sel.w = WMIN(WMWidthOfString(font,
4209 &tb->text[tPtr->tpos], strlen(needle)),
4210 tPtr->docWidth - tPtr->sel.x);
4211 tPtr->flags.ownsSelection = True;
4212 paintText(tPtr);
4214 return True;
4218 tb = (direction>0) ? tb->next : tb->prior;
4219 if(tb) {
4220 pos = (direction>0) ? 0 : tb->used;
4224 return False;
4228 Bool
4229 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4231 if (!tPtr->flags.ownsSelection)
4232 return False;
4234 removeSelection(tPtr);
4236 if(replacement) {
4237 insertTextInteractively(tPtr, replacement, strlen(replacement));
4238 updateCursorPosition(tPtr);
4239 paintText(tPtr);
4242 return True;