restoring copy of wtext.c to cvs (wif ssh)
[wmaker-crm.git] / WINGs / wtext.c
blob6e91edb3fc5cccad39d883aeea2179e19d0566a9
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 GC bgGC; /* the background GC to draw with */
110 GC fgGC; /* the foreground GC to draw with */
111 GC stippledGC; /* the GC to overlay selected graphics with */
112 Pixmap db; /* the buffer on which to draw */
113 WMPixmap *bgPixmap; /* the background pixmap */
115 myRect visible; /* the actual rectangle that can be drawn into */
116 myRect cursor; /* the position and (height) of cursor */
117 myRect sel; /* the selection rectangle */
119 WMPoint clicked; /* where in the _document_ was clicked */
121 unsigned short tpos; /* the position in the currentTextBlock */
122 unsigned short docWidth; /* the width of the entire document */
123 unsigned int docHeight; /* the height of the entire document */
125 TextBlock *firstTextBlock;
126 TextBlock *lastTextBlock;
127 TextBlock *currentTextBlock;
129 WMArray *gfxItems; /* a nice array of graphic items */
131 #if DO_BLINK
132 WMHandlerID timerID; /* for nice twinky-winky */
133 #endif
135 WMAction *parser;
136 WMAction *writer;
137 WMTextDelegate *delegate;
138 Time lastClickTime;
140 WMRulerMargins *margins; /* an array of margins */
142 unsigned int nMargins:7; /* the total number of margins in use */
143 struct {
144 unsigned int monoFont:1; /* whether to ignore formats and graphic */
145 unsigned int focused:1; /* whether this instance has input focus */
146 unsigned int editable:1; /* "silly user, you can't edit me" */
147 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
148 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
149 unsigned int extendSelection:1; /* shift-drag to select more regions */
151 unsigned int rulerShown:1; /* whether the ruler is shown or not */
152 unsigned int frozen:1; /* whether screen updates are to be made */
153 unsigned int cursorShown:1; /* whether to show the cursor */
154 unsigned int acceptsGraphic:1;/* accept graphic when dropped */
155 unsigned int horizOnDemand:1;/* if a large image should appear*/
156 unsigned int needsLayOut:1; /* in case of Append/Deletes */
157 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
158 unsigned int indentNewLine:1;/* add " " for a newline typed */
159 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
160 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
161 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
162 WMAlignment alignment:2; /* the alignment for text */
163 WMReliefType relief:3; /* the relief to display with */
164 unsigned int isOverGraphic:2;/* the mouse is over a graphic */
165 unsigned int first:1; /* for plain text parsing, newline? */
166 /* unsigned int RESERVED:1; */
167 } flags;
168 } Text;
171 #define NOTIFY(T,C,N,A) {\
172 WMNotification *notif = WMCreateNotification(N,T,A);\
173 if ((T)->delegate && (T)->delegate->C)\
174 (*(T)->delegate->C)((T)->delegate,notif);\
175 WMPostNotification(notif);\
176 WMReleaseNotification(notif);}
179 #define TYPETEXT 0
181 /* just to print blocks of text not terminated by \0 */
182 static void
183 output(char *ptr, int len)
185 char s[len+1];
186 memcpy(s, ptr, len);
187 s[len] = 0;
188 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
189 printf("[%s]\n", s);
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
199 #define STIPPLE_WIDTH 8
200 #define STIPPLE_HEIGHT 8
201 static unsigned char STIPPLE_BITS[] = {
202 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
207 static char *default_bullet[] = {
208 "6 6 4 1",
209 " c None s None", ". c black",
210 "X c white", "o c #808080",
211 " ... ",
212 ".XX.. ",
213 ".XX..o",
214 ".....o",
215 " ...oo",
216 " ooo "};
219 static void handleEvents(XEvent *event, void *data);
220 static void layOutDocument(Text *tPtr);
221 static void updateScrollers(Text *tPtr);
224 static int
225 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
227 unsigned int i=0;
229 for(i=0; i < tPtr->nMargins; i++) {
231 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
232 return i;
235 return -1;
240 static int
241 newMargin(Text *tPtr, WMRulerMargins *margins)
243 int n;
245 if (!margins) {
246 tPtr->margins[0].retainCount++;
247 return 0;
250 n = getMarginNumber(tPtr, margins);
252 if (n == -1) {
254 if(tPtr->nMargins >= 127) {
255 n = tPtr->nMargins-1;
256 return n;
259 tPtr->margins = wrealloc(tPtr->margins,
260 (++tPtr->nMargins)*sizeof(WMRulerMargins));
262 n = tPtr->nMargins-1;
263 tPtr->margins[n].left = margins->left;
264 tPtr->margins[n].first = margins->first;
265 tPtr->margins[n].body = margins->body;
266 tPtr->margins[n].right = margins->right;
267 /* for each tab... */
268 tPtr->margins[n].retainCount = 1;
269 } else {
270 tPtr->margins[n].retainCount++;
273 return n;
276 static Bool
277 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
279 unsigned short i, w, lw, selected = False, extend = False;
280 myRect sel;
283 /* if selection rectangle completely encloses the section */
284 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
285 && (tb->sections[s]._y + tb->sections[s].h
286 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
287 sel.x = 0;
288 sel.w = tPtr->visible.w;
289 selected = extend = True;
291 /* or if it starts on a line and then goes further down */
292 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
293 && (tb->sections[s]._y + tb->sections[s].h
294 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
295 && (tb->sections[s]._y + tb->sections[s].h
296 >= tPtr->visible.y + tPtr->sel.y) ) {
297 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
298 sel.w = tPtr->visible.w;
299 selected = extend = True;
301 /* or if it begins before a line, but ends on it */
302 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
303 && (tb->sections[s]._y + tb->sections[s].h
304 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
305 && (tb->sections[s]._y
306 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
308 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
309 sel.w = tPtr->sel.x + tPtr->sel.w;
310 else
311 sel.w = tPtr->sel.x;
313 sel.x = 0;
314 selected = True;
316 /* or if the selection rectangle lies entirely within a line */
317 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
318 && (tPtr->sel.w >= 2)
319 && (tb->sections[s]._y + tb->sections[s].h
320 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
321 sel.x = tPtr->sel.x;
322 sel.w = tPtr->sel.w;
323 selected = True;
326 if (selected) {
327 selected = False;
329 /* if not within (modified) selection rectangle */
330 if ( tb->sections[s].x > sel.x + sel.w
331 || tb->sections[s].x + tb->sections[s].w < sel.x)
332 return False;
334 if (tb->graphic) {
335 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
336 && tb->sections[s].x >= sel.x) {
337 rect->width = tb->sections[s].w;
338 rect->x = tb->sections[s].x;
339 selected = True;
341 } else {
343 i = tb->sections[s].begin;
344 lw = 0;
346 if (0&& tb->sections[s].x >= sel.x) {
347 tb->s_begin = tb->sections[s].begin;
348 goto _selEnd;
351 while (++i <= tb->sections[s].end) {
353 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
354 lw += w;
356 if (lw + tb->sections[s].x >= sel.x
357 || i == tb->sections[s].end ) {
358 lw -= w;
359 i--;
360 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
361 break;
365 if (i > tb->sections[s].end) {
366 printf("WasSelected: (i > tb->sections[s].end) \n");
367 return False;
370 _selEnd: rect->x = tb->sections[s].x + lw;
371 lw = 0;
372 while(++i <= tb->sections[s].end) {
374 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
375 lw += w;
377 if (lw + rect->x >= sel.x + sel.w
378 || i == tb->sections[s].end ) {
380 if (i != tb->sections[s].end) {
381 lw -= w;
382 i--;
385 rect->width = lw;
386 if (tb->sections[s].last && sel.x + sel.w
387 >= tb->sections[s].x + tb->sections[s].w
388 && extend ) {
389 rect->width += (tPtr->visible.w - rect->x - lw);
392 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
393 selected = True;
394 break;
400 if (selected) {
401 rect->y = tb->sections[s]._y - tPtr->vpos;
402 rect->height = tb->sections[s].h;
403 if(tb->graphic) { printf("DEBUG: graphic s%d h%d\n", s,tb->sections[s].h);}
405 return selected;
409 static void
410 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color, int underlined)
412 TextBlock *tb;
413 int isFont=False;
415 tb = tPtr->firstTextBlock;
416 if (!tb || !tPtr->flags.ownsSelection)
417 return;
419 if(font && (!color || underlined==-1))
420 isFont = True;
422 while (tb) {
423 if (tPtr->flags.monoFont || tb->selected) {
425 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
426 || tb->graphic) {
428 if(isFont) {
429 if(!tb->graphic) {
430 WMReleaseFont(tb->d.font);
431 tb->d.font = WMRetainFont(font);
433 } else if(underlined !=-1) {
434 tb->underlined = underlined;
435 } else {
436 WMReleaseColor(tb->color);
437 tb->color = WMRetainColor(color);
440 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
442 TextBlock *midtb, *otb = tb;
444 if(underlined != -1) {
445 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
446 &(tb->text[tb->s_begin]), tb->d.font, tb->color,
447 False, (tb->s_end - tb->s_begin));
448 } else {
449 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
450 &(tb->text[tb->s_begin]),
451 (isFont?font:tb->d.font),
452 (isFont?tb->color:color),
453 False, (tb->s_end - tb->s_begin));
457 if (midtb) {
458 if(underlined != -1) {
459 midtb->underlined = underlined;
460 } else {
461 midtb->underlined = otb->underlined;
464 midtb->selected = !True;
465 midtb->s_begin = 0;
466 midtb->s_end = midtb->used;
467 tPtr->currentTextBlock = tb;
468 WMAppendTextBlock(tPtr, midtb);
469 tb = tPtr->currentTextBlock;
472 if (otb->used - otb->s_end > 0) {
473 TextBlock *ntb;
474 ntb = (TextBlock *)
475 WMCreateTextBlockWithText(tPtr,
476 &(otb->text[otb->s_end]), otb->d.font, otb->color,
477 False, otb->used - otb->s_end);
479 if (ntb) {
480 ntb->underlined = otb->underlined;
481 ntb->selected = False;
482 WMAppendTextBlock(tPtr, ntb);
483 tb = tPtr->currentTextBlock;
487 if (midtb) {
488 tPtr->currentTextBlock = midtb;
491 otb->selected = False;
492 otb->used = otb->s_begin;
496 tb = tb->next;
499 tPtr->flags.needsLayOut = True;
500 WMThawText(tPtr);
502 /* in case the size changed... */
503 if(isFont && tPtr->currentTextBlock) {
504 TextBlock *tb = tPtr->currentTextBlock;
506 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
507 tPtr->sel.y = 3 + tb->sections[0]._y;
508 tPtr->sel.h = tb->sections[tb->nsections-1]._y - tb->sections[0]._y;
509 tPtr->sel.w = tb->sections[tb->nsections-1].w;
510 if(tb->sections[tb->nsections-1]._y != tb->sections[0]._y) {
511 tPtr->sel.x = 0;
513 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
519 static Bool
520 removeSelection(Text *tPtr)
522 TextBlock *tb = NULL;
523 Bool first = False;
525 if (!(tb = tPtr->firstTextBlock))
526 return False;
528 while (tb) {
529 if (tb->selected) {
530 if(!first && !tb->graphic) {
531 WMReleaseFont(tPtr->dFont);
532 tPtr->dFont = WMRetainFont(tb->d.font);
533 first = True;
536 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
537 tPtr->currentTextBlock = tb;
538 if(tb->next) {
539 tPtr->tpos = 0;
540 } else if(tb->prior) {
541 if(tb->prior->graphic)
542 tPtr->tpos = 1;
543 else
544 tPtr->tpos = tb->prior->used;
545 } else tPtr->tpos = 0;
547 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
548 tb = tPtr->currentTextBlock;
549 continue;
551 } else if (tb->s_end <= tb->used) {
552 memmove(&(tb->text[tb->s_begin]),
553 &(tb->text[tb->s_end]), tb->used - tb->s_end);
554 tb->used -= (tb->s_end - tb->s_begin);
555 tb->selected = False;
556 tPtr->tpos = tb->s_begin;
561 tb = tb->next;
563 return True;
566 static TextBlock *
567 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
569 TextBlock *hold = tb;
571 if (!tb)
572 return NULL;
574 while (tb) {
575 if (!tb->graphic)
576 break;
577 tb = (dir? tb->next : tb->prior);
580 if(!tb) {
581 tb = hold;
582 while (tb) {
583 if (!tb->graphic)
584 break;
585 tb = (dir? tb->prior : tb->next);
589 if(!tb)
590 return NULL;
592 return tb;
596 static Bool
597 updateStartForCurrentTextBlock(Text *tPtr, int x, int y, int *dir,
598 TextBlock *tb)
600 if (tPtr->flags.monoFont && tb->graphic) {
601 tb = getFirstNonGraphicBlockFor(tb, *dir);
602 if(!tb)
603 return 0;
605 if (tb->graphic) {
606 tPtr->currentTextBlock =
607 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
608 tPtr->tpos = 0;
609 return 0;
614 if(!tb->sections)
615 layOutDocument(tPtr);
616 if(!tb->sections)
617 return 0;
619 *dir = !(y <= tb->sections[0].y);
620 if(*dir) {
621 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
622 && (y >= tb->sections[0]._y ) ) {
623 /* if it's on the same line */
624 if(x < tb->sections[0].x)
625 *dir = 0;
627 } else {
628 if ( ( y <= tb->sections[tb->nsections-1]._y
629 + tb->sections[tb->nsections-1].h )
630 && (y >= tb->sections[tb->nsections-1]._y ) ) {
631 /* if it's on the same line */
632 if(x > tb->sections[tb->nsections-1].x)
633 *dir = 1;
637 return 1;
641 static void
642 paintText(Text *tPtr)
644 TextBlock *tb;
645 WMFont *font;
646 GC gc, greyGC;
647 char *text;
648 int len, y, c, s, done=False, prev_y=-23, dir /* 1 = down */;
649 WMScreen *scr = tPtr->view->screen;
650 Display *dpy = tPtr->view->screen->display;
651 Window win = tPtr->view->window;
652 WMColor *color;
654 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
655 return;
658 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
659 0, 0, tPtr->visible.w, tPtr->visible.h);
661 if (tPtr->bgPixmap) {
662 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
663 (tPtr->visible.w-tPtr->visible.x-tPtr->bgPixmap->width)/2,
664 (tPtr->visible.h-tPtr->visible.y-tPtr->bgPixmap->height)/2);
667 if (! (tb = tPtr->currentTextBlock)) {
668 if (! (tb = tPtr->firstTextBlock)) {
669 goto _copy_area;
673 if (tPtr->flags.ownsSelection) {
674 color = WMGrayColor(scr);
675 greyGC = WMColorGC(color);
676 WMReleaseColor(color);
679 done = False;
683 /* first, which direction? Don't waste time looking all over,
684 since the parts to be drawn will most likely be near what
685 was previously drawn */
686 if(!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
687 goto _copy_area;
689 while(tb) {
691 if (tb->graphic && tPtr->flags.monoFont)
692 goto _getSibling;
694 if(dir) {
695 if(tPtr->vpos <= tb->sections[tb->nsections-1]._y
696 + tb->sections[tb->nsections-1].h)
697 break;
698 } else {
699 if(tPtr->vpos >= tb->sections[tb->nsections-1]._y
700 + tb->sections[tb->nsections-1].h)
701 break;
704 _getSibling:
705 if(dir) {
706 if(tb->next)
707 tb = tb->next;
708 else break;
709 } else {
710 if(tb->prior)
711 tb = tb->prior;
712 else break;
717 /* first, place all text that can be viewed */
718 while (!done && tb) {
721 if (tb->graphic) {
722 tb = tb->next;
723 continue;
726 tb->selected = False;
728 for(s=0; s<tb->nsections && !done; s++) {
730 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
731 done = True;
732 break;
735 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
736 continue;
738 if (tPtr->flags.monoFont) {
739 font = tPtr->dFont;
740 gc = tPtr->fgGC;
741 } else {
742 font = tb->d.font;
743 gc = WMColorGC(tb->color);
746 if (tPtr->flags.ownsSelection) {
747 XRectangle rect;
749 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
750 tb->selected = True;
751 XFillRectangle(dpy, tPtr->db, greyGC,
752 rect.x, rect.y, rect.width, rect.height);
756 prev_y = tb->sections[s]._y;
758 len = tb->sections[s].end - tb->sections[s].begin;
759 text = &(tb->text[tb->sections[s].begin]);
760 y = tb->sections[s].y - tPtr->vpos;
761 WMDrawString(scr, tPtr->db, gc, font,
762 tb->sections[s].x - tPtr->hpos, y, text, len);
764 if (!tPtr->flags.monoFont && tb->underlined) {
765 XDrawLine(dpy, tPtr->db, gc,
766 tb->sections[s].x - tPtr->hpos,
767 y + font->y + 1,
768 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
769 y + font->y + 1);
774 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, tPtr->fgGC,
854 tPtr->cursor.x, y,
855 tPtr->cursor.x, y + tPtr->cursor.h);
858 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 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, tPtr->fgGC,
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 line_height = WMAX(line_height, font->height + max_d);
1483 text = &(tb->text[items[i].begin]);
1484 len = items[i].end - items[i].begin;
1485 if (tPtr->flags.alignment != WALeft)
1486 lw += WMWidthOfString(font, text, len);
1490 if (tPtr->flags.alignment == WARight) {
1491 j = tPtr->visible.w - lw;
1492 } else if (tPtr->flags.alignment == WACenter) {
1493 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1496 for(i=0; i<nitems; i++) {
1497 tb = items[i].tb;
1499 if (tbsame == tb) { /* extend it, since it's on same line */
1500 tb->sections[tb->nsections-1].end = items[i].end;
1501 n = tb->nsections-1;
1502 } else {
1503 tb->sections = wrealloc(tb->sections,
1504 (++tb->nsections)*sizeof(Section));
1505 n = tb->nsections-1;
1506 tb->sections[n]._y = y + max_d;
1507 tb->sections[n].max_d = max_d;
1508 tb->sections[n].x = x+j;
1509 tb->sections[n].h = line_height;
1510 tb->sections[n].begin = items[i].begin;
1511 tb->sections[n].end = items[i].end;
1514 tb->sections[n].last = (i+1 == nitems);
1516 if (tb->graphic) {
1517 if (!tPtr->flags.monoFont) {
1518 if(tb->object) {
1519 WMWidget *wdt = tb->d.widget;
1520 tb->sections[n].y = max_d + y
1521 + line_height - WMWidgetHeight(wdt);
1522 tb->sections[n].w = WMWidgetWidth(wdt);
1523 } else {
1524 tb->sections[n].y = y + line_height
1525 + max_d - tb->d.pixmap->height;
1526 tb->sections[n].w = tb->d.pixmap->width;
1528 x += tb->sections[n].w;
1530 } else {
1531 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1532 len = items[i].end - items[i].begin;
1533 text = &(tb->text[items[i].begin]);
1535 tb->sections[n].y = y+line_height-font->y;
1536 tb->sections[n].w =
1537 WMWidthOfString(font,
1538 &(tb->text[tb->sections[n].begin]),
1539 tb->sections[n].end - tb->sections[n].begin);
1541 x += WMWidthOfString(font, text, len);
1544 tbsame = tb;
1547 return line_height;
1552 static void
1553 layOutDocument(Text *tPtr)
1555 TextBlock *tb;
1556 myLineItems *items = NULL;
1557 unsigned int itemsSize=0, nitems=0, begin, end;
1558 WMFont *font;
1559 unsigned int x, y=0, lw = 0, width=0, bmargin;
1560 char *start=NULL, *mark=NULL;
1562 if ( tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)) )
1563 return;
1565 assert(tPtr->visible.w > 20);
1567 tPtr->docWidth = tPtr->visible.w;
1568 x = tPtr->margins[tb->marginN].first;
1569 bmargin = tPtr->margins[tb->marginN].body;
1571 /* only partial layOut needed: re-Lay only affected textblocks */
1572 if (tPtr->flags.laidOut) {
1573 tb = tPtr->currentTextBlock;
1575 /* search backwards for textblocks on same line */
1576 while (tb->prior) {
1577 if (!tb->sections || tb->nsections<1) {
1578 tb = tPtr->firstTextBlock;
1579 tPtr->flags.laidOut = False;
1580 y = 0;
1581 goto _layOut;
1584 if(!tb->prior->sections || tb->prior->nsections<1) {
1585 tb = tPtr->firstTextBlock;
1586 tPtr->flags.laidOut = False;
1587 y = 0;
1588 goto _layOut;
1591 if (tb->sections[0]._y !=
1592 tb->prior->sections[tb->prior->nsections-1]._y) {
1593 break;
1595 tb = tb->prior;
1598 if(tb->prior && tb->prior->sections && tb->prior->nsections>0) {
1599 y = tb->prior->sections[tb->prior->nsections-1]._y +
1600 tb->prior->sections[tb->prior->nsections-1].h -
1601 tb->prior->sections[tb->prior->nsections-1].max_d;
1602 } else {
1603 y = 0;
1607 _layOut:
1608 while (tb) {
1610 if (tb->sections && tb->nsections>0) {
1611 wfree(tb->sections);
1612 tb->sections = NULL;
1613 tb->nsections = 0;
1616 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1617 TextBlock *next = tb->next;
1618 tPtr->currentTextBlock = tb;
1619 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1620 tb = next;
1621 tb->first = True;
1622 continue;
1625 if (tb->first && tb != tPtr->firstTextBlock) {
1626 y += layOutLine(tPtr, items, nitems, x, y);
1627 x = tPtr->margins[tb->marginN].first;
1628 bmargin = tPtr->margins[tb->marginN].body;
1629 nitems = 0;
1630 lw = 0;
1633 if (tb->graphic) {
1634 if (!tPtr->flags.monoFont) {
1635 if(tb->object)
1636 width = WMWidgetWidth(tb->d.widget);
1637 else
1638 width = tb->d.pixmap->width;
1640 if (width > tPtr->docWidth)
1641 tPtr->docWidth = width;
1643 lw += width;
1644 if (lw >= tPtr->visible.w - x ) {
1645 y += layOutLine(tPtr, items, nitems, x, y);
1646 nitems = 0;
1647 x = bmargin;
1648 lw = width;
1651 if(nitems + 1> itemsSize) {
1652 items = wrealloc(items,
1653 (++itemsSize)*sizeof(myLineItems));
1656 items[nitems].tb = tb;
1657 items[nitems].begin = 0;
1658 items[nitems].end = 0;
1659 nitems++;
1662 } else if ((start = tb->text)) {
1663 begin = end = 0;
1664 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1666 while (start) {
1667 mark = strchr(start, ' ');
1668 if (mark) {
1669 end += (int)(mark-start)+1;
1670 start = mark+1;
1671 } else {
1672 end += strlen(start);
1673 start = mark;
1676 if (end > tb->used)
1677 end = tb->used;
1679 if (end-begin > 0) {
1681 width = WMWidthOfString(font,
1682 &tb->text[begin], end-begin);
1684 /* if it won't fit, char wrap it */
1685 if (width >= tPtr->visible.w) {
1686 char *t = &tb->text[begin];
1687 int l=end-begin, i=0;
1688 do {
1689 width = WMWidthOfString(font, t, ++i);
1690 } while (width < tPtr->visible.w && i < l);
1691 if(i>2) i--;
1692 end = begin+i;
1693 start = &tb->text[end];
1696 lw += width;
1699 if (lw >= tPtr->visible.w - x) {
1700 y += layOutLine(tPtr, items, nitems, x, y);
1701 lw = width;
1702 x = bmargin;
1703 nitems = 0;
1706 if(nitems + 1 > itemsSize) {
1707 items = wrealloc(items,
1708 (++itemsSize)*sizeof(myLineItems));
1711 items[nitems].tb = tb;
1712 items[nitems].begin = begin;
1713 items[nitems].end = end;
1714 nitems++;
1716 begin = end;
1721 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1722 if(0&&tPtr->flags.laidOut
1723 && tb->next && tb->next->sections && tb->next->nsections>0
1724 && (tPtr->vpos + tPtr->visible.h
1725 < tb->next->sections[0]._y)) {
1726 if(tPtr->lastTextBlock->sections
1727 && tPtr->lastTextBlock->nsections > 0 ) {
1728 TextBlock *ltb = tPtr->lastTextBlock;
1729 int ly = ltb->sections[ltb->nsections-1]._y;
1730 int lh = ltb->sections[ltb->nsections-1].h;
1731 int ss, sd;
1733 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d;
1734 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections-1].max_d);
1736 y += layOutLine(tPtr, items, nitems, x, y);
1737 ss= ly+lh-y;
1738 sd = tPtr->docHeight-y;
1740 printf("dif %d-%d: %d\n", ss, sd, ss-sd);
1741 y += tb->next->sections[0]._y-y;
1742 nitems = 0;
1743 printf("nitems%d\n", nitems);
1744 if(ss-sd!=0)
1745 y = tPtr->docHeight+ss-sd;
1747 break;
1748 } else {
1749 tPtr->flags.laidOut = False;
1753 tb = tb->next;
1757 if (nitems > 0)
1758 y += layOutLine(tPtr, items, nitems, x, y);
1760 if (tPtr->docHeight != y+10) {
1761 tPtr->docHeight = y+10;
1762 updateScrollers(tPtr);
1765 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1766 XEvent event;
1768 tPtr->flags.horizOnDemand = True;
1769 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1770 event.type = Expose;
1771 handleEvents(&event, (void *)tPtr);
1773 } else if(tPtr->docWidth <= tPtr->visible.w
1774 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1775 tPtr->flags.horizOnDemand = False;
1776 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1779 tPtr->flags.laidOut = True;
1781 if(items && itemsSize > 0)
1782 wfree(items);
1787 static void
1788 textDidResize(W_ViewDelegate *self, WMView *view)
1790 Text *tPtr = (Text *)view->self;
1791 unsigned short w = tPtr->view->size.width;
1792 unsigned short h = tPtr->view->size.height;
1793 unsigned short rh = 0, vw = 0, rel;
1795 rel = (tPtr->flags.relief == WRFlat);
1797 if (tPtr->ruler && tPtr->flags.rulerShown) {
1798 WMMoveWidget(tPtr->ruler, 2, 2);
1799 WMResizeWidget(tPtr->ruler, w - 4, 40);
1800 rh = 40;
1803 if (tPtr->vS) {
1804 WMMoveWidget(tPtr->vS, 1 - (rel?1:0), rh + 1 - (rel?1:0));
1805 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel?2:0));
1806 vw = 20;
1807 WMSetRulerOffset(tPtr->ruler,22);
1808 } else WMSetRulerOffset(tPtr->ruler, 2);
1810 if (tPtr->hS) {
1811 if (tPtr->vS) {
1812 WMMoveWidget(tPtr->hS, vw, h - 21);
1813 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1814 } else {
1815 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1816 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1820 tPtr->visible.x = (tPtr->vS)?24:4;
1821 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1822 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1823 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1824 tPtr->visible.h -= (tPtr->hS)?20:0;
1825 tPtr->margins[0].right = tPtr->visible.w;
1827 if (tPtr->view->flags.realized) {
1829 if (tPtr->db) {
1830 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1831 tPtr->db = (Pixmap) NULL;
1834 if (tPtr->visible.w < 40)
1835 tPtr->visible.w = 40;
1836 if (tPtr->visible.h < 20)
1837 tPtr->visible.h = 20;
1839 if(!tPtr->db) {
1840 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1841 tPtr->view->window, tPtr->visible.w,
1842 tPtr->visible.h, tPtr->view->screen->depth);
1846 WMThawText(tPtr);
1849 W_ViewDelegate _TextViewDelegate =
1851 NULL,
1852 NULL,
1853 textDidResize,
1854 NULL,
1857 #define TEXT_BUFFER_INCR 8
1858 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1860 static void
1861 clearText(Text *tPtr)
1863 tPtr->vpos = tPtr->hpos = 0;
1864 tPtr->docHeight = tPtr->docWidth = 0;
1865 tPtr->cursor.x = -23;
1867 if (!tPtr->firstTextBlock)
1868 return;
1870 while (tPtr->currentTextBlock)
1871 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1873 tPtr->firstTextBlock = NULL;
1874 tPtr->currentTextBlock = NULL;
1875 tPtr->lastTextBlock = NULL;
1876 WMEmptyArray(tPtr->gfxItems);
1879 /* possibly remove a single character from the currentTextBlock,
1880 or if there's a selection, remove it...
1881 note that Delete and Backspace are treated differently */
1882 static void
1883 deleteTextInteractively(Text *tPtr, KeySym ksym)
1885 TextBlock *tb;
1886 Bool back = (Bool) (ksym == XK_BackSpace);
1887 Bool done = 1, wasFirst = 0;
1889 if (!tPtr->flags.editable)
1890 return;
1892 if ( !(tb = tPtr->currentTextBlock) )
1893 return;
1895 if (tPtr->flags.ownsSelection) {
1896 if(removeSelection(tPtr))
1897 layOutDocument(tPtr);
1898 return;
1901 wasFirst = tb->first;
1902 if (back && tPtr->tpos < 1) {
1903 if (tb->prior) {
1904 if(tb->prior->blank) {
1905 tPtr->currentTextBlock = tb->prior;
1906 WMRemoveTextBlock(tPtr);
1907 tPtr->currentTextBlock = tb;
1908 tb->first = True;
1909 layOutDocument(tPtr);
1910 return;
1911 } else {
1912 if(tb->blank) {
1913 TextBlock *prior = tb->prior;
1914 tPtr->currentTextBlock = tb;
1915 WMRemoveTextBlock(tPtr);
1916 tb = prior;
1917 } else {
1918 tb = tb->prior;
1921 if(tb->graphic)
1922 tPtr->tpos = 1;
1923 else
1924 tPtr->tpos = tb->used;
1926 tPtr->currentTextBlock = tb;
1927 done = 1;
1928 if(wasFirst) {
1929 if(tb->next)
1930 tb->next->first = False;
1931 layOutDocument(tPtr);
1932 return;
1938 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1939 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1940 if (back)
1941 tPtr->tpos--;
1942 memmove(&(tb->text[tPtr->tpos]),
1943 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1944 tb->used--;
1945 done = 0;
1948 /* if there are no characters left to back over in the textblock,
1949 but it still has characters to the right of the cursor: */
1950 if ( (back? (tPtr->tpos == 0 && !done) : ( tPtr->tpos >= tb->used))
1951 || tb->graphic) {
1953 /* no more chars, and it's marked as blank? */
1954 if(tb->blank) {
1955 TextBlock *sibling = (back? tb->prior : tb->next);
1957 if(tb->used == 0 || tb->graphic)
1958 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1960 if (sibling) {
1961 tPtr->currentTextBlock = sibling;
1962 if(tb->graphic)
1963 tPtr->tpos = (back? 1 : 0);
1964 else
1965 tPtr->tpos = (back? sibling->used : 0);
1967 /* no more chars, so mark it as blank */
1968 } else if(tb->used == 0) {
1969 tb->blank = 1;
1970 } else if(tb->graphic) {
1971 Bool hasNext = (Bool)(tb->next);
1973 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1974 if(hasNext) {
1975 tPtr->tpos = 0;
1976 } else if(tPtr->currentTextBlock) {
1977 tPtr->tpos = (tPtr->currentTextBlock->graphic?
1978 1 : tPtr->currentTextBlock->used);
1980 } else printf("DEBUG: unaccounted for... catch this!\n");
1983 layOutDocument(tPtr);
1987 static void
1988 insertTextInteractively(Text *tPtr, char *text, int len)
1990 TextBlock *tb;
1991 char *newline = NULL;
1993 if (!tPtr->flags.editable) {
1994 return;
1997 if (len < 1 || !text)
1998 return;
2001 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
2002 return;
2005 if (tPtr->flags.ownsSelection)
2006 removeSelection(tPtr);
2009 if (tPtr->flags.ignoreNewLine) {
2010 int i;
2011 for(i=0; i<len; i++) {
2012 if (text[i] == '\n')
2013 text[i] = ' ';
2017 tb = tPtr->currentTextBlock;
2018 if (!tb || tb->graphic) {
2019 tPtr->tpos = 0;
2020 WMAppendTextStream(tPtr, text);
2021 layOutDocument(tPtr);
2022 return;
2025 if ((newline = strchr(text, '\n'))) {
2026 int nlen = (int)(newline-text);
2027 int s = tb->used - tPtr->tpos;
2028 char save[s];
2031 if (!tb->blank && nlen>0) {
2032 if (s > 0) {
2033 memcpy(save, &tb->text[tPtr->tpos], s);
2034 tb->used -= (tb->used - tPtr->tpos);
2036 insertTextInteractively(tPtr, text, nlen);
2037 newline++;
2038 WMAppendTextStream(tPtr, newline);
2039 if (s>0)
2040 insertTextInteractively(tPtr, save, s);
2042 } else {
2043 if (tPtr->tpos>0 && tPtr->tpos < tb->used
2044 && !tb->graphic && tb->text) {
2046 unsigned short savePos = tPtr->tpos;
2047 void *ntb = WMCreateTextBlockWithText(
2048 tPtr, &tb->text[tPtr->tpos],
2049 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
2051 if(tb->sections[0].end == tPtr->tpos)
2052 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2053 NULL, tb->d.font, tb->color, True, 0));
2055 tb->used = savePos;
2056 WMAppendTextBlock(tPtr, ntb);
2057 tPtr->tpos = 0;
2059 } else if (tPtr->tpos == tb->used) {
2060 if(tPtr->flags.indentNewLine) {
2061 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2062 " ", tb->d.font, tb->color, True, 4));
2063 tPtr->tpos = 4;
2064 } else {
2065 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2066 NULL, tb->d.font, tb->color, True, 0));
2067 tPtr->tpos = 0;
2069 } else if (tPtr->tpos == 0) {
2070 if(tPtr->flags.indentNewLine) {
2071 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2072 " ", tb->d.font, tb->color, True, 4));
2073 } else {
2074 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
2075 NULL, tb->d.font, tb->color, True, 0));
2077 tPtr->tpos = 0;
2078 if(tPtr->currentTextBlock->next)
2079 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2083 } else {
2084 if (tb->used + len >= tb->allocated) {
2085 tb->allocated = reqBlockSize(tb->used+len);
2086 tb->text = wrealloc(tb->text, tb->allocated);
2089 if (tb->blank) {
2090 memcpy(tb->text, text, len);
2091 tb->used = len;
2092 tPtr->tpos = len;
2093 tb->text[tb->used] = 0;
2094 tb->blank = False;
2096 } else {
2097 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
2098 tb->used-tPtr->tpos+1);
2099 memmove(&tb->text[tPtr->tpos], text, len);
2100 tb->used += len;
2101 tPtr->tpos += len;
2102 tb->text[tb->used] = 0;
2107 layOutDocument(tPtr);
2111 static void
2112 selectRegion(Text *tPtr, int x, int y)
2115 if (x < 0 || y < 0)
2116 return;
2118 y += (tPtr->flags.rulerShown? 40: 0);
2119 y += tPtr->vpos;
2120 if (y>10)
2121 y -= 10; /* the original offset */
2123 x -= tPtr->visible.x-2;
2124 if (x<0)
2125 x=0;
2127 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2128 tPtr->sel.w = abs(tPtr->clicked.x - x);
2129 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2130 tPtr->sel.h = abs(tPtr->clicked.y - y);
2132 tPtr->flags.ownsSelection = True;
2133 paintText(tPtr);
2137 static void
2138 releaseSelection(Text *tPtr)
2140 TextBlock *tb = tPtr->firstTextBlock;
2142 while(tb) {
2143 tb->selected = False;
2144 tb = tb->next;
2146 tPtr->flags.ownsSelection = False;
2147 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
2148 CurrentTime);
2150 paintText(tPtr);
2154 WMData*
2155 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
2156 Atom *type)
2158 Text *tPtr = view->self;
2159 Display *dpy = tPtr->view->screen->display;
2160 Atom _TARGETS;
2161 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2162 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2163 WMData *data = NULL;
2166 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2167 char *text = WMGetTextSelectedStream(tPtr);
2169 if (text) {
2170 data = WMCreateDataWithBytes(text, strlen(text));
2171 WMSetDataFormat(data, TYPETEXT);
2173 *type = target;
2174 return data;
2175 } else printf("didn't get it\n");
2177 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2178 if (target == _TARGETS) {
2179 Atom *ptr;
2181 ptr = wmalloc(4 * sizeof(Atom));
2182 ptr[0] = _TARGETS;
2183 ptr[1] = XA_STRING;
2184 ptr[2] = TEXT;
2185 ptr[3] = COMPOUND_TEXT;
2187 data = WMCreateDataWithBytes(ptr, 4*4);
2188 WMSetDataFormat(data, 32);
2190 *type = target;
2191 return data;
2194 return NULL;
2197 static void
2198 lostHandler(WMView *view, Atom selection, void *cdata)
2200 releaseSelection((WMText *)view->self);
2203 static WMSelectionProcs selectionHandler = {
2204 requestHandler, lostHandler, NULL
2208 static void
2209 ownershipObserver(void *observerData, WMNotification *notification)
2211 if (observerData != WMGetNotificationClientData(notification))
2212 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2215 static void
2216 autoSelectText(Text *tPtr, int clicks)
2218 int x, start;
2219 TextBlock *tb;
2220 char *mark = NULL, behind, ahead;
2222 if(!(tb = tPtr->currentTextBlock))
2223 return;
2225 if(clicks == 2) {
2228 switch(tb->text[tPtr->tpos]) {
2229 case ' ': return;
2231 case '<': case '>': behind = '<'; ahead = '>'; break;
2232 case '{': case '}': behind = '{'; ahead = '}'; break;
2233 case '[': case ']': behind = '['; ahead = ']'; break;
2235 default: behind = ahead = ' ';
2238 tPtr->sel.y = tPtr->cursor.y+5;
2239 tPtr->sel.h = 6;/*tPtr->cursor.h-10;*/
2241 if(tb->graphic) {
2242 tPtr->sel.x = tb->sections[0].x;
2243 tPtr->sel.w = tb->sections[0].w;
2244 } else {
2245 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
2247 start = tPtr->tpos;
2248 while(start > 0 && tb->text[start-1] != behind)
2249 start--;
2251 x = tPtr->cursor.x;
2252 if(tPtr->tpos > start){
2253 x -= WMWidthOfString(font, &tb->text[start],
2254 tPtr->tpos - start);
2256 tPtr->sel.x = (x<0?0:x)+1;
2258 if((mark = strchr(&tb->text[start], ahead))) {
2259 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2260 (int)(mark - &tb->text[start]));
2261 } else if(tb->used > start) {
2262 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2263 tb->used - start);
2267 } else if(clicks == 3) {
2268 TextBlock *cur = tb;
2270 while(tb && !tb->first) {
2271 tb = tb->prior;
2273 tPtr->sel.y = tb->sections[0]._y;
2275 tb = cur;
2276 while(tb->next && !tb->next->first) {
2277 tb = tb->next;
2279 tPtr->sel.h = tb->sections[tb->nsections-1]._y
2280 + 5 - tPtr->sel.y;
2282 tPtr->sel.x = 0;
2283 tPtr->sel.w = tPtr->docWidth;
2284 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2287 if (!tPtr->flags.ownsSelection) {
2288 WMCreateSelectionHandler(tPtr->view,
2289 XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2290 tPtr->flags.ownsSelection = True;
2292 paintText(tPtr);
2297 static void
2298 fontChanged(void *observerData, WMNotification *notification)
2300 WMText *tPtr = (WMText *) observerData;
2301 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
2302 printf("fontChanged\n");
2304 if(!tPtr || !font)
2305 return;
2307 if (tPtr->flags.ownsSelection)
2308 WMSetTextSelectionFont(tPtr, font);
2312 static void
2313 handleTextKeyPress(Text *tPtr, XEvent *event)
2315 char buffer[64];
2316 KeySym ksym;
2317 int control_pressed = False;
2318 TextBlock *tb = NULL;
2320 if (((XKeyEvent *) event)->state & ControlMask)
2321 control_pressed = True;
2322 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2324 switch(ksym) {
2326 case XK_Home:
2327 if((tPtr->currentTextBlock = tPtr->firstTextBlock))
2328 tPtr->tpos = 0;
2329 updateCursorPosition(tPtr);
2330 paintText(tPtr);
2331 break;
2333 case XK_End:
2334 if((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2335 if(tPtr->currentTextBlock->graphic)
2336 tPtr->tpos = 1;
2337 else
2338 tPtr->tpos = tPtr->currentTextBlock->used;
2340 updateCursorPosition(tPtr);
2341 paintText(tPtr);
2342 break;
2344 case XK_Left:
2345 if(!(tb = tPtr->currentTextBlock))
2346 break;
2347 if(tb->graphic)
2348 goto L_imaGFX;
2350 if(tPtr->tpos==0) {
2351 L_imaGFX: if(tb->prior) {
2352 tPtr->currentTextBlock = tb->prior;
2353 if(tPtr->currentTextBlock->graphic)
2354 tPtr->tpos = 1;
2355 else
2356 tPtr->tpos = tPtr->currentTextBlock->used;
2358 if(!tb->first && tPtr->tpos > 0)
2359 tPtr->tpos--;
2360 } else tPtr->tpos = 0;
2361 } else tPtr->tpos--;
2362 updateCursorPosition(tPtr);
2363 paintText(tPtr);
2364 break;
2366 case XK_Right:
2367 if(!(tb = tPtr->currentTextBlock))
2368 break;
2369 if(tb->graphic)
2370 goto R_imaGFX;
2371 if(tPtr->tpos == tb->used) {
2372 R_imaGFX: if(tb->next) {
2373 tPtr->currentTextBlock = tb->next;
2374 tPtr->tpos = 0;
2375 if(!tb->next->first && tb->next->used>0)
2376 tPtr->tpos++;
2377 } else {
2378 if(tb->graphic)
2379 tPtr->tpos = 1;
2380 else
2381 tPtr->tpos = tb->used;
2383 } else tPtr->tpos++;
2384 updateCursorPosition(tPtr);
2385 paintText(tPtr);
2386 break;
2388 case XK_Down:
2389 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2390 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2391 paintText(tPtr);
2392 break;
2394 case XK_Up:
2395 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2396 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2397 paintText(tPtr);
2398 break;
2400 case XK_BackSpace:
2401 case XK_Delete:
2402 #ifdef XK_KP_Delete
2403 case XK_KP_Delete:
2404 #endif
2405 deleteTextInteractively(tPtr, ksym);
2406 updateCursorPosition(tPtr);
2407 paintText(tPtr);
2408 break;
2410 case XK_Control_R :
2411 case XK_Control_L :
2412 control_pressed = True;
2413 break;
2415 case XK_Tab:
2416 insertTextInteractively(tPtr, " ", 4);
2417 updateCursorPosition(tPtr);
2418 paintText(tPtr);
2419 break;
2421 case XK_Return:
2422 *buffer = '\n';
2423 default:
2424 if (*buffer != 0 && !control_pressed) {
2425 insertTextInteractively(tPtr, buffer, strlen(buffer));
2426 updateCursorPosition(tPtr);
2427 paintText(tPtr);
2429 } else if (control_pressed && ksym==XK_r) {
2430 Bool i = !tPtr->flags.rulerShown;
2431 WMShowTextRuler(tPtr, i);
2432 tPtr->flags.rulerShown = i;
2434 else if (control_pressed && *buffer == '\a')
2435 XBell(tPtr->view->screen->display, 0);
2436 else
2437 WMRelayToNextResponder(tPtr->view, event);
2440 if (!control_pressed && tPtr->flags.ownsSelection)
2441 releaseSelection(tPtr);
2445 static void
2446 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
2447 void *cdata, WMData *data)
2449 Text *tPtr = (Text *)view->self;
2450 char *text;
2452 tPtr->flags.waitingForSelection = 0;
2454 if (data) {
2455 text = (char*)WMDataBytes(data);
2457 if (tPtr->parser) {
2458 (tPtr->parser) (tPtr, (void *) text);
2459 layOutDocument(tPtr);
2460 } else insertTextInteractively(tPtr, text, strlen(text));
2461 updateCursorPosition(tPtr);
2462 paintText(tPtr);
2464 } else {
2465 int n;
2467 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2469 if (text) {
2470 text[n] = 0;
2471 if (tPtr->parser) {
2472 (tPtr->parser) (tPtr, (void *) text);
2473 layOutDocument(tPtr);
2474 } else insertTextInteractively(tPtr, text, n);
2475 updateCursorPosition(tPtr);
2476 paintText(tPtr);
2478 XFree(text);
2486 static void
2487 handleActionEvents(XEvent *event, void *data)
2489 Text *tPtr = (Text *)data;
2490 Display *dpy = event->xany.display;
2491 KeySym ksym;
2494 switch (event->type) {
2495 case KeyPress:
2496 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2497 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2498 tPtr->flags.extendSelection = True;
2499 return;
2502 if (tPtr->flags.focused) {
2503 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2504 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2505 GrabModeAsync, GrabModeAsync, None,
2506 tPtr->view->screen->invisibleCursor, CurrentTime);
2507 tPtr->flags.pointerGrabbed = True;
2508 handleTextKeyPress(tPtr, event);
2510 } break;
2512 case KeyRelease:
2513 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2514 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2515 tPtr->flags.extendSelection = False;
2516 return;
2517 /* end modify flag so selection can be extended */
2519 break;
2522 case MotionNotify:
2524 if (tPtr->flags.pointerGrabbed) {
2525 tPtr->flags.pointerGrabbed = False;
2526 XUngrabPointer(dpy, CurrentTime);
2529 if(tPtr->flags.waitingForSelection)
2530 break;
2532 if ((event->xmotion.state & Button1Mask)) {
2533 TextBlock *tb = tPtr->currentTextBlock;
2535 if(tb && tPtr->flags.isOverGraphic &&
2536 tb->graphic && !tb->object) {
2537 WMSize offs;
2538 WMPixmap *pixmap = tb->d.pixmap;
2539 char *types[2] = {"application/X-image", NULL};
2541 offs.width = 2;
2542 offs.height = 2;
2544 WMDragImageFromView(tPtr->view, pixmap, types,
2545 wmkpoint(event->xmotion.x_root, event->xmotion.y_root),
2546 offs, event, True);
2549 } else {
2550 if (!tPtr->flags.ownsSelection) {
2551 WMCreateSelectionHandler(tPtr->view,
2552 XA_PRIMARY, event->xbutton.time,
2553 &selectionHandler, NULL);
2554 tPtr->flags.ownsSelection = True;
2557 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2558 break;
2561 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2562 break;
2565 case ButtonPress:
2567 if (tPtr->flags.pointerGrabbed) {
2568 tPtr->flags.pointerGrabbed = False;
2569 XUngrabPointer(dpy, CurrentTime);
2570 break;
2573 if (tPtr->flags.waitingForSelection)
2574 break;
2576 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2577 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2578 return;
2581 if (tPtr->flags.ownsSelection)
2582 releaseSelection(tPtr);
2585 if (event->xbutton.button == Button1) {
2587 if(WMIsDoubleClick(event)) {
2588 TextBlock *tb = tPtr->currentTextBlock;
2590 tPtr->lastClickTime = event->xbutton.time;
2591 if(tb && tb->graphic && !tb->object) {
2592 char desc[tb->used+1];
2593 memcpy(desc, tb->text, tb->used);
2594 desc[tb->used] = 0;
2595 if(tPtr->delegate) {
2596 if(tPtr->delegate->didDoubleClickOnPicture)
2597 (*tPtr->delegate->didDoubleClickOnPicture)
2598 (tPtr->delegate, desc);
2600 } else {
2601 autoSelectText(tPtr, 2);
2603 break;
2604 } else if(event->xbutton.time - tPtr->lastClickTime
2605 < WINGsConfiguration.doubleClickDelay) {
2606 tPtr->lastClickTime = event->xbutton.time;
2607 autoSelectText(tPtr, 3);
2608 break;
2611 if (!tPtr->flags.focused) {
2612 WMSetFocusToWidget(tPtr);
2613 tPtr->flags.focused = True;
2616 tPtr->lastClickTime = event->xbutton.time;
2617 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2618 paintText(tPtr);
2621 if (event->xbutton.button
2622 == WINGsConfiguration.mouseWheelDown) {
2623 WMScrollText(tPtr, 16);
2624 break;
2627 if (event->xbutton.button
2628 == WINGsConfiguration.mouseWheelUp) {
2629 WMScrollText(tPtr, -16);
2630 break;
2633 if (event->xbutton.button == Button2) {
2634 char *text = NULL;
2635 int n;
2637 if (!tPtr->flags.editable) {
2638 XBell(dpy, 0);
2639 break;
2642 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2643 event->xbutton.time, pasteText, NULL)) {
2645 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2646 tPtr->flags.waitingForSelection = 0;
2648 if (text) {
2649 text[n] = 0;
2651 if (tPtr->parser) {
2652 (tPtr->parser) (tPtr, (void *) text);
2653 layOutDocument(tPtr);
2655 else
2656 insertTextInteractively(tPtr, text, n);
2658 XFree(text);
2659 #if 0
2660 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2661 (void*)WMInsertTextEvent);
2662 #endif
2663 updateCursorPosition(tPtr);
2664 paintText(tPtr);
2666 } else {
2667 tPtr->flags.waitingForSelection = True;
2670 break;
2674 case ButtonRelease:
2675 if (tPtr->flags.pointerGrabbed) {
2676 tPtr->flags.pointerGrabbed = False;
2677 XUngrabPointer(dpy, CurrentTime);
2678 break;
2681 if (tPtr->flags.waitingForSelection)
2682 break;
2688 static void
2689 handleEvents(XEvent *event, void *data)
2691 Text *tPtr = (Text *)data;
2693 switch(event->type) {
2694 case Expose:
2696 if (event->xexpose.count!=0)
2697 break;
2699 if(tPtr->hS) {
2700 if (!(W_VIEW(tPtr->hS))->flags.realized)
2701 WMRealizeWidget(tPtr->hS);
2704 if(tPtr->vS) {
2705 if (!(W_VIEW(tPtr->vS))->flags.realized)
2706 WMRealizeWidget(tPtr->vS);
2709 if(tPtr->ruler) {
2710 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2711 WMRealizeWidget(tPtr->ruler);
2715 if(!tPtr->db)
2716 textDidResize(tPtr->view->delegate, tPtr->view);
2718 paintText(tPtr);
2719 break;
2721 case FocusIn:
2722 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2723 != tPtr->view)
2724 return;
2725 tPtr->flags.focused = True;
2726 #if DO_BLINK
2727 if (tPtr->flags.editable && !tPtr->timerID) {
2728 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2729 blinkCursor, tPtr);
2731 #endif
2733 break;
2735 case FocusOut:
2736 tPtr->flags.focused = False;
2737 paintText(tPtr);
2738 #if DO_BLINK
2739 if (tPtr->timerID) {
2740 WMDeleteTimerHandler(tPtr->timerID);
2741 tPtr->timerID = NULL;
2743 #endif
2744 break;
2747 case DestroyNotify:
2748 clearText(tPtr);
2749 if(tPtr->db)
2750 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2751 if(tPtr->gfxItems)
2752 WMEmptyArray(tPtr->gfxItems);
2753 #if DO_BLINK
2754 if (tPtr->timerID)
2755 WMDeleteTimerHandler(tPtr->timerID);
2756 #endif
2757 WMReleaseFont(tPtr->dFont);
2758 WMReleaseColor(tPtr->dColor);
2759 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2760 WMRemoveNotificationObserver(tPtr);
2762 wfree(tPtr);
2764 break;
2770 static void
2771 insertPlainText(Text *tPtr, char *text)
2773 char *start, *mark;
2774 void *tb = NULL;
2776 start = text;
2777 while (start) {
2778 mark = strchr(start, '\n');
2779 if (mark) {
2780 tb = WMCreateTextBlockWithText(tPtr,
2781 start, tPtr->dFont,
2782 tPtr->dColor, tPtr->flags.first, (int)(mark-start));
2783 start = mark+1;
2784 tPtr->flags.first = True;
2785 } else {
2786 if (start && strlen(start)) {
2787 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2788 tPtr->dColor, tPtr->flags.first, strlen(start));
2789 } else tb = NULL;
2790 tPtr->flags.first = False;
2791 start = mark;
2794 if (tPtr->flags.prepend)
2795 WMPrependTextBlock(tPtr, tb);
2796 else
2797 WMAppendTextBlock(tPtr, tb);
2802 static void
2803 rulerMoveCallBack(WMWidget *w, void *self)
2805 Text *tPtr = (Text *)self;
2807 if (!tPtr)
2808 return;
2809 if (W_CLASS(tPtr) != WC_Text)
2810 return;
2812 paintText(tPtr);
2816 static void
2817 rulerReleaseCallBack(WMWidget *w, void *self)
2819 Text *tPtr = (Text *)self;
2821 if (!tPtr)
2822 return;
2823 if (W_CLASS(tPtr) != WC_Text)
2824 return;
2826 WMThawText(tPtr);
2827 return;
2830 static unsigned
2831 draggingSourceOperation(WMView *self, Bool local)
2833 return WDOperationCopy;
2836 static WMData*
2837 fetchDragData(WMView *self, char *type)
2839 TextBlock *tb = ((WMText *)self->self)->currentTextBlock;
2840 char *desc;
2841 WMData *data;
2843 if (!tb)
2844 return NULL;
2846 printf("type is [%s]\n", type);
2847 desc = wmalloc(tb->used+1);
2848 memcpy(desc, tb->text, tb->used);
2849 desc[tb->used] = 0;
2850 data = WMCreateDataWithBytes(desc, strlen(desc)+1);
2852 wfree(desc);
2854 return data;
2858 static WMDragSourceProcs _DragSourceProcs = {
2859 draggingSourceOperation,
2860 NULL,
2861 NULL,
2862 fetchDragData
2866 static unsigned
2867 draggingEntered(WMView *self, WMDraggingInfo *info)
2869 printf("draggingEntered\n");
2870 return WDOperationCopy;
2874 static unsigned
2875 draggingUpdated(WMView *self, WMDraggingInfo *info)
2877 return WDOperationCopy;
2881 static void
2882 draggingExited(WMView *self, WMDraggingInfo *info)
2884 printf("draggingExited\n");
2887 static Bool
2888 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2890 printf("prepareForDragOperation\n");
2891 return True;
2895 char *badbadbad;
2897 static void
2898 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2899 void *cdata, WMData *data)
2901 badbadbad = wstrdup((char *)WMDataBytes(data));
2905 /* when it's done in WINGs, remove this */
2907 Bool
2908 requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2910 WMScreen *scr = W_VIEW_SCREEN(view);
2912 if (!WMRequestSelection(scr->dragInfo.destView,
2913 scr->xdndSelectionAtom,
2914 XInternAtom(scr->display, type, False),
2915 scr->dragInfo.timestamp,
2916 receivedData, &scr->dragInfo)) {
2917 wwarning("could not request data for dropped data");
2921 XEvent ev;
2923 ev.type = ClientMessage;
2924 ev.xclient.message_type = scr->xdndFinishedAtom;
2925 ev.xclient.format = 32;
2926 ev.xclient.window = info->destinationWindow;
2927 ev.xclient.data.l[0] = 0;
2928 ev.xclient.data.l[1] = 0;
2929 ev.xclient.data.l[2] = 0;
2930 ev.xclient.data.l[3] = 0;
2931 ev.xclient.data.l[4] = 0;
2933 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2934 XFlush(scr->display);
2936 return True;
2939 static Bool
2940 performDragOperation(WMView *self, WMDraggingInfo *info)
2942 WMColor *color;
2943 WMText *tPtr = (WMText *)self->self;
2945 if (!tPtr)
2946 return True;
2948 requestDroppedData(tPtr->view, info, "application/X-color");
2949 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2950 if(color) {
2951 WMSetTextSelectionColor(tPtr, color);
2952 WMReleaseColor(color);
2957 return True;
2960 static void
2961 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2963 printf("concludeDragOperation\n");
2967 static WMDragDestinationProcs _DragDestinationProcs = {
2968 draggingEntered,
2969 draggingUpdated,
2970 draggingExited,
2971 prepareForDragOperation,
2972 performDragOperation,
2973 concludeDragOperation
2977 char *
2978 getStream(WMText *tPtr, int sel, int array)
2980 TextBlock *tb = NULL;
2981 char *text = NULL;
2982 unsigned long where = 0;
2984 if (!tPtr)
2985 return NULL;
2987 if (!(tb = tPtr->firstTextBlock))
2988 return NULL;
2990 if (tPtr->writer) {
2991 (tPtr->writer) (tPtr, (void *) text);
2992 return text;
2995 tb = tPtr->firstTextBlock;
2996 while (tb) {
2998 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
3000 if (!sel || (tb->graphic && tb->selected)) {
3002 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
3003 && tb != tPtr->firstTextBlock) {
3004 text = wrealloc(text, where+1);
3005 text[where++] = '\n';
3008 if(tb->blank)
3009 goto _gSnext;
3011 if(tb->graphic && array) {
3012 text = wrealloc(text, where+4);
3013 text[where++] = 0xFA;
3014 text[where++] = (tb->used>>8)&0x0ff;
3015 text[where++] = tb->used&0x0ff;
3016 text[where++] = tb->allocated; /* extra info */
3018 text = wrealloc(text, where+tb->used);
3019 memcpy(&text[where], tb->text, tb->used);
3020 where += tb->used;
3023 } else if (sel && tb->selected) {
3025 if (!tPtr->flags.ignoreNewLine && tb->blank) {
3026 text = wrealloc(text, where+1);
3027 text[where++] = '\n';
3030 if(tb->blank)
3031 goto _gSnext;
3033 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
3034 memcpy(&text[where], &tb->text[tb->s_begin],
3035 tb->s_end - tb->s_begin);
3036 where += tb->s_end - tb->s_begin;
3041 _gSnext:tb = tb->next;
3044 /* +1 for the end of string, let's be nice */
3045 text = wrealloc(text, where+1);
3046 text[where] = 0;
3047 return text;
3051 static void
3052 releaseStreamObjects(void *data)
3054 if(data)
3055 wfree(data);
3058 WMArray *
3059 getStreamObjects(WMText *tPtr, int sel)
3061 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
3062 WMData *data;
3063 char *stream;
3064 unsigned short len;
3065 char *start, *fa, *desc;
3067 stream = getStream(tPtr, sel, 1);
3068 if(!stream)
3069 return NULL;
3071 start = stream;
3072 while (start) {
3074 fa = strchr(start, 0xFA);
3075 if (fa) {
3076 if((int)(fa - start)>0) {
3077 desc = start;
3078 desc[(int)(fa - start)] = 0;
3079 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
3080 WMSetDataFormat(data, TYPETEXT);
3081 WMAddToArray(array, (void *) data);
3084 len = *(fa+1)*0xff + *(fa+2);
3085 data = WMCreateDataWithBytes((void *)(fa+4), len);
3086 WMSetDataFormat(data, *(fa+3));
3087 WMAddToArray(array, (void *) data);
3088 start = fa + len + 4;
3090 } else {
3091 if (start && strlen(start)) {
3092 data = WMCreateDataWithBytes((void *)start, strlen(start));
3093 WMSetDataFormat(data, TYPETEXT);
3094 WMAddToArray(array, (void *) data);
3096 start = fa;
3100 wfree(stream);
3101 return array;
3105 WMText *
3106 WMCreateTextForDocumentType(WMWidget *parent, WMAction *parser, WMAction *writer)
3108 Text *tPtr;
3109 Display *dpy;
3110 WMScreen *scr;
3111 XGCValues gcv;
3113 tPtr = wmalloc(sizeof(Text));
3114 memset(tPtr, 0, sizeof(Text));
3115 tPtr->widgetClass = WC_Text;
3116 tPtr->view = W_CreateView(W_VIEW(parent));
3117 if (!tPtr->view) {
3118 perror("could not create text's view\n");
3119 wfree(tPtr);
3120 return NULL;
3123 dpy = tPtr->view->screen->display;
3124 scr = tPtr->view->screen;
3126 tPtr->view->self = tPtr;
3127 tPtr->view->attribs.cursor = scr->textCursor;
3128 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
3129 W_ResizeView(tPtr->view, 250, 200);
3131 tPtr->dColor = WMWhiteColor(scr);
3132 tPtr->bgGC = WMColorGC(tPtr->dColor);
3133 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
3134 WMReleaseColor(tPtr->dColor);
3136 tPtr->dColor = WMBlackColor(scr);
3137 tPtr->fgGC = WMColorGC(tPtr->dColor);
3139 gcv.graphics_exposures = False;
3140 gcv.foreground = W_PIXEL(scr->gray);
3141 gcv.background = W_PIXEL(scr->darkGray);
3142 gcv.fill_style = FillStippled;
3143 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr),
3144 STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
3145 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3146 GCForeground|GCBackground|GCStipple
3147 |GCFillStyle|GCGraphicsExposures, &gcv);
3149 tPtr->ruler = NULL;
3150 tPtr->vS = NULL;
3151 tPtr->hS = NULL;
3153 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(scr, 12));
3155 tPtr->view->delegate = &_TextViewDelegate;
3157 tPtr->delegate = NULL;
3159 #if DO_BLINK
3160 tPtr->timerID = NULL;
3161 #endif
3163 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
3164 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
3165 handleEvents, tPtr);
3167 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
3168 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
3169 handleActionEvents, tPtr);
3171 WMAddNotificationObserver(ownershipObserver, tPtr,
3172 "_lostOwnership", tPtr);
3174 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3175 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3179 char *types[3] = {"application/X-color", "application/X-image", NULL};
3180 WMRegisterViewForDraggedTypes(tPtr->view, types);
3183 WMAddNotificationObserver(fontChanged, tPtr,
3184 "WMFontPanelDidChangeNotification", tPtr);
3186 tPtr->firstTextBlock = NULL;
3187 tPtr->lastTextBlock = NULL;
3188 tPtr->currentTextBlock = NULL;
3189 tPtr->tpos = 0;
3191 tPtr->gfxItems = WMCreateArray(4);
3193 tPtr->parser = parser;
3194 tPtr->writer = writer;
3196 tPtr->sel.x = tPtr->sel.y = 2;
3197 tPtr->sel.w = tPtr->sel.h = 0;
3199 tPtr->clicked.x = tPtr->clicked.y = 2;
3201 tPtr->visible.x = tPtr->visible.y = 2;
3202 tPtr->visible.h = tPtr->view->size.height;
3203 tPtr->visible.w = tPtr->view->size.width - 4;
3205 tPtr->cursor.x = -23;
3207 tPtr->docWidth = 0;
3208 tPtr->docHeight = 0;
3209 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
3210 default_bullet);
3211 tPtr->db = (Pixmap) NULL;
3212 tPtr->bgPixmap = NULL;
3214 tPtr->margins = WMGetRulerMargins(NULL);
3215 tPtr->margins->right = tPtr->visible.w;
3216 tPtr->nMargins = 1;
3218 tPtr->flags.rulerShown = False;
3219 tPtr->flags.monoFont = False;
3220 tPtr->flags.focused = False;
3221 tPtr->flags.editable = True;
3222 tPtr->flags.ownsSelection = False;
3223 tPtr->flags.pointerGrabbed = False;
3224 tPtr->flags.extendSelection = False;
3225 tPtr->flags.frozen = False;
3226 tPtr->flags.cursorShown = True;
3227 tPtr->flags.acceptsGraphic = False;
3228 tPtr->flags.horizOnDemand = False;
3229 tPtr->flags.needsLayOut = False;
3230 tPtr->flags.ignoreNewLine = False;
3231 tPtr->flags.indentNewLine = False;
3232 tPtr->flags.laidOut = False;
3233 tPtr->flags.ownsSelection = False;
3234 tPtr->flags.waitingForSelection = False;
3235 tPtr->flags.prepend = False;
3236 tPtr->flags.isOverGraphic = False;
3237 tPtr->flags.relief = WRSunken;
3238 tPtr->flags.isOverGraphic = 0;
3239 tPtr->flags.alignment = WALeft;
3240 tPtr->flags.first = True;
3242 return tPtr;
3245 void
3246 WMPrependTextStream(WMText *tPtr, char *text)
3248 CHECK_CLASS(tPtr, WC_Text);
3250 if(!text) {
3251 if (tPtr->flags.ownsSelection)
3252 releaseSelection(tPtr);
3253 clearText(tPtr);
3254 updateScrollers(tPtr);
3255 return;
3258 tPtr->flags.prepend = True;
3259 if (text && tPtr->parser)
3260 (tPtr->parser) (tPtr, (void *) text);
3261 else
3262 insertPlainText(tPtr, text);
3264 tPtr->flags.needsLayOut = True;
3265 tPtr->tpos = 0;
3266 if(!tPtr->flags.frozen) {
3267 layOutDocument(tPtr);
3272 void
3273 WMAppendTextStream(WMText *tPtr, char *text)
3275 CHECK_CLASS(tPtr, WC_Text);
3277 if(!text) {
3278 if (tPtr->flags.ownsSelection)
3279 releaseSelection(tPtr);
3280 clearText(tPtr);
3281 updateScrollers(tPtr);
3282 return;
3285 tPtr->flags.prepend = False;
3286 if (text && tPtr->parser)
3287 (tPtr->parser) (tPtr, (void *) text);
3288 else
3289 insertPlainText(tPtr, text);
3291 tPtr->flags.needsLayOut = True;
3292 if(tPtr->currentTextBlock) {
3293 if(tPtr->currentTextBlock->graphic)
3294 tPtr->tpos = 1;
3295 else
3296 tPtr->tpos = tPtr->currentTextBlock->used;
3299 if(!tPtr->flags.frozen) {
3300 layOutDocument(tPtr);
3305 char *
3306 WMGetTextStream(WMText *tPtr)
3308 CHECK_CLASS(tPtr, WC_Text);
3309 return getStream(tPtr, 0, 0);
3312 char *
3313 WMGetTextSelectedStream(WMText *tPtr)
3315 CHECK_CLASS(tPtr, WC_Text);
3316 return getStream(tPtr, 1, 0);
3319 WMArray *
3320 WMGetTextObjects(WMText *tPtr)
3322 CHECK_CLASS(tPtr, WC_Text);
3323 return getStreamObjects(tPtr, 0);
3326 WMArray *
3327 WMGetTextSelectedObjects(WMText *tPtr)
3329 CHECK_CLASS(tPtr, WC_Text);
3330 return getStreamObjects(tPtr, 1);
3334 void
3335 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
3337 CHECK_CLASS(tPtr, WC_Text);
3339 tPtr->delegate = delegate;
3343 void *
3344 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
3345 char *description, WMColor *color,
3346 unsigned short first, unsigned short extraInfo)
3348 TextBlock *tb;
3350 if (!w || !description || !color)
3351 return NULL;
3353 tb = wmalloc(sizeof(TextBlock));
3354 if (!tb)
3355 return NULL;
3357 tb->text = wstrdup(description);
3358 tb->used = strlen(description);
3359 tb->blank = False;
3360 tb->d.widget = w;
3361 tb->color = WMRetainColor(color);
3362 tb->marginN = newMargin(tPtr, NULL);
3363 tb->allocated = extraInfo;
3364 tb->first = first;
3365 tb->kanji = False;
3366 tb->graphic = True;
3367 tb->object = True;
3368 tb->underlined = False;
3369 tb->selected = False;
3370 tb->script = 0;
3371 tb->sections = NULL;
3372 tb->nsections = 0;
3373 tb->prior = NULL;
3374 tb->next = NULL;
3376 return tb;
3380 void *
3381 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
3382 char *description, WMColor *color,
3383 unsigned short first, unsigned short extraInfo)
3385 TextBlock *tb;
3387 if (!p || !description || !color)
3388 return NULL;
3390 tb = wmalloc(sizeof(TextBlock));
3391 if (!tb)
3392 return NULL;
3394 tb->text = wstrdup(description);
3395 tb->used = strlen(description);
3396 tb->blank = False;
3397 tb->d.pixmap = WMRetainPixmap(p);
3398 tb->color = WMRetainColor(color);
3399 tb->marginN = newMargin(tPtr, NULL);
3400 tb->allocated = extraInfo;
3401 tb->first = first;
3402 tb->kanji = False;
3403 tb->graphic = True;
3404 tb->object = False;
3405 tb->underlined = False;
3406 tb->selected = False;
3407 tb->script = 0;
3408 tb->sections = NULL;
3409 tb->nsections = 0;
3410 tb->prior = NULL;
3411 tb->next = NULL;
3413 return tb;
3417 void*
3418 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3419 unsigned short first, unsigned short len)
3421 TextBlock *tb;
3423 if (!font || !color)
3424 return NULL;
3426 tb = wmalloc(sizeof(TextBlock));
3427 if (!tb)
3428 return NULL;
3430 tb->allocated = reqBlockSize(len);
3431 tb->text = (char *)wmalloc(tb->allocated);
3432 memset(tb->text, 0, tb->allocated);
3434 if (len < 1|| !text || (*text == '\n' && len==1 )) {
3435 *tb->text = ' ';
3436 tb->used = 1;
3437 tb->blank = True;
3438 } else {
3439 memcpy(tb->text, text, len);
3440 tb->used = len;
3441 tb->blank = False;
3443 tb->text[tb->used] = 0;
3445 tb->d.font = WMRetainFont(font);
3446 tb->color = WMRetainColor(color);
3447 tb->marginN = newMargin(tPtr, NULL);
3448 tb->first = first;
3449 tb->kanji = False;
3450 tb->graphic = False;
3451 tb->underlined = False;
3452 tb->selected = False;
3453 tb->script = 0;
3454 tb->sections = NULL;
3455 tb->nsections = 0;
3456 tb->prior = NULL;
3457 tb->next = NULL;
3458 return tb;
3461 void
3462 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3463 unsigned int kanji, unsigned int underlined, int script,
3464 WMRulerMargins *margins)
3466 TextBlock *tb = (TextBlock *) vtb;
3467 if (!tb)
3468 return;
3470 tb->first = first;
3471 tb->kanji = kanji;
3472 tb->underlined = underlined;
3473 tb->script = script;
3474 tb->marginN = newMargin(tPtr, margins);
3477 void
3478 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3479 unsigned int *kanji, unsigned int *underlined, int *script,
3480 WMRulerMargins *margins)
3482 TextBlock *tb = (TextBlock *) vtb;
3483 if (!tb)
3484 return;
3486 if (first) *first = tb->first;
3487 if (kanji) *kanji = tb->kanji;
3488 if (underlined) *underlined = tb->underlined;
3489 if (script) *script = tb->script;
3490 if (margins) margins = &tPtr->margins[tb->marginN];
3495 void
3496 WMPrependTextBlock(WMText *tPtr, void *vtb)
3498 TextBlock *tb = (TextBlock *)vtb;
3500 if (!tPtr || !tb)
3501 return;
3503 if (tb->graphic) {
3504 if(tb->object) {
3505 WMWidget *w = tb->d.widget;
3506 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3507 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3508 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3511 WMAddToArray(tPtr->gfxItems, (void *)tb);
3512 tPtr->tpos = 1;
3514 } else {
3515 tPtr->tpos = tb->used;
3518 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3519 tb->next = tb->prior = NULL;
3520 tb->first = True;
3521 tPtr->lastTextBlock = tPtr->firstTextBlock
3522 = tPtr->currentTextBlock = tb;
3523 return;
3526 if(!tb->first) {
3527 tb->marginN = tPtr->currentTextBlock->marginN;
3530 tb->next = tPtr->currentTextBlock;
3531 tb->prior = tPtr->currentTextBlock->prior;
3532 if (tPtr->currentTextBlock->prior)
3533 tPtr->currentTextBlock->prior->next = tb;
3535 tPtr->currentTextBlock->prior = tb;
3536 if (!tb->prior)
3537 tPtr->firstTextBlock = tb;
3539 tPtr->currentTextBlock = tb;
3543 void
3544 WMAppendTextBlock(WMText *tPtr, void *vtb)
3546 TextBlock *tb = (TextBlock *)vtb;
3548 if (!tPtr || !tb)
3549 return;
3551 if (tb->graphic) {
3552 if(tb->object) {
3553 WMWidget *w = tb->d.widget;
3554 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3555 (W_VIEW(w))->attribs.cursor =
3556 tPtr->view->screen->defaultCursor;
3557 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3560 WMAddToArray(tPtr->gfxItems, (void *)tb);
3561 tPtr->tpos = 1;
3563 } else {
3564 tPtr->tpos = tb->used;
3568 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3569 tb->next = tb->prior = NULL;
3570 tb->first = True;
3571 tPtr->lastTextBlock = tPtr->firstTextBlock
3572 = tPtr->currentTextBlock = tb;
3573 return;
3576 if(!tb->first) {
3577 tb->marginN = tPtr->currentTextBlock->marginN;
3580 tb->next = tPtr->currentTextBlock->next;
3581 tb->prior = tPtr->currentTextBlock;
3582 if (tPtr->currentTextBlock->next)
3583 tPtr->currentTextBlock->next->prior = tb;
3585 tPtr->currentTextBlock->next = tb;
3587 if (!tb->next)
3588 tPtr->lastTextBlock = tb;
3590 tPtr->currentTextBlock = tb;
3593 void *
3594 WMRemoveTextBlock(WMText *tPtr)
3596 TextBlock *tb = NULL;
3598 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3599 || !tPtr->currentTextBlock) {
3600 return NULL;
3603 tb = tPtr->currentTextBlock;
3604 if (tb->graphic) {
3605 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3607 if(tb->object) {
3608 WMUnmapWidget(tb->d.widget);
3612 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3613 if (tPtr->currentTextBlock->next)
3614 tPtr->currentTextBlock->next->prior = NULL;
3616 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3617 tPtr->currentTextBlock = tPtr->firstTextBlock;
3619 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3620 tPtr->currentTextBlock->prior->next = NULL;
3621 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3622 tPtr->currentTextBlock = tPtr->lastTextBlock;
3623 } else {
3624 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3625 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3626 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3629 return (void *)tb;
3633 static void
3634 destroyWidget(WMWidget *widget)
3636 if(!widget)
3637 return;
3639 WMDestroyWidget(widget);
3640 wfree(widget);
3643 void
3644 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3646 TextBlock *tb = (TextBlock *)vtb;
3647 if (!tPtr || !tb)
3648 return;
3650 if (tb->graphic) {
3651 if(tb->object) {
3652 /* naturally, there's a danger to destroying widgets whose action
3653 brings us here: ie. press a button to destroy it...
3654 need to find a safer way. till then... this stays commented out */
3655 /* 5 months later... destroy it 10 seconds after now which should be
3656 enough time for the widget's action to be completed... :-) */
3657 WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);
3658 tb->d.widget = NULL;
3660 } else {
3661 WMReleasePixmap(tb->d.pixmap);
3662 tb->d.pixmap = NULL;
3664 } else {
3665 WMReleaseFont(tb->d.font);
3668 WMReleaseColor(tb->color);
3669 if (tb->sections && tb->nsections > 0)
3670 wfree(tb->sections);
3671 wfree(tb->text);
3672 wfree(tb);
3673 tb = NULL;
3678 void
3679 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3681 if (!tPtr)
3682 return;
3684 if (color)
3685 tPtr->fgGC = WMColorGC(color);
3686 else {
3687 WMColor *color = WMBlackColor(tPtr->view->screen);
3688 tPtr->fgGC = WMColorGC(color);
3689 WMReleaseColor(color);
3692 paintText(tPtr);
3695 void
3696 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3698 if (!tPtr)
3699 return;
3701 if (color) {
3702 tPtr->bgGC = WMColorGC(color);
3703 W_SetViewBackgroundColor(tPtr->view, color);
3704 } else {
3705 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3706 W_SetViewBackgroundColor(tPtr->view,
3707 WMWhiteColor(tPtr->view->screen));
3710 paintText(tPtr);
3713 void WMSetTextBackgroundPixmap(WMText *tPtr, WMPixmap *pixmap)
3715 if (!tPtr)
3716 return;
3718 if (tPtr->bgPixmap)
3719 WMReleasePixmap(tPtr->bgPixmap);
3721 if (pixmap)
3722 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3723 else
3724 tPtr->bgPixmap = NULL;
3727 void
3728 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3730 if (!tPtr)
3731 return;
3732 tPtr->flags.relief = relief;
3733 textDidResize(tPtr->view->delegate, tPtr->view);
3736 void
3737 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3739 if (!tPtr)
3740 return;
3742 if (shouldhave && !tPtr->hS) {
3743 tPtr->hS = WMCreateScroller(tPtr);
3744 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3745 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3746 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3747 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3748 WMMapWidget(tPtr->hS);
3749 } else if (!shouldhave && tPtr->hS) {
3750 WMUnmapWidget(tPtr->hS);
3751 WMDestroyWidget(tPtr->hS);
3752 tPtr->hS = NULL;
3755 tPtr->hpos = 0;
3756 tPtr->prevHpos = 0;
3757 textDidResize(tPtr->view->delegate, tPtr->view);
3761 void
3762 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3764 if (!tPtr)
3765 return;
3767 if(shouldhave && !tPtr->ruler) {
3768 tPtr->ruler = WMCreateRuler(tPtr);
3769 (W_VIEW(tPtr->ruler))->attribs.cursor =
3770 tPtr->view->screen->defaultCursor;
3771 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3772 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3773 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3774 } else if(!shouldhave && tPtr->ruler) {
3775 WMShowTextRuler(tPtr, False);
3776 WMDestroyWidget(tPtr->ruler);
3777 tPtr->ruler = NULL;
3779 textDidResize(tPtr->view->delegate, tPtr->view);
3782 void
3783 WMShowTextRuler(WMText *tPtr, Bool show)
3785 if(!tPtr)
3786 return;
3787 if(!tPtr->ruler)
3788 return;
3790 if(tPtr->flags.monoFont)
3791 show = False;
3793 tPtr->flags.rulerShown = show;
3794 if(show) {
3795 WMMapWidget(tPtr->ruler);
3796 } else {
3797 WMUnmapWidget(tPtr->ruler);
3800 textDidResize(tPtr->view->delegate, tPtr->view);
3803 Bool
3804 WMGetTextRulerShown(WMText *tPtr)
3806 if(!tPtr)
3807 return 0;
3809 if(!tPtr->ruler)
3810 return 0;
3812 return tPtr->flags.rulerShown;
3816 void
3817 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3819 if (!tPtr)
3820 return;
3822 if (shouldhave && !tPtr->vS) {
3823 tPtr->vS = WMCreateScroller(tPtr);
3824 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3825 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3826 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3827 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3828 WMMapWidget(tPtr->vS);
3829 } else if (!shouldhave && tPtr->vS) {
3830 WMUnmapWidget(tPtr->vS);
3831 WMDestroyWidget(tPtr->vS);
3832 tPtr->vS = NULL;
3835 tPtr->vpos = 0;
3836 tPtr->prevVpos = 0;
3837 textDidResize(tPtr->view->delegate, tPtr->view);
3842 Bool
3843 WMScrollText(WMText *tPtr, int amount)
3845 Bool scroll=False;
3846 if (!tPtr)
3847 return False;
3848 if (amount == 0 || !tPtr->view->flags.realized)
3849 return False;
3851 if (amount < 0) {
3852 if (tPtr->vpos > 0) {
3853 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3854 else tPtr->vpos=0;
3855 scroll=True;
3857 } else {
3858 int limit = tPtr->docHeight - tPtr->visible.h;
3859 if (tPtr->vpos < limit) {
3860 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3861 else tPtr->vpos = limit;
3862 scroll = True;
3866 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3867 updateScrollers(tPtr);
3868 paintText(tPtr);
3870 tPtr->prevVpos = tPtr->vpos;
3871 return scroll;
3874 Bool
3875 WMPageText(WMText *tPtr, Bool direction)
3877 if (!tPtr) return False;
3878 if (!tPtr->view->flags.realized) return False;
3880 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3883 void
3884 WMSetTextEditable(WMText *tPtr, Bool editable)
3886 if (!tPtr)
3887 return;
3888 tPtr->flags.editable = editable;
3891 int
3892 WMGetTextEditable(WMText *tPtr)
3894 if (!tPtr)
3895 return 0;
3896 return tPtr->flags.editable;
3899 void
3900 WMSetTextIndentNewLines(WMText *tPtr, Bool indent)
3902 if (!tPtr)
3903 return;
3904 tPtr->flags.indentNewLine = indent;
3907 void
3908 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3910 if (!tPtr)
3911 return;
3912 tPtr->flags.ignoreNewLine = ignore;
3915 Bool
3916 WMGetTextIgnoresNewline(WMText *tPtr)
3918 if (!tPtr)
3919 return True;
3920 return tPtr->flags.ignoreNewLine;
3923 void
3924 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3926 if (!tPtr)
3927 return;
3929 if (mono) {
3930 if(tPtr->flags.rulerShown)
3931 WMShowTextRuler(tPtr, False);
3932 if(tPtr->flags.alignment != WALeft)
3933 tPtr->flags.alignment = WALeft;
3936 tPtr->flags.monoFont = mono;
3937 textDidResize(tPtr->view->delegate, tPtr->view);
3940 Bool
3941 WMGetTextUsesMonoFont(WMText *tPtr)
3943 if (!tPtr)
3944 return True;
3945 return tPtr->flags.monoFont;
3949 void
3950 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3952 if (!tPtr)
3953 return;
3955 WMReleaseFont(tPtr->dFont);
3956 if (font)
3957 tPtr->dFont = WMRetainFont(font);
3958 else
3959 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3962 WMFont *
3963 WMGetTextDefaultFont(WMText *tPtr)
3965 if (!tPtr)
3966 return NULL;
3967 else
3968 return WMRetainFont(tPtr->dFont);
3971 void
3972 WMSetTextDefaultColor(WMText *tPtr, WMColor *color)
3974 if (!tPtr)
3975 return;
3977 WMReleaseColor(tPtr->dColor);
3978 if (color)
3979 tPtr->dColor = WMRetainColor(color);
3980 else
3981 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3984 WMColor *
3985 WMGetTextDefaultColor(WMText *tPtr)
3987 if (!tPtr)
3988 return NULL;
3989 else
3990 return WMRetainColor(tPtr->dColor);
3993 void
3994 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3996 if (!tPtr)
3997 return;
3998 if(tPtr->flags.monoFont)
3999 tPtr->flags.alignment = WALeft;
4000 else
4001 tPtr->flags.alignment = alignment;
4002 WMThawText(tPtr);
4005 int
4006 WMGetTextInsertType(WMText *tPtr)
4008 if (!tPtr)
4009 return 0;
4010 return tPtr->flags.prepend;
4014 void
4015 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
4017 if (!tPtr || !color)
4018 return;
4020 setSelectionProperty(tPtr, NULL, color, -1);
4023 WMColor *
4024 WMGetTextSelectionColor(WMText *tPtr)
4026 TextBlock *tb;
4028 if (!tPtr)
4029 return NULL;
4031 tb = tPtr->currentTextBlock;
4033 if (!tb || !tPtr->flags.ownsSelection)
4034 return NULL;
4036 if(!tb->selected)
4037 return NULL;
4039 return tb->color;
4043 void
4044 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
4046 if (!tPtr || !font)
4047 return;
4049 setSelectionProperty(tPtr, font, NULL, -1) ;
4052 WMFont *
4053 WMGetTextSelectionFont(WMText *tPtr)
4055 TextBlock *tb;
4057 if (!tPtr)
4058 return NULL;
4060 tb = tPtr->currentTextBlock;
4062 if (!tb || !tPtr->flags.ownsSelection)
4063 return NULL;
4065 if(!tb->selected)
4066 return NULL;
4068 if(tb->graphic) {
4069 tb = getFirstNonGraphicBlockFor(tb, 1);
4070 if(!tb)
4071 return NULL;
4073 return (tb->selected ? tb->d.font : NULL);
4077 void
4078 WMSetTextSelectionUnderlined(WMText *tPtr, int underlined)
4080 if (!tPtr || (underlined!=0 && underlined !=1))
4081 return;
4083 setSelectionProperty(tPtr, NULL, NULL, underlined);
4087 int
4088 WMGetTextSelectionUnderlined(WMText *tPtr)
4090 TextBlock *tb;
4092 if (!tPtr)
4093 return 0;
4095 tb = tPtr->currentTextBlock;
4097 if (!tb || !tPtr->flags.ownsSelection)
4098 return 0;
4100 if(!tb->selected)
4101 return 0;
4103 return tb->underlined;
4107 void
4108 WMFreezeText(WMText *tPtr)
4110 if (!tPtr)
4111 return;
4113 tPtr->flags.frozen = True;
4117 void
4118 WMThawText(WMText *tPtr)
4120 if (!tPtr)
4121 return;
4123 tPtr->flags.frozen = False;
4125 if(tPtr->flags.monoFont) {
4126 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
4127 TextBlock *tb;
4129 /* make sure to unmap widgets no matter where they are */
4130 /* they'll be later remapped if needed by paintText */
4131 for(j=0; j<c; j++) {
4132 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
4133 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
4134 WMUnmapWidget(tb->d.widget);
4140 tPtr->flags.laidOut = False;
4141 layOutDocument(tPtr);
4142 updateScrollers(tPtr);
4143 paintText(tPtr);
4144 tPtr->flags.needsLayOut = False;
4148 /* find first occurence of a string */
4149 static char *
4150 mystrstr(char *haystack, char *needle, unsigned short len, char *end,
4151 Bool caseSensitive)
4153 char *ptr;
4155 if(!haystack || !needle || !end)
4156 return NULL;
4158 for (ptr = haystack; ptr < end; ptr++) {
4159 if(caseSensitive) {
4160 if (*ptr == *needle && !strncmp(ptr, needle, len))
4161 return ptr;
4163 } else {
4164 if (tolower(*ptr) == tolower(*needle) &&
4165 !strncasecmp(ptr, needle, len))
4166 return ptr;
4170 return NULL;
4173 /* find last occurence of a string */
4174 static char *
4175 mystrrstr(char *haystack, char *needle, unsigned short len, char *end,
4176 Bool caseSensitive)
4178 char *ptr;
4180 if(!haystack || !needle || !end)
4181 return NULL;
4183 for (ptr = haystack-2; ptr > end; ptr--) {
4184 if(caseSensitive) {
4185 if (*ptr == *needle && !strncmp(ptr, needle, len))
4186 return ptr;
4187 } else {
4188 if (tolower(*ptr) == tolower(*needle) &&
4189 !strncasecmp(ptr, needle, len))
4190 return ptr;
4194 return NULL;
4198 Bool
4199 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
4200 Bool caseSensitive)
4202 TextBlock *tb;
4203 char *mark=NULL;
4204 unsigned short pos;
4206 if (!tPtr || !needle)
4207 return False;
4209 #if 0
4210 if (! (tb = tPtr->currentTextBlock)) {
4211 if (! (tb = ( (direction > 0) ?
4212 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
4213 return False;
4215 } else {
4216 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4217 tb = (direction>0) ? tb->next : tb->prior; */
4218 if(tb != tPtr->lastTextBlock)
4219 tb = tb->prior;
4221 #endif
4222 tb = tPtr->currentTextBlock;
4223 pos = tPtr->tpos;
4226 while(tb) {
4227 if (!tb->graphic) {
4229 if(direction > 0) {
4230 if(pos+1 < tb->used)
4231 pos++;
4233 if(tb->used - pos> 0 && pos > 0) {
4234 mark = mystrstr(&tb->text[pos], needle,
4235 strlen(needle), &tb->text[tb->used], caseSensitive);
4237 } else {
4238 tb = tb->next;
4239 pos = 0;
4240 continue;
4243 } else {
4244 if(pos-1 > 0)
4245 pos--;
4247 if(pos > 0) {
4248 mark = mystrrstr(&tb->text[pos], needle,
4249 strlen(needle), tb->text, caseSensitive);
4250 } else {
4251 tb = tb->prior;
4252 if(!tb)
4253 return False;
4254 pos = tb->used;
4255 continue;
4260 if(mark) {
4261 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
4263 tPtr->tpos = (int)(mark - tb->text);
4264 tPtr->currentTextBlock = tb;
4265 updateCursorPosition(tPtr);
4266 tPtr->sel.y = tPtr->cursor.y+5;
4267 tPtr->sel.h = tPtr->cursor.h-10;
4268 tPtr->sel.x = tPtr->cursor.x +1;
4269 tPtr->sel.w = WMIN(WMWidthOfString(font,
4270 &tb->text[tPtr->tpos], strlen(needle)),
4271 tPtr->docWidth - tPtr->sel.x);
4272 tPtr->flags.ownsSelection = True;
4273 paintText(tPtr);
4275 return True;
4279 tb = (direction>0) ? tb->next : tb->prior;
4280 if(tb) {
4281 pos = (direction>0) ? 0 : tb->used;
4285 return False;
4289 Bool
4290 WMReplaceTextSelection(WMText *tPtr, char *replacement)
4292 if (!tPtr)
4293 return False;
4295 if (!tPtr->flags.ownsSelection)
4296 return False;
4298 removeSelection(tPtr);
4300 if(replacement) {
4301 insertTextInteractively(tPtr, replacement, strlen(replacement));
4302 updateCursorPosition(tPtr);
4303 paintText(tPtr);
4306 return True;