2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
6 #include <X11/keysym.h>
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 */
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 */
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 */
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;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
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 */
128 WMHandlerID timerID
; /* for nice twinky-winky */
133 WMTextDelegate
*delegate
;
136 WMRulerMargins
*margins
; /* an array of margins */
138 unsigned int nMargins
:7; /* the total number of margins in use */
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; */
165 WMArray
*xdndSourceTypes
;
166 WMArray
*xdndDestinationTypes
;
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);}
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr
, int len
)
184 s
= wmalloc(len
+ 1);
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
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
[] = {
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
)
226 for (i
= 0; i
< tPtr
->nMargins
; i
++) {
228 if (WMIsMarginEqualToMargin(&tPtr
->margins
[i
], margins
))
235 static int newMargin(Text
* tPtr
, WMRulerMargins
* margins
)
240 tPtr
->margins
[0].retainCount
++;
244 n
= getMarginNumber(tPtr
, margins
);
248 if (tPtr
->nMargins
>= 127) {
249 n
= tPtr
->nMargins
- 1;
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;
263 tPtr
->margins
[n
].retainCount
++;
269 static Bool
sectionWasSelected(Text
* tPtr
, TextBlock
* tb
, XRectangle
* rect
, int s
)
271 unsigned short i
, w
, lw
, selected
= False
, extend
= False
;
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
)) {
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
;
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
)) {
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
)
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
;
326 i
= tb
->sections
[s
].begin
;
329 if (0 && tb
->sections
[s
].x
>= sel
.x
) {
330 tb
->s_begin
= tb
->sections
[s
].begin
;
334 while (++i
<= tb
->sections
[s
].end
) {
336 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
- 1]), 1);
339 if (lw
+ tb
->sections
[s
].x
>= sel
.x
|| i
== tb
->sections
[s
].end
) {
342 tb
->s_begin
= (tb
->selected
? WMIN(tb
->s_begin
, i
) : i
);
347 if (i
> tb
->sections
[s
].end
) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
352 _selEnd
: rect
->x
= tb
->sections
[s
].x
+ lw
;
354 while (++i
<= tb
->sections
[s
].end
) {
356 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
- 1]), 1);
359 if (lw
+ rect
->x
>= sel
.x
+ sel
.w
|| i
== tb
->sections
[s
].end
) {
361 if (i
!= tb
->sections
[s
].end
) {
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
);
381 rect
->y
= tb
->sections
[s
]._y
- tPtr
->vpos
;
382 rect
->height
= tb
->sections
[s
].h
;
384 printf("DEBUG: graphic s%d h%d\n", s
, tb
->sections
[s
].h
);
391 static void setSelectionProperty(WMText
* tPtr
, WMFont
* font
, WMColor
* color
, int underlined
)
396 tb
= tPtr
->firstTextBlock
;
397 if (!tb
|| !tPtr
->flags
.ownsSelection
)
400 if (font
&& (!color
|| underlined
== -1))
404 if (tPtr
->flags
.monoFont
|| tb
->selected
) {
406 if (tPtr
->flags
.monoFont
|| (tb
->s_end
- tb
->s_begin
== tb
->used
)
411 WMReleaseFont(tb
->d
.font
);
412 tb
->d
.font
= WMRetainFont(font
);
414 } else if (underlined
!= -1) {
415 tb
->underlined
= underlined
;
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
,
430 (tb
->s_end
- tb
->s_begin
));
432 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
433 &(tb
->text
[tb
->s_begin
]),
434 (isFont
? font
: tb
->d
.
437 color
: color
), False
,
438 (tb
->s_end
- tb
->s_begin
));
442 if (underlined
!= -1) {
443 midtb
->underlined
= underlined
;
445 midtb
->underlined
= otb
->underlined
;
448 midtb
->selected
= !True
;
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) {
459 WMCreateTextBlockWithText(tPtr
,
460 &(otb
->text
[otb
->s_end
]), otb
->d
.font
,
461 otb
->color
, False
, otb
->used
- otb
->s_end
);
464 ntb
->underlined
= otb
->underlined
;
465 ntb
->selected
= False
;
466 WMAppendTextBlock(tPtr
, ntb
);
467 tb
= tPtr
->currentTextBlock
;
472 tPtr
->currentTextBlock
= midtb
;
475 otb
->selected
= False
;
476 otb
->used
= otb
->s_begin
;
483 tPtr
->flags
.needsLayOut
= True
;
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
) {
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
;
507 if (!(tb
= tPtr
->firstTextBlock
))
512 if (!first
&& !tb
->graphic
) {
513 WMReleaseFont(tPtr
->dFont
);
514 tPtr
->dFont
= WMRetainFont(tb
->d
.font
);
518 if ((tb
->s_end
- tb
->s_begin
== tb
->used
) || tb
->graphic
) {
519 tPtr
->currentTextBlock
= tb
;
522 } else if (tb
->prior
) {
523 if (tb
->prior
->graphic
)
526 tPtr
->tpos
= tb
->prior
->used
;
530 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
531 tb
= tPtr
->currentTextBlock
;
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
;
548 static TextBlock
*getFirstNonGraphicBlockFor(TextBlock
* tb
, short dir
)
550 TextBlock
*hold
= tb
;
558 tb
= (dir
? tb
->next
: tb
->prior
);
566 tb
= (dir
? tb
->prior
: tb
->next
);
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
);
584 tPtr
->currentTextBlock
= (*dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
591 layOutDocument(tPtr
);
595 *dir
= !(y
<= tb
->sections
[0].y
);
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
)
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
)
615 static void paintText(Text
* tPtr
)
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
;
626 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
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
)) {
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
))
653 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
657 if (tPtr
->vpos
<= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
660 if (tPtr
->vpos
>= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
678 /* first, place all text that can be viewed */
679 while (!done
&& tb
) {
685 tb
->selected
= False
;
687 for (s
= 0; s
< tb
->nsections
&& !done
; s
++) {
689 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
694 if (tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
697 if (tPtr
->flags
.monoFont
) {
699 color
= tPtr
->fgColor
;
705 if (tPtr
->flags
.ownsSelection
) {
708 if (sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
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
,
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
) {
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
) {
743 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
744 WMUnmapWidget(tb
->d
.widget
);
748 /* if it's viewable, and not mapped, map it */
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;
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;
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
) {
778 if (sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
779 Drawable d
= (0 && tb
->object
?
780 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
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
);
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
)
822 x
-= tPtr
->visible
.x
;
824 y
-= tPtr
->visible
.y
;
827 if (tPtr
->flags
.ownsSelection
) {
829 && tPtr
->sel
.y
<= y
&& tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
&& tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
830 tPtr
->flags
.isOverGraphic
= 1;
836 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
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;
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;
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
);
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
);
883 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
, blinkCursor
, data
);
886 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
890 static void updateCursorPosition(Text
* tPtr
)
892 TextBlock
*tb
= NULL
;
895 if (tPtr
->flags
.needsLayOut
)
896 layOutDocument(tPtr
);
898 if (!(tb
= tPtr
->currentTextBlock
)) {
899 if (!(tb
= tPtr
->firstTextBlock
)) {
900 WMFont
*font
= tPtr
->dFont
;
902 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
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
;
921 x
+= tb
->sections
[0].w
;
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
)
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
);
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
) {
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" */
965 if (tPtr
->flags
.needsLayOut
)
966 layOutDocument(tPtr
);
968 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
972 x
-= (tPtr
->visible
.x
- 2);
976 /* clicked is relative to document, not window... */
980 if (!(tb
= tPtr
->currentTextBlock
)) {
981 if (!(tb
= tPtr
->firstTextBlock
)) {
982 WMFont
*font
= tPtr
->dFont
;
984 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
991 /* first, which direction? Most likely, newly clicked
992 position will be close to previous */
993 if (!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
996 s
= (dir
? 0 : tb
->nsections
- 1);
997 if (y
>= tb
->sections
[s
]._y
&& y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1001 /* get the first (or last) section of the TextBlock that
1002 lies about the vertical click point */
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
);
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
))) {
1023 if ((dir
? tb
->next
: tb
->prior
)) {
1024 tb
= (dir
? tb
->next
: tb
->prior
);
1026 break; /* goto _doneH; */
1031 if (s
< 0 || s
>= tb
->nsections
) {
1032 s
= (dir
? tb
->nsections
- 1 : 0);
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
);
1050 _y
= tb
->sections
[s
]._y
;
1054 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1055 tb
= (dir
? tb
->next
: tb
->prior
);
1062 _w
= WMWidgetWidth(tb
->d
.widget
) - 5;
1064 _w
= tb
->d
.pixmap
->width
- 5;
1066 if (tb
->sections
[0].x
+ _w
>= x
)
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
)
1077 if (tb
->sections
[s
].x
<= x
)
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
);
1086 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1087 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
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
;
1099 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1101 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1102 } else if (pos
> tb
->sections
[s
].begin
) {
1104 WMWidthOfString(tb
->d
.font
,
1105 &(tb
->text
[tb
->sections
[s
].begin
]),
1106 pos
- tb
->sections
[s
].begin
);
1113 if ((dir
? tb
->next
: tb
->prior
)) {
1114 tb
= (dir
? tb
->next
: tb
->prior
);
1120 s
= (dir
? 0 : tb
->nsections
- 1);
1123 /* we have said TextBlock, now where within it? */
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) {
1132 tPtr
->cursor
.x
+= gw
;
1134 printf("first %d\n", tb
->first
);
1136 if (tb
->prior
->graphic
)
1139 pos
= tb
->prior
->used
;
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
;
1157 while (pos
< len
&& WMWidthOfString(f
, text
, pos
+ 1) < _w
)
1160 tPtr
->cursor
.x
= tb
->sections
[s
].x
+ (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1162 pos
+= tb
->sections
[s
].begin
;
1168 tPtr
->tpos
= (pos
<= 1) ? pos
: 0;
1170 tPtr
->tpos
= (pos
< tb
->used
) ? pos
: tb
->used
;
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
) {
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
)
1203 if (tPtr
->docHeight
<= tPtr
->visible
.h
) {
1204 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
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
);
1216 if (tPtr
->docWidth
<= tPtr
->visible
.w
) {
1217 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
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
);
1229 static void scrollersCallBack(WMWidget
* w
, void *self
)
1231 Text
*tPtr
= (Text
*) self
;
1232 Bool scroll
= False
;
1235 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1238 if (w
== tPtr
->vS
) {
1240 height
= tPtr
->visible
.h
;
1242 which
= WMGetScrollerHitPart(tPtr
->vS
);
1245 case WSDecrementLine
:
1246 if (tPtr
->vpos
> 0) {
1247 if (tPtr
->vpos
> 16)
1254 case WSIncrementLine
:{
1255 int limit
= tPtr
->docHeight
- height
;
1256 if (tPtr
->vpos
< limit
) {
1257 if (tPtr
->vpos
< limit
- 16)
1265 case WSDecrementPage
:
1266 if (((int)tPtr
->vpos
- (int)height
) >= 0)
1267 tPtr
->vpos
-= height
;
1272 case WSIncrementPage
:
1273 tPtr
->vpos
+= height
;
1274 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1275 tPtr
->vpos
= tPtr
->docHeight
- height
;
1279 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1280 * (float)(tPtr
->docHeight
- height
);
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
);
1297 case WSDecrementLine
:
1298 if (tPtr
->hpos
> 0) {
1299 if (tPtr
->hpos
> 16)
1306 case WSIncrementLine
:{
1307 int limit
= tPtr
->docWidth
- width
;
1308 if (tPtr
->hpos
< limit
) {
1309 if (tPtr
->hpos
< limit
- 16)
1317 case WSDecrementPage
:
1318 if (((int)tPtr
->hpos
- (int)width
) >= 0)
1319 tPtr
->hpos
-= width
;
1324 case WSIncrementPage
:
1325 tPtr
->hpos
+= width
;
1326 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1327 tPtr
->hpos
= tPtr
->docWidth
- width
;
1331 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1332 * (float)(tPtr
->docWidth
- width
);
1339 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1340 tPtr
->prevHpos
= tPtr
->hpos
;
1344 updateScrollers(tPtr
);
1351 unsigned short begin
, end
; /* what part of the text block */
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
;
1359 TextBlock
*tb
, *tbsame
= NULL
;
1361 if (!items
|| nitems
== 0)
1364 for (i
= 0; i
< nitems
; i
++) {
1368 if (!tPtr
->flags
.monoFont
) {
1370 WMWidget
*wdt
= tb
->d
.widget
;
1371 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1372 if (tPtr
->flags
.alignment
!= WALeft
)
1373 lw
+= WMWidgetWidth(wdt
);
1375 line_height
= WMAX(line_height
, tb
->d
.pixmap
->height
+ max_d
);
1376 if (tPtr
->flags
.alignment
!= WALeft
)
1377 lw
+= tb
->d
.pixmap
->width
;
1382 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1383 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
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
++) {
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;
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
);
1419 if (!tPtr
->flags
.monoFont
) {
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
);
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
;
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
;
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
);
1451 static void layOutDocument(Text
* tPtr
)
1454 myLineItems
*items
= NULL
;
1455 unsigned int itemsSize
= 0, nitems
= 0, begin
, end
;
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
)))
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 */
1475 if (!tb
->sections
|| tb
->nsections
< 1) {
1476 tb
= tPtr
->firstTextBlock
;
1477 tPtr
->flags
.laidOut
= False
;
1482 if (!tb
->prior
->sections
|| tb
->prior
->nsections
< 1) {
1483 tb
= tPtr
->firstTextBlock
;
1484 tPtr
->flags
.laidOut
= False
;
1489 if (tb
->sections
[0]._y
!= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
) {
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
;
1507 if (tb
->sections
&& tb
->nsections
> 0) {
1508 wfree(tb
->sections
);
1509 tb
->sections
= NULL
;
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
));
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
;
1531 if (!tPtr
->flags
.monoFont
) {
1533 width
= WMWidgetWidth(tb
->d
.widget
);
1535 width
= tb
->d
.pixmap
->width
;
1537 if (width
> tPtr
->docWidth
)
1538 tPtr
->docWidth
= width
;
1541 if (lw
>= tPtr
->visible
.w
- x
) {
1542 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
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;
1558 } else if ((start
= tb
->text
)) {
1560 font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
1563 mark
= strchr(start
, ' ');
1565 end
+= (int)(mark
- start
) + 1;
1568 end
+= strlen(start
);
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;
1584 width
= WMWidthOfString(font
, t
, ++i
);
1585 } while (width
< tPtr
->visible
.w
&& i
< l
);
1589 start
= &tb
->text
[end
];
1595 if (lw
>= tPtr
->visible
.w
- x
) {
1596 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
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
;
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
;
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
);
1630 sd
= tPtr
->docHeight
- y
;
1632 printf("dif %d-%d: %d\n", ss
, sd
, ss
- sd
);
1633 y
+= tb
->next
->sections
[0]._y
- y
;
1635 printf("nitems%d\n", nitems
);
1637 y
= tPtr
->docHeight
+ ss
- sd
;
1641 tPtr
->flags
.laidOut
= False
;
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
) {
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)
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 */
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);
1695 WMMoveWidget(tPtr
->vS
, 1 - (rel
? 1 : 0), rh
+ 1 - (rel
? 1 : 0));
1696 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
? 2 : 0));
1698 WMSetRulerOffset(tPtr
->ruler
, 22);
1700 WMSetRulerOffset(tPtr
->ruler
, 2);
1704 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1705 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
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
) {
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;
1732 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1733 tPtr
->view
->window
, tPtr
->visible
.w
,
1734 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1741 W_ViewDelegate _TextViewDelegate
= {
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
)
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
)
1776 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1777 Bool done
= 1, wasFirst
= 0;
1779 if (!tPtr
->flags
.editable
)
1782 if (!(tb
= tPtr
->currentTextBlock
))
1785 if (tPtr
->flags
.ownsSelection
) {
1786 if (removeSelection(tPtr
))
1787 layOutDocument(tPtr
);
1791 wasFirst
= tb
->first
;
1792 if (back
&& tPtr
->tpos
< 1) {
1794 if (tb
->prior
->blank
) {
1795 tPtr
->currentTextBlock
= tb
->prior
;
1796 WMRemoveTextBlock(tPtr
);
1797 tPtr
->currentTextBlock
= tb
;
1799 layOutDocument(tPtr
);
1803 TextBlock
*prior
= tb
->prior
;
1804 tPtr
->currentTextBlock
= tb
;
1805 WMRemoveTextBlock(tPtr
);
1814 tPtr
->tpos
= tb
->used
;
1816 tPtr
->currentTextBlock
= tb
;
1820 tb
->next
->first
= False
;
1821 layOutDocument(tPtr
);
1828 if ((tb
->used
> 0) && ((back
? tPtr
->tpos
> 0 : 1))
1829 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1832 memmove(&(tb
->text
[tPtr
->tpos
]), &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
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
))
1842 /* no more chars, and it's marked as blank? */
1844 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1846 if (tb
->used
== 0 || tb
->graphic
)
1847 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1850 tPtr
->currentTextBlock
= sibling
;
1852 tPtr
->tpos
= (back
? 1 : 0);
1854 tPtr
->tpos
= (back
? sibling
->used
: 0);
1856 /* no more chars, so mark it as blank */
1857 } else if (tb
->used
== 0) {
1859 } else if (tb
->graphic
) {
1860 Bool hasNext
= (tb
->next
!= NULL
);
1862 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1865 } else if (tPtr
->currentTextBlock
) {
1866 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
? 1 : tPtr
->currentTextBlock
->used
);
1869 printf("DEBUG: unaccounted for... catch this!\n");
1872 layOutDocument(tPtr
);
1875 static void insertTextInteractively(Text
* tPtr
, char *text
, int len
)
1878 char *newline
= NULL
;
1880 if (!tPtr
->flags
.editable
) {
1884 if (len
< 1 || !text
)
1887 if (tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1890 if (tPtr
->flags
.ownsSelection
)
1891 removeSelection(tPtr
);
1893 if (tPtr
->flags
.ignoreNewLine
) {
1895 for (i
= 0; i
< len
; i
++) {
1896 if (text
[i
] == '\n')
1901 tb
= tPtr
->currentTextBlock
;
1902 if (!tb
|| tb
->graphic
) {
1904 WMAppendTextStream(tPtr
, text
);
1905 layOutDocument(tPtr
);
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) {
1918 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
1919 tb
->used
-= (tb
->used
- tPtr
->tpos
);
1921 insertTextInteractively(tPtr
, text
, nlen
);
1923 WMAppendTextStream(tPtr
, newline
);
1925 insertTextInteractively(tPtr
, save
, s
);
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
,
1939 tb
->color
, True
, 0));
1942 WMAppendTextBlock(tPtr
, ntb
);
1945 } else if (tPtr
->tpos
== tb
->used
) {
1946 if (tPtr
->flags
.indentNewLine
) {
1947 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1949 tb
->color
, True
, 4));
1952 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1954 tb
->color
, True
, 0));
1957 } else if (tPtr
->tpos
== 0) {
1958 if (tPtr
->flags
.indentNewLine
) {
1959 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1961 tb
->color
, True
, 4));
1963 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1965 tb
->color
, True
, 0));
1968 if (tPtr
->currentTextBlock
->next
)
1969 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
1973 if (tb
->used
+ len
>= tb
->allocated
) {
1974 tb
->allocated
= reqBlockSize(tb
->used
+ len
);
1975 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
1979 memcpy(tb
->text
, text
, len
);
1982 tb
->text
[tb
->used
] = 0;
1986 memmove(&(tb
->text
[tPtr
->tpos
+ len
]), &tb
->text
[tPtr
->tpos
], tb
->used
- tPtr
->tpos
+ 1);
1987 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
1990 tb
->text
[tb
->used
] = 0;
1995 layOutDocument(tPtr
);
1998 static void selectRegion(Text
* tPtr
, int x
, int y
)
2004 y
+= (tPtr
->flags
.rulerShown
? 40 : 0);
2007 y
-= 10; /* the original offset */
2009 x
-= tPtr
->visible
.x
- 2;
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
;
2022 static void releaseSelection(Text
* tPtr
)
2024 TextBlock
*tb
= tPtr
->firstTextBlock
;
2027 tb
->selected
= False
;
2030 tPtr
->flags
.ownsSelection
= False
;
2031 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
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
;
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 */
2049 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2050 char *text
= WMGetTextSelectedStream(tPtr
);
2053 data
= WMCreateDataWithBytes(text
, strlen(text
));
2054 WMSetDataFormat(data
, TYPETEXT
);
2060 printf("didn't get it\n");
2062 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2063 if (target
== _TARGETS
) {
2066 array
[0] = _TARGETS
;
2067 array
[1] = XA_STRING
;
2069 array
[3] = COMPOUND_TEXT
;
2071 data
= WMCreateDataWithBytes(&array
, sizeof(array
));
2072 WMSetDataFormat(data
, 32);
2081 static void lostHandler(WMView
* view
, Atom selection
, void *cdata
)
2083 /* Parameter not used, but tell the compiler that it is ok */
2087 releaseSelection((WMText
*) view
->self
);
2090 static WMSelectionProcs selectionHandler
= {
2091 requestHandler
, lostHandler
, NULL
2094 static void ownershipObserver(void *observerData
, WMNotification
* notification
)
2096 if (observerData
!= WMGetNotificationClientData(notification
))
2097 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2100 static void autoSelectText(Text
* tPtr
, int clicks
)
2104 char *mark
= NULL
, behind
, ahead
;
2106 if (!(tb
= tPtr
->currentTextBlock
))
2111 switch (tb
->text
[tPtr
->tpos
]) {
2115 case '<': case '>': behind = '<'; ahead = '>'; break;
2116 case '{': case '}': behind = '{'; ahead = '}'; break;
2117 case '[': case ']': behind = '['; ahead = ']'; break;
2120 behind
= ahead
= ' ';
2123 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
2124 tPtr
->sel
.h
= 6; /*tPtr->cursor.h-10; */
2127 tPtr
->sel
.x
= tb
->sections
[0].x
;
2128 tPtr
->sel
.w
= tb
->sections
[0].w
;
2130 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
2133 while (start
> 0 && tb
->text
[start
- 1] != behind
)
2137 if (tPtr
->tpos
> start
) {
2138 x
-= WMWidthOfString(font
, &tb
->text
[start
], tPtr
->tpos
- start
);
2140 tPtr
->sel
.x
= (x
< 0 ? 0 : x
) + 1;
2142 if ((mark
= strchr(&tb
->text
[start
], ahead
))) {
2143 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2144 (int)(mark
- &tb
->text
[start
]));
2145 } else if (tb
->used
> start
) {
2146 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
], tb
->used
- start
);
2150 } else if (clicks
== 3) {
2151 TextBlock
*cur
= tb
;
2153 while (!tb
->first
) {
2157 wwarning("corrupted list of text blocks in WMText, while searching for first block");
2159 goto error_select_3clicks
;
2162 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2165 while (tb
->next
&& !tb
->next
->first
) {
2168 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
- 1]._y
+ 5 - tPtr
->sel
.y
;
2170 error_select_3clicks
:
2172 tPtr
->sel
.w
= tPtr
->docWidth
;
2173 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2176 if (!tPtr
->flags
.ownsSelection
) {
2177 WMCreateSelectionHandler(tPtr
->view
, XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2178 tPtr
->flags
.ownsSelection
= True
;
2185 static void fontChanged(void *observerData
, WMNotification
* notification
)
2187 WMText
*tPtr
= (WMText
*) observerData
;
2188 WMFont
*font
= (WMFont
*) WMGetNotificationClientData(notification
);
2189 printf("fontChanged\n");
2194 if (tPtr
->flags
.ownsSelection
)
2195 WMSetTextSelectionFont(tPtr
, font
);
2199 static void handleTextKeyPress(Text
* tPtr
, XEvent
* event
)
2203 int control_pressed
= False
;
2204 TextBlock
*tb
= NULL
;
2206 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2207 control_pressed
= True
;
2208 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2213 if ((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2215 updateCursorPosition(tPtr
);
2220 if ((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2221 if (tPtr
->currentTextBlock
->graphic
)
2224 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2226 updateCursorPosition(tPtr
);
2231 if (!(tb
= tPtr
->currentTextBlock
))
2234 if (tb
->graphic
|| tPtr
->tpos
== 0) {
2236 tPtr
->currentTextBlock
= tb
->prior
;
2237 if (tPtr
->currentTextBlock
->graphic
)
2240 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2242 if (!tb
->first
&& tPtr
->tpos
> 0)
2248 updateCursorPosition(tPtr
);
2253 if (!(tb
= tPtr
->currentTextBlock
))
2255 if (tb
->graphic
|| tPtr
->tpos
== tb
->used
) {
2257 tPtr
->currentTextBlock
= tb
->next
;
2259 if (!tb
->next
->first
&& tb
->next
->used
> 0)
2265 tPtr
->tpos
= tb
->used
;
2269 updateCursorPosition(tPtr
);
2274 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2275 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2280 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2281 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2290 deleteTextInteractively(tPtr
, ksym
);
2291 updateCursorPosition(tPtr
);
2297 control_pressed
= True
;
2301 insertTextInteractively(tPtr
, " ", 4);
2302 updateCursorPosition(tPtr
);
2309 if (*buffer
!= 0 && !control_pressed
) {
2310 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2311 updateCursorPosition(tPtr
);
2314 } else if (control_pressed
&& ksym
== XK_r
) {
2315 Bool i
= !tPtr
->flags
.rulerShown
;
2316 WMShowTextRuler(tPtr
, i
);
2317 tPtr
->flags
.rulerShown
= i
;
2318 } else if (control_pressed
&& *buffer
== '\a') {
2319 XBell(tPtr
->view
->screen
->display
, 0);
2321 WMRelayToNextResponder(tPtr
->view
, event
);
2325 if (!control_pressed
&& tPtr
->flags
.ownsSelection
) {
2326 releaseSelection(tPtr
);
2330 static void pasteText(WMView
* view
, Atom selection
, Atom target
, Time timestamp
, void *cdata
, WMData
* data
)
2332 Text
*tPtr
= (Text
*) view
->self
;
2335 /* Parameter not used, but tell the compiler that it is ok */
2341 tPtr
->flags
.waitingForSelection
= 0;
2344 text
= (char *)WMDataBytes(data
);
2347 (tPtr
->parser
) (tPtr
, (void *)text
);
2348 layOutDocument(tPtr
);
2350 insertTextInteractively(tPtr
, text
, strlen(text
));
2351 updateCursorPosition(tPtr
);
2357 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2362 (tPtr
->parser
) (tPtr
, (void *)text
);
2363 layOutDocument(tPtr
);
2365 insertTextInteractively(tPtr
, text
, n
);
2366 updateCursorPosition(tPtr
);
2375 static void handleActionEvents(XEvent
* event
, void *data
)
2377 Text
*tPtr
= (Text
*) data
;
2378 Display
*dpy
= event
->xany
.display
;
2381 switch (event
->type
) {
2383 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2384 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2385 tPtr
->flags
.extendSelection
= True
;
2389 if (tPtr
->flags
.focused
) {
2390 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2391 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
2392 GrabModeAsync
, GrabModeAsync
, None
,
2393 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2394 tPtr
->flags
.pointerGrabbed
= True
;
2395 handleTextKeyPress(tPtr
, event
);
2401 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2402 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2403 tPtr
->flags
.extendSelection
= False
;
2405 /* end modify flag so selection can be extended */
2411 if (tPtr
->flags
.pointerGrabbed
) {
2412 tPtr
->flags
.pointerGrabbed
= False
;
2413 XUngrabPointer(dpy
, CurrentTime
);
2416 if (tPtr
->flags
.waitingForSelection
)
2419 if ((event
->xmotion
.state
& Button1Mask
)) {
2421 if (WMIsDraggingFromView(tPtr
->view
)) {
2422 WMDragImageFromView(tPtr
->view
, event
);
2426 if (!tPtr
->flags
.ownsSelection
) {
2427 WMCreateSelectionHandler(tPtr
->view
,
2428 XA_PRIMARY
, event
->xbutton
.time
, &selectionHandler
, NULL
);
2429 tPtr
->flags
.ownsSelection
= True
;
2431 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2435 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2440 if (tPtr
->flags
.pointerGrabbed
) {
2441 tPtr
->flags
.pointerGrabbed
= False
;
2442 XUngrabPointer(dpy
, CurrentTime
);
2446 if (tPtr
->flags
.waitingForSelection
)
2449 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2450 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2454 if (tPtr
->flags
.ownsSelection
)
2455 releaseSelection(tPtr
);
2457 if (event
->xbutton
.button
== Button1
) {
2458 TextBlock
*tb
= tPtr
->currentTextBlock
;
2460 if (WMIsDoubleClick(event
)) {
2462 tPtr
->lastClickTime
= event
->xbutton
.time
;
2463 if (tb
&& tb
->graphic
&& !tb
->object
) {
2464 if (tPtr
->delegate
&& tPtr
->delegate
->didDoubleClickOnPicture
) {
2467 desc
= wmalloc(tb
->used
+ 1);
2468 memcpy(desc
, tb
->text
, tb
->used
);
2470 (*tPtr
->delegate
->didDoubleClickOnPicture
) (tPtr
->delegate
, desc
);
2474 autoSelectText(tPtr
, 2);
2477 } else if (event
->xbutton
.time
- tPtr
->lastClickTime
< WINGsConfiguration
.doubleClickDelay
) {
2478 tPtr
->lastClickTime
= event
->xbutton
.time
;
2479 autoSelectText(tPtr
, 3);
2483 if (!tPtr
->flags
.focused
) {
2484 WMSetFocusToWidget(tPtr
);
2485 tPtr
->flags
.focused
= True
;
2486 } else if (tb
&& tPtr
->flags
.isOverGraphic
&& tb
->graphic
&& !tb
->object
&& tb
->d
.pixmap
) {
2488 WMSetViewDragImage(tPtr
->view
, tb
->d
.pixmap
);
2489 WMDragImageFromView(tPtr
->view
, event
);
2493 tPtr
->lastClickTime
= event
->xbutton
.time
;
2494 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2498 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelDown
) {
2499 WMScrollText(tPtr
, 16);
2503 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
) {
2504 WMScrollText(tPtr
, -16);
2508 if (event
->xbutton
.button
== Button2
) {
2512 if (!tPtr
->flags
.editable
) {
2517 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2518 event
->xbutton
.time
, pasteText
, NULL
)) {
2520 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2521 tPtr
->flags
.waitingForSelection
= 0;
2527 (tPtr
->parser
) (tPtr
, (void *)text
);
2528 layOutDocument(tPtr
);
2530 insertTextInteractively(tPtr
, text
, n
);
2534 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2535 (void *)WMInsertTextEvent
);
2537 updateCursorPosition(tPtr
);
2541 tPtr
->flags
.waitingForSelection
= True
;
2548 if (tPtr
->flags
.pointerGrabbed
) {
2549 tPtr
->flags
.pointerGrabbed
= False
;
2550 XUngrabPointer(dpy
, CurrentTime
);
2554 if (tPtr
->flags
.waitingForSelection
)
2557 if (WMIsDraggingFromView(tPtr
->view
))
2558 WMDragImageFromView(tPtr
->view
, event
);
2563 static void handleEvents(XEvent
* event
, void *data
)
2565 Text
*tPtr
= (Text
*) data
;
2567 switch (event
->type
) {
2570 if (event
->xexpose
.count
!= 0)
2574 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2575 WMRealizeWidget(tPtr
->hS
);
2579 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2580 WMRealizeWidget(tPtr
->vS
);
2584 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2585 WMRealizeWidget(tPtr
->ruler
);
2590 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2596 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2599 tPtr
->flags
.focused
= True
;
2601 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2602 tPtr
->timerID
= WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY
, blinkCursor
, tPtr
);
2609 tPtr
->flags
.focused
= False
;
2612 if (tPtr
->timerID
) {
2613 WMDeleteTimerHandler(tPtr
->timerID
);
2614 tPtr
->timerID
= NULL
;
2622 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2624 WMEmptyArray(tPtr
->gfxItems
);
2627 WMDeleteTimerHandler(tPtr
->timerID
);
2629 WMReleaseFont(tPtr
->dFont
);
2630 WMReleaseColor(tPtr
->dColor
);
2631 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2632 WMRemoveNotificationObserver(tPtr
);
2634 WMFreeArray(tPtr
->xdndSourceTypes
);
2635 WMFreeArray(tPtr
->xdndDestinationTypes
);
2644 static void insertPlainText(Text
* tPtr
, const char *text
)
2646 const char *start
, *mark
;
2651 mark
= strchr(start
, '\n');
2653 tb
= WMCreateTextBlockWithText(tPtr
,
2655 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
- start
));
2657 tPtr
->flags
.first
= True
;
2659 if (start
&& strlen(start
)) {
2660 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2661 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2664 tPtr
->flags
.first
= False
;
2668 if (tPtr
->flags
.prepend
)
2669 WMPrependTextBlock(tPtr
, tb
);
2671 WMAppendTextBlock(tPtr
, tb
);
2675 static void rulerMoveCallBack(WMWidget
* w
, void *self
)
2677 Text
*tPtr
= (Text
*) self
;
2679 /* Parameter not used, but tell the compiler that it is ok */
2684 if (W_CLASS(tPtr
) != WC_Text
)
2690 static void rulerReleaseCallBack(WMWidget
* w
, void *self
)
2692 Text
*tPtr
= (Text
*) self
;
2694 /* Parameter not used, but tell the compiler that it is ok */
2699 if (W_CLASS(tPtr
) != WC_Text
)
2706 static WMArray
*dropDataTypes(WMView
* self
)
2708 return ((Text
*) self
->self
)->xdndSourceTypes
;
2711 static WMDragOperationType
wantedDropOperation(WMView
* self
)
2713 /* Parameter not used, but tell the compiler that it is ok */
2716 return WDOperationCopy
;
2719 static Bool
acceptDropOperation(WMView
* self
, WMDragOperationType allowedOperation
)
2721 /* Parameter not used, but tell the compiler that it is ok */
2724 return (allowedOperation
== WDOperationCopy
);
2727 static WMData
*fetchDragData(WMView
* self
, char *type
)
2729 TextBlock
*tb
= ((WMText
*) self
->self
)->currentTextBlock
;
2733 if (strcmp(type
, "text/plain")) {
2737 desc
= wmalloc(tb
->used
+ 1);
2738 memcpy(desc
, tb
->text
, tb
->used
);
2740 data
= WMCreateDataWithBytes(desc
, strlen(desc
) + 1);
2750 static WMDragSourceProcs _DragSourceProcs
= {
2752 wantedDropOperation
,
2754 acceptDropOperation
,
2760 static WMArray
*requiredDataTypes(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2762 /* Parameter not used, but tell the compiler that it is ok */
2764 (void) sourceDataTypes
;
2766 return ((Text
*) self
->self
)->xdndDestinationTypes
;
2769 static WMDragOperationType
allowedOperation(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2771 /* Parameter not used, but tell the compiler that it is ok */
2774 (void) sourceDataTypes
;
2776 return WDOperationCopy
;
2779 static void performDragOperation(WMView
* self
, WMArray
* dropData
, WMArray
* operations
, WMPoint
* dropLocation
)
2781 WMText
*tPtr
= (WMText
*) self
->self
;
2786 /* Parameter not used, but tell the compiler that it is ok */
2788 (void) dropLocation
;
2792 /* only one required type, implies only one drop data */
2794 /* get application/X-color if any */
2795 data
= (WMData
*) WMPopFromArray(dropData
);
2797 colorName
= (char *)WMDataBytes(data
);
2798 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
2801 WMSetTextSelectionColor(tPtr
, color
);
2802 WMReleaseColor(color
);
2808 static WMDragDestinationProcs _DragDestinationProcs
= {
2813 performDragOperation
,
2817 static char *getStream(WMText
* tPtr
, int sel
, int array
)
2819 TextBlock
*tb
= NULL
;
2821 unsigned long where
= 0;
2826 if (!(tb
= tPtr
->firstTextBlock
))
2830 (tPtr
->writer
) (tPtr
, (void *)text
);
2834 tb
= tPtr
->firstTextBlock
;
2837 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2839 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2841 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2842 && tb
!= tPtr
->firstTextBlock
) {
2843 text
= wrealloc(text
, where
+ 1);
2844 text
[where
++] = '\n';
2850 if (tb
->graphic
&& array
) {
2851 text
= wrealloc(text
, where
+ 4);
2852 text
[where
++] = 0xFA;
2853 text
[where
++] = (tb
->used
>> 8) & 0x0ff;
2854 text
[where
++] = tb
->used
& 0x0ff;
2855 text
[where
++] = tb
->allocated
; /* extra info */
2857 text
= wrealloc(text
, where
+ tb
->used
);
2858 memcpy(&text
[where
], tb
->text
, tb
->used
);
2861 } else if (sel
&& tb
->selected
) {
2863 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
2864 text
= wrealloc(text
, where
+ 1);
2865 text
[where
++] = '\n';
2871 text
= wrealloc(text
, where
+ (tb
->s_end
- tb
->s_begin
));
2872 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
], tb
->s_end
- tb
->s_begin
);
2873 where
+= tb
->s_end
- tb
->s_begin
;
2878 _gSnext
: tb
= tb
->next
;
2881 /* +1 for the end of string, let's be nice */
2882 text
= wrealloc(text
, where
+ 1);
2887 static void releaseStreamObjects(void *data
)
2893 static WMArray
*getStreamObjects(WMText
* tPtr
, int sel
)
2899 char *start
, *fa
, *desc
;
2901 stream
= getStream(tPtr
, sel
, 1);
2905 array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
2910 fa
= strchr(start
, 0xFA);
2912 if ((int)(fa
- start
) > 0) {
2914 desc
[(int)(fa
- start
)] = 0;
2915 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
2916 WMSetDataFormat(data
, TYPETEXT
);
2917 WMAddToArray(array
, (void *)data
);
2920 len
= *(fa
+ 1) * 0xff + *(fa
+ 2);
2921 data
= WMCreateDataWithBytes((void *)(fa
+ 4), len
);
2922 WMSetDataFormat(data
, *(fa
+ 3));
2923 WMAddToArray(array
, (void *)data
);
2924 start
= fa
+ len
+ 4;
2927 if (start
&& strlen(start
)) {
2928 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
2929 WMSetDataFormat(data
, TYPETEXT
);
2930 WMAddToArray(array
, (void *)data
);
2940 #define XDND_TEXT_DATA_TYPE "text/plain"
2941 #define XDND_COLOR_DATA_TYPE "application/X-color"
2942 static WMArray
*getXdndSourceTypeArray(void)
2944 WMArray
*types
= WMCreateArray(1);
2945 WMAddToArray(types
, XDND_TEXT_DATA_TYPE
);
2949 static WMArray
*getXdndDestinationTypeArray(void)
2951 WMArray
*types
= WMCreateArray(1);
2952 WMAddToArray(types
, XDND_COLOR_DATA_TYPE
);
2956 WMText
*WMCreateTextForDocumentType(WMWidget
* parent
, WMAction
* parser
, WMAction
* writer
)
2963 tPtr
= wmalloc(sizeof(Text
));
2964 tPtr
->widgetClass
= WC_Text
;
2965 tPtr
->view
= W_CreateView(W_VIEW(parent
));
2967 perror("could not create text's view\n");
2972 dpy
= tPtr
->view
->screen
->display
;
2973 scr
= tPtr
->view
->screen
;
2975 tPtr
->view
->self
= tPtr
;
2976 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
2977 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
2978 W_ResizeView(tPtr
->view
, 250, 200);
2980 tPtr
->dColor
= WMBlackColor(scr
);
2981 tPtr
->fgColor
= WMBlackColor(scr
);
2982 tPtr
->bgColor
= WMWhiteColor(scr
);
2983 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
2985 gcv
.graphics_exposures
= False
;
2986 gcv
.foreground
= W_PIXEL(scr
->gray
);
2987 gcv
.background
= W_PIXEL(scr
->darkGray
);
2988 gcv
.fill_style
= FillStippled
;
2989 /* why not use scr->stipple here? */
2990 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
, STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
2991 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
2992 GCForeground
| GCBackground
| GCStipple
2993 | GCFillStyle
| GCGraphicsExposures
, &gcv
);
2999 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
3001 tPtr
->view
->delegate
= &_TextViewDelegate
;
3003 tPtr
->delegate
= NULL
;
3006 tPtr
->timerID
= NULL
;
3009 WMCreateEventHandler(tPtr
->view
, ExposureMask
| StructureNotifyMask
3010 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
, handleEvents
, tPtr
);
3012 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
| ButtonPressMask
3013 | KeyReleaseMask
| KeyPressMask
| Button1MotionMask
, handleActionEvents
, tPtr
);
3015 WMAddNotificationObserver(ownershipObserver
, tPtr
, WMSelectionOwnerDidChangeNotification
, tPtr
);
3017 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3018 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3021 WMArray
*types
= WMCreateArray(2);
3022 WMAddToArray(types
, "application/X-color");
3023 WMAddToArray(types
, "application/X-image");
3024 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3028 /*WMAddNotificationObserver(fontChanged, tPtr,
3029 WMFontPanelDidChangeNotification, tPtr); */
3031 tPtr
->firstTextBlock
= NULL
;
3032 tPtr
->lastTextBlock
= NULL
;
3033 tPtr
->currentTextBlock
= NULL
;
3036 tPtr
->gfxItems
= WMCreateArray(4);
3038 tPtr
->parser
= parser
;
3039 tPtr
->writer
= writer
;
3041 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3042 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3044 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3046 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3047 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3048 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3050 tPtr
->cursor
.x
= -23;
3053 tPtr
->docHeight
= 0;
3054 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
, default_bullet
);
3055 tPtr
->db
= (Pixmap
) NULL
;
3056 tPtr
->bgPixmap
= NULL
;
3058 tPtr
->margins
= WMGetRulerMargins(NULL
);
3059 tPtr
->margins
->right
= tPtr
->visible
.w
;
3062 tPtr
->flags
.rulerShown
= False
;
3063 tPtr
->flags
.monoFont
= False
;
3064 tPtr
->flags
.focused
= False
;
3065 tPtr
->flags
.editable
= True
;
3066 tPtr
->flags
.ownsSelection
= False
;
3067 tPtr
->flags
.pointerGrabbed
= False
;
3068 tPtr
->flags
.extendSelection
= False
;
3069 tPtr
->flags
.frozen
= False
;
3070 tPtr
->flags
.cursorShown
= True
;
3071 tPtr
->flags
.acceptsGraphic
= False
;
3072 tPtr
->flags
.horizOnDemand
= False
;
3073 tPtr
->flags
.needsLayOut
= False
;
3074 tPtr
->flags
.ignoreNewLine
= False
;
3075 tPtr
->flags
.indentNewLine
= False
;
3076 tPtr
->flags
.laidOut
= False
;
3077 tPtr
->flags
.ownsSelection
= False
;
3078 tPtr
->flags
.waitingForSelection
= False
;
3079 tPtr
->flags
.prepend
= False
;
3080 tPtr
->flags
.isOverGraphic
= False
;
3081 tPtr
->flags
.relief
= WRSunken
;
3082 tPtr
->flags
.isOverGraphic
= 0;
3083 tPtr
->flags
.alignment
= WALeft
;
3084 tPtr
->flags
.first
= True
;
3086 tPtr
->xdndSourceTypes
= getXdndSourceTypeArray();
3087 tPtr
->xdndDestinationTypes
= getXdndDestinationTypeArray();
3092 void WMPrependTextStream(WMText
* tPtr
, const char *text
)
3094 CHECK_CLASS(tPtr
, WC_Text
);
3097 if (tPtr
->flags
.ownsSelection
)
3098 releaseSelection(tPtr
);
3100 updateScrollers(tPtr
);
3104 tPtr
->flags
.prepend
= True
;
3105 if (text
&& tPtr
->parser
)
3106 (tPtr
->parser
) (tPtr
, (void *)text
);
3108 insertPlainText(tPtr
, text
);
3110 tPtr
->flags
.needsLayOut
= True
;
3112 if (!tPtr
->flags
.frozen
) {
3113 layOutDocument(tPtr
);
3117 void WMAppendTextStream(WMText
* tPtr
, const char *text
)
3119 CHECK_CLASS(tPtr
, WC_Text
);
3122 if (tPtr
->flags
.ownsSelection
)
3123 releaseSelection(tPtr
);
3125 updateScrollers(tPtr
);
3129 tPtr
->flags
.prepend
= False
;
3130 if (text
&& tPtr
->parser
)
3131 (tPtr
->parser
) (tPtr
, (void *)text
);
3133 insertPlainText(tPtr
, text
);
3135 tPtr
->flags
.needsLayOut
= True
;
3136 if (tPtr
->currentTextBlock
) {
3137 if (tPtr
->currentTextBlock
->graphic
)
3140 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3143 if (!tPtr
->flags
.frozen
) {
3144 layOutDocument(tPtr
);
3148 char *WMGetTextStream(WMText
* tPtr
)
3150 CHECK_CLASS(tPtr
, WC_Text
);
3152 return getStream(tPtr
, 0, 0);
3155 char *WMGetTextSelectedStream(WMText
* tPtr
)
3157 CHECK_CLASS(tPtr
, WC_Text
);
3159 return getStream(tPtr
, 1, 0);
3162 WMArray
*WMGetTextObjects(WMText
* tPtr
)
3164 CHECK_CLASS(tPtr
, WC_Text
);
3166 return getStreamObjects(tPtr
, 0);
3169 WMArray
*WMGetTextSelectedObjects(WMText
* tPtr
)
3171 CHECK_CLASS(tPtr
, WC_Text
);
3173 return getStreamObjects(tPtr
, 1);
3176 void WMSetTextDelegate(WMText
* tPtr
, WMTextDelegate
* delegate
)
3178 CHECK_CLASS(tPtr
, WC_Text
);
3180 tPtr
->delegate
= delegate
;
3183 void *WMCreateTextBlockWithObject(WMText
* tPtr
, WMWidget
* w
,
3184 const char *description
, WMColor
* color
,
3185 unsigned short first
, unsigned short extraInfo
)
3189 if (!w
|| !description
|| !color
)
3192 tb
= wmalloc(sizeof(TextBlock
));
3194 tb
->text
= wstrdup(description
);
3195 tb
->used
= strlen(description
);
3198 tb
->color
= WMRetainColor(color
);
3199 tb
->marginN
= newMargin(tPtr
, NULL
);
3200 tb
->allocated
= extraInfo
;
3205 tb
->underlined
= False
;
3206 tb
->selected
= False
;
3208 tb
->sections
= NULL
;
3216 void *WMCreateTextBlockWithPixmap(WMText
* tPtr
, WMPixmap
* p
,
3217 const char *description
, WMColor
* color
,
3218 unsigned short first
, unsigned short extraInfo
)
3222 if (!p
|| !description
|| !color
)
3225 tb
= wmalloc(sizeof(TextBlock
));
3227 tb
->text
= wstrdup(description
);
3228 tb
->used
= strlen(description
);
3230 tb
->d
.pixmap
= WMRetainPixmap(p
);
3231 tb
->color
= WMRetainColor(color
);
3232 tb
->marginN
= newMargin(tPtr
, NULL
);
3233 tb
->allocated
= extraInfo
;
3238 tb
->underlined
= False
;
3239 tb
->selected
= False
;
3241 tb
->sections
= NULL
;
3249 void *WMCreateTextBlockWithText(WMText
* tPtr
, const char *text
, WMFont
* font
, WMColor
* color
,
3250 unsigned short first
, unsigned short len
)
3254 if (!font
|| !color
)
3257 tb
= wmalloc(sizeof(TextBlock
));
3259 tb
->allocated
= reqBlockSize(len
);
3260 tb
->text
= (char *)wmalloc(tb
->allocated
);
3262 if (len
< 1 || !text
|| (*text
== '\n' && len
== 1)) {
3267 memcpy(tb
->text
, text
, len
);
3271 tb
->text
[tb
->used
] = 0;
3273 tb
->d
.font
= WMRetainFont(font
);
3274 tb
->color
= WMRetainColor(color
);
3275 tb
->marginN
= newMargin(tPtr
, NULL
);
3278 tb
->graphic
= False
;
3279 tb
->underlined
= False
;
3280 tb
->selected
= False
;
3282 tb
->sections
= NULL
;
3290 WMSetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int first
,
3291 unsigned int kanji
, unsigned int underlined
, int script
, WMRulerMargins
* margins
)
3293 TextBlock
*tb
= (TextBlock
*) vtb
;
3299 tb
->underlined
= underlined
;
3300 tb
->script
= script
;
3301 tb
->marginN
= newMargin(tPtr
, margins
);
3305 WMGetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int *first
,
3306 unsigned int *kanji
, unsigned int *underlined
, int *script
, WMRulerMargins
*margins
)
3308 TextBlock
*tb
= (TextBlock
*) vtb
;
3317 *underlined
= tb
->underlined
;
3319 *script
= tb
->script
;
3321 *margins
= tPtr
->margins
[tb
->marginN
];
3324 static int prepareTextBlock(WMText
*tPtr
, TextBlock
*tb
)
3328 WMWidget
*w
= tb
->d
.widget
;
3329 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3330 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3331 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3334 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3338 tPtr
->tpos
= tb
->used
;
3341 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3342 tb
->next
= tb
->prior
= NULL
;
3344 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3349 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3356 void WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3358 TextBlock
*tb
= (TextBlock
*) vtb
;
3360 if (!tb
|| !prepareTextBlock(tPtr
, tb
))
3363 tb
->next
= tPtr
->currentTextBlock
;
3364 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3365 if (tPtr
->currentTextBlock
->prior
)
3366 tPtr
->currentTextBlock
->prior
->next
= tb
;
3368 tPtr
->currentTextBlock
->prior
= tb
;
3370 tPtr
->firstTextBlock
= tb
;
3372 tPtr
->currentTextBlock
= tb
;
3375 void WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3377 TextBlock
*tb
= (TextBlock
*) vtb
;
3379 if (!tb
|| !prepareTextBlock(tPtr
, tb
))
3382 tb
->next
= tPtr
->currentTextBlock
->next
;
3383 tb
->prior
= tPtr
->currentTextBlock
;
3384 if (tPtr
->currentTextBlock
->next
)
3385 tPtr
->currentTextBlock
->next
->prior
= tb
;
3387 tPtr
->currentTextBlock
->next
= tb
;
3390 tPtr
->lastTextBlock
= tb
;
3392 tPtr
->currentTextBlock
= tb
;
3395 void *WMRemoveTextBlock(WMText
* tPtr
)
3397 TextBlock
*tb
= NULL
;
3399 if (!tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
|| !tPtr
->currentTextBlock
) {
3403 tb
= tPtr
->currentTextBlock
;
3405 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3408 WMUnmapWidget(tb
->d
.widget
);
3412 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3413 if (tPtr
->currentTextBlock
->next
)
3414 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3416 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3417 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3419 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3420 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3421 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3422 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3424 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3425 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3426 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3433 static void destroyWidget(WMWidget
* widget
)
3435 WMDestroyWidget(widget
);
3436 // -- never do this -- wfree(widget);
3440 void WMDestroyTextBlock(WMText
* tPtr
, void *vtb
)
3442 TextBlock
*tb
= (TextBlock
*) vtb
;
3444 /* Parameter not used, but tell the compiler that it is ok */
3452 /* naturally, there's a danger to destroying widgets whose action
3453 * brings us here: ie. press a button to destroy it...
3454 * need to find a safer way. till then... this stays commented out */
3455 /* 5 months later... destroy it 10 seconds after now which should
3456 * be enough time for the widget's action to be completed... :-) */
3457 /* This is a bad assumption. Just destroy the widget here.
3458 * if the caller needs it, it can protect it with W_RetainView()
3459 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3460 WMDestroyWidget(tb
->d
.widget
);
3462 WMReleasePixmap(tb
->d
.pixmap
);
3465 WMReleaseFont(tb
->d
.font
);
3468 WMReleaseColor(tb
->color
);
3469 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3471 wfree(tb
->sections
);
3476 void WMSetTextForegroundColor(WMText
* tPtr
, WMColor
* color
)
3479 WMReleaseColor(tPtr
->fgColor
);
3481 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3486 void WMSetTextBackgroundColor(WMText
* tPtr
, WMColor
* color
)
3489 WMReleaseColor(tPtr
->bgColor
);
3491 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3492 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3497 void WMSetTextBackgroundPixmap(WMText
* tPtr
, WMPixmap
* pixmap
)
3500 WMReleasePixmap(tPtr
->bgPixmap
);
3503 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3505 tPtr
->bgPixmap
= NULL
;
3508 void WMSetTextRelief(WMText
* tPtr
, WMReliefType relief
)
3510 tPtr
->flags
.relief
= relief
;
3511 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3514 void WMSetTextHasHorizontalScroller(WMText
* tPtr
, Bool shouldhave
)
3516 if (shouldhave
&& !tPtr
->hS
) {
3517 tPtr
->hS
= WMCreateScroller(tPtr
);
3518 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3519 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3520 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3521 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3522 WMMapWidget(tPtr
->hS
);
3523 } else if (!shouldhave
&& tPtr
->hS
) {
3524 WMUnmapWidget(tPtr
->hS
);
3525 WMDestroyWidget(tPtr
->hS
);
3531 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3534 void WMSetTextHasRuler(WMText
* tPtr
, Bool shouldhave
)
3536 if (shouldhave
&& !tPtr
->ruler
) {
3537 tPtr
->ruler
= WMCreateRuler(tPtr
);
3538 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3539 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3540 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3541 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3542 } else if (!shouldhave
&& tPtr
->ruler
) {
3543 WMShowTextRuler(tPtr
, False
);
3544 WMDestroyWidget(tPtr
->ruler
);
3547 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3550 void WMShowTextRuler(WMText
* tPtr
, Bool show
)
3555 if (tPtr
->flags
.monoFont
)
3558 tPtr
->flags
.rulerShown
= show
;
3560 WMMapWidget(tPtr
->ruler
);
3562 WMUnmapWidget(tPtr
->ruler
);
3565 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3568 Bool
WMGetTextRulerShown(WMText
* tPtr
)
3573 return tPtr
->flags
.rulerShown
;
3576 void WMSetTextHasVerticalScroller(WMText
* tPtr
, Bool shouldhave
)
3578 if (shouldhave
&& !tPtr
->vS
) {
3579 tPtr
->vS
= WMCreateScroller(tPtr
);
3580 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3581 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3582 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3583 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3584 WMMapWidget(tPtr
->vS
);
3585 } else if (!shouldhave
&& tPtr
->vS
) {
3586 WMUnmapWidget(tPtr
->vS
);
3587 WMDestroyWidget(tPtr
->vS
);
3593 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3596 Bool
WMScrollText(WMText
* tPtr
, int amount
)
3598 Bool scroll
= False
;
3600 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3604 if (tPtr
->vpos
> 0) {
3605 if (tPtr
->vpos
> abs(amount
))
3606 tPtr
->vpos
+= amount
;
3612 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3613 if (tPtr
->vpos
< limit
) {
3614 if (tPtr
->vpos
< limit
- amount
)
3615 tPtr
->vpos
+= amount
;
3622 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3623 updateScrollers(tPtr
);
3626 tPtr
->prevVpos
= tPtr
->vpos
;
3630 Bool
WMPageText(WMText
* tPtr
, Bool direction
)
3632 if (!tPtr
->view
->flags
.realized
)
3635 return WMScrollText(tPtr
, direction
? tPtr
->visible
.h
: -tPtr
->visible
.h
);
3638 void WMSetTextEditable(WMText
* tPtr
, Bool editable
)
3640 tPtr
->flags
.editable
= editable
;
3643 int WMGetTextEditable(WMText
* tPtr
)
3645 return tPtr
->flags
.editable
;
3648 void WMSetTextIndentNewLines(WMText
* tPtr
, Bool indent
)
3650 tPtr
->flags
.indentNewLine
= indent
;
3653 void WMSetTextIgnoresNewline(WMText
* tPtr
, Bool ignore
)
3655 tPtr
->flags
.ignoreNewLine
= ignore
;
3658 Bool
WMGetTextIgnoresNewline(WMText
* tPtr
)
3660 return tPtr
->flags
.ignoreNewLine
;
3663 void WMSetTextUsesMonoFont(WMText
* tPtr
, Bool mono
)
3666 if (tPtr
->flags
.rulerShown
)
3667 WMShowTextRuler(tPtr
, False
);
3668 if (tPtr
->flags
.alignment
!= WALeft
)
3669 tPtr
->flags
.alignment
= WALeft
;
3672 tPtr
->flags
.monoFont
= mono
;
3673 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3676 Bool
WMGetTextUsesMonoFont(WMText
* tPtr
)
3678 return tPtr
->flags
.monoFont
;
3681 void WMSetTextDefaultFont(WMText
* tPtr
, WMFont
* font
)
3684 WMReleaseFont(tPtr
->dFont
);
3687 tPtr
->dFont
= WMRetainFont(font
);
3689 tPtr
->dFont
= WMSystemFontOfSize(tPtr
->view
->screen
, 12);
3693 WMFont
*WMGetTextDefaultFont(WMText
* tPtr
)
3695 return WMRetainFont(tPtr
->dFont
);
3698 void WMSetTextDefaultColor(WMText
* tPtr
, WMColor
* color
)
3701 WMReleaseColor(tPtr
->dColor
);
3704 tPtr
->dColor
= WMRetainColor(color
);
3706 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3710 WMColor
*WMGetTextDefaultColor(WMText
* tPtr
)
3712 return tPtr
->dColor
;
3715 void WMSetTextAlignment(WMText
* tPtr
, WMAlignment alignment
)
3717 if (tPtr
->flags
.monoFont
)
3718 tPtr
->flags
.alignment
= WALeft
;
3720 tPtr
->flags
.alignment
= alignment
;
3724 int WMGetTextInsertType(WMText
* tPtr
)
3726 return tPtr
->flags
.prepend
;
3729 void WMSetTextSelectionColor(WMText
* tPtr
, WMColor
* color
)
3731 setSelectionProperty(tPtr
, NULL
, color
, -1);
3734 WMColor
*WMGetTextSelectionColor(WMText
* tPtr
)
3738 tb
= tPtr
->currentTextBlock
;
3740 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3749 void WMSetTextSelectionFont(WMText
* tPtr
, WMFont
* font
)
3751 setSelectionProperty(tPtr
, font
, NULL
, -1);
3754 WMFont
*WMGetTextSelectionFont(WMText
* tPtr
)
3758 tb
= tPtr
->currentTextBlock
;
3760 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3767 tb
= getFirstNonGraphicBlockFor(tb
, 1);
3771 return (tb
->selected
? tb
->d
.font
: NULL
);
3774 void WMSetTextSelectionUnderlined(WMText
* tPtr
, int underlined
)
3777 if (underlined
!= 0 && underlined
!= 1)
3780 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
3783 int WMGetTextSelectionUnderlined(WMText
* tPtr
)
3787 tb
= tPtr
->currentTextBlock
;
3789 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3795 return tb
->underlined
;
3798 void WMFreezeText(WMText
* tPtr
)
3800 tPtr
->flags
.frozen
= True
;
3803 void WMThawText(WMText
* tPtr
)
3805 tPtr
->flags
.frozen
= False
;
3807 if (tPtr
->flags
.monoFont
) {
3808 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
3811 /* make sure to unmap widgets no matter where they are */
3812 /* they'll be later remapped if needed by paintText */
3813 for (j
= 0; j
< c
; j
++) {
3814 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
3815 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
3816 WMUnmapWidget(tb
->d
.widget
);
3821 tPtr
->flags
.laidOut
= False
;
3822 layOutDocument(tPtr
);
3823 updateScrollers(tPtr
);
3825 tPtr
->flags
.needsLayOut
= False
;
3829 /* find first occurence of a string */
3830 static const char *mystrstr(const char *haystack
, const char *needle
, unsigned short len
, const char *end
, Bool caseSensitive
)
3834 if (!haystack
|| !needle
|| !end
)
3837 for (ptr
= haystack
; ptr
< end
; ptr
++) {
3838 if (caseSensitive
) {
3839 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3843 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3851 /* find last occurence of a string */
3852 static const char *mystrrstr(const char *haystack
, const char *needle
, unsigned short len
, const char *end
, Bool caseSensitive
)
3856 if (!haystack
|| !needle
|| !end
)
3859 for (ptr
= haystack
- 2; ptr
> end
; ptr
--) {
3860 if (caseSensitive
) {
3861 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3864 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3872 Bool
WMFindInTextStream(WMText
* tPtr
, const char *needle
, Bool direction
, Bool caseSensitive
)
3875 const char *mark
= NULL
;
3879 if (!(tb
= tPtr
->currentTextBlock
)) {
3880 if (!(tb
= ((direction
> 0) ? tPtr
->firstTextBlock
: tPtr
->lastTextBlock
))) {
3884 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3885 tb = (direction>0) ? tb->next : tb->prior; */
3886 if (tb
!= tPtr
->lastTextBlock
)
3890 tb
= tPtr
->currentTextBlock
;
3896 if (direction
> 0) {
3897 if (pos
+ 1 < tb
->used
)
3900 if (tb
->used
- pos
> 0 && pos
> 0) {
3901 mark
= mystrstr(&tb
->text
[pos
], needle
,
3902 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
3915 mark
= mystrrstr(&tb
->text
[pos
], needle
,
3916 strlen(needle
), tb
->text
, caseSensitive
);
3927 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
3929 tPtr
->tpos
= (int)(mark
- tb
->text
);
3930 tPtr
->currentTextBlock
= tb
;
3931 updateCursorPosition(tPtr
);
3932 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
3933 tPtr
->sel
.h
= tPtr
->cursor
.h
- 10;
3934 tPtr
->sel
.x
= tPtr
->cursor
.x
+ 1;
3935 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
3936 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
3937 tPtr
->docWidth
- tPtr
->sel
.x
);
3938 tPtr
->flags
.ownsSelection
= True
;
3945 tb
= (direction
> 0) ? tb
->next
: tb
->prior
;
3947 pos
= (direction
> 0) ? 0 : tb
->used
;
3954 Bool
WMReplaceTextSelection(WMText
* tPtr
, char *replacement
)
3956 if (!tPtr
->flags
.ownsSelection
)
3959 removeSelection(tPtr
);
3962 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
3963 updateCursorPosition(tPtr
);