]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
Working on ssg-ifying sky.
[flightgear.git] / src / GUI / gui.cxx
1 /**************************************************************************
2  * gui.cxx
3  *
4  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
5  * project.
6  *
7  * Additional mouse supported added by David Megginson, 1999.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #ifdef FG_MATH_EXCEPTION_CLASH
34 #  include <math.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include <GL/glut.h>
42 #include <simgear/xgl/xgl.h>
43
44 #if defined(FX) && defined(XMESA)
45 #  include <GL/xmesa.h>
46 #endif
47
48 #include STL_STRING
49
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include <simgear/constants.h>
54 #include <simgear/debug/logstream.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/screen/screen-dump.hxx>
57
58 #include <Include/general.hxx>
59 #include <Aircraft/aircraft.hxx>
60 #include <Airports/simple.hxx>
61 #include <Cockpit/panel.hxx>
62 #include <Controls/controls.hxx>
63 #include <FDM/flight.hxx>
64 #include <Main/options.hxx>
65 #include <Main/fg_init.hxx>
66 #include <Main/views.hxx>
67 #ifdef FG_NETWORK_OLK
68 #include <NetworkOLK/network.h>
69 #endif
70 #include <Time/fg_time.hxx>
71
72 #if defined( WIN32 ) && !defined( __CYGWIN__ )
73 #  include <Screen/win32-printer.h>
74 #  include <Screen/GlBitmaps.h>
75 #endif
76
77 /*
78  * trackball.h
79  * A virtual trackball implementation
80  * Written by Gavin Bell for Silicon Graphics, November 1988.
81  */
82 /*
83  * Pass the x and y coordinates of the last and current positions of
84  * the mouse, scaled so they are from (-1.0 ... 1.0).
85  *
86  * The resulting rotation is returned as a quaternion rotation in the
87  * first paramater.
88  */
89 void
90 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
91
92 /*
93  * Given two quaternions, add them together to get a third quaternion.
94  * Adding quaternions to get a compound rotation is analagous to adding
95  * translations to get a compound translation.  When incrementally
96  * adding rotations, the first argument here should be the new
97
98   * rotation, the second and third the total rotation (which will be
99  * over-written with the resulting new total rotation).
100  */
101 void
102 add_quats(float *q1, float *q2, float *dest);
103
104 /*
105  * A useful function, builds a rotation matrix in Matrix based on
106  * given quaternion.
107  */
108 void
109 build_rotmatrix(float m[4][4], float q[4]);
110
111 /*
112  * This function computes a quaternion based on an axis (defined by
113  * the given vector) and an angle about which to rotate.  The angle is
114  * expressed in radians.  The result is put into the third argument.
115  */
116 void
117 axis_to_quat(float a[3], float phi, float q[4]);
118
119
120 #include "gui.h"
121
122 FG_USING_STD(string);
123
124 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
125 FG_USING_STD(cout);
126 #endif
127
128 #if defined(WIN32) || defined(__CYGWIN32__)
129 #define WIN32_CURSOR_TWEAKS
130 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
131 #define X_CURSOR_TWEAKS
132 #endif
133
134 // hack, should come from an include someplace
135 extern void fgInitVisuals( void );
136 extern void fgReshape( int width, int height );
137 extern void fgRenderFrame( void );
138
139 puFont guiFnt = 0;
140 fntTexFont *guiFntHandle = 0;
141
142 static puMenuBar    *mainMenuBar = 0;
143 //static puButton     *hideMenuButton = 0;
144
145 static puDialogBox  *dialogBox = 0;
146 static puFrame      *dialogFrame = 0;
147 static puText       *dialogBoxMessage = 0;
148 static puOneShot    *dialogBoxOkButton = 0;
149
150
151 static puDialogBox  *YNdialogBox = 0;
152 static puFrame      *YNdialogFrame = 0;
153 static puText       *YNdialogBoxMessage = 0;
154 static puOneShot    *YNdialogBoxOkButton = 0;
155 static puOneShot    *YNdialogBoxNoButton = 0;
156
157 static char msg_OK[]     = "OK";
158 static char msg_NO[]     = "NO";
159 static char msg_YES[]    = "YES";
160 static char msg_CANCEL[] = "Cancel";
161 static char msg_RESET[]  = "Reset";
162
163 char *gui_msg_OK;     // "OK"
164 char *gui_msg_NO;     // "NO"
165 char *gui_msg_YES;    // "YES"
166 char *gui_msg_CANCEL; // "CANCEL"
167 char *gui_msg_RESET;  // "RESET"
168
169 static char global_dialog_string[256];
170
171 // from autopilot.cxx
172 extern void NewAltitude( puObject *cb );
173 extern void NewHeading( puObject *cb );
174 extern void fgAPAdjust( puObject * );
175 extern void NewTgtAirport( puObject *cb );
176 bool fgAPTerrainFollowEnabled( void );
177 bool fgAPAltitudeEnabled( void );
178 bool fgAPHeadingEnabled( void );
179 bool fgAPWayPointEnabled( void );
180 bool fgAPAutoThrottleEnabled( void );
181
182 // from cockpit.cxx
183 extern void fgLatLonFormatToggle( puObject *);
184
185 /* --------------------------------------------------------------------
186 Mouse stuff
187 ---------------------------------------------------------------------*/
188
189 static int _mX = 0;
190 static int _mY = 0;
191 static int _savedX = 0;
192 static int _savedY = 0;
193 static int last_buttons = 0 ;
194 static int mouse_active = 0;
195 static int menu_on = 0;
196 static int mouse_joystick_control = 0;
197
198 //static time_t mouse_off_time;
199 //static int mouse_timed_out;
200
201 // to allow returning to previous view
202 // on second left click in MOUSE_VIEW mode
203 // This has file scope so that it can be reset
204 // if the little rodent is moved  NHV
205 static  int _mVtoggle;
206
207 // we break up the glutGetModifiers return mask
208 // once per loop and stash what we need in these
209 static int glut_active_shift;
210 static int glut_active_ctrl;
211 static int glut_active_alt;
212
213 static float lastquat[4];
214 static float curquat[4];
215 static float _quat0[4];
216 float quat_mat[4][4];
217
218 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
219 // else smooth out the view panning to .01 radian per frame
220 // see view_offset smoothing mechanism in main.cxx
221 #define NO_SMOOTH_MOUSE_VIEW
222
223 // uncomment following to
224 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
225
226 /* --------------------------------------------------------------------
227 Support for mouse as control yoke (david@megginson.com)
228
229 - right button toggles between pointer and yoke
230 - horizontal drag with no buttons moves ailerons
231 - vertical drag with no buttons moves elevators
232 - horizontal drag with left button moves brakes (left=on)
233 - vertical drag with left button moves throttle (up=more)
234 - horizontal drag with middle button moves rudder
235 - vertical drag with middle button moves trim
236
237 For the *_sensitivity variables, a lower number means more sensitive.
238
239 TODO: figure out how to keep pointer from leaving window in yoke mode.
240 TODO: add thresholds and null zones
241 TODO: sensitivity should be configurable at user option.
242 TODO: allow differential braking (this will be useful if FlightGear
243       ever supports tail-draggers like the DC-3)
244 ---------------------------------------------------------------------*/
245
246 typedef enum {
247     MOUSE_POINTER,
248     MOUSE_YOKE,
249     MOUSE_VIEW
250 } MouseMode;
251
252 MouseMode mouse_mode = MOUSE_POINTER;
253
254 static double aileron_sensitivity = 1.0/500.0;
255 static double elevator_sensitivity = 1.0/500.0;
256 static double brake_sensitivity = 1.0/250.0;
257 static double throttle_sensitivity = 1.0/250.0;
258 static double rudder_sensitivity = 1.0/500.0;
259 static double trim_sensitivity = 1.0/1000.0;
260
261 static inline void Quat0( void ) {
262     curquat[0] = _quat0[0];
263     curquat[1] = _quat0[1];
264     curquat[2] = _quat0[2];
265     curquat[3] = _quat0[3];
266 }
267
268 static inline int left_button( void ) {
269     return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
270 }
271
272 static inline int middle_button( void ) {
273     return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
274 }
275
276 static inline int right_button( void ) {
277     return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
278 }
279
280 static inline void TurnCursorOn( void )
281 {
282     mouse_active = ~0;
283 #if defined(WIN32_CURSOR_TWEAKS)
284     switch (mouse_mode) {
285         case MOUSE_POINTER:
286             glutSetCursor(GLUT_CURSOR_INHERIT);
287             break;
288         case MOUSE_YOKE:
289             glutSetCursor(GLUT_CURSOR_CROSSHAIR);
290             break;
291         case MOUSE_VIEW:
292             glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
293             break;
294     }
295 #endif
296 #if defined(X_CURSOR_TWEAKS)
297     glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
298 #endif
299 }
300
301 static inline void TurnCursorOff( void )
302 {
303     mouse_active = 0;
304 #if defined(WIN32_CURSOR_TWEAKS)
305     glutSetCursor(GLUT_CURSOR_NONE);
306 #elif defined(X_CURSOR_TWEAKS)
307     glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
308 #endif
309 }
310
311 void maybeToggleMouse( void )
312 {
313 #if defined(WIN32_CURSOR_TWEAKS)
314     static int first_time = ~0;
315     static int mouse_changed = 0;
316
317     if ( first_time ) {
318         if(!mouse_active) {
319             mouse_changed = ~mouse_changed;
320             TurnCursorOn();
321         }
322     } else {
323         if( mouse_mode != MOUSE_POINTER )
324             return;
325         if( mouse_changed ) {
326             mouse_changed = ~mouse_changed;
327             if(mouse_active) {
328                 TurnCursorOff();
329             }
330         }
331     }
332     first_time = ~first_time;
333 #endif // #ifdef WIN32
334 }
335
336 // Call with FALSE to init and TRUE to restore
337 void BusyCursor( int restore )
338 {
339     static GLenum cursor = (GLenum) 0;
340     if( restore ) {
341         glutSetCursor(cursor);
342     } else {
343         cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
344 #if defined(WIN32_CURSOR_TWEAKS)
345         TurnCursorOn();
346 #endif
347         glutSetCursor( GLUT_CURSOR_WAIT );
348     }
349 }
350
351 int guiGetMouseButton(void)
352 {
353     return last_buttons;
354 }
355
356 void guiGetMouse(int *x, int *y)
357 {
358     *x = _mX;
359     *y = _mY;
360 };
361
362 void guiMotionFunc ( int x, int y )
363 {
364     int ww, wh, need_warp = 0;
365     float W, H;
366     double offset;
367 //  FGTime *t = FGTime::cur_time_params;
368 //  if( mouse_timed_out ) {
369 //      if( t->get_cur_time() > mouse_off_time ) {
370 //          moused_timed_out = 0;
371 //          TurnCursorOn();
372 //          glutPostRedisplay () ;
373 //      }
374 //  }
375
376     if (mouse_mode == MOUSE_POINTER) {
377         puMouse ( x, y ) ;
378         glutPostRedisplay () ;
379     } else {
380         if( x == _mX && y == _mY)
381             return;
382         
383         // reset left click MOUSE_VIEW toggle feature
384         _mVtoggle = 0;
385         
386         ww = current_view.get_winWidth();
387         wh = current_view.get_winHeight();
388         
389         switch (mouse_mode) {
390             case MOUSE_YOKE:
391                 if( !mouse_joystick_control ) {
392                     mouse_joystick_control = 1;
393                     current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
394                 } else {
395                     if ( left_button() ) {
396                         offset = (_mX - x) * brake_sensitivity;
397                         controls.move_brake(FGControls::ALL_WHEELS, offset);
398                         offset = (_mY - y) * throttle_sensitivity;
399                         controls.move_throttle(FGControls::ALL_ENGINES, offset);
400                     } else if ( right_button() ) {
401                         if( !fgAPHeadingEnabled() ) {
402                             offset = (x - _mX) * rudder_sensitivity;
403                             controls.move_rudder(offset);
404                         }
405                         if( !fgAPAltitudeEnabled() ) {
406                             offset = (_mY - y) * trim_sensitivity;
407                             controls.move_elevator_trim(offset);
408                         }
409                     } else {
410                         if( !fgAPHeadingEnabled() ) {
411                             offset = (x - _mX) * aileron_sensitivity;
412                             controls.move_aileron(offset);
413                         }
414                         if( !fgAPAltitudeEnabled() ) {
415                             offset = (_mY - y) * elevator_sensitivity;
416                             controls.move_elevator(offset);
417                         }
418                     }
419                 }
420                 // Keep the mouse in the window.
421                 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
422                     x = ww / 2;
423                     y = wh / 2;
424                     need_warp = 1;
425                 }
426                 break;
427                 
428             case MOUSE_VIEW:
429                 if( y <= 0 ) {
430 #define CONTRAINED_MOUSE_VIEW_Y
431 #ifdef CONTRAINED_MOUSE_VIEW_Y
432                     y = 1;
433 #else
434                     y = wh-2;
435 #endif // CONTRAINED_MOUSE_VIEW_Y
436                     need_warp = 1;
437                 } else if( y >= wh-1) {
438 #ifdef CONTRAINED_MOUSE_VIEW_Y
439                     y = wh-2;
440 #else
441                     y = 1;
442 #endif // CONTRAINED_MOUSE_VIEW_Y
443                     need_warp = 1;
444                 }
445                 // wrap MOUSE_VIEW mode cursor x position
446                 if ( x <= 0 ) {
447                     need_warp = 1;
448                     x = ww-2;
449                 } else if ( x >= ww-1 ) {
450                     need_warp = 1;
451                     x = 1;
452                 }
453                 // try to get FG_PI movement in each half of screen
454                 // do spherical pan
455                 W = ww;
456                 H = wh;
457                 if( middle_button() ) {
458                     trackball(lastquat,
459                               (2.0f * _mX - W) / W,
460                               0, //(H - 2.0f * y) / H,         // 3
461                               (2.0f * x - W) / W,
462                               0 //(H - 2.0f * _mY) / H       // 1
463                              );
464                     x = _mX;
465                     y = _mY;
466                     need_warp = 1;
467                 } else {
468                     trackball(lastquat,
469                               0, //(2.0f * _mX - W) / W,  // 0
470                               (H - 2.0f * y) / H,         // 3
471                               0, //(2.0f * x - W) / W,    // 2
472                               (H - 2.0f * _mY) / H        // 1 
473                              );
474                 }
475                 add_quats(lastquat, curquat, curquat);
476                 build_rotmatrix(quat_mat, curquat);
477                 
478                 // do horizontal pan
479                 // this could be done in above quat
480                 // but requires redoing view pipeline
481                 offset = current_view.get_goal_view_offset();
482                 offset += ((_mX - x) * FG_2PI / W );
483                 while (offset < 0.0) {
484                     offset += FG_2PI;
485                 }
486                 while (offset > FG_2PI) {
487                     offset -= FG_2PI;
488                 }
489                 current_view.set_goal_view_offset(offset);
490 #ifdef NO_SMOOTH_MOUSE_VIEW
491                 current_view.set_view_offset(offset);
492 #endif
493                 break;
494             
495             default:
496                 break;
497         }
498     }
499     if( need_warp)
500         glutWarpPointer(x, y);
501     
502     // Record the new mouse position.
503     _mX = x;
504     _mY = y;
505 }
506
507
508 void guiMouseFunc(int button, int updown, int x, int y)
509 {
510     int glutModifiers;
511
512     // private MOUSE_VIEW state variables
513     // to allow alternate left clicks in MOUSE_VIEW mode
514     // to toggle between current offsets and straight ahead
515     // uses _mVtoggle
516     static int _mVx, _mVy, _Vx, _Vy;
517     static float _quat[4];
518     static double _view_offset;
519     
520     // general purpose variables
521     double offset;
522             
523     glutModifiers = glutGetModifiers();
524     glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
525     glut_active_ctrl  = glutModifiers & GLUT_ACTIVE_CTRL; 
526     glut_active_alt   = glutModifiers & GLUT_ACTIVE_ALT;
527     
528     // Was the left button pressed?
529     if (updown == GLUT_DOWN ) {
530         if( button == GLUT_LEFT_BUTTON)
531         {
532             switch (mouse_mode) {
533                 case MOUSE_POINTER:
534                     break;
535                 case MOUSE_YOKE:
536                     break;
537                 case MOUSE_VIEW:
538                     if(_mVtoggle) {
539                         // resume previous view offsets
540                         _mX = _mVx;
541                         _mY = _mVy;
542                         x = _Vx;
543                         y = _Vy;
544                         curquat[0] = _quat[0];
545                         curquat[1] = _quat[1];
546                         curquat[2] = _quat[2];
547                         curquat[3] = _quat[3];
548                         current_view.set_goal_view_offset(_view_offset);
549 #ifdef NO_SMOOTH_MOUSE_VIEW
550                         current_view.set_view_offset(_view_offset);
551 #endif
552                     } else {
553                         // center view
554                         _mVx = _mX;
555                         _mVy = _mY;
556                         _Vx = x;
557                         _Vy = y;
558                         _quat[0] = curquat[0];
559                         _quat[1] = curquat[1];
560                         _quat[2] = curquat[2];
561                         _quat[3] = curquat[3];
562                         x = current_view.get_winWidth()/2;
563                         y = current_view.get_winHeight()/2;
564                         Quat0();
565                         _view_offset = current_view.get_goal_view_offset();
566                         current_view.set_goal_view_offset(0.0);
567 #ifdef NO_SMOOTH_MOUSE_VIEW
568                         current_view.set_view_offset(0.0);
569 #endif
570                     }
571                     glutWarpPointer( x , y);
572                     build_rotmatrix(quat_mat, curquat);
573                     _mVtoggle = ~_mVtoggle;
574                     break;
575             }
576         }else if ( button == GLUT_RIGHT_BUTTON) {
577             switch (mouse_mode) {
578                 case MOUSE_POINTER:
579                     mouse_mode = MOUSE_YOKE;
580                     mouse_joystick_control = 0;
581                     _savedX = x;
582                     _savedY = y;
583                     // start with zero point in center of screen
584                     _mX = current_view.get_winWidth()/2;
585                     _mY = current_view.get_winHeight()/2;
586                     
587                     // try to have the MOUSE_YOKE position
588                     // reflect the current stick position
589                     offset = controls.get_aileron();
590                     x = _mX - (int)(offset * aileron_sensitivity);
591                     offset = controls.get_elevator();
592                     y = _mY - (int)(offset * elevator_sensitivity);
593                     
594                     glutSetCursor(GLUT_CURSOR_CROSSHAIR);
595                     FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
596                     break;
597                     
598                 case MOUSE_YOKE:
599                     mouse_mode = MOUSE_VIEW;
600                     current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
601                     x = current_view.get_winWidth()/2;
602                     y = current_view.get_winHeight()/2;
603                     _mVtoggle = 0;
604                     Quat0();
605                     build_rotmatrix(quat_mat, curquat);
606                     glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
607                     FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
608                     break;
609                     
610                 case MOUSE_VIEW:
611                     mouse_mode = MOUSE_POINTER;
612                     x = _savedX;
613                     y = _savedY;
614 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
615                     Quat0();
616                     build_rotmatrix(quat_mat, curquat);
617                     current_view.set_goal_view_offset(0.0);
618 #ifdef NO_SMOOTH_MOUSE_VIEW
619                     current_view.set_view_offset(0.0);
620 #endif
621 #endif      // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
622                     glutSetCursor(GLUT_CURSOR_INHERIT);
623                     
624                     if(!menu_on)
625                         TurnCursorOff();
626                     
627                     FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
628                     break;
629             }     
630             glutWarpPointer( x, y );
631         } // END RIGHT BUTTON
632     } // END UPDOWN == GLUT_DOWN
633     
634     // Note which button is pressed.
635     if ( updown == GLUT_DOWN ) {
636         last_buttons |=  ( 1 << button ) ;
637     } else {
638         last_buttons &= ~( 1 << button ) ;
639     }
640     
641     // If we're in pointer mode, let PUI
642     // know what's going on.
643     if (mouse_mode == MOUSE_POINTER) {
644         puMouse (button, updown, x,y);
645     }
646     
647     // Register the new position (if it
648     // hasn't been registered already).
649     _mX = x;
650     _mY = y;
651     
652     glutPostRedisplay ();
653 }
654
655 /* ================ General Purpose Functions ================ */
656
657 // Intercept the Escape Key
658 void ConfirmExitDialog(void)
659 {
660     FG_PUSH_PUI_DIALOG( YNdialogBox );
661 }
662
663 // General Purpose Message Box
664 void mkDialog (const char *txt)
665 {
666     strncpy(global_dialog_string, txt, 256);
667     dialogBoxMessage->setLabel(global_dialog_string);
668     FG_PUSH_PUI_DIALOG( dialogBox );
669 }
670
671 // Repair any damage done to the Panel by other Gui Items
672 void guiFixPanel( void )
673 {
674     int toggle_pause;
675
676     if ( current_options.get_panel_status() ) {
677         FGView *v = &current_view;
678         FGTime *t = FGTime::cur_time_params;
679
680         if( (toggle_pause = !t->getPause()) )
681             t->togglePauseMode();
682
683         // this seems to be the only way to do this :-(
684         // problem is the viewport has been mucked with
685         xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) );
686         FGPanel::OurPanel->ReInit(0, 0, 1024, 768);
687
688         if(toggle_pause)
689             t->togglePauseMode();
690     }
691 }
692
693 // Toggle the Menu and Mouse display state
694 void guiToggleMenu(void)
695 {
696     if( menu_on ) {
697         // printf("Hiding Menu\n");
698         mainMenuBar->hide  ();
699 #if defined(WIN32_CURSOR_TWEAKS)
700         if( mouse_mode == MOUSE_POINTER )
701             TurnCursorOff();
702 #endif // #ifdef WIN32_CURSOR_TWEAKS
703     } else {
704         // printf("Showing Menu\n");
705         mainMenuBar->reveal();
706 #ifdef WIN32
707         TurnCursorOn();
708 #endif // #ifdef WIN32
709     }
710     menu_on = ~menu_on;
711 }
712     
713 /* -----------------------------------------------------------------------
714 the Gui callback functions 
715 ____________________________________________________________________*/
716
717 void reInit(puObject *cb)
718 {
719     BusyCursor(0);
720     Quat0();
721     build_rotmatrix(quat_mat, curquat);
722     fgReInitSubsystems();
723     BusyCursor(1);
724 }
725         
726 // This is the accessor function
727 void guiTogglePanel(puObject *cb)
728 {
729     current_options.toggle_panel();
730 }
731     
732 //void MenuHideMenuCb(puObject *cb)
733 void hideMenuCb (puObject *cb)
734 {
735     guiToggleMenu();
736 }
737
738 void goodBye(puObject *)
739 {
740     // FG_LOG( FG_INPUT, FG_ALERT,
741     //      "Program exiting normally at user request." );
742     cout << "Program exiting normally at user request." << endl;
743
744 #ifdef FG_NETWORK_OLK    
745     if ( current_options.get_network_olk() ) {
746         if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
747     }
748 #endif
749
750     //  if(gps_bug)
751     //      fclose(gps_bug);
752
753     exit(-1);
754 }
755
756
757 void goAwayCb (puObject *me)
758 {
759     FG_POP_PUI_DIALOG( dialogBox );
760 }
761
762 void mkDialogInit (void)
763 {
764     //  printf("mkDialogInit\n");
765     int x = (current_options.get_xsize()/2 - 400/2);
766     int y = (current_options.get_ysize()/2 - 100/2);
767     dialogBox = new puDialogBox (x, y); // 150, 50
768     {
769         dialogFrame = new puFrame (0,0,400,100);
770         dialogBoxMessage  =  new puText         (10, 70);
771         dialogBoxMessage  -> setLabel           ("");
772         dialogBoxOkButton =  new puOneShot      (180, 10, 240, 50);
773         dialogBoxOkButton -> setLegend          (gui_msg_OK);
774         dialogBoxOkButton -> makeReturnDefault  (TRUE );
775         dialogBoxOkButton -> setCallback        (goAwayCb);
776     }
777     FG_FINALIZE_PUI_DIALOG( dialogBox );
778 }
779
780 void MayBeGoodBye(puObject *)
781 {
782     ConfirmExitDialog(); 
783 }
784
785 void goAwayYesNoCb(puObject *me)
786 {
787     FG_POP_PUI_DIALOG( YNdialogBox);
788 }
789
790 void ConfirmExitDialogInit(void)
791 {
792     char msg[] = "Really Quit";
793     char *s;
794
795     //  printf("ConfirmExitDialogInit\n");
796     int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
797
798     int x = (current_options.get_xsize()/2 - 400/2);
799     int y = (current_options.get_ysize()/2 - 100/2);
800         
801     YNdialogBox = new puDialogBox (x, y); // 150, 50
802     //  YNdialogBox = new puDialogBox (150, 50);
803     {
804         YNdialogFrame = new puFrame (0,0,400, 100);
805         
806         YNdialogBoxMessage  =  new puText         (len, 70);
807         YNdialogBoxMessage  -> setDefaultValue    (msg);
808         YNdialogBoxMessage  -> getDefaultValue    (&s);
809         YNdialogBoxMessage  -> setLabel           (s);
810         
811         YNdialogBoxOkButton =  new puOneShot      (100, 10, 160, 50);
812         YNdialogBoxOkButton -> setLegend          (gui_msg_OK);
813         YNdialogBoxOkButton -> setCallback        (goodBye);
814         
815         YNdialogBoxNoButton =  new puOneShot      (240, 10, 300, 50);
816         YNdialogBoxNoButton -> setLegend          (gui_msg_NO);
817         //      YNdialogBoxNoButton -> makeReturnDefault  (TRUE );
818         YNdialogBoxNoButton -> setCallback        (goAwayYesNoCb);
819     }
820     FG_FINALIZE_PUI_DIALOG( YNdialogBox );
821 }
822
823 void notCb (puObject *)
824 {
825     mkDialog ("This function isn't implemented yet");
826 }
827
828 void helpCb (puObject *)
829 {
830     string command;
831         
832 #if defined(FX) && !defined(WIN32)
833 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
834     if ( global_fullscreen ) {
835         global_fullscreen = false;
836         XMesaSetFXmode( XMESA_FX_WINDOW );
837     }
838 #  endif
839 #endif
840         
841 #if !defined(WIN32)
842     string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
843         
844     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
845         command = "netscape -remote \"openURL(" + url + ")\" &";
846     } else {
847         command = "netscape " + url + " &";
848     }
849 #else
850     command = "webrun.bat";
851 #endif
852         
853     system( command.c_str() );
854     //string text = "Help started in netscape window.";
855
856     //mkDialog (text.c_str());
857     mkDialog ("Help started in netscape window.");
858 }
859
860 #if defined( WIN32 ) && !defined( __CYGWIN__)
861
862 static void rotateView( double roll, double pitch, double yaw )
863 {
864         // rotate view
865 }
866
867 static GLBitmap *b1 = NULL;
868 extern FGInterface cur_view_fdm;
869 GLubyte *hiResScreenCapture( int multiplier )
870 {
871         float oldfov = current_options.get_fov();
872         float fov = oldfov / multiplier;
873         FGView *v = &current_view;
874         current_options.set_fov(fov);
875         v->force_update_fov_math();
876     fgInitVisuals();
877     int cur_width = current_view.get_winWidth( );
878     int cur_height = current_view.get_winHeight( );
879         if (b1) delete( b1 );
880         // New empty (mostly) bitmap
881         b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
882         int x,y;
883         for ( y = 0; y < multiplier; y++ )
884         {
885                 for ( x = 0; x < multiplier; x++ )
886                 {
887                         fgReshape( cur_width, cur_height );
888                         // pan to tile
889                         rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
890                         fgRenderFrame();
891                         // restore view
892                         GlBitmap b2;
893                         b1->copyBitmap( &b2, cur_width*x, cur_height*y );
894                 }
895         }
896         current_view.UpdateViewParams(cur_view_fdm);
897         current_options.set_fov(oldfov);
898         v->force_update_fov_math();
899         return b1->getBitmap();
900 }
901 #endif
902
903
904 #if defined( WIN32 ) && !defined( __CYGWIN__)
905 // win32 print screen function
906 void printScreen ( puObject *obj ) {
907     bool show_pu_cursor = false;
908     TurnCursorOff();
909     if ( !puCursorIsHidden() ) {
910         show_pu_cursor = true;
911         puHideCursor();
912     }
913     BusyCursor( 0 );
914     mainMenuBar->hide();
915
916     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
917     int cur_width = current_view.get_winWidth( );
918     int cur_height = current_view.get_winHeight( );
919     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
920         p.End( hiResScreenCapture(3) );
921
922     if( menu_on ) {
923         mainMenuBar->reveal();
924     }
925     BusyCursor(1);
926     if ( show_pu_cursor ) {
927         puShowCursor();
928     }
929     TurnCursorOn();
930 }
931 #endif // #ifdef WIN32
932
933
934 void dumpSnapShot ( puObject *obj ) {
935     fgDumpSnapShot();
936 }
937
938
939 // do a screen snap shot
940 void fgDumpSnapShot () {
941     bool show_pu_cursor = false;
942
943     mainMenuBar->hide();
944     TurnCursorOff();
945     if ( !puCursorIsHidden() ) {
946         show_pu_cursor = true;
947         puHideCursor();
948     }
949
950     fgInitVisuals();
951     fgReshape( current_options.get_xsize(), current_options.get_ysize() );
952
953     // we need two render frames here to clear the menu and cursor
954     // ... not sure why but doing an extra fgFenderFrame() shoulnd't
955     // hurt anything
956     fgRenderFrame();
957     fgRenderFrame();
958
959     my_glDumpWindow( "fgfs-screen.ppm", 
960                      current_options.get_xsize(), 
961                      current_options.get_ysize() );
962     
963     mkDialog ("Snap shot saved to fgfs-screen.ppm");
964
965     if ( show_pu_cursor ) {
966         puShowCursor();
967     }
968
969     TurnCursorOn();
970     if( menu_on ) {
971         mainMenuBar->reveal();
972     }
973
974 }
975
976
977 /// The beginnings of teleportation :-)
978 //  Needs cleaning up but works
979 //  These statics should disapear when this is a class
980 static puDialogBox     *AptDialog = 0;
981 static puFrame         *AptDialogFrame = 0;
982 static puText          *AptDialogMessage = 0;
983 static puInput         *AptDialogInput = 0;
984
985 static char NewAirportId[16];
986 static char NewAirportLabel[] = "Enter New Airport ID"; 
987
988 static puOneShot       *AptDialogOkButton = 0;
989 static puOneShot       *AptDialogCancelButton = 0;
990 static puOneShot       *AptDialogResetButton = 0;
991
992 void AptDialog_Cancel(puObject *)
993 {
994     FG_POP_PUI_DIALOG( AptDialog );
995 }
996
997 void AptDialog_OK (puObject *)
998 {
999         fgAIRPORTS airports;
1000         fgAIRPORT a;
1001     
1002     FGTime *t = FGTime::cur_time_params;
1003     int PauseMode = t->getPause();
1004     if(!PauseMode)
1005         t->togglePauseMode();
1006
1007     char *s;
1008     AptDialogInput->getValue(&s);
1009     string AptId(s);
1010
1011         cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1012     
1013     AptDialog_Cancel( NULL );
1014     
1015     if ( AptId.length() ) {
1016         // set initial position from airport id
1017         FG_LOG( FG_GENERAL, FG_INFO,
1018                 "Attempting to set starting position from airport code "
1019                 << AptId );
1020
1021         airports.load("apt_simple");
1022         if ( airports.search( AptId, &a ) )
1023         {
1024             current_options.set_airport_id( AptId.c_str() );
1025             BusyCursor(0);
1026             fgReInitSubsystems();
1027             BusyCursor(1);
1028         } else {
1029             AptId  += " not in database.";
1030             mkDialog(AptId.c_str());
1031         }
1032     }
1033     if( PauseMode != t->getPause() )
1034         t->togglePauseMode();
1035 }
1036
1037 void AptDialog_Reset(puObject *)
1038 {
1039     //  strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1040     sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1041     AptDialogInput->setValue ( NewAirportId );
1042     AptDialogInput->setCursor( 0 ) ;
1043 }
1044
1045 void NewAirport(puObject *cb)
1046 {
1047     //  strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1048     sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1049 //      cout << "NewAirport " << NewAirportId << endl;
1050     AptDialogInput->setValue( NewAirportId );
1051
1052     FG_PUSH_PUI_DIALOG( AptDialog );
1053 }
1054
1055 static void NewAirportInit(void)
1056 {
1057     sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1058     int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1059                                       NewAirportLabel ) / 2;
1060
1061     AptDialog = new puDialogBox (150, 50);
1062     {
1063         AptDialogFrame   = new puFrame           (0,0,350, 150);
1064         AptDialogMessage = new puText            (len, 110);
1065         AptDialogMessage ->    setLabel          (NewAirportLabel);
1066
1067         AptDialogInput   = new puInput           (50, 70, 300, 100);
1068         AptDialogInput   ->    setValue          (NewAirportId);
1069         AptDialogInput   ->    acceptInput();
1070
1071         AptDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
1072         AptDialogOkButton     ->     setLegend   (gui_msg_OK);
1073         AptDialogOkButton     ->     setCallback (AptDialog_OK);
1074         AptDialogOkButton     ->     makeReturnDefault(TRUE);
1075
1076         AptDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
1077         AptDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
1078         AptDialogCancelButton ->     setCallback (AptDialog_Cancel);
1079
1080         AptDialogResetButton  =  new puOneShot   (240, 10, 300, 50);
1081         AptDialogResetButton  ->     setLegend   (gui_msg_RESET);
1082         AptDialogResetButton  ->     setCallback (AptDialog_Reset);
1083     }
1084         cout << "NewAirportInit " << NewAirportId << endl;
1085     FG_FINALIZE_PUI_DIALOG( AptDialog );
1086 }
1087
1088 #ifdef FG_NETWORK_OLK
1089
1090 /// The beginnings of networking :-)
1091 //  Needs cleaning up but works
1092 //  These statics should disapear when this is a class
1093 static puDialogBox     *NetIdDialog = 0;
1094 static puFrame         *NetIdDialogFrame = 0;
1095 static puText          *NetIdDialogMessage = 0;
1096 static puInput         *NetIdDialogInput = 0;
1097
1098 static char NewNetId[16];
1099 static char NewNetIdLabel[] = "Enter New Callsign"; 
1100 extern char *fgd_callsign;
1101
1102 static puOneShot       *NetIdDialogOkButton = 0;
1103 static puOneShot       *NetIdDialogCancelButton = 0;
1104
1105 void NetIdDialog_Cancel(puObject *)
1106 {
1107     FG_POP_PUI_DIALOG( NetIdDialog );
1108 }
1109
1110 void NetIdDialog_OK (puObject *)
1111 {
1112     string NetId;
1113     
1114     FGTime *t = FGTime::cur_time_params;
1115     int PauseMode = t->getPause();
1116     if(!PauseMode)
1117         t->togglePauseMode();
1118 /*  
1119    The following needs some cleanup because 
1120    "string options.NetId" and "char *net_callsign" 
1121 */
1122     NetIdDialogInput->getValue(&net_callsign);
1123     NetId = net_callsign;
1124     
1125     NetIdDialog_Cancel( NULL );
1126     current_options.set_net_id( NetId.c_str() );
1127     strcpy( fgd_callsign, net_callsign);
1128 //    strcpy( fgd_callsign, current_options.get_net_id().c_str());
1129 /* Entering a callsign indicates : user wants Net HUD Info */
1130     net_hud_display = 1;
1131
1132     if( PauseMode != t->getPause() )
1133         t->togglePauseMode();
1134 }
1135
1136 void NewCallSign(puObject *cb)
1137 {
1138     sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1139 //    sprintf( NewNetId, "%s", fgd_callsign );
1140     NetIdDialogInput->setValue( NewNetId );
1141
1142     FG_PUSH_PUI_DIALOG( NetIdDialog );
1143 }
1144
1145 static void NewNetIdInit(void)
1146 {
1147     sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1148 //    sprintf( NewNetId, "%s", fgd_callsign );
1149     int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1150                                       NewNetIdLabel ) / 2;
1151
1152     NetIdDialog = new puDialogBox (150, 50);
1153     {
1154         NetIdDialogFrame   = new puFrame           (0,0,350, 150);
1155         NetIdDialogMessage = new puText            (len, 110);
1156         NetIdDialogMessage ->    setLabel          (NewNetIdLabel);
1157
1158         NetIdDialogInput   = new puInput           (50, 70, 300, 100);
1159         NetIdDialogInput   ->    setValue          (NewNetId);
1160         NetIdDialogInput   ->    acceptInput();
1161
1162         NetIdDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
1163         NetIdDialogOkButton     ->     setLegend   (gui_msg_OK);
1164         NetIdDialogOkButton     ->     setCallback (NetIdDialog_OK);
1165         NetIdDialogOkButton     ->     makeReturnDefault(TRUE);
1166
1167         NetIdDialogCancelButton =  new puOneShot   (240, 10, 300, 50);
1168         NetIdDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
1169         NetIdDialogCancelButton ->     setCallback (NetIdDialog_Cancel);
1170
1171     }
1172     FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1173 }
1174
1175 static void net_display_toggle( puObject *cb)
1176 {
1177         net_hud_display = (net_hud_display) ? 0 : 1;
1178         printf("Toggle net_hud_display : %d\n", net_hud_display);
1179 }
1180
1181 static void net_register( puObject *cb)
1182 {
1183         fgd_send_com( "1", FGFS_host );
1184         net_is_registered = 0;
1185         printf("Registering to deamon\n");
1186 }
1187
1188 static void net_unregister( puObject *cb)
1189 {
1190         fgd_send_com( "8", FGFS_host );
1191         net_is_registered = -1;
1192         printf("Unregistering from deamon\n");
1193 }
1194
1195
1196 /*************** Deamon communication **********/
1197
1198 //  These statics should disapear when this is a class
1199 static puDialogBox     *NetFGDDialog = 0;
1200 static puFrame         *NetFGDDialogFrame = 0;
1201 static puText          *NetFGDDialogMessage = 0;
1202 //static puInput         *NetFGDDialogInput = 0;
1203
1204 //static char NewNetId[16];
1205 static char NewNetFGDLabel[] = "Scan for deamon                        "; 
1206 static char NewFGDHost[64] = "olk.mcp.de"; 
1207 static int  NewFGDPortLo = 10000;
1208 static int  NewFGDPortHi = 10001;
1209  
1210 //extern char *fgd_callsign;
1211 extern u_short base_port, end_port;
1212 extern int fgd_ip, verbose, current_port;
1213 extern char *fgd_host;
1214
1215
1216 static puOneShot       *NetFGDDialogOkButton = 0;
1217 static puOneShot       *NetFGDDialogCancelButton = 0;
1218 static puOneShot       *NetFGDDialogScanButton = 0;
1219
1220 static puInput         *NetFGDHostDialogInput = 0;
1221 static puInput         *NetFGDPortLoDialogInput = 0;
1222 static puInput         *NetFGDPortHiDialogInput = 0;
1223
1224 void NetFGDDialog_Cancel(puObject *)
1225 {
1226     FG_POP_PUI_DIALOG( NetFGDDialog );
1227 }
1228
1229 void NetFGDDialog_OK (puObject *)
1230 {
1231     char *NetFGD;    
1232
1233     FGTime *t = FGTime::cur_time_params;
1234     int PauseMode = t->getPause();
1235     if(!PauseMode) t->togglePauseMode();
1236     NetFGDHostDialogInput->getValue( &NetFGD );
1237     strcpy( fgd_host, NetFGD);
1238     NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1239     NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1240     NetFGDDialog_Cancel( NULL );
1241     if( PauseMode != t->getPause() )
1242         t->togglePauseMode();
1243 }
1244
1245 void NetFGDDialog_SCAN (puObject *)
1246 {
1247     char *NetFGD;
1248     int fgd_port;
1249     
1250     FGTime *t = FGTime::cur_time_params;
1251     int PauseMode = t->getPause();
1252     if(!PauseMode) t->togglePauseMode();
1253 //    printf("Vor getvalue %s\n");
1254     NetFGDHostDialogInput->getValue( &NetFGD );
1255 //    printf("Vor strcpy %s\n", (char *) NetFGD);
1256     strcpy( fgd_host, NetFGD);
1257     NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1258     NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1259     printf("FGD: %s  Port-Start: %d Port-End: %d\n", fgd_host, 
1260                  base_port, end_port);
1261     net_resolv_fgd(fgd_host);
1262     printf("Resolve : %d\n", net_r);
1263     if( PauseMode != t->getPause() )  t->togglePauseMode();
1264     if ( net_r == 0 ) {
1265       fgd_port = 10000;
1266       strcpy( fgd_name, "");
1267       for( current_port = base_port; ( current_port <= end_port); current_port++) {
1268           fgd_send_com("0" , FGFS_host);
1269           sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port); 
1270           printf("FGD: searching %s\n", fgd_name);
1271           if ( strcmp( fgd_name, "") != 0 ) {
1272              sprintf( NewNetFGDLabel , "Found %s at Port: %d", 
1273                                               fgd_name, current_port);
1274              fgd_port = current_port;
1275              current_port = end_port+1;
1276           }
1277       }
1278       current_port = end_port = base_port = fgd_port;
1279     }
1280     NetFGDDialog_Cancel( NULL );
1281 }
1282
1283
1284 void net_fgd_scan(puObject *cb)
1285 {
1286     NewFGDPortLo = base_port;
1287     NewFGDPortHi = end_port;
1288     strcpy( NewFGDHost, fgd_host);
1289     NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1290     NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1291     NetFGDHostDialogInput->setValue( NewFGDHost );
1292
1293     FG_PUSH_PUI_DIALOG( NetFGDDialog );
1294 }
1295
1296
1297 static void NewNetFGDInit(void)
1298 {
1299 //    sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1300 //    sprintf( NewNetId, "%s", fgd_callsign );
1301     int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1302                                       NewNetFGDLabel ) / 2;
1303
1304     NetFGDDialog = new puDialogBox (310, 30);
1305     {
1306         NetFGDDialogFrame   = new puFrame           (0,0,320, 170);
1307         NetFGDDialogMessage = new puText            (len, 140);
1308         NetFGDDialogMessage ->    setLabel          (NewNetFGDLabel);
1309
1310         NetFGDPortLoDialogInput   = new puInput           (50, 70, 127, 100);
1311         NetFGDPortLoDialogInput   ->    setValue          (NewFGDPortLo);
1312         NetFGDPortLoDialogInput   ->    acceptInput();
1313
1314         NetFGDPortHiDialogInput   = new puInput           (199, 70, 275, 100);
1315         NetFGDPortHiDialogInput   ->    setValue          (NewFGDPortHi);
1316         NetFGDPortHiDialogInput   ->    acceptInput();
1317
1318         NetFGDHostDialogInput   = new puInput           (50, 100, 275, 130);
1319         NetFGDHostDialogInput   ->    setValue          (NewFGDHost);
1320         NetFGDHostDialogInput   ->    acceptInput();
1321
1322         NetFGDDialogScanButton     =  new puOneShot   (130, 10, 200, 50);
1323         NetFGDDialogScanButton     ->     setLegend   ("Scan");
1324         NetFGDDialogScanButton     ->     setCallback (NetFGDDialog_SCAN);
1325         NetFGDDialogScanButton     ->     makeReturnDefault(FALSE);
1326
1327         NetFGDDialogOkButton     =  new puOneShot   (50, 10, 120, 50);
1328         NetFGDDialogOkButton     ->     setLegend   (gui_msg_OK);
1329         NetFGDDialogOkButton     ->     setCallback (NetFGDDialog_OK);
1330         NetFGDDialogOkButton     ->     makeReturnDefault(TRUE);
1331
1332         NetFGDDialogCancelButton =  new puOneShot   (210, 10, 280, 50);
1333         NetFGDDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
1334         NetFGDDialogCancelButton ->     setCallback (NetFGDDialog_Cancel);
1335
1336     }
1337     FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1338 }
1339
1340 /*
1341 static void net_display_toggle( puObject *cb)
1342 {
1343         net_hud_display = (net_hud_display) ? 0 : 1;
1344         printf("Toggle net_hud_display : %d\n", net_hud_display);
1345 }
1346
1347 */
1348
1349 #endif
1350
1351 /***************  End Networking  **************/
1352
1353
1354
1355 /* -----------------------------------------------------------------------
1356 The menu stuff 
1357 ---------------------------------------------------------------------*/
1358 char *fileSubmenu               [] = {
1359     "Exit", /* "Close", "---------", */
1360 #if defined( WIN32 ) && !defined( __CYGWIN__)
1361     "Print",
1362 #endif
1363     "Snap Shot",
1364     /* "---------", "Save", */ 
1365     "Reset", NULL
1366 };
1367 puCallback fileSubmenuCb        [] = {
1368     MayBeGoodBye, /* hideMenuCb, NULL, */
1369 #if defined( WIN32 ) && !defined( __CYGWIN__)
1370     printScreen, 
1371 #endif
1372     /* NULL, notCb, */
1373     dumpSnapShot,
1374     reInit, NULL
1375 };
1376
1377 /*
1378 char *editSubmenu               [] = {
1379     "Edit text", NULL
1380 };
1381 puCallback editSubmenuCb        [] = {
1382     notCb, NULL
1383 };
1384 */
1385
1386 char *viewSubmenu               [] = {
1387     /* "Cockpit View > ", "View >","------------", */ "Toggle Panel...", NULL
1388 };
1389 puCallback viewSubmenuCb        [] = {
1390     /* notCb, notCb, NULL, guiTogglePanel, */ NULL
1391 };
1392
1393 char *aircraftSubmenu           [] = {
1394     "Autopilot", "Heading", "Altitude", "Navigation", "Airport", 
1395     /* "Communication", */ NULL
1396 };
1397 puCallback aircraftSubmenuCb    [] = {
1398     fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport, 
1399     /* notCb, */ NULL
1400 };
1401
1402 char *environmentSubmenu        [] = {
1403     "Airport", /* "Terrain", "Weather", */ NULL
1404 };
1405 puCallback environmentSubmenuCb [] = {
1406     NewAirport, /* notCb, notCb, */ NULL
1407 };
1408
1409 /*
1410 char *optionsSubmenu            [] = {
1411     "Preferences", "Realism & Reliablity...", NULL
1412 };
1413 puCallback optionsSubmenuCb     [] = {
1414     notCb, notCb, NULL
1415 };
1416 */
1417
1418 #ifdef FG_NETWORK_OLK
1419 char *networkSubmenu            [] = {
1420     "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1421     "Register to FGD",
1422     "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1423     "Toggle Display", NULL
1424 };
1425 puCallback networkSubmenuCb     [] = {
1426     /* notCb, notCb, notCb, notCb, */ 
1427     net_unregister, 
1428     net_register, 
1429     net_fgd_scan, NewCallSign, 
1430     net_display_toggle, NULL
1431 };
1432 #endif
1433
1434 char *helpSubmenu               [] = {
1435     /* "About...", */ "Help", NULL
1436 };
1437 puCallback helpSubmenuCb        [] = {
1438     /* notCb, */ helpCb, NULL
1439 };
1440
1441
1442 /* -------------------------------------------------------------------------
1443 init the gui
1444 _____________________________________________________________________*/
1445
1446
1447 void guiInit()
1448 {
1449     char *mesa_win_state;
1450
1451     // Initialize PUI
1452     puInit();
1453     puSetDefaultStyle         ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1454     puSetDefaultColourScheme  (0.8, 0.8, 0.8, 0.4);
1455
1456     // Initialize our GLOBAL GUI STRINGS
1457     gui_msg_OK     = msg_OK;     // "OK"
1458     gui_msg_NO     = msg_NO;     // "NO"
1459     gui_msg_YES    = msg_YES;    // "YES"
1460     gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1461     gui_msg_RESET  = msg_RESET;  // "RESET"
1462
1463     // Next check home directory
1464     FGPath fntpath;
1465     char* envp = ::getenv( "FG_FONTS" );
1466     if ( envp != NULL ) {
1467         fntpath.set( envp );
1468     } else {
1469         fntpath.set( current_options.get_fg_root() );
1470         fntpath.append( "Fonts" );
1471     }
1472
1473     // Install our fast fonts
1474     fntpath.append( "typewriter.txf" );
1475     guiFntHandle = new fntTexFont ;
1476     guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1477     puFont GuiFont ( guiFntHandle, 15 ) ;
1478     puSetDefaultFonts( GuiFont, GuiFont ) ;
1479     guiFnt = puGetDefaultLabelFont();
1480   
1481     if ( current_options.get_mouse_pointer() == 0 ) {
1482         // no preference specified for mouse pointer, attempt to autodetect...
1483         // Determine if we need to render the cursor, or if the windowing
1484         // system will do it.  First test if we are rendering with glide.
1485         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1486             // Test for the MESA_GLX_FX env variable
1487             if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1488                 // test if we are fullscreen mesa/glide
1489                 if ( (mesa_win_state[0] == 'f') ||
1490                      (mesa_win_state[0] == 'F') ) {
1491                     puShowCursor ();
1492                 }
1493             }
1494         }
1495         mouse_active = ~mouse_active;
1496     } else if ( current_options.get_mouse_pointer() == 1 ) {
1497         // don't show pointer
1498     } else if ( current_options.get_mouse_pointer() == 2 ) {
1499         // force showing pointer
1500         puShowCursor();
1501         mouse_active = ~mouse_active;
1502     }
1503         
1504     // MOUSE_VIEW mode stuff
1505     trackball(_quat0, 0.0, 0.0, 0.0, 0.0);  
1506     Quat0();
1507     build_rotmatrix(quat_mat, curquat);
1508
1509     // Set up our Dialog Boxes
1510     ConfirmExitDialogInit();
1511     NewAirportInit();
1512 #ifdef FG_NETWORK_OLK
1513     NewNetIdInit();
1514     NewNetFGDInit();
1515 #endif
1516     mkDialogInit();
1517     
1518     // Make the menu bar
1519     mainMenuBar = new puMenuBar ();
1520     mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1521     // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1522     mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1523     mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1524     mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1525     // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1526 #ifdef FG_NETWORK_OLK
1527     mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1528 #endif
1529     mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1530     mainMenuBar-> close ();
1531     // Set up menu bar toggle
1532     menu_on = ~0;
1533 }
1534
1535
1536
1537 /*
1538  * Trackball code:
1539  *
1540  * Implementation of a virtual trackball.
1541  * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1542  *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1543  *
1544  * Vector manip code:
1545  *
1546  * Original code from:
1547  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1548  *
1549  * Much mucking with by:
1550  * Gavin Bell
1551  */
1552 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1553 #pragma warning (disable:4244)          /* disable bogus conversion warnings */
1554 #endif
1555 #include <math.h>
1556 #include <stdio.h>
1557 //#include "trackball.h"
1558
1559 /*
1560  * This size should really be based on the distance from the center of
1561  * rotation to the point on the object underneath the mouse.  That
1562  * point would then track the mouse as closely as possible.  This is a
1563  * simple example, though, so that is left as an Exercise for the
1564  * Programmer.
1565  */
1566 #define TRACKBALLSIZE  (0.8f)
1567 #define SQRT(x) sqrt(x)
1568
1569 /*
1570  * Local function prototypes (not defined in trackball.h)
1571  */
1572 static float tb_project_to_sphere(float, float, float);
1573 static void normalize_quat(float [4]);
1574
1575 static void
1576 vzero(float *v)
1577 {
1578     v[0] = 0.0;
1579     v[1] = 0.0;
1580     v[2] = 0.0;
1581 }
1582
1583 static void
1584 vset(float *v, float x, float y, float z)
1585 {
1586     v[0] = x;
1587     v[1] = y;
1588     v[2] = z;
1589 }
1590
1591 static void
1592 vsub(const float *src1, const float *src2, float *dst)
1593 {
1594     dst[0] = src1[0] - src2[0];
1595     dst[1] = src1[1] - src2[1];
1596     dst[2] = src1[2] - src2[2];
1597 }
1598
1599 static void
1600 vcopy(const float *v1, float *v2)
1601 {
1602     register int i;
1603     for (i = 0 ; i < 3 ; i++)
1604         v2[i] = v1[i];
1605 }
1606
1607 static void
1608 vcross(const float *v1, const float *v2, float *cross)
1609 {
1610     float temp[3];
1611
1612     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1613     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1614     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1615     vcopy(temp, cross);
1616 }
1617
1618 static float
1619 vlength(const float *v)
1620 {
1621     float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1622     return SQRT(tmp);
1623 }
1624
1625 static void
1626 vscale(float *v, float div)
1627 {
1628     v[0] *= div;
1629     v[1] *= div;
1630     v[2] *= div;
1631 }
1632
1633 static void
1634 vnormal(float *v)
1635 {
1636     vscale(v,1.0/vlength(v));
1637 }
1638
1639 static float
1640 vdot(const float *v1, const float *v2)
1641 {
1642     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1643 }
1644
1645 static void
1646 vadd(const float *src1, const float *src2, float *dst)
1647 {
1648     dst[0] = src1[0] + src2[0];
1649     dst[1] = src1[1] + src2[1];
1650     dst[2] = src1[2] + src2[2];
1651 }
1652
1653 /*
1654  *  Given an axis and angle, compute quaternion.
1655  */
1656 void
1657 axis_to_quat(float a[3], float phi, float q[4])
1658 {
1659     double sinphi2, cosphi2;
1660     double phi2 = phi/2.0;
1661     sinphi2 = sin(phi2);
1662     cosphi2 = cos(phi2);
1663     vnormal(a);
1664     vcopy(a,q);
1665     vscale(q,sinphi2);
1666     q[3] = cosphi2;
1667 }
1668
1669 /*
1670  * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1671  * if we are away from the center of the sphere.
1672  */
1673 static float
1674 tb_project_to_sphere(float r, float x, float y)
1675 {
1676     float d, t, z, tmp;
1677
1678     tmp = x*x + y*y;
1679     d = SQRT(tmp);
1680     if (d < r * 0.70710678118654752440) {    /* Inside sphere */
1681         tmp = r*r - d*d;
1682         z = SQRT(tmp);
1683     } else {           /* On hyperbola */
1684         t = r / 1.41421356237309504880;
1685         z = t*t / d;
1686     }
1687     return z;
1688 }
1689
1690 /*
1691  * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0
1692  * If they don't add up to 1.0, dividing by their magnitued will
1693  * renormalize them.
1694  *
1695  * Note: See the following for more information on quaternions:
1696  *
1697  * - Shoemake, K., Animating rotation with quaternion curves, Computer
1698  *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1699  * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1700  *   graphics, The Visual Computer 5, 2-13, 1989.
1701  */
1702 static void
1703 normalize_quat(float q[4])
1704 {
1705     int i;
1706     float mag, tmp;
1707
1708     tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1709     mag = 1.0 / SQRT(tmp);
1710     for (i = 0; i < 4; i++)
1711         q[i] *= mag;
1712 }
1713
1714 /*
1715  * Ok, simulate a track-ball.  Project the points onto the virtual
1716  * trackball, then figure out the axis of rotation, which is the cross
1717  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1718  * Note:  This is a deformed trackball-- is a trackball in the center,
1719  * but is deformed into a hyperbolic sheet of rotation away from the
1720  * center.  This particular function was chosen after trying out
1721  * several variations.
1722  *
1723  * It is assumed that the arguments to this routine are in the range
1724  * (-1.0 ... 1.0)
1725  */
1726 void
1727 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1728 {
1729     float a[3]; /* Axis of rotation */
1730     float phi;  /* how much to rotate about axis */
1731     float p1[3], p2[3], d[3];
1732     float t;
1733
1734     if (p1x == p2x && p1y == p2y) {
1735         /* Zero rotation */
1736         vzero(q);
1737         q[3] = 1.0;
1738         return;
1739     }
1740
1741     /*
1742      * First, figure out z-coordinates for projection of P1 and P2 to
1743      * deformed sphere
1744      */
1745     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1746     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1747
1748     /*
1749      *  Now, we want the cross product of P1 and P2
1750      */
1751     vcross(p2,p1,a);
1752
1753     /*
1754      *  Figure out how much to rotate around that axis.
1755      */
1756     vsub(p1,p2,d);
1757     t = vlength(d) / (2.0*TRACKBALLSIZE);
1758
1759     /*
1760      * Avoid problems with out-of-control values...
1761      */
1762     if (t > 1.0) t = 1.0;
1763     if (t < -1.0) t = -1.0;
1764     phi = 2.0 * asin(t);
1765
1766     axis_to_quat(a,phi,q);
1767 }
1768
1769 /*
1770  * Given two rotations, e1 and e2, expressed as quaternion rotations,
1771  * figure out the equivalent single rotation and stuff it into dest.
1772  *
1773  * This routine also normalizes the result every RENORMCOUNT times it is
1774  * called, to keep error from creeping in.
1775  *
1776  * NOTE: This routine is written so that q1 or q2 may be the same
1777  * as dest (or each other).
1778  */
1779
1780 #define RENORMCOUNT 97
1781
1782 void
1783 add_quats(float q1[4], float q2[4], float dest[4])
1784 {
1785     static int count=0;
1786     float t1[4], t2[4], t3[4];
1787     float tf[4];
1788
1789 #if 0
1790 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1791 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1792 #endif
1793
1794     vcopy(q1,t1);
1795     vscale(t1,q2[3]);
1796
1797     vcopy(q2,t2);
1798     vscale(t2,q1[3]);
1799
1800     vcross(q2,q1,t3);
1801     vadd(t1,t2,tf);
1802     vadd(t3,tf,tf);
1803     tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1804
1805 #if 0
1806 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1807 #endif
1808
1809     dest[0] = tf[0];
1810     dest[1] = tf[1];
1811     dest[2] = tf[2];
1812     dest[3] = tf[3];
1813
1814     if (++count > RENORMCOUNT) {
1815         count = 0;
1816         normalize_quat(dest);
1817     }
1818 }
1819
1820 /*
1821  * Build a rotation matrix, given a quaternion rotation.
1822  *
1823  */
1824 void
1825 build_rotmatrix(float m[4][4], float q[4])
1826 {
1827 //#define TRANSPOSED_QUAT
1828 #ifndef TRANSPOSED_QUAT
1829     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1830     m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1831     m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1832     m[0][3] = 0.0;
1833
1834     m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1835     m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1836     m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1837     m[1][3] = 0.0;
1838
1839     m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1840     m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1841     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1842     
1843     m[2][3] = 0.0;
1844     m[3][0] = 0.0;
1845     m[3][1] = 0.0;
1846     m[3][2] = 0.0;
1847     m[3][3] = 1.0;
1848 #else //  TRANSPOSED_QUAT
1849     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1850     m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1851     m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1852     m[0][3] = 0.0;
1853
1854     m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1855     m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1856     m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1857     m[1][3] = 0.0;
1858
1859     m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1860     m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1861     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1862     m[2][3] = 0.0;
1863     
1864     m[3][0] = 0.0;
1865     m[3][1] = 0.0;
1866     m[3][2] = 0.0;
1867     m[3][3] = 1.0;
1868 #endif // 0
1869 }
1870