libyui-ncurses  2.55.0
NCCustomStatusItemSelector.cc
1 /*
2  Copyright (C) 2019 SUSE LLC
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: NCCustomStatusItemSelector.cc
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ncurses"
27 #include <yui/YUILog.h>
28 #include "NCLabel.h"
29 #include "NCCustomStatusItemSelector.h"
30 
31 using std::string;
32 
33 
35  const YItemCustomStatusVector & customStates )
36  : NCItemSelectorBase( parent, customStates )
37 {
38  yuiDebug() << endl;
39 }
40 
41 
43 {
44  yuiDebug() << endl;
45 }
46 
47 
49 {
50  NCTableTag * tag = new NCCustomStatusTableTag( this, item );
51  YUI_CHECK_NEW( tag );
52 
53  return tag;
54 }
55 
56 
59 {
60  NCTableTag * tag = NCItemSelectorBase::tagCell( index );
61 
62  return tag ? dynamic_cast<NCCustomStatusTableTag *>( tag ) : 0;
63 }
64 
65 
67 {
68  if ( ! item )
69  return;
70 
71  // yuiDebug() << "Updating status indicator for \"" << item->label() << "\"" << endl;
72  NCCustomStatusTableTag * tag = (NCCustomStatusTableTag *) item->data();
73  YUI_CHECK_PTR( tag );
74 
75  tag->updateStatusIndicator();
76  DrawPad();
77 }
78 
79 
81 {
82  YItem *item = currentItem();
83 
84  if ( item )
85  {
86  int oldStatus = item->status();
87  int newStatus = customStatus( oldStatus ).nextStatus();
88 
89  yuiDebug() << "Cycling status of item \""
90  << item->label() << "\": "
91  << oldStatus << " -> " << newStatus
92  << endl;
93 
94  if ( newStatus != -1 && oldStatus != newStatus )
95  {
96  item->setStatus( newStatus );
98  }
99  }
100 }
101 
102 
103 bool NCCustomStatusItemSelector::statusChangeAllowed( int fromStatus, int toStatus )
104 {
105  if ( fromStatus == toStatus ) // No use setting to the same status as before
106  return false;
107 
108  if ( ! validCustomStatusIndex( fromStatus ) || ! validCustomStatusIndex( toStatus ) )
109  return false;
110 
111  return customStatus( fromStatus ).nextStatus() == toStatus;
112 }
113 
114 
117 {
118  NCursesEvent event( NCursesEvent::menu );
119  event.selection = (YMenuItem *) item;
120 
121  yuiDebug() << "Sending MenuEvent for item \"" << item->label() << "\"" << endl;
122 
123  return event;
124 }
125 
126 
127 // ----------------------------------------------------------------------
128 
129 
130 NCCustomStatusTableTag::NCCustomStatusTableTag( YItemSelector * parentSelector, YItem * item )
131  : NCTableTag( item )
132  , _parentSelector( parentSelector )
133 {
134  YUI_CHECK_PTR( _parentSelector );
135  updateStatusIndicator();
136 }
137 
138 
140 {
141  YItem * item = origItem();
142 
143  if ( item )
144  {
145  string statusText = _parentSelector->customStatus( item->status() ).textIndicator();
146 
147  // Since the parent class overwrote SetLabel() to do nothing,
148  // we need to go one class up the class hierarchy to set the text.
149  NCTableCol::SetLabel( NCstring( statusText ) );
150  }
151 }
152 
153 
154 void NCCustomStatusTableTag::DrawAt( NCursesWindow & w,
155  const wrect at,
156  NCTableStyle & tableStyle,
157  NCTableLine::STATE linestate,
158  unsigned colidx ) const
159 {
160  // Undo the parent class's overwriting DrawAt():
161  // Call the next superclass in the class hierarchy
162  NCTableCol::DrawAt( w, at, tableStyle, linestate, colidx );
163 }
164 
165 
167 {
168  YItem * item = origItem();
169 
170  return item ? item->status() : 0;
171 }
172 
173 
175 {
176  YItem * item = origItem();
177 
178  if ( item )
179  {
180  item->setStatus( newStatus );
182  }
183 }
184 
185 
186 void NCCustomStatusTableTag::SetSelected( bool sel )
187 {
188  setStatus( sel ? 1 : 0 );
189 }
190 
191 
192 bool NCCustomStatusTableTag::Selected() const
193 {
194  return status() != 0;
195 }
196 
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
virtual void cycleCurrentItemStatus()
Cycle the status of the current item through its possible values.
virtual NCCustomStatusTableTag * tagCell(int index) const
Return the tag cell (the cell with the "[x]" or "(x)" selector) for the item with the specified index...
NCCustomStatusItemSelector(YWidget *parent, const YItemCustomStatusVector &customStates)
Constructor.
virtual void updateCustomStatusIndicator(YItem *item)
Update the status indicator.
virtual NCursesEvent valueChangedNotify(YItem *item)
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
virtual NCTableTag * createTagCell(YItem *item)
Create a tag cell for an item.
virtual ~NCCustomStatusItemSelector()
Destructor.
Specialized subclass of NCTableTag that can not only handle a boolean "selected" flag (and accordingl...
void updateStatusIndicator()
Update the status indicator according to the status of the associated item, i.e.
void setStatus(int newStatus)
Set the numeric status value of the associated item and update the status indicator.
int status() const
Return the numeric status value of the associated item.
virtual NCTableTag * tagCell(int index) const
Return the tag cell (the cell with the "[x]" or "(x)" selector) for the item with the specified index...
virtual YItem * currentItem() const
Return the current item, i.e.
C++ class for windows.
Definition: ncursesw.h:904