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/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 SG_HAVE_NATIVE_SGI_COMPILERS
96 #define snprintf _snprintf
99 // main.cxx hack, should come from an include someplace
100 extern void fgInitVisuals( void );
101 extern void fgReshape( int width, int height );
102 extern void fgRenderFrame( void );
105 fntTexFont *guiFntHandle = 0;
107 static puMenuBar *mainMenuBar = 0;
108 //static puButton *hideMenuButton = 0;
110 static puDialogBox *dialogBox = 0;
111 static puFrame *dialogFrame = 0;
112 static puText *dialogBoxMessage = 0;
113 static puOneShot *dialogBoxOkButton = 0;
116 static puDialogBox *YNdialogBox = 0;
117 static puFrame *YNdialogFrame = 0;
118 static puText *YNdialogBoxMessage = 0;
119 static puOneShot *YNdialogBoxOkButton = 0;
120 static puOneShot *YNdialogBoxNoButton = 0;
122 static char msg_OK[] = "OK";
123 static char msg_NO[] = "NO";
124 static char msg_YES[] = "YES";
125 static char msg_CANCEL[] = "Cancel";
126 static char msg_RESET[] = "Reset";
128 char *gui_msg_OK; // "OK"
129 char *gui_msg_NO; // "NO"
130 char *gui_msg_YES; // "YES"
131 char *gui_msg_CANCEL; // "CANCEL"
132 char *gui_msg_RESET; // "RESET"
134 static char global_dialog_string[256];
137 extern void fgLatLonFormatToggle( puObject *);
140 /* ================ General Purpose Functions ================ */
142 // Intercept the Escape Key
143 void ConfirmExitDialog(void)
145 FG_PUSH_PUI_DIALOG( YNdialogBox );
148 // General Purpose Message Box
149 void mkDialog (const char *txt)
151 strncpy(global_dialog_string, txt, 256);
152 dialogBoxMessage->setLabel(global_dialog_string);
153 FG_PUSH_PUI_DIALOG( dialogBox );
156 // Toggle the Menu and Mouse display state
157 void guiToggleMenu(void)
160 // printf("Hiding Menu\n");
161 mainMenuBar->hide ();
162 #if defined(WIN32_CURSOR_TWEAKS)
163 if( mouse_mode == MOUSE_POINTER )
165 #endif // #ifdef WIN32_CURSOR_TWEAKS
167 // printf("Showing Menu\n");
168 mainMenuBar->reveal();
171 #endif // #ifdef WIN32
173 gui_menu_on = ~gui_menu_on;
176 /* -----------------------------------------------------------------------
177 the Gui callback functions
178 ____________________________________________________________________*/
181 // Hier Neu :-) This is my newly added code
182 // Added by David Findlay <nedz@bigpond.com>
183 // on Sunday 3rd of December
185 // Start new Save Dialog Box
186 static puDialogBox *SaveDialog = 0;
187 static puFrame *SaveDialogFrame = 0;
188 static puText *SaveDialogMessage = 0;
189 static puInput *SaveDialogInput = 0;
191 static puOneShot *SaveDialogOkButton = 0;
192 static puOneShot *SaveDialogCancelButton = 0;
193 static puOneShot *SaveDialogResetButton = 0;
195 // Default save filename
196 static char saveFile[256] = "fgfs.sav";
199 void SaveDialogCancel(puObject *) {
200 FG_POP_PUI_DIALOG( SaveDialog );
203 // If press OK do this
204 void SaveDialogOk(puObject*) {
206 FG_POP_PUI_DIALOG( SaveDialog );
209 SaveDialogInput->getValue(&s);
212 cout << saveFile << endl;
213 if (output.good() && fgSaveFlight(output)) {
215 mkDialog("Saved flight");
216 SG_LOG(SG_INPUT, SG_INFO, "Saved flight");
218 mkDialog("Cannot save flight");
219 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight");
224 static void saveFlight(puObject *cv) {
225 SaveDialog = new puDialogBox (150, 50);
227 SaveDialogFrame = new puFrame (0,0,350, 150);
228 SaveDialogMessage = new puText (
229 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
230 SaveDialogMessage -> setLabel ("File Name:");
232 SaveDialogInput = new puInput (50, 70, 300, 100);
233 SaveDialogInput -> setValue (saveFile);
234 SaveDialogInput -> acceptInput();
236 SaveDialogOkButton = new puOneShot (50, 10, 110, 50);
237 SaveDialogOkButton -> setLegend (gui_msg_OK);
238 SaveDialogOkButton -> setCallback ( SaveDialogOk );
239 SaveDialogOkButton -> makeReturnDefault(TRUE);
241 SaveDialogCancelButton = new puOneShot (140, 10, 210, 50);
242 SaveDialogCancelButton -> setLegend (gui_msg_CANCEL);
243 SaveDialogCancelButton -> setCallback ( SaveDialogCancel );
245 FG_FINALIZE_PUI_DIALOG( SaveDialog );
247 SaveDialog -> reveal();
251 static puDialogBox *LoadDialog = 0;
252 static puFrame *LoadDialogFrame = 0;
253 static puText *LoadDialogMessage = 0;
254 static puInput *LoadDialogInput = 0;
256 static puOneShot *LoadDialogOkButton = 0;
257 static puOneShot *LoadDialogCancelButton = 0;
258 static puOneShot *LoadDialogResetButton = 0;
260 // Default load filename
261 static char loadFile[256] = "fgfs.sav";
263 // Do this if the person click okay
264 void LoadDialogOk(puObject *) {
266 FG_POP_PUI_DIALOG( LoadDialog );
269 LoadDialogInput->getValue(&l);
272 if (input.good() && fgLoadFlight(input)) {
274 mkDialog("Loaded flight");
275 SG_LOG(SG_INPUT, SG_INFO, "Restored flight");
277 mkDialog("Failed to load flight");
278 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight");
282 // Do this is the person presses cancel
283 void LoadDialogCancel(puObject *) {
284 FG_POP_PUI_DIALOG( LoadDialog );
287 // Create Load Dialog
288 static void loadFlight(puObject *cb)
290 LoadDialog = new puDialogBox (150, 50);
292 LoadDialogFrame = new puFrame (0,0,350, 150);
293 LoadDialogMessage = new puText (
294 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
295 LoadDialogMessage -> setLabel ("File Name:");
297 LoadDialogInput = new puInput (50, 70, 300, 100);
298 LoadDialogInput -> setValue (loadFile);
299 LoadDialogInput -> acceptInput();
301 LoadDialogOkButton = new puOneShot (50, 10, 110, 50);
302 LoadDialogOkButton -> setLegend (gui_msg_OK);
303 LoadDialogOkButton -> setCallback ( LoadDialogOk );
304 LoadDialogOkButton -> makeReturnDefault(TRUE);
306 LoadDialogCancelButton = new puOneShot (140, 10, 210, 50);
307 LoadDialogCancelButton -> setLegend (gui_msg_CANCEL);
308 LoadDialogCancelButton -> setCallback ( LoadDialogCancel );
310 FG_FINALIZE_PUI_DIALOG( LoadDialog );
312 LoadDialog -> reveal();
315 // This is the accessor function
316 void guiTogglePanel(puObject *cb)
318 if (fgGetBool("/sim/panel/visibility"))
319 fgSetBool("/sim/panel/visibility", false);
321 fgSetBool("/sim/panel/visibility", true);
323 fgReshape(fgGetInt("/sim/startup/xsize"),
324 fgGetInt("/sim/startup/ysize"));
327 //void MenuHideMenuCb(puObject *cb)
328 void hideMenuCb (puObject *cb)
333 void goodBye(puObject *)
335 // SG_LOG( SG_INPUT, SG_ALERT,
336 // "Program exiting normally at user request." );
337 cout << "Program exiting normally at user request." << endl;
339 #ifdef FG_NETWORK_OLK
340 if ( fgGetBool("/sim/networking/network-olk") ) {
341 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
345 // close all external I/O connections
352 void goAwayCb (puObject *me)
354 FG_POP_PUI_DIALOG( dialogBox );
357 void mkDialogInit (void)
359 // printf("mkDialogInit\n");
360 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
361 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
362 dialogBox = new puDialogBox (x, y); // 150, 50
364 dialogFrame = new puFrame (0,0,400,100);
365 dialogBoxMessage = new puText (10, 70);
366 dialogBoxMessage -> setLabel ("");
367 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
368 dialogBoxOkButton -> setLegend (gui_msg_OK);
369 dialogBoxOkButton -> makeReturnDefault (TRUE );
370 dialogBoxOkButton -> setCallback (goAwayCb);
372 FG_FINALIZE_PUI_DIALOG( dialogBox );
375 void MayBeGoodBye(puObject *)
380 void goAwayYesNoCb(puObject *me)
382 FG_POP_PUI_DIALOG( YNdialogBox);
385 void ConfirmExitDialogInit(void)
387 char msg[] = "Really Quit";
390 // printf("ConfirmExitDialogInit\n");
391 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
393 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
394 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
396 YNdialogBox = new puDialogBox (x, y); // 150, 50
397 // YNdialogBox = new puDialogBox (150, 50);
399 YNdialogFrame = new puFrame (0,0,400, 100);
401 YNdialogBoxMessage = new puText (len, 70);
402 YNdialogBoxMessage -> setDefaultValue (msg);
403 YNdialogBoxMessage -> getDefaultValue (&s);
404 YNdialogBoxMessage -> setLabel (s);
406 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
407 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
408 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
409 YNdialogBoxOkButton -> setCallback (goodBye);
411 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
412 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
413 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
415 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
418 void notCb (puObject *)
420 mkDialog ("This function isn't implemented yet");
423 void helpCb (puObject *)
427 #if defined(FX) && !defined(WIN32)
428 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
429 if ( global_fullscreen ) {
430 global_fullscreen = false;
431 XMesaSetFXmode( XMESA_FX_WINDOW );
437 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
439 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
440 command = "netscape -remote \"openURL(" + url + ")\" &";
442 command = "netscape " + url + " &";
445 command = "webrun.bat";
448 system( command.c_str() );
449 //string text = "Help started in netscape window.";
451 //mkDialog (text.c_str());
452 mkDialog ("Help started in netscape window.");
455 #if defined( WIN32 ) && !defined( __CYGWIN__)
457 static void rotateView( double roll, double pitch, double yaw )
462 static GlBitmap *b1 = NULL;
463 extern FGInterface cur_view_fdm;
464 GLubyte *hiResScreenCapture( int multiplier )
466 float oldfov = fgGetDouble("/sim/field-of-view");
467 float fov = oldfov / multiplier;
468 FGViewer *v = globals->get_current_view();
469 fgSetDouble("/sim/field-of-view", fov);
471 int cur_width = fgGetInt("/sim/startup/xsize");
472 int cur_height = fgGetInt("/sim/startup/ysize");
473 if (b1) delete( b1 );
474 // New empty (mostly) bitmap
475 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
477 for ( y = 0; y < multiplier; y++ ) {
478 for ( x = 0; x < multiplier; x++ ) {
479 fgReshape( cur_width, cur_height );
481 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
485 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
488 fgSetDouble("/sim/field-of-view", oldfov);
489 return b1->getBitmap();
494 #if defined( WIN32 ) && !defined( __CYGWIN__)
495 // win32 print screen function
496 void printScreen ( puObject *obj ) {
497 bool show_pu_cursor = false;
499 if ( !puCursorIsHidden() ) {
500 show_pu_cursor = true;
506 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
507 int cur_width = fgGetInt("/sim/startup/xsize");
508 int cur_height = fgGetInt("/sim/startup/ysize");
509 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
510 p.End( hiResScreenCapture(3) );
513 mainMenuBar->reveal();
516 if ( show_pu_cursor ) {
521 #endif // #ifdef WIN32
524 void dumpSnapShot ( puObject *obj ) {
529 // do a screen snap shot
530 void fgDumpSnapShot () {
531 bool show_pu_cursor = false;
532 char *filename = new char [24];
534 static int count = 1;
536 int freeze = globals->get_freeze();
538 globals->set_freeze( true );
542 if ( !puCursorIsHidden() ) {
543 show_pu_cursor = true;
548 fgReshape( fgGetInt("/sim/startup/xsize"),
549 fgGetInt("/sim/startup/ysize") );
551 // we need two render frames here to clear the menu and cursor
552 // ... not sure why but doing an extra fgRenderFrame() shouldn't
557 while (count < 1000) {
559 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
560 if ( (fp = fopen(filename, "r")) == NULL )
565 if ( sg_glDumpWindow( filename,
566 fgGetInt("/sim/startup/xsize"),
567 fgGetInt("/sim/startup/ysize")) ) {
568 message = "Snap shot saved to ";
571 message = "Failed to save to ";
575 mkDialog (message.c_str());
579 if ( show_pu_cursor ) {
585 mainMenuBar->reveal();
589 globals->set_freeze( false );
592 #ifdef FG_NETWORK_OLK
593 static void net_display_toggle( puObject *cb)
595 net_hud_display = (net_hud_display) ? 0 : 1;
596 printf("Toggle net_hud_display : %d\n", net_hud_display);
599 static void net_register( puObject *cb)
601 fgd_send_com( "1", FGFS_host );
602 net_is_registered = 0;
603 printf("Registering to deamon\n");
606 static void net_unregister( puObject *cb)
608 fgd_send_com( "8", FGFS_host );
609 net_is_registered = -1;
610 printf("Unregistering from deamon\n");
613 extern void net_fgd_scan(puObject *cb);
614 #endif // #ifdef FG_NETWORK_OLK
616 /* -----------------------------------------------------------------------
618 ---------------------------------------------------------------------*/
619 char *fileSubmenu [] = {
620 "Exit", /* "Close", "---------", */
621 #if defined( WIN32 ) && !defined( __CYGWIN__)
631 puCallback fileSubmenuCb [] = {
632 MayBeGoodBye, /* hideMenuCb, NULL, */
633 #if defined( WIN32 ) && !defined( __CYGWIN__)
646 char *editSubmenu [] = {
649 puCallback editSubmenuCb [] = {
654 extern void fgHUDalphaAdjust( puObject * );
655 char *viewSubmenu [] = {
658 /* "Cockpit View > ", "View >","------------", */
659 "Toggle Panel...", NULL
661 puCallback viewSubmenuCb [] = {
664 /* notCb, notCb, NULL, */
670 char *autopilotSubmenu [] = {
671 "Toggle HUD Format", "Adjust AP Settings",
673 "Clear Route", "Skip Current Waypoint", "Add Waypoint",
675 "Set Altitude", "Set Heading",
679 puCallback autopilotSubmenuCb [] = {
680 fgLatLonFormatToggle, fgAPAdjust,
682 ClearRoute, PopWayPoint, AddWayPoint,
684 NewAltitude, NewHeading,
688 char *environmentSubmenu [] = {
689 "Goto Airport", /* "Terrain", "Weather", */ NULL
691 puCallback environmentSubmenuCb [] = {
692 NewAirport, /* notCb, notCb, */ NULL
696 char *optionsSubmenu [] = {
697 "Preferences", "Realism & Reliablity...", NULL
699 puCallback optionsSubmenuCb [] = {
704 #ifdef FG_NETWORK_OLK
705 char *networkSubmenu [] = {
706 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
708 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
709 "Toggle Display", NULL
711 puCallback networkSubmenuCb [] = {
712 /* notCb, notCb, notCb, notCb, */
715 net_fgd_scan, NewCallSign,
716 net_display_toggle, NULL
720 char *helpSubmenu [] = {
721 /* "About...", */ "Help", NULL
723 puCallback helpSubmenuCb [] = {
724 /* notCb, */ helpCb, NULL
728 /* -------------------------------------------------------------------------
730 _____________________________________________________________________*/
735 char *mesa_win_state;
739 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
740 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
742 // Initialize our GLOBAL GUI STRINGS
743 gui_msg_OK = msg_OK; // "OK"
744 gui_msg_NO = msg_NO; // "NO"
745 gui_msg_YES = msg_YES; // "YES"
746 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
747 gui_msg_RESET = msg_RESET; // "RESET"
749 // Next check home directory
751 char* envp = ::getenv( "FG_FONTS" );
752 if ( envp != NULL ) {
755 fntpath.set( globals->get_fg_root() );
756 fntpath.append( "Fonts" );
759 // Install our fast fonts
760 fntpath.append( "typewriter.txf" );
761 guiFntHandle = new fntTexFont ;
762 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
763 puFont GuiFont ( guiFntHandle, 15 ) ;
764 puSetDefaultFonts( GuiFont, GuiFont ) ;
765 guiFnt = puGetDefaultLabelFont();
767 if (!fgHasValue("/sim/startup/mouse-pointer")) {
768 // no preference specified for mouse pointer, attempt to autodetect...
769 // Determine if we need to render the cursor, or if the windowing
770 // system will do it. First test if we are rendering with glide.
771 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
772 // Test for the MESA_GLX_FX env variable
773 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
774 // test if we are fullscreen mesa/glide
775 if ( (mesa_win_state[0] == 'f') ||
776 (mesa_win_state[0] == 'F') ) {
781 // mouse_active = ~mouse_active;
782 } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
783 // don't show pointer
785 // force showing pointer
787 // mouse_active = ~mouse_active;
790 // MOUSE_VIEW mode stuff
793 // Set up our Dialog Boxes
794 ConfirmExitDialogInit();
796 #ifdef FG_NETWORK_OLK
804 mainMenuBar = new puMenuBar ();
805 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
806 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
807 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
808 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
809 mainMenuBar -> add_submenu ("Autopilot", autopilotSubmenu, autopilotSubmenuCb);
810 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
811 #ifdef FG_NETWORK_OLK
812 if ( fgGetBool("/sim/networking/network-olk") ) {
813 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
816 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
817 mainMenuBar-> close ();
818 // Set up menu bar toggle