YSTest: BookmarkUI.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

BookmarkUI.cpp
浏览该文件的文档.
1 /*
2 © 2013-2015 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 "BookmarkUI.h"
29 #include "ShlReader.h"
30
31 namespace YReader
32 {
33
34 namespace
35 {
36
37 using namespace std;
38
40 string
41 ConvertToUIString(Bookmark::PositionType pos, ShlTextReader& shl)
42 {
43 auto line(shl.GetSlice(pos, 48U));
44
45 if(line.size() > 1)
46 {
47 const bool b(line[0] == '\n');
48 const auto i(line.find("\r\n", b));
49
50 if(i != string::npos)
51 line.erase(i);
52 if(b)
53 // XXX: Use cbegin after libstdc++ bug resolved.
54 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60278 .
55 line.erase(line.begin());
56 }
57 return to_string(pos) + " " + std::move(line);
58 }
60 vector<String>
61 ConvertToUIString(const BookmarkList& lst, ShlTextReader& shl)
62 {
63 vector<String> vec;
64
65 vec.reserve(lst.size());
66 for(const auto pos : lst)
67 vec.push_back(ConvertToUIString(pos, shl));
68 return vec;
69 }
70
71 BookmarkList
72 ConvertToBookmarkList(const vector<String>& lst)
73 {
74 BookmarkList bookmarks;
75
76 bookmarks.reserve(lst.size());
77 for(const auto& str : lst)
78 bookmarks.push_back(stoul(str.GetMBCS()));
79 return bookmarks;
80 }
81
82 } // unnamed namespace;
83
84 BookmarkPanel::BookmarkPanel(const BookmarkList& bm, ShlTextReader& shl)
85 : DialogPanel(Size(MainScreenWidth, MainScreenHeight)),
86 // XXX: Conversion to 'SPos' might be implementation-defined.
87 lbPosition({8, 32, 240, 128}),
88 btnAdd(Rect(SPos(GetWidth()) - 80, 4, 16, 16), 210),
89 btnRemove(Rect(SPos(GetWidth()) - 60, 4, 16, 16), 210), shell(shl),
90 bookmarks(bm)
91 {
92 const auto stop_routing_after_direct([](KeyEventArgs&& e) ynothrow{
93 if(e.Strategy == RoutedEventArgs::Bubble)
94 e.Handled = true;
95 });
96
97 AddWidgets(*this, lbPosition, btnAdd, btnRemove),
98 yunseq(
99 btnAdd.Text = u"+",
100 btnRemove.Text = u"-",
101 FetchEvent<KeyDown>(lbPosition) += stop_routing_after_direct,
102 FetchEvent<KeyHeld>(lbPosition) += stop_routing_after_direct,
103 FetchEvent<Click>(btnOK) += [this]{
104 bookmarks = ConvertToBookmarkList(lbPosition.GetList());
105 },
106 FetchEvent<Click>(btnAdd) += [this]{
107 auto& lst(lbPosition.GetListRef());
108 auto idx(GetSelected());
109
110 if(idx < 0)
111 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
112 idx = ptrdiff_t(lst.size());
113 lst.insert(lst.begin() + idx,
114 String(ConvertToUIString(shell.get().GetReaderPosition(), shell)));
115 lbPosition.AdjustViewForContent();
116 lbPosition.UpdateView();
117 },
118 FetchEvent<Click>(btnRemove) += [this]{
119 auto& lst(lbPosition.GetListRef());
120 const auto idx(GetSelected());
121
122 if(idx >= 0)
123 {
124 lst.erase(lst.cbegin() + idx);
125 lbPosition.AdjustViewForContent();
126 lbPosition.UpdateView();
127 }
128 }
129 );
130 }
131 ImplDeDtor(BookmarkPanel)
132
133 BookmarkList::difference_type
134 BookmarkPanel::GetSelected() const
135 {
136 return lbPosition.IsSelected()
137 ? BookmarkList::difference_type(lbPosition.GetSelectedIndex()) : -1;
138 }
139
140 void
141 BookmarkPanel::LoadBookmarks()
142 {
143 lbPosition.GetListRef() = ConvertToUIString(bookmarks, shell);
144 }
145
146 } // namespace YReader;
147
YReader::Bookmark::PositionType
size_t PositionType
文件位置类型。
Definition: ReadingList.h:49
YReader::BookmarkPanel
书签管理面板。
Definition: BookmarkUI.h:44
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
YSLib::UI::DialogPanel
对话面板。
Definition: UIContainerEx.h:78
YReader
Definition: About.h:33
YReader::ShlTextReader
文本阅读器 Shell 。
Definition: ShlReader.h:226
YReader::BookmarkPanel::LoadBookmarks
void LoadBookmarks()
YReader::BookmarkPanel::lbPosition
ListBox lbPosition
Definition: BookmarkUI.h:50
YSLib::UI::AddWidgets
void AddWidgets(_tCon &con, _tWidgets &...wgts)
向部件容器添加子部件。
Definition: YUIContainer.h:443
YSLib::UI::RoutedEventArgs::Bubble
气泡事件：向上遍历视图树时触发。
Definition: YWidgetEvent.h:107
YReader::ImplDeDtor
ImplDeDtor(AboutPanel) SystemInformationPanel
Definition: About.cpp:103
std
Definition: memory.hpp:1100
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:42
ShlReader.h
Shell 阅读器框架。
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:996
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:540
YSLib::Text::str
const char const char * str
Definition: TextFile.h:77
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:293
YSLib::i
return i
Definition: YCoreUtilities.h:176
BookmarkUI.h
书签界面。
YReader::BookmarkPanel::shell
lref< ShlTextReader > shell
Definition: BookmarkUI.h:56
YSLib::UI::KeyEventArgs
按键输入事件参数类。
Definition: YWidgetEvent.h:186
YReader::BookmarkPanel::BookmarkPanel
BookmarkPanel(const BookmarkList &, ShlTextReader &)
Definition: BookmarkUI.cpp:84