panel_interface.cpp
Go to the documentation of this file.
1 
29 
30 #include <cmath>
31 
32 namespace log_view {
33 
34 PanelInterface::PanelInterface(int height, int width, int y, int x) :
35  x_(x),
36  y_(y),
37  width_(width),
38  height_(height)
39 {
40  window_ = newwin(height_, width_, y_, x_);
41  panel_ = new_panel(window_);
42 }
43 
45  delwin(window_);
46 }
47 
49  werase(window_);
50  cleared_ = true;
51  refresh();
52 }
53 
54 void PanelInterface::resize(int height, int width, int y, int x) {
55  x_ = x;
56  y_ = y;
57  width_ = width;
58  height_ = height;
59  werase(window_);
60  mvwin(window_, y_, x_);
61  wresize(window_, height_, width_);
62  cleared_ = true;
63  refresh();
64 }
65 
67  if (!canInput() || !focus_) {
68  return false;
69  }
70 
71  bool key_used = false;
72  if (val < 256 && isprint(val)) {
73  if (input_loc_ == -1 || input_loc_ >= input_text_.size()) {
74  input_text_ += val;
75  }
76  else {
77  input_loc_ = std::max(0, input_loc_);
78  input_text_.insert(input_loc_, 1, static_cast<char>(val));
79  input_loc_++;
80  }
81  key_used = true;
82  }
83  else if (!input_text_.empty() && val == KEY_BACKSPACE && (input_loc_ == -1 || input_loc_ > 0)) {
84  if (input_loc_ == -1 || input_loc_ >= input_text_.size()) {
85  input_text_.pop_back();
86  }
87  else {
88  input_text_.erase(input_loc_ - 1, 1);
89  input_loc_--;
90  }
91  key_used = true;
92  }
93  else if (!input_text_.empty() && val == KEY_DC && input_loc_ != -1) {
94  input_text_.erase(input_loc_, 1);
95  if (input_loc_ >= input_text_.size()) {
96  input_loc_ = -1;
97  }
98  key_used = true;
99  }
100  else if (input_loc_ != 0 && val == KEY_LEFT) {
101  if (input_loc_ == -1) {
102  input_loc_ = input_text_.size();
103  }
104  input_loc_--;
105  key_used = true;
106  }
107  else if (input_loc_ != -1 && val == KEY_RIGHT) {
108  input_loc_++;
109  if (input_loc_ >= input_text_.size()) {
110  input_loc_ = -1;
111  }
112  key_used = true;
113  }
114 
115  if (key_used) {
116  // trigger underlying action of input (filtering, etc.)
117  activate(true);
118 
119  // refresh display
120  werase(window_);
121  cleared_ = true;
122  refresh();
123  }
124 
125  return key_used;
126 }
127 
129  if (!canNavigate() || hidden_ || (canFocus() && !focus())) {
130  return false;
131  }
132 
133  bool key_used = false;
134  if (key == KEY_NPAGE) {
135  pageDown();
136  key_used = true;
137  }
138  else if (key == KEY_PPAGE) {
139  pageUp();
140  key_used = true;
141  }
142  else if (key == KEY_UP) {
143  move(-1);
144  key_used = true;
145  }
146  else if (key == KEY_DOWN) {
147  move(1);
148  key_used = true;
149  }
150  else if (key == KEY_END) {
151  follow(true);
152  key_used = true;
153  }
154  else if (key == KEY_HOME) {
155  moveTo(0);
156  key_used = true;
157  }
158  else if (key == KEY_LEFT) {
159  shift(-5);
160  key_used = true;
161  }
162  else if (key == KEY_RIGHT) {
163  shift(5);
164  key_used = true;
165  }
166  else if (canSelect() && key == ' ') {
167  select();
168  key_used = true;
169  }
170 
171  if (key_used) {
172  // refresh display
173  werase(window_);
174  cleared_ = true;
175  refresh();
176  }
177 
178  return key_used;
179 }
180 
181 bool PanelInterface::encloses(int y, int x) {
182  return y >= y_ && y < y_ + height_ && x >= x_ && x < x_ + width_;
183 }
184 
185 void PanelInterface::hide(bool enable) {
186  if (enable == hidden_) {
187  return;
188  }
189 
190  hidden_ = enable;
191  if (hidden_) {
192  hide_panel(panel_);
193  activate(false);
194  focus_ = false;
195  }
196  else {
197  show_panel(panel_);
198  activate(true);
199  input_loc_ = -1;
200  if (canFocus()) {
201  focus_ = true;
202  }
203  }
204 }
205 
206 bool PanelInterface::setFocus(bool enable) {
207  focus_ = false;
208  if (enable && !hidden_ && canFocus()) {
209  focus_ = true;
210  }
211 
212  return focus_;
213 }
214 
216  if (focus_ && canInput()) {
217  int loc = input_loc_;
218  if (loc == -1) {
219  loc = input_text_.size();
220  }
221 
222  wmove(window_, 0, inputOffset() + loc);
223  show_panel(panel_);
224  curs_set(1);
225  return true;
226  }
227  return false;
228 }
229 
230 void PanelInterface::drawScrollBar(size_t count, int height, int y, int x) {
231  if (count <= height) {
232  return;
233  }
234 
235  mvwvline(window_, y, x, 0, height);
236 
237  int64_t cursor = getCursor();
238  if (cursor < 0) {
239  cursor = count;
240  }
241 
242  if (count >= 2 * height) {
243  float percent = std::max(0.0f, static_cast<float>((cursor - height)) / (count - height));
244  int scroll_loc = std::round(percent * (height - 1));
245 
246  wattron(window_, A_REVERSE);
247  mvwprintw(window_, y + scroll_loc, x, " ");
248  wattroff(window_, A_REVERSE);
249  }
250  else {
251  int size = 2 * height - count;
252  cursor = std::max(static_cast<int64_t>(0), cursor - height);
253 
254  wattron(window_, A_REVERSE);
255  for (size_t i = cursor; i < cursor + size; i++) {
256  mvwprintw(window_, y + i, x, " ");
257  }
258  wattroff(window_, A_REVERSE);
259  }
260 }
261 
263  top_panel(panel_);
264 }
265 
266 int PanelInterface::x() const {
267  return x_;
268 }
269 
270 int PanelInterface::y() const {
271  return y_;
272 }
273 
275  return width_;
276 }
277 
279  return height_;
280 }
281 
283  return hidden_;
284 }
285 
287  return !hidden_;
288 }
289 
290 bool PanelInterface::focus() const {
291  return canFocus() && focus_;
292 }
293 
295  return getContentSize() > getContentHeight();
296 }
297 
298 void PanelInterface::follow(bool enable) {
299  if (enable && !following()) {
300  last_content_size_ = 0;
301  last_cursor_ = -1;
302  setCursor(-1);
303  }
304  else if (!enable && following()) {
306  }
307 }
308 
310  move(-(getContentHeight()));
311 }
312 
315 }
316 
317 void PanelInterface::move(int step) {
318  if (step == 0 || getContentSize() == 0) {
319  return;
320  }
321 
322  if (following() && step > 0) {
323  return;
324  }
325 
326  if (following() && step < 0) {
327  follow(false);
328  }
329 
330  int64_t cursor = getCursor();
331  int64_t dst = std::max(static_cast<int64_t>(0), cursor + step);
332 
333  moveTo(dst);
334 }
335 
336 void PanelInterface::moveTo(size_t index) {
337  size_t view_size = getContentHeight();
338  if (getContentSize() <= view_size || index > getContentSize() - 1) {
339  follow(true);
340  return;
341  }
342 
343  if (index < view_size) {
344  index = view_size;
345  }
346  follow(false);
347  setCursor(index);
348 }
349 
350 void PanelInterface::shift(int cols) {
351  int shift = shift_;
352  if (cols < 0) {
353  shift += cols;
354  shift = std::max(0, shift);
355  }
356  else if (shift_ + getContentWidth() < max_length_) {
357  shift += cols;
358  }
359 
360  shift_ = shift;
361 }
362 
363 } // namespace log_view
virtual bool handleInput(int key)
virtual int getContentHeight() const
virtual bool scrollbar() const
virtual bool encloses(int y, int x)
f
virtual int width() const
virtual void resize(int height, int width, int y, int x)
virtual void refresh()=0
virtual int inputOffset() const
virtual int getContentWidth() const
virtual void moveTo(size_t index)
virtual int y() const
virtual bool canSelect() const
virtual bool hidden() const
virtual void hide(bool enable)
virtual void move(int step)
virtual bool canFocus() const
virtual int x() const
virtual bool setFocus(bool enable)
virtual bool canNavigate() const
virtual void shift(int cols)
virtual bool visible() const
virtual bool handleNavigation(int key)
virtual void activate(bool enable)
virtual size_t getContentSize() const
virtual int64_t getCursor() const
PanelInterface(int height, int width, int y, int x)
virtual int height() const
virtual void drawScrollBar(size_t count, int height, int y, int x)
virtual bool focus() const
virtual bool canInput() const
virtual void follow(bool enable)


log_view
Author(s): Marc Alban
autogenerated on Thu Mar 4 2021 03:21:52