libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Window.hpp
Go to the documentation of this file.
1 #ifndef WINDOW_HPP
2 #define WINDOW_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 <SDL2/SDL.h>
23 #include "SDLHandle.hpp"
24 #include "Signal.hpp"
25 #include "Theme.hpp"
26 #include "LateReturn.hpp"
27 #include "Alertable.hpp"
28 
29 struct SDL_Window;
30 struct SDL_Renderer;
31 
32 namespace AlgAudio{
33 
34 class UIWidget;
35 
37 class Window : public virtual SubscriptionsManager, public IAlertable, public std::enable_shared_from_this<Window>{
38 private:
39  SDLHandle h;
40 public:
48  static std::shared_ptr<Window> Create(std::string title = "AlgAudio", int w = 350, int h = 300, bool centered = true, bool resizable = true);
49  ~Window();
50 
52  Window(const Window&) = delete;
53  Window& operator=(const Window&) = delete;
54 
57  void Render();
58 
61  void Insert(std::shared_ptr<UIWidget> child);
62 
64  void SetNeedsRedrawing();
65 
66  virtual void ProcessCloseEvent();
67  void ProcessMouseButtonEvent(bool down, MouseButton button, Point2D);
68  void ProcessWheelEvent(MouseButton button);
70  void ProcessEnterEvent();
71  void ProcessLeaveEvent();
72  void ProcessResizeEvent();
73  virtual void ProcessKeyboardEvent(KeyData data);
74 
75  template<class W, typename... Args>
76  std::shared_ptr<W> Create(Args... args){
77  return W::Create(shared_from_this(), args...);
78  }
79 
81 
83  unsigned int GetID() const {return id;}
85  Size2D GetSize() const;
87  SDL_Renderer* GetRenderer() const {return renderer;}
89  SDL_Window* GetWindow() const {return window;}
90 
91  // Empty implementation.
92  virtual LateReturn<int> ShowSimpleAlert( std::string,std::string,std::string,AlertType,Color,Color)
93  {throw Exceptions::WindowNotAlertable("This window is not capable of displaying alerts.");}
94  virtual LateReturn<> ShowErrorAlert(std::string, std::string)
95  {throw Exceptions::WindowNotAlertable("This window is not capable of displaying alerts.");}
96 
98  std::shared_ptr<UIWidget> GetRoot() const {return child;}
99 protected:
100  Window(std::string title, int w, int h, bool centered = true, bool resizable = true);
101 private:
102  std::string title;
103  int width;
104  int height;
105  unsigned int id;
106  SDL_Window* window;
107  SDL_Renderer* renderer;
108  // Note: SDL_GLContext is defined as a typedef for void*, so it's a pointer type.
109  SDL_GLContext context;
110 
111  Point2D prev_motion = Point2D(0,0);
112  bool mouse_just_entered = false;
113  Point2D last_mouse_pos = Point2D(0,0);
114 
115  bool needs_redrawing = true;
116 
117  std::shared_ptr<UIWidget> child;
118 };
119 
120 } //namespace AlgAudio
121 #endif // WINDOW_HPP
Definition: Color.hpp:28
static std::shared_ptr< Window > Create(std::string title="AlgAudio", int w=350, int h=300, bool centered=true, bool resizable=true)
virtual LateReturn< int > ShowSimpleAlert(std::string, std::string, std::string, AlertType, Color, Color)
Definition: Window.hpp:92
unsigned int GetID() const
Definition: Window.hpp:83
void ProcessMouseButtonEvent(bool down, MouseButton button, Point2D)
Window(const Window &)=delete
Window & operator=(const Window &)=delete
void ProcessMotionEvent(Point2D)
Definition: SDLHandle.hpp:40
Definition: Alertable.hpp:58
Point2D_< int > Point2D
Definition: Utilities.hpp:35
Signal on_close
Definition: Window.hpp:80
std::shared_ptr< W > Create(Args...args)
Definition: Window.hpp:76
Size2D GetSize() const
Definition: Signal.hpp:247
void ProcessEnterEvent()
MouseButton
Definition: Utilities.hpp:190
void ProcessLeaveEvent()
Definition: Window.hpp:37
SDL_Window * GetWindow() const
Definition: Window.hpp:89
virtual void ProcessCloseEvent()
Definition: Alertable.hpp:50
void SetNeedsRedrawing()
void ProcessWheelEvent(MouseButton button)
SDL_Renderer * GetRenderer() const
Definition: Window.hpp:87
Definition: Alertable.hpp:26
void * SDL_GLContext
Definition: DrawContext.hpp:31
void ProcessResizeEvent()
Definition: Signal.hpp:145
virtual LateReturn ShowErrorAlert(std::string, std::string)
Definition: Window.hpp:94
void Insert(std::shared_ptr< UIWidget > child)
Definition: LateReturn.hpp:35
AlertType
Definition: Alertable.hpp:42
Definition: Utilities.hpp:157
Definition: Utilities.hpp:40
std::shared_ptr< UIWidget > GetRoot() const
Definition: Window.hpp:98
virtual void ProcessKeyboardEvent(KeyData data)