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