libyui-ncurses  2.44.1
 All Classes Functions Variables
NCPopupInfo.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: NCPopupInfo.cc
20 
21  Author: Gabriele Strattner <gs@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCPopupInfo.h"
28 
29 #include "NCTree.h"
30 #include <yui/YMenuButton.h>
31 #include <yui/YDialog.h>
32 #include "NCLayoutBox.h"
33 #include "NCSpacing.h"
34 
35 
36 namespace
37 {
38  const std::string idOk( "ok" );
39  const std::string idCancel( "cancel" );
40 }
41 
42 
43 NCPopupInfo::NCPopupInfo( const wpos at,
44  const std::string & headline,
45  const std::string & text,
46  std::string okButtonLabel,
47  std::string cancelButtonLabel )
48  : NCPopup( at, false )
49  , helpText( 0 )
50  , okButton( 0 )
51  , cancelButton( 0 )
52  , hDim( 50 )
53  , vDim( 20 )
54  , visible( false )
55 {
56  createLayout( headline, text, okButtonLabel, cancelButtonLabel );
57 }
58 
59 
60 NCPopupInfo::~NCPopupInfo()
61 {
62 }
63 
64 
65 void NCPopupInfo::createLayout( const std::string & headline,
66  const std::string & text,
67  std::string okButtonLabel,
68  std::string cancelButtonLabel )
69 {
70  // the vertical split is the (only) child of the dialog
71  NCLayoutBox * split = new NCLayoutBox( this, YD_VERT );
72 
73  // add the headline
74  new NCLabel( split, headline, true, false ); // isHeading = true
75 
76  // add the rich text widget
77  helpText = new NCRichText( split, text );
78 
79  NCLayoutBox * hSplit = new NCLayoutBox( split, YD_HORIZ );
80 
81  if ( okButtonLabel != "" && cancelButtonLabel != "" )
82  {
83  new NCSpacing( hSplit, YD_HORIZ, true, 0.4 ); // stretchable = true
84  }
85 
86  if ( okButtonLabel != "" )
87  {
88  // add the OK button
89  okButton = new NCPushButton( hSplit, okButtonLabel );
90  okButton->setFunctionKey( 10 );
91  }
92 
93  if ( cancelButtonLabel != "" )
94  {
95  new NCSpacing( hSplit, YD_HORIZ, true, 0.4 );
96 
97  // add the Cancel button
98  cancelButton = new NCPushButton( hSplit, cancelButtonLabel );
99  cancelButton->setFunctionKey( 9 );
100 
101  new NCSpacing( hSplit, YD_HORIZ, true, 0.4 );
102  }
103 
104  //If we don't have cancel button and have single ok button instead
105  //let's focus it by default (#397393)
106  if ( cancelButtonLabel == "" && okButton )
107  focusOkButton();
108 
109  //the same with missing ok button and single cancel button
110  if ( okButtonLabel == "" && cancelButton )
111  focusCancelButton();
112 }
113 
114 
115 NCursesEvent & NCPopupInfo::showInfoPopup( )
116 {
117  postevent = NCursesEvent();
118 
119  do
120  {
121  popupDialog( );
122  }
123  while ( postAgain() );
124 
125  popdownDialog();
126 
127  return postevent;
128 }
129 
130 
131 void NCPopupInfo::popup()
132 {
133  initDialog();
134  showDialog();
135  activate( true );
136  visible = true;
137 }
138 
139 
140 void NCPopupInfo::popdown()
141 {
142  activate( false );
143  closeDialog();
144  visible = false;
145 }
146 
147 
148 int NCPopupInfo::preferredWidth()
149 {
150  int horDim = hDim;
151 
152  if ( hDim >= NCurses::cols() )
153  horDim = NCurses::cols() - 10;
154 
155  return horDim;
156 }
157 
158 
159 int NCPopupInfo::preferredHeight()
160 {
161  int vertDim = vDim;
162 
163  if ( vDim >= NCurses::lines() )
164  vertDim = NCurses::lines() - 5;
165 
166  return vertDim;
167 }
168 
169 
171 NCPopupInfo::wHandleInput( wint_t ch )
172 {
173  if ( ch == 27 ) // ESC
174  return NCursesEvent::cancel;
175 
176  if ( ch == KEY_RETURN )
177  return NCursesEvent::button;
178 
179  return NCDialog::wHandleInput( ch );
180 }
181 
182 
183 bool NCPopupInfo::postAgain()
184 {
185  if ( ! postevent.widget )
186  return false;
187 
188  if ( okButton && cancelButton )
189  {
190  if ( postevent.widget == cancelButton )
191  {
192  yuiMilestone() << "Cancel button pressed" << std::endl;
193  // close the dialog
194  postevent = NCursesEvent::cancel;
195  }
196 
197  // else - nothing to do (postevent is already set)
198  }
199 
200  if ( postevent == NCursesEvent::button || postevent == NCursesEvent::cancel )
201  {
202  // return false means: close the popup dialog
203  return false;
204  }
205 
206  return true;
207 }
208 
209 
virtual void activate()
Definition: NCDialog.cc:312
Definition: position.h:109