]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/hyperlnk.cpp
add convenient GetViewStart() and Scroll() overloads taking wxPoint instead of 2...
[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
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
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);
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
108 wxHyperlinkCtrl *m_hyperlink;
914f5157 109 wxHyperlinkCtrl *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
VZ
120 wxRadioBox *m_radioAlignMode;
121
f9300d42
WS
122private:
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage)
125};
126
127// ----------------------------------------------------------------------------
128// event tables
129// ----------------------------------------------------------------------------
130
131BEGIN_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)
914f5157
VZ
135
136 EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment)
f9300d42
WS
137END_EVENT_TABLE()
138
139// ============================================================================
140// implementation
141// ============================================================================
142
143IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage, wxT("Hyperlink"),
144 GENERIC_CTRLS
145 );
146
147HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book,
148 wxImageList *imaglist)
453535a7
WS
149 :WidgetsPage(book, imaglist, hyperlnk_xpm)
150{
151}
152
153void HyperlinkWidgetsPage::CreateContent()
f9300d42
WS
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
914f5157
VZ
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
f9300d42 186 // right pane
914f5157
VZ
187 wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL);
188 wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL);
f9300d42
WS
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
914f5157
VZ
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
f9300d42
WS
217
218 // the 3 panes panes compose the window
219 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
914f5157 220 sizerTop->Add(szHyperlinkLong, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
f9300d42
WS
221
222 // final initializations
223 Reset();
224
225 SetSizer(sizerTop);
f9300d42
WS
226}
227
228void HyperlinkWidgetsPage::Reset()
229{
230 m_label->SetValue(m_hyperlink->GetLabel());
231 m_url->SetValue(m_hyperlink->GetURL());
232}
233
234void HyperlinkWidgetsPage::CreateHyperlink()
235{
236 const wxString label = m_hyperlink->GetLabel();
237 const wxString url = m_hyperlink->GetURL();
238
914f5157
VZ
239 wxHyperlinkCtrl *hyp = new wxHyperlinkCtrl(this,
240 HyperlinkPage_Ctrl,
241 label,
242 url);
f9300d42 243
914f5157
VZ
244 // update sizer's child window
245 GetSizer()->Replace(m_hyperlink, hyp, true);
246
247 // update our pointer
f9300d42 248 delete m_hyperlink;
914f5157 249 m_hyperlink = hyp;
f9300d42 250
914f5157
VZ
251 // relayout the sizer
252 GetSizer()->Layout();
253}
254
255void HyperlinkWidgetsPage::CreateHyperlinkLong(long style)
256{
257 style = (wxHL_DEFAULT_STYLE & ~wxHL_ALIGN_CENTRE)|style;
258 wxHyperlinkCtrl *hyp = new wxHyperlinkCtrl(this,
259 wxID_ANY,
260 wxT("This is a long hyperlink"),
261 wxT("www.wxwidgets.org"),
262 wxDefaultPosition,
263 wxDefaultSize,
264 style);
265
266 // update sizer's child window
267 GetSizer()->Replace(m_hyperlinkLong, hyp, true);
268
269 // update our pointer
270 delete m_hyperlinkLong;
271 m_hyperlinkLong = hyp;
272
273 // relayout the sizer
274 GetSizer()->Layout();
f9300d42
WS
275}
276
277// ----------------------------------------------------------------------------
278// event handlers
279// ----------------------------------------------------------------------------
280
281void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
282{
283 Reset();
284
285 CreateHyperlink();
286}
287
288void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
289{
290 m_hyperlink->SetLabel(m_label->GetValue());
291 CreateHyperlink();
292}
293
294void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent& WXUNUSED(event))
295{
296 m_hyperlink->SetURL(m_url->GetValue());
297 CreateHyperlink();
298}
299
914f5157
VZ
300void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent& WXUNUSED(event))
301{
302 long addstyle;
303 switch ( m_radioAlignMode->GetSelection() )
304 {
305 default:
306 case Align_Max:
307 wxFAIL_MSG( _T("unknown alignment") );
308 // fall through
309
310 case Align_Left:
311 addstyle = wxHL_ALIGN_LEFT;
312 break;
313
314 case Align_Centre:
315 addstyle = wxHL_ALIGN_CENTRE;
316 break;
317
318 case Align_Right:
319 addstyle = wxHL_ALIGN_RIGHT;
320 break;
321 }
322
323 CreateHyperlinkLong(addstyle);
324}
325
f9300d42 326#endif // wxUSE_HYPERLINKCTRL