home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / testfrac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-06  |  5.4 KB  |  205 lines

  1. /*
  2. ** testfrac.c
  3. **
  4. ** How to make a widget to choose the fraction of tests to be run.
  5. **
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <X11/Intrinsic.h>
  10. #include <X11/StringDefs.h>
  11. #include <X11/Xaw/Form.h>
  12. #include <X11/Xaw/Label.h>
  13. #include <X11/Xaw/Scrollbar.h>
  14. #include "xgc.h"
  15.  
  16. extern void interpret();
  17.  
  18. extern XtAppContext appcontext;
  19.  
  20. #define SCROLLBAR_LENGTH 125
  21. #define SLIDER_LENGTH 0.2    /* proportion of scrollbar taken up
  22.                    by the slider */
  23.  
  24. static Widget label;        /* the label */
  25. static Widget slider;        /* the scrollbar */
  26. static Widget percent;    /* label with chosen percentage */
  27.  
  28. static float fraction;        /* what percent has been chosen */
  29. static int   oldpercent = -1;    /* so we only update when the slider has
  30.                    been moved */
  31.  
  32. /* slider_jump(w,data,position)
  33. ** ----------------------------
  34. ** This function is called if the user moves the scrollbar to a new
  35. ** position (generally, by using the middle button).  It updates
  36. ** information about where the scrollbar is.
  37. */
  38.  
  39. /*ARGSUSED*/
  40. static void
  41. slider_jump(w, data, position)
  42.      Widget w;
  43.      caddr_t data;
  44.      caddr_t position;
  45. {
  46.   static Arg percentargs[] = {
  47.     {XtNlabel,   (XtArgVal) NULL}
  48.   };
  49.  
  50.   float oldpercent;        /* where the scrollbar is */
  51.   float newpercent;        /* normalized scrollbar */
  52.   char snewpercent[3];        /* string representation of scrollbar */
  53.  
  54.   oldpercent = *(float *) position;
  55.  
  56.   /* We want the scrollbar to be at 100% when the right edge of the slider
  57.   ** hits the end of the scrollbar, not the left edge.  When the right edge
  58.   ** is at 1.0, the left edge is at 1.0 - SLIDER_LENGTH.  Normalize
  59.   ** accordingly.  */
  60.    
  61.   newpercent = oldpercent / (1.0 - SLIDER_LENGTH);
  62.  
  63.   /* If the slider's partially out of the scrollbar, move it back in. */
  64.  
  65.   if (newpercent > 1.0) {
  66.     newpercent = 1.0;
  67.     XawScrollbarSetThumb( slider, 1.0 - SLIDER_LENGTH, SLIDER_LENGTH);
  68.   }
  69.  
  70.   /* Store the position of the silder where it can be found */
  71.  
  72.   *(float *)data = newpercent;
  73.  
  74.   /* Update the label widget */
  75.  
  76.   sprintf(snewpercent,"%d",(int)(newpercent*100));
  77.   percentargs[0].value = (XtArgVal) snewpercent;
  78.   XtSetValues(percent, percentargs, XtNumber(percentargs));
  79. }
  80.  
  81. /* slider_scroll(w,data,position)
  82. ** ------------------------------
  83. ** This function is called when the user does incremental scrolling, 
  84. ** generally with the left or right button.  Right now it just ignores it.
  85. */
  86.  
  87. /*ARGSUSED*/
  88. static void
  89. slider_scroll(w, data, position)
  90.      Widget w;
  91.      caddr_t data;
  92.      caddr_t position;
  93. {}
  94.  
  95. /*ARGSUSED*/
  96. static void
  97. update(w,event,params,num_params)
  98.      Widget w;
  99.      XEvent *event;
  100.      String *params;
  101.      int *num_params;
  102. {
  103.   char buf[80];
  104.   int newpercent;
  105.  
  106.   newpercent = (int)(fraction * 100.0);
  107.   if (newpercent != oldpercent) {
  108.     sprintf(buf, "percent %d\n", (int)(fraction * 100.0));
  109.     interpret(buf);
  110.     oldpercent = newpercent;
  111.   }
  112. }
  113.  
  114. /* create_testfrac_choice(w)
  115. ** -------------------------
  116. ** Inside w (a form widget), creates:
  117. **   1. A label "Percentage of Test"
  118. **   2. A scrollbar for the user to choose the percentage (from 0 to 100)
  119. **   3. A label with the current percentage displayed on it.
  120. ** The percentage starts at 100.
  121. **
  122. ** When the pointer leaves the scrollbar, a string is sent to interpret()
  123. ** so that it knows the position of the scrollbar.
  124. */
  125.  
  126. void
  127. create_testfrac_choice(w)
  128.      Widget w;
  129. {
  130.   static XtCallbackRec jumpcallbacks[] = {
  131.     {(XtCallbackProc) slider_jump, NULL},
  132.     {NULL,                         NULL}
  133.   };
  134.  
  135.   static XtCallbackRec scrollcallbacks[] = {
  136.     {(XtCallbackProc) slider_scroll, NULL},
  137.     {NULL,                           NULL}
  138.   };
  139.  
  140.   static Arg labelargs[] = {
  141.     {XtNborderWidth,  (XtArgVal) 0},
  142.     {XtNjustify,      (XtArgVal) XtJustifyRight},
  143.     {XtNvertDistance, (XtArgVal) 4}
  144.   };
  145.  
  146.   static Arg percentargs[] = {
  147.     {XtNborderWidth,    (XtArgVal) 1},
  148.     {XtNhorizDistance,  (XtArgVal) 10},
  149.     {XtNfromHoriz,      (XtArgVal) NULL}
  150.   };
  151.  
  152.   static Arg scrollargs[] = {
  153.     {XtNorientation,     (XtArgVal) XtorientHorizontal},
  154.     {XtNlength,          (XtArgVal) SCROLLBAR_LENGTH},
  155.     {XtNthickness,       (XtArgVal) 10},
  156.     {XtNshown,           (XtArgVal) 10},
  157.     {XtNhorizDistance,   (XtArgVal) 10},
  158.     {XtNfromHoriz,       (XtArgVal) NULL},
  159.     {XtNjumpProc,        (XtArgVal) NULL},
  160.     {XtNscrollProc,      (XtArgVal) NULL}
  161.   };
  162.     
  163.   static char *translationtable = "<Leave>: Update()";
  164.  
  165.   static XtActionsRec actiontable[] = {
  166.     {"Update",  (XtActionProc) update},
  167.     {NULL,      NULL}
  168.   };
  169.  
  170.   /* Let the scrollbar know where to store information where we
  171.   ** can see it */
  172.  
  173.   jumpcallbacks[0].closure = (caddr_t) &fraction;
  174.  
  175.   label = XtCreateManagedWidget("Percentage of Test",labelWidgetClass,w,
  176.                 labelargs,XtNumber(labelargs));
  177.  
  178.   percentargs[2].value = (XtArgVal) label;
  179.  
  180.   percent = XtCreateManagedWidget("100",labelWidgetClass,w,
  181.                   percentargs,XtNumber(percentargs));
  182.  
  183.   scrollargs[5].value = (XtArgVal) percent;
  184.   scrollargs[6].value = (XtArgVal) jumpcallbacks;
  185.   scrollargs[7].value = (XtArgVal) scrollcallbacks;
  186.  
  187.   slider = XtCreateManagedWidget("Slider",scrollbarWidgetClass,w,
  188.                  scrollargs,XtNumber(scrollargs));
  189.  
  190.   XtAppAddActions(appcontext,actiontable,XtNumber(actiontable));
  191.   XtOverrideTranslations(slider,XtParseTranslationTable(translationtable));
  192.  
  193.   /* Start the thumb out at 100% */
  194.  
  195.   XawScrollbarSetThumb(slider, 1.0 - SLIDER_LENGTH, SLIDER_LENGTH);
  196. }
  197.  
  198. void
  199. update_slider(newpercent)
  200.      int newpercent;
  201. {
  202.   fraction = (float) newpercent / 100.0;
  203.   XawScrollbarSetThumb(slider, fraction / (1.0-SLIDER_LENGTH), SLIDER_LENGTH);
  204. }
  205.