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 /* These id are used when sharing the selected text between applications */
219 static Atom XA_Targets
= None
;
220 static Atom XA_Format_Text
= None
;
221 static Atom XA_Format_Compound_Text
= None
;
223 static void handleEvents(XEvent
* event
, void *data
);
224 static void layOutDocument(Text
* tPtr
);
225 static void updateScrollers(Text
* tPtr
);
227 static int getMarginNumber(Text
* tPtr
, WMRulerMargins
* margins
)
231 for (i
= 0; i
< tPtr
->nMargins
; i
++) {
233 if (WMIsMarginEqualToMargin(&tPtr
->margins
[i
], margins
))
240 static int newMargin(Text
* tPtr
, WMRulerMargins
* margins
)
245 tPtr
->margins
[0].retainCount
++;
249 n
= getMarginNumber(tPtr
, margins
);
253 if (tPtr
->nMargins
>= 127) {
254 n
= tPtr
->nMargins
- 1;
258 tPtr
->margins
= wrealloc(tPtr
->margins
, (++tPtr
->nMargins
) * sizeof(WMRulerMargins
));
260 n
= tPtr
->nMargins
- 1;
261 tPtr
->margins
[n
].left
= margins
->left
;
262 tPtr
->margins
[n
].first
= margins
->first
;
263 tPtr
->margins
[n
].body
= margins
->body
;
264 tPtr
->margins
[n
].right
= margins
->right
;
265 /* for each tab... */
266 tPtr
->margins
[n
].retainCount
= 1;
268 tPtr
->margins
[n
].retainCount
++;
274 static Bool
sectionWasSelected(Text
* tPtr
, TextBlock
* tb
, XRectangle
* rect
, int s
)
276 unsigned short i
, w
, lw
, selected
= False
, extend
= False
;
279 /* if selection rectangle completely encloses the section */
280 if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
281 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
<= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)) {
283 sel
.w
= tPtr
->visible
.w
;
284 selected
= extend
= True
;
286 /* or if it starts on a line and then goes further down */
287 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
288 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
<= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
289 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)) {
290 sel
.x
= WMAX(tPtr
->sel
.x
, tPtr
->clicked
.x
);
291 sel
.w
= tPtr
->visible
.w
;
292 selected
= extend
= True
;
294 /* or if it begins before a line, but ends on it */
295 } else if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
296 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
>= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
297 && (tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)) {
299 if (1 || tPtr
->sel
.x
+ tPtr
->sel
.w
> tPtr
->clicked
.x
)
300 sel
.w
= tPtr
->sel
.x
+ tPtr
->sel
.w
;
307 /* or if the selection rectangle lies entirely within a line */
308 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
309 && (tPtr
->sel
.w
>= 2)
310 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
>= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)) {
319 /* if not within (modified) selection rectangle */
320 if (tb
->sections
[s
].x
> sel
.x
+ sel
.w
|| tb
->sections
[s
].x
+ tb
->sections
[s
].w
< sel
.x
)
324 if (tb
->sections
[s
].x
+ tb
->sections
[s
].w
<= sel
.x
+ sel
.w
&& tb
->sections
[s
].x
>= sel
.x
) {
325 rect
->width
= tb
->sections
[s
].w
;
326 rect
->x
= tb
->sections
[s
].x
;
331 i
= tb
->sections
[s
].begin
;
334 if (0 && tb
->sections
[s
].x
>= sel
.x
) {
335 tb
->s_begin
= tb
->sections
[s
].begin
;
339 while (++i
<= tb
->sections
[s
].end
) {
341 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
- 1]), 1);
344 if (lw
+ tb
->sections
[s
].x
>= sel
.x
|| i
== tb
->sections
[s
].end
) {
347 tb
->s_begin
= (tb
->selected
? WMIN(tb
->s_begin
, i
) : i
);
352 if (i
> tb
->sections
[s
].end
) {
353 printf("WasSelected: (i > tb->sections[s].end) \n");
357 _selEnd
: rect
->x
= tb
->sections
[s
].x
+ lw
;
359 while (++i
<= tb
->sections
[s
].end
) {
361 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
- 1]), 1);
364 if (lw
+ rect
->x
>= sel
.x
+ sel
.w
|| i
== tb
->sections
[s
].end
) {
366 if (i
!= tb
->sections
[s
].end
) {
372 if (tb
->sections
[s
].last
&& sel
.x
+ sel
.w
373 >= tb
->sections
[s
].x
+ tb
->sections
[s
].w
&& extend
) {
374 rect
->width
+= (tPtr
->visible
.w
- rect
->x
- lw
);
377 tb
->s_end
= (tb
->selected
? WMAX(tb
->s_end
, i
) : i
);
386 rect
->y
= tb
->sections
[s
]._y
- tPtr
->vpos
;
387 rect
->height
= tb
->sections
[s
].h
;
389 printf("DEBUG: graphic s%d h%d\n", s
, tb
->sections
[s
].h
);
396 static void setSelectionProperty(WMText
* tPtr
, WMFont
* font
, WMColor
* color
, int underlined
)
401 tb
= tPtr
->firstTextBlock
;
402 if (!tb
|| !tPtr
->flags
.ownsSelection
)
405 if (font
&& (!color
|| underlined
== -1))
409 if (tPtr
->flags
.monoFont
|| tb
->selected
) {
411 if (tPtr
->flags
.monoFont
|| (tb
->s_end
- tb
->s_begin
== tb
->used
)
416 WMReleaseFont(tb
->d
.font
);
417 tb
->d
.font
= WMRetainFont(font
);
419 } else if (underlined
!= -1) {
420 tb
->underlined
= underlined
;
422 WMReleaseColor(tb
->color
);
423 tb
->color
= WMRetainColor(color
);
426 } else if (tb
->s_end
<= tb
->used
&& tb
->s_begin
< tb
->s_end
) {
428 TextBlock
*midtb
, *otb
= tb
;
430 if (underlined
!= -1) {
431 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
432 &(tb
->text
[tb
->s_begin
]),
433 tb
->d
.font
, tb
->color
,
435 (tb
->s_end
- tb
->s_begin
));
437 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
438 &(tb
->text
[tb
->s_begin
]),
439 (isFont
? font
: tb
->d
.
442 color
: color
), False
,
443 (tb
->s_end
- tb
->s_begin
));
447 if (underlined
!= -1) {
448 midtb
->underlined
= underlined
;
450 midtb
->underlined
= otb
->underlined
;
453 midtb
->selected
= !True
;
455 midtb
->s_end
= midtb
->used
;
456 tPtr
->currentTextBlock
= tb
;
457 WMAppendTextBlock(tPtr
, midtb
);
458 tb
= tPtr
->currentTextBlock
;
461 if (otb
->used
- otb
->s_end
> 0) {
464 WMCreateTextBlockWithText(tPtr
,
465 &(otb
->text
[otb
->s_end
]), otb
->d
.font
,
466 otb
->color
, False
, otb
->used
- otb
->s_end
);
469 ntb
->underlined
= otb
->underlined
;
470 ntb
->selected
= False
;
471 WMAppendTextBlock(tPtr
, ntb
);
472 tb
= tPtr
->currentTextBlock
;
477 tPtr
->currentTextBlock
= midtb
;
480 otb
->selected
= False
;
481 otb
->used
= otb
->s_begin
;
488 tPtr
->flags
.needsLayOut
= True
;
491 /* in case the size changed... */
492 if (isFont
&& tPtr
->currentTextBlock
) {
493 TextBlock
*tb
= tPtr
->currentTextBlock
;
495 printf("%d %d %d\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
496 tPtr
->sel
.y
= 3 + tb
->sections
[0]._y
;
497 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
- 1]._y
- tb
->sections
[0]._y
;
498 tPtr
->sel
.w
= tb
->sections
[tb
->nsections
- 1].w
;
499 if (tb
->sections
[tb
->nsections
- 1]._y
!= tb
->sections
[0]._y
) {
502 printf("%d %d %d\n\n\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
507 static Bool
removeSelection(Text
* tPtr
)
509 TextBlock
*tb
= NULL
;
512 if (!(tb
= tPtr
->firstTextBlock
))
517 if (!first
&& !tb
->graphic
) {
518 WMReleaseFont(tPtr
->dFont
);
519 tPtr
->dFont
= WMRetainFont(tb
->d
.font
);
523 if ((tb
->s_end
- tb
->s_begin
== tb
->used
) || tb
->graphic
) {
524 tPtr
->currentTextBlock
= tb
;
527 } else if (tb
->prior
) {
528 if (tb
->prior
->graphic
)
531 tPtr
->tpos
= tb
->prior
->used
;
535 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
536 tb
= tPtr
->currentTextBlock
;
539 } else if (tb
->s_end
<= tb
->used
) {
540 memmove(&(tb
->text
[tb
->s_begin
]), &(tb
->text
[tb
->s_end
]), tb
->used
- tb
->s_end
);
541 tb
->used
-= (tb
->s_end
- tb
->s_begin
);
542 tb
->selected
= False
;
543 tPtr
->tpos
= tb
->s_begin
;
553 static TextBlock
*getFirstNonGraphicBlockFor(TextBlock
* tb
, short dir
)
555 TextBlock
*hold
= tb
;
563 tb
= (dir
? tb
->next
: tb
->prior
);
571 tb
= (dir
? tb
->prior
: tb
->next
);
581 static Bool
updateStartForCurrentTextBlock(Text
* tPtr
, int x
, int y
, int *dir
, TextBlock
* tb
)
583 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
584 tb
= getFirstNonGraphicBlockFor(tb
, *dir
);
589 tPtr
->currentTextBlock
= (*dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
596 layOutDocument(tPtr
);
600 *dir
= !(y
<= tb
->sections
[0].y
);
602 if ((y
<= tb
->sections
[0]._y
+ tb
->sections
[0].h
)
603 && (y
>= tb
->sections
[0]._y
)) {
604 /* if it's on the same line */
605 if (x
< tb
->sections
[0].x
)
609 if ((y
<= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
610 && (y
>= tb
->sections
[tb
->nsections
- 1]._y
)) {
611 /* if it's on the same line */
612 if (x
> tb
->sections
[tb
->nsections
- 1].x
)
620 static void paintText(Text
* tPtr
)
625 int len
, y
, c
, s
, done
= False
, dir
; /* dir 1 = down */
626 WMScreen
*scr
= tPtr
->view
->screen
;
627 Display
*dpy
= tPtr
->view
->screen
->display
;
628 Window win
= tPtr
->view
->window
;
631 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
634 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(tPtr
->bgColor
), 0, 0, tPtr
->visible
.w
, tPtr
->visible
.h
);
636 if (tPtr
->bgPixmap
) {
637 WMDrawPixmap(tPtr
->bgPixmap
, tPtr
->db
,
638 (tPtr
->visible
.w
- tPtr
->visible
.x
- tPtr
->bgPixmap
->width
) / 2,
639 (tPtr
->visible
.h
- tPtr
->visible
.y
- tPtr
->bgPixmap
->height
) / 2);
642 if (!(tb
= tPtr
->currentTextBlock
)) {
643 if (!(tb
= tPtr
->firstTextBlock
)) {
650 /* first, which direction? Don't waste time looking all over,
651 since the parts to be drawn will most likely be near what
652 was previously drawn */
653 if (!updateStartForCurrentTextBlock(tPtr
, 0, tPtr
->vpos
, &dir
, tb
))
658 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
662 if (tPtr
->vpos
<= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
665 if (tPtr
->vpos
>= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
683 /* first, place all text that can be viewed */
684 while (!done
&& tb
) {
690 tb
->selected
= False
;
692 for (s
= 0; s
< tb
->nsections
&& !done
; s
++) {
694 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
699 if (tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
702 if (tPtr
->flags
.monoFont
) {
704 color
= tPtr
->fgColor
;
710 if (tPtr
->flags
.ownsSelection
) {
713 if (sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
715 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(scr
->gray
),
716 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
720 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
721 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
722 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
723 WMDrawString(scr
, tPtr
->db
, color
, font
, tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
725 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
726 XDrawLine(dpy
, tPtr
->db
, WMColorGC(color
),
727 tb
->sections
[s
].x
- tPtr
->hpos
,
729 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
, y
+ font
->y
+ 1);
732 tb
= (!done
? tb
->next
: NULL
);
735 /* now , show all graphic items that can be viewed */
736 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
737 if (c
> 0 && !tPtr
->flags
.monoFont
) {
740 for (j
= 0; j
< c
; j
++) {
741 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
743 /* if it's not viewable, and mapped, unmap it */
744 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
745 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
748 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
749 WMUnmapWidget(tb
->d
.widget
);
753 /* if it's viewable, and not mapped, map it */
755 W_View
*view
= W_VIEW(tb
->d
.widget
);
757 if (!view
->flags
.realized
)
758 WMRealizeWidget(tb
->d
.widget
);
759 if (!view
->flags
.mapped
) {
760 XMapWindow(view
->screen
->display
, view
->window
);
761 XFlush(view
->screen
->display
);
762 view
->flags
.mapped
= 1;
767 WMMoveWidget(tb
->d
.widget
,
768 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
769 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
770 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
773 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
774 tb
->sections
[0].x
- tPtr
->hpos
,
775 tb
->sections
[0].y
- tPtr
->vpos
);
776 h
= tb
->d
.pixmap
->height
+ 1;
780 if (tPtr
->flags
.ownsSelection
) {
783 if (sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
784 Drawable d
= (0 && tb
->object
?
785 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
788 XFillRectangle(dpy
, d
, tPtr
->stippledGC
,
789 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
790 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
794 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
795 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
796 tb
->sections
[0].x
- tPtr
->hpos
,
797 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
798 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
799 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
806 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
&& tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
807 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
808 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tPtr
->fgColor
),
809 tPtr
->cursor
.x
, y
, tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
812 XCopyArea(dpy
, tPtr
->db
, win
, WMColorGC(tPtr
->bgColor
), 0, 0,
813 tPtr
->visible
.w
, tPtr
->visible
.h
, tPtr
->visible
.x
, tPtr
->visible
.y
);
815 W_DrawRelief(scr
, win
, 0, 0, tPtr
->view
->size
.width
, tPtr
->view
->size
.height
, tPtr
->flags
.relief
);
817 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
818 XDrawLine(dpy
, win
, WMColorGC(tPtr
->fgColor
), 2, 42, tPtr
->view
->size
.width
- 4, 42);
822 static void mouseOverObject(Text
* tPtr
, int x
, int y
)
827 x
-= tPtr
->visible
.x
;
829 y
-= tPtr
->visible
.y
;
832 if (tPtr
->flags
.ownsSelection
) {
834 && tPtr
->sel
.y
<= y
&& tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
&& tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
835 tPtr
->flags
.isOverGraphic
= 1;
841 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
844 tPtr
->flags
.isOverGraphic
= 0;
846 for (j
= 0; j
< c
; j
++) {
847 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
849 if (!tb
|| !tb
->sections
) {
850 tPtr
->flags
.isOverGraphic
= 0;
855 if (tb
->sections
[0].x
<= x
856 && tb
->sections
[0].y
<= y
857 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
858 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
859 tPtr
->flags
.isOverGraphic
= 3;
869 tPtr
->flags
.isOverGraphic
= 0;
871 tPtr
->view
->attribs
.cursor
= (result
? tPtr
->view
->screen
->defaultCursor
: tPtr
->view
->screen
->textCursor
);
873 XSetWindowAttributes attribs
;
874 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
875 XChangeWindowAttributes(tPtr
->view
->screen
->display
, tPtr
->view
->window
, CWCursor
, &attribs
);
881 static void blinkCursor(void *data
)
883 Text
*tPtr
= (Text
*) data
;
885 if (tPtr
->flags
.cursorShown
) {
886 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
, blinkCursor
, data
);
888 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
, blinkCursor
, data
);
891 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
895 static void updateCursorPosition(Text
* tPtr
)
897 TextBlock
*tb
= NULL
;
900 if (tPtr
->flags
.needsLayOut
)
901 layOutDocument(tPtr
);
903 if (!(tb
= tPtr
->currentTextBlock
)) {
904 if (!(tb
= tPtr
->firstTextBlock
)) {
905 WMFont
*font
= tPtr
->dFont
;
907 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
917 y
= tb
->sections
[0].y
;
918 h
= tb
->sections
[0].h
;
919 x
= tb
->sections
[0].x
;
921 } else if (tb
->graphic
) {
922 y
= tb
->sections
[0].y
;
923 h
= tb
->sections
[0].h
;
924 x
= tb
->sections
[0].x
;
926 x
+= tb
->sections
[0].w
;
929 if (tPtr
->tpos
> tb
->used
)
930 tPtr
->tpos
= tb
->used
;
932 for (s
= 0; s
< tb
->nsections
- 1; s
++) {
934 if (tPtr
->tpos
>= tb
->sections
[s
].begin
&& tPtr
->tpos
<= tb
->sections
[s
].end
)
938 y
= tb
->sections
[s
]._y
;
939 h
= tb
->sections
[s
].h
;
940 x
= tb
->sections
[s
].x
+ WMWidthOfString((tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
),
941 &tb
->text
[tb
->sections
[s
].begin
],
942 tPtr
->tpos
- tb
->sections
[s
].begin
);
949 /* scroll the bars if the cursor is not visible */
950 if (tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
951 if (tPtr
->cursor
.y
+ tPtr
->cursor
.h
> tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
) {
953 (tPtr
->cursor
.y
+ tPtr
->cursor
.h
+ 10
954 - (tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
));
955 } else if (tPtr
->cursor
.y
< tPtr
->vpos
+ tPtr
->visible
.y
) {
956 tPtr
->vpos
-= (tPtr
->vpos
+ tPtr
->visible
.y
- tPtr
->cursor
.y
);
961 updateScrollers(tPtr
);
964 static void cursorToTextPosition(Text
* tPtr
, int x
, int y
)
966 TextBlock
*tb
= NULL
;
967 int done
= False
, s
, pos
, len
, _w
, _y
, dir
= 1; /* 1 == "down" */
970 if (tPtr
->flags
.needsLayOut
)
971 layOutDocument(tPtr
);
973 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
977 x
-= (tPtr
->visible
.x
- 2);
981 /* clicked is relative to document, not window... */
985 if (!(tb
= tPtr
->currentTextBlock
)) {
986 if (!(tb
= tPtr
->firstTextBlock
)) {
987 WMFont
*font
= tPtr
->dFont
;
989 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
996 /* first, which direction? Most likely, newly clicked
997 position will be close to previous */
998 if (!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
1001 s
= (dir
? 0 : tb
->nsections
- 1);
1002 if (y
>= tb
->sections
[s
]._y
&& y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1006 /* get the first (or last) section of the TextBlock that
1007 lies about the vertical click point */
1009 while (!done
&& tb
) {
1011 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1012 if ((dir
? tb
->next
: tb
->prior
))
1013 tb
= (dir
? tb
->next
: tb
->prior
);
1017 s
= (dir
? 0 : tb
->nsections
- 1);
1018 while (!done
&& (dir
? (s
< tb
->nsections
) : (s
>= 0))) {
1020 if ((dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) : (y
>= tb
->sections
[s
]._y
))) {
1028 if ((dir
? tb
->next
: tb
->prior
)) {
1029 tb
= (dir
? tb
->next
: tb
->prior
);
1031 break; /* goto _doneH; */
1036 if (s
< 0 || s
>= tb
->nsections
) {
1037 s
= (dir
? tb
->nsections
- 1 : 0);
1041 /* we have the line, which TextBlock on that line is it? */
1042 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1043 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1044 TextBlock
*hold
= tb
;
1045 tb
= getFirstNonGraphicBlockFor(hold
, dir
);
1055 _y
= tb
->sections
[s
]._y
;
1059 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1060 tb
= (dir
? tb
->next
: tb
->prior
);
1067 _w
= WMWidgetWidth(tb
->d
.widget
) - 5;
1069 _w
= tb
->d
.pixmap
->width
- 5;
1071 if (tb
->sections
[0].x
+ _w
>= x
)
1074 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1075 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1076 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1077 if (tb
->sections
[s
].x
+ _w
>= x
)
1082 if (tb
->sections
[s
].x
<= x
)
1086 if ((dir
? tb
->next
: tb
->prior
)) {
1087 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1088 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1089 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1091 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1092 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1097 if (_y
!= nxt
->sections
[dir
? 0 : nxt
->nsections
- 1]._y
) {
1098 /* this must be the last/first on this line. stop */
1099 pos
= (dir
? tb
->sections
[s
].end
: 0);
1100 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1104 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1106 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1107 } else if (pos
> tb
->sections
[s
].begin
) {
1109 WMWidthOfString(tb
->d
.font
,
1110 &(tb
->text
[tb
->sections
[s
].begin
]),
1111 pos
- tb
->sections
[s
].begin
);
1118 if ((dir
? tb
->next
: tb
->prior
)) {
1119 tb
= (dir
? tb
->next
: tb
->prior
);
1125 s
= (dir
? 0 : tb
->nsections
- 1);
1128 /* we have said TextBlock, now where within it? */
1131 int gw
= (tb
->object
? WMWidgetWidth(tb
->d
.widget
) : tb
->d
.pixmap
->width
);
1133 tPtr
->cursor
.x
= tb
->sections
[0].x
;
1135 if (x
> tPtr
->cursor
.x
+ gw
/ 2) {
1137 tPtr
->cursor
.x
+= gw
;
1139 printf("first %d\n", tb
->first
);
1141 if (tb
->prior
->graphic
)
1144 pos
= tb
->prior
->used
;
1155 WMFont
*f
= tb
->d
.font
;
1156 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1157 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1159 _w
= x
- tb
->sections
[s
].x
;
1162 while (pos
< len
&& WMWidthOfString(f
, text
, pos
+ 1) < _w
)
1165 tPtr
->cursor
.x
= tb
->sections
[s
].x
+ (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1167 pos
+= tb
->sections
[s
].begin
;
1173 tPtr
->tpos
= (pos
<= 1) ? pos
: 0;
1175 tPtr
->tpos
= (pos
< tb
->used
) ? pos
: tb
->used
;
1179 printf("...for this app will surely crash :-)\n");
1181 tPtr
->currentTextBlock
= tb
;
1182 tPtr
->cursor
.h
= tb
->sections
[s
].h
;
1183 tPtr
->cursor
.y
= tb
->sections
[s
]._y
;
1185 /* scroll the bars if the cursor is not visible */
1186 if (tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1187 if (tPtr
->cursor
.y
+ tPtr
->cursor
.h
> tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
) {
1189 (tPtr
->cursor
.y
+ tPtr
->cursor
.h
+ 10
1190 - (tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
));
1191 updateScrollers(tPtr
);
1192 } else if (tPtr
->cursor
.y
< tPtr
->vpos
+ tPtr
->visible
.y
) {
1193 tPtr
->vpos
-= (tPtr
->vpos
+ tPtr
->visible
.y
- tPtr
->cursor
.y
);
1194 updateScrollers(tPtr
);
1201 static void updateScrollers(Text
* tPtr
)
1204 if (tPtr
->flags
.frozen
)
1208 if (tPtr
->docHeight
<= tPtr
->visible
.h
) {
1209 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
1212 float hmax
= (float)(tPtr
->docHeight
);
1213 WMSetScrollerParameters(tPtr
->vS
,
1214 ((float)tPtr
->vpos
) / (hmax
- (float)tPtr
->visible
.h
),
1215 (float)tPtr
->visible
.h
/ hmax
);
1221 if (tPtr
->docWidth
<= tPtr
->visible
.w
) {
1222 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
1225 float wmax
= (float)(tPtr
->docWidth
);
1226 WMSetScrollerParameters(tPtr
->hS
,
1227 ((float)tPtr
->hpos
) / (wmax
- (float)tPtr
->visible
.w
),
1228 (float)tPtr
->visible
.w
/ wmax
);
1234 static void scrollersCallBack(WMWidget
* w
, void *self
)
1236 Text
*tPtr
= (Text
*) self
;
1237 Bool scroll
= False
;
1240 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1243 if (w
== tPtr
->vS
) {
1245 height
= tPtr
->visible
.h
;
1247 which
= WMGetScrollerHitPart(tPtr
->vS
);
1250 case WSDecrementLine
:
1251 if (tPtr
->vpos
> 0) {
1252 if (tPtr
->vpos
> 16)
1259 case WSIncrementLine
:{
1260 int limit
= tPtr
->docHeight
- height
;
1261 if (tPtr
->vpos
< limit
) {
1262 if (tPtr
->vpos
< limit
- 16)
1270 case WSDecrementPage
:
1271 if (((int)tPtr
->vpos
- (int)height
) >= 0)
1272 tPtr
->vpos
-= height
;
1277 case WSIncrementPage
:
1278 tPtr
->vpos
+= height
;
1279 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1280 tPtr
->vpos
= tPtr
->docHeight
- height
;
1284 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1285 * (float)(tPtr
->docHeight
- height
);
1292 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1293 tPtr
->prevVpos
= tPtr
->vpos
;
1296 if (w
== tPtr
->hS
) {
1297 int width
= tPtr
->visible
.w
;
1299 which
= WMGetScrollerHitPart(tPtr
->hS
);
1302 case WSDecrementLine
:
1303 if (tPtr
->hpos
> 0) {
1304 if (tPtr
->hpos
> 16)
1311 case WSIncrementLine
:{
1312 int limit
= tPtr
->docWidth
- width
;
1313 if (tPtr
->hpos
< limit
) {
1314 if (tPtr
->hpos
< limit
- 16)
1322 case WSDecrementPage
:
1323 if (((int)tPtr
->hpos
- (int)width
) >= 0)
1324 tPtr
->hpos
-= width
;
1329 case WSIncrementPage
:
1330 tPtr
->hpos
+= width
;
1331 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1332 tPtr
->hpos
= tPtr
->docWidth
- width
;
1336 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1337 * (float)(tPtr
->docWidth
- width
);
1344 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1345 tPtr
->prevHpos
= tPtr
->hpos
;
1349 updateScrollers(tPtr
);
1356 unsigned short begin
, end
; /* what part of the text block */
1359 static int layOutLine(Text
* tPtr
, myLineItems
* items
, int nitems
, int x
, int y
)
1361 int i
, j
= 0, lw
= 0, line_height
= 0, max_d
= 0, len
, n
;
1364 TextBlock
*tb
, *tbsame
= NULL
;
1366 if (!items
|| nitems
== 0)
1369 for (i
= 0; i
< nitems
; i
++) {
1373 if (!tPtr
->flags
.monoFont
) {
1375 WMWidget
*wdt
= tb
->d
.widget
;
1376 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1377 if (tPtr
->flags
.alignment
!= WALeft
)
1378 lw
+= WMWidgetWidth(wdt
);
1380 line_height
= WMAX(line_height
, tb
->d
.pixmap
->height
+ max_d
);
1381 if (tPtr
->flags
.alignment
!= WALeft
)
1382 lw
+= tb
->d
.pixmap
->width
;
1387 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1388 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1390 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1391 text
= &(tb
->text
[items
[i
].begin
]);
1392 len
= items
[i
].end
- items
[i
].begin
;
1393 if (tPtr
->flags
.alignment
!= WALeft
)
1394 lw
+= WMWidthOfString(font
, text
, len
);
1398 if (tPtr
->flags
.alignment
== WARight
) {
1399 j
= tPtr
->visible
.w
- lw
;
1400 } else if (tPtr
->flags
.alignment
== WACenter
) {
1401 j
= (int)((float)(tPtr
->visible
.w
- lw
)) / 2.0;
1404 for (i
= 0; i
< nitems
; i
++) {
1407 if (tbsame
== tb
) { /* extend it, since it's on same line */
1408 tb
->sections
[tb
->nsections
- 1].end
= items
[i
].end
;
1409 n
= tb
->nsections
- 1;
1411 tb
->sections
= wrealloc(tb
->sections
, (++tb
->nsections
) * sizeof(Section
));
1412 n
= tb
->nsections
- 1;
1413 tb
->sections
[n
]._y
= y
+ max_d
;
1414 tb
->sections
[n
].max_d
= max_d
;
1415 tb
->sections
[n
].x
= x
+ j
;
1416 tb
->sections
[n
].h
= line_height
;
1417 tb
->sections
[n
].begin
= items
[i
].begin
;
1418 tb
->sections
[n
].end
= items
[i
].end
;
1421 tb
->sections
[n
].last
= (i
+ 1 == nitems
);
1424 if (!tPtr
->flags
.monoFont
) {
1426 WMWidget
*wdt
= tb
->d
.widget
;
1427 tb
->sections
[n
].y
= max_d
+ y
+ line_height
- WMWidgetHeight(wdt
);
1428 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1430 tb
->sections
[n
].y
= y
+ line_height
+ max_d
- tb
->d
.pixmap
->height
;
1431 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1433 x
+= tb
->sections
[n
].w
;
1436 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1437 len
= items
[i
].end
- items
[i
].begin
;
1438 text
= &(tb
->text
[items
[i
].begin
]);
1440 tb
->sections
[n
].y
= y
+ line_height
- font
->y
;
1442 WMWidthOfString(font
,
1443 &(tb
->text
[tb
->sections
[n
].begin
]),
1444 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1446 x
+= WMWidthOfString(font
, text
, len
);
1456 static void layOutDocument(Text
* tPtr
)
1459 myLineItems
*items
= NULL
;
1460 unsigned int itemsSize
= 0, nitems
= 0, begin
, end
;
1462 unsigned int x
, y
= 0, lw
= 0, width
= 0, bmargin
;
1463 const char *start
= NULL
, *mark
= NULL
;
1465 if (tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)))
1468 assert(tPtr
->visible
.w
> 20);
1470 tPtr
->docWidth
= tPtr
->visible
.w
;
1471 x
= tPtr
->margins
[tb
->marginN
].first
;
1472 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1474 /* only partial layOut needed: re-Lay only affected textblocks */
1475 if (tPtr
->flags
.laidOut
) {
1476 tb
= tPtr
->currentTextBlock
;
1478 /* search backwards for textblocks on same line */
1480 if (!tb
->sections
|| tb
->nsections
< 1) {
1481 tb
= tPtr
->firstTextBlock
;
1482 tPtr
->flags
.laidOut
= False
;
1487 if (!tb
->prior
->sections
|| tb
->prior
->nsections
< 1) {
1488 tb
= tPtr
->firstTextBlock
;
1489 tPtr
->flags
.laidOut
= False
;
1494 if (tb
->sections
[0]._y
!= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
) {
1500 if (tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
> 0) {
1501 y
= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
+
1502 tb
->prior
->sections
[tb
->prior
->nsections
- 1].h
-
1503 tb
->prior
->sections
[tb
->prior
->nsections
- 1].max_d
;
1512 if (tb
->sections
&& tb
->nsections
> 0) {
1513 wfree(tb
->sections
);
1514 tb
->sections
= NULL
;
1518 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1519 TextBlock
*next
= tb
->next
;
1520 tPtr
->currentTextBlock
= tb
;
1521 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1527 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1528 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1529 x
= tPtr
->margins
[tb
->marginN
].first
;
1530 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1536 if (!tPtr
->flags
.monoFont
) {
1538 width
= WMWidgetWidth(tb
->d
.widget
);
1540 width
= tb
->d
.pixmap
->width
;
1542 if (width
> tPtr
->docWidth
)
1543 tPtr
->docWidth
= width
;
1546 if (lw
>= tPtr
->visible
.w
- x
) {
1547 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1553 if (nitems
+ 1 > itemsSize
) {
1554 items
= wrealloc(items
, (++itemsSize
) * sizeof(myLineItems
));
1557 items
[nitems
].tb
= tb
;
1558 items
[nitems
].begin
= 0;
1559 items
[nitems
].end
= 0;
1563 } else if ((start
= tb
->text
)) {
1565 font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
1568 mark
= strchr(start
, ' ');
1570 end
+= (int)(mark
- start
) + 1;
1573 end
+= strlen(start
);
1580 if (end
- begin
> 0) {
1582 width
= WMWidthOfString(font
, &tb
->text
[begin
], end
- begin
);
1584 /* if it won't fit, char wrap it */
1585 if (width
>= tPtr
->visible
.w
) {
1586 char *t
= &tb
->text
[begin
];
1587 int l
= end
- begin
, i
= 0;
1589 width
= WMWidthOfString(font
, t
, ++i
);
1590 } while (width
< tPtr
->visible
.w
&& i
< l
);
1594 start
= &tb
->text
[end
];
1600 if (lw
>= tPtr
->visible
.w
- x
) {
1601 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1607 if (nitems
+ 1 > itemsSize
) {
1608 items
= wrealloc(items
, (++itemsSize
) * sizeof(myLineItems
));
1611 items
[nitems
].tb
= tb
;
1612 items
[nitems
].begin
= begin
;
1613 items
[nitems
].end
= end
;
1620 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1621 if (0 && tPtr
->flags
.laidOut
1622 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
> 0
1623 && (tPtr
->vpos
+ tPtr
->visible
.h
< tb
->next
->sections
[0]._y
)) {
1624 if (tPtr
->lastTextBlock
->sections
&& tPtr
->lastTextBlock
->nsections
> 0) {
1625 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1626 int ly
= ltb
->sections
[ltb
->nsections
- 1]._y
;
1627 int lh
= ltb
->sections
[ltb
->nsections
- 1].h
;
1630 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
- 1].max_d
;
1631 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
- 1].max_d
);
1633 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1635 sd
= tPtr
->docHeight
- y
;
1637 printf("dif %d-%d: %d\n", ss
, sd
, ss
- sd
);
1638 y
+= tb
->next
->sections
[0]._y
- y
;
1640 printf("nitems%d\n", nitems
);
1642 y
= tPtr
->docHeight
+ ss
- sd
;
1646 tPtr
->flags
.laidOut
= False
;
1654 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1656 if (tPtr
->docHeight
!= y
+ 10) {
1657 tPtr
->docHeight
= y
+ 10;
1658 updateScrollers(tPtr
);
1661 if (tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1664 tPtr
->flags
.horizOnDemand
= True
;
1665 WMSetTextHasHorizontalScroller((WMText
*) tPtr
, True
);
1666 event
.type
= Expose
;
1667 handleEvents(&event
, (void *)tPtr
);
1669 } else if (tPtr
->docWidth
<= tPtr
->visible
.w
&& tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1670 tPtr
->flags
.horizOnDemand
= False
;
1671 WMSetTextHasHorizontalScroller((WMText
*) tPtr
, False
);
1674 tPtr
->flags
.laidOut
= True
;
1676 if (items
&& itemsSize
> 0)
1681 static void textDidResize(W_ViewDelegate
* self
, WMView
* view
)
1683 Text
*tPtr
= (Text
*) view
->self
;
1684 unsigned short w
= tPtr
->view
->size
.width
;
1685 unsigned short h
= tPtr
->view
->size
.height
;
1686 unsigned short rh
= 0, vw
= 0, rel
;
1688 /* Parameter not used, but tell the compiler that it is ok */
1691 rel
= (tPtr
->flags
.relief
== WRFlat
);
1693 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1694 WMMoveWidget(tPtr
->ruler
, 2, 2);
1695 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1700 WMMoveWidget(tPtr
->vS
, 1 - (rel
? 1 : 0), rh
+ 1 - (rel
? 1 : 0));
1701 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
? 2 : 0));
1703 WMSetRulerOffset(tPtr
->ruler
, 22);
1705 WMSetRulerOffset(tPtr
->ruler
, 2);
1709 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1710 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1712 WMMoveWidget(tPtr
->hS
, vw
+ 1, h
- 21);
1713 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1717 tPtr
->visible
.x
= (tPtr
->vS
) ? 24 : 4;
1718 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) ? 43 : 3;
1719 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1720 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1721 tPtr
->visible
.h
-= (tPtr
->hS
) ? 20 : 0;
1722 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1724 if (tPtr
->view
->flags
.realized
) {
1727 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1728 tPtr
->db
= (Pixmap
) NULL
;
1731 if (tPtr
->visible
.w
< 40)
1732 tPtr
->visible
.w
= 40;
1733 if (tPtr
->visible
.h
< 20)
1734 tPtr
->visible
.h
= 20;
1737 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1738 tPtr
->view
->window
, tPtr
->visible
.w
,
1739 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1746 W_ViewDelegate _TextViewDelegate
= {
1754 #define TEXT_BUFFER_INCR 8
1755 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1757 static void clearText(Text
* tPtr
)
1759 tPtr
->vpos
= tPtr
->hpos
= 0;
1760 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1761 tPtr
->cursor
.x
= -23;
1763 if (!tPtr
->firstTextBlock
)
1766 while (tPtr
->currentTextBlock
)
1767 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1769 tPtr
->firstTextBlock
= NULL
;
1770 tPtr
->currentTextBlock
= NULL
;
1771 tPtr
->lastTextBlock
= NULL
;
1772 WMEmptyArray(tPtr
->gfxItems
);
1775 /* possibly remove a single character from the currentTextBlock,
1776 or if there's a selection, remove it...
1777 note that Delete and Backspace are treated differently */
1778 static void deleteTextInteractively(Text
* tPtr
, KeySym ksym
)
1781 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1782 Bool done
= 1, wasFirst
= 0;
1784 if (!tPtr
->flags
.editable
)
1787 if (!(tb
= tPtr
->currentTextBlock
))
1790 if (tPtr
->flags
.ownsSelection
) {
1791 if (removeSelection(tPtr
))
1792 layOutDocument(tPtr
);
1796 wasFirst
= tb
->first
;
1797 if (back
&& tPtr
->tpos
< 1) {
1799 if (tb
->prior
->blank
) {
1800 tPtr
->currentTextBlock
= tb
->prior
;
1801 WMRemoveTextBlock(tPtr
);
1802 tPtr
->currentTextBlock
= tb
;
1804 layOutDocument(tPtr
);
1808 TextBlock
*prior
= tb
->prior
;
1809 tPtr
->currentTextBlock
= tb
;
1810 WMRemoveTextBlock(tPtr
);
1819 tPtr
->tpos
= tb
->used
;
1821 tPtr
->currentTextBlock
= tb
;
1825 tb
->next
->first
= False
;
1826 layOutDocument(tPtr
);
1833 if ((tb
->used
> 0) && ((back
? tPtr
->tpos
> 0 : 1))
1834 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1837 memmove(&(tb
->text
[tPtr
->tpos
]), &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1842 /* if there are no characters left to back over in the textblock,
1843 but it still has characters to the right of the cursor: */
1844 if ((back
? (tPtr
->tpos
== 0 && !done
) : (tPtr
->tpos
>= tb
->used
))
1847 /* no more chars, and it's marked as blank? */
1849 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1851 if (tb
->used
== 0 || tb
->graphic
)
1852 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1855 tPtr
->currentTextBlock
= sibling
;
1857 tPtr
->tpos
= (back
? 1 : 0);
1859 tPtr
->tpos
= (back
? sibling
->used
: 0);
1861 /* no more chars, so mark it as blank */
1862 } else if (tb
->used
== 0) {
1864 } else if (tb
->graphic
) {
1865 Bool hasNext
= (tb
->next
!= NULL
);
1867 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1870 } else if (tPtr
->currentTextBlock
) {
1871 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
? 1 : tPtr
->currentTextBlock
->used
);
1874 printf("DEBUG: unaccounted for... catch this!\n");
1877 layOutDocument(tPtr
);
1880 static void insertTextInteractively(Text
* tPtr
, char *text
, int len
)
1883 char *newline
= NULL
;
1885 if (!tPtr
->flags
.editable
) {
1889 if (len
< 1 || !text
)
1892 if (tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1895 if (tPtr
->flags
.ownsSelection
)
1896 removeSelection(tPtr
);
1898 if (tPtr
->flags
.ignoreNewLine
) {
1900 for (i
= 0; i
< len
; i
++) {
1901 if (text
[i
] == '\n')
1906 tb
= tPtr
->currentTextBlock
;
1907 if (!tb
|| tb
->graphic
) {
1909 WMAppendTextStream(tPtr
, text
);
1910 layOutDocument(tPtr
);
1914 if ((newline
= strchr(text
, '\n'))) {
1915 int nlen
= (int)(newline
- text
);
1916 int s
= tb
->used
- tPtr
->tpos
;
1918 if (!tb
->blank
&& nlen
> 0) {
1923 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
1924 tb
->used
-= (tb
->used
- tPtr
->tpos
);
1926 insertTextInteractively(tPtr
, text
, nlen
);
1928 WMAppendTextStream(tPtr
, newline
);
1930 insertTextInteractively(tPtr
, save
, s
);
1934 if (tPtr
->tpos
> 0 && tPtr
->tpos
< tb
->used
&& !tb
->graphic
&& tb
->text
) {
1936 unsigned short savePos
= tPtr
->tpos
;
1937 void *ntb
= WMCreateTextBlockWithText(tPtr
, &tb
->text
[tPtr
->tpos
],
1938 tb
->d
.font
, tb
->color
, True
,
1939 tb
->used
- tPtr
->tpos
);
1941 if (tb
->sections
[0].end
== tPtr
->tpos
)
1942 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1944 tb
->color
, True
, 0));
1947 WMAppendTextBlock(tPtr
, ntb
);
1950 } else if (tPtr
->tpos
== tb
->used
) {
1951 if (tPtr
->flags
.indentNewLine
) {
1952 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1954 tb
->color
, True
, 4));
1957 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1959 tb
->color
, True
, 0));
1962 } else if (tPtr
->tpos
== 0) {
1963 if (tPtr
->flags
.indentNewLine
) {
1964 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1966 tb
->color
, True
, 4));
1968 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1970 tb
->color
, True
, 0));
1973 if (tPtr
->currentTextBlock
->next
)
1974 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
1978 if (tb
->used
+ len
>= tb
->allocated
) {
1979 tb
->allocated
= reqBlockSize(tb
->used
+ len
);
1980 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
1984 memcpy(tb
->text
, text
, len
);
1987 tb
->text
[tb
->used
] = 0;
1991 memmove(&(tb
->text
[tPtr
->tpos
+ len
]), &tb
->text
[tPtr
->tpos
], tb
->used
- tPtr
->tpos
+ 1);
1992 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
1995 tb
->text
[tb
->used
] = 0;
2000 layOutDocument(tPtr
);
2003 static void selectRegion(Text
* tPtr
, int x
, int y
)
2009 y
+= (tPtr
->flags
.rulerShown
? 40 : 0);
2012 y
-= 10; /* the original offset */
2014 x
-= tPtr
->visible
.x
- 2;
2018 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2019 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2020 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2021 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2023 tPtr
->flags
.ownsSelection
= True
;
2027 static void releaseSelection(Text
* tPtr
)
2029 TextBlock
*tb
= tPtr
->firstTextBlock
;
2032 tb
->selected
= False
;
2035 tPtr
->flags
.ownsSelection
= False
;
2036 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2041 static WMData
*requestHandler(WMView
* view
, Atom selection
, Atom target
, void *cdata
, Atom
* type
)
2043 Text
*tPtr
= view
->self
;
2044 WMData
*data
= NULL
;
2046 /* Parameter not used, but tell the compiler that it is ok */
2050 if (target
== XA_STRING
|| target
== XA_Format_Text
|| target
== XA_Format_Compound_Text
) {
2051 char *text
= WMGetTextSelectedStream(tPtr
);
2054 data
= WMCreateDataWithBytes(text
, strlen(text
));
2055 WMSetDataFormat(data
, TYPETEXT
);
2061 printf("didn't get it\n");
2063 if (target
== XA_Targets
) {
2066 array
[0] = XA_Targets
;
2067 array
[1] = XA_STRING
;
2068 array
[2] = XA_Format_Text
;
2069 array
[3] = XA_Format_Compound_Text
;
2071 data
= WMCreateDataWithBytes(&array
, sizeof(array
));
2072 WMSetDataFormat(data
, 32);
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
);
2310 if (*buffer
!= 0 && !control_pressed
) {
2311 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2312 updateCursorPosition(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);
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
;
2336 /* Parameter not used, but tell the compiler that it is ok */
2342 tPtr
->flags
.waitingForSelection
= 0;
2345 text
= (char *)WMDataBytes(data
);
2348 (tPtr
->parser
) (tPtr
, (void *)text
);
2349 layOutDocument(tPtr
);
2351 insertTextInteractively(tPtr
, text
, strlen(text
));
2352 updateCursorPosition(tPtr
);
2358 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2363 (tPtr
->parser
) (tPtr
, (void *)text
);
2364 layOutDocument(tPtr
);
2366 insertTextInteractively(tPtr
, text
, n
);
2367 updateCursorPosition(tPtr
);
2376 static void handleActionEvents(XEvent
* event
, void *data
)
2378 Text
*tPtr
= (Text
*) data
;
2379 Display
*dpy
= event
->xany
.display
;
2382 switch (event
->type
) {
2384 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2385 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2386 tPtr
->flags
.extendSelection
= True
;
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
);
2402 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2403 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2404 tPtr
->flags
.extendSelection
= False
;
2406 /* end modify flag so selection can be extended */
2412 if (tPtr
->flags
.pointerGrabbed
) {
2413 tPtr
->flags
.pointerGrabbed
= False
;
2414 XUngrabPointer(dpy
, CurrentTime
);
2417 if (tPtr
->flags
.waitingForSelection
)
2420 if ((event
->xmotion
.state
& Button1Mask
)) {
2422 if (WMIsDraggingFromView(tPtr
->view
)) {
2423 WMDragImageFromView(tPtr
->view
, event
);
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
);
2436 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2441 if (tPtr
->flags
.pointerGrabbed
) {
2442 tPtr
->flags
.pointerGrabbed
= False
;
2443 XUngrabPointer(dpy
, CurrentTime
);
2447 if (tPtr
->flags
.waitingForSelection
)
2450 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2451 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
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
) {
2468 desc
= wmalloc(tb
->used
+ 1);
2469 memcpy(desc
, tb
->text
, tb
->used
);
2471 (*tPtr
->delegate
->didDoubleClickOnPicture
) (tPtr
->delegate
, desc
);
2475 autoSelectText(tPtr
, 2);
2478 } else if (event
->xbutton
.time
- tPtr
->lastClickTime
< WINGsConfiguration
.doubleClickDelay
) {
2479 tPtr
->lastClickTime
= event
->xbutton
.time
;
2480 autoSelectText(tPtr
, 3);
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
);
2494 tPtr
->lastClickTime
= event
->xbutton
.time
;
2495 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2499 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelDown
) {
2500 WMScrollText(tPtr
, 16);
2504 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
) {
2505 WMScrollText(tPtr
, -16);
2509 if (event
->xbutton
.button
== Button2
) {
2513 if (!tPtr
->flags
.editable
) {
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;
2528 (tPtr
->parser
) (tPtr
, (void *)text
);
2529 layOutDocument(tPtr
);
2531 insertTextInteractively(tPtr
, text
, n
);
2535 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2536 (void *)WMInsertTextEvent
);
2538 updateCursorPosition(tPtr
);
2542 tPtr
->flags
.waitingForSelection
= True
;
2550 if (tPtr
->flags
.pointerGrabbed
) {
2551 tPtr
->flags
.pointerGrabbed
= False
;
2552 XUngrabPointer(dpy
, CurrentTime
);
2556 if (tPtr
->flags
.waitingForSelection
)
2559 if (WMIsDraggingFromView(tPtr
->view
))
2560 WMDragImageFromView(tPtr
->view
, event
);
2565 static void handleEvents(XEvent
* event
, void *data
)
2567 Text
*tPtr
= (Text
*) data
;
2569 switch (event
->type
) {
2572 if (event
->xexpose
.count
!= 0)
2576 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2577 WMRealizeWidget(tPtr
->hS
);
2581 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2582 WMRealizeWidget(tPtr
->vS
);
2586 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2587 WMRealizeWidget(tPtr
->ruler
);
2592 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2598 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2601 tPtr
->flags
.focused
= True
;
2603 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2604 tPtr
->timerID
= WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY
, blinkCursor
, tPtr
);
2611 tPtr
->flags
.focused
= False
;
2614 if (tPtr
->timerID
) {
2615 WMDeleteTimerHandler(tPtr
->timerID
);
2616 tPtr
->timerID
= NULL
;
2624 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2626 WMEmptyArray(tPtr
->gfxItems
);
2629 WMDeleteTimerHandler(tPtr
->timerID
);
2631 WMReleaseFont(tPtr
->dFont
);
2632 WMReleaseColor(tPtr
->dColor
);
2633 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2634 WMRemoveNotificationObserver(tPtr
);
2636 WMFreeArray(tPtr
->xdndSourceTypes
);
2637 WMFreeArray(tPtr
->xdndDestinationTypes
);
2646 static void insertPlainText(Text
* tPtr
, const char *text
)
2648 const char *start
, *mark
;
2653 mark
= strchr(start
, '\n');
2655 tb
= WMCreateTextBlockWithText(tPtr
,
2657 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
- start
));
2659 tPtr
->flags
.first
= True
;
2661 if (start
&& strlen(start
)) {
2662 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2663 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2666 tPtr
->flags
.first
= False
;
2670 if (tPtr
->flags
.prepend
)
2671 WMPrependTextBlock(tPtr
, tb
);
2673 WMAppendTextBlock(tPtr
, tb
);
2677 static void rulerMoveCallBack(WMWidget
* w
, void *self
)
2679 Text
*tPtr
= (Text
*) self
;
2681 /* Parameter not used, but tell the compiler that it is ok */
2686 if (W_CLASS(tPtr
) != WC_Text
)
2692 static void rulerReleaseCallBack(WMWidget
* w
, void *self
)
2694 Text
*tPtr
= (Text
*) self
;
2696 /* Parameter not used, but tell the compiler that it is ok */
2701 if (W_CLASS(tPtr
) != WC_Text
)
2708 static WMArray
*dropDataTypes(WMView
* self
)
2710 return ((Text
*) self
->self
)->xdndSourceTypes
;
2713 static WMDragOperationType
wantedDropOperation(WMView
* self
)
2715 /* Parameter not used, but tell the compiler that it is ok */
2718 return WDOperationCopy
;
2721 static Bool
acceptDropOperation(WMView
* self
, WMDragOperationType allowedOperation
)
2723 /* Parameter not used, but tell the compiler that it is ok */
2726 return (allowedOperation
== WDOperationCopy
);
2729 static WMData
*fetchDragData(WMView
* self
, char *type
)
2731 TextBlock
*tb
= ((WMText
*) self
->self
)->currentTextBlock
;
2735 if (strcmp(type
, "text/plain")) {
2739 desc
= wmalloc(tb
->used
+ 1);
2740 memcpy(desc
, tb
->text
, tb
->used
);
2742 data
= WMCreateDataWithBytes(desc
, strlen(desc
) + 1);
2752 static WMDragSourceProcs _DragSourceProcs
= {
2754 wantedDropOperation
,
2756 acceptDropOperation
,
2762 static WMArray
*requiredDataTypes(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2764 /* Parameter not used, but tell the compiler that it is ok */
2766 (void) sourceDataTypes
;
2768 return ((Text
*) self
->self
)->xdndDestinationTypes
;
2771 static WMDragOperationType
allowedOperation(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2773 /* Parameter not used, but tell the compiler that it is ok */
2776 (void) sourceDataTypes
;
2778 return WDOperationCopy
;
2781 static void performDragOperation(WMView
* self
, WMArray
* dropData
, WMArray
* operations
, WMPoint
* dropLocation
)
2783 WMText
*tPtr
= (WMText
*) self
->self
;
2788 /* Parameter not used, but tell the compiler that it is ok */
2790 (void) dropLocation
;
2794 /* only one required type, implies only one drop data */
2796 /* get application/X-color if any */
2797 data
= (WMData
*) WMPopFromArray(dropData
);
2799 colorName
= (char *)WMDataBytes(data
);
2800 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
2803 WMSetTextSelectionColor(tPtr
, color
);
2804 WMReleaseColor(color
);
2810 static WMDragDestinationProcs _DragDestinationProcs
= {
2815 performDragOperation
,
2819 static char *getStream(WMText
* tPtr
, int sel
, int array
)
2821 TextBlock
*tb
= NULL
;
2823 unsigned long where
= 0;
2828 if (!(tb
= tPtr
->firstTextBlock
))
2832 (tPtr
->writer
) (tPtr
, (void *)text
);
2836 tb
= tPtr
->firstTextBlock
;
2839 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2841 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2843 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2844 && tb
!= tPtr
->firstTextBlock
) {
2845 text
= wrealloc(text
, where
+ 1);
2846 text
[where
++] = '\n';
2852 if (tb
->graphic
&& array
) {
2853 text
= wrealloc(text
, where
+ 4);
2854 text
[where
++] = 0xFA;
2855 text
[where
++] = (tb
->used
>> 8) & 0x0ff;
2856 text
[where
++] = tb
->used
& 0x0ff;
2857 text
[where
++] = tb
->allocated
; /* extra info */
2859 text
= wrealloc(text
, where
+ tb
->used
);
2860 memcpy(&text
[where
], tb
->text
, tb
->used
);
2863 } else if (sel
&& tb
->selected
) {
2865 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
2866 text
= wrealloc(text
, where
+ 1);
2867 text
[where
++] = '\n';
2873 text
= wrealloc(text
, where
+ (tb
->s_end
- tb
->s_begin
));
2874 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
], tb
->s_end
- tb
->s_begin
);
2875 where
+= tb
->s_end
- tb
->s_begin
;
2880 _gSnext
: tb
= tb
->next
;
2883 /* +1 for the end of string, let's be nice */
2884 text
= wrealloc(text
, where
+ 1);
2889 static void releaseStreamObjects(void *data
)
2895 static WMArray
*getStreamObjects(WMText
* tPtr
, int sel
)
2901 char *start
, *fa
, *desc
;
2903 stream
= getStream(tPtr
, sel
, 1);
2907 array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
2912 fa
= strchr(start
, 0xFA);
2914 if ((int)(fa
- start
) > 0) {
2916 desc
[(int)(fa
- start
)] = 0;
2917 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
2918 WMSetDataFormat(data
, TYPETEXT
);
2919 WMAddToArray(array
, (void *)data
);
2922 len
= *(fa
+ 1) * 0xff + *(fa
+ 2);
2923 data
= WMCreateDataWithBytes((void *)(fa
+ 4), len
);
2924 WMSetDataFormat(data
, *(fa
+ 3));
2925 WMAddToArray(array
, (void *)data
);
2926 start
= fa
+ len
+ 4;
2929 if (start
&& strlen(start
)) {
2930 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
2931 WMSetDataFormat(data
, TYPETEXT
);
2932 WMAddToArray(array
, (void *)data
);
2942 #define XDND_TEXT_DATA_TYPE "text/plain"
2943 #define XDND_COLOR_DATA_TYPE "application/X-color"
2944 static WMArray
*getXdndSourceTypeArray(void)
2946 WMArray
*types
= WMCreateArray(1);
2947 WMAddToArray(types
, XDND_TEXT_DATA_TYPE
);
2951 static WMArray
*getXdndDestinationTypeArray(void)
2953 WMArray
*types
= WMCreateArray(1);
2954 WMAddToArray(types
, XDND_COLOR_DATA_TYPE
);
2958 WMText
*WMCreateTextForDocumentType(WMWidget
* parent
, WMAction
* parser
, WMAction
* writer
)
2965 tPtr
= wmalloc(sizeof(Text
));
2966 tPtr
->widgetClass
= WC_Text
;
2967 tPtr
->view
= W_CreateView(W_VIEW(parent
));
2969 perror("could not create text's view\n");
2974 dpy
= tPtr
->view
->screen
->display
;
2975 scr
= tPtr
->view
->screen
;
2977 if (XA_Targets
== None
) {
2979 * Because the X protocol guaranties that the value will never change in
2980 * the lifespan of the server, we query the values only the first time a
2983 XA_Targets
= XInternAtom(dpy
, "TARGETS", False
);
2984 XA_Format_Text
= XInternAtom(dpy
, "TEXT", False
);
2985 XA_Format_Compound_Text
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2988 tPtr
->view
->self
= tPtr
;
2989 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
2990 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
2991 W_ResizeView(tPtr
->view
, 250, 200);
2993 tPtr
->dColor
= WMBlackColor(scr
);
2994 tPtr
->fgColor
= WMBlackColor(scr
);
2995 tPtr
->bgColor
= WMWhiteColor(scr
);
2996 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
2998 gcv
.graphics_exposures
= False
;
2999 gcv
.foreground
= W_PIXEL(scr
->gray
);
3000 gcv
.background
= W_PIXEL(scr
->darkGray
);
3001 gcv
.fill_style
= FillStippled
;
3002 /* why not use scr->stipple here? */
3003 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
, STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
3004 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
3005 GCForeground
| GCBackground
| GCStipple
3006 | GCFillStyle
| GCGraphicsExposures
, &gcv
);
3012 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
3014 tPtr
->view
->delegate
= &_TextViewDelegate
;
3016 tPtr
->delegate
= NULL
;
3019 tPtr
->timerID
= NULL
;
3022 WMCreateEventHandler(tPtr
->view
, ExposureMask
| StructureNotifyMask
3023 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
, handleEvents
, tPtr
);
3025 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
| ButtonPressMask
3026 | KeyReleaseMask
| KeyPressMask
| Button1MotionMask
, handleActionEvents
, tPtr
);
3028 WMAddNotificationObserver(ownershipObserver
, tPtr
, WMSelectionOwnerDidChangeNotification
, tPtr
);
3030 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3031 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3034 WMArray
*types
= WMCreateArray(2);
3035 WMAddToArray(types
, "application/X-color");
3036 WMAddToArray(types
, "application/X-image");
3037 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3041 /*WMAddNotificationObserver(fontChanged, tPtr,
3042 WMFontPanelDidChangeNotification, tPtr); */
3044 tPtr
->firstTextBlock
= NULL
;
3045 tPtr
->lastTextBlock
= NULL
;
3046 tPtr
->currentTextBlock
= NULL
;
3049 tPtr
->gfxItems
= WMCreateArray(4);
3051 tPtr
->parser
= parser
;
3052 tPtr
->writer
= writer
;
3054 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3055 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3057 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3059 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3060 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3061 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3063 tPtr
->cursor
.x
= -23;
3066 tPtr
->docHeight
= 0;
3067 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
, default_bullet
);
3068 tPtr
->db
= (Pixmap
) NULL
;
3069 tPtr
->bgPixmap
= NULL
;
3071 tPtr
->margins
= WMGetRulerMargins(NULL
);
3072 tPtr
->margins
->right
= tPtr
->visible
.w
;
3075 tPtr
->flags
.rulerShown
= False
;
3076 tPtr
->flags
.monoFont
= False
;
3077 tPtr
->flags
.focused
= False
;
3078 tPtr
->flags
.editable
= True
;
3079 tPtr
->flags
.ownsSelection
= False
;
3080 tPtr
->flags
.pointerGrabbed
= False
;
3081 tPtr
->flags
.extendSelection
= False
;
3082 tPtr
->flags
.frozen
= False
;
3083 tPtr
->flags
.cursorShown
= True
;
3084 tPtr
->flags
.acceptsGraphic
= False
;
3085 tPtr
->flags
.horizOnDemand
= False
;
3086 tPtr
->flags
.needsLayOut
= False
;
3087 tPtr
->flags
.ignoreNewLine
= False
;
3088 tPtr
->flags
.indentNewLine
= False
;
3089 tPtr
->flags
.laidOut
= False
;
3090 tPtr
->flags
.ownsSelection
= False
;
3091 tPtr
->flags
.waitingForSelection
= False
;
3092 tPtr
->flags
.prepend
= False
;
3093 tPtr
->flags
.isOverGraphic
= False
;
3094 tPtr
->flags
.relief
= WRSunken
;
3095 tPtr
->flags
.isOverGraphic
= 0;
3096 tPtr
->flags
.alignment
= WALeft
;
3097 tPtr
->flags
.first
= True
;
3099 tPtr
->xdndSourceTypes
= getXdndSourceTypeArray();
3100 tPtr
->xdndDestinationTypes
= getXdndDestinationTypeArray();
3105 void WMPrependTextStream(WMText
* tPtr
, const char *text
)
3107 CHECK_CLASS(tPtr
, WC_Text
);
3110 if (tPtr
->flags
.ownsSelection
)
3111 releaseSelection(tPtr
);
3113 updateScrollers(tPtr
);
3117 tPtr
->flags
.prepend
= True
;
3118 if (text
&& tPtr
->parser
)
3119 (tPtr
->parser
) (tPtr
, (void *)text
);
3121 insertPlainText(tPtr
, text
);
3123 tPtr
->flags
.needsLayOut
= True
;
3125 if (!tPtr
->flags
.frozen
) {
3126 layOutDocument(tPtr
);
3130 void WMAppendTextStream(WMText
* tPtr
, const char *text
)
3132 CHECK_CLASS(tPtr
, WC_Text
);
3135 if (tPtr
->flags
.ownsSelection
)
3136 releaseSelection(tPtr
);
3138 updateScrollers(tPtr
);
3142 tPtr
->flags
.prepend
= False
;
3143 if (text
&& tPtr
->parser
)
3144 (tPtr
->parser
) (tPtr
, (void *)text
);
3146 insertPlainText(tPtr
, text
);
3148 tPtr
->flags
.needsLayOut
= True
;
3149 if (tPtr
->currentTextBlock
) {
3150 if (tPtr
->currentTextBlock
->graphic
)
3153 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3156 if (!tPtr
->flags
.frozen
) {
3157 layOutDocument(tPtr
);
3161 char *WMGetTextStream(WMText
* tPtr
)
3163 CHECK_CLASS(tPtr
, WC_Text
);
3165 return getStream(tPtr
, 0, 0);
3168 char *WMGetTextSelectedStream(WMText
* tPtr
)
3170 CHECK_CLASS(tPtr
, WC_Text
);
3172 return getStream(tPtr
, 1, 0);
3175 WMArray
*WMGetTextObjects(WMText
* tPtr
)
3177 CHECK_CLASS(tPtr
, WC_Text
);
3179 return getStreamObjects(tPtr
, 0);
3182 WMArray
*WMGetTextSelectedObjects(WMText
* tPtr
)
3184 CHECK_CLASS(tPtr
, WC_Text
);
3186 return getStreamObjects(tPtr
, 1);
3189 void WMSetTextDelegate(WMText
* tPtr
, WMTextDelegate
* delegate
)
3191 CHECK_CLASS(tPtr
, WC_Text
);
3193 tPtr
->delegate
= delegate
;
3196 void *WMCreateTextBlockWithObject(WMText
* tPtr
, WMWidget
* w
,
3197 const char *description
, WMColor
* color
,
3198 unsigned short first
, unsigned short extraInfo
)
3202 if (!w
|| !description
|| !color
)
3205 tb
= wmalloc(sizeof(TextBlock
));
3207 tb
->text
= wstrdup(description
);
3208 tb
->used
= strlen(description
);
3211 tb
->color
= WMRetainColor(color
);
3212 tb
->marginN
= newMargin(tPtr
, NULL
);
3213 tb
->allocated
= extraInfo
;
3218 tb
->underlined
= False
;
3219 tb
->selected
= False
;
3221 tb
->sections
= NULL
;
3229 void *WMCreateTextBlockWithPixmap(WMText
* tPtr
, WMPixmap
* p
,
3230 const char *description
, WMColor
* color
,
3231 unsigned short first
, unsigned short extraInfo
)
3235 if (!p
|| !description
|| !color
)
3238 tb
= wmalloc(sizeof(TextBlock
));
3240 tb
->text
= wstrdup(description
);
3241 tb
->used
= strlen(description
);
3243 tb
->d
.pixmap
= WMRetainPixmap(p
);
3244 tb
->color
= WMRetainColor(color
);
3245 tb
->marginN
= newMargin(tPtr
, NULL
);
3246 tb
->allocated
= extraInfo
;
3251 tb
->underlined
= False
;
3252 tb
->selected
= False
;
3254 tb
->sections
= NULL
;
3262 void *WMCreateTextBlockWithText(WMText
* tPtr
, const char *text
, WMFont
* font
, WMColor
* color
,
3263 unsigned short first
, unsigned short len
)
3267 if (!font
|| !color
)
3270 tb
= wmalloc(sizeof(TextBlock
));
3272 tb
->allocated
= reqBlockSize(len
);
3273 tb
->text
= (char *)wmalloc(tb
->allocated
);
3275 if (len
< 1 || !text
|| (*text
== '\n' && len
== 1)) {
3280 memcpy(tb
->text
, text
, len
);
3284 tb
->text
[tb
->used
] = 0;
3286 tb
->d
.font
= WMRetainFont(font
);
3287 tb
->color
= WMRetainColor(color
);
3288 tb
->marginN
= newMargin(tPtr
, NULL
);
3291 tb
->graphic
= False
;
3292 tb
->underlined
= False
;
3293 tb
->selected
= False
;
3295 tb
->sections
= NULL
;
3303 WMSetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int first
,
3304 unsigned int kanji
, unsigned int underlined
, int script
, WMRulerMargins
* margins
)
3306 TextBlock
*tb
= (TextBlock
*) vtb
;
3312 tb
->underlined
= underlined
;
3313 tb
->script
= script
;
3314 tb
->marginN
= newMargin(tPtr
, margins
);
3318 WMGetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int *first
,
3319 unsigned int *kanji
, unsigned int *underlined
, int *script
, WMRulerMargins
*margins
)
3321 TextBlock
*tb
= (TextBlock
*) vtb
;
3330 *underlined
= tb
->underlined
;
3332 *script
= tb
->script
;
3334 *margins
= tPtr
->margins
[tb
->marginN
];
3337 static int prepareTextBlock(WMText
*tPtr
, TextBlock
*tb
)
3341 WMWidget
*w
= tb
->d
.widget
;
3342 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3343 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3344 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3347 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3351 tPtr
->tpos
= tb
->used
;
3354 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3355 tb
->next
= tb
->prior
= NULL
;
3357 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3362 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3369 void WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3371 TextBlock
*tb
= (TextBlock
*) vtb
;
3373 if (!tb
|| !prepareTextBlock(tPtr
, tb
))
3376 tb
->next
= tPtr
->currentTextBlock
;
3377 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3378 if (tPtr
->currentTextBlock
->prior
)
3379 tPtr
->currentTextBlock
->prior
->next
= tb
;
3381 tPtr
->currentTextBlock
->prior
= tb
;
3383 tPtr
->firstTextBlock
= tb
;
3385 tPtr
->currentTextBlock
= tb
;
3388 void WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3390 TextBlock
*tb
= (TextBlock
*) vtb
;
3392 if (!tb
|| !prepareTextBlock(tPtr
, tb
))
3395 tb
->next
= tPtr
->currentTextBlock
->next
;
3396 tb
->prior
= tPtr
->currentTextBlock
;
3397 if (tPtr
->currentTextBlock
->next
)
3398 tPtr
->currentTextBlock
->next
->prior
= tb
;
3400 tPtr
->currentTextBlock
->next
= tb
;
3403 tPtr
->lastTextBlock
= tb
;
3405 tPtr
->currentTextBlock
= tb
;
3408 void *WMRemoveTextBlock(WMText
* tPtr
)
3410 TextBlock
*tb
= NULL
;
3412 if (!tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
|| !tPtr
->currentTextBlock
) {
3416 tb
= tPtr
->currentTextBlock
;
3418 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3421 WMUnmapWidget(tb
->d
.widget
);
3425 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3426 if (tPtr
->currentTextBlock
->next
)
3427 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3429 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3430 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3432 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3433 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3434 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3435 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3437 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3438 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3439 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3446 static void destroyWidget(WMWidget
* widget
)
3448 WMDestroyWidget(widget
);
3449 // -- never do this -- wfree(widget);
3453 void WMDestroyTextBlock(WMText
* tPtr
, void *vtb
)
3455 TextBlock
*tb
= (TextBlock
*) vtb
;
3457 /* Parameter not used, but tell the compiler that it is ok */
3465 /* naturally, there's a danger to destroying widgets whose action
3466 * brings us here: ie. press a button to destroy it...
3467 * need to find a safer way. till then... this stays commented out */
3468 /* 5 months later... destroy it 10 seconds after now which should
3469 * be enough time for the widget's action to be completed... :-) */
3470 /* This is a bad assumption. Just destroy the widget here.
3471 * if the caller needs it, it can protect it with W_RetainView()
3472 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3473 WMDestroyWidget(tb
->d
.widget
);
3475 WMReleasePixmap(tb
->d
.pixmap
);
3478 WMReleaseFont(tb
->d
.font
);
3481 WMReleaseColor(tb
->color
);
3482 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3484 wfree(tb
->sections
);
3489 void WMSetTextForegroundColor(WMText
* tPtr
, WMColor
* color
)
3492 WMReleaseColor(tPtr
->fgColor
);
3494 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3499 void WMSetTextBackgroundColor(WMText
* tPtr
, WMColor
* color
)
3502 WMReleaseColor(tPtr
->bgColor
);
3504 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3505 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3510 void WMSetTextBackgroundPixmap(WMText
* tPtr
, WMPixmap
* pixmap
)
3513 WMReleasePixmap(tPtr
->bgPixmap
);
3516 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3518 tPtr
->bgPixmap
= NULL
;
3521 void WMSetTextRelief(WMText
* tPtr
, WMReliefType relief
)
3523 tPtr
->flags
.relief
= relief
;
3524 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3527 void WMSetTextHasHorizontalScroller(WMText
* tPtr
, Bool shouldhave
)
3529 if (shouldhave
&& !tPtr
->hS
) {
3530 tPtr
->hS
= WMCreateScroller(tPtr
);
3531 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3532 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3533 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3534 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3535 WMMapWidget(tPtr
->hS
);
3536 } else if (!shouldhave
&& tPtr
->hS
) {
3537 WMUnmapWidget(tPtr
->hS
);
3538 WMDestroyWidget(tPtr
->hS
);
3544 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3547 void WMSetTextHasRuler(WMText
* tPtr
, Bool shouldhave
)
3549 if (shouldhave
&& !tPtr
->ruler
) {
3550 tPtr
->ruler
= WMCreateRuler(tPtr
);
3551 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3552 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3553 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3554 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3555 } else if (!shouldhave
&& tPtr
->ruler
) {
3556 WMShowTextRuler(tPtr
, False
);
3557 WMDestroyWidget(tPtr
->ruler
);
3560 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3563 void WMShowTextRuler(WMText
* tPtr
, Bool show
)
3568 if (tPtr
->flags
.monoFont
)
3571 tPtr
->flags
.rulerShown
= show
;
3573 WMMapWidget(tPtr
->ruler
);
3575 WMUnmapWidget(tPtr
->ruler
);
3578 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3581 Bool
WMGetTextRulerShown(WMText
* tPtr
)
3586 return tPtr
->flags
.rulerShown
;
3589 void WMSetTextHasVerticalScroller(WMText
* tPtr
, Bool shouldhave
)
3591 if (shouldhave
&& !tPtr
->vS
) {
3592 tPtr
->vS
= WMCreateScroller(tPtr
);
3593 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3594 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3595 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3596 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3597 WMMapWidget(tPtr
->vS
);
3598 } else if (!shouldhave
&& tPtr
->vS
) {
3599 WMUnmapWidget(tPtr
->vS
);
3600 WMDestroyWidget(tPtr
->vS
);
3606 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3609 Bool
WMScrollText(WMText
* tPtr
, int amount
)
3611 Bool scroll
= False
;
3613 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3617 if (tPtr
->vpos
> 0) {
3618 if (tPtr
->vpos
> abs(amount
))
3619 tPtr
->vpos
+= amount
;
3625 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3626 if (tPtr
->vpos
< limit
) {
3627 if (tPtr
->vpos
< limit
- amount
)
3628 tPtr
->vpos
+= amount
;
3635 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3636 updateScrollers(tPtr
);
3639 tPtr
->prevVpos
= tPtr
->vpos
;
3643 Bool
WMPageText(WMText
* tPtr
, Bool direction
)
3645 if (!tPtr
->view
->flags
.realized
)
3648 return WMScrollText(tPtr
, direction
? tPtr
->visible
.h
: -tPtr
->visible
.h
);
3651 void WMSetTextEditable(WMText
* tPtr
, Bool editable
)
3653 tPtr
->flags
.editable
= editable
;
3656 int WMGetTextEditable(WMText
* tPtr
)
3658 return tPtr
->flags
.editable
;
3661 void WMSetTextIndentNewLines(WMText
* tPtr
, Bool indent
)
3663 tPtr
->flags
.indentNewLine
= indent
;
3666 void WMSetTextIgnoresNewline(WMText
* tPtr
, Bool ignore
)
3668 tPtr
->flags
.ignoreNewLine
= ignore
;
3671 Bool
WMGetTextIgnoresNewline(WMText
* tPtr
)
3673 return tPtr
->flags
.ignoreNewLine
;
3676 void WMSetTextUsesMonoFont(WMText
* tPtr
, Bool mono
)
3679 if (tPtr
->flags
.rulerShown
)
3680 WMShowTextRuler(tPtr
, False
);
3681 if (tPtr
->flags
.alignment
!= WALeft
)
3682 tPtr
->flags
.alignment
= WALeft
;
3685 tPtr
->flags
.monoFont
= mono
;
3686 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3689 Bool
WMGetTextUsesMonoFont(WMText
* tPtr
)
3691 return tPtr
->flags
.monoFont
;
3694 void WMSetTextDefaultFont(WMText
* tPtr
, WMFont
* font
)
3697 WMReleaseFont(tPtr
->dFont
);
3700 tPtr
->dFont
= WMRetainFont(font
);
3702 tPtr
->dFont
= WMSystemFontOfSize(tPtr
->view
->screen
, 12);
3706 WMFont
*WMGetTextDefaultFont(WMText
* tPtr
)
3708 return WMRetainFont(tPtr
->dFont
);
3711 void WMSetTextDefaultColor(WMText
* tPtr
, WMColor
* color
)
3714 WMReleaseColor(tPtr
->dColor
);
3717 tPtr
->dColor
= WMRetainColor(color
);
3719 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3723 WMColor
*WMGetTextDefaultColor(WMText
* tPtr
)
3725 return tPtr
->dColor
;
3728 void WMSetTextAlignment(WMText
* tPtr
, WMAlignment alignment
)
3730 if (tPtr
->flags
.monoFont
)
3731 tPtr
->flags
.alignment
= WALeft
;
3733 tPtr
->flags
.alignment
= alignment
;
3737 int WMGetTextInsertType(WMText
* tPtr
)
3739 return tPtr
->flags
.prepend
;
3742 void WMSetTextSelectionColor(WMText
* tPtr
, WMColor
* color
)
3744 setSelectionProperty(tPtr
, NULL
, color
, -1);
3747 WMColor
*WMGetTextSelectionColor(WMText
* tPtr
)
3751 tb
= tPtr
->currentTextBlock
;
3753 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3762 void WMSetTextSelectionFont(WMText
* tPtr
, WMFont
* font
)
3764 setSelectionProperty(tPtr
, font
, NULL
, -1);
3767 WMFont
*WMGetTextSelectionFont(WMText
* tPtr
)
3771 tb
= tPtr
->currentTextBlock
;
3773 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3780 tb
= getFirstNonGraphicBlockFor(tb
, 1);
3784 return (tb
->selected
? tb
->d
.font
: NULL
);
3787 void WMSetTextSelectionUnderlined(WMText
* tPtr
, int underlined
)
3790 if (underlined
!= 0 && underlined
!= 1)
3793 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
3796 int WMGetTextSelectionUnderlined(WMText
* tPtr
)
3800 tb
= tPtr
->currentTextBlock
;
3802 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3808 return tb
->underlined
;
3811 void WMFreezeText(WMText
* tPtr
)
3813 tPtr
->flags
.frozen
= True
;
3816 void WMThawText(WMText
* tPtr
)
3818 tPtr
->flags
.frozen
= False
;
3820 if (tPtr
->flags
.monoFont
) {
3821 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
3824 /* make sure to unmap widgets no matter where they are */
3825 /* they'll be later remapped if needed by paintText */
3826 for (j
= 0; j
< c
; j
++) {
3827 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
3828 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
3829 WMUnmapWidget(tb
->d
.widget
);
3834 tPtr
->flags
.laidOut
= False
;
3835 layOutDocument(tPtr
);
3836 updateScrollers(tPtr
);
3838 tPtr
->flags
.needsLayOut
= False
;
3842 /* find first occurence of a string */
3843 static const char *mystrstr(const char *haystack
, const char *needle
, unsigned short len
, const char *end
, Bool caseSensitive
)
3847 if (!haystack
|| !needle
|| !end
)
3850 for (ptr
= haystack
; ptr
< end
; ptr
++) {
3851 if (caseSensitive
) {
3852 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3856 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3864 /* find last occurence of a string */
3865 static const char *mystrrstr(const char *haystack
, const char *needle
, unsigned short len
, const char *end
, Bool caseSensitive
)
3869 if (!haystack
|| !needle
|| !end
)
3872 for (ptr
= haystack
- 2; ptr
> end
; ptr
--) {
3873 if (caseSensitive
) {
3874 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3877 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3885 Bool
WMFindInTextStream(WMText
* tPtr
, const char *needle
, Bool direction
, Bool caseSensitive
)
3888 const char *mark
= NULL
;
3892 if (!(tb
= tPtr
->currentTextBlock
)) {
3893 if (!(tb
= ((direction
> 0) ? tPtr
->firstTextBlock
: tPtr
->lastTextBlock
))) {
3897 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3898 tb = (direction>0) ? tb->next : tb->prior; */
3899 if (tb
!= tPtr
->lastTextBlock
)
3903 tb
= tPtr
->currentTextBlock
;
3909 if (direction
> 0) {
3910 if (pos
+ 1 < tb
->used
)
3913 if (tb
->used
- pos
> 0 && pos
> 0) {
3914 mark
= mystrstr(&tb
->text
[pos
], needle
,
3915 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
3928 mark
= mystrrstr(&tb
->text
[pos
], needle
,
3929 strlen(needle
), tb
->text
, caseSensitive
);
3940 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
3942 tPtr
->tpos
= (int)(mark
- tb
->text
);
3943 tPtr
->currentTextBlock
= tb
;
3944 updateCursorPosition(tPtr
);
3945 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
3946 tPtr
->sel
.h
= tPtr
->cursor
.h
- 10;
3947 tPtr
->sel
.x
= tPtr
->cursor
.x
+ 1;
3948 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
3949 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
3950 tPtr
->docWidth
- tPtr
->sel
.x
);
3951 tPtr
->flags
.ownsSelection
= True
;
3958 tb
= (direction
> 0) ? tb
->next
: tb
->prior
;
3960 pos
= (direction
> 0) ? 0 : tb
->used
;
3967 Bool
WMReplaceTextSelection(WMText
* tPtr
, char *replacement
)
3969 if (!tPtr
->flags
.ownsSelection
)
3972 removeSelection(tPtr
);
3975 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
3976 updateCursorPosition(tPtr
);