]>
Commit | Line | Data |
---|---|---|
f9300d42 WS |
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 | |
f9300d42 | 7 | // Copyright: (c) 2003 wxWindows team |
526954c5 | 8 | // Licence: wxWindows licence |
f9300d42 WS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_HYPERLINKCTRL | |
27 | ||
28 | // for all others, include the necessary headers | |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #include "wx/log.h" | |
32 | ||
33 | #include "wx/bitmap.h" | |
34 | #include "wx/button.h" | |
35 | #include "wx/checkbox.h" | |
36 | #include "wx/radiobox.h" | |
37 | #include "wx/statbox.h" | |
38 | #include "wx/stattext.h" | |
39 | #include "wx/textctrl.h" | |
40 | ||
41 | #include "wx/sizer.h" | |
42 | #endif | |
43 | ||
38ffe8cd | 44 | #include "wx/hyperlink.h" |
f9300d42 WS |
45 | |
46 | #include "widgets.h" | |
47 | ||
48 | #include "icons/hyperlnk.xpm" | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // constants | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | // control ids | |
55 | enum | |
56 | { | |
57 | HyperlinkPage_Reset = wxID_HIGHEST, | |
58 | HyperlinkPage_SetLabel, | |
59 | HyperlinkPage_SetURL, | |
60 | HyperlinkPage_Ctrl | |
61 | }; | |
62 | ||
914f5157 VZ |
63 | // alignment radiobox indices |
64 | enum | |
65 | { | |
66 | Align_Left, | |
67 | Align_Centre, | |
68 | Align_Right, | |
69 | Align_Max | |
70 | }; | |
71 | ||
f9300d42 WS |
72 | // ---------------------------------------------------------------------------- |
73 | // CheckBoxWidgetsPage | |
74 | // ---------------------------------------------------------------------------- | |
75 | ||
76 | class HyperlinkWidgetsPage : public WidgetsPage | |
77 | { | |
78 | public: | |
79 | HyperlinkWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist); | |
453535a7 | 80 | virtual ~HyperlinkWidgetsPage() {} |
f9300d42 WS |
81 | |
82 | virtual wxControl *GetWidget() const { return m_hyperlink; } | |
83 | virtual void RecreateWidget() { CreateHyperlink(); } | |
84 | ||
453535a7 WS |
85 | // lazy creation of the content |
86 | virtual void CreateContent(); | |
87 | ||
f9300d42 WS |
88 | protected: |
89 | // event handlers | |
90 | void OnButtonSetLabel(wxCommandEvent& event); | |
91 | void OnButtonSetURL(wxCommandEvent& event); | |
92 | ||
93 | void OnButtonReset(wxCommandEvent& event); | |
914f5157 | 94 | void OnAlignment(wxCommandEvent& event); |
bc927c9a | 95 | void OnGeneric(wxCommandEvent& event); |
f9300d42 WS |
96 | |
97 | // reset the control parameters | |
98 | void Reset(); | |
99 | ||
100 | // (re)create the hyperctrl | |
101 | void CreateHyperlink(); | |
914f5157 | 102 | void CreateHyperlinkLong(long); |
f9300d42 WS |
103 | |
104 | // the controls | |
105 | // ------------ | |
106 | ||
107 | // the checkbox itself and the sizer it is in | |
bc927c9a VZ |
108 | wxGenericHyperlinkCtrl *m_hyperlink; |
109 | wxGenericHyperlinkCtrl *m_hyperlinkLong; | |
f9300d42 WS |
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 | ||
914f5157 | 120 | wxRadioBox *m_radioAlignMode; |
bc927c9a | 121 | wxCheckBox *m_checkGeneric; |
914f5157 | 122 | |
f9300d42 WS |
123 | private: |
124 | DECLARE_EVENT_TABLE() | |
125 | DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage) | |
126 | }; | |
127 | ||
128 | // ---------------------------------------------------------------------------- | |
129 | // event tables | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | BEGIN_EVENT_TABLE(HyperlinkWidgetsPage, WidgetsPage) | |
133 | EVT_BUTTON(HyperlinkPage_Reset, HyperlinkWidgetsPage::OnButtonReset) | |
134 | EVT_BUTTON(HyperlinkPage_SetLabel, HyperlinkWidgetsPage::OnButtonSetLabel) | |
135 | EVT_BUTTON(HyperlinkPage_SetURL, HyperlinkWidgetsPage::OnButtonSetURL) | |
914f5157 VZ |
136 | |
137 | EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment) | |
bc927c9a | 138 | EVT_CHECKBOX(wxID_ANY, HyperlinkWidgetsPage::OnGeneric) |
f9300d42 WS |
139 | END_EVENT_TABLE() |
140 | ||
141 | // ============================================================================ | |
142 | // implementation | |
143 | // ============================================================================ | |
144 | ||
145 | IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage, wxT("Hyperlink"), | |
146 | GENERIC_CTRLS | |
147 | ); | |
148 | ||
149 | HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book, | |
150 | wxImageList *imaglist) | |
453535a7 WS |
151 | :WidgetsPage(book, imaglist, hyperlnk_xpm) |
152 | { | |
153 | } | |
154 | ||
155 | void HyperlinkWidgetsPage::CreateContent() | |
f9300d42 WS |
156 | { |
157 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
158 | ||
159 | // left pane | |
160 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Hyperlink details")); | |
161 | ||
162 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
163 | ||
164 | sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel , wxT("Set &Label"), wxID_ANY, &m_label ), | |
165 | 0, wxALL | wxALIGN_RIGHT , 5 ); | |
166 | ||
167 | sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL , wxT("Set &URL"), wxID_ANY, &m_url ), | |
168 | 0, wxALL | wxALIGN_RIGHT , 5 ); | |
169 | ||
914f5157 VZ |
170 | static const wxString alignments[] = |
171 | { | |
9a83f860 VZ |
172 | wxT("&left"), |
173 | wxT("¢re"), | |
174 | wxT("&right") | |
914f5157 VZ |
175 | }; |
176 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments) == Align_Max, | |
177 | AlignMismatch ); | |
178 | ||
9a83f860 | 179 | m_radioAlignMode = new wxRadioBox(this, wxID_ANY, wxT("alignment"), |
914f5157 VZ |
180 | wxDefaultPosition, wxDefaultSize, |
181 | WXSIZEOF(alignments), alignments); | |
182 | m_radioAlignMode->SetSelection(1); // start with "centre" selected since | |
183 | // wxHL_DEFAULT_STYLE contains wxHL_ALIGN_CENTRE | |
184 | sizerLeft->Add(m_radioAlignMode, 0, wxALL|wxGROW, 5); | |
185 | ||
bc927c9a VZ |
186 | m_checkGeneric = new wxCheckBox(this, wxID_ANY, wxT("Use generic version"), |
187 | wxDefaultPosition, wxDefaultSize); | |
188 | sizerLeft->Add(m_checkGeneric, 0, wxALL|wxGROW, 5); | |
914f5157 | 189 | |
f9300d42 | 190 | // right pane |
914f5157 VZ |
191 | wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL); |
192 | wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL); | |
f9300d42 WS |
193 | |
194 | m_visit = new wxStaticText(this, wxID_ANY, wxT("Visit ")); | |
195 | ||
bc927c9a VZ |
196 | if (m_checkGeneric->IsChecked()) |
197 | { | |
198 | m_hyperlink = new wxGenericHyperlinkCtrl(this, | |
199 | HyperlinkPage_Ctrl, | |
200 | wxT("wxWidgets website"), | |
201 | wxT("www.wxwidgets.org")); | |
202 | } | |
203 | else | |
204 | { | |
205 | m_hyperlink = new wxHyperlinkCtrl(this, | |
206 | HyperlinkPage_Ctrl, | |
207 | wxT("wxWidgets website"), | |
208 | wxT("www.wxwidgets.org")); | |
209 | } | |
f9300d42 WS |
210 | |
211 | m_fun = new wxStaticText(this, wxID_ANY, wxT(" for fun!")); | |
212 | ||
914f5157 VZ |
213 | szHyperlink->Add(0, 0, 1, wxCENTRE); |
214 | szHyperlink->Add(m_visit, 0, wxCENTRE); | |
215 | szHyperlink->Add(m_hyperlink, 0, wxCENTRE); | |
216 | szHyperlink->Add(m_fun, 0, wxCENTRE); | |
217 | szHyperlink->Add(0, 0, 1, wxCENTRE); | |
218 | szHyperlink->SetMinSize(150, 0); | |
219 | ||
bc927c9a VZ |
220 | if (m_checkGeneric->IsChecked()) |
221 | { | |
222 | m_hyperlinkLong = new wxGenericHyperlinkCtrl(this, | |
223 | wxID_ANY, | |
224 | wxT("This is a long hyperlink"), | |
225 | wxT("www.wxwidgets.org")); | |
226 | } | |
227 | else | |
228 | { | |
229 | m_hyperlinkLong = new wxHyperlinkCtrl(this, | |
230 | wxID_ANY, | |
231 | wxT("This is a long hyperlink"), | |
232 | wxT("www.wxwidgets.org")); | |
233 | } | |
914f5157 VZ |
234 | |
235 | szHyperlinkLong->Add(0, 0, 1, wxCENTRE); | |
236 | szHyperlinkLong->Add(szHyperlink, 0, wxCENTRE|wxGROW); | |
237 | szHyperlinkLong->Add(0, 0, 1, wxCENTRE); | |
238 | szHyperlinkLong->Add(m_hyperlinkLong, 0, wxGROW); | |
239 | szHyperlinkLong->Add(0, 0, 1, wxCENTRE); | |
240 | ||
f9300d42 WS |
241 | |
242 | // the 3 panes panes compose the window | |
243 | sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10); | |
914f5157 | 244 | sizerTop->Add(szHyperlinkLong, 1, wxGROW | (wxALL & ~wxRIGHT), 10); |
f9300d42 WS |
245 | |
246 | // final initializations | |
247 | Reset(); | |
248 | ||
249 | SetSizer(sizerTop); | |
f9300d42 WS |
250 | } |
251 | ||
252 | void HyperlinkWidgetsPage::Reset() | |
253 | { | |
254 | m_label->SetValue(m_hyperlink->GetLabel()); | |
255 | m_url->SetValue(m_hyperlink->GetURL()); | |
256 | } | |
257 | ||
258 | void HyperlinkWidgetsPage::CreateHyperlink() | |
259 | { | |
260 | const wxString label = m_hyperlink->GetLabel(); | |
261 | const wxString url = m_hyperlink->GetURL(); | |
262 | ||
bc927c9a VZ |
263 | wxGenericHyperlinkCtrl *hyp; |
264 | if (m_checkGeneric->IsChecked()) | |
265 | { | |
266 | hyp = new wxGenericHyperlinkCtrl(this, | |
267 | HyperlinkPage_Ctrl, | |
268 | label, | |
269 | url); | |
270 | } | |
271 | else | |
272 | { | |
273 | hyp = new wxHyperlinkCtrl(this, | |
274 | HyperlinkPage_Ctrl, | |
275 | label, | |
276 | url); | |
277 | } | |
f9300d42 | 278 | |
914f5157 VZ |
279 | // update sizer's child window |
280 | GetSizer()->Replace(m_hyperlink, hyp, true); | |
281 | ||
282 | // update our pointer | |
f9300d42 | 283 | delete m_hyperlink; |
914f5157 | 284 | m_hyperlink = hyp; |
f9300d42 | 285 | |
914f5157 VZ |
286 | // relayout the sizer |
287 | GetSizer()->Layout(); | |
288 | } | |
289 | ||
290 | void HyperlinkWidgetsPage::CreateHyperlinkLong(long style) | |
291 | { | |
292 | style = (wxHL_DEFAULT_STYLE & ~wxHL_ALIGN_CENTRE)|style; | |
bc927c9a VZ |
293 | |
294 | wxGenericHyperlinkCtrl *hyp; | |
295 | if (m_checkGeneric->IsChecked()) | |
296 | { | |
297 | hyp = new wxGenericHyperlinkCtrl(this, | |
298 | wxID_ANY, | |
299 | wxT("This is a long hyperlink"), | |
300 | wxT("www.wxwidgets.org"), | |
301 | wxDefaultPosition, | |
302 | wxDefaultSize, | |
303 | style); | |
304 | } | |
305 | else | |
306 | { | |
307 | hyp = new wxHyperlinkCtrl(this, | |
308 | wxID_ANY, | |
309 | wxT("This is a long hyperlink"), | |
310 | wxT("www.wxwidgets.org"), | |
311 | wxDefaultPosition, | |
312 | wxDefaultSize, | |
313 | style); | |
314 | } | |
914f5157 VZ |
315 | |
316 | // update sizer's child window | |
317 | GetSizer()->Replace(m_hyperlinkLong, hyp, true); | |
318 | ||
319 | // update our pointer | |
320 | delete m_hyperlinkLong; | |
321 | m_hyperlinkLong = hyp; | |
322 | ||
323 | // relayout the sizer | |
324 | GetSizer()->Layout(); | |
f9300d42 WS |
325 | } |
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // event handlers | |
329 | // ---------------------------------------------------------------------------- | |
330 | ||
331 | void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
332 | { | |
333 | Reset(); | |
334 | ||
335 | CreateHyperlink(); | |
336 | } | |
337 | ||
338 | void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event)) | |
339 | { | |
340 | m_hyperlink->SetLabel(m_label->GetValue()); | |
341 | CreateHyperlink(); | |
342 | } | |
343 | ||
344 | void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent& WXUNUSED(event)) | |
345 | { | |
346 | m_hyperlink->SetURL(m_url->GetValue()); | |
347 | CreateHyperlink(); | |
348 | } | |
349 | ||
914f5157 VZ |
350 | void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent& WXUNUSED(event)) |
351 | { | |
352 | long addstyle; | |
353 | switch ( m_radioAlignMode->GetSelection() ) | |
354 | { | |
355 | default: | |
356 | case Align_Max: | |
9a83f860 | 357 | wxFAIL_MSG( wxT("unknown alignment") ); |
914f5157 VZ |
358 | // fall through |
359 | ||
360 | case Align_Left: | |
361 | addstyle = wxHL_ALIGN_LEFT; | |
362 | break; | |
363 | ||
364 | case Align_Centre: | |
365 | addstyle = wxHL_ALIGN_CENTRE; | |
366 | break; | |
367 | ||
368 | case Align_Right: | |
369 | addstyle = wxHL_ALIGN_RIGHT; | |
370 | break; | |
371 | } | |
372 | ||
373 | CreateHyperlinkLong(addstyle); | |
374 | } | |
375 | ||
bc927c9a VZ |
376 | void HyperlinkWidgetsPage::OnGeneric(wxCommandEvent& event) |
377 | { | |
378 | CreateHyperlink(); | |
379 | OnAlignment(event); | |
380 | } | |
381 | ||
f9300d42 | 382 | #endif // wxUSE_HYPERLINKCTRL |