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