ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Window.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 
15 #include "visualization/gui/Gui.h"
16 #include "visualization/gui/Menu.h"
20 
21 namespace cloudViewer {
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 cloudViewer
std::function< void(std::shared_ptr< core::Tensor >)> callback
int width
int size
int height
void SetFocusWidget(Widget *w)
Sets.
Definition: Window.cpp:595
const std::vector< std::shared_ptr< Widget > > & GetChildren() const
Definition: Window.cpp:431
void SetNeedsLayout()
Instructs the window to relayout before the next draw.
Definition: Window.cpp:571
void OnTickEvent(const TickEvent &e)
Definition: Window.cpp:1204
void OnTextInput(const TextInputEvent &e)
Definition: Window.cpp:1195
const Theme & GetTheme() const
Definition: Window.cpp:445
Window(const std::string &title, int flags=0)
Definition: Window.cpp:237
Point GlobalToWindowCoord(int global_x, int global_y)
Returns the global point (in OS pixels) in window local coordinates.
Definition: Window.cpp:543
void SetSize(const Size &size)
Sets the size of the window in pixels. Includes menubar on Linux.
Definition: Window.cpp:508
void AddChild(std::shared_ptr< Widget > w)
Definition: Window.cpp:597
WindowSystem::OSWindow GetOSWindow() const
Definition: Window.cpp:701
void SetOnMenuItemActivated(Menu::ItemId item_id, std::function< void()> callback)
Definition: Window.cpp:602
void SetOnKeyEvent(std::function< bool(const KeyEvent &)> callback)
Definition: Window.cpp:615
visualization::rendering::Renderer & GetRenderer() const
Definition: Window.cpp:447
void SetTitle(const char *title)
Definition: Window.cpp:466
virtual void Layout(const LayoutContext &context)
Definition: Window.cpp:681
void ShowDialog(std::shared_ptr< Dialog > dlg)
Definition: Window.cpp:619
void OnKeyEvent(const KeyEvent &e)
Definition: Window.cpp:1154
void SetOnTickEvent(std::function< bool()> callback)
Definition: Window.cpp:607
void CloseDialog()
Closes the dialog.
Definition: Window.cpp:648
void ShowMessageBox(const char *title, const char *message)
Definition: Window.cpp:661
float GetScaling() const
Returns the scaling factor from OS pixels to device pixels.
Definition: Window.cpp:538
void SetOnClose(std::function< bool()> callback)
Definition: Window.cpp:611
virtual void OnMenuItemSelected(Menu::ItemId item_id)
Definition: Window.cpp:693
void OnMouseEvent(const MouseEvent &e)
Definition: Window.cpp:1034
virtual void OnDragDropped(const char *path)
Definition: Window.cpp:1224
ImGuiContext * context
Definition: Window.cpp:76
static const std::string path
Definition: PointCloud.cpp:59
Generic file read and write utility for python interface.