Open3D (C++ API)
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 
33 #include "Open3D/GUI/Events.h"
34 #include "Open3D/GUI/Gui.h"
35 #include "Open3D/GUI/Menu.h"
36 #include "Open3D/GUI/Widget.h"
38 
39 struct GLFWwindow;
40 
41 namespace open3d {
42 namespace gui {
43 
44 class Dialog;
45 class Menu;
46 class Renderer;
47 struct Theme;
48 
49 class Window {
50  friend class Application;
51  friend class Renderer;
52 
53 public:
54  static const int FLAG_TOPMOST;
55 
59  explicit Window(const std::string& title, int flags = 0);
60 
64  Window(const std::string& title,
65  int width,
66  int height,
67  int flags = 0); // centered
68 
72  Window(const std::string& title,
73  int x,
74  int y,
75  int width,
76  int height,
77  int flags = 0);
78  virtual ~Window();
79 
80  const Theme& GetTheme() const;
82 
85  Rect GetOSFrame() const;
88  void SetOSFrame(const Rect& r);
89 
90  const char* GetTitle() const;
91  void SetTitle(const char* title);
92 
95  void SizeToFit();
96 
98  void SetSize(const Size& size);
101  Size GetSize() const;
104  Rect GetContentRect() const;
106  float GetScaling() const;
108  Point GlobalToWindowCoord(int global_x, int global_y);
109 
110  bool IsVisible() const;
111  void Show(bool vis = true);
114  void Close();
115 
117  void SetNeedsLayout();
120  void PostRedraw();
121 
122  void SetTopmost(bool topmost);
123  void RaiseToTop() const;
124 
125  bool IsActiveWindow() const;
126 
128  void SetFocusWidget(Widget* w);
129 
130  void AddChild(std::shared_ptr<Widget> w);
131 
134  void ShowDialog(std::shared_ptr<Dialog> dlg);
136  void CloseDialog();
137 
138  void ShowMessageBox(const char* title, const char* message);
139 
140 protected:
146  virtual Size CalcPreferredSize();
147 
151  virtual void Layout(const Theme& theme);
152 
153  // Override to handle menu items
154  virtual void OnMenuItemSelected(Menu::ItemId item_id);
155 
156  // Override to handle drag and drop on the windows.
157  virtual void OnDragDropped(const char* path);
158 
159 private:
160  enum DrawResult { NONE, REDRAW };
161  DrawResult OnDraw();
162  Widget::DrawResult DrawOnce(bool is_layout_pass);
163  void OnResize();
164  void OnMouseEvent(const MouseEvent& e);
165  void OnKeyEvent(const KeyEvent& e);
166  void OnTextInput(const TextInputEvent& e);
167  bool OnTickEvent(const TickEvent& e);
168  void* MakeDrawContextCurrent() const;
169  void RestoreDrawContext(void* old_context) const;
170  void* GetNativeDrawable() const;
171 
172  static void DrawCallback(GLFWwindow* window);
173  static void ResizeCallback(GLFWwindow* window, int os_width, int os_height);
174  static void RescaleCallback(GLFWwindow* window, float xscale, float yscale);
175  static void MouseMoveCallback(GLFWwindow* window, double x, double y);
176  static void MouseButtonCallback(GLFWwindow* window,
177  int button,
178  int action,
179  int mods);
180  static void MouseScrollCallback(GLFWwindow* window, double dx, double dy);
181  static void KeyCallback(
182  GLFWwindow* window, int key, int scancode, int action, int mods);
183  static void CharCallback(GLFWwindow* window, unsigned int utf32char);
184  static void DragDropCallback(GLFWwindow*, int count, const char* paths[]);
185  static void CloseCallback(GLFWwindow* window);
186  static void UpdateAfterEvent(Window* w);
187 
188 private:
189  struct Impl;
190  std::unique_ptr<Impl> impl_;
191 };
192 
193 } // namespace gui
194 } // namespace open3d
void SetNeedsLayout()
Instructs the window to relayout before the next draw.
Definition: Window.cpp:485
void Close()
Definition: Window.cpp:483
Definition: Events.h:171
virtual Size CalcPreferredSize()
Definition: Window.cpp:397
void PostRedraw()
Definition: Window.cpp:487
bool IsVisible() const
Definition: Window.cpp:471
Definition: Gui.h:32
virtual void OnMenuItemSelected(Menu::ItemId item_id)
Definition: Window.cpp:558
static const int FLAG_TOPMOST
Definition: Window.h:54
void SetTitle(const char *title)
Definition: Window.cpp:389
Point GlobalToWindowCoord(int global_x, int global_y)
Returns the global point (in OS pixels) in window local coordinates.
Definition: Window.cpp:465
void RaiseToTop() const
Definition: Window.cpp:489
Definition: Gui.h:40
Definition: Window.h:49
Definition: Window.cpp:139
Window(const std::string &title, int flags=0)
Definition: Window.cpp:176
const char * GetTitle() const
Definition: Window.cpp:387
const Theme & GetTheme() const
Definition: Window.cpp:369
int size
Definition: FilePCD.cpp:57
void Show(bool vis=true)
Definition: Window.cpp:475
void SetSize(const Size &size)
Sets the size of the window in pixels. Includes menubar on Linux.
Definition: Window.cpp:420
virtual void Layout(const Theme &theme)
Definition: Window.cpp:546
void SetTopmost(bool topmost)
Definition: Renderer.h:64
Definition: Events.h:74
float GetScaling() const
Returns the scaling factor from OS pixels to device pixels.
Definition: Window.cpp:454
void CloseDialog()
Closes the dialog.
Definition: Window.cpp:526
int count
Definition: FilePCD.cpp:59
Definition: Widget.h:53
void SizeToFit()
Definition: Window.cpp:412
Definition: Events.h:181
Definition: Events.h:53
Definition: Open3DViewer.h:29
void AddChild(std::shared_ptr< Widget > w)
Definition: Window.cpp:497
visualization::Renderer & GetRenderer() const
Definition: Window.cpp:371
virtual void OnDragDropped(const char *path)
Definition: Window.cpp:1030
void SetFocusWidget(Widget *w)
Sets.
Definition: Window.cpp:495
Definition: Gui.h:48
Size GetSize() const
Definition: Window.cpp:434
int height
Definition: FilePCD.cpp:70
void ShowMessageBox(const char *title, const char *message)
Definition: Window.cpp:533
void SetOSFrame(const Rect &r)
Definition: Window.cpp:382
Rect GetContentRect() const
Definition: Window.cpp:440
Rect GetOSFrame() const
Definition: Window.cpp:375
DrawResult
Definition: Widget.h:89
bool IsActiveWindow() const
Definition: Window.cpp:491
void ShowDialog(std::shared_ptr< Dialog > dlg)
Definition: Window.cpp:502
Definition: Application.h:40
int ItemId
Definition: Menu.h:45
virtual ~Window()
Definition: Window.cpp:347
void * GetNativeDrawable(GLFWwindow *glfw_window)
Definition: NativeLinux.cpp:40
int width
Definition: FilePCD.cpp:69
Definition: Theme.h:38