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 <Aircraft/controls.hxx>
73 #include <Airports/simple.hxx>
74 #include <Autopilot/auto_gui.hxx>
75 #include <Cockpit/panel.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"
101 extern void fgLatLonFormatToggle( puObject *);
103 #if defined( TR_HIRES_SNAP)
104 #include <simgear/screen/tr.h>
105 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
106 GLfloat x_end, GLfloat y_end );
110 // TODO: remove after the last hardcoded dialog has died
111 char *gui_msg_OK = "OK";
112 char *gui_msg_NO = "NO";
113 char *gui_msg_YES = "YES";
114 char *gui_msg_CANCEL = "CANCEL";
115 char *gui_msg_RESET = "RESET";
118 const __fg_gui_fn_t __fg_gui_fn[] = {
123 {"dumpHiResSnapShot", fgHiResDumpWrapper},
125 {"dumpSnapShot", fgDumpSnapShotWrapper},
126 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
127 {"printScreen", printScreen},
131 {"guiTogglePanel", guiTogglePanel},
132 {"prop_pickerView", prop_pickerView},
135 {"fgPresetAirport", fgPresetAirport},
136 {"fgPresetRunway", fgPresetRunway},
137 {"fgPresetOffsetDistance", fgPresetOffsetDistance},
138 {"fgPresetAltitude", fgPresetAltitude},
139 {"fgPresetGlideslope", fgPresetGlideslope},
140 {"fgPresetAirspeed", fgPresetAirspeed},
141 {"fgPresetCommit", fgPresetCommit},
144 {"NewAltitude", NewAltitude},
145 {"NewHeading", NewHeading},
146 {"AddWayPoint", AddWayPoint},
147 {"PopWayPoint", PopWayPoint},
148 {"ClearRoute", ClearRoute},
149 {"fgLatLonFormatToggle", fgLatLonFormatToggle},
154 // Structure termination
159 /* ================ General Purpose Functions ================ */
161 // General Purpose Message Box
162 void mkDialog (const char *txt)
164 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
167 SGPropertyNode_ptr dlg = gui->getDialog("message");
171 dlg->setStringValue("text/label", txt);
172 dlg->setStringValue("button/legend", "OK");
173 gui->showDialog("message");
176 // Message Box to report an error.
177 void guiErrorMessage (const char *txt)
179 SG_LOG(SG_GENERAL, SG_ALERT, txt);
183 // Message Box to report a throwable (usually an exception).
184 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
188 msg += throwable.getFormattedMessage();
189 if (!throwable.getOrigin().empty()) {
190 msg += "\n (reported by ";
191 msg += throwable.getOrigin();
194 SG_LOG(SG_GENERAL, SG_ALERT, msg);
195 mkDialog(msg.c_str());
200 /* -----------------------------------------------------------------------
201 the Gui callback functions
202 ____________________________________________________________________*/
205 // Hier Neu :-) This is my newly added code
206 // Added by David Findlay <nedz@bigpond.com>
207 // on Sunday 3rd of December
210 // This is the accessor function
211 void guiTogglePanel(puObject *cb)
213 if (fgGetBool("/sim/panel/visibility"))
214 fgSetBool("/sim/panel/visibility", false);
216 fgSetBool("/sim/panel/visibility", true);
218 globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
219 fgGetInt("/sim/startup/ysize"));
222 void helpCb (puObject *)
226 #if defined(FX) && !defined(WIN32)
227 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
228 if ( globals->get_fullscreen() ) {
229 globals->set_fullscreen(false);
230 XMesaSetFXmode( XMESA_FX_WINDOW );
235 SGPath path( globals->get_fg_root() );
236 path.append( "Docs/index.html" );
240 string help_app = fgGetString("/sim/startup/browser-app");
242 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
243 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
245 command = help_app + " " + path.str();
248 system( command.c_str() );
252 // Look for favorite browser
253 char Dummy[1024], ExecName[1024], browserParameter[1024];
254 char win32_name[1024];
256 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
258 strncpy(win32_name,path.c_str(), 1024);
261 FindExecutable(win32_name, Dummy, ExecName);
262 snprintf(browserParameter, 1024, "file:///%s", win32_name);
263 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
268 mkDialog ("Help started in your web browser window.");
271 #if defined( TR_HIRES_SNAP)
276 bool show_pu_cursor = false;
277 bool menu_status = fgGetBool("/sim/menubar/visibility");
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);
289 fgSetBool("/sim/menubar/visibility", false);
291 if ( !puCursorIsHidden() ) {
292 show_pu_cursor = true;
296 FGRenderer *renderer = globals->get_renderer();
298 renderer->resize( fgGetInt("/sim/startup/xsize"),
299 fgGetInt("/sim/startup/ysize") );
301 // we need two render frames here to clear the menu and cursor
302 // ... not sure why but doing an extra fgRenderFrame() shouldn't
304 //renderer->update( true );
305 //renderer->update( true );
307 // This ImageSize stuff is a temporary hack
308 // should probably use 128x128 tile size and
309 // support any image size
311 // This should be a requester to get multiplier from user
312 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
313 int width = fgGetInt("/sim/startup/xsize");
314 int height = fgGetInt("/sim/startup/ysize");
316 /* allocate buffer large enough to store one tile */
317 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
319 printf("Malloc of tile buffer failed!\n");
323 int imageWidth = multiplier*width;
324 int imageHeight = multiplier*height;
326 /* allocate buffer to hold a row of tiles */
328 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
331 printf("Malloc of tile row buffer failed!\n");
334 TRcontext *tr = trNew();
335 trTileSize(tr, width, height, 0);
336 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
337 trImageSize(tr, imageWidth, imageHeight);
338 trRowOrder(tr, TR_TOP_TO_BOTTOM);
339 sgFrustum *frustum = ssgGetFrustum();
341 frustum->getLeft(), frustum->getRight(),
342 frustum->getBot(), frustum->getTop(),
343 frustum->getNear(), frustum->getFar());
345 /* Prepare ppm output file */
346 while (count < 1000) {
347 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
348 if ( (f = fopen(filename, "r")) == NULL )
352 f = fopen(filename, "wb");
354 printf("Couldn't open image file: %s\n", filename);
360 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
361 fprintf(f,"%i %i\n", imageWidth, imageHeight);
364 /* just to be safe... */
365 glPixelStorei(GL_PACK_ALIGNMENT, 1);
367 /* Because the HUD and Panel change the ViewPort we will
368 * need to handle some lowlevel stuff ourselves */
369 int ncols = trGet(tr, TR_COLUMNS);
370 int nrows = trGet(tr, TR_ROWS);
372 bool do_hud = fgGetBool("/sim/hud/visibility");
373 GLfloat hud_col_step = 640.0 / ncols;
374 GLfloat hud_row_step = 480.0 / nrows;
376 bool do_panel = fgPanelVisible();
377 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
378 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
384 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
385 int curRow = trGet(tr, TR_CURRENT_ROW);
386 renderer->update( false );
388 fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
389 (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
391 globals->get_current_panel()->update(
392 curColumn*panel_col_step, panel_col_step,
393 curRow*panel_row_step, panel_row_step );
394 more = trEndTile(tr);
396 /* save tile into tile row buffer*/
397 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
398 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
399 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
400 int xOffset = curColumn * bytesPerTileRow;
401 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
403 for (i=0;i<height;i++) {
404 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
405 tile + i*bytesPerTileRow, /* Src */
406 bytesPerCurrentTileRow); /* Byte count*/
409 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
410 /* write this buffered row of tiles to the file */
411 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
412 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
414 for (i=0;i<curTileHeight;i++) {
415 /* Remember, OpenGL images are bottom to top. Have to reverse. */
416 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
417 fwrite(rowPtr, 1, imageWidth*3, f);
423 renderer->resize( width, height );
429 message = "Snapshot saved to \"";
432 mkDialog (message.c_str());
439 if ( show_pu_cursor ) {
444 fgSetBool("/sim/menubar/visibility", menu_status);
447 fgSetBool("/sim/freeze/master", false);
450 #endif // #if defined( TR_HIRES_SNAP)
453 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
455 void rotateView( double roll, double pitch, double yaw )
461 GLubyte *hiResScreenCapture( int multiplier )
463 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
464 float fov = oldfov / multiplier;
465 FGViewer *v = globals->get_current_view();
466 fgSetDouble("/sim/current-view/field-of-view", fov);
467 globals->get_renderer()->init();
468 int cur_width = fgGetInt("/sim/startup/xsize");
469 int cur_height = fgGetInt("/sim/startup/ysize");
471 // New empty (mostly) bitmap
472 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
474 for ( y = 0; y < multiplier; y++ ) {
475 for ( x = 0; x < multiplier; x++ ) {
476 globals->get_renderer()->resize( cur_width, cur_height );
478 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
479 globals->get_renderer()->update( false );
482 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
485 fgSetDouble("/sim/current-view/field-of-view", oldfov);
486 return b1->getBitmap();
490 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
491 // win32 print screen function
492 void printScreen ( puObject *obj ) {
493 bool show_pu_cursor = false;
495 if ( !puCursorIsHidden() ) {
496 show_pu_cursor = true;
501 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
502 int cur_width = fgGetInt("/sim/startup/xsize");
503 int cur_height = fgGetInt("/sim/startup/ysize");
504 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
505 p.End( hiResScreenCapture(3) );
508 if ( show_pu_cursor ) {
513 #endif // #ifdef WIN32
516 void fgDumpSnapShotWrapper ( puObject *obj ) {
521 void fgHiResDumpWrapper ( puObject *obj ) {
526 // do a screen snap shot
527 void fgDumpSnapShot () {
528 bool show_pu_cursor = false;
529 char *filename = new char [24];
531 static int count = 1;
533 static const SGPropertyNode *master_freeze
534 = fgGetNode("/sim/freeze/master");
536 bool freeze = master_freeze->getBoolValue();
538 fgSetBool("/sim/freeze/master", true);
542 if ( !puCursorIsHidden() ) {
543 show_pu_cursor = true;
547 FGRenderer *renderer = globals->get_renderer();
549 renderer->resize( fgGetInt("/sim/startup/xsize"),
550 fgGetInt("/sim/startup/ysize") );
552 // we need two render frames here to clear the menu and cursor
553 // ... not sure why but doing an extra fgRenderFrame() shouldn't
555 renderer->update( true );
556 renderer->update( true );
558 while (count < 1000) {
560 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
561 if ( (fp = fopen(filename, "r")) == NULL )
566 if ( sg_glDumpWindow( filename,
567 fgGetInt("/sim/startup/xsize"),
568 fgGetInt("/sim/startup/ysize")) ) {
569 message = "Snapshot saved to \"";
573 message = "Failed to save to \"";
578 mkDialog (message.c_str());
582 if ( show_pu_cursor ) {
589 fgSetBool("/sim/freeze/master", false);