WINGs: Added 'const' attribute to function 'WMCreateHashTable'
[wmaker-crm.git] / WINGs / wtext.c
blob80295bcb03f1b1c9be03a246af864599ab1f75e5
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 static void handleEvents(XEvent * event, void *data);
219 static void layOutDocument(Text * tPtr);
220 static void updateScrollers(Text * tPtr);
222 static int getMarginNumber(Text * tPtr, WMRulerMargins * margins)
224 unsigned int i = 0;
226 for (i = 0; i < tPtr->nMargins; i++) {
228 if (WMIsMarginEqualToMargin(&tPtr->margins[i], margins))
229 return i;
232 return -1;
235 static int newMargin(Text * tPtr, WMRulerMargins * margins)
237 int n;
239 if (!margins) {
240 tPtr->margins[0].retainCount++;
241 return 0;
244 n = getMarginNumber(tPtr, margins);
246 if (n == -1) {
248 if (tPtr->nMargins >= 127) {
249 n = tPtr->nMargins - 1;
250 return n;
253 tPtr->margins = wrealloc(tPtr->margins, (++tPtr->nMargins) * sizeof(WMRulerMargins));
255 n = tPtr->nMargins - 1;
256 tPtr->margins[n].left = margins->left;
257 tPtr->margins[n].first = margins->first;
258 tPtr->margins[n].body = margins->body;
259 tPtr->margins[n].right = margins->right;
260 /* for each tab... */
261 tPtr->margins[n].retainCount = 1;
262 } else {
263 tPtr->margins[n].retainCount++;
266 return n;
269 static Bool sectionWasSelected(Text * tPtr, TextBlock * tb, XRectangle * rect, int s)
271 unsigned short i, w, lw, selected = False, extend = False;
272 myRect sel;
274 /* if selection rectangle completely encloses the section */
275 if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
276 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
277 sel.x = 0;
278 sel.w = tPtr->visible.w;
279 selected = extend = True;
281 /* or if it starts on a line and then goes further down */
282 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
283 && (tb->sections[s]._y + tb->sections[s].h <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
284 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y)) {
285 sel.x = WMAX(tPtr->sel.x, tPtr->clicked.x);
286 sel.w = tPtr->visible.w;
287 selected = extend = True;
289 /* or if it begins before a line, but ends on it */
290 } else if ((tb->sections[s]._y >= tPtr->visible.y + tPtr->sel.y)
291 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)
292 && (tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
294 if (1 || tPtr->sel.x + tPtr->sel.w > tPtr->clicked.x)
295 sel.w = tPtr->sel.x + tPtr->sel.w;
296 else
297 sel.w = tPtr->sel.x;
299 sel.x = 0;
300 selected = True;
302 /* or if the selection rectangle lies entirely within a line */
303 } else if ((tb->sections[s]._y <= tPtr->visible.y + tPtr->sel.y)
304 && (tPtr->sel.w >= 2)
305 && (tb->sections[s]._y + tb->sections[s].h >= tPtr->visible.y + tPtr->sel.y + tPtr->sel.h)) {
306 sel.x = tPtr->sel.x;
307 sel.w = tPtr->sel.w;
308 selected = True;
311 if (selected) {
312 selected = False;
314 /* if not within (modified) selection rectangle */
315 if (tb->sections[s].x > sel.x + sel.w || tb->sections[s].x + tb->sections[s].w < sel.x)
316 return False;
318 if (tb->graphic) {
319 if (tb->sections[s].x + tb->sections[s].w <= sel.x + sel.w && tb->sections[s].x >= sel.x) {
320 rect->width = tb->sections[s].w;
321 rect->x = tb->sections[s].x;
322 selected = True;
324 } else {
326 i = tb->sections[s].begin;
327 lw = 0;
329 if (0 && tb->sections[s].x >= sel.x) {
330 tb->s_begin = tb->sections[s].begin;
331 goto _selEnd;
334 while (++i <= tb->sections[s].end) {
336 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
337 lw += w;
339 if (lw + tb->sections[s].x >= sel.x || i == tb->sections[s].end) {
340 lw -= w;
341 i--;
342 tb->s_begin = (tb->selected ? WMIN(tb->s_begin, i) : i);
343 break;
347 if (i > tb->sections[s].end) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
349 return False;
352 _selEnd: rect->x = tb->sections[s].x + lw;
353 lw = 0;
354 while (++i <= tb->sections[s].end) {
356 w = WMWidthOfString(tb->d.font, &(tb->text[i - 1]), 1);
357 lw += w;
359 if (lw + rect->x >= sel.x + sel.w || i == tb->sections[s].end) {
361 if (i != tb->sections[s].end) {
362 lw -= w;
363 i--;
366 rect->width = lw;
367 if (tb->sections[s].last && sel.x + sel.w
368 >= tb->sections[s].x + tb->sections[s].w && extend) {
369 rect->width += (tPtr->visible.w - rect->x - lw);
372 tb->s_end = (tb->selected ? WMAX(tb->s_end, i) : i);
373 selected = True;
374 break;
380 if (selected) {
381 rect->y = tb->sections[s]._y - tPtr->vpos;
382 rect->height = tb->sections[s].h;
383 if (tb->graphic) {
384 printf("DEBUG: graphic s%d h%d\n", s, tb->sections[s].h);
387 return selected;
391 static void setSelectionProperty(WMText * tPtr, WMFont * font, WMColor * color, int underlined)
393 TextBlock *tb;
394 int isFont = False;
396 tb = tPtr->firstTextBlock;
397 if (!tb || !tPtr->flags.ownsSelection)
398 return;
400 if (font && (!color || underlined == -1))
401 isFont = True;
403 while (tb) {
404 if (tPtr->flags.monoFont || tb->selected) {
406 if (tPtr->flags.monoFont || (tb->s_end - tb->s_begin == tb->used)
407 || tb->graphic) {
409 if (isFont) {
410 if (!tb->graphic) {
411 WMReleaseFont(tb->d.font);
412 tb->d.font = WMRetainFont(font);
414 } else if (underlined != -1) {
415 tb->underlined = underlined;
416 } else {
417 WMReleaseColor(tb->color);
418 tb->color = WMRetainColor(color);
421 } else if (tb->s_end <= tb->used && tb->s_begin < tb->s_end) {
423 TextBlock *midtb, *otb = tb;
425 if (underlined != -1) {
426 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
427 &(tb->text[tb->s_begin]),
428 tb->d.font, tb->color,
429 False,
430 (tb->s_end - tb->s_begin));
431 } else {
432 midtb = (TextBlock *) WMCreateTextBlockWithText(tPtr,
433 &(tb->text[tb->s_begin]),
434 (isFont ? font : tb->d.
435 font),
436 (isFont ? tb->
437 color : color), False,
438 (tb->s_end - tb->s_begin));
441 if (midtb) {
442 if (underlined != -1) {
443 midtb->underlined = underlined;
444 } else {
445 midtb->underlined = otb->underlined;
448 midtb->selected = !True;
449 midtb->s_begin = 0;
450 midtb->s_end = midtb->used;
451 tPtr->currentTextBlock = tb;
452 WMAppendTextBlock(tPtr, midtb);
453 tb = tPtr->currentTextBlock;
456 if (otb->used - otb->s_end > 0) {
457 TextBlock *ntb;
458 ntb = (TextBlock *)
459 WMCreateTextBlockWithText(tPtr,
460 &(otb->text[otb->s_end]), otb->d.font,
461 otb->color, False, otb->used - otb->s_end);
463 if (ntb) {
464 ntb->underlined = otb->underlined;
465 ntb->selected = False;
466 WMAppendTextBlock(tPtr, ntb);
467 tb = tPtr->currentTextBlock;
471 if (midtb) {
472 tPtr->currentTextBlock = midtb;
475 otb->selected = False;
476 otb->used = otb->s_begin;
480 tb = tb->next;
483 tPtr->flags.needsLayOut = True;
484 WMThawText(tPtr);
486 /* in case the size changed... */
487 if (isFont && tPtr->currentTextBlock) {
488 TextBlock *tb = tPtr->currentTextBlock;
490 printf("%d %d %d\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
491 tPtr->sel.y = 3 + tb->sections[0]._y;
492 tPtr->sel.h = tb->sections[tb->nsections - 1]._y - tb->sections[0]._y;
493 tPtr->sel.w = tb->sections[tb->nsections - 1].w;
494 if (tb->sections[tb->nsections - 1]._y != tb->sections[0]._y) {
495 tPtr->sel.x = 0;
497 printf("%d %d %d\n\n\n", tPtr->sel.y, tPtr->sel.h, tPtr->sel.w);
502 static Bool removeSelection(Text * tPtr)
504 TextBlock *tb = NULL;
505 Bool first = False;
507 if (!(tb = tPtr->firstTextBlock))
508 return False;
510 while (tb) {
511 if (tb->selected) {
512 if (!first && !tb->graphic) {
513 WMReleaseFont(tPtr->dFont);
514 tPtr->dFont = WMRetainFont(tb->d.font);
515 first = True;
518 if ((tb->s_end - tb->s_begin == tb->used) || tb->graphic) {
519 tPtr->currentTextBlock = tb;
520 if (tb->next) {
521 tPtr->tpos = 0;
522 } else if (tb->prior) {
523 if (tb->prior->graphic)
524 tPtr->tpos = 1;
525 else
526 tPtr->tpos = tb->prior->used;
527 } else
528 tPtr->tpos = 0;
530 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
531 tb = tPtr->currentTextBlock;
532 continue;
534 } else if (tb->s_end <= tb->used) {
535 memmove(&(tb->text[tb->s_begin]), &(tb->text[tb->s_end]), tb->used - tb->s_end);
536 tb->used -= (tb->s_end - tb->s_begin);
537 tb->selected = False;
538 tPtr->tpos = tb->s_begin;
543 tb = tb->next;
545 return True;
548 static TextBlock *getFirstNonGraphicBlockFor(TextBlock * tb, short dir)
550 TextBlock *hold = tb;
552 if (!tb)
553 return NULL;
555 while (tb) {
556 if (!tb->graphic)
557 break;
558 tb = (dir ? tb->next : tb->prior);
561 if (!tb) {
562 tb = hold;
563 while (tb) {
564 if (!tb->graphic)
565 break;
566 tb = (dir ? tb->prior : tb->next);
570 if (!tb)
571 return NULL;
573 return tb;
576 static Bool updateStartForCurrentTextBlock(Text * tPtr, int x, int y, int *dir, TextBlock * tb)
578 if (tPtr->flags.monoFont && tb->graphic) {
579 tb = getFirstNonGraphicBlockFor(tb, *dir);
580 if (!tb)
581 return 0;
583 if (tb->graphic) {
584 tPtr->currentTextBlock = (dir ? tPtr->lastTextBlock : tPtr->firstTextBlock);
585 tPtr->tpos = 0;
586 return 0;
590 if (!tb->sections) {
591 layOutDocument(tPtr);
592 return 0;
595 *dir = !(y <= tb->sections[0].y);
596 if (*dir) {
597 if ((y <= tb->sections[0]._y + tb->sections[0].h)
598 && (y >= tb->sections[0]._y)) {
599 /* if it's on the same line */
600 if (x < tb->sections[0].x)
601 *dir = 0;
603 } else {
604 if ((y <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
605 && (y >= tb->sections[tb->nsections - 1]._y)) {
606 /* if it's on the same line */
607 if (x > tb->sections[tb->nsections - 1].x)
608 *dir = 1;
612 return 1;
615 static void paintText(Text * tPtr)
617 TextBlock *tb;
618 WMFont *font;
619 const char *text;
620 int len, y, c, s, done = False, dir /* 1 = down */ ;
621 WMScreen *scr = tPtr->view->screen;
622 Display *dpy = tPtr->view->screen->display;
623 Window win = tPtr->view->window;
624 WMColor *color;
626 if (!tPtr->view->flags.realized || !tPtr->db || tPtr->flags.frozen)
627 return;
629 XFillRectangle(dpy, tPtr->db, WMColorGC(tPtr->bgColor), 0, 0, tPtr->visible.w, tPtr->visible.h);
631 if (tPtr->bgPixmap) {
632 WMDrawPixmap(tPtr->bgPixmap, tPtr->db,
633 (tPtr->visible.w - tPtr->visible.x - tPtr->bgPixmap->width) / 2,
634 (tPtr->visible.h - tPtr->visible.y - tPtr->bgPixmap->height) / 2);
637 if (!(tb = tPtr->currentTextBlock)) {
638 if (!(tb = tPtr->firstTextBlock)) {
639 goto _copy_area;
643 done = False;
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr, 0, tPtr->vpos, &dir, tb))
649 goto _copy_area;
651 while (tb) {
653 if (tb->graphic && tPtr->flags.monoFont)
654 goto _getSibling;
656 if (dir) {
657 if (tPtr->vpos <= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
658 break;
659 } else {
660 if (tPtr->vpos >= tb->sections[tb->nsections - 1]._y + tb->sections[tb->nsections - 1].h)
661 break;
664 _getSibling:
665 if (dir) {
666 if (tb->next)
667 tb = tb->next;
668 else
669 break;
670 } else {
671 if (tb->prior)
672 tb = tb->prior;
673 else
674 break;
678 /* first, place all text that can be viewed */
679 while (!done && tb) {
680 if (tb->graphic) {
681 tb = tb->next;
682 continue;
685 tb->selected = False;
687 for (s = 0; s < tb->nsections && !done; s++) {
689 if (tb->sections[s]._y > tPtr->vpos + tPtr->visible.h) {
690 done = True;
691 break;
694 if (tb->sections[s].y + tb->sections[s].h < tPtr->vpos)
695 continue;
697 if (tPtr->flags.monoFont) {
698 font = tPtr->dFont;
699 color = tPtr->fgColor;
700 } else {
701 font = tb->d.font;
702 color = tb->color;
705 if (tPtr->flags.ownsSelection) {
706 XRectangle rect;
708 if (sectionWasSelected(tPtr, tb, &rect, s)) {
709 tb->selected = True;
710 XFillRectangle(dpy, tPtr->db, WMColorGC(scr->gray),
711 rect.x, rect.y, rect.width, rect.height);
715 len = tb->sections[s].end - tb->sections[s].begin;
716 text = &(tb->text[tb->sections[s].begin]);
717 y = tb->sections[s].y - tPtr->vpos;
718 WMDrawString(scr, tPtr->db, color, font, tb->sections[s].x - tPtr->hpos, y, text, len);
720 if (!tPtr->flags.monoFont && tb->underlined) {
721 XDrawLine(dpy, tPtr->db, WMColorGC(color),
722 tb->sections[s].x - tPtr->hpos,
723 y + font->y + 1,
724 tb->sections[s].x + tb->sections[s].w - tPtr->hpos, y + font->y + 1);
727 tb = (!done ? tb->next : NULL);
730 /* now , show all graphic items that can be viewed */
731 c = WMGetArrayItemCount(tPtr->gfxItems);
732 if (c > 0 && !tPtr->flags.monoFont) {
733 int j, h;
735 for (j = 0; j < c; j++) {
736 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
738 /* if it's not viewable, and mapped, unmap it */
739 if (tb->sections[0]._y + tb->sections[0].h <= tPtr->vpos
740 || tb->sections[0]._y >= tPtr->vpos + tPtr->visible.h) {
742 if (tb->object) {
743 if ((W_VIEW(tb->d.widget))->flags.mapped) {
744 WMUnmapWidget(tb->d.widget);
747 } else {
748 /* if it's viewable, and not mapped, map it */
749 if (tb->object) {
750 W_View *view = W_VIEW(tb->d.widget);
752 if (!view->flags.realized)
753 WMRealizeWidget(tb->d.widget);
754 if (!view->flags.mapped) {
755 XMapWindow(view->screen->display, view->window);
756 XFlush(view->screen->display);
757 view->flags.mapped = 1;
761 if (tb->object) {
762 WMMoveWidget(tb->d.widget,
763 tb->sections[0].x + tPtr->visible.x - tPtr->hpos,
764 tb->sections[0].y + tPtr->visible.y - tPtr->vpos);
765 h = WMWidgetHeight(tb->d.widget) + 1;
767 } else {
768 WMDrawPixmap(tb->d.pixmap, tPtr->db,
769 tb->sections[0].x - tPtr->hpos,
770 tb->sections[0].y - tPtr->vpos);
771 h = tb->d.pixmap->height + 1;
775 if (tPtr->flags.ownsSelection) {
776 XRectangle rect;
778 if (sectionWasSelected(tPtr, tb, &rect, 0)) {
779 Drawable d = (0 && tb->object ?
780 (WMWidgetView(tb->d.widget))->window : tPtr->db);
782 tb->selected = True;
783 XFillRectangle(dpy, d, tPtr->stippledGC,
784 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
785 rect.x, rect.y, rect.width, rect.height);
789 if (!tPtr->flags.monoFont && tb->underlined) {
790 XDrawLine(dpy, tPtr->db, WMColorGC(tb->color),
791 tb->sections[0].x - tPtr->hpos,
792 tb->sections[0].y + h - tPtr->vpos,
793 tb->sections[0].x + tb->sections[0].w - tPtr->hpos,
794 tb->sections[0].y + h - tPtr->vpos);
800 _copy_area:
801 if (tPtr->flags.editable && tPtr->flags.cursorShown && tPtr->cursor.x != -23 && tPtr->flags.focused) {
802 int y = tPtr->cursor.y - tPtr->vpos;
803 XDrawLine(dpy, tPtr->db, WMColorGC(tPtr->fgColor),
804 tPtr->cursor.x, y, tPtr->cursor.x, y + tPtr->cursor.h);
807 XCopyArea(dpy, tPtr->db, win, WMColorGC(tPtr->bgColor), 0, 0,
808 tPtr->visible.w, tPtr->visible.h, tPtr->visible.x, tPtr->visible.y);
810 W_DrawRelief(scr, win, 0, 0, tPtr->view->size.width, tPtr->view->size.height, tPtr->flags.relief);
812 if (tPtr->ruler && tPtr->flags.rulerShown)
813 XDrawLine(dpy, win, WMColorGC(tPtr->fgColor), 2, 42, tPtr->view->size.width - 4, 42);
817 static void mouseOverObject(Text * tPtr, int x, int y)
819 TextBlock *tb;
820 Bool result = False;
822 x -= tPtr->visible.x;
823 x += tPtr->hpos;
824 y -= tPtr->visible.y;
825 y += tPtr->vpos;
827 if (tPtr->flags.ownsSelection) {
828 if (tPtr->sel.x <= x
829 && tPtr->sel.y <= y && tPtr->sel.x + tPtr->sel.w >= x && tPtr->sel.y + tPtr->sel.h >= y) {
830 tPtr->flags.isOverGraphic = 1;
831 result = True;
835 if (!result) {
836 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
838 if (c < 1)
839 tPtr->flags.isOverGraphic = 0;
841 for (j = 0; j < c; j++) {
842 tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j);
844 if (!tb || !tb->sections) {
845 tPtr->flags.isOverGraphic = 0;
846 return;
849 if (!tb->object) {
850 if (tb->sections[0].x <= x
851 && tb->sections[0].y <= y
852 && tb->sections[0].x + tb->sections[0].w >= x
853 && tb->sections[0].y + tb->d.pixmap->height >= y) {
854 tPtr->flags.isOverGraphic = 3;
855 result = True;
856 break;
863 if (!result)
864 tPtr->flags.isOverGraphic = 0;
866 tPtr->view->attribs.cursor = (result ? tPtr->view->screen->defaultCursor : tPtr->view->screen->textCursor);
868 XSetWindowAttributes attribs;
869 attribs.cursor = tPtr->view->attribs.cursor;
870 XChangeWindowAttributes(tPtr->view->screen->display, tPtr->view->window, CWCursor, &attribs);
874 #if DO_BLINK
876 static void blinkCursor(void *data)
878 Text *tPtr = (Text *) data;
880 if (tPtr->flags.cursorShown) {
881 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
882 } else {
883 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
885 paintText(tPtr);
886 tPtr->flags.cursorShown = !tPtr->flags.cursorShown;
888 #endif
890 static void updateCursorPosition(Text * tPtr)
892 TextBlock *tb = NULL;
893 int x, y, h, s;
895 if (tPtr->flags.needsLayOut)
896 layOutDocument(tPtr);
898 if (!(tb = tPtr->currentTextBlock)) {
899 if (!(tb = tPtr->firstTextBlock)) {
900 WMFont *font = tPtr->dFont;
901 tPtr->tpos = 0;
902 tPtr->cursor.h = font->height + abs(font->height - font->y);
904 tPtr->cursor.y = 2;
905 tPtr->cursor.x = 2;
906 return;
910 if (tb->blank) {
911 tPtr->tpos = 0;
912 y = tb->sections[0].y;
913 h = tb->sections[0].h;
914 x = tb->sections[0].x;
916 } else if (tb->graphic) {
917 y = tb->sections[0].y;
918 h = tb->sections[0].h;
919 x = tb->sections[0].x;
920 if (tPtr->tpos == 1)
921 x += tb->sections[0].w;
923 } else {
924 if (tPtr->tpos > tb->used)
925 tPtr->tpos = tb->used;
927 for (s = 0; s < tb->nsections - 1; s++) {
929 if (tPtr->tpos >= tb->sections[s].begin && tPtr->tpos <= tb->sections[s].end)
930 break;
933 y = tb->sections[s]._y;
934 h = tb->sections[s].h;
935 x = tb->sections[s].x + WMWidthOfString((tPtr->flags.monoFont ? tPtr->dFont : tb->d.font),
936 &tb->text[tb->sections[s].begin],
937 tPtr->tpos - tb->sections[s].begin);
940 tPtr->cursor.y = y;
941 tPtr->cursor.h = h;
942 tPtr->cursor.x = x;
944 /* scroll the bars if the cursor is not visible */
945 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
946 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
947 tPtr->vpos +=
948 (tPtr->cursor.y + tPtr->cursor.h + 10
949 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
950 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
951 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
956 updateScrollers(tPtr);
959 static void cursorToTextPosition(Text * tPtr, int x, int y)
961 TextBlock *tb = NULL;
962 int done = False, s, pos, len, _w, _y, dir = 1; /* 1 == "down" */
963 const char *text;
965 if (tPtr->flags.needsLayOut)
966 layOutDocument(tPtr);
968 y += (tPtr->vpos - tPtr->visible.y);
969 if (y < 0)
970 y = 0;
972 x -= (tPtr->visible.x - 2);
973 if (x < 0)
974 x = 0;
976 /* clicked is relative to document, not window... */
977 tPtr->clicked.x = x;
978 tPtr->clicked.y = y;
980 if (!(tb = tPtr->currentTextBlock)) {
981 if (!(tb = tPtr->firstTextBlock)) {
982 WMFont *font = tPtr->dFont;
983 tPtr->tpos = 0;
984 tPtr->cursor.h = font->height + abs(font->height - font->y);
985 tPtr->cursor.y = 2;
986 tPtr->cursor.x = 2;
987 return;
991 /* first, which direction? Most likely, newly clicked
992 position will be close to previous */
993 if (!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))
994 return;
996 s = (dir ? 0 : tb->nsections - 1);
997 if (y >= tb->sections[s]._y && y <= tb->sections[s]._y + tb->sections[s].h) {
998 goto _doneV;
1001 /* get the first (or last) section of the TextBlock that
1002 lies about the vertical click point */
1003 done = False;
1004 while (!done && tb) {
1006 if (tPtr->flags.monoFont && tb->graphic) {
1007 if ((dir ? tb->next : tb->prior))
1008 tb = (dir ? tb->next : tb->prior);
1009 continue;
1012 s = (dir ? 0 : tb->nsections - 1);
1013 while (!done && (dir ? (s < tb->nsections) : (s >= 0))) {
1015 if ((dir ? (y <= tb->sections[s]._y + tb->sections[s].h) : (y >= tb->sections[s]._y))) {
1016 done = True;
1017 } else {
1018 dir ? s++ : s--;
1022 if (!done) {
1023 if ((dir ? tb->next : tb->prior)) {
1024 tb = (dir ? tb->next : tb->prior);
1025 } else {
1026 pos = tb->used;
1027 break; /* goto _doneH; */
1032 if (s < 0 || s >= tb->nsections) {
1033 s = (dir ? tb->nsections - 1 : 0);
1036 _doneV:
1037 /* we have the line, which TextBlock on that line is it? */
1038 pos = (dir ? 0 : tb->sections[s].begin);
1039 if (tPtr->flags.monoFont && tb->graphic) {
1040 TextBlock *hold = tb;
1041 tb = getFirstNonGraphicBlockFor(hold, dir);
1043 if (!tb) {
1044 tPtr->tpos = 0;
1045 tb = hold;
1046 s = 0;
1047 goto _doNothing;
1051 if (tb->blank)
1052 _w = 0;
1054 _y = tb->sections[s]._y;
1056 while (tb) {
1058 if (tPtr->flags.monoFont && tb->graphic) {
1059 tb = (dir ? tb->next : tb->prior);
1060 continue;
1063 if (dir) {
1064 if (tb->graphic) {
1065 if (tb->object)
1066 _w = WMWidgetWidth(tb->d.widget) - 5;
1067 else
1068 _w = tb->d.pixmap->width - 5;
1070 if (tb->sections[0].x + _w >= x)
1071 break;
1072 } else {
1073 text = &(tb->text[tb->sections[s].begin]);
1074 len = tb->sections[s].end - tb->sections[s].begin;
1075 _w = WMWidthOfString(tb->d.font, text, len);
1076 if (tb->sections[s].x + _w >= x)
1077 break;
1080 } else {
1081 if (tb->sections[s].x <= x)
1082 break;
1085 if ((dir ? tb->next : tb->prior)) {
1086 TextBlock *nxt = (dir ? tb->next : tb->prior);
1087 if (tPtr->flags.monoFont && nxt->graphic) {
1088 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1089 if (!nxt) {
1090 pos = (dir ? 0 : tb->sections[s].begin);
1091 tPtr->cursor.x = tb->sections[s].x;
1092 goto _doneH;
1096 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1097 /* this must be the last/first on this line. stop */
1098 pos = (dir ? tb->sections[s].end : 0);
1099 tPtr->cursor.x = tb->sections[s].x;
1100 if (!tb->blank) {
1101 if (tb->graphic) {
1102 if (tb->object)
1103 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1104 else
1105 tPtr->cursor.x += tb->d.pixmap->width;
1106 } else if (pos > tb->sections[s].begin) {
1107 tPtr->cursor.x +=
1108 WMWidthOfString(tb->d.font,
1109 &(tb->text[tb->sections[s].begin]),
1110 pos - tb->sections[s].begin);
1113 goto _doneH;
1117 if ((dir ? tb->next : tb->prior)) {
1118 tb = (dir ? tb->next : tb->prior);
1119 } else {
1120 done = True;
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;
1256 scroll = True;
1258 break;
1260 case WSIncrementLine:{
1261 int limit = tPtr->docHeight - height;
1262 if (tPtr->vpos < limit) {
1263 if (tPtr->vpos < limit - 16)
1264 tPtr->vpos += 16;
1265 else
1266 tPtr->vpos = limit;
1267 scroll = True;
1270 break;
1272 case WSDecrementPage:
1273 if (((int)tPtr->vpos - (int)height) >= 0)
1274 tPtr->vpos -= height;
1275 else
1276 tPtr->vpos = 0;
1278 scroll = True;
1279 break;
1281 case WSIncrementPage:
1282 tPtr->vpos += height;
1283 if (tPtr->vpos > (tPtr->docHeight - height))
1284 tPtr->vpos = tPtr->docHeight - height;
1285 scroll = True;
1286 break;
1288 case WSKnob:
1289 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1290 * (float)(tPtr->docHeight - height);
1291 scroll = True;
1292 break;
1294 case WSKnobSlot:
1295 case WSNoPart:
1296 break;
1298 scroll = (tPtr->vpos != tPtr->prevVpos);
1299 tPtr->prevVpos = tPtr->vpos;
1302 if (w == tPtr->hS) {
1303 int width = tPtr->visible.w;
1305 which = WMGetScrollerHitPart(tPtr->hS);
1306 switch (which) {
1308 case WSDecrementLine:
1309 if (tPtr->hpos > 0) {
1310 if (tPtr->hpos > 16)
1311 tPtr->hpos -= 16;
1312 else
1313 tPtr->hpos = 0;
1314 scroll = True;
1316 break;
1318 case WSIncrementLine:{
1319 int limit = tPtr->docWidth - width;
1320 if (tPtr->hpos < limit) {
1321 if (tPtr->hpos < limit - 16)
1322 tPtr->hpos += 16;
1323 else
1324 tPtr->hpos = limit;
1325 scroll = True;
1328 break;
1330 case WSDecrementPage:
1331 if (((int)tPtr->hpos - (int)width) >= 0)
1332 tPtr->hpos -= width;
1333 else
1334 tPtr->hpos = 0;
1336 scroll = True;
1337 break;
1339 case WSIncrementPage:
1340 tPtr->hpos += width;
1341 if (tPtr->hpos > (tPtr->docWidth - width))
1342 tPtr->hpos = tPtr->docWidth - width;
1343 scroll = True;
1344 break;
1346 case WSKnob:
1347 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1348 * (float)(tPtr->docWidth - width);
1349 scroll = True;
1350 break;
1352 case WSKnobSlot:
1353 case WSNoPart:
1354 break;
1356 scroll = (tPtr->hpos != tPtr->prevHpos);
1357 tPtr->prevHpos = tPtr->hpos;
1360 if (scroll) {
1361 updateScrollers(tPtr);
1362 paintText(tPtr);
1366 typedef struct {
1367 TextBlock *tb;
1368 unsigned short begin, end; /* what part of the text block */
1369 } myLineItems;
1371 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1373 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1374 WMFont *font;
1375 const char *text;
1376 TextBlock *tb, *tbsame = NULL;
1378 if (!items || nitems == 0)
1379 return 0;
1381 for (i = 0; i < nitems; i++) {
1382 tb = items[i].tb;
1384 if (tb->graphic) {
1385 if (!tPtr->flags.monoFont) {
1386 if (tb->object) {
1387 WMWidget *wdt = tb->d.widget;
1388 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1389 if (tPtr->flags.alignment != WALeft)
1390 lw += WMWidgetWidth(wdt);
1391 } else {
1392 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1393 if (tPtr->flags.alignment != WALeft)
1394 lw += tb->d.pixmap->width;
1398 } else {
1399 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1400 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1401 max_d = 2;
1402 line_height = WMAX(line_height, font->height + max_d);
1403 text = &(tb->text[items[i].begin]);
1404 len = items[i].end - items[i].begin;
1405 if (tPtr->flags.alignment != WALeft)
1406 lw += WMWidthOfString(font, text, len);
1410 if (tPtr->flags.alignment == WARight) {
1411 j = tPtr->visible.w - lw;
1412 } else if (tPtr->flags.alignment == WACenter) {
1413 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1416 for (i = 0; i < nitems; i++) {
1417 tb = items[i].tb;
1419 if (tbsame == tb) { /* extend it, since it's on same line */
1420 tb->sections[tb->nsections - 1].end = items[i].end;
1421 n = tb->nsections - 1;
1422 } else {
1423 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1424 n = tb->nsections - 1;
1425 tb->sections[n]._y = y + max_d;
1426 tb->sections[n].max_d = max_d;
1427 tb->sections[n].x = x + j;
1428 tb->sections[n].h = line_height;
1429 tb->sections[n].begin = items[i].begin;
1430 tb->sections[n].end = items[i].end;
1433 tb->sections[n].last = (i + 1 == nitems);
1435 if (tb->graphic) {
1436 if (!tPtr->flags.monoFont) {
1437 if (tb->object) {
1438 WMWidget *wdt = tb->d.widget;
1439 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1440 tb->sections[n].w = WMWidgetWidth(wdt);
1441 } else {
1442 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1443 tb->sections[n].w = tb->d.pixmap->width;
1445 x += tb->sections[n].w;
1447 } else {
1448 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1449 len = items[i].end - items[i].begin;
1450 text = &(tb->text[items[i].begin]);
1452 tb->sections[n].y = y + line_height - font->y;
1453 tb->sections[n].w =
1454 WMWidthOfString(font,
1455 &(tb->text[tb->sections[n].begin]),
1456 tb->sections[n].end - tb->sections[n].begin);
1458 x += WMWidthOfString(font, text, len);
1461 tbsame = tb;
1464 return line_height;
1468 static void layOutDocument(Text * tPtr)
1470 TextBlock *tb;
1471 myLineItems *items = NULL;
1472 unsigned int itemsSize = 0, nitems = 0, begin, end;
1473 WMFont *font;
1474 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1475 const char *start = NULL, *mark = NULL;
1477 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1478 return;
1480 assert(tPtr->visible.w > 20);
1482 tPtr->docWidth = tPtr->visible.w;
1483 x = tPtr->margins[tb->marginN].first;
1484 bmargin = tPtr->margins[tb->marginN].body;
1486 /* only partial layOut needed: re-Lay only affected textblocks */
1487 if (tPtr->flags.laidOut) {
1488 tb = tPtr->currentTextBlock;
1490 /* search backwards for textblocks on same line */
1491 while (tb->prior) {
1492 if (!tb->sections || tb->nsections < 1) {
1493 tb = tPtr->firstTextBlock;
1494 tPtr->flags.laidOut = False;
1495 y = 0;
1496 goto _layOut;
1499 if (!tb->prior->sections || tb->prior->nsections < 1) {
1500 tb = tPtr->firstTextBlock;
1501 tPtr->flags.laidOut = False;
1502 y = 0;
1503 goto _layOut;
1506 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1507 break;
1509 tb = tb->prior;
1512 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1513 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1514 tb->prior->sections[tb->prior->nsections - 1].h -
1515 tb->prior->sections[tb->prior->nsections - 1].max_d;
1516 } else {
1517 y = 0;
1521 _layOut:
1522 while (tb) {
1524 if (tb->sections && tb->nsections > 0) {
1525 wfree(tb->sections);
1526 tb->sections = NULL;
1527 tb->nsections = 0;
1530 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1531 TextBlock *next = tb->next;
1532 tPtr->currentTextBlock = tb;
1533 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1534 tb = next;
1535 tb->first = True;
1536 continue;
1539 if (tb->first && tb != tPtr->firstTextBlock) {
1540 y += layOutLine(tPtr, items, nitems, x, y);
1541 x = tPtr->margins[tb->marginN].first;
1542 bmargin = tPtr->margins[tb->marginN].body;
1543 nitems = 0;
1544 lw = 0;
1547 if (tb->graphic) {
1548 if (!tPtr->flags.monoFont) {
1549 if (tb->object)
1550 width = WMWidgetWidth(tb->d.widget);
1551 else
1552 width = tb->d.pixmap->width;
1554 if (width > tPtr->docWidth)
1555 tPtr->docWidth = width;
1557 lw += width;
1558 if (lw >= tPtr->visible.w - x) {
1559 y += layOutLine(tPtr, items, nitems, x, y);
1560 nitems = 0;
1561 x = bmargin;
1562 lw = width;
1565 if (nitems + 1 > itemsSize) {
1566 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1569 items[nitems].tb = tb;
1570 items[nitems].begin = 0;
1571 items[nitems].end = 0;
1572 nitems++;
1575 } else if ((start = tb->text)) {
1576 begin = end = 0;
1577 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1579 while (start) {
1580 mark = strchr(start, ' ');
1581 if (mark) {
1582 end += (int)(mark - start) + 1;
1583 start = mark + 1;
1584 } else {
1585 end += strlen(start);
1586 start = mark;
1589 if (end > tb->used)
1590 end = tb->used;
1592 if (end - begin > 0) {
1594 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1596 /* if it won't fit, char wrap it */
1597 if (width >= tPtr->visible.w) {
1598 char *t = &tb->text[begin];
1599 int l = end - begin, i = 0;
1600 do {
1601 width = WMWidthOfString(font, t, ++i);
1602 } while (width < tPtr->visible.w && i < l);
1603 if (i > 2)
1604 i--;
1605 end = begin + i;
1606 start = &tb->text[end];
1609 lw += width;
1612 if (lw >= tPtr->visible.w - x) {
1613 y += layOutLine(tPtr, items, nitems, x, y);
1614 lw = width;
1615 x = bmargin;
1616 nitems = 0;
1619 if (nitems + 1 > itemsSize) {
1620 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1623 items[nitems].tb = tb;
1624 items[nitems].begin = begin;
1625 items[nitems].end = end;
1626 nitems++;
1628 begin = end;
1632 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1633 if (0 && tPtr->flags.laidOut
1634 && tb->next && tb->next->sections && tb->next->nsections > 0
1635 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1636 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1637 TextBlock *ltb = tPtr->lastTextBlock;
1638 int ly = ltb->sections[ltb->nsections - 1]._y;
1639 int lh = ltb->sections[ltb->nsections - 1].h;
1640 int ss, sd;
1642 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1643 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1645 y += layOutLine(tPtr, items, nitems, x, y);
1646 ss = ly + lh - y;
1647 sd = tPtr->docHeight - y;
1649 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1650 y += tb->next->sections[0]._y - y;
1651 nitems = 0;
1652 printf("nitems%d\n", nitems);
1653 if (ss - sd != 0)
1654 y = tPtr->docHeight + ss - sd;
1656 break;
1657 } else {
1658 tPtr->flags.laidOut = False;
1662 tb = tb->next;
1665 if (nitems > 0)
1666 y += layOutLine(tPtr, items, nitems, x, y);
1668 if (tPtr->docHeight != y + 10) {
1669 tPtr->docHeight = y + 10;
1670 updateScrollers(tPtr);
1673 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1674 XEvent event;
1676 tPtr->flags.horizOnDemand = True;
1677 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1678 event.type = Expose;
1679 handleEvents(&event, (void *)tPtr);
1681 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1682 tPtr->flags.horizOnDemand = False;
1683 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1686 tPtr->flags.laidOut = True;
1688 if (items && itemsSize > 0)
1689 wfree(items);
1693 static void textDidResize(W_ViewDelegate * self, WMView * view)
1695 Text *tPtr = (Text *) view->self;
1696 unsigned short w = tPtr->view->size.width;
1697 unsigned short h = tPtr->view->size.height;
1698 unsigned short rh = 0, vw = 0, rel;
1700 /* Parameter not used, but tell the compiler that it is ok */
1701 (void) self;
1703 rel = (tPtr->flags.relief == WRFlat);
1705 if (tPtr->ruler && tPtr->flags.rulerShown) {
1706 WMMoveWidget(tPtr->ruler, 2, 2);
1707 WMResizeWidget(tPtr->ruler, w - 4, 40);
1708 rh = 40;
1711 if (tPtr->vS) {
1712 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1713 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1714 vw = 20;
1715 WMSetRulerOffset(tPtr->ruler, 22);
1716 } else
1717 WMSetRulerOffset(tPtr->ruler, 2);
1719 if (tPtr->hS) {
1720 if (tPtr->vS) {
1721 WMMoveWidget(tPtr->hS, vw, h - 21);
1722 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1723 } else {
1724 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1725 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1729 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1730 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1731 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1732 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1733 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1734 tPtr->margins[0].right = tPtr->visible.w;
1736 if (tPtr->view->flags.realized) {
1738 if (tPtr->db) {
1739 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1740 tPtr->db = (Pixmap) NULL;
1743 if (tPtr->visible.w < 40)
1744 tPtr->visible.w = 40;
1745 if (tPtr->visible.h < 20)
1746 tPtr->visible.h = 20;
1748 if (!tPtr->db) {
1749 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1750 tPtr->view->window, tPtr->visible.w,
1751 tPtr->visible.h, tPtr->view->screen->depth);
1755 WMThawText(tPtr);
1758 W_ViewDelegate _TextViewDelegate = {
1759 NULL,
1760 NULL,
1761 textDidResize,
1762 NULL,
1763 NULL
1766 #define TEXT_BUFFER_INCR 8
1767 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1769 static void clearText(Text * tPtr)
1771 tPtr->vpos = tPtr->hpos = 0;
1772 tPtr->docHeight = tPtr->docWidth = 0;
1773 tPtr->cursor.x = -23;
1775 if (!tPtr->firstTextBlock)
1776 return;
1778 while (tPtr->currentTextBlock)
1779 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1781 tPtr->firstTextBlock = NULL;
1782 tPtr->currentTextBlock = NULL;
1783 tPtr->lastTextBlock = NULL;
1784 WMEmptyArray(tPtr->gfxItems);
1787 /* possibly remove a single character from the currentTextBlock,
1788 or if there's a selection, remove it...
1789 note that Delete and Backspace are treated differently */
1790 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1792 TextBlock *tb;
1793 Bool back = (Bool) (ksym == XK_BackSpace);
1794 Bool done = 1, wasFirst = 0;
1796 if (!tPtr->flags.editable)
1797 return;
1799 if (!(tb = tPtr->currentTextBlock))
1800 return;
1802 if (tPtr->flags.ownsSelection) {
1803 if (removeSelection(tPtr))
1804 layOutDocument(tPtr);
1805 return;
1808 wasFirst = tb->first;
1809 if (back && tPtr->tpos < 1) {
1810 if (tb->prior) {
1811 if (tb->prior->blank) {
1812 tPtr->currentTextBlock = tb->prior;
1813 WMRemoveTextBlock(tPtr);
1814 tPtr->currentTextBlock = tb;
1815 tb->first = True;
1816 layOutDocument(tPtr);
1817 return;
1818 } else {
1819 if (tb->blank) {
1820 TextBlock *prior = tb->prior;
1821 tPtr->currentTextBlock = tb;
1822 WMRemoveTextBlock(tPtr);
1823 tb = prior;
1824 } else {
1825 tb = tb->prior;
1828 if (tb->graphic)
1829 tPtr->tpos = 1;
1830 else
1831 tPtr->tpos = tb->used;
1833 tPtr->currentTextBlock = tb;
1834 done = 1;
1835 if (wasFirst) {
1836 if (tb->next)
1837 tb->next->first = False;
1838 layOutDocument(tPtr);
1839 return;
1845 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1846 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1847 if (back)
1848 tPtr->tpos--;
1849 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1850 tb->used--;
1851 done = 0;
1854 /* if there are no characters left to back over in the textblock,
1855 but it still has characters to the right of the cursor: */
1856 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1857 || tb->graphic) {
1859 /* no more chars, and it's marked as blank? */
1860 if (tb->blank) {
1861 TextBlock *sibling = (back ? tb->prior : tb->next);
1863 if (tb->used == 0 || tb->graphic)
1864 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1866 if (sibling) {
1867 tPtr->currentTextBlock = sibling;
1868 if (tb->graphic)
1869 tPtr->tpos = (back ? 1 : 0);
1870 else
1871 tPtr->tpos = (back ? sibling->used : 0);
1873 /* no more chars, so mark it as blank */
1874 } else if (tb->used == 0) {
1875 tb->blank = 1;
1876 } else if (tb->graphic) {
1877 Bool hasNext = (tb->next != NULL);
1879 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1880 if (hasNext) {
1881 tPtr->tpos = 0;
1882 } else if (tPtr->currentTextBlock) {
1883 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1885 } else
1886 printf("DEBUG: unaccounted for... catch this!\n");
1889 layOutDocument(tPtr);
1892 static void insertTextInteractively(Text * tPtr, char *text, int len)
1894 TextBlock *tb;
1895 char *newline = NULL;
1897 if (!tPtr->flags.editable) {
1898 return;
1901 if (len < 1 || !text)
1902 return;
1904 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1905 return;
1907 if (tPtr->flags.ownsSelection)
1908 removeSelection(tPtr);
1910 if (tPtr->flags.ignoreNewLine) {
1911 int i;
1912 for (i = 0; i < len; i++) {
1913 if (text[i] == '\n')
1914 text[i] = ' ';
1918 tb = tPtr->currentTextBlock;
1919 if (!tb || tb->graphic) {
1920 tPtr->tpos = 0;
1921 WMAppendTextStream(tPtr, text);
1922 layOutDocument(tPtr);
1923 return;
1926 if ((newline = strchr(text, '\n'))) {
1927 int nlen = (int)(newline - text);
1928 int s = tb->used - tPtr->tpos;
1930 if (!tb->blank && nlen > 0) {
1931 char *save = NULL;
1933 if (s > 0) {
1934 save = wmalloc(s);
1935 memcpy(save, &tb->text[tPtr->tpos], s);
1936 tb->used -= (tb->used - tPtr->tpos);
1938 insertTextInteractively(tPtr, text, nlen);
1939 newline++;
1940 WMAppendTextStream(tPtr, newline);
1941 if (s > 0) {
1942 insertTextInteractively(tPtr, save, s);
1943 wfree(save);
1945 } else {
1946 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1948 unsigned short savePos = tPtr->tpos;
1949 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1950 tb->d.font, tb->color, True,
1951 tb->used - tPtr->tpos);
1953 if (tb->sections[0].end == tPtr->tpos)
1954 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1955 NULL, tb->d.font,
1956 tb->color, True, 0));
1958 tb->used = savePos;
1959 WMAppendTextBlock(tPtr, ntb);
1960 tPtr->tpos = 0;
1962 } else if (tPtr->tpos == tb->used) {
1963 if (tPtr->flags.indentNewLine) {
1964 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1965 " ", tb->d.font,
1966 tb->color, True, 4));
1967 tPtr->tpos = 4;
1968 } else {
1969 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1970 NULL, tb->d.font,
1971 tb->color, True, 0));
1972 tPtr->tpos = 0;
1974 } else if (tPtr->tpos == 0) {
1975 if (tPtr->flags.indentNewLine) {
1976 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1977 " ", tb->d.font,
1978 tb->color, True, 4));
1979 } else {
1980 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1981 NULL, tb->d.font,
1982 tb->color, True, 0));
1984 tPtr->tpos = 0;
1985 if (tPtr->currentTextBlock->next)
1986 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1989 } else {
1990 if (tb->used + len >= tb->allocated) {
1991 tb->allocated = reqBlockSize(tb->used + len);
1992 tb->text = wrealloc(tb->text, tb->allocated);
1995 if (tb->blank) {
1996 memcpy(tb->text, text, len);
1997 tb->used = len;
1998 tPtr->tpos = len;
1999 tb->text[tb->used] = 0;
2000 tb->blank = False;
2002 } else {
2003 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
2004 memmove(&tb->text[tPtr->tpos], text, len);
2005 tb->used += len;
2006 tPtr->tpos += len;
2007 tb->text[tb->used] = 0;
2012 layOutDocument(tPtr);
2015 static void selectRegion(Text * tPtr, int x, int y)
2018 if (x < 0 || y < 0)
2019 return;
2021 y += (tPtr->flags.rulerShown ? 40 : 0);
2022 y += tPtr->vpos;
2023 if (y > 10)
2024 y -= 10; /* the original offset */
2026 x -= tPtr->visible.x - 2;
2027 if (x < 0)
2028 x = 0;
2030 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2031 tPtr->sel.w = abs(tPtr->clicked.x - x);
2032 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2033 tPtr->sel.h = abs(tPtr->clicked.y - y);
2035 tPtr->flags.ownsSelection = True;
2036 paintText(tPtr);
2039 static void releaseSelection(Text * tPtr)
2041 TextBlock *tb = tPtr->firstTextBlock;
2043 while (tb) {
2044 tb->selected = False;
2045 tb = tb->next;
2047 tPtr->flags.ownsSelection = False;
2048 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2050 paintText(tPtr);
2053 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2055 Text *tPtr = view->self;
2056 Display *dpy = tPtr->view->screen->display;
2057 Atom _TARGETS;
2058 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2059 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2060 WMData *data = NULL;
2062 /* Parameter not used, but tell the compiler that it is ok */
2063 (void) selection;
2064 (void) cdata;
2066 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2067 char *text = WMGetTextSelectedStream(tPtr);
2069 if (text) {
2070 data = WMCreateDataWithBytes(text, strlen(text));
2071 WMSetDataFormat(data, TYPETEXT);
2073 *type = target;
2074 return data;
2075 } else
2076 printf("didn't get it\n");
2078 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2079 if (target == _TARGETS) {
2080 Atom *ptr;
2082 ptr = wmalloc(4 * sizeof(Atom));
2083 ptr[0] = _TARGETS;
2084 ptr[1] = XA_STRING;
2085 ptr[2] = TEXT;
2086 ptr[3] = COMPOUND_TEXT;
2088 data = WMCreateDataWithBytes(ptr, 4 * 4);
2089 WMSetDataFormat(data, 32);
2091 *type = target;
2092 return data;
2095 return NULL;
2098 static void lostHandler(WMView * view, Atom selection, void *cdata)
2100 /* Parameter not used, but tell the compiler that it is ok */
2101 (void) selection;
2102 (void) cdata;
2104 releaseSelection((WMText *) view->self);
2107 static WMSelectionProcs selectionHandler = {
2108 requestHandler, lostHandler, NULL
2111 static void ownershipObserver(void *observerData, WMNotification * notification)
2113 if (observerData != WMGetNotificationClientData(notification))
2114 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2117 static void autoSelectText(Text * tPtr, int clicks)
2119 int x, start;
2120 TextBlock *tb;
2121 char *mark = NULL, behind, ahead;
2123 if (!(tb = tPtr->currentTextBlock))
2124 return;
2126 if (clicks == 2) {
2128 switch (tb->text[tPtr->tpos]) {
2129 case ' ':
2130 return;
2132 case '<': case '>': behind = '<'; ahead = '>'; break;
2133 case '{': case '}': behind = '{'; ahead = '}'; break;
2134 case '[': case ']': behind = '['; ahead = ']'; break;
2136 default:
2137 behind = ahead = ' ';
2140 tPtr->sel.y = tPtr->cursor.y + 5;
2141 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2143 if (tb->graphic) {
2144 tPtr->sel.x = tb->sections[0].x;
2145 tPtr->sel.w = tb->sections[0].w;
2146 } else {
2147 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2149 start = tPtr->tpos;
2150 while (start > 0 && tb->text[start - 1] != behind)
2151 start--;
2153 x = tPtr->cursor.x;
2154 if (tPtr->tpos > start) {
2155 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2157 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2159 if ((mark = strchr(&tb->text[start], ahead))) {
2160 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2161 (int)(mark - &tb->text[start]));
2162 } else if (tb->used > start) {
2163 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2167 } else if (clicks == 3) {
2168 TextBlock *cur = tb;
2170 while (tb && !tb->first) {
2171 tb = tb->prior;
2173 tPtr->sel.y = tb->sections[0]._y;
2175 tb = cur;
2176 while (tb->next && !tb->next->first) {
2177 tb = tb->next;
2179 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2181 tPtr->sel.x = 0;
2182 tPtr->sel.w = tPtr->docWidth;
2183 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2186 if (!tPtr->flags.ownsSelection) {
2187 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2188 tPtr->flags.ownsSelection = True;
2190 paintText(tPtr);
2194 # if 0
2195 static void fontChanged(void *observerData, WMNotification * notification)
2197 WMText *tPtr = (WMText *) observerData;
2198 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2199 printf("fontChanged\n");
2201 if (!tPtr || !font)
2202 return;
2204 if (tPtr->flags.ownsSelection)
2205 WMSetTextSelectionFont(tPtr, font);
2207 #endif
2209 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2211 char buffer[64];
2212 KeySym ksym;
2213 int control_pressed = False;
2214 TextBlock *tb = NULL;
2216 if (((XKeyEvent *) event)->state & ControlMask)
2217 control_pressed = True;
2218 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2220 switch (ksym) {
2222 case XK_Home:
2223 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2224 tPtr->tpos = 0;
2225 updateCursorPosition(tPtr);
2226 paintText(tPtr);
2227 break;
2229 case XK_End:
2230 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2231 if (tPtr->currentTextBlock->graphic)
2232 tPtr->tpos = 1;
2233 else
2234 tPtr->tpos = tPtr->currentTextBlock->used;
2236 updateCursorPosition(tPtr);
2237 paintText(tPtr);
2238 break;
2240 case XK_Left:
2241 if (!(tb = tPtr->currentTextBlock))
2242 break;
2243 if (tb->graphic)
2244 goto L_imaGFX;
2246 if (tPtr->tpos == 0) {
2247 L_imaGFX:
2248 if (tb->prior) {
2249 tPtr->currentTextBlock = tb->prior;
2250 if (tPtr->currentTextBlock->graphic)
2251 tPtr->tpos = 1;
2252 else
2253 tPtr->tpos = tPtr->currentTextBlock->used;
2255 if (!tb->first && tPtr->tpos > 0)
2256 tPtr->tpos--;
2257 } else
2258 tPtr->tpos = 0;
2259 } else
2260 tPtr->tpos--;
2261 updateCursorPosition(tPtr);
2262 paintText(tPtr);
2263 break;
2265 case XK_Right:
2266 if (!(tb = tPtr->currentTextBlock))
2267 break;
2268 if (tb->graphic)
2269 goto R_imaGFX;
2270 if (tPtr->tpos == tb->used) {
2271 R_imaGFX:
2272 if (tb->next) {
2273 tPtr->currentTextBlock = tb->next;
2274 tPtr->tpos = 0;
2275 if (!tb->next->first && tb->next->used > 0)
2276 tPtr->tpos++;
2277 } else {
2278 if (tb->graphic)
2279 tPtr->tpos = 1;
2280 else
2281 tPtr->tpos = tb->used;
2283 } else
2284 tPtr->tpos++;
2285 updateCursorPosition(tPtr);
2286 paintText(tPtr);
2287 break;
2289 case XK_Down:
2290 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2291 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2292 paintText(tPtr);
2293 break;
2295 case XK_Up:
2296 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2297 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2298 paintText(tPtr);
2299 break;
2301 case XK_BackSpace:
2302 case XK_Delete:
2303 #ifdef XK_KP_Delete
2304 case XK_KP_Delete:
2305 #endif
2306 deleteTextInteractively(tPtr, ksym);
2307 updateCursorPosition(tPtr);
2308 paintText(tPtr);
2309 break;
2311 case XK_Control_R:
2312 case XK_Control_L:
2313 control_pressed = True;
2314 break;
2316 case XK_Tab:
2317 insertTextInteractively(tPtr, " ", 4);
2318 updateCursorPosition(tPtr);
2319 paintText(tPtr);
2320 break;
2322 case XK_Return:
2323 *buffer = '\n';
2324 default:
2325 if (*buffer != 0 && !control_pressed) {
2326 insertTextInteractively(tPtr, buffer, strlen(buffer));
2327 updateCursorPosition(tPtr);
2328 paintText(tPtr);
2330 } else if (control_pressed && ksym == XK_r) {
2331 Bool i = !tPtr->flags.rulerShown;
2332 WMShowTextRuler(tPtr, i);
2333 tPtr->flags.rulerShown = i;
2334 } else if (control_pressed && *buffer == '\a') {
2335 XBell(tPtr->view->screen->display, 0);
2336 } else {
2337 WMRelayToNextResponder(tPtr->view, event);
2341 if (!control_pressed && tPtr->flags.ownsSelection) {
2342 releaseSelection(tPtr);
2346 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2348 Text *tPtr = (Text *) view->self;
2349 char *text;
2351 /* Parameter not used, but tell the compiler that it is ok */
2352 (void) selection;
2353 (void) target;
2354 (void) timestamp;
2355 (void) cdata;
2357 tPtr->flags.waitingForSelection = 0;
2359 if (data) {
2360 text = (char *)WMDataBytes(data);
2362 if (tPtr->parser) {
2363 (tPtr->parser) (tPtr, (void *)text);
2364 layOutDocument(tPtr);
2365 } else
2366 insertTextInteractively(tPtr, text, strlen(text));
2367 updateCursorPosition(tPtr);
2368 paintText(tPtr);
2370 } else {
2371 int n;
2373 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2375 if (text) {
2376 text[n] = 0;
2377 if (tPtr->parser) {
2378 (tPtr->parser) (tPtr, (void *)text);
2379 layOutDocument(tPtr);
2380 } else
2381 insertTextInteractively(tPtr, text, n);
2382 updateCursorPosition(tPtr);
2383 paintText(tPtr);
2385 XFree(text);
2391 static void handleActionEvents(XEvent * event, void *data)
2393 Text *tPtr = (Text *) data;
2394 Display *dpy = event->xany.display;
2395 KeySym ksym;
2397 switch (event->type) {
2398 case KeyPress:
2399 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2400 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2401 tPtr->flags.extendSelection = True;
2402 return;
2405 if (tPtr->flags.focused) {
2406 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2407 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2408 GrabModeAsync, GrabModeAsync, None,
2409 tPtr->view->screen->invisibleCursor, CurrentTime);
2410 tPtr->flags.pointerGrabbed = True;
2411 handleTextKeyPress(tPtr, event);
2414 break;
2416 case KeyRelease:
2417 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2418 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2419 tPtr->flags.extendSelection = False;
2420 return;
2421 /* end modify flag so selection can be extended */
2423 break;
2425 case MotionNotify:
2427 if (tPtr->flags.pointerGrabbed) {
2428 tPtr->flags.pointerGrabbed = False;
2429 XUngrabPointer(dpy, CurrentTime);
2432 if (tPtr->flags.waitingForSelection)
2433 break;
2435 if ((event->xmotion.state & Button1Mask)) {
2437 if (WMIsDraggingFromView(tPtr->view)) {
2438 WMDragImageFromView(tPtr->view, event);
2439 break;
2442 if (!tPtr->flags.ownsSelection) {
2443 WMCreateSelectionHandler(tPtr->view,
2444 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2445 tPtr->flags.ownsSelection = True;
2447 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2448 break;
2451 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2452 break;
2454 case ButtonPress:
2456 if (tPtr->flags.pointerGrabbed) {
2457 tPtr->flags.pointerGrabbed = False;
2458 XUngrabPointer(dpy, CurrentTime);
2459 break;
2462 if (tPtr->flags.waitingForSelection)
2463 break;
2465 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2466 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2467 return;
2470 if (tPtr->flags.ownsSelection)
2471 releaseSelection(tPtr);
2473 if (event->xbutton.button == Button1) {
2474 TextBlock *tb = tPtr->currentTextBlock;
2476 if (WMIsDoubleClick(event)) {
2478 tPtr->lastClickTime = event->xbutton.time;
2479 if (tb && tb->graphic && !tb->object) {
2480 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2481 char *desc;
2483 desc = wmalloc(tb->used + 1);
2484 memcpy(desc, tb->text, tb->used);
2485 desc[tb->used] = 0;
2486 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2487 wfree(desc);
2489 } else {
2490 autoSelectText(tPtr, 2);
2492 break;
2493 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2494 tPtr->lastClickTime = event->xbutton.time;
2495 autoSelectText(tPtr, 3);
2496 break;
2499 if (!tPtr->flags.focused) {
2500 WMSetFocusToWidget(tPtr);
2501 tPtr->flags.focused = True;
2502 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2504 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2505 WMDragImageFromView(tPtr->view, event);
2506 break;
2509 tPtr->lastClickTime = event->xbutton.time;
2510 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2511 paintText(tPtr);
2514 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2515 WMScrollText(tPtr, 16);
2516 break;
2519 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2520 WMScrollText(tPtr, -16);
2521 break;
2524 if (event->xbutton.button == Button2) {
2525 char *text = NULL;
2526 int n;
2528 if (!tPtr->flags.editable) {
2529 XBell(dpy, 0);
2530 break;
2533 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2534 event->xbutton.time, pasteText, NULL)) {
2536 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2537 tPtr->flags.waitingForSelection = 0;
2539 if (text) {
2540 text[n] = 0;
2542 if (tPtr->parser) {
2543 (tPtr->parser) (tPtr, (void *)text);
2544 layOutDocument(tPtr);
2545 } else
2546 insertTextInteractively(tPtr, text, n);
2548 XFree(text);
2549 #if 0
2550 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2551 (void *)WMInsertTextEvent);
2552 #endif
2553 updateCursorPosition(tPtr);
2554 paintText(tPtr);
2556 } else {
2557 tPtr->flags.waitingForSelection = True;
2560 break;
2563 case ButtonRelease:
2564 if (tPtr->flags.pointerGrabbed) {
2565 tPtr->flags.pointerGrabbed = False;
2566 XUngrabPointer(dpy, CurrentTime);
2567 break;
2570 if (tPtr->flags.waitingForSelection)
2571 break;
2573 if (WMIsDraggingFromView(tPtr->view))
2574 WMDragImageFromView(tPtr->view, event);
2579 static void handleEvents(XEvent * event, void *data)
2581 Text *tPtr = (Text *) data;
2583 switch (event->type) {
2584 case Expose:
2586 if (event->xexpose.count != 0)
2587 break;
2589 if (tPtr->hS) {
2590 if (!(W_VIEW(tPtr->hS))->flags.realized)
2591 WMRealizeWidget(tPtr->hS);
2594 if (tPtr->vS) {
2595 if (!(W_VIEW(tPtr->vS))->flags.realized)
2596 WMRealizeWidget(tPtr->vS);
2599 if (tPtr->ruler) {
2600 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2601 WMRealizeWidget(tPtr->ruler);
2605 if (!tPtr->db)
2606 textDidResize(tPtr->view->delegate, tPtr->view);
2608 paintText(tPtr);
2609 break;
2611 case FocusIn:
2612 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2613 != tPtr->view)
2614 return;
2615 tPtr->flags.focused = True;
2616 #if DO_BLINK
2617 if (tPtr->flags.editable && !tPtr->timerID) {
2618 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2620 #endif
2622 break;
2624 case FocusOut:
2625 tPtr->flags.focused = False;
2626 paintText(tPtr);
2627 #if DO_BLINK
2628 if (tPtr->timerID) {
2629 WMDeleteTimerHandler(tPtr->timerID);
2630 tPtr->timerID = NULL;
2632 #endif
2633 break;
2635 case DestroyNotify:
2636 clearText(tPtr);
2637 if (tPtr->db)
2638 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2639 if (tPtr->gfxItems)
2640 WMEmptyArray(tPtr->gfxItems);
2641 #if DO_BLINK
2642 if (tPtr->timerID)
2643 WMDeleteTimerHandler(tPtr->timerID);
2644 #endif
2645 WMReleaseFont(tPtr->dFont);
2646 WMReleaseColor(tPtr->dColor);
2647 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2648 WMRemoveNotificationObserver(tPtr);
2650 WMFreeArray(tPtr->xdndSourceTypes);
2651 WMFreeArray(tPtr->xdndDestinationTypes);
2653 wfree(tPtr);
2655 break;
2660 static void insertPlainText(Text * tPtr, const char *text)
2662 const char *start, *mark;
2663 void *tb = NULL;
2665 start = text;
2666 while (start) {
2667 mark = strchr(start, '\n');
2668 if (mark) {
2669 tb = WMCreateTextBlockWithText(tPtr,
2670 start, tPtr->dFont,
2671 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2672 start = mark + 1;
2673 tPtr->flags.first = True;
2674 } else {
2675 if (start && strlen(start)) {
2676 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2677 tPtr->dColor, tPtr->flags.first, strlen(start));
2678 } else
2679 tb = NULL;
2680 tPtr->flags.first = False;
2681 start = mark;
2684 if (tPtr->flags.prepend)
2685 WMPrependTextBlock(tPtr, tb);
2686 else
2687 WMAppendTextBlock(tPtr, tb);
2691 static void rulerMoveCallBack(WMWidget * w, void *self)
2693 Text *tPtr = (Text *) self;
2695 /* Parameter not used, but tell the compiler that it is ok */
2696 (void) w;
2698 if (!tPtr)
2699 return;
2700 if (W_CLASS(tPtr) != WC_Text)
2701 return;
2703 paintText(tPtr);
2706 static void rulerReleaseCallBack(WMWidget * w, void *self)
2708 Text *tPtr = (Text *) self;
2710 /* Parameter not used, but tell the compiler that it is ok */
2711 (void) w;
2713 if (!tPtr)
2714 return;
2715 if (W_CLASS(tPtr) != WC_Text)
2716 return;
2718 WMThawText(tPtr);
2719 return;
2722 static WMArray *dropDataTypes(WMView * self)
2724 return ((Text *) self->self)->xdndSourceTypes;
2727 static WMDragOperationType wantedDropOperation(WMView * self)
2729 /* Parameter not used, but tell the compiler that it is ok */
2730 (void) self;
2732 return WDOperationCopy;
2735 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2737 /* Parameter not used, but tell the compiler that it is ok */
2738 (void) self;
2740 return (allowedOperation == WDOperationCopy);
2743 static WMData *fetchDragData(WMView * self, char *type)
2745 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2746 char *desc;
2747 WMData *data;
2749 if (strcmp(type, "text/plain")) {
2750 if (!tb)
2751 return NULL;
2753 desc = wmalloc(tb->used + 1);
2754 memcpy(desc, tb->text, tb->used);
2755 desc[tb->used] = 0;
2756 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2758 wfree(desc);
2760 return data;
2763 return NULL;
2766 static WMDragSourceProcs _DragSourceProcs = {
2767 dropDataTypes,
2768 wantedDropOperation,
2769 NULL,
2770 acceptDropOperation,
2771 NULL,
2772 NULL,
2773 fetchDragData
2776 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2778 /* Parameter not used, but tell the compiler that it is ok */
2779 (void) request;
2780 (void) sourceDataTypes;
2782 return ((Text *) self->self)->xdndDestinationTypes;
2785 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2787 /* Parameter not used, but tell the compiler that it is ok */
2788 (void) self;
2789 (void) request;
2790 (void) sourceDataTypes;
2792 return WDOperationCopy;
2795 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2797 WMText *tPtr = (WMText *) self->self;
2798 WMData *data;
2799 char *colorName;
2800 WMColor *color;
2802 /* Parameter not used, but tell the compiler that it is ok */
2803 (void) operations;
2804 (void) dropLocation;
2806 if (tPtr) {
2808 /* only one required type, implies only one drop data */
2810 /* get application/X-color if any */
2811 data = (WMData *) WMPopFromArray(dropData);
2812 if (data != NULL) {
2813 colorName = (char *)WMDataBytes(data);
2814 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2816 if (color) {
2817 WMSetTextSelectionColor(tPtr, color);
2818 WMReleaseColor(color);
2824 static WMDragDestinationProcs _DragDestinationProcs = {
2825 NULL,
2826 requiredDataTypes,
2827 allowedOperation,
2828 NULL,
2829 performDragOperation,
2830 NULL
2833 static char *getStream(WMText * tPtr, int sel, int array)
2835 TextBlock *tb = NULL;
2836 char *text = NULL;
2837 unsigned long where = 0;
2839 if (!tPtr)
2840 return NULL;
2842 if (!(tb = tPtr->firstTextBlock))
2843 return NULL;
2845 if (tPtr->writer) {
2846 (tPtr->writer) (tPtr, (void *)text);
2847 return text;
2850 tb = tPtr->firstTextBlock;
2851 while (tb) {
2853 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2855 if (!sel || (tb->graphic && tb->selected)) {
2857 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2858 && tb != tPtr->firstTextBlock) {
2859 text = wrealloc(text, where + 1);
2860 text[where++] = '\n';
2863 if (tb->blank)
2864 goto _gSnext;
2866 if (tb->graphic && array) {
2867 text = wrealloc(text, where + 4);
2868 text[where++] = 0xFA;
2869 text[where++] = (tb->used >> 8) & 0x0ff;
2870 text[where++] = tb->used & 0x0ff;
2871 text[where++] = tb->allocated; /* extra info */
2873 text = wrealloc(text, where + tb->used);
2874 memcpy(&text[where], tb->text, tb->used);
2875 where += tb->used;
2877 } else if (sel && tb->selected) {
2879 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2880 text = wrealloc(text, where + 1);
2881 text[where++] = '\n';
2884 if (tb->blank)
2885 goto _gSnext;
2887 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2888 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2889 where += tb->s_end - tb->s_begin;
2894 _gSnext: tb = tb->next;
2897 /* +1 for the end of string, let's be nice */
2898 text = wrealloc(text, where + 1);
2899 text[where] = 0;
2900 return text;
2903 static void releaseStreamObjects(void *data)
2905 if (data)
2906 wfree(data);
2909 static WMArray *getStreamObjects(WMText * tPtr, int sel)
2911 WMArray *array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2912 WMData *data;
2913 char *stream;
2914 unsigned short len;
2915 char *start, *fa, *desc;
2917 stream = getStream(tPtr, sel, 1);
2918 if (!stream)
2919 return NULL;
2921 start = stream;
2922 while (start) {
2924 fa = strchr(start, 0xFA);
2925 if (fa) {
2926 if ((int)(fa - start) > 0) {
2927 desc = start;
2928 desc[(int)(fa - start)] = 0;
2929 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2930 WMSetDataFormat(data, TYPETEXT);
2931 WMAddToArray(array, (void *)data);
2934 len = *(fa + 1) * 0xff + *(fa + 2);
2935 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2936 WMSetDataFormat(data, *(fa + 3));
2937 WMAddToArray(array, (void *)data);
2938 start = fa + len + 4;
2940 } else {
2941 if (start && strlen(start)) {
2942 data = WMCreateDataWithBytes((void *)start, strlen(start));
2943 WMSetDataFormat(data, TYPETEXT);
2944 WMAddToArray(array, (void *)data);
2946 start = fa;
2950 wfree(stream);
2951 return array;
2954 #define XDND_TEXT_DATA_TYPE "text/plain"
2955 #define XDND_COLOR_DATA_TYPE "application/X-color"
2956 static WMArray *getXdndSourceTypeArray(void)
2958 WMArray *types = WMCreateArray(1);
2959 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2960 return types;
2963 static WMArray *getXdndDestinationTypeArray(void)
2965 WMArray *types = WMCreateArray(1);
2966 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2967 return types;
2970 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2972 Text *tPtr;
2973 Display *dpy;
2974 WMScreen *scr;
2975 XGCValues gcv;
2977 tPtr = wmalloc(sizeof(Text));
2978 tPtr->widgetClass = WC_Text;
2979 tPtr->view = W_CreateView(W_VIEW(parent));
2980 if (!tPtr->view) {
2981 perror("could not create text's view\n");
2982 wfree(tPtr);
2983 return NULL;
2986 dpy = tPtr->view->screen->display;
2987 scr = tPtr->view->screen;
2989 tPtr->view->self = tPtr;
2990 tPtr->view->attribs.cursor = scr->textCursor;
2991 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2992 W_ResizeView(tPtr->view, 250, 200);
2994 tPtr->dColor = WMBlackColor(scr);
2995 tPtr->fgColor = WMBlackColor(scr);
2996 tPtr->bgColor = WMWhiteColor(scr);
2997 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2999 gcv.graphics_exposures = False;
3000 gcv.foreground = W_PIXEL(scr->gray);
3001 gcv.background = W_PIXEL(scr->darkGray);
3002 gcv.fill_style = FillStippled;
3003 /* why not use scr->stipple here? */
3004 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
3005 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
3006 GCForeground | GCBackground | GCStipple
3007 | GCFillStyle | GCGraphicsExposures, &gcv);
3009 tPtr->ruler = NULL;
3010 tPtr->vS = NULL;
3011 tPtr->hS = NULL;
3013 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3015 tPtr->view->delegate = &_TextViewDelegate;
3017 tPtr->delegate = NULL;
3019 #if DO_BLINK
3020 tPtr->timerID = NULL;
3021 #endif
3023 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
3024 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
3026 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
3027 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
3029 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
3031 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3032 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3035 WMArray *types = WMCreateArray(2);
3036 WMAddToArray(types, "application/X-color");
3037 WMAddToArray(types, "application/X-image");
3038 WMRegisterViewForDraggedTypes(tPtr->view, 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 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3339 TextBlock *tb = (TextBlock *) vtb;
3341 if (!tb)
3342 return;
3344 if (tb->graphic) {
3345 if (tb->object) {
3346 WMWidget *w = tb->d.widget;
3347 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3348 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3349 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3352 WMAddToArray(tPtr->gfxItems, (void *)tb);
3353 tPtr->tpos = 1;
3355 } else {
3356 tPtr->tpos = tb->used;
3359 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3360 tb->next = tb->prior = NULL;
3361 tb->first = True;
3362 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3363 return;
3366 if (!tb->first) {
3367 tb->marginN = tPtr->currentTextBlock->marginN;
3370 tb->next = tPtr->currentTextBlock;
3371 tb->prior = tPtr->currentTextBlock->prior;
3372 if (tPtr->currentTextBlock->prior)
3373 tPtr->currentTextBlock->prior->next = tb;
3375 tPtr->currentTextBlock->prior = tb;
3376 if (!tb->prior)
3377 tPtr->firstTextBlock = tb;
3379 tPtr->currentTextBlock = tb;
3382 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3384 TextBlock *tb = (TextBlock *) vtb;
3386 if (!tb)
3387 return;
3389 if (tb->graphic) {
3390 if (tb->object) {
3391 WMWidget *w = tb->d.widget;
3392 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3393 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3394 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3397 WMAddToArray(tPtr->gfxItems, (void *)tb);
3398 tPtr->tpos = 1;
3400 } else {
3401 tPtr->tpos = tb->used;
3404 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3405 tb->next = tb->prior = NULL;
3406 tb->first = True;
3407 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3408 return;
3411 if (!tb->first) {
3412 tb->marginN = tPtr->currentTextBlock->marginN;
3415 tb->next = tPtr->currentTextBlock->next;
3416 tb->prior = tPtr->currentTextBlock;
3417 if (tPtr->currentTextBlock->next)
3418 tPtr->currentTextBlock->next->prior = tb;
3420 tPtr->currentTextBlock->next = tb;
3422 if (!tb->next)
3423 tPtr->lastTextBlock = tb;
3425 tPtr->currentTextBlock = tb;
3428 void *WMRemoveTextBlock(WMText * tPtr)
3430 TextBlock *tb = NULL;
3432 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3433 return NULL;
3436 tb = tPtr->currentTextBlock;
3437 if (tb->graphic) {
3438 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3440 if (tb->object) {
3441 WMUnmapWidget(tb->d.widget);
3445 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3446 if (tPtr->currentTextBlock->next)
3447 tPtr->currentTextBlock->next->prior = NULL;
3449 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3450 tPtr->currentTextBlock = tPtr->firstTextBlock;
3452 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3453 tPtr->currentTextBlock->prior->next = NULL;
3454 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3455 tPtr->currentTextBlock = tPtr->lastTextBlock;
3456 } else {
3457 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3458 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3459 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3462 return (void *)tb;
3465 #if 0
3466 static void destroyWidget(WMWidget * widget)
3468 WMDestroyWidget(widget);
3469 // -- never do this -- wfree(widget);
3471 #endif
3473 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3475 TextBlock *tb = (TextBlock *) vtb;
3477 /* Parameter not used, but tell the compiler that it is ok */
3478 (void) tPtr;
3480 if (!tb)
3481 return;
3483 if (tb->graphic) {
3484 if (tb->object) {
3485 /* naturally, there's a danger to destroying widgets whose action
3486 * brings us here: ie. press a button to destroy it...
3487 * need to find a safer way. till then... this stays commented out */
3488 /* 5 months later... destroy it 10 seconds after now which should
3489 * be enough time for the widget's action to be completed... :-) */
3490 /* This is a bad assumption. Just destroy the widget here.
3491 * if the caller needs it, it can protect it with W_RetainView()
3492 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3493 WMDestroyWidget(tb->d.widget);
3494 } else {
3495 WMReleasePixmap(tb->d.pixmap);
3497 } else {
3498 WMReleaseFont(tb->d.font);
3501 WMReleaseColor(tb->color);
3502 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3503 if (tb->sections)
3504 wfree(tb->sections);
3505 wfree(tb->text);
3506 wfree(tb);
3509 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3511 if (tPtr->fgColor)
3512 WMReleaseColor(tPtr->fgColor);
3514 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3516 paintText(tPtr);
3519 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3521 if (tPtr->bgColor)
3522 WMReleaseColor(tPtr->bgColor);
3524 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3525 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3527 paintText(tPtr);
3530 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3532 if (tPtr->bgPixmap)
3533 WMReleasePixmap(tPtr->bgPixmap);
3535 if (pixmap)
3536 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3537 else
3538 tPtr->bgPixmap = NULL;
3541 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3543 tPtr->flags.relief = relief;
3544 textDidResize(tPtr->view->delegate, tPtr->view);
3547 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3549 if (shouldhave && !tPtr->hS) {
3550 tPtr->hS = WMCreateScroller(tPtr);
3551 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3552 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3553 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3554 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3555 WMMapWidget(tPtr->hS);
3556 } else if (!shouldhave && tPtr->hS) {
3557 WMUnmapWidget(tPtr->hS);
3558 WMDestroyWidget(tPtr->hS);
3559 tPtr->hS = NULL;
3562 tPtr->hpos = 0;
3563 tPtr->prevHpos = 0;
3564 textDidResize(tPtr->view->delegate, tPtr->view);
3567 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3569 if (shouldhave && !tPtr->ruler) {
3570 tPtr->ruler = WMCreateRuler(tPtr);
3571 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3572 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3573 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3574 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3575 } else if (!shouldhave && tPtr->ruler) {
3576 WMShowTextRuler(tPtr, False);
3577 WMDestroyWidget(tPtr->ruler);
3578 tPtr->ruler = NULL;
3580 textDidResize(tPtr->view->delegate, tPtr->view);
3583 void WMShowTextRuler(WMText * tPtr, Bool show)
3585 if (!tPtr->ruler)
3586 return;
3588 if (tPtr->flags.monoFont)
3589 show = False;
3591 tPtr->flags.rulerShown = show;
3592 if (show) {
3593 WMMapWidget(tPtr->ruler);
3594 } else {
3595 WMUnmapWidget(tPtr->ruler);
3598 textDidResize(tPtr->view->delegate, tPtr->view);
3601 Bool WMGetTextRulerShown(WMText * tPtr)
3603 if (!tPtr->ruler)
3604 return False;
3606 return tPtr->flags.rulerShown;
3609 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3611 if (shouldhave && !tPtr->vS) {
3612 tPtr->vS = WMCreateScroller(tPtr);
3613 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3614 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3615 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3616 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3617 WMMapWidget(tPtr->vS);
3618 } else if (!shouldhave && tPtr->vS) {
3619 WMUnmapWidget(tPtr->vS);
3620 WMDestroyWidget(tPtr->vS);
3621 tPtr->vS = NULL;
3624 tPtr->vpos = 0;
3625 tPtr->prevVpos = 0;
3626 textDidResize(tPtr->view->delegate, tPtr->view);
3629 Bool WMScrollText(WMText * tPtr, int amount)
3631 Bool scroll = False;
3633 if (amount == 0 || !tPtr->view->flags.realized)
3634 return False;
3636 if (amount < 0) {
3637 if (tPtr->vpos > 0) {
3638 if (tPtr->vpos > abs(amount))
3639 tPtr->vpos += amount;
3640 else
3641 tPtr->vpos = 0;
3642 scroll = True;
3644 } else {
3645 int limit = tPtr->docHeight - tPtr->visible.h;
3646 if (tPtr->vpos < limit) {
3647 if (tPtr->vpos < limit - amount)
3648 tPtr->vpos += amount;
3649 else
3650 tPtr->vpos = limit;
3651 scroll = True;
3655 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3656 updateScrollers(tPtr);
3657 paintText(tPtr);
3659 tPtr->prevVpos = tPtr->vpos;
3660 return scroll;
3663 Bool WMPageText(WMText * tPtr, Bool direction)
3665 if (!tPtr->view->flags.realized)
3666 return False;
3668 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3671 void WMSetTextEditable(WMText * tPtr, Bool editable)
3673 tPtr->flags.editable = editable;
3676 int WMGetTextEditable(WMText * tPtr)
3678 return tPtr->flags.editable;
3681 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3683 tPtr->flags.indentNewLine = indent;
3686 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3688 tPtr->flags.ignoreNewLine = ignore;
3691 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3693 return tPtr->flags.ignoreNewLine;
3696 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3698 if (mono) {
3699 if (tPtr->flags.rulerShown)
3700 WMShowTextRuler(tPtr, False);
3701 if (tPtr->flags.alignment != WALeft)
3702 tPtr->flags.alignment = WALeft;
3705 tPtr->flags.monoFont = mono;
3706 textDidResize(tPtr->view->delegate, tPtr->view);
3709 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3711 return tPtr->flags.monoFont;
3714 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3716 if (tPtr->dFont)
3717 WMReleaseFont(tPtr->dFont);
3719 if (font) {
3720 tPtr->dFont = WMRetainFont(font);
3721 } else {
3722 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3726 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3728 return WMRetainFont(tPtr->dFont);
3731 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3733 if (tPtr->dColor)
3734 WMReleaseColor(tPtr->dColor);
3736 if (color) {
3737 tPtr->dColor = WMRetainColor(color);
3738 } else {
3739 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3743 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3745 return tPtr->dColor;
3748 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3750 if (tPtr->flags.monoFont)
3751 tPtr->flags.alignment = WALeft;
3752 else
3753 tPtr->flags.alignment = alignment;
3754 WMThawText(tPtr);
3757 int WMGetTextInsertType(WMText * tPtr)
3759 return tPtr->flags.prepend;
3762 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3764 setSelectionProperty(tPtr, NULL, color, -1);
3767 WMColor *WMGetTextSelectionColor(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 return tb->color;
3782 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3784 setSelectionProperty(tPtr, font, NULL, -1);
3787 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3789 TextBlock *tb;
3791 tb = tPtr->currentTextBlock;
3793 if (!tb || !tPtr->flags.ownsSelection)
3794 return NULL;
3796 if (!tb->selected)
3797 return NULL;
3799 if (tb->graphic) {
3800 tb = getFirstNonGraphicBlockFor(tb, 1);
3801 if (!tb)
3802 return NULL;
3804 return (tb->selected ? tb->d.font : NULL);
3807 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3809 /* // check this */
3810 if (underlined != 0 && underlined != 1)
3811 return;
3813 setSelectionProperty(tPtr, NULL, NULL, underlined);
3816 int WMGetTextSelectionUnderlined(WMText * tPtr)
3818 TextBlock *tb;
3820 tb = tPtr->currentTextBlock;
3822 if (!tb || !tPtr->flags.ownsSelection)
3823 return 0;
3825 if (!tb->selected)
3826 return 0;
3828 return tb->underlined;
3831 void WMFreezeText(WMText * tPtr)
3833 tPtr->flags.frozen = True;
3836 void WMThawText(WMText * tPtr)
3838 tPtr->flags.frozen = False;
3840 if (tPtr->flags.monoFont) {
3841 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3842 TextBlock *tb;
3844 /* make sure to unmap widgets no matter where they are */
3845 /* they'll be later remapped if needed by paintText */
3846 for (j = 0; j < c; j++) {
3847 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3848 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3849 WMUnmapWidget(tb->d.widget);
3854 tPtr->flags.laidOut = False;
3855 layOutDocument(tPtr);
3856 updateScrollers(tPtr);
3857 paintText(tPtr);
3858 tPtr->flags.needsLayOut = False;
3862 /* find first occurence of a string */
3863 static const char *mystrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3865 const char *ptr;
3867 if (!haystack || !needle || !end)
3868 return NULL;
3870 for (ptr = haystack; ptr < end; ptr++) {
3871 if (caseSensitive) {
3872 if (*ptr == *needle && !strncmp(ptr, needle, len))
3873 return ptr;
3875 } else {
3876 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3877 return ptr;
3881 return NULL;
3884 /* find last occurence of a string */
3885 static const char *mystrrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3887 const char *ptr;
3889 if (!haystack || !needle || !end)
3890 return NULL;
3892 for (ptr = haystack - 2; ptr > end; ptr--) {
3893 if (caseSensitive) {
3894 if (*ptr == *needle && !strncmp(ptr, needle, len))
3895 return ptr;
3896 } else {
3897 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3898 return ptr;
3902 return NULL;
3905 Bool WMFindInTextStream(WMText * tPtr, const char *needle, Bool direction, Bool caseSensitive)
3907 TextBlock *tb;
3908 const char *mark = NULL;
3909 unsigned short pos;
3911 #if 0
3912 if (!(tb = tPtr->currentTextBlock)) {
3913 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3914 return False;
3916 } else {
3917 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3918 tb = (direction>0) ? tb->next : tb->prior; */
3919 if (tb != tPtr->lastTextBlock)
3920 tb = tb->prior;
3922 #endif
3923 tb = tPtr->currentTextBlock;
3924 pos = tPtr->tpos;
3926 while (tb) {
3927 if (!tb->graphic) {
3929 if (direction > 0) {
3930 if (pos + 1 < tb->used)
3931 pos++;
3933 if (tb->used - pos > 0 && pos > 0) {
3934 mark = mystrstr(&tb->text[pos], needle,
3935 strlen(needle), &tb->text[tb->used], caseSensitive);
3937 } else {
3938 tb = tb->next;
3939 pos = 0;
3940 continue;
3943 } else {
3944 if (pos - 1 > 0)
3945 pos--;
3947 if (pos > 0) {
3948 mark = mystrrstr(&tb->text[pos], needle,
3949 strlen(needle), tb->text, caseSensitive);
3950 } else {
3951 tb = tb->prior;
3952 if (!tb)
3953 return False;
3954 pos = tb->used;
3955 continue;
3959 if (mark) {
3960 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3962 tPtr->tpos = (int)(mark - tb->text);
3963 tPtr->currentTextBlock = tb;
3964 updateCursorPosition(tPtr);
3965 tPtr->sel.y = tPtr->cursor.y + 5;
3966 tPtr->sel.h = tPtr->cursor.h - 10;
3967 tPtr->sel.x = tPtr->cursor.x + 1;
3968 tPtr->sel.w = WMIN(WMWidthOfString(font,
3969 &tb->text[tPtr->tpos], strlen(needle)),
3970 tPtr->docWidth - tPtr->sel.x);
3971 tPtr->flags.ownsSelection = True;
3972 paintText(tPtr);
3974 return True;
3978 tb = (direction > 0) ? tb->next : tb->prior;
3979 if (tb) {
3980 pos = (direction > 0) ? 0 : tb->used;
3984 return False;
3987 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3989 if (!tPtr->flags.ownsSelection)
3990 return False;
3992 removeSelection(tPtr);
3994 if (replacement) {
3995 insertTextInteractively(tPtr, replacement, strlen(replacement));
3996 updateCursorPosition(tPtr);
3997 paintText(tPtr);
4000 return True;