AWind
TabControl.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 #include "MainWindow.h"
23 #include "Button.h"
24 
26 class TabControl : public Window, public ITouchEventReceiver
27 {
28  LinkedList<Button> _listButtons; //list of buttons on the left scrren side
29  LinkedList<Window> _listWindow; //list of depended windows (
30  static const int _szy = 30;
31 public:
33 
39  TabControl(const __FlashStringHelper * name, int left, int top, int width, int height):Window(name, left, top, width, height)
40  {
41  SetDecorators(Environment::Get()->FindDecorators(F("TabControl")));
42  AddChild(new Window(F("Back tab buttons"), 0, 0, width, _szy));
43  Children()[0]->SetDecorators(Environment::Get()->FindDecorators(F("BackTabButtons")));
44  }
46  void AddTab(const __FlashStringHelper *buttonName,Window *window)
47  {
48  int wnd_width=Width();
49  int wnd_height=Height();
50  int x=0;
51  int y=0;
52  Button * button=new Button(0,0,0,0,buttonName);
53  button->SetMargins(5,7);
54  button->RegisterTouchEventReceiver(this);
55  Children()[0]->AddChild(button);
56  AddChild(window);
57  window->Move(1, _szy+1, wnd_width-2,wnd_height - _szy-2);
58  _listButtons.Add(button);
59  _listWindow.Add(window);
60  if(_listWindow.Count()>1)
61  window->SetVisible(false);
62  else
63  _listButtons[0]->SetDecorators(Environment::Get()->FindDecorators(F("ActiveTabButton")));
64  UpdateTabs(0);
65  }
67  void UpdateTabs(int sel_index)
68  {
69  int szx = Width() / _listButtons.Count();
70 
71  for (int i = 0;i < _listButtons.Count();i++)
72  {
73  int offset_y = i == sel_index ? 0 : 3;
74  _listButtons[i]->Move(0 + szx*i, offset_y, szx - 1, _szy- offset_y);
75  }
76  Children()[0]->Invalidate();
77  }
79  void NotifyTouch(Window *window)
80  {
81  int sel_index=-1;
82  for(int i=0;i<_listButtons.Count();i++)
83  {
84  if(window == _listButtons[i])
85  {
86  sel_index=i;
87  break;
88  }
89  }
90  if(sel_index >=0)
91  {
92  UpdateTabs(sel_index);
93  DecoratorList *buttonDeco=Environment::Get()->FindDecorators(F("Button"));
94  for(int i=0;i<_listButtons.Count();i++)
95  {
96  _listWindow[i]->SetVisible(i==sel_index);
97  if(i==sel_index)
98  {
99  _listWindow[i]->Invalidate();
100  _listButtons[i]->SetDecorators(Environment::Get()->FindDecorators(F("ActiveTabButton")));
101  }
102  else
103  {
104  if(_listButtons[i]->GetDecorators()!=buttonDeco)
105  {
106  _listButtons[i]->SetDecorators(buttonDeco);
107  //_listButtons[i]->Invalidate();
108  }
109  }
110  }
111  }
112  //out<<F("Window selected: ")<<sel_index<<endl;
113  }
114 };
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
static Environment * Get()
Returns singltone instance of environment.
Definition: Environment.h:44
void NotifyTouch(Window *window)
Events routing for gui interaction (see RegisterTouchEventReceiver and public ITouchEventReceiver dec...
Definition: TabControl.h:79
TabControl(const __FlashStringHelper *name, int left, int top, int width, int height)
Constructor.
Definition: TabControl.h:39
virtual void Move(int left, int top, int width, int height)
Moves and resizes window relativly to the parent window.
Definition: Window.h:149
void UpdateTabs(int sel_index)
If new tab is selected, the size of buttons and their appearance are adjusted.
Definition: TabControl.h:67
LinkedList< Window > & Children()
Returns list of children window.
Definition: Window.h:223
int Height()
Returns window height.
Definition: Window.h:189
Window(const __FlashStringHelper *name, int left, int top, int width, int height)
Constructor.
Definition: Window.h:60
TabControl. Control element wich allows intersactive switch between chidl windows.
Definition: TabControl.h:26
void AddChild(Window *window)
Adds window child window.
Definition: Window.h:194
void SetVisible(bool isVisible)
Sets window visibility status.
Definition: Window.h:205
virtual void SetDecorators(DecoratorList *decorators)
Sets window decorators list.
Definition: Window.h:76
Implement button control.
Definition: Button.h:24
DecoratorList * FindDecorators(const __FlashStringHelper *id)
Finds registered decorators by the name.
Definition: Environment.h:59
Interface that provides screen touch notifications. If you want receive this notification in the targ...
Definition: ITouchEventReceiver.h:24
int Width()
Returns window width.
Definition: Window.h:184
void AddTab(const __FlashStringHelper *buttonName, Window *window)
Adds pair: button + corresponding window. The size of added window is adjusted automatically.
Definition: TabControl.h:46
void SetMargins(int offset_x, int offset_y)
Defines offset from left and top for text.
Definition: TextBox.h:58