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