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