1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **************************************************************************/
29 #include <Include/compiler.h>
31 #ifdef FG_MATH_EXCEPTION_CLASH
42 #if defined(FX) && defined(XMESA)
43 # include <GL/xmesa.h>
51 #include <Include/general.hxx>
52 #include <Debug/logstream.hxx>
53 #include <Aircraft/aircraft.hxx>
54 #include <Airports/simple.hxx>
55 #include <Cockpit/panel.hxx>
56 #include <FDM/flight.hxx>
57 #include <Main/options.hxx>
58 #include <Main/fg_init.hxx>
59 #include <Main/views.hxx>
60 #include <Misc/fgpath.hxx>
61 #include <Time/fg_time.hxx>
68 fntTexFont *guiFntHandle = 0;
70 static puMenuBar *mainMenuBar = 0;
71 //static puButton *hideMenuButton = 0;
73 static puDialogBox *dialogBox = 0;
74 static puFrame *dialogFrame = 0;
75 static puText *dialogBoxMessage = 0;
76 static puOneShot *dialogBoxOkButton = 0;
79 static puDialogBox *YNdialogBox = 0;
80 static puFrame *YNdialogFrame = 0;
81 static puText *YNdialogBoxMessage = 0;
82 static puOneShot *YNdialogBoxOkButton = 0;
83 static puOneShot *YNdialogBoxNoButton = 0;
85 static char msg_OK[] = "OK";
86 static char msg_NO[] = "NO";
87 static char msg_YES[] = "YES";
88 static char msg_CANCEL[] = "Cancel";
89 static char msg_RESET[] = "Reset";
91 char *gui_msg_OK; // "OK"
92 char *gui_msg_NO; // "NO"
93 char *gui_msg_YES; // "YES"
94 char *gui_msg_CANCEL; // "CANCEL"
95 char *gui_msg_RESET; // "RESET"
97 static char global_dialog_string[256];
99 extern void NewAltitude( puObject *cb );
100 extern void NewHeading( puObject *cb );
101 extern void fgAPAdjust( puObject * );
102 extern void fgLatLonFormatToggle( puObject *);
104 /* --------------------------------------------------------------------
106 ---------------------------------------------------------------------*/
110 static int last_buttons = 0 ;
111 static int mouse_active = 0;
112 static int menu_on = 0;
114 void guiMotionFunc ( int x, int y )
119 glutPostRedisplay () ;
122 void guiMouseFunc(int button, int updown, int x, int y)
126 if ( updown == PU_DOWN )
127 last_buttons |= ( 1 << button ) ;
129 last_buttons &= ~( 1 << button ) ;
130 puMouse (button, updown, x,y);
131 glutPostRedisplay ();
134 int guiGetMouseButton(void)
139 void guiGetMouse(int *x, int *y)
145 static inline void TurnCursorOn( void )
148 #if defined ( WIN32 ) || defined(__CYGWIN32__)
149 glutSetCursor(GLUT_CURSOR_INHERIT);
151 #if (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
152 glutWarpPointer( glutGet(GLUT_SCREEN_WIDTH)/2, glutGet(GLUT_SCREEN_HEIGHT)/2);
156 static inline void TurnCursorOff( void )
159 #if defined ( WIN32 ) || defined(__CYGWIN32__)
160 glutSetCursor(GLUT_CURSOR_NONE);
161 #else // I guess this is what we want to do ??
162 #if (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
163 glutWarpPointer( glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
168 void maybeToggleMouse( void )
171 static int first_time = ~0;
172 static int mouse_changed = 0;
176 mouse_changed = ~mouse_changed;
180 if( mouse_changed ) {
181 mouse_changed = ~mouse_changed;
187 first_time = ~first_time;
188 #endif // #ifdef WIN32
191 // Call with FALSE to init and TRUE to restore
192 void BusyCursor( int restore )
194 static int cursor = 0;
196 glutSetCursor(cursor);
198 cursor = glutGet( GLUT_WINDOW_CURSOR );
202 glutSetCursor( GLUT_CURSOR_WAIT );
205 /* ================ General Purpose Functions ================ */
207 // Intercept the Escape Key
208 void ConfirmExitDialog(void)
210 FG_PUSH_PUI_DIALOG( YNdialogBox );
213 // General Purpose Message Box
214 void mkDialog (const char *txt)
216 strncpy(global_dialog_string, txt, 256);
217 dialogBoxMessage->setLabel(global_dialog_string);
218 FG_PUSH_PUI_DIALOG( dialogBox );
221 // Repair any damage done to the Panel by other Gui Items
222 void guiFixPanel( void )
226 if ( current_options.get_panel_status() ) {
227 FGView *v = ¤t_view;
228 FGTime *t = FGTime::cur_time_params;
230 if( (toggle_pause = !t->getPause()) )
231 t->togglePauseMode();
233 // this seems to be the only way to do this :-(
234 // problem is the viewport has been mucked with
235 xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) );
236 FGPanel::OurPanel->ReInit(0, 0, 1024, 768);
239 t->togglePauseMode();
243 // Toggle the Menu and Mouse display state
244 void guiToggleMenu(void)
247 // printf("Hiding Menu\n");
248 mainMenuBar->hide ();
251 #endif // #ifdef WIN32
253 // printf("Showing Menu\n");
254 mainMenuBar->reveal();
257 #endif // #ifdef WIN32
262 /* -----------------------------------------------------------------------
263 the Gui callback functions
264 ____________________________________________________________________*/
266 void reInit(puObject *cb)
269 fgReInitSubsystems();
273 // This is the accessor function
274 void guiTogglePanel(puObject *cb)
276 current_options.toggle_panel();
279 //void MenuHideMenuCb(puObject *cb)
280 void hideMenuCb (puObject *cb)
285 void goodBye(puObject *)
287 // FG_LOG( FG_INPUT, FG_ALERT,
288 // "Program exiting normally at user request." );
289 cout << "Program exiting normally at user request." << endl;
298 void goAwayCb (puObject *me)
300 FG_POP_PUI_DIALOG( dialogBox );
303 void mkDialogInit (void)
305 // printf("mkDialogInit\n");
306 int x = (current_options.get_xsize()/2 - 400/2);
307 int y = (current_options.get_ysize()/2 - 100/2);
308 dialogBox = new puDialogBox (x, y); // 150, 50
310 dialogFrame = new puFrame (0,0,400,100);
311 dialogBoxMessage = new puText (10, 70);
312 dialogBoxMessage -> setLabel ("");
313 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
314 dialogBoxOkButton -> setLegend (gui_msg_OK);
315 dialogBoxOkButton -> makeReturnDefault (TRUE );
316 dialogBoxOkButton -> setCallback (goAwayCb);
318 FG_FINALIZE_PUI_DIALOG( dialogBox );
321 void MayBeGoodBye(puObject *)
326 void goAwayYesNoCb(puObject *me)
328 FG_POP_PUI_DIALOG( YNdialogBox);
331 void ConfirmExitDialogInit(void)
333 char msg[] = "Really Quit";
336 // printf("ConfirmExitDialogInit\n");
337 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
339 int x = (current_options.get_xsize()/2 - 400/2);
340 int y = (current_options.get_ysize()/2 - 100/2);
342 YNdialogBox = new puDialogBox (x, y); // 150, 50
343 // YNdialogBox = new puDialogBox (150, 50);
345 YNdialogFrame = new puFrame (0,0,400, 100);
347 YNdialogBoxMessage = new puText (len, 70);
348 YNdialogBoxMessage -> setDefaultValue (msg);
349 YNdialogBoxMessage -> getDefaultValue (&s);
350 YNdialogBoxMessage -> setLabel (s);
352 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
353 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
354 YNdialogBoxOkButton -> setCallback (goodBye);
356 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
357 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
358 // YNdialogBoxNoButton -> makeReturnDefault (TRUE );
359 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
361 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
364 void notCb (puObject *)
366 mkDialog ("This function isn't implemented yet");
369 void helpCb (puObject *)
373 #if defined(FX) && !defined(WIN32)
374 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
375 if ( global_fullscreen ) {
376 global_fullscreen = false;
377 XMesaSetFXmode( XMESA_FX_WINDOW );
383 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
385 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
386 command = "netscape -remote \"openURL(" + url + ")\" &";
388 command = "netscape " + url + " &";
391 command = "webrun.bat";
394 system( command.c_str() );
395 //string text = "Help started in netscape window.";
397 //mkDialog (text.c_str());
398 mkDialog ("Help started in netscape window.");
401 /// The beginnings of teleportation :-)
402 // Needs cleaning up but works
403 // These statics should disapear when this is a class
404 static puDialogBox *AptDialog = 0;
405 static puFrame *AptDialogFrame = 0;
406 static puText *AptDialogMessage = 0;
407 static puInput *AptDialogInput = 0;
409 static char NewAirportId[16];
410 static char NewAirportLabel[] = "Enter New Airport ID";
412 static puOneShot *AptDialogOkButton = 0;
413 static puOneShot *AptDialogCancelButton = 0;
414 static puOneShot *AptDialogResetButton = 0;
416 void AptDialog_Cancel(puObject *)
418 FG_POP_PUI_DIALOG( AptDialog );
421 void AptDialog_OK (puObject *)
425 FGTime *t = FGTime::cur_time_params;
426 int PauseMode = t->getPause();
428 t->togglePauseMode();
431 AptDialogInput->getValue(&s);
434 AptDialog_Cancel( NULL );
436 if ( AptId.length() ) {
437 // set initial position from airport id
441 FG_LOG( FG_GENERAL, FG_INFO,
442 "Attempting to set starting position from airport code "
445 airports.load("apt_simple");
446 if ( airports.search( AptId, &a ) )
448 current_options.set_airport_id( AptId.c_str() );
450 fgReInitSubsystems();
453 AptId += " not in database.";
454 mkDialog(AptId.c_str());
457 if( PauseMode != t->getPause() )
458 t->togglePauseMode();
461 void AptDialog_Reset(puObject *)
463 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
464 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
465 AptDialogInput->setValue ( NewAirportId );
466 AptDialogInput->setCursor( 0 ) ;
469 void NewAirport(puObject *cb)
471 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
472 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
473 AptDialogInput->setValue( NewAirportId );
475 FG_PUSH_PUI_DIALOG( AptDialog );
478 static void NewAirportInit(void)
480 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
481 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
482 NewAirportLabel ) / 2;
484 AptDialog = new puDialogBox (150, 50);
486 AptDialogFrame = new puFrame (0,0,350, 150);
487 AptDialogMessage = new puText (len, 110);
488 AptDialogMessage -> setLabel (NewAirportLabel);
490 AptDialogInput = new puInput (50, 70, 300, 100);
491 AptDialogInput -> setValue (NewAirportId);
492 AptDialogInput -> acceptInput();
494 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
495 AptDialogOkButton -> setLegend (gui_msg_OK);
496 AptDialogOkButton -> setCallback (AptDialog_OK);
497 AptDialogOkButton -> makeReturnDefault(TRUE);
499 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
500 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
501 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
503 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
504 AptDialogResetButton -> setLegend (gui_msg_RESET);
505 AptDialogResetButton -> setCallback (AptDialog_Reset);
507 FG_FINALIZE_PUI_DIALOG( AptDialog );
511 /* -----------------------------------------------------------------------
513 ---------------------------------------------------------------------*/
514 char *fileSubmenu [] = {
515 "Exit", "Close", "---------", "Print", "---------", "Save", "Reset", NULL
517 puCallback fileSubmenuCb [] = {
518 MayBeGoodBye, hideMenuCb, NULL, notCb, NULL, notCb, reInit, NULL
521 char *editSubmenu [] = {
524 puCallback editSubmenuCb [] = {
528 char *viewSubmenu [] = {
529 "Cockpit View > ", "View >","------------", "Toggle Panel...", NULL
531 puCallback viewSubmenuCb [] = {
532 notCb, notCb, NULL, guiTogglePanel, NULL
535 char *aircraftSubmenu [] = {
536 "Autopilot", "Heading", "Altitude", "Navigation", "Communication", NULL
538 puCallback aircraftSubmenuCb [] = {
539 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, notCb, NULL
542 char *environmentSubmenu [] = {
543 "Airport", "Terrain", "Weather", NULL
545 puCallback environmentSubmenuCb [] = {
546 NewAirport, notCb, notCb, NULL
549 char *optionsSubmenu [] = {
550 "Preferences", "Realism & Reliablity...", NULL
552 puCallback optionsSubmenuCb [] = {
556 char *helpSubmenu [] = {
557 "About...", "Help", NULL
559 puCallback helpSubmenuCb [] = {
564 /* -------------------------------------------------------------------------
566 _____________________________________________________________________*/
571 char *mesa_win_state;
575 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
576 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
578 // Initialize our GLOBAL GUI STRINGS
579 gui_msg_OK = msg_OK; // "OK"
580 gui_msg_NO = msg_NO; // "NO"
581 gui_msg_YES = msg_YES; // "YES"
582 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
583 gui_msg_RESET = msg_RESET; // "RESET"
585 // Next check home directory
587 char* envp = ::getenv( "FG_FONTS" );
588 if ( envp != NULL ) {
591 fntpath.set( current_options.get_fg_root() );
592 fntpath.append( "Fonts" );
595 // Install our fast fonts
596 fntpath.append( "typewriter.txf" );
597 guiFntHandle = new fntTexFont ;
598 guiFntHandle -> load ( fntpath.c_str() ) ;
599 puFont GuiFont ( guiFntHandle, 15 ) ;
600 puSetDefaultFonts( GuiFont, GuiFont ) ;
601 guiFnt = puGetDefaultLabelFont();
603 if ( current_options.get_mouse_pointer() == 0 ) {
604 // no preference specified for mouse pointer, attempt to autodetect...
605 // Determine if we need to render the cursor, or if the windowing
606 // system will do it. First test if we are rendering with glide.
607 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
608 // Test for the MESA_GLX_FX env variable
609 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
610 // test if we are fullscreen mesa/glide
611 if ( (mesa_win_state[0] == 'f') ||
612 (mesa_win_state[0] == 'F') ) {
617 mouse_active = ~mouse_active;
618 } else if ( current_options.get_mouse_pointer() == 1 ) {
619 // don't show pointer
620 } else if ( current_options.get_mouse_pointer() == 2 ) {
621 // force showing pointer
623 mouse_active = ~mouse_active;
626 // Set up our Dialog Boxes
627 ConfirmExitDialogInit();
632 mainMenuBar = new puMenuBar ();
633 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
634 mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
635 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
636 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
637 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
638 mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
639 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
640 mainMenuBar-> close ();
641 // Set up menu bar toggle