]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/hyperlnk.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[wxWidgets.git] / samples / widgets / hyperlnk.cpp
index 5c091ba34c0c4ff2fdccfb5c6eb1a04daf1c7849..437dc9d5c2c44a140e8f4c1a3214472fa4fed751 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     27 Sep 2003
 // Id:          $Id$
 // Copyright:   (c) 2003 wxWindows team
-// License:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -42,7 +42,7 @@
     #include "wx/sizer.h"
 #endif
 
-#include "wx/Hyperlink.h"
+#include "wx/hyperlink.h"
 
 #include "widgets.h"
 
@@ -61,6 +61,15 @@ enum
     HyperlinkPage_Ctrl
 };
 
+// alignment radiobox indices
+enum
+{
+    Align_Left,
+    Align_Centre,
+    Align_Right,
+    Align_Max
+};
+
 // ----------------------------------------------------------------------------
 // CheckBoxWidgetsPage
 // ----------------------------------------------------------------------------
@@ -69,30 +78,36 @@ class HyperlinkWidgetsPage : public WidgetsPage
 {
 public:
     HyperlinkWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
-    virtual ~HyperlinkWidgetsPage(){};
+    virtual ~HyperlinkWidgetsPage() {}
 
     virtual wxControl *GetWidget() const { return m_hyperlink; }
     virtual void RecreateWidget() { CreateHyperlink(); }
 
+    // lazy creation of the content
+    virtual void CreateContent();
+
 protected:
     // event handlers
     void OnButtonSetLabel(wxCommandEvent& event);
     void OnButtonSetURL(wxCommandEvent& event);
 
     void OnButtonReset(wxCommandEvent& event);
+    void OnAlignment(wxCommandEvent& event);
+    void OnGeneric(wxCommandEvent& event);
 
     // reset the control parameters
     void Reset();
 
     // (re)create the hyperctrl
     void CreateHyperlink();
+    void CreateHyperlinkLong(long);
 
     // the controls
     // ------------
 
     // the checkbox itself and the sizer it is in
-    wxHyperlinkCtrl *m_hyperlink;
-    wxSizer *m_sizerHyperlink;
+    wxGenericHyperlinkCtrl *m_hyperlink;
+    wxGenericHyperlinkCtrl *m_hyperlinkLong;
 
     wxTextCtrl *m_label;
     wxTextCtrl *m_url;
@@ -103,6 +118,9 @@ protected:
     // the text entries for command parameters
     wxTextCtrl *m_textLabel;
 
+    wxRadioBox *m_radioAlignMode;
+    wxCheckBox *m_checkGeneric;
+
 private:
     DECLARE_EVENT_TABLE()
     DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage)
@@ -116,6 +134,9 @@ BEGIN_EVENT_TABLE(HyperlinkWidgetsPage, WidgetsPage)
     EVT_BUTTON(HyperlinkPage_Reset, HyperlinkWidgetsPage::OnButtonReset)
     EVT_BUTTON(HyperlinkPage_SetLabel, HyperlinkWidgetsPage::OnButtonSetLabel)
     EVT_BUTTON(HyperlinkPage_SetURL, HyperlinkWidgetsPage::OnButtonSetURL)
+
+    EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment)
+    EVT_CHECKBOX(wxID_ANY, HyperlinkWidgetsPage::OnGeneric)
 END_EVENT_TABLE()
 
 // ============================================================================
@@ -128,7 +149,11 @@ IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage, wxT("Hyperlink"),
 
 HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book,
                                            wxImageList *imaglist)
-                      :WidgetsPage(book, imaglist, hyperlnk_xpm)
+                     :WidgetsPage(book, imaglist, hyperlnk_xpm)
+{
+}
+
+void HyperlinkWidgetsPage::CreateContent()
 {
     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
 
@@ -143,36 +168,86 @@ HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book,
     sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL , wxT("Set &URL"), wxID_ANY, &m_url ),
                     0, wxALL | wxALIGN_RIGHT , 5 );
 
