]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
Added two missing files from JSBSim.org that were missing in the last sync.
[flightgear.git] / src / GUI / gui_funcs.cxx
1 /**************************************************************************
2  * gui_funcs.cxx
3  *
4  * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
5  *
6  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
7  * project.
8  *
9  * Additional mouse supported added by David Megginson, 1999.
10  *
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.
15  *
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.
20  *
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.
24  *
25  * $Id$
26  **************************************************************************/
27
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #include <fstream>
36 #include <string>
37 #include <cstring>
38 #include <sstream>
39
40 #include <stdlib.h>
41
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/screen/screen-dump.hxx>
45
46 #include <Cockpit/panel.hxx>
47 #include <Main/globals.hxx>
48 #include <Main/fg_props.hxx>
49 #include <Main/fg_os.hxx>
50 #include <Main/renderer.hxx>
51 #include <Main/viewmgr.hxx>
52 #include <GUI/new_gui.hxx>
53
54 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
55 #  include <simgear/screen/win32-printer.h>
56 #  include <simgear/screen/GlBitmaps.h>
57 #endif
58 #ifdef __MINGW32__
59 #include <shellapi.h>
60 #endif
61 #include "gui.h"
62
63 using std::string;
64
65
66 #if defined( TR_HIRES_SNAP)
67 #include <simgear/screen/tr.h>
68 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
69                          GLfloat x_end, GLfloat y_end );
70 #endif
71
72
73 const __fg_gui_fn_t __fg_gui_fn[] = {
74 #ifdef TR_HIRES_SNAP
75         {"dumpHiResSnapShot", fgHiResDumpWrapper},
76 #endif
77         {"dumpSnapShot", fgDumpSnapShotWrapper},
78 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
79         {"printScreen", printScreen},
80 #endif
81         // Help
82         {"helpCb", helpCb},
83
84         // Structure termination
85         {"", NULL}
86 };
87
88
89 /* ================ General Purpose Functions ================ */
90
91 // General Purpose Message Box. Makes sure no more than 5 different
92 // messages are displayed at the same time, and none of them are
93 // duplicates. (5 is a *lot*, but this will hardly ever be reached
94 // and we don't want to miss any, either.)
95 void mkDialog (const char *txt)
96 {
97     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
98     if (!gui)
99         return;
100     SGPropertyNode *master = gui->getDialogProperties("message");
101     if (!master)
102         return;
103
104     const int maxdialogs = 5;
105     string name;
106     SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
107     int i;
108     for (i = 0; i < maxdialogs; i++) {
109         std::ostringstream s;
110         s << "message-" << i;
111         name = s.str();
112
113         if (!msg->getNode(name.c_str(), false))
114             break;
115
116         if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
117             SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
118             return;
119         }
120     }
121     if (i == maxdialogs)
122         return;
123     msg = msg->getNode(name.c_str(), true);
124     msg->setStringValue("message", txt);
125     msg = msg->getNode("dialog", true);
126     copyProperties(master, msg);
127     msg->setStringValue("name", name.c_str());
128     gui->newDialog(msg);
129     gui->showDialog(name.c_str());
130 }
131
132 // Message Box to report an error.
133 void guiErrorMessage (const char *txt)
134 {
135     SG_LOG(SG_GENERAL, SG_ALERT, txt);
136     mkDialog(txt);
137 }
138
139 // Message Box to report a throwable (usually an exception).
140 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
141 {
142     string msg = txt;
143     msg += '\n';
144     msg += throwable.getFormattedMessage();
145     if (!std::strlen(throwable.getOrigin()) != 0) {
146         msg += "\n (reported by ";
147         msg += throwable.getOrigin();
148         msg += ')';
149     }
150     SG_LOG(SG_GENERAL, SG_ALERT, msg);
151     mkDialog(msg.c_str());
152 }
153
154
155
156 /* -----------------------------------------------------------------------
157 the Gui callback functions 
158 ____________________________________________________________________*/
159
160
161 // Hier Neu :-) This is my newly added code
162 // Added by David Findlay <nedz@bigpond.com>
163 // on Sunday 3rd of December
164
165
166 void helpCb ()
167 {
168     string command;
169         
170     SGPath path( globals->get_fg_root() );
171     path.append( "Docs/index.html" );
172         
173 #if !defined(WIN32)
174
175     command = globals->get_browser();
176     string::size_type pos;
177     if ((pos = command.find("%u", 0)) != string::npos)
178         command.replace(pos, 2, path.str());
179     else
180         command += " " + path.str();
181
182     command += " &";
183     system( command.c_str() );
184
185 #else // WIN32
186
187     // Look for favorite browser
188     char Dummy[1024], ExecName[1024], browserParameter[1024];
189     char win32_name[1024];
190 # ifdef __CYGWIN__
191     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
192 # else
193     strncpy(win32_name,path.c_str(), 1024);
194 # endif
195     Dummy[0] = 0;
196     FindExecutable(win32_name, Dummy, ExecName);
197     snprintf(browserParameter, 1024, "file:///%s", win32_name);
198     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
199                    SW_SHOWNORMAL ) ;
200
201 #endif
202         
203     mkDialog ("Help started in your web browser window.");
204 }
205
206 #if defined( TR_HIRES_SNAP)
207 void fgHiResDump()
208 {
209     FILE *f;
210     string message;
211     bool menu_status = fgGetBool("/sim/menubar/visibility");
212     char *filename = new char [24];
213     static int count = 1;
214
215     static const SGPropertyNode *master_freeze
216         = fgGetNode("/sim/freeze/master");
217
218     bool freeze = master_freeze->getBoolValue();
219     if ( !freeze ) {
220         fgSetBool("/sim/freeze/master", true);
221     }
222
223     fgSetBool("/sim/menubar/visibility", false);
224     int mouse = fgGetMouseCursor();
225     fgSetMouseCursor(MOUSE_CURSOR_NONE);
226
227     FGRenderer *renderer = globals->get_renderer();
228 //     renderer->init();
229     renderer->resize( fgGetInt("/sim/startup/xsize"),
230                       fgGetInt("/sim/startup/ysize") );
231
232     // we need two render frames here to clear the menu and cursor
233     // ... not sure why but doing an extra fgRenderFrame() shouldn't
234     // hurt anything
235     //renderer->update( true );
236     //renderer->update( true );
237
238     // This ImageSize stuff is a temporary hack
239     // should probably use 128x128 tile size and
240     // support any image size
241
242     // This should be a requester to get multiplier from user
243     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
244     int width = fgGetInt("/sim/startup/xsize");
245     int height = fgGetInt("/sim/startup/ysize");
246         
247     /* allocate buffer large enough to store one tile */
248     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
249     if (!tile) {
250         delete [] filename;
251         printf("Malloc of tile buffer failed!\n");
252         return;
253     }
254
255     int imageWidth  = multiplier*width;
256     int imageHeight = multiplier*height;
257
258     /* allocate buffer to hold a row of tiles */
259     GLubyte *buffer
260         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
261     if (!buffer) {
262         delete [] filename;
263         free(tile);
264         printf("Malloc of tile row buffer failed!\n");
265         return;
266     }
267     TRcontext *tr = trNew();
268     trTileSize(tr, width, height, 0);
269     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
270     trImageSize(tr, imageWidth, imageHeight);
271     trRowOrder(tr, TR_TOP_TO_BOTTOM);
272     // OSGFIXME
273 //     sgFrustum *frustum = ssgGetFrustum();
274 //     trFrustum(tr,
275 //               frustum->getLeft(), frustum->getRight(),
276 //               frustum->getBot(),  frustum->getTop(), 
277 //               frustum->getNear(), frustum->getFar());
278         
279     /* Prepare ppm output file */
280     while (count < 1000) {
281         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
282         if ( (f = fopen(filename, "r")) == NULL )
283             break;
284         fclose(f);
285     }
286     f = fopen(filename, "wb");
287     if (!f) {
288         printf("Couldn't open image file: %s\n", filename);
289         delete [] filename;
290         free(buffer);
291         free(tile);
292         return;
293     }
294     fprintf(f,"P6\n");
295     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
296     fprintf(f,"%i %i\n", imageWidth, imageHeight);
297     fprintf(f,"255\n");
298
299     /* just to be safe... */
300     glPixelStorei(GL_PACK_ALIGNMENT, 1);
301
302     // OSGFIXME
303 #if 0
304     /* Because the HUD and Panel change the ViewPort we will
305      * need to handle some lowlevel stuff ourselves */
306     int ncols = trGet(tr, TR_COLUMNS);
307     int nrows = trGet(tr, TR_ROWS);
308
309     bool do_hud = fgGetBool("/sim/hud/visibility");
310     GLfloat hud_col_step = 640.0 / ncols;
311     GLfloat hud_row_step = 480.0 / nrows;
312         
313     bool do_panel = fgPanelVisible();
314     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
315     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
316 #endif
317     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
318     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
319     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
320     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
321     glHint(GL_FOG_HINT, GL_NICEST);
322         
323     /* Draw tiles */
324     int more = 1;
325     while (more) {
326         trBeginTile(tr);
327         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
328         // int curRow =  trGet(tr, TR_CURRENT_ROW);
329
330         renderer->update( false );
331         // OSGFIXME
332 //         if ( do_hud )
333 //             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
334 //                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
335         // OSGFIXME
336 //         if (do_panel)
337 //             globals->get_current_panel()->update(
338 //                                    curColumn*panel_col_step, panel_col_step,
339 //                                    curRow*panel_row_step,    panel_row_step );
340         more = trEndTile(tr);
341
342         /* save tile into tile row buffer*/
343         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
344         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
345         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
346         int xOffset = curColumn * bytesPerTileRow;
347         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
348         int i;
349         for (i=0;i<height;i++) {
350             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
351                    tile + i*bytesPerTileRow,              /* Src */
352                    bytesPerCurrentTileRow);               /* Byte count*/
353         }
354
355         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
356             /* write this buffered row of tiles to the file */
357             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
358             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
359             int i;
360             for (i=0;i<curTileHeight;i++) {
361                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
362                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
363                 fwrite(rowPtr, 1, imageWidth*3, f);
364             }
365         }
366
367     }
368
369     renderer->resize( width, height );
370
371     trDelete(tr);
372
373     glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
374     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
375     glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
376     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
377     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
378          (!fgGetBool("/sim/rendering/shading"))) {
379         // if fastest fog requested, or if flat shading force fastest
380         glHint ( GL_FOG_HINT, GL_FASTEST );
381     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
382         glHint ( GL_FOG_HINT, GL_DONT_CARE );
383     }
384
385     fclose(f);
386
387     message = "Snapshot saved to \"";
388     message += filename;
389     message += "\".";
390     mkDialog (message.c_str());
391
392     free(tile);
393     free(buffer);
394
395     delete [] filename;
396
397     fgSetMouseCursor(mouse);
398     fgSetBool("/sim/menubar/visibility", menu_status);
399
400     if ( !freeze ) {
401         fgSetBool("/sim/freeze/master", false);
402     }
403 }
404 #endif // #if defined( TR_HIRES_SNAP)
405
406
407 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
408
409 void rotateView( double roll, double pitch, double yaw )
410 {
411         // rotate view
412 }
413
414 GlBitmap *b1 = NULL;
415 GLubyte *hiResScreenCapture( int multiplier )
416 {
417     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
418     float fov = oldfov / multiplier;
419     FGViewer *v = globals->get_current_view();
420     fgSetDouble("/sim/current-view/field-of-view", fov);
421 //     globals->get_renderer()->init();
422     int cur_width = fgGetInt("/sim/startup/xsize");
423     int cur_height = fgGetInt("/sim/startup/ysize");
424     delete( b1 );
425     // New empty (mostly) bitmap
426     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
427     int x,y;
428     for ( y = 0; y < multiplier; y++ ) {
429         for ( x = 0; x < multiplier; x++ ) {
430             globals->get_renderer()->resize( cur_width, cur_height );
431             // pan to tile
432             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
433             globals->get_renderer()->update( false );
434             // restore view
435             GlBitmap b2;
436             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
437         }
438     }
439     fgSetDouble("/sim/current-view/field-of-view", oldfov);
440     return b1->getBitmap();
441 }
442 #endif
443
444 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
445 // win32 print screen function
446 void printScreen () {
447     int mouse = fgGetMouseCursor();
448     fgSetMouseCursor(MOUSE_CURSOR_NONE);
449
450     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
451     int cur_width = fgGetInt("/sim/startup/xsize");
452     int cur_height = fgGetInt("/sim/startup/ysize");
453     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
454     p.End( hiResScreenCapture(3) );
455
456     fgSetMouseCursor(mouse);
457 }
458 #endif // #ifdef WIN32
459
460
461 void fgDumpSnapShotWrapper () {
462     fgDumpSnapShot();
463 }
464
465
466 void fgHiResDumpWrapper () {
467     fgHiResDump();
468 }
469
470
471 // do a screen snap shot
472 bool fgDumpSnapShot () {
473     static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
474
475     bool freeze = master_freeze->getBoolValue();
476     if ( !freeze ) {
477         fgSetBool("/sim/freeze/master", true);
478     }
479
480     int mouse = fgGetMouseCursor();
481     fgSetMouseCursor(MOUSE_CURSOR_NONE);
482
483     fgSetBool("/sim/signals/screenshot", true);
484
485     FGRenderer *renderer = globals->get_renderer();
486     renderer->resize( fgGetInt("/sim/startup/xsize"),
487                       fgGetInt("/sim/startup/ysize") );
488
489     // we need two render frames here to clear the menu and cursor
490     // ... not sure why but doing an extra fgRenderFrame() shouldn't
491     // hurt anything
492     renderer->update( true );
493     renderer->update( true );
494
495     string dir = fgGetString("/sim/paths/screenshot-dir");
496     if (dir.empty())
497         dir = fgGetString("/sim/fg-current");
498
499     SGPath path(dir + '/');
500     if (path.create_dir( 0755 )) {
501         SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
502                 << dir << "'. Trying home directory.");
503         dir = fgGetString("/sim/fg-home");
504     }
505
506     char filename[24];
507     static int count = 1;
508     while (count < 1000) {
509         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
510
511         SGPath p(dir);
512         p.append(filename);
513         if (!p.exists()) {
514             path.set(p.str());
515             break;
516         }
517     }
518
519     int result = sg_glDumpWindow(path.c_str(),
520                                  fgGetInt("/sim/startup/xsize"),
521                                  fgGetInt("/sim/startup/ysize"));
522
523     fgSetString("/sim/paths/screenshot-last", path.c_str());
524     fgSetBool("/sim/signals/screenshot", false);
525
526     fgSetMouseCursor(mouse);
527
528     if ( !freeze ) {
529         fgSetBool("/sim/freeze/master", false);
530     }
531     return result != 0;
532 }
533
534 // do an entire scenegraph dump
535 void fgDumpSceneGraph()
536 {
537     char *filename = new char [24];
538     string message;
539     static int count = 1;
540
541     static const SGPropertyNode *master_freeze
542         = fgGetNode("/sim/freeze/master");
543
544     bool freeze = master_freeze->getBoolValue();
545     if ( !freeze ) {
546         fgSetBool("/sim/freeze/master", true);
547     }
548
549     while (count < 1000) {
550         FILE *fp;
551         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
552         if ( (fp = fopen(filename, "r")) == NULL )
553             break;
554         fclose(fp);
555     }
556
557     if ( fgDumpSceneGraphToFile(filename)) {
558         message = "Entire scene graph saved to \"";
559         message += filename;
560         message += "\".";
561     } else {
562         message = "Failed to save to \"";
563         message += filename;
564         message += "\".";
565     }
566
567     mkDialog (message.c_str());
568
569     delete [] filename;
570
571     if ( !freeze ) {
572         fgSetBool("/sim/freeze/master", false);
573     }
574 }
575
576     
577 // do an terrain branch dump
578 void fgDumpTerrainBranch()
579 {
580     char *filename = new char [24];
581     string message;
582     static int count = 1;
583
584     static const SGPropertyNode *master_freeze
585         = fgGetNode("/sim/freeze/master");
586
587     bool freeze = master_freeze->getBoolValue();
588     if ( !freeze ) {
589         fgSetBool("/sim/freeze/master", true);
590     }
591
592     while (count < 1000) {
593         FILE *fp;
594         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
595         if ( (fp = fopen(filename, "r")) == NULL )
596             break;
597         fclose(fp);
598     }
599
600     if ( fgDumpTerrainBranchToFile(filename)) {
601         message = "Terrain graph saved to \"";
602         message += filename;
603         message += "\".";
604     } else {
605         message = "Failed to save to \"";
606         message += filename;
607         message += "\".";
608     }
609
610     mkDialog (message.c_str());
611
612     delete [] filename;
613
614     if ( !freeze ) {
615         fgSetBool("/sim/freeze/master", false);
616     }
617 }
618
619     
620