libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UIWidget.hpp
Go to the documentation of this file.
1 #ifndef UIWIDGET_HPP
2 #define UIWIDGET_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <memory>
22 #include <iostream>
23 #include "DrawContext.hpp"
24 #include "Signal.hpp"
25 #include "Color.hpp"
26 #include "SDLTexture.hpp"
27 #include "UIMouseEventsBase.hpp"
28 
29 namespace AlgAudio{
30 
31 class Window;
32 
34 public:
35  virtual void RequestFocus() = 0;
36 };
37 
65 class UIWidget : public UIMouseEventsBase, public virtual SubscriptionsManager, public std::enable_shared_from_this<UIWidget>{
66 protected:
67  UIWidget(std::weak_ptr<Window> parent_window);
68 public:
69 
70  virtual ~UIWidget() {}
71 
72  void Draw(DrawContext& c);
73  virtual void CustomDraw(DrawContext& c) = 0;
74  void Resize(Size2D s);
75  virtual void CustomResize(Size2D) {};
76 
77  /* Mouse events are inherited from UIMouseEventsBase */
78  /* Keyboard event. */
79  virtual void OnKeyboard(KeyData) {}
80 
84 
85  std::weak_ptr<UIWidget> parent;
86 
88  inline bool HasZeroArea() const {return GetRequestedSize().IsEmpty(); }
89 
99  inline Size2D GetRequestedSize() const {
100  if(display_mode == DisplayMode::Invisible) return Size2D(0,0);
101  return Size2D(
102  std::max(minimal_size.width, custom_size.width ),
103  std::max(minimal_size.height, custom_size.height)
104  );
105  }
111 
114  void SetCustomSize(Size2D size);
115 
116  void SetBackColor(const Color& c){
117  clear_color = c;
119  }
120  void SetFrontColor(const Color& c){
121  overlay_color = c;
123  }
124 
125  bool HasParent(){
126  // Conversion from shared_ptr to bool determines whether the
127  // pointer is non-empty
128  return (bool)(parent.lock());
129  }
130 
133  Point2D GetPosInParent(std::shared_ptr<UIWidget> ancestor);
134 
137  class ID{
138  public:
139  ID() : id("none") {}
140  ID(const ID& other) : id(other.id) {}
141  explicit ID(const std::string& s) : id(s) {}
142  std::string ToString() const {return id;}
143  bool operator==(const ID& other) const {return id == other.id;}
144  bool operator!=(const ID& other) const {return id != other.id;}
145  bool operator<(const ID& other) const {return id < other.id;}
146  ID& operator=(const ID& other) {id = other.id; return *this;}
147  private:
148  std::string id;
149  };
151 
154  std::shared_ptr<UIWidget> FindChild(ID search_id){
155  if(search_id == widget_id) return shared_from_this();
156  else return CustomFindChild(search_id);
157  }
158  virtual std::shared_ptr<UIWidget> CustomFindChild(ID) const {return nullptr;}
159 
162  virtual void RequestFocus() override;
165  virtual void OnFocusChanged(bool) {}
166 
167  std::shared_ptr<Window> GetWindow() {return window.lock();}
168 
172  bool debug_this_widget = false;
173 protected:
182  void SetNeedsRedrawing();
183 
185  std::weak_ptr<Window> window;
186 
195  void SetMinimalSize(Size2D);
196 
198  virtual void OnChildRequestedSizeChanged() {}
200  virtual void OnChildVisibilityChanged() {}
208  virtual void OnChildFocusRequested(std::shared_ptr<UIWidget>) {}
213  virtual bool OnChildFocusTested(std::shared_ptr<const UIWidget>) {return false;}
214 
220  bool IsFocused() const;
221 
226  bool IsRoot() const;
227 private:
228  Color clear_color = Color(0,0,0,0);
229  Color overlay_color = Color(0,0,0,0);
230  bool needs_redrawing = true;
231  Size2D minimal_size = Size2D(0,0);
232  Size2D custom_size = Size2D(0,0);
233 
234  // This flag is used to track incorrect usage of SetMinimalSize()
235  bool in_custom_resize = false;
236 
237  std::shared_ptr<SDLTexture> cache_texture;
238  void RedrawToCache(Size2D size);
239 };
240 
241 
242 } // namespace AlgAudio
243 #endif //UIWIDGET_HPP
ID()
Definition: UIWidget.hpp:139
Definition: UIWidget.hpp:65
Definition: Color.hpp:28
Size2D GetCurrentSize() const
Definition: UIWidget.hpp:110
virtual void CustomDraw(DrawContext &c)=0
void Draw(DrawContext &c)
Definition: DrawContext.hpp:65
ID widget_id
Definition: UIWidget.hpp:150
std::weak_ptr< UIWidget > parent
Definition: UIWidget.hpp:85
bool IsRoot() const
virtual void RequestFocus() override
UIWidget(std::weak_ptr< Window > parent_window)
int height
Definition: Utilities.hpp:46
Size2D current_size
Definition: UIWidget.hpp:174
Definition: Signal.hpp:247
void TriggerFakeResize()
Definition: UIWidget.hpp:83
Point2D GetPosInParent(std::shared_ptr< UIWidget > ancestor)
virtual ~UIWidget()
Definition: UIWidget.hpp:70
std::string ToString() const
Definition: UIWidget.hpp:142
ID(const ID &other)
Definition: UIWidget.hpp:140
bool operator<(const ID &other) const
Definition: UIWidget.hpp:145
bool HasParent()
Definition: UIWidget.hpp:125
Definition: UIWidget.hpp:33
bool debug_this_widget
Definition: UIWidget.hpp:172
bool IsFocused() const
Size2D GetRequestedSize() const
Definition: UIWidget.hpp:99
virtual void CustomResize(Size2D)
Definition: UIWidget.hpp:75
virtual void RequestFocus()=0
Definition: Utilities.hpp:34
int width
Definition: Utilities.hpp:46
Definition: Alertable.hpp:26
Definition: UIWidget.hpp:137
virtual void OnChildRequestedSizeChanged()
Definition: UIWidget.hpp:198
DisplayMode display_mode
Definition: UIVisibilityBase.hpp:50
Definition: UIMouseEventsBase.hpp:26
std::shared_ptr< Window > GetWindow()
Definition: UIWidget.hpp:167
virtual std::shared_ptr< UIWidget > CustomFindChild(ID) const
Definition: UIWidget.hpp:158
ID & operator=(const ID &other)
Definition: UIWidget.hpp:146
virtual bool OnChildFocusTested(std::shared_ptr< const UIWidget >)
Definition: UIWidget.hpp:213
virtual void OnKeyboard(KeyData)
Definition: UIWidget.hpp:79
virtual void OnChildVisibilityChanged()
Definition: UIWidget.hpp:200
void Resize(Size2D s)
ID(const std::string &s)
Definition: UIWidget.hpp:141
bool operator!=(const ID &other) const
Definition: UIWidget.hpp:144
void SetCustomSize(Size2D size)
virtual void OnChildFocusRequested(std::shared_ptr< UIWidget >)
Definition: UIWidget.hpp:208
void SetMinimalSize(Size2D)
bool operator==(const ID &other) const
Definition: UIWidget.hpp:143
std::shared_ptr< UIWidget > FindChild(ID search_id)
Definition: UIWidget.hpp:154
bool IsEmpty() const
Definition: Utilities.hpp:45
std::weak_ptr< Window > window
Definition: UIWidget.hpp:185
void SetBackColor(const Color &c)
Definition: UIWidget.hpp:116
virtual void OnFocusChanged(bool)
Definition: UIWidget.hpp:165
Definition: Utilities.hpp:157
Definition: Utilities.hpp:40
void SetFrontColor(const Color &c)
Definition: UIWidget.hpp:120
bool HasZeroArea() const
Definition: UIWidget.hpp:88