YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ywidget.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2009 - 2013.
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/ydesktop.h"
29 #include "YSLib/UI/YBrush.h"
30 #include "YSLib/UI/ystyle.h"
31 
33 
35 
36 bool
37 Contains(const IWidget& wgt, SPos x, SPos y)
38 {
39  return GetBoundsOf(wgt).Contains(x, y);
40 }
41 
42 bool
43 ContainsVisible(const IWidget& wgt, SPos x, SPos y)
44 {
45  return IsVisible(wgt) && Contains(wgt, x, y);
46 }
47 
48 
49 void
50 SetBoundsOf(IWidget& wgt, const Rect& r)
51 {
52  SetLocationOf(wgt, r.GetPoint()),
53  SetSizeOf(wgt, r.GetSize());
54 }
55 
56 void
57 SetInvalidationOf(IWidget& wgt)
58 {
59  wgt.GetRenderer().CommitInvalidation(Rect(GetSizeOf(wgt)));
60 }
61 
62 void
64 {
65  if(const auto pCon = FetchContainerPtr(wgt))
66  pCon->GetRenderer().CommitInvalidation(GetBoundsOf(wgt));
67 }
68 
69 void
70 SetLocationOf(IWidget& wgt, const Point& pt)
71 {
72  wgt.GetView().SetLocation(pt);
73  CallEvent<Move>(wgt, UIEventArgs(wgt));
74 }
75 
76 void
77 SetSizeOf(IWidget& wgt, const Size& s)
78 {
79  wgt.GetRenderer().SetSize(s);
80  wgt.GetView().SetSize(s);
81  CallEvent<Resize>(wgt, UIEventArgs(wgt));
82 }
83 
84 
85 void
86 Close(IWidget& wgt)
87 {
88  Hide(wgt);
89  if(const auto pCon = FetchContainerPtr(wgt))
90  ClearFocusingOf(*pCon);
91 }
92 
93 void
94 DrawArrow(PaintEventArgs&& e, IWidget& wgt, SDst half_size, Rotation rot,
95  Color c)
96 {
97  Drawing::DrawArrow(e.Target, Rect(e.Location, GetSizeOf(wgt)), half_size,
98  rot, c);
99 }
100 
101 void
102 Hide(IWidget& wgt)
103 {
104  SetVisibleOf(wgt, false);
105  ReleaseFocus(wgt);
106  Invalidate(wgt);
107 }
108 
109 void
110 Invalidate(IWidget& wgt)
111 {
112  Invalidate(wgt, Rect(GetSizeOf(wgt)));
113 }
114 void
115 Invalidate(IWidget& wgt, const Rect& bounds)
116 {
117  auto pWgt(&wgt);
118  Rect r(bounds);
119 
120  do
121  {
122  r = pWgt->GetRenderer().CommitInvalidation(r);
123  r.GetPointRef() += GetLocationOf(*pWgt);
124  }while((pWgt = FetchContainerPtr(*pWgt)));
125 }
126 
127 void
128 PaintChild(IWidget& wgt, PaintEventArgs&& e)
129 {
130  auto& sender(e.GetSender());
131 
132  if(Clip(e.ClipArea, Rect(e.Location += GetLocationOf(sender),
133  GetSizeOf(sender))))
134  wgt.GetRenderer().Paint(sender, std::move(e));
135 }
136 Rect
137 PaintChild(IWidget& wgt, const PaintContext& pc)
138 {
139  PaintEventArgs e(wgt, pc);
140 
141  PaintChild(wgt, std::move(e));
142  return e.ClipArea;
143 }
144 
145 void
146 RequestToTop(IWidget& wgt)
147 {
148  if(auto pFrm = dynamic_cast<Panel*>(FetchContainerPtr(wgt)))
149  pFrm->MoveToTop(wgt);
150 }
151 
152 void
153 Show(IWidget& wgt)
154 {
155  SetVisibleOf(wgt, true);
156  RequestFocus(wgt);
157  Invalidate(wgt);
158 }
159 
160 
161 Widget::Widget(const Rect& r, Color b, Color f)
162  : view_ptr(new View(r)), renderer_ptr(new Renderer()),
163  controller_ptr(new WidgetController(false)),
164  Background(SolidBrush(b)), ForeColor(f)
165 {
166  InitializeEvents();
167 }
168 Widget::Widget(const Widget& wgt)
169  : view_ptr(ClonePolymorphic(wgt.view_ptr)),
170  renderer_ptr(ClonePolymorphic(wgt.renderer_ptr)),
171  controller_ptr(ClonePolymorphic(wgt.controller_ptr)),
172  Background(wgt.Background), ForeColor(wgt.ForeColor)
173 {}
174 Widget::~Widget()
175 {
176  DoReleaseFocus(*this);
177 }
178 
179 void
180 Widget::InitializeEvents()
181 {
182  (FetchEvent<Paint>(*this).Add(std::ref(Background), BackgroundPriority))
183  += std::bind(&Widget::Refresh, this, std::placeholders::_1);
184 }
185 
187 Widget::GetController() const
188 {
189  if(!controller_ptr)
190  throw BadEvent();
191  return *controller_ptr;
192 }
193 
194 void
195 Widget::SetRenderer(unique_ptr<Renderer>&& p)
196 {
197  renderer_ptr = p ? std::move(p) : unique_ptr<Renderer>(new Renderer());
198  renderer_ptr->SetSize(GetSizeOf(*this));
199 }
200 void
201 Widget::SetView(unique_ptr<View>&& p)
202 {
203  view_ptr = p ? std::move(p)
204  : unique_ptr<View>(new View(GetBoundsOf(*this)));
205 }
206 
207 void
208 Widget::Refresh(PaintEventArgs&& e)
209 {
210  if(!e.ClipArea.IsUnstrictlyEmpty())
211  {
212  auto pr(GetChildren());
213 
214  while(pr.first != pr.second)
215  {
216  if(IsVisible(*pr.first))
217  e.ClipArea |= PaintChild(*pr.first, e);
218  ++pr.first;
219  }
220  }
221 }
222 
224 
225 YSL_END
226