2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
7 #include <X11/keysym.h>
13 * - verify what happens with XK_return in insertTextInt...
14 * - selection code... selects can be funny if it crosses over. use rect?
15 * - also inspect behaviour for WACenter and WARight
16 * - what if a widget grabs the click... howto say: "pressed me"?
17 * note that WMCreateEventHandler takes one data, but need widget & tPtr
18 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
19 * - check if support for Horizontal Scroll is complete
20 * - Tabs now are simply replaced by 4 spaces...
21 * - redo blink code to reduce paint event... use pixmap buffer...
22 * - add paragraph support (full) and '\n' code in getStream..
26 /* a Section is a section of a TextBlock that describes what parts
27 of a TextBlock has been laid out on which "line"...
28 o this greatly aids redraw, scroll and selection.
29 o this is created during layoutLine, but may be later modified.
30 o there may be many Sections per TextBlock, hence the array */
32 unsigned int x
, y
; /* where to draw it from */
33 unsigned short w
, h
; /* its width and height */
34 unsigned short begin
; /* where the layout begins */
35 unsigned short end
; /* where it ends */
36 unsigned short max_d
; /* a quick hack for layOut if(laidOut) */
37 unsigned short last
:1; /* is it the last section on a "line"? */
38 unsigned int _y
:31; /* the "line" it and other textblocks are on */
42 /* a TextBlock is a node in a doubly-linked list of TextBlocks containing:
43 o text for the block, color and font
44 o or a pointer to the pixmap
45 o OR a pointer to the widget and the (text) description for its graphic
48 typedef struct _TextBlock
{
49 struct _TextBlock
*next
; /* next text block in linked list */
50 struct _TextBlock
*prior
; /* prior text block in linked list */
52 char *text
; /* pointer to text (could be kanji) */
53 /* or to the object's description */
55 WMFont
*font
; /* the font */
56 WMWidget
*widget
; /* the embedded widget */
57 WMPixmap
*pixmap
; /* the pixmap */
58 } d
; /* description */
60 unsigned short used
; /* number of chars in this block */
61 unsigned short allocated
; /* size of allocation (in chars) */
62 WMColor
*color
; /* the color */
64 Section
*sections
; /* the region for layouts (a growable array) */
65 /* an _array_! of size _nsections_ */
67 unsigned short s_begin
; /* where the selection begins */
68 unsigned short s_end
; /* where it ends */
70 unsigned int first
:1; /* first TextBlock in paragraph */
71 unsigned int blank
:1; /* ie. blank paragraph */
72 unsigned int kanji
:1; /* is of 16-bit characters or not */
73 unsigned int graphic
:1; /* graphic or text: text=0 */
74 unsigned int object
:1; /* embedded object or pixmap */
75 unsigned int underlined
:1; /* underlined or not */
76 unsigned int selected
:1; /* selected or not */
77 unsigned int nsections
:8; /* over how many "lines" a TextBlock wraps */
78 int script
:8; /* script in points: negative for subscript */
79 unsigned int marginN
:8; /* which of the margins in the tPtr to use */
80 unsigned int nClicks
:2; /* single, double, triple clicks */
81 unsigned int RESERVED
:7;
85 /* I'm lazy: visible.h vs. visible.size.height :-) */
91 typedef struct W_Text
{
92 W_Class widgetClass
; /* the class number of this widget */
93 W_View
*view
; /* the view referring to this instance */
95 WMRuler
*ruler
; /* the ruler widget to manipulate paragraphs */
97 WMScroller
*vS
; /* the vertical scroller */
98 unsigned int vpos
; /* the current vertical position */
99 unsigned int prevVpos
; /* the previous vertical position */
101 WMScroller
*hS
; /* the horizontal scroller */
102 unsigned int hpos
; /* the current horizontal position */
103 unsigned int prevHpos
; /* the previous horizontal position */
105 WMFont
*dFont
; /* the default font */
106 WMColor
*dColor
; /* the default color */
107 WMPixmap
*dBulletPix
; /* the default pixmap for bullets */
109 WMColor
*fgColor
; /* The current foreground color */
110 WMColor
*bgColor
; /* The background color */
112 GC stippledGC
; /* the GC to overlay selected graphics with */
113 Pixmap db
; /* the buffer on which to draw */
114 WMPixmap
*bgPixmap
; /* the background pixmap */
116 myRect visible
; /* the actual rectangle that can be drawn into */
117 myRect cursor
; /* the position and (height) of cursor */
118 myRect sel
; /* the selection rectangle */
120 WMPoint clicked
; /* where in the _document_ was clicked */
122 unsigned short tpos
; /* the position in the currentTextBlock */
123 unsigned short docWidth
; /* the width of the entire document */
124 unsigned int docHeight
; /* the height of the entire document */
126 TextBlock
*firstTextBlock
;
127 TextBlock
*lastTextBlock
;
128 TextBlock
*currentTextBlock
;
130 WMArray
*gfxItems
; /* a nice array of graphic items */
133 WMHandlerID timerID
; /* for nice twinky-winky */
138 WMTextDelegate
*delegate
;
141 WMRulerMargins
*margins
; /* an array of margins */
143 unsigned int nMargins
:7; /* the total number of margins in use */
145 unsigned int monoFont
:1; /* whether to ignore formats and graphic */
146 unsigned int focused
:1; /* whether this instance has input focus */
147 unsigned int editable
:1; /* "silly user, you can't edit me" */
148 unsigned int ownsSelection
:1; /* "I ownz the current selection!" */
149 unsigned int pointerGrabbed
:1;/* "heh, gib me pointer" */
150 unsigned int extendSelection
:1; /* shift-drag to select more regions */
152 unsigned int rulerShown
:1; /* whether the ruler is shown or not */
153 unsigned int frozen
:1; /* whether screen updates are to be made */
154 unsigned int cursorShown
:1; /* whether to show the cursor */
155 unsigned int acceptsGraphic
:1;/* accept graphic when dropped */
156 unsigned int horizOnDemand
:1;/* if a large image should appear*/
157 unsigned int needsLayOut
:1; /* in case of Append/Deletes */
158 unsigned int ignoreNewLine
:1;/* turn it into a ' ' in streams > 1 */
159 unsigned int indentNewLine
:1;/* add " " for a newline typed */
160 unsigned int laidOut
:1; /* have the TextBlocks all been laid out */
161 unsigned int waitingForSelection
:1; /* I don't wanna wait in vain... */
162 unsigned int prepend
:1; /* prepend=1, append=0 (for parsers) */
163 WMAlignment alignment
:2; /* the alignment for text */
164 WMReliefType relief
:3; /* the relief to display with */
165 unsigned int isOverGraphic
:2;/* the mouse is over a graphic */
166 unsigned int first
:1; /* for plain text parsing, newline? */
167 /* unsigned int RESERVED:1; */
172 #define NOTIFY(T,C,N,A) {\
173 WMNotification *notif = WMCreateNotification(N,T,A);\
174 if ((T)->delegate && (T)->delegate->C)\
175 (*(T)->delegate->C)((T)->delegate,notif);\
176 WMPostNotification(notif);\
177 WMReleaseNotification(notif);}
183 /* just to print blocks of text not terminated by \0 */
185 output(char *ptr
, int len
)
192 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
200 #define CURSOR_BLINK_ON_DELAY 600
201 #define CURSOR_BLINK_OFF_DELAY 400
205 #define STIPPLE_WIDTH 8
206 #define STIPPLE_HEIGHT 8
207 static unsigned char STIPPLE_BITS
[] = {
208 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
213 static char *default_bullet
[] = {
215 " c None s None", ". c black",
216 "X c white", "o c #808080",
225 static void handleEvents(XEvent
*event
, void *data
);
226 static void layOutDocument(Text
*tPtr
);
227 static void updateScrollers(Text
*tPtr
);
231 getMarginNumber(Text
*tPtr
, WMRulerMargins
*margins
)
235 for(i
=0; i
< tPtr
->nMargins
; i
++) {
237 if(WMIsMarginEqualToMargin(&tPtr
->margins
[i
], margins
))
247 newMargin(Text
*tPtr
, WMRulerMargins
*margins
)
252 tPtr
->margins
[0].retainCount
++;
256 n
= getMarginNumber(tPtr
, margins
);
260 if(tPtr
->nMargins
>= 127) {
261 n
= tPtr
->nMargins
-1;
265 tPtr
->margins
= wrealloc(tPtr
->margins
,
266 (++tPtr
->nMargins
)*sizeof(WMRulerMargins
));
268 n
= tPtr
->nMargins
-1;
269 tPtr
->margins
[n
].left
= margins
->left
;
270 tPtr
->margins
[n
].first
= margins
->first
;
271 tPtr
->margins
[n
].body
= margins
->body
;
272 tPtr
->margins
[n
].right
= margins
->right
;
273 /* for each tab... */
274 tPtr
->margins
[n
].retainCount
= 1;
276 tPtr
->margins
[n
].retainCount
++;
283 sectionWasSelected(Text
*tPtr
, TextBlock
*tb
, XRectangle
*rect
, int s
)
285 unsigned short i
, w
, lw
, selected
= False
, extend
= False
;
289 /* if selection rectangle completely encloses the section */
290 if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
291 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
292 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
294 sel
.w
= tPtr
->visible
.w
;
295 selected
= extend
= True
;
297 /* or if it starts on a line and then goes further down */
298 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
299 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
300 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
301 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
302 >= tPtr
->visible
.y
+ tPtr
->sel
.y
) ) {
303 sel
.x
= WMAX(tPtr
->sel
.x
, tPtr
->clicked
.x
);
304 sel
.w
= tPtr
->visible
.w
;
305 selected
= extend
= True
;
307 /* or if it begins before a line, but ends on it */
308 } else if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
309 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
310 >= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
311 && (tb
->sections
[s
]._y
312 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
314 if (1||tPtr
->sel
.x
+ tPtr
->sel
.w
> tPtr
->clicked
.x
)
315 sel
.w
= tPtr
->sel
.x
+ tPtr
->sel
.w
;
322 /* or if the selection rectangle lies entirely within a line */
323 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
324 && (tPtr
->sel
.w
>= 2)
325 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
326 >= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
335 /* if not within (modified) selection rectangle */
336 if ( tb
->sections
[s
].x
> sel
.x
+ sel
.w
337 || tb
->sections
[s
].x
+ tb
->sections
[s
].w
< sel
.x
)
341 if ( tb
->sections
[s
].x
+ tb
->sections
[s
].w
<= sel
.x
+ sel
.w
342 && tb
->sections
[s
].x
>= sel
.x
) {
343 rect
->width
= tb
->sections
[s
].w
;
344 rect
->x
= tb
->sections
[s
].x
;
349 i
= tb
->sections
[s
].begin
;
352 if (0&& tb
->sections
[s
].x
>= sel
.x
) {
353 tb
->s_begin
= tb
->sections
[s
].begin
;
357 while (++i
<= tb
->sections
[s
].end
) {
359 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
-1]), 1);
362 if (lw
+ tb
->sections
[s
].x
>= sel
.x
363 || i
== tb
->sections
[s
].end
) {
366 tb
->s_begin
= (tb
->selected
? WMIN(tb
->s_begin
, i
) : i
);
371 if (i
> tb
->sections
[s
].end
) {
372 printf("WasSelected: (i > tb->sections[s].end) \n");
376 _selEnd
: rect
->x
= tb
->sections
[s
].x
+ lw
;
378 while(++i
<= tb
->sections
[s
].end
) {
380 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
-1]), 1);
383 if (lw
+ rect
->x
>= sel
.x
+ sel
.w
384 || i
== tb
->sections
[s
].end
) {
386 if (i
!= tb
->sections
[s
].end
) {
392 if (tb
->sections
[s
].last
&& sel
.x
+ sel
.w
393 >= tb
->sections
[s
].x
+ tb
->sections
[s
].w
395 rect
->width
+= (tPtr
->visible
.w
- rect
->x
- lw
);
398 tb
->s_end
= (tb
->selected
? WMAX(tb
->s_end
, i
) : i
);
407 rect
->y
= tb
->sections
[s
]._y
- tPtr
->vpos
;
408 rect
->height
= tb
->sections
[s
].h
;
409 if(tb
->graphic
) { printf("DEBUG: graphic s%d h%d\n", s
,tb
->sections
[s
].h
);}
416 setSelectionProperty(WMText
*tPtr
, WMFont
*font
, WMColor
*color
, int underlined
)
421 tb
= tPtr
->firstTextBlock
;
422 if (!tb
|| !tPtr
->flags
.ownsSelection
)
425 if (font
&& (!color
|| underlined
==-1))
429 if (tPtr
->flags
.monoFont
|| tb
->selected
) {
431 if (tPtr
->flags
.monoFont
|| (tb
->s_end
- tb
->s_begin
== tb
->used
)
436 WMReleaseFont(tb
->d
.font
);
437 tb
->d
.font
= WMRetainFont(font
);
439 } else if(underlined
!=-1) {
440 tb
->underlined
= underlined
;
442 WMReleaseColor(tb
->color
);
443 tb
->color
= WMRetainColor(color
);
446 } else if (tb
->s_end
<= tb
->used
&& tb
->s_begin
< tb
->s_end
) {
448 TextBlock
*midtb
, *otb
= tb
;
450 if(underlined
!= -1) {
451 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
452 &(tb
->text
[tb
->s_begin
]), tb
->d
.font
, tb
->color
,
453 False
, (tb
->s_end
- tb
->s_begin
));
455 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
456 &(tb
->text
[tb
->s_begin
]),
457 (isFont
?font
:tb
->d
.font
),
458 (isFont
?tb
->color
:color
),
459 False
, (tb
->s_end
- tb
->s_begin
));
464 if(underlined
!= -1) {
465 midtb
->underlined
= underlined
;
467 midtb
->underlined
= otb
->underlined
;
470 midtb
->selected
= !True
;
472 midtb
->s_end
= midtb
->used
;
473 tPtr
->currentTextBlock
= tb
;
474 WMAppendTextBlock(tPtr
, midtb
);
475 tb
= tPtr
->currentTextBlock
;
478 if (otb
->used
- otb
->s_end
> 0) {
481 WMCreateTextBlockWithText(tPtr
,
482 &(otb
->text
[otb
->s_end
]), otb
->d
.font
, otb
->color
,
483 False
, otb
->used
- otb
->s_end
);
486 ntb
->underlined
= otb
->underlined
;
487 ntb
->selected
= False
;
488 WMAppendTextBlock(tPtr
, ntb
);
489 tb
= tPtr
->currentTextBlock
;
494 tPtr
->currentTextBlock
= midtb
;
497 otb
->selected
= False
;
498 otb
->used
= otb
->s_begin
;
505 tPtr
->flags
.needsLayOut
= True
;
508 /* in case the size changed... */
509 if(isFont
&& tPtr
->currentTextBlock
) {
510 TextBlock
*tb
= tPtr
->currentTextBlock
;
512 printf("%d %d %d\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
513 tPtr
->sel
.y
= 3 + tb
->sections
[0]._y
;
514 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
- tb
->sections
[0]._y
;
515 tPtr
->sel
.w
= tb
->sections
[tb
->nsections
-1].w
;
516 if(tb
->sections
[tb
->nsections
-1]._y
!= tb
->sections
[0]._y
) {
519 printf("%d %d %d\n\n\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
526 removeSelection(Text
*tPtr
)
528 TextBlock
*tb
= NULL
;
531 if (!(tb
= tPtr
->firstTextBlock
))
536 if(!first
&& !tb
->graphic
) {
537 WMReleaseFont(tPtr
->dFont
);
538 tPtr
->dFont
= WMRetainFont(tb
->d
.font
);
542 if ( (tb
->s_end
- tb
->s_begin
== tb
->used
) || tb
->graphic
) {
543 tPtr
->currentTextBlock
= tb
;
546 } else if(tb
->prior
) {
547 if(tb
->prior
->graphic
)
550 tPtr
->tpos
= tb
->prior
->used
;
551 } else tPtr
->tpos
= 0;
553 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
554 tb
= tPtr
->currentTextBlock
;
557 } else if (tb
->s_end
<= tb
->used
) {
558 memmove(&(tb
->text
[tb
->s_begin
]),
559 &(tb
->text
[tb
->s_end
]), tb
->used
- tb
->s_end
);
560 tb
->used
-= (tb
->s_end
- tb
->s_begin
);
561 tb
->selected
= False
;
562 tPtr
->tpos
= tb
->s_begin
;
574 getFirstNonGraphicBlockFor(TextBlock
*tb
, short dir
)
576 TextBlock
*hold
= tb
;
584 tb
= (dir
? tb
->next
: tb
->prior
);
592 tb
= (dir
? tb
->prior
: tb
->next
);
604 updateStartForCurrentTextBlock(Text
*tPtr
, int x
, int y
, int *dir
,
607 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
608 tb
= getFirstNonGraphicBlockFor(tb
, *dir
);
613 tPtr
->currentTextBlock
=
614 (dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
622 layOutDocument(tPtr
);
626 *dir
= !(y
<= tb
->sections
[0].y
);
628 if ( ( y
<= tb
->sections
[0]._y
+ tb
->sections
[0].h
)
629 && (y
>= tb
->sections
[0]._y
) ) {
630 /* if it's on the same line */
631 if(x
< tb
->sections
[0].x
)
635 if ( ( y
<= tb
->sections
[tb
->nsections
-1]._y
636 + tb
->sections
[tb
->nsections
-1].h
)
637 && (y
>= tb
->sections
[tb
->nsections
-1]._y
) ) {
638 /* if it's on the same line */
639 if(x
> tb
->sections
[tb
->nsections
-1].x
)
649 paintText(Text
*tPtr
)
654 int len
, y
, c
, s
, done
=False
, prev_y
=-23, dir
/* 1 = down */;
655 WMScreen
*scr
= tPtr
->view
->screen
;
656 Display
*dpy
= tPtr
->view
->screen
->display
;
657 Window win
= tPtr
->view
->window
;
660 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
664 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(tPtr
->bgColor
), 0, 0,
665 tPtr
->visible
.w
, tPtr
->visible
.h
);
667 if (tPtr
->bgPixmap
) {
668 WMDrawPixmap(tPtr
->bgPixmap
, tPtr
->db
,
669 (tPtr
->visible
.w
-tPtr
->visible
.x
-tPtr
->bgPixmap
->width
)/2,
670 (tPtr
->visible
.h
-tPtr
->visible
.y
-tPtr
->bgPixmap
->height
)/2);
673 if (! (tb
= tPtr
->currentTextBlock
)) {
674 if (! (tb
= tPtr
->firstTextBlock
)) {
683 /* first, which direction? Don't waste time looking all over,
684 since the parts to be drawn will most likely be near what
685 was previously drawn */
686 if(!updateStartForCurrentTextBlock(tPtr
, 0, tPtr
->vpos
, &dir
, tb
))
691 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
695 if(tPtr
->vpos
<= tb
->sections
[tb
->nsections
-1]._y
696 + tb
->sections
[tb
->nsections
-1].h
)
699 if(tPtr
->vpos
>= tb
->sections
[tb
->nsections
-1]._y
700 + tb
->sections
[tb
->nsections
-1].h
)
717 /* first, place all text that can be viewed */
718 while (!done
&& tb
) {
724 tb
->selected
= False
;
726 for(s
=0; s
<tb
->nsections
&& !done
; s
++) {
728 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
733 if ( tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
736 if (tPtr
->flags
.monoFont
) {
738 color
= tPtr
->fgColor
;
744 if (tPtr
->flags
.ownsSelection
) {
747 if (sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
749 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(scr
->gray
),
750 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
754 prev_y
= tb
->sections
[s
]._y
;
756 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
757 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
758 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
759 WMDrawString(scr
, tPtr
->db
, color
, font
,
760 tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
762 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
763 XDrawLine(dpy
, tPtr
->db
, WMColorGC(color
),
764 tb
->sections
[s
].x
- tPtr
->hpos
,
766 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
,
770 tb
= (!done
? tb
->next
: NULL
);
773 /* now , show all graphic items that can be viewed */
774 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
775 if (c
> 0 && !tPtr
->flags
.monoFont
) {
779 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
781 /* if it's not viewable, and mapped, unmap it */
782 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
783 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
786 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
787 WMUnmapWidget(tb
->d
.widget
);
791 /* if it's viewable, and not mapped, map it */
793 W_View
*view
= W_VIEW(tb
->d
.widget
);
795 if (!view
->flags
.realized
)
796 WMRealizeWidget(tb
->d
.widget
);
797 if(!view
->flags
.mapped
) {
798 XMapWindow(view
->screen
->display
, view
->window
);
799 XFlush(view
->screen
->display
);
800 view
->flags
.mapped
= 1;
805 WMMoveWidget(tb
->d
.widget
,
806 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
807 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
808 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
811 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
812 tb
->sections
[0].x
- tPtr
->hpos
,
813 tb
->sections
[0].y
- tPtr
->vpos
);
814 h
= tb
->d
.pixmap
->height
+ 1;
818 if (tPtr
->flags
.ownsSelection
) {
821 if ( sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
822 Drawable d
= (0&&tb
->object
?
823 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
826 XFillRectangle(dpy
, d
, tPtr
->stippledGC
,
827 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,*/
828 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
832 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
833 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
834 tb
->sections
[0].x
- tPtr
->hpos
,
835 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
836 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
837 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
845 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
846 && tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
847 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
848 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tPtr
->fgColor
),
850 tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
853 XCopyArea(dpy
, tPtr
->db
, win
, WMColorGC(tPtr
->bgColor
), 0, 0,
854 tPtr
->visible
.w
, tPtr
->visible
.h
,
855 tPtr
->visible
.x
, tPtr
->visible
.y
);
857 W_DrawRelief(scr
, win
, 0, 0,
858 tPtr
->view
->size
.width
, tPtr
->view
->size
.height
,
861 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
862 XDrawLine(dpy
, win
, WMColorGC(tPtr
->fgColor
),
863 2, 42, tPtr
->view
->size
.width
-4, 42);
868 mouseOverObject(Text
*tPtr
, int x
, int y
)
873 x
-= tPtr
->visible
.x
;
875 y
-= tPtr
->visible
.y
;
878 if(tPtr
->flags
.ownsSelection
) {
881 && tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
882 && tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
883 tPtr
->flags
.isOverGraphic
= 1;
890 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
893 tPtr
->flags
.isOverGraphic
= 0;
897 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
899 if(!tb
|| !tb
->sections
) {
900 tPtr
->flags
.isOverGraphic
= 0;
905 if(tb
->sections
[0].x
<= x
906 && tb
->sections
[0].y
<= y
907 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
908 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
909 tPtr
->flags
.isOverGraphic
= 3;
920 tPtr
->flags
.isOverGraphic
= 0;
923 tPtr
->view
->attribs
.cursor
= (result
?
924 tPtr
->view
->screen
->defaultCursor
925 : tPtr
->view
->screen
->textCursor
);
927 XSetWindowAttributes attribs
;
928 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
929 XChangeWindowAttributes(tPtr
->view
->screen
->display
,
930 tPtr
->view
->window
, CWCursor
,
938 blinkCursor(void *data
)
940 Text
*tPtr
= (Text
*)data
;
942 if (tPtr
->flags
.cursorShown
) {
943 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
,
946 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
,
950 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
955 updateCursorPosition(Text
*tPtr
)
957 TextBlock
*tb
= NULL
;
960 if(tPtr
->flags
.needsLayOut
)
961 layOutDocument(tPtr
);
963 if (! (tb
= tPtr
->currentTextBlock
)) {
964 if (! (tb
= tPtr
->firstTextBlock
)) {
965 WMFont
*font
= tPtr
->dFont
;
967 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
-font
->y
);
978 y
= tb
->sections
[0].y
;
979 h
= tb
->sections
[0].h
;
980 x
= tb
->sections
[0].x
;
982 } else if(tb
->graphic
) {
983 y
= tb
->sections
[0].y
;
984 h
= tb
->sections
[0].h
;
985 x
= tb
->sections
[0].x
;
987 x
+= tb
->sections
[0].w
;
990 if(tPtr
->tpos
> tb
->used
)
991 tPtr
->tpos
= tb
->used
;
993 for(s
=0; s
<tb
->nsections
-1; s
++) {
995 if(tPtr
->tpos
>= tb
->sections
[s
].begin
996 && tPtr
->tpos
<= tb
->sections
[s
].end
)
1000 y
= tb
->sections
[s
]._y
;
1001 h
= tb
->sections
[s
].h
;
1002 x
= tb
->sections
[s
].x
+ WMWidthOfString(
1003 (tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
),
1004 &tb
->text
[tb
->sections
[s
].begin
],
1005 tPtr
->tpos
- tb
->sections
[s
].begin
);
1013 /* scroll the bars if the cursor is not visible */
1014 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1015 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1016 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1018 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1019 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1020 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1021 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1026 updateScrollers(tPtr
);
1031 cursorToTextPosition(Text
*tPtr
, int x
, int y
)
1033 TextBlock
*tb
= NULL
;
1034 int done
=False
, s
, pos
, len
, _w
, _y
, dir
=1; /* 1 == "down" */
1037 if(tPtr
->flags
.needsLayOut
)
1038 layOutDocument(tPtr
);
1040 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
1044 x
-= (tPtr
->visible
.x
- 2);
1048 /* clicked is relative to document, not window... */
1049 tPtr
->clicked
.x
= x
;
1050 tPtr
->clicked
.y
= y
;
1052 if (! (tb
= tPtr
->currentTextBlock
)) {
1053 if (! (tb
= tPtr
->firstTextBlock
)) {
1054 WMFont
*font
= tPtr
->dFont
;
1056 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
-font
->y
);
1063 /* first, which direction? Most likely, newly clicked
1064 position will be close to previous */
1065 if(!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
1069 s
= (dir
? 0 : tb
->nsections
-1);
1070 if ( y
>= tb
->sections
[s
]._y
1071 && y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1075 /* get the first (or last) section of the TextBlock that
1076 lies about the vertical click point */
1078 while (!done
&& tb
) {
1080 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1081 if( (dir
?tb
->next
:tb
->prior
))
1082 tb
= (dir
?tb
->next
:tb
->prior
);
1086 s
= (dir
? 0 : tb
->nsections
-1);
1087 while (!done
&& (dir
? (s
<tb
->nsections
) : (s
>=0) )) {
1089 if ( (dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) :
1090 ( y
>= tb
->sections
[s
]._y
) ) ) {
1098 if ( (dir
? tb
->next
: tb
->prior
)) {
1099 tb
= (dir
? tb
->next
: tb
->prior
);
1102 break; /* goto _doneH; */
1108 if (s
<0 || s
>=tb
->nsections
) {
1109 s
= (dir
? tb
->nsections
-1 : 0);
1113 /* we have the line, which TextBlock on that line is it? */
1114 pos
= (dir
?0:tb
->sections
[s
].begin
);
1115 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1116 TextBlock
*hold
= tb
;
1117 tb
= getFirstNonGraphicBlockFor(hold
, dir
);
1131 _y
= tb
->sections
[s
]._y
;
1135 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1136 tb
= (dir
? tb
->next
: tb
->prior
);
1143 _w
= WMWidgetWidth(tb
->d
.widget
)-5;
1145 _w
= tb
->d
.pixmap
->width
-5;
1147 if (tb
->sections
[0].x
+ _w
>= x
)
1150 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1151 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1152 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1153 if (tb
->sections
[s
].x
+ _w
>= x
)
1158 if (tb
->sections
[s
].x
<= x
)
1162 if ((dir
? tb
->next
: tb
->prior
)) {
1163 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1164 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1165 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1167 pos
= (dir
?0:tb
->sections
[s
].begin
);
1168 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1173 if (_y
!= nxt
->sections
[dir
?0:nxt
->nsections
-1]._y
) {
1174 /* this must be the last/first on this line. stop */
1175 pos
= (dir
? tb
->sections
[s
].end
: 0);
1176 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1180 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1182 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1183 } else if (pos
> tb
->sections
[s
].begin
) {
1185 WMWidthOfString(tb
->d
.font
,
1186 &(tb
->text
[tb
->sections
[s
].begin
]),
1187 pos
- tb
->sections
[s
].begin
);
1194 if ( (dir
? tb
->next
: tb
->prior
)) {
1195 tb
= (dir
? tb
->next
: tb
->prior
);
1202 s
= (dir
? 0 : tb
->nsections
-1);
1205 /* we have said TextBlock, now where within it? */
1208 int gw
= (tb
->object
?
1209 WMWidgetWidth(tb
->d
.widget
) : tb
->d
.pixmap
->width
);
1211 tPtr
->cursor
.x
= tb
->sections
[0].x
;
1213 if(x
> tPtr
->cursor
.x
+ gw
/2) {
1215 tPtr
->cursor
.x
+= gw
;
1217 printf("first %d\n", tb
->first
);
1219 if(tb
->prior
->graphic
) pos
= 1;
1220 else pos
= tb
->prior
->used
;
1230 WMFont
*f
= tb
->d
.font
;
1231 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1232 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1234 _w
= x
- tb
->sections
[s
].x
;
1237 while (pos
<len
&& WMWidthOfString(f
, text
, pos
+1) < _w
)
1240 tPtr
->cursor
.x
= tb
->sections
[s
].x
+
1241 (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1243 pos
+= tb
->sections
[s
].begin
;
1249 tPtr
->tpos
= (pos
<=1)? pos
: 0;
1251 tPtr
->tpos
= (pos
<tb
->used
)? pos
: tb
->used
;
1255 printf("...for this app will surely crash :-)\n");
1257 tPtr
->currentTextBlock
= tb
;
1258 tPtr
->cursor
.h
= tb
->sections
[s
].h
;
1259 tPtr
->cursor
.y
= tb
->sections
[s
]._y
;
1261 /* scroll the bars if the cursor is not visible */
1262 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1263 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1264 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1266 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1267 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1268 updateScrollers(tPtr
);
1269 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1270 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1271 updateScrollers(tPtr
);
1280 updateScrollers(Text
*tPtr
)
1283 if (tPtr
->flags
.frozen
)
1287 if (tPtr
->docHeight
<= tPtr
->visible
.h
) {
1288 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
1291 float hmax
= (float)(tPtr
->docHeight
);
1292 WMSetScrollerParameters(tPtr
->vS
,
1293 ((float)tPtr
->vpos
)/(hmax
- (float)tPtr
->visible
.h
),
1294 (float)tPtr
->visible
.h
/hmax
);
1296 } else tPtr
->vpos
= 0;
1299 if (tPtr
->docWidth
<= tPtr
->visible
.w
) {
1300 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
1303 float wmax
= (float)(tPtr
->docWidth
);
1304 WMSetScrollerParameters(tPtr
->hS
,
1305 ((float)tPtr
->hpos
)/(wmax
- (float)tPtr
->visible
.w
),
1306 (float)tPtr
->visible
.w
/wmax
);
1308 } else tPtr
->hpos
= 0;
1312 scrollersCallBack(WMWidget
*w
, void *self
)
1314 Text
*tPtr
= (Text
*)self
;
1315 Bool scroll
= False
;
1318 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1321 if (w
== tPtr
->vS
) {
1323 height
= tPtr
->visible
.h
;
1325 which
= WMGetScrollerHitPart(tPtr
->vS
);
1328 case WSDecrementLine
:
1329 if (tPtr
->vpos
> 0) {
1330 if (tPtr
->vpos
>16) tPtr
->vpos
-=16;
1336 case WSIncrementLine
: {
1337 int limit
= tPtr
->docHeight
- height
;
1338 if (tPtr
->vpos
< limit
) {
1339 if (tPtr
->vpos
<limit
-16) tPtr
->vpos
+=16;
1340 else tPtr
->vpos
=limit
;
1346 case WSDecrementPage
:
1347 if(((int)tPtr
->vpos
- (int)height
) >= 0)
1348 tPtr
->vpos
-= height
;
1355 case WSIncrementPage
:
1356 tPtr
->vpos
+= height
;
1357 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1358 tPtr
->vpos
= tPtr
->docHeight
- height
;
1364 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1365 * (float)(tPtr
->docHeight
- height
);
1373 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1374 tPtr
->prevVpos
= tPtr
->vpos
;
1378 if (w
== tPtr
->hS
) {
1379 int width
= tPtr
->visible
.w
;
1381 which
= WMGetScrollerHitPart(tPtr
->hS
);
1384 case WSDecrementLine
:
1385 if (tPtr
->hpos
> 0) {
1386 if (tPtr
->hpos
>16) tPtr
->hpos
-=16;
1391 case WSIncrementLine
: {
1392 int limit
= tPtr
->docWidth
- width
;
1393 if (tPtr
->hpos
< limit
) {
1394 if (tPtr
->hpos
<limit
-16) tPtr
->hpos
+=16;
1395 else tPtr
->hpos
=limit
;
1400 case WSDecrementPage
:
1401 if(((int)tPtr
->hpos
- (int)width
) >= 0)
1402 tPtr
->hpos
-= width
;
1409 case WSIncrementPage
:
1410 tPtr
->hpos
+= width
;
1411 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1412 tPtr
->hpos
= tPtr
->docWidth
- width
;
1418 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1419 * (float)(tPtr
->docWidth
- width
);
1427 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1428 tPtr
->prevHpos
= tPtr
->hpos
;
1432 updateScrollers(tPtr
);
1441 unsigned short begin
, end
; /* what part of the text block */
1446 layOutLine(Text
*tPtr
, myLineItems
*items
, int nitems
, int x
, int y
)
1448 int i
, j
=0, lw
= 0, line_height
=0, max_d
=0, len
, n
;
1451 TextBlock
*tb
, *tbsame
=NULL
;
1453 if(!items
|| nitems
== 0)
1456 for(i
=0; i
<nitems
; i
++) {
1460 if (!tPtr
->flags
.monoFont
) {
1462 WMWidget
*wdt
= tb
->d
.widget
;
1463 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1464 if (tPtr
->flags
.alignment
!= WALeft
)
1465 lw
+= WMWidgetWidth(wdt
);
1467 line_height
= WMAX(line_height
,
1468 tb
->d
.pixmap
->height
+ max_d
);
1469 if (tPtr
->flags
.alignment
!= WALeft
)
1470 lw
+= tb
->d
.pixmap
->width
;
1475 font
= (tPtr
->flags
.monoFont
)?tPtr
->dFont
: tb
->d
.font
;
1476 /*max_d = WMAX(max_d, abs(font->height-font->y));*/
1478 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1479 text
= &(tb
->text
[items
[i
].begin
]);
1480 len
= items
[i
].end
- items
[i
].begin
;
1481 if (tPtr
->flags
.alignment
!= WALeft
)
1482 lw
+= WMWidthOfString(font
, text
, len
);
1486 if (tPtr
->flags
.alignment
== WARight
) {
1487 j
= tPtr
->visible
.w
- lw
;
1488 } else if (tPtr
->flags
.alignment
== WACenter
) {
1489 j
= (int) ((float)(tPtr
->visible
.w
- lw
))/2.0;
1492 for(i
=0; i
<nitems
; i
++) {
1495 if (tbsame
== tb
) { /* extend it, since it's on same line */
1496 tb
->sections
[tb
->nsections
-1].end
= items
[i
].end
;
1497 n
= tb
->nsections
-1;
1499 tb
->sections
= wrealloc(tb
->sections
,
1500 (++tb
->nsections
)*sizeof(Section
));
1501 n
= tb
->nsections
-1;
1502 tb
->sections
[n
]._y
= y
+ max_d
;
1503 tb
->sections
[n
].max_d
= max_d
;
1504 tb
->sections
[n
].x
= x
+j
;
1505 tb
->sections
[n
].h
= line_height
;
1506 tb
->sections
[n
].begin
= items
[i
].begin
;
1507 tb
->sections
[n
].end
= items
[i
].end
;
1510 tb
->sections
[n
].last
= (i
+1 == nitems
);
1513 if (!tPtr
->flags
.monoFont
) {
1515 WMWidget
*wdt
= tb
->d
.widget
;
1516 tb
->sections
[n
].y
= max_d
+ y
1517 + line_height
- WMWidgetHeight(wdt
);
1518 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1520 tb
->sections
[n
].y
= y
+ line_height
1521 + max_d
- tb
->d
.pixmap
->height
;
1522 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1524 x
+= tb
->sections
[n
].w
;
1527 font
= (tPtr
->flags
.monoFont
)? tPtr
->dFont
: tb
->d
.font
;
1528 len
= items
[i
].end
- items
[i
].begin
;
1529 text
= &(tb
->text
[items
[i
].begin
]);
1531 tb
->sections
[n
].y
= y
+line_height
-font
->y
;
1533 WMWidthOfString(font
,
1534 &(tb
->text
[tb
->sections
[n
].begin
]),
1535 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1537 x
+= WMWidthOfString(font
, text
, len
);
1549 layOutDocument(Text
*tPtr
)
1552 myLineItems
*items
= NULL
;
1553 unsigned int itemsSize
=0, nitems
=0, begin
, end
;
1555 unsigned int x
, y
=0, lw
= 0, width
=0, bmargin
;
1556 char *start
=NULL
, *mark
=NULL
;
1558 if ( tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)) )
1561 assert(tPtr
->visible
.w
> 20);
1563 tPtr
->docWidth
= tPtr
->visible
.w
;
1564 x
= tPtr
->margins
[tb
->marginN
].first
;
1565 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1567 /* only partial layOut needed: re-Lay only affected textblocks */
1568 if (tPtr
->flags
.laidOut
) {
1569 tb
= tPtr
->currentTextBlock
;
1571 /* search backwards for textblocks on same line */
1573 if (!tb
->sections
|| tb
->nsections
<1) {
1574 tb
= tPtr
->firstTextBlock
;
1575 tPtr
->flags
.laidOut
= False
;
1580 if(!tb
->prior
->sections
|| tb
->prior
->nsections
<1) {
1581 tb
= tPtr
->firstTextBlock
;
1582 tPtr
->flags
.laidOut
= False
;
1587 if (tb
->sections
[0]._y
!=
1588 tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
) {
1594 if(tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
>0) {
1595 y
= tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
+
1596 tb
->prior
->sections
[tb
->prior
->nsections
-1].h
-
1597 tb
->prior
->sections
[tb
->prior
->nsections
-1].max_d
;
1606 if (tb
->sections
&& tb
->nsections
>0) {
1607 wfree(tb
->sections
);
1608 tb
->sections
= NULL
;
1612 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1613 TextBlock
*next
= tb
->next
;
1614 tPtr
->currentTextBlock
= tb
;
1615 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1621 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1622 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1623 x
= tPtr
->margins
[tb
->marginN
].first
;
1624 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1630 if (!tPtr
->flags
.monoFont
) {
1632 width
= WMWidgetWidth(tb
->d
.widget
);
1634 width
= tb
->d
.pixmap
->width
;
1636 if (width
> tPtr
->docWidth
)
1637 tPtr
->docWidth
= width
;
1640 if (lw
>= tPtr
->visible
.w
- x
) {
1641 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1647 if(nitems
+ 1> itemsSize
) {
1648 items
= wrealloc(items
,
1649 (++itemsSize
)*sizeof(myLineItems
));
1652 items
[nitems
].tb
= tb
;
1653 items
[nitems
].begin
= 0;
1654 items
[nitems
].end
= 0;
1658 } else if ((start
= tb
->text
)) {
1660 font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
1663 mark
= strchr(start
, ' ');
1665 end
+= (int)(mark
-start
)+1;
1668 end
+= strlen(start
);
1675 if (end
-begin
> 0) {
1677 width
= WMWidthOfString(font
,
1678 &tb
->text
[begin
], end
-begin
);
1680 /* if it won't fit, char wrap it */
1681 if (width
>= tPtr
->visible
.w
) {
1682 char *t
= &tb
->text
[begin
];
1683 int l
=end
-begin
, i
=0;
1685 width
= WMWidthOfString(font
, t
, ++i
);
1686 } while (width
< tPtr
->visible
.w
&& i
< l
);
1689 start
= &tb
->text
[end
];
1695 if (lw
>= tPtr
->visible
.w
- x
) {
1696 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1702 if(nitems
+ 1 > itemsSize
) {
1703 items
= wrealloc(items
,
1704 (++itemsSize
)*sizeof(myLineItems
));
1707 items
[nitems
].tb
= tb
;
1708 items
[nitems
].begin
= begin
;
1709 items
[nitems
].end
= end
;
1717 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1718 if(0&&tPtr
->flags
.laidOut
1719 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
>0
1720 && (tPtr
->vpos
+ tPtr
->visible
.h
1721 < tb
->next
->sections
[0]._y
)) {
1722 if(tPtr
->lastTextBlock
->sections
1723 && tPtr
->lastTextBlock
->nsections
> 0 ) {
1724 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1725 int ly
= ltb
->sections
[ltb
->nsections
-1]._y
;
1726 int lh
= ltb
->sections
[ltb
->nsections
-1].h
;
1729 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
;
1730 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
);
1732 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1734 sd
= tPtr
->docHeight
-y
;
1736 printf("dif %d-%d: %d\n", ss
, sd
, ss
-sd
);
1737 y
+= tb
->next
->sections
[0]._y
-y
;
1739 printf("nitems%d\n", nitems
);
1741 y
= tPtr
->docHeight
+ss
-sd
;
1745 tPtr
->flags
.laidOut
= False
;
1754 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1756 if (tPtr
->docHeight
!= y
+10) {
1757 tPtr
->docHeight
= y
+10;
1758 updateScrollers(tPtr
);
1761 if(tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1764 tPtr
->flags
.horizOnDemand
= True
;
1765 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, True
);
1766 event
.type
= Expose
;
1767 handleEvents(&event
, (void *)tPtr
);
1769 } else if(tPtr
->docWidth
<= tPtr
->visible
.w
1770 && tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1771 tPtr
->flags
.horizOnDemand
= False
;
1772 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, False
);
1775 tPtr
->flags
.laidOut
= True
;
1777 if(items
&& itemsSize
> 0)
1784 textDidResize(W_ViewDelegate
*self
, WMView
*view
)
1786 Text
*tPtr
= (Text
*)view
->self
;
1787 unsigned short w
= tPtr
->view
->size
.width
;
1788 unsigned short h
= tPtr
->view
->size
.height
;
1789 unsigned short rh
= 0, vw
= 0, rel
;
1791 rel
= (tPtr
->flags
.relief
== WRFlat
);
1793 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1794 WMMoveWidget(tPtr
->ruler
, 2, 2);
1795 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1800 WMMoveWidget(tPtr
->vS
, 1 - (rel
?1:0), rh
+ 1 - (rel
?1:0));
1801 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
?2:0));
1803 WMSetRulerOffset(tPtr
->ruler
,22);
1804 } else WMSetRulerOffset(tPtr
->ruler
, 2);
1808 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1809 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1811 WMMoveWidget(tPtr
->hS
, vw
+1, h
- 21);
1812 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1816 tPtr
->visible
.x
= (tPtr
->vS
)?24:4;
1817 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)?43:3;
1818 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1819 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1820 tPtr
->visible
.h
-= (tPtr
->hS
)?20:0;
1821 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1823 if (tPtr
->view
->flags
.realized
) {
1826 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1827 tPtr
->db
= (Pixmap
) NULL
;
1830 if (tPtr
->visible
.w
< 40)
1831 tPtr
->visible
.w
= 40;
1832 if (tPtr
->visible
.h
< 20)
1833 tPtr
->visible
.h
= 20;
1836 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1837 tPtr
->view
->window
, tPtr
->visible
.w
,
1838 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1845 W_ViewDelegate _TextViewDelegate
=
1853 #define TEXT_BUFFER_INCR 8
1854 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1857 clearText(Text
*tPtr
)
1859 tPtr
->vpos
= tPtr
->hpos
= 0;
1860 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1861 tPtr
->cursor
.x
= -23;
1863 if (!tPtr
->firstTextBlock
)
1866 while (tPtr
->currentTextBlock
)
1867 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1869 tPtr
->firstTextBlock
= NULL
;
1870 tPtr
->currentTextBlock
= NULL
;
1871 tPtr
->lastTextBlock
= NULL
;
1872 WMEmptyArray(tPtr
->gfxItems
);
1875 /* possibly remove a single character from the currentTextBlock,
1876 or if there's a selection, remove it...
1877 note that Delete and Backspace are treated differently */
1879 deleteTextInteractively(Text
*tPtr
, KeySym ksym
)
1882 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1883 Bool done
= 1, wasFirst
= 0;
1885 if (!tPtr
->flags
.editable
)
1888 if ( !(tb
= tPtr
->currentTextBlock
) )
1891 if (tPtr
->flags
.ownsSelection
) {
1892 if(removeSelection(tPtr
))
1893 layOutDocument(tPtr
);
1897 wasFirst
= tb
->first
;
1898 if (back
&& tPtr
->tpos
< 1) {
1900 if(tb
->prior
->blank
) {
1901 tPtr
->currentTextBlock
= tb
->prior
;
1902 WMRemoveTextBlock(tPtr
);
1903 tPtr
->currentTextBlock
= tb
;
1905 layOutDocument(tPtr
);
1909 TextBlock
*prior
= tb
->prior
;
1910 tPtr
->currentTextBlock
= tb
;
1911 WMRemoveTextBlock(tPtr
);
1920 tPtr
->tpos
= tb
->used
;
1922 tPtr
->currentTextBlock
= tb
;
1926 tb
->next
->first
= False
;
1927 layOutDocument(tPtr
);
1934 if ( (tb
->used
> 0) && ((back
?tPtr
->tpos
> 0:1))
1935 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1938 memmove(&(tb
->text
[tPtr
->tpos
]),
1939 &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1944 /* if there are no characters left to back over in the textblock,
1945 but it still has characters to the right of the cursor: */
1946 if ( (back
? (tPtr
->tpos
== 0 && !done
) : ( tPtr
->tpos
>= tb
->used
))
1949 /* no more chars, and it's marked as blank? */
1951 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1953 if(tb
->used
== 0 || tb
->graphic
)
1954 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1957 tPtr
->currentTextBlock
= sibling
;
1959 tPtr
->tpos
= (back
? 1 : 0);
1961 tPtr
->tpos
= (back
? sibling
->used
: 0);
1963 /* no more chars, so mark it as blank */
1964 } else if(tb
->used
== 0) {
1966 } else if(tb
->graphic
) {
1967 Bool hasNext
= (Bool
)(tb
->next
);
1969 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1972 } else if(tPtr
->currentTextBlock
) {
1973 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
?
1974 1 : tPtr
->currentTextBlock
->used
);
1976 } else printf("DEBUG: unaccounted for... catch this!\n");
1979 layOutDocument(tPtr
);
1984 insertTextInteractively(Text
*tPtr
, char *text
, int len
)
1987 char *newline
= NULL
;
1989 if (!tPtr
->flags
.editable
) {
1993 if (len
< 1 || !text
)
1997 if(tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
2001 if (tPtr
->flags
.ownsSelection
)
2002 removeSelection(tPtr
);
2005 if (tPtr
->flags
.ignoreNewLine
) {
2007 for(i
=0; i
<len
; i
++) {
2008 if (text
[i
] == '\n')
2013 tb
= tPtr
->currentTextBlock
;
2014 if (!tb
|| tb
->graphic
) {
2016 WMAppendTextStream(tPtr
, text
);
2017 layOutDocument(tPtr
);
2021 if ((newline
= strchr(text
, '\n'))) {
2022 int nlen
= (int)(newline
-text
);
2023 int s
= tb
->used
- tPtr
->tpos
;
2025 if (!tb
->blank
&& nlen
>0) {
2030 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
2031 tb
->used
-= (tb
->used
- tPtr
->tpos
);
2033 insertTextInteractively(tPtr
, text
, nlen
);
2035 WMAppendTextStream(tPtr
, newline
);
2037 insertTextInteractively(tPtr
, save
, s
);
2041 if (tPtr
->tpos
>0 && tPtr
->tpos
< tb
->used
2042 && !tb
->graphic
&& tb
->text
) {
2044 unsigned short savePos
= tPtr
->tpos
;
2045 void *ntb
= WMCreateTextBlockWithText(
2046 tPtr
, &tb
->text
[tPtr
->tpos
],
2047 tb
->d
.font
, tb
->color
, True
, tb
->used
- tPtr
->tpos
);
2049 if(tb
->sections
[0].end
== tPtr
->tpos
)
2050 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2051 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2054 WMAppendTextBlock(tPtr
, ntb
);
2057 } else if (tPtr
->tpos
== tb
->used
) {
2058 if(tPtr
->flags
.indentNewLine
) {
2059 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2060 " ", tb
->d
.font
, tb
->color
, True
, 4));
2063 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2064 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2067 } else if (tPtr
->tpos
== 0) {
2068 if(tPtr
->flags
.indentNewLine
) {
2069 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2070 " ", tb
->d
.font
, tb
->color
, True
, 4));
2072 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2073 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2076 if(tPtr
->currentTextBlock
->next
)
2077 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
2081 if (tb
->used
+ len
>= tb
->allocated
) {
2082 tb
->allocated
= reqBlockSize(tb
->used
+len
);
2083 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
2087 memcpy(tb
->text
, text
, len
);
2090 tb
->text
[tb
->used
] = 0;
2094 memmove(&(tb
->text
[tPtr
->tpos
+len
]), &tb
->text
[tPtr
->tpos
],
2095 tb
->used
-tPtr
->tpos
+1);
2096 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2099 tb
->text
[tb
->used
] = 0;
2104 layOutDocument(tPtr
);
2109 selectRegion(Text
*tPtr
, int x
, int y
)
2115 y
+= (tPtr
->flags
.rulerShown
? 40: 0);
2118 y
-= 10; /* the original offset */
2120 x
-= tPtr
->visible
.x
-2;
2124 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2125 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2126 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2127 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2129 tPtr
->flags
.ownsSelection
= True
;
2135 releaseSelection(Text
*tPtr
)
2137 TextBlock
*tb
= tPtr
->firstTextBlock
;
2140 tb
->selected
= False
;
2143 tPtr
->flags
.ownsSelection
= False
;
2144 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
,
2152 requestHandler(WMView
*view
, Atom selection
, Atom target
, void *cdata
,
2155 Text
*tPtr
= view
->self
;
2156 Display
*dpy
= tPtr
->view
->screen
->display
;
2158 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2159 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2160 WMData
*data
= NULL
;
2163 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2164 char *text
= WMGetTextSelectedStream(tPtr
);
2167 data
= WMCreateDataWithBytes(text
, strlen(text
));
2168 WMSetDataFormat(data
, TYPETEXT
);
2172 } else printf("didn't get it\n");
2174 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2175 if (target
== _TARGETS
) {
2178 ptr
= wmalloc(4 * sizeof(Atom
));
2182 ptr
[3] = COMPOUND_TEXT
;
2184 data
= WMCreateDataWithBytes(ptr
, 4*4);
2185 WMSetDataFormat(data
, 32);
2196 lostHandler(WMView
*view
, Atom selection
, void *cdata
)
2198 releaseSelection((WMText
*)view
->self
);
2202 static WMSelectionProcs selectionHandler
= {
2203 requestHandler
, lostHandler
, NULL
2208 ownershipObserver(void *observerData
, WMNotification
*notification
)
2210 if (observerData
!= WMGetNotificationClientData(notification
))
2211 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2216 autoSelectText(Text
*tPtr
, int clicks
)
2220 char *mark
= NULL
, behind
, ahead
;
2222 if(!(tb
= tPtr
->currentTextBlock
))
2228 switch(tb
->text
[tPtr
->tpos
]) {
2231 case '<': case '>': behind = '<'; ahead = '>'; break;
2232 case '{': case '}': behind = '{'; ahead = '}'; break;
2233 case '[': case ']': behind = '['; ahead = ']'; break;
2235 default: behind
= ahead
= ' ';
2238 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
2239 tPtr
->sel
.h
= 6;/*tPtr->cursor.h-10;*/
2242 tPtr
->sel
.x
= tb
->sections
[0].x
;
2243 tPtr
->sel
.w
= tb
->sections
[0].w
;
2245 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
2248 while(start
> 0 && tb
->text
[start
-1] != behind
)
2252 if(tPtr
->tpos
> start
){
2253 x
-= WMWidthOfString(font
, &tb
->text
[start
],
2254 tPtr
->tpos
- start
);
2256 tPtr
->sel
.x
= (x
<0?0:x
)+1;
2258 if((mark
= strchr(&tb
->text
[start
], ahead
))) {
2259 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2260 (int)(mark
- &tb
->text
[start
]));
2261 } else if(tb
->used
> start
) {
2262 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2267 } else if(clicks
== 3) {
2268 TextBlock
*cur
= tb
;
2270 while(tb
&& !tb
->first
) {
2273 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2276 while(tb
->next
&& !tb
->next
->first
) {
2279 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
2283 tPtr
->sel
.w
= tPtr
->docWidth
;
2284 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2287 if (!tPtr
->flags
.ownsSelection
) {
2288 WMCreateSelectionHandler(tPtr
->view
,
2289 XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2290 tPtr
->flags
.ownsSelection
= True
;
2298 fontChanged(void *observerData
, WMNotification
*notification
)
2300 WMText
*tPtr
= (WMText
*) observerData
;
2301 WMFont
*font
= (WMFont
*)WMGetNotificationClientData(notification
);
2302 printf("fontChanged\n");
2307 if (tPtr
->flags
.ownsSelection
)
2308 WMSetTextSelectionFont(tPtr
, font
);
2313 handleTextKeyPress(Text
*tPtr
, XEvent
*event
)
2317 int control_pressed
= False
;
2318 TextBlock
*tb
= NULL
;
2320 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2321 control_pressed
= True
;
2322 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2327 if((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2329 updateCursorPosition(tPtr
);
2334 if((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2335 if(tPtr
->currentTextBlock
->graphic
)
2338 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2340 updateCursorPosition(tPtr
);
2345 if(!(tb
= tPtr
->currentTextBlock
))
2353 tPtr
->currentTextBlock
= tb
->prior
;
2354 if(tPtr
->currentTextBlock
->graphic
)
2357 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2359 if(!tb
->first
&& tPtr
->tpos
> 0)
2361 } else tPtr
->tpos
= 0;
2362 } else tPtr
->tpos
--;
2363 updateCursorPosition(tPtr
);
2368 if(!(tb
= tPtr
->currentTextBlock
))
2372 if(tPtr
->tpos
== tb
->used
) {
2375 tPtr
->currentTextBlock
= tb
->next
;
2377 if(!tb
->next
->first
&& tb
->next
->used
>0)
2383 tPtr
->tpos
= tb
->used
;
2385 } else tPtr
->tpos
++;
2386 updateCursorPosition(tPtr
);
2391 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2392 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2397 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2398 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2407 deleteTextInteractively(tPtr
, ksym
);
2408 updateCursorPosition(tPtr
);
2414 control_pressed
= True
;
2418 insertTextInteractively(tPtr
, " ", 4);
2419 updateCursorPosition(tPtr
);
2426 if (*buffer
!= 0 && !control_pressed
) {
2427 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2428 updateCursorPosition(tPtr
);
2431 } else if (control_pressed
&& ksym
==XK_r
) {
2432 Bool i
= !tPtr
->flags
.rulerShown
;
2433 WMShowTextRuler(tPtr
, i
);
2434 tPtr
->flags
.rulerShown
= i
;
2435 } else if (control_pressed
&& *buffer
== '\a') {
2436 XBell(tPtr
->view
->screen
->display
, 0);
2438 WMRelayToNextResponder(tPtr
->view
, event
);
2442 if (!control_pressed
&& tPtr
->flags
.ownsSelection
) {
2443 releaseSelection(tPtr
);
2449 pasteText(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2450 void *cdata
, WMData
*data
)
2452 Text
*tPtr
= (Text
*)view
->self
;
2455 tPtr
->flags
.waitingForSelection
= 0;
2458 text
= (char*)WMDataBytes(data
);
2461 (tPtr
->parser
) (tPtr
, (void *) text
);
2462 layOutDocument(tPtr
);
2463 } else insertTextInteractively(tPtr
, text
, strlen(text
));
2464 updateCursorPosition(tPtr
);
2470 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2475 (tPtr
->parser
) (tPtr
, (void *) text
);
2476 layOutDocument(tPtr
);
2477 } else insertTextInteractively(tPtr
, text
, n
);
2478 updateCursorPosition(tPtr
);
2490 handleActionEvents(XEvent
*event
, void *data
)
2492 Text
*tPtr
= (Text
*)data
;
2493 Display
*dpy
= event
->xany
.display
;
2497 switch (event
->type
) {
2499 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2500 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2501 tPtr
->flags
.extendSelection
= True
;
2505 if (tPtr
->flags
.focused
) {
2506 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2507 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
2508 GrabModeAsync
, GrabModeAsync
, None
,
2509 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2510 tPtr
->flags
.pointerGrabbed
= True
;
2511 handleTextKeyPress(tPtr
, event
);
2516 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2517 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2518 tPtr
->flags
.extendSelection
= False
;
2520 /* end modify flag so selection can be extended */
2527 if (tPtr
->flags
.pointerGrabbed
) {
2528 tPtr
->flags
.pointerGrabbed
= False
;
2529 XUngrabPointer(dpy
, CurrentTime
);
2532 if(tPtr
->flags
.waitingForSelection
)
2535 if ((event
->xmotion
.state
& Button1Mask
)) {
2536 TextBlock
*tb
= tPtr
->currentTextBlock
;
2538 if(tb
&& tPtr
->flags
.isOverGraphic
&&
2539 tb
->graphic
&& !tb
->object
) {
2541 WMPixmap
*pixmap
= tb
->d
.pixmap
;
2542 char *types
[2] = {"application/X-image", NULL
};
2547 WMDragImageFromView(tPtr
->view
, pixmap
, types
,
2548 wmkpoint(event
->xmotion
.x_root
, event
->xmotion
.y_root
),
2553 if (!tPtr
->flags
.ownsSelection
) {
2554 WMCreateSelectionHandler(tPtr
->view
,
2555 XA_PRIMARY
, event
->xbutton
.time
,
2556 &selectionHandler
, NULL
);
2557 tPtr
->flags
.ownsSelection
= True
;
2560 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2564 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2570 if (tPtr
->flags
.pointerGrabbed
) {
2571 tPtr
->flags
.pointerGrabbed
= False
;
2572 XUngrabPointer(dpy
, CurrentTime
);
2576 if (tPtr
->flags
.waitingForSelection
)
2579 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2580 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2584 if (tPtr
->flags
.ownsSelection
)
2585 releaseSelection(tPtr
);
2588 if (event
->xbutton
.button
== Button1
) {
2590 if(WMIsDoubleClick(event
)) {
2591 TextBlock
*tb
= tPtr
->currentTextBlock
;
2593 tPtr
->lastClickTime
= event
->xbutton
.time
;
2594 if(tb
&& tb
->graphic
&& !tb
->object
) {
2595 if(tPtr
->delegate
&& tPtr
->delegate
->didDoubleClickOnPicture
) {
2598 desc
= wmalloc(tb
->used
+1);
2599 memcpy(desc
, tb
->text
, tb
->used
);
2601 (*tPtr
->delegate
->didDoubleClickOnPicture
)(tPtr
->delegate
, desc
);
2605 autoSelectText(tPtr
, 2);
2608 } else if(event
->xbutton
.time
- tPtr
->lastClickTime
2609 < WINGsConfiguration
.doubleClickDelay
) {
2610 tPtr
->lastClickTime
= event
->xbutton
.time
;
2611 autoSelectText(tPtr
, 3);
2615 if (!tPtr
->flags
.focused
) {
2616 WMSetFocusToWidget(tPtr
);
2617 tPtr
->flags
.focused
= True
;
2620 tPtr
->lastClickTime
= event
->xbutton
.time
;
2621 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2625 if (event
->xbutton
.button
2626 == WINGsConfiguration
.mouseWheelDown
) {
2627 WMScrollText(tPtr
, 16);
2631 if (event
->xbutton
.button
2632 == WINGsConfiguration
.mouseWheelUp
) {
2633 WMScrollText(tPtr
, -16);
2637 if (event
->xbutton
.button
== Button2
) {
2641 if (!tPtr
->flags
.editable
) {
2646 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2647 event
->xbutton
.time
, pasteText
, NULL
)) {
2649 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2650 tPtr
->flags
.waitingForSelection
= 0;
2656 (tPtr
->parser
) (tPtr
, (void *) text
);
2657 layOutDocument(tPtr
);
2660 insertTextInteractively(tPtr
, text
, n
);
2664 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2665 (void*)WMInsertTextEvent
);
2667 updateCursorPosition(tPtr
);
2671 tPtr
->flags
.waitingForSelection
= True
;
2679 if (tPtr
->flags
.pointerGrabbed
) {
2680 tPtr
->flags
.pointerGrabbed
= False
;
2681 XUngrabPointer(dpy
, CurrentTime
);
2685 if (tPtr
->flags
.waitingForSelection
)
2693 handleEvents(XEvent
*event
, void *data
)
2695 Text
*tPtr
= (Text
*)data
;
2697 switch(event
->type
) {
2700 if (event
->xexpose
.count
!=0)
2704 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2705 WMRealizeWidget(tPtr
->hS
);
2709 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2710 WMRealizeWidget(tPtr
->vS
);
2714 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2715 WMRealizeWidget(tPtr
->ruler
);
2720 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2726 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2729 tPtr
->flags
.focused
= True
;
2731 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2732 tPtr
->timerID
= WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY
,
2740 tPtr
->flags
.focused
= False
;
2743 if (tPtr
->timerID
) {
2744 WMDeleteTimerHandler(tPtr
->timerID
);
2745 tPtr
->timerID
= NULL
;
2754 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2756 WMEmptyArray(tPtr
->gfxItems
);
2759 WMDeleteTimerHandler(tPtr
->timerID
);
2761 WMReleaseFont(tPtr
->dFont
);
2762 WMReleaseColor(tPtr
->dColor
);
2763 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2764 WMRemoveNotificationObserver(tPtr
);
2775 insertPlainText(Text
*tPtr
, char *text
)
2782 mark
= strchr(start
, '\n');
2784 tb
= WMCreateTextBlockWithText(tPtr
,
2786 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
-start
));
2788 tPtr
->flags
.first
= True
;
2790 if (start
&& strlen(start
)) {
2791 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2792 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2794 tPtr
->flags
.first
= False
;
2798 if (tPtr
->flags
.prepend
)
2799 WMPrependTextBlock(tPtr
, tb
);
2801 WMAppendTextBlock(tPtr
, tb
);
2807 rulerMoveCallBack(WMWidget
*w
, void *self
)
2809 Text
*tPtr
= (Text
*)self
;
2813 if (W_CLASS(tPtr
) != WC_Text
)
2821 rulerReleaseCallBack(WMWidget
*w
, void *self
)
2823 Text
*tPtr
= (Text
*)self
;
2827 if (W_CLASS(tPtr
) != WC_Text
)
2835 draggingSourceOperation(WMView
*self
, Bool local
)
2837 return WDOperationCopy
;
2841 fetchDragData(WMView
*self
, char *type
)
2843 TextBlock
*tb
= ((WMText
*)self
->self
)->currentTextBlock
;
2850 printf("type is [%s]\n", type
);
2851 desc
= wmalloc(tb
->used
+1);
2852 memcpy(desc
, tb
->text
, tb
->used
);
2854 data
= WMCreateDataWithBytes(desc
, strlen(desc
)+1);
2862 static WMDragSourceProcs _DragSourceProcs
= {
2863 draggingSourceOperation
,
2871 draggingEntered(WMView
*self
, WMDraggingInfo
*info
)
2873 printf("draggingEntered\n");
2874 return WDOperationCopy
;
2879 draggingUpdated(WMView
*self
, WMDraggingInfo
*info
)
2881 return WDOperationCopy
;
2886 draggingExited(WMView
*self
, WMDraggingInfo
*info
)
2888 printf("draggingExited\n");
2892 prepareForDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2894 printf("prepareForDragOperation\n");
2902 receivedData(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2903 void *cdata
, WMData
*data
)
2905 badbadbad
= wstrdup((char *)WMDataBytes(data
));
2909 /* when it's done in WINGs, remove this */
2912 requestDroppedData(WMView
*view
, WMDraggingInfo
*info
, char *type
)
2914 WMScreen
*scr
= W_VIEW_SCREEN(view
);
2916 if (!WMRequestSelection(scr
->dragInfo
.destView
,
2917 scr
->xdndSelectionAtom
,
2918 XInternAtom(scr
->display
, type
, False
),
2919 scr
->dragInfo
.timestamp
,
2920 receivedData
, &scr
->dragInfo
)) {
2921 wwarning("could not request data for dropped data");
2927 ev
.type
= ClientMessage
;
2928 ev
.xclient
.message_type
= scr
->xdndFinishedAtom
;
2929 ev
.xclient
.format
= 32;
2930 ev
.xclient
.window
= info
->destinationWindow
;
2931 ev
.xclient
.data
.l
[0] = 0;
2932 ev
.xclient
.data
.l
[1] = 0;
2933 ev
.xclient
.data
.l
[2] = 0;
2934 ev
.xclient
.data
.l
[3] = 0;
2935 ev
.xclient
.data
.l
[4] = 0;
2937 XSendEvent(scr
->display
, info
->sourceWindow
, False
, 0, &ev
);
2938 XFlush(scr
->display
);
2944 performDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2947 WMText
*tPtr
= (WMText
*)self
->self
;
2952 requestDroppedData(tPtr
->view
, info
, "application/X-color");
2953 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), badbadbad
, True
);
2955 WMSetTextSelectionColor(tPtr
, color
);
2956 WMReleaseColor(color
);
2965 concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2967 printf("concludeDragOperation\n");
2971 static WMDragDestinationProcs _DragDestinationProcs
= {
2975 prepareForDragOperation
,
2976 performDragOperation
,
2977 concludeDragOperation
2982 getStream(WMText
*tPtr
, int sel
, int array
)
2984 TextBlock
*tb
= NULL
;
2986 unsigned long where
= 0;
2991 if (!(tb
= tPtr
->firstTextBlock
))
2995 (tPtr
->writer
) (tPtr
, (void *) text
);
2999 tb
= tPtr
->firstTextBlock
;
3002 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
3004 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
3006 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
3007 && tb
!= tPtr
->firstTextBlock
) {
3008 text
= wrealloc(text
, where
+1);
3009 text
[where
++] = '\n';
3015 if(tb
->graphic
&& array
) {
3016 text
= wrealloc(text
, where
+4);
3017 text
[where
++] = 0xFA;
3018 text
[where
++] = (tb
->used
>>8)&0x0ff;
3019 text
[where
++] = tb
->used
&0x0ff;
3020 text
[where
++] = tb
->allocated
; /* extra info */
3022 text
= wrealloc(text
, where
+tb
->used
);
3023 memcpy(&text
[where
], tb
->text
, tb
->used
);
3027 } else if (sel
&& tb
->selected
) {
3029 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
3030 text
= wrealloc(text
, where
+1);
3031 text
[where
++] = '\n';
3037 text
= wrealloc(text
, where
+(tb
->s_end
- tb
->s_begin
));
3038 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
],
3039 tb
->s_end
- tb
->s_begin
);
3040 where
+= tb
->s_end
- tb
->s_begin
;
3045 _gSnext
:tb
= tb
->next
;
3048 /* +1 for the end of string, let's be nice */
3049 text
= wrealloc(text
, where
+1);
3056 releaseStreamObjects(void *data
)
3063 getStreamObjects(WMText
*tPtr
, int sel
)
3065 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
3069 char *start
, *fa
, *desc
;
3071 stream
= getStream(tPtr
, sel
, 1);
3078 fa
= strchr(start
, 0xFA);
3080 if((int)(fa
- start
)>0) {
3082 desc
[(int)(fa
- start
)] = 0;
3083 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
3084 WMSetDataFormat(data
, TYPETEXT
);
3085 WMAddToArray(array
, (void *) data
);
3088 len
= *(fa
+1)*0xff + *(fa
+2);
3089 data
= WMCreateDataWithBytes((void *)(fa
+4), len
);
3090 WMSetDataFormat(data
, *(fa
+3));
3091 WMAddToArray(array
, (void *) data
);
3092 start
= fa
+ len
+ 4;
3095 if (start
&& strlen(start
)) {
3096 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
3097 WMSetDataFormat(data
, TYPETEXT
);
3098 WMAddToArray(array
, (void *) data
);
3110 WMCreateTextForDocumentType(WMWidget
*parent
, WMAction
*parser
, WMAction
*writer
)
3117 tPtr
= wmalloc(sizeof(Text
));
3118 memset(tPtr
, 0, sizeof(Text
));
3119 tPtr
->widgetClass
= WC_Text
;
3120 tPtr
->view
= W_CreateView(W_VIEW(parent
));
3122 perror("could not create text's view\n");
3127 dpy
= tPtr
->view
->screen
->display
;
3128 scr
= tPtr
->view
->screen
;
3130 tPtr
->view
->self
= tPtr
;
3131 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
3132 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3133 W_ResizeView(tPtr
->view
, 250, 200);
3135 tPtr
->dColor
= WMBlackColor(scr
);
3136 tPtr
->fgColor
= WMBlackColor(scr
);
3137 tPtr
->bgColor
= WMWhiteColor(scr
);
3138 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3140 gcv
.graphics_exposures
= False
;
3141 gcv
.foreground
= W_PIXEL(scr
->gray
);
3142 gcv
.background
= W_PIXEL(scr
->darkGray
);
3143 gcv
.fill_style
= FillStippled
;
3144 /* why not use scr->stipple here? */
3145 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
,
3146 STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
3147 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
3148 GCForeground
|GCBackground
|GCStipple
3149 |GCFillStyle
|GCGraphicsExposures
, &gcv
);
3155 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
3157 tPtr
->view
->delegate
= &_TextViewDelegate
;
3159 tPtr
->delegate
= NULL
;
3162 tPtr
->timerID
= NULL
;
3165 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
3166 |EnterWindowMask
|LeaveWindowMask
|FocusChangeMask
,
3167 handleEvents
, tPtr
);
3169 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
|ButtonPressMask
3170 |KeyReleaseMask
|KeyPressMask
|Button1MotionMask
,
3171 handleActionEvents
, tPtr
);
3173 WMAddNotificationObserver(ownershipObserver
, tPtr
,
3174 WMSelectionOwnerDidChangeNotification
,
3177 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3178 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3182 char *types
[3] = {"application/X-color", "application/X-image", NULL
};
3183 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3186 /*WMAddNotificationObserver(fontChanged, tPtr,
3187 WMFontPanelDidChangeNotification, tPtr);*/
3189 tPtr
->firstTextBlock
= NULL
;
3190 tPtr
->lastTextBlock
= NULL
;
3191 tPtr
->currentTextBlock
= NULL
;
3194 tPtr
->gfxItems
= WMCreateArray(4);
3196 tPtr
->parser
= parser
;
3197 tPtr
->writer
= writer
;
3199 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3200 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3202 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3204 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3205 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3206 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3208 tPtr
->cursor
.x
= -23;
3211 tPtr
->docHeight
= 0;
3212 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
,
3214 tPtr
->db
= (Pixmap
) NULL
;
3215 tPtr
->bgPixmap
= NULL
;
3217 tPtr
->margins
= WMGetRulerMargins(NULL
);
3218 tPtr
->margins
->right
= tPtr
->visible
.w
;
3221 tPtr
->flags
.rulerShown
= False
;
3222 tPtr
->flags
.monoFont
= False
;
3223 tPtr
->flags
.focused
= False
;
3224 tPtr
->flags
.editable
= True
;
3225 tPtr
->flags
.ownsSelection
= False
;
3226 tPtr
->flags
.pointerGrabbed
= False
;
3227 tPtr
->flags
.extendSelection
= False
;
3228 tPtr
->flags
.frozen
= False
;
3229 tPtr
->flags
.cursorShown
= True
;
3230 tPtr
->flags
.acceptsGraphic
= False
;
3231 tPtr
->flags
.horizOnDemand
= False
;
3232 tPtr
->flags
.needsLayOut
= False
;
3233 tPtr
->flags
.ignoreNewLine
= False
;
3234 tPtr
->flags
.indentNewLine
= False
;
3235 tPtr
->flags
.laidOut
= False
;
3236 tPtr
->flags
.ownsSelection
= False
;
3237 tPtr
->flags
.waitingForSelection
= False
;
3238 tPtr
->flags
.prepend
= False
;
3239 tPtr
->flags
.isOverGraphic
= False
;
3240 tPtr
->flags
.relief
= WRSunken
;
3241 tPtr
->flags
.isOverGraphic
= 0;
3242 tPtr
->flags
.alignment
= WALeft
;
3243 tPtr
->flags
.first
= True
;
3249 WMPrependTextStream(WMText
*tPtr
, char *text
)
3251 CHECK_CLASS(tPtr
, WC_Text
);
3254 if (tPtr
->flags
.ownsSelection
)
3255 releaseSelection(tPtr
);
3257 updateScrollers(tPtr
);
3261 tPtr
->flags
.prepend
= True
;
3262 if (text
&& tPtr
->parser
)
3263 (tPtr
->parser
) (tPtr
, (void *) text
);
3265 insertPlainText(tPtr
, text
);
3267 tPtr
->flags
.needsLayOut
= True
;
3269 if(!tPtr
->flags
.frozen
) {
3270 layOutDocument(tPtr
);
3276 WMAppendTextStream(WMText
*tPtr
, char *text
)
3278 CHECK_CLASS(tPtr
, WC_Text
);
3281 if (tPtr
->flags
.ownsSelection
)
3282 releaseSelection(tPtr
);
3284 updateScrollers(tPtr
);
3288 tPtr
->flags
.prepend
= False
;
3289 if (text
&& tPtr
->parser
)
3290 (tPtr
->parser
) (tPtr
, (void *) text
);
3292 insertPlainText(tPtr
, text
);
3294 tPtr
->flags
.needsLayOut
= True
;
3295 if(tPtr
->currentTextBlock
) {
3296 if(tPtr
->currentTextBlock
->graphic
)
3299 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3302 if(!tPtr
->flags
.frozen
) {
3303 layOutDocument(tPtr
);
3309 WMGetTextStream(WMText
*tPtr
)
3311 CHECK_CLASS(tPtr
, WC_Text
);
3313 return getStream(tPtr
, 0, 0);
3318 WMGetTextSelectedStream(WMText
*tPtr
)
3320 CHECK_CLASS(tPtr
, WC_Text
);
3322 return getStream(tPtr
, 1, 0);
3327 WMGetTextObjects(WMText
*tPtr
)
3329 CHECK_CLASS(tPtr
, WC_Text
);
3331 return getStreamObjects(tPtr
, 0);
3335 WMGetTextSelectedObjects(WMText
*tPtr
)
3337 CHECK_CLASS(tPtr
, WC_Text
);
3339 return getStreamObjects(tPtr
, 1);
3344 WMSetTextDelegate(WMText
*tPtr
, WMTextDelegate
*delegate
)
3346 CHECK_CLASS(tPtr
, WC_Text
);
3348 tPtr
->delegate
= delegate
;
3353 WMCreateTextBlockWithObject(WMText
*tPtr
, WMWidget
*w
,
3354 char *description
, WMColor
*color
,
3355 unsigned short first
, unsigned short extraInfo
)
3359 if (!w
|| !description
|| !color
)
3362 tb
= wmalloc(sizeof(TextBlock
));
3364 tb
->text
= wstrdup(description
);
3365 tb
->used
= strlen(description
);
3368 tb
->color
= WMRetainColor(color
);
3369 tb
->marginN
= newMargin(tPtr
, NULL
);
3370 tb
->allocated
= extraInfo
;
3375 tb
->underlined
= False
;
3376 tb
->selected
= False
;
3378 tb
->sections
= NULL
;
3388 WMCreateTextBlockWithPixmap(WMText
*tPtr
, WMPixmap
*p
,
3389 char *description
, WMColor
*color
,
3390 unsigned short first
, unsigned short extraInfo
)
3394 if (!p
|| !description
|| !color
)
3397 tb
= wmalloc(sizeof(TextBlock
));
3399 tb
->text
= wstrdup(description
);
3400 tb
->used
= strlen(description
);
3402 tb
->d
.pixmap
= WMRetainPixmap(p
);
3403 tb
->color
= WMRetainColor(color
);
3404 tb
->marginN
= newMargin(tPtr
, NULL
);
3405 tb
->allocated
= extraInfo
;
3410 tb
->underlined
= False
;
3411 tb
->selected
= False
;
3413 tb
->sections
= NULL
;
3423 WMCreateTextBlockWithText(WMText
*tPtr
, char *text
, WMFont
*font
, WMColor
*color
,
3424 unsigned short first
, unsigned short len
)
3428 if (!font
|| !color
)
3431 tb
= wmalloc(sizeof(TextBlock
));
3433 tb
->allocated
= reqBlockSize(len
);
3434 tb
->text
= (char *)wmalloc(tb
->allocated
);
3435 memset(tb
->text
, 0, tb
->allocated
);
3437 if (len
< 1|| !text
|| (*text
== '\n' && len
==1 )) {
3442 memcpy(tb
->text
, text
, len
);
3446 tb
->text
[tb
->used
] = 0;
3448 tb
->d
.font
= WMRetainFont(font
);
3449 tb
->color
= WMRetainColor(color
);
3450 tb
->marginN
= newMargin(tPtr
, NULL
);
3453 tb
->graphic
= False
;
3454 tb
->underlined
= False
;
3455 tb
->selected
= False
;
3457 tb
->sections
= NULL
;
3466 WMSetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int first
,
3467 unsigned int kanji
, unsigned int underlined
, int script
,
3468 WMRulerMargins
*margins
)
3470 TextBlock
*tb
= (TextBlock
*) vtb
;
3476 tb
->underlined
= underlined
;
3477 tb
->script
= script
;
3478 tb
->marginN
= newMargin(tPtr
, margins
);
3483 WMGetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int *first
,
3484 unsigned int *kanji
, unsigned int *underlined
, int *script
,
3485 WMRulerMargins
*margins
)
3487 TextBlock
*tb
= (TextBlock
*) vtb
;
3491 if (first
) *first
= tb
->first
;
3492 if (kanji
) *kanji
= tb
->kanji
;
3493 if (underlined
) *underlined
= tb
->underlined
;
3494 if (script
) *script
= tb
->script
;
3495 if (margins
) margins
= &tPtr
->margins
[tb
->marginN
];
3500 WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3502 TextBlock
*tb
= (TextBlock
*)vtb
;
3509 WMWidget
*w
= tb
->d
.widget
;
3510 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3511 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3512 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3515 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3519 tPtr
->tpos
= tb
->used
;
3522 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3523 tb
->next
= tb
->prior
= NULL
;
3525 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3526 = tPtr
->currentTextBlock
= tb
;
3531 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3534 tb
->next
= tPtr
->currentTextBlock
;
3535 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3536 if (tPtr
->currentTextBlock
->prior
)
3537 tPtr
->currentTextBlock
->prior
->next
= tb
;
3539 tPtr
->currentTextBlock
->prior
= tb
;
3541 tPtr
->firstTextBlock
= tb
;
3543 tPtr
->currentTextBlock
= tb
;
3548 WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3550 TextBlock
*tb
= (TextBlock
*)vtb
;
3557 WMWidget
*w
= tb
->d
.widget
;
3558 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3559 (W_VIEW(w
))->attribs
.cursor
=
3560 tPtr
->view
->screen
->defaultCursor
;
3561 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3564 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3568 tPtr
->tpos
= tb
->used
;
3572 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3573 tb
->next
= tb
->prior
= NULL
;
3575 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3576 = tPtr
->currentTextBlock
= tb
;
3581 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3584 tb
->next
= tPtr
->currentTextBlock
->next
;
3585 tb
->prior
= tPtr
->currentTextBlock
;
3586 if (tPtr
->currentTextBlock
->next
)
3587 tPtr
->currentTextBlock
->next
->prior
= tb
;
3589 tPtr
->currentTextBlock
->next
= tb
;
3592 tPtr
->lastTextBlock
= tb
;
3594 tPtr
->currentTextBlock
= tb
;
3599 WMRemoveTextBlock(WMText
*tPtr
)
3601 TextBlock
*tb
= NULL
;
3603 if (!tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
||
3604 !tPtr
->currentTextBlock
) {
3608 tb
= tPtr
->currentTextBlock
;
3610 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3613 WMUnmapWidget(tb
->d
.widget
);
3617 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3618 if (tPtr
->currentTextBlock
->next
)
3619 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3621 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3622 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3624 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3625 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3626 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3627 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3629 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3630 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3631 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3640 destroyWidget(WMWidget
*widget
)
3642 WMDestroyWidget(widget
);
3643 // -- never do this -- wfree(widget);
3649 WMDestroyTextBlock(WMText
*tPtr
, void *vtb
)
3651 TextBlock
*tb
= (TextBlock
*)vtb
;
3657 /* naturally, there's a danger to destroying widgets whose action
3658 * brings us here: ie. press a button to destroy it...
3659 * need to find a safer way. till then... this stays commented out */
3660 /* 5 months later... destroy it 10 seconds after now which should
3661 * be enough time for the widget's action to be completed... :-) */
3662 /* This is a bad assumption. Just destroy the widget here.
3663 * if the caller needs it, it can protect it with W_RetainView()
3664 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3665 WMDestroyWidget(tb
->d
.widget
);
3667 WMReleasePixmap(tb
->d
.pixmap
);
3670 WMReleaseFont(tb
->d
.font
);
3673 WMReleaseColor(tb
->color
);
3674 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3676 wfree(tb
->sections
);
3683 WMSetTextForegroundColor(WMText
*tPtr
, WMColor
*color
)
3686 WMReleaseColor(tPtr
->fgColor
);
3688 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3695 WMSetTextBackgroundColor(WMText
*tPtr
, WMColor
*color
)
3698 WMReleaseColor(tPtr
->bgColor
);
3700 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3701 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3708 WMSetTextBackgroundPixmap(WMText
*tPtr
, WMPixmap
*pixmap
)
3711 WMReleasePixmap(tPtr
->bgPixmap
);
3714 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3716 tPtr
->bgPixmap
= NULL
;
3721 WMSetTextRelief(WMText
*tPtr
, WMReliefType relief
)
3723 tPtr
->flags
.relief
= relief
;
3724 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3729 WMSetTextHasHorizontalScroller(WMText
*tPtr
, Bool shouldhave
)
3731 if (shouldhave
&& !tPtr
->hS
) {
3732 tPtr
->hS
= WMCreateScroller(tPtr
);
3733 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3734 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3735 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3736 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3737 WMMapWidget(tPtr
->hS
);
3738 } else if (!shouldhave
&& tPtr
->hS
) {
3739 WMUnmapWidget(tPtr
->hS
);
3740 WMDestroyWidget(tPtr
->hS
);
3746 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3751 WMSetTextHasRuler(WMText
*tPtr
, Bool shouldhave
)
3753 if(shouldhave
&& !tPtr
->ruler
) {
3754 tPtr
->ruler
= WMCreateRuler(tPtr
);
3755 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
=
3756 tPtr
->view
->screen
->defaultCursor
;
3757 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3758 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3759 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3760 } else if(!shouldhave
&& tPtr
->ruler
) {
3761 WMShowTextRuler(tPtr
, False
);
3762 WMDestroyWidget(tPtr
->ruler
);
3765 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3769 WMShowTextRuler(WMText
*tPtr
, Bool show
)
3774 if(tPtr
->flags
.monoFont
)
3777 tPtr
->flags
.rulerShown
= show
;
3779 WMMapWidget(tPtr
->ruler
);
3781 WMUnmapWidget(tPtr
->ruler
);
3784 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3789 WMGetTextRulerShown(WMText
*tPtr
)
3794 return tPtr
->flags
.rulerShown
;
3799 WMSetTextHasVerticalScroller(WMText
*tPtr
, Bool shouldhave
)
3801 if (shouldhave
&& !tPtr
->vS
) {
3802 tPtr
->vS
= WMCreateScroller(tPtr
);
3803 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3804 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3805 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3806 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3807 WMMapWidget(tPtr
->vS
);
3808 } else if (!shouldhave
&& tPtr
->vS
) {
3809 WMUnmapWidget(tPtr
->vS
);
3810 WMDestroyWidget(tPtr
->vS
);
3816 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3821 WMScrollText(WMText
*tPtr
, int amount
)
3825 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3829 if (tPtr
->vpos
> 0) {
3830 if (tPtr
->vpos
> abs(amount
)) tPtr
->vpos
+= amount
;
3835 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3836 if (tPtr
->vpos
< limit
) {
3837 if (tPtr
->vpos
< limit
-amount
) tPtr
->vpos
+= amount
;
3838 else tPtr
->vpos
= limit
;
3843 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3844 updateScrollers(tPtr
);
3847 tPtr
->prevVpos
= tPtr
->vpos
;
3853 WMPageText(WMText
*tPtr
, Bool direction
)
3855 if (!tPtr
->view
->flags
.realized
)
3858 return WMScrollText(tPtr
, direction
?tPtr
->visible
.h
:-tPtr
->visible
.h
);
3863 WMSetTextEditable(WMText
*tPtr
, Bool editable
)
3865 tPtr
->flags
.editable
= editable
;
3870 WMGetTextEditable(WMText
*tPtr
)
3872 return tPtr
->flags
.editable
;
3876 WMSetTextIndentNewLines(WMText
*tPtr
, Bool indent
)
3878 tPtr
->flags
.indentNewLine
= indent
;
3882 WMSetTextIgnoresNewline(WMText
*tPtr
, Bool ignore
)
3884 tPtr
->flags
.ignoreNewLine
= ignore
;
3888 WMGetTextIgnoresNewline(WMText
*tPtr
)
3890 return tPtr
->flags
.ignoreNewLine
;
3894 WMSetTextUsesMonoFont(WMText
*tPtr
, Bool mono
)
3897 if(tPtr
->flags
.rulerShown
)
3898 WMShowTextRuler(tPtr
, False
);
3899 if(tPtr
->flags
.alignment
!= WALeft
)
3900 tPtr
->flags
.alignment
= WALeft
;
3903 tPtr
->flags
.monoFont
= mono
;
3904 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3908 WMGetTextUsesMonoFont(WMText
*tPtr
)
3910 return tPtr
->flags
.monoFont
;
3915 WMSetTextDefaultFont(WMText
*tPtr
, WMFont
*font
)
3918 WMReleaseFont(tPtr
->dFont
);
3921 tPtr
->dFont
= WMRetainFont(font
);
3923 tPtr
->dFont
= WMSystemFontOfSize(tPtr
->view
->screen
, 12);
3929 WMGetTextDefaultFont(WMText
*tPtr
)
3931 return WMRetainFont(tPtr
->dFont
);
3936 WMSetTextDefaultColor(WMText
*tPtr
, WMColor
*color
)
3939 WMReleaseColor(tPtr
->dColor
);
3942 tPtr
->dColor
= WMRetainColor(color
);
3944 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3950 WMGetTextDefaultColor(WMText
*tPtr
)
3952 return tPtr
->dColor
;
3957 WMSetTextAlignment(WMText
*tPtr
, WMAlignment alignment
)
3959 if(tPtr
->flags
.monoFont
)
3960 tPtr
->flags
.alignment
= WALeft
;
3962 tPtr
->flags
.alignment
= alignment
;
3968 WMGetTextInsertType(WMText
*tPtr
)
3970 return tPtr
->flags
.prepend
;
3975 WMSetTextSelectionColor(WMText
*tPtr
, WMColor
*color
)
3977 setSelectionProperty(tPtr
, NULL
, color
, -1);
3982 WMGetTextSelectionColor(WMText
*tPtr
)
3986 tb
= tPtr
->currentTextBlock
;
3988 if (!tb
|| !tPtr
->flags
.ownsSelection
)
3999 WMSetTextSelectionFont(WMText
*tPtr
, WMFont
*font
)
4001 setSelectionProperty(tPtr
, font
, NULL
, -1) ;
4006 WMGetTextSelectionFont(WMText
*tPtr
)
4010 tb
= tPtr
->currentTextBlock
;
4012 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4019 tb
= getFirstNonGraphicBlockFor(tb
, 1);
4023 return (tb
->selected
? tb
->d
.font
: NULL
);
4028 WMSetTextSelectionUnderlined(WMText
*tPtr
, int underlined
)
4031 if (underlined
!=0 && underlined
!=1)
4034 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
4039 WMGetTextSelectionUnderlined(WMText
*tPtr
)
4043 tb
= tPtr
->currentTextBlock
;
4045 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4051 return tb
->underlined
;
4056 WMFreezeText(WMText
*tPtr
)
4058 tPtr
->flags
.frozen
= True
;
4063 WMThawText(WMText
*tPtr
)
4065 tPtr
->flags
.frozen
= False
;
4067 if(tPtr
->flags
.monoFont
) {
4068 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
4071 /* make sure to unmap widgets no matter where they are */
4072 /* they'll be later remapped if needed by paintText */
4073 for(j
=0; j
<c
; j
++) {
4074 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
4075 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
4076 WMUnmapWidget(tb
->d
.widget
);
4082 tPtr
->flags
.laidOut
= False
;
4083 layOutDocument(tPtr
);
4084 updateScrollers(tPtr
);
4086 tPtr
->flags
.needsLayOut
= False
;
4090 /* find first occurence of a string */
4092 mystrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4097 if(!haystack
|| !needle
|| !end
)
4100 for (ptr
= haystack
; ptr
< end
; ptr
++) {
4102 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4106 if (tolower(*ptr
) == tolower(*needle
) &&
4107 !strncasecmp(ptr
, needle
, len
))
4115 /* find last occurence of a string */
4117 mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4122 if(!haystack
|| !needle
|| !end
)
4125 for (ptr
= haystack
-2; ptr
> end
; ptr
--) {
4127 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4130 if (tolower(*ptr
) == tolower(*needle
) &&
4131 !strncasecmp(ptr
, needle
, len
))
4141 WMFindInTextStream(WMText
*tPtr
, char *needle
, Bool direction
,
4150 if (! (tb
= tPtr
->currentTextBlock
)) {
4151 if (! (tb
= ( (direction
> 0) ?
4152 tPtr
->firstTextBlock
: tPtr
->lastTextBlock
) ) ){
4156 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4157 tb = (direction>0) ? tb->next : tb->prior; */
4158 if(tb
!= tPtr
->lastTextBlock
)
4162 tb
= tPtr
->currentTextBlock
;
4170 if(pos
+1 < tb
->used
)
4173 if(tb
->used
- pos
> 0 && pos
> 0) {
4174 mark
= mystrstr(&tb
->text
[pos
], needle
,
4175 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
4188 mark
= mystrrstr(&tb
->text
[pos
], needle
,
4189 strlen(needle
), tb
->text
, caseSensitive
);
4201 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
4203 tPtr
->tpos
= (int)(mark
- tb
->text
);
4204 tPtr
->currentTextBlock
= tb
;
4205 updateCursorPosition(tPtr
);
4206 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
4207 tPtr
->sel
.h
= tPtr
->cursor
.h
-10;
4208 tPtr
->sel
.x
= tPtr
->cursor
.x
+1;
4209 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
4210 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
4211 tPtr
->docWidth
- tPtr
->sel
.x
);
4212 tPtr
->flags
.ownsSelection
= True
;
4219 tb
= (direction
>0) ? tb
->next
: tb
->prior
;
4221 pos
= (direction
>0) ? 0 : tb
->used
;
4230 WMReplaceTextSelection(WMText
*tPtr
, char *replacement
)
4232 if (!tPtr
->flags
.ownsSelection
)
4235 removeSelection(tPtr
);
4238 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
4239 updateCursorPosition(tPtr
);