]>
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
8 // Copyright: (c) 2003 wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_HYPERLINKCTRL
29 // for all others, include the necessary headers
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"
45 #include "wx/hyperlink.h"
49 #include "icons/hyperlnk.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 HyperlinkPage_Reset
= wxID_HIGHEST
,
59 HyperlinkPage_SetLabel
,
64 // alignment radiobox indices
73 // ----------------------------------------------------------------------------
74 // CheckBoxWidgetsPage
75 // ----------------------------------------------------------------------------
77 class HyperlinkWidgetsPage
: public WidgetsPage
80 HyperlinkWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
81 virtual ~HyperlinkWidgetsPage() {}
83 virtual wxControl
*GetWidget() const { return m_hyperlink
; }
84 virtual void RecreateWidget() { CreateHyperlink(); }
86 // lazy creation of the content
87 virtual void CreateContent();
91 void OnButtonSetLabel(wxCommandEvent
& event
);
92 void OnButtonSetURL(wxCommandEvent
& event
);
94 void OnButtonReset(wxCommandEvent
& event
);
95 void OnAlignment(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 wxHyperlinkCtrl
*m_hyperlink
;
109 wxHyperlinkCtrl
*m_hyperlinkLong
;
114 wxStaticText
*m_visit
;
117 // the text entries for command parameters
118 wxTextCtrl
*m_textLabel
;
120 wxRadioBox
*m_radioAlignMode
;
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage
)
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 BEGIN_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
)
136 EVT_RADIOBOX(wxID_ANY
, HyperlinkWidgetsPage::OnAlignment
)
139 // ============================================================================
141 // ============================================================================
143 IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage
, wxT("Hyperlink"),
147 HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl
*book
,
148 wxImageList
*imaglist
)
149 :WidgetsPage(book
, imaglist
, hyperlnk_xpm
)
153 void HyperlinkWidgetsPage::CreateContent()
155 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
158 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Hyperlink details"));
160 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
162 sizerLeft
->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel
, wxT("Set &Label"), wxID_ANY
, &m_label
),
163 0, wxALL
| wxALIGN_RIGHT
, 5 );
165 sizerLeft
->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL
, wxT("Set &URL"), wxID_ANY
, &m_url
),
166 0, wxALL
| wxALIGN_RIGHT
, 5 );
168 static const wxString alignments
[] =
174 wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments
) == Align_Max
,
177 m_radioAlignMode
= new wxRadioBox(this, wxID_ANY
, wxT("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);
187 wxSizer
*szHyperlinkLong
= new wxBoxSizer(wxVERTICAL
);
188 wxSizer
*szHyperlink
= new wxBoxSizer(wxHORIZONTAL
);
190 m_visit
= new wxStaticText(this, wxID_ANY
, wxT("Visit "));
192 m_hyperlink
= new wxHyperlinkCtrl(this,
194 wxT("wxWidgets website"),
195 wxT("www.wxwidgets.org"));
197 m_fun
= new wxStaticText(this, wxID_ANY
, wxT(" for fun!"));
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);
206 m_hyperlinkLong
= new wxHyperlinkCtrl(this,
208 wxT("This is a long hyperlink"),
209 wxT("www.wxwidgets.org"));
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
);
218 // the 3 panes panes compose the window
219 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
220 sizerTop
->Add(szHyperlinkLong
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
222 // final initializations
228 void HyperlinkWidgetsPage::Reset()
230 m_label
->SetValue(m_hyperlink
->GetLabel());
231 m_url
->SetValue(m_hyperlink
->GetURL());
234 void HyperlinkWidgetsPage::CreateHyperlink()
236 const wxString label
= m_hyperlink
->GetLabel();
237 const wxString url
= m_hyperlink
->GetURL();
239 wxHyperlinkCtrl
*hyp
= new wxHyperlinkCtrl(this,
244 // update sizer's child window
245 GetSizer()->Replace(m_hyperlink
, hyp
, true);
247 // update our pointer
251 // relayout the sizer
252 GetSizer()->Layout();
255 void HyperlinkWidgetsPage::CreateHyperlinkLong(long style
)
257 style
= (wxHL_DEFAULT_STYLE
& ~wxHL_ALIGN_CENTRE
)|style
;
258 wxHyperlinkCtrl
*hyp
= new wxHyperlinkCtrl(this,
260 wxT("This is a long hyperlink"),
261 wxT("www.wxwidgets.org"),
266 // update sizer's child window
267 GetSizer()->Replace(m_hyperlinkLong
, hyp
, true);
269 // update our pointer
270 delete m_hyperlinkLong
;
271 m_hyperlinkLong
= hyp
;
273 // relayout the sizer
274 GetSizer()->Layout();
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
288 void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
290 m_hyperlink
->SetLabel(m_label
->GetValue());
294 void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent
& WXUNUSED(event
))
296 m_hyperlink
->SetURL(m_url
->GetValue());
300 void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent
& WXUNUSED(event
))
303 switch ( m_radioAlignMode
->GetSelection() )
307 wxFAIL_MSG( wxT("unknown alignment") );
311 addstyle
= wxHL_ALIGN_LEFT
;
315 addstyle
= wxHL_ALIGN_CENTRE
;
319 addstyle
= wxHL_ALIGN_RIGHT
;
323 CreateHyperlinkLong(addstyle
);
326 #endif // wxUSE_HYPERLINKCTRL