libyui-ncurses  2.57.2
NCPad.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCPad.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef NCPad_h
26 #define NCPad_h
27 
28 #include <iosfwd>
29 
30 #include "NCurses.h"
31 #include "NCWidget.h"
32 
33 
34 //! Interface for scroll callbacks
36 {
37 public:
38 
39  virtual ~NCSchrollCB() {}
40 
41  /// @param total virtual size
42  /// @param visible size of the visible part
43  /// @param start position of the visible part
44  virtual void HScroll( unsigned total, unsigned visible, unsigned start ) {}
45 
46  /// @param total virtual size
47  /// @param visible size of the visible part
48  /// @param start position of the visible part
49  virtual void VScroll( unsigned total, unsigned visible, unsigned start ) {}
50 
51  virtual void ScrollHead( NCursesWindow & w, unsigned ccol ) {}
52 
53  virtual void AdjustPadSize( wsze & minsze ) {}
54 };
55 
56 /**
57  * Forward the scroll callbacks to another object.
58  * By default it forwards to itself
59  */
60 class NCScrollHint : protected NCSchrollCB
61 {
62 private:
63 
64  NCSchrollCB * redirect;
65 
66 protected:
67 
68  NCScrollHint() : redirect( this ) {}
69 
70  virtual ~NCScrollHint() {}
71 
72 protected:
73 
74  virtual void SetHead( NCursesWindow & w, unsigned ccol )
75  {
76  redirect->ScrollHead( w, ccol );
77  }
78 
79  void VSet( unsigned total, unsigned visible, unsigned start )
80  {
81  redirect->VScroll( total, visible, start );
82  }
83 
84  void HSet( unsigned total, unsigned visible, unsigned start )
85  {
86  redirect->HScroll( total, visible, start );
87  }
88 
89  virtual void SetPadSize( wsze & minsze )
90  {
91  redirect->AdjustPadSize( minsze );
92  }
93 
94 public:
95 
96  //! Set the receiver of callbacks to *dest*
97  void SendSchrollCB( NCSchrollCB * dest ) { redirect = ( dest ? dest : this ); }
98 
99  virtual void SendHead() {}
100 };
101 
102 
103 /// A virtual window with a real viewport (which is NCursesWindow)
104 /// and a scrolling mechanism.
105 ///
106 /// In the underlying C ncurses library, a *pad* is a virtual window without a
107 /// position. Of course that is not very useful without any way to display it
108 /// so there's a `prefresh` function to draw a portion of that pad into a
109 /// visible "viewport" window. Our NCursesPad fork just directly forwards to
110 /// `prefresh` without remembering the window. Upstream NCursesPad does know a
111 /// viewport window and so does NCPad (*destwin*).
112 class NCPad : public NCursesPad, public NCScrollHint
113 {
114 private:
115 
116  /** The real height in case the NCursesPad is truncated, otherwise \c 0.
117  *
118  * \note Don't use _vheight directly, but \ref vheight.
119  *
120  * Up to ncurses5, ncurses uses \c short for window dimensions (can't hold
121  * more than 32768 lines). If \ref resize truncated the window, the real
122  * size is in \ref _vheight. Longer lists need to be paged.
123  *
124  * \todo Once all NCPad based types are able to page, \a maxPadHeight could be
125  * std::set to e.g \c 1024 to avoid bigger widgets in memory. Currently just
126  * \ref NCTablePad supports paging. If paging is \c ON, all content lines are
127  * written via \ref directDraw. Without paging \ref DoRedraw is reponsible for this.
128  */
129  int _vheight;
130 
131 protected:
132 
133  const NCWidget & parw;
134 
135  NCursesWindow * destwin; ///< Where to draw us (may be nullptr, not owned)
136  ///< Destination rectangle: (Pos is always 0, 0)
137  wrect drect;
138  wrect srect; ///< Source rectangle: the visible part of this pad
139  wpos maxdpos;
140  wpos maxspos;
141 
142  bool dclear; ///< should destwin be cleared before contents is copied there
143  bool dirty;
144 
145  /** The (virtual) height of the Pad (even if truncated). */
146  int vheight() const { return _vheight ? _vheight : height(); }
147 
148  /** Whether the Pad is truncated (we're paging). */
149  bool paging() const { return _vheight; }
150 
151  virtual int dirtyPad() { dirty = false; return setpos( CurPos() ); }
152 
153  /// Set the visible position to *newpos* (but clamp by *maxspos*), then \ref update.
154  virtual int setpos( const wpos & newpos );
155 
156  /// Adjust CurPos relatively by *offset*
157  int adjpos( const wpos & offset )
158  {
159  return setpos( CurPos() + offset );
160  }
161 
162  virtual void updateScrollHint();
163 
164  /** Directly draw a table item at a specific location.
165  *
166  * \ref update usually copies the visible table content from the
167  * \ref NCursesPad to \ref destwin. In case the \ref NCursesPad
168  * is truncated, the visible lines are prepared immediately before
169  * they are written to \ref destwin
170  * .
171  * \see \ref _vheight.
172  */
173  virtual void directDraw( NCursesWindow & w, const wrect at, unsigned lineno ) {}
174 
175 public:
176 
177  /// @param p (used just for styling info, NOT sizing)
178  NCPad( int lines, int cols, const NCWidget & p );
179  virtual ~NCPad() {}
180 
181 public:
182 
183  NCursesWindow * Destwin() { return destwin; }
184 
185  /// @param dwin (not owned)
186  virtual void Destwin( NCursesWindow * dwin );
187 
188  virtual void resize( wsze nsze );
189  // OMFG this little overload does something completely different than
190  // the one above
191  virtual int resize( int lines, int columns ) { return NCursesWindow::resize(lines, columns );}
192  virtual void wRecoded();
193  virtual void setDirty() { dirty = true; }
194 
195  int update();
196  virtual int setpos() { return setpos( CurPos() ); }
197 
198  virtual wpos CurPos() const { return srect.Pos; }
199 
200  int ScrlTo( const wpos & newpos )
201  {
202  return setpos( newpos );
203  }
204 
205  /// Scroll to a line, keeping the column
206  int ScrlLine( int line )
207  {
208  return setpos( wpos( line, srect.Pos.C ) );
209  }
210 
211  /// Scroll to a column, keeping the line
212  int ScrlCol( int col )
213  {
214  return setpos( wpos( srect.Pos.L, col ) );
215  }
216 
217  int ScrlDown( int lines = 1 )
218  {
219  return adjpos( wpos( lines, 0 ) );
220  }
221 
222  int ScrlUp( int lines = 1 )
223  {
224  return adjpos( wpos( -lines, 0 ) );
225  }
226 
227  int ScrlRight( int cols = 1 )
228  {
229  return adjpos( wpos( 0, cols ) );
230  }
231 
232  int ScrlLeft( int cols = 1 )
233  {
234  return adjpos( wpos( 0, -cols ) );
235  }
236 
237  int ScrlToLastLine()
238  {
239  return ScrlDown( vheight() );
240  }
241 
242  virtual bool handleInput( wint_t key );
243 };
244 
245 
246 #endif // NCPad_h
NCursesWindow::lines
static int lines()
Number of lines on terminal, not window.
Definition: ncursesw.h:1044
wsze
Screen dimension (screen size) in the order height, width: (H, W)
Definition: position.h:154
NCPad::destwin
NCursesWindow * destwin
Where to draw us (may be nullptr, not owned)
Definition: NCPad.h:135
NCursesWindow
C++ class for windows.
Definition: ncursesw.h:907
NCPad::ScrlCol
int ScrlCol(int col)
Scroll to a column, keeping the line.
Definition: NCPad.h:212
NCPad::ScrlLine
int ScrlLine(int line)
Scroll to a line, keeping the column.
Definition: NCPad.h:206
NCPad::vheight
int vheight() const
The (virtual) height of the Pad (even if truncated).
Definition: NCPad.h:146
NCursesWindow::cols
static int cols()
Number of cols on terminal, not window.
Definition: ncursesw.h:1049
NCSchrollCB
Interface for scroll callbacks.
Definition: NCPad.h:36
NCursesWindow::w
WINDOW * w
the curses WINDOW
Definition: ncursesw.h:949
NCWidget
Definition: NCWidget.h:46
NCPad
A virtual window with a real viewport (which is NCursesWindow) and a scrolling mechanism.
Definition: NCPad.h:113
wpos
Screen position pair in the order line, column: (L, C)
Definition: position.h:110
NCSchrollCB::HScroll
virtual void HScroll(unsigned total, unsigned visible, unsigned start)
Definition: NCPad.h:44
NCSchrollCB::VScroll
virtual void VScroll(unsigned total, unsigned visible, unsigned start)
Definition: NCPad.h:49
NCursesWindow::height
int height() const
Number of lines in this window.
Definition: ncursesw.h:1072
NCPad::paging
bool paging() const
Whether the Pad is truncated (we're paging).
Definition: NCPad.h:149
NCPad::NCPad
NCPad(int lines, int cols, const NCWidget &p)
Definition: NCPad.cc:40
NCPad::dclear
bool dclear
should destwin be cleared before contents is copied there
Definition: NCPad.h:142
NCScrollHint
Forward the scroll callbacks to another object.
Definition: NCPad.h:61
NCPad::setpos
virtual int setpos(const wpos &newpos)
Set the visible position to newpos (but clamp by maxspos), then update.
Definition: NCPad.cc:158
wrect
A rectangle is defined by its position and size: wpos Pos, wsze Sze.
Definition: position.h:194
NCursesPad
Definition: ncursesw.h:1832
NCPad::directDraw
virtual void directDraw(NCursesWindow &w, const wrect at, unsigned lineno)
Directly draw a table item at a specific location.
Definition: NCPad.h:173
NCScrollHint::SendSchrollCB
void SendSchrollCB(NCSchrollCB *dest)
Set the receiver of callbacks to dest
Definition: NCPad.h:97
NCPad::srect
wrect srect
Source rectangle: the visible part of this pad.
Definition: NCPad.h:138
NCPad::adjpos
int adjpos(const wpos &offset)
Adjust CurPos relatively by offset
Definition: NCPad.h:157