1 /* $XConsortium: AsciiSink.c,v 1.62 94/04/17 20:11:41 kaleb Exp $ */
3 /***********************************************************
5 Copyright (c) 1987, 1988, 1994 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
29 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
33 Permission to use, copy, modify, and distribute this software and its
34 documentation for any purpose and without fee is hereby granted,
35 provided that the above copyright notice appear in all copies and that
36 both that copyright notice and this permission notice appear in
37 supporting documentation, and that the name of Digital not be
38 used in advertising or publicity pertaining to distribution of the
39 software without specific, written prior permission.
41 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
42 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
43 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
44 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
45 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
46 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 ******************************************************************/
53 #include <X11/Xatom.h>
54 #include <X11/IntrinsicP.h>
55 #include <X11/StringDefs.h>
56 #include <X11/Xaw/XawInit.h>
57 #include "AsciiSinkP.h"
58 #include <X11/Xaw/AsciiSrcP.h> /* For source function defs. */
59 #include <X11/Xaw/TextP.h> /* I also reach into the text widget. */
62 #undef GETLASTPOS /* We will use our own GETLASTPOS. */
65 #define GETLASTPOS XawTextSourceScan(source, (XawTextPosition) 0, XawstAll, XawsdRight, 1, TRUE)
67 static void Initialize(), Destroy();
68 static Boolean
SetValues();
69 static int MaxLines(), MaxHeight();
70 static void SetTabs();
72 static void DisplayText(), InsertCursor(), FindPosition();
73 static void FindDistance(), Resolve(), GetCursorBounds();
75 #define offset(field) XtOffsetOf(AsciiSinkRec, ascii_sink.field)
77 static XtResource resources
[] = {
78 {XtNfont
, XtCFont
, XtRFontStruct
, sizeof (XFontStruct
*),
79 offset(font
), XtRString
, XtDefaultFont
},
80 {XtNecho
, XtCOutput
, XtRBoolean
, sizeof(Boolean
),
81 offset(echo
), XtRImmediate
, (XtPointer
) True
},
82 {XtNdisplayNonprinting
, XtCOutput
, XtRBoolean
, sizeof(Boolean
),
83 offset(display_nonprinting
), XtRImmediate
, (XtPointer
) True
},
87 #define SuperClass (&textSinkClassRec)
88 AsciiSinkClassRec asciiSinkClassRec
= {
90 /* core_class fields */
91 /* superclass */ (WidgetClass
) SuperClass
,
92 /* class_name */ "AsciiSink",
93 /* widget_size */ sizeof(AsciiSinkRec
),
94 /* class_initialize */ XawInitializeWidgetSet
,
95 /* class_part_initialize */ NULL
,
96 /* class_inited */ FALSE
,
97 /* initialize */ Initialize
,
98 /* initialize_hook */ NULL
,
102 /* resources */ resources
,
103 /* num_resources */ XtNumber(resources
),
104 /* xrm_class */ NULLQUARK
,
109 /* destroy */ Destroy
,
112 /* set_values */ SetValues
,
113 /* set_values_hook */ NULL
,
115 /* get_values_hook */ NULL
,
117 /* version */ XtVersion
,
118 /* callback_private */ NULL
,
124 /* text_sink_class fields */
126 /* DisplayText */ DisplayText
,
127 /* InsertCursor */ InsertCursor
,
128 /* ClearToBackground */ XtInheritClearToBackground
,
129 /* FindPosition */ FindPosition
,
130 /* FindDistance */ FindDistance
,
131 /* Resolve */ Resolve
,
132 /* MaxLines */ MaxLines
,
133 /* MaxHeight */ MaxHeight
,
134 /* SetTabs */ SetTabs
,
135 /* GetCursorBounds */ GetCursorBounds
137 /* ascii_sink_class fields */
143 WidgetClass asciiSinkObjectClass
= (WidgetClass
)&asciiSinkClassRec
;
153 int i
, width
, nonPrinting
;
154 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
155 XFontStruct
*font
= sink
->ascii_sink
.font
;
158 if ( c
== XawLF
) return(0);
161 /* Adjust for Left Margin. */
162 x
-= ((TextWidget
) XtParent(w
))->text
.margin
.left
;
164 if (x
>= (int)XtParent(w
)->core
.width
) return 0;
165 for (i
= 0, tab
= sink
->text_sink
.tabs
;
166 i
< sink
->text_sink
.tab_count
; i
++, tab
++) {
168 if (*tab
< (int)XtParent(w
)->core
.width
)
177 if ( (nonPrinting
= (c
< (unsigned char) XawSP
)) )
178 if (sink
->ascii_sink
.display_nonprinting
)
185 if (font
->per_char
&&
186 (c
>= font
->min_char_or_byte2
&& c
<= font
->max_char_or_byte2
))
187 width
= font
->per_char
[c
- font
->min_char_or_byte2
].width
;
189 width
= font
->min_bounds
.width
;
192 width
+= CharWidth(w
, x
, (unsigned char) '^');
197 /* Function Name: PaintText
198 * Description: Actually paints the text into the windoe.
199 * Arguments: w - the text widget.
200 * gc - gc to paint text with.
201 * x, y - location to paint the text.
202 * buf, len - buffer and length of text to paint.
203 * Returns: the width of the text painted, or 0.
205 * NOTE: If this string attempts to paint past the end of the window
206 * then this function will return zero.
210 PaintText(w
, gc
, x
, y
, buf
, len
)
217 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
218 TextWidget ctx
= (TextWidget
) XtParent(w
);
221 Dimension width
= XTextWidth(sink
->ascii_sink
.font
, (char *) buf
, len
);
222 max_x
= (Position
) ctx
->core
.width
;
224 if ( ((int) width
) <= -x
) /* Don't draw if we can't see it. */
227 XDrawImageString(XtDisplay(ctx
), XtWindow(ctx
), gc
,
228 (int) x
, (int) y
, (char *) buf
, len
);
229 if ( (((Position
) width
+ x
) > max_x
) && (ctx
->text
.margin
.right
!= 0) ) {
230 x
= ctx
->core
.width
- ctx
->text
.margin
.right
;
231 width
= ctx
->text
.margin
.right
;
232 XFillRectangle(XtDisplay((Widget
) ctx
), XtWindow( (Widget
) ctx
),
233 sink
->ascii_sink
.normgc
, (int) x
,
234 (int) y
- sink
->ascii_sink
.font
->ascent
,
235 (unsigned int) width
,
236 (unsigned int) (sink
->ascii_sink
.font
->ascent
+
237 sink
->ascii_sink
.font
->descent
));
243 /* Sink Object Functions */
246 * This function does not know about drawing more than one line of text.
250 DisplayText(w
, x
, y
, pos1
, pos2
, highlight
)
254 XawTextPosition pos1
, pos2
;
256 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
257 Widget source
= XawTextGetSource(XtParent(w
));
258 unsigned char buf
[BUFSIZ
];
262 GC gc
= highlight
? sink
->ascii_sink
.invgc
: sink
->ascii_sink
.normgc
;
263 GC invgc
= highlight
? sink
->ascii_sink
.normgc
: sink
->ascii_sink
.invgc
;
265 if (!sink
->ascii_sink
.echo
) return;
267 y
+= sink
->ascii_sink
.font
->ascent
;
268 for ( j
= 0 ; pos1
< pos2
; ) {
269 pos1
= XawTextSourceRead(source
, pos1
, &blk
, (int)(pos2
- pos1
));
270 for (k
= 0; k
< blk
.length
; k
++) {
271 if (j
>= BUFSIZ
) { /* buffer full, dump the text. */
272 x
+= PaintText(w
, gc
, x
, y
, buf
, j
);
276 if (buf
[j
] == XawLF
) /* line feeds ('\n') are not printed. */
279 else if (buf
[j
] == '\t') {
283 if ((j
!= 0) && ((temp
= PaintText(w
, gc
, x
, y
, buf
, j
)) == 0))
287 width
= CharWidth(w
, x
, (unsigned char) '\t');
288 XFillRectangle(XtDisplayOfObject(w
), XtWindowOfObject(w
),
290 (int) y
- sink
->ascii_sink
.font
->ascent
,
291 (unsigned int) width
,
292 (unsigned int) (sink
->ascii_sink
.font
->ascent
+
293 sink
->ascii_sink
.font
->descent
));
297 else if ( buf
[j
] < (unsigned char) ' ' ) {
298 if (sink
->ascii_sink
.display_nonprinting
) {
299 buf
[j
+ 1] = buf
[j
] + '@';
310 (void) PaintText(w
, gc
, x
, y
, buf
, j
);
313 #define insertCursor_width 6
314 #define insertCursor_height 3
315 static char insertCursor_bits
[] = {0x0c, 0x1e, 0x33};
318 CreateInsertCursor(s
)
321 return (XCreateBitmapFromData (DisplayOfScreen(s
), RootWindowOfScreen(s
),
322 insertCursor_bits
, insertCursor_width
, insertCursor_height
));
325 /* Function Name: GetCursorBounds
326 * Description: Returns the size and location of the cursor.
327 * Arguments: w - the text object.
328 * RETURNED rect - an X rectangle to return the cursor bounds in.
333 GetCursorBounds(w
, rect
)
337 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
339 rect
->width
= (unsigned short) insertCursor_width
;
340 rect
->height
= (unsigned short) insertCursor_height
;
341 rect
->x
= sink
->ascii_sink
.cursor_x
- (short) (rect
->width
/ 2);
342 rect
->y
= sink
->ascii_sink
.cursor_y
- (short) rect
->height
;
346 * The following procedure manages the "insert" cursor.
350 InsertCursor (w
, x
, y
, state
)
353 XawTextInsertState state
;
355 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
356 Widget text_widget
= XtParent(w
);
359 sink
->ascii_sink
.cursor_x
= x
;
360 sink
->ascii_sink
.cursor_y
= y
;
362 GetCursorBounds(w
, &rect
);
363 if (state
!= sink
->ascii_sink
.laststate
&& XtIsRealized(text_widget
))
364 XCopyPlane(XtDisplay(text_widget
),
365 sink
->ascii_sink
.insertCursorOn
,
366 XtWindow(text_widget
), sink
->ascii_sink
.xorgc
,
367 0, 0, (unsigned int) rect
.width
, (unsigned int) rect
.height
,
368 (int) rect
.x
, (int) rect
.y
, 1);
369 sink
->ascii_sink
.laststate
= state
;
373 * Given two positions, find the distance between them.
377 FindDistance (w
, fromPos
, fromx
, toPos
, resWidth
, resPos
, resHeight
)
379 XawTextPosition fromPos
; /* First position. */
380 int fromx
; /* Horizontal location of first position. */
381 XawTextPosition toPos
; /* Second position. */
382 int *resWidth
; /* Distance between fromPos and resPos. */
383 XawTextPosition
*resPos
; /* Actual second position used. */
384 int *resHeight
; /* Height required. */
386 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
387 Widget source
= XawTextGetSource(XtParent(w
));
389 XawTextPosition index
, lastPos
;
393 /* we may not need this */
394 lastPos
= GETLASTPOS
;
395 XawTextSourceRead(source
, fromPos
, &blk
, (int)(toPos
- fromPos
));
397 for (index
= fromPos
; index
!= toPos
&& index
< lastPos
; index
++) {
398 if ((int)index
- blk
.firstPos
>= blk
.length
)
399 XawTextSourceRead(source
, index
, &blk
, (int)(toPos
- fromPos
));
400 c
= blk
.ptr
[index
- blk
.firstPos
];
401 *resWidth
+= CharWidth(w
, fromx
+ *resWidth
, c
);
408 *resHeight
= sink
->ascii_sink
.font
->ascent
+sink
->ascii_sink
.font
->descent
;
413 FindPosition(w
, fromPos
, fromx
, width
, stopAtWordBreak
,
414 resPos
, resWidth
, resHeight
)
416 XawTextPosition fromPos
; /* Starting position. */
417 int fromx
; /* Horizontal location of starting position.*/
418 int width
; /* Desired width. */
419 int stopAtWordBreak
; /* Whether the resulting position should be at
421 XawTextPosition
*resPos
; /* Resulting position. */
422 int *resWidth
; /* Actual width used. */
423 int *resHeight
; /* Height required. */
425 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
426 Widget source
= XawTextGetSource(XtParent(w
));
428 XawTextPosition lastPos
, index
, whiteSpacePosition
;
429 int lastWidth
, whiteSpaceWidth
;
430 Boolean whiteSpaceSeen
;
434 lastPos
= GETLASTPOS
;
436 XawTextSourceRead(source
, fromPos
, &blk
, BUFSIZ
);
438 whiteSpaceSeen
= FALSE
;
440 for (index
= fromPos
; *resWidth
<= width
&& index
< lastPos
; index
++) {
441 lastWidth
= *resWidth
;
442 if ((int)index
- blk
.firstPos
>= blk
.length
)
443 XawTextSourceRead(source
, index
, &blk
, BUFSIZ
);
444 c
= blk
.ptr
[index
- blk
.firstPos
];
445 *resWidth
+= CharWidth(w
, fromx
+ *resWidth
, c
);
447 if ((c
== XawSP
|| c
== XawTAB
) && *resWidth
<= width
) {
448 whiteSpaceSeen
= TRUE
;
449 whiteSpacePosition
= index
;
450 whiteSpaceWidth
= *resWidth
;
457 if (*resWidth
> width
&& index
> fromPos
) {
458 *resWidth
= lastWidth
;
460 if (stopAtWordBreak
&& whiteSpaceSeen
) {
461 index
= whiteSpacePosition
+ 1;
462 *resWidth
= whiteSpaceWidth
;
465 if (index
== lastPos
&& c
!= XawLF
) index
= lastPos
+ 1;
467 *resHeight
= sink
->ascii_sink
.font
->ascent
+sink
->ascii_sink
.font
->descent
;
471 Resolve (w
, pos
, fromx
, width
, leftPos
, rightPos
)
475 XawTextPosition
*leftPos
, *rightPos
;
477 int resWidth
, resHeight
;
478 Widget source
= XawTextGetSource(XtParent(w
));
480 FindPosition(w
, pos
, fromx
, width
, FALSE
, leftPos
, &resWidth
, &resHeight
);
481 if (*leftPos
> GETLASTPOS
)
482 *leftPos
= GETLASTPOS
;
483 *rightPos
= *leftPos
;
488 AsciiSinkObject sink
;
490 XtGCMask valuemask
= (GCFont
|
491 GCGraphicsExposures
| GCForeground
| GCBackground
);
494 values
.font
= sink
->ascii_sink
.font
->fid
;
495 values
.graphics_exposures
= (Bool
) FALSE
;
497 values
.foreground
= sink
->text_sink
.foreground
;
498 values
.background
= sink
->text_sink
.background
;
499 sink
->ascii_sink
.normgc
= XtGetGC((Widget
)sink
, valuemask
, &values
);
501 values
.foreground
= sink
->text_sink
.background
;
502 values
.background
= sink
->text_sink
.foreground
;
503 sink
->ascii_sink
.invgc
= XtGetGC((Widget
)sink
, valuemask
, &values
);
505 values
.function
= GXxor
;
506 values
.background
= (unsigned long) 0L; /* (pix ^ 0) = pix */
507 values
.foreground
= (sink
->text_sink
.background
^
508 sink
->text_sink
.foreground
);
509 valuemask
= GCGraphicsExposures
| GCFunction
| GCForeground
| GCBackground
;
511 sink
->ascii_sink
.xorgc
= XtGetGC((Widget
)sink
, valuemask
, &values
);
515 /***** Public routines *****/
517 /* Function Name: Initialize
518 * Description: Initializes the TextSink Object.
519 * Arguments: request, new - the requested and new values for the object
527 Initialize(request
, new, args
, num_args
)
532 AsciiSinkObject sink
= (AsciiSinkObject
) new;
536 sink
->ascii_sink
.insertCursorOn
= CreateInsertCursor(XtScreenOfObject(new));
537 sink
->ascii_sink
.laststate
= XawisOff
;
538 sink
->ascii_sink
.cursor_x
= sink
->ascii_sink
.cursor_y
= 0;
541 /* Function Name: Destroy
542 * Description: This function cleans up when the object is
544 * Arguments: w - the AsciiSink Object.
552 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
554 XtReleaseGC(w
, sink
->ascii_sink
.normgc
);
555 XtReleaseGC(w
, sink
->ascii_sink
.invgc
);
556 XtReleaseGC(w
, sink
->ascii_sink
.xorgc
);
557 XFreePixmap(XtDisplayOfObject(w
), sink
->ascii_sink
.insertCursorOn
);
560 /* Function Name: SetValues
561 * Description: Sets the values for the AsciiSink
562 * Arguments: current - current state of the object.
563 * request - what was requested.
564 * new - what the object will become.
565 * Returns: True if redisplay is needed.
570 SetValues(current
, request
, new, args
, num_args
)
571 Widget current
, request
, new;
575 AsciiSinkObject w
= (AsciiSinkObject
) new;
576 AsciiSinkObject old_w
= (AsciiSinkObject
) current
;
578 if (w
->ascii_sink
.font
!= old_w
->ascii_sink
.font
579 || w
->text_sink
.background
!= old_w
->text_sink
.background
580 || w
->text_sink
.foreground
!= old_w
->text_sink
.foreground
) {
581 XtReleaseGC((Widget
)w
, w
->ascii_sink
.normgc
);
582 XtReleaseGC((Widget
)w
, w
->ascii_sink
.invgc
);
583 XtReleaseGC((Widget
)w
, w
->ascii_sink
.xorgc
);
585 ((TextWidget
)XtParent(new))->text
.redisplay_needed
= True
;
587 if ( (w
->ascii_sink
.echo
!= old_w
->ascii_sink
.echo
) ||
588 (w
->ascii_sink
.display_nonprinting
!=
589 old_w
->ascii_sink
.display_nonprinting
) )
590 ((TextWidget
)XtParent(new))->text
.redisplay_needed
= True
;
596 /* Function Name: MaxLines
597 * Description: Finds the Maximum number of lines that will fit in
599 * Arguments: w - the AsciiSink Object.
600 * height - height to fit lines into.
601 * Returns: the number of lines that will fit.
610 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
613 font_height
= sink
->ascii_sink
.font
->ascent
+ sink
->ascii_sink
.font
->descent
;
614 return( ((int) height
) / font_height
);
617 /* Function Name: MaxHeight
618 * Description: Finds the Minium height that will contain a given number
620 * Arguments: w - the AsciiSink Object.
621 * lines - the number of lines.
622 * Returns: the height.
631 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
633 return(lines
* (sink
->ascii_sink
.font
->ascent
+
634 sink
->ascii_sink
.font
->descent
));
637 /* Function Name: SetTabs
638 * Description: Sets the Tab stops.
639 * Arguments: w - the AsciiSink Object.
640 * tab_count - the number of tabs in the list.
641 * tabs - the text positions of the tabs.
646 SetTabs(w
, tab_count
, tabs
)
651 AsciiSinkObject sink
= (AsciiSinkObject
) w
;
653 Atom XA_FIGURE_WIDTH
;
654 unsigned long figure_width
= 0;
655 XFontStruct
*font
= sink
->ascii_sink
.font
;
658 * Find the figure width of the current font.
661 XA_FIGURE_WIDTH
= XInternAtom(XtDisplayOfObject(w
), "FIGURE_WIDTH", FALSE
);
662 if ( (XA_FIGURE_WIDTH
!= None
) &&
663 ( (!XGetFontProperty(font
, XA_FIGURE_WIDTH
, &figure_width
)) ||
664 (figure_width
== 0)) )
665 if (font
->per_char
&& font
->min_char_or_byte2
<= '$' &&
666 font
->max_char_or_byte2
>= '$')
667 figure_width
= font
->per_char
['$' - font
->min_char_or_byte2
].width
;
669 figure_width
= font
->max_bounds
.width
;
671 if (tab_count
> sink
->text_sink
.tab_count
) {
672 sink
->text_sink
.tabs
= (Position
*)
673 XtRealloc((char *) sink
->text_sink
.tabs
,
674 (Cardinal
) (tab_count
* sizeof(Position
)));
675 sink
->text_sink
.char_tabs
= (short *)
676 XtRealloc((char *) sink
->text_sink
.char_tabs
,
677 (Cardinal
) (tab_count
* sizeof(short)));
680 for ( i
= 0 ; i
< tab_count
; i
++ ) {
681 sink
->text_sink
.tabs
[i
] = tabs
[i
] * figure_width
;
682 sink
->text_sink
.char_tabs
[i
] = tabs
[i
];
685 sink
->text_sink
.tab_count
= tab_count
;
688 { TextWidget ctx
= (TextWidget
)XtParent(w
);
689 ctx
->text
.redisplay_needed
= True
;
690 _XawTextBuildLineTable(ctx
, ctx
->text
.lt
.top
, TRUE
);