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