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