2 /* WINGs WMText: multi-line/font/color/graphic text widget, by Nwanua. */
6 #include <X11/keysym.h>
13 * - FIX wrap... long lines that don't fit are not char wrapped yet.
14 * - hrm... something to do with already having tbs...
15 * - selection code... selects can be funny if it crosses over. use rect?
16 *- also inspect behaviour for WACenter and WARight
17 * - FIX: graphix blocks MUST be skipped if monoFont even though they exist!
18 * - check if support for Horizontal Scroll is complete
19 * - assess danger of destroying widgets whose actions link to other pages
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 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 :-) */
94 typedef struct W_Text
{
95 W_Class widgetClass
; /* the class number of this widget */
96 W_View
*view
; /* the view referring to this instance */
98 WMRuler
*ruler
; /* the ruler widget to manipulate paragraphs */
100 WMScroller
*vS
; /* the vertical scroller */
101 unsigned int vpos
; /* the current vertical position */
102 unsigned int prevVpos
; /* the previous vertical position */
104 WMScroller
*hS
; /* the horizontal scroller */
105 unsigned int hpos
; /* the current horizontal position */
106 unsigned int prevHpos
; /* the previous horizontal position */
108 WMFont
*dFont
; /* the default font */
109 WMColor
*dColor
; /* the default color */
110 WMPixmap
*dBulletPix
; /* the default pixmap for bullets */
112 GC bgGC
; /* the background GC to draw with */
113 GC fgGC
; /* the foreground GC to draw with */
114 Pixmap db
; /* the buffer on which to draw */
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) { WMNotification *notif = WMCreateNotification(N,T,A);\
173 if ((T)->delegate && (T)->delegate->C)\
174 (*(T)->delegate->C)((T)->delegate,notif);\
175 WMPostNotification(notif);\
176 WMReleaseNotification(notif);}
182 output(char *ptr
, int len
)
187 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
193 #define CURSOR_BLINK_ON_DELAY 600
194 #define CURSOR_BLINK_OFF_DELAY 400
197 static char *default_bullet
[] = {
199 " c None s None", ". c black",
200 "X c white", "o c #808080",
208 static void handleEvents(XEvent
*event
, void *data
);
209 static void layOutDocument(Text
*tPtr
);
210 static void updateScrollers(Text
*tPtr
);
214 getMarginNumber(Text
*tPtr
, WMRulerMargins
*margins
)
218 for(i
=0; i
< tPtr
->nMargins
; i
++) {
220 if(WMIsMarginEqualToMargin(&tPtr
->margins
[i
], margins
))
230 newMargin(Text
*tPtr
, WMRulerMargins
*margins
)
235 tPtr
->margins
[0].retainCount
++;
239 n
= getMarginNumber(tPtr
, margins
);
243 tPtr
->margins
= wrealloc(tPtr
->margins
,
244 (++tPtr
->nMargins
)*sizeof(WMRulerMargins
));
246 n
= tPtr
->nMargins
-1;
247 tPtr
->margins
[n
].left
= margins
->left
;
248 tPtr
->margins
[n
].first
= margins
->first
;
249 tPtr
->margins
[n
].body
= margins
->body
;
250 tPtr
->margins
[n
].right
= margins
->right
;
251 /* for each tab... */
252 tPtr
->margins
[n
].retainCount
= 1;
254 tPtr
->margins
[n
].retainCount
++;
261 sectionWasSelected(Text
*tPtr
, TextBlock
*tb
, XRectangle
*rect
, int s
)
263 unsigned short i
, w
, lw
, selected
= False
, extend
= False
;
267 /* if selection rectangle completely encloses the section */
268 if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
269 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
270 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
272 sel
.w
= tPtr
->visible
.w
;
273 selected
= extend
= True
;
275 /* or if it starts on a line and then goes further down */
276 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
277 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
278 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
279 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
280 >= tPtr
->visible
.y
+ tPtr
->sel
.y
) ) {
281 sel
.x
= WMAX(tPtr
->sel
.x
, tPtr
->clicked
.x
);
282 sel
.w
= tPtr
->visible
.w
;
283 selected
= extend
= True
;
285 /* or if it begins before a line, but ends on it */
286 } else if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
287 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
288 >= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
289 && (tb
->sections
[s
]._y
290 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
292 if (1||tPtr
->sel
.x
+ tPtr
->sel
.w
> tPtr
->clicked
.x
)
293 sel
.w
= tPtr
->sel
.x
+ tPtr
->sel
.w
;
300 /* or if the selection rectangle lies entirely within a line */
301 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
302 && (tPtr
->sel
.w
>= 2)
303 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
304 >= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
313 /* if not within (modified) selection rectangle */
314 if ( tb
->sections
[s
].x
> sel
.x
+ sel
.w
315 || tb
->sections
[s
].x
+ tb
->sections
[s
].w
< sel
.x
)
319 if ( tb
->sections
[s
].x
+ tb
->sections
[s
].w
<= sel
.x
+ sel
.w
320 && tb
->sections
[s
].x
>= sel
.x
) {
321 rect
->width
= tb
->sections
[s
].w
;
322 rect
->x
= tb
->sections
[s
].x
;
327 i
= tb
->sections
[s
].begin
;
330 if (0&& tb
->sections
[s
].x
>= sel
.x
) {
331 tb
->s_begin
= tb
->sections
[s
].begin
;
335 while (++i
<= tb
->sections
[s
].end
) {
337 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
-1]), 1);
340 if (lw
+ tb
->sections
[s
].x
>= sel
.x
341 || i
== tb
->sections
[s
].end
) {
344 tb
->s_begin
= (tb
->selected
? WMIN(tb
->s_begin
, i
) : i
);
349 if (i
> tb
->sections
[s
].end
) {
350 printf("WasSelected: (i > tb->sections[s].end) \n");
354 _selEnd
: rect
->x
= tb
->sections
[s
].x
+ lw
;
356 while(++i
<= tb
->sections
[s
].end
) {
358 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
-1]), 1);
361 if (lw
+ rect
->x
>= sel
.x
+ sel
.w
362 || i
== tb
->sections
[s
].end
) {
364 if (i
!= tb
->sections
[s
].end
) {
370 if (tb
->sections
[s
].last
&& sel
.x
+ sel
.w
371 >= tb
->sections
[s
].x
+ tb
->sections
[s
].w
373 rect
->width
+= (tPtr
->visible
.w
- rect
->x
- lw
);
376 tb
->s_end
= (tb
->selected
? WMAX(tb
->s_end
, i
) : i
);
382 rect
->y
= tb
->sections
[s
]._y
- tPtr
->vpos
;
383 rect
->height
= tb
->sections
[s
].h
;
384 if(tb
->graphic
) { printf("graphic s%d h%d\n", s
,tb
->sections
[s
].h
);}
391 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
]), tb
->d
.font
, tb
->color
,
428 False
, (tb
->s_end
- tb
->s_begin
));
430 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
431 &(tb
->text
[tb
->s_begin
]),
432 (isFont
?font
:tb
->d
.font
),
433 (isFont
?tb
->color
:color
),
434 False
, (tb
->s_end
- tb
->s_begin
));
439 if(underlined
!= -1) {
440 midtb
->underlined
= underlined
;
442 midtb
->underlined
= otb
->underlined
;
445 midtb
->selected
= !True
;
447 midtb
->s_end
= midtb
->used
;
448 tPtr
->currentTextBlock
= tb
;
449 WMAppendTextBlock(tPtr
, midtb
);
450 tb
= tPtr
->currentTextBlock
;
453 if (otb
->used
- otb
->s_end
> 0) {
456 WMCreateTextBlockWithText(tPtr
,
457 &(otb
->text
[otb
->s_end
]), otb
->d
.font
, otb
->color
,
458 False
, otb
->used
- otb
->s_end
);
461 ntb
->underlined
= otb
->underlined
;
462 ntb
->selected
= False
;
463 WMAppendTextBlock(tPtr
, ntb
);
464 tb
= tPtr
->currentTextBlock
;
469 tPtr
->currentTextBlock
= midtb
;
472 otb
->selected
= False
;
473 otb
->used
= otb
->s_begin
;
480 tPtr
->flags
.needsLayOut
= True
;
483 /* in case the size changed... */
484 if(isFont
&& tPtr
->currentTextBlock
) {
485 TextBlock
*tb
= tPtr
->currentTextBlock
;
487 printf("%d %d %d\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
488 tPtr
->sel
.y
= 3 + tb
->sections
[0]._y
;
489 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
- tb
->sections
[0]._y
;
490 tPtr
->sel
.w
= tb
->sections
[tb
->nsections
-1].w
;
491 if(tb
->sections
[tb
->nsections
-1]._y
!= tb
->sections
[0]._y
) {
494 printf("%d %d %d\n\n\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
501 removeSelection(Text
*tPtr
)
503 TextBlock
*tb
= NULL
;
506 if (!(tb
= tPtr
->firstTextBlock
))
511 if(!first
&& !tb
->graphic
) {
512 WMReleaseFont(tPtr
->dFont
);
513 tPtr
->dFont
= WMRetainFont(tb
->d
.font
);
517 if ( (tb
->s_end
- tb
->s_begin
== tb
->used
) || tb
->graphic
) {
518 tPtr
->currentTextBlock
= tb
;
519 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
520 tb
= tPtr
->currentTextBlock
;
525 } else if (tb
->s_end
<= tb
->used
) {
526 memmove(&(tb
->text
[tb
->s_begin
]),
527 &(tb
->text
[tb
->s_end
]), tb
->used
- tb
->s_end
);
528 tb
->used
-= (tb
->s_end
- tb
->s_begin
);
529 tb
->selected
= False
;
530 tPtr
->tpos
= tb
->s_begin
;
541 getFirstNonGraphicBlockFor(TextBlock
*tb
, short dir
)
543 TextBlock
*hold
= tb
;
551 tb
= (dir
? tb
->next
: tb
->prior
);
559 tb
= (dir
? tb
->prior
: tb
->next
);
570 updateStartForCurrentTextBlock(Text
*tPtr
, int x
, int y
, int *dir
,
573 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
574 tb
= getFirstNonGraphicBlockFor(tb
, *dir
);
576 tPtr
->currentTextBlock
=
577 (dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
583 *dir
= !(y
<= tb
->sections
[0].y
);
585 if ( ( y
<= tb
->sections
[0]._y
+ tb
->sections
[0].h
)
586 && (y
>= tb
->sections
[0]._y
) ) {
587 /* if it's on the same line */
588 if(x
< tb
->sections
[0].x
)
592 if ( ( y
<= tb
->sections
[tb
->nsections
-1]._y
593 + tb
->sections
[tb
->nsections
-1].h
)
594 && (y
>= tb
->sections
[tb
->nsections
-1]._y
) ) {
595 /* if it's on the same line */
596 if(x
> tb
->sections
[tb
->nsections
-1].x
)
606 paintText(Text
*tPtr
)
612 int len
, y
, c
, s
, done
=False
, prev_y
=-23, dir
/* 1 = down */;
613 WMScreen
*scr
= tPtr
->view
->screen
;
614 Display
*dpy
= tPtr
->view
->screen
->display
;
615 Window win
= tPtr
->view
->window
;
617 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
620 XFillRectangle(dpy
, tPtr
->db
, tPtr
->bgGC
,
621 0, 0, tPtr
->visible
.w
, tPtr
->visible
.h
);
623 if (! (tb
= tPtr
->currentTextBlock
)) {
624 if (! (tb
= tPtr
->firstTextBlock
)) {
629 if (tPtr
->flags
.ownsSelection
)
630 greyGC
= WMColorGC(WMGrayColor(scr
));
636 /* first, which direction? Don't waste time looking all over,
637 since the parts to be drawn will most likely be near what
638 was previously drawn */
639 if(!updateStartForCurrentTextBlock(tPtr
, 0, tPtr
->vpos
, &dir
, tb
))
644 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
648 if(tPtr
->vpos
<= tb
->sections
[tb
->nsections
-1]._y
649 + tb
->sections
[tb
->nsections
-1].h
)
652 if(tPtr
->vpos
>= tb
->sections
[tb
->nsections
-1]._y
653 + tb
->sections
[tb
->nsections
-1].h
)
670 /* first, place all text that can be viewed */
671 while (!done
&& tb
) {
673 /* paragraph diagnostic
674 if(tb->blank) {tb->text[0] = 'F'; } */
681 tb
->selected
= False
;
683 for(s
=0; s
<tb
->nsections
&& !done
; s
++) {
685 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
690 if ( tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
693 if (tPtr
->flags
.monoFont
) {
698 gc
= WMColorGC(tb
->color
);
701 if (tPtr
->flags
.ownsSelection
) {
704 if ( sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
706 XFillRectangle(dpy
, tPtr
->db
, greyGC
,
707 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
711 prev_y
= tb
->sections
[s
]._y
;
713 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
714 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
715 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
716 WMDrawString(scr
, tPtr
->db
, gc
, font
,
717 tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
719 if (tb
->underlined
) {
720 XDrawLine(dpy
, tPtr
->db
, gc
,
721 tb
->sections
[s
].x
- tPtr
->hpos
,
723 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
,
729 tb
= (!done
? tb
->next
: NULL
);
733 /* now , show all graphic items that can be viewed */
734 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
735 if (c
> 0 && !tPtr
->flags
.monoFont
) {
739 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
741 /* if it's not viewable, and mapped, unmap it */
742 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
743 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
746 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
747 WMUnmapWidget(tb
->d
.widget
);
751 /* if it's viewable, and not mapped, map it */
753 W_View
*view
= W_VIEW(tb
->d
.widget
);
755 if (!view
->flags
.realized
)
756 WMRealizeWidget(tb
->d
.widget
);
757 if(!view
->flags
.mapped
) {
758 XMapWindow(view
->screen
->display
, view
->window
);
759 XFlush(view
->screen
->display
);
760 view
->flags
.mapped
= 1;
764 if (tPtr
->flags
.ownsSelection
) {
767 if ( sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
769 XFillRectangle(dpy
, tPtr
->db
, greyGC
,
770 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
775 WMMoveWidget(tb
->d
.widget
,
776 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
777 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
778 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
781 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
782 tb
->sections
[0].x
- tPtr
->hpos
,
783 tb
->sections
[0].y
- tPtr
->vpos
);
784 h
= tb
->d
.pixmap
->height
+ 1;
788 if (tb
->underlined
) {
789 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
790 tb
->sections
[0].x
- tPtr
->hpos
,
791 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
792 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
793 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
798 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
799 && tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
800 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
801 XDrawLine(dpy
, tPtr
->db
, tPtr
->fgGC
,
803 tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
806 XCopyArea(dpy
, tPtr
->db
, win
, tPtr
->bgGC
, 0, 0,
807 tPtr
->visible
.w
, tPtr
->visible
.h
,
808 tPtr
->visible
.x
, tPtr
->visible
.y
);
810 W_DrawRelief(scr
, win
, 0, 0,
811 tPtr
->view
->size
.width
, tPtr
->view
->size
.height
,
814 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
815 XDrawLine(dpy
, win
, tPtr
->fgGC
,
816 2, 42, tPtr
->view
->size
.width
-4, 42);
821 mouseOverObject(Text
*tPtr
, int x
, int y
)
826 x
-= tPtr
->visible
.x
;
828 y
-= tPtr
->visible
.y
;
831 if(tPtr
->flags
.ownsSelection
) {
834 && tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
835 && tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
836 tPtr
->flags
.isOverGraphic
= 1;
843 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
846 tPtr
->flags
.isOverGraphic
= 0;
850 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
852 if(!tb
|| !tb
->sections
) {
853 tPtr
->flags
.isOverGraphic
= 0;
858 if(tb
->sections
[0].x
<= x
859 && tb
->sections
[0].y
<= y
860 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
861 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
862 tPtr
->flags
.isOverGraphic
= 3;
873 tPtr
->flags
.isOverGraphic
= 0;
876 tPtr
->view
->attribs
.cursor
= (result
?
877 tPtr
->view
->screen
->defaultCursor
878 : tPtr
->view
->screen
->textCursor
);
880 XSetWindowAttributes attribs
;
881 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
882 XChangeWindowAttributes(tPtr
->view
->screen
->display
,
883 tPtr
->view
->window
, CWCursor
,
891 blinkCursor(void *data
)
893 Text
*tPtr
= (Text
*)data
;
895 if (tPtr
->flags
.cursorShown
) {
896 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
,
899 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
,
903 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
908 updateCursorPosition(Text
*tPtr
)
910 TextBlock
*tb
= NULL
;
913 if(tPtr
->flags
.needsLayOut
)
914 layOutDocument(tPtr
);
916 if (! (tb
= tPtr
->currentTextBlock
)) {
917 if (! (tb
= tPtr
->firstTextBlock
)) {
919 tPtr
->cursor
.h
= tPtr
->dFont
->height
;
929 y
= tb
->sections
[0].y
;
930 h
= tb
->sections
[0].h
;
931 x
= tb
->sections
[0].x
;
933 } else if(tb
->graphic
) {
934 y
= tb
->sections
[0].y
;
935 h
= tb
->sections
[0].h
;
936 x
= tb
->sections
[0].x
;
939 if(tPtr
->tpos
> tb
->used
)
940 tPtr
->tpos
= tb
->used
;
942 for(s
=0; s
<tb
->nsections
-1; s
++) {
944 if(tPtr
->tpos
>= tb
->sections
[s
].begin
945 && tPtr
->tpos
<= tb
->sections
[s
].end
)
949 y
= tb
->sections
[s
]._y
;
950 h
= tb
->sections
[s
].h
;
951 x
= tb
->sections
[s
].x
+ WMWidthOfString(
952 (tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
),
953 &tb
->text
[tb
->sections
[s
].begin
],
954 tPtr
->tpos
- tb
->sections
[s
].begin
);
962 /* scroll the bars if the cursor is not visible */
963 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
964 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
965 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
967 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
968 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
969 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
970 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
975 updateScrollers(tPtr
);
980 cursorToTextPosition(Text
*tPtr
, int x
, int y
)
982 TextBlock
*tb
= NULL
;
983 int done
=False
, s
, pos
, len
, _w
, _y
, dir
=1; /* 1 == "down" */
986 if(tPtr
->flags
.needsLayOut
)
987 layOutDocument(tPtr
);
989 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
993 x
-= (tPtr
->visible
.x
- 2);
997 /* clicked is relative to document, not window... */
1001 if (! (tb
= tPtr
->currentTextBlock
)) {
1002 if (! (tb
= tPtr
->firstTextBlock
)) {
1004 tPtr
->cursor
.h
= tPtr
->dFont
->height
;
1011 /* first, which direction? Most likely, newly clicked
1012 position will be close to previous */
1013 if(!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
1017 s
= (dir
? 0 : tb
->nsections
-1);
1018 if ( y
>= tb
->sections
[s
]._y
1019 && y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1023 /* get the first (or last) section of the TextBlock that
1024 lies about the vertical click point */
1026 while (!done
&& tb
) {
1028 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1029 if( (dir
?tb
->next
:tb
->prior
))
1030 tb
= (dir
?tb
->next
:tb
->prior
);
1034 s
= (dir
? 0 : tb
->nsections
-1);
1035 while (!done
&& (dir
? (s
<tb
->nsections
) : (s
>=0) )) {
1037 if ( (dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) :
1038 ( y
>= tb
->sections
[s
]._y
) ) ) {
1046 if ( (dir
? tb
->next
: tb
->prior
)) {
1047 tb
= (dir
? tb
->next
: tb
->prior
);
1050 break; /* goto _doneH; */
1056 if (s
<0 || s
>=tb
->nsections
) {
1057 s
= (dir
? tb
->nsections
-1 : 0);
1061 /* we have the line, which TextBlock on that line is it? */
1062 pos
= (dir
?0:tb
->sections
[s
].begin
);
1063 if (tPtr
->flags
.monoFont
&& tb
->graphic
)
1064 tb
= getFirstNonGraphicBlockFor(tb
, dir
);
1069 _y
= tb
->sections
[s
]._y
;
1073 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1074 tb
= (dir
? tb
->next
: tb
->prior
);
1081 _w
= WMWidgetWidth(tb
->d
.widget
)-5;
1083 _w
= tb
->d
.pixmap
->width
-5;
1085 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1086 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1087 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1088 if (tb
->sections
[s
].x
+ _w
>= x
)
1093 if (tb
->sections
[s
].x
<= x
)
1097 if ((dir
? tb
->next
: tb
->prior
)) {
1098 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1099 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1100 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1102 pos
= (dir
?0:tb
->sections
[s
].begin
);
1103 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1108 if (_y
!= nxt
->sections
[dir
?0:nxt
->nsections
-1]._y
) {
1109 /* this must be the last/first on this line. stop */
1110 pos
= (dir
? tb
->sections
[s
].end
: 0);
1111 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1115 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1117 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1118 } else if (pos
> tb
->sections
[s
].begin
) {
1120 WMWidthOfString(tb
->d
.font
,
1121 &(tb
->text
[tb
->sections
[s
].begin
]),
1122 pos
- tb
->sections
[s
].begin
);
1129 if ( (dir
? tb
->next
: tb
->prior
)) {
1130 tb
= (dir
? tb
->next
: tb
->prior
);
1137 s
= (dir
? 0 : tb
->nsections
-1);
1140 /* we have said TextBlock, now where within it? */
1141 if (tb
&& !tb
->graphic
) {
1142 WMFont
*f
= tb
->d
.font
;
1143 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1144 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1146 _w
= x
- tb
->sections
[s
].x
;
1149 while (pos
<len
&& WMWidthOfString(f
, text
, pos
+1) < _w
)
1152 tPtr
->cursor
.x
= tb
->sections
[s
].x
+
1153 (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1155 pos
+= tb
->sections
[s
].begin
;
1157 tPtr
->tpos
= (pos
<tb
->used
)? pos
: tb
->used
;
1161 printf("...for this app will surely crash :-)\n");
1163 tPtr
->currentTextBlock
= tb
;
1164 tPtr
->cursor
.h
= tb
->sections
[s
].h
;
1165 tPtr
->cursor
.y
= tb
->sections
[s
]._y
;
1167 /* scroll the bars if the cursor is not visible */
1168 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1169 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1170 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1172 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1173 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1174 updateScrollers(tPtr
);
1175 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1176 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1177 updateScrollers(tPtr
);
1185 autoSelectText(Text
*tPtr
, int clicks
)
1189 char *mark
= NULL
, behind
, ahead
;
1191 if(!(tb
= tPtr
->currentTextBlock
))
1197 switch(tb
->text
[tPtr
->tpos
]) {
1200 case '<': case '>': behind = '<'; ahead = '>'; break;
1201 case '{': case '}': behind = '{'; ahead = '}'; break;
1202 case '[': case ']': behind = '['; ahead = ']'; break;
1204 default: behind
= ahead
= ' ';
1207 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
1208 tPtr
->sel
.h
= 6;/*tPtr->cursor.h-10;*/
1211 tPtr
->sel
.x
= tb
->sections
[0].x
;
1212 tPtr
->sel
.w
= tb
->sections
[0].w
;
1214 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
1217 while(start
> 0 && tb
->text
[start
-1] != behind
)
1221 if(tPtr
->tpos
> start
){
1222 x
-= WMWidthOfString(font
, &tb
->text
[start
],
1223 tPtr
->tpos
- start
);
1225 tPtr
->sel
.x
= (x
<0?0:x
)+1;
1227 if((mark
= strchr(&tb
->text
[start
], ahead
))) {
1228 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
1229 (int)(mark
- &tb
->text
[start
]));
1230 } else if(tb
->used
> start
) {
1231 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
1236 } else if(clicks
== 3) {
1237 TextBlock
*cur
= tb
;
1239 while(tb
&& !tb
->first
) {
1242 tPtr
->sel
.y
= tb
->sections
[0]._y
;
1245 while(tb
->next
&& !tb
->next
->first
) {
1248 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
1252 tPtr
->sel
.w
= tPtr
->docWidth
;
1253 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
1256 tPtr
->flags
.ownsSelection
= True
;
1263 updateScrollers(Text
*tPtr
)
1266 if (tPtr
->flags
.frozen
)
1270 if (tPtr
->docHeight
< tPtr
->visible
.h
) {
1271 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
1274 float hmax
= (float)(tPtr
->docHeight
);
1275 WMSetScrollerParameters(tPtr
->vS
,
1276 ((float)tPtr
->vpos
)/(hmax
- (float)tPtr
->visible
.h
),
1277 (float)tPtr
->visible
.h
/hmax
);
1279 } else tPtr
->vpos
= 0;
1282 if (tPtr
->docWidth
< tPtr
->visible
.w
) {
1283 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
1286 float wmax
= (float)(tPtr
->docWidth
);
1287 WMSetScrollerParameters(tPtr
->hS
,
1288 ((float)tPtr
->hpos
)/(wmax
- (float)tPtr
->visible
.w
),
1289 (float)tPtr
->visible
.w
/wmax
);
1291 } else tPtr
->hpos
= 0;
1295 scrollersCallBack(WMWidget
*w
, void *self
)
1297 Text
*tPtr
= (Text
*)self
;
1298 Bool scroll
= False
;
1301 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1304 if (w
== tPtr
->vS
) {
1306 height
= tPtr
->visible
.h
;
1308 which
= WMGetScrollerHitPart(tPtr
->vS
);
1311 case WSDecrementLine
:
1312 if (tPtr
->vpos
> 0) {
1313 if (tPtr
->vpos
>16) tPtr
->vpos
-=16;
1319 case WSIncrementLine
: {
1320 int limit
= tPtr
->docHeight
- height
;
1321 if (tPtr
->vpos
< limit
) {
1322 if (tPtr
->vpos
<limit
-16) tPtr
->vpos
+=16;
1323 else tPtr
->vpos
=limit
;
1329 case WSDecrementPage
:
1330 if(((int)tPtr
->vpos
- (int)height
) >= 0)
1331 tPtr
->vpos
-= height
;
1338 case WSIncrementPage
:
1339 tPtr
->vpos
+= height
;
1340 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1341 tPtr
->vpos
= tPtr
->docHeight
- height
;
1347 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1348 * (float)(tPtr
->docHeight
- height
);
1356 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1357 tPtr
->prevVpos
= tPtr
->vpos
;
1361 if (w
== tPtr
->hS
) {
1362 int width
= tPtr
->visible
.w
;
1364 which
= WMGetScrollerHitPart(tPtr
->hS
);
1367 case WSDecrementLine
:
1368 if (tPtr
->hpos
> 0) {
1369 if (tPtr
->hpos
>16) tPtr
->hpos
-=16;
1374 case WSIncrementLine
: {
1375 int limit
= tPtr
->docWidth
- width
;
1376 if (tPtr
->hpos
< limit
) {
1377 if (tPtr
->hpos
<limit
-16) tPtr
->hpos
+=16;
1378 else tPtr
->hpos
=limit
;
1382 case WSDecrementPage
:
1383 if(((int)tPtr
->hpos
- (int)width
) >= 0)
1384 tPtr
->hpos
-= width
;
1391 case WSIncrementPage
:
1392 tPtr
->hpos
+= width
;
1393 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1394 tPtr
->hpos
= tPtr
->docWidth
- width
;
1400 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1401 * (float)(tPtr
->docWidth
- width
);
1409 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1410 tPtr
->prevHpos
= tPtr
->hpos
;
1414 updateScrollers(tPtr
);
1423 unsigned short begin
, end
; /* what part of the text block */
1428 layOutLine(Text
*tPtr
, myLineItems
*items
, int nitems
, int x
, int y
)
1430 int i
, j
=0, lw
= 0, line_height
=0, max_d
=0, len
, n
;
1434 TextBlock
*tbsame
=NULL
;
1436 if(!items
|| nitems
== 0)
1439 for(i
=0; i
<nitems
; i
++) {
1443 if (!tPtr
->flags
.monoFont
) {
1445 WMWidget
*wdt
= tb
->d
.widget
;
1446 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1447 if (tPtr
->flags
.alignment
!= WALeft
)
1448 lw
+= WMWidgetWidth(wdt
);
1450 line_height
= WMAX(line_height
,
1451 tb
->d
.pixmap
->height
+ max_d
);
1452 if (tPtr
->flags
.alignment
!= WALeft
)
1453 lw
+= tb
->d
.pixmap
->width
;
1458 font
= (tPtr
->flags
.monoFont
)?tPtr
->dFont
: tb
->d
.font
;
1459 max_d
= WMAX(max_d
, abs(font
->height
-font
->y
));
1460 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1461 text
= &(tb
->text
[items
[i
].begin
]);
1462 len
= items
[i
].end
- items
[i
].begin
;
1463 if (tPtr
->flags
.alignment
!= WALeft
)
1464 lw
+= WMWidthOfString(font
, text
, len
);
1468 if (tPtr
->flags
.alignment
== WARight
) {
1469 j
= tPtr
->visible
.w
- lw
;
1470 } else if (tPtr
->flags
.alignment
== WACenter
) {
1471 j
= (int) ((float)(tPtr
->visible
.w
- lw
))/2.0;
1474 for(i
=0; i
<nitems
; i
++) {
1477 if (tbsame
== tb
) { /* extend it, since it's on same line */
1478 tb
->sections
[tb
->nsections
-1].end
= items
[i
].end
;
1479 n
= tb
->nsections
-1;
1481 tb
->sections
= wrealloc(tb
->sections
,
1482 (++tb
->nsections
)*sizeof(Section
));
1483 n
= tb
->nsections
-1;
1484 tb
->sections
[n
]._y
= y
+ max_d
;
1485 tb
->sections
[n
].max_d
= max_d
;
1486 tb
->sections
[n
].x
= x
+j
;
1487 tb
->sections
[n
].h
= line_height
;
1488 tb
->sections
[n
].begin
= items
[i
].begin
;
1489 tb
->sections
[n
].end
= items
[i
].end
;
1492 tb
->sections
[n
].last
= (i
+1 == nitems
);
1495 if (!tPtr
->flags
.monoFont
) {
1497 WMWidget
*wdt
= tb
->d
.widget
;
1498 tb
->sections
[n
].y
= max_d
+ y
1499 + line_height
- WMWidgetHeight(wdt
);
1500 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1502 tb
->sections
[n
].y
= y
+ line_height
1503 + max_d
- tb
->d
.pixmap
->height
;
1504 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1506 x
+= tb
->sections
[n
].w
;
1509 font
= (tPtr
->flags
.monoFont
)? tPtr
->dFont
: tb
->d
.font
;
1510 len
= items
[i
].end
- items
[i
].begin
;
1511 text
= &(tb
->text
[items
[i
].begin
]);
1513 tb
->sections
[n
].y
= y
+line_height
-font
->y
;
1515 WMWidthOfString(font
,
1516 &(tb
->text
[tb
->sections
[n
].begin
]),
1517 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1519 x
+= WMWidthOfString(font
, text
, len
);
1531 layOutDocument(Text
*tPtr
)
1534 myLineItems
*items
= NULL
;
1535 unsigned int itemsSize
=0, nitems
=0, begin
, end
;
1537 unsigned int x
, y
=0, lw
= 0, width
=0, bmargin
;
1538 char *start
=NULL
, *mark
=NULL
;
1540 if ( tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)) )
1543 tPtr
->docWidth
= tPtr
->visible
.w
;
1544 x
= tPtr
->margins
[tb
->marginN
].first
;
1545 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1547 /* only partial layOut needed: re-Lay only affected textblocks */
1548 if (tPtr
->flags
.laidOut
) {
1549 tb
= tPtr
->currentTextBlock
;
1551 /* search backwards for textblocks on same line */
1553 if (!tb
->sections
|| tb
->nsections
<1) {
1554 tb
= tPtr
->firstTextBlock
;
1555 tPtr
->flags
.laidOut
= False
;
1560 if(!tb
->prior
->sections
|| tb
->prior
->nsections
<1) {
1561 tb
= tPtr
->firstTextBlock
;
1562 tPtr
->flags
.laidOut
= False
;
1567 if (tb
->sections
[0]._y
!=
1568 tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
) {
1574 if(tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
>0) {
1575 y
= tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
+
1576 tb
->prior
->sections
[tb
->prior
->nsections
-1].h
-
1577 tb
->prior
->sections
[tb
->prior
->nsections
-1].max_d
;
1586 if (tb
->sections
&& tb
->nsections
>0) {
1587 wfree(tb
->sections
);
1588 tb
->sections
= NULL
;
1592 if (tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1593 TextBlock
*next
= tb
->next
;
1594 tPtr
->currentTextBlock
= tb
;
1595 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1601 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1602 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1603 x
= tPtr
->margins
[tb
->marginN
].first
;
1604 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1610 if (!tPtr
->flags
.monoFont
) {
1612 width
= WMWidgetWidth(tb
->d
.widget
);
1614 width
= tb
->d
.pixmap
->width
;
1616 if (width
> tPtr
->docWidth
)
1617 tPtr
->docWidth
= width
;
1620 if (lw
>= tPtr
->visible
.w
- x
) {
1621 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1627 if(nitems
+ 1> itemsSize
) {
1628 printf("realloc %d nitems\n", nitems
);
1629 items
= wrealloc(items
,
1630 (++itemsSize
)*sizeof(myLineItems
));
1633 items
[nitems
].tb
= tb
;
1634 items
[nitems
].begin
= 0;
1635 items
[nitems
].end
= 0;
1639 } else if ((start
= tb
->text
)) {
1641 font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
1644 mark
= strchr(start
, ' ');
1646 end
+= (int)(mark
-start
)+1;
1649 end
+= strlen(start
);
1656 if (end
-begin
> 0) {
1658 width
= WMWidthOfString(font
,
1659 &tb
->text
[begin
], end
-begin
);
1661 /* if it won't fit, break it up */
1662 if (width
> tPtr
->visible
.w
) {
1663 char *t
= &tb
->text
[begin
];
1664 int l
=end
-begin
, i
=0;
1665 printf("%d > %d\n", width
, tPtr
->visible
.w
);
1667 width
= WMWidthOfString(font
, t
, ++i
);
1668 } while (width
< tPtr
->visible
.w
&& i
< l
);
1677 if (lw
>= tPtr
->visible
.w
- x
) {
1678 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1684 if(nitems
+ 1 > itemsSize
) {
1685 items
= wrealloc(items
,
1686 (++itemsSize
)*sizeof(myLineItems
));
1689 items
[nitems
].tb
= tb
;
1690 items
[nitems
].begin
= begin
;
1691 items
[nitems
].end
= end
;
1699 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1700 if(0&&tPtr
->flags
.laidOut
1701 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
>0
1702 && (tPtr
->vpos
+ tPtr
->visible
.h
1703 < tb
->next
->sections
[0]._y
)) {
1704 if(tPtr
->lastTextBlock
->sections
1705 && tPtr
->lastTextBlock
->nsections
> 0 ) {
1706 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1707 int ly
= ltb
->sections
[ltb
->nsections
-1]._y
;
1708 int lh
= ltb
->sections
[ltb
->nsections
-1].h
;
1711 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
;
1712 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
);
1714 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1716 sd
= tPtr
->docHeight
-y
;
1718 printf("dif %d-%d: %d\n", ss
, sd
, ss
-sd
);
1719 y
+= tb
->next
->sections
[0]._y
-y
;
1721 printf("nitems%d\n", nitems
);
1723 y
= tPtr
->docHeight
+ss
-sd
;
1727 tPtr
->flags
.laidOut
= False
;
1736 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1738 if (tPtr
->docHeight
!= y
+10) {
1739 tPtr
->docHeight
= y
+10;
1740 updateScrollers(tPtr
);
1743 if(tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1746 tPtr
->flags
.horizOnDemand
= True
;
1747 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, True
);
1748 event
.type
= Expose
;
1749 handleEvents(&event
, (void *)tPtr
);
1751 } else if(tPtr
->docWidth
<= tPtr
->visible
.w
1752 && tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1753 tPtr
->flags
.horizOnDemand
= False
;
1754 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, False
);
1757 tPtr
->flags
.laidOut
= True
;
1759 if(items
&& itemsSize
> 0)
1765 textDidResize(W_ViewDelegate
*self
, WMView
*view
)
1767 Text
*tPtr
= (Text
*)view
->self
;
1768 unsigned short w
= tPtr
->view
->size
.width
;
1769 unsigned short h
= tPtr
->view
->size
.height
;
1770 unsigned short rh
= 0, vw
= 0, rel
;
1772 rel
= (tPtr
->flags
.relief
== WRFlat
);
1774 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1775 WMMoveWidget(tPtr
->ruler
, 2, 2);
1776 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1781 WMMoveWidget(tPtr
->vS
, 1 - (rel
?1:0), rh
+ 1 - (rel
?1:0));
1782 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
?2:0));
1784 WMSetRulerOffset(tPtr
->ruler
,22);
1785 } else WMSetRulerOffset(tPtr
->ruler
, 2);
1789 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1790 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1792 WMMoveWidget(tPtr
->hS
, vw
+1, h
- 21);
1793 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1797 tPtr
->visible
.x
= (tPtr
->vS
)?24:4;
1798 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)?43:3;
1799 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1800 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1801 tPtr
->visible
.h
-= (tPtr
->hS
)?20:0;
1802 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1804 if (tPtr
->view
->flags
.realized
) {
1807 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1808 tPtr
->db
= (Pixmap
) NULL
;
1811 if (tPtr
->visible
.w
< 40)
1812 tPtr
->visible
.w
= 40;
1813 if (tPtr
->visible
.h
< 20)
1814 tPtr
->visible
.h
= 20;
1817 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1818 tPtr
->view
->window
, tPtr
->visible
.w
,
1819 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1826 W_ViewDelegate _TextViewDelegate
=
1834 /* nice, divisble-by-16 blocks */
1835 static inline unsigned short
1836 reqBlockSize(unsigned short requested
)
1838 return requested
+ 16 - (requested
%16);
1843 clearText(Text
*tPtr
)
1845 tPtr
->vpos
= tPtr
->hpos
= 0;
1846 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1848 if (!tPtr
->firstTextBlock
)
1851 while (tPtr
->currentTextBlock
)
1852 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1854 tPtr
->firstTextBlock
= NULL
;
1855 tPtr
->currentTextBlock
= NULL
;
1856 tPtr
->lastTextBlock
= NULL
;
1857 WMEmptyArray(tPtr
->gfxItems
);
1861 deleteTextInteractively(Text
*tPtr
, KeySym ksym
)
1864 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1868 if (!tPtr
->flags
.editable
) {
1869 XBell(tPtr
->view
->screen
->display
, 0);
1873 if ( !(tb
= tPtr
->currentTextBlock
) )
1876 if (tPtr
->flags
.ownsSelection
) {
1877 if(removeSelection(tPtr
))
1878 layOutDocument(tPtr
);
1882 wasFirst
= tb
->first
;
1883 if (back
&& tPtr
->tpos
< 1) {
1885 if(tb
->prior
->blank
) {
1886 tPtr
->currentTextBlock
= tb
->prior
;
1887 WMRemoveTextBlock(tPtr
);
1888 tPtr
->currentTextBlock
= tb
;
1890 layOutDocument(tPtr
);
1894 TextBlock
*prior
= tb
->prior
;
1895 tPtr
->currentTextBlock
= tb
;
1896 WMRemoveTextBlock(tPtr
);
1902 tPtr
->tpos
= tb
->used
;
1903 tPtr
->currentTextBlock
= tb
;
1907 tb
->next
->first
= False
;
1908 layOutDocument(tPtr
);
1915 if ( (tb
->used
> 0) && ((back
?tPtr
->tpos
> 0:1))
1916 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1919 memmove(&(tb
->text
[tPtr
->tpos
]),
1920 &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1925 if ( (back
? (tPtr
->tpos
< 1 && !done
) : ( tPtr
->tpos
>= tb
->used
))
1928 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1930 if(tb
->used
== 0 || tb
->graphic
)
1931 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1934 tPtr
->currentTextBlock
= sibling
;
1935 tPtr
->tpos
= (back
? sibling
->used
: 0);
1939 layOutDocument(tPtr
);
1944 insertTextInteractively(Text
*tPtr
, char *text
, int len
)
1947 char *newline
= NULL
;
1949 if (!tPtr
->flags
.editable
) {
1950 XBell(tPtr
->view
->screen
->display
, 0);
1954 if (len
< 1 || !text
)
1958 if(tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1962 if (tPtr
->flags
.ownsSelection
)
1963 removeSelection(tPtr
);
1966 if (tPtr
->flags
.ignoreNewLine
) {
1968 for(i
=0; i
<len
; i
++) {
1969 if (text
[i
] == '\n')
1974 tb
= tPtr
->currentTextBlock
;
1975 if (!tb
|| tb
->graphic
) {
1977 WMAppendTextStream(tPtr
, text
);
1978 layOutDocument(tPtr
);
1982 if ((newline
= strchr(text
, '\n'))) {
1983 int nlen
= (int)(newline
-text
);
1984 int s
= tb
->used
- tPtr
->tpos
;
1986 if (!tb
->blank
&& nlen
>0) {
1988 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
1989 tb
->used
-= (tb
->used
- tPtr
->tpos
);
1991 insertTextInteractively(tPtr
, text
, nlen
);
1993 WMAppendTextStream(tPtr
, newline
);
1995 insertTextInteractively(tPtr
, save
, s
);
1998 if (tPtr
->tpos
>0 && tPtr
->tpos
< tb
->used
1999 && !tb
->graphic
&& tb
->text
) {
2001 void *ntb
= WMCreateTextBlockWithText(
2002 tPtr
, &tb
->text
[tPtr
->tpos
],
2003 tb
->d
.font
, tb
->color
, True
, tb
->used
- tPtr
->tpos
);
2004 tb
->used
= tPtr
->tpos
;
2005 WMAppendTextBlock(tPtr
, ntb
);
2008 } else if (tPtr
->tpos
== tb
->used
|| tPtr
->tpos
== 0) {
2009 if(tPtr
->flags
.indentNewLine
) {
2010 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2011 " ", tb
->d
.font
, tb
->color
, True
, 4));
2014 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2015 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2022 if (tb
->used
+ len
>= tb
->allocated
) {
2023 tb
->allocated
= reqBlockSize(tb
->used
+len
);
2024 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
2028 memcpy(tb
->text
, text
, len
);
2031 tb
->text
[tb
->used
] = 0;
2035 memmove(&(tb
->text
[tPtr
->tpos
+len
]), &tb
->text
[tPtr
->tpos
],
2036 tb
->used
-tPtr
->tpos
+1);
2037 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2040 tb
->text
[tb
->used
] = 0;
2045 layOutDocument(tPtr
);
2050 selectRegion(Text
*tPtr
, int x
, int y
)
2056 y
+= (tPtr
->flags
.rulerShown
? 40: 0);
2059 y
-= 10; /* the original offset */
2061 x
-= tPtr
->visible
.x
-2;
2065 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2066 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2067 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2068 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2070 tPtr
->flags
.ownsSelection
= True
;
2076 releaseSelection(Text
*tPtr
)
2078 TextBlock
*tb
= tPtr
->firstTextBlock
;
2081 tb
->selected
= False
;
2084 tPtr
->flags
.ownsSelection
= False
;
2085 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
,
2093 requestHandler(WMView
*view
, Atom selection
, Atom target
, void *cdata
,
2096 Text
*tPtr
= view
->self
;
2097 Display
*dpy
= tPtr
->view
->screen
->display
;
2099 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2100 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2101 WMData
*data
= NULL
;
2104 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2105 char *text
= WMGetTextSelectedStream(tPtr
);
2108 printf("got text [%s]\n", text
);
2109 data
= WMCreateDataWithBytes(text
, strlen(text
));
2110 WMSetDataFormat(data
, TYPETEXT
);
2114 } else printf("didn't get it\n");
2116 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2117 if (target
== _TARGETS
) {
2120 ptr
= wmalloc(4 * sizeof(Atom
));
2124 ptr
[3] = COMPOUND_TEXT
;
2126 data
= WMCreateDataWithBytes(ptr
, 4*4);
2127 WMSetDataFormat(data
, 32);
2137 lostHandler(WMView
*view
, Atom selection
, void *cdata
)
2139 releaseSelection((WMText
*)view
->self
);
2142 static WMSelectionProcs selectionHandler
= {
2143 requestHandler
, lostHandler
, NULL
2148 ownershipObserver(void *observerData
, WMNotification
*notification
)
2150 if (observerData
!= WMGetNotificationClientData(notification
))
2151 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2156 fontChanged(void *observerData
, WMNotification
*notification
)
2158 WMText
*tPtr
= (WMText
*) observerData
;
2159 WMFont
*font
= (WMFont
*)WMGetNotificationClientData(notification
);
2160 printf("fontChanged\n");
2165 if (tPtr
->flags
.ownsSelection
)
2166 WMSetTextSelectionFont(tPtr
, font
);
2171 handleTextKeyPress(Text
*tPtr
, XEvent
*event
)
2175 int control_pressed
= False
;
2176 TextBlock
*tb
= NULL
;
2178 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2179 control_pressed
= True
;
2180 buffer
[XLookupString(&event
->xkey
, buffer
, 1, &ksym
, NULL
)] = 0;
2185 if(!(tb
= tPtr
->currentTextBlock
))
2191 L_imaGFX
: if(tb
->prior
) {
2192 tPtr
->currentTextBlock
= tb
->prior
;
2193 tPtr
->tpos
= tPtr
->currentTextBlock
->used
-1;
2194 } else tPtr
->tpos
= 0;
2195 } else tPtr
->tpos
--;
2196 updateCursorPosition(tPtr
);
2201 if(!(tb
= tPtr
->currentTextBlock
))
2205 if(tPtr
->tpos
== tb
->used
) {
2206 R_imaGFX
: if(tb
->next
) {
2207 tPtr
->currentTextBlock
= tb
->next
;
2209 } else tPtr
->tpos
= tb
->used
;
2210 } else tPtr
->tpos
++;
2211 updateCursorPosition(tPtr
);
2216 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2217 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2222 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2223 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2230 deleteTextInteractively(tPtr
, ksym
);
2231 updateCursorPosition(tPtr
);
2237 control_pressed
= True
;
2241 insertTextInteractively(tPtr
, " ", 4);
2242 updateCursorPosition(tPtr
);
2249 if (buffer
[0] != 0 && !control_pressed
) {
2250 insertTextInteractively(tPtr
, buffer
, 1);
2251 updateCursorPosition(tPtr
);
2254 } else if (control_pressed
&& ksym
==XK_r
) {
2255 Bool i
= !tPtr
->flags
.rulerShown
;
2256 WMShowTextRuler(tPtr
, i
);
2257 tPtr
->flags
.rulerShown
= i
;
2259 else if (control_pressed
&& buffer
[0] == '\a')
2260 XBell(tPtr
->view
->screen
->display
, 0);
2263 if (!control_pressed
&& tPtr
->flags
.ownsSelection
)
2264 releaseSelection(tPtr
);
2268 handleWidgetPress(XEvent
*event
, void *data
)
2270 TextBlock
*tb
= (TextBlock
*)data
;
2278 tPtr
->currentTextBlock
= tb
;
2279 tPtr
->flags
.isOverGraphic
= 2;
2281 output(tb
->text
, tb
->used
);
2283 if (!tPtr
->flags
.focused
) {
2284 WMSetFocusToWidget(tPtr
);
2285 tPtr
->flags
.focused
= True
;
2292 handleActionEvents(XEvent
*event
, void *data
)
2294 Text
*tPtr
= (Text
*)data
;
2295 Display
*dpy
= event
->xany
.display
;
2299 switch (event
->type
) {
2301 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2302 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2303 tPtr
->flags
.extendSelection
= True
;
2307 if (tPtr
->flags
.focused
) {
2308 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2309 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
2310 GrabModeAsync
, GrabModeAsync
, None
,
2311 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2312 tPtr
->flags
.pointerGrabbed
= True
;
2313 handleTextKeyPress(tPtr
, event
);
2318 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2319 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2320 tPtr
->flags
.extendSelection
= False
;
2322 /* end modify flag so selection can be extended */
2329 if (tPtr
->flags
.pointerGrabbed
) {
2330 tPtr
->flags
.pointerGrabbed
= False
;
2331 XUngrabPointer(dpy
, CurrentTime
);
2334 if(tPtr
->flags
.waitingForSelection
)
2337 if ((event
->xmotion
.state
& Button1Mask
)) {
2338 if (!tPtr
->flags
.ownsSelection
) {
2339 WMCreateSelectionHandler(tPtr
->view
,
2340 XA_PRIMARY
, event
->xbutton
.time
,
2341 &selectionHandler
, NULL
);
2342 tPtr
->flags
.ownsSelection
= True
;
2344 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2348 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2354 if (tPtr
->flags
.pointerGrabbed
) {
2355 tPtr
->flags
.pointerGrabbed
= False
;
2356 XUngrabPointer(dpy
, CurrentTime
);
2360 if (tPtr
->flags
.waitingForSelection
)
2363 if (tPtr
->flags
.extendSelection
) {
2364 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2369 if (event
->xbutton
.button
== Button1
) {
2371 if(WMIsDoubleClick(event
)) {
2372 TextBlock
*tb
= tPtr
->currentTextBlock
;
2374 if(tb
&& tb
->graphic
&& !tb
->object
) {
2375 char desc
[tb
->used
+1];
2376 memcpy(desc
, tb
->text
, tb
->used
);
2378 if(tPtr
->delegate
) {
2379 if(tPtr
->delegate
->didDoubleClickOnPicture
)
2380 (*tPtr
->delegate
->didDoubleClickOnPicture
)
2381 (tPtr
->delegate
, desc
);
2384 autoSelectText(tPtr
, 2);
2386 tPtr
->lastClickTime
= event
->xbutton
.time
;
2388 } else if(event
->xbutton
.time
- tPtr
->lastClickTime
2389 < WINGsConfiguration
.doubleClickDelay
) {
2390 autoSelectText(tPtr
, 3);
2394 if (!tPtr
->flags
.focused
) {
2395 WMSetFocusToWidget(tPtr
);
2396 tPtr
->flags
.focused
= True
;
2399 if (tPtr
->flags
.ownsSelection
)
2400 releaseSelection(tPtr
);
2402 tPtr
->lastClickTime
= event
->xbutton
.time
;
2403 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2407 if (event
->xbutton
.button
2408 == WINGsConfiguration
.mouseWheelDown
) {
2409 WMScrollText(tPtr
, 16);
2413 if (event
->xbutton
.button
2414 == WINGsConfiguration
.mouseWheelUp
) {
2415 WMScrollText(tPtr
, -16);
2419 if (event
->xbutton
.button
== Button2
) {
2423 if (!tPtr
->flags
.editable
) {
2429 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2430 event
->xbutton
.time
, pasteText
, NULL
)) {
2434 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2440 (tPtr
->parser
) (tPtr
, (void *) text
);
2442 insertTextInteractively(tPtr
, text
, n
);
2446 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2447 (void*)WMInsertTextEvent
);
2451 tPtr
->flags
.waitingForSelection
= True
;
2460 if (tPtr
->flags
.pointerGrabbed
) {
2461 tPtr
->flags
.pointerGrabbed
= False
;
2462 XUngrabPointer(dpy
, CurrentTime
);
2466 if (tPtr
->flags
.waitingForSelection
)
2474 handleEvents(XEvent
*event
, void *data
)
2476 Text
*tPtr
= (Text
*)data
;
2478 switch(event
->type
) {
2481 if (event
->xexpose
.count
!=0)
2485 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2486 WMRealizeWidget(tPtr
->hS
);
2490 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2491 WMRealizeWidget(tPtr
->vS
);
2495 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2496 WMRealizeWidget(tPtr
->ruler
);
2501 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2507 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2510 tPtr
->flags
.focused
= True
;
2512 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2513 tPtr
->timerID
= WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY
,
2521 tPtr
->flags
.focused
= False
;
2524 if (tPtr
->timerID
) {
2525 WMDeleteTimerHandler(tPtr
->timerID
);
2526 tPtr
->timerID
= NULL
;
2535 WMDestroyWidget(tPtr
->hS
);
2537 WMDestroyWidget(tPtr
->vS
);
2539 WMDestroyWidget(tPtr
->ruler
);
2541 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2543 WMEmptyArray(tPtr
->gfxItems
);
2546 WMDeleteTimerHandler(tPtr
->timerID
);
2548 WMReleaseFont(tPtr
->dFont
);
2549 WMReleaseColor(tPtr
->dColor
);
2550 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2551 WMRemoveNotificationObserver(tPtr
);
2562 insertPlainText(Text
*tPtr
, char *text
)
2569 mark
= strchr(start
, '\n');
2571 tb
= WMCreateTextBlockWithText(tPtr
,
2573 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
-start
));
2575 tPtr
->flags
.first
= True
;
2577 if (start
&& strlen(start
)) {
2578 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2579 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2581 tPtr
->flags
.first
= False
;
2585 if (tPtr
->flags
.prepend
)
2586 WMPrependTextBlock(tPtr
, tb
);
2588 WMAppendTextBlock(tPtr
, tb
);
2596 rulerMoveCallBack(WMWidget
*w
, void *self
)
2598 Text
*tPtr
= (Text
*)self
;
2601 if (W_CLASS(tPtr
) != WC_Text
)
2609 rulerReleaseCallBack(WMWidget
*w
, void *self
)
2611 Text
*tPtr
= (Text
*)self
;
2614 if (W_CLASS(tPtr
) != WC_Text
)
2623 draggingEntered(WMView
*self
, WMDraggingInfo
*info
)
2625 printf("draggingEntered\n");
2626 return WDOperationCopy
;
2631 draggingUpdated(WMView
*self
, WMDraggingInfo
*info
)
2633 return WDOperationCopy
;
2638 draggingExited(WMView
*self
, WMDraggingInfo
*info
)
2640 printf("draggingExited\n");
2644 prepareForDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2646 printf("prepareForDragOperation\n");
2654 receivedData(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2655 void *cdata
, WMData
*data
)
2657 badbadbad
= wstrdup((char *)WMDataBytes(data
));
2661 /* when it's done in WINGs, remove this */
2663 Bool
requestDroppedData(WMView
*view
, WMDraggingInfo
*info
, char *type
)
2665 WMScreen
*scr
= W_VIEW_SCREEN(view
);
2667 if (!WMRequestSelection(scr
->dragInfo
.destView
,
2668 scr
->xdndSelectionAtom
,
2669 XInternAtom(scr
->display
, type
, False
),
2670 scr
->dragInfo
.timestamp
,
2671 receivedData
, &scr
->dragInfo
)) {
2672 wwarning("could not request data for dropped data");
2679 ev
.type
= ClientMessage
;
2680 ev
.xclient
.message_type
= scr
->xdndFinishedAtom
;
2681 ev
.xclient
.format
= 32;
2682 ev
.xclient
.window
= info
->destinationWindow
;
2683 ev
.xclient
.data
.l
[0] = 0;
2684 ev
.xclient
.data
.l
[1] = 0;
2685 ev
.xclient
.data
.l
[2] = 0;
2686 ev
.xclient
.data
.l
[3] = 0;
2687 ev
.xclient
.data
.l
[4] = 0;
2689 XSendEvent(scr
->display
, info
->sourceWindow
, False
, 0, &ev
);
2690 XFlush(scr
->display
);
2696 performDragOperation(WMView
*self
, WMDraggingInfo
*info
, WMData
*data
)
2699 WMText
*tPtr
= (WMText
*)self
->self
;
2704 requestDroppedData(tPtr
->view
, info
, "application/X-color");
2705 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), badbadbad
, True
);
2707 WMSetTextSelectionColor(tPtr
, color
);
2708 WMReleaseColor(color
);
2717 concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2719 printf("concludeDragOperation\n");
2723 static WMDragDestinationProcs _DragDestinationProcs
= {
2727 prepareForDragOperation
,
2728 performDragOperation
,
2729 concludeDragOperation
2734 getStream(WMText
*tPtr
, int sel
, int array
)
2736 TextBlock
*tb
= NULL
;
2738 unsigned long where
= 0;
2743 if (!(tb
= tPtr
->firstTextBlock
))
2747 (tPtr
->writer
) (tPtr
, (void *) text
);
2751 tb
= tPtr
->firstTextBlock
;
2754 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2756 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2758 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2759 && tb
!= tPtr
->firstTextBlock
) {
2760 text
= wrealloc(text
, where
+1);
2761 text
[where
++] = '\n';
2767 if(tb
->graphic
&& array
) {
2768 text
= wrealloc(text
, where
+4);
2769 text
[where
++] = 0xFA;
2770 text
[where
++] = (tb
->used
>>8)&0x0ff;
2771 text
[where
++] = tb
->used
&0x0ff;
2772 text
[where
++] = tb
->allocated
; /* extra info */
2774 text
= wrealloc(text
, where
+tb
->used
);
2775 memcpy(&text
[where
], tb
->text
, tb
->used
);
2779 } else if (sel
&& tb
->selected
) {
2781 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2782 && tb
!= tPtr
->firstTextBlock
) {
2783 text
= wrealloc(text
, where
+1);
2784 text
[where
++] = '\n';
2790 text
= wrealloc(text
, where
+(tb
->s_end
- tb
->s_begin
));
2791 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
],
2792 tb
->s_end
- tb
->s_begin
);
2793 where
+= tb
->s_end
- tb
->s_begin
;
2798 _gSnext
:tb
= tb
->next
;
2801 /* +1 for the end of string, let's be nice */
2802 text
= wrealloc(text
, where
+1);
2809 releaseStreamObjects(void *data
)
2816 getStreamObjects(WMText
*tPtr
, int sel
)
2818 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
2822 char *start
, *fa
, *desc
;
2824 stream
= getStream(tPtr
, sel
, 1);
2831 fa
= strchr(start
, 0xFA);
2833 if((int)(fa
- start
)>0) {
2835 desc
[(int)(fa
- start
)] = 0;
2836 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
2837 WMSetDataFormat(data
, TYPETEXT
);
2838 WMAddToArray(array
, (void *) data
);
2841 len
= *(fa
+1)*0xff + *(fa
+2);
2842 data
= WMCreateDataWithBytes((void *)(fa
+4), len
);
2843 WMSetDataFormat(data
, *(fa
+3));
2844 WMAddToArray(array
, (void *) data
);
2845 start
= fa
+ len
+ 4;
2848 if (start
&& strlen(start
)) {
2849 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
2850 WMSetDataFormat(data
, TYPETEXT
);
2851 WMAddToArray(array
, (void *) data
);
2864 WMCreateTextForDocumentType(WMWidget
*parent
,
2865 WMAction
*parser
, WMAction
*writer
)
2867 Text
*tPtr
= wmalloc(sizeof(Text
));
2869 printf("could not create text widget\n");
2874 memset(tPtr
, 0, sizeof(Text
));
2875 tPtr
->widgetClass
= WC_Text
;
2876 tPtr
->view
= W_CreateView(W_VIEW(parent
));
2878 perror("could not create text's view\n");
2882 tPtr
->view
->self
= tPtr
;
2883 tPtr
->view
->attribs
.cursor
= tPtr
->view
->screen
->textCursor
;
2884 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
2885 W_ResizeView(tPtr
->view
, 250, 200);
2887 tPtr
->dColor
= WMWhiteColor(tPtr
->view
->screen
);
2888 tPtr
->bgGC
= WMColorGC(tPtr
->dColor
);
2889 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->dColor
);
2890 WMReleaseColor(tPtr
->dColor
);
2892 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
2893 tPtr
->fgGC
= WMColorGC(tPtr
->dColor
);
2899 tPtr
->dFont
= WMRetainFont(WMSystemFontOfSize(tPtr
->view
->screen
, 12));
2901 tPtr
->view
->delegate
= &_TextViewDelegate
;
2903 tPtr
->delegate
= NULL
;
2906 tPtr
->timerID
= NULL
;
2909 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
2910 |EnterWindowMask
|LeaveWindowMask
|FocusChangeMask
,
2911 handleEvents
, tPtr
);
2913 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
|ButtonPressMask
2914 |KeyReleaseMask
|KeyPressMask
|Button1MotionMask
,
2915 handleActionEvents
, tPtr
);
2917 WMAddNotificationObserver(ownershipObserver
, tPtr
,
2918 "_lostOwnership", tPtr
);
2921 char *types
[2] = {"application/X-color", NULL
};
2922 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
2923 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
2926 WMAddNotificationObserver(fontChanged
, tPtr
,
2927 "WMFontPanelDidChangeNotification", tPtr
);
2929 tPtr
->firstTextBlock
= NULL
;
2930 tPtr
->lastTextBlock
= NULL
;
2931 tPtr
->currentTextBlock
= NULL
;
2934 tPtr
->gfxItems
= WMCreateArray(4);
2936 tPtr
->parser
= parser
;
2937 tPtr
->writer
= writer
;
2939 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
2940 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
2942 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
2944 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
2945 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
2946 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
2948 tPtr
->cursor
.x
= -23;
2951 tPtr
->docHeight
= 0;
2952 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
,
2954 tPtr
->db
= (Pixmap
) NULL
;
2956 tPtr
->margins
= WMGetRulerMargins(NULL
);
2957 tPtr
->margins
->right
= tPtr
->visible
.w
;
2960 tPtr
->flags
.rulerShown
= False
;
2961 tPtr
->flags
.monoFont
= False
;
2962 tPtr
->flags
.focused
= False
;
2963 tPtr
->flags
.editable
= True
;
2964 tPtr
->flags
.ownsSelection
= False
;
2965 tPtr
->flags
.pointerGrabbed
= False
;
2966 tPtr
->flags
.extendSelection
= False
;
2967 tPtr
->flags
.frozen
= False
;
2968 tPtr
->flags
.cursorShown
= True
;
2969 tPtr
->flags
.acceptsGraphic
= False
;
2970 tPtr
->flags
.horizOnDemand
= False
;
2971 tPtr
->flags
.needsLayOut
= False
;
2972 tPtr
->flags
.ignoreNewLine
= False
;
2973 tPtr
->flags
.indentNewLine
= False
;
2974 tPtr
->flags
.laidOut
= False
;
2975 tPtr
->flags
.waitingForSelection
= False
;
2976 tPtr
->flags
.prepend
= False
;
2977 tPtr
->flags
.isOverGraphic
= False
;
2978 tPtr
->flags
.relief
= WRSunken
;
2979 tPtr
->flags
.isOverGraphic
= 0;
2980 tPtr
->flags
.alignment
= WALeft
;
2981 tPtr
->flags
.first
= True
;
2987 WMPrependTextStream(WMText
*tPtr
, char *text
)
2989 CHECK_CLASS(tPtr
, WC_Text
);
2992 if(tPtr
->flags
.ownsSelection
)
2993 releaseSelection(tPtr
);
2996 updateScrollers(tPtr
);
3001 tPtr
->flags
.prepend
= True
;
3002 if (text
&& tPtr
->parser
)
3003 (tPtr
->parser
) (tPtr
, (void *) text
);
3005 insertPlainText(tPtr
, text
);
3007 tPtr
->flags
.needsLayOut
= True
;
3013 WMAppendTextStream(WMText
*tPtr
, char *text
)
3015 CHECK_CLASS(tPtr
, WC_Text
);
3018 if(tPtr
->flags
.ownsSelection
)
3019 releaseSelection(tPtr
);
3022 updateScrollers(tPtr
);
3027 tPtr
->flags
.prepend
= False
;
3028 if (text
&& tPtr
->parser
)
3029 (tPtr
->parser
) (tPtr
, (void *) text
);
3031 insertPlainText(tPtr
, text
);
3033 tPtr
->flags
.needsLayOut
= True
;
3034 if(tPtr
->currentTextBlock
)
3035 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3042 WMGetTextStream(WMText
*tPtr
)
3044 CHECK_CLASS(tPtr
, WC_Text
);
3045 return getStream(tPtr
, 0, 0);
3049 WMGetTextSelectedStream(WMText
*tPtr
)
3051 CHECK_CLASS(tPtr
, WC_Text
);
3052 return getStream(tPtr
, 1, 0);
3056 WMGetTextObjects(WMText
*tPtr
)
3058 CHECK_CLASS(tPtr
, WC_Text
);
3059 return getStreamObjects(tPtr
, 0);
3063 WMGetTextSelectedObjects(WMText
*tPtr
)
3065 CHECK_CLASS(tPtr
, WC_Text
);
3066 return getStreamObjects(tPtr
, 1);
3071 WMSetTextDelegate(WMText
*tPtr
, WMTextDelegate
*delegate
)
3073 CHECK_CLASS(tPtr
, WC_Text
);
3075 tPtr
->delegate
= delegate
;
3080 WMCreateTextBlockWithObject(WMText
*tPtr
, WMWidget
*w
,
3081 char *description
, WMColor
*color
,
3082 unsigned short first
, unsigned short extraInfo
)
3086 if (!w
|| !description
|| !color
)
3089 tb
= wmalloc(sizeof(TextBlock
));
3093 tb
->text
= wstrdup(description
);
3094 tb
->used
= strlen(description
);
3097 tb
->color
= WMRetainColor(color
);
3098 tb
->marginN
= newMargin(tPtr
, NULL
);
3099 tb
->allocated
= extraInfo
;
3104 tb
->underlined
= False
;
3105 tb
->selected
= False
;
3107 tb
->sections
= NULL
;
3117 WMCreateTextBlockWithPixmap(WMText
*tPtr
, WMPixmap
*p
,
3118 char *description
, WMColor
*color
,
3119 unsigned short first
, unsigned short extraInfo
)
3123 if (!p
|| !description
|| !color
)
3126 tb
= wmalloc(sizeof(TextBlock
));
3130 tb
->text
= wstrdup(description
);
3131 tb
->used
= strlen(description
);
3133 tb
->d
.pixmap
= WMRetainPixmap(p
);
3134 tb
->color
= WMRetainColor(color
);
3135 tb
->marginN
= newMargin(tPtr
, NULL
);
3136 tb
->allocated
= extraInfo
;
3141 tb
->underlined
= False
;
3142 tb
->selected
= False
;
3144 tb
->sections
= NULL
;
3153 WMCreateTextBlockWithText(WMText
*tPtr
, char *text
, WMFont
*font
, WMColor
*color
,
3154 unsigned short first
, unsigned short len
)
3158 if (!font
|| !color
)
3161 tb
= wmalloc(sizeof(TextBlock
));
3165 tb
->allocated
= reqBlockSize(len
);
3166 tb
->text
= (char *)wmalloc(tb
->allocated
);
3167 memset(tb
->text
, 0, tb
->allocated
);
3169 if (len
< 1|| !text
|| (*text
== '\n' && len
==1 )) {
3174 memcpy(tb
->text
, text
, len
);
3178 tb
->text
[tb
->used
] = 0;
3180 tb
->d
.font
= WMRetainFont(font
);
3181 tb
->color
= WMRetainColor(color
);
3182 tb
->marginN
= newMargin(tPtr
, NULL
);
3185 tb
->graphic
= False
;
3186 tb
->underlined
= False
;
3187 tb
->selected
= False
;
3189 tb
->sections
= NULL
;
3197 WMSetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int first
,
3198 unsigned int kanji
, unsigned int underlined
, int script
,
3199 WMRulerMargins
*margins
)
3201 TextBlock
*tb
= (TextBlock
*) vtb
;
3207 tb
->underlined
= underlined
;
3208 tb
->script
= script
;
3209 tb
->marginN
= newMargin(tPtr
, margins
);
3213 WMGetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int *first
,
3214 unsigned int *kanji
, unsigned int *underlined
, int *script
,
3215 WMRulerMargins
*margins
)
3217 TextBlock
*tb
= (TextBlock
*) vtb
;
3221 if (first
) *first
= tb
->first
;
3222 if (kanji
) *kanji
= tb
->kanji
;
3223 if (underlined
) *underlined
= tb
->underlined
;
3224 if (script
) *script
= tb
->script
;
3225 if (margins
) margins
= &tPtr
->margins
[tb
->marginN
];
3231 WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3233 TextBlock
*tb
= (TextBlock
*)vtb
;
3240 WMWidget
*w
= tb
->d
.widget
;
3241 WMCreateEventHandler(W_VIEW(w
), ButtonPressMask
,
3242 handleWidgetPress
, tb
);
3243 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3244 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3245 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3248 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3250 } else tPtr
->tpos
= tb
->used
;
3252 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3253 tb
->next
= tb
->prior
= NULL
;
3255 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3256 = tPtr
->currentTextBlock
= tb
;
3261 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3264 tb
->next
= tPtr
->currentTextBlock
;
3265 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3266 if (tPtr
->currentTextBlock
->prior
)
3267 tPtr
->currentTextBlock
->prior
->next
= tb
;
3269 tPtr
->currentTextBlock
->prior
= tb
;
3271 tPtr
->firstTextBlock
= tb
;
3273 tPtr
->currentTextBlock
= tb
;
3278 WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3280 TextBlock
*tb
= (TextBlock
*)vtb
;
3287 WMWidget
*w
= tb
->d
.widget
;
3288 WMCreateEventHandler(W_VIEW(w
), ButtonPressMask
,
3289 handleWidgetPress
, tb
);
3290 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3291 (W_VIEW(w
))->attribs
.cursor
=
3292 tPtr
->view
->screen
->defaultCursor
;
3293 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3296 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3298 } else tPtr
->tpos
= tb
->used
;
3300 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3301 tb
->next
= tb
->prior
= NULL
;
3303 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3304 = tPtr
->currentTextBlock
= tb
;
3309 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3312 tb
->next
= tPtr
->currentTextBlock
->next
;
3313 tb
->prior
= tPtr
->currentTextBlock
;
3314 if (tPtr
->currentTextBlock
->next
)
3315 tPtr
->currentTextBlock
->next
->prior
= tb
;
3317 tPtr
->currentTextBlock
->next
= tb
;
3320 tPtr
->lastTextBlock
= tb
;
3322 tPtr
->currentTextBlock
= tb
;
3326 WMRemoveTextBlock(WMText
*tPtr
)
3328 TextBlock
*tb
= NULL
;
3330 if (!tPtr
|| !tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
3331 || !tPtr
->currentTextBlock
) {
3332 printf("cannot remove non existent TextBlock!\b");
3336 tb
= tPtr
->currentTextBlock
;
3338 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3341 WMDeleteEventHandler(W_VIEW(tb
->d
.widget
), ButtonPressMask
,
3342 handleWidgetPress
, tb
);
3343 WMUnmapWidget(tb
->d
.widget
);
3347 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3348 if (tPtr
->currentTextBlock
->next
)
3349 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3351 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3352 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3354 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3355 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3356 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3357 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3359 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3360 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3361 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3368 WMDestroyTextBlock(WMText
*tPtr
, void *vtb
)
3370 TextBlock
*tb
= (TextBlock
*)vtb
;
3376 /* naturally, there's a danger to destroying
3377 widgets whose action brings us here:
3378 ie. press a button to destroy it... need to
3379 find a safer way. till then... this stays commented out */
3380 /* WMDestroyWidget(tb->d.widget);
3381 wfree(tb->d.widget); */
3382 tb
->d
.widget
= NULL
;
3384 WMReleasePixmap(tb
->d
.pixmap
);
3385 tb
->d
.pixmap
= NULL
;
3388 WMReleaseFont(tb
->d
.font
);
3391 WMReleaseColor(tb
->color
);
3392 if (tb
->sections
&& tb
->nsections
> 0)
3393 wfree(tb
->sections
);
3402 WMSetTextForegroundColor(WMText
*tPtr
, WMColor
*color
)
3408 tPtr
->fgGC
= WMColorGC(color
);
3410 tPtr
->fgGC
= WMColorGC(WMBlackColor(tPtr
->view
->screen
));
3416 WMSetTextBackgroundColor(WMText
*tPtr
, WMColor
*color
)
3422 tPtr
->bgGC
= WMColorGC(color
);
3423 W_SetViewBackgroundColor(tPtr
->view
, color
);
3425 tPtr
->bgGC
= WMColorGC(WMWhiteColor(tPtr
->view
->screen
));
3426 W_SetViewBackgroundColor(tPtr
->view
,
3427 WMWhiteColor(tPtr
->view
->screen
));
3434 WMSetTextRelief(WMText
*tPtr
, WMReliefType relief
)
3438 tPtr
->flags
.relief
= relief
;
3439 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3443 WMSetTextHasHorizontalScroller(WMText
*tPtr
, Bool shouldhave
)
3448 if (shouldhave
&& !tPtr
->hS
) {
3449 tPtr
->hS
= WMCreateScroller(tPtr
);
3450 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3451 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3452 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3453 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3454 WMMapWidget(tPtr
->hS
);
3455 } else if (!shouldhave
&& tPtr
->hS
) {
3456 WMUnmapWidget(tPtr
->hS
);
3457 WMDestroyWidget(tPtr
->hS
);
3463 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3468 WMSetTextHasRuler(WMText
*tPtr
, Bool shouldhave
)
3473 if(shouldhave
&& !tPtr
->ruler
) {
3474 tPtr
->ruler
= WMCreateRuler(tPtr
);
3475 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
=
3476 tPtr
->view
->screen
->defaultCursor
;
3477 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3478 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3479 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3480 } else if(!shouldhave
&& tPtr
->ruler
) {
3481 WMShowTextRuler(tPtr
, False
);
3482 WMDestroyWidget(tPtr
->ruler
);
3485 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3489 WMShowTextRuler(WMText
*tPtr
, Bool show
)
3496 if(tPtr
->flags
.monoFont
)
3499 tPtr
->flags
.rulerShown
= show
;
3501 WMMapWidget(tPtr
->ruler
);
3503 WMUnmapWidget(tPtr
->ruler
);
3506 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3510 WMGetTextRulerShown(WMText
*tPtr
)
3518 return tPtr
->flags
.rulerShown
;
3523 WMSetTextHasVerticalScroller(WMText
*tPtr
, Bool shouldhave
)
3528 if (shouldhave
&& !tPtr
->vS
) {
3529 tPtr
->vS
= WMCreateScroller(tPtr
);
3530 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3531 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3532 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3533 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3534 WMMapWidget(tPtr
->vS
);
3535 } else if (!shouldhave
&& tPtr
->vS
) {
3536 WMUnmapWidget(tPtr
->vS
);
3537 WMDestroyWidget(tPtr
->vS
);
3543 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3549 WMScrollText(WMText
*tPtr
, int amount
)
3554 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3558 if (tPtr
->vpos
> 0) {
3559 if (tPtr
->vpos
> abs(amount
)) tPtr
->vpos
+= amount
;
3563 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3564 if (tPtr
->vpos
< limit
) {
3565 if (tPtr
->vpos
< limit
-amount
) tPtr
->vpos
+= amount
;
3566 else tPtr
->vpos
= limit
;
3570 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3571 updateScrollers(tPtr
);
3574 tPtr
->prevVpos
= tPtr
->vpos
;
3579 WMPageText(WMText
*tPtr
, Bool direction
)
3581 if (!tPtr
) return False
;
3582 if (!tPtr
->view
->flags
.realized
) return False
;
3584 return WMScrollText(tPtr
, direction
?tPtr
->visible
.h
:-tPtr
->visible
.h
);
3588 WMSetTextEditable(WMText
*tPtr
, Bool editable
)
3592 tPtr
->flags
.editable
= editable
;
3596 WMGetTextEditable(WMText
*tPtr
)
3600 return tPtr
->flags
.editable
;
3604 WMSetTextIndentNewLines(WMText
*tPtr
, Bool indent
)
3608 tPtr
->flags
.indentNewLine
= indent
;
3612 WMSetTextIgnoresNewline(WMText
*tPtr
, Bool ignore
)
3616 // tPtr->flags.ignoreNewLine = ignore;
3620 WMGetTextIgnoresNewline(WMText
*tPtr
)
3624 return tPtr
->flags
.ignoreNewLine
;
3628 WMSetTextUsesMonoFont(WMText
*tPtr
, Bool mono
)
3634 if(tPtr
->flags
.rulerShown
)
3635 WMShowTextRuler(tPtr
, False
);
3636 if(tPtr
->flags
.alignment
!= WALeft
)
3637 tPtr
->flags
.alignment
= WALeft
;
3640 tPtr
->flags
.monoFont
= mono
;
3641 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3645 WMGetTextUsesMonoFont(WMText
*tPtr
)
3649 return tPtr
->flags
.monoFont
;
3654 WMSetTextDefaultFont(WMText
*tPtr
, WMFont
*font
)
3659 WMReleaseFont(tPtr
->dFont
);
3661 tPtr
->dFont
= WMRetainFont(font
);
3663 tPtr
->dFont
= WMRetainFont(WMSystemFontOfSize(tPtr
->view
->screen
, 12));
3667 WMGetTextDefaultFont(WMText
*tPtr
)
3672 return WMRetainFont(tPtr
->dFont
);
3676 WMSetTextDefaultColor(WMText
*tPtr
, WMColor
*color
)
3681 WMReleaseColor(tPtr
->dColor
);
3683 tPtr
->dColor
= WMRetainColor(color
);
3685 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3689 WMGetTextDefaultColor(WMText
*tPtr
)
3694 return WMRetainColor(tPtr
->dColor
);
3698 WMSetTextAlignment(WMText
*tPtr
, WMAlignment alignment
)
3702 if(tPtr
->flags
.monoFont
)
3703 tPtr
->flags
.alignment
= WALeft
;
3705 tPtr
->flags
.alignment
= alignment
;
3710 WMGetTextInsertType(WMText
*tPtr
)
3714 return tPtr
->flags
.prepend
;
3719 WMSetTextSelectionColor(WMText
*tPtr
, WMColor
*color
)
3721 if (!tPtr
|| !color
)
3724 setSelectionProperty(tPtr
, NULL
, color
, -1);
3728 WMSetTextSelectionFont(WMText
*tPtr
, WMFont
*font
)
3733 setSelectionProperty(tPtr
, font
, NULL
, -1) ;
3737 WMSetTextSelectionUnderlined(WMText
*tPtr
, int underlined
)
3739 if (!tPtr
|| (underlined
!=0 && underlined
!=1))
3742 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
3749 WMFreezeText(WMText
*tPtr
)
3754 tPtr
->flags
.frozen
= True
;
3759 WMThawText(WMText
*tPtr
)
3764 tPtr
->flags
.frozen
= False
;
3766 if(tPtr
->flags
.monoFont
) {
3767 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
3770 /* make sure to unmap widgets no matter where they are */
3771 /* they'll be later remapped if needed by paintText */
3772 for(j
=0; j
<c
; j
++) {
3773 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
3774 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
3775 WMUnmapWidget(tb
->d
.widget
);
3781 tPtr
->flags
.laidOut
= False
;
3782 layOutDocument(tPtr
);
3783 updateScrollers(tPtr
);
3785 tPtr
->flags
.needsLayOut
= False
;
3791 mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
3796 if(!haystack
|| !needle
)
3799 for (ptr
= haystack
-2; ptr
> end
; ptr
--) {
3801 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
3804 if (tolower(*ptr
) == tolower(*needle
) &&
3805 strncasecmp(ptr
, needle
, len
))
3815 WMFindInTextStream(WMText
*tPtr
, char *needle
, Bool direction
,
3822 if (!tPtr
|| !needle
)
3825 if (! (tb
= tPtr
->currentTextBlock
)) {
3826 if (! (tb
= ( (direction
> 0) ?
3827 tPtr
->firstTextBlock
: tPtr
->lastTextBlock
) ) ){
3831 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
3832 tb = (direction>0) ? tb->next : tb->prior; */
3833 if(tb
!= tPtr
->lastTextBlock
)
3841 if(pos
+1 < tb
->used
)
3844 if(tb
->used
- pos
> 0 && pos
> 0) {
3845 char tmp
= tb
->text
[tb
->used
];
3846 tb
->text
[tb
->used
] = 0;
3848 output(&tb
->text
[pos
], tb
->used
- pos
);
3850 mark
= strstr(&tb
->text
[pos
], needle
);
3852 mark
= mystrrstr(&tb
->text
[pos
], needle
,
3853 strlen(needle
), tb
->text
, caseSensitive
);
3855 tb
->text
[tb
->used
] = tmp
;
3862 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
3864 tPtr
->tpos
= (int)(mark
- tb
->text
);
3865 tPtr
->currentTextBlock
= tb
;
3866 updateCursorPosition(tPtr
);
3867 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
3868 tPtr
->sel
.h
= tPtr
->cursor
.h
-10;
3869 tPtr
->sel
.x
= tPtr
->cursor
.x
+1;
3870 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
3871 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
3872 tPtr
->docWidth
- tPtr
->sel
.x
);
3873 tPtr
->flags
.ownsSelection
= True
;
3880 tb
= (direction
>0) ? tb
->next
: tb
->prior
;