Open3D (C++ API)  0.17.0
Window.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 
20 
21 namespace open3d {
22 namespace visualization {
23 namespace gui {
24 
25 class Dialog;
26 class Menu;
27 class Renderer;
28 struct Theme;
29 
30 class Window {
31  friend class Application;
32  friend class Renderer;
33 
34 public:
35  static const int FLAG_HIDDEN;
36  static const int FLAG_TOPMOST;
37 
41  explicit Window(const std::string& title, int flags = 0);
42 
46  Window(const std::string& title,
47  int width,
48  int height,
49  int flags = 0); // centered
50 
54  Window(const std::string& title,
55  int x,
56  int y,
57  int width,
58  int height,
59  int flags = 0);
60  virtual ~Window();
61 
62  const Theme& GetTheme() const;
64 
67  Rect GetOSFrame() const;
70  void SetOSFrame(const Rect& r);
71 
72  const char* GetTitle() const;
73  void SetTitle(const char* title);
74 
77  void SizeToFit();
78 
80  void SetSize(const Size& size);
83  Size GetSize() const;
86  Rect GetContentRect() const;
88  float GetScaling() const;
90  Point GlobalToWindowCoord(int global_x, int global_y);
91 
92  bool IsVisible() const;
93  void Show(bool vis = true);
96  void Close();
97 
99  void SetNeedsLayout();
102  void PostRedraw();
103 
104  void SetTopmost(bool topmost);
105  void RaiseToTop() const;
106 
107  bool IsActiveWindow() const;
108 
110  void SetFocusWidget(Widget* w);
111 
112  void AddChild(std::shared_ptr<Widget> w);
113 
118  std::function<void()> callback);
119 
123  void SetOnTickEvent(std::function<bool()> callback);
124 
128  void SetOnClose(std::function<bool()> callback);
129 
133  void SetOnKeyEvent(std::function<bool(const KeyEvent&)> callback);
134 
137  void ShowDialog(std::shared_ptr<Dialog> dlg);
139  void CloseDialog();
140 
141  void ShowMessageBox(const char* title, const char* message);
142 
145  void DestroyWindow();
146 
147  // Override to handle menu items
148  virtual void OnMenuItemSelected(Menu::ItemId item_id);
149 
150  // Override to handle drag and drop on the windows.
151  virtual void OnDragDropped(const char* path);
152 
153  // Shows or hides the menubar, except on macOS when using real windows.
154  // This is intended to be used when using HeadlessWindowSystem but may
155  // be useful in other circumstances.
156  void ShowMenu(bool show);
157 
158  int GetMouseMods() const; // internal, for WindowSystem
159 
163  std::string GetWebRTCUID() const;
164 
165 protected:
171  virtual Size CalcPreferredSize();
172 
176  virtual void Layout(const LayoutContext& context);
177 
179 
180  const std::vector<std::shared_ptr<Widget>>& GetChildren() const;
181 
182 public:
183  // these are intended for internal delivery of events
184  void OnDraw();
185  void OnResize();
186  void OnMouseEvent(const MouseEvent& e);
187  void OnKeyEvent(const KeyEvent& e);
188  void OnTextInput(const TextInputEvent& e);
189  void OnTickEvent(const TickEvent& e);
190 
192 
193 private:
194  void CreateRenderer();
195  Widget::DrawResult DrawOnce(bool is_layout_pass);
196  void* MakeDrawContextCurrent() const;
197  void RestoreDrawContext(void* old_context) const;
198 
199 private:
200  struct Impl;
201  std::unique_ptr<Impl> impl_;
202 };
203 
204 } // namespace gui
205 } // namespace visualization
206 } // namespace open3d
std::function< void(std::shared_ptr< core::Tensor >)> callback
Definition: FilamentRenderer.cpp:182
ImGuiContext * context
Definition: Window.cpp:76
Definition: Application.h:39
int ItemId
Definition: MenuBase.h:28
Definition: Widget.h:49
DrawResult
Definition: Widget.h:95
Definition: Window.h:30
WindowSystem::OSWindow GetOSWindow() const
Definition: Window.cpp:701
void SetOnMenuItemActivated(Menu::ItemId item_id, std::function< void()> callback)
Definition: Window.cpp:602
const Theme & GetTheme() const
Definition: Window.cpp:445
void OnMouseEvent(const MouseEvent &e)
Definition: Window.cpp:1034
void CloseDialog()
Closes the dialog.
Definition: Window.cpp:648
void SetTitle(const char *title)
Definition: Window.cpp:466
void SetOnKeyEvent(std::function< bool(const KeyEvent &)> callback)
Definition: Window.cpp:615
void PostRedraw()
Definition: Window.cpp:573
LayoutContext GetLayoutContext()
Definition: Window.cpp:679
void SetOnClose(std::function< bool()> callback)
Definition: Window.cpp:611
void AddChild(std::shared_ptr< Widget > w)
Definition: Window.cpp:597
const char * GetTitle() const
Definition: Window.cpp:464
void SetOSFrame(const Rect &r)
Definition: Window.cpp:458
std::string GetWebRTCUID() const
Definition: Window.cpp:418
friend class Renderer
Definition: Window.h:32
void ShowMessageBox(const char *title, const char *message)
Definition: Window.cpp:661
void ShowDialog(std::shared_ptr< Dialog > dlg)
Definition: Window.cpp:619
virtual void Layout(const LayoutContext &context)
Definition: Window.cpp:681
virtual void OnDragDropped(const char *path)
Definition: Window.cpp:1224
Size GetSize() const
Definition: Window.cpp:521
virtual Size CalcPreferredSize()
Definition: Window.cpp:475
int GetMouseMods() const
Definition: Window.cpp:416
void SetSize(const Size &size)
Sets the size of the window in pixels. Includes menubar on Linux.
Definition: Window.cpp:508
static const int FLAG_HIDDEN
Definition: Window.h:35
void Show(bool vis=true)
Definition: Window.cpp:554
visualization::rendering::Renderer & GetRenderer() const
Definition: Window.cpp:447
void OnResize()
Definition: Window.cpp:956
Rect GetOSFrame() const
Definition: Window.cpp:451
void Close()
Definition: Window.cpp:559
Point GlobalToWindowCoord(int global_x, int global_y)
Returns the global point (in OS pixels) in window local coordinates.
Definition: Window.cpp:543
void RaiseToTop() const
Definition: Window.cpp:585
const std::vector< std::shared_ptr< Widget > > & GetChildren() const
Definition: Window.cpp:431
void OnDraw()
Definition: Window.cpp:925
void SetNeedsLayout()
Instructs the window to relayout before the next draw.
Definition: Window.cpp:571
void DestroyWindow()
Definition: Window.cpp:409
bool IsVisible() const
Definition: Window.cpp:549
void OnTickEvent(const TickEvent &e)
Definition: Window.cpp:1204
bool IsActiveWindow() const
Definition: Window.cpp:590
void SetOnTickEvent(std::function< bool()> callback)
Definition: Window.cpp:607
static const int FLAG_TOPMOST
Definition: Window.h:36
float GetScaling() const
Returns the scaling factor from OS pixels to device pixels.
Definition: Window.cpp:538
virtual void OnMenuItemSelected(Menu::ItemId item_id)
Definition: Window.cpp:693
Rect GetContentRect() const
Definition: Window.cpp:526
void SetFocusWidget(Widget *w)
Sets.
Definition: Window.cpp:595
Window(const std::string &title, int flags=0)
Definition: Window.cpp:237
void ShowMenu(bool show)
Definition: Window.cpp:674
void OnTextInput(const TextInputEvent &e)
Definition: Window.cpp:1195
virtual ~Window()
Definition: Window.cpp:400
void SizeToFit()
Definition: Window.cpp:500
void OnKeyEvent(const KeyEvent &e)
Definition: Window.cpp:1154
void * OSWindow
Definition: WindowSystem.h:29
int width
Definition: FilePCD.cpp:52
int size
Definition: FilePCD.cpp:40
int height
Definition: FilePCD.cpp:53
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Events.h:203
Definition: Theme.h:20