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