]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
Tweaks to go along with changes in SGSocket.
[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/globals.hxx>
70 #include <Main/options.hxx>
71 #include <Main/views.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( 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 #if 0 // this function does nothing anyway
674     bool freeze = globals->get_freeze();
675
676     if ( current_options.get_panel_status() ) {
677         // FGView *v = &current_view;
678         if( !freeze )
679             globals->set_freeze( true );
680
681         // we must have some something here at some point but this
682         // function does nothing now.
683
684         if( !freeze )
685             globals->set_freeze( false );
686     }
687 #endif
688 }
689
690 // Toggle the Menu and Mouse display state
691 void guiToggleMenu(void)
692 {
693     if( menu_on ) {
694         // printf("Hiding Menu\n");
695         mainMenuBar->hide  ();
696 #if defined(WIN32_CURSOR_TWEAKS)
697         if( mouse_mode == MOUSE_POINTER )
698             TurnCursorOff();
699 #endif // #ifdef WIN32_CURSOR_TWEAKS
700     } else {
701         // printf("Showing Menu\n");
702         mainMenuBar->reveal();
703 #ifdef WIN32
704         TurnCursorOn();
705 #endif // #ifdef WIN32
706     }
707     menu_on = ~menu_on;
708 }
709     
710 /* -----------------------------------------------------------------------
711 the Gui callback functions 
712 ____________________________________________________________________*/
713
714 static void saveFlight(puObject *cv)
715 {
716     BusyCursor(0);
717     ofstream output("fgfs.sav");
718     if (output.good() && fgSaveFlight(output)) {
719       output.close();
720       mkDialog("Saved flight to ./fgfs.sav");
721       FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
722     } else {
723       mkDialog("Cannot save flight to ./fgfs.sav");
724       FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
725     }
726     BusyCursor(1);
727 }
728
729 static void loadFlight(puObject *cb)
730 {
731     BusyCursor(0);
732     ifstream input("fgfs.sav");
733     if (input.good() && fgLoadFlight(input)) {
734       input.close();
735       mkDialog("Loaded flight from fgfs.sav");
736       FG_LOG(FG_INPUT, FG_INFO, "Restored flight from ./fgfs.sav");
737     } else {
738       mkDialog("Failed to load flight from fgfs.sav");
739       FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from ./fgfs.sav");
740     }
741     BusyCursor(1);
742 }
743
744 void reInit(puObject *cb)
745 {
746     BusyCursor(0);
747     Quat0();
748     build_rotmatrix(quat_mat, curquat);
749     fgReInitSubsystems();
750     BusyCursor(1);
751 }
752
753 static void toggleClouds(puObject *cb)
754 {
755     FGBFI::setClouds( !FGBFI::getClouds() );
756 }
757         
758 // This is the accessor function
759 void guiTogglePanel(puObject *cb)
760 {
761     current_options.toggle_panel();
762 }
763     
764 //void MenuHideMenuCb(puObject *cb)
765 void hideMenuCb (puObject *cb)
766 {
767     guiToggleMenu();
768 }
769
770 void goodBye(puObject *)
771 {
772     // FG_LOG( FG_INPUT, FG_ALERT,
773     //      "Program exiting normally at user request." );
774     cout << "Program exiting normally at user request." << endl;
775
776 #ifdef FG_NETWORK_OLK    
777     if ( current_options.get_network_olk() ) {
778         if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
779     }
780 #endif
781
782     //  if(gps_bug)
783     //      fclose(gps_bug);
784
785     exit(-1);
786 }
787
788
789 void goAwayCb (puObject *me)
790 {
791     FG_POP_PUI_DIALOG( dialogBox );
792 }
793
794 void mkDialogInit (void)
795 {
796     //  printf("mkDialogInit\n");
797     int x = (current_options.get_xsize()/2 - 400/2);
798     int y = (current_options.get_ysize()/2 - 100/2);
799     dialogBox = new puDialogBox (x, y); // 150, 50
800     {
801         dialogFrame = new puFrame (0,0,400,100);
802         dialogBoxMessage  =  new puText         (10, 70);
803         dialogBoxMessage  -> setLabel           ("");
804         dialogBoxOkButton =  new puOneShot      (180, 10, 240, 50);
805         dialogBoxOkButton -> setLegend          (gui_msg_OK);
806         dialogBoxOkButton -> makeReturnDefault  (TRUE );
807         dialogBoxOkButton -> setCallback        (goAwayCb);
808     }
809     FG_FINALIZE_PUI_DIALOG( dialogBox );
810 }
811
812 void MayBeGoodBye(puObject *)
813 {
814     ConfirmExitDialog(); 
815 }
816
817 void goAwayYesNoCb(puObject *me)
818 {
819     FG_POP_PUI_DIALOG( YNdialogBox);
820 }
821
822 void ConfirmExitDialogInit(void)
823 {
824     char msg[] = "Really Quit";
825     char *s;
826
827     //  printf("ConfirmExitDialogInit\n");
828     int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
829
830     int x = (current_options.get_xsize()/2 - 400/2);
831     int y = (current_options.get_ysize()/2 - 100/2);
832         
833     YNdialogBox = new puDialogBox (x, y); // 150, 50
834     //  YNdialogBox = new puDialogBox (150, 50);
835     {
836         YNdialogFrame = new puFrame (0,0,400, 100);
837         
838         YNdialogBoxMessage  =  new puText         (len, 70);
839         YNdialogBoxMessage  -> setDefaultValue    (msg);
840         YNdialogBoxMessage  -> getDefaultValue    (&s);
841         YNdialogBoxMessage  -> setLabel           (s);
842         
843         YNdialogBoxOkButton =  new puOneShot      (100, 10, 160, 50);
844         YNdialogBoxOkButton -> setLegend          (gui_msg_OK);
845         YNdialogBoxOkButton -> makeReturnDefault  (TRUE );
846         YNdialogBoxOkButton -> setCallback        (goodBye);
847         
848         YNdialogBoxNoButton =  new puOneShot      (240, 10, 300, 50);
849         YNdialogBoxNoButton -> setLegend          (gui_msg_NO);
850         YNdialogBoxNoButton -> setCallback        (goAwayYesNoCb);
851     }
852     FG_FINALIZE_PUI_DIALOG( YNdialogBox );
853 }
854
855 void notCb (puObject *)
856 {
857     mkDialog ("This function isn't implemented yet");
858 }
859
860 void helpCb (puObject *)
861 {
862     string command;
863         
864 #if defined(FX) && !defined(WIN32)
865 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
866     if ( global_fullscreen ) {
867         global_fullscreen = false;
868         XMesaSetFXmode( XMESA_FX_WINDOW );
869     }
870 #  endif
871 #endif
872         
873 #if !defined(WIN32)
874     string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
875         
876     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
877         command = "netscape -remote \"openURL(" + url + ")\" &";
878     } else {
879         command = "netscape " + url + " &";
880     }
881 #else
882     command = "webrun.bat";
883 #endif
884         
885     system( command.c_str() );
886     //string text = "Help started in netscape window.";
887
888     //mkDialog (text.c_str());
889     mkDialog ("Help started in netscape window.");
890 }
891
892 #if defined( WIN32 ) && !defined( __CYGWIN__)
893
894 static void rotateView( double roll, double pitch, double yaw )
895 {
896         // rotate view
897 }
898
899 static GlBitmap *b1 = NULL;
900 extern FGInterface cur_view_fdm;
901 GLubyte *hiResScreenCapture( int multiplier )
902 {
903         float oldfov = current_options.get_fov();
904         float fov = oldfov / multiplier;
905         FGView *v = &current_view;
906         current_options.set_fov(fov);
907         v->force_update_fov_math();
908     fgInitVisuals();
909     int cur_width = current_view.get_winWidth( );
910     int cur_height = current_view.get_winHeight( );
911         if (b1) delete( b1 );
912         // New empty (mostly) bitmap
913         b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
914         int x,y;
915         for ( y = 0; y < multiplier; y++ )
916         {
917                 for ( x = 0; x < multiplier; x++ )
918                 {
919                         fgReshape( cur_width, cur_height );
920                         // pan to tile
921                         rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
922                         fgRenderFrame();
923                         // restore view
924                         GlBitmap b2;
925                         b1->copyBitmap( &b2, cur_width*x, cur_height*y );
926                 }
927         }
928         current_view.UpdateViewParams(cur_view_fdm);
929         current_options.set_fov(oldfov);
930         v->force_update_fov_math();
931         return b1->getBitmap();
932 }
933 #endif
934
935
936 #if defined( WIN32 ) && !defined( __CYGWIN__)
937 // win32 print screen function
938 void printScreen ( puObject *obj ) {
939     bool show_pu_cursor = false;
940     TurnCursorOff();
941     if ( !puCursorIsHidden() ) {
942         show_pu_cursor = true;
943         puHideCursor();
944     }
945     BusyCursor( 0 );
946     mainMenuBar->hide();
947
948     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
949     int cur_width = current_view.get_winWidth( );
950     int cur_height = current_view.get_winHeight( );
951     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
952         p.End( hiResScreenCapture(3) );
953
954     if( menu_on ) {
955         mainMenuBar->reveal();
956     }
957     BusyCursor(1);
958     if ( show_pu_cursor ) {
959         puShowCursor();
960     }
961     TurnCursorOn();
962 }
963 #endif // #ifdef WIN32
964
965
966 void dumpSnapShot ( puObject *obj ) {
967     fgDumpSnapShot();
968 }
969
970
971 // do a screen snap shot
972 void fgDumpSnapShot () {
973     bool show_pu_cursor = false;
974
975     mainMenuBar->hide();
976     TurnCursorOff();
977     if ( !puCursorIsHidden() ) {
978         show_pu_cursor = true;
979         puHideCursor();
980     }
981
982     fgInitVisuals();
983     fgReshape( current_options.get_xsize(), current_options.get_ysize() );
984
985     // we need two render frames here to clear the menu and cursor
986     // ... not sure why but doing an extra fgFenderFrame() shoulnd't
987     // hurt anything
988     fgRenderFrame();
989     fgRenderFrame();
990
991     my_glDumpWindow( "fgfs-screen.ppm", 
992                      current_options.get_xsize(), 
993                      current_options.get_ysize() );
994     
995     mkDialog ("Snap shot saved to fgfs-screen.ppm");
996
997     if ( show_pu_cursor ) {
998         puShowCursor();
999     }
1000
1001     TurnCursorOn();
1002     if( menu_on ) {
1003         mainMenuBar->reveal();
1004     }
1005
1006 }
1007
1008
1009 /// The beginnings of teleportation :-)
1010 //  Needs cleaning up but works
1011 //  These statics should disapear when this is a class
1012 static puDialogBox     *AptDialog = 0;
1013 static puFrame         *AptDialogFrame = 0;
1014 static puText          *AptDialogMessage = 0;
1015 static puInput         *AptDialogInput = 0;
1016
1017 static char NewAirportId[16];
1018 static char NewAirportLabel[] = "Enter New Airport ID"; 
1019
1020 static puOneShot       *AptDialogOkButton = 0;
1021 static puOneShot       *AptDialogCancelButton = 0;
1022 static puOneShot       *AptDialogResetButton = 0;
1023
1024 void AptDialog_Cancel(puObject *)
1025 {
1026     FG_POP_PUI_DIALOG( AptDialog );
1027 }
1028
1029 void AptDialog_OK (puObject *)
1030 {
1031     FGPath path( current_options.get_fg_root() );
1032     path.append( "Airports" );
1033     path.append( "simple.mk4" );
1034     FGAirports airports( path.c_str() );
1035
1036     FGAirport a;
1037     
1038     int freeze = globals->get_freeze();
1039     if(!freeze)
1040         globals->set_freeze( true );
1041
1042     char *s;
1043     AptDialogInput->getValue(&s);
1044     string AptId(s);
1045
1046         cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1047     
1048     AptDialog_Cancel( NULL );
1049     
1050     if ( AptId.length() ) {
1051         // set initial position from airport id
1052         FG_LOG( FG_GENERAL, FG_INFO,
1053                 "Attempting to set starting position from airport code "
1054                 << AptId );
1055
1056         if ( airports.search( AptId, &a ) )
1057         {
1058             current_options.set_airport_id( AptId.c_str() );
1059             BusyCursor(0);
1060             fgReInitSubsystems();
1061             BusyCursor(1);
1062         } else {
1063             AptId  += " not in database.";
1064             mkDialog(AptId.c_str());
1065         }
1066     }
1067     if(!freeze)
1068         globals->set_freeze( false );
1069 }
1070
1071 void AptDialog_Reset(puObject *)
1072 {
1073     //  strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1074     sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1075     AptDialogInput->setValue ( NewAirportId );
1076     AptDialogInput->setCursor( 0 ) ;
1077 }
1078
1079 void NewAirport(puObject *cb)
1080 {
1081     //  strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1082     sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1083 //      cout << "NewAirport " << NewAirportId << endl;
1084     AptDialogInput->setValue( NewAirportId );
1085
1086     FG_PUSH_PUI_DIALOG( AptDialog );
1087 }
1088
1089 static void NewAirportInit(void)
1090 {
1091     sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1092     int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1093                                       NewAirportLabel ) / 2;
1094
1095     AptDialog = new puDialogBox (150, 50);
1096     {
1097         AptDialogFrame   = new puFrame           (0,0,350, 150);
1098         AptDialogMessage = new puText            (len, 110);
1099         AptDialogMessage ->    setLabel          (NewAirportLabel);
1100
1101         AptDialogInput   = new puInput           (50, 70, 300, 100);
1102         AptDialogInput   ->    setValue          (NewAirportId);
1103         AptDialogInput   ->    acceptInput();
1104
1105         AptDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
1106         AptDialogOkButton     ->     setLegend   (gui_msg_OK);
1107         AptDialogOkButton     ->     setCallback (AptDialog_OK);
1108         AptDialogOkButton     ->     makeReturnDefault(TRUE);
1109
1110         AptDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
1111         AptDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
1112         AptDialogCancelButton ->     setCallback (AptDialog_Cancel);
1113
1114         AptDialogResetButton  =  new puOneShot   (240, 10, 300, 50);
1115         AptDialogResetButton  ->     setLegend   (gui_msg_RESET);
1116         AptDialogResetButton  ->     setCallback (AptDialog_Reset);
1117     }
1118         cout << "NewAirportInit " << NewAirportId << endl;
1119     FG_FINALIZE_PUI_DIALOG( AptDialog );
1120 }
1121
1122 #ifdef FG_NETWORK_OLK
1123
1124 /// The beginnings of networking :-)
1125 //  Needs cleaning up but works
1126 //  These statics should disapear when this is a class
1127 static puDialogBox     *NetIdDialog = 0;
1128 static puFrame         *NetIdDialogFrame = 0;
1129 static puText          *NetIdDialogMessage = 0;
1130 static puInput         *NetIdDialogInput = 0;
1131
1132 static char NewNetId[16];
1133 static char NewNetIdLabel[] = "Enter New Callsign"; 
1134 extern char *fgd_callsign;
1135
1136 static puOneShot       *NetIdDialogOkButton = 0;
1137 static puOneShot       *NetIdDialogCancelButton = 0;
1138
1139 void NetIdDialog_Cancel(puObject *)
1140 {
1141     FG_POP_PUI_DIALOG( NetIdDialog );
1142 }
1143
1144 void NetIdDialog_OK (puObject *)
1145 {
1146     string NetId;
1147     
1148     bool freeze = globals->get_freeze();
1149     if(!freeze)
1150         globals->set_freeze( true );
1151 /*  
1152    The following needs some cleanup because 
1153    "string options.NetId" and "char *net_callsign" 
1154 */
1155     NetIdDialogInput->getValue(&net_callsign);
1156     NetId = net_callsign;
1157     
1158     NetIdDialog_Cancel( NULL );
1159     current_options.set_net_id( NetId.c_str() );
1160     strcpy( fgd_callsign, net_callsign);
1161 //    strcpy( fgd_callsign, current_options.get_net_id().c_str());
1162 /* Entering a callsign indicates : user wants Net HUD Info */
1163     net_hud_display = 1;
1164
1165     if(!freeze)
1166         globals->set_freeze( false );
1167 }
1168
1169 void NewCallSign(puObject *cb)
1170 {
1171     sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1172 //    sprintf( NewNetId, "%s", fgd_callsign );
1173     NetIdDialogInput->setValue( NewNetId );
1174
1175     FG_PUSH_PUI_DIALOG( NetIdDialog );
1176 }
1177
1178 static void NewNetIdInit(void)
1179 {
1180     sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1181 //    sprintf( NewNetId, "%s", fgd_callsign );
1182     int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1183                                       NewNetIdLabel ) / 2;
1184
1185     NetIdDialog = new puDialogBox (150, 50);
1186     {
1187         NetIdDialogFrame   = new puFrame           (0,0,350, 150);
1188         NetIdDialogMessage = new puText            (len, 110);
1189         NetIdDialogMessage ->    setLabel          (NewNetIdLabel);
1190
1191         NetIdDialogInput   = new puInput           (50, 70, 300, 100);
1192         NetIdDialogInput   ->    setValue          (NewNetId);
1193         NetIdDialogInput   ->    acceptInput();
1194
1195         NetIdDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
1196         NetIdDialogOkButton     ->     setLegend   (gui_msg_OK);
1197         NetIdDialogOkButton     ->     setCallback (NetIdDialog_OK);
1198         NetIdDialogOkButton     ->     makeReturnDefault(TRUE);
1199
1200         NetIdDialogCancelButton =  new puOneShot   (240, 10, 300, 50);
1201         NetIdDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
1202         NetIdDialogCancelButton ->     setCallback (NetIdDialog_Cancel);
1203
1204     }
1205     FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1206 }
1207
1208 static void net_display_toggle( puObject *cb)
1209 {
1210         net_hud_display = (net_hud_display) ? 0 : 1;
1211         printf("Toggle net_hud_display : %d\n", net_hud_display);
1212 }
1213
1214 static void net_register( puObject *cb)
1215 {
1216         fgd_send_com( "1", FGFS_host );
1217         net_is_registered = 0;
1218         printf("Registering to deamon\n");
1219 }
1220
1221 static void net_unregister( puObject *cb)
1222 {
1223         fgd_send_com( "8", FGFS_host );
1224         net_is_registered = -1;
1225         printf("Unregistering from deamon\n");
1226 }
1227
1228
1229 /*************** Deamon communication **********/
1230
1231 //  These statics should disapear when this is a class
1232 static puDialogBox     *NetFGDDialog = 0;
1233 static puFrame         *NetFGDDialogFrame = 0;
1234 static puText          *NetFGDDialogMessage = 0;
1235 //static puInput         *NetFGDDialogInput = 0;
1236
1237 //static char NewNetId[16];
1238 static char NewNetFGDLabel[] = "Scan for deamon                        "; 
1239 static char NewFGDHost[64] = "olk.mcp.de"; 
1240 static int  NewFGDPortLo = 10000;
1241 static int  NewFGDPortHi = 10001;
1242  
1243 //extern char *fgd_callsign;
1244 extern u_short base_port, end_port;
1245 extern int fgd_ip, verbose, current_port;
1246 extern char *fgd_host;
1247
1248
1249 static puOneShot       *NetFGDDialogOkButton = 0;
1250 static puOneShot       *NetFGDDialogCancelButton = 0;
1251 static puOneShot       *NetFGDDialogScanButton = 0;
1252
1253 static puInput         *NetFGDHostDialogInput = 0;
1254 static puInput         *NetFGDPortLoDialogInput = 0;
1255 static puInput         *NetFGDPortHiDialogInput = 0;
1256
1257 void NetFGDDialog_Cancel(puObject *)
1258 {
1259     FG_POP_PUI_DIALOG( NetFGDDialog );
1260 }
1261
1262 void NetFGDDialog_OK (puObject *)
1263 {
1264     char *NetFGD;    
1265
1266     bool freeze = globals->get_freeze();
1267     if(!freeze)
1268         globals->set_freeze( true );
1269     NetFGDHostDialogInput->getValue( &NetFGD );
1270     strcpy( fgd_host, NetFGD);
1271     NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1272     NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1273     NetFGDDialog_Cancel( NULL );
1274     if(!freeze)
1275         globals->set_freeze( false );
1276 }
1277
1278 void NetFGDDialog_SCAN (puObject *)
1279 {
1280     char *NetFGD;
1281     int fgd_port;
1282     
1283     bool freeze = globals->get_freeze();
1284     if(!freeze)
1285         globals->set_freeze( true );
1286 //    printf("Vor getvalue %s\n");
1287     NetFGDHostDialogInput->getValue( &NetFGD );
1288 //    printf("Vor strcpy %s\n", (char *) NetFGD);
1289     strcpy( fgd_host, NetFGD);
1290     NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1291     NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1292     printf("FGD: %s  Port-Start: %d Port-End: %d\n", fgd_host, 
1293                  base_port, end_port);
1294     net_resolv_fgd(fgd_host);
1295     printf("Resolve : %d\n", net_r);
1296     if(!freeze)
1297         globals->set_freeze( false );
1298     if ( net_r == 0 ) {
1299       fgd_port = 10000;
1300       strcpy( fgd_name, "");
1301       for( current_port = base_port; ( current_port <= end_port); current_port++) {
1302           fgd_send_com("0" , FGFS_host);
1303           sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port); 
1304           printf("FGD: searching %s\n", fgd_name);
1305           if ( strcmp( fgd_name, "") != 0 ) {
1306              sprintf( NewNetFGDLabel , "Found %s at Port: %d", 
1307                                               fgd_name, current_port);
1308              fgd_port = current_port;
1309              current_port = end_port+1;
1310           }
1311       }
1312       current_port = end_port = base_port = fgd_port;
1313     }
1314     NetFGDDialog_Cancel( NULL );
1315 }
1316
1317
1318 void net_fgd_scan(puObject *cb)
1319 {
1320     NewFGDPortLo = base_port;
1321     NewFGDPortHi = end_port;
1322     strcpy( NewFGDHost, fgd_host);
1323     NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1324     NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1325     NetFGDHostDialogInput->setValue( NewFGDHost );
1326
1327     FG_PUSH_PUI_DIALOG( NetFGDDialog );
1328 }
1329
1330
1331 static void NewNetFGDInit(void)
1332 {
1333 //    sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1334 //    sprintf( NewNetId, "%s", fgd_callsign );
1335     int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1336                                       NewNetFGDLabel ) / 2;
1337
1338     NetFGDDialog = new puDialogBox (310, 30);
1339     {
1340         NetFGDDialogFrame   = new puFrame           (0,0,320, 170);
1341         NetFGDDialogMessage = new puText            (len, 140);
1342         NetFGDDialogMessage ->    setLabel          (NewNetFGDLabel);
1343
1344         NetFGDPortLoDialogInput   = new puInput           (50, 70, 127, 100);
1345         NetFGDPortLoDialogInput   ->    setValue          (NewFGDPortLo);
1346         NetFGDPortLoDialogInput   ->    acceptInput();
1347
1348         NetFGDPortHiDialogInput   = new puInput           (199, 70, 275, 100);
1349         NetFGDPortHiDialogInput   ->    setValue          (NewFGDPortHi);
1350         NetFGDPortHiDialogInput   ->    acceptInput();
1351
1352         NetFGDHostDialogInput   = new puInput           (50, 100, 275, 130);
1353         NetFGDHostDialogInput   ->    setValue          (NewFGDHost);
1354         NetFGDHostDialogInput   ->    acceptInput();
1355
1356         NetFGDDialogScanButton     =  new puOneShot   (130, 10, 200, 50);
1357         NetFGDDialogScanButton     ->     setLegend   ("Scan");
1358         NetFGDDialogScanButton     ->     setCallback (NetFGDDialog_SCAN);
1359         NetFGDDialogScanButton     ->     makeReturnDefault(FALSE);
1360
1361         NetFGDDialogOkButton     =  new puOneShot   (50, 10, 120, 50);
1362         NetFGDDialogOkButton     ->     setLegend   (gui_msg_OK);
1363         NetFGDDialogOkButton     ->     setCallback (NetFGDDialog_OK);
1364         NetFGDDialogOkButton     ->     makeReturnDefault(TRUE);
1365
1366         NetFGDDialogCancelButton =  new puOneShot   (210, 10, 280, 50);
1367         NetFGDDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
1368         NetFGDDialogCancelButton ->     setCallback (NetFGDDialog_Cancel);
1369
1370     }
1371     FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1372 }
1373
1374 /*
1375 static void net_display_toggle( puObject *cb)
1376 {
1377         net_hud_display = (net_hud_display) ? 0 : 1;
1378         printf("Toggle net_hud_display : %d\n", net_hud_display);
1379 }
1380
1381 */
1382
1383 #endif
1384
1385 /***************  End Networking  **************/
1386
1387
1388
1389 /* -----------------------------------------------------------------------
1390 The menu stuff 
1391 ---------------------------------------------------------------------*/
1392 char *fileSubmenu               [] = {
1393     "Exit", /* "Close", "---------", */
1394 #if defined( WIN32 ) && !defined( __CYGWIN__)
1395     "Print",
1396 #endif
1397     "Snap Shot",
1398     "---------", 
1399     "Reset", 
1400     "Load flight",
1401     "Save flight",
1402     NULL
1403 };
1404 puCallback fileSubmenuCb        [] = {
1405     MayBeGoodBye, /* hideMenuCb, NULL, */
1406 #if defined( WIN32 ) && !defined( __CYGWIN__)
1407     printScreen, 
1408 #endif
1409     /* NULL, notCb, */
1410     dumpSnapShot,
1411     NULL,
1412     reInit, 
1413     loadFlight,
1414     saveFlight,
1415     NULL
1416 };
1417
1418 /*
1419 char *editSubmenu               [] = {
1420     "Edit text", NULL
1421 };
1422 puCallback editSubmenuCb        [] = {
1423     notCb, NULL
1424 };
1425 */
1426
1427 extern void fgHUDalphaAdjust( puObject * );
1428 char *viewSubmenu               [] = {
1429     "HUD Alpha",
1430     /* "Cockpit View > ", "View >","------------", */
1431     "Toggle Panel...", NULL
1432 };
1433 puCallback viewSubmenuCb        [] = {
1434     fgHUDalphaAdjust,
1435     /* notCb, notCb, NULL, */
1436     guiTogglePanel, NULL
1437 };
1438
1439 char *aircraftSubmenu           [] = {
1440     "Autopilot", "Heading", "Altitude", "Navigation", "Airport", 
1441     /* "Communication", */ NULL
1442 };
1443 puCallback aircraftSubmenuCb    [] = {
1444     fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport, 
1445     /* notCb, */ NULL
1446 };
1447
1448 char *environmentSubmenu        [] = {
1449     "Toggle Clouds",
1450     "Goto Airport", /* "Terrain", "Weather", */ NULL
1451 };
1452 puCallback environmentSubmenuCb [] = {
1453     toggleClouds,
1454     NewAirport, /* notCb, notCb, */ NULL
1455 };
1456
1457 /*
1458 char *optionsSubmenu            [] = {
1459     "Preferences", "Realism & Reliablity...", NULL
1460 };
1461 puCallback optionsSubmenuCb     [] = {
1462     notCb, notCb, NULL
1463 };
1464 */
1465
1466 #ifdef FG_NETWORK_OLK
1467 char *networkSubmenu            [] = {
1468     "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1469     "Register to FGD",
1470     "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1471     "Toggle Display", NULL
1472 };
1473 puCallback networkSubmenuCb     [] = {
1474     /* notCb, notCb, notCb, notCb, */ 
1475     net_unregister, 
1476     net_register, 
1477     net_fgd_scan, NewCallSign, 
1478     net_display_toggle, NULL
1479 };
1480 #endif
1481
1482 char *helpSubmenu               [] = {
1483     /* "About...", */ "Help", NULL
1484 };
1485 puCallback helpSubmenuCb        [] = {
1486     /* notCb, */ helpCb, NULL
1487 };
1488
1489
1490 /* -------------------------------------------------------------------------
1491 init the gui
1492 _____________________________________________________________________*/
1493
1494
1495 void guiInit()
1496 {
1497     char *mesa_win_state;
1498
1499     // Initialize PUI
1500     puInit();
1501     puSetDefaultStyle         ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1502     puSetDefaultColourScheme  (0.8, 0.8, 0.8, 0.4);
1503
1504     // Initialize our GLOBAL GUI STRINGS
1505     gui_msg_OK     = msg_OK;     // "OK"
1506     gui_msg_NO     = msg_NO;     // "NO"
1507     gui_msg_YES    = msg_YES;    // "YES"
1508     gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1509     gui_msg_RESET  = msg_RESET;  // "RESET"
1510
1511     // Next check home directory
1512     FGPath fntpath;
1513     char* envp = ::getenv( "FG_FONTS" );
1514     if ( envp != NULL ) {
1515         fntpath.set( envp );
1516     } else {
1517         fntpath.set( current_options.get_fg_root() );
1518         fntpath.append( "Fonts" );
1519     }
1520
1521     // Install our fast fonts
1522     fntpath.append( "typewriter.txf" );
1523     guiFntHandle = new fntTexFont ;
1524     guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1525     puFont GuiFont ( guiFntHandle, 15 ) ;
1526     puSetDefaultFonts( GuiFont, GuiFont ) ;
1527     guiFnt = puGetDefaultLabelFont();
1528   
1529     if ( current_options.get_mouse_pointer() == 0 ) {
1530         // no preference specified for mouse pointer, attempt to autodetect...
1531         // Determine if we need to render the cursor, or if the windowing
1532         // system will do it.  First test if we are rendering with glide.
1533         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1534             // Test for the MESA_GLX_FX env variable
1535             if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1536                 // test if we are fullscreen mesa/glide
1537                 if ( (mesa_win_state[0] == 'f') ||
1538                      (mesa_win_state[0] == 'F') ) {
1539                     puShowCursor ();
1540                 }
1541             }
1542         }
1543         mouse_active = ~mouse_active;
1544     } else if ( current_options.get_mouse_pointer() == 1 ) {
1545         // don't show pointer
1546     } else if ( current_options.get_mouse_pointer() == 2 ) {
1547         // force showing pointer
1548         puShowCursor();
1549         mouse_active = ~mouse_active;
1550     }
1551         
1552     // MOUSE_VIEW mode stuff
1553     trackball(_quat0, 0.0, 0.0, 0.0, 0.0);  
1554     Quat0();
1555     build_rotmatrix(quat_mat, curquat);
1556
1557     // Set up our Dialog Boxes
1558     ConfirmExitDialogInit();
1559     NewAirportInit();
1560 #ifdef FG_NETWORK_OLK
1561     NewNetIdInit();
1562     NewNetFGDInit();
1563 #endif
1564     mkDialogInit();
1565     
1566     // Make the menu bar
1567     mainMenuBar = new puMenuBar ();
1568     mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1569     // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1570     mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1571     mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1572     mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1573     // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1574 #ifdef FG_NETWORK_OLK
1575     mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1576 #endif
1577     mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1578     mainMenuBar-> close ();
1579     // Set up menu bar toggle
1580     menu_on = ~0;
1581 }
1582
1583
1584
1585 /*
1586  * Trackball code:
1587  *
1588  * Implementation of a virtual trackball.
1589  * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1590  *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1591  *
1592  * Vector manip code:
1593  *
1594  * Original code from:
1595  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1596  *
1597  * Much mucking with by:
1598  * Gavin Bell
1599  */
1600 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1601 #pragma warning (disable:4244)          /* disable bogus conversion warnings */
1602 #endif
1603 #include <math.h>
1604 #include <stdio.h>
1605 //#include "trackball.h"
1606
1607 /*
1608  * This size should really be based on the distance from the center of
1609  * rotation to the point on the object underneath the mouse.  That
1610  * point would then track the mouse as closely as possible.  This is a
1611  * simple example, though, so that is left as an Exercise for the
1612  * Programmer.
1613  */
1614 #define TRACKBALLSIZE  (0.8f)
1615 #define SQRT(x) sqrt(x)
1616
1617 /*
1618  * Local function prototypes (not defined in trackball.h)
1619  */
1620 static float tb_project_to_sphere(float, float, float);
1621 static void normalize_quat(float [4]);
1622
1623 static void
1624 vzero(float *v)
1625 {
1626     v[0] = 0.0;
1627     v[1] = 0.0;
1628     v[2] = 0.0;
1629 }
1630
1631 static void
1632 vset(float *v, float x, float y, float z)
1633 {
1634     v[0] = x;
1635     v[1] = y;
1636     v[2] = z;
1637 }
1638
1639 static void
1640 vsub(const float *src1, const float *src2, float *dst)
1641 {
1642     dst[0] = src1[0] - src2[0];
1643     dst[1] = src1[1] - src2[1];
1644     dst[2] = src1[2] - src2[2];
1645 }
1646
1647 static void
1648 vcopy(const float *v1, float *v2)
1649 {
1650     register int i;
1651     for (i = 0 ; i < 3 ; i++)
1652         v2[i] = v1[i];
1653 }
1654
1655 static void
1656 vcross(const float *v1, const float *v2, float *cross)
1657 {
1658     float temp[3];
1659
1660     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1661     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1662     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1663     vcopy(temp, cross);
1664 }
1665
1666 static float
1667 vlength(const float *v)
1668 {
1669     float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1670     return SQRT(tmp);
1671 }
1672
1673 static void
1674 vscale(float *v, float div)
1675 {
1676     v[0] *= div;
1677     v[1] *= div;
1678     v[2] *= div;
1679 }
1680
1681 static void
1682 vnormal(float *v)
1683 {
1684     vscale(v,1.0/vlength(v));
1685 }
1686
1687 static float
1688 vdot(const float *v1, const float *v2)
1689 {
1690     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1691 }
1692
1693 static void
1694 vadd(const float *src1, const float *src2, float *dst)
1695 {
1696     dst[0] = src1[0] + src2[0];
1697     dst[1] = src1[1] + src2[1];
1698     dst[2] = src1[2] + src2[2];
1699 }
1700
1701 /*
1702  *  Given an axis and angle, compute quaternion.
1703  */
1704 void
1705 axis_to_quat(float a[3], float phi, float q[4])
1706 {
1707     double sinphi2, cosphi2;
1708     double phi2 = phi/2.0;
1709     sinphi2 = sin(phi2);
1710     cosphi2 = cos(phi2);
1711     vnormal(a);
1712     vcopy(a,q);
1713     vscale(q,sinphi2);
1714     q[3] = cosphi2;
1715 }
1716
1717 /*
1718  * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1719  * if we are away from the center of the sphere.
1720  */
1721 static float
1722 tb_project_to_sphere(float r, float x, float y)
1723 {
1724     float d, t, z, tmp;
1725
1726     tmp = x*x + y*y;
1727     d = SQRT(tmp);
1728     if (d < r * 0.70710678118654752440) {    /* Inside sphere */
1729         tmp = r*r - d*d;
1730         z = SQRT(tmp);
1731     } else {           /* On hyperbola */
1732         t = r / 1.41421356237309504880;
1733         z = t*t / d;
1734     }
1735     return z;
1736 }
1737
1738 /*
1739  * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0
1740  * If they don't add up to 1.0, dividing by their magnitued will
1741  * renormalize them.
1742  *
1743  * Note: See the following for more information on quaternions:
1744  *
1745  * - Shoemake, K., Animating rotation with quaternion curves, Computer
1746  *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1747  * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1748  *   graphics, The Visual Computer 5, 2-13, 1989.
1749  */
1750 static void
1751 normalize_quat(float q[4])
1752 {
1753     int i;
1754     float mag, tmp;
1755
1756     tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1757     mag = 1.0 / SQRT(tmp);
1758     for (i = 0; i < 4; i++)
1759         q[i] *= mag;
1760 }
1761
1762 /*
1763  * Ok, simulate a track-ball.  Project the points onto the virtual
1764  * trackball, then figure out the axis of rotation, which is the cross
1765  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1766  * Note:  This is a deformed trackball-- is a trackball in the center,
1767  * but is deformed into a hyperbolic sheet of rotation away from the
1768  * center.  This particular function was chosen after trying out
1769  * several variations.
1770  *
1771  * It is assumed that the arguments to this routine are in the range
1772  * (-1.0 ... 1.0)
1773  */
1774 void
1775 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1776 {
1777     float a[3]; /* Axis of rotation */
1778     float phi;  /* how much to rotate about axis */
1779     float p1[3], p2[3], d[3];
1780     float t;
1781
1782     if (p1x == p2x && p1y == p2y) {
1783         /* Zero rotation */
1784         vzero(q);
1785         q[3] = 1.0;
1786         return;
1787     }
1788
1789     /*
1790      * First, figure out z-coordinates for projection of P1 and P2 to
1791      * deformed sphere
1792      */
1793     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1794     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1795
1796     /*
1797      *  Now, we want the cross product of P1 and P2
1798      */
1799     vcross(p2,p1,a);
1800
1801     /*
1802      *  Figure out how much to rotate around that axis.
1803      */
1804     vsub(p1,p2,d);
1805     t = vlength(d) / (2.0*TRACKBALLSIZE);
1806
1807     /*
1808      * Avoid problems with out-of-control values...
1809      */
1810     if (t > 1.0) t = 1.0;
1811     if (t < -1.0) t = -1.0;
1812     phi = 2.0 * asin(t);
1813
1814     axis_to_quat(a,phi,q);
1815 }
1816
1817 /*
1818  * Given two rotations, e1 and e2, expressed as quaternion rotations,
1819  * figure out the equivalent single rotation and stuff it into dest.
1820  *
1821  * This routine also normalizes the result every RENORMCOUNT times it is
1822  * called, to keep error from creeping in.
1823  *
1824  * NOTE: This routine is written so that q1 or q2 may be the same
1825  * as dest (or each other).
1826  */
1827
1828 #define RENORMCOUNT 97
1829
1830 void
1831 add_quats(float q1[4], float q2[4], float dest[4])
1832 {
1833     static int count=0;
1834     float t1[4], t2[4], t3[4];
1835     float tf[4];
1836
1837 #if 0
1838 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1839 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1840 #endif
1841
1842     vcopy(q1,t1);
1843     vscale(t1,q2[3]);
1844
1845     vcopy(q2,t2);
1846     vscale(t2,q1[3]);
1847
1848     vcross(q2,q1,t3);
1849     vadd(t1,t2,tf);
1850     vadd(t3,tf,tf);
1851     tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1852
1853 #if 0
1854 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1855 #endif
1856
1857     dest[0] = tf[0];
1858     dest[1] = tf[1];
1859     dest[2] = tf[2];
1860     dest[3] = tf[3];
1861
1862     if (++count > RENORMCOUNT) {
1863         count = 0;
1864         normalize_quat(dest);
1865     }
1866 }
1867
1868 /*
1869  * Build a rotation matrix, given a quaternion rotation.
1870  *
1871  */
1872 void
1873 build_rotmatrix(float m[4][4], float q[4])
1874 {
1875 //#define TRANSPOSED_QUAT
1876 #ifndef TRANSPOSED_QUAT
1877     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1878     m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1879     m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1880     m[0][3] = 0.0;
1881
1882     m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1883     m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1884     m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1885     m[1][3] = 0.0;
1886
1887     m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1888     m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1889     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1890     
1891     m[2][3] = 0.0;
1892     m[3][0] = 0.0;
1893     m[3][1] = 0.0;
1894     m[3][2] = 0.0;
1895     m[3][3] = 1.0;
1896 #else //  TRANSPOSED_QUAT
1897     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1898     m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1899     m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1900     m[0][3] = 0.0;
1901
1902     m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1903     m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1904     m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1905     m[1][3] = 0.0;
1906
1907     m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1908     m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1909     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1910     m[2][3] = 0.0;
1911     
1912     m[3][0] = 0.0;
1913     m[3][1] = 0.0;
1914     m[3][2] = 0.0;
1915     m[3][3] = 1.0;
1916 #endif // 0
1917 }
1918