]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/widgets/hyperlnk.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[wxWidgets.git] / samples / widgets / hyperlnk.cpp
... / ...
CommitLineData
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// Licence: wxWindows licence
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
56enum
57{
58 HyperlinkPage_Reset = wxID_HIGHEST,
59 HyperlinkPage_SetLabel,
60 HyperlinkPage_SetURL,
61 HyperlinkPage_Ctrl
62};
63
64// alignment radiobox indices
65enum
66{
67 Align_Left,
68 Align_Centre,
69 Align_Right,
70 Align_Max
71};
72
73// ----------------------------------------------------------------------------
74// CheckBoxWidgetsPage
75// ----------------------------------------------------------------------------
76
77class HyperlinkWidgetsPage : public WidgetsPage
78{
79public:
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
89protected:
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 void OnGeneric(wxCommandEvent& event);
97
98 // reset the control parameters
99 void Reset();
100
101 // (re)create the hyperctrl
102 void CreateHyperlink();
103 void CreateHyperlinkLong(long);
104
105 // the controls
106 // ------------
107
108 // the checkbox itself and the sizer it is in
109 wxGenericHyperlinkCtrl *m_hyperlink;
110 wxGenericHyperlinkCtrl *m_hyperlinkLong;
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
121 wxRadioBox *m_radioAlignMode;
122 wxCheckBox *m_checkGeneric;
123
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)
137
138 EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment)
139 EVT_CHECKBOX(wxID_ANY, HyperlinkWidgetsPage::OnGeneric)
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)
152 :WidgetsPage(book, imaglist, hyperlnk_xpm)
153{
154}
155
156void HyperlinkWidgetsPage::CreateContent()
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
171 static const wxString alignments[] =
172 {
173 wxT("&left"),
174 wxT("&centre"),
175 wxT("&right")
176 };
177 wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments) == Align_Max,
178 AlignMismatch );
179
180 m_radioAlignMode = new wxRadioBox(this, wxID_ANY, wxT("alignment"),
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
187 m_checkGeneric = new wxCheckBox(this, wxID_ANY, wxT("Use generic version"),
188 wxDefaultPosition, wxDefaultSize);
189 sizerLeft->Add(m_checkGeneric, 0, wxALL|wxGROW, 5);
190
191 // right pane
192 wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL);
193 wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL);
194
195 m_visit = new wxStaticText(this, wxID_ANY, wxT("Visit "));
196
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 }
211
212 m_fun = new wxStaticText(this, wxID_ANY, wxT(" for fun!"));
213
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
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 }
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
242
243 // the 3 panes panes compose the window
244 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
245 sizerTop->Add(szHyperlinkLong, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
246
247 // final initializations
248 Reset();
249
250 SetSizer(sizerTop);
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
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 }
279
280 // update sizer's child window
281 GetSizer()->Replace(m_hyperlink, hyp, true);
282
283 // update our pointer
284 delete m_hyperlink;
285 m_hyperlink = hyp;
286
287 // relayout the sizer
288 GetSizer()->Layout();
289}
290
291void HyperlinkWidgetsPage::CreateHyperlinkLong(long style)
292{
293 style = (wxHL_DEFAULT_STYLE & ~wxHL_ALIGN_CENTRE)|style;
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 }
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();
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
351void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent& WXUNUSED(event))
352{
353 long addstyle;
354 switch ( m_radioAlignMode->GetSelection() )
355 {
356 default:
357 case Align_Max:
358 wxFAIL_MSG( wxT("unknown alignment") );
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
377void HyperlinkWidgetsPage::OnGeneric(wxCommandEvent& event)
378{
379 CreateHyperlink();
380 OnAlignment(event);
381}
382
383#endif // wxUSE_HYPERLINKCTRL