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