home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / fontutils-0.6-base.tgz / fontutils-0.6-base.tar / fsf / fontutils / widgets / Label.c < prev    next >
C/C++ Source or Header  |  1992-08-19  |  15KB  |  494 lines

  1. /* Label.c: implementation of the Athena Label widget.  The expose
  2.    routine has been modified from the distributed version.
  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. /*
  28.  * Label.c - Label widget
  29.  *
  30.  */
  31.  
  32. #define XtStrlen(s)        ((s) ? strlen(s) : 0)
  33.  
  34. #include "config.h"
  35.  
  36. #include <ctype.h>
  37. #include <X11/IntrinsicP.h>
  38. #include <X11/StringDefs.h>
  39. #include <X11/Xaw/XawInit.h>
  40. #include <X11/Xaw/LabelP.h>
  41. #include <X11/Xmu/Drawing.h>
  42.  
  43. extern void XmuReleaseStippledPixmap ();
  44.  
  45. #define streq(a,b) (strcmp( (a), (b) ) == 0)
  46.  
  47. #define MULTI_LINE_LABEL 32767
  48.  
  49. /****************************************************************
  50.  *
  51.  * Full class record constant
  52.  *
  53.  ****************************************************************/
  54.  
  55. /* Private Data */
  56.  
  57. #define offset(field) XtOffset(LabelWidget, field)
  58. static XtResource resources[] = {
  59.     {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  60.     offset(label.foreground), XtRString, "XtDefaultForeground"},
  61.     {XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  62.     offset(label.font),XtRString, "XtDefaultFont"},
  63.     {XtNlabel,  XtCLabel, XtRString, sizeof(String),
  64.     offset(label.label), XtRString, NULL},
  65.     {XtNjustify, XtCJustify, XtRJustify, sizeof(XtJustify),
  66.     offset(label.justify), XtRImmediate, (caddr_t)XtJustifyCenter},
  67.     {XtNinternalWidth, XtCWidth, XtRDimension,  sizeof(Dimension),
  68.     offset(label.internal_width), XtRImmediate, (caddr_t)4},
  69.     {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
  70.     offset(label.internal_height), XtRImmediate, (caddr_t)2},
  71.     {XtNbitmap, XtCPixmap, XtRBitmap, sizeof(Pixmap),
  72.     offset(label.pixmap), XtRImmediate, (caddr_t)None},
  73.     {XtNresize, XtCResize, XtRBoolean, sizeof(Boolean),
  74.     offset(label.resize), XtRImmediate, (caddr_t)True},
  75. };
  76.  
  77. static void Initialize();
  78. static void Resize();
  79. static void Redisplay();
  80. static Boolean SetValues();
  81. static void ClassInitialize();
  82. static void Destroy();
  83. static XtGeometryResult QueryGeometry();
  84.  
  85. LabelClassRec labelClassRec = {
  86.   {
  87. /* core_class fields */    
  88. #define superclass        (&simpleClassRec)
  89.     /* superclass          */    (WidgetClass) superclass,
  90.     /* class_name          */    "Label",
  91.     /* widget_size          */    sizeof(LabelRec),
  92.     /* class_initialize       */    ClassInitialize,
  93.     /* class_part_initialize    */    NULL,
  94.     /* class_inited           */    FALSE,
  95.     /* initialize          */    Initialize,
  96.     /* initialize_hook        */    NULL,
  97.     /* realize              */    XtInheritRealize,
  98.     /* actions              */    NULL,
  99.     /* num_actions          */    0,
  100.     /* resources          */    resources,
  101.     /* num_resources          */    XtNumber(resources),
  102.     /* xrm_class          */    NULLQUARK,
  103.     /* compress_motion          */    TRUE,
  104.     /* compress_exposure      */    TRUE,
  105.     /* compress_enterleave    */    TRUE,
  106.     /* visible_interest          */    FALSE,
  107.     /* destroy              */    Destroy,
  108.     /* resize              */    Resize,
  109.     /* expose              */    Redisplay,
  110.     /* set_values          */    SetValues,
  111.     /* set_values_hook        */    NULL,
  112.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  113.     /* get_values_hook        */    NULL,
  114.     /* accept_focus         */    NULL,
  115.     /* version            */    XtVersion,
  116.     /* callback_private       */    NULL,
  117.     /* tm_table               */    NULL,
  118.     /* query_geometry        */    QueryGeometry,
  119.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  120.     /* extension        */    NULL
  121.   },
  122. /* Simple class fields initialization */
  123.   {
  124.     /* change_sensitive        */    XtInheritChangeSensitive
  125.   }
  126. };
  127. WidgetClass labelWidgetClass = (WidgetClass)&labelClassRec;
  128. /****************************************************************
  129.  *
  130.  * Private Procedures
  131.  *
  132.  ****************************************************************/
  133.  
  134. static void ClassInitialize()
  135. {
  136.     extern void XmuCvtStringToJustify();
  137.     extern void XmuCvtStringToBitmap();
  138.     static XtConvertArgRec screenConvertArg[] = {
  139.         {XtWidgetBaseOffset, (caddr_t) XtOffset(Widget, core.screen),
  140.          sizeof(Screen *)}
  141.     };
  142.     XawInitializeWidgetSet();
  143.     XtAddConverter( XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0 );
  144.     XtAddConverter("String", "Bitmap", XmuCvtStringToBitmap,
  145.            screenConvertArg, XtNumber(screenConvertArg));
  146. } /* ClassInitialize */
  147.  
  148. /*
  149.  * Calculate width and height of displayed text in pixels
  150.  */
  151.  
  152. static void SetTextWidthAndHeight(lw)
  153.     LabelWidget lw;
  154. {
  155.     register XFontStruct    *fs = lw->label.font;
  156.     char *nl;
  157.  
  158.     if (lw->label.pixmap != None) {
  159.     Window root;
  160.     int x, y;
  161.     unsigned int width, height, bw, depth;
  162.     if (XGetGeometry(XtDisplay(lw), lw->label.pixmap, &root, &x, &y,
  163.              &width, &height, &bw, &depth)) {
  164.         lw->label.label_height = height;
  165.         lw->label.label_width = width;
  166.         lw->label.label_len = depth;
  167.         return;
  168.     }
  169.     }
  170.  
  171.     lw->label.label_height = fs->max_bounds.ascent + fs->max_bounds.descent;
  172.     if (lw->label.label == NULL) {
  173.     lw->label.label_len = 0;
  174.     lw->label.label_width = 0;
  175.     }
  176.     else if ((nl = strchr(lw->label.label, '\n')) != NULL) {
  177.     char *label;
  178.     lw->label.label_len = MULTI_LINE_LABEL;
  179.     lw->label.label_width = 0;
  180.     for (label = lw->label.label; nl != NULL; nl = strchr(label, '\n')) {
  181.         int width = XTextWidth(fs, label, (int)(nl - label));
  182.         if (width > lw->label.label_width) lw->label.label_width = width;
  183.         label = nl + 1;
  184.         if (*label)
  185.         lw->label.label_height +=
  186.             fs->max_bounds.ascent + fs->max_bounds.descent;
  187.     }
  188.     if (*label) {
  189.         int width = XTextWidth(fs, label, strlen(label));
  190.         if (width > lw->label.label_width) lw->label.label_width = width;
  191.     }
  192.     } else {
  193.     lw->label.label_len = strlen(lw->label.label);
  194.     lw->label.label_width =
  195.         XTextWidth(fs, lw->label.label, (int) lw->label.label_len);
  196.     }
  197. }
  198.  
  199. static void GetnormalGC(lw)
  200.     LabelWidget lw;
  201. {
  202.     XGCValues    values;
  203.  
  204.     values.foreground    = lw->label.foreground;
  205.     values.background    = lw->core.background_pixel;
  206.     values.font        = lw->label.font->fid;
  207.  
  208.     lw->label.normal_GC = XtGetGC(
  209.     (Widget)lw,
  210.     (unsigned) GCForeground | GCBackground | GCFont,
  211.     &values);
  212. }
  213.  
  214. static void GetgrayGC(lw)
  215.     LabelWidget lw;
  216. {
  217.     XGCValues    values;
  218.  
  219.     values.foreground = lw->label.foreground;
  220.     values.background = lw->core.background_pixel;
  221.     values.font          = lw->label.font->fid;
  222.     values.fill_style = FillTiled;
  223.     values.tile       = XmuCreateStippledPixmap(XtScreen((Widget)lw),
  224.                         lw->label.foreground, 
  225.                         lw->core.background_pixel,
  226.                         lw->core.depth);
  227.  
  228.     lw->label.stipple = values.tile;
  229.     lw->label.gray_GC = XtGetGC((Widget)lw, 
  230.                 (unsigned) GCForeground | GCBackground |
  231.                        GCFont | GCTile | GCFillStyle,
  232.                 &values);
  233. }
  234.  
  235. /* ARGSUSED */
  236. static void Initialize(request, new)
  237.  Widget request, new;
  238. {
  239.     LabelWidget lw = (LabelWidget) new;
  240.  
  241.     if (lw->label.label == NULL) 
  242.         lw->label.label = XtNewString(lw->core.name);
  243.     else {
  244.         lw->label.label = XtNewString(lw->label.label);
  245.     }
  246.  
  247.     GetnormalGC(lw);
  248.     GetgrayGC(lw);
  249.  
  250.     SetTextWidthAndHeight(lw);
  251.  
  252.     if (lw->core.width == 0)
  253.         lw->core.width = lw->label.label_width + 2 * lw->label.internal_width;
  254.     if (lw->core.height == 0)
  255.         lw->core.height = lw->label.label_height + 2*lw->label.internal_height;
  256.  
  257.     lw->label.label_x = lw->label.label_y = 0;
  258.     (*XtClass(new)->core_class.resize) ((Widget)lw);
  259.  
  260. } /* Initialize */
  261.  
  262. /* This routine is called whenever part of the widget needs to be
  263.    redisplayed.   The REGION argument is the union of all rectangles
  264.    needing redisplay.  It can be NULL; see `the expose procedure'
  265.    section in the Xt manual.  */
  266.  
  267. /* ARGSUSED */
  268. static void Redisplay(w, event, region)
  269.     Widget w;
  270.     XEvent *event;
  271.     Region region;
  272. {
  273.    LabelWidget lw = (LabelWidget) w;
  274.    GC gc;
  275.  
  276.    if (region != NULL &&
  277.        XRectInRegion(region, lw->label.label_x, lw->label.label_y,
  278.              lw->label.label_width, lw->label.label_height)
  279.          == RectangleOut)
  280.        return;
  281.  
  282.    gc = XtIsSensitive((Widget) lw) ? lw->label.normal_GC : lw->label.gray_GC;
  283. #ifdef notdef
  284.    if (region != NULL) XSetRegion(XtDisplay(w), gc, region);
  285. #endif /*notdef*/
  286.    if (lw->label.pixmap == None) {
  287.        int len = lw->label.label_len;
  288.        char *label = lw->label.label;
  289.        Position y = lw->label.label_y + lw->label.font->max_bounds.ascent;
  290.        if (len == MULTI_LINE_LABEL) {
  291.        char *nl;
  292.        while ((nl = strchr (label, '\n')) != NULL) {
  293.            XDrawString(
  294.                XtDisplay(w), XtWindow(w), gc, lw->label.label_x,
  295.                y, label, (int)(nl - label));
  296.            y += lw->label.font->max_bounds.ascent + lw->label.font->max_bounds.descent;
  297.            label = nl + 1;
  298.        }
  299.        len = strlen(label);
  300.        }
  301.        if (len)
  302.        XDrawString(
  303.                XtDisplay(w), XtWindow(w), gc, lw->label.label_x,
  304.                y, label, len);
  305.    } else if (lw->label.label_len == 1) { /* depth */
  306.        XRectangle r;
  307.        
  308.        if (region == NULL)
  309.          { /* Redraw the whole window, since we're not given an expose
  310.               rectangle.  */
  311.            r.x = 0;
  312.            r.y = 0;
  313.            r.width = lw->label.label_width;
  314.            r.height = lw->label.label_height;
  315.          }
  316.        else
  317.          XClipBox (region, &r);
  318.  
  319.        XCopyPlane(
  320.           XtDisplay(w), lw->label.pixmap, XtWindow(w), gc,
  321.           r.x, r.y, r.width, r.height,
  322.           lw->label.label_x + r.x, lw->label.label_y + r.y, 1L);
  323.    } else {
  324.        /* Should use Region information here, too, but all my pixmaps
  325.           are bitmaps, so I don't care.  */
  326.        XCopyArea(
  327.          XtDisplay(w), lw->label.pixmap, XtWindow(w), gc,
  328.          0, 0, lw->label.label_width, lw->label.label_height,
  329.          lw->label.label_x, lw->label.label_y);
  330.    }
  331. #ifdef notdef
  332.    if (region != NULL) XSetClipMask(XtDisplay(w), gc, (Pixmap)None);
  333. #endif /* notdef */
  334. }
  335.  
  336. static void _Reposition(lw, width, height, dx, dy)
  337.     register LabelWidget lw;
  338.     Dimension width, height;
  339.     Position *dx, *dy;
  340. {
  341.     Position newPos;
  342.     switch (lw->label.justify) {
  343.  
  344.     case XtJustifyLeft   :
  345.         newPos = lw->label.internal_width;
  346.         break;
  347.  
  348.     case XtJustifyRight  :
  349.         newPos = width -
  350.         (lw->label.label_width + lw->label.internal_width);
  351.         break;
  352.  
  353.     case XtJustifyCenter :
  354.         newPos = (width - lw->label.label_width) / 2;
  355.         break;
  356.     }
  357.     if (newPos < (Position)lw->label.internal_width)
  358.     newPos = lw->label.internal_width;
  359.     *dx = newPos - lw->label.label_x;
  360.     lw->label.label_x = newPos;
  361.     *dy = (newPos = (height - lw->label.label_height) / 2) - lw->label.label_y;
  362.     lw->label.label_y = newPos;
  363.     return;
  364. }
  365.  
  366. static void Resize(w)
  367.     Widget w;
  368. {
  369.     Position dx, dy;
  370.     LabelWidget lw = (LabelWidget)w;
  371.     SetTextWidthAndHeight (lw);
  372.     _Reposition(lw, w->core.width, w->core.height, &dx, &dy);
  373. }
  374.  
  375. /*
  376.  * Set specified arguments into widget
  377.  */
  378.  
  379. #define PIXMAP 0
  380. #define WIDTH 1
  381. #define HEIGHT 2
  382. #define NUM_CHECKS 3
  383.  
  384. static Boolean SetValues(current, request, new, args, num_args)
  385.     Widget current, request, new;
  386.     ArgList args;
  387.     Cardinal *num_args;
  388. {
  389.     LabelWidget curlw = (LabelWidget) current;
  390.     LabelWidget reqlw = (LabelWidget) request;
  391.     LabelWidget newlw = (LabelWidget) new;
  392.     int i;
  393.     Boolean was_resized = False, redisplay = False, checks[NUM_CHECKS];
  394.  
  395.     for (i = 0; i < NUM_CHECKS; i++)
  396.     checks[i] = FALSE;
  397.  
  398.     for (i = 0; i < *num_args; i++) {
  399.     if (streq(XtNbitmap, args[i].name))
  400.         checks[PIXMAP] = TRUE;
  401.     if (streq(XtNwidth, args[i].name))
  402.         checks[WIDTH] = TRUE;
  403.     if (streq(XtNheight, args[i].name))
  404.         checks[HEIGHT] = TRUE;
  405.     }
  406.  
  407.     if (newlw->label.label == NULL) {
  408.     newlw->label.label = newlw->core.name;
  409.     }
  410.  
  411.     if (curlw->label.label != newlw->label.label) {
  412.         if (curlw->label.label != curlw->core.name)
  413.         XtFree( (char *)curlw->label.label );
  414.  
  415.     if (newlw->label.label != newlw->core.name) {
  416.         newlw->label.label = XtNewString( newlw->label.label );
  417.     }
  418.     was_resized = True;
  419.     }
  420.  
  421.     if (was_resized || (curlw->label.font != newlw->label.font) ||
  422.     (curlw->label.justify != newlw->label.justify) || checks[PIXMAP]) {
  423.  
  424.     SetTextWidthAndHeight(newlw);
  425.     was_resized = True;
  426.     }
  427.  
  428.     /* recalculate the window size if something has changed. */
  429.     if (newlw->label.resize && was_resized) {
  430.     if ((curlw->core.width == reqlw->core.width) && !checks[WIDTH])
  431.         newlw->core.width = (newlw->label.label_width +
  432.                  2 * newlw->label.internal_width);
  433.  
  434.     if ((curlw->core.height == reqlw->core.height) && !checks[HEIGHT])
  435.         newlw->core.height = (newlw->label.label_height + 
  436.                   2 * newlw->label.internal_height);
  437.     }
  438.  
  439.     if (curlw->label.foreground != newlw->label.foreground
  440.     || curlw->label.font->fid != newlw->label.font->fid) {
  441.  
  442.     XtReleaseGC(new, curlw->label.normal_GC);
  443.     XtReleaseGC(new, curlw->label.gray_GC);
  444.     XmuReleaseStippledPixmap( XtScreen(current), curlw->label.stipple );
  445.     GetnormalGC(newlw);
  446.     GetgrayGC(newlw);
  447.     redisplay = True;
  448.     }
  449.  
  450.     if ((curlw->label.internal_width != newlw->label.internal_width)
  451.         || (curlw->label.internal_height != newlw->label.internal_height)
  452.     || was_resized) {
  453.     /* Resize() will be called if geometry changes succeed */
  454.     Position dx, dy;
  455.     _Reposition(newlw, curlw->core.width, curlw->core.height, &dx, &dy);
  456.     }
  457.  
  458.     return was_resized || redisplay ||
  459.        XtIsSensitive(current) != XtIsSensitive(new);
  460. }
  461.  
  462. static void Destroy(w)
  463.     Widget w;
  464. {
  465.     LabelWidget lw = (LabelWidget)w;
  466.  
  467.     XtFree( lw->label.label );
  468.     XtReleaseGC( w, lw->label.normal_GC );
  469.     XtReleaseGC( w, lw->label.gray_GC);
  470.     XmuReleaseStippledPixmap( XtScreen(w), lw->label.stipple );
  471. }
  472.  
  473.  
  474. static XtGeometryResult QueryGeometry(w, intended, preferred)
  475.     Widget w;
  476.     XtWidgetGeometry *intended, *preferred;
  477. {
  478.     register LabelWidget lw = (LabelWidget)w;
  479.  
  480.     preferred->request_mode = CWWidth | CWHeight;
  481.     preferred->width = lw->label.label_width + 2 * lw->label.internal_width;
  482.     preferred->height = lw->label.label_height + 2*lw->label.internal_height;
  483.     if (  ((intended->request_mode & (CWWidth | CWHeight))
  484.            == (CWWidth | CWHeight)) &&
  485.       intended->width == preferred->width &&
  486.       intended->height == preferred->height)
  487.     return XtGeometryYes;
  488.     else if (preferred->width == w->core.width &&
  489.          preferred->height == w->core.height)
  490.     return XtGeometryNo;
  491.     else
  492.     return XtGeometryAlmost;
  493. }
  494.