]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
- limit max number of messages displayed at the same time to 5 (Ideally,
[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 #ifdef SG_MATH_EXCEPTION_CLASH
36 #  include <math.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #  include <windows.h>
41 #endif
42
43 #include SG_GL_H
44
45 #if defined(FX) && defined(XMESA)
46 #  include <GL/xmesa.h>
47 #endif
48
49 #include STL_FSTREAM
50 #include STL_STRING
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 // for help call back
56 #ifdef WIN32
57 # include <shellapi.h>
58 # ifdef __CYGWIN__
59 #  include <sys/cygwin.h>
60 # endif
61 #endif
62
63 #include <sstream>
64
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>
69
70 #include <Include/general.hxx>
71 #include <Aircraft/aircraft.hxx>
72 #include <Aircraft/controls.hxx>
73 #include <Airports/simple.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <FDM/flight.hxx>
76 #include <Main/main.hxx>
77 #include <Main/fg_init.hxx>
78 #include <Main/fg_io.hxx>
79 #include <Main/globals.hxx>
80 #include <Main/fg_props.hxx>
81 #include <Main/renderer.hxx>
82 #include <Main/viewmgr.hxx>
83 #include <GUI/new_gui.hxx>
84
85 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
86 #  include <simgear/screen/win32-printer.h>
87 #  include <simgear/screen/GlBitmaps.h>
88 #endif
89
90 #include "gui.h"
91 #include "gui_local.hxx"
92 #include "sgVec3Slider.hxx"
93
94 SG_USING_STD(string);
95 SG_USING_STD(cout);
96
97
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 );
102 #endif
103
104
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";
111
112
113 const __fg_gui_fn_t __fg_gui_fn[] = {
114
115         // File
116         {"reInit", reInit},
117 #ifdef TR_HIRES_SNAP
118         {"dumpHiResSnapShot", fgHiResDumpWrapper},
119 #endif
120         {"dumpSnapShot", fgDumpSnapShotWrapper},
121 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
122         {"printScreen", printScreen},
123 #endif
124         // Help
125         {"helpCb", helpCb},
126
127         // Structure termination
128         {"", NULL}
129 };
130
131
132 /* ================ General Purpose Functions ================ */
133
134 // General Purpose Message Box. Makes sure no more than 5 different
135 // messages are displayed at the same time, and none of them are
136 // duplicates. (5 is a *lot*, but this will hardly ever be reached
137 // and we don't want to miss any, either.)
138 void mkDialog (const char *txt)
139 {
140     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
141     if (!gui)
142         return;
143     SGPropertyNode *master = gui->getDialogProperties("message");
144     if (!master)
145         return;
146
147     const int maxdialogs = 5;
148     string name;
149     SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
150     int i;
151     for (i = 0; i < maxdialogs; i++) {
152         std::ostringstream s;
153         s << "message-" << i;
154         name = s.str();
155
156         if (!msg->getNode(name.c_str(), false))
157             break;
158
159         if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
160             SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
161             return;
162         }
163     }
164     if (i == maxdialogs)
165         return;
166     msg = msg->getNode(name.c_str(), true);
167     msg->setStringValue("message", txt);
168     msg = msg->getNode("dialog", true);
169     copyProperties(master, msg);
170     msg->setStringValue("name", name.c_str());
171     gui->newDialog(msg);
172     gui->showDialog(name.c_str());
173 }
174
175 // Message Box to report an error.
176 void guiErrorMessage (const char *txt)
177 {
178     SG_LOG(SG_GENERAL, SG_ALERT, txt);
179     mkDialog(txt);
180 }
181
182 // Message Box to report a throwable (usually an exception).
183 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
184 {
185     string msg = txt;
186     msg += '\n';
187     msg += throwable.getFormattedMessage();
188     if (!throwable.getOrigin().empty()) {
189         msg += "\n (reported by ";
190         msg += throwable.getOrigin();
191         msg += ')';
192     }
193     SG_LOG(SG_GENERAL, SG_ALERT, msg);
194     mkDialog(msg.c_str());
195 }
196
197
198
199 /* -----------------------------------------------------------------------
200 the Gui callback functions 
201 ____________________________________________________________________*/
202
203
204 // Hier Neu :-) This is my newly added code
205 // Added by David Findlay <nedz@bigpond.com>
206 // on Sunday 3rd of December
207
208
209 void helpCb (puObject *)
210 {
211     string command;
212         
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 );
218     }
219 #  endif
220 #endif
221         
222     SGPath path( globals->get_fg_root() );
223     path.append( "Docs/index.html" );
224         
225 #if !defined(WIN32)
226
227     string help_app = fgGetString("/sim/startup/browser-app");
228
229     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
230         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
231     } else {
232         command = help_app + " " + path.str();
233     }
234     command += " &";
235     system( command.c_str() );
236
237 #else // WIN32
238
239     // Look for favorite browser
240     char Dummy[1024], ExecName[1024], browserParameter[1024];
241     char win32_name[1024];
242 # ifdef __CYGWIN__
243     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
244 # else
245     strncpy(win32_name,path.c_str(), 1024);
246 # endif
247     Dummy[0] = 0;
248     FindExecutable(win32_name, Dummy, ExecName);
249     snprintf(browserParameter, 1024, "file:///%s", win32_name);
250     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
251                    SW_SHOWNORMAL ) ;
252
253 #endif
254         
255     mkDialog ("Help started in your web browser window.");
256 }
257
258 #if defined( TR_HIRES_SNAP)
259 void fgHiResDump()
260 {
261     FILE *f;
262     string message;
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;
267
268     static const SGPropertyNode *master_freeze
269         = fgGetNode("/sim/freeze/master");
270
271     bool freeze = master_freeze->getBoolValue();
272     if ( !freeze ) {
273         fgSetBool("/sim/freeze/master", true);
274     }
275
276     fgSetBool("/sim/menubar/visibility", false);
277     TurnCursorOff();
278     if ( !puCursorIsHidden() ) {
279         show_pu_cursor = true;
280         puHideCursor();
281     }
282
283     FGRenderer *renderer = globals->get_renderer();
284 //     renderer->init();
285     renderer->resize( fgGetInt("/sim/startup/xsize"),
286                       fgGetInt("/sim/startup/ysize") );
287
288     // we need two render frames here to clear the menu and cursor
289     // ... not sure why but doing an extra fgRenderFrame() shouldn't
290     // hurt anything
291     //renderer->update( true );
292     //renderer->update( true );
293
294     // This ImageSize stuff is a temporary hack
295     // should probably use 128x128 tile size and
296     // support any image size
297
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");
302         
303     /* allocate buffer large enough to store one tile */
304     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
305     if (!tile) {
306         printf("Malloc of tile buffer failed!\n");
307         return;
308     }
309
310     int imageWidth  = multiplier*width;
311     int imageHeight = multiplier*height;
312
313     /* allocate buffer to hold a row of tiles */
314     GLubyte *buffer
315         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
316     if (!buffer) {
317         free(tile);
318         printf("Malloc of tile row buffer failed!\n");
319         return;
320     }
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);
326     // OSGFIXME
327 //     sgFrustum *frustum = ssgGetFrustum();
328 //     trFrustum(tr,
329 //               frustum->getLeft(), frustum->getRight(),
330 //               frustum->getBot(),  frustum->getTop(), 
331 //               frustum->getNear(), frustum->getFar());
332         
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 )
337             break;
338         fclose(f);
339     }
340     f = fopen(filename, "wb");
341     if (!f) {
342         printf("Couldn't open image file: %s\n", filename);
343         free(buffer);
344         free(tile);
345         return;
346     }
347     fprintf(f,"P6\n");
348     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
349     fprintf(f,"%i %i\n", imageWidth, imageHeight);
350     fprintf(f,"255\n");
351
352     /* just to be safe... */
353     glPixelStorei(GL_PACK_ALIGNMENT, 1);
354
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);
359
360     bool do_hud = fgGetBool("/sim/hud/visibility");
361     GLfloat hud_col_step = 640.0 / ncols;
362     GLfloat hud_row_step = 480.0 / nrows;
363         
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;
367
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);
373         
374     /* Draw tiles */
375     int more = 1;
376     while (more) {
377         trBeginTile(tr);
378         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
379         int curRow =  trGet(tr, TR_CURRENT_ROW);
380
381         renderer->update( false );
382         // OSGFIXME
383 //         if ( do_hud )
384 //             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
385 //                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
386         // OSGFIXME
387 //         if (do_panel)
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);
392
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);
399         int i;
400         for (i=0;i<height;i++) {
401             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
402                    tile + i*bytesPerTileRow,              /* Src */
403                    bytesPerCurrentTileRow);               /* Byte count*/
404         }
405
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);
410             int i;
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);
415             }
416         }
417
418     }
419
420     renderer->resize( width, height );
421
422     trDelete(tr);
423
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 );
434     }
435
436     fclose(f);
437
438     message = "Snapshot saved to \"";
439     message += filename;
440     message += "\".";
441     mkDialog (message.c_str());
442
443     free(tile);
444     free(buffer);
445
446     delete [] filename;
447
448     if ( show_pu_cursor ) {
449         puShowCursor();
450     }
451
452     TurnCursorOn();
453     fgSetBool("/sim/menubar/visibility", menu_status);
454
455     if ( !freeze ) {
456         fgSetBool("/sim/freeze/master", false);
457     }
458 }
459 #endif // #if defined( TR_HIRES_SNAP)
460
461
462 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
463
464 void rotateView( double roll, double pitch, double yaw )
465 {
466         // rotate view
467 }
468
469 GlBitmap *b1 = NULL;
470 GLubyte *hiResScreenCapture( int multiplier )
471 {
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");
479     delete( b1 );
480     // New empty (mostly) bitmap
481     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
482     int x,y;
483     for ( y = 0; y < multiplier; y++ ) {
484         for ( x = 0; x < multiplier; x++ ) {
485             globals->get_renderer()->resize( cur_width, cur_height );
486             // pan to tile
487             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
488             globals->get_renderer()->update( false );
489             // restore view
490             GlBitmap b2;
491             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
492         }
493     }
494     fgSetDouble("/sim/current-view/field-of-view", oldfov);
495     return b1->getBitmap();
496 }
497 #endif
498
499 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
500 // win32 print screen function
501 void printScreen ( puObject *obj ) {
502     bool show_pu_cursor = false;
503     TurnCursorOff();
504     if ( !puCursorIsHidden() ) {
505         show_pu_cursor = true;
506         puHideCursor();
507     }
508
509     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
510     int cur_width = fgGetInt("/sim/startup/xsize");
511     int cur_height = fgGetInt("/sim/startup/ysize");
512     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
513     p.End( hiResScreenCapture(3) );
514
515     if ( show_pu_cursor ) {
516         puShowCursor();
517     }
518     TurnCursorOn();
519 }
520 #endif // #ifdef WIN32
521
522
523 void fgDumpSnapShotWrapper ( puObject *obj ) {
524     fgDumpSnapShot();
525 }
526
527
528 void fgHiResDumpWrapper ( puObject *obj ) {
529     fgHiResDump();
530 }
531
532
533 // do a screen snap shot
534 void fgDumpSnapShot () {
535     bool show_pu_cursor = false;
536     char *filename = new char [24];
537     static int count = 1;
538
539     static const SGPropertyNode *master_freeze
540         = fgGetNode("/sim/freeze/master");
541
542     bool freeze = master_freeze->getBoolValue();
543     if ( !freeze ) {
544         fgSetBool("/sim/freeze/master", true);
545     }
546
547     TurnCursorOff();
548     if ( !puCursorIsHidden() ) {
549         show_pu_cursor = true;
550         puHideCursor();
551     }
552     fgSetBool("/sim/signals/screenshot", true);
553
554     FGRenderer *renderer = globals->get_renderer();
555 //     renderer->init();
556     renderer->resize( fgGetInt("/sim/startup/xsize"),
557                         fgGetInt("/sim/startup/ysize") );
558
559     // we need two render frames here to clear the menu and cursor
560     // ... not sure why but doing an extra fgRenderFrame() shouldn't
561     // hurt anything
562     renderer->update( true );
563     renderer->update( true );
564
565     while (count < 1000) {
566         FILE *fp;
567         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
568         if ( (fp = fopen(filename, "r")) == NULL )
569             break;
570         fclose(fp);
571     }
572
573     int result = sg_glDumpWindow(filename,
574                                  fgGetInt("/sim/startup/xsize"),
575                                  fgGetInt("/sim/startup/ysize"));
576     fgSetString("/sim/last-screenshot", result ? filename : "");
577
578     fgSetBool("/sim/signals/screenshot", false);
579     //mkDialog (message.c_str());
580
581     delete [] filename;
582
583     if ( show_pu_cursor ) {
584         puShowCursor();
585     }
586
587     TurnCursorOn();
588
589     if ( !freeze ) {
590         fgSetBool("/sim/freeze/master", false);
591     }
592 }
593
594 // do a screen snap shot
595 void fgDumpSceneGraph()
596 {
597     char *filename = new char [24];
598     string message;
599     static int count = 1;
600
601     FGRenderer *renderer = globals->get_renderer();
602
603     static const SGPropertyNode *master_freeze
604         = fgGetNode("/sim/freeze/master");
605
606     bool freeze = master_freeze->getBoolValue();
607     if ( !freeze ) {
608         fgSetBool("/sim/freeze/master", true);
609     }
610
611     while (count < 1000) {
612         FILE *fp;
613         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
614         if ( (fp = fopen(filename, "r")) == NULL )
615             break;
616         fclose(fp);
617     }
618
619     if ( fgDumpSceneGraphToFile(filename)) {
620         message = "Scene graphe saved to \"";
621         message += filename;
622         message += "\".";
623     } else {
624         message = "Failed to save to \"";
625         message += filename;
626         message += "\".";
627     }
628
629     mkDialog (message.c_str());
630
631     delete [] filename;
632
633     if ( !freeze ) {
634         fgSetBool("/sim/freeze/master", false);
635     }
636 }
637
638     
639