libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCPopupInfo.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:       NCPopupInfo.cc
00020 
00021    Author:     Gabriele Strattner <gs@suse.de>
00022 
00023 /-*/
00024 
00025 #define  YUILogComponent "ncurses"
00026 #include <yui/YUILog.h>
00027 #include "NCPopupInfo.h"
00028 
00029 #include "NCTree.h"
00030 #include <yui/YMenuButton.h>
00031 #include <yui/YDialog.h>
00032 #include "NCLayoutBox.h"
00033 #include "NCSpacing.h"
00034 
00035 
00036 namespace
00037 {
00038     const std::string idOk( "ok" );
00039     const std::string idCancel( "cancel" );
00040 }
00041 
00042 
00043 NCPopupInfo::NCPopupInfo( const wpos at,
00044                           const std::string & headline,
00045                           const std::string & text,
00046                           std::string okButtonLabel,
00047                           std::string cancelButtonLabel )
00048         : NCPopup( at, false )
00049         , helpText( 0 )
00050         , okButton( 0 )
00051         , cancelButton( 0 )
00052         , hDim( 50 )
00053         , vDim( 20 )
00054         , visible( false )
00055 {
00056     createLayout( headline, text, okButtonLabel, cancelButtonLabel );
00057 }
00058 
00059 
00060 NCPopupInfo::~NCPopupInfo()
00061 {
00062 }
00063 
00064 
00065 void NCPopupInfo::createLayout( const std::string & headline,
00066                                 const std::string & text,
00067                                 std::string okButtonLabel,
00068                                 std::string cancelButtonLabel )
00069 {
00070     // the vertical split is the (only) child of the dialog
00071     NCLayoutBox * split = new NCLayoutBox( this, YD_VERT );
00072 
00073     // add the headline
00074     new NCLabel( split, headline, true, false ); // isHeading = true
00075 
00076     // add the rich text widget
00077     helpText = new NCRichText( split, text );
00078 
00079     NCLayoutBox * hSplit = new NCLayoutBox( split, YD_HORIZ );
00080 
00081     if ( okButtonLabel != "" && cancelButtonLabel != "" )
00082     {
00083         new NCSpacing( hSplit, YD_HORIZ, true, 0.4 ); // stretchable = true
00084     }
00085 
00086     if ( okButtonLabel != "" )
00087     {
00088         // add the OK button
00089         okButton = new NCPushButton( hSplit, okButtonLabel );
00090         okButton->setFunctionKey( 10 );
00091     }
00092 
00093     if ( cancelButtonLabel != "" )
00094     {
00095         new NCSpacing( hSplit, YD_HORIZ, true, 0.4 );
00096 
00097         // add the Cancel button
00098         cancelButton = new NCPushButton( hSplit, cancelButtonLabel );
00099         cancelButton->setFunctionKey( 9 );
00100 
00101         new NCSpacing( hSplit, YD_HORIZ, true, 0.4 );
00102     }
00103 
00104     //If we don't have cancel button and have single ok button instead
00105     //let's focus it by default (#397393)
00106     if ( cancelButtonLabel == "" && okButton )
00107         focusOkButton();
00108 
00109     //the same with missing ok button and single cancel button
00110     if ( okButtonLabel == "" && cancelButton )
00111         focusCancelButton();
00112 }
00113 
00114 
00115 NCursesEvent & NCPopupInfo::showInfoPopup( )
00116 {
00117     postevent = NCursesEvent();
00118 
00119     do
00120     {
00121         popupDialog( );
00122     }
00123     while ( postAgain() );
00124 
00125     popdownDialog();
00126 
00127     return postevent;
00128 }
00129 
00130 
00131 void NCPopupInfo::popup()
00132 {
00133     initDialog();
00134     showDialog();
00135     activate( true );
00136     visible = true;
00137 }
00138 
00139 
00140 void NCPopupInfo::popdown()
00141 {
00142     activate( false );
00143     closeDialog();
00144     visible = false;
00145 }
00146 
00147 
00148 int NCPopupInfo::preferredWidth()
00149 {
00150     int horDim = hDim;
00151 
00152     if ( hDim >= NCurses::cols() )
00153         horDim = NCurses::cols() - 10;
00154 
00155     return horDim;
00156 }
00157 
00158 
00159 int NCPopupInfo::preferredHeight()
00160 {
00161     int vertDim = vDim;
00162 
00163     if ( vDim >= NCurses::lines() )
00164         vertDim = NCurses::lines() - 5;
00165 
00166     return vertDim;
00167 }
00168 
00169 
00170 NCursesEvent
00171 NCPopupInfo::wHandleInput( wint_t ch )
00172 {
00173     if ( ch == 27 ) // ESC
00174         return NCursesEvent::cancel;
00175 
00176     if ( ch == KEY_RETURN )
00177         return NCursesEvent::button;
00178 
00179     return NCDialog::wHandleInput( ch );
00180 }
00181 
00182 
00183 bool NCPopupInfo::postAgain()
00184 {
00185     if ( ! postevent.widget )
00186         return false;
00187 
00188     if ( okButton && cancelButton )
00189     {
00190         if ( postevent.widget == cancelButton )
00191         {
00192             yuiMilestone() << "Cancel button pressed" << std::endl;
00193             // close the dialog
00194             postevent = NCursesEvent::cancel;
00195         }
00196 
00197         // else - nothing to do (postevent is already set)
00198     }
00199 
00200     if ( postevent == NCursesEvent::button || postevent == NCursesEvent::cancel )
00201     {
00202         // return false means: close the popup dialog
00203         return false;
00204     }
00205 
00206     return true;
00207 }
00208 
00209 
 All Classes Functions Variables