home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / Arrow.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  16.5 KB  |  528 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*-----------------------------------------*/
  19. /*                                                                        */
  20. /* Name:        <Xfe/Arrow.c>                                            */
  21. /* Description:    XfeArrow widget source.                                    */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <Xfe/ArrowP.h>
  28. #include <Xfe/ManagerP.h>
  29. #include <Xm/RowColumnP.h>
  30.  
  31. #define ARROW_GC(w,ap) \
  32. (_XfeIsSensitive(w) ? _XfeLabelGetLabelGC(w) : ap->arrow_insens_GC)
  33.  
  34. #define ARMED_GC(w,bp) \
  35. ((bp->fill_on_arm && bp->armed) ? bp->armed_GC : _XfeBackgroundGC(w))
  36.  
  37. #define RAISED_GC(w,bp) \
  38. ((bp->raise_on_enter && bp->raised) ? bp->label_raised_GC : _XfeBackgroundGC(w))
  39.  
  40. /*----------------------------------------------------------------------*/
  41. /*                                                                        */
  42. /* Warnings and messages                                                */
  43. /*                                                                        */
  44. /*----------------------------------------------------------------------*/
  45. #define MESSAGE1 "Widget is not a XfeButton."
  46.  
  47. /*----------------------------------------------------------------------*/
  48. /*                                                                        */
  49. /* Core class methods                                                    */
  50. /*                                                                        */
  51. /*----------------------------------------------------------------------*/
  52. static void     Initialize        (Widget,Widget,ArgList,Cardinal *);
  53. static void     Destroy            (Widget);
  54. static Boolean    SetValues        (Widget,Widget,Widget,ArgList,Cardinal *);
  55.  
  56. /*----------------------------------------------------------------------*/
  57. /*                                                                        */
  58. /* XfePrimitive class methods                                            */
  59. /*                                                                        */
  60. /*----------------------------------------------------------------------*/
  61. static void    PreferredGeometry    (Widget,Dimension *,Dimension *);
  62. static void    DrawBackground        (Widget,XEvent *,Region,XRectangle *);
  63. static void    DrawComponents        (Widget,XEvent *,Region,XRectangle *);
  64.  
  65. /*----------------------------------------------------------------------*/
  66. /*                                                                        */
  67. /* Misc XfeArrow functions                                                */
  68. /*                                                                        */
  69. /*----------------------------------------------------------------------*/
  70. static void    DrawArrow                (Widget,XEvent *,Region,XRectangle *);
  71.  
  72. /*----------------------------------------------------------------------*/
  73. /*                                                                        */
  74. /* Component Preparation codes                                            */
  75. /*                                                                        */
  76. /*----------------------------------------------------------------------*/
  77.  
  78. /*----------------------------------------------------------------------*/
  79. /*                                                                        */
  80. /* XfeArrow Resources                                                    */
  81. /*                                                                        */
  82. /*----------------------------------------------------------------------*/
  83. static XtResource resources[] = 
  84. {
  85.     /* Arrow resources */
  86.     { 
  87.         XmNarrowDirection,
  88.         XmCArrowDirection,
  89.         XmRArrowDirection,
  90.         sizeof(unsigned char),
  91.         XtOffsetOf(XfeArrowRec , xfe_arrow . arrow_direction),
  92.         XmRImmediate, 
  93.         (XtPointer) XmARROW_DOWN
  94.     },
  95.     { 
  96.         XmNarrowWidth,
  97.         XmCArrowWidth,
  98.         XmRHorizontalDimension,
  99.         sizeof(Dimension),
  100.         XtOffsetOf(XfeArrowRec , xfe_arrow . arrow_width),
  101.         XmRImmediate, 
  102.         (XtPointer) 12
  103.     },
  104.     { 
  105.         XmNarrowHeight,
  106.         XmCArrowHeight,
  107.         XmRVerticalDimension,
  108.         sizeof(Dimension),
  109.         XtOffsetOf(XfeArrowRec , xfe_arrow . arrow_height),
  110.         XmRImmediate, 
  111.         (XtPointer) 12
  112.     },
  113.  
  114.     /* Force XmNbuttonLayout to XmBUTTON_PIXMAP_ONLY */
  115.     { 
  116.         XmNbuttonLayout,
  117.         XmCButtonLayout,
  118.         XmRButtonLayout,
  119.         sizeof(unsigned char),
  120.         XtOffsetOf(XfeArrowRec , xfe_button . button_layout),
  121.         XmRImmediate, 
  122.         (XtPointer) XmBUTTON_PIXMAP_ONLY
  123.     },
  124.  
  125.     /* Force margins to 2 */
  126.     { 
  127.         XmNmarginBottom,
  128.         XmCMarginBottom,
  129.         XmRVerticalDimension,
  130.         sizeof(Dimension),
  131.         XtOffsetOf(XfeArrowRec , xfe_primitive . margin_bottom),
  132.         XmRImmediate, 
  133.         (XtPointer) 2
  134.     },
  135.     { 
  136.         XmNmarginLeft,
  137.         XmCMarginLeft,
  138.         XmRHorizontalDimension,
  139.         sizeof(Dimension),
  140.         XtOffsetOf(XfeArrowRec , xfe_primitive . margin_left),
  141.         XmRImmediate, 
  142.         (XtPointer) 2
  143.     },
  144.     { 
  145.         XmNmarginRight,
  146.         XmCMarginRight,
  147.         XmRHorizontalDimension,
  148.         sizeof(Dimension),
  149.         XtOffsetOf(XfeArrowRec , xfe_primitive . margin_right),
  150.         XmRImmediate, 
  151.         (XtPointer) 2
  152.     },
  153.     { 
  154.         XmNmarginTop,
  155.         XmCMarginTop,
  156.         XmRVerticalDimension,
  157.         sizeof(Dimension),
  158.         XtOffsetOf(XfeArrowRec , xfe_primitive . margin_top),
  159.         XmRImmediate, 
  160.         (XtPointer) 2
  161.     },
  162.  
  163.     /* Force XmNshadowThickness to 0 */
  164.     { 
  165.         XmNshadowThickness,
  166.         XmCShadowThickness,
  167.         XmRHorizontalDimension,
  168.         sizeof(Dimension),
  169.         XtOffsetOf(XfeArrowRec , primitive . shadow_thickness),
  170.         XmRImmediate, 
  171.         (XtPointer) 0
  172.     },
  173. };
  174.  
  175. /*----------------------------------------------------------------------*/
  176. /*                                                                        */
  177. /* XfeButton Synthetic Resources                                        */
  178. /*                                                                        */
  179. /*----------------------------------------------------------------------*/
  180. static XmSyntheticResource syn_resources[] =
  181. {
  182.     { 
  183.         XmNarrowHeight,
  184.         sizeof(Dimension),
  185.         XtOffsetOf(XfeArrowRec , xfe_arrow . arrow_height),
  186.         _XmFromHorizontalPixels,
  187.         _XmToHorizontalPixels 
  188.     },
  189.     { 
  190.         XmNarrowWidth,
  191.         sizeof(Dimension),
  192.         XtOffsetOf(XfeArrowRec , xfe_arrow . arrow_width),
  193.         _XmFromVerticalPixels,
  194.         _XmToVerticalPixels 
  195.     },
  196. };
  197.  
  198. /*----------------------------------------------------------------------*/
  199. /*                                                                        */
  200. /* XfeArrow widget class record initialization                            */
  201. /*                                                                        */
  202. /*----------------------------------------------------------------------*/
  203. _XFE_WIDGET_CLASS_RECORD(arrow,Arrow) =
  204. {
  205.     {
  206.         /* Core Part */
  207.         (WidgetClass) &xfeButtonClassRec,        /* superclass             */
  208.         "XfeArrow",                                /* class_name             */
  209.         sizeof(XfeArrowRec),                    /* widget_size            */
  210.         NULL,                                    /* class_initialize       */
  211.         NULL,                                    /* class_part_initialize*/
  212.         FALSE,                                  /* class_inited           */
  213.         Initialize,                             /* initialize             */
  214.         NULL,                                   /* initialize_hook        */
  215.         XtInheritRealize,                       /* realize                */
  216.         NULL,                                    /* actions                */
  217.         0,                                        /* num_actions            */
  218.         resources,                              /* resources              */
  219.         XtNumber(resources),                    /* num_resources          */
  220.         NULLQUARK,                              /* xrm_class              */
  221.         TRUE,                                   /* compress_motion        */
  222.         XtExposeCompressMaximal,                /* compress_exposure      */
  223.         TRUE,                                   /* compress_enterleave    */
  224.         FALSE,                                  /* visible_interest       */
  225.         Destroy,                                /* destroy                */
  226.         XtInheritResize,                        /* resize                 */
  227.         XtInheritExpose,                        /* expose                 */
  228.         SetValues,                              /* set_values             */
  229.         NULL,                                   /* set_values_hook        */
  230.         XtInheritSetValuesAlmost,                /* set_values_almost      */
  231.         NULL,                                    /* get_values_hook        */
  232.         NULL,                                   /* accept_focus           */
  233.         XtVersion,                              /* version                */
  234.         NULL,                                   /* callback_private       */
  235.         XtInheritTranslations,                    /* tm_table               */
  236.         XtInheritQueryGeometry,                    /* query_geometry         */
  237.         XtInheritDisplayAccelerator,            /* display accel          */
  238.         NULL,                                   /* extension              */
  239.     },
  240.  
  241.     /* XmPrimitive Part */
  242.     {
  243.         XmInheritBorderHighlight,                /* border_highlight        */
  244.         XmInheritBorderUnhighlight,                /* border_unhighlight     */
  245.         XtInheritTranslations,                  /* translations           */
  246.         XmInheritArmAndActivate,                /* arm_and_activate       */
  247.         syn_resources,                            /* syn resources          */
  248.         XtNumber(syn_resources),                /* num syn_resources      */
  249.         NULL,                                    /* extension              */
  250.     },
  251.     
  252.     /* XfePrimitive Part */
  253.     {
  254.         XfeInheritBitGravity,                    /* bit_gravity            */
  255.         PreferredGeometry,                        /* preferred_geometry    */
  256.         XfeInheritMinimumGeometry,                /* minimum_geometry        */
  257.         XfeInheritUpdateRect,                    /* update_rect            */
  258.         NULL,                                    /* prepare_components    */
  259.         XfeInheritLayoutComponents,                /* layout_components    */
  260.         DrawBackground,                            /* draw_background        */
  261.         XfeInheritDrawShadow,                    /* draw_shadow            */
  262.         DrawComponents,                            /* draw_components        */
  263.         NULL,                                    /* extension            */
  264.     },
  265.  
  266.     /* XfeLabel Part */
  267.     {
  268.         XfeInheritLayoutString,                    /* layout_string        */
  269.         XfeInheritDrawString,                    /* draw_string            */
  270.         XfeInheritDrawSelection,                /* draw_selection        */
  271.         XfeInheritGetLabelGC,                    /* get_label_gc            */
  272.         XfeInheritGetSelectionGC,                /* get_selection_gc        */
  273.         NULL,                                    /* extension            */
  274.     },
  275.  
  276.     /* XfeButton Part */
  277.     {
  278.         XfeInheritLayoutPixmap,                    /* layout_pixmap        */
  279.         XfeInheritDrawPixmap,                    /* draw_pixmap            */
  280.         XfeInheritDrawRaiseBorder,                /* draw_raise_border    */
  281.         XfeInheritArmTimeout,                    /* arm_timeout            */
  282.         NULL,                                    /* extension            */
  283.     },
  284.  
  285.     /* XfeArrow Part */
  286.     {
  287.         NULL,                                    /* extension            */
  288.     },
  289. };
  290.  
  291. /*----------------------------------------------------------------------*/
  292. /*                                                                        */
  293. /* xfeArrowWidgetClass declaration.                                        */
  294. /*                                                                        */
  295. /*----------------------------------------------------------------------*/
  296. _XFE_WIDGET_CLASS(arrow,Arrow);
  297.  
  298. /*----------------------------------------------------------------------*/
  299. /*                                                                        */
  300. /* Core class methods                                                    */
  301. /*                                                                        */
  302. /*----------------------------------------------------------------------*/
  303. static void
  304. Initialize(Widget rw,Widget nw,ArgList args,Cardinal *nargs)
  305. {
  306.     XfeArrowPart *    ap = _XfeArrowPart(nw);
  307.  
  308.     /* Make sure rep types are ok */
  309.     XfeRepTypeCheck(nw,XmRArrowDirection,&ap->arrow_direction,XmARROW_DOWN);
  310.  
  311.     /* Allocate the insensitve GC  */
  312.     ap->arrow_insens_GC = XfeAllocateColorGc(nw,_XfeForeground(nw),_XfeBackgroundPixel(nw),False);
  313.  
  314.     /* Finish of initialization */
  315.     _XfePrimitiveChainInitialize(rw,nw,xfeArrowWidgetClass);
  316. }
  317. /*----------------------------------------------------------------------*/
  318. static void
  319. Destroy(Widget w)
  320. {
  321.     XfeArrowPart *    ap = _XfeArrowPart(w);
  322.  
  323.     XtReleaseGC(w,ap->arrow_insens_GC);
  324. }
  325. /*----------------------------------------------------------------------*/
  326. static Boolean
  327. SetValues(Widget ow,Widget rw,Widget nw,ArgList args,Cardinal *nargs)
  328. {
  329.     XfeArrowPart *    np = _XfeArrowPart(nw);
  330.     XfeArrowPart *    op = _XfeArrowPart(ow);
  331.  
  332.     /* arrow_direction */
  333.     if (np->arrow_direction != op->arrow_direction)
  334.     {
  335.         /* Make sure arrow direction is ok */
  336.         XfeRepTypeCheck(nw,XmRArrowDirection,&np->arrow_direction,
  337.                         XmARROW_DOWN);
  338.  
  339.         _XfeConfigFlags(nw) |= XfeConfigExpose;
  340.     }
  341.  
  342.     /* arrow_width */
  343.     if ((np->arrow_width != op->arrow_width) || 
  344.         (np->arrow_height != op->arrow_height))
  345.     {
  346.         _XfeConfigFlags(nw) |= XfeConfigGLE;
  347.     }
  348.  
  349.     /* foreground or background */
  350.     if ((_XfeForeground(nw) != _XfeForeground(ow)) ||
  351.         (_XfeBackgroundPixel(nw) != _XfeBackgroundPixel(ow)))
  352.     {
  353.         XtReleaseGC(nw,np->arrow_insens_GC);
  354.  
  355.         np->arrow_insens_GC = XfeAllocateColorGc(nw,_XfeForeground(nw),_XfeBackgroundPixel(nw),False);
  356.         
  357.     }
  358.  
  359.     return _XfePrimitiveChainSetValues(ow,rw,nw,xfeArrowWidgetClass);
  360. }
  361. /*----------------------------------------------------------------------*/
  362.  
  363.  
  364. /*----------------------------------------------------------------------*/
  365. /*                                                                        */
  366. /* XfePrimitive methods                                                    */
  367. /*                                                                        */
  368. /*----------------------------------------------------------------------*/
  369. static void
  370. PreferredGeometry(Widget w,Dimension *width,Dimension *height)
  371. {
  372.     XfeArrowPart *    ap = _XfeArrowPart(w);
  373.     XfeButtonPart *    bp = _XfeButtonPart(w);
  374.  
  375.     *width  = 
  376.         _XfeOffsetLeft(w) + _XfeOffsetRight(w) + ap->arrow_width;
  377.  
  378.     *height = 
  379.         _XfeOffsetTop(w)  + _XfeOffsetBottom(w) + ap->arrow_height;
  380.  
  381.     /* Include the raise_border_thickenss if needed */
  382.     if (bp->raise_on_enter)
  383.     {
  384.         *width  += (2 * bp->raise_border_thickness);
  385.         *height += (2 * bp->raise_border_thickness);
  386.     }
  387. }
  388. /*----------------------------------------------------------------------*/
  389. static void
  390. DrawBackground(Widget w,XEvent *event,Region region,XRectangle * clip_rect)
  391. {
  392.     XfeArrowPart *    ap = _XfeArrowPart(w);
  393.     XfeButtonPart *    bp = _XfeButtonPart(w);
  394.  
  395.     if (bp->emulate_motif)
  396.     {
  397.         return;
  398.     }
  399.  
  400. #if 0    
  401.     (*xfeButtonClassRec.xfe_primitive_class.draw_background)(w,
  402.                                                              event,
  403.                                                              region,
  404.                                                              clip_rect);
  405. #else
  406.     /* Fill the background if needed */
  407.     if (bp->fill_on_arm && bp->armed)
  408.     {
  409.         XFillRectangle(XtDisplay(w),
  410.                        _XfePrimitiveDrawable(w),
  411.                        bp->armed_GC,
  412.                        0,0,
  413.                        _XfeWidth(w),_XfeHeight(w));
  414.     }
  415.     else if (bp->fill_on_enter && (bp->raised || _XfePointerInside(w)))
  416.     {
  417.         XFillRectangle(XtDisplay(w),
  418.                        _XfePrimitiveDrawable(w),
  419.                        bp->label_raised_GC,
  420.                        0,0,
  421.                        _XfeWidth(w),_XfeHeight(w));
  422.     }
  423. #endif
  424. }
  425. /*----------------------------------------------------------------------*/
  426. static void
  427. DrawComponents(Widget w,XEvent *event,Region region,XRectangle * clip_rect)
  428. {
  429.     /* Invoke draw_string method */
  430.     _XfeLabelDrawString(w,event,region,clip_rect);
  431.  
  432.     /* Invoke draw_pixmap method */
  433.     _XfeButtonDrawPixmap(w,event,region,clip_rect);
  434.  
  435.     /* Invoke draw_border method */
  436.     _XfeButtonDrawRaiseBorder(w,event,region,clip_rect);
  437.  
  438.     /* Draw the arrow */
  439.     DrawArrow(w,event,region,clip_rect);
  440. }
  441. /*----------------------------------------------------------------------*/
  442.  
  443. /*----------------------------------------------------------------------*/
  444. /*                                                                        */
  445. /* Misc XfeArrow functions                                                */
  446. /*                                                                        */
  447. /*----------------------------------------------------------------------*/
  448. static void
  449. DrawArrow(Widget w,XEvent *event,Region region,XRectangle * clip_rect)
  450. {
  451.     XfeArrowPart *        ap = _XfeArrowPart(w);
  452.     XfeButtonPart *        bp = _XfeButtonPart(w);
  453.     Dimension            width = XfeMin(ap->arrow_width,_XfeRectWidth(w));
  454.     Dimension            height = XfeMin(ap->arrow_height,_XfeRectHeight(w));
  455.     Position            x = (_XfeWidth(w) - width) / 2;
  456.     Position            y = (_XfeHeight(w) - height) / 2;
  457.  
  458.     /* Draw the arrow as an XmArrowButton would */
  459.     if (bp->emulate_motif)
  460.     {
  461.         GC gc = _XfeBackgroundGC(w);
  462.  
  463.         if (bp->raised)
  464.         {
  465.             if (bp->raise_on_enter && bp->raised)
  466.             {
  467.                 gc = bp->label_raised_GC;
  468.             }
  469.         }
  470.         else if (bp->armed)
  471.         {
  472.             if (bp->fill_on_arm && bp->armed)
  473.             {
  474.                 gc = bp->armed_GC;
  475.             }
  476.         }
  477.  
  478.         XfeDrawMotifArrow(XtDisplay(w),
  479.                           _XfePrimitiveDrawable(w), 
  480.                           _XfeTopShadowGC(w),
  481.                           _XfeBottomShadowGC(w),
  482.                           gc,
  483.                           x,
  484.                           y, 
  485.                           width,
  486.                           height,
  487.                           ap->arrow_direction,
  488.                           2, /* hard coded to be compatible with Motif */
  489.                           bp->armed);
  490.     }
  491.     /* Draw the arrow as an XfeButton would */
  492.     else
  493.     {
  494. #if 1
  495.         XfeDrawArrow(XtDisplay(w),
  496.                      _XfePrimitiveDrawable(w),
  497.                      ARROW_GC(w,ap),
  498.                      x,y,
  499.                      width,height,
  500.                      ap->arrow_direction);
  501. #else
  502.         XfeDrawMotifArrow(XtDisplay(w),
  503.                           _XfePrimitiveDrawable(w), 
  504.                           NULL,
  505.                           NULL,
  506.                           ARROW_GC(w,ap),
  507.                           x,y, 
  508.                           width,height,
  509.                           ap->arrow_direction,
  510.                           0,
  511.                           False);
  512. #endif
  513.     }
  514. }
  515. /*----------------------------------------------------------------------*/
  516.  
  517. /*----------------------------------------------------------------------*/
  518. /*                                                                        */
  519. /* XfeArrow Public Methods                                                */
  520. /*                                                                        */
  521. /*----------------------------------------------------------------------*/
  522. Widget
  523. XfeCreateArrow(Widget pw,char * name,Arg * av,Cardinal ac)
  524. {
  525.     return XtCreateWidget(name,xfeArrowWidgetClass,pw,av,ac);
  526. }
  527. /*----------------------------------------------------------------------*/
  528.