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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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>
63 #include <simgear/constants.h>
64 #include <simgear/debug/logstream.hxx>
65 #include <simgear/misc/sg_path.hxx>
66 #include <simgear/screen/screen-dump.hxx>
68 #include <Include/general.hxx>
69 #include <Aircraft/aircraft.hxx>
70 #include <Aircraft/controls.hxx>
71 #include <Airports/simple.hxx>
72 #include <Cockpit/panel.hxx>
73 #include <FDM/flight.hxx>
74 #include <Main/main.hxx>
75 #include <Main/fg_init.hxx>
76 #include <Main/fg_io.hxx>
77 #include <Main/globals.hxx>
78 #include <Main/fg_props.hxx>
79 #include <Main/renderer.hxx>
80 #include <Main/viewmgr.hxx>
81 #include <GUI/new_gui.hxx>
83 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
84 # include <simgear/screen/win32-printer.h>
85 # include <simgear/screen/GlBitmaps.h>
89 #include "gui_local.hxx"
90 #include "preset_dlg.hxx"
91 #include "prop_picker.hxx"
92 #include "sgVec3Slider.hxx"
98 #if defined( TR_HIRES_SNAP)
99 #include <simgear/screen/tr.h>
100 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
101 GLfloat x_end, GLfloat y_end );
105 // TODO: remove after the last hardcoded dialog has died
106 char *gui_msg_OK = "OK";
107 char *gui_msg_NO = "NO";
108 char *gui_msg_YES = "YES";
109 char *gui_msg_CANCEL = "CANCEL";
110 char *gui_msg_RESET = "RESET";
113 const __fg_gui_fn_t __fg_gui_fn[] = {
118 {"dumpHiResSnapShot", fgHiResDumpWrapper},
120 {"dumpSnapShot", fgDumpSnapShotWrapper},
121 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
122 {"printScreen", printScreen},
126 {"guiTogglePanel", guiTogglePanel},
127 {"prop_pickerView", prop_pickerView},
130 {"fgPresetAirport", fgPresetAirport},
131 {"fgPresetRunway", fgPresetRunway},
132 {"fgPresetOffsetDistance", fgPresetOffsetDistance},
133 {"fgPresetAltitude", fgPresetAltitude},
134 {"fgPresetGlideslope", fgPresetGlideslope},
135 {"fgPresetAirspeed", fgPresetAirspeed},
136 {"fgPresetCommit", fgPresetCommit},
141 // Structure termination
146 /* ================ General Purpose Functions ================ */
148 // General Purpose Message Box
149 void mkDialog (const char *txt)
151 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
154 SGPropertyNode_ptr dlg = gui->getDialogProperties("message");
158 dlg->setStringValue("text/label", txt);
159 dlg->setStringValue("button/legend", "OK");
160 gui->showDialog("message");
163 // Message Box to report an error.
164 void guiErrorMessage (const char *txt)
166 SG_LOG(SG_GENERAL, SG_ALERT, txt);
170 // Message Box to report a throwable (usually an exception).
171 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
175 msg += throwable.getFormattedMessage();
176 if (!throwable.getOrigin().empty()) {
177 msg += "\n (reported by ";
178 msg += throwable.getOrigin();
181 SG_LOG(SG_GENERAL, SG_ALERT, msg);
182 mkDialog(msg.c_str());
187 /* -----------------------------------------------------------------------
188 the Gui callback functions
189 ____________________________________________________________________*/
192 // Hier Neu :-) This is my newly added code
193 // Added by David Findlay <nedz@bigpond.com>
194 // on Sunday 3rd of December
197 // This is the accessor function
198 void guiTogglePanel(puObject *cb)
200 if (fgGetBool("/sim/panel/visibility"))
201 fgSetBool("/sim/panel/visibility", false);
203 fgSetBool("/sim/panel/visibility", true);
205 globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
206 fgGetInt("/sim/startup/ysize"));
209 void helpCb (puObject *)
213 #if defined(FX) && !defined(WIN32)
214 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
215 if ( globals->get_fullscreen() ) {
216 globals->set_fullscreen(false);
217 XMesaSetFXmode( XMESA_FX_WINDOW );
222 SGPath path( globals->get_fg_root() );
223 path.append( "Docs/index.html" );
227 string help_app = fgGetString("/sim/startup/browser-app");
229 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
230 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
232 command = help_app + " " + path.str();
235 system( command.c_str() );
239 // Look for favorite browser
240 char Dummy[1024], ExecName[1024], browserParameter[1024];
241 char win32_name[1024];
243 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
245 strncpy(win32_name,path.c_str(), 1024);
248 FindExecutable(win32_name, Dummy, ExecName);
249 snprintf(browserParameter, 1024, "file:///%s", win32_name);
250 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
255 mkDialog ("Help started in your web browser window.");
258 #if defined( TR_HIRES_SNAP)
263 bool show_pu_cursor = false;
264 bool menu_status = fgGetBool("/sim/menubar/visibility");
265 char *filename = new char [24];
266 static int count = 1;
268 static const SGPropertyNode *master_freeze
269 = fgGetNode("/sim/freeze/master");
271 bool freeze = master_freeze->getBoolValue();
273 fgSetBool("/sim/freeze/master", true);
276 fgSetBool("/sim/menubar/visibility", false);
278 if ( !puCursorIsHidden() ) {
279 show_pu_cursor = true;
283 FGRenderer *renderer = globals->get_renderer();
285 renderer->resize( fgGetInt("/sim/startup/xsize"),
286 fgGetInt("/sim/startup/ysize") );
288 // we need two render frames here to clear the menu and cursor
289 // ... not sure why but doing an extra fgRenderFrame() shouldn't
291 //renderer->update( true );
292 //renderer->update( true );
294 // This ImageSize stuff is a temporary hack
295 // should probably use 128x128 tile size and
296 // support any image size
298 // This should be a requester to get multiplier from user
299 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
300 int width = fgGetInt("/sim/startup/xsize");
301 int height = fgGetInt("/sim/startup/ysize");
303 /* allocate buffer large enough to store one tile */
304 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
306 printf("Malloc of tile buffer failed!\n");
310 int imageWidth = multiplier*width;
311 int imageHeight = multiplier*height;
313 /* allocate buffer to hold a row of tiles */
315 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
318 printf("Malloc of tile row buffer failed!\n");
321 TRcontext *tr = trNew();
322 trTileSize(tr, width, height, 0);
323 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
324 trImageSize(tr, imageWidth, imageHeight);
325 trRowOrder(tr, TR_TOP_TO_BOTTOM);
327 // sgFrustum *frustum = ssgGetFrustum();
329 // frustum->getLeft(), frustum->getRight(),
330 // frustum->getBot(), frustum->getTop(),
331 // frustum->getNear(), frustum->getFar());
333 /* Prepare ppm output file */
334 while (count < 1000) {
335 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
336 if ( (f = fopen(filename, "r")) == NULL )
340 f = fopen(filename, "wb");
342 printf("Couldn't open image file: %s\n", filename);
348 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
349 fprintf(f,"%i %i\n", imageWidth, imageHeight);
352 /* just to be safe... */
353 glPixelStorei(GL_PACK_ALIGNMENT, 1);
355 /* Because the HUD and Panel change the ViewPort we will
356 * need to handle some lowlevel stuff ourselves */
357 int ncols = trGet(tr, TR_COLUMNS);
358 int nrows = trGet(tr, TR_ROWS);
360 bool do_hud = fgGetBool("/sim/hud/visibility");
361 GLfloat hud_col_step = 640.0 / ncols;
362 GLfloat hud_row_step = 480.0 / nrows;
364 bool do_panel = fgPanelVisible();
365 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
366 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
368 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
369 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
370 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
371 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
372 glHint(GL_FOG_HINT, GL_NICEST);
378 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
379 int curRow = trGet(tr, TR_CURRENT_ROW);
381 renderer->update( false );
384 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
385 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
388 // globals->get_current_panel()->update(
389 // curColumn*panel_col_step, panel_col_step,
390 // curRow*panel_row_step, panel_row_step );
391 more = trEndTile(tr);
393 /* save tile into tile row buffer*/
394 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
395 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
396 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
397 int xOffset = curColumn * bytesPerTileRow;
398 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
400 for (i=0;i<height;i++) {
401 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
402 tile + i*bytesPerTileRow, /* Src */
403 bytesPerCurrentTileRow); /* Byte count*/
406 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
407 /* write this buffered row of tiles to the file */
408 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
409 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
411 for (i=0;i<curTileHeight;i++) {
412 /* Remember, OpenGL images are bottom to top. Have to reverse. */
413 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
414 fwrite(rowPtr, 1, imageWidth*3, f);
420 renderer->resize( width, height );
424 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
425 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
426 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
427 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
428 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
429 (!fgGetBool("/sim/rendering/shading"))) {
430 // if fastest fog requested, or if flat shading force fastest
431 glHint ( GL_FOG_HINT, GL_FASTEST );
432 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
433 glHint ( GL_FOG_HINT, GL_DONT_CARE );
438 message = "Snapshot saved to \"";
441 mkDialog (message.c_str());
448 if ( show_pu_cursor ) {
453 fgSetBool("/sim/menubar/visibility", menu_status);
456 fgSetBool("/sim/freeze/master", false);
459 #endif // #if defined( TR_HIRES_SNAP)
462 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
464 void rotateView( double roll, double pitch, double yaw )
470 GLubyte *hiResScreenCapture( int multiplier )
472 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
473 float fov = oldfov / multiplier;
474 FGViewer *v = globals->get_current_view();
475 fgSetDouble("/sim/current-view/field-of-view", fov);
476 // globals->get_renderer()->init();
477 int cur_width = fgGetInt("/sim/startup/xsize");
478 int cur_height = fgGetInt("/sim/startup/ysize");
480 // New empty (mostly) bitmap
481 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
483 for ( y = 0; y < multiplier; y++ ) {
484 for ( x = 0; x < multiplier; x++ ) {
485 globals->get_renderer()->resize( cur_width, cur_height );
487 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
488 globals->get_renderer()->update( false );
491 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
494 fgSetDouble("/sim/current-view/field-of-view", oldfov);
495 return b1->getBitmap();
499 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
500 // win32 print screen function
501 void printScreen ( puObject *obj ) {
502 bool show_pu_cursor = false;
504 if ( !puCursorIsHidden() ) {
505 show_pu_cursor = true;
510 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
511 int cur_width = fgGetInt("/sim/startup/xsize");
512 int cur_height = fgGetInt("/sim/startup/ysize");
513 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
514 p.End( hiResScreenCapture(3) );
517 if ( show_pu_cursor ) {
522 #endif // #ifdef WIN32
525 void fgDumpSnapShotWrapper ( puObject *obj ) {
530 void fgHiResDumpWrapper ( puObject *obj ) {
535 // do a screen snap shot
536 void fgDumpSnapShot () {
537 bool show_pu_cursor = false;
538 char *filename = new char [24];
540 static int count = 1;
542 static const SGPropertyNode *master_freeze
543 = fgGetNode("/sim/freeze/master");
545 bool freeze = master_freeze->getBoolValue();
547 fgSetBool("/sim/freeze/master", true);
551 if ( !puCursorIsHidden() ) {
552 show_pu_cursor = true;
555 fgSetBool("/sim/signals/screenshot", true);
557 FGRenderer *renderer = globals->get_renderer();
559 renderer->resize( fgGetInt("/sim/startup/xsize"),
560 fgGetInt("/sim/startup/ysize") );
562 // we need two render frames here to clear the menu and cursor
563 // ... not sure why but doing an extra fgRenderFrame() shouldn't
565 renderer->update( true );
566 renderer->update( true );
568 while (count < 1000) {
570 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
571 if ( (fp = fopen(filename, "r")) == NULL )
576 if ( sg_glDumpWindow( filename,
577 fgGetInt("/sim/startup/xsize"),
578 fgGetInt("/sim/startup/ysize")) ) {
579 message = "Snapshot saved to \"";
583 message = "Failed to save to \"";
588 fgSetBool("/sim/signals/screenshot", false);
589 mkDialog (message.c_str());
593 if ( show_pu_cursor ) {
600 fgSetBool("/sim/freeze/master", false);