]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
a3fc3a043d1e6ff7e3bbb6148393655db4273267
[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 <simgear/constants.h>
64 #include <simgear/debug/logstream.hxx>
65 #include <simgear/misc/sg_path.hxx>
66 #include <simgear/screen/screen-dump.hxx>
67
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>
82
83 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
84 #  include <simgear/screen/win32-printer.h>
85 #  include <simgear/screen/GlBitmaps.h>
86 #endif
87
88 #include "gui.h"
89 #include "gui_local.hxx"
90 #include "sgVec3Slider.hxx"
91
92 SG_USING_STD(string);
93 SG_USING_STD(cout);
94
95
96 #if defined( TR_HIRES_SNAP)
97 #include <simgear/screen/tr.h>
98 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
99                          GLfloat x_end, GLfloat y_end );
100 #endif
101
102
103 // TODO: remove after the last hardcoded dialog has died
104 char *gui_msg_OK     = "OK";
105 char *gui_msg_NO     = "NO";
106 char *gui_msg_YES    = "YES";
107 char *gui_msg_CANCEL = "CANCEL";
108 char *gui_msg_RESET  = "RESET";
109
110
111 const __fg_gui_fn_t __fg_gui_fn[] = {
112
113         // File
114         {"reInit", reInit},
115 #ifdef TR_HIRES_SNAP
116         {"dumpHiResSnapShot", fgHiResDumpWrapper},
117 #endif
118         {"dumpSnapShot", fgDumpSnapShotWrapper},
119 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
120         {"printScreen", printScreen},
121 #endif
122
123         //View
124         {"guiTogglePanel", guiTogglePanel},
125
126         // Help
127         {"helpCb", helpCb},
128
129         // Structure termination
130         {"", NULL}
131 };
132
133
134 /* ================ General Purpose Functions ================ */
135
136 // General Purpose Message Box
137 void mkDialog (const char *txt)
138 {
139     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
140     if (!gui)
141         return;
142     SGPropertyNode_ptr dlg = gui->getDialogProperties("message");
143     if (!dlg)
144         return;
145
146     dlg->setStringValue("text/label", txt);
147     dlg->setStringValue("button/legend", "OK");
148     gui->showDialog("message");
149 }
150
151 // Message Box to report an error.
152 void guiErrorMessage (const char *txt)
153 {
154     SG_LOG(SG_GENERAL, SG_ALERT, txt);
155     mkDialog(txt);
156 }
157
158 // Message Box to report a throwable (usually an exception).
159 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
160 {
161     string msg = txt;
162     msg += '\n';
163     msg += throwable.getFormattedMessage();
164     if (!throwable.getOrigin().empty()) {
165       msg += "\n (reported by ";
166       msg += throwable.getOrigin();
167       msg += ')';
168     }
169     SG_LOG(SG_GENERAL, SG_ALERT, msg);
170     mkDialog(msg.c_str());
171 }
172
173
174
175 /* -----------------------------------------------------------------------
176 the Gui callback functions 
177 ____________________________________________________________________*/
178
179
180 // Hier Neu :-) This is my newly added code
181 // Added by David Findlay <nedz@bigpond.com>
182 // on Sunday 3rd of December
183
184
185 // This is the accessor function
186 void guiTogglePanel(puObject *cb)
187 {
188   if (fgGetBool("/sim/panel/visibility"))
189     fgSetBool("/sim/panel/visibility", false);
190   else
191     fgSetBool("/sim/panel/visibility", true);
192
193   globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
194                                   fgGetInt("/sim/startup/ysize"));
195 }
196
197 void helpCb (puObject *)
198 {
199     string command;
200         
201 #if defined(FX) && !defined(WIN32)
202 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
203     if ( globals->get_fullscreen() ) {
204         globals->set_fullscreen(false);
205         XMesaSetFXmode( XMESA_FX_WINDOW );
206     }
207 #  endif
208 #endif
209         
210     SGPath path( globals->get_fg_root() );
211     path.append( "Docs/index.html" );
212         
213 #if !defined(WIN32)
214
215     string help_app = fgGetString("/sim/startup/browser-app");
216
217     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
218         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
219     } else {
220         command = help_app + " " + path.str();
221     }
222     command += " &";
223     system( command.c_str() );
224
225 #else // WIN32
226
227     // Look for favorite browser
228     char Dummy[1024], ExecName[1024], browserParameter[1024];
229     char win32_name[1024];
230 # ifdef __CYGWIN__
231     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
232 # else
233     strncpy(win32_name,path.c_str(), 1024);
234 # endif
235     Dummy[0] = 0;
236     FindExecutable(win32_name, Dummy, ExecName);
237     snprintf(browserParameter, 1024, "file:///%s", win32_name);
238     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
239                    SW_SHOWNORMAL ) ;
240
241 #endif
242         
243     mkDialog ("Help started in your web browser window.");
244 }
245
246 #if defined( TR_HIRES_SNAP)
247 void fgHiResDump()
248 {
249     FILE *f;
250     string message;
251     bool show_pu_cursor = false;
252     bool menu_status = fgGetBool("/sim/menubar/visibility");
253     char *filename = new char [24];
254     static int count = 1;
255
256     static const SGPropertyNode *master_freeze
257         = fgGetNode("/sim/freeze/master");
258
259     bool freeze = master_freeze->getBoolValue();
260     if ( !freeze ) {
261         fgSetBool("/sim/freeze/master", true);
262     }
263
264     fgSetBool("/sim/menubar/visibility", false);
265     TurnCursorOff();
266     if ( !puCursorIsHidden() ) {
267         show_pu_cursor = true;
268         puHideCursor();
269     }
270
271     FGRenderer *renderer = globals->get_renderer();
272 //     renderer->init();
273     renderer->resize( fgGetInt("/sim/startup/xsize"),
274                       fgGetInt("/sim/startup/ysize") );
275
276     // we need two render frames here to clear the menu and cursor
277     // ... not sure why but doing an extra fgRenderFrame() shouldn't
278     // hurt anything
279     //renderer->update( true );
280     //renderer->update( true );
281
282     // This ImageSize stuff is a temporary hack
283     // should probably use 128x128 tile size and
284     // support any image size
285
286     // This should be a requester to get multiplier from user
287     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
288     int width = fgGetInt("/sim/startup/xsize");
289     int height = fgGetInt("/sim/startup/ysize");
290         
291     /* allocate buffer large enough to store one tile */
292     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
293     if (!tile) {
294         printf("Malloc of tile buffer failed!\n");
295         return;
296     }
297
298     int imageWidth  = multiplier*width;
299     int imageHeight = multiplier*height;
300
301     /* allocate buffer to hold a row of tiles */
302     GLubyte *buffer
303         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
304     if (!buffer) {
305         free(tile);
306         printf("Malloc of tile row buffer failed!\n");
307         return;
308     }
309     TRcontext *tr = trNew();
310     trTileSize(tr, width, height, 0);
311     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
312     trImageSize(tr, imageWidth, imageHeight);
313     trRowOrder(tr, TR_TOP_TO_BOTTOM);
314     // OSGFIXME
315 //     sgFrustum *frustum = ssgGetFrustum();
316 //     trFrustum(tr,
317 //               frustum->getLeft(), frustum->getRight(),
318 //               frustum->getBot(),  frustum->getTop(), 
319 //               frustum->getNear(), frustum->getFar());
320         
321     /* Prepare ppm output file */
322     while (count < 1000) {
323         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
324         if ( (f = fopen(filename, "r")) == NULL )
325             break;
326         fclose(f);
327     }
328     f = fopen(filename, "wb");
329     if (!f) {
330         printf("Couldn't open image file: %s\n", filename);
331         free(buffer);
332         free(tile);
333         return;
334     }
335     fprintf(f,"P6\n");
336     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
337     fprintf(f,"%i %i\n", imageWidth, imageHeight);
338     fprintf(f,"255\n");
339
340     /* just to be safe... */
341     glPixelStorei(GL_PACK_ALIGNMENT, 1);
342
343     /* Because the HUD and Panel change the ViewPort we will
344      * need to handle some lowlevel stuff ourselves */
345     int ncols = trGet(tr, TR_COLUMNS);
346     int nrows = trGet(tr, TR_ROWS);
347
348     bool do_hud = fgGetBool("/sim/hud/visibility");
349     GLfloat hud_col_step = 640.0 / ncols;
350     GLfloat hud_row_step = 480.0 / nrows;
351         
352     bool do_panel = fgPanelVisible();
353     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
354     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
355
356     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
357     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
358     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
359     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
360     glHint(GL_FOG_HINT, GL_NICEST);
361         
362     /* Draw tiles */
363     int more = 1;
364     while (more) {
365         trBeginTile(tr);
366         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
367         int curRow =  trGet(tr, TR_CURRENT_ROW);
368
369         renderer->update( false );
370         // OSGFIXME
371 //         if ( do_hud )
372 //             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
373 //                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
374         // OSGFIXME
375 //         if (do_panel)
376 //             globals->get_current_panel()->update(
377 //                                    curColumn*panel_col_step, panel_col_step,
378 //                                    curRow*panel_row_step,    panel_row_step );
379         more = trEndTile(tr);
380
381         /* save tile into tile row buffer*/
382         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
383         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
384         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
385         int xOffset = curColumn * bytesPerTileRow;
386         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
387         int i;
388         for (i=0;i<height;i++) {
389             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
390                    tile + i*bytesPerTileRow,              /* Src */
391                    bytesPerCurrentTileRow);               /* Byte count*/
392         }
393
394         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
395             /* write this buffered row of tiles to the file */
396             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
397             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
398             int i;
399             for (i=0;i<curTileHeight;i++) {
400                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
401                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
402                 fwrite(rowPtr, 1, imageWidth*3, f);
403             }
404         }
405
406     }
407
408     renderer->resize( width, height );
409
410     trDelete(tr);
411
412     glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
413     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
414     glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
415     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
416     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
417          (!fgGetBool("/sim/rendering/shading"))) {
418         // if fastest fog requested, or if flat shading force fastest
419         glHint ( GL_FOG_HINT, GL_FASTEST );
420     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
421         glHint ( GL_FOG_HINT, GL_DONT_CARE );
422     }
423
424     fclose(f);
425
426     message = "Snapshot saved to \"";
427     message += filename;
428     message += "\".";
429     mkDialog (message.c_str());
430
431     free(tile);
432     free(buffer);
433
434     delete [] filename;
435
436     if ( show_pu_cursor ) {
437         puShowCursor();
438     }
439
440     TurnCursorOn();
441     fgSetBool("/sim/menubar/visibility", menu_status);
442
443     if ( !freeze ) {
444         fgSetBool("/sim/freeze/master", false);
445     }
446 }
447 #endif // #if defined( TR_HIRES_SNAP)
448
449
450 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
451
452 void rotateView( double roll, double pitch, double yaw )
453 {
454         // rotate view
455 }
456
457 GlBitmap *b1 = NULL;
458 GLubyte *hiResScreenCapture( int multiplier )
459 {
460     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
461     float fov = oldfov / multiplier;
462     FGViewer *v = globals->get_current_view();
463     fgSetDouble("/sim/current-view/field-of-view", fov);
464 //     globals->get_renderer()->init();
465     int cur_width = fgGetInt("/sim/startup/xsize");
466     int cur_height = fgGetInt("/sim/startup/ysize");
467     delete( b1 );
468     // New empty (mostly) bitmap
469     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
470     int x,y;
471     for ( y = 0; y < multiplier; y++ ) {
472         for ( x = 0; x < multiplier; x++ ) {
473             globals->get_renderer()->resize( cur_width, cur_height );
474             // pan to tile
475             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
476             globals->get_renderer()->update( false );
477             // restore view
478             GlBitmap b2;
479             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
480         }
481     }
482     fgSetDouble("/sim/current-view/field-of-view", oldfov);
483     return b1->getBitmap();
484 }
485 #endif
486
487 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
488 // win32 print screen function
489 void printScreen ( puObject *obj ) {
490     bool show_pu_cursor = false;
491     TurnCursorOff();
492     if ( !puCursorIsHidden() ) {
493         show_pu_cursor = true;
494         puHideCursor();
495     }
496     // BusyCursor( 0 );
497
498     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
499     int cur_width = fgGetInt("/sim/startup/xsize");
500     int cur_height = fgGetInt("/sim/startup/ysize");
501     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
502     p.End( hiResScreenCapture(3) );
503
504     // BusyCursor(1);
505     if ( show_pu_cursor ) {
506         puShowCursor();
507     }
508     TurnCursorOn();
509 }
510 #endif // #ifdef WIN32
511
512
513 void fgDumpSnapShotWrapper ( puObject *obj ) {
514     fgDumpSnapShot();
515 }
516
517
518 void fgHiResDumpWrapper ( puObject *obj ) {
519     fgHiResDump();
520 }
521
522
523 // do a screen snap shot
524 void fgDumpSnapShot () {
525     bool show_pu_cursor = false;
526     char *filename = new char [24];
527     string message;
528     static int count = 1;
529
530     static const SGPropertyNode *master_freeze
531         = fgGetNode("/sim/freeze/master");
532
533     bool freeze = master_freeze->getBoolValue();
534     if ( !freeze ) {
535         fgSetBool("/sim/freeze/master", true);
536     }
537
538     TurnCursorOff();
539     if ( !puCursorIsHidden() ) {
540         show_pu_cursor = true;
541         puHideCursor();
542     }
543     fgSetBool("/sim/signals/screenshot", true);
544
545     FGRenderer *renderer = globals->get_renderer();
546 //     renderer->init();
547     renderer->resize( fgGetInt("/sim/startup/xsize"),
548                         fgGetInt("/sim/startup/ysize") );
549
550     // we need two render frames here to clear the menu and cursor
551     // ... not sure why but doing an extra fgRenderFrame() shouldn't
552     // hurt anything
553     renderer->update( true );
554     renderer->update( true );
555
556     while (count < 1000) {
557         FILE *fp;
558         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
559         if ( (fp = fopen(filename, "r")) == NULL )
560             break;
561         fclose(fp);
562     }
563
564     if ( sg_glDumpWindow( filename,
565                           fgGetInt("/sim/startup/xsize"), 
566                           fgGetInt("/sim/startup/ysize")) ) {
567         message = "Snapshot saved to \"";
568         message += filename;
569         message += "\".";
570     } else {
571         message = "Failed to save to \"";
572         message += filename;
573         message += "\".";
574     }
575
576     fgSetBool("/sim/signals/screenshot", false);
577     mkDialog (message.c_str());
578
579     delete [] filename;
580
581     if ( show_pu_cursor ) {
582         puShowCursor();
583     }
584
585     TurnCursorOn();
586
587     if ( !freeze ) {
588         fgSetBool("/sim/freeze/master", false);
589     }
590 }
591