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