Debian Codegraph @

libSoXt/src/mixedMode/SoXtSlider.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990-93 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.5 $
44 |
45 | Classes:
46 | _SoXtSlider
47 |
48 | Author(s) : Alain Dumesny
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53 #include <inttypes.h>
54 #include <Inventor/Xt/SoXt.h>
55 #include <Inventor/Xt/devices/SoXtMouse.h>
56 #include <Inventor/errors/SoDebugError.h>
57 #include "SoFloatCallbackList.h"
58 #include "_SoXtUIRegion.h"
59 #include "_SoXtSlider.h"
60
61 #include <cstdlib>
62
63 #include <X11/StringDefs.h>
64 #include <X11/Intrinsic.h>
65 #include <Xm/Form.h>
66 #include <Xm/Text.h>
67 #include <Xm/Label.h>
68 #include <Xm/LabelG.h>
69
70 #include <GL/gl.h>
71
72
73 /*
74 * Defines
75 */
76
77 #define BOX_H 12 // thum height
78 #define BOX_L 7 // thum width/2
79 #define SPACE 2
80 #define THUMB_SPACE 1
81 #define SIDE (UI_THICK + SPACE + UI_THICK)
82
83 // pixels between the slider and the label
84 #define TEXT_OFFSET 6
85
86 ////////////////////////////////////////////////////////////////////////
87 //
88 // Public constructor - build the widget right now
89 //
90 _SoXtSlider::_SoXtSlider(
91 Widget parent,
92 const char *name,
93 SbBool buildInsideParent)
94 : SoXtGLWidget(
95 parent,
96 name,
97 buildInsideParent,
98 SO_GLX_RGB,
99 FALSE) // tell GLWidget not to build just yet
100 //
101 ////////////////////////////////////////////////////////////////////////
102 {
103 // In this case, this component is what the app wants, so buildNow = TRUE
104 constructorCommon(TRUE);
105 }
106
107 ////////////////////////////////////////////////////////////////////////
108 //
109 // SoEXTENDER constructor - the subclass tells us whether to build or not
110 //
111 _SoXtSlider::_SoXtSlider(
112 Widget parent,
113 const char *name,
114 SbBool buildInsideParent,
115 SbBool buildNow)
116 : SoXtGLWidget(
117 parent,
118 name,
119 buildInsideParent,
120 SO_GLX_RGB,
121 FALSE) // tell GLWidget not to build just yet
122 //
123 ////////////////////////////////////////////////////////////////////////
124 {
125 // In this case, this component may be what the app wants,
126 // or it may want a subclass of this component. Pass along buildNow
127 // as it was passed to us.
128 constructorCommon(buildNow);
129 }
130
131 ////////////////////////////////////////////////////////////////////////
132 //
133 // Called by the constructors
134 //
135 // private
136 //
137 void
138 _SoXtSlider::constructorCommon(SbBool buildNow)
139 //
140 //////////////////////////////////////////////////////////////////////
141 {
142 mouse = new SoXtMouse(ButtonPressMask | ButtonMotionMask | ButtonReleaseMask);
143
144 // init local vars
145 startCallbacks = new SoFloatCallbackList;
146 changedCallbacks = new SoFloatCallbackList;
147 finishCallbacks = new SoFloatCallbackList;
148 interactive = FALSE;
149 value = 0.0;
150 sliderWidget = labelWidget = numberWidget = mgrWidget = NULL;
151 labelStr = NULL;
152 numberVisible = TRUE; // show the number by default
153
154 // slider geometry vars which do not depend on window size
155 slx1 = sly1 = SIDE;
156 thumy1 = THUMB_SPACE;
157 thumy2 = thumy1 + BOX_H;
158
159 sliderSize.setValue(110, 25); // default slider size
160
161 // Build the widget tree, and let SoXtComponent know about our base widget.
162 if (buildNow) {
163 Widget w = buildWidget(getParentWidget());
164 setBaseWidget(w);
165 }
166 }
167
168 ////////////////////////////////////////////////////////////////////////
169 //
170 // Destructor.
171 //
172
173 _SoXtSlider::~_SoXtSlider()
174 //
175 ////////////////////////////////////////////////////////////////////////
176 {
177 delete startCallbacks;
178 delete changedCallbacks;
179 delete finishCallbacks;
180 if (labelStr != NULL) free(labelStr);
181 delete mouse;
182 }
183
184
185 ////////////////////////////////////////////////////////////////////////
186 //
187 // This routine draws the entire slider region.
188 //
189 // Use: virtual public
190
191 void
192 _SoXtSlider::redraw()
193 //
194 ////////////////////////////////////////////////////////////////////////
195 {
196 if (! isVisible())
197 return;
198 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
199
200 // compute thum position
201 position = slx1 + short(value * (slx2 - slx1));
202 thumx1 = position - BOX_L;
203 thumx2 = position + BOX_L;
204
205 // draw slider surrounding
206 SbVec2s size = getGlxSize();
207 SoDrawDownUIRegion(0, 0, size[0]-1, size[1]-1);
208
209 // now call the routine which does drawing inside the slider top region
210 drawSliderTopRegion();
211 }
212
213
214 ////////////////////////////////////////////////////////////////////////
215 //
216 // Sets the label string.
217 //
218 // Use: public
219
220 void
221 _SoXtSlider::setLabel(const char *newlabel)
222 //
223 ////////////////////////////////////////////////////////////////////////
224 {
225 // out with the old...
226 if (labelStr != NULL) free(labelStr);
227 labelStr = NULL;
228
229 // in with the new...
230 if (newlabel != NULL)
231 labelStr = strdup(newlabel);
232
233 if (getWidget() != NULL)
234 doLabelLayout();
235 }
236
237 ////////////////////////////////////////////////////////////////////////
238 //
239 // Sets visibility of the numeric field
240 //
241 // Use: public
242
243 void
244 _SoXtSlider::setNumericFieldVisible(SbBool setVisible)
245 //
246 ////////////////////////////////////////////////////////////////////////
247 {
248 if (setVisible) {
249 if (numberVisible)
250 return; // already visible
251 else {
252 // make the number visible
253 numberVisible = TRUE;
254 if (getWidget() != NULL)
255 doNumberLayout();
256 }
257 }
258 else {
259 if (! numberVisible)
260 return; // already not visible
261 else {
262 // make the number not visible
263 numberVisible = FALSE;
264 if (getWidget() != NULL)
265 doNumberLayout();
266 }
267 }
268 }
269
270
271 ////////////////////////////////////////////////////////////////////////
272 //
273 // lays out the motif label (if any) and slider relative to it.
274 //
275 // Use: private
276
277 void
278 _SoXtSlider::doLabelLayout()
279 //
280 ////////////////////////////////////////////////////////////////////////
281 {
282 int n;
283 Arg args[10];
284
285 if (labelStr == NULL) {
286
287 XtSetArg(args[0], XmNleftAttachment, XmATTACH_FORM);
288 XtSetValues(sliderWidget, args, 1);
289
290 if (labelWidget != 0) {
291 //??? XtUnmanage first?
292 XtDestroyWidget(labelWidget);
293 labelWidget = 0;
294 }
295 }
296 else {
297 if (labelWidget == 0) {
298
299 labelWidget = XtCreateWidget(labelStr, xmLabelGadgetClass, mgrWidget,
300 NULL, 0);
301
302 n = 0;
303 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
304 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
305 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
306 XtSetArg(args[n], XmNrightAttachment, XmATTACH_NONE); n++;
307 XtSetValues(labelWidget, args, n);
308
309 n = 0;
310 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
311 XtSetArg(args[n], XmNleftWidget, labelWidget); n++;
312 XtSetArg(args[n], XmNleftOffset, TEXT_OFFSET); n++;
313 XtSetValues(sliderWidget, args, n);
314
315 XtManageChild(labelWidget);
316 }
317 else {
318 XtSetArg(args[0], XmNname, labelStr);
319 XtSetValues(labelWidget, args, 1);
320 }
321 }
322 }
323
324 ////////////////////////////////////////////////////////////////////////
325 //
326 // lays out the motif numeric text field (if any) and slider relative to it.
327 //
328 // Use: private
329
330 void
331 _SoXtSlider::doNumberLayout()
332 //
333 ////////////////////////////////////////////////////////////////////////
334 {
335 int n;
336 Arg args[10];
337
338 if (! numberVisible) {
339
340 XtSetArg(args[0], XmNrightAttachment, XmATTACH_FORM);
341 XtSetValues(sliderWidget, args, 1);
342
343 if (numberWidget != NULL) {
344 //??? XtUnmanage first?
345 XtDestroyWidget(numberWidget);
346 numberWidget = NULL;
347 }
348 }
349 else {
350 if (numberWidget == NULL) {
351
352 n = 0;
353 XtSetArg(args[n], XmNhighlightThickness, 1); n++;
354 XtSetArg(args[n], XmNcolumns, 4); n++;
355 numberWidget = XtCreateWidget("sliderText", xmTextWidgetClass,
356 mgrWidget, args, n);
357 XtAddCallback(numberWidget, XmNactivateCallback,
358 (XtCallbackProc) _SoXtSlider::textFieldCB, (XtPointer) this);
359
360 n = 0;
361 XtSetArg(args[n], XmNtopAttachment, XmATTACH_NONE); n++;
362 XtSetArg(args[n], XmNleftAttachment, XmATTACH_NONE); n++;
363 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
364 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
365 XtSetValues(numberWidget, args, n);
366
367 n = 0;
368 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
369 XtSetArg(args[n], XmNrightWidget, numberWidget); n++;
370 XtSetValues(sliderWidget, args, n);
371
372 // now set the string to the slider value
373 char valStr[6];
374 sprintf(valStr, "%.2f", getValue());
375 XmTextSetString(numberWidget, valStr);
376
377 XtManageChild(numberWidget);
378 }
379 #ifdef DEBUG
380 else {
381 SoDebugError::post("_SoXtSlider::doNumberLayout::menuPick",
382 "INTERNAL ERROR");
383 }
384 #endif
385 }
386 }
387
388
389
390 ////////////////////////////////////////////////////////////////////////
391 //
392 // Description:
393 // This builds the parent Glx widget, then registers interest
394 // in mouse events.
395 //
396 // Use: virtual protected
397 Widget
398 _SoXtSlider::buildWidget(Widget parent)
399 //
400 ////////////////////////////////////////////////////////////////////////
401 {
402 int n;
403 Arg args[10];
404
405 //
406 // create a top form to hold everything together
407 //
408 mgrWidget = XtCreateWidget(getWidgetName(), xmFormWidgetClass, parent, NULL, 0);
409 // ??? do we need to register this widget class?
410
411 //
412 // create _SoXtSlider and text widget
413 //
414 sliderWidget = SoXtGLWidget::buildWidget(mgrWidget);
415 SoXt::setWidgetSize(sliderWidget, sliderSize);
416 mouse->enable(getNormalWidget(),
417 (XtEventHandler) SoXtGLWidget::eventHandler,
418 (XtPointer) this);
419
420
421 //
422 // do layout
423 //
424
425 n = 0;
426 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
427 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
428 XtSetValues(sliderWidget, args, n);
429
430 doLabelLayout();
431 doNumberLayout();
432
433 // manage that slider
434 XtManageChild(sliderWidget);
435
436 return mgrWidget;
437
438 }
439
440 ////////////////////////////////////////////////////////////////////////
441 //
442 // Resize the slider (only) widget
443 //
444 // Use: virtual public
445 void
446 _SoXtSlider::setSliderSize(const SbVec2s &newSize)
447 //
448 ////////////////////////////////////////////////////////////////////////
449 {
450 if (sliderWidget != NULL)
451 SoXt::setWidgetSize(sliderWidget, newSize);
452 sliderSize = newSize;
453 }
454
455 ////////////////////////////////////////////////////////////////////////
456 //
457 // Returns the slider (only) widget size
458 //
459 // Use: virtual public
460 SbVec2s
461 _SoXtSlider::getSliderSize()
462 //
463 ////////////////////////////////////////////////////////////////////////
464 {
465 return sliderSize;
466 }
467
468 ////////////////////////////////////////////////////////////////////////
469 //
470 // Process the passed X event.
471 //
472 // Use: virtual public
473
474 void
475 _SoXtSlider::processEvent(XAnyEvent *xe)
476 //
477 ////////////////////////////////////////////////////////////////////////
478 {
479 short x, y;
480 XButtonEvent *be;
481 XMotionEvent *me;
482 SbVec2s size = getGlxSize();
483
484 switch (xe->type) {
485 case ButtonPress:
486 be = (XButtonEvent *) xe;
487 if (be->button == Button1) {
488
489 startCallbacks->invokeCallbacks(value);
490 interactive = TRUE;
491
492 // check for thumb picking region
493 x = short(be->x);
494 y = short(size[1] - be->y);
495 if (x>thumx1 && x<thumx2 &&
496 y>thumy1 && y<thumy2)
497 posdiff = x - position;
498 else {
499 posdiff = 0;
500 setValue((x - slx1 - posdiff) / float(slx2 - slx1));
501 }
502 }
503 break;
504
505 case ButtonRelease:
506 be = (XButtonEvent *) xe;
507 if (be->button == Button1) {
508 interactive = FALSE;
509 finishCallbacks->invokeCallbacks(value);
510 }
511 break;
512
513 case MotionNotify:
514 me = (XMotionEvent *) xe;
515 if (me->state & Button1Mask)
516 setValue((me->x - slx1 - posdiff) / float(slx2 - slx1));
517 break;
518 }
519 }
520
521
522 ////////////////////////////////////////////////////////////////////////
523 //
524 // Sets the slider to this value..
525 //
526 // Use: public
527
528 void
529 _SoXtSlider::setValue(float v)
530 //
531 ////////////////////////////////////////////////////////////////////////
532 {
533 // assign the clipped value and check for redraw needs
534 float old = value;
535 value = (v > 1.0) ? 1.0 : ((v < 0.0) ? 0.0 : v) ;
536
537 if (value == old)
538 return;
539
540 redraw();
541
542 // update text field
543 if (numberWidget != NULL) {
544 char valStr[6];
545 sprintf(valStr, "%.2f", value);
546 XmTextSetString(numberWidget, valStr);
547 }
548
549 changedCallbacks->invokeCallbacks(value);
550 }
551
552
553 ////////////////////////////////////////////////////////////////////////
554 //
555 // This routine is when the window has changed size
556 //
557 // Use: virtual protected
558
559 void
560 _SoXtSlider::sizeChanged(const SbVec2s &newSize)
561 //
562 ////////////////////////////////////////////////////////////////////////
563 {
564 // calculate slider parameters which depends on window size
565 slx2 = newSize[0] - SIDE -1;
566 sly2 = newSize[1] - SIDE -1;
567
568 // reset projection
569 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
570 glViewport(0, 0, newSize[0], newSize[1]);
571 glMatrixMode(GL_PROJECTION);
572 glLoadIdentity();
573 glOrtho(0, newSize[0], 0, newSize[1], -1, 1);
574 }
575
576
577 ////////////////////////////////////////////////////////////////////////
578 //
579 // Draws the little white maker. subclasses can redefine this routine to
580 // make the slider top region look like anything they want.
581 //
582 // Use: virtual protected
583
584 void
585 _SoXtSlider::drawSliderTopRegion()
586 //
587 ////////////////////////////////////////////////////////////////////////
588 {
589 MAIN_UI_COLOR;
590 glRects(slx1, sly1, slx2, sly2);
591 SoDrawThumbUIRegion(thumx1, thumy1, thumx2, thumy2);
592
593 if (isDoubleBuffer())
594 glXSwapBuffers(getDisplay(), getNormalWindow());
595 else
596 glFlush();
597 }
598
599 void
600 _SoXtSlider::addStartCallback(_SoXtSliderCB *f, void *userData)
601 { startCallbacks->addCallback((SoFloatCallbackListCB *)f, userData); }
602
603 void
604 _SoXtSlider::addValueChangedCallback(_SoXtSliderCB *f, void *userData)
605 { changedCallbacks->addCallback((SoFloatCallbackListCB *)f, userData); }
606
607 void
608 _SoXtSlider::addFinishCallback(_SoXtSliderCB *f, void *userData)
609 { finishCallbacks->addCallback((SoFloatCallbackListCB *)f, userData); }
610
611 void
612 _SoXtSlider::removeStartCallback(_SoXtSliderCB *f, void *userData)
613 { startCallbacks->removeCallback((SoFloatCallbackListCB *)f, userData); }
614
615 void
616 _SoXtSlider::removeValueChangedCallback(_SoXtSliderCB *f, void *userData)
617 { changedCallbacks->removeCallback((SoFloatCallbackListCB *)f, userData); }
618
619 void
620 _SoXtSlider::removeFinishCallback(_SoXtSliderCB *f, void *userData)
621 { finishCallbacks->removeCallback((SoFloatCallbackListCB *)f, userData); }
622
623
624
625
626 //
627 ////////////////////////////////////////////////////////////////////////
628 // static callbacks stubs
629 ////////////////////////////////////////////////////////////////////////
630 //
631
632
633
634
635 //
636 // called whenever the user types a new value in the text field.
637 //
638 void
639 _SoXtSlider::textFieldCB(Widget w, _SoXtSlider *p, XtPointer)
640 {
641 // get text value from the label
642 char *str = XmTextGetString(w);
643 float val;
644 if (sscanf(str, "%f", &val))
645 p->setValue(val);
646 else {
647 // reformat the text field
648 char valStr[6];
649 sprintf(valStr, "%.2f", p->getValue());
650 XmTextSetString(w, valStr);
651 }
652 free(str);
653
654 // make the text field loose the focus
655 XmProcessTraversal(SoXt::getShellWidget(w), XmTRAVERSE_CURRENT);
656 }
657
658