WPrefs: convert workspacename.xpm X11 color name to hex
[wmaker-crm.git] / WINGs / wtext.c
blobee61411fec3c62d4cbda3bd5788afe69749aefb6
2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
4 #include "WINGsP.h"
5 #include <ctype.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 #define DO_BLINK 0
11 /* TODO:
12 * - verify what happens with XK_return in insertTextInt...
13 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
24 /* a Section is a section of a TextBlock that describes what parts
25 of a TextBlock has been laid out on which "line"...
26 o this greatly aids redraw, scroll and selection.
27 o this is created during layoutLine, but may be later modified.
28 o there may be many Sections per TextBlock, hence the array */
29 typedef struct {
30 unsigned int x, y; /* where to draw it from */
31 unsigned short w, h; /* its width and height */
32 unsigned short begin; /* where the layout begins */
33 unsigned short end; /* where it ends */
34 unsigned short max_d; /* a quick hack for layOut if(laidOut) */
35 unsigned short last:1; /* is it the last section on a "line"? */
36 unsigned int _y:31; /* the "line" it and other textblocks are on */
37 } Section;
39 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
40 o text for the block, color and font
41 o or a pointer to the pixmap
42 o OR a pointer to the widget and the (text) description for its graphic
45 typedef struct _TextBlock {
46 struct _TextBlock *next; /* next text block in linked list */
47 struct _TextBlock *prior; /* prior text block in linked list */
49 char *text; /* pointer to text (could be kanji) */
50 /* or to the object's description */
51 union {
52 WMFont *font; /* the font */
53 WMWidget *widget; /* the embedded widget */
54 WMPixmap *pixmap; /* the pixmap */
55 } d; /* description */
57 unsigned short used; /* number of chars in this block */
58 unsigned short allocated; /* size of allocation (in chars) */
59 WMColor *color; /* the color */
61 Section *sections; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
64 unsigned short s_begin; /* where the selection begins */
65 unsigned short s_end; /* where it ends */
67 unsigned int first:1; /* first TextBlock in paragraph */
68 unsigned int blank:1; /* ie. blank paragraph */
69 unsigned int kanji:1; /* is of 16-bit characters or not */
70 unsigned int graphic:1; /* graphic or text: text=0 */
71 unsigned int object:1; /* embedded object or pixmap */
72 unsigned int underlined:1; /* underlined or not */
73 unsigned int selected:1; /* selected or not */
74 unsigned int nsections:8; /* over how many "lines" a TextBlock wraps */
75 int script:8; /* script in points: negative for subscript */
76 unsigned int marginN:8; /* which of the margins in the tPtr to use */
77 unsigned int nClicks:2; /* single, double, triple clicks */
78 unsigned int RESERVED:7;
79 } TextBlock;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
82 typedef struct {
83 int y, x, h, w;
84 } myRect;
86 typedef struct W_Text {
87 W_Class widgetClass; /* the class number of this widget */
88 W_View *view; /* the view referring to this instance */
90 WMRuler *ruler; /* the ruler widget to manipulate paragraphs */
92 WMScroller *vS; /* the vertical scroller */
93 unsigned int vpos; /* the current vertical position */
94 unsigned int prevVpos; /* the previous vertical position */
96 WMScroller *hS; /* the horizontal scroller */
97 unsigned int hpos; /* the current horizontal position */
98 unsigned int prevHpos; /* the previous horizontal position */
100 WMFont *dFont; /* the default font */
101 WMColor *dColor; /* the default color */
102 WMPixmap *dBulletPix; /* the default pixmap for bullets */
104 WMColor *fgColor; /* The current foreground color */
105 WMColor *bgColor; /* The background color */
107 GC stippledGC; /* the GC to overlay selected graphics with */
108 Pixmap db; /* the buffer on which to draw */
109 WMPixmap *bgPixmap; /* the background pixmap */
111 myRect visible; /* the actual rectangle that can be drawn into */
112 myRect cursor; /* the position and (height) of cursor */
113 myRect sel; /* the selection rectangle */
115 WMPoint clicked; /* where in the _document_ was clicked */
117 unsigned short tpos; /* the position in the currentTextBlock */
118 unsigned short docWidth; /* the width of the entire document */
119 unsigned int docHeight; /* the height of the entire document */
121 TextBlock *firstTextBlock;
122 TextBlock *lastTextBlock;
123 TextBlock *currentTextBlock;
125 WMArray *gfxItems; /* a nice array of graphic items */
127 #if DO_BLINK
128 WMHandlerID timerID; /* for nice twinky-winky */
129 #endif
131 WMAction *parser;
132 WMAction *writer;
133 WMTextDelegate *delegate;
134 Time lastClickTime;
136 WMRulerMargins *margins; /* an array of margins */
138 unsigned int nMargins:7; /* the total number of margins in use */
139 struct {
140 unsigned int monoFont:1; /* whether to ignore formats and graphic */
141 unsigned int focused:1; /* whether this instance has input focus */
142 unsigned int editable:1; /* "silly user, you can't edit me" */
143 unsigned int ownsSelection:1; /* "I ownz the current selection!" */
144 unsigned int pointerGrabbed:1; /* "heh, gib me pointer" */
145 unsigned int extendSelection:1; /* shift-drag to select more regions */
147 unsigned int rulerShown:1; /* whether the ruler is shown or not */
148 unsigned int frozen:1; /* whether screen updates are to be made */
149 unsigned int cursorShown:1; /* whether to show the cursor */
150 unsigned int acceptsGraphic:1; /* accept graphic when dropped */
151 unsigned int horizOnDemand:1; /* if a large image should appear */
152 unsigned int needsLayOut:1; /* in case of Append/Deletes */
153 unsigned int ignoreNewLine:1; /* turn it into a ' ' in streams > 1 */
154 unsigned int indentNewLine:1; /* add " " for a newline typed */
155 unsigned int laidOut:1; /* have the TextBlocks all been laid out */
156 unsigned int waitingForSelection:1; /* I don't wanna wait in vain... */
157 unsigned int prepend:1; /* prepend=1, append=0 (for parsers) */
158 WMAlignment alignment:2; /* the alignment for text */
159 WMReliefType relief:3; /* the relief to display with */
160 unsigned int isOverGraphic:2; /* the mouse is over a graphic */
161 unsigned int first:1; /* for plain text parsing, newline? */
162 /* unsigned int RESERVED:1; */
163 } flags;
165 WMArray *xdndSourceTypes;
166 WMArray *xdndDestinationTypes;
167 } Text;
169 #define NOTIFY(T,C,N,A) {\
170 WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
176 #define TYPETEXT 0
178 #if 0
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr, int len)
182 char *s;
184 s = wmalloc(len + 1);
185 memcpy(s, ptr, len);
186 s[len] = 0;
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
188 printf("[%s]\n", s);
189 wfree(s);
191 #endif
193 #if DO_BLINK
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
196 #endif
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
204 static char *default_bullet[] = {
205 "6 6 4 1",
206 " c None s None",
207 ". c black",
208 "X c white",
209 "o c #808080",
210 " ... ",
211 ".XX.. ",
212 ".XX..o",
213 ".....o",
214 " ...oo",
215 " ooo "
218 /* These id are used when sharing the selected text between applications */
219 static Atom XA_Targets = None;
220 static Atom XA_Format_Text = None;
221 static Atom XA_Format_Compound_Text = None;
223 static void handleEvents(XEvent * event, void *data);
224 static void layOutDocument(Text * tPtr);
225 static void updateScrollers(Text * tPtr);
227 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
229 unsigned int i = 0;
231 for (i = 0; i < tPtr->nMargins; i++) {
233 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
234 return i;
237 return -1;
240 static int newMargin(Text * tPtr, WMRulerMargins * margins)
242 int n;
244 if (!margins) {
245 tPtr->margins[0].retainCount++;
246 return 0;
249 n = getMarginNumber(tPtr, margins);
251 if (n == -1) {
253 if (tPtr->nMargins >= 127) {
254 n = tPtr->nMargins - 1;
255 return n;
258 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
260 n = tPtr->nMargins - 1;
261 tPtr->margins[n].left = margins->left;
262 tPtr->margins[n].first = margins->first;
263 tPtr->margins[n].body = margins->body;
264 tPtr->margins[n].right = margins->right;
265 /* for each tab... */
266 tPtr->margins[n].retainCount = 1;
267 } else {
268 tPtr->margins[n].retainCount++;
271 return n;
274 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
276 unsigned short i, w, lw, selected = False, extend = False;
277 myRect sel;
279 /* if selection rectangle completely encloses the section */
280 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
281 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
282 sel.x = 0;
283 sel.w = tPtr->visible.w;
284 selected = extend = True;
286 /* or if it starts on a line and then goes further down */
287 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
288 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
289 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y)) {
290 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
291 sel.w = tPtr->visible.w;
292 selected = extend = True;
294 /* or if it begins before a line, but ends on it */
295 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
296 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
297 && (tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
299 if (1 || tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
300 sel.w = tPtr->sel.x + tPtr->sel.w;
301 else
302 sel.w = tPtr->sel.x;
304 sel.x = 0;
305 selected = True;
307 /* or if the selection rectangle lies entirely within a line */
308 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
309 && (tPtr->sel.w >= 2)
310 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
311 sel.x = tPtr->sel.x;
312 sel.w = tPtr->sel.w;
313 selected = True;
316 if (selected) {
317 selected = False;
319 /* if not within (modified) selection rectangle */
320 if (tb->sections[s].x > sel.x + sel.w || tb->sections[s].x + tb->sections[s].w < sel.x)
321 return False;
323 if (tb->graphic) {
324 if (tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w && tb->sections[s].x >= sel.x) {
325 rect->width = tb->sections[s].w;
326 rect->x = tb->sections[s].x;
327 selected = True;
329 } else {
331 i = tb->sections[s].begin;
332 lw = 0;
334 if (0 && tb->sections[s].x >= sel.x) {
335 tb->s_begin = tb->sections[s].begin;
336 goto _selEnd;
339 while (++i <= tb->sections[s].end) {
341 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
342 lw += w;
344 if (lw + tb->sections[s].x >= sel.x || i == tb->sections[s].end) {
345 lw -= w;
346 i--;
347 tb->s_begin = (tb->selected ? WMIN(tb->s_begin, i) : i);
348 break;
352 if (i > tb->sections[s].end) {
353 printf("WasSelected: (i > tb->sections[s].end) \n");
354 return False;
357 _selEnd: rect->x = tb->sections[s].x + lw;
358 lw = 0;
359 while (++i <= tb->sections[s].end) {
361 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
362 lw += w;
364 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
366 if (i != tb->sections[s].end) {
367 lw -= w;
368 i--;
371 rect->width = lw;
372 if (tb->sections[s].last && sel.x + sel.w
373 >= tb->sections[s].x + tb->sections[s].w && extend) {
374 rect->width += (tPtr->visible.w - rect->x - lw);
377 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
378 selected = True;
379 break;
385 if (selected) {
386 rect->y = tb->sections[s]._y - tPtr->vpos;
387 rect->height = tb->sections[s].h;
388 if (tb->graphic) {
389 printf("DEBUG: graphic s%d h%d\n", s, tb->sections[s].h);
392 return selected;
396 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
398 TextBlock *tb;
399 int isFont = False;
401 tb = tPtr->firstTextBlock;
402 if (!tb || !tPtr->flags.ownsSelection)
403 return;
405 if (font && (!color || underlined == -1))
406 isFont = True;
408 while (tb) {
409 if (tPtr->flags.monoFont || tb->selected) {
411 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
412 || tb->graphic) {
414 if (isFont) {
415 if (!tb->graphic) {
416 WMReleaseFont(tb->d.font);
417 tb->d.font = WMRetainFont(font);
419 } else if (underlined != -1) {
420 tb->underlined = underlined;
421 } else {
422 WMReleaseColor(tb->color);
423 tb->color = WMRetainColor(color);
426 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
428 TextBlock *midtb, *otb = tb;
430 if (underlined != -1) {
431 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
432 &(tb->text[tb->s_begin]),
433 tb->d.font, tb->color,
434 False,
435 (tb->s_end - tb->s_begin));
436 } else {
437 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
438 &(tb->text[tb->s_begin]),
439 (isFont ? font : tb->d.
440 font),
441 (isFont ? tb->
442 color : color), False,
443 (tb->s_end - tb->s_begin));
446 if (midtb) {
447 if (underlined != -1) {
448 midtb->underlined = underlined;
449 } else {
450 midtb->underlined = otb->underlined;
453 midtb->selected = !True;
454 midtb->s_begin = 0;
455 midtb->s_end = midtb->used;
456 tPtr->currentTextBlock = tb;
457 WMAppendTextBlock(tPtr, midtb);
458 tb = tPtr->currentTextBlock;
461 if (otb->used - otb->s_end > 0) {
462 TextBlock *ntb;
463 ntb = (TextBlock *)
464 WMCreateTextBlockWithText(tPtr,
465 &(otb->text[otb->s_end]), otb->d.font,
466 otb->color, False, otb->used - otb->s_end);
468 if (ntb) {
469 ntb->underlined = otb->underlined;
470 ntb->selected = False;
471 WMAppendTextBlock(tPtr, ntb);
472 tb = tPtr->currentTextBlock;
476 if (midtb) {
477 tPtr->currentTextBlock = midtb;
480 otb->selected = False;
481 otb->used = otb->s_begin;
485 tb = tb->next;
488 tPtr->flags.needsLayOut = True;
489 WMThawText(tPtr);
491 /* in case the size changed... */
492 if (isFont && tPtr->currentTextBlock) {
493 TextBlock *tb = tPtr->currentTextBlock;
495 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
496 tPtr->sel.y = 3 + tb->sections[0]._y;
497 tPtr->sel.h = tb->sections[tb->nsections - 1]._y - tb->sections[0]._y;
498 tPtr->sel.w = tb->sections[tb->nsections - 1].w;
499 if (tb->sections[tb->nsections - 1]._y != tb->sections[0]._y) {
500 tPtr->sel.x = 0;
502 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
507 static Bool removeSelection(Text * tPtr)
509 TextBlock *tb = NULL;
510 Bool first = False;
512 if (!(tb = tPtr->firstTextBlock))
513 return False;
515 while (tb) {
516 if (tb->selected) {
517 if (!first && !tb->graphic) {
518 WMReleaseFont(tPtr->dFont);
519 tPtr->dFont = WMRetainFont(tb->d.font);
520 first = True;
523 if ((tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
524 tPtr->currentTextBlock = tb;
525 if (tb->next) {
526 tPtr->tpos = 0;
527 } else if (tb->prior) {
528 if (tb->prior->graphic)
529 tPtr->tpos = 1;
530 else
531 tPtr->tpos = tb->prior->used;
532 } else
533 tPtr->tpos = 0;
535 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
536 tb = tPtr->currentTextBlock;
537 continue;
539 } else if (tb->s_end <= tb->used) {
540 memmove(&(tb->text[tb->s_begin]), &(tb->text[tb->s_end]), tb->used - tb->s_end);
541 tb->used -= (tb->s_end - tb->s_begin);
542 tb->selected = False;
543 tPtr->tpos = tb->s_begin;
548 tb = tb->next;
550 return True;
553 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
555 TextBlock *hold = tb;
557 if (!tb)
558 return NULL;
560 while (tb) {
561 if (!tb->graphic)
562 break;
563 tb = (dir ? tb->next : tb->prior);
566 if (!tb) {
567 tb = hold;
568 while (tb) {
569 if (!tb->graphic)
570 break;
571 tb = (dir ? tb->prior : tb->next);
575 if (!tb)
576 return NULL;
578 return tb;
581 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
583 if (tPtr->flags.monoFont && tb->graphic) {
584 tb = getFirstNonGraphicBlockFor(tb, *dir);
585 if (!tb)
586 return 0;
588 if (tb->graphic) {
589 tPtr->currentTextBlock = (*dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
590 tPtr->tpos = 0;
591 return 0;
595 if (!tb->sections) {
596 layOutDocument(tPtr);
597 return 0;
600 *dir = !(y <= tb->sections[0].y);
601 if (*dir) {
602 if ((y <= tb->sections[0]._y + tb->sections[0].h)
603 && (y >= tb->sections[0]._y)) {
604 /* if it's on the same line */
605 if (x < tb->sections[0].x)
606 *dir = 0;
608 } else {
609 if ((y <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
610 && (y >= tb->sections[tb->nsections - 1]._y)) {
611 /* if it's on the same line */
612 if (x > tb->sections[tb->nsections - 1].x)
613 *dir = 1;
617 return 1;
620 static void paintText(Text * tPtr)
622 TextBlock *tb;
623 WMFont *font;
624 const char *text;
625 int len, y, c, s, done = False, dir; /* dir 1 = down */
626 WMScreen *scr = tPtr->view->screen;
627 Display *dpy = tPtr->view->screen->display;
628 Window win = tPtr->view->window;
629 WMColor *color;
631 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
632 return;
634 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
636 if (tPtr->bgPixmap) {
637 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
638 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
639 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
642 if (!(tb = tPtr->currentTextBlock)) {
643 if (!(tb = tPtr->firstTextBlock)) {
644 goto _copy_area;
648 done = False;
650 /* first, which direction? Don't waste time looking all over,
651 since the parts to be drawn will most likely be near what
652 was previously drawn */
653 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
654 goto _copy_area;
656 while (tb) {
658 if (tb->graphic && tPtr->flags.monoFont)
659 goto _getSibling;
661 if (dir) {
662 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
663 break;
664 } else {
665 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
666 break;
669 _getSibling:
670 if (dir) {
671 if (tb->next)
672 tb = tb->next;
673 else
674 break;
675 } else {
676 if (tb->prior)
677 tb = tb->prior;
678 else
679 break;
683 /* first, place all text that can be viewed */
684 while (!done && tb) {
685 if (tb->graphic) {
686 tb = tb->next;
687 continue;
690 tb->selected = False;
692 for (s = 0; s < tb->nsections && !done; s++) {
694 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
695 done = True;
696 break;
699 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
700 continue;
702 if (tPtr->flags.monoFont) {
703 font = tPtr->dFont;
704 color = tPtr->fgColor;
705 } else {
706 font = tb->d.font;
707 color = tb->color;
710 if (tPtr->flags.ownsSelection) {
711 XRectangle rect;
713 if (sectionWasSelected(tPtr, tb, &rect, s)) {
714 tb->selected = True;
715 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
716 rect.x, rect.y, rect.width, rect.height);
720 len = tb->sections[s].end - tb->sections[s].begin;
721 text = &(tb->text[tb->sections[s].begin]);
722 y = tb->sections[s].y - tPtr->vpos;
723 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
725 if (!tPtr->flags.monoFont && tb->underlined) {
726 XDrawLine(dpy, tPtr->db, WMColorGC(color),
727 tb->sections[s].x - tPtr->hpos,
728 y + font->y + 1,
729 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
732 tb = (!done ? tb->next : NULL);
735 /* now , show all graphic items that can be viewed */
736 c = WMGetArrayItemCount(tPtr->gfxItems);
737 if (c > 0 && !tPtr->flags.monoFont) {
738 int j, h;
740 for (j = 0; j < c; j++) {
741 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
743 /* if it's not viewable, and mapped, unmap it */
744 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
745 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
747 if (tb->object) {
748 if ((W_VIEW(tb->d.widget))->flags.mapped) {
749 WMUnmapWidget(tb->d.widget);
752 } else {
753 /* if it's viewable, and not mapped, map it */
754 if (tb->object) {
755 W_View *view = W_VIEW(tb->d.widget);
757 if (!view->flags.realized)
758 WMRealizeWidget(tb->d.widget);
759 if (!view->flags.mapped) {
760 XMapWindow(view->screen->display, view->window);
761 XFlush(view->screen->display);
762 view->flags.mapped = 1;
766 if (tb->object) {
767 WMMoveWidget(tb->d.widget,
768 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
769 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
770 h = WMWidgetHeight(tb->d.widget) + 1;
772 } else {
773 WMDrawPixmap(tb->d.pixmap, tPtr->db,
774 tb->sections[0].x - tPtr->hpos,
775 tb->sections[0].y - tPtr->vpos);
776 h = tb->d.pixmap->height + 1;
780 if (tPtr->flags.ownsSelection) {
781 XRectangle rect;
783 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
784 Drawable d = (0 && tb->object ?
785 (WMWidgetView(tb->d.widget))->window : tPtr->db);
787 tb->selected = True;
788 XFillRectangle(dpy, d, tPtr->stippledGC,
789 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
790 rect.x, rect.y, rect.width, rect.height);
794 if (!tPtr->flags.monoFont && tb->underlined) {
795 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
796 tb->sections[0].x - tPtr->hpos,
797 tb->sections[0].y + h - tPtr->vpos,
798 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
799 tb->sections[0].y + h - tPtr->vpos);
805 _copy_area:
806 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
807 int y = tPtr->cursor.y - tPtr->vpos;
808 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
809 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
812 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
813 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
815 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
817 if (tPtr->ruler && tPtr->flags.rulerShown)
818 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
822 static void mouseOverObject(Text * tPtr, int x, int y)
824 TextBlock *tb;
825 Bool result = False;
827 x -= tPtr->visible.x;
828 x += tPtr->hpos;
829 y -= tPtr->visible.y;
830 y += tPtr->vpos;
832 if (tPtr->flags.ownsSelection) {
833 if (tPtr->sel.x <= x
834 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
835 tPtr->flags.isOverGraphic = 1;
836 result = True;
840 if (!result) {
841 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
843 if (c < 1)
844 tPtr->flags.isOverGraphic = 0;
846 for (j = 0; j < c; j++) {
847 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
849 if (!tb || !tb->sections) {
850 tPtr->flags.isOverGraphic = 0;
851 return;
854 if (!tb->object) {
855 if (tb->sections[0].x <= x
856 && tb->sections[0].y <= y
857 && tb->sections[0].x + tb->sections[0].w >= x
858 && tb->sections[0].y + tb->d.pixmap->height >= y) {
859 tPtr->flags.isOverGraphic = 3;
860 result = True;
861 break;
868 if (!result)
869 tPtr->flags.isOverGraphic = 0;
871 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
873 XSetWindowAttributes attribs;
874 attribs.cursor = tPtr->view->attribs.cursor;
875 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
879 #if DO_BLINK
881 static void blinkCursor(void *data)
883 Text *tPtr = (Text *) data;
885 if (tPtr->flags.cursorShown) {
886 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
887 } else {
888 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
890 paintText(tPtr);
891 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
893 #endif
895 static void updateCursorPosition(Text * tPtr)
897 TextBlock *tb = NULL;
898 int x, y, h, s;
900 if (tPtr->flags.needsLayOut)
901 layOutDocument(tPtr);
903 if (!(tb = tPtr->currentTextBlock)) {
904 if (!(tb = tPtr->firstTextBlock)) {
905 WMFont *font = tPtr->dFont;
906 tPtr->tpos = 0;
907 tPtr->cursor.h = font->height + abs(font->height - font->y);
909 tPtr->cursor.y = 2;
910 tPtr->cursor.x = 2;
911 return;
915 if (tb->blank) {
916 tPtr->tpos = 0;
917 y = tb->sections[0].y;
918 h = tb->sections[0].h;
919 x = tb->sections[0].x;
921 } else if (tb->graphic) {
922 y = tb->sections[0].y;
923 h = tb->sections[0].h;
924 x = tb->sections[0].x;
925 if (tPtr->tpos == 1)
926 x += tb->sections[0].w;
928 } else {
929 if (tPtr->tpos > tb->used)
930 tPtr->tpos = tb->used;
932 for (s = 0; s < tb->nsections - 1; s++) {
934 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
935 break;
938 y = tb->sections[s]._y;
939 h = tb->sections[s].h;
940 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
941 &tb->text[tb->sections[s].begin],
942 tPtr->tpos - tb->sections[s].begin);
945 tPtr->cursor.y = y;
946 tPtr->cursor.h = h;
947 tPtr->cursor.x = x;
949 /* scroll the bars if the cursor is not visible */
950 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
951 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
952 tPtr->vpos +=
953 (tPtr->cursor.y + tPtr->cursor.h + 10
954 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
955 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
956 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
961 updateScrollers(tPtr);
964 static void cursorToTextPosition(Text * tPtr, int x, int y)
966 TextBlock *tb = NULL;
967 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
968 const char *text;
970 if (tPtr->flags.needsLayOut)
971 layOutDocument(tPtr);
973 y += (tPtr->vpos - tPtr->visible.y);
974 if (y < 0)
975 y = 0;
977 x -= (tPtr->visible.x - 2);
978 if (x < 0)
979 x = 0;
981 /* clicked is relative to document, not window... */
982 tPtr->clicked.x = x;
983 tPtr->clicked.y = y;
985 if (!(tb = tPtr->currentTextBlock)) {
986 if (!(tb = tPtr->firstTextBlock)) {
987 WMFont *font = tPtr->dFont;
988 tPtr->tpos = 0;
989 tPtr->cursor.h = font->height + abs(font->height - font->y);
990 tPtr->cursor.y = 2;
991 tPtr->cursor.x = 2;
992 return;
996 /* first, which direction? Most likely, newly clicked
997 position will be close to previous */
998 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
999 return;
1001 s = (dir ? 0 : tb->nsections - 1);
1002 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
1003 goto _doneV;
1006 /* get the first (or last) section of the TextBlock that
1007 lies about the vertical click point */
1008 done = False;
1009 while (!done && tb) {
1011 if (tPtr->flags.monoFont && tb->graphic) {
1012 if ((dir ? tb->next : tb->prior))
1013 tb = (dir ? tb->next : tb->prior);
1014 continue;
1017 s = (dir ? 0 : tb->nsections - 1);
1018 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1020 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1021 done = True;
1022 } else {
1023 dir ? s++ : s--;
1027 if (!done) {
1028 if ((dir ? tb->next : tb->prior)) {
1029 tb = (dir ? tb->next : tb->prior);
1030 } else {
1031 break; /* goto _doneH; */
1036 if (s < 0 || s >= tb->nsections) {
1037 s = (dir ? tb->nsections - 1 : 0);
1040 _doneV:
1041 /* we have the line, which TextBlock on that line is it? */
1042 pos = (dir ? 0 : tb->sections[s].begin);
1043 if (tPtr->flags.monoFont && tb->graphic) {
1044 TextBlock *hold = tb;
1045 tb = getFirstNonGraphicBlockFor(hold, dir);
1047 if (!tb) {
1048 tPtr->tpos = 0;
1049 tb = hold;
1050 s = 0;
1051 goto _doNothing;
1055 _y = tb->sections[s]._y;
1057 while (tb) {
1059 if (tPtr->flags.monoFont && tb->graphic) {
1060 tb = (dir ? tb->next : tb->prior);
1061 continue;
1064 if (dir) {
1065 if (tb->graphic) {
1066 if (tb->object)
1067 _w = WMWidgetWidth(tb->d.widget) - 5;
1068 else
1069 _w = tb->d.pixmap->width - 5;
1071 if (tb->sections[0].x + _w >= x)
1072 break;
1073 } else {
1074 text = &(tb->text[tb->sections[s].begin]);
1075 len = tb->sections[s].end - tb->sections[s].begin;
1076 _w = WMWidthOfString(tb->d.font, text, len);
1077 if (tb->sections[s].x + _w >= x)
1078 break;
1081 } else {
1082 if (tb->sections[s].x <= x)
1083 break;
1086 if ((dir ? tb->next : tb->prior)) {
1087 TextBlock *nxt = (dir ? tb->next : tb->prior);
1088 if (tPtr->flags.monoFont && nxt->graphic) {
1089 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1090 if (!nxt) {
1091 pos = (dir ? 0 : tb->sections[s].begin);
1092 tPtr->cursor.x = tb->sections[s].x;
1093 goto _doneH;
1097 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1098 /* this must be the last/first on this line. stop */
1099 pos = (dir ? tb->sections[s].end : 0);
1100 tPtr->cursor.x = tb->sections[s].x;
1101 if (!tb->blank) {
1102 if (tb->graphic) {
1103 if (tb->object)
1104 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1105 else
1106 tPtr->cursor.x += tb->d.pixmap->width;
1107 } else if (pos > tb->sections[s].begin) {
1108 tPtr->cursor.x +=
1109 WMWidthOfString(tb->d.font,
1110 &(tb->text[tb->sections[s].begin]),
1111 pos - tb->sections[s].begin);
1114 goto _doneH;
1118 if ((dir ? tb->next : tb->prior)) {
1119 tb = (dir ? tb->next : tb->prior);
1120 } else {
1121 break;
1124 if (tb)
1125 s = (dir ? 0 : tb->nsections - 1);
1128 /* we have said TextBlock, now where within it? */
1129 if (tb) {
1130 if (tb->graphic) {
1131 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1133 tPtr->cursor.x = tb->sections[0].x;
1135 if (x > tPtr->cursor.x + gw / 2) {
1136 pos = 1;
1137 tPtr->cursor.x += gw;
1138 } else {
1139 printf("first %d\n", tb->first);
1140 if (tb->prior) {
1141 if (tb->prior->graphic)
1142 pos = 1;
1143 else
1144 pos = tb->prior->used;
1145 tb = tb->prior;
1146 } else
1147 pos = 0;
1151 s = 0;
1152 goto _doneH;
1154 } else {
1155 WMFont *f = tb->d.font;
1156 len = tb->sections[s].end - tb->sections[s].begin;
1157 text = &(tb->text[tb->sections[s].begin]);
1159 _w = x - tb->sections[s].x;
1160 pos = 0;
1162 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1163 pos++;
1165 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1167 pos += tb->sections[s].begin;
1171 _doneH:
1172 if (tb->graphic) {
1173 tPtr->tpos = (pos <= 1) ? pos : 0;
1174 } else {
1175 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1177 _doNothing:
1178 if (!tb)
1179 printf("...for this app will surely crash :-)\n");
1181 tPtr->currentTextBlock = tb;
1182 tPtr->cursor.h = tb->sections[s].h;
1183 tPtr->cursor.y = tb->sections[s]._y;
1185 /* scroll the bars if the cursor is not visible */
1186 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1187 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1188 tPtr->vpos +=
1189 (tPtr->cursor.y + tPtr->cursor.h + 10
1190 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1191 updateScrollers(tPtr);
1192 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1193 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1194 updateScrollers(tPtr);
1201 static void updateScrollers(Text * tPtr)
1204 if (tPtr->flags.frozen)
1205 return;
1207 if (tPtr->vS) {
1208 if (tPtr->docHeight <= tPtr->visible.h) {
1209 WMSetScrollerParameters(tPtr->vS, 0, 1);
1210 tPtr->vpos = 0;
1211 } else {
1212 float hmax = (float)(tPtr->docHeight);
1213 WMSetScrollerParameters(tPtr->vS,
1214 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1215 (float)tPtr->visible.h / hmax);
1217 } else
1218 tPtr->vpos = 0;
1220 if (tPtr->hS) {
1221 if (tPtr->docWidth <= tPtr->visible.w) {
1222 WMSetScrollerParameters(tPtr->hS, 0, 1);
1223 tPtr->hpos = 0;
1224 } else {
1225 float wmax = (float)(tPtr->docWidth);
1226 WMSetScrollerParameters(tPtr->hS,
1227 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1228 (float)tPtr->visible.w / wmax);
1230 } else
1231 tPtr->hpos = 0;
1234 static void scrollersCallBack(WMWidget * w, void *self)
1236 Text *tPtr = (Text *) self;
1237 Bool scroll = False;
1238 int which;
1240 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1241 return;
1243 if (w == tPtr->vS) {
1244 int height;
1245 height = tPtr->visible.h;
1247 which = WMGetScrollerHitPart(tPtr->vS);
1248 switch (which) {
1250 case WSDecrementLine:
1251 if (tPtr->vpos > 0) {
1252 if (tPtr->vpos > 16)
1253 tPtr->vpos -= 16;
1254 else
1255 tPtr->vpos = 0;
1257 break;
1259 case WSIncrementLine:{
1260 int limit = tPtr->docHeight - height;
1261 if (tPtr->vpos < limit) {
1262 if (tPtr->vpos < limit - 16)
1263 tPtr->vpos += 16;
1264 else
1265 tPtr->vpos = limit;
1268 break;
1270 case WSDecrementPage:
1271 if (((int)tPtr->vpos - (int)height) >= 0)
1272 tPtr->vpos -= height;
1273 else
1274 tPtr->vpos = 0;
1275 break;
1277 case WSIncrementPage:
1278 tPtr->vpos += height;
1279 if (tPtr->vpos > (tPtr->docHeight - height))
1280 tPtr->vpos = tPtr->docHeight - height;
1281 break;
1283 case WSKnob:
1284 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1285 * (float)(tPtr->docHeight - height);
1286 break;
1288 case WSKnobSlot:
1289 case WSNoPart:
1290 break;
1292 scroll = (tPtr->vpos != tPtr->prevVpos);
1293 tPtr->prevVpos = tPtr->vpos;
1296 if (w == tPtr->hS) {
1297 int width = tPtr->visible.w;
1299 which = WMGetScrollerHitPart(tPtr->hS);
1300 switch (which) {
1302 case WSDecrementLine:
1303 if (tPtr->hpos > 0) {
1304 if (tPtr->hpos > 16)
1305 tPtr->hpos -= 16;
1306 else
1307 tPtr->hpos = 0;
1309 break;
1311 case WSIncrementLine:{
1312 int limit = tPtr->docWidth - width;
1313 if (tPtr->hpos < limit) {
1314 if (tPtr->hpos < limit - 16)
1315 tPtr->hpos += 16;
1316 else
1317 tPtr->hpos = limit;
1320 break;
1322 case WSDecrementPage:
1323 if (((int)tPtr->hpos - (int)width) >= 0)
1324 tPtr->hpos -= width;
1325 else
1326 tPtr->hpos = 0;
1327 break;
1329 case WSIncrementPage:
1330 tPtr->hpos += width;
1331 if (tPtr->hpos > (tPtr->docWidth - width))
1332 tPtr->hpos = tPtr->docWidth - width;
1333 break;
1335 case WSKnob:
1336 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1337 * (float)(tPtr->docWidth - width);
1338 break;
1340 case WSKnobSlot:
1341 case WSNoPart:
1342 break;
1344 scroll = (tPtr->hpos != tPtr->prevHpos);
1345 tPtr->prevHpos = tPtr->hpos;
1348 if (scroll) {
1349 updateScrollers(tPtr);
1350 paintText(tPtr);
1354 typedef struct {
1355 TextBlock *tb;
1356 unsigned short begin, end; /* what part of the text block */
1357 } myLineItems;
1359 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1361 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1362 WMFont *font;
1363 const char *text;
1364 TextBlock *tb, *tbsame = NULL;
1366 if (!items || nitems == 0)
1367 return 0;
1369 for (i = 0; i < nitems; i++) {
1370 tb = items[i].tb;
1372 if (tb->graphic) {
1373 if (!tPtr->flags.monoFont) {
1374 if (tb->object) {
1375 WMWidget *wdt = tb->d.widget;
1376 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1377 if (tPtr->flags.alignment != WALeft)
1378 lw += WMWidgetWidth(wdt);
1379 } else {
1380 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1381 if (tPtr->flags.alignment != WALeft)
1382 lw += tb->d.pixmap->width;
1386 } else {
1387 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1388 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1389 max_d = 2;
1390 line_height = WMAX(line_height, font->height + max_d);
1391 text = &(tb->text[items[i].begin]);
1392 len = items[i].end - items[i].begin;
1393 if (tPtr->flags.alignment != WALeft)
1394 lw += WMWidthOfString(font, text, len);
1398 if (tPtr->flags.alignment == WARight) {
1399 j = tPtr->visible.w - lw;
1400 } else if (tPtr->flags.alignment == WACenter) {
1401 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1404 for (i = 0; i < nitems; i++) {
1405 tb = items[i].tb;
1407 if (tbsame == tb) { /* extend it, since it's on same line */
1408 tb->sections[tb->nsections - 1].end = items[i].end;
1409 n = tb->nsections - 1;
1410 } else {
1411 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1412 n = tb->nsections - 1;
1413 tb->sections[n]._y = y + max_d;
1414 tb->sections[n].max_d = max_d;
1415 tb->sections[n].x = x + j;
1416 tb->sections[n].h = line_height;
1417 tb->sections[n].begin = items[i].begin;
1418 tb->sections[n].end = items[i].end;
1421 tb->sections[n].last = (i + 1 == nitems);
1423 if (tb->graphic) {
1424 if (!tPtr->flags.monoFont) {
1425 if (tb->object) {
1426 WMWidget *wdt = tb->d.widget;
1427 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1428 tb->sections[n].w = WMWidgetWidth(wdt);
1429 } else {
1430 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1431 tb->sections[n].w = tb->d.pixmap->width;
1433 x += tb->sections[n].w;
1435 } else {
1436 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1437 len = items[i].end - items[i].begin;
1438 text = &(tb->text[items[i].begin]);
1440 tb->sections[n].y = y + line_height - font->y;
1441 tb->sections[n].w =
1442 WMWidthOfString(font,
1443 &(tb->text[tb->sections[n].begin]),
1444 tb->sections[n].end - tb->sections[n].begin);
1446 x += WMWidthOfString(font, text, len);
1449 tbsame = tb;
1452 return line_height;
1456 static void layOutDocument(Text * tPtr)
1458 TextBlock *tb;
1459 myLineItems *items = NULL;
1460 unsigned int itemsSize = 0, nitems = 0, begin, end;
1461 WMFont *font;
1462 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1463 const char *start = NULL, *mark = NULL;
1465 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1466 return;
1468 assert(tPtr->visible.w > 20);
1470 tPtr->docWidth = tPtr->visible.w;
1471 x = tPtr->margins[tb->marginN].first;
1472 bmargin = tPtr->margins[tb->marginN].body;
1474 /* only partial layOut needed: re-Lay only affected textblocks */
1475 if (tPtr->flags.laidOut) {
1476 tb = tPtr->currentTextBlock;
1478 /* search backwards for textblocks on same line */
1479 while (tb->prior) {
1480 if (!tb->sections || tb->nsections < 1) {
1481 tb = tPtr->firstTextBlock;
1482 tPtr->flags.laidOut = False;
1483 y = 0;
1484 goto _layOut;
1487 if (!tb->prior->sections || tb->prior->nsections < 1) {
1488 tb = tPtr->firstTextBlock;
1489 tPtr->flags.laidOut = False;
1490 y = 0;
1491 goto _layOut;
1494 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1495 break;
1497 tb = tb->prior;
1500 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1501 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1502 tb->prior->sections[tb->prior->nsections - 1].h -
1503 tb->prior->sections[tb->prior->nsections - 1].max_d;
1504 } else {
1505 y = 0;
1509 _layOut:
1510 while (tb) {
1512 if (tb->sections && tb->nsections > 0) {
1513 wfree(tb->sections);
1514 tb->sections = NULL;
1515 tb->nsections = 0;
1518 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1519 TextBlock *next = tb->next;
1520 tPtr->currentTextBlock = tb;
1521 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1522 tb = next;
1523 tb->first = True;
1524 continue;
1527 if (tb->first && tb != tPtr->firstTextBlock) {
1528 y += layOutLine(tPtr, items, nitems, x, y);
1529 x = tPtr->margins[tb->marginN].first;
1530 bmargin = tPtr->margins[tb->marginN].body;
1531 nitems = 0;
1532 lw = 0;
1535 if (tb->graphic) {
1536 if (!tPtr->flags.monoFont) {
1537 if (tb->object)
1538 width = WMWidgetWidth(tb->d.widget);
1539 else
1540 width = tb->d.pixmap->width;
1542 if (width > tPtr->docWidth)
1543 tPtr->docWidth = width;
1545 lw += width;
1546 if (lw >= tPtr->visible.w - x) {
1547 y += layOutLine(tPtr, items, nitems, x, y);
1548 nitems = 0;
1549 x = bmargin;
1550 lw = width;
1553 if (nitems + 1 > itemsSize) {
1554 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1557 items[nitems].tb = tb;
1558 items[nitems].begin = 0;
1559 items[nitems].end = 0;
1560 nitems++;
1563 } else if ((start = tb->text)) {
1564 begin = end = 0;
1565 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1567 while (start) {
1568 mark = strchr(start, ' ');
1569 if (mark) {
1570 end += (int)(mark - start) + 1;
1571 start = mark + 1;
1572 } else {
1573 end += strlen(start);
1574 start = mark;
1577 if (end > tb->used)
1578 end = tb->used;
1580 if (end - begin > 0) {
1582 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1584 /* if it won't fit, char wrap it */
1585 if (width >= tPtr->visible.w) {
1586 char *t = &tb->text[begin];
1587 int l = end - begin, i = 0;
1588 do {
1589 width = WMWidthOfString(font, t, ++i);
1590 } while (width < tPtr->visible.w && i < l);
1591 if (i > 2)
1592 i--;
1593 end = begin + i;
1594 start = &tb->text[end];
1597 lw += width;
1600 if (lw >= tPtr->visible.w - x) {
1601 y += layOutLine(tPtr, items, nitems, x, y);
1602 lw = width;
1603 x = bmargin;
1604 nitems = 0;
1607 if (nitems + 1 > itemsSize) {
1608 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1611 items[nitems].tb = tb;
1612 items[nitems].begin = begin;
1613 items[nitems].end = end;
1614 nitems++;
1616 begin = end;
1620 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1621 if (0 && tPtr->flags.laidOut
1622 && tb->next && tb->next->sections && tb->next->nsections > 0
1623 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1624 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1625 TextBlock *ltb = tPtr->lastTextBlock;
1626 int ly = ltb->sections[ltb->nsections - 1]._y;
1627 int lh = ltb->sections[ltb->nsections - 1].h;
1628 int ss, sd;
1630 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1631 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1633 y += layOutLine(tPtr, items, nitems, x, y);
1634 ss = ly + lh - y;
1635 sd = tPtr->docHeight - y;
1637 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1638 y += tb->next->sections[0]._y - y;
1639 nitems = 0;
1640 printf("nitems%d\n", nitems);
1641 if (ss - sd != 0)
1642 y = tPtr->docHeight + ss - sd;
1644 break;
1645 } else {
1646 tPtr->flags.laidOut = False;
1650 tb = tb->next;
1653 if (nitems > 0)
1654 y += layOutLine(tPtr, items, nitems, x, y);
1656 if (tPtr->docHeight != y + 10) {
1657 tPtr->docHeight = y + 10;
1658 updateScrollers(tPtr);
1661 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1662 XEvent event;
1664 tPtr->flags.horizOnDemand = True;
1665 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1666 event.type = Expose;
1667 handleEvents(&event, (void *)tPtr);
1669 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1670 tPtr->flags.horizOnDemand = False;
1671 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1674 tPtr->flags.laidOut = True;
1676 if (items && itemsSize > 0)
1677 wfree(items);
1681 static void textDidResize(W_ViewDelegate * self, WMView * view)
1683 Text *tPtr = (Text *) view->self;
1684 unsigned short w = tPtr->view->size.width;
1685 unsigned short h = tPtr->view->size.height;
1686 unsigned short rh = 0, vw = 0, rel;
1688 /* Parameter not used, but tell the compiler that it is ok */
1689 (void) self;
1691 rel = (tPtr->flags.relief == WRFlat);
1693 if (tPtr->ruler && tPtr->flags.rulerShown) {
1694 WMMoveWidget(tPtr->ruler, 2, 2);
1695 WMResizeWidget(tPtr->ruler, w - 4, 40);
1696 rh = 40;
1699 if (tPtr->vS) {
1700 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1701 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1702 vw = 20;
1703 WMSetRulerOffset(tPtr->ruler, 22);
1704 } else
1705 WMSetRulerOffset(tPtr->ruler, 2);
1707 if (tPtr->hS) {
1708 if (tPtr->vS) {
1709 WMMoveWidget(tPtr->hS, vw, h - 21);
1710 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1711 } else {
1712 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1713 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1717 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1718 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1719 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1720 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1721 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1722 tPtr->margins[0].right = tPtr->visible.w;
1724 if (tPtr->view->flags.realized) {
1726 if (tPtr->db) {
1727 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1728 tPtr->db = (Pixmap) NULL;
1731 if (tPtr->visible.w < 40)
1732 tPtr->visible.w = 40;
1733 if (tPtr->visible.h < 20)
1734 tPtr->visible.h = 20;
1736 if (!tPtr->db) {
1737 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1738 tPtr->view->window, tPtr->visible.w,
1739 tPtr->visible.h, tPtr->view->screen->depth);
1743 WMThawText(tPtr);
1746 W_ViewDelegate _TextViewDelegate = {
1747 NULL,
1748 NULL,
1749 textDidResize,
1750 NULL,
1751 NULL
1754 #define TEXT_BUFFER_INCR 8
1755 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1757 static void clearText(Text * tPtr)
1759 tPtr->vpos = tPtr->hpos = 0;
1760 tPtr->docHeight = tPtr->docWidth = 0;
1761 tPtr->cursor.x = -23;
1763 if (!tPtr->firstTextBlock)
1764 return;
1766 while (tPtr->currentTextBlock)
1767 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1769 tPtr->firstTextBlock = NULL;
1770 tPtr->currentTextBlock = NULL;
1771 tPtr->lastTextBlock = NULL;
1772 WMEmptyArray(tPtr->gfxItems);
1775 /* possibly remove a single character from the currentTextBlock,
1776 or if there's a selection, remove it...
1777 note that Delete and Backspace are treated differently */
1778 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1780 TextBlock *tb;
1781 Bool back = (Bool) (ksym == XK_BackSpace);
1782 Bool done = 1, wasFirst = 0;
1784 if (!tPtr->flags.editable)
1785 return;
1787 if (!(tb = tPtr->currentTextBlock))
1788 return;
1790 if (tPtr->flags.ownsSelection) {
1791 if (removeSelection(tPtr))
1792 layOutDocument(tPtr);
1793 return;
1796 wasFirst = tb->first;
1797 if (back && tPtr->tpos < 1) {
1798 if (tb->prior) {
1799 if (tb->prior->blank) {
1800 tPtr->currentTextBlock = tb->prior;
1801 WMRemoveTextBlock(tPtr);
1802 tPtr->currentTextBlock = tb;
1803 tb->first = True;
1804 layOutDocument(tPtr);
1805 return;
1806 } else {
1807 if (tb->blank) {
1808 TextBlock *prior = tb->prior;
1809 tPtr->currentTextBlock = tb;
1810 WMRemoveTextBlock(tPtr);
1811 tb = prior;
1812 } else {
1813 tb = tb->prior;
1816 if (tb->graphic)
1817 tPtr->tpos = 1;
1818 else
1819 tPtr->tpos = tb->used;
1821 tPtr->currentTextBlock = tb;
1822 done = 1;
1823 if (wasFirst) {
1824 if (tb->next)
1825 tb->next->first = False;
1826 layOutDocument(tPtr);
1827 return;
1833 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1834 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1835 if (back)
1836 tPtr->tpos--;
1837 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1838 tb->used--;
1839 done = 0;
1842 /* if there are no characters left to back over in the textblock,
1843 but it still has characters to the right of the cursor: */
1844 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1845 || tb->graphic) {
1847 /* no more chars, and it's marked as blank? */
1848 if (tb->blank) {
1849 TextBlock *sibling = (back ? tb->prior : tb->next);
1851 if (tb->used == 0 || tb->graphic)
1852 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1854 if (sibling) {
1855 tPtr->currentTextBlock = sibling;
1856 if (tb->graphic)
1857 tPtr->tpos = (back ? 1 : 0);
1858 else
1859 tPtr->tpos = (back ? sibling->used : 0);
1861 /* no more chars, so mark it as blank */
1862 } else if (tb->used == 0) {
1863 tb->blank = 1;
1864 } else if (tb->graphic) {
1865 Bool hasNext = (tb->next != NULL);
1867 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1868 if (hasNext) {
1869 tPtr->tpos = 0;
1870 } else if (tPtr->currentTextBlock) {
1871 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1873 } else
1874 printf("DEBUG: unaccounted for... catch this!\n");
1877 layOutDocument(tPtr);
1880 static void insertTextInteractively(Text * tPtr, char *text, int len)
1882 TextBlock *tb;
1883 char *newline = NULL;
1885 if (!tPtr->flags.editable) {
1886 return;
1889 if (len < 1 || !text)
1890 return;
1892 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1893 return;
1895 if (tPtr->flags.ownsSelection)
1896 removeSelection(tPtr);
1898 if (tPtr->flags.ignoreNewLine) {
1899 int i;
1900 for (i = 0; i < len; i++) {
1901 if (text[i] == '\n')
1902 text[i] = ' ';
1906 tb = tPtr->currentTextBlock;
1907 if (!tb || tb->graphic) {
1908 tPtr->tpos = 0;
1909 WMAppendTextStream(tPtr, text);
1910 layOutDocument(tPtr);
1911 return;
1914 if ((newline = strchr(text, '\n'))) {
1915 int nlen = (int)(newline - text);
1916 int s = tb->used - tPtr->tpos;
1918 if (!tb->blank && nlen > 0) {
1919 char *save = NULL;
1921 if (s > 0) {
1922 save = wmalloc(s);
1923 memcpy(save, &tb->text[tPtr->tpos], s);
1924 tb->used -= (tb->used - tPtr->tpos);
1926 insertTextInteractively(tPtr, text, nlen);
1927 newline++;
1928 WMAppendTextStream(tPtr, newline);
1929 if (s > 0) {
1930 insertTextInteractively(tPtr, save, s);
1931 wfree(save);
1933 } else {
1934 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1936 unsigned short savePos = tPtr->tpos;
1937 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1938 tb->d.font, tb->color, True,
1939 tb->used - tPtr->tpos);
1941 if (tb->sections[0].end == tPtr->tpos)
1942 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1943 NULL, tb->d.font,
1944 tb->color, True, 0));
1946 tb->used = savePos;
1947 WMAppendTextBlock(tPtr, ntb);
1948 tPtr->tpos = 0;
1950 } else if (tPtr->tpos == tb->used) {
1951 if (tPtr->flags.indentNewLine) {
1952 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1953 " ", tb->d.font,
1954 tb->color, True, 4));
1955 tPtr->tpos = 4;
1956 } else {
1957 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1958 NULL, tb->d.font,
1959 tb->color, True, 0));
1960 tPtr->tpos = 0;
1962 } else if (tPtr->tpos == 0) {
1963 if (tPtr->flags.indentNewLine) {
1964 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1965 " ", tb->d.font,
1966 tb->color, True, 4));
1967 } else {
1968 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1969 NULL, tb->d.font,
1970 tb->color, True, 0));
1972 tPtr->tpos = 0;
1973 if (tPtr->currentTextBlock->next)
1974 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1977 } else {
1978 if (tb->used + len >= tb->allocated) {
1979 tb->allocated = reqBlockSize(tb->used + len);
1980 tb->text = wrealloc(tb->text, tb->allocated);
1983 if (tb->blank) {
1984 memcpy(tb->text, text, len);
1985 tb->used = len;
1986 tPtr->tpos = len;
1987 tb->text[tb->used] = 0;
1988 tb->blank = False;
1990 } else {
1991 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
1992 memmove(&tb->text[tPtr->tpos], text, len);
1993 tb->used += len;
1994 tPtr->tpos += len;
1995 tb->text[tb->used] = 0;
2000 layOutDocument(tPtr);
2003 static void selectRegion(Text * tPtr, int x, int y)
2006 if (x < 0 || y < 0)
2007 return;
2009 y += (tPtr->flags.rulerShown ? 40 : 0);
2010 y += tPtr->vpos;
2011 if (y > 10)
2012 y -= 10; /* the original offset */
2014 x -= tPtr->visible.x - 2;
2015 if (x < 0)
2016 x = 0;
2018 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2019 tPtr->sel.w = abs(tPtr->clicked.x - x);
2020 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2021 tPtr->sel.h = abs(tPtr->clicked.y - y);
2023 tPtr->flags.ownsSelection = True;
2024 paintText(tPtr);
2027 static void releaseSelection(Text * tPtr)
2029 TextBlock *tb = tPtr->firstTextBlock;
2031 while (tb) {
2032 tb->selected = False;
2033 tb = tb->next;
2035 tPtr->flags.ownsSelection = False;
2036 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2038 paintText(tPtr);
2041 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2043 Text *tPtr = view->self;
2044 WMData *data = NULL;
2046 /* Parameter not used, but tell the compiler that it is ok */
2047 (void) selection;
2048 (void) cdata;
2050 if (target == XA_STRING || target == XA_Format_Text || target == XA_Format_Compound_Text) {
2051 char *text = WMGetTextSelectedStream(tPtr);
2053 if (text) {
2054 data = WMCreateDataWithBytes(text, strlen(text));
2055 WMSetDataFormat(data, TYPETEXT);
2056 wfree(text);
2058 *type = target;
2059 return data;
2060 } else
2061 printf("didn't get it\n");
2063 if (target == XA_Targets) {
2064 Atom array[4];
2066 array[0] = XA_Targets;
2067 array[1] = XA_STRING;
2068 array[2] = XA_Format_Text;
2069 array[3] = XA_Format_Compound_Text;
2071 data = WMCreateDataWithBytes(&array, sizeof(array));
2072 WMSetDataFormat(data, 32);
2074 *type = target;
2075 return data;
2078 return NULL;
2081 static void lostHandler(WMView * view, Atom selection, void *cdata)
2083 /* Parameter not used, but tell the compiler that it is ok */
2084 (void) selection;
2085 (void) cdata;
2087 releaseSelection((WMText *) view->self);
2090 static WMSelectionProcs selectionHandler = {
2091 requestHandler, lostHandler, NULL
2094 static void ownershipObserver(void *observerData, WMNotification * notification)
2096 if (observerData != WMGetNotificationClientData(notification))
2097 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2100 static void autoSelectText(Text * tPtr, int clicks)
2102 int x, start;
2103 TextBlock *tb;
2104 char *mark = NULL, behind, ahead;
2106 if (!(tb = tPtr->currentTextBlock))
2107 return;
2109 if (clicks == 2) {
2111 switch (tb->text[tPtr->tpos]) {
2112 case ' ':
2113 return;
2115 case '<': case '>': behind = '<'; ahead = '>'; break;
2116 case '{': case '}': behind = '{'; ahead = '}'; break;
2117 case '[': case ']': behind = '['; ahead = ']'; break;
2119 default:
2120 behind = ahead = ' ';
2123 tPtr->sel.y = tPtr->cursor.y + 5;
2124 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2126 if (tb->graphic) {
2127 tPtr->sel.x = tb->sections[0].x;
2128 tPtr->sel.w = tb->sections[0].w;
2129 } else {
2130 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2132 start = tPtr->tpos;
2133 while (start > 0 && tb->text[start - 1] != behind)
2134 start--;
2136 x = tPtr->cursor.x;
2137 if (tPtr->tpos > start) {
2138 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2140 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2142 if ((mark = strchr(&tb->text[start], ahead))) {
2143 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2144 (int)(mark - &tb->text[start]));
2145 } else if (tb->used > start) {
2146 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2150 } else if (clicks == 3) {
2151 TextBlock *cur = tb;
2153 while (!tb->first) {
2154 tb = tb->prior;
2155 if (tb == NULL) {
2156 #ifndef NDEBUG
2157 wwarning("corrupted list of text blocks in WMText, while searching for first block");
2158 #endif
2159 goto error_select_3clicks;
2162 tPtr->sel.y = tb->sections[0]._y;
2164 tb = cur;
2165 while (tb->next && !tb->next->first) {
2166 tb = tb->next;
2168 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2170 error_select_3clicks:
2171 tPtr->sel.x = 0;
2172 tPtr->sel.w = tPtr->docWidth;
2173 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2176 if (!tPtr->flags.ownsSelection) {
2177 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2178 tPtr->flags.ownsSelection = True;
2180 paintText(tPtr);
2184 # if 0
2185 static void fontChanged(void *observerData, WMNotification * notification)
2187 WMText *tPtr = (WMText *) observerData;
2188 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2189 printf("fontChanged\n");
2191 if (!tPtr || !font)
2192 return;
2194 if (tPtr->flags.ownsSelection)
2195 WMSetTextSelectionFont(tPtr, font);
2197 #endif
2199 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2201 char buffer[64];
2202 KeySym ksym;
2203 int control_pressed = False;
2204 TextBlock *tb = NULL;
2206 if (((XKeyEvent *) event)->state & ControlMask)
2207 control_pressed = True;
2208 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2210 switch (ksym) {
2212 case XK_Home:
2213 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2214 tPtr->tpos = 0;
2215 updateCursorPosition(tPtr);
2216 paintText(tPtr);
2217 break;
2219 case XK_End:
2220 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2221 if (tPtr->currentTextBlock->graphic)
2222 tPtr->tpos = 1;
2223 else
2224 tPtr->tpos = tPtr->currentTextBlock->used;
2226 updateCursorPosition(tPtr);
2227 paintText(tPtr);
2228 break;
2230 case XK_Left:
2231 if (!(tb = tPtr->currentTextBlock))
2232 break;
2234 if (tb->graphic || tPtr->tpos == 0) {
2235 if (tb->prior) {
2236 tPtr->currentTextBlock = tb->prior;
2237 if (tPtr->currentTextBlock->graphic)
2238 tPtr->tpos = 1;
2239 else
2240 tPtr->tpos = tPtr->currentTextBlock->used;
2242 if (!tb->first && tPtr->tpos > 0)
2243 tPtr->tpos--;
2244 } else
2245 tPtr->tpos = 0;
2246 } else
2247 tPtr->tpos--;
2248 updateCursorPosition(tPtr);
2249 paintText(tPtr);
2250 break;
2252 case XK_Right:
2253 if (!(tb = tPtr->currentTextBlock))
2254 break;
2255 if (tb->graphic || tPtr->tpos == tb->used) {
2256 if (tb->next) {
2257 tPtr->currentTextBlock = tb->next;
2258 tPtr->tpos = 0;
2259 if (!tb->next->first && tb->next->used > 0)
2260 tPtr->tpos++;
2261 } else {
2262 if (tb->graphic)
2263 tPtr->tpos = 1;
2264 else
2265 tPtr->tpos = tb->used;
2267 } else
2268 tPtr->tpos++;
2269 updateCursorPosition(tPtr);
2270 paintText(tPtr);
2271 break;
2273 case XK_Down:
2274 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2275 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2276 paintText(tPtr);
2277 break;
2279 case XK_Up:
2280 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2281 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2282 paintText(tPtr);
2283 break;
2285 case XK_BackSpace:
2286 case XK_Delete:
2287 #ifdef XK_KP_Delete
2288 case XK_KP_Delete:
2289 #endif
2290 deleteTextInteractively(tPtr, ksym);
2291 updateCursorPosition(tPtr);
2292 paintText(tPtr);
2293 break;
2295 case XK_Control_R:
2296 case XK_Control_L:
2297 control_pressed = True;
2298 break;
2300 case XK_Tab:
2301 insertTextInteractively(tPtr, " ", 4);
2302 updateCursorPosition(tPtr);
2303 paintText(tPtr);
2304 break;
2306 case XK_Return:
2307 *buffer = '\n';
2308 /* FALLTHRU */
2309 default:
2310 if (*buffer != 0 && !control_pressed) {
2311 insertTextInteractively(tPtr, buffer, strlen(buffer));
2312 updateCursorPosition(tPtr);
2313 paintText(tPtr);
2315 } else if (control_pressed && ksym == XK_r) {
2316 Bool i = !tPtr->flags.rulerShown;
2317 WMShowTextRuler(tPtr, i);
2318 tPtr->flags.rulerShown = i;
2319 } else if (control_pressed && *buffer == '\a') {
2320 XBell(tPtr->view->screen->display, 0);
2321 } else {
2322 WMRelayToNextResponder(tPtr->view, event);
2326 if (!control_pressed && tPtr->flags.ownsSelection) {
2327 releaseSelection(tPtr);
2331 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2333 Text *tPtr = (Text *) view->self;
2334 char *text;
2336 /* Parameter not used, but tell the compiler that it is ok */
2337 (void) selection;
2338 (void) target;
2339 (void) timestamp;
2340 (void) cdata;
2342 tPtr->flags.waitingForSelection = 0;
2344 if (data) {
2345 text = (char *)WMDataBytes(data);
2347 if (tPtr->parser) {
2348 (tPtr->parser) (tPtr, (void *)text);
2349 layOutDocument(tPtr);
2350 } else
2351 insertTextInteractively(tPtr, text, strlen(text));
2352 updateCursorPosition(tPtr);
2353 paintText(tPtr);
2355 } else {
2356 int n;
2358 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2360 if (text) {
2361 text[n] = 0;
2362 if (tPtr->parser) {
2363 (tPtr->parser) (tPtr, (void *)text);
2364 layOutDocument(tPtr);
2365 } else
2366 insertTextInteractively(tPtr, text, n);
2367 updateCursorPosition(tPtr);
2368 paintText(tPtr);
2370 XFree(text);
2376 static void handleActionEvents(XEvent * event, void *data)
2378 Text *tPtr = (Text *) data;
2379 Display *dpy = event->xany.display;
2380 KeySym ksym;
2382 switch (event->type) {
2383 case KeyPress:
2384 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2385 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2386 tPtr->flags.extendSelection = True;
2387 return;
2390 if (tPtr->flags.focused) {
2391 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2392 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2393 GrabModeAsync, GrabModeAsync, None,
2394 tPtr->view->screen->invisibleCursor, CurrentTime);
2395 tPtr->flags.pointerGrabbed = True;
2396 handleTextKeyPress(tPtr, event);
2399 break;
2401 case KeyRelease:
2402 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2403 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2404 tPtr->flags.extendSelection = False;
2405 return;
2406 /* end modify flag so selection can be extended */
2408 break;
2410 case MotionNotify:
2412 if (tPtr->flags.pointerGrabbed) {
2413 tPtr->flags.pointerGrabbed = False;
2414 XUngrabPointer(dpy, CurrentTime);
2417 if (tPtr->flags.waitingForSelection)
2418 break;
2420 if ((event->xmotion.state & Button1Mask)) {
2422 if (WMIsDraggingFromView(tPtr->view)) {
2423 WMDragImageFromView(tPtr->view, event);
2424 break;
2427 if (!tPtr->flags.ownsSelection) {
2428 WMCreateSelectionHandler(tPtr->view,
2429 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2430 tPtr->flags.ownsSelection = True;
2432 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2433 break;
2436 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2437 break;
2439 case ButtonPress:
2441 if (tPtr->flags.pointerGrabbed) {
2442 tPtr->flags.pointerGrabbed = False;
2443 XUngrabPointer(dpy, CurrentTime);
2444 break;
2447 if (tPtr->flags.waitingForSelection)
2448 break;
2450 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2451 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2452 return;
2455 if (tPtr->flags.ownsSelection)
2456 releaseSelection(tPtr);
2458 if (event->xbutton.button == Button1) {
2459 TextBlock *tb = tPtr->currentTextBlock;
2461 if (WMIsDoubleClick(event)) {
2463 tPtr->lastClickTime = event->xbutton.time;
2464 if (tb && tb->graphic && !tb->object) {
2465 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2466 char *desc;
2468 desc = wmalloc(tb->used + 1);
2469 memcpy(desc, tb->text, tb->used);
2470 desc[tb->used] = 0;
2471 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2472 wfree(desc);
2474 } else {
2475 autoSelectText(tPtr, 2);
2477 break;
2478 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2479 tPtr->lastClickTime = event->xbutton.time;
2480 autoSelectText(tPtr, 3);
2481 break;
2484 if (!tPtr->flags.focused) {
2485 WMSetFocusToWidget(tPtr);
2486 tPtr->flags.focused = True;
2487 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2489 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2490 WMDragImageFromView(tPtr->view, event);
2491 break;
2494 tPtr->lastClickTime = event->xbutton.time;
2495 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2496 paintText(tPtr);
2499 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2500 WMScrollText(tPtr, 16);
2501 break;
2504 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2505 WMScrollText(tPtr, -16);
2506 break;
2509 if (event->xbutton.button == Button2) {
2510 char *text = NULL;
2511 int n;
2513 if (!tPtr->flags.editable) {
2514 XBell(dpy, 0);
2515 break;
2518 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2519 event->xbutton.time, pasteText, NULL)) {
2521 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2522 tPtr->flags.waitingForSelection = 0;
2524 if (text) {
2525 text[n] = 0;
2527 if (tPtr->parser) {
2528 (tPtr->parser) (tPtr, (void *)text);
2529 layOutDocument(tPtr);
2530 } else
2531 insertTextInteractively(tPtr, text, n);
2533 XFree(text);
2534 #if 0
2535 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2536 (void *)WMInsertTextEvent);
2537 #endif
2538 updateCursorPosition(tPtr);
2539 paintText(tPtr);
2541 } else {
2542 tPtr->flags.waitingForSelection = True;
2545 break;
2548 /* FALLTHRU */
2549 case ButtonRelease:
2550 if (tPtr->flags.pointerGrabbed) {
2551 tPtr->flags.pointerGrabbed = False;
2552 XUngrabPointer(dpy, CurrentTime);
2553 break;
2556 if (tPtr->flags.waitingForSelection)
2557 break;
2559 if (WMIsDraggingFromView(tPtr->view))
2560 WMDragImageFromView(tPtr->view, event);
2565 static void handleEvents(XEvent * event, void *data)
2567 Text *tPtr = (Text *) data;
2569 switch (event->type) {
2570 case Expose:
2572 if (event->xexpose.count != 0)
2573 break;
2575 if (tPtr->hS) {
2576 if (!(W_VIEW(tPtr->hS))->flags.realized)
2577 WMRealizeWidget(tPtr->hS);
2580 if (tPtr->vS) {
2581 if (!(W_VIEW(tPtr->vS))->flags.realized)
2582 WMRealizeWidget(tPtr->vS);
2585 if (tPtr->ruler) {
2586 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2587 WMRealizeWidget(tPtr->ruler);
2591 if (!tPtr->db)
2592 textDidResize(tPtr->view->delegate, tPtr->view);
2594 paintText(tPtr);
2595 break;
2597 case FocusIn:
2598 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2599 != tPtr->view)
2600 return;
2601 tPtr->flags.focused = True;
2602 #if DO_BLINK
2603 if (tPtr->flags.editable && !tPtr->timerID) {
2604 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2606 #endif
2608 break;
2610 case FocusOut:
2611 tPtr->flags.focused = False;
2612 paintText(tPtr);
2613 #if DO_BLINK
2614 if (tPtr->timerID) {
2615 WMDeleteTimerHandler(tPtr->timerID);
2616 tPtr->timerID = NULL;
2618 #endif
2619 break;
2621 case DestroyNotify:
2622 clearText(tPtr);
2623 if (tPtr->db)
2624 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2625 if (tPtr->gfxItems)
2626 WMEmptyArray(tPtr->gfxItems);
2627 #if DO_BLINK
2628 if (tPtr->timerID)
2629 WMDeleteTimerHandler(tPtr->timerID);
2630 #endif
2631 WMReleaseFont(tPtr->dFont);
2632 WMReleaseColor(tPtr->dColor);
2633 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2634 WMRemoveNotificationObserver(tPtr);
2636 WMFreeArray(tPtr->xdndSourceTypes);
2637 WMFreeArray(tPtr->xdndDestinationTypes);
2639 wfree(tPtr);
2641 break;
2646 static void insertPlainText(Text * tPtr, const char *text)
2648 const char *start, *mark;
2649 void *tb = NULL;
2651 start = text;
2652 while (start) {
2653 mark = strchr(start, '\n');
2654 if (mark) {
2655 tb = WMCreateTextBlockWithText(tPtr,
2656 start, tPtr->dFont,
2657 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2658 start = mark + 1;
2659 tPtr->flags.first = True;
2660 } else {
2661 if (start && strlen(start)) {
2662 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2663 tPtr->dColor, tPtr->flags.first, strlen(start));
2664 } else
2665 tb = NULL;
2666 tPtr->flags.first = False;
2667 start = mark;
2670 if (tPtr->flags.prepend)
2671 WMPrependTextBlock(tPtr, tb);
2672 else
2673 WMAppendTextBlock(tPtr, tb);
2677 static void rulerMoveCallBack(WMWidget * w, void *self)
2679 Text *tPtr = (Text *) self;
2681 /* Parameter not used, but tell the compiler that it is ok */
2682 (void) w;
2684 if (!tPtr)
2685 return;
2686 if (W_CLASS(tPtr) != WC_Text)
2687 return;
2689 paintText(tPtr);
2692 static void rulerReleaseCallBack(WMWidget * w, void *self)
2694 Text *tPtr = (Text *) self;
2696 /* Parameter not used, but tell the compiler that it is ok */
2697 (void) w;
2699 if (!tPtr)
2700 return;
2701 if (W_CLASS(tPtr) != WC_Text)
2702 return;
2704 WMThawText(tPtr);
2705 return;
2708 static WMArray *dropDataTypes(WMView * self)
2710 return ((Text *) self->self)->xdndSourceTypes;
2713 static WMDragOperationType wantedDropOperation(WMView * self)
2715 /* Parameter not used, but tell the compiler that it is ok */
2716 (void) self;
2718 return WDOperationCopy;
2721 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2723 /* Parameter not used, but tell the compiler that it is ok */
2724 (void) self;
2726 return (allowedOperation == WDOperationCopy);
2729 static WMData *fetchDragData(WMView * self, char *type)
2731 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2732 char *desc;
2733 WMData *data;
2735 if (strcmp(type, "text/plain")) {
2736 if (!tb)
2737 return NULL;
2739 desc = wmalloc(tb->used + 1);
2740 memcpy(desc, tb->text, tb->used);
2741 desc[tb->used] = 0;
2742 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2744 wfree(desc);
2746 return data;
2749 return NULL;
2752 static WMDragSourceProcs _DragSourceProcs = {
2753 dropDataTypes,
2754 wantedDropOperation,
2755 NULL,
2756 acceptDropOperation,
2757 NULL,
2758 NULL,
2759 fetchDragData
2762 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2764 /* Parameter not used, but tell the compiler that it is ok */
2765 (void) request;
2766 (void) sourceDataTypes;
2768 return ((Text *) self->self)->xdndDestinationTypes;
2771 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2773 /* Parameter not used, but tell the compiler that it is ok */
2774 (void) self;
2775 (void) request;
2776 (void) sourceDataTypes;
2778 return WDOperationCopy;
2781 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2783 WMText *tPtr = (WMText *) self->self;
2784 WMData *data;
2785 char *colorName;
2786 WMColor *color;
2788 /* Parameter not used, but tell the compiler that it is ok */
2789 (void) operations;
2790 (void) dropLocation;
2792 if (tPtr) {
2794 /* only one required type, implies only one drop data */
2796 /* get application/X-color if any */
2797 data = (WMData *) WMPopFromArray(dropData);
2798 if (data != NULL) {
2799 colorName = (char *)WMDataBytes(data);
2800 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2802 if (color) {
2803 WMSetTextSelectionColor(tPtr, color);
2804 WMReleaseColor(color);
2810 static WMDragDestinationProcs _DragDestinationProcs = {
2811 NULL,
2812 requiredDataTypes,
2813 allowedOperation,
2814 NULL,
2815 performDragOperation,
2816 NULL
2819 static char *getStream(WMText * tPtr, int sel, int array)
2821 TextBlock *tb = NULL;
2822 char *text = NULL;
2823 unsigned long where = 0;
2825 if (!tPtr)
2826 return NULL;
2828 if (!(tb = tPtr->firstTextBlock))
2829 return NULL;
2831 if (tPtr->writer) {
2832 (tPtr->writer) (tPtr, (void *)text);
2833 return text;
2836 tb = tPtr->firstTextBlock;
2837 while (tb) {
2839 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2841 if (!sel || (tb->graphic && tb->selected)) {
2843 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2844 && tb != tPtr->firstTextBlock) {
2845 text = wrealloc(text, where + 1);
2846 text[where++] = '\n';
2849 if (tb->blank)
2850 goto _gSnext;
2852 if (tb->graphic && array) {
2853 text = wrealloc(text, where + 4);
2854 text[where++] = 0xFA;
2855 text[where++] = (tb->used >> 8) & 0x0ff;
2856 text[where++] = tb->used & 0x0ff;
2857 text[where++] = tb->allocated; /* extra info */
2859 text = wrealloc(text, where + tb->used);
2860 memcpy(&text[where], tb->text, tb->used);
2861 where += tb->used;
2863 } else if (sel && tb->selected) {
2865 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2866 text = wrealloc(text, where + 1);
2867 text[where++] = '\n';
2870 if (tb->blank)
2871 goto _gSnext;
2873 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2874 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2875 where += tb->s_end - tb->s_begin;
2880 _gSnext: tb = tb->next;
2883 /* +1 for the end of string, let's be nice */
2884 text = wrealloc(text, where + 1);
2885 text[where] = 0;
2886 return text;
2889 static void releaseStreamObjects(void *data)
2891 if (data)
2892 wfree(data);
2895 static WMArray *getStreamObjects(WMText * tPtr, int sel)
2897 WMArray *array;
2898 WMData *data;
2899 char *stream;
2900 unsigned short len;
2901 char *start, *fa, *desc;
2903 stream = getStream(tPtr, sel, 1);
2904 if (!stream)
2905 return NULL;
2907 array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2909 start = stream;
2910 while (start) {
2912 fa = strchr(start, 0xFA);
2913 if (fa) {
2914 if ((int)(fa - start) > 0) {
2915 desc = start;
2916 desc[(int)(fa - start)] = 0;
2917 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2918 WMSetDataFormat(data, TYPETEXT);
2919 WMAddToArray(array, (void *)data);
2922 len = *(fa + 1) * 0xff + *(fa + 2);
2923 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2924 WMSetDataFormat(data, *(fa + 3));
2925 WMAddToArray(array, (void *)data);
2926 start = fa + len + 4;
2928 } else {
2929 if (start && strlen(start)) {
2930 data = WMCreateDataWithBytes((void *)start, strlen(start));
2931 WMSetDataFormat(data, TYPETEXT);
2932 WMAddToArray(array, (void *)data);
2934 start = fa;
2938 wfree(stream);
2939 return array;
2942 #define XDND_TEXT_DATA_TYPE "text/plain"
2943 #define XDND_COLOR_DATA_TYPE "application/X-color"
2944 static WMArray *getXdndSourceTypeArray(void)
2946 WMArray *types = WMCreateArray(1);
2947 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2948 return types;
2951 static WMArray *getXdndDestinationTypeArray(void)
2953 WMArray *types = WMCreateArray(1);
2954 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2955 return types;
2958 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2960 Text *tPtr;
2961 Display *dpy;
2962 WMScreen *scr;
2963 XGCValues gcv;
2965 tPtr = wmalloc(sizeof(Text));
2966 tPtr->widgetClass = WC_Text;
2967 tPtr->view = W_CreateView(W_VIEW(parent));
2968 if (!tPtr->view) {
2969 perror("could not create text's view\n");
2970 wfree(tPtr);
2971 return NULL;
2974 dpy = tPtr->view->screen->display;
2975 scr = tPtr->view->screen;
2977 if (XA_Targets == None) {
2979 * Because the X protocol guaranties that the value will never change in
2980 * the lifespan of the server, we query the values only the first time a
2981 * widget is created
2983 XA_Targets = XInternAtom(dpy, "TARGETS", False);
2984 XA_Format_Text = XInternAtom(dpy, "TEXT", False);
2985 XA_Format_Compound_Text = XInternAtom(dpy, "COMPOUND_TEXT", False);
2988 tPtr->view->self = tPtr;
2989 tPtr->view->attribs.cursor = scr->textCursor;
2990 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2991 W_ResizeView(tPtr->view, 250, 200);
2993 tPtr->dColor = WMBlackColor(scr);
2994 tPtr->fgColor = WMBlackColor(scr);
2995 tPtr->bgColor = WMWhiteColor(scr);
2996 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2998 gcv.graphics_exposures = False;
2999 gcv.foreground = W_PIXEL(scr->gray);
3000 gcv.background = W_PIXEL(scr->darkGray);
3001 gcv.fill_style = FillStippled;
3002 /* why not use scr->stipple here? */
3003 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
3004 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3005 GCForeground | GCBackground | GCStipple
3006 | GCFillStyle | GCGraphicsExposures, &gcv);
3008 tPtr->ruler = NULL;
3009 tPtr->vS = NULL;
3010 tPtr->hS = NULL;
3012 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3014 tPtr->view->delegate = &_TextViewDelegate;
3016 tPtr->delegate = NULL;
3018 #if DO_BLINK
3019 tPtr->timerID = NULL;
3020 #endif
3022 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
3023 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
3025 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
3026 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
3028 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
3030 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3031 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3034 WMArray *types = WMCreateArray(2);
3035 WMAddToArray(types, "application/X-color");
3036 WMAddToArray(types, "application/X-image");
3037 WMRegisterViewForDraggedTypes(tPtr->view, types);
3038 WMFreeArray(types);
3041 /*WMAddNotificationObserver(fontChanged, tPtr,
3042 WMFontPanelDidChangeNotification, tPtr); */
3044 tPtr->firstTextBlock = NULL;
3045 tPtr->lastTextBlock = NULL;
3046 tPtr->currentTextBlock = NULL;
3047 tPtr->tpos = 0;
3049 tPtr->gfxItems = WMCreateArray(4);
3051 tPtr->parser = parser;
3052 tPtr->writer = writer;
3054 tPtr->sel.x = tPtr->sel.y = 2;
3055 tPtr->sel.w = tPtr->sel.h = 0;
3057 tPtr->clicked.x = tPtr->clicked.y = 2;
3059 tPtr->visible.x = tPtr->visible.y = 2;
3060 tPtr->visible.h = tPtr->view->size.height;
3061 tPtr->visible.w = tPtr->view->size.width - 4;
3063 tPtr->cursor.x = -23;
3065 tPtr->docWidth = 0;
3066 tPtr->docHeight = 0;
3067 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3068 tPtr->db = (Pixmap) NULL;
3069 tPtr->bgPixmap = NULL;
3071 tPtr->margins = WMGetRulerMargins(NULL);
3072 tPtr->margins->right = tPtr->visible.w;
3073 tPtr->nMargins = 1;
3075 tPtr->flags.rulerShown = False;
3076 tPtr->flags.monoFont = False;
3077 tPtr->flags.focused = False;
3078 tPtr->flags.editable = True;
3079 tPtr->flags.ownsSelection = False;
3080 tPtr->flags.pointerGrabbed = False;
3081 tPtr->flags.extendSelection = False;
3082 tPtr->flags.frozen = False;
3083 tPtr->flags.cursorShown = True;
3084 tPtr->flags.acceptsGraphic = False;
3085 tPtr->flags.horizOnDemand = False;
3086 tPtr->flags.needsLayOut = False;
3087 tPtr->flags.ignoreNewLine = False;
3088 tPtr->flags.indentNewLine = False;
3089 tPtr->flags.laidOut = False;
3090 tPtr->flags.ownsSelection = False;
3091 tPtr->flags.waitingForSelection = False;
3092 tPtr->flags.prepend = False;
3093 tPtr->flags.isOverGraphic = False;
3094 tPtr->flags.relief = WRSunken;
3095 tPtr->flags.isOverGraphic = 0;
3096 tPtr->flags.alignment = WALeft;
3097 tPtr->flags.first = True;
3099 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3100 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3102 return tPtr;
3105 void WMPrependTextStream(WMText * tPtr, const char *text)
3107 CHECK_CLASS(tPtr, WC_Text);
3109 if (!text) {
3110 if (tPtr->flags.ownsSelection)
3111 releaseSelection(tPtr);
3112 clearText(tPtr);
3113 updateScrollers(tPtr);
3114 return;
3117 tPtr->flags.prepend = True;
3118 if (text && tPtr->parser)
3119 (tPtr->parser) (tPtr, (void *)text);
3120 else
3121 insertPlainText(tPtr, text);
3123 tPtr->flags.needsLayOut = True;
3124 tPtr->tpos = 0;
3125 if (!tPtr->flags.frozen) {
3126 layOutDocument(tPtr);
3130 void WMAppendTextStream(WMText * tPtr, const char *text)
3132 CHECK_CLASS(tPtr, WC_Text);
3134 if (!text) {
3135 if (tPtr->flags.ownsSelection)
3136 releaseSelection(tPtr);
3137 clearText(tPtr);
3138 updateScrollers(tPtr);
3139 return;
3142 tPtr->flags.prepend = False;
3143 if (text && tPtr->parser)
3144 (tPtr->parser) (tPtr, (void *)text);
3145 else
3146 insertPlainText(tPtr, text);
3148 tPtr->flags.needsLayOut = True;
3149 if (tPtr->currentTextBlock) {
3150 if (tPtr->currentTextBlock->graphic)
3151 tPtr->tpos = 1;
3152 else
3153 tPtr->tpos = tPtr->currentTextBlock->used;
3156 if (!tPtr->flags.frozen) {
3157 layOutDocument(tPtr);
3161 char *WMGetTextStream(WMText * tPtr)
3163 CHECK_CLASS(tPtr, WC_Text);
3165 return getStream(tPtr, 0, 0);
3168 char *WMGetTextSelectedStream(WMText * tPtr)
3170 CHECK_CLASS(tPtr, WC_Text);
3172 return getStream(tPtr, 1, 0);
3175 WMArray *WMGetTextObjects(WMText * tPtr)
3177 CHECK_CLASS(tPtr, WC_Text);
3179 return getStreamObjects(tPtr, 0);
3182 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3184 CHECK_CLASS(tPtr, WC_Text);
3186 return getStreamObjects(tPtr, 1);
3189 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3191 CHECK_CLASS(tPtr, WC_Text);
3193 tPtr->delegate = delegate;
3196 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3197 const char *description, WMColor * color,
3198 unsigned short first, unsigned short extraInfo)
3200 TextBlock *tb;
3202 if (!w || !description || !color)
3203 return NULL;
3205 tb = wmalloc(sizeof(TextBlock));
3207 tb->text = wstrdup(description);
3208 tb->used = strlen(description);
3209 tb->blank = False;
3210 tb->d.widget = w;
3211 tb->color = WMRetainColor(color);
3212 tb->marginN = newMargin(tPtr, NULL);
3213 tb->allocated = extraInfo;
3214 tb->first = first;
3215 tb->kanji = False;
3216 tb->graphic = True;
3217 tb->object = True;
3218 tb->underlined = False;
3219 tb->selected = False;
3220 tb->script = 0;
3221 tb->sections = NULL;
3222 tb->nsections = 0;
3223 tb->prior = NULL;
3224 tb->next = NULL;
3226 return tb;
3229 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3230 const char *description, WMColor * color,
3231 unsigned short first, unsigned short extraInfo)
3233 TextBlock *tb;
3235 if (!p || !description || !color)
3236 return NULL;
3238 tb = wmalloc(sizeof(TextBlock));
3240 tb->text = wstrdup(description);
3241 tb->used = strlen(description);
3242 tb->blank = False;
3243 tb->d.pixmap = WMRetainPixmap(p);
3244 tb->color = WMRetainColor(color);
3245 tb->marginN = newMargin(tPtr, NULL);
3246 tb->allocated = extraInfo;
3247 tb->first = first;
3248 tb->kanji = False;
3249 tb->graphic = True;
3250 tb->object = False;
3251 tb->underlined = False;
3252 tb->selected = False;
3253 tb->script = 0;
3254 tb->sections = NULL;
3255 tb->nsections = 0;
3256 tb->prior = NULL;
3257 tb->next = NULL;
3259 return tb;
3262 void *WMCreateTextBlockWithText(WMText * tPtr, const char *text, WMFont * font, WMColor * color,
3263 unsigned short first, unsigned short len)
3265 TextBlock *tb;
3267 if (!font || !color)
3268 return NULL;
3270 tb = wmalloc(sizeof(TextBlock));
3272 tb->allocated = reqBlockSize(len);
3273 tb->text = (char *)wmalloc(tb->allocated);
3275 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3276 *tb->text = ' ';
3277 tb->used = 1;
3278 tb->blank = True;
3279 } else {
3280 memcpy(tb->text, text, len);
3281 tb->used = len;
3282 tb->blank = False;
3284 tb->text[tb->used] = 0;
3286 tb->d.font = WMRetainFont(font);
3287 tb->color = WMRetainColor(color);
3288 tb->marginN = newMargin(tPtr, NULL);
3289 tb->first = first;
3290 tb->kanji = False;
3291 tb->graphic = False;
3292 tb->underlined = False;
3293 tb->selected = False;
3294 tb->script = 0;
3295 tb->sections = NULL;
3296 tb->nsections = 0;
3297 tb->prior = NULL;
3298 tb->next = NULL;
3299 return tb;
3302 void
3303 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3304 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3306 TextBlock *tb = (TextBlock *) vtb;
3307 if (!tb)
3308 return;
3310 tb->first = first;
3311 tb->kanji = kanji;
3312 tb->underlined = underlined;
3313 tb->script = script;
3314 tb->marginN = newMargin(tPtr, margins);
3317 void
3318 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3319 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins *margins)
3321 TextBlock *tb = (TextBlock *) vtb;
3322 if (!tb)
3323 return;
3325 if (first)
3326 *first = tb->first;
3327 if (kanji)
3328 *kanji = tb->kanji;
3329 if (underlined)
3330 *underlined = tb->underlined;
3331 if (script)
3332 *script = tb->script;
3333 if (margins)
3334 *margins = tPtr->margins[tb->marginN];
3337 static int prepareTextBlock(WMText *tPtr, TextBlock *tb)
3339 if (tb->graphic) {
3340 if (tb->object) {
3341 WMWidget *w = tb->d.widget;
3342 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3343 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3344 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3347 WMAddToArray(tPtr->gfxItems, (void *)tb);
3348 tPtr->tpos = 1;
3350 } else {
3351 tPtr->tpos = tb->used;
3354 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3355 tb->next = tb->prior = NULL;
3356 tb->first = True;
3357 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3358 return 0;
3361 if (!tb->first) {
3362 tb->marginN = tPtr->currentTextBlock->marginN;
3365 return 1;
3369 void WMPrependTextBlock(WMText *tPtr, void *vtb)
3371 TextBlock *tb = (TextBlock *) vtb;
3373 if (!tb || !prepareTextBlock(tPtr, tb))
3374 return;
3376 tb->next = tPtr->currentTextBlock;
3377 tb->prior = tPtr->currentTextBlock->prior;
3378 if (tPtr->currentTextBlock->prior)
3379 tPtr->currentTextBlock->prior->next = tb;
3381 tPtr->currentTextBlock->prior = tb;
3382 if (!tb->prior)
3383 tPtr->firstTextBlock = tb;
3385 tPtr->currentTextBlock = tb;
3388 void WMAppendTextBlock(WMText *tPtr, void *vtb)
3390 TextBlock *tb = (TextBlock *) vtb;
3392 if (!tb || !prepareTextBlock(tPtr, tb))
3393 return;
3395 tb->next = tPtr->currentTextBlock->next;
3396 tb->prior = tPtr->currentTextBlock;
3397 if (tPtr->currentTextBlock->next)
3398 tPtr->currentTextBlock->next->prior = tb;
3400 tPtr->currentTextBlock->next = tb;
3402 if (!tb->next)
3403 tPtr->lastTextBlock = tb;
3405 tPtr->currentTextBlock = tb;
3408 void *WMRemoveTextBlock(WMText * tPtr)
3410 TextBlock *tb = NULL;
3412 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3413 return NULL;
3416 tb = tPtr->currentTextBlock;
3417 if (tb->graphic) {
3418 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3420 if (tb->object) {
3421 WMUnmapWidget(tb->d.widget);
3425 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3426 if (tPtr->currentTextBlock->next)
3427 tPtr->currentTextBlock->next->prior = NULL;
3429 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3430 tPtr->currentTextBlock = tPtr->firstTextBlock;
3432 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3433 tPtr->currentTextBlock->prior->next = NULL;
3434 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3435 tPtr->currentTextBlock = tPtr->lastTextBlock;
3436 } else {
3437 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3438 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3439 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3442 return (void *)tb;
3445 #if 0
3446 static void destroyWidget(WMWidget * widget)
3448 WMDestroyWidget(widget);
3449 // -- never do this -- wfree(widget);
3451 #endif
3453 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3455 TextBlock *tb = (TextBlock *) vtb;
3457 /* Parameter not used, but tell the compiler that it is ok */
3458 (void) tPtr;
3460 if (!tb)
3461 return;
3463 if (tb->graphic) {
3464 if (tb->object) {
3465 /* naturally, there's a danger to destroying widgets whose action
3466 * brings us here: ie. press a button to destroy it...
3467 * need to find a safer way. till then... this stays commented out */
3468 /* 5 months later... destroy it 10 seconds after now which should
3469 * be enough time for the widget's action to be completed... :-) */
3470 /* This is a bad assumption. Just destroy the widget here.
3471 * if the caller needs it, it can protect it with W_RetainView()
3472 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3473 WMDestroyWidget(tb->d.widget);
3474 } else {
3475 WMReleasePixmap(tb->d.pixmap);
3477 } else {
3478 WMReleaseFont(tb->d.font);
3481 WMReleaseColor(tb->color);
3482 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3483 if (tb->sections)
3484 wfree(tb->sections);
3485 wfree(tb->text);
3486 wfree(tb);
3489 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3491 if (tPtr->fgColor)
3492 WMReleaseColor(tPtr->fgColor);
3494 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3496 paintText(tPtr);
3499 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3501 if (tPtr->bgColor)
3502 WMReleaseColor(tPtr->bgColor);
3504 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3505 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3507 paintText(tPtr);
3510 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3512 if (tPtr->bgPixmap)
3513 WMReleasePixmap(tPtr->bgPixmap);
3515 if (pixmap)
3516 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3517 else
3518 tPtr->bgPixmap = NULL;
3521 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3523 tPtr->flags.relief = relief;
3524 textDidResize(tPtr->view->delegate, tPtr->view);
3527 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3529 if (shouldhave && !tPtr->hS) {
3530 tPtr->hS = WMCreateScroller(tPtr);
3531 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3532 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3533 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3534 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3535 WMMapWidget(tPtr->hS);
3536 } else if (!shouldhave && tPtr->hS) {
3537 WMUnmapWidget(tPtr->hS);
3538 WMDestroyWidget(tPtr->hS);
3539 tPtr->hS = NULL;
3542 tPtr->hpos = 0;
3543 tPtr->prevHpos = 0;
3544 textDidResize(tPtr->view->delegate, tPtr->view);
3547 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3549 if (shouldhave && !tPtr->ruler) {
3550 tPtr->ruler = WMCreateRuler(tPtr);
3551 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3552 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3553 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3554 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3555 } else if (!shouldhave && tPtr->ruler) {
3556 WMShowTextRuler(tPtr, False);
3557 WMDestroyWidget(tPtr->ruler);
3558 tPtr->ruler = NULL;
3560 textDidResize(tPtr->view->delegate, tPtr->view);
3563 void WMShowTextRuler(WMText * tPtr, Bool show)
3565 if (!tPtr->ruler)
3566 return;
3568 if (tPtr->flags.monoFont)
3569 show = False;
3571 tPtr->flags.rulerShown = show;
3572 if (show) {
3573 WMMapWidget(tPtr->ruler);
3574 } else {
3575 WMUnmapWidget(tPtr->ruler);
3578 textDidResize(tPtr->view->delegate, tPtr->view);
3581 Bool WMGetTextRulerShown(WMText * tPtr)
3583 if (!tPtr->ruler)
3584 return False;
3586 return tPtr->flags.rulerShown;
3589 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3591 if (shouldhave && !tPtr->vS) {
3592 tPtr->vS = WMCreateScroller(tPtr);
3593 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3594 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3595 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3596 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3597 WMMapWidget(tPtr->vS);
3598 } else if (!shouldhave && tPtr->vS) {
3599 WMUnmapWidget(tPtr->vS);
3600 WMDestroyWidget(tPtr->vS);
3601 tPtr->vS = NULL;
3604 tPtr->vpos = 0;
3605 tPtr->prevVpos = 0;
3606 textDidResize(tPtr->view->delegate, tPtr->view);
3609 Bool WMScrollText(WMText * tPtr, int amount)
3611 Bool scroll = False;
3613 if (amount == 0 || !tPtr->view->flags.realized)
3614 return False;
3616 if (amount < 0) {
3617 if (tPtr->vpos > 0) {
3618 if (tPtr->vpos > abs(amount))
3619 tPtr->vpos += amount;
3620 else
3621 tPtr->vpos = 0;
3622 scroll = True;
3624 } else {
3625 int limit = tPtr->docHeight - tPtr->visible.h;
3626 if (tPtr->vpos < limit) {
3627 if (tPtr->vpos < limit - amount)
3628 tPtr->vpos += amount;
3629 else
3630 tPtr->vpos = limit;
3631 scroll = True;
3635 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3636 updateScrollers(tPtr);
3637 paintText(tPtr);
3639 tPtr->prevVpos = tPtr->vpos;
3640 return scroll;
3643 Bool WMPageText(WMText * tPtr, Bool direction)
3645 if (!tPtr->view->flags.realized)
3646 return False;
3648 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3651 void WMSetTextEditable(WMText * tPtr, Bool editable)
3653 tPtr->flags.editable = editable;
3656 int WMGetTextEditable(WMText * tPtr)
3658 return tPtr->flags.editable;
3661 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3663 tPtr->flags.indentNewLine = indent;
3666 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3668 tPtr->flags.ignoreNewLine = ignore;
3671 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3673 return tPtr->flags.ignoreNewLine;
3676 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3678 if (mono) {
3679 if (tPtr->flags.rulerShown)
3680 WMShowTextRuler(tPtr, False);
3681 if (tPtr->flags.alignment != WALeft)
3682 tPtr->flags.alignment = WALeft;
3685 tPtr->flags.monoFont = mono;
3686 textDidResize(tPtr->view->delegate, tPtr->view);
3689 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3691 return tPtr->flags.monoFont;
3694 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3696 if (tPtr->dFont)
3697 WMReleaseFont(tPtr->dFont);
3699 if (font) {
3700 tPtr->dFont = WMRetainFont(font);
3701 } else {
3702 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3706 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3708 return WMRetainFont(tPtr->dFont);
3711 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3713 if (tPtr->dColor)
3714 WMReleaseColor(tPtr->dColor);
3716 if (color) {
3717 tPtr->dColor = WMRetainColor(color);
3718 } else {
3719 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3723 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3725 return tPtr->dColor;
3728 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3730 if (tPtr->flags.monoFont)
3731 tPtr->flags.alignment = WALeft;
3732 else
3733 tPtr->flags.alignment = alignment;
3734 WMThawText(tPtr);
3737 int WMGetTextInsertType(WMText * tPtr)
3739 return tPtr->flags.prepend;
3742 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3744 setSelectionProperty(tPtr, NULL, color, -1);
3747 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3749 TextBlock *tb;
3751 tb = tPtr->currentTextBlock;
3753 if (!tb || !tPtr->flags.ownsSelection)
3754 return NULL;
3756 if (!tb->selected)
3757 return NULL;
3759 return tb->color;
3762 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3764 setSelectionProperty(tPtr, font, NULL, -1);
3767 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3769 TextBlock *tb;
3771 tb = tPtr->currentTextBlock;
3773 if (!tb || !tPtr->flags.ownsSelection)
3774 return NULL;
3776 if (!tb->selected)
3777 return NULL;
3779 if (tb->graphic) {
3780 tb = getFirstNonGraphicBlockFor(tb, 1);
3781 if (!tb)
3782 return NULL;
3784 return (tb->selected ? tb->d.font : NULL);
3787 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3789 /* // check this */
3790 if (underlined != 0 && underlined != 1)
3791 return;
3793 setSelectionProperty(tPtr, NULL, NULL, underlined);
3796 int WMGetTextSelectionUnderlined(WMText * tPtr)
3798 TextBlock *tb;
3800 tb = tPtr->currentTextBlock;
3802 if (!tb || !tPtr->flags.ownsSelection)
3803 return 0;
3805 if (!tb->selected)
3806 return 0;
3808 return tb->underlined;
3811 void WMFreezeText(WMText * tPtr)
3813 tPtr->flags.frozen = True;
3816 void WMThawText(WMText * tPtr)
3818 tPtr->flags.frozen = False;
3820 if (tPtr->flags.monoFont) {
3821 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3822 TextBlock *tb;
3824 /* make sure to unmap widgets no matter where they are */
3825 /* they'll be later remapped if needed by paintText */
3826 for (j = 0; j < c; j++) {
3827 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3828 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3829 WMUnmapWidget(tb->d.widget);
3834 tPtr->flags.laidOut = False;
3835 layOutDocument(tPtr);
3836 updateScrollers(tPtr);
3837 paintText(tPtr);
3838 tPtr->flags.needsLayOut = False;
3842 /* find first occurence of a string */
3843 static const char *mystrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3845 const char *ptr;
3847 if (!haystack || !needle || !end)
3848 return NULL;
3850 for (ptr = haystack; ptr < end; ptr++) {
3851 if (caseSensitive) {
3852 if (*ptr == *needle && !strncmp(ptr, needle, len))
3853 return ptr;
3855 } else {
3856 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3857 return ptr;
3861 return NULL;
3864 /* find last occurence of a string */
3865 static const char *mystrrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3867 const char *ptr;
3869 if (!haystack || !needle || !end)
3870 return NULL;
3872 for (ptr = haystack - 2; ptr > end; ptr--) {
3873 if (caseSensitive) {
3874 if (*ptr == *needle && !strncmp(ptr, needle, len))
3875 return ptr;
3876 } else {
3877 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3878 return ptr;
3882 return NULL;
3885 Bool WMFindInTextStream(WMText * tPtr, const char *needle, Bool direction, Bool caseSensitive)
3887 TextBlock *tb;
3888 const char *mark = NULL;
3889 unsigned short pos;
3891 #if 0
3892 if (!(tb = tPtr->currentTextBlock)) {
3893 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3894 return False;
3896 } else {
3897 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3898 tb = (direction>0) ? tb->next : tb->prior; */
3899 if (tb != tPtr->lastTextBlock)
3900 tb = tb->prior;
3902 #endif
3903 tb = tPtr->currentTextBlock;
3904 pos = tPtr->tpos;
3906 while (tb) {
3907 if (!tb->graphic) {
3909 if (direction > 0) {
3910 if (pos + 1 < tb->used)
3911 pos++;
3913 if (tb->used - pos > 0 && pos > 0) {
3914 mark = mystrstr(&tb->text[pos], needle,
3915 strlen(needle), &tb->text[tb->used], caseSensitive);
3917 } else {
3918 tb = tb->next;
3919 pos = 0;
3920 continue;
3923 } else {
3924 if (pos - 1 > 0)
3925 pos--;
3927 if (pos > 0) {
3928 mark = mystrrstr(&tb->text[pos], needle,
3929 strlen(needle), tb->text, caseSensitive);
3930 } else {
3931 tb = tb->prior;
3932 if (!tb)
3933 return False;
3934 pos = tb->used;
3935 continue;
3939 if (mark) {
3940 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3942 tPtr->tpos = (int)(mark - tb->text);
3943 tPtr->currentTextBlock = tb;
3944 updateCursorPosition(tPtr);
3945 tPtr->sel.y = tPtr->cursor.y + 5;
3946 tPtr->sel.h = tPtr->cursor.h - 10;
3947 tPtr->sel.x = tPtr->cursor.x + 1;
3948 tPtr->sel.w = WMIN(WMWidthOfString(font,
3949 &tb->text[tPtr->tpos], strlen(needle)),
3950 tPtr->docWidth - tPtr->sel.x);
3951 tPtr->flags.ownsSelection = True;
3952 paintText(tPtr);
3954 return True;
3958 tb = (direction > 0) ? tb->next : tb->prior;
3959 if (tb) {
3960 pos = (direction > 0) ? 0 : tb->used;
3964 return False;
3967 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3969 if (!tPtr->flags.ownsSelection)
3970 return False;
3972 removeSelection(tPtr);
3974 if (replacement) {
3975 insertTextInteractively(tPtr, replacement, strlen(replacement));
3976 updateCursorPosition(tPtr);
3977 paintText(tPtr);
3980 return True;