]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/hyperlnk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxHyperlinkCtrl
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
7 // Copyright: (c) 2003 wxWindows team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_HYPERLINKCTRL
28 // for all others, include the necessary headers
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"
44 #include "wx/hyperlink.h"
48 #include "icons/hyperlnk.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 HyperlinkPage_Reset
= wxID_HIGHEST
,
58 HyperlinkPage_SetLabel
,
63 // alignment radiobox indices
72 // ----------------------------------------------------------------------------
73 // CheckBoxWidgetsPage
74 // ----------------------------------------------------------------------------
76 class HyperlinkWidgetsPage
: public WidgetsPage
79 HyperlinkWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
80 virtual ~HyperlinkWidgetsPage() {}
82 virtual wxControl
*GetWidget() const { return m_hyperlink
; }
83 virtual void RecreateWidget() { CreateHyperlink(); }
85 // lazy creation of the content
86 virtual void CreateContent();
90 void OnButtonSetLabel(wxCommandEvent
& event
);
91 void OnButtonSetURL(wxCommandEvent
& event
);
93 void OnButtonReset(wxCommandEvent
& event
);
94 void OnAlignment(wxCommandEvent
& event
);
95 void OnGeneric(wxCommandEvent
& event
);
97 // reset the control parameters
100 // (re)create the hyperctrl
101 void CreateHyperlink();
102 void CreateHyperlinkLong(long);
107 // the checkbox itself and the sizer it is in
108 wxGenericHyperlinkCtrl
*m_hyperlink
;
109 wxGenericHyperlinkCtrl
*m_hyperlinkLong
;
114 wxStaticText
*m_visit
;
117 // the text entries for command parameters
118 wxTextCtrl
*m_textLabel
;
120 wxRadioBox
*m_radioAlignMode
;
121 wxCheckBox
*m_checkGeneric
;
124 DECLARE_EVENT_TABLE()
125 DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage
)
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
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
)
137 EVT_RADIOBOX(wxID_ANY
, HyperlinkWidgetsPage::OnAlignment
)
138 EVT_CHECKBOX(wxID_ANY
, HyperlinkWidgetsPage::OnGeneric
)
141 // ============================================================================
143 // ============================================================================
145 IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage
, wxT("Hyperlink"),
149 HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl
*book
,
150 wxImageList
*imaglist
)
151 :WidgetsPage(book
, imaglist
, hyperlnk_xpm
)
155 void HyperlinkWidgetsPage::CreateContent()
157 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
160 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Hyperlink details"));
162 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
164 sizerLeft
->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel
, wxT("Set &Label"), wxID_ANY
, &m_label
),
165 0, wxALL
| wxALIGN_RIGHT
, 5 );
167 sizerLeft
->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL
, wxT("Set &URL"), wxID_ANY
, &m_url
),
168 0, wxALL
| wxALIGN_RIGHT
, 5 );
170 static const wxString alignments
[] =
176 wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments
) == Align_Max
,
179 m_radioAlignMode
= new wxRadioBox(this, wxID_ANY
, wxT("alignment"),
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);
186 m_checkGeneric
= new wxCheckBox(this, wxID_ANY
, wxT("Use generic version"),
187 wxDefaultPosition
, wxDefaultSize
);
188 sizerLeft
->Add(m_checkGeneric
, 0, wxALL
|wxGROW
, 5);
191 wxSizer
*szHyperlinkLong
= new wxBoxSizer(wxVERTICAL
);
192 wxSizer
*szHyperlink
= new wxBoxSizer(wxHORIZONTAL
);
194 m_visit
= new wxStaticText(this, wxID_ANY
, wxT("Visit "));
196 if (m_checkGeneric
->IsChecked())
198 m_hyperlink
= new wxGenericHyperlinkCtrl(this,
200 wxT("wxWidgets website"),
201 wxT("www.wxwidgets.org"));
205 m_hyperlink
= new wxHyperlinkCtrl(this,
207 wxT("wxWidgets website"),
208 wxT("www.wxwidgets.org"));
211 m_fun
= new wxStaticText(this, wxID_ANY
, wxT(" for fun!"));
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);
220 if (m_checkGeneric
->IsChecked())
222 m_hyperlinkLong
= new wxGenericHyperlinkCtrl(this,
224 wxT("This is a long hyperlink"),
225 wxT("www.wxwidgets.org"));
229 m_hyperlinkLong
= new wxHyperlinkCtrl(this,
231 wxT("This is a long hyperlink"),
232 wxT("www.wxwidgets.org"));
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
);
242 // the 3 panes panes compose the window
243 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
244 sizerTop
->Add(szHyperlinkLong
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
246 // final initializations
252 void HyperlinkWidgetsPage::Reset()
254 m_label
->SetValue(m_hyperlink
->GetLabel());
255 m_url
->SetValue(m_hyperlink
->GetURL());
258 void HyperlinkWidgetsPage::CreateHyperlink()
260 const wxString label
= m_hyperlink
->GetLabel();
261 const wxString url
= m_hyperlink
->GetURL();
263 wxGenericHyperlinkCtrl
*hyp
;
264 if (m_checkGeneric
->IsChecked())
266 hyp
= new wxGenericHyperlinkCtrl(this,
273 hyp
= new wxHyperlinkCtrl(this,
279 // update sizer's child window
280 GetSizer()->Replace(m_hyperlink
, hyp
, true);
282 // update our pointer
286 // relayout the sizer
287 GetSizer()->Layout();
290 void HyperlinkWidgetsPage::CreateHyperlinkLong(long style
)
292 style
= (wxHL_DEFAULT_STYLE
& ~wxHL_ALIGN_CENTRE
)|style
;
294 wxGenericHyperlinkCtrl
*hyp
;
295 if (m_checkGeneric
->IsChecked())
297 hyp
= new wxGenericHyperlinkCtrl(this,
299 wxT("This is a long hyperlink"),
300 wxT("www.wxwidgets.org"),
307 hyp
= new wxHyperlinkCtrl(this,
309 wxT("This is a long hyperlink"),
310 wxT("www.wxwidgets.org"),
316 // update sizer's child window
317 GetSizer()->Replace(m_hyperlinkLong
, hyp
, true);
319 // update our pointer
320 delete m_hyperlinkLong
;
321 m_hyperlinkLong
= hyp
;
323 // relayout the sizer
324 GetSizer()->Layout();
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
338 void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
340 m_hyperlink
->SetLabel(m_label
->GetValue());
344 void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent
& WXUNUSED(event
))
346 m_hyperlink
->SetURL(m_url
->GetValue());
350 void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent
& WXUNUSED(event
))
353 switch ( m_radioAlignMode
->GetSelection() )
357 wxFAIL_MSG( wxT("unknown alignment") );
361 addstyle
= wxHL_ALIGN_LEFT
;
365 addstyle
= wxHL_ALIGN_CENTRE
;
369 addstyle
= wxHL_ALIGN_RIGHT
;
373 CreateHyperlinkLong(addstyle
);
376 void HyperlinkWidgetsPage::OnGeneric(wxCommandEvent
& event
)
382 #endif // wxUSE_HYPERLINKCTRL