wxIScan
wxoptionsdialog.cpp
Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 // Project:     wxIView
00003 // Purpose:     Complex wxWidgets sample
00004 // Name:        wxoptionsdialog.cpp
00005 // Author:      Daniel Nell
00006 // Created:     2006/02/16
00007 // Copyright:   (c) Daniel Nell
00008 // Licence:     wxWindows licence
00009 // Modified by:
00010 /////////////////////////////////////////////////////////////////////////////
00011 
00012 // For compilers that support precompilation, includes "wx/wx.h".
00013 #include <wx/wxprec.h>
00014 #ifndef WX_PRECOMP
00015    #include <wx/wx.h>
00016 #endif
00017 
00018 // Include additional wxWidgets headers.
00019 #include <wx/config.h>
00020 #include <wx/statline.h>
00021 #include <wx/listbook.h>
00022 
00023 // Include private headers.
00024 #include "wxoptionsdialog.h"
00025 
00026 
00027 //////////////////////////////////////////////////////////
00028 // class wxOptionStaticLine
00029 //
00030 wxOptionStaticLine::wxOptionStaticLine()
00031 {
00032 }
00033 
00034 void wxOptionStaticLine::Setup( wxWindow *poWindow, int nId )
00035 {
00036     m_poStaticLine= new wxStaticLine( poWindow, nId );
00037     if( !m_poStaticLine )
00038     {
00039         wxLogError( _( "Cannot create static line pseudo option." ) );
00040         return;
00041     }
00042 }
00043 
00044 bool wxOptionStaticLine::AddToSizer( wxBoxSizer *poSizer )
00045 {
00046     return m_poStaticLine
00047            ? (bool)poSizer->Add( m_poStaticLine, 0, wxGROW | wxLEFT | wxRIGHT, 1 )
00048            : false;
00049 }
00050 
00051 
00052 //////////////////////////////////////////////////////////
00053 // class wxOptionCheckBox
00054 //
00055 wxOptionCheckBox::wxOptionCheckBox( const wxString &strText, bool *pbValue )
00056  : wxOptionBase()
00057 {
00058     m_pbValue= pbValue;
00059     m_strText= strText;
00060 }
00061 
00062 bool wxOptionCheckBox::AddToSizer( wxBoxSizer *poSizer )
00063 {
00064     return m_poCheckBox
00065            ? (bool)poSizer->Add( m_poCheckBox, 0, wxALIGN_LEFT | wxALL, 1 )
00066            : false;
00067 }
00068 
00069 void wxOptionCheckBox::Setup( wxWindow *poWindow, int nId )
00070 {
00071     m_poCheckBox= new wxCheckBox( poWindow, nId, m_strText );
00072     if( !m_poCheckBox )
00073     {
00074         wxLogError( _( "Cannot create option checkbox." ) );
00075         return;
00076     }
00077     m_poCheckBox->SetValue( *m_pbValue );
00078 }
00079 
00080 void wxOptionCheckBox::StoreValue()
00081 {
00082     (*m_pbValue)= m_poCheckBox->GetValue();
00083 }
00084 
00085 
00086 //////////////////////////////////////////////////////////
00087 // class wxOptionTextBox
00088 //
00089 wxOptionTextBox::wxOptionTextBox( const wxString &strText, wxString *pstrValue, int nWidth )
00090 {
00091     m_strText= strText;
00092     m_pstrValue= pstrValue;
00093     m_nWidth= nWidth;
00094 }
00095 
00096 bool wxOptionTextBox::AddToSizer( wxBoxSizer *poSizer )
00097 {
00098     if( !m_poTextCtrl )
00099     {
00100         return false;
00101     }
00102 
00103     wxBoxSizer *poHSizer= new wxBoxSizer( wxHORIZONTAL );
00104 
00105     if( !poHSizer )
00106     {
00107         return false;
00108     }
00109     if(    !poHSizer->Add( new wxStaticText( m_poWindow, wxID_ANY, m_strText ), 0,
00110                            wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT )
00111         || !poHSizer->Add( m_poTextCtrl, 0,
00112                            wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT )
00113     )
00114     {
00115         delete poHSizer;
00116         return false;
00117     }
00118     return (bool)poSizer->Add( poHSizer, 0, wxALIGN_RIGHT | wxALL, 1 );
00119 }
00120 
00121 void wxOptionTextBox::Setup( wxWindow *poWindow, int nId )
00122 {
00123     m_poWindow= poWindow;
00124 
00125     m_poTextCtrl= new wxTextCtrl( m_poWindow, nId, *m_pstrValue, wxDefaultPosition, wxSize( m_nWidth, -1 ) );
00126     if( !m_poTextCtrl )
00127     {
00128         wxLogError( _( "Cannot create option textbox." ) );
00129         return;
00130     }
00131 }
00132 
00133 void wxOptionTextBox::StoreValue()
00134 {
00135     (*m_pstrValue)= m_poTextCtrl->GetValue();
00136 }
00137 
00138 
00139 
00140 //////////////////////////////////////////////////////////
00141 // class wxOptionTextBox
00142 //
00143 wxOptionComboBox::wxOptionComboBox( const wxString &strText, wxString *pstrValue, int nWidth )
00144 {
00145     m_strText= strText;
00146     m_pstrValue= pstrValue;
00147     m_nWidth= nWidth;
00148 }
00149 
00150 wxOptionComboBox::~wxOptionComboBox()
00151 {
00152     // Get the global configuration object and set path.
00153     wxConfigBase *poConfig = wxConfigBase::Get();
00154     poConfig->SetPath( wxString::Format( wxT( "/wxOptionComboBox/%d" ), m_nId ) );
00155 
00156     // Write history to config (file/registry).
00157     int nCount= m_astrValueHistory.GetCount();
00158 
00159     for( int i= 0; i < nCount; i++ )
00160     {
00161         poConfig->Write( wxString::Format( wxT( "Text%d" ), i ), m_astrValueHistory[i] );
00162     }
00163 }
00164 
00165 bool wxOptionComboBox::AddToSizer( wxBoxSizer *poSizer )
00166 {
00167     if( !m_poComboBox )
00168     {
00169         return false;
00170     }
00171 
00172     wxBoxSizer *poHSizer= new wxBoxSizer( wxHORIZONTAL );
00173 
00174     if( !poHSizer )
00175     {
00176         return false;
00177     }
00178     if(    !poHSizer->Add( new wxStaticText( m_poWindow, wxID_ANY, m_strText ), 0,
00179                            wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT )
00180         || !poHSizer->Add( m_poComboBox, 0,
00181                            wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT )
00182     )
00183     {
00184         delete poHSizer;
00185         return false;
00186     }
00187     return (bool)poSizer->Add( poHSizer, 0, wxALIGN_RIGHT | wxALL, 1 );
00188 }
00189 
00190 void wxOptionComboBox::Setup( wxWindow *poWindow, int nId )
00191 {
00192     // ...
00193     m_poWindow= poWindow;
00194     m_nId= nId;
00195 
00196     // Get the global configuration object and set path.
00197     wxConfigBase *poConfig = wxConfigBase::Get();
00198     poConfig->SetPath( wxString::Format( wxT( "/wxOptionComboBox/%d" ), m_nId ) );
00199 
00200     // ...
00201     wxString strTextId;
00202     int i= 0;
00203 
00204     for( strTextId.Printf( wxT( "Text%d" ), i );
00205          poConfig->HasEntry( strTextId );
00206          i++, strTextId.Printf( wxT( "Text%d" ), i ) )
00207     {
00208         wxString strTemp= poConfig->Read( strTextId, wxT( "" ) );
00209         m_astrValueHistory.Add( strTemp );
00210     }
00211 
00212     // Create the combobox.
00213     m_poComboBox= new wxComboBox( m_poWindow, m_nId, *m_pstrValue, wxDefaultPosition, wxSize( m_nWidth, -1 ),
00214                                   m_astrValueHistory, wxCB_DROPDOWN | wxCB_SORT );
00215     if( !m_poComboBox )
00216     {
00217         wxLogError( _( "Cannot create option combobox." ) );
00218         return;
00219     }
00220 }
00221 
00222 void wxOptionComboBox::StoreValue()
00223 {
00224     // Get the text from the edit control.
00225     (*m_pstrValue)= m_poComboBox->GetValue();
00226 
00227     // Check if the text is already in the history list and add it if not.
00228     int nCount= m_astrValueHistory.GetCount();
00229     int i= 0;
00230 
00231     for( ; i < nCount; i++ )
00232     {
00233         if( m_astrValueHistory[i] == (*m_pstrValue) )
00234         {
00235             return;
00236         }
00237     }
00238     m_astrValueHistory.Add( (*m_pstrValue) );
00239 }
00240 
00241 
00242 //////////////////////////////////////////////////////////
00243 // The standard options dialog
00244 //
00245 BEGIN_EVENT_TABLE( wxOptionsDialog, wxDialog )
00246     EVT_BUTTON( wxID_OK, wxOptionsDialog::OnOk )
00247 END_EVENT_TABLE()
00248 
00249 
00250 wxOptionsDialog::wxOptionsDialog( wxWindow *poParentWindow )
00251   : wxDialog( poParentWindow, -1,
00252               wxTheApp->GetAppName()  + wxT( " - " ) + _( "Options" ),
00253               wxDefaultPosition,
00254               wxDefaultSize,
00255               wxDEFAULT_DIALOG_STYLE /* | wxRESIZE_BORDER */ ),
00256     m_nOptionCount( 0 ),
00257     m_nOptionId( WXOPTIONSDIALOGIDOPTION )
00258 {
00259     // Initialize members. (Nothing to do, yet.)
00260 }
00261 
00262 wxOptionsDialog::~wxOptionsDialog()
00263 {
00264     // Clean up options.
00265     while( m_nOptionCount > 0 )
00266     {
00267         m_nOptionCount--;
00268         delete m_apoOptions[m_nOptionCount];
00269     }
00270 }
00271 
00272 void wxOptionsDialog::SetupDialog()
00273 {
00274     // Create layout.
00275     wxBoxSizer *poSizer= new wxBoxSizer( wxVERTICAL );
00276 
00277     // Create controls.
00278     for( int i= 0; i < m_nOptionCount; i++ )
00279     {
00280         m_apoOptions[i]->AddToSizer( poSizer );
00281     }
00282     poSizer->Add( new wxStaticLine( this ), 0, wxGROW | wxLEFT | wxRIGHT, 1 );
00283     poSizer->Add( CreateButtonSizer( wxOK | wxCANCEL ), 0, wxALIGN_CENTER | wxALL, 5 );
00284 
00285     // Activate.
00286 //    SetSizer( poSizer );
00287 GetParent()->SetSizer( poSizer );
00288 //GetParent()->Layout();
00289 //poSizer->SetSizeHints( GetParent() );
00290 //poSizer->Fit( GetParent() );
00291 //GetParent()->SetAutoLayout( true );
00292 //wxBoxSizer *poMSizer= new wxBoxSizer( wxVERTICAL );
00293 //SetSizer( poMSizer );
00294 //Layout();
00295     SetAutoLayout( true );
00296     poSizer->SetSizeHints( this );
00297     poSizer->Fit( this );
00298     Centre( wxBOTH | wxCENTRE_ON_SCREEN );
00299 }
00300 
00301 bool wxOptionsDialog::AppendOption( wxOptionBase *poOption )
00302 {
00303     if( !poOption )
00304     {
00305         return false;
00306     }
00307     if( m_nOptionCount >= WXOPTIONSDIALOGMAXOPTION )
00308     {
00309         return false;
00310     }
00311     poOption->Setup( GetParent(), m_nOptionId );
00312     m_nOptionId++;
00313     m_apoOptions[m_nOptionCount]= poOption;
00314     m_nOptionCount++;
00315     return true;
00316 }
00317 
00318 void wxOptionsDialog::OnOk( wxCommandEvent& WXUNUSED( oEvent ) )
00319 {
00320     // Get values.
00321     for( int i= 0; i < m_nOptionCount; i++ )
00322     {
00323         m_apoOptions[i]->StoreValue();
00324     }
00325 
00326     // End modal dialog mode and set the return value to 'OK'.
00327     EndModal( wxID_OK );
00328 }
00329 
00330 
00331 #if __NOTYETRELEASED__ >= 2
00332 //////////////////////////////////////////////////////////
00333 // The list-/choice-/notebook options dialog
00334 //
00335 wxOptionsBookDialog::wxOptionsBookDialog( wxWindow *poParentWindow, const wxString &strPageName,
00336                                           wxBookCtrlBase *poBookCtrl )
00337  : wxOptionsDialog( poParentWindow )
00338 {
00339     // Initialize members.
00340     m_poBookCtrl= poBookCtrl ? poBookCtrl : new wxListbook( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLB_LEFT );
00341 
00342     // Do further initialization.
00343     //
00344     // Add the first page to the ...book.
00345     AddPage( strPageName );
00346 }
00347 
00348 bool wxOptionsBookDialog::AddPage( const wxString &strPageName )
00349 {
00350     wxPanel *poPanel= new wxPanel( m_poBookCtrl );
00351 
00352     if( !poPanel )
00353     {
00354         wxLogError( _( "Cannot create new panel for options to place onto." ) );
00355         return false;
00356     }
00357     if( !m_poBookCtrl->AddPage( poPanel, strPageName, false, -1 ) )
00358     {
00359         wxLogWarning( _( "Cannot add new page." ) );
00360         delete poPanel;
00361         return false;
00362     }
00363     m_poParentForControls= poPanel;
00364     return true;
00365 }
00366 #endif // __NOTYETRELEASED__