YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
BookmarkUI.cpp
浏览该文件的文档.
1 /*
2  © 2013-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 "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  line.erase(line.begin());
54  }
55  return to_string(pos) + " " + std::move(line);
56 }
58 vector<String>
59 ConvertToUIString(const BookmarkList& lst, ShlTextReader& shl)
60 {
61  vector<String> vec;
62 
63  vec.reserve(lst.size());
64  for(const auto pos : lst)
65  vec.push_back(ConvertToUIString(pos, shl));
66  return vec;
67 }
68 
70 ConvertToBookmarkList(const vector<String>& lst)
71 {
72  BookmarkList bookmarks;
73 
74  bookmarks.reserve(lst.size());
75  for(const auto& str : lst)
76  bookmarks.push_back(stoul(str.GetMBCS()));
77  return bookmarks;
78 }
79 
80 } // unnamed namespace;
81 
83  : DialogPanel(Size(MainScreenWidth, MainScreenHeight)),
84  lbPosition({8, 32, 240, 128}),
85  btnAdd(Rect(GetWidth() - 80, 4, 16, 16), 210),
86  btnRemove(Rect(GetWidth() - 60, 4, 16, 16), 210), shell(shl), bookmarks(bm)
87 {
88  const auto stop_routing_after_direct([](KeyEventArgs&& e){
89  if(e.Strategy == RoutedEventArgs::Bubble)
90  e.Handled = true;
91  });
92 
93  AddWidgets(*this, lbPosition, btnAdd, btnRemove),
94  yunseq(
95  btnAdd.Text = u"+",
96  btnRemove.Text = u"-",
97  FetchEvent<KeyDown>(lbPosition) += stop_routing_after_direct,
98  FetchEvent<KeyHeld>(lbPosition) += stop_routing_after_direct,
99  FetchEvent<Click>(btnOK) += [this]{
100  bookmarks = ConvertToBookmarkList(lbPosition.GetList());
101  },
102  FetchEvent<Click>(btnAdd) += [this]{
103  auto& lst(lbPosition.GetListRef());
104  auto idx(GetSelected());
105 
106  if(idx < 0)
107  idx = lst.size();
108  lst.insert(lst.begin() + idx,
109  String(ConvertToUIString(shell.get().GetReaderPosition(), shell)));
110  lbPosition.AdjustViewForContent();
111  lbPosition.UpdateView();
112  },
113  FetchEvent<Click>(btnRemove) += [this]{
114  auto& lst(lbPosition.GetListRef());
115  const auto idx(GetSelected());
116 
117  if(idx >= 0)
118  {
119  lst.erase(lst.begin() + idx);
120  lbPosition.AdjustViewForContent();
121  lbPosition.UpdateView();
122  }
123  }
124  );
125 }
126 
127 BookmarkList::difference_type
129 {
130  return lbPosition.IsSelected() ? lbPosition.GetSelectedIndex() : -1;
131 }
132 
133 void
135 {
136  lbPosition.GetListRef() = ConvertToUIString(bookmarks, shell);
137 }
138 
139 } // namespace YReader;
140 
按键输入事件参数类。
Definition: ywgtevt.h:167
文本阅读器 Shell 。
Definition: ShlReader.h:215
size_t PositionType
文件位置类型。
Definition: ReadingList.h:49
书签界面。
void AddWidgets(_tCon &con, _tWidgets &...wgts)
向部件容器添加子部件。
Definition: yuicont.h:400
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
vector< Bookmark::PositionType > BookmarkList
书签列表类型。
Definition: ReadingList.h:82
BookmarkList::difference_type GetSelected() const
Definition: BookmarkUI.cpp:128
BookmarkPanel(const BookmarkList &, ShlTextReader &)
Definition: BookmarkUI.cpp:82
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
气泡事件:向上遍历视图树时触发。
Definition: ywgtevt.h:102
屏幕区域大小。
Definition: ygdibase.h:249
std::reference_wrapper< ShlTextReader > shell
Definition: BookmarkUI.h:55
Shell 阅读器框架。
对话面板。
Definition: uicontx.h:73