libyui-ncurses  2.57.2
NCTablePad.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  Copyright (C) 2020 SUSE LLC
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 
18 /*-/
19 
20  File: NCTablePad.cc
21 
22  Authors: Michael Andres <ma@suse.de>
23  Stefan Hundhammer <shundhammer@suse.de>
24 
25 /-*/
26 
27 #define YUILogComponent "ncurses"
28 #include <yui/YUILog.h>
29 #include "NCTablePad.h"
30 
31 
32 
33 NCTablePad::NCTablePad( int lines, int cols, const NCWidget & p )
34  : NCTablePadBase( lines, cols, p )
35 {
36 }
37 
38 
39 NCTablePad::~NCTablePad()
40 {
41 }
42 
43 
45 {
46  if ( !Destwin() )
47  {
48  dirty = true;
49  return OK;
50  }
51 
52  prepareRedraw();
53 
54  if ( ! paging() )
56  // else
57  // item drawing requested via directDraw()
58 
59  drawHeader();
60 
61  dirty = false;
62 
63  return update();
64 }
65 
66 
67 void NCTablePad::directDraw( NCursesWindow & w, const wrect at, unsigned lineNo )
68 {
69  if ( lineNo < Lines() )
70  {
71  _visibleItems[ lineNo ]->DrawAt( w,
72  at,
73  _itemStyle,
74  ( (unsigned) currentLineNo() == lineNo) );
75  }
76  else
77  yuiWarning() << "Illegal Line no " << lineNo << " (" << Lines() << ")" << std::endl;
78 }
79 
80 
81 bool NCTablePad::handleInput( wint_t key )
82 {
83  bool handled = false;
84 
85  switch ( key )
86  {
87  // At this time, there are no more special keys to handle on this
88  // level. This method is a stub for future extension if any more keys
89  // need to be handled.
90  //
91  // Add 'case KEY_XXX' branches here if there should be any
92  // and don't forget to set 'handled' to 'true'.
93 #if 0
94  case KEY_SOMETHING: // Sample
95  doSomething();
96  handled = true;
97  break;
98 #endif
99 
100  default: // Call parent class input handler
101 
102  // This also calls currentItemHandleInput() as the first thing it
103  // does to forward key presses to the item at the current cursor
104  // position. Many operations such as opening, closing or selecting
105  // an item are done on that level.
106  handled = NCTablePadBase::handleInput( key );
107  break;
108  }
109 
110  return handled;
111 }
112 
113 
114 bool NCTablePad::setItemByKey( int key )
115 {
116  if ( HotCol() >= Cols() )
117  return false;
118 
119  if ( key < 0 || UCHAR_MAX < key )
120  return false;
121 
122  unsigned hcol = HotCol();
123  unsigned hkey = tolower( key );
124 
125  for ( unsigned i = 0; i < visibleLines(); ++i )
126  {
127  if ( _items[i]->GetCol( hcol )->hasHotkey()
128  && (unsigned) tolower( _items[i]->GetCol( hcol )->hotkey() ) == hkey )
129  {
130  ScrlLine( i );
131  return true;
132  }
133  }
134 
135  return false;
136 }
137 
138 
139 void NCTablePad::stripHotkeys()
140 {
141  for ( unsigned i = 0; i < Lines(); ++i )
142  {
143  if ( _items[i] )
144  {
145  _items[i]->stripHotkeys();
146  }
147  }
148 }
149 
150 
151 typedef std::vector<NCTableLine *>::const_iterator NCTableLineIterator;
152 
153 int NCTablePad::findIndexById( int id ) const
154 {
155  NCTableLineIterator begin = _items.begin();
156  NCTableLineIterator end = _items.end();
157  NCTableLineIterator found = find_if( begin, end,
158  [id](NCTableLine * line)
159  {
160  return line->index() == id;
161  });
162 
163  if ( found == end )
164  return -1;
165  else
166  return found - begin;
167 }
NCursesWindow
C++ class for windows.
Definition: ncursesw.h:907
NCTablePad::DoRedraw
virtual int DoRedraw()
Redraw the pad.
Definition: NCTablePad.cc:44
NCPad::ScrlLine
int ScrlLine(int line)
Scroll to a line, keeping the column.
Definition: NCPad.h:206
NCTablePadBase::Lines
unsigned Lines() const
Return the number of table lines (logical, not screen)
Definition: NCTablePadBase.h:122
NCursesWindow::w
WINDOW * w
the curses WINDOW
Definition: ncursesw.h:949
NCTablePadBase::prepareRedraw
virtual void prepareRedraw()
Prepare a redraw: Update the format if needed, set the background, clear the old content.
Definition: NCTablePadBase.cc:261
NCWidget
Definition: NCWidget.h:46
NCTablePadBase::drawHeader
virtual void drawHeader()
Redraw the table header.
Definition: NCTablePadBase.cc:285
NCTablePad::handleInput
virtual bool handleInput(wint_t key)
Handle a keyboard input event.
Definition: NCTablePad.cc:81
NCTablePadBase::visibleLines
unsigned visibleLines() const
Return the number of lines that are currently visible.
Definition: NCTablePadBase.h:89
NCTablePadBase::currentLineNo
int currentLineNo() const
Return the current line number (the cursor position).
Definition: NCTablePadBase.h:261
NCTablePadBase::_visibleItems
std::vector< NCTableLine * > _visibleItems
not owned
Definition: NCTablePadBase.h:290
NCTablePadBase::drawContentLines
virtual void drawContentLines()
Redraw the (visible) content lines one by one.
Definition: NCTablePadBase.cc:271
NCTablePad::findIndexById
int findIndexById(int id) const
Find the item index in a sorted table.
Definition: NCTablePad.cc:153
NCTablePadBase::handleInput
virtual bool handleInput(wint_t key)
Handle a keyboard input event.
Definition: NCTablePadBase.cc:386
NCTablePadBase::_items
std::vector< NCTableLine * > _items
(owned)
Definition: NCTablePadBase.h:289
NCTableLine::index
int index() const
Return the unique index by which this line can be identified.
Definition: NCTableItem.h:137
NCPad::paging
bool paging() const
Whether the Pad is truncated (we're paging).
Definition: NCPad.h:149
NCTableLine
One line in a NCTable with multiple cells and an optional tree hierarchy.
Definition: NCTableItem.h:68
wrect
A rectangle is defined by its position and size: wpos Pos, wsze Sze.
Definition: position.h:194
NCTablePadBase
An NCPad for an NCTable or an NCTree.
Definition: NCTablePadBase.h:59
NCTablePadBase::Cols
unsigned Cols() const
Return the number of table columns (logical, not screen)
Definition: NCTablePadBase.h:117
NCTablePad::directDraw
virtual void directDraw(NCursesWindow &w, const wrect at, unsigned lineno)
Directly draw a table item at a specific location.
Definition: NCTablePad.cc:67