]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
Tidy up the autoconf/automake configuration a bit.
[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/viewmgr.hxx>
72
73 #ifdef FG_NETWORK_OLK
74 #include <NetworkOLK/network.h>
75 #endif
76    
77 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
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 #include "prop_picker.hxx"
88
89 SG_USING_STD(string);
90
91 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
92 SG_USING_STD(cout);
93 #endif
94
95 // main.cxx hack, should come from an include someplace
96 extern void fgInitVisuals( void );
97 extern void fgReshape( int width, int height );
98 extern void fgRenderFrame( void );
99
100 extern void initDialog (void);
101 extern void mkDialogInit (void);
102 extern void ConfirmExitDialogInit(void);
103
104
105 puFont guiFnt = 0;
106 fntTexFont *guiFntHandle = 0;
107 int gui_menu_on = 0;
108 puMenuBar    *mainMenuBar = 0;
109 //static puButton     *hideMenuButton = 0;
110
111 // from cockpit.cxx
112 extern void fgLatLonFormatToggle( puObject *);
113
114 // from gui_funcs.cxx
115 extern void saveFlight(puObject *);
116 extern void loadFlight(puObject *);
117 extern void reInit(puObject *);
118 extern void dumpSnapShot(puObject *);
119 #ifdef TR_HIRES_SNAP
120 extern void dumpHiResSnapShot(puObject *);
121 #endif
122 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
123 extern void printScreen(puObject *);
124 #endif
125 extern void MayBeGoodBye(puObject *);
126 extern void guiTogglePanel(puObject *);
127 extern void PilotOffsetAdjust(puObject *);
128 extern void fgHUDalphaAdjust(puObject *);
129 extern void prop_pickerView(puObject *);
130 extern void NewAirport(puObject *);
131 #ifdef FG_NETWORK_OLK
132 extern void net_display_toggle(puObject *);
133 extern void NewCallSign(puObject *);
134 extern void net_fgd_scan(puObject *);
135 extern void net_register(puObject *);
136 extern void net_unregister(puObject *);
137 #endif
138 extern void NewAltitude(puObject *);
139 extern void AddWayPoint(puObject *);
140 extern void PopWayPoint(puObject *);
141 extern void ClearRoute(puObject *);
142 extern void fgAPAdjust(puObject *);
143 extern void fgLatLonFormatToggle(puObject *);
144 extern void helpCb(puObject *);
145
146 static const struct {
147         char *name;
148         void (*fn)(puObject *);
149 } __fg_gui_fn[] = {
150
151         // File
152         {"saveFlight", saveFlight},
153         {"loadFlight", loadFlight},
154         {"reInit", reInit},
155 #ifdef TR_HIRES_SNAP
156         {"dumpHiResSnapShot", dumpHiResSnapShot},
157 #endif
158         {"dumpSnapShot", dumpSnapShot},
159 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
160         {"printScreen", printScreen},
161 #endif
162         {"MayBeGoodBye", MayBeGoodBye},
163
164         //View
165         {"guiTogglePanel", guiTogglePanel},
166         {"PilotOffsetAdjust", PilotOffsetAdjust},
167         {"fgHUDalphaAdjust", fgHUDalphaAdjust},
168         {"prop_pickerView", prop_pickerView},
169
170         // Environment
171         {"NewAirport", NewAirport},
172
173         // Network
174 #ifdef FG_NETWORK_OLK
175         {"net_display_toggle", net_display_toggle},
176         {"NewCallSign", NewCallSign},
177         {"net_fgd_scan", net_fgd_scan},
178         {"net_register", net_register},
179         {"net_unregister", net_unregister},
180 #endif
181
182         // Autopilot
183         {"NewAltitude", NewAltitude},
184         {"AddWayPoint", AddWayPoint},
185         {"PopWayPoint", PopWayPoint},
186         {"ClearRoute", ClearRoute},
187         {"fgAPAdjust", fgAPAdjust},
188         {"fgLatLonFormatToggle", fgLatLonFormatToggle},
189
190         // Help
191         {"helpCb", helpCb},
192
193         // Structure termination
194         {"", NULL}
195 };
196
197
198
199 struct fg_gui_t {
200         char *name;
201         char **submenu;
202         puCallback *cb;
203 } *Menu;
204 unsigned int Menu_size;
205
206 void initMenu()
207 {
208      SGPropertyNode main;
209      SGPath spath( globals->get_fg_root() );
210      spath.append( "menu.xml" );
211
212      try {
213          readProperties(spath.c_str(), &main);
214      } catch (const sg_exception &ex) {
215          SG_LOG(SG_GENERAL, SG_ALERT, "Error processing the menu file.");
216          return;
217      }
218
219      SG_LOG(SG_GENERAL, SG_INFO, "Reading menu entries.");
220
221      // Make the menu bar
222      mainMenuBar = new puMenuBar ();
223
224      SGPropertyNode *menu = main.getChild("menu");
225
226      vector<SGPropertyNode_ptr>submenu = menu->getChildren("submenu");
227
228      Menu_size = 1+submenu.size();
229      Menu = (fg_gui_t *)calloc(Menu_size, sizeof(fg_gui_t));
230
231      for (unsigned int h = 0; h < submenu.size(); h++) {
232
233          vector<SGPropertyNode_ptr>option = submenu[h]->getChildren("option");
234
235          //
236          // Make sure all entries will fit into allocated memory
237          //
238          Menu[h].submenu = (char **)calloc(1+option.size(), sizeof(char *));
239          Menu[h].cb = (puCallback *)calloc(1+option.size(), sizeof(puCallback));
240
241          for (unsigned int i = 0; i < option.size(); i++) {
242
243              SGPropertyNode *name = option[i]->getNode("name");
244              SGPropertyNode *call = option[i]->getNode("call");
245              SGPropertyNode *sep = option[i]->getNode("seperator");
246
247              if (sep)
248                 Menu[h].submenu[i] = strdup("----------");
249
250              else if (call && strcmp(call->getStringValue(), ""))
251                  Menu[h].submenu[i] = strdup(name->getStringValue());
252
253              else
254                  Menu[h].submenu[i] = strdup("not specified");
255
256              Menu[h].cb[i] = NULL;
257              for (unsigned int j=0; __fg_gui_fn[j].fn; j++)
258                  if (call &&
259                      !strcmp(call->getStringValue(), __fg_gui_fn[j].name) )
260                  {
261                      Menu[h].cb[i] = __fg_gui_fn[j].fn;
262                      break;
263                  }
264          }
265
266          SGPropertyNode *name = submenu[h]->getNode("name");
267
268          Menu[h].name = strdup(name->getStringValue());
269          mainMenuBar->add_submenu(Menu[h].name, Menu[h].submenu, Menu[h].cb);
270
271     }
272
273     mainMenuBar->close();
274 }
275
276
277 // FIXME: Has to be called from somewhere
278 //        or better yet, turn the menu into a class of its own
279 void destroyMenu(void) {
280     for(unsigned int i=0; i < Menu_size; i++) {
281
282         free(Menu[i].name);
283
284                                 // FIXME: don't use strdup/free
285         for(unsigned int j=0; Menu[i].submenu[j] != NULL; j++)
286            free(Menu[i].submenu[j]);
287     }
288 }
289
290
291
292
293 /* -------------------------------------------------------------------------
294 init the gui
295 _____________________________________________________________________*/
296
297
298 void guiInit()
299 {
300     char *mesa_win_state;
301
302     // Initialize PUI
303     puInit();
304     puSetDefaultStyle         ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
305     puSetDefaultColourScheme  (0.8, 0.8, 0.8, 0.4);
306
307     initDialog();
308
309     // Next check home directory
310     SGPath fntpath;
311     char* envp = ::getenv( "FG_FONTS" );
312     if ( envp != NULL ) {
313         fntpath.set( envp );
314     } else {
315         fntpath.set( globals->get_fg_root() );
316         fntpath.append( "Fonts" );
317     }
318
319     // Install our fast fonts
320     fntpath.append( "typewriter.txf" );
321     guiFntHandle = new fntTexFont ;
322     guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
323     puFont GuiFont ( guiFntHandle, 15 ) ;
324     puSetDefaultFonts( GuiFont, GuiFont ) ;
325     guiFnt = puGetDefaultLabelFont();
326   
327     if (!fgHasNode("/sim/startup/mouse-pointer")) {
328         // no preference specified for mouse pointer, attempt to autodetect...
329         // Determine if we need to render the cursor, or if the windowing
330         // system will do it.  First test if we are rendering with glide.
331         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
332             // Test for the MESA_GLX_FX env variable
333             if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
334                 // test if we are fullscreen mesa/glide
335                 if ( (mesa_win_state[0] == 'f') ||
336                      (mesa_win_state[0] == 'F') ) {
337                     puShowCursor ();
338                 }
339             }
340         }
341 //        mouse_active = ~mouse_active;
342     } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
343         // don't show pointer
344     } else {
345         // force showing pointer
346         puShowCursor();
347 //        mouse_active = ~mouse_active;
348     }
349         
350     // MOUSE_VIEW mode stuff
351         initMouseQuat();
352
353     // Set up our Dialog Boxes
354     ConfirmExitDialogInit();
355     NewAirportInit();
356         
357 #ifdef FG_NETWORK_OLK
358     NewNetIdInit();
359     NewNetFGDInit();
360 #endif
361
362     mkDialogInit();
363
364     initMenu();
365     
366     // Set up menu bar toggle
367     gui_menu_on = ~0;
368
369     if (!strcmp(fgGetString("/sim/flight-model"), "ada")) {
370         guiToggleMenu(); // Menu off by default
371     }
372 }