- the list multiple selection code is working now. it still needs some
[wmaker-crm.git] / WINGs / wtext.c
blob00aa52b848535168f1562dd26bc832c21698d1f4
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:
30 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
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-2; 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 if(!(tb = tPtr->currentTextBlock))
2001 break;
2002 if(tb->graphic)
2003 goto L_imaGFX;
2005 if(tPtr->tpos==0) {
2006 L_imaGFX: if(tb->prior) {
2007 tPtr->currentTextBlock = tb->prior;
2008 tPtr->tpos = tPtr->currentTextBlock->used -1;
2009 } else tPtr->tpos = 0;
2010 } else tPtr->tpos--;
2011 updateCursorPosition(tPtr);
2012 paintText(tPtr);
2013 break;
2015 case XK_Right:
2016 if(!(tb = tPtr->currentTextBlock))
2017 break;
2018 if(tb->graphic)
2019 goto R_imaGFX;
2020 if(tPtr->tpos == tb->used) {
2021 R_imaGFX: if(tb->next) {
2022 tPtr->currentTextBlock = tb->next;
2023 tPtr->tpos = 1;
2024 } else tPtr->tpos = tb->used;
2025 } else tPtr->tpos++;
2026 updateCursorPosition(tPtr);
2027 paintText(tPtr);
2028 break;
2030 case XK_Down:
2031 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2032 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2033 paintText(tPtr);
2034 break;
2036 case XK_Up:
2037 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2038 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2039 paintText(tPtr);
2040 break;
2042 case XK_BackSpace:
2043 case XK_Delete:
2044 case XK_KP_Delete:
2045 deleteTextInteractively(tPtr, ksym);
2046 updateCursorPosition(tPtr);
2047 paintText(tPtr);
2048 break;
2050 case XK_Control_R :
2051 case XK_Control_L :
2052 control_pressed = True;
2053 break;
2055 case XK_Return:
2056 buffer[0] = '\n';
2057 default:
2058 if (buffer[0] != 0 && !control_pressed) {
2059 insertTextInteractively(tPtr, buffer, 1);
2060 updateCursorPosition(tPtr);
2061 paintText(tPtr);
2063 } else if (control_pressed && ksym==XK_r) {
2064 Bool i = !tPtr->flags.rulerShown;
2065 WMShowTextRuler(tPtr, i);
2066 tPtr->flags.rulerShown = i;
2068 else if (control_pressed && buffer[0] == '\a')
2069 XBell(tPtr->view->screen->display, 0);
2072 if (!control_pressed && tPtr->flags.ownsSelection)
2073 releaseSelection(tPtr);
2076 static void
2077 handleWidgetPress(XEvent *event, void *data)
2079 TextBlock *tb = (TextBlock *)data;
2080 Text *tPtr;
2081 WMWidget *w;
2083 if (!tb)
2084 return;
2085 /* this little bit of nastiness here saves a boatload of trouble */
2086 w = (WMWidget *)(((W_VIEW(tb->d.widget))->parent)->self);
2087 if (W_CLASS(w) != WC_Text)
2088 return;
2089 tPtr = (Text*)w;
2090 tPtr->currentTextBlock = getFirstNonGraphicBlockFor(tb, 1);
2091 if (!tPtr->currentTextBlock)
2092 tPtr->currentTextBlock = tb;
2093 tPtr->tpos = 0;
2094 output(tPtr->currentTextBlock->text, tPtr->currentTextBlock->used);
2095 #if 0
2096 if (!tPtr->flags.focused) {
2097 WMSetFocusToWidget(tPtr);
2098 tPtr->flags.focused = True;
2100 #endif
2104 static void
2105 handleActionEvents(XEvent *event, void *data)
2107 Text *tPtr = (Text *)data;
2108 Display *dpy = event->xany.display;
2109 KeySym ksym;
2112 switch (event->type) {
2113 case KeyPress:
2114 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2115 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2116 tPtr->flags.extendSelection = True;
2117 return;
2120 if (tPtr->flags.focused) {
2121 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2122 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2123 GrabModeAsync, GrabModeAsync, None,
2124 tPtr->view->screen->invisibleCursor, CurrentTime);
2125 tPtr->flags.pointerGrabbed = True;
2126 handleTextKeyPress(tPtr, event);
2128 } break;
2130 case KeyRelease:
2131 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2132 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2133 tPtr->flags.extendSelection = False;
2134 return;
2135 /* end modify flag so selection can be extended */
2137 break;
2140 case MotionNotify:
2142 if (tPtr->flags.pointerGrabbed) {
2143 tPtr->flags.pointerGrabbed = False;
2144 XUngrabPointer(dpy, CurrentTime);
2147 if(tPtr->flags.waitingForSelection)
2148 break;
2150 if ((event->xmotion.state & Button1Mask)) {
2151 if (!tPtr->flags.ownsSelection) {
2152 WMCreateSelectionHandler(tPtr->view,
2153 XA_PRIMARY, event->xbutton.time,
2154 &selectionHandler, NULL);
2155 tPtr->flags.ownsSelection = True;
2157 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2159 break;
2162 case ButtonPress:
2164 tPtr->flags.buttonHeld = True;
2166 if (tPtr->flags.pointerGrabbed) {
2167 tPtr->flags.pointerGrabbed = False;
2168 XUngrabPointer(dpy, CurrentTime);
2169 break;
2172 if (tPtr->flags.waitingForSelection)
2173 break;
2175 if (tPtr->flags.extendSelection) {
2176 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2177 return;
2181 if (event->xbutton.button == Button1) {
2183 if(WMIsDoubleClick(event)) {
2184 printf("lastClickTime: %d \n", event->xbutton.time -tPtr->lastClickTime);
2185 autoSelectText(tPtr, 2);
2186 tPtr->lastClickTime = event->xbutton.time;
2187 break;
2189 if(0&&event->xbutton.time - tPtr->lastClickTime
2190 < 1.5*WINGsConfiguration.doubleClickDelay) {
2191 ;// autoSelectText(tPtr, 3);
2192 break;
2195 //WMGetTextStreamIntoArray(tPtr);
2197 if (!tPtr->flags.focused) {
2198 WMSetFocusToWidget(tPtr);
2199 tPtr->flags.focused = True;
2202 if (tPtr->flags.ownsSelection)
2203 releaseSelection(tPtr);
2205 tPtr->lastClickTime = event->xbutton.time;
2206 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2207 paintText(tPtr);
2210 if (event->xbutton.button
2211 == WINGsConfiguration.mouseWheelDown) {
2212 WMScrollText(tPtr, -16);
2213 break;
2216 if (event->xbutton.button
2217 == WINGsConfiguration.mouseWheelUp) {
2218 WMScrollText(tPtr, 16);
2219 break;
2222 if (event->xbutton.button == Button2) {
2223 char *text = NULL;
2224 int n;
2226 if (!tPtr->flags.editable) {
2227 XBell(dpy, 0);
2228 break;
2231 #if 0
2232 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2233 event->xbutton.time, pasteText, NULL)) {
2234 #endif
2237 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2239 if (text) {
2240 text[n] = 0;
2242 if (tPtr->parser)
2243 (tPtr->parser) (tPtr, (void *) text);
2244 else
2245 insertTextInteractively(tPtr, text, n);
2247 XFree(text);
2248 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2249 (void*)WMInsertTextEvent);
2251 } else {
2252 tPtr->flags.waitingForSelection = True;
2255 break;
2259 case ButtonRelease:
2261 tPtr->flags.buttonHeld = False;
2263 if (tPtr->flags.pointerGrabbed) {
2264 tPtr->flags.pointerGrabbed = False;
2265 XUngrabPointer(dpy, CurrentTime);
2266 break;
2269 if (tPtr->flags.waitingForSelection)
2270 break;
2276 static void
2277 handleEvents(XEvent *event, void *data)
2279 Text *tPtr = (Text *)data;
2281 switch(event->type) {
2282 case Expose:
2284 if (event->xexpose.count!=0)
2285 break;
2287 if(tPtr->hS) {
2288 if (!(W_VIEW(tPtr->hS))->flags.realized)
2289 WMRealizeWidget(tPtr->hS);
2290 if (!((W_VIEW(tPtr->hS))->flags.mapped))
2291 WMMapWidget(tPtr->hS);
2294 if(tPtr->vS) {
2295 if (!(W_VIEW(tPtr->vS))->flags.realized)
2296 WMRealizeWidget(tPtr->vS);
2297 if (!((W_VIEW(tPtr->vS))->flags.mapped))
2298 WMMapWidget(tPtr->vS);
2301 if(tPtr->ruler) {
2302 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2303 WMRealizeWidget(tPtr->ruler);
2305 if (!((W_VIEW(tPtr->ruler))->flags.mapped)
2306 && tPtr->flags.rulerShown)
2307 WMMapWidget(tPtr->ruler);
2310 if(!tPtr->db)
2311 textDidResize(tPtr->view->delegate, tPtr->view);
2313 paintText(tPtr);
2314 break;
2316 case FocusIn:
2317 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2318 != tPtr->view)
2319 return;
2320 tPtr->flags.focused = True;
2321 #if DO_BLINK
2322 if (tPtr->flags.editable && !tPtr->timerID) {
2323 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2324 blinkCursor, tPtr);
2326 #endif
2328 break;
2330 case FocusOut:
2331 tPtr->flags.focused = False;
2332 paintText(tPtr);
2333 #if DO_BLINK
2334 if (tPtr->timerID) {
2335 WMDeleteTimerHandler(tPtr->timerID);
2336 tPtr->timerID = NULL;
2338 #endif
2339 break;
2342 case DestroyNotify:
2343 clearText(tPtr);
2344 if(tPtr->hS)
2345 WMDestroyWidget(tPtr->hS);
2346 if(tPtr->vS)
2347 WMDestroyWidget(tPtr->vS);
2348 if(tPtr->ruler)
2349 WMDestroyWidget(tPtr->ruler);
2350 if(tPtr->db)
2351 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2352 if(tPtr->gfxItems)
2353 WMFreeArray(tPtr->gfxItems);
2354 #if DO_BLINK
2355 if (tPtr->timerID)
2356 WMDeleteTimerHandler(tPtr->timerID);
2357 #endif
2358 WMReleaseFont(tPtr->dFont);
2359 WMReleaseColor(tPtr->dColor);
2360 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2361 WMRemoveNotificationObserver(tPtr);
2363 wfree(tPtr);
2365 break;
2371 static void
2372 insertText(WMText *tPtr, char *stream)
2374 char *ptr, *begin, *end;
2375 void *tb = NULL;
2376 char c;
2377 char b, e, again=False;
2378 unsigned long tokenLength, tk;
2379 unsigned long textLength, tx;
2381 if (!stream) {
2382 clearText(tPtr);
2383 return;
2386 WMAppendTextBlock(tPtr,
2387 WMCreateTextBlockWithText(tPtr, stream, tPtr->dFont, tPtr->dColor,
2388 1, strlen(stream)));
2389 return;
2390 ptr = stream;
2391 while (ptr) {
2392 again = False;
2393 b = '<'; e = '>';
2394 again:
2395 begin = strchr(ptr, b);
2396 if (begin) {
2397 end = strchr(begin+1, e);
2398 if(end) {
2399 if((int)(end-begin)-1 > 1)
2400 output(begin+1, (int)(end-begin)-1);
2402 ptr = begin+1;
2403 } else if(!again) {
2404 b = '>'; e = '<';
2405 again = True;
2406 printf("again!\n");
2407 goto again;
2408 } else {
2409 if (ptr && strlen(ptr)) {
2410 printf("boo\n");
2412 ptr = begin;
2416 #if 0
2417 while( (c=*(stream++))) {
2418 //printf("%c", c);
2419 if((c == '\n' && tPtr->flags.parsingHTML) || c =='\t')
2420 //c = ' '; //continue;
2421 continue;
2422 if(c == ' ') {
2423 if(wasspace)
2424 continue;
2425 wasspace = 1;
2426 }else wasspace = 0;
2428 if(c == '\n') {
2429 parseToken(tPtr, token, -1);
2431 } else if(c == '<' && !open) {
2432 open=1;
2433 if(textlen>0) {
2434 text[textlen] = 0;
2435 tb = WMCreateTextBlockWithText(tPtr, text, cfmt.cfont,
2436 cfmt.ccolor, cfmt.first, textlen);
2437 //WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
2438 WMAppendTextBlock(tPtr, tb);
2439 cfmt.first = False;
2440 //printf("%s\n", text);
2442 textlen = 0;
2443 } else if(c == '>' && open) {
2444 token[tk] = 0;
2445 if(tk>0) parseToken(tPtr, token, tk);
2446 open=0;
2447 tk=0;
2448 } else {
2449 if(open) {
2450 if(tk < MAX_TOKEN_SIZE) token[tk++] = c;
2451 } else if(textlen < MAX_TEXT_SIZE) text[textlen++] = c;
2455 if(tk>0) { token[tk] = 0; parseToken(tPtr, token, tk);}
2456 if(textlen>0) {
2457 text[textlen] = 0;
2458 //printf("%s\n", text);
2459 tb = WMCreateTextBlockWithText(tPtr, text,
2460 (WMFont *)WMGetFromArray(cfmt.fonts,
2461 WMGetArrayItemCount(cfmt.fonts)-1),
2462 (WMColor *)WMGetFromArray(cfmt.colors,
2463 WMGetArrayItemCount(cfmt.colors)-1),
2464 cfmt.first, textlen);
2465 //WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
2466 WMAppendTextBlock(tPtr, tb);
2467 cfmt.first = False;
2469 #endif
2474 #if 0
2475 start = text;
2476 while (start) {
2477 mark = strchr(start, '\n');
2478 if (mark) {
2479 tb = WMCreateTextBlockWithText(tPtr,
2480 start, tPtr->dFont,
2481 tPtr->dColor, True, (int)(mark-start));
2482 start = mark+1;
2483 } else {
2484 if (start && strlen(start)) {
2485 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2486 tPtr->dColor, False, strlen(start));
2487 } else tb = NULL;
2488 start = mark;
2491 if (tPtr->flags.prepend)
2492 WMPrependTextBlock(tPtr, tb);
2493 else
2494 WMAppendTextBlock(tPtr, tb);
2496 return;
2497 #endif
2502 static void
2503 rulerMoveCallBack(WMWidget *w, void *self)
2505 Text *tPtr = (Text *)self;
2506 if (!tPtr)
2507 return;
2508 if (W_CLASS(tPtr) != WC_Text)
2509 return;
2511 paintText(tPtr);
2515 static void
2516 rulerReleaseCallBack(WMWidget *w, void *self)
2518 Text *tPtr = (Text *)self;
2519 if (!tPtr)
2520 return;
2521 if (W_CLASS(tPtr) != WC_Text)
2522 return;
2524 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
2525 return;
2529 static unsigned
2530 draggingEntered(WMView *self, WMDraggingInfo *info)
2532 printf("draggingEntered\n");
2533 return WDOperationCopy;
2537 static unsigned
2538 draggingUpdated(WMView *self, WMDraggingInfo *info)
2540 return WDOperationCopy;
2544 static void
2545 draggingExited(WMView *self, WMDraggingInfo *info)
2547 printf("draggingExited\n");
2550 static Bool
2551 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2553 printf("prepareForDragOperation\n");
2554 return True;
2558 char *badbadbad;
2560 static void
2561 receivedData(WMView *view, Atom selection, Atom target, Time timestamp,
2562 void *cdata, WMData *data)
2564 badbadbad = wstrdup((char *)WMDataBytes(data));
2568 /* when it's done in WINGs, remove this */
2570 Bool requestDroppedData(WMView *view, WMDraggingInfo *info, char *type)
2572 WMScreen *scr = W_VIEW_SCREEN(view);
2574 if (!WMRequestSelection(scr->dragInfo.destView,
2575 scr->xdndSelectionAtom,
2576 XInternAtom(scr->display, type, False),
2577 scr->dragInfo.timestamp,
2578 receivedData, &scr->dragInfo)) {
2579 wwarning("could not request data for dropped data");
2584 XEvent ev;
2586 ev.type = ClientMessage;
2587 ev.xclient.message_type = scr->xdndFinishedAtom;
2588 ev.xclient.format = 32;
2589 ev.xclient.window = info->destinationWindow;
2590 ev.xclient.data.l[0] = 0;
2591 ev.xclient.data.l[1] = 0;
2592 ev.xclient.data.l[2] = 0;
2593 ev.xclient.data.l[3] = 0;
2594 ev.xclient.data.l[4] = 0;
2596 XSendEvent(scr->display, info->sourceWindow, False, 0, &ev);
2597 XFlush(scr->display);
2601 static Bool
2602 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
2604 WMColor *color;
2605 WMText *tPtr = (WMText *)self->self;
2607 if (!tPtr)
2608 return True;
2610 requestDroppedData(tPtr->view, info, "application/X-color");
2611 color = WMCreateNamedColor(W_VIEW_SCREEN(self), badbadbad, True);
2612 if(color) {
2613 WMSetTextSelectionColor(tPtr, color);
2614 WMReleaseColor(color);
2619 return True;
2622 static void
2623 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2625 printf("concludeDragOperation\n");
2629 static WMDragDestinationProcs _DragDestinationProcs = {
2630 draggingEntered,
2631 draggingUpdated,
2632 draggingExited,
2633 prepareForDragOperation,
2634 performDragOperation,
2635 concludeDragOperation
2638 static void
2639 releaseArrayData(void *data)
2641 if(data)
2642 wfree(data);
2646 char *
2647 getStream(WMText *tPtr, int sel, int array)
2649 TextBlock *tb = NULL;
2650 char *text = NULL;
2651 unsigned long where = 0;
2653 if (!tPtr)
2654 return NULL;
2656 if (!(tb = tPtr->firstTextBlock))
2657 return NULL;
2659 /* this might be tricky to get right... not yet implemented */
2660 if (tPtr->writer) {
2661 (tPtr->writer) (tPtr, (void *) text);
2662 return text;
2665 tb = tPtr->firstTextBlock;
2666 while (tb) {
2668 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2670 if (!sel || (tb->graphic && tb->selected)) {
2672 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)) {
2673 text = wrealloc(text, where+1);
2674 text[where++] = '\n';
2677 if(tb->graphic && array) {
2678 text = wrealloc(text, where+3);
2679 text[where++] = 'G';//0xFA;
2680 text[where++] = 'A';//tb->used;
2681 text[where++] = 'H';//18+tb->allocated;
2684 text = wrealloc(text, where+tb->used);
2685 memcpy(&text[where], tb->text, tb->used);
2686 where += tb->used;
2689 } else if (sel && tb->selected) {
2691 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)) {
2692 text = wrealloc(text, where+1);
2693 text[where++] = '\n';
2696 text = wrealloc(text, where+(tb->s_end - tb->s_begin));
2697 memcpy(&text[where], &tb->text[tb->s_begin],
2698 tb->s_end - tb->s_begin);
2699 where += tb->s_end - tb->s_begin;
2704 tb = tb->next;
2707 /* +1 for the end of string, let's be nice */
2708 text = wrealloc(text, where+1);
2709 text[where] = 0;
2710 return text;
2715 WMArray *
2716 getStreamIntoArray(WMText *tPtr, int sel)
2718 WMArray *array = WMCreateArray(4);
2719 WMData *data;
2720 char *stream;
2721 unsigned long loc=0, len;
2722 unsigned long end;
2723 int i, count;
2726 stream = getStream(tPtr, sel, 0);
2727 if(!stream)
2728 return NULL;
2731 printf(stream);
2732 return;
2733 end = strlen(stream);
2735 #if 0
2736 data = WMCreateDataWithBytes((void *)(fa+2), len);
2737 WMSetDataFormat(data, 32);
2738 WMAddToArray(array, (void *) data);
2739 start = fa + len + 2;
2741 } else {
2742 if (start && strlen(start)) {
2743 data = WMCreateDataWithBytes((void *)start, strlen(start));
2744 WMSetDataFormat(data, 8);
2745 WMAddToArray(array, (void *) data);
2747 start = fa;
2751 wfree(stream);
2752 return array;
2753 #endif
2754 WMFreeArray(array);
2758 WMText *
2759 WMCreateText(WMWidget *parent)
2761 Text *tPtr = wmalloc(sizeof(Text));
2762 if (!tPtr) {
2763 printf("could not create text widget\n");
2764 return NULL;
2768 memset(tPtr, 0, sizeof(Text));
2769 tPtr->widgetClass = WC_Text;
2770 tPtr->view = W_CreateView(W_VIEW(parent));
2771 if (!tPtr->view) {
2772 perror("could not create text's view\n");
2773 free(tPtr);
2774 return NULL;
2776 tPtr->view->self = tPtr;
2777 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2778 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2779 W_ResizeView(tPtr->view, 250, 200);
2781 tPtr->dColor = WMWhiteColor(tPtr->view->screen);
2782 tPtr->bgGC = WMColorGC(tPtr->dColor);
2783 W_SetViewBackgroundColor(tPtr->view, tPtr->dColor);
2784 WMReleaseColor(tPtr->dColor);
2786 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2787 tPtr->fgGC = WMColorGC(tPtr->dColor);
2789 tPtr->ruler = NULL;
2790 tPtr->vS = NULL;
2791 tPtr->hS = NULL;
2793 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
2795 tPtr->view->delegate = &_TextViewDelegate;
2797 #if DO_BLINK
2798 tPtr->timerID = NULL;
2799 #endif
2801 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2802 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2803 handleEvents, tPtr);
2805 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2806 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2807 handleActionEvents, tPtr);
2809 WMAddNotificationObserver(ownershipObserver, tPtr,
2810 "_lostOwnership", tPtr);
2812 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2814 char *types[2] = {"application/X-color", NULL};
2815 WMRegisterViewForDraggedTypes(tPtr->view, types);
2818 WMAddNotificationObserver(fontChanged, tPtr,
2819 "WMFontPanelDidChangeNotification", tPtr);
2821 tPtr->firstTextBlock = NULL;
2822 tPtr->lastTextBlock = NULL;
2823 tPtr->currentTextBlock = NULL;
2824 tPtr->tpos = 0;
2826 tPtr->gfxItems = WMCreateArray(4);
2828 tPtr->parser = NULL;
2829 tPtr->writer = NULL;
2831 tPtr->sel.x = tPtr->sel.y = 2;
2832 tPtr->sel.w = tPtr->sel.h = 0;
2834 tPtr->clicked.x = tPtr->clicked.y = 2;
2836 tPtr->visible.x = tPtr->visible.y = 2;
2837 tPtr->visible.h = tPtr->view->size.height;
2838 tPtr->visible.w = tPtr->view->size.width - 4;
2840 tPtr->cursor.x = -23;
2842 tPtr->docWidth = 0;
2843 tPtr->docHeight = 0;
2844 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2845 default_bullet);
2846 tPtr->db = (Pixmap) NULL;
2848 tPtr->margins = WMGetRulerMargins(NULL);
2849 tPtr->margins->right = tPtr->visible.w;
2850 tPtr->nMargins = 1;
2852 tPtr->flags.rulerShown = False;
2853 tPtr->flags.monoFont = False;
2854 tPtr->flags.focused = False;
2855 tPtr->flags.editable = True;
2856 tPtr->flags.ownsSelection = False;
2857 tPtr->flags.pointerGrabbed = False;
2858 tPtr->flags.buttonHeld = False;
2859 tPtr->flags.extendSelection = False;
2860 tPtr->flags.frozen = False;
2861 tPtr->flags.cursorShown = True;
2862 tPtr->flags.clickPos = 1;
2863 tPtr->flags.horizOnDemand = False;
2864 tPtr->flags.needsRefresh = False;
2865 tPtr->flags.ignoreNewLine = False;
2866 tPtr->flags.laidOut = False;
2867 tPtr->flags.waitingForSelection = False;
2868 tPtr->flags.prepend = False;
2869 tPtr->flags.parsingHTML = False;
2870 tPtr->flags.relief = WRSunken;
2871 tPtr->flags.alignment = WALeft;
2873 return tPtr;
2876 void
2877 WMPrependTextStream(WMText *tPtr, char *text)
2879 CHECK_CLASS(tPtr, WC_Text);
2881 if(!text)
2882 releaseSelection(tPtr);
2884 tPtr->flags.prepend = True;
2885 if (text && tPtr->parser)
2886 (tPtr->parser) (tPtr, (void *) text);
2887 else
2888 insertText(tPtr, text);
2890 tPtr->flags.needsRefresh = True;
2894 void
2895 WMAppendTextStream(WMText *tPtr, char *text)
2897 CHECK_CLASS(tPtr, WC_Text);
2899 if(!text)
2900 releaseSelection(tPtr);
2902 tPtr->flags.prepend = False;
2903 if (text && tPtr->parser)
2904 (tPtr->parser) (tPtr, (void *) text);
2905 else
2906 insertText(tPtr, text);
2908 tPtr->flags.needsRefresh = True;
2913 char *
2914 WMGetTextStream(WMText *tPtr)
2916 CHECK_CLASS(tPtr, WC_Text);
2917 return getStream(tPtr, 0, 0);
2920 char *
2921 WMGetTextSelected(WMText *tPtr)
2923 CHECK_CLASS(tPtr, WC_Text);
2924 return getStream(tPtr, 1, 0);
2927 WMArray *
2928 WMGetTextStreamIntoArray(WMText *tPtr)
2930 CHECK_CLASS(tPtr, WC_Text);
2931 return getStreamIntoArray(tPtr, 0);
2934 WMArray *
2935 WMGetTextSelectedIntoArray(WMText *tPtr)
2937 CHECK_CLASS(tPtr, WC_Text);
2938 return getStreamIntoArray(tPtr, 1);
2942 void
2943 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
2945 CHECK_CLASS(tPtr, WC_Text);
2947 tPtr->delegate = delegate;
2951 void *
2952 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
2953 char *description, WMColor *color,
2954 unsigned short first, unsigned short extraInfo)
2956 TextBlock *tb;
2957 unsigned short length;
2959 if (!w || !description || !color)
2960 return NULL;
2962 tb = wmalloc(sizeof(TextBlock));
2963 if (!tb)
2964 return NULL;
2966 length = strlen(description);
2967 tb->text = (char *)wmalloc(length);
2968 memset(tb->text, 0, length);
2969 memcpy(tb->text, description, length);
2970 tb->used = length;
2971 tb->blank = False;
2972 tb->d.widget = w;
2973 tb->color = WMRetainColor(color);
2974 tb->marginN = newMargin(tPtr, NULL);
2975 tb->allocated = extraInfo;
2976 tb->first = first;
2977 tb->kanji = False;
2978 tb->graphic = True;
2979 tb->object = True;
2980 tb->underlined = False;
2981 tb->selected = False;
2982 tb->script = 0;
2983 tb->sections = NULL;
2984 tb->nsections = 0;
2985 tb->prior = NULL;
2986 tb->next = NULL;
2988 return tb;
2992 void *
2993 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
2994 char *description, WMColor *color,
2995 unsigned short first, unsigned short extraInfo)
2997 TextBlock *tb;
2998 unsigned short length;
3000 if (!p || !description || !color)
3001 return NULL;
3003 tb = wmalloc(sizeof(TextBlock));
3004 if (!tb)
3005 return NULL;
3007 length = strlen(description);
3008 tb->text = (char *)wmalloc(length);
3009 memset(tb->text, 0, length);
3010 memcpy(tb->text, description, length);
3011 tb->used = length;
3012 tb->blank = False;
3013 tb->d.pixmap = p;
3014 tb->color = WMRetainColor(color);
3015 tb->marginN = newMargin(tPtr, NULL);
3016 tb->allocated = extraInfo;
3017 tb->first = first;
3018 tb->kanji = False;
3019 tb->graphic = True;
3020 tb->object = False;
3021 tb->underlined = False;
3022 tb->selected = False;
3023 tb->script = 0;
3024 tb->sections = NULL;
3025 tb->nsections = 0;
3026 tb->prior = NULL;
3027 tb->next = NULL;
3029 return tb;
3032 void *
3033 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
3034 unsigned short first, unsigned short length)
3036 TextBlock *tb;
3038 if (!font || !color)
3039 return NULL;
3041 tb = wmalloc(sizeof(TextBlock));
3042 if (!tb)
3043 return NULL;
3045 tb->allocated = reqBlockSize(length);
3046 tb->text = (char *)wmalloc(tb->allocated);
3047 memset(tb->text, 0, tb->allocated);
3049 if (length < 1|| !text ) {
3050 *tb->text = ' ';
3051 tb->used = 1;
3052 tb->blank = True;
3053 } else {
3054 memcpy(tb->text, text, length);
3055 tb->used = length;
3056 tb->blank = False;
3059 tb->d.font = WMRetainFont(font);
3060 tb->color = WMRetainColor(color);
3061 tb->marginN = newMargin(tPtr, NULL);
3062 tb->first = first;
3063 tb->kanji = False;
3064 tb->graphic = False;
3065 tb->underlined = False;
3066 tb->selected = False;
3067 tb->script = 0;
3068 tb->sections = NULL;
3069 tb->nsections = 0;
3070 tb->prior = NULL;
3071 tb->next = NULL;
3072 return tb;
3075 void
3076 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
3077 unsigned int kanji, unsigned int underlined, int script,
3078 WMRulerMargins *margins)
3080 TextBlock *tb = (TextBlock *) vtb;
3081 if (!tb)
3082 return;
3084 tb->first = first;
3085 tb->kanji = kanji;
3086 tb->underlined = underlined;
3087 tb->script = script;
3088 tb->marginN = newMargin(tPtr, margins);
3091 void
3092 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
3093 unsigned int *kanji, unsigned int *underlined, int *script,
3094 WMRulerMargins *margins)
3096 TextBlock *tb = (TextBlock *) vtb;
3097 if (!tb)
3098 return;
3100 if (first) *first = tb->first;
3101 if (kanji) *kanji = tb->kanji;
3102 if (underlined) *underlined = tb->underlined;
3103 if (script) *script = tb->script;
3104 if (margins) margins = &tPtr->margins[tb->marginN];
3109 void
3110 WMPrependTextBlock(WMText *tPtr, void *vtb)
3112 TextBlock *tb = (TextBlock *)vtb;
3114 if (!tPtr || !tb)
3115 return;
3117 if (tb->graphic) {
3118 if(tb->object) {
3119 WMWidget *w = tb->d.widget;
3120 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3121 handleWidgetPress, tb);
3122 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3123 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3124 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3127 WMAddToArray(tPtr->gfxItems, (void *)tb);
3128 tPtr->tpos = 0;
3129 } else tPtr->tpos = tb->used;
3131 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3132 tb->next = tb->prior = NULL;
3133 tb->first = True;
3134 tPtr->lastTextBlock = tPtr->firstTextBlock
3135 = tPtr->currentTextBlock = tb;
3136 return;
3139 if(!tb->first) {
3140 tb->marginN = tPtr->currentTextBlock->marginN;
3143 tb->next = tPtr->currentTextBlock;
3144 tb->prior = tPtr->currentTextBlock->prior;
3145 if (tPtr->currentTextBlock->prior)
3146 tPtr->currentTextBlock->prior->next = tb;
3148 tPtr->currentTextBlock->prior = tb;
3149 if (!tb->prior)
3150 tPtr->firstTextBlock = tb;
3152 tPtr->currentTextBlock = tb;
3156 void
3157 WMAppendTextBlock(WMText *tPtr, void *vtb)
3159 TextBlock *tb = (TextBlock *)vtb;
3161 if (!tPtr || !tb)
3162 return;
3164 if (tb->graphic) {
3165 if(tb->object) {
3166 WMWidget *w = tb->d.widget;
3167 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
3168 handleWidgetPress, tb);
3169 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3170 (W_VIEW(w))->attribs.cursor =
3171 tPtr->view->screen->defaultCursor;
3172 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3175 WMAddToArray(tPtr->gfxItems, (void *)tb);
3176 tPtr->tpos = 0;
3177 } else tPtr->tpos = tb->used;
3179 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3180 tb->next = tb->prior = NULL;
3181 tb->first = True;
3182 tPtr->lastTextBlock = tPtr->firstTextBlock
3183 = tPtr->currentTextBlock = tb;
3184 return;
3187 if(!tb->first) {
3188 tb->marginN = tPtr->currentTextBlock->marginN;
3191 tb->next = tPtr->currentTextBlock->next;
3192 tb->prior = tPtr->currentTextBlock;
3193 if (tPtr->currentTextBlock->next)
3194 tPtr->currentTextBlock->next->prior = tb;
3196 tPtr->currentTextBlock->next = tb;
3198 if (!tb->next)
3199 tPtr->lastTextBlock = tb;
3201 tPtr->currentTextBlock = tb;
3204 void *
3205 WMRemoveTextBlock(WMText *tPtr)
3207 TextBlock *tb = NULL;
3209 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
3210 || !tPtr->currentTextBlock) {
3211 printf("cannot remove non existent TextBlock!\b");
3212 return tb;
3215 tb = tPtr->currentTextBlock;
3216 if (tb->graphic) {
3217 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3219 if(tb->object) {
3220 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
3221 handleWidgetPress, tb);
3222 WMUnmapWidget(tb->d.widget);
3226 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3227 if (tPtr->currentTextBlock->next)
3228 tPtr->currentTextBlock->next->prior = NULL;
3230 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3231 tPtr->currentTextBlock = tPtr->firstTextBlock;
3233 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3234 tPtr->currentTextBlock->prior->next = NULL;
3235 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3236 tPtr->currentTextBlock = tPtr->lastTextBlock;
3237 } else {
3238 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3239 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3240 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3243 return (void *)tb;
3246 void
3247 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3249 TextBlock *tb = (TextBlock *)vtb;
3250 if (!tPtr || !tb)
3251 return;
3253 if (tb->graphic) {
3254 if(tb->object) {
3255 /* naturally, there's a danger to destroying
3256 widgets whose action brings us here:
3257 ie. press a button to destroy it... need to
3258 find a safer way. till then... this stays commented out */
3259 /* WMDestroyWidget(tb->d.widget);
3260 wfree(tb->d.widget); */
3261 tb->d.widget = NULL;
3262 } else {
3263 WMReleasePixmap(tb->d.pixmap);
3264 tb->d.pixmap = NULL;
3266 } else {
3267 WMReleaseFont(tb->d.font);
3270 WMReleaseColor(tb->color);
3271 if (tb->sections && tb->nsections > 0)
3272 wfree(tb->sections);
3273 wfree(tb->text);
3274 wfree(tb);
3275 tb = NULL;
3279 void
3280 WMRefreshText(WMText *tPtr, int vpos, int hpos)
3282 if (!tPtr || vpos<0 || hpos<0)
3283 return;
3285 if (tPtr->flags.frozen && !tPtr->flags.needsRefresh)
3286 return;
3288 if(tPtr->flags.monoFont) {
3289 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3290 TextBlock *tb;
3292 /* make sure to unmap widgets no matter where they are */
3293 for(j=0; j<c; j++) {
3294 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3295 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3296 WMUnmapWidget(tb->d.widget);
3302 if (tPtr->vpos != vpos) {
3303 if (vpos < 0 || tPtr->docHeight < tPtr->visible.h) {
3304 tPtr->vpos = 0;
3305 } else if(tPtr->docHeight - vpos > tPtr->visible.h - tPtr->visible.y) {
3306 tPtr->vpos = vpos;
3307 } else {
3308 tPtr->vpos = tPtr->docHeight - tPtr->visible.h;
3312 if (tPtr->hpos != hpos) {
3313 if (hpos < 0 || tPtr->docWidth < tPtr->visible.w) {
3314 tPtr->hpos = 0;
3315 } else if(tPtr->docWidth - hpos > tPtr->visible.w - tPtr->visible.x) {
3316 tPtr->hpos = hpos;
3317 } else {
3318 tPtr->hpos = tPtr->docWidth - tPtr->visible.w;
3323 tPtr->flags.laidOut = False;
3324 layOutDocument(tPtr);
3325 updateScrollers(tPtr);
3326 paintText(tPtr);
3327 tPtr->flags.needsRefresh = False;
3331 void
3332 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3334 if (!tPtr)
3335 return;
3337 if (color)
3338 tPtr->fgGC = WMColorGC(color);
3339 else
3340 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3342 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3345 void
3346 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3348 if (!tPtr)
3349 return;
3351 if (color) {
3352 tPtr->bgGC = WMColorGC(color);
3353 W_SetViewBackgroundColor(tPtr->view, color);
3354 } else {
3355 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3356 W_SetViewBackgroundColor(tPtr->view,
3357 WMWhiteColor(tPtr->view->screen));
3360 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3363 void
3364 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3366 if (!tPtr)
3367 return;
3368 tPtr->flags.relief = relief;
3369 paintText(tPtr);
3372 void
3373 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3375 if (!tPtr)
3376 return;
3378 if (shouldhave && !tPtr->hS) {
3379 tPtr->hS = WMCreateScroller(tPtr);
3380 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3381 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3382 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3383 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3384 WMMapWidget(tPtr->hS);
3385 } else if (!shouldhave && tPtr->hS) {
3386 WMUnmapWidget(tPtr->hS);
3387 WMDestroyWidget(tPtr->hS);
3388 tPtr->hS = NULL;
3391 tPtr->hpos = 0;
3392 tPtr->prevHpos = 0;
3393 textDidResize(tPtr->view->delegate, tPtr->view);
3397 void
3398 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3400 if (!tPtr)
3401 return;
3403 if(shouldhave && !tPtr->ruler) {
3404 tPtr->ruler = WMCreateRuler(tPtr);
3405 (W_VIEW(tPtr->ruler))->attribs.cursor =
3406 tPtr->view->screen->defaultCursor;
3407 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3408 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3409 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3410 } else if(!shouldhave && tPtr->ruler) {
3411 WMShowTextRuler(tPtr, False);
3412 WMDestroyWidget(tPtr->ruler);
3413 tPtr->ruler = NULL;
3415 textDidResize(tPtr->view->delegate, tPtr->view);
3418 void
3419 WMShowTextRuler(WMText *tPtr, Bool show)
3421 if(!tPtr)
3422 return;
3423 if(!tPtr->ruler)
3424 return;
3426 if(tPtr->flags.monoFont)
3427 show = False;
3429 tPtr->flags.rulerShown = show;
3430 if(show) {
3431 WMMapWidget(tPtr->ruler);
3432 } else {
3433 WMUnmapWidget(tPtr->ruler);
3436 textDidResize(tPtr->view->delegate, tPtr->view);
3439 Bool
3440 WMGetTextRulerShown(WMText *tPtr)
3442 if(!tPtr)
3443 return 0;
3445 if(!tPtr->ruler)
3446 return 0;
3448 return tPtr->flags.rulerShown;
3452 void
3453 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3455 if (!tPtr)
3456 return;
3458 if (shouldhave && !tPtr->vS) {
3459 tPtr->vS = WMCreateScroller(tPtr);
3460 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3461 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3462 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3463 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3464 WMMapWidget(tPtr->vS);
3465 } else if (!shouldhave && tPtr->vS) {
3466 WMUnmapWidget(tPtr->vS);
3467 WMDestroyWidget(tPtr->vS);
3468 tPtr->vS = NULL;
3471 tPtr->vpos = 0;
3472 tPtr->prevVpos = 0;
3473 textDidResize(tPtr->view->delegate, tPtr->view);
3478 Bool
3479 WMScrollText(WMText *tPtr, int amount)
3481 Bool scroll=False;
3482 if (!tPtr)
3483 return False;
3484 if (amount == 0 || !tPtr->view->flags.realized)
3485 return False;
3487 if (amount < 0) {
3488 if (tPtr->vpos > 0) {
3489 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3490 else tPtr->vpos=0;
3491 scroll=True;
3492 } } else {
3493 int limit = tPtr->docHeight - tPtr->visible.h;
3494 if (tPtr->vpos < limit) {
3495 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3496 else tPtr->vpos = limit;
3497 scroll = True;
3498 } }
3500 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3501 updateScrollers(tPtr);
3502 paintText(tPtr);
3504 tPtr->prevVpos = tPtr->vpos;
3505 return scroll;
3508 Bool
3509 WMPageText(WMText *tPtr, Bool direction)
3511 if (!tPtr) return False;
3512 if (!tPtr->view->flags.realized) return False;
3514 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3517 void
3518 WMSetTextEditable(WMText *tPtr, Bool editable)
3520 if (!tPtr)
3521 return;
3522 tPtr->flags.editable = editable;
3525 int
3526 WMGetTextEditable(WMText *tPtr)
3528 if (!tPtr)
3529 return 0;
3530 return tPtr->flags.editable;
3533 void
3534 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3536 if (!tPtr)
3537 return;
3538 tPtr->flags.ignoreNewLine = ignore;
3541 Bool
3542 WMGetTextIgnoresNewline(WMText *tPtr)
3544 if (!tPtr)
3545 return True;
3546 return tPtr->flags.ignoreNewLine;
3549 void
3550 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3552 if (!tPtr)
3553 return;
3554 if (mono && tPtr->flags.rulerShown)
3555 WMShowTextRuler(tPtr, False);
3557 tPtr->flags.monoFont = mono;
3558 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3561 Bool
3562 WMGetTextUsesMonoFont(WMText *tPtr)
3564 if (!tPtr)
3565 return True;
3566 return tPtr->flags.monoFont;
3570 void
3571 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3573 if (!tPtr)
3574 return;
3576 WMReleaseFont(tPtr->dFont);
3577 if (font)
3578 tPtr->dFont = WMRetainFont(font);
3579 else
3580 tPtr->dFont = WMRetainFont(WMSystemFontOfSize(tPtr->view->screen, 12));
3583 WMFont *
3584 WMGetTextDefaultFont(WMText *tPtr)
3586 if (!tPtr)
3587 return NULL;
3588 else
3589 return tPtr->dFont;
3592 void
3593 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3595 if (!tPtr)
3596 return;
3597 tPtr->flags.alignment = alignment;
3598 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3601 void
3602 WMSetTextParser(WMText *tPtr, WMAction *parser)
3604 if (!tPtr)
3605 return;
3606 tPtr->parser = parser;
3609 void
3610 WMSetTextWriter(WMText *tPtr, WMAction *writer)
3612 if (!tPtr)
3613 return;
3614 tPtr->writer = writer;
3617 int
3618 WMGetTextInsertType(WMText *tPtr)
3620 if (!tPtr)
3621 return 0;
3622 return tPtr->flags.prepend;
3626 void
3627 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3629 if (!tPtr || !color)
3630 return;
3632 setSelectionProperty(tPtr, NULL, color);
3637 void
3638 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3640 if (!tPtr || !font)
3641 return;
3643 setSelectionProperty(tPtr, font, NULL);
3647 void
3648 WMFreezeText(WMText *tPtr)
3650 if (!tPtr)
3651 return;
3653 tPtr->flags.frozen = True;
3656 void
3657 WMThawText(WMText *tPtr)
3659 if (!tPtr)
3660 return;
3662 tPtr->flags.frozen = False;
3666 Bool
3667 WMFindInTextStream(WMText *tPtr, char *needle, Bool direction,
3668 Bool caseSensitive)
3670 TextBlock *tb;
3671 char *s, *mark;
3672 unsigned short pos;
3674 if (!tPtr || !needle)
3675 return False;
3677 if (! (tb = tPtr->currentTextBlock)) {
3678 if (! (tb = ( (direction > 0) ?
3679 tPtr->firstTextBlock : tPtr->lastTextBlock) ) ){
3680 return False;
3682 } else {
3683 //if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3684 // tb = (direction>0) ? tb->next : tb->prior;
3685 if(tb != tPtr->lastTextBlock)
3686 tb = tb->prior;
3690 while(tb) {
3691 if (!tb->graphic) {
3692 pos = tPtr->tpos;
3693 if(pos+1 < tb->used)
3694 pos++;
3696 if(tb->used - pos> 0 && pos > 0) {
3697 char tmp = tb->text[tb->used];
3698 tb->text[tb->used] = 0;
3700 if(direction > 0)
3701 mark = strstr(&tb->text[pos], needle);
3702 else
3703 mark = mystrrstr(&tb->text[pos], needle,
3704 strlen(needle), tb->text);
3705 tb->text[tb->used] = tmp;
3707 } else {
3708 return False;
3711 if(mark) {
3712 WMFont *font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
3714 tPtr->tpos = (int)(mark - tb->text);
3715 tPtr->currentTextBlock = tb;
3716 updateCursorPosition(tPtr);
3717 tPtr->sel.y = tPtr->cursor.y+5;
3718 tPtr->sel.h = tPtr->cursor.h-10;
3719 tPtr->sel.x = tPtr->cursor.x +1;
3720 tPtr->sel.w = WMIN(WMWidthOfString(font,
3721 &tb->text[tPtr->tpos], strlen(needle)),
3722 tPtr->docWidth - tPtr->sel.x);
3723 tPtr->flags.ownsSelection = True;
3724 paintText(tPtr);
3726 return True;
3730 tb = (direction>0) ? tb->next : tb->prior;
3731 pos = 0;
3734 return False;
3738 typedef struct _currentFormat {
3739 WMArray *fonts;
3740 WMArray *colors;
3741 WMColor *ccolor;
3742 WMFont *cfont;
3743 WMRulerMargins margins;
3744 //WMArray *aligns; // for tables...
3745 /* the following are "nested"
3746 i.e.: <b><b><i></b><b></i>
3747 1 2 1 1 2 0 get it? */
3748 short i;
3749 short b;
3750 short u;
3751 short fmargin;
3752 short bmargin;
3753 short first:1;
3754 short type:1;
3755 WMAlignment align:2;
3756 short ul:3; /* how "nested"... up to 8 levels deep */
3757 short comment:1; /* ignore text till --> */
3758 short RESERVED:10;
3759 } CFMT;
3760 CFMT cfmt;
3764 #if 0
3765 getArg(char *t, short type, void *arg)
3767 short d=0;
3768 while(*(++t) && !d) {
3769 if(type==0) {
3770 if(*t>='0' && *t<='9') {
3771 sscanf(t, "%d", arg);
3772 while(*t&& (*t<'0' || *t>'9'))
3773 t++;
3774 d=1;
3779 #endif
3781 static void
3782 parseToken(WMText *tPtr, char *token, short tk)
3784 short open=0; /* 0 starts, 1 closes */
3785 void *tb= NULL;
3786 int prepend = WMGetTextInsertType(tPtr);
3787 WMScreen *scr = tPtr->view->screen;
3789 if(tk == -1) {
3790 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
3791 NULL, cfmt.cfont, cfmt.ccolor, True, 0));
3792 return;
3796 while(*token && *token == ' ')
3797 token++;
3799 if(*token == '/') {
3800 token++;
3801 open = 1;
3803 while(*token == ' ')
3804 token++;
3807 if(!tPtr->flags.parsingHTML) {
3808 if(mystrcasecmp(token, "html", 4, False)) {
3809 printf("got HTMLLLL: [%s]\n", token);
3810 tPtr->flags.parsingHTML = True;
3811 } else {
3812 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
3813 token, cfmt.cfont, cfmt.ccolor, cfmt.first, strlen(token)));
3816 return;
3820 if(strlen(token)==1) {
3821 /* nice and fast for small tokens... no need for too much brain
3822 power here */
3823 switch(TOLOWER(*token)) {
3824 case 'i':
3825 if(!open) {
3826 cfmt.cfont = WMConvertFontToItalic(scr, cfmt.cfont);
3827 WMAddToArray(cfmt.fonts, (void *)cfmt.cfont);
3828 } else { /*dun wanna remove the baseFont eh? */
3829 int count = WMGetArrayItemCount(cfmt.fonts);
3830 if(count>1) {
3831 WMDeleteFromArray(cfmt.fonts, count-1);
3833 cfmt.cfont = (WMFont *)WMGetFromArray(cfmt.fonts,
3834 WMGetArrayItemCount(cfmt.fonts)-1);
3835 }printf("i\n"); break;
3837 case 'b':
3838 if(!open) {
3839 cfmt.cfont = WMConvertFontToBold(scr, cfmt.cfont);
3840 WMAddToArray(cfmt.fonts, (void *)cfmt.cfont);
3841 } else { /*dun wanna remove the baseFont eh? */
3842 int count = WMGetArrayItemCount(cfmt.fonts);
3843 if(count>1)
3844 WMDeleteFromArray(cfmt.fonts, count-1);
3845 cfmt.cfont = (WMFont *)WMGetFromArray(cfmt.fonts,
3846 WMGetArrayItemCount(cfmt.fonts)-1);
3847 } break;
3848 case 'p':
3849 cfmt.first = True;
3850 tb = WMCreateTextBlockWithText(tPtr, NULL, cfmt.cfont,
3851 cfmt.ccolor, cfmt.first, 0);
3852 // WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
3853 WMAppendTextBlock(tPtr, tb);
3854 cfmt.first = False;
3855 break;
3856 case 'u': cfmt.u = !open; break;
3858 } else {
3859 if(mystrcasecmp(token, "br", 2, False)) {
3860 cfmt.first = True;
3862 else if(mystrcasecmp(token, "ul", 2, False)) {
3863 if(open) {
3864 if(cfmt.ul>1) cfmt.ul--;
3865 } else cfmt.ul++;
3866 if(cfmt.ul) {
3867 cfmt.bmargin = cfmt.ul*30;
3868 cfmt.fmargin = cfmt.bmargin-10;
3869 } else cfmt.fmargin = cfmt.bmargin = 0;
3870 } else if(mystrcasecmp(token, "li", 2, False)) {
3871 cfmt.first = True;
3872 //change margins... create a new margin....
3873 //(cfmt.fmargin, cfmt.bmargin,
3874 } else if(mystrcasecmp(token, "html", 4, False)) {
3875 tPtr->flags.parsingHTML = !open;
3877 } else if(mystrcasecmp(token, "align", 5, False))
3878 ;//printf("align");
3879 else if(mystrcasecmp(token, "img", 3, False)) {
3880 if(!open) {
3881 char *mark=NULL;
3882 WMPixmap *pixmap;
3883 token+=3;
3884 while(*token == ' ') token++;
3885 do {
3886 switch(TOLOWER(*token)) {
3887 case 's':
3888 if(TOLOWER(*(1+token)) == 'r' && TOLOWER(*(2+token)) == 'c') {
3889 mark = strchr(token, '=');
3890 if(mark) {
3891 char img[256], *iptr;
3892 token = mark+1;
3893 if(!token) return;
3894 sscanf(token, "%s", img);
3895 iptr = img;
3896 if(*img == '\"') { img[strlen(img)-1] = 0; iptr++;}
3897 pixmap = WMCreatePixmapFromFile(scr, iptr);
3898 if(pixmap) {
3899 tb = WMCreateTextBlockWithPixmap(tPtr, pixmap,
3900 iptr, cfmt.ccolor, cfmt.first, 0);
3901 // WMSetTextBlockProperties(tb, cfmt.first,
3902 // False, (cfmt.u?1:0), 0, cfmt.margins);
3903 WMAppendTextBlock(tPtr, tb);
3904 cfmt.first = False;
3906 //printf("[%s]\n", iptr);
3907 } } break; } } while(*(token++));
3909 } else if(mystrcasecmp(token, "font", 4, False)) {
3910 #if 0
3911 if(open) {
3912 cfmt.cfont = (WMFont *)WMGetFromArray(cfmt.fonts,
3913 WMGetArrayItemCount(cfmt.fonts)-1);
3914 } else
3915 (WMColor *)WMGetFromArray(cfmt.colors,
3916 WMGetArrayItemCount(cfmt.colors)-1),
3917 #endif
3919 else if(mystrcasecmp(token, "center", 6, False)) {
3920 printf("center\n");
3921 if(open) cfmt.align = WALeft;
3922 else cfmt.align = WACenter;
3923 cfmt.first = True;
3924 //change margins...
3931 //printf("parse token (%s)[%s]\n", open?"close":"open", token);
3932 #if 0
3933 i=0;
3934 //while(*token && !isspace(*(token))) token++;
3935 //printf("A:%d a:%d z%d Z%d\n", '1', 'a', 'Z', 'z');
3936 do {
3937 if(!mm) {
3938 if(c>=65 && c<=122) { major[i++] = c;
3939 } else if(c==' ' || c=='='){ major[i] = 0; i=0; mm=1;
3940 printf("\nmajor: [%s]", major);}
3941 } else {
3942 if(c!=' ') {
3943 minor[i++] = c;
3944 } else { minor[i] = 0; i=0; printf(" minor: [%s] ", minor);}
3946 }while((c = *(++token)));
3947 #endif
3950 //printf("parse token (%s)[%s]\n", open?"close":"open", token);