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