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