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