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