Debian Codegraph @

apps/samples/widgets/MyThumbWhl.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.2 $
44 |
45 | Classes:
46 | MyThumbWheel
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
54 #include <cmath>
55
56 #include <X11/StringDefs.h>
57 #include <X11/Intrinsic.h>
58
59 #include <Inventor/Xt/devices/SoXtMouse.h>
60 #include "MyFloatCallbackList.h"
61 #include "MyThumbWheel.h"
62 #include "MyUIRegion.h"
63 #include <GL/gl.h>
64
65
66 /*
67 * Defines
68 */
69
70 #define TICK_NUM 21
71 #define PART1 4
72 #define PART2 5
73 #define PART3 7
74 #define PART4 9
75 #define UI_THICK 3
76
77 #define RECT(x1, y1, x2, y2) \
78 glBegin(GL_LINE_LOOP); \
79 glVertex2s(x1, y1); glVertex2s(x1, y2); \
80 glVertex2s(x2, y2); glVertex2s(x2, y1); \
81 glEnd();
82
83 ////////////////////////////////////////////////////////////////////////
84 //
85 // Public constructor - build the widget right now
86 //
87 MyThumbWheel::MyThumbWheel(
88 Widget parent,
89 const char *name,
90 SbBool buildInsideParent,
91 SbBool horiz)
92 : SoXtGLWidget(
93 parent,
94 name,
95 buildInsideParent,
96 SO_GLX_RGB,
97 FALSE) // tell GLWidget not to build just yet
98 //
99 ////////////////////////////////////////////////////////////////////////
100 {
101 // In this case, this component is what the app wants, so buildNow = TRUE
102 constructorCommon(horiz, TRUE);
103 }
104
105 ////////////////////////////////////////////////////////////////////////
106 //
107 // SoEXTENDER constructor - the subclass tells us whether to build or not
108 //
109 MyThumbWheel::MyThumbWheel(
110 Widget parent,
111 const char *name,
112 SbBool buildInsideParent,
113 SbBool horiz,
114 SbBool buildNow)
115 : SoXtGLWidget(
116 parent,
117 name,
118 buildInsideParent,
119 SO_GLX_RGB,
120 FALSE) // tell GLWidget not to build just yet
121 //
122 ////////////////////////////////////////////////////////////////////////
123 {
124 // In this case, this component may be what the app wants,
125 // or it may want a subclass of this component. Pass along buildNow
126 // as it was passed to us.
127 constructorCommon(horiz, buildNow);
128 }
129
130 ////////////////////////////////////////////////////////////////////////
131 //
132 // Called by the constructors
133 //
134 // private
135 //
136 void
137 MyThumbWheel::constructorCommon(SbBool horiz, SbBool buildNow)
138 //
139 //////////////////////////////////////////////////////////////////////
140 {
141 mouse = new SoXtMouse(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
142
143 // init local vars
144 startCallbacks = new MyFloatCallbackList;
145 changedCallbacks = new MyFloatCallbackList;
146 finishCallbacks = new MyFloatCallbackList;
147 interactive = FALSE;
148 value = 0.0;
149 horizontal = horiz;
150
151 // default size
152 if (horizontal)
153 setGlxSize( SbVec2s(120, 22) );
154 else setGlxSize( SbVec2s(22, 120) );
155
156 // Build the widget tree, and let SoXtComponent know about our base widget.
157 if (buildNow) {
158 Widget w = buildWidget(getParentWidget());
159 setBaseWidget(w);
160 }
161 }
162
163 ////////////////////////////////////////////////////////////////////////
164 //
165 // Destructor.
166 //
167 MyThumbWheel::~MyThumbWheel()
168 //
169 ////////////////////////////////////////////////////////////////////////
170 {
171 delete startCallbacks;
172 delete changedCallbacks;
173 delete finishCallbacks;
174 delete mouse;
175 }
176
177 ////////////////////////////////////////////////////////////////////////
178 //
179 // This routine draws the entire thumb wheel region.
180 //
181 // Use: virtual protected
182 //
183 void
184 MyThumbWheel::redraw()
185 //
186 ////////////////////////////////////////////////////////////////////////
187 {
188 if (! isVisible())
189 return;
190
191 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
192
193 int i, n;
194 short x, y, x1, y1, x2, y2;
195 short mid, rad, l, d;
196 float angle, ang_inc;
197
198 SbVec2s size = getGlxSize();
199 x1 = y1 = 0;
200 x2 = size[0] - 1;
201 y2 = size[1] - 1;
202
203 drawDownUIBorders(x1, y1, x2, y2);
204 x1+=UI_THICK; y1+=UI_THICK; x2-=UI_THICK; y2-=UI_THICK;
205
206 LIGHT1_UI_COLOR;
207 RECT(x1, y1, x2, y2);
208 x1++; y1++; x2--; y2--;
209 RECT(x1, y1, x2, y2);
210 x1++; y1++; x2--; y2--;
211
212 glBegin(GL_LINES);
213
214 if (horizontal) {
215 DARK3_UI_COLOR; glVertex2s(x1,y2); glVertex2s(x2,y2);
216 BLACK_UI_COLOR; glVertex2s(x1,y1); glVertex2s(x2,y1);
217 y1++; y2--;
218
219 l = x2 - x1;
220 d = 0;
221 BLACK_UI_COLOR;
222 glVertex2s(x1+d, y1); glVertex2s(x2-d, y1);
223 glVertex2s(x1+d, y2); glVertex2s(x2-d, y2);
224 d = (short)(l * .06);
225 DARK2_UI_COLOR;
226 glVertex2s(x1+d, y1); glVertex2s(x2-d, y1);
227 glVertex2s(x1+d, y2); glVertex2s(x2-d, y2);
228 d = (short)(l * .12);
229 DARK1_UI_COLOR;
230 glVertex2s(x1+d, y1); glVertex2s(x2-d, y1);
231 glVertex2s(x1+d, y2); glVertex2s(x2-d, y2);
232 d = (short)(l * .20);
233 MAIN_UI_COLOR;
234 glVertex2s(x1+d, y1); glVertex2s(x2-d, y1);
235 glVertex2s(x1+d, y2); glVertex2s(x2-d, y2);
236 d = (short)(l * .30);
237 LIGHT1_UI_COLOR;
238 glVertex2s(x1+d, y1); glVertex2s(x2-d, y1);
239 glVertex2s(x1+d, y2); glVertex2s(x2-d, y2);
240 d = (short)(l * .40);
241 WHITE_UI_COLOR;
242 glVertex2s(x1+d, y1); glVertex2s(x2-d, y1);
243 glVertex2s(x1+d, y2); glVertex2s(x2-d, y2);
244 x1++; y1++; x2--; y2--;
245 }
246 else {
247 DARK3_UI_COLOR; glVertex2s(x1,y1); glVertex2s(x1,y2);
248 BLACK_UI_COLOR; glVertex2s(x2,y1); glVertex2s(x2,y2);
249 x1++; x2--;
250
251 l = y2 - y1;
252 d = 0;
253 BLACK_UI_COLOR;
254 glVertex2s(x1, y1+d); glVertex2s(x1, y2-d);
255 glVertex2s(x2, y1+d); glVertex2s(x2, y2-d);
256 d = (short)(l * .06);
257 DARK2_UI_COLOR;
258 glVertex2s(x1, y1+d); glVertex2s(x1, y2-d);
259 glVertex2s(x2, y1+d); glVertex2s(x2, y2-d);
260 d = (short)(l * .12);
261 DARK1_UI_COLOR;
262 glVertex2s(x1, y1+d); glVertex2s(x1, y2-d);
263 glVertex2s(x2, y1+d); glVertex2s(x2, y2-d);
264 d = (short)(l * .20);
265 MAIN_UI_COLOR;
266 glVertex2s(x1, y1+d); glVertex2s(x1, y2-d);
267 glVertex2s(x2, y1+d); glVertex2s(x2, y2-d);
268 d = (short)(l * .30);
269 LIGHT1_UI_COLOR;
270 glVertex2s(x1, y1+d); glVertex2s(x1, y2-d);
271 glVertex2s(x2, y1+d); glVertex2s(x2, y2-d);
272 d = (short)(l * .40);
273 WHITE_UI_COLOR;
274 glVertex2s(x1, y1+d); glVertex2s(x1, y2-d);
275 glVertex2s(x2, y1+d); glVertex2s(x2, y2-d);
276 x1++; y1++; x2--; y2--;
277 }
278
279 glEnd();
280
281 MAIN_UI_COLOR;
282 glRecti(x1, y1, x2, y2);
283
284
285 //
286 // draw the tick marks
287 //
288
289 angle = value;
290 ang_inc = M_PI / TICK_NUM;
291 n = (int)(floorf(angle / ang_inc));
292 angle -= n * ang_inc;
293
294 glBegin(GL_LINES);
295
296 if (horizontal) {
297 mid = size[0] / 2;
298 rad = mid - UI_THICK - 2;
299
300 for (i=0; i<TICK_NUM; i++) {
301 x = mid - (short)(cosf(angle) * rad) - 1;
302
303 if (i<PART1 || i>(TICK_NUM - PART1)) {
304 BLACK_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
305 x++;
306 DARK1_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
307 }
308 else if (i<PART2 || i>(TICK_NUM - PART2)) {
309 DARK3_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
310 }
311 else if (i<PART3 || i>(TICK_NUM - PART3)) {
312 LIGHT1_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
313 x++;
314 DARK2_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
315 }
316 else if (i<PART4 || i>(TICK_NUM - PART4)) {
317 WHITE_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
318 x++;
319 DARK2_UI_COLOR; glVertex2s(x, y1); glVertex2s(x, y2);
320 }
321 else {
322 WHITE_UI_COLOR;
323 x--; glVertex2s(x, y1); glVertex2s(x, y2);
324 x++; glVertex2s(x, y1); glVertex2s(x, y2);
325 DARK2_UI_COLOR;
326 x++; glVertex2s(x, y1); glVertex2s(x, y2);
327 }
328
329 angle += ang_inc;
330 }
331 }
332 else {
333 mid = size[1] / 2;
334 rad = mid - UI_THICK - 2;
335
336 for (i=0; i<TICK_NUM; i++) {
337 y = mid - (short)(cosf(angle) * rad);
338
339 if (i<PART1 || i>(TICK_NUM - PART1)) {
340 BLACK_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
341 y--;
342 DARK1_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
343 }
344 else if (i<PART2 || i>(TICK_NUM - PART2)) {
345 DARK3_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
346 }
347 else if (i<PART3 || i>(TICK_NUM - PART3)) {
348 LIGHT1_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
349 y--;
350 DARK2_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
351 }
352 else if (i<PART4 || i>(TICK_NUM - PART4)) {
353 WHITE_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
354 y--;
355 DARK2_UI_COLOR; glVertex2s(x1, y); glVertex2s(x2, y);
356 }
357 else {
358 WHITE_UI_COLOR;
359 y++; glVertex2s(x1, y); glVertex2s(x2, y);
360 y--; glVertex2s(x1, y); glVertex2s(x2, y);
361 DARK2_UI_COLOR;
362 y--; glVertex2s(x1, y); glVertex2s(x2, y);
363 }
364
365 angle += ang_inc;
366 }
367 }
368
369 glEnd();
370 glFlush();
371 }
372
373 ////////////////////////////////////////////////////////////////////////
374 //
375 // Description:
376 // This builds the parent Glx widget, then registers interest
377 // in mouse events.
378 //
379 // Use: protected
380 Widget
381 MyThumbWheel::buildWidget(Widget parent)
382 //
383 ////////////////////////////////////////////////////////////////////////
384 {
385 Widget w = SoXtGLWidget::buildWidget(parent);
386
387 mouse->enable(getNormalWidget(),
388 (XtEventHandler) SoXtGLWidget::eventHandler,
389 (XtPointer) this);
390
391 return w;
392 }
393
394 ////////////////////////////////////////////////////////////////////////
395 //
396 // Process the passed X event.
397 //
398 // Use: virtual protected
399 //
400 void
401 MyThumbWheel::processEvent(XAnyEvent *xe)
402 //
403 ////////////////////////////////////////////////////////////////////////
404 {
405 XButtonEvent *be;
406 XMotionEvent *me;
407 SbVec2s size = getGlxSize();
408
409 switch (xe->type) {
410 case ButtonPress:
411 be = (XButtonEvent *) xe;
412 if (be->button == Button1) {
413
414 startCallbacks->invokeCallbacks(value);
415 interactive = TRUE;
416
417 // get starting point
418 lastPosition = (horizontal) ? be->x : (size[1] - be->y);
419 }
420 break;
421
422 case ButtonRelease:
423 be = (XButtonEvent *) xe;
424 if (be->button == Button1) {
425 interactive = FALSE;
426 finishCallbacks->invokeCallbacks(value);
427 }
428 break;
429
430 case MotionNotify:
431 me = (XMotionEvent *) xe;
432 if (me->state & Button1Mask) {
433 float r;
434 if (horizontal)
435 r = (me->x - lastPosition) / float(size[0] - 2 * UI_THICK);
436 else
437 r = (size[1] - me->y - lastPosition) / float(size[1] - 2 * UI_THICK);
438
439 // now rotate wheel
440 if (r != 0.0) {
441 value += r * M_PI;
442 changedCallbacks->invokeCallbacks(value);
443 redraw();
444 }
445
446 lastPosition = (horizontal) ? me->x : (size[1] - me->y);
447 }
448 break;
449 }
450 }
451
452 ////////////////////////////////////////////////////////////////////////
453 //
454 // Sets the thumb wheel to this value..
455 //
456 // Use: public
457 //
458 void
459 MyThumbWheel::setValue(float v)
460 //
461 ////////////////////////////////////////////////////////////////////////
462 {
463 if (value == v)
464 return;
465
466 value = v;
467
468 // call the callbacks
469 changedCallbacks->invokeCallbacks(value);
470
471 redraw();
472 }
473
474 ////////////////////////////////////////////////////////////////////////
475 //
476 // This routine is when the window has changed size
477 //
478 // Use: virtual protected
479 //
480 void
481 MyThumbWheel::sizeChanged(const SbVec2s &newSize)
482 //
483 ////////////////////////////////////////////////////////////////////////
484 {
485 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
486
487 // reset projection
488 glViewport(0, 0, newSize[0], newSize[1]);
489 glMatrixMode(GL_PROJECTION);
490 glLoadIdentity();
491 glOrtho(0, newSize[0], 0, newSize[1], -1, 1);
492 }
493
494 void
495 MyThumbWheel::addStartCallback(MyThumbWheelCB *f, void *userData)
496 { startCallbacks->addCallback((MyFloatCallbackListCB *)f, userData); }
497
498 void
499 MyThumbWheel::addValueChangedCallback(MyThumbWheelCB *f, void *userData)
500 { changedCallbacks->addCallback((MyFloatCallbackListCB *)f, userData); }
501
502 void
503 MyThumbWheel::addFinishCallback(MyThumbWheelCB *f, void *userData)
504 { finishCallbacks->addCallback((MyFloatCallbackListCB *)f, userData); }
505
506 void
507 MyThumbWheel::removeStartCallback(MyThumbWheelCB *f, void *userData)
508 { startCallbacks->removeCallback((MyFloatCallbackListCB *)f, userData); }
509
510 void
511 MyThumbWheel::removeValueChangedCallback(MyThumbWheelCB *f, void *userData)
512 { changedCallbacks->removeCallback((MyFloatCallbackListCB *)f, userData); }
513
514 void
515 MyThumbWheel::removeFinishCallback(MyThumbWheelCB *f, void *userData)
516 { finishCallbacks->removeCallback((MyFloatCallbackListCB *)f, userData); }