]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
Viewer update from Jim Wilson:
[flightgear.git] / src / GUI / gui.cxx
1 /**************************************************************************
2  * gui.cxx
3  *
4  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
5  * project.
6  *
7  * Additional mouse supported added by David Megginson, 1999.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #ifdef SG_MATH_EXCEPTION_CLASH
34 #  include <math.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include <GL/glut.h>
42 #include <GL/gl.h>
43
44 #if defined(FX) && defined(XMESA)
45 #  include <GL/xmesa.h>
46 #endif
47
48 #include STL_FSTREAM
49 #include STL_STRING
50
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include <simgear/constants.h>
55 #include <simgear/debug/logstream.hxx>
56 #include <simgear/misc/sg_path.hxx>
57 #include <simgear/screen/screen-dump.hxx>
58
59 #include <Include/general.hxx>
60 #include <Aircraft/aircraft.hxx>
61 #include <Airports/simple.hxx>
62 #include <Autopilot/auto_gui.hxx>
63 #include <Autopilot/newauto.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Controls/controls.hxx>
66 #include <FDM/flight.hxx>
67 #include <Main/fg_init.hxx>
68 #include <Main/fg_io.hxx>
69 #include <Main/globals.hxx>
70 #include <Main/fg_props.hxx>
71 #include <Main/viewmgr.hxx>
72
73 #ifdef FG_NETWORK_OLK
74 #include <NetworkOLK/network.h>
75 #endif
76    
77 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
78 #  include <simgear/screen/win32-printer.h>
79 #  include <simgear/screen/GlBitmaps.h>
80 #endif
81
82 #include "gui.h"
83 #include "gui_local.hxx"
84 #include "apt_dlg.hxx"
85 #include "net_dlg.hxx"
86 #include "sgVec3Slider.hxx"
87 #include "prop_picker.hxx"
88
89 SG_USING_STD(string);
90
91 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
92 SG_USING_STD(cout);
93 #endif
94
95 #if defined(_MSC_VER) || defined(__MINGW32__)
96 #define  snprintf    _snprintf
97 #endif   /* _MSC_VER */
98
99 // main.cxx hack, should come from an include someplace
100 extern void fgInitVisuals( void );
101 extern void fgReshape( int width, int height );
102 extern void fgRenderFrame( void );
103
104 puFont guiFnt = 0;
105 fntTexFont *guiFntHandle = 0;
106 int gui_menu_on = 0;
107 static puMenuBar    *mainMenuBar = 0;
108 //static puButton     *hideMenuButton = 0;
109
110 static puDialogBox  *dialogBox = 0;
111 static puFrame      *dialogFrame = 0;
112 static puText       *dialogBoxMessage = 0;
113 static puOneShot    *dialogBoxOkButton = 0;
114
115
116 static puDialogBox  *YNdialogBox = 0;
117 static puFrame      *YNdialogFrame = 0;
118 static puText       *YNdialogBoxMessage = 0;
119 static puOneShot    *YNdialogBoxOkButton = 0;
120 static puOneShot    *YNdialogBoxNoButton = 0;
121
122 static char msg_OK[]     = "OK";
123 static char msg_NO[]     = "NO";
124 static char msg_YES[]    = "YES";
125 static char msg_CANCEL[] = "Cancel";
126 static char msg_RESET[]  = "Reset";
127
128 char *gui_msg_OK;     // "OK"
129 char *gui_msg_NO;     // "NO"
130 char *gui_msg_YES;    // "YES"
131 char *gui_msg_CANCEL; // "CANCEL"
132 char *gui_msg_RESET;  // "RESET"
133
134 static char global_dialog_string[256];
135
136 // from cockpit.cxx
137 extern void fgLatLonFormatToggle( puObject *);
138
139
140 /* ================ General Purpose Functions ================ */
141
142 // Intercept the Escape Key
143 void ConfirmExitDialog(void)
144 {
145     FG_PUSH_PUI_DIALOG( YNdialogBox );
146 }
147
148 // General Purpose Message Box
149 void mkDialog (const char *txt)
150 {
151     strncpy(global_dialog_string, txt, 256);
152     dialogBoxMessage->setLabel(global_dialog_string);
153     FG_PUSH_PUI_DIALOG( dialogBox );
154 }
155
156 // Message Box to report an error.
157 void guiErrorMessage (const char *txt)
158 {
159     SG_LOG(SG_GENERAL, SG_ALERT, txt);
160     if (dialogBox != 0)
161       mkDialog(txt);
162 }
163
164 // Message Box to report a throwable (usually an exception).
165 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
166 {
167     string msg = txt;
168     msg += '\n';
169     msg += throwable.getFormattedMessage();
170     if (!throwable.getOrigin().empty()) {
171       msg += "\n (reported by ";
172       msg += throwable.getOrigin();
173       msg += ')';
174     }
175     SG_LOG(SG_GENERAL, SG_ALERT, msg);
176     if (dialogBox != 0)
177       mkDialog(msg.c_str());
178 }
179
180 // Toggle the Menu and Mouse display state
181 void guiToggleMenu(void)
182 {
183     if( gui_menu_on ) {
184         // printf("Hiding Menu\n");
185         mainMenuBar->hide  ();
186 #if defined(WIN32_CURSOR_TWEAKS_OFF)
187         if( mouse_mode == MOUSE_POINTER )
188             TurnCursorOff();
189 #endif // WIN32_CURSOR_TWEAKS_OFF
190     } else {
191         // printf("Showing Menu\n");
192         mainMenuBar->reveal();
193 #ifdef WIN32
194         TurnCursorOn();
195 #endif // WIN32
196     }
197     gui_menu_on = ~gui_menu_on;
198 }
199     
200 /* -----------------------------------------------------------------------
201 the Gui callback functions 
202 ____________________________________________________________________*/
203
204
205 // Hier Neu :-) This is my newly added code
206 // Added by David Findlay <nedz@bigpond.com>
207 // on Sunday 3rd of December
208
209 // Start new Save Dialog Box
210 static puDialogBox     *SaveDialog = 0;
211 static puFrame         *SaveDialogFrame = 0;
212 static puText          *SaveDialogMessage = 0;
213 static puInput         *SaveDialogInput = 0;
214
215 static puOneShot       *SaveDialogOkButton = 0;
216 static puOneShot       *SaveDialogCancelButton = 0;
217 // static puOneShot       *SaveDialogResetButton = 0;
218
219 // Default save filename
220 static char saveFile[256] = "fgfs.sav";
221
222 // Cancel Button
223 void SaveDialogCancel(puObject *) {
224     FG_POP_PUI_DIALOG( SaveDialog );
225 }
226
227 // If press OK do this
228 void SaveDialogOk(puObject*) {
229
230     FG_POP_PUI_DIALOG( SaveDialog );
231
232     char *s;
233     SaveDialogInput->getValue(&s);
234
235     ofstream output(s);
236     cout << saveFile << endl;
237     if (output.good() && fgSaveFlight(output)) {
238         output.close();
239         mkDialog("Saved flight");
240         SG_LOG(SG_INPUT, SG_INFO, "Saved flight");
241     } else {
242         mkDialog("Cannot save flight");
243         SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight");
244     }
245 }
246
247 // Create Dialog
248 static void saveFlight(puObject *cv) {
249     SaveDialog = new puDialogBox (150, 50);
250     {
251         SaveDialogFrame   = new puFrame           (0,0,350, 150);
252         SaveDialogMessage
253             = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
254         SaveDialogMessage ->    setLabel          ("File Name:");
255
256         SaveDialogInput   = new puInput           (50, 70, 300, 100);
257         SaveDialogInput   ->    setValue          (saveFile);
258         SaveDialogInput   ->    acceptInput();
259
260         SaveDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
261         SaveDialogOkButton     ->     setLegend   (gui_msg_OK);
262         SaveDialogOkButton     ->     setCallback ( SaveDialogOk );
263         SaveDialogOkButton     ->     makeReturnDefault(TRUE);
264
265         SaveDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
266         SaveDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
267         SaveDialogCancelButton ->     setCallback ( SaveDialogCancel );
268     }
269     FG_FINALIZE_PUI_DIALOG( SaveDialog );
270    
271     SaveDialog -> reveal();
272 }
273
274 // Load Dialog Start
275 static puDialogBox     *LoadDialog = 0;
276 static puFrame         *LoadDialogFrame = 0;
277 static puText          *LoadDialogMessage = 0;
278 static puInput         *LoadDialogInput = 0;
279
280 static puOneShot       *LoadDialogOkButton = 0;
281 static puOneShot       *LoadDialogCancelButton = 0;
282 // static puOneShot       *LoadDialogResetButton = 0;
283
284 // Default load filename
285 static char loadFile[256] = "fgfs.sav";
286
287 // Do this if the person click okay
288 void LoadDialogOk(puObject *) {
289
290     FG_POP_PUI_DIALOG( LoadDialog );
291
292     char *l;
293     LoadDialogInput->getValue(&l);
294
295     ifstream input(l);
296     if (input.good() && fgLoadFlight(input)) {
297         input.close();
298         mkDialog("Loaded flight");
299         SG_LOG(SG_INPUT, SG_INFO, "Restored flight");
300     } else {
301         mkDialog("Failed to load flight");
302         SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight");
303     }
304 }
305
306 // Do this if the person presses cancel
307 void LoadDialogCancel(puObject *) {
308     FG_POP_PUI_DIALOG( LoadDialog );
309 }
310
311 // Create Load Dialog
312 static void loadFlight(puObject *cb)
313 {
314     LoadDialog = new puDialogBox (150, 50);
315     {
316         LoadDialogFrame   = new puFrame           (0,0,350, 150);
317         LoadDialogMessage
318             = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
319         LoadDialogMessage ->    setLabel          ("File Name:");
320
321         LoadDialogInput   = new puInput           (50, 70, 300, 100);
322         LoadDialogInput   ->    setValue          (loadFile);
323         LoadDialogInput   ->    acceptInput();
324
325         LoadDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
326         LoadDialogOkButton     ->     setLegend   (gui_msg_OK);
327         LoadDialogOkButton     ->     setCallback ( LoadDialogOk );
328         LoadDialogOkButton     ->     makeReturnDefault(TRUE);
329
330         LoadDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
331         LoadDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
332         LoadDialogCancelButton ->     setCallback ( LoadDialogCancel );
333     }
334     FG_FINALIZE_PUI_DIALOG( LoadDialog );
335    
336     LoadDialog -> reveal();
337 }
338
339 // This is the accessor function
340 void guiTogglePanel(puObject *cb)
341 {
342   if (fgGetBool("/sim/panel/visibility"))
343     fgSetBool("/sim/panel/visibility", false);
344   else
345     fgSetBool("/sim/panel/visibility", true);
346
347   fgReshape(fgGetInt("/sim/startup/xsize"),
348             fgGetInt("/sim/startup/ysize"));
349 }
350     
351 //void MenuHideMenuCb(puObject *cb)
352 void hideMenuCb (puObject *cb)
353 {
354     guiToggleMenu();
355 }
356
357 void goodBye(puObject *)
358 {
359     // SG_LOG( SG_INPUT, SG_ALERT,
360     //      "Program exiting normally at user request." );
361     cout << "Program exiting normally at user request." << endl;
362
363 #ifdef FG_NETWORK_OLK    
364     if ( fgGetBool("/sim/networking/network-olk") ) {
365         if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
366     }
367 #endif
368
369     // close all external I/O connections
370     fgIOShutdownAll();
371
372     exit(0);
373 }
374
375
376 void goAwayCb (puObject *me)
377 {
378     FG_POP_PUI_DIALOG( dialogBox );
379 }
380
381 void mkDialogInit (void)
382 {
383     //  printf("mkDialogInit\n");
384     int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
385     int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
386     dialogBox = new puDialogBox (x, y); // 150, 50
387     {
388         dialogFrame = new puFrame (0,0,400,100);
389         dialogBoxMessage  =  new puText         (10, 70);
390         dialogBoxMessage  -> setLabel           ("");
391         dialogBoxOkButton =  new puOneShot      (180, 10, 240, 50);
392         dialogBoxOkButton -> setLegend          (gui_msg_OK);
393         dialogBoxOkButton -> makeReturnDefault  (TRUE );
394         dialogBoxOkButton -> setCallback        (goAwayCb);
395     }
396     FG_FINALIZE_PUI_DIALOG( dialogBox );
397 }
398
399 void MayBeGoodBye(puObject *)
400 {
401     ConfirmExitDialog(); 
402 }
403
404 void goAwayYesNoCb(puObject *me)
405 {
406     FG_POP_PUI_DIALOG( YNdialogBox);
407 }
408
409 void ConfirmExitDialogInit(void)
410 {
411     char msg[] = "Really Quit";
412     char *s;
413
414     //  printf("ConfirmExitDialogInit\n");
415     int len = 200 - puGetDefaultLabelFont().getStringWidth ( msg ) / 2;
416
417     int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
418     int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
419         
420     YNdialogBox = new puDialogBox (x, y); // 150, 50
421     {
422         YNdialogFrame = new puFrame (0,0,400, 100);
423         
424         YNdialogBoxMessage  =  new puText         (len, 70);
425         YNdialogBoxMessage  -> setDefaultValue    (msg);
426         YNdialogBoxMessage  -> getDefaultValue    (&s);
427         YNdialogBoxMessage  -> setLabel           (s);
428         
429         YNdialogBoxOkButton =  new puOneShot      (100, 10, 160, 50);
430         YNdialogBoxOkButton -> setLegend          (gui_msg_OK);
431         YNdialogBoxOkButton -> makeReturnDefault  (TRUE );
432         YNdialogBoxOkButton -> setCallback        (goodBye);
433         
434         YNdialogBoxNoButton =  new puOneShot      (240, 10, 300, 50);
435         YNdialogBoxNoButton -> setLegend          (gui_msg_NO);
436         YNdialogBoxNoButton -> setCallback        (goAwayYesNoCb);
437     }
438     FG_FINALIZE_PUI_DIALOG( YNdialogBox );
439 }
440
441 void notCb (puObject *)
442 {
443     mkDialog ("This function isn't implemented yet");
444 }
445
446 void helpCb (puObject *)
447 {
448     string command;
449         
450 #if defined(FX) && !defined(WIN32)
451 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
452     if ( globals->get_fullscreen() ) {
453         globals->set_fullscreen(false);
454         XMesaSetFXmode( XMESA_FX_WINDOW );
455     }
456 #  endif
457 #endif
458         
459     SGPath path( globals->get_fg_root() );
460     path.append( "Docs/index.html" );
461         
462 #if !defined(WIN32)
463     string help_app = fgGetString("/sim/startup/browser-app");
464
465     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
466         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
467     } else {
468         command = help_app + " " + path.str();
469     }
470     command += " &";
471 #else // WIN32
472         command = "start ";
473         command += path.str();
474 #endif
475         
476     system( command.c_str() );
477     mkDialog ("Help started in netscape window.");
478 }
479
480 #define TR_HIRES_SNAP
481 #if defined( TR_HIRES_SNAP)
482 #include <simgear/screen/tr.h>
483 extern void trRenderFrame( void );
484 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
485                          GLfloat x_end, GLfloat y_end );
486
487 void fgHiResDump()
488 {
489     FILE *f;
490     string message;
491     bool show_pu_cursor = false;
492     bool show_menu = false;
493     char *filename = new char [24];
494     static int count = 1;
495
496     static const SGPropertyNode *master_freeze
497         = fgGetNode("/sim/freeze/master");
498
499     bool freeze = master_freeze->getBoolValue();
500     if ( !freeze ) {
501         fgSetBool("/sim/freeze/master", true);
502     }
503
504     if(gui_menu_on) {
505         show_menu = true;
506         guiToggleMenu();
507     }
508         
509     if ( !puCursorIsHidden() ) {
510         show_pu_cursor = true;
511         puHideCursor();
512     }
513
514     fgInitVisuals();
515     fgReshape( fgGetInt("/sim/startup/xsize"),
516                fgGetInt("/sim/startup/ysize") );
517
518     // we need two render frames here to clear the menu and cursor
519     // ... not sure why but doing an extra fgRenderFrame() shouldn't
520     // hurt anything
521     fgRenderFrame();
522     fgRenderFrame();
523
524     // Make sure we have SSG projection primed for current view
525     glMatrixMode(GL_MODELVIEW);
526     glLoadIdentity();
527     ssgSetCamera( (sgVec4 *)globals->get_current_view()->get_VIEW() );
528     ssgSetFOV( globals->get_current_view()->get_h_fov(),
529                globals->get_current_view()->get_v_fov() );
530     // ssgSetNearFar( 10.0f, 120000.0f );
531     ssgSetNearFar( 0.5f, 1200000.0f );
532
533         
534     // This ImageSize stuff is a temporary hack
535     // should probably use 128x128 tile size and
536     // support any image size
537
538     // This should be a requester to get multiplier from user
539     int multiplier = 3;
540     int width = fgGetInt("/sim/startup/xsize");
541     int height = fgGetInt("/sim/startup/ysize");
542         
543     /* allocate buffer large enough to store one tile */
544     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
545     if (!tile) {
546         printf("Malloc of tile buffer failed!\n");
547         return;
548     }
549
550     int imageWidth  = multiplier*width;
551     int imageHeight = multiplier*height;
552
553     /* allocate buffer to hold a row of tiles */
554     GLubyte *buffer
555         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
556     if (!buffer) {
557         free(tile);
558         printf("Malloc of tile row buffer failed!\n");
559         return;
560     }
561     TRcontext *tr = trNew();
562     trTileSize(tr, width, height, 0);
563     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
564     trImageSize(tr, imageWidth, imageHeight);
565     trRowOrder(tr, TR_TOP_TO_BOTTOM);
566     sgFrustum *frustum = ssgGetFrustum();
567     trFrustum(tr,
568               frustum->getLeft(), frustum->getRight(),
569               frustum->getBot(),  frustum->getTop(), 
570               frustum->getNear(), frustum->getFar());
571         
572     /* Prepare ppm output file */
573     while (count < 1000) {
574         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
575         if ( (f = fopen(filename, "r")) == NULL )
576             break;
577         fclose(f);
578     }
579     f = fopen(filename, "wb");
580     if (!f) {
581         printf("Couldn't open image file: %s\n", filename);
582         free(buffer);
583         free(tile);
584         return;
585     }
586     fprintf(f,"P6\n");
587     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
588     fprintf(f,"%i %i\n", imageWidth, imageHeight);
589     fprintf(f,"255\n");
590
591     /* just to be safe... */
592     glPixelStorei(GL_PACK_ALIGNMENT, 1);
593
594     /* Because the HUD and Panel change the ViewPort we will
595      * need to handle some lowlevel stuff ourselves */
596     int ncols = trGet(tr, TR_COLUMNS);
597     int nrows = trGet(tr, TR_ROWS);
598
599     bool do_hud = fgGetBool("/sim/hud/visibility");
600     GLfloat hud_col_step = 640.0 / ncols;
601     GLfloat hud_row_step = 480.0 / nrows;
602         
603     bool do_panel = fgPanelVisible();
604     GLfloat panel_col_step = current_panel->getWidth() / ncols;
605     GLfloat panel_row_step = current_panel->getHeight() / nrows;
606         
607     /* Draw tiles */
608     int more = 1;
609     while (more) {
610         trBeginTile(tr);
611         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
612         int curRow =  trGet(tr, TR_CURRENT_ROW);
613         trRenderFrame();
614         if ( do_hud )
615             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
616                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
617         if (do_panel)
618             current_panel->update( curColumn*panel_col_step, panel_col_step,
619                                    curRow*panel_row_step,    panel_row_step );
620         more = trEndTile(tr);
621
622         /* save tile into tile row buffer*/
623         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
624         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
625         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
626         int xOffset = curColumn * bytesPerTileRow;
627         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
628         int i;
629         for (i=0;i<height;i++) {
630             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
631                    tile + i*bytesPerTileRow,              /* Src */
632                    bytesPerCurrentTileRow);               /* Byte count*/
633         }
634
635         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
636             /* write this buffered row of tiles to the file */
637             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
638             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
639             int i;
640             for (i=0;i<curTileHeight;i++) {
641                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
642                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
643                 fwrite(rowPtr, 1, imageWidth*3, f);
644             }
645         }
646
647     }
648
649     fgReshape( width, height );
650
651     trDelete(tr);
652
653     fclose(f);
654
655     message = "Snap shot saved to ";
656     message += filename;
657     mkDialog (message.c_str());
658
659     free(tile);
660     free(buffer);
661
662     //  message = "Snap shot saved to ";
663     //  message += filename;
664     //  mkDialog (message.c_str());
665
666     delete [] filename;
667
668     if( show_menu )
669         guiToggleMenu();
670
671     if ( show_pu_cursor ) {
672         puShowCursor();
673     }
674
675     if ( !freeze ) {
676         fgSetBool("/sim/freeze/master", false);
677     }
678 }
679 #endif // #if defined( TR_HIRES_SNAP)
680
681
682 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
683
684 static void rotateView( double roll, double pitch, double yaw )
685 {
686         // rotate view
687 }
688
689 static GlBitmap *b1 = NULL;
690 extern FGInterface cur_view_fdm;
691 GLubyte *hiResScreenCapture( int multiplier )
692 {
693     float oldfov = fgGetDouble("/sim/field-of-view");
694     float fov = oldfov / multiplier;
695     FGViewer *v = globals->get_current_view();
696     fgSetDouble("/sim/field-of-view", fov);
697     fgInitVisuals();
698     int cur_width = fgGetInt("/sim/startup/xsize");
699     int cur_height = fgGetInt("/sim/startup/ysize");
700     if (b1) delete( b1 );
701     // New empty (mostly) bitmap
702     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
703     int x,y;
704     for ( y = 0; y < multiplier; y++ ) {
705         for ( x = 0; x < multiplier; x++ ) {
706             fgReshape( cur_width, cur_height );
707             // pan to tile
708             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
709             fgRenderFrame();
710             // restore view
711             GlBitmap b2;
712             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
713         }
714     }
715     fgSetDouble("/sim/field-of-view", oldfov);
716     return b1->getBitmap();
717 }
718 #endif
719
720
721 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
722 // win32 print screen function
723 void printScreen ( puObject *obj ) {
724     bool show_pu_cursor = false;
725     TurnCursorOff();
726     if ( !puCursorIsHidden() ) {
727         show_pu_cursor = true;
728         puHideCursor();
729     }
730     // BusyCursor( 0 );
731     mainMenuBar->hide();
732
733     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
734     int cur_width = fgGetInt("/sim/startup/xsize");
735     int cur_height = fgGetInt("/sim/startup/ysize");
736     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
737         p.End( hiResScreenCapture(3) );
738
739     if( gui_menu_on ) {
740         mainMenuBar->reveal();
741     }
742     // BusyCursor(1);
743     if ( show_pu_cursor ) {
744         puShowCursor();
745     }
746     TurnCursorOn();
747 }
748 #endif // #ifdef WIN32
749
750
751 void dumpSnapShot ( puObject *obj ) {
752     fgDumpSnapShot();
753 }
754
755
756 void dumpHiResSnapShot ( puObject *obj ) {
757     fgHiResDump();
758 }
759
760
761 // do a screen snap shot
762 void fgDumpSnapShot () {
763     bool show_pu_cursor = false;
764     char *filename = new char [24];
765     string message;
766     static int count = 1;
767
768     static const SGPropertyNode *master_freeze
769         = fgGetNode("/sim/freeze/master");
770
771     bool freeze = master_freeze->getBoolValue();
772     if ( !freeze ) {
773         fgSetBool("/sim/freeze/master", true);
774     }
775
776     mainMenuBar->hide();
777     TurnCursorOff();
778     if ( !puCursorIsHidden() ) {
779         show_pu_cursor = true;
780         puHideCursor();
781     }
782
783     fgInitVisuals();
784     fgReshape( fgGetInt("/sim/startup/xsize"),
785                fgGetInt("/sim/startup/ysize") );
786
787     // we need two render frames here to clear the menu and cursor
788     // ... not sure why but doing an extra fgRenderFrame() shouldn't
789     // hurt anything
790     fgRenderFrame();
791     fgRenderFrame();
792
793     while (count < 1000) {
794         FILE *fp;
795         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
796         if ( (fp = fopen(filename, "r")) == NULL )
797             break;
798         fclose(fp);
799     }
800
801     if ( sg_glDumpWindow( filename,
802                           fgGetInt("/sim/startup/xsize"), 
803                           fgGetInt("/sim/startup/ysize")) ) {
804         message = "Snap shot saved to ";
805         message += filename;
806     } else {
807         message = "Failed to save to ";
808         message += filename;
809     }
810
811     mkDialog (message.c_str());
812
813     delete [] filename;
814
815     if ( show_pu_cursor ) {
816         puShowCursor();
817     }
818
819     TurnCursorOn();
820     if( gui_menu_on ) {
821         mainMenuBar->reveal();
822     }
823
824     if ( !freeze ) {
825         fgSetBool("/sim/freeze/master", false);
826     }
827 }
828
829 #ifdef FG_NETWORK_OLK
830 static void net_display_toggle( puObject *cb)
831 {
832         net_hud_display = (net_hud_display) ? 0 : 1;
833         printf("Toggle net_hud_display : %d\n", net_hud_display);
834 }
835
836 static void net_register( puObject *cb)
837 {
838         fgd_send_com( "1", FGFS_host );
839         net_is_registered = 0;
840         printf("Registering to deamon\n");
841 }
842
843 static void net_unregister( puObject *cb)
844 {
845         fgd_send_com( "8", FGFS_host );
846         net_is_registered = -1;
847         printf("Unregistering from deamon\n");
848 }
849
850 extern void net_fgd_scan(puObject *cb);
851 #endif // #ifdef FG_NETWORK_OLK
852
853 /* -----------------------------------------------------------------------
854 The menu stuff 
855 ---------------------------------------------------------------------*/
856 char *fileSubmenu               [] = {
857     "Exit", /* "Close", "---------", */
858 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
859     "Print",
860 #endif
861     "Snap Shot",
862     "Hires Snap Shot",
863     "---------", 
864     "Reset", 
865     "Load flight",
866     "Save flight",
867     NULL
868 };
869 puCallback fileSubmenuCb        [] = {
870     MayBeGoodBye, /* hideMenuCb, NULL, */
871 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
872     printScreen, 
873 #endif
874     /* NULL, notCb, */
875     dumpSnapShot,
876     dumpHiResSnapShot,
877     NULL,
878     reInit, 
879     loadFlight,
880     saveFlight,
881     NULL
882 };
883
884 /*
885 char *editSubmenu               [] = {
886     "Edit text", NULL
887 };
888 puCallback editSubmenuCb        [] = {
889     notCb, NULL
890 };
891 */
892
893 extern void fgHUDalphaAdjust( puObject * );
894 char *viewSubmenu               [] = {
895     "Properties",
896     "HUD Alpha",
897     "Pilot Offset",
898     /* "Cockpit View > ", "View >","------------", */
899     "Toggle Panel...", NULL
900 };
901 puCallback viewSubmenuCb        [] = {
902     prop_pickerView,
903     fgHUDalphaAdjust,
904     PilotOffsetAdjust,
905     /* notCb, notCb, NULL, */
906     guiTogglePanel, NULL
907 };
908
909 //  "---------", 
910
911 char *autopilotSubmenu           [] = {
912     "Toggle HUD Format", "Adjust AP Settings",
913     "---------", 
914     "Clear Route", "Skip Current Waypoint", "Add Waypoint",
915     "---------", 
916     "Set Altitude", "Set Heading",
917     NULL
918 };
919
920 puCallback autopilotSubmenuCb    [] = {
921     fgLatLonFormatToggle, fgAPAdjust,
922     NULL,
923     ClearRoute, PopWayPoint, AddWayPoint,
924     NULL,
925     NewAltitude, NewHeading,
926     /* notCb, */ NULL
927 };
928
929 char *environmentSubmenu        [] = {
930     "Goto Airport", /* "Terrain", "Weather", */ NULL
931 };
932 puCallback environmentSubmenuCb [] = {
933     NewAirport, /* notCb, notCb, */ NULL
934 };
935
936 /*
937 char *optionsSubmenu            [] = {
938     "Preferences", "Realism & Reliablity...", NULL
939 };
940 puCallback optionsSubmenuCb     [] = {
941     notCb, notCb, NULL
942 };
943 */
944
945 #ifdef FG_NETWORK_OLK
946 char *networkSubmenu            [] = {
947     "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
948     "Register to FGD",
949     "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
950     "Toggle Display", NULL
951 };
952 puCallback networkSubmenuCb     [] = {
953     /* notCb, notCb, notCb, notCb, */ 
954     net_unregister, 
955     net_register, 
956     net_fgd_scan, NewCallSign, 
957     net_display_toggle, NULL
958 };
959 #endif
960
961 char *helpSubmenu               [] = {
962     /* "About...", */ "Help", NULL
963 };
964 puCallback helpSubmenuCb        [] = {
965     /* notCb, */ helpCb, NULL
966 };
967
968
969 /* -------------------------------------------------------------------------
970 init the gui
971 _____________________________________________________________________*/
972
973
974 void guiInit()
975 {
976     char *mesa_win_state;
977
978     // Initialize PUI
979     puInit();
980     puSetDefaultStyle         ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
981     puSetDefaultColourScheme  (0.8, 0.8, 0.8, 0.4);
982
983     // Initialize our GLOBAL GUI STRINGS
984     gui_msg_OK     = msg_OK;     // "OK"
985     gui_msg_NO     = msg_NO;     // "NO"
986     gui_msg_YES    = msg_YES;    // "YES"
987     gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
988     gui_msg_RESET  = msg_RESET;  // "RESET"
989
990     // Next check home directory
991     SGPath fntpath;
992     char* envp = ::getenv( "FG_FONTS" );
993     if ( envp != NULL ) {
994         fntpath.set( envp );
995     } else {
996         fntpath.set( globals->get_fg_root() );
997         fntpath.append( "Fonts" );
998     }
999
1000     // Install our fast fonts
1001     fntpath.append( "typewriter.txf" );
1002     guiFntHandle = new fntTexFont ;
1003     guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1004     puFont GuiFont ( guiFntHandle, 15 ) ;
1005     puSetDefaultFonts( GuiFont, GuiFont ) ;
1006     guiFnt = puGetDefaultLabelFont();
1007   
1008     if (!fgHasNode("/sim/startup/mouse-pointer")) {
1009         // no preference specified for mouse pointer, attempt to autodetect...
1010         // Determine if we need to render the cursor, or if the windowing
1011         // system will do it.  First test if we are rendering with glide.
1012         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1013             // Test for the MESA_GLX_FX env variable
1014             if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1015                 // test if we are fullscreen mesa/glide
1016                 if ( (mesa_win_state[0] == 'f') ||
1017                      (mesa_win_state[0] == 'F') ) {
1018                     puShowCursor ();
1019                 }
1020             }
1021         }
1022 //        mouse_active = ~mouse_active;
1023     } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
1024         // don't show pointer
1025     } else {
1026         // force showing pointer
1027         puShowCursor();
1028 //        mouse_active = ~mouse_active;
1029     }
1030         
1031     // MOUSE_VIEW mode stuff
1032         initMouseQuat();
1033
1034     // Set up our Dialog Boxes
1035     ConfirmExitDialogInit();
1036     NewAirportInit();
1037         
1038 #ifdef FG_NETWORK_OLK
1039     NewNetIdInit();
1040     NewNetFGDInit();
1041 #endif
1042
1043         mkDialogInit();
1044     
1045     // Make the menu bar
1046     mainMenuBar = new puMenuBar ();
1047     mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1048     // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1049     mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1050     mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1051     mainMenuBar -> add_submenu ("Autopilot", autopilotSubmenu, autopilotSubmenuCb);
1052     // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1053 #ifdef FG_NETWORK_OLK
1054     if ( fgGetBool("/sim/networking/network-olk") ) {
1055         mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1056     }
1057 #endif
1058     mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1059     mainMenuBar-> close ();
1060     // Set up menu bar toggle
1061     gui_menu_on = ~0;
1062
1063     if (!strcmp("/sim/flight-model", "ada")) {
1064         guiToggleMenu(); // Menu off by default
1065     }
1066 }
1067