libyui-ncurses  2.48.3
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  std::string old_textdomain = textdomain( NULL );
71  setTextdomain( "ncurses" );
72 
73  // the vertical split is the (only) child of the dialog
74  NCLayoutBox * split = new NCLayoutBox( this, YD_VERT );
75 
76  // add the headline
77  new NCLabel( split, headline, true, false ); // isHeading = true
78 
79  // add the rich text widget
80  helpText = new NCRichText( split, text );
81 
82  NCLayoutBox * hSplit = new NCLayoutBox( split, YD_HORIZ );
83 
84  if ( okButtonLabel != "" && cancelButtonLabel != "" )
85  {
86  new NCSpacing( hSplit, YD_HORIZ, true, 0.4 ); // stretchable = true
87  }
88 
89  if ( okButtonLabel != "" )
90  {
91  // add the OK button
92  okButton = new NCPushButton( hSplit, okButtonLabel );
93  okButton->setFunctionKey( 10 );
94  }
95 
96  if ( cancelButtonLabel != "" )
97  {
98  new NCSpacing( hSplit, YD_HORIZ, true, 0.4 );
99 
100  // add the Cancel button
101  cancelButton = new NCPushButton( hSplit, cancelButtonLabel );
102  cancelButton->setFunctionKey( 9 );
103 
104  new NCSpacing( hSplit, YD_HORIZ, true, 0.4 );
105  }
106 
107  //If we don't have cancel button and have single ok button instead
108  //let's focus it by default (#397393)
109  if ( cancelButtonLabel == "" && okButton )
110  focusOkButton();
111 
112  //the same with missing ok button and single cancel button
113  if ( okButtonLabel == "" && cancelButton )
114  focusCancelButton();
115 
116  // restore former text domain
117  setTextdomain( old_textdomain.c_str() );
118 }
119 
120 
121 NCursesEvent & NCPopupInfo::showInfoPopup( )
122 {
123  postevent = NCursesEvent();
124 
125  do
126  {
127  popupDialog( );
128  }
129  while ( postAgain() );
130 
131  popdownDialog();
132 
133  return postevent;
134 }
135 
136 
137 void NCPopupInfo::popup()
138 {
139  initDialog();
140  showDialog();
141  activate( true );
142  visible = true;
143 }
144 
145 
146 void NCPopupInfo::popdown()
147 {
148  activate( false );
149  closeDialog();
150  visible = false;
151 }
152 
153 
154 int NCPopupInfo::preferredWidth()
155 {
156  int horDim = hDim;
157 
158  if ( hDim >= NCurses::cols() )
159  horDim = NCurses::cols() - 10;
160 
161  return horDim;
162 }
163 
164 
165 int NCPopupInfo::preferredHeight()
166 {
167  int vertDim = vDim;
168 
169  if ( vDim >= NCurses::lines() )
170  vertDim = NCurses::lines() - 5;
171 
172  return vertDim;
173 }
174 
175 
177 NCPopupInfo::wHandleInput( wint_t ch )
178 {
179  if ( ch == 27 ) // ESC
180  return NCursesEvent::cancel;
181 
182  if ( ch == KEY_RETURN )
183  return NCursesEvent::button;
184 
185  return NCDialog::wHandleInput( ch );
186 }
187 
188 
189 bool NCPopupInfo::postAgain()
190 {
191  if ( ! postevent.widget )
192  return false;
193 
194  if ( okButton && cancelButton )
195  {
196  if ( postevent.widget == cancelButton )
197  {
198  yuiMilestone() << "Cancel button pressed" << std::endl;
199  // close the dialog
200  postevent = NCursesEvent::cancel;
201  }
202 
203  // else - nothing to do (postevent is already set)
204  }
205 
206  if ( postevent == NCursesEvent::button || postevent == NCursesEvent::cancel )
207  {
208  // return false means: close the popup dialog
209  return false;
210  }
211 
212  return true;
213 }
214 
215 
virtual void activate()
Activate this dialog: Make sure that it is shown as the topmost dialog of this application and that i...
Definition: NCDialog.cc:307
Definition: position.h:109