YSTest: YFocus.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

YFocus.cpp
浏览该文件的文档.
1 /*
2 © 2010-2016 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_YControl
30
31 namespace YSLib
32 {
33
34 namespace UI
35 {
36
37 bool
38 IsFocused(const IWidget& wgt)
39 {
40 const auto p(FetchContainerPtr(wgt));
41
42 return p ? FetchFocusingPtr(*p).get() == &wgt : false;
43 }
44
45 bool
46 DoRequestFocus(IWidget& wgt, bool release_event)
47 {
48 if(const auto p = FetchContainerPtr(wgt))
49 {
50 auto& p_focusing(p->GetView().FocusingPtr);
51
52 if(p_focusing.get() != &wgt)
53 {
54 if(p_focusing)
55 {
56 YAssert(IsFocused(*p_focusing),
57 "Invalid focusing state found.");
58 if(release_event)
59 ReleaseFocusFrom(*p_focusing, wgt);
60 }
61 p_focusing = make_observer(&wgt);
62 return true;
63 }
64 }
65 return {};
66 }
67
68 bool
69 DoReleaseFocus(IWidget& wgt)
70 {
71 if(const auto p = FetchContainerPtr(wgt))
72 {
73 auto& p_focusing(p->GetView().FocusingPtr);
74
75 if(p_focusing.get() == &wgt)
76 {
77 p_focusing = {};
78 return true;
79 }
80 }
81 return {};
82 }
83
84 void
85 RequestFocusFrom(IWidget& dst, IWidget& src)
86 {
87 if(DoRequestFocus(dst, true))
88 CallEvent<GotFocus>(dst, UIEventArgs(src));
89 for(auto p = FetchFocusingPtr(dst); p; p = FetchFocusingPtr(*p))
90 CallEvent<GotFocus>(*p, RoutedEventArgs(src));
91 }
92
93 void
94 ReleaseFocusFrom(IWidget& dst, IWidget& src)
95 {
96 auto p(make_observer(&dst));
97
98 while(const auto p_foc = FetchFocusingPtr(*p))
99 p = p_foc;
100 for(; p.get() != &dst; p = FetchContainerPtr(*p))
101 {
102 YAssert(p, "Wrong child focus state found.");
103 if(DoReleaseFocus(*p))
104 CallEvent<LostFocus>(*p, RoutedEventArgs(src));
105 }
106 if(DoReleaseFocus(dst))
107 CallEvent<LostFocus>(dst, UIEventArgs(src));
108 }
109
110 void
111 ClearFocusingOf(IWidget& wgt)
112 {
113 if(const auto p = FetchFocusingPtr(wgt))
114 {
115 wgt.GetView().FocusingPtr = {};
116 CallEvent<LostFocus>(*p, UIEventArgs(wgt));
117 }
118 }
119
120 bool
121 IsFocusedCascade(const IWidget& wgt, observer_ptr<const IWidget> p_top)
122 {
123 auto p_wgt(make_observer(&wgt));
124
125 while(const auto p_con = FetchContainerPtr(*p_wgt))
126 {
127 if(p_con == p_top)
128 break;
129 if(FetchFocusingPtr(*p_con) != p_wgt)
130 return {};
131 p_wgt = p_con;
132 }
133 return true;
134 }
135
136 void
137 RequestFocusCascade(IWidget& wgt)
138 {
139 for(auto p(&wgt); p; p = FetchContainerPtr(*p).get())
140 RequestFocus(*p);
141 }
142
143 void
144 ReleaseFocusCascade(IWidget& wgt)
145 {
146 for(auto p(&wgt); p; p = FetchContainerPtr(*p).get())
147 ReleaseFocus(*p);
148 }
149
150 } // namespace UI;
151
152 } // namespace YSLib;
153
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:95
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
UI
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:326
YSLib::UI::IsFocused
YF_API bool IsFocused(const IWidget &)
判断部件是否取得焦点。
Definition: YFocus.cpp:38
YSLib::UI::DoReleaseFocus
YF_API bool DoReleaseFocus(IWidget &)
释放焦点：验证并清空容器部件的焦点指针。
Definition: YFocus.cpp:69
YSLib::UI::ClearFocusingOf
wgt wgt YF_API void ClearFocusingOf(IWidget &)
清除焦点指针并以此部件作为事件源调用被清除焦点部件的 LostFocus 事件。
Definition: YFocus.cpp:111
YSLib::UI::wgt
IWidget & wgt
Definition: YControl.h:112
YSLib::UI::RequestFocusFrom
YF_API void RequestFocusFrom(IWidget &dst, IWidget &src)
向部件容器请求获得焦点，并指定 GotFocus 事件发送控件。
Definition: YFocus.cpp:85
YSLib::UI::ReleaseFocusCascade
YF_API void ReleaseFocusCascade(IWidget &)
级联释放部件及上层容器焦点。
Definition: YFocus.cpp:144
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
YSLib::UI::ReleaseFocusFrom
YF_API void ReleaseFocusFrom(IWidget &dst, IWidget &src)
释放焦点，并指定 LostFocus 事件发送控件。
Definition: YFocus.cpp:94
YSLib::UI::RequestFocusCascade
YF_API void RequestFocusCascade(IWidget &)
级联请求部件及上层容器焦点。
Definition: YFocus.cpp:137
YSLib::UI::FetchFocusingPtr
pt pt Y FetchFocusingPtr
Definition: YWidget.h:145
YSLib::UI::DoRequestFocus
YF_API bool DoRequestFocus(IWidget &, bool release_event={})
向部件容器请求获得焦点，并指定 GotFocus 事件发送控件。
Definition: YFocus.cpp:46
YModules.h
YSLib::UI::IsFocusedCascade
YF_API bool IsFocusedCascade(const IWidget &, observer_ptr< const IWidget >={})
判断部件相对指针指定的部件是否具有级联焦点。
Definition: YFocus.cpp:121
YSLib::UI::UIEventArgs
用户界面事件参数基类。
Definition: YWidgetEvent.h:59
YSLib::UI::RoutedEventArgs
路由事件参数基类。
Definition: YWidgetEvent.h:98
CHRLib::p
const auto p(d)