libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCAskForDirectory.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       NCAskForExistingDirectory.cc
00020 
00021    Author:     Gabriele Strattner <gs@suse.de>
00022 
00023 /-*/
00024 
00025 #include "NCAskForDirectory.h"
00026 
00027 
00028 #define  YUILogComponent "ncurses"
00029 #include <yui/YUILog.h>
00030 #include <yui/YDialog.h>
00031 #include <yui/YTypes.h>
00032 #include "NCWidgetFactory.h"
00033 #include "NCWidgetFactory.h"
00034 #include "NCLayoutBox.h"
00035 #include "NCSpacing.h"
00036 #include "NCFrame.h"
00037 #include "NCi18n.h"
00038 
00039 #include <sys/types.h>
00040 #include <unistd.h>
00041 #include <dirent.h>
00042 #include <sys/errno.h>
00043 
00044 /*
00045   Textdomain "ncurses"
00046 */
00047 
00048 
00049 
00050 NCAskForExistingDirectory::NCAskForExistingDirectory( const wpos at,
00051                                                       const std::string & iniDir,
00052                                                       const std::string & headline )
00053     : NCPopup( at, true )
00054     , okButton( 0 )
00055     , cancelButton( 0 )
00056     , dirName( 0 )
00057     , dirList( 0 )
00058     , detailed( 0 )
00059 {
00060     createLayout( iniDir, headline );
00061 }
00062 
00063 
00064 NCAskForExistingDirectory::~NCAskForExistingDirectory()
00065 {
00066 }
00067 
00068 
00069 void NCAskForExistingDirectory::createLayout( const std::string & iniDir,
00070                                               const std::string & headline )
00071 {
00072     std::string old_textdomain = textdomain( NULL );
00073     setTextdomain( "ncurses" );
00074 
00075     // the vertical split is the (only) child of the dialog
00076     YLayoutBox * split = YUI::widgetFactory()->createVBox( this );
00077 
00078     // the headline
00079     new NCLabel( split, headline, true, false );        // isHeading = true
00080 
00081     YFrame * frame = YUI::widgetFactory()->createFrame( split, "" );
00082 
00083     // label for text field showing the selected dir
00084     dirName = new NCComboBox( frame, _( "Selected Directory:" ), false ); // editable = false
00085     dirName->setNotify( true );
00086     dirName->setStretchable( YD_HORIZ, true );
00087 
00088     // add the checkBox detailed
00089     YLayoutBox * hSplit = YUI::widgetFactory()->createHBox( split );
00090 
00091     // label for checkbox
00092     detailed = new NCCheckBox( hSplit, _( "&Detailed View" ), false );
00093     detailed->setNotify( true );
00094 
00095     // create table header for table type T_Overview
00096     YTableHeader * tableHeader = new YTableHeader();
00097     tableHeader->addColumn( " ", YAlignBegin );
00098     tableHeader->addColumn( _( "Directory Name" ), YAlignBegin );
00099 
00100     // add the list of directories
00101     dirList = new NCDirectoryTable( split,
00102                                     tableHeader,
00103                                     NCFileTable::T_Overview,
00104                                     iniDir );
00105 
00106     YUI::widgetFactory()->createSpacing( split, YD_VERT, false, 1.0 );
00107 
00108     // HBox for the buttons
00109     YLayoutBox * hSplit1 = YUI::widgetFactory()->createHBox( split );
00110 
00111     YUI::widgetFactory()->createSpacing( hSplit1, YD_HORIZ, true, 0.2 );        // stretchable = true
00112 
00113     // add the OK button
00114     okButton = new NCPushButton( hSplit1, _( "&OK" ) );
00115     okButton->setFunctionKey( 10 );
00116     okButton->setStretchable( YD_HORIZ, true );
00117 
00118     YUI::widgetFactory()->createSpacing( hSplit1, YD_HORIZ, true, 0.4 );
00119 
00120     // add the Cancel button
00121     cancelButton = new NCPushButton( hSplit1, _( "&Cancel" ) );
00122     cancelButton->setFunctionKey( 9 );
00123     cancelButton->setStretchable( YD_HORIZ, true );
00124 
00125     YUI::widgetFactory()->createSpacing( hSplit1, YD_HORIZ, true, 0.2 );
00126     // restore former text domain
00127     setTextdomain( old_textdomain.c_str() );
00128 }
00129 
00130 
00131 NCursesEvent & NCAskForExistingDirectory::showDirPopup( )
00132 {
00133     postevent = NCursesEvent();
00134 
00135     if ( !dirList || !dirName )
00136         return postevent;
00137 
00138     dirList->fillList( );
00139 
00140     dirList->setKeyboardFocus();
00141 
00142     dirName->addItem( dirList->getCurrentDir(),
00143                       true );           // selected
00144 
00145     // event loop
00146     do
00147     {
00148         popupDialog();
00149     }
00150     while ( postAgain() );
00151 
00152     popdownDialog();
00153 
00154 
00155     return postevent;
00156 }
00157 
00158 
00159 int NCAskForExistingDirectory::preferredWidth()
00160 {
00161     return NCurses::cols() - 10;
00162 }
00163 
00164 
00165 int NCAskForExistingDirectory::preferredHeight()
00166 {
00167     return NCurses::lines() - 4;
00168 }
00169 
00170 
00171 NCursesEvent NCAskForExistingDirectory::wHandleInput( wint_t ch )
00172 {
00173     if ( ch == 27 ) // ESC
00174         return NCursesEvent::cancel;
00175 
00176     return NCDialog::wHandleInput( ch );
00177 }
00178 
00179 
00180 bool NCAskForExistingDirectory::postAgain( )
00181 {
00182     if ( !postevent.widget )
00183         return false;
00184 
00185     postevent.detail = NCursesEvent::NODETAIL;
00186 
00187     if ( postevent.widget == okButton )
00188     {
00189         postevent.result = dirList->getCurrentDir();
00190         // return false means: close the popup
00191         return false;
00192     }
00193     else if ( postevent.widget == dirList )
00194     {
00195         if ( postevent.result == "" )
00196             return true;
00197 
00198         // show the currently selected directory
00199         yuiDebug() << "Add item: " <<  postevent.result << std::endl;
00200 
00201         dirName->addItem( postevent.result,
00202                           true );
00203 
00204         if ( postevent.reason == YEvent::Activated )
00205         {
00206             // fill the directory list
00207             dirList->fillList();
00208         }
00209     }
00210     else if ( postevent.widget == dirName )
00211     {
00212         dirList->setStartDir( dirName->value() );
00213         dirList->fillList();
00214     }
00215     else if ( postevent.widget == detailed )
00216     {
00217         bool details = getCheckBoxValue( detailed );
00218 
00219         if ( details )
00220         {
00221             dirList->setTableType( NCFileTable::T_Detailed );
00222         }
00223         else
00224         {
00225             dirList->setTableType( NCFileTable::T_Overview );
00226         }
00227 
00228         dirList->fillList();
00229     }
00230     else
00231     {
00232         postevent.result = "";
00233         return false;
00234     }
00235 
00236     if ( postevent.widget == cancelButton ||
00237          postevent == NCursesEvent::cancel )
00238     {
00239         postevent.result = "";
00240         return false;
00241     }
00242 
00243     return true;
00244 }
00245 
00246 
00247 bool NCAskForExistingDirectory::getCheckBoxValue( NCCheckBox * checkBox )
00248 {
00249     if ( checkBox )
00250     {
00251         // return whether the option is selected or not
00252         return ( checkBox->isChecked() );
00253     }
00254 
00255     return false;
00256 }
 All Classes Functions Variables