*** empty log message ***
[wmaker-crm.git] / WINGs / wtext.c
blob4016453c247d0dfb29b564586e87e32483933b13
1 /*
2 * WINGs WMText: multi-line/font/color/graphic text widget
4 * Copyright (c) 1999-2000 Nwanua Elumeze <nwanua@windowmaker.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "WINGsP.h"
23 #include <X11/keysym.h>
24 #include <X11/Xatom.h>
26 #define DO_BLINK 0
29 /* TODO:
31 * - FIX wrap... long lines that don't fit are not char wrapped yet.
32 * - check if support for Horizontal Scroll is complete
33 * - FIX html parser: 1. <b>foo</i> should STILL BE BOLD!
34 * - 2. " foo > bar " should not confuse it.
35 * - assess danger of destroying widgets whose actions link to other pages
36 * - change cursor shape around pixmaps
37 * - redo blink code to reduce paint event... use pixmap buffer...
38 * - add paragraph support (full) and '\n' code in getStream..
39 * - use currentTextBlock and neighbours for fast paint and layout
40 * - replace copious uses of Refreshtext with appropriate layOut()...
44 /* a Section is a section of a TextBlock that describes what parts
45 of a TextBlock has been laid out on which "line"...
46 o this greatly aids redraw, scroll and selection.
47 o this is created during layoutLine, but may be later modified.
48 o there may be many Sections per TextBlock, hence the array */
49 typedef struct {
50 unsigned int x, y; /* where to draw it from */
51 unsigned short w, h; /* its width and height */
52 unsigned short begin; /* where the layout begins */
53 unsigned short end ; /* where it ends */
54 unsigned short last:1; /* is it the last section on a "line"? */
55 unsigned int _y:31; /* the "line" it and other textblocks are on */
56 } Section;
59 /* a TextBlock is a doubly-linked list of TextBlocks containing:
60 o text for the block, color and font
61 o or a pointer to the pixmap
62 o OR a pointer to the widget and the (text) description for its graphic
65 typedef struct _TextBlock {
66 struct _TextBlock *next; /* next text block in linked list */
67 struct _TextBlock *prior; /* prior text block in linked list */
69 char *text; /* pointer to text (could be kanji) */
70 /* or to the object's description */
71 union {
72 WMFont *font; /* the font */
73 WMWidget *widget; /* the embedded widget */
74 WMPixmap *pixmap; /* the pixmap */
75 } d; /* description */
77 unsigned short used; /* number of chars in this block */
78 unsigned short allocated; /* size of allocation (in chars) */
79 WMColor *color; /* the color */
81 Section *sections; /* the region for layouts (a growable array) */
82 /* an _array_! of size _nsections_ */
84 unsigned short s_begin; /* where the selection begins */
85 unsigned short s_end; /* where it ends */
87 unsigned int first:1; /* first TextBlock in paragraph */
88 unsigned int blank:1; /* ie. blank paragraph */
89 unsigned int kanji:1; /* is of 16-bit characters or not */
90 unsigned int graphic:1; /* graphic or text: text=0 */
91 unsigned int object:1; /* embedded object or pixmap */
92 unsigned int underlined:1; /* underlined or not */
93 unsigned int selected:1; /* selected or not */
94 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
95 int script:8; /* script in points: negative for subscript */
96 unsigned int marginN:8; /* which of the margins in the tPtr to use */
97 unsigned int nClicks:2; /* single, double, triple clicks */
98 unsigned int RESERVED:7;
99 } TextBlock;
102 /* somehow visible.h beats the hell outta visible.size.height :-) */
103 typedef struct {
104 unsigned int y;
105 unsigned int x;
106 unsigned int h;
107 unsigned int w;
108 } myRect;
111 typedef struct W_Text {
112 W_Class widgetClass; /* the class number of this widget */
113 W_View *view; /* the view referring to this instance */
115 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
117 WMScroller *vS; /* the vertical scroller */
118 unsigned int vpos; /* the current vertical position */
119 unsigned int prevVpos; /* the previous vertical position */
121 WMScroller *hS; /* the horizontal scroller */
122 unsigned int hpos; /* the current horizontal position */
123 unsigned int prevHpos; /* the previous horizontal position */
125 WMFont *dFont; /* the default font */
126 WMColor *dColor; /* the default color */
127 WMPixmap *dBulletPix; /* the default pixmap for bullets */
129 GC bgGC; /* the background GC to draw with */
130 GC fgGC; /* the foreground GC to draw with */
131 Pixmap db; /* the buffer on which to draw */
133 myRect visible; /* the actual rectangle that can be drawn into */
134 myRect cursor; /* the position and (height) of cursor */
135 myRect sel; /* the selection rectangle */
137 WMPoint clicked; /* where in the _document_ was clicked */
139 unsigned short tpos; /* the position in the currentTextBlock */
140 unsigned short docWidth; /* the width of the entire document */
141 unsigned int docHeight; /* the height of the entire document */
143 TextBlock *firstTextBlock;
144 TextBlock *lastTextBlock;
145 TextBlock *currentTextBlock;
147 WMArray *gfxItems; /* a nice array of graphic items */
149 #if DO_BLINK
150 WMHandlerID timerID; /* for nice twinky-winky */
151 #endif
153 WMAction *parser;
154 WMAction *writer;
155 WMTextDelegate *delegate;
156 Time lastClickTime;
158 WMRulerMargins *margins; /* an array of margins */
160 unsigned int nMargins:8; /* the total number of margins in use */
161 struct {
162 unsigned int monoFont:1; /* whether to ignore formats */
163 unsigned int focused:1; /* whether this instance has input focus */
164 unsigned int editable:1; /* "silly user, you can't edit me" */
165 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
166 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
167 unsigned int buttonHeld:1; /* the user is holding down the button */
168 unsigned int extendSelection:1; /* shift-drag to select more regions */
170 unsigned int rulerShown:1; /* whether the ruler is shown or not */
171 unsigned int frozen:1; /* whether screen updates are to be made */
172 unsigned int cursorShown:1; /* whether to show the cursor */
173 unsigned int clickPos:1; /* clicked before=0 or after=1 a graphic: */
174 /* (within counts as after too) */
176 unsigned int horizOnDemand:1;/* if a large image should appear*/
177 unsigned int needsRefresh:1; /* in case of Append/Deletes */
178 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
179 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
180 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
181 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
182 unsigned int parsingHTML:1; /* how to interpret a stream of text */
183 WMAlignment alignment:2; /* the alignment for text */
184 WMReliefType relief:3; /* the relief to display with */
185 unsigned int RESERVED:2;
186 } flags;
187 } Text;
190 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
191 if ((T)->delegate && (T)->delegate->C)\
192 (*(T)->delegate->C)((T)->delegate,notif);\
193 WMPostNotification(notif);\
194 WMReleaseNotification(notif);}
199 * A hack to speed up caseless_equal. Thanks to Quincey Koziol for
200 * developing it for the "chimera" folks so I could use it 7 years later ;-)
201 * Constraint: nothing but '\0' may map to 0
203 static unsigned char map_table[256] = {
204 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
205 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
206 52,53,54,55,56,57,58,59,60,61,62,63,64,97,98,99,100,101,102,103,104,105,
207 106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,91,
208 92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
209 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,
210 130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,
211 148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,
212 166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
213 184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,
214 202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,
215 220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,
216 238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255};
218 void HTMLParser(Text *tPtr, char *stream);
219 #define MAX_TOKEN_SIZE 255
220 #define MAX_TEXT_SIZE 1023
222 #define TOLOWER(x) (map_table[(int)x])
224 #define ISALNUM(x) ( ((x>='0') && (x<='9')) \
225 || ((x>='a') && (x<='z')) || ((x>='A') && x<='Z'))
227 static void
228 output(char *ptr, int len)
230 char s[len+1];
231 memcpy(s, ptr, len);
232 s[len] = 0;
233 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
234 printf("[%s]\n", s);
238 /* tb->text doesn't necessarily end in '\0' hmph! (strchr) */
239 static inline char *
240 mystrchr(char *s, char needle, unsigned short len)
242 char *haystack = s;
244 if (!haystack || len < 1)
245 return NULL;
247 while ( (int) (haystack - s) < len ) {
248 if (*haystack == needle)
249 return haystack;
250 haystack++;
252 return NULL;
256 /* tb->text doesn't necessarily end in '\0' hmph! (strchr) */
257 static inline char *
258 mystrrstr(char *haystack, char *needle, unsigned short len, char *end)
260 char *ptr;
262 if(!haystack || !needle)
263 return NULL;
265 for (ptr = haystack; ptr > end; ptr--) {
266 if (*ptr == *needle && !strncmp(ptr, needle, len))
267 return ptr;
269 return NULL;
272 static int
273 mystrcasecmp(const unsigned char *s1, const unsigned char *s2,
274 int len, Bool caseSensitive)
276 if (!*s1 || !*s2 || len<1)
277 return 0;
279 while (*s2 != '\0' && len>0) {
280 if ( (caseSensitive? (*s1) : TOLOWER (*s1))
281 != (caseSensitive? (*s2) : TOLOWER (*s2)))
282 return 0;
284 s1++;
285 s2++;
286 len--;
288 return (*s1=='\0' || !ISALNUM(*s1))?1:0;
293 #if DO_BLINK
294 #define CURSOR_BLINK_ON_DELAY 600
295 #define CURSOR_BLINK_OFF_DELAY 400
296 #endif
298 static char *default_bullet[] = {
299 "6 6 4 1",
300 " c None s None", ". c black",
301 "X c white", "o c #808080",
302 " ... ",
303 ".XX.. ",
304 ".XX..o",
305 ".....o",
306 " ...oo",
307 " ooo "};
309 static void
310 handleEvents(XEvent *event, void *data);
313 static int
314 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
316 unsigned int i=0;
318 for(i=0; i < tPtr->nMargins; i++) {
320 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
321 return i;
324 return -1;
329 static int
330 newMargin(Text *tPtr, WMRulerMargins *margins)
332 int n;
334 if (!margins) {
335 tPtr->margins[0].retainCount++;
336 return 0;
339 n = getMarginNumber(tPtr, margins);
341 if (n == -1) {
343 tPtr->margins = wrealloc(tPtr->margins,
344 (++tPtr->nMargins)*sizeof(WMRulerMargins));
346 n = tPtr->nMargins-1;
347 tPtr->margins[n].left = margins->left;
348 tPtr->margins[n].first = margins->first;
349 tPtr->margins[n].body = margins->body;
350 tPtr->margins[n].right = margins->right;
351 /* for each tab... */
352 tPtr->margins[n].retainCount = 1;
353 } else {
354 tPtr->margins[n].retainCount++;
357 return n;
360 static Bool
361 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
363 unsigned short i, w, lw, selected = False, extend = False;
364 myRect sel;
367 /* if selection rectangle completely encloses the section */
368 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
369 && (tb->sections[s]._y + tb->sections[s].h
370 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
371 sel.x = 0;
372 sel.w = tPtr->visible.w;
373 selected = extend = True;
375 /* or if it starts on a line and then goes further down */
376 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
377 && (tb->sections[s]._y + tb->sections[s].h
378 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
379 && (tb->sections[s]._y + tb->sections[s].h
380 >= tPtr->visible.y + tPtr->sel.y) ) {
381 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
382 sel.w = tPtr->visible.w;
383 selected = extend = True;
385 /* or if it begins before a line, but ends on it */
386 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
387 && (tb->sections[s]._y + tb->sections[s].h
388 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
389 && (tb->sections[s]._y
390 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
392 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
393 sel.w = tPtr->sel.x + tPtr->sel.w;
394 else
395 sel.w = tPtr->sel.x;
397 sel.x = 0;
398 selected = True;
400 /* or if the selection rectangle lies entirely within a line */
401 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
402 && (tPtr->sel.w >= 2)
403 && (tb->sections[s]._y + tb->sections[s].h
404 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
405 sel.x = tPtr->sel.x;
406 sel.w = tPtr->sel.w;
407 selected = True;
410 if (selected) {
411 selected = False;
413 /* if not within (modified) selection rectangle */
414 if ( tb->sections[s].x > sel.x + sel.w
415 || tb->sections[s].x + tb->sections[s].w < sel.x)
416 return False;
418 if (tb->graphic) {
419 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
420 && tb->sections[s].x >= sel.x) {
421 rect->width = tb->sections[s].w;
422 rect->x = tb->sections[s].x;
423 selected = True;
425 } else {
427 i = tb->sections[s].begin;
428 lw = 0;
430 if (0&& tb->sections[s].x >= sel.x) {
431 tb->s_begin = tb->sections[s].begin;
432 goto _selEnd;
435 while (++i <= tb->sections[s].end) {
437 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
438 lw += w;
440 if (lw + tb->sections[s].x >= sel.x
441 || i == tb->sections[s].end ) {
442 lw -= w;
443 i--;
444 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
445 break;
449 if (i > tb->sections[s].end) {
450 printf("WasSelected: (i > tb->sections[s].end) \n");
451 return False;
454 _selEnd: rect->x = tb->sections[s].x + lw;
455 lw = 0;
456 while(++i <= tb->sections[s].end) {
458 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
459 lw += w;
461 if (lw + rect->x >= sel.x + sel.w
462 || i == tb->sections[s].end ) {
464 if (i != tb->sections[s].end) {
465 lw -= w;
466 i--;
469 rect->width = lw;
470 if (tb->sections[s].last && sel.x + sel.w
471 >= tb->sections[s].x + tb->sections[s].w
472 && extend ) {
473 rect->width += (tPtr->visible.w - rect->x - lw);
476 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
477 selected = True;
478 break;
479 } } } }
481 if (selected) {
482 rect->y = tb->sections[s]._y - tPtr->vpos;
483 rect->height = tb->sections[s].h;
484 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
486 return selected;
490 static void
491 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color)
493 TextBlock *tb;
494 int isFont=False;
496 if((font && color) || (!font && !color))
497 return;
499 if(font && !color)
500 isFont = True;
502 tb = tPtr->firstTextBlock;
503 if (!tb || !tPtr->flags.ownsSelection)
504 return;
506 while (tb) {
507 if (tPtr->flags.monoFont || tb->selected) {
509 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
510 || tb->graphic) {
512 if(isFont) {
513 if(!tb->graphic) {
514 WMReleaseFont(tb->d.font);
515 tb->d.font = WMRetainFont(font);
517 } else {
518 WMReleaseColor(tb->color);
519 tb->color = WMRetainColor(color);
522 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
524 TextBlock *otb = tb;
526 TextBlock *ntb = (TextBlock *)
527 WMCreateTextBlockWithText(tPtr,
528 &(tb->text[tb->s_begin]),
529 (isFont?font:tb->d.font),
530 (isFont?tb->color:color),
531 False, (tb->s_end - tb->s_begin));
533 if (ntb) {
534 ntb->selected = True;
535 ntb->s_begin = 0;
536 ntb->s_end = ntb->used;
537 tPtr->currentTextBlock = tb;
538 WMAppendTextBlock(tPtr, ntb);
539 tb = tPtr->currentTextBlock;
542 if (otb->used - otb->s_end > 0) {
543 ntb = (TextBlock *)
544 WMCreateTextBlockWithText(tPtr,
545 &(otb->text[otb->s_end]), otb->d.font, otb->color,
546 False, otb->used - otb->s_end);
548 if (ntb) {
549 ntb->selected = False;
550 WMAppendTextBlock(tPtr, ntb);
551 tb = tPtr->currentTextBlock;
555 otb->selected = False;
556 otb->used = otb->s_begin;
560 tb = tb->next;
563 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
567 static void
568 removeSelection(Text *tPtr)
570 TextBlock *tb = NULL;
572 if (!(tb = tPtr->firstTextBlock))
573 return;
575 while (tb) {
576 if (tb->selected) {
578 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
579 tPtr->currentTextBlock = tb;
580 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
581 tb = tPtr->currentTextBlock;
582 if (tb)
583 tPtr->tpos = 0;
584 continue;
586 } else if (tb->s_end <= tb->used) {
587 memmove(&(tb->text[tb->s_begin]),
588 &(tb->text[tb->s_end]), tb->used - tb->s_end);
589 tb->used -= (tb->s_end - tb->s_begin);
590 tb->selected = False;
591 tPtr->tpos = tb->s_begin;
596 tb = tb->next;
601 static void
602 paintText(Text *tPtr)
604 TextBlock *tb;
605 WMFont *font;
606 GC gc, greyGC;
607 char *text;
608 int len, y, c, s, done=False, prev_y=-23;
609 WMScreen *scr = tPtr->view->screen;
610 Display *dpy = tPtr->view->screen->display;
611 Window win = tPtr->view->window;
613 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
614 return;
616 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
617 0, 0, tPtr->visible.w, tPtr->visible.h);
619 if(!(tb = tPtr->firstTextBlock))
620 goto _copy_area;
622 if (tPtr->flags.ownsSelection)
623 greyGC = WMColorGC(WMGrayColor(scr));
625 done = False;
627 /* first, place all text that can be viewed */
628 while (!done && tb) {
630 if (tb->graphic) {
631 tb = tb->next;
632 continue;
635 tb->selected = False;
637 for(s=0; s<tb->nsections && !done; s++) {
639 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
640 done = True;
641 break;
644 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
645 continue;
647 if (tPtr->flags.monoFont) {
648 font = tPtr->dFont;
649 gc = tPtr->fgGC;
650 } else {
651 font = tb->d.font;
652 gc = WMColorGC(tb->color);
655 if (tPtr->flags.ownsSelection) {
656 XRectangle rect;
658 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
659 tb->selected = True;
660 XFillRectangle(dpy, tPtr->db, greyGC,
661 rect.x, rect.y, rect.width, rect.height);
665 prev_y = tb->sections[s]._y;
667 len = tb->sections[s].end - tb->sections[s].begin;
668 text = &(tb->text[tb->sections[s].begin]);
669 y = tb->sections[s].y - tPtr->vpos;
670 WMDrawString(scr, tPtr->db, gc, font,
671 tb->sections[s].x - tPtr->hpos, y, text, len);
673 if (tb->underlined) {
674 XDrawLine(dpy, tPtr->db, gc,
675 tb->sections[s].x - tPtr->hpos,
676 y + font->y + 1,
677 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
678 y + font->y + 1);
683 tb = (!done? tb->next : NULL);
687 /* now , show all graphic items that can be viewed */
688 c = WMGetArrayItemCount(tPtr->gfxItems);
689 if (c > 0 && !tPtr->flags.monoFont) {
690 int j, h;
692 for(j=0; j<c; j++) {
693 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
694 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
695 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
697 if(tb->object) {
698 if ((W_VIEW(tb->d.widget))->flags.mapped) {
699 WMUnmapWidget(tb->d.widget);
702 } else {
703 if(tb->object) {
704 if (!(W_VIEW(tb->d.widget))->flags.mapped) {
705 if (!(W_VIEW(tb->d.widget))->flags.realized)
706 WMRealizeWidget(tb->d.widget);
707 WMMapWidget(tb->d.widget);
708 WMLowerWidget(tb->d.widget);
712 if (tPtr->flags.ownsSelection) {
713 XRectangle rect;
715 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
716 tb->selected = True;
717 XFillRectangle(dpy, tPtr->db, greyGC,
718 rect.x, rect.y, rect.width, rect.height);
722 if(tb->object) {
723 WMMoveWidget(tb->d.widget,
724 tb->sections[0].x - tPtr->hpos,
725 tb->sections[0].y - tPtr->vpos);
726 h = WMWidgetHeight(tb->d.widget) + 1;
728 } else {
729 WMDrawPixmap(tb->d.pixmap, tPtr->db,
730 tb->sections[0].x - tPtr->hpos,
731 tb->sections[0].y - tPtr->vpos);
732 h = tb->d.pixmap->height + 1;
736 if (tb->underlined) {
737 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
738 tb->sections[0].x,
739 tb->sections[0].y + h,
740 tb->sections[0].x + tb->sections[0].w,
741 tb->sections[0].y + h);
742 } } } }
745 _copy_area:
746 if (tPtr->flags.editable && tPtr->flags.cursorShown
747 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
748 int y = tPtr->cursor.y - tPtr->vpos;
749 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
750 tPtr->cursor.x, y,
751 tPtr->cursor.x, y + tPtr->cursor.h);
754 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC, 0, 0,
755 tPtr->visible.w, tPtr->visible.h,
756 tPtr->visible.x, tPtr->visible.y);
758 W_DrawRelief(scr, win, 0, 0,
759 tPtr->view->size.width, tPtr->view->size.height,
760 tPtr->flags.relief);
762 if (tPtr->ruler && tPtr->flags.rulerShown)
763 XDrawLine(dpy, win, tPtr->fgGC,
764 2, 42, tPtr->view->size.width-4, 42);
769 #if DO_BLINK
771 static void
772 blinkCursor(void *data)
774 Text *tPtr = (Text*)data;
776 if (tPtr->flags.cursorShown) {
777 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
778 blinkCursor, data);
779 } else {
780 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
781 blinkCursor, data);
783 paintText(tPtr);
784 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
786 #endif
788 static TextBlock *
789 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
791 if (!tb)
792 return NULL;
793 while (tb) {
794 if (!tb->graphic)
795 break;
796 tb = (dir? tb->next : tb->prior);
799 return tb;
803 static void
804 updateCursorPosition(Text *tPtr)
806 TextBlock *tb = NULL;
807 int x, y, h, s;
809 if(tPtr->flags.needsRefresh)
810 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
812 if (! (tb = tPtr->currentTextBlock)) {
813 if (! (tb = tPtr->firstTextBlock)) {
814 tPtr->tpos = 0;
815 tPtr->cursor.h = tPtr->dFont->height;
816 tPtr->cursor.y = 2;
817 tPtr->cursor.x = 2;
818 return;
822 if(tb->graphic) {
823 y = tb->sections[0].y;
824 h = tb->sections[0].h;
825 x = tb->sections[0].x;
826 } else {
827 if(tPtr->tpos > tb->used)
828 tPtr->tpos = tb->used;
830 for(s=0; s<tb->nsections; s++) {
832 if(tPtr->tpos >= tb->sections[s].begin
833 && tPtr->tpos <= tb->sections[s].end)
834 break;
837 y = tb->sections[s]._y;
838 h = tb->sections[s].h;
839 x = tb->sections[s].x + WMWidthOfString(
840 (tPtr->flags.monoFont?tPtr->dFont:tb->d.font),
841 &tb->text[tb->sections[s].begin],
842 tPtr->tpos - tb->sections[s].begin);
845 tPtr->cursor.y = y;
846 tPtr->cursor.h = h;
847 tPtr->cursor.x = x;
851 static void
852 cursorToTextPosition(Text *tPtr, int x, int y)
854 TextBlock *tb = NULL;
855 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
856 char *text;
858 if(tPtr->flags.needsRefresh)
859 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
861 y += (tPtr->vpos - tPtr->visible.y);
862 if (y<0)
863 y = 0;
865 x -= (tPtr->visible.x - 2);
866 if (x<0)
867 x=0;
869 /* clicked is relative to document, not window... */
870 tPtr->clicked.x = x;
871 tPtr->clicked.y = y;
873 if (! (tb = tPtr->currentTextBlock)) {
874 if (! (tb = tPtr->firstTextBlock)) {
875 tPtr->tpos = 0;
876 tPtr->cursor.h = tPtr->dFont->height;
877 tPtr->cursor.y = 2;
878 tPtr->cursor.x = 2;
879 return;
883 /* first, which direction? Most likely, newly clicked
884 position will be close to previous */
885 dir = !(y <= tb->sections[0].y);
886 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
887 && (y >= tb->sections[0]._y ) ) {
888 /* if it's on the same line */
889 if(x < tb->sections[0].x)
890 dir = 0;
891 if(x >= tb->sections[0].x)
892 dir = 1;
895 tb = tPtr->firstTextBlock;
896 dir = 1;
898 if (tPtr->flags.monoFont && tb->graphic) {
899 tb = getFirstNonGraphicBlockFor(tb, 1);
900 if (!tb) {
901 tPtr->currentTextBlock =
902 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
903 tPtr->tpos = 0;
904 return;
908 s = (dir? 0 : tb->nsections-1);
909 if ( y >= tb->sections[s]._y
910 && y <= tb->sections[s]._y + tb->sections[s].h) {
911 goto _doneV;
914 /* get the first section of the TextBlock that lies about
915 the vertical click point */
916 done = False;
917 while (!done && tb) {
919 if (tPtr->flags.monoFont && tb->graphic) {
920 if(tb->next)
921 tb = tb->next;
922 continue;
925 s = (dir? 0 : tb->nsections-1);
926 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
928 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
929 ( y >= tb->sections[s]._y ) ) ) {
930 done = True;
931 } else {
932 dir? s++ : s--;
936 if (!done) {
937 if ( (dir? tb->next : tb->prior)) {
938 tb = (dir ? tb->next : tb->prior);
939 } else {
940 pos = tb->used;
941 break; /* goto _doneH; */
947 if (s<0 || s>=tb->nsections) {
948 s = (dir? tb->nsections-1 : 0);
951 _doneV:
952 /* we have the line, which TextBlock on that line is it? */
953 pos = 0;
954 if (tPtr->flags.monoFont && tb->graphic)
955 tb = getFirstNonGraphicBlockFor(tb, dir);
956 if (tb) {
957 if ((dir? tb->sections[s].x >= x : tb->sections[s].x < x))
958 goto _doneH;
960 #if 0
961 if(tb->blank) {
962 _w = 0;
963 printf("blank\n");
964 } else {
965 text = &(tb->text[tb->sections[s].begin]);
966 len = tb->sections[s].end - tb->sections[s].begin;
967 _w = WMWidthOfString(tb->d.font, text, len);
969 printf("here %d %d \n", tb->sections[s].x + _w, x);
970 if ((dir? tb->sections[s].x + _w < x : tb->sections[s].x + _w >= x)) {
971 pos = tb->sections[s].end;
972 tPtr->cursor.x = tb->sections[s].x + _w;
973 goto _doneH;
975 #endif
976 _y = tb->sections[s]._y;
979 while (tb) {
981 if (tPtr->flags.monoFont && tb->graphic) {
982 tb = (dir ? tb->next : tb->prior);
983 continue;
986 if (dir) {
987 if (tb->graphic) {
988 if(tb->object)
989 _w = WMWidgetWidth(tb->d.widget);
990 else
991 _w = tb->d.pixmap->width;
992 } else {
993 text = &(tb->text[tb->sections[s].begin]);
994 len = tb->sections[s].end - tb->sections[s].begin;
995 _w = WMWidthOfString(tb->d.font, text, len);
996 if (tb->sections[s].x + _w >= x)
997 break;
1000 } else {
1001 if (tb->sections[s].x <= x)
1002 break;
1005 if ((dir? tb->next : tb->prior)) {
1006 TextBlock *nxt = (dir? tb->next : tb->prior);
1007 if (tPtr->flags.monoFont && nxt->graphic) {
1008 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1009 if (!nxt) {
1010 pos = 0;
1011 tPtr->cursor.x = tb->sections[s].x;
1012 goto _doneH;
1016 if (_y != nxt->sections[0]._y) {
1017 /* this must be the last/first on this line. stop */
1018 pos = (dir? tb->sections[s].end : 0);
1019 tPtr->cursor.x = tb->sections[s].x;
1020 if (!tb->blank) {
1021 if (tb->graphic) {
1022 if(tb->object)
1023 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1024 else
1025 tPtr->cursor.x += tb->d.pixmap->width;
1026 } else if (pos > tb->sections[s].begin) {
1027 tPtr->cursor.x +=
1028 WMWidthOfString(tb->d.font,
1029 &(tb->text[tb->sections[s].begin]),
1030 pos - tb->sections[s].begin);
1033 goto _doneH;
1037 if ( (dir? tb->next : tb->prior)) {
1038 tb = (dir ? tb->next : tb->prior);
1039 } else {
1040 done = True;
1041 break;
1044 if (tb)
1045 s = (dir? 0 : tb->nsections-1);
1048 /* we have said TextBlock, now where within it? */
1049 if (tb && !tb->graphic) {
1050 WMFont *f = tb->d.font;
1051 len = tb->sections[s].end - tb->sections[s].begin;
1052 text = &(tb->text[tb->sections[s].begin]);
1054 _w = x - tb->sections[s].x;
1055 pos = 0;
1057 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
1058 pos++;
1060 tPtr->cursor.x = tb->sections[s].x +
1061 (pos? WMWidthOfString(f, text, pos) : 0);
1063 pos += tb->sections[s].begin;
1064 _doneH:
1065 tPtr->tpos = (pos<tb->used)? pos : tb->used;
1068 if (!tb)
1069 printf("this app will surely crash :-)\n");
1071 tPtr->currentTextBlock = tb;
1072 tPtr->cursor.h = tb->sections[s].h;
1073 tPtr->cursor.y = tb->sections[s]._y;
1077 static void
1078 autoSelectText(Text *tPtr, int clicks)
1080 int x, y;
1081 TextBlock *tb;
1082 int start, end;
1083 char *mark = NULL;
1085 if(!(tb = tPtr->currentTextBlock))
1086 return;
1088 if(clicks == 2) {
1090 if(tb->text[tPtr->tpos] == ' ')
1091 return;
1093 tPtr->sel.y = tPtr->cursor.y+5;
1094 tPtr->sel.h = tPtr->cursor.h-10;
1096 if(tb->graphic) {
1097 tPtr->sel.x = tb->sections[0].x;
1098 tPtr->sel.w = tb->sections[0].w;
1099 } else {
1100 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1102 start = tPtr->tpos;
1103 while(start > 0 && tb->text[start-1] != ' ')
1104 start--;
1106 x = tPtr->cursor.x + 0*WMWidthOfString(font, &tb->text[start], 1);
1107 if(tPtr->tpos > start){
1108 output(&tb->text[start], tPtr->tpos - start);
1109 x -= WMWidthOfString(font, &tb->text[start],
1110 tPtr->tpos - start);
1112 tPtr->sel.x = (x<0?0:x)+1;
1114 if((mark = mystrchr(&tb->text[start], ' ', tb->used-start))) {
1115 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
1116 (int)(mark - &tb->text[start]));
1117 } else {
1118 tPtr->sel.w = tPtr->docWidth - tPtr->sel.x;
1122 } else if(clicks == 3) {
1123 tPtr->sel.x = tPtr->visible.x;
1124 tPtr->sel.w = tPtr->docWidth;
1127 tPtr->flags.ownsSelection = True;
1128 paintText(tPtr);
1132 static void
1133 updateScrollers(Text *tPtr)
1136 if (tPtr->flags.frozen)
1137 return;
1139 if (tPtr->vS) {
1140 if (tPtr->docHeight < tPtr->visible.h) {
1141 WMSetScrollerParameters(tPtr->vS, 0, 1);
1142 tPtr->vpos = 0;
1143 } else {
1144 float hmax = (float)(tPtr->docHeight);
1145 WMSetScrollerParameters(tPtr->vS,
1146 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1147 (float)tPtr->visible.h/hmax);
1149 } else tPtr->vpos = 0;
1151 if (tPtr->hS) {
1152 if (tPtr->docWidth < tPtr->visible.w) {
1153 WMSetScrollerParameters(tPtr->hS, 0, 1);
1154 tPtr->hpos = 0;
1155 } else {
1156 float wmax = (float)(tPtr->docWidth);
1157 WMSetScrollerParameters(tPtr->hS,
1158 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1159 (float)tPtr->visible.w/wmax);
1161 } else tPtr->hpos = 0;
1164 static void
1165 scrollersCallBack(WMWidget *w, void *self)
1167 Text *tPtr = (Text *)self;
1168 Bool scroll = False;
1169 Bool dimple = False;
1170 int which;
1172 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1173 return;
1175 if (w == tPtr->vS) {
1176 int height;
1177 height = tPtr->visible.h;
1179 which = WMGetScrollerHitPart(tPtr->vS);
1180 switch(which) {
1181 case WSDecrementLine:
1182 if (tPtr->vpos > 0) {
1183 if (tPtr->vpos>16) tPtr->vpos-=16;
1184 else tPtr->vpos=0;
1185 scroll=True;
1186 }break;
1187 case WSIncrementLine: {
1188 int limit = tPtr->docHeight - height;
1189 if (tPtr->vpos < limit) {
1190 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1191 else tPtr->vpos=limit;
1192 scroll = True;
1193 }}break;
1194 case WSDecrementPage:
1195 tPtr->vpos -= height;
1197 if (tPtr->vpos < 0)
1198 tPtr->vpos = 0;
1199 dimple = True;
1200 scroll = True;
1201 printf("dimple needs to jump to mouse location ;-/\n");
1202 break;
1203 case WSIncrementPage:
1204 tPtr->vpos += height;
1205 if (tPtr->vpos > (tPtr->docHeight - height))
1206 tPtr->vpos = tPtr->docHeight - height;
1207 dimple = True;
1208 scroll = True;
1209 printf("dimple needs to jump to mouse location ;-/\n");
1210 break;
1213 case WSKnob:
1214 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1215 * (float)(tPtr->docHeight - height);
1216 scroll = True;
1217 break;
1219 case WSKnobSlot:
1220 case WSNoPart:
1221 printf("WSNoPart, WSKnobSlot\n");
1222 #if 0
1223 float hmax = (float)(tPtr->docHeight);
1224 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1225 (float)tPtr->visible.h/hmax;
1226 dimple =where mouse is.
1227 #endif
1228 break;
1230 scroll = (tPtr->vpos != tPtr->prevVpos);
1231 tPtr->prevVpos = tPtr->vpos;
1234 if (w == tPtr->hS) {
1235 int width = tPtr->visible.w;
1237 which = WMGetScrollerHitPart(tPtr->hS);
1238 switch(which) {
1239 case WSDecrementLine:
1240 if (tPtr->hpos > 0) {
1241 if (tPtr->hpos>16) tPtr->hpos-=16;
1242 else tPtr->hpos=0;
1243 scroll=True;
1244 }break;
1245 case WSIncrementLine: {
1246 int limit = tPtr->docWidth - width;
1247 if (tPtr->hpos < limit) {
1248 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1249 else tPtr->hpos=limit;
1250 scroll = True;
1251 }}break;
1252 case WSDecrementPage:
1253 tPtr->hpos -= width;
1255 if (tPtr->hpos < 0)
1256 tPtr->hpos = 0;
1257 dimple = True;
1258 scroll = True;
1259 printf("dimple needs to jump to mouse location ;-/\n");
1260 break;
1261 case WSIncrementPage:
1262 tPtr->hpos += width;
1263 if (tPtr->hpos > (tPtr->docWidth - width))
1264 tPtr->hpos = tPtr->docWidth - width;
1265 dimple = True;
1266 scroll = True;
1267 printf("dimple needs to jump to mouse location ;-/\n");
1268 break;
1271 case WSKnob:
1272 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1273 * (float)(tPtr->docWidth - width);
1274 scroll = True;
1275 break;
1277 case WSKnobSlot:
1278 case WSNoPart:
1279 printf("WSNoPart, WSKnobSlot\n");
1280 #if 0
1281 float wmax = (float)(tPtr->docWidth);
1282 ((float)tPtr->vpos)/(wmax - (float)tPtr->visible.w),
1283 (float)tPtr->visible.w/wmax;
1284 dimple =where mouse is.
1285 #endif
1286 break;
1288 scroll = (tPtr->hpos != tPtr->prevHpos);
1289 tPtr->prevHpos = tPtr->hpos;
1292 if (scroll) {
1294 if (0&&dimple) {
1295 if (tPtr->rulerShown)
1296 XClearArea(tPtr->view->screen->display, tPtr->view->window, 22, 47,
1297 tPtr->view->size.width-24, tPtr->view->size.height-49, True);
1298 else
1299 XClearArea(tPtr->view->screen->display, tPtr->view->window, 22, 2,
1300 tPtr->view->size.width-24, tPtr->view->size.height-4, True);
1303 if (dimple || which == WSDecrementLine || which == WSIncrementLine)
1304 updateScrollers(tPtr);
1305 paintText(tPtr);
1311 typedef struct {
1312 TextBlock *tb;
1313 unsigned short begin, end; /* what part of the text block */
1314 } myLineItems;
1317 static int
1318 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1320 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1321 WMFont *font;
1322 char *text;
1323 TextBlock *tb;
1324 TextBlock *tbsame=NULL;
1326 if(!items || nitems == 0)
1327 return 0;
1329 for(i=0; i<nitems; i++) {
1330 tb = items[i].tb;
1332 if (tb->graphic) {
1333 if (!tPtr->flags.monoFont) {
1334 if(tb->object) {
1335 WMWidget *wdt = tb->d.widget;
1336 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1337 if (tPtr->flags.alignment != WALeft)
1338 lw += WMWidgetWidth(wdt);
1339 } else {
1340 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1341 if (tPtr->flags.alignment != WALeft)
1342 lw += tb->d.pixmap->width;
1346 } else {
1347 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1348 max_d = WMAX(max_d, abs(font->height-font->y));
1349 line_height = WMAX(line_height, font->height + max_d);
1350 text = &(tb->text[items[i].begin]);
1351 len = items[i].end - items[i].begin;
1352 if (tPtr->flags.alignment != WALeft)
1353 lw += WMWidthOfString(font, text, len);
1357 if (tPtr->flags.alignment == WARight) {
1358 j = tPtr->visible.w - lw;
1359 } else if (tPtr->flags.alignment == WACenter) {
1360 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1363 for(i=0; i<nitems; i++) {
1364 tb = items[i].tb;
1366 if (tbsame == tb) { /*extend it, since it's on same line */
1367 tb->sections[tb->nsections-1].end = items[i].end;
1368 n = tb->nsections-1;
1369 } else {
1370 tb->sections = wrealloc(tb->sections,
1371 (++tb->nsections)*sizeof(Section));
1372 n = tb->nsections-1;
1373 tb->sections[n]._y = y + max_d;
1374 tb->sections[n].x = x+j;
1375 tb->sections[n].h = line_height;
1376 tb->sections[n].begin = items[i].begin;
1377 tb->sections[n].end = items[i].end;
1379 if (tb->graphic && tb->object) {
1380 tb->sections[n].x += tPtr->visible.x;
1381 tb->sections[n].y += tPtr->visible.y;
1385 tb->sections[n].last = (i+1 == nitems);
1387 if (tb->graphic) {
1388 if (!tPtr->flags.monoFont) {
1389 if(tb->object) {
1390 WMWidget *wdt = tb->d.widget;
1391 tb->sections[n].y = max_d + y
1392 + line_height - WMWidgetHeight(wdt);
1393 tb->sections[n].w = WMWidgetWidth(wdt);
1394 } else {
1395 tb->sections[n].y = y + max_d;
1396 tb->sections[n].w = tb->d.pixmap->width;
1398 x += tb->sections[n].w;
1400 } else {
1401 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1402 len = items[i].end - items[i].begin;
1403 text = &(tb->text[items[i].begin]);
1405 tb->sections[n].y = y+line_height-font->y;
1406 tb->sections[n].w =
1407 WMWidthOfString(font,
1408 &(tb->text[tb->sections[n].begin]),
1409 tb->sections[n].end - tb->sections[n].begin);
1411 x += WMWidthOfString(font, text, len);
1414 tbsame = tb;
1417 return line_height;
1422 static void
1423 layOutDocument(Text *tPtr)
1425 TextBlock *tb;
1426 myLineItems *items = NULL;
1427 unsigned int itemsSize=0, nitems=0;
1428 WMFont *font;
1429 unsigned int x, y=0, prev_y, lw = 0, width=0, bmargin;
1432 Bool lhc = !tPtr->flags.laidOut; /* line height changed? */
1434 char *start=NULL, *mark=NULL;
1435 unsigned int begin, end;
1437 if (tPtr->flags.frozen)
1438 return;
1440 if (!(tb = tPtr->firstTextBlock))
1441 return;
1443 tPtr->docWidth = tPtr->visible.w;
1444 x = 0; /* tPtr->margins[tb->marginN].first; */
1445 printf("x:%d\n", x);
1446 bmargin = tPtr->margins[tb->marginN].body;
1448 if (0&&tPtr->flags.laidOut) {
1449 tb = tPtr->currentTextBlock;
1450 if (tb->sections && tb->nsections>0)
1451 prev_y = tb->sections[tb->nsections-1]._y;
1452 y+=10;
1453 printf("1 prev_y %d \n", prev_y);
1455 /* search backwards for textblocks on same line */
1456 while (tb) {
1457 if (!tb->sections || tb->nsections<1) {
1458 tb = tPtr->firstTextBlock;
1459 break;
1461 if (tb->sections[tb->nsections-1]._y != prev_y) {
1462 tb = tb->next;
1463 break;
1465 /* prev_y = tb->sections[tb->nsections-1]._y; */
1466 tb = tb->prior;
1468 y = 0; /* tb->sections[tb->nsections-1]._y; */
1469 printf("2 prev_y %d \n\n", tb->sections[tb->nsections-1]._y);
1473 while (tb) {
1475 if (tb->sections && tb->nsections>0) {
1476 wfree(tb->sections);
1477 tb->sections = NULL;
1478 tb->nsections = 0;
1481 if (tb->first && tb != tPtr->firstTextBlock) {
1482 y += layOutLine(tPtr, items, nitems, x, y);
1483 x = 0*tPtr->margins[tb->marginN].first;
1484 bmargin = tPtr->margins[tb->marginN].body;
1485 nitems = 0;
1486 lw = 0;
1489 if (tb->graphic) {
1490 if (!tPtr->flags.monoFont) {
1491 if(tb->object)
1492 width = WMWidgetWidth(tb->d.widget);
1493 else
1494 width = tb->d.pixmap->width;
1496 if (width > tPtr->docWidth)
1497 tPtr->docWidth = width;
1499 lw += width;
1500 if (lw >= tPtr->visible.w - x ) {
1501 y += layOutLine(tPtr, items, nitems, x, y);
1502 nitems = 0;
1503 x = 0*bmargin;
1504 lw = width;
1507 if(nitems + 1> itemsSize) {
1508 items = wrealloc(items,
1509 (++itemsSize)*sizeof(myLineItems));
1512 items[nitems].tb = tb;
1513 items[nitems].begin = 0;
1514 items[nitems].end = 0;
1515 nitems++;
1518 } else if ((start = tb->text)) {
1519 begin = end = 0;
1520 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1522 while (start) {
1523 mark = mystrchr(start, ' ', tb->used);
1524 if (mark) {
1525 end += (int)(mark-start)+1;
1526 start = mark+1;
1527 } else {
1528 end += strlen(start);
1529 start = mark;
1532 if (end > tb->used)
1533 end = tb->used;
1535 if (end-begin > 0) {
1537 width = WMWidthOfString(font,
1538 &tb->text[begin], end-begin);
1540 /* if it won't fit, break it up */
1541 if (width > tPtr->visible.w) {
1542 char *t = &tb->text[begin];
1543 int l=end-begin, i=0;
1544 do {
1545 width = WMWidthOfString(font, t, ++i);
1546 } while (width < tPtr->visible.w && i < l);
1547 end = begin+i;
1548 if (start)
1549 start -= l-i;
1552 lw += width;
1555 if (lw >= tPtr->visible.w - x) {
1556 y += layOutLine(tPtr, items, nitems, x, y);
1557 lw = width;
1558 x = bmargin;
1559 nitems = 0;
1562 if(nitems + 1 > itemsSize) {
1563 items = wrealloc(items,
1564 (++itemsSize)*sizeof(myLineItems));
1567 items[nitems].tb = tb;
1568 items[nitems].begin = begin;
1569 items[nitems].end = end;
1570 nitems++;
1572 begin = end;
1575 tb = tb->next;
1579 if (nitems > 0)
1580 y += layOutLine(tPtr, items, nitems, x, y);
1581 if (lhc) {
1582 tPtr->docHeight = y+10;
1583 updateScrollers(tPtr);
1586 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1587 XEvent event;
1589 tPtr->flags.horizOnDemand = True;
1590 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1591 event.type = Expose;
1592 handleEvents(&event, (void *)tPtr);
1594 } else if(tPtr->docWidth <= tPtr->visible.w
1595 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1596 tPtr->flags.horizOnDemand = False;
1597 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1599 tPtr->flags.laidOut = True;
1602 if(items && itemsSize > 0)
1603 wfree(items);
1607 static void
1608 textDidResize(W_ViewDelegate *self, WMView *view)
1610 Text *tPtr = (Text *)view->self;
1611 unsigned short w = tPtr->view->size.width;
1612 unsigned short h = tPtr->view->size.height;
1613 unsigned short rh = 0, vw = 0;
1615 if (tPtr->ruler && tPtr->flags.rulerShown) {
1616 WMMoveWidget(tPtr->ruler, 2, 2);
1617 WMResizeWidget(tPtr->ruler, w - 4, 40);
1618 rh = 40;
1621 if (tPtr->vS) {
1622 WMMoveWidget(tPtr->vS, 1, rh + 1);
1623 WMResizeWidget(tPtr->vS, 20, h - rh - 2);
1624 vw = 20;
1625 WMSetRulerOffset(tPtr->ruler,22);
1626 } else WMSetRulerOffset(tPtr->ruler, 2);
1628 if (tPtr->hS) {
1629 if (tPtr->vS) {
1630 WMMoveWidget(tPtr->hS, vw, h - 21);
1631 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1632 } else {
1633 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1634 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1638 tPtr->visible.x = (tPtr->vS)?24:4;
1639 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1640 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1641 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1642 tPtr->visible.h -= (tPtr->hS)?20:0;
1643 tPtr->margins[0].right = tPtr->visible.w;
1645 if (tPtr->view->flags.realized) {
1647 if (tPtr->db) {
1648 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1649 tPtr->db = (Pixmap) NULL;
1652 if (tPtr->visible.w < 40)
1653 tPtr->visible.w = 40;
1654 if (tPtr->visible.h < 20)
1655 tPtr->visible.h = 20;
1657 if(!tPtr->db) {
1658 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1659 tPtr->view->window, tPtr->visible.w,
1660 tPtr->visible.h, tPtr->view->screen->depth);
1664 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1667 W_ViewDelegate _TextViewDelegate =
1669 NULL,
1670 NULL,
1671 textDidResize,
1672 NULL,
1675 /* nice, divisble-by-16 blocks */
1676 static inline unsigned short
1677 reqBlockSize(unsigned short requested)
1679 return requested + 16 - (requested%16);
1683 static void
1684 clearText(Text *tPtr)
1686 if (!tPtr->firstTextBlock)
1687 return;
1689 while (tPtr->currentTextBlock)
1690 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1692 tPtr->firstTextBlock = NULL;
1693 tPtr->currentTextBlock = NULL;
1694 tPtr->lastTextBlock = NULL;
1697 static void
1698 deleteTextInteractively(Text *tPtr, KeySym ksym)
1700 TextBlock *tb = tPtr->currentTextBlock;
1701 Bool back = (Bool) (ksym == XK_BackSpace);
1702 Bool done = 1;
1704 if (!tPtr->flags.editable || tPtr->flags.buttonHeld) {
1705 XBell(tPtr->view->screen->display, 0);
1706 return;
1709 if (!tb)
1710 return;
1712 tPtr->flags.needsRefresh = True;
1714 if (tPtr->flags.ownsSelection) {
1715 removeSelection(tPtr);
1716 return;
1719 if (back && tPtr->tpos < 1) {
1720 if (tb->prior) {
1721 tb = tb->prior;
1722 tb->first = False;
1723 tPtr->tpos = tb->used;
1724 tPtr->currentTextBlock = tb;
1725 done = 1;
1729 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1730 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1731 if (back)
1732 tPtr->tpos--;
1733 memmove(&(tb->text[tPtr->tpos]),
1734 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1735 tb->used--;
1736 done = 0;
1739 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1740 || tb->graphic) {
1742 TextBlock *sibling = (back? tb->prior : tb->next);
1744 if(tb->used == 0 || tb->graphic)
1745 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1747 if (sibling) {
1748 tPtr->currentTextBlock = sibling;
1749 tPtr->tpos = (back? sibling->used : 0);
1753 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1757 static void
1758 insertTextInteractively(Text *tPtr, char *text, int len)
1760 TextBlock *tb;
1761 char *newline = NULL;
1763 if (!tPtr->flags.editable || tPtr->flags.buttonHeld) {
1764 XBell(tPtr->view->screen->display, 0);
1765 return;
1768 if (len < 1 || !text)
1769 return;
1772 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1773 return;
1775 if (tPtr->flags.ownsSelection)
1776 removeSelection(tPtr);
1778 tPtr->flags.needsRefresh = True;
1780 if (tPtr->flags.ignoreNewLine) {
1781 int i;
1782 for(i=0; i<len; i++) {
1783 if (text[i] == '\n')
1784 text[i] = ' ';
1788 tb = tPtr->currentTextBlock;
1789 if (!tb || tb->graphic) {
1790 text[len] = 0;
1791 WMAppendTextStream(tPtr, text);
1792 if (tPtr->currentTextBlock) {
1793 tPtr->tpos = tPtr->currentTextBlock->used;
1795 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1796 return;
1799 if ((newline = mystrchr(text, '\n', len))) {
1800 int nlen = (int)(newline-text);
1801 int s = tb->used - tPtr->tpos;
1802 char save[s];
1804 if (!tb->blank && nlen>0) {
1805 if (s > 0) {
1806 memcpy(save, &tb->text[tPtr->tpos], s);
1807 tb->used -= (tb->used - tPtr->tpos);
1809 text[nlen] = 0;
1810 insertTextInteractively(tPtr, text, nlen);
1811 newline++;
1812 WMAppendTextStream(tPtr, newline);
1813 if (s>0)
1814 insertTextInteractively(tPtr, save, s);
1816 } else {
1817 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1818 && !tb->graphic && tb->text) {
1820 void *ntb = WMCreateTextBlockWithText(
1821 tPtr, &tb->text[tPtr->tpos],
1822 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1823 tb->used = tPtr->tpos;
1824 WMAppendTextBlock(tPtr, ntb);
1825 tPtr->tpos = 0;
1826 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
1827 void *ntb = WMCreateTextBlockWithText(tPtr,
1828 NULL, tb->d.font, tb->color, True, 0);
1830 if (tPtr->tpos>0)
1831 WMAppendTextBlock(tPtr, ntb);
1832 else
1833 WMPrependTextBlock(tPtr, ntb);
1834 tPtr->tpos = 1;
1838 } else {
1840 if (tb->used + len >= tb->allocated) {
1841 tb->allocated = reqBlockSize(tb->used+len);
1842 tb->text = wrealloc(tb->text, tb->allocated);
1845 if (tb->blank) {
1846 memcpy(tb->text, text, len);
1847 tb->used = len;
1848 tPtr->tpos = len;
1849 tb->blank = False;
1850 } else {
1851 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
1852 tb->used-tPtr->tpos+1);
1853 memmove(&tb->text[tPtr->tpos], text, len);
1854 tb->used += len;
1855 tPtr->tpos += len;
1860 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1864 static void
1865 selectRegion(Text *tPtr, int x, int y)
1868 if (x < 0 || y < 0)
1869 return;
1871 y += (tPtr->flags.rulerShown? 40: 0);
1872 y += tPtr->vpos;
1873 if (y>10)
1874 y -= 10; /* the original offset */
1876 x -= tPtr->visible.x-2;
1877 if (x<0)
1878 x=0;
1880 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
1881 tPtr->sel.w = abs(tPtr->clicked.x - x);
1882 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
1883 tPtr->sel.h = abs(tPtr->clicked.y - y);
1885 tPtr->flags.ownsSelection = True;
1886 paintText(tPtr);
1890 static void
1891 releaseSelection(Text *tPtr)
1893 TextBlock *tb = tPtr->firstTextBlock;
1895 while(tb) {
1896 tb->selected = False;
1897 tb = tb->next;
1899 tPtr->flags.ownsSelection = False;
1900 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
1901 CurrentTime);
1903 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1907 WMData*
1908 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
1909 Atom *type)
1911 Text *tPtr = view->self;
1912 Display *dpy = tPtr->view->screen->display;
1913 Atom _TARGETS;
1914 Atom TEXT = XInternAtom(dpy, "TEXT", False);
1915 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
1916 WMData *data = NULL;
1919 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
1920 char *text = WMGetTextSelected(tPtr);
1922 if (text) {
1923 printf("got text [%s]\n", text);
1924 data = WMCreateDataWithBytes(text, strlen(text));
1925 WMSetDataFormat(data, 8);
1927 *type = target;
1928 return data;
1929 } else printf("didn't get it\n");
1931 _TARGETS = XInternAtom(dpy, "TARGETS", False);
1932 if (target == _TARGETS) {
1933 Atom *ptr;
1935 ptr = wmalloc(4 * sizeof(Atom));
1936 ptr[0] = _TARGETS;
1937 ptr[1] = XA_STRING;
1938 ptr[2] = TEXT;
1939 ptr[3] = COMPOUND_TEXT;
1941 data = WMCreateDataWithBytes(ptr, 4*4);
1942 WMSetDataFormat(data, 32);
1944 *type = target;
1945 return data;
1948 return NULL;
1951 static void
1952 lostHandler(WMView *view, Atom selection, void *cdata)
1954 releaseSelection((WMText *)view->self);
1957 static WMSelectionProcs selectionHandler = {
1958 requestHandler, lostHandler, NULL
1962 static void
1963 ownershipObserver(void *observerData, WMNotification *notification)
1965 if (observerData != WMGetNotificationClientData(notification))
1966 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
1970 static void
1971 fontChanged(void *observerData, WMNotification *notification)
1973 WMText *tPtr = (WMText *) observerData;
1974 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
1975 printf("fontChanged\n");
1977 if(!tPtr || !font)
1978 return;
1980 if (tPtr->flags.ownsSelection)
1981 WMSetTextSelectionFont(tPtr, font);
1985 static void
1986 handleTextKeyPress(Text *tPtr, XEvent *event)
1988 char buffer[2];
1989 KeySym ksym;
1990 int control_pressed = False;
1991 TextBlock *tb = NULL;
1993 if (((XKeyEvent *) event)->state & ControlMask)
1994 control_pressed = True;
1995 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
1997 switch(ksym) {
1999 case XK_Left:
2000 printf("foind %d \n", WMFindInTextStream(tPtr, "ion", 0, 0));
2001 return;
2002 if(!(tb = tPtr->currentTextBlock))
2003 break;
2004 if(tb->graphic)
2005 goto L_imaGFX;
2007 if(tPtr->tpos==0) {
2008 L_imaGFX: if(tb->prior) {
2009 tPtr->currentTextBlock = tb->prior;
2010 tPtr->tpos = tPtr->currentTextBlock->used -1;
2011 } else tPtr->tpos = 0;
2012 } else tPtr->tpos--;
2013 updateCursorPosition(tPtr);
2014 paintText(tPtr);
2015 break;
2017 case XK_Right:
2018 printf("foind %d \n", WMFindInTextStream(tPtr, "ion", 1, 0));
2019 return;
2020 if(!(tb = tPtr->currentTextBlock))
2021 break;
2022 if(tb->graphic)
2023 goto R_imaGFX;
2024 if(tPtr->tpos == tb->used) {
2025 R_imaGFX: if(tb->next) {
2026 tPtr->currentTextBlock = tb->next;
2027 tPtr->tpos = 1;
2028 } else tPtr->tpos = tb->used;
2029 } else tPtr->tpos++;
2030 updateCursorPosition(tPtr);
2031 paintText(tPtr);
2032 break;
2034 case XK_Down:
2035 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2036 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2037 paintText(tPtr);
2038 break;
2040 case XK_Up:
2041 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2042 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2043 paintText(tPtr);
2044 break;
2046 case XK_BackSpace:
2047 case XK_Delete:
2048 case XK_KP_Delete:
2049 deleteTextInteractively(tPtr, ksym);
2050 updateCursorPosition(tPtr);
2051 paintText(tPtr);
2052 break;
2054 case XK_Control_R :
2055 case XK_Control_L :
2056 control_pressed = True;
2057 break;
2059 case XK_Return:
2060 buffer[0] = '\n';
2061 default:
2062 if (buffer[0] != 0 && !control_pressed) {
2063 insertTextInteractively(tPtr, buffer, 1);
2064 updateCursorPosition(tPtr);
2065 paintText(tPtr);
2067 } else if (control_pressed && ksym==XK_r) {
2068 Bool i = !tPtr->flags.rulerShown;
2069 WMShowTextRuler(tPtr, i);
2070 tPtr->flags.rulerShown = i;
2072 else if (control_pressed && buffer[0] == '\a')
2073 XBell(tPtr->view->screen->display, 0);
2076 if (!control_pressed && tPtr->flags.ownsSelection)
2077 releaseSelection(tPtr);
2080 static void
2081 handleWidgetPress(XEvent *event, void *data)
2083 TextBlock *tb = (TextBlock *)data;
2084 Text *tPtr;
2085 WMWidget *w;
2087 if (!tb)
2088 return;
2089 /* this little bit of nastiness here saves a boatload of trouble */
2090 w = (WMWidget *)(((W_VIEW(tb->d.widget))->parent)->self);
2091 if (W_CLASS(w) != WC_Text)
2092 return;
2093 tPtr = (Text*)w;
2094 tPtr->currentTextBlock = getFirstNonGraphicBlockFor(tb, 1);
2095 if (!tPtr->currentTextBlock)
2096 tPtr->currentTextBlock = tb;
2097 tPtr->tpos = 0;
2098 output(tPtr->currentTextBlock->text, tPtr->currentTextBlock->used);
2099 #if 0
2100 if (!tPtr->flags.focused) {
2101 WMSetFocusToWidget(tPtr);
2102 tPtr->flags.focused = True;
2104 #endif
2108 static void
2109 handleActionEvents(XEvent *event, void *data)
2111 Text *tPtr = (Text *)data;
2112 Display *dpy = event->xany.display;
2113 KeySym ksym;
2116 switch (event->type) {
2117 case KeyPress:
2118 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2119 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2120 tPtr->flags.extendSelection = True;
2121 return;
2124 if (tPtr->flags.focused) {
2125 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2126 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2127 GrabModeAsync, GrabModeAsync, None,
2128 tPtr->view->screen->invisibleCursor, CurrentTime);
2129 tPtr->flags.pointerGrabbed = True;
2130 handleTextKeyPress(tPtr, event);
2132 } break;
2134 case KeyRelease:
2135 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2136 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2137 tPtr->flags.extendSelection = False;
2138 return;
2139 /* end modify flag so selection can be extended */
2141 break;
2144 case MotionNotify:
2146 if (tPtr->flags.pointerGrabbed) {
2147 tPtr->flags.pointerGrabbed = False;
2148 XUngrabPointer(dpy, CurrentTime);
2151 if(tPtr->flags.waitingForSelection)
2152 break;
2154 if ((event->xmotion.state & Button1Mask)) {
2155 if (!tPtr->flags.ownsSelection) {
2156 WMCreateSelectionHandler(tPtr->view,
2157 XA_PRIMARY, event->xbutton.time,
2158 &selectionHandler, NULL);
2159 tPtr->flags.ownsSelection = True;
2161 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2163 break;
2166 case ButtonPress:
2168 tPtr->flags.buttonHeld = True;
2170 if (tPtr->flags.pointerGrabbed) {
2171 tPtr->flags.pointerGrabbed = False;
2172 XUngrabPointer(dpy, CurrentTime);
2173 break;
2176 if (tPtr->flags.waitingForSelection)
2177 break;
2179 if (tPtr->flags.extendSelection) {
2180 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2181 return;
2185 if (event->xbutton.button == Button1) {
2187 if(WMIsDoubleClick(event)) {
2188 printf("lastClickTime: %d \n", event->xbutton.time -tPtr->lastClickTime);
2189 autoSelectText(tPtr, 2);
2190 tPtr->lastClickTime = event->xbutton.time;
2191 break;
2193 if(0&&event->xbutton.time - tPtr->lastClickTime
2194 < 1.5*WINGsConfiguration.doubleClickDelay) {
2195 ;// autoSelectText(tPtr, 3);
2196 break;
2199 //WMGetTextStreamIntoArray(tPtr);
2201 if (!tPtr->flags.focused) {
2202 WMSetFocusToWidget(tPtr);
2203 tPtr->flags.focused = True;
2206 if (tPtr->flags.ownsSelection)
2207 releaseSelection(tPtr);
2209 tPtr->lastClickTime = event->xbutton.time;
2210 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2211 paintText(tPtr);
2214 if (event->xbutton.button
2215 == WINGsConfiguration.mouseWheelDown) {
2216 WMScrollText(tPtr, -16);
2217 break;
2220 if (event->xbutton.button
2221 == WINGsConfiguration.mouseWheelUp) {
2222 WMScrollText(tPtr, 16);
2223 break;
2226 if (event->xbutton.button == Button2) {
2227 char *text = NULL;
2228 int n;
2230 if (!tPtr->flags.editable) {
2231 XBell(dpy, 0);
2232 break;
2235 #if 0
2236 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2237 event->xbutton.time, pasteText, NULL)) {
2238 #endif
2241 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2243 if (text) {
2244 text[n] = 0;
2246 if (tPtr->parser)
2247 (tPtr->parser) (tPtr, (void *) text);
2248 else
2249 insertTextInteractively(tPtr, text, n);
2251 XFree(text);
2252 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2253 (void*)WMInsertTextEvent);
2255 } else {
2256 tPtr->flags.waitingForSelection = True;
2259 break;
2263 case ButtonRelease:
2265 tPtr->flags.buttonHeld = False;
2267 if (tPtr->flags.pointerGrabbed) {
2268 tPtr->flags.pointerGrabbed = False;
2269 XUngrabPointer(dpy, CurrentTime);
2270 break;
2273 if (tPtr->flags.waitingForSelection)
2274 break;
2280 static void
2281 handleEvents(XEvent *event, void *data)
2283 Text *tPtr = (Text *)data;
2285 switch(event->type) {
2286 case Expose:
2288 if (event->xexpose.count!=0)
2289 break;
2291 if(tPtr->hS) {
2292 if (!(W_VIEW(tPtr->hS))->flags.realized)
2293 WMRealizeWidget(tPtr->hS);
2294 if (!((W_VIEW(tPtr->hS))->flags.mapped))
2295 WMMapWidget(tPtr->hS);
2298 if(tPtr->vS) {
2299 if (!(W_VIEW(tPtr->vS))->flags.realized)
2300 WMRealizeWidget(tPtr->vS);
2301 if (!((W_VIEW(tPtr->vS))->flags.mapped))
2302 WMMapWidget(tPtr->vS);
2305 if(tPtr->ruler) {
2306 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2307 WMRealizeWidget(tPtr->ruler);
2309 if (!((W_VIEW(tPtr->ruler))->flags.mapped)
2310 && tPtr->flags.rulerShown)
2311 WMMapWidget(tPtr->ruler);
2314 if(!tPtr->db)
2315 textDidResize(tPtr->view->delegate, tPtr->view);
2317 paintText(tPtr);
2318 break;
2320 case FocusIn:
2321 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2322 != tPtr->view)
2323 return;
2324 tPtr->flags.focused = True;
2325 #if DO_BLINK
2326 if (tPtr->flags.editable && !tPtr->timerID) {
2327 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2328 blinkCursor, tPtr);
2330 #endif
2332 break;
2334 case FocusOut:
2335 tPtr->flags.focused = False;
2336 paintText(tPtr);
2337 #if DO_BLINK
2338 if (tPtr->timerID) {
2339 WMDeleteTimerHandler(tPtr->timerID);
2340 tPtr->timerID = NULL;
2342 #endif
2343 break;
2346 case DestroyNotify:
2347 clearText(tPtr);
2348 if(tPtr->hS)
2349 WMDestroyWidget(tPtr->hS);
2350 if(tPtr->vS)
2351 WMDestroyWidget(tPtr->vS);
2352 if(tPtr->ruler)
2353 WMDestroyWidget(tPtr->ruler);
2354 if(tPtr->db)
2355 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2356 if(tPtr->gfxItems)
2357 WMFreeArray(tPtr->gfxItems);
2358 #if DO_BLINK
2359 if (tPtr->timerID)
2360 WMDeleteTimerHandler(tPtr->timerID);
2361 #endif
2362 WMReleaseFont(tPtr->dFont);
2363 WMReleaseColor(tPtr->dColor);
2364 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2365 WMRemoveNotificationObserver(tPtr);
2367 wfree(tPtr);
2369 break;
2375 static void
2376 insertText(WMText *tPtr, char *stream)
2378 char *ptr, *begin, *end;
2379 void *tb = NULL;
2380 char c;
2381 char b, e, again=False;
2382 unsigned long tokenLength, tk;
2383 unsigned long textLength, tx;
2385 if (!stream) {
2386 clearText(tPtr);
2387 return;
2390 WMAppendTextBlock(tPtr,
2391 WMCreateTextBlockWithText(tPtr, stream, tPtr->dFont, tPtr->dColor,
2392 1, strlen(stream)));
2393 return;
2394 ptr = stream;
2395 while (ptr) {
2396 again = False;
2397 b = '<'; e = '>';
2398 again:
2399 begin = strchr(ptr, b);
2400 if (begin) {
2401 end = strchr(begin+1, e);
2402 if(end) {
2403 if((int)(end-begin)-1 > 1)
2404 output(begin+1, (int)(end-begin)-1);
2406 ptr = begin+1;
2407 } else if(!again) {
2408 b = '>'; e = '<';
2409 again = True;
2410 printf("again!\n");
2411 goto again;
2412 } else {
2413 if (ptr && strlen(ptr)) {
2414 printf("boo\n");
2416 ptr = begin;
2420 #if 0
2421 while( (c=*(stream++))) {
2422 //printf("%c", c);
2423 if((c == '\n' && tPtr->flags.parsingHTML) || c =='\t')
2424 //c = ' '; //continue;
2425 continue;
2426 if(c == ' ') {
2427 if(wasspace)
2428 continue;
2429 wasspace = 1;
2430 }else wasspace = 0;
2432 if(c == '\n') {
2433 parseToken(tPtr, token, -1);
2435 } else if(c == '<' && !open) {
2436 open=1;
2437 if(textlen>0) {
2438 text[textlen] = 0;
2439 tb = WMCreateTextBlockWithText(tPtr, text, cfmt.cfont,
2440 cfmt.ccolor, cfmt.first, textlen);
2441 //WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
2442 WMAppendTextBlock(tPtr, tb);
2443 cfmt.first = False;
2444 //printf("%s\n", text);
2446 textlen = 0;
2447 } else if(c == '>' && open) {
2448 token[tk] = 0;
2449 if(tk>0) parseToken(tPtr, token, tk);
2450 open=0;
2451 tk=0;
2452 } else {
2453 if(open) {
2454 if(tk < MAX_TOKEN_SIZE) token[tk++] = c;
2455 } else if(textlen < MAX_TEXT_SIZE) text[textlen++] = c;
2459 if(tk>0) { token[tk] = 0; parseToken(tPtr, token, tk);}
2460 if(textlen>0) {
2461 text[textlen] = 0;
2462 //printf("%s\n", text);
2463 tb = WMCreateTextBlockWithText(tPtr, text,
2464 (WMFont *)WMGetFromArray(cfmt.fonts,
2465 WMGetArrayItemCount(cfmt.fonts)-1),
2466 (WMColor *)WMGetFromArray(cfmt.colors,
2467 WMGetArrayItemCount(cfmt.colors)-1),
2468 cfmt.first, textlen);
2469 //WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
2470 WMAppendTextBlock(tPtr, tb);
2471 cfmt.first = False;
2473 #endif
2478 #if 0
2479 start = text;
2480 while (start) {
2481 mark = strchr(start, '\n');
2482 if (mark) {
2483 tb = WMCreateTextBlockWithText(tPtr,
2484 start, tPtr->dFont,
2485 tPtr->dColor, True, (int)(mark-start));
2486 start = mark+1;
2487 } else {
2488 if (start && strlen(start)) {
2489 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2490 tPtr->dColor, False, strlen(start));
2491 } else tb = NULL;
2492 start = mark;
2495 if (tPtr->flags.prepend)
2496 WMPrependTextBlock(tPtr, tb);
2497 else
2498 WMAppendTextBlock(tPtr, tb);
2500 return;
2501 #endif
2506 static void
2507 rulerMoveCallBack(WMWidget *w, void *self)
2509 Text *tPtr = (Text *)self;
2510 if (!tPtr)
2511 return;
2512 if (W_CLASS(tPtr) != WC_Text)
2513 return;
2515 paintText(tPtr);
2519 static void
2520 rulerReleaseCallBack(WMWidget *w, void *self)
2522 Text *tPtr = (Text *)self;
2523 if (!tPtr)
2524 return;
2525 if (W_CLASS(tPtr) != WC_Text)
2526 return;
2528 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
2529 return;
2533 static unsigned
2534 draggingEntered(WMView *self, WMDraggingInfo *info)
2536 printf("draggingEntered\n");
2537 return WDOperationCopy;
2541 static unsigned
2542 draggingUpdated(WMView *self, WMDraggingInfo *info)
2544 return WDOperationCopy;
2548 static void
2549 draggingExited(WMView *self, WMDraggingInfo *info)
2551 printf("draggingExited\n");
2554 static Bool
2555 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2557 printf("prepareForDragOperation\n");
2558 return True;
2562 char *badbadbad;
2564 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2565 void *cdata, WMData *data)
2567 badbadbad = wstrdup((char *)WMDataBytes(data));
2571 /* when it's done in WINGs, remove this */
2573 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2575 WMScreen *scr = W_VIEW_SCREEN(view);
2577 if (!WMRequestSelection(scr->dragInfo.destView,
2578 scr->xdndSelectionAtom,
2579 XInternAtom(scr->display, type, False),
2580 scr->dragInfo.timestamp,
2581 receivedData, &scr->dragInfo)) {
2582 wwarning("could not request data for dropped data");
2587 XEvent ev;
2589 ev.type = ClientMessage;
2590 ev.xclient.message_type = scr->xdndFinishedAtom;
2591 ev.xclient.format = 32;
2592 ev.xclient.window = info->destinationWindow;
2593 ev.xclient.data.l[0] = 0;
2594 ev.xclient.data.l[1] = 0;
2595 ev.xclient.data.l[2] = 0;
2596 ev.xclient.data.l[3] = 0;
2597 ev.xclient.data.l[4] = 0;
2599 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2600 XFlush(scr->display);
2604 static Bool
2605 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2607 WMColor *color;
2608 WMText *tPtr = (WMText *)self->self;
2610 if (!tPtr)
2611 return True;
2613 requestDroppedData(tPtr->view, info, "application/X-color");
2614 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2615 if(color) {
2616 WMSetTextSelectionColor(tPtr, color);
2617 WMReleaseColor(color);
2622 return True;
2625 static void
2626 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2628 printf("concludeDragOperation\n");
2632 static WMDragDestinationProcs _DragDestinationProcs = {
2633 draggingEntered,
2634 draggingUpdated,
2635 draggingExited,
2636 prepareForDragOperation,
2637 performDragOperation,
2638 concludeDragOperation
2641 static void
2642 releaseArrayData(void *data)
2644 if(data)
2645 wfree(data);
2649 char *
2650 getStream(WMText *tPtr, int sel, int array)
2652 TextBlock *tb = NULL;
2653 char *text = NULL;
2654 unsigned long where = 0;
2656 if (!tPtr)
2657 return NULL;
2659 if (!(tb = tPtr->firstTextBlock))
2660 return NULL;
2662 /* this might be tricky to get right... not yet implemented */
2663 if (tPtr->writer) {
2664 (tPtr->writer) (tPtr, (void *) text);
2665 return text;
2668 tb = tPtr->firstTextBlock;
2669 while (tb) {
2671 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2673 if (!sel || (tb->graphic && tb->selected)) {
2675 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)) {
2676 text = wrealloc(text, where+1);
2677 text[where++] = '\n';
2680 if(tb->graphic && array) {
2681 text = wrealloc(text, where+3);
2682 text[where++] = 'G';//0xFA;
2683 text[where++] = 'A';//tb->used;
2684 text[where++] = 'H';//18+tb->allocated;
2687 text = wrealloc(text, where+tb->used);
2688 memcpy(&text[where], tb->text, tb->used);
2689 where += tb->used;
2692 } else if (sel && tb->selected) {
2694 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)) {
2695 text = wrealloc(text, where+1);
2696 text[where++] = '\n';
2699 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2700 memcpy(&text[where], &tb->text[tb->s_begin],
2701 tb->s_end - tb->s_begin);
2702 where += tb->s_end - tb->s_begin;
2707 tb = tb->next;
2710 /* +1 for the end of string, let's be nice */
2711 text = wrealloc(text, where+1);
2712 text[where] = 0;
2713 return text;
2718 WMArray *
2719 getStreamIntoArray(WMText *tPtr, int sel)
2721 WMArray *array = WMCreateArray(4);
2722 WMData *data;
2723 char *stream;
2724 unsigned long loc=0, len;
2725 unsigned long end;
2726 int i, count;
2729 stream = getStream(tPtr, sel, 0);
2730 if(!stream)
2731 return NULL;
2734 printf(stream);
2735 return;
2736 end = strlen(stream);
2738 #if 0
2739 data = WMCreateDataWithBytes((void *)(fa+2), len);
2740 WMSetDataFormat(data, 32);
2741 WMAddToArray(array, (void *) data);
2742 start = fa + len + 2;
2744 } else {
2745 if (start && strlen(start)) {
2746 data = WMCreateDataWithBytes((void *)start, strlen(start));
2747 WMSetDataFormat(data, 8);
2748 WMAddToArray(array, (void *) data);
2750 start = fa;
2754 wfree(stream);
2755 return array;
2756 #endif
2757 WMFreeArray(array);
2761 WMText *
2762 WMCreateText(WMWidget *parent)
2764 Text *tPtr = wmalloc(sizeof(Text));
2765 if (!tPtr) {
2766 printf("could not create text widget\n");
2767 return NULL;
2771 memset(tPtr, 0, sizeof(Text));
2772 tPtr->widgetClass = WC_Text;
2773 tPtr->view = W_CreateView(W_VIEW(parent));
2774 if (!tPtr->view) {
2775 perror("could not create text's view\n");
2776 free(tPtr);
2777 return NULL;
2779 tPtr->view->self = tPtr;
2780 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2781 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2782 W_ResizeView(tPtr->view, 250, 200);
2784 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2785 tPtr->bgGC = WMColorGC(tPtr->dColor);
2786 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2787 WMReleaseColor(tPtr->dColor);
2789 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2790 tPtr->fgGC = WMColorGC(tPtr->dColor);
2792 tPtr->ruler = NULL;
2793 tPtr->vS = NULL;
2794 tPtr->hS = NULL;
2796 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2798 tPtr->view->delegate = &_TextViewDelegate;
2800 #if DO_BLINK
2801 tPtr->timerID = NULL;
2802 #endif
2804 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2805 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2806 handleEvents, tPtr);
2808 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2809 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2810 handleActionEvents, tPtr);
2812 WMAddNotificationObserver(ownershipObserver, tPtr,
2813 "_lostOwnership", tPtr);
2815 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2817 char *types[2] = {"application/X-color", NULL};
2818 WMRegisterViewForDraggedTypes(tPtr->view, types);
2821 WMAddNotificationObserver(fontChanged, tPtr,
2822 "WMFontPanelDidChangeNotification", tPtr);
2824 tPtr->firstTextBlock = NULL;
2825 tPtr->lastTextBlock = NULL;
2826 tPtr->currentTextBlock = NULL;
2827 tPtr->tpos = 0;
2829 tPtr->gfxItems = WMCreateArray(4);
2831 tPtr->parser = NULL;
2832 tPtr->writer = NULL;
2834 tPtr->sel.x = tPtr->sel.y = 2;
2835 tPtr->sel.w = tPtr->sel.h = 0;
2837 tPtr->clicked.x = tPtr->clicked.y = 2;
2839 tPtr->visible.x = tPtr->visible.y = 2;
2840 tPtr->visible.h = tPtr->view->size.height;
2841 tPtr->visible.w = tPtr->view->size.width - 4;
2843 tPtr->cursor.x = -23;
2845 tPtr->docWidth = 0;
2846 tPtr->docHeight = 0;
2847 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2848 default_bullet);
2849 tPtr->db = (Pixmap) NULL;
2851 tPtr->margins = WMGetRulerMargins(NULL);
2852 tPtr->margins->right = tPtr->visible.w;
2853 tPtr->nMargins = 1;
2855 tPtr->flags.rulerShown = False;
2856 tPtr->flags.monoFont = False;
2857 tPtr->flags.focused = False;
2858 tPtr->flags.editable = True;
2859 tPtr->flags.ownsSelection = False;
2860 tPtr->flags.pointerGrabbed = False;
2861 tPtr->flags.buttonHeld = False;
2862 tPtr->flags.extendSelection = False;
2863 tPtr->flags.frozen = False;
2864 tPtr->flags.cursorShown = True;
2865 tPtr->flags.clickPos = 1;
2866 tPtr->flags.horizOnDemand = False;
2867 tPtr->flags.needsRefresh = False;
2868 tPtr->flags.ignoreNewLine = False;
2869 tPtr->flags.laidOut = False;
2870 tPtr->flags.waitingForSelection = False;
2871 tPtr->flags.prepend = False;
2872 tPtr->flags.parsingHTML = False;
2873 tPtr->flags.relief = WRSunken;
2874 tPtr->flags.alignment = WALeft;
2876 return tPtr;
2879 void
2880 WMPrependTextStream(WMText *tPtr, char *text)
2882 CHECK_CLASS(tPtr, WC_Text);
2884 if(!text)
2885 releaseSelection(tPtr);
2887 tPtr->flags.prepend = True;
2888 if (text && tPtr->parser)
2889 (tPtr->parser) (tPtr, (void *) text);
2890 else
2891 insertText(tPtr, text);
2893 tPtr->flags.needsRefresh = True;
2897 void
2898 WMAppendTextStream(WMText *tPtr, char *text)
2900 CHECK_CLASS(tPtr, WC_Text);
2902 if(!text)
2903 releaseSelection(tPtr);
2905 tPtr->flags.prepend = False;
2906 if (text && tPtr->parser)
2907 (tPtr->parser) (tPtr, (void *) text);
2908 else
2909 insertText(tPtr, text);
2911 tPtr->flags.needsRefresh = True;
2916 char *
2917 WMGetTextStream(WMText *tPtr)
2919 CHECK_CLASS(tPtr, WC_Text);
2920 return getStream(tPtr, 0, 0);
2923 char *
2924 WMGetTextSelected(WMText *tPtr)
2926 CHECK_CLASS(tPtr, WC_Text);
2927 return getStream(tPtr, 1, 0);
2930 WMArray *
2931 WMGetTextStreamIntoArray(WMText *tPtr)
2933 CHECK_CLASS(tPtr, WC_Text);
2934 return getStreamIntoArray(tPtr, 0);
2937 WMArray *
2938 WMGetTextSelectedIntoArray(WMText *tPtr)
2940 CHECK_CLASS(tPtr, WC_Text);
2941 return getStreamIntoArray(tPtr, 1);
2945 void
2946 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
2948 CHECK_CLASS(tPtr, WC_Text);
2950 tPtr->delegate = delegate;
2954 void *
2955 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
2956 char *description, WMColor *color,
2957 unsigned short first, unsigned short extraInfo)
2959 TextBlock *tb;
2960 unsigned short length;
2962 if (!w || !description || !color)
2963 return NULL;
2965 tb = wmalloc(sizeof(TextBlock));
2966 if (!tb)
2967 return NULL;
2969 length = strlen(description);
2970 tb->text = (char *)wmalloc(length);
2971 memset(tb->text, 0, length);
2972 memcpy(tb->text, description, length);
2973 tb->used = length;
2974 tb->blank = False;
2975 tb->d.widget = w;
2976 tb->color = WMRetainColor(color);
2977 tb->marginN = newMargin(tPtr, NULL);
2978 tb->allocated = extraInfo;
2979 tb->first = first;
2980 tb->kanji = False;
2981 tb->graphic = True;
2982 tb->object = True;
2983 tb->underlined = False;
2984 tb->selected = False;
2985 tb->script = 0;
2986 tb->sections = NULL;
2987 tb->nsections = 0;
2988 tb->prior = NULL;
2989 tb->next = NULL;
2991 return tb;
2995 void *
2996 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
2997 char *description, WMColor *color,
2998 unsigned short first, unsigned short extraInfo)
3000 TextBlock *tb;
3001 unsigned short length;
3003 if (!p || !description || !color)
3004 return NULL;
3006 tb = wmalloc(sizeof(TextBlock));
3007 if (!tb)
3008 return NULL;
3010 length = strlen(description);
3011 tb->text = (char *)wmalloc(length);
3012 memset(tb->text, 0, length);
3013 memcpy(tb->text, description, length);
3014 tb->used = length;
3015 tb->blank = False;
3016 tb->d.pixmap = p;
3017 tb->color = WMRetainColor(color);
3018 tb->marginN = newMargin(tPtr, NULL);
3019 tb->allocated = extraInfo;
3020 tb->first = first;
3021 tb->kanji = False;
3022 tb->graphic = True;
3023 tb->object = False;
3024 tb->underlined = False;
3025 tb->selected = False;
3026 tb->script = 0;
3027 tb->sections = NULL;
3028 tb->nsections = 0;
3029 tb->prior = NULL;
3030 tb->next = NULL;
3032 return tb;
3035 void *
3036 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3037 unsigned short first, unsigned short length)
3039 TextBlock *tb;
3041 if (!font || !color)
3042 return NULL;
3044 tb = wmalloc(sizeof(TextBlock));
3045 if (!tb)
3046 return NULL;
3048 tb->allocated = reqBlockSize(length);
3049 tb->text = (char *)wmalloc(tb->allocated);
3050 memset(tb->text, 0, tb->allocated);
3052 if (length < 1|| !text ) {
3053 *tb->text = ' ';
3054 tb->used = 1;
3055 tb->blank = True;
3056 } else {
3057 memcpy(tb->text, text, length);
3058 tb->used = length;
3059 tb->blank = False;
3062 tb->d.font = WMRetainFont(font);
3063 tb->color = WMRetainColor(color);
3064 tb->marginN = newMargin(tPtr, NULL);
3065 tb->first = first;
3066 tb->kanji = False;
3067 tb->graphic = False;
3068 tb->underlined = False;
3069 tb->selected = False;
3070 tb->script = 0;
3071 tb->sections = NULL;
3072 tb->nsections = 0;
3073 tb->prior = NULL;
3074 tb->next = NULL;
3075 return tb;
3078 void
3079 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3080 unsigned int kanji, unsigned int underlined, int script,
3081 WMRulerMargins *margins)
3083 TextBlock *tb = (TextBlock *) vtb;
3084 if (!tb)
3085 return;
3087 tb->first = first;
3088 tb->kanji = kanji;
3089 tb->underlined = underlined;
3090 tb->script = script;
3091 tb->marginN = newMargin(tPtr, margins);
3094 void
3095 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3096 unsigned int *kanji, unsigned int *underlined, int *script,
3097 WMRulerMargins *margins)
3099 TextBlock *tb = (TextBlock *) vtb;
3100 if (!tb)
3101 return;
3103 if (first) *first = tb->first;
3104 if (kanji) *kanji = tb->kanji;
3105 if (underlined) *underlined = tb->underlined;
3106 if (script) *script = tb->script;
3107 if (margins) margins = &tPtr->margins[tb->marginN];
3112 void
3113 WMPrependTextBlock(WMText *tPtr, void *vtb)
3115 TextBlock *tb = (TextBlock *)vtb;
3117 if (!tPtr || !tb)
3118 return;
3120 if (tb->graphic) {
3121 if(tb->object) {
3122 WMWidget *w = tb->d.widget;
3123 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3124 handleWidgetPress, tb);
3125 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3126 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3127 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3130 WMAddToArray(tPtr->gfxItems, (void *)tb);
3131 tPtr->tpos = 0;
3132 } else tPtr->tpos = tb->used;
3134 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3135 tb->next = tb->prior = NULL;
3136 tb->first = True;
3137 tPtr->lastTextBlock = tPtr->firstTextBlock
3138 = tPtr->currentTextBlock = tb;
3139 return;
3142 if(!tb->first) {
3143 tb->marginN = tPtr->currentTextBlock->marginN;
3146 tb->next = tPtr->currentTextBlock;
3147 tb->prior = tPtr->currentTextBlock->prior;
3148 if (tPtr->currentTextBlock->prior)
3149 tPtr->currentTextBlock->prior->next = tb;
3151 tPtr->currentTextBlock->prior = tb;
3152 if (!tb->prior)
3153 tPtr->firstTextBlock = tb;
3155 tPtr->currentTextBlock = tb;
3159 void
3160 WMAppendTextBlock(WMText *tPtr, void *vtb)
3162 TextBlock *tb = (TextBlock *)vtb;
3164 if (!tPtr || !tb)
3165 return;
3167 if (tb->graphic) {
3168 if(tb->object) {
3169 WMWidget *w = tb->d.widget;
3170 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3171 handleWidgetPress, tb);
3172 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3173 (W_VIEW(w))->attribs.cursor =
3174 tPtr->view->screen->defaultCursor;
3175 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3178 WMAddToArray(tPtr->gfxItems, (void *)tb);
3179 tPtr->tpos = 0;
3180 } else tPtr->tpos = tb->used;
3182 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3183 tb->next = tb->prior = NULL;
3184 tb->first = True;
3185 tPtr->lastTextBlock = tPtr->firstTextBlock
3186 = tPtr->currentTextBlock = tb;
3187 return;
3190 if(!tb->first) {
3191 tb->marginN = tPtr->currentTextBlock->marginN;
3194 tb->next = tPtr->currentTextBlock->next;
3195 tb->prior = tPtr->currentTextBlock;
3196 if (tPtr->currentTextBlock->next)
3197 tPtr->currentTextBlock->next->prior = tb;
3199 tPtr->currentTextBlock->next = tb;
3201 if (!tb->next)
3202 tPtr->lastTextBlock = tb;
3204 tPtr->currentTextBlock = tb;
3207 void *
3208 WMRemoveTextBlock(WMText *tPtr)
3210 TextBlock *tb = NULL;
3212 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3213 || !tPtr->currentTextBlock) {
3214 printf("cannot remove non existent TextBlock!\b");
3215 return tb;
3218 tb = tPtr->currentTextBlock;
3219 if (tb->graphic) {
3220 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3222 if(tb->object) {
3223 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3224 handleWidgetPress, tb);
3225 WMUnmapWidget(tb->d.widget);
3229 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3230 if (tPtr->currentTextBlock->next)
3231 tPtr->currentTextBlock->next->prior = NULL;
3233 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3234 tPtr->currentTextBlock = tPtr->firstTextBlock;
3236 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3237 tPtr->currentTextBlock->prior->next = NULL;
3238 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3239 tPtr->currentTextBlock = tPtr->lastTextBlock;
3240 } else {
3241 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3242 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3243 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3246 return (void *)tb;
3249 void
3250 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3252 TextBlock *tb = (TextBlock *)vtb;
3253 if (!tPtr || !tb)
3254 return;
3256 if (tb->graphic) {
3257 if(tb->object) {
3258 /* naturally, there's a danger to destroying
3259 widgets whose action brings us here:
3260 ie. press a button to destroy it... need to
3261 find a safer way. till then... this stays commented out */
3262 /* WMDestroyWidget(tb->d.widget);
3263 wfree(tb->d.widget); */
3264 tb->d.widget = NULL;
3265 } else {
3266 WMReleasePixmap(tb->d.pixmap);
3267 tb->d.pixmap = NULL;
3269 } else {
3270 WMReleaseFont(tb->d.font);
3273 WMReleaseColor(tb->color);
3274 if (tb->sections && tb->nsections > 0)
3275 wfree(tb->sections);
3276 wfree(tb->text);
3277 wfree(tb);
3278 tb = NULL;
3282 void
3283 WMRefreshText(WMText *tPtr, int vpos, int hpos)
3285 if (!tPtr || vpos<0 || hpos<0)
3286 return;
3288 if (tPtr->flags.frozen && !tPtr->flags.needsRefresh)
3289 return;
3291 if(tPtr->flags.monoFont) {
3292 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3293 TextBlock *tb;
3295 /* make sure to unmap widgets no matter where they are */
3296 for(j=0; j<c; j++) {
3297 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3298 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3299 WMUnmapWidget(tb->d.widget);
3305 if (tPtr->vpos != vpos) {
3306 if (vpos < 0 || tPtr->docHeight < tPtr->visible.h) {
3307 tPtr->vpos = 0;
3308 } else if(tPtr->docHeight - vpos > tPtr->visible.h - tPtr->visible.y) {
3309 tPtr->vpos = vpos;
3310 } else {
3311 tPtr->vpos = tPtr->docHeight - tPtr->visible.h;
3315 if (tPtr->hpos != hpos) {
3316 if (hpos < 0 || tPtr->docWidth < tPtr->visible.w) {
3317 tPtr->hpos = 0;
3318 } else if(tPtr->docWidth - hpos > tPtr->visible.w - tPtr->visible.x) {
3319 tPtr->hpos = hpos;
3320 } else {
3321 tPtr->hpos = tPtr->docWidth - tPtr->visible.w;
3326 tPtr->flags.laidOut = False;
3327 layOutDocument(tPtr);
3328 updateScrollers(tPtr);
3329 paintText(tPtr);
3330 tPtr->flags.needsRefresh = False;
3334 void
3335 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3337 if (!tPtr)
3338 return;
3340 if (color)
3341 tPtr->fgGC = WMColorGC(color);
3342 else
3343 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3345 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3348 void
3349 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3351 if (!tPtr)
3352 return;
3354 if (color) {
3355 tPtr->bgGC = WMColorGC(color);
3356 W_SetViewBackgroundColor(tPtr->view, color);
3357 } else {
3358 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3359 W_SetViewBackgroundColor(tPtr->view,
3360 WMWhiteColor(tPtr->view->screen));
3363 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3366 void
3367 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3369 if (!tPtr)
3370 return;
3371 tPtr->flags.relief = relief;
3372 paintText(tPtr);
3375 void
3376 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3378 if (!tPtr)
3379 return;
3381 if (shouldhave && !tPtr->hS) {
3382 tPtr->hS = WMCreateScroller(tPtr);
3383 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3384 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3385 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3386 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3387 WMMapWidget(tPtr->hS);
3388 } else if (!shouldhave && tPtr->hS) {
3389 WMUnmapWidget(tPtr->hS);
3390 WMDestroyWidget(tPtr->hS);
3391 tPtr->hS = NULL;
3394 tPtr->hpos = 0;
3395 tPtr->prevHpos = 0;
3396 textDidResize(tPtr->view->delegate, tPtr->view);
3400 void
3401 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3403 if (!tPtr)
3404 return;
3406 if(shouldhave && !tPtr->ruler) {
3407 tPtr->ruler = WMCreateRuler(tPtr);
3408 (W_VIEW(tPtr->ruler))->attribs.cursor =
3409 tPtr->view->screen->defaultCursor;
3410 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3411 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3412 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3413 } else if(!shouldhave && tPtr->ruler) {
3414 WMShowTextRuler(tPtr, False);
3415 WMDestroyWidget(tPtr->ruler);
3416 tPtr->ruler = NULL;
3418 textDidResize(tPtr->view->delegate, tPtr->view);
3421 void
3422 WMShowTextRuler(WMText *tPtr, Bool show)
3424 if(!tPtr)
3425 return;
3426 if(!tPtr->ruler)
3427 return;
3429 if(tPtr->flags.monoFont)
3430 show = False;
3432 tPtr->flags.rulerShown = show;
3433 if(show) {
3434 WMMapWidget(tPtr->ruler);
3435 } else {
3436 WMUnmapWidget(tPtr->ruler);
3439 textDidResize(tPtr->view->delegate, tPtr->view);
3442 Bool
3443 WMGetTextRulerShown(WMText *tPtr)
3445 if(!tPtr)
3446 return 0;
3448 if(!tPtr->ruler)
3449 return 0;
3451 return tPtr->flags.rulerShown;
3455 void
3456 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3458 if (!tPtr)
3459 return;
3461 if (shouldhave && !tPtr->vS) {
3462 tPtr->vS = WMCreateScroller(tPtr);
3463 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3464 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3465 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3466 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3467 WMMapWidget(tPtr->vS);
3468 } else if (!shouldhave && tPtr->vS) {
3469 WMUnmapWidget(tPtr->vS);
3470 WMDestroyWidget(tPtr->vS);
3471 tPtr->vS = NULL;
3474 tPtr->vpos = 0;
3475 tPtr->prevVpos = 0;
3476 textDidResize(tPtr->view->delegate, tPtr->view);
3481 Bool
3482 WMScrollText(WMText *tPtr, int amount)
3484 Bool scroll=False;
3485 if (!tPtr)
3486 return False;
3487 if (amount == 0 || !tPtr->view->flags.realized)
3488 return False;
3490 if (amount < 0) {
3491 if (tPtr->vpos > 0) {
3492 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3493 else tPtr->vpos=0;
3494 scroll=True;
3495 } } else {
3496 int limit = tPtr->docHeight - tPtr->visible.h;
3497 if (tPtr->vpos < limit) {
3498 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3499 else tPtr->vpos = limit;
3500 scroll = True;
3501 } }
3503 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3504 updateScrollers(tPtr);
3505 paintText(tPtr);
3507 tPtr->prevVpos = tPtr->vpos;
3508 return scroll;
3511 Bool
3512 WMPageText(WMText *tPtr, Bool direction)
3514 if (!tPtr) return False;
3515 if (!tPtr->view->flags.realized) return False;
3517 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3520 void
3521 WMSetTextEditable(WMText *tPtr, Bool editable)
3523 if (!tPtr)
3524 return;
3525 tPtr->flags.editable = editable;
3528 int
3529 WMGetTextEditable(WMText *tPtr)
3531 if (!tPtr)
3532 return 0;
3533 return tPtr->flags.editable;
3536 void
3537 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3539 if (!tPtr)
3540 return;
3541 tPtr->flags.ignoreNewLine = ignore;
3544 Bool
3545 WMGetTextIgnoresNewline(WMText *tPtr)
3547 if (!tPtr)
3548 return True;
3549 return tPtr->flags.ignoreNewLine;
3552 void
3553 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3555 if (!tPtr)
3556 return;
3557 if (mono && tPtr->flags.rulerShown)
3558 WMShowTextRuler(tPtr, False);
3560 tPtr->flags.monoFont = mono;
3561 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3564 Bool
3565 WMGetTextUsesMonoFont(WMText *tPtr)
3567 if (!tPtr)
3568 return True;
3569 return tPtr->flags.monoFont;
3573 void
3574 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3576 if (!tPtr)
3577 return;
3579 WMReleaseFont(tPtr->dFont);
3580 if (font)
3581 tPtr->dFont = WMRetainFont(font);
3582 else
3583 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3586 WMFont *
3587 WMGetTextDefaultFont(WMText *tPtr)
3589 if (!tPtr)
3590 return NULL;
3591 else
3592 return tPtr->dFont;
3595 void
3596 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3598 if (!tPtr)
3599 return;
3600 tPtr->flags.alignment = alignment;
3601 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3604 void
3605 WMSetTextParser(WMText *tPtr, WMAction *parser)
3607 if (!tPtr)
3608 return;
3609 tPtr->parser = parser;
3612 void
3613 WMSetTextWriter(WMText *tPtr, WMAction *writer)
3615 if (!tPtr)
3616 return;
3617 tPtr->writer = writer;
3620 int
3621 WMGetTextInsertType(WMText *tPtr)
3623 if (!tPtr)
3624 return 0;
3625 return tPtr->flags.prepend;
3629 void
3630 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3632 if (!tPtr || !color)
3633 return;
3635 setSelectionProperty(tPtr, NULL, color);
3640 void
3641 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3643 if (!tPtr || !font)
3644 return;
3646 setSelectionProperty(tPtr, font, NULL);
3650 void
3651 WMFreezeText(WMText *tPtr)
3653 if (!tPtr)
3654 return;
3656 tPtr->flags.frozen = True;
3659 void
3660 WMThawText(WMText *tPtr)
3662 if (!tPtr)
3663 return;
3665 tPtr->flags.frozen = False;
3669 Bool
3670 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3671 Bool caseSensitive)
3673 TextBlock *tb;
3674 char *s, *mark;
3675 unsigned short pos;
3677 if (!tPtr || !needle)
3678 return False;
3680 if (! (tb = tPtr->currentTextBlock)) {
3681 if (! (tb = ( (direction > 0) ?
3682 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3683 return False;
3685 } else {
3686 //if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3687 // tb = (direction>0) ? tb->next : tb->prior;
3688 if(tb != tPtr->lastTextBlock)
3689 tb = tb->prior;
3693 while(tb) {
3694 if (!tb->graphic) {
3695 pos = tPtr->tpos;
3696 if(pos+1 < tb->used)
3697 pos++;
3698 pos--;
3699 if(tb->used - pos> 0 && pos > 0) {
3700 char tmp = tb->text[tb->used];
3701 tb->text[tb->used] = 0;
3703 if(direction > 0)
3704 mark = strstr(&tb->text[pos], needle);
3705 else
3706 mark = mystrrstr(&tb->text[pos], needle,
3707 strlen(needle), tb->text);
3709 tb->text[tb->used] = tmp;
3711 } else {
3712 return False;
3715 if(mark) {
3716 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3718 tPtr->tpos = (int)(mark - tb->text);
3720 tPtr->currentTextBlock = tb;
3721 updateCursorPosition(tPtr);
3722 tPtr->sel.y = tPtr->cursor.y+5;
3723 tPtr->sel.h = tPtr->cursor.h-10;
3724 tPtr->sel.x = tPtr->cursor.x +1;
3725 tPtr->sel.w = WMIN(WMWidthOfString(font,
3726 &tb->text[tPtr->tpos], strlen(needle)),
3727 tPtr->docWidth - tPtr->sel.x);
3728 tPtr->flags.ownsSelection = True;
3729 paintText(tPtr);
3731 return True;
3735 tb = (direction>0) ? tb->next : tb->prior;
3736 pos = 0;
3739 return False;
3743 typedef struct _currentFormat {
3744 WMArray *fonts;
3745 WMArray *colors;
3746 WMColor *ccolor;
3747 WMFont *cfont;
3748 WMRulerMargins margins;
3749 //WMArray *aligns; // for tables...
3750 /* the following are "nested"
3751 i.e.: <b><b><i></b><b></i>
3752 1 2 1 1 2 0 get it? */
3753 short i;
3754 short b;
3755 short u;
3756 short fmargin;
3757 short bmargin;
3758 short first:1;
3759 short type:1;
3760 WMAlignment align:2;
3761 short ul:3; /* how "nested"... up to 8 levels deep */
3762 short comment:1; /* ignore text till --> */
3763 short RESERVED:10;
3764 } CFMT;
3765 CFMT cfmt;
3769 #if 0
3770 getArg(char *t, short type, void *arg)
3772 short d=0;
3773 while(*(++t) && !d) {
3774 if(type==0) {
3775 if(*t>='0' && *t<='9') {
3776 sscanf(t, "%d", arg);
3777 while(*t&& (*t<'0' || *t>'9'))
3778 t++;
3779 d=1;
3784 #endif
3786 static void
3787 parseToken(WMText *tPtr, char *token, short tk)
3789 short open=0; /* 0 starts, 1 closes */
3790 void *tb= NULL;
3791 int prepend = WMGetTextInsertType(tPtr);
3792 WMScreen *scr = tPtr->view->screen;
3794 if(tk == -1) {
3795 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
3796 NULL, cfmt.cfont, cfmt.ccolor, True, 0));
3797 return;
3801 while(*token && *token == ' ')
3802 token++;
3804 if(*token == '/') {
3805 token++;
3806 open = 1;
3808 while(*token == ' ')
3809 token++;
3812 if(!tPtr->flags.parsingHTML) {
3813 if(mystrcasecmp(token, "html", 4, False)) {
3814 printf("got HTMLLLL: [%s]\n", token);
3815 tPtr->flags.parsingHTML = True;
3816 } else {
3817 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
3818 token, cfmt.cfont, cfmt.ccolor, cfmt.first, strlen(token)));
3821 return;
3825 if(strlen(token)==1) {
3826 /* nice and fast for small tokens... no need for too much brain
3827 power here */
3828 switch(TOLOWER(*token)) {
3829 case 'i':
3830 if(!open) {
3831 cfmt.cfont = WMConvertFontToItalic(scr, cfmt.cfont);
3832 WMAddToArray(cfmt.fonts, (void *)cfmt.cfont);
3833 } else { /*dun wanna remove the baseFont eh? */
3834 int count = WMGetArrayItemCount(cfmt.fonts);
3835 if(count>1) {
3836 WMDeleteFromArray(cfmt.fonts, count-1);
3838 cfmt.cfont = (WMFont *)WMGetFromArray(cfmt.fonts,
3839 WMGetArrayItemCount(cfmt.fonts)-1);
3840 }printf("i\n"); break;
3842 case 'b':
3843 if(!open) {
3844 cfmt.cfont = WMConvertFontToBold(scr, cfmt.cfont);
3845 WMAddToArray(cfmt.fonts, (void *)cfmt.cfont);
3846 } else { /*dun wanna remove the baseFont eh? */
3847 int count = WMGetArrayItemCount(cfmt.fonts);
3848 if(count>1)
3849 WMDeleteFromArray(cfmt.fonts, count-1);
3850 cfmt.cfont = (WMFont *)WMGetFromArray(cfmt.fonts,
3851 WMGetArrayItemCount(cfmt.fonts)-1);
3852 } break;
3853 case 'p':
3854 cfmt.first = True;
3855 tb = WMCreateTextBlockWithText(tPtr, NULL, cfmt.cfont,
3856 cfmt.ccolor, cfmt.first, 0);
3857 // WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
3858 WMAppendTextBlock(tPtr, tb);
3859 cfmt.first = False;
3860 break;
3861 case 'u': cfmt.u = !open; break;
3863 } else {
3864 if(mystrcasecmp(token, "br", 2, False)) {
3865 cfmt.first = True;
3867 else if(mystrcasecmp(token, "ul", 2, False)) {
3868 if(open) {
3869 if(cfmt.ul>1) cfmt.ul--;
3870 } else cfmt.ul++;
3871 if(cfmt.ul) {
3872 cfmt.bmargin = cfmt.ul*30;
3873 cfmt.fmargin = cfmt.bmargin-10;
3874 } else cfmt.fmargin = cfmt.bmargin = 0;
3875 } else if(mystrcasecmp(token, "li", 2, False)) {
3876 cfmt.first = True;
3877 //change margins... create a new margin....
3878 //(cfmt.fmargin, cfmt.bmargin,
3879 } else if(mystrcasecmp(token, "html", 4, False)) {
3880 tPtr->flags.parsingHTML = !open;
3882 } else if(mystrcasecmp(token, "align", 5, False))
3883 ;//printf("align");
3884 else if(mystrcasecmp(token, "img", 3, False)) {
3885 if(!open) {
3886 char *mark=NULL;
3887 WMPixmap *pixmap;
3888 token+=3;
3889 while(*token == ' ') token++;
3890 do {
3891 switch(TOLOWER(*token)) {
3892 case 's':
3893 if(TOLOWER(*(1+token)) == 'r' && TOLOWER(*(2+token)) == 'c') {
3894 mark = strchr(token, '=');
3895 if(mark) {
3896 char img[256], *iptr;
3897 token = mark+1;
3898 if(!token) return;
3899 sscanf(token, "%s", img);
3900 iptr = img;
3901 if(*img == '\"') { img[strlen(img)-1] = 0; iptr++;}
3902 pixmap = WMCreatePixmapFromFile(scr, iptr);
3903 if(pixmap) {
3904 tb = WMCreateTextBlockWithPixmap(tPtr, pixmap,
3905 iptr, cfmt.ccolor, cfmt.first, 0);
3906 // WMSetTextBlockProperties(tb, cfmt.first,
3907 // False, (cfmt.u?1:0), 0, cfmt.margins);
3908 WMAppendTextBlock(tPtr, tb);
3909 cfmt.first = False;
3911 //printf("[%s]\n", iptr);
3912 } } break; } } while(*(token++));
3914 } else if(mystrcasecmp(token, "font", 4, False)) {
3915 #if 0
3916 if(open) {
3917 cfmt.cfont = (WMFont *)WMGetFromArray(cfmt.fonts,
3918 WMGetArrayItemCount(cfmt.fonts)-1);
3919 } else
3920 (WMColor *)WMGetFromArray(cfmt.colors,
3921 WMGetArrayItemCount(cfmt.colors)-1),
3922 #endif
3924 else if(mystrcasecmp(token, "center", 6, False)) {
3925 printf("center\n");
3926 if(open) cfmt.align = WALeft;
3927 else cfmt.align = WACenter;
3928 cfmt.first = True;
3929 //change margins...
3936 //printf("parse token (%s)[%s]\n", open?"close":"open", token);
3937 #if 0
3938 i=0;
3939 //while(*token && !isspace(*(token))) token++;
3940 //printf("A:%d a:%d z%d Z%d\n", '1', 'a', 'Z', 'z');
3941 do {
3942 if(!mm) {
3943 if(c>=65 && c<=122) { major[i++] = c;
3944 } else if(c==' ' || c=='='){ major[i] = 0; i=0; mm=1;
3945 printf("\nmajor: [%s]", major);}
3946 } else {
3947 if(c!=' ') {
3948 minor[i++] = c;
3949 } else { minor[i] = 0; i=0; printf(" minor: [%s] ", minor);}
3951 }while((c = *(++token)));
3952 #endif
3955 //printf("parse token (%s)[%s]\n", open?"close":"open", token);