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: hyperlnk.cpp 43755 2006-12-03 13:43:44Z VZ $
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
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
97    // reset the control parameters
98    void Reset();
99
100    // (re)create the hyperctrl
101    void CreateHyperlink();
102    void CreateHyperlinkLong(long);
103
104    // the controls
105    // ------------
106
107    // the checkbox itself and the sizer it is in
108    wxHyperlinkCtrl *m_hyperlink;
109    wxHyperlinkCtrl *m_hyperlinkLong;
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
120    wxRadioBox *m_radioAlignMode;
121
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)
135
136    EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment)
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)
149                     :WidgetsPage(book, imaglist, hyperlnk_xpm)
150{
151}
152
153void HyperlinkWidgetsPage::CreateContent()
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
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
186    // right pane
187    wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL);
188    wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL);
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
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
217
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);
221
222    // final initializations
223    Reset();
224
225    SetSizer(sizerTop);
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
239    wxHyperlinkCtrl *hyp = new wxHyperlinkCtrl(this,
240                                               HyperlinkPage_Ctrl,
241                                               label,
242                                               url);
243
244    // update sizer's child window
245    GetSizer()->Replace(m_hyperlink, hyp, true);
246
247    // update our pointer
248    delete m_hyperlink;
249    m_hyperlink = hyp;
250
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();
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
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
326#endif // wxUSE_HYPERLINKCTRL
327