Debian Codegraph @

libSoXt/src/mixedMode/SoXtColWheel.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 | _SoXtColorWheel
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 #if DEBUG
55 #include <iostream>
56 #endif
57 #include <inttypes.h>
58 #include <Inventor/Xt/devices/SoXtMouse.h>
59 #include "_SoXtColorWheel.h"
60 #include "_SoXtUIRegion.h"
61
62 #include <cstdlib>
63 #include <cmath>
64
65 #include <GL/gl.h>
66
67 /*
68 * Defines
69 */
70
71 #define CIRCLE 5
72 #define STRIPES 32
73 #define SPACE 3
74 #define MARKER_SLOT 3
75
76 #define myMapcolor(i, r, g, b) \
77 XColor col; \
78 col.red = r << 8; \
79 col.green = g << 8; \
80 col.blue = b << 8; \
81 col.flags = DoRed | DoGreen | DoBlue; \
82 col.pixel = i; \
83 XStoreColor(getDisplay(), overlayColorMap, &col);
84
85
86 ////////////////////////////////////////////////////////////////////////
87 //
88 // Public constructor - build the widget right now
89 //
90 _SoXtColorWheel::_SoXtColorWheel(
91 Widget parent,
92 const char *name,
93 SbBool buildInsideParent)
94 : SoXtGLWidget(
95 parent,
96 name,
97 buildInsideParent,
98 SO_GLX_RGB | SO_GLX_OVERLAY,
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 _SoXtColorWheel::_SoXtColorWheel(
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 | SO_GLX_OVERLAY,
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 _SoXtColorWheel::constructorCommon(SbBool buildNow)
139 //
140 //////////////////////////////////////////////////////////////////////
141 {
142 mouse = new SoXtMouse(ButtonPressMask | ButtonMotionMask | ButtonReleaseMask);
143
144 // init local vars
145 WYSIWYGmode = FALSE;
146 hsvColor[0] = hsvColor[1] = 0.0;
147 hsvColor[2] = 1.0;
148 setGlxSize( SbVec2s(100, 100) ); // default size
149
150 // callback lists
151 startCallbacks = new SoCallbackList;
152 changedCallbacks = new SoCallbackList;
153 finishCallbacks = new SoCallbackList;
154 interactive = FALSE;
155
156 // allocate memory for the geometry/colors
157 int num = 1 + CIRCLE * (STRIPES + 1);
158 geometry = (SbVec2f *)malloc(num * sizeof(SbVec2f));
159 defaultColors = (SbColor *)malloc(num * sizeof(SbColor));
160 colors = (SbColor *)malloc(num * sizeof(SbColor));
161
162 // ??? reset the geometry to prevent the window manager under PI a362
163 // ??? to be killed (redrawing occurs even when window hasn't been
164 // ??? mapped yet so those values would be bogus)
165 for (int i=0; i<num; i++)
166 geometry[i].setValue(0,0);
167
168 // make the full intensity colors
169 makeWheelColors(defaultColors, 1.0);
170
171 // Build the widget tree, and let SoXtComponent know about our base widget.
172 if (buildNow) {
173 Widget w = buildWidget(getParentWidget());
174 setBaseWidget(w);
175 }
176 }
177
178
179 ////////////////////////////////////////////////////////////////////////
180 //
181 // Destructor.
182 //
183
184 _SoXtColorWheel::~_SoXtColorWheel()
185 //
186 ////////////////////////////////////////////////////////////////////////
187 {
188 if (geometry != NULL)
189 free(geometry);
190 if (defaultColors != NULL)
191 free(defaultColors);
192 if (colors != NULL)
193 free(colors);
194
195 delete startCallbacks;
196 delete changedCallbacks;
197 delete finishCallbacks;
198
199 delete mouse;
200 }
201
202
203 ////////////////////////////////////////////////////////////////////////
204 //
205 // This routine redraws the normal plane color wheel region.
206 //
207 // Use: virtual public
208
209 void
210 _SoXtColorWheel::redraw()
211 //
212 ////////////////////////////////////////////////////////////////////////
213 {
214 if (! isVisible())
215 return;
216 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
217
218 // make a complete drawing
219 drawWheelSurrounding();
220 drawWheelColors();
221
222 if (isDoubleBuffer())
223 glXSwapBuffers(getDisplay(), getNormalWindow());
224 else
225 glFlush();
226 }
227
228
229 ////////////////////////////////////////////////////////////////////////
230 //
231 // This routine redraws the color wheel overlay region (marker).
232 //
233 // Use: virtual public
234
235 void
236 _SoXtColorWheel::redrawOverlay()
237 //
238 ////////////////////////////////////////////////////////////////////////
239 {
240 drawWheelMarker();
241 }
242
243
244 ////////////////////////////////////////////////////////////////////////
245 //
246 // Process the passed X event.
247 //
248 // Use: virtual public
249
250 void
251 _SoXtColorWheel::processEvent(XAnyEvent *xe)
252 //
253 ////////////////////////////////////////////////////////////////////////
254 {
255 short x, y;
256 XButtonEvent *be;
257 XMotionEvent *me;
258 SbVec2s size = getGlxSize();
259
260 switch (xe->type) {
261 case ButtonPress:
262 be = (XButtonEvent *) xe;
263 if (be->button == Button1) {
264
265 // check if click is in color wheel
266 x = short(be->x) - cx;
267 y = short(size[1] - be->y) - cy;
268 if ((x*x + y*y) < ((radius+5)*(radius+5))) {
269 startCallbacks->invokeCallbacks(hsvColor);
270 interactive = TRUE;
271 moveWheelMarker(x, y);
272 }
273 }
274 break;
275
276 case ButtonRelease:
277 be = (XButtonEvent *) xe;
278 if (be->button == Button1 && interactive) {
279 interactive = FALSE;
280 finishCallbacks->invokeCallbacks(hsvColor);
281 }
282 break;
283
284 case MotionNotify:
285 me = (XMotionEvent *) xe;
286 if (me->state & Button1Mask)
287 moveWheelMarker(short(me->x) - cx, short(size[1] - me->y) - cy);
288 break;
289 }
290 }
291
292
293 ////////////////////////////////////////////////////////////////////////
294 //
295 // This routine sets the color wheel current color.
296 //
297 // usage: public
298
299 void
300 _SoXtColorWheel::setBaseColor(
301 const float hsv[3])
302 //
303 ////////////////////////////////////////////////////////////////////////
304 {
305 // check for redraw needs first
306 SbBool valueChanged = (hsvColor[2] != hsv[2]);
307 SbBool redrawColors = (WYSIWYGmode && valueChanged);
308 SbBool redrawMarker = (hsvColor[0]!=hsv[0] || hsvColor[1]!=hsv[1]);
309
310 // assign new color
311 hsvColor[0] = hsv[0];
312 hsvColor[1] = hsv[1];
313 hsvColor[2] = hsv[2];
314
315 // do wheel colors redraw
316 if (redrawColors) {
317 makeWheelColors(colors, hsv[2]);
318
319 if (! isVisible())
320 return;
321 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
322 drawWheelColors();
323 if (isDoubleBuffer())
324 glXSwapBuffers(getDisplay(), getNormalWindow());
325 else
326 glFlush();
327 checkMarkerColor();
328 }
329
330 // do marker redraw
331 if (redrawMarker)
332 drawWheelMarker();
333
334 if (redrawMarker || valueChanged)
335 changedCallbacks->invokeCallbacks(hsvColor);
336 }
337
338
339 ////////////////////////////////////////////////////////////////////////
340 //
341 // This routine sets the WYSIWYG mode.
342 //
343 // usage: public
344
345 void
346 _SoXtColorWheel::setWYSIWYG(SbBool flag)
347 //
348 ////////////////////////////////////////////////////////////////////////
349 {
350 if (WYSIWYGmode == flag)
351 return;
352
353 WYSIWYGmode = flag;
354
355 // build WYSIWYG colors
356 if (WYSIWYGmode)
357 makeWheelColors(colors, hsvColor[2]);
358
359 // now check if a redraw is needed
360 if (hsvColor[2] != 1.0) {
361
362 if (! isVisible())
363 return;
364 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
365 drawWheelColors();
366 if (isDoubleBuffer())
367 glXSwapBuffers(getDisplay(), getNormalWindow());
368 else
369 glFlush();
370 checkMarkerColor();
371 }
372 }
373
374
375 ////////////////////////////////////////////////////////////////////////
376 //
377 // Description:
378 // This builds the parent Glx widget, then registers interest
379 // in mouse events.
380 //
381 // Use: protected
382 Widget
383 _SoXtColorWheel::buildWidget(Widget parent)
384 //
385 ////////////////////////////////////////////////////////////////////////
386 {
387 Widget mgr = SoXtGLWidget::buildWidget(parent);
388
389 Widget w = getOverlayWidget() ? getOverlayWidget() : getNormalWidget();
390 mouse->enable(w,
391 (XtEventHandler) SoXtGLWidget::eventHandler,
392 (XtPointer) this);
393
394 return mgr;
395 }
396
397
398 ////////////////////////////////////////////////////////////////////////
399 //
400 // This routine is called at window creation time to init the GL state
401 //
402 // Use: virtual protected
403
404 void
405 _SoXtColorWheel::initOverlayGraphic()
406 //
407 ////////////////////////////////////////////////////////////////////////
408 {
409 myMapcolor(MARKER_SLOT, 0, 0, 0);
410 blackMarker = TRUE;
411 }
412
413
414 ////////////////////////////////////////////////////////////////////////
415 //
416 // This routine is called when window changed size.
417 //
418 // Use: virtual private
419
420 void
421 _SoXtColorWheel::sizeChanged(const SbVec2s &newSize)
422 //
423 ////////////////////////////////////////////////////////////////////////
424 {
425 //
426 // recompute things that depend on window size
427 //
428 short min = (newSize[0] < newSize[1]) ? newSize[0] : newSize[1];
429 radius = (min - (4*UI_THICK + 2*SPACE)) / 2;
430 cx = newSize[0] / 2;
431 cy = newSize[1] / 2;
432
433 makeWheelGeometry();
434
435 // reset projection
436 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
437 glViewport(0, 0, newSize[0], newSize[1]);
438 glMatrixMode(GL_PROJECTION);
439 glLoadIdentity();
440 glOrtho(0, newSize[0], 0, newSize[1], -1, 1);
441
442 if (getOverlayWindow() != 0) {
443 glXMakeCurrent(getDisplay(), getOverlayWindow(), getOverlayContext());
444 glViewport(0, 0, newSize[0], newSize[1]);
445 glMatrixMode(GL_PROJECTION);
446 glLoadIdentity();
447 glOrtho(0, newSize[0], 0, newSize[1], -1, 1);
448 }
449 }
450
451
452 ////////////////////////////////////////////////////////////////////////
453 //
454 // This routine computes the color wheel geometry.
455 //
456 // Use: private
457
458 void
459 _SoXtColorWheel::makeWheelGeometry()
460 //
461 ////////////////////////////////////////////////////////////////////////
462 {
463 SbVec2f *v = geometry;
464 float c[STRIPES+1][2];
465 int i, j;
466
467 // makes the master circle
468 float r = radius / float(CIRCLE);
469 float angle = 2.0 * M_PI / float(STRIPES);
470 c[0][0] = c[STRIPES][0] = r;
471 c[0][1] = c[STRIPES][1] = 0.0;
472 for (i=1; i<STRIPES; i++) {
473 c[i][0] = r * cosf(i*angle);
474 c[i][1] = r * sinf(i*angle);
475 }
476
477 //
478 // now build the color wheel geometry
479 //
480
481 // center point
482 (*v)[0] = cx;
483 (*v++)[1] = cy;
484
485 for (j=1; j<=CIRCLE; j++) {
486 for (i=0; i<=STRIPES; i++) {
487 (*v)[0] = cx + j * c[i][0];
488 (*v++)[1] = cy + j * c[i][1];
489 }
490 }
491 }
492
493
494 ////////////////////////////////////////////////////////////////////////
495 //
496 // This routine computes the color wheel colors given a color
497 // intensity.
498 //
499 // Use: private
500
501 void
502 _SoXtColorWheel::makeWheelColors(
503 SbColor *c,
504 float intensity)
505 //
506 ////////////////////////////////////////////////////////////////////////
507 {
508 float hsv[3];
509 float Hinc, Sinc;
510 int i, j;
511
512 hsv[2] = intensity;
513 Hinc = 1.0 / float(STRIPES);
514 Sinc = 1.0 / float(CIRCLE);
515
516 // center color
517 (*c)[0] = (*c)[1] = (*c)[2] = intensity; c++;
518
519 //
520 // now make the wheel colors
521 //
522 for (j=1; j<=CIRCLE; j++) {
523
524 hsv[1] = j * Sinc;
525 for (i=0; i<STRIPES; i++) {
526
527 hsv[0] = i * Hinc;
528 (c++)->setHSVValue(hsv);
529 }
530
531 // last color is same as first color
532 (*c)[0] = (*(c-STRIPES))[0];
533 (*c)[1] = (*(c-STRIPES))[1];
534 (*c)[2] = (*(c-STRIPES))[2];
535 c++;
536 }
537 }
538
539
540 ////////////////////////////////////////////////////////////////////////
541 //
542 // This routine draws the color wheel surroundings.
543 //
544 // Use: private
545
546 void
547 _SoXtColorWheel::drawWheelSurrounding()
548 //
549 ////////////////////////////////////////////////////////////////////////
550 {
551 glClearColor(.66, .66, .66, 1.0);
552 glClear(GL_COLOR_BUFFER_BIT);
553
554 // draw outer border
555 SbVec2s size = getGlxSize();
556 SoDrawDownUIBorders(0, 0, size[0]-1, size[1]-1);
557 }
558
559
560 ////////////////////////////////////////////////////////////////////////
561 //
562 // This routine draws the color wheel top part (colors).
563 //
564 // Use: private
565
566 void
567 _SoXtColorWheel::drawWheelColors()
568 //
569 ////////////////////////////////////////////////////////////////////////
570 {
571 SbColor *c1, *c2;
572 SbVec2f *v1, *v2;
573 int i, j;
574
575 //
576 // draw center circle as triangle fan
577 //
578 c1 = (WYSIWYGmode) ? colors : defaultColors;
579 v1 = geometry;
580
581 glBegin(GL_TRIANGLE_FAN);
582 for (i=0; i<(STRIPES+2); i++) {
583 glColor3fv((c1++)->getValue());
584 glVertex2fv((v1++)->getValue());
585 }
586 glEnd();
587
588 //
589 // draw the remaining stripes as quad meshes
590 //
591 c1 = (WYSIWYGmode) ? colors : defaultColors;
592 v1 = geometry;
593 c1++; v1++;
594 c2 = c1 + STRIPES + 1;
595 v2 = v1 + STRIPES + 1;
596
597 for (j=1; j<CIRCLE; j++) {
598
599 glBegin(GL_QUAD_STRIP);
600 for (i=0; i<=STRIPES; i++) {
601 glColor3fv((c1++)->getValue());
602 glVertex2fv((v1++)->getValue());
603 glColor3fv((c2++)->getValue());
604 glVertex2fv((v2++)->getValue());
605 }
606 glEnd();
607 }
608 }
609
610
611 ////////////////////////////////////////////////////////////////////////
612 //
613 // This routine checks the marker color and changes it if needed.
614 //
615 // Use: private
616
617 void
618 _SoXtColorWheel::checkMarkerColor()
619 //
620 ////////////////////////////////////////////////////////////////////////
621 {
622 if (getOverlayWindow() == 0)
623 return;
624
625 // map a black or white color depending on background
626
627 if (WYSIWYGmode && hsvColor[2]<0.6) { // should be white
628 if (blackMarker) {
629 glXMakeCurrent(getDisplay(), getOverlayWindow(), getOverlayContext());
630 myMapcolor(MARKER_SLOT, 255, 255, 255);
631 blackMarker = FALSE;
632 }
633 }
634 else { // should be black
635 if (!blackMarker) {
636 glXMakeCurrent(getDisplay(), getOverlayWindow(), getOverlayContext());
637 myMapcolor(MARKER_SLOT, 0, 0, 0);
638 blackMarker = TRUE;
639 }
640 }
641 }
642
643 ////////////////////////////////////////////////////////////////////////
644 //
645 // This routine draws the color marker feedback (in overlay).
646 //
647 // Use: private
648
649 void
650 _SoXtColorWheel::drawWheelMarker()
651 //
652 ////////////////////////////////////////////////////////////////////////
653 {
654 if (getOverlayWindow() == 0 || ! isVisible())
655 return;
656 glXMakeCurrent(getDisplay(), getOverlayWindow(), getOverlayContext());
657
658 // find radius and angle from hsv color, from which position is determined.
659 short x, y;
660 float rad = hsvColor[1] * radius;
661 float angle = 2.0 * M_PI * hsvColor[0];
662 x = cx + short(rad * cosf(angle));
663 y = cy + short(rad * sinf(angle));
664
665 //
666 // now draw dot in overlay plane
667 //
668 glClearIndex(0);
669 glClear(GL_COLOR_BUFFER_BIT);
670 glIndexi(MARKER_SLOT);
671
672 // ??? doing a GL_LINE_LOOP seems to be missing the top right
673 // ??? pixel due to subpixel == TRUE in openGL.
674 glBegin(GL_LINE_STRIP);
675 glVertex2s(x+3, y+3); glVertex2s(x+3, y-3);
676 glVertex2s(x-3, y-3); glVertex2s(x-3, y+3);
677 glVertex2s(x+3+1, y+3);
678 glEnd();
679
680 glFlush();
681 }
682
683 ////////////////////////////////////////////////////////////////////////
684 //
685 // This routine moves the color maker based of mouse position.
686 //
687 // Use: private
688 //
689 void
690 _SoXtColorWheel::moveWheelMarker(
691 short x, short y) // wheel center relative coordinates
692 //
693 ////////////////////////////////////////////////////////////////////////
694 {
695 // find the saturation based on distance
696 float s = sqrtf(x*x + y*y) / float(radius);
697 if (s > 1.0)
698 s = 1.0;
699
700 // now find the hue based on the angle
701 float angle = atan2(float(y), float(x));
702 if (angle < 0.0)
703 angle += 2.0 * M_PI;
704 float h = angle / (2.0 * M_PI);
705
706 // check if redraw and callback are needed
707 if (hsvColor[0] != h || hsvColor[1] != s) {
708
709 hsvColor[0] = h;
710 hsvColor[1] = s;
711
712 drawWheelMarker();
713 changedCallbacks->invokeCallbacks(hsvColor);
714 }
715 }