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

  1. /* $XConsortium: SmeLine.c,v 1.13 91/07/23 12:23:21 rws Exp $ */
  2.  
  3. /*
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Chris D. Peterson, MIT X Consortium
  24.  */
  25.  
  26. /*
  27.  * Sme.c - Source code for the generic menu entry
  28.  *
  29.  * Date:    September 26, 1989
  30.  *
  31.  * By:      Chris D. Peterson
  32.  *          MIT X Consortium 
  33.  *          kit@expo.lcs.mit.edu
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <X11/IntrinsicP.h>
  38. #include <X11/StringDefs.h>
  39.  
  40. #include <X11/Xaw/XawInit.h>
  41. #include <X11/Xaw/SmeLineP.h>
  42. #include <X11/Xaw/Cardinals.h>
  43.  
  44. #define offset(field) XtOffsetOf(SmeLineRec, sme_line.field)
  45. static XtResource resources[] = {
  46.   {XtNlineWidth, XtCLineWidth, XtRDimension, sizeof(Dimension),
  47.      offset(line_width), XtRImmediate, (XtPointer) 1},
  48.   {XtNstipple, XtCStipple, XtRBitmap, sizeof(Pixmap),
  49.      offset(stipple), XtRImmediate, (XtPointer) XtUnspecifiedPixmap},
  50.   {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  51.      offset(foreground), XtRString, XtDefaultForeground},
  52. };   
  53. #undef offset
  54.  
  55. /*
  56.  * Function definitions. 
  57.  */
  58.  
  59. static void Redisplay(), Initialize();
  60. static void DestroyGC(), CreateGC();
  61. static Boolean SetValues();
  62. static XtGeometryResult QueryGeometry();
  63.  
  64.  
  65. #define SUPERCLASS (&smeClassRec)
  66.  
  67. SmeLineClassRec smeLineClassRec = {
  68.   {
  69.     /* superclass         */    (WidgetClass) SUPERCLASS,
  70.     /* class_name         */    "SmeLine",
  71.     /* size               */    sizeof(SmeLineRec),
  72.     /* class_initialize   */    XawInitializeWidgetSet,
  73.     /* class_part_initialize*/    NULL,
  74.     /* Class init'ed      */    FALSE,
  75.     /* initialize         */    Initialize,
  76.     /* initialize_hook    */    NULL,
  77.     /* realize            */    NULL,
  78.     /* actions            */    NULL,
  79.     /* num_actions        */    ZERO,
  80.     /* resources          */    resources,
  81.     /* resource_count     */    XtNumber(resources),
  82.     /* xrm_class          */    NULLQUARK,
  83.     /* compress_motion    */    FALSE, 
  84.     /* compress_exposure  */    FALSE,
  85.     /* compress_enterleave*/     FALSE,
  86.     /* visible_interest   */    FALSE,
  87.     /* destroy            */    DestroyGC,
  88.     /* resize             */    NULL,
  89.     /* expose             */    Redisplay,
  90.     /* set_values         */    SetValues,
  91.     /* set_values_hook    */    NULL,
  92.     /* set_values_almost  */    XtInheritSetValuesAlmost,  
  93.     /* get_values_hook    */    NULL,            
  94.     /* accept_focus       */    NULL,
  95.     /* intrinsics version */    XtVersion,
  96.     /* callback offsets   */    NULL,
  97.     /* tm_table          */    NULL,
  98.     /* query_geometry      */    QueryGeometry,
  99.     /* display_accelerator*/    NULL,
  100.     /* extension      */    NULL
  101.   },{
  102.     /* Menu Entry Fields */
  103.       
  104.     /* highlight */             XtInheritHighlight,
  105.     /* unhighlight */           XtInheritUnhighlight,
  106.     /* notify */        XtInheritNotify,        
  107.     /* extension */             NULL                
  108.   },{
  109.     /* Line Menu Entry Fields */
  110.     /* extension */             NULL                
  111.   }
  112. };
  113.  
  114. WidgetClass smeLineObjectClass = (WidgetClass) &smeLineClassRec;
  115.  
  116. /************************************************************
  117.  *
  118.  * Semi-Public Functions.
  119.  *
  120.  ************************************************************/
  121.  
  122. /*      Function Name: Initialize
  123.  *      Description: Initializes the simple menu widget
  124.  *      Arguments: request - the widget requested by the argument list.
  125.  *                 new     - the new widget with both resource and non
  126.  *                           resource values.
  127.  *      Returns: none.
  128.  */
  129.  
  130. /* ARGSUSED */
  131. static void
  132. Initialize(request, new)
  133. Widget request, new;
  134. {
  135.     SmeLineObject entry = (SmeLineObject) new;
  136.  
  137.     if (entry->rectangle.height == 0)
  138.     entry->rectangle.height = entry->sme_line.line_width;
  139.  
  140.     CreateGC(new);
  141. }
  142.  
  143. /*    Function Name: CreateGC
  144.  *    Description: Creates the GC for the line entry widget.
  145.  *    Arguments: w - the Line entry widget.
  146.  *    Returns: none
  147.  *
  148.  *      We can only share the GC if there is no stipple, because
  149.  *      we need to change the stipple origin when drawing.
  150.  */
  151.  
  152. static void
  153. CreateGC(w)
  154. Widget w;
  155. {
  156.     SmeLineObject entry = (SmeLineObject) w;
  157.     XGCValues values;
  158.     XtGCMask mask = GCForeground | GCGraphicsExposures | GCLineWidth ;
  159.     
  160.     values.foreground = entry->sme_line.foreground;
  161.     values.graphics_exposures = FALSE;
  162.     values.line_width = entry->sme_line.line_width;
  163.     
  164.     if (entry->sme_line.stipple != XtUnspecifiedPixmap) {
  165.     values.stipple = entry->sme_line.stipple;
  166.     values.fill_style = FillStippled; 
  167.     mask |= GCStipple | GCFillStyle;
  168.     
  169.     entry->sme_line.gc = XCreateGC(XtDisplayOfObject(w), 
  170.                       RootWindowOfScreen(XtScreenOfObject(w)),
  171.                       mask, &values);
  172.     }
  173.     else
  174.     entry->sme_line.gc = XtGetGC(w, mask, &values);
  175. }
  176.  
  177. /*    Function Name: DestroyGC
  178.  *    Description: Destroys the GC when we are done with it.
  179.  *    Arguments: w - the Line entry widget.
  180.  *    Returns: none
  181.  */
  182.  
  183. static void
  184. DestroyGC(w)
  185. Widget w;
  186. {
  187.     SmeLineObject entry = (SmeLineObject) w;
  188.  
  189.     if (entry->sme_line.stipple != XtUnspecifiedPixmap) 
  190.     XFreeGC(XtDisplayOfObject(w), entry->sme_line.gc);
  191.     else
  192.     XtReleaseGC(w, entry->sme_line.gc);
  193. }
  194.  
  195. /*    Function Name: Redisplay
  196.  *    Description: Paints the Line
  197.  *    Arguments: w - the menu entry.
  198.  *                 event, region - NOT USED.
  199.  *    Returns: none
  200.  */
  201.  
  202. /*ARGSUSED*/
  203. static void
  204. Redisplay(w, event, region)
  205. Widget w;
  206. XEvent * event;
  207. Region region;
  208. {
  209.     SmeLineObject entry = (SmeLineObject) w;
  210.     int y = entry->rectangle.y + 
  211.         (int)(entry->rectangle.height - entry->sme_line.line_width) / 2;
  212.  
  213.     if (entry->sme_line.stipple != XtUnspecifiedPixmap) 
  214.     XSetTSOrigin(XtDisplayOfObject(w), entry->sme_line.gc, 0, y);
  215.  
  216.     XFillRectangle(XtDisplayOfObject(w), XtWindowOfObject(w),
  217.            entry->sme_line.gc, 
  218.            0, y, (unsigned int) entry->rectangle.width, 
  219.            (unsigned int) entry->sme_line.line_width );
  220. }
  221.  
  222. /*      Function Name: SetValues
  223.  *      Description: Relayout the menu when one of the resources is changed.
  224.  *      Arguments: current - current state of the widget.
  225.  *                 request - what was requested.
  226.  *                 new - what the widget will become.
  227.  *      Returns: none
  228.  */
  229.  
  230. /* ARGSUSED */
  231. static Boolean
  232. SetValues(current, request, new)
  233. Widget current, request, new;
  234. {
  235.     SmeLineObject entry = (SmeLineObject) new;
  236.     SmeLineObject old_entry = (SmeLineObject) current;
  237.   
  238.     if ( (entry->sme_line.line_width != old_entry->sme_line.line_width) &&
  239.      (entry->sme_line.stipple != old_entry->sme_line.stipple) ) {
  240.     DestroyGC(current);
  241.     CreateGC(new);
  242.     return(TRUE);
  243.     }
  244.     return(FALSE);
  245. }
  246.  
  247. /*    Function Name: QueryGeometry.
  248.  *    Description: Returns the preferred geometry for this widget.
  249.  *    Arguments: w - the menu entry object.
  250.  *                 itended, return - the intended and return geometry info.
  251.  *    Returns: A Geometry Result.
  252.  *
  253.  * See the Intrinsics manual for details on what this function is for.
  254.  * 
  255.  * I just return the height and a width of 1.
  256.  */
  257.  
  258. static XtGeometryResult
  259. QueryGeometry(w, intended, return_val) 
  260. Widget w;
  261. XtWidgetGeometry *intended, *return_val;
  262. {
  263.     SmeObject entry = (SmeObject) w;
  264.     Dimension width;
  265.     XtGeometryResult ret_val = XtGeometryYes;
  266.     XtGeometryMask mode = intended->request_mode;
  267.  
  268.     width = 1;            /* we can be really small. */
  269.  
  270.     if ( ((mode & CWWidth) && (intended->width != width)) ||
  271.      !(mode & CWWidth) ) {
  272.     return_val->request_mode |= CWWidth;
  273.     return_val->width = width;
  274.     mode = return_val->request_mode;
  275.     
  276.     if ( (mode & CWWidth) && (width == entry->rectangle.width) )
  277.         return(XtGeometryNo);
  278.     return(XtGeometryAlmost);
  279.     }
  280.     return(ret_val);
  281. }
  282.