YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
BookmarkUI.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 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 "BookmarkUI.h"
29 #include "ShlReader.h"
30 
31 YSL_BEGIN_NAMESPACE(YReader)
32 
33 namespace
34 {
35 
36 using namespace std;
37 
39 string
40 ConvertToUIString(Bookmark::PositionType pos, ShlTextReader& shl)
41 {
42  auto line(shl.GetSlice(pos, 48U));
43 
44  if(line.size() > 1)
45  {
46  const bool b(line[0] == '\n');
47  const auto i(line.find("\r\n", b));
48 
49  if(i != string::npos)
50  line.erase(i);
51  if(b)
52  line.erase(line.begin());
53  }
54  return to_string(pos) + " " + std::move(line);
55 }
57 vector<String>
58 ConvertToUIString(const BookmarkList& lst, ShlTextReader& shl)
59 {
60  vector<String> vec;
61 
62  vec.reserve(lst.size());
63  for(const auto pos : lst)
64  vec.push_back(ConvertToUIString(pos, shl));
65  return vec;
66 }
67 
69 ConvertToBookmarkList(const vector<String>& lst)
70 {
72 
73  bookmarks.reserve(lst.size());
74  for(const auto& str : lst)
75  bookmarks.push_back(stoi(str.GetMBCS()));
76  return bookmarks;
77 }
78 
79 } // unnamed namespace;
80 
82  : DialogPanel(Size(MainScreenWidth, MainScreenHeight)),
83  lbPosition({8, 32, 240, 128}),
84  btnAdd(Rect(GetWidth() - 80, 4, 16, 16), 210),
85  btnRemove(Rect(GetWidth() - 60, 4, 16, 16), 210), shell(shl), bookmarks(bm)
86 {
87  const auto stop_routing_after_direct([](KeyEventArgs&& e){
88  if(e.Strategy == RoutedEventArgs::Bubble)
89  e.Handled = true;
90  });
91 
92  AddWidgets(*this, lbPosition, btnAdd, btnRemove),
93  yunseq(
94  btnAdd.Text = u"+",
95  btnRemove.Text = u"-",
96  FetchEvent<KeyDown>(lbPosition) += stop_routing_after_direct,
97  FetchEvent<KeyHeld>(lbPosition) += stop_routing_after_direct,
98  FetchEvent<Click>(btnOK) += [this](TouchEventArgs&&){
99  bookmarks = std::move(ConvertToBookmarkList(lbPosition.GetList()));
100  },
101  FetchEvent<Click>(btnAdd) += [this](TouchEventArgs&&){
102  auto& lst(lbPosition.GetListRef());
103  auto idx(GetSelected());
104 
105  if(idx < 0)
106  idx = lst.size();
107  lst.insert(lst.begin() + idx, String(
108  ConvertToUIString(shell.get().GetReaderPosition(), shell)));
109  lbPosition.AdjustViewForContent();
110  lbPosition.UpdateView();
111  },
112  FetchEvent<Click>(btnRemove) += [this](TouchEventArgs&&){
113  auto& lst(lbPosition.GetListRef());
114  const auto idx(GetSelected());
115 
116  if(idx >= 0)
117  {
118  lst.erase(lst.begin() + idx);
119  lbPosition.AdjustViewForContent();
120  lbPosition.UpdateView();
121  }
122  }
123  );
124 }
125 
126 BookmarkList::difference_type
128 {
129  return lbPosition.IsSelected() ? lbPosition.GetSelectedIndex() : -1;
130 }
131 
132 void
134 {
135  lbPosition.GetListRef() = ConvertToUIString(bookmarks, shell);
136 }
137 
138 YSL_END_NAMESPACE(YReader)
139