wrlib: add explicit type definition in API to allow compiler Type Checks (3/3)
[wmaker-crm.git] / WINGs / wtext.c
blob734dc8bc909581b24f8434e2229087ceea041523
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 break; /* goto _doneH; */
1031 if (s < 0 || s >= tb->nsections) {
1032 s = (dir ? tb->nsections - 1 : 0);
1035 _doneV:
1036 /* we have the line, which TextBlock on that line is it? */
1037 pos = (dir ? 0 : tb->sections[s].begin);
1038 if (tPtr->flags.monoFont && tb->graphic) {
1039 TextBlock *hold = tb;
1040 tb = getFirstNonGraphicBlockFor(hold, dir);
1042 if (!tb) {
1043 tPtr->tpos = 0;
1044 tb = hold;
1045 s = 0;
1046 goto _doNothing;
1050 _y = tb->sections[s]._y;
1052 while (tb) {
1054 if (tPtr->flags.monoFont && tb->graphic) {
1055 tb = (dir ? tb->next : tb->prior);
1056 continue;
1059 if (dir) {
1060 if (tb->graphic) {
1061 if (tb->object)
1062 _w = WMWidgetWidth(tb->d.widget) - 5;
1063 else
1064 _w = tb->d.pixmap->width - 5;
1066 if (tb->sections[0].x + _w >= x)
1067 break;
1068 } else {
1069 text = &(tb->text[tb->sections[s].begin]);
1070 len = tb->sections[s].end - tb->sections[s].begin;
1071 _w = WMWidthOfString(tb->d.font, text, len);
1072 if (tb->sections[s].x + _w >= x)
1073 break;
1076 } else {
1077 if (tb->sections[s].x <= x)
1078 break;
1081 if ((dir ? tb->next : tb->prior)) {
1082 TextBlock *nxt = (dir ? tb->next : tb->prior);
1083 if (tPtr->flags.monoFont && nxt->graphic) {
1084 nxt = getFirstNonGraphicBlockFor(nxt, dir);
1085 if (!nxt) {
1086 pos = (dir ? 0 : tb->sections[s].begin);
1087 tPtr->cursor.x = tb->sections[s].x;
1088 goto _doneH;
1092 if (_y != nxt->sections[dir ? 0 : nxt->nsections - 1]._y) {
1093 /* this must be the last/first on this line. stop */
1094 pos = (dir ? tb->sections[s].end : 0);
1095 tPtr->cursor.x = tb->sections[s].x;
1096 if (!tb->blank) {
1097 if (tb->graphic) {
1098 if (tb->object)
1099 tPtr->cursor.x += WMWidgetWidth(tb->d.widget);
1100 else
1101 tPtr->cursor.x += tb->d.pixmap->width;
1102 } else if (pos > tb->sections[s].begin) {
1103 tPtr->cursor.x +=
1104 WMWidthOfString(tb->d.font,
1105 &(tb->text[tb->sections[s].begin]),
1106 pos - tb->sections[s].begin);
1109 goto _doneH;
1113 if ((dir ? tb->next : tb->prior)) {
1114 tb = (dir ? tb->next : tb->prior);
1115 } else {
1116 break;
1119 if (tb)
1120 s = (dir ? 0 : tb->nsections - 1);
1123 /* we have said TextBlock, now where within it? */
1124 if (tb) {
1125 if (tb->graphic) {
1126 int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);
1128 tPtr->cursor.x = tb->sections[0].x;
1130 if (x > tPtr->cursor.x + gw / 2) {
1131 pos = 1;
1132 tPtr->cursor.x += gw;
1133 } else {
1134 printf("first %d\n", tb->first);
1135 if (tb->prior) {
1136 if (tb->prior->graphic)
1137 pos = 1;
1138 else
1139 pos = tb->prior->used;
1140 tb = tb->prior;
1141 } else
1142 pos = 0;
1146 s = 0;
1147 goto _doneH;
1149 } else {
1150 WMFont *f = tb->d.font;
1151 len = tb->sections[s].end - tb->sections[s].begin;
1152 text = &(tb->text[tb->sections[s].begin]);
1154 _w = x - tb->sections[s].x;
1155 pos = 0;
1157 while (pos < len && WMWidthOfString(f, text, pos + 1) < _w)
1158 pos++;
1160 tPtr->cursor.x = tb->sections[s].x + (pos ? WMWidthOfString(f, text, pos) : 0);
1162 pos += tb->sections[s].begin;
1166 _doneH:
1167 if (tb->graphic) {
1168 tPtr->tpos = (pos <= 1) ? pos : 0;
1169 } else {
1170 tPtr->tpos = (pos < tb->used) ? pos : tb->used;
1172 _doNothing:
1173 if (!tb)
1174 printf("...for this app will surely crash :-)\n");
1176 tPtr->currentTextBlock = tb;
1177 tPtr->cursor.h = tb->sections[s].h;
1178 tPtr->cursor.y = tb->sections[s]._y;
1180 /* scroll the bars if the cursor is not visible */
1181 if (tPtr->flags.editable && tPtr->cursor.x != -23) {
1182 if (tPtr->cursor.y + tPtr->cursor.h > tPtr->vpos + tPtr->visible.y + tPtr->visible.h) {
1183 tPtr->vpos +=
1184 (tPtr->cursor.y + tPtr->cursor.h + 10
1185 - (tPtr->vpos + tPtr->visible.y + tPtr->visible.h));
1186 updateScrollers(tPtr);
1187 } else if (tPtr->cursor.y < tPtr->vpos + tPtr->visible.y) {
1188 tPtr->vpos -= (tPtr->vpos + tPtr->visible.y - tPtr->cursor.y);
1189 updateScrollers(tPtr);
1196 static void updateScrollers(Text * tPtr)
1199 if (tPtr->flags.frozen)
1200 return;
1202 if (tPtr->vS) {
1203 if (tPtr->docHeight <= tPtr->visible.h) {
1204 WMSetScrollerParameters(tPtr->vS, 0, 1);
1205 tPtr->vpos = 0;
1206 } else {
1207 float hmax = (float)(tPtr->docHeight);
1208 WMSetScrollerParameters(tPtr->vS,
1209 ((float)tPtr->vpos) / (hmax - (float)tPtr->visible.h),
1210 (float)tPtr->visible.h / hmax);
1212 } else
1213 tPtr->vpos = 0;
1215 if (tPtr->hS) {
1216 if (tPtr->docWidth <= tPtr->visible.w) {
1217 WMSetScrollerParameters(tPtr->hS, 0, 1);
1218 tPtr->hpos = 0;
1219 } else {
1220 float wmax = (float)(tPtr->docWidth);
1221 WMSetScrollerParameters(tPtr->hS,
1222 ((float)tPtr->hpos) / (wmax - (float)tPtr->visible.w),
1223 (float)tPtr->visible.w / wmax);
1225 } else
1226 tPtr->hpos = 0;
1229 static void scrollersCallBack(WMWidget * w, void *self)
1231 Text *tPtr = (Text *) self;
1232 Bool scroll = False;
1233 int which;
1235 if (!tPtr->view->flags.realized || tPtr->flags.frozen)
1236 return;
1238 if (w == tPtr->vS) {
1239 int height;
1240 height = tPtr->visible.h;
1242 which = WMGetScrollerHitPart(tPtr->vS);
1243 switch (which) {
1245 case WSDecrementLine:
1246 if (tPtr->vpos > 0) {
1247 if (tPtr->vpos > 16)
1248 tPtr->vpos -= 16;
1249 else
1250 tPtr->vpos = 0;
1252 break;
1254 case WSIncrementLine:{
1255 int limit = tPtr->docHeight - height;
1256 if (tPtr->vpos < limit) {
1257 if (tPtr->vpos < limit - 16)
1258 tPtr->vpos += 16;
1259 else
1260 tPtr->vpos = limit;
1263 break;
1265 case WSDecrementPage:
1266 if (((int)tPtr->vpos - (int)height) >= 0)
1267 tPtr->vpos -= height;
1268 else
1269 tPtr->vpos = 0;
1270 break;
1272 case WSIncrementPage:
1273 tPtr->vpos += height;
1274 if (tPtr->vpos > (tPtr->docHeight - height))
1275 tPtr->vpos = tPtr->docHeight - height;
1276 break;
1278 case WSKnob:
1279 tPtr->vpos = WMGetScrollerValue(tPtr->vS)
1280 * (float)(tPtr->docHeight - height);
1281 break;
1283 case WSKnobSlot:
1284 case WSNoPart:
1285 break;
1287 scroll = (tPtr->vpos != tPtr->prevVpos);
1288 tPtr->prevVpos = tPtr->vpos;
1291 if (w == tPtr->hS) {
1292 int width = tPtr->visible.w;
1294 which = WMGetScrollerHitPart(tPtr->hS);
1295 switch (which) {
1297 case WSDecrementLine:
1298 if (tPtr->hpos > 0) {
1299 if (tPtr->hpos > 16)
1300 tPtr->hpos -= 16;
1301 else
1302 tPtr->hpos = 0;
1304 break;
1306 case WSIncrementLine:{
1307 int limit = tPtr->docWidth - width;
1308 if (tPtr->hpos < limit) {
1309 if (tPtr->hpos < limit - 16)
1310 tPtr->hpos += 16;
1311 else
1312 tPtr->hpos = limit;
1315 break;
1317 case WSDecrementPage:
1318 if (((int)tPtr->hpos - (int)width) >= 0)
1319 tPtr->hpos -= width;
1320 else
1321 tPtr->hpos = 0;
1322 break;
1324 case WSIncrementPage:
1325 tPtr->hpos += width;
1326 if (tPtr->hpos > (tPtr->docWidth - width))
1327 tPtr->hpos = tPtr->docWidth - width;
1328 break;
1330 case WSKnob:
1331 tPtr->hpos = WMGetScrollerValue(tPtr->hS)
1332 * (float)(tPtr->docWidth - width);
1333 break;
1335 case WSKnobSlot:
1336 case WSNoPart:
1337 break;
1339 scroll = (tPtr->hpos != tPtr->prevHpos);
1340 tPtr->prevHpos = tPtr->hpos;
1343 if (scroll) {
1344 updateScrollers(tPtr);
1345 paintText(tPtr);
1349 typedef struct {
1350 TextBlock *tb;
1351 unsigned short begin, end; /* what part of the text block */
1352 } myLineItems;
1354 static int layOutLine(Text * tPtr, myLineItems * items, int nitems, int x, int y)
1356 int i, j = 0, lw = 0, line_height = 0, max_d = 0, len, n;
1357 WMFont *font;
1358 const char *text;
1359 TextBlock *tb, *tbsame = NULL;
1361 if (!items || nitems == 0)
1362 return 0;
1364 for (i = 0; i < nitems; i++) {
1365 tb = items[i].tb;
1367 if (tb->graphic) {
1368 if (!tPtr->flags.monoFont) {
1369 if (tb->object) {
1370 WMWidget *wdt = tb->d.widget;
1371 line_height = WMAX(line_height, WMWidgetHeight(wdt));
1372 if (tPtr->flags.alignment != WALeft)
1373 lw += WMWidgetWidth(wdt);
1374 } else {
1375 line_height = WMAX(line_height, tb->d.pixmap->height + max_d);
1376 if (tPtr->flags.alignment != WALeft)
1377 lw += tb->d.pixmap->width;
1381 } else {
1382 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1383 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1384 max_d = 2;
1385 line_height = WMAX(line_height, font->height + max_d);
1386 text = &(tb->text[items[i].begin]);
1387 len = items[i].end - items[i].begin;
1388 if (tPtr->flags.alignment != WALeft)
1389 lw += WMWidthOfString(font, text, len);
1393 if (tPtr->flags.alignment == WARight) {
1394 j = tPtr->visible.w - lw;
1395 } else if (tPtr->flags.alignment == WACenter) {
1396 j = (int)((float)(tPtr->visible.w - lw)) / 2.0;
1399 for (i = 0; i < nitems; i++) {
1400 tb = items[i].tb;
1402 if (tbsame == tb) { /* extend it, since it's on same line */
1403 tb->sections[tb->nsections - 1].end = items[i].end;
1404 n = tb->nsections - 1;
1405 } else {
1406 tb->sections = wrealloc(tb->sections, (++tb->nsections) * sizeof(Section));
1407 n = tb->nsections - 1;
1408 tb->sections[n]._y = y + max_d;
1409 tb->sections[n].max_d = max_d;
1410 tb->sections[n].x = x + j;
1411 tb->sections[n].h = line_height;
1412 tb->sections[n].begin = items[i].begin;
1413 tb->sections[n].end = items[i].end;
1416 tb->sections[n].last = (i + 1 == nitems);
1418 if (tb->graphic) {
1419 if (!tPtr->flags.monoFont) {
1420 if (tb->object) {
1421 WMWidget *wdt = tb->d.widget;
1422 tb->sections[n].y = max_d + y + line_height - WMWidgetHeight(wdt);
1423 tb->sections[n].w = WMWidgetWidth(wdt);
1424 } else {
1425 tb->sections[n].y = y + line_height + max_d - tb->d.pixmap->height;
1426 tb->sections[n].w = tb->d.pixmap->width;
1428 x += tb->sections[n].w;
1430 } else {
1431 font = (tPtr->flags.monoFont) ? tPtr->dFont : tb->d.font;
1432 len = items[i].end - items[i].begin;
1433 text = &(tb->text[items[i].begin]);
1435 tb->sections[n].y = y + line_height - font->y;
1436 tb->sections[n].w =
1437 WMWidthOfString(font,
1438 &(tb->text[tb->sections[n].begin]),
1439 tb->sections[n].end - tb->sections[n].begin);
1441 x += WMWidthOfString(font, text, len);
1444 tbsame = tb;
1447 return line_height;
1451 static void layOutDocument(Text * tPtr)
1453 TextBlock *tb;
1454 myLineItems *items = NULL;
1455 unsigned int itemsSize = 0, nitems = 0, begin, end;
1456 WMFont *font;
1457 unsigned int x, y = 0, lw = 0, width = 0, bmargin;
1458 const char *start = NULL, *mark = NULL;
1460 if (tPtr->flags.frozen || (!(tb = tPtr->firstTextBlock)))
1461 return;
1463 assert(tPtr->visible.w > 20);
1465 tPtr->docWidth = tPtr->visible.w;
1466 x = tPtr->margins[tb->marginN].first;
1467 bmargin = tPtr->margins[tb->marginN].body;
1469 /* only partial layOut needed: re-Lay only affected textblocks */
1470 if (tPtr->flags.laidOut) {
1471 tb = tPtr->currentTextBlock;
1473 /* search backwards for textblocks on same line */
1474 while (tb->prior) {
1475 if (!tb->sections || tb->nsections < 1) {
1476 tb = tPtr->firstTextBlock;
1477 tPtr->flags.laidOut = False;
1478 y = 0;
1479 goto _layOut;
1482 if (!tb->prior->sections || tb->prior->nsections < 1) {
1483 tb = tPtr->firstTextBlock;
1484 tPtr->flags.laidOut = False;
1485 y = 0;
1486 goto _layOut;
1489 if (tb->sections[0]._y != tb->prior->sections[tb->prior->nsections - 1]._y) {
1490 break;
1492 tb = tb->prior;
1495 if (tb->prior && tb->prior->sections && tb->prior->nsections > 0) {
1496 y = tb->prior->sections[tb->prior->nsections - 1]._y +
1497 tb->prior->sections[tb->prior->nsections - 1].h -
1498 tb->prior->sections[tb->prior->nsections - 1].max_d;
1499 } else {
1500 y = 0;
1504 _layOut:
1505 while (tb) {
1507 if (tb->sections && tb->nsections > 0) {
1508 wfree(tb->sections);
1509 tb->sections = NULL;
1510 tb->nsections = 0;
1513 if (tb->first && tb->blank && tb->next && !tb->next->first) {
1514 TextBlock *next = tb->next;
1515 tPtr->currentTextBlock = tb;
1516 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1517 tb = next;
1518 tb->first = True;
1519 continue;
1522 if (tb->first && tb != tPtr->firstTextBlock) {
1523 y += layOutLine(tPtr, items, nitems, x, y);
1524 x = tPtr->margins[tb->marginN].first;
1525 bmargin = tPtr->margins[tb->marginN].body;
1526 nitems = 0;
1527 lw = 0;
1530 if (tb->graphic) {
1531 if (!tPtr->flags.monoFont) {
1532 if (tb->object)
1533 width = WMWidgetWidth(tb->d.widget);
1534 else
1535 width = tb->d.pixmap->width;
1537 if (width > tPtr->docWidth)
1538 tPtr->docWidth = width;
1540 lw += width;
1541 if (lw >= tPtr->visible.w - x) {
1542 y += layOutLine(tPtr, items, nitems, x, y);
1543 nitems = 0;
1544 x = bmargin;
1545 lw = width;
1548 if (nitems + 1 > itemsSize) {
1549 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1552 items[nitems].tb = tb;
1553 items[nitems].begin = 0;
1554 items[nitems].end = 0;
1555 nitems++;
1558 } else if ((start = tb->text)) {
1559 begin = end = 0;
1560 font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
1562 while (start) {
1563 mark = strchr(start, ' ');
1564 if (mark) {
1565 end += (int)(mark - start) + 1;
1566 start = mark + 1;
1567 } else {
1568 end += strlen(start);
1569 start = mark;
1572 if (end > tb->used)
1573 end = tb->used;
1575 if (end - begin > 0) {
1577 width = WMWidthOfString(font, &tb->text[begin], end - begin);
1579 /* if it won't fit, char wrap it */
1580 if (width >= tPtr->visible.w) {
1581 char *t = &tb->text[begin];
1582 int l = end - begin, i = 0;
1583 do {
1584 width = WMWidthOfString(font, t, ++i);
1585 } while (width < tPtr->visible.w && i < l);
1586 if (i > 2)
1587 i--;
1588 end = begin + i;
1589 start = &tb->text[end];
1592 lw += width;
1595 if (lw >= tPtr->visible.w - x) {
1596 y += layOutLine(tPtr, items, nitems, x, y);
1597 lw = width;
1598 x = bmargin;
1599 nitems = 0;
1602 if (nitems + 1 > itemsSize) {
1603 items = wrealloc(items, (++itemsSize) * sizeof(myLineItems));
1606 items[nitems].tb = tb;
1607 items[nitems].begin = begin;
1608 items[nitems].end = end;
1609 nitems++;
1611 begin = end;
1615 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1616 if (0 && tPtr->flags.laidOut
1617 && tb->next && tb->next->sections && tb->next->nsections > 0
1618 && (tPtr->vpos + tPtr->visible.h < tb->next->sections[0]._y)) {
1619 if (tPtr->lastTextBlock->sections && tPtr->lastTextBlock->nsections > 0) {
1620 TextBlock *ltb = tPtr->lastTextBlock;
1621 int ly = ltb->sections[ltb->nsections - 1]._y;
1622 int lh = ltb->sections[ltb->nsections - 1].h;
1623 int ss, sd;
1625 lh += 1 + tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d;
1626 printf("it's %d\n", tPtr->visible.y + ltb->sections[ltb->nsections - 1].max_d);
1628 y += layOutLine(tPtr, items, nitems, x, y);
1629 ss = ly + lh - y;
1630 sd = tPtr->docHeight - y;
1632 printf("dif %d-%d: %d\n", ss, sd, ss - sd);
1633 y += tb->next->sections[0]._y - y;
1634 nitems = 0;
1635 printf("nitems%d\n", nitems);
1636 if (ss - sd != 0)
1637 y = tPtr->docHeight + ss - sd;
1639 break;
1640 } else {
1641 tPtr->flags.laidOut = False;
1645 tb = tb->next;
1648 if (nitems > 0)
1649 y += layOutLine(tPtr, items, nitems, x, y);
1651 if (tPtr->docHeight != y + 10) {
1652 tPtr->docHeight = y + 10;
1653 updateScrollers(tPtr);
1656 if (tPtr->docWidth > tPtr->visible.w && !tPtr->hS) {
1657 XEvent event;
1659 tPtr->flags.horizOnDemand = True;
1660 WMSetTextHasHorizontalScroller((WMText *) tPtr, True);
1661 event.type = Expose;
1662 handleEvents(&event, (void *)tPtr);
1664 } else if (tPtr->docWidth <= tPtr->visible.w && tPtr->hS && tPtr->flags.horizOnDemand) {
1665 tPtr->flags.horizOnDemand = False;
1666 WMSetTextHasHorizontalScroller((WMText *) tPtr, False);
1669 tPtr->flags.laidOut = True;
1671 if (items && itemsSize > 0)
1672 wfree(items);
1676 static void textDidResize(W_ViewDelegate * self, WMView * view)
1678 Text *tPtr = (Text *) view->self;
1679 unsigned short w = tPtr->view->size.width;
1680 unsigned short h = tPtr->view->size.height;
1681 unsigned short rh = 0, vw = 0, rel;
1683 /* Parameter not used, but tell the compiler that it is ok */
1684 (void) self;
1686 rel = (tPtr->flags.relief == WRFlat);
1688 if (tPtr->ruler && tPtr->flags.rulerShown) {
1689 WMMoveWidget(tPtr->ruler, 2, 2);
1690 WMResizeWidget(tPtr->ruler, w - 4, 40);
1691 rh = 40;
1694 if (tPtr->vS) {
1695 WMMoveWidget(tPtr->vS, 1 - (rel ? 1 : 0), rh + 1 - (rel ? 1 : 0));
1696 WMResizeWidget(tPtr->vS, 20, h - rh - 2 + (rel ? 2 : 0));
1697 vw = 20;
1698 WMSetRulerOffset(tPtr->ruler, 22);
1699 } else
1700 WMSetRulerOffset(tPtr->ruler, 2);
1702 if (tPtr->hS) {
1703 if (tPtr->vS) {
1704 WMMoveWidget(tPtr->hS, vw, h - 21);
1705 WMResizeWidget(tPtr->hS, w - vw - 1, 20);
1706 } else {
1707 WMMoveWidget(tPtr->hS, vw + 1, h - 21);
1708 WMResizeWidget(tPtr->hS, w - vw - 2, 20);
1712 tPtr->visible.x = (tPtr->vS) ? 24 : 4;
1713 tPtr->visible.y = (tPtr->ruler && tPtr->flags.rulerShown) ? 43 : 3;
1714 tPtr->visible.w = tPtr->view->size.width - tPtr->visible.x - 8;
1715 tPtr->visible.h = tPtr->view->size.height - tPtr->visible.y;
1716 tPtr->visible.h -= (tPtr->hS) ? 20 : 0;
1717 tPtr->margins[0].right = tPtr->visible.w;
1719 if (tPtr->view->flags.realized) {
1721 if (tPtr->db) {
1722 XFreePixmap(tPtr->view->screen->display, tPtr->db);
1723 tPtr->db = (Pixmap) NULL;
1726 if (tPtr->visible.w < 40)
1727 tPtr->visible.w = 40;
1728 if (tPtr->visible.h < 20)
1729 tPtr->visible.h = 20;
1731 if (!tPtr->db) {
1732 tPtr->db = XCreatePixmap(tPtr->view->screen->display,
1733 tPtr->view->window, tPtr->visible.w,
1734 tPtr->visible.h, tPtr->view->screen->depth);
1738 WMThawText(tPtr);
1741 W_ViewDelegate _TextViewDelegate = {
1742 NULL,
1743 NULL,
1744 textDidResize,
1745 NULL,
1746 NULL
1749 #define TEXT_BUFFER_INCR 8
1750 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1752 static void clearText(Text * tPtr)
1754 tPtr->vpos = tPtr->hpos = 0;
1755 tPtr->docHeight = tPtr->docWidth = 0;
1756 tPtr->cursor.x = -23;
1758 if (!tPtr->firstTextBlock)
1759 return;
1761 while (tPtr->currentTextBlock)
1762 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1764 tPtr->firstTextBlock = NULL;
1765 tPtr->currentTextBlock = NULL;
1766 tPtr->lastTextBlock = NULL;
1767 WMEmptyArray(tPtr->gfxItems);
1770 /* possibly remove a single character from the currentTextBlock,
1771 or if there's a selection, remove it...
1772 note that Delete and Backspace are treated differently */
1773 static void deleteTextInteractively(Text * tPtr, KeySym ksym)
1775 TextBlock *tb;
1776 Bool back = (Bool) (ksym == XK_BackSpace);
1777 Bool done = 1, wasFirst = 0;
1779 if (!tPtr->flags.editable)
1780 return;
1782 if (!(tb = tPtr->currentTextBlock))
1783 return;
1785 if (tPtr->flags.ownsSelection) {
1786 if (removeSelection(tPtr))
1787 layOutDocument(tPtr);
1788 return;
1791 wasFirst = tb->first;
1792 if (back && tPtr->tpos < 1) {
1793 if (tb->prior) {
1794 if (tb->prior->blank) {
1795 tPtr->currentTextBlock = tb->prior;
1796 WMRemoveTextBlock(tPtr);
1797 tPtr->currentTextBlock = tb;
1798 tb->first = True;
1799 layOutDocument(tPtr);
1800 return;
1801 } else {
1802 if (tb->blank) {
1803 TextBlock *prior = tb->prior;
1804 tPtr->currentTextBlock = tb;
1805 WMRemoveTextBlock(tPtr);
1806 tb = prior;
1807 } else {
1808 tb = tb->prior;
1811 if (tb->graphic)
1812 tPtr->tpos = 1;
1813 else
1814 tPtr->tpos = tb->used;
1816 tPtr->currentTextBlock = tb;
1817 done = 1;
1818 if (wasFirst) {
1819 if (tb->next)
1820 tb->next->first = False;
1821 layOutDocument(tPtr);
1822 return;
1828 if ((tb->used > 0) && ((back ? tPtr->tpos > 0 : 1))
1829 && (tPtr->tpos <= tb->used) && !tb->graphic) {
1830 if (back)
1831 tPtr->tpos--;
1832 memmove(&(tb->text[tPtr->tpos]), &(tb->text[tPtr->tpos + 1]), tb->used - tPtr->tpos);
1833 tb->used--;
1834 done = 0;
1837 /* if there are no characters left to back over in the textblock,
1838 but it still has characters to the right of the cursor: */
1839 if ((back ? (tPtr->tpos == 0 && !done) : (tPtr->tpos >= tb->used))
1840 || tb->graphic) {
1842 /* no more chars, and it's marked as blank? */
1843 if (tb->blank) {
1844 TextBlock *sibling = (back ? tb->prior : tb->next);
1846 if (tb->used == 0 || tb->graphic)
1847 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1849 if (sibling) {
1850 tPtr->currentTextBlock = sibling;
1851 if (tb->graphic)
1852 tPtr->tpos = (back ? 1 : 0);
1853 else
1854 tPtr->tpos = (back ? sibling->used : 0);
1856 /* no more chars, so mark it as blank */
1857 } else if (tb->used == 0) {
1858 tb->blank = 1;
1859 } else if (tb->graphic) {
1860 Bool hasNext = (tb->next != NULL);
1862 WMDestroyTextBlock(tPtr, WMRemoveTextBlock(tPtr));
1863 if (hasNext) {
1864 tPtr->tpos = 0;
1865 } else if (tPtr->currentTextBlock) {
1866 tPtr->tpos = (tPtr->currentTextBlock->graphic ? 1 : tPtr->currentTextBlock->used);
1868 } else
1869 printf("DEBUG: unaccounted for... catch this!\n");
1872 layOutDocument(tPtr);
1875 static void insertTextInteractively(Text * tPtr, char *text, int len)
1877 TextBlock *tb;
1878 char *newline = NULL;
1880 if (!tPtr->flags.editable) {
1881 return;
1884 if (len < 1 || !text)
1885 return;
1887 if (tPtr->flags.ignoreNewLine && *text == '\n' && len == 1)
1888 return;
1890 if (tPtr->flags.ownsSelection)
1891 removeSelection(tPtr);
1893 if (tPtr->flags.ignoreNewLine) {
1894 int i;
1895 for (i = 0; i < len; i++) {
1896 if (text[i] == '\n')
1897 text[i] = ' ';
1901 tb = tPtr->currentTextBlock;
1902 if (!tb || tb->graphic) {
1903 tPtr->tpos = 0;
1904 WMAppendTextStream(tPtr, text);
1905 layOutDocument(tPtr);
1906 return;
1909 if ((newline = strchr(text, '\n'))) {
1910 int nlen = (int)(newline - text);
1911 int s = tb->used - tPtr->tpos;
1913 if (!tb->blank && nlen > 0) {
1914 char *save = NULL;
1916 if (s > 0) {
1917 save = wmalloc(s);
1918 memcpy(save, &tb->text[tPtr->tpos], s);
1919 tb->used -= (tb->used - tPtr->tpos);
1921 insertTextInteractively(tPtr, text, nlen);
1922 newline++;
1923 WMAppendTextStream(tPtr, newline);
1924 if (s > 0) {
1925 insertTextInteractively(tPtr, save, s);
1926 wfree(save);
1928 } else {
1929 if (tPtr->tpos > 0 && tPtr->tpos < tb->used && !tb->graphic && tb->text) {
1931 unsigned short savePos = tPtr->tpos;
1932 void *ntb = WMCreateTextBlockWithText(tPtr, &tb->text[tPtr->tpos],
1933 tb->d.font, tb->color, True,
1934 tb->used - tPtr->tpos);
1936 if (tb->sections[0].end == tPtr->tpos)
1937 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1938 NULL, tb->d.font,
1939 tb->color, True, 0));
1941 tb->used = savePos;
1942 WMAppendTextBlock(tPtr, ntb);
1943 tPtr->tpos = 0;
1945 } else if (tPtr->tpos == tb->used) {
1946 if (tPtr->flags.indentNewLine) {
1947 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1948 " ", tb->d.font,
1949 tb->color, True, 4));
1950 tPtr->tpos = 4;
1951 } else {
1952 WMAppendTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1953 NULL, tb->d.font,
1954 tb->color, True, 0));
1955 tPtr->tpos = 0;
1957 } else if (tPtr->tpos == 0) {
1958 if (tPtr->flags.indentNewLine) {
1959 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1960 " ", tb->d.font,
1961 tb->color, True, 4));
1962 } else {
1963 WMPrependTextBlock(tPtr, WMCreateTextBlockWithText(tPtr,
1964 NULL, tb->d.font,
1965 tb->color, True, 0));
1967 tPtr->tpos = 0;
1968 if (tPtr->currentTextBlock->next)
1969 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
1972 } else {
1973 if (tb->used + len >= tb->allocated) {
1974 tb->allocated = reqBlockSize(tb->used + len);
1975 tb->text = wrealloc(tb->text, tb->allocated);
1978 if (tb->blank) {
1979 memcpy(tb->text, text, len);
1980 tb->used = len;
1981 tPtr->tpos = len;
1982 tb->text[tb->used] = 0;
1983 tb->blank = False;
1985 } else {
1986 memmove(&(tb->text[tPtr->tpos + len]), &tb->text[tPtr->tpos], tb->used - tPtr->tpos + 1);
1987 memmove(&tb->text[tPtr->tpos], text, len);
1988 tb->used += len;
1989 tPtr->tpos += len;
1990 tb->text[tb->used] = 0;
1995 layOutDocument(tPtr);
1998 static void selectRegion(Text * tPtr, int x, int y)
2001 if (x < 0 || y < 0)
2002 return;
2004 y += (tPtr->flags.rulerShown ? 40 : 0);
2005 y += tPtr->vpos;
2006 if (y > 10)
2007 y -= 10; /* the original offset */
2009 x -= tPtr->visible.x - 2;
2010 if (x < 0)
2011 x = 0;
2013 tPtr->sel.x = WMAX(0, WMIN(tPtr->clicked.x, x));
2014 tPtr->sel.w = abs(tPtr->clicked.x - x);
2015 tPtr->sel.y = WMAX(0, WMIN(tPtr->clicked.y, y));
2016 tPtr->sel.h = abs(tPtr->clicked.y - y);
2018 tPtr->flags.ownsSelection = True;
2019 paintText(tPtr);
2022 static void releaseSelection(Text * tPtr)
2024 TextBlock *tb = tPtr->firstTextBlock;
2026 while (tb) {
2027 tb->selected = False;
2028 tb = tb->next;
2030 tPtr->flags.ownsSelection = False;
2031 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2033 paintText(tPtr);
2036 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
2038 Text *tPtr = view->self;
2039 Display *dpy = tPtr->view->screen->display;
2040 Atom _TARGETS;
2041 Atom TEXT = XInternAtom(dpy, "TEXT", False);
2042 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
2043 WMData *data = NULL;
2045 /* Parameter not used, but tell the compiler that it is ok */
2046 (void) selection;
2047 (void) cdata;
2049 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
2050 char *text = WMGetTextSelectedStream(tPtr);
2052 if (text) {
2053 data = WMCreateDataWithBytes(text, strlen(text));
2054 WMSetDataFormat(data, TYPETEXT);
2055 wfree(text);
2057 *type = target;
2058 return data;
2059 } else
2060 printf("didn't get it\n");
2062 _TARGETS = XInternAtom(dpy, "TARGETS", False);
2063 if (target == _TARGETS) {
2064 Atom *ptr;
2066 ptr = wmalloc(4 * sizeof(Atom));
2067 ptr[0] = _TARGETS;
2068 ptr[1] = XA_STRING;
2069 ptr[2] = TEXT;
2070 ptr[3] = COMPOUND_TEXT;
2072 data = WMCreateDataWithBytes(ptr, 4 * 4);
2073 WMSetDataFormat(data, 32);
2075 *type = target;
2076 return data;
2079 return NULL;
2082 static void lostHandler(WMView * view, Atom selection, void *cdata)
2084 /* Parameter not used, but tell the compiler that it is ok */
2085 (void) selection;
2086 (void) cdata;
2088 releaseSelection((WMText *) view->self);
2091 static WMSelectionProcs selectionHandler = {
2092 requestHandler, lostHandler, NULL
2095 static void ownershipObserver(void *observerData, WMNotification * notification)
2097 if (observerData != WMGetNotificationClientData(notification))
2098 lostHandler(WMWidgetView(observerData), XA_PRIMARY, NULL);
2101 static void autoSelectText(Text * tPtr, int clicks)
2103 int x, start;
2104 TextBlock *tb;
2105 char *mark = NULL, behind, ahead;
2107 if (!(tb = tPtr->currentTextBlock))
2108 return;
2110 if (clicks == 2) {
2112 switch (tb->text[tPtr->tpos]) {
2113 case ' ':
2114 return;
2116 case '<': case '>': behind = '<'; ahead = '>'; break;
2117 case '{': case '}': behind = '{'; ahead = '}'; break;
2118 case '[': case ']': behind = '['; ahead = ']'; break;
2120 default:
2121 behind = ahead = ' ';
2124 tPtr->sel.y = tPtr->cursor.y + 5;
2125 tPtr->sel.h = 6; /*tPtr->cursor.h-10; */
2127 if (tb->graphic) {
2128 tPtr->sel.x = tb->sections[0].x;
2129 tPtr->sel.w = tb->sections[0].w;
2130 } else {
2131 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
2133 start = tPtr->tpos;
2134 while (start > 0 && tb->text[start - 1] != behind)
2135 start--;
2137 x = tPtr->cursor.x;
2138 if (tPtr->tpos > start) {
2139 x -= WMWidthOfString(font, &tb->text[start], tPtr->tpos - start);
2141 tPtr->sel.x = (x < 0 ? 0 : x) + 1;
2143 if ((mark = strchr(&tb->text[start], ahead))) {
2144 tPtr->sel.w = WMWidthOfString(font, &tb->text[start],
2145 (int)(mark - &tb->text[start]));
2146 } else if (tb->used > start) {
2147 tPtr->sel.w = WMWidthOfString(font, &tb->text[start], tb->used - start);
2151 } else if (clicks == 3) {
2152 TextBlock *cur = tb;
2154 while (!tb->first) {
2155 tb = tb->prior;
2156 if (tb == NULL) {
2157 #ifndef NDEBUG
2158 wwarning("corrupted list of text blocks in WMText, while searching for first block");
2159 #endif
2160 goto error_select_3clicks;
2163 tPtr->sel.y = tb->sections[0]._y;
2165 tb = cur;
2166 while (tb->next && !tb->next->first) {
2167 tb = tb->next;
2169 tPtr->sel.h = tb->sections[tb->nsections - 1]._y + 5 - tPtr->sel.y;
2171 error_select_3clicks:
2172 tPtr->sel.x = 0;
2173 tPtr->sel.w = tPtr->docWidth;
2174 tPtr->clicked.x = 0; /* only for now, fix sel. code */
2177 if (!tPtr->flags.ownsSelection) {
2178 WMCreateSelectionHandler(tPtr->view, XA_PRIMARY, tPtr->lastClickTime, &selectionHandler, NULL);
2179 tPtr->flags.ownsSelection = True;
2181 paintText(tPtr);
2185 # if 0
2186 static void fontChanged(void *observerData, WMNotification * notification)
2188 WMText *tPtr = (WMText *) observerData;
2189 WMFont *font = (WMFont *) WMGetNotificationClientData(notification);
2190 printf("fontChanged\n");
2192 if (!tPtr || !font)
2193 return;
2195 if (tPtr->flags.ownsSelection)
2196 WMSetTextSelectionFont(tPtr, font);
2198 #endif
2200 static void handleTextKeyPress(Text * tPtr, XEvent * event)
2202 char buffer[64];
2203 KeySym ksym;
2204 int control_pressed = False;
2205 TextBlock *tb = NULL;
2207 if (((XKeyEvent *) event)->state & ControlMask)
2208 control_pressed = True;
2209 buffer[XLookupString(&event->xkey, buffer, 63, &ksym, NULL)] = 0;
2211 switch (ksym) {
2213 case XK_Home:
2214 if ((tPtr->currentTextBlock = tPtr->firstTextBlock))
2215 tPtr->tpos = 0;
2216 updateCursorPosition(tPtr);
2217 paintText(tPtr);
2218 break;
2220 case XK_End:
2221 if ((tPtr->currentTextBlock = tPtr->lastTextBlock)) {
2222 if (tPtr->currentTextBlock->graphic)
2223 tPtr->tpos = 1;
2224 else
2225 tPtr->tpos = tPtr->currentTextBlock->used;
2227 updateCursorPosition(tPtr);
2228 paintText(tPtr);
2229 break;
2231 case XK_Left:
2232 if (!(tb = tPtr->currentTextBlock))
2233 break;
2235 if (tb->graphic || tPtr->tpos == 0) {
2236 if (tb->prior) {
2237 tPtr->currentTextBlock = tb->prior;
2238 if (tPtr->currentTextBlock->graphic)
2239 tPtr->tpos = 1;
2240 else
2241 tPtr->tpos = tPtr->currentTextBlock->used;
2243 if (!tb->first && tPtr->tpos > 0)
2244 tPtr->tpos--;
2245 } else
2246 tPtr->tpos = 0;
2247 } else
2248 tPtr->tpos--;
2249 updateCursorPosition(tPtr);
2250 paintText(tPtr);
2251 break;
2253 case XK_Right:
2254 if (!(tb = tPtr->currentTextBlock))
2255 break;
2256 if (tb->graphic || tPtr->tpos == tb->used) {
2257 if (tb->next) {
2258 tPtr->currentTextBlock = tb->next;
2259 tPtr->tpos = 0;
2260 if (!tb->next->first && tb->next->used > 0)
2261 tPtr->tpos++;
2262 } else {
2263 if (tb->graphic)
2264 tPtr->tpos = 1;
2265 else
2266 tPtr->tpos = tb->used;
2268 } else
2269 tPtr->tpos++;
2270 updateCursorPosition(tPtr);
2271 paintText(tPtr);
2272 break;
2274 case XK_Down:
2275 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2276 tPtr->clicked.y + tPtr->cursor.h - tPtr->vpos);
2277 paintText(tPtr);
2278 break;
2280 case XK_Up:
2281 cursorToTextPosition(tPtr, tPtr->cursor.x + tPtr->visible.x,
2282 tPtr->visible.y + tPtr->cursor.y - tPtr->vpos - 3);
2283 paintText(tPtr);
2284 break;
2286 case XK_BackSpace:
2287 case XK_Delete:
2288 #ifdef XK_KP_Delete
2289 case XK_KP_Delete:
2290 #endif
2291 deleteTextInteractively(tPtr, ksym);
2292 updateCursorPosition(tPtr);
2293 paintText(tPtr);
2294 break;
2296 case XK_Control_R:
2297 case XK_Control_L:
2298 control_pressed = True;
2299 break;
2301 case XK_Tab:
2302 insertTextInteractively(tPtr, " ", 4);
2303 updateCursorPosition(tPtr);
2304 paintText(tPtr);
2305 break;
2307 case XK_Return:
2308 *buffer = '\n';
2309 default:
2310 if (*buffer != 0 && !control_pressed) {
2311 insertTextInteractively(tPtr, buffer, strlen(buffer));
2312 updateCursorPosition(tPtr);
2313 paintText(tPtr);
2315 } else if (control_pressed && ksym == XK_r) {
2316 Bool i = !tPtr->flags.rulerShown;
2317 WMShowTextRuler(tPtr, i);
2318 tPtr->flags.rulerShown = i;
2319 } else if (control_pressed && *buffer == '\a') {
2320 XBell(tPtr->view->screen->display, 0);
2321 } else {
2322 WMRelayToNextResponder(tPtr->view, event);
2326 if (!control_pressed && tPtr->flags.ownsSelection) {
2327 releaseSelection(tPtr);
2331 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
2333 Text *tPtr = (Text *) view->self;
2334 char *text;
2336 /* Parameter not used, but tell the compiler that it is ok */
2337 (void) selection;
2338 (void) target;
2339 (void) timestamp;
2340 (void) cdata;
2342 tPtr->flags.waitingForSelection = 0;
2344 if (data) {
2345 text = (char *)WMDataBytes(data);
2347 if (tPtr->parser) {
2348 (tPtr->parser) (tPtr, (void *)text);
2349 layOutDocument(tPtr);
2350 } else
2351 insertTextInteractively(tPtr, text, strlen(text));
2352 updateCursorPosition(tPtr);
2353 paintText(tPtr);
2355 } else {
2356 int n;
2358 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2360 if (text) {
2361 text[n] = 0;
2362 if (tPtr->parser) {
2363 (tPtr->parser) (tPtr, (void *)text);
2364 layOutDocument(tPtr);
2365 } else
2366 insertTextInteractively(tPtr, text, n);
2367 updateCursorPosition(tPtr);
2368 paintText(tPtr);
2370 XFree(text);
2376 static void handleActionEvents(XEvent * event, void *data)
2378 Text *tPtr = (Text *) data;
2379 Display *dpy = event->xany.display;
2380 KeySym ksym;
2382 switch (event->type) {
2383 case KeyPress:
2384 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2385 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2386 tPtr->flags.extendSelection = True;
2387 return;
2390 if (tPtr->flags.focused) {
2391 XGrabPointer(dpy, W_VIEW(tPtr)->window, False,
2392 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2393 GrabModeAsync, GrabModeAsync, None,
2394 tPtr->view->screen->invisibleCursor, CurrentTime);
2395 tPtr->flags.pointerGrabbed = True;
2396 handleTextKeyPress(tPtr, event);
2399 break;
2401 case KeyRelease:
2402 ksym = XLookupKeysym((XKeyEvent *) event, 0);
2403 if (ksym == XK_Shift_R || ksym == XK_Shift_L) {
2404 tPtr->flags.extendSelection = False;
2405 return;
2406 /* end modify flag so selection can be extended */
2408 break;
2410 case MotionNotify:
2412 if (tPtr->flags.pointerGrabbed) {
2413 tPtr->flags.pointerGrabbed = False;
2414 XUngrabPointer(dpy, CurrentTime);
2417 if (tPtr->flags.waitingForSelection)
2418 break;
2420 if ((event->xmotion.state & Button1Mask)) {
2422 if (WMIsDraggingFromView(tPtr->view)) {
2423 WMDragImageFromView(tPtr->view, event);
2424 break;
2427 if (!tPtr->flags.ownsSelection) {
2428 WMCreateSelectionHandler(tPtr->view,
2429 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
2430 tPtr->flags.ownsSelection = True;
2432 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2433 break;
2436 mouseOverObject(tPtr, event->xmotion.x, event->xmotion.y);
2437 break;
2439 case ButtonPress:
2441 if (tPtr->flags.pointerGrabbed) {
2442 tPtr->flags.pointerGrabbed = False;
2443 XUngrabPointer(dpy, CurrentTime);
2444 break;
2447 if (tPtr->flags.waitingForSelection)
2448 break;
2450 if (tPtr->flags.extendSelection && tPtr->flags.ownsSelection) {
2451 selectRegion(tPtr, event->xmotion.x, event->xmotion.y);
2452 return;
2455 if (tPtr->flags.ownsSelection)
2456 releaseSelection(tPtr);
2458 if (event->xbutton.button == Button1) {
2459 TextBlock *tb = tPtr->currentTextBlock;
2461 if (WMIsDoubleClick(event)) {
2463 tPtr->lastClickTime = event->xbutton.time;
2464 if (tb && tb->graphic && !tb->object) {
2465 if (tPtr->delegate && tPtr->delegate->didDoubleClickOnPicture) {
2466 char *desc;
2468 desc = wmalloc(tb->used + 1);
2469 memcpy(desc, tb->text, tb->used);
2470 desc[tb->used] = 0;
2471 (*tPtr->delegate->didDoubleClickOnPicture) (tPtr->delegate, desc);
2472 wfree(desc);
2474 } else {
2475 autoSelectText(tPtr, 2);
2477 break;
2478 } else if (event->xbutton.time - tPtr->lastClickTime < WINGsConfiguration.doubleClickDelay) {
2479 tPtr->lastClickTime = event->xbutton.time;
2480 autoSelectText(tPtr, 3);
2481 break;
2484 if (!tPtr->flags.focused) {
2485 WMSetFocusToWidget(tPtr);
2486 tPtr->flags.focused = True;
2487 } else if (tb && tPtr->flags.isOverGraphic && tb->graphic && !tb->object && tb->d.pixmap) {
2489 WMSetViewDragImage(tPtr->view, tb->d.pixmap);
2490 WMDragImageFromView(tPtr->view, event);
2491 break;
2494 tPtr->lastClickTime = event->xbutton.time;
2495 cursorToTextPosition(tPtr, event->xmotion.x, event->xmotion.y);
2496 paintText(tPtr);
2499 if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2500 WMScrollText(tPtr, 16);
2501 break;
2504 if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2505 WMScrollText(tPtr, -16);
2506 break;
2509 if (event->xbutton.button == Button2) {
2510 char *text = NULL;
2511 int n;
2513 if (!tPtr->flags.editable) {
2514 XBell(dpy, 0);
2515 break;
2518 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
2519 event->xbutton.time, pasteText, NULL)) {
2521 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
2522 tPtr->flags.waitingForSelection = 0;
2524 if (text) {
2525 text[n] = 0;
2527 if (tPtr->parser) {
2528 (tPtr->parser) (tPtr, (void *)text);
2529 layOutDocument(tPtr);
2530 } else
2531 insertTextInteractively(tPtr, text, n);
2533 XFree(text);
2534 #if 0
2535 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
2536 (void *)WMInsertTextEvent);
2537 #endif
2538 updateCursorPosition(tPtr);
2539 paintText(tPtr);
2541 } else {
2542 tPtr->flags.waitingForSelection = True;
2545 break;
2548 case ButtonRelease:
2549 if (tPtr->flags.pointerGrabbed) {
2550 tPtr->flags.pointerGrabbed = False;
2551 XUngrabPointer(dpy, CurrentTime);
2552 break;
2555 if (tPtr->flags.waitingForSelection)
2556 break;
2558 if (WMIsDraggingFromView(tPtr->view))
2559 WMDragImageFromView(tPtr->view, event);
2564 static void handleEvents(XEvent * event, void *data)
2566 Text *tPtr = (Text *) data;
2568 switch (event->type) {
2569 case Expose:
2571 if (event->xexpose.count != 0)
2572 break;
2574 if (tPtr->hS) {
2575 if (!(W_VIEW(tPtr->hS))->flags.realized)
2576 WMRealizeWidget(tPtr->hS);
2579 if (tPtr->vS) {
2580 if (!(W_VIEW(tPtr->vS))->flags.realized)
2581 WMRealizeWidget(tPtr->vS);
2584 if (tPtr->ruler) {
2585 if (!(W_VIEW(tPtr->ruler))->flags.realized)
2586 WMRealizeWidget(tPtr->ruler);
2590 if (!tPtr->db)
2591 textDidResize(tPtr->view->delegate, tPtr->view);
2593 paintText(tPtr);
2594 break;
2596 case FocusIn:
2597 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))
2598 != tPtr->view)
2599 return;
2600 tPtr->flags.focused = True;
2601 #if DO_BLINK
2602 if (tPtr->flags.editable && !tPtr->timerID) {
2603 tPtr->timerID = WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
2605 #endif
2607 break;
2609 case FocusOut:
2610 tPtr->flags.focused = False;
2611 paintText(tPtr);
2612 #if DO_BLINK
2613 if (tPtr->timerID) {
2614 WMDeleteTimerHandler(tPtr->timerID);
2615 tPtr->timerID = NULL;
2617 #endif
2618 break;
2620 case DestroyNotify:
2621 clearText(tPtr);
2622 if (tPtr->db)
2623 XFreePixmap(tPtr->view->screen->display, tPtr->db);
2624 if (tPtr->gfxItems)
2625 WMEmptyArray(tPtr->gfxItems);
2626 #if DO_BLINK
2627 if (tPtr->timerID)
2628 WMDeleteTimerHandler(tPtr->timerID);
2629 #endif
2630 WMReleaseFont(tPtr->dFont);
2631 WMReleaseColor(tPtr->dColor);
2632 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
2633 WMRemoveNotificationObserver(tPtr);
2635 WMFreeArray(tPtr->xdndSourceTypes);
2636 WMFreeArray(tPtr->xdndDestinationTypes);
2638 wfree(tPtr);
2640 break;
2645 static void insertPlainText(Text * tPtr, const char *text)
2647 const char *start, *mark;
2648 void *tb = NULL;
2650 start = text;
2651 while (start) {
2652 mark = strchr(start, '\n');
2653 if (mark) {
2654 tb = WMCreateTextBlockWithText(tPtr,
2655 start, tPtr->dFont,
2656 tPtr->dColor, tPtr->flags.first, (int)(mark - start));
2657 start = mark + 1;
2658 tPtr->flags.first = True;
2659 } else {
2660 if (start && strlen(start)) {
2661 tb = WMCreateTextBlockWithText(tPtr, start, tPtr->dFont,
2662 tPtr->dColor, tPtr->flags.first, strlen(start));
2663 } else
2664 tb = NULL;
2665 tPtr->flags.first = False;
2666 start = mark;
2669 if (tPtr->flags.prepend)
2670 WMPrependTextBlock(tPtr, tb);
2671 else
2672 WMAppendTextBlock(tPtr, tb);
2676 static void rulerMoveCallBack(WMWidget * w, void *self)
2678 Text *tPtr = (Text *) self;
2680 /* Parameter not used, but tell the compiler that it is ok */
2681 (void) w;
2683 if (!tPtr)
2684 return;
2685 if (W_CLASS(tPtr) != WC_Text)
2686 return;
2688 paintText(tPtr);
2691 static void rulerReleaseCallBack(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 WMThawText(tPtr);
2704 return;
2707 static WMArray *dropDataTypes(WMView * self)
2709 return ((Text *) self->self)->xdndSourceTypes;
2712 static WMDragOperationType wantedDropOperation(WMView * self)
2714 /* Parameter not used, but tell the compiler that it is ok */
2715 (void) self;
2717 return WDOperationCopy;
2720 static Bool acceptDropOperation(WMView * self, WMDragOperationType allowedOperation)
2722 /* Parameter not used, but tell the compiler that it is ok */
2723 (void) self;
2725 return (allowedOperation == WDOperationCopy);
2728 static WMData *fetchDragData(WMView * self, char *type)
2730 TextBlock *tb = ((WMText *) self->self)->currentTextBlock;
2731 char *desc;
2732 WMData *data;
2734 if (strcmp(type, "text/plain")) {
2735 if (!tb)
2736 return NULL;
2738 desc = wmalloc(tb->used + 1);
2739 memcpy(desc, tb->text, tb->used);
2740 desc[tb->used] = 0;
2741 data = WMCreateDataWithBytes(desc, strlen(desc) + 1);
2743 wfree(desc);
2745 return data;
2748 return NULL;
2751 static WMDragSourceProcs _DragSourceProcs = {
2752 dropDataTypes,
2753 wantedDropOperation,
2754 NULL,
2755 acceptDropOperation,
2756 NULL,
2757 NULL,
2758 fetchDragData
2761 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2763 /* Parameter not used, but tell the compiler that it is ok */
2764 (void) request;
2765 (void) sourceDataTypes;
2767 return ((Text *) self->self)->xdndDestinationTypes;
2770 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
2772 /* Parameter not used, but tell the compiler that it is ok */
2773 (void) self;
2774 (void) request;
2775 (void) sourceDataTypes;
2777 return WDOperationCopy;
2780 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
2782 WMText *tPtr = (WMText *) self->self;
2783 WMData *data;
2784 char *colorName;
2785 WMColor *color;
2787 /* Parameter not used, but tell the compiler that it is ok */
2788 (void) operations;
2789 (void) dropLocation;
2791 if (tPtr) {
2793 /* only one required type, implies only one drop data */
2795 /* get application/X-color if any */
2796 data = (WMData *) WMPopFromArray(dropData);
2797 if (data != NULL) {
2798 colorName = (char *)WMDataBytes(data);
2799 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
2801 if (color) {
2802 WMSetTextSelectionColor(tPtr, color);
2803 WMReleaseColor(color);
2809 static WMDragDestinationProcs _DragDestinationProcs = {
2810 NULL,
2811 requiredDataTypes,
2812 allowedOperation,
2813 NULL,
2814 performDragOperation,
2815 NULL
2818 static char *getStream(WMText * tPtr, int sel, int array)
2820 TextBlock *tb = NULL;
2821 char *text = NULL;
2822 unsigned long where = 0;
2824 if (!tPtr)
2825 return NULL;
2827 if (!(tb = tPtr->firstTextBlock))
2828 return NULL;
2830 if (tPtr->writer) {
2831 (tPtr->writer) (tPtr, (void *)text);
2832 return text;
2835 tb = tPtr->firstTextBlock;
2836 while (tb) {
2838 if (!tb->graphic || (tb->graphic && !tPtr->flags.monoFont)) {
2840 if (!sel || (tb->graphic && tb->selected)) {
2842 if (!tPtr->flags.ignoreNewLine && (tb->first || tb->blank)
2843 && tb != tPtr->firstTextBlock) {
2844 text = wrealloc(text, where + 1);
2845 text[where++] = '\n';
2848 if (tb->blank)
2849 goto _gSnext;
2851 if (tb->graphic && array) {
2852 text = wrealloc(text, where + 4);
2853 text[where++] = 0xFA;
2854 text[where++] = (tb->used >> 8) & 0x0ff;
2855 text[where++] = tb->used & 0x0ff;
2856 text[where++] = tb->allocated; /* extra info */
2858 text = wrealloc(text, where + tb->used);
2859 memcpy(&text[where], tb->text, tb->used);
2860 where += tb->used;
2862 } else if (sel && tb->selected) {
2864 if (!tPtr->flags.ignoreNewLine && tb->blank) {
2865 text = wrealloc(text, where + 1);
2866 text[where++] = '\n';
2869 if (tb->blank)
2870 goto _gSnext;
2872 text = wrealloc(text, where + (tb->s_end - tb->s_begin));
2873 memcpy(&text[where], &tb->text[tb->s_begin], tb->s_end - tb->s_begin);
2874 where += tb->s_end - tb->s_begin;
2879 _gSnext: tb = tb->next;
2882 /* +1 for the end of string, let's be nice */
2883 text = wrealloc(text, where + 1);
2884 text[where] = 0;
2885 return text;
2888 static void releaseStreamObjects(void *data)
2890 if (data)
2891 wfree(data);
2894 static WMArray *getStreamObjects(WMText * tPtr, int sel)
2896 WMArray *array;
2897 WMData *data;
2898 char *stream;
2899 unsigned short len;
2900 char *start, *fa, *desc;
2902 stream = getStream(tPtr, sel, 1);
2903 if (!stream)
2904 return NULL;
2906 array = WMCreateArrayWithDestructor(4, releaseStreamObjects);
2908 start = stream;
2909 while (start) {
2911 fa = strchr(start, 0xFA);
2912 if (fa) {
2913 if ((int)(fa - start) > 0) {
2914 desc = start;
2915 desc[(int)(fa - start)] = 0;
2916 data = WMCreateDataWithBytes((void *)desc, (int)(fa - start));
2917 WMSetDataFormat(data, TYPETEXT);
2918 WMAddToArray(array, (void *)data);
2921 len = *(fa + 1) * 0xff + *(fa + 2);
2922 data = WMCreateDataWithBytes((void *)(fa + 4), len);
2923 WMSetDataFormat(data, *(fa + 3));
2924 WMAddToArray(array, (void *)data);
2925 start = fa + len + 4;
2927 } else {
2928 if (start && strlen(start)) {
2929 data = WMCreateDataWithBytes((void *)start, strlen(start));
2930 WMSetDataFormat(data, TYPETEXT);
2931 WMAddToArray(array, (void *)data);
2933 start = fa;
2937 wfree(stream);
2938 return array;
2941 #define XDND_TEXT_DATA_TYPE "text/plain"
2942 #define XDND_COLOR_DATA_TYPE "application/X-color"
2943 static WMArray *getXdndSourceTypeArray(void)
2945 WMArray *types = WMCreateArray(1);
2946 WMAddToArray(types, XDND_TEXT_DATA_TYPE);
2947 return types;
2950 static WMArray *getXdndDestinationTypeArray(void)
2952 WMArray *types = WMCreateArray(1);
2953 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
2954 return types;
2957 WMText *WMCreateTextForDocumentType(WMWidget * parent, WMAction * parser, WMAction * writer)
2959 Text *tPtr;
2960 Display *dpy;
2961 WMScreen *scr;
2962 XGCValues gcv;
2964 tPtr = wmalloc(sizeof(Text));
2965 tPtr->widgetClass = WC_Text;
2966 tPtr->view = W_CreateView(W_VIEW(parent));
2967 if (!tPtr->view) {
2968 perror("could not create text's view\n");
2969 wfree(tPtr);
2970 return NULL;
2973 dpy = tPtr->view->screen->display;
2974 scr = tPtr->view->screen;
2976 tPtr->view->self = tPtr;
2977 tPtr->view->attribs.cursor = scr->textCursor;
2978 tPtr->view->attribFlags |= CWOverrideRedirect | CWCursor;
2979 W_ResizeView(tPtr->view, 250, 200);
2981 tPtr->dColor = WMBlackColor(scr);
2982 tPtr->fgColor = WMBlackColor(scr);
2983 tPtr->bgColor = WMWhiteColor(scr);
2984 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
2986 gcv.graphics_exposures = False;
2987 gcv.foreground = W_PIXEL(scr->gray);
2988 gcv.background = W_PIXEL(scr->darkGray);
2989 gcv.fill_style = FillStippled;
2990 /* why not use scr->stipple here? */
2991 gcv.stipple = XCreateBitmapFromData(dpy, W_DRAWABLE(scr), STIPPLE_BITS, STIPPLE_WIDTH, STIPPLE_HEIGHT);
2992 tPtr->stippledGC = XCreateGC(dpy, W_DRAWABLE(scr),
2993 GCForeground | GCBackground | GCStipple
2994 | GCFillStyle | GCGraphicsExposures, &gcv);
2996 tPtr->ruler = NULL;
2997 tPtr->vS = NULL;
2998 tPtr->hS = NULL;
3000 tPtr->dFont = WMSystemFontOfSize(scr, 12);
3002 tPtr->view->delegate = &_TextViewDelegate;
3004 tPtr->delegate = NULL;
3006 #if DO_BLINK
3007 tPtr->timerID = NULL;
3008 #endif
3010 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask
3011 | EnterWindowMask | LeaveWindowMask | FocusChangeMask, handleEvents, tPtr);
3013 WMCreateEventHandler(tPtr->view, ButtonReleaseMask | ButtonPressMask
3014 | KeyReleaseMask | KeyPressMask | Button1MotionMask, handleActionEvents, tPtr);
3016 WMAddNotificationObserver(ownershipObserver, tPtr, WMSelectionOwnerDidChangeNotification, tPtr);
3018 WMSetViewDragSourceProcs(tPtr->view, &_DragSourceProcs);
3019 WMSetViewDragDestinationProcs(tPtr->view, &_DragDestinationProcs);
3022 WMArray *types = WMCreateArray(2);
3023 WMAddToArray(types, "application/X-color");
3024 WMAddToArray(types, "application/X-image");
3025 WMRegisterViewForDraggedTypes(tPtr->view, types);
3026 WMFreeArray(types);
3029 /*WMAddNotificationObserver(fontChanged, tPtr,
3030 WMFontPanelDidChangeNotification, tPtr); */
3032 tPtr->firstTextBlock = NULL;
3033 tPtr->lastTextBlock = NULL;
3034 tPtr->currentTextBlock = NULL;
3035 tPtr->tpos = 0;
3037 tPtr->gfxItems = WMCreateArray(4);
3039 tPtr->parser = parser;
3040 tPtr->writer = writer;
3042 tPtr->sel.x = tPtr->sel.y = 2;
3043 tPtr->sel.w = tPtr->sel.h = 0;
3045 tPtr->clicked.x = tPtr->clicked.y = 2;
3047 tPtr->visible.x = tPtr->visible.y = 2;
3048 tPtr->visible.h = tPtr->view->size.height;
3049 tPtr->visible.w = tPtr->view->size.width - 4;
3051 tPtr->cursor.x = -23;
3053 tPtr->docWidth = 0;
3054 tPtr->docHeight = 0;
3055 tPtr->dBulletPix = WMCreatePixmapFromXPMData(tPtr->view->screen, default_bullet);
3056 tPtr->db = (Pixmap) NULL;
3057 tPtr->bgPixmap = NULL;
3059 tPtr->margins = WMGetRulerMargins(NULL);
3060 tPtr->margins->right = tPtr->visible.w;
3061 tPtr->nMargins = 1;
3063 tPtr->flags.rulerShown = False;
3064 tPtr->flags.monoFont = False;
3065 tPtr->flags.focused = False;
3066 tPtr->flags.editable = True;
3067 tPtr->flags.ownsSelection = False;
3068 tPtr->flags.pointerGrabbed = False;
3069 tPtr->flags.extendSelection = False;
3070 tPtr->flags.frozen = False;
3071 tPtr->flags.cursorShown = True;
3072 tPtr->flags.acceptsGraphic = False;
3073 tPtr->flags.horizOnDemand = False;
3074 tPtr->flags.needsLayOut = False;
3075 tPtr->flags.ignoreNewLine = False;
3076 tPtr->flags.indentNewLine = False;
3077 tPtr->flags.laidOut = False;
3078 tPtr->flags.ownsSelection = False;
3079 tPtr->flags.waitingForSelection = False;
3080 tPtr->flags.prepend = False;
3081 tPtr->flags.isOverGraphic = False;
3082 tPtr->flags.relief = WRSunken;
3083 tPtr->flags.isOverGraphic = 0;
3084 tPtr->flags.alignment = WALeft;
3085 tPtr->flags.first = True;
3087 tPtr->xdndSourceTypes = getXdndSourceTypeArray();
3088 tPtr->xdndDestinationTypes = getXdndDestinationTypeArray();
3090 return tPtr;
3093 void WMPrependTextStream(WMText * tPtr, const char *text)
3095 CHECK_CLASS(tPtr, WC_Text);
3097 if (!text) {
3098 if (tPtr->flags.ownsSelection)
3099 releaseSelection(tPtr);
3100 clearText(tPtr);
3101 updateScrollers(tPtr);
3102 return;
3105 tPtr->flags.prepend = True;
3106 if (text && tPtr->parser)
3107 (tPtr->parser) (tPtr, (void *)text);
3108 else
3109 insertPlainText(tPtr, text);
3111 tPtr->flags.needsLayOut = True;
3112 tPtr->tpos = 0;
3113 if (!tPtr->flags.frozen) {
3114 layOutDocument(tPtr);
3118 void WMAppendTextStream(WMText * tPtr, const char *text)
3120 CHECK_CLASS(tPtr, WC_Text);
3122 if (!text) {
3123 if (tPtr->flags.ownsSelection)
3124 releaseSelection(tPtr);
3125 clearText(tPtr);
3126 updateScrollers(tPtr);
3127 return;
3130 tPtr->flags.prepend = False;
3131 if (text && tPtr->parser)
3132 (tPtr->parser) (tPtr, (void *)text);
3133 else
3134 insertPlainText(tPtr, text);
3136 tPtr->flags.needsLayOut = True;
3137 if (tPtr->currentTextBlock) {
3138 if (tPtr->currentTextBlock->graphic)
3139 tPtr->tpos = 1;
3140 else
3141 tPtr->tpos = tPtr->currentTextBlock->used;
3144 if (!tPtr->flags.frozen) {
3145 layOutDocument(tPtr);
3149 char *WMGetTextStream(WMText * tPtr)
3151 CHECK_CLASS(tPtr, WC_Text);
3153 return getStream(tPtr, 0, 0);
3156 char *WMGetTextSelectedStream(WMText * tPtr)
3158 CHECK_CLASS(tPtr, WC_Text);
3160 return getStream(tPtr, 1, 0);
3163 WMArray *WMGetTextObjects(WMText * tPtr)
3165 CHECK_CLASS(tPtr, WC_Text);
3167 return getStreamObjects(tPtr, 0);
3170 WMArray *WMGetTextSelectedObjects(WMText * tPtr)
3172 CHECK_CLASS(tPtr, WC_Text);
3174 return getStreamObjects(tPtr, 1);
3177 void WMSetTextDelegate(WMText * tPtr, WMTextDelegate * delegate)
3179 CHECK_CLASS(tPtr, WC_Text);
3181 tPtr->delegate = delegate;
3184 void *WMCreateTextBlockWithObject(WMText * tPtr, WMWidget * w,
3185 const char *description, WMColor * color,
3186 unsigned short first, unsigned short extraInfo)
3188 TextBlock *tb;
3190 if (!w || !description || !color)
3191 return NULL;
3193 tb = wmalloc(sizeof(TextBlock));
3195 tb->text = wstrdup(description);
3196 tb->used = strlen(description);
3197 tb->blank = False;
3198 tb->d.widget = w;
3199 tb->color = WMRetainColor(color);
3200 tb->marginN = newMargin(tPtr, NULL);
3201 tb->allocated = extraInfo;
3202 tb->first = first;
3203 tb->kanji = False;
3204 tb->graphic = True;
3205 tb->object = True;
3206 tb->underlined = False;
3207 tb->selected = False;
3208 tb->script = 0;
3209 tb->sections = NULL;
3210 tb->nsections = 0;
3211 tb->prior = NULL;
3212 tb->next = NULL;
3214 return tb;
3217 void *WMCreateTextBlockWithPixmap(WMText * tPtr, WMPixmap * p,
3218 const char *description, WMColor * color,
3219 unsigned short first, unsigned short extraInfo)
3221 TextBlock *tb;
3223 if (!p || !description || !color)
3224 return NULL;
3226 tb = wmalloc(sizeof(TextBlock));
3228 tb->text = wstrdup(description);
3229 tb->used = strlen(description);
3230 tb->blank = False;
3231 tb->d.pixmap = WMRetainPixmap(p);
3232 tb->color = WMRetainColor(color);
3233 tb->marginN = newMargin(tPtr, NULL);
3234 tb->allocated = extraInfo;
3235 tb->first = first;
3236 tb->kanji = False;
3237 tb->graphic = True;
3238 tb->object = False;
3239 tb->underlined = False;
3240 tb->selected = False;
3241 tb->script = 0;
3242 tb->sections = NULL;
3243 tb->nsections = 0;
3244 tb->prior = NULL;
3245 tb->next = NULL;
3247 return tb;
3250 void *WMCreateTextBlockWithText(WMText * tPtr, const char *text, WMFont * font, WMColor * color,
3251 unsigned short first, unsigned short len)
3253 TextBlock *tb;
3255 if (!font || !color)
3256 return NULL;
3258 tb = wmalloc(sizeof(TextBlock));
3260 tb->allocated = reqBlockSize(len);
3261 tb->text = (char *)wmalloc(tb->allocated);
3263 if (len < 1 || !text || (*text == '\n' && len == 1)) {
3264 *tb->text = ' ';
3265 tb->used = 1;
3266 tb->blank = True;
3267 } else {
3268 memcpy(tb->text, text, len);
3269 tb->used = len;
3270 tb->blank = False;
3272 tb->text[tb->used] = 0;
3274 tb->d.font = WMRetainFont(font);
3275 tb->color = WMRetainColor(color);
3276 tb->marginN = newMargin(tPtr, NULL);
3277 tb->first = first;
3278 tb->kanji = False;
3279 tb->graphic = False;
3280 tb->underlined = False;
3281 tb->selected = False;
3282 tb->script = 0;
3283 tb->sections = NULL;
3284 tb->nsections = 0;
3285 tb->prior = NULL;
3286 tb->next = NULL;
3287 return tb;
3290 void
3291 WMSetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int first,
3292 unsigned int kanji, unsigned int underlined, int script, WMRulerMargins * margins)
3294 TextBlock *tb = (TextBlock *) vtb;
3295 if (!tb)
3296 return;
3298 tb->first = first;
3299 tb->kanji = kanji;
3300 tb->underlined = underlined;
3301 tb->script = script;
3302 tb->marginN = newMargin(tPtr, margins);
3305 void
3306 WMGetTextBlockProperties(WMText * tPtr, void *vtb, unsigned int *first,
3307 unsigned int *kanji, unsigned int *underlined, int *script, WMRulerMargins *margins)
3309 TextBlock *tb = (TextBlock *) vtb;
3310 if (!tb)
3311 return;
3313 if (first)
3314 *first = tb->first;
3315 if (kanji)
3316 *kanji = tb->kanji;
3317 if (underlined)
3318 *underlined = tb->underlined;
3319 if (script)
3320 *script = tb->script;
3321 if (margins)
3322 *margins = tPtr->margins[tb->marginN];
3325 void WMPrependTextBlock(WMText * tPtr, void *vtb)
3327 TextBlock *tb = (TextBlock *) vtb;
3329 if (!tb)
3330 return;
3332 if (tb->graphic) {
3333 if (tb->object) {
3334 WMWidget *w = tb->d.widget;
3335 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3336 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3337 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3340 WMAddToArray(tPtr->gfxItems, (void *)tb);
3341 tPtr->tpos = 1;
3343 } else {
3344 tPtr->tpos = tb->used;
3347 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3348 tb->next = tb->prior = NULL;
3349 tb->first = True;
3350 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3351 return;
3354 if (!tb->first) {
3355 tb->marginN = tPtr->currentTextBlock->marginN;
3358 tb->next = tPtr->currentTextBlock;
3359 tb->prior = tPtr->currentTextBlock->prior;
3360 if (tPtr->currentTextBlock->prior)
3361 tPtr->currentTextBlock->prior->next = tb;
3363 tPtr->currentTextBlock->prior = tb;
3364 if (!tb->prior)
3365 tPtr->firstTextBlock = tb;
3367 tPtr->currentTextBlock = tb;
3370 void WMAppendTextBlock(WMText * tPtr, void *vtb)
3372 TextBlock *tb = (TextBlock *) vtb;
3374 if (!tb)
3375 return;
3377 if (tb->graphic) {
3378 if (tb->object) {
3379 WMWidget *w = tb->d.widget;
3380 if (W_CLASS(w) != WC_TextField && W_CLASS(w) != WC_Text) {
3381 (W_VIEW(w))->attribs.cursor = tPtr->view->screen->defaultCursor;
3382 (W_VIEW(w))->attribFlags |= CWOverrideRedirect | CWCursor;
3385 WMAddToArray(tPtr->gfxItems, (void *)tb);
3386 tPtr->tpos = 1;
3388 } else {
3389 tPtr->tpos = tb->used;
3392 if (!tPtr->lastTextBlock || !tPtr->firstTextBlock) {
3393 tb->next = tb->prior = NULL;
3394 tb->first = True;
3395 tPtr->lastTextBlock = tPtr->firstTextBlock = tPtr->currentTextBlock = tb;
3396 return;
3399 if (!tb->first) {
3400 tb->marginN = tPtr->currentTextBlock->marginN;
3403 tb->next = tPtr->currentTextBlock->next;
3404 tb->prior = tPtr->currentTextBlock;
3405 if (tPtr->currentTextBlock->next)
3406 tPtr->currentTextBlock->next->prior = tb;
3408 tPtr->currentTextBlock->next = tb;
3410 if (!tb->next)
3411 tPtr->lastTextBlock = tb;
3413 tPtr->currentTextBlock = tb;
3416 void *WMRemoveTextBlock(WMText * tPtr)
3418 TextBlock *tb = NULL;
3420 if (!tPtr->firstTextBlock || !tPtr->lastTextBlock || !tPtr->currentTextBlock) {
3421 return NULL;
3424 tb = tPtr->currentTextBlock;
3425 if (tb->graphic) {
3426 WMRemoveFromArray(tPtr->gfxItems, (void *)tb);
3428 if (tb->object) {
3429 WMUnmapWidget(tb->d.widget);
3433 if (tPtr->currentTextBlock == tPtr->firstTextBlock) {
3434 if (tPtr->currentTextBlock->next)
3435 tPtr->currentTextBlock->next->prior = NULL;
3437 tPtr->firstTextBlock = tPtr->currentTextBlock->next;
3438 tPtr->currentTextBlock = tPtr->firstTextBlock;
3440 } else if (tPtr->currentTextBlock == tPtr->lastTextBlock) {
3441 tPtr->currentTextBlock->prior->next = NULL;
3442 tPtr->lastTextBlock = tPtr->currentTextBlock->prior;
3443 tPtr->currentTextBlock = tPtr->lastTextBlock;
3444 } else {
3445 tPtr->currentTextBlock->prior->next = tPtr->currentTextBlock->next;
3446 tPtr->currentTextBlock->next->prior = tPtr->currentTextBlock->prior;
3447 tPtr->currentTextBlock = tPtr->currentTextBlock->next;
3450 return (void *)tb;
3453 #if 0
3454 static void destroyWidget(WMWidget * widget)
3456 WMDestroyWidget(widget);
3457 // -- never do this -- wfree(widget);
3459 #endif
3461 void WMDestroyTextBlock(WMText * tPtr, void *vtb)
3463 TextBlock *tb = (TextBlock *) vtb;
3465 /* Parameter not used, but tell the compiler that it is ok */
3466 (void) tPtr;
3468 if (!tb)
3469 return;
3471 if (tb->graphic) {
3472 if (tb->object) {
3473 /* naturally, there's a danger to destroying widgets whose action
3474 * brings us here: ie. press a button to destroy it...
3475 * need to find a safer way. till then... this stays commented out */
3476 /* 5 months later... destroy it 10 seconds after now which should
3477 * be enough time for the widget's action to be completed... :-) */
3478 /* This is a bad assumption. Just destroy the widget here.
3479 * if the caller needs it, it can protect it with W_RetainView()
3480 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3481 WMDestroyWidget(tb->d.widget);
3482 } else {
3483 WMReleasePixmap(tb->d.pixmap);
3485 } else {
3486 WMReleaseFont(tb->d.font);
3489 WMReleaseColor(tb->color);
3490 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3491 if (tb->sections)
3492 wfree(tb->sections);
3493 wfree(tb->text);
3494 wfree(tb);
3497 void WMSetTextForegroundColor(WMText * tPtr, WMColor * color)
3499 if (tPtr->fgColor)
3500 WMReleaseColor(tPtr->fgColor);
3502 tPtr->fgColor = WMRetainColor(color ? color : tPtr->view->screen->black);
3504 paintText(tPtr);
3507 void WMSetTextBackgroundColor(WMText * tPtr, WMColor * color)
3509 if (tPtr->bgColor)
3510 WMReleaseColor(tPtr->bgColor);
3512 tPtr->bgColor = WMRetainColor(color ? color : tPtr->view->screen->white);
3513 W_SetViewBackgroundColor(tPtr->view, tPtr->bgColor);
3515 paintText(tPtr);
3518 void WMSetTextBackgroundPixmap(WMText * tPtr, WMPixmap * pixmap)
3520 if (tPtr->bgPixmap)
3521 WMReleasePixmap(tPtr->bgPixmap);
3523 if (pixmap)
3524 tPtr->bgPixmap = WMRetainPixmap(pixmap);
3525 else
3526 tPtr->bgPixmap = NULL;
3529 void WMSetTextRelief(WMText * tPtr, WMReliefType relief)
3531 tPtr->flags.relief = relief;
3532 textDidResize(tPtr->view->delegate, tPtr->view);
3535 void WMSetTextHasHorizontalScroller(WMText * tPtr, Bool shouldhave)
3537 if (shouldhave && !tPtr->hS) {
3538 tPtr->hS = WMCreateScroller(tPtr);
3539 (W_VIEW(tPtr->hS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3540 (W_VIEW(tPtr->hS))->attribFlags |= CWOverrideRedirect | CWCursor;
3541 WMSetScrollerArrowsPosition(tPtr->hS, WSAMinEnd);
3542 WMSetScrollerAction(tPtr->hS, scrollersCallBack, tPtr);
3543 WMMapWidget(tPtr->hS);
3544 } else if (!shouldhave && tPtr->hS) {
3545 WMUnmapWidget(tPtr->hS);
3546 WMDestroyWidget(tPtr->hS);
3547 tPtr->hS = NULL;
3550 tPtr->hpos = 0;
3551 tPtr->prevHpos = 0;
3552 textDidResize(tPtr->view->delegate, tPtr->view);
3555 void WMSetTextHasRuler(WMText * tPtr, Bool shouldhave)
3557 if (shouldhave && !tPtr->ruler) {
3558 tPtr->ruler = WMCreateRuler(tPtr);
3559 (W_VIEW(tPtr->ruler))->attribs.cursor = tPtr->view->screen->defaultCursor;
3560 (W_VIEW(tPtr->ruler))->attribFlags |= CWOverrideRedirect | CWCursor;
3561 WMSetRulerReleaseAction(tPtr->ruler, rulerReleaseCallBack, tPtr);
3562 WMSetRulerMoveAction(tPtr->ruler, rulerMoveCallBack, tPtr);
3563 } else if (!shouldhave && tPtr->ruler) {
3564 WMShowTextRuler(tPtr, False);
3565 WMDestroyWidget(tPtr->ruler);
3566 tPtr->ruler = NULL;
3568 textDidResize(tPtr->view->delegate, tPtr->view);
3571 void WMShowTextRuler(WMText * tPtr, Bool show)
3573 if (!tPtr->ruler)
3574 return;
3576 if (tPtr->flags.monoFont)
3577 show = False;
3579 tPtr->flags.rulerShown = show;
3580 if (show) {
3581 WMMapWidget(tPtr->ruler);
3582 } else {
3583 WMUnmapWidget(tPtr->ruler);
3586 textDidResize(tPtr->view->delegate, tPtr->view);
3589 Bool WMGetTextRulerShown(WMText * tPtr)
3591 if (!tPtr->ruler)
3592 return False;
3594 return tPtr->flags.rulerShown;
3597 void WMSetTextHasVerticalScroller(WMText * tPtr, Bool shouldhave)
3599 if (shouldhave && !tPtr->vS) {
3600 tPtr->vS = WMCreateScroller(tPtr);
3601 (W_VIEW(tPtr->vS))->attribs.cursor = tPtr->view->screen->defaultCursor;
3602 (W_VIEW(tPtr->vS))->attribFlags |= CWOverrideRedirect | CWCursor;
3603 WMSetScrollerArrowsPosition(tPtr->vS, WSAMaxEnd);
3604 WMSetScrollerAction(tPtr->vS, scrollersCallBack, tPtr);
3605 WMMapWidget(tPtr->vS);
3606 } else if (!shouldhave && tPtr->vS) {
3607 WMUnmapWidget(tPtr->vS);
3608 WMDestroyWidget(tPtr->vS);
3609 tPtr->vS = NULL;
3612 tPtr->vpos = 0;
3613 tPtr->prevVpos = 0;
3614 textDidResize(tPtr->view->delegate, tPtr->view);
3617 Bool WMScrollText(WMText * tPtr, int amount)
3619 Bool scroll = False;
3621 if (amount == 0 || !tPtr->view->flags.realized)
3622 return False;
3624 if (amount < 0) {
3625 if (tPtr->vpos > 0) {
3626 if (tPtr->vpos > abs(amount))
3627 tPtr->vpos += amount;
3628 else
3629 tPtr->vpos = 0;
3630 scroll = True;
3632 } else {
3633 int limit = tPtr->docHeight - tPtr->visible.h;
3634 if (tPtr->vpos < limit) {
3635 if (tPtr->vpos < limit - amount)
3636 tPtr->vpos += amount;
3637 else
3638 tPtr->vpos = limit;
3639 scroll = True;
3643 if (scroll && tPtr->vpos != tPtr->prevVpos) {
3644 updateScrollers(tPtr);
3645 paintText(tPtr);
3647 tPtr->prevVpos = tPtr->vpos;
3648 return scroll;
3651 Bool WMPageText(WMText * tPtr, Bool direction)
3653 if (!tPtr->view->flags.realized)
3654 return False;
3656 return WMScrollText(tPtr, direction ? tPtr->visible.h : -tPtr->visible.h);
3659 void WMSetTextEditable(WMText * tPtr, Bool editable)
3661 tPtr->flags.editable = editable;
3664 int WMGetTextEditable(WMText * tPtr)
3666 return tPtr->flags.editable;
3669 void WMSetTextIndentNewLines(WMText * tPtr, Bool indent)
3671 tPtr->flags.indentNewLine = indent;
3674 void WMSetTextIgnoresNewline(WMText * tPtr, Bool ignore)
3676 tPtr->flags.ignoreNewLine = ignore;
3679 Bool WMGetTextIgnoresNewline(WMText * tPtr)
3681 return tPtr->flags.ignoreNewLine;
3684 void WMSetTextUsesMonoFont(WMText * tPtr, Bool mono)
3686 if (mono) {
3687 if (tPtr->flags.rulerShown)
3688 WMShowTextRuler(tPtr, False);
3689 if (tPtr->flags.alignment != WALeft)
3690 tPtr->flags.alignment = WALeft;
3693 tPtr->flags.monoFont = mono;
3694 textDidResize(tPtr->view->delegate, tPtr->view);
3697 Bool WMGetTextUsesMonoFont(WMText * tPtr)
3699 return tPtr->flags.monoFont;
3702 void WMSetTextDefaultFont(WMText * tPtr, WMFont * font)
3704 if (tPtr->dFont)
3705 WMReleaseFont(tPtr->dFont);
3707 if (font) {
3708 tPtr->dFont = WMRetainFont(font);
3709 } else {
3710 tPtr->dFont = WMSystemFontOfSize(tPtr->view->screen, 12);
3714 WMFont *WMGetTextDefaultFont(WMText * tPtr)
3716 return WMRetainFont(tPtr->dFont);
3719 void WMSetTextDefaultColor(WMText * tPtr, WMColor * color)
3721 if (tPtr->dColor)
3722 WMReleaseColor(tPtr->dColor);
3724 if (color) {
3725 tPtr->dColor = WMRetainColor(color);
3726 } else {
3727 tPtr->dColor = WMBlackColor(tPtr->view->screen);
3731 WMColor *WMGetTextDefaultColor(WMText * tPtr)
3733 return tPtr->dColor;
3736 void WMSetTextAlignment(WMText * tPtr, WMAlignment alignment)
3738 if (tPtr->flags.monoFont)
3739 tPtr->flags.alignment = WALeft;
3740 else
3741 tPtr->flags.alignment = alignment;
3742 WMThawText(tPtr);
3745 int WMGetTextInsertType(WMText * tPtr)
3747 return tPtr->flags.prepend;
3750 void WMSetTextSelectionColor(WMText * tPtr, WMColor * color)
3752 setSelectionProperty(tPtr, NULL, color, -1);
3755 WMColor *WMGetTextSelectionColor(WMText * tPtr)
3757 TextBlock *tb;
3759 tb = tPtr->currentTextBlock;
3761 if (!tb || !tPtr->flags.ownsSelection)
3762 return NULL;
3764 if (!tb->selected)
3765 return NULL;
3767 return tb->color;
3770 void WMSetTextSelectionFont(WMText * tPtr, WMFont * font)
3772 setSelectionProperty(tPtr, font, NULL, -1);
3775 WMFont *WMGetTextSelectionFont(WMText * tPtr)
3777 TextBlock *tb;
3779 tb = tPtr->currentTextBlock;
3781 if (!tb || !tPtr->flags.ownsSelection)
3782 return NULL;
3784 if (!tb->selected)
3785 return NULL;
3787 if (tb->graphic) {
3788 tb = getFirstNonGraphicBlockFor(tb, 1);
3789 if (!tb)
3790 return NULL;
3792 return (tb->selected ? tb->d.font : NULL);
3795 void WMSetTextSelectionUnderlined(WMText * tPtr, int underlined)
3797 /* // check this */
3798 if (underlined != 0 && underlined != 1)
3799 return;
3801 setSelectionProperty(tPtr, NULL, NULL, underlined);
3804 int WMGetTextSelectionUnderlined(WMText * tPtr)
3806 TextBlock *tb;
3808 tb = tPtr->currentTextBlock;
3810 if (!tb || !tPtr->flags.ownsSelection)
3811 return 0;
3813 if (!tb->selected)
3814 return 0;
3816 return tb->underlined;
3819 void WMFreezeText(WMText * tPtr)
3821 tPtr->flags.frozen = True;
3824 void WMThawText(WMText * tPtr)
3826 tPtr->flags.frozen = False;
3828 if (tPtr->flags.monoFont) {
3829 int j, c = WMGetArrayItemCount(tPtr->gfxItems);
3830 TextBlock *tb;
3832 /* make sure to unmap widgets no matter where they are */
3833 /* they'll be later remapped if needed by paintText */
3834 for (j = 0; j < c; j++) {
3835 if ((tb = (TextBlock *) WMGetFromArray(tPtr->gfxItems, j))) {
3836 if (tb->object && ((W_VIEW(tb->d.widget))->flags.mapped))
3837 WMUnmapWidget(tb->d.widget);
3842 tPtr->flags.laidOut = False;
3843 layOutDocument(tPtr);
3844 updateScrollers(tPtr);
3845 paintText(tPtr);
3846 tPtr->flags.needsLayOut = False;
3850 /* find first occurence of a string */
3851 static const char *mystrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3853 const char *ptr;
3855 if (!haystack || !needle || !end)
3856 return NULL;
3858 for (ptr = haystack; ptr < end; ptr++) {
3859 if (caseSensitive) {
3860 if (*ptr == *needle && !strncmp(ptr, needle, len))
3861 return ptr;
3863 } else {
3864 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3865 return ptr;
3869 return NULL;
3872 /* find last occurence of a string */
3873 static const char *mystrrstr(const char *haystack, const char *needle, unsigned short len, const char *end, Bool caseSensitive)
3875 const char *ptr;
3877 if (!haystack || !needle || !end)
3878 return NULL;
3880 for (ptr = haystack - 2; ptr > end; ptr--) {
3881 if (caseSensitive) {
3882 if (*ptr == *needle && !strncmp(ptr, needle, len))
3883 return ptr;
3884 } else {
3885 if (tolower(*ptr) == tolower(*needle) && !strncasecmp(ptr, needle, len))
3886 return ptr;
3890 return NULL;
3893 Bool WMFindInTextStream(WMText * tPtr, const char *needle, Bool direction, Bool caseSensitive)
3895 TextBlock *tb;
3896 const char *mark = NULL;
3897 unsigned short pos;
3899 #if 0
3900 if (!(tb = tPtr->currentTextBlock)) {
3901 if (!(tb = ((direction > 0) ? tPtr->firstTextBlock : tPtr->lastTextBlock))) {
3902 return False;
3904 } else {
3905 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3906 tb = (direction>0) ? tb->next : tb->prior; */
3907 if (tb != tPtr->lastTextBlock)
3908 tb = tb->prior;
3910 #endif
3911 tb = tPtr->currentTextBlock;
3912 pos = tPtr->tpos;
3914 while (tb) {
3915 if (!tb->graphic) {
3917 if (direction > 0) {
3918 if (pos + 1 < tb->used)
3919 pos++;
3921 if (tb->used - pos > 0 && pos > 0) {
3922 mark = mystrstr(&tb->text[pos], needle,
3923 strlen(needle), &tb->text[tb->used], caseSensitive);
3925 } else {
3926 tb = tb->next;
3927 pos = 0;
3928 continue;
3931 } else {
3932 if (pos - 1 > 0)
3933 pos--;
3935 if (pos > 0) {
3936 mark = mystrrstr(&tb->text[pos], needle,
3937 strlen(needle), tb->text, caseSensitive);
3938 } else {
3939 tb = tb->prior;
3940 if (!tb)
3941 return False;
3942 pos = tb->used;
3943 continue;
3947 if (mark) {
3948 WMFont *font = tPtr->flags.monoFont ? tPtr->dFont : tb->d.font;
3950 tPtr->tpos = (int)(mark - tb->text);
3951 tPtr->currentTextBlock = tb;
3952 updateCursorPosition(tPtr);
3953 tPtr->sel.y = tPtr->cursor.y + 5;
3954 tPtr->sel.h = tPtr->cursor.h - 10;
3955 tPtr->sel.x = tPtr->cursor.x + 1;
3956 tPtr->sel.w = WMIN(WMWidthOfString(font,
3957 &tb->text[tPtr->tpos], strlen(needle)),
3958 tPtr->docWidth - tPtr->sel.x);
3959 tPtr->flags.ownsSelection = True;
3960 paintText(tPtr);
3962 return True;
3966 tb = (direction > 0) ? tb->next : tb->prior;
3967 if (tb) {
3968 pos = (direction > 0) ? 0 : tb->used;
3972 return False;
3975 Bool WMReplaceTextSelection(WMText * tPtr, char *replacement)
3977 if (!tPtr->flags.ownsSelection)
3978 return False;
3980 removeSelection(tPtr);
3982 if (replacement) {
3983 insertTextInteractively(tPtr, replacement, strlen(replacement));
3984 updateCursorPosition(tPtr);
3985 paintText(tPtr);
3988 return True;