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