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
, prev_y
= -23, 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 prev_y
= tb
->sections
[s
]._y
;
717 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
718 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
719 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
720 WMDrawString(scr
, tPtr
->db
, color
, font
, tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
722 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
723 XDrawLine(dpy
, tPtr
->db
, WMColorGC(color
),
724 tb
->sections
[s
].x
- tPtr
->hpos
,
726 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
, y
+ font
->y
+ 1);
729 tb
= (!done
? tb
->next
: NULL
);
732 /* now , show all graphic items that can be viewed */
733 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
734 if (c
> 0 && !tPtr
->flags
.monoFont
) {
737 for (j
= 0; j
< c
; j
++) {
738 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
740 /* if it's not viewable, and mapped, unmap it */
741 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
742 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
745 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
746 WMUnmapWidget(tb
->d
.widget
);
750 /* if it's viewable, and not mapped, map it */
752 W_View
*view
= W_VIEW(tb
->d
.widget
);
754 if (!view
->flags
.realized
)
755 WMRealizeWidget(tb
->d
.widget
);
756 if (!view
->flags
.mapped
) {
757 XMapWindow(view
->screen
->display
, view
->window
);
758 XFlush(view
->screen
->display
);
759 view
->flags
.mapped
= 1;
764 WMMoveWidget(tb
->d
.widget
,
765 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
766 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
767 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
770 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
771 tb
->sections
[0].x
- tPtr
->hpos
,
772 tb
->sections
[0].y
- tPtr
->vpos
);
773 h
= tb
->d
.pixmap
->height
+ 1;
777 if (tPtr
->flags
.ownsSelection
) {
780 if (sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
781 Drawable d
= (0 && tb
->object
?
782 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
785 XFillRectangle(dpy
, d
, tPtr
->stippledGC
,
786 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
787 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
791 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
792 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
793 tb
->sections
[0].x
- tPtr
->hpos
,
794 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
795 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
796 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
803 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
&& tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
804 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
805 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tPtr
->fgColor
),
806 tPtr
->cursor
.x
, y
, tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
809 XCopyArea(dpy
, tPtr
->db
, win
, WMColorGC(tPtr
->bgColor
), 0, 0,
810 tPtr
->visible
.w
, tPtr
->visible
.h
, tPtr
->visible
.x
, tPtr
->visible
.y
);
812 W_DrawRelief(scr
, win
, 0, 0, tPtr
->view
->size
.width
, tPtr
->view
->size
.height
, tPtr
->flags
.relief
);
814 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
815 XDrawLine(dpy
, win
, WMColorGC(tPtr
->fgColor
), 2, 42, tPtr
->view
->size
.width
- 4, 42);
819 static void mouseOverObject(Text
* tPtr
, int x
, int y
)
824 x
-= tPtr
->visible
.x
;
826 y
-= tPtr
->visible
.y
;
829 if (tPtr
->flags
.ownsSelection
) {
831 && tPtr
->sel
.y
<= y
&& tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
&& tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
832 tPtr
->flags
.isOverGraphic
= 1;
838 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
841 tPtr
->flags
.isOverGraphic
= 0;
843 for (j
= 0; j
< c
; j
++) {
844 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
846 if (!tb
|| !tb
->sections
) {
847 tPtr
->flags
.isOverGraphic
= 0;
852 if (tb
->sections
[0].x
<= x
853 && tb
->sections
[0].y
<= y
854 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
855 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
856 tPtr
->flags
.isOverGraphic
= 3;
866 tPtr
->flags
.isOverGraphic
= 0;
868 tPtr
->view
->attribs
.cursor
= (result
? tPtr
->view
->screen
->defaultCursor
: tPtr
->view
->screen
->textCursor
);
870 XSetWindowAttributes attribs
;
871 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
872 XChangeWindowAttributes(tPtr
->view
->screen
->display
, tPtr
->view
->window
, CWCursor
, &attribs
);
878 static void blinkCursor(void *data
)
880 Text
*tPtr
= (Text
*) data
;
882 if (tPtr
->flags
.cursorShown
) {
883 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
, blinkCursor
, data
);
885 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
, blinkCursor
, data
);
888 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
892 static void updateCursorPosition(Text
* tPtr
)
894 TextBlock
*tb
= NULL
;
897 if (tPtr
->flags
.needsLayOut
)
898 layOutDocument(tPtr
);
900 if (!(tb
= tPtr
->currentTextBlock
)) {
901 if (!(tb
= tPtr
->firstTextBlock
)) {
902 WMFont
*font
= tPtr
->dFont
;
904 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
914 y
= tb
->sections
[0].y
;
915 h
= tb
->sections
[0].h
;
916 x
= tb
->sections
[0].x
;
918 } else if (tb
->graphic
) {
919 y
= tb
->sections
[0].y
;
920 h
= tb
->sections
[0].h
;
921 x
= tb
->sections
[0].x
;
923 x
+= tb
->sections
[0].w
;
926 if (tPtr
->tpos
> tb
->used
)
927 tPtr
->tpos
= tb
->used
;
929 for (s
= 0; s
< tb
->nsections
- 1; s
++) {
931 if (tPtr
->tpos
>= tb
->sections
[s
].begin
&& tPtr
->tpos
<= tb
->sections
[s
].end
)
935 y
= tb
->sections
[s
]._y
;
936 h
= tb
->sections
[s
].h
;
937 x
= tb
->sections
[s
].x
+ WMWidthOfString((tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
),
938 &tb
->text
[tb
->sections
[s
].begin
],
939 tPtr
->tpos
- tb
->sections
[s
].begin
);
946 /* scroll the bars if the cursor is not visible */
947 if (tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
948 if (tPtr
->cursor
.y
+ tPtr
->cursor
.h
> tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
) {
950 (tPtr
->cursor
.y
+ tPtr
->cursor
.h
+ 10
951 - (tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
));
952 } else if (tPtr
->cursor
.y
< tPtr
->vpos
+ tPtr
->visible
.y
) {
953 tPtr
->vpos
-= (tPtr
->vpos
+ tPtr
->visible
.y
- tPtr
->cursor
.y
);
958 updateScrollers(tPtr
);
961 static void cursorToTextPosition(Text
* tPtr
, int x
, int y
)
963 TextBlock
*tb
= NULL
;
964 int done
= False
, s
, pos
, len
, _w
, _y
, dir
= 1; /* 1 == "down" */
967 if (tPtr
->flags
.needsLayOut
)
968 layOutDocument(tPtr
);
970 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
974 x
-= (tPtr
->visible
.x
- 2);
978 /* clicked is relative to document, not window... */
982 if (!(tb
= tPtr
->currentTextBlock
)) {
983 if (!(tb
= tPtr
->firstTextBlock
)) {
984 WMFont
*font
= tPtr
->dFont
;
986 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
993 /* first, which direction? Most likely, newly clicked
994 position will be close to previous */
995 if (!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
998 s
= (dir
? 0 : tb
->nsections
- 1);
999 if (y
>= tb
->sections
[s
]._y
&& y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1003 /* get the first (or last) section of the TextBlock that
1004 lies about the vertical click point */
1006 while (!done
&& tb
) {
1008 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1009 if ((dir
? tb
->next
: tb
->prior
))
1010 tb
= (dir
? tb
->next
: tb
->prior
);
1014 s
= (dir
? 0 : tb
->nsections
- 1);
1015 while (!done
&& (dir
? (s
< tb
->nsections
) : (s
>= 0))) {
1017 if ((dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) : (y
>= tb
->sections
[s
]._y
))) {
1025 if ((dir
? tb
->next
: tb
->prior
)) {
1026 tb
= (dir
? tb
->next
: tb
->prior
);
1029 break; /* goto _doneH; */
1034 if (s
< 0 || s
>= tb
->nsections
) {
1035 s
= (dir
? tb
->nsections
- 1 : 0);
1039 /* we have the line, which TextBlock on that line is it? */
1040 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1041 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1042 TextBlock
*hold
= tb
;
1043 tb
= getFirstNonGraphicBlockFor(hold
, dir
);
1056 _y
= tb
->sections
[s
]._y
;
1060 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1061 tb
= (dir
? tb
->next
: tb
->prior
);
1068 _w
= WMWidgetWidth(tb
->d
.widget
) - 5;
1070 _w
= tb
->d
.pixmap
->width
- 5;
1072 if (tb
->sections
[0].x
+ _w
>= x
)
1075 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1076 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1077 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1078 if (tb
->sections
[s
].x
+ _w
>= x
)
1083 if (tb
->sections
[s
].x
<= x
)
1087 if ((dir
? tb
->next
: tb
->prior
)) {
1088 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1089 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1090 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1092 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1093 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1098 if (_y
!= nxt
->sections
[dir
? 0 : nxt
->nsections
- 1]._y
) {
1099 /* this must be the last/first on this line. stop */
1100 pos
= (dir
? tb
->sections
[s
].end
: 0);
1101 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1105 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1107 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1108 } else if (pos
> tb
->sections
[s
].begin
) {
1110 WMWidthOfString(tb
->d
.font
,
1111 &(tb
->text
[tb
->sections
[s
].begin
]),
1112 pos
- tb
->sections
[s
].begin
);
1119 if ((dir
? tb
->next
: tb
->prior
)) {
1120 tb
= (dir
? tb
->next
: tb
->prior
);
1127 s
= (dir
? 0 : tb
->nsections
- 1);
1130 /* we have said TextBlock, now where within it? */
1133 int gw
= (tb
->object
? WMWidgetWidth(tb
->d
.widget
) : tb
->d
.pixmap
->width
);
1135 tPtr
->cursor
.x
= tb
->sections
[0].x
;
1137 if (x
> tPtr
->cursor
.x
+ gw
/ 2) {
1139 tPtr
->cursor
.x
+= gw
;
1141 printf("first %d\n", tb
->first
);
1143 if (tb
->prior
->graphic
)
1146 pos
= tb
->prior
->used
;
1157 WMFont
*f
= tb
->d
.font
;
1158 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1159 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1161 _w
= x
- tb
->sections
[s
].x
;
1164 while (pos
< len
&& WMWidthOfString(f
, text
, pos
+ 1) < _w
)
1167 tPtr
->cursor
.x
= tb
->sections
[s
].x
+ (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1169 pos
+= tb
->sections
[s
].begin
;
1175 tPtr
->tpos
= (pos
<= 1) ? pos
: 0;
1177 tPtr
->tpos
= (pos
< tb
->used
) ? pos
: tb
->used
;
1181 printf("...for this app will surely crash :-)\n");
1183 tPtr
->currentTextBlock
= tb
;
1184 tPtr
->cursor
.h
= tb
->sections
[s
].h
;
1185 tPtr
->cursor
.y
= tb
->sections
[s
]._y
;
1187 /* scroll the bars if the cursor is not visible */
1188 if (tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1189 if (tPtr
->cursor
.y
+ tPtr
->cursor
.h
> tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
) {
1191 (tPtr
->cursor
.y
+ tPtr
->cursor
.h
+ 10
1192 - (tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
));
1193 updateScrollers(tPtr
);
1194 } else if (tPtr
->cursor
.y
< tPtr
->vpos
+ tPtr
->visible
.y
) {
1195 tPtr
->vpos
-= (tPtr
->vpos
+ tPtr
->visible
.y
- tPtr
->cursor
.y
);
1196 updateScrollers(tPtr
);
1203 static void updateScrollers(Text
* tPtr
)
1206 if (tPtr
->flags
.frozen
)
1210 if (tPtr
->docHeight
<= tPtr
->visible
.h
) {
1211 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
1214 float hmax
= (float)(tPtr
->docHeight
);
1215 WMSetScrollerParameters(tPtr
->vS
,
1216 ((float)tPtr
->vpos
) / (hmax
- (float)tPtr
->visible
.h
),
1217 (float)tPtr
->visible
.h
/ hmax
);
1223 if (tPtr
->docWidth
<= tPtr
->visible
.w
) {
1224 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
1227 float wmax
= (float)(tPtr
->docWidth
);
1228 WMSetScrollerParameters(tPtr
->hS
,
1229 ((float)tPtr
->hpos
) / (wmax
- (float)tPtr
->visible
.w
),
1230 (float)tPtr
->visible
.w
/ wmax
);
1236 static void scrollersCallBack(WMWidget
* w
, void *self
)
1238 Text
*tPtr
= (Text
*) self
;
1239 Bool scroll
= False
;
1242 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1245 if (w
== tPtr
->vS
) {
1247 height
= tPtr
->visible
.h
;
1249 which
= WMGetScrollerHitPart(tPtr
->vS
);
1252 case WSDecrementLine
:
1253 if (tPtr
->vpos
> 0) {
1254 if (tPtr
->vpos
> 16)
1262 case WSIncrementLine
:{
1263 int limit
= tPtr
->docHeight
- height
;
1264 if (tPtr
->vpos
< limit
) {
1265 if (tPtr
->vpos
< limit
- 16)
1274 case WSDecrementPage
:
1275 if (((int)tPtr
->vpos
- (int)height
) >= 0)
1276 tPtr
->vpos
-= height
;
1283 case WSIncrementPage
:
1284 tPtr
->vpos
+= height
;
1285 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1286 tPtr
->vpos
= tPtr
->docHeight
- height
;
1291 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1292 * (float)(tPtr
->docHeight
- height
);
1300 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1301 tPtr
->prevVpos
= tPtr
->vpos
;
1304 if (w
== tPtr
->hS
) {
1305 int width
= tPtr
->visible
.w
;
1307 which
= WMGetScrollerHitPart(tPtr
->hS
);
1310 case WSDecrementLine
:
1311 if (tPtr
->hpos
> 0) {
1312 if (tPtr
->hpos
> 16)
1320 case WSIncrementLine
:{
1321 int limit
= tPtr
->docWidth
- width
;
1322 if (tPtr
->hpos
< limit
) {
1323 if (tPtr
->hpos
< limit
- 16)
1332 case WSDecrementPage
:
1333 if (((int)tPtr
->hpos
- (int)width
) >= 0)
1334 tPtr
->hpos
-= width
;
1341 case WSIncrementPage
:
1342 tPtr
->hpos
+= width
;
1343 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1344 tPtr
->hpos
= tPtr
->docWidth
- width
;
1349 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1350 * (float)(tPtr
->docWidth
- width
);
1358 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1359 tPtr
->prevHpos
= tPtr
->hpos
;
1363 updateScrollers(tPtr
);
1370 unsigned short begin
, end
; /* what part of the text block */
1373 static int layOutLine(Text
* tPtr
, myLineItems
* items
, int nitems
, int x
, int y
)
1375 int i
, j
= 0, lw
= 0, line_height
= 0, max_d
= 0, len
, n
;
1378 TextBlock
*tb
, *tbsame
= NULL
;
1380 if (!items
|| nitems
== 0)
1383 for (i
= 0; i
< nitems
; i
++) {
1387 if (!tPtr
->flags
.monoFont
) {
1389 WMWidget
*wdt
= tb
->d
.widget
;
1390 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1391 if (tPtr
->flags
.alignment
!= WALeft
)
1392 lw
+= WMWidgetWidth(wdt
);
1394 line_height
= WMAX(line_height
, tb
->d
.pixmap
->height
+ max_d
);
1395 if (tPtr
->flags
.alignment
!= WALeft
)
1396 lw
+= tb
->d
.pixmap
->width
;
1401 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1402 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1404 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1405 text
= &(tb
->text
[items
[i
].begin
]);
1406 len
= items
[i
].end
- items
[i
].begin
;
1407 if (tPtr
->flags
.alignment
!= WALeft
)
1408 lw
+= WMWidthOfString(font
, text
, len
);
1412 if (tPtr
->flags
.alignment
== WARight
) {
1413 j
= tPtr
->visible
.w
- lw
;
1414 } else if (tPtr
->flags
.alignment
== WACenter
) {
1415 j
= (int)((float)(tPtr
->visible
.w
- lw
)) / 2.0;
1418 for (i
= 0; i
< nitems
; i
++) {
1421 if (tbsame
== tb
) { /* extend it, since it's on same line */
1422 tb
->sections
[tb
->nsections
- 1].end
= items
[i
].end
;
1423 n
= tb
->nsections
- 1;
1425 tb
->sections
= wrealloc(tb
->sections
, (++tb
->nsections
) * sizeof(Section
));
1426 n
= tb
->nsections
- 1;
1427 tb
->sections
[n
]._y
= y
+ max_d
;
1428 tb
->sections
[n
].max_d
= max_d
;
1429 tb
->sections
[n
].x
= x
+ j
;
1430 tb
->sections
[n
].h
= line_height
;
1431 tb
->sections
[n
].begin
= items
[i
].begin
;
1432 tb
->sections
[n
].end
= items
[i
].end
;
1435 tb
->sections
[n
].last
= (i
+ 1 == nitems
);
1438 if (!tPtr
->flags
.monoFont
) {
1440 WMWidget
*wdt
= tb
->d
.widget
;
1441 tb
->sections
[n
].y
= max_d
+ y
+ line_height
- WMWidgetHeight(wdt
);
1442 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1444 tb
->sections
[n
].y
= y
+ line_height
+ max_d
- tb
->d
.pixmap
->height
;
1445 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1447 x
+= tb
->sections
[n
].w
;
1450 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1451 len
= items
[i
].end
- items
[i
].begin
;
1452 text
= &(tb
->text
[items
[i
].begin
]);
1454 tb
->sections
[n
].y
= y
+ line_height
- font
->y
;
1456 WMWidthOfString(font
,
1457 &(tb
->text
[tb
->sections
[n
].begin
]),
1458 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1460 x
+= WMWidthOfString(font
, text
, len
);
1470 static void layOutDocument(Text
* tPtr
)
1473 myLineItems
*items
= NULL
;
1474 unsigned int itemsSize
= 0, nitems
= 0, begin
, end
;
1476 unsigned int x
, y
= 0, lw
= 0, width
= 0, bmargin
;
1477 char *start
= NULL
, *mark
= NULL
;
1479 if (tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)))
1482 assert(tPtr
->visible
.w
> 20);
1484 tPtr
->docWidth
= tPtr
->visible
.w
;
1485 x
= tPtr
->margins
[tb
->marginN
].first
;
1486 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1488 /* only partial layOut needed: re-Lay only affected textblocks */
1489 if (tPtr
->flags
.laidOut
) {
1490 tb
= tPtr
->currentTextBlock
;
1492 /* search backwards for textblocks on same line */
1494 if (!tb
->sections
|| tb
->nsections
< 1) {
1495 tb
= tPtr
->firstTextBlock
;
1496 tPtr
->flags
.laidOut
= False
;
1501 if (!tb
->prior
->sections
|| tb
->prior
->nsections
< 1) {
1502 tb
= tPtr
->firstTextBlock
;
1503 tPtr
->flags
.laidOut
= False
;
1508 if (tb
->sections
[0]._y
!= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
) {
1514 if (tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
> 0) {
1515 y
= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
+
1516 tb
->prior
->sections
[tb
->prior
->nsections
- 1].h
-
1517 tb
->prior
->sections
[tb
->prior
->nsections
- 1].max_d
;
1526 if (tb
->sections
&& tb
->nsections
> 0) {
1527 wfree(tb
->sections
);
1528 tb
->sections
= NULL
;
1532 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1533 TextBlock
*next
= tb
->next
;
1534 tPtr
->currentTextBlock
= tb
;
1535 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1541 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1542 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1543 x
= tPtr
->margins
[tb
->marginN
].first
;
1544 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1550 if (!tPtr
->flags
.monoFont
) {
1552 width
= WMWidgetWidth(tb
->d
.widget
);
1554 width
= tb
->d
.pixmap
->width
;
1556 if (width
> tPtr
->docWidth
)
1557 tPtr
->docWidth
= width
;
1560 if (lw
>= tPtr
->visible
.w
- x
) {
1561 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1567 if (nitems
+ 1 > itemsSize
) {
1568 items
= wrealloc(items
, (++itemsSize
) * sizeof(myLineItems
));
1571 items
[nitems
].tb
= tb
;
1572 items
[nitems
].begin
= 0;
1573 items
[nitems
].end
= 0;
1577 } else if ((start
= tb
->text
)) {
1579 font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
1582 mark
= strchr(start
, ' ');
1584 end
+= (int)(mark
- start
) + 1;
1587 end
+= strlen(start
);
1594 if (end
- begin
> 0) {
1596 width
= WMWidthOfString(font
, &tb
->text
[begin
], end
- begin
);
1598 /* if it won't fit, char wrap it */
1599 if (width
>= tPtr
->visible
.w
) {
1600 char *t
= &tb
->text
[begin
];
1601 int l
= end
- begin
, i
= 0;
1603 width
= WMWidthOfString(font
, t
, ++i
);
1604 } while (width
< tPtr
->visible
.w
&& i
< l
);
1608 start
= &tb
->text
[end
];
1614 if (lw
>= tPtr
->visible
.w
- x
) {
1615 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1621 if (nitems
+ 1 > itemsSize
) {
1622 items
= wrealloc(items
, (++itemsSize
) * sizeof(myLineItems
));
1625 items
[nitems
].tb
= tb
;
1626 items
[nitems
].begin
= begin
;
1627 items
[nitems
].end
= end
;
1634 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1635 if (0 && tPtr
->flags
.laidOut
1636 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
> 0
1637 && (tPtr
->vpos
+ tPtr
->visible
.h
< tb
->next
->sections
[0]._y
)) {
1638 if (tPtr
->lastTextBlock
->sections
&& tPtr
->lastTextBlock
->nsections
> 0) {
1639 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1640 int ly
= ltb
->sections
[ltb
->nsections
- 1]._y
;
1641 int lh
= ltb
->sections
[ltb
->nsections
- 1].h
;
1644 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
- 1].max_d
;
1645 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
- 1].max_d
);
1647 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1649 sd
= tPtr
->docHeight
- y
;
1651 printf("dif %d-%d: %d\n", ss
, sd
, ss
- sd
);
1652 y
+= tb
->next
->sections
[0]._y
- y
;
1654 printf("nitems%d\n", nitems
);
1656 y
= tPtr
->docHeight
+ ss
- sd
;
1660 tPtr
->flags
.laidOut
= False
;
1668 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1670 if (tPtr
->docHeight
!= y
+ 10) {
1671 tPtr
->docHeight
= y
+ 10;
1672 updateScrollers(tPtr
);
1675 if (tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1678 tPtr
->flags
.horizOnDemand
= True
;
1679 WMSetTextHasHorizontalScroller((WMText
*) tPtr
, True
);
1680 event
.type
= Expose
;
1681 handleEvents(&event
, (void *)tPtr
);
1683 } else if (tPtr
->docWidth
<= tPtr
->visible
.w
&& tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1684 tPtr
->flags
.horizOnDemand
= False
;
1685 WMSetTextHasHorizontalScroller((WMText
*) tPtr
, False
);
1688 tPtr
->flags
.laidOut
= True
;
1690 if (items
&& itemsSize
> 0)
1695 static void textDidResize(W_ViewDelegate
* self
, WMView
* view
)
1697 Text
*tPtr
= (Text
*) view
->self
;
1698 unsigned short w
= tPtr
->view
->size
.width
;
1699 unsigned short h
= tPtr
->view
->size
.height
;
1700 unsigned short rh
= 0, vw
= 0, rel
;
1702 rel
= (tPtr
->flags
.relief
== WRFlat
);
1704 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1705 WMMoveWidget(tPtr
->ruler
, 2, 2);
1706 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1711 WMMoveWidget(tPtr
->vS
, 1 - (rel
? 1 : 0), rh
+ 1 - (rel
? 1 : 0));
1712 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
? 2 : 0));
1714 WMSetRulerOffset(tPtr
->ruler
, 22);
1716 WMSetRulerOffset(tPtr
->ruler
, 2);
1720 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1721 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1723 WMMoveWidget(tPtr
->hS
, vw
+ 1, h
- 21);
1724 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1728 tPtr
->visible
.x
= (tPtr
->vS
) ? 24 : 4;
1729 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) ? 43 : 3;
1730 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1731 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1732 tPtr
->visible
.h
-= (tPtr
->hS
) ? 20 : 0;
1733 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1735 if (tPtr
->view
->flags
.realized
) {
1738 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1739 tPtr
->db
= (Pixmap
) NULL
;
1742 if (tPtr
->visible
.w
< 40)
1743 tPtr
->visible
.w
= 40;
1744 if (tPtr
->visible
.h
< 20)
1745 tPtr
->visible
.h
= 20;
1748 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1749 tPtr
->view
->window
, tPtr
->visible
.w
,
1750 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1757 W_ViewDelegate _TextViewDelegate
= {
1765 #define TEXT_BUFFER_INCR 8
1766 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1768 static void clearText(Text
* tPtr
)
1770 tPtr
->vpos
= tPtr
->hpos
= 0;
1771 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1772 tPtr
->cursor
.x
= -23;
1774 if (!tPtr
->firstTextBlock
)
1777 while (tPtr
->currentTextBlock
)
1778 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1780 tPtr
->firstTextBlock
= NULL
;
1781 tPtr
->currentTextBlock
= NULL
;
1782 tPtr
->lastTextBlock
= NULL
;
1783 WMEmptyArray(tPtr
->gfxItems
);
1786 /* possibly remove a single character from the currentTextBlock,
1787 or if there's a selection, remove it...
1788 note that Delete and Backspace are treated differently */
1789 static void deleteTextInteractively(Text
* tPtr
, KeySym ksym
)
1792 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1793 Bool done
= 1, wasFirst
= 0;
1795 if (!tPtr
->flags
.editable
)
1798 if (!(tb
= tPtr
->currentTextBlock
))
1801 if (tPtr
->flags
.ownsSelection
) {
1802 if (removeSelection(tPtr
))
1803 layOutDocument(tPtr
);
1807 wasFirst
= tb
->first
;
1808 if (back
&& tPtr
->tpos
< 1) {
1810 if (tb
->prior
->blank
) {
1811 tPtr
->currentTextBlock
= tb
->prior
;
1812 WMRemoveTextBlock(tPtr
);
1813 tPtr
->currentTextBlock
= tb
;
1815 layOutDocument(tPtr
);
1819 TextBlock
*prior
= tb
->prior
;
1820 tPtr
->currentTextBlock
= tb
;
1821 WMRemoveTextBlock(tPtr
);
1830 tPtr
->tpos
= tb
->used
;
1832 tPtr
->currentTextBlock
= tb
;
1836 tb
->next
->first
= False
;
1837 layOutDocument(tPtr
);
1844 if ((tb
->used
> 0) && ((back
? tPtr
->tpos
> 0 : 1))
1845 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1848 memmove(&(tb
->text
[tPtr
->tpos
]), &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1853 /* if there are no characters left to back over in the textblock,
1854 but it still has characters to the right of the cursor: */
1855 if ((back
? (tPtr
->tpos
== 0 && !done
) : (tPtr
->tpos
>= tb
->used
))
1858 /* no more chars, and it's marked as blank? */
1860 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1862 if (tb
->used
== 0 || tb
->graphic
)
1863 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1866 tPtr
->currentTextBlock
= sibling
;
1868 tPtr
->tpos
= (back
? 1 : 0);
1870 tPtr
->tpos
= (back
? sibling
->used
: 0);
1872 /* no more chars, so mark it as blank */
1873 } else if (tb
->used
== 0) {
1875 } else if (tb
->graphic
) {
1876 Bool hasNext
= (tb
->next
!= NULL
);
1878 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1881 } else if (tPtr
->currentTextBlock
) {
1882 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
? 1 : tPtr
->currentTextBlock
->used
);
1885 printf("DEBUG: unaccounted for... catch this!\n");
1888 layOutDocument(tPtr
);
1891 static void insertTextInteractively(Text
* tPtr
, char *text
, int len
)
1894 char *newline
= NULL
;
1896 if (!tPtr
->flags
.editable
) {
1900 if (len
< 1 || !text
)
1903 if (tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1906 if (tPtr
->flags
.ownsSelection
)
1907 removeSelection(tPtr
);
1909 if (tPtr
->flags
.ignoreNewLine
) {
1911 for (i
= 0; i
< len
; i
++) {
1912 if (text
[i
] == '\n')
1917 tb
= tPtr
->currentTextBlock
;
1918 if (!tb
|| tb
->graphic
) {
1920 WMAppendTextStream(tPtr
, text
);
1921 layOutDocument(tPtr
);
1925 if ((newline
= strchr(text
, '\n'))) {
1926 int nlen
= (int)(newline
- text
);
1927 int s
= tb
->used
- tPtr
->tpos
;
1929 if (!tb
->blank
&& nlen
> 0) {
1934 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
1935 tb
->used
-= (tb
->used
- tPtr
->tpos
);
1937 insertTextInteractively(tPtr
, text
, nlen
);
1939 WMAppendTextStream(tPtr
, newline
);
1941 insertTextInteractively(tPtr
, save
, s
);
1945 if (tPtr
->tpos
> 0 && tPtr
->tpos
< tb
->used
&& !tb
->graphic
&& tb
->text
) {
1947 unsigned short savePos
= tPtr
->tpos
;
1948 void *ntb
= WMCreateTextBlockWithText(tPtr
, &tb
->text
[tPtr
->tpos
],
1949 tb
->d
.font
, tb
->color
, True
,
1950 tb
->used
- tPtr
->tpos
);
1952 if (tb
->sections
[0].end
== tPtr
->tpos
)
1953 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1955 tb
->color
, True
, 0));
1958 WMAppendTextBlock(tPtr
, ntb
);
1961 } else if (tPtr
->tpos
== tb
->used
) {
1962 if (tPtr
->flags
.indentNewLine
) {
1963 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1965 tb
->color
, True
, 4));
1968 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1970 tb
->color
, True
, 0));
1973 } else if (tPtr
->tpos
== 0) {
1974 if (tPtr
->flags
.indentNewLine
) {
1975 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1977 tb
->color
, True
, 4));
1979 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1981 tb
->color
, True
, 0));
1984 if (tPtr
->currentTextBlock
->next
)
1985 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
1989 if (tb
->used
+ len
>= tb
->allocated
) {
1990 tb
->allocated
= reqBlockSize(tb
->used
+ len
);
1991 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
1995 memcpy(tb
->text
, text
, len
);
1998 tb
->text
[tb
->used
] = 0;
2002 memmove(&(tb
->text
[tPtr
->tpos
+ len
]), &tb
->text
[tPtr
->tpos
], tb
->used
- tPtr
->tpos
+ 1);
2003 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2006 tb
->text
[tb
->used
] = 0;
2011 layOutDocument(tPtr
);
2014 static void selectRegion(Text
* tPtr
, int x
, int y
)
2020 y
+= (tPtr
->flags
.rulerShown
? 40 : 0);
2023 y
-= 10; /* the original offset */
2025 x
-= tPtr
->visible
.x
- 2;
2029 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2030 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2031 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2032 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2034 tPtr
->flags
.ownsSelection
= True
;
2038 static void releaseSelection(Text
* tPtr
)
2040 TextBlock
*tb
= tPtr
->firstTextBlock
;
2043 tb
->selected
= False
;
2046 tPtr
->flags
.ownsSelection
= False
;
2047 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2052 WMData
*requestHandler(WMView
* view
, Atom selection
, Atom target
, void *cdata
, Atom
* type
)
2054 Text
*tPtr
= view
->self
;
2055 Display
*dpy
= tPtr
->view
->screen
->display
;
2057 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2058 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2059 WMData
*data
= NULL
;
2061 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2062 char *text
= WMGetTextSelectedStream(tPtr
);
2065 data
= WMCreateDataWithBytes(text
, strlen(text
));
2066 WMSetDataFormat(data
, TYPETEXT
);
2071 printf("didn't get it\n");
2073 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2074 if (target
== _TARGETS
) {
2077 ptr
= wmalloc(4 * sizeof(Atom
));
2081 ptr
[3] = COMPOUND_TEXT
;
2083 data
= WMCreateDataWithBytes(ptr
, 4 * 4);
2084 WMSetDataFormat(data
, 32);
2093 static void lostHandler(WMView
* view
, Atom selection
, void *cdata
)
2095 releaseSelection((WMText
*) view
->self
);
2098 static WMSelectionProcs selectionHandler
= {
2099 requestHandler
, lostHandler
, NULL
2102 static void ownershipObserver(void *observerData
, WMNotification
* notification
)
2104 if (observerData
!= WMGetNotificationClientData(notification
))
2105 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2108 static void autoSelectText(Text
* tPtr
, int clicks
)
2112 char *mark
= NULL
, behind
, ahead
;
2114 if (!(tb
= tPtr
->currentTextBlock
))
2119 switch (tb
->text
[tPtr
->tpos
]) {
2123 case '<': case '>': behind = '<'; ahead = '>'; break;
2124 case '{': case '}': behind = '{'; ahead = '}'; break;
2125 case '[': case ']': behind = '['; ahead = ']'; break;
2128 behind
= ahead
= ' ';
2131 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
2132 tPtr
->sel
.h
= 6; /*tPtr->cursor.h-10; */
2135 tPtr
->sel
.x
= tb
->sections
[0].x
;
2136 tPtr
->sel
.w
= tb
->sections
[0].w
;
2138 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
2141 while (start
> 0 && tb
->text
[start
- 1] != behind
)
2145 if (tPtr
->tpos
> start
) {
2146 x
-= WMWidthOfString(font
, &tb
->text
[start
], tPtr
->tpos
- start
);
2148 tPtr
->sel
.x
= (x
< 0 ? 0 : x
) + 1;
2150 if ((mark
= strchr(&tb
->text
[start
], ahead
))) {
2151 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2152 (int)(mark
- &tb
->text
[start
]));
2153 } else if (tb
->used
> start
) {
2154 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
], tb
->used
- start
);
2158 } else if (clicks
== 3) {
2159 TextBlock
*cur
= tb
;
2161 while (tb
&& !tb
->first
) {
2164 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2167 while (tb
->next
&& !tb
->next
->first
) {
2170 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
- 1]._y
+ 5 - tPtr
->sel
.y
;
2173 tPtr
->sel
.w
= tPtr
->docWidth
;
2174 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2177 if (!tPtr
->flags
.ownsSelection
) {
2178 WMCreateSelectionHandler(tPtr
->view
, XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2179 tPtr
->flags
.ownsSelection
= True
;
2186 static void fontChanged(void *observerData
, WMNotification
* notification
)
2188 WMText
*tPtr
= (WMText
*) observerData
;
2189 WMFont
*font
= (WMFont
*) WMGetNotificationClientData(notification
);
2190 printf("fontChanged\n");
2195 if (tPtr
->flags
.ownsSelection
)
2196 WMSetTextSelectionFont(tPtr
, font
);
2200 static void handleTextKeyPress(Text
* tPtr
, XEvent
* event
)
2204 int control_pressed
= False
;
2205 TextBlock
*tb
= NULL
;
2207 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2208 control_pressed
= True
;
2209 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2214 if ((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2216 updateCursorPosition(tPtr
);
2221 if ((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2222 if (tPtr
->currentTextBlock
->graphic
)
2225 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2227 updateCursorPosition(tPtr
);
2232 if (!(tb
= tPtr
->currentTextBlock
))
2237 if (tPtr
->tpos
== 0) {
2240 tPtr
->currentTextBlock
= tb
->prior
;
2241 if (tPtr
->currentTextBlock
->graphic
)
2244 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2246 if (!tb
->first
&& tPtr
->tpos
> 0)
2252 updateCursorPosition(tPtr
);
2257 if (!(tb
= tPtr
->currentTextBlock
))
2261 if (tPtr
->tpos
== tb
->used
) {
2264 tPtr
->currentTextBlock
= tb
->next
;
2266 if (!tb
->next
->first
&& tb
->next
->used
> 0)
2272 tPtr
->tpos
= tb
->used
;
2276 updateCursorPosition(tPtr
);
2281 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2282 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2287 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2288 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2297 deleteTextInteractively(tPtr
, ksym
);
2298 updateCursorPosition(tPtr
);
2304 control_pressed
= True
;
2308 insertTextInteractively(tPtr
, " ", 4);
2309 updateCursorPosition(tPtr
);
2316 if (*buffer
!= 0 && !control_pressed
) {
2317 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2318 updateCursorPosition(tPtr
);
2321 } else if (control_pressed
&& ksym
== XK_r
) {
2322 Bool i
= !tPtr
->flags
.rulerShown
;
2323 WMShowTextRuler(tPtr
, i
);
2324 tPtr
->flags
.rulerShown
= i
;
2325 } else if (control_pressed
&& *buffer
== '\a') {
2326 XBell(tPtr
->view
->screen
->display
, 0);
2328 WMRelayToNextResponder(tPtr
->view
, event
);
2332 if (!control_pressed
&& tPtr
->flags
.ownsSelection
) {
2333 releaseSelection(tPtr
);
2337 static void pasteText(WMView
* view
, Atom selection
, Atom target
, Time timestamp
, void *cdata
, WMData
* data
)
2339 Text
*tPtr
= (Text
*) view
->self
;
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
;
2549 if (tPtr
->flags
.pointerGrabbed
) {
2550 tPtr
->flags
.pointerGrabbed
= False
;
2551 XUngrabPointer(dpy
, CurrentTime
);
2555 if (tPtr
->flags
.waitingForSelection
)
2558 if (WMIsDraggingFromView(tPtr
->view
))
2559 WMDragImageFromView(tPtr
->view
, event
);
2564 static void handleEvents(XEvent
* event
, void *data
)
2566 Text
*tPtr
= (Text
*) data
;
2568 switch (event
->type
) {
2571 if (event
->xexpose
.count
!= 0)
2575 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2576 WMRealizeWidget(tPtr
->hS
);
2580 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2581 WMRealizeWidget(tPtr
->vS
);
2585 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2586 WMRealizeWidget(tPtr
->ruler
);
2591 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2597 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2600 tPtr
->flags
.focused
= True
;
2602 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2603 tPtr
->timerID
= WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY
, blinkCursor
, tPtr
);
2610 tPtr
->flags
.focused
= False
;
2613 if (tPtr
->timerID
) {
2614 WMDeleteTimerHandler(tPtr
->timerID
);
2615 tPtr
->timerID
= NULL
;
2623 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2625 WMEmptyArray(tPtr
->gfxItems
);
2628 WMDeleteTimerHandler(tPtr
->timerID
);
2630 WMReleaseFont(tPtr
->dFont
);
2631 WMReleaseColor(tPtr
->dColor
);
2632 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2633 WMRemoveNotificationObserver(tPtr
);
2635 WMFreeArray(tPtr
->xdndSourceTypes
);
2636 WMFreeArray(tPtr
->xdndDestinationTypes
);
2645 static void insertPlainText(Text
* tPtr
, char *text
)
2652 mark
= strchr(start
, '\n');
2654 tb
= WMCreateTextBlockWithText(tPtr
,
2656 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
- start
));
2658 tPtr
->flags
.first
= True
;
2660 if (start
&& strlen(start
)) {
2661 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2662 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2665 tPtr
->flags
.first
= False
;
2669 if (tPtr
->flags
.prepend
)
2670 WMPrependTextBlock(tPtr
, tb
);
2672 WMAppendTextBlock(tPtr
, tb
);
2676 static void rulerMoveCallBack(WMWidget
* w
, void *self
)
2678 Text
*tPtr
= (Text
*) self
;
2682 if (W_CLASS(tPtr
) != WC_Text
)
2688 static void rulerReleaseCallBack(WMWidget
* w
, void *self
)
2690 Text
*tPtr
= (Text
*) self
;
2694 if (W_CLASS(tPtr
) != WC_Text
)
2701 static WMArray
*dropDataTypes(WMView
* self
)
2703 return ((Text
*) self
->self
)->xdndSourceTypes
;
2706 static WMDragOperationType
wantedDropOperation(WMView
* self
)
2708 return WDOperationCopy
;
2711 static Bool
acceptDropOperation(WMView
* self
, WMDragOperationType allowedOperation
)
2713 return (allowedOperation
== WDOperationCopy
);
2716 static WMData
*fetchDragData(WMView
* self
, char *type
)
2718 TextBlock
*tb
= ((WMText
*) self
->self
)->currentTextBlock
;
2722 if (strcmp(type
, "text/plain")) {
2726 desc
= wmalloc(tb
->used
+ 1);
2727 memcpy(desc
, tb
->text
, tb
->used
);
2729 data
= WMCreateDataWithBytes(desc
, strlen(desc
) + 1);
2739 static WMDragSourceProcs _DragSourceProcs
= {
2741 wantedDropOperation
,
2743 acceptDropOperation
,
2749 static WMArray
*requiredDataTypes(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2751 return ((Text
*) self
->self
)->xdndDestinationTypes
;
2754 static WMDragOperationType
allowedOperation(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2756 return WDOperationCopy
;
2759 static void performDragOperation(WMView
* self
, WMArray
* dropData
, WMArray
* operations
, WMPoint
* dropLocation
)
2761 WMText
*tPtr
= (WMText
*) self
->self
;
2768 /* only one required type, implies only one drop data */
2770 /* get application/X-color if any */
2771 data
= (WMData
*) WMPopFromArray(dropData
);
2773 colorName
= (char *)WMDataBytes(data
);
2774 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
2777 WMSetTextSelectionColor(tPtr
, color
);
2778 WMReleaseColor(color
);
2784 static WMDragDestinationProcs _DragDestinationProcs
= {
2789 performDragOperation
,
2793 char *getStream(WMText
* tPtr
, int sel
, int array
)
2795 TextBlock
*tb
= NULL
;
2797 unsigned long where
= 0;
2802 if (!(tb
= tPtr
->firstTextBlock
))
2806 (tPtr
->writer
) (tPtr
, (void *)text
);
2810 tb
= tPtr
->firstTextBlock
;
2813 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2815 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2817 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2818 && tb
!= tPtr
->firstTextBlock
) {
2819 text
= wrealloc(text
, where
+ 1);
2820 text
[where
++] = '\n';
2826 if (tb
->graphic
&& array
) {
2827 text
= wrealloc(text
, where
+ 4);
2828 text
[where
++] = 0xFA;
2829 text
[where
++] = (tb
->used
>> 8) & 0x0ff;
2830 text
[where
++] = tb
->used
& 0x0ff;
2831 text
[where
++] = tb
->allocated
; /* extra info */
2833 text
= wrealloc(text
, where
+ tb
->used
);
2834 memcpy(&text
[where
], tb
->text
, tb
->used
);
2837 } else if (sel
&& tb
->selected
) {
2839 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
2840 text
= wrealloc(text
, where
+ 1);
2841 text
[where
++] = '\n';
2847 text
= wrealloc(text
, where
+ (tb
->s_end
- tb
->s_begin
));
2848 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
], tb
->s_end
- tb
->s_begin
);
2849 where
+= tb
->s_end
- tb
->s_begin
;
2854 _gSnext
: tb
= tb
->next
;
2857 /* +1 for the end of string, let's be nice */
2858 text
= wrealloc(text
, where
+ 1);
2863 static void releaseStreamObjects(void *data
)
2869 WMArray
*getStreamObjects(WMText
* tPtr
, int sel
)
2871 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
2875 char *start
, *fa
, *desc
;
2877 stream
= getStream(tPtr
, sel
, 1);
2884 fa
= strchr(start
, 0xFA);
2886 if ((int)(fa
- start
) > 0) {
2888 desc
[(int)(fa
- start
)] = 0;
2889 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
2890 WMSetDataFormat(data
, TYPETEXT
);
2891 WMAddToArray(array
, (void *)data
);
2894 len
= *(fa
+ 1) * 0xff + *(fa
+ 2);
2895 data
= WMCreateDataWithBytes((void *)(fa
+ 4), len
);
2896 WMSetDataFormat(data
, *(fa
+ 3));
2897 WMAddToArray(array
, (void *)data
);
2898 start
= fa
+ len
+ 4;
2901 if (start
&& strlen(start
)) {
2902 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
2903 WMSetDataFormat(data
, TYPETEXT
);
2904 WMAddToArray(array
, (void *)data
);
2914 #define XDND_TEXT_DATA_TYPE "text/plain"
2915 #define XDND_COLOR_DATA_TYPE "application/X-color"
2916 static WMArray
*getXdndSourceTypeArray()
2918 WMArray
*types
= WMCreateArray(1);
2919 WMAddToArray(types
, XDND_TEXT_DATA_TYPE
);
2923 static WMArray
*getXdndDestinationTypeArray()
2925 WMArray
*types
= WMCreateArray(1);
2926 WMAddToArray(types
, XDND_COLOR_DATA_TYPE
);
2930 WMText
*WMCreateTextForDocumentType(WMWidget
* parent
, WMAction
* parser
, WMAction
* writer
)
2937 tPtr
= wmalloc(sizeof(Text
));
2938 tPtr
->widgetClass
= WC_Text
;
2939 tPtr
->view
= W_CreateView(W_VIEW(parent
));
2941 perror("could not create text's view\n");
2946 dpy
= tPtr
->view
->screen
->display
;
2947 scr
= tPtr
->view
->screen
;
2949 tPtr
->view
->self
= tPtr
;
2950 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
2951 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
2952 W_ResizeView(tPtr
->view
, 250, 200);
2954 tPtr
->dColor
= WMBlackColor(scr
);
2955 tPtr
->fgColor
= WMBlackColor(scr
);
2956 tPtr
->bgColor
= WMWhiteColor(scr
);
2957 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
2959 gcv
.graphics_exposures
= False
;
2960 gcv
.foreground
= W_PIXEL(scr
->gray
);
2961 gcv
.background
= W_PIXEL(scr
->darkGray
);
2962 gcv
.fill_style
= FillStippled
;
2963 /* why not use scr->stipple here? */
2964 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
, STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
2965 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
2966 GCForeground
| GCBackground
| GCStipple
2967 | GCFillStyle
| GCGraphicsExposures
, &gcv
);
2973 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
2975 tPtr
->view
->delegate
= &_TextViewDelegate
;
2977 tPtr
->delegate
= NULL
;
2980 tPtr
->timerID
= NULL
;
2983 WMCreateEventHandler(tPtr
->view
, ExposureMask
| StructureNotifyMask
2984 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
, handleEvents
, tPtr
);
2986 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
| ButtonPressMask
2987 | KeyReleaseMask
| KeyPressMask
| Button1MotionMask
, handleActionEvents
, tPtr
);
2989 WMAddNotificationObserver(ownershipObserver
, tPtr
, WMSelectionOwnerDidChangeNotification
, tPtr
);
2991 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
2992 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
2995 WMArray
*types
= WMCreateArray(2);
2996 WMAddToArray(types
, "application/X-color");
2997 WMAddToArray(types
, "application/X-image");
2998 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3001 /*WMAddNotificationObserver(fontChanged, tPtr,
3002 WMFontPanelDidChangeNotification, tPtr); */
3004 tPtr
->firstTextBlock
= NULL
;
3005 tPtr
->lastTextBlock
= NULL
;
3006 tPtr
->currentTextBlock
= NULL
;
3009 tPtr
->gfxItems
= WMCreateArray(4);
3011 tPtr
->parser
= parser
;
3012 tPtr
->writer
= writer
;
3014 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3015 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3017 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3019 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3020 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3021 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3023 tPtr
->cursor
.x
= -23;
3026 tPtr
->docHeight
= 0;
3027 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
, default_bullet
);
3028 tPtr
->db
= (Pixmap
) NULL
;
3029 tPtr
->bgPixmap
= NULL
;
3031 tPtr
->margins
= WMGetRulerMargins(NULL
);
3032 tPtr
->margins
->right
= tPtr
->visible
.w
;
3035 tPtr
->flags
.rulerShown
= False
;
3036 tPtr
->flags
.monoFont
= False
;
3037 tPtr
->flags
.focused
= False
;
3038 tPtr
->flags
.editable
= True
;
3039 tPtr
->flags
.ownsSelection
= False
;
3040 tPtr
->flags
.pointerGrabbed
= False
;
3041 tPtr
->flags
.extendSelection
= False
;
3042 tPtr
->flags
.frozen
= False
;
3043 tPtr
->flags
.cursorShown
= True
;
3044 tPtr
->flags
.acceptsGraphic
= False
;
3045 tPtr
->flags
.horizOnDemand
= False
;
3046 tPtr
->flags
.needsLayOut
= False
;
3047 tPtr
->flags
.ignoreNewLine
= False
;
3048 tPtr
->flags
.indentNewLine
= False
;
3049 tPtr
->flags
.laidOut
= False
;
3050 tPtr
->flags
.ownsSelection
= False
;
3051 tPtr
->flags
.waitingForSelection
= False
;
3052 tPtr
->flags
.prepend
= False
;
3053 tPtr
->flags
.isOverGraphic
= False
;
3054 tPtr
->flags
.relief
= WRSunken
;
3055 tPtr
->flags
.isOverGraphic
= 0;
3056 tPtr
->flags
.alignment
= WALeft
;
3057 tPtr
->flags
.first
= True
;
3059 tPtr
->xdndSourceTypes
= getXdndSourceTypeArray();
3060 tPtr
->xdndDestinationTypes
= getXdndDestinationTypeArray();
3065 void WMPrependTextStream(WMText
* tPtr
, char *text
)
3067 CHECK_CLASS(tPtr
, WC_Text
);
3070 if (tPtr
->flags
.ownsSelection
)
3071 releaseSelection(tPtr
);
3073 updateScrollers(tPtr
);
3077 tPtr
->flags
.prepend
= True
;
3078 if (text
&& tPtr
->parser
)
3079 (tPtr
->parser
) (tPtr
, (void *)text
);
3081 insertPlainText(tPtr
, text
);
3083 tPtr
->flags
.needsLayOut
= True
;
3085 if (!tPtr
->flags
.frozen
) {
3086 layOutDocument(tPtr
);
3090 void WMAppendTextStream(WMText
* tPtr
, char *text
)
3092 CHECK_CLASS(tPtr
, WC_Text
);
3095 if (tPtr
->flags
.ownsSelection
)
3096 releaseSelection(tPtr
);
3098 updateScrollers(tPtr
);
3102 tPtr
->flags
.prepend
= False
;
3103 if (text
&& tPtr
->parser
)
3104 (tPtr
->parser
) (tPtr
, (void *)text
);
3106 insertPlainText(tPtr
, text
);
3108 tPtr
->flags
.needsLayOut
= True
;
3109 if (tPtr
->currentTextBlock
) {
3110 if (tPtr
->currentTextBlock
->graphic
)
3113 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3116 if (!tPtr
->flags
.frozen
) {
3117 layOutDocument(tPtr
);
3121 char *WMGetTextStream(WMText
* tPtr
)
3123 CHECK_CLASS(tPtr
, WC_Text
);
3125 return getStream(tPtr
, 0, 0);
3128 char *WMGetTextSelectedStream(WMText
* tPtr
)
3130 CHECK_CLASS(tPtr
, WC_Text
);
3132 return getStream(tPtr
, 1, 0);
3135 WMArray
*WMGetTextObjects(WMText
* tPtr
)
3137 CHECK_CLASS(tPtr
, WC_Text
);
3139 return getStreamObjects(tPtr
, 0);
3142 WMArray
*WMGetTextSelectedObjects(WMText
* tPtr
)
3144 CHECK_CLASS(tPtr
, WC_Text
);
3146 return getStreamObjects(tPtr
, 1);
3149 void WMSetTextDelegate(WMText
* tPtr
, WMTextDelegate
* delegate
)
3151 CHECK_CLASS(tPtr
, WC_Text
);
3153 tPtr
->delegate
= delegate
;
3156 void *WMCreateTextBlockWithObject(WMText
* tPtr
, WMWidget
* w
,
3157 char *description
, WMColor
* color
,
3158 unsigned short first
, unsigned short extraInfo
)
3162 if (!w
|| !description
|| !color
)
3165 tb
= wmalloc(sizeof(TextBlock
));
3167 tb
->text
= wstrdup(description
);
3168 tb
->used
= strlen(description
);
3171 tb
->color
= WMRetainColor(color
);
3172 tb
->marginN
= newMargin(tPtr
, NULL
);
3173 tb
->allocated
= extraInfo
;
3178 tb
->underlined
= False
;
3179 tb
->selected
= False
;
3181 tb
->sections
= NULL
;
3189 void *WMCreateTextBlockWithPixmap(WMText
* tPtr
, WMPixmap
* p
,
3190 char *description
, WMColor
* color
,
3191 unsigned short first
, unsigned short extraInfo
)
3195 if (!p
|| !description
|| !color
)
3198 tb
= wmalloc(sizeof(TextBlock
));
3200 tb
->text
= wstrdup(description
);
3201 tb
->used
= strlen(description
);
3203 tb
->d
.pixmap
= WMRetainPixmap(p
);
3204 tb
->color
= WMRetainColor(color
);
3205 tb
->marginN
= newMargin(tPtr
, NULL
);
3206 tb
->allocated
= extraInfo
;
3211 tb
->underlined
= False
;
3212 tb
->selected
= False
;
3214 tb
->sections
= NULL
;
3222 void *WMCreateTextBlockWithText(WMText
* tPtr
, char *text
, WMFont
* font
, WMColor
* color
,
3223 unsigned short first
, unsigned short len
)
3227 if (!font
|| !color
)
3230 tb
= wmalloc(sizeof(TextBlock
));
3232 tb
->allocated
= reqBlockSize(len
);
3233 tb
->text
= (char *)wmalloc(tb
->allocated
);
3235 if (len
< 1 || !text
|| (*text
== '\n' && len
== 1)) {
3240 memcpy(tb
->text
, text
, len
);
3244 tb
->text
[tb
->used
] = 0;
3246 tb
->d
.font
= WMRetainFont(font
);
3247 tb
->color
= WMRetainColor(color
);
3248 tb
->marginN
= newMargin(tPtr
, NULL
);
3251 tb
->graphic
= False
;
3252 tb
->underlined
= False
;
3253 tb
->selected
= False
;
3255 tb
->sections
= NULL
;
3263 WMSetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int first
,
3264 unsigned int kanji
, unsigned int underlined
, int script
, WMRulerMargins
* margins
)
3266 TextBlock
*tb
= (TextBlock
*) vtb
;
3272 tb
->underlined
= underlined
;
3273 tb
->script
= script
;
3274 tb
->marginN
= newMargin(tPtr
, margins
);
3278 WMGetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int *first
,
3279 unsigned int *kanji
, unsigned int *underlined
, int *script
, WMRulerMargins
* margins
)
3281 TextBlock
*tb
= (TextBlock
*) vtb
;
3290 *underlined
= tb
->underlined
;
3292 *script
= tb
->script
;
3294 margins
= &tPtr
->margins
[tb
->marginN
];
3297 void WMPrependTextBlock(WMText
* tPtr
, void *vtb
)
3299 TextBlock
*tb
= (TextBlock
*) vtb
;
3306 WMWidget
*w
= tb
->d
.widget
;
3307 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3308 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3309 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3312 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3316 tPtr
->tpos
= tb
->used
;
3319 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3320 tb
->next
= tb
->prior
= NULL
;
3322 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3327 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3330 tb
->next
= tPtr
->currentTextBlock
;
3331 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3332 if (tPtr
->currentTextBlock
->prior
)
3333 tPtr
->currentTextBlock
->prior
->next
= tb
;
3335 tPtr
->currentTextBlock
->prior
= tb
;
3337 tPtr
->firstTextBlock
= tb
;
3339 tPtr
->currentTextBlock
= tb
;
3342 void WMAppendTextBlock(WMText
* tPtr
, void *vtb
)
3344 TextBlock
*tb
= (TextBlock
*) vtb
;
3351 WMWidget
*w
= tb
->d
.widget
;
3352 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3353 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3354 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3357 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3361 tPtr
->tpos
= tb
->used
;
3364 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3365 tb
->next
= tb
->prior
= NULL
;
3367 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3372 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3375 tb
->next
= tPtr
->currentTextBlock
->next
;
3376 tb
->prior
= tPtr
->currentTextBlock
;
3377 if (tPtr
->currentTextBlock
->next
)
3378 tPtr
->currentTextBlock
->next
->prior
= tb
;
3380 tPtr
->currentTextBlock
->next
= tb
;
3383 tPtr
->lastTextBlock
= tb
;
3385 tPtr
->currentTextBlock
= tb
;
3388 void *WMRemoveTextBlock(WMText
* tPtr
)
3390 TextBlock
*tb
= NULL
;
3392 if (!tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
|| !tPtr
->currentTextBlock
) {
3396 tb
= tPtr
->currentTextBlock
;
3398 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3401 WMUnmapWidget(tb
->d
.widget
);
3405 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3406 if (tPtr
->currentTextBlock
->next
)
3407 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3409 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3410 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3412 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3413 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3414 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3415 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3417 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3418 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3419 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3426 static void destroyWidget(WMWidget
* widget
)
3428 WMDestroyWidget(widget
);
3429 // -- never do this -- wfree(widget);
3433 void WMDestroyTextBlock(WMText
* tPtr
, void *vtb
)
3435 TextBlock
*tb
= (TextBlock
*) vtb
;
3441 /* naturally, there's a danger to destroying widgets whose action
3442 * brings us here: ie. press a button to destroy it...
3443 * need to find a safer way. till then... this stays commented out */
3444 /* 5 months later... destroy it 10 seconds after now which should
3445 * be enough time for the widget's action to be completed... :-) */
3446 /* This is a bad assumption. Just destroy the widget here.
3447 * if the caller needs it, it can protect it with W_RetainView()
3448 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3449 WMDestroyWidget(tb
->d
.widget
);
3451 WMReleasePixmap(tb
->d
.pixmap
);
3454 WMReleaseFont(tb
->d
.font
);
3457 WMReleaseColor(tb
->color
);
3458 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3460 wfree(tb
->sections
);
3465 void WMSetTextForegroundColor(WMText
* tPtr
, WMColor
* color
)
3468 WMReleaseColor(tPtr
->fgColor
);
3470 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3475 void WMSetTextBackgroundColor(WMText
* tPtr
, WMColor
* color
)
3478 WMReleaseColor(tPtr
->bgColor
);
3480 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3481 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3486 void WMSetTextBackgroundPixmap(WMText
* tPtr
, WMPixmap
* pixmap
)
3489 WMReleasePixmap(tPtr
->bgPixmap
);
3492 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3494 tPtr
->bgPixmap
= NULL
;
3497 void WMSetTextRelief(WMText
* tPtr
, WMReliefType relief
)
3499 tPtr
->flags
.relief
= relief
;
3500 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3503 void WMSetTextHasHorizontalScroller(WMText
* tPtr
, Bool shouldhave
)
3505 if (shouldhave
&& !tPtr
->hS
) {
3506 tPtr
->hS
= WMCreateScroller(tPtr
);
3507 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3508 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3509 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3510 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3511 WMMapWidget(tPtr
->hS
);
3512 } else if (!shouldhave
&& tPtr
->hS
) {
3513 WMUnmapWidget(tPtr
->hS
);
3514 WMDestroyWidget(tPtr
->hS
);
3520 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3523 void WMSetTextHasRuler(WMText
* tPtr
, Bool shouldhave
)
3525 if (shouldhave
&& !tPtr
->ruler
) {
3526 tPtr
->ruler
= WMCreateRuler(tPtr
);
3527 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3528 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3529 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3530 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3531 } else if (!shouldhave
&& tPtr
->ruler
) {
3532 WMShowTextRuler(tPtr
, False
);
3533 WMDestroyWidget(tPtr
->ruler
);
3536 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3539 void WMShowTextRuler(WMText
* tPtr
, Bool show
)
3544 if (tPtr
->flags
.monoFont
)
3547 tPtr
->flags
.rulerShown
= show
;
3549 WMMapWidget(tPtr
->ruler
);
3551 WMUnmapWidget(tPtr
->ruler
);
3554 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3557 Bool
WMGetTextRulerShown(WMText
* tPtr
)
3562 return tPtr
->flags
.rulerShown
;
3565 void WMSetTextHasVerticalScroller(WMText
* tPtr
, Bool shouldhave
)
3567 if (shouldhave
&& !tPtr
->vS
) {
3568 tPtr
->vS
= WMCreateScroller(tPtr
);
3569 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3570 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3571 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3572 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3573 WMMapWidget(tPtr
->vS
);
3574 } else if (!shouldhave
&& tPtr
->vS
) {
3575 WMUnmapWidget(tPtr
->vS
);
3576 WMDestroyWidget(tPtr
->vS
);
3582 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3585 Bool
WMScrollText(WMText
* tPtr
, int amount
)
3587 Bool scroll
= False
;
3589 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3593 if (tPtr
->vpos
> 0) {
3594 if (tPtr
->vpos
> abs(amount
))
3595 tPtr
->vpos
+= amount
;
3601 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3602 if (tPtr
->vpos
< limit
) {
3603 if (tPtr
->vpos
< limit
- amount
)
3604 tPtr
->vpos
+= amount
;
3611 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3612 updateScrollers(tPtr
);
3615 tPtr
->prevVpos
= tPtr
->vpos
;
3619 Bool
WMPageText(WMText
* tPtr
, Bool direction
)
3621 if (!tPtr
->view
->flags
.realized
)
3624 return WMScrollText(tPtr
, direction
? tPtr
->visible
.h
: -tPtr
->visible
.h
);
3627 void WMSetTextEditable(WMText
* tPtr
, Bool editable
)
3629 tPtr
->flags
.editable
= editable
;
3632 int WMGetTextEditable(WMText
* tPtr
)
3634 return tPtr
->flags
.editable
;
3637 void WMSetTextIndentNewLines(WMText
* tPtr
, Bool indent
)
3639 tPtr
->flags
.indentNewLine
= indent
;
3642 void WMSetTextIgnoresNewline(WMText
* tPtr
, Bool ignore
)
3644 tPtr
->flags
.ignoreNewLine
= ignore
;
3647 Bool
WMGetTextIgnoresNewline(WMText
* tPtr
)
3649 return tPtr
->flags
.ignoreNewLine
;
3652 void WMSetTextUsesMonoFont(WMText
* tPtr
, Bool mono
)
3655 if (tPtr
->flags
.rulerShown
)
3656 WMShowTextRuler(tPtr
, False
);
3657 if (tPtr
->flags
.alignment
!= WALeft
)
3658 tPtr
->flags
.alignment
= WALeft
;
3661 tPtr
->flags
.monoFont
= mono
;
3662 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3665 Bool
WMGetTextUsesMonoFont(WMText
* tPtr
)
3667 return tPtr
->flags
.monoFont
;
3670 void WMSetTextDefaultFont(WMText
* tPtr
, WMFont
* font
)
3673 WMReleaseFont(tPtr
->dFont
);
3676 tPtr
->dFont
= WMRetainFont(font
);
3678 tPtr
->dFont
= WMSystemFontOfSize(tPtr
->view
->screen
, 12);
3682 WMFont
*WMGetTextDefaultFont(WMText
* tPtr
)
3684 return WMRetainFont(tPtr
->dFont
);
3687 void WMSetTextDefaultColor(WMText
* tPtr
, WMColor
* color
)
3690 WMReleaseColor(tPtr
->dColor
);
3693 tPtr
->dColor
= WMRetainColor(color
);
3695 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3699 WMColor
*WMGetTextDefaultColor(WMText
* tPtr
)
3701 return tPtr
->dColor
;
3704 void WMSetTextAlignment(WMText
* tPtr
, WMAlignment alignment
)
3706 if (tPtr
->flags
.monoFont
)
3707 tPtr
->flags
.alignment
= WALeft
;
3709 tPtr
->flags
.alignment
= alignment
;
3713 int WMGetTextInsertType(WMText
* tPtr
)
3715 return tPtr
->flags
.prepend
;
3718 void WMSetTextSelectionColor(WMText
* tPtr
, WMColor
* color
)
3720 setSelectionProperty(tPtr
, NULL
, color
, -1);
3723 WMColor
*WMGetTextSelectionColor(WMText
* tPtr
)
3727 tb
= tPtr
->currentTextBlock
;
3729 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3738 void WMSetTextSelectionFont(WMText
* tPtr
, WMFont
* font
)
3740 setSelectionProperty(tPtr
, font
, NULL
, -1);
3743 WMFont
*WMGetTextSelectionFont(WMText
* tPtr
)
3747 tb
= tPtr
->currentTextBlock
;
3749 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3756 tb
= getFirstNonGraphicBlockFor(tb
, 1);
3760 return (tb
->selected
? tb
->d
.font
: NULL
);
3763 void WMSetTextSelectionUnderlined(WMText
* tPtr
, int underlined
)
3766 if (underlined
!= 0 && underlined
!= 1)
3769 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
3772 int WMGetTextSelectionUnderlined(WMText
* tPtr
)
3776 tb
= tPtr
->currentTextBlock
;
3778 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3784 return tb
->underlined
;
3787 void WMFreezeText(WMText
* tPtr
)
3789 tPtr
->flags
.frozen
= True
;
3792 void WMThawText(WMText
* tPtr
)
3794 tPtr
->flags
.frozen
= False
;
3796 if (tPtr
->flags
.monoFont
) {
3797 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
3800 /* make sure to unmap widgets no matter where they are */
3801 /* they'll be later remapped if needed by paintText */
3802 for (j
= 0; j
< c
; j
++) {
3803 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
3804 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
3805 WMUnmapWidget(tb
->d
.widget
);
3810 tPtr
->flags
.laidOut
= False
;
3811 layOutDocument(tPtr
);
3812 updateScrollers(tPtr
);
3814 tPtr
->flags
.needsLayOut
= False
;
3818 /* find first occurence of a string */
3819 static char *mystrstr(char *haystack
, char *needle
, unsigned short len
, char *end
, Bool caseSensitive
)
3823 if (!haystack
|| !needle
|| !end
)
3826 for (ptr
= haystack
; ptr
< end
; ptr
++) {
3827 if (caseSensitive
) {
3828 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3832 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3840 /* find last occurence of a string */
3841 static char *mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
, Bool caseSensitive
)
3845 if (!haystack
|| !needle
|| !end
)
3848 for (ptr
= haystack
- 2; ptr
> end
; ptr
--) {
3849 if (caseSensitive
) {
3850 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3853 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3861 Bool
WMFindInTextStream(WMText
* tPtr
, char *needle
, Bool direction
, Bool caseSensitive
)
3868 if (!(tb
= tPtr
->currentTextBlock
)) {
3869 if (!(tb
= ((direction
> 0) ? tPtr
->firstTextBlock
: tPtr
->lastTextBlock
))) {
3873 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3874 tb = (direction>0) ? tb->next : tb->prior; */
3875 if (tb
!= tPtr
->lastTextBlock
)
3879 tb
= tPtr
->currentTextBlock
;
3885 if (direction
> 0) {
3886 if (pos
+ 1 < tb
->used
)
3889 if (tb
->used
- pos
> 0 && pos
> 0) {
3890 mark
= mystrstr(&tb
->text
[pos
], needle
,
3891 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
3904 mark
= mystrrstr(&tb
->text
[pos
], needle
,
3905 strlen(needle
), tb
->text
, caseSensitive
);
3916 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
3918 tPtr
->tpos
= (int)(mark
- tb
->text
);
3919 tPtr
->currentTextBlock
= tb
;
3920 updateCursorPosition(tPtr
);
3921 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
3922 tPtr
->sel
.h
= tPtr
->cursor
.h
- 10;
3923 tPtr
->sel
.x
= tPtr
->cursor
.x
+ 1;
3924 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
3925 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
3926 tPtr
->docWidth
- tPtr
->sel
.x
);
3927 tPtr
->flags
.ownsSelection
= True
;
3934 tb
= (direction
> 0) ? tb
->next
: tb
->prior
;
3936 pos
= (direction
> 0) ? 0 : tb
->used
;
3943 Bool
WMReplaceTextSelection(WMText
* tPtr
, char *replacement
)
3945 if (!tPtr
->flags
.ownsSelection
)
3948 removeSelection(tPtr
);
3951 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
3952 updateCursorPosition(tPtr
);