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[] = {
140 {"dumpHiResSnapShot", fgHiResDumpWrapper},
142 {"dumpSnapShot", fgDumpSnapShotWrapper},
143 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
144 {"printScreen", printScreen},
146 {"MayBeGoodBye", MayBeGoodBye},
149 {"guiTogglePanel", guiTogglePanel},
150 {"fgHUDalphaAdjust", fgHUDalphaAdjust},
151 {"prop_pickerView", prop_pickerView},
154 {"fgPresetAirport", fgPresetAirport},
155 {"fgPresetRunway", fgPresetRunway},
156 {"fgPresetOffsetDistance", fgPresetOffsetDistance},
157 {"fgPresetAltitude", fgPresetAltitude},
158 {"fgPresetGlideslope", fgPresetGlideslope},
159 {"fgPresetAirspeed", fgPresetAirspeed},
160 {"fgPresetCommit", fgPresetCommit},
163 {"NewAltitude", NewAltitude},
164 {"NewHeading", NewHeading},
165 {"AddWayPoint", AddWayPoint},
166 {"PopWayPoint", PopWayPoint},
167 {"ClearRoute", ClearRoute},
168 {"fgLatLonFormatToggle", fgLatLonFormatToggle},
173 // Structure termination
178 /* ================ General Purpose Functions ================ */
180 void initDialog(void) {
181 // Initialize our GLOBAL GUI STRINGS
182 gui_msg_OK = msg_OK; // "OK"
183 gui_msg_NO = msg_NO; // "NO"
184 gui_msg_YES = msg_YES; // "YES"
185 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
186 gui_msg_RESET = msg_RESET; // "RESET"
189 // General Purpose Message Box
190 void mkDialog (const char *txt)
192 strncpy(global_dialog_string, txt, 256);
193 dialogBoxMessage->setLabel(global_dialog_string);
194 FG_PUSH_PUI_DIALOG( dialogBox );
197 // Message Box to report an error.
198 void guiErrorMessage (const char *txt)
200 SG_LOG(SG_GENERAL, SG_ALERT, txt);
205 // Message Box to report a throwable (usually an exception).
206 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
210 msg += throwable.getFormattedMessage();
211 if (!throwable.getOrigin().empty()) {
212 msg += "\n (reported by ";
213 msg += throwable.getOrigin();
216 SG_LOG(SG_GENERAL, SG_ALERT, msg);
218 mkDialog(msg.c_str());
221 // Intercept the Escape Key
222 void ConfirmExitDialog(void)
224 FG_PUSH_PUI_DIALOG( YNdialogBox );
229 /* -----------------------------------------------------------------------
230 the Gui callback functions
231 ____________________________________________________________________*/
234 // Hier Neu :-) This is my newly added code
235 // Added by David Findlay <nedz@bigpond.com>
236 // on Sunday 3rd of December
239 // This is the accessor function
240 void guiTogglePanel(puObject *cb)
242 if (fgGetBool("/sim/panel/visibility"))
243 fgSetBool("/sim/panel/visibility", false);
245 fgSetBool("/sim/panel/visibility", true);
247 globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
248 fgGetInt("/sim/startup/ysize"));
251 void goodBye(puObject *)
253 // SG_LOG( SG_INPUT, SG_ALERT,
254 // "Program exiting normally at user request." );
255 cout << "Program exiting normally at user request." << endl;
261 void goAwayCb (puObject *me)
263 FG_POP_PUI_DIALOG( dialogBox );
266 void mkDialogInit (void)
268 // printf("mkDialogInit\n");
269 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
270 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
271 dialogBox = new puDialogBox (x, y); // 150, 50
273 dialogFrame = new puFrame (0,0,400,100);
274 dialogBoxMessage = new puText (10, 70);
275 dialogBoxMessage -> setLabel ("");
276 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
277 dialogBoxOkButton -> setLegend (gui_msg_OK);
278 dialogBoxOkButton -> makeReturnDefault (TRUE );
279 dialogBoxOkButton -> setCallback (goAwayCb);
281 FG_FINALIZE_PUI_DIALOG( dialogBox );
284 void MayBeGoodBye(puObject *)
289 void goAwayYesNoCb(puObject *me)
291 FG_POP_PUI_DIALOG( YNdialogBox);
294 void ConfirmExitDialogInit(void)
296 char msg[] = "Really Quit";
299 // printf("ConfirmExitDialogInit\n");
300 int len = 200 - puGetDefaultLabelFont().getStringWidth ( msg ) / 2;
302 int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
303 int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
305 YNdialogBox = new puDialogBox (x, y); // 150, 50
307 YNdialogFrame = new puFrame (0,0,400, 100);
309 YNdialogBoxMessage = new puText (len, 70);
310 YNdialogBoxMessage -> setDefaultValue (msg);
311 YNdialogBoxMessage -> getDefaultValue (&s);
312 YNdialogBoxMessage -> setLabel (s);
314 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
315 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
316 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
317 YNdialogBoxOkButton -> setCallback (goodBye);
319 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
320 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
321 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
323 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
326 void notCb (puObject *)
328 mkDialog ("This function isn't implemented yet");
331 void helpCb (puObject *)
335 #if defined(FX) && !defined(WIN32)
336 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
337 if ( globals->get_fullscreen() ) {
338 globals->set_fullscreen(false);
339 XMesaSetFXmode( XMESA_FX_WINDOW );
344 SGPath path( globals->get_fg_root() );
345 path.append( "Docs/index.html" );
349 string help_app = fgGetString("/sim/startup/browser-app");
351 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
352 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
354 command = help_app + " " + path.str();
357 system( command.c_str() );
361 // Look for favorite browser
362 char Dummy[1024], ExecName[1024], browserParameter[1024];
363 char win32_name[1024];
365 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
367 strcpy(win32_name,path.c_str());
370 FindExecutable(win32_name, Dummy, ExecName);
371 sprintf(browserParameter, "file:///%s", win32_name);
372 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
377 mkDialog ("Help started in your web browser window.");
380 #if defined( TR_HIRES_SNAP)
385 bool show_pu_cursor = false;
386 char *filename = new char [24];
387 static int count = 1;
389 static const SGPropertyNode *master_freeze
390 = fgGetNode("/sim/freeze/master");
392 bool freeze = master_freeze->getBoolValue();
394 fgSetBool("/sim/freeze/master", true);
398 if ( !puCursorIsHidden() ) {
399 show_pu_cursor = true;
403 FGRenderer *renderer = globals->get_renderer();
405 renderer->resize( fgGetInt("/sim/startup/xsize"),
406 fgGetInt("/sim/startup/ysize") );
408 // we need two render frames here to clear the menu and cursor
409 // ... not sure why but doing an extra fgRenderFrame() shouldn't
411 renderer->update( true );
412 renderer->update( true );
414 // This ImageSize stuff is a temporary hack
415 // should probably use 128x128 tile size and
416 // support any image size
418 // This should be a requester to get multiplier from user
419 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
420 int width = fgGetInt("/sim/startup/xsize");
421 int height = fgGetInt("/sim/startup/ysize");
423 /* allocate buffer large enough to store one tile */
424 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
426 printf("Malloc of tile buffer failed!\n");
430 int imageWidth = multiplier*width;
431 int imageHeight = multiplier*height;
433 /* allocate buffer to hold a row of tiles */
435 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
438 printf("Malloc of tile row buffer failed!\n");
441 TRcontext *tr = trNew();
442 trTileSize(tr, width, height, 0);
443 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
444 trImageSize(tr, imageWidth, imageHeight);
445 trRowOrder(tr, TR_TOP_TO_BOTTOM);
446 sgFrustum *frustum = ssgGetFrustum();
448 frustum->getLeft(), frustum->getRight(),
449 frustum->getBot(), frustum->getTop(),
450 frustum->getNear(), frustum->getFar());
452 /* Prepare ppm output file */
453 while (count < 1000) {
454 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
455 if ( (f = fopen(filename, "r")) == NULL )
459 f = fopen(filename, "wb");
461 printf("Couldn't open image file: %s\n", filename);
467 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
468 fprintf(f,"%i %i\n", imageWidth, imageHeight);
471 /* just to be safe... */
472 glPixelStorei(GL_PACK_ALIGNMENT, 1);
474 /* Because the HUD and Panel change the ViewPort we will
475 * need to handle some lowlevel stuff ourselves */
476 int ncols = trGet(tr, TR_COLUMNS);
477 int nrows = trGet(tr, TR_ROWS);
479 bool do_hud = fgGetBool("/sim/hud/visibility");
480 GLfloat hud_col_step = 640.0 / ncols;
481 GLfloat hud_row_step = 480.0 / nrows;
483 bool do_panel = fgPanelVisible();
484 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
485 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
491 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
492 int curRow = trGet(tr, TR_CURRENT_ROW);
493 renderer->update( false );
495 fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
496 (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
498 globals->get_current_panel()->update(
499 curColumn*panel_col_step, panel_col_step,
500 curRow*panel_row_step, panel_row_step );
501 more = trEndTile(tr);
503 /* save tile into tile row buffer*/
504 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
505 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
506 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
507 int xOffset = curColumn * bytesPerTileRow;
508 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
510 for (i=0;i<height;i++) {
511 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
512 tile + i*bytesPerTileRow, /* Src */
513 bytesPerCurrentTileRow); /* Byte count*/
516 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
517 /* write this buffered row of tiles to the file */
518 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
519 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
521 for (i=0;i<curTileHeight;i++) {
522 /* Remember, OpenGL images are bottom to top. Have to reverse. */
523 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
524 fwrite(rowPtr, 1, imageWidth*3, f);
530 renderer->resize( width, height );
536 message = "Snap shot saved to ";
538 mkDialog (message.c_str());
543 // message = "Snap shot saved to ";
544 // message += filename;
545 // mkDialog (message.c_str());
549 if ( show_pu_cursor ) {
556 fgSetBool("/sim/freeze/master", false);
559 #endif // #if defined( TR_HIRES_SNAP)
562 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
564 void rotateView( double roll, double pitch, double yaw )
570 GLubyte *hiResScreenCapture( int multiplier )
572 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
573 float fov = oldfov / multiplier;
574 FGViewer *v = globals->get_current_view();
575 fgSetDouble("/sim/current-view/field-of-view", fov);
576 globals->get_renderer()->init();
577 int cur_width = fgGetInt("/sim/startup/xsize");
578 int cur_height = fgGetInt("/sim/startup/ysize");
580 // New empty (mostly) bitmap
581 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
583 for ( y = 0; y < multiplier; y++ ) {
584 for ( x = 0; x < multiplier; x++ ) {
585 globals->get_renderer()->resize( cur_width, cur_height );
587 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
588 globals->get_renderer()->update( false );
591 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
594 fgSetDouble("/sim/current-view/field-of-view", oldfov);
595 return b1->getBitmap();
599 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
600 // win32 print screen function
601 void printScreen ( puObject *obj ) {
602 bool show_pu_cursor = false;
604 if ( !puCursorIsHidden() ) {
605 show_pu_cursor = true;
610 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
611 int cur_width = fgGetInt("/sim/startup/xsize");
612 int cur_height = fgGetInt("/sim/startup/ysize");
613 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
614 p.End( hiResScreenCapture(3) );
617 if ( show_pu_cursor ) {
622 #endif // #ifdef WIN32
625 void fgDumpSnapShotWrapper ( puObject *obj ) {
630 void fgHiResDumpWrapper ( puObject *obj ) {
635 // do a screen snap shot
636 void fgDumpSnapShot () {
637 bool show_pu_cursor = false;
638 char *filename = new char [24];
640 static int count = 1;
642 static const SGPropertyNode *master_freeze
643 = fgGetNode("/sim/freeze/master");
645 bool freeze = master_freeze->getBoolValue();
647 fgSetBool("/sim/freeze/master", true);
651 if ( !puCursorIsHidden() ) {
652 show_pu_cursor = true;
656 FGRenderer *renderer = globals->get_renderer();
658 renderer->resize( fgGetInt("/sim/startup/xsize"),
659 fgGetInt("/sim/startup/ysize") );
661 // we need two render frames here to clear the menu and cursor
662 // ... not sure why but doing an extra fgRenderFrame() shouldn't
664 renderer->update( true );
665 renderer->update( true );
667 while (count < 1000) {
669 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
670 if ( (fp = fopen(filename, "r")) == NULL )
675 if ( sg_glDumpWindow( filename,
676 fgGetInt("/sim/startup/xsize"),
677 fgGetInt("/sim/startup/ysize")) ) {
678 message = "Snap shot saved to ";
681 message = "Failed to save to ";
685 mkDialog (message.c_str());
689 if ( show_pu_cursor ) {
696 fgSetBool("/sim/freeze/master", false);