libyui-ncurses  2.55.0
NCLabel.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: NCLabel.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCurses.h"
28 #include "NCLabel.h"
29 
30 #define AUTO_WRAP_WIDTH 10
31 #define AUTO_WRAP_HEIGHT 1
32 
33 using std::string;
34 
35 
36 NCLabel::NCLabel( YWidget * parent,
37  const string & nlabel,
38  bool isHeading,
39  bool isOutputField )
40  : YLabel( parent, nlabel, isHeading, isOutputField )
41  , NCWidget( parent )
42  , heading( isHeading )
43 {
44  yuiDebug() << std::endl;
45  setText( nlabel );
46  hotlabel = &label;
47  wstate = NC::WSdumb;
48 }
49 
50 
51 NCLabel::~NCLabel()
52 {
53  yuiDebug() << std::endl;
54 }
55 
56 
57 int NCLabel::preferredWidth()
58 {
59  int width;
60 
61  if ( autoWrap() )
62  {
63  if ( layoutPass() == 2 )
64  {
65  // Use the width passed down to us from the parent layout manager
66  // in the last setSize() call. This is the definitive width that
67  // will be used after taking all other children of the layout into
68  // consideration, also including making widgets smaller due to size
69  // restrictions, or redistributing excess space.
70  //
71  // Since this widget can auto-wrap its contents, we accept
72  // basically any width; we just need to adapt the preferred height
73  // accordingly.
74  width = wrapper.lineWidth();
75  }
76  else
77  {
78  // Use a preliminary width. Typically, this widget should be
79  // wrapped into a MinSize or MinWidth which hopefully gives us a
80  // much more useful width than this.
81  //
82  // We would also just use 0 here, but that would make debugging
83  // really hard since the widget might completly disappear.
84  //
85  // The real width that will be used will be set in the setSize()
86  // call following the recursive preferredWidth() /
87  // preferredHeight() calls in the widget tree.
88  width = AUTO_WRAP_WIDTH;
89  }
90  }
91  else // ! autoWrap()
92  {
93  width = wGetDefsze().W;
94  }
95 
96  return width;
97 }
98 
99 
100 int NCLabel::preferredHeight()
101 {
102  int height;
103 
104  if ( autoWrap() )
105  {
106  if ( layoutPass() == 2 )
107  {
108  // This is where the magic happens:
109  //
110  // setSize() in the first layout pass gave us the real width which
111  // we stored in as the wrapper's lineWidth. We can now let the
112  // wrapper wrap the text into that width and calculate the height
113  // (the number of lines of the wrapped text) that is really needed
114  // based on that width.
115  height = wrapper.lines();
116  label = NCstring( wrapper.wrappedText() );
117  }
118  else
119  {
120  height = AUTO_WRAP_HEIGHT;
121  }
122  }
123  else // ! autoWrap()
124  {
125  height = wGetDefsze().H;
126  }
127 
128  return height;
129 }
130 
131 
132 void NCLabel::setEnabled( bool do_bv )
133 {
134  NCWidget::setEnabled( do_bv );
135  YLabel::setEnabled( do_bv );
136 }
137 
138 
139 void NCLabel::setSize( int newWidth, int newHeight )
140 {
141  if ( autoWrap() && layoutPass() == 1 )
142  wrapper.setLineWidth( newWidth );
143 
144  wRelocate( wpos( 0 ), wsze( newHeight, newWidth ) );
145 }
146 
147 
148 void NCLabel::setText( const string & newLabel )
149 {
150  label = NCstring( newLabel );
151  defsze = label.size();
152  YLabel::setText( newLabel );
153  Redraw();
154 }
155 
156 
157 void NCLabel::wRedraw()
158 {
159  if ( !win )
160  return;
161 
162  chtype bg = heading ? wStyle().dumb.title
163  : wStyle().dumb.text;
164 
165  win->bkgd( bg );
166  win->clear();
167 
168  if ( autoWrap() )
169  label = NCstring( wrapper.wrappedText() );
170 
171  label.drawAt( *win, bg, bg );
172 }
173 
174 
175 void NCLabel::setAutoWrap( bool autoWrap )
176 {
177  YLabel::setAutoWrap( autoWrap );
178 
179  if ( autoWrap )
180  {
181  wrapper.setText( NCstring( text() ).str() );
182  // Delay setting 'label' until the line width for wrapping is set
183  }
184  else
185  {
186  // This will probably never happen since the default for autoWrap is
187  // 'false' and nobody will ever set an auto-wrapping label back to
188  // non-autowrapping programatically.
189  //
190  // But anyway, just in case: Let's revert the values to the initial
191  // defaults.
192 
193  label = NCstring( text() );
194  defsze = label.size();
195  wrapper.clear();
196  }
197 }
198 
wsze
Definition: position.h:155
NCstring
Definition: NCstring.h:33
NCWordWrapper::setLineWidth
void setLineWidth(int width)
Set the maximum line width to wrap into.
Definition: NCWordWrapper.cc:58
NCWidget::setEnabled
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
NCursesWindow::clear
int clear()
Clear the window.
Definition: ncursesw.h:1521
NCWidget
Definition: NCWidget.h:46
wpos
Definition: position.h:110
NCWordWrapper::setText
void setText(const std::wstring &origText)
Set the original text to wrap.
Definition: NCWordWrapper.cc:48
NCursesWindow::bkgd
int bkgd(const chtype ch)
Set the background property and apply it to the window.
Definition: ncursesw.h:1442
NCWordWrapper::wrappedText
const std::wstring & wrappedText()
Wrap the original text and return the wrapped text.
Definition: NCWordWrapper.cc:86
NCLabel::setEnabled
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCLabel.cc:132
NCWordWrapper::clear
void clear()
Clear the old content.
Definition: NCWordWrapper.cc:68
NCWordWrapper::lineWidth
int lineWidth() const
Return the last used maximum line width.
Definition: NCWordWrapper.h:75
NCWordWrapper::lines
int lines()
Return the number of lines after wrapping the original text.
Definition: NCWordWrapper.cc:78