Open3D (C++ API)  0.12.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 
143  void SetOnTickEvent(std::function<bool()> callback);
144 
147  void ShowDialog(std::shared_ptr<Dialog> dlg);
149  void CloseDialog();
150 
151  void ShowMessageBox(const char* title, const char* message);
152 
155  void DestroyWindow();
156 
157 protected:
163  virtual Size CalcPreferredSize();
164 
168  virtual void Layout(const Theme& theme);
169 
170  // Override to handle menu items
171  virtual void OnMenuItemSelected(Menu::ItemId item_id);
172 
173  // Override to handle drag and drop on the windows.
174  virtual void OnDragDropped(const char* path);
175 
176  const std::vector<std::shared_ptr<Widget>>& GetChildren() const;
177 
178 private:
179  enum DrawResult { NONE, REDRAW };
180  DrawResult OnDraw();
181  Widget::DrawResult DrawOnce(bool is_layout_pass);
182  void ForceRedrawSceneWidget();
183  void OnResize();
184  void OnMouseEvent(const MouseEvent& e);
185  void OnKeyEvent(const KeyEvent& e);
186  void OnTextInput(const TextInputEvent& e);
187  bool OnTickEvent(const TickEvent& e);
188  void* MakeDrawContextCurrent() const;
189  void RestoreDrawContext(void* old_context) const;
190  void* GetNativeDrawable() const;
191 
192  static void DrawCallback(GLFWwindow* window);
193  static void ResizeCallback(GLFWwindow* window, int os_width, int os_height);
194  static void WindowMovedCallback(GLFWwindow* window, int os_x, int os_y);
195  static void RescaleCallback(GLFWwindow* window, float xscale, float yscale);
196  static void MouseMoveCallback(GLFWwindow* window, double x, double y);
197  static void MouseButtonCallback(GLFWwindow* window,
198  int button,
199  int action,
200  int mods);
201  static void MouseScrollCallback(GLFWwindow* window, double dx, double dy);
202  static void KeyCallback(
203  GLFWwindow* window, int key, int scancode, int action, int mods);
204  static void CharCallback(GLFWwindow* window, unsigned int utf32char);
205  static void DragDropCallback(GLFWwindow*, int count, const char* paths[]);
206  static void CloseCallback(GLFWwindow* window);
207  static void UpdateAfterEvent(Window* w);
208 
209 private:
210  struct Impl;
211  std::unique_ptr<Impl> impl_;
212 };
213 
214 } // namespace gui
215 } // namespace visualization
216 } // namespace open3d
friend class Renderer
Definition: Window.h:52
Rect GetOSFrame() const
Definition: Window.cpp:486
Definition: Theme.h:39
Definition: Application.h:55
Definition: Window.h:50
void ShowMessageBox(const char *title, const char *message)
Definition: Window.cpp:709
virtual ~Window()
Definition: Window.cpp:446
virtual void Layout(const Theme &theme)
Definition: Window.cpp:722
void RaiseToTop() const
Definition: Window.cpp:623
Point GlobalToWindowCoord(int global_x, int global_y)
Returns the global point (in OS pixels) in window local coordinates.
Definition: Window.cpp:590
void SetTitle(const char *title)
Definition: Window.cpp:500
virtual Size CalcPreferredSize()
Definition: Window.cpp:508
visualization::rendering::Renderer & GetRenderer() const
Definition: Window.cpp:482
Definition: Widget.h:62
void Close()
Definition: Window.cpp:608
void SetOSFrame(const Rect &r)
Definition: Window.cpp:493
void SetFocusWidget(Widget *w)
Sets.
Definition: Window.cpp:629
void CloseDialog()
Closes the dialog.
Definition: Window.cpp:686
void DestroyWindow()
Definition: Window.cpp:455
void SetNeedsLayout()
Instructs the window to relayout before the next draw.
Definition: Window.cpp:610
void SizeToFit()
Definition: Window.cpp:532
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:540
bool IsActiveWindow() const
Definition: Window.cpp:625
Window(const std::string &title, int flags=0)
Definition: Window.cpp:198
const Theme & GetTheme() const
Definition: Window.cpp:480
void PostRedraw()
Definition: Window.cpp:612
const char * GetTitle() const
Definition: Window.cpp:498
bool IsVisible() const
Definition: Window.cpp:596
static const int FLAG_HIDDEN
Definition: Window.h:55
virtual void OnDragDropped(const char *path)
Definition: Window.cpp:1258
float GetScaling() const
Returns the scaling factor from OS pixels to device pixels.
Definition: Window.cpp:575
virtual void OnMenuItemSelected(Menu::ItemId item_id)
Definition: Window.cpp:734
Size GetSize() const
Definition: Window.cpp:555
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:631
int ItemId
Definition: Menu.h:51
Definition: PinholeCameraIntrinsic.cpp:35
void Show(bool vis=true)
Definition: Window.cpp:600
int height
Definition: FilePCD.cpp:72
void SetOnMenuItemActivated(Menu::ItemId item_id, std::function< void()> callback)
Definition: Window.cpp:636
Rect GetContentRect() const
Definition: Window.cpp:561
const std::vector< std::shared_ptr< Widget > > & GetChildren() const
Definition: Window.cpp:462
Definition: Events.h:172
void SetOnTickEvent(std::function< bool()> callback)
Definition: Window.cpp:641
void ShowDialog(std::shared_ptr< Dialog > dlg)
Definition: Window.cpp:645
DrawResult
Definition: Widget.h:98
int width
Definition: FilePCD.cpp:71