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