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 const 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 /* Parameter not used, but tell the compiler that it is ok */
1703 rel
= (tPtr
->flags
.relief
== WRFlat
);
1705 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1706 WMMoveWidget(tPtr
->ruler
, 2, 2);
1707 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1712 WMMoveWidget(tPtr
->vS
, 1 - (rel
? 1 : 0), rh
+ 1 - (rel
? 1 : 0));
1713 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
? 2 : 0));
1715 WMSetRulerOffset(tPtr
->ruler
, 22);
1717 WMSetRulerOffset(tPtr
->ruler
, 2);
1721 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1722 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1724 WMMoveWidget(tPtr
->hS
, vw
+ 1, h
- 21);
1725 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1729 tPtr
->visible
.x
= (tPtr
->vS
) ? 24 : 4;
1730 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) ? 43 : 3;
1731 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1732 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1733 tPtr
->visible
.h
-= (tPtr
->hS
) ? 20 : 0;
1734 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1736 if (tPtr
->view
->flags
.realized
) {
1739 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1740 tPtr
->db
= (Pixmap
) NULL
;
1743 if (tPtr
->visible
.w
< 40)
1744 tPtr
->visible
.w
= 40;
1745 if (tPtr
->visible
.h
< 20)
1746 tPtr
->visible
.h
= 20;
1749 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1750 tPtr
->view
->window
, tPtr
->visible
.w
,
1751 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1758 W_ViewDelegate _TextViewDelegate
= {
1766 #define TEXT_BUFFER_INCR 8
1767 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1769 static void clearText(Text
* tPtr
)
1771 tPtr
->vpos
= tPtr
->hpos
= 0;
1772 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1773 tPtr
->cursor
.x
= -23;
1775 if (!tPtr
->firstTextBlock
)
1778 while (tPtr
->currentTextBlock
)
1779 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1781 tPtr
->firstTextBlock
= NULL
;
1782 tPtr
->currentTextBlock
= NULL
;
1783 tPtr
->lastTextBlock
= NULL
;
1784 WMEmptyArray(tPtr
->gfxItems
);
1787 /* possibly remove a single character from the currentTextBlock,
1788 or if there's a selection, remove it...
1789 note that Delete and Backspace are treated differently */
1790 static void deleteTextInteractively(Text
* tPtr
, KeySym ksym
)
1793 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1794 Bool done
= 1, wasFirst
= 0;
1796 if (!tPtr
->flags
.editable
)
1799 if (!(tb
= tPtr
->currentTextBlock
))
1802 if (tPtr
->flags
.ownsSelection
) {
1803 if (removeSelection(tPtr
))
1804 layOutDocument(tPtr
);
1808 wasFirst
= tb
->first
;
1809 if (back
&& tPtr
->tpos
< 1) {
1811 if (tb
->prior
->blank
) {
1812 tPtr
->currentTextBlock
= tb
->prior
;
1813 WMRemoveTextBlock(tPtr
);
1814 tPtr
->currentTextBlock
= tb
;
1816 layOutDocument(tPtr
);
1820 TextBlock
*prior
= tb
->prior
;
1821 tPtr
->currentTextBlock
= tb
;
1822 WMRemoveTextBlock(tPtr
);
1831 tPtr
->tpos
= tb
->used
;
1833 tPtr
->currentTextBlock
= tb
;
1837 tb
->next
->first
= False
;
1838 layOutDocument(tPtr
);
1845 if ((tb
->used
> 0) && ((back
? tPtr
->tpos
> 0 : 1))
1846 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1849 memmove(&(tb
->text
[tPtr
->tpos
]), &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1854 /* if there are no characters left to back over in the textblock,
1855 but it still has characters to the right of the cursor: */
1856 if ((back
? (tPtr
->tpos
== 0 && !done
) : (tPtr
->tpos
>= tb
->used
))
1859 /* no more chars, and it's marked as blank? */
1861 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1863 if (tb
->used
== 0 || tb
->graphic
)
1864 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1867 tPtr
->currentTextBlock
= sibling
;
1869 tPtr
->tpos
= (back
? 1 : 0);
1871 tPtr
->tpos
= (back
? sibling
->used
: 0);
1873 /* no more chars, so mark it as blank */
1874 } else if (tb
->used
== 0) {
1876 } else if (tb
->graphic
) {
1877 Bool hasNext
= (tb
->next
!= NULL
);
1879 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1882 } else if (tPtr
->currentTextBlock
) {
1883 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
? 1 : tPtr
->currentTextBlock
->used
);
1886 printf("DEBUG: unaccounted for... catch this!\n");
1889 layOutDocument(tPtr
);
1892 static void insertTextInteractively(Text
* tPtr
, char *text
, int len
)
1895 char *newline
= NULL
;
1897 if (!tPtr
->flags
.editable
) {
1901 if (len
< 1 || !text
)
1904 if (tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1907 if (tPtr
->flags
.ownsSelection
)
1908 removeSelection(tPtr
);
1910 if (tPtr
->flags
.ignoreNewLine
) {
1912 for (i
= 0; i
< len
; i
++) {
1913 if (text
[i
] == '\n')
1918 tb
= tPtr
->currentTextBlock
;
1919 if (!tb
|| tb
->graphic
) {
1921 WMAppendTextStream(tPtr
, text
);
1922 layOutDocument(tPtr
);
1926 if ((newline
= strchr(text
, '\n'))) {
1927 int nlen
= (int)(newline
- text
);
1928 int s
= tb
->used
- tPtr
->tpos
;
1930 if (!tb
->blank
&& nlen
> 0) {
1935 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
1936 tb
->used
-= (tb
->used
- tPtr
->tpos
);
1938 insertTextInteractively(tPtr
, text
, nlen
);
1940 WMAppendTextStream(tPtr
, newline
);
1942 insertTextInteractively(tPtr
, save
, s
);
1946 if (tPtr
->tpos
> 0 && tPtr
->tpos
< tb
->used
&& !tb
->graphic
&& tb
->text
) {
1948 unsigned short savePos
= tPtr
->tpos
;
1949 void *ntb
= WMCreateTextBlockWithText(tPtr
, &tb
->text
[tPtr
->tpos
],
1950 tb
->d
.font
, tb
->color
, True
,
1951 tb
->used
- tPtr
->tpos
);
1953 if (tb
->sections
[0].end
== tPtr
->tpos
)
1954 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1956 tb
->color
, True
, 0));
1959 WMAppendTextBlock(tPtr
, ntb
);
1962 } else if (tPtr
->tpos
== tb
->used
) {
1963 if (tPtr
->flags
.indentNewLine
) {
1964 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1966 tb
->color
, True
, 4));
1969 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1971 tb
->color
, True
, 0));
1974 } else if (tPtr
->tpos
== 0) {
1975 if (tPtr
->flags
.indentNewLine
) {
1976 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1978 tb
->color
, True
, 4));
1980 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
1982 tb
->color
, True
, 0));
1985 if (tPtr
->currentTextBlock
->next
)
1986 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
1990 if (tb
->used
+ len
>= tb
->allocated
) {
1991 tb
->allocated
= reqBlockSize(tb
->used
+ len
);
1992 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
1996 memcpy(tb
->text
, text
, len
);
1999 tb
->text
[tb
->used
] = 0;
2003 memmove(&(tb
->text
[tPtr
->tpos
+ len
]), &tb
->text
[tPtr
->tpos
], tb
->used
- tPtr
->tpos
+ 1);
2004 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2007 tb
->text
[tb
->used
] = 0;
2012 layOutDocument(tPtr
);
2015 static void selectRegion(Text
* tPtr
, int x
, int y
)
2021 y
+= (tPtr
->flags
.rulerShown
? 40 : 0);
2024 y
-= 10; /* the original offset */
2026 x
-= tPtr
->visible
.x
- 2;
2030 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2031 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2032 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2033 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2035 tPtr
->flags
.ownsSelection
= True
;
2039 static void releaseSelection(Text
* tPtr
)
2041 TextBlock
*tb
= tPtr
->firstTextBlock
;
2044 tb
->selected
= False
;
2047 tPtr
->flags
.ownsSelection
= False
;
2048 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2053 static WMData
*requestHandler(WMView
* view
, Atom selection
, Atom target
, void *cdata
, Atom
* type
)
2055 Text
*tPtr
= view
->self
;
2056 Display
*dpy
= tPtr
->view
->screen
->display
;
2058 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2059 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2060 WMData
*data
= NULL
;
2062 /* Parameter not used, but tell the compiler that it is ok */
2066 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2067 char *text
= WMGetTextSelectedStream(tPtr
);
2070 data
= WMCreateDataWithBytes(text
, strlen(text
));
2071 WMSetDataFormat(data
, TYPETEXT
);
2076 printf("didn't get it\n");
2078 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2079 if (target
== _TARGETS
) {
2082 ptr
= wmalloc(4 * sizeof(Atom
));
2086 ptr
[3] = COMPOUND_TEXT
;
2088 data
= WMCreateDataWithBytes(ptr
, 4 * 4);
2089 WMSetDataFormat(data
, 32);
2098 static void lostHandler(WMView
* view
, Atom selection
, void *cdata
)
2100 /* Parameter not used, but tell the compiler that it is ok */
2104 releaseSelection((WMText
*) view
->self
);
2107 static WMSelectionProcs selectionHandler
= {
2108 requestHandler
, lostHandler
, NULL
2111 static void ownershipObserver(void *observerData
, WMNotification
* notification
)
2113 if (observerData
!= WMGetNotificationClientData(notification
))
2114 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2117 static void autoSelectText(Text
* tPtr
, int clicks
)
2121 char *mark
= NULL
, behind
, ahead
;
2123 if (!(tb
= tPtr
->currentTextBlock
))
2128 switch (tb
->text
[tPtr
->tpos
]) {
2132 case '<': case '>': behind = '<'; ahead = '>'; break;
2133 case '{': case '}': behind = '{'; ahead = '}'; break;
2134 case '[': case ']': behind = '['; ahead = ']'; break;
2137 behind
= ahead
= ' ';
2140 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
2141 tPtr
->sel
.h
= 6; /*tPtr->cursor.h-10; */
2144 tPtr
->sel
.x
= tb
->sections
[0].x
;
2145 tPtr
->sel
.w
= tb
->sections
[0].w
;
2147 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
2150 while (start
> 0 && tb
->text
[start
- 1] != behind
)
2154 if (tPtr
->tpos
> start
) {
2155 x
-= WMWidthOfString(font
, &tb
->text
[start
], tPtr
->tpos
- start
);
2157 tPtr
->sel
.x
= (x
< 0 ? 0 : x
) + 1;
2159 if ((mark
= strchr(&tb
->text
[start
], ahead
))) {
2160 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2161 (int)(mark
- &tb
->text
[start
]));
2162 } else if (tb
->used
> start
) {
2163 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
], tb
->used
- start
);
2167 } else if (clicks
== 3) {
2168 TextBlock
*cur
= tb
;
2170 while (tb
&& !tb
->first
) {
2173 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2176 while (tb
->next
&& !tb
->next
->first
) {
2179 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
- 1]._y
+ 5 - tPtr
->sel
.y
;
2182 tPtr
->sel
.w
= tPtr
->docWidth
;
2183 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2186 if (!tPtr
->flags
.ownsSelection
) {
2187 WMCreateSelectionHandler(tPtr
->view
, XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2188 tPtr
->flags
.ownsSelection
= True
;
2195 static void fontChanged(void *observerData
, WMNotification
* notification
)
2197 WMText
*tPtr
= (WMText
*) observerData
;
2198 WMFont
*font
= (WMFont
*) WMGetNotificationClientData(notification
);
2199 printf("fontChanged\n");
2204 if (tPtr
->flags
.ownsSelection
)
2205 WMSetTextSelectionFont(tPtr
, font
);
2209 static void handleTextKeyPress(Text
* tPtr
, XEvent
* event
)
2213 int control_pressed
= False
;
2214 TextBlock
*tb
= NULL
;
2216 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2217 control_pressed
= True
;
2218 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2223 if ((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2225 updateCursorPosition(tPtr
);
2230 if ((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2231 if (tPtr
->currentTextBlock
->graphic
)
2234 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2236 updateCursorPosition(tPtr
);
2241 if (!(tb
= tPtr
->currentTextBlock
))
2246 if (tPtr
->tpos
== 0) {
2249 tPtr
->currentTextBlock
= tb
->prior
;
2250 if (tPtr
->currentTextBlock
->graphic
)
2253 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2255 if (!tb
->first
&& tPtr
->tpos
> 0)
2261 updateCursorPosition(tPtr
);
2266 if (!(tb
= tPtr
->currentTextBlock
))
2270 if (tPtr
->tpos
== tb
->used
) {
2273 tPtr
->currentTextBlock
= tb
->next
;
2275 if (!tb
->next
->first
&& tb
->next
->used
> 0)
2281 tPtr
->tpos
= tb
->used
;
2285 updateCursorPosition(tPtr
);
2290 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2291 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2296 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2297 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2306 deleteTextInteractively(tPtr
, ksym
);
2307 updateCursorPosition(tPtr
);
2313 control_pressed
= True
;
2317 insertTextInteractively(tPtr
, " ", 4);
2318 updateCursorPosition(tPtr
);
2325 if (*buffer
!= 0 && !control_pressed
) {
2326 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2327 updateCursorPosition(tPtr
);
2330 } else if (control_pressed
&& ksym
== XK_r
) {
2331 Bool i
= !tPtr
->flags
.rulerShown
;
2332 WMShowTextRuler(tPtr
, i
);
2333 tPtr
->flags
.rulerShown
= i
;
2334 } else if (control_pressed
&& *buffer
== '\a') {
2335 XBell(tPtr
->view
->screen
->display
, 0);
2337 WMRelayToNextResponder(tPtr
->view
, event
);
2341 if (!control_pressed
&& tPtr
->flags
.ownsSelection
) {
2342 releaseSelection(tPtr
);
2346 static void pasteText(WMView
* view
, Atom selection
, Atom target
, Time timestamp
, void *cdata
, WMData
* data
)
2348 Text
*tPtr
= (Text
*) view
->self
;
2351 /* Parameter not used, but tell the compiler that it is ok */
2357 tPtr
->flags
.waitingForSelection
= 0;
2360 text
= (char *)WMDataBytes(data
);
2363 (tPtr
->parser
) (tPtr
, (void *)text
);
2364 layOutDocument(tPtr
);
2366 insertTextInteractively(tPtr
, text
, strlen(text
));
2367 updateCursorPosition(tPtr
);
2373 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2378 (tPtr
->parser
) (tPtr
, (void *)text
);
2379 layOutDocument(tPtr
);
2381 insertTextInteractively(tPtr
, text
, n
);
2382 updateCursorPosition(tPtr
);
2391 static void handleActionEvents(XEvent
* event
, void *data
)
2393 Text
*tPtr
= (Text
*) data
;
2394 Display
*dpy
= event
->xany
.display
;
2397 switch (event
->type
) {
2399 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2400 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2401 tPtr
->flags
.extendSelection
= True
;
2405 if (tPtr
->flags
.focused
) {
2406 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2407 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
2408 GrabModeAsync
, GrabModeAsync
, None
,
2409 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2410 tPtr
->flags
.pointerGrabbed
= True
;
2411 handleTextKeyPress(tPtr
, event
);
2417 ksym
= XLookupKeysym((XKeyEvent
*) event
, 0);
2418 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2419 tPtr
->flags
.extendSelection
= False
;
2421 /* end modify flag so selection can be extended */
2427 if (tPtr
->flags
.pointerGrabbed
) {
2428 tPtr
->flags
.pointerGrabbed
= False
;
2429 XUngrabPointer(dpy
, CurrentTime
);
2432 if (tPtr
->flags
.waitingForSelection
)
2435 if ((event
->xmotion
.state
& Button1Mask
)) {
2437 if (WMIsDraggingFromView(tPtr
->view
)) {
2438 WMDragImageFromView(tPtr
->view
, event
);
2442 if (!tPtr
->flags
.ownsSelection
) {
2443 WMCreateSelectionHandler(tPtr
->view
,
2444 XA_PRIMARY
, event
->xbutton
.time
, &selectionHandler
, NULL
);
2445 tPtr
->flags
.ownsSelection
= True
;
2447 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2451 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2456 if (tPtr
->flags
.pointerGrabbed
) {
2457 tPtr
->flags
.pointerGrabbed
= False
;
2458 XUngrabPointer(dpy
, CurrentTime
);
2462 if (tPtr
->flags
.waitingForSelection
)
2465 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2466 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2470 if (tPtr
->flags
.ownsSelection
)
2471 releaseSelection(tPtr
);
2473 if (event
->xbutton
.button
== Button1
) {
2474 TextBlock
*tb
= tPtr
->currentTextBlock
;
2476 if (WMIsDoubleClick(event
)) {
2478 tPtr
->lastClickTime
= event
->xbutton
.time
;
2479 if (tb
&& tb
->graphic
&& !tb
->object
) {
2480 if (tPtr
->delegate
&& tPtr
->delegate
->didDoubleClickOnPicture
) {
2483 desc
= wmalloc(tb
->used
+ 1);
2484 memcpy(desc
, tb
->text
, tb
->used
);
2486 (*tPtr
->delegate
->didDoubleClickOnPicture
) (tPtr
->delegate
, desc
);
2490 autoSelectText(tPtr
, 2);
2493 } else if (event
->xbutton
.time
- tPtr
->lastClickTime
< WINGsConfiguration
.doubleClickDelay
) {
2494 tPtr
->lastClickTime
= event
->xbutton
.time
;
2495 autoSelectText(tPtr
, 3);
2499 if (!tPtr
->flags
.focused
) {
2500 WMSetFocusToWidget(tPtr
);
2501 tPtr
->flags
.focused
= True
;
2502 } else if (tb
&& tPtr
->flags
.isOverGraphic
&& tb
->graphic
&& !tb
->object
&& tb
->d
.pixmap
) {
2504 WMSetViewDragImage(tPtr
->view
, tb
->d
.pixmap
);
2505 WMDragImageFromView(tPtr
->view
, event
);
2509 tPtr
->lastClickTime
= event
->xbutton
.time
;
2510 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2514 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelDown
) {
2515 WMScrollText(tPtr
, 16);
2519 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
) {
2520 WMScrollText(tPtr
, -16);
2524 if (event
->xbutton
.button
== Button2
) {
2528 if (!tPtr
->flags
.editable
) {
2533 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2534 event
->xbutton
.time
, pasteText
, NULL
)) {
2536 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2537 tPtr
->flags
.waitingForSelection
= 0;
2543 (tPtr
->parser
) (tPtr
, (void *)text
);
2544 layOutDocument(tPtr
);
2546 insertTextInteractively(tPtr
, text
, n
);
2550 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2551 (void *)WMInsertTextEvent
);
2553 updateCursorPosition(tPtr
);
2557 tPtr
->flags
.waitingForSelection
= True
;
2564 if (tPtr
->flags
.pointerGrabbed
) {
2565 tPtr
->flags
.pointerGrabbed
= False
;
2566 XUngrabPointer(dpy
, CurrentTime
);
2570 if (tPtr
->flags
.waitingForSelection
)
2573 if (WMIsDraggingFromView(tPtr
->view
))
2574 WMDragImageFromView(tPtr
->view
, event
);
2579 static void handleEvents(XEvent
* event
, void *data
)
2581 Text
*tPtr
= (Text
*) data
;
2583 switch (event
->type
) {
2586 if (event
->xexpose
.count
!= 0)
2590 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2591 WMRealizeWidget(tPtr
->hS
);
2595 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2596 WMRealizeWidget(tPtr
->vS
);
2600 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2601 WMRealizeWidget(tPtr
->ruler
);
2606 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2612 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2615 tPtr
->flags
.focused
= True
;
2617 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2618 tPtr
->timerID
= WMAddTimerHandler(12 + 0 * CURSOR_BLINK_ON_DELAY
, blinkCursor
, tPtr
);
2625 tPtr
->flags
.focused
= False
;
2628 if (tPtr
->timerID
) {
2629 WMDeleteTimerHandler(tPtr
->timerID
);
2630 tPtr
->timerID
= NULL
;
2638 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2640 WMEmptyArray(tPtr
->gfxItems
);
2643 WMDeleteTimerHandler(tPtr
->timerID
);
2645 WMReleaseFont(tPtr
->dFont
);
2646 WMReleaseColor(tPtr
->dColor
);
2647 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2648 WMRemoveNotificationObserver(tPtr
);
2650 WMFreeArray(tPtr
->xdndSourceTypes
);
2651 WMFreeArray(tPtr
->xdndDestinationTypes
);
2660 static void insertPlainText(Text
* tPtr
, const char *text
)
2662 const char *start
, *mark
;
2667 mark
= strchr(start
, '\n');
2669 tb
= WMCreateTextBlockWithText(tPtr
,
2671 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
- start
));
2673 tPtr
->flags
.first
= True
;
2675 if (start
&& strlen(start
)) {
2676 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2677 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2680 tPtr
->flags
.first
= False
;
2684 if (tPtr
->flags
.prepend
)
2685 WMPrependTextBlock(tPtr
, tb
);
2687 WMAppendTextBlock(tPtr
, tb
);
2691 static void rulerMoveCallBack(WMWidget
* w
, void *self
)
2693 Text
*tPtr
= (Text
*) self
;
2695 /* Parameter not used, but tell the compiler that it is ok */
2700 if (W_CLASS(tPtr
) != WC_Text
)
2706 static void rulerReleaseCallBack(WMWidget
* w
, void *self
)
2708 Text
*tPtr
= (Text
*) self
;
2710 /* Parameter not used, but tell the compiler that it is ok */
2715 if (W_CLASS(tPtr
) != WC_Text
)
2722 static WMArray
*dropDataTypes(WMView
* self
)
2724 return ((Text
*) self
->self
)->xdndSourceTypes
;
2727 static WMDragOperationType
wantedDropOperation(WMView
* self
)
2729 /* Parameter not used, but tell the compiler that it is ok */
2732 return WDOperationCopy
;
2735 static Bool
acceptDropOperation(WMView
* self
, WMDragOperationType allowedOperation
)
2737 /* Parameter not used, but tell the compiler that it is ok */
2740 return (allowedOperation
== WDOperationCopy
);
2743 static WMData
*fetchDragData(WMView
* self
, char *type
)
2745 TextBlock
*tb
= ((WMText
*) self
->self
)->currentTextBlock
;
2749 if (strcmp(type
, "text/plain")) {
2753 desc
= wmalloc(tb
->used
+ 1);
2754 memcpy(desc
, tb
->text
, tb
->used
);
2756 data
= WMCreateDataWithBytes(desc
, strlen(desc
) + 1);
2766 static WMDragSourceProcs _DragSourceProcs
= {
2768 wantedDropOperation
,
2770 acceptDropOperation
,
2776 static WMArray
*requiredDataTypes(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2778 /* Parameter not used, but tell the compiler that it is ok */
2780 (void) sourceDataTypes
;
2782 return ((Text
*) self
->self
)->xdndDestinationTypes
;
2785 static WMDragOperationType
allowedOperation(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
2787 /* Parameter not used, but tell the compiler that it is ok */
2790 (void) sourceDataTypes
;
2792 return WDOperationCopy
;
2795 static void performDragOperation(WMView
* self
, WMArray
* dropData
, WMArray
* operations
, WMPoint
* dropLocation
)
2797 WMText
*tPtr
= (WMText
*) self
->self
;
2802 /* Parameter not used, but tell the compiler that it is ok */
2804 (void) dropLocation
;
2808 /* only one required type, implies only one drop data */
2810 /* get application/X-color if any */
2811 data
= (WMData
*) WMPopFromArray(dropData
);
2813 colorName
= (char *)WMDataBytes(data
);
2814 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
2817 WMSetTextSelectionColor(tPtr
, color
);
2818 WMReleaseColor(color
);
2824 static WMDragDestinationProcs _DragDestinationProcs
= {
2829 performDragOperation
,
2833 static char *getStream(WMText
* tPtr
, int sel
, int array
)
2835 TextBlock
*tb
= NULL
;
2837 unsigned long where
= 0;
2842 if (!(tb
= tPtr
->firstTextBlock
))
2846 (tPtr
->writer
) (tPtr
, (void *)text
);
2850 tb
= tPtr
->firstTextBlock
;
2853 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2855 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2857 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2858 && tb
!= tPtr
->firstTextBlock
) {
2859 text
= wrealloc(text
, where
+ 1);
2860 text
[where
++] = '\n';
2866 if (tb
->graphic
&& array
) {
2867 text
= wrealloc(text
, where
+ 4);
2868 text
[where
++] = 0xFA;
2869 text
[where
++] = (tb
->used
>> 8) & 0x0ff;
2870 text
[where
++] = tb
->used
& 0x0ff;
2871 text
[where
++] = tb
->allocated
; /* extra info */
2873 text
= wrealloc(text
, where
+ tb
->used
);
2874 memcpy(&text
[where
], tb
->text
, tb
->used
);
2877 } else if (sel
&& tb
->selected
) {
2879 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
2880 text
= wrealloc(text
, where
+ 1);
2881 text
[where
++] = '\n';
2887 text
= wrealloc(text
, where
+ (tb
->s_end
- tb
->s_begin
));
2888 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
], tb
->s_end
- tb
->s_begin
);
2889 where
+= tb
->s_end
- tb
->s_begin
;
2894 _gSnext
: tb
= tb
->next
;
2897 /* +1 for the end of string, let's be nice */
2898 text
= wrealloc(text
, where
+ 1);
2903 static void releaseStreamObjects(void *data
)
2909 static WMArray
*getStreamObjects(WMText
* tPtr
, int sel
)
2911 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
2915 char *start
, *fa
, *desc
;
2917 stream
= getStream(tPtr
, sel
, 1);
2924 fa
= strchr(start
, 0xFA);
2926 if ((int)(fa
- start
) > 0) {
2928 desc
[(int)(fa
- start
)] = 0;
2929 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
2930 WMSetDataFormat(data
, TYPETEXT
);
2931 WMAddToArray(array
, (void *)data
);
2934 len
= *(fa
+ 1) * 0xff + *(fa
+ 2);
2935 data
= WMCreateDataWithBytes((void *)(fa
+ 4), len
);
2936 WMSetDataFormat(data
, *(fa
+ 3));
2937 WMAddToArray(array
, (void *)data
);
2938 start
= fa
+ len
+ 4;
2941 if (start
&& strlen(start
)) {
2942 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
2943 WMSetDataFormat(data
, TYPETEXT
);
2944 WMAddToArray(array
, (void *)data
);
2954 #define XDND_TEXT_DATA_TYPE "text/plain"
2955 #define XDND_COLOR_DATA_TYPE "application/X-color"
2956 static WMArray
*getXdndSourceTypeArray(void)
2958 WMArray
*types
= WMCreateArray(1);
2959 WMAddToArray(types
, XDND_TEXT_DATA_TYPE
);
2963 static WMArray
*getXdndDestinationTypeArray(void)
2965 WMArray
*types
= WMCreateArray(1);
2966 WMAddToArray(types
, XDND_COLOR_DATA_TYPE
);
2970 WMText
*WMCreateTextForDocumentType(WMWidget
* parent
, WMAction
* parser
, WMAction
* writer
)
2977 tPtr
= wmalloc(sizeof(Text
));
2978 tPtr
->widgetClass
= WC_Text
;
2979 tPtr
->view
= W_CreateView(W_VIEW(parent
));
2981 perror("could not create text's view\n");
2986 dpy
= tPtr
->view
->screen
->display
;
2987 scr
= tPtr
->view
->screen
;
2989 tPtr
->view
->self
= tPtr
;
2990 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
2991 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
2992 W_ResizeView(tPtr
->view
, 250, 200);
2994 tPtr
->dColor
= WMBlackColor(scr
);
2995 tPtr
->fgColor
= WMBlackColor(scr
);
2996 tPtr
->bgColor
= WMWhiteColor(scr
);
2997 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
2999 gcv
.graphics_exposures
= False
;
3000 gcv
.foreground
= W_PIXEL(scr
->gray
);
3001 gcv
.background
= W_PIXEL(scr
->darkGray
);
3002 gcv
.fill_style
= FillStippled
;
3003 /* why not use scr->stipple here? */
3004 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
, STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
3005 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
3006 GCForeground
| GCBackground
| GCStipple
3007 | GCFillStyle
| GCGraphicsExposures
, &gcv
);
3013 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
3015 tPtr
->view
->delegate
= &_TextViewDelegate
;
3017 tPtr
->delegate
= NULL
;
3020 tPtr
->timerID
= NULL
;
3023 WMCreateEventHandler(tPtr
->view
, ExposureMask
| StructureNotifyMask
3024 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
, handleEvents
, tPtr
);
3026 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
| ButtonPressMask
3027 | KeyReleaseMask
| KeyPressMask
| Button1MotionMask
, handleActionEvents
, tPtr
);
3029 WMAddNotificationObserver(ownershipObserver
, tPtr
, WMSelectionOwnerDidChangeNotification
, tPtr
);
3031 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3032 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3035 WMArray
*types
= WMCreateArray(2);
3036 WMAddToArray(types
, "application/X-color");
3037 WMAddToArray(types
, "application/X-image");
3038 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3041 /*WMAddNotificationObserver(fontChanged, tPtr,
3042 WMFontPanelDidChangeNotification, tPtr); */
3044 tPtr
->firstTextBlock
= NULL
;
3045 tPtr
->lastTextBlock
= NULL
;
3046 tPtr
->currentTextBlock
= NULL
;
3049 tPtr
->gfxItems
= WMCreateArray(4);
3051 tPtr
->parser
= parser
;
3052 tPtr
->writer
= writer
;
3054 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3055 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3057 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3059 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3060 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3061 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3063 tPtr
->cursor
.x
= -23;
3066 tPtr
->docHeight
= 0;
3067 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
, default_bullet
);
3068 tPtr
->db
= (Pixmap
) NULL
;
3069 tPtr
->bgPixmap
= NULL
;
3071 tPtr
->margins
= WMGetRulerMargins(NULL
);
3072 tPtr
->margins
->right
= tPtr
->visible
.w
;
3075 tPtr
->flags
.rulerShown
= False
;
3076 tPtr
->flags
.monoFont
= False
;
3077 tPtr
->flags
.focused
= False
;
3078 tPtr
->flags
.editable
= True
;
3079 tPtr
->flags
.ownsSelection
= False
;
3080 tPtr
->flags
.pointerGrabbed
= False
;
3081 tPtr
->flags
.extendSelection
= False
;
3082 tPtr
->flags
.frozen
= False
;
3083 tPtr
->flags
.cursorShown
= True
;
3084 tPtr
->flags
.acceptsGraphic
= False
;
3085 tPtr
->flags
.horizOnDemand
= False
;
3086 tPtr
->flags
.needsLayOut
= False
;
3087 tPtr
->flags
.ignoreNewLine
= False
;
3088 tPtr
->flags
.indentNewLine
= False
;
3089 tPtr
->flags
.laidOut
= False
;
3090 tPtr
->flags
.ownsSelection
= False
;
3091 tPtr
->flags
.waitingForSelection
= False
;
3092 tPtr
->flags
.prepend
= False
;
3093 tPtr
->flags
.isOverGraphic
= False
;
3094 tPtr
->flags
.relief
= WRSunken
;
3095 tPtr
->flags
.isOverGraphic
= 0;
3096 tPtr
->flags
.alignment
= WALeft
;
3097 tPtr
->flags
.first
= True
;
3099 tPtr
->xdndSourceTypes
= getXdndSourceTypeArray();
3100 tPtr
->xdndDestinationTypes
= getXdndDestinationTypeArray();
3105 void WMPrependTextStream(WMText
* tPtr
, const char *text
)
3107 CHECK_CLASS(tPtr
, WC_Text
);
3110 if (tPtr
->flags
.ownsSelection
)
3111 releaseSelection(tPtr
);
3113 updateScrollers(tPtr
);
3117 tPtr
->flags
.prepend
= True
;
3118 if (text
&& tPtr
->parser
)
3119 (tPtr
->parser
) (tPtr
, (void *)text
);
3121 insertPlainText(tPtr
, text
);
3123 tPtr
->flags
.needsLayOut
= True
;
3125 if (!tPtr
->flags
.frozen
) {
3126 layOutDocument(tPtr
);
3130 void WMAppendTextStream(WMText
* tPtr
, const char *text
)
3132 CHECK_CLASS(tPtr
, WC_Text
);
3135 if (tPtr
->flags
.ownsSelection
)
3136 releaseSelection(tPtr
);
3138 updateScrollers(tPtr
);
3142 tPtr
->flags
.prepend
= False
;
3143 if (text
&& tPtr
->parser
)
3144 (tPtr
->parser
) (tPtr
, (void *)text
);
3146 insertPlainText(tPtr
, text
);
3148 tPtr
->flags
.needsLayOut
= True
;
3149 if (tPtr
->currentTextBlock
) {
3150 if (tPtr
->currentTextBlock
->graphic
)
3153 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3156 if (!tPtr
->flags
.frozen
) {
3157 layOutDocument(tPtr
);
3161 char *WMGetTextStream(WMText
* tPtr
)
3163 CHECK_CLASS(tPtr
, WC_Text
);
3165 return getStream(tPtr
, 0, 0);
3168 char *WMGetTextSelectedStream(WMText
* tPtr
)
3170 CHECK_CLASS(tPtr
, WC_Text
);
3172 return getStream(tPtr
, 1, 0);
3175 WMArray
*WMGetTextObjects(WMText
* tPtr
)
3177 CHECK_CLASS(tPtr
, WC_Text
);
3179 return getStreamObjects(tPtr
, 0);
3182 WMArray
*WMGetTextSelectedObjects(WMText
* tPtr
)
3184 CHECK_CLASS(tPtr
, WC_Text
);
3186 return getStreamObjects(tPtr
, 1);
3189 void WMSetTextDelegate(WMText
* tPtr
, WMTextDelegate
* delegate
)
3191 CHECK_CLASS(tPtr
, WC_Text
);
3193 tPtr
->delegate
= delegate
;
3196 void *WMCreateTextBlockWithObject(WMText
* tPtr
, WMWidget
* w
,
3197 const char *description
, WMColor
* color
,
3198 unsigned short first
, unsigned short extraInfo
)
3202 if (!w
|| !description
|| !color
)
3205 tb
= wmalloc(sizeof(TextBlock
));
3207 tb
->text
= wstrdup(description
);
3208 tb
->used
= strlen(description
);
3211 tb
->color
= WMRetainColor(color
);
3212 tb
->marginN
= newMargin(tPtr
, NULL
);
3213 tb
->allocated
= extraInfo
;
3218 tb
->underlined
= False
;
3219 tb
->selected
= False
;
3221 tb
->sections
= NULL
;
3229 void *WMCreateTextBlockWithPixmap(WMText
* tPtr
, WMPixmap
* p
,
3230 const char *description
, WMColor
* color
,
3231 unsigned short first
, unsigned short extraInfo
)
3235 if (!p
|| !description
|| !color
)
3238 tb
= wmalloc(sizeof(TextBlock
));
3240 tb
->text
= wstrdup(description
);
3241 tb
->used
= strlen(description
);
3243 tb
->d
.pixmap
= WMRetainPixmap(p
);
3244 tb
->color
= WMRetainColor(color
);
3245 tb
->marginN
= newMargin(tPtr
, NULL
);
3246 tb
->allocated
= extraInfo
;
3251 tb
->underlined
= False
;
3252 tb
->selected
= False
;
3254 tb
->sections
= NULL
;
3262 void *WMCreateTextBlockWithText(WMText
* tPtr
, const char *text
, WMFont
* font
, WMColor
* color
,
3263 unsigned short first
, unsigned short len
)
3267 if (!font
|| !color
)
3270 tb
= wmalloc(sizeof(TextBlock
));
3272 tb
->allocated
= reqBlockSize(len
);
3273 tb
->text
= (char *)wmalloc(tb
->allocated
);
3275 if (len
< 1 || !text
|| (*text
== '\n' && len
== 1)) {
3280 memcpy(tb
->text
, text
, len
);
3284 tb
->text
[tb
->used
] = 0;
3286 tb
->d
.font
= WMRetainFont(font
);
3287 tb
->color
= WMRetainColor(color
);
3288 tb
->marginN
= newMargin(tPtr
, NULL
);
3291 tb
->graphic
= False
;
3292 tb
->underlined
= False
;
3293 tb
->selected
= False
;
3295 tb
->sections
= NULL
;
3303 WMSetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int first
,
3304 unsigned int kanji
, unsigned int underlined
, int script
, WMRulerMargins
* margins
)
3306 TextBlock
*tb
= (TextBlock
*) vtb
;
3312 tb
->underlined
= underlined
;
3313 tb
->script
= script
;
3314 tb
->marginN
= newMargin(tPtr
, margins
);
3318 WMGetTextBlockProperties(WMText
* tPtr
, void *vtb
, unsigned int *first
,
3319 unsigned int *kanji
, unsigned int *underlined
, int *script
, WMRulerMargins
* margins
)
3321 TextBlock
*tb
= (TextBlock
*) vtb
;
3330 *underlined
= tb
->underlined
;
3332 *script
= tb
->script
;
3334 margins
= &tPtr
->margins
[tb
->marginN
];
3337 void WMPrependTextBlock(WMText
* tPtr
, void *vtb
)
3339 TextBlock
*tb
= (TextBlock
*) vtb
;
3346 WMWidget
*w
= tb
->d
.widget
;
3347 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3348 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3349 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3352 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3356 tPtr
->tpos
= tb
->used
;
3359 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3360 tb
->next
= tb
->prior
= NULL
;
3362 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3367 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3370 tb
->next
= tPtr
->currentTextBlock
;
3371 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3372 if (tPtr
->currentTextBlock
->prior
)
3373 tPtr
->currentTextBlock
->prior
->next
= tb
;
3375 tPtr
->currentTextBlock
->prior
= tb
;
3377 tPtr
->firstTextBlock
= tb
;
3379 tPtr
->currentTextBlock
= tb
;
3382 void WMAppendTextBlock(WMText
* tPtr
, void *vtb
)
3384 TextBlock
*tb
= (TextBlock
*) vtb
;
3391 WMWidget
*w
= tb
->d
.widget
;
3392 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3393 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3394 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3397 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3401 tPtr
->tpos
= tb
->used
;
3404 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3405 tb
->next
= tb
->prior
= NULL
;
3407 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
= tPtr
->currentTextBlock
= tb
;
3412 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3415 tb
->next
= tPtr
->currentTextBlock
->next
;
3416 tb
->prior
= tPtr
->currentTextBlock
;
3417 if (tPtr
->currentTextBlock
->next
)
3418 tPtr
->currentTextBlock
->next
->prior
= tb
;
3420 tPtr
->currentTextBlock
->next
= tb
;
3423 tPtr
->lastTextBlock
= tb
;
3425 tPtr
->currentTextBlock
= tb
;
3428 void *WMRemoveTextBlock(WMText
* tPtr
)
3430 TextBlock
*tb
= NULL
;
3432 if (!tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
|| !tPtr
->currentTextBlock
) {
3436 tb
= tPtr
->currentTextBlock
;
3438 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3441 WMUnmapWidget(tb
->d
.widget
);
3445 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3446 if (tPtr
->currentTextBlock
->next
)
3447 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3449 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3450 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3452 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3453 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3454 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3455 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3457 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3458 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3459 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3466 static void destroyWidget(WMWidget
* widget
)
3468 WMDestroyWidget(widget
);
3469 // -- never do this -- wfree(widget);
3473 void WMDestroyTextBlock(WMText
* tPtr
, void *vtb
)
3475 TextBlock
*tb
= (TextBlock
*) vtb
;
3477 /* Parameter not used, but tell the compiler that it is ok */
3485 /* naturally, there's a danger to destroying widgets whose action
3486 * brings us here: ie. press a button to destroy it...
3487 * need to find a safer way. till then... this stays commented out */
3488 /* 5 months later... destroy it 10 seconds after now which should
3489 * be enough time for the widget's action to be completed... :-) */
3490 /* This is a bad assumption. Just destroy the widget here.
3491 * if the caller needs it, it can protect it with W_RetainView()
3492 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3493 WMDestroyWidget(tb
->d
.widget
);
3495 WMReleasePixmap(tb
->d
.pixmap
);
3498 WMReleaseFont(tb
->d
.font
);
3501 WMReleaseColor(tb
->color
);
3502 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3504 wfree(tb
->sections
);
3509 void WMSetTextForegroundColor(WMText
* tPtr
, WMColor
* color
)
3512 WMReleaseColor(tPtr
->fgColor
);
3514 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3519 void WMSetTextBackgroundColor(WMText
* tPtr
, WMColor
* color
)
3522 WMReleaseColor(tPtr
->bgColor
);
3524 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3525 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3530 void WMSetTextBackgroundPixmap(WMText
* tPtr
, WMPixmap
* pixmap
)
3533 WMReleasePixmap(tPtr
->bgPixmap
);
3536 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3538 tPtr
->bgPixmap
= NULL
;
3541 void WMSetTextRelief(WMText
* tPtr
, WMReliefType relief
)
3543 tPtr
->flags
.relief
= relief
;
3544 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3547 void WMSetTextHasHorizontalScroller(WMText
* tPtr
, Bool shouldhave
)
3549 if (shouldhave
&& !tPtr
->hS
) {
3550 tPtr
->hS
= WMCreateScroller(tPtr
);
3551 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3552 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3553 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3554 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3555 WMMapWidget(tPtr
->hS
);
3556 } else if (!shouldhave
&& tPtr
->hS
) {
3557 WMUnmapWidget(tPtr
->hS
);
3558 WMDestroyWidget(tPtr
->hS
);
3564 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3567 void WMSetTextHasRuler(WMText
* tPtr
, Bool shouldhave
)
3569 if (shouldhave
&& !tPtr
->ruler
) {
3570 tPtr
->ruler
= WMCreateRuler(tPtr
);
3571 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3572 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3573 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3574 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3575 } else if (!shouldhave
&& tPtr
->ruler
) {
3576 WMShowTextRuler(tPtr
, False
);
3577 WMDestroyWidget(tPtr
->ruler
);
3580 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3583 void WMShowTextRuler(WMText
* tPtr
, Bool show
)
3588 if (tPtr
->flags
.monoFont
)
3591 tPtr
->flags
.rulerShown
= show
;
3593 WMMapWidget(tPtr
->ruler
);
3595 WMUnmapWidget(tPtr
->ruler
);
3598 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3601 Bool
WMGetTextRulerShown(WMText
* tPtr
)
3606 return tPtr
->flags
.rulerShown
;
3609 void WMSetTextHasVerticalScroller(WMText
* tPtr
, Bool shouldhave
)
3611 if (shouldhave
&& !tPtr
->vS
) {
3612 tPtr
->vS
= WMCreateScroller(tPtr
);
3613 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3614 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3615 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3616 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3617 WMMapWidget(tPtr
->vS
);
3618 } else if (!shouldhave
&& tPtr
->vS
) {
3619 WMUnmapWidget(tPtr
->vS
);
3620 WMDestroyWidget(tPtr
->vS
);
3626 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3629 Bool
WMScrollText(WMText
* tPtr
, int amount
)
3631 Bool scroll
= False
;
3633 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3637 if (tPtr
->vpos
> 0) {
3638 if (tPtr
->vpos
> abs(amount
))
3639 tPtr
->vpos
+= amount
;
3645 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3646 if (tPtr
->vpos
< limit
) {
3647 if (tPtr
->vpos
< limit
- amount
)
3648 tPtr
->vpos
+= amount
;
3655 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3656 updateScrollers(tPtr
);
3659 tPtr
->prevVpos
= tPtr
->vpos
;
3663 Bool
WMPageText(WMText
* tPtr
, Bool direction
)
3665 if (!tPtr
->view
->flags
.realized
)
3668 return WMScrollText(tPtr
, direction
? tPtr
->visible
.h
: -tPtr
->visible
.h
);
3671 void WMSetTextEditable(WMText
* tPtr
, Bool editable
)
3673 tPtr
->flags
.editable
= editable
;
3676 int WMGetTextEditable(WMText
* tPtr
)
3678 return tPtr
->flags
.editable
;
3681 void WMSetTextIndentNewLines(WMText
* tPtr
, Bool indent
)
3683 tPtr
->flags
.indentNewLine
= indent
;
3686 void WMSetTextIgnoresNewline(WMText
* tPtr
, Bool ignore
)
3688 tPtr
->flags
.ignoreNewLine
= ignore
;
3691 Bool
WMGetTextIgnoresNewline(WMText
* tPtr
)
3693 return tPtr
->flags
.ignoreNewLine
;
3696 void WMSetTextUsesMonoFont(WMText
* tPtr
, Bool mono
)
3699 if (tPtr
->flags
.rulerShown
)
3700 WMShowTextRuler(tPtr
, False
);
3701 if (tPtr
->flags
.alignment
!= WALeft
)
3702 tPtr
->flags
.alignment
= WALeft
;
3705 tPtr
->flags
.monoFont
= mono
;
3706 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3709 Bool
WMGetTextUsesMonoFont(WMText
* tPtr
)
3711 return tPtr
->flags
.monoFont
;
3714 void WMSetTextDefaultFont(WMText
* tPtr
, WMFont
* font
)
3717 WMReleaseFont(tPtr
->dFont
);
3720 tPtr
->dFont
= WMRetainFont(font
);
3722 tPtr
->dFont
= WMSystemFontOfSize(tPtr
->view
->screen
, 12);
3726 WMFont
*WMGetTextDefaultFont(WMText
* tPtr
)
3728 return WMRetainFont(tPtr
->dFont
);
3731 void WMSetTextDefaultColor(WMText
* tPtr
, WMColor
* color
)
3734 WMReleaseColor(tPtr
->dColor
);
3737 tPtr
->dColor
= WMRetainColor(color
);
3739 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3743 WMColor
*WMGetTextDefaultColor(WMText
* tPtr
)
3745 return tPtr
->dColor
;
3748 void WMSetTextAlignment(WMText
* tPtr
, WMAlignment alignment
)
3750 if (tPtr
->flags
.monoFont
)
3751 tPtr
->flags
.alignment
= WALeft
;
3753 tPtr
->flags
.alignment
= alignment
;
3757 int WMGetTextInsertType(WMText
* tPtr
)
3759 return tPtr
->flags
.prepend
;
3762 void WMSetTextSelectionColor(WMText
* tPtr
, WMColor
* color
)
3764 setSelectionProperty(tPtr
, NULL
, color
, -1);
3767 WMColor
*WMGetTextSelectionColor(WMText
* tPtr
)
3771 tb
= tPtr
->currentTextBlock
;
3773 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3782 void WMSetTextSelectionFont(WMText
* tPtr
, WMFont
* font
)
3784 setSelectionProperty(tPtr
, font
, NULL
, -1);
3787 WMFont
*WMGetTextSelectionFont(WMText
* tPtr
)
3791 tb
= tPtr
->currentTextBlock
;
3793 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3800 tb
= getFirstNonGraphicBlockFor(tb
, 1);
3804 return (tb
->selected
? tb
->d
.font
: NULL
);
3807 void WMSetTextSelectionUnderlined(WMText
* tPtr
, int underlined
)
3810 if (underlined
!= 0 && underlined
!= 1)
3813 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
3816 int WMGetTextSelectionUnderlined(WMText
* tPtr
)
3820 tb
= tPtr
->currentTextBlock
;
3822 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3828 return tb
->underlined
;
3831 void WMFreezeText(WMText
* tPtr
)
3833 tPtr
->flags
.frozen
= True
;
3836 void WMThawText(WMText
* tPtr
)
3838 tPtr
->flags
.frozen
= False
;
3840 if (tPtr
->flags
.monoFont
) {
3841 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
3844 /* make sure to unmap widgets no matter where they are */
3845 /* they'll be later remapped if needed by paintText */
3846 for (j
= 0; j
< c
; j
++) {
3847 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
3848 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
3849 WMUnmapWidget(tb
->d
.widget
);
3854 tPtr
->flags
.laidOut
= False
;
3855 layOutDocument(tPtr
);
3856 updateScrollers(tPtr
);
3858 tPtr
->flags
.needsLayOut
= False
;
3862 /* find first occurence of a string */
3863 static const char *mystrstr(const char *haystack
, const char *needle
, unsigned short len
, const char *end
, Bool caseSensitive
)
3867 if (!haystack
|| !needle
|| !end
)
3870 for (ptr
= haystack
; ptr
< end
; ptr
++) {
3871 if (caseSensitive
) {
3872 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3876 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3884 /* find last occurence of a string */
3885 static const char *mystrrstr(const char *haystack
, const char *needle
, unsigned short len
, const char *end
, Bool caseSensitive
)
3889 if (!haystack
|| !needle
|| !end
)
3892 for (ptr
= haystack
- 2; ptr
> end
; ptr
--) {
3893 if (caseSensitive
) {
3894 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3897 if (tolower(*ptr
) == tolower(*needle
) && !strncasecmp(ptr
, needle
, len
))
3905 Bool
WMFindInTextStream(WMText
* tPtr
, const char *needle
, Bool direction
, Bool caseSensitive
)
3908 const char *mark
= NULL
;
3912 if (!(tb
= tPtr
->currentTextBlock
)) {
3913 if (!(tb
= ((direction
> 0) ? tPtr
->firstTextBlock
: tPtr
->lastTextBlock
))) {
3917 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3918 tb = (direction>0) ? tb->next : tb->prior; */
3919 if (tb
!= tPtr
->lastTextBlock
)
3923 tb
= tPtr
->currentTextBlock
;
3929 if (direction
> 0) {
3930 if (pos
+ 1 < tb
->used
)
3933 if (tb
->used
- pos
> 0 && pos
> 0) {
3934 mark
= mystrstr(&tb
->text
[pos
], needle
,
3935 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
3948 mark
= mystrrstr(&tb
->text
[pos
], needle
,
3949 strlen(needle
), tb
->text
, caseSensitive
);
3960 WMFont
*font
= tPtr
->flags
.monoFont
? tPtr
->dFont
: tb
->d
.font
;
3962 tPtr
->tpos
= (int)(mark
- tb
->text
);
3963 tPtr
->currentTextBlock
= tb
;
3964 updateCursorPosition(tPtr
);
3965 tPtr
->sel
.y
= tPtr
->cursor
.y
+ 5;
3966 tPtr
->sel
.h
= tPtr
->cursor
.h
- 10;
3967 tPtr
->sel
.x
= tPtr
->cursor
.x
+ 1;
3968 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
3969 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
3970 tPtr
->docWidth
- tPtr
->sel
.x
);
3971 tPtr
->flags
.ownsSelection
= True
;
3978 tb
= (direction
> 0) ? tb
->next
: tb
->prior
;
3980 pos
= (direction
> 0) ? 0 : tb
->used
;
3987 Bool
WMReplaceTextSelection(WMText
* tPtr
, char *replacement
)
3989 if (!tPtr
->flags
.ownsSelection
)
3992 removeSelection(tPtr
);
3995 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
3996 updateCursorPosition(tPtr
);