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