ncmpc  0.30
ListWindow.hxx
Go to the documentation of this file.
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2018 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef LIST_WINDOW_HXX
21 #define LIST_WINDOW_HXX
22 
23 #include "config.h"
24 #include "command.hxx"
25 #include "colors.hxx"
26 #include "ncmpc_curses.h"
27 #include "Size.hxx"
28 
29 class ListText;
30 class ListRenderer;
31 
39  unsigned start_index;
40 
45  unsigned end_index;
46 
47  constexpr bool empty() const {
48  return start_index >= end_index;
49  }
50 
51  constexpr bool Contains(unsigned i) const {
52  return i >= start_index && i < end_index;
53  }
54 
55  struct const_iterator {
56  unsigned value;
57 
59  ++value;
60  return *this;
61  }
62 
63  constexpr bool operator==(const const_iterator &other) const {
64  return value == other.value;
65  }
66 
67  constexpr bool operator!=(const const_iterator &other) const {
68  return !(*this == other);
69  }
70 
71  const unsigned &operator *() const {
72  return value;
73  }
74  };
75 
76  constexpr const_iterator begin() const {
77  return {start_index};
78  }
79 
80  constexpr const_iterator end() const {
81  return {end_index};
82  }
83 };
84 
85 struct ListWindow {
86  WINDOW *w;
88 
92  unsigned length = 0;
93 
94  unsigned start = 0;
95  unsigned selected = 0;
96 
100  unsigned range_base = 0;
101 
105  bool range_selection = false;
106 
107  bool hide_cursor = false;
108 
109  ListWindow(WINDOW *_w, Size _size)
110  :w(_w), size(_size) {}
111 
113  void Reset();
114 
115  void Resize(Size _size);
116 
117  void SetLength(unsigned length);
118 
119  void Paint(const ListRenderer &renderer) const;
120 
122  bool HandleCommand(command_t cmd);
123 
128  bool HandleScrollCommand(command_t cmd);
129 
130 #ifdef HAVE_GETMOUSE
131 
135  bool HandleMouse(mmask_t bstate, int y);
136 #endif
137 
141  void Center(unsigned n);
142 
147  void ScrollTo(unsigned n);
148 
152  void SetCursor(unsigned i);
153 
158  void MoveCursor(unsigned n);
159 
160  void MoveCursorNext();
161  void MoveCursorPrevious();
162  void MoveCursorTop();
163  void MoveCursorMiddle();
164  void MoveCursorBottom();
165  void MoveCursorFirst();
166  void MoveCursorLast();
167  void MoveCursorNextPage();
168  void MoveCursorPreviousPage();
169 
170  void ScrollUp(unsigned n);
171  void ScrollDown(unsigned n);
172 
177  void FetchCursor();
178 
184  gcc_pure
185  ListWindowRange GetRange() const;
186 
190  bool Find(const ListText &text,
191  const char *str,
192  bool wrap,
193  bool bell_on_wrap);
194 
198  bool ReverseFind(const ListText &text,
199  const char *str,
200  bool wrap,
201  bool bell_on_wrap);
202 
207  bool Jump(const ListText &text, const char *str);
208 
209 private:
210  gcc_pure
211  unsigned ValidateIndex(unsigned i) const;
212 
213  void CheckSelected();
214 
219  void CheckOrigin() {
221  }
222 };
223 
224 #endif
void Center(unsigned n)
bool Find(const ListText &text, const char *str, bool wrap, bool bell_on_wrap)
unsigned start
Definition: ListWindow.hxx:94
constexpr const_iterator begin() const
Definition: ListWindow.hxx:76
void SetLength(unsigned length)
unsigned value
Definition: ListWindow.hxx:56
Definition: ListWindow.hxx:35
void MoveCursorLast()
ListWindow(WINDOW *_w, Size _size)
Definition: ListWindow.hxx:109
void ScrollTo(unsigned n)
void MoveCursorMiddle()
void MoveCursorPreviousPage()
void ScrollUp(unsigned n)
bool hide_cursor
Definition: ListWindow.hxx:107
constexpr bool Contains(unsigned i) const
Definition: ListWindow.hxx:51
constexpr bool operator==(const const_iterator &other) const
Definition: ListWindow.hxx:63
const unsigned & operator*() const
Definition: ListWindow.hxx:71
bool HandleCommand(command_t cmd)
void MoveCursorBottom()
Definition: ListWindow.hxx:55
unsigned range_base
Definition: ListWindow.hxx:100
void FetchCursor()
const_iterator & operator++()
Definition: ListWindow.hxx:58
void Paint(const ListRenderer &renderer) const
void SetCursor(unsigned i)
Definition: ListRenderer.hxx:25
Definition: Size.hxx:26
void MoveCursorNextPage()
bool HandleScrollCommand(command_t cmd)
void Reset()
void MoveCursorTop()
constexpr const_iterator end() const
Definition: ListWindow.hxx:80
void MoveCursor(unsigned n)
void Resize(Size _size)
unsigned start_index
Definition: ListWindow.hxx:39
Definition: ListText.hxx:25
WINDOW * w
Definition: ListWindow.hxx:86
constexpr bool empty() const
Definition: ListWindow.hxx:47
unsigned end_index
Definition: ListWindow.hxx:45
bool Jump(const ListText &text, const char *str)
void MoveCursorFirst()
#define gcc_pure
Definition: Compiler.h:101
command_t
Definition: command.hxx:35
unsigned length
Definition: ListWindow.hxx:92
bool ReverseFind(const ListText &text, const char *str, bool wrap, bool bell_on_wrap)
Size size
Definition: ListWindow.hxx:87
Definition: ListWindow.hxx:85
constexpr bool operator!=(const const_iterator &other) const
Definition: ListWindow.hxx:67
gcc_pure ListWindowRange GetRange() const
bool range_selection
Definition: ListWindow.hxx:105
unsigned selected
Definition: ListWindow.hxx:95
void ScrollDown(unsigned n)
void MoveCursorPrevious()
void MoveCursorNext()