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