refeditdlg.cpp

00001 
00002 // Name:        refeditdlg.cpp
00003 // Purpose:     wxPackageReferenceEditDlg
00004 // Author:      Francesco Montorsi
00005 // Modified by:
00006 // Created:     Thu 10 Aug 2006 18:49:40 CEST
00007 // RCS-ID:      $Id: refeditdlg.cpp,v 1.4 2007/01/27 23:26:21 frm Exp $
00008 // Copyright:   (c) 2006 Francesco Montorsi
00009 // Licence:     wxWidgets license
00011 
00012 
00013 // For compilers that support precompilation, includes "wx/wx.h".
00014 #include "wx/wxprec.h"
00015 
00016 #ifdef __BORLANDC__
00017 #pragma hdrstop
00018 #endif
00019 
00020 #ifndef WX_PRECOMP
00021 #include "wx/wx.h"
00022 #endif
00023 
00024 #include <wx/url.h>
00025 #include <wx/filename.h>
00026 #include <wx/filesys.h>
00027 
00028 #include "wx/miscutils.h"
00029 #include "guipkg/refeditdlg.h"
00030 
00031 
00032 // ----------------------------------------------------------------------------
00033 // wxPackageReferenceEditDlg
00034 // ----------------------------------------------------------------------------
00035 
00036 IMPLEMENT_DYNAMIC_CLASS( wxPackageReferenceEditDlg, wxDialog )
00037 BEGIN_EVENT_TABLE( wxPackageReferenceEditDlg, wxDialog )
00038     EVT_BUTTON( ID_REFEDIT_LOCALBROWSE, wxPackageReferenceEditDlg::OnLocalBrowse )
00039     EVT_BUTTON( wxID_OK, wxPackageReferenceEditDlg::OnOK )
00040 END_EVENT_TABLE()
00041 
00042 wxPackageReferenceEditDlg::wxPackageReferenceEditDlg( )
00043 {
00044 }
00045 
00046 wxPackageReferenceEditDlg::wxPackageReferenceEditDlg( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
00047 {
00048     Create(parent, id, caption, pos, size, style);
00049 }
00050 
00051 bool wxPackageReferenceEditDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
00052 {
00054     m_pLocal = NULL;
00055     m_pRemote = NULL;
00057 
00059     SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
00060     wxDialog::Create( parent, id, caption, pos, size, style );
00061 
00062     CreateControls();
00063     if (GetSizer())
00064     {
00065         GetSizer()->SetSizeHints(this);
00066     }
00067     Centre();
00069     return true;
00070 }
00071 
00072 void wxPackageReferenceEditDlg::CreateControls()
00073 {
00075     wxPackageReferenceEditDlg* itemDialog1 = this;
00076 
00077     wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
00078     itemDialog1->SetSizer(itemBoxSizer2);
00079 
00080     wxStaticText* itemStaticText3 = new wxStaticText( itemDialog1, wxID_STATIC, _("The wxWidgets package format allows you to specify two types of links for the same \nresource: a local and a remote reference."), wxDefaultPosition, wxDefaultSize, 0 );
00081     itemBoxSizer2->Add(itemStaticText3, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 5);
00082 
00083     wxStaticBox* itemStaticBoxSizer4Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Local reference"));
00084     wxStaticBoxSizer* itemStaticBoxSizer4 = new wxStaticBoxSizer(itemStaticBoxSizer4Static, wxVERTICAL);
00085     itemBoxSizer2->Add(itemStaticBoxSizer4, 0, wxGROW|wxALL, 5);
00086 
00087     wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("The local reference will be used when the package has been downloaded and \nthus must be a path relative to the root directory of the WXZ package."), wxDefaultPosition, wxDefaultSize, 0 );
00088     itemStaticBoxSizer4->Add(itemStaticText5, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 5);
00089 
00090     wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
00091     itemStaticBoxSizer4->Add(itemBoxSizer6, 0, wxGROW, 5);
00092 
00093     wxStaticText* itemStaticText7 = new wxStaticText( itemDialog1, wxID_STATIC, _("Local reference:"), wxDefaultPosition, wxDefaultSize, 0 );
00094     itemBoxSizer6->Add(itemStaticText7, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);
00095 
00096     m_pLocal = new wxTextCtrl( itemDialog1, ID_REFEDIT_LOCAL, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
00097     itemBoxSizer6->Add(m_pLocal, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
00098 
00099     wxButton* itemButton9 = new wxButton( itemDialog1, ID_REFEDIT_LOCALBROWSE, _("Browse"), wxDefaultPosition, wxDefaultSize, 0 );
00100     itemBoxSizer6->Add(itemButton9, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
00101 
00102     wxStaticBox* itemStaticBoxSizer10Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Remote reference"));
00103     wxStaticBoxSizer* itemStaticBoxSizer10 = new wxStaticBoxSizer(itemStaticBoxSizer10Static, wxVERTICAL);
00104     itemBoxSizer2->Add(itemStaticBoxSizer10, 0, wxGROW|wxALL, 5);
00105 
00106     wxStaticText* itemStaticText11 = new wxStaticText( itemDialog1, wxID_STATIC, _("The remote reference will be used when the package has not been downloaded \nyet and thus must be an URL to a remote resource."), wxDefaultPosition, wxDefaultSize, 0 );
00107     itemStaticBoxSizer10->Add(itemStaticText11, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 5);
00108 
00109     wxBoxSizer* itemBoxSizer12 = new wxBoxSizer(wxHORIZONTAL);
00110     itemStaticBoxSizer10->Add(itemBoxSizer12, 0, wxGROW, 5);
00111 
00112     wxStaticText* itemStaticText13 = new wxStaticText( itemDialog1, wxID_STATIC, _("Remote reference:"), wxDefaultPosition, wxDefaultSize, 0 );
00113     itemBoxSizer12->Add(itemStaticText13, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);
00114 
00115     m_pRemote = new wxTextCtrl( itemDialog1, ID_REFEDIT_REMOTE, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
00116     itemBoxSizer12->Add(m_pRemote, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
00117 
00118     wxStdDialogButtonSizer* itemStdDialogButtonSizer15 = new wxStdDialogButtonSizer;
00119 
00120     itemBoxSizer2->Add(itemStdDialogButtonSizer15, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
00121     wxButton* itemButton16 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
00122     itemStdDialogButtonSizer15->AddButton(itemButton16);
00123 
00124     wxButton* itemButton17 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
00125     itemStdDialogButtonSizer15->AddButton(itemButton17);
00126 
00127     itemStdDialogButtonSizer15->Realize();
00128 
00130 
00131     m_pLocal->SetValue(wxPACKAGEREF_DEFAULT_LOCALREF);
00132     m_pRemote->SetValue(wxPACKAGEREF_DEFAULT_HREF);
00133     m_strWildcard = wxT("*");
00134 }
00135 
00136 bool wxPackageReferenceEditDlg::ShowToolTips()
00137 {
00138     return true;
00139 }
00140 
00141 
00142 
00143 // ----------------------------------------------------------------------------
00144 // wxPackageReferenceEditDlg - event handlers
00145 // ----------------------------------------------------------------------------
00146 
00147 void wxPackageReferenceEditDlg::OnLocalBrowse(wxCommandEvent &WXUNUSED(ev))
00148 {
00149     // choose the default path to show
00150     wxString v = m_pLocal->GetValue();
00151     wxString defpath = (v.IsEmpty() || v == wxPACKAGEREF_DEFAULT_LOCALREF) ?
00152                 m_strRoot : wxFileSystem::URLToFileName(v).GetFullPath();
00153 
00154     wxFileDialog dlg(this, wxT("Choose the local file"),
00155                      defpath, wxEmptyString, m_strWildcard,
00156                      wxFD_DEFAULT_STYLE|wxFD_FILE_MUST_EXIST);
00157 
00158     wxFileName lref, root = wxFileName::DirName(m_strRoot);
00159     if (!root.IsOk() || !root.DirExists())
00160     {
00161         wxLogError(wxT("Invalid project root directory '%s'.\nCorrect it and retry."), 
00162                    m_strRoot.c_str());
00163         return;
00164     }
00165 
00166     bool valid;
00167     do
00168     {
00169         valid = true;
00170         if (dlg.ShowModal() == wxID_CANCEL)
00171             return;
00172 
00173         valid = wxIsSubfolderOf(root, dlg.GetPath());
00174 
00175         if (!valid)
00176         {
00177             wxASSERT(!m_strRoot.IsEmpty());
00178             wxString str = wxString::Format(
00179                 wxT("As local reference you must select a file located under the project root directory '%s'..."),
00180                 m_strRoot.c_str());
00181             wxMessageBox(str, wxT("Error"), wxOK|wxICON_ERROR);
00182         }
00183 
00184     } while (!valid);
00185 
00186     // we have a valid ref!
00187     // make it relative to the project root directory
00188     lref = dlg.GetPath();
00189     if (root.GetDirCount() > 0)
00190         root.RemoveLastDir();
00191     lref.MakeRelativeTo(root.GetPath());
00192     wxASSERT(lref.IsRelative());
00193 
00194     // wxFileSystem::FileNameToURL will make the filename absolute while we
00195     // want to avoid this; thus we'll do our own wxFileName --> URL conversion
00196     m_pLocal->SetValue(wxT("file:///") + lref.GetFullPath(wxPATH_UNIX));
00197 }
00198 
00199 void wxPackageReferenceEditDlg::OnOK(wxCommandEvent &WXUNUSED(ev))
00200 {
00201     wxString r(m_pRemote->GetValue()), l(m_pLocal->GetValue());
00202 
00203     // both empty are not allowed!
00204     if ((r.IsEmpty() || r == wxPACKAGEREF_DEFAULT_HREF) &&
00205         (l.IsEmpty() || l == wxPACKAGEREF_DEFAULT_LOCALREF))
00206     {
00207         wxLogError(wxT("Please insert at least one of the required URLs."));
00208         return;
00209     }
00210 
00211     // check remote URL
00212     if (!r.IsEmpty())
00213     {
00214         wxURL u(r);
00215         if (u.GetError() != wxURL_NOERR)
00216         {
00217             wxLogError(wxT("Invalid remote URL: %s"), r.c_str());
00218             return;
00219         }
00220         else if (u.GetScheme() != wxT("http"))
00221         {
00222             wxLogError(wxT("Unknown '%s' protocol used in the remote URL. Please use the HTTP protocol."), u.GetScheme().c_str());
00223             return;
00224         }
00225     }
00226 
00227     // check local URL
00228     if (!l.IsEmpty())
00229     {
00230         wxURL u(l);
00231         if (u.GetError() != wxURL_NOERR)
00232         {
00233             wxLogError(wxT("Invalid local URL: %s"), l.c_str());
00234             return;
00235         }
00236         else if (u.GetScheme() != wxT("file"))
00237         {
00238             wxLogError(wxT("Unknown '%s' protocol used in the local URL. Please use the FILE protocol."), u.GetScheme().c_str());
00239             return;
00240         }
00241     }
00242 
00243     // all checks passed
00244     EndModal(wxID_OK);
00245 }
00246 

Generated on Thu Feb 1 22:14:31 2007 for wxWidgets Package Manager by  doxygen 1.5.1-p1