]> git.saurik.com Git - wxWidgets.git/blob - src/msw/hyperlink.cpp
1f432871297a354fcd5f56f884e2aebfff1d0bf0
[wxWidgets.git] / src / msw / hyperlink.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/hyperlink.cpp
3 // Purpose: Hyperlink control
4 // Author: Rickard Westerlund
5 // Created: 2010-08-03
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ----------------------------------------------------------------------------
12 // headers
13 // ----------------------------------------------------------------------------
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #if wxUSE_HYPERLINKCTRL && wxUSE_UNICODE
23
24 #include "wx/hyperlink.h"
25
26 #ifndef WX_PRECOMP
27 #include "wx/stattext.h"
28 #include "wx/msw/private.h"
29 #endif
30
31 // ----------------------------------------------------------------------------
32 // Definitions
33 // ----------------------------------------------------------------------------
34
35 #ifndef LM_GETIDEALSIZE
36 #define LM_GETIDEALSIZE (WM_USER + 0x301)
37 #endif
38
39 #ifndef LWS_RIGHT
40 #define LWS_RIGHT 0x0020
41 #endif
42
43 #ifndef WC_LINK
44 #define WC_LINK L"SysLink"
45 #endif
46
47 // ----------------------------------------------------------------------------
48 // Helper functions
49 // ----------------------------------------------------------------------------
50
51 namespace
52 {
53 bool HasNativeHyperlinkCtrl()
54 {
55 return wxGetWinVersion() >= wxWinVersion_XP;
56 }
57
58 wxString GetLabelForSysLink(const wxString& text, const wxString& url)
59 {
60 return wxString("<A HREF=\"") + wxStaticText::RemoveMarkup(url) + "\">"
61 + wxStaticText::RemoveMarkup(text) + "</A>";
62 }
63 }
64
65 // ----------------------------------------------------------------------------
66 // wxHyperlinkCtrl
67 // ----------------------------------------------------------------------------
68
69 bool wxHyperlinkCtrl::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxString& label, const wxString& url,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString& name)
76 {
77 if ( !HasNativeHyperlinkCtrl() )
78 {
79 return wxGenericHyperlinkCtrl::Create( parent, id, label, url, pos,
80 size, style, name );
81 }
82
83 if ( !CreateControl(parent, id, pos, size, style,
84 wxDefaultValidator, name) )
85 {
86 return false;
87 }
88
89 SetURL( url );
90 SetVisited( false );
91
92 WXDWORD exstyle;
93 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
94
95 if ( !MSWCreateControl(WC_LINK, msStyle, pos, size,
96 GetLabelForSysLink( label, url ), exstyle) )
97 {
98 return false;
99 }
100
101 // Make sure both the label and URL are non-empty strings.
102 SetURL(url.empty() ? label : url);
103 SetLabel(label.empty() ? url : label);
104
105 ConnectMenuHandlers();
106
107 return true;
108 }
109
110 WXDWORD wxHyperlinkCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
111 {
112 WXDWORD msStyle = wxControl::MSWGetStyle( style, exstyle );
113
114 if ( style & wxHL_ALIGN_RIGHT )
115 msStyle |= LWS_RIGHT;
116
117 return msStyle;
118 }
119
120 void wxHyperlinkCtrl::SetURL(const wxString &url)
121 {
122 if ( !HasNativeHyperlinkCtrl() )
123 {
124 wxGenericHyperlinkCtrl::SetURL( url );
125 return;
126 }
127
128 if ( GetURL() != url )
129 SetVisited( false );
130 wxGenericHyperlinkCtrl::SetURL( url );
131 wxWindow::SetLabel( GetLabelForSysLink(m_labelOrig, url) );
132 }
133
134 void wxHyperlinkCtrl::SetLabel(const wxString &label)
135 {
136 if ( !HasNativeHyperlinkCtrl() )
137 {
138 wxGenericHyperlinkCtrl::SetLabel( label );
139 return;
140 }
141
142 m_labelOrig = label;
143 wxWindow::SetLabel( GetLabelForSysLink(label, GetURL()) );
144 InvalidateBestSize();
145 }
146
147 wxSize wxHyperlinkCtrl::DoGetBestClientSize() const
148 {
149 // LM_GETIDEALSIZE only exists under Vista so use the generic version even
150 // when using the native control under XP
151 if ( wxGetWinVersion() < wxWinVersion_6 )
152 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
153
154 SIZE idealSize;
155 ::SendMessage(m_hWnd, LM_GETIDEALSIZE, 0, (LPARAM)&idealSize);
156
157 return wxSize(idealSize.cx, idealSize.cy);
158 }
159
160 bool wxHyperlinkCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
161 {
162 if ( HasNativeHyperlinkCtrl() )
163 {
164 switch ( ((LPNMHDR) lParam)->code )
165 {
166 case NM_CLICK:
167 case NM_RETURN:
168 SetVisited();
169 SendEvent();
170 return 0;
171 }
172 }
173
174 return wxGenericHyperlinkCtrl::MSWOnNotify(idCtrl, lParam, result);
175 }
176
177 #endif // wxUSE_HYPERLINKCTRL && wxUSE_UNICODE