]> git.saurik.com Git - wxWidgets.git/blob - src/msw/hyperlink.cpp
Compilation fix for old SDKs in new native MSW wxHyperLinkCtrl code.
[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 #endif
29
30 // ----------------------------------------------------------------------------
31 // Definitions
32 // ----------------------------------------------------------------------------
33
34 #ifndef LM_GETIDEALSIZE
35 #define LM_GETIDEALSIZE (WM_USER + 0x301)
36 #endif
37
38 #ifndef LWS_RIGHT
39 #define LWS_RIGHT 0x0020
40 #endif
41
42 #ifndef WC_LINK
43 #define WC_LINK L"SysLink"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // Helper functions
48 // ----------------------------------------------------------------------------
49
50 namespace
51 {
52 bool HasNativeHyperlinkCtrl()
53 {
54 return wxGetWinVersion() >= wxWinVersion_XP;
55 }
56
57 wxString GetLabelForSysLink(const wxString& text, const wxString& url)
58 {
59 return wxString("<A HREF=\"") + wxStaticText::RemoveMarkup(url) + "\">"
60 + wxStaticText::RemoveMarkup(text) + "</A>";
61 }
62 }
63
64 // ----------------------------------------------------------------------------
65 // wxHyperlinkCtrl
66 // ----------------------------------------------------------------------------
67
68 bool wxHyperlinkCtrl::Create(wxWindow *parent,
69 wxWindowID id,
70 const wxString& label, const wxString& url,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 {
76 if ( !HasNativeHyperlinkCtrl() )
77 {
78 return wxGenericHyperlinkCtrl::Create( parent, id, label, url, pos,
79 size, style, name );
80 }
81
82 if ( !CreateControl(parent, id, pos, size, style,
83 wxDefaultValidator, name) )
84 {
85 return false;
86 }
87
88 SetURL( url );
89 SetVisited( false );
90
91 WXDWORD exstyle;
92 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
93
94 if ( !MSWCreateControl(WC_LINK, msStyle, pos, size,
95 GetLabelForSysLink( label, url ), exstyle) )
96 {
97 return false;
98 }
99
100 // Make sure both the label and URL are non-empty strings.
101 SetURL(url.empty() ? label : url);
102 SetLabel(label.empty() ? url : label);
103
104 ConnectMenuHandlers();
105
106 return true;
107 }
108
109 WXDWORD wxHyperlinkCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
110 {
111 WXDWORD msStyle = wxControl::MSWGetStyle( style, exstyle );
112
113 if ( style & wxHL_ALIGN_RIGHT )
114 msStyle |= LWS_RIGHT;
115
116 return msStyle;
117 }
118
119 void wxHyperlinkCtrl::SetURL(const wxString &url)
120 {
121 if ( !HasNativeHyperlinkCtrl() )
122 {
123 wxGenericHyperlinkCtrl::SetURL( url );
124 return;
125 }
126
127 if ( GetURL() != url )
128 SetVisited( false );
129 wxGenericHyperlinkCtrl::SetURL( url );
130 wxWindow::SetLabel( GetLabelForSysLink(m_labelOrig, url) );
131 }
132
133 void wxHyperlinkCtrl::SetLabel(const wxString &label)
134 {
135 if ( !HasNativeHyperlinkCtrl() )
136 {
137 wxGenericHyperlinkCtrl::SetLabel( label );
138 return;
139 }
140
141 m_labelOrig = label;
142 wxWindow::SetLabel( GetLabelForSysLink(label, GetURL()) );
143 InvalidateBestSize();
144 }
145
146 wxSize wxHyperlinkCtrl::DoGetBestClientSize() const
147 {
148 // LM_GETIDEALSIZE only exists under Vista so use the generic version even
149 // when using the native control under XP
150 if ( wxGetWinVersion() < wxWinVersion_6 )
151 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
152
153 SIZE idealSize;
154 ::SendMessage(m_hWnd, LM_GETIDEALSIZE, 0, (LPARAM)&idealSize);
155
156 return wxSize(idealSize.cx, idealSize.cy);
157 }
158
159 bool wxHyperlinkCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
160 {
161 if ( HasNativeHyperlinkCtrl() )
162 {
163 switch ( ((LPNMHDR) lParam)->code )
164 {
165 case NM_CLICK:
166 case NM_RETURN:
167 SetVisited();
168 SendEvent();
169 return 0;
170 }
171 }
172
173 return wxGenericHyperlinkCtrl::MSWOnNotify(idCtrl, lParam, result);
174 }
175
176 #endif // wxUSE_HYPERLINKCTRL && wxUSE_UNICODE