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 GC bgGC
; /* the background GC to draw with */
110 GC fgGC
; /* the foreground GC to draw with */
111 GC stippledGC
; /* the GC to overlay selected graphics with */
112 Pixmap db
; /* the buffer on which to draw */
113 WMPixmap
*bgPixmap
; /* the background pixmap */
115 myRect visible
; /* the actual rectangle that can be drawn into */
116 myRect cursor
; /* the position and (height) of cursor */
117 myRect sel
; /* the selection rectangle */
119 WMPoint clicked
; /* where in the _document_ was clicked */
121 unsigned short tpos
; /* the position in the currentTextBlock */
122 unsigned short docWidth
; /* the width of the entire document */
123 unsigned int docHeight
; /* the height of the entire document */
125 TextBlock
*firstTextBlock
;
126 TextBlock
*lastTextBlock
;
127 TextBlock
*currentTextBlock
;
129 WMArray
*gfxItems
; /* a nice array of graphic items */
132 WMHandlerID timerID
; /* for nice twinky-winky */
137 WMTextDelegate
*delegate
;
140 WMRulerMargins
*margins
; /* an array of margins */
142 unsigned int nMargins
:7; /* the total number of margins in use */
144 unsigned int monoFont
:1; /* whether to ignore formats and graphic */
145 unsigned int focused
:1; /* whether this instance has input focus */
146 unsigned int editable
:1; /* "silly user, you can't edit me" */
147 unsigned int ownsSelection
:1; /* "I ownz the current selection!" */
148 unsigned int pointerGrabbed
:1;/* "heh, gib me pointer" */
149 unsigned int extendSelection
:1; /* shift-drag to select more regions */
151 unsigned int rulerShown
:1; /* whether the ruler is shown or not */
152 unsigned int frozen
:1; /* whether screen updates are to be made */
153 unsigned int cursorShown
:1; /* whether to show the cursor */
154 unsigned int acceptsGraphic
:1;/* accept graphic when dropped */
155 unsigned int horizOnDemand
:1;/* if a large image should appear*/
156 unsigned int needsLayOut
:1; /* in case of Append/Deletes */
157 unsigned int ignoreNewLine
:1;/* turn it into a ' ' in streams > 1 */
158 unsigned int indentNewLine
:1;/* add " " for a newline typed */
159 unsigned int laidOut
:1; /* have the TextBlocks all been laid out */
160 unsigned int waitingForSelection
:1; /* I don't wanna wait in vain... */
161 unsigned int prepend
:1; /* prepend=1, append=0 (for parsers) */
162 WMAlignment alignment
:2; /* the alignment for text */
163 WMReliefType relief
:3; /* the relief to display with */
164 unsigned int isOverGraphic
:2;/* the mouse is over a graphic */
165 unsigned int first
:1; /* for plain text parsing, newline? */
166 /* unsigned int RESERVED:1; */
171 #define NOTIFY(T,C,N,A) {\
172 WMNotification *notif = WMCreateNotification(N,T,A);\
173 if ((T)->delegate && (T)->delegate->C)\
174 (*(T)->delegate->C)((T)->delegate,notif);\
175 WMPostNotification(notif);\
176 WMReleaseNotification(notif);}
181 /* just to print blocks of text not terminated by \0 */
183 output(char *ptr
, int len
)
188 /* printf(" s is [%s] (%d)\n", s, strlen(s)); */
194 #define CURSOR_BLINK_ON_DELAY 600
195 #define CURSOR_BLINK_OFF_DELAY 400
199 #define STIPPLE_WIDTH 8
200 #define STIPPLE_HEIGHT 8
201 static unsigned char STIPPLE_BITS
[] = {
202 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa
207 static char *default_bullet
[] = {
209 " c None s None", ". c black",
210 "X c white", "o c #808080",
219 static void handleEvents(XEvent
*event
, void *data
);
220 static void layOutDocument(Text
*tPtr
);
221 static void updateScrollers(Text
*tPtr
);
225 getMarginNumber(Text
*tPtr
, WMRulerMargins
*margins
)
229 for(i
=0; i
< tPtr
->nMargins
; i
++) {
231 if(WMIsMarginEqualToMargin(&tPtr
->margins
[i
], margins
))
241 newMargin(Text
*tPtr
, WMRulerMargins
*margins
)
246 tPtr
->margins
[0].retainCount
++;
250 n
= getMarginNumber(tPtr
, margins
);
254 if(tPtr
->nMargins
>= 127) {
255 n
= tPtr
->nMargins
-1;
259 tPtr
->margins
= wrealloc(tPtr
->margins
,
260 (++tPtr
->nMargins
)*sizeof(WMRulerMargins
));
262 n
= tPtr
->nMargins
-1;
263 tPtr
->margins
[n
].left
= margins
->left
;
264 tPtr
->margins
[n
].first
= margins
->first
;
265 tPtr
->margins
[n
].body
= margins
->body
;
266 tPtr
->margins
[n
].right
= margins
->right
;
267 /* for each tab... */
268 tPtr
->margins
[n
].retainCount
= 1;
270 tPtr
->margins
[n
].retainCount
++;
277 sectionWasSelected(Text
*tPtr
, TextBlock
*tb
, XRectangle
*rect
, int s
)
279 unsigned short i
, w
, lw
, selected
= False
, extend
= False
;
283 /* if selection rectangle completely encloses the section */
284 if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
285 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
286 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
288 sel
.w
= tPtr
->visible
.w
;
289 selected
= extend
= True
;
291 /* or if it starts on a line and then goes further down */
292 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
293 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
294 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
295 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
296 >= tPtr
->visible
.y
+ tPtr
->sel
.y
) ) {
297 sel
.x
= WMAX(tPtr
->sel
.x
, tPtr
->clicked
.x
);
298 sel
.w
= tPtr
->visible
.w
;
299 selected
= extend
= True
;
301 /* or if it begins before a line, but ends on it */
302 } else if ((tb
->sections
[s
]._y
>= tPtr
->visible
.y
+ tPtr
->sel
.y
)
303 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
304 >= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
)
305 && (tb
->sections
[s
]._y
306 <= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
308 if (1||tPtr
->sel
.x
+ tPtr
->sel
.w
> tPtr
->clicked
.x
)
309 sel
.w
= tPtr
->sel
.x
+ tPtr
->sel
.w
;
316 /* or if the selection rectangle lies entirely within a line */
317 } else if ((tb
->sections
[s
]._y
<= tPtr
->visible
.y
+ tPtr
->sel
.y
)
318 && (tPtr
->sel
.w
>= 2)
319 && (tb
->sections
[s
]._y
+ tb
->sections
[s
].h
320 >= tPtr
->visible
.y
+ tPtr
->sel
.y
+ tPtr
->sel
.h
) ) {
329 /* if not within (modified) selection rectangle */
330 if ( tb
->sections
[s
].x
> sel
.x
+ sel
.w
331 || tb
->sections
[s
].x
+ tb
->sections
[s
].w
< sel
.x
)
335 if ( tb
->sections
[s
].x
+ tb
->sections
[s
].w
<= sel
.x
+ sel
.w
336 && tb
->sections
[s
].x
>= sel
.x
) {
337 rect
->width
= tb
->sections
[s
].w
;
338 rect
->x
= tb
->sections
[s
].x
;
343 i
= tb
->sections
[s
].begin
;
346 if (0&& tb
->sections
[s
].x
>= sel
.x
) {
347 tb
->s_begin
= tb
->sections
[s
].begin
;
351 while (++i
<= tb
->sections
[s
].end
) {
353 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
-1]), 1);
356 if (lw
+ tb
->sections
[s
].x
>= sel
.x
357 || i
== tb
->sections
[s
].end
) {
360 tb
->s_begin
= (tb
->selected
? WMIN(tb
->s_begin
, i
) : i
);
365 if (i
> tb
->sections
[s
].end
) {
366 printf("WasSelected: (i > tb->sections[s].end) \n");
370 _selEnd
: rect
->x
= tb
->sections
[s
].x
+ lw
;
372 while(++i
<= tb
->sections
[s
].end
) {
374 w
= WMWidthOfString(tb
->d
.font
, &(tb
->text
[i
-1]), 1);
377 if (lw
+ rect
->x
>= sel
.x
+ sel
.w
378 || i
== tb
->sections
[s
].end
) {
380 if (i
!= tb
->sections
[s
].end
) {
386 if (tb
->sections
[s
].last
&& sel
.x
+ sel
.w
387 >= tb
->sections
[s
].x
+ tb
->sections
[s
].w
389 rect
->width
+= (tPtr
->visible
.w
- rect
->x
- lw
);
392 tb
->s_end
= (tb
->selected
? WMAX(tb
->s_end
, i
) : i
);
401 rect
->y
= tb
->sections
[s
]._y
- tPtr
->vpos
;
402 rect
->height
= tb
->sections
[s
].h
;
403 if(tb
->graphic
) { printf("DEBUG: graphic s%d h%d\n", s
,tb
->sections
[s
].h
);}
410 setSelectionProperty(WMText
*tPtr
, WMFont
*font
, WMColor
*color
, int underlined
)
415 tb
= tPtr
->firstTextBlock
;
416 if (!tb
|| !tPtr
->flags
.ownsSelection
)
419 if(font
&& (!color
|| underlined
==-1))
423 if (tPtr
->flags
.monoFont
|| tb
->selected
) {
425 if (tPtr
->flags
.monoFont
|| (tb
->s_end
- tb
->s_begin
== tb
->used
)
430 WMReleaseFont(tb
->d
.font
);
431 tb
->d
.font
= WMRetainFont(font
);
433 } else if(underlined
!=-1) {
434 tb
->underlined
= underlined
;
436 WMReleaseColor(tb
->color
);
437 tb
->color
= WMRetainColor(color
);
440 } else if (tb
->s_end
<= tb
->used
&& tb
->s_begin
< tb
->s_end
) {
442 TextBlock
*midtb
, *otb
= tb
;
444 if(underlined
!= -1) {
445 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
446 &(tb
->text
[tb
->s_begin
]), tb
->d
.font
, tb
->color
,
447 False
, (tb
->s_end
- tb
->s_begin
));
449 midtb
= (TextBlock
*) WMCreateTextBlockWithText(tPtr
,
450 &(tb
->text
[tb
->s_begin
]),
451 (isFont
?font
:tb
->d
.font
),
452 (isFont
?tb
->color
:color
),
453 False
, (tb
->s_end
- tb
->s_begin
));
458 if(underlined
!= -1) {
459 midtb
->underlined
= underlined
;
461 midtb
->underlined
= otb
->underlined
;
464 midtb
->selected
= !True
;
466 midtb
->s_end
= midtb
->used
;
467 tPtr
->currentTextBlock
= tb
;
468 WMAppendTextBlock(tPtr
, midtb
);
469 tb
= tPtr
->currentTextBlock
;
472 if (otb
->used
- otb
->s_end
> 0) {
475 WMCreateTextBlockWithText(tPtr
,
476 &(otb
->text
[otb
->s_end
]), otb
->d
.font
, otb
->color
,
477 False
, otb
->used
- otb
->s_end
);
480 ntb
->underlined
= otb
->underlined
;
481 ntb
->selected
= False
;
482 WMAppendTextBlock(tPtr
, ntb
);
483 tb
= tPtr
->currentTextBlock
;
488 tPtr
->currentTextBlock
= midtb
;
491 otb
->selected
= False
;
492 otb
->used
= otb
->s_begin
;
499 tPtr
->flags
.needsLayOut
= True
;
502 /* in case the size changed... */
503 if(isFont
&& tPtr
->currentTextBlock
) {
504 TextBlock
*tb
= tPtr
->currentTextBlock
;
506 printf("%d %d %d\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
507 tPtr
->sel
.y
= 3 + tb
->sections
[0]._y
;
508 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
- tb
->sections
[0]._y
;
509 tPtr
->sel
.w
= tb
->sections
[tb
->nsections
-1].w
;
510 if(tb
->sections
[tb
->nsections
-1]._y
!= tb
->sections
[0]._y
) {
513 printf("%d %d %d\n\n\n", tPtr
->sel
.y
, tPtr
->sel
.h
, tPtr
->sel
.w
);
520 removeSelection(Text
*tPtr
)
522 TextBlock
*tb
= NULL
;
525 if (!(tb
= tPtr
->firstTextBlock
))
530 if(!first
&& !tb
->graphic
) {
531 WMReleaseFont(tPtr
->dFont
);
532 tPtr
->dFont
= WMRetainFont(tb
->d
.font
);
536 if ( (tb
->s_end
- tb
->s_begin
== tb
->used
) || tb
->graphic
) {
537 tPtr
->currentTextBlock
= tb
;
540 } else if(tb
->prior
) {
541 if(tb
->prior
->graphic
)
544 tPtr
->tpos
= tb
->prior
->used
;
545 } else tPtr
->tpos
= 0;
547 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
548 tb
= tPtr
->currentTextBlock
;
551 } else if (tb
->s_end
<= tb
->used
) {
552 memmove(&(tb
->text
[tb
->s_begin
]),
553 &(tb
->text
[tb
->s_end
]), tb
->used
- tb
->s_end
);
554 tb
->used
-= (tb
->s_end
- tb
->s_begin
);
555 tb
->selected
= False
;
556 tPtr
->tpos
= tb
->s_begin
;
567 getFirstNonGraphicBlockFor(TextBlock
*tb
, short dir
)
569 TextBlock
*hold
= tb
;
577 tb
= (dir
? tb
->next
: tb
->prior
);
585 tb
= (dir
? tb
->prior
: tb
->next
);
597 updateStartForCurrentTextBlock(Text
*tPtr
, int x
, int y
, int *dir
,
600 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
601 tb
= getFirstNonGraphicBlockFor(tb
, *dir
);
606 tPtr
->currentTextBlock
=
607 (dir
? tPtr
->lastTextBlock
: tPtr
->firstTextBlock
);
615 layOutDocument(tPtr
);
619 *dir
= !(y
<= tb
->sections
[0].y
);
621 if ( ( y
<= tb
->sections
[0]._y
+ tb
->sections
[0].h
)
622 && (y
>= tb
->sections
[0]._y
) ) {
623 /* if it's on the same line */
624 if(x
< tb
->sections
[0].x
)
628 if ( ( y
<= tb
->sections
[tb
->nsections
-1]._y
629 + tb
->sections
[tb
->nsections
-1].h
)
630 && (y
>= tb
->sections
[tb
->nsections
-1]._y
) ) {
631 /* if it's on the same line */
632 if(x
> tb
->sections
[tb
->nsections
-1].x
)
642 paintText(Text
*tPtr
)
648 int len
, y
, c
, s
, done
=False
, prev_y
=-23, dir
/* 1 = down */;
649 WMScreen
*scr
= tPtr
->view
->screen
;
650 Display
*dpy
= tPtr
->view
->screen
->display
;
651 Window win
= tPtr
->view
->window
;
654 if (!tPtr
->view
->flags
.realized
|| !tPtr
->db
|| tPtr
->flags
.frozen
)
658 XFillRectangle(dpy
, tPtr
->db
, tPtr
->bgGC
,
659 0, 0, tPtr
->visible
.w
, tPtr
->visible
.h
);
661 if (tPtr
->bgPixmap
) {
662 WMDrawPixmap(tPtr
->bgPixmap
, tPtr
->db
,
663 (tPtr
->visible
.w
-tPtr
->visible
.x
-tPtr
->bgPixmap
->width
)/2,
664 (tPtr
->visible
.h
-tPtr
->visible
.y
-tPtr
->bgPixmap
->height
)/2);
667 if (! (tb
= tPtr
->currentTextBlock
)) {
668 if (! (tb
= tPtr
->firstTextBlock
)) {
673 if (tPtr
->flags
.ownsSelection
) {
674 color
= WMGrayColor(scr
);
675 greyGC
= WMColorGC(color
);
676 WMReleaseColor(color
);
683 /* first, which direction? Don't waste time looking all over,
684 since the parts to be drawn will most likely be near what
685 was previously drawn */
686 if(!updateStartForCurrentTextBlock(tPtr
, 0, tPtr
->vpos
, &dir
, tb
))
691 if (tb
->graphic
&& tPtr
->flags
.monoFont
)
695 if(tPtr
->vpos
<= tb
->sections
[tb
->nsections
-1]._y
696 + tb
->sections
[tb
->nsections
-1].h
)
699 if(tPtr
->vpos
>= tb
->sections
[tb
->nsections
-1]._y
700 + tb
->sections
[tb
->nsections
-1].h
)
717 /* first, place all text that can be viewed */
718 while (!done
&& tb
) {
726 tb
->selected
= False
;
728 for(s
=0; s
<tb
->nsections
&& !done
; s
++) {
730 if (tb
->sections
[s
]._y
> tPtr
->vpos
+ tPtr
->visible
.h
) {
735 if ( tb
->sections
[s
].y
+ tb
->sections
[s
].h
< tPtr
->vpos
)
738 if (tPtr
->flags
.monoFont
) {
743 gc
= WMColorGC(tb
->color
);
746 if (tPtr
->flags
.ownsSelection
) {
749 if ( sectionWasSelected(tPtr
, tb
, &rect
, s
)) {
751 XFillRectangle(dpy
, tPtr
->db
, greyGC
,
752 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
756 prev_y
= tb
->sections
[s
]._y
;
758 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
759 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
760 y
= tb
->sections
[s
].y
- tPtr
->vpos
;
761 WMDrawString(scr
, tPtr
->db
, gc
, font
,
762 tb
->sections
[s
].x
- tPtr
->hpos
, y
, text
, len
);
764 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
765 XDrawLine(dpy
, tPtr
->db
, gc
,
766 tb
->sections
[s
].x
- tPtr
->hpos
,
768 tb
->sections
[s
].x
+ tb
->sections
[s
].w
- tPtr
->hpos
,
774 tb
= (!done
? tb
->next
: NULL
);
778 /* now , show all graphic items that can be viewed */
779 c
= WMGetArrayItemCount(tPtr
->gfxItems
);
780 if (c
> 0 && !tPtr
->flags
.monoFont
) {
784 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
786 /* if it's not viewable, and mapped, unmap it */
787 if (tb
->sections
[0]._y
+ tb
->sections
[0].h
<= tPtr
->vpos
788 || tb
->sections
[0]._y
>= tPtr
->vpos
+ tPtr
->visible
.h
) {
791 if ((W_VIEW(tb
->d
.widget
))->flags
.mapped
) {
792 WMUnmapWidget(tb
->d
.widget
);
796 /* if it's viewable, and not mapped, map it */
798 W_View
*view
= W_VIEW(tb
->d
.widget
);
800 if (!view
->flags
.realized
)
801 WMRealizeWidget(tb
->d
.widget
);
802 if(!view
->flags
.mapped
) {
803 XMapWindow(view
->screen
->display
, view
->window
);
804 XFlush(view
->screen
->display
);
805 view
->flags
.mapped
= 1;
810 WMMoveWidget(tb
->d
.widget
,
811 tb
->sections
[0].x
+ tPtr
->visible
.x
- tPtr
->hpos
,
812 tb
->sections
[0].y
+ tPtr
->visible
.y
- tPtr
->vpos
);
813 h
= WMWidgetHeight(tb
->d
.widget
) + 1;
816 WMDrawPixmap(tb
->d
.pixmap
, tPtr
->db
,
817 tb
->sections
[0].x
- tPtr
->hpos
,
818 tb
->sections
[0].y
- tPtr
->vpos
);
819 h
= tb
->d
.pixmap
->height
+ 1;
823 if (tPtr
->flags
.ownsSelection
) {
826 if ( sectionWasSelected(tPtr
, tb
, &rect
, 0)) {
827 Drawable d
= (0&&tb
->object
?
828 (WMWidgetView(tb
->d
.widget
))->window
: tPtr
->db
);
831 XFillRectangle(dpy
, d
, tPtr
->stippledGC
,
832 //XFillRectangle(dpy, tPtr->db, tPtr->stippledGC,
833 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
837 if (!tPtr
->flags
.monoFont
&& tb
->underlined
) {
838 XDrawLine(dpy
, tPtr
->db
, WMColorGC(tb
->color
),
839 tb
->sections
[0].x
- tPtr
->hpos
,
840 tb
->sections
[0].y
+ h
- tPtr
->vpos
,
841 tb
->sections
[0].x
+ tb
->sections
[0].w
- tPtr
->hpos
,
842 tb
->sections
[0].y
+ h
- tPtr
->vpos
);
850 if (tPtr
->flags
.editable
&& tPtr
->flags
.cursorShown
851 && tPtr
->cursor
.x
!= -23 && tPtr
->flags
.focused
) {
852 int y
= tPtr
->cursor
.y
- tPtr
->vpos
;
853 XDrawLine(dpy
, tPtr
->db
, tPtr
->fgGC
,
855 tPtr
->cursor
.x
, y
+ tPtr
->cursor
.h
);
858 XCopyArea(dpy
, tPtr
->db
, win
, tPtr
->bgGC
, 0, 0,
859 tPtr
->visible
.w
, tPtr
->visible
.h
,
860 tPtr
->visible
.x
, tPtr
->visible
.y
);
862 W_DrawRelief(scr
, win
, 0, 0,
863 tPtr
->view
->size
.width
, tPtr
->view
->size
.height
,
866 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)
867 XDrawLine(dpy
, win
, tPtr
->fgGC
,
868 2, 42, tPtr
->view
->size
.width
-4, 42);
873 mouseOverObject(Text
*tPtr
, int x
, int y
)
878 x
-= tPtr
->visible
.x
;
880 y
-= tPtr
->visible
.y
;
883 if(tPtr
->flags
.ownsSelection
) {
886 && tPtr
->sel
.x
+ tPtr
->sel
.w
>= x
887 && tPtr
->sel
.y
+ tPtr
->sel
.h
>= y
) {
888 tPtr
->flags
.isOverGraphic
= 1;
895 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
898 tPtr
->flags
.isOverGraphic
= 0;
902 tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
);
904 if(!tb
|| !tb
->sections
) {
905 tPtr
->flags
.isOverGraphic
= 0;
910 if(tb
->sections
[0].x
<= x
911 && tb
->sections
[0].y
<= y
912 && tb
->sections
[0].x
+ tb
->sections
[0].w
>= x
913 && tb
->sections
[0].y
+ tb
->d
.pixmap
->height
>= y
) {
914 tPtr
->flags
.isOverGraphic
= 3;
925 tPtr
->flags
.isOverGraphic
= 0;
928 tPtr
->view
->attribs
.cursor
= (result
?
929 tPtr
->view
->screen
->defaultCursor
930 : tPtr
->view
->screen
->textCursor
);
932 XSetWindowAttributes attribs
;
933 attribs
.cursor
= tPtr
->view
->attribs
.cursor
;
934 XChangeWindowAttributes(tPtr
->view
->screen
->display
,
935 tPtr
->view
->window
, CWCursor
,
943 blinkCursor(void *data
)
945 Text
*tPtr
= (Text
*)data
;
947 if (tPtr
->flags
.cursorShown
) {
948 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
,
951 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
,
955 tPtr
->flags
.cursorShown
= !tPtr
->flags
.cursorShown
;
960 updateCursorPosition(Text
*tPtr
)
962 TextBlock
*tb
= NULL
;
965 if(tPtr
->flags
.needsLayOut
)
966 layOutDocument(tPtr
);
968 if (! (tb
= tPtr
->currentTextBlock
)) {
969 if (! (tb
= tPtr
->firstTextBlock
)) {
970 WMFont
*font
= tPtr
->dFont
;
972 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
-font
->y
);
983 y
= tb
->sections
[0].y
;
984 h
= tb
->sections
[0].h
;
985 x
= tb
->sections
[0].x
;
987 } else if(tb
->graphic
) {
988 y
= tb
->sections
[0].y
;
989 h
= tb
->sections
[0].h
;
990 x
= tb
->sections
[0].x
;
992 x
+= tb
->sections
[0].w
;
995 if(tPtr
->tpos
> tb
->used
)
996 tPtr
->tpos
= tb
->used
;
998 for(s
=0; s
<tb
->nsections
-1; s
++) {
1000 if(tPtr
->tpos
>= tb
->sections
[s
].begin
1001 && tPtr
->tpos
<= tb
->sections
[s
].end
)
1005 y
= tb
->sections
[s
]._y
;
1006 h
= tb
->sections
[s
].h
;
1007 x
= tb
->sections
[s
].x
+ WMWidthOfString(
1008 (tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
),
1009 &tb
->text
[tb
->sections
[s
].begin
],
1010 tPtr
->tpos
- tb
->sections
[s
].begin
);
1018 /* scroll the bars if the cursor is not visible */
1019 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1020 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1021 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1023 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1024 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1025 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1026 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1031 updateScrollers(tPtr
);
1036 cursorToTextPosition(Text
*tPtr
, int x
, int y
)
1038 TextBlock
*tb
= NULL
;
1039 int done
=False
, s
, pos
, len
, _w
, _y
, dir
=1; /* 1 == "down" */
1042 if(tPtr
->flags
.needsLayOut
)
1043 layOutDocument(tPtr
);
1045 y
+= (tPtr
->vpos
- tPtr
->visible
.y
);
1049 x
-= (tPtr
->visible
.x
- 2);
1053 /* clicked is relative to document, not window... */
1054 tPtr
->clicked
.x
= x
;
1055 tPtr
->clicked
.y
= y
;
1057 if (! (tb
= tPtr
->currentTextBlock
)) {
1058 if (! (tb
= tPtr
->firstTextBlock
)) {
1059 WMFont
*font
= tPtr
->dFont
;
1061 tPtr
->cursor
.h
= font
->height
+ abs(font
->height
-font
->y
);
1068 /* first, which direction? Most likely, newly clicked
1069 position will be close to previous */
1070 if(!updateStartForCurrentTextBlock(tPtr
, x
, y
, &dir
, tb
))
1074 s
= (dir
? 0 : tb
->nsections
-1);
1075 if ( y
>= tb
->sections
[s
]._y
1076 && y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) {
1080 /* get the first (or last) section of the TextBlock that
1081 lies about the vertical click point */
1083 while (!done
&& tb
) {
1085 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1086 if( (dir
?tb
->next
:tb
->prior
))
1087 tb
= (dir
?tb
->next
:tb
->prior
);
1091 s
= (dir
? 0 : tb
->nsections
-1);
1092 while (!done
&& (dir
? (s
<tb
->nsections
) : (s
>=0) )) {
1094 if ( (dir
? (y
<= tb
->sections
[s
]._y
+ tb
->sections
[s
].h
) :
1095 ( y
>= tb
->sections
[s
]._y
) ) ) {
1103 if ( (dir
? tb
->next
: tb
->prior
)) {
1104 tb
= (dir
? tb
->next
: tb
->prior
);
1107 break; /* goto _doneH; */
1113 if (s
<0 || s
>=tb
->nsections
) {
1114 s
= (dir
? tb
->nsections
-1 : 0);
1118 /* we have the line, which TextBlock on that line is it? */
1119 pos
= (dir
?0:tb
->sections
[s
].begin
);
1120 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1121 TextBlock
*hold
= tb
;
1122 tb
= getFirstNonGraphicBlockFor(hold
, dir
);
1136 _y
= tb
->sections
[s
]._y
;
1140 if (tPtr
->flags
.monoFont
&& tb
->graphic
) {
1141 tb
= (dir
? tb
->next
: tb
->prior
);
1148 _w
= WMWidgetWidth(tb
->d
.widget
)-5;
1150 _w
= tb
->d
.pixmap
->width
-5;
1152 if (tb
->sections
[0].x
+ _w
>= x
)
1155 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1156 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1157 _w
= WMWidthOfString(tb
->d
.font
, text
, len
);
1158 if (tb
->sections
[s
].x
+ _w
>= x
)
1163 if (tb
->sections
[s
].x
<= x
)
1167 if ((dir
? tb
->next
: tb
->prior
)) {
1168 TextBlock
*nxt
= (dir
? tb
->next
: tb
->prior
);
1169 if (tPtr
->flags
.monoFont
&& nxt
->graphic
) {
1170 nxt
= getFirstNonGraphicBlockFor(nxt
, dir
);
1172 pos
= (dir
?0:tb
->sections
[s
].begin
);
1173 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1178 if (_y
!= nxt
->sections
[dir
?0:nxt
->nsections
-1]._y
) {
1179 /* this must be the last/first on this line. stop */
1180 pos
= (dir
? tb
->sections
[s
].end
: 0);
1181 tPtr
->cursor
.x
= tb
->sections
[s
].x
;
1185 tPtr
->cursor
.x
+= WMWidgetWidth(tb
->d
.widget
);
1187 tPtr
->cursor
.x
+= tb
->d
.pixmap
->width
;
1188 } else if (pos
> tb
->sections
[s
].begin
) {
1190 WMWidthOfString(tb
->d
.font
,
1191 &(tb
->text
[tb
->sections
[s
].begin
]),
1192 pos
- tb
->sections
[s
].begin
);
1199 if ( (dir
? tb
->next
: tb
->prior
)) {
1200 tb
= (dir
? tb
->next
: tb
->prior
);
1207 s
= (dir
? 0 : tb
->nsections
-1);
1210 /* we have said TextBlock, now where within it? */
1213 int gw
= (tb
->object
?
1214 WMWidgetWidth(tb
->d
.widget
) : tb
->d
.pixmap
->width
);
1216 tPtr
->cursor
.x
= tb
->sections
[0].x
;
1218 if(x
> tPtr
->cursor
.x
+ gw
/2) {
1220 tPtr
->cursor
.x
+= gw
;
1222 printf("first %d\n", tb
->first
);
1224 if(tb
->prior
->graphic
) pos
= 1;
1225 else pos
= tb
->prior
->used
;
1235 WMFont
*f
= tb
->d
.font
;
1236 len
= tb
->sections
[s
].end
- tb
->sections
[s
].begin
;
1237 text
= &(tb
->text
[tb
->sections
[s
].begin
]);
1239 _w
= x
- tb
->sections
[s
].x
;
1242 while (pos
<len
&& WMWidthOfString(f
, text
, pos
+1) < _w
)
1245 tPtr
->cursor
.x
= tb
->sections
[s
].x
+
1246 (pos
? WMWidthOfString(f
, text
, pos
) : 0);
1248 pos
+= tb
->sections
[s
].begin
;
1254 tPtr
->tpos
= (pos
<=1)? pos
: 0;
1256 tPtr
->tpos
= (pos
<tb
->used
)? pos
: tb
->used
;
1260 printf("...for this app will surely crash :-)\n");
1262 tPtr
->currentTextBlock
= tb
;
1263 tPtr
->cursor
.h
= tb
->sections
[s
].h
;
1264 tPtr
->cursor
.y
= tb
->sections
[s
]._y
;
1266 /* scroll the bars if the cursor is not visible */
1267 if(tPtr
->flags
.editable
&& tPtr
->cursor
.x
!= -23) {
1268 if(tPtr
->cursor
.y
+tPtr
->cursor
.h
1269 > tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
) {
1271 (tPtr
->cursor
.y
+tPtr
->cursor
.h
+10
1272 - (tPtr
->vpos
+tPtr
->visible
.y
+tPtr
->visible
.h
));
1273 updateScrollers(tPtr
);
1274 } else if(tPtr
->cursor
.y
< tPtr
->vpos
+tPtr
->visible
.y
) {
1275 tPtr
->vpos
-= (tPtr
->vpos
+tPtr
->visible
.y
-tPtr
->cursor
.y
);
1276 updateScrollers(tPtr
);
1285 updateScrollers(Text
*tPtr
)
1288 if (tPtr
->flags
.frozen
)
1292 if (tPtr
->docHeight
< tPtr
->visible
.h
) {
1293 WMSetScrollerParameters(tPtr
->vS
, 0, 1);
1296 float hmax
= (float)(tPtr
->docHeight
);
1297 WMSetScrollerParameters(tPtr
->vS
,
1298 ((float)tPtr
->vpos
)/(hmax
- (float)tPtr
->visible
.h
),
1299 (float)tPtr
->visible
.h
/hmax
);
1301 } else tPtr
->vpos
= 0;
1304 if (tPtr
->docWidth
< tPtr
->visible
.w
) {
1305 WMSetScrollerParameters(tPtr
->hS
, 0, 1);
1308 float wmax
= (float)(tPtr
->docWidth
);
1309 WMSetScrollerParameters(tPtr
->hS
,
1310 ((float)tPtr
->hpos
)/(wmax
- (float)tPtr
->visible
.w
),
1311 (float)tPtr
->visible
.w
/wmax
);
1313 } else tPtr
->hpos
= 0;
1317 scrollersCallBack(WMWidget
*w
, void *self
)
1319 Text
*tPtr
= (Text
*)self
;
1320 Bool scroll
= False
;
1323 if (!tPtr
->view
->flags
.realized
|| tPtr
->flags
.frozen
)
1326 if (w
== tPtr
->vS
) {
1328 height
= tPtr
->visible
.h
;
1330 which
= WMGetScrollerHitPart(tPtr
->vS
);
1333 case WSDecrementLine
:
1334 if (tPtr
->vpos
> 0) {
1335 if (tPtr
->vpos
>16) tPtr
->vpos
-=16;
1341 case WSIncrementLine
: {
1342 int limit
= tPtr
->docHeight
- height
;
1343 if (tPtr
->vpos
< limit
) {
1344 if (tPtr
->vpos
<limit
-16) tPtr
->vpos
+=16;
1345 else tPtr
->vpos
=limit
;
1351 case WSDecrementPage
:
1352 if(((int)tPtr
->vpos
- (int)height
) >= 0)
1353 tPtr
->vpos
-= height
;
1360 case WSIncrementPage
:
1361 tPtr
->vpos
+= height
;
1362 if (tPtr
->vpos
> (tPtr
->docHeight
- height
))
1363 tPtr
->vpos
= tPtr
->docHeight
- height
;
1369 tPtr
->vpos
= WMGetScrollerValue(tPtr
->vS
)
1370 * (float)(tPtr
->docHeight
- height
);
1378 scroll
= (tPtr
->vpos
!= tPtr
->prevVpos
);
1379 tPtr
->prevVpos
= tPtr
->vpos
;
1383 if (w
== tPtr
->hS
) {
1384 int width
= tPtr
->visible
.w
;
1386 which
= WMGetScrollerHitPart(tPtr
->hS
);
1389 case WSDecrementLine
:
1390 if (tPtr
->hpos
> 0) {
1391 if (tPtr
->hpos
>16) tPtr
->hpos
-=16;
1396 case WSIncrementLine
: {
1397 int limit
= tPtr
->docWidth
- width
;
1398 if (tPtr
->hpos
< limit
) {
1399 if (tPtr
->hpos
<limit
-16) tPtr
->hpos
+=16;
1400 else tPtr
->hpos
=limit
;
1405 case WSDecrementPage
:
1406 if(((int)tPtr
->hpos
- (int)width
) >= 0)
1407 tPtr
->hpos
-= width
;
1414 case WSIncrementPage
:
1415 tPtr
->hpos
+= width
;
1416 if (tPtr
->hpos
> (tPtr
->docWidth
- width
))
1417 tPtr
->hpos
= tPtr
->docWidth
- width
;
1423 tPtr
->hpos
= WMGetScrollerValue(tPtr
->hS
)
1424 * (float)(tPtr
->docWidth
- width
);
1432 scroll
= (tPtr
->hpos
!= tPtr
->prevHpos
);
1433 tPtr
->prevHpos
= tPtr
->hpos
;
1437 updateScrollers(tPtr
);
1446 unsigned short begin
, end
; /* what part of the text block */
1451 layOutLine(Text
*tPtr
, myLineItems
*items
, int nitems
, int x
, int y
)
1453 int i
, j
=0, lw
= 0, line_height
=0, max_d
=0, len
, n
;
1456 TextBlock
*tb
, *tbsame
=NULL
;
1458 if(!items
|| nitems
== 0)
1461 for(i
=0; i
<nitems
; i
++) {
1465 if (!tPtr
->flags
.monoFont
) {
1467 WMWidget
*wdt
= tb
->d
.widget
;
1468 line_height
= WMAX(line_height
, WMWidgetHeight(wdt
));
1469 if (tPtr
->flags
.alignment
!= WALeft
)
1470 lw
+= WMWidgetWidth(wdt
);
1472 line_height
= WMAX(line_height
,
1473 tb
->d
.pixmap
->height
+ max_d
);
1474 if (tPtr
->flags
.alignment
!= WALeft
)
1475 lw
+= tb
->d
.pixmap
->width
;
1480 font
= (tPtr
->flags
.monoFont
)?tPtr
->dFont
: tb
->d
.font
;
1481 max_d
= WMAX(max_d
, abs(font
->height
-font
->y
));
1482 line_height
= WMAX(line_height
, font
->height
+ max_d
);
1483 text
= &(tb
->text
[items
[i
].begin
]);
1484 len
= items
[i
].end
- items
[i
].begin
;
1485 if (tPtr
->flags
.alignment
!= WALeft
)
1486 lw
+= WMWidthOfString(font
, text
, len
);
1490 if (tPtr
->flags
.alignment
== WARight
) {
1491 j
= tPtr
->visible
.w
- lw
;
1492 } else if (tPtr
->flags
.alignment
== WACenter
) {
1493 j
= (int) ((float)(tPtr
->visible
.w
- lw
))/2.0;
1496 for(i
=0; i
<nitems
; i
++) {
1499 if (tbsame
== tb
) { /* extend it, since it's on same line */
1500 tb
->sections
[tb
->nsections
-1].end
= items
[i
].end
;
1501 n
= tb
->nsections
-1;
1503 tb
->sections
= wrealloc(tb
->sections
,
1504 (++tb
->nsections
)*sizeof(Section
));
1505 n
= tb
->nsections
-1;
1506 tb
->sections
[n
]._y
= y
+ max_d
;
1507 tb
->sections
[n
].max_d
= max_d
;
1508 tb
->sections
[n
].x
= x
+j
;
1509 tb
->sections
[n
].h
= line_height
;
1510 tb
->sections
[n
].begin
= items
[i
].begin
;
1511 tb
->sections
[n
].end
= items
[i
].end
;
1514 tb
->sections
[n
].last
= (i
+1 == nitems
);
1517 if (!tPtr
->flags
.monoFont
) {
1519 WMWidget
*wdt
= tb
->d
.widget
;
1520 tb
->sections
[n
].y
= max_d
+ y
1521 + line_height
- WMWidgetHeight(wdt
);
1522 tb
->sections
[n
].w
= WMWidgetWidth(wdt
);
1524 tb
->sections
[n
].y
= y
+ line_height
1525 + max_d
- tb
->d
.pixmap
->height
;
1526 tb
->sections
[n
].w
= tb
->d
.pixmap
->width
;
1528 x
+= tb
->sections
[n
].w
;
1531 font
= (tPtr
->flags
.monoFont
)? tPtr
->dFont
: tb
->d
.font
;
1532 len
= items
[i
].end
- items
[i
].begin
;
1533 text
= &(tb
->text
[items
[i
].begin
]);
1535 tb
->sections
[n
].y
= y
+line_height
-font
->y
;
1537 WMWidthOfString(font
,
1538 &(tb
->text
[tb
->sections
[n
].begin
]),
1539 tb
->sections
[n
].end
- tb
->sections
[n
].begin
);
1541 x
+= WMWidthOfString(font
, text
, len
);
1553 layOutDocument(Text
*tPtr
)
1556 myLineItems
*items
= NULL
;
1557 unsigned int itemsSize
=0, nitems
=0, begin
, end
;
1559 unsigned int x
, y
=0, lw
= 0, width
=0, bmargin
;
1560 char *start
=NULL
, *mark
=NULL
;
1562 if ( tPtr
->flags
.frozen
|| (!(tb
= tPtr
->firstTextBlock
)) )
1565 assert(tPtr
->visible
.w
> 20);
1567 tPtr
->docWidth
= tPtr
->visible
.w
;
1568 x
= tPtr
->margins
[tb
->marginN
].first
;
1569 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1571 /* only partial layOut needed: re-Lay only affected textblocks */
1572 if (tPtr
->flags
.laidOut
) {
1573 tb
= tPtr
->currentTextBlock
;
1575 /* search backwards for textblocks on same line */
1577 if (!tb
->sections
|| tb
->nsections
<1) {
1578 tb
= tPtr
->firstTextBlock
;
1579 tPtr
->flags
.laidOut
= False
;
1584 if(!tb
->prior
->sections
|| tb
->prior
->nsections
<1) {
1585 tb
= tPtr
->firstTextBlock
;
1586 tPtr
->flags
.laidOut
= False
;
1591 if (tb
->sections
[0]._y
!=
1592 tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
) {
1598 if(tb
->prior
&& tb
->prior
->sections
&& tb
->prior
->nsections
>0) {
1599 y
= tb
->prior
->sections
[tb
->prior
->nsections
-1]._y
+
1600 tb
->prior
->sections
[tb
->prior
->nsections
-1].h
-
1601 tb
->prior
->sections
[tb
->prior
->nsections
-1].max_d
;
1610 if (tb
->sections
&& tb
->nsections
>0) {
1611 wfree(tb
->sections
);
1612 tb
->sections
= NULL
;
1616 if (tb
->first
&& tb
->blank
&& tb
->next
&& !tb
->next
->first
) {
1617 TextBlock
*next
= tb
->next
;
1618 tPtr
->currentTextBlock
= tb
;
1619 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1625 if (tb
->first
&& tb
!= tPtr
->firstTextBlock
) {
1626 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1627 x
= tPtr
->margins
[tb
->marginN
].first
;
1628 bmargin
= tPtr
->margins
[tb
->marginN
].body
;
1634 if (!tPtr
->flags
.monoFont
) {
1636 width
= WMWidgetWidth(tb
->d
.widget
);
1638 width
= tb
->d
.pixmap
->width
;
1640 if (width
> tPtr
->docWidth
)
1641 tPtr
->docWidth
= width
;
1644 if (lw
>= tPtr
->visible
.w
- x
) {
1645 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1651 if(nitems
+ 1> itemsSize
) {
1652 items
= wrealloc(items
,
1653 (++itemsSize
)*sizeof(myLineItems
));
1656 items
[nitems
].tb
= tb
;
1657 items
[nitems
].begin
= 0;
1658 items
[nitems
].end
= 0;
1662 } else if ((start
= tb
->text
)) {
1664 font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
1667 mark
= strchr(start
, ' ');
1669 end
+= (int)(mark
-start
)+1;
1672 end
+= strlen(start
);
1679 if (end
-begin
> 0) {
1681 width
= WMWidthOfString(font
,
1682 &tb
->text
[begin
], end
-begin
);
1684 /* if it won't fit, char wrap it */
1685 if (width
>= tPtr
->visible
.w
) {
1686 char *t
= &tb
->text
[begin
];
1687 int l
=end
-begin
, i
=0;
1689 width
= WMWidthOfString(font
, t
, ++i
);
1690 } while (width
< tPtr
->visible
.w
&& i
< l
);
1693 start
= &tb
->text
[end
];
1699 if (lw
>= tPtr
->visible
.w
- x
) {
1700 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1706 if(nitems
+ 1 > itemsSize
) {
1707 items
= wrealloc(items
,
1708 (++itemsSize
)*sizeof(myLineItems
));
1711 items
[nitems
].tb
= tb
;
1712 items
[nitems
].begin
= begin
;
1713 items
[nitems
].end
= end
;
1721 /* not yet fully ready. but is already VERY FAST for a 3Mbyte file ;-) */
1722 if(0&&tPtr
->flags
.laidOut
1723 && tb
->next
&& tb
->next
->sections
&& tb
->next
->nsections
>0
1724 && (tPtr
->vpos
+ tPtr
->visible
.h
1725 < tb
->next
->sections
[0]._y
)) {
1726 if(tPtr
->lastTextBlock
->sections
1727 && tPtr
->lastTextBlock
->nsections
> 0 ) {
1728 TextBlock
*ltb
= tPtr
->lastTextBlock
;
1729 int ly
= ltb
->sections
[ltb
->nsections
-1]._y
;
1730 int lh
= ltb
->sections
[ltb
->nsections
-1].h
;
1733 lh
+= 1 + tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
;
1734 printf("it's %d\n", tPtr
->visible
.y
+ ltb
->sections
[ltb
->nsections
-1].max_d
);
1736 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1738 sd
= tPtr
->docHeight
-y
;
1740 printf("dif %d-%d: %d\n", ss
, sd
, ss
-sd
);
1741 y
+= tb
->next
->sections
[0]._y
-y
;
1743 printf("nitems%d\n", nitems
);
1745 y
= tPtr
->docHeight
+ss
-sd
;
1749 tPtr
->flags
.laidOut
= False
;
1758 y
+= layOutLine(tPtr
, items
, nitems
, x
, y
);
1760 if (tPtr
->docHeight
!= y
+10) {
1761 tPtr
->docHeight
= y
+10;
1762 updateScrollers(tPtr
);
1765 if(tPtr
->docWidth
> tPtr
->visible
.w
&& !tPtr
->hS
) {
1768 tPtr
->flags
.horizOnDemand
= True
;
1769 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, True
);
1770 event
.type
= Expose
;
1771 handleEvents(&event
, (void *)tPtr
);
1773 } else if(tPtr
->docWidth
<= tPtr
->visible
.w
1774 && tPtr
->hS
&& tPtr
->flags
.horizOnDemand
) {
1775 tPtr
->flags
.horizOnDemand
= False
;
1776 WMSetTextHasHorizontalScroller((WMText
*)tPtr
, False
);
1779 tPtr
->flags
.laidOut
= True
;
1781 if(items
&& itemsSize
> 0)
1788 textDidResize(W_ViewDelegate
*self
, WMView
*view
)
1790 Text
*tPtr
= (Text
*)view
->self
;
1791 unsigned short w
= tPtr
->view
->size
.width
;
1792 unsigned short h
= tPtr
->view
->size
.height
;
1793 unsigned short rh
= 0, vw
= 0, rel
;
1795 rel
= (tPtr
->flags
.relief
== WRFlat
);
1797 if (tPtr
->ruler
&& tPtr
->flags
.rulerShown
) {
1798 WMMoveWidget(tPtr
->ruler
, 2, 2);
1799 WMResizeWidget(tPtr
->ruler
, w
- 4, 40);
1804 WMMoveWidget(tPtr
->vS
, 1 - (rel
?1:0), rh
+ 1 - (rel
?1:0));
1805 WMResizeWidget(tPtr
->vS
, 20, h
- rh
- 2 + (rel
?2:0));
1807 WMSetRulerOffset(tPtr
->ruler
,22);
1808 } else WMSetRulerOffset(tPtr
->ruler
, 2);
1812 WMMoveWidget(tPtr
->hS
, vw
, h
- 21);
1813 WMResizeWidget(tPtr
->hS
, w
- vw
- 1, 20);
1815 WMMoveWidget(tPtr
->hS
, vw
+1, h
- 21);
1816 WMResizeWidget(tPtr
->hS
, w
- vw
- 2, 20);
1820 tPtr
->visible
.x
= (tPtr
->vS
)?24:4;
1821 tPtr
->visible
.y
= (tPtr
->ruler
&& tPtr
->flags
.rulerShown
)?43:3;
1822 tPtr
->visible
.w
= tPtr
->view
->size
.width
- tPtr
->visible
.x
- 8;
1823 tPtr
->visible
.h
= tPtr
->view
->size
.height
- tPtr
->visible
.y
;
1824 tPtr
->visible
.h
-= (tPtr
->hS
)?20:0;
1825 tPtr
->margins
[0].right
= tPtr
->visible
.w
;
1827 if (tPtr
->view
->flags
.realized
) {
1830 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
1831 tPtr
->db
= (Pixmap
) NULL
;
1834 if (tPtr
->visible
.w
< 40)
1835 tPtr
->visible
.w
= 40;
1836 if (tPtr
->visible
.h
< 20)
1837 tPtr
->visible
.h
= 20;
1840 tPtr
->db
= XCreatePixmap(tPtr
->view
->screen
->display
,
1841 tPtr
->view
->window
, tPtr
->visible
.w
,
1842 tPtr
->visible
.h
, tPtr
->view
->screen
->depth
);
1849 W_ViewDelegate _TextViewDelegate
=
1857 #define TEXT_BUFFER_INCR 8
1858 #define reqBlockSize(requested) (requested + TEXT_BUFFER_INCR)
1861 clearText(Text
*tPtr
)
1863 tPtr
->vpos
= tPtr
->hpos
= 0;
1864 tPtr
->docHeight
= tPtr
->docWidth
= 0;
1865 tPtr
->cursor
.x
= -23;
1867 if (!tPtr
->firstTextBlock
)
1870 while (tPtr
->currentTextBlock
)
1871 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1873 tPtr
->firstTextBlock
= NULL
;
1874 tPtr
->currentTextBlock
= NULL
;
1875 tPtr
->lastTextBlock
= NULL
;
1876 WMEmptyArray(tPtr
->gfxItems
);
1879 /* possibly remove a single character from the currentTextBlock,
1880 or if there's a selection, remove it...
1881 note that Delete and Backspace are treated differently */
1883 deleteTextInteractively(Text
*tPtr
, KeySym ksym
)
1886 Bool back
= (Bool
) (ksym
== XK_BackSpace
);
1887 Bool done
= 1, wasFirst
= 0;
1889 if (!tPtr
->flags
.editable
)
1892 if ( !(tb
= tPtr
->currentTextBlock
) )
1895 if (tPtr
->flags
.ownsSelection
) {
1896 if(removeSelection(tPtr
))
1897 layOutDocument(tPtr
);
1901 wasFirst
= tb
->first
;
1902 if (back
&& tPtr
->tpos
< 1) {
1904 if(tb
->prior
->blank
) {
1905 tPtr
->currentTextBlock
= tb
->prior
;
1906 WMRemoveTextBlock(tPtr
);
1907 tPtr
->currentTextBlock
= tb
;
1909 layOutDocument(tPtr
);
1913 TextBlock
*prior
= tb
->prior
;
1914 tPtr
->currentTextBlock
= tb
;
1915 WMRemoveTextBlock(tPtr
);
1924 tPtr
->tpos
= tb
->used
;
1926 tPtr
->currentTextBlock
= tb
;
1930 tb
->next
->first
= False
;
1931 layOutDocument(tPtr
);
1938 if ( (tb
->used
> 0) && ((back
?tPtr
->tpos
> 0:1))
1939 && (tPtr
->tpos
<= tb
->used
) && !tb
->graphic
) {
1942 memmove(&(tb
->text
[tPtr
->tpos
]),
1943 &(tb
->text
[tPtr
->tpos
+ 1]), tb
->used
- tPtr
->tpos
);
1948 /* if there are no characters left to back over in the textblock,
1949 but it still has characters to the right of the cursor: */
1950 if ( (back
? (tPtr
->tpos
== 0 && !done
) : ( tPtr
->tpos
>= tb
->used
))
1953 /* no more chars, and it's marked as blank? */
1955 TextBlock
*sibling
= (back
? tb
->prior
: tb
->next
);
1957 if(tb
->used
== 0 || tb
->graphic
)
1958 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1961 tPtr
->currentTextBlock
= sibling
;
1963 tPtr
->tpos
= (back
? 1 : 0);
1965 tPtr
->tpos
= (back
? sibling
->used
: 0);
1967 /* no more chars, so mark it as blank */
1968 } else if(tb
->used
== 0) {
1970 } else if(tb
->graphic
) {
1971 Bool hasNext
= (Bool
)(tb
->next
);
1973 WMDestroyTextBlock(tPtr
, WMRemoveTextBlock(tPtr
));
1976 } else if(tPtr
->currentTextBlock
) {
1977 tPtr
->tpos
= (tPtr
->currentTextBlock
->graphic
?
1978 1 : tPtr
->currentTextBlock
->used
);
1980 } else printf("DEBUG: unaccounted for... catch this!\n");
1983 layOutDocument(tPtr
);
1988 insertTextInteractively(Text
*tPtr
, char *text
, int len
)
1991 char *newline
= NULL
;
1993 if (!tPtr
->flags
.editable
) {
1997 if (len
< 1 || !text
)
2001 if(tPtr
->flags
.ignoreNewLine
&& *text
== '\n' && len
== 1)
2005 if (tPtr
->flags
.ownsSelection
)
2006 removeSelection(tPtr
);
2009 if (tPtr
->flags
.ignoreNewLine
) {
2011 for(i
=0; i
<len
; i
++) {
2012 if (text
[i
] == '\n')
2017 tb
= tPtr
->currentTextBlock
;
2018 if (!tb
|| tb
->graphic
) {
2020 WMAppendTextStream(tPtr
, text
);
2021 layOutDocument(tPtr
);
2025 if ((newline
= strchr(text
, '\n'))) {
2026 int nlen
= (int)(newline
-text
);
2027 int s
= tb
->used
- tPtr
->tpos
;
2029 if (!tb
->blank
&& nlen
>0) {
2031 memcpy(save
, &tb
->text
[tPtr
->tpos
], s
);
2032 tb
->used
-= (tb
->used
- tPtr
->tpos
);
2034 insertTextInteractively(tPtr
, text
, nlen
);
2036 WMAppendTextStream(tPtr
, newline
);
2038 insertTextInteractively(tPtr
, save
, s
);
2041 if (tPtr
->tpos
>0 && tPtr
->tpos
< tb
->used
2042 && !tb
->graphic
&& tb
->text
) {
2044 void *ntb
= WMCreateTextBlockWithText(
2045 tPtr
, &tb
->text
[tPtr
->tpos
],
2046 tb
->d
.font
, tb
->color
, True
, tb
->used
- tPtr
->tpos
);
2047 tb
->used
= tPtr
->tpos
;
2048 WMAppendTextBlock(tPtr
, ntb
);
2051 } else if (tPtr
->tpos
== tb
->used
|| tPtr
->tpos
== 0) {
2052 if(tPtr
->flags
.indentNewLine
) {
2053 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2054 " ", tb
->d
.font
, tb
->color
, True
, 4));
2057 WMAppendTextBlock(tPtr
, WMCreateTextBlockWithText(tPtr
,
2058 NULL
, tb
->d
.font
, tb
->color
, True
, 0));
2065 if (tb
->used
+ len
>= tb
->allocated
) {
2066 tb
->allocated
= reqBlockSize(tb
->used
+len
);
2067 tb
->text
= wrealloc(tb
->text
, tb
->allocated
);
2071 memcpy(tb
->text
, text
, len
);
2074 tb
->text
[tb
->used
] = 0;
2078 memmove(&(tb
->text
[tPtr
->tpos
+len
]), &tb
->text
[tPtr
->tpos
],
2079 tb
->used
-tPtr
->tpos
+1);
2080 memmove(&tb
->text
[tPtr
->tpos
], text
, len
);
2083 tb
->text
[tb
->used
] = 0;
2088 layOutDocument(tPtr
);
2093 selectRegion(Text
*tPtr
, int x
, int y
)
2099 y
+= (tPtr
->flags
.rulerShown
? 40: 0);
2102 y
-= 10; /* the original offset */
2104 x
-= tPtr
->visible
.x
-2;
2108 tPtr
->sel
.x
= WMAX(0, WMIN(tPtr
->clicked
.x
, x
));
2109 tPtr
->sel
.w
= abs(tPtr
->clicked
.x
- x
);
2110 tPtr
->sel
.y
= WMAX(0, WMIN(tPtr
->clicked
.y
, y
));
2111 tPtr
->sel
.h
= abs(tPtr
->clicked
.y
- y
);
2113 tPtr
->flags
.ownsSelection
= True
;
2119 releaseSelection(Text
*tPtr
)
2121 TextBlock
*tb
= tPtr
->firstTextBlock
;
2124 tb
->selected
= False
;
2127 tPtr
->flags
.ownsSelection
= False
;
2128 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
,
2136 requestHandler(WMView
*view
, Atom selection
, Atom target
, void *cdata
,
2139 Text
*tPtr
= view
->self
;
2140 Display
*dpy
= tPtr
->view
->screen
->display
;
2142 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
2143 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
2144 WMData
*data
= NULL
;
2147 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
2148 char *text
= WMGetTextSelectedStream(tPtr
);
2151 data
= WMCreateDataWithBytes(text
, strlen(text
));
2152 WMSetDataFormat(data
, TYPETEXT
);
2156 } else printf("didn't get it\n");
2158 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
2159 if (target
== _TARGETS
) {
2162 ptr
= wmalloc(4 * sizeof(Atom
));
2166 ptr
[3] = COMPOUND_TEXT
;
2168 data
= WMCreateDataWithBytes(ptr
, 4*4);
2169 WMSetDataFormat(data
, 32);
2179 lostHandler(WMView
*view
, Atom selection
, void *cdata
)
2181 releaseSelection((WMText
*)view
->self
);
2184 static WMSelectionProcs selectionHandler
= {
2185 requestHandler
, lostHandler
, NULL
2190 ownershipObserver(void *observerData
, WMNotification
*notification
)
2192 if (observerData
!= WMGetNotificationClientData(notification
))
2193 lostHandler(WMWidgetView(observerData
), XA_PRIMARY
, NULL
);
2197 autoSelectText(Text
*tPtr
, int clicks
)
2201 char *mark
= NULL
, behind
, ahead
;
2203 if(!(tb
= tPtr
->currentTextBlock
))
2209 switch(tb
->text
[tPtr
->tpos
]) {
2212 case '<': case '>': behind = '<'; ahead = '>'; break;
2213 case '{': case '}': behind = '{'; ahead = '}'; break;
2214 case '[': case ']': behind = '['; ahead = ']'; break;
2216 default: behind
= ahead
= ' ';
2219 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
2220 tPtr
->sel
.h
= 6;/*tPtr->cursor.h-10;*/
2223 tPtr
->sel
.x
= tb
->sections
[0].x
;
2224 tPtr
->sel
.w
= tb
->sections
[0].w
;
2226 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
2229 while(start
> 0 && tb
->text
[start
-1] != behind
)
2233 if(tPtr
->tpos
> start
){
2234 x
-= WMWidthOfString(font
, &tb
->text
[start
],
2235 tPtr
->tpos
- start
);
2237 tPtr
->sel
.x
= (x
<0?0:x
)+1;
2239 if((mark
= strchr(&tb
->text
[start
], ahead
))) {
2240 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2241 (int)(mark
- &tb
->text
[start
]));
2242 } else if(tb
->used
> start
) {
2243 tPtr
->sel
.w
= WMWidthOfString(font
, &tb
->text
[start
],
2248 } else if(clicks
== 3) {
2249 TextBlock
*cur
= tb
;
2251 while(tb
&& !tb
->first
) {
2254 tPtr
->sel
.y
= tb
->sections
[0]._y
;
2257 while(tb
->next
&& !tb
->next
->first
) {
2260 tPtr
->sel
.h
= tb
->sections
[tb
->nsections
-1]._y
2264 tPtr
->sel
.w
= tPtr
->docWidth
;
2265 tPtr
->clicked
.x
= 0; /* only for now, fix sel. code */
2268 if (!tPtr
->flags
.ownsSelection
) {
2269 WMCreateSelectionHandler(tPtr
->view
,
2270 XA_PRIMARY
, tPtr
->lastClickTime
, &selectionHandler
, NULL
);
2271 tPtr
->flags
.ownsSelection
= True
;
2279 fontChanged(void *observerData
, WMNotification
*notification
)
2281 WMText
*tPtr
= (WMText
*) observerData
;
2282 WMFont
*font
= (WMFont
*)WMGetNotificationClientData(notification
);
2283 printf("fontChanged\n");
2288 if (tPtr
->flags
.ownsSelection
)
2289 WMSetTextSelectionFont(tPtr
, font
);
2294 handleTextKeyPress(Text
*tPtr
, XEvent
*event
)
2298 int control_pressed
= False
;
2299 TextBlock
*tb
= NULL
;
2301 if (((XKeyEvent
*) event
)->state
& ControlMask
)
2302 control_pressed
= True
;
2303 buffer
[XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
)] = 0;
2308 if((tPtr
->currentTextBlock
= tPtr
->firstTextBlock
))
2310 updateCursorPosition(tPtr
);
2315 if((tPtr
->currentTextBlock
= tPtr
->lastTextBlock
)) {
2316 if(tPtr
->currentTextBlock
->graphic
)
2319 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2321 updateCursorPosition(tPtr
);
2326 if(!(tb
= tPtr
->currentTextBlock
))
2332 L_imaGFX
: if(tb
->prior
) {
2333 tPtr
->currentTextBlock
= tb
->prior
;
2334 if(tPtr
->currentTextBlock
->graphic
)
2337 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
2339 if(!tb
->first
&& tPtr
->tpos
> 0)
2341 } else tPtr
->tpos
= 0;
2342 } else tPtr
->tpos
--;
2343 updateCursorPosition(tPtr
);
2348 if(!(tb
= tPtr
->currentTextBlock
))
2352 if(tPtr
->tpos
== tb
->used
) {
2353 R_imaGFX
: if(tb
->next
) {
2354 tPtr
->currentTextBlock
= tb
->next
;
2356 if(!tb
->next
->first
&& tb
->next
->used
>0)
2362 tPtr
->tpos
= tb
->used
;
2364 } else tPtr
->tpos
++;
2365 updateCursorPosition(tPtr
);
2370 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2371 tPtr
->clicked
.y
+ tPtr
->cursor
.h
- tPtr
->vpos
);
2376 cursorToTextPosition(tPtr
, tPtr
->cursor
.x
+ tPtr
->visible
.x
,
2377 tPtr
->visible
.y
+ tPtr
->cursor
.y
- tPtr
->vpos
- 3);
2386 deleteTextInteractively(tPtr
, ksym
);
2387 updateCursorPosition(tPtr
);
2393 control_pressed
= True
;
2397 insertTextInteractively(tPtr
, " ", 4);
2398 updateCursorPosition(tPtr
);
2405 if (*buffer
!= 0 && !control_pressed
) {
2406 insertTextInteractively(tPtr
, buffer
, strlen(buffer
));
2407 updateCursorPosition(tPtr
);
2410 } else if (control_pressed
&& ksym
==XK_r
) {
2411 Bool i
= !tPtr
->flags
.rulerShown
;
2412 WMShowTextRuler(tPtr
, i
);
2413 tPtr
->flags
.rulerShown
= i
;
2415 else if (control_pressed
&& *buffer
== '\a')
2416 XBell(tPtr
->view
->screen
->display
, 0);
2418 WMRelayToNextResponder(tPtr
->view
, event
);
2421 if (!control_pressed
&& tPtr
->flags
.ownsSelection
)
2422 releaseSelection(tPtr
);
2427 pasteText(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2428 void *cdata
, WMData
*data
)
2430 Text
*tPtr
= (Text
*)view
->self
;
2433 tPtr
->flags
.waitingForSelection
= 0;
2436 text
= (char*)WMDataBytes(data
);
2439 (tPtr
->parser
) (tPtr
, (void *) text
);
2440 layOutDocument(tPtr
);
2441 } else insertTextInteractively(tPtr
, text
, strlen(text
));
2442 updateCursorPosition(tPtr
);
2448 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2453 (tPtr
->parser
) (tPtr
, (void *) text
);
2454 layOutDocument(tPtr
);
2455 } else insertTextInteractively(tPtr
, text
, n
);
2456 updateCursorPosition(tPtr
);
2468 handleActionEvents(XEvent
*event
, void *data
)
2470 Text
*tPtr
= (Text
*)data
;
2471 Display
*dpy
= event
->xany
.display
;
2475 switch (event
->type
) {
2477 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2478 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2479 tPtr
->flags
.extendSelection
= True
;
2483 if (tPtr
->flags
.focused
) {
2484 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
2485 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
2486 GrabModeAsync
, GrabModeAsync
, None
,
2487 tPtr
->view
->screen
->invisibleCursor
, CurrentTime
);
2488 tPtr
->flags
.pointerGrabbed
= True
;
2489 handleTextKeyPress(tPtr
, event
);
2494 ksym
= XLookupKeysym((XKeyEvent
*)event
, 0);
2495 if (ksym
== XK_Shift_R
|| ksym
== XK_Shift_L
) {
2496 tPtr
->flags
.extendSelection
= False
;
2498 /* end modify flag so selection can be extended */
2505 if (tPtr
->flags
.pointerGrabbed
) {
2506 tPtr
->flags
.pointerGrabbed
= False
;
2507 XUngrabPointer(dpy
, CurrentTime
);
2510 if(tPtr
->flags
.waitingForSelection
)
2513 if ((event
->xmotion
.state
& Button1Mask
)) {
2514 TextBlock
*tb
= tPtr
->currentTextBlock
;
2516 if(tb
&& tPtr
->flags
.isOverGraphic
&&
2517 tb
->graphic
&& !tb
->object
) {
2519 WMPixmap
*pixmap
= tb
->d
.pixmap
;
2520 char *types
[2] = {"application/X-image", NULL
};
2525 WMDragImageFromView(tPtr
->view
, pixmap
, types
,
2526 wmkpoint(event
->xmotion
.x_root
, event
->xmotion
.y_root
),
2531 if (!tPtr
->flags
.ownsSelection
) {
2532 WMCreateSelectionHandler(tPtr
->view
,
2533 XA_PRIMARY
, event
->xbutton
.time
,
2534 &selectionHandler
, NULL
);
2535 tPtr
->flags
.ownsSelection
= True
;
2538 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2542 mouseOverObject(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2548 if (tPtr
->flags
.pointerGrabbed
) {
2549 tPtr
->flags
.pointerGrabbed
= False
;
2550 XUngrabPointer(dpy
, CurrentTime
);
2554 if (tPtr
->flags
.waitingForSelection
)
2557 if (tPtr
->flags
.extendSelection
&& tPtr
->flags
.ownsSelection
) {
2558 selectRegion(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2562 if (tPtr
->flags
.ownsSelection
)
2563 releaseSelection(tPtr
);
2566 if (event
->xbutton
.button
== Button1
) {
2568 if(WMIsDoubleClick(event
)) {
2569 TextBlock
*tb
= tPtr
->currentTextBlock
;
2571 tPtr
->lastClickTime
= event
->xbutton
.time
;
2572 if(tb
&& tb
->graphic
&& !tb
->object
) {
2573 char desc
[tb
->used
+1];
2574 memcpy(desc
, tb
->text
, tb
->used
);
2576 if(tPtr
->delegate
) {
2577 if(tPtr
->delegate
->didDoubleClickOnPicture
)
2578 (*tPtr
->delegate
->didDoubleClickOnPicture
)
2579 (tPtr
->delegate
, desc
);
2582 autoSelectText(tPtr
, 2);
2585 } else if(event
->xbutton
.time
- tPtr
->lastClickTime
2586 < WINGsConfiguration
.doubleClickDelay
) {
2587 tPtr
->lastClickTime
= event
->xbutton
.time
;
2588 autoSelectText(tPtr
, 3);
2592 if (!tPtr
->flags
.focused
) {
2593 WMSetFocusToWidget(tPtr
);
2594 tPtr
->flags
.focused
= True
;
2597 tPtr
->lastClickTime
= event
->xbutton
.time
;
2598 cursorToTextPosition(tPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
2602 if (event
->xbutton
.button
2603 == WINGsConfiguration
.mouseWheelDown
) {
2604 WMScrollText(tPtr
, 16);
2608 if (event
->xbutton
.button
2609 == WINGsConfiguration
.mouseWheelUp
) {
2610 WMScrollText(tPtr
, -16);
2614 if (event
->xbutton
.button
== Button2
) {
2618 if (!tPtr
->flags
.editable
) {
2623 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
2624 event
->xbutton
.time
, pasteText
, NULL
)) {
2626 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
2627 tPtr
->flags
.waitingForSelection
= 0;
2633 (tPtr
->parser
) (tPtr
, (void *) text
);
2634 layOutDocument(tPtr
);
2637 insertTextInteractively(tPtr
, text
, n
);
2641 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
2642 (void*)WMInsertTextEvent
);
2644 updateCursorPosition(tPtr
);
2648 tPtr
->flags
.waitingForSelection
= True
;
2656 if (tPtr
->flags
.pointerGrabbed
) {
2657 tPtr
->flags
.pointerGrabbed
= False
;
2658 XUngrabPointer(dpy
, CurrentTime
);
2662 if (tPtr
->flags
.waitingForSelection
)
2670 handleEvents(XEvent
*event
, void *data
)
2672 Text
*tPtr
= (Text
*)data
;
2674 switch(event
->type
) {
2677 if (event
->xexpose
.count
!=0)
2681 if (!(W_VIEW(tPtr
->hS
))->flags
.realized
)
2682 WMRealizeWidget(tPtr
->hS
);
2686 if (!(W_VIEW(tPtr
->vS
))->flags
.realized
)
2687 WMRealizeWidget(tPtr
->vS
);
2691 if (!(W_VIEW(tPtr
->ruler
))->flags
.realized
)
2692 WMRealizeWidget(tPtr
->ruler
);
2697 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
2703 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))
2706 tPtr
->flags
.focused
= True
;
2708 if (tPtr
->flags
.editable
&& !tPtr
->timerID
) {
2709 tPtr
->timerID
= WMAddTimerHandler(12+0*CURSOR_BLINK_ON_DELAY
,
2717 tPtr
->flags
.focused
= False
;
2720 if (tPtr
->timerID
) {
2721 WMDeleteTimerHandler(tPtr
->timerID
);
2722 tPtr
->timerID
= NULL
;
2731 XFreePixmap(tPtr
->view
->screen
->display
, tPtr
->db
);
2733 WMEmptyArray(tPtr
->gfxItems
);
2736 WMDeleteTimerHandler(tPtr
->timerID
);
2738 WMReleaseFont(tPtr
->dFont
);
2739 WMReleaseColor(tPtr
->dColor
);
2740 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
2741 WMRemoveNotificationObserver(tPtr
);
2752 insertPlainText(Text
*tPtr
, char *text
)
2759 mark
= strchr(start
, '\n');
2761 tb
= WMCreateTextBlockWithText(tPtr
,
2763 tPtr
->dColor
, tPtr
->flags
.first
, (int)(mark
-start
));
2765 tPtr
->flags
.first
= True
;
2767 if (start
&& strlen(start
)) {
2768 tb
= WMCreateTextBlockWithText(tPtr
, start
, tPtr
->dFont
,
2769 tPtr
->dColor
, tPtr
->flags
.first
, strlen(start
));
2771 tPtr
->flags
.first
= False
;
2775 if (tPtr
->flags
.prepend
)
2776 WMPrependTextBlock(tPtr
, tb
);
2778 WMAppendTextBlock(tPtr
, tb
);
2784 rulerMoveCallBack(WMWidget
*w
, void *self
)
2786 Text
*tPtr
= (Text
*)self
;
2790 if (W_CLASS(tPtr
) != WC_Text
)
2798 rulerReleaseCallBack(WMWidget
*w
, void *self
)
2800 Text
*tPtr
= (Text
*)self
;
2804 if (W_CLASS(tPtr
) != WC_Text
)
2812 draggingSourceOperation(WMView
*self
, Bool local
)
2814 return WDOperationCopy
;
2818 fetchDragData(WMView
*self
, char *type
)
2820 TextBlock
*tb
= ((WMText
*)self
->self
)->currentTextBlock
;
2827 printf("type is [%s]\n", type
);
2828 desc
= wmalloc(tb
->used
+1);
2829 memcpy(desc
, tb
->text
, tb
->used
);
2831 data
= WMCreateDataWithBytes(desc
, strlen(desc
)+1);
2839 static WMDragSourceProcs _DragSourceProcs
= {
2840 draggingSourceOperation
,
2848 draggingEntered(WMView
*self
, WMDraggingInfo
*info
)
2850 printf("draggingEntered\n");
2851 return WDOperationCopy
;
2856 draggingUpdated(WMView
*self
, WMDraggingInfo
*info
)
2858 return WDOperationCopy
;
2863 draggingExited(WMView
*self
, WMDraggingInfo
*info
)
2865 printf("draggingExited\n");
2869 prepareForDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2871 printf("prepareForDragOperation\n");
2879 receivedData(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
2880 void *cdata
, WMData
*data
)
2882 badbadbad
= wstrdup((char *)WMDataBytes(data
));
2886 /* when it's done in WINGs, remove this */
2889 requestDroppedData(WMView
*view
, WMDraggingInfo
*info
, char *type
)
2891 WMScreen
*scr
= W_VIEW_SCREEN(view
);
2893 if (!WMRequestSelection(scr
->dragInfo
.destView
,
2894 scr
->xdndSelectionAtom
,
2895 XInternAtom(scr
->display
, type
, False
),
2896 scr
->dragInfo
.timestamp
,
2897 receivedData
, &scr
->dragInfo
)) {
2898 wwarning("could not request data for dropped data");
2904 ev
.type
= ClientMessage
;
2905 ev
.xclient
.message_type
= scr
->xdndFinishedAtom
;
2906 ev
.xclient
.format
= 32;
2907 ev
.xclient
.window
= info
->destinationWindow
;
2908 ev
.xclient
.data
.l
[0] = 0;
2909 ev
.xclient
.data
.l
[1] = 0;
2910 ev
.xclient
.data
.l
[2] = 0;
2911 ev
.xclient
.data
.l
[3] = 0;
2912 ev
.xclient
.data
.l
[4] = 0;
2914 XSendEvent(scr
->display
, info
->sourceWindow
, False
, 0, &ev
);
2915 XFlush(scr
->display
);
2921 performDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2924 WMText
*tPtr
= (WMText
*)self
->self
;
2929 requestDroppedData(tPtr
->view
, info
, "application/X-color");
2930 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), badbadbad
, True
);
2932 WMSetTextSelectionColor(tPtr
, color
);
2933 WMReleaseColor(color
);
2942 concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
)
2944 printf("concludeDragOperation\n");
2948 static WMDragDestinationProcs _DragDestinationProcs
= {
2952 prepareForDragOperation
,
2953 performDragOperation
,
2954 concludeDragOperation
2959 getStream(WMText
*tPtr
, int sel
, int array
)
2961 TextBlock
*tb
= NULL
;
2963 unsigned long where
= 0;
2968 if (!(tb
= tPtr
->firstTextBlock
))
2972 (tPtr
->writer
) (tPtr
, (void *) text
);
2976 tb
= tPtr
->firstTextBlock
;
2979 if (!tb
->graphic
|| (tb
->graphic
&& !tPtr
->flags
.monoFont
)) {
2981 if (!sel
|| (tb
->graphic
&& tb
->selected
)) {
2983 if (!tPtr
->flags
.ignoreNewLine
&& (tb
->first
|| tb
->blank
)
2984 && tb
!= tPtr
->firstTextBlock
) {
2985 text
= wrealloc(text
, where
+1);
2986 text
[where
++] = '\n';
2992 if(tb
->graphic
&& array
) {
2993 text
= wrealloc(text
, where
+4);
2994 text
[where
++] = 0xFA;
2995 text
[where
++] = (tb
->used
>>8)&0x0ff;
2996 text
[where
++] = tb
->used
&0x0ff;
2997 text
[where
++] = tb
->allocated
; /* extra info */
2999 text
= wrealloc(text
, where
+tb
->used
);
3000 memcpy(&text
[where
], tb
->text
, tb
->used
);
3004 } else if (sel
&& tb
->selected
) {
3006 if (!tPtr
->flags
.ignoreNewLine
&& tb
->blank
) {
3007 text
= wrealloc(text
, where
+1);
3008 text
[where
++] = '\n';
3014 text
= wrealloc(text
, where
+(tb
->s_end
- tb
->s_begin
));
3015 memcpy(&text
[where
], &tb
->text
[tb
->s_begin
],
3016 tb
->s_end
- tb
->s_begin
);
3017 where
+= tb
->s_end
- tb
->s_begin
;
3022 _gSnext
:tb
= tb
->next
;
3025 /* +1 for the end of string, let's be nice */
3026 text
= wrealloc(text
, where
+1);
3033 releaseStreamObjects(void *data
)
3040 getStreamObjects(WMText
*tPtr
, int sel
)
3042 WMArray
*array
= WMCreateArrayWithDestructor(4, releaseStreamObjects
);
3046 char *start
, *fa
, *desc
;
3048 stream
= getStream(tPtr
, sel
, 1);
3055 fa
= strchr(start
, 0xFA);
3057 if((int)(fa
- start
)>0) {
3059 desc
[(int)(fa
- start
)] = 0;
3060 data
= WMCreateDataWithBytes((void *)desc
, (int)(fa
- start
));
3061 WMSetDataFormat(data
, TYPETEXT
);
3062 WMAddToArray(array
, (void *) data
);
3065 len
= *(fa
+1)*0xff + *(fa
+2);
3066 data
= WMCreateDataWithBytes((void *)(fa
+4), len
);
3067 WMSetDataFormat(data
, *(fa
+3));
3068 WMAddToArray(array
, (void *) data
);
3069 start
= fa
+ len
+ 4;
3072 if (start
&& strlen(start
)) {
3073 data
= WMCreateDataWithBytes((void *)start
, strlen(start
));
3074 WMSetDataFormat(data
, TYPETEXT
);
3075 WMAddToArray(array
, (void *) data
);
3087 WMCreateTextForDocumentType(WMWidget
*parent
, WMAction
*parser
, WMAction
*writer
)
3094 tPtr
= wmalloc(sizeof(Text
));
3095 memset(tPtr
, 0, sizeof(Text
));
3096 tPtr
->widgetClass
= WC_Text
;
3097 tPtr
->view
= W_CreateView(W_VIEW(parent
));
3099 perror("could not create text's view\n");
3104 dpy
= tPtr
->view
->screen
->display
;
3105 scr
= tPtr
->view
->screen
;
3107 tPtr
->view
->self
= tPtr
;
3108 tPtr
->view
->attribs
.cursor
= scr
->textCursor
;
3109 tPtr
->view
->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3110 W_ResizeView(tPtr
->view
, 250, 200);
3112 tPtr
->dColor
= WMWhiteColor(scr
);
3113 tPtr
->bgGC
= WMColorGC(tPtr
->dColor
);
3114 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->dColor
);
3115 WMReleaseColor(tPtr
->dColor
);
3117 tPtr
->dColor
= WMBlackColor(scr
);
3118 tPtr
->fgGC
= WMColorGC(tPtr
->dColor
);
3120 gcv
.graphics_exposures
= False
;
3121 gcv
.foreground
= W_PIXEL(scr
->gray
);
3122 gcv
.background
= W_PIXEL(scr
->darkGray
);
3123 gcv
.fill_style
= FillStippled
;
3124 gcv
.stipple
= XCreateBitmapFromData(dpy
, W_DRAWABLE(scr
),
3125 STIPPLE_BITS
, STIPPLE_WIDTH
, STIPPLE_HEIGHT
);
3126 tPtr
->stippledGC
= XCreateGC(dpy
, W_DRAWABLE(scr
),
3127 GCForeground
|GCBackground
|GCStipple
3128 |GCFillStyle
|GCGraphicsExposures
, &gcv
);
3134 tPtr
->dFont
= WMRetainFont(WMSystemFontOfSize(scr
, 12));
3136 tPtr
->view
->delegate
= &_TextViewDelegate
;
3138 tPtr
->delegate
= NULL
;
3141 tPtr
->timerID
= NULL
;
3144 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
3145 |EnterWindowMask
|LeaveWindowMask
|FocusChangeMask
,
3146 handleEvents
, tPtr
);
3148 WMCreateEventHandler(tPtr
->view
, ButtonReleaseMask
|ButtonPressMask
3149 |KeyReleaseMask
|KeyPressMask
|Button1MotionMask
,
3150 handleActionEvents
, tPtr
);
3152 WMAddNotificationObserver(ownershipObserver
, tPtr
,
3153 "_lostOwnership", tPtr
);
3155 WMSetViewDragSourceProcs(tPtr
->view
, &_DragSourceProcs
);
3156 WMSetViewDragDestinationProcs(tPtr
->view
, &_DragDestinationProcs
);
3160 char *types
[3] = {"application/X-color", "application/X-image", NULL
};
3161 WMRegisterViewForDraggedTypes(tPtr
->view
, types
);
3164 WMAddNotificationObserver(fontChanged
, tPtr
,
3165 "WMFontPanelDidChangeNotification", tPtr
);
3167 tPtr
->firstTextBlock
= NULL
;
3168 tPtr
->lastTextBlock
= NULL
;
3169 tPtr
->currentTextBlock
= NULL
;
3172 tPtr
->gfxItems
= WMCreateArray(4);
3174 tPtr
->parser
= parser
;
3175 tPtr
->writer
= writer
;
3177 tPtr
->sel
.x
= tPtr
->sel
.y
= 2;
3178 tPtr
->sel
.w
= tPtr
->sel
.h
= 0;
3180 tPtr
->clicked
.x
= tPtr
->clicked
.y
= 2;
3182 tPtr
->visible
.x
= tPtr
->visible
.y
= 2;
3183 tPtr
->visible
.h
= tPtr
->view
->size
.height
;
3184 tPtr
->visible
.w
= tPtr
->view
->size
.width
- 4;
3186 tPtr
->cursor
.x
= -23;
3189 tPtr
->docHeight
= 0;
3190 tPtr
->dBulletPix
= WMCreatePixmapFromXPMData(tPtr
->view
->screen
,
3192 tPtr
->db
= (Pixmap
) NULL
;
3193 tPtr
->bgPixmap
= NULL
;
3195 tPtr
->margins
= WMGetRulerMargins(NULL
);
3196 tPtr
->margins
->right
= tPtr
->visible
.w
;
3199 tPtr
->flags
.rulerShown
= False
;
3200 tPtr
->flags
.monoFont
= False
;
3201 tPtr
->flags
.focused
= False
;
3202 tPtr
->flags
.editable
= True
;
3203 tPtr
->flags
.ownsSelection
= False
;
3204 tPtr
->flags
.pointerGrabbed
= False
;
3205 tPtr
->flags
.extendSelection
= False
;
3206 tPtr
->flags
.frozen
= False
;
3207 tPtr
->flags
.cursorShown
= True
;
3208 tPtr
->flags
.acceptsGraphic
= False
;
3209 tPtr
->flags
.horizOnDemand
= False
;
3210 tPtr
->flags
.needsLayOut
= False
;
3211 tPtr
->flags
.ignoreNewLine
= False
;
3212 tPtr
->flags
.indentNewLine
= False
;
3213 tPtr
->flags
.laidOut
= False
;
3214 tPtr
->flags
.ownsSelection
= False
;
3215 tPtr
->flags
.waitingForSelection
= False
;
3216 tPtr
->flags
.prepend
= False
;
3217 tPtr
->flags
.isOverGraphic
= False
;
3218 tPtr
->flags
.relief
= WRSunken
;
3219 tPtr
->flags
.isOverGraphic
= 0;
3220 tPtr
->flags
.alignment
= WALeft
;
3221 tPtr
->flags
.first
= True
;
3227 WMPrependTextStream(WMText
*tPtr
, char *text
)
3229 CHECK_CLASS(tPtr
, WC_Text
);
3232 if (tPtr
->flags
.ownsSelection
)
3233 releaseSelection(tPtr
);
3235 updateScrollers(tPtr
);
3239 tPtr
->flags
.prepend
= True
;
3240 if (text
&& tPtr
->parser
)
3241 (tPtr
->parser
) (tPtr
, (void *) text
);
3243 insertPlainText(tPtr
, text
);
3245 tPtr
->flags
.needsLayOut
= True
;
3247 if(!tPtr
->flags
.frozen
) {
3248 layOutDocument(tPtr
);
3254 WMAppendTextStream(WMText
*tPtr
, char *text
)
3256 CHECK_CLASS(tPtr
, WC_Text
);
3259 if (tPtr
->flags
.ownsSelection
)
3260 releaseSelection(tPtr
);
3262 updateScrollers(tPtr
);
3266 tPtr
->flags
.prepend
= False
;
3267 if (text
&& tPtr
->parser
)
3268 (tPtr
->parser
) (tPtr
, (void *) text
);
3270 insertPlainText(tPtr
, text
);
3272 tPtr
->flags
.needsLayOut
= True
;
3273 if(tPtr
->currentTextBlock
) {
3274 if(tPtr
->currentTextBlock
->graphic
)
3277 tPtr
->tpos
= tPtr
->currentTextBlock
->used
;
3280 if(!tPtr
->flags
.frozen
) {
3281 layOutDocument(tPtr
);
3287 WMGetTextStream(WMText
*tPtr
)
3289 CHECK_CLASS(tPtr
, WC_Text
);
3290 return getStream(tPtr
, 0, 0);
3294 WMGetTextSelectedStream(WMText
*tPtr
)
3296 CHECK_CLASS(tPtr
, WC_Text
);
3297 return getStream(tPtr
, 1, 0);
3301 WMGetTextObjects(WMText
*tPtr
)
3303 CHECK_CLASS(tPtr
, WC_Text
);
3304 return getStreamObjects(tPtr
, 0);
3308 WMGetTextSelectedObjects(WMText
*tPtr
)
3310 CHECK_CLASS(tPtr
, WC_Text
);
3311 return getStreamObjects(tPtr
, 1);
3316 WMSetTextDelegate(WMText
*tPtr
, WMTextDelegate
*delegate
)
3318 CHECK_CLASS(tPtr
, WC_Text
);
3320 tPtr
->delegate
= delegate
;
3325 WMCreateTextBlockWithObject(WMText
*tPtr
, WMWidget
*w
,
3326 char *description
, WMColor
*color
,
3327 unsigned short first
, unsigned short extraInfo
)
3331 if (!w
|| !description
|| !color
)
3334 tb
= wmalloc(sizeof(TextBlock
));
3338 tb
->text
= wstrdup(description
);
3339 tb
->used
= strlen(description
);
3342 tb
->color
= WMRetainColor(color
);
3343 tb
->marginN
= newMargin(tPtr
, NULL
);
3344 tb
->allocated
= extraInfo
;
3349 tb
->underlined
= False
;
3350 tb
->selected
= False
;
3352 tb
->sections
= NULL
;
3362 WMCreateTextBlockWithPixmap(WMText
*tPtr
, WMPixmap
*p
,
3363 char *description
, WMColor
*color
,
3364 unsigned short first
, unsigned short extraInfo
)
3368 if (!p
|| !description
|| !color
)
3371 tb
= wmalloc(sizeof(TextBlock
));
3375 tb
->text
= wstrdup(description
);
3376 tb
->used
= strlen(description
);
3378 tb
->d
.pixmap
= WMRetainPixmap(p
);
3379 tb
->color
= WMRetainColor(color
);
3380 tb
->marginN
= newMargin(tPtr
, NULL
);
3381 tb
->allocated
= extraInfo
;
3386 tb
->underlined
= False
;
3387 tb
->selected
= False
;
3389 tb
->sections
= NULL
;
3399 WMCreateTextBlockWithText(WMText
*tPtr
, char *text
, WMFont
*font
, WMColor
*color
,
3400 unsigned short first
, unsigned short len
)
3404 if (!font
|| !color
)
3407 tb
= wmalloc(sizeof(TextBlock
));
3411 tb
->allocated
= reqBlockSize(len
);
3412 tb
->text
= (char *)wmalloc(tb
->allocated
);
3413 memset(tb
->text
, 0, tb
->allocated
);
3415 if (len
< 1|| !text
|| (*text
== '\n' && len
==1 )) {
3420 memcpy(tb
->text
, text
, len
);
3424 tb
->text
[tb
->used
] = 0;
3426 tb
->d
.font
= WMRetainFont(font
);
3427 tb
->color
= WMRetainColor(color
);
3428 tb
->marginN
= newMargin(tPtr
, NULL
);
3431 tb
->graphic
= False
;
3432 tb
->underlined
= False
;
3433 tb
->selected
= False
;
3435 tb
->sections
= NULL
;
3443 WMSetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int first
,
3444 unsigned int kanji
, unsigned int underlined
, int script
,
3445 WMRulerMargins
*margins
)
3447 TextBlock
*tb
= (TextBlock
*) vtb
;
3453 tb
->underlined
= underlined
;
3454 tb
->script
= script
;
3455 tb
->marginN
= newMargin(tPtr
, margins
);
3459 WMGetTextBlockProperties(WMText
*tPtr
, void *vtb
, unsigned int *first
,
3460 unsigned int *kanji
, unsigned int *underlined
, int *script
,
3461 WMRulerMargins
*margins
)
3463 TextBlock
*tb
= (TextBlock
*) vtb
;
3467 if (first
) *first
= tb
->first
;
3468 if (kanji
) *kanji
= tb
->kanji
;
3469 if (underlined
) *underlined
= tb
->underlined
;
3470 if (script
) *script
= tb
->script
;
3471 if (margins
) margins
= &tPtr
->margins
[tb
->marginN
];
3477 WMPrependTextBlock(WMText
*tPtr
, void *vtb
)
3479 TextBlock
*tb
= (TextBlock
*)vtb
;
3486 WMWidget
*w
= tb
->d
.widget
;
3487 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3488 (W_VIEW(w
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3489 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3492 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3498 tPtr
->tpos
= tb
->used
;
3501 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3502 tb
->next
= tb
->prior
= NULL
;
3504 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3505 = tPtr
->currentTextBlock
= tb
;
3510 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3513 tb
->next
= tPtr
->currentTextBlock
;
3514 tb
->prior
= tPtr
->currentTextBlock
->prior
;
3515 if (tPtr
->currentTextBlock
->prior
)
3516 tPtr
->currentTextBlock
->prior
->next
= tb
;
3518 tPtr
->currentTextBlock
->prior
= tb
;
3520 tPtr
->firstTextBlock
= tb
;
3522 tPtr
->currentTextBlock
= tb
;
3527 WMAppendTextBlock(WMText
*tPtr
, void *vtb
)
3529 TextBlock
*tb
= (TextBlock
*)vtb
;
3536 WMWidget
*w
= tb
->d
.widget
;
3537 if (W_CLASS(w
) != WC_TextField
&& W_CLASS(w
) != WC_Text
) {
3538 (W_VIEW(w
))->attribs
.cursor
=
3539 tPtr
->view
->screen
->defaultCursor
;
3540 (W_VIEW(w
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3543 WMAddToArray(tPtr
->gfxItems
, (void *)tb
);
3550 tPtr
->tpos
= tb
->used
;
3554 if (!tPtr
->lastTextBlock
|| !tPtr
->firstTextBlock
) {
3555 tb
->next
= tb
->prior
= NULL
;
3557 tPtr
->lastTextBlock
= tPtr
->firstTextBlock
3558 = tPtr
->currentTextBlock
= tb
;
3563 tb
->marginN
= tPtr
->currentTextBlock
->marginN
;
3566 tb
->next
= tPtr
->currentTextBlock
->next
;
3567 tb
->prior
= tPtr
->currentTextBlock
;
3568 if (tPtr
->currentTextBlock
->next
)
3569 tPtr
->currentTextBlock
->next
->prior
= tb
;
3571 tPtr
->currentTextBlock
->next
= tb
;
3574 tPtr
->lastTextBlock
= tb
;
3576 tPtr
->currentTextBlock
= tb
;
3580 WMRemoveTextBlock(WMText
*tPtr
)
3582 TextBlock
*tb
= NULL
;
3584 if (!tPtr
|| !tPtr
->firstTextBlock
|| !tPtr
->lastTextBlock
3585 || !tPtr
->currentTextBlock
) {
3589 tb
= tPtr
->currentTextBlock
;
3591 WMRemoveFromArray(tPtr
->gfxItems
, (void *)tb
);
3594 WMUnmapWidget(tb
->d
.widget
);
3598 if (tPtr
->currentTextBlock
== tPtr
->firstTextBlock
) {
3599 if (tPtr
->currentTextBlock
->next
)
3600 tPtr
->currentTextBlock
->next
->prior
= NULL
;
3602 tPtr
->firstTextBlock
= tPtr
->currentTextBlock
->next
;
3603 tPtr
->currentTextBlock
= tPtr
->firstTextBlock
;
3605 } else if (tPtr
->currentTextBlock
== tPtr
->lastTextBlock
) {
3606 tPtr
->currentTextBlock
->prior
->next
= NULL
;
3607 tPtr
->lastTextBlock
= tPtr
->currentTextBlock
->prior
;
3608 tPtr
->currentTextBlock
= tPtr
->lastTextBlock
;
3610 tPtr
->currentTextBlock
->prior
->next
= tPtr
->currentTextBlock
->next
;
3611 tPtr
->currentTextBlock
->next
->prior
= tPtr
->currentTextBlock
->prior
;
3612 tPtr
->currentTextBlock
= tPtr
->currentTextBlock
->next
;
3620 destroyWidget(WMWidget
*widget
)
3625 WMDestroyWidget(widget
);
3630 WMDestroyTextBlock(WMText
*tPtr
, void *vtb
)
3632 TextBlock
*tb
= (TextBlock
*)vtb
;
3638 /* naturally, there's a danger to destroying widgets whose action
3639 brings us here: ie. press a button to destroy it...
3640 need to find a safer way. till then... this stays commented out */
3641 /* 5 months later... destroy it 10 seconds after now which should be
3642 enough time for the widget's action to be completed... :-) */
3643 WMAddTimerHandler(10000, destroyWidget
, (void *)tb
->d
.widget
);
3644 tb
->d
.widget
= NULL
;
3647 WMReleasePixmap(tb
->d
.pixmap
);
3648 tb
->d
.pixmap
= NULL
;
3651 WMReleaseFont(tb
->d
.font
);
3654 WMReleaseColor(tb
->color
);
3655 if (tb
->sections
&& tb
->nsections
> 0)
3656 wfree(tb
->sections
);
3665 WMSetTextForegroundColor(WMText
*tPtr
, WMColor
*color
)
3671 tPtr
->fgGC
= WMColorGC(color
);
3673 WMColor
*color
= WMBlackColor(tPtr
->view
->screen
);
3674 tPtr
->fgGC
= WMColorGC(color
);
3675 WMReleaseColor(color
);
3682 WMSetTextBackgroundColor(WMText
*tPtr
, WMColor
*color
)
3688 tPtr
->bgGC
= WMColorGC(color
);
3689 W_SetViewBackgroundColor(tPtr
->view
, color
);
3691 tPtr
->bgGC
= WMColorGC(WMWhiteColor(tPtr
->view
->screen
));
3692 W_SetViewBackgroundColor(tPtr
->view
,
3693 WMWhiteColor(tPtr
->view
->screen
));
3699 void WMSetTextBackgroundPixmap(WMText
*tPtr
, WMPixmap
*pixmap
)
3705 WMReleasePixmap(tPtr
->bgPixmap
);
3708 tPtr
->bgPixmap
= WMRetainPixmap(pixmap
);
3710 tPtr
->bgPixmap
= NULL
;
3714 WMSetTextRelief(WMText
*tPtr
, WMReliefType relief
)
3718 tPtr
->flags
.relief
= relief
;
3719 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3723 WMSetTextHasHorizontalScroller(WMText
*tPtr
, Bool shouldhave
)
3728 if (shouldhave
&& !tPtr
->hS
) {
3729 tPtr
->hS
= WMCreateScroller(tPtr
);
3730 (W_VIEW(tPtr
->hS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3731 (W_VIEW(tPtr
->hS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3732 WMSetScrollerArrowsPosition(tPtr
->hS
, WSAMinEnd
);
3733 WMSetScrollerAction(tPtr
->hS
, scrollersCallBack
, tPtr
);
3734 WMMapWidget(tPtr
->hS
);
3735 } else if (!shouldhave
&& tPtr
->hS
) {
3736 WMUnmapWidget(tPtr
->hS
);
3737 WMDestroyWidget(tPtr
->hS
);
3743 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3748 WMSetTextHasRuler(WMText
*tPtr
, Bool shouldhave
)
3753 if(shouldhave
&& !tPtr
->ruler
) {
3754 tPtr
->ruler
= WMCreateRuler(tPtr
);
3755 (W_VIEW(tPtr
->ruler
))->attribs
.cursor
=
3756 tPtr
->view
->screen
->defaultCursor
;
3757 (W_VIEW(tPtr
->ruler
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3758 WMSetRulerReleaseAction(tPtr
->ruler
, rulerReleaseCallBack
, tPtr
);
3759 WMSetRulerMoveAction(tPtr
->ruler
, rulerMoveCallBack
, tPtr
);
3760 } else if(!shouldhave
&& tPtr
->ruler
) {
3761 WMShowTextRuler(tPtr
, False
);
3762 WMDestroyWidget(tPtr
->ruler
);
3765 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3769 WMShowTextRuler(WMText
*tPtr
, Bool show
)
3776 if(tPtr
->flags
.monoFont
)
3779 tPtr
->flags
.rulerShown
= show
;
3781 WMMapWidget(tPtr
->ruler
);
3783 WMUnmapWidget(tPtr
->ruler
);
3786 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3790 WMGetTextRulerShown(WMText
*tPtr
)
3798 return tPtr
->flags
.rulerShown
;
3803 WMSetTextHasVerticalScroller(WMText
*tPtr
, Bool shouldhave
)
3808 if (shouldhave
&& !tPtr
->vS
) {
3809 tPtr
->vS
= WMCreateScroller(tPtr
);
3810 (W_VIEW(tPtr
->vS
))->attribs
.cursor
= tPtr
->view
->screen
->defaultCursor
;
3811 (W_VIEW(tPtr
->vS
))->attribFlags
|= CWOverrideRedirect
| CWCursor
;
3812 WMSetScrollerArrowsPosition(tPtr
->vS
, WSAMaxEnd
);
3813 WMSetScrollerAction(tPtr
->vS
, scrollersCallBack
, tPtr
);
3814 WMMapWidget(tPtr
->vS
);
3815 } else if (!shouldhave
&& tPtr
->vS
) {
3816 WMUnmapWidget(tPtr
->vS
);
3817 WMDestroyWidget(tPtr
->vS
);
3823 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3829 WMScrollText(WMText
*tPtr
, int amount
)
3834 if (amount
== 0 || !tPtr
->view
->flags
.realized
)
3838 if (tPtr
->vpos
> 0) {
3839 if (tPtr
->vpos
> abs(amount
)) tPtr
->vpos
+= amount
;
3844 int limit
= tPtr
->docHeight
- tPtr
->visible
.h
;
3845 if (tPtr
->vpos
< limit
) {
3846 if (tPtr
->vpos
< limit
-amount
) tPtr
->vpos
+= amount
;
3847 else tPtr
->vpos
= limit
;
3852 if (scroll
&& tPtr
->vpos
!= tPtr
->prevVpos
) {
3853 updateScrollers(tPtr
);
3856 tPtr
->prevVpos
= tPtr
->vpos
;
3861 WMPageText(WMText
*tPtr
, Bool direction
)
3863 if (!tPtr
) return False
;
3864 if (!tPtr
->view
->flags
.realized
) return False
;
3866 return WMScrollText(tPtr
, direction
?tPtr
->visible
.h
:-tPtr
->visible
.h
);
3870 WMSetTextEditable(WMText
*tPtr
, Bool editable
)
3874 tPtr
->flags
.editable
= editable
;
3878 WMGetTextEditable(WMText
*tPtr
)
3882 return tPtr
->flags
.editable
;
3886 WMSetTextIndentNewLines(WMText
*tPtr
, Bool indent
)
3890 tPtr
->flags
.indentNewLine
= indent
;
3894 WMSetTextIgnoresNewline(WMText
*tPtr
, Bool ignore
)
3898 tPtr
->flags
.ignoreNewLine
= ignore
;
3902 WMGetTextIgnoresNewline(WMText
*tPtr
)
3906 return tPtr
->flags
.ignoreNewLine
;
3910 WMSetTextUsesMonoFont(WMText
*tPtr
, Bool mono
)
3916 if(tPtr
->flags
.rulerShown
)
3917 WMShowTextRuler(tPtr
, False
);
3918 if(tPtr
->flags
.alignment
!= WALeft
)
3919 tPtr
->flags
.alignment
= WALeft
;
3922 tPtr
->flags
.monoFont
= mono
;
3923 textDidResize(tPtr
->view
->delegate
, tPtr
->view
);
3927 WMGetTextUsesMonoFont(WMText
*tPtr
)
3931 return tPtr
->flags
.monoFont
;
3936 WMSetTextDefaultFont(WMText
*tPtr
, WMFont
*font
)
3941 WMReleaseFont(tPtr
->dFont
);
3943 tPtr
->dFont
= WMRetainFont(font
);
3945 tPtr
->dFont
= WMRetainFont(WMSystemFontOfSize(tPtr
->view
->screen
, 12));
3949 WMGetTextDefaultFont(WMText
*tPtr
)
3954 return WMRetainFont(tPtr
->dFont
);
3958 WMSetTextDefaultColor(WMText
*tPtr
, WMColor
*color
)
3963 WMReleaseColor(tPtr
->dColor
);
3965 tPtr
->dColor
= WMRetainColor(color
);
3967 tPtr
->dColor
= WMBlackColor(tPtr
->view
->screen
);
3971 WMGetTextDefaultColor(WMText
*tPtr
)
3976 return WMRetainColor(tPtr
->dColor
);
3980 WMSetTextAlignment(WMText
*tPtr
, WMAlignment alignment
)
3984 if(tPtr
->flags
.monoFont
)
3985 tPtr
->flags
.alignment
= WALeft
;
3987 tPtr
->flags
.alignment
= alignment
;
3992 WMGetTextInsertType(WMText
*tPtr
)
3996 return tPtr
->flags
.prepend
;
4001 WMSetTextSelectionColor(WMText
*tPtr
, WMColor
*color
)
4003 if (!tPtr
|| !color
)
4006 setSelectionProperty(tPtr
, NULL
, color
, -1);
4010 WMGetTextSelectionColor(WMText
*tPtr
)
4017 tb
= tPtr
->currentTextBlock
;
4019 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4030 WMSetTextSelectionFont(WMText
*tPtr
, WMFont
*font
)
4035 setSelectionProperty(tPtr
, font
, NULL
, -1) ;
4039 WMGetTextSelectionFont(WMText
*tPtr
)
4046 tb
= tPtr
->currentTextBlock
;
4048 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4055 tb
= getFirstNonGraphicBlockFor(tb
, 1);
4059 return (tb
->selected
? tb
->d
.font
: NULL
);
4064 WMSetTextSelectionUnderlined(WMText
*tPtr
, int underlined
)
4066 if (!tPtr
|| (underlined
!=0 && underlined
!=1))
4069 setSelectionProperty(tPtr
, NULL
, NULL
, underlined
);
4074 WMGetTextSelectionUnderlined(WMText
*tPtr
)
4081 tb
= tPtr
->currentTextBlock
;
4083 if (!tb
|| !tPtr
->flags
.ownsSelection
)
4089 return tb
->underlined
;
4094 WMFreezeText(WMText
*tPtr
)
4099 tPtr
->flags
.frozen
= True
;
4104 WMThawText(WMText
*tPtr
)
4109 tPtr
->flags
.frozen
= False
;
4111 if(tPtr
->flags
.monoFont
) {
4112 int j
, c
= WMGetArrayItemCount(tPtr
->gfxItems
);
4115 /* make sure to unmap widgets no matter where they are */
4116 /* they'll be later remapped if needed by paintText */
4117 for(j
=0; j
<c
; j
++) {
4118 if ((tb
= (TextBlock
*) WMGetFromArray(tPtr
->gfxItems
, j
))) {
4119 if (tb
->object
&& ((W_VIEW(tb
->d
.widget
))->flags
.mapped
))
4120 WMUnmapWidget(tb
->d
.widget
);
4126 tPtr
->flags
.laidOut
= False
;
4127 layOutDocument(tPtr
);
4128 updateScrollers(tPtr
);
4130 tPtr
->flags
.needsLayOut
= False
;
4134 /* find first occurence of a string */
4136 mystrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4141 if(!haystack
|| !needle
|| !end
)
4144 for (ptr
= haystack
; ptr
< end
; ptr
++) {
4146 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4150 if (tolower(*ptr
) == tolower(*needle
) &&
4151 !strncasecmp(ptr
, needle
, len
))
4159 /* find last occurence of a string */
4161 mystrrstr(char *haystack
, char *needle
, unsigned short len
, char *end
,
4166 if(!haystack
|| !needle
|| !end
)
4169 for (ptr
= haystack
-2; ptr
> end
; ptr
--) {
4171 if (*ptr
== *needle
&& !strncmp(ptr
, needle
, len
))
4174 if (tolower(*ptr
) == tolower(*needle
) &&
4175 !strncasecmp(ptr
, needle
, len
))
4185 WMFindInTextStream(WMText
*tPtr
, char *needle
, Bool direction
,
4192 if (!tPtr
|| !needle
)
4196 if (! (tb
= tPtr
->currentTextBlock
)) {
4197 if (! (tb
= ( (direction
> 0) ?
4198 tPtr
->firstTextBlock
: tPtr
->lastTextBlock
) ) ){
4202 /* if(tb != ((direction>0) ?tPtr->firstTextBlock : tPtr->lastTextBlock))
4203 tb = (direction>0) ? tb->next : tb->prior; */
4204 if(tb
!= tPtr
->lastTextBlock
)
4208 tb
= tPtr
->currentTextBlock
;
4216 if(pos
+1 < tb
->used
)
4219 if(tb
->used
- pos
> 0 && pos
> 0) {
4220 mark
= mystrstr(&tb
->text
[pos
], needle
,
4221 strlen(needle
), &tb
->text
[tb
->used
], caseSensitive
);
4234 mark
= mystrrstr(&tb
->text
[pos
], needle
,
4235 strlen(needle
), tb
->text
, caseSensitive
);
4247 WMFont
*font
= tPtr
->flags
.monoFont
?tPtr
->dFont
:tb
->d
.font
;
4249 tPtr
->tpos
= (int)(mark
- tb
->text
);
4250 tPtr
->currentTextBlock
= tb
;
4251 updateCursorPosition(tPtr
);
4252 tPtr
->sel
.y
= tPtr
->cursor
.y
+5;
4253 tPtr
->sel
.h
= tPtr
->cursor
.h
-10;
4254 tPtr
->sel
.x
= tPtr
->cursor
.x
+1;
4255 tPtr
->sel
.w
= WMIN(WMWidthOfString(font
,
4256 &tb
->text
[tPtr
->tpos
], strlen(needle
)),
4257 tPtr
->docWidth
- tPtr
->sel
.x
);
4258 tPtr
->flags
.ownsSelection
= True
;
4265 tb
= (direction
>0) ? tb
->next
: tb
->prior
;
4267 pos
= (direction
>0) ? 0 : tb
->used
;
4276 WMReplaceTextSelection(WMText
*tPtr
, char *replacement
)
4281 if (!tPtr
->flags
.ownsSelection
)
4284 removeSelection(tPtr
);
4287 insertTextInteractively(tPtr
, replacement
, strlen(replacement
));
4288 updateCursorPosition(tPtr
);