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