]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/hyperlnk.cpp
437dc9d5c2c44a140e8f4c1a3214472fa4fed751
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
);
96 void OnGeneric(wxCommandEvent
& event
);
98 // reset the control parameters
101 // (re)create the hyperctrl
102 void CreateHyperlink();
103 void CreateHyperlinkLong(long);
108 // the checkbox itself and the sizer it is in
109 wxGenericHyperlinkCtrl
*m_hyperlink
;
110 wxGenericHyperlinkCtrl
*m_hyperlinkLong
;
115 wxStaticText
*m_visit
;
118 // the text entries for command parameters
119 wxTextCtrl
*m_textLabel
;
121 wxRadioBox
*m_radioAlignMode
;
122 wxCheckBox
*m_checkGeneric
;
125 DECLARE_EVENT_TABLE()
126 DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage
)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 BEGIN_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
)
138 EVT_RADIOBOX(wxID_ANY
, HyperlinkWidgetsPage::OnAlignment
)
139 EVT_CHECKBOX(wxID_ANY
, HyperlinkWidgetsPage::OnGeneric
)
142 // ============================================================================
144 // ============================================================================
146 IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage
, wxT("Hyperlink"),
150 HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl
*book
,
151 wxImageList
*imaglist
)
152 :WidgetsPage(book
, imaglist
, hyperlnk_xpm
)
156 void HyperlinkWidgetsPage::CreateContent()
158 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
161 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Hyperlink details"));
163 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
165 sizerLeft
->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel
, wxT("Set &Label"), wxID_ANY
, &m_label
),
166 0, wxALL
| wxALIGN_RIGHT
, 5 );
168 sizerLeft
->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL
, wxT("Set &URL"), wxID_ANY
, &m_url
),
169 0, wxALL
| wxALIGN_RIGHT
, 5 );
171 static const wxString alignments
[] =
177 wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments
) == Align_Max
,
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);
187 m_checkGeneric
= new wxCheckBox(this, wxID_ANY
, wxT("Use generic version"),
188 wxDefaultPosition
, wxDefaultSize
);
189 sizerLeft
->Add(m_checkGeneric
, 0, wxALL
|wxGROW
, 5);
192 wxSizer
*szHyperlinkLong
= new wxBoxSizer(wxVERTICAL
);
193 wxSizer
*szHyperlink
= new wxBoxSizer(wxHORIZONTAL
);
195 m_visit
= new wxStaticText(this, wxID_ANY
, wxT("Visit "));
197 if (m_checkGeneric
->IsChecked())
199 m_hyperlink
= new wxGenericHyperlinkCtrl(this,
201 wxT("wxWidgets website"),
202 wxT("www.wxwidgets.org"));
206 m_hyperlink
= new wxHyperlinkCtrl(this,
208 wxT("wxWidgets website"),
209 wxT("www.wxwidgets.org"));
212 m_fun
= new wxStaticText(this, wxID_ANY
, wxT(" for fun!"));
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);
221 if (m_checkGeneric
->IsChecked())
223 m_hyperlinkLong
= new wxGenericHyperlinkCtrl(this,
225 wxT("This is a long hyperlink"),
226 wxT("www.wxwidgets.org"));
230 m_hyperlinkLong
= new wxHyperlinkCtrl(this,
232 wxT("This is a long hyperlink"),
233 wxT("www.wxwidgets.org"));
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
);
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);
247 // final initializations
253 void HyperlinkWidgetsPage::Reset()
255 m_label
->SetValue(m_hyperlink
->GetLabel());
256 m_url
->SetValue(m_hyperlink
->GetURL());
259 void HyperlinkWidgetsPage::CreateHyperlink()
261 const wxString label
= m_hyperlink
->GetLabel();
262 const wxString url
= m_hyperlink
->GetURL();
264 wxGenericHyperlinkCtrl
*hyp
;
265 if (m_checkGeneric
->IsChecked())
267 hyp
= new wxGenericHyperlinkCtrl(this,
274 hyp
= new wxHyperlinkCtrl(this,
280 // update sizer's child window
281 GetSizer()->Replace(m_hyperlink
, hyp
, true);
283 // update our pointer
287 // relayout the sizer
288 GetSizer()->Layout();
291 void HyperlinkWidgetsPage::CreateHyperlinkLong(long style
)
293 style
= (wxHL_DEFAULT_STYLE
& ~wxHL_ALIGN_CENTRE
)|style
;
295 wxGenericHyperlinkCtrl
*hyp
;
296 if (m_checkGeneric
->IsChecked())
298 hyp
= new wxGenericHyperlinkCtrl(this,
300 wxT("This is a long hyperlink"),
301 wxT("www.wxwidgets.org"),
308 hyp
= new wxHyperlinkCtrl(this,
310 wxT("This is a long hyperlink"),
311 wxT("www.wxwidgets.org"),
317 // update sizer's child window
318 GetSizer()->Replace(m_hyperlinkLong
, hyp
, true);
320 // update our pointer
321 delete m_hyperlinkLong
;
322 m_hyperlinkLong
= hyp
;
324 // relayout the sizer
325 GetSizer()->Layout();
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
339 void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
341 m_hyperlink
->SetLabel(m_label
->GetValue());
345 void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent
& WXUNUSED(event
))
347 m_hyperlink
->SetURL(m_url
->GetValue());
351 void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent
& WXUNUSED(event
))
354 switch ( m_radioAlignMode
->GetSelection() )
358 wxFAIL_MSG( wxT("unknown alignment") );
362 addstyle
= wxHL_ALIGN_LEFT
;
366 addstyle
= wxHL_ALIGN_CENTRE
;
370 addstyle
= wxHL_ALIGN_RIGHT
;
374 CreateHyperlinkLong(addstyle
);
377 void HyperlinkWidgetsPage::OnGeneric(wxCommandEvent
& event
)
383 #endif // wxUSE_HYPERLINKCTRL