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