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>
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 FG_HAVE_NATIVE_SGI_COMPILERS
94 #if defined(WIN32) || defined(__CYGWIN32__)
95 #define WIN32_CURSOR_TWEAKS
96 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
97 #define X_CURSOR_TWEAKS
100 // main.cxx hack, should come from an include someplace
101 extern void fgInitVisuals( void );
102 extern void fgReshape( int width, int height );
103 extern void fgRenderFrame( void );
106 fntTexFont *guiFntHandle = 0;
108 static puMenuBar *mainMenuBar = 0;
109 //static puButton *hideMenuButton = 0;
111 static puDialogBox *dialogBox = 0;
112 static puFrame *dialogFrame = 0;
113 static puText *dialogBoxMessage = 0;
114 static puOneShot *dialogBoxOkButton = 0;
117 static puDialogBox *YNdialogBox = 0;
118 static puFrame *YNdialogFrame = 0;
119 static puText *YNdialogBoxMessage = 0;
120 static puOneShot *YNdialogBoxOkButton = 0;
121 static puOneShot *YNdialogBoxNoButton = 0;
123 static char msg_OK[] = "OK";
124 static char msg_NO[] = "NO";
125 static char msg_YES[] = "YES";
126 static char msg_CANCEL[] = "Cancel";
127 static char msg_RESET[] = "Reset";
129 char *gui_msg_OK; // "OK"
130 char *gui_msg_NO; // "NO"
131 char *gui_msg_YES; // "YES"
132 char *gui_msg_CANCEL; // "CANCEL"
133 char *gui_msg_RESET; // "RESET"
135 static char global_dialog_string[256];
138 extern void fgLatLonFormatToggle( puObject *);
141 /* ================ General Purpose Functions ================ */
143 // Intercept the Escape Key
144 void ConfirmExitDialog(void)
146 FG_PUSH_PUI_DIALOG( YNdialogBox );
149 // General Purpose Message Box
150 void mkDialog (const char *txt)
152 strncpy(global_dialog_string, txt, 256);
153 dialogBoxMessage->setLabel(global_dialog_string);
154 FG_PUSH_PUI_DIALOG( dialogBox );
157 // Toggle the Menu and Mouse display state
158 void guiToggleMenu(void)
161 // printf("Hiding Menu\n");
162 mainMenuBar->hide ();
163 #if defined(WIN32_CURSOR_TWEAKS)
164 if( mouse_mode == MOUSE_POINTER )
166 #endif // #ifdef WIN32_CURSOR_TWEAKS
168 // printf("Showing Menu\n");
169 mainMenuBar->reveal();
172 #endif // #ifdef WIN32
174 gui_menu_on = ~gui_menu_on;
177 /* -----------------------------------------------------------------------
178 the Gui callback functions
179 ____________________________________________________________________*/
182 // Hier Neu :-) This is my newly added code
183 // Added by David Findlay <nedz@bigpond.com>
184 // on Sunday 3rd of December
186 // Start new Save Dialog Box
187 static puDialogBox *SaveDialog = 0;
188 static puFrame *SaveDialogFrame = 0;
189 static puText *SaveDialogMessage = 0;
190 static puInput *SaveDialogInput = 0;
192 static puOneShot *SaveDialogOkButton = 0;
193 static puOneShot *SaveDialogCancelButton = 0;
194 static puOneShot *SaveDialogResetButton = 0;
196 // Default save filename
197 static char saveFile[256] = "fgfs.sav";
200 void SaveDialogCancel(puObject *) {
201 FG_POP_PUI_DIALOG( SaveDialog );
204 // If press OK do this
205 void SaveDialogOk(puObject*) {
207 FG_POP_PUI_DIALOG( SaveDialog );
210 SaveDialogInput->getValue(&s);
213 cout << saveFile << endl;
214 if (output.good() && fgSaveFlight(output)) {
216 mkDialog("Saved flight");
217 FG_LOG(FG_INPUT, FG_INFO, "Saved flight");
219 mkDialog("Cannot save flight");
220 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight");
225 static void saveFlight(puObject *cv) {
226 SaveDialog = new puDialogBox (150, 50);
228 SaveDialogFrame = new puFrame (0,0,350, 150);
229 SaveDialogMessage = new puText (
230 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
231 SaveDialogMessage -> setLabel ("File Name:");
233 SaveDialogInput = new puInput (50, 70, 300, 100);
234 SaveDialogInput -> setValue (saveFile);
235 SaveDialogInput -> acceptInput();
237 SaveDialogOkButton = new puOneShot (50, 10, 110, 50);
238 SaveDialogOkButton -> setLegend (gui_msg_OK);
239 SaveDialogOkButton -> setCallback ( SaveDialogOk );
240 SaveDialogOkButton -> makeReturnDefault(TRUE);
242 SaveDialogCancelButton = new puOneShot (140, 10, 210, 50);
243 SaveDialogCancelButton -> setLegend (gui_msg_CANCEL);
244 SaveDialogCancelButton -> setCallback ( SaveDialogCancel );
246 FG_FINALIZE_PUI_DIALOG( SaveDialog );
248 SaveDialog -> reveal();
252 static puDialogBox *LoadDialog = 0;
253 static puFrame *LoadDialogFrame = 0;
254 static puText *LoadDialogMessage = 0;
255 static puInput *LoadDialogInput = 0;
257 static puOneShot *LoadDialogOkButton = 0;
258 static puOneShot *LoadDialogCancelButton = 0;
259 static puOneShot *LoadDialogResetButton = 0;
261 // Default load filename
262 static char loadFile[256] = "fgfs.sav";
264 // Do this if the person click okay
265 void LoadDialogOk(puObject *) {
267 FG_POP_PUI_DIALOG( LoadDialog );
270 LoadDialogInput->getValue(&l);
273 if (input.good() && fgLoadFlight(input)) {
275 mkDialog("Loaded flight");
276 FG_LOG(FG_INPUT, FG_INFO, "Restored flight");
278 mkDialog("Failed to load flight");
279 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight");
283 // Do this is the person presses cancel
284 void LoadDialogCancel(puObject *) {
285 FG_POP_PUI_DIALOG( LoadDialog );
288 // Create Load Dialog
289 static void loadFlight(puObject *cb)
291 LoadDialog = new puDialogBox (150, 50);
293 LoadDialogFrame = new puFrame (0,0,350, 150);
294 LoadDialogMessage = new puText (
295 (150 - puGetStringWidth( puGetDefaultLabelFont(), "File Name:" ) / 2), 110);
296 LoadDialogMessage -> setLabel ("File Name:");
298 LoadDialogInput = new puInput (50, 70, 300, 100);
299 LoadDialogInput -> setValue (loadFile);
300 LoadDialogInput -> acceptInput();
302 LoadDialogOkButton = new puOneShot (50, 10, 110, 50);
303 LoadDialogOkButton -> setLegend (gui_msg_OK);
304 LoadDialogOkButton -> setCallback ( LoadDialogOk );
305 LoadDialogOkButton -> makeReturnDefault(TRUE);
307 LoadDialogCancelButton = new puOneShot (140, 10, 210, 50);
308 LoadDialogCancelButton -> setLegend (gui_msg_CANCEL);
309 LoadDialogCancelButton -> setCallback ( LoadDialogCancel );
311 FG_FINALIZE_PUI_DIALOG( LoadDialog );
313 LoadDialog -> reveal();
316 // This is the accessor function
317 void guiTogglePanel(puObject *cb)
319 if (fgGetBool("/sim/panel/visibility"))
320 fgSetBool("/sim/panel/visibility", false);
322 fgSetBool("/sim/panel/visibility", true);
324 fgReshape(fgGetInt("/sim/startup/xsize"),
325 fgGetInt("/sim/startup/ysize"));
328 //void MenuHideMenuCb(puObject *cb)
329 void hideMenuCb (puObject *cb)
334 void goodBye(puObject *)
336 // FG_LOG( FG_INPUT, FG_ALERT,
337 // "Program exiting normally at user request." );
338 cout << "Program exiting normally at user request." << endl;
340 #ifdef FG_NETWORK_OLK
341 if ( fgGetBool("/sim/networking/network-olk") ) {
342 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
346 // close all external I/O connections
353 void goAwayCb (puObject *me)
355 FG_POP_PUI_DIALOG( dialogBox );
358 void mkDialogInit (void)
360 // printf("mkDialogInit\n");
361 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
362 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
363 dialogBox = new puDialogBox (x, y); // 150, 50
365 dialogFrame = new puFrame (0,0,400,100);
366 dialogBoxMessage = new puText (10, 70);
367 dialogBoxMessage -> setLabel ("");
368 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
369 dialogBoxOkButton -> setLegend (gui_msg_OK);
370 dialogBoxOkButton -> makeReturnDefault (TRUE );
371 dialogBoxOkButton -> setCallback (goAwayCb);
373 FG_FINALIZE_PUI_DIALOG( dialogBox );
376 void MayBeGoodBye(puObject *)
381 void goAwayYesNoCb(puObject *me)
383 FG_POP_PUI_DIALOG( YNdialogBox);
386 void ConfirmExitDialogInit(void)
388 char msg[] = "Really Quit";
391 // printf("ConfirmExitDialogInit\n");
392 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
394 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
395 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
397 YNdialogBox = new puDialogBox (x, y); // 150, 50
398 // YNdialogBox = new puDialogBox (150, 50);
400 YNdialogFrame = new puFrame (0,0,400, 100);
402 YNdialogBoxMessage = new puText (len, 70);
403 YNdialogBoxMessage -> setDefaultValue (msg);
404 YNdialogBoxMessage -> getDefaultValue (&s);
405 YNdialogBoxMessage -> setLabel (s);
407 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
408 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
409 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
410 YNdialogBoxOkButton -> setCallback (goodBye);
412 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
413 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
414 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
416 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
419 void notCb (puObject *)
421 mkDialog ("This function isn't implemented yet");
424 void helpCb (puObject *)
428 #if defined(FX) && !defined(WIN32)
429 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
430 if ( global_fullscreen ) {
431 global_fullscreen = false;
432 XMesaSetFXmode( XMESA_FX_WINDOW );
438 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
440 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
441 command = "netscape -remote \"openURL(" + url + ")\" &";
443 command = "netscape " + url + " &";
446 command = "webrun.bat";
449 system( command.c_str() );
450 //string text = "Help started in netscape window.";
452 //mkDialog (text.c_str());
453 mkDialog ("Help started in netscape window.");
456 #if defined( WIN32 ) && !defined( __CYGWIN__)
458 static void rotateView( double roll, double pitch, double yaw )
463 static GlBitmap *b1 = NULL;
464 extern FGInterface cur_view_fdm;
465 GLubyte *hiResScreenCapture( int multiplier )
467 float oldfov = fgGetDouble("/sim/field-of-view");
468 float fov = oldfov / multiplier;
469 FGViewer *v = globals->get_current_view();
470 fgSetDouble("/sim/field-of-view", fov);
472 int cur_width = fgGetInt("/sim/startup/xsize");
473 int cur_height = fgGetInt("/sim/startup/ysize");
474 if (b1) delete( b1 );
475 // New empty (mostly) bitmap
476 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
478 for ( y = 0; y < multiplier; y++ ) {
479 for ( x = 0; x < multiplier; x++ ) {
480 fgReshape( cur_width, cur_height );
482 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
486 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
489 fgSetDouble("/sim/field-of-view", oldfov);
490 return b1->getBitmap();
495 #if defined( WIN32 ) && !defined( __CYGWIN__)
496 // win32 print screen function
497 void printScreen ( puObject *obj ) {
498 bool show_pu_cursor = false;
500 if ( !puCursorIsHidden() ) {
501 show_pu_cursor = true;
507 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
508 int cur_width = fgGetInt("/sim/startup/xsize");
509 int cur_height = fgGetInt("/sim/startup/ysize");
510 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
511 p.End( hiResScreenCapture(3) );
514 mainMenuBar->reveal();
517 if ( show_pu_cursor ) {
522 #endif // #ifdef WIN32
525 void dumpSnapShot ( puObject *obj ) {
530 // do a screen snap shot
531 void fgDumpSnapShot () {
532 bool show_pu_cursor = false;
534 int freeze = globals->get_freeze();
536 globals->set_freeze( true );
540 if ( !puCursorIsHidden() ) {
541 show_pu_cursor = true;
546 fgReshape( fgGetInt("/sim/startup/xsize"),
547 fgGetInt("/sim/startup/ysize") );
549 // we need two render frames here to clear the menu and cursor
550 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
555 my_glDumpWindow( "fgfs-screen.ppm",
556 fgGetInt("/sim/startup/xsize"),
557 fgGetInt("/sim/startup/ysize") );
559 mkDialog ("Snap shot saved to fgfs-screen.ppm");
561 if ( show_pu_cursor ) {
567 mainMenuBar->reveal();
571 globals->set_freeze( false );
574 #ifdef FG_NETWORK_OLK
575 static void net_display_toggle( puObject *cb)
577 net_hud_display = (net_hud_display) ? 0 : 1;
578 printf("Toggle net_hud_display : %d\n", net_hud_display);
581 static void net_register( puObject *cb)
583 fgd_send_com( "1", FGFS_host );
584 net_is_registered = 0;
585 printf("Registering to deamon\n");
588 static void net_unregister( puObject *cb)
590 fgd_send_com( "8", FGFS_host );
591 net_is_registered = -1;
592 printf("Unregistering from deamon\n");
595 extern void net_fgd_scan(puObject *cb);
596 #endif // #ifdef FG_NETWORK_OLK
598 /* -----------------------------------------------------------------------
600 ---------------------------------------------------------------------*/
601 char *fileSubmenu [] = {
602 "Exit", /* "Close", "---------", */
603 #if defined( WIN32 ) && !defined( __CYGWIN__)
613 puCallback fileSubmenuCb [] = {
614 MayBeGoodBye, /* hideMenuCb, NULL, */
615 #if defined( WIN32 ) && !defined( __CYGWIN__)
628 char *editSubmenu [] = {
631 puCallback editSubmenuCb [] = {
636 extern void fgHUDalphaAdjust( puObject * );
637 char *viewSubmenu [] = {
640 /* "Cockpit View > ", "View >","------------", */
641 "Toggle Panel...", NULL
643 puCallback viewSubmenuCb [] = {
646 /* notCb, notCb, NULL, */
652 char *autopilotSubmenu [] = {
653 "Toggle HUD Format", "Adjust AP Settings",
655 "Clear Route", "Skip Current Waypoint", "Add Waypoint",
657 "Set Altitude", "Set Heading",
661 puCallback autopilotSubmenuCb [] = {
662 fgLatLonFormatToggle, fgAPAdjust,
664 ClearRoute, PopWayPoint, AddWayPoint,
666 NewAltitude, NewHeading,
670 char *environmentSubmenu [] = {
671 "Goto Airport", /* "Terrain", "Weather", */ NULL
673 puCallback environmentSubmenuCb [] = {
674 NewAirport, /* notCb, notCb, */ NULL
678 char *optionsSubmenu [] = {
679 "Preferences", "Realism & Reliablity...", NULL
681 puCallback optionsSubmenuCb [] = {
686 #ifdef FG_NETWORK_OLK
687 char *networkSubmenu [] = {
688 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
690 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
691 "Toggle Display", NULL
693 puCallback networkSubmenuCb [] = {
694 /* notCb, notCb, notCb, notCb, */
697 net_fgd_scan, NewCallSign,
698 net_display_toggle, NULL
702 char *helpSubmenu [] = {
703 /* "About...", */ "Help", NULL
705 puCallback helpSubmenuCb [] = {
706 /* notCb, */ helpCb, NULL
710 /* -------------------------------------------------------------------------
712 _____________________________________________________________________*/
717 char *mesa_win_state;
721 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
722 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
724 // Initialize our GLOBAL GUI STRINGS
725 gui_msg_OK = msg_OK; // "OK"
726 gui_msg_NO = msg_NO; // "NO"
727 gui_msg_YES = msg_YES; // "YES"
728 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
729 gui_msg_RESET = msg_RESET; // "RESET"
731 // Next check home directory
733 char* envp = ::getenv( "FG_FONTS" );
734 if ( envp != NULL ) {
737 fntpath.set( globals->get_fg_root() );
738 fntpath.append( "Fonts" );
741 // Install our fast fonts
742 fntpath.append( "typewriter.txf" );
743 guiFntHandle = new fntTexFont ;
744 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
745 puFont GuiFont ( guiFntHandle, 15 ) ;
746 puSetDefaultFonts( GuiFont, GuiFont ) ;
747 guiFnt = puGetDefaultLabelFont();
749 if (!fgHasValue("/sim/startup/mouse-pointer")) {
750 // no preference specified for mouse pointer, attempt to autodetect...
751 // Determine if we need to render the cursor, or if the windowing
752 // system will do it. First test if we are rendering with glide.
753 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
754 // Test for the MESA_GLX_FX env variable
755 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
756 // test if we are fullscreen mesa/glide
757 if ( (mesa_win_state[0] == 'f') ||
758 (mesa_win_state[0] == 'F') ) {
763 // mouse_active = ~mouse_active;
764 } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
765 // don't show pointer
767 // force showing pointer
769 // mouse_active = ~mouse_active;
772 // MOUSE_VIEW mode stuff
775 // Set up our Dialog Boxes
776 ConfirmExitDialogInit();
778 #ifdef FG_NETWORK_OLK
786 mainMenuBar = new puMenuBar ();
787 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
788 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
789 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
790 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
791 mainMenuBar -> add_submenu ("Autopilot", autopilotSubmenu, autopilotSubmenuCb);
792 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
793 #ifdef FG_NETWORK_OLK
794 if ( fgGetBool("/sim/networking/network-olk") ) {
795 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
798 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
799 mainMenuBar-> close ();
800 // Set up menu bar toggle