forked from libglui/glui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglui.cpp
2108 lines (1581 loc) · 55.5 KB
/
glui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
GLUI User Interface Toolkit
---------------------------
glui.cpp
--------------------------------------------------
Copyright (c) 1998 Paul Rademacher
WWW: https://github.com/libglui/glui
Issues: https://github.com/libglui/glui/issues
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*****************************************************************************/
#include "glui_internal_control.h"
#include "tinyformat.h"
#include <algorithm>
/**
Note: moving this routine here from glui_add_controls.cpp prevents the linker
from touching glui_add_controls.o in non-deprecated programs, which
descreases the linked size of small GLUI programs substantially (100K+). (OSL 2006/06)
*/
void GLUI_Node::add_child_to_control(GLUI_Node *parent,GLUI_Control *child)
{
GLUI_Control *parent_control;
/*** Collapsible nodes have to be handled differently, b/c the first and
last children are swapped in and out ***/
parent_control = ((GLUI_Control*)parent);
if ( parent_control->collapsible == true ) {
if ( NOT parent_control->is_open ) {
/** Swap in the original first and last children **/
parent_control->child_head = parent_control->collapsed_node.child_head;
parent_control->child_tail = parent_control->collapsed_node.child_tail;
/*** Link this control ***/
child->link_this_to_parent_last( parent_control );
/** Swap the children back out ***/
parent_control->collapsed_node.child_head = parent_control->child_head;
parent_control->collapsed_node.child_tail = parent_control->child_tail;
parent_control->child_head = NULL;
parent_control->child_tail = NULL;
}
else {
child->link_this_to_parent_last( parent_control );
}
}
else {
child->link_this_to_parent_last( parent_control );
}
child->glui = (GLUI*) parent_control->glui;
child->update_size();
child->enabled = parent_control->enabled;
child->glui->refresh();
/** Now set the 'hidden' var based on the parent **/
if ( parent_control->hidden OR
(parent_control->collapsible AND NOT parent_control->is_open ) )
{
child->hidden = true;
}
}
/************************************ GLUI_Node::add_control() **************/
int GLUI_Node::add_control( GLUI_Control *child )
{
add_child_to_control(this,child);
return true;
}
/************************************ GLUI_Main::add_control() **************/
int GLUI_Main::add_control( GLUI_Node *parent, GLUI_Control *control )
{
add_child_to_control(parent,control);
return true;
}
/*** This object must be used to create a GLUI ***/
GLUI_Master_Object GLUI_Master;
/************************************ finish_drawing() ***********
Probably a silly routine. Called after all event handling callbacks.
*/
static void finish_drawing()
{
glFinish();
}
/************************************ GLUI_CB::operator()() ************/
void GLUI_CB::operator()(GLUI_Control*ctrl) const
{
if (idCB) idCB(ctrl->user_id);
if (objCB) objCB(ctrl);
}
/************************************************ GLUI::GLUI() **********/
int GLUI::init( const GLUI_String &text, long flags, int x, int y, int parent_window )
{
int old_glut_window;
this->flags = flags;
window_name = text;
buffer_mode = buffer_back; ///< New smooth way
//buffer_mode = buffer_front; ///< Old flickery way (a bit faster).
/*** We copy over the current window callthroughs ***/
/*** (I think this might actually only be needed for subwindows) ***/
/* glut_keyboard_CB = GLUI_Master.glut_keyboard_CB;
glut_reshape_CB = GLUI_Master.glut_reshape_CB;
glut_special_CB = GLUI_Master.glut_special_CB;
glut_mouse_CB = GLUI_Master.glut_mouse_CB;*/
if ( (flags & GLUI_SUBWINDOW) != GLUI_SUBWINDOW ) { /* not a subwindow, creating a new top-level window */
old_glut_window = glutGetWindow();
create_standalone_window( window_name.c_str(), x, y );
setup_default_glut_callbacks();
if ( old_glut_window > 0 )
glutSetWindow( old_glut_window );
top_level_glut_window_id = glut_window_id;
}
else /* *is* a subwindow */
{
old_glut_window = glutGetWindow();
create_subwindow( parent_window, flags );
setup_default_glut_callbacks();
if ( old_glut_window > 0 )
glutSetWindow( old_glut_window );
top_level_glut_window_id = parent_window;
/*
glutReshapeFunc( glui_parent_window_reshape_func );
glutSpecialFunc( glui_parent_window_special_func );
glutKeyboardFunc( glui_parent_window_keyboard_func );
glutMouseFunc( glui_parent_window_mouse_func );
*/
}
return true;
}
/**************************** GLUI_Main::create_standalone_window() ********/
void GLUI_Main::create_standalone_window( const GLUI_String &name, int x, int y )
{
glutInitWindowSize( 100, 100 );
if ( x >= 0 OR y >= 0 )
glutInitWindowPosition( x, y );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glut_window_id = glutCreateWindow( name.c_str() );
}
/******************************** GLUI_Main::create_subwindow() **********/
void GLUI_Main::create_subwindow( int parent_window, int window_alignment )
{
glut_window_id = glutCreateSubWindow(parent_window, 0,0, 100, 100);
this->parent_window = parent_window;
}
/**************************** GLUI_Main::setup_default_glut_callbacks() *****/
void GLUI_Main::setup_default_glut_callbacks()
{
glutDisplayFunc( display_func );
glutReshapeFunc( reshape_func );
glutKeyboardFunc( keyboard_func );
glutSpecialFunc( special_func );
glutMouseFunc( mouse_func );
glutMotionFunc( motion_func );
glutPassiveMotionFunc( passive_motion_func );
glutEntryFunc( entry_func );
glutVisibilityFunc( visibility_func );
/* glutIdleFunc( idle_func ); // FIXME! 100% CPU usage! */
}
/********************************************** glui_display_func() ********/
void GLUI_Main::display_func()
{
GLUI *glui;
/* printf( "display func\n" ); */
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->display();
/*
Do not do anything after the above line, b/c the GLUI
window might have just closed itself
*/
}
}
/********************************************** glui_reshape_func() ********/
void GLUI_Main::reshape_func(int w,int h )
{
GLUI *glui;
GLUI_Glut_Window *glut_window;
int current_window;
/*printf( "glui_reshape_func(): %d w/h: %d/%d\n", glutGetWindow(), w, h ); */
current_window = glutGetWindow();
/*** First check if this is main glut window ***/
glut_window = GLUI_Master.find_glut_window( current_window );
if ( glut_window ) {
if (glut_window->glut_reshape_CB) glut_window->glut_reshape_CB(w,h);
/*** Now send reshape events to all subwindows ***/
glui = (GLUI*) GLUI_Master.gluis.first_child();
while(glui) {
if ( TEST_AND( glui->flags, GLUI_SUBWINDOW) AND
glui->parent_window == current_window ) {
glutSetWindow( glui->get_glut_window_id());
glui->reshape(w,h);
/* glui->check_subwindow_position(); */
}
glui = (GLUI*) glui->next();
}
}
else {
/*** A standalone GLUI window ***/
glui = GLUI_Master.find_glui_by_window_id( current_window );
if ( glui ) {
glui->reshape(w,h);
}
}
}
/********************************************** glui_keyboard_func() ********/
void GLUI_Main::keyboard_func(unsigned char key, int x, int y)
{
GLUI *glui;
int current_window;
GLUI_Glut_Window *glut_window;
current_window = glutGetWindow();
glut_window = GLUI_Master.find_glut_window( current_window );
/*printf( "key: %d\n", current_window ); */
if ( glut_window ) { /** Was event in a GLUT window? **/
if ( GLUI_Master.active_control_glui AND GLUI_Master.active_control ) {
glutSetWindow( GLUI_Master.active_control_glui->get_glut_window_id() );
GLUI_Master.active_control_glui->keyboard(key,x,y);
finish_drawing();
glutSetWindow( current_window );
}
else {
if (glut_window->glut_keyboard_CB)
glut_window->glut_keyboard_CB( key, x, y );
}
}
else { /*** Nope, event was in a standalone GLUI window **/
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->keyboard(key,x,y);
finish_drawing();
}
}
}
/************************************************ glui_special_func() ********/
void GLUI_Main::special_func(int key, int x, int y)
{
GLUI *glui;
int current_window;
GLUI_Glut_Window *glut_window;
current_window = glutGetWindow();
glut_window = GLUI_Master.find_glut_window( current_window );
if (glut_window) /** Was event in a GLUT window? **/
{
if ( GLUI_Master.active_control_glui AND GLUI_Master.active_control )
{
glutSetWindow( GLUI_Master.active_control_glui->get_glut_window_id() );
GLUI_Master.active_control_glui->special(key,x,y);
finish_drawing();
glutSetWindow( current_window );
}
else
{
if (glut_window->glut_special_CB)
glut_window->glut_special_CB( key, x, y );
}
}
else /*** Nope, event was in a standalone GLUI window **/
{
glui = GLUI_Master.find_glui_by_window_id(glutGetWindow());
if ( glui )
{
glui->special(key,x,y);
finish_drawing();
}
}
}
/********************************************** glui_mouse_func() ********/
void GLUI_Main::mouse_func(int button, int state, int x, int y)
{
GLUI *glui;
int current_window;
GLUI_Glut_Window *glut_window;
current_window = glutGetWindow();
glut_window = GLUI_Master.find_glut_window( current_window );
if ( glut_window ) { /** Was event in a GLUT window? **/
if ( GLUI_Master.active_control_glui != NULL )
GLUI_Master.active_control_glui->deactivate_current_control();
if (glut_window->glut_mouse_CB)
glut_window->glut_mouse_CB( button, state, x, y );
finish_drawing();
}
else { /** Nope - event was in a GLUI standalone window **/
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->passive_motion( 0,0 );
glui->mouse( button, state, x, y );
finish_drawing();
}
}
}
/********************************************** glui_motion_func() ********/
void GLUI_Main::motion_func(int x, int y)
{
GLUI *glui;
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->motion(x,y);
finish_drawing();
}
}
/**************************************** glui_passive_motion_func() ********/
void GLUI_Main::passive_motion_func(int x, int y)
{
GLUI *glui;
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->passive_motion(x,y);
finish_drawing();
}
}
/********************************************** glui_entry_func() ********/
void GLUI_Main::entry_func(int state)
{
GLUI *glui;
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->entry(state);
}
}
/******************************************** glui_visibility_func() ********/
void GLUI_Main::visibility_func(int state)
{
GLUI *glui;
/* printf( "IN GLUI VISIBILITY()\n" ); */
/* fflush( stdout ); */
glui = GLUI_Master.find_glui_by_window_id( glutGetWindow() );
if ( glui ) {
glui->visibility(state);
}
}
/********************************************** glui_idle_func() ********/
/* Send idle event to each glui, then to the main window */
void GLUI_Main::idle_func()
{
GLUI *glui;
glui = (GLUI*) GLUI_Master.gluis.first_child();
while( glui ) {
glui->idle();
finish_drawing();
glui = (GLUI*) glui->next();
}
if ( GLUI_Master.glut_idle_CB ) {
/*** We set the current glut window before calling the user's
idle function, even though glut explicitly says the window id is
undefined in an idle callback. ***/
/** Check what the current window is first ***/
/*** Arbitrarily set the window id to the main gfx window of the
first glui window ***/
/* int current_window, new_window; */
/* current_window = glutGetWindow(); */
/* if (GLUI_Master.gluis.first_child() != NULL ) { */
/* new_window = ((GLUI_Main*)GLUI_Master.gluis.first_child())-> */
/* main_gfx_window_id; */
/* if ( new_window > 0 AND new_window != old_window ) { */
/* --- Window is changed only if its not already the current window ---*/
/* glutSetWindow( new_window ); */
/* } */
/*} */
GLUI_Master.glut_idle_CB();
}
}
/*********************************** GLUI_Master_Object::GLUI_Master_Object() ******/
GLUI_Master_Object::GLUI_Master_Object()
: glui_id_counter(1),
glut_idle_CB(NULL)
{
}
GLUI_Master_Object::~GLUI_Master_Object()
{
}
/*********************************** GLUI_Master_Object::create_glui() ******/
GLUI *GLUI_Master_Object::create_glui( const GLUI_String &name, long flags,int x,int y )
{
GLUI *new_glui = new GLUI;
new_glui->init( name, flags, x, y, -1 );
new_glui->link_this_to_parent_last( &this->gluis );
return new_glui;
}
/************************** GLUI_Master_Object::create_glui_subwindow() ******/
GLUI *GLUI_Master_Object::create_glui_subwindow( int parent_window,
long flags )
{
GLUI *new_glui = new GLUI;
GLUI_String new_name = tfm::format("subwin_%p", this );
new_glui->init( new_name.c_str(), flags | GLUI_SUBWINDOW, 0,0,
parent_window );
new_glui->main_panel->set_int_val( GLUI_PANEL_EMBOSSED );
new_glui->link_this_to_parent_last( &this->gluis );
return new_glui;
}
/********************** GLUI_Master_Object::find_glui_by_window_id() ********/
GLUI *GLUI_Master_Object::find_glui_by_window_id( int window_id )
{
GLUI_Node *node;
node = gluis.first_child();
while( node ) {
if ( ((GLUI*)node)->get_glut_window_id() == window_id )
return (GLUI*) node;
node = node->next();
}
return NULL;
}
/******************************************** GLUI_Main::display() **********/
void GLUI_Main::display()
{
int win_w, win_h;
/* SUBTLE: on freeGLUT, the correct window is always already set.
But older versions of GLUT need this call, or else subwindows
don't update properly when resizing or damage-painting.
*/
glutSetWindow( glut_window_id );
/* Set up OpenGL state for widget drawing */
glDisable( GL_DEPTH_TEST );
glCullFace( GL_BACK );
glDisable( GL_CULL_FACE );
glDisable( GL_LIGHTING );
set_current_draw_buffer();
/**** This function is used as a special place to do 'safe' processing,
e.g., handling window close requests.
That is, we can't close the window directly in the callback, so
we set a flag, post a redisplay message (which eventually calls
this function), then close the window safely in here. ****/
if ( closing ) {
close_internal();
return;
}
/* if ( TEST_AND( this->flags, GLUI_SUBWINDOW ))
check_subwindow_position();
*/
win_w = glutGet( GLUT_WINDOW_WIDTH );
win_h = glutGet( GLUT_WINDOW_HEIGHT );
/*** Check here if the window needs resizing ***/
if ( win_w != main_panel->w OR win_h != main_panel->h ) {
glutReshapeWindow( main_panel->w, main_panel->h );
return;
}
/******* Draw GLUI window ******/
glClearColor( bkgd_color[0] / 255.0f,
bkgd_color[1] / 255.0f,
bkgd_color[2] / 255.0f,
1.0f );
glClear( GL_COLOR_BUFFER_BIT ); /* | GL_DEPTH_BUFFER_BIT ); */
set_ortho_projection();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
/*** Rotate image so y increases downward.
In normal OpenGL, y increases upward. ***/
glTranslatef( (float) win_w/2.0, (float) win_h/2.0, 0.0 );
glRotatef( 180.0, 0.0, 1.0, 0.0 );
glRotatef( 180.0, 0.0, 0.0, 1.0 );
glTranslatef( (float) -win_w/2.0, (float) -win_h/2.0, 0.0 );
// Recursively draw the main panel
// main_panel->draw_bkgd_box( 0, 0, win_w, win_h );
main_panel->draw_recursive( 0, 0 );
switch (buffer_mode) {
case buffer_front: /* Make sure drawing gets to screen */
glFlush();
break;
case buffer_back: /* Bring back buffer to front */
glutSwapBuffers();
break;
}
}
/*************************************** _glutBitmapWidthString() **********/
int _glutBitmapWidthString( void *font, const GLUI_String &s )
{
int width = 0;
for (auto i : s) width += glutBitmapWidth( font, i );
return width;
}
/************************************ _glutBitmapString *********************/
/* Displays the contents of a string using GLUT's bitmap character function */
/* Does not handle newlines */
void _glutBitmapString( void *font, const GLUI_String &s )
{
for (auto i : s) glutBitmapCharacter( font, i );
}
/****************************** GLUI_Main::reshape() **************/
void GLUI_Main::reshape( int reshape_w, int reshape_h )
{
int new_w, new_h;
pack_controls();
new_w = main_panel->w;/* + 1; */
new_h = main_panel->h;/* + 1; */
if ( reshape_w != new_w OR reshape_h != new_h ) {
this->w = new_w;
this->h = new_h;
glutReshapeWindow( new_w, new_h );
}
else {
}
if ( TEST_AND( this->flags, GLUI_SUBWINDOW ) ) {
check_subwindow_position();
/***** if ( TEST_AND(this->flags,GLUI_SUBWINDOW_LEFT )) {
}
else if ( TEST_AND(this->flags,GLUI_SUBWINDOW_LEFT )) {
}
else if ( TEST_AND(this->flags,GLUI_SUBWINDOW_LEFT )) {
}
else if ( TEST_AND(this->flags,GLUI_SUBWINDOW_RIGHT )) {
}
****/
}
glViewport( 0, 0, new_w, new_h );
/* printf( "%d: %d\n", glutGetWindow(), this->flags ); */
glutPostRedisplay();
}
/****************************** GLUI_Main::keyboard() **************/
void GLUI_Main::keyboard(unsigned char key, int x, int y)
{
GLUI_Control *new_control;
curr_modifiers = glutGetModifiers();
/*** If it's a tab or shift tab, we don't pass it on to the controls.
Instead, we use it to cycle through active controls ***/
if ( key == '\t' AND !mouse_button_down AND
(!active_control || !active_control->wants_tabs())) {
if ( curr_modifiers & GLUT_ACTIVE_SHIFT ) {
new_control = find_prev_control( active_control );
}
else {
new_control = find_next_control( active_control );
}
/* if ( new_control )
printf( "new_control: %s\n", new_control->name );
*/
deactivate_current_control();
activate_control( new_control, GLUI_ACTIVATE_TAB );
}
else if ( key == ' ' AND active_control
AND active_control->spacebar_mouse_click ) {
/*** If the user presses the spacebar, and a non-edittext control
is active, we send it a mouse down event followed by a mouse up
event (simulated mouse-click) ***/
active_control->mouse_down_handler( 0, 0 );
active_control->mouse_up_handler( 0, 0, true );
} else {
/*** Pass the keystroke onto the active control, if any ***/
if ( active_control != NULL )
active_control->key_handler( key, curr_modifiers );
}
}
/****************************** GLUI_Main::special() **************/
void GLUI_Main::special(int key, int x, int y)
{
curr_modifiers = glutGetModifiers();
/*** Pass the keystroke onto the active control, if any ***/
if ( active_control != NULL )
active_control->special_handler( key, glutGetModifiers() );
}
/****************************** GLUI_Main::mouse() **************/
void GLUI_Main::mouse(int button, int state, int x, int y)
{
int callthrough;
GLUI_Control *control;
/* printf( "MOUSE: %d %d\n", button, state ); */
callthrough = true;
curr_modifiers = glutGetModifiers();
if ( button == GLUT_LEFT ) {
control = find_control( x, y );
/*if ( control ) printf( "control: %s\n", control->name.c_str() ); */
if ( mouse_button_down AND active_control != NULL AND
state == GLUT_UP )
{
/** We just released the mouse, which was depressed at some control **/
callthrough = active_control->
mouse_up_handler( x, y, control==active_control);
glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
if ( active_control AND
active_control->active_type == GLUI_CONTROL_ACTIVE_MOUSEDOWN AND 0)
{
/*** This is a control that needs to be deactivated when the
mouse button is released ****/
deactivate_current_control();
}
}
else {
if ( control ) {
if ( NOT mouse_button_down AND state == GLUT_DOWN ) {
/*** We just pressed the mouse down at some control ***/
if ( active_control != control ) {
if ( active_control != NULL ) {
/** There is an active control still - deactivate it ***/
deactivate_current_control();
}
}
if ( control->enabled ) {
activate_control( control, GLUI_ACTIVATE_MOUSE );
callthrough = control->mouse_down_handler( x, y );
}
}
}
}
if ( state == GLUT_DOWN )
mouse_button_down = true;
else if ( state == GLUT_UP )
mouse_button_down = false;
}
/**
NO CALLTHROUGH NEEDED FOR MOUSE EVENTS
if ( callthrough AND glut_mouse_CB )
glut_mouse_CB( button, state, x, y );
**/
callthrough=callthrough; /* To get rid of compiler warnings */
}
/****************************** GLUI_Main::motion() **************/
void GLUI_Main::motion(int x, int y)
{
int callthrough;
GLUI_Control *control;
/* printf( "MOTION: %d %d\n", x, y ); */
callthrough = true;
control = find_control(x,y);
if ( mouse_button_down AND active_control != NULL ) {
callthrough =
active_control->mouse_held_down_handler(x,y,control==active_control);
}
/**
NO CALLTHROUGH NEEDED FOR MOUSE EVENTS
if ( callthrough AND glut_motion_CB )
glut_motion_CB(x,y);
**/
callthrough=callthrough; /* To get rid of compiler warnings */
}
/*********************** GLUI_Main::passive_motion() **************/
void GLUI_Main::passive_motion(int x, int y)
{
GLUI_Control *control;
control = find_control( x, y );
/* printf( "%p %p\n", control, mouse_over_control ); */
if ( control != mouse_over_control ) {
if ( mouse_over_control ) {
mouse_over_control->mouse_over( false, x, y );
}
if ( control ) {
control->mouse_over( true, x, y );
mouse_over_control = control;
}
}
/*
if ( curr_cursor != GLUT_CURSOR_INHERIT ) {
curr_cursor = GLUT_CURSOR_INHERIT;
glutSetCursor( GLUT_CURSOR_INHERIT );
}*/
}
/****************************** GLUI_Main::entry() **************/
void GLUI_Main::entry(int state)
{
/*if ( NOT active_control OR ( active_control AND ( active_control->type == GLUI_CONTROL_EDITTEXT
OR active_control->type == GLUI_CONTROL_SPINNER) ) )*/
glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
}
/****************************** GLUI_Main::visibility() **************/
void GLUI_Main::visibility(int state)
{
}
/****************************** GLUI_Main::idle() **************/
void GLUI_Main::idle()
{
/*** Pass the idle event onto the active control, if any ***/
/* printf( "IDLE \t" ); */
if ( active_control != NULL ) {
/* First we check if the control actually needs the idle right now.
Otherwise, let's avoid wasting cycles and OpenGL context switching */
if ( active_control->needs_idle() ) {
/*** Set the current glut window to the glui window */
/*** But don't change the window if we're already at that window ***/
if ( glut_window_id > 0 AND glutGetWindow() != glut_window_id ) {
glutSetWindow( glut_window_id );
}
active_control->idle();
}
}
}
int GLUI_Main::needs_idle()
{
return active_control != NULL && active_control->needs_idle();
}
/******************************************* GLUI_Main::find_control() ******/
GLUI_Control *GLUI_Main::find_control( int x, int y )
{
GLUI_Control *node, *last_container;
last_container = NULL;
node = main_panel;
while( node != NULL ) {
if ( !dynamic_cast<GLUI_Column*>(node) AND
PT_IN_BOX( x, y,
node->x_abs, node->x_abs + node->w,
node->y_abs, node->y_abs + node->h )
)
{
/*** Point is inside current node ***/
if ( node->first_child() == NULL ) {
/*** SPECIAL CASE: for edittext boxes, we make sure click is
in box, and not on name string. This should be generalized
for all controls later... ***/
if ( dynamic_cast<GLUI_EditText*>(node) ) {
if ( x < node->x_abs + ((GLUI_EditText*)node)->text_x_offset )
return (GLUI_Control*) node->parent();
}
return node; /* point is inside this node, and node has no children,
so return this node as the selected node */
}
else {
/*** This is a container class ***/
last_container = node;
node = (GLUI_Control*) node->first_child(); /* Descend into child */
}
}
else {
node = (GLUI_Control*) node->next();
}
}
/** No leaf-level nodes found to accept the mouse click, so
return the last container control found which DOES accept the click **/
if ( last_container ) {
/* printf( "ctrl: '%s'\n", last_container->name ); */
return last_container;
}
else {
return NULL;
}
}
/************************************* GLUI_Main::pack_controls() ***********/
void GLUI_Main::pack_controls()
{
main_panel->pack(0,0);
/**** Now align controls within their bounds ****/
align_controls( main_panel );
/*** If this is a subwindow, expand panel to fit parent window ***/
if ( TEST_AND( this->flags, GLUI_SUBWINDOW ) ) {
int parent_h, parent_w;
int orig_window;
orig_window = glutGetWindow();
glutSetWindow( this->top_level_glut_window_id );
parent_h = glutGet( GLUT_WINDOW_HEIGHT );
parent_w = glutGet( GLUT_WINDOW_WIDTH );
glutSetWindow( orig_window );
/* printf( "%d %d\n", parent_h, parent_w ); */
if ( 1 ) {
if ( TEST_AND(this->flags,GLUI_SUBWINDOW_TOP )) {
main_panel->w = std::max( main_panel->w, parent_w );
}
else if ( TEST_AND(this->flags,GLUI_SUBWINDOW_LEFT )) {
main_panel->h = std::max( main_panel->h, parent_h );
}
else if ( TEST_AND(this->flags,GLUI_SUBWINDOW_BOTTOM )) {
main_panel->w = std::max( main_panel->w, parent_w );