]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
425f4c066e52a14e065aaeb8068fc6cd9016bbe2
[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 HAVE_WINDOWS_H
34 #  include <windows.h>
35 #endif
36
37 #include <simgear/misc/props.hxx>
38 #include <simgear/misc/props_io.hxx>
39 #include <simgear/misc/exception.hxx>
40 #include <simgear/misc/sg_path.hxx>
41
42 #include <plib/pu.h>
43
44 #include <Include/general.hxx>
45 #include <Main/globals.hxx>
46 #include <Main/fg_props.hxx>
47
48 #include "gui.h"
49 #include "gui_local.hxx"
50 #include "net_dlg.hxx"
51 #include "preset_dlg.hxx"
52
53
54 // main.cxx hack, should come from an include someplace
55 extern void fgInitVisuals( void );
56 extern void fgRenderFrame( void );
57
58 extern void initDialog (void);
59 extern void mkDialogInit (void);
60 extern void ConfirmExitDialogInit(void);
61
62
63 puFont guiFnt = 0;
64 fntTexFont *guiFntHandle = 0;
65 #if defined(FG_OLD_MENUBAR)
66 int gui_menu_on = 0;
67 puMenuBar    *mainMenuBar = 0;
68 //static puButton     *hideMenuButton = 0;
69
70
71 struct fg_gui_t {
72         char *name;
73         char **submenu;
74         puCallback *cb;
75 } *Menu;
76 unsigned int Menu_size;
77
78 void initMenu()
79 {
80      SGPropertyNode main;
81
82      try {
83          fgLoadProps("menu.xml", &main);
84      } catch (const sg_exception &ex) {
85          SG_LOG(SG_GENERAL, SG_ALERT, "Error processing the menu file.");
86          return;
87      }
88
89      SG_LOG(SG_GENERAL, SG_INFO, "Reading menu entries.");
90
91      // Make the menu bar
92      mainMenuBar = new puMenuBar ();
93
94      SGPropertyNode *menu = main.getChild("menu");
95      SGPropertyNode *locale = globals->get_locale();
96
97      vector<SGPropertyNode_ptr>submenu = menu->getChildren("submenu");
98
99      Menu_size = 1+submenu.size();
100      Menu = (fg_gui_t *)calloc(Menu_size, sizeof(fg_gui_t));
101
102      for (unsigned int h = 0; h < submenu.size(); h++) {
103
104          vector<SGPropertyNode_ptr>option = submenu[h]->getChildren("option");
105
106          //
107          // Make sure all entries will fit into allocated memory
108          //
109          Menu[h].submenu = (char **)calloc(1+option.size(), sizeof(char *));
110          Menu[h].cb = (puCallback *)calloc(1+option.size(), sizeof(puCallback));
111
112          for (unsigned int i = 0; i < option.size(); i++) {
113
114              SGPropertyNode *name = option[i]->getNode("name");
115              SGPropertyNode *call = option[i]->getNode("call");
116              SGPropertyNode *sep = option[i]->getNode("seperator");
117
118              int pos = option.size()-i-1;
119              if (sep) {
120                 Menu[h].submenu[pos] = strdup("----------");
121              } else if (call && strcmp(call->getStringValue(), "")) {
122                  string text = locale->getStringValue( name->getStringValue(),
123                                                        "strings/null" );
124                  Menu[h].submenu[pos]
125                      = strdup(text.c_str());
126              } else {
127                  Menu[h].submenu[pos] = strdup("not specified");
128              }
129
130              Menu[h].cb[pos] = NULL;
131              for (unsigned int j=0; __fg_gui_fn[j].fn; j++)
132                  if (call &&
133                      !strcmp(call->getStringValue(), __fg_gui_fn[j].name) )
134                  {
135                      Menu[h].cb[pos] = __fg_gui_fn[j].fn;
136                      break;
137                  }
138          }
139
140          SGPropertyNode *name = submenu[h]->getNode("name");
141          string text = locale->getStringValue( name->getStringValue(),
142                                                "strings/null" );
143
144          Menu[h].name = strdup(text.c_str());
145          mainMenuBar->add_submenu(Menu[h].name, Menu[h].submenu, Menu[h].cb);
146
147     }
148
149     mainMenuBar->close();
150 }
151
152
153 // FIXME: Has to be called from somewhere
154 //        or better yet, turn the menu into a class of its own
155 void destroyMenu(void) {
156     for(unsigned int i=0; i < Menu_size; i++) {
157
158         free(Menu[i].name);
159
160                                 // FIXME: don't use strdup/free
161         for(unsigned int j=0; Menu[i].submenu[j] != NULL; j++)
162            free(Menu[i].submenu[j]);
163     }
164 }
165 #endif
166
167
168
169 /* -------------------------------------------------------------------------
170 init the gui
171 _____________________________________________________________________*/
172
173
174 void guiInit()
175 {
176     char *mesa_win_state;
177
178     // Initialize PUI
179     puInit();
180     puSetDefaultStyle         ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
181     puSetDefaultColourScheme  (0.8, 0.8, 0.9, 0.8);
182
183     initDialog();
184
185     // Next check home directory
186     SGPath fntpath;
187     char* envp = ::getenv( "FG_FONTS" );
188     if ( envp != NULL ) {
189         fntpath.set( envp );
190     } else {
191         fntpath.set( globals->get_fg_root() );
192         fntpath.append( "Fonts" );
193     }
194
195     // Install our fast fonts
196     SGPropertyNode *locale = globals->get_locale();
197     fntpath.append(locale->getStringValue("font", "typewriter.txf"));
198     guiFntHandle = new fntTexFont ;
199     guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
200     puFont GuiFont ( guiFntHandle, 15 ) ;
201     puSetDefaultFonts( GuiFont, GuiFont ) ;
202     guiFnt = puGetDefaultLabelFont();
203   
204     if (!fgHasNode("/sim/startup/mouse-pointer")) {
205         // no preference specified for mouse pointer, attempt to autodetect...
206         // Determine if we need to render the cursor, or if the windowing
207         // system will do it.  First test if we are rendering with glide.
208         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
209             // Test for the MESA_GLX_FX env variable
210             if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
211                 // test if we are fullscreen mesa/glide
212                 if ( (mesa_win_state[0] == 'f') ||
213                      (mesa_win_state[0] == 'F') ) {
214                     puShowCursor ();
215                 }
216             }
217         }
218 //        mouse_active = ~mouse_active;
219     } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
220         // don't show pointer
221     } else {
222         // force showing pointer
223         puShowCursor();
224 //        mouse_active = ~mouse_active;
225     }
226         
227     // MOUSE_VIEW mode stuff
228         initMouseQuat();
229
230     // Set up our Dialog Boxes
231     ConfirmExitDialogInit();
232     fgPresetInit();
233         
234 #ifdef FG_NETWORK_OLK
235     NewNetIdInit();
236     NewNetFGDInit();
237 #endif
238
239     mkDialogInit();
240
241 #if defined(FG_OLD_MENUBAR)
242     initMenu();
243     
244     // Set up menu bar toggle
245     gui_menu_on = ~0;
246
247     if (!strcmp(fgGetString("/sim/flight-model"), "ada")) {
248         guiToggleMenu(); // Menu off by default
249     }
250 #endif
251 }