libyui-ncurses  2.44.1
 All Classes Functions Variables
NCTablePad.cc
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: NCTablePad.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCTablePad.h"
28 #include "NCPopupMenu.h"
29 
30 #include <limits.h>
31 
32 
33 NCTablePad::NCTablePad( int lines, int cols, const NCWidget & p )
34  : NCPad( lines, cols, p )
35  , Headpad( 1, 1 )
36  , dirtyHead( false )
37  , dirtyFormat( false )
38  , ItemStyle( p )
39  , Headline( 0 )
40  , Items( 0 )
41  , citem( 0 )
42  , sortStrategy ( new NCTableSortDefault )
43 {
44 }
45 
46 
47 
48 NCTablePad::~NCTablePad()
49 {
50  ClearTable();
51 }
52 
53 
54 
55 void NCTablePad::assertLine( unsigned idx )
56 {
57  if ( idx >= Lines() )
58  SetLines( idx + 1 );
59 }
60 
61 
62 
63 void NCTablePad::SetLines( unsigned idx )
64 {
65  if ( idx == Lines() )
66  return;
67 
68  unsigned olines = Lines();
69 
70  if ( idx < Lines() )
71  {
72  for ( unsigned i = idx; i < Lines(); ++i )
73  {
74  delete Items[i];
75  }
76  }
77 
78  Items.resize( idx, 0 );
79 
80  for ( unsigned i = olines; i < Lines(); ++i )
81  {
82  if ( !Items[i] )
83  Items[i] = new NCTableLine( 0 );
84  }
85 
86  DirtyFormat();
87 }
88 
89 
90 
91 void NCTablePad::SetLines( std::vector<NCTableLine*> & nItems )
92 {
93  SetLines( 0 );
94  Items = nItems;
95 
96  for ( unsigned i = 0; i < Lines(); ++i )
97  {
98  if ( !Items[i] )
99  Items[i] = new NCTableLine( 0 );
100  }
101 
102  DirtyFormat();
103 }
104 
105 
106 
107 void NCTablePad::AddLine( unsigned idx, NCTableLine * item )
108 {
109  assertLine( idx );
110  delete Items[idx];
111  Items[idx] = item ? item : new NCTableLine( 0 );
112 
113  DirtyFormat();
114 }
115 
116 
117 
118 void NCTablePad::DelLine( unsigned idx )
119 {
120  if ( idx < Lines() )
121  {
122  Items[idx]->ClearLine();
123  DirtyFormat();
124  }
125 }
126 
127 
128 
129 const NCTableLine * NCTablePad::GetLine( unsigned idx ) const
130 {
131  if ( idx < Lines() )
132  return Items[idx];
133 
134  return 0;
135 }
136 
137 
138 
139 NCTableLine * NCTablePad::ModifyLine( unsigned idx )
140 {
141  if ( idx < Lines() )
142  {
143  DirtyFormat();
144  return Items[idx];
145  }
146 
147  return 0;
148 }
149 
150 
151 
152 bool NCTablePad::SetHeadline( const std::vector<NCstring> & head )
153 {
154  bool hascontent = ItemStyle.SetStyleFrom( head );
155  DirtyFormat();
156  update();
157  return hascontent;
158 }
159 
160 
161 
162 void NCTablePad::wRecoded()
163 {
164  DirtyFormat();
165  update();
166 }
167 
168 
169 
170 wpos NCTablePad::CurPos() const
171 {
172  citem.C = srect.Pos.C;
173  return citem;
174 }
175 
176 
177 
178 wsze NCTablePad::UpdateFormat()
179 {
180  yuiDebug() << std::endl;
181  dirty = true;
182  dirtyFormat = false;
183  ItemStyle.ResetToMinCols();
184 
185  for ( unsigned l = 0; l < Lines(); ++l )
186  {
187  Items[l]->UpdateFormat( ItemStyle );
188  }
189 
190  resize( wsze( Lines(), ItemStyle.TableWidth() ) );
191 
192  return wsze( Lines(), ItemStyle.TableWidth() );
193 }
194 
195 
196 
197 int NCTablePad::DoRedraw()
198 {
199  if ( !Destwin() )
200  {
201  dirty = true;
202  return OK;
203  }
204 
205  yuiDebug() << "dirtyFormat " << dirtyFormat << std::endl;
206 
207  if ( dirtyFormat )
208  UpdateFormat();
209 
210  bkgdset( ItemStyle.getBG() );
211 
212  clear();
213 
214  wsze lSze( 1, width() );
215 
216  if ( ! pageing() )
217  {
218  for ( unsigned l = 0; l < Lines(); ++l )
219  {
220  Items[l]->DrawAt( *this, wrect( wpos( l, 0 ), lSze ),
221  ItemStyle, (( unsigned )citem.L == l ) );
222  }
223  }
224  // else: item drawing requested via directDraw
225 
226  if ( Headpad.width() != width() )
227  Headpad.resize( 1, width() );
228 
229  Headpad.clear();
230 
231  ItemStyle.Headline().DrawAt( Headpad, wrect( wpos( 0, 0 ), lSze ),
232  ItemStyle, false );
233 
234  SendHead();
235 
236  dirty = false;
237 
238  return update();
239 }
240 
241 
242 
243 void NCTablePad::directDraw( NCursesWindow & w, const wrect at, unsigned lineno )
244 {
245  if ( lineno < Lines() )
246  Items[lineno]->DrawAt( w, at, ItemStyle, ((unsigned)citem.L == lineno) );
247  else
248  yuiWarning() << "Illegal Lineno " << lineno << " (" << Lines() << ")" << std::endl;
249 }
250 
251 
252 
253 int NCTablePad::setpos( const wpos & newpos )
254 {
255  if ( !Lines() )
256  {
257  if ( dirty || dirtyFormat )
258  return DoRedraw();
259 
260  return OK;
261  }
262 
263  yuiDebug() << newpos << " : l " << Lines() << " : cl " << citem.L
264 
265  << " : d " << dirty << " : df " << dirtyFormat << std::endl;
266 
267  if ( dirtyFormat )
268  UpdateFormat();
269 
270  // save old values
271  int oitem = citem.L;
272 
273  int opos = srect.Pos.C;
274 
275  // calc new values
276  citem.L = newpos.L < 0 ? 0 : newpos.L;
277 
278  if (( unsigned )citem.L >= Lines() )
279  citem.L = Lines() - 1;
280 
281  srect.Pos = wpos( citem.L - ( drect.Sze.H - 1 ) / 2, newpos.C ).between( 0, maxspos );
282 
283  if ( dirty )
284  {
285  return DoRedraw();
286  }
287 
288  if ( ! pageing() )
289  {
290  // adjust only
291  if ( citem.L != oitem )
292  {
293  Items[oitem]->DrawAt( *this, wrect( wpos( oitem, 0 ), wsze( 1, width() ) ),
294  ItemStyle, false );
295  }
296 
297  Items[citem.L]->DrawAt( *this, wrect( wpos( citem.L, 0 ), wsze( 1, width() ) ),
298 
299  ItemStyle, true );
300  }
301  // else: item drawing requested via directDraw
302 
303  if ( srect.Pos.C != opos )
304  SendHead();
305 
306  return update();
307 }
308 
309 
310 
311 void NCTablePad::updateScrollHint()
312 {
313  NCPad::updateScrollHint();
314 }
315 
316 
317 
318 bool NCTablePad::setItemByKey( int key )
319 {
320  if ( HotCol() >= Cols() )
321  return false;
322 
323  if ( key < 0 || UCHAR_MAX < key )
324  return false;
325 
326  unsigned hcol = HotCol();
327 
328  unsigned hkey = tolower( key );
329 
330  for ( unsigned l = 0; l < Lines(); ++l )
331  {
332  if ( Items[l]->GetCol( hcol )->hasHotkey()
333  &&
334  ( unsigned )tolower( Items[l]->GetCol( hcol )->hotkey() ) == hkey )
335  {
336  ScrlLine( l );
337  return true;
338  }
339  }
340 
341  return false;
342 }
343 
344 //
345 // setOrder() sorts the table according to given column by calling
346 // the sort startegy. Sorting in reverse order is only done
347 // if 'do_reverse' is set to 'true'.
348 //
349 void NCTablePad::setOrder( int col, bool do_reverse )
350 {
351  if ( col < 0 )
352  return;
353 
354  if ( sortStrategy->getColumn() == col && do_reverse )
355  {
356  std::reverse( Items.begin(), Items.end() );
357  }
358  else
359  {
360  sortStrategy->setColumn( col );
361  sortStrategy->sort( Items.begin(), Items.end(), col );
362  }
363 
364  dirty = true;
365  update();
366 }
367 
368 
369 
370 bool NCTablePad::handleInput( wint_t key )
371 {
372  return NCPad::handleInput( key );
373 }
374 
375 void NCTablePad::stripHotkeys()
376 {
377  for ( unsigned i = 0; i < Lines(); ++i )
378  {
379  if ( Items[i] )
380  {
381  Items[i]->stripHotkeys();
382  }
383  }
384 }
385 
386 
387 std::ostream & operator<<( std::ostream & STREAM, const NCTablePad & OBJ )
388 {
389  STREAM << "TablePad: lines " << OBJ.Lines() << std::endl;
390 
391  for ( unsigned idx = 0; idx < OBJ.Lines(); ++idx )
392  {
393  STREAM << idx << " " << *OBJ.GetLine( idx );
394  }
395 
396  return STREAM;
397 }
398 
C++ class for windows.
Definition: ncursesw.h:904
void bkgdset(chtype ch)
Definition: ncursesw.h:1448
Definition: NCPad.h:93
Definition: position.h:109
virtual void directDraw(NCursesWindow &w, const wrect at, unsigned lineno)
Definition: NCTablePad.cc:243
Definition: position.h:154
int width() const
Definition: ncursesw.h:1075
bool pageing() const
Definition: NCPad.h:129