Call wxThread::Create() from Run() automatically.
[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/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 // Any "&"s in the text should appear on the screen and not be (mis)
66 // interpreted as mnemonics.
67 return wxString::Format("<A HREF=\"%s\">%s</A>",
68 url,
69 wxControl::EscapeMnemonics(text));
70 }
71 }
72
73 // ----------------------------------------------------------------------------
74 // wxHyperlinkCtrl
75 // ----------------------------------------------------------------------------
76
77 bool wxHyperlinkCtrl::Create(wxWindow *parent,
78 wxWindowID id,
79 const wxString& label, const wxString& url,
80 const wxPoint& pos,
81 const wxSize& size,
82 long style,
83 const wxString& name)
84 {
85 if ( !HasNativeHyperlinkCtrl() )
86 {
87 return wxGenericHyperlinkCtrl::Create( parent, id, label, url, pos,
88 size, style, name );
89 }
90
91 if ( !CreateControl(parent, id, pos, size, style,
92 wxDefaultValidator, name) )
93 {
94 return false;
95 }
96
97 SetURL( url );
98 SetVisited( false );
99
100 WXDWORD exstyle;
101 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
102
103 if ( !MSWCreateControl(WC_LINK, msStyle, pos, size,
104 GetLabelForSysLink( label, url ), exstyle) )
105 {
106 return false;
107 }
108
109 // Make sure both the label and URL are non-empty strings.
110 SetURL(url.empty() ? label : url);
111 SetLabel(label.empty() ? url : label);
112
113 ConnectMenuHandlers();
114
115 return true;
116 }
117
118 WXDWORD wxHyperlinkCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
119 {
120 WXDWORD msStyle = wxControl::MSWGetStyle( style, exstyle );
121
122 if ( style & wxHL_ALIGN_RIGHT )
123 msStyle |= LWS_RIGHT;
124
125 return msStyle;
126 }
127
128 void wxHyperlinkCtrl::SetURL(const wxString &url)
129 {
130 if ( !HasNativeHyperlinkCtrl() )
131 {
132 wxGenericHyperlinkCtrl::SetURL( url );
133 return;
134 }
135
136 if ( GetURL() != url )
137 SetVisited( false );
138 wxGenericHyperlinkCtrl::SetURL( url );
139 wxWindow::SetLabel( GetLabelForSysLink(m_labelOrig, url) );
140 }
141
142 void wxHyperlinkCtrl::SetLabel(const wxString &label)
143 {
144 if ( !HasNativeHyperlinkCtrl() )
145 {
146 wxGenericHyperlinkCtrl::SetLabel( label );
147 return;
148 }
149
150 m_labelOrig = label;
151 wxWindow::SetLabel( GetLabelForSysLink(label, GetURL()) );
152 InvalidateBestSize();
153 }
154
155 wxSize wxHyperlinkCtrl::DoGetBestClientSize() const
156 {
157 // LM_GETIDEALSIZE only exists under Vista so use the generic version even
158 // when using the native control under XP
159 if ( !HasNativeHyperlinkCtrl() || (wxGetWinVersion() < wxWinVersion_6) )
160 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
161
162 SIZE idealSize;
163 ::SendMessage(m_hWnd, LM_GETIDEALSIZE, 0, (LPARAM)&idealSize);
164
165 return wxSize(idealSize.cx, idealSize.cy);
166 }
167
168 bool wxHyperlinkCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
169 {
170 if ( HasNativeHyperlinkCtrl() )
171 {
172 switch ( ((LPNMHDR) lParam)->code )
173 {
174 case NM_CLICK:
175 case NM_RETURN:
176 SetVisited();
177 SendEvent();
178 return 0;
179 }
180 }
181
182 return wxGenericHyperlinkCtrl::MSWOnNotify(idCtrl, lParam, result);
183 }
184
185 #endif // wxUSE_HYPERLINKCTRL && wxUSE_UNICODE