On-demand creation of the pages for speedup of sample launch.
[wxWidgets.git] / samples / widgets / hyperlnk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: hyperlnk.cpp
4 // Purpose: Part of the widgets sample showing wxHyperlinkCtrl
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
7 // Id: $Id$
8 // Copyright: (c) 2003 wxWindows team
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_HYPERLINKCTRL
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33
34 #include "wx/bitmap.h"
35 #include "wx/button.h"
36 #include "wx/checkbox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/stattext.h"
40 #include "wx/textctrl.h"
41
42 #include "wx/sizer.h"
43 #endif
44
45 #include "wx/hyperlink.h"
46
47 #include "widgets.h"
48
49 #include "icons/hyperlnk.xpm"
50
51 // ----------------------------------------------------------------------------
52 // constants
53 // ----------------------------------------------------------------------------
54
55 // control ids
56 enum
57 {
58 HyperlinkPage_Reset = wxID_HIGHEST,
59 HyperlinkPage_SetLabel,
60 HyperlinkPage_SetURL,
61 HyperlinkPage_Ctrl
62 };
63
64 // ----------------------------------------------------------------------------
65 // CheckBoxWidgetsPage
66 // ----------------------------------------------------------------------------
67
68 class HyperlinkWidgetsPage : public WidgetsPage
69 {
70 public:
71 HyperlinkWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
72 virtual ~HyperlinkWidgetsPage() {}
73
74 virtual wxControl *GetWidget() const { return m_hyperlink; }
75 virtual void RecreateWidget() { CreateHyperlink(); }
76
77 // lazy creation of the content
78 virtual void CreateContent();
79
80 protected:
81 // event handlers
82 void OnButtonSetLabel(wxCommandEvent& event);
83 void OnButtonSetURL(wxCommandEvent& event);
84
85 void OnButtonReset(wxCommandEvent& event);
86
87 // reset the control parameters
88 void Reset();
89
90 // (re)create the hyperctrl
91 void CreateHyperlink();
92
93 // the controls
94 // ------------
95
96 // the checkbox itself and the sizer it is in
97 wxHyperlinkCtrl *m_hyperlink;
98 wxSizer *m_sizerHyperlink;
99
100 wxTextCtrl *m_label;
101 wxTextCtrl *m_url;
102
103 wxStaticText *m_visit;
104 wxStaticText *m_fun;
105
106 // the text entries for command parameters
107 wxTextCtrl *m_textLabel;
108
109 private:
110 DECLARE_EVENT_TABLE()
111 DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage)
112 };
113
114 // ----------------------------------------------------------------------------
115 // event tables
116 // ----------------------------------------------------------------------------
117
118 BEGIN_EVENT_TABLE(HyperlinkWidgetsPage, WidgetsPage)
119 EVT_BUTTON(HyperlinkPage_Reset, HyperlinkWidgetsPage::OnButtonReset)
120 EVT_BUTTON(HyperlinkPage_SetLabel, HyperlinkWidgetsPage::OnButtonSetLabel)
121 EVT_BUTTON(HyperlinkPage_SetURL, HyperlinkWidgetsPage::OnButtonSetURL)
122 END_EVENT_TABLE()
123
124 // ============================================================================
125 // implementation
126 // ============================================================================
127
128 IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage, wxT("Hyperlink"),
129 GENERIC_CTRLS
130 );
131
132 HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book,
133 wxImageList *imaglist)
134 :WidgetsPage(book, imaglist, hyperlnk_xpm)
135 {
136 }
137
138 void HyperlinkWidgetsPage::CreateContent()
139 {
140 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
141
142 // left pane
143 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Hyperlink details"));
144
145 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
146
147 sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel , wxT("Set &Label"), wxID_ANY, &m_label ),
148 0, wxALL | wxALIGN_RIGHT , 5 );
149
150 sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL , wxT("Set &URL"), wxID_ANY, &m_url ),
151 0, wxALL | wxALIGN_RIGHT , 5 );
152
153 // right pane
154 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
155
156 m_visit = new wxStaticText(this, wxID_ANY, wxT("Visit "));
157
158 m_hyperlink = new wxHyperlinkCtrl(this,
159 HyperlinkPage_Ctrl,
160 wxT("wxWidgets website"),
161 wxT("www.wxwidgets.org"));
162
163 m_fun = new wxStaticText(this, wxID_ANY, wxT(" for fun!"));
164
165 sizerRight->Add(0, 0, 1, wxCENTRE);
166 sizerRight->Add(m_visit, 0, wxCENTRE);
167 sizerRight->Add(m_hyperlink, 0, wxCENTRE);
168 sizerRight->Add(m_fun, 0, wxCENTRE);
169 sizerRight->Add(0, 0, 1, wxCENTRE);
170 sizerRight->SetMinSize(150, 0);
171 m_sizerHyperlink = sizerRight; // save it to modify it later
172
173 // the 3 panes panes compose the window
174 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
175 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
176
177 // final initializations
178 Reset();
179
180 SetSizer(sizerTop);
181
182 sizerTop->Fit(this);
183 }
184
185 void HyperlinkWidgetsPage::Reset()
186 {
187 m_label->SetValue(m_hyperlink->GetLabel());
188 m_url->SetValue(m_hyperlink->GetURL());
189 }
190
191 void HyperlinkWidgetsPage::CreateHyperlink()
192 {
193 const wxString label = m_hyperlink->GetLabel();
194 const wxString url = m_hyperlink->GetURL();
195
196 size_t count = m_sizerHyperlink->GetChildren().GetCount();
197 for ( size_t n = 0; n < count; n++ )
198 {
199 m_sizerHyperlink->Remove(0);
200 }
201
202 delete m_hyperlink;
203
204 m_hyperlink = new wxHyperlinkCtrl(this,
205 HyperlinkPage_Ctrl,
206 label,
207 url);
208
209 m_sizerHyperlink->Add(0, 0, 1, wxCENTRE);
210 m_sizerHyperlink->Add(m_visit, 0, wxCENTRE);
211 m_sizerHyperlink->Add(m_hyperlink, 0, wxCENTRE);
212 m_sizerHyperlink->Add(m_fun, 0, wxCENTRE);
213 m_sizerHyperlink->Add(0, 0, 1, wxCENTRE);
214 m_sizerHyperlink->Layout();
215 }
216
217 // ----------------------------------------------------------------------------
218 // event handlers
219 // ----------------------------------------------------------------------------
220
221 void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
222 {
223 Reset();
224
225 CreateHyperlink();
226 }
227
228 void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
229 {
230 m_hyperlink->SetLabel(m_label->GetValue());
231 CreateHyperlink();
232 }
233
234 void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent& WXUNUSED(event))
235 {
236 m_hyperlink->SetURL(m_url->GetValue());
237 CreateHyperlink();
238 }
239
240 #endif // wxUSE_HYPERLINKCTRL