libyui-ncurses  2.44.1
 All Classes Functions Variables
NCAskForDirectory.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: NCAskForExistingDirectory.cc
20 
21  Author: Gabriele Strattner <gs@suse.de>
22 
23 /-*/
24 
25 #include "NCAskForDirectory.h"
26 
27 
28 #define YUILogComponent "ncurses"
29 #include <yui/YUILog.h>
30 #include <yui/YDialog.h>
31 #include <yui/YTypes.h>
32 #include "NCWidgetFactory.h"
33 #include "NCWidgetFactory.h"
34 #include "NCLayoutBox.h"
35 #include "NCSpacing.h"
36 #include "NCFrame.h"
37 #include "NCi18n.h"
38 
39 #include <sys/types.h>
40 #include <unistd.h>
41 #include <dirent.h>
42 #include <sys/errno.h>
43 
44 /*
45  Textdomain "ncurses"
46 */
47 
48 
49 
50 NCAskForExistingDirectory::NCAskForExistingDirectory( const wpos at,
51  const std::string & iniDir,
52  const std::string & headline )
53  : NCPopup( at, true )
54  , okButton( 0 )
55  , cancelButton( 0 )
56  , dirName( 0 )
57  , dirList( 0 )
58  , detailed( 0 )
59 {
60  createLayout( iniDir, headline );
61 }
62 
63 
64 NCAskForExistingDirectory::~NCAskForExistingDirectory()
65 {
66 }
67 
68 
69 void NCAskForExistingDirectory::createLayout( const std::string & iniDir,
70  const std::string & headline )
71 {
72  std::string old_textdomain = textdomain( NULL );
73  setTextdomain( "ncurses" );
74 
75  // the vertical split is the (only) child of the dialog
76  YLayoutBox * split = YUI::widgetFactory()->createVBox( this );
77 
78  // the headline
79  new NCLabel( split, headline, true, false ); // isHeading = true
80 
81  YFrame * frame = YUI::widgetFactory()->createFrame( split, "" );
82 
83  // label for text field showing the selected dir
84  dirName = new NCComboBox( frame, _( "Selected Directory:" ), false ); // editable = false
85  dirName->setNotify( true );
86  dirName->setStretchable( YD_HORIZ, true );
87 
88  // add the checkBox detailed
89  YLayoutBox * hSplit = YUI::widgetFactory()->createHBox( split );
90 
91  // label for checkbox
92  detailed = new NCCheckBox( hSplit, _( "&Detailed View" ), false );
93  detailed->setNotify( true );
94 
95  // create table header for table type T_Overview
96  YTableHeader * tableHeader = new YTableHeader();
97  tableHeader->addColumn( " ", YAlignBegin );
98  tableHeader->addColumn( _( "Directory Name" ), YAlignBegin );
99 
100  // add the list of directories
101  dirList = new NCDirectoryTable( split,
102  tableHeader,
103  NCFileTable::T_Overview,
104  iniDir );
105 
106  YUI::widgetFactory()->createSpacing( split, YD_VERT, false, 1.0 );
107 
108  // HBox for the buttons
109  YLayoutBox * hSplit1 = YUI::widgetFactory()->createHBox( split );
110 
111  YUI::widgetFactory()->createSpacing( hSplit1, YD_HORIZ, true, 0.2 ); // stretchable = true
112 
113  // add the OK button
114  okButton = new NCPushButton( hSplit1, _( "&OK" ) );
115  okButton->setFunctionKey( 10 );
116  okButton->setStretchable( YD_HORIZ, true );
117 
118  YUI::widgetFactory()->createSpacing( hSplit1, YD_HORIZ, true, 0.4 );
119 
120  // add the Cancel button
121  cancelButton = new NCPushButton( hSplit1, _( "&Cancel" ) );
122  cancelButton->setFunctionKey( 9 );
123  cancelButton->setStretchable( YD_HORIZ, true );
124 
125  YUI::widgetFactory()->createSpacing( hSplit1, YD_HORIZ, true, 0.2 );
126  // restore former text domain
127  setTextdomain( old_textdomain.c_str() );
128 }
129 
130 
132 {
133  postevent = NCursesEvent();
134 
135  if ( !dirList || !dirName )
136  return postevent;
137 
138  dirList->fillList( );
139 
140  dirList->setKeyboardFocus();
141 
142  dirName->addItem( dirList->getCurrentDir(),
143  true ); // selected
144 
145  // event loop
146  do
147  {
148  popupDialog();
149  }
150  while ( postAgain() );
151 
152  popdownDialog();
153 
154 
155  return postevent;
156 }
157 
158 
159 int NCAskForExistingDirectory::preferredWidth()
160 {
161  return NCurses::cols() - 10;
162 }
163 
164 
165 int NCAskForExistingDirectory::preferredHeight()
166 {
167  return NCurses::lines() - 4;
168 }
169 
170 
171 NCursesEvent NCAskForExistingDirectory::wHandleInput( wint_t ch )
172 {
173  if ( ch == 27 ) // ESC
174  return NCursesEvent::cancel;
175 
176  return NCDialog::wHandleInput( ch );
177 }
178 
179 
180 bool NCAskForExistingDirectory::postAgain( )
181 {
182  if ( !postevent.widget )
183  return false;
184 
185  postevent.detail = NCursesEvent::NODETAIL;
186 
187  if ( postevent.widget == okButton )
188  {
189  postevent.result = dirList->getCurrentDir();
190  // return false means: close the popup
191  return false;
192  }
193  else if ( postevent.widget == dirList )
194  {
195  if ( postevent.result == "" )
196  return true;
197 
198  // show the currently selected directory
199  yuiDebug() << "Add item: " << postevent.result << std::endl;
200 
201  dirName->addItem( postevent.result,
202  true );
203 
204  if ( postevent.reason == YEvent::Activated )
205  {
206  // fill the directory list
207  dirList->fillList();
208  }
209  }
210  else if ( postevent.widget == dirName )
211  {
212  dirList->setStartDir( dirName->value() );
213  dirList->fillList();
214  }
215  else if ( postevent.widget == detailed )
216  {
217  bool details = getCheckBoxValue( detailed );
218 
219  if ( details )
220  {
221  dirList->setTableType( NCFileTable::T_Detailed );
222  }
223  else
224  {
225  dirList->setTableType( NCFileTable::T_Overview );
226  }
227 
228  dirList->fillList();
229  }
230  else
231  {
232  postevent.result = "";
233  return false;
234  }
235 
236  if ( postevent.widget == cancelButton ||
237  postevent == NCursesEvent::cancel )
238  {
239  postevent.result = "";
240  return false;
241  }
242 
243  return true;
244 }
245 
246 
247 bool NCAskForExistingDirectory::getCheckBoxValue( NCCheckBox * checkBox )
248 {
249  if ( checkBox )
250  {
251  // return whether the option is selected or not
252  return ( checkBox->isChecked() );
253  }
254 
255  return false;
256 }
Definition: position.h:109
void setStartDir(const std::string &start)
void createLayout(const std::string &initialDir, const std::string &headline)
void setTableType(NCFileSelectionType type)
std::string getCurrentDir()
virtual bool fillList()