home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / ProgressBar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  24.9 KB  |  906 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/ProgressBar.c>                                        */
  21. /* Description:    XfeProgressBar widget source.                            */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27.  
  28. #include <Xfe/ProgressBarP.h>
  29.  
  30. /*----------------------------------------------------------------------*/
  31. /*                                                                        */
  32. /* Warnings and messages                                                */
  33. /*                                                                        */
  34. /*----------------------------------------------------------------------*/
  35. #define MESSAGE1  "Widget is not a XfeProgressBar."
  36. #define MESSAGE2  "XmNstartPercent must be >= than 0."
  37. #define MESSAGE3  "XmNstartPercent must be <= than 100."
  38. #define MESSAGE4  "XmNendPercent must be >= than 0."
  39. #define MESSAGE5  "XmNendPercent must be <= than 100."
  40. #define MESSAGE6  "XmNendPercent must be greater than XmNstartPercent."
  41. #define MESSAGE7  "XmNcylonRunning is a read-only resource."
  42. #define MESSAGE8  "XmNcylonWidth must be >= 1."
  43. #define MESSAGE9  "XmNcylonWidth must be <= 50."
  44. #define MESSAGE10 "XmNcylonOffset must be >= 1."
  45. #define MESSAGE11 "XmNcylonOffset must be <= 50."
  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    LayoutComponents    (Widget);
  62. static void    DrawComponents        (Widget,XEvent *,Region,XRectangle *);
  63.  
  64. /*----------------------------------------------------------------------*/
  65. /*                                                                        */
  66. /* Misc XfeProgressBar functions                                        */
  67. /*                                                                        */
  68. /*----------------------------------------------------------------------*/
  69. static void    BarDraw                (Widget,Region,XRectangle *);
  70. static void    BarLayout            (Widget);
  71. static void    CheckPercentages    (Widget);
  72. static void    CheckCylonWidth        (Widget);
  73. static void    CheckCylonOffset    (Widget);
  74.  
  75. /*----------------------------------------------------------------------*/
  76. /*                                                                        */
  77. /* Misc cylon functions                                                    */
  78. /*                                                                        */
  79. /*----------------------------------------------------------------------*/
  80. static void            CylonTimeout        (XtPointer,XtIntervalId *);
  81. static void            RemoveTimeout        (Widget);
  82. static void            AddTimeout            (Widget);
  83. static void            CheckCylonWidth        (Widget);
  84.  
  85. /*----------------------------------------------------------------------*/
  86. /*                                                                        */
  87. /* XfeProgressBar Resources                                                */
  88. /*                                                                        */
  89. /*----------------------------------------------------------------------*/
  90. static XtResource resources[] = 
  91. {
  92.     /* Resources */
  93.     { 
  94.         XmNbarColor,
  95.         XmCBarColor,
  96.         XmRPixel,
  97.         sizeof(Pixel),
  98.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . bar_color),
  99.         XmRCallProc, 
  100.         (XtPointer) _XfeCallProcSelectPixel
  101.     },
  102.     { 
  103.         XmNstartPercent,
  104.         XmCStartPercent,
  105.         XmRInt,
  106.         sizeof(int),
  107.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . start_percent),
  108.         XmRImmediate, 
  109.         (XtPointer) 0
  110.     },
  111.     { 
  112.         XmNendPercent,
  113.         XmCEndPercent,
  114.         XmRInt,
  115.         sizeof(int),
  116.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . end_percent),
  117.         XmRImmediate, 
  118.         (XtPointer) 0
  119.     },
  120.     { 
  121.         XmNcylonInterval,
  122.         XmCCylonInterval,
  123.         XmRInt,
  124.         sizeof(int),
  125.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . cylon_interval),
  126.         XmRImmediate, 
  127.         (XtPointer) XfeDEFAULT_CYLON_INTERVAL
  128.     },
  129.     { 
  130.         XmNcylonOffset,
  131.         XmCCylonOffset,
  132.         XmRInt,
  133.         sizeof(int),
  134.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . cylon_offset),
  135.         XmRImmediate, 
  136.         (XtPointer) 2
  137.     },
  138.     { 
  139.         XmNcylonRunning,
  140.         XmCReadOnly,
  141.         XmRBoolean,
  142.         sizeof(Boolean),
  143.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . cylon_running),
  144.         XmRImmediate, 
  145.         (XtPointer) False
  146.     },
  147.     { 
  148.         XmNcylonWidth,
  149.         XmCCylonWidth,
  150.         XmRInt,
  151.         sizeof(int),
  152.         XtOffsetOf(XfeProgressBarRec , xfe_progress_bar . cylon_width),
  153.         XmRImmediate, 
  154.         (XtPointer) 20
  155.     },
  156.  
  157.     /* Force margins to 0 */
  158.     { 
  159.         XmNmarginBottom,
  160.         XmCMarginBottom,
  161.         XmRVerticalDimension,
  162.         sizeof(Dimension),
  163.         XtOffsetOf(XfeProgressBarRec , xfe_primitive . margin_bottom),
  164.         XmRImmediate, 
  165.         (XtPointer) 0
  166.     },
  167.     { 
  168.         XmNmarginLeft,
  169.         XmCMarginLeft,
  170.         XmRHorizontalDimension,
  171.         sizeof(Dimension),
  172.         XtOffsetOf(XfeProgressBarRec , xfe_primitive . margin_left),
  173.         XmRImmediate, 
  174.         (XtPointer) 0
  175.     },
  176.     { 
  177.         XmNmarginRight,
  178.         XmCMarginRight,
  179.         XmRHorizontalDimension,
  180.         sizeof(Dimension),
  181.         XtOffsetOf(XfeProgressBarRec , xfe_primitive . margin_right),
  182.         XmRImmediate, 
  183.         (XtPointer) 0
  184.     },
  185.     { 
  186.         XmNmarginTop,
  187.         XmCMarginTop,
  188.         XmRVerticalDimension,
  189.         sizeof(Dimension),
  190.         XtOffsetOf(XfeProgressBarRec , xfe_primitive . margin_top),
  191.         XmRImmediate, 
  192.         (XtPointer) 0
  193.     },
  194.     
  195.     /* Force the shadow type to XmSHADOW_IN */
  196.     { 
  197.         XmNshadowType,
  198.         XmCShadowType,
  199.         XmRShadowType,
  200.         sizeof(unsigned char),
  201.         XtOffsetOf(XfeProgressBarRec , xfe_primitive . shadow_type),
  202.         XmRImmediate, 
  203.         (XtPointer) XmSHADOW_IN
  204.     },
  205.  
  206.     /* Force buffer type to XmBUFFER_PRIVATE */
  207.     { 
  208.         XmNbufferType,
  209.         XmCBufferType,
  210.         XmRBufferType,
  211.         sizeof(unsigned char),
  212.         XtOffsetOf(XfeProgressBarRec , xfe_primitive . buffer_type),
  213.         XmRImmediate, 
  214.         (XtPointer) XmBUFFER_PRIVATE
  215.     },
  216. };
  217.  
  218. /*----------------------------------------------------------------------*/
  219. /*                                                                        */
  220. /* XfeProgressBar widget class record initialization                    */
  221. /*                                                                        */
  222. /*----------------------------------------------------------------------*/
  223. _XFE_WIDGET_CLASS_RECORD(progressbar,ProgressBar) =
  224. {
  225.     {
  226.         /* Core Part */
  227.         (WidgetClass) &xfeLabelClassRec,        /* superclass             */
  228.         "XfeProgressBar",                        /* class_name            */
  229.         sizeof(XfeProgressBarRec),                /* widget_size            */
  230.         NULL,                                    /* class_initialize       */
  231.         NULL,                                    /* class_part_initialize*/
  232.         FALSE,                                  /* class_inited           */
  233.         Initialize,                             /* initialize             */
  234.         NULL,                                   /* initialize_hook        */
  235.         XtInheritRealize,                       /* realize                */
  236.         NULL,                                    /* actions                */
  237.         0,                                        /* num_actions            */
  238.         resources,                              /* resources              */
  239.         XtNumber(resources),                    /* num_resources          */
  240.         NULLQUARK,                              /* xrm_class              */
  241.         TRUE,                                   /* compress_motion        */
  242.         XtExposeCompressMaximal,                /* compress_exposure      */
  243.         TRUE,                                   /* compress_enterleave    */
  244.         FALSE,                                  /* visible_interest       */
  245.         Destroy,                                /* destroy                */
  246.         XtInheritResize,                        /* resize                 */
  247.         XtInheritExpose,                        /* expose                 */
  248.         SetValues,                              /* set_values             */
  249.         NULL,                                   /* set_values_hook        */
  250.         XtInheritSetValuesAlmost,                /* set_values_almost      */
  251.         NULL,                                    /* get_values_hook        */
  252.         NULL,                                   /* accept_focus           */
  253.         XtVersion,                              /* version                */
  254.         NULL,                                   /* callback_private       */
  255.         XtInheritTranslations,                    /* tm_table               */
  256.         XtInheritQueryGeometry,                    /* query_geometry         */
  257.         XtInheritDisplayAccelerator,            /* display accel          */
  258.         NULL,                                   /* extension              */
  259.     },
  260.  
  261.     /* XmPrimitive Part */
  262.     {
  263.         XmInheritBorderHighlight,                /* border_highlight     */
  264.         XmInheritBorderUnhighlight,                /* border_unhighlight     */
  265.         XtInheritTranslations,                  /* translations           */
  266.         NULL,                                    /* arm_and_activate       */
  267.         NULL,                                    /* syn resources          */
  268.         0,                                        /* num syn_resources      */
  269.         NULL,                                    /* extension              */
  270.     },
  271.  
  272.     /* XfePrimitive Part */
  273.     {
  274.         XfeInheritBitGravity,                    /* bit_gravity            */
  275.         XfeInheritPreferredGeometry,            /* preferred_geometry    */
  276.         XfeInheritMinimumGeometry,                /* minimum_geometry        */
  277.         XfeInheritUpdateRect,                    /* update_rect            */
  278.         NULL,                                    /* prepare_components    */
  279.         LayoutComponents,                        /* layout_components    */
  280.         XfeInheritDrawBackground,                /* draw_background        */
  281.         XfeInheritDrawShadow,                    /* draw_shadow            */
  282.         DrawComponents,                            /* draw_components        */
  283.         NULL,                                    /* extension            */
  284.     },
  285.  
  286.     /* XfeLabel Part */
  287.     {
  288.         XfeInheritLayoutString,                    /* layout_string        */
  289.         XfeInheritDrawString,                    /* draw_string            */
  290.         XfeInheritDrawSelection,                /* draw_selection        */
  291.         XfeInheritGetLabelGC,                    /* get_label_gc            */
  292.         XfeInheritGetSelectionGC,                /* get_selection_gc        */
  293.         NULL,                                    /* extension            */
  294.     },
  295.  
  296.     /* XfeProgressBar Part */
  297.     {
  298.         NULL,                                    /* extension            */
  299.     },
  300. };
  301.  
  302. /*----------------------------------------------------------------------*/
  303. /*                                                                        */
  304. /* xfeProgressBarWidgetClass declaration.                                */
  305. /*                                                                        */
  306. /*----------------------------------------------------------------------*/
  307. _XFE_WIDGET_CLASS(progressbar,ProgressBar);
  308.  
  309. /*----------------------------------------------------------------------*/
  310. /*                                                                        */
  311. /* Core Class methods                                                    */
  312. /*                                                                        */
  313. /*----------------------------------------------------------------------*/
  314. static void
  315. Initialize(Widget rw,Widget nw,ArgList args,Cardinal *nargs)
  316. {
  317.     XfeProgressBarPart * pp = _XfeProgressBarPart(nw);
  318.  
  319.     /* Check the percentages */
  320.     CheckPercentages(nw);
  321.  
  322.     /* Check cylon width */
  323.     CheckCylonWidth(nw);
  324.  
  325.     /* Check cylon offset */
  326.     CheckCylonOffset(nw);
  327.  
  328.     /* Allocate the bar GCs */
  329.     pp->bar_GC = XfeAllocateColorGc(nw,pp->bar_color,None,True);
  330.     
  331.     pp->bar_insens_GC = XfeAllocateColorGc(nw,pp->bar_color,
  332.                                            _XfeBackgroundPixel(nw),
  333.                                            False);
  334.  
  335.     /* Initialize private members */
  336.     pp->timer_id        = 0;
  337.     pp->cylon_direction    = True;
  338.     pp->cylon_index        = 0;
  339.  
  340.     /* Finish of initialization */
  341.     _XfePrimitiveChainInitialize(rw,nw,xfeProgressBarWidgetClass);
  342. }
  343. /*----------------------------------------------------------------------*/
  344. static void
  345. Destroy(Widget w)
  346. {
  347.     XfeProgressBarPart * pp = _XfeProgressBarPart(w);
  348.  
  349.     /* Release GCs */
  350.     XtReleaseGC(w,pp->bar_GC);
  351.     XtReleaseGC(w,pp->bar_insens_GC);
  352.  
  353.     /* Remove the timeout if needed */
  354.     RemoveTimeout(w);
  355. }
  356. /*----------------------------------------------------------------------*/
  357. static Boolean
  358. SetValues(Widget ow,Widget rw,Widget nw,ArgList args,Cardinal *nargs)
  359. {
  360.     XfeProgressBarPart *    np = _XfeProgressBarPart(nw);
  361.     XfeProgressBarPart *    op = _XfeProgressBarPart(ow);
  362.     Boolean                    bar_gc_flag = False;
  363.     Boolean                    bar_insens_gc_flag = False;
  364.     Boolean                    stop_cylon = False;
  365.  
  366.     /* cylon_running */
  367.     if (np->cylon_running != op->cylon_running)
  368.     {
  369.         np->cylon_running = op->cylon_running;
  370.  
  371.         _XfeWarning(nw,MESSAGE7);
  372.     }
  373.  
  374.     /* cylon_width */
  375.     if (np->cylon_width != op->cylon_width)
  376.     {
  377.         /* Check new cylon width */
  378.         CheckCylonWidth(nw);
  379.  
  380.         _XfeConfigFlags(nw) |= XfeConfigExpose;
  381.     }
  382.  
  383.     /* cylon_offset */
  384.     if (np->cylon_offset != op->cylon_offset)
  385.     {
  386.         /* Check new cylon offset */
  387.         CheckCylonOffset(nw);
  388.  
  389.         _XfeConfigFlags(nw) |= XfeConfigExpose;
  390.     }
  391.  
  392.     /* bar_color */
  393.     if (np->bar_color != op->bar_color)
  394.     {
  395.         bar_gc_flag = True;   
  396.         bar_insens_gc_flag = True;   
  397.  
  398.         _XfeConfigFlags(nw) |= XfeConfigExpose;
  399.     }
  400.  
  401.     /* background */
  402.     if (_XfeBackgroundPixel(nw) != _XfeBackgroundPixel(ow))
  403.     {
  404.         bar_insens_gc_flag = True;   
  405.  
  406.         if (!_XfeSensitive(nw))
  407.         {
  408.             _XfeConfigFlags(nw) |= XfeConfigExpose;
  409.         }
  410.     }
  411.  
  412.     /* start_percent */
  413.     if (np->start_percent != op->start_percent)
  414.     {
  415.         _XfeConfigFlags(nw) |= (XfeConfigExpose|XfeConfigLayout);   
  416.  
  417.         CheckPercentages(nw);
  418.  
  419.         /* Stop the cylon since a change in percentage occured */
  420.         stop_cylon = True;
  421.     }
  422.  
  423.     /* end_percent */
  424.     if (np->end_percent != op->end_percent)
  425.     {
  426.         _XfeConfigFlags(nw) |= (XfeConfigExpose|XfeConfigLayout);   
  427.  
  428.         CheckPercentages(nw);
  429.  
  430.         /* Stop the cylon since a change in percentage occured */
  431.         stop_cylon = True;
  432.     }
  433.  
  434.     /* Stop the cylon if needed */
  435.     if (stop_cylon)
  436.     {
  437.         XfeProgressBarCylonStop(nw);
  438.     }
  439.  
  440.     /* Update the bar GC */
  441.     if (bar_gc_flag)
  442.     {
  443.         /* Release the old bar GC */
  444.         XtReleaseGC(nw,np->bar_GC);
  445.     
  446.     /* Allocate the new bar GC */
  447.         np->bar_GC = XfeAllocateColorGc(nw,np->bar_color,None,True);
  448.     }
  449.  
  450.     /* Update the bar insensitive GC */
  451.     if (bar_insens_gc_flag)
  452.     {
  453.         /* Release the old bar insensitive GC */
  454.         XtReleaseGC(nw,np->bar_insens_GC);
  455.     
  456.         /* Allocate the new bar insensitive GC */
  457.         np->bar_insens_GC = XfeAllocateColorGc(nw,np->bar_color,
  458.                                                _XfeBackgroundPixel(nw),
  459.                                                False);
  460.     }
  461.  
  462.     return _XfePrimitiveChainSetValues(ow,rw,nw,xfeProgressBarWidgetClass);
  463. }
  464. /*----------------------------------------------------------------------*/
  465.  
  466. /*----------------------------------------------------------------------*/
  467. /*                                                                        */
  468. /* XfePrimitive methods                                                    */
  469. /*                                                                        */
  470. /*----------------------------------------------------------------------*/
  471. static void
  472. LayoutComponents(Widget w)
  473. {
  474.     /* Invoke layout_string method */
  475.     _XfeLabelLayoutString(w);
  476.  
  477.     /* Layout the bar */
  478.     BarLayout(w);
  479. }
  480. /*----------------------------------------------------------------------*/
  481. static void
  482. DrawComponents(Widget w,XEvent *event,Region region,XRectangle * clip_rect)
  483. {
  484.     /* Draw the bar first */
  485.     BarDraw(w,region,clip_rect);
  486.  
  487.     /* Invoke draw_string method */
  488.     _XfeLabelDrawString(w,event,region,clip_rect);
  489. }
  490. /*----------------------------------------------------------------------*/
  491.  
  492. /*----------------------------------------------------------------------*/
  493. /*                                                                        */
  494. /* Misc XfeProgressBar functions                                        */
  495. /*                                                                        */
  496. /*----------------------------------------------------------------------*/
  497. static void
  498. CheckPercentages(Widget w)
  499. {
  500.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  501.  
  502.     /* Check start percent */
  503.     if (pp->start_percent < 0)
  504.     {
  505.         pp->start_percent = 0;
  506.         _XfeWarning(w,MESSAGE2);
  507.     }
  508.     else if (pp->start_percent > 100)
  509.     {
  510.         pp->start_percent = 100;
  511.         _XfeWarning(w,MESSAGE3);
  512.     }
  513.  
  514.     /* Check end percent */
  515.     if (pp->end_percent < 0)
  516.     {
  517.         pp->end_percent = 0;
  518.         _XfeWarning(w,MESSAGE4);
  519.     }
  520.     else if (pp->end_percent > 100)
  521.     {
  522.         pp->end_percent = 100;
  523.         _XfeWarning(w,MESSAGE5);
  524.     }
  525.  
  526.     /* Check whether end > start */
  527.     if (pp->end_percent < pp->start_percent)
  528.     {
  529.         pp->end_percent = pp->start_percent;
  530.         _XfeWarning(w,MESSAGE4);
  531.     }
  532. }
  533. /*----------------------------------------------------------------------*/
  534. static void
  535. CheckCylonWidth(Widget w)
  536. {
  537.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  538.  
  539.     /* Check start percent */
  540.     if (pp->cylon_width < 1)
  541.     {
  542.         pp->cylon_width = 1;
  543.  
  544.         _XfeWarning(w,MESSAGE8);
  545.     }
  546.     else if (pp->cylon_width > 50)
  547.     {
  548.         pp->cylon_width = 50;
  549.  
  550.         _XfeWarning(w,MESSAGE9);
  551.     }
  552. }
  553. /*----------------------------------------------------------------------*/
  554. static void
  555. CheckCylonOffset(Widget w)
  556. {
  557.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  558.  
  559.     /* Check start percent */
  560.     if (pp->cylon_offset < 1)
  561.     {
  562.         pp->cylon_offset = 1;
  563.  
  564.         _XfeWarning(w,MESSAGE10);
  565.     }
  566.     else if (pp->cylon_offset > 50)
  567.     {
  568.         pp->cylon_offset = 50;
  569.  
  570.         _XfeWarning(w,MESSAGE11);
  571.     }
  572. }
  573. /*----------------------------------------------------------------------*/
  574. static void
  575. BarDraw(Widget w,Region region,XRectangle * clip_rect)
  576. {
  577.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  578.  
  579.     /* Draw the bar if needed */
  580.     if (pp->start_percent != pp->end_percent)
  581.     {
  582.         XFillRectangle(XtDisplay(w),
  583.                        _XfePrimitiveDrawable(w),
  584.                        _XfeSensitive(w) ? pp->bar_GC : pp->bar_insens_GC,
  585.                        pp->bar_rect.x,
  586.                        pp->bar_rect.y,
  587.                        pp->bar_rect.width,
  588.                        pp->bar_rect.height);
  589.     }
  590. }
  591. /*----------------------------------------------------------------------*/
  592. static void
  593. BarLayout(Widget w)
  594. {
  595.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  596.     float        x1;
  597.     float        x2;
  598.  
  599. #if 0
  600.     x1 = (float) pp->start_percent / 100.0 * (float) _XfeRectWidth(w);
  601.     x2 = (float) pp->end_percent / 100.0 * (float) _XfeRectWidth(w);
  602. #else
  603.     x1 = ((float) pp->start_percent * (float) _XfeRectWidth(w)) / 100.0;
  604.     x2 = ((float) pp->end_percent * (float) _XfeRectWidth(w)) / 100.0;
  605. #endif
  606.  
  607.     pp->bar_rect.x = _XfeRectX(w) + (int) x1;
  608.     pp->bar_rect.y = _XfeRectY(w);
  609.  
  610.     pp->bar_rect.width = (int) (x2 - x1);
  611.     pp->bar_rect.height =_XfeRectHeight(w);
  612.  
  613. #if 0
  614.     printf("x1 = %f,x2 = %f\t(x2 - x1) = %f =(int) %d\n",
  615.            x1,
  616.            x2,
  617.            (x2 - x1),
  618.            (int) (x2 - x1));
  619. #endif
  620. }
  621. /*----------------------------------------------------------------------*/
  622.  
  623. /*----------------------------------------------------------------------*/
  624. /*                                                                        */
  625. /* Misc cylon functions                                                    */
  626. /*                                                                        */
  627. /*----------------------------------------------------------------------*/
  628. static void
  629. CylonTimeout(XtPointer closure,XtIntervalId * id)
  630. {
  631.     Widget                    w = (Widget) closure;
  632.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  633.     
  634.     /*
  635.      *    Always clear timer_id, so we don't kill someone else's timer
  636.      *    by accident later.
  637.      */
  638.     pp->timer_id = 0;
  639.  
  640.     /* Make sure the widget is still alive */
  641.     if (!_XfeIsAlive(w))
  642.     {
  643.         return;
  644.     }
  645.  
  646.     /* Make sure the cylon is running */
  647.     if (!pp->cylon_running)
  648.     {
  649.         return;
  650.     }
  651.  
  652.     /* Tick once */
  653.     XfeProgressBarCylonTick(w);
  654.  
  655.     /* Add the timeout again */
  656.     AddTimeout(w);
  657. }
  658. /*----------------------------------------------------------------------*/
  659. static void
  660. AddTimeout(Widget w)
  661. {
  662.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  663.  
  664.     assert( _XfeIsAlive(w) );
  665.  
  666.     pp->timer_id = XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  667.                                    pp->cylon_interval,
  668.                                    CylonTimeout,
  669.                                    (XtPointer) w);
  670. }
  671. /*----------------------------------------------------------------------*/
  672. static void
  673. RemoveTimeout(Widget w)
  674. {
  675.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  676.  
  677.     if (pp->timer_id)
  678.     {
  679.         XtRemoveTimeOut(pp->timer_id);
  680.  
  681.         pp->timer_id = 0;
  682.     }
  683. }
  684. /*----------------------------------------------------------------------*/
  685.  
  686. /*----------------------------------------------------------------------*/
  687. /*                                                                        */
  688. /* XfeProgressBar public functions                                        */
  689. /*                                                                        */
  690. /*----------------------------------------------------------------------*/
  691. /*extern*/ Widget
  692. XfeCreateProgressBar(Widget parent,char *name,Arg *args,Cardinal count)
  693. {
  694.     return (XtCreateWidget(name,xfeProgressBarWidgetClass,parent,args,count));
  695. }
  696. /*----------------------------------------------------------------------*/
  697. /*extern*/ void    
  698. XfeProgressBarSetPercentages(Widget w,int start,int end)
  699. {
  700.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  701.  
  702.     assert( _XfeIsAlive(w) );
  703.     assert( XfeIsProgressBar(w));
  704.     assert( start >= 0 );
  705.     assert( end <= 100 );
  706.     assert( end >= start );
  707.  
  708.     /* Stop the cylon if needed */
  709.     if (pp->cylon_running)
  710.     {
  711.         XfeProgressBarCylonStop(w);
  712.     }
  713.  
  714.     /* Assign the new percentages */
  715.     pp->start_percent = start;
  716.     pp->end_percent = end;
  717.  
  718.     /* Make sure the new percentages are ok */
  719.     CheckPercentages(w);
  720.  
  721.     /* Relayout the widget */
  722.     _XfePrimitiveLayoutComponents(w);
  723.     
  724.     /* Erase the old stuff */
  725.     _XfePrimitiveClearBackground(w);
  726.  
  727.     /* Make sure the widget is realized before drawing anything */
  728.     if (!XtIsRealized(w))
  729.     {
  730.         return;
  731.     }
  732.  
  733.     /* Redraw the everything */
  734.     _XfePrimitiveDrawEverything(w,NULL,NULL);
  735.     
  736.     /* Expose the widget */
  737.     XfeExpose(w,NULL,NULL);
  738. }
  739. /*----------------------------------------------------------------------*/
  740. /*extern*/ void    
  741. XfeProgressBarSetComponents(Widget w,XmString xm_label,int start,int end)
  742. {
  743.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  744.     XfeLabelPart *            lp = _XfeLabelPart(w);
  745.     
  746.     assert( _XfeIsAlive(w) );
  747.     assert( XfeIsProgressBar(w));
  748.     assert( start >= 0 );
  749.     assert( end <= 100 );
  750.     assert( end >= start );
  751.     assert( xm_label != NULL );
  752.  
  753.     /* Stop the cylon if needed */
  754.     if (pp->cylon_running)
  755.     {
  756.         XfeProgressBarCylonStop(w);
  757.     }
  758.  
  759.     /* Free the old string */
  760.     XmStringFree(lp->label_string);
  761.  
  762.     /* Make a copy of the given string */
  763.     if (xm_label)
  764.     {
  765.         lp->label_string = XfeXmStringCopy(w,xm_label,XtName(w));
  766.     }
  767.     else
  768.     {
  769.         lp->label_string = XmStringCreate("",XmFONTLIST_DEFAULT_TAG);
  770.     }
  771.  
  772.     /* Assign the new percentages */
  773.     pp->start_percent = start;
  774.     pp->end_percent = end;
  775.  
  776.     /* Make sure the new percentages are ok */
  777.     CheckPercentages(w);
  778.  
  779.     /* Prepare the widget */
  780.     _XfePrimitivePrepareComponents(w,XfePrepareAll);
  781.  
  782.     /* Relayout the widget */
  783.     _XfePrimitiveLayoutComponents(w);
  784.  
  785.     /* Make sure the widget is realized before drawing anything */
  786.     if (!XtIsRealized(w))
  787.     {
  788.         return;
  789.     }
  790.  
  791.     /* Erase the old stuff */
  792.     _XfePrimitiveClearBackground(w);
  793.  
  794.     /* Redraw the buffer */
  795.     _XfePrimitiveDrawEverything(w,NULL,NULL);
  796.  
  797.     /* Draw the buffer onto the window */
  798.     XfeExpose(w,NULL,NULL);
  799. }
  800. /*----------------------------------------------------------------------*/
  801. void
  802. XfeProgressBarCylonStart(Widget w)
  803. {
  804.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  805.  
  806.     assert( _XfeIsAlive(w) );
  807.     assert( XfeIsProgressBar(w) );
  808.  
  809.     /* Make sure the cylon is not already running */
  810.     if (pp->cylon_running)
  811.     {
  812.         return;
  813.     }
  814.  
  815.     pp->cylon_running = True;
  816.  
  817.     /* Update the percentages */
  818.     XfeProgressBarSetPercentages(w,
  819.                                  pp->cylon_index,
  820.                                  pp->cylon_index + pp->cylon_width);
  821.  
  822.     /* Add the timeout */
  823.      AddTimeout(w);
  824. }
  825. /*----------------------------------------------------------------------*/
  826. void
  827. XfeProgressBarCylonStop(Widget w)
  828. {
  829.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  830.  
  831.     assert( _XfeIsAlive(w) );
  832.     assert( XfeIsProgressBar(w) );
  833.  
  834.     /* Make sure the cylon is running */
  835.     if (!pp->cylon_running)
  836.     {
  837.         return;
  838.     }
  839.  
  840.     pp->cylon_running = False;
  841.  
  842.     /* Remove the timeout if needed */
  843.      RemoveTimeout(w);
  844. }
  845. /*----------------------------------------------------------------------*/
  846. void
  847. XfeProgressBarCylonReset(Widget w)
  848. {
  849.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  850.  
  851.     assert( _XfeIsAlive(w) );
  852.     assert( XfeIsProgressBar(w) );
  853.  
  854.     pp->cylon_index = 0;
  855.  
  856.     /* Update the percentages */
  857.     XfeProgressBarSetPercentages(w,
  858.                                  pp->cylon_index,
  859.                                  pp->cylon_index + pp->cylon_width);
  860. }
  861. /*----------------------------------------------------------------------*/
  862. void
  863. XfeProgressBarCylonTick(Widget w)
  864. {
  865.     XfeProgressBarPart *    pp = _XfeProgressBarPart(w);
  866.     
  867.     assert( _XfeIsAlive(w) );
  868.     assert( XfeIsProgressBar(w) );
  869.  
  870.     /* If the cylon is not running, start it */
  871.     if (!pp->cylon_running)
  872.     {
  873.         XfeProgressBarCylonStart(w);
  874.     }
  875.  
  876.     /* Update the cylon position */
  877.     if (pp->cylon_direction)
  878.     {
  879.         pp->cylon_index += pp->cylon_offset;
  880.     }
  881.     else
  882.     {
  883.         pp->cylon_index -= pp->cylon_offset;
  884.     }
  885.  
  886.     /* Check for cylon edge positions */
  887.     if (pp->cylon_index > (100 - pp->cylon_width))
  888.     {
  889.            pp->cylon_index = 100 - pp->cylon_width;
  890.         
  891.         pp->cylon_direction = False;
  892.     }
  893.     else if (pp->cylon_index < 0)
  894.     {
  895.           pp->cylon_index = 0;
  896.  
  897.         pp->cylon_direction = True;
  898.     }
  899.  
  900.     /* Update the percentages */
  901.     XfeProgressBarSetPercentages(w,
  902.                                  pp->cylon_index,
  903.                                  pp->cylon_index + pp->cylon_width);
  904. }
  905. /*----------------------------------------------------------------------*/
  906.