]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/hyperlnk.cpp
Fix mem leak.
[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 // alignment radiobox indices
65 enum
66 {
67 Align_Left,
68 Align_Centre,
69 Align_Right,
70 Align_Max
71 };
72
73 // ----------------------------------------------------------------------------
74 // CheckBoxWidgetsPage
75 // ----------------------------------------------------------------------------
76
77 class HyperlinkWidgetsPage : public WidgetsPage
78 {
79 public:
80 HyperlinkWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
81 virtual ~HyperlinkWidgetsPage() {}
82
83 virtual wxControl *GetWidget() const { return m_hyperlink; }
84 virtual void RecreateWidget() { CreateHyperlink(); }
85
86 // lazy creation of the content
87 virtual void CreateContent();
88
89 protected:
90 // event handlers
91 void OnButtonSetLabel(wxCommandEvent& event);
92 void OnButtonSetURL(wxCommandEvent& event);
93
94 void OnButtonReset(wxCommandEvent& event);
95 void OnAlignment(wxCommandEvent& event);
96
97 // reset the control parameters
98 void Reset();
99
100 // (re)create the hyperctrl
101 void CreateHyperlink();
102 void CreateHyperlinkLong(long);
103
104 // the controls
105 // ------------
106
107 // the checkbox itself and the sizer it is in
108 wxHyperlinkCtrl *m_hyperlink;
109 wxHyperlinkCtrl *m_hyperlinkLong;
110
111 wxTextCtrl *m_label;
112 wxTextCtrl *m_url;
113
114 wxStaticText *m_visit;
115 wxStaticText *m_fun;
116
117 // the text entries for command parameters
118 wxTextCtrl *m_textLabel;
119
120 wxRadioBox *m_radioAlignMode;
121
122 private:
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage)
125 };
126
127 // ----------------------------------------------------------------------------
128 // event tables
129 // ----------------------------------------------------------------------------
130
131 BEGIN_EVENT_TABLE(HyperlinkWidgetsPage, WidgetsPage)
132 EVT_BUTTON(HyperlinkPage_Reset, HyperlinkWidgetsPage::OnButtonReset)
133 EVT_BUTTON(HyperlinkPage_SetLabel, HyperlinkWidgetsPage::OnButtonSetLabel)
134 EVT_BUTTON(HyperlinkPage_SetURL, HyperlinkWidgetsPage::OnButtonSetURL)
135
136 EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment)
137 END_EVENT_TABLE()
138
139 // ============================================================================
140 // implementation
141 // ============================================================================
142
143 IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage, wxT("Hyperlink"),
144 GENERIC_CTRLS
145 );
146
147 HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book,
148 wxImageList *imaglist)
149 :WidgetsPage(book, imaglist, hyperlnk_xpm)
150 {
151 }
152
153 void HyperlinkWidgetsPage::CreateContent()
154 {
155 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
156
157 // left pane
158 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Hyperlink details"));
159
160 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
161
162 sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel , wxT("Set &Label"), wxID_ANY, &m_label ),
163 0, wxALL | wxALIGN_RIGHT , 5 );
164
165 sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL , wxT("Set &URL"), wxID_ANY, &m_url ),
166 0, wxALL | wxALIGN_RIGHT , 5 );
167
168 static const wxString alignments[] =
169 {
170 _T("&left"),
171 _T("&centre"),
172 _T("&right")
173 };
174 wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments) == Align_Max,
175 AlignMismatch );
176
177 m_radioAlignMode = new wxRadioBox(this, wxID_ANY, _T("alignment"),
178 wxDefaultPosition, wxDefaultSize,
179 WXSIZEOF(alignments), alignments);
180 m_radioAlignMode->SetSelection(1); // start with "centre" selected since
181 // wxHL_DEFAULT_STYLE contains wxHL_ALIGN_CENTRE
182 sizerLeft->Add(m_radioAlignMode, 0, wxALL|wxGROW, 5);
183
184
185
186 // right pane
187 wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL);
188 wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL);
189
190 m_visit = new wxStaticText(this, wxID_ANY, wxT("Visit "));
191
192 m_hyperlink = new wxHyperlinkCtrl(this,
193 HyperlinkPage_Ctrl,
194 wxT("wxWidgets website"),
195 wxT("www.wxwidgets.org"));
196
197 m_fun = new wxStaticText(this, wxID_ANY, wxT(" for fun!"));
198
199 szHyperlink->Add(0, 0, 1, wxCENTRE);
200 szHyperlink->Add(m_visit, 0, wxCENTRE);
201 szHyperlink->Add(m_hyperlink, 0, wxCENTRE);
202 szHyperlink->Add(m_fun, 0, wxCENTRE);
203 szHyperlink->Add(0, 0, 1, wxCENTRE);
204 szHyperlink->SetMinSize(150, 0);
205
206 m_hyperlinkLong = new wxHyperlinkCtrl(this,
207 wxID_ANY,
208 wxT("This is a long hyperlink"),
209 wxT("www.wxwidgets.org"));
210
211 szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
212 szHyperlinkLong->Add(szHyperlink, 0, wxCENTRE|wxGROW);
213 szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
214 szHyperlinkLong->Add(m_hyperlinkLong, 0, wxGROW);
215 szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
216
217
218 // the 3 panes panes compose the window
219 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
220 sizerTop->Add(szHyperlinkLong, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
221
222 // final initializations
223 Reset();
224
225 SetSizer(sizerTop);
226
227 sizerTop->Fit(this);
228 }
229
230 void HyperlinkWidgetsPage::Reset()
231 {
232 m_label->SetValue(m_hyperlink->GetLabel());
233 m_url->SetValue(m_hyperlink->GetURL());
234 }
235
236 void HyperlinkWidgetsPage::CreateHyperlink()
237 {
238 const wxString label = m_hyperlink->GetLabel();
239 const wxString url = m_hyperlink->GetURL();
240
241 wxHyperlinkCtrl *hyp = new wxHyperlinkCtrl(this,
242 HyperlinkPage_Ctrl,
243 label,
244 url);
245
246 // update sizer's child window
247 GetSizer()->Replace(m_hyperlink, hyp, true);
248
249 // update our pointer
250 delete m_hyperlink;
251 m_hyperlink = hyp;
252
253 // relayout the sizer
254 GetSizer()->Layout();
255 }
256
257 void HyperlinkWidgetsPage::CreateHyperlinkLong(long style)
258 {
259 style = (wxHL_DEFAULT_STYLE & ~wxHL_ALIGN_CENTRE)|style;
260 wxHyperlinkCtrl *hyp = new wxHyperlinkCtrl(this,
261 wxID_ANY,
262 wxT("This is a long hyperlink"),
263 wxT("www.wxwidgets.org"),
264 wxDefaultPosition,
265 wxDefaultSize,
266 style);
267
268 // update sizer's child window
269 GetSizer()->Replace(m_hyperlinkLong, hyp, true);
270
271 // update our pointer
272 delete m_hyperlinkLong;
273 m_hyperlinkLong = hyp;
274
275 // relayout the sizer
276 GetSizer()->Layout();
277 }
278
279 // ----------------------------------------------------------------------------
280 // event handlers
281 // ----------------------------------------------------------------------------
282
283 void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
284 {
285 Reset();
286
287 CreateHyperlink();
288 }
289
290 void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
291 {
292 m_hyperlink->SetLabel(m_label->GetValue());
293 CreateHyperlink();
294 }
295
296 void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent& WXUNUSED(event))
297 {
298 m_hyperlink->SetURL(m_url->GetValue());
299 CreateHyperlink();
300 }
301
302 void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent& WXUNUSED(event))
303 {
304 long addstyle;
305 switch ( m_radioAlignMode->GetSelection() )
306 {
307 default:
308 case Align_Max:
309 wxFAIL_MSG( _T("unknown alignment") );
310 // fall through
311
312 case Align_Left:
313 addstyle = wxHL_ALIGN_LEFT;
314 break;
315
316 case Align_Centre:
317 addstyle = wxHL_ALIGN_CENTRE;
318 break;
319
320 case Align_Right:
321 addstyle = wxHL_ALIGN_RIGHT;
322 break;
323 }
324
325 CreateHyperlinkLong(addstyle);
326 }
327
328 #endif // wxUSE_HYPERLINKCTRL