1 /**************************************************************************
4 * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
6 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
9 * Additional mouse supported added by David Megginson, 1999.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 **************************************************************************/
33 #include <simgear/compiler.h>
35 #ifdef SG_MATH_EXCEPTION_CLASH
46 #if defined(FX) && defined(XMESA)
47 # include <GL/xmesa.h>
56 #include <simgear/constants.h>
57 #include <simgear/debug/logstream.hxx>
58 #include <simgear/misc/sg_path.hxx>
59 #include <simgear/screen/screen-dump.hxx>
61 #include <Include/general.hxx>
62 #include <Aircraft/aircraft.hxx>
63 #include <Airports/simple.hxx>
64 #include <Autopilot/auto_gui.hxx>
65 #include <Autopilot/newauto.hxx>
66 #include <Cockpit/panel.hxx>
67 #include <Controls/controls.hxx>
68 #include <FDM/flight.hxx>
69 #include <Main/fg_init.hxx>
70 #include <Main/fg_io.hxx>
71 #include <Main/globals.hxx>
72 #include <Main/fg_props.hxx>
73 #include <Main/viewmgr.hxx>
76 #include <NetworkOLK/network.h>
79 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
80 # include <simgear/screen/win32-printer.h>
81 # include <simgear/screen/GlBitmaps.h>
85 #include "gui_local.hxx"
86 #include "apt_dlg.hxx"
87 #include "net_dlg.hxx"
88 #include "sgVec3Slider.hxx"
89 #include "prop_picker.hxx"
93 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
97 // main.cxx hack, should come from an include someplace
98 extern void fgInitVisuals( void );
99 extern void fgReshape( int width, int height );
100 extern void fgRenderFrame( void );
102 extern FGInterface cur_view_fdm;
103 extern void fgHUDalphaAdjust( puObject * );
106 extern void fgLatLonFormatToggle( puObject *);
108 #ifdef FG_NETWORK_OLK
109 extern void net_fgd_scan(puObject *cb);
110 #endif // #ifdef FG_NETWORK_OLK
112 #define TR_HIRES_SNAP 1
114 #if defined( TR_HIRES_SNAP)
115 #include <simgear/screen/tr.h>
116 extern void trRenderFrame( void );
117 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
118 GLfloat x_end, GLfloat y_end );
121 extern puMenuBar *mainMenuBar;
123 puDialogBox *dialogBox = 0;
124 puFrame *dialogFrame = 0;
125 puText *dialogBoxMessage = 0;
126 puOneShot *dialogBoxOkButton = 0;
128 puDialogBox *YNdialogBox = 0;
129 puFrame *YNdialogFrame = 0;
130 puText *YNdialogBoxMessage = 0;
131 puOneShot *YNdialogBoxOkButton = 0;
132 puOneShot *YNdialogBoxNoButton = 0;
134 char *gui_msg_OK; // "OK"
135 char *gui_msg_NO; // "NO"
136 char *gui_msg_YES; // "YES"
137 char *gui_msg_CANCEL; // "CANCEL"
138 char *gui_msg_RESET; // "RESET"
140 char msg_OK[] = "OK";
141 char msg_NO[] = "NO";
142 char msg_YES[] = "YES";
143 char msg_CANCEL[] = "Cancel";
144 char msg_RESET[] = "Reset";
146 char global_dialog_string[256];
149 /* ================ General Purpose Functions ================ */
151 void initDialog(void) {
152 // Initialize our GLOBAL GUI STRINGS
153 gui_msg_OK = msg_OK; // "OK"
154 gui_msg_NO = msg_NO; // "NO"
155 gui_msg_YES = msg_YES; // "YES"
156 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
157 gui_msg_RESET = msg_RESET; // "RESET"
160 // General Purpose Message Box
161 void mkDialog (const char *txt)
163 strncpy(global_dialog_string, txt, 256);
164 dialogBoxMessage->setLabel(global_dialog_string);
165 FG_PUSH_PUI_DIALOG( dialogBox );
168 // Message Box to report an error.
169 void guiErrorMessage (const char *txt)
171 SG_LOG(SG_GENERAL, SG_ALERT, txt);
176 // Message Box to report a throwable (usually an exception).
177 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
181 msg += throwable.getFormattedMessage();
182 if (!throwable.getOrigin().empty()) {
183 msg += "\n (reported by ";
184 msg += throwable.getOrigin();
187 SG_LOG(SG_GENERAL, SG_ALERT, msg);
189 mkDialog(msg.c_str());
192 // Toggle the Menu and Mouse display state
193 void guiToggleMenu(void)
196 // printf("Hiding Menu\n");
197 mainMenuBar->hide ();
198 #if defined(WIN32_CURSOR_TWEAKS_OFF)
199 if( mouse_mode == MOUSE_POINTER )
201 #endif // WIN32_CURSOR_TWEAKS_OFF
203 // printf("Showing Menu\n");
204 mainMenuBar->reveal();
209 gui_menu_on = ~gui_menu_on;
212 // Intercept the Escape Key
213 void ConfirmExitDialog(void)
215 FG_PUSH_PUI_DIALOG( YNdialogBox );
220 /* -----------------------------------------------------------------------
221 the Gui callback functions
222 ____________________________________________________________________*/
225 // Hier Neu :-) This is my newly added code
226 // Added by David Findlay <nedz@bigpond.com>
227 // on Sunday 3rd of December
229 // Start new Save Dialog Box
230 puDialogBox *SaveDialog = 0;
231 puFrame *SaveDialogFrame = 0;
232 puText *SaveDialogMessage = 0;
233 puInput *SaveDialogInput = 0;
235 puOneShot *SaveDialogOkButton = 0;
236 puOneShot *SaveDialogCancelButton = 0;
237 // static puOneShot *SaveDialogResetButton = 0;
239 // Default save filename
240 char saveFile[256] = "fgfs.sav";
243 void SaveDialogCancel(puObject *) {
244 FG_POP_PUI_DIALOG( SaveDialog );
247 // If press OK do this
248 void SaveDialogOk(puObject*) {
250 FG_POP_PUI_DIALOG( SaveDialog );
253 SaveDialogInput->getValue(&s);
256 cout << saveFile << endl;
257 if (output.good() && fgSaveFlight(output)) {
259 mkDialog("Saved flight");
260 SG_LOG(SG_INPUT, SG_INFO, "Saved flight");
262 mkDialog("Cannot save flight");
263 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight");
268 void saveFlight(puObject *cv) {
269 SaveDialog = new puDialogBox (150, 50);
271 SaveDialogFrame = new puFrame (0,0,350, 150);
273 = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
274 SaveDialogMessage -> setLabel ("File Name:");
276 SaveDialogInput = new puInput (50, 70, 300, 100);
277 SaveDialogInput -> setValue (saveFile);
278 SaveDialogInput -> acceptInput();
280 SaveDialogOkButton = new puOneShot (50, 10, 110, 50);
281 SaveDialogOkButton -> setLegend (gui_msg_OK);
282 SaveDialogOkButton -> setCallback ( SaveDialogOk );
283 SaveDialogOkButton -> makeReturnDefault(TRUE);
285 SaveDialogCancelButton = new puOneShot (140, 10, 210, 50);
286 SaveDialogCancelButton -> setLegend (gui_msg_CANCEL);
287 SaveDialogCancelButton -> setCallback ( SaveDialogCancel );
289 FG_FINALIZE_PUI_DIALOG( SaveDialog );
291 SaveDialog -> reveal();
295 puDialogBox *LoadDialog = 0;
296 puFrame *LoadDialogFrame = 0;
297 puText *LoadDialogMessage = 0;
298 puInput *LoadDialogInput = 0;
300 puOneShot *LoadDialogOkButton = 0;
301 puOneShot *LoadDialogCancelButton = 0;
302 // static puOneShot *LoadDialogResetButton = 0;
304 // Default load filename
305 char loadFile[256] = "fgfs.sav";
307 // Do this if the person click okay
308 void LoadDialogOk(puObject *) {
310 FG_POP_PUI_DIALOG( LoadDialog );
313 LoadDialogInput->getValue(&l);
316 if (input.good() && fgLoadFlight(input)) {
318 mkDialog("Loaded flight");
319 SG_LOG(SG_INPUT, SG_INFO, "Restored flight");
321 mkDialog("Failed to load flight");
322 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight");
326 // Do this if the person presses cancel
327 void LoadDialogCancel(puObject *) {
328 FG_POP_PUI_DIALOG( LoadDialog );
331 // Create Load Dialog
332 void loadFlight(puObject *cb)
334 LoadDialog = new puDialogBox (150, 50);
336 LoadDialogFrame = new puFrame (0,0,350, 150);
338 = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
339 LoadDialogMessage -> setLabel ("File Name:");
341 LoadDialogInput = new puInput (50, 70, 300, 100);
342 LoadDialogInput -> setValue (loadFile);
343 LoadDialogInput -> acceptInput();
345 LoadDialogOkButton = new puOneShot (50, 10, 110, 50);
346 LoadDialogOkButton -> setLegend (gui_msg_OK);
347 LoadDialogOkButton -> setCallback ( LoadDialogOk );
348 LoadDialogOkButton -> makeReturnDefault(TRUE);
350 LoadDialogCancelButton = new puOneShot (140, 10, 210, 50);
351 LoadDialogCancelButton -> setLegend (gui_msg_CANCEL);
352 LoadDialogCancelButton -> setCallback ( LoadDialogCancel );
354 FG_FINALIZE_PUI_DIALOG( LoadDialog );
356 LoadDialog -> reveal();
359 // This is the accessor function
360 void guiTogglePanel(puObject *cb)
362 if (fgGetBool("/sim/panel/visibility"))
363 fgSetBool("/sim/panel/visibility", false);
365 fgSetBool("/sim/panel/visibility", true);
367 fgReshape(fgGetInt("/sim/startup/xsize"),
368 fgGetInt("/sim/startup/ysize"));
371 //void MenuHideMenuCb(puObject *cb)
372 void hideMenuCb (puObject *cb)
377 void goodBye(puObject *)
379 // SG_LOG( SG_INPUT, SG_ALERT,
380 // "Program exiting normally at user request." );
381 cout << "Program exiting normally at user request." << endl;
383 #ifdef FG_NETWORK_OLK
384 if ( fgGetBool("/sim/networking/network-olk") ) {
385 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
389 // close all external I/O connections
396 void goAwayCb (puObject *me)
398 FG_POP_PUI_DIALOG( dialogBox );
401 void mkDialogInit (void)
403 // printf("mkDialogInit\n");
404 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
405 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
406 dialogBox = new puDialogBox (x, y); // 150, 50
408 dialogFrame = new puFrame (0,0,400,100);
409 dialogBoxMessage = new puText (10, 70);
410 dialogBoxMessage -> setLabel ("");
411 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
412 dialogBoxOkButton -> setLegend (gui_msg_OK);
413 dialogBoxOkButton -> makeReturnDefault (TRUE );
414 dialogBoxOkButton -> setCallback (goAwayCb);
416 FG_FINALIZE_PUI_DIALOG( dialogBox );
419 void MayBeGoodBye(puObject *)
424 void goAwayYesNoCb(puObject *me)
426 FG_POP_PUI_DIALOG( YNdialogBox);
429 void ConfirmExitDialogInit(void)
431 char msg[] = "Really Quit";
434 // printf("ConfirmExitDialogInit\n");
435 int len = 200 - puGetDefaultLabelFont().getStringWidth ( msg ) / 2;
437 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
438 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
440 YNdialogBox = new puDialogBox (x, y); // 150, 50
442 YNdialogFrame = new puFrame (0,0,400, 100);
444 YNdialogBoxMessage = new puText (len, 70);
445 YNdialogBoxMessage -> setDefaultValue (msg);
446 YNdialogBoxMessage -> getDefaultValue (&s);
447 YNdialogBoxMessage -> setLabel (s);
449 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
450 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
451 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
452 YNdialogBoxOkButton -> setCallback (goodBye);
454 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
455 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
456 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
458 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
461 void notCb (puObject *)
463 mkDialog ("This function isn't implemented yet");
466 void helpCb (puObject *)
470 #if defined(FX) && !defined(WIN32)
471 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
472 if ( globals->get_fullscreen() ) {
473 globals->set_fullscreen(false);
474 XMesaSetFXmode( XMESA_FX_WINDOW );
479 SGPath path( globals->get_fg_root() );
480 path.append( "Docs/index.html" );
483 string help_app = fgGetString("/sim/startup/browser-app");
485 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
486 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
488 command = help_app + " " + path.str();
493 command += path.str();
496 system( command.c_str() );
497 mkDialog ("Help started in netscape window.");
500 #if defined( TR_HIRES_SNAP)
505 bool show_pu_cursor = false;
506 bool show_menu = false;
507 char *filename = new char [24];
508 static int count = 1;
510 static const SGPropertyNode *master_freeze
511 = fgGetNode("/sim/freeze/master");
513 bool freeze = master_freeze->getBoolValue();
515 fgSetBool("/sim/freeze/master", true);
523 if ( !puCursorIsHidden() ) {
524 show_pu_cursor = true;
529 fgReshape( fgGetInt("/sim/startup/xsize"),
530 fgGetInt("/sim/startup/ysize") );
532 // we need two render frames here to clear the menu and cursor
533 // ... not sure why but doing an extra fgRenderFrame() shouldn't
538 // Make sure we have SSG projection primed for current view
539 glMatrixMode(GL_MODELVIEW);
541 ssgSetCamera( (sgVec4 *)globals->get_current_view()->get_VIEW() );
542 ssgSetFOV( globals->get_current_view()->get_h_fov(),
543 globals->get_current_view()->get_v_fov() );
544 // ssgSetNearFar( 10.0f, 120000.0f );
545 ssgSetNearFar( 0.5f, 1200000.0f );
548 // This ImageSize stuff is a temporary hack
549 // should probably use 128x128 tile size and
550 // support any image size
552 // This should be a requester to get multiplier from user
554 int width = fgGetInt("/sim/startup/xsize");
555 int height = fgGetInt("/sim/startup/ysize");
557 /* allocate buffer large enough to store one tile */
558 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
560 printf("Malloc of tile buffer failed!\n");
564 int imageWidth = multiplier*width;
565 int imageHeight = multiplier*height;
567 /* allocate buffer to hold a row of tiles */
569 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
572 printf("Malloc of tile row buffer failed!\n");
575 TRcontext *tr = trNew();
576 trTileSize(tr, width, height, 0);
577 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
578 trImageSize(tr, imageWidth, imageHeight);
579 trRowOrder(tr, TR_TOP_TO_BOTTOM);
580 sgFrustum *frustum = ssgGetFrustum();
582 frustum->getLeft(), frustum->getRight(),
583 frustum->getBot(), frustum->getTop(),
584 frustum->getNear(), frustum->getFar());
586 /* Prepare ppm output file */
587 while (count < 1000) {
588 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
589 if ( (f = fopen(filename, "r")) == NULL )
593 f = fopen(filename, "wb");
595 printf("Couldn't open image file: %s\n", filename);
601 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
602 fprintf(f,"%i %i\n", imageWidth, imageHeight);
605 /* just to be safe... */
606 glPixelStorei(GL_PACK_ALIGNMENT, 1);
608 /* Because the HUD and Panel change the ViewPort we will
609 * need to handle some lowlevel stuff ourselves */
610 int ncols = trGet(tr, TR_COLUMNS);
611 int nrows = trGet(tr, TR_ROWS);
613 bool do_hud = fgGetBool("/sim/hud/visibility");
614 GLfloat hud_col_step = 640.0 / ncols;
615 GLfloat hud_row_step = 480.0 / nrows;
617 bool do_panel = fgPanelVisible();
618 GLfloat panel_col_step = current_panel->getWidth() / ncols;
619 GLfloat panel_row_step = current_panel->getHeight() / nrows;
625 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
626 int curRow = trGet(tr, TR_CURRENT_ROW);
629 fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
630 (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
632 current_panel->update( curColumn*panel_col_step, panel_col_step,
633 curRow*panel_row_step, panel_row_step );
634 more = trEndTile(tr);
636 /* save tile into tile row buffer*/
637 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
638 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
639 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
640 int xOffset = curColumn * bytesPerTileRow;
641 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
643 for (i=0;i<height;i++) {
644 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
645 tile + i*bytesPerTileRow, /* Src */
646 bytesPerCurrentTileRow); /* Byte count*/
649 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
650 /* write this buffered row of tiles to the file */
651 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
652 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
654 for (i=0;i<curTileHeight;i++) {
655 /* Remember, OpenGL images are bottom to top. Have to reverse. */
656 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
657 fwrite(rowPtr, 1, imageWidth*3, f);
663 fgReshape( width, height );
669 message = "Snap shot saved to ";
671 mkDialog (message.c_str());
676 // message = "Snap shot saved to ";
677 // message += filename;
678 // mkDialog (message.c_str());
685 if ( show_pu_cursor ) {
690 fgSetBool("/sim/freeze/master", false);
693 #endif // #if defined( TR_HIRES_SNAP)
696 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
698 void rotateView( double roll, double pitch, double yaw )
704 GLubyte *hiResScreenCapture( int multiplier )
706 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
707 float fov = oldfov / multiplier;
708 FGViewer *v = globals->get_current_view();
709 fgSetDouble("/sim/current-view/field-of-view", fov);
711 int cur_width = fgGetInt("/sim/startup/xsize");
712 int cur_height = fgGetInt("/sim/startup/ysize");
713 if (b1) delete( b1 );
714 // New empty (mostly) bitmap
715 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
717 for ( y = 0; y < multiplier; y++ ) {
718 for ( x = 0; x < multiplier; x++ ) {
719 fgReshape( cur_width, cur_height );
721 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
725 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
728 fgSetDouble("/sim/current-view/field-of-view", oldfov);
729 return b1->getBitmap();
733 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
734 // win32 print screen function
735 void printScreen ( puObject *obj ) {
736 bool show_pu_cursor = false;
738 if ( !puCursorIsHidden() ) {
739 show_pu_cursor = true;
745 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
746 int cur_width = fgGetInt("/sim/startup/xsize");
747 int cur_height = fgGetInt("/sim/startup/ysize");
748 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
749 p.End( hiResScreenCapture(3) );
752 mainMenuBar->reveal();
755 if ( show_pu_cursor ) {
760 #endif // #ifdef WIN32
763 void dumpSnapShot ( puObject *obj ) {
768 void dumpHiResSnapShot ( puObject *obj ) {
773 // do a screen snap shot
774 void fgDumpSnapShot () {
775 bool show_pu_cursor = false;
776 char *filename = new char [24];
778 static int count = 1;
780 static const SGPropertyNode *master_freeze
781 = fgGetNode("/sim/freeze/master");
783 bool freeze = master_freeze->getBoolValue();
785 fgSetBool("/sim/freeze/master", true);
790 if ( !puCursorIsHidden() ) {
791 show_pu_cursor = true;
796 fgReshape( fgGetInt("/sim/startup/xsize"),
797 fgGetInt("/sim/startup/ysize") );
799 // we need two render frames here to clear the menu and cursor
800 // ... not sure why but doing an extra fgRenderFrame() shouldn't
805 while (count < 1000) {
807 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
808 if ( (fp = fopen(filename, "r")) == NULL )
813 if ( sg_glDumpWindow( filename,
814 fgGetInt("/sim/startup/xsize"),
815 fgGetInt("/sim/startup/ysize")) ) {
816 message = "Snap shot saved to ";
819 message = "Failed to save to ";
823 mkDialog (message.c_str());
827 if ( show_pu_cursor ) {
833 mainMenuBar->reveal();
837 fgSetBool("/sim/freeze/master", false);
841 #ifdef FG_NETWORK_OLK
842 void net_display_toggle( puObject *cb)
844 net_hud_display = (net_hud_display) ? 0 : 1;
845 printf("Toggle net_hud_display : %d\n", net_hud_display);
848 void net_register( puObject *cb)
850 fgd_send_com( "1", FGFS_host );
851 net_is_registered = 0;
852 printf("Registering to deamon\n");
855 void net_unregister( puObject *cb)
857 fgd_send_com( "8", FGFS_host );
858 net_is_registered = -1;
859 printf("Unregistering from deamon\n");
862 #endif // #ifdef FG_NETWORK_OLK