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));*/
1477 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1478 text
= &(tb
->text
[items
[i
].begin
]);
1479 len
= items
[i
].end
- items
[i
].begin
;
1480 if (tPtr
->flags
.alignment
!= WALeft
)
1481 lw
+= WMWidthOfString(font
, text
, len
);
1485 if (tPtr
->flags
.alignment
== WARight
) {
1486 j
= tPtr
->visible
.w
- lw
;
1487 } else if (tPtr
->flags
.alignment
== WACenter
) {
1488 j
= (int) ((float)(tPtr
->visible
.w
- lw
))/2.0;
1491 for(i
=0; i
<nitems
; i
++) {
1494 if (tbsame
== tb
) { /* extend it, since it's on same line */
1495 tb
->sections
[tb
->nsections
-1].end
= items
[i
].end
;
1496 n
= tb
->nsections
-1;
1498 tb
->sections
= wrealloc(tb
->sections
,
1499 (++tb
->nsections
)*sizeof(Section
));
1500 n
= tb
->nsections
-1;
1501 tb
->sections
[n
]._y
= y
+ max_d
;
1502 tb
->sections
[n
].max_d
= max_d
;
1503 tb
->sections
[n
].x
= x
+j
;
1504 tb
->sections
[n
].h
= line_height
;
1505 tb
->sections
[n
].begin
= items
[i
].begin
;
1506 tb
->sections
[n
].end
= items
[i
].end
;
1509 tb
->sections
[n
].last
= (i
+1 == nitems
);
1512 if (!tPtr
->flags
.monoFont
) {
1514 WMWidget
*wdt
= tb
->d
.widget
;
1515 tb
->sections
[n
].y
= max_d
+ y
1516 + line_height
- WMWidgetHeight(wdt
);
1517 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1519 tb
->sections
[n
].y
= y
+ line_height
1520 + max_d
- tb
->d
.pixmap
->height
;
1521 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1523 x
+= tb
->sections
[n
].w
;
1526 font
= (tPtr
->flags
.monoFont
)? tPtr
->dFont
: tb
->d
.font
;
1527 len
= items
[i
].end
- items
[i
].begin
;
1528 text
= &(tb
->text
[items
[i
].begin
]);
1530 tb
->sections
[n
].y
= y
+line_height
-font
->y
;
1532 WMWidthOfString(font
,
1533 &(tb
->text
[tb
->sections
[n
].begin
]),
1534 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1536 x
+= WMWidthOfString(font
, text
, len
);
1548 layOutDocument(Text
*tPtr
)
1551 myLineItems
*items
= NULL
;
1552 unsigned int itemsSize
=0, nitems
=0, begin
, end
;
1554 unsigned int x
, y
=0, lw
= 0, width
=0, bmargin
;
1555 char *start
=NULL
, *mark
=NULL
;
1557 if ( tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)) )
1560 assert(tPtr
->visible
.w
> 20);
1562 tPtr
->docWidth
= tPtr
->visible
.w
;
1563 x
= tPtr
->margins
[tb
->marginN
].first
;
1564 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1566 /* only partial layOut needed: re-Lay only affected textblocks */
1567 if (tPtr
->flags
.laidOut
) {
1568 tb
= tPtr
->currentTextBlock
;
1570 /* search backwards for textblocks on same line */
1572 if (!tb
->sections
|| tb
->nsections
<1) {
1573 tb
= tPtr
->firstTextBlock
;
1574 tPtr
->flags
.laidOut
= False
;
1579 if(!tb
->prior
->sections
|| tb
->prior
->nsections
<1) {
1580 tb
= tPtr
->firstTextBlock
;
1581 tPtr
->flags
.laidOut
= False
;
1586 if (tb
->sections
[0]._y
!=
1587 tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
) {
1593 if(tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
>0) {
1594 y
= tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
+
1595 tb
->prior
->sections
[tb
->prior
->nsections
-1].h
-
1596 tb
->prior
->sections
[tb
->prior
->nsections
-1].max_d
;
1605 if (tb
->sections
&& tb
->nsections
>0) {
1606 wfree(tb
->sections
);
1607 tb
->sections
= NULL
;
1611 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1612 TextBlock
*next
= tb
->next
;
1613 tPtr
->currentTextBlock
= tb
;
1614 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1620 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1621 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1622 x
= tPtr
->margins
[tb
->marginN
].first
;
1623 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1629 if (!tPtr
->flags
.monoFont
) {
1631 width
= WMWidgetWidth(tb
->d
.widget
);
1633 width
= tb
->d
.pixmap
->width
;
1635 if (width
> tPtr
->docWidth
)
1636 tPtr
->docWidth
= width
;
1639 if (lw
>= tPtr
->visible
.w
- x
) {
1640 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1646 if(nitems
+ 1> itemsSize
) {
1647 items
= wrealloc(items
,
1648 (++itemsSize
)*sizeof(myLineItems
));
1651 items
[nitems
].tb
= tb
;
1652 items
[nitems
].begin
= 0;
1653 items
[nitems
].end
= 0;
1657 } else if ((start
= tb
->text
)) {
1659 font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
1662 mark
= strchr(start
, ' ');
1664 end
+= (int)(mark
-start
)+1;
1667 end
+= strlen(start
);
1674 if (end
-begin
> 0) {
1676 width
= WMWidthOfString(font
,
1677 &tb
->text
[begin
], end
-begin
);
1679 /* if it won't fit, char wrap it */
1680 if (width
>= tPtr
->visible
.w
) {
1681 char *t
= &tb
->text
[begin
];
1682 int l
=end
-begin
, i
=0;
1684 width
= WMWidthOfString(font
, t
, ++i
);
1685 } while (width
< tPtr
->visible
.w
&& i
< l
);
1688 start
= &tb
->text
[end
];
1694 if (lw
>= tPtr
->visible
.w
- x
) {
1695 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1701 if(nitems
+ 1 > itemsSize
) {
1702 items
= wrealloc(items
,
1703 (++itemsSize
)*sizeof(myLineItems
));
1706 items
[nitems
].tb
= tb
;
1707 items
[nitems
].begin
= begin
;
1708 items
[nitems
].end
= end
;
1716 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1717 if(0&&tPtr
->flags
.laidOut
1718 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
>0
1719 && (tPtr
->vpos
+ tPtr
->visible
.h
1720 < tb
->next
->sections
[0]._y
)) {
1721 if(tPtr
->lastTextBlock
->sections
1722 && tPtr
->lastTextBlock
->nsections
> 0 ) {
1723 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1724 int ly
= ltb
->sections
[ltb
->nsections
-1]._y
;
1725 int lh
= ltb
->sections
[ltb
->nsections
-1].h
;
1728 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
;
1729 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
);
1731 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1733 sd
= tPtr
->docHeight
-y
;
1735 printf("dif %d-%d: %d\n", ss
, sd
, ss
-sd
);
1736 y
+= tb
->next
->sections
[0]._y
-y
;
1738 printf("nitems%d\n", nitems
);
1740 y
= tPtr
->docHeight
+ss
-sd
;
1744 tPtr
->flags
.laidOut
= False
;
1753 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1755 if (tPtr
->docHeight
!= y
+10) {
1756 tPtr
->docHeight
= y
+10;
1757 updateScrollers(tPtr
);
1760 if(tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1763 tPtr
->flags
.horizOnDemand
= True
;
1764 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, True
);
1765 event
.type
= Expose
;
1766 handleEvents(&event
, (void *)tPtr
);
1768 } else if(tPtr
->docWidth
<= tPtr
->visible
.w
1769 && tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1770 tPtr
->flags
.horizOnDemand
= False
;
1771 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, False
);
1774 tPtr
->flags
.laidOut
= True
;
1776 if(items
&& itemsSize
> 0)
1783 textDidResize(W_ViewDelegate
*self
, WMView
*view
)
1785 Text
*tPtr
= (Text
*)view
->self
;
1786 unsigned short w
= tPtr
->view
->size
.width
;
1787 unsigned short h
= tPtr
->view
->size
.height
;
1788 unsigned short rh
= 0, vw
= 0, rel
;
1790 rel
= (tPtr
->flags
.relief
== WRFlat
);
1792 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1793 WMMoveWidget(tPtr
->ruler
, 2, 2);
1794 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1799 WMMoveWidget(tPtr
->vS
, 1 - (rel
?1:0), rh
+ 1 - (rel
?1:0));
1800 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
?2:0));
1802 WMSetRulerOffset(tPtr
->ruler
,22);
1803 } else WMSetRulerOffset(tPtr
->ruler
, 2);
1807 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1808 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1810 WMMoveWidget(tPtr
->hS
, vw
+1, h
- 21);
1811 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1815 tPtr
->visible
.x
= (tPtr
->vS
)?24:4;
1816 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)?43:3;
1817 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1818 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1819 tPtr
->visible
.h
-= (tPtr
->hS
)?20:0;
1820 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1822 if (tPtr
->view
->flags
.realized
) {
1825 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1826 tPtr
->db
= (Pixmap
) NULL
;
1829 if (tPtr
->visible
.w
< 40)
1830 tPtr
->visible
.w
= 40;
1831 if (tPtr
->visible
.h
< 20)
1832 tPtr
->visible
.h
= 20;
1835 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1836 tPtr
->view
->window
, tPtr
->visible
.w
,
1837 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1844 W_ViewDelegate _TextViewDelegate
=
1852 #define TEXT_BUFFER_INCR 8
1853 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1856 clearText(Text
*tPtr
)
1858 tPtr
->vpos
= tPtr
->hpos
= 0;
1859 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1860 tPtr
->cursor
.x
= -23;
1862 if (!tPtr
->firstTextBlock
)
1865 while (tPtr
->currentTextBlock
)
1866 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1868 tPtr
->firstTextBlock
= NULL
;
1869 tPtr
->currentTextBlock
= NULL
;
1870 tPtr
->lastTextBlock
= NULL
;
1871 WMEmptyArray(tPtr
->gfxItems
);
1874 /* possibly remove a single character from the currentTextBlock,
1875 or if there's a selection, remove it...
1876 note that Delete and Backspace are treated differently */
1878 deleteTextInteractively(Text
*tPtr
, KeySym ksym
)
1881 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1882 Bool done
= 1, wasFirst
= 0;
1884 if (!tPtr
->flags
.editable
)
1887 if ( !(tb
= tPtr
->currentTextBlock
) )
1890 if (tPtr
->flags
.ownsSelection
) {
1891 if(removeSelection(tPtr
))
1892 layOutDocument(tPtr
);
1896 wasFirst
= tb
->first
;
1897 if (back
&& tPtr
->tpos
< 1) {
1899 if(tb
->prior
->blank
) {
1900 tPtr
->currentTextBlock
= tb
->prior
;
1901 WMRemoveTextBlock(tPtr
);
1902 tPtr
->currentTextBlock
= tb
;
1904 layOutDocument(tPtr
);
1908 TextBlock
*prior
= tb
->prior
;
1909 tPtr
->currentTextBlock
= tb
;
1910 WMRemoveTextBlock(tPtr
);
1919 tPtr
->tpos
= tb
->used
;
1921 tPtr
->currentTextBlock
= tb
;
1925 tb
->next
->first
= False
;
1926 layOutDocument(tPtr
);
1933 if ( (tb
->used
> 0) && ((back
?tPtr
->tpos
> 0:1))
1934 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1937 memmove(&(tb
->text
[tPtr
->tpos
]),
1938 &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1943 /* if there are no characters left to back over in the textblock,
1944 but it still has characters to the right of the cursor: */
1945 if ( (back
? (tPtr
->tpos
== 0 && !done
) : ( tPtr
->tpos
>= tb
->used
))
1948 /* no more chars, and it's marked as blank? */
1950 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1952 if(tb
->used
== 0 || tb
->graphic
)
1953 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1956 tPtr
->currentTextBlock
= sibling
;
1958 tPtr
->tpos
= (back
? 1 : 0);
1960 tPtr
->tpos
= (back
? sibling
->used
: 0);
1962 /* no more chars, so mark it as blank */
1963 } else if(tb
->used
== 0) {
1965 } else if(tb
->graphic
) {
1966 Bool hasNext
= (Bool
)(tb
->next
);
1968 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1971 } else if(tPtr
->currentTextBlock
) {
1972 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
?
1973 1 : tPtr
->currentTextBlock
->used
);
1975 } else printf("DEBUG: unaccounted for... catch this!\n");
1978 layOutDocument(tPtr
);
1983 insertTextInteractively(Text
*tPtr
, char *text
, int len
)
1986 char *newline
= NULL
;
1988 if (!tPtr
->flags
.editable
) {
1992 if (len
< 1 || !text
)
1996 if(tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
2000 if (tPtr
->flags
.ownsSelection
)
2001 removeSelection(tPtr
);
2004 if (tPtr
->flags
.ignoreNewLine
) {
2006 for(i
=0; i
<len
; i
++) {
2007 if (text
[i
] == '\n')
2012 tb
= tPtr
->currentTextBlock
;
2013 if (!tb
|| tb
->graphic
) {
2015 WMAppendTextStream(tPtr
, text
);
2016 layOutDocument(tPtr
);
2020 if ((newline
= strchr(text
, '\n'))) {
2021 int nlen
= (int)(newline
-text
);
2022 int s
= tb
->used
- tPtr
->tpos
;
2024 if (!tb
->blank
&& nlen
>0) {
2029 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
2030 tb
->used
-= (tb
->used
- tPtr
->tpos
);
2032 insertTextInteractively(tPtr
, text
, nlen
);
2034 WMAppendTextStream(tPtr
, newline
);
2036 insertTextInteractively(tPtr
, save
, s
);
2040 if (tPtr
->tpos
>0 && tPtr
->tpos
< tb
->used
2041 && !tb
->graphic
&& tb
->text
) {
2043 unsigned short savePos
= tPtr
->tpos
;
2044 void *ntb
= WMCreateTextBlockWithText(
2045 tPtr
, &tb
->text
[tPtr
->tpos
],
2046 tb
->d
.font
, tb
->color
, True
, tb
->used
- tPtr
->tpos
);
2048 if(tb
->sections
[0].end
== tPtr
->tpos
)
2049 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2050 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2053 WMAppendTextBlock(tPtr
, ntb
);
2056 } else if (tPtr
->tpos
== tb
->used
) {
2057 if(tPtr
->flags
.indentNewLine
) {
2058 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2059 " ", tb
->d
.font
, tb
->color
, True
, 4));
2062 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2063 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2066 } else if (tPtr
->tpos
== 0) {
2067 if(tPtr
->flags
.indentNewLine
) {
2068 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2069 " ", tb
->d
.font
, tb
->color
, True
, 4));
2071 WMPrependTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2072 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2075 if(tPtr
->currentTextBlock
->next
)
2076 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
2080 if (tb
->used
+ len
>= tb
->allocated
) {
2081 tb
->allocated
= reqBlockSize(tb
->used
+len
);
2082 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
2086 memcpy(tb
->text
, text
, len
);
2089 tb
->text
[tb
->used
] = 0;
2093 memmove(&(tb
->text
[tPtr
->tpos
+len
]), &tb
->text
[tPtr
->tpos
],
2094 tb
->used
-tPtr
->tpos
+1);
2095 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2098 tb
->text
[tb
->used
] = 0;
2103 layOutDocument(tPtr
);
2108 selectRegion(Text
*tPtr
, int x
, int y
)
2114 y
+= (tPtr
->flags
.rulerShown
? 40: 0);
2117 y
-= 10; /* the original offset */
2119 x
-= tPtr
->visible
.x
-2;
2123 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2124 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2125 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2126 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2128 tPtr
->flags
.ownsSelection
= True
;
2134 releaseSelection(Text
*tPtr
)
2136 TextBlock
*tb
= tPtr
->firstTextBlock
;
2139 tb
->selected
= False
;
2142 tPtr
->flags
.ownsSelection
= False
;
2143 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
,
2151 requestHandler(WMView
*view
, Atom selection
, Atom target
, void *cdata
,
2154 Text
*tPtr
= view
->self
;
2155 Display
*dpy
= tPtr
->view
->screen
->display
;
2157 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2158 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2159 WMData
*data
= NULL
;
2162 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2163 char *text
= WMGetTextSelectedStream(tPtr
);
2166 data
= WMCreateDataWithBytes(text
, strlen(text
));
2167 WMSetDataFormat(data
, TYPETEXT
);
2171 } else printf("didn't get it\n");
2173 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2174 if (target
== _TARGETS
) {
2177 ptr
= wmalloc(4 * sizeof(Atom
));
2181 ptr
[3] = COMPOUND_TEXT
;
2183 data
= WMCreateDataWithBytes(ptr
, 4*4);
2184 WMSetDataFormat(data
, 32);
2195 lostHandler(WMView
*view
, Atom selection
, void *cdata
)
2197 releaseSelection((WMText
*)view
->self
);
2201 static WMSelectionProcs selectionHandler
= {
2202 requestHandler
, lostHandler
, NULL
2207 ownershipObserver(void *observerData
, WMNotification
*notification
)
2209 if (observerData
!= WMGetNotificationClientData(notification
))
2210 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2215 autoSelectText(Text
*tPtr
, int clicks
)
2219 char *mark
= NULL
, behind
, ahead
;
2221 if(!(tb
= tPtr
->currentTextBlock
))
2227 switch(tb
->text
[tPtr
->tpos
]) {
2230 case '<': case '>': behind = '<'; ahead = '>'; break;
2231 case '{': case '}': behind = '{'; ahead = '}'; break;
2232 case '[': case ']': behind = '['; ahead = ']'; break;
2234 default: behind
= ahead
= ' ';
2237 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
2238 tPtr
->sel
.h
= 6;/*tPtr->cursor.h-10;*/
2241 tPtr
->sel
.x
= tb
->sections
[0].x
;
2242 tPtr
->sel
.w
= tb
->sections
[0].w
;
2244 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
2247 while(start
> 0 && tb
->text
[start
-1] != behind
)
2251 if(tPtr
->tpos
> start
){
2252 x
-= WMWidthOfString(font
, &tb
->text
[start
],
2253 tPtr
->tpos
- start
);
2255 tPtr
->sel
.x
= (x
<0?0:x
)+1;
2257 if((mark
= strchr(&tb
->text
[start
], ahead
))) {
2258 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2259 (int)(mark
- &tb
->text
[start
]));
2260 } else if(tb
->used
> start
) {
2261 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2266 } else if(clicks
== 3) {
2267 TextBlock
*cur
= tb
;
2269 while(tb
&& !tb
->first
) {
2272 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2275 while(tb
->next
&& !tb
->next
->first
) {
2278 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
2282 tPtr
->sel
.w
= tPtr
->docWidth
;
2283 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2286 if (!tPtr
->flags
.ownsSelection
) {
2287 WMCreateSelectionHandler(tPtr
->view
,
2288 XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2289 tPtr
->flags
.ownsSelection
= True
;
2297 fontChanged(void *observerData
, WMNotification
*notification
)
2299 WMText
*tPtr
= (WMText
*) observerData
;
2300 WMFont
*font
= (WMFont
*)WMGetNotificationClientData(notification
);
2301 printf("fontChanged\n");
2306 if (tPtr
->flags
.ownsSelection
)
2307 WMSetTextSelectionFont(tPtr
, font
);
2312 handleTextKeyPress(Text
*tPtr
, XEvent
*event
)
2316 int control_pressed
= False
;
2317 TextBlock
*tb
= NULL
;
2319 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2320 control_pressed
= True
;
2321 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2326 if((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2328 updateCursorPosition(tPtr
);
2333 if((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2334 if(tPtr
->currentTextBlock
->graphic
)
2337 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2339 updateCursorPosition(tPtr
);
2344 if(!(tb
= tPtr
->currentTextBlock
))
2352 tPtr
->currentTextBlock
= tb
->prior
;
2353 if(tPtr
->currentTextBlock
->graphic
)
2356 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2358 if(!tb
->first
&& tPtr
->tpos
> 0)
2360 } else tPtr
->tpos
= 0;
2361 } else tPtr
->tpos
--;
2362 updateCursorPosition(tPtr
);
2367 if(!(tb
= tPtr
->currentTextBlock
))
2371 if(tPtr
->tpos
== tb
->used
) {
2374 tPtr
->currentTextBlock
= tb
->next
;
2376 if(!tb
->next
->first
&& tb
->next
->used
>0)
2382 tPtr
->tpos
= tb
->used
;
2384 } else tPtr
->tpos
++;
2385 updateCursorPosition(tPtr
);
2390 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2391 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2396 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2397 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2406 deleteTextInteractively(tPtr
, ksym
);
2407 updateCursorPosition(tPtr
);
2413 control_pressed
= True
;
2417 insertTextInteractively(tPtr
, " ", 4);
2418 updateCursorPosition(tPtr
);
2425 if (*buffer
!= 0 && !control_pressed
) {
2426 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2427 updateCursorPosition(tPtr
);
2430 } else if (control_pressed
&& ksym
==XK_r
) {
2431 Bool i
= !tPtr
->flags
.rulerShown
;
2432 WMShowTextRuler(tPtr
, i
);
2433 tPtr
->flags
.rulerShown
= i
;
2435 else if (control_pressed
&& *buffer
== '\a')
2436 XBell(tPtr
->view
->screen
->display
, 0);
2438 WMRelayToNextResponder(tPtr
->view
, event
);
2441 if (!control_pressed
&& tPtr
->flags
.ownsSelection
)
2442 releaseSelection(tPtr
);
2447 pasteText(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2448 void *cdata
, WMData
*data
)
2450 Text
*tPtr
= (Text
*)view
->self
;
2453 tPtr
->flags
.waitingForSelection
= 0;
2456 text
= (char*)WMDataBytes(data
);
2459 (tPtr
->parser
) (tPtr
, (void *) text
);
2460 layOutDocument(tPtr
);
2461 } else insertTextInteractively(tPtr
, text
, strlen(text
));
2462 updateCursorPosition(tPtr
);
2468 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2473 (tPtr
->parser
) (tPtr
, (void *) text
);
2474 layOutDocument(tPtr
);
2475 } else insertTextInteractively(tPtr
, text
, n
);
2476 updateCursorPosition(tPtr
);
2488 handleActionEvents(XEvent
*event
, void *data
)
2490 Text
*tPtr
= (Text
*)data
;
2491 Display
*dpy
= event
->xany
.display
;
2495 switch (event
->type
) {
2497 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2498 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2499 tPtr
->flags
.extendSelection
= True
;
2503 if (tPtr
->flags
.focused
) {
2504 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2505 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
2506 GrabModeAsync
, GrabModeAsync
, None
,
2507 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2508 tPtr
->flags
.pointerGrabbed
= True
;
2509 handleTextKeyPress(tPtr
, event
);
2514 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2515 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2516 tPtr
->flags
.extendSelection
= False
;
2518 /* end modify flag so selection can be extended */
2525 if (tPtr
->flags
.pointerGrabbed
) {
2526 tPtr
->flags
.pointerGrabbed
= False
;
2527 XUngrabPointer(dpy
, CurrentTime
);
2530 if(tPtr
->flags
.waitingForSelection
)
2533 if ((event
->xmotion
.state
& Button1Mask
)) {
2534 TextBlock
*tb
= tPtr
->currentTextBlock
;
2536 if(tb
&& tPtr
->flags
.isOverGraphic
&&
2537 tb
->graphic
&& !tb
->object
) {
2539 WMPixmap
*pixmap
= tb
->d
.pixmap
;
2540 char *types
[2] = {"application/X-image", NULL
};
2545 WMDragImageFromView(tPtr
->view
, pixmap
, types
,
2546 wmkpoint(event
->xmotion
.x_root
, event
->xmotion
.y_root
),
2551 if (!tPtr
->flags
.ownsSelection
) {
2552 WMCreateSelectionHandler(tPtr
->view
,
2553 XA_PRIMARY
, event
->xbutton
.time
,
2554 &selectionHandler
, NULL
);
2555 tPtr
->flags
.ownsSelection
= True
;
2558 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2562 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2568 if (tPtr
->flags
.pointerGrabbed
) {
2569 tPtr
->flags
.pointerGrabbed
= False
;
2570 XUngrabPointer(dpy
, CurrentTime
);
2574 if (tPtr
->flags
.waitingForSelection
)
2577 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2578 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2582 if (tPtr
->flags
.ownsSelection
)
2583 releaseSelection(tPtr
);
2586 if (event
->xbutton
.button
== Button1
) {
2588 if(WMIsDoubleClick(event
)) {
2589 TextBlock
*tb
= tPtr
->currentTextBlock
;
2591 tPtr
->lastClickTime
= event
->xbutton
.time
;
2592 if(tb
&& tb
->graphic
&& !tb
->object
) {
2593 if(tPtr
->delegate
&& tPtr
->delegate
->didDoubleClickOnPicture
) {
2596 desc
= wmalloc(tb
->used
+1);
2597 memcpy(desc
, tb
->text
, tb
->used
);
2599 (*tPtr
->delegate
->didDoubleClickOnPicture
)(tPtr
->delegate
, desc
);
2603 autoSelectText(tPtr
, 2);
2606 } else if(event
->xbutton
.time
- tPtr
->lastClickTime
2607 < WINGsConfiguration
.doubleClickDelay
) {
2608 tPtr
->lastClickTime
= event
->xbutton
.time
;
2609 autoSelectText(tPtr
, 3);
2613 if (!tPtr
->flags
.focused
) {
2614 WMSetFocusToWidget(tPtr
);
2615 tPtr
->flags
.focused
= True
;
2618 tPtr
->lastClickTime
= event
->xbutton
.time
;
2619 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2623 if (event
->xbutton
.button
2624 == WINGsConfiguration
.mouseWheelDown
) {
2625 WMScrollText(tPtr
, 16);
2629 if (event
->xbutton
.button
2630 == WINGsConfiguration
.mouseWheelUp
) {
2631 WMScrollText(tPtr
, -16);
2635 if (event
->xbutton
.button
== Button2
) {
2639 if (!tPtr
->flags
.editable
) {
2644 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2645 event
->xbutton
.time
, pasteText
, NULL
)) {
2647 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2648 tPtr
->flags
.waitingForSelection
= 0;
2654 (tPtr
->parser
) (tPtr
, (void *) text
);
2655 layOutDocument(tPtr
);
2658 insertTextInteractively(tPtr
, text
, n
);
2662 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2663 (void*)WMInsertTextEvent
);
2665 updateCursorPosition(tPtr
);
2669 tPtr
->flags
.waitingForSelection
= True
;
2677 if (tPtr
->flags
.pointerGrabbed
) {
2678 tPtr
->flags
.pointerGrabbed
= False
;
2679 XUngrabPointer(dpy
, CurrentTime
);
2683 if (tPtr
->flags
.waitingForSelection
)
2691 handleEvents(XEvent
*event
, void *data
)
2693 Text
*tPtr
= (Text
*)data
;
2695 switch(event
->type
) {
2698 if (event
->xexpose
.count
!=0)
2702 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2703 WMRealizeWidget(tPtr
->hS
);
2707 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2708 WMRealizeWidget(tPtr
->vS
);
2712 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2713 WMRealizeWidget(tPtr
->ruler
);
2718 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2724 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2727 tPtr
->flags
.focused
= True
;
2729 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2730 tPtr
->timerID
= WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY
,
2738 tPtr
->flags
.focused
= False
;
2741 if (tPtr
->timerID
) {
2742 WMDeleteTimerHandler(tPtr
->timerID
);
2743 tPtr
->timerID
= NULL
;
2752 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2754 WMEmptyArray(tPtr
->gfxItems
);
2757 WMDeleteTimerHandler(tPtr
->timerID
);
2759 WMReleaseFont(tPtr
->dFont
);
2760 WMReleaseColor(tPtr
->dColor
);
2761 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2762 WMRemoveNotificationObserver(tPtr
);
2773 insertPlainText(Text
*tPtr
, char *text
)
2780 mark
= strchr(start
, '\n');
2782 tb
= WMCreateTextBlockWithText(tPtr
,
2784 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
-start
));
2786 tPtr
->flags
.first
= True
;
2788 if (start
&& strlen(start
)) {
2789 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2790 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2792 tPtr
->flags
.first
= False
;
2796 if (tPtr
->flags
.prepend
)
2797 WMPrependTextBlock(tPtr
, tb
);
2799 WMAppendTextBlock(tPtr
, tb
);
2805 rulerMoveCallBack(WMWidget
*w
, void *self
)
2807 Text
*tPtr
= (Text
*)self
;
2811 if (W_CLASS(tPtr
) != WC_Text
)
2819 rulerReleaseCallBack(WMWidget
*w
, void *self
)
2821 Text
*tPtr
= (Text
*)self
;
2825 if (W_CLASS(tPtr
) != WC_Text
)
2833 draggingSourceOperation(WMView
*self
, Bool local
)
2835 return WDOperationCopy
;
2839 fetchDragData(WMView
*self
, char *type
)
2841 TextBlock
*tb
= ((WMText
*)self
->self
)->currentTextBlock
;
2848 printf("type is [%s]\n", type
);
2849 desc
= wmalloc(tb
->used
+1);
2850 memcpy(desc
, tb
->text
, tb
->used
);
2852 data
= WMCreateDataWithBytes(desc
, strlen(desc
)+1);
2860 static WMDragSourceProcs _DragSourceProcs
= {
2861 draggingSourceOperation
,
2869 draggingEntered(WMView
*self
, WMDraggingInfo
*info
)
2871 printf("draggingEntered\n");
2872 return WDOperationCopy
;
2877 draggingUpdated(WMView
*self
, WMDraggingInfo
*info
)
2879 return WDOperationCopy
;
2884 draggingExited(WMView
*self
, WMDraggingInfo
*info
)
2886 printf("draggingExited\n");
2890 prepareForDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2892 printf("prepareForDragOperation\n");
2900 receivedData(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2901 void *cdata
, WMData
*data
)
2903 badbadbad
= wstrdup((char *)WMDataBytes(data
));
2907 /* when it's done in WINGs, remove this */
2910 requestDroppedData(WMView
*view
, WMDraggingInfo
*info
, char *type
)
2912 WMScreen
*scr
= W_VIEW_SCREEN(view
);
2914 if (!WMRequestSelection(scr
->dragInfo
.destView
,
2915 scr
->xdndSelectionAtom
,
2916 XInternAtom(scr
->display
, type
, False
),
2917 scr
->dragInfo
.timestamp
,
2918 receivedData
, &scr
->dragInfo
)) {
2919 wwarning("could not request data for dropped data");
2925 ev
.type
= ClientMessage
;
2926 ev
.xclient
.message_type
= scr
->xdndFinishedAtom
;
2927 ev
.xclient
.format
= 32;
2928 ev
.xclient
.window
= info
->destinationWindow
;
2929 ev
.xclient
.data
.l
[0] = 0;
2930 ev
.xclient
.data
.l
[1] = 0;
2931 ev
.xclient
.data
.l
[2] = 0;
2932 ev
.xclient
.data
.l
[3] = 0;
2933 ev
.xclient
.data
.l
[4] = 0;
2935 XSendEvent(scr
->display
, info
->sourceWindow
, False
, 0, &ev
);
2936 XFlush(scr
->display
);
2942 performDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2945 WMText
*tPtr
= (WMText
*)self
->self
;
2950 requestDroppedData(tPtr
->view
, info
, "application/X-color");
2951 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), badbadbad
, True
);
2953 WMSetTextSelectionColor(tPtr
, color
);
2954 WMReleaseColor(color
);
2963 concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2965 printf("concludeDragOperation\n");
2969 static WMDragDestinationProcs _DragDestinationProcs
= {
2973 prepareForDragOperation
,
2974 performDragOperation
,
2975 concludeDragOperation
2980 getStream(WMText
*tPtr
, int sel
, int array
)
2982 TextBlock
*tb
= NULL
;
2984 unsigned long where
= 0;
2989 if (!(tb
= tPtr
->firstTextBlock
))
2993 (tPtr
->writer
) (tPtr
, (void *) text
);
2997 tb
= tPtr
->firstTextBlock
;
3000 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
3002 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
3004 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
3005 && tb
!= tPtr
->firstTextBlock
) {
3006 text
= wrealloc(text
, where
+1);
3007 text
[where
++] = '\n';
3013 if(tb
->graphic
&& array
) {
3014 text
= wrealloc(text
, where
+4);
3015 text
[where
++] = 0xFA;
3016 text
[where
++] = (tb
->used
>>8)&0x0ff;
3017 text
[where
++] = tb
->used
&0x0ff;
3018 text
[where
++] = tb
->allocated
; /* extra info */
3020 text
= wrealloc(text
, where
+tb
->used
);
3021 memcpy(&text
[where
], tb
->text
, tb
->used
);
3025 } else if (sel
&& tb
->selected
) {
3027 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
3028 text
= wrealloc(text
, where
+1);
3029 text
[where
++] = '\n';
3035 text
= wrealloc(text
, where
+(tb
->s_end
- tb
->s_begin
));
3036 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
],
3037 tb
->s_end
- tb
->s_begin
);
3038 where
+= tb
->s_end
- tb
->s_begin
;
3043 _gSnext
:tb
= tb
->next
;
3046 /* +1 for the end of string, let's be nice */
3047 text
= wrealloc(text
, where
+1);
3054 releaseStreamObjects(void *data
)
3061 getStreamObjects(WMText
*tPtr
, int sel
)
3063 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
3067 char *start
, *fa
, *desc
;
3069 stream
= getStream(tPtr
, sel
, 1);
3076 fa
= strchr(start
, 0xFA);
3078 if((int)(fa
- start
)>0) {
3080 desc
[(int)(fa
- start
)] = 0;
3081 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
3082 WMSetDataFormat(data
, TYPETEXT
);
3083 WMAddToArray(array
, (void *) data
);
3086 len
= *(fa
+1)*0xff + *(fa
+2);
3087 data
= WMCreateDataWithBytes((void *)(fa
+4), len
);
3088 WMSetDataFormat(data
, *(fa
+3));
3089 WMAddToArray(array
, (void *) data
);
3090 start
= fa
+ len
+ 4;
3093 if (start
&& strlen(start
)) {
3094 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
3095 WMSetDataFormat(data
, TYPETEXT
);
3096 WMAddToArray(array
, (void *) data
);
3108 WMCreateTextForDocumentType(WMWidget
*parent
, WMAction
*parser
, WMAction
*writer
)
3115 tPtr
= wmalloc(sizeof(Text
));
3116 memset(tPtr
, 0, sizeof(Text
));
3117 tPtr
->widgetClass
= WC_Text
;
3118 tPtr
->view
= W_CreateView(W_VIEW(parent
));
3120 perror("could not create text's view\n");
3125 dpy
= tPtr
->view
->screen
->display
;
3126 scr
= tPtr
->view
->screen
;
3128 tPtr
->view
->self
= tPtr
;
3129 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
3130 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3131 W_ResizeView(tPtr
->view
, 250, 200);
3133 tPtr
->dColor
= WMBlackColor(scr
);
3134 tPtr
->fgColor
= WMBlackColor(scr
);
3135 tPtr
->bgColor
= WMWhiteColor(scr
);
3136 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3138 gcv
.graphics_exposures
= False
;
3139 gcv
.foreground
= W_PIXEL(scr
->gray
);
3140 gcv
.background
= W_PIXEL(scr
->darkGray
);
3141 gcv
.fill_style
= FillStippled
;
3142 /* why not use scr->stipple here? */
3143 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
), STIPPLE_BITS
,
3144 STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
3145 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
3146 GCForeground
|GCBackground
|GCStipple
3147 |GCFillStyle
|GCGraphicsExposures
, &gcv
);
3153 tPtr
->dFont
= WMSystemFontOfSize(scr
, 12);
3155 tPtr
->view
->delegate
= &_TextViewDelegate
;
3157 tPtr
->delegate
= NULL
;
3160 tPtr
->timerID
= NULL
;
3163 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
3164 |EnterWindowMask
|LeaveWindowMask
|FocusChangeMask
,
3165 handleEvents
, tPtr
);
3167 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
|ButtonPressMask
3168 |KeyReleaseMask
|KeyPressMask
|Button1MotionMask
,
3169 handleActionEvents
, tPtr
);
3171 WMAddNotificationObserver(ownershipObserver
, tPtr
,
3172 WMSelectionOwnerDidChangeNotification
,
3175 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3176 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3180 char *types
[3] = {"application/X-color", "application/X-image", NULL
};
3181 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3184 /*WMAddNotificationObserver(fontChanged, tPtr,
3185 WMFontPanelDidChangeNotification, tPtr);*/
3187 tPtr
->firstTextBlock
= NULL
;
3188 tPtr
->lastTextBlock
= NULL
;
3189 tPtr
->currentTextBlock
= NULL
;
3192 tPtr
->gfxItems
= WMCreateArray(4);
3194 tPtr
->parser
= parser
;
3195 tPtr
->writer
= writer
;
3197 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3198 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3200 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3202 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3203 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3204 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3206 tPtr
->cursor
.x
= -23;
3209 tPtr
->docHeight
= 0;
3210 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
,
3212 tPtr
->db
= (Pixmap
) NULL
;
3213 tPtr
->bgPixmap
= NULL
;
3215 tPtr
->margins
= WMGetRulerMargins(NULL
);
3216 tPtr
->margins
->right
= tPtr
->visible
.w
;
3219 tPtr
->flags
.rulerShown
= False
;
3220 tPtr
->flags
.monoFont
= False
;
3221 tPtr
->flags
.focused
= False
;
3222 tPtr
->flags
.editable
= True
;
3223 tPtr
->flags
.ownsSelection
= False
;
3224 tPtr
->flags
.pointerGrabbed
= False
;
3225 tPtr
->flags
.extendSelection
= False
;
3226 tPtr
->flags
.frozen
= False
;
3227 tPtr
->flags
.cursorShown
= True
;
3228 tPtr
->flags
.acceptsGraphic
= False
;
3229 tPtr
->flags
.horizOnDemand
= False
;
3230 tPtr
->flags
.needsLayOut
= False
;
3231 tPtr
->flags
.ignoreNewLine
= False
;
3232 tPtr
->flags
.indentNewLine
= False
;
3233 tPtr
->flags
.laidOut
= False
;
3234 tPtr
->flags
.ownsSelection
= False
;
3235 tPtr
->flags
.waitingForSelection
= False
;
3236 tPtr
->flags
.prepend
= False
;
3237 tPtr
->flags
.isOverGraphic
= False
;
3238 tPtr
->flags
.relief
= WRSunken
;
3239 tPtr
->flags
.isOverGraphic
= 0;
3240 tPtr
->flags
.alignment
= WALeft
;
3241 tPtr
->flags
.first
= True
;
3247 WMPrependTextStream(WMText
*tPtr
, char *text
)
3249 CHECK_CLASS(tPtr
, WC_Text
);
3252 if (tPtr
->flags
.ownsSelection
)
3253 releaseSelection(tPtr
);
3255 updateScrollers(tPtr
);
3259 tPtr
->flags
.prepend
= True
;
3260 if (text
&& tPtr
->parser
)
3261 (tPtr
->parser
) (tPtr
, (void *) text
);
3263 insertPlainText(tPtr
, text
);
3265 tPtr
->flags
.needsLayOut
= True
;
3267 if(!tPtr
->flags
.frozen
) {
3268 layOutDocument(tPtr
);
3274 WMAppendTextStream(WMText
*tPtr
, char *text
)
3276 CHECK_CLASS(tPtr
, WC_Text
);
3279 if (tPtr
->flags
.ownsSelection
)
3280 releaseSelection(tPtr
);
3282 updateScrollers(tPtr
);
3286 tPtr
->flags
.prepend
= False
;
3287 if (text
&& tPtr
->parser
)
3288 (tPtr
->parser
) (tPtr
, (void *) text
);
3290 insertPlainText(tPtr
, text
);
3292 tPtr
->flags
.needsLayOut
= True
;
3293 if(tPtr
->currentTextBlock
) {
3294 if(tPtr
->currentTextBlock
->graphic
)
3297 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3300 if(!tPtr
->flags
.frozen
) {
3301 layOutDocument(tPtr
);
3307 WMGetTextStream(WMText
*tPtr
)
3309 CHECK_CLASS(tPtr
, WC_Text
);
3310 return getStream(tPtr
, 0, 0);
3314 WMGetTextSelectedStream(WMText
*tPtr
)
3316 CHECK_CLASS(tPtr
, WC_Text
);
3317 return getStream(tPtr
, 1, 0);
3321 WMGetTextObjects(WMText
*tPtr
)
3323 CHECK_CLASS(tPtr
, WC_Text
);
3324 return getStreamObjects(tPtr
, 0);
3328 WMGetTextSelectedObjects(WMText
*tPtr
)
3330 CHECK_CLASS(tPtr
, WC_Text
);
3331 return getStreamObjects(tPtr
, 1);
3336 WMSetTextDelegate(WMText
*tPtr
, WMTextDelegate
*delegate
)
3338 CHECK_CLASS(tPtr
, WC_Text
);
3340 tPtr
->delegate
= delegate
;
3345 WMCreateTextBlockWithObject(WMText
*tPtr
, WMWidget
*w
,
3346 char *description
, WMColor
*color
,
3347 unsigned short first
, unsigned short extraInfo
)
3351 if (!w
|| !description
|| !color
)
3354 tb
= wmalloc(sizeof(TextBlock
));
3358 tb
->text
= wstrdup(description
);
3359 tb
->used
= strlen(description
);
3362 tb
->color
= WMRetainColor(color
);
3363 tb
->marginN
= newMargin(tPtr
, NULL
);
3364 tb
->allocated
= extraInfo
;
3369 tb
->underlined
= False
;
3370 tb
->selected
= False
;
3372 tb
->sections
= NULL
;
3382 WMCreateTextBlockWithPixmap(WMText
*tPtr
, WMPixmap
*p
,
3383 char *description
, WMColor
*color
,
3384 unsigned short first
, unsigned short extraInfo
)
3388 if (!p
|| !description
|| !color
)
3391 tb
= wmalloc(sizeof(TextBlock
));
3395 tb
->text
= wstrdup(description
);
3396 tb
->used
= strlen(description
);
3398 tb
->d
.pixmap
= WMRetainPixmap(p
);
3399 tb
->color
= WMRetainColor(color
);
3400 tb
->marginN
= newMargin(tPtr
, NULL
);
3401 tb
->allocated
= extraInfo
;
3406 tb
->underlined
= False
;
3407 tb
->selected
= False
;
3409 tb
->sections
= NULL
;
3419 WMCreateTextBlockWithText(WMText
*tPtr
, char *text
, WMFont
*font
, WMColor
*color
,
3420 unsigned short first
, unsigned short len
)
3424 if (!font
|| !color
)
3427 tb
= wmalloc(sizeof(TextBlock
));
3431 tb
->allocated
= reqBlockSize(len
);
3432 tb
->text
= (char *)wmalloc(tb
->allocated
);
3433 memset(tb
->text
, 0, tb
->allocated
);
3435 if (len
< 1|| !text
|| (*text
== '\n' && len
==1 )) {
3440 memcpy(tb
->text
, text
, len
);
3444 tb
->text
[tb
->used
] = 0;
3446 tb
->d
.font
= WMRetainFont(font
);
3447 tb
->color
= WMRetainColor(color
);
3448 tb
->marginN
= newMargin(tPtr
, NULL
);
3451 tb
->graphic
= False
;
3452 tb
->underlined
= False
;
3453 tb
->selected
= False
;
3455 tb
->sections
= NULL
;
3463 WMSetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int first
,
3464 unsigned int kanji
, unsigned int underlined
, int script
,
3465 WMRulerMargins
*margins
)
3467 TextBlock
*tb
= (TextBlock
*) vtb
;
3473 tb
->underlined
= underlined
;
3474 tb
->script
= script
;
3475 tb
->marginN
= newMargin(tPtr
, margins
);
3479 WMGetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int *first
,
3480 unsigned int *kanji
, unsigned int *underlined
, int *script
,
3481 WMRulerMargins
*margins
)
3483 TextBlock
*tb
= (TextBlock
*) vtb
;
3487 if (first
) *first
= tb
->first
;
3488 if (kanji
) *kanji
= tb
->kanji
;
3489 if (underlined
) *underlined
= tb
->underlined
;
3490 if (script
) *script
= tb
->script
;
3491 if (margins
) margins
= &tPtr
->margins
[tb
->marginN
];
3497 WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3499 TextBlock
*tb
= (TextBlock
*)vtb
;
3506 WMWidget
*w
= tb
->d
.widget
;
3507 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3508 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3509 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3512 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3516 tPtr
->tpos
= tb
->used
;
3519 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3520 tb
->next
= tb
->prior
= NULL
;
3522 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3523 = tPtr
->currentTextBlock
= tb
;
3528 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3531 tb
->next
= tPtr
->currentTextBlock
;
3532 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3533 if (tPtr
->currentTextBlock
->prior
)
3534 tPtr
->currentTextBlock
->prior
->next
= tb
;
3536 tPtr
->currentTextBlock
->prior
= tb
;
3538 tPtr
->firstTextBlock
= tb
;
3540 tPtr
->currentTextBlock
= tb
;
3545 WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3547 TextBlock
*tb
= (TextBlock
*)vtb
;
3554 WMWidget
*w
= tb
->d
.widget
;
3555 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3556 (W_VIEW(w
))->attribs
.cursor
=
3557 tPtr
->view
->screen
->defaultCursor
;
3558 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3561 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3565 tPtr
->tpos
= tb
->used
;
3569 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3570 tb
->next
= tb
->prior
= NULL
;
3572 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3573 = tPtr
->currentTextBlock
= tb
;
3578 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3581 tb
->next
= tPtr
->currentTextBlock
->next
;
3582 tb
->prior
= tPtr
->currentTextBlock
;
3583 if (tPtr
->currentTextBlock
->next
)
3584 tPtr
->currentTextBlock
->next
->prior
= tb
;
3586 tPtr
->currentTextBlock
->next
= tb
;
3589 tPtr
->lastTextBlock
= tb
;
3591 tPtr
->currentTextBlock
= tb
;
3596 WMRemoveTextBlock(WMText
*tPtr
)
3598 TextBlock
*tb
= NULL
;
3600 if (!tPtr
|| !tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
3601 || !tPtr
->currentTextBlock
) {
3605 tb
= tPtr
->currentTextBlock
;
3607 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3610 WMUnmapWidget(tb
->d
.widget
);
3614 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3615 if (tPtr
->currentTextBlock
->next
)
3616 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3618 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3619 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3621 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3622 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3623 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3624 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3626 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3627 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3628 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3637 destroyWidget(WMWidget
*widget
)
3639 WMDestroyWidget(widget
);
3640 // -- never do this -- wfree(widget);
3646 WMDestroyTextBlock(WMText
*tPtr
, void *vtb
)
3648 TextBlock
*tb
= (TextBlock
*)vtb
;
3654 /* naturally, there's a danger to destroying widgets whose action
3655 * brings us here: ie. press a button to destroy it...
3656 * need to find a safer way. till then... this stays commented out */
3657 /* 5 months later... destroy it 10 seconds after now which should
3658 * be enough time for the widget's action to be completed... :-) */
3659 /* This is a bad assumption. Just destroy the widget here.
3660 * if the caller needs it, it can protect it with W_RetainView()
3661 * WMAddTimerHandler(10000, destroyWidget, (void *)tb->d.widget);*/
3662 WMDestroyWidget(tb
->d
.widget
);
3664 WMReleasePixmap(tb
->d
.pixmap
);
3667 WMReleaseFont(tb
->d
.font
);
3670 WMReleaseColor(tb
->color
);
3671 /* isn't this going to memleak if nsections==0? if (tb->sections && tb->nsections > 0) */
3673 wfree(tb
->sections
);
3680 WMSetTextForegroundColor(WMText
*tPtr
, WMColor
*color
)
3685 WMReleaseColor(tPtr
->fgColor
);
3686 tPtr
->fgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->black
);
3692 WMSetTextBackgroundColor(WMText
*tPtr
, WMColor
*color
)
3697 WMReleaseColor(tPtr
->bgColor
);
3698 tPtr
->bgColor
= WMRetainColor(color
? color
: tPtr
->view
->screen
->white
);
3699 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->bgColor
);
3704 void WMSetTextBackgroundPixmap(WMText
*tPtr
, WMPixmap
*pixmap
)
3710 WMReleasePixmap(tPtr
->bgPixmap
);
3713 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3715 tPtr
->bgPixmap
= NULL
;
3719 WMSetTextRelief(WMText
*tPtr
, WMReliefType relief
)
3723 tPtr
->flags
.relief
= relief
;
3724 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3728 WMSetTextHasHorizontalScroller(WMText
*tPtr
, Bool shouldhave
)
3733 if (shouldhave
&& !tPtr
->hS
) {
3734 tPtr
->hS
= WMCreateScroller(tPtr
);
3735 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3736 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3737 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3738 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3739 WMMapWidget(tPtr
->hS
);
3740 } else if (!shouldhave
&& tPtr
->hS
) {
3741 WMUnmapWidget(tPtr
->hS
);
3742 WMDestroyWidget(tPtr
->hS
);
3748 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3753 WMSetTextHasRuler(WMText
*tPtr
, Bool shouldhave
)
3758 if(shouldhave
&& !tPtr
->ruler
) {
3759 tPtr
->ruler
= WMCreateRuler(tPtr
);
3760 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
=
3761 tPtr
->view
->screen
->defaultCursor
;
3762 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3763 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3764 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3765 } else if(!shouldhave
&& tPtr
->ruler
) {
3766 WMShowTextRuler(tPtr
, False
);
3767 WMDestroyWidget(tPtr
->ruler
);
3770 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3774 WMShowTextRuler(WMText
*tPtr
, Bool show
)
3781 if(tPtr
->flags
.monoFont
)
3784 tPtr
->flags
.rulerShown
= show
;
3786 WMMapWidget(tPtr
->ruler
);
3788 WMUnmapWidget(tPtr
->ruler
);
3791 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3795 WMGetTextRulerShown(WMText
*tPtr
)
3803 return tPtr
->flags
.rulerShown
;
3808 WMSetTextHasVerticalScroller(WMText
*tPtr
, Bool shouldhave
)
3813 if (shouldhave
&& !tPtr
->vS
) {
3814 tPtr
->vS
= WMCreateScroller(tPtr
);
3815 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3816 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3817 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3818 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3819 WMMapWidget(tPtr
->vS
);
3820 } else if (!shouldhave
&& tPtr
->vS
) {
3821 WMUnmapWidget(tPtr
->vS
);
3822 WMDestroyWidget(tPtr
->vS
);
3828 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3834 WMScrollText(WMText
*tPtr
, int amount
)
3839 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3843 if (tPtr
->vpos
> 0) {
3844 if (tPtr
->vpos
> abs(amount
)) tPtr
->vpos
+= amount
;
3849 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3850 if (tPtr
->vpos
< limit
) {
3851 if (tPtr
->vpos
< limit
-amount
) tPtr
->vpos
+= amount
;
3852 else tPtr
->vpos
= limit
;
3857 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3858 updateScrollers(tPtr
);
3861 tPtr
->prevVpos
= tPtr
->vpos
;
3866 WMPageText(WMText
*tPtr
, Bool direction
)
3868 if (!tPtr
) return False
;
3869 if (!tPtr
->view
->flags
.realized
) return False
;
3871 return WMScrollText(tPtr
, direction
?tPtr
->visible
.h
:-tPtr
->visible
.h
);
3875 WMSetTextEditable(WMText
*tPtr
, Bool editable
)
3879 tPtr
->flags
.editable
= editable
;
3883 WMGetTextEditable(WMText
*tPtr
)
3887 return tPtr
->flags
.editable
;
3891 WMSetTextIndentNewLines(WMText
*tPtr
, Bool indent
)
3895 tPtr
->flags
.indentNewLine
= indent
;
3899 WMSetTextIgnoresNewline(WMText
*tPtr
, Bool ignore
)
3903 tPtr
->flags
.ignoreNewLine
= ignore
;
3907 WMGetTextIgnoresNewline(WMText
*tPtr
)
3911 return tPtr
->flags
.ignoreNewLine
;
3915 WMSetTextUsesMonoFont(WMText
*tPtr
, Bool mono
)
3921 if(tPtr
->flags
.rulerShown
)
3922 WMShowTextRuler(tPtr
, False
);
3923 if(tPtr
->flags
.alignment
!= WALeft
)
3924 tPtr
->flags
.alignment
= WALeft
;
3927 tPtr
->flags
.monoFont
= mono
;
3928 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3932 WMGetTextUsesMonoFont(WMText
*tPtr
)
3936 return tPtr
->flags
.monoFont
;
3941 WMSetTextDefaultFont(WMText
*tPtr
, WMFont
*font
)
3946 WMReleaseFont(tPtr
->dFont
);
3948 tPtr
->dFont
= WMRetainFont(font
);
3950 tPtr
->dFont
= WMRetainFont(WMSystemFontOfSize(tPtr
->view
->screen
, 12));
3954 WMGetTextDefaultFont(WMText
*tPtr
)
3959 return WMRetainFont(tPtr
->dFont
);
3963 WMSetTextDefaultColor(WMText
*tPtr
, WMColor
*color
)
3968 WMReleaseColor(tPtr
->dColor
);
3970 tPtr
->dColor
= WMRetainColor(color
);
3972 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3976 WMGetTextDefaultColor(WMText
*tPtr
)
3981 return WMRetainColor(tPtr
->dColor
);
3985 WMSetTextAlignment(WMText
*tPtr
, WMAlignment alignment
)
3989 if(tPtr
->flags
.monoFont
)
3990 tPtr
->flags
.alignment
= WALeft
;
3992 tPtr
->flags
.alignment
= alignment
;
3997 WMGetTextInsertType(WMText
*tPtr
)
4001 return tPtr
->flags
.prepend
;
4006 WMSetTextSelectionColor(WMText
*tPtr
, WMColor
*color
)
4008 if (!tPtr
|| !color
)
4011 setSelectionProperty(tPtr
, NULL
, color
, -1);
4015 WMGetTextSelectionColor(WMText
*tPtr
)
4022 tb
= tPtr
->currentTextBlock
;
4024 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4035 WMSetTextSelectionFont(WMText
*tPtr
, WMFont
*font
)
4040 setSelectionProperty(tPtr
, font
, NULL
, -1) ;
4044 WMGetTextSelectionFont(WMText
*tPtr
)
4051 tb
= tPtr
->currentTextBlock
;
4053 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4060 tb
= getFirstNonGraphicBlockFor(tb
, 1);
4064 return (tb
->selected
? tb
->d
.font
: NULL
);
4069 WMSetTextSelectionUnderlined(WMText
*tPtr
, int underlined
)
4071 if (!tPtr
|| (underlined
!=0 && underlined
!=1))
4074 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
4079 WMGetTextSelectionUnderlined(WMText
*tPtr
)
4086 tb
= tPtr
->currentTextBlock
;
4088 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4094 return tb
->underlined
;
4099 WMFreezeText(WMText
*tPtr
)
4104 tPtr
->flags
.frozen
= True
;
4109 WMThawText(WMText
*tPtr
)
4114 tPtr
->flags
.frozen
= False
;
4116 if(tPtr
->flags
.monoFont
) {
4117 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
4120 /* make sure to unmap widgets no matter where they are */
4121 /* they'll be later remapped if needed by paintText */
4122 for(j
=0; j
<c
; j
++) {
4123 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
4124 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
4125 WMUnmapWidget(tb
->d
.widget
);
4131 tPtr
->flags
.laidOut
= False
;
4132 layOutDocument(tPtr
);
4133 updateScrollers(tPtr
);
4135 tPtr
->flags
.needsLayOut
= False
;
4139 /* find first occurence of a string */
4141 mystrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4146 if(!haystack
|| !needle
|| !end
)
4149 for (ptr
= haystack
; ptr
< end
; ptr
++) {
4151 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4155 if (tolower(*ptr
) == tolower(*needle
) &&
4156 !strncasecmp(ptr
, needle
, len
))
4164 /* find last occurence of a string */
4166 mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4171 if(!haystack
|| !needle
|| !end
)
4174 for (ptr
= haystack
-2; ptr
> end
; ptr
--) {
4176 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4179 if (tolower(*ptr
) == tolower(*needle
) &&
4180 !strncasecmp(ptr
, needle
, len
))
4190 WMFindInTextStream(WMText
*tPtr
, char *needle
, Bool direction
,
4197 if (!tPtr
|| !needle
)
4201 if (! (tb
= tPtr
->currentTextBlock
)) {
4202 if (! (tb
= ( (direction
> 0) ?
4203 tPtr
->firstTextBlock
: tPtr
->lastTextBlock
) ) ){
4207 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4208 tb = (direction>0) ? tb->next : tb->prior; */
4209 if(tb
!= tPtr
->lastTextBlock
)
4213 tb
= tPtr
->currentTextBlock
;
4221 if(pos
+1 < tb
->used
)
4224 if(tb
->used
- pos
> 0 && pos
> 0) {
4225 mark
= mystrstr(&tb
->text
[pos
], needle
,
4226 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
4239 mark
= mystrrstr(&tb
->text
[pos
], needle
,
4240 strlen(needle
), tb
->text
, caseSensitive
);
4252 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
4254 tPtr
->tpos
= (int)(mark
- tb
->text
);
4255 tPtr
->currentTextBlock
= tb
;
4256 updateCursorPosition(tPtr
);
4257 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
4258 tPtr
->sel
.h
= tPtr
->cursor
.h
-10;
4259 tPtr
->sel
.x
= tPtr
->cursor
.x
+1;
4260 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
4261 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
4262 tPtr
->docWidth
- tPtr
->sel
.x
);
4263 tPtr
->flags
.ownsSelection
= True
;
4270 tb
= (direction
>0) ? tb
->next
: tb
->prior
;
4272 pos
= (direction
>0) ? 0 : tb
->used
;
4281 WMReplaceTextSelection(WMText
*tPtr
, char *replacement
)
4286 if (!tPtr
->flags
.ownsSelection
)
4289 removeSelection(tPtr
);
4292 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
4293 updateCursorPosition(tPtr
);