]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
get rid of mouse.cxx and pu{Show|Hide}Cursor()
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 HAVE_WINDOWS_H
36 #  include <windows.h>
37 #endif
38
39 #include SG_GL_H
40
41 #include <fstream>
42 #include <string>
43 #include <cstring>
44 #include <sstream>
45
46 #include <stdlib.h>
47
48 // for help call back
49 #ifdef WIN32
50 # include <shellapi.h>
51 # ifdef __CYGWIN__
52 #  include <sys/cygwin.h>
53 # endif
54 #endif
55
56 #include <simgear/debug/logstream.hxx>
57 #include <simgear/misc/sg_path.hxx>
58 #include <simgear/screen/screen-dump.hxx>
59
60 #include <Cockpit/panel.hxx>
61 #include <Main/globals.hxx>
62 #include <Main/fg_props.hxx>
63 #include <Main/renderer.hxx>
64 #include <Main/viewmgr.hxx>
65 #include <GUI/new_gui.hxx>
66
67 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
68 #  include <simgear/screen/win32-printer.h>
69 #  include <simgear/screen/GlBitmaps.h>
70 #endif
71
72 #include "gui.h"
73
74 using std::string;
75
76
77 #if defined( TR_HIRES_SNAP)
78 #include <simgear/screen/tr.h>
79 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
80                          GLfloat x_end, GLfloat y_end );
81 #endif
82
83
84 const __fg_gui_fn_t __fg_gui_fn[] = {
85 #ifdef TR_HIRES_SNAP
86         {"dumpHiResSnapShot", fgHiResDumpWrapper},
87 #endif
88         {"dumpSnapShot", fgDumpSnapShotWrapper},
89 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
90         {"printScreen", printScreen},
91 #endif
92         // Help
93         {"helpCb", helpCb},
94
95         // Structure termination
96         {"", NULL}
97 };
98
99
100 /* ================ General Purpose Functions ================ */
101
102 // General Purpose Message Box. Makes sure no more than 5 different
103 // messages are displayed at the same time, and none of them are
104 // duplicates. (5 is a *lot*, but this will hardly ever be reached
105 // and we don't want to miss any, either.)
106 void mkDialog (const char *txt)
107 {
108     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
109     if (!gui)
110         return;
111     SGPropertyNode *master = gui->getDialogProperties("message");
112     if (!master)
113         return;
114
115     const int maxdialogs = 5;
116     string name;
117     SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
118     int i;
119     for (i = 0; i < maxdialogs; i++) {
120         std::ostringstream s;
121         s << "message-" << i;
122         name = s.str();
123
124         if (!msg->getNode(name.c_str(), false))
125             break;
126
127         if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
128             SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
129             return;
130         }
131     }
132     if (i == maxdialogs)
133         return;
134     msg = msg->getNode(name.c_str(), true);
135     msg->setStringValue("message", txt);
136     msg = msg->getNode("dialog", true);
137     copyProperties(master, msg);
138     msg->setStringValue("name", name.c_str());
139     gui->newDialog(msg);
140     gui->showDialog(name.c_str());
141 }
142
143 // Message Box to report an error.
144 void guiErrorMessage (const char *txt)
145 {
146     SG_LOG(SG_GENERAL, SG_ALERT, txt);
147     mkDialog(txt);
148 }
149
150 // Message Box to report a throwable (usually an exception).
151 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
152 {
153     string msg = txt;
154     msg += '\n';
155     msg += throwable.getFormattedMessage();
156     if (!throwable.getOrigin().empty()) {
157         msg += "\n (reported by ";
158         msg += throwable.getOrigin();
159         msg += ')';
160     }
161     SG_LOG(SG_GENERAL, SG_ALERT, msg);
162     mkDialog(msg.c_str());
163 }
164
165
166
167 /* -----------------------------------------------------------------------
168 the Gui callback functions 
169 ____________________________________________________________________*/
170
171
172 // Hier Neu :-) This is my newly added code
173 // Added by David Findlay <nedz@bigpond.com>
174 // on Sunday 3rd of December
175
176
177 void helpCb ()
178 {
179     string command;
180         
181     SGPath path( globals->get_fg_root() );
182     path.append( "Docs/index.html" );
183         
184 #if !defined(WIN32)
185
186     command = globals->get_browser();
187     string::size_type pos;
188     if ((pos = command.find("%u", 0)) != string::npos)
189         command.replace(pos, 2, path.str());
190     else
191         command += " " + path.str();
192
193     command += " &";
194     system( command.c_str() );
195
196 #else // WIN32
197
198     // Look for favorite browser
199     char Dummy[1024], ExecName[1024], browserParameter[1024];
200     char win32_name[1024];
201 # ifdef __CYGWIN__
202     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
203 # else
204     strncpy(win32_name,path.c_str(), 1024);
205 # endif
206     Dummy[0] = 0;
207     FindExecutable(win32_name, Dummy, ExecName);
208     snprintf(browserParameter, 1024, "file:///%s", win32_name);
209     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
210                    SW_SHOWNORMAL ) ;
211
212 #endif
213         
214     mkDialog ("Help started in your web browser window.");
215 }
216
217 #if defined( TR_HIRES_SNAP)
218 void fgHiResDump()
219 {
220     FILE *f;
221     string message;
222     bool menu_status = fgGetBool("/sim/menubar/visibility");
223     char *filename = new char [24];
224     static int count = 1;
225
226     static const SGPropertyNode *master_freeze
227         = fgGetNode("/sim/freeze/master");
228
229     bool freeze = master_freeze->getBoolValue();
230     if ( !freeze ) {
231         fgSetBool("/sim/freeze/master", true);
232     }
233
234     fgSetBool("/sim/menubar/visibility", false);
235     int mouse = fgGetMouseCursor();
236     fgSetMouseCursor(MOUSE_CURSOR_NONE);
237
238     FGRenderer *renderer = globals->get_renderer();
239 //     renderer->init();
240     renderer->resize( fgGetInt("/sim/startup/xsize"),
241                       fgGetInt("/sim/startup/ysize") );
242
243     // we need two render frames here to clear the menu and cursor
244     // ... not sure why but doing an extra fgRenderFrame() shouldn't
245     // hurt anything
246     //renderer->update( true );
247     //renderer->update( true );
248
249     // This ImageSize stuff is a temporary hack
250     // should probably use 128x128 tile size and
251     // support any image size
252
253     // This should be a requester to get multiplier from user
254     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
255     int width = fgGetInt("/sim/startup/xsize");
256     int height = fgGetInt("/sim/startup/ysize");
257         
258     /* allocate buffer large enough to store one tile */
259     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
260     if (!tile) {
261         printf("Malloc of tile buffer failed!\n");
262         return;
263     }
264
265     int imageWidth  = multiplier*width;
266     int imageHeight = multiplier*height;
267
268     /* allocate buffer to hold a row of tiles */
269     GLubyte *buffer
270         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
271     if (!buffer) {
272         free(tile);
273         printf("Malloc of tile row buffer failed!\n");
274         return;
275     }
276     TRcontext *tr = trNew();
277     trTileSize(tr, width, height, 0);
278     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
279     trImageSize(tr, imageWidth, imageHeight);
280     trRowOrder(tr, TR_TOP_TO_BOTTOM);
281     // OSGFIXME
282 //     sgFrustum *frustum = ssgGetFrustum();
283 //     trFrustum(tr,
284 //               frustum->getLeft(), frustum->getRight(),
285 //               frustum->getBot(),  frustum->getTop(), 
286 //               frustum->getNear(), frustum->getFar());
287         
288     /* Prepare ppm output file */
289     while (count < 1000) {
290         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
291         if ( (f = fopen(filename, "r")) == NULL )
292             break;
293         fclose(f);
294     }
295     f = fopen(filename, "wb");
296     if (!f) {
297         printf("Couldn't open image file: %s\n", filename);
298         free(buffer);
299         free(tile);
300         return;
301     }
302     fprintf(f,"P6\n");
303     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
304     fprintf(f,"%i %i\n", imageWidth, imageHeight);
305     fprintf(f,"255\n");
306
307     /* just to be safe... */
308     glPixelStorei(GL_PACK_ALIGNMENT, 1);
309
310     /* Because the HUD and Panel change the ViewPort we will
311      * need to handle some lowlevel stuff ourselves */
312     int ncols = trGet(tr, TR_COLUMNS);
313     int nrows = trGet(tr, TR_ROWS);
314
315     bool do_hud = fgGetBool("/sim/hud/visibility");
316     GLfloat hud_col_step = 640.0 / ncols;
317     GLfloat hud_row_step = 480.0 / nrows;
318         
319     bool do_panel = fgPanelVisible();
320     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
321     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
322
323     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
324     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
325     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
326     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
327     glHint(GL_FOG_HINT, GL_NICEST);
328         
329     /* Draw tiles */
330     int more = 1;
331     while (more) {
332         trBeginTile(tr);
333         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
334         int curRow =  trGet(tr, TR_CURRENT_ROW);
335
336         renderer->update( false );
337         // OSGFIXME
338 //         if ( do_hud )
339 //             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
340 //                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
341         // OSGFIXME
342 //         if (do_panel)
343 //             globals->get_current_panel()->update(
344 //                                    curColumn*panel_col_step, panel_col_step,
345 //                                    curRow*panel_row_step,    panel_row_step );
346         more = trEndTile(tr);
347
348         /* save tile into tile row buffer*/
349         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
350         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
351         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
352         int xOffset = curColumn * bytesPerTileRow;
353         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
354         int i;
355         for (i=0;i<height;i++) {
356             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
357                    tile + i*bytesPerTileRow,              /* Src */
358                    bytesPerCurrentTileRow);               /* Byte count*/
359         }
360
361         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
362             /* write this buffered row of tiles to the file */
363             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
364             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
365             int i;
366             for (i=0;i<curTileHeight;i++) {
367                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
368                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
369                 fwrite(rowPtr, 1, imageWidth*3, f);
370             }
371         }
372
373     }
374
375     renderer->resize( width, height );
376
377     trDelete(tr);
378
379     glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
380     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
381     glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
382     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
383     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
384          (!fgGetBool("/sim/rendering/shading"))) {
385         // if fastest fog requested, or if flat shading force fastest
386         glHint ( GL_FOG_HINT, GL_FASTEST );
387     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
388         glHint ( GL_FOG_HINT, GL_DONT_CARE );
389     }
390
391     fclose(f);
392
393     message = "Snapshot saved to \"";
394     message += filename;
395     message += "\".";
396     mkDialog (message.c_str());
397
398     free(tile);
399     free(buffer);
400
401     delete [] filename;
402
403     fgSetMouseCursor(mouse);
404     fgSetBool("/sim/menubar/visibility", menu_status);
405
406     if ( !freeze ) {
407         fgSetBool("/sim/freeze/master", false);
408     }
409 }
410 #endif // #if defined( TR_HIRES_SNAP)
411
412
413 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
414
415 void rotateView( double roll, double pitch, double yaw )
416 {
417         // rotate view
418 }
419
420 GlBitmap *b1 = NULL;
421 GLubyte *hiResScreenCapture( int multiplier )
422 {
423     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
424     float fov = oldfov / multiplier;
425     FGViewer *v = globals->get_current_view();
426     fgSetDouble("/sim/current-view/field-of-view", fov);
427 //     globals->get_renderer()->init();
428     int cur_width = fgGetInt("/sim/startup/xsize");
429     int cur_height = fgGetInt("/sim/startup/ysize");
430     delete( b1 );
431     // New empty (mostly) bitmap
432     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
433     int x,y;
434     for ( y = 0; y < multiplier; y++ ) {
435         for ( x = 0; x < multiplier; x++ ) {
436             globals->get_renderer()->resize( cur_width, cur_height );
437             // pan to tile
438             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
439             globals->get_renderer()->update( false );
440             // restore view
441             GlBitmap b2;
442             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
443         }
444     }
445     fgSetDouble("/sim/current-view/field-of-view", oldfov);
446     return b1->getBitmap();
447 }
448 #endif
449
450 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
451 // win32 print screen function
452 void printScreen () {
453     int mouse = fgGetMouseCursor();
454     fgSetMouseCursor(MOUSE_CURSOR_NONE);
455
456     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
457     int cur_width = fgGetInt("/sim/startup/xsize");
458     int cur_height = fgGetInt("/sim/startup/ysize");
459     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
460     p.End( hiResScreenCapture(3) );
461
462     fgSetMouseCursor(mouse);
463 }
464 #endif // #ifdef WIN32
465
466
467 void fgDumpSnapShotWrapper () {
468     fgDumpSnapShot();
469 }
470
471
472 void fgHiResDumpWrapper () {
473     fgHiResDump();
474 }
475
476
477 // do a screen snap shot
478 bool fgDumpSnapShot () {
479     static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
480
481     bool freeze = master_freeze->getBoolValue();
482     if ( !freeze ) {
483         fgSetBool("/sim/freeze/master", true);
484     }
485
486     int mouse = fgGetMouseCursor();
487     fgSetMouseCursor(MOUSE_CURSOR_NONE);
488
489     fgSetBool("/sim/signals/screenshot", true);
490
491     FGRenderer *renderer = globals->get_renderer();
492     renderer->resize( fgGetInt("/sim/startup/xsize"),
493                       fgGetInt("/sim/startup/ysize") );
494
495     // we need two render frames here to clear the menu and cursor
496     // ... not sure why but doing an extra fgRenderFrame() shouldn't
497     // hurt anything
498     renderer->update( true );
499     renderer->update( true );
500
501     string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
502     SGPath path(dir + '/');
503     if (path.create_dir( 0755 )) {
504         SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
505                 << dir << "'. Trying home directory.");
506         dir = fgGetString("/sim/fg-home");
507     }
508
509     char filename[24];
510     static int count = 1;
511     while (count < 1000) {
512         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
513
514         SGPath p(dir);
515         p.append(filename);
516         if (!p.exists()) {
517             path.set(p.str());
518             break;
519         }
520     }
521
522     int result = sg_glDumpWindow(path.c_str(),
523                                  fgGetInt("/sim/startup/xsize"),
524                                  fgGetInt("/sim/startup/ysize"));
525
526     fgSetString("/sim/paths/screenshot-last", path.c_str());
527     fgSetBool("/sim/signals/screenshot", false);
528
529     fgSetMouseCursor(mouse);
530
531     if ( !freeze ) {
532         fgSetBool("/sim/freeze/master", false);
533     }
534     return result != 0;
535 }
536
537 // do an entire scenegraph dump
538 void fgDumpSceneGraph()
539 {
540     char *filename = new char [24];
541     string message;
542     static int count = 1;
543
544     FGRenderer *renderer = globals->get_renderer();
545
546     static const SGPropertyNode *master_freeze
547         = fgGetNode("/sim/freeze/master");
548
549     bool freeze = master_freeze->getBoolValue();
550     if ( !freeze ) {
551         fgSetBool("/sim/freeze/master", true);
552     }
553
554     while (count < 1000) {
555         FILE *fp;
556         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
557         if ( (fp = fopen(filename, "r")) == NULL )
558             break;
559         fclose(fp);
560     }
561
562     if ( fgDumpSceneGraphToFile(filename)) {
563         message = "Entire scene graph saved to \"";
564         message += filename;
565         message += "\".";
566     } else {
567         message = "Failed to save to \"";
568         message += filename;
569         message += "\".";
570     }
571
572     mkDialog (message.c_str());
573
574     delete [] filename;
575
576     if ( !freeze ) {
577         fgSetBool("/sim/freeze/master", false);
578     }
579 }
580
581     
582 // do an terrain branch dump
583 void fgDumpTerrainBranch()
584 {
585     char *filename = new char [24];
586     string message;
587     static int count = 1;
588
589     FGRenderer *renderer = globals->get_renderer();
590
591     static const SGPropertyNode *master_freeze
592         = fgGetNode("/sim/freeze/master");
593
594     bool freeze = master_freeze->getBoolValue();
595     if ( !freeze ) {
596         fgSetBool("/sim/freeze/master", true);
597     }
598
599     while (count < 1000) {
600         FILE *fp;
601         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
602         if ( (fp = fopen(filename, "r")) == NULL )
603             break;
604         fclose(fp);
605     }
606
607     if ( fgDumpTerrainBranchToFile(filename)) {
608         message = "Terrain graph saved to \"";
609         message += filename;
610         message += "\".";
611     } else {
612         message = "Failed to save to \"";
613         message += filename;
614         message += "\".";
615     }
616
617     mkDialog (message.c_str());
618
619     delete [] filename;
620
621     if ( !freeze ) {
622         fgSetBool("/sim/freeze/master", false);
623     }
624 }
625
626     
627