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
45 #if defined(FX) && defined(XMESA)
46 # include <GL/xmesa.h>
57 # include <shellapi.h>
59 # include <sys/cygwin.h>
65 #include <simgear/constants.h>
66 #include <simgear/debug/logstream.hxx>
67 #include <simgear/misc/sg_path.hxx>
68 #include <simgear/screen/screen-dump.hxx>
70 #include <Include/general.hxx>
71 #include <Aircraft/aircraft.hxx>
72 #include <Airports/simple.hxx>
73 #include <Autopilot/auto_gui.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <Controls/controls.hxx>
76 #include <FDM/flight.hxx>
77 #include <Main/main.hxx>
78 #include <Main/fg_init.hxx>
79 #include <Main/fg_io.hxx>
80 #include <Main/globals.hxx>
81 #include <Main/fg_props.hxx>
82 #include <Main/renderer.hxx>
83 #include <Main/viewmgr.hxx>
85 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
86 # include <simgear/screen/win32-printer.h>
87 # include <simgear/screen/GlBitmaps.h>
91 #include "gui_local.hxx"
92 #include "preset_dlg.hxx"
93 #include "prop_picker.hxx"
94 #include "sgVec3Slider.hxx"
99 extern void fgHUDalphaAdjust( puObject * );
102 extern void fgLatLonFormatToggle( puObject *);
104 #if defined( TR_HIRES_SNAP)
105 #include <simgear/screen/tr.h>
106 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
107 GLfloat x_end, GLfloat y_end );
110 puDialogBox *dialogBox = 0;
111 puFrame *dialogFrame = 0;
112 puText *dialogBoxMessage = 0;
113 puOneShot *dialogBoxOkButton = 0;
115 puDialogBox *YNdialogBox = 0;
116 puFrame *YNdialogFrame = 0;
117 puText *YNdialogBoxMessage = 0;
118 puOneShot *YNdialogBoxOkButton = 0;
119 puOneShot *YNdialogBoxNoButton = 0;
121 char *gui_msg_OK; // "OK"
122 char *gui_msg_NO; // "NO"
123 char *gui_msg_YES; // "YES"
124 char *gui_msg_CANCEL; // "CANCEL"
125 char *gui_msg_RESET; // "RESET"
127 char msg_OK[] = "OK";
128 char msg_NO[] = "NO";
129 char msg_YES[] = "YES";
130 char msg_CANCEL[] = "Cancel";
131 char msg_RESET[] = "Reset";
133 char global_dialog_string[256];
135 const __fg_gui_fn_t __fg_gui_fn[] = {
138 {"saveFlight", saveFlight},
139 {"loadFlight", loadFlight},
142 {"dumpHiResSnapShot", fgHiResDumpWrapper},
144 {"dumpSnapShot", fgDumpSnapShotWrapper},
145 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
146 {"printScreen", printScreen},
148 {"MayBeGoodBye", MayBeGoodBye},
151 {"guiTogglePanel", guiTogglePanel},
152 {"PilotOffsetAdjust", PilotOffsetAdjust},
153 {"fgHUDalphaAdjust", fgHUDalphaAdjust},
154 {"prop_pickerView", prop_pickerView},
157 {"fgPresetAirport", fgPresetAirport},
158 {"fgPresetRunway", fgPresetRunway},
159 {"fgPresetOffsetDistance", fgPresetOffsetDistance},
160 {"fgPresetAltitude", fgPresetAltitude},
161 {"fgPresetGlideslope", fgPresetGlideslope},
162 {"fgPresetAirspeed", fgPresetAirspeed},
163 {"fgPresetCommit", fgPresetCommit},
166 {"NewAltitude", NewAltitude},
167 {"NewHeading", NewHeading},
168 {"AddWayPoint", AddWayPoint},
169 {"PopWayPoint", PopWayPoint},
170 {"ClearRoute", ClearRoute},
171 {"fgLatLonFormatToggle", fgLatLonFormatToggle},
176 // Structure termination
181 /* ================ General Purpose Functions ================ */
183 void initDialog(void) {
184 // Initialize our GLOBAL GUI STRINGS
185 gui_msg_OK = msg_OK; // "OK"
186 gui_msg_NO = msg_NO; // "NO"
187 gui_msg_YES = msg_YES; // "YES"
188 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
189 gui_msg_RESET = msg_RESET; // "RESET"
192 // General Purpose Message Box
193 void mkDialog (const char *txt)
195 strncpy(global_dialog_string, txt, 256);
196 dialogBoxMessage->setLabel(global_dialog_string);
197 FG_PUSH_PUI_DIALOG( dialogBox );
200 // Message Box to report an error.
201 void guiErrorMessage (const char *txt)
203 SG_LOG(SG_GENERAL, SG_ALERT, txt);
208 // Message Box to report a throwable (usually an exception).
209 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
213 msg += throwable.getFormattedMessage();
214 if (!throwable.getOrigin().empty()) {
215 msg += "\n (reported by ";
216 msg += throwable.getOrigin();
219 SG_LOG(SG_GENERAL, SG_ALERT, msg);
221 mkDialog(msg.c_str());
224 // Intercept the Escape Key
225 void ConfirmExitDialog(void)
227 FG_PUSH_PUI_DIALOG( YNdialogBox );
232 /* -----------------------------------------------------------------------
233 the Gui callback functions
234 ____________________________________________________________________*/
237 // Hier Neu :-) This is my newly added code
238 // Added by David Findlay <nedz@bigpond.com>
239 // on Sunday 3rd of December
241 // Start new Save Dialog Box
242 puDialogBox *SaveDialog = 0;
243 puFrame *SaveDialogFrame = 0;
244 puText *SaveDialogMessage = 0;
245 puInput *SaveDialogInput = 0;
247 puOneShot *SaveDialogOkButton = 0;
248 puOneShot *SaveDialogCancelButton = 0;
249 // static puOneShot *SaveDialogResetButton = 0;
251 // Default save filename
252 char saveFile[256] = "fgfs.sav";
255 void SaveDialogCancel(puObject *) {
256 FG_POP_PUI_DIALOG( SaveDialog );
259 // If press OK do this
260 void SaveDialogOk(puObject*) {
262 FG_POP_PUI_DIALOG( SaveDialog );
265 SaveDialogInput->getValue(&s);
268 // cout << saveFile << endl;
269 if (output.good() && fgSaveFlight(output)) {
271 mkDialog("Saved flight");
272 SG_LOG(SG_INPUT, SG_INFO, "Saved flight");
274 mkDialog("Cannot save flight");
275 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight");
280 void saveFlight(puObject *cv) {
281 SaveDialog = new puDialogBox (150, 50);
283 SaveDialogFrame = new puFrame (0,0,350, 150);
285 = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
286 SaveDialogMessage -> setLabel ("File Name:");
288 SaveDialogInput = new puInput (50, 70, 300, 100);
289 SaveDialogInput -> setValue (saveFile);
290 SaveDialogInput -> acceptInput();
292 SaveDialogOkButton = new puOneShot (50, 10, 110, 50);
293 SaveDialogOkButton -> setLegend (gui_msg_OK);
294 SaveDialogOkButton -> setCallback ( SaveDialogOk );
295 SaveDialogOkButton -> makeReturnDefault(TRUE);
297 SaveDialogCancelButton = new puOneShot (140, 10, 210, 50);
298 SaveDialogCancelButton -> setLegend (gui_msg_CANCEL);
299 SaveDialogCancelButton -> setCallback ( SaveDialogCancel );
301 FG_FINALIZE_PUI_DIALOG( SaveDialog );
303 SaveDialog -> reveal();
307 puDialogBox *LoadDialog = 0;
308 puFrame *LoadDialogFrame = 0;
309 puText *LoadDialogMessage = 0;
310 puInput *LoadDialogInput = 0;
312 puOneShot *LoadDialogOkButton = 0;
313 puOneShot *LoadDialogCancelButton = 0;
314 // static puOneShot *LoadDialogResetButton = 0;
316 // Default load filename
317 char loadFile[256] = "fgfs.sav";
319 // Do this if the person click okay
320 void LoadDialogOk(puObject *) {
322 FG_POP_PUI_DIALOG( LoadDialog );
325 LoadDialogInput->getValue(&l);
328 if (input.good() && fgLoadFlight(input)) {
330 mkDialog("Loaded flight");
331 SG_LOG(SG_INPUT, SG_INFO, "Restored flight");
333 mkDialog("Failed to load flight");
334 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight");
338 // Do this if the person presses cancel
339 void LoadDialogCancel(puObject *) {
340 FG_POP_PUI_DIALOG( LoadDialog );
343 // Create Load Dialog
344 void loadFlight(puObject *cb)
346 LoadDialog = new puDialogBox (150, 50);
348 LoadDialogFrame = new puFrame (0,0,350, 150);
350 = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
351 LoadDialogMessage -> setLabel ("File Name:");
353 LoadDialogInput = new puInput (50, 70, 300, 100);
354 LoadDialogInput -> setValue (loadFile);
355 LoadDialogInput -> acceptInput();
357 LoadDialogOkButton = new puOneShot (50, 10, 110, 50);
358 LoadDialogOkButton -> setLegend (gui_msg_OK);
359 LoadDialogOkButton -> setCallback ( LoadDialogOk );
360 LoadDialogOkButton -> makeReturnDefault(TRUE);
362 LoadDialogCancelButton = new puOneShot (140, 10, 210, 50);
363 LoadDialogCancelButton -> setLegend (gui_msg_CANCEL);
364 LoadDialogCancelButton -> setCallback ( LoadDialogCancel );
366 FG_FINALIZE_PUI_DIALOG( LoadDialog );
368 LoadDialog -> reveal();
371 // This is the accessor function
372 void guiTogglePanel(puObject *cb)
374 if (fgGetBool("/sim/panel/visibility"))
375 fgSetBool("/sim/panel/visibility", false);
377 fgSetBool("/sim/panel/visibility", true);
379 globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
380 fgGetInt("/sim/startup/ysize"));
383 void goodBye(puObject *)
385 // SG_LOG( SG_INPUT, SG_ALERT,
386 // "Program exiting normally at user request." );
387 cout << "Program exiting normally at user request." << endl;
393 void goAwayCb (puObject *me)
395 FG_POP_PUI_DIALOG( dialogBox );
398 void mkDialogInit (void)
400 // printf("mkDialogInit\n");
401 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
402 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
403 dialogBox = new puDialogBox (x, y); // 150, 50
405 dialogFrame = new puFrame (0,0,400,100);
406 dialogBoxMessage = new puText (10, 70);
407 dialogBoxMessage -> setLabel ("");
408 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
409 dialogBoxOkButton -> setLegend (gui_msg_OK);
410 dialogBoxOkButton -> makeReturnDefault (TRUE );
411 dialogBoxOkButton -> setCallback (goAwayCb);
413 FG_FINALIZE_PUI_DIALOG( dialogBox );
416 void MayBeGoodBye(puObject *)
421 void goAwayYesNoCb(puObject *me)
423 FG_POP_PUI_DIALOG( YNdialogBox);
426 void ConfirmExitDialogInit(void)
428 char msg[] = "Really Quit";
431 // printf("ConfirmExitDialogInit\n");
432 int len = 200 - puGetDefaultLabelFont().getStringWidth ( msg ) / 2;
434 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
435 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
437 YNdialogBox = new puDialogBox (x, y); // 150, 50
439 YNdialogFrame = new puFrame (0,0,400, 100);
441 YNdialogBoxMessage = new puText (len, 70);
442 YNdialogBoxMessage -> setDefaultValue (msg);
443 YNdialogBoxMessage -> getDefaultValue (&s);
444 YNdialogBoxMessage -> setLabel (s);
446 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
447 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
448 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
449 YNdialogBoxOkButton -> setCallback (goodBye);
451 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
452 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
453 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
455 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
458 void notCb (puObject *)
460 mkDialog ("This function isn't implemented yet");
463 void helpCb (puObject *)
467 #if defined(FX) && !defined(WIN32)
468 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
469 if ( globals->get_fullscreen() ) {
470 globals->set_fullscreen(false);
471 XMesaSetFXmode( XMESA_FX_WINDOW );
476 SGPath path( globals->get_fg_root() );
477 path.append( "Docs/index.html" );
481 string help_app = fgGetString("/sim/startup/browser-app");
483 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
484 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
486 command = help_app + " " + path.str();
489 system( command.c_str() );
493 // Look for favorite browser
494 char Dummy[1024], ExecName[1024], browserParameter[1024];
495 char win32_name[1024];
497 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
499 strcpy(win32_name,path.c_str());
502 FindExecutable(win32_name, Dummy, ExecName);
503 sprintf(browserParameter, "file:///%s", win32_name);
504 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
509 mkDialog ("Help started in your web browser window.");
512 #if defined( TR_HIRES_SNAP)
517 bool show_pu_cursor = false;
518 char *filename = new char [24];
519 static int count = 1;
521 static const SGPropertyNode *master_freeze
522 = fgGetNode("/sim/freeze/master");
524 bool freeze = master_freeze->getBoolValue();
526 fgSetBool("/sim/freeze/master", true);
530 if ( !puCursorIsHidden() ) {
531 show_pu_cursor = true;
535 FGRenderer *renderer = globals->get_renderer();
537 renderer->resize( fgGetInt("/sim/startup/xsize"),
538 fgGetInt("/sim/startup/ysize") );
540 // we need two render frames here to clear the menu and cursor
541 // ... not sure why but doing an extra fgRenderFrame() shouldn't
543 renderer->update( true );
544 renderer->update( true );
546 // This ImageSize stuff is a temporary hack
547 // should probably use 128x128 tile size and
548 // support any image size
550 // This should be a requester to get multiplier from user
551 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
552 int width = fgGetInt("/sim/startup/xsize");
553 int height = fgGetInt("/sim/startup/ysize");
555 /* allocate buffer large enough to store one tile */
556 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
558 printf("Malloc of tile buffer failed!\n");
562 int imageWidth = multiplier*width;
563 int imageHeight = multiplier*height;
565 /* allocate buffer to hold a row of tiles */
567 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
570 printf("Malloc of tile row buffer failed!\n");
573 TRcontext *tr = trNew();
574 trTileSize(tr, width, height, 0);
575 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
576 trImageSize(tr, imageWidth, imageHeight);
577 trRowOrder(tr, TR_TOP_TO_BOTTOM);
578 sgFrustum *frustum = ssgGetFrustum();
580 frustum->getLeft(), frustum->getRight(),
581 frustum->getBot(), frustum->getTop(),
582 frustum->getNear(), frustum->getFar());
584 /* Prepare ppm output file */
585 while (count < 1000) {
586 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
587 if ( (f = fopen(filename, "r")) == NULL )
591 f = fopen(filename, "wb");
593 printf("Couldn't open image file: %s\n", filename);
599 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
600 fprintf(f,"%i %i\n", imageWidth, imageHeight);
603 /* just to be safe... */
604 glPixelStorei(GL_PACK_ALIGNMENT, 1);
606 /* Because the HUD and Panel change the ViewPort we will
607 * need to handle some lowlevel stuff ourselves */
608 int ncols = trGet(tr, TR_COLUMNS);
609 int nrows = trGet(tr, TR_ROWS);
611 bool do_hud = fgGetBool("/sim/hud/visibility");
612 GLfloat hud_col_step = 640.0 / ncols;
613 GLfloat hud_row_step = 480.0 / nrows;
615 bool do_panel = fgPanelVisible();
616 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
617 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
623 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
624 int curRow = trGet(tr, TR_CURRENT_ROW);
625 renderer->update( false );
627 fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
628 (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
630 globals->get_current_panel()->update(
631 curColumn*panel_col_step, panel_col_step,
632 curRow*panel_row_step, panel_row_step );
633 more = trEndTile(tr);
635 /* save tile into tile row buffer*/
636 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
637 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
638 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
639 int xOffset = curColumn * bytesPerTileRow;
640 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
642 for (i=0;i<height;i++) {
643 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
644 tile + i*bytesPerTileRow, /* Src */
645 bytesPerCurrentTileRow); /* Byte count*/
648 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
649 /* write this buffered row of tiles to the file */
650 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
651 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
653 for (i=0;i<curTileHeight;i++) {
654 /* Remember, OpenGL images are bottom to top. Have to reverse. */
655 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
656 fwrite(rowPtr, 1, imageWidth*3, f);
662 renderer->resize( width, height );
668 message = "Snap shot saved to ";
670 mkDialog (message.c_str());
675 // message = "Snap shot saved to ";
676 // message += filename;
677 // mkDialog (message.c_str());
681 if ( show_pu_cursor ) {
688 fgSetBool("/sim/freeze/master", false);
691 #endif // #if defined( TR_HIRES_SNAP)
694 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
696 void rotateView( double roll, double pitch, double yaw )
702 GLubyte *hiResScreenCapture( int multiplier )
704 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
705 float fov = oldfov / multiplier;
706 FGViewer *v = globals->get_current_view();
707 fgSetDouble("/sim/current-view/field-of-view", fov);
708 globals->get_renderer()->init();
709 int cur_width = fgGetInt("/sim/startup/xsize");
710 int cur_height = fgGetInt("/sim/startup/ysize");
712 // New empty (mostly) bitmap
713 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
715 for ( y = 0; y < multiplier; y++ ) {
716 for ( x = 0; x < multiplier; x++ ) {
717 globals->get_renderer()->resize( cur_width, cur_height );
719 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
720 globals->get_renderer()->update( false );
723 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
726 fgSetDouble("/sim/current-view/field-of-view", oldfov);
727 return b1->getBitmap();
731 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
732 // win32 print screen function
733 void printScreen ( puObject *obj ) {
734 bool show_pu_cursor = false;
736 if ( !puCursorIsHidden() ) {
737 show_pu_cursor = true;
742 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
743 int cur_width = fgGetInt("/sim/startup/xsize");
744 int cur_height = fgGetInt("/sim/startup/ysize");
745 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
746 p.End( hiResScreenCapture(3) );
749 if ( show_pu_cursor ) {
754 #endif // #ifdef WIN32
757 void fgDumpSnapShotWrapper ( puObject *obj ) {
762 void fgHiResDumpWrapper ( puObject *obj ) {
767 // do a screen snap shot
768 void fgDumpSnapShot () {
769 bool show_pu_cursor = false;
770 char *filename = new char [24];
772 static int count = 1;
774 static const SGPropertyNode *master_freeze
775 = fgGetNode("/sim/freeze/master");
777 bool freeze = master_freeze->getBoolValue();
779 fgSetBool("/sim/freeze/master", true);
783 if ( !puCursorIsHidden() ) {
784 show_pu_cursor = true;
788 FGRenderer *renderer = globals->get_renderer();
790 renderer->resize( fgGetInt("/sim/startup/xsize"),
791 fgGetInt("/sim/startup/ysize") );
793 // we need two render frames here to clear the menu and cursor
794 // ... not sure why but doing an extra fgRenderFrame() shouldn't
796 renderer->update( true );
797 renderer->update( true );
799 while (count < 1000) {
801 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
802 if ( (fp = fopen(filename, "r")) == NULL )
807 if ( sg_glDumpWindow( filename,
808 fgGetInt("/sim/startup/xsize"),
809 fgGetInt("/sim/startup/ysize")) ) {
810 message = "Snap shot saved to ";
813 message = "Failed to save to ";
817 mkDialog (message.c_str());
821 if ( show_pu_cursor ) {
828 fgSetBool("/sim/freeze/master", false);