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 SG_MATH_EXCEPTION_CLASH
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/sg_path.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/fg_init.hxx>
68 #include <Main/fg_io.hxx>
69 #include <Main/globals.hxx>
70 #include <Main/fg_props.hxx>
71 #include <Main/options.hxx>
74 #include <NetworkOLK/network.h>
77 #if defined( WIN32 ) && !defined( __CYGWIN__ )
78 # include <simgear/screen/win32-printer.h>
79 # include <simgear/screen/GlBitmaps.h>
83 #include "gui_local.hxx"
84 #include "apt_dlg.hxx"
85 #include "net_dlg.hxx"
86 #include "sgVec3Slider.hxx"
90 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
95 #define snprintf _snprintf
98 // main.cxx hack, should come from an include someplace
99 extern void fgInitVisuals( void );
100 extern void fgReshape( int width, int height );
101 extern void fgRenderFrame( void );
104 fntTexFont *guiFntHandle = 0;
106 static puMenuBar *mainMenuBar = 0;
107 //static puButton *hideMenuButton = 0;
109 static puDialogBox *dialogBox = 0;
110 static puFrame *dialogFrame = 0;
111 static puText *dialogBoxMessage = 0;
112 static puOneShot *dialogBoxOkButton = 0;
115 static puDialogBox *YNdialogBox = 0;
116 static puFrame *YNdialogFrame = 0;
117 static puText *YNdialogBoxMessage = 0;
118 static puOneShot *YNdialogBoxOkButton = 0;
119 static puOneShot *YNdialogBoxNoButton = 0;
121 static char msg_OK[] = "OK";
122 static char msg_NO[] = "NO";
123 static char msg_YES[] = "YES";
124 static char msg_CANCEL[] = "Cancel";
125 static char msg_RESET[] = "Reset";
127 char *gui_msg_OK; // "OK"
128 char *gui_msg_NO; // "NO"
129 char *gui_msg_YES; // "YES"
130 char *gui_msg_CANCEL; // "CANCEL"
131 char *gui_msg_RESET; // "RESET"
133 static char global_dialog_string[256];
136 extern void fgLatLonFormatToggle( puObject *);
139 /* ================ General Purpose Functions ================ */
141 // Intercept the Escape Key
142 void ConfirmExitDialog(void)
144 FG_PUSH_PUI_DIALOG( YNdialogBox );
147 // General Purpose Message Box
148 void mkDialog (const char *txt)
150 strncpy(global_dialog_string, txt, 256);
151 dialogBoxMessage->setLabel(global_dialog_string);
152 FG_PUSH_PUI_DIALOG( dialogBox );
155 // Toggle the Menu and Mouse display state
156 void guiToggleMenu(void)
159 // printf("Hiding Menu\n");
160 mainMenuBar->hide ();
161 #if defined(WIN32_CURSOR_TWEAKS)
162 if( mouse_mode == MOUSE_POINTER )
164 #endif // #ifdef WIN32_CURSOR_TWEAKS
166 // printf("Showing Menu\n");
167 mainMenuBar->reveal();
170 #endif // #ifdef WIN32
172 gui_menu_on = ~gui_menu_on;
175 /* -----------------------------------------------------------------------
176 the Gui callback functions
177 ____________________________________________________________________*/
180 // Hier Neu :-) This is my newly added code
181 // Added by David Findlay <nedz@bigpond.com>
182 // on Sunday 3rd of December
184 // Start new Save Dialog Box
185 static puDialogBox *SaveDialog = 0;
186 static puFrame *SaveDialogFrame = 0;
187 static puText *SaveDialogMessage = 0;
188 static puInput *SaveDialogInput = 0;
190 static puOneShot *SaveDialogOkButton = 0;
191 static puOneShot *SaveDialogCancelButton = 0;
192 static puOneShot *SaveDialogResetButton = 0;
194 // Default save filename
195 static char saveFile[256] = "fgfs.sav";
198 void SaveDialogCancel(puObject *) {
199 FG_POP_PUI_DIALOG( SaveDialog );
202 // If press OK do this
203 void SaveDialogOk(puObject*) {
205 FG_POP_PUI_DIALOG( SaveDialog );
208 SaveDialogInput->getValue(&s);
211 cout << saveFile << endl;
212 if (output.good() && fgSaveFlight(output)) {
214 mkDialog("Saved flight");
215 SG_LOG(SG_INPUT, SG_INFO, "Saved flight");
217 mkDialog("Cannot save flight");
218 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight");
223 static void saveFlight(puObject *cv) {
224 SaveDialog = new puDialogBox (150, 50);
226 SaveDialogFrame = new puFrame (0,0,350, 150);
227 SaveDialogMessage = new puText (
228 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
229 SaveDialogMessage -> setLabel ("File Name:");
231 SaveDialogInput = new puInput (50, 70, 300, 100);
232 SaveDialogInput -> setValue (saveFile);
233 SaveDialogInput -> acceptInput();
235 SaveDialogOkButton = new puOneShot (50, 10, 110, 50);
236 SaveDialogOkButton -> setLegend (gui_msg_OK);
237 SaveDialogOkButton -> setCallback ( SaveDialogOk );
238 SaveDialogOkButton -> makeReturnDefault(TRUE);
240 SaveDialogCancelButton = new puOneShot (140, 10, 210, 50);
241 SaveDialogCancelButton -> setLegend (gui_msg_CANCEL);
242 SaveDialogCancelButton -> setCallback ( SaveDialogCancel );
244 FG_FINALIZE_PUI_DIALOG( SaveDialog );
246 SaveDialog -> reveal();
250 static puDialogBox *LoadDialog = 0;
251 static puFrame *LoadDialogFrame = 0;
252 static puText *LoadDialogMessage = 0;
253 static puInput *LoadDialogInput = 0;
255 static puOneShot *LoadDialogOkButton = 0;
256 static puOneShot *LoadDialogCancelButton = 0;
257 static puOneShot *LoadDialogResetButton = 0;
259 // Default load filename
260 static char loadFile[256] = "fgfs.sav";
262 // Do this if the person click okay
263 void LoadDialogOk(puObject *) {
265 FG_POP_PUI_DIALOG( LoadDialog );
268 LoadDialogInput->getValue(&l);
271 if (input.good() && fgLoadFlight(input)) {
273 mkDialog("Loaded flight");
274 SG_LOG(SG_INPUT, SG_INFO, "Restored flight");
276 mkDialog("Failed to load flight");
277 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight");
281 // Do this is the person presses cancel
282 void LoadDialogCancel(puObject *) {
283 FG_POP_PUI_DIALOG( LoadDialog );
286 // Create Load Dialog
287 static void loadFlight(puObject *cb)
289 LoadDialog = new puDialogBox (150, 50);
291 LoadDialogFrame = new puFrame (0,0,350, 150);
292 LoadDialogMessage = new puText (
293 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
294 LoadDialogMessage -> setLabel ("File Name:");
296 LoadDialogInput = new puInput (50, 70, 300, 100);
297 LoadDialogInput -> setValue (loadFile);
298 LoadDialogInput -> acceptInput();
300 LoadDialogOkButton = new puOneShot (50, 10, 110, 50);
301 LoadDialogOkButton -> setLegend (gui_msg_OK);
302 LoadDialogOkButton -> setCallback ( LoadDialogOk );
303 LoadDialogOkButton -> makeReturnDefault(TRUE);
305 LoadDialogCancelButton = new puOneShot (140, 10, 210, 50);
306 LoadDialogCancelButton -> setLegend (gui_msg_CANCEL);
307 LoadDialogCancelButton -> setCallback ( LoadDialogCancel );
309 FG_FINALIZE_PUI_DIALOG( LoadDialog );
311 LoadDialog -> reveal();
314 // This is the accessor function
315 void guiTogglePanel(puObject *cb)
317 if (fgGetBool("/sim/panel/visibility"))
318 fgSetBool("/sim/panel/visibility", false);
320 fgSetBool("/sim/panel/visibility", true);
322 fgReshape(fgGetInt("/sim/startup/xsize"),
323 fgGetInt("/sim/startup/ysize"));
326 //void MenuHideMenuCb(puObject *cb)
327 void hideMenuCb (puObject *cb)
332 void goodBye(puObject *)
334 // SG_LOG( SG_INPUT, SG_ALERT,
335 // "Program exiting normally at user request." );
336 cout << "Program exiting normally at user request." << endl;
338 #ifdef FG_NETWORK_OLK
339 if ( fgGetBool("/sim/networking/network-olk") ) {
340 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
344 // close all external I/O connections
351 void goAwayCb (puObject *me)
353 FG_POP_PUI_DIALOG( dialogBox );
356 void mkDialogInit (void)
358 // printf("mkDialogInit\n");
359 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
360 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
361 dialogBox = new puDialogBox (x, y); // 150, 50
363 dialogFrame = new puFrame (0,0,400,100);
364 dialogBoxMessage = new puText (10, 70);
365 dialogBoxMessage -> setLabel ("");
366 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
367 dialogBoxOkButton -> setLegend (gui_msg_OK);
368 dialogBoxOkButton -> makeReturnDefault (TRUE );
369 dialogBoxOkButton -> setCallback (goAwayCb);
371 FG_FINALIZE_PUI_DIALOG( dialogBox );
374 void MayBeGoodBye(puObject *)
379 void goAwayYesNoCb(puObject *me)
381 FG_POP_PUI_DIALOG( YNdialogBox);
384 void ConfirmExitDialogInit(void)
386 char msg[] = "Really Quit";
389 // printf("ConfirmExitDialogInit\n");
390 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
392 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
393 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
395 YNdialogBox = new puDialogBox (x, y); // 150, 50
396 // YNdialogBox = new puDialogBox (150, 50);
398 YNdialogFrame = new puFrame (0,0,400, 100);
400 YNdialogBoxMessage = new puText (len, 70);
401 YNdialogBoxMessage -> setDefaultValue (msg);
402 YNdialogBoxMessage -> getDefaultValue (&s);
403 YNdialogBoxMessage -> setLabel (s);
405 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
406 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
407 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
408 YNdialogBoxOkButton -> setCallback (goodBye);
410 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
411 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
412 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
414 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
417 void notCb (puObject *)
419 mkDialog ("This function isn't implemented yet");
422 void helpCb (puObject *)
426 #if defined(FX) && !defined(WIN32)
427 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
428 if ( global_fullscreen ) {
429 global_fullscreen = false;
430 XMesaSetFXmode( XMESA_FX_WINDOW );
436 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
438 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
439 command = "netscape -remote \"openURL(" + url + ")\" &";
441 command = "netscape " + url + " &";
444 command = "webrun.bat";
447 system( command.c_str() );
448 //string text = "Help started in netscape window.";
450 //mkDialog (text.c_str());
451 mkDialog ("Help started in netscape window.");
454 #if defined( WIN32 ) && !defined( __CYGWIN__)
456 static void rotateView( double roll, double pitch, double yaw )
461 static GlBitmap *b1 = NULL;
462 extern FGInterface cur_view_fdm;
463 GLubyte *hiResScreenCapture( int multiplier )
465 float oldfov = fgGetDouble("/sim/field-of-view");
466 float fov = oldfov / multiplier;
467 FGViewer *v = globals->get_current_view();
468 fgSetDouble("/sim/field-of-view", fov);
470 int cur_width = fgGetInt("/sim/startup/xsize");
471 int cur_height = fgGetInt("/sim/startup/ysize");
472 if (b1) delete( b1 );
473 // New empty (mostly) bitmap
474 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
476 for ( y = 0; y < multiplier; y++ ) {
477 for ( x = 0; x < multiplier; x++ ) {
478 fgReshape( cur_width, cur_height );
480 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
484 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
487 fgSetDouble("/sim/field-of-view", oldfov);
488 return b1->getBitmap();
493 #if defined( WIN32 ) && !defined( __CYGWIN__)
494 // win32 print screen function
495 void printScreen ( puObject *obj ) {
496 bool show_pu_cursor = false;
498 if ( !puCursorIsHidden() ) {
499 show_pu_cursor = true;
505 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
506 int cur_width = fgGetInt("/sim/startup/xsize");
507 int cur_height = fgGetInt("/sim/startup/ysize");
508 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
509 p.End( hiResScreenCapture(3) );
512 mainMenuBar->reveal();
515 if ( show_pu_cursor ) {
520 #endif // #ifdef WIN32
523 void dumpSnapShot ( puObject *obj ) {
528 // do a screen snap shot
529 void fgDumpSnapShot () {
530 bool show_pu_cursor = false;
531 char *filename = new char [24];
533 static int count = 1;
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 fgRenderFrame() shouldn't
556 while (count < 1000) {
558 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
559 if ( (fp = fopen(filename, "r")) == NULL )
564 if ( sg_glDumpWindow( filename,
565 fgGetInt("/sim/startup/xsize"),
566 fgGetInt("/sim/startup/ysize")) ) {
567 message = "Snap shot saved to ";
570 message = "Failed to save to ";
574 mkDialog (message.c_str());
578 if ( show_pu_cursor ) {
584 mainMenuBar->reveal();
588 globals->set_freeze( false );
591 #ifdef FG_NETWORK_OLK
592 static void net_display_toggle( puObject *cb)
594 net_hud_display = (net_hud_display) ? 0 : 1;
595 printf("Toggle net_hud_display : %d\n", net_hud_display);
598 static void net_register( puObject *cb)
600 fgd_send_com( "1", FGFS_host );
601 net_is_registered = 0;
602 printf("Registering to deamon\n");
605 static void net_unregister( puObject *cb)
607 fgd_send_com( "8", FGFS_host );
608 net_is_registered = -1;
609 printf("Unregistering from deamon\n");
612 extern void net_fgd_scan(puObject *cb);
613 #endif // #ifdef FG_NETWORK_OLK
615 /* -----------------------------------------------------------------------
617 ---------------------------------------------------------------------*/
618 char *fileSubmenu [] = {
619 "Exit", /* "Close", "---------", */
620 #if defined( WIN32 ) && !defined( __CYGWIN__)
630 puCallback fileSubmenuCb [] = {
631 MayBeGoodBye, /* hideMenuCb, NULL, */
632 #if defined( WIN32 ) && !defined( __CYGWIN__)
645 char *editSubmenu [] = {
648 puCallback editSubmenuCb [] = {
653 extern void fgHUDalphaAdjust( puObject * );
654 char *viewSubmenu [] = {
657 /* "Cockpit View > ", "View >","------------", */
658 "Toggle Panel...", NULL
660 puCallback viewSubmenuCb [] = {
663 /* notCb, notCb, NULL, */
669 char *autopilotSubmenu [] = {
670 "Toggle HUD Format", "Adjust AP Settings",
672 "Clear Route", "Skip Current Waypoint", "Add Waypoint",
674 "Set Altitude", "Set Heading",
678 puCallback autopilotSubmenuCb [] = {
679 fgLatLonFormatToggle, fgAPAdjust,
681 ClearRoute, PopWayPoint, AddWayPoint,
683 NewAltitude, NewHeading,
687 char *environmentSubmenu [] = {
688 "Goto Airport", /* "Terrain", "Weather", */ NULL
690 puCallback environmentSubmenuCb [] = {
691 NewAirport, /* notCb, notCb, */ NULL
695 char *optionsSubmenu [] = {
696 "Preferences", "Realism & Reliablity...", NULL
698 puCallback optionsSubmenuCb [] = {
703 #ifdef FG_NETWORK_OLK
704 char *networkSubmenu [] = {
705 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
707 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
708 "Toggle Display", NULL
710 puCallback networkSubmenuCb [] = {
711 /* notCb, notCb, notCb, notCb, */
714 net_fgd_scan, NewCallSign,
715 net_display_toggle, NULL
719 char *helpSubmenu [] = {
720 /* "About...", */ "Help", NULL
722 puCallback helpSubmenuCb [] = {
723 /* notCb, */ helpCb, NULL
727 /* -------------------------------------------------------------------------
729 _____________________________________________________________________*/
734 char *mesa_win_state;
738 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
739 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
741 // Initialize our GLOBAL GUI STRINGS
742 gui_msg_OK = msg_OK; // "OK"
743 gui_msg_NO = msg_NO; // "NO"
744 gui_msg_YES = msg_YES; // "YES"
745 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
746 gui_msg_RESET = msg_RESET; // "RESET"
748 // Next check home directory
750 char* envp = ::getenv( "FG_FONTS" );
751 if ( envp != NULL ) {
754 fntpath.set( globals->get_fg_root() );
755 fntpath.append( "Fonts" );
758 // Install our fast fonts
759 fntpath.append( "typewriter.txf" );
760 guiFntHandle = new fntTexFont ;
761 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
762 puFont GuiFont ( guiFntHandle, 15 ) ;
763 puSetDefaultFonts( GuiFont, GuiFont ) ;
764 guiFnt = puGetDefaultLabelFont();
766 if (!fgHasNode("/sim/startup/mouse-pointer")) {
767 // no preference specified for mouse pointer, attempt to autodetect...
768 // Determine if we need to render the cursor, or if the windowing
769 // system will do it. First test if we are rendering with glide.
770 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
771 // Test for the MESA_GLX_FX env variable
772 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
773 // test if we are fullscreen mesa/glide
774 if ( (mesa_win_state[0] == 'f') ||
775 (mesa_win_state[0] == 'F') ) {
780 // mouse_active = ~mouse_active;
781 } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
782 // don't show pointer
784 // force showing pointer
786 // mouse_active = ~mouse_active;
789 // MOUSE_VIEW mode stuff
792 // Set up our Dialog Boxes
793 ConfirmExitDialogInit();
795 #ifdef FG_NETWORK_OLK
803 mainMenuBar = new puMenuBar ();
804 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
805 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
806 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
807 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
808 mainMenuBar -> add_submenu ("Autopilot", autopilotSubmenu, autopilotSubmenuCb);
809 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
810 #ifdef FG_NETWORK_OLK
811 if ( fgGetBool("/sim/networking/network-olk") ) {
812 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
815 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
816 mainMenuBar-> close ();
817 // Set up menu bar toggle