Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!csus.edu!ucdavis!csusac!unify!openlook!openlook-request From: salzman@Eng.Sun.COM (Isaac Salzman) Newsgroups: comp.windows.open-look Subject: Re: sliders with floating point values? Message-ID: Date: 14 Nov 90 06:47:33 GMT Lines: 193 >mday@pollack.mmwb.ucsf.edu (Mark Day) writes: >1. Is there a floating point slider hidden somewhere in the xview library? no. >2. Is the xview specification "frozen", or is there still time to add such a b >east? actually, you mean that *OPEN LOOK* spec. i doub't it's completely "frozen", but i have no idea about the status of other types of sliders and panel items in general. what's really needed is a general purpose slider (so you can have strings or whatever you want instead of just numeric values - int, float, or otherwise). >3. Has anyone implemented an xview floating point slider that I can borrow? >(I'll return it, I promise) sort of. i hacked one up using a normal slider and a PANEL_TEXT (with LOTS of help from Devguide). basically, you create a PANEL_SLIDER with no decorations (labels, text output of value, etc.). drop a PANEL_TEXT in front of it: make it read only. so it looks like this: This is a Float Slider: ____ I---[]----------I If you really want to label the ends of the slider, dump a couple PANEL_MESSAGE items in front of/behind the slider end points. so it's not really a floating pt. slider, but it looks like one. you package things up so that when the slider value changes, the associated text item is updated to reflect the floating point value. when you get the value from the slider you need to convert it to floating pt. i've got it in some code i'm hacking on, and it seems to work just fine. i've chopped out the relevant pieces (along with an example main that i threw together off the top of my head) and included them below. it's relatively straightforward. if you have any problems trying to use this, drop me some (private) e-mail. ciao! -- * Isaac J. Salzman ---- * Sun Microsystems Inc., Audio Tools & Integration /o o/ / * 2550 Garcia Ave., Mt. View, CA 94043-1100, MS: MTV23-03 | v | | * AT&T : +1 415-336-4338 _| |_/ * Internet : salzman@Eng.Sun.COM / | | * UUCP : ...!sun!salzman | | | ------------------------------------------------------------------ #include #include #include /* attach a ptr to this struct to a slider to cvt it's value to * the appropriate fp value and stuff it in the text area. */ #define FPSTRLEN 10 /* string len for cvt'ing floats... */ #define CVTODBL(val, scale) ((double)(val * (double)scale)) #define CVTOINT(val, scale) (nint(val / (double)scale)) struct float_slider_data { Panel_text text; /* text item to set w/float value */ double scale; /* scale factor */ }; Attr_attribute FPSLIDERKEY; /* for sliders - to cvt to floating pt. */ /* convert a slider to "floating point" slider. */ void make_float_slider(slider, text, scale) Panel_slider slider; Panel_text text; double scale; { struct float_slider_data *fsdp; if (!(fsdp = (struct float_slider_data *) calloc(1, sizeof(struct float_slider_data)))) { fprintf(stderr,"warning: can't malloc float_slider_data\n"); return; } fsdp->scale = scale; fsdp->text = text; xv_set(slider, PANEL_NOTIFY_LEVEL, PANEL_ALL, /* ALWAYS send notify events */ XV_KEY_DATA, FPSLIDERKEY, fsdp, NULL); } /* update the text item associated with a "floating pt" slider. * if FPSLIDERKEY isn't set on the slider, ignore... */ void update_float_slider_text(slider, val) Panel_slider slider; int val; { struct float_slider_data *fsdp; int ival; double dval; char sval[FPSTRLEN]; if (!(fsdp = (struct float_slider_data *) xv_get(slider, XV_KEY_DATA, FPSLIDERKEY))) { fprintf(stderr, "warning: can't get FPSLIDERKEY for slider\n"); return; } /* XXX the precision should depend on the size of the text * area and the min/max and scale ... */ dval = CVTODBL(val, fsdp->scale); if (dval < 10.0) sprintf(sval, "%2.4f", dval); else sprintf(sval, "%4.2f", dval); xv_set(fsdp->text, PANEL_VALUE, sval, NULL); } /* a routine for getting the floating pt. value of the slider */ double get_float_slider_value(s) Panel_slider s; { struct float_slider_data *fsdp; ival i; if (fsdp = (struct float_slider_data *) xv_get(s, XV_KEY_DATA, FPSLIDERKEY)) { ival = xv_get(s, PANEL_VALUE); return (CVTODBL(ival, fsdp->scale)); } return (0.0); } /* typical notify proc for a "floating pt. slider" */ void slider_notify(item, value, event) Panel_item item; int value; Event *event; { /* do stuff with slider value, if you want, then update the * the associated text item for the floating pt. slider. */ update_float_slider_text(item, value); } main() { Frame f; Panel p; Panel_slider s; Panel_text t; xv_init(...); FPSLIDERKEY = xv_unique_key(); f = xv_create(NULL, FRAME, ...); p = xv_create(f, PANEL, ...); s = xv_create(p, PANEL_SLIDER, ..., PANEL_NOTIFY_PROC, slider_notify, NULL); t = xv_create(p, PANEL_TEXT, ...); /* scale is 0.10, so if the MIN is 0 and the MAX is 10, you'll get * 0.0 to 1.0 in 1/10 unit increments (0.10, 0.20, ..., 1.0). make_float_slider(s, t, 0.10); /* set slider (and slider text) to some initial value. */ xv_set(s, PANEL_VALUE, CVTOINT(0.20, 0.10), NULL); update_float_slider_text(s, CVTOINT(0.20, 0.10)); /* and off you go. everytime the slider is moved, the associated * text item will be updated to represent the floating pt. * value of the slider. */ xv_main_loop(f); }