hyperlnk.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // Copyright: (c) 2003 wxWindows team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #if wxUSE_HYPERLINKCTRL
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/app.h"
  25. #include "wx/log.h"
  26. #include "wx/bitmap.h"
  27. #include "wx/button.h"
  28. #include "wx/checkbox.h"
  29. #include "wx/radiobox.h"
  30. #include "wx/statbox.h"
  31. #include "wx/stattext.h"
  32. #include "wx/textctrl.h"
  33. #include "wx/sizer.h"
  34. #endif
  35. #include "wx/hyperlink.h"
  36. #include "widgets.h"
  37. #include "icons/hyperlnk.xpm"
  38. // ----------------------------------------------------------------------------
  39. // constants
  40. // ----------------------------------------------------------------------------
  41. // control ids
  42. enum
  43. {
  44. HyperlinkPage_Reset = wxID_HIGHEST,
  45. HyperlinkPage_SetLabel,
  46. HyperlinkPage_SetURL,
  47. HyperlinkPage_Ctrl
  48. };
  49. // alignment radiobox indices
  50. enum
  51. {
  52. Align_Left,
  53. Align_Centre,
  54. Align_Right,
  55. Align_Max
  56. };
  57. // ----------------------------------------------------------------------------
  58. // CheckBoxWidgetsPage
  59. // ----------------------------------------------------------------------------
  60. class HyperlinkWidgetsPage : public WidgetsPage
  61. {
  62. public:
  63. HyperlinkWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  64. virtual ~HyperlinkWidgetsPage() {}
  65. virtual wxControl *GetWidget() const { return m_hyperlink; }
  66. virtual void RecreateWidget() { CreateHyperlink(); }
  67. // lazy creation of the content
  68. virtual void CreateContent();
  69. protected:
  70. // event handlers
  71. void OnButtonSetLabel(wxCommandEvent& event);
  72. void OnButtonSetURL(wxCommandEvent& event);
  73. void OnButtonReset(wxCommandEvent& event);
  74. void OnAlignment(wxCommandEvent& event);
  75. void OnGeneric(wxCommandEvent& event);
  76. // reset the control parameters
  77. void Reset();
  78. // (re)create the hyperctrl
  79. void CreateHyperlink();
  80. void CreateHyperlinkLong(long);
  81. // the controls
  82. // ------------
  83. // the checkbox itself and the sizer it is in
  84. wxGenericHyperlinkCtrl *m_hyperlink;
  85. wxGenericHyperlinkCtrl *m_hyperlinkLong;
  86. wxTextCtrl *m_label;
  87. wxTextCtrl *m_url;
  88. wxStaticText *m_visit;
  89. wxStaticText *m_fun;
  90. // the text entries for command parameters
  91. wxTextCtrl *m_textLabel;
  92. wxRadioBox *m_radioAlignMode;
  93. wxCheckBox *m_checkGeneric;
  94. private:
  95. wxDECLARE_EVENT_TABLE();
  96. DECLARE_WIDGETS_PAGE(HyperlinkWidgetsPage)
  97. };
  98. // ----------------------------------------------------------------------------
  99. // event tables
  100. // ----------------------------------------------------------------------------
  101. wxBEGIN_EVENT_TABLE(HyperlinkWidgetsPage, WidgetsPage)
  102. EVT_BUTTON(HyperlinkPage_Reset, HyperlinkWidgetsPage::OnButtonReset)
  103. EVT_BUTTON(HyperlinkPage_SetLabel, HyperlinkWidgetsPage::OnButtonSetLabel)
  104. EVT_BUTTON(HyperlinkPage_SetURL, HyperlinkWidgetsPage::OnButtonSetURL)
  105. EVT_RADIOBOX(wxID_ANY, HyperlinkWidgetsPage::OnAlignment)
  106. EVT_CHECKBOX(wxID_ANY, HyperlinkWidgetsPage::OnGeneric)
  107. wxEND_EVENT_TABLE()
  108. // ============================================================================
  109. // implementation
  110. // ============================================================================
  111. IMPLEMENT_WIDGETS_PAGE(HyperlinkWidgetsPage, wxT("Hyperlink"),
  112. GENERIC_CTRLS
  113. );
  114. HyperlinkWidgetsPage::HyperlinkWidgetsPage(WidgetsBookCtrl *book,
  115. wxImageList *imaglist)
  116. :WidgetsPage(book, imaglist, hyperlnk_xpm)
  117. {
  118. }
  119. void HyperlinkWidgetsPage::CreateContent()
  120. {
  121. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  122. // left pane
  123. wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Hyperlink details"));
  124. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  125. sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetLabel , wxT("Set &Label"), wxID_ANY, &m_label ),
  126. 0, wxALL | wxALIGN_RIGHT , 5 );
  127. sizerLeft->Add( CreateSizerWithTextAndButton( HyperlinkPage_SetURL , wxT("Set &URL"), wxID_ANY, &m_url ),
  128. 0, wxALL | wxALIGN_RIGHT , 5 );
  129. static const wxString alignments[] =
  130. {
  131. wxT("&left"),
  132. wxT("&centre"),
  133. wxT("&right")
  134. };
  135. wxCOMPILE_TIME_ASSERT( WXSIZEOF(alignments) == Align_Max,
  136. AlignMismatch );
  137. m_radioAlignMode = new wxRadioBox(this, wxID_ANY, wxT("alignment"),
  138. wxDefaultPosition, wxDefaultSize,
  139. WXSIZEOF(alignments), alignments);
  140. m_radioAlignMode->SetSelection(1); // start with "centre" selected since
  141. // wxHL_DEFAULT_STYLE contains wxHL_ALIGN_CENTRE
  142. sizerLeft->Add(m_radioAlignMode, 0, wxALL|wxGROW, 5);
  143. m_checkGeneric = new wxCheckBox(this, wxID_ANY, wxT("Use generic version"),
  144. wxDefaultPosition, wxDefaultSize);
  145. sizerLeft->Add(m_checkGeneric, 0, wxALL|wxGROW, 5);
  146. // right pane
  147. wxSizer *szHyperlinkLong = new wxBoxSizer(wxVERTICAL);
  148. wxSizer *szHyperlink = new wxBoxSizer(wxHORIZONTAL);
  149. m_visit = new wxStaticText(this, wxID_ANY, wxT("Visit "));
  150. if (m_checkGeneric->IsChecked())
  151. {
  152. m_hyperlink = new wxGenericHyperlinkCtrl(this,
  153. HyperlinkPage_Ctrl,
  154. wxT("wxWidgets website"),
  155. wxT("www.wxwidgets.org"));
  156. }
  157. else
  158. {
  159. m_hyperlink = new wxHyperlinkCtrl(this,
  160. HyperlinkPage_Ctrl,
  161. wxT("wxWidgets website"),
  162. wxT("www.wxwidgets.org"));
  163. }
  164. m_fun = new wxStaticText(this, wxID_ANY, wxT(" for fun!"));
  165. szHyperlink->Add(0, 0, 1, wxCENTRE);
  166. szHyperlink->Add(m_visit, 0, wxCENTRE);
  167. szHyperlink->Add(m_hyperlink, 0, wxCENTRE);
  168. szHyperlink->Add(m_fun, 0, wxCENTRE);
  169. szHyperlink->Add(0, 0, 1, wxCENTRE);
  170. szHyperlink->SetMinSize(150, 0);
  171. if (m_checkGeneric->IsChecked())
  172. {
  173. m_hyperlinkLong = new wxGenericHyperlinkCtrl(this,
  174. wxID_ANY,
  175. wxT("This is a long hyperlink"),
  176. wxT("www.wxwidgets.org"));
  177. }
  178. else
  179. {
  180. m_hyperlinkLong = new wxHyperlinkCtrl(this,
  181. wxID_ANY,
  182. wxT("This is a long hyperlink"),
  183. wxT("www.wxwidgets.org"));
  184. }
  185. szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
  186. szHyperlinkLong->Add(szHyperlink, 0, wxCENTRE|wxGROW);
  187. szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
  188. szHyperlinkLong->Add(m_hyperlinkLong, 0, wxGROW);
  189. szHyperlinkLong->Add(0, 0, 1, wxCENTRE);
  190. // the 3 panes panes compose the window
  191. sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
  192. sizerTop->Add(szHyperlinkLong, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  193. // final initializations
  194. Reset();
  195. SetSizer(sizerTop);
  196. }
  197. void HyperlinkWidgetsPage::Reset()
  198. {
  199. m_label->SetValue(m_hyperlink->GetLabel());
  200. m_url->SetValue(m_hyperlink->GetURL());
  201. }
  202. void HyperlinkWidgetsPage::CreateHyperlink()
  203. {
  204. const wxString label = m_hyperlink->GetLabel();
  205. const wxString url = m_hyperlink->GetURL();
  206. wxGenericHyperlinkCtrl *hyp;
  207. if (m_checkGeneric->IsChecked())
  208. {
  209. hyp = new wxGenericHyperlinkCtrl(this,
  210. HyperlinkPage_Ctrl,
  211. label,
  212. url);
  213. }
  214. else
  215. {
  216. hyp = new wxHyperlinkCtrl(this,
  217. HyperlinkPage_Ctrl,
  218. label,
  219. url);
  220. }
  221. // update sizer's child window
  222. GetSizer()->Replace(m_hyperlink, hyp, true);
  223. // update our pointer
  224. delete m_hyperlink;
  225. m_hyperlink = hyp;
  226. // relayout the sizer
  227. GetSizer()->Layout();
  228. }
  229. void HyperlinkWidgetsPage::CreateHyperlinkLong(long style)
  230. {
  231. style = (wxHL_DEFAULT_STYLE & ~wxHL_ALIGN_CENTRE)|style;
  232. wxGenericHyperlinkCtrl *hyp;
  233. if (m_checkGeneric->IsChecked())
  234. {
  235. hyp = new wxGenericHyperlinkCtrl(this,
  236. wxID_ANY,
  237. wxT("This is a long hyperlink"),
  238. wxT("www.wxwidgets.org"),
  239. wxDefaultPosition,
  240. wxDefaultSize,
  241. style);
  242. }
  243. else
  244. {
  245. hyp = new wxHyperlinkCtrl(this,
  246. wxID_ANY,
  247. wxT("This is a long hyperlink"),
  248. wxT("www.wxwidgets.org"),
  249. wxDefaultPosition,
  250. wxDefaultSize,
  251. style);
  252. }
  253. // update sizer's child window
  254. GetSizer()->Replace(m_hyperlinkLong, hyp, true);
  255. // update our pointer
  256. delete m_hyperlinkLong;
  257. m_hyperlinkLong = hyp;
  258. // relayout the sizer
  259. GetSizer()->Layout();
  260. }
  261. // ----------------------------------------------------------------------------
  262. // event handlers
  263. // ----------------------------------------------------------------------------
  264. void HyperlinkWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  265. {
  266. Reset();
  267. CreateHyperlink();
  268. }
  269. void HyperlinkWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
  270. {
  271. m_hyperlink->SetLabel(m_label->GetValue());
  272. CreateHyperlink();
  273. }
  274. void HyperlinkWidgetsPage::OnButtonSetURL(wxCommandEvent& WXUNUSED(event))
  275. {
  276. m_hyperlink->SetURL(m_url->GetValue());
  277. CreateHyperlink();
  278. }
  279. void HyperlinkWidgetsPage::OnAlignment(wxCommandEvent& WXUNUSED(event))
  280. {
  281. long addstyle;
  282. switch ( m_radioAlignMode->GetSelection() )
  283. {
  284. default:
  285. case Align_Max:
  286. wxFAIL_MSG( wxT("unknown alignment") );
  287. // fall through
  288. case Align_Left:
  289. addstyle = wxHL_ALIGN_LEFT;
  290. break;
  291. case Align_Centre:
  292. addstyle = wxHL_ALIGN_CENTRE;
  293. break;
  294. case Align_Right:
  295. addstyle = wxHL_ALIGN_RIGHT;
  296. break;
  297. }
  298. CreateHyperlinkLong(addstyle);
  299. }
  300. void HyperlinkWidgetsPage::OnGeneric(wxCommandEvent& event)
  301. {
  302. CreateHyperlink();
  303. OnAlignment(event);
  304. }
  305. #endif // wxUSE_HYPERLINKCTRL