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