changed indentation to use spaces only
[wmaker-crm.git] / WINGs / wtext.c
blobfe9cbd2724596dd0f5771e58aca53cea43337f68
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;
170 WMArray *xdndSourceTypes;
171 WMArray *xdndDestinationTypes;
172 } Text;
175 #define NOTIFY(T,C,N,A) {\
176 WMNotification *notif = WMCreateNotification(N,T,A);\
177 if ((T)->delegate && (T)->delegate->C)\
178 (*(T)->delegate->C)((T)->delegate,notif);\
179 WMPostNotification(notif);\
180 WMReleaseNotification(notif);}
183 #define TYPETEXT 0
185 #if 0
186 /* just to print blocks of text not terminated by \0 */
187 static void
188 output(char *ptr, int len)
190 char *s;
192 s = wmalloc(len+1);
193 memcpy(s, ptr, len);
194 s[len] = 0;
195 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
196 printf("[%s]\n", s);
197 wfree(s);
199 #endif
202 #if DO_BLINK
203 #define CURSOR_BLINK_ON_DELAY 600
204 #define CURSOR_BLINK_OFF_DELAY 400
205 #endif
208 #define STIPPLE_WIDTH 8
209 #define STIPPLE_HEIGHT 8
210 static unsigned char STIPPLE_BITS[] = {
211 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
216 static char *default_bullet[] = {
217 "6 6 4 1",
218 " c None s None",
219 ". c black",
220 "X c white",
221 "o c #808080",
222 " ... ",
223 ".XX.. ",
224 ".XX..o",
225 ".....o",
226 " ...oo",
227 " ooo "};
230 static void handleEvents(XEvent *event, void *data);
231 static void layOutDocument(Text *tPtr);
232 static void updateScrollers(Text *tPtr);
235 static int
236 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
238 unsigned int i=0;
240 for(i=0; i < tPtr->nMargins; i++) {
242 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
243 return i;
246 return -1;
251 static int
252 newMargin(Text *tPtr, WMRulerMargins *margins)
254 int n;
256 if (!margins) {
257 tPtr->margins[0].retainCount++;
258 return 0;
261 n = getMarginNumber(tPtr, margins);
263 if (n == -1) {
265 if(tPtr->nMargins >= 127) {
266 n = tPtr->nMargins-1;
267 return n;
270 tPtr->margins = wrealloc(tPtr->margins,
271 (++tPtr->nMargins)*sizeof(WMRulerMargins));
273 n = tPtr->nMargins-1;
274 tPtr->margins[n].left = margins->left;
275 tPtr->margins[n].first = margins->first;
276 tPtr->margins[n].body = margins->body;
277 tPtr->margins[n].right = margins->right;
278 /* for each tab... */
279 tPtr->margins[n].retainCount = 1;
280 } else {
281 tPtr->margins[n].retainCount++;
284 return n;
287 static Bool
288 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
290 unsigned short i, w, lw, selected = False, extend = False;
291 myRect sel;
294 /* if selection rectangle completely encloses the section */
295 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
296 && (tb->sections[s]._y + tb->sections[s].h
297 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
298 sel.x = 0;
299 sel.w = tPtr->visible.w;
300 selected = extend = True;
302 /* or if it starts on a line and then goes further down */
303 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
304 && (tb->sections[s]._y + tb->sections[s].h
305 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
306 && (tb->sections[s]._y + tb->sections[s].h
307 >= tPtr->visible.y + tPtr->sel.y) ) {
308 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
309 sel.w = tPtr->visible.w;
310 selected = extend = True;
312 /* or if it begins before a line, but ends on it */
313 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
314 && (tb->sections[s]._y + tb->sections[s].h
315 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
316 && (tb->sections[s]._y
317 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
319 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
320 sel.w = tPtr->sel.x + tPtr->sel.w;
321 else
322 sel.w = tPtr->sel.x;
324 sel.x = 0;
325 selected = True;
327 /* or if the selection rectangle lies entirely within a line */
328 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
329 && (tPtr->sel.w >= 2)
330 && (tb->sections[s]._y + tb->sections[s].h
331 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
332 sel.x = tPtr->sel.x;
333 sel.w = tPtr->sel.w;
334 selected = True;
337 if (selected) {
338 selected = False;
340 /* if not within (modified) selection rectangle */
341 if ( tb->sections[s].x > sel.x + sel.w
342 || tb->sections[s].x + tb->sections[s].w < sel.x)
343 return False;
345 if (tb->graphic) {
346 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
347 && tb->sections[s].x >= sel.x) {
348 rect->width = tb->sections[s].w;
349 rect->x = tb->sections[s].x;
350 selected = True;
352 } else {
354 i = tb->sections[s].begin;
355 lw = 0;
357 if (0&& tb->sections[s].x >= sel.x) {
358 tb->s_begin = tb->sections[s].begin;
359 goto _selEnd;
362 while (++i <= tb->sections[s].end) {
364 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
365 lw += w;
367 if (lw + tb->sections[s].x >= sel.x
368 || i == tb->sections[s].end ) {
369 lw -= w;
370 i--;
371 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
372 break;
376 if (i > tb->sections[s].end) {
377 printf("WasSelected: (i > tb->sections[s].end) \n");
378 return False;
381 _selEnd: rect->x = tb->sections[s].x + lw;
382 lw = 0;
383 while(++i <= tb->sections[s].end) {
385 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
386 lw += w;
388 if (lw + rect->x >= sel.x + sel.w
389 || i == tb->sections[s].end ) {
391 if (i != tb->sections[s].end) {
392 lw -= w;
393 i--;
396 rect->width = lw;
397 if (tb->sections[s].last && sel.x + sel.w
398 >= tb->sections[s].x + tb->sections[s].w
399 && extend ) {
400 rect->width += (tPtr->visible.w - rect->x - lw);
403 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
404 selected = True;
405 break;
411 if (selected) {
412 rect->y = tb->sections[s]._y - tPtr->vpos;
413 rect->height = tb->sections[s].h;
414 if(tb->graphic) { printf("DEBUG: graphic s%d h%d\n", s,tb->sections[s].h);}
416 return selected;
420 static void
421 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
423 TextBlock *tb;
424 int isFont=False;
426 tb = tPtr->firstTextBlock;
427 if (!tb || !tPtr->flags.ownsSelection)
428 return;
430 if (font && (!color || underlined==-1))
431 isFont = True;
433 while (tb) {
434 if (tPtr->flags.monoFont || tb->selected) {
436 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
437 || tb->graphic) {
439 if(isFont) {
440 if(!tb->graphic) {
441 WMReleaseFont(tb->d.font);
442 tb->d.font = WMRetainFont(font);
444 } else if(underlined !=-1) {
445 tb->underlined = underlined;
446 } else {
447 WMReleaseColor(tb->color);
448 tb->color = WMRetainColor(color);
451 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
453 TextBlock *midtb, *otb = tb;
455 if(underlined != -1) {
456 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
457 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
458 False, (tb->s_end - tb->s_begin));
459 } else {
460 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
461 &(tb->text[tb->s_begin]),
462 (isFont?font:tb->d.font),
463 (isFont?tb->color:color),
464 False, (tb->s_end - tb->s_begin));
468 if (midtb) {
469 if(underlined != -1) {
470 midtb->underlined = underlined;
471 } else {
472 midtb->underlined = otb->underlined;
475 midtb->selected = !True;
476 midtb->s_begin = 0;
477 midtb->s_end = midtb->used;
478 tPtr->currentTextBlock = tb;
479 WMAppendTextBlock(tPtr, midtb);
480 tb = tPtr->currentTextBlock;
483 if (otb->used - otb->s_end > 0) {
484 TextBlock *ntb;
485 ntb = (TextBlock *)
486 WMCreateTextBlockWithText(tPtr,
487 &(otb->text[otb->s_end]), otb->d.font, otb->color,
488 False, otb->used - otb->s_end);
490 if (ntb) {
491 ntb->underlined = otb->underlined;
492 ntb->selected = False;
493 WMAppendTextBlock(tPtr, ntb);
494 tb = tPtr->currentTextBlock;
498 if (midtb) {
499 tPtr->currentTextBlock = midtb;
502 otb->selected = False;
503 otb->used = otb->s_begin;
507 tb = tb->next;
510 tPtr->flags.needsLayOut = True;
511 WMThawText(tPtr);
513 /* in case the size changed... */
514 if(isFont && tPtr->currentTextBlock) {
515 TextBlock *tb = tPtr->currentTextBlock;
517 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
518 tPtr->sel.y = 3 + tb->sections[0]._y;
519 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
520 tPtr->sel.w = tb->sections[tb->nsections-1].w;
521 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
522 tPtr->sel.x = 0;
524 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
530 static Bool
531 removeSelection(Text *tPtr)
533 TextBlock *tb = NULL;
534 Bool first = False;
536 if (!(tb = tPtr->firstTextBlock))
537 return False;
539 while (tb) {
540 if (tb->selected) {
541 if(!first && !tb->graphic) {
542 WMReleaseFont(tPtr->dFont);
543 tPtr->dFont = WMRetainFont(tb->d.font);
544 first = True;
547 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
548 tPtr->currentTextBlock = tb;
549 if(tb->next) {
550 tPtr->tpos = 0;
551 } else if(tb->prior) {
552 if(tb->prior->graphic)
553 tPtr->tpos = 1;
554 else
555 tPtr->tpos = tb->prior->used;
556 } else tPtr->tpos = 0;
558 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
559 tb = tPtr->currentTextBlock;
560 continue;
562 } else if (tb->s_end <= tb->used) {
563 memmove(&(tb->text[tb->s_begin]),
564 &(tb->text[tb->s_end]), tb->used - tb->s_end);
565 tb->used -= (tb->s_end - tb->s_begin);
566 tb->selected = False;
567 tPtr->tpos = tb->s_begin;
572 tb = tb->next;
574 return True;
578 static TextBlock*
579 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
581 TextBlock *hold = tb;
583 if (!tb)
584 return NULL;
586 while (tb) {
587 if (!tb->graphic)
588 break;
589 tb = (dir? tb->next : tb->prior);
592 if(!tb) {
593 tb = hold;
594 while (tb) {
595 if (!tb->graphic)
596 break;
597 tb = (dir? tb->prior : tb->next);
601 if(!tb)
602 return NULL;
604 return tb;
608 static Bool
609 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
610 TextBlock *tb)
612 if (tPtr->flags.monoFont && tb->graphic) {
613 tb = getFirstNonGraphicBlockFor(tb, *dir);
614 if(!tb)
615 return 0;
617 if (tb->graphic) {
618 tPtr->currentTextBlock =
619 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
620 tPtr->tpos = 0;
621 return 0;
626 if(!tb->sections) {
627 layOutDocument(tPtr);
628 return 0;
631 *dir = !(y <= tb->sections[0].y);
632 if(*dir) {
633 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
634 && (y >= tb->sections[0]._y ) ) {
635 /* if it's on the same line */
636 if(x < tb->sections[0].x)
637 *dir = 0;
639 } else {
640 if ( ( y <= tb->sections[tb->nsections-1]._y
641 + tb->sections[tb->nsections-1].h )
642 && (y >= tb->sections[tb->nsections-1]._y ) ) {
643 /* if it's on the same line */
644 if(x > tb->sections[tb->nsections-1].x)
645 *dir = 1;
649 return 1;
653 static void
654 paintText(Text *tPtr)
656 TextBlock *tb;
657 WMFont *font;
658 char *text;
659 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
660 WMScreen *scr = tPtr->view->screen;
661 Display *dpy = tPtr->view->screen->display;
662 Window win = tPtr->view->window;
663 WMColor *color;
665 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
666 return;
669 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0,
670 tPtr->visible.w, tPtr->visible.h);
672 if (tPtr->bgPixmap) {
673 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
674 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
675 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
678 if (! (tb = tPtr->currentTextBlock)) {
679 if (! (tb = tPtr->firstTextBlock)) {
680 goto _copy_area;
684 done = False;
688 /* first, which direction? Don't waste time looking all over,
689 since the parts to be drawn will most likely be near what
690 was previously drawn */
691 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
692 goto _copy_area;
694 while(tb) {
696 if (tb->graphic && tPtr->flags.monoFont)
697 goto _getSibling;
699 if(dir) {
700 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
701 + tb->sections[tb->nsections-1].h)
702 break;
703 } else {
704 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
705 + tb->sections[tb->nsections-1].h)
706 break;
709 _getSibling:
710 if(dir) {
711 if(tb->next)
712 tb = tb->next;
713 else break;
714 } else {
715 if(tb->prior)
716 tb = tb->prior;
717 else break;
722 /* first, place all text that can be viewed */
723 while (!done && tb) {
724 if (tb->graphic) {
725 tb = tb->next;
726 continue;
729 tb->selected = False;
731 for(s=0; s<tb->nsections && !done; s++) {
733 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
734 done = True;
735 break;
738 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
739 continue;
741 if (tPtr->flags.monoFont) {
742 font = tPtr->dFont;
743 color = tPtr->fgColor;
744 } else {
745 font = tb->d.font;
746 color = tb->color;
749 if (tPtr->flags.ownsSelection) {
750 XRectangle rect;
752 if (sectionWasSelected(tPtr, tb, &rect, s)) {
753 tb->selected = True;
754 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
755 rect.x, rect.y, rect.width, rect.height);
759 prev_y = tb->sections[s]._y;
761 len = tb->sections[s].end - tb->sections[s].begin;
762 text = &(tb->text[tb->sections[s].begin]);
763 y = tb->sections[s].y - tPtr->vpos;
764 WMDrawString(scr, tPtr->db, color, font,
765 tb->sections[s].x - tPtr->hpos, y, text, len);
767 if (!tPtr->flags.monoFont && tb->underlined) {
768 XDrawLine(dpy, tPtr->db, WMColorGC(color),
769 tb->sections[s].x - tPtr->hpos,
770 y + font->y + 1,
771 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
772 y + font->y + 1);
775 tb = (!done? tb->next : NULL);
778 /* now , show all graphic items that can be viewed */
779 c = WMGetArrayItemCount(tPtr->gfxItems);
780 if (c > 0 && !tPtr->flags.monoFont) {
781 int j, h;
783 for(j=0; j<c; j++) {
784 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
786 /* if it's not viewable, and mapped, unmap it */
787 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
788 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
790 if(tb->object) {
791 if ((W_VIEW(tb->d.widget))->flags.mapped) {
792 WMUnmapWidget(tb->d.widget);
795 } else {
796 /* if it's viewable, and not mapped, map it */
797 if(tb->object) {
798 W_View *view = W_VIEW(tb->d.widget);
800 if (!view->flags.realized)
801 WMRealizeWidget(tb->d.widget);
802 if(!view->flags.mapped) {
803 XMapWindow(view->screen->display, view->window);
804 XFlush(view->screen->display);
805 view->flags.mapped = 1;
809 if(tb->object) {
810 WMMoveWidget(tb->d.widget,
811 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
812 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
813 h = WMWidgetHeight(tb->d.widget) + 1;
815 } else {
816 WMDrawPixmap(tb->d.pixmap, tPtr->db,
817 tb->sections[0].x - tPtr->hpos,
818 tb->sections[0].y - tPtr->vpos);
819 h = tb->d.pixmap->height + 1;
823 if (tPtr->flags.ownsSelection) {
824 XRectangle rect;
826 if ( sectionWasSelected(tPtr, tb, &rect, 0)) {
827 Drawable d = (0&&tb->object?
828 (WMWidgetView(tb->d.widget))->window : tPtr->db);
830 tb->selected = True;
831 XFillRectangle(dpy, d, tPtr->stippledGC,
832 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,*/
833 rect.x, rect.y, rect.width, rect.height);
837 if (!tPtr->flags.monoFont && tb->underlined) {
838 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
839 tb->sections[0].x - tPtr->hpos,
840 tb->sections[0].y + h - tPtr->vpos,
841 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
842 tb->sections[0].y + h - tPtr->vpos);
849 _copy_area:
850 if (tPtr->flags.editable && tPtr->flags.cursorShown
851 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
852 int y = tPtr->cursor.y - tPtr->vpos;
853 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
854 tPtr->cursor.x, y,
855 tPtr->cursor.x, y + tPtr->cursor.h);
858 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
859 tPtr->visible.w, tPtr->visible.h,
860 tPtr->visible.x, tPtr->visible.y);
862 W_DrawRelief(scr, win, 0, 0,
863 tPtr->view->size.width, tPtr->view->size.height,
864 tPtr->flags.relief);
866 if (tPtr->ruler && tPtr->flags.rulerShown)
867 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor),
868 2, 42, tPtr->view->size.width-4, 42);
872 static void
873 mouseOverObject(Text *tPtr, int x, int y)
875 TextBlock *tb;
876 Bool result = False;
878 x -= tPtr->visible.x;
879 x += tPtr->hpos;
880 y -= tPtr->visible.y;
881 y += tPtr->vpos;
883 if(tPtr->flags.ownsSelection) {
884 if(tPtr->sel.x <= x
885 && tPtr->sel.y <= y
886 && tPtr->sel.x + tPtr->sel.w >= x
887 && tPtr->sel.y + tPtr->sel.h >= y) {
888 tPtr->flags.isOverGraphic = 1;
889 result = True;
894 if(!result) {
895 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
897 if (c<1)
898 tPtr->flags.isOverGraphic = 0;
901 for(j=0; j<c; j++) {
902 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
904 if(!tb || !tb->sections) {
905 tPtr->flags.isOverGraphic = 0;
906 return;
909 if(!tb->object) {
910 if(tb->sections[0].x <= x
911 && tb->sections[0].y <= y
912 && tb->sections[0].x + tb->sections[0].w >= x
913 && tb->sections[0].y + tb->d.pixmap->height >= y ) {
914 tPtr->flags.isOverGraphic = 3;
915 result = True;
916 break;
924 if(!result)
925 tPtr->flags.isOverGraphic = 0;
928 tPtr->view->attribs.cursor = (result?
929 tPtr->view->screen->defaultCursor
930 : tPtr->view->screen->textCursor);
932 XSetWindowAttributes attribs;
933 attribs.cursor = tPtr->view->attribs.cursor;
934 XChangeWindowAttributes(tPtr->view->screen->display,
935 tPtr->view->window, CWCursor,
936 &attribs);
940 #if DO_BLINK
942 static void
943 blinkCursor(void *data)
945 Text *tPtr = (Text*)data;
947 if (tPtr->flags.cursorShown) {
948 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
949 blinkCursor, data);
950 } else {
951 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
952 blinkCursor, data);
954 paintText(tPtr);
955 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
957 #endif
959 static void
960 updateCursorPosition(Text *tPtr)
962 TextBlock *tb = NULL;
963 int x, y, h, s;
965 if(tPtr->flags.needsLayOut)
966 layOutDocument(tPtr);
968 if (! (tb = tPtr->currentTextBlock)) {
969 if (! (tb = tPtr->firstTextBlock)) {
970 WMFont *font = tPtr->dFont;
971 tPtr->tpos = 0;
972 tPtr->cursor.h = font->height + abs(font->height-font->y);
974 tPtr->cursor.y = 2;
975 tPtr->cursor.x = 2;
976 return;
981 if(tb->blank) {
982 tPtr->tpos = 0;
983 y = tb->sections[0].y;
984 h = tb->sections[0].h;
985 x = tb->sections[0].x;
987 } else if(tb->graphic) {
988 y = tb->sections[0].y;
989 h = tb->sections[0].h;
990 x = tb->sections[0].x;
991 if(tPtr->tpos == 1)
992 x += tb->sections[0].w;
994 } else {
995 if(tPtr->tpos > tb->used)
996 tPtr->tpos = tb->used;
998 for(s=0; s<tb->nsections-1; s++) {
1000 if(tPtr->tpos >= tb->sections[s].begin
1001 && tPtr->tpos <= tb->sections[s].end)
1002 break;
1005 y = tb->sections[s]._y;
1006 h = tb->sections[s].h;
1007 x = tb->sections[s].x + WMWidthOfString(
1008 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
1009 &tb->text[tb->sections[s].begin],
1010 tPtr->tpos - tb->sections[s].begin);
1013 tPtr->cursor.y = y;
1014 tPtr->cursor.h = h;
1015 tPtr->cursor.x = x;
1018 /* scroll the bars if the cursor is not visible */
1019 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1020 if(tPtr->cursor.y+tPtr->cursor.h
1021 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1022 tPtr->vpos +=
1023 (tPtr->cursor.y+tPtr->cursor.h+10
1024 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1025 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1026 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1031 updateScrollers(tPtr);
1035 static void
1036 cursorToTextPosition(Text *tPtr, int x, int y)
1038 TextBlock *tb = NULL;
1039 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
1040 char *text;
1042 if(tPtr->flags.needsLayOut)
1043 layOutDocument(tPtr);
1045 y += (tPtr->vpos - tPtr->visible.y);
1046 if (y<0)
1047 y = 0;
1049 x -= (tPtr->visible.x - 2);
1050 if (x<0)
1051 x=0;
1053 /* clicked is relative to document, not window... */
1054 tPtr->clicked.x = x;
1055 tPtr->clicked.y = y;
1057 if (! (tb = tPtr->currentTextBlock)) {
1058 if (! (tb = tPtr->firstTextBlock)) {
1059 WMFont *font = tPtr->dFont;
1060 tPtr->tpos = 0;
1061 tPtr->cursor.h = font->height + abs(font->height-font->y);
1062 tPtr->cursor.y = 2;
1063 tPtr->cursor.x = 2;
1064 return;
1068 /* first, which direction? Most likely, newly clicked
1069 position will be close to previous */
1070 if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
1071 return;
1074 s = (dir? 0 : tb->nsections-1);
1075 if ( y >= tb->sections[s]._y
1076 && y <= tb->sections[s]._y + tb->sections[s].h) {
1077 goto _doneV;
1080 /* get the first (or last) section of the TextBlock that
1081 lies about the vertical click point */
1082 done = False;
1083 while (!done && tb) {
1085 if (tPtr->flags.monoFont && tb->graphic) {
1086 if( (dir?tb->next:tb->prior))
1087 tb = (dir?tb->next:tb->prior);
1088 continue;
1091 s = (dir? 0 : tb->nsections-1);
1092 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
1094 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
1095 ( y >= tb->sections[s]._y ) ) ) {
1096 done = True;
1097 } else {
1098 dir? s++ : s--;
1102 if (!done) {
1103 if ( (dir? tb->next : tb->prior)) {
1104 tb = (dir ? tb->next : tb->prior);
1105 } else {
1106 pos = tb->used;
1107 break; /* goto _doneH; */
1113 if (s<0 || s>=tb->nsections) {
1114 s = (dir? tb->nsections-1 : 0);
1117 _doneV:
1118 /* we have the line, which TextBlock on that line is it? */
1119 pos = (dir?0:tb->sections[s].begin);
1120 if (tPtr->flags.monoFont && tb->graphic) {
1121 TextBlock *hold = tb;
1122 tb = getFirstNonGraphicBlockFor(hold, dir);
1124 if(!tb) {
1125 tPtr->tpos = 0;
1126 tb = hold;
1127 s = 0;
1128 goto _doNothing;
1133 if(tb->blank)
1134 _w = 0;
1136 _y = tb->sections[s]._y;
1138 while (tb) {
1140 if (tPtr->flags.monoFont && tb->graphic) {
1141 tb = (dir ? tb->next : tb->prior);
1142 continue;
1145 if (dir) {
1146 if (tb->graphic) {
1147 if(tb->object)
1148 _w = WMWidgetWidth(tb->d.widget)-5;
1149 else
1150 _w = tb->d.pixmap->width-5;
1152 if (tb->sections[0].x + _w >= x)
1153 break;
1154 } else {
1155 text = &(tb->text[tb->sections[s].begin]);
1156 len = tb->sections[s].end - tb->sections[s].begin;
1157 _w = WMWidthOfString(tb->d.font, text, len);
1158 if (tb->sections[s].x + _w >= x)
1159 break;
1162 } else {
1163 if (tb->sections[s].x <= x)
1164 break;
1167 if ((dir? tb->next : tb->prior)) {
1168 TextBlock *nxt = (dir? tb->next : tb->prior);
1169 if (tPtr->flags.monoFont && nxt->graphic) {
1170 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1171 if (!nxt) {
1172 pos = (dir?0:tb->sections[s].begin);
1173 tPtr->cursor.x = tb->sections[s].x;
1174 goto _doneH;
1178 if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {
1179 /* this must be the last/first on this line. stop */
1180 pos = (dir? tb->sections[s].end : 0);
1181 tPtr->cursor.x = tb->sections[s].x;
1182 if (!tb->blank) {
1183 if (tb->graphic) {
1184 if(tb->object)
1185 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1186 else
1187 tPtr->cursor.x += tb->d.pixmap->width;
1188 } else if (pos > tb->sections[s].begin) {
1189 tPtr->cursor.x +=
1190 WMWidthOfString(tb->d.font,
1191 &(tb->text[tb->sections[s].begin]),
1192 pos - tb->sections[s].begin);
1195 goto _doneH;
1199 if ( (dir? tb->next : tb->prior)) {
1200 tb = (dir ? tb->next : tb->prior);
1201 } else {
1202 done = True;
1203 break;
1206 if (tb)
1207 s = (dir? 0 : tb->nsections-1);
1210 /* we have said TextBlock, now where within it? */
1211 if (tb) {
1212 if(tb->graphic) {
1213 int gw = (tb->object ?
1214 WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1216 tPtr->cursor.x = tb->sections[0].x;
1218 if(x > tPtr->cursor.x + gw/2) {
1219 pos = 1;
1220 tPtr->cursor.x += gw;
1221 } else {
1222 printf("first %d\n", tb->first);
1223 if(tb->prior) {
1224 if(tb->prior->graphic) pos = 1;
1225 else pos = tb->prior->used;
1226 tb = tb->prior;
1227 } else pos = 0;
1231 s = 0;
1232 goto _doneH;
1234 } else {
1235 WMFont *f = tb->d.font;
1236 len = tb->sections[s].end - tb->sections[s].begin;
1237 text = &(tb->text[tb->sections[s].begin]);
1239 _w = x - tb->sections[s].x;
1240 pos = 0;
1242 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1243 pos++;
1245 tPtr->cursor.x = tb->sections[s].x +
1246 (pos? WMWidthOfString(f, text, pos) : 0);
1248 pos += tb->sections[s].begin;
1252 _doneH:
1253 if(tb->graphic) {
1254 tPtr->tpos = (pos<=1)? pos : 0;
1255 } else {
1256 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1258 _doNothing:
1259 if (!tb)
1260 printf("...for this app will surely crash :-)\n");
1262 tPtr->currentTextBlock = tb;
1263 tPtr->cursor.h = tb->sections[s].h;
1264 tPtr->cursor.y = tb->sections[s]._y;
1266 /* scroll the bars if the cursor is not visible */
1267 if(tPtr->flags.editable && tPtr->cursor.x != -23) {
1268 if(tPtr->cursor.y+tPtr->cursor.h
1269 > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {
1270 tPtr->vpos +=
1271 (tPtr->cursor.y+tPtr->cursor.h+10
1272 - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));
1273 updateScrollers(tPtr);
1274 } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {
1275 tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);
1276 updateScrollers(tPtr);
1284 static void
1285 updateScrollers(Text *tPtr)
1288 if (tPtr->flags.frozen)
1289 return;
1291 if (tPtr->vS) {
1292 if (tPtr->docHeight <= tPtr->visible.h) {
1293 WMSetScrollerParameters(tPtr->vS, 0, 1);
1294 tPtr->vpos = 0;
1295 } else {
1296 float hmax = (float)(tPtr->docHeight);
1297 WMSetScrollerParameters(tPtr->vS,
1298 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1299 (float)tPtr->visible.h/hmax);
1301 } else tPtr->vpos = 0;
1303 if (tPtr->hS) {
1304 if (tPtr->docWidth <= tPtr->visible.w) {
1305 WMSetScrollerParameters(tPtr->hS, 0, 1);
1306 tPtr->hpos = 0;
1307 } else {
1308 float wmax = (float)(tPtr->docWidth);
1309 WMSetScrollerParameters(tPtr->hS,
1310 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1311 (float)tPtr->visible.w/wmax);
1313 } else tPtr->hpos = 0;
1316 static void
1317 scrollersCallBack(WMWidget *w, void *self)
1319 Text *tPtr = (Text *)self;
1320 Bool scroll = False;
1321 int which;
1323 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1324 return;
1326 if (w == tPtr->vS) {
1327 int height;
1328 height = tPtr->visible.h;
1330 which = WMGetScrollerHitPart(tPtr->vS);
1331 switch(which) {
1333 case WSDecrementLine:
1334 if (tPtr->vpos > 0) {
1335 if (tPtr->vpos>16) tPtr->vpos-=16;
1336 else tPtr->vpos=0;
1337 scroll=True;
1339 break;
1341 case WSIncrementLine: {
1342 int limit = tPtr->docHeight - height;
1343 if (tPtr->vpos < limit) {
1344 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1345 else tPtr->vpos=limit;
1346 scroll = True;
1349 break;
1351 case WSDecrementPage:
1352 if(((int)tPtr->vpos - (int)height) >= 0)
1353 tPtr->vpos -= height;
1354 else
1355 tPtr->vpos = 0;
1357 scroll = True;
1358 break;
1360 case WSIncrementPage:
1361 tPtr->vpos += height;
1362 if (tPtr->vpos > (tPtr->docHeight - height))
1363 tPtr->vpos = tPtr->docHeight - height;
1364 scroll = True;
1365 break;
1368 case WSKnob:
1369 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1370 * (float)(tPtr->docHeight - height);
1371 scroll = True;
1372 break;
1374 case WSKnobSlot:
1375 case WSNoPart:
1376 break;
1378 scroll = (tPtr->vpos != tPtr->prevVpos);
1379 tPtr->prevVpos = tPtr->vpos;
1383 if (w == tPtr->hS) {
1384 int width = tPtr->visible.w;
1386 which = WMGetScrollerHitPart(tPtr->hS);
1387 switch(which) {
1389 case WSDecrementLine:
1390 if (tPtr->hpos > 0) {
1391 if (tPtr->hpos>16) tPtr->hpos-=16;
1392 else tPtr->hpos=0;
1393 scroll=True;
1394 }break;
1396 case WSIncrementLine: {
1397 int limit = tPtr->docWidth - width;
1398 if (tPtr->hpos < limit) {
1399 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1400 else tPtr->hpos=limit;
1401 scroll = True;
1403 }break;
1405 case WSDecrementPage:
1406 if(((int)tPtr->hpos - (int)width) >= 0)
1407 tPtr->hpos -= width;
1408 else
1409 tPtr->hpos = 0;
1411 scroll = True;
1412 break;
1414 case WSIncrementPage:
1415 tPtr->hpos += width;
1416 if (tPtr->hpos > (tPtr->docWidth - width))
1417 tPtr->hpos = tPtr->docWidth - width;
1418 scroll = True;
1419 break;
1422 case WSKnob:
1423 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1424 * (float)(tPtr->docWidth - width);
1425 scroll = True;
1426 break;
1428 case WSKnobSlot:
1429 case WSNoPart:
1430 break;
1432 scroll = (tPtr->hpos != tPtr->prevHpos);
1433 tPtr->prevHpos = tPtr->hpos;
1436 if (scroll) {
1437 updateScrollers(tPtr);
1438 paintText(tPtr);
1444 typedef struct {
1445 TextBlock *tb;
1446 unsigned short begin, end; /* what part of the text block */
1447 } myLineItems;
1450 static int
1451 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1453 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1454 WMFont *font;
1455 char *text;
1456 TextBlock *tb, *tbsame=NULL;
1458 if(!items || nitems == 0)
1459 return 0;
1461 for(i=0; i<nitems; i++) {
1462 tb = items[i].tb;
1464 if (tb->graphic) {
1465 if (!tPtr->flags.monoFont) {
1466 if(tb->object) {
1467 WMWidget *wdt = tb->d.widget;
1468 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1469 if (tPtr->flags.alignment != WALeft)
1470 lw += WMWidgetWidth(wdt);
1471 } else {
1472 line_height = WMAX(line_height,
1473 tb->d.pixmap->height + max_d);
1474 if (tPtr->flags.alignment != WALeft)
1475 lw += tb->d.pixmap->width;
1479 } else {
1480 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1481 /*max_d = WMAX(max_d, abs(font->height-font->y));*/
1482 max_d = 2;
1483 line_height = WMAX(line_height, font->height + max_d);
1484 text = &(tb->text[items[i].begin]);
1485 len = items[i].end - items[i].begin;
1486 if (tPtr->flags.alignment != WALeft)
1487 lw += WMWidthOfString(font, text, len);
1491 if (tPtr->flags.alignment == WARight) {
1492 j = tPtr->visible.w - lw;
1493 } else if (tPtr->flags.alignment == WACenter) {
1494 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1497 for(i=0; i<nitems; i++) {
1498 tb = items[i].tb;
1500 if (tbsame == tb) { /* extend it, since it's on same line */
1501 tb->sections[tb->nsections-1].end = items[i].end;
1502 n = tb->nsections-1;
1503 } else {
1504 tb->sections = wrealloc(tb->sections,
1505 (++tb->nsections)*sizeof(Section));
1506 n = tb->nsections-1;
1507 tb->sections[n]._y = y + max_d;
1508 tb->sections[n].max_d = max_d;
1509 tb->sections[n].x = x+j;
1510 tb->sections[n].h = line_height;
1511 tb->sections[n].begin = items[i].begin;
1512 tb->sections[n].end = items[i].end;
1515 tb->sections[n].last = (i+1 == nitems);
1517 if (tb->graphic) {
1518 if (!tPtr->flags.monoFont) {
1519 if(tb->object) {
1520 WMWidget *wdt = tb->d.widget;
1521 tb->sections[n].y = max_d + y
1522 + line_height - WMWidgetHeight(wdt);
1523 tb->sections[n].w = WMWidgetWidth(wdt);
1524 } else {
1525 tb->sections[n].y = y + line_height
1526 + max_d - tb->d.pixmap->height;
1527 tb->sections[n].w = tb->d.pixmap->width;
1529 x += tb->sections[n].w;
1531 } else {
1532 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1533 len = items[i].end - items[i].begin;
1534 text = &(tb->text[items[i].begin]);
1536 tb->sections[n].y = y+line_height-font->y;
1537 tb->sections[n].w =
1538 WMWidthOfString(font,
1539 &(tb->text[tb->sections[n].begin]),
1540 tb->sections[n].end - tb->sections[n].begin);
1542 x += WMWidthOfString(font, text, len);
1545 tbsame = tb;
1548 return line_height;
1553 static void
1554 layOutDocument(Text *tPtr)
1556 TextBlock *tb;
1557 myLineItems *items = NULL;
1558 unsigned int itemsSize=0, nitems=0, begin, end;
1559 WMFont *font;
1560 unsigned int x, y=0, lw = 0, width=0, bmargin;
1561 char *start=NULL, *mark=NULL;
1563 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1564 return;
1566 assert(tPtr->visible.w > 20);
1568 tPtr->docWidth = tPtr->visible.w;
1569 x = tPtr->margins[tb->marginN].first;
1570 bmargin = tPtr->margins[tb->marginN].body;
1572 /* only partial layOut needed: re-Lay only affected textblocks */
1573 if (tPtr->flags.laidOut) {
1574 tb = tPtr->currentTextBlock;
1576 /* search backwards for textblocks on same line */
1577 while (tb->prior) {
1578 if (!tb->sections || tb->nsections<1) {
1579 tb = tPtr->firstTextBlock;
1580 tPtr->flags.laidOut = False;
1581 y = 0;
1582 goto _layOut;
1585 if(!tb->prior->sections || tb->prior->nsections<1) {
1586 tb = tPtr->firstTextBlock;
1587 tPtr->flags.laidOut = False;
1588 y = 0;
1589 goto _layOut;
1592 if (tb->sections[0]._y !=
1593 tb->prior->sections[tb->prior->nsections-1]._y) {
1594 break;
1596 tb = tb->prior;
1599 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1600 y = tb->prior->sections[tb->prior->nsections-1]._y +
1601 tb->prior->sections[tb->prior->nsections-1].h -
1602 tb->prior->sections[tb->prior->nsections-1].max_d;
1603 } else {
1604 y = 0;
1608 _layOut:
1609 while (tb) {
1611 if (tb->sections && tb->nsections>0) {
1612 wfree(tb->sections);
1613 tb->sections = NULL;
1614 tb->nsections = 0;
1617 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1618 TextBlock *next = tb->next;
1619 tPtr->currentTextBlock = tb;
1620 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1621 tb = next;
1622 tb->first = True;
1623 continue;
1626 if (tb->first && tb != tPtr->firstTextBlock) {
1627 y += layOutLine(tPtr, items, nitems, x, y);
1628 x = tPtr->margins[tb->marginN].first;
1629 bmargin = tPtr->margins[tb->marginN].body;
1630 nitems = 0;
1631 lw = 0;
1634 if (tb->graphic) {
1635 if (!tPtr->flags.monoFont) {
1636 if(tb->object)
1637 width = WMWidgetWidth(tb->d.widget);
1638 else
1639 width = tb->d.pixmap->width;
1641 if (width > tPtr->docWidth)
1642 tPtr->docWidth = width;
1644 lw += width;
1645 if (lw >= tPtr->visible.w - x ) {
1646 y += layOutLine(tPtr, items, nitems, x, y);
1647 nitems = 0;
1648 x = bmargin;
1649 lw = width;
1652 if(nitems + 1> itemsSize) {
1653 items = wrealloc(items,
1654 (++itemsSize)*sizeof(myLineItems));
1657 items[nitems].tb = tb;
1658 items[nitems].begin = 0;
1659 items[nitems].end = 0;
1660 nitems++;
1663 } else if ((start = tb->text)) {
1664 begin = end = 0;
1665 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1667 while (start) {
1668 mark = strchr(start, ' ');
1669 if (mark) {
1670 end += (int)(mark-start)+1;
1671 start = mark+1;
1672 } else {
1673 end += strlen(start);
1674 start = mark;
1677 if (end > tb->used)
1678 end = tb->used;
1680 if (end-begin > 0) {
1682 width = WMWidthOfString(font,
1683 &tb->text[begin], end-begin);
1685 /* if it won't fit, char wrap it */
1686 if (width >= tPtr->visible.w) {
1687 char *t = &tb->text[begin];
1688 int l=end-begin, i=0;
1689 do {
1690 width = WMWidthOfString(font, t, ++i);
1691 } while (width < tPtr->visible.w && i < l);
1692 if(i>2) i--;
1693 end = begin+i;
1694 start = &tb->text[end];
1697 lw += width;
1700 if (lw >= tPtr->visible.w - x) {
1701 y += layOutLine(tPtr, items, nitems, x, y);
1702 lw = width;
1703 x = bmargin;
1704 nitems = 0;
1707 if(nitems + 1 > itemsSize) {
1708 items = wrealloc(items,
1709 (++itemsSize)*sizeof(myLineItems));
1712 items[nitems].tb = tb;
1713 items[nitems].begin = begin;
1714 items[nitems].end = end;
1715 nitems++;
1717 begin = end;
1722 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1723 if(0&&tPtr->flags.laidOut
1724 && tb->next && tb->next->sections && tb->next->nsections>0
1725 && (tPtr->vpos + tPtr->visible.h
1726 < tb->next->sections[0]._y)) {
1727 if(tPtr->lastTextBlock->sections
1728 && tPtr->lastTextBlock->nsections > 0 ) {
1729 TextBlock *ltb = tPtr->lastTextBlock;
1730 int ly = ltb->sections[ltb->nsections-1]._y;
1731 int lh = ltb->sections[ltb->nsections-1].h;
1732 int ss, sd;
1734 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1735 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1737 y += layOutLine(tPtr, items, nitems, x, y);
1738 ss= ly+lh-y;
1739 sd = tPtr->docHeight-y;
1741 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1742 y += tb->next->sections[0]._y-y;
1743 nitems = 0;
1744 printf("nitems%d\n", nitems);
1745 if(ss-sd!=0)
1746 y = tPtr->docHeight+ss-sd;
1748 break;
1749 } else {
1750 tPtr->flags.laidOut = False;
1754 tb = tb->next;
1758 if (nitems > 0)
1759 y += layOutLine(tPtr, items, nitems, x, y);
1761 if (tPtr->docHeight != y+10) {
1762 tPtr->docHeight = y+10;
1763 updateScrollers(tPtr);
1766 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1767 XEvent event;
1769 tPtr->flags.horizOnDemand = True;
1770 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1771 event.type = Expose;
1772 handleEvents(&event, (void *)tPtr);
1774 } else if(tPtr->docWidth <= tPtr->visible.w
1775 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1776 tPtr->flags.horizOnDemand = False;
1777 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1780 tPtr->flags.laidOut = True;
1782 if(items && itemsSize > 0)
1783 wfree(items);
1788 static void
1789 textDidResize(W_ViewDelegate *self, WMView *view)
1791 Text *tPtr = (Text *)view->self;
1792 unsigned short w = tPtr->view->size.width;
1793 unsigned short h = tPtr->view->size.height;
1794 unsigned short rh = 0, vw = 0, rel;
1796 rel = (tPtr->flags.relief == WRFlat);
1798 if (tPtr->ruler && tPtr->flags.rulerShown) {
1799 WMMoveWidget(tPtr->ruler, 2, 2);
1800 WMResizeWidget(tPtr->ruler, w - 4, 40);
1801 rh = 40;
1804 if (tPtr->vS) {
1805 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1806 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1807 vw = 20;
1808 WMSetRulerOffset(tPtr->ruler,22);
1809 } else WMSetRulerOffset(tPtr->ruler, 2);
1811 if (tPtr->hS) {
1812 if (tPtr->vS) {
1813 WMMoveWidget(tPtr->hS, vw, h - 21);
1814 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1815 } else {
1816 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1817 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1821 tPtr->visible.x = (tPtr->vS)?24:4;
1822 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1823 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1824 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1825 tPtr->visible.h -= (tPtr->hS)?20:0;
1826 tPtr->margins[0].right = tPtr->visible.w;
1828 if (tPtr->view->flags.realized) {
1830 if (tPtr->db) {
1831 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1832 tPtr->db = (Pixmap) NULL;
1835 if (tPtr->visible.w < 40)
1836 tPtr->visible.w = 40;
1837 if (tPtr->visible.h < 20)
1838 tPtr->visible.h = 20;
1840 if(!tPtr->db) {
1841 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1842 tPtr->view->window, tPtr->visible.w,
1843 tPtr->visible.h, tPtr->view->screen->depth);
1847 WMThawText(tPtr);
1850 W_ViewDelegate _TextViewDelegate =
1852 NULL,
1853 NULL,
1854 textDidResize,
1855 NULL,
1858 #define TEXT_BUFFER_INCR 8
1859 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1861 static void
1862 clearText(Text *tPtr)
1864 tPtr->vpos = tPtr->hpos = 0;
1865 tPtr->docHeight = tPtr->docWidth = 0;
1866 tPtr->cursor.x = -23;
1868 if (!tPtr->firstTextBlock)
1869 return;
1871 while (tPtr->currentTextBlock)
1872 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1874 tPtr->firstTextBlock = NULL;
1875 tPtr->currentTextBlock = NULL;
1876 tPtr->lastTextBlock = NULL;
1877 WMEmptyArray(tPtr->gfxItems);
1880 /* possibly remove a single character from the currentTextBlock,
1881 or if there's a selection, remove it...
1882 note that Delete and Backspace are treated differently */
1883 static void
1884 deleteTextInteractively(Text *tPtr, KeySym ksym)
1886 TextBlock *tb;
1887 Bool back = (Bool) (ksym == XK_BackSpace);
1888 Bool done = 1, wasFirst = 0;
1890 if (!tPtr->flags.editable)
1891 return;
1893 if ( !(tb = tPtr->currentTextBlock) )
1894 return;
1896 if (tPtr->flags.ownsSelection) {
1897 if(removeSelection(tPtr))
1898 layOutDocument(tPtr);
1899 return;
1902 wasFirst = tb->first;
1903 if (back && tPtr->tpos < 1) {
1904 if (tb->prior) {
1905 if(tb->prior->blank) {
1906 tPtr->currentTextBlock = tb->prior;
1907 WMRemoveTextBlock(tPtr);
1908 tPtr->currentTextBlock = tb;
1909 tb->first = True;
1910 layOutDocument(tPtr);
1911 return;
1912 } else {
1913 if(tb->blank) {
1914 TextBlock *prior = tb->prior;
1915 tPtr->currentTextBlock = tb;
1916 WMRemoveTextBlock(tPtr);
1917 tb = prior;
1918 } else {
1919 tb = tb->prior;
1922 if(tb->graphic)
1923 tPtr->tpos = 1;
1924 else
1925 tPtr->tpos = tb->used;
1927 tPtr->currentTextBlock = tb;
1928 done = 1;
1929 if(wasFirst) {
1930 if(tb->next)
1931 tb->next->first = False;
1932 layOutDocument(tPtr);
1933 return;
1939 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1940 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1941 if (back)
1942 tPtr->tpos--;
1943 memmove(&(tb->text[tPtr->tpos]),
1944 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1945 tb->used--;
1946 done = 0;
1949 /* if there are no characters left to back over in the textblock,
1950 but it still has characters to the right of the cursor: */
1951 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1952 || tb->graphic) {
1954 /* no more chars, and it's marked as blank? */
1955 if(tb->blank) {
1956 TextBlock *sibling = (back? tb->prior : tb->next);
1958 if(tb->used == 0 || tb->graphic)
1959 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1961 if (sibling) {
1962 tPtr->currentTextBlock = sibling;
1963 if(tb->graphic)
1964 tPtr->tpos = (back? 1 : 0);
1965 else
1966 tPtr->tpos = (back? sibling->used : 0);
1968 /* no more chars, so mark it as blank */
1969 } else if(tb->used == 0) {
1970 tb->blank = 1;
1971 } else if(tb->graphic) {
1972 Bool hasNext = (Bool)(tb->next);
1974 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1975 if(hasNext) {
1976 tPtr->tpos = 0;
1977 } else if(tPtr->currentTextBlock) {
1978 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1979 1 : tPtr->currentTextBlock->used);
1981 } else printf("DEBUG: unaccounted for... catch this!\n");
1984 layOutDocument(tPtr);
1988 static void
1989 insertTextInteractively(Text *tPtr, char *text, int len)
1991 TextBlock *tb;
1992 char *newline = NULL;
1994 if (!tPtr->flags.editable) {
1995 return;
1998 if (len < 1 || !text)
1999 return;
2002 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
2003 return;
2006 if (tPtr->flags.ownsSelection)
2007 removeSelection(tPtr);
2010 if (tPtr->flags.ignoreNewLine) {
2011 int i;
2012 for(i=0; i<len; i++) {
2013 if (text[i] == '\n')
2014 text[i] = ' ';
2018 tb = tPtr->currentTextBlock;
2019 if (!tb || tb->graphic) {
2020 tPtr->tpos = 0;
2021 WMAppendTextStream(tPtr, text);
2022 layOutDocument(tPtr);
2023 return;
2026 if ((newline = strchr(text, '\n'))) {
2027 int nlen = (int)(newline-text);
2028 int s = tb->used - tPtr->tpos;
2030 if (!tb->blank && nlen>0) {
2031 char *save=NULL;
2033 if (s > 0) {
2034 save = wmalloc(s);
2035 memcpy(save, &tb->text[tPtr->tpos], s);
2036 tb->used -= (tb->used - tPtr->tpos);
2038 insertTextInteractively(tPtr, text, nlen);
2039 newline++;
2040 WMAppendTextStream(tPtr, newline);
2041 if (s>0) {
2042 insertTextInteractively(tPtr, save, s);
2043 wfree(save);
2045 } else {
2046 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2047 && !tb->graphic && tb->text) {
2049 unsigned short savePos = tPtr->tpos;
2050 void *ntb = WMCreateTextBlockWithText(
2051 tPtr, &tb->text[tPtr->tpos],
2052 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2054 if(tb->sections[0].end == tPtr->tpos)
2055 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2056 NULL, tb->d.font, tb->color, True, 0));
2058 tb->used = savePos;
2059 WMAppendTextBlock(tPtr, ntb);
2060 tPtr->tpos = 0;
2062 } else if (tPtr->tpos == tb->used) {
2063 if(tPtr->flags.indentNewLine) {
2064 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2065 " ", tb->d.font, tb->color, True, 4));
2066 tPtr->tpos = 4;
2067 } else {
2068 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2069 NULL, tb->d.font, tb->color, True, 0));
2070 tPtr->tpos = 0;
2072 } else if (tPtr->tpos == 0) {
2073 if(tPtr->flags.indentNewLine) {
2074 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2075 " ", tb->d.font, tb->color, True, 4));
2076 } else {
2077 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2078 NULL, tb->d.font, tb->color, True, 0));
2080 tPtr->tpos = 0;
2081 if(tPtr->currentTextBlock->next)
2082 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2085 } else {
2086 if (tb->used + len >= tb->allocated) {
2087 tb->allocated = reqBlockSize(tb->used+len);
2088 tb->text = wrealloc(tb->text, tb->allocated);
2091 if (tb->blank) {
2092 memcpy(tb->text, text, len);
2093 tb->used = len;
2094 tPtr->tpos = len;
2095 tb->text[tb->used] = 0;
2096 tb->blank = False;
2098 } else {
2099 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2100 tb->used-tPtr->tpos+1);
2101 memmove(&tb->text[tPtr->tpos], text, len);
2102 tb->used += len;
2103 tPtr->tpos += len;
2104 tb->text[tb->used] = 0;
2109 layOutDocument(tPtr);
2113 static void
2114 selectRegion(Text *tPtr, int x, int y)
2117 if (x < 0 || y < 0)
2118 return;
2120 y += (tPtr->flags.rulerShown? 40: 0);
2121 y += tPtr->vpos;
2122 if (y>10)
2123 y -= 10; /* the original offset */
2125 x -= tPtr->visible.x-2;
2126 if (x<0)
2127 x=0;
2129 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2130 tPtr->sel.w = abs(tPtr->clicked.x - x);
2131 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2132 tPtr->sel.h = abs(tPtr->clicked.y - y);
2134 tPtr->flags.ownsSelection = True;
2135 paintText(tPtr);
2139 static void
2140 releaseSelection(Text *tPtr)
2142 TextBlock *tb = tPtr->firstTextBlock;
2144 while(tb) {
2145 tb->selected = False;
2146 tb = tb->next;
2148 tPtr->flags.ownsSelection = False;
2149 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2150 CurrentTime);
2152 paintText(tPtr);
2156 WMData*
2157 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2158 Atom *type)
2160 Text *tPtr = view->self;
2161 Display *dpy = tPtr->view->screen->display;
2162 Atom _TARGETS;
2163 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2164 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2165 WMData *data = NULL;
2168 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2169 char *text = WMGetTextSelectedStream(tPtr);
2171 if (text) {
2172 data = WMCreateDataWithBytes(text, strlen(text));
2173 WMSetDataFormat(data, TYPETEXT);
2175 *type = target;
2176 return data;
2177 } else printf("didn't get it\n");
2179 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2180 if (target == _TARGETS) {
2181 Atom *ptr;
2183 ptr = wmalloc(4 * sizeof(Atom));
2184 ptr[0] = _TARGETS;
2185 ptr[1] = XA_STRING;
2186 ptr[2] = TEXT;
2187 ptr[3] = COMPOUND_TEXT;
2189 data = WMCreateDataWithBytes(ptr, 4*4);
2190 WMSetDataFormat(data, 32);
2192 *type = target;
2193 return data;
2196 return NULL;
2200 static void
2201 lostHandler(WMView *view, Atom selection, void *cdata)
2203 releaseSelection((WMText *)view->self);
2207 static WMSelectionProcs selectionHandler = {
2208 requestHandler, lostHandler, NULL
2212 static void
2213 ownershipObserver(void *observerData, WMNotification *notification)
2215 if (observerData != WMGetNotificationClientData(notification))
2216 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2220 static void
2221 autoSelectText(Text *tPtr, int clicks)
2223 int x, start;
2224 TextBlock *tb;
2225 char *mark = NULL, behind, ahead;
2227 if(!(tb = tPtr->currentTextBlock))
2228 return;
2230 if(clicks == 2) {
2233 switch(tb->text[tPtr->tpos]) {
2234 case ' ': return;
2236 case '<': case '>': behind = '<'; ahead = '>'; break;
2237 case '{': case '}': behind = '{'; ahead = '}'; break;
2238 case '[': case ']': behind = '['; ahead = ']'; break;
2240 default: behind = ahead = ' ';
2243 tPtr->sel.y = tPtr->cursor.y+5;
2244 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2246 if(tb->graphic) {
2247 tPtr->sel.x = tb->sections[0].x;
2248 tPtr->sel.w = tb->sections[0].w;
2249 } else {
2250 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2252 start = tPtr->tpos;
2253 while(start > 0 && tb->text[start-1] != behind)
2254 start--;
2256 x = tPtr->cursor.x;
2257 if(tPtr->tpos > start){
2258 x -= WMWidthOfString(font, &tb->text[start],
2259 tPtr->tpos - start);
2261 tPtr->sel.x = (x<0?0:x)+1;
2263 if((mark = strchr(&tb->text[start], ahead))) {
2264 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2265 (int)(mark - &tb->text[start]));
2266 } else if(tb->used > start) {
2267 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2268 tb->used - start);
2272 } else if(clicks == 3) {
2273 TextBlock *cur = tb;
2275 while(tb && !tb->first) {
2276 tb = tb->prior;
2278 tPtr->sel.y = tb->sections[0]._y;
2280 tb = cur;
2281 while(tb->next && !tb->next->first) {
2282 tb = tb->next;
2284 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2285 + 5 - tPtr->sel.y;
2287 tPtr->sel.x = 0;
2288 tPtr->sel.w = tPtr->docWidth;
2289 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2292 if (!tPtr->flags.ownsSelection) {
2293 WMCreateSelectionHandler(tPtr->view,
2294 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2295 tPtr->flags.ownsSelection = True;
2297 paintText(tPtr);
2302 static void
2303 fontChanged(void *observerData, WMNotification *notification)
2305 WMText *tPtr = (WMText *) observerData;
2306 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2307 printf("fontChanged\n");
2309 if(!tPtr || !font)
2310 return;
2312 if (tPtr->flags.ownsSelection)
2313 WMSetTextSelectionFont(tPtr, font);
2317 static void
2318 handleTextKeyPress(Text *tPtr, XEvent *event)
2320 char buffer[64];
2321 KeySym ksym;
2322 int control_pressed = False;
2323 TextBlock *tb = NULL;
2325 if (((XKeyEvent *) event)->state & ControlMask)
2326 control_pressed = True;
2327 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2329 switch(ksym) {
2331 case XK_Home:
2332 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2333 tPtr->tpos = 0;
2334 updateCursorPosition(tPtr);
2335 paintText(tPtr);
2336 break;
2338 case XK_End:
2339 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2340 if(tPtr->currentTextBlock->graphic)
2341 tPtr->tpos = 1;
2342 else
2343 tPtr->tpos = tPtr->currentTextBlock->used;
2345 updateCursorPosition(tPtr);
2346 paintText(tPtr);
2347 break;
2349 case XK_Left:
2350 if(!(tb = tPtr->currentTextBlock))
2351 break;
2352 if(tb->graphic)
2353 goto L_imaGFX;
2355 if(tPtr->tpos==0) {
2356 L_imaGFX:
2357 if(tb->prior) {
2358 tPtr->currentTextBlock = tb->prior;
2359 if(tPtr->currentTextBlock->graphic)
2360 tPtr->tpos = 1;
2361 else
2362 tPtr->tpos = tPtr->currentTextBlock->used;
2364 if(!tb->first && tPtr->tpos > 0)
2365 tPtr->tpos--;
2366 } else tPtr->tpos = 0;
2367 } else tPtr->tpos--;
2368 updateCursorPosition(tPtr);
2369 paintText(tPtr);
2370 break;
2372 case XK_Right:
2373 if(!(tb = tPtr->currentTextBlock))
2374 break;
2375 if(tb->graphic)
2376 goto R_imaGFX;
2377 if(tPtr->tpos == tb->used) {
2378 R_imaGFX:
2379 if(tb->next) {
2380 tPtr->currentTextBlock = tb->next;
2381 tPtr->tpos = 0;
2382 if(!tb->next->first && tb->next->used>0)
2383 tPtr->tpos++;
2384 } else {
2385 if(tb->graphic)
2386 tPtr->tpos = 1;
2387 else
2388 tPtr->tpos = tb->used;
2390 } else tPtr->tpos++;
2391 updateCursorPosition(tPtr);
2392 paintText(tPtr);
2393 break;
2395 case XK_Down:
2396 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2397 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2398 paintText(tPtr);
2399 break;
2401 case XK_Up:
2402 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2403 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2404 paintText(tPtr);
2405 break;
2407 case XK_BackSpace:
2408 case XK_Delete:
2409 #ifdef XK_KP_Delete
2410 case XK_KP_Delete:
2411 #endif
2412 deleteTextInteractively(tPtr, ksym);
2413 updateCursorPosition(tPtr);
2414 paintText(tPtr);
2415 break;
2417 case XK_Control_R :
2418 case XK_Control_L :
2419 control_pressed = True;
2420 break;
2422 case XK_Tab:
2423 insertTextInteractively(tPtr, " ", 4);
2424 updateCursorPosition(tPtr);
2425 paintText(tPtr);
2426 break;
2428 case XK_Return:
2429 *buffer = '\n';
2430 default:
2431 if (*buffer != 0 && !control_pressed) {
2432 insertTextInteractively(tPtr, buffer, strlen(buffer));
2433 updateCursorPosition(tPtr);
2434 paintText(tPtr);
2436 } else if (control_pressed && ksym==XK_r) {
2437 Bool i = !tPtr->flags.rulerShown;
2438 WMShowTextRuler(tPtr, i);
2439 tPtr->flags.rulerShown = i;
2440 } else if (control_pressed && *buffer == '\a') {
2441 XBell(tPtr->view->screen->display, 0);
2442 } else {
2443 WMRelayToNextResponder(tPtr->view, event);
2447 if (!control_pressed && tPtr->flags.ownsSelection) {
2448 releaseSelection(tPtr);
2453 static void
2454 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2455 void *cdata, WMData *data)
2457 Text *tPtr = (Text *)view->self;
2458 char *text;
2460 tPtr->flags.waitingForSelection = 0;
2462 if (data) {
2463 text = (char*)WMDataBytes(data);
2465 if (tPtr->parser) {
2466 (tPtr->parser) (tPtr, (void *) text);
2467 layOutDocument(tPtr);
2468 } else insertTextInteractively(tPtr, text, strlen(text));
2469 updateCursorPosition(tPtr);
2470 paintText(tPtr);
2472 } else {
2473 int n;
2475 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2477 if (text) {
2478 text[n] = 0;
2479 if (tPtr->parser) {
2480 (tPtr->parser) (tPtr, (void *) text);
2481 layOutDocument(tPtr);
2482 } else insertTextInteractively(tPtr, text, n);
2483 updateCursorPosition(tPtr);
2484 paintText(tPtr);
2486 XFree(text);
2494 static void
2495 handleActionEvents(XEvent *event, void *data)
2497 Text *tPtr = (Text *)data;
2498 Display *dpy = event->xany.display;
2499 KeySym ksym;
2502 switch (event->type) {
2503 case KeyPress:
2504 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2505 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2506 tPtr->flags.extendSelection = True;
2507 return;
2510 if (tPtr->flags.focused) {
2511 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2512 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2513 GrabModeAsync, GrabModeAsync, None,
2514 tPtr->view->screen->invisibleCursor, CurrentTime);
2515 tPtr->flags.pointerGrabbed = True;
2516 handleTextKeyPress(tPtr, event);
2518 } break;
2520 case KeyRelease:
2521 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2522 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2523 tPtr->flags.extendSelection = False;
2524 return;
2525 /* end modify flag so selection can be extended */
2527 break;
2530 case MotionNotify:
2532 if (tPtr->flags.pointerGrabbed) {
2533 tPtr->flags.pointerGrabbed = False;
2534 XUngrabPointer(dpy, CurrentTime);
2537 if(tPtr->flags.waitingForSelection)
2538 break;
2540 if ((event->xmotion.state & Button1Mask)) {
2542 if (WMIsDraggingFromView(tPtr->view)) {
2543 WMDragImageFromView(tPtr->view, event);
2544 break;
2547 if (!tPtr->flags.ownsSelection) {
2548 WMCreateSelectionHandler(tPtr->view,
2549 XA_PRIMARY, event->xbutton.time,
2550 &selectionHandler, NULL);
2551 tPtr->flags.ownsSelection = True;
2553 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2554 break;
2557 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2558 break;
2561 case ButtonPress:
2563 if (tPtr->flags.pointerGrabbed) {
2564 tPtr->flags.pointerGrabbed = False;
2565 XUngrabPointer(dpy, CurrentTime);
2566 break;
2569 if (tPtr->flags.waitingForSelection)
2570 break;
2572 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2573 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2574 return;
2577 if (tPtr->flags.ownsSelection)
2578 releaseSelection(tPtr);
2581 if (event->xbutton.button == Button1) {
2582 TextBlock *tb = tPtr->currentTextBlock;
2584 if(WMIsDoubleClick(event)) {
2586 tPtr->lastClickTime = event->xbutton.time;
2587 if(tb && tb->graphic && !tb->object) {
2588 if(tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2589 char *desc;
2591 desc = wmalloc(tb->used+1);
2592 memcpy(desc, tb->text, tb->used);
2593 desc[tb->used] = 0;
2594 (*tPtr->delegate->didDoubleClickOnPicture)(tPtr->delegate, desc);
2595 wfree(desc);
2597 } else {
2598 autoSelectText(tPtr, 2);
2600 break;
2601 } else if (event->xbutton.time - tPtr->lastClickTime
2602 < WINGsConfiguration.doubleClickDelay) {
2603 tPtr->lastClickTime = event->xbutton.time;
2604 autoSelectText(tPtr, 3);
2605 break;
2608 if (!tPtr->flags.focused) {
2609 WMSetFocusToWidget(tPtr);
2610 tPtr->flags.focused = True;
2611 } else if (tb && tPtr->flags.isOverGraphic &&
2612 tb->graphic && !tb->object && tb->d.pixmap) {
2614 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2615 WMDragImageFromView(tPtr->view, event);
2616 break;
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;
2687 if (WMIsDraggingFromView(tPtr->view))
2688 WMDragImageFromView(tPtr->view, event);
2694 static void
2695 handleEvents(XEvent *event, void *data)
2697 Text *tPtr = (Text *)data;
2699 switch(event->type) {
2700 case Expose:
2702 if (event->xexpose.count!=0)
2703 break;
2705 if(tPtr->hS) {
2706 if (!(W_VIEW(tPtr->hS))->flags.realized)
2707 WMRealizeWidget(tPtr->hS);
2710 if(tPtr->vS) {
2711 if (!(W_VIEW(tPtr->vS))->flags.realized)
2712 WMRealizeWidget(tPtr->vS);
2715 if(tPtr->ruler) {
2716 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2717 WMRealizeWidget(tPtr->ruler);
2721 if(!tPtr->db)
2722 textDidResize(tPtr->view->delegate, tPtr->view);
2724 paintText(tPtr);
2725 break;
2727 case FocusIn:
2728 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2729 != tPtr->view)
2730 return;
2731 tPtr->flags.focused = True;
2732 #if DO_BLINK
2733 if (tPtr->flags.editable && !tPtr->timerID) {
2734 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2735 blinkCursor, tPtr);
2737 #endif
2739 break;
2741 case FocusOut:
2742 tPtr->flags.focused = False;
2743 paintText(tPtr);
2744 #if DO_BLINK
2745 if (tPtr->timerID) {
2746 WMDeleteTimerHandler(tPtr->timerID);
2747 tPtr->timerID = NULL;
2749 #endif
2750 break;
2753 case DestroyNotify:
2754 clearText(tPtr);
2755 if(tPtr->db)
2756 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2757 if(tPtr->gfxItems)
2758 WMEmptyArray(tPtr->gfxItems);
2759 #if DO_BLINK
2760 if (tPtr->timerID)
2761 WMDeleteTimerHandler(tPtr->timerID);
2762 #endif
2763 WMReleaseFont(tPtr->dFont);
2764 WMReleaseColor(tPtr->dColor);
2765 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2766 WMRemoveNotificationObserver(tPtr);
2768 WMFreeArray(tPtr->xdndSourceTypes);
2769 WMFreeArray(tPtr->xdndDestinationTypes);
2771 wfree(tPtr);
2773 break;
2779 static void
2780 insertPlainText(Text *tPtr, char *text)
2782 char *start, *mark;
2783 void *tb = NULL;
2785 start = text;
2786 while (start) {
2787 mark = strchr(start, '\n');
2788 if (mark) {
2789 tb = WMCreateTextBlockWithText(tPtr,
2790 start, tPtr->dFont,
2791 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2792 start = mark+1;
2793 tPtr->flags.first = True;
2794 } else {
2795 if (start && strlen(start)) {
2796 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2797 tPtr->dColor, tPtr->flags.first, strlen(start));
2798 } else tb = NULL;
2799 tPtr->flags.first = False;
2800 start = mark;
2803 if (tPtr->flags.prepend)
2804 WMPrependTextBlock(tPtr, tb);
2805 else
2806 WMAppendTextBlock(tPtr, tb);
2811 static void
2812 rulerMoveCallBack(WMWidget *w, void *self)
2814 Text *tPtr = (Text *)self;
2816 if (!tPtr)
2817 return;
2818 if (W_CLASS(tPtr) != WC_Text)
2819 return;
2821 paintText(tPtr);
2825 static void
2826 rulerReleaseCallBack(WMWidget *w, void *self)
2828 Text *tPtr = (Text *)self;
2830 if (!tPtr)
2831 return;
2832 if (W_CLASS(tPtr) != WC_Text)
2833 return;
2835 WMThawText(tPtr);
2836 return;
2840 static WMArray*
2841 dropDataTypes(WMView *self)
2843 return ((Text*)self->self)->xdndSourceTypes;
2847 static WMDragOperationType
2848 wantedDropOperation(WMView *self)
2850 return WDOperationCopy;
2854 static Bool
2855 acceptDropOperation(WMView *self, WMDragOperationType allowedOperation)
2857 return (allowedOperation == WDOperationCopy);
2861 static WMData*
2862 fetchDragData(WMView *self, char *type)
2864 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2865 char *desc;
2866 WMData *data;
2868 if (strcmp(type, "text/plain")) {
2869 if (!tb)
2870 return NULL;
2872 desc = wmalloc(tb->used+1);
2873 memcpy(desc, tb->text, tb->used);
2874 desc[tb->used] = 0;
2875 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2877 wfree(desc);
2879 return data;
2882 return NULL;
2886 static WMDragSourceProcs _DragSourceProcs = {
2887 dropDataTypes,
2888 wantedDropOperation,
2889 NULL,
2890 acceptDropOperation,
2891 NULL,
2892 NULL,
2893 fetchDragData
2897 static WMArray*
2898 requiredDataTypes(WMView *self, WMDragOperationType request, WMArray *sourceDataTypes)
2900 return ((Text*)self->self)->xdndDestinationTypes;
2904 static WMDragOperationType
2905 allowedOperation(WMView *self, WMDragOperationType request, WMArray *sourceDataTypes)
2907 return WDOperationCopy;
2911 static void
2912 performDragOperation(WMView *self, WMArray *dropData, WMArray *operations,
2913 WMPoint* dropLocation)
2915 WMText *tPtr = (WMText *)self->self;
2916 WMData* data;
2917 char* colorName;
2918 WMColor *color;
2920 if (tPtr) {
2922 /* only one required type, implies only one drop data */
2924 /* get application/X-color if any */
2925 data = (WMData*)WMPopFromArray(dropData);
2926 if (data != NULL) {
2927 colorName = (char*)WMDataBytes(data);
2928 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2930 if(color) {
2931 WMSetTextSelectionColor(tPtr, color);
2932 WMReleaseColor(color);
2938 static WMDragDestinationProcs _DragDestinationProcs = {
2939 NULL,
2940 requiredDataTypes,
2941 allowedOperation,
2942 NULL,
2943 performDragOperation,
2944 NULL
2948 char *
2949 getStream(WMText *tPtr, int sel, int array)
2951 TextBlock *tb = NULL;
2952 char *text = NULL;
2953 unsigned long where = 0;
2955 if (!tPtr)
2956 return NULL;
2958 if (!(tb = tPtr->firstTextBlock))
2959 return NULL;
2961 if (tPtr->writer) {
2962 (tPtr->writer) (tPtr, (void *) text);
2963 return text;
2966 tb = tPtr->firstTextBlock;
2967 while (tb) {
2969 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2971 if (!sel || (tb->graphic && tb->selected)) {
2973 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2974 && tb != tPtr->firstTextBlock) {
2975 text = wrealloc(text, where+1);
2976 text[where++] = '\n';
2979 if(tb->blank)
2980 goto _gSnext;
2982 if(tb->graphic && array) {
2983 text = wrealloc(text, where+4);
2984 text[where++] = 0xFA;
2985 text[where++] = (tb->used>>8)&0x0ff;
2986 text[where++] = tb->used&0x0ff;
2987 text[where++] = tb->allocated; /* extra info */
2989 text = wrealloc(text, where+tb->used);
2990 memcpy(&text[where], tb->text, tb->used);
2991 where += tb->used;
2994 } else if (sel && tb->selected) {
2996 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2997 text = wrealloc(text, where+1);
2998 text[where++] = '\n';
3001 if(tb->blank)
3002 goto _gSnext;
3004 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3005 memcpy(&text[where], &tb->text[tb->s_begin],
3006 tb->s_end - tb->s_begin);
3007 where += tb->s_end - tb->s_begin;
3012 _gSnext:tb = tb->next;
3015 /* +1 for the end of string, let's be nice */
3016 text = wrealloc(text, where+1);
3017 text[where] = 0;
3018 return text;
3022 static void
3023 releaseStreamObjects(void *data)
3025 if(data)
3026 wfree(data);
3029 WMArray *
3030 getStreamObjects(WMText *tPtr, int sel)
3032 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3033 WMData *data;
3034 char *stream;
3035 unsigned short len;
3036 char *start, *fa, *desc;
3038 stream = getStream(tPtr, sel, 1);
3039 if(!stream)
3040 return NULL;
3042 start = stream;
3043 while (start) {
3045 fa = strchr(start, 0xFA);
3046 if (fa) {
3047 if((int)(fa - start)>0) {
3048 desc = start;
3049 desc[(int)(fa - start)] = 0;
3050 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3051 WMSetDataFormat(data, TYPETEXT);
3052 WMAddToArray(array, (void *) data);
3055 len = *(fa+1)*0xff + *(fa+2);
3056 data = WMCreateDataWithBytes((void *)(fa+4), len);
3057 WMSetDataFormat(data, *(fa+3));
3058 WMAddToArray(array, (void *) data);
3059 start = fa + len + 4;
3061 } else {
3062 if (start && strlen(start)) {
3063 data = WMCreateDataWithBytes((void *)start, strlen(start));
3064 WMSetDataFormat(data, TYPETEXT);
3065 WMAddToArray(array, (void *) data);
3067 start = fa;
3071 wfree(stream);
3072 return array;
3076 #define XDND_TEXT_DATA_TYPE "text/plain"
3077 #define XDND_COLOR_DATA_TYPE "application/X-color"
3078 static WMArray*
3079 getXdndSourceTypeArray()
3081 WMArray *types = WMCreateArray(1);
3082 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
3083 return types;
3087 static WMArray*
3088 getXdndDestinationTypeArray()
3090 WMArray *types = WMCreateArray(1);
3091 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
3092 return types;
3096 WMText*
3097 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3099 Text *tPtr;
3100 Display *dpy;
3101 WMScreen *scr;
3102 XGCValues gcv;
3104 tPtr = wmalloc(sizeof(Text));
3105 memset(tPtr, 0, sizeof(Text));
3106 tPtr->widgetClass = WC_Text;
3107 tPtr->view = W_CreateView(W_VIEW(parent));
3108 if (!tPtr->view) {
3109 perror("could not create text's view\n");
3110 wfree(tPtr);
3111 return NULL;
3114 dpy = tPtr->view->screen->display;
3115 scr = tPtr->view->screen;
3117 tPtr->view->self = tPtr;
3118 tPtr->view->attribs.cursor = scr->textCursor;
3119 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3120 W_ResizeView(tPtr->view, 250, 200);
3122 tPtr->dColor = WMBlackColor(scr);
3123 tPtr->fgColor = WMBlackColor(scr);
3124 tPtr->bgColor = WMWhiteColor(scr);
3125 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3127 gcv.graphics_exposures = False;
3128 gcv.foreground = W_PIXEL(scr->gray);
3129 gcv.background = W_PIXEL(scr->darkGray);
3130 gcv.fill_style = FillStippled;
3131 /* why not use scr->stipple here? */
3132 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS,
3133 STIPPLE_WIDTH, STIPPLE_HEIGHT);
3134 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3135 GCForeground|GCBackground|GCStipple
3136 |GCFillStyle|GCGraphicsExposures, &gcv);
3138 tPtr->ruler = NULL;
3139 tPtr->vS = NULL;
3140 tPtr->hS = NULL;
3142 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3144 tPtr->view->delegate = &_TextViewDelegate;
3146 tPtr->delegate = NULL;
3148 #if DO_BLINK
3149 tPtr->timerID = NULL;
3150 #endif
3152 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3153 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3154 handleEvents, tPtr);
3156 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3157 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3158 handleActionEvents, tPtr);
3160 WMAddNotificationObserver(ownershipObserver, tPtr,
3161 WMSelectionOwnerDidChangeNotification,
3162 tPtr);
3164 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3165 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3169 WMArray *types = WMCreateArray(2);
3170 WMAddToArray(types, "application/X-color");
3171 WMAddToArray(types, "application/X-image");
3172 WMRegisterViewForDraggedTypes(tPtr->view, types);
3175 /*WMAddNotificationObserver(fontChanged, tPtr,
3176 WMFontPanelDidChangeNotification, tPtr);*/
3178 tPtr->firstTextBlock = NULL;
3179 tPtr->lastTextBlock = NULL;
3180 tPtr->currentTextBlock = NULL;
3181 tPtr->tpos = 0;
3183 tPtr->gfxItems = WMCreateArray(4);
3185 tPtr->parser = parser;
3186 tPtr->writer = writer;
3188 tPtr->sel.x = tPtr->sel.y = 2;
3189 tPtr->sel.w = tPtr->sel.h = 0;
3191 tPtr->clicked.x = tPtr->clicked.y = 2;
3193 tPtr->visible.x = tPtr->visible.y = 2;
3194 tPtr->visible.h = tPtr->view->size.height;
3195 tPtr->visible.w = tPtr->view->size.width - 4;
3197 tPtr->cursor.x = -23;
3199 tPtr->docWidth = 0;
3200 tPtr->docHeight = 0;
3201 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3202 default_bullet);
3203 tPtr->db = (Pixmap) NULL;
3204 tPtr->bgPixmap = NULL;
3206 tPtr->margins = WMGetRulerMargins(NULL);
3207 tPtr->margins->right = tPtr->visible.w;
3208 tPtr->nMargins = 1;
3210 tPtr->flags.rulerShown = False;
3211 tPtr->flags.monoFont = False;
3212 tPtr->flags.focused = False;
3213 tPtr->flags.editable = True;
3214 tPtr->flags.ownsSelection = False;
3215 tPtr->flags.pointerGrabbed = False;
3216 tPtr->flags.extendSelection = False;
3217 tPtr->flags.frozen = False;
3218 tPtr->flags.cursorShown = True;
3219 tPtr->flags.acceptsGraphic = False;
3220 tPtr->flags.horizOnDemand = False;
3221 tPtr->flags.needsLayOut = False;
3222 tPtr->flags.ignoreNewLine = False;
3223 tPtr->flags.indentNewLine = False;
3224 tPtr->flags.laidOut = False;
3225 tPtr->flags.ownsSelection = False;
3226 tPtr->flags.waitingForSelection = False;
3227 tPtr->flags.prepend = False;
3228 tPtr->flags.isOverGraphic = False;
3229 tPtr->flags.relief = WRSunken;
3230 tPtr->flags.isOverGraphic = 0;
3231 tPtr->flags.alignment = WALeft;
3232 tPtr->flags.first = True;
3234 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3235 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3237 return tPtr;
3240 void
3241 WMPrependTextStream(WMText *tPtr, char *text)
3243 CHECK_CLASS(tPtr, WC_Text);
3245 if(!text) {
3246 if (tPtr->flags.ownsSelection)
3247 releaseSelection(tPtr);
3248 clearText(tPtr);
3249 updateScrollers(tPtr);
3250 return;
3253 tPtr->flags.prepend = True;
3254 if (text && tPtr->parser)
3255 (tPtr->parser) (tPtr, (void *) text);
3256 else
3257 insertPlainText(tPtr, text);
3259 tPtr->flags.needsLayOut = True;
3260 tPtr->tpos = 0;
3261 if(!tPtr->flags.frozen) {
3262 layOutDocument(tPtr);
3267 void
3268 WMAppendTextStream(WMText *tPtr, char *text)
3270 CHECK_CLASS(tPtr, WC_Text);
3272 if(!text) {
3273 if (tPtr->flags.ownsSelection)
3274 releaseSelection(tPtr);
3275 clearText(tPtr);
3276 updateScrollers(tPtr);
3277 return;
3280 tPtr->flags.prepend = False;
3281 if (text && tPtr->parser)
3282 (tPtr->parser) (tPtr, (void *) text);
3283 else
3284 insertPlainText(tPtr, text);
3286 tPtr->flags.needsLayOut = True;
3287 if(tPtr->currentTextBlock) {
3288 if(tPtr->currentTextBlock->graphic)
3289 tPtr->tpos = 1;
3290 else
3291 tPtr->tpos = tPtr->currentTextBlock->used;
3294 if(!tPtr->flags.frozen) {
3295 layOutDocument(tPtr);
3300 char*
3301 WMGetTextStream(WMText *tPtr)
3303 CHECK_CLASS(tPtr, WC_Text);
3305 return getStream(tPtr, 0, 0);
3309 char*
3310 WMGetTextSelectedStream(WMText *tPtr)
3312 CHECK_CLASS(tPtr, WC_Text);
3314 return getStream(tPtr, 1, 0);
3318 WMArray*
3319 WMGetTextObjects(WMText *tPtr)
3321 CHECK_CLASS(tPtr, WC_Text);
3323 return getStreamObjects(tPtr, 0);
3326 WMArray*
3327 WMGetTextSelectedObjects(WMText *tPtr)
3329 CHECK_CLASS(tPtr, WC_Text);
3331 return getStreamObjects(tPtr, 1);
3335 void
3336 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3338 CHECK_CLASS(tPtr, WC_Text);
3340 tPtr->delegate = delegate;
3344 void*
3345 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3346 char *description, WMColor *color,
3347 unsigned short first, unsigned short extraInfo)
3349 TextBlock *tb;
3351 if (!w || !description || !color)
3352 return NULL;
3354 tb = wmalloc(sizeof(TextBlock));
3356 tb->text = wstrdup(description);
3357 tb->used = strlen(description);
3358 tb->blank = False;
3359 tb->d.widget = w;
3360 tb->color = WMRetainColor(color);
3361 tb->marginN = newMargin(tPtr, NULL);
3362 tb->allocated = extraInfo;
3363 tb->first = first;
3364 tb->kanji = False;
3365 tb->graphic = True;
3366 tb->object = True;
3367 tb->underlined = False;
3368 tb->selected = False;
3369 tb->script = 0;
3370 tb->sections = NULL;
3371 tb->nsections = 0;
3372 tb->prior = NULL;
3373 tb->next = NULL;
3375 return tb;
3379 void*
3380 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3381 char *description, WMColor *color,
3382 unsigned short first, unsigned short extraInfo)
3384 TextBlock *tb;
3386 if (!p || !description || !color)
3387 return NULL;
3389 tb = wmalloc(sizeof(TextBlock));
3391 tb->text = wstrdup(description);
3392 tb->used = strlen(description);
3393 tb->blank = False;
3394 tb->d.pixmap = WMRetainPixmap(p);
3395 tb->color = WMRetainColor(color);
3396 tb->marginN = newMargin(tPtr, NULL);
3397 tb->allocated = extraInfo;
3398 tb->first = first;
3399 tb->kanji = False;
3400 tb->graphic = True;
3401 tb->object = False;
3402 tb->underlined = False;
3403 tb->selected = False;
3404 tb->script = 0;
3405 tb->sections = NULL;
3406 tb->nsections = 0;
3407 tb->prior = NULL;
3408 tb->next = NULL;
3410 return tb;
3414 void*
3415 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3416 unsigned short first, unsigned short len)
3418 TextBlock *tb;
3420 if (!font || !color)
3421 return NULL;
3423 tb = wmalloc(sizeof(TextBlock));
3425 tb->allocated = reqBlockSize(len);
3426 tb->text = (char *)wmalloc(tb->allocated);
3427 memset(tb->text, 0, tb->allocated);
3429 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3430 *tb->text = ' ';
3431 tb->used = 1;
3432 tb->blank = True;
3433 } else {
3434 memcpy(tb->text, text, len);
3435 tb->used = len;
3436 tb->blank = False;
3438 tb->text[tb->used] = 0;
3440 tb->d.font = WMRetainFont(font);
3441 tb->color = WMRetainColor(color);
3442 tb->marginN = newMargin(tPtr, NULL);
3443 tb->first = first;
3444 tb->kanji = False;
3445 tb->graphic = False;
3446 tb->underlined = False;
3447 tb->selected = False;
3448 tb->script = 0;
3449 tb->sections = NULL;
3450 tb->nsections = 0;
3451 tb->prior = NULL;
3452 tb->next = NULL;
3453 return tb;
3457 void
3458 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3459 unsigned int kanji, unsigned int underlined, int script,
3460 WMRulerMargins *margins)
3462 TextBlock *tb = (TextBlock *) vtb;
3463 if (!tb)
3464 return;
3466 tb->first = first;
3467 tb->kanji = kanji;
3468 tb->underlined = underlined;
3469 tb->script = script;
3470 tb->marginN = newMargin(tPtr, margins);
3474 void
3475 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3476 unsigned int *kanji, unsigned int *underlined, int *script,
3477 WMRulerMargins *margins)
3479 TextBlock *tb = (TextBlock *) vtb;
3480 if (!tb)
3481 return;
3483 if (first) *first = tb->first;
3484 if (kanji) *kanji = tb->kanji;
3485 if (underlined) *underlined = tb->underlined;
3486 if (script) *script = tb->script;
3487 if (margins) margins = &tPtr->margins[tb->marginN];
3491 void
3492 WMPrependTextBlock(WMText *tPtr, void *vtb)
3494 TextBlock *tb = (TextBlock *)vtb;
3496 if (!tb)
3497 return;
3499 if (tb->graphic) {
3500 if(tb->object) {
3501 WMWidget *w = tb->d.widget;
3502 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3503 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3504 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3507 WMAddToArray(tPtr->gfxItems, (void *)tb);
3508 tPtr->tpos = 1;
3510 } else {
3511 tPtr->tpos = tb->used;
3514 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3515 tb->next = tb->prior = NULL;
3516 tb->first = True;
3517 tPtr->lastTextBlock = tPtr->firstTextBlock
3518 = tPtr->currentTextBlock = tb;
3519 return;
3522 if(!tb->first) {
3523 tb->marginN = tPtr->currentTextBlock->marginN;
3526 tb->next = tPtr->currentTextBlock;
3527 tb->prior = tPtr->currentTextBlock->prior;
3528 if (tPtr->currentTextBlock->prior)
3529 tPtr->currentTextBlock->prior->next = tb;
3531 tPtr->currentTextBlock->prior = tb;
3532 if (!tb->prior)
3533 tPtr->firstTextBlock = tb;
3535 tPtr->currentTextBlock = tb;
3539 void
3540 WMAppendTextBlock(WMText *tPtr, void *vtb)
3542 TextBlock *tb = (TextBlock *)vtb;
3544 if (!tb)
3545 return;
3547 if (tb->graphic) {
3548 if(tb->object) {
3549 WMWidget *w = tb->d.widget;
3550 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3551 (W_VIEW(w))->attribs.cursor =
3552 tPtr->view->screen->defaultCursor;
3553 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3556 WMAddToArray(tPtr->gfxItems, (void *)tb);
3557 tPtr->tpos = 1;
3559 } else {
3560 tPtr->tpos = tb->used;
3564 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3565 tb->next = tb->prior = NULL;
3566 tb->first = True;
3567 tPtr->lastTextBlock = tPtr->firstTextBlock
3568 = tPtr->currentTextBlock = tb;
3569 return;
3572 if(!tb->first) {
3573 tb->marginN = tPtr->currentTextBlock->marginN;
3576 tb->next = tPtr->currentTextBlock->next;
3577 tb->prior = tPtr->currentTextBlock;
3578 if (tPtr->currentTextBlock->next)
3579 tPtr->currentTextBlock->next->prior = tb;
3581 tPtr->currentTextBlock->next = tb;
3583 if (!tb->next)
3584 tPtr->lastTextBlock = tb;
3586 tPtr->currentTextBlock = tb;
3590 void*
3591 WMRemoveTextBlock(WMText *tPtr)
3593 TextBlock *tb = NULL;
3595 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock ||
3596 !tPtr->currentTextBlock) {
3597 return NULL;
3600 tb = tPtr->currentTextBlock;
3601 if (tb->graphic) {
3602 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3604 if(tb->object) {
3605 WMUnmapWidget(tb->d.widget);
3609 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3610 if (tPtr->currentTextBlock->next)
3611 tPtr->currentTextBlock->next->prior = NULL;
3613 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3614 tPtr->currentTextBlock = tPtr->firstTextBlock;
3616 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3617 tPtr->currentTextBlock->prior->next = NULL;
3618 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3619 tPtr->currentTextBlock = tPtr->lastTextBlock;
3620 } else {
3621 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3622 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3623 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3626 return (void *)tb;
3630 #if 0
3631 static void
3632 destroyWidget(WMWidget *widget)
3634 WMDestroyWidget(widget);
3635 // -- never do this -- wfree(widget);
3637 #endif
3640 void
3641 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3643 TextBlock *tb = (TextBlock *)vtb;
3644 if (!tb)
3645 return;
3647 if (tb->graphic) {
3648 if(tb->object) {
3649 /* naturally, there's a danger to destroying widgets whose action
3650 * brings us here: ie. press a button to destroy it...
3651 * need to find a safer way. till then... this stays commented out */
3652 /* 5 months later... destroy it 10 seconds after now which should
3653 * be enough time for the widget's action to be completed... :-) */
3654 /* This is a bad assumption. Just destroy the widget here.
3655 * if the caller needs it, it can protect it with W_RetainView()
3656 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3657 WMDestroyWidget(tb->d.widget);
3658 } else {
3659 WMReleasePixmap(tb->d.pixmap);
3661 } else {
3662 WMReleaseFont(tb->d.font);
3665 WMReleaseColor(tb->color);
3666 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3667 if (tb->sections)
3668 wfree(tb->sections);
3669 wfree(tb->text);
3670 wfree(tb);
3674 void
3675 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3677 if (tPtr->fgColor)
3678 WMReleaseColor(tPtr->fgColor);
3680 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3682 paintText(tPtr);
3686 void
3687 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3689 if (tPtr->bgColor)
3690 WMReleaseColor(tPtr->bgColor);
3692 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3693 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3695 paintText(tPtr);
3699 void
3700 WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3702 if (tPtr->bgPixmap)
3703 WMReleasePixmap(tPtr->bgPixmap);
3705 if (pixmap)
3706 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3707 else
3708 tPtr->bgPixmap = NULL;
3712 void
3713 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3715 tPtr->flags.relief = relief;
3716 textDidResize(tPtr->view->delegate, tPtr->view);
3720 void
3721 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3723 if (shouldhave && !tPtr->hS) {
3724 tPtr->hS = WMCreateScroller(tPtr);
3725 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3726 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3727 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3728 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3729 WMMapWidget(tPtr->hS);
3730 } else if (!shouldhave && tPtr->hS) {
3731 WMUnmapWidget(tPtr->hS);
3732 WMDestroyWidget(tPtr->hS);
3733 tPtr->hS = NULL;
3736 tPtr->hpos = 0;
3737 tPtr->prevHpos = 0;
3738 textDidResize(tPtr->view->delegate, tPtr->view);
3742 void
3743 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3745 if(shouldhave && !tPtr->ruler) {
3746 tPtr->ruler = WMCreateRuler(tPtr);
3747 (W_VIEW(tPtr->ruler))->attribs.cursor =
3748 tPtr->view->screen->defaultCursor;
3749 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3750 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3751 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3752 } else if(!shouldhave && tPtr->ruler) {
3753 WMShowTextRuler(tPtr, False);
3754 WMDestroyWidget(tPtr->ruler);
3755 tPtr->ruler = NULL;
3757 textDidResize(tPtr->view->delegate, tPtr->view);
3760 void
3761 WMShowTextRuler(WMText *tPtr, Bool show)
3763 if(!tPtr->ruler)
3764 return;
3766 if(tPtr->flags.monoFont)
3767 show = False;
3769 tPtr->flags.rulerShown = show;
3770 if(show) {
3771 WMMapWidget(tPtr->ruler);
3772 } else {
3773 WMUnmapWidget(tPtr->ruler);
3776 textDidResize(tPtr->view->delegate, tPtr->view);
3780 Bool
3781 WMGetTextRulerShown(WMText *tPtr)
3783 if(!tPtr->ruler)
3784 return False;
3786 return tPtr->flags.rulerShown;
3790 void
3791 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3793 if (shouldhave && !tPtr->vS) {
3794 tPtr->vS = WMCreateScroller(tPtr);
3795 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3796 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3797 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3798 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3799 WMMapWidget(tPtr->vS);
3800 } else if (!shouldhave && tPtr->vS) {
3801 WMUnmapWidget(tPtr->vS);
3802 WMDestroyWidget(tPtr->vS);
3803 tPtr->vS = NULL;
3806 tPtr->vpos = 0;
3807 tPtr->prevVpos = 0;
3808 textDidResize(tPtr->view->delegate, tPtr->view);
3812 Bool
3813 WMScrollText(WMText *tPtr, int amount)
3815 Bool scroll=False;
3817 if (amount == 0 || !tPtr->view->flags.realized)
3818 return False;
3820 if (amount < 0) {
3821 if (tPtr->vpos > 0) {
3822 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3823 else tPtr->vpos=0;
3824 scroll=True;
3826 } else {
3827 int limit = tPtr->docHeight - tPtr->visible.h;
3828 if (tPtr->vpos < limit) {
3829 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3830 else tPtr->vpos = limit;
3831 scroll = True;
3835 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3836 updateScrollers(tPtr);
3837 paintText(tPtr);
3839 tPtr->prevVpos = tPtr->vpos;
3840 return scroll;
3844 Bool
3845 WMPageText(WMText *tPtr, Bool direction)
3847 if (!tPtr->view->flags.realized)
3848 return False;
3850 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3854 void
3855 WMSetTextEditable(WMText *tPtr, Bool editable)
3857 tPtr->flags.editable = editable;
3862 WMGetTextEditable(WMText *tPtr)
3864 return tPtr->flags.editable;
3867 void
3868 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3870 tPtr->flags.indentNewLine = indent;
3873 void
3874 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3876 tPtr->flags.ignoreNewLine = ignore;
3879 Bool
3880 WMGetTextIgnoresNewline(WMText *tPtr)
3882 return tPtr->flags.ignoreNewLine;
3885 void
3886 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3888 if (mono) {
3889 if(tPtr->flags.rulerShown)
3890 WMShowTextRuler(tPtr, False);
3891 if(tPtr->flags.alignment != WALeft)
3892 tPtr->flags.alignment = WALeft;
3895 tPtr->flags.monoFont = mono;
3896 textDidResize(tPtr->view->delegate, tPtr->view);
3899 Bool
3900 WMGetTextUsesMonoFont(WMText *tPtr)
3902 return tPtr->flags.monoFont;
3906 void
3907 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3909 if (tPtr->dFont)
3910 WMReleaseFont(tPtr->dFont);
3912 if (font) {
3913 tPtr->dFont = WMRetainFont(font);
3914 } else {
3915 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3920 WMFont*
3921 WMGetTextDefaultFont(WMText *tPtr)
3923 return WMRetainFont(tPtr->dFont);
3927 void
3928 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3930 if (tPtr->dColor)
3931 WMReleaseColor(tPtr->dColor);
3933 if (color) {
3934 tPtr->dColor = WMRetainColor(color);
3935 } else {
3936 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3941 WMColor*
3942 WMGetTextDefaultColor(WMText *tPtr)
3944 return tPtr->dColor;
3948 void
3949 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3951 if(tPtr->flags.monoFont)
3952 tPtr->flags.alignment = WALeft;
3953 else
3954 tPtr->flags.alignment = alignment;
3955 WMThawText(tPtr);
3960 WMGetTextInsertType(WMText *tPtr)
3962 return tPtr->flags.prepend;
3966 void
3967 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3969 setSelectionProperty(tPtr, NULL, color, -1);
3973 WMColor*
3974 WMGetTextSelectionColor(WMText *tPtr)
3976 TextBlock *tb;
3978 tb = tPtr->currentTextBlock;
3980 if (!tb || !tPtr->flags.ownsSelection)
3981 return NULL;
3983 if(!tb->selected)
3984 return NULL;
3986 return tb->color;
3990 void
3991 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3993 setSelectionProperty(tPtr, font, NULL, -1) ;
3997 WMFont*
3998 WMGetTextSelectionFont(WMText *tPtr)
4000 TextBlock *tb;
4002 tb = tPtr->currentTextBlock;
4004 if (!tb || !tPtr->flags.ownsSelection)
4005 return NULL;
4007 if(!tb->selected)
4008 return NULL;
4010 if(tb->graphic) {
4011 tb = getFirstNonGraphicBlockFor(tb, 1);
4012 if(!tb)
4013 return NULL;
4015 return (tb->selected ? tb->d.font : NULL);
4019 void
4020 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4022 // check this
4023 if (underlined!=0 && underlined!=1)
4024 return;
4026 setSelectionProperty(tPtr, NULL, NULL, underlined);
4031 WMGetTextSelectionUnderlined(WMText *tPtr)
4033 TextBlock *tb;
4035 tb = tPtr->currentTextBlock;
4037 if (!tb || !tPtr->flags.ownsSelection)
4038 return 0;
4040 if(!tb->selected)
4041 return 0;
4043 return tb->underlined;
4047 void
4048 WMFreezeText(WMText *tPtr)
4050 tPtr->flags.frozen = True;
4054 void
4055 WMThawText(WMText *tPtr)
4057 tPtr->flags.frozen = False;
4059 if(tPtr->flags.monoFont) {
4060 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4061 TextBlock *tb;
4063 /* make sure to unmap widgets no matter where they are */
4064 /* they'll be later remapped if needed by paintText */
4065 for(j=0; j<c; j++) {
4066 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4067 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4068 WMUnmapWidget(tb->d.widget);
4074 tPtr->flags.laidOut = False;
4075 layOutDocument(tPtr);
4076 updateScrollers(tPtr);
4077 paintText(tPtr);
4078 tPtr->flags.needsLayOut = False;
4082 /* find first occurence of a string */
4083 static char *
4084 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4085 Bool caseSensitive)
4087 char *ptr;
4089 if(!haystack || !needle || !end)
4090 return NULL;
4092 for (ptr = haystack; ptr < end; ptr++) {
4093 if(caseSensitive) {
4094 if (*ptr == *needle && !strncmp(ptr, needle, len))
4095 return ptr;
4097 } else {
4098 if (tolower(*ptr) == tolower(*needle) &&
4099 !strncasecmp(ptr, needle, len))
4100 return ptr;
4104 return NULL;
4107 /* find last occurence of a string */
4108 static char *
4109 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4110 Bool caseSensitive)
4112 char *ptr;
4114 if(!haystack || !needle || !end)
4115 return NULL;
4117 for (ptr = haystack-2; ptr > end; ptr--) {
4118 if(caseSensitive) {
4119 if (*ptr == *needle && !strncmp(ptr, needle, len))
4120 return ptr;
4121 } else {
4122 if (tolower(*ptr) == tolower(*needle) &&
4123 !strncasecmp(ptr, needle, len))
4124 return ptr;
4128 return NULL;
4132 Bool
4133 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4134 Bool caseSensitive)
4136 TextBlock *tb;
4137 char *mark=NULL;
4138 unsigned short pos;
4141 #if 0
4142 if (! (tb = tPtr->currentTextBlock)) {
4143 if (! (tb = ( (direction > 0) ?
4144 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4145 return False;
4147 } else {
4148 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4149 tb = (direction>0) ? tb->next : tb->prior; */
4150 if(tb != tPtr->lastTextBlock)
4151 tb = tb->prior;
4153 #endif
4154 tb = tPtr->currentTextBlock;
4155 pos = tPtr->tpos;
4158 while(tb) {
4159 if (!tb->graphic) {
4161 if(direction > 0) {
4162 if(pos+1 < tb->used)
4163 pos++;
4165 if(tb->used - pos> 0 && pos > 0) {
4166 mark = mystrstr(&tb->text[pos], needle,
4167 strlen(needle), &tb->text[tb->used], caseSensitive);
4169 } else {
4170 tb = tb->next;
4171 pos = 0;
4172 continue;
4175 } else {
4176 if(pos-1 > 0)
4177 pos--;
4179 if(pos > 0) {
4180 mark = mystrrstr(&tb->text[pos], needle,
4181 strlen(needle), tb->text, caseSensitive);
4182 } else {
4183 tb = tb->prior;
4184 if(!tb)
4185 return False;
4186 pos = tb->used;
4187 continue;
4192 if(mark) {
4193 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4195 tPtr->tpos = (int)(mark - tb->text);
4196 tPtr->currentTextBlock = tb;
4197 updateCursorPosition(tPtr);
4198 tPtr->sel.y = tPtr->cursor.y+5;
4199 tPtr->sel.h = tPtr->cursor.h-10;
4200 tPtr->sel.x = tPtr->cursor.x +1;
4201 tPtr->sel.w = WMIN(WMWidthOfString(font,
4202 &tb->text[tPtr->tpos], strlen(needle)),
4203 tPtr->docWidth - tPtr->sel.x);
4204 tPtr->flags.ownsSelection = True;
4205 paintText(tPtr);
4207 return True;
4211 tb = (direction>0) ? tb->next : tb->prior;
4212 if(tb) {
4213 pos = (direction>0) ? 0 : tb->used;
4217 return False;
4221 Bool
4222 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4224 if (!tPtr->flags.ownsSelection)
4225 return False;
4227 removeSelection(tPtr);
4229 if(replacement) {
4230 insertTextInteractively(tPtr, replacement, strlen(replacement));
4231 updateCursorPosition(tPtr);
4232 paintText(tPtr);
4235 return True;