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
;
573 getFirstNonGraphicBlockFor(TextBlock
*tb
, short dir
)
575 TextBlock
*hold
= tb
;
583 tb
= (dir
? tb
->next
: tb
->prior
);
591 tb
= (dir
? tb
->prior
: tb
->next
);
603 updateStartForCurrentTextBlock(Text
*tPtr
, int x
, int y
, int *dir
,
606 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
607 tb
= getFirstNonGraphicBlockFor(tb
, *dir
);
612 tPtr
->currentTextBlock
=
613 (dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
621 layOutDocument(tPtr
);
625 *dir
= !(y
<= tb
->sections
[0].y
);
627 if ( ( y
<= tb
->sections
[0]._y
+ tb
->sections
[0].h
)
628 && (y
>= tb
->sections
[0]._y
) ) {
629 /* if it's on the same line */
630 if(x
< tb
->sections
[0].x
)
634 if ( ( y
<= tb
->sections
[tb
->nsections
-1]._y
635 + tb
->sections
[tb
->nsections
-1].h
)
636 && (y
>= tb
->sections
[tb
->nsections
-1]._y
) ) {
637 /* if it's on the same line */
638 if(x
> tb
->sections
[tb
->nsections
-1].x
)
648 paintText(Text
*tPtr
)
653 int len
, y
, c
, s
, done
=False
, prev_y
=-23, dir
/* 1 = down */;
654 WMScreen
*scr
= tPtr
->view
->screen
;
655 Display
*dpy
= tPtr
->view
->screen
->display
;
656 Window win
= tPtr
->view
->window
;
659 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
663 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(tPtr
->bgColor
), 0, 0,
664 tPtr
->visible
.w
, tPtr
->visible
.h
);
666 if (tPtr
->bgPixmap
) {
667 WMDrawPixmap(tPtr
->bgPixmap
, tPtr
->db
,
668 (tPtr
->visible
.w
-tPtr
->visible
.x
-tPtr
->bgPixmap
->width
)/2,
669 (tPtr
->visible
.h
-tPtr
->visible
.y
-tPtr
->bgPixmap
->height
)/2);
672 if (! (tb
= tPtr
->currentTextBlock
)) {
673 if (! (tb
= tPtr
->firstTextBlock
)) {
682 /* first, which direction? Don't waste time looking all over,
683 since the parts to be drawn will most likely be near what
684 was previously drawn */
685 if(!updateStartForCurrentTextBlock(tPtr
, 0, tPtr
->vpos
, &dir
, tb
))
690 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
694 if(tPtr
->vpos
<= tb
->sections
[tb
->nsections
-1]._y
695 + tb
->sections
[tb
->nsections
-1].h
)
698 if(tPtr
->vpos
>= tb
->sections
[tb
->nsections
-1]._y
699 + tb
->sections
[tb
->nsections
-1].h
)
716 /* first, place all text that can be viewed */
717 while (!done
&& tb
) {
723 tb
->selected
= False
;
725 for(s
=0; s
<tb
->nsections
&& !done
; s
++) {
727 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
732 if ( tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
735 if (tPtr
->flags
.monoFont
) {
737 color
= tPtr
->fgColor
;
743 if (tPtr
->flags
.ownsSelection
) {
746 if ( sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
748 XFillRectangle(dpy
, tPtr
->db
, WMColorGC(scr
->gray
),
749 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
753 prev_y
= tb
->sections
[s
]._y
;
755 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
756 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
757 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
758 WMDrawString(scr
, tPtr
->db
, color
, font
,
759 tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
761 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
762 XDrawLine(dpy
, tPtr
->db
, WMColorGC(color
),
763 tb
->sections
[s
].x
- tPtr
->hpos
,
765 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
,
769 tb
= (!done
? tb
->next
: NULL
);
772 /* now , show all graphic items that can be viewed */
773 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
774 if (c
> 0 && !tPtr
->flags
.monoFont
) {
778 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
780 /* if it's not viewable, and mapped, unmap it */
781 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
782 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
785 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
786 WMUnmapWidget(tb
->d
.widget
);
790 /* if it's viewable, and not mapped, map it */
792 W_View
*view
= W_VIEW(tb
->d
.widget
);
794 if (!view
->flags
.realized
)
795 WMRealizeWidget(tb
->d
.widget
);
796 if(!view
->flags
.mapped
) {
797 XMapWindow(view
->screen
->display
, view
->window
);
798 XFlush(view
->screen
->display
);
799 view
->flags
.mapped
= 1;
804 WMMoveWidget(tb
->d
.widget
,
805 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
806 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
807 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
810 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
811 tb
->sections
[0].x
- tPtr
->hpos
,
812 tb
->sections
[0].y
- tPtr
->vpos
);
813 h
= tb
->d
.pixmap
->height
+ 1;
817 if (tPtr
->flags
.ownsSelection
) {
820 if ( sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
821 Drawable d
= (0&&tb
->object
?
822 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
825 XFillRectangle(dpy
, d
, tPtr
->stippledGC
,
826 /*XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,*/
827 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
831 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
832 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
833 tb
->sections
[0].x
- tPtr
->hpos
,
834 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
835 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
836 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
844 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
845 && tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
846 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
847 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tPtr
->fgColor
),
849 tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
852 XCopyArea(dpy
, tPtr
->db
, win
, WMColorGC(tPtr
->bgColor
), 0, 0,
853 tPtr
->visible
.w
, tPtr
->visible
.h
,
854 tPtr
->visible
.x
, tPtr
->visible
.y
);
856 W_DrawRelief(scr
, win
, 0, 0,
857 tPtr
->view
->size
.width
, tPtr
->view
->size
.height
,
860 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
861 XDrawLine(dpy
, win
, WMColorGC(tPtr
->fgColor
),
862 2, 42, tPtr
->view
->size
.width
-4, 42);
867 mouseOverObject(Text
*tPtr
, int x
, int y
)
872 x
-= tPtr
->visible
.x
;
874 y
-= tPtr
->visible
.y
;
877 if(tPtr
->flags
.ownsSelection
) {
880 && tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
881 && tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
882 tPtr
->flags
.isOverGraphic
= 1;
889 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
892 tPtr
->flags
.isOverGraphic
= 0;
896 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
898 if(!tb
|| !tb
->sections
) {
899 tPtr
->flags
.isOverGraphic
= 0;
904 if(tb
->sections
[0].x
<= x
905 && tb
->sections
[0].y
<= y
906 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
907 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
908 tPtr
->flags
.isOverGraphic
= 3;
919 tPtr
->flags
.isOverGraphic
= 0;
922 tPtr
->view
->attribs
.cursor
= (result
?
923 tPtr
->view
->screen
->defaultCursor
924 : tPtr
->view
->screen
->textCursor
);
926 XSetWindowAttributes attribs
;
927 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
928 XChangeWindowAttributes(tPtr
->view
->screen
->display
,
929 tPtr
->view
->window
, CWCursor
,
937 blinkCursor(void *data
)
939 Text
*tPtr
= (Text
*)data
;
941 if (tPtr
->flags
.cursorShown
) {
942 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
,
945 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
,
949 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
954 updateCursorPosition(Text
*tPtr
)
956 TextBlock
*tb
= NULL
;
959 if(tPtr
->flags
.needsLayOut
)
960 layOutDocument(tPtr
);
962 if (! (tb
= tPtr
->currentTextBlock
)) {
963 if (! (tb
= tPtr
->firstTextBlock
)) {
964 WMFont
*font
= tPtr
->dFont
;
966 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
-font
->y
);
977 y
= tb
->sections
[0].y
;
978 h
= tb
->sections
[0].h
;
979 x
= tb
->sections
[0].x
;
981 } else if(tb
->graphic
) {
982 y
= tb
->sections
[0].y
;
983 h
= tb
->sections
[0].h
;
984 x
= tb
->sections
[0].x
;
986 x
+= tb
->sections
[0].w
;
989 if(tPtr
->tpos
> tb
->used
)
990 tPtr
->tpos
= tb
->used
;
992 for(s
=0; s
<tb
->nsections
-1; s
++) {
994 if(tPtr
->tpos
>= tb
->sections
[s
].begin
995 && tPtr
->tpos
<= tb
->sections
[s
].end
)
999 y
= tb
->sections
[s
]._y
;
1000 h
= tb
->sections
[s
].h
;
1001 x
= tb
->sections
[s
].x
+ WMWidthOfString(
1002 (tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
),
1003 &tb
->text
[tb
->sections
[s
].begin
],
1004 tPtr
->tpos
- tb
->sections
[s
].begin
);
1012 /* scroll the bars if the cursor is not visible */
1013 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1014 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1015 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1017 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1018 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1019 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1020 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1025 updateScrollers(tPtr
);
1030 cursorToTextPosition(Text
*tPtr
, int x
, int y
)
1032 TextBlock
*tb
= NULL
;
1033 int done
=False
, s
, pos
, len
, _w
, _y
, dir
=1; /* 1 == "down" */
1036 if(tPtr
->flags
.needsLayOut
)
1037 layOutDocument(tPtr
);
1039 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
1043 x
-= (tPtr
->visible
.x
- 2);
1047 /* clicked is relative to document, not window... */
1048 tPtr
->clicked
.x
= x
;
1049 tPtr
->clicked
.y
= y
;
1051 if (! (tb
= tPtr
->currentTextBlock
)) {
1052 if (! (tb
= tPtr
->firstTextBlock
)) {
1053 WMFont
*font
= tPtr
->dFont
;
1055 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
-font
->y
);
1062 /* first, which direction? Most likely, newly clicked
1063 position will be close to previous */
1064 if(!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
1068 s
= (dir
? 0 : tb
->nsections
-1);
1069 if ( y
>= tb
->sections
[s
]._y
1070 && y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1074 /* get the first (or last) section of the TextBlock that
1075 lies about the vertical click point */
1077 while (!done
&& tb
) {
1079 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1080 if( (dir
?tb
->next
:tb
->prior
))
1081 tb
= (dir
?tb
->next
:tb
->prior
);
1085 s
= (dir
? 0 : tb
->nsections
-1);
1086 while (!done
&& (dir
? (s
<tb
->nsections
) : (s
>=0) )) {
1088 if ( (dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) :
1089 ( y
>= tb
->sections
[s
]._y
) ) ) {
1097 if ( (dir
? tb
->next
: tb
->prior
)) {
1098 tb
= (dir
? tb
->next
: tb
->prior
);
1101 break; /* goto _doneH; */
1107 if (s
<0 || s
>=tb
->nsections
) {
1108 s
= (dir
? tb
->nsections
-1 : 0);
1112 /* we have the line, which TextBlock on that line is it? */
1113 pos
= (dir
?0:tb
->sections
[s
].begin
);
1114 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1115 TextBlock
*hold
= tb
;
1116 tb
= getFirstNonGraphicBlockFor(hold
, dir
);
1130 _y
= tb
->sections
[s
]._y
;
1134 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1135 tb
= (dir
? tb
->next
: tb
->prior
);
1142 _w
= WMWidgetWidth(tb
->d
.widget
)-5;
1144 _w
= tb
->d
.pixmap
->width
-5;
1146 if (tb
->sections
[0].x
+ _w
>= x
)
1149 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1150 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1151 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1152 if (tb
->sections
[s
].x
+ _w
>= x
)
1157 if (tb
->sections
[s
].x
<= x
)
1161 if ((dir
? tb
->next
: tb
->prior
)) {
1162 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1163 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1164 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1166 pos
= (dir
?0:tb
->sections
[s
].begin
);
1167 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1172 if (_y
!= nxt
->sections
[dir
?0:nxt
->nsections
-1]._y
) {
1173 /* this must be the last/first on this line. stop */
1174 pos
= (dir
? tb
->sections
[s
].end
: 0);
1175 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1179 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1181 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1182 } else if (pos
> tb
->sections
[s
].begin
) {
1184 WMWidthOfString(tb
->d
.font
,
1185 &(tb
->text
[tb
->sections
[s
].begin
]),
1186 pos
- tb
->sections
[s
].begin
);
1193 if ( (dir
? tb
->next
: tb
->prior
)) {
1194 tb
= (dir
? tb
->next
: tb
->prior
);
1201 s
= (dir
? 0 : tb
->nsections
-1);
1204 /* we have said TextBlock, now where within it? */
1207 int gw
= (tb
->object
?
1208 WMWidgetWidth(tb
->d
.widget
) : tb
->d
.pixmap
->width
);
1210 tPtr
->cursor
.x
= tb
->sections
[0].x
;
1212 if(x
> tPtr
->cursor
.x
+ gw
/2) {
1214 tPtr
->cursor
.x
+= gw
;
1216 printf("first %d\n", tb
->first
);
1218 if(tb
->prior
->graphic
) pos
= 1;
1219 else pos
= tb
->prior
->used
;
1229 WMFont
*f
= tb
->d
.font
;
1230 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1231 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1233 _w
= x
- tb
->sections
[s
].x
;
1236 while (pos
<len
&& WMWidthOfString(f
, text
, pos
+1) < _w
)
1239 tPtr
->cursor
.x
= tb
->sections
[s
].x
+
1240 (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1242 pos
+= tb
->sections
[s
].begin
;
1248 tPtr
->tpos
= (pos
<=1)? pos
: 0;
1250 tPtr
->tpos
= (pos
<tb
->used
)? pos
: tb
->used
;
1254 printf("...for this app will surely crash :-)\n");
1256 tPtr
->currentTextBlock
= tb
;
1257 tPtr
->cursor
.h
= tb
->sections
[s
].h
;
1258 tPtr
->cursor
.y
= tb
->sections
[s
]._y
;
1260 /* scroll the bars if the cursor is not visible */
1261 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1262 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1263 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1265 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1266 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1267 updateScrollers(tPtr
);
1268 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1269 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1270 updateScrollers(tPtr
);
1279 updateScrollers(Text
*tPtr
)
1282 if (tPtr
->flags
.frozen
)
1286 if (tPtr
->docHeight
<= tPtr
->visible
.h
) {
1287 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
1290 float hmax
= (float)(tPtr
->docHeight
);
1291 WMSetScrollerParameters(tPtr
->vS
,
1292 ((float)tPtr
->vpos
)/(hmax
- (float)tPtr
->visible
.h
),
1293 (float)tPtr
->visible
.h
/hmax
);
1295 } else tPtr
->vpos
= 0;
1298 if (tPtr
->docWidth
<= tPtr
->visible
.w
) {
1299 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
1302 float wmax
= (float)(tPtr
->docWidth
);
1303 WMSetScrollerParameters(tPtr
->hS
,
1304 ((float)tPtr
->hpos
)/(wmax
- (float)tPtr
->visible
.w
),
1305 (float)tPtr
->visible
.w
/wmax
);
1307 } else tPtr
->hpos
= 0;
1311 scrollersCallBack(WMWidget
*w
, void *self
)
1313 Text
*tPtr
= (Text
*)self
;
1314 Bool scroll
= False
;
1317 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1320 if (w
== tPtr
->vS
) {
1322 height
= tPtr
->visible
.h
;
1324 which
= WMGetScrollerHitPart(tPtr
->vS
);
1327 case WSDecrementLine
:
1328 if (tPtr
->vpos
> 0) {
1329 if (tPtr
->vpos
>16) tPtr
->vpos
-=16;
1335 case WSIncrementLine
: {
1336 int limit
= tPtr
->docHeight
- height
;
1337 if (tPtr
->vpos
< limit
) {
1338 if (tPtr
->vpos
<limit
-16) tPtr
->vpos
+=16;
1339 else tPtr
->vpos
=limit
;
1345 case WSDecrementPage
:
1346 if(((int)tPtr
->vpos
- (int)height
) >= 0)
1347 tPtr
->vpos
-= height
;
1354 case WSIncrementPage
:
1355 tPtr
->vpos
+= height
;
1356 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1357 tPtr
->vpos
= tPtr
->docHeight
- height
;
1363 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1364 * (float)(tPtr
->docHeight
- height
);
1372 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1373 tPtr
->prevVpos
= tPtr
->vpos
;
1377 if (w
== tPtr
->hS
) {
1378 int width
= tPtr
->visible
.w
;
1380 which
= WMGetScrollerHitPart(tPtr
->hS
);
1383 case WSDecrementLine
:
1384 if (tPtr
->hpos
> 0) {
1385 if (tPtr
->hpos
>16) tPtr
->hpos
-=16;
1390 case WSIncrementLine
: {
1391 int limit
= tPtr
->docWidth
- width
;
1392 if (tPtr
->hpos
< limit
) {
1393 if (tPtr
->hpos
<limit
-16) tPtr
->hpos
+=16;
1394 else tPtr
->hpos
=limit
;
1399 case WSDecrementPage
:
1400 if(((int)tPtr
->hpos
- (int)width
) >= 0)
1401 tPtr
->hpos
-= width
;
1408 case WSIncrementPage
:
1409 tPtr
->hpos
+= width
;
1410 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1411 tPtr
->hpos
= tPtr
->docWidth
- width
;
1417 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1418 * (float)(tPtr
->docWidth
- width
);
1426 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1427 tPtr
->prevHpos
= tPtr
->hpos
;
1431 updateScrollers(tPtr
);
1440 unsigned short begin
, end
; /* what part of the text block */
1445 layOutLine(Text
*tPtr
, myLineItems
*items
, int nitems
, int x
, int y
)
1447 int i
, j
=0, lw
= 0, line_height
=0, max_d
=0, len
, n
;
1450 TextBlock
*tb
, *tbsame
=NULL
;
1452 if(!items
|| nitems
== 0)
1455 for(i
=0; i
<nitems
; i
++) {
1459 if (!tPtr
->flags
.monoFont
) {
1461 WMWidget
*wdt
= tb
->d
.widget
;
1462 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1463 if (tPtr
->flags
.alignment
!= WALeft
)
1464 lw
+= WMWidgetWidth(wdt
);
1466 line_height
= WMAX(line_height
,
1467 tb
->d
.pixmap
->height
+ max_d
);
1468 if (tPtr
->flags
.alignment
!= WALeft
)
1469 lw
+= tb
->d
.pixmap
->width
;
1474 font
= (tPtr
->flags
.monoFont
)?tPtr
->dFont
: tb
->d
.font
;
1475 max_d
= WMAX(max_d
, abs(font
->height
-font
->y
));
1476 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1477 text
= &(tb
->text
[items
[i
].begin
]);
1478 len
= items
[i
].end
- items
[i
].begin
;
1479 if (tPtr
->flags
.alignment
!= WALeft
)
1480 lw
+= WMWidthOfString(font
, text
, len
);
1484 if (tPtr
->flags
.alignment
== WARight
) {
1485 j
= tPtr
->visible
.w
- lw
;
1486 } else if (tPtr
->flags
.alignment
== WACenter
) {
1487 j
= (int) ((float)(tPtr
->visible
.w
- lw
))/2.0;
1490 for(i
=0; i
<nitems
; i
++) {
1493 if (tbsame
== tb
) { /* extend it, since it's on same line */
1494 tb
->sections
[tb
->nsections
-1].end
= items
[i
].end
;
1495 n
= tb
->nsections
-1;
1497 tb
->sections
= wrealloc(tb
->sections
,
1498 (++tb
->nsections
)*sizeof(Section
));
1499 n
= tb
->nsections
-1;
1500 tb
->sections
[n
]._y
= y
+ max_d
;
1501 tb
->sections
[n
].max_d
= max_d
;
1502 tb
->sections
[n
].x
= x
+j
;
1503 tb
->sections
[n
].h
= line_height
;
1504 tb
->sections
[n
].begin
= items
[i
].begin
;
1505 tb
->sections
[n
].end
= items
[i
].end
;
1508 tb
->sections
[n
].last
= (i
+1 == nitems
);
1511 if (!tPtr
->flags
.monoFont
) {
1513 WMWidget
*wdt
= tb
->d
.widget
;
1514 tb
->sections
[n
].y
= max_d
+ y
1515 + line_height
- WMWidgetHeight(wdt
);
1516 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1518 tb
->sections
[n
].y
= y
+ line_height
1519 + max_d
- tb
->d
.pixmap
->height
;
1520 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1522 x
+= tb
->sections
[n
].w
;
1525 font
= (tPtr
->flags
.monoFont
)? tPtr
->dFont
: tb
->d
.font
;
1526 len
= items
[i
].end
- items
[i
].begin
;
1527 text
= &(tb
->text
[items
[i
].begin
]);
1529 tb
->sections
[n
].y
= y
+line_height
-font
->y
;
1531 WMWidthOfString(font
,
1532 &(tb
->text
[tb
->sections
[n
].begin
]),
1533 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1535 x
+= WMWidthOfString(font
, text
, len
);
1547 layOutDocument(Text
*tPtr
)
1550 myLineItems
*items
= NULL
;
1551 unsigned int itemsSize
=0, nitems
=0, begin
, end
;
1553 unsigned int x
, y
=0, lw
= 0, width
=0, bmargin
;
1554 char *start
=NULL
, *mark
=NULL
;
1556 if ( tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)) )
1559 assert(tPtr
->visible
.w
> 20);
1561 tPtr
->docWidth
= tPtr
->visible
.w
;
1562 x
= tPtr
->margins
[tb
->marginN
].first
;
1563 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1565 /* only partial layOut needed: re-Lay only affected textblocks */
1566 if (tPtr
->flags
.laidOut
) {
1567 tb
= tPtr
->currentTextBlock
;
1569 /* search backwards for textblocks on same line */
1571 if (!tb
->sections
|| tb
->nsections
<1) {
1572 tb
= tPtr
->firstTextBlock
;
1573 tPtr
->flags
.laidOut
= False
;
1578 if(!tb
->prior
->sections
|| tb
->prior
->nsections
<1) {
1579 tb
= tPtr
->firstTextBlock
;
1580 tPtr
->flags
.laidOut
= False
;
1585 if (tb
->sections
[0]._y
!=
1586 tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
) {
1592 if(tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
>0) {
1593 y
= tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
+
1594 tb
->prior
->sections
[tb
->prior
->nsections
-1].h
-
1595 tb
->prior
->sections
[tb
->prior
->nsections
-1].max_d
;
1604 if (tb
->sections
&& tb
->nsections
>0) {
1605 wfree(tb
->sections
);
1606 tb
->sections
= NULL
;
1610 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1611 TextBlock
*next
= tb
->next
;
1612 tPtr
->currentTextBlock
= tb
;
1613 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1619 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1620 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1621 x
= tPtr
->margins
[tb
->marginN
].first
;
1622 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1628 if (!tPtr
->flags
.monoFont
) {
1630 width
= WMWidgetWidth(tb
->d
.widget
);
1632 width
= tb
->d
.pixmap
->width
;
1634 if (width
> tPtr
->docWidth
)
1635 tPtr
->docWidth
= width
;
1638 if (lw
>= tPtr
->visible
.w
- x
) {
1639 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1645 if(nitems
+ 1> itemsSize
) {
1646 items
= wrealloc(items
,
1647 (++itemsSize
)*sizeof(myLineItems
));
1650 items
[nitems
].tb
= tb
;
1651 items
[nitems
].begin
= 0;
1652 items
[nitems
].end
= 0;
1656 } else if ((start
= tb
->text
)) {
1658 font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
1661 mark
= strchr(start
, ' ');
1663 end
+= (int)(mark
-start
)+1;
1666 end
+= strlen(start
);
1673 if (end
-begin
> 0) {
1675 width
= WMWidthOfString(font
,
1676 &tb
->text
[begin
], end
-begin
);
1678 /* if it won't fit, char wrap it */
1679 if (width
>= tPtr
->visible
.w
) {
1680 char *t
= &tb
->text
[begin
];
1681 int l
=end
-begin
, i
=0;
1683 width
= WMWidthOfString(font
, t
, ++i
);
1684 } while (width
< tPtr
->visible
.w
&& i
< l
);
1687 start
= &tb
->text
[end
];
1693 if (lw
>= tPtr
->visible
.w
- x
) {
1694 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1700 if(nitems
+ 1 > itemsSize
) {
1701 items
= wrealloc(items
,
1702 (++itemsSize
)*sizeof(myLineItems
));
1705 items
[nitems
].tb
= tb
;
1706 items
[nitems
].begin
= begin
;
1707 items
[nitems
].end
= end
;
1715 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1716 if(0&&tPtr
->flags
.laidOut
1717 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
>0
1718 && (tPtr
->vpos
+ tPtr
->visible
.h
1719 < tb
->next
->sections
[0]._y
)) {
1720 if(tPtr
->lastTextBlock
->sections
1721 && tPtr
->lastTextBlock
->nsections
> 0 ) {
1722 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1723 int ly
= ltb
->sections
[ltb
->nsections
-1]._y
;
1724 int lh
= ltb
->sections
[ltb
->nsections
-1].h
;
1727 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
;
1728 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
);
1730 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1732 sd
= tPtr
->docHeight
-y
;
1734 printf("dif %d-%d: %d\n", ss
, sd
, ss
-sd
);
1735 y
+= tb
->next
->sections
[0]._y
-y
;
1737 printf("nitems%d\n", nitems
);
1739 y
= tPtr
->docHeight
+ss
-sd
;
1743 tPtr
->flags
.laidOut
= False
;
1752 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1754 if (tPtr
->docHeight
!= y
+10) {
1755 tPtr
->docHeight
= y
+10;
1756 updateScrollers(tPtr
);
1759 if(tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1762 tPtr
->flags
.horizOnDemand
= True
;
1763 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, True
);
1764 event
.type
= Expose
;
1765 handleEvents(&event
, (void *)tPtr
);
1767 } else if(tPtr
->docWidth
<= tPtr
->visible
.w
1768 && tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1769 tPtr
->flags
.horizOnDemand
= False
;
1770 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, False
);
1773 tPtr
->flags
.laidOut
= True
;
1775 if(items
&& itemsSize
> 0)
1782 textDidResize(W_ViewDelegate
*self
, WMView
*view
)
1784 Text
*tPtr
= (Text
*)view
->self
;
1785 unsigned short w
= tPtr
->view
->size
.width
;
1786 unsigned short h
= tPtr
->view
->size
.height
;
1787 unsigned short rh
= 0, vw
= 0, rel
;
1789 rel
= (tPtr
->flags
.relief
== WRFlat
);
1791 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1792 WMMoveWidget(tPtr
->ruler
, 2, 2);
1793 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1798 WMMoveWidget(tPtr
->vS
, 1 - (rel
?1:0), rh
+ 1 - (rel
?1:0));
1799 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
?2:0));
1801 WMSetRulerOffset(tPtr
->ruler
,22);
1802 } else WMSetRulerOffset(tPtr
->ruler
, 2);
1806 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1807 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1809 WMMoveWidget(tPtr
->hS
, vw
+1, h
- 21);
1810 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1814 tPtr
->visible
.x
= (tPtr
->vS
)?24:4;
1815 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)?43:3;
1816 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1817 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1818 tPtr
->visible
.h
-= (tPtr
->hS
)?20:0;
1819 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1821 if (tPtr
->view
->flags
.realized
) {
1824 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1825 tPtr
->db
= (Pixmap
) NULL
;
1828 if (tPtr
->visible
.w
< 40)
1829 tPtr
->visible
.w
= 40;
1830 if (tPtr
->visible
.h
< 20)
1831 tPtr
->visible
.h
= 20;
1834 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1835 tPtr
->view
->window
, tPtr
->visible
.w
,
1836 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1843 W_ViewDelegate _TextViewDelegate
=
1851 #define TEXT_BUFFER_INCR 8
1852 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1855 clearText(Text
*tPtr
)
1857 tPtr
->vpos
= tPtr
->hpos
= 0;
1858 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1859 tPtr
->cursor
.x
= -23;
1861 if (!tPtr
->firstTextBlock
)
1864 while (tPtr
->currentTextBlock
)
1865 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1867 tPtr
->firstTextBlock
= NULL
;
1868 tPtr
->currentTextBlock
= NULL
;
1869 tPtr
->lastTextBlock
= NULL
;
1870 WMEmptyArray(tPtr
->gfxItems
);
1873 /* possibly remove a single character from the currentTextBlock,
1874 or if there's a selection, remove it...
1875 note that Delete and Backspace are treated differently */
1877 deleteTextInteractively(Text
*tPtr
, KeySym ksym
)
1880 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1881 Bool done
= 1, wasFirst
= 0;
1883 if (!tPtr
->flags
.editable
)
1886 if ( !(tb
= tPtr
->currentTextBlock
) )
1889 if (tPtr
->flags
.ownsSelection
) {
1890 if(removeSelection(tPtr
))
1891 layOutDocument(tPtr
);
1895 wasFirst
= tb
->first
;
1896 if (back
&& tPtr
->tpos
< 1) {
1898 if(tb
->prior
->blank
) {
1899 tPtr
->currentTextBlock
= tb
->prior
;
1900 WMRemoveTextBlock(tPtr
);
1901 tPtr
->currentTextBlock
= tb
;
1903 layOutDocument(tPtr
);
1907 TextBlock
*prior
= tb
->prior
;
1908 tPtr
->currentTextBlock
= tb
;
1909 WMRemoveTextBlock(tPtr
);
1918 tPtr
->tpos
= tb
->used
;
1920 tPtr
->currentTextBlock
= tb
;
1924 tb
->next
->first
= False
;
1925 layOutDocument(tPtr
);
1932 if ( (tb
->used
> 0) && ((back
?tPtr
->tpos
> 0:1))
1933 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1936 memmove(&(tb
->text
[tPtr
->tpos
]),
1937 &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1942 /* if there are no characters left to back over in the textblock,
1943 but it still has characters to the right of the cursor: */
1944 if ( (back
? (tPtr
->tpos
== 0 && !done
) : ( tPtr
->tpos
>= tb
->used
))
1947 /* no more chars, and it's marked as blank? */
1949 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1951 if(tb
->used
== 0 || tb
->graphic
)
1952 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1955 tPtr
->currentTextBlock
= sibling
;
1957 tPtr
->tpos
= (back
? 1 : 0);
1959 tPtr
->tpos
= (back
? sibling
->used
: 0);
1961 /* no more chars, so mark it as blank */
1962 } else if(tb
->used
== 0) {
1964 } else if(tb
->graphic
) {
1965 Bool hasNext
= (Bool
)(tb
->next
);
1967 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1970 } else if(tPtr
->currentTextBlock
) {
1971 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
?
1972 1 : tPtr
->currentTextBlock
->used
);
1974 } else printf("DEBUG: unaccounted for... catch this!\n");
1977 layOutDocument(tPtr
);
1982 insertTextInteractively(Text
*tPtr
, char *text
, int len
)
1985 char *newline
= NULL
;
1987 if (!tPtr
->flags
.editable
) {
1991 if (len
< 1 || !text
)
1995 if(tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
1999 if (tPtr
->flags
.ownsSelection
)
2000 removeSelection(tPtr
);
2003 if (tPtr
->flags
.ignoreNewLine
) {
2005 for(i
=0; i
<len
; i
++) {
2006 if (text
[i
] == '\n')
2011 tb
= tPtr
->currentTextBlock
;
2012 if (!tb
|| tb
->graphic
) {
2014 WMAppendTextStream(tPtr
, text
);
2015 layOutDocument(tPtr
);
2019 if ((newline
= strchr(text
, '\n'))) {
2020 int nlen
= (int)(newline
-text
);
2021 int s
= tb
->used
- tPtr
->tpos
;
2023 if (!tb
->blank
&& nlen
>0) {
2028 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
2029 tb
->used
-= (tb
->used
- tPtr
->tpos
);
2031 insertTextInteractively(tPtr
, text
, nlen
);
2033 WMAppendTextStream(tPtr
, newline
);
2035 insertTextInteractively(tPtr
, save
, s
);
2039 if (tPtr
->tpos
>0 && tPtr
->tpos
< tb
->used
2040 && !tb
->graphic
&& tb
->text
) {
2042 unsigned short savePos
= tPtr
->tpos
;
2043 void *ntb
= WMCreateTextBlockWithText(
2044 tPtr
, &tb
->text
[tPtr
->tpos
],
2045 tb
->d
.font
, tb
->color
, True
, tb
->used
- tPtr
->tpos
);
2047 if(tb
->sections
[0].end
== tPtr
->tpos
)
2048 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2049 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2052 WMAppendTextBlock(tPtr
, ntb
);
2055 } else if (tPtr
->tpos
== tb
->used
) {
2056 if(tPtr
->flags
.indentNewLine
) {
2057 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2058 " ", tb
->d
.font
, tb
->color
, True
, 4));
2061 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2062 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2065 } else if (tPtr
->tpos
== 0) {
2066 if(tPtr
->flags
.indentNewLine
) {
2067 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2068 " ", tb
->d
.font
, tb
->color
, True
, 4));
2070 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2071 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2074 if(tPtr
->currentTextBlock
->next
)
2075 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
2079 if (tb
->used
+ len
>= tb
->allocated
) {
2080 tb
->allocated
= reqBlockSize(tb
->used
+len
);
2081 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
2085 memcpy(tb
->text
, text
, len
);
2088 tb
->text
[tb
->used
] = 0;
2092 memmove(&(tb
->text
[tPtr
->tpos
+len
]), &tb
->text
[tPtr
->tpos
],
2093 tb
->used
-tPtr
->tpos
+1);
2094 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2097 tb
->text
[tb
->used
] = 0;
2102 layOutDocument(tPtr
);
2107 selectRegion(Text
*tPtr
, int x
, int y
)
2113 y
+= (tPtr
->flags
.rulerShown
? 40: 0);
2116 y
-= 10; /* the original offset */
2118 x
-= tPtr
->visible
.x
-2;
2122 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2123 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2124 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2125 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2127 tPtr
->flags
.ownsSelection
= True
;
2133 releaseSelection(Text
*tPtr
)
2135 TextBlock
*tb
= tPtr
->firstTextBlock
;
2138 tb
->selected
= False
;
2141 tPtr
->flags
.ownsSelection
= False
;
2142 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
,
2150 requestHandler(WMView
*view
, Atom selection
, Atom target
, void *cdata
,
2153 Text
*tPtr
= view
->self
;
2154 Display
*dpy
= tPtr
->view
->screen
->display
;
2156 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2157 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2158 WMData
*data
= NULL
;
2161 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2162 char *text
= WMGetTextSelectedStream(tPtr
);
2165 data
= WMCreateDataWithBytes(text
, strlen(text
));
2166 WMSetDataFormat(data
, TYPETEXT
);
2170 } else printf("didn't get it\n");
2172 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2173 if (target
== _TARGETS
) {
2176 ptr
= wmalloc(4 * sizeof(Atom
));
2180 ptr
[3] = COMPOUND_TEXT
;
2182 data
= WMCreateDataWithBytes(ptr
, 4*4);
2183 WMSetDataFormat(data
, 32);
2194 lostHandler(WMView
*view
, Atom selection
, void *cdata
)
2196 releaseSelection((WMText
*)view
->self
);
2200 static WMSelectionProcs selectionHandler
= {
2201 requestHandler
, lostHandler
, NULL
2206 ownershipObserver(void *observerData
, WMNotification
*notification
)
2208 if (observerData
!= WMGetNotificationClientData(notification
))
2209 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2214 autoSelectText(Text
*tPtr
, int clicks
)
2218 char *mark
= NULL
, behind
, ahead
;
2220 if(!(tb
= tPtr
->currentTextBlock
))
2226 switch(tb
->text
[tPtr
->tpos
]) {
2229 case '<': case '>': behind = '<'; ahead = '>'; break;
2230 case '{': case '}': behind = '{'; ahead = '}'; break;
2231 case '[': case ']': behind = '['; ahead = ']'; break;
2233 default: behind
= ahead
= ' ';
2236 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
2237 tPtr
->sel
.h
= 6;/*tPtr->cursor.h-10;*/
2240 tPtr
->sel
.x
= tb
->sections
[0].x
;
2241 tPtr
->sel
.w
= tb
->sections
[0].w
;
2243 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
2246 while(start
> 0 && tb
->text
[start
-1] != behind
)
2250 if(tPtr
->tpos
> start
){
2251 x
-= WMWidthOfString(font
, &tb
->text
[start
],
2252 tPtr
->tpos
- start
);
2254 tPtr
->sel
.x
= (x
<0?0:x
)+1;
2256 if((mark
= strchr(&tb
->text
[start
], ahead
))) {
2257 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2258 (int)(mark
- &tb
->text
[start
]));
2259 } else if(tb
->used
> start
) {
2260 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2265 } else if(clicks
== 3) {
2266 TextBlock
*cur
= tb
;
2268 while(tb
&& !tb
->first
) {
2271 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2274 while(tb
->next
&& !tb
->next
->first
) {
2277 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
2281 tPtr
->sel
.w
= tPtr
->docWidth
;
2282 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2285 if (!tPtr
->flags
.ownsSelection
) {
2286 WMCreateSelectionHandler(tPtr
->view
,
2287 XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2288 tPtr
->flags
.ownsSelection
= True
;
2296 fontChanged(void *observerData
, WMNotification
*notification
)
2298 WMText
*tPtr
= (WMText
*) observerData
;
2299 WMFont
*font
= (WMFont
*)WMGetNotificationClientData(notification
);
2300 printf("fontChanged\n");
2305 if (tPtr
->flags
.ownsSelection
)
2306 WMSetTextSelectionFont(tPtr
, font
);
2311 handleTextKeyPress(Text
*tPtr
, XEvent
*event
)
2315 int control_pressed
= False
;
2316 TextBlock
*tb
= NULL
;
2318 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2319 control_pressed
= True
;
2320 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2325 if((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2327 updateCursorPosition(tPtr
);
2332 if((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2333 if(tPtr
->currentTextBlock
->graphic
)
2336 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2338 updateCursorPosition(tPtr
);
2343 if(!(tb
= tPtr
->currentTextBlock
))
2349 L_imaGFX
: if(tb
->prior
) {
2350 tPtr
->currentTextBlock
= tb
->prior
;
2351 if(tPtr
->currentTextBlock
->graphic
)
2354 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2356 if(!tb
->first
&& tPtr
->tpos
> 0)
2358 } else tPtr
->tpos
= 0;
2359 } else tPtr
->tpos
--;
2360 updateCursorPosition(tPtr
);
2365 if(!(tb
= tPtr
->currentTextBlock
))
2369 if(tPtr
->tpos
== tb
->used
) {
2370 R_imaGFX
: if(tb
->next
) {
2371 tPtr
->currentTextBlock
= tb
->next
;
2373 if(!tb
->next
->first
&& tb
->next
->used
>0)
2379 tPtr
->tpos
= tb
->used
;
2381 } else tPtr
->tpos
++;
2382 updateCursorPosition(tPtr
);
2387 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2388 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2393 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2394 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2403 deleteTextInteractively(tPtr
, ksym
);
2404 updateCursorPosition(tPtr
);
2410 control_pressed
= True
;
2414 insertTextInteractively(tPtr
, " ", 4);
2415 updateCursorPosition(tPtr
);
2422 if (*buffer
!= 0 && !control_pressed
) {
2423 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2424 updateCursorPosition(tPtr
);
2427 } else if (control_pressed
&& ksym
==XK_r
) {
2428 Bool i
= !tPtr
->flags
.rulerShown
;
2429 WMShowTextRuler(tPtr
, i
);
2430 tPtr
->flags
.rulerShown
= i
;
2432 else if (control_pressed
&& *buffer
== '\a')
2433 XBell(tPtr
->view
->screen
->display
, 0);
2435 WMRelayToNextResponder(tPtr
->view
, event
);
2438 if (!control_pressed
&& tPtr
->flags
.ownsSelection
)
2439 releaseSelection(tPtr
);
2444 pasteText(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2445 void *cdata
, WMData
*data
)
2447 Text
*tPtr
= (Text
*)view
->self
;
2450 tPtr
->flags
.waitingForSelection
= 0;
2453 text
= (char*)WMDataBytes(data
);
2456 (tPtr
->parser
) (tPtr
, (void *) text
);
2457 layOutDocument(tPtr
);
2458 } else insertTextInteractively(tPtr
, text
, strlen(text
));
2459 updateCursorPosition(tPtr
);
2465 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2470 (tPtr
->parser
) (tPtr
, (void *) text
);
2471 layOutDocument(tPtr
);
2472 } else insertTextInteractively(tPtr
, text
, n
);
2473 updateCursorPosition(tPtr
);
2485 handleActionEvents(XEvent
*event
, void *data
)
2487 Text
*tPtr
= (Text
*)data
;
2488 Display
*dpy
= event
->xany
.display
;
2492 switch (event
->type
) {
2494 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2495 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2496 tPtr
->flags
.extendSelection
= True
;
2500 if (tPtr
->flags
.focused
) {
2501 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2502 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
2503 GrabModeAsync
, GrabModeAsync
, None
,
2504 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2505 tPtr
->flags
.pointerGrabbed
= True
;
2506 handleTextKeyPress(tPtr
, event
);
2511 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2512 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2513 tPtr
->flags
.extendSelection
= False
;
2515 /* end modify flag so selection can be extended */
2522 if (tPtr
->flags
.pointerGrabbed
) {
2523 tPtr
->flags
.pointerGrabbed
= False
;
2524 XUngrabPointer(dpy
, CurrentTime
);
2527 if(tPtr
->flags
.waitingForSelection
)
2530 if ((event
->xmotion
.state
& Button1Mask
)) {
2531 TextBlock
*tb
= tPtr
->currentTextBlock
;
2533 if(tb
&& tPtr
->flags
.isOverGraphic
&&
2534 tb
->graphic
&& !tb
->object
) {
2536 WMPixmap
*pixmap
= tb
->d
.pixmap
;
2537 char *types
[2] = {"application/X-image", NULL
};
2542 WMDragImageFromView(tPtr
->view
, pixmap
, types
,
2543 wmkpoint(event
->xmotion
.x_root
, event
->xmotion
.y_root
),
2548 if (!tPtr
->flags
.ownsSelection
) {
2549 WMCreateSelectionHandler(tPtr
->view
,
2550 XA_PRIMARY
, event
->xbutton
.time
,
2551 &selectionHandler
, NULL
);
2552 tPtr
->flags
.ownsSelection
= True
;
2555 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2559 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2565 if (tPtr
->flags
.pointerGrabbed
) {
2566 tPtr
->flags
.pointerGrabbed
= False
;
2567 XUngrabPointer(dpy
, CurrentTime
);
2571 if (tPtr
->flags
.waitingForSelection
)
2574 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2575 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2579 if (tPtr
->flags
.ownsSelection
)
2580 releaseSelection(tPtr
);
2583 if (event
->xbutton
.button
== Button1
) {
2585 if(WMIsDoubleClick(event
)) {
2586 TextBlock
*tb
= tPtr
->currentTextBlock
;
2588 tPtr
->lastClickTime
= event
->xbutton
.time
;
2589 if(tb
&& tb
->graphic
&& !tb
->object
) {
2590 if(tPtr
->delegate
&& tPtr
->delegate
->didDoubleClickOnPicture
) {
2593 desc
= wmalloc(tb
->used
+1);
2594 memcpy(desc
, tb
->text
, tb
->used
);
2596 (*tPtr
->delegate
->didDoubleClickOnPicture
)(tPtr
->delegate
, desc
);
2600 autoSelectText(tPtr
, 2);
2603 } else if(event
->xbutton
.time
- tPtr
->lastClickTime
2604 < WINGsConfiguration
.doubleClickDelay
) {
2605 tPtr
->lastClickTime
= event
->xbutton
.time
;
2606 autoSelectText(tPtr
, 3);
2610 if (!tPtr
->flags
.focused
) {
2611 WMSetFocusToWidget(tPtr
);
2612 tPtr
->flags
.focused
= True
;
2615 tPtr
->lastClickTime
= event
->xbutton
.time
;
2616 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2620 if (event
->xbutton
.button
2621 == WINGsConfiguration
.mouseWheelDown
) {
2622 WMScrollText(tPtr
, 16);
2626 if (event
->xbutton
.button
2627 == WINGsConfiguration
.mouseWheelUp
) {
2628 WMScrollText(tPtr
, -16);
2632 if (event
->xbutton
.button
== Button2
) {
2636 if (!tPtr
->flags
.editable
) {
2641 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2642 event
->xbutton
.time
, pasteText
, NULL
)) {
2644 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2645 tPtr
->flags
.waitingForSelection
= 0;
2651 (tPtr
->parser
) (tPtr
, (void *) text
);
2652 layOutDocument(tPtr
);
2655 insertTextInteractively(tPtr
, text
, n
);
2659 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2660 (void*)WMInsertTextEvent
);
2662 updateCursorPosition(tPtr
);
2666 tPtr
->flags
.waitingForSelection
= True
;
2674 if (tPtr
->flags
.pointerGrabbed
) {
2675 tPtr
->flags
.pointerGrabbed
= False
;
2676 XUngrabPointer(dpy
, CurrentTime
);
2680 if (tPtr
->flags
.waitingForSelection
)
2688 handleEvents(XEvent
*event
, void *data
)
2690 Text
*tPtr
= (Text
*)data
;
2692 switch(event
->type
) {
2695 if (event
->xexpose
.count
!=0)
2699 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2700 WMRealizeWidget(tPtr
->hS
);
2704 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2705 WMRealizeWidget(tPtr
->vS
);
2709 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2710 WMRealizeWidget(tPtr
->ruler
);
2715 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2721 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2724 tPtr
->flags
.focused
= True
;
2726 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2727 tPtr
->timerID
= WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY
,
2735 tPtr
->flags
.focused
= False
;
2738 if (tPtr
->timerID
) {
2739 WMDeleteTimerHandler(tPtr
->timerID
);
2740 tPtr
->timerID
= NULL
;
2749 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2751 WMEmptyArray(tPtr
->gfxItems
);
2754 WMDeleteTimerHandler(tPtr
->timerID
);
2756 WMReleaseFont(tPtr
->dFont
);
2757 WMReleaseColor(tPtr
->dColor
);
2758 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2759 WMRemoveNotificationObserver(tPtr
);
2770 insertPlainText(Text
*tPtr
, char *text
)
2777 mark
= strchr(start
, '\n');
2779 tb
= WMCreateTextBlockWithText(tPtr
,
2781 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
-start
));
2783 tPtr
->flags
.first
= True
;
2785 if (start
&& strlen(start
)) {
2786 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2787 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2789 tPtr
->flags
.first
= False
;
2793 if (tPtr
->flags
.prepend
)
2794 WMPrependTextBlock(tPtr
, tb
);
2796 WMAppendTextBlock(tPtr
, tb
);
2802 rulerMoveCallBack(WMWidget
*w
, void *self
)
2804 Text
*tPtr
= (Text
*)self
;
2808 if (W_CLASS(tPtr
) != WC_Text
)
2816 rulerReleaseCallBack(WMWidget
*w
, void *self
)
2818 Text
*tPtr
= (Text
*)self
;
2822 if (W_CLASS(tPtr
) != WC_Text
)
2830 draggingSourceOperation(WMView
*self
, Bool local
)
2832 return WDOperationCopy
;
2836 fetchDragData(WMView
*self
, char *type
)
2838 TextBlock
*tb
= ((WMText
*)self
->self
)->currentTextBlock
;
2845 printf("type is [%s]\n", type
);
2846 desc
= wmalloc(tb
->used
+1);
2847 memcpy(desc
, tb
->text
, tb
->used
);
2849 data
= WMCreateDataWithBytes(desc
, strlen(desc
)+1);
2857 static WMDragSourceProcs _DragSourceProcs
= {
2858 draggingSourceOperation
,
2866 draggingEntered(WMView
*self
, WMDraggingInfo
*info
)
2868 printf("draggingEntered\n");
2869 return WDOperationCopy
;
2874 draggingUpdated(WMView
*self
, WMDraggingInfo
*info
)
2876 return WDOperationCopy
;
2881 draggingExited(WMView
*self
, WMDraggingInfo
*info
)
2883 printf("draggingExited\n");
2887 prepareForDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2889 printf("prepareForDragOperation\n");
2897 receivedData(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2898 void *cdata
, WMData
*data
)
2900 badbadbad
= wstrdup((char *)WMDataBytes(data
));
2904 /* when it's done in WINGs, remove this */
2907 requestDroppedData(WMView
*view
, WMDraggingInfo
*info
, char *type
)
2909 WMScreen
*scr
= W_VIEW_SCREEN(view
);
2911 if (!WMRequestSelection(scr
->dragInfo
.destView
,
2912 scr
->xdndSelectionAtom
,
2913 XInternAtom(scr
->display
, type
, False
),
2914 scr
->dragInfo
.timestamp
,
2915 receivedData
, &scr
->dragInfo
)) {
2916 wwarning("could not request data for dropped data");
2922 ev
.type
= ClientMessage
;
2923 ev
.xclient
.message_type
= scr
->xdndFinishedAtom
;
2924 ev
.xclient
.format
= 32;
2925 ev
.xclient
.window
= info
->destinationWindow
;
2926 ev
.xclient
.data
.l
[0] = 0;
2927 ev
.xclient
.data
.l
[1] = 0;
2928 ev
.xclient
.data
.l
[2] = 0;
2929 ev
.xclient
.data
.l
[3] = 0;
2930 ev
.xclient
.data
.l
[4] = 0;
2932 XSendEvent(scr
->display
, info
->sourceWindow
, False
, 0, &ev
);
2933 XFlush(scr
->display
);
2939 performDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2942 WMText
*tPtr
= (WMText
*)self
->self
;
2947 requestDroppedData(tPtr
->view
, info
, "application/X-color");
2948 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), badbadbad
, True
);
2950 WMSetTextSelectionColor(tPtr
, color
);
2951 WMReleaseColor(color
);
2960 concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2962 printf("concludeDragOperation\n");
2966 static WMDragDestinationProcs _DragDestinationProcs
= {
2970 prepareForDragOperation
,
2971 performDragOperation
,
2972 concludeDragOperation
2977 getStream(WMText
*tPtr
, int sel
, int array
)
2979 TextBlock
*tb
= NULL
;
2981 unsigned long where
= 0;
2986 if (!(tb
= tPtr
->firstTextBlock
))
2990 (tPtr
->writer
) (tPtr
, (void *) text
);
2994 tb
= tPtr
->firstTextBlock
;
2997 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2999 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
3001 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
3002 && tb
!= tPtr
->firstTextBlock
) {
3003 text
= wrealloc(text
, where
+1);
3004 text
[where
++] = '\n';
3010 if(tb
->graphic
&& array
) {
3011 text
= wrealloc(text
, where
+4);
3012 text
[where
++] = 0xFA;
3013 text
[where
++] = (tb
->used
>>8)&0x0ff;
3014 text
[where
++] = tb
->used
&0x0ff;
3015 text
[where
++] = tb
->allocated
; /* extra info */
3017 text
= wrealloc(text
, where
+tb
->used
);
3018 memcpy(&text
[where
], tb
->text
, tb
->used
);
3022 } else if (sel
&& tb
->selected
) {
3024 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
3025 text
= wrealloc(text
, where
+1);
3026 text
[where
++] = '\n';
3032 text
= wrealloc(text
, where
+(tb
->s_end
- tb
->s_begin
));
3033 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
],
3034 tb
->s_end
- tb
->s_begin
);
3035 where
+= tb
->s_end
- tb
->s_begin
;
3040 _gSnext
:tb
= tb
->next
;
3043 /* +1 for the end of string, let's be nice */
3044 text
= wrealloc(text
, where
+1);
3051 releaseStreamObjects(void *data
)
3058 getStreamObjects(WMText
*tPtr
, int sel
)
3060 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
3064 char *start
, *fa
, *desc
;
3066 stream
= getStream(tPtr
, sel
, 1);
3073 fa
= strchr(start
, 0xFA);
3075 if((int)(fa
- start
)>0) {
3077 desc
[(int)(fa
- start
)] = 0;
3078 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
3079 WMSetDataFormat(data
, TYPETEXT
);
3080 WMAddToArray(array
, (void *) data
);
3083 len
= *(fa
+1)*0xff + *(fa
+2);
3084 data
= WMCreateDataWithBytes((void *)(fa
+4), len
);
3085 WMSetDataFormat(data
, *(fa
+3));
3086 WMAddToArray(array
, (void *) data
);
3087 start
= fa
+ len
+ 4;
3090 if (start
&& strlen(start
)) {
3091 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
3092 WMSetDataFormat(data
, TYPETEXT
);
3093 WMAddToArray(array
, (void *) data
);
3105 WMCreateTextForDocumentType(WMWidget
*parent
, WMAction
*parser
, WMAction
*writer
)
3112 tPtr
= wmalloc(sizeof(Text
));
3113 memset(tPtr
, 0, sizeof(Text
));
3114 tPtr
->widgetClass
= WC_Text
;
3115 tPtr
->view
= W_CreateView(W_VIEW(parent
));
3117 perror("could not create text's view\n");
3122 dpy
= tPtr
->view
->screen
->display
;
3123 scr
= tPtr
->view
->screen
;
3125 tPtr
->view
->self
= tPtr
;
3126 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
3127 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3128 W_ResizeView(tPtr
->view
, 250, 200);
3130 tPtr
->dColor
= WMBlackColor(scr
);
3131 tPtr
->fgColor
= WMBlackColor(scr
);
3132 tPtr
->bgColor
= WMWhiteColor(scr
);
3133 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3135 gcv
.graphics_exposures
= False
;
3136 gcv
.foreground
= W_PIXEL(scr
->gray
);
3137 gcv
.background
= W_PIXEL(scr
->darkGray
);
3138 gcv
.fill_style
= FillStippled
;
3139 /* why not use scr->stipple here? */
3140 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
,
3141 STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
3142 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
3143 GCForeground
|GCBackground
|GCStipple
3144 |GCFillStyle
|GCGraphicsExposures
, &gcv
);
3150 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
3152 tPtr
->view
->delegate
= &_TextViewDelegate
;
3154 tPtr
->delegate
= NULL
;
3157 tPtr
->timerID
= NULL
;
3160 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
3161 |EnterWindowMask
|LeaveWindowMask
|FocusChangeMask
,
3162 handleEvents
, tPtr
);
3164 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
|ButtonPressMask
3165 |KeyReleaseMask
|KeyPressMask
|Button1MotionMask
,
3166 handleActionEvents
, tPtr
);
3168 WMAddNotificationObserver(ownershipObserver
, tPtr
,
3169 WMSelectionOwnerDidChangeNotification
,
3172 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3173 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3177 char *types
[3] = {"application/X-color", "application/X-image", NULL
};
3178 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3181 /*WMAddNotificationObserver(fontChanged, tPtr,
3182 WMFontPanelDidChangeNotification, tPtr);*/
3184 tPtr
->firstTextBlock
= NULL
;
3185 tPtr
->lastTextBlock
= NULL
;
3186 tPtr
->currentTextBlock
= NULL
;
3189 tPtr
->gfxItems
= WMCreateArray(4);
3191 tPtr
->parser
= parser
;
3192 tPtr
->writer
= writer
;
3194 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3195 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3197 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3199 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3200 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3201 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3203 tPtr
->cursor
.x
= -23;
3206 tPtr
->docHeight
= 0;
3207 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
,
3209 tPtr
->db
= (Pixmap
) NULL
;
3210 tPtr
->bgPixmap
= NULL
;
3212 tPtr
->margins
= WMGetRulerMargins(NULL
);
3213 tPtr
->margins
->right
= tPtr
->visible
.w
;
3216 tPtr
->flags
.rulerShown
= False
;
3217 tPtr
->flags
.monoFont
= False
;
3218 tPtr
->flags
.focused
= False
;
3219 tPtr
->flags
.editable
= True
;
3220 tPtr
->flags
.ownsSelection
= False
;
3221 tPtr
->flags
.pointerGrabbed
= False
;
3222 tPtr
->flags
.extendSelection
= False
;
3223 tPtr
->flags
.frozen
= False
;
3224 tPtr
->flags
.cursorShown
= True
;
3225 tPtr
->flags
.acceptsGraphic
= False
;
3226 tPtr
->flags
.horizOnDemand
= False
;
3227 tPtr
->flags
.needsLayOut
= False
;
3228 tPtr
->flags
.ignoreNewLine
= False
;
3229 tPtr
->flags
.indentNewLine
= False
;
3230 tPtr
->flags
.laidOut
= False
;
3231 tPtr
->flags
.ownsSelection
= False
;
3232 tPtr
->flags
.waitingForSelection
= False
;
3233 tPtr
->flags
.prepend
= False
;
3234 tPtr
->flags
.isOverGraphic
= False
;
3235 tPtr
->flags
.relief
= WRSunken
;
3236 tPtr
->flags
.isOverGraphic
= 0;
3237 tPtr
->flags
.alignment
= WALeft
;
3238 tPtr
->flags
.first
= True
;
3244 WMPrependTextStream(WMText
*tPtr
, char *text
)
3246 CHECK_CLASS(tPtr
, WC_Text
);
3249 if (tPtr
->flags
.ownsSelection
)
3250 releaseSelection(tPtr
);
3252 updateScrollers(tPtr
);
3256 tPtr
->flags
.prepend
= True
;
3257 if (text
&& tPtr
->parser
)
3258 (tPtr
->parser
) (tPtr
, (void *) text
);
3260 insertPlainText(tPtr
, text
);
3262 tPtr
->flags
.needsLayOut
= True
;
3264 if(!tPtr
->flags
.frozen
) {
3265 layOutDocument(tPtr
);
3271 WMAppendTextStream(WMText
*tPtr
, char *text
)
3273 CHECK_CLASS(tPtr
, WC_Text
);
3276 if (tPtr
->flags
.ownsSelection
)
3277 releaseSelection(tPtr
);
3279 updateScrollers(tPtr
);
3283 tPtr
->flags
.prepend
= False
;
3284 if (text
&& tPtr
->parser
)
3285 (tPtr
->parser
) (tPtr
, (void *) text
);
3287 insertPlainText(tPtr
, text
);
3289 tPtr
->flags
.needsLayOut
= True
;
3290 if(tPtr
->currentTextBlock
) {
3291 if(tPtr
->currentTextBlock
->graphic
)
3294 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3297 if(!tPtr
->flags
.frozen
) {
3298 layOutDocument(tPtr
);
3304 WMGetTextStream(WMText
*tPtr
)
3306 CHECK_CLASS(tPtr
, WC_Text
);
3307 return getStream(tPtr
, 0, 0);
3311 WMGetTextSelectedStream(WMText
*tPtr
)
3313 CHECK_CLASS(tPtr
, WC_Text
);
3314 return getStream(tPtr
, 1, 0);
3318 WMGetTextObjects(WMText
*tPtr
)
3320 CHECK_CLASS(tPtr
, WC_Text
);
3321 return getStreamObjects(tPtr
, 0);
3325 WMGetTextSelectedObjects(WMText
*tPtr
)
3327 CHECK_CLASS(tPtr
, WC_Text
);
3328 return getStreamObjects(tPtr
, 1);
3333 WMSetTextDelegate(WMText
*tPtr
, WMTextDelegate
*delegate
)
3335 CHECK_CLASS(tPtr
, WC_Text
);
3337 tPtr
->delegate
= delegate
;
3342 WMCreateTextBlockWithObject(WMText
*tPtr
, WMWidget
*w
,
3343 char *description
, WMColor
*color
,
3344 unsigned short first
, unsigned short extraInfo
)
3348 if (!w
|| !description
|| !color
)
3351 tb
= wmalloc(sizeof(TextBlock
));
3355 tb
->text
= wstrdup(description
);
3356 tb
->used
= strlen(description
);
3359 tb
->color
= WMRetainColor(color
);
3360 tb
->marginN
= newMargin(tPtr
, NULL
);
3361 tb
->allocated
= extraInfo
;
3366 tb
->underlined
= False
;
3367 tb
->selected
= False
;
3369 tb
->sections
= NULL
;
3379 WMCreateTextBlockWithPixmap(WMText
*tPtr
, WMPixmap
*p
,
3380 char *description
, WMColor
*color
,
3381 unsigned short first
, unsigned short extraInfo
)
3385 if (!p
|| !description
|| !color
)
3388 tb
= wmalloc(sizeof(TextBlock
));
3392 tb
->text
= wstrdup(description
);
3393 tb
->used
= strlen(description
);
3395 tb
->d
.pixmap
= WMRetainPixmap(p
);
3396 tb
->color
= WMRetainColor(color
);
3397 tb
->marginN
= newMargin(tPtr
, NULL
);
3398 tb
->allocated
= extraInfo
;
3403 tb
->underlined
= False
;
3404 tb
->selected
= False
;
3406 tb
->sections
= NULL
;
3416 WMCreateTextBlockWithText(WMText
*tPtr
, char *text
, WMFont
*font
, WMColor
*color
,
3417 unsigned short first
, unsigned short len
)
3421 if (!font
|| !color
)
3424 tb
= wmalloc(sizeof(TextBlock
));
3428 tb
->allocated
= reqBlockSize(len
);
3429 tb
->text
= (char *)wmalloc(tb
->allocated
);
3430 memset(tb
->text
, 0, tb
->allocated
);
3432 if (len
< 1|| !text
|| (*text
== '\n' && len
==1 )) {
3437 memcpy(tb
->text
, text
, len
);
3441 tb
->text
[tb
->used
] = 0;
3443 tb
->d
.font
= WMRetainFont(font
);
3444 tb
->color
= WMRetainColor(color
);
3445 tb
->marginN
= newMargin(tPtr
, NULL
);
3448 tb
->graphic
= False
;
3449 tb
->underlined
= False
;
3450 tb
->selected
= False
;
3452 tb
->sections
= NULL
;
3460 WMSetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int first
,
3461 unsigned int kanji
, unsigned int underlined
, int script
,
3462 WMRulerMargins
*margins
)
3464 TextBlock
*tb
= (TextBlock
*) vtb
;
3470 tb
->underlined
= underlined
;
3471 tb
->script
= script
;
3472 tb
->marginN
= newMargin(tPtr
, margins
);
3476 WMGetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int *first
,
3477 unsigned int *kanji
, unsigned int *underlined
, int *script
,
3478 WMRulerMargins
*margins
)
3480 TextBlock
*tb
= (TextBlock
*) vtb
;
3484 if (first
) *first
= tb
->first
;
3485 if (kanji
) *kanji
= tb
->kanji
;
3486 if (underlined
) *underlined
= tb
->underlined
;
3487 if (script
) *script
= tb
->script
;
3488 if (margins
) margins
= &tPtr
->margins
[tb
->marginN
];
3494 WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3496 TextBlock
*tb
= (TextBlock
*)vtb
;
3503 WMWidget
*w
= tb
->d
.widget
;
3504 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3505 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3506 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3509 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3513 tPtr
->tpos
= tb
->used
;
3516 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3517 tb
->next
= tb
->prior
= NULL
;
3519 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3520 = tPtr
->currentTextBlock
= tb
;
3525 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3528 tb
->next
= tPtr
->currentTextBlock
;
3529 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3530 if (tPtr
->currentTextBlock
->prior
)
3531 tPtr
->currentTextBlock
->prior
->next
= tb
;
3533 tPtr
->currentTextBlock
->prior
= tb
;
3535 tPtr
->firstTextBlock
= tb
;
3537 tPtr
->currentTextBlock
= tb
;
3542 WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3544 TextBlock
*tb
= (TextBlock
*)vtb
;
3551 WMWidget
*w
= tb
->d
.widget
;
3552 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3553 (W_VIEW(w
))->attribs
.cursor
=
3554 tPtr
->view
->screen
->defaultCursor
;
3555 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3558 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3562 tPtr
->tpos
= tb
->used
;
3566 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3567 tb
->next
= tb
->prior
= NULL
;
3569 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3570 = tPtr
->currentTextBlock
= tb
;
3575 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3578 tb
->next
= tPtr
->currentTextBlock
->next
;
3579 tb
->prior
= tPtr
->currentTextBlock
;
3580 if (tPtr
->currentTextBlock
->next
)
3581 tPtr
->currentTextBlock
->next
->prior
= tb
;
3583 tPtr
->currentTextBlock
->next
= tb
;
3586 tPtr
->lastTextBlock
= tb
;
3588 tPtr
->currentTextBlock
= tb
;
3593 WMRemoveTextBlock(WMText
*tPtr
)
3595 TextBlock
*tb
= NULL
;
3597 if (!tPtr
|| !tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
3598 || !tPtr
->currentTextBlock
) {
3602 tb
= tPtr
->currentTextBlock
;
3604 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3607 WMUnmapWidget(tb
->d
.widget
);
3611 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3612 if (tPtr
->currentTextBlock
->next
)
3613 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3615 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3616 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3618 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3619 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3620 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3621 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3623 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3624 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3625 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3634 destroyWidget(WMWidget
*widget
)
3636 WMDestroyWidget(widget
);
3637 // -- never do this -- wfree(widget);
3643 WMDestroyTextBlock(WMText
*tPtr
, void *vtb
)
3645 TextBlock
*tb
= (TextBlock
*)vtb
;
3651 /* naturally, there's a danger to destroying widgets whose action
3652 * brings us here: ie. press a button to destroy it...
3653 * need to find a safer way. till then... this stays commented out */
3654 /* 5 months later... destroy it 10 seconds after now which should
3655 * be enough time for the widget's action to be completed... :-) */
3656 /* This is a bad assumption. Just destroy the widget here.
3657 * if the caller needs it, it can protect it with W_RetainView()
3658 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3659 WMDestroyWidget(tb
->d
.widget
);
3661 WMReleasePixmap(tb
->d
.pixmap
);
3664 WMReleaseFont(tb
->d
.font
);
3667 WMReleaseColor(tb
->color
);
3668 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3670 wfree(tb
->sections
);
3677 WMSetTextForegroundColor(WMText
*tPtr
, WMColor
*color
)
3682 WMReleaseColor(tPtr
->fgColor
);
3683 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3689 WMSetTextBackgroundColor(WMText
*tPtr
, WMColor
*color
)
3694 WMReleaseColor(tPtr
->bgColor
);
3695 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3696 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3701 void WMSetTextBackgroundPixmap(WMText
*tPtr
, WMPixmap
*pixmap
)
3707 WMReleasePixmap(tPtr
->bgPixmap
);
3710 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3712 tPtr
->bgPixmap
= NULL
;
3716 WMSetTextRelief(WMText
*tPtr
, WMReliefType relief
)
3720 tPtr
->flags
.relief
= relief
;
3721 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3725 WMSetTextHasHorizontalScroller(WMText
*tPtr
, Bool shouldhave
)
3730 if (shouldhave
&& !tPtr
->hS
) {
3731 tPtr
->hS
= WMCreateScroller(tPtr
);
3732 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3733 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3734 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3735 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3736 WMMapWidget(tPtr
->hS
);
3737 } else if (!shouldhave
&& tPtr
->hS
) {
3738 WMUnmapWidget(tPtr
->hS
);
3739 WMDestroyWidget(tPtr
->hS
);
3745 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3750 WMSetTextHasRuler(WMText
*tPtr
, Bool shouldhave
)
3755 if(shouldhave
&& !tPtr
->ruler
) {
3756 tPtr
->ruler
= WMCreateRuler(tPtr
);
3757 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
=
3758 tPtr
->view
->screen
->defaultCursor
;
3759 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3760 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3761 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3762 } else if(!shouldhave
&& tPtr
->ruler
) {
3763 WMShowTextRuler(tPtr
, False
);
3764 WMDestroyWidget(tPtr
->ruler
);
3767 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3771 WMShowTextRuler(WMText
*tPtr
, Bool show
)
3778 if(tPtr
->flags
.monoFont
)
3781 tPtr
->flags
.rulerShown
= show
;
3783 WMMapWidget(tPtr
->ruler
);
3785 WMUnmapWidget(tPtr
->ruler
);
3788 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3792 WMGetTextRulerShown(WMText
*tPtr
)
3800 return tPtr
->flags
.rulerShown
;
3805 WMSetTextHasVerticalScroller(WMText
*tPtr
, Bool shouldhave
)
3810 if (shouldhave
&& !tPtr
->vS
) {
3811 tPtr
->vS
= WMCreateScroller(tPtr
);
3812 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3813 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3814 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3815 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3816 WMMapWidget(tPtr
->vS
);
3817 } else if (!shouldhave
&& tPtr
->vS
) {
3818 WMUnmapWidget(tPtr
->vS
);
3819 WMDestroyWidget(tPtr
->vS
);
3825 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3831 WMScrollText(WMText
*tPtr
, int amount
)
3836 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3840 if (tPtr
->vpos
> 0) {
3841 if (tPtr
->vpos
> abs(amount
)) tPtr
->vpos
+= amount
;
3846 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3847 if (tPtr
->vpos
< limit
) {
3848 if (tPtr
->vpos
< limit
-amount
) tPtr
->vpos
+= amount
;
3849 else tPtr
->vpos
= limit
;
3854 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3855 updateScrollers(tPtr
);
3858 tPtr
->prevVpos
= tPtr
->vpos
;
3863 WMPageText(WMText
*tPtr
, Bool direction
)
3865 if (!tPtr
) return False
;
3866 if (!tPtr
->view
->flags
.realized
) return False
;
3868 return WMScrollText(tPtr
, direction
?tPtr
->visible
.h
:-tPtr
->visible
.h
);
3872 WMSetTextEditable(WMText
*tPtr
, Bool editable
)
3876 tPtr
->flags
.editable
= editable
;
3880 WMGetTextEditable(WMText
*tPtr
)
3884 return tPtr
->flags
.editable
;
3888 WMSetTextIndentNewLines(WMText
*tPtr
, Bool indent
)
3892 tPtr
->flags
.indentNewLine
= indent
;
3896 WMSetTextIgnoresNewline(WMText
*tPtr
, Bool ignore
)
3900 tPtr
->flags
.ignoreNewLine
= ignore
;
3904 WMGetTextIgnoresNewline(WMText
*tPtr
)
3908 return tPtr
->flags
.ignoreNewLine
;
3912 WMSetTextUsesMonoFont(WMText
*tPtr
, Bool mono
)
3918 if(tPtr
->flags
.rulerShown
)
3919 WMShowTextRuler(tPtr
, False
);
3920 if(tPtr
->flags
.alignment
!= WALeft
)
3921 tPtr
->flags
.alignment
= WALeft
;
3924 tPtr
->flags
.monoFont
= mono
;
3925 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3929 WMGetTextUsesMonoFont(WMText
*tPtr
)
3933 return tPtr
->flags
.monoFont
;
3938 WMSetTextDefaultFont(WMText
*tPtr
, WMFont
*font
)
3943 WMReleaseFont(tPtr
->dFont
);
3945 tPtr
->dFont
= WMRetainFont(font
);
3947 tPtr
->dFont
= WMRetainFont(WMSystemFontOfSize(tPtr
->view
->screen
, 12));
3951 WMGetTextDefaultFont(WMText
*tPtr
)
3956 return WMRetainFont(tPtr
->dFont
);
3960 WMSetTextDefaultColor(WMText
*tPtr
, WMColor
*color
)
3965 WMReleaseColor(tPtr
->dColor
);
3967 tPtr
->dColor
= WMRetainColor(color
);
3969 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3973 WMGetTextDefaultColor(WMText
*tPtr
)
3978 return WMRetainColor(tPtr
->dColor
);
3982 WMSetTextAlignment(WMText
*tPtr
, WMAlignment alignment
)
3986 if(tPtr
->flags
.monoFont
)
3987 tPtr
->flags
.alignment
= WALeft
;
3989 tPtr
->flags
.alignment
= alignment
;
3994 WMGetTextInsertType(WMText
*tPtr
)
3998 return tPtr
->flags
.prepend
;
4003 WMSetTextSelectionColor(WMText
*tPtr
, WMColor
*color
)
4005 if (!tPtr
|| !color
)
4008 setSelectionProperty(tPtr
, NULL
, color
, -1);
4012 WMGetTextSelectionColor(WMText
*tPtr
)
4019 tb
= tPtr
->currentTextBlock
;
4021 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4032 WMSetTextSelectionFont(WMText
*tPtr
, WMFont
*font
)
4037 setSelectionProperty(tPtr
, font
, NULL
, -1) ;
4041 WMGetTextSelectionFont(WMText
*tPtr
)
4048 tb
= tPtr
->currentTextBlock
;
4050 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4057 tb
= getFirstNonGraphicBlockFor(tb
, 1);
4061 return (tb
->selected
? tb
->d
.font
: NULL
);
4066 WMSetTextSelectionUnderlined(WMText
*tPtr
, int underlined
)
4068 if (!tPtr
|| (underlined
!=0 && underlined
!=1))
4071 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
4076 WMGetTextSelectionUnderlined(WMText
*tPtr
)
4083 tb
= tPtr
->currentTextBlock
;
4085 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4091 return tb
->underlined
;
4096 WMFreezeText(WMText
*tPtr
)
4101 tPtr
->flags
.frozen
= True
;
4106 WMThawText(WMText
*tPtr
)
4111 tPtr
->flags
.frozen
= False
;
4113 if(tPtr
->flags
.monoFont
) {
4114 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
4117 /* make sure to unmap widgets no matter where they are */
4118 /* they'll be later remapped if needed by paintText */
4119 for(j
=0; j
<c
; j
++) {
4120 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
4121 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
4122 WMUnmapWidget(tb
->d
.widget
);
4128 tPtr
->flags
.laidOut
= False
;
4129 layOutDocument(tPtr
);
4130 updateScrollers(tPtr
);
4132 tPtr
->flags
.needsLayOut
= False
;
4136 /* find first occurence of a string */
4138 mystrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4143 if(!haystack
|| !needle
|| !end
)
4146 for (ptr
= haystack
; ptr
< end
; ptr
++) {
4148 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4152 if (tolower(*ptr
) == tolower(*needle
) &&
4153 !strncasecmp(ptr
, needle
, len
))
4161 /* find last occurence of a string */
4163 mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4168 if(!haystack
|| !needle
|| !end
)
4171 for (ptr
= haystack
-2; ptr
> end
; ptr
--) {
4173 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4176 if (tolower(*ptr
) == tolower(*needle
) &&
4177 !strncasecmp(ptr
, needle
, len
))
4187 WMFindInTextStream(WMText
*tPtr
, char *needle
, Bool direction
,
4194 if (!tPtr
|| !needle
)
4198 if (! (tb
= tPtr
->currentTextBlock
)) {
4199 if (! (tb
= ( (direction
> 0) ?
4200 tPtr
->firstTextBlock
: tPtr
->lastTextBlock
) ) ){
4204 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4205 tb = (direction>0) ? tb->next : tb->prior; */
4206 if(tb
!= tPtr
->lastTextBlock
)
4210 tb
= tPtr
->currentTextBlock
;
4218 if(pos
+1 < tb
->used
)
4221 if(tb
->used
- pos
> 0 && pos
> 0) {
4222 mark
= mystrstr(&tb
->text
[pos
], needle
,
4223 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
4236 mark
= mystrrstr(&tb
->text
[pos
], needle
,
4237 strlen(needle
), tb
->text
, caseSensitive
);
4249 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
4251 tPtr
->tpos
= (int)(mark
- tb
->text
);
4252 tPtr
->currentTextBlock
= tb
;
4253 updateCursorPosition(tPtr
);
4254 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
4255 tPtr
->sel
.h
= tPtr
->cursor
.h
-10;
4256 tPtr
->sel
.x
= tPtr
->cursor
.x
+1;
4257 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
4258 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
4259 tPtr
->docWidth
- tPtr
->sel
.x
);
4260 tPtr
->flags
.ownsSelection
= True
;
4267 tb
= (direction
>0) ? tb
->next
: tb
->prior
;
4269 pos
= (direction
>0) ? 0 : tb
->used
;
4278 WMReplaceTextSelection(WMText
*tPtr
, char *replacement
)
4283 if (!tPtr
->flags
.ownsSelection
)
4286 removeSelection(tPtr
);
4289 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
4290 updateCursorPosition(tPtr
);