YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
HexBrowser.cpp
浏览该文件的文档.
1 /*
2  © 2011-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 "HexBrowser.h"
29 
30 namespace YSLib
31 {
32 
33 using namespace Drawing;
34 using namespace Text;
35 
36 namespace UI
37 {
38 
40  : TextState(fc), item_num(0), datCurrent()
41 {
43 }
44 
45 
46 HexViewArea::HexViewArea(const Rect& r, FontCache& fc)
47  : ScrollableContainer(r), HexView(fc),
48  model()
49 {
50  SetVisibleOf(hsbHorizontal, false);
51  SetVisibleOf(vsbVertical, true);
52  vsbVertical.SetSmallDelta(1);
53  yunseq(
54  vsbVertical.GetTrackRef().GetScroll() += [this](ScrollEventArgs&& e){
55  LocateViewPosition(round(e.GetValue()));
56  },
57  FetchEvent<KeyDown>(*this) += [this](KeyEventArgs&& e){
58  using namespace KeyCodes;
59 
60  const auto& k(e.GetKeys());
61 
62  if(k.count() != 1)
63  return;
64 
66 
67  if(k[Down])
69  else if(k[PgUp])
71  else if(k[PgDn])
73  else if(!k[Up])
74  return;
75  vsbVertical.LocateThumb(k[Up] || k[Down] ? vsbVertical.GetSmallDelta()
76  : vsbVertical.GetLargeDelta(), t);
77  RequestFocus(*this);
78  e.Handled = true;
79  },
80  FetchEvent<KeyHeld>(*this) += OnKeyHeld
81  );
82  Reset();
83 }
84 
85 void
86 HexViewArea::Load(const char* path)
87 {
88  Reset();
89  model = make_unique<File>(path);
90 
91  const IndexType n_total_ln((model.GetSize() + ItemPerLine - 1)
92  / ItemPerLine);
93 
94  if(n_total_ln > GetItemNum())
95  {
96  vsbVertical.SetMaxValue(n_total_ln - GetItemNum());
97  vsbVertical.SetLargeDelta(GetItemNum());
98  }
99  else
100  SetVisibleOf(vsbVertical, false);
101 }
102 
103 void
105 {
106  UpdateData(ItemPerLine * line);
107  UpdateView(true);
108 }
109 
110 void
112 {
113  using namespace Text;
114 
115  // TODO: Refresh for 'rect' properly.
116 // Background(PaintEventArgs(*this, e.Target, e.Location, Rect(e.Location,
117 // GetWidth(), GetHeight())));
118 // Background(PaintEventArgs(*this, e));
119  ScrollableContainer::Refresh(std::move(e));
121 
122  // NOTE: It seems there is a bug in linker for checking odr-using.
124  auto& y(TextState.Pen.Y);
125  const SDst lh(GetItemHeight()), h(GetHeight()),
126  w_all(GetWidth() - vsbVertical.GetWidth()
127  - GetHorizontalOf(TextState.Margin)),
128  w_blank(w_all / (10 + ItemPerLine * 3)),
129  w_ch((w_all - w_blank * (1 + ItemPerLine)) / (8 + ItemPerLine * 2)),
130  w_addr(w_ch * 8 + w_blank),
131  w_item(w_ch * 2 + w_blank);
132  const int fsize(model.GetSize());
133  auto& pen_x(TextState.Pen.X);
134  TextRenderer tr(TextState, e.Target);
135  auto pos(model.GetPosition());
136  auto i_data(datCurrent.begin());
137 
138  while(y < h && pos < fsize && i_data < datCurrent.end())
139  {
140  pen_x = TextState.Margin.Left;
141 
142  auto x(pen_x);
143 
144  {
145  char straddr[(32 >> 2) + 1];
146 
147  std::sprintf(straddr, "%08X", pos);
148  PutLine(tr, straddr);
149  }
150  x += w_addr;
151 
152  const auto n(min<IndexType>(fsize - pos, ItemPerLine));
153 
154  for(IndexType j(0); j < n; yunseq(++j, i_data += 2, x += w_item))
155  {
156  pen_x = x;
157  PutLine(tr, &*i_data, &*i_data + 2);
158  }
159  yunseq(y += lh + TextState.LineGap, pos += ItemPerLine);
160  }
161 }
162 
163 void
165 {
166  vsbVertical.SetValue(0);
167  datCurrent.clear();
168  UpdateItemNum(GetHeight());
169  UpdateView();
170 }
171 
172 void
174 {
175  if(model.IsValid() && pos < model.GetSize())
176  {
177  const DataType::size_type n(ItemPerLine * GetItemNum() * 2);
178 
179  model.SetPosition(pos, SEEK_SET);
180  datCurrent.resize(n);
181 
182  auto b(datCurrent.begin());
183  const auto e(datCurrent.end());
184 
185  while(!model.CheckEOF() && b != e)
186  {
187  byte c(std::fgetc(model.GetPtr()));
188  char h, l;
189 
190  yunseq(h = (c >> 4 & 0x0F) + '0', l = (c & 0x0F) + '0');
191  *b++ = h > '9' ? h + 'A' - '9' - 1 : h;
192  *b++ = l > '9' ? l + 'A' - '9' - 1 : l;
193  }
194  // vsbVertical.SetValue(pos / ItemPerLine);
195  datCurrent.resize(b - datCurrent.begin());
196  model.SetPosition(pos, SEEK_SET); // Refresh 需要据此判断接近文件结尾。
197  }
198 }
199 
200 void
201 HexViewArea::UpdateView(bool is_active)
202 {
203  ViewChanged(ViewArgs(*this, is_active));
204  Invalidate(*this);
205 }
206 
207 } // namespace UI;
208 
209 } // namespace YSLib;
210 
Point Pen
笔坐标。
Definition: TextBase.h:99
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Definition: HexBrowser.cpp:111
static yconstexpr size_t ItemPerLine
每行数据总数(字节)。
Definition: HexBrowser.h:104
u8 LineGap
行距。
Definition: TextBase.h:100
YF_API void OnKeyHeld(KeyEventArgs &&)
处理键接触保持事件。
Definition: ycontrol.cpp:108
std::uint32_t u32
Definition: yadaptor.h:69
滚动事件参数类。
Definition: scroll.h:67
按键输入事件参数类。
Definition: ywgtevt.h:167
YF_API void Invalidate(IWidget &, const Rect &)
无效化:使相对于部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.cpp:111
滚动框大距离减量移动。
部件绘制参数。
Definition: ywgtevt.h:276
void UpdateView(TextList &tl, bool is_active)
Definition: textlist.cpp:431
unsigned char byte
字节类型。
Definition: ydef.h:555
ScrollCategory
滚动类别。
Definition: scroll.h:49
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
std::uintptr_t IndexType
索引类型。
Definition: HexBrowser.h:106
Padding Margin
边距:文本区域到显示区域的距离。
Definition: TextBase.h:94
十六进制视图。
Definition: HexBrowser.h:101
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
滚动框小距离减量移动。
带滚动条的容器。
Definition: scroll.h:414
字体缓存。
Definition: Font.h:415
文本状态。
Definition: TextBase.h:87
文本渲染器:简单实现。
Definition: TextRenderer.h:361
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
void LocateViewPosition(SDst)
定位视图顶端至指定竖直位置。
Drawing::Color Color
笔颜色。
Definition: TextBase.h:58
void LocateViewPosition(size_t)
定位视图顶端至指定竖直位置(行数)。
Definition: HexBrowser.cpp:104
十六进制浏览器。
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
_tIter PutLine(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串,直至行尾或字符迭代终止。
Definition: TextRenderer.h:110
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
bounds & r
Definition: ydraw.h:220
滚动框大距离增量移动。
void UpdateView(bool={})
更新视图。
Definition: HexBrowser.cpp:201
DataType datCurrent
当前显示的数据。
Definition: HexBrowser.h:120
virtual void Refresh(PaintEventArgs &&)
刷新:按指定参数绘制界面并更新状态。
Definition: ywidget.cpp:264
PDefHOp(TextState &,=, const PenStyle &ps) ImplRet(PenStyle void ResetPen()
赋值:笔样式。
Definition: TextBase.h:145
滚动框小距离增量移动。
GValueEventArgs< bool > ViewArgs
视图参数类型。
Definition: textlist.h:67
HexView(FontCache &=FetchDefaultFontCache())
Definition: HexBrowser.cpp:39