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