AWind
Window.h
1 /*
2  AWind.h - Arduino window library support for Color TFT LCD Boards
3  Copyright (C)2015 Andrei Degtiarev. All right reserved
4 
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/AWind
8 
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the CC BY-NC-SA 3.0 license.
12  Please see the included documents for further information.
13 
14  Commercial use of this library requires you to buy a license that
15  will allow commercial use. This includes using the library,
16  modified or not, as a tool to sell products.
17 
18  The license applies to all part of the library including the
19  examples and tools supplied with the library.
20 */
21 #pragma once
22 
23 #include "Color.h"
24 
25 #include "DC.h"
26 #include "ITouchEventReceiver.h"
27 #include "Decorator.h"
28 #include "IDialogClosedEventReceiver.h"
29 #include "Environment.h"
30 
31 class Dialog;
32 
34 class Window
35 {
36 protected:
37  int _left;
38  int _top;
39  int _width;
40  int _height;
41  bool _isVisible;
42  LinkedList<Window> _children;
44 //#ifdef DEBUG_AWIND
45  //const __FlashStringHelper *_name; //!< internal window name that helps by debugging. For some reason preprocessor definiton #ifdef DEBUG_AWIND does not work here. So if you define debug you dhould uncomment this line
46 //#endif
47  bool _isDirty;
48  DecoratorList *_decorators;
50 
51 public:
53 
60  Window(const __FlashStringHelper * name,int left,int top,int width,int height):
61  _left(left),
62  _top(top),
63  _width(width),
64  _height(height),
65  _isVisible(true)
66 #ifdef DEBUG_AWIND
67  ,_name(name)
68 #endif
69  {
70  _parent = NULL;
71  _isDirty=true;
72  _decorators=NULL;
73  _touchEventReceiver=NULL;
74  }
76  virtual void SetDecorators(DecoratorList *decorators)
77  {
78  _decorators=decorators;
79  }
81  DecoratorList * GetDecorators()
82  {
83  return _decorators;
84  }
86  void AddDecorator(Decorator *decorator)
87  {
88  if(_decorators == NULL)
89  _decorators=new DecoratorList();
90  _decorators->Add(decorator);
91  }
92  Dialog *FindDialog(const __FlashStringHelper *id);
93  void RegisterDialog(const __FlashStringHelper *id,Dialog *dlg);
94  IDialogClosedEventReceiver::DialogResults DoDialog(Dialog *dlg);
95 
96 
98 
103  {
104  _touchEventReceiver=touchEventReceiver;
105  }
107  virtual bool IsAwaitTouch()
108  {
109  return _touchEventReceiver!=NULL;
110  }
112  virtual void OnTouching(DC *dc)
113  {
114  //out<<"OnTouching"<<endln;
115  PrepareDC(dc);
116  dc->SetColor(Color::Red);
117  dc->DrawRoundRect(0,0,Width(),Height());
118  dc->DrawRoundRect(1,1,Width()-1,Height()-1);
119  }
121  virtual bool OnTouch(int x,int y)
122  {
123  //out<<F("OnTouch")<<endln;
124  if(_touchEventReceiver!=NULL)
125  {
126  //out<<F("TouchEvent generated")<<endln;
127  _touchEventReceiver->NotifyTouch(this);
128  return true;
129  }
130  return false;
131  }
134  {
135  Window *parent=this;
136  while(parent->Parent()!=NULL)
137  {
138  parent=parent->Parent();
139  }
140  return parent;
141  }
143 
149  virtual void Move(int left,int top,int width,int height)
150  {
151  _left = left;
152  _top = top;
153  _width = width;
154  _height = height;
155  }
157  void Invalidate()
158  {
159  _isDirty=true;
160  }
162  bool IsDirty()
163  {
164  return _isDirty;
165  }
166 #ifdef DEBUG_AWIND
167  const __FlashStringHelper *Name()
169  {
170  return _name;
171  }
172 #endif
173  int Left()
175  {
176  return _left;
177  }
179  int Top()
180  {
181  return _top;
182  }
184  int Width()
185  {
186  return _width;
187  }
189  int Height()
190  {
191  return _height;
192  }
194  void AddChild(Window * window)
195  {
196  window->SetParent(this);
197  _children.Add(window);
198  }
201  {
202  return _parent;
203  }
205  void SetVisible(bool isVisible)
206  {
207  _isVisible=isVisible;
208 
209  }
211  bool IsVisible()
212  {
213  bool retCode = _isVisible;
214  Window * crWnd = this;
215  while (retCode && crWnd->Parent() != NULL)
216  {
217  crWnd = crWnd->Parent();
218  retCode = crWnd->_isVisible;
219  }
220  return retCode;
221  }
223  LinkedList<Window> & Children()
224  {
225  return _children;
226  }
228 
231  void Redraw(DC *dc)
232  {
233  PrepareDC(dc);
234  _isDirty=false;
235  if(_decorators!=NULL)
236  {
237  for(int i=0;i<_decorators->Count();i++)
238  {
239  (*_decorators)[i]->Draw(dc,0,0,_width,_height);
240  }
241  }
242  OnDraw(dc);
243  }
244 protected:
246 
249  void PrepareDC(DC *dc)
250  {
251  dc->Reset();
252  Window *crWnd=this;
253  while(crWnd!=NULL)
254  {
255  dc->Offset(crWnd->Left(),crWnd->Top());
256  crWnd=crWnd->Parent();
257  }
258  }
260 
263  virtual void OnDraw(DC *dc)
264  {
265  }
266 private:
268  void SetParent(Window *window)
269  {
270  _parent = window;
271  }
272 };
bool IsDirty()
Returns true if window has to be updated.
Definition: Window.h:162
void RegisterTouchEventReceiver(ITouchEventReceiver *touchEventReceiver)
Registers receiver for touch event.
Definition: Window.h:102
DecoratorList * GetDecorators()
Returns window decorators list.
Definition: Window.h:81
Base class for all window objects. Provides basic window functionality.
Definition: Window.h:34
virtual bool IsAwaitTouch()
Returns true if window await touch action (like button) or false if touch manager should ignore this ...
Definition: Window.h:107
int Left()
Returns window left coordinate relative to the parent window.
Definition: Window.h:174
void Redraw(DC *dc)
Performs full window redraw.
Definition: Window.h:231
Window * Parent()
Returns Parent window.
Definition: Window.h:200
DecoratorList * _decorators
contains list of drawig commands. If they are shared between more than one window -> SRAM usage optim...
Definition: Window.h:48
Window * RootWindow()
Returns pointer to root window. MainWindow does not have any parents.
Definition: Window.h:133
ITouchEventReceiver * _touchEventReceiver
call back event receiver for touch actions
Definition: Window.h:49
virtual bool OnTouch(int x, int y)
Touch manager calls this function right after touch is released.
Definition: Window.h:121
virtual void NotifyTouch(Window *window)=0
Has to be implemented in target class.
int _height
window height
Definition: Window.h:40
virtual void Move(int left, int top, int width, int height)
Moves and resizes window relativly to the parent window.
Definition: Window.h:149
Window * _parent
pointer to parent window
Definition: Window.h:43
LinkedList< Window > _children
list of children windows. All children window are positioned relative to parent window ...
Definition: Window.h:42
void PrepareDC(DC *dc)
Setups window coordinate system. This function called by window manager right before window has to be...
Definition: Window.h:249
LinkedList< Window > & Children()
Returns list of children window.
Definition: Window.h:223
Base class for dialog objects. See Dialogs example Provides basic window functionality.
Definition: Dialog.h:25
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
int Height()
Returns window height.
Definition: Window.h:189
void Invalidate()
If function is called than the window manager updates the window.
Definition: Window.h:157
Base class for window decorators. This concept allows sharing of drawing setting among more than one ...
Definition: Decorator.h:24
int _top
window top coordinate relative to the parent window
Definition: Window.h:38
Window(const __FlashStringHelper *name, int left, int top, int width, int height)
Constructor.
Definition: Window.h:60
void Offset(int offset_x, int offset_y)
Initializes drawing coordinate system offset.
Definition: DC.h:84
bool _isVisible
if this variable is false this window and all child windows are not visualized
Definition: Window.h:41
virtual void OnDraw(DC *dc)
If derived class needs to draw something in window client area, this function has to be ovverriden...
Definition: Window.h:263
bool IsVisible()
Returns true if window visible and false is hidden.
Definition: Window.h:211
void AddChild(Window *window)
Adds window child window.
Definition: Window.h:194
void Reset()
Resets device context into initial condition.
Definition: DC.h:72
void SetVisible(bool isVisible)
Sets window visibility status.
Definition: Window.h:205
void AddDecorator(Decorator *decorator)
Adds decorator to the decaorator list.
Definition: Window.h:86
int Top()
Returns window top coordinate relative to the parent window.
Definition: Window.h:179
virtual void SetDecorators(DecoratorList *decorators)
Sets window decorators list.
Definition: Window.h:76
int _width
window width
Definition: Window.h:39
bool _isDirty
if true than window manager will redraw this window.
Definition: Window.h:47
void DrawRoundRect(int left, int top, int right, int bottom)
Draws rounded rectangle. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:156
Interface that provides screen touch notifications. If you want receive this notification in the targ...
Definition: ITouchEventReceiver.h:24
virtual void OnTouching(DC *dc)
Touch manager calls this function in the loop as long as touch action proceeds.
Definition: Window.h:112
int Width()
Returns window width.
Definition: Window.h:184
int _left
window left coordinate relative to the parent window
Definition: Window.h:37