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>
84 #include <GUI/new_gui.hxx>
86 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
87 # include <simgear/screen/win32-printer.h>
88 # include <simgear/screen/GlBitmaps.h>
92 #include "gui_local.hxx"
93 #include "preset_dlg.hxx"
94 #include "prop_picker.hxx"
95 #include "sgVec3Slider.hxx"
100 extern void fgHUDalphaAdjust( puObject * );
103 extern void fgLatLonFormatToggle( puObject *);
105 #if defined( TR_HIRES_SNAP)
106 #include <simgear/screen/tr.h>
107 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
108 GLfloat x_end, GLfloat y_end );
112 // TODO: remove after the last hardcoded dialog has died
113 char *gui_msg_OK = "OK";
114 char *gui_msg_NO = "NO";
115 char *gui_msg_YES = "YES";
116 char *gui_msg_CANCEL = "CANCEL";
117 char *gui_msg_RESET = "RESET";
120 const __fg_gui_fn_t __fg_gui_fn[] = {
125 {"dumpHiResSnapShot", fgHiResDumpWrapper},
127 {"dumpSnapShot", fgDumpSnapShotWrapper},
128 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
129 {"printScreen", printScreen},
133 {"guiTogglePanel", guiTogglePanel},
134 {"fgHUDalphaAdjust", fgHUDalphaAdjust},
135 {"prop_pickerView", prop_pickerView},
138 {"fgPresetAirport", fgPresetAirport},
139 {"fgPresetRunway", fgPresetRunway},
140 {"fgPresetOffsetDistance", fgPresetOffsetDistance},
141 {"fgPresetAltitude", fgPresetAltitude},
142 {"fgPresetGlideslope", fgPresetGlideslope},
143 {"fgPresetAirspeed", fgPresetAirspeed},
144 {"fgPresetCommit", fgPresetCommit},
147 {"NewAltitude", NewAltitude},
148 {"NewHeading", NewHeading},
149 {"AddWayPoint", AddWayPoint},
150 {"PopWayPoint", PopWayPoint},
151 {"ClearRoute", ClearRoute},
152 {"fgLatLonFormatToggle", fgLatLonFormatToggle},
157 // Structure termination
162 /* ================ General Purpose Functions ================ */
164 // General Purpose Message Box
165 void mkDialog (const char *txt)
167 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
168 SGPropertyNode_ptr dlg = gui->getDialog("message");
172 dlg->setStringValue("text/label", txt);
173 dlg->setStringValue("button/legend", "OK");
174 gui->showDialog("message");
177 // Message Box to report an error.
178 void guiErrorMessage (const char *txt)
180 SG_LOG(SG_GENERAL, SG_ALERT, txt);
184 // Message Box to report a throwable (usually an exception).
185 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
189 msg += throwable.getFormattedMessage();
190 if (!throwable.getOrigin().empty()) {
191 msg += "\n (reported by ";
192 msg += throwable.getOrigin();
195 SG_LOG(SG_GENERAL, SG_ALERT, msg);
196 mkDialog(msg.c_str());
201 /* -----------------------------------------------------------------------
202 the Gui callback functions
203 ____________________________________________________________________*/
206 // Hier Neu :-) This is my newly added code
207 // Added by David Findlay <nedz@bigpond.com>
208 // on Sunday 3rd of December
211 // This is the accessor function
212 void guiTogglePanel(puObject *cb)
214 if (fgGetBool("/sim/panel/visibility"))
215 fgSetBool("/sim/panel/visibility", false);
217 fgSetBool("/sim/panel/visibility", true);
219 globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
220 fgGetInt("/sim/startup/ysize"));
223 void helpCb (puObject *)
227 #if defined(FX) && !defined(WIN32)
228 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
229 if ( globals->get_fullscreen() ) {
230 globals->set_fullscreen(false);
231 XMesaSetFXmode( XMESA_FX_WINDOW );
236 SGPath path( globals->get_fg_root() );
237 path.append( "Docs/index.html" );
241 string help_app = fgGetString("/sim/startup/browser-app");
243 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
244 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
246 command = help_app + " " + path.str();
249 system( command.c_str() );
253 // Look for favorite browser
254 char Dummy[1024], ExecName[1024], browserParameter[1024];
255 char win32_name[1024];
257 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
259 strcpy(win32_name,path.c_str());
262 FindExecutable(win32_name, Dummy, ExecName);
263 sprintf(browserParameter, "file:///%s", win32_name);
264 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
269 mkDialog ("Help started in your web browser window.");
272 #if defined( TR_HIRES_SNAP)
277 bool show_pu_cursor = false;
278 char *filename = new char [24];
279 static int count = 1;
281 static const SGPropertyNode *master_freeze
282 = fgGetNode("/sim/freeze/master");
284 bool freeze = master_freeze->getBoolValue();
286 fgSetBool("/sim/freeze/master", true);
290 if ( !puCursorIsHidden() ) {
291 show_pu_cursor = true;
295 FGRenderer *renderer = globals->get_renderer();
297 renderer->resize( fgGetInt("/sim/startup/xsize"),
298 fgGetInt("/sim/startup/ysize") );
300 // we need two render frames here to clear the menu and cursor
301 // ... not sure why but doing an extra fgRenderFrame() shouldn't
303 renderer->update( true );
304 renderer->update( true );
306 // This ImageSize stuff is a temporary hack
307 // should probably use 128x128 tile size and
308 // support any image size
310 // This should be a requester to get multiplier from user
311 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
312 int width = fgGetInt("/sim/startup/xsize");
313 int height = fgGetInt("/sim/startup/ysize");
315 /* allocate buffer large enough to store one tile */
316 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
318 printf("Malloc of tile buffer failed!\n");
322 int imageWidth = multiplier*width;
323 int imageHeight = multiplier*height;
325 /* allocate buffer to hold a row of tiles */
327 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
330 printf("Malloc of tile row buffer failed!\n");
333 TRcontext *tr = trNew();
334 trTileSize(tr, width, height, 0);
335 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
336 trImageSize(tr, imageWidth, imageHeight);
337 trRowOrder(tr, TR_TOP_TO_BOTTOM);
338 sgFrustum *frustum = ssgGetFrustum();
340 frustum->getLeft(), frustum->getRight(),
341 frustum->getBot(), frustum->getTop(),
342 frustum->getNear(), frustum->getFar());
344 /* Prepare ppm output file */
345 while (count < 1000) {
346 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
347 if ( (f = fopen(filename, "r")) == NULL )
351 f = fopen(filename, "wb");
353 printf("Couldn't open image file: %s\n", filename);
359 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
360 fprintf(f,"%i %i\n", imageWidth, imageHeight);
363 /* just to be safe... */
364 glPixelStorei(GL_PACK_ALIGNMENT, 1);
366 /* Because the HUD and Panel change the ViewPort we will
367 * need to handle some lowlevel stuff ourselves */
368 int ncols = trGet(tr, TR_COLUMNS);
369 int nrows = trGet(tr, TR_ROWS);
371 bool do_hud = fgGetBool("/sim/hud/visibility");
372 GLfloat hud_col_step = 640.0 / ncols;
373 GLfloat hud_row_step = 480.0 / nrows;
375 bool do_panel = fgPanelVisible();
376 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
377 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
383 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
384 int curRow = trGet(tr, TR_CURRENT_ROW);
385 renderer->update( false );
387 fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
388 (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
390 globals->get_current_panel()->update(
391 curColumn*panel_col_step, panel_col_step,
392 curRow*panel_row_step, panel_row_step );
393 more = trEndTile(tr);
395 /* save tile into tile row buffer*/
396 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
397 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
398 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
399 int xOffset = curColumn * bytesPerTileRow;
400 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
402 for (i=0;i<height;i++) {
403 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
404 tile + i*bytesPerTileRow, /* Src */
405 bytesPerCurrentTileRow); /* Byte count*/
408 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
409 /* write this buffered row of tiles to the file */
410 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
411 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
413 for (i=0;i<curTileHeight;i++) {
414 /* Remember, OpenGL images are bottom to top. Have to reverse. */
415 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
416 fwrite(rowPtr, 1, imageWidth*3, f);
422 renderer->resize( width, height );
428 message = "Snapshot saved to \"";
431 mkDialog (message.c_str());
438 if ( show_pu_cursor ) {
445 fgSetBool("/sim/freeze/master", false);
448 #endif // #if defined( TR_HIRES_SNAP)
451 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
453 void rotateView( double roll, double pitch, double yaw )
459 GLubyte *hiResScreenCapture( int multiplier )
461 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
462 float fov = oldfov / multiplier;
463 FGViewer *v = globals->get_current_view();
464 fgSetDouble("/sim/current-view/field-of-view", fov);
465 globals->get_renderer()->init();
466 int cur_width = fgGetInt("/sim/startup/xsize");
467 int cur_height = fgGetInt("/sim/startup/ysize");
469 // New empty (mostly) bitmap
470 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
472 for ( y = 0; y < multiplier; y++ ) {
473 for ( x = 0; x < multiplier; x++ ) {
474 globals->get_renderer()->resize( cur_width, cur_height );
476 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
477 globals->get_renderer()->update( false );
480 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
483 fgSetDouble("/sim/current-view/field-of-view", oldfov);
484 return b1->getBitmap();
488 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
489 // win32 print screen function
490 void printScreen ( puObject *obj ) {
491 bool show_pu_cursor = false;
493 if ( !puCursorIsHidden() ) {
494 show_pu_cursor = true;
499 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
500 int cur_width = fgGetInt("/sim/startup/xsize");
501 int cur_height = fgGetInt("/sim/startup/ysize");
502 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
503 p.End( hiResScreenCapture(3) );
506 if ( show_pu_cursor ) {
511 #endif // #ifdef WIN32
514 void fgDumpSnapShotWrapper ( puObject *obj ) {
519 void fgHiResDumpWrapper ( puObject *obj ) {
524 // do a screen snap shot
525 void fgDumpSnapShot () {
526 bool show_pu_cursor = false;
527 char *filename = new char [24];
529 static int count = 1;
531 static const SGPropertyNode *master_freeze
532 = fgGetNode("/sim/freeze/master");
534 bool freeze = master_freeze->getBoolValue();
536 fgSetBool("/sim/freeze/master", true);
540 if ( !puCursorIsHidden() ) {
541 show_pu_cursor = true;
545 FGRenderer *renderer = globals->get_renderer();
547 renderer->resize( fgGetInt("/sim/startup/xsize"),
548 fgGetInt("/sim/startup/ysize") );
550 // we need two render frames here to clear the menu and cursor
551 // ... not sure why but doing an extra fgRenderFrame() shouldn't
553 renderer->update( true );
554 renderer->update( true );
556 while (count < 1000) {
558 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
559 if ( (fp = fopen(filename, "r")) == NULL )
564 if ( sg_glDumpWindow( filename,
565 fgGetInt("/sim/startup/xsize"),
566 fgGetInt("/sim/startup/ysize")) ) {
567 message = "Snapshot saved to \"";
571 message = "Failed to save to \"";
576 mkDialog (message.c_str());
580 if ( show_pu_cursor ) {
587 fgSetBool("/sim/freeze/master", false);