2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
6 #include <X11/keysym.h>
12 * - verify what happens with XK_return in insertTextInt...
13 * - selection code... selects can be funny if it crosses over. use rect?
14 * - also inspect behaviour for WACenter and WARight
15 * - what if a widget grabs the click... howto say: "pressed me"?
16 * note that WMCreateEventHandler takes one data, but need widget & tPtr
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - Tabs now are simply replaced by 4 spaces...
20 * - redo blink code to reduce paint event... use pixmap buffer...
21 * - add paragraph support (full) and '\n' code in getStream..
24 /* a Section is a section of a TextBlock that describes what parts
25 of a TextBlock has been laid out on which "line"...
26 o this greatly aids redraw, scroll and selection.
27 o this is created during layoutLine, but may be later modified.
28 o there may be many Sections per TextBlock, hence the array */
30 unsigned int x
, y
; /* where to draw it from */
31 unsigned short w
, h
; /* its width and height */
32 unsigned short begin
; /* where the layout begins */
33 unsigned short end
; /* where it ends */
34 unsigned short max_d
; /* a quick hack for layOut if(laidOut) */
35 unsigned short last
:1; /* is it the last section on a "line"? */
36 unsigned int _y
:31; /* the "line" it and other textblocks are on */
39 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
40 o text for the block, color and font
41 o or a pointer to the pixmap
42 o OR a pointer to the widget and the (text) description for its graphic
45 typedef struct _TextBlock
{
46 struct _TextBlock
*next
; /* next text block in linked list */
47 struct _TextBlock
*prior
; /* prior text block in linked list */
49 char *text
; /* pointer to text (could be kanji) */
50 /* or to the object's description */
52 WMFont
*font
; /* the font */
53 WMWidget
*widget
; /* the embedded widget */
54 WMPixmap
*pixmap
; /* the pixmap */
55 } d
; /* description */
57 unsigned short used
; /* number of chars in this block */
58 unsigned short allocated
; /* size of allocation (in chars) */
59 WMColor
*color
; /* the color */
61 Section
*sections
; /* the region for layouts (a growable array) */
62 /* an _array_! of size _nsections_ */
64 unsigned short s_begin
; /* where the selection begins */
65 unsigned short s_end
; /* where it ends */
67 unsigned int first
:1; /* first TextBlock in paragraph */
68 unsigned int blank
:1; /* ie. blank paragraph */
69 unsigned int kanji
:1; /* is of 16-bit characters or not */
70 unsigned int graphic
:1; /* graphic or text: text=0 */
71 unsigned int object
:1; /* embedded object or pixmap */
72 unsigned int underlined
:1; /* underlined or not */
73 unsigned int selected
:1; /* selected or not */
74 unsigned int nsections
:8; /* over how many "lines" a TextBlock wraps */
75 int script
:8; /* script in points: negative for subscript */
76 unsigned int marginN
:8; /* which of the margins in the tPtr to use */
77 unsigned int nClicks
:2; /* single, double, triple clicks */
78 unsigned int RESERVED
:7;
81 /* I'm lazy: visible.h vs. visible.size.height :-) */
86 typedef struct W_Text
{
87 W_Class widgetClass
; /* the class number of this widget */
88 W_View
*view
; /* the view referring to this instance */
90 WMRuler
*ruler
; /* the ruler widget to manipulate paragraphs */
92 WMScroller
*vS
; /* the vertical scroller */
93 unsigned int vpos
; /* the current vertical position */
94 unsigned int prevVpos
; /* the previous vertical position */
96 WMScroller
*hS
; /* the horizontal scroller */
97 unsigned int hpos
; /* the current horizontal position */
98 unsigned int prevHpos
; /* the previous horizontal position */
100 WMFont
*dFont
; /* the default font */
101 WMColor
*dColor
; /* the default color */
102 WMPixmap
*dBulletPix
; /* the default pixmap for bullets */
104 WMColor
*fgColor
; /* The current foreground color */
105 WMColor
*bgColor
; /* The background color */
107 GC stippledGC
; /* the GC to overlay selected graphics with */
108 Pixmap db
; /* the buffer on which to draw */
109 WMPixmap
*bgPixmap
; /* the background pixmap */
111 myRect visible
; /* the actual rectangle that can be drawn into */
112 myRect cursor
; /* the position and (height) of cursor */
113 myRect sel
; /* the selection rectangle */
115 WMPoint clicked
; /* where in the _document_ was clicked */
117 unsigned short tpos
; /* the position in the currentTextBlock */
118 unsigned short docWidth
; /* the width of the entire document */
119 unsigned int docHeight
; /* the height of the entire document */
121 TextBlock
*firstTextBlock
;
122 TextBlock
*lastTextBlock
;
123 TextBlock
*currentTextBlock
;
125 WMArray
*gfxItems
; /* a nice array of graphic items */
128 WMHandlerID timerID
; /* for nice twinky-winky */
133 WMTextDelegate
*delegate
;
136 WMRulerMargins
*margins
; /* an array of margins */
138 unsigned int nMargins
:7; /* the total number of margins in use */
140 unsigned int monoFont
:1; /* whether to ignore formats and graphic */
141 unsigned int focused
:1; /* whether this instance has input focus */
142 unsigned int editable
:1; /* "silly user, you can't edit me" */
143 unsigned int ownsSelection
:1; /* "I ownz the current selection!" */
144 unsigned int pointerGrabbed
:1; /* "heh, gib me pointer" */
145 unsigned int extendSelection
:1; /* shift-drag to select more regions */
147 unsigned int rulerShown
:1; /* whether the ruler is shown or not */
148 unsigned int frozen
:1; /* whether screen updates are to be made */
149 unsigned int cursorShown
:1; /* whether to show the cursor */
150 unsigned int acceptsGraphic
:1; /* accept graphic when dropped */
151 unsigned int horizOnDemand
:1; /* if a large image should appear */
152 unsigned int needsLayOut
:1; /* in case of Append/Deletes */
153 unsigned int ignoreNewLine
:1; /* turn it into a ' ' in streams > 1 */
154 unsigned int indentNewLine
:1; /* add " " for a newline typed */
155 unsigned int laidOut
:1; /* have the TextBlocks all been laid out */
156 unsigned int waitingForSelection
:1; /* I don't wanna wait in vain... */
157 unsigned int prepend
:1; /* prepend=1, append=0 (for parsers) */
158 WMAlignment alignment
:2; /* the alignment for text */
159 WMReliefType relief
:3; /* the relief to display with */
160 unsigned int isOverGraphic
:2; /* the mouse is over a graphic */
161 unsigned int first
:1; /* for plain text parsing, newline? */
162 /* unsigned int RESERVED:1; */
165 WMArray
*xdndSourceTypes
;
166 WMArray
*xdndDestinationTypes
;
169 #define NOTIFY(T,C,N,A) {\
170 WMNotification *notif = WMCreateNotification(N,T,A);\
171 if ((T)->delegate && (T)->delegate->C)\
172 (*(T)->delegate->C)((T)->delegate,notif);\
173 WMPostNotification(notif);\
174 WMReleaseNotification(notif);}
179 /* just to print blocks of text not terminated by \0 */
180 static void output(char *ptr
, int len
)
184 s
= wmalloc(len
+ 1);
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
198 #define STIPPLE_WIDTH 8
199 #define STIPPLE_HEIGHT 8
200 static char STIPPLE_BITS
[] = {
201 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
204 static char *default_bullet
[] = {
218 static void handleEvents(XEvent
* event
, void *data
);
219 static void layOutDocument(Text
* tPtr
);
220 static void updateScrollers(Text
* tPtr
);
222 static int getMarginNumber(Text
* tPtr
, WMRulerMargins
* margins
)
226 for (i
= 0; i
< tPtr
->nMargins
; i
++) {
228 if (WMIsMarginEqualToMargin(&tPtr
->margins
[i
], margins
))
235 static int newMargin(Text
* tPtr
, WMRulerMargins
* margins
)
240 tPtr
->margins
[0].retainCount
++;
244 n
= getMarginNumber(tPtr
, margins
);
248 if (tPtr
->nMargins
>= 127) {
249 n
= tPtr
->nMargins
- 1;
253 tPtr
->margins
= wrealloc(tPtr
->margins
, (++tPtr
->nMargins
) * sizeof(WMRulerMargins
));
255 n
= tPtr
->nMargins
- 1;
256 tPtr
->margins
[n
].left
= margins
->left
;
257 tPtr
->margins
[n
].first
= margins
->first
;
258 tPtr
->margins
[n
].body
= margins
->body
;
259 tPtr
->margins
[n
].right
= margins
->right
;
260 /* for each tab... */
261 tPtr
->margins
[n
].retainCount
= 1;
263 tPtr
->margins
[n
].retainCount
++;
269 static Bool
sectionWasSelected(Text
* tPtr
, TextBlock
* tb
, XRectangle
* rect
, int s
)
271 unsigned short i
, w
, lw
, selected
= False
, extend
= False
;
274 /* if selection rectangle completely encloses the section */
275 if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
276 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
<= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)) {
278 sel
.w
= tPtr
->visible
.w
;
279 selected
= extend
= True
;
281 /* or if it starts on a line and then goes further down */
282 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
283 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
<= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
284 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)) {
285 sel
.x
= WMAX(tPtr
->sel
.x
, tPtr
->clicked
.x
);
286 sel
.w
= tPtr
->visible
.w
;
287 selected
= extend
= True
;
289 /* or if it begins before a line, but ends on it */
290 } else if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
291 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
>= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
292 && (tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)) {
294 if (1 || tPtr
->sel
.x
+ tPtr
->sel
.w
> tPtr
->clicked
.x
)
295 sel
.w
= tPtr
->sel
.x
+ tPtr
->sel
.w
;
302 /* or if the selection rectangle lies entirely within a line */
303 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
304 && (tPtr
->sel
.w
>= 2)
305 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
>= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)) {
314 /* if not within (modified) selection rectangle */
315 if (tb
->sections
[s
].x
> sel
.x
+ sel
.w
|| tb
->sections
[s
].x
+ tb
->sections
[s
].w
< sel
.x
)
319 if (tb
->sections
[s
].x
+ tb
->sections
[s
].w
<= sel
.x
+ sel
.w
&& tb
->sections
[s
].x
>= sel
.x
) {
320 rect
->width
= tb
->sections
[s
].w
;
321 rect
->x
= tb
->sections
[s
].x
;
326 i
= tb
->sections
[s
].begin
;
329 if (0 && tb
->sections
[s
].x
>= sel
.x
) {
330 tb
->s_begin
= tb
->sections
[s
].begin
;
334 while (++i
<= tb
->sections
[s
].end
) {
336 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
- 1]), 1);
339 if (lw
+ tb
->sections
[s
].x
>= sel
.x
|| i
== tb
->sections
[s
].end
) {
342 tb
->s_begin
= (tb
->selected
? WMIN(tb
->s_begin
, i
) : i
);
347 if (i
> tb
->sections
[s
].end
) {
348 printf("WasSelected: (i > tb->sections[s].end) \n");
352 _selEnd
: rect
->x
= tb
->sections
[s
].x
+ lw
;
354 while (++i
<= tb
->sections
[s
].end
) {
356 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
- 1]), 1);
359 if (lw
+ rect
->x
>= sel
.x
+ sel
.w
|| i
== tb
->sections
[s
].end
) {
361 if (i
!= tb
->sections
[s
].end
) {
367 if (tb
->sections
[s
].last
&& sel
.x
+ sel
.w
368 >= tb
->sections
[s
].x
+ tb
->sections
[s
].w
&& extend
) {
369 rect
->width
+= (tPtr
->visible
.w
- rect
->x
- lw
);
372 tb
->s_end
= (tb
->selected
? WMAX(tb
->s_end
, i
) : i
);
381 rect
->y
= tb
->sections
[s
]._y
- tPtr
->vpos
;
382 rect
->height
= tb
->sections
[s
].h
;
384 printf("DEBUG: graphic s%d h%d\n", s
, tb
->sections
[s
].h
);
391 static void setSelectionProperty(WMText
* tPtr
, WMFont
* font
, WMColor
* color
, int underlined
)
396 tb
= tPtr
->firstTextBlock
;
397 if (!tb
|| !tPtr
->flags
.ownsSelection
)
400 if (font
&& (!color
|| underlined
== -1))
404 if (tPtr
->flags
.monoFont
|| tb
->selected
) {
406 if (tPtr
->flags
.monoFont
|| (tb
->s_end
- tb
->s_begin
== tb
->used
)
411 WMReleaseFont(tb
->d
.font
);
412 tb
->d
.font
= WMRetainFont(font
);
414 } else if (underlined
!= -1) {
415 tb
->underlined
= underlined
;
417 WMReleaseColor(tb
->color
);
418 tb
->color
= WMRetainColor(color
);
421 } else if (tb
->s_end
<= tb
->used
&& tb
->s_begin
< tb
->s_end
) {
423 TextBlock
*midtb
, *otb
= tb
;
425 if (underlined
!= -1) {
426 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
427 &(tb
->text
[tb
->s_begin
]),
428 tb
->d
.font
, tb
->color
,
430 (tb
->s_end
- tb
->s_begin
));
432 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
433 &(tb
->text
[tb
->s_begin
]),
434 (isFont
? font
: tb
->d
.
437 color
: color
), False
,
438 (tb
->s_end
- tb
->s_begin
));
442 if (underlined
!= -1) {
443 midtb
->underlined
= underlined
;
445 midtb
->underlined
= otb
->underlined
;
448 midtb
->selected
= !True
;
450 midtb
->s_end
= midtb
->used
;
451 tPtr
->currentTextBlock
= tb
;
452 WMAppendTextBlock(tPtr
, midtb
);
453 tb
= tPtr
->currentTextBlock
;
456 if (otb
->used
- otb
->s_end
> 0) {
459 WMCreateTextBlockWithText(tPtr
,
460 &(otb
->text
[otb
->s_end
]), otb
->d
.font
,
461 otb
->color
, False
, otb
->used
- otb
->s_end
);
464 ntb
->underlined
= otb
->underlined
;
465 ntb
->selected
= False
;
466 WMAppendTextBlock(tPtr
, ntb
);
467 tb
= tPtr
->currentTextBlock
;
472 tPtr
->currentTextBlock
= midtb
;
475 otb
->selected
= False
;
476 otb
->used
= otb
->s_begin
;
483 tPtr
->flags
.needsLayOut
= True
;
486 /* in case the size changed... */
487 if (isFont
&& tPtr
->currentTextBlock
) {
488 TextBlock
*tb
= tPtr
->currentTextBlock
;
490 printf("%d %d %d\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
491 tPtr
->sel
.y
= 3 + tb
->sections
[0]._y
;
492 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
- 1]._y
- tb
->sections
[0]._y
;
493 tPtr
->sel
.w
= tb
->sections
[tb
->nsections
- 1].w
;
494 if (tb
->sections
[tb
->nsections
- 1]._y
!= tb
->sections
[0]._y
) {
497 printf("%d %d %d\n\n\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
502 static Bool
removeSelection(Text
* tPtr
)
504 TextBlock
*tb
= NULL
;
507 if (!(tb
= tPtr
->firstTextBlock
))
512 if (!first
&& !tb
->graphic
) {
513 WMReleaseFont(tPtr
->dFont
);
514 tPtr
->dFont
= WMRetainFont(tb
->d
.font
);
518 if ((tb
->s_end
- tb
->s_begin
== tb
->used
) || tb
->graphic
) {
519 tPtr
->currentTextBlock
= tb
;
522 } else if (tb
->prior
) {
523 if (tb
->prior
->graphic
)
526 tPtr
->tpos
= tb
->prior
->used
;
530 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
531 tb
= tPtr
->currentTextBlock
;
534 } else if (tb
->s_end
<= tb
->used
) {
535 memmove(&(tb
->text
[tb
->s_begin
]), &(tb
->text
[tb
->s_end
]), tb
->used
- tb
->s_end
);
536 tb
->used
-= (tb
->s_end
- tb
->s_begin
);
537 tb
->selected
= False
;
538 tPtr
->tpos
= tb
->s_begin
;
548 static TextBlock
*getFirstNonGraphicBlockFor(TextBlock
* tb
, short dir
)
550 TextBlock
*hold
= tb
;
558 tb
= (dir
? tb
->next
: tb
->prior
);
566 tb
= (dir
? tb
->prior
: tb
->next
);
576 static Bool
updateStartForCurrentTextBlock(Text
* tPtr
, int x
, int y
, int *dir
, TextBlock
* tb
)
578 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
579 tb
= getFirstNonGraphicBlockFor(tb
, *dir
);
584 tPtr
->currentTextBlock
= (dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
591 layOutDocument(tPtr
);
595 *dir
= !(y
<= tb
->sections
[0].y
);
597 if ((y
<= tb
->sections
[0]._y
+ tb
->sections
[0].h
)
598 && (y
>= tb
->sections
[0]._y
)) {
599 /* if it's on the same line */
600 if (x
< tb
->sections
[0].x
)
604 if ((y
<= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
605 && (y
>= tb
->sections
[tb
->nsections
- 1]._y
)) {
606 /* if it's on the same line */
607 if (x
> tb
->sections
[tb
->nsections
- 1].x
)
615 static void paintText(Text
* tPtr
)
620 int len
, y
, c
, s
, done
= False
, dir
/* 1 = down */ ;
621 WMScreen
*scr
= tPtr
->view
->screen
;
622 Display
*dpy
= tPtr
->view
->screen
->display
;
623 Window win
= tPtr
->view
->window
;
626 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
629 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(tPtr
->bgColor
), 0, 0, tPtr
->visible
.w
, tPtr
->visible
.h
);
631 if (tPtr
->bgPixmap
) {
632 WMDrawPixmap(tPtr
->bgPixmap
, tPtr
->db
,
633 (tPtr
->visible
.w
- tPtr
->visible
.x
- tPtr
->bgPixmap
->width
) / 2,
634 (tPtr
->visible
.h
- tPtr
->visible
.y
- tPtr
->bgPixmap
->height
) / 2);
637 if (!(tb
= tPtr
->currentTextBlock
)) {
638 if (!(tb
= tPtr
->firstTextBlock
)) {
645 /* first, which direction? Don't waste time looking all over,
646 since the parts to be drawn will most likely be near what
647 was previously drawn */
648 if (!updateStartForCurrentTextBlock(tPtr
, 0, tPtr
->vpos
, &dir
, tb
))
653 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
657 if (tPtr
->vpos
<= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
660 if (tPtr
->vpos
>= tb
->sections
[tb
->nsections
- 1]._y
+ tb
->sections
[tb
->nsections
- 1].h
)
678 /* first, place all text that can be viewed */
679 while (!done
&& tb
) {
685 tb
->selected
= False
;
687 for (s
= 0; s
< tb
->nsections
&& !done
; s
++) {
689 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
694 if (tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
697 if (tPtr
->flags
.monoFont
) {
699 color
= tPtr
->fgColor
;
705 if (tPtr
->flags
.ownsSelection
) {
708 if (sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
710 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(scr
->gray
),
711 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
715 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
716 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
717 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
718 WMDrawString(scr
, tPtr
->db
, color
, font
, tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
720 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
721 XDrawLine(dpy
, tPtr
->db
, WMColorGC(color
),
722 tb
->sections
[s
].x
- tPtr
->hpos
,
724 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
, y
+ font
->y
+ 1);
727 tb
= (!done
? tb
->next
: NULL
);
730 /* now , show all graphic items that can be viewed */
731 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
732 if (c
> 0 && !tPtr
->flags
.monoFont
) {
735 for (j
= 0; j
< c
; j
++) {
736 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
738 /* if it's not viewable, and mapped, unmap it */
739 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
740 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
743 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
744 WMUnmapWidget(tb
->d
.widget
);
748 /* if it's viewable, and not mapped, map it */
750 W_View
*view
= W_VIEW(tb
->d
.widget
);
752 if (!view
->flags
.realized
)
753 WMRealizeWidget(tb
->d
.widget
);
754 if (!view
->flags
.mapped
) {
755 XMapWindow(view
->screen
->display
, view
->window
);
756 XFlush(view
->screen
->display
);
757 view
->flags
.mapped
= 1;
762 WMMoveWidget(tb
->d
.widget
,
763 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
764 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
765 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
768 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
769 tb
->sections
[0].x
- tPtr
->hpos
,
770 tb
->sections
[0].y
- tPtr
->vpos
);
771 h
= tb
->d
.pixmap
->height
+ 1;
775 if (tPtr
->flags
.ownsSelection
) {
778 if (sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
779 Drawable d
= (0 && tb
->object
?
780 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
783 XFillRectangle(dpy
, d
, tPtr
->stippledGC
,
784 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC, */
785 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
789 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
790 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
791 tb
->sections
[0].x
- tPtr
->hpos
,
792 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
793 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
794 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
801 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
&& tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
802 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
803 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tPtr
->fgColor
),
804 tPtr
->cursor
.x
, y
, tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
807 XCopyArea(dpy
, tPtr
->db
, win
, WMColorGC(tPtr
->bgColor
), 0, 0,
808 tPtr
->visible
.w
, tPtr
->visible
.h
, tPtr
->visible
.x
, tPtr
->visible
.y
);
810 W_DrawRelief(scr
, win
, 0, 0, tPtr
->view
->size
.width
, tPtr
->view
->size
.height
, tPtr
->flags
.relief
);
812 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
813 XDrawLine(dpy
, win
, WMColorGC(tPtr
->fgColor
), 2, 42, tPtr
->view
->size
.width
- 4, 42);
817 static void mouseOverObject(Text
* tPtr
, int x
, int y
)
822 x
-= tPtr
->visible
.x
;
824 y
-= tPtr
->visible
.y
;
827 if (tPtr
->flags
.ownsSelection
) {
829 && tPtr
->sel
.y
<= y
&& tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
&& tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
830 tPtr
->flags
.isOverGraphic
= 1;
836 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
839 tPtr
->flags
.isOverGraphic
= 0;
841 for (j
= 0; j
< c
; j
++) {
842 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
844 if (!tb
|| !tb
->sections
) {
845 tPtr
->flags
.isOverGraphic
= 0;
850 if (tb
->sections
[0].x
<= x
851 && tb
->sections
[0].y
<= y
852 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
853 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
854 tPtr
->flags
.isOverGraphic
= 3;
864 tPtr
->flags
.isOverGraphic
= 0;
866 tPtr
->view
->attribs
.cursor
= (result
? tPtr
->view
->screen
->defaultCursor
: tPtr
->view
->screen
->textCursor
);
868 XSetWindowAttributes attribs
;
869 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
870 XChangeWindowAttributes(tPtr
->view
->screen
->display
, tPtr
->view
->window
, CWCursor
, &attribs
);
876 static void blinkCursor(void *data
)
878 Text
*tPtr
= (Text
*) data
;
880 if (tPtr
->flags
.cursorShown
) {
881 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
, blinkCursor
, data
);
883 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
, blinkCursor
, data
);
886 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
890 static void updateCursorPosition(Text
* tPtr
)
892 TextBlock
*tb
= NULL
;
895 if (tPtr
->flags
.needsLayOut
)
896 layOutDocument(tPtr
);
898 if (!(tb
= tPtr
->currentTextBlock
)) {
899 if (!(tb
= tPtr
->firstTextBlock
)) {
900 WMFont
*font
= tPtr
->dFont
;
902 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
912 y
= tb
->sections
[0].y
;
913 h
= tb
->sections
[0].h
;
914 x
= tb
->sections
[0].x
;
916 } else if (tb
->graphic
) {
917 y
= tb
->sections
[0].y
;
918 h
= tb
->sections
[0].h
;
919 x
= tb
->sections
[0].x
;
921 x
+= tb
->sections
[0].w
;
924 if (tPtr
->tpos
> tb
->used
)
925 tPtr
->tpos
= tb
->used
;
927 for (s
= 0; s
< tb
->nsections
- 1; s
++) {
929 if (tPtr
->tpos
>= tb
->sections
[s
].begin
&& tPtr
->tpos
<= tb
->sections
[s
].end
)
933 y
= tb
->sections
[s
]._y
;
934 h
= tb
->sections
[s
].h
;
935 x
= tb
->sections
[s
].x
+ WMWidthOfString((tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
),
936 &tb
->text
[tb
->sections
[s
].begin
],
937 tPtr
->tpos
- tb
->sections
[s
].begin
);
944 /* scroll the bars if the cursor is not visible */
945 if (tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
946 if (tPtr
->cursor
.y
+ tPtr
->cursor
.h
> tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
) {
948 (tPtr
->cursor
.y
+ tPtr
->cursor
.h
+ 10
949 - (tPtr
->vpos
+ tPtr
->visible
.y
+ tPtr
->visible
.h
));
950 } else if (tPtr
->cursor
.y
< tPtr
->vpos
+ tPtr
->visible
.y
) {
951 tPtr
->vpos
-= (tPtr
->vpos
+ tPtr
->visible
.y
- tPtr
->cursor
.y
);
956 updateScrollers(tPtr
);
959 static void cursorToTextPosition(Text
* tPtr
, int x
, int y
)
961 TextBlock
*tb
= NULL
;
962 int done
= False
, s
, pos
, len
, _w
, _y
, dir
= 1; /* 1 == "down" */
965 if (tPtr
->flags
.needsLayOut
)
966 layOutDocument(tPtr
);
968 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
972 x
-= (tPtr
->visible
.x
- 2);
976 /* clicked is relative to document, not window... */
980 if (!(tb
= tPtr
->currentTextBlock
)) {
981 if (!(tb
= tPtr
->firstTextBlock
)) {
982 WMFont
*font
= tPtr
->dFont
;
984 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
- font
->y
);
991 /* first, which direction? Most likely, newly clicked
992 position will be close to previous */
993 if (!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
996 s
= (dir
? 0 : tb
->nsections
- 1);
997 if (y
>= tb
->sections
[s
]._y
&& y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1001 /* get the first (or last) section of the TextBlock that
1002 lies about the vertical click point */
1004 while (!done
&& tb
) {
1006 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1007 if ((dir
? tb
->next
: tb
->prior
))
1008 tb
= (dir
? tb
->next
: tb
->prior
);
1012 s
= (dir
? 0 : tb
->nsections
- 1);
1013 while (!done
&& (dir
? (s
< tb
->nsections
) : (s
>= 0))) {
1015 if ((dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) : (y
>= tb
->sections
[s
]._y
))) {
1023 if ((dir
? tb
->next
: tb
->prior
)) {
1024 tb
= (dir
? tb
->next
: tb
->prior
);
1027 break; /* goto _doneH; */
1032 if (s
< 0 || s
>= tb
->nsections
) {
1033 s
= (dir
? tb
->nsections
- 1 : 0);
1037 /* we have the line, which TextBlock on that line is it? */
1038 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1039 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1040 TextBlock
*hold
= tb
;
1041 tb
= getFirstNonGraphicBlockFor(hold
, dir
);
1054 _y
= tb
->sections
[s
]._y
;
1058 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1059 tb
= (dir
? tb
->next
: tb
->prior
);
1066 _w
= WMWidgetWidth(tb
->d
.widget
) - 5;
1068 _w
= tb
->d
.pixmap
->width
- 5;
1070 if (tb
->sections
[0].x
+ _w
>= x
)
1073 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1074 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1075 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1076 if (tb
->sections
[s
].x
+ _w
>= x
)
1081 if (tb
->sections
[s
].x
<= x
)
1085 if ((dir
? tb
->next
: tb
->prior
)) {
1086 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1087 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1088 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1090 pos
= (dir
? 0 : tb
->sections
[s
].begin
);
1091 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1096 if (_y
!= nxt
->sections
[dir
? 0 : nxt
->nsections
- 1]._y
) {
1097 /* this must be the last/first on this line. stop */
1098 pos
= (dir
? tb
->sections
[s
].end
: 0);
1099 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1103 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1105 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1106 } else if (pos
> tb
->sections
[s
].begin
) {
1108 WMWidthOfString(tb
->d
.font
,
1109 &(tb
->text
[tb
->sections
[s
].begin
]),
1110 pos
- tb
->sections
[s
].begin
);
1117 if ((dir
? tb
->next
: tb
->prior
)) {
1118 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)
1260 case WSIncrementLine
:{
1261 int limit
= tPtr
->docHeight
- height
;
1262 if (tPtr
->vpos
< limit
) {
1263 if (tPtr
->vpos
< limit
- 16)
1272 case WSDecrementPage
:
1273 if (((int)tPtr
->vpos
- (int)height
) >= 0)
1274 tPtr
->vpos
-= height
;
1281 case WSIncrementPage
:
1282 tPtr
->vpos
+= height
;
1283 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1284 tPtr
->vpos
= tPtr
->docHeight
- height
;
1289 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1290 * (float)(tPtr
->docHeight
- height
);
1298 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1299 tPtr
->prevVpos
= tPtr
->vpos
;
1302 if (w
== tPtr
->hS
) {
1303 int width
= tPtr
->visible
.w
;
1305 which
= WMGetScrollerHitPart(tPtr
->hS
);
1308 case WSDecrementLine
:
1309 if (tPtr
->hpos
> 0) {
1310 if (tPtr
->hpos
> 16)
1318 case WSIncrementLine
:{
1319 int limit
= tPtr
->docWidth
- width
;
1320 if (tPtr
->hpos
< limit
) {
1321 if (tPtr
->hpos
< limit
- 16)
1330 case WSDecrementPage
:
1331 if (((int)tPtr
->hpos
- (int)width
) >= 0)
1332 tPtr
->hpos
-= width
;
1339 case WSIncrementPage
:
1340 tPtr
->hpos
+= width
;
1341 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1342 tPtr
->hpos
= tPtr
->docWidth
- width
;
1347 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1348 * (float)(tPtr
->docWidth
- width
);
1356 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1357 tPtr
->prevHpos
= tPtr
->hpos
;
1361 updateScrollers(tPtr
);
1368 unsigned short begin
, end
; /* what part of the text block */
1371 static int layOutLine(Text
* tPtr
, myLineItems
* items
, int nitems
, int x
, int y
)
1373 int i
, j
= 0, lw
= 0, line_height
= 0, max_d
= 0, len
, n
;
1376 TextBlock
*tb
, *tbsame
= NULL
;
1378 if (!items
|| nitems
== 0)
1381 for (i
= 0; i
< nitems
; i
++) {
1385 if (!tPtr
->flags
.monoFont
) {
1387 WMWidget
*wdt
= tb
->d
.widget
;
1388 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1389 if (tPtr
->flags
.alignment
!= WALeft
)
1390 lw
+= WMWidgetWidth(wdt
);
1392 line_height
= WMAX(line_height
, tb
->d
.pixmap
->height
+ max_d
);
1393 if (tPtr
->flags
.alignment
!= WALeft
)
1394 lw
+= tb
->d
.pixmap
->width
;
1399 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1400 /*max_d = WMAX(max_d, abs(font->height-font->y)); */
1402 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1403 text
= &(tb
->text
[items
[i
].begin
]);
1404 len
= items
[i
].end
- items
[i
].begin
;
1405 if (tPtr
->flags
.alignment
!= WALeft
)
1406 lw
+= WMWidthOfString(font
, text
, len
);
1410 if (tPtr
->flags
.alignment
== WARight
) {
1411 j
= tPtr
->visible
.w
- lw
;
1412 } else if (tPtr
->flags
.alignment
== WACenter
) {
1413 j
= (int)((float)(tPtr
->visible
.w
- lw
)) / 2.0;
1416 for (i
= 0; i
< nitems
; i
++) {
1419 if (tbsame
== tb
) { /* extend it, since it's on same line */
1420 tb
->sections
[tb
->nsections
- 1].end
= items
[i
].end
;
1421 n
= tb
->nsections
- 1;
1423 tb
->sections
= wrealloc(tb
->sections
, (++tb
->nsections
) * sizeof(Section
));
1424 n
= tb
->nsections
- 1;
1425 tb
->sections
[n
]._y
= y
+ max_d
;
1426 tb
->sections
[n
].max_d
= max_d
;
1427 tb
->sections
[n
].x
= x
+ j
;
1428 tb
->sections
[n
].h
= line_height
;
1429 tb
->sections
[n
].begin
= items
[i
].begin
;
1430 tb
->sections
[n
].end
= items
[i
].end
;
1433 tb
->sections
[n
].last
= (i
+ 1 == nitems
);
1436 if (!tPtr
->flags
.monoFont
) {
1438 WMWidget
*wdt
= tb
->d
.widget
;
1439 tb
->sections
[n
].y
= max_d
+ y
+ line_height
- WMWidgetHeight(wdt
);
1440 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1442 tb
->sections
[n
].y
= y
+ line_height
+ max_d
- tb
->d
.pixmap
->height
;
1443 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1445 x
+= tb
->sections
[n
].w
;
1448 font
= (tPtr
->flags
.monoFont
) ? tPtr
->dFont
: tb
->d
.font
;
1449 len
= items
[i
].end
- items
[i
].begin
;
1450 text
= &(tb
->text
[items
[i
].begin
]);
1452 tb
->sections
[n
].y
= y
+ line_height
- font
->y
;
1454 WMWidthOfString(font
,
1455 &(tb
->text
[tb
->sections
[n
].begin
]),
1456 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1458 x
+= WMWidthOfString(font
, text
, len
);
1468 static void layOutDocument(Text
* tPtr
)
1471 myLineItems
*items
= NULL
;
1472 unsigned int itemsSize
= 0, nitems
= 0, begin
, end
;
1474 unsigned int x
, y
= 0, lw
= 0, width
= 0, bmargin
;
1475 char *start
= NULL
, *mark
= NULL
;
1477 if (tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)))
1480 assert(tPtr
->visible
.w
> 20);
1482 tPtr
->docWidth
= tPtr
->visible
.w
;
1483 x
= tPtr
->margins
[tb
->marginN
].first
;
1484 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1486 /* only partial layOut needed: re-Lay only affected textblocks */
1487 if (tPtr
->flags
.laidOut
) {
1488 tb
= tPtr
->currentTextBlock
;
1490 /* search backwards for textblocks on same line */
1492 if (!tb
->sections
|| tb
->nsections
< 1) {
1493 tb
= tPtr
->firstTextBlock
;
1494 tPtr
->flags
.laidOut
= False
;
1499 if (!tb
->prior
->sections
|| tb
->prior
->nsections
< 1) {
1500 tb
= tPtr
->firstTextBlock
;
1501 tPtr
->flags
.laidOut
= False
;
1506 if (tb
->sections
[0]._y
!= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
) {
1512 if (tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
> 0) {
1513 y
= tb
->prior
->sections
[tb
->prior
->nsections
- 1]._y
+
1514 tb
->prior
->sections
[tb
->prior
->nsections
- 1].h
-
1515 tb
->prior
->sections
[tb
->prior
->nsections
- 1].max_d
;
1524 if (tb
->sections
&& tb
->nsections
> 0) {
1525 wfree(tb
->sections
);
1526 tb
->sections
= NULL
;
1530 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1531 TextBlock
*next
= tb
->next
;
1532 tPtr
->currentTextBlock
= tb
;
1533 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1539 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1540 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1541 x
= tPtr
->margins
[tb
->marginN
].first
;
1542 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1548 if (!tPtr
->flags
.monoFont
) {
1550 width
= WMWidgetWidth(tb
->d
.widget
);
1552 width
= tb
->d
.pixmap
->width
;
1554 if (width
> tPtr
->docWidth
)
1555 tPtr
->docWidth
= width
;
1558 if (lw
>= tPtr
->visible
.w
- x
) {
1559 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1565 if (nitems
+ 1 > itemsSize
) {
1566 items
= wrealloc(items
, (++itemsSize
) * sizeof(myLineItems
));
1569 items
[nitems
].tb
= tb
;
1570 items
[nitems
].begin
= 0;
1571 items
[nitems
].end
= 0;
1575 } else if ((start
= tb
->text
)) {
1577 font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
1580 mark
= strchr(start
, ' ');
1582 end
+= (int)(mark
- start
) + 1;
1585 end
+= strlen(start
);
1592 if (end
- begin
> 0) {
1594 width
= WMWidthOfString(font
, &tb
->text
[begin
], end
- begin
);
1596 /* if it won't fit, char wrap it */
1597 if (width
>= tPtr
->visible
.w
) {
1598 char *t
= &tb
->text
[begin
];
1599 int l
= end
- begin
, i
= 0;
1601 width
= WMWidthOfString(font
, t
, ++i
);
1602 } while (width
< tPtr
->visible
.w
&& i
< l
);
1606 start
= &tb
->text
[end
];
1612 if (lw
>= tPtr
->visible
.w
- x
) {
1613 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1619 if (nitems
+ 1 > itemsSize
) {
1620 items
= wrealloc(items
, (++itemsSize
) * sizeof(myLineItems
));
1623 items
[nitems
].tb
= tb
;
1624 items
[nitems
].begin
= begin
;
1625 items
[nitems
].end
= end
;
1632 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1633 if (0 && tPtr
->flags
.laidOut
1634 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
> 0
1635 && (tPtr
->vpos
+ tPtr
->visible
.h
< tb
->next
->sections
[0]._y
)) {
1636 if (tPtr
->lastTextBlock
->sections
&& tPtr
->lastTextBlock
->nsections
> 0) {
1637 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1638 int ly
= ltb
->sections
[ltb
->nsections
- 1]._y
;
1639 int lh
= ltb
->sections
[ltb
->nsections
- 1].h
;
1642 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
- 1].max_d
;
1643 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
- 1].max_d
);
1645 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1647 sd
= tPtr
->docHeight
- y
;
1649 printf("dif %d-%d: %d\n", ss
, sd
, ss
- sd
);
1650 y
+= tb
->next
->sections
[0]._y
- y
;
1652 printf("nitems%d\n", nitems
);
1654 y
= tPtr
->docHeight
+ ss
- sd
;
1658 tPtr
->flags
.laidOut
= False
;
1666 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1668 if (tPtr
->docHeight
!= y
+ 10) {
1669 tPtr
->docHeight
= y
+ 10;
1670 updateScrollers(tPtr
);
1673 if (tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1676 tPtr
->flags
.horizOnDemand
= True
;
1677 WMSetTextHasHorizontalScroller((WMText
*) tPtr
, True
);
1678 event
.type
= Expose
;
1679 handleEvents(&event
, (void *)tPtr
);
1681 } else if (tPtr
->docWidth
<= tPtr
->visible
.w
&& tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1682 tPtr
->flags
.horizOnDemand
= False
;
1683 WMSetTextHasHorizontalScroller((WMText
*) tPtr
, False
);
1686 tPtr
->flags
.laidOut
= True
;
1688 if (items
&& itemsSize
> 0)
1693 static void textDidResize(W_ViewDelegate
* self
, WMView
* view
)
1695 Text
*tPtr
= (Text
*) view
->self
;
1696 unsigned short w
= tPtr
->view
->size
.width
;
1697 unsigned short h
= tPtr
->view
->size
.height
;
1698 unsigned short rh
= 0, vw
= 0, rel
;
1700 rel
= (tPtr
->flags
.relief
== WRFlat
);
1702 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1703 WMMoveWidget(tPtr
->ruler
, 2, 2);
1704 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1709 WMMoveWidget(tPtr
->vS
, 1 - (rel
? 1 : 0), rh
+ 1 - (rel
? 1 : 0));
1710 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
? 2 : 0));
1712 WMSetRulerOffset(tPtr
->ruler
, 22);
1714 WMSetRulerOffset(tPtr
->ruler
, 2);
1718 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1719 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1721 WMMoveWidget(tPtr
->hS
, vw
+ 1, h
- 21);
1722 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1726 tPtr
->visible
.x
= (tPtr
->vS
) ? 24 : 4;
1727 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) ? 43 : 3;
1728 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1729 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1730 tPtr
->visible
.h
-= (tPtr
->hS
) ? 20 : 0;
1731 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1733 if (tPtr
->view
->flags
.realized
) {
1736 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1737 tPtr
->db
= (Pixmap
) NULL
;
1740 if (tPtr
->visible
.w
< 40)
1741 tPtr
->visible
.w
= 40;
1742 if (tPtr
->visible
.h
< 20)
1743 tPtr
->visible
.h
= 20;
1746 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1747 tPtr
->view
->window
, tPtr
->visible
.w
,
1748 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1755 W_ViewDelegate _TextViewDelegate
= {
1763 #define TEXT_BUFFER_INCR 8
1764 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1766 static void clearText(Text
* tPtr
)
1768 tPtr
->vpos
= tPtr
->hpos
= 0;
1769 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1770 tPtr
->cursor
.x
= -23;
1772 if (!tPtr
->firstTextBlock
)
1775 while (tPtr
->currentTextBlock
)
1776 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1778 tPtr
->firstTextBlock
= NULL
;
1779 tPtr
->currentTextBlock
= NULL
;
1780 tPtr
->lastTextBlock
= NULL
;
1781 WMEmptyArray(tPtr
->gfxItems
);
1784 /* possibly remove a single character from the currentTextBlock,
1785 or if there's a selection, remove it...
1786 note that Delete and Backspace are treated differently */
1787 static void deleteTextInteractively(Text
* tPtr
, KeySym ksym
)
1790 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1791 Bool done
= 1, wasFirst
= 0;
1793 if (!tPtr
->flags
.editable
)
1796 if (!(tb
= tPtr
->currentTextBlock
))
1799 if (tPtr
->flags
.ownsSelection
) {
1800 if (removeSelection(tPtr
))
1801 layOutDocument(tPtr
);
1805 wasFirst
= tb
->first
;
1806 if (back
&& tPtr
->tpos
< 1) {
1808 if (tb
->prior
->blank
) {
1809 tPtr
->currentTextBlock
= tb
->prior
;
1810 WMRemoveTextBlock(tPtr
);
1811 tPtr
->currentTextBlock
= tb
;
1813 layOutDocument(tPtr
);
1817 TextBlock
*prior
= tb
->prior
;
1818 tPtr
->currentTextBlock
= tb
;
1819 WMRemoveTextBlock(tPtr
);
1828 tPtr
->tpos
= tb
->used
;
1830 tPtr
->currentTextBlock
= tb
;
1834 tb
->next
->first
= False
;
1835 layOutDocument(tPtr
);
1842 if ((tb
->used
> 0) && ((back
? tPtr
->tpos
> 0 : 1))
1843 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1846 memmove(&(tb
->text
[tPtr
->tpos
]), &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1851 /* if there are no characters left to back over in the textblock,
1852 but it still has characters to the right of the cursor: */
1853 if ((back
? (tPtr
->tpos
== 0 && !done
) : (tPtr
->tpos
>= tb
->used
))
1856 /* no more chars, and it's marked as blank? */
1858 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1860 if (tb
->used
== 0 || tb
->graphic
)
1861 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1864 tPtr
->currentTextBlock
= sibling
;
1866 tPtr
->tpos
= (back
? 1 : 0);
1868 tPtr
->tpos
= (back
? sibling
->used
: 0);
1870 /* no more chars, so mark it as blank */
1871 } else if (tb
->used
== 0) {
1873 } else if (tb
->graphic
) {
1874 Bool hasNext
= (tb
->next
!= NULL
);
1876 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1879 } else if (tPtr
->currentTextBlock
) {
1880 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
? 1 : tPtr
->currentTextBlock
->used
);
1883 printf("DEBUG: unaccounted for... catch this!\n");
1886 layOutDocument(tPtr
);
1889 static void insertTextInteractively(Text
* tPtr
, char *text
, int len
)
1892 char *newline
= NULL
;
1894 if (!tPtr
->flags
.editable
) {
1898 if (len
< 1 || !text
)
1901 if (tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1904 if (tPtr
->flags
.ownsSelection
)
1905 removeSelection(tPtr
);
1907 if (tPtr
->flags
.ignoreNewLine
) {
1909 for (i
= 0; i
< len
; i
++) {
1910 if (text
[i
] == '\n')
1915 tb
= tPtr
->currentTextBlock
;
1916 if (!tb
|| tb
->graphic
) {
1918 WMAppendTextStream(tPtr
, text
);
1919 layOutDocument(tPtr
);
1923 if ((newline
= strchr(text
, '\n'))) {
1924 int nlen
= (int)(newline
- text
);
1925 int s
= tb
->used
- tPtr
->tpos
;
1927 if (!tb
->blank
&& nlen
> 0) {
1932 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
1933 tb
->used
-= (tb
->used
- tPtr
->tpos
);
1935 insertTextInteractively(tPtr
, text
, nlen
);
1937 WMAppendTextStream(tPtr
, newline
);
1939 insertTextInteractively(tPtr
, save
, s
);
1943 if (tPtr
->tpos
> 0 && tPtr
->tpos
< tb
->used
&& !tb
->graphic
&& tb
->text
) {
1945 unsigned short savePos
= tPtr
->tpos
;
1946 void *ntb
= WMCreateTextBlockWithText(tPtr
, &tb
->text
[tPtr
->tpos
],
1947 tb
->d
.font
, tb
->color
, True
,
1948 tb
->used
- tPtr
->tpos
);
1950 if (tb
->sections
[0].end
== tPtr
->tpos
)
1951 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1953 tb
->color
, True
, 0));
1956 WMAppendTextBlock(tPtr
, ntb
);
1959 } else if (tPtr
->tpos
== tb
->used
) {
1960 if (tPtr
->flags
.indentNewLine
) {
1961 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1963 tb
->color
, True
, 4));
1966 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1968 tb
->color
, True
, 0));
1971 } else if (tPtr
->tpos
== 0) {
1972 if (tPtr
->flags
.indentNewLine
) {
1973 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1975 tb
->color
, True
, 4));
1977 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1979 tb
->color
, True
, 0));
1982 if (tPtr
->currentTextBlock
->next
)
1983 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
1987 if (tb
->used
+ len
>= tb
->allocated
) {
1988 tb
->allocated
= reqBlockSize(tb
->used
+ len
);
1989 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
1993 memcpy(tb
->text
, text
, len
);
1996 tb
->text
[tb
->used
] = 0;
2000 memmove(&(tb
->text
[tPtr
->tpos
+ len
]), &tb
->text
[tPtr
->tpos
], tb
->used
- tPtr
->tpos
+ 1);
2001 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2004 tb
->text
[tb
->used
] = 0;
2009 layOutDocument(tPtr
);
2012 static void selectRegion(Text
* tPtr
, int x
, int y
)
2018 y
+= (tPtr
->flags
.rulerShown
? 40 : 0);
2021 y
-= 10; /* the original offset */
2023 x
-= tPtr
->visible
.x
- 2;
2027 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2028 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2029 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2030 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2032 tPtr
->flags
.ownsSelection
= True
;
2036 static void releaseSelection(Text
* tPtr
)
2038 TextBlock
*tb
= tPtr
->firstTextBlock
;
2041 tb
->selected
= False
;
2044 tPtr
->flags
.ownsSelection
= False
;
2045 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2050 WMData
*requestHandler(WMView
* view
, Atom selection
, Atom target
, void *cdata
, Atom
* type
)
2052 Text
*tPtr
= view
->self
;
2053 Display
*dpy
= tPtr
->view
->screen
->display
;
2055 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2056 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2057 WMData
*data
= NULL
;
2059 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2060 char *text
= WMGetTextSelectedStream(tPtr
);
2063 data
= WMCreateDataWithBytes(text
, strlen(text
));
2064 WMSetDataFormat(data
, TYPETEXT
);
2069 printf("didn't get it\n");
2071 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2072 if (target
== _TARGETS
) {
2075 ptr
= wmalloc(4 * sizeof(Atom
));
2079 ptr
[3] = COMPOUND_TEXT
;
2081 data
= WMCreateDataWithBytes(ptr
, 4 * 4);
2082 WMSetDataFormat(data
, 32);
2091 static void lostHandler(WMView
* view
, Atom selection
, void *cdata
)
2093 releaseSelection((WMText
*) view
->self
);
2096 static WMSelectionProcs selectionHandler
= {
2097 requestHandler
, lostHandler
, NULL
2100 static void ownershipObserver(void *observerData
, WMNotification
* notification
)
2102 if (observerData
!= WMGetNotificationClientData(notification
))
2103 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2106 static void autoSelectText(Text
* tPtr
, int clicks
)
2110 char *mark
= NULL
, behind
, ahead
;
2112 if (!(tb
= tPtr
->currentTextBlock
))
2117 switch (tb
->text
[tPtr
->tpos
]) {
2121 case '<': case '>': behind = '<'; ahead = '>'; break;
2122 case '{': case '}': behind = '{'; ahead = '}'; break;
2123 case '[': case ']': behind = '['; ahead = ']'; break;
2126 behind
= ahead
= ' ';
2129 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
2130 tPtr
->sel
.h
= 6; /*tPtr->cursor.h-10; */
2133 tPtr
->sel
.x
= tb
->sections
[0].x
;
2134 tPtr
->sel
.w
= tb
->sections
[0].w
;
2136 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
2139 while (start
> 0 && tb
->text
[start
- 1] != behind
)
2143 if (tPtr
->tpos
> start
) {
2144 x
-= WMWidthOfString(font
, &tb
->text
[start
], tPtr
->tpos
- start
);
2146 tPtr
->sel
.x
= (x
< 0 ? 0 : x
) + 1;
2148 if ((mark
= strchr(&tb
->text
[start
], ahead
))) {
2149 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2150 (int)(mark
- &tb
->text
[start
]));
2151 } else if (tb
->used
> start
) {
2152 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
], tb
->used
- start
);
2156 } else if (clicks
== 3) {
2157 TextBlock
*cur
= tb
;
2159 while (tb
&& !tb
->first
) {
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
;
2171 tPtr
->sel
.w
= tPtr
->docWidth
;
2172 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2175 if (!tPtr
->flags
.ownsSelection
) {
2176 WMCreateSelectionHandler(tPtr
->view
, XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2177 tPtr
->flags
.ownsSelection
= True
;
2184 static void fontChanged(void *observerData
, WMNotification
* notification
)
2186 WMText
*tPtr
= (WMText
*) observerData
;
2187 WMFont
*font
= (WMFont
*) WMGetNotificationClientData(notification
);
2188 printf("fontChanged\n");
2193 if (tPtr
->flags
.ownsSelection
)
2194 WMSetTextSelectionFont(tPtr
, font
);
2198 static void handleTextKeyPress(Text
* tPtr
, XEvent
* event
)
2202 int control_pressed
= False
;
2203 TextBlock
*tb
= NULL
;
2205 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2206 control_pressed
= True
;
2207 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2212 if ((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2214 updateCursorPosition(tPtr
);
2219 if ((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2220 if (tPtr
->currentTextBlock
->graphic
)
2223 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2225 updateCursorPosition(tPtr
);
2230 if (!(tb
= tPtr
->currentTextBlock
))
2235 if (tPtr
->tpos
== 0) {
2238 tPtr
->currentTextBlock
= tb
->prior
;
2239 if (tPtr
->currentTextBlock
->graphic
)
2242 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2244 if (!tb
->first
&& tPtr
->tpos
> 0)
2250 updateCursorPosition(tPtr
);
2255 if (!(tb
= tPtr
->currentTextBlock
))
2259 if (tPtr
->tpos
== tb
->used
) {
2262 tPtr
->currentTextBlock
= tb
->next
;
2264 if (!tb
->next
->first
&& tb
->next
->used
> 0)
2270 tPtr
->tpos
= tb
->used
;
2274 updateCursorPosition(tPtr
);
2279 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2280 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2285 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2286 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2295 deleteTextInteractively(tPtr
, ksym
);
2296 updateCursorPosition(tPtr
);
2302 control_pressed
= True
;
2306 insertTextInteractively(tPtr
, " ", 4);
2307 updateCursorPosition(tPtr
);
2314 if (*buffer
!= 0 && !control_pressed
) {
2315 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2316 updateCursorPosition(tPtr
);
2319 } else if (control_pressed
&& ksym
== XK_r
) {
2320 Bool i
= !tPtr
->flags
.rulerShown
;
2321 WMShowTextRuler(tPtr
, i
);
2322 tPtr
->flags
.rulerShown
= i
;
2323 } else if (control_pressed
&& *buffer
== '\a') {
2324 XBell(tPtr
->view
->screen
->display
, 0);
2326 WMRelayToNextResponder(tPtr
->view
, event
);
2330 if (!control_pressed
&& tPtr
->flags
.ownsSelection
) {
2331 releaseSelection(tPtr
);
2335 static void pasteText(WMView
* view
, Atom selection
, Atom target
, Time timestamp
, void *cdata
, WMData
* data
)
2337 Text
*tPtr
= (Text
*) view
->self
;
2340 tPtr
->flags
.waitingForSelection
= 0;
2343 text
= (char *)WMDataBytes(data
);
2346 (tPtr
->parser
) (tPtr
, (void *)text
);
2347 layOutDocument(tPtr
);
2349 insertTextInteractively(tPtr
, text
, strlen(text
));
2350 updateCursorPosition(tPtr
);
2356 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2361 (tPtr
->parser
) (tPtr
, (void *)text
);
2362 layOutDocument(tPtr
);
2364 insertTextInteractively(tPtr
, text
, n
);
2365 updateCursorPosition(tPtr
);
2374 static void handleActionEvents(XEvent
* event
, void *data
)
2376 Text
*tPtr
= (Text
*) data
;
2377 Display
*dpy
= event
->xany
.display
;
2380 switch (event
->type
) {
2382 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2383 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2384 tPtr
->flags
.extendSelection
= True
;
2388 if (tPtr
->flags
.focused
) {
2389 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2390 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
2391 GrabModeAsync
, GrabModeAsync
, None
,
2392 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2393 tPtr
->flags
.pointerGrabbed
= True
;
2394 handleTextKeyPress(tPtr
, event
);
2400 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2401 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2402 tPtr
->flags
.extendSelection
= False
;
2404 /* end modify flag so selection can be extended */
2410 if (tPtr
->flags
.pointerGrabbed
) {
2411 tPtr
->flags
.pointerGrabbed
= False
;
2412 XUngrabPointer(dpy
, CurrentTime
);
2415 if (tPtr
->flags
.waitingForSelection
)
2418 if ((event
->xmotion
.state
& Button1Mask
)) {
2420 if (WMIsDraggingFromView(tPtr
->view
)) {
2421 WMDragImageFromView(tPtr
->view
, event
);
2425 if (!tPtr
->flags
.ownsSelection
) {
2426 WMCreateSelectionHandler(tPtr
->view
,
2427 XA_PRIMARY
, event
->xbutton
.time
, &selectionHandler
, NULL
);
2428 tPtr
->flags
.ownsSelection
= True
;
2430 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2434 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2439 if (tPtr
->flags
.pointerGrabbed
) {
2440 tPtr
->flags
.pointerGrabbed
= False
;
2441 XUngrabPointer(dpy
, CurrentTime
);
2445 if (tPtr
->flags
.waitingForSelection
)
2448 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2449 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2453 if (tPtr
->flags
.ownsSelection
)
2454 releaseSelection(tPtr
);
2456 if (event
->xbutton
.button
== Button1
) {
2457 TextBlock
*tb
= tPtr
->currentTextBlock
;
2459 if (WMIsDoubleClick(event
)) {
2461 tPtr
->lastClickTime
= event
->xbutton
.time
;
2462 if (tb
&& tb
->graphic
&& !tb
->object
) {
2463 if (tPtr
->delegate
&& tPtr
->delegate
->didDoubleClickOnPicture
) {
2466 desc
= wmalloc(tb
->used
+ 1);
2467 memcpy(desc
, tb
->text
, tb
->used
);
2469 (*tPtr
->delegate
->didDoubleClickOnPicture
) (tPtr
->delegate
, desc
);
2473 autoSelectText(tPtr
, 2);
2476 } else if (event
->xbutton
.time
- tPtr
->lastClickTime
< WINGsConfiguration
.doubleClickDelay
) {
2477 tPtr
->lastClickTime
= event
->xbutton
.time
;
2478 autoSelectText(tPtr
, 3);
2482 if (!tPtr
->flags
.focused
) {
2483 WMSetFocusToWidget(tPtr
);
2484 tPtr
->flags
.focused
= True
;
2485 } else if (tb
&& tPtr
->flags
.isOverGraphic
&& tb
->graphic
&& !tb
->object
&& tb
->d
.pixmap
) {
2487 WMSetViewDragImage(tPtr
->view
, tb
->d
.pixmap
);
2488 WMDragImageFromView(tPtr
->view
, event
);
2492 tPtr
->lastClickTime
= event
->xbutton
.time
;
2493 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2497 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelDown
) {
2498 WMScrollText(tPtr
, 16);
2502 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
) {
2503 WMScrollText(tPtr
, -16);
2507 if (event
->xbutton
.button
== Button2
) {
2511 if (!tPtr
->flags
.editable
) {
2516 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2517 event
->xbutton
.time
, pasteText
, NULL
)) {
2519 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2520 tPtr
->flags
.waitingForSelection
= 0;
2526 (tPtr
->parser
) (tPtr
, (void *)text
);
2527 layOutDocument(tPtr
);
2529 insertTextInteractively(tPtr
, text
, n
);
2533 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2534 (void *)WMInsertTextEvent
);
2536 updateCursorPosition(tPtr
);
2540 tPtr
->flags
.waitingForSelection
= True
;
2547 if (tPtr
->flags
.pointerGrabbed
) {
2548 tPtr
->flags
.pointerGrabbed
= False
;
2549 XUngrabPointer(dpy
, CurrentTime
);
2553 if (tPtr
->flags
.waitingForSelection
)
2556 if (WMIsDraggingFromView(tPtr
->view
))
2557 WMDragImageFromView(tPtr
->view
, event
);
2562 static void handleEvents(XEvent
* event
, void *data
)
2564 Text
*tPtr
= (Text
*) data
;
2566 switch (event
->type
) {
2569 if (event
->xexpose
.count
!= 0)
2573 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2574 WMRealizeWidget(tPtr
->hS
);
2578 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2579 WMRealizeWidget(tPtr
->vS
);
2583 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2584 WMRealizeWidget(tPtr
->ruler
);
2589 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2595 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2598 tPtr
->flags
.focused
= True
;
2600 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2601 tPtr
->timerID
= WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY
, blinkCursor
, tPtr
);
2608 tPtr
->flags
.focused
= False
;
2611 if (tPtr
->timerID
) {
2612 WMDeleteTimerHandler(tPtr
->timerID
);
2613 tPtr
->timerID
= NULL
;
2621 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2623 WMEmptyArray(tPtr
->gfxItems
);
2626 WMDeleteTimerHandler(tPtr
->timerID
);
2628 WMReleaseFont(tPtr
->dFont
);
2629 WMReleaseColor(tPtr
->dColor
);
2630 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2631 WMRemoveNotificationObserver(tPtr
);
2633 WMFreeArray(tPtr
->xdndSourceTypes
);
2634 WMFreeArray(tPtr
->xdndDestinationTypes
);
2643 static void insertPlainText(Text
* tPtr
, char *text
)
2650 mark
= strchr(start
, '\n');
2652 tb
= WMCreateTextBlockWithText(tPtr
,
2654 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
- start
));
2656 tPtr
->flags
.first
= True
;
2658 if (start
&& strlen(start
)) {
2659 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2660 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2663 tPtr
->flags
.first
= False
;
2667 if (tPtr
->flags
.prepend
)
2668 WMPrependTextBlock(tPtr
, tb
);
2670 WMAppendTextBlock(tPtr
, tb
);
2674 static void rulerMoveCallBack(WMWidget
* w
, void *self
)
2676 Text
*tPtr
= (Text
*) self
;
2680 if (W_CLASS(tPtr
) != WC_Text
)
2686 static void rulerReleaseCallBack(WMWidget
* w
, void *self
)
2688 Text
*tPtr
= (Text
*) self
;
2692 if (W_CLASS(tPtr
) != WC_Text
)
2699 static WMArray
*dropDataTypes(WMView
* self
)
2701 return ((Text
*) self
->self
)->xdndSourceTypes
;
2704 static WMDragOperationType
wantedDropOperation(WMView
* self
)
2706 return WDOperationCopy
;
2709 static Bool
acceptDropOperation(WMView
* self
, WMDragOperationType allowedOperation
)
2711 return (allowedOperation
== WDOperationCopy
);
2714 static WMData
*fetchDragData(WMView
* self
, char *type
)
2716 TextBlock
*tb
= ((WMText
*) self
->self
)->currentTextBlock
;
2720 if (strcmp(type
, "text/plain")) {
2724 desc
= wmalloc(tb
->used
+ 1);
2725 memcpy(desc
, tb
->text
, tb
->used
);
2727 data
= WMCreateDataWithBytes(desc
, strlen(desc
) + 1);
2737 static WMDragSourceProcs _DragSourceProcs
= {
2739 wantedDropOperation
,
2741 acceptDropOperation
,
2747 static WMArray
*requiredDataTypes(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2749 return ((Text
*) self
->self
)->xdndDestinationTypes
;
2752 static WMDragOperationType
allowedOperation(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2754 return WDOperationCopy
;
2757 static void performDragOperation(WMView
* self
, WMArray
* dropData
, WMArray
* operations
, WMPoint
* dropLocation
)
2759 WMText
*tPtr
= (WMText
*) self
->self
;
2766 /* only one required type, implies only one drop data */
2768 /* get application/X-color if any */
2769 data
= (WMData
*) WMPopFromArray(dropData
);
2771 colorName
= (char *)WMDataBytes(data
);
2772 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
2775 WMSetTextSelectionColor(tPtr
, color
);
2776 WMReleaseColor(color
);
2782 static WMDragDestinationProcs _DragDestinationProcs
= {
2787 performDragOperation
,
2791 char *getStream(WMText
* tPtr
, int sel
, int array
)
2793 TextBlock
*tb
= NULL
;
2795 unsigned long where
= 0;
2800 if (!(tb
= tPtr
->firstTextBlock
))
2804 (tPtr
->writer
) (tPtr
, (void *)text
);
2808 tb
= tPtr
->firstTextBlock
;
2811 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2813 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2815 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2816 && tb
!= tPtr
->firstTextBlock
) {
2817 text
= wrealloc(text
, where
+ 1);
2818 text
[where
++] = '\n';
2824 if (tb
->graphic
&& array
) {
2825 text
= wrealloc(text
, where
+ 4);
2826 text
[where
++] = 0xFA;
2827 text
[where
++] = (tb
->used
>> 8) & 0x0ff;
2828 text
[where
++] = tb
->used
& 0x0ff;
2829 text
[where
++] = tb
->allocated
; /* extra info */
2831 text
= wrealloc(text
, where
+ tb
->used
);
2832 memcpy(&text
[where
], tb
->text
, tb
->used
);
2835 } else if (sel
&& tb
->selected
) {
2837 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
2838 text
= wrealloc(text
, where
+ 1);
2839 text
[where
++] = '\n';
2845 text
= wrealloc(text
, where
+ (tb
->s_end
- tb
->s_begin
));
2846 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
], tb
->s_end
- tb
->s_begin
);
2847 where
+= tb
->s_end
- tb
->s_begin
;
2852 _gSnext
: tb
= tb
->next
;
2855 /* +1 for the end of string, let's be nice */
2856 text
= wrealloc(text
, where
+ 1);
2861 static void releaseStreamObjects(void *data
)
2867 WMArray
*getStreamObjects(WMText
* tPtr
, int sel
)
2869 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
2873 char *start
, *fa
, *desc
;
2875 stream
= getStream(tPtr
, sel
, 1);
2882 fa
= strchr(start
, 0xFA);
2884 if ((int)(fa
- start
) > 0) {
2886 desc
[(int)(fa
- start
)] = 0;
2887 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
2888 WMSetDataFormat(data
, TYPETEXT
);
2889 WMAddToArray(array
, (void *)data
);
2892 len
= *(fa
+ 1) * 0xff + *(fa
+ 2);
2893 data
= WMCreateDataWithBytes((void *)(fa
+ 4), len
);
2894 WMSetDataFormat(data
, *(fa
+ 3));
2895 WMAddToArray(array
, (void *)data
);
2896 start
= fa
+ len
+ 4;
2899 if (start
&& strlen(start
)) {
2900 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
2901 WMSetDataFormat(data
, TYPETEXT
);
2902 WMAddToArray(array
, (void *)data
);
2912 #define XDND_TEXT_DATA_TYPE "text/plain"
2913 #define XDND_COLOR_DATA_TYPE "application/X-color"
2914 static WMArray
*getXdndSourceTypeArray()
2916 WMArray
*types
= WMCreateArray(1);
2917 WMAddToArray(types
, XDND_TEXT_DATA_TYPE
);
2921 static WMArray
*getXdndDestinationTypeArray()
2923 WMArray
*types
= WMCreateArray(1);
2924 WMAddToArray(types
, XDND_COLOR_DATA_TYPE
);
2928 WMText
*WMCreateTextForDocumentType(WMWidget
* parent
, WMAction
* parser
, WMAction
* writer
)
2935 tPtr
= wmalloc(sizeof(Text
));
2936 tPtr
->widgetClass
= WC_Text
;
2937 tPtr
->view
= W_CreateView(W_VIEW(parent
));
2939 perror("could not create text's view\n");
2944 dpy
= tPtr
->view
->screen
->display
;
2945 scr
= tPtr
->view
->screen
;
2947 tPtr
->view
->self
= tPtr
;
2948 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
2949 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
2950 W_ResizeView(tPtr
->view
, 250, 200);
2952 tPtr
->dColor
= WMBlackColor(scr
);
2953 tPtr
->fgColor
= WMBlackColor(scr
);
2954 tPtr
->bgColor
= WMWhiteColor(scr
);
2955 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
2957 gcv
.graphics_exposures
= False
;
2958 gcv
.foreground
= W_PIXEL(scr
->gray
);
2959 gcv
.background
= W_PIXEL(scr
->darkGray
);
2960 gcv
.fill_style
= FillStippled
;
2961 /* why not use scr->stipple here? */
2962 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
, STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
2963 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
2964 GCForeground
| GCBackground
| GCStipple
2965 | GCFillStyle
| GCGraphicsExposures
, &gcv
);
2971 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
2973 tPtr
->view
->delegate
= &_TextViewDelegate
;
2975 tPtr
->delegate
= NULL
;
2978 tPtr
->timerID
= NULL
;
2981 WMCreateEventHandler(tPtr
->view
, ExposureMask
| StructureNotifyMask
2982 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
, handleEvents
, tPtr
);
2984 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
| ButtonPressMask
2985 | KeyReleaseMask
| KeyPressMask
| Button1MotionMask
, handleActionEvents
, tPtr
);
2987 WMAddNotificationObserver(ownershipObserver
, tPtr
, WMSelectionOwnerDidChangeNotification
, tPtr
);
2989 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
2990 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
2993 WMArray
*types
= WMCreateArray(2);
2994 WMAddToArray(types
, "application/X-color");
2995 WMAddToArray(types
, "application/X-image");
2996 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
2999 /*WMAddNotificationObserver(fontChanged, tPtr,
3000 WMFontPanelDidChangeNotification, tPtr); */
3002 tPtr
->firstTextBlock
= NULL
;
3003 tPtr
->lastTextBlock
= NULL
;
3004 tPtr
->currentTextBlock
= NULL
;
3007 tPtr
->gfxItems
= WMCreateArray(4);
3009 tPtr
->parser
= parser
;
3010 tPtr
->writer
= writer
;
3012 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3013 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3015 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3017 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3018 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3019 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3021 tPtr
->cursor
.x
= -23;
3024 tPtr
->docHeight
= 0;
3025 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
, default_bullet
);
3026 tPtr
->db
= (Pixmap
) NULL
;
3027 tPtr
->bgPixmap
= NULL
;
3029 tPtr
->margins
= WMGetRulerMargins(NULL
);
3030 tPtr
->margins
->right
= tPtr
->visible
.w
;
3033 tPtr
->flags
.rulerShown
= False
;
3034 tPtr
->flags
.monoFont
= False
;
3035 tPtr
->flags
.focused
= False
;
3036 tPtr
->flags
.editable
= True
;
3037 tPtr
->flags
.ownsSelection
= False
;
3038 tPtr
->flags
.pointerGrabbed
= False
;
3039 tPtr
->flags
.extendSelection
= False
;
3040 tPtr
->flags
.frozen
= False
;
3041 tPtr
->flags
.cursorShown
= True
;
3042 tPtr
->flags
.acceptsGraphic
= False
;
3043 tPtr
->flags
.horizOnDemand
= False
;
3044 tPtr
->flags
.needsLayOut
= False
;
3045 tPtr
->flags
.ignoreNewLine
= False
;
3046 tPtr
->flags
.indentNewLine
= False
;
3047 tPtr
->flags
.laidOut
= False
;
3048 tPtr
->flags
.ownsSelection
= False
;
3049 tPtr
->flags
.waitingForSelection
= False
;
3050 tPtr
->flags
.prepend
= False
;
3051 tPtr
->flags
.isOverGraphic
= False
;
3052 tPtr
->flags
.relief
= WRSunken
;
3053 tPtr
->flags
.isOverGraphic
= 0;
3054 tPtr
->flags
.alignment
= WALeft
;
3055 tPtr
->flags
.first
= True
;
3057 tPtr
->xdndSourceTypes
= getXdndSourceTypeArray();
3058 tPtr
->xdndDestinationTypes
= getXdndDestinationTypeArray();
3063 void WMPrependTextStream(WMText
* tPtr
, char *text
)
3065 CHECK_CLASS(tPtr
, WC_Text
);
3068 if (tPtr
->flags
.ownsSelection
)
3069 releaseSelection(tPtr
);
3071 updateScrollers(tPtr
);
3075 tPtr
->flags
.prepend
= True
;
3076 if (text
&& tPtr
->parser
)
3077 (tPtr
->parser
) (tPtr
, (void *)text
);
3079 insertPlainText(tPtr
, text
);
3081 tPtr
->flags
.needsLayOut
= True
;
3083 if (!tPtr
->flags
.frozen
) {
3084 layOutDocument(tPtr
);
3088 void WMAppendTextStream(WMText
* tPtr
, char *text
)
3090 CHECK_CLASS(tPtr
, WC_Text
);
3093 if (tPtr
->flags
.ownsSelection
)
3094 releaseSelection(tPtr
);
3096 updateScrollers(tPtr
);
3100 tPtr
->flags
.prepend
= False
;
3101 if (text
&& tPtr
->parser
)
3102 (tPtr
->parser
) (tPtr
, (void *)text
);
3104 insertPlainText(tPtr
, text
);
3106 tPtr
->flags
.needsLayOut
= True
;
3107 if (tPtr
->currentTextBlock
) {
3108 if (tPtr
->currentTextBlock
->graphic
)
3111 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3114 if (!tPtr
->flags
.frozen
) {
3115 layOutDocument(tPtr
);
3119 char *WMGetTextStream(WMText
* tPtr
)
3121 CHECK_CLASS(tPtr
, WC_Text
);
3123 return getStream(tPtr
, 0, 0);
3126 char *WMGetTextSelectedStream(WMText
* tPtr
)
3128 CHECK_CLASS(tPtr
, WC_Text
);
3130 return getStream(tPtr
, 1, 0);
3133 WMArray
*WMGetTextObjects(WMText
* tPtr
)
3135 CHECK_CLASS(tPtr
, WC_Text
);
3137 return getStreamObjects(tPtr
, 0);
3140 WMArray
*WMGetTextSelectedObjects(WMText
* tPtr
)
3142 CHECK_CLASS(tPtr
, WC_Text
);
3144 return getStreamObjects(tPtr
, 1);
3147 void WMSetTextDelegate(WMText
* tPtr
, WMTextDelegate
* delegate
)
3149 CHECK_CLASS(tPtr
, WC_Text
);
3151 tPtr
->delegate
= delegate
;
3154 void *WMCreateTextBlockWithObject(WMText
* tPtr
, WMWidget
* w
,
3155 char *description
, WMColor
* color
,
3156 unsigned short first
, unsigned short extraInfo
)
3160 if (!w
|| !description
|| !color
)
3163 tb
= wmalloc(sizeof(TextBlock
));
3165 tb
->text
= wstrdup(description
);
3166 tb
->used
= strlen(description
);
3169 tb
->color
= WMRetainColor(color
);
3170 tb
->marginN
= newMargin(tPtr
, NULL
);
3171 tb
->allocated
= extraInfo
;
3176 tb
->underlined
= False
;
3177 tb
->selected
= False
;
3179 tb
->sections
= NULL
;
3187 void *WMCreateTextBlockWithPixmap(WMText
* tPtr
, WMPixmap
* p
,
3188 char *description
, WMColor
* color
,
3189 unsigned short first
, unsigned short extraInfo
)
3193 if (!p
|| !description
|| !color
)
3196 tb
= wmalloc(sizeof(TextBlock
));
3198 tb
->text
= wstrdup(description
);
3199 tb
->used
= strlen(description
);
3201 tb
->d
.pixmap
= WMRetainPixmap(p
);
3202 tb
->color
= WMRetainColor(color
);
3203 tb
->marginN
= newMargin(tPtr
, NULL
);
3204 tb
->allocated
= extraInfo
;
3209 tb
->underlined
= False
;
3210 tb
->selected
= False
;
3212 tb
->sections
= NULL
;
3220 void *WMCreateTextBlockWithText(WMText
* tPtr
, char *text
, WMFont
* font
, WMColor
* color
,
3221 unsigned short first
, unsigned short len
)
3225 if (!font
|| !color
)
3228 tb
= wmalloc(sizeof(TextBlock
));
3230 tb
->allocated
= reqBlockSize(len
);
3231 tb
->text
= (char *)wmalloc(tb
->allocated
);
3233 if (len
< 1 || !text
|| (*text
== '\n' && len
== 1)) {
3238 memcpy(tb
->text
, text
, len
);
3242 tb
->text
[tb
->used
] = 0;
3244 tb
->d
.font
= WMRetainFont(font
);
3245 tb
->color
= WMRetainColor(color
);
3246 tb
->marginN
= newMargin(tPtr
, NULL
);
3249 tb
->graphic
= False
;
3250 tb
->underlined
= False
;
3251 tb
->selected
= False
;
3253 tb
->sections
= NULL
;
3261 WMSetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int first
,
3262 unsigned int kanji
, unsigned int underlined
, int script
, WMRulerMargins
* margins
)
3264 TextBlock
*tb
= (TextBlock
*) vtb
;
3270 tb
->underlined
= underlined
;
3271 tb
->script
= script
;
3272 tb
->marginN
= newMargin(tPtr
, margins
);
3276 WMGetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int *first
,
3277 unsigned int *kanji
, unsigned int *underlined
, int *script
, WMRulerMargins
* margins
)
3279 TextBlock
*tb
= (TextBlock
*) vtb
;
3288 *underlined
= tb
->underlined
;
3290 *script
= tb
->script
;
3292 margins
= &tPtr
->margins
[tb
->marginN
];
3295 void WMPrependTextBlock(WMText
* tPtr
, void *vtb
)
3297 TextBlock
*tb
= (TextBlock
*) vtb
;
3304 WMWidget
*w
= tb
->d
.widget
;
3305 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3306 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3307 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3310 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3314 tPtr
->tpos
= tb
->used
;
3317 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3318 tb
->next
= tb
->prior
= NULL
;
3320 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3325 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3328 tb
->next
= tPtr
->currentTextBlock
;
3329 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3330 if (tPtr
->currentTextBlock
->prior
)
3331 tPtr
->currentTextBlock
->prior
->next
= tb
;
3333 tPtr
->currentTextBlock
->prior
= tb
;
3335 tPtr
->firstTextBlock
= tb
;
3337 tPtr
->currentTextBlock
= tb
;
3340 void WMAppendTextBlock(WMText
* tPtr
, void *vtb
)
3342 TextBlock
*tb
= (TextBlock
*) vtb
;
3349 WMWidget
*w
= tb
->d
.widget
;
3350 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3351 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3352 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3355 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3359 tPtr
->tpos
= tb
->used
;
3362 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3363 tb
->next
= tb
->prior
= NULL
;
3365 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3370 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3373 tb
->next
= tPtr
->currentTextBlock
->next
;
3374 tb
->prior
= tPtr
->currentTextBlock
;
3375 if (tPtr
->currentTextBlock
->next
)
3376 tPtr
->currentTextBlock
->next
->prior
= tb
;
3378 tPtr
->currentTextBlock
->next
= tb
;
3381 tPtr
->lastTextBlock
= tb
;
3383 tPtr
->currentTextBlock
= tb
;
3386 void *WMRemoveTextBlock(WMText
* tPtr
)
3388 TextBlock
*tb
= NULL
;
3390 if (!tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
|| !tPtr
->currentTextBlock
) {
3394 tb
= tPtr
->currentTextBlock
;
3396 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3399 WMUnmapWidget(tb
->d
.widget
);
3403 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3404 if (tPtr
->currentTextBlock
->next
)
3405 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3407 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3408 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3410 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3411 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3412 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3413 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3415 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3416 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3417 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3424 static void destroyWidget(WMWidget
* widget
)
3426 WMDestroyWidget(widget
);
3427 // -- never do this -- wfree(widget);
3431 void WMDestroyTextBlock(WMText
* tPtr
, void *vtb
)
3433 TextBlock
*tb
= (TextBlock
*) vtb
;
3439 /* naturally, there's a danger to destroying widgets whose action
3440 * brings us here: ie. press a button to destroy it...
3441 * need to find a safer way. till then... this stays commented out */
3442 /* 5 months later... destroy it 10 seconds after now which should
3443 * be enough time for the widget's action to be completed... :-) */
3444 /* This is a bad assumption. Just destroy the widget here.
3445 * if the caller needs it, it can protect it with W_RetainView()
3446 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3447 WMDestroyWidget(tb
->d
.widget
);
3449 WMReleasePixmap(tb
->d
.pixmap
);
3452 WMReleaseFont(tb
->d
.font
);
3455 WMReleaseColor(tb
->color
);
3456 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3458 wfree(tb
->sections
);
3463 void WMSetTextForegroundColor(WMText
* tPtr
, WMColor
* color
)
3466 WMReleaseColor(tPtr
->fgColor
);
3468 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3473 void WMSetTextBackgroundColor(WMText
* tPtr
, WMColor
* color
)
3476 WMReleaseColor(tPtr
->bgColor
);
3478 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3479 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3484 void WMSetTextBackgroundPixmap(WMText
* tPtr
, WMPixmap
* pixmap
)
3487 WMReleasePixmap(tPtr
->bgPixmap
);
3490 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3492 tPtr
->bgPixmap
= NULL
;
3495 void WMSetTextRelief(WMText
* tPtr
, WMReliefType relief
)
3497 tPtr
->flags
.relief
= relief
;
3498 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3501 void WMSetTextHasHorizontalScroller(WMText
* tPtr
, Bool shouldhave
)
3503 if (shouldhave
&& !tPtr
->hS
) {
3504 tPtr
->hS
= WMCreateScroller(tPtr
);
3505 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3506 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3507 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3508 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3509 WMMapWidget(tPtr
->hS
);
3510 } else if (!shouldhave
&& tPtr
->hS
) {
3511 WMUnmapWidget(tPtr
->hS
);
3512 WMDestroyWidget(tPtr
->hS
);
3518 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3521 void WMSetTextHasRuler(WMText
* tPtr
, Bool shouldhave
)
3523 if (shouldhave
&& !tPtr
->ruler
) {
3524 tPtr
->ruler
= WMCreateRuler(tPtr
);
3525 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3526 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3527 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3528 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3529 } else if (!shouldhave
&& tPtr
->ruler
) {
3530 WMShowTextRuler(tPtr
, False
);
3531 WMDestroyWidget(tPtr
->ruler
);
3534 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3537 void WMShowTextRuler(WMText
* tPtr
, Bool show
)
3542 if (tPtr
->flags
.monoFont
)
3545 tPtr
->flags
.rulerShown
= show
;
3547 WMMapWidget(tPtr
->ruler
);
3549 WMUnmapWidget(tPtr
->ruler
);
3552 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3555 Bool
WMGetTextRulerShown(WMText
* tPtr
)
3560 return tPtr
->flags
.rulerShown
;
3563 void WMSetTextHasVerticalScroller(WMText
* tPtr
, Bool shouldhave
)
3565 if (shouldhave
&& !tPtr
->vS
) {
3566 tPtr
->vS
= WMCreateScroller(tPtr
);
3567 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3568 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3569 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3570 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3571 WMMapWidget(tPtr
->vS
);
3572 } else if (!shouldhave
&& tPtr
->vS
) {
3573 WMUnmapWidget(tPtr
->vS
);
3574 WMDestroyWidget(tPtr
->vS
);
3580 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3583 Bool
WMScrollText(WMText
* tPtr
, int amount
)
3585 Bool scroll
= False
;
3587 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3591 if (tPtr
->vpos
> 0) {
3592 if (tPtr
->vpos
> abs(amount
))
3593 tPtr
->vpos
+= amount
;
3599 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3600 if (tPtr
->vpos
< limit
) {
3601 if (tPtr
->vpos
< limit
- amount
)
3602 tPtr
->vpos
+= amount
;
3609 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3610 updateScrollers(tPtr
);
3613 tPtr
->prevVpos
= tPtr
->vpos
;
3617 Bool
WMPageText(WMText
* tPtr
, Bool direction
)
3619 if (!tPtr
->view
->flags
.realized
)
3622 return WMScrollText(tPtr
, direction
? tPtr
->visible
.h
: -tPtr
->visible
.h
);
3625 void WMSetTextEditable(WMText
* tPtr
, Bool editable
)
3627 tPtr
->flags
.editable
= editable
;
3630 int WMGetTextEditable(WMText
* tPtr
)
3632 return tPtr
->flags
.editable
;
3635 void WMSetTextIndentNewLines(WMText
* tPtr
, Bool indent
)
3637 tPtr
->flags
.indentNewLine
= indent
;
3640 void WMSetTextIgnoresNewline(WMText
* tPtr
, Bool ignore
)
3642 tPtr
->flags
.ignoreNewLine
= ignore
;
3645 Bool
WMGetTextIgnoresNewline(WMText
* tPtr
)
3647 return tPtr
->flags
.ignoreNewLine
;
3650 void WMSetTextUsesMonoFont(WMText
* tPtr
, Bool mono
)
3653 if (tPtr
->flags
.rulerShown
)
3654 WMShowTextRuler(tPtr
, False
);
3655 if (tPtr
->flags
.alignment
!= WALeft
)
3656 tPtr
->flags
.alignment
= WALeft
;
3659 tPtr
->flags
.monoFont
= mono
;
3660 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3663 Bool
WMGetTextUsesMonoFont(WMText
* tPtr
)
3665 return tPtr
->flags
.monoFont
;
3668 void WMSetTextDefaultFont(WMText
* tPtr
, WMFont
* font
)
3671 WMReleaseFont(tPtr
->dFont
);
3674 tPtr
->dFont
= WMRetainFont(font
);
3676 tPtr
->dFont
= WMSystemFontOfSize(tPtr
->view
->screen
, 12);
3680 WMFont
*WMGetTextDefaultFont(WMText
* tPtr
)
3682 return WMRetainFont(tPtr
->dFont
);
3685 void WMSetTextDefaultColor(WMText
* tPtr
, WMColor
* color
)
3688 WMReleaseColor(tPtr
->dColor
);
3691 tPtr
->dColor
= WMRetainColor(color
);
3693 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3697 WMColor
*WMGetTextDefaultColor(WMText
* tPtr
)
3699 return tPtr
->dColor
;
3702 void WMSetTextAlignment(WMText
* tPtr
, WMAlignment alignment
)
3704 if (tPtr
->flags
.monoFont
)
3705 tPtr
->flags
.alignment
= WALeft
;
3707 tPtr
->flags
.alignment
= alignment
;
3711 int WMGetTextInsertType(WMText
* tPtr
)
3713 return tPtr
->flags
.prepend
;
3716 void WMSetTextSelectionColor(WMText
* tPtr
, WMColor
* color
)
3718 setSelectionProperty(tPtr
, NULL
, color
, -1);
3721 WMColor
*WMGetTextSelectionColor(WMText
* tPtr
)
3725 tb
= tPtr
->currentTextBlock
;
3727 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3736 void WMSetTextSelectionFont(WMText
* tPtr
, WMFont
* font
)
3738 setSelectionProperty(tPtr
, font
, NULL
, -1);
3741 WMFont
*WMGetTextSelectionFont(WMText
* tPtr
)
3745 tb
= tPtr
->currentTextBlock
;
3747 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3754 tb
= getFirstNonGraphicBlockFor(tb
, 1);
3758 return (tb
->selected
? tb
->d
.font
: NULL
);
3761 void WMSetTextSelectionUnderlined(WMText
* tPtr
, int underlined
)
3764 if (underlined
!= 0 && underlined
!= 1)
3767 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
3770 int WMGetTextSelectionUnderlined(WMText
* tPtr
)
3774 tb
= tPtr
->currentTextBlock
;
3776 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3782 return tb
->underlined
;
3785 void WMFreezeText(WMText
* tPtr
)
3787 tPtr
->flags
.frozen
= True
;
3790 void WMThawText(WMText
* tPtr
)
3792 tPtr
->flags
.frozen
= False
;
3794 if (tPtr
->flags
.monoFont
) {
3795 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
3798 /* make sure to unmap widgets no matter where they are */
3799 /* they'll be later remapped if needed by paintText */
3800 for (j
= 0; j
< c
; j
++) {
3801 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
3802 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
3803 WMUnmapWidget(tb
->d
.widget
);
3808 tPtr
->flags
.laidOut
= False
;
3809 layOutDocument(tPtr
);
3810 updateScrollers(tPtr
);
3812 tPtr
->flags
.needsLayOut
= False
;
3816 /* find first occurence of a string */
3817 static char *mystrstr(char *haystack
, char *needle
, unsigned short len
, char *end
, Bool caseSensitive
)
3821 if (!haystack
|| !needle
|| !end
)
3824 for (ptr
= haystack
; ptr
< end
; ptr
++) {
3825 if (caseSensitive
) {
3826 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3830 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3838 /* find last occurence of a string */
3839 static char *mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
, Bool caseSensitive
)
3843 if (!haystack
|| !needle
|| !end
)
3846 for (ptr
= haystack
- 2; ptr
> end
; ptr
--) {
3847 if (caseSensitive
) {
3848 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3851 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3859 Bool
WMFindInTextStream(WMText
* tPtr
, char *needle
, Bool direction
, Bool caseSensitive
)
3866 if (!(tb
= tPtr
->currentTextBlock
)) {
3867 if (!(tb
= ((direction
> 0) ? tPtr
->firstTextBlock
: tPtr
->lastTextBlock
))) {
3871 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3872 tb = (direction>0) ? tb->next : tb->prior; */
3873 if (tb
!= tPtr
->lastTextBlock
)
3877 tb
= tPtr
->currentTextBlock
;
3883 if (direction
> 0) {
3884 if (pos
+ 1 < tb
->used
)
3887 if (tb
->used
- pos
> 0 && pos
> 0) {
3888 mark
= mystrstr(&tb
->text
[pos
], needle
,
3889 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
3902 mark
= mystrrstr(&tb
->text
[pos
], needle
,
3903 strlen(needle
), tb
->text
, caseSensitive
);
3914 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
3916 tPtr
->tpos
= (int)(mark
- tb
->text
);
3917 tPtr
->currentTextBlock
= tb
;
3918 updateCursorPosition(tPtr
);
3919 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
3920 tPtr
->sel
.h
= tPtr
->cursor
.h
- 10;
3921 tPtr
->sel
.x
= tPtr
->cursor
.x
+ 1;
3922 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
3923 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
3924 tPtr
->docWidth
- tPtr
->sel
.x
);
3925 tPtr
->flags
.ownsSelection
= True
;
3932 tb
= (direction
> 0) ? tb
->next
: tb
->prior
;
3934 pos
= (direction
> 0) ? 0 : tb
->used
;
3941 Bool
WMReplaceTextSelection(WMText
* tPtr
, char *replacement
)
3943 if (!tPtr
->flags
.ownsSelection
)
3946 removeSelection(tPtr
);
3949 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
3950 updateCursorPosition(tPtr
);