*** empty log message ***
[wmaker-crm.git] / WINGs / wtext.c
blob0ceed11d5ae248d2e830cffb81c0737186bd2b7d
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
28 WMFont * WMGetFontPlain(WMScreen *scrPtr, WMFont *font);
29 WMFont * WMGetFontBold(WMScreen *scrPtr, WMFont *font);
30 WMFont * WMGetFontItalic(WMScreen *scrPtr, WMFont *font);
31 WMFont * WMGetFontOfSize(WMScreen *scrPtr, WMFont *font, int size);
33 /* TODO:
35 * - FIX html parser: 1. <b>foo</i> should STILL BE BOLD!
36 * - 2. " foo > bar " should not confuse it.
37 * - change Bag stuffs to WMArray
38 * - assess danger of destroying widgets whose actions link to other pages
39 * - integrate WMGetFont* functions into WINGs proper (fontpanel)?
40 * - change cursor shape around pixmaps
41 * - redo blink code to reduce paint event... use pixmap buffer...
42 * - add paragraph support (full) and '\n' code in getStream..
43 * - use currentTextBlock and neighbours for fast paint and layout
44 * - replace copious uses of Refreshtext with appropriate layOut()...
45 * - WMFindInTextStream should also highlight found text...
46 * - add full support for Horizontal Scroll
50 /* a Section is a section of a TextBlock that describes what parts
51 of a TextBlock has been laid out on which "line"...
52 o this greatly aids redraw, scroll and selection.
53 o this is created during layoutLine, but may be later modified.
54 o there may be many Sections per TextBlock, hence the array */
55 typedef struct {
56 unsigned int x, y; /* where to draw it from */
57 unsigned short w, h; /* its width and height */
58 unsigned short begin; /* where the layout begins */
59 unsigned short end ; /* where it ends */
60 unsigned short last:1; /* is it the last section on a "line"? */
61 unsigned int _y:31; /* the "line" it and other textblocks are on */
62 } Section;
65 /* a TextBlock is a doubly-linked list of TextBlocks containing:
66 o text for the block, color and font
67 o or a pointer to the pixmap
68 o OR a pointer to the widget and the (text) description for its graphic
71 typedef struct _TextBlock {
72 struct _TextBlock *next; /* next text block in linked list */
73 struct _TextBlock *prior; /* prior text block in linked list */
75 char *text; /* pointer to text (could be kanji) */
76 /* or to the object's description */
77 union {
78 WMFont *font; /* the font */
79 WMWidget *widget; /* the embedded widget */
80 WMPixmap *pixmap; /* the pixmap */
81 } d; /* description */
83 unsigned short used; /* number of chars in this block */
84 unsigned short allocated; /* size of allocation (in chars) */
85 WMColor *color; /* the color */
87 Section *sections; /* the region for layouts (a growable array) */
88 /* an _array_! of size _nsections_ */
90 unsigned short s_begin; /* where the selection begins */
91 unsigned short s_end; /* where it ends */
93 unsigned int first:1; /* first TextBlock in paragraph */
94 unsigned int blank:1; /* ie. blank paragraph */
95 unsigned int kanji:1; /* is of 16-bit characters or not */
96 unsigned int graphic:1; /* graphic or text: text=0 */
97 unsigned int object:1; /* embedded object or pixmap */
98 unsigned int underlined:1; /* underlined or not */
99 unsigned int selected:1; /* selected or not */
100 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
101 int script:8; /* script in points: negative for subscript */
102 unsigned int marginN:8; /* which of the margins in the tPtr to use */
103 unsigned int RESERVED:9;
104 } TextBlock;
107 /* somehow visible.h beats the hell outta visible.size.height :-) */
108 typedef struct {
109 unsigned int y;
110 unsigned int x;
111 unsigned int h;
112 unsigned int w;
113 } myRect;
116 typedef struct W_Text {
117 W_Class widgetClass; /* the class number of this widget */
118 W_View *view; /* the view referring to this instance */
120 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
122 WMScroller *vS; /* the vertical scroller */
123 unsigned int vpos; /* the current vertical position */
124 unsigned int prevVpos; /* the previous vertical position */
126 WMScroller *hS; /* the horizontal scroller */
127 unsigned int hpos; /* the current horizontal position */
128 unsigned int prevHpos; /* the previous horizontal position */
130 WMFont *dFont; /* the default font */
131 WMColor *dColor; /* the default color */
132 WMPixmap *dBulletPix; /* the default pixmap for bullets */
134 GC bgGC; /* the background GC to draw with */
135 GC fgGC; /* the foreground GC to draw with */
136 Pixmap db; /* the buffer on which to draw */
138 myRect visible; /* the actual rectangle that can be drawn into */
139 myRect cursor; /* the position and (height) of cursor */
140 myRect sel; /* the selection rectangle */
142 WMPoint clicked; /* where in the _document_ was clicked */
144 unsigned short tpos; /* the position in the currentTextBlock */
145 unsigned short docWidth; /* the width of the entire document */
146 unsigned int docHeight; /* the height of the entire document */
148 TextBlock *firstTextBlock;
149 TextBlock *lastTextBlock;
150 TextBlock *currentTextBlock;
152 WMBag *gfxItems; /* a nice bag containing graphic items */
154 #if DO_BLINK
155 WMHandlerID timerID; /* for nice twinky-winky */
156 #endif
158 WMAction *parser;
159 WMAction *writer;
160 WMTextDelegate *delegate;
162 WMRulerMargins *margins; /* an array of margins */
164 unsigned int nMargins:8; /* the total number of margins in use */
165 struct {
166 unsigned int monoFont:1; /* whether to ignore formats */
167 unsigned int focused:1; /* whether this instance has input focus */
168 unsigned int editable:1; /* "silly user, you can't edit me" */
169 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
170 unsigned int pointerGrabbed:1;/* "heh, gib me pointer" */
171 unsigned int buttonHeld:1; /* the user is holding down the button */
172 unsigned int extendSelection:1; /* shift-drag to select more regions */
174 unsigned int rulerShown:1; /* whether the ruler is shown or not */
175 unsigned int frozen:1; /* whether screen updates are to be made */
176 unsigned int cursorShown:1; /* whether to show the cursor */
177 unsigned int clickPos:1; /* clicked before=0 or after=1 a graphic: */
178 /* (within counts as after too) */
180 unsigned int horizOnDemand:1;/* if a large image should appear*/
181 unsigned int needsRefresh:1; /* in case of Append/Deletes */
182 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
183 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
184 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
185 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
186 unsigned int parsingHTML:1; /* how to interpret a stream of text */
187 WMAlignment alignment:2; /* the alignment for text */
188 WMReliefType relief:3; /* the relief to display with */
189 unsigned int RESERVED:2;
190 } flags;
191 } Text;
194 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
195 if ((T)->delegate && (T)->delegate->C)\
196 (*(T)->delegate->C)((T)->delegate,notif);\
197 WMPostNotification(notif);\
198 WMReleaseNotification(notif);}
203 * A hack to speed up caseless_equal. Thanks to Quincey Koziol for
204 * developing it for the "chimera" folks so I could use it 7 years later ;-)
205 * Constraint: nothing but '\0' may map to 0
207 static unsigned char map_table[256] = {
208 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,
209 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
210 52,53,54,55,56,57,58,59,60,61,62,63,64,97,98,99,100,101,102,103,104,105,
211 106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,91,
212 92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
213 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,
214 130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,
215 148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,
216 166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
217 184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,
218 202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,
219 220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,
220 238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255};
222 void HTMLParser(Text *tPtr, char *stream);
223 #define MAX_TOKEN_SIZE 255
224 #define MAX_TEXT_SIZE 1023
226 #define TOLOWER(x) (map_table[(int)x])
228 #define ISALNUM(x) ( ((x>='0') && (x<='9')) \
229 || ((x>='a') && (x<='z')) || ((x>='A') && x<='Z'))
231 #if DO_BLINK
232 #define CURSOR_BLINK_ON_DELAY 600
233 #define CURSOR_BLINK_OFF_DELAY 400
234 #endif
236 static char *default_bullet[] = {
237 "6 6 4 1",
238 " c None s None", ". c black",
239 "X c white", "o c #808080",
240 " ... ",
241 ".XX.. ",
242 ".XX..o",
243 ".....o",
244 " ...oo",
245 " ooo "};
247 static void
248 handleEvents(XEvent *event, void *data);
251 static int
252 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
254 unsigned int i=0;
256 for(i=0; i < tPtr->nMargins; i++) {
258 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
259 return i;
262 return -1;
267 static int
268 newMargin(Text *tPtr, WMRulerMargins *margins)
270 int n;
272 if (!margins) {
273 tPtr->margins[0].retainCount++;
274 return 0;
277 n = getMarginNumber(tPtr, margins);
279 if (n == -1) {
281 tPtr->margins = wrealloc(tPtr->margins,
282 (++tPtr->nMargins)*sizeof(WMRulerMargins));
284 n = tPtr->nMargins-1;
285 tPtr->margins[n].left = margins->left;
286 tPtr->margins[n].first = margins->first;
287 tPtr->margins[n].body = margins->body;
288 tPtr->margins[n].right = margins->right;
289 //for tabs.
290 tPtr->margins[n].retainCount = 1;
291 } else {
292 tPtr->margins[n].retainCount++;
295 return n;
298 static Bool
299 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
301 unsigned short i, w, lw, selected = False, extend = False;
302 myRect sel;
305 /* if selection rectangle completely encloses the section */
306 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
307 && (tb->sections[s]._y + tb->sections[s].h
308 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
309 sel.x = 0;
310 sel.w = tPtr->visible.w;
311 selected = extend = True;
313 /* or if it starts on a line and then goes further down */
314 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
315 && (tb->sections[s]._y + tb->sections[s].h
316 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
317 && (tb->sections[s]._y + tb->sections[s].h
318 >= tPtr->visible.y + tPtr->sel.y) ) {
319 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
320 sel.w = tPtr->visible.w;
321 selected = extend = True;
323 /* or if it begins before a line, but ends on it */
324 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
325 && (tb->sections[s]._y + tb->sections[s].h
326 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
327 && (tb->sections[s]._y
328 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
330 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
331 sel.w = tPtr->sel.x + tPtr->sel.w;
332 else
333 sel.w = tPtr->sel.x;
335 sel.x = 0;
336 selected = True;
338 /* or if the selection rectangle lies entirely within a line */
339 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
340 && (tPtr->sel.w >= 2)
341 && (tb->sections[s]._y + tb->sections[s].h
342 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
343 sel.x = tPtr->sel.x;
344 sel.w = tPtr->sel.w;
345 selected = True;
348 if (selected) {
349 selected = False;
351 /* if not within (modified) selection rectangle */
352 if ( tb->sections[s].x > sel.x + sel.w
353 || tb->sections[s].x + tb->sections[s].w < sel.x)
354 return False;
356 if (tb->graphic) {
357 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
358 && tb->sections[s].x >= sel.x) {
359 rect->width = tb->sections[s].w;
360 rect->x = tb->sections[s].x;
361 selected = True;
363 } else {
365 i = tb->sections[s].begin;
366 lw = 0;
368 if (0&& tb->sections[s].x >= sel.x) {
369 tb->s_begin = tb->sections[s].begin;
370 goto _selEnd;
373 while (++i <= tb->sections[s].end) {
375 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
376 lw += w;
378 if (lw + tb->sections[s].x >= sel.x
379 || i == tb->sections[s].end ) {
380 lw -= w;
381 i--;
382 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
383 break;
387 if (i > tb->sections[s].end) {
388 printf("WasSelected: (i > tb->sections[s].end) \n");
389 return False;
392 _selEnd: rect->x = tb->sections[s].x + lw;
393 lw = 0;
394 while(++i <= tb->sections[s].end) {
396 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
397 lw += w;
399 if (lw + rect->x >= sel.x + sel.w
400 || i == tb->sections[s].end ) {
402 if (i != tb->sections[s].end) {
403 lw -= w;
404 i--;
407 rect->width = lw;
408 if (tb->sections[s].last && sel.x + sel.w
409 >= tb->sections[s].x + tb->sections[s].w
410 && extend ) {
411 rect->width += (tPtr->visible.w - rect->x - lw);
414 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
415 selected = True;
416 break;
417 } } } }
419 if (selected) {
420 rect->y = tb->sections[s]._y - tPtr->vpos;
421 rect->height = tb->sections[s].h;
422 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
424 return selected;
428 static void
429 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color)
431 TextBlock *tb;
432 int isFont=False;
434 if((font && color) || (!font && !color))
435 return;
437 if(font && !color)
438 isFont = True;
441 tb = tPtr->firstTextBlock;
442 if (!tb || !tPtr->flags.ownsSelection)
443 return;
446 while (tb) {
447 if (tb->selected) {
449 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
451 if(isFont) {
452 if(!tb->graphic) {
453 WMReleaseFont(tb->d.font);
454 tb->d.font = WMRetainFont(font);
456 } else {
457 WMReleaseColor(tb->color);
458 tb->color = WMRetainColor(color);
461 } else if (tb->s_end <= tb->used) {
463 TextBlock *otb = tb;
464 int count=0;
465 TextBlock *ntb = (TextBlock *)
466 WMCreateTextBlockWithText(tPtr,
467 &(tb->text[tb->s_begin]),
468 (isFont?font:tb->d.font),
469 (isFont?tb->color:color),
470 False, (tb->s_end - tb->s_begin));
472 if (ntb) {
473 ntb->selected = True;
474 ntb->s_begin = 0;
475 ntb->s_end = ntb->used;
476 WMAppendTextBlock(tPtr, ntb);
477 count++;
480 #if 0
481 if (tb->used > tb->s_end) {
482 ntb = (TextBlock *)
483 WMCreateTextBlockWithText(tPtr,
484 &(tb->text[tb->s_end]),
485 (isFont?font:tb->d.font),
486 (isFont?tb->color:color),
487 False, tb->used - tb->s_end);
489 if (ntb) {
490 ntb->selected = True;
491 ntb->s_begin = 0;
492 ntb->s_end = ntb->used;
493 WMAppendTextBlock(tPtr, ntb);
494 count++;
497 #endif
499 if (count == 1)
500 tb = otb->next;
501 else if (count == 2)
502 tb = otb->next->next;
504 tb->used = tb->s_end = tb->s_begin;
508 tb = tb->next;
511 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
515 static void
516 removeSelection(Text *tPtr)
518 TextBlock *tb = NULL;
520 if (!(tb = tPtr->firstTextBlock))
521 return;
523 while (tb) {
524 if (tb->selected) {
526 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
527 tPtr->currentTextBlock = tb;
528 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
529 tb = tPtr->currentTextBlock;
530 if (tb)
531 tPtr->tpos = 0;
532 continue;
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]),
536 &(tb->text[tb->s_end]), tb->used - tb->s_end);
537 tb->used -= (tb->s_end - tb->s_begin);
538 tb->selected = False;
539 tPtr->tpos = tb->s_begin;
544 tb = tb->next;
549 static void
550 paintText(Text *tPtr)
552 TextBlock *tb = tPtr->firstTextBlock;
553 WMFont *font;
554 GC gc, greyGC;
555 char *text;
556 int len, y, c, s, done=False;
557 int prev_y=-23;
558 WMScreen *scr = tPtr->view->screen;
559 Display *dpy = tPtr->view->screen->display;
560 Window win = tPtr->view->window;
562 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
563 return;
565 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
566 0, 0, tPtr->visible.w, tPtr->visible.h);
568 tb = tPtr->firstTextBlock;
569 if (!tb)
570 goto _copy_area;
573 if (tPtr->flags.ownsSelection)
574 greyGC = WMColorGC(WMGrayColor(scr));
576 done = False;
578 while (!done && tb) {
580 if (tb->graphic) {
581 tb = tb->next;
582 continue;
585 tb->selected = False;
587 for(s=0; s<tb->nsections && !done; s++) {
589 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
590 done = True;
591 break;
594 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
595 continue;
597 if (tPtr->flags.monoFont) {
598 font = tPtr->dFont;
599 gc = tPtr->fgGC;
600 } else {
601 font = tb->d.font;
602 gc = WMColorGC(tb->color);
605 if (tPtr->flags.ownsSelection) {
606 XRectangle rect;
608 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
609 tb->selected = True;
610 XFillRectangle(dpy, tPtr->db, greyGC,
611 rect.x, rect.y, rect.width, rect.height);
615 prev_y = tb->sections[s]._y;
617 len = tb->sections[s].end - tb->sections[s].begin;
618 text = &(tb->text[tb->sections[s].begin]);
619 y = tb->sections[s].y - tPtr->vpos;
620 WMDrawString(scr, tPtr->db, gc, font,
621 tb->sections[s].x - tPtr->hpos, y, text, len);
623 if (tb->underlined) {
624 XDrawLine(dpy, tPtr->db, gc,
625 tb->sections[s].x - tPtr->hpos,
626 y + font->y + 1,
627 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
628 y + font->y + 1);
633 tb = (!done? tb->next : NULL);
637 c = WMGetBagItemCount(tPtr->gfxItems);
638 if (c > 0 && !tPtr->flags.monoFont) {
639 int j, h;
641 for(j=0; j<c; j++) {
642 tb = (TextBlock *) WMGetFromBag(tPtr->gfxItems, j);
643 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
644 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
646 if(tb->object) {
647 if ((W_VIEW(tb->d.widget))->flags.mapped) {
648 WMUnmapWidget(tb->d.widget);
651 } else {
652 if(tb->object) {
653 if (!(W_VIEW(tb->d.widget))->flags.mapped) {
654 if (!(W_VIEW(tb->d.widget))->flags.realized)
655 WMRealizeWidget(tb->d.widget);
656 WMMapWidget(tb->d.widget);
657 WMLowerWidget(tb->d.widget);
661 if (tPtr->flags.ownsSelection) {
662 XRectangle rect;
664 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
665 tb->selected = True;
666 XFillRectangle(dpy, tPtr->db, greyGC,
667 rect.x, rect.y, rect.width, rect.height);
671 if(tb->object) {
672 WMMoveWidget(tb->d.widget,
673 tb->sections[0].x - tPtr->hpos,
674 tb->sections[0].y - tPtr->vpos);
675 h = WMWidgetHeight(tb->d.widget) + 1;
677 } else {
678 WMDrawPixmap(tb->d.pixmap, tPtr->db,
679 tb->sections[0].x - tPtr->hpos,
680 tb->sections[0].y - tPtr->vpos);
681 h = tb->d.pixmap->height + 1;
685 if (tb->underlined) {
686 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
687 tb->sections[0].x,
688 tb->sections[0].y + h,
689 tb->sections[0].x + tb->sections[0].w,
690 tb->sections[0].y + h);
691 } } } }
694 _copy_area:
695 if (tPtr->flags.editable && tPtr->flags.cursorShown
696 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
697 int y = tPtr->cursor.y - tPtr->vpos;
698 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
699 tPtr->cursor.x, y,
700 tPtr->cursor.x, y + tPtr->cursor.h);
703 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC,
704 0, 0,
705 tPtr->visible.w, tPtr->visible.h,
706 tPtr->visible.x, tPtr->visible.y);
709 W_DrawRelief(scr, win, 0, 0,
710 tPtr->view->size.width, tPtr->view->size.height,
711 tPtr->flags.relief);
713 if (tPtr->ruler && tPtr->flags.rulerShown)
714 XDrawLine(dpy, win,
715 tPtr->fgGC, 2, 42,
716 tPtr->view->size.width-4, 42);
721 #if DO_BLINK
723 static void
724 blinkCursor(void *data)
726 Text *tPtr = (Text*)data;
728 if (tPtr->flags.cursorShown) {
729 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
730 blinkCursor, data);
731 } else {
732 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
733 blinkCursor, data);
735 paintText(tPtr);
736 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
738 #endif
740 static TextBlock *
741 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
743 if (!tb)
744 return NULL;
745 while (tb) {
746 if (!tb->graphic)
747 break;
748 tb = (dir? tb->next : tb->prior);
751 return tb;
755 static void
756 cursorToTextPosition(Text *tPtr, int x, int y)
758 TextBlock *tb = NULL;
759 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
760 char *text;
762 if(tPtr->flags.needsRefresh)
763 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
765 y += (tPtr->vpos - tPtr->visible.y);
766 if (y<0)
767 y = 0;
769 x -= (tPtr->visible.x - 2);
770 if (x<0)
771 x=0;
773 /* clicked is relative to document, not window... */
774 tPtr->clicked.x = x;
775 tPtr->clicked.y = y;
777 if (! (tb = tPtr->currentTextBlock)) {
778 if (! (tb = tPtr->firstTextBlock)) {
779 tPtr->tpos = 0;
780 tPtr->cursor.h = tPtr->dFont->height;
781 tPtr->cursor.y = 2;
782 tPtr->cursor.x = 2;
783 return;
787 /* first, which direction? Most likely, newly clicked
788 position will be close to previous */
789 dir = !(y <= tb->sections[0].y);
790 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
791 && (y >= tb->sections[0]._y ) ) {
792 /* if it's on the same line */
793 if(x < tb->sections[0].x)
794 dir = 0;
795 if(x >= tb->sections[0].x)
796 dir = 1;
799 tb = tPtr->firstTextBlock;
800 dir = 1;
802 if (tPtr->flags.monoFont && tb->graphic) {
803 tb = getFirstNonGraphicBlockFor(tb, 1);
804 if (!tb) {
805 tPtr->currentTextBlock =
806 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
807 tPtr->tpos = 0;
808 return;
812 s = (dir? 0 : tb->nsections-1);
813 if ( y >= tb->sections[s]._y
814 && y <= tb->sections[s]._y + tb->sections[s].h) {
815 goto _doneV;
818 /* get the first section of the TextBlock that lies about
819 the vertical click point */
820 done = False;
821 while (!done && tb) {
823 if (tPtr->flags.monoFont && tb->graphic) {
824 if(tb->next)
825 tb = tb->next;
826 continue;
829 s = (dir? 0 : tb->nsections-1);
830 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
832 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
833 ( y >= tb->sections[s]._y ) ) ) {
834 done = True;
835 } else {
836 dir? s++ : s--;
840 if (!done) {
841 if ( (dir? tb->next : tb->prior)) {
842 tb = (dir ? tb->next : tb->prior);
843 } else {
844 pos = tb->used;
845 break; //goto _doneH;
851 if (s<0 || s>=tb->nsections) {
852 s = (dir? tb->nsections-1 : 0);
855 _doneV:
856 /* we have the line, which TextBlock on that line is it? */
857 pos = 0;
858 if (tPtr->flags.monoFont && tb->graphic)
859 tb = getFirstNonGraphicBlockFor(tb, dir);
860 if (tb) {
861 if ((dir? tb->sections[s].x >= x : tb->sections[s].x < x))
862 goto _doneH;
864 #if 0
865 if(tb->blank) {
866 _w = 0;
867 printf("blank\n");
868 } else {
869 text = &(tb->text[tb->sections[s].begin]);
870 len = tb->sections[s].end - tb->sections[s].begin;
871 _w = WMWidthOfString(tb->d.font, text, len);
873 printf("here %d %d \n", tb->sections[s].x + _w, x);
874 if ((dir? tb->sections[s].x + _w < x : tb->sections[s].x + _w >= x)) {
875 pos = tb->sections[s].end;
876 tPtr->cursor.x = tb->sections[s].x + _w;
877 goto _doneH;
879 #endif
880 _y = tb->sections[s]._y;
883 while (tb) {
885 if (tPtr->flags.monoFont && tb->graphic) {
886 tb = (dir ? tb->next : tb->prior);
887 continue;
890 if (dir) {
891 if (tb->graphic) {
892 if(tb->object)
893 _w = WMWidgetWidth(tb->d.widget);
894 else
895 _w = tb->d.pixmap->width;
896 } else {
897 text = &(tb->text[tb->sections[s].begin]);
898 len = tb->sections[s].end - tb->sections[s].begin;
899 _w = WMWidthOfString(tb->d.font, text, len);
900 if (tb->sections[s].x + _w >= x)
901 break;
904 } else {
905 if (tb->sections[s].x <= x)
906 break;
909 if ((dir? tb->next : tb->prior)) {
910 TextBlock *nxt = (dir? tb->next : tb->prior);
911 if (tPtr->flags.monoFont && nxt->graphic) {
912 nxt = getFirstNonGraphicBlockFor(nxt, dir);
913 if (!nxt) {
914 pos = 0;
915 tPtr->cursor.x = tb->sections[s].x;
916 goto _doneH;
920 if (_y != nxt->sections[0]._y) {
921 /* this must be the last/first on this line. stop */
922 pos = (dir? tb->sections[s].end : 0);
923 tPtr->cursor.x = tb->sections[s].x;
924 if (!tb->blank) {
925 if (tb->graphic) {
926 if(tb->object)
927 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
928 else
929 tPtr->cursor.x += tb->d.pixmap->width;
930 } else if (pos > tb->sections[s].begin) {
931 tPtr->cursor.x +=
932 WMWidthOfString(tb->d.font,
933 &(tb->text[tb->sections[s].begin]),
934 pos - tb->sections[s].begin);
937 goto _doneH;
941 if ( (dir? tb->next : tb->prior)) {
942 tb = (dir ? tb->next : tb->prior);
943 } else {
944 done = True;
945 break;
948 if (tb)
949 s = (dir? 0 : tb->nsections-1);
952 /* we have said TextBlock, now where within it? */
953 if (tb && !tb->graphic) {
954 WMFont *f = tb->d.font;
955 len = tb->sections[s].end - tb->sections[s].begin;
956 text = &(tb->text[tb->sections[s].begin]);
958 _w = x - tb->sections[s].x;
959 pos = 0;
961 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
962 pos++;
964 tPtr->cursor.x = tb->sections[s].x +
965 (pos? WMWidthOfString(f, text, pos) : 0);
967 pos += tb->sections[s].begin;
968 _doneH:
969 tPtr->tpos = (pos<tb->used)? pos : tb->used;
972 tPtr->currentTextBlock = tb;
973 tPtr->cursor.h = tb->sections[s].h;
974 tPtr->cursor.y = tb->sections[s]._y;
976 if (!tb)
977 printf("will hang :-)\n");
981 static void
982 updateScrollers(Text *tPtr)
985 if (tPtr->flags.frozen)
986 return;
988 if (tPtr->vS) {
989 if (tPtr->docHeight < tPtr->visible.h) {
990 WMSetScrollerParameters(tPtr->vS, 0, 1);
991 tPtr->vpos = 0;
992 } else {
993 float hmax = (float)(tPtr->docHeight);
994 WMSetScrollerParameters(tPtr->vS,
995 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
996 (float)tPtr->visible.h/hmax);
998 } else tPtr->vpos = 0;
1000 if (tPtr->hS) {
1001 if (tPtr->docWidth < tPtr->visible.w) {
1002 WMSetScrollerParameters(tPtr->hS, 0, 1);
1003 tPtr->hpos = 0;
1004 } else {
1005 float wmax = (float)(tPtr->docWidth);
1006 WMSetScrollerParameters(tPtr->hS,
1007 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
1008 (float)tPtr->visible.w/wmax);
1010 } else tPtr->hpos = 0;
1013 static void
1014 scrollersCallBack(WMWidget *w, void *self)
1016 Text *tPtr = (Text *)self;
1017 Bool scroll = False;
1018 Bool dimple = False;
1019 int which;
1021 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1022 return;
1024 if (w == tPtr->vS) {
1025 int height;
1026 height = tPtr->visible.h;
1028 which = WMGetScrollerHitPart(tPtr->vS);
1029 switch(which) {
1030 case WSDecrementLine:
1031 if (tPtr->vpos > 0) {
1032 if (tPtr->vpos>16) tPtr->vpos-=16;
1033 else tPtr->vpos=0;
1034 scroll=True;
1035 }break;
1036 case WSIncrementLine: {
1037 int limit = tPtr->docHeight - height;
1038 if (tPtr->vpos < limit) {
1039 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1040 else tPtr->vpos=limit;
1041 scroll = True;
1042 }}break;
1043 case WSDecrementPage:
1044 tPtr->vpos -= height;
1046 if (tPtr->vpos < 0)
1047 tPtr->vpos = 0;
1048 dimple = True;
1049 scroll = True;
1050 printf("dimple needs to jump to mouse location ;-/\n");
1051 break;
1052 case WSIncrementPage:
1053 tPtr->vpos += height;
1054 if (tPtr->vpos > (tPtr->docHeight - height))
1055 tPtr->vpos = tPtr->docHeight - height;
1056 dimple = True;
1057 scroll = True;
1058 printf("dimple needs to jump to mouse location ;-/\n");
1059 break;
1062 case WSKnob:
1063 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1064 * (float)(tPtr->docHeight - height);
1065 scroll = True;
1066 break;
1068 case WSKnobSlot:
1069 case WSNoPart:
1070 printf("WSNoPart, WSKnobSlot\n");
1071 #if 0
1072 float hmax = (float)(tPtr->docHeight);
1073 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1074 (float)tPtr->visible.h/hmax;
1075 dimple =where mouse is.
1076 #endif
1077 break;
1079 scroll = (tPtr->vpos != tPtr->prevVpos);
1080 tPtr->prevVpos = tPtr->vpos;
1083 if (w == tPtr->hS) {
1084 int width = tPtr->visible.w;
1086 which = WMGetScrollerHitPart(tPtr->hS);
1087 switch(which) {
1088 case WSDecrementLine:
1089 if (tPtr->hpos > 0) {
1090 if (tPtr->hpos>16) tPtr->hpos-=16;
1091 else tPtr->hpos=0;
1092 scroll=True;
1093 }break;
1094 case WSIncrementLine: {
1095 int limit = tPtr->docWidth - width;
1096 if (tPtr->hpos < limit) {
1097 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1098 else tPtr->hpos=limit;
1099 scroll = True;
1100 }}break;
1101 case WSDecrementPage:
1102 tPtr->hpos -= width;
1104 if (tPtr->hpos < 0)
1105 tPtr->hpos = 0;
1106 dimple = True;
1107 scroll = True;
1108 printf("dimple needs to jump to mouse location ;-/\n");
1109 break;
1110 case WSIncrementPage:
1111 tPtr->hpos += width;
1112 if (tPtr->hpos > (tPtr->docWidth - width))
1113 tPtr->hpos = tPtr->docWidth - width;
1114 dimple = True;
1115 scroll = True;
1116 printf("dimple needs to jump to mouse location ;-/\n");
1117 break;
1120 case WSKnob:
1121 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1122 * (float)(tPtr->docWidth - width);
1123 scroll = True;
1124 break;
1126 case WSKnobSlot:
1127 case WSNoPart:
1128 printf("WSNoPart, WSKnobSlot\n");
1129 #if 0
1130 float wmax = (float)(tPtr->docWidth);
1131 ((float)tPtr->vpos)/(wmax - (float)tPtr->visible.w),
1132 (float)tPtr->visible.w/wmax;
1133 dimple =where mouse is.
1134 #endif
1135 break;
1137 scroll = (tPtr->hpos != tPtr->prevHpos);
1138 tPtr->prevHpos = tPtr->hpos;
1141 if (scroll) {
1143 if (0&&dimple) {
1144 if (tPtr->rulerShown)
1145 XClearArea(tPtr->view->screen->display, tPtr->view->window, 22, 47,
1146 tPtr->view->size.width-24, tPtr->view->size.height-49, True);
1147 else
1148 XClearArea(tPtr->view->screen->display, tPtr->view->window, 22, 2,
1149 tPtr->view->size.width-24, tPtr->view->size.height-4, True);
1152 if (dimple || which == WSDecrementLine || which == WSIncrementLine)
1153 updateScrollers(tPtr);
1154 paintText(tPtr);
1160 typedef struct {
1161 TextBlock *tb;
1162 unsigned short begin, end; /* what part of the text block */
1163 } myLineItems;
1166 static int
1167 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1169 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1170 WMFont *font;
1171 char *text;
1172 TextBlock *tb;
1173 TextBlock *tbsame=NULL;
1175 if(!items || nitems == 0)
1176 return 0;
1178 for(i=0; i<nitems; i++) {
1179 tb = items[i].tb;
1181 if (tb->graphic) {
1182 if (!tPtr->flags.monoFont) {
1183 if(tb->object) {
1184 WMWidget *wdt = tb->d.widget;
1185 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1186 if (tPtr->flags.alignment != WALeft)
1187 lw += WMWidgetWidth(wdt);
1188 } else {
1189 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1190 if (tPtr->flags.alignment != WALeft)
1191 lw += tb->d.pixmap->width;
1195 } else {
1196 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1197 max_d = WMAX(max_d, abs(font->height-font->y));
1198 line_height = WMAX(line_height, font->height + max_d);
1199 text = &(tb->text[items[i].begin]);
1200 len = items[i].end - items[i].begin;
1201 if (tPtr->flags.alignment != WALeft)
1202 lw += WMWidthOfString(font, text, len);
1206 if (tPtr->flags.alignment == WARight) {
1207 j = tPtr->visible.w - lw;
1208 } else if (tPtr->flags.alignment == WACenter) {
1209 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1212 for(i=0; i<nitems; i++) {
1213 tb = items[i].tb;
1215 if (tbsame == tb) { /*extend it, since it's on same line */
1216 tb->sections[tb->nsections-1].end = items[i].end;
1217 n = tb->nsections-1;
1218 } else {
1219 tb->sections = wrealloc(tb->sections,
1220 (++tb->nsections)*sizeof(Section));
1221 n = tb->nsections-1;
1222 tb->sections[n]._y = y + max_d;
1223 tb->sections[n].x = x+j;
1224 tb->sections[n].h = line_height;
1225 tb->sections[n].begin = items[i].begin;
1226 tb->sections[n].end = items[i].end;
1228 if (tb->graphic && tb->object) {
1229 tb->sections[n].x += tPtr->visible.x;
1230 tb->sections[n].y += tPtr->visible.y;
1234 tb->sections[n].last = (i+1 == nitems);
1236 if (tb->graphic) {
1237 if (!tPtr->flags.monoFont) {
1238 if(tb->object) {
1239 WMWidget *wdt = tb->d.widget;
1240 tb->sections[n].y = max_d + y
1241 + line_height - WMWidgetHeight(wdt);
1242 tb->sections[n].w = WMWidgetWidth(wdt);
1243 } else {
1244 tb->sections[n].y = y + max_d;
1245 tb->sections[n].w = tb->d.pixmap->width;
1247 x += tb->sections[n].w;
1249 } else {
1250 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1251 len = items[i].end - items[i].begin;
1252 text = &(tb->text[items[i].begin]);
1254 tb->sections[n].y = y+line_height-font->y;
1255 tb->sections[n].w =
1256 WMWidthOfString(font,
1257 &(tb->text[tb->sections[n].begin]),
1258 tb->sections[n].end - tb->sections[n].begin);
1260 x += WMWidthOfString(font, text, len);
1263 tbsame = tb;
1266 return line_height;
1271 static void
1272 output(char *ptr, int len)
1274 char s[len+1];
1275 memcpy(s, ptr, len);
1276 s[len] = 0;
1277 //printf(" s is [%s] (%d)\n", s, strlen(s));
1278 printf("[%s]\n", s);
1282 /* tb->text doesn't necessarily end in '\0' hmph! (strchr) */
1283 static inline char *
1284 mystrchr(char *s, char needle, unsigned short len)
1286 char *haystack = s;
1288 if (!haystack || len < 1)
1289 return NULL;
1291 while ( (int) (haystack - s) < len ) {
1292 if (*haystack == needle)
1293 return haystack;
1294 haystack++;
1296 return NULL;
1299 static int
1300 mystrcasecmp(const unsigned char *s1, const unsigned char *s2)
1302 if (!*s1 || !*s2)
1303 return 0;
1305 while (*s2 != '\0') {
1306 if (TOLOWER (*s1) != TOLOWER (*s2)) /* true if *s1 == 0 ! */
1307 return 0;
1308 s1++;
1309 s2++;
1311 return (*s1=='\0' || !ISALNUM(*s1))?1:0;
1316 static void
1317 layOutDocument(Text *tPtr)
1319 TextBlock *tb;
1320 myLineItems *items = NULL;
1321 unsigned int itemsSize=0, nitems=0;
1322 WMFont *font;
1323 unsigned int x, y=0, prev_y, lw = 0, width=0, bmargin;
1326 Bool lhc = !tPtr->flags.laidOut; /* line height changed? */
1328 char *start=NULL, *mark=NULL;
1329 unsigned int begin, end;
1331 if (tPtr->flags.frozen)
1332 return;
1334 if (!(tb = tPtr->firstTextBlock))
1335 return;
1337 tPtr->docWidth = tPtr->visible.w;
1338 x = 0;//tPtr->margins[tb->marginN].first;
1339 printf("x:%d\n", x);
1340 bmargin = tPtr->margins[tb->marginN].body;
1342 if (0&&tPtr->flags.laidOut) {
1343 tb = tPtr->currentTextBlock;
1344 if (tb->sections && tb->nsections>0)
1345 prev_y = tb->sections[tb->nsections-1]._y;
1346 y+=10;
1347 printf("1 prev_y %d \n", prev_y);
1349 /* search backwards for textblocks on same line */
1350 while (tb) {
1351 if (!tb->sections || tb->nsections<1) {
1352 tb = tPtr->firstTextBlock;
1353 break;
1355 if (tb->sections[tb->nsections-1]._y != prev_y) {
1356 tb = tb->next;
1357 break;
1359 // prev_y = tb->sections[tb->nsections-1]._y;
1360 tb = tb->prior;
1362 y = 0;//tb->sections[tb->nsections-1]._y;
1363 printf("2 prev_y %d \n\n", tb->sections[tb->nsections-1]._y);
1367 while (tb) {
1369 if (tb->sections && tb->nsections>0) {
1370 wfree(tb->sections);
1371 tb->sections = NULL;
1372 tb->nsections = 0;
1375 if (tb->first && tb != tPtr->firstTextBlock) {
1376 y += layOutLine(tPtr, items, nitems, x, y);
1377 x = 0*tPtr->margins[tb->marginN].first;
1378 bmargin = tPtr->margins[tb->marginN].body;
1379 nitems = 0;
1380 lw = 0;
1383 if (tb->graphic) {
1384 if (!tPtr->flags.monoFont) {
1385 if(tb->object)
1386 width = WMWidgetWidth(tb->d.widget);
1387 else
1388 width = tb->d.pixmap->width;
1390 if (width > tPtr->docWidth) { //tPtr->visible.w) {
1391 printf("rescale graphix to fit?\n");
1392 printf("%d %d\n", width, tPtr->visible.w);
1393 tPtr->docWidth = width;//tPtr->visible.w + (width-tPtr->visible.w);
1396 lw += width;
1397 if (lw >= tPtr->visible.w - x ) {
1398 y += layOutLine(tPtr, items, nitems, x, y);
1399 nitems = 0;
1400 x = 0*bmargin;
1401 lw = width;
1404 if(nitems + 1> itemsSize) {
1405 items = wrealloc(items,
1406 (++itemsSize)*sizeof(myLineItems));
1409 items[nitems].tb = tb;
1410 items[nitems].begin = 0;
1411 items[nitems].end = 0;
1412 nitems++;
1415 } else if ((start = tb->text)) {
1416 begin = end = 0;
1417 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1419 while (start) {
1420 mark = mystrchr(start, ' ', tb->used);
1421 if (mark) {
1422 end += (int)(mark-start)+1;
1423 start = mark+1;
1424 } else {
1425 end += strlen(start);
1426 start = mark;
1429 if (end > tb->used)
1430 end = tb->used;
1432 if (end-begin > 0) {
1434 width = WMWidthOfString(font,
1435 &tb->text[begin], end-begin);
1437 if (width > tPtr->visible.w) { /* break this tb up */
1438 char *t = &tb->text[begin];
1439 int l=end-begin, i=0;
1440 do {
1441 width = WMWidthOfString(font, t, ++i);
1442 } while (width < tPtr->visible.w && i < l);
1443 end = begin+i;
1444 if (start) // and since (nil)-4 = 0xfffffffd
1445 start -= l-i;
1448 lw += width;
1451 if (lw >= tPtr->visible.w - x) {
1452 y += layOutLine(tPtr, items, nitems, x, y);
1453 lw = width;
1454 x = bmargin;
1455 nitems = 0;
1458 if(nitems + 1 > itemsSize) {
1459 items = wrealloc(items,
1460 (++itemsSize)*sizeof(myLineItems));
1463 items[nitems].tb = tb;
1464 items[nitems].begin = begin;
1465 items[nitems].end = end;
1466 nitems++;
1468 begin = end;
1471 tb = tb->next;
1475 if (nitems > 0)
1476 y += layOutLine(tPtr, items, nitems, x, y);
1477 if (lhc) {
1478 tPtr->docHeight = y+10;
1479 updateScrollers(tPtr);
1482 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1483 XEvent event;
1485 tPtr->flags.horizOnDemand = True;
1486 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1487 event.type = Expose;
1488 handleEvents(&event, (void *)tPtr);
1490 } else if(tPtr->docWidth <= tPtr->visible.w
1491 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1492 tPtr->flags.horizOnDemand = False;
1493 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1495 tPtr->flags.laidOut = True;
1498 if(items && itemsSize > 0)
1499 wfree(items);
1503 static void
1504 textDidResize(W_ViewDelegate *self, WMView *view)
1506 Text *tPtr = (Text *)view->self;
1507 unsigned short w = tPtr->view->size.width;
1508 unsigned short h = tPtr->view->size.height;
1509 unsigned short rh = 0, vw = 0;
1511 if (tPtr->ruler && tPtr->flags.rulerShown) {
1512 WMMoveWidget(tPtr->ruler, 2, 2);
1513 WMResizeWidget(tPtr->ruler, w - 4, 40);
1514 rh = 40;
1517 if (tPtr->vS) {
1518 WMMoveWidget(tPtr->vS, 1, rh + 1);
1519 WMResizeWidget(tPtr->vS, 20, h - rh - 2);
1520 vw = 20;
1521 WMSetRulerOffset(tPtr->ruler,22);
1522 } else WMSetRulerOffset(tPtr->ruler, 2);
1524 if (tPtr->hS) {
1525 if (tPtr->vS) {
1526 WMMoveWidget(tPtr->hS, vw, h - 21);
1527 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1528 } else {
1529 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1530 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1534 tPtr->visible.x = (tPtr->vS)?24:4;
1535 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1536 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1537 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1538 tPtr->visible.h -= (tPtr->hS)?20:0;
1539 tPtr->margins[0].right = tPtr->visible.w;
1541 if (tPtr->view->flags.realized) {
1543 if (tPtr->db) {
1544 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1545 tPtr->db = (Pixmap) NULL;
1548 if (tPtr->visible.w < 40)
1549 tPtr->visible.w = 40;
1550 if (tPtr->visible.h < 20)
1551 tPtr->visible.h = 20;
1553 //if (size change or !db
1554 if(!tPtr->db) {
1555 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1556 tPtr->view->window, tPtr->visible.w,
1557 tPtr->visible.h, tPtr->view->screen->depth);
1561 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1564 W_ViewDelegate _TextViewDelegate =
1566 NULL,
1567 NULL,
1568 textDidResize,
1569 NULL,
1572 /* nice, divisble-by-16 blocks */
1573 static inline unsigned short
1574 reqBlockSize(unsigned short requested)
1576 return requested + 16 - (requested%16);
1580 static void
1581 clearText(Text *tPtr)
1583 if (!tPtr->firstTextBlock)
1584 return;
1586 while (tPtr->currentTextBlock)
1587 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1589 tPtr->firstTextBlock = NULL;
1590 tPtr->currentTextBlock = NULL;
1591 tPtr->lastTextBlock = NULL;
1594 static void
1595 deleteTextInteractively(Text *tPtr, KeySym ksym)
1597 TextBlock *tb = tPtr->currentTextBlock;
1598 Bool back = (Bool) (ksym == XK_BackSpace);
1599 Bool done = 1;
1601 if (!tPtr->flags.editable || tPtr->flags.buttonHeld) {
1602 XBell(tPtr->view->screen->display, 0);
1603 return;
1606 if (!tb)
1607 return;
1609 tPtr->flags.needsRefresh = True;
1611 if (tPtr->flags.ownsSelection) {
1612 removeSelection(tPtr);
1613 return;
1616 if (back && tPtr->tpos < 1) {
1617 if (tb->prior) {
1618 tb = tb->prior;
1619 tb->first = False;
1620 tPtr->tpos = tb->used;
1621 tPtr->currentTextBlock = tb;
1622 done = 1;
1626 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1627 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1628 if (back)
1629 tPtr->tpos--;
1630 memmove(&(tb->text[tPtr->tpos]),
1631 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1632 tb->used--;
1633 done = 0;
1636 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1637 || tb->graphic) {
1639 TextBlock *sibling = (back? tb->prior : tb->next);
1641 if(tb->used == 0 || tb->graphic)
1642 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1644 if (sibling) {
1645 tPtr->currentTextBlock = sibling;
1646 tPtr->tpos = (back? sibling->used : 0);
1650 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1654 static void
1655 insertTextInteractively(Text *tPtr, char *text, int len)
1657 TextBlock *tb;
1658 char *newline = NULL;
1660 if (!tPtr->flags.editable || tPtr->flags.buttonHeld) {
1661 XBell(tPtr->view->screen->display, 0);
1662 return;
1665 #if 0
1666 if(*text == 'c') {
1667 WMColor *color = WMCreateNamedColor(W_VIEW_SCREEN(tPtr->view),
1668 "Blue", True);
1669 WMSetTextSelectionColor(tPtr, color);
1670 return;
1672 #endif
1674 if (len < 1 || !text)
1675 return;
1678 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1679 return;
1681 if (tPtr->flags.ownsSelection)
1682 removeSelection(tPtr);
1684 tPtr->flags.needsRefresh = True;
1686 if (tPtr->flags.ignoreNewLine) {
1687 int i;
1688 for(i=0; i<len; i++) {
1689 if (text[i] == '\n')
1690 text[i] = ' ';
1694 tb = tPtr->currentTextBlock;
1695 if (!tb || tb->graphic) {
1696 text[len] = 0;
1697 WMAppendTextStream(tPtr, text);
1698 if (tPtr->currentTextBlock) {
1699 tPtr->tpos = tPtr->currentTextBlock->used;
1701 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1702 return;
1705 if ((newline = strchr(text, '\n'))) {
1706 int nlen = (int)(newline-text);
1707 int s = tb->used - tPtr->tpos;
1708 char save[s];
1710 if (!tb->blank && nlen>0) {
1711 if (s > 0) {
1712 memcpy(save, &tb->text[tPtr->tpos], s);
1713 tb->used -= (tb->used - tPtr->tpos);
1715 text[nlen] = 0;
1716 insertTextInteractively(tPtr, text, nlen);
1717 newline++;
1718 WMAppendTextStream(tPtr, newline);
1719 if (s>0)
1720 insertTextInteractively(tPtr, save, s);
1722 } else {
1723 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1724 && !tb->graphic && tb->text) {
1726 void *ntb = WMCreateTextBlockWithText(
1727 tPtr, &tb->text[tPtr->tpos],
1728 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1729 tb->used = tPtr->tpos;
1730 WMAppendTextBlock(tPtr, ntb);
1731 tPtr->tpos = 0;
1732 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
1733 void *ntb = WMCreateTextBlockWithText(tPtr,
1734 NULL, tb->d.font, tb->color, True, 0);
1736 if (tPtr->tpos>0)
1737 WMAppendTextBlock(tPtr, ntb);
1738 else
1739 WMPrependTextBlock(tPtr, ntb);
1740 tPtr->tpos = 1;
1744 } else {
1746 if (tb->used + len >= tb->allocated) {
1747 tb->allocated = reqBlockSize(tb->used+len);
1748 tb->text = wrealloc(tb->text, tb->allocated);
1751 if (tb->blank) {
1752 memcpy(tb->text, text, len);
1753 tb->used = len;
1754 tPtr->tpos = len;
1755 tb->blank = False;
1756 } else {
1757 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
1758 tb->used-tPtr->tpos+1);
1759 memmove(&tb->text[tPtr->tpos], text, len);
1760 tb->used += len;
1761 tPtr->tpos += len;
1766 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1770 static void
1771 selectRegion(Text *tPtr, int x, int y)
1774 if (x < 0 || y < 0)
1775 return;
1777 y += (tPtr->flags.rulerShown? 40: 0);
1778 y += tPtr->vpos;
1779 if (y>10)
1780 y -= 10; /* the original offset */
1782 x -= tPtr->visible.x-2;
1783 if (x<0)
1784 x=0;
1786 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
1787 tPtr->sel.w = abs(tPtr->clicked.x - x);
1788 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
1789 tPtr->sel.h = abs(tPtr->clicked.y - y);
1791 tPtr->flags.ownsSelection = True;
1792 paintText(tPtr);
1796 static void
1797 releaseSelection(Text *tPtr)
1799 TextBlock *tb = tPtr->firstTextBlock;
1801 while(tb) {
1802 tb->selected = False;
1803 tb = tb->next;
1805 tPtr->flags.ownsSelection = False;
1806 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
1807 CurrentTime);
1809 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1813 WMData*
1814 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
1815 Atom *type)
1817 Text *tPtr = view->self;
1818 Display *dpy = tPtr->view->screen->display;
1819 Atom _TARGETS;
1820 Atom TEXT = XInternAtom(dpy, "TEXT", False);
1821 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
1822 WMData *data = NULL;
1825 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
1826 char *text = WMGetTextSelected(tPtr);
1828 if (text) {
1829 printf("got text [%s]\n", text);
1830 data = WMCreateDataWithBytes(text, strlen(text));
1831 WMSetDataFormat(data, 8);
1833 *type = target;
1834 return data;
1835 } else printf("didn't get it\n");
1837 _TARGETS = XInternAtom(dpy, "TARGETS", False);
1838 if (target == _TARGETS) {
1839 Atom *ptr;
1841 ptr = wmalloc(4 * sizeof(Atom));
1842 ptr[0] = _TARGETS;
1843 ptr[1] = XA_STRING;
1844 ptr[2] = TEXT;
1845 ptr[3] = COMPOUND_TEXT;
1847 data = WMCreateDataWithBytes(ptr, 4*4);
1848 WMSetDataFormat(data, 32);
1850 *type = target;
1851 return data;
1854 return NULL;
1857 static void
1858 lostHandler(WMView *view, Atom selection, void *cdata)
1860 releaseSelection((WMText *)view->self);
1863 static WMSelectionProcs selectionHandler = {
1864 requestHandler, lostHandler, NULL
1868 static void
1869 ownershipObserver(void *observerData, WMNotification *notification)
1871 // if (observerData != WMGetNotificationClientData(notification))
1872 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
1873 printf("lost ownership\n");
1877 static void
1878 fontChanged(void *observerData, WMNotification *notification)
1880 WMText *tPtr = (WMText *) observerData;
1881 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
1882 printf("fontChanged\n");
1884 if(!tPtr || !font)
1885 return;
1887 if (tPtr->flags.ownsSelection)
1888 WMSetTextSelectionFont(tPtr, font);
1891 static void
1892 handleTextKeyPress(Text *tPtr, XEvent *event)
1894 char buffer[2];
1895 KeySym ksym;
1896 int control_pressed = False;
1897 // int h=1;
1899 if (((XKeyEvent *) event)->state & ControlMask)
1900 control_pressed = True;
1901 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
1903 switch(ksym) {
1905 case XK_Right:
1906 WMScrollText(tPtr, -14);
1907 case XK_Left: {
1908 TextBlock *tb = tPtr->currentTextBlock;
1909 int x = tPtr->cursor.x + tPtr->visible.x;
1910 int y = tPtr->visible.y + tPtr->cursor.y + tPtr->cursor.h;
1911 int w, pos;
1913 #if 0
1914 if(!tb)
1915 break;
1916 if(tb->graphic) {
1917 if(tb->object) {
1918 w = WMWidgetWidth(tb->d.widget);
1919 } else {
1920 w = tb->d.pixmap->width;
1922 } else {
1923 pos = tPtr->tpos;
1924 w = WMWidthOfString(tb->d.font, &tb->text[pos], 1);
1927 cursorToTextPosition(tPtr, w + tPtr->cursor.x + tPtr->visible.x,
1928 3 + tPtr->visible.y + tPtr->cursor.y
1929 + tPtr->cursor.h - tPtr->vpos);
1930 if(x == tPtr->cursor.x + tPtr->visible.x) {
1931 printf("same %d %d\n", x, tPtr->cursor.x + tPtr->visible.x);
1932 cursorToTextPosition(tPtr, tPtr->visible.x,
1933 3 + tPtr->visible.y + tPtr->cursor.y + tPtr->cursor.h);
1935 paintText(tPtr);
1936 #endif
1938 break;
1940 case XK_Down:
1941 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
1942 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
1943 paintText(tPtr);
1944 break;
1946 case XK_Up:
1947 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
1948 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
1949 paintText(tPtr);
1950 break;
1952 case XK_BackSpace:
1953 case XK_Delete:
1954 case XK_KP_Delete:
1955 deleteTextInteractively(tPtr, ksym);
1956 break;
1958 case XK_Control_R :
1959 case XK_Control_L :
1960 control_pressed = True;
1961 break;
1963 case XK_Return:
1964 buffer[0] = '\n';
1965 default:
1966 if (buffer[0] != 0 && !control_pressed) {
1967 insertTextInteractively(tPtr, buffer, 1);
1969 } else if (control_pressed && ksym==XK_r) {
1970 Bool i = !tPtr->flags.rulerShown;
1971 WMShowTextRuler(tPtr, i);
1972 tPtr->flags.rulerShown = i;
1974 else if (control_pressed && buffer[0] == '\a')
1975 XBell(tPtr->view->screen->display, 0);
1978 if (!control_pressed && tPtr->flags.ownsSelection)
1979 releaseSelection(tPtr);
1982 static void
1983 handleWidgetPress(XEvent *event, void *data)
1985 TextBlock *tb = (TextBlock *)data;
1986 Text *tPtr;
1987 WMWidget *w;
1989 if (!tb)
1990 return;
1991 /* this little bit of nastiness here saves a boatload of trouble */
1992 w = (WMWidget *)(((W_VIEW(tb->d.widget))->parent)->self);
1993 if (W_CLASS(w) != WC_Text)
1994 return;
1995 tPtr = (Text*)w;
1996 tPtr->currentTextBlock = getFirstNonGraphicBlockFor(tb, 1);
1997 if (!tPtr->currentTextBlock)
1998 tPtr->currentTextBlock = tb;
1999 tPtr->tpos = 0;
2000 output(tPtr->currentTextBlock->text, tPtr->currentTextBlock->used);
2001 //if (!tPtr->flags.focused) {
2002 // WMSetFocusToWidget(tPtr);
2003 // tPtr->flags.focused = True;
2004 //}
2008 static void
2009 handleActionEvents(XEvent *event, void *data)
2011 Text *tPtr = (Text *)data;
2012 Display *dpy = event->xany.display;
2013 KeySym ksym;
2016 switch (event->type) {
2017 case KeyPress:
2018 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2019 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2020 tPtr->flags.extendSelection = True;
2021 return;
2024 if (tPtr->flags.focused) {
2025 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2026 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2027 GrabModeAsync, GrabModeAsync, None,
2028 tPtr->view->screen->invisibleCursor, CurrentTime);
2029 tPtr->flags.pointerGrabbed = True;
2030 handleTextKeyPress(tPtr, event);
2032 } break;
2034 case KeyRelease:
2035 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2036 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2037 tPtr->flags.extendSelection = False;
2038 return;
2039 //end modify flag so selection can be extended
2041 break;
2044 case MotionNotify:
2046 if (tPtr->flags.pointerGrabbed) {
2047 tPtr->flags.pointerGrabbed = False;
2048 XUngrabPointer(dpy, CurrentTime);
2051 if(tPtr->flags.waitingForSelection)
2052 break;
2054 if ((event->xmotion.state & Button1Mask)) {
2055 if (!tPtr->flags.ownsSelection) {
2056 WMCreateSelectionHandler(tPtr->view,
2057 XA_PRIMARY, event->xbutton.time,
2058 &selectionHandler, NULL);
2059 tPtr->flags.ownsSelection = True;
2061 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2063 break;
2066 case ButtonPress:
2068 tPtr->flags.buttonHeld = True;
2070 if (tPtr->flags.pointerGrabbed) {
2071 tPtr->flags.pointerGrabbed = False;
2072 XUngrabPointer(dpy, CurrentTime);
2073 break;
2076 if (tPtr->flags.waitingForSelection)
2077 break;
2079 if (tPtr->flags.extendSelection) {
2080 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2081 return;
2085 if (event->xbutton.button == Button1) {
2087 if (!tPtr->flags.focused) {
2088 WMSetFocusToWidget(tPtr);
2089 tPtr->flags.focused = True;
2092 if (tPtr->flags.ownsSelection)
2093 releaseSelection(tPtr);
2094 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2095 paintText(tPtr);
2098 if (event->xbutton.button
2099 == WINGsConfiguration.mouseWheelDown) {
2100 WMScrollText(tPtr, -16);
2101 break;
2104 if (event->xbutton.button
2105 == WINGsConfiguration.mouseWheelUp) {
2106 WMScrollText(tPtr, 16);
2107 break;
2110 if (event->xbutton.button == Button2) {
2111 char *text = NULL;
2112 int n;
2114 if (!tPtr->flags.editable) {
2115 XBell(dpy, 0);
2116 break;
2119 #if 0
2120 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2121 event->xbutton.time, pasteText, NULL)) {
2122 #endif
2125 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2127 if (text) {
2128 text[n] = 0;
2130 if (tPtr->parser)
2131 (tPtr->parser) (tPtr, (void *) text);
2132 else
2133 insertTextInteractively(tPtr, text, n);
2135 XFree(text);
2136 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2137 (void*)WMInsertTextEvent);
2139 } else {
2140 tPtr->flags.waitingForSelection = True;
2143 break;
2147 case ButtonRelease:
2149 tPtr->flags.buttonHeld = False;
2151 if (tPtr->flags.pointerGrabbed) {
2152 tPtr->flags.pointerGrabbed = False;
2153 XUngrabPointer(dpy, CurrentTime);
2154 break;
2157 if (tPtr->flags.waitingForSelection)
2158 break;
2164 static void
2165 handleEvents(XEvent *event, void *data)
2167 Text *tPtr = (Text *)data;
2169 switch(event->type) {
2170 case Expose:
2172 if (event->xexpose.count!=0)
2173 break;
2175 if(tPtr->hS) {
2176 if (!(W_VIEW(tPtr->hS))->flags.realized)
2177 WMRealizeWidget(tPtr->hS);
2178 if (!((W_VIEW(tPtr->hS))->flags.mapped))
2179 WMMapWidget(tPtr->hS);
2182 if(tPtr->vS) {
2183 if (!(W_VIEW(tPtr->vS))->flags.realized)
2184 WMRealizeWidget(tPtr->vS);
2185 if (!((W_VIEW(tPtr->vS))->flags.mapped))
2186 WMMapWidget(tPtr->vS);
2189 if(tPtr->ruler) {
2190 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2191 WMRealizeWidget(tPtr->ruler);
2193 if (!((W_VIEW(tPtr->ruler))->flags.mapped)
2194 && tPtr->flags.rulerShown)
2195 WMMapWidget(tPtr->ruler);
2198 if(!tPtr->db)
2199 textDidResize(tPtr->view->delegate, tPtr->view);
2201 paintText(tPtr);
2202 break;
2204 case FocusIn:
2205 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2206 != tPtr->view)
2207 return;
2208 tPtr->flags.focused = True;
2209 #if DO_BLINK
2210 if (tPtr->flags.editable && !tPtr->timerID) {
2211 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2212 blinkCursor, tPtr);
2214 #endif
2216 break;
2218 case FocusOut:
2219 tPtr->flags.focused = False;
2220 paintText(tPtr);
2221 #if DO_BLINK
2222 if (tPtr->timerID) {
2223 WMDeleteTimerHandler(tPtr->timerID);
2224 tPtr->timerID = NULL;
2226 #endif
2227 break;
2230 case DestroyNotify:
2231 clearText(tPtr);
2232 if(tPtr->hS)
2233 WMDestroyWidget(tPtr->hS);
2234 if(tPtr->vS)
2235 WMDestroyWidget(tPtr->vS);
2236 if(tPtr->ruler)
2237 WMDestroyWidget(tPtr->ruler);
2238 if(tPtr->db)
2239 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2240 if(tPtr->gfxItems)
2241 WMFreeBag(tPtr->gfxItems);
2242 #if DO_BLINK
2243 if (tPtr->timerID)
2244 WMDeleteTimerHandler(tPtr->timerID);
2245 #endif
2246 WMReleaseFont(tPtr->dFont);
2247 WMReleaseColor(tPtr->dColor);
2248 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2249 WMRemoveNotificationObserver(tPtr);
2251 wfree(tPtr);
2253 break;
2259 static void
2260 insertText(WMText *tPtr, char *text)
2262 void *tb = NULL;
2263 char *start, *mark;
2265 if (!text) {
2266 clearText(tPtr);
2267 return;
2270 start = text;
2271 while (start) {
2272 mark = strchr(start, '\n');
2273 if (mark) {
2274 tb = WMCreateTextBlockWithText(tPtr,
2275 start, tPtr->dFont,
2276 tPtr->dColor, True, (int)(mark-start));
2277 start = mark+1;
2278 } else {
2279 if (start && strlen(start)) {
2280 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2281 tPtr->dColor, False, strlen(start));
2282 } else tb = NULL;
2283 start = mark;
2286 if (tPtr->flags.prepend)
2287 WMPrependTextBlock(tPtr, tb);
2288 else
2289 WMAppendTextBlock(tPtr, tb);
2291 return;
2296 static void
2297 rulerMoveCallBack(WMWidget *w, void *self)
2299 Text *tPtr = (Text *)self;
2300 if (!tPtr)
2301 return;
2302 if (W_CLASS(tPtr) != WC_Text)
2303 return;
2305 paintText(tPtr);
2309 static void
2310 rulerReleaseCallBack(WMWidget *w, void *self)
2312 Text *tPtr = (Text *)self;
2313 if (!tPtr)
2314 return;
2315 if (W_CLASS(tPtr) != WC_Text)
2316 return;
2318 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
2319 return;
2323 #if 0
2324 static unsigned
2325 draggingEntered(WMView *self, WMDraggingInfo *info)
2327 printf("draggingEntered\n");
2328 return WDOperationCopy;
2332 static unsigned
2333 draggingUpdated(WMView *self, WMDraggingInfo *info)
2335 printf("draggingUpdated\n");
2336 return WDOperationCopy;
2340 static void
2341 draggingExited(WMView *self, WMDraggingInfo *info)
2343 printf("draggingExited\n");
2346 static void
2347 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2349 printf("draggingExited\n");
2350 return;//"bll"; //"application/X-color";
2354 static Bool
2355 performDragOperation(WMView *self, WMDraggingInfo *info) //, WMData *data)
2357 char *colorName = "Blue";// (char*)WMDataBytes(data);
2358 WMColor *color;
2359 WMText *tPtr = (WMText *)self->self;
2361 if (!tPtr)
2362 return False;
2364 if (tPtr->flags.monoFont)
2365 return False;
2367 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2368 printf("color [%s] %p\n", colorName, color);
2369 if(color) {
2370 WMSetTextSelectionColor(tPtr, color);
2371 WMReleaseColor(color);
2374 return True;
2377 static void
2378 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2380 printf("concludeDragOperation\n");
2384 static WMDragDestinationProcs _DragDestinationProcs = {
2385 draggingEntered,
2386 draggingUpdated,
2387 draggingExited,
2388 prepareForDragOperation,
2389 performDragOperation,
2390 concludeDragOperation
2392 #endif
2394 static void
2395 releaseBagData(void *data)
2397 if(data)
2398 wfree(data);
2402 char *
2403 getStream(WMText *tPtr, int sel)
2405 TextBlock *tb = NULL;
2406 char *text = NULL;
2407 unsigned long where = 0;
2409 if (!tPtr)
2410 return NULL;
2412 if (!(tb = tPtr->firstTextBlock))
2413 return NULL;
2415 /* this might be tricky to get right... not yet implemented */
2416 if (tPtr->writer) {
2417 (tPtr->writer) (tPtr, (void *) text);
2418 return text;
2421 tb = tPtr->firstTextBlock;
2422 while (tb) {
2424 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2427 if (!sel || (tb->graphic && tb->selected)) {
2428 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)) {
2429 text = wrealloc(text, 1);
2430 text[where++] = '\n';
2433 if(tb->graphic) {
2434 text = wrealloc(text, 2);
2435 text[where++] = 0xFA;
2436 text[where++] = tb->used;
2439 text = wrealloc(text, tb->used);
2440 memcpy(&text[where], tb->text, tb->used);
2441 where += tb->used;
2443 } else if (sel && tb->selected) {
2445 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)) {
2446 text = wrealloc(text, 1);
2447 text[where++] = '\n';
2450 text = wrealloc(text, (tb->s_end - tb->s_begin));
2451 memcpy(&text[where], &tb->text[tb->s_begin],
2452 tb->s_end - tb->s_begin);
2453 where += tb->s_end - tb->s_begin;
2457 tb = tb->next;
2460 text = wrealloc(text, 1); /* +1 for the end of string, let's be nice */
2461 text[where] = 0;
2462 return text;
2467 WMBag *
2468 getStreamIntoBag(WMText *tPtr, int sel)
2470 char *stream, *start = NULL, *fa = NULL;
2471 WMBag *bag;
2472 WMData *data;
2474 if (!tPtr)
2475 return NULL;
2478 stream = getStream(tPtr, sel);
2479 if(!stream)
2480 return NULL;
2482 bag = WMCreateBagWithDestructor(4, releaseBagData);
2484 start = stream;
2485 while (start) {
2486 fa = strchr(start, 0xFA);
2487 if (fa) {
2488 unsigned char len = *(fa+1);
2490 if(start != fa) {
2491 data = WMCreateDataWithBytes((void *)start, (int)(fa - start));
2492 WMSetDataFormat(data, 8);
2493 WMPutInBag(bag, (void *) data);
2496 data = WMCreateDataWithBytes((void *)(fa+2), len);
2497 WMSetDataFormat(data, 32);
2498 WMPutInBag(bag, (void *) data);
2499 start = fa + len + 2;
2501 } else {
2502 if (start && strlen(start)) {
2503 data = WMCreateDataWithBytes((void *)start, strlen(start));
2504 WMSetDataFormat(data, 8);
2505 WMPutInBag(bag, (void *) data);
2507 start = fa;
2511 wfree(stream);
2512 return bag;
2516 WMText *
2517 WMCreateText(WMWidget *parent)
2519 Text *tPtr = wmalloc(sizeof(Text));
2520 if (!tPtr) {
2521 printf("could not create text widget\n");
2522 return NULL;
2525 #if 0
2526 printf("sizeof:\n");
2527 printf(" TextBlock %d\n", sizeof(TextBlock));
2528 printf(" Section %d\n", sizeof(Section));
2529 printf(" WMRulerMargins %d\n", sizeof(WMRulerMargins));
2530 printf(" char * %d\n", sizeof(char *));
2531 printf(" void * %d\n", sizeof(void *));
2532 printf(" short %d\n", sizeof(short));
2533 printf(" Text %d\n", sizeof(Text));
2534 #endif
2536 memset(tPtr, 0, sizeof(Text));
2537 tPtr->widgetClass = WC_Text;
2538 tPtr->view = W_CreateView(W_VIEW(parent));
2539 if (!tPtr->view) {
2540 perror("could not create text's view\n");
2541 free(tPtr);
2542 return NULL;
2544 tPtr->view->self = tPtr;
2545 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2546 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2547 W_ResizeView(tPtr->view, 250, 200);
2548 tPtr->bgGC = WMColorGC(tPtr->view->screen->white);
2549 tPtr->fgGC = WMColorGC(tPtr->view->screen->black);
2550 W_SetViewBackgroundColor(tPtr->view, tPtr->view->screen->white);
2552 tPtr->ruler = NULL;
2553 tPtr->vS = NULL;
2554 tPtr->hS = NULL;
2556 tPtr->dFont = WMRetainFont(tPtr->view->screen->normalFont);
2558 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2560 tPtr->view->delegate = &_TextViewDelegate;
2562 #if DO_BLINK
2563 tPtr->timerID = NULL;
2564 #endif
2566 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2567 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2568 handleEvents, tPtr);
2570 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2571 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2572 handleActionEvents, tPtr);
2574 WMAddNotificationObserver(ownershipObserver, tPtr,
2575 "_lostOwnership", tPtr);
2577 #if 0
2578 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2580 char *types[2] = {"application/X-color", NULL};
2581 WMRegisterViewForDraggedTypes(tPtr->view, types);
2583 #endif
2585 WMAddNotificationObserver(fontChanged, tPtr,
2586 "WMFontPanelDidChangeNotification", tPtr);
2588 tPtr->firstTextBlock = NULL;
2589 tPtr->lastTextBlock = NULL;
2590 tPtr->currentTextBlock = NULL;
2591 tPtr->tpos = 0;
2593 tPtr->gfxItems = WMCreateBag(4);
2595 tPtr->parser = NULL;
2596 tPtr->writer = NULL;
2598 tPtr->sel.x = tPtr->sel.y = 2;
2599 tPtr->sel.w = tPtr->sel.h = 0;
2601 tPtr->clicked.x = tPtr->clicked.y = 2;
2603 tPtr->visible.x = tPtr->visible.y = 2;
2604 tPtr->visible.h = tPtr->view->size.height;
2605 tPtr->visible.w = tPtr->view->size.width - 4;
2607 tPtr->cursor.x = -23;
2609 tPtr->docWidth = 0;
2610 tPtr->docHeight = 0;
2611 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2612 default_bullet);
2613 tPtr->db = (Pixmap) NULL;
2615 tPtr->margins = WMGetRulerMargins(NULL);
2616 tPtr->margins->right = tPtr->visible.w;
2617 tPtr->nMargins = 1;
2619 tPtr->flags.rulerShown = False;
2620 tPtr->flags.monoFont = False;
2621 tPtr->flags.focused = False;
2622 tPtr->flags.editable = True;
2623 tPtr->flags.ownsSelection = False;
2624 tPtr->flags.pointerGrabbed = False;
2625 tPtr->flags.buttonHeld = False;
2626 tPtr->flags.extendSelection = False;
2627 tPtr->flags.frozen = False;
2628 tPtr->flags.cursorShown = True;
2629 tPtr->flags.clickPos = 1;
2630 tPtr->flags.horizOnDemand = False;
2631 tPtr->flags.needsRefresh = False;
2632 tPtr->flags.ignoreNewLine = False;
2633 tPtr->flags.laidOut = False;
2634 tPtr->flags.waitingForSelection = False;
2635 tPtr->flags.prepend = False;
2636 tPtr->flags.parsingHTML = False;
2637 tPtr->flags.relief = WRSunken;
2638 tPtr->flags.alignment = WALeft;
2640 return tPtr;
2643 void
2644 WMPrependTextStream(WMText *tPtr, char *text)
2646 CHECK_CLASS(tPtr, WC_Text);
2648 if(!text)
2649 releaseSelection(tPtr);
2651 tPtr->flags.prepend = True;
2652 if (text && tPtr->parser)
2653 (tPtr->parser) (tPtr, (void *) text);
2654 else
2655 insertText(tPtr, text);
2657 tPtr->flags.needsRefresh = True;
2661 void
2662 WMAppendTextStream(WMText *tPtr, char *text)
2664 CHECK_CLASS(tPtr, WC_Text);
2666 if(!text)
2667 releaseSelection(tPtr);
2669 tPtr->flags.prepend = False;
2670 if (text && tPtr->parser)
2671 (tPtr->parser) (tPtr, (void *) text);
2672 else
2673 insertText(tPtr, text);
2675 tPtr->flags.needsRefresh = True;
2680 char *
2681 WMGetTextStream(WMText *tPtr)
2683 CHECK_CLASS(tPtr, WC_Text);
2684 return getStream(tPtr, 0);
2687 char *
2688 WMGetTextSelected(WMText *tPtr)
2690 CHECK_CLASS(tPtr, WC_Text);
2691 return getStream(tPtr, 1);
2694 WMBag *
2695 WMGetTextStreamIntoBag(WMText *tPtr)
2697 CHECK_CLASS(tPtr, WC_Text);
2698 return getStreamIntoBag(tPtr, 0);
2701 WMBag *
2702 WMGetTextSelectedIntoBag(WMText *tPtr)
2704 CHECK_CLASS(tPtr, WC_Text);
2705 return getStreamIntoBag(tPtr, 1);
2709 void
2710 WMSetTextDelegate(WMText *tPtr, WMTextDelegate *delegate)
2712 CHECK_CLASS(tPtr, WC_Text);
2714 tPtr->delegate = delegate;
2718 void *
2719 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
2720 char *description, WMColor *color,
2721 unsigned short first, unsigned short reserved)
2723 TextBlock *tb;
2724 unsigned short length;
2726 if (!w || !description || !color)
2727 return NULL;
2729 tb = wmalloc(sizeof(TextBlock));
2730 if (!tb)
2731 return NULL;
2733 length = strlen(description);
2734 tb->text = (char *)wmalloc(length);
2735 memset(tb->text, 0, length);
2736 memcpy(tb->text, description, length);
2737 tb->used = length;
2738 tb->blank = False;
2739 tb->d.widget = w;
2740 tb->color = WMRetainColor(color);
2741 tb->marginN = newMargin(tPtr, NULL);
2742 tb->allocated = 0;
2743 tb->first = first;
2744 tb->kanji = False;
2745 tb->graphic = True;
2746 tb->object = True;
2747 tb->underlined = False;
2748 tb->selected = False;
2749 tb->script = 0;
2750 tb->sections = NULL;
2751 tb->nsections = 0;
2752 tb->prior = NULL;
2753 tb->next = NULL;
2755 return tb;
2759 void *
2760 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
2761 char *description, WMColor *color,
2762 unsigned short first, unsigned short reserved)
2764 TextBlock *tb;
2765 unsigned short length;
2767 if (!p || !description || !color)
2768 return NULL;
2770 tb = wmalloc(sizeof(TextBlock));
2771 if (!tb)
2772 return NULL;
2774 length = strlen(description);
2775 tb->text = (char *)wmalloc(length);
2776 memset(tb->text, 0, length);
2777 memcpy(tb->text, description, length);
2778 tb->used = length;
2779 tb->blank = False;
2780 tb->d.pixmap = p;
2781 tb->color = WMRetainColor(color);
2782 tb->marginN = newMargin(tPtr, NULL);
2783 tb->allocated = 0;
2784 tb->first = first;
2785 tb->kanji = False;
2786 tb->graphic = True;
2787 tb->object = False;
2788 tb->underlined = False;
2789 tb->selected = False;
2790 tb->script = 0;
2791 tb->sections = NULL;
2792 tb->nsections = 0;
2793 tb->prior = NULL;
2794 tb->next = NULL;
2796 return tb;
2799 void *
2800 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
2801 unsigned short first, unsigned short length)
2803 TextBlock *tb;
2805 if (!font || !color)
2806 return NULL;
2808 tb = wmalloc(sizeof(TextBlock));
2809 if (!tb)
2810 return NULL;
2812 tb->allocated = reqBlockSize(length);
2813 tb->text = (char *)wmalloc(tb->allocated);
2814 memset(tb->text, 0, tb->allocated);
2816 if (length < 1|| !text ) { // || *text == '\n') {
2817 *tb->text = ' ';
2818 tb->used = 1;
2819 tb->blank = True;
2820 } else {
2821 memcpy(tb->text, text, length);
2822 tb->used = length;
2823 tb->blank = False;
2826 tb->d.font = WMRetainFont(font);
2827 tb->color = WMRetainColor(color);
2828 tb->marginN = newMargin(tPtr, NULL);
2829 tb->first = first;
2830 tb->kanji = False;
2831 tb->graphic = False;
2832 tb->underlined = False;
2833 tb->selected = False;
2834 tb->script = 0;
2835 tb->sections = NULL;
2836 tb->nsections = 0;
2837 tb->prior = NULL;
2838 tb->next = NULL;
2839 return tb;
2842 void
2843 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
2844 unsigned int kanji, unsigned int underlined, int script,
2845 WMRulerMargins *margins)
2847 TextBlock *tb = (TextBlock *) vtb;
2848 if (!tb)
2849 return;
2851 tb->first = first;
2852 tb->kanji = kanji;
2853 tb->underlined = underlined;
2854 tb->script = script;
2855 tb->marginN = newMargin(tPtr, margins);
2858 void
2859 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
2860 unsigned int *kanji, unsigned int *underlined, int *script,
2861 WMRulerMargins *margins)
2863 TextBlock *tb = (TextBlock *) vtb;
2864 if (!tb)
2865 return;
2867 if (first) *first = tb->first;
2868 if (kanji) *kanji = tb->kanji;
2869 if (underlined) *underlined = tb->underlined;
2870 if (script) *script = tb->script;
2871 if (margins) margins = &tPtr->margins[tb->marginN];
2876 void
2877 WMPrependTextBlock(WMText *tPtr, void *vtb)
2879 TextBlock *tb = (TextBlock *)vtb;
2881 if (!tPtr || !tb)
2882 return;
2884 if (tb->graphic) {
2885 if(tb->object) {
2886 WMWidget *w = tb->d.widget;
2887 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
2888 handleWidgetPress, tb);
2889 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
2890 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
2891 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
2894 WMPutInBag(tPtr->gfxItems, (void *)tb);
2895 tPtr->tpos = 0;
2896 } else tPtr->tpos = tb->used;
2898 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
2899 tb->next = tb->prior = NULL;
2900 tb->first = True;
2901 tPtr->lastTextBlock = tPtr->firstTextBlock
2902 = tPtr->currentTextBlock = tb;
2903 return;
2906 if(!tb->first) {
2907 tb->marginN = tPtr->currentTextBlock->marginN;
2910 tb->next = tPtr->currentTextBlock;
2911 tb->prior = tPtr->currentTextBlock->prior;
2912 if (tPtr->currentTextBlock->prior)
2913 tPtr->currentTextBlock->prior->next = tb;
2915 tPtr->currentTextBlock->prior = tb;
2916 if (!tb->prior)
2917 tPtr->firstTextBlock = tb;
2919 tPtr->currentTextBlock = tb;
2923 void
2924 WMAppendTextBlock(WMText *tPtr, void *vtb)
2926 TextBlock *tb = (TextBlock *)vtb;
2928 if (!tPtr || !tb)
2929 return;
2931 if (tb->graphic) {
2932 if(tb->object) {
2933 WMWidget *w = tb->d.widget;
2934 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
2935 handleWidgetPress, tb);
2936 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
2937 (W_VIEW(w))->attribs.cursor =
2938 tPtr->view->screen->defaultCursor;
2939 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
2942 WMPutInBag(tPtr->gfxItems, (void *)tb);
2943 tPtr->tpos = 0;
2944 } else tPtr->tpos = tb->used;
2946 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
2947 tb->next = tb->prior = NULL;
2948 tb->first = True;
2949 tPtr->lastTextBlock = tPtr->firstTextBlock
2950 = tPtr->currentTextBlock = tb;
2951 return;
2954 if(!tb->first) {
2955 tb->marginN = tPtr->currentTextBlock->marginN;
2958 tb->next = tPtr->currentTextBlock->next;
2959 tb->prior = tPtr->currentTextBlock;
2960 if (tPtr->currentTextBlock->next)
2961 tPtr->currentTextBlock->next->prior = tb;
2963 tPtr->currentTextBlock->next = tb;
2965 if (!tb->next)
2966 tPtr->lastTextBlock = tb;
2968 tPtr->currentTextBlock = tb;
2971 void *
2972 WMRemoveTextBlock(WMText *tPtr)
2974 TextBlock *tb = NULL;
2976 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
2977 || !tPtr->currentTextBlock) {
2978 printf("cannot remove non existent TextBlock!\b");
2979 return tb;
2982 tb = tPtr->currentTextBlock;
2983 if (tb->graphic) {
2984 WMRemoveFromBag(tPtr->gfxItems, (void *)tb);
2986 if(tb->object) {
2987 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
2988 handleWidgetPress, tb);
2989 WMUnmapWidget(tb->d.widget);
2993 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
2994 if (tPtr->currentTextBlock->next)
2995 tPtr->currentTextBlock->next->prior = NULL;
2997 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
2998 tPtr->currentTextBlock = tPtr->firstTextBlock;
3000 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3001 tPtr->currentTextBlock->prior->next = NULL;
3002 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3003 tPtr->currentTextBlock = tPtr->lastTextBlock;
3004 } else {
3005 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3006 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3007 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3010 return (void *)tb;
3013 void
3014 WMDestroyTextBlock(WMText *tPtr, void *vtb)
3016 TextBlock *tb = (TextBlock *)vtb;
3017 if (!tPtr || !tb)
3018 return;
3020 if (tb->graphic) {
3021 if(tb->object) {
3022 /* naturally, there's a danger to destroying
3023 widgets whose action brings us here:
3024 ie. press a button to destroy it... need to
3025 find a safer way. till then... this stays commented out */
3026 //WMDestroyWidget(tb->d.widget);
3027 //wfree(tb->d.widget);
3028 tb->d.widget = NULL;
3029 } else {
3030 WMReleasePixmap(tb->d.pixmap);
3031 tb->d.pixmap = NULL;
3033 } else {
3034 WMReleaseFont(tb->d.font);
3037 WMReleaseColor(tb->color);
3038 if (tb->sections && tb->nsections > 0)
3039 wfree(tb->sections);
3040 wfree(tb->text);
3041 wfree(tb);
3042 tb = NULL;
3046 void
3047 WMRefreshText(WMText *tPtr, int vpos, int hpos)
3049 if (!tPtr || vpos<0 || hpos<0)
3050 return;
3052 if (tPtr->flags.frozen && !tPtr->flags.needsRefresh)
3053 return;
3055 if(tPtr->flags.monoFont) {
3056 int j, c = WMGetBagItemCount(tPtr->gfxItems);
3057 TextBlock *tb;
3059 /* make sure to unmap widgets no matter where they are */
3060 for(j=0; j<c; j++) {
3061 if ((tb = (TextBlock *) WMGetFromBag(tPtr->gfxItems, j))) {
3062 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3063 WMUnmapWidget(tb->d.widget);
3069 if (tPtr->vpos != vpos) {
3070 if (vpos < 0 || tPtr->docHeight < tPtr->visible.h) {
3071 tPtr->vpos = 0;
3072 } else if(tPtr->docHeight - vpos > tPtr->visible.h - tPtr->visible.y) {
3073 tPtr->vpos = vpos;
3074 } else {
3075 tPtr->vpos = tPtr->docHeight - tPtr->visible.h;
3079 if (tPtr->hpos != hpos) {
3080 if (hpos < 0 || tPtr->docWidth < tPtr->visible.w) {
3081 tPtr->hpos = 0;
3082 } else if(tPtr->docWidth - hpos > tPtr->visible.w - tPtr->visible.x) {
3083 tPtr->hpos = hpos;
3084 } else {
3085 tPtr->hpos = tPtr->docWidth - tPtr->visible.w;
3090 tPtr->flags.laidOut = False;
3091 layOutDocument(tPtr);
3092 updateScrollers(tPtr);
3093 paintText(tPtr);
3094 tPtr->flags.needsRefresh = False;
3098 void
3099 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3101 if (!tPtr)
3102 return;
3104 if (color)
3105 tPtr->fgGC = WMColorGC(color);
3106 else
3107 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3109 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3112 void
3113 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3115 if (!tPtr)
3116 return;
3118 if (color) {
3119 tPtr->bgGC = WMColorGC(color);
3120 W_SetViewBackgroundColor(tPtr->view, color);
3121 } else {
3122 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3123 W_SetViewBackgroundColor(tPtr->view,
3124 WMWhiteColor(tPtr->view->screen));
3127 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3130 void
3131 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3133 if (!tPtr)
3134 return;
3135 tPtr->flags.relief = relief;
3136 paintText(tPtr);
3139 void
3140 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3142 if (!tPtr)
3143 return;
3145 if (shouldhave && !tPtr->hS) {
3146 tPtr->hS = WMCreateScroller(tPtr);
3147 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3148 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3149 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3150 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3151 WMMapWidget(tPtr->hS);
3152 } else if (!shouldhave && tPtr->hS) {
3153 WMUnmapWidget(tPtr->hS);
3154 WMDestroyWidget(tPtr->hS);
3155 tPtr->hS = NULL;
3158 tPtr->hpos = 0;
3159 tPtr->prevHpos = 0;
3160 textDidResize(tPtr->view->delegate, tPtr->view);
3164 void
3165 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3167 if (!tPtr)
3168 return;
3170 if(shouldhave && !tPtr->ruler) {
3171 tPtr->ruler = WMCreateRuler(tPtr);
3172 (W_VIEW(tPtr->ruler))->attribs.cursor =
3173 tPtr->view->screen->defaultCursor;
3174 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3175 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3176 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3177 } else if(!shouldhave && tPtr->ruler) {
3178 WMShowTextRuler(tPtr, False);
3179 WMDestroyWidget(tPtr->ruler);
3180 tPtr->ruler = NULL;
3182 textDidResize(tPtr->view->delegate, tPtr->view);
3185 void
3186 WMShowTextRuler(WMText *tPtr, Bool show)
3188 if(!tPtr)
3189 return;
3190 if(!tPtr->ruler)
3191 return;
3193 if(tPtr->flags.monoFont)
3194 show = False;
3196 tPtr->flags.rulerShown = show;
3197 if(show) {
3198 WMMapWidget(tPtr->ruler);
3199 } else {
3200 WMUnmapWidget(tPtr->ruler);
3203 textDidResize(tPtr->view->delegate, tPtr->view);
3206 Bool
3207 WMGetTextRulerShown(WMText *tPtr)
3209 if(!tPtr)
3210 return 0;
3212 if(!tPtr->ruler)
3213 return 0;
3215 return tPtr->flags.rulerShown;
3219 void
3220 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3222 if (!tPtr)
3223 return;
3225 if (shouldhave && !tPtr->vS) {
3226 tPtr->vS = WMCreateScroller(tPtr);
3227 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3228 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3229 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3230 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3231 WMMapWidget(tPtr->vS);
3232 } else if (!shouldhave && tPtr->vS) {
3233 WMUnmapWidget(tPtr->vS);
3234 WMDestroyWidget(tPtr->vS);
3235 tPtr->vS = NULL;
3238 tPtr->vpos = 0;
3239 tPtr->prevVpos = 0;
3240 textDidResize(tPtr->view->delegate, tPtr->view);
3245 Bool
3246 WMScrollText(WMText *tPtr, int amount)
3248 Bool scroll=False;
3249 if (!tPtr)
3250 return False;
3251 if (amount == 0 || !tPtr->view->flags.realized)
3252 return False;
3254 if (amount < 0) {
3255 if (tPtr->vpos > 0) {
3256 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3257 else tPtr->vpos=0;
3258 scroll=True;
3259 } } else {
3260 int limit = tPtr->docHeight - tPtr->visible.h;
3261 if (tPtr->vpos < limit) {
3262 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3263 else tPtr->vpos = limit;
3264 scroll = True;
3265 } }
3267 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3268 updateScrollers(tPtr);
3269 paintText(tPtr);
3271 tPtr->prevVpos = tPtr->vpos;
3272 return scroll;
3275 Bool
3276 WMPageText(WMText *tPtr, Bool direction)
3278 if (!tPtr) return False;
3279 if (!tPtr->view->flags.realized) return False;
3281 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3284 void
3285 WMSetTextEditable(WMText *tPtr, Bool editable)
3287 if (!tPtr)
3288 return;
3289 tPtr->flags.editable = editable;
3292 int
3293 WMGetTextEditable(WMText *tPtr)
3295 if (!tPtr)
3296 return 0;
3297 return tPtr->flags.editable;
3300 void
3301 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3303 if (!tPtr)
3304 return;
3305 tPtr->flags.ignoreNewLine = ignore;
3308 Bool
3309 WMGetTextIgnoresNewline(WMText *tPtr)
3311 if (!tPtr)
3312 return True;
3313 return tPtr->flags.ignoreNewLine;
3316 void
3317 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3319 if (!tPtr)
3320 return;
3321 if (mono && tPtr->flags.rulerShown)
3322 WMShowTextRuler(tPtr, False);
3324 tPtr->flags.monoFont = mono;
3325 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3328 Bool
3329 WMGetTextUsesMonoFont(WMText *tPtr)
3331 if (!tPtr)
3332 return True;
3333 return tPtr->flags.monoFont;
3337 void
3338 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3340 if (!tPtr)
3341 return;
3343 WMReleaseFont(tPtr->dFont);
3344 if (font)
3345 tPtr->dFont = font;
3346 else
3347 tPtr->dFont = WMRetainFont(tPtr->view->screen->normalFont);
3350 WMFont *
3351 WMGetTextDefaultFont(WMText *tPtr)
3353 if (!tPtr)
3354 return NULL;
3355 else
3356 return tPtr->dFont;
3359 void
3360 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3362 if (!tPtr)
3363 return;
3364 tPtr->flags.alignment = alignment;
3365 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3368 void
3369 WMSetTextParser(WMText *tPtr, WMAction *parser)
3371 if (!tPtr)
3372 return;
3373 tPtr->parser = parser;
3376 void
3377 WMSetTextWriter(WMText *tPtr, WMAction *writer)
3379 if (!tPtr)
3380 return;
3381 tPtr->writer = writer;
3384 int
3385 WMGetTextInsertType(WMText *tPtr)
3387 if (!tPtr)
3388 return 0;
3389 return tPtr->flags.prepend;
3393 void
3394 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3396 if (!tPtr || !color)
3397 return;
3399 setSelectionProperty(tPtr, NULL, color);
3404 void
3405 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3407 if (!tPtr || !font)
3408 return;
3410 setSelectionProperty(tPtr, font, NULL);
3414 void
3415 WMFreezeText(WMText *tPtr)
3417 if (!tPtr)
3418 return;
3420 tPtr->flags.frozen = True;
3423 void
3424 WMThawText(WMText *tPtr)
3426 if (!tPtr)
3427 return;
3429 tPtr->flags.frozen = False;
3433 Bool
3434 WMFindInTextStream(WMText *tPtr, char *needle)
3436 char *haystack = NULL;
3437 Bool result;
3439 if (!tPtr || !needle)
3440 return False;
3442 if ( !(haystack = "WMGetTextStream(tPtr)"))
3443 return False;
3445 result = (Bool) strstr(haystack, needle);
3446 wfree(haystack);
3447 return result;
3450 /* would be nice to have in WINGs proper... */
3451 static void
3452 changeFontProp(char *fname, char *newprop, int which)
3454 char before[128], prop[128], after[128];
3455 char *ptr, *bptr;
3456 int part=0;
3458 if(!fname || !prop)
3459 return;
3461 ptr = fname;
3462 bptr = before;
3463 while (*ptr) {
3464 if(*ptr == '-') {
3465 *bptr = 0;
3466 if(part==which) bptr = prop;
3467 else if(part==which+1) bptr = after;
3468 *bptr++ = *ptr;
3469 part++;
3470 } else {
3471 *bptr++ = *ptr;
3472 } ptr++;
3473 }*bptr = 0;
3474 snprintf(fname, 255, "%s-%s%s", before, newprop, after);
3478 WMFont *
3479 WMGetFontPlain(WMScreen *scrPtr, WMFont *font)
3481 WMFont *nfont=NULL;
3482 if(!scrPtr|| !font)
3483 return NULL;
3484 return font;
3489 WMFont *
3490 WMGetFontBold(WMScreen *scrPtr, WMFont *font)
3492 WMFont *newfont=NULL;
3493 char fname[256];
3494 if(!scrPtr || !font)
3495 return NULL;
3496 snprintf(fname, 255, font->name);
3497 changeFontProp(fname, "bold", 2);
3498 newfont = WMCreateNormalFont(scrPtr, fname);
3499 if(!newfont)
3500 newfont = font;
3501 return newfont;
3506 WMFont *
3507 WMGetFontItalic(WMScreen *scrPtr, WMFont *font)
3509 WMFont *newfont=NULL;
3510 char fname[256];
3511 if(!scrPtr || !font)
3512 return NULL;
3513 snprintf(fname, 255, font->name);
3514 changeFontProp(fname, "o", 3);
3515 newfont = WMCreateNormalFont(scrPtr, fname);
3516 if(!newfont)
3517 newfont = font;
3518 return newfont;
3522 WMFont *
3523 WMGetFontOfSize(WMScreen *scrPtr, WMFont *font, int size)
3525 WMFont *nfont=NULL;
3526 if(!scrPtr || !font || size<1)
3527 return NULL;
3528 return font;
3534 typedef struct _currentFormat {
3535 WMBag *fonts;
3536 WMBag *colors;
3537 WMColor *ccolor;
3538 WMFont *cfont;
3539 WMRulerMargins margins;
3540 //WMBag *aligns; // for tables...
3541 /* the following are "nested"
3542 i.e.: <b><b><i></b><b></i>
3543 1 2 1 1 2 0 get it? */
3544 short i;
3545 short b;
3546 short u;
3547 short fmargin;
3548 short bmargin;
3549 short first:1;
3550 short type:1;
3551 WMAlignment align:2;
3552 short ul:3; /* how "nested"... up to 8 levels deep */
3553 short comment:1; /* ignore text till --> */
3554 short RESERVED:10;
3555 } CFMT;
3556 CFMT cfmt;
3560 #if 0
3561 getArg(char *t, short type, void *arg)
3563 short d=0;
3564 while(*(++t) && !d) {
3565 if(type==0) {
3566 if(*t>='0' && *t<='9') {
3567 sscanf(t, "%d", arg);
3568 while(*t&& (*t<'0' || *t>'9'))
3569 t++;
3570 d=1;
3575 #endif
3577 void parseToken(WMText *tPtr, char *token, short tk)
3579 short open=0; /* 0 starts, 1 closes */
3580 void *tb= NULL;
3581 int prepend = WMGetTextInsertType(tPtr);
3582 WMScreen *scr = tPtr->view->screen;
3584 if(tk == -1) {
3585 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
3586 NULL, cfmt.cfont, cfmt.ccolor, True, 0));
3587 return;
3591 while(*token && *token == ' ')
3592 token++;
3594 if(*token == '/') {
3595 token++;
3596 open = 1;
3598 while(*token == ' ')
3599 token++;
3602 if(!tPtr->flags.parsingHTML) {
3603 if(mystrcasecmp(token, "html")) {
3604 printf("got HTMLLLL: [%s]\n", token);
3605 tPtr->flags.parsingHTML = True;
3606 } else {
3607 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
3608 token, cfmt.cfont, cfmt.ccolor, cfmt.first, strlen(token)));
3611 return;
3615 if(strlen(token)==1) {
3616 /* nice and fast for small tokens... no need for too much brain
3617 power here */
3618 switch(TOLOWER(*token)) {
3619 case 'i':
3620 if(!open) {
3621 cfmt.cfont = WMGetFontItalic(scr, cfmt.cfont);
3622 WMPutInBag(cfmt.fonts, (void *)cfmt.cfont);
3623 } else { /*dun wanna remove the baseFont eh? */
3624 int count = WMGetBagItemCount(cfmt.fonts);
3625 if(count>1) {
3626 WMDeleteFromBag(cfmt.fonts, count-1);
3628 cfmt.cfont = (WMFont *)WMGetFromBag(cfmt.fonts,
3629 WMGetBagItemCount(cfmt.fonts)-1);
3630 }printf("i\n"); break;
3632 case 'b':
3633 if(!open) {
3634 cfmt.cfont = WMGetFontBold(scr, cfmt.cfont);
3635 WMPutInBag(cfmt.fonts, (void *)cfmt.cfont);
3636 } else { /*dun wanna remove the baseFont eh? */
3637 int count = WMGetBagItemCount(cfmt.fonts);
3638 if(count>1)
3639 WMDeleteFromBag(cfmt.fonts, count-1);
3640 cfmt.cfont = (WMFont *)WMGetFromBag(cfmt.fonts,
3641 WMGetBagItemCount(cfmt.fonts)-1);
3642 } break;
3643 case 'p':
3644 cfmt.first = True;
3645 tb = WMCreateTextBlockWithText(tPtr, NULL, cfmt.cfont,
3646 cfmt.ccolor, cfmt.first, 0);
3647 // WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
3648 WMAppendTextBlock(tPtr, tb);
3649 cfmt.first = False;
3650 break;
3651 case 'u': cfmt.u = !open; break;
3653 } else {
3654 if(mystrcasecmp(token, "br")) {
3655 cfmt.first = True;
3657 else if(mystrcasecmp(token, "ul")) {
3658 if(open) {
3659 if(cfmt.ul>1) cfmt.ul--;
3660 } else cfmt.ul++;
3661 if(cfmt.ul) {
3662 cfmt.bmargin = cfmt.ul*30;
3663 cfmt.fmargin = cfmt.bmargin-10;
3664 } else cfmt.fmargin = cfmt.bmargin = 0;
3665 } else if(mystrcasecmp(token, "li")) {
3666 cfmt.first = True;
3667 //change margins... create a new margin....
3668 //(cfmt.fmargin, cfmt.bmargin,
3669 } else if(mystrcasecmp(token, "html")) {
3670 tPtr->flags.parsingHTML = !open;
3672 } else if(mystrcasecmp(token, "align"))
3673 ;//printf("align");
3674 else if(mystrcasecmp(token, "img")) {
3675 if(!open) {
3676 char *mark=NULL;
3677 WMPixmap *pixmap;
3678 token+=3;
3679 while(*token == ' ') token++;
3680 do {
3681 switch(TOLOWER(*token)) {
3682 case 's':
3683 if(TOLOWER(*(1+token)) == 'r' && TOLOWER(*(2+token)) == 'c') {
3684 mark = strchr(token, '=');
3685 if(mark) {
3686 char img[256], *iptr;
3687 token = mark+1;
3688 if(!token) return;
3689 sscanf(token, "%s", img);
3690 iptr = img;
3691 if(*img == '\"') { img[strlen(img)-1] = 0; iptr++;}
3692 pixmap = WMCreatePixmapFromFile(scr, iptr);
3693 if(pixmap) {
3694 tb = WMCreateTextBlockWithPixmap(tPtr, pixmap,
3695 iptr, cfmt.ccolor, cfmt.first, 0);
3696 // WMSetTextBlockProperties(tb, cfmt.first,
3697 // False, (cfmt.u?1:0), 0, cfmt.margins);
3698 WMAppendTextBlock(tPtr, tb);
3699 cfmt.first = False;
3701 //printf("[%s]\n", iptr);
3702 } } break; } } while(*(token++));
3704 } else if(mystrcasecmp(token, "font")) {
3705 #if 0
3706 if(open) {
3707 cfmt.cfont = (WMFont *)WMGetFromBag(cfmt.fonts,
3708 WMGetBagItemCount(cfmt.fonts)-1);
3709 } else
3710 (WMColor *)WMGetFromBag(cfmt.colors,
3711 WMGetBagItemCount(cfmt.colors)-1),
3712 #endif
3714 else if(mystrcasecmp(token, "center")) {
3715 printf("center\n");
3716 if(open) cfmt.align = WALeft;
3717 else cfmt.align = WACenter;
3718 cfmt.first = True;
3719 //change margins...
3726 //printf("parse token (%s)[%s]\n", open?"close":"open", token);
3727 #if 0
3728 i=0;
3729 //while(*token && !isspace(*(token))) token++;
3730 //printf("A:%d a:%d z%d Z%d\n", '1', 'a', 'Z', 'z');
3731 do {
3732 if(!mm) {
3733 if(c>=65 && c<=122) { major[i++] = c;
3734 } else if(c==' ' || c=='='){ major[i] = 0; i=0; mm=1;
3735 printf("\nmajor: [%s]", major);}
3736 } else {
3737 if(c!=' ') {
3738 minor[i++] = c;
3739 } else { minor[i] = 0; i=0; printf(" minor: [%s] ", minor);}
3741 }while((c = *(++token)));
3742 #endif
3745 //printf("parse token (%s)[%s]\n", open?"close":"open", token);