1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * Additional mouse supported added by David Megginson, 1999.
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.
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.
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.
24 **************************************************************************/
31 #include <simgear/compiler.h>
33 #ifdef FG_MATH_EXCEPTION_CLASH
42 #include <simgear/xgl/xgl.h>
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
54 #include <simgear/constants.h>
55 #include <simgear/debug/logstream.hxx>
56 #include <simgear/misc/fgpath.hxx>
57 #include <simgear/screen/screen-dump.hxx>
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 #include <Main/options.hxx>
75 #include <NetworkOLK/network.h>
78 #if defined( WIN32 ) && !defined( __CYGWIN__ )
79 # include <simgear/screen/win32-printer.h>
80 # include <simgear/screen/GlBitmaps.h>
84 #include "gui_local.hxx"
85 #include "apt_dlg.hxx"
86 #include "net_dlg.hxx"
87 #include "sgVec3Slider.hxx"
91 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
95 #if defined(WIN32) || defined(__CYGWIN32__)
96 #define WIN32_CURSOR_TWEAKS
97 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
98 #define X_CURSOR_TWEAKS
101 // main.cxx hack, should come from an include someplace
102 extern void fgInitVisuals( void );
103 extern void fgReshape( int width, int height );
104 extern void fgRenderFrame( void );
107 fntTexFont *guiFntHandle = 0;
109 static puMenuBar *mainMenuBar = 0;
110 //static puButton *hideMenuButton = 0;
112 static puDialogBox *dialogBox = 0;
113 static puFrame *dialogFrame = 0;
114 static puText *dialogBoxMessage = 0;
115 static puOneShot *dialogBoxOkButton = 0;
118 static puDialogBox *YNdialogBox = 0;
119 static puFrame *YNdialogFrame = 0;
120 static puText *YNdialogBoxMessage = 0;
121 static puOneShot *YNdialogBoxOkButton = 0;
122 static puOneShot *YNdialogBoxNoButton = 0;
124 static char msg_OK[] = "OK";
125 static char msg_NO[] = "NO";
126 static char msg_YES[] = "YES";
127 static char msg_CANCEL[] = "Cancel";
128 static char msg_RESET[] = "Reset";
130 char *gui_msg_OK; // "OK"
131 char *gui_msg_NO; // "NO"
132 char *gui_msg_YES; // "YES"
133 char *gui_msg_CANCEL; // "CANCEL"
134 char *gui_msg_RESET; // "RESET"
136 static char global_dialog_string[256];
139 extern void fgLatLonFormatToggle( puObject *);
142 /* ================ General Purpose Functions ================ */
144 // Intercept the Escape Key
145 void ConfirmExitDialog(void)
147 FG_PUSH_PUI_DIALOG( YNdialogBox );
150 // General Purpose Message Box
151 void mkDialog (const char *txt)
153 strncpy(global_dialog_string, txt, 256);
154 dialogBoxMessage->setLabel(global_dialog_string);
155 FG_PUSH_PUI_DIALOG( dialogBox );
158 // Toggle the Menu and Mouse display state
159 void guiToggleMenu(void)
162 // printf("Hiding Menu\n");
163 mainMenuBar->hide ();
164 #if defined(WIN32_CURSOR_TWEAKS)
165 if( mouse_mode == MOUSE_POINTER )
167 #endif // #ifdef WIN32_CURSOR_TWEAKS
169 // printf("Showing Menu\n");
170 mainMenuBar->reveal();
173 #endif // #ifdef WIN32
175 gui_menu_on = ~gui_menu_on;
178 /* -----------------------------------------------------------------------
179 the Gui callback functions
180 ____________________________________________________________________*/
183 // Hier Neu :-) This is my newly added code
184 // Added by David Findlay <nedz@bigpond.com>
185 // on Sunday 3rd of December
187 // Start new Save Dialog Box
188 static puDialogBox *SaveDialog = 0;
189 static puFrame *SaveDialogFrame = 0;
190 static puText *SaveDialogMessage = 0;
191 static puInput *SaveDialogInput = 0;
193 static puOneShot *SaveDialogOkButton = 0;
194 static puOneShot *SaveDialogCancelButton = 0;
195 static puOneShot *SaveDialogResetButton = 0;
197 // Default save filename
198 static char saveFile[256] = "fgfs.sav";
201 void SaveDialogCancel(puObject *) {
202 FG_POP_PUI_DIALOG( SaveDialog );
205 // If press OK do this
206 void SaveDialogOk(puObject*) {
208 FG_POP_PUI_DIALOG( SaveDialog );
211 SaveDialogInput->getValue(&s);
214 cout << saveFile << endl;
215 if (output.good() && fgSaveFlight(output)) {
217 mkDialog("Saved flight");
218 FG_LOG(FG_INPUT, FG_INFO, "Saved flight");
220 mkDialog("Cannot save flight");
221 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight");
226 static void saveFlight(puObject *cv) {
227 SaveDialog = new puDialogBox (150, 50);
229 SaveDialogFrame = new puFrame (0,0,350, 150);
230 SaveDialogMessage = new puText (
231 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
232 SaveDialogMessage -> setLabel ("File Name:");
234 SaveDialogInput = new puInput (50, 70, 300, 100);
235 SaveDialogInput -> setValue (saveFile);
236 SaveDialogInput -> acceptInput();
238 SaveDialogOkButton = new puOneShot (50, 10, 110, 50);
239 SaveDialogOkButton -> setLegend (gui_msg_OK);
240 SaveDialogOkButton -> setCallback ( SaveDialogOk );
241 SaveDialogOkButton -> makeReturnDefault(TRUE);
243 SaveDialogCancelButton = new puOneShot (140, 10, 210, 50);
244 SaveDialogCancelButton -> setLegend (gui_msg_CANCEL);
245 SaveDialogCancelButton -> setCallback ( SaveDialogCancel );
247 FG_FINALIZE_PUI_DIALOG( SaveDialog );
249 SaveDialog -> reveal();
253 static puDialogBox *LoadDialog = 0;
254 static puFrame *LoadDialogFrame = 0;
255 static puText *LoadDialogMessage = 0;
256 static puInput *LoadDialogInput = 0;
258 static puOneShot *LoadDialogOkButton = 0;
259 static puOneShot *LoadDialogCancelButton = 0;
260 static puOneShot *LoadDialogResetButton = 0;
262 // Default load filename
263 static char loadFile[256] = "fgfs.sav";
265 // Do this if the person click okay
266 void LoadDialogOk(puObject *) {
268 FG_POP_PUI_DIALOG( LoadDialog );
271 LoadDialogInput->getValue(&l);
274 if (input.good() && fgLoadFlight(input)) {
276 mkDialog("Loaded flight");
277 FG_LOG(FG_INPUT, FG_INFO, "Restored flight");
279 mkDialog("Failed to load flight");
280 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight");
284 // Do this is the person presses cancel
285 void LoadDialogCancel(puObject *) {
286 FG_POP_PUI_DIALOG( LoadDialog );
289 // Create Load Dialog
290 static void loadFlight(puObject *cb)
292 LoadDialog = new puDialogBox (150, 50);
294 LoadDialogFrame = new puFrame (0,0,350, 150);
295 LoadDialogMessage = new puText (
296 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
297 LoadDialogMessage -> setLabel ("File Name:");
299 LoadDialogInput = new puInput (50, 70, 300, 100);
300 LoadDialogInput -> setValue (loadFile);
301 LoadDialogInput -> acceptInput();
303 LoadDialogOkButton = new puOneShot (50, 10, 110, 50);
304 LoadDialogOkButton -> setLegend (gui_msg_OK);
305 LoadDialogOkButton -> setCallback ( LoadDialogOk );
306 LoadDialogOkButton -> makeReturnDefault(TRUE);
308 LoadDialogCancelButton = new puOneShot (140, 10, 210, 50);
309 LoadDialogCancelButton -> setLegend (gui_msg_CANCEL);
310 LoadDialogCancelButton -> setCallback ( LoadDialogCancel );
312 FG_FINALIZE_PUI_DIALOG( LoadDialog );
314 LoadDialog -> reveal();
317 // This is the accessor function
318 void guiTogglePanel(puObject *cb)
320 if (fgGetBool("/sim/panel/visibility"))
321 fgSetBool("/sim/panel/visibility", false);
323 fgSetBool("/sim/panel/visibility", true);
325 fgReshape(fgGetInt("/sim/startup/xsize"),
326 fgGetInt("/sim/startup/ysize"));
329 //void MenuHideMenuCb(puObject *cb)
330 void hideMenuCb (puObject *cb)
335 void goodBye(puObject *)
337 // FG_LOG( FG_INPUT, FG_ALERT,
338 // "Program exiting normally at user request." );
339 cout << "Program exiting normally at user request." << endl;
341 #ifdef FG_NETWORK_OLK
342 if ( fgGetBool("/sim/networking/network-olk") ) {
343 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
347 // close all external I/O connections
354 void goAwayCb (puObject *me)
356 FG_POP_PUI_DIALOG( dialogBox );
359 void mkDialogInit (void)
361 // printf("mkDialogInit\n");
362 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
363 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
364 dialogBox = new puDialogBox (x, y); // 150, 50
366 dialogFrame = new puFrame (0,0,400,100);
367 dialogBoxMessage = new puText (10, 70);
368 dialogBoxMessage -> setLabel ("");
369 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
370 dialogBoxOkButton -> setLegend (gui_msg_OK);
371 dialogBoxOkButton -> makeReturnDefault (TRUE );
372 dialogBoxOkButton -> setCallback (goAwayCb);
374 FG_FINALIZE_PUI_DIALOG( dialogBox );
377 void MayBeGoodBye(puObject *)
382 void goAwayYesNoCb(puObject *me)
384 FG_POP_PUI_DIALOG( YNdialogBox);
387 void ConfirmExitDialogInit(void)
389 char msg[] = "Really Quit";
392 // printf("ConfirmExitDialogInit\n");
393 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
395 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
396 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
398 YNdialogBox = new puDialogBox (x, y); // 150, 50
399 // YNdialogBox = new puDialogBox (150, 50);
401 YNdialogFrame = new puFrame (0,0,400, 100);
403 YNdialogBoxMessage = new puText (len, 70);
404 YNdialogBoxMessage -> setDefaultValue (msg);
405 YNdialogBoxMessage -> getDefaultValue (&s);
406 YNdialogBoxMessage -> setLabel (s);
408 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
409 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
410 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
411 YNdialogBoxOkButton -> setCallback (goodBye);
413 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
414 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
415 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
417 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
420 void notCb (puObject *)
422 mkDialog ("This function isn't implemented yet");
425 void helpCb (puObject *)
429 #if defined(FX) && !defined(WIN32)
430 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
431 if ( global_fullscreen ) {
432 global_fullscreen = false;
433 XMesaSetFXmode( XMESA_FX_WINDOW );
439 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
441 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
442 command = "netscape -remote \"openURL(" + url + ")\" &";
444 command = "netscape " + url + " &";
447 command = "webrun.bat";
450 system( command.c_str() );
451 //string text = "Help started in netscape window.";
453 //mkDialog (text.c_str());
454 mkDialog ("Help started in netscape window.");
457 #if defined( WIN32 ) && !defined( __CYGWIN__)
459 static void rotateView( double roll, double pitch, double yaw )
464 static GlBitmap *b1 = NULL;
465 extern FGInterface cur_view_fdm;
466 GLubyte *hiResScreenCapture( int multiplier )
468 float oldfov = fgGetDouble("/sim/field-of-view");
469 float fov = oldfov / multiplier;
470 FGViewer *v = globals->get_current_view();
471 fgSetDouble("/sim/field-of-view", fov);
473 int cur_width = fgGetInt("/sim/startup/xsize");
474 int cur_height = fgGetInt("/sim/startup/ysize");
475 if (b1) delete( b1 );
476 // New empty (mostly) bitmap
477 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
479 for ( y = 0; y < multiplier; y++ ) {
480 for ( x = 0; x < multiplier; x++ ) {
481 fgReshape( cur_width, cur_height );
483 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
487 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
490 fgSetDouble("/sim/field-of-view", oldfov);
491 return b1->getBitmap();
496 #if defined( WIN32 ) && !defined( __CYGWIN__)
497 // win32 print screen function
498 void printScreen ( puObject *obj ) {
499 bool show_pu_cursor = false;
501 if ( !puCursorIsHidden() ) {
502 show_pu_cursor = true;
508 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
509 int cur_width = fgGetInt("/sim/startup/xsize");
510 int cur_height = fgGetInt("/sim/startup/ysize");
511 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
512 p.End( hiResScreenCapture(3) );
515 mainMenuBar->reveal();
518 if ( show_pu_cursor ) {
523 #endif // #ifdef WIN32
526 void dumpSnapShot ( puObject *obj ) {
531 // do a screen snap shot
532 void fgDumpSnapShot () {
533 bool show_pu_cursor = false;
535 int freeze = globals->get_freeze();
537 globals->set_freeze( true );
541 if ( !puCursorIsHidden() ) {
542 show_pu_cursor = true;
547 fgReshape( fgGetInt("/sim/startup/xsize"),
548 fgGetInt("/sim/startup/ysize") );
550 // we need two render frames here to clear the menu and cursor
551 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
556 my_glDumpWindow( "fgfs-screen.ppm",
557 fgGetInt("/sim/startup/xsize"),
558 fgGetInt("/sim/startup/ysize") );
560 mkDialog ("Snap shot saved to fgfs-screen.ppm");
562 if ( show_pu_cursor ) {
568 mainMenuBar->reveal();
572 globals->set_freeze( false );
575 #ifdef FG_NETWORK_OLK
576 static void net_display_toggle( puObject *cb)
578 net_hud_display = (net_hud_display) ? 0 : 1;
579 printf("Toggle net_hud_display : %d\n", net_hud_display);
582 static void net_register( puObject *cb)
584 fgd_send_com( "1", FGFS_host );
585 net_is_registered = 0;
586 printf("Registering to deamon\n");
589 static void net_unregister( puObject *cb)
591 fgd_send_com( "8", FGFS_host );
592 net_is_registered = -1;
593 printf("Unregistering from deamon\n");
596 extern void net_fgd_scan(puObject *cb);
597 #endif // #ifdef FG_NETWORK_OLK
599 /* -----------------------------------------------------------------------
601 ---------------------------------------------------------------------*/
602 char *fileSubmenu [] = {
603 "Exit", /* "Close", "---------", */
604 #if defined( WIN32 ) && !defined( __CYGWIN__)
614 puCallback fileSubmenuCb [] = {
615 MayBeGoodBye, /* hideMenuCb, NULL, */
616 #if defined( WIN32 ) && !defined( __CYGWIN__)
629 char *editSubmenu [] = {
632 puCallback editSubmenuCb [] = {
637 extern void fgHUDalphaAdjust( puObject * );
638 char *viewSubmenu [] = {
641 /* "Cockpit View > ", "View >","------------", */
642 "Toggle Panel...", NULL
644 puCallback viewSubmenuCb [] = {
647 /* notCb, notCb, NULL, */
653 char *autopilotSubmenu [] = {
654 "Toggle HUD Format", "Adjust AP Settings",
656 "Clear Route", "Skip Current Waypoint", "Add Waypoint",
658 "Set Altitude", "Set Heading",
662 puCallback autopilotSubmenuCb [] = {
663 fgLatLonFormatToggle, fgAPAdjust,
665 ClearRoute, PopWayPoint, AddWayPoint,
667 NewAltitude, NewHeading,
671 char *environmentSubmenu [] = {
672 "Goto Airport", /* "Terrain", "Weather", */ NULL
674 puCallback environmentSubmenuCb [] = {
675 NewAirport, /* notCb, notCb, */ NULL
679 char *optionsSubmenu [] = {
680 "Preferences", "Realism & Reliablity...", NULL
682 puCallback optionsSubmenuCb [] = {
687 #ifdef FG_NETWORK_OLK
688 char *networkSubmenu [] = {
689 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
691 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
692 "Toggle Display", NULL
694 puCallback networkSubmenuCb [] = {
695 /* notCb, notCb, notCb, notCb, */
698 net_fgd_scan, NewCallSign,
699 net_display_toggle, NULL
703 char *helpSubmenu [] = {
704 /* "About...", */ "Help", NULL
706 puCallback helpSubmenuCb [] = {
707 /* notCb, */ helpCb, NULL
711 /* -------------------------------------------------------------------------
713 _____________________________________________________________________*/
718 char *mesa_win_state;
722 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
723 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
725 // Initialize our GLOBAL GUI STRINGS
726 gui_msg_OK = msg_OK; // "OK"
727 gui_msg_NO = msg_NO; // "NO"
728 gui_msg_YES = msg_YES; // "YES"
729 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
730 gui_msg_RESET = msg_RESET; // "RESET"
732 // Next check home directory
734 char* envp = ::getenv( "FG_FONTS" );
735 if ( envp != NULL ) {
738 fntpath.set( globals->get_fg_root() );
739 fntpath.append( "Fonts" );
742 // Install our fast fonts
743 fntpath.append( "typewriter.txf" );
744 guiFntHandle = new fntTexFont ;
745 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
746 puFont GuiFont ( guiFntHandle, 15 ) ;
747 puSetDefaultFonts( GuiFont, GuiFont ) ;
748 guiFnt = puGetDefaultLabelFont();
750 if (!fgHasValue("/sim/startup/mouse-pointer")) {
751 // no preference specified for mouse pointer, attempt to autodetect...
752 // Determine if we need to render the cursor, or if the windowing
753 // system will do it. First test if we are rendering with glide.
754 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
755 // Test for the MESA_GLX_FX env variable
756 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
757 // test if we are fullscreen mesa/glide
758 if ( (mesa_win_state[0] == 'f') ||
759 (mesa_win_state[0] == 'F') ) {
764 // mouse_active = ~mouse_active;
765 } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
766 // don't show pointer
768 // force showing pointer
770 // mouse_active = ~mouse_active;
773 // MOUSE_VIEW mode stuff
776 // Set up our Dialog Boxes
777 ConfirmExitDialogInit();
779 #ifdef FG_NETWORK_OLK
787 mainMenuBar = new puMenuBar ();
788 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
789 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
790 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
791 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
792 mainMenuBar -> add_submenu ("Autopilot", autopilotSubmenu, autopilotSubmenuCb);
793 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
794 #ifdef FG_NETWORK_OLK
795 if ( fgGetBool("/sim/networking/network-olk") ) {
796 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
799 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
800 mainMenuBar-> close ();
801 // Set up menu bar toggle