+    static const wxString alignments[] =
+    {
+        wxT("&left"),
+        wxT("&centre"),
+        wxT("&right")
+    };
+    wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments) == Align_Max,
+                           AlignMismatch );
+
+    m_radioAlignMode = new wxRadioBox(this, wxID_ANY, wxT("alignment"),
+                                      wxDefaultPosition, wxDefaultSize,
+                                      WXSIZEOF(alignments), alignments);
+    m_radioAlignMode->SetSelection(1);  // start with "centre" selected since
+                                        // wxHL_DEFAULT_STYLE contains wxHL_ALIGN_CENTRE
+    sizerLeft->Add(m_radioAlignMode, 0, wxALL|wxGROW, 5);
+
+    m_checkGeneric = new wxCheckBox(this, wxID_ANY, wxT("Use generic version"),
+                                    wxDefaultPosition, wxDefaultSize);
+    sizerLeft->Add(m_checkGeneric, 0, wxALL|wxGROW, 5);
+
     // right pane
-    wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
+    wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL);
+    wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL);
 
     m_visit = new wxStaticText(this, wxID_ANY, wxT("Visit "));
 
-    m_hyperlink = new wxHyperlinkCtrl(this,
-                                      HyperlinkPage_Ctrl,
-                                      wxT("wxWidgets website"),
-                                      wxT("www.wxwidgets.org"));
+    if (m_checkGeneric->IsChecked())
+    {
+        m_hyperlink = new wxGenericHyperlinkCtrl(this,
+                                          HyperlinkPage_Ctrl,
+                                          wxT("wxWidgets website"),
+                                          wxT("www.wxwidgets.org"));
+    }
+    else
+    {
+        m_hyperlink = new wxHyperlinkCtrl(this,
+                                          HyperlinkPage_Ctrl,
+                                          wxT("wxWidgets website"),
+                                          wxT("www.wxwidgets.org"));
+    }
 
     m_fun = new wxStaticText(this, wxID_ANY, wxT(" for fun!"));
 
-    sizerRight->Add(0, 0, 1, wxCENTRE);
-    sizerRight->Add(m_visit, 0, wxCENTRE);
-    sizerRight->Add(m_hyperlink, 0, wxCENTRE);
-    sizerRight->Add(m_fun, 0, wxCENTRE);
-    sizerRight->Add(0, 0, 1, wxCENTRE);
-    sizerRight->SetMinSize(150, 0);
-    m_sizerHyperlink = sizerRight; // save it to modify it later
+    szHyperlink->Add(0, 0, 1, wxCENTRE);
+    szHyperlink->Add(m_visit, 0, wxCENTRE);
+    szHyperlink->Add(m_hyperlink, 0, wxCENTRE);
+    szHyperlink->Add(m_fun, 0, wxCENTRE);
+    szHyperlink->Add(0, 0, 1, wxCENTRE);
+    szHyperlink->SetMinSize(150, 0);
+
+    if (m_checkGeneric->IsChecked())
+    {
+        m_hyperlinkLong = new wxGenericHyperlinkCtrl(this,
+                                              wxID_ANY,
+                                              wxT("This is a long hyperlink"),
+                                              wxT("www.wxwidgets.org"));
+    }
+    else
+    {
+        m_hyperlinkLong = new wxHyperlinkCtrl(this,
+                                              wxID_ANY,
+                                              wxT("This is a long hyperlink"),
+                                              wxT("www.wxwidgets.org"));
+    }
+
+    szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
+    szHyperlinkLong->Add(szHyperlink, 0, wxCENTRE|wxGROW);
+    szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
+    szHyperlinkLong->Add(m_hyperlinkLong, 0, wxGROW);
+    szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
+
 
     // the 3 panes panes compose the window
     sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
-    sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
+    sizerTop->Add(szHyperlinkLong, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
 
     // final initializations
     Reset();
 
     SetSizer(sizerTop);
-
-    sizerTop->Fit(this);
 }
 
 void HyperlinkWidgetsPage::Reset()
@@ -186,25 +261,68 @@ void HyperlinkWidgetsPage::CreateHyperlink()
     const wxString label = m_hyperlink->GetLabel();
     const wxString url = m_hyperlink->GetURL();
 
-    size_t count = m_sizerHyperlink->GetChildren().GetCount();
-    for ( size_t n = 0; n < count; n++ )
+    wxGenericHyperlinkCtrl *hyp;
+    if (m_checkGeneric->IsChecked())
     {
-        m_sizerHyperlink->Remove(0);
+        hyp = new wxGenericHyperlinkCtrl(this,
+                                  HyperlinkPage_Ctrl,
+                                  label,
+                                  url);
     }
+    else
+    {
+        hyp = new wxHyperlinkCtrl(this,
+                                  HyperlinkPage_Ctrl,
+                                  label,
+                                  url);
+    }
+
+    // update sizer's child window
+    GetSizer()->Replace(m_hyperlink, hyp, true);
 
+    // update our pointer
     delete m_hyperlink;
+    m_hyperlink = hyp;
+
+    // relayout the sizer
+    GetSizer()->Layout();
+}
+
+void HyperlinkWidgetsPage::CreateHyperlinkLong(long style)
+{
+    style = (wxHL_DEFAULT_STYLE & ~wxHL_ALIGN_CENTRE)|style;
+
+    wxGenericHyperlinkCtrl *hyp;
+    if (m_checkGeneric->IsChecked())
+    {
+        hyp = new wxGenericHyperlinkCtrl(this,
+                                  wxID_ANY,
+                                  wxT("This is a long hyperlink"),
+                                  wxT("www.wxwidgets.org"),
+                                  wxDefaultPosition,
+                                  wxDefaultSize,
+                                  style);
+    }
+    else
+    {
+        hyp = new wxHyperlinkCtrl(this,
+                                  wxID_ANY,
+                                  wxT("This is a long hyperlink"),
+                                  wxT("www.wxwidgets.org"),
+                                  wxDefaultPosition,
+                                  wxDefaultSize,
+                                  style);
+    }
 
-    m_hyperlink = new wxHyperlinkCtrl(this,
-                                      HyperlinkPage_Ctrl,
-                                      label,
-                                      url);
-
-    m_sizerHyperlink->Add(0, 0, 1, wxCENTRE);
-    m_sizerHyperlink->Add(m_visit, 0, wxCENTRE);
-    m_sizerHyperlink->Add(m_hyperlink, 0, wxCENTRE);
-    m_sizerHyperlink->Add(m_fun, 0, wxCENTRE);
-    m_sizerHyperlink->Add(0, 0, 1, wxCENTRE);
-    m_sizerHyperlink->Layout();
+    // update sizer's child window
+    GetSizer()->Replace(m_hyperlinkLong, hyp, true);
+
+    // update our pointer
+    delete m_hyperlinkLong;
+    m_hyperlinkLong = hyp;
+
+    // relayout the sizer
+    GetSizer()->Layout();
 }
 
 // ----------------------------------------------------------------------------
@@ -230,4 +348,36 @@ void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent& WXUNUSED(event))
     CreateHyperlink();
 }
 
+void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent& WXUNUSED(event))
+{
+    long addstyle;
+    switch ( m_radioAlignMode->GetSelection() )
+    {
+        default:
+        case Align_Max:
+            wxFAIL_MSG( wxT("unknown alignment") );
+            // fall through
+
+        case Align_Left:
+            addstyle = wxHL_ALIGN_LEFT;
+            break;
+
+        case Align_Centre:
+            addstyle = wxHL_ALIGN_CENTRE;
+            break;
+
+        case Align_Right:
+            addstyle = wxHL_ALIGN_RIGHT;
+            break;
+    }
+
+    CreateHyperlinkLong(addstyle);
+}
+
+void HyperlinkWidgetsPage::OnGeneric(wxCommandEvent& event)
+{
+    CreateHyperlink();
+    OnAlignment(event);
+}
+
 #endif // wxUSE_HYPERLINKCTRL