YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
TabControl.cpp
浏览该文件的文档.
1 /*
2  © 2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #include "YSLib/UI/YModules.h"
29 #include YFM_YSLib_UI_TabControl
30 #include YFM_YSLib_UI_YGUI
31 #include YFM_YSLib_Service_YGDI // for Drawing::Clip;
32 #include <ystdex/iterator.hpp>
33 
34 namespace YSLib
35 {
36 
37 using namespace Drawing;
38 
39 namespace UI
40 {
41 
44 {
45  FetchEvent<Resize>(*this) += [this]{
46  PerformLayout();
47  };
48 }
49 
50 void
51 TabBar::operator+=(IWidget& tab)
52 {
53  MLinearUIContainer::operator+=(tab);
54  SetContainerPtrOf(tab, this);
55 }
56 
57 bool
58 TabBar::operator-=(IWidget& tab)
59 {
60  return RemoveFrom(tab, *this) ? MLinearUIContainer::operator-=(tab) : false;
61 }
62 
63 void
65 {
66  auto& sender(e.GetSender());
67 
68  DrawRect(e.Target, e.ClipArea, Rect(e.Location, GetSizeOf(sender)),
69  FetchGUIState().Colors[IsFocused(sender)
71 }
72 
73 SPos
75 {
76  const SDst h(GetHeight());
77  SPos x(Offset);
78 
79  for(const auto& p_wgt : vWidgets)
80  {
81  YAssertNonnull(p_wgt);
82 
83  auto& wgt(*p_wgt);
84  Size s(GetSizeOf(wgt));
85 
86  if(s.Width < MinWidth)
87  s.Width = MinWidth;
88  if(h < s.Height)
89  s.Height = h;
90  SetBoundsOf(wgt, Rect(x, 0, s));
91  x += s.Width;
92  }
93  return x;
94 }
95 
96 void
98 {
99  if(!e.ClipArea.IsUnstrictlyEmpty())
100  for(auto pr(GetChildren()); pr.first != pr.second; ++pr.first)
101  {
102  auto& wgt(*pr.first);
103 
104  if(IsVisible(wgt))
105  {
106  PaintContext& pc(e);
107  PaintEventArgs e(wgt, pc);
108 
109  if(Clip(e.ClipArea, Rect(e.Location += GetLocationOf(wgt),
110  GetSizeOf(wgt))))
111  {
113  PaintTabBackground(std::move(e));
114  wgt.GetRenderer().Paint(wgt, std::move(e));
116  PaintTabForeground(std::move(e));
117  }
118  pc.ClipArea |= e.ClipArea;
119  }
120  }
121 }
122 
123 bool
125 {
126  return IsEnabled(ctl);
127 }
128 
129 
131  : Control(r),
132  p_bar(new TabBar(Size(r.Width, BarHeight)))
133 {}
134 
135 void
136 TabControl::operator+=(IWidget& wgt)
137 {
138  tab_pages.push_back(&dynamic_cast<Panel&>(wgt));
139  SetContainerPtrOf(wgt, this);
140  SetVisibleOf(wgt, {});
141 }
142 
143 void
145 {
146  AddWidgetHandlerAdaptor<TouchDown>(ctl, *this, [&]{
147  if(SwitchTab(ctl))
148  Invalidate(*this);
149  });
150 }
151 
152 Rect
153 TabControl::GetPageBounds() const ynothrow
154 {
155  const SDst h(GetHeight());
156  const SDst h_tb(max<SDst>(GetTabBarRef().GetHeight(), BarHeight));
157 
158  return Rect(0, h_tb, GetWidth(), h < h_tb ? 0 : h - h_tb);
159 }
160 
161 size_t
162 TabControl::Find(IWidget& wgt) const
163 {
164  return std::find(tab_pages.cbegin(), tab_pages.cend(), &wgt)
165  - tab_pages.cbegin();
166 }
167 
168 bool
170 {
171  const auto idx(GetTabBarRef().Find(ctl));
172 
173  return idx < GetTabCount() ? SwitchPage(idx) : false;
174 }
175 
176 bool
178 {
179  if(idx < tab_pages.size())
180  {
181  if(const auto p_page = tab_pages[idx])
182  return SwitchPage(*p_page);
183  }
184  return {};
185 }
186 bool
188 {
189  YAssert(Find(ctl) != tab_pages.size(), "Invalid page found."),
191  if(p_bar->SwitchPage(ctl))
192  {
193  if(p_page)
194  SetVisibleOf(*p_page, {});
195 
196  const auto i(Find(ctl));
197 
198  p_page = tab_pages[i];
199  SetBoundsOf(*p_page, GetPageBounds()),
200  SetVisibleOf(*p_page, true);
201  return true;
202  }
203  return {};
204 }
205 
206 void
208 {
209  auto& tb(GetTabBarRef());
210 
211  tb.PerformLayout();
212 
213  auto pr(tb.GetChildren());
214  const auto tp_count(tab_pages.size());
215  size_t i(0);
216 
217  for(auto pr(tb.GetChildren()); !p_page && i < tp_count
218  && pr.first != pr.second; yunseq(++pr.first, ++i))
219  if(tab_pages[i])
220  {
221  p_page = tab_pages[i];
222  SetVisibleOf(*p_page, true),
223  SetBoundsOf(*p_page, GetPageBounds());
224  }
225  Invalidate(*this);
226 }
227 
228 } // namespace UI;
229 
230 } // namespace YSLib;
231 
const vector< Panel * > tab_pages void Attach(Control &)
附加:添加 TouchDown 事件处理器。
Definition: TabControl.cpp:144
bool Clip(Rect &x, const Rect &y)
剪切操作:取标准矩形交集并判断是否严格非空。
Definition: ygdi.h:134
pt pt Y const IWidget &wgt const IWidget &wgt GetSizeOf
无效化:使相对于部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.h:156
pt pt Y const IWidget &wgt GetLocationOf
Definition: ywidget.h:148
unique_ptr< TabBar > p_bar
标签栏。
Definition: TabControl.h:125
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Definition: TabControl.cpp:97
GetTabBarRef().GetCount()) DefGetter(const ynothrow
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
bool SwitchTab(Control &)
按标签切换页面。
Definition: TabControl.cpp:169
virtual bool operator-=(IWidget &)
Definition: TabControl.cpp:58
void SetBoundsOf(IWidget &wgt, const Rect &r)
Definition: ywidget.cpp:53
YF_API void Invalidate(IWidget &, const Rect &)
无效化:使相对于部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.cpp:111
static void DefaultPaintTabBorder(PaintEventArgs &&)
绘制默认标签页边框。
Definition: TabControl.cpp:64
void UpdateTabPages()
更新标签页状态。
Definition: TabControl.cpp:207
SDst BarHeight
标签栏初始高。
Definition: TabControl.h:118
部件绘制参数。
Definition: ywgtevt.h:276
std::function< void(PaintEventArgs &&)> PaintTabForeground
标签前景附加绘制操作。
Definition: TabControl.h:64
SDst Height
宽和高。
Definition: ygdibase.h:258
不活动边框背景。
Definition: ystyle.h:193
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
控件。
Definition: ycontrol.h:275
vector< Panel * > tab_pages
Definition: TabControl.h:127
YF_API bool RemoveFrom(IWidget &, IWidget &)
从容器中移除部件。
Definition: yuicont.cpp:144
SPos Offset
首个标签所在的起始偏移位置。
Definition: TabControl.h:51
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
YF_API bool IsFocused(const IWidget &)
判断部件是否取得焦点。
Definition: yfocus.cpp:38
TabControl(const Rect &={})
Definition: TabControl.cpp:130
_tWidget & wgt
Definition: ywgtevt.h:596
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
SPos PerformLayout()
布局。
Definition: TabControl.cpp:74
bool operator-=(IWidget &)
从部件组移除部件。
Definition: yuicont.cpp:165
通用迭代器。
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
const IWidget &wgt SPos
Definition: ywidget.h:104
#define YAssertNonnull(_expr)
Definition: cassert.h:81
线性部件容器模块。
Definition: yuicont.h:176
bool IsEnabled(const IWidget &wgt)
判断部件是否为可用的控件。
Definition: ycontrol.h:86
bool SwitchPage(size_t)
切换页面。
Definition: TabControl.cpp:177
TabBar(const Rect &={})
Definition: TabControl.cpp:42
活动边框背景。
Definition: ystyle.h:192
标签栏控件。
Definition: TabControl.h:45
bounds & r
Definition: ydraw.h:220
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
屏幕区域大小。
Definition: ygdibase.h:249
virtual void operator+=(IWidget &)
Definition: TabControl.cpp:51
size_t Find(IWidget &) const
查找页面部件。
Definition: TabControl.cpp:162
SDst MinWidth
每个标签最小的位置。
Definition: TabControl.h:53
bool SwitchPage(Control &)
切换页面。
Definition: TabControl.cpp:124
p1 p1 p2 p2 c YF_API void DrawRect(const Graphics &g, const Rect &bounds, const Point &pt, const Size &s, Color c)
描画标准矩形。
Definition: ydraw.cpp:131
AController *controller_ptr Renderer *renderer_ptr View *view_ptr SPos
Definition: ywidget.h:442
std::function< void(PaintEventArgs &&)> PaintTabBackground
标签背景附加绘制操作。
Definition: TabControl.h:58
#define YAssert(_expr, _msg)
Definition: cassert.h:73