1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        panel.cpp
3 // Purpose:     wxCurlConnectionSettingsPanel
4 // Author:      Francesco Montorsi
5 // Created:     2007/05/5
6 // RCS-ID:      $Id: panel.cpp 1240 2010-03-10 23:54:25Z frm $
7 // Copyright:   (c) 2007 Francesco Montorsi
8 // Licence:     wxWidgets licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14 
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18 
19 // includes
20 #ifndef WX_PRECOMP
21     #include <wx/log.h>
22     #include <wx/intl.h>        // for _() support
23     #include <wx/utils.h>       // for wxMilliSleep
24 
25     #if wxUSE_GUI
26         #include <wx/textctrl.h>
27         #include <wx/gauge.h>
28         #include <wx/stattext.h>
29         #include <wx/sizer.h>
30         #include <wx/msgdlg.h>
31         #include <wx/settings.h>
32         #include <wx/button.h>
33         #include <wx/statbmp.h>
34     #endif
35 #endif
36 
37 #ifdef __WXMSW__
38     #include <wx/msw/msvcrt.h>      // useful to catch memory leaks when compiling under MSVC
39 #endif
40 
41 #include <wx/checkbox.h>
42 #include <wx/filename.h>
43 #include <wx/statline.h>
44 #include "wx/curl/panel.h"
45 
46 
47 // ----------------------------------------------------------------------------
48 // wxCurlConnectionSettingsPanel
49 // ----------------------------------------------------------------------------
50 
51 enum
52 {
53     UseProxy = wxID_HIGHEST+1
54 };
55 
IMPLEMENT_DYNAMIC_CLASS(wxCurlConnectionSettingsPanel,wxPanel)56 IMPLEMENT_DYNAMIC_CLASS( wxCurlConnectionSettingsPanel, wxPanel )
57 BEGIN_EVENT_TABLE( wxCurlConnectionSettingsPanel, wxPanel )
58     EVT_CHECKBOX( UseProxy, wxCurlConnectionSettingsPanel::OnUseProxy )
59 END_EVENT_TABLE()
60 
61 bool wxCurlConnectionSettingsPanel::Create(wxWindow* parent, wxWindowID id,
62                                            const wxString &msg,
63                                            const wxPoint& pos, const wxSize& size,
64                                            long style, const wxString& name)
65 {
66     if (!wxPanel::Create(parent, id, pos, size, style, name))
67         return false;
68 
69     // set up our controls
70     CreateControls(msg);
71 
72     if (GetMinSize().GetWidth() < 250)
73         SetMinSize(wxSize(250, -1));
74 
75     return true;
76 }
77 
78 #define BORDER          5
79 
AddSizerRow(wxSizer * sz,const wxString & name,bool grow)80 wxTextCtrl *wxCurlConnectionSettingsPanel::AddSizerRow(wxSizer *sz, const wxString &name, bool grow)
81 {
82     // the static text
83     wxStaticText *st = new wxStaticText( this, wxID_STATIC, name, wxDefaultPosition, wxDefaultSize );
84 
85     // the value
86     wxTextCtrl *ret = new wxTextCtrl( this, wxID_ANY );
87 
88     long flags = wxALIGN_CENTER_VERTICAL|wxTOP|wxRIGHT;
89     sz->Add(st, 0, flags, BORDER);
90     sz->Add(ret, 1, grow ? flags|wxGROW : flags, BORDER);
91 
92     return ret;
93 }
94 
CreateControls(const wxString & msg)95 void wxCurlConnectionSettingsPanel::CreateControls(const wxString &msg)
96 {
97     wxBoxSizer* main = new wxBoxSizer(wxVERTICAL);
98 
99     // message row
100     if (!msg.IsEmpty())
101     {
102         wxStaticText *st = new wxStaticText( this, wxID_STATIC, msg );
103         main->Add(st, 0, wxLEFT|wxTOP|wxRIGHT|wxBOTTOM|wxGROW, BORDER);
104     }
105 
106     wxFlexGridSizer *gs = new wxFlexGridSizer(0 /* calc automatically the # of rows */, 2, 0, 0);
107 
108     // authentication options
109     if (HasFlag(wxCCSP_AUTHENTICATION_OPTIONS))
110     {
111         m_pUsername = AddSizerRow(gs, _("Username:"));
112         m_pPassword = AddSizerRow(gs, _("Password:"));
113         gs->AddStretchSpacer(1);
114         gs->AddStretchSpacer(1);
115     }
116 
117     // port options
118     if (HasFlag(wxCCSP_PORT_OPTION))
119     {
120         m_pPort = AddSizerRow(gs, _("Port:"), false);
121         gs->AddStretchSpacer(1);
122         gs->AddStretchSpacer(1);
123 
124         m_pPort->SetValue(wxS("-1"));
125     }
126 
127     // column 1 contains the text controls:
128     gs->AddGrowableCol(1);
129     main->Add(gs, 1, wxGROW|wxALL, BORDER);
130 
131     // proxy options
132     if (HasFlag(wxCCSP_PROXY_OPTIONS))
133     {
134         wxSizer* proxy = new wxStaticBoxSizer(wxVERTICAL, this, _("Proxy settings"));
135 
136         m_pProxyCheckBox = new wxCheckBox(this, UseProxy, _("Use proxy"));
137         m_pProxyCheckBox->SetValue(false);
138         proxy->Add(m_pProxyCheckBox, 0, wxLEFT|wxTOP, BORDER);
139 
140         wxFlexGridSizer *gs = new wxFlexGridSizer(0 /* calc automatically the # of rows */, 2, 0, 0);
141         m_pProxyHost = AddSizerRow(gs, _("Proxy host:"));
142         m_pProxyUsername = AddSizerRow(gs, _("Proxy username:"));
143         m_pProxyPassword = AddSizerRow(gs, _("Proxy password:"));
144         m_pProxyPort = AddSizerRow(gs, _("Proxy port:"), false);
145         m_pProxyPort->SetValue(wxS("-1"));
146         proxy->Add(gs, 0, wxGROW|wxALL, BORDER);
147 
148         // column 1 contains the text controls:
149         gs->AddGrowableCol(1);
150 
151         main->AddSpacer(10);
152         main->Add(proxy, 0, wxGROW|wxLEFT|wxRIGHT, BORDER);
153 #if wxCHECK_VERSION(2,9,0)
154         main->AddStretchSpacer(1);
155 #endif
156 
157         // be default disable the proxy controls
158         wxCommandEvent fake;
159         fake.SetInt(0);
160         OnUseProxy(fake);
161     }
162 
163     this->SetSizerAndFit(main);
164     main->SetSizeHints(this);
165 }
166 
SetCURLOptions(wxCurlBase * p)167 void wxCurlConnectionSettingsPanel::SetCURLOptions(wxCurlBase *p)
168 {
169     wxASSERT(p);
170 
171     if (HasFlag(wxCCSP_PORT_OPTION))
172     {
173         long port = -1;     // -1 tell libCURL to use the default port
174         m_pPort->GetValue().ToLong(&port);
175         p->SetPort(port);
176     }
177 
178     if (HasFlag(wxCCSP_AUTHENTICATION_OPTIONS))
179     {
180         p->SetUsername(m_pUsername->GetValue());
181         p->SetPassword(m_pPassword->GetValue());
182     }
183 
184     if (HasFlag(wxCCSP_PROXY_OPTIONS))
185     {
186         p->UseProxy(m_pProxyCheckBox->GetValue());
187 
188         p->SetProxyHost(m_pProxyHost->GetValue());
189         p->SetProxyUsername(m_pProxyUsername->GetValue());
190         p->SetProxyPassword(m_pProxyPassword->GetValue());
191 
192         long port = -1;     // -1 tell libCURL to use the default port
193         m_pProxyPort->GetValue().ToLong(&port);
194         p->SetProxyPort(port);
195     }
196 }
197 
198 
199 // ----------------------------------------------------------------------------
200 // wxCurlConnectionSettingsPanel - button events
201 // ----------------------------------------------------------------------------
202 
OnUseProxy(wxCommandEvent & ev)203 void wxCurlConnectionSettingsPanel::OnUseProxy(wxCommandEvent &ev)
204 {
205     m_pProxyHost->Enable(ev.IsChecked());
206     m_pProxyUsername->Enable(ev.IsChecked());
207     m_pProxyPassword->Enable(ev.IsChecked());
208     m_pProxyPort->Enable(ev.IsChecked());
209 }
210 
211 
212