Fixed some bugs, and compilation warnings.
[wmaker-crm.git] / WINGs / wtext.c
blob3046213806f34b384d63c07123140606a9c2f37c
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.
21 #include "WINGsP.h"
22 #include <X11/keysym.h>
23 #include <X11/Xatom.h>
25 #define DO_BLINK 0
27 WMFont * WMGetFontPlain(WMScreen *scrPtr, WMFont *font);
28 WMFont * WMGetFontBold(WMScreen *scrPtr, WMFont *font);
29 WMFont * WMGetFontItalic(WMScreen *scrPtr, WMFont *font);
30 WMFont * WMGetFontOfSize(WMScreen *scrPtr, WMFont *font, int size);
32 /* TODO:
34 * - change Bag stuffs to WMArray
35 * - assess danger of destroying widgets whose actions link to other pages
36 * - integrate WMGetFont* functions into WINGs proper (fontpanel)?
37 * - change cursor shape around pixmaps
38 * - redo blink code to reduce paint event... use pixmap buffer...
39 * - add paragraph support (full) and '\n' code in getStream..
40 * - use currentTextBlock and neighbours for fast paint and layout
41 * - replace copious uses of Refreshtext with appropriate layOut()...
42 * - WMFindInTextStream should also highlight found text...
43 * - add full support for Horizontal Scroll
47 /* a Section is a section of a TextBlock that describes what parts
48 of a TextBlock has been laid out on which "line"...
49 o this greatly aids redraw, scroll and selection.
50 o this is created during layoutLine, but may be later modified.
51 o there may be many Sections per TextBlock, hence the array */
52 typedef struct {
53 unsigned int x, y; /* where to draw it from */
54 unsigned short w, h; /* its width and height */
55 unsigned short begin; /* where the layout begins */
56 unsigned short end ; /* where it ends */
57 unsigned short last:1; /* is it the last section on a "line"? */
58 unsigned int _y:31; /* the "line" it and other textblocks are on */
59 } Section;
62 /* a TextBlock is a doubly-linked list of TextBlocks containing:
63 o text for the block, color and font
64 o or a pointer to the pixmap
65 o OR a pointer to the widget and the (text) description for its graphic
68 typedef struct _TextBlock {
69 struct _TextBlock *next; /* next text block in linked list */
70 struct _TextBlock *prior; /* prior text block in linked list */
72 char *text; /* pointer to text (could be kanji) */
73 /* or to the object's description */
74 union {
75 WMFont *font; /* the font */
76 WMWidget *widget; /* the embedded widget */
77 WMPixmap *pixmap; /* the pixmap */
78 } d; /* description */
80 unsigned short used; /* number of chars in this block */
81 unsigned short allocated; /* size of allocation (in chars) */
82 WMColor *color; /* the color */
84 Section *sections; /* the region for layouts (a growable array) */
85 /* an _array_! of size _nsections_ */
87 unsigned short s_begin; /* where the selection begins */
88 unsigned short s_end; /* where it ends */
90 unsigned int first:1; /* first TextBlock in paragraph */
91 unsigned int blank:1; /* ie. blank paragraph */
92 unsigned int kanji:1; /* is of 16-bit characters or not */
93 unsigned int graphic:1; /* graphic or text: text=0 */
94 unsigned int object:1; /* embedded object or pixmap */
95 unsigned int underlined:1; /* underlined or not */
96 unsigned int selected:1; /* selected or not */
97 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
98 int script:8; /* script in points: negative for subscript */
99 unsigned int marginN:8; /* which of the margins in the tPtr to use */
100 unsigned int RESERVED:9;
101 } TextBlock;
104 /* somehow visible.h beats the hell outta visible.size.height :-) */
105 typedef struct {
106 unsigned int y;
107 unsigned int x;
108 unsigned int h;
109 unsigned int w;
110 } myRect;
113 typedef struct W_Text {
114 W_Class widgetClass; /* the class number of this widget */
115 W_View *view; /* the view referring to this instance */
117 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
119 WMScroller *vS; /* the vertical scroller */
120 unsigned int vpos; /* the current vertical position */
121 unsigned int prevVpos; /* the previous vertical position */
123 WMScroller *hS; /* the horizontal scroller */
124 unsigned int hpos; /* the current horizontal position */
125 unsigned int prevHpos; /* the previous horizontal position */
127 WMFont *dFont; /* the default font */
128 WMColor *dColor; /* the default color */
129 WMPixmap *dBulletPix; /* the default pixmap for bullets */
131 GC bgGC; /* the background GC to draw with */
132 GC fgGC; /* the foreground GC to draw with */
133 Pixmap db; /* the buffer on which to draw */
135 myRect visible; /* the actual rectangle that can be drawn into */
136 myRect cursor; /* the position and (height) of cursor */
137 myRect sel; /* the selection rectangle */
139 WMPoint clicked; /* where in the _document_ was clicked */
141 unsigned short tpos; /* the position in the currentTextBlock */
142 unsigned short docWidth; /* the width of the entire document */
143 unsigned int docHeight; /* the height of the entire document */
145 TextBlock *firstTextBlock;
146 TextBlock *lastTextBlock;
147 TextBlock *currentTextBlock;
149 WMBag *gfxItems; /* a nice bag containing graphic items */
151 #if DO_BLINK
152 WMHandlerID timerID; /* for nice twinky-winky */
153 #endif
155 WMAction *parser;
156 WMAction *writer;
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 waitingForSelection:1; /* dum dee dumm... */
169 unsigned int extendSelection:1; /* shift-drag to select more regions */
171 unsigned int rulerShown:1; /* whether the ruler is shown or not */
172 unsigned int frozen:1; /* whether screen updates are to be made */
173 unsigned int cursorShown:1; /* whether to show the cursor */
174 unsigned int clickPos:1; /* clicked before=0 or after=1 a graphic: */
175 /* (within counts as after too) */
177 unsigned int horizOnDemand:1;/* if a large image should appear*/
178 unsigned int needsRefresh:1; /* in case of Append/Deletes */
179 unsigned int ignoreNewLine:1;/* turn it into a ' ' in streams > 1 */
180 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
181 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
182 WMAlignment alignment:2; /* the alignment for text */
183 WMReliefType relief:3; /* the relief to display with */
184 unsigned int RESERVED:2;
185 } flags;
186 } Text;
190 * A hack to speed up caseless_equal. Thanks to Quincey Koziol for
191 * developing it for the "chimera" folks so I could use it 7 years later ;-)
192 * Constraint: nothing but '\0' may map to 0
194 static unsigned char map_table[256] = {
195 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,
196 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
197 52,53,54,55,56,57,58,59,60,61,62,63,64,97,98,99,100,101,102,103,104,105,
198 106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,91,
199 92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
200 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,
201 130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,
202 148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,
203 166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
204 184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,
205 202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,
206 220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,
207 238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255};
209 #define MAX_TB_PER_LINE 64
210 #define MAX_TOKEN_SIZE 255
211 #define MAX_TEXT_SIZE 1023
213 #define TOLOWER(x) (map_table[(int)x])
215 #define ISALNUM(x) ( ((x>='0') && (x<='9')) \
216 || ((x>='a') && (x<='z')) || ((x>='A') && x<='Z'))
218 #if DO_BLINK
219 #define CURSOR_BLINK_ON_DELAY 600
220 #define CURSOR_BLINK_OFF_DELAY 400
221 #endif
223 static char *default_bullet[] = {
224 "6 6 4 1",
225 " c None s None", ". c black",
226 "X c white", "o c #808080",
227 " ... ",
228 ".XX.. ",
229 ".XX..o",
230 ".....o",
231 " ...oo",
232 " ooo "};
234 static void
235 handleEvents(XEvent *event, void *data);
238 static int
239 getMarginNumber(Text *tPtr, WMRulerMargins *margins)
241 unsigned int i=0;
243 for(i=0; i < tPtr->nMargins; i++) {
245 if(WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
246 return i;
249 return -1;
255 static int
256 newMargin(Text *tPtr, WMRulerMargins *margins)
258 int n;
260 if (!margins) {
261 tPtr->margins[0].retainCount++;
262 return 0;
265 n = getMarginNumber(tPtr, margins);
267 if (n == -1) {
269 tPtr->margins = wrealloc(tPtr->margins,
270 (++tPtr->nMargins)*sizeof(WMRulerMargins));
272 n = tPtr->nMargins-1;
273 tPtr->margins[n].left = margins->left;
274 tPtr->margins[n].first = margins->first;
275 tPtr->margins[n].body = margins->body;
276 tPtr->margins[n].right = margins->right;
277 //for tabs.
278 tPtr->margins[n].retainCount = 1;
279 } else {
280 tPtr->margins[n].retainCount++;
283 return n;
286 static Bool
287 sectionWasSelected(Text *tPtr, TextBlock *tb, XRectangle *rect, int s)
289 unsigned short i, w, lw, selected = False, extend = False;
290 myRect sel;
293 /* if selection rectangle completely encloses the section */
294 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
295 && (tb->sections[s]._y + tb->sections[s].h
296 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
297 sel.x = 0;
298 sel.w = tPtr->visible.w;
299 selected = extend = True;
301 /* or if it starts on a line and then goes further down */
302 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
303 && (tb->sections[s]._y + tb->sections[s].h
304 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
305 && (tb->sections[s]._y + tb->sections[s].h
306 >= tPtr->visible.y + tPtr->sel.y) ) {
307 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
308 sel.w = tPtr->visible.w;
309 selected = extend = True;
311 /* or if it begins before a line, but ends on it */
312 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
313 && (tb->sections[s]._y + tb->sections[s].h
314 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
315 && (tb->sections[s]._y
316 <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
318 if (1||tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
319 sel.w = tPtr->sel.x + tPtr->sel.w;
320 else
321 sel.w = tPtr->sel.x;
323 sel.x = 0;
324 selected = True;
326 /* or if the selection rectangle lies entirely within a line */
327 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
328 && (tPtr->sel.w >= 2)
329 && (tb->sections[s]._y + tb->sections[s].h
330 >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h) ) {
331 sel.x = tPtr->sel.x;
332 sel.w = tPtr->sel.w;
333 selected = True;
336 if (selected) {
337 selected = False;
339 /* if not within (modified) selection rectangle */
340 if ( tb->sections[s].x > sel.x + sel.w
341 || tb->sections[s].x + tb->sections[s].w < sel.x)
342 return False;
344 if (tb->graphic) {
345 if ( tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w
346 && tb->sections[s].x >= sel.x) {
347 rect->width = tb->sections[s].w;
348 rect->x = tb->sections[s].x;
349 selected = True;
351 } else {
353 i = tb->sections[s].begin;
354 lw = 0;
356 if (0&& tb->sections[s].x >= sel.x) {
357 tb->s_begin = tb->sections[s].begin;
358 goto _selEnd;
361 while (++i <= tb->sections[s].end) {
363 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
364 lw += w;
366 if (lw + tb->sections[s].x >= sel.x
367 || i == tb->sections[s].end ) {
368 lw -= w;
369 i--;
370 tb->s_begin = (tb->selected? WMIN(tb->s_begin, i) : i);
371 break;
375 if (i > tb->sections[s].end) {
376 printf("WasSelected: (i > tb->sections[s].end) \n");
377 return False;
380 _selEnd: rect->x = tb->sections[s].x + lw;
381 lw = 0;
382 while(++i <= tb->sections[s].end) {
384 w = WMWidthOfString(tb->d.font, &(tb->text[i-1]), 1);
385 lw += w;
387 if (lw + rect->x >= sel.x + sel.w
388 || i == tb->sections[s].end ) {
390 if (i != tb->sections[s].end) {
391 lw -= w;
392 i--;
395 rect->width = lw;
396 if (tb->sections[s].last && sel.x + sel.w
397 >= tb->sections[s].x + tb->sections[s].w
398 && extend ) {
399 rect->width += (tPtr->visible.w - rect->x - lw);
402 tb->s_end = (tb->selected? WMAX(tb->s_end, i) : i);
403 selected = True;
404 break;
405 } } } }
407 if (selected) {
408 rect->y = tb->sections[s]._y - tPtr->vpos;
409 rect->height = tb->sections[s].h;
410 if(tb->graphic) { printf("graphic s%d h%d\n", s,tb->sections[s].h);}
412 return selected;
416 static void
417 setSelectionProperty(WMText *tPtr, WMFont *font, WMColor *color)
419 TextBlock *tb;
420 int isFont=False;
422 if((font && color) || (!font && !color))
423 return;
425 if(font && !color)
426 isFont = True;
429 tb = tPtr->firstTextBlock;
430 if (!tb || !tPtr->flags.ownsSelection)
431 return;
434 while (tb) {
435 if (tb->selected) {
437 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
439 if(isFont) {
440 if(!tb->graphic) {
441 WMReleaseFont(tb->d.font);
442 tb->d.font = WMRetainFont(font);
444 } else {
445 WMReleaseColor(tb->color);
446 tb->color = WMRetainColor(color);
449 } else if (tb->s_end <= tb->used) {
451 TextBlock *otb = tb;
452 int count=0;
453 TextBlock *ntb = (TextBlock *)
454 WMCreateTextBlockWithText(tPtr,
455 &(tb->text[tb->s_begin]),
456 (isFont?font:tb->d.font),
457 (isFont?tb->color:color),
458 False, (tb->s_end - tb->s_begin));
460 if (ntb) {
461 ntb->selected = True;
462 ntb->s_begin = 0;
463 ntb->s_end = ntb->used;
464 WMAppendTextBlock(tPtr, ntb);
465 count++;
468 #if 0
469 if (tb->used > tb->s_end) {
470 ntb = (TextBlock *)
471 WMCreateTextBlockWithText(tPtr,
472 &(tb->text[tb->s_end]),
473 (isFont?font:tb->d.font),
474 (isFont?tb->color:color),
475 False, tb->used - tb->s_end);
477 if (ntb) {
478 ntb->selected = True;
479 ntb->s_begin = 0;
480 ntb->s_end = ntb->used;
481 WMAppendTextBlock(tPtr, ntb);
482 count++;
485 #endif
487 if (count == 1)
488 tb = otb->next;
489 else if (count == 2)
490 tb = otb->next->next;
492 tb->used = tb->s_end = tb->s_begin;
496 tb = tb->next;
499 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
503 static void
504 removeSelection(Text *tPtr)
506 TextBlock *tb = NULL;
508 if (!(tb = tPtr->firstTextBlock))
509 return;
511 while (tb) {
512 if (tb->selected) {
514 if ( (tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
515 tPtr->currentTextBlock = tb;
516 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
517 tb = tPtr->currentTextBlock;
518 if (tb)
519 tPtr->tpos = 0;
520 continue;
522 } else if (tb->s_end <= tb->used) {
523 memmove(&(tb->text[tb->s_begin]),
524 &(tb->text[tb->s_end]), tb->used - tb->s_end);
525 tb->used -= (tb->s_end - tb->s_begin);
526 tb->selected = False;
527 tPtr->tpos = tb->s_begin;
532 tb = tb->next;
537 static void
538 paintText(Text *tPtr)
540 TextBlock *tb = tPtr->firstTextBlock;
541 WMFont *font;
542 GC gc, greyGC;
543 char *text;
544 int len, y, c, s, done=False;
545 int prev_y=-23;
546 WMScreen *scr = tPtr->view->screen;
547 Display *dpy = tPtr->view->screen->display;
548 Window win = tPtr->view->window;
550 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
551 return;
553 XFillRectangle(dpy, tPtr->db, tPtr->bgGC,
554 0, 0, tPtr->visible.w, tPtr->visible.h);
556 tb = tPtr->firstTextBlock;
557 if (!tb)
558 goto _copy_area;
561 if (tPtr->flags.ownsSelection)
562 greyGC = WMColorGC(WMGrayColor(scr));
564 done = False;
566 while (!done && tb) {
568 if (tb->graphic) {
569 tb = tb->next;
570 continue;
573 tb->selected = False;
575 for(s=0; s<tb->nsections && !done; s++) {
577 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
578 done = True;
579 break;
582 if ( tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
583 continue;
585 if (tPtr->flags.monoFont) {
586 font = tPtr->dFont;
587 gc = tPtr->fgGC;
588 } else {
589 font = tb->d.font;
590 gc = WMColorGC(tb->color);
593 if (tPtr->flags.ownsSelection) {
594 XRectangle rect;
596 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
597 tb->selected = True;
598 XFillRectangle(dpy, tPtr->db, greyGC,
599 rect.x, rect.y, rect.width, rect.height);
603 prev_y = tb->sections[s]._y;
605 len = tb->sections[s].end - tb->sections[s].begin;
606 text = &(tb->text[tb->sections[s].begin]);
607 y = tb->sections[s].y - tPtr->vpos;
608 WMDrawString(scr, tPtr->db, gc, font,
609 tb->sections[s].x - tPtr->hpos, y, text, len);
611 if (tb->underlined) {
612 XDrawLine(dpy, tPtr->db, gc,
613 tb->sections[s].x - tPtr->hpos,
614 y + font->y + 1,
615 tb->sections[s].x + tb->sections[s].w - tPtr->hpos,
616 y + font->y + 1);
621 tb = (!done? tb->next : NULL);
625 c = WMGetBagItemCount(tPtr->gfxItems);
626 if (c > 0 && !tPtr->flags.monoFont) {
627 int j, h;
629 for(j=0; j<c; j++) {
630 tb = (TextBlock *) WMGetFromBag(tPtr->gfxItems, j);
631 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
632 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h ) {
634 if(tb->object) {
635 if ((W_VIEW(tb->d.widget))->flags.mapped) {
636 WMUnmapWidget(tb->d.widget);
639 } else {
640 if(tb->object) {
641 if (!(W_VIEW(tb->d.widget))->flags.mapped) {
642 if (!(W_VIEW(tb->d.widget))->flags.realized)
643 WMRealizeWidget(tb->d.widget);
644 WMMapWidget(tb->d.widget);
645 WMLowerWidget(tb->d.widget);
649 if (tPtr->flags.ownsSelection) {
650 XRectangle rect;
652 if ( sectionWasSelected(tPtr, tb, &rect, s)) {
653 tb->selected = True;
654 XFillRectangle(dpy, tPtr->db, greyGC,
655 rect.x, rect.y, rect.width, rect.height);
659 if(tb->object) {
660 WMMoveWidget(tb->d.widget,
661 tb->sections[0].x - tPtr->hpos,
662 tb->sections[0].y - tPtr->vpos);
663 h = WMWidgetHeight(tb->d.widget) + 1;
665 } else {
666 WMDrawPixmap(tb->d.pixmap, tPtr->db,
667 tb->sections[0].x - tPtr->hpos,
668 tb->sections[0].y - tPtr->vpos);
669 h = tb->d.pixmap->height + 1;
673 if (tb->underlined) {
674 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
675 tb->sections[0].x,
676 tb->sections[0].y + h,
677 tb->sections[0].x + tb->sections[0].w,
678 tb->sections[0].y + h);
679 } } } }
682 _copy_area:
683 if (tPtr->flags.editable && tPtr->flags.cursorShown
684 && tPtr->cursor.x != -23 && tPtr->flags.focused) {
685 int y = tPtr->cursor.y - tPtr->vpos;
686 XDrawLine(dpy, tPtr->db, tPtr->fgGC,
687 tPtr->cursor.x, y,
688 tPtr->cursor.x, y + tPtr->cursor.h);
691 XCopyArea(dpy, tPtr->db, win, tPtr->bgGC,
692 0, 0,
693 tPtr->visible.w, tPtr->visible.h,
694 tPtr->visible.x, tPtr->visible.y);
697 W_DrawRelief(scr, win, 0, 0,
698 tPtr->view->size.width, tPtr->view->size.height,
699 tPtr->flags.relief);
701 if (tPtr->ruler && tPtr->flags.rulerShown)
702 XDrawLine(dpy, win,
703 tPtr->fgGC, 2, 42,
704 tPtr->view->size.width-4, 42);
709 #if DO_BLINK
711 static void
712 blinkCursor(void *data)
714 Text *tPtr = (Text*)data;
716 if (tPtr->flags.cursorShown) {
717 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY,
718 blinkCursor, data);
719 } else {
720 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
721 blinkCursor, data);
723 paintText(tPtr);
724 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
726 #endif
728 static TextBlock *
729 getFirstNonGraphicBlockFor(TextBlock *tb, short dir)
731 if (!tb)
732 return NULL;
733 while (tb) {
734 if (!tb->graphic)
735 break;
736 tb = (dir? tb->next : tb->prior);
739 return tb;
743 static void
744 cursorToTextPosition(Text *tPtr, int x, int y)
746 TextBlock *tb = NULL;
747 int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */
748 char *text;
750 if(tPtr->flags.needsRefresh)
751 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
753 y += (tPtr->vpos - tPtr->visible.y);
754 if (y<0)
755 y = 0;
757 x -= (tPtr->visible.x - 2);
758 if (x<0)
759 x=0;
761 /* clicked is relative to document, not window... */
762 tPtr->clicked.x = x;
763 tPtr->clicked.y = y;
765 if (! (tb = tPtr->currentTextBlock)) {
766 if (! (tb = tPtr->firstTextBlock)) {
767 tPtr->tpos = 0;
768 tPtr->cursor.h = tPtr->dFont->height;
769 tPtr->cursor.y = 2;
770 tPtr->cursor.x = 2;
771 return;
775 /* first, which direction? Most likely, newly clicked
776 position will be close to previous */
777 dir = !(y <= tb->sections[0].y);
778 if ( ( y <= tb->sections[0]._y + tb->sections[0].h )
779 && (y >= tb->sections[0]._y ) ) {
780 /* if it's on the same line */
781 if(x < tb->sections[0].x)
782 dir = 0;
783 if(x >= tb->sections[0].x)
784 dir = 1;
787 tb = tPtr->firstTextBlock;
788 dir = 1;
790 if (tPtr->flags.monoFont && tb->graphic) {
791 tb = getFirstNonGraphicBlockFor(tb, 1);
792 if (!tb) {
793 tPtr->currentTextBlock =
794 (dir? tPtr->lastTextBlock : tPtr->firstTextBlock);
795 tPtr->tpos = 0;
796 return;
800 s = (dir? 0 : tb->nsections-1);
801 if ( y >= tb->sections[s]._y
802 && y <= tb->sections[s]._y + tb->sections[s].h) {
803 goto _doneV;
806 /* get the first section of the TextBlock that lies about
807 the vertical click point */
808 done = False;
809 while (!done && tb) {
811 if (tPtr->flags.monoFont && tb->graphic) {
812 if(tb->next)
813 tb = tb->next;
814 continue;
817 s = (dir? 0 : tb->nsections-1);
818 while (!done && (dir? (s<tb->nsections) : (s>=0) )) {
820 if ( (dir? (y <= tb->sections[s]._y + tb->sections[s].h) :
821 ( y >= tb->sections[s]._y ) ) ) {
822 done = True;
823 } else {
824 dir? s++ : s--;
828 if (!done) {
829 if ( (dir? tb->next : tb->prior)) {
830 tb = (dir ? tb->next : tb->prior);
831 } else {
832 pos = tb->used;
833 break; //goto _doneH;
839 if (s<0 || s>=tb->nsections) {
840 s = (dir? tb->nsections-1 : 0);
843 _doneV:
844 /* we have the line, which TextBlock on that line is it? */
845 pos = 0;
846 if (tPtr->flags.monoFont && tb->graphic)
847 tb = getFirstNonGraphicBlockFor(tb, dir);
848 if (tb) {
849 if ((dir? tb->sections[s].x >= x : tb->sections[s].x < x))
850 goto _doneH;
852 #if 0
853 if(tb->blank) {
854 _w = 0;
855 printf("blank\n");
856 } else {
857 text = &(tb->text[tb->sections[s].begin]);
858 len = tb->sections[s].end - tb->sections[s].begin;
859 _w = WMWidthOfString(tb->d.font, text, len);
861 printf("here %d %d \n", tb->sections[s].x + _w, x);
862 if ((dir? tb->sections[s].x + _w < x : tb->sections[s].x + _w >= x)) {
863 pos = tb->sections[s].end;
864 tPtr->cursor.x = tb->sections[s].x + _w;
865 goto _doneH;
867 #endif
868 _y = tb->sections[s]._y;
871 while (tb) {
873 if (tPtr->flags.monoFont && tb->graphic) {
874 tb = (dir ? tb->next : tb->prior);
875 continue;
878 if (dir) {
879 if (tb->graphic) {
880 if(tb->object)
881 _w = WMWidgetWidth(tb->d.widget);
882 else
883 _w = tb->d.pixmap->width;
884 } else {
885 text = &(tb->text[tb->sections[s].begin]);
886 len = tb->sections[s].end - tb->sections[s].begin;
887 _w = WMWidthOfString(tb->d.font, text, len);
888 if (tb->sections[s].x + _w >= x)
889 break;
892 } else {
893 if (tb->sections[s].x <= x)
894 break;
897 if ((dir? tb->next : tb->prior)) {
898 TextBlock *nxt = (dir? tb->next : tb->prior);
899 if (tPtr->flags.monoFont && nxt->graphic) {
900 nxt = getFirstNonGraphicBlockFor(nxt, dir);
901 if (!nxt) {
902 pos = 0;
903 tPtr->cursor.x = tb->sections[s].x;
904 goto _doneH;
908 if (_y != nxt->sections[0]._y) {
909 /* this must be the last/first on this line. stop */
910 pos = (dir? tb->sections[s].end : 0);
911 tPtr->cursor.x = tb->sections[s].x;
912 if (!tb->blank) {
913 if (tb->graphic) {
914 if(tb->object)
915 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
916 else
917 tPtr->cursor.x += tb->d.pixmap->width;
918 } else if (pos > tb->sections[s].begin) {
919 tPtr->cursor.x +=
920 WMWidthOfString(tb->d.font,
921 &(tb->text[tb->sections[s].begin]),
922 pos - tb->sections[s].begin);
925 goto _doneH;
929 if ( (dir? tb->next : tb->prior)) {
930 tb = (dir ? tb->next : tb->prior);
931 } else {
932 done = True;
933 break;
936 if (tb)
937 s = (dir? 0 : tb->nsections-1);
940 /* we have said TextBlock, now where within it? */
941 if (tb && !tb->graphic) {
942 WMFont *f = tb->d.font;
943 len = tb->sections[s].end - tb->sections[s].begin;
944 text = &(tb->text[tb->sections[s].begin]);
946 _w = x - tb->sections[s].x;
947 pos = 0;
949 while (pos<len && WMWidthOfString(f, text, pos+1) < _w)
950 pos++;
952 tPtr->cursor.x = tb->sections[s].x +
953 (pos? WMWidthOfString(f, text, pos) : 0);
955 pos += tb->sections[s].begin;
956 _doneH:
957 tPtr->tpos = (pos<tb->used)? pos : tb->used;
960 tPtr->currentTextBlock = tb;
961 tPtr->cursor.h = tb->sections[s].h;
962 tPtr->cursor.y = tb->sections[s]._y;
964 if (!tb)
965 printf("will hang :-)\n");
969 static void
970 updateScrollers(Text *tPtr)
973 if (tPtr->flags.frozen)
974 return;
976 if (tPtr->vS) {
977 if (tPtr->docHeight < tPtr->visible.h) {
978 WMSetScrollerParameters(tPtr->vS, 0, 1);
979 tPtr->vpos = 0;
980 } else {
981 float hmax = (float)(tPtr->docHeight);
982 WMSetScrollerParameters(tPtr->vS,
983 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
984 (float)tPtr->visible.h/hmax);
986 } else tPtr->vpos = 0;
988 if (tPtr->hS) {
989 if (tPtr->docWidth < tPtr->visible.w) {
990 WMSetScrollerParameters(tPtr->hS, 0, 1);
991 tPtr->hpos = 0;
992 } else {
993 float wmax = (float)(tPtr->docWidth);
994 WMSetScrollerParameters(tPtr->hS,
995 ((float)tPtr->hpos)/(wmax - (float)tPtr->visible.w),
996 (float)tPtr->visible.w/wmax);
998 } else tPtr->hpos = 0;
1001 static void
1002 scrollersCallBack(WMWidget *w, void *self)
1004 Text *tPtr = (Text *)self;
1005 Bool scroll = False;
1006 Bool dimple = False;
1007 int which;
1009 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1010 return;
1012 if (w == tPtr->vS) {
1013 int height;
1014 height = tPtr->visible.h;
1016 which = WMGetScrollerHitPart(tPtr->vS);
1017 switch(which) {
1018 case WSDecrementLine:
1019 if (tPtr->vpos > 0) {
1020 if (tPtr->vpos>16) tPtr->vpos-=16;
1021 else tPtr->vpos=0;
1022 scroll=True;
1023 }break;
1024 case WSIncrementLine: {
1025 int limit = tPtr->docHeight - height;
1026 if (tPtr->vpos < limit) {
1027 if (tPtr->vpos<limit-16) tPtr->vpos+=16;
1028 else tPtr->vpos=limit;
1029 scroll = True;
1030 }}break;
1031 case WSDecrementPage:
1032 tPtr->vpos -= height;
1034 if (tPtr->vpos < 0)
1035 tPtr->vpos = 0;
1036 dimple = True;
1037 scroll = True;
1038 printf("dimple needs to jump to mouse location ;-/\n");
1039 break;
1040 case WSIncrementPage:
1041 tPtr->vpos += height;
1042 if (tPtr->vpos > (tPtr->docHeight - height))
1043 tPtr->vpos = tPtr->docHeight - height;
1044 dimple = True;
1045 scroll = True;
1046 printf("dimple needs to jump to mouse location ;-/\n");
1047 break;
1050 case WSKnob:
1051 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1052 * (float)(tPtr->docHeight - height);
1053 scroll = True;
1054 break;
1056 case WSKnobSlot:
1057 case WSNoPart:
1058 printf("WSNoPart, WSKnobSlot\n");
1059 #if 0
1060 float hmax = (float)(tPtr->docHeight);
1061 ((float)tPtr->vpos)/(hmax - (float)tPtr->visible.h),
1062 (float)tPtr->visible.h/hmax;
1063 dimple =where mouse is.
1064 #endif
1065 break;
1067 scroll = (tPtr->vpos != tPtr->prevVpos);
1068 tPtr->prevVpos = tPtr->vpos;
1071 if (w == tPtr->hS) {
1072 int width = tPtr->visible.w;
1074 which = WMGetScrollerHitPart(tPtr->hS);
1075 switch(which) {
1076 case WSDecrementLine:
1077 if (tPtr->hpos > 0) {
1078 if (tPtr->hpos>16) tPtr->hpos-=16;
1079 else tPtr->hpos=0;
1080 scroll=True;
1081 }break;
1082 case WSIncrementLine: {
1083 int limit = tPtr->docWidth - width;
1084 if (tPtr->hpos < limit) {
1085 if (tPtr->hpos<limit-16) tPtr->hpos+=16;
1086 else tPtr->hpos=limit;
1087 scroll = True;
1088 }}break;
1089 case WSDecrementPage:
1090 tPtr->hpos -= width;
1092 if (tPtr->hpos < 0)
1093 tPtr->hpos = 0;
1094 dimple = True;
1095 scroll = True;
1096 printf("dimple needs to jump to mouse location ;-/\n");
1097 break;
1098 case WSIncrementPage:
1099 tPtr->hpos += width;
1100 if (tPtr->hpos > (tPtr->docWidth - width))
1101 tPtr->hpos = tPtr->docWidth - width;
1102 dimple = True;
1103 scroll = True;
1104 printf("dimple needs to jump to mouse location ;-/\n");
1105 break;
1108 case WSKnob:
1109 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1110 * (float)(tPtr->docWidth - width);
1111 scroll = True;
1112 break;
1114 case WSKnobSlot:
1115 case WSNoPart:
1116 printf("WSNoPart, WSKnobSlot\n");
1117 #if 0
1118 float wmax = (float)(tPtr->docWidth);
1119 ((float)tPtr->vpos)/(wmax - (float)tPtr->visible.w),
1120 (float)tPtr->visible.w/wmax;
1121 dimple =where mouse is.
1122 #endif
1123 break;
1125 scroll = (tPtr->hpos != tPtr->prevHpos);
1126 tPtr->prevHpos = tPtr->hpos;
1129 if (scroll) {
1131 if (0&&dimple) {
1132 if (tPtr->rulerShown)
1133 XClearArea(tPtr->view->screen->display, tPtr->view->window, 22, 47,
1134 tPtr->view->size.width-24, tPtr->view->size.height-49, True);
1135 else
1136 XClearArea(tPtr->view->screen->display, tPtr->view->window, 22, 2,
1137 tPtr->view->size.width-24, tPtr->view->size.height-4, True);
1140 if (dimple || which == WSDecrementLine || which == WSIncrementLine)
1141 updateScrollers(tPtr);
1142 paintText(tPtr);
1148 typedef struct {
1149 TextBlock *tb;
1150 unsigned short begin, end; /* what part of the text block */
1151 } myLineItems;
1154 static int
1155 layOutLine(Text *tPtr, myLineItems *items, int nitems, int x, int y)
1157 int i, j=0, lw = 0, line_height=0, max_d=0, len, n;
1158 WMFont *font;
1159 char *text;
1160 TextBlock *tb;
1161 TextBlock *tbsame=NULL;
1163 for(i=0; i<nitems; i++) {
1164 tb = items[i].tb;
1166 if (tb->graphic) {
1167 if (!tPtr->flags.monoFont) {
1168 if(tb->object) {
1169 WMWidget *wdt = tb->d.widget;
1170 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1171 if (tPtr->flags.alignment != WALeft)
1172 lw += WMWidgetWidth(wdt);
1173 } else {
1174 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1175 if (tPtr->flags.alignment != WALeft)
1176 lw += tb->d.pixmap->width;
1180 } else {
1181 font = (tPtr->flags.monoFont)?tPtr->dFont : tb->d.font;
1182 max_d = WMAX(max_d, abs(font->height-font->y));
1183 line_height = WMAX(line_height, font->height + max_d);
1184 text = &(tb->text[items[i].begin]);
1185 len = items[i].end - items[i].begin;
1186 if (tPtr->flags.alignment != WALeft)
1187 lw += WMWidthOfString(font, text, len);
1191 if (tPtr->flags.alignment == WARight) {
1192 j = tPtr->visible.w - lw;
1193 } else if (tPtr->flags.alignment == WACenter) {
1194 j = (int) ((float)(tPtr->visible.w - lw))/2.0;
1197 for(i=0; i<nitems; i++) {
1198 tb = items[i].tb;
1200 if (tbsame == tb) { /*extend it, since it's on same line */
1201 tb->sections[tb->nsections-1].end = items[i].end;
1202 n = tb->nsections-1;
1203 } else {
1204 tb->sections = wrealloc(tb->sections,
1205 (++tb->nsections)*sizeof(Section));
1206 n = tb->nsections-1;
1207 tb->sections[n]._y = y + max_d;
1208 tb->sections[n].x = x+j;
1209 tb->sections[n].h = line_height;
1210 tb->sections[n].begin = items[i].begin;
1211 tb->sections[n].end = items[i].end;
1213 if (tb->graphic && tb->object) {
1214 tb->sections[n].x += tPtr->visible.x;
1215 tb->sections[n].y += tPtr->visible.y;
1219 tb->sections[n].last = (i+1 == nitems);
1221 if (tb->graphic) {
1222 if (!tPtr->flags.monoFont) {
1223 if(tb->object) {
1224 WMWidget *wdt = tb->d.widget;
1225 tb->sections[n].y = max_d + y
1226 + line_height - WMWidgetHeight(wdt);
1227 tb->sections[n].w = WMWidgetWidth(wdt);
1228 } else {
1229 tb->sections[n].y = y + max_d;
1230 tb->sections[n].w = tb->d.pixmap->width;
1232 x += tb->sections[n].w;
1234 } else {
1235 font = (tPtr->flags.monoFont)? tPtr->dFont : tb->d.font;
1236 len = items[i].end - items[i].begin;
1237 text = &(tb->text[items[i].begin]);
1239 tb->sections[n].y = y+line_height-font->y;
1240 tb->sections[n].w =
1241 WMWidthOfString(font,
1242 &(tb->text[tb->sections[n].begin]),
1243 tb->sections[n].end - tb->sections[n].begin);
1245 x += WMWidthOfString(font, text, len);
1248 tbsame = tb;
1251 return line_height;
1256 static void
1257 output(char *ptr, int len)
1259 char s[len+1];
1260 memcpy(s, ptr, len);
1261 s[len] = 0;
1262 //printf(" s is [%s] (%d)\n", s, strlen(s));
1263 printf("[%s]\n", s);
1267 /* tb->text doesn't necessarily end in '\0' hmph! (strchr) */
1268 static inline char *
1269 mystrchr(char *s, char needle, unsigned short len)
1271 char *haystack = s;
1273 if (!haystack || len < 1)
1274 return NULL;
1276 while ( (int) (haystack - s) < len ) {
1277 if (*haystack == needle)
1278 return haystack;
1279 haystack++;
1281 return NULL;
1284 static int
1285 mystrcasecmp(const unsigned char *s1, const unsigned char *s2)
1287 if (!*s1 || !*s2)
1288 return 0;
1290 while (*s2 != '\0') {
1291 if (TOLOWER (*s1) != TOLOWER (*s2)) /* true if *s1 == 0 ! */
1292 return 0;
1293 s1++;
1294 s2++;
1296 return (*s1=='\0' || !ISALNUM(*s1))?1:0;
1300 static void
1301 layOutDocument(Text *tPtr)
1303 TextBlock *tb;
1304 myLineItems items[MAX_TB_PER_LINE];
1305 WMFont *font;
1306 Bool lhc = !tPtr->flags.laidOut; /* line height changed? */
1307 int prev_y, nitems=0, x=0, y=0, lw = 0, width=0;
1309 char *start=NULL, *mark=NULL;
1310 int begin, end;
1312 if (tPtr->flags.frozen)
1313 return;
1315 if (!(tb = tPtr->firstTextBlock))
1316 return;
1318 tPtr->docWidth = tPtr->visible.w;
1320 if (0&&tPtr->flags.laidOut) {
1321 tb = tPtr->currentTextBlock;
1322 if (tb->sections && tb->nsections>0)
1323 prev_y = tb->sections[tb->nsections-1]._y;
1324 y+=10;
1325 printf("1 prev_y %d \n", prev_y);
1327 /* search backwards for textblocks on same line */
1328 while (tb) {
1329 if (!tb->sections || tb->nsections<1) {
1330 tb = tPtr->firstTextBlock;
1331 break;
1333 if (tb->sections[tb->nsections-1]._y != prev_y) {
1334 tb = tb->next;
1335 break;
1337 // prev_y = tb->sections[tb->nsections-1]._y;
1338 tb = tb->prior;
1340 y = 0;//tb->sections[tb->nsections-1]._y;
1341 printf("2 prev_y %d \n\n", tb->sections[tb->nsections-1]._y);
1345 while (tb) {
1347 if (tb->sections && tb->nsections>0) {
1348 wfree(tb->sections);
1349 tb->sections = NULL;
1350 tb->nsections = 0;
1353 if (tb->first) {
1354 y += layOutLine(tPtr, items, nitems, x, y);
1355 x = 0;//tb->margins.first;
1356 nitems = 0;
1357 lw = 0;
1360 if (tb->graphic) {
1361 if (!tPtr->flags.monoFont) {
1362 if(tb->object)
1363 width = WMWidgetWidth(tb->d.widget);
1364 else
1365 width = tb->d.pixmap->width;
1367 if (width > tPtr->docWidth) { //tPtr->visible.w) {
1368 printf("rescale graphix to fit?\n");
1369 printf("%d %d\n", width, tPtr->visible.w);
1370 tPtr->docWidth = width;//tPtr->visible.w + (width-tPtr->visible.w);
1373 lw += width;
1374 if (lw >= tPtr->visible.w - x
1375 || nitems >= MAX_TB_PER_LINE) {
1376 y += layOutLine(tPtr, items, nitems, x, y);
1377 nitems = 0;
1378 x = 0;//tb->margins.first;
1379 lw = width;
1382 items[nitems].tb = tb;
1383 items[nitems].begin = 0;
1384 items[nitems].end = 0;
1385 nitems++;
1388 } else if ((start = tb->text)) {
1389 begin = end = 0;
1390 font = tPtr->flags.monoFont?tPtr->dFont:tb->d.font;
1392 while (start) {
1393 mark = mystrchr(start, ' ', tb->used);
1394 if (mark) {
1395 end += (int)(mark-start)+1;
1396 start = mark+1;
1397 } else {
1398 end += strlen(start);
1399 start = mark;
1402 if (end > tb->used)
1403 end = tb->used;
1405 if (end-begin > 0) {
1407 width = WMWidthOfString(font,
1408 &tb->text[begin], end-begin);
1410 if (width > tPtr->visible.w) { /* break this tb up */
1411 char *t = &tb->text[begin];
1412 int l=end-begin, i=0;
1413 do {
1414 width = WMWidthOfString(font, t, ++i);
1415 } while (width < tPtr->visible.w && i < l);
1416 end = begin+i;
1417 if (start) // and since (nil)-4 = 0xfffffffd
1418 start -= l-i;
1421 lw += width;
1424 if ((lw >= tPtr->visible.w - x)
1425 || nitems >= MAX_TB_PER_LINE) {
1426 y += layOutLine(tPtr, items, nitems, x, y);
1427 lw = width;
1428 x = 0;//tb->margins.first;
1429 nitems = 0;
1432 items[nitems].tb = tb;
1433 items[nitems].begin = begin;
1434 items[nitems].end = end;
1435 nitems++;
1437 begin = end;
1440 tb = tb->next;
1444 if (nitems > 0)
1445 y += layOutLine(tPtr, items, nitems, x, y);
1446 if (lhc) {
1447 tPtr->docHeight = y+10;
1448 updateScrollers(tPtr);
1451 if(tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1452 XEvent event;
1454 tPtr->flags.horizOnDemand = True;
1455 WMSetTextHasHorizontalScroller((WMText*)tPtr, True);
1456 event.type = Expose;
1457 handleEvents(&event, (void *)tPtr);
1459 } else if(tPtr->docWidth <= tPtr->visible.w
1460 && tPtr->hS && tPtr->flags.horizOnDemand ) {
1461 tPtr->flags.horizOnDemand = False;
1462 WMSetTextHasHorizontalScroller((WMText*)tPtr, False);
1464 tPtr->flags.laidOut = True;
1469 static void
1470 textDidResize(W_ViewDelegate *self, WMView *view)
1472 Text *tPtr = (Text *)view->self;
1473 unsigned short w = tPtr->view->size.width;
1474 unsigned short h = tPtr->view->size.height;
1475 unsigned short rh = 0, vw = 0;
1477 if (tPtr->ruler && tPtr->flags.rulerShown) {
1478 WMMoveWidget(tPtr->ruler, 2, 2);
1479 WMResizeWidget(tPtr->ruler, w - 4, 40);
1480 rh = 40;
1483 if (tPtr->vS) {
1484 WMMoveWidget(tPtr->vS, 1, rh + 1);
1485 WMResizeWidget(tPtr->vS, 20, h - rh - 2);
1486 vw = 20;
1487 WMSetRulerOffset(tPtr->ruler,22);
1488 } else WMSetRulerOffset(tPtr->ruler, 2);
1490 if (tPtr->hS) {
1491 if (tPtr->vS) {
1492 WMMoveWidget(tPtr->hS, vw, h - 21);
1493 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1494 } else {
1495 WMMoveWidget(tPtr->hS, vw+1, h - 21);
1496 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1500 tPtr->visible.x = (tPtr->vS)?21:1;
1501 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown)?43:3;
1502 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 2;
1503 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1504 tPtr->visible.h -= (tPtr->hS)?20:0;
1505 tPtr->margins[0].right = tPtr->visible.w;
1507 if (tPtr->view->flags.realized) {
1509 if (tPtr->db) {
1510 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1511 tPtr->db = (Pixmap) NULL;
1514 if (tPtr->visible.w < 40)
1515 tPtr->visible.w = 40;
1516 if (tPtr->visible.h < 20)
1517 tPtr->visible.h = 20;
1519 //if (size change or !db
1520 if(!tPtr->db) {
1521 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1522 tPtr->view->window, tPtr->visible.w,
1523 tPtr->visible.h, tPtr->view->screen->depth);
1527 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1530 W_ViewDelegate _TextViewDelegate =
1532 NULL,
1533 NULL,
1534 textDidResize,
1535 NULL,
1538 /* nice, divisble-by-16 blocks */
1539 static inline unsigned short
1540 reqBlockSize(unsigned short requested)
1542 return requested + 16 - (requested%16);
1546 static void
1547 clearText(Text *tPtr)
1549 if (!tPtr->firstTextBlock)
1550 return;
1552 while (tPtr->currentTextBlock)
1553 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1555 tPtr->firstTextBlock = NULL;
1556 tPtr->currentTextBlock = NULL;
1557 tPtr->lastTextBlock = NULL;
1560 static void
1561 deleteTextInteractively(Text *tPtr, KeySym ksym)
1563 TextBlock *tb = tPtr->currentTextBlock;
1564 Bool back = (Bool) (ksym == XK_BackSpace);
1565 Bool done = 1;
1567 if (!tPtr->flags.editable || tPtr->flags.buttonHeld) {
1568 XBell(tPtr->view->screen->display, 0);
1569 return;
1572 if (!tb)
1573 return;
1575 tPtr->flags.needsRefresh = True;
1577 if (tPtr->flags.ownsSelection) {
1578 removeSelection(tPtr);
1579 return;
1582 if (back && tPtr->tpos < 1) {
1583 if (tb->prior) {
1584 tb = tb->prior;
1585 tb->first = False;
1586 tPtr->tpos = tb->used;
1587 tPtr->currentTextBlock = tb;
1588 done = 1;
1592 if ( (tb->used > 0) && ((back?tPtr->tpos > 0:1))
1593 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1594 if (back)
1595 tPtr->tpos--;
1596 memmove(&(tb->text[tPtr->tpos]),
1597 &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1598 tb->used--;
1599 done = 0;
1602 if ( (back? (tPtr->tpos < 1 && !done) : ( tPtr->tpos >= tb->used))
1603 || tb->graphic) {
1605 TextBlock *sibling = (back? tb->prior : tb->next);
1607 if(tb->used == 0 || tb->graphic)
1608 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1610 if (sibling) {
1611 tPtr->currentTextBlock = sibling;
1612 tPtr->tpos = (back? sibling->used : 0);
1616 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1620 static void
1621 insertTextInteractively(Text *tPtr, char *text, int len)
1623 TextBlock *tb;
1624 char *newline = NULL;
1626 if (!tPtr->flags.editable || tPtr->flags.buttonHeld) {
1627 XBell(tPtr->view->screen->display, 0);
1628 return;
1631 #if 0
1632 if(*text == 'c') {
1633 WMColor *color = WMCreateNamedColor(W_VIEW_SCREEN(tPtr->view),
1634 "Blue", True);
1635 WMSetTextSelectionColor(tPtr, color);
1636 return;
1638 #endif
1640 if (len < 1 || !text)
1641 return;
1644 if(tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1645 return;
1647 if (tPtr->flags.ownsSelection)
1648 removeSelection(tPtr);
1650 tPtr->flags.needsRefresh = True;
1652 if (tPtr->flags.ignoreNewLine) {
1653 int i;
1654 for(i=0; i<len; i++) {
1655 if (text[i] == '\n')
1656 text[i] = ' ';
1660 tb = tPtr->currentTextBlock;
1661 if (!tb || tb->graphic) {
1662 text[len] = 0;
1663 WMAppendTextStream(tPtr, text);
1664 if (tPtr->currentTextBlock) {
1665 tPtr->tpos = tPtr->currentTextBlock->used;
1667 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1668 return;
1671 if ((newline = strchr(text, '\n'))) {
1672 int nlen = (int)(newline-text);
1673 int s = tb->used - tPtr->tpos;
1674 char save[s];
1676 if (!tb->blank && nlen>0) {
1677 if (s > 0) {
1678 memcpy(save, &tb->text[tPtr->tpos], s);
1679 tb->used -= (tb->used - tPtr->tpos);
1681 text[nlen] = 0;
1682 insertTextInteractively(tPtr, text, nlen);
1683 newline++;
1684 WMAppendTextStream(tPtr, newline);
1685 if (s>0)
1686 insertTextInteractively(tPtr, save, s);
1688 } else {
1689 if (tPtr->tpos>0 && tPtr->tpos < tb->used
1690 && !tb->graphic && tb->text) {
1692 void *ntb = WMCreateTextBlockWithText(
1693 tPtr, &tb->text[tPtr->tpos],
1694 tb->d.font, tb->color, True, tb->used - tPtr->tpos);
1695 tb->used = tPtr->tpos;
1696 WMAppendTextBlock(tPtr, ntb);
1697 tPtr->tpos = 0;
1698 } else if (tPtr->tpos == tb->used || tPtr->tpos == 0) {
1699 void *ntb = WMCreateTextBlockWithText(tPtr,
1700 NULL, tb->d.font, tb->color, True, 0);
1702 if (tPtr->tpos>0)
1703 WMAppendTextBlock(tPtr, ntb);
1704 else
1705 WMPrependTextBlock(tPtr, ntb);
1706 tPtr->tpos = 1;
1710 } else {
1712 if (tb->used + len >= tb->allocated) {
1713 tb->allocated = reqBlockSize(tb->used+len);
1714 tb->text = wrealloc(tb->text, tb->allocated);
1717 if (tb->blank) {
1718 memcpy(tb->text, text, len);
1719 tb->used = len;
1720 tPtr->tpos = len;
1721 tb->blank = False;
1722 } else {
1723 memmove(&(tb->text[tPtr->tpos+len]), &tb->text[tPtr->tpos],
1724 tb->used-tPtr->tpos+1);
1725 memmove(&tb->text[tPtr->tpos], text, len);
1726 tb->used += len;
1727 tPtr->tpos += len;
1732 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1736 static void
1737 selectRegion(Text *tPtr, int x, int y)
1740 if (x < 0 || y < 0)
1741 return;
1743 y += (tPtr->flags.rulerShown? 40: 0);
1744 y += tPtr->vpos;
1745 if (y>10)
1746 y -= 10; /* the original offset */
1748 x -= tPtr->visible.x-2;
1749 if (x<0)
1750 x=0;
1752 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
1753 tPtr->sel.w = abs(tPtr->clicked.x - x);
1754 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
1755 tPtr->sel.h = abs(tPtr->clicked.y - y);
1757 tPtr->flags.ownsSelection = True;
1758 paintText(tPtr);
1762 static void
1763 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
1764 void *cdata, WMData *data)
1766 Text *tPtr = (Text *)view->self;
1767 char *str;
1769 tPtr->flags.waitingForSelection = False;
1770 if (data) {
1771 str = (char*)WMDataBytes(data);
1772 if (0&&tPtr->parser) {
1773 /* parser is not yet well behaved to do this properly..*/
1774 (tPtr->parser) (tPtr, (void *) str);
1775 } else {
1776 insertTextInteractively(tPtr, str, strlen(str));
1778 } else {
1779 int n;
1780 str = XFetchBuffer(tPtr->view->screen->display, &n, 0);
1781 if (str) {
1782 str[n] = 0;
1783 if (0&&tPtr->parser) {
1784 /* parser is not yet well behaved to do this properly..*/
1785 (tPtr->parser) (tPtr, (void *) str);
1786 } else {
1787 insertTextInteractively(tPtr, str, n);
1789 XFree(str);
1795 static void
1796 releaseSelection(Text *tPtr)
1798 TextBlock *tb = tPtr->firstTextBlock;
1800 while(tb) {
1801 tb->selected = False;
1802 tb = tb->next;
1804 tPtr->flags.ownsSelection = False;
1805 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY,
1806 CurrentTime);
1808 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
1811 static WMData *
1812 requestHandler(WMView *view, Atom selection, Atom target,
1813 void *cdata, Atom *type)
1815 Text *tPtr = view->self;
1816 Display *dpy = tPtr->view->screen->display;
1817 Atom TEXT = XInternAtom(dpy, "TEXT", False);
1818 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
1819 Atom _TARGETS;
1820 WMData *data;
1822 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
1823 char *text = WMGetTextSelected(tPtr);
1825 if(text) {
1826 WMData *data = WMCreateDataWithBytes(text, strlen(text));
1827 WMSetDataFormat(data, 8);
1828 wfree(text);
1829 *type = target;
1830 return data;
1833 } else if(target == XInternAtom(dpy, "PIXMAP", False)) {
1834 data = WMCreateDataWithBytes("paste a pixmap", 14);
1835 WMSetDataFormat(data, 8);
1836 *type = target;
1837 return data;
1840 _TARGETS = XInternAtom(dpy, "TARGETS", False);
1841 if (target == _TARGETS) {
1842 Atom *ptr;
1844 ptr = wmalloc(4 * sizeof(Atom));
1845 ptr[0] = _TARGETS;
1846 ptr[1] = XA_STRING;
1847 ptr[2] = TEXT;
1848 ptr[3] = COMPOUND_TEXT;
1850 data = WMCreateDataWithBytes(ptr, 4*4);
1851 WMSetDataFormat(data, 32);
1853 *type = target;
1854 return data;
1857 return NULL;
1861 static void
1862 lostHandler(WMView *view, Atom selection, void *cdata)
1864 releaseSelection((WMText *)view->self);
1867 static WMSelectionProcs selectionHandler = {
1868 requestHandler, lostHandler, NULL
1871 static void
1872 ownershipObserver(void *observerData, WMNotification *notification)
1874 WMText *to = (WMText *)observerData;
1875 WMText *tw = (WMText *)WMGetNotificationClientData(notification);
1876 if (to != tw)
1877 lostHandler(to->view, XA_PRIMARY, NULL);
1880 static void
1881 fontChanged(void *observerData, WMNotification *notification)
1883 WMText *tPtr = (WMText *) observerData;
1884 WMFont *font = (WMFont *)WMGetNotificationClientData(notification);
1885 printf("fontChanged\n");
1887 if(!tPtr || !font)
1888 return;
1890 if (tPtr->flags.ownsSelection)
1891 WMSetTextSelectionFont(tPtr, font);
1894 static void
1895 handleTextKeyPress(Text *tPtr, XEvent *event)
1897 char buffer[2];
1898 KeySym ksym;
1899 int control_pressed = False;
1900 // int h=1;
1902 if (((XKeyEvent *) event)->state & ControlMask)
1903 control_pressed = True;
1904 buffer[XLookupString(&event->xkey, buffer, 1, &ksym, NULL)] = 0;
1906 switch(ksym) {
1908 case XK_Right:
1909 WMScrollText(tPtr, -14);
1910 case XK_Left: {
1911 //TextBlock *tb = tPtr->currentTextBlock;
1912 //int x = tPtr->cursor.x + tPtr->visible.x;
1913 //int y = tPtr->visible.y + tPtr->cursor.y + tPtr->cursor.h;
1915 #if 0
1916 if(!tb)
1917 break;
1918 if(tb->graphic) {
1919 if(tb->object) {
1920 w = WMWidgetWidth(tb->d.widget);
1921 } else {
1922 w = tb->d.pixmap->width;
1924 } else {
1925 pos = tPtr->tpos;
1926 w = WMWidthOfString(tb->d.font, &tb->text[pos], 1);
1929 cursorToTextPosition(tPtr, w + tPtr->cursor.x + tPtr->visible.x,
1930 3 + tPtr->visible.y + tPtr->cursor.y
1931 + tPtr->cursor.h - tPtr->vpos);
1932 if(x == tPtr->cursor.x + tPtr->visible.x) {
1933 printf("same %d %d\n", x, tPtr->cursor.x + tPtr->visible.x);
1934 cursorToTextPosition(tPtr, tPtr->visible.x,
1935 3 + tPtr->visible.y + tPtr->cursor.y + tPtr->cursor.h);
1937 paintText(tPtr);
1938 #endif
1940 break;
1942 case XK_Down:
1943 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
1944 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
1945 paintText(tPtr);
1946 break;
1948 case XK_Up:
1949 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
1950 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
1951 paintText(tPtr);
1952 break;
1954 case XK_BackSpace:
1955 case XK_Delete:
1956 case XK_KP_Delete:
1957 deleteTextInteractively(tPtr, ksym);
1958 break;
1960 case XK_Control_R :
1961 case XK_Control_L :
1962 control_pressed = True;
1963 break;
1965 case XK_Return:
1966 buffer[0] = '\n';
1967 default:
1968 if (buffer[0] != 0 && !control_pressed) {
1969 insertTextInteractively(tPtr, buffer, 1);
1971 } else if (control_pressed && ksym==XK_r) {
1972 Bool i = !tPtr->flags.rulerShown;
1973 WMShowTextRuler(tPtr, i);
1974 tPtr->flags.rulerShown = i;
1976 else if (control_pressed && buffer[0] == '\a')
1977 XBell(tPtr->view->screen->display, 0);
1980 if (!control_pressed && tPtr->flags.ownsSelection)
1981 releaseSelection(tPtr);
1984 static void
1985 handleWidgetPress(XEvent *event, void *data)
1987 TextBlock *tb = (TextBlock *)data;
1988 Text *tPtr;
1989 WMWidget *w;
1991 if (!tb)
1992 return;
1993 /* this little bit of nastiness here saves a boatload of trouble */
1994 w = (WMWidget *)(((W_VIEW(tb->d.widget))->parent)->self);
1995 if (W_CLASS(w) != WC_Text)
1996 return;
1997 tPtr = (Text*)w;
1998 tPtr->currentTextBlock = getFirstNonGraphicBlockFor(tb, 1);
1999 if (!tPtr->currentTextBlock)
2000 tPtr->currentTextBlock = tb;
2001 tPtr->tpos = 0;
2002 output(tPtr->currentTextBlock->text, tPtr->currentTextBlock->used);
2003 //if (!tPtr->flags.focused) {
2004 // WMSetFocusToWidget(tPtr);
2005 // tPtr->flags.focused = True;
2006 //}
2010 static void
2011 handleActionEvents(XEvent *event, void *data)
2013 Text *tPtr = (Text *)data;
2014 Display *dpy = event->xany.display;
2015 KeySym ksym;
2018 switch (event->type) {
2019 case KeyPress:
2020 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2021 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2022 tPtr->flags.extendSelection = True;
2023 return;
2026 if (tPtr->flags.waitingForSelection)
2027 return;
2028 if (tPtr->flags.focused) {
2029 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2030 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
2031 GrabModeAsync, GrabModeAsync, None,
2032 tPtr->view->screen->invisibleCursor, CurrentTime);
2033 tPtr->flags.pointerGrabbed = True;
2034 handleTextKeyPress(tPtr, event);
2036 } break;
2038 case KeyRelease:
2039 ksym = XLookupKeysym((XKeyEvent*)event, 0);
2040 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2041 tPtr->flags.extendSelection = False;
2042 return;
2043 //end modify flag so selection can be extended
2045 break;
2048 case MotionNotify:
2049 if (tPtr->flags.pointerGrabbed) {
2050 tPtr->flags.pointerGrabbed = False;
2051 XUngrabPointer(dpy, CurrentTime);
2054 if ((event->xmotion.state & Button1Mask)) {
2055 if (!tPtr->flags.ownsSelection) {
2056 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY,
2057 event->xbutton.time, &selectionHandler, NULL);
2058 tPtr->flags.ownsSelection = True;
2060 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2062 break;
2065 case ButtonPress:
2066 tPtr->flags.buttonHeld = True;
2067 if (tPtr->flags.extendSelection) {
2068 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2069 return;
2071 if (event->xbutton.button == Button1) {
2073 if (!tPtr->flags.focused) {
2074 WMSetFocusToWidget(tPtr);
2075 tPtr->flags.focused = True;
2078 if (tPtr->flags.ownsSelection)
2079 releaseSelection(tPtr);
2080 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2081 paintText(tPtr);
2082 if (tPtr->flags.pointerGrabbed) {
2083 tPtr->flags.pointerGrabbed = False;
2084 XUngrabPointer(dpy, CurrentTime);
2085 break;
2089 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown)
2090 WMScrollText(tPtr, -16);
2091 else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp)
2092 WMScrollText(tPtr, 16);
2093 break;
2095 case ButtonRelease:
2096 tPtr->flags.buttonHeld = False;
2097 if (tPtr->flags.pointerGrabbed) {
2098 tPtr->flags.pointerGrabbed = False;
2099 XUngrabPointer(dpy, CurrentTime);
2100 break;
2102 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown
2103 || event->xbutton.button == WINGsConfiguration.mouseWheelUp)
2104 break;
2106 if (event->xbutton.button == Button2) {
2107 char *text = NULL;
2108 int n;
2110 if (!tPtr->flags.editable) {
2111 XBell(dpy, 0);
2112 break;
2115 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2116 event->xbutton.time, pasteText, NULL)) {
2117 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2118 if (text) {
2119 text[n] = 0;
2120 if (0&&tPtr->parser) {
2121 /* parser is not yet well behaved to do this properly..*/
2122 (tPtr->parser) (tPtr, (void *) text);
2123 } else {
2124 insertTextInteractively(tPtr, text, n-1);
2126 XFree(text);
2127 } else tPtr->flags.waitingForSelection = True;
2130 break;
2137 static void
2138 handleEvents(XEvent *event, void *data)
2140 Text *tPtr = (Text *)data;
2142 switch(event->type) {
2143 case Expose:
2145 if (event->xexpose.count!=0)
2146 break;
2148 if(tPtr->hS) {
2149 if (!(W_VIEW(tPtr->hS))->flags.realized)
2150 WMRealizeWidget(tPtr->hS);
2151 if (!((W_VIEW(tPtr->hS))->flags.mapped))
2152 WMMapWidget(tPtr->hS);
2155 if(tPtr->vS) {
2156 if (!(W_VIEW(tPtr->vS))->flags.realized)
2157 WMRealizeWidget(tPtr->vS);
2158 if (!((W_VIEW(tPtr->vS))->flags.mapped))
2159 WMMapWidget(tPtr->vS);
2162 if(tPtr->ruler) {
2163 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2164 WMRealizeWidget(tPtr->ruler);
2166 if (!((W_VIEW(tPtr->ruler))->flags.mapped)
2167 && tPtr->flags.rulerShown)
2168 WMMapWidget(tPtr->ruler);
2171 if(!tPtr->db)
2172 textDidResize(tPtr->view->delegate, tPtr->view);
2174 paintText(tPtr);
2175 break;
2177 case FocusIn:
2178 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2179 != tPtr->view)
2180 return;
2181 tPtr->flags.focused = True;
2182 #if DO_BLINK
2183 if (tPtr->flags.editable && !tPtr->timerID) {
2184 tPtr->timerID = WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY,
2185 blinkCursor, tPtr);
2187 #endif
2189 break;
2191 case FocusOut:
2192 tPtr->flags.focused = False;
2193 paintText(tPtr);
2194 #if DO_BLINK
2195 if (tPtr->timerID) {
2196 WMDeleteTimerHandler(tPtr->timerID);
2197 tPtr->timerID = NULL;
2199 #endif
2200 break;
2202 case DestroyNotify:
2203 clearText(tPtr);
2204 if(tPtr->hS)
2205 WMDestroyWidget(tPtr->hS);
2206 if(tPtr->vS)
2207 WMDestroyWidget(tPtr->vS);
2208 if(tPtr->ruler)
2209 WMDestroyWidget(tPtr->ruler);
2210 if(tPtr->db)
2211 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2212 if(tPtr->gfxItems)
2213 WMFreeBag(tPtr->gfxItems);
2214 #if DO_BLINK
2215 if (tPtr->timerID)
2216 WMDeleteTimerHandler(tPtr->timerID);
2217 #endif
2218 WMReleaseFont(tPtr->dFont);
2219 WMReleaseColor(tPtr->dColor);
2220 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2221 WMRemoveNotificationObserver(tPtr);
2223 wfree(tPtr);
2225 break;
2231 static void
2232 insertPlainText(WMText *tPtr, char *text)
2234 char *start, *mark;
2235 void *tb = NULL;
2237 if (!text) {
2238 clearText(tPtr);
2239 return;
2242 start = text;
2243 while (start) {
2244 mark = strchr(start, '\n');
2245 if (mark) {
2246 tb = WMCreateTextBlockWithText(tPtr,
2247 start, tPtr->dFont,
2248 tPtr->dColor, True, (int)(mark-start));
2249 start = mark+1;
2250 } else {
2251 if (start && strlen(start)) {
2252 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2253 tPtr->dColor, False, strlen(start));
2254 } else tb = NULL;
2255 start = mark;
2258 if (tPtr->flags.prepend)
2259 WMPrependTextBlock(tPtr, tb);
2260 else
2261 WMAppendTextBlock(tPtr, tb);
2263 return;
2268 static void
2269 rulerMoveCallBack(WMWidget *w, void *self)
2271 Text *tPtr = (Text *)self;
2272 if (!tPtr)
2273 return;
2274 if (W_CLASS(tPtr) != WC_Text)
2275 return;
2277 paintText(tPtr);
2281 static void
2282 rulerReleaseCallBack(WMWidget *w, void *self)
2284 Text *tPtr = (Text *)self;
2285 if (!tPtr)
2286 return;
2287 if (W_CLASS(tPtr) != WC_Text)
2288 return;
2290 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
2291 return;
2295 #if 0
2296 static unsigned
2297 draggingEntered(WMView *self, WMDraggingInfo *info)
2299 printf("draggingEntered\n");
2300 return WDOperationCopy;
2304 static unsigned
2305 draggingUpdated(WMView *self, WMDraggingInfo *info)
2307 printf("draggingUpdated\n");
2308 return WDOperationCopy;
2312 static void
2313 draggingExited(WMView *self, WMDraggingInfo *info)
2315 printf("draggingExited\n");
2318 static void
2319 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
2321 printf("draggingExited\n");
2322 return;//"bll"; //"application/X-color";
2326 static Bool
2327 performDragOperation(WMView *self, WMDraggingInfo *info) //, WMData *data)
2329 char *colorName = "Blue";// (char*)WMDataBytes(data);
2330 WMColor *color;
2331 WMText *tPtr = (WMText *)self->self;
2333 if (!tPtr)
2334 return False;
2336 if (tPtr->flags.monoFont)
2337 return False;
2339 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2340 printf("color [%s] %p\n", colorName, color);
2341 if(color) {
2342 WMSetTextSelectionColor(tPtr, color);
2343 WMReleaseColor(color);
2346 return True;
2349 static void
2350 concludeDragOperation(WMView *self, WMDraggingInfo *info)
2352 printf("concludeDragOperation\n");
2356 static WMDragDestinationProcs _DragDestinationProcs = {
2357 draggingEntered,
2358 draggingUpdated,
2359 draggingExited,
2360 prepareForDragOperation,
2361 performDragOperation,
2362 concludeDragOperation
2364 #endif
2366 static void
2367 releaseBagData(void *data)
2369 if(data)
2370 wfree(data);
2374 char *
2375 getStream(WMText *tPtr, int sel)
2377 TextBlock *tb = NULL;
2378 char *text = NULL;
2379 unsigned long length = 0, where = 0;
2381 if (!tPtr)
2382 return NULL;
2384 if (!(tb = tPtr->firstTextBlock))
2385 return NULL;
2387 /* this might be tricky to get right... not yet implemented */
2388 if (tPtr->writer) {
2389 (tPtr->writer) (tPtr, (void *) text);
2390 return text;
2394 /* first, how large a buffer would we want? */
2395 while (tb) {
2397 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2400 if (!sel || (tb->graphic && tb->selected)) {
2401 length += tb->used;
2402 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank))
2403 length += 1;
2404 if (tb->graphic)
2405 length += 2; /* field markers 0xFA and size */
2406 } else if (sel && tb->selected) {
2407 length += (tb->s_end - tb->s_begin);
2408 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank))
2409 length += 1;
2413 tb = tb->next;
2416 text = wmalloc(length+1); /* +1 for the end of string, let's be nice */
2417 tb = tPtr->firstTextBlock;
2418 while (tb) {
2420 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2423 if (!sel || (tb->graphic && tb->selected)) {
2424 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank))
2425 text[where++] = '\n';
2426 if(tb->graphic) {
2427 text[where++] = 0xFA;
2428 text[where++] = tb->used;
2430 memcpy(&text[where], tb->text, tb->used);
2431 where += tb->used;
2433 } else if (sel && tb->selected) {
2434 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank))
2435 text[where++] = '\n';
2436 memcpy(&text[where], &tb->text[tb->s_begin],
2437 tb->s_end - tb->s_begin);
2438 where += tb->s_end - tb->s_begin;
2442 tb = tb->next;
2445 text[where] = 0;
2446 return text;
2451 WMBag *
2452 getStreamIntoBag(WMText *tPtr, int sel)
2454 char *stream, *start = NULL, *fa = NULL;
2455 WMBag *bag;
2456 WMData *data;
2458 if (!tPtr)
2459 return NULL;
2462 stream = getStream(tPtr, sel);
2463 if(!stream)
2464 return NULL;
2466 bag = WMCreateBagWithDestructor(4, releaseBagData);
2468 start = stream;
2469 while (start) {
2470 fa = strchr(start, 0xFA);
2471 if (fa) {
2472 unsigned char len = *(fa+1);
2474 if(start != fa) {
2475 data = WMCreateDataWithBytes((void *)start, (int)(fa - start));
2476 WMSetDataFormat(data, 8);
2477 WMPutInBag(bag, (void *) data);
2480 data = WMCreateDataWithBytes((void *)(fa+2), len);
2481 WMSetDataFormat(data, 32);
2482 WMPutInBag(bag, (void *) data);
2483 start = fa + len + 2;
2485 } else {
2486 if (start && strlen(start)) {
2487 data = WMCreateDataWithBytes((void *)start, strlen(start));
2488 WMSetDataFormat(data, 8);
2489 WMPutInBag(bag, (void *) data);
2491 start = fa;
2495 wfree(stream);
2496 return bag;
2500 WMText *
2501 WMCreateText(WMWidget *parent)
2503 Text *tPtr = wmalloc(sizeof(Text));
2504 if (!tPtr) {
2505 printf("could not create text widget\n");
2506 return NULL;
2509 #if 0
2510 printf("sizeof:\n");
2511 printf(" TextBlock %d\n", sizeof(TextBlock));
2512 printf(" Section %d\n", sizeof(Section));
2513 printf(" WMRulerMargins %d\n", sizeof(WMRulerMargins));
2514 printf(" char * %d\n", sizeof(char *));
2515 printf(" void * %d\n", sizeof(void *));
2516 printf(" short %d\n", sizeof(short));
2517 printf(" Text %d\n", sizeof(Text));
2518 #endif
2520 memset(tPtr, 0, sizeof(Text));
2521 tPtr->widgetClass = WC_Text;
2522 tPtr->view = W_CreateView(W_VIEW(parent));
2523 if (!tPtr->view) {
2524 perror("could not create text's view\n");
2525 free(tPtr);
2526 return NULL;
2528 tPtr->view->self = tPtr;
2529 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
2530 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2531 W_ResizeView(tPtr->view, 250, 200);
2532 tPtr->bgGC = WMColorGC(tPtr->view->screen->white);
2533 tPtr->fgGC = WMColorGC(tPtr->view->screen->black);
2534 W_SetViewBackgroundColor(tPtr->view, tPtr->view->screen->white);
2536 tPtr->ruler = NULL;
2537 tPtr->vS = NULL;
2538 tPtr->hS = NULL;
2540 tPtr->dFont = WMRetainFont(tPtr->view->screen->normalFont);
2542 tPtr->dColor = WMBlackColor(tPtr->view->screen);
2544 tPtr->view->delegate = &_TextViewDelegate;
2546 #if DO_BLINK
2547 tPtr->timerID = NULL;
2548 #endif
2550 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
2551 |EnterWindowMask|LeaveWindowMask|FocusChangeMask,
2552 handleEvents, tPtr);
2554 WMCreateEventHandler(tPtr->view, ButtonReleaseMask|ButtonPressMask
2555 |KeyReleaseMask|KeyPressMask|Button1MotionMask,
2556 handleActionEvents, tPtr);
2558 WMAddNotificationObserver(ownershipObserver, tPtr, "_lostOwnership", tPtr);
2560 #if 0
2561 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
2563 char *types[2] = {"application/X-color", NULL};
2564 WMRegisterViewForDraggedTypes(tPtr->view, types);
2566 #endif
2568 WMAddNotificationObserver(fontChanged, tPtr,
2569 "WMFontPanelDidChangeNotification", tPtr);
2571 tPtr->firstTextBlock = NULL;
2572 tPtr->lastTextBlock = NULL;
2573 tPtr->currentTextBlock = NULL;
2574 tPtr->tpos = 0;
2576 tPtr->gfxItems = WMCreateBag(4);
2578 tPtr->parser = NULL;
2579 tPtr->writer = NULL;
2581 tPtr->sel.x = tPtr->sel.y = 2;
2582 tPtr->sel.w = tPtr->sel.h = 0;
2584 tPtr->clicked.x = tPtr->clicked.y = 2;
2586 tPtr->visible.x = tPtr->visible.y = 2;
2587 tPtr->visible.h = tPtr->view->size.height;
2588 tPtr->visible.w = tPtr->view->size.width - 4;
2590 tPtr->cursor.x = -23;
2592 tPtr->docWidth = 0;
2593 tPtr->docHeight = 0;
2594 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen,
2595 default_bullet);
2596 tPtr->db = (Pixmap) NULL;
2598 tPtr->margins = WMGetRulerMargins(NULL);
2599 tPtr->margins->right = tPtr->visible.w;
2600 tPtr->nMargins = 1;
2602 tPtr->flags.rulerShown = False;
2603 tPtr->flags.monoFont = False;
2604 tPtr->flags.focused = False;
2605 tPtr->flags.editable = True;
2606 tPtr->flags.ownsSelection = False;
2607 tPtr->flags.pointerGrabbed = False;
2608 tPtr->flags.buttonHeld = False;
2609 tPtr->flags.waitingForSelection = False;
2610 tPtr->flags.extendSelection = False;
2611 tPtr->flags.frozen = False;
2612 tPtr->flags.cursorShown = True;
2613 tPtr->flags.clickPos = 1;
2614 tPtr->flags.horizOnDemand = False;
2615 tPtr->flags.needsRefresh = False;
2616 tPtr->flags.ignoreNewLine = False;
2617 tPtr->flags.laidOut = False;
2618 tPtr->flags.prepend = False;
2619 tPtr->flags.relief = WRFlat;
2620 tPtr->flags.alignment = WALeft;
2622 return tPtr;
2625 void
2626 WMPrependTextStream(WMText *tPtr, char *text)
2628 if (!tPtr)
2629 return;
2631 if(!text)
2632 releaseSelection(tPtr);
2634 tPtr->flags.prepend = True;
2635 if (text && tPtr->parser)
2636 (tPtr->parser) (tPtr, (void *) text);
2637 else
2638 insertPlainText(tPtr, text);
2640 tPtr->flags.needsRefresh = True;
2644 void
2645 WMAppendTextStream(WMText *tPtr, char *text)
2647 if (!tPtr)
2648 return;
2650 if(!text)
2651 releaseSelection(tPtr);
2653 tPtr->flags.prepend = False;
2654 if (text && tPtr->parser)
2655 (tPtr->parser) (tPtr, (void *) text);
2656 else
2657 insertPlainText(tPtr, text);
2659 tPtr->flags.needsRefresh = True;
2664 char *
2665 WMGetTextStream(WMText *tPtr)
2667 return getStream(tPtr, 0);
2670 char *
2671 WMGetTextSelected(WMText *tPtr)
2673 return getStream(tPtr, 1);
2676 WMBag *
2677 WMGetTextStreamIntoBag(WMText *tPtr)
2679 return getStreamIntoBag(tPtr, 0);
2682 WMBag *
2683 WMGetTextSelectedIntoBag(WMText *tPtr)
2685 return getStreamIntoBag(tPtr, 1);
2689 void *
2690 WMCreateTextBlockWithObject(WMText *tPtr, WMWidget *w,
2691 char *description, WMColor *color,
2692 unsigned short first, unsigned short reserved)
2694 TextBlock *tb;
2695 unsigned short length;
2697 if (!w || !description || !color)
2698 return NULL;
2700 tb = wmalloc(sizeof(TextBlock));
2701 if (!tb)
2702 return NULL;
2704 length = strlen(description);
2705 tb->text = (char *)wmalloc(length);
2706 memset(tb->text, 0, length);
2707 memcpy(tb->text, description, length);
2708 tb->used = length;
2709 tb->blank = False;
2710 tb->d.widget = w;
2711 tb->color = WMRetainColor(color);
2712 tb->marginN = newMargin(tPtr, NULL);
2713 tb->allocated = 0;
2714 tb->first = first;
2715 tb->kanji = False;
2716 tb->graphic = True;
2717 tb->object = True;
2718 tb->underlined = False;
2719 tb->selected = False;
2720 tb->script = 0;
2721 tb->sections = NULL;
2722 tb->nsections = 0;
2723 tb->prior = NULL;
2724 tb->next = NULL;
2726 return tb;
2730 void *
2731 WMCreateTextBlockWithPixmap(WMText *tPtr, WMPixmap *p,
2732 char *description, WMColor *color,
2733 unsigned short first, unsigned short reserved)
2735 TextBlock *tb;
2736 unsigned short length;
2738 if (!p || !description || !color)
2739 return NULL;
2741 tb = wmalloc(sizeof(TextBlock));
2742 if (!tb)
2743 return NULL;
2745 length = strlen(description);
2746 tb->text = (char *)wmalloc(length);
2747 memset(tb->text, 0, length);
2748 memcpy(tb->text, description, length);
2749 tb->used = length;
2750 tb->blank = False;
2751 tb->d.pixmap = p;
2752 tb->color = WMRetainColor(color);
2753 tb->marginN = newMargin(tPtr, NULL);
2754 tb->allocated = 0;
2755 tb->first = first;
2756 tb->kanji = False;
2757 tb->graphic = True;
2758 tb->object = False;
2759 tb->underlined = False;
2760 tb->selected = False;
2761 tb->script = 0;
2762 tb->sections = NULL;
2763 tb->nsections = 0;
2764 tb->prior = NULL;
2765 tb->next = NULL;
2767 return tb;
2770 void *
2771 WMCreateTextBlockWithText(WMText *tPtr, char *text, WMFont *font, WMColor *color,
2772 unsigned short first, unsigned short length)
2774 TextBlock *tb;
2776 if (!font || !color)
2777 return NULL;
2779 tb = wmalloc(sizeof(TextBlock));
2780 if (!tb)
2781 return NULL;
2783 tb->allocated = reqBlockSize(length);
2784 tb->text = (char *)wmalloc(tb->allocated);
2785 memset(tb->text, 0, tb->allocated);
2787 if (length < 1|| !text ) { // || *text == '\n') {
2788 *tb->text = ' ';
2789 tb->used = 1;
2790 tb->blank = True;
2791 } else {
2792 memcpy(tb->text, text, length);
2793 tb->used = length;
2794 tb->blank = False;
2797 tb->d.font = WMRetainFont(font);
2798 tb->color = WMRetainColor(color);
2799 tb->marginN = newMargin(tPtr, NULL);
2800 tb->first = first;
2801 tb->kanji = False;
2802 tb->graphic = False;
2803 tb->underlined = False;
2804 tb->selected = False;
2805 tb->script = 0;
2806 tb->sections = NULL;
2807 tb->nsections = 0;
2808 tb->prior = NULL;
2809 tb->next = NULL;
2810 return tb;
2813 void
2814 WMSetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int first,
2815 unsigned int kanji, unsigned int underlined, int script,
2816 WMRulerMargins *margins)
2818 TextBlock *tb = (TextBlock *) vtb;
2819 if (!tb)
2820 return;
2822 tb->first = first;
2823 tb->kanji = kanji;
2824 tb->underlined = underlined;
2825 tb->script = script;
2826 tb->marginN = newMargin(tPtr, margins);
2829 void
2830 WMGetTextBlockProperties(WMText *tPtr, void *vtb, unsigned int *first,
2831 unsigned int *kanji, unsigned int *underlined, int *script,
2832 WMRulerMargins *margins)
2834 TextBlock *tb = (TextBlock *) vtb;
2835 if (!tb)
2836 return;
2838 if (first) *first = tb->first;
2839 if (kanji) *kanji = tb->kanji;
2840 if (underlined) *underlined = tb->underlined;
2841 if (script) *script = tb->script;
2842 if (margins) margins = &tPtr->margins[tb->marginN];
2847 void
2848 WMPrependTextBlock(WMText *tPtr, void *vtb)
2850 TextBlock *tb = (TextBlock *)vtb;
2852 if (!tPtr || !tb)
2853 return;
2855 if (tb->graphic) {
2856 if(tb->object) {
2857 WMWidget *w = tb->d.widget;
2858 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
2859 handleWidgetPress, tb);
2860 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
2861 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
2862 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
2865 WMPutInBag(tPtr->gfxItems, (void *)tb);
2866 tPtr->tpos = 0;
2867 } else tPtr->tpos = tb->used;
2869 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
2870 tb->next = tb->prior = NULL;
2871 tPtr->lastTextBlock = tPtr->firstTextBlock
2872 = tPtr->currentTextBlock = tb;
2873 return;
2876 tb->next = tPtr->currentTextBlock;
2877 tb->prior = tPtr->currentTextBlock->prior;
2878 if (tPtr->currentTextBlock->prior)
2879 tPtr->currentTextBlock->prior->next = tb;
2881 tPtr->currentTextBlock->prior = tb;
2882 if (!tb->prior)
2883 tPtr->firstTextBlock = tb;
2885 tPtr->currentTextBlock = tb;
2889 void
2890 WMAppendTextBlock(WMText *tPtr, void *vtb)
2892 TextBlock *tb = (TextBlock *)vtb;
2894 if (!tPtr || !tb)
2895 return;
2897 if (tb->graphic) {
2898 if(tb->object) {
2899 WMWidget *w = tb->d.widget;
2900 WMCreateEventHandler(W_VIEW(w), ButtonPressMask,
2901 handleWidgetPress, tb);
2902 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
2903 (W_VIEW(w))->attribs.cursor =
2904 tPtr->view->screen->defaultCursor;
2905 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
2908 WMPutInBag(tPtr->gfxItems, (void *)tb);
2909 tPtr->tpos = 0;
2910 } else tPtr->tpos = tb->used;
2912 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
2913 tb->next = tb->prior = NULL;
2914 tPtr->lastTextBlock = tPtr->firstTextBlock
2915 = tPtr->currentTextBlock = tb;
2916 return;
2919 tb->next = tPtr->currentTextBlock->next;
2920 tb->prior = tPtr->currentTextBlock;
2921 if (tPtr->currentTextBlock->next)
2922 tPtr->currentTextBlock->next->prior = tb;
2924 tPtr->currentTextBlock->next = tb;
2926 if (!tb->next)
2927 tPtr->lastTextBlock = tb;
2929 tPtr->currentTextBlock = tb;
2932 void *
2933 WMRemoveTextBlock(WMText *tPtr)
2935 TextBlock *tb = NULL;
2937 if (!tPtr || !tPtr->firstTextBlock || !tPtr->lastTextBlock
2938 || !tPtr->currentTextBlock) {
2939 printf("cannot remove non existent TextBlock!\b");
2940 return tb;
2943 tb = tPtr->currentTextBlock;
2944 if (tb->graphic) {
2945 WMRemoveFromBag(tPtr->gfxItems, (void *)tb);
2947 if(tb->object) {
2948 WMDeleteEventHandler(W_VIEW(tb->d.widget), ButtonPressMask,
2949 handleWidgetPress, tb);
2950 WMUnmapWidget(tb->d.widget);
2954 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
2955 if (tPtr->currentTextBlock->next)
2956 tPtr->currentTextBlock->next->prior = NULL;
2958 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
2959 tPtr->currentTextBlock = tPtr->firstTextBlock;
2961 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
2962 tPtr->currentTextBlock->prior->next = NULL;
2963 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
2964 tPtr->currentTextBlock = tPtr->lastTextBlock;
2965 } else {
2966 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
2967 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
2968 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
2971 return (void *)tb;
2974 void
2975 WMDestroyTextBlock(WMText *tPtr, void *vtb)
2977 TextBlock *tb = (TextBlock *)vtb;
2978 if (!tPtr || !tb)
2979 return;
2981 if (tb->graphic) {
2982 if(tb->object) {
2983 /* naturally, there's a danger to destroying
2984 widgets whose action brings us here:
2985 ie. press a button to destroy it... need to
2986 find a safer way. till then... this stays commented out */
2987 //WMDestroyWidget(tb->d.widget);
2988 //wfree(tb->d.widget);
2989 tb->d.widget = NULL;
2990 } else {
2991 WMReleasePixmap(tb->d.pixmap);
2992 tb->d.pixmap = NULL;
2994 } else {
2995 WMReleaseFont(tb->d.font);
2998 WMReleaseColor(tb->color);
2999 if (tb->sections && tb->nsections > 0)
3000 wfree(tb->sections);
3001 wfree(tb->text);
3002 wfree(tb);
3003 tb = NULL;
3007 void
3008 WMRefreshText(WMText *tPtr, int vpos, int hpos)
3010 if (!tPtr || vpos<0 || hpos<0)
3011 return;
3013 if (tPtr->flags.frozen && !tPtr->flags.needsRefresh)
3014 return;
3016 if(tPtr->flags.monoFont) {
3017 int j, c = WMGetBagItemCount(tPtr->gfxItems);
3018 TextBlock *tb;
3020 /* make sure to unmap widgets no matter where they are */
3021 for(j=0; j<c; j++) {
3022 if ((tb = (TextBlock *) WMGetFromBag(tPtr->gfxItems, j))) {
3023 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3024 WMUnmapWidget(tb->d.widget);
3030 if (tPtr->vpos != vpos) {
3031 if (vpos < 0 || tPtr->docHeight < tPtr->visible.h) {
3032 tPtr->vpos = 0;
3033 } else if(tPtr->docHeight - vpos > tPtr->visible.h - tPtr->visible.y) {
3034 tPtr->vpos = vpos;
3035 } else {
3036 tPtr->vpos = tPtr->docHeight - tPtr->visible.h;
3040 if (tPtr->hpos != hpos) {
3041 if (hpos < 0 || tPtr->docWidth < tPtr->visible.w) {
3042 tPtr->hpos = 0;
3043 } else if(tPtr->docWidth - hpos > tPtr->visible.w - tPtr->visible.x) {
3044 tPtr->hpos = hpos;
3045 } else {
3046 tPtr->hpos = tPtr->docWidth - tPtr->visible.w;
3051 tPtr->flags.laidOut = False;
3052 layOutDocument(tPtr);
3053 updateScrollers(tPtr);
3054 paintText(tPtr);
3055 tPtr->flags.needsRefresh = False;
3059 void
3060 WMSetTextForegroundColor(WMText *tPtr, WMColor *color)
3062 if (!tPtr)
3063 return;
3065 if (color)
3066 tPtr->fgGC = WMColorGC(color);
3067 else
3068 tPtr->fgGC = WMColorGC(WMBlackColor(tPtr->view->screen));
3070 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3073 void
3074 WMSetTextBackgroundColor(WMText *tPtr, WMColor *color)
3076 if (!tPtr)
3077 return;
3079 if (color) {
3080 tPtr->bgGC = WMColorGC(color);
3081 W_SetViewBackgroundColor(tPtr->view, color);
3082 } else {
3083 tPtr->bgGC = WMColorGC(WMWhiteColor(tPtr->view->screen));
3084 W_SetViewBackgroundColor(tPtr->view,
3085 WMWhiteColor(tPtr->view->screen));
3088 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3091 void
3092 WMSetTextRelief(WMText *tPtr, WMReliefType relief)
3094 if (!tPtr)
3095 return;
3096 tPtr->flags.relief = relief;
3097 paintText(tPtr);
3100 void
3101 WMSetTextHasHorizontalScroller(WMText *tPtr, Bool shouldhave)
3103 if (!tPtr)
3104 return;
3106 if (shouldhave && !tPtr->hS) {
3107 tPtr->hS = WMCreateScroller(tPtr);
3108 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3109 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3110 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3111 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3112 WMMapWidget(tPtr->hS);
3113 } else if (!shouldhave && tPtr->hS) {
3114 WMUnmapWidget(tPtr->hS);
3115 WMDestroyWidget(tPtr->hS);
3116 tPtr->hS = NULL;
3119 tPtr->hpos = 0;
3120 tPtr->prevHpos = 0;
3121 textDidResize(tPtr->view->delegate, tPtr->view);
3125 void
3126 WMSetTextHasRuler(WMText *tPtr, Bool shouldhave)
3128 if (!tPtr)
3129 return;
3131 if(shouldhave && !tPtr->ruler) {
3132 tPtr->ruler = WMCreateRuler(tPtr);
3133 (W_VIEW(tPtr->ruler))->attribs.cursor =
3134 tPtr->view->screen->defaultCursor;
3135 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3136 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3137 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3138 } else if(!shouldhave && tPtr->ruler) {
3139 WMShowTextRuler(tPtr, False);
3140 WMDestroyWidget(tPtr->ruler);
3141 tPtr->ruler = NULL;
3143 textDidResize(tPtr->view->delegate, tPtr->view);
3146 void
3147 WMShowTextRuler(WMText *tPtr, Bool show)
3149 if(!tPtr)
3150 return;
3151 if(!tPtr->ruler)
3152 return;
3154 if(tPtr->flags.monoFont)
3155 show = False;
3157 tPtr->flags.rulerShown = show;
3158 if(show) {
3159 WMMapWidget(tPtr->ruler);
3160 } else {
3161 WMUnmapWidget(tPtr->ruler);
3164 textDidResize(tPtr->view->delegate, tPtr->view);
3167 Bool
3168 WMGetTextRulerShown(WMText *tPtr)
3170 if(!tPtr)
3171 return 0;
3173 if(!tPtr->ruler)
3174 return 0;
3176 return tPtr->flags.rulerShown;
3180 void
3181 WMSetTextHasVerticalScroller(WMText *tPtr, Bool shouldhave)
3183 if (!tPtr)
3184 return;
3186 if (shouldhave && !tPtr->vS) {
3187 tPtr->vS = WMCreateScroller(tPtr);
3188 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3189 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3190 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3191 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3192 WMMapWidget(tPtr->vS);
3193 } else if (!shouldhave && tPtr->vS) {
3194 WMUnmapWidget(tPtr->vS);
3195 WMDestroyWidget(tPtr->vS);
3196 tPtr->vS = NULL;
3199 tPtr->vpos = 0;
3200 tPtr->prevVpos = 0;
3201 textDidResize(tPtr->view->delegate, tPtr->view);
3206 Bool
3207 WMScrollText(WMText *tPtr, int amount)
3209 Bool scroll=False;
3210 if (!tPtr)
3211 return False;
3212 if (amount == 0 || !tPtr->view->flags.realized)
3213 return False;
3215 if (amount < 0) {
3216 if (tPtr->vpos > 0) {
3217 if (tPtr->vpos > abs(amount)) tPtr->vpos += amount;
3218 else tPtr->vpos=0;
3219 scroll=True;
3220 } } else {
3221 int limit = tPtr->docHeight - tPtr->visible.h;
3222 if (tPtr->vpos < limit) {
3223 if (tPtr->vpos < limit-amount) tPtr->vpos += amount;
3224 else tPtr->vpos = limit;
3225 scroll = True;
3226 } }
3228 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3229 updateScrollers(tPtr);
3230 paintText(tPtr);
3232 tPtr->prevVpos = tPtr->vpos;
3233 return scroll;
3236 Bool
3237 WMPageText(WMText *tPtr, Bool direction)
3239 if (!tPtr) return False;
3240 if (!tPtr->view->flags.realized) return False;
3242 return WMScrollText(tPtr, direction?tPtr->visible.h:-tPtr->visible.h);
3245 void
3246 WMSetTextEditable(WMText *tPtr, Bool editable)
3248 if (!tPtr)
3249 return;
3250 tPtr->flags.editable = editable;
3253 int
3254 WMGetTextEditable(WMText *tPtr)
3256 if (!tPtr)
3257 return 0;
3258 return tPtr->flags.editable;
3261 void
3262 WMSetTextIgnoresNewline(WMText *tPtr, Bool ignore)
3264 if (!tPtr)
3265 return;
3266 tPtr->flags.ignoreNewLine = ignore;
3269 Bool
3270 WMGetTextIgnoresNewline(WMText *tPtr)
3272 if (!tPtr)
3273 return True;
3274 return tPtr->flags.ignoreNewLine;
3277 void
3278 WMSetTextUsesMonoFont(WMText *tPtr, Bool mono)
3280 if (!tPtr)
3281 return;
3282 if (mono && tPtr->flags.rulerShown)
3283 WMShowTextRuler(tPtr, False);
3285 tPtr->flags.monoFont = mono;
3286 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3289 Bool
3290 WMGetTextUsesMonoFont(WMText *tPtr)
3292 if (!tPtr)
3293 return True;
3294 return tPtr->flags.monoFont;
3298 void
3299 WMSetTextDefaultFont(WMText *tPtr, WMFont *font)
3301 if (!tPtr)
3302 return;
3304 WMReleaseFont(tPtr->dFont);
3305 if (font)
3306 tPtr->dFont = font;
3307 else
3308 tPtr->dFont = WMRetainFont(tPtr->view->screen->normalFont);
3311 WMFont *
3312 WMGetTextDefaultFont(WMText *tPtr)
3314 if (!tPtr)
3315 return NULL;
3316 else
3317 return tPtr->dFont;
3320 void
3321 WMSetTextAlignment(WMText *tPtr, WMAlignment alignment)
3323 if (!tPtr)
3324 return;
3325 tPtr->flags.alignment = alignment;
3326 WMRefreshText(tPtr, tPtr->vpos, tPtr->hpos);
3329 void
3330 WMSetTextParser(WMText *tPtr, WMAction *parser)
3332 if (!tPtr)
3333 return;
3334 tPtr->parser = parser;
3337 void
3338 WMSetTextWriter(WMText *tPtr, WMAction *writer)
3340 if (!tPtr)
3341 return;
3342 tPtr->writer = writer;
3345 int
3346 WMGetTextInsertType(WMText *tPtr)
3348 if (!tPtr)
3349 return 0;
3350 return tPtr->flags.prepend;
3354 void
3355 WMSetTextSelectionColor(WMText *tPtr, WMColor *color)
3357 if (!tPtr || !color)
3358 return;
3360 setSelectionProperty(tPtr, NULL, color);
3365 void
3366 WMSetTextSelectionFont(WMText *tPtr, WMFont *font)
3368 if (!tPtr || !font)
3369 return;
3371 setSelectionProperty(tPtr, font, NULL);
3375 void
3376 WMFreezeText(WMText *tPtr)
3378 if (!tPtr)
3379 return;
3381 tPtr->flags.frozen = True;
3384 void
3385 WMThawText(WMText *tPtr)
3387 if (!tPtr)
3388 return;
3390 tPtr->flags.frozen = False;
3394 Bool
3395 WMFindInTextStream(WMText *tPtr, char *needle)
3397 char *haystack = NULL;
3398 Bool result;
3400 if (!tPtr || !needle)
3401 return False;
3403 if ( !(haystack = "WMGetTextStream(tPtr)"))
3404 return False;
3406 result = (Bool) strstr(haystack, needle);
3407 wfree(haystack);
3408 return result;
3414 #if 0
3415 typedef struct _currentFormat {
3416 WMBag *fonts;
3417 WMBag *colors;
3418 WMColor *ccolor;
3419 WMFont *cfont;
3420 WMRulerMargins margins;
3421 //WMBag *aligns; // for tables...
3422 /* the following are "nested"
3423 i.e.: <b><b><i></b><b></i>
3424 1 2 1 1 2 0 get it? */
3425 short i;
3426 short b;
3427 short u;
3428 short fmargin;
3429 short bmargin;
3430 short first:1;
3431 short type:1;
3432 WMAlignment align:2;
3433 short ul:3; /* how "nested"... up to 8 levels deep */
3434 short comment:1; /* ignore text till --> */
3435 short RESERVED:10;
3436 } CFMT;
3437 CFMT cfmt;
3441 #if 0
3442 getArg(char *t, short type, void *arg)
3444 short d=0;
3445 while(*(++t) && !d) {
3446 if(type==0) {
3447 if(*t>='0' && *t<='9') {
3448 sscanf(t, "%d", arg);
3449 while(*t&& (*t<'0' || *t>'9'))
3450 t++;
3451 d=1;
3456 #endif
3458 void parseToken(WMText *tPtr, char *token, short tk)
3460 short mode=0; /* 0 starts, 1 closes */
3461 void *tb= NULL;
3462 int prepend = WMGetTextInsertType(tPtr);
3464 while(*token && isspace(*(token))) token++;
3465 if(*token == '/') {
3466 token++;
3467 mode = 1;
3468 while(isspace(*(token))) token++;
3471 if(strlen(token)==1) {
3472 /* nice and fast for small tokens... no need for too much brain
3473 power here */
3474 switch(TOLOWER(*token)) {
3475 case 'i':
3476 if(!mode) {
3477 cfmt.cfont = WMGetFontItalic(scr, cfmt.cfont);
3478 WMPutInBag(cfmt.fonts, (void *)cfmt.cfont);
3479 } else { /*dun wanna remove the baseFont eh? */
3480 int count = WMGetBagItemCount(cfmt.fonts);
3481 if(count>1)
3482 WMDeleteFromBag(cfmt.fonts, count-1);
3483 cfmt.cfont = (WMFont *)WMGetFromBag(cfmt.fonts,
3484 WMGetBagItemCount(cfmt.fonts)-1);
3485 } break;
3486 case 'b':
3487 if(!mode) {
3488 cfmt.cfont = WMGetFontBold(scr, cfmt.cfont);
3489 WMPutInBag(cfmt.fonts, (void *)cfmt.cfont);
3490 } else { /*dun wanna remove the baseFont eh? */
3491 int count = WMGetBagItemCount(cfmt.fonts);
3492 if(count>1)
3493 WMDeleteFromBag(cfmt.fonts, count-1);
3494 cfmt.cfont = (WMFont *)WMGetFromBag(cfmt.fonts,
3495 WMGetBagItemCount(cfmt.fonts)-1);
3496 } break;
3497 case 'p':
3498 cfmt.first = True;
3499 tb = WMCreateTextBlockWithText(NULL, cfmt.cfont,
3500 cfmt.ccolor, cfmt.first, 0);
3501 WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
3502 //WMAppendTextBlock(tPtr, tb);
3503 cfmt.first = False;
3504 break;
3505 case 'u': cfmt.u = !mode; break;
3507 } else { /* the <HTML> tag is, as far as I'm concerned, useless */
3508 if(mystrcasecmp(token, "br")) {
3509 cfmt.first = True;
3511 else if(mystrcasecmp(token, "ul")) {
3512 if(mode) {
3513 if(cfmt.ul>1) cfmt.ul--;
3514 } else cfmt.ul++;
3515 if(cfmt.ul) {
3516 cfmt.bmargin = cfmt.ul*30;
3517 cfmt.fmargin = cfmt.bmargin-10;
3518 } else cfmt.fmargin = cfmt.bmargin = 0;
3519 } else if(mystrcasecmp(token, "li")) {
3520 cfmt.first = True;
3521 //change margins... create a new margin....
3522 //(cfmt.fmargin, cfmt.bmargin,
3523 } else if(mystrcasecmp(token, "align"))
3524 ;//printf("align");
3525 else if(mystrcasecmp(token, "img")) {
3526 if(!mode) {
3527 char *mark=NULL;
3528 WMPixmap *pixmap;
3529 token+=3;
3530 while(isspace(*(token))) token++;
3531 do {
3532 switch(TOLOWER(*token)) {
3533 case 's':
3534 if(TOLOWER(*(1+token)) == 'r' && TOLOWER(*(2+token)) == 'c') {
3535 mark = strchr(token, '=');
3536 if(mark) {
3537 char img[256], *iptr;
3538 token = mark+1;
3539 if(!token) return;
3540 sscanf(token, "%s", img);
3541 iptr = img;
3542 if(*img == '\"') { img[strlen(img)-1] = 0; iptr++;}
3543 pixmap = WMCreatePixmapFromFile(scr, iptr);
3544 if(pixmap) {
3545 tb = WMCreateTextBlockWithPixmap(pixmap,
3546 iptr, cfmt.ccolor, cfmt.first, 0);
3547 WMSetTextBlockProperties(tb, cfmt.first,
3548 False, (cfmt.u?1:0), 0, cfmt.margins);
3549 WMAppendTextBlock(tPtr, tb);
3550 cfmt.first = False;
3552 //printf("[%s]\n", iptr);
3553 } } break; } } while(*(token++));
3555 } else if(mystrcasecmp(token, "font")) {
3556 #if 0
3557 if(mode) {
3558 cfmt.cfont = (WMFont *)WMGetFromBag(cfmt.fonts,
3559 WMGetBagItemCount(cfmt.fonts)-1);
3560 } else
3561 (WMColor *)WMGetFromBag(cfmt.colors,
3562 WMGetBagItemCount(cfmt.colors)-1),
3563 #endif
3565 else if(mystrcasecmp(token, "center")) {
3566 printf("center\n");
3567 if(mode) cfmt.align = WALeft;
3568 else cfmt.align = WACenter;
3569 cfmt.first = True;
3570 //change margins...
3577 //printf("parse token (%s)[%s]\n", mode?"close":"open", token);
3578 #if 0
3579 i=0;
3580 //while(*token && !isspace(*(token))) token++;
3581 //printf("A:%d a:%d z%d Z%d\n", '1', 'a', 'Z', 'z');
3582 do {
3583 if(!mm) {
3584 if(c>=65 && c<=122) { major[i++] = c;
3585 } else if(c==' ' || c=='='){ major[i] = 0; i=0; mm=1;
3586 printf("\nmajor: [%s]", major);}
3587 } else {
3588 if(c!=' ') {
3589 minor[i++] = c;
3590 } else { minor[i] = 0; i=0; printf(" minor: [%s] ", minor);}
3592 }while((c = *(++token)));
3593 #endif
3596 //printf("parse token (%s)[%s]\n", mode?"close":"open", token);
3599 void HTMLParser(WMWidget *w, void *clientData)
3601 static short init=1; /* have we been here at least once before? */
3602 char *stream = (char *) clientData;
3603 WMText *tPtr = (WMText *)w;
3604 void *tb = NULL;
3605 char c;
3606 char token[MAX_TOKEN_SIZE+1];
3607 char text[MAX_TEXT_SIZE+1];
3608 short mode=0;
3609 short tk=0, textlen=0;
3610 short wasspace=0;
3612 if(!tPtr || !stream)
3613 return;
3615 cfmt.type = WMGetTextInsertType(tPtr);
3616 if(1||init) {
3617 cfmt.fonts = WMCreateBag(4); /* there sould always be at least 1 font... */
3618 cfmt.cfont = WMGetTextDefaultFont(tPtr);
3619 WMPutInBag(cfmt.fonts, (void *)cfmt.cfont);
3620 cfmt.colors = WMCreateBag(4);
3621 cfmt.ccolor = WMBlackColor(scr);
3622 WMPutInBag(cfmt.colors, (void *)cfmt.ccolor);
3623 cfmt.i = cfmt.b = cfmt.u = cfmt.ul = 0;
3624 cfmt.align = WALeft;
3625 cfmt.fmargin = cfmt.bmargin = 0;
3626 init = 0;
3629 #if 0
3630 if(strlen(stream) == 1 && stream[0] == '\n') {
3631 /* sometimes if the text entered is a single char AND is a newline,
3632 the user prolly typed it */
3633 cfmt.para = (cfmt.actions.createParagraph) (cfmt.fmargin, cfmt.bmargin,
3634 WMWidgetWidth(tPtr)-30, NULL, 0, cfmt.align);
3635 (cfmt.actions.insertParagraph) (tPtr, cfmt.para, cfmt.type);
3636 return;
3638 #endif
3644 while( (c=*(stream++))) {
3645 //printf("%c", c);
3646 if(c == '\n' || c =='\t')
3647 //c = ' '; //continue;
3648 continue;
3649 if(c == ' ') {
3650 if(wasspace)
3651 continue;
3652 wasspace = 1;
3653 }else wasspace = 0;
3655 if(c == '<' && !mode) {
3656 mode=1;
3657 if(textlen>0) {
3658 text[textlen] = 0;
3659 tb = WMCreateTextBlockWithText(text, cfmt.cfont,
3660 cfmt.ccolor, cfmt.first, textlen);
3661 WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
3662 WMAppendTextBlock(tPtr, tb);
3663 cfmt.first = False;
3664 //printf("%s\n", text);
3666 textlen = 0;
3667 } else if(c == '>' && mode) {
3668 token[tk] = 0;
3669 if(tk>0) parseToken(tPtr, token, tk);
3670 mode=0;
3671 tk=0;
3672 } else {
3673 if(mode) {
3674 if(tk < MAX_TOKEN_SIZE) token[tk++] = c;
3675 } else if(textlen < MAX_TEXT_SIZE) text[textlen++] = c;
3679 if(tk>0) { token[tk] = 0; parseToken(tPtr, token, tk);}
3680 if(textlen>0) {
3681 text[textlen] = 0;
3682 //printf("%s\n", text);
3683 tb = WMCreateTextBlockWithText(text,
3684 (WMFont *)WMGetFromBag(cfmt.fonts,
3685 WMGetBagItemCount(cfmt.fonts)-1),
3686 (WMColor *)WMGetFromBag(cfmt.colors,
3687 WMGetBagItemCount(cfmt.colors)-1),
3688 cfmt.first, textlen);
3689 WMSetTextBlockProperties(tb, cfmt.first, False, (cfmt.u?1:0), 0, cfmt.margins);
3690 WMAppendTextBlock(tPtr, tb);
3691 cfmt.first = False;
3696 #endif