Eclipse SUMO - Simulation of Urban MObility
GUIMainWindow.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <algorithm>
28 #include <fx.h>
29 // fx3d includes windows.h so we need to guard against macro pollution
30 #ifdef WIN32
31 #define NOMINMAX
32 #endif
33 #include <fx3d.h>
34 #ifdef WIN32
35 #undef NOMINMAX
36 #endif
42 #include "GUIAppEnum.h"
43 #include "GUIMainWindow.h"
44 #include "GUIGlChildWindow.h"
45 
46 
47 // ===========================================================================
48 // static member definitions
49 // ===========================================================================
51 
52 // ===========================================================================
53 // member method definitions
54 // ===========================================================================
56  FXMainWindow(a, "SUMO-gui main window", nullptr, nullptr, DECOR_ALL, 20, 20, 600, 400),
57  myAmFullScreen(false),
58  myTrackerLock(true),
59  myGLVisual(new FXGLVisual(a, VISUAL_DOUBLEBUFFER)),
60  myAmGaming(false),
61  myListInternal(false),
62  myListParking(true),
63  myListTeleporting(false) {
64 
65  FXFontDesc fdesc;
66  getApp()->getNormalFont()->getFontDesc(fdesc);
67  fdesc.weight = FXFont::Bold;
68  myBoldFont = new FXFont(getApp(), fdesc);
69 
70  myTopDock = new FXDockSite(this, LAYOUT_SIDE_TOP | LAYOUT_FILL_X);
71  myBottomDock = new FXDockSite(this, LAYOUT_SIDE_BOTTOM | LAYOUT_FILL_X);
72  myLeftDock = new FXDockSite(this, LAYOUT_SIDE_LEFT | LAYOUT_FILL_Y);
73  myRightDock = new FXDockSite(this, LAYOUT_SIDE_RIGHT | LAYOUT_FILL_Y);
74  if (myInstance != nullptr) {
75  throw ProcessError("MainWindow initialized twice");
76  }
77  myInstance = this;
78  //myGLVisual->setStencilSize(8); // enable stencil buffer
79 }
80 
81 
83  delete myBoldFont;
84  delete myTopDock;
85  delete myBottomDock;
86  delete myLeftDock;
87  delete myRightDock;
88 }
89 
90 
91 
92 void
94  myGLWindows.push_back(child);
95 }
96 
97 
98 void
100  std::vector<GUIGlChildWindow*>::iterator i = std::find(myGLWindows.begin(), myGLWindows.end(), child);
101  if (i != myGLWindows.end()) {
102  myGLWindows.erase(i);
103  }
104 }
105 
106 
107 void
108 GUIMainWindow::addChild(FXMainWindow* child) {
109  myTrackerLock.lock();
110  myTrackerWindows.push_back(child);
111  myTrackerLock.unlock();
112 }
113 
114 
115 void
116 GUIMainWindow::removeChild(FXMainWindow* child) {
117  myTrackerLock.lock();
118  std::vector<FXMainWindow*>::iterator i = std::find(myTrackerWindows.begin(), myTrackerWindows.end(), child);
119  myTrackerWindows.erase(i);
120  myTrackerLock.unlock();
121 }
122 
123 
124 std::vector<std::string>
126  std::vector<std::string> ret;
127  for (GUIGlChildWindow* const window : myGLWindows) {
128  ret.push_back(window->getTitle().text());
129  }
130  return ret;
131 }
132 
133 
135 GUIMainWindow::getViewByID(const std::string& id) const {
136  for (GUIGlChildWindow* const window : myGLWindows) {
137  if (std::string(window->getTitle().text()) == id) {
138  return window;
139  }
140  }
141  return nullptr;
142 }
143 
144 
145 FXFont*
147  return myBoldFont;
148 }
149 
150 
151 void
153  // inform views
154  myMDIClient->forallWindows(this, FXSEL(SEL_COMMAND, MID_SIMSTEP), nullptr);
155  // inform other windows
156  myTrackerLock.lock();
157  for (int i = 0; i < (int)myTrackerWindows.size(); i++) {
158  myTrackerWindows[i]->handle(this, FXSEL(SEL_COMMAND, MID_SIMSTEP), nullptr);
159  }
160  myTrackerLock.unlock();
161 }
162 
163 
164 FXGLVisual*
166  return myGLVisual;
167 }
168 
169 
170 FXLabel&
172  return *myCartesianCoordinate;
173 }
174 
175 
176 FXLabel&
178  return *myGeoCoordinate;
179 }
180 
181 
184  if (myInstance != nullptr) {
185  return myInstance;
186  }
187  throw ProcessError("A GUIMainWindow instance was not yet constructed.");
188 }
189 
190 
193  GUIGlChildWindow* w = dynamic_cast<GUIGlChildWindow*>(myMDIClient->getActiveChild());
194  if (w != nullptr) {
195  return w->getView();
196  }
197  return nullptr;
198 }
199 
200 
201 void
203  int windowWidth = getApp()->reg().readIntEntry("SETTINGS", "width", 600);
204  int windowHeight = getApp()->reg().readIntEntry("SETTINGS", "height", 400);
205  const OptionsCont& oc = OptionsCont::getOptions();
206  if (oc.isSet("window-size")) {
207  std::vector<std::string> windowSize = oc.getStringVector("window-size");
208  if (windowSize.size() != 2) {
209  WRITE_ERROR("option window-size requires INT,INT");
210  } else {
211  try {
212  windowWidth = StringUtils::toInt(windowSize[0]);
213  windowHeight = StringUtils::toInt(windowSize[1]);
214  } catch (NumberFormatException& e) {
215  WRITE_ERROR("option window-size requires INT,INT " + toString(e.what()));
216  }
217  }
218  }
219  if (oc.isSet("window-size") || getApp()->reg().readIntEntry("SETTINGS", "maximized", 0) == 0 || oc.isSet("window-pos")) {
220  // when restoring previous pos, make sure the window fits fully onto the current screen
221  int x = MAX2(0, MIN2(getApp()->reg().readIntEntry("SETTINGS", "x", 150), getApp()->getRootWindow()->getWidth() - windowWidth));
222  int y = MAX2(50, MIN2(getApp()->reg().readIntEntry("SETTINGS", "y", 150), getApp()->getRootWindow()->getHeight() - windowHeight));
223  if (oc.isSet("window-pos")) {
224  std::vector<std::string> windowPos = oc.getStringVector("window-pos");
225  if (windowPos.size() != 2) {
226  WRITE_ERROR("option window-pos requires INT,INT");
227  } else {
228  try {
229  x = StringUtils::toInt(windowPos[0]);
230  y = StringUtils::toInt(windowPos[1]);
231  } catch (NumberFormatException& e) {
232  WRITE_ERROR("option window-pos requires INT,INT " + toString(e.what()));
233  }
234  }
235  }
236  move(x, y);
237  resize(windowWidth, windowHeight);
238  }
239 }
240 
241 void
243  if (!myAmFullScreen) {
244  getApp()->reg().writeIntEntry("SETTINGS", "x", getX());
245  getApp()->reg().writeIntEntry("SETTINGS", "y", getY());
246  getApp()->reg().writeIntEntry("SETTINGS", "width", getWidth());
247  getApp()->reg().writeIntEntry("SETTINGS", "height", getHeight());
248  }
249 }
250 
251 /****************************************************************************/
252 
std::vector< FXMainWindow * > myTrackerWindows
FXLabel * myGeoCoordinate
FXGLVisual * getGLVisual() const
void setWindowSizeAndPos()
perform initial window positioning and sizing according to user options / previous call ...
FXFont * myBoldFont
Font used for popup-menu titles.
FXDockSite * myRightDock
T MAX2(T a, T b)
Definition: StdDefs.h:80
FXGLVisual * myGLVisual
The gl-visual used.
FXMutex myTrackerLock
A lock to make the removal and addition of trackers secure.
std::vector< std::string > getViewIDs() const
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
FXLabel & getCartesianLabel()
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
GUISUMOAbstractView * getActiveView() const
get the active view or 0
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
static GUIMainWindow * getInstance()
FXDockSite * myLeftDock
static GUIMainWindow * myInstance
the singleton window instance
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
FXFont * getBoldFont()
T MIN2(T a, T b)
Definition: StdDefs.h:74
void addGLChild(GUIGlChildWindow *child)
Adds a further child window to the list.
FXLabel & getGeoLabel()
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
std::vector< GUIGlChildWindow * > myGLWindows
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
void removeChild(FXMainWindow *child)
FXLabel * myCartesianCoordinate
Labels for the current cartesian and geo-coordinate.
FXDockSite * myBottomDock
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
A Simulation step was performed.
Definition: GUIAppEnum.h:441
FXDockSite * myTopDock
A storage for options typed value containers)
Definition: OptionsCont.h:90
virtual ~GUIMainWindow()
FXMDIClient * myMDIClient
The multi view panel.
bool myAmFullScreen
whether to show the window in full screen mode
GUIGlChildWindow * getViewByID(const std::string &id) const
void addChild(FXMainWindow *child)
void storeWindowSizeAndPos()
record window position and size in registry
void removeGLChild(GUIGlChildWindow *child)
removes the given child window from the list