home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / AsciiSink.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-21  |  16.4 KB  |  565 lines

  1. /* $XConsortium: AsciiSink.c,v 1.57 91/07/21 20:35:00 converse Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. #include <stdio.h>
  28.  
  29. #include <X11/Xlib.h>
  30. #include <X11/Xutil.h>
  31. #include <X11/Xatom.h>
  32. #include <X11/IntrinsicP.h>
  33. #include <X11/StringDefs.h>
  34. #include <X11/Xaw/XawInit.h>
  35. #include <X11/Xaw/AsciiSinkP.h>
  36. #include <X11/Xaw/AsciiSrcP.h>    /* For source function defs. */
  37. #include <X11/Xaw/TextP.h>    /* I also reach into the text widget. */
  38.  
  39. #ifdef GETLASTPOS
  40. #undef GETLASTPOS        /* We will use our own GETLASTPOS. */
  41. #endif
  42.  
  43. #define GETLASTPOS XawTextSourceScan(source, 0, XawstAll, XawsdRight, 1, TRUE)
  44.  
  45. static void Initialize(), Destroy();
  46. static Boolean SetValues();
  47.  
  48. static void DisplayText(), InsertCursor(), FindPosition();
  49. static void FindDistance(), Resolve(), GetCursorBounds();
  50.  
  51. #define offset(field) XtOffsetOf(AsciiSinkRec, ascii_sink.field)
  52.  
  53. static XtResource resources[] = {
  54.     {XtNecho, XtCOutput, XtRBoolean, sizeof(Boolean),
  55.     offset(echo), XtRImmediate, (XtPointer) True},
  56.     {XtNdisplayNonprinting, XtCOutput, XtRBoolean, sizeof(Boolean),
  57.     offset(display_nonprinting), XtRImmediate, (XtPointer) True},
  58. };
  59. #undef offset
  60.  
  61. #define SuperClass        (&textSinkClassRec)
  62. AsciiSinkClassRec asciiSinkClassRec = {
  63.   {
  64. /* core_class fields */    
  65.     /* superclass          */    (WidgetClass) SuperClass,
  66.     /* class_name          */    "AsciiSink",
  67.     /* widget_size          */    sizeof(AsciiSinkRec),
  68.     /* class_initialize       */    XawInitializeWidgetSet,
  69.     /* class_part_initialize    */    NULL,
  70.     /* class_inited           */    FALSE,
  71.     /* initialize          */    Initialize,
  72.     /* initialize_hook        */    NULL,
  73.     /* obj1              */    NULL,
  74.     /* obj2              */    NULL,
  75.     /* obj3              */    0,
  76.     /* resources          */    resources,
  77.     /* num_resources          */    XtNumber(resources),
  78.     /* xrm_class          */    NULLQUARK,
  79.     /* obj4              */    FALSE,
  80.     /* obj5              */    FALSE,
  81.     /* obj6            */    FALSE,
  82.     /* obj7              */    FALSE,
  83.     /* destroy              */    Destroy,
  84.     /* obj8              */    NULL,
  85.     /* obj9              */    NULL,
  86.     /* set_values          */    SetValues,
  87.     /* set_values_hook        */    NULL,
  88.     /* obj10            */    NULL,
  89.     /* get_values_hook        */    NULL,
  90.     /* obj11             */    NULL,
  91.     /* version            */    XtVersion,
  92.     /* callback_private       */    NULL,
  93.     /* obj12               */    NULL,
  94.     /* obj13            */    NULL,
  95.     /* obj14            */    NULL,
  96.     /* extension        */    NULL
  97.   },
  98. /* text_sink_class fields */
  99.   {
  100.     /* DisplayText              */      DisplayText,
  101.     /* InsertCursor             */      InsertCursor,
  102.     /* ClearToBackground        */      XtInheritClearToBackground,
  103.     /* FindPosition             */      FindPosition,
  104.     /* FindDistance             */      FindDistance,
  105.     /* Resolve                  */      Resolve,
  106.     /* MaxLines                 */      XtInheritMaxLines,
  107.     /* MaxHeight                */      XtInheritMaxHeight,
  108.     /* SetTabs                  */      XtInheritSetTabs,
  109.     /* GetCursorBounds          */      GetCursorBounds
  110.   },
  111. /* ascii_sink_class fields */
  112.   {
  113.     /* unused            */    0
  114.   }
  115. };
  116.  
  117. WidgetClass asciiSinkObjectClass = (WidgetClass)&asciiSinkClassRec;
  118.  
  119. /* Utilities */
  120.  
  121. static int 
  122. CharWidth (w, x, c)
  123. Widget w;
  124. int x;
  125. unsigned char c;
  126. {
  127.     register int    i, width, nonPrinting;
  128.     AsciiSinkObject sink = (AsciiSinkObject) w;
  129.     XFontStruct *font = sink->text_sink.font;
  130.     Position *tab;
  131.  
  132.     if ( c == XawLF ) return(0);
  133.  
  134.     if (c == XawTAB) {
  135.     /* Adjust for Left Margin. */
  136.     x -= ((TextWidget) XtParent(w))->text.margin.left;
  137.  
  138.     if (x >= (int)XtParent(w)->core.width) return 0;
  139.     for (i = 0, tab = sink->text_sink.tabs ; 
  140.          i < sink->text_sink.tab_count ; i++, tab++) {
  141.         if (x < *tab) {
  142.         if (*tab < (int)XtParent(w)->core.width)
  143.             return *tab - x;
  144.         else
  145.             return 0;
  146.         }
  147.     }
  148.     return 0;
  149.     }
  150.  
  151.     if ( (nonPrinting = (c < (unsigned char) XawSP)) )
  152.     if (sink->ascii_sink.display_nonprinting)
  153.         c += '@';
  154.     else {
  155.         c = XawSP;
  156.         nonPrinting = False;
  157.     }
  158.  
  159.     if (font->per_char &&
  160.         (c >= font->min_char_or_byte2 && c <= font->max_char_or_byte2))
  161.     width = font->per_char[c - font->min_char_or_byte2].width;
  162.     else
  163.     width = font->min_bounds.width;
  164.  
  165.     if (nonPrinting)
  166.     width += CharWidth(w, x, (unsigned char) '^');
  167.  
  168.     return width;
  169. }
  170.  
  171. /*    Function Name: PaintText
  172.  *    Description: Actually paints the text into the windoe.
  173.  *    Arguments: w - the text widget.
  174.  *                 gc - gc to paint text with.
  175.  *                 x, y - location to paint the text.
  176.  *                 buf, len - buffer and length of text to paint.
  177.  *    Returns: the width of the text painted, or 0.
  178.  *
  179.  * NOTE:  If this string attempts to paint past the end of the window
  180.  *        then this function will return zero.
  181.  */
  182.  
  183. static Dimension
  184. PaintText(w, gc, x, y, buf, len)
  185. Widget w;
  186. GC gc;
  187. Position x, y;
  188. unsigned char * buf;
  189. int len;
  190. {
  191.     AsciiSinkObject sink = (AsciiSinkObject) w;
  192.     TextWidget ctx = (TextWidget) XtParent(w);
  193.  
  194.     Position max_x;
  195.     Dimension width = XTextWidth(sink->text_sink.font, (char *) buf, len); 
  196.     max_x = (Position) ctx->core.width;
  197.  
  198.     if ( ((int) width) <= -x)               /* Don't draw if we can't see it. */
  199.       return(width);
  200.  
  201.     XDrawImageString(XtDisplay(ctx), XtWindow(ctx), gc, 
  202.              (int) x, (int) y, (char *) buf, len);
  203.     if ( (((Position) width + x) > max_x) && (ctx->text.margin.right != 0) ) {
  204.     x = ctx->core.width - ctx->text.margin.right;
  205.     width = ctx->text.margin.right;
  206.     XFillRectangle(XtDisplay((Widget) ctx), XtWindow( (Widget) ctx),
  207.                sink->ascii_sink.normgc, (int) x,
  208.                (int) y - sink->text_sink.font->ascent, 
  209.                (unsigned int) width,
  210.                (unsigned int) (sink->text_sink.font->ascent +
  211.                        sink->text_sink.font->descent));
  212.     return(0);
  213.     }
  214.     return(width);
  215. }
  216.  
  217. /* Sink Object Functions */
  218.  
  219. /*
  220.  * This function does not know about drawing more than one line of text.
  221.  */
  222.  
  223. static void 
  224. DisplayText(w, x, y, pos1, pos2, highlight)
  225. Widget w;
  226. Position x, y;
  227. Boolean highlight;
  228. XawTextPosition pos1, pos2;
  229. {
  230.     AsciiSinkObject sink = (AsciiSinkObject) w;
  231.     Widget source = XawTextGetSource(XtParent(w));
  232.     unsigned char buf[BUFSIZ];
  233.  
  234.     int j, k;
  235.     XawTextBlock blk;
  236.     GC gc = highlight ? sink->ascii_sink.invgc : sink->ascii_sink.normgc;
  237.     GC invgc = highlight ? sink->ascii_sink.normgc : sink->ascii_sink.invgc;
  238.  
  239.     if (!sink->ascii_sink.echo) return;
  240.  
  241.     y += sink->text_sink.font->ascent;
  242.     for ( j = 0 ; pos1 < pos2 ; ) {
  243.     pos1 = XawTextSourceRead(source, pos1, &blk, pos2 - pos1);
  244.     for (k = 0; k < blk.length; k++) {
  245.         if (j >= BUFSIZ) {    /* buffer full, dump the text. */
  246.             x += PaintText(w, gc, x, y, buf, j);
  247.         j = 0;
  248.         }
  249.         buf[j] = blk.ptr[k];
  250.         if (buf[j] == XawLF)    /* line feeds ('\n') are not printed. */
  251.             continue;
  252.  
  253.         else if (buf[j] == '\t') {
  254.             Position temp = 0;
  255.         Dimension width;
  256.  
  257.             if ((j != 0) && ((temp = PaintText(w, gc, x, y, buf, j)) == 0))
  258.           return;
  259.  
  260.             x += temp;
  261.         width = CharWidth(w, x, (unsigned char) '\t');
  262.         XFillRectangle(XtDisplayOfObject(w), XtWindowOfObject(w),
  263.                    invgc, (int) x,
  264.                    (int) y - sink->text_sink.font->ascent,
  265.                    (unsigned int) width,
  266.                    (unsigned int) (sink->text_sink.font->ascent +
  267.                            sink->text_sink.font->descent));
  268.         x += width;
  269.         j = -1;
  270.         }
  271.         else if ( buf[j] < (unsigned char) ' ' ) {
  272.             if (sink->ascii_sink.display_nonprinting) {
  273.             buf[j + 1] = buf[j] + '@';
  274.             buf[j] = '^';
  275.             j++;
  276.         }
  277.         else
  278.             buf[j] = ' ';
  279.         }
  280.         j++;
  281.     }
  282.     }
  283.     if (j > 0)
  284.         (void) PaintText(w, gc, x, y, buf, j);
  285. }
  286.  
  287. #define insertCursor_width 6
  288. #define insertCursor_height 3
  289. static char insertCursor_bits[] = {0x0c, 0x1e, 0x33};
  290.  
  291. static Pixmap
  292. CreateInsertCursor(s)
  293. Screen *s;
  294. {
  295.     return (XCreateBitmapFromData (DisplayOfScreen(s), RootWindowOfScreen(s),
  296.           insertCursor_bits, insertCursor_width, insertCursor_height));
  297. }
  298.  
  299. /*    Function Name: GetCursorBounds
  300.  *    Description: Returns the size and location of the cursor.
  301.  *    Arguments: w - the text object.
  302.  * RETURNED        rect - an X rectangle to return the cursor bounds in.
  303.  *    Returns: none.
  304.  */
  305.  
  306. static void
  307. GetCursorBounds(w, rect)
  308. Widget w;
  309. XRectangle * rect;
  310. {
  311.     AsciiSinkObject sink = (AsciiSinkObject) w;
  312.  
  313.     rect->width = (unsigned short) insertCursor_width;
  314.     rect->height = (unsigned short) insertCursor_height;
  315.     rect->x = sink->ascii_sink.cursor_x - (short) (rect->width / 2);
  316.     rect->y = sink->ascii_sink.cursor_y - (short) rect->height;
  317. }
  318.  
  319. /*
  320.  * The following procedure manages the "insert" cursor.
  321.  */
  322.  
  323. static void
  324. InsertCursor (w, x, y, state)
  325. Widget w;
  326. Position x, y;
  327. XawTextInsertState state;
  328. {
  329.     AsciiSinkObject sink = (AsciiSinkObject) w;
  330.     Widget text_widget = XtParent(w);
  331.     XRectangle rect;
  332.  
  333.     sink->ascii_sink.cursor_x = x;
  334.     sink->ascii_sink.cursor_y = y;
  335.  
  336.     GetCursorBounds(w, &rect);
  337.     if (state != sink->ascii_sink.laststate && XtIsRealized(text_widget)) 
  338.         XCopyPlane(XtDisplay(text_widget),
  339.            sink->ascii_sink.insertCursorOn,
  340.            XtWindow(text_widget), sink->ascii_sink.xorgc,
  341.            0, 0, (unsigned int) rect.width, (unsigned int) rect.height,
  342.            (int) rect.x, (int) rect.y, 1);
  343.     sink->ascii_sink.laststate = state;
  344. }
  345.  
  346. /*
  347.  * Given two positions, find the distance between them.
  348.  */
  349.  
  350. static void
  351. FindDistance (w, fromPos, fromx, toPos, resWidth, resPos, resHeight)
  352. Widget w;
  353. XawTextPosition fromPos;    /* First position. */
  354. int fromx;            /* Horizontal location of first position. */
  355. XawTextPosition toPos;        /* Second position. */
  356. int *resWidth;            /* Distance between fromPos and resPos. */
  357. XawTextPosition *resPos;    /* Actual second position used. */
  358. int *resHeight;            /* Height required. */
  359. {
  360.     AsciiSinkObject sink = (AsciiSinkObject) w;
  361.     Widget source = XawTextGetSource(XtParent(w));
  362.  
  363.     register XawTextPosition index, lastPos;
  364.     register unsigned char c;
  365.     XawTextBlock blk;
  366.  
  367.     /* we may not need this */
  368.     lastPos = GETLASTPOS;
  369.     XawTextSourceRead(source, fromPos, &blk, toPos - fromPos);
  370.     *resWidth = 0;
  371.     for (index = fromPos; index != toPos && index < lastPos; index++) {
  372.     if (index - blk.firstPos >= blk.length)
  373.         XawTextSourceRead(source, index, &blk, toPos - fromPos);
  374.     c = blk.ptr[index - blk.firstPos];
  375.     *resWidth += CharWidth(w, fromx + *resWidth, c);
  376.     if (c == XawLF) {
  377.         index++;
  378.         break;
  379.     }
  380.     }
  381.     *resPos = index;
  382.     *resHeight = sink->text_sink.font->ascent +sink->text_sink.font->descent;
  383. }
  384.  
  385.  
  386. static void
  387. FindPosition(w, fromPos, fromx, width, stopAtWordBreak, 
  388.           resPos, resWidth, resHeight)
  389. Widget w;
  390. XawTextPosition fromPos;     /* Starting position. */
  391. int fromx;            /* Horizontal location of starting position.*/
  392. int width;            /* Desired width. */
  393. int stopAtWordBreak;        /* Whether the resulting position should be at
  394.                    a word break. */
  395. XawTextPosition *resPos;    /* Resulting position. */
  396. int *resWidth;            /* Actual width used. */
  397. int *resHeight;            /* Height required. */
  398. {
  399.     AsciiSinkObject sink = (AsciiSinkObject) w;
  400.     Widget source = XawTextGetSource(XtParent(w));
  401.  
  402.     XawTextPosition lastPos, index, whiteSpacePosition;
  403.     int     lastWidth, whiteSpaceWidth;
  404.     Boolean whiteSpaceSeen;
  405.     unsigned char c;
  406.     XawTextBlock blk;
  407.  
  408.     lastPos = GETLASTPOS;
  409.  
  410.     XawTextSourceRead(source, fromPos, &blk, BUFSIZ);
  411.     *resWidth = 0;
  412.     whiteSpaceSeen = FALSE;
  413.     c = 0;
  414.     for (index = fromPos; *resWidth <= width && index < lastPos; index++) {
  415.     lastWidth = *resWidth;
  416.     if (index - blk.firstPos >= blk.length)
  417.         XawTextSourceRead(source, index, &blk, BUFSIZ);
  418.     c = blk.ptr[index - blk.firstPos];
  419.     *resWidth += CharWidth(w, fromx + *resWidth, c);
  420.  
  421.     if ((c == XawSP || c == XawTAB) && *resWidth <= width) {
  422.         whiteSpaceSeen = TRUE;
  423.         whiteSpacePosition = index;
  424.         whiteSpaceWidth = *resWidth;
  425.     }
  426.     if (c == XawLF) {
  427.         index++;
  428.         break;
  429.     }
  430.     }
  431.     if (*resWidth > width && index > fromPos) {
  432.     *resWidth = lastWidth;
  433.     index--;
  434.     if (stopAtWordBreak && whiteSpaceSeen) {
  435.         index = whiteSpacePosition + 1;
  436.         *resWidth = whiteSpaceWidth;
  437.     }
  438.     }
  439.     if (index == lastPos && c != XawLF) index = lastPos + 1;
  440.     *resPos = index;
  441.     *resHeight = sink->text_sink.font->ascent +sink->text_sink.font->descent;
  442. }
  443.  
  444. static void
  445. Resolve (w, pos, fromx, width, leftPos, rightPos)
  446. Widget w;
  447. XawTextPosition pos;
  448. int fromx, width;
  449. XawTextPosition *leftPos, *rightPos;
  450. {
  451.     int resWidth, resHeight;
  452.     Widget source = XawTextGetSource(XtParent(w));
  453.  
  454.     FindPosition(w, pos, fromx, width, FALSE, leftPos, &resWidth, &resHeight);
  455.     if (*leftPos > GETLASTPOS)
  456.       *leftPos = GETLASTPOS;
  457.     *rightPos = *leftPos;
  458. }
  459.  
  460. static void
  461. GetGC(sink)
  462. AsciiSinkObject sink;
  463. {
  464.     XtGCMask valuemask = (GCFont | 
  465.               GCGraphicsExposures | GCForeground | GCBackground );
  466.     XGCValues values;
  467.  
  468.     values.font = sink->text_sink.font->fid;
  469.     values.graphics_exposures = (Bool) FALSE;
  470.     
  471.     values.foreground = sink->text_sink.foreground;
  472.     values.background = sink->text_sink.background;
  473.     sink->ascii_sink.normgc = XtGetGC((Widget)sink, valuemask, &values);
  474.     
  475.     values.foreground = sink->text_sink.background;
  476.     values.background = sink->text_sink.foreground;
  477.     sink->ascii_sink.invgc = XtGetGC((Widget)sink, valuemask, &values);
  478.     
  479.     values.function = GXxor;
  480.     values.background = (unsigned long) 0L;    /* (pix ^ 0) = pix */
  481.     values.foreground = (sink->text_sink.background ^ 
  482.              sink->text_sink.foreground);
  483.     valuemask = GCGraphicsExposures | GCFunction | GCForeground | GCBackground;
  484.     
  485.     sink->ascii_sink.xorgc = XtGetGC((Widget)sink, valuemask, &values);
  486. }
  487.  
  488.  
  489. /***** Public routines *****/
  490.  
  491. /*    Function Name: Initialize
  492.  *    Description: Initializes the TextSink Object.
  493.  *    Arguments: request, new - the requested and new values for the object
  494.  *                                instance.
  495.  *    Returns: none.
  496.  *
  497.  */
  498.  
  499. /* ARGSUSED */
  500. static void
  501. Initialize(request, new)
  502. Widget request, new;
  503. {
  504.     AsciiSinkObject sink = (AsciiSinkObject) new;
  505.  
  506.     GetGC(sink);
  507.     
  508.     sink->ascii_sink.insertCursorOn= CreateInsertCursor(XtScreenOfObject(new));
  509.     sink->ascii_sink.laststate = XawisOff;
  510.     sink->ascii_sink.cursor_x = sink->ascii_sink.cursor_y = 0;
  511. }
  512.  
  513. /*    Function Name: Destroy
  514.  *    Description: This function cleans up when the object is 
  515.  *                   destroyed.
  516.  *    Arguments: w - the AsciiSink Object.
  517.  *    Returns: none.
  518.  */
  519.  
  520. static void
  521. Destroy(w)
  522. Widget w;
  523. {
  524.    AsciiSinkObject sink = (AsciiSinkObject) w;
  525.  
  526.    XtReleaseGC(w, sink->ascii_sink.normgc);
  527.    XtReleaseGC(w, sink->ascii_sink.invgc);
  528.    XtReleaseGC(w, sink->ascii_sink.xorgc);
  529.    XFreePixmap(XtDisplayOfObject(w), sink->ascii_sink.insertCursorOn);
  530. }
  531.  
  532. /*    Function Name: SetValues
  533.  *    Description: Sets the values for the AsciiSink
  534.  *    Arguments: current - current state of the object.
  535.  *                 request - what was requested.
  536.  *                 new - what the object will become.
  537.  *    Returns: True if redisplay is needed.
  538.  */
  539.  
  540. /* ARGSUSED */
  541. static Boolean
  542. SetValues(current, request, new)
  543. Widget current, request, new;
  544. {
  545.     AsciiSinkObject w = (AsciiSinkObject) new;
  546.     AsciiSinkObject old_w = (AsciiSinkObject) current;
  547.  
  548.     if (w->text_sink.font != old_w->text_sink.font
  549.     || w->text_sink.background != old_w->text_sink.background
  550.     || w->text_sink.foreground != old_w->text_sink.foreground) {
  551.     XtReleaseGC((Widget)w, w->ascii_sink.normgc);
  552.     XtReleaseGC((Widget)w, w->ascii_sink.invgc);
  553.     XtReleaseGC((Widget)w, w->ascii_sink.xorgc);
  554.     GetGC(w);
  555.     ((TextWidget)XtParent(new))->text.redisplay_needed = True;
  556.     } else {
  557.     if ( (w->ascii_sink.echo != old_w->ascii_sink.echo) ||
  558.          (w->ascii_sink.display_nonprinting != 
  559.                                      old_w->ascii_sink.display_nonprinting) )
  560.         ((TextWidget)XtParent(new))->text.redisplay_needed = True;
  561.     }
  562.     
  563.     return False;
  564. }
  565.