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