Open3D (C++ API)  0.11.0
Window.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <functional>
30 #include <memory>
31 #include <string>
32 
38 
39 struct GLFWwindow;
40 
41 namespace open3d {
42 namespace visualization {
43 namespace gui {
44 
45 class Dialog;
46 class Menu;
47 class Renderer;
48 struct Theme;
49 
50 class Window {
51  friend class Application;
52  friend class Renderer;
53 
54 public:
55  static const int FLAG_HIDDEN;
56  static const int FLAG_TOPMOST;
57 
61  explicit Window(const std::string& title, int flags = 0);
62 
66  Window(const std::string& title,
67  int width,
68  int height,
69  int flags = 0); // centered
70 
74  Window(const std::string& title,
75  int x,
76  int y,
77  int width,
78  int height,
79  int flags = 0);
80  virtual ~Window();
81 
82  const Theme& GetTheme() const;
84 
87  Rect GetOSFrame() const;
90  void SetOSFrame(const Rect& r);
91 
92  const char* GetTitle() const;
93  void SetTitle(const char* title);
94 
97  void SizeToFit();
98 
100  void SetSize(const Size& size);
103  Size GetSize() const;
106  Rect GetContentRect() const;
108  float GetScaling() const;
110  Point GlobalToWindowCoord(int global_x, int global_y);
111 
112  bool IsVisible() const;
113  void Show(bool vis = true);
116  void Close();
117 
119  void SetNeedsLayout();
122  void PostRedraw();
123 
124  void SetTopmost(bool topmost);
125  void RaiseToTop() const;
126 
127  bool IsActiveWindow() const;
128 
130  void SetFocusWidget(Widget* w);
131 
132  void AddChild(std::shared_ptr<Widget> w);
133 
138  std::function<void()> callback);
139 
140  void SetOnTickEvent(std::function<bool()> callback);
141 
144  void ShowDialog(std::shared_ptr<Dialog> dlg);
146  void CloseDialog();
147 
148  void ShowMessageBox(const char* title, const char* message);
149 
152  void DestroyWindow();
153 
154 protected:
160  virtual Size CalcPreferredSize();
161 
165  virtual void Layout(const Theme& theme);
166 
167  // Override to handle menu items
168  virtual void OnMenuItemSelected(Menu::ItemId item_id);
169 
170  // Override to handle drag and drop on the windows.
171  virtual void OnDragDropped(const char* path);
172 
173  const std::vector<std::shared_ptr<Widget>>& GetChildren() const;
174 
175 private:
176  enum DrawResult { NONE, REDRAW };
177  DrawResult OnDraw();
178  Widget::DrawResult DrawOnce(bool is_layout_pass);
179  void ForceRedrawSceneWidget();
180  void OnResize();
181  void OnMouseEvent(const MouseEvent& e);
182  void OnKeyEvent(const KeyEvent& e);
183  void OnTextInput(const TextInputEvent& e);
184  bool OnTickEvent(const TickEvent& e);
185  void* MakeDrawContextCurrent() const;
186  void RestoreDrawContext(void* old_context) const;
187  void* GetNativeDrawable() const;
188 
189  static void DrawCallback(GLFWwindow* window);
190  static void ResizeCallback(GLFWwindow* window, int os_width, int os_height);
191  static void WindowMovedCallback(GLFWwindow* window, int os_x, int os_y);
192  static void RescaleCallback(GLFWwindow* window, float xscale, float yscale);
193  static void MouseMoveCallback(GLFWwindow* window, double x, double y);
194  static void MouseButtonCallback(GLFWwindow* window,
195  int button,
196  int action,
197  int mods);
198  static void MouseScrollCallback(GLFWwindow* window, double dx, double dy);
199  static void KeyCallback(
200  GLFWwindow* window, int key, int scancode, int action, int mods);
201  static void CharCallback(GLFWwindow* window, unsigned int utf32char);
202  static void DragDropCallback(GLFWwindow*, int count, const char* paths[]);
203  static void CloseCallback(GLFWwindow* window);
204  static void UpdateAfterEvent(Window* w);
205 
206 private:
207  struct Impl;
208  std::unique_ptr<Impl> impl_;
209 };
210 
211 } // namespace gui
212 } // namespace visualization
213 } // namespace open3d
friend class Renderer
Definition: Window.h:52
Rect GetOSFrame() const
Definition: Window.cpp:423
Definition: Theme.h:39
Definition: Application.h:52
Definition: Window.h:50
void ShowMessageBox(const char *title, const char *message)
Definition: Window.cpp:637
virtual ~Window()
Definition: Window.cpp:383
virtual void Layout(const Theme &theme)
Definition: Window.cpp:650
void RaiseToTop() const
Definition: Window.cpp:560
Point GlobalToWindowCoord(int global_x, int global_y)
Returns the global point (in OS pixels) in window local coordinates.
Definition: Window.cpp:527
void SetTitle(const char *title)
Definition: Window.cpp:437
virtual Size CalcPreferredSize()
Definition: Window.cpp:445
visualization::rendering::Renderer & GetRenderer() const
Definition: Window.cpp:419
Definition: Widget.h:62
void Close()
Definition: Window.cpp:545
void SetOSFrame(const Rect &r)
Definition: Window.cpp:430
void SetFocusWidget(Widget *w)
Sets.
Definition: Window.cpp:566
void CloseDialog()
Closes the dialog.
Definition: Window.cpp:623
void DestroyWindow()
Definition: Window.cpp:392
void SetNeedsLayout()
Instructs the window to relayout before the next draw.
Definition: Window.cpp:547
void SizeToFit()
Definition: Window.cpp:469
void * GetNativeDrawable(GLFWwindow *glfw_window)
Definition: NativeLinux.cpp:39
int size
Definition: FilePCD.cpp:59
void SetSize(const Size &size)
Sets the size of the window in pixels. Includes menubar on Linux.
Definition: Window.cpp:477
bool IsActiveWindow() const
Definition: Window.cpp:562
Window(const std::string &title, int flags=0)
Definition: Window.cpp:198
const Theme & GetTheme() const
Definition: Window.cpp:417
void PostRedraw()
Definition: Window.cpp:549
const char * GetTitle() const
Definition: Window.cpp:435
bool IsVisible() const
Definition: Window.cpp:533
static const int FLAG_HIDDEN
Definition: Window.h:55
virtual void OnDragDropped(const char *path)
Definition: Window.cpp:1186
float GetScaling() const
Returns the scaling factor from OS pixels to device pixels.
Definition: Window.cpp:512
virtual void OnMenuItemSelected(Menu::ItemId item_id)
Definition: Window.cpp:662
Size GetSize() const
Definition: Window.cpp:492
int count
Definition: FilePCD.cpp:61
static const int FLAG_TOPMOST
Definition: Window.h:56
void AddChild(std::shared_ptr< Widget > w)
Definition: Window.cpp:568
int ItemId
Definition: Menu.h:51
Definition: PinholeCameraIntrinsic.cpp:35
void Show(bool vis=true)
Definition: Window.cpp:537
int height
Definition: FilePCD.cpp:72
void SetOnMenuItemActivated(Menu::ItemId item_id, std::function< void()> callback)
Definition: Window.cpp:573
Rect GetContentRect() const
Definition: Window.cpp:498
const std::vector< std::shared_ptr< Widget > > & GetChildren() const
Definition: Window.cpp:399
Definition: Events.h:172
void SetOnTickEvent(std::function< bool()> callback)
Definition: Window.cpp:578
void ShowDialog(std::shared_ptr< Dialog > dlg)
Definition: Window.cpp:582
DrawResult
Definition: Widget.h:98
int width
Definition: FilePCD.cpp:71