]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
40ff126a | 2 | // Name: src/msw/textctrl.cpp |
2bda0e17 KB |
3 | // Purpose: wxTextCtrl |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a1b82138 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
a1b82138 VZ |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
a1b82138 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
3180bc0e | 27 | #if wxUSE_TEXTCTRL && !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) |
1e6feb95 | 28 | |
2bda0e17 | 29 | #ifndef WX_PRECOMP |
a1b82138 VZ |
30 | #include "wx/textctrl.h" |
31 | #include "wx/settings.h" | |
32 | #include "wx/brush.h" | |
33 | #include "wx/utils.h" | |
381dd4bf | 34 | #include "wx/intl.h" |
a1b82138 | 35 | #include "wx/log.h" |
381dd4bf | 36 | #include "wx/app.h" |
2b5f62a0 | 37 | #include "wx/menu.h" |
e2bcbdfb | 38 | #include "wx/math.h" |
02761f6c | 39 | #include "wx/module.h" |
193d0c93 | 40 | #include "wx/wxcrtvararg.h" |
2bda0e17 KB |
41 | #endif |
42 | ||
8ef51d67 | 43 | #include "wx/sysopt.h" |
f6bcfd97 | 44 | |
47d67540 | 45 | #if wxUSE_CLIPBOARD |
a1b82138 | 46 | #include "wx/clipbrd.h" |
2bda0e17 KB |
47 | #endif |
48 | ||
a1b82138 VZ |
49 | #include "wx/textfile.h" |
50 | ||
51 | #include <windowsx.h> | |
52 | ||
2bda0e17 | 53 | #include "wx/msw/private.h" |
9a88fc58 | 54 | #include "wx/msw/winundef.h" |
8f825d89 | 55 | #include "wx/msw/mslu.h" |
2bda0e17 | 56 | |
a1b82138 | 57 | #include <string.h> |
2bda0e17 | 58 | #include <stdlib.h> |
4676948b JS |
59 | |
60 | #ifndef __WXWINCE__ | |
a1b82138 | 61 | #include <sys/types.h> |
4676948b | 62 | #endif |
fbc535ff | 63 | |
a40c9444 VZ |
64 | #if wxUSE_RICHEDIT |
65 | ||
8ef51d67 JS |
66 | #if wxUSE_INKEDIT |
67 | #include "wx/dynlib.h" | |
68 | #endif | |
69 | ||
a40c9444 VZ |
70 | // old mingw32 has richedit stuff directly in windows.h and doesn't have |
71 | // richedit.h at all | |
72 | #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__) | |
cd471848 | 73 | #include <richedit.h> |
2bda0e17 KB |
74 | #endif |
75 | ||
a40c9444 VZ |
76 | #endif // wxUSE_RICHEDIT |
77 | ||
7212d155 PC |
78 | #include "wx/msw/missing.h" |
79 | ||
20a0e999 VZ |
80 | #if wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT |
81 | ||
82 | // dummy value used for m_dropTarget, different from any valid pointer value | |
83 | // (which are all even under Windows) and NULL | |
84 | static wxDropTarget * | |
5c33522f | 85 | wxRICHTEXT_DEFAULT_DROPTARGET = reinterpret_cast<wxDropTarget *>(1); |
20a0e999 VZ |
86 | |
87 | #endif // wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT | |
88 | ||
b12915c1 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // private classes | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | #if wxUSE_RICHEDIT | |
94 | ||
a5aa8086 | 95 | // this module initializes RichEdit DLL(s) if needed |
b12915c1 VZ |
96 | class wxRichEditModule : public wxModule |
97 | { | |
98 | public: | |
628c219e VZ |
99 | enum Version |
100 | { | |
101 | Version_1, // riched32.dll | |
102 | Version_2or3, // both use riched20.dll | |
103 | Version_41, // msftedit.dll (XP SP1 and Windows 2003) | |
104 | Version_Max | |
105 | }; | |
106 | ||
b12915c1 VZ |
107 | virtual bool OnInit(); |
108 | virtual void OnExit(); | |
109 | ||
628c219e VZ |
110 | // load the richedit DLL for the specified version of rich edit |
111 | static bool Load(Version version); | |
b12915c1 | 112 | |
8ef51d67 JS |
113 | #if wxUSE_INKEDIT |
114 | // load the InkEdit library | |
115 | static bool LoadInkEdit(); | |
116 | #endif | |
117 | ||
b12915c1 | 118 | private: |
a5aa8086 | 119 | // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs |
628c219e | 120 | static HINSTANCE ms_hRichEdit[Version_Max]; |
b12915c1 | 121 | |
8ef51d67 JS |
122 | #if wxUSE_INKEDIT |
123 | static wxDynamicLibrary ms_inkEditLib; | |
124 | static bool ms_inkEditLibLoadAttemped; | |
125 | #endif | |
126 | ||
b12915c1 VZ |
127 | DECLARE_DYNAMIC_CLASS(wxRichEditModule) |
128 | }; | |
129 | ||
628c219e | 130 | HINSTANCE wxRichEditModule::ms_hRichEdit[Version_Max] = { NULL, NULL, NULL }; |
b12915c1 | 131 | |
8ef51d67 JS |
132 | #if wxUSE_INKEDIT |
133 | wxDynamicLibrary wxRichEditModule::ms_inkEditLib; | |
134 | bool wxRichEditModule::ms_inkEditLibLoadAttemped = false; | |
135 | #endif | |
136 | ||
b12915c1 VZ |
137 | IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule) |
138 | ||
139 | #endif // wxUSE_RICHEDIT | |
e702ff0f | 140 | |
2c62dd25 VZ |
141 | // a small class used to set m_updatesCount to 0 (to filter duplicate events if |
142 | // necessary) and to reset it back to -1 afterwards | |
143 | class UpdatesCountFilter | |
144 | { | |
145 | public: | |
146 | UpdatesCountFilter(int& count) | |
147 | : m_count(count) | |
148 | { | |
f6519b40 | 149 | wxASSERT_MSG( m_count == -1 || m_count == -2, |
9a83f860 | 150 | wxT("wrong initial m_updatesCount value") ); |
2c62dd25 | 151 | |
f6519b40 VZ |
152 | if (m_count != -2) |
153 | m_count = 0; | |
154 | //else: we don't want to count how many update events we get as we're going | |
155 | // to ignore all of them | |
2c62dd25 VZ |
156 | } |
157 | ||
158 | ~UpdatesCountFilter() | |
159 | { | |
160 | m_count = -1; | |
161 | } | |
162 | ||
163 | // return true if an event has been received | |
164 | bool GotUpdate() const | |
165 | { | |
166 | return m_count == 1; | |
167 | } | |
168 | ||
169 | private: | |
170 | int& m_count; | |
171 | ||
c0c133e1 | 172 | wxDECLARE_NO_COPY_CLASS(UpdatesCountFilter); |
2c62dd25 VZ |
173 | }; |
174 | ||
a1b82138 VZ |
175 | // ---------------------------------------------------------------------------- |
176 | // event tables and other macros | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
9d112688 | 179 | BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) |
a1b82138 | 180 | EVT_CHAR(wxTextCtrl::OnChar) |
e2cf30aa | 181 | EVT_KEY_DOWN(wxTextCtrl::OnKeyDown) |
a1b82138 VZ |
182 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) |
183 | ||
2b5f62a0 | 184 | #if wxUSE_RICHEDIT |
26f60eb6 | 185 | EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu) |
2b5f62a0 VZ |
186 | #endif |
187 | ||
a1b82138 VZ |
188 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) |
189 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
190 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
191 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
192 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
2b5f62a0 VZ |
193 | EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete) |
194 | EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll) | |
a1b82138 VZ |
195 | |
196 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
197 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
198 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
199 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
200 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
2b5f62a0 VZ |
201 | EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete) |
202 | EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll) | |
e3a6a6b2 VZ |
203 | |
204 | EVT_SET_FOCUS(wxTextCtrl::OnSetFocus) | |
2bda0e17 | 205 | END_EVENT_TABLE() |
e702ff0f | 206 | |
a1b82138 VZ |
207 | // ============================================================================ |
208 | // implementation | |
209 | // ============================================================================ | |
210 | ||
211 | // ---------------------------------------------------------------------------- | |
212 | // creation | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
aac7e7fe | 215 | void wxTextCtrl::Init() |
2bda0e17 | 216 | { |
cd471848 | 217 | #if wxUSE_RICHEDIT |
aac7e7fe | 218 | m_verRichEdit = 0; |
2b5f62a0 | 219 | #endif // wxUSE_RICHEDIT |
5036ea90 | 220 | |
8ef51d67 JS |
221 | #if wxUSE_INKEDIT && wxUSE_RICHEDIT |
222 | m_isInkEdit = 0; | |
223 | #endif | |
224 | ||
2b5f62a0 | 225 | m_privateContextMenu = NULL; |
2c62dd25 | 226 | m_updatesCount = -1; |
e3a6a6b2 | 227 | m_isNativeCaretShown = true; |
2b5f62a0 VZ |
228 | } |
229 | ||
230 | wxTextCtrl::~wxTextCtrl() | |
231 | { | |
1a8e8d69 | 232 | #if wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT |
88b9909d VZ |
233 | if ( m_dropTarget == wxRICHTEXT_DEFAULT_DROPTARGET ) |
234 | { | |
235 | // don't try to destroy this dummy pointer in the base class dtor | |
236 | m_dropTarget = NULL; | |
237 | } | |
1a8e8d69 | 238 | #endif // wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT |
88b9909d | 239 | |
caea50ac | 240 | delete m_privateContextMenu; |
2bda0e17 KB |
241 | } |
242 | ||
9b99c1e3 VZ |
243 | bool wxTextCtrl::Create(wxWindow *parent, |
244 | wxWindowID id, | |
c085e333 VZ |
245 | const wxString& value, |
246 | const wxPoint& pos, | |
a1b82138 VZ |
247 | const wxSize& size, |
248 | long style, | |
c085e333 VZ |
249 | const wxValidator& validator, |
250 | const wxString& name) | |
2bda0e17 | 251 | { |
a1b82138 | 252 | // base initialization |
f4d233c7 | 253 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
bfbb0b4c | 254 | return false; |
2bda0e17 | 255 | |
9b99c1e3 VZ |
256 | if ( !MSWCreateText(value, pos, size) ) |
257 | return false; | |
258 | ||
20a0e999 VZ |
259 | #if wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT |
260 | if ( IsRich() ) | |
261 | { | |
262 | // rich text controls have a default associated drop target which | |
263 | // allows them to receive (rich) text dropped on them, which is nice, | |
264 | // but prevents us from associating a user-defined drop target with | |
265 | // them as we need to unregister the old one first | |
266 | // | |
267 | // to make it work, we set m_dropTarget to this special value initially | |
268 | // and check for it in our SetDropTarget() | |
269 | m_dropTarget = wxRICHTEXT_DEFAULT_DROPTARGET; | |
270 | } | |
271 | #endif // wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT | |
272 | ||
9b99c1e3 VZ |
273 | return true; |
274 | } | |
275 | ||
a047aff2 JS |
276 | // returns true if the platform should explicitly apply a theme border |
277 | bool wxTextCtrl::CanApplyThemeBorder() const | |
278 | { | |
279 | #ifdef __WXWINCE__ | |
280 | return false; | |
281 | #else | |
282 | // Standard text control already handles theming | |
283 | return ((GetWindowStyle() & (wxTE_RICH|wxTE_RICH2)) != 0); | |
284 | #endif | |
285 | } | |
286 | ||
9b99c1e3 VZ |
287 | bool wxTextCtrl::MSWCreateText(const wxString& value, |
288 | const wxPoint& pos, | |
289 | const wxSize& size) | |
290 | { | |
b2d5a7ee VZ |
291 | // translate wxWin style flags to MSW ones |
292 | WXDWORD msStyle = MSWGetCreateWindowFlags(); | |
cfdd3a98 | 293 | |
a1b82138 | 294 | // do create the control - either an EDIT or RICHEDIT |
b12915c1 | 295 | wxString windowClass = wxT("EDIT"); |
628c219e | 296 | |
afafd942 JS |
297 | #if defined(__POCKETPC__) || defined(__SMARTPHONE__) |
298 | // A control that capitalizes the first letter | |
9b99c1e3 | 299 | if ( HasFlag(wxTE_CAPITALIZE) ) |
afafd942 | 300 | windowClass = wxT("CAPEDIT"); |
628c219e | 301 | #endif |
cd471848 | 302 | |
57c208c5 | 303 | #if wxUSE_RICHEDIT |
a5aa8086 VZ |
304 | if ( m_windowStyle & wxTE_AUTO_URL ) |
305 | { | |
306 | // automatic URL detection only works in RichEdit 2.0+ | |
307 | m_windowStyle |= wxTE_RICH2; | |
308 | } | |
309 | ||
310 | if ( m_windowStyle & wxTE_RICH2 ) | |
311 | { | |
312 | // using richedit 2.0 implies using wxTE_RICH | |
313 | m_windowStyle |= wxTE_RICH; | |
314 | } | |
315 | ||
316 | // we need to load the richedit DLL before creating the rich edit control | |
c49245f8 | 317 | if ( m_windowStyle & wxTE_RICH ) |
a1b82138 | 318 | { |
628c219e VZ |
319 | // versions 2.0, 3.0 and 4.1 of rich edit are mostly compatible with |
320 | // each other but not with version 1.0, so we have separate flags for | |
321 | // the version 1.0 and the others (and so m_verRichEdit may be 0 (plain | |
322 | // EDIT control), 1 for version 1.0 or 2 for any higher version) | |
a5aa8086 | 323 | // |
628c219e VZ |
324 | // notice that 1.0 has no Unicode support at all so in Unicode build we |
325 | // must use another version | |
326 | ||
a5aa8086 | 327 | #if wxUSE_UNICODE |
628c219e | 328 | m_verRichEdit = 2; |
a5aa8086 | 329 | #else // !wxUSE_UNICODE |
628c219e | 330 | m_verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1; |
a5aa8086 | 331 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
0d0512bd | 332 | |
8ef51d67 JS |
333 | #if wxUSE_INKEDIT |
334 | // First test if we can load an ink edit control. Normally, all edit | |
335 | // controls will be made ink edit controls if a tablet environment is | |
336 | // found (or if msw.inkedit != 0 and the InkEd.dll is present). | |
337 | // However, an application can veto ink edit controls by either specifying | |
338 | // msw.inkedit = 0 or by removing wxTE_RICH[2] from the style. | |
57a67daf | 339 | // |
8ef51d67 JS |
340 | if ((wxSystemSettings::HasFeature(wxSYS_TABLET_PRESENT) || wxSystemOptions::GetOptionInt(wxT("msw.inkedit")) != 0) && |
341 | !(wxSystemOptions::HasOption(wxT("msw.inkedit")) && wxSystemOptions::GetOptionInt(wxT("msw.inkedit")) == 0)) | |
0d0512bd | 342 | { |
8ef51d67 | 343 | if (wxRichEditModule::LoadInkEdit()) |
57a67daf DS |
344 | { |
345 | windowClass = INKEDIT_CLASS; | |
346 | ||
347 | #if wxUSE_INKEDIT && wxUSE_RICHEDIT | |
8ef51d67 | 348 | m_isInkEdit = 1; |
57a67daf DS |
349 | #endif |
350 | ||
8ef51d67 JS |
351 | // Fake rich text version for other calls |
352 | m_verRichEdit = 2; | |
628c219e | 353 | } |
8ef51d67 JS |
354 | } |
355 | #endif | |
57a67daf | 356 | |
d2f434e4 | 357 | #if wxUSE_INKEDIT |
122d1d56 | 358 | if (!IsInkEdit()) |
d2f434e4 | 359 | #endif // wxUSE_INKEDIT |
8ef51d67 JS |
360 | { |
361 | if ( m_verRichEdit == 2 ) | |
40ff126a | 362 | { |
8ef51d67 JS |
363 | if ( wxRichEditModule::Load(wxRichEditModule::Version_41) ) |
364 | { | |
365 | // yes, class name for version 4.1 really is 5.0 | |
9a83f860 | 366 | windowClass = wxT("RICHEDIT50W"); |
4f7c00f2 VZ |
367 | |
368 | m_verRichEdit = 4; | |
8ef51d67 JS |
369 | } |
370 | else if ( wxRichEditModule::Load(wxRichEditModule::Version_2or3) ) | |
371 | { | |
9a83f860 | 372 | windowClass = wxT("RichEdit20") |
628c219e | 373 | #if wxUSE_UNICODE |
9a83f860 | 374 | wxT("W"); |
628c219e | 375 | #else // ANSI |
9a83f860 | 376 | wxT("A"); |
628c219e | 377 | #endif // Unicode/ANSI |
8ef51d67 JS |
378 | } |
379 | else // failed to load msftedit.dll and riched20.dll | |
380 | { | |
381 | m_verRichEdit = 1; | |
382 | } | |
628c219e | 383 | } |
0d0512bd | 384 | |
8ef51d67 | 385 | if ( m_verRichEdit == 1 ) |
b12915c1 | 386 | { |
8ef51d67 | 387 | if ( wxRichEditModule::Load(wxRichEditModule::Version_1) ) |
f8387e15 | 388 | { |
9a83f860 | 389 | windowClass = wxT("RICHEDIT"); |
f8387e15 | 390 | } |
8ef51d67 JS |
391 | else // failed to load any richedit control DLL |
392 | { | |
393 | // only give the error msg once if the DLL can't be loaded | |
394 | static bool s_errorGiven = false; // MT ok as only used by GUI | |
395 | ||
396 | if ( !s_errorGiven ) | |
397 | { | |
398 | wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll")); | |
628c219e | 399 | |
8ef51d67 JS |
400 | s_errorGiven = true; |
401 | } | |
402 | ||
403 | m_verRichEdit = 0; | |
404 | } | |
b12915c1 | 405 | } |
40ff126a | 406 | } // !useInkEdit |
a1b82138 | 407 | } |
b12915c1 | 408 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 409 | |
c8b204e6 VZ |
410 | // we need to turn '\n's into "\r\n"s for the multiline controls |
411 | wxString valueWin; | |
412 | if ( m_windowStyle & wxTE_MULTILINE ) | |
413 | { | |
414 | valueWin = wxTextFile::Translate(value, wxTextFileType_Dos); | |
415 | } | |
416 | else // single line | |
417 | { | |
418 | valueWin = value; | |
419 | } | |
420 | ||
8f08b250 VZ |
421 | // suppress events sent during control creation: we're called either from |
422 | // the ctor and then we shouldn't generate any events for compatibility | |
423 | // with the other ports, or from SetWindowStyleFlag() and then we shouldn't | |
424 | // generate the events because our text doesn't really change, the fact | |
425 | // that we (sometimes) need to recreate the control is just an | |
426 | // implementation detail | |
427 | m_updatesCount = -2; | |
428 | ||
e0a050e3 | 429 | if ( !MSWCreateControl(windowClass.wx_str(), msStyle, pos, size, valueWin) ) |
bfbb0b4c | 430 | return false; |
2bda0e17 | 431 | |
8f08b250 VZ |
432 | m_updatesCount = -1; |
433 | ||
57c208c5 | 434 | #if wxUSE_RICHEDIT |
8ef51d67 | 435 | if (IsRich()) |
a1b82138 | 436 | { |
8ef51d67 JS |
437 | #if wxUSE_INKEDIT |
438 | if (IsInkEdit()) | |
439 | { | |
440 | // Pass IEM_InsertText (0) as wParam, in order to have the ink always | |
441 | // converted to text. | |
442 | ::SendMessage(GetHwnd(), EM_SETINKINSERTMODE, 0, 0); | |
40ff126a | 443 | |
8ef51d67 JS |
444 | // Make sure the mouse can be used for input |
445 | ::SendMessage(GetHwnd(), EM_SETUSEMOUSEFORINPUT, 1, 0); | |
446 | } | |
447 | #endif | |
40ff126a | 448 | |
5036ea90 VZ |
449 | // enable the events we're interested in: we want to get EN_CHANGE as |
450 | // for the normal controls | |
451 | LPARAM mask = ENM_CHANGE; | |
c57e3339 | 452 | |
8ef51d67 | 453 | if (GetRichVersion() == 1 && !IsInkEdit()) |
1dae1d00 VZ |
454 | { |
455 | // we also need EN_MSGFILTER for richedit 1.0 for the reasons | |
456 | // explained in its handler | |
457 | mask |= ENM_MOUSEEVENTS; | |
9cf4b439 VZ |
458 | |
459 | // we also need to force the appearance of the vertical scrollbar | |
460 | // initially as otherwise the control doesn't refresh correctly | |
461 | // after resize: but once the vertical scrollbar had been shown | |
462 | // (even if it's subsequently hidden) it does | |
463 | // | |
464 | // this is clearly a bug and for now it has been only noticed under | |
465 | // Windows XP, so if we're sure it works correctly under other | |
466 | // systems we could do this only for XP | |
467 | SetSize(-1, 1); // 1 is small enough to force vert scrollbar | |
b370e758 | 468 | SetInitialSize(size); |
1dae1d00 VZ |
469 | } |
470 | else if ( m_windowStyle & wxTE_AUTO_URL ) | |
c57e3339 VZ |
471 | { |
472 | mask |= ENM_LINK; | |
473 | ||
474 | ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0); | |
475 | } | |
476 | ||
477 | ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask); | |
a1b82138 | 478 | } |
d2356abe | 479 | else |
c57e3339 | 480 | #endif // wxUSE_RICHEDIT |
d2356abe VZ |
481 | if ( HasFlag(wxTE_MULTILINE) && HasFlag(wxTE_READONLY) ) |
482 | { | |
483 | // non-rich read-only multiline controls have grey background by | |
484 | // default under MSW but this is not always appropriate, so forcefully | |
485 | // reset the background colour to normal default | |
486 | // | |
487 | // this is not ideal but, after a long discussion on wx-dev (see | |
488 | // http://thread.gmane.org/gmane.comp.lib.wxwidgets.devel/116360/) it | |
489 | // was finally deemed to be the best behaviour by default (and ideally | |
490 | // we'd have a way to change this, see #11521) | |
491 | SetBackgroundColour(GetClassDefaultAttributes().colBg); | |
492 | } | |
2bda0e17 | 493 | |
8c56ac83 JS |
494 | #ifndef __WXWINCE__ |
495 | // Without this, if we pass the size in the constructor and then don't change it, | |
496 | // the themed borders will be drawn incorrectly. | |
497 | SetWindowPos(GetHwnd(), NULL, 0, 0, 0, 0, | |
498 | SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE| | |
499 | SWP_FRAMECHANGED); | |
500 | #endif | |
501 | ||
bfbb0b4c | 502 | return true; |
2bda0e17 KB |
503 | } |
504 | ||
505 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 506 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 507 | { |
aac7e7fe | 508 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 509 | |
aac7e7fe VZ |
510 | HWND hWnd = GetHwnd(); |
511 | long style = ::GetWindowLong(hWnd, GWL_STYLE); | |
2bda0e17 | 512 | |
aac7e7fe | 513 | // retrieve the style to see whether this is an edit or richedit ctrl |
cd471848 | 514 | #if wxUSE_RICHEDIT |
aac7e7fe | 515 | wxString classname = wxGetWindowClass(GetHWND()); |
2bda0e17 | 516 | |
9a83f860 | 517 | if ( classname.IsSameAs(wxT("EDIT"), false /* no case */) ) |
aac7e7fe VZ |
518 | { |
519 | m_verRichEdit = 0; | |
520 | } | |
521 | else // rich edit? | |
522 | { | |
523 | wxChar c; | |
9a83f860 | 524 | if ( wxSscanf(classname, wxT("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 ) |
aac7e7fe | 525 | { |
9a83f860 | 526 | wxLogDebug(wxT("Unknown edit control '%s'."), classname.c_str()); |
2bda0e17 | 527 | |
aac7e7fe VZ |
528 | m_verRichEdit = 0; |
529 | } | |
530 | } | |
a1b82138 | 531 | #endif // wxUSE_RICHEDIT |
c085e333 | 532 | |
aac7e7fe VZ |
533 | if (style & ES_MULTILINE) |
534 | m_windowStyle |= wxTE_MULTILINE; | |
535 | if (style & ES_PASSWORD) | |
536 | m_windowStyle |= wxTE_PASSWORD; | |
537 | if (style & ES_READONLY) | |
538 | m_windowStyle |= wxTE_READONLY; | |
539 | if (style & ES_WANTRETURN) | |
540 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
e015d1f7 JS |
541 | if (style & ES_CENTER) |
542 | m_windowStyle |= wxTE_CENTRE; | |
543 | if (style & ES_RIGHT) | |
544 | m_windowStyle |= wxTE_RIGHT; | |
2bda0e17 KB |
545 | } |
546 | ||
b2d5a7ee VZ |
547 | WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const |
548 | { | |
549 | long msStyle = wxControl::MSWGetStyle(style, exstyle); | |
550 | ||
fa2f57be | 551 | // styles which we always add by default |
b2d5a7ee VZ |
552 | if ( style & wxTE_MULTILINE ) |
553 | { | |
b2d5a7ee VZ |
554 | msStyle |= ES_MULTILINE | ES_WANTRETURN; |
555 | if ( !(style & wxTE_NO_VSCROLL) ) | |
db50ec5a VZ |
556 | { |
557 | // always adjust the vertical scrollbar automatically if we have it | |
558 | msStyle |= WS_VSCROLL | ES_AUTOVSCROLL; | |
559 | ||
34433938 | 560 | #if wxUSE_RICHEDIT |
db50ec5a VZ |
561 | // we have to use this style for the rich edit controls because |
562 | // without it the vertical scrollbar never appears at all in | |
563 | // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it) | |
564 | if ( style & wxTE_RICH2 ) | |
565 | { | |
566 | msStyle |= ES_DISABLENOSCROLL; | |
567 | } | |
34433938 | 568 | #endif // wxUSE_RICHEDIT |
db50ec5a | 569 | } |
b2d5a7ee VZ |
570 | |
571 | style |= wxTE_PROCESS_ENTER; | |
572 | } | |
573 | else // !multiline | |
574 | { | |
575 | // there is really no reason to not have this style for single line | |
576 | // text controls | |
577 | msStyle |= ES_AUTOHSCROLL; | |
578 | } | |
579 | ||
0376ed54 VZ |
580 | // note that wxTE_DONTWRAP is the same as wxHSCROLL so if we have a horz |
581 | // scrollbar, there is no wrapping -- which makes sense | |
582 | if ( style & wxTE_DONTWRAP ) | |
db50ec5a VZ |
583 | { |
584 | // automatically scroll the control horizontally as necessary | |
ce192630 VZ |
585 | // |
586 | // NB: ES_AUTOHSCROLL is needed for richedit controls or they don't | |
587 | // show horz scrollbar at all, even in spite of WS_HSCROLL, and as | |
588 | // it doesn't seem to do any harm for plain edit controls, add it | |
589 | // always | |
590 | msStyle |= WS_HSCROLL | ES_AUTOHSCROLL; | |
db50ec5a | 591 | } |
b2d5a7ee VZ |
592 | |
593 | if ( style & wxTE_READONLY ) | |
594 | msStyle |= ES_READONLY; | |
595 | ||
596 | if ( style & wxTE_PASSWORD ) | |
597 | msStyle |= ES_PASSWORD; | |
598 | ||
b2d5a7ee VZ |
599 | if ( style & wxTE_NOHIDESEL ) |
600 | msStyle |= ES_NOHIDESEL; | |
601 | ||
db50ec5a | 602 | // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0 |
e015d1f7 JS |
603 | if ( style & wxTE_CENTRE ) |
604 | msStyle |= ES_CENTER; | |
db50ec5a | 605 | else if ( style & wxTE_RIGHT ) |
e015d1f7 | 606 | msStyle |= ES_RIGHT; |
db50ec5a | 607 | else |
0376ed54 | 608 | msStyle |= ES_LEFT; // ES_LEFT is 0 as well but for consistency... |
e015d1f7 | 609 | |
b2d5a7ee VZ |
610 | return msStyle; |
611 | } | |
612 | ||
613 | void wxTextCtrl::SetWindowStyleFlag(long style) | |
614 | { | |
66c72081 VZ |
615 | // changing the alignment of the control dynamically works under Win2003 |
616 | // (but not older Windows version: it seems to work under some versions of | |
617 | // XP but not other ones, and we have no way to determine it so be | |
618 | // conservative here) and only for plain EDIT controls (not RICH ones) and | |
619 | // we have to recreate the control to make it always work | |
620 | if ( IsRich() || wxGetWinVersion() < wxWinVersion_2003 ) | |
9b99c1e3 VZ |
621 | { |
622 | const long alignMask = wxTE_LEFT | wxTE_CENTRE | wxTE_RIGHT; | |
623 | if ( (style & alignMask) != (GetWindowStyle() & alignMask) ) | |
624 | { | |
625 | const wxString value = GetValue(); | |
626 | const wxPoint pos = GetPosition(); | |
627 | const wxSize size = GetSize(); | |
628 | ||
629 | // delete the old window | |
630 | HWND hwnd = GetHwnd(); | |
631 | DissociateHandle(); | |
632 | ::DestroyWindow(hwnd); | |
633 | ||
634 | // create the new one with the updated flags | |
635 | m_windowStyle = style; | |
636 | MSWCreateText(value, pos, size); | |
637 | ||
638 | // and make sure it has the same attributes as before | |
639 | if ( m_hasFont ) | |
640 | { | |
641 | // calling SetFont(m_font) would do nothing as the code would | |
642 | // notice that the font didn't change, so force it to believe | |
643 | // that it did | |
644 | wxFont font = m_font; | |
645 | m_font = wxNullFont; | |
646 | SetFont(font); | |
647 | } | |
648 | ||
649 | if ( m_hasFgCol ) | |
650 | { | |
651 | wxColour colFg = m_foregroundColour; | |
652 | m_foregroundColour = wxNullColour; | |
653 | SetForegroundColour(colFg); | |
654 | } | |
655 | ||
656 | if ( m_hasBgCol ) | |
657 | { | |
658 | wxColour colBg = m_backgroundColour; | |
659 | m_backgroundColour = wxNullColour; | |
660 | SetBackgroundColour(colBg); | |
661 | } | |
662 | ||
663 | // note that text styles are lost but this is probably not a big | |
664 | // problem: if you use styles, you probably don't use nor change | |
665 | // alignment flags anyhow | |
666 | ||
667 | return; | |
668 | } | |
669 | } | |
670 | ||
b2d5a7ee VZ |
671 | #if wxUSE_RICHEDIT |
672 | // we have to deal with some styles separately because they can't be | |
673 | // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed | |
674 | // using richedit-specific EM_SETOPTIONS | |
675 | if ( IsRich() && | |
676 | ((style & wxTE_NOHIDESEL) != (GetWindowStyle() & wxTE_NOHIDESEL)) ) | |
677 | { | |
678 | bool set = (style & wxTE_NOHIDESEL) != 0; | |
679 | ||
680 | ::SendMessage(GetHwnd(), EM_SETOPTIONS, set ? ECOOP_OR : ECOOP_AND, | |
681 | set ? ECO_NOHIDESEL : ~ECO_NOHIDESEL); | |
682 | } | |
683 | #endif // wxUSE_RICHEDIT | |
684 | ||
685 | wxControl::SetWindowStyleFlag(style); | |
686 | } | |
687 | ||
a1b82138 VZ |
688 | // ---------------------------------------------------------------------------- |
689 | // set/get the controls text | |
690 | // ---------------------------------------------------------------------------- | |
691 | ||
28fdd8db VZ |
692 | bool wxTextCtrl::IsEmpty() const |
693 | { | |
694 | // this is an optimization for multiline controls containing a lot of text | |
695 | if ( IsMultiLine() && GetNumberOfLines() != 1 ) | |
696 | return false; | |
697 | ||
698 | return wxTextCtrlBase::IsEmpty(); | |
699 | } | |
700 | ||
cd471848 | 701 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 702 | { |
a5aa8086 VZ |
703 | // range 0..-1 is special for GetRange() and means to retrieve all text |
704 | return GetRange(0, -1); | |
705 | } | |
706 | ||
707 | wxString wxTextCtrl::GetRange(long from, long to) const | |
708 | { | |
709 | wxString str; | |
710 | ||
711 | if ( from >= to && to != -1 ) | |
712 | { | |
713 | // nothing to retrieve | |
714 | return str; | |
715 | } | |
716 | ||
b12915c1 | 717 | #if wxUSE_RICHEDIT |
aac7e7fe | 718 | if ( IsRich() ) |
b12915c1 | 719 | { |
3988b155 | 720 | int len = GetWindowTextLength(GetHwnd()); |
a5aa8086 | 721 | if ( len > from ) |
3988b155 | 722 | { |
7411f983 VZ |
723 | if ( to == -1 ) |
724 | to = len; | |
725 | ||
11db7d81 | 726 | #if !wxUSE_UNICODE |
7411f983 VZ |
727 | // we must use EM_STREAMOUT if we don't want to lose all characters |
728 | // not representable in the current character set (EM_GETTEXTRANGE | |
729 | // simply replaces them with question marks...) | |
e669d184 | 730 | if ( GetRichVersion() > 1 ) |
7411f983 | 731 | { |
e669d184 VZ |
732 | // we must have some encoding, otherwise any 8bit chars in the |
733 | // control are simply *lost* (replaced by '?') | |
734 | wxFontEncoding encoding = wxFONTENCODING_SYSTEM; | |
735 | ||
7411f983 | 736 | wxFont font = m_defaultStyle.GetFont(); |
a1b806b9 | 737 | if ( !font.IsOk() ) |
7411f983 VZ |
738 | font = GetFont(); |
739 | ||
a1b806b9 | 740 | if ( font.IsOk() ) |
7411f983 | 741 | { |
e669d184 VZ |
742 | encoding = font.GetEncoding(); |
743 | } | |
744 | ||
beee6bf8 | 745 | #if wxUSE_INTL |
e669d184 VZ |
746 | if ( encoding == wxFONTENCODING_SYSTEM ) |
747 | { | |
748 | encoding = wxLocale::GetSystemEncoding(); | |
749 | } | |
beee6bf8 | 750 | #endif // wxUSE_INTL |
e669d184 VZ |
751 | |
752 | if ( encoding == wxFONTENCODING_SYSTEM ) | |
753 | { | |
754 | encoding = wxFONTENCODING_ISO8859_1; | |
755 | } | |
756 | ||
757 | str = StreamOut(encoding); | |
758 | ||
759 | if ( !str.empty() ) | |
760 | { | |
761 | // we have to manually extract the required part, luckily | |
52a9e329 VZ |
762 | // this is easy in this case as EOL characters in str are |
763 | // just LFs because we remove CRs in wxRichEditStreamOut | |
9ffa7227 | 764 | str = str.Mid(from, to - from); |
7411f983 VZ |
765 | } |
766 | } | |
767 | ||
768 | // StreamOut() wasn't used or failed, try to do it in normal way | |
769 | if ( str.empty() ) | |
11db7d81 | 770 | #endif // !wxUSE_UNICODE |
de564874 MB |
771 | { |
772 | // alloc one extra WORD as needed by the control | |
773 | wxStringBuffer tmp(str, ++len); | |
774 | wxChar *p = tmp; | |
b12915c1 | 775 | |
de564874 MB |
776 | TEXTRANGE textRange; |
777 | textRange.chrg.cpMin = from; | |
11db7d81 | 778 | textRange.chrg.cpMax = to; |
de564874 | 779 | textRange.lpstrText = p; |
b12915c1 | 780 | |
bfbb0b4c WS |
781 | (void)::SendMessage(GetHwnd(), EM_GETTEXTRANGE, |
782 | 0, (LPARAM)&textRange); | |
b12915c1 | 783 | |
de564874 | 784 | if ( m_verRichEdit > 1 ) |
a5aa8086 | 785 | { |
de564874 MB |
786 | // RichEdit 2.0 uses just CR ('\r') for the |
787 | // newlines which is neither Unix nor Windows | |
788 | // style - convert it to something reasonable | |
789 | for ( ; *p; p++ ) | |
790 | { | |
9a83f860 VZ |
791 | if ( *p == wxT('\r') ) |
792 | *p = wxT('\n'); | |
de564874 | 793 | } |
a5aa8086 | 794 | } |
3988b155 VZ |
795 | } |
796 | ||
a5aa8086 VZ |
797 | if ( m_verRichEdit == 1 ) |
798 | { | |
799 | // convert to the canonical form - see comment below | |
800 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
801 | } | |
3988b155 VZ |
802 | } |
803 | //else: no text at all, leave the string empty | |
b12915c1 | 804 | } |
a5aa8086 | 805 | else |
b12915c1 | 806 | #endif // wxUSE_RICHEDIT |
a5aa8086 | 807 | { |
135b23b2 VZ |
808 | // retrieve all text: wxTextEntry method works even for multiline |
809 | // controls and must be used for single line ones to account for hints | |
810 | str = wxTextEntry::GetValue(); | |
b12915c1 | 811 | |
a5aa8086 VZ |
812 | // need only a range? |
813 | if ( from < to ) | |
814 | { | |
815 | str = str.Mid(from, to - from); | |
816 | } | |
817 | ||
818 | // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the | |
819 | // canonical one (same one as above) for consistency with the other kinds | |
820 | // of controls and, more importantly, with the other ports | |
821 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
822 | } | |
b12915c1 | 823 | |
a5aa8086 | 824 | return str; |
2bda0e17 KB |
825 | } |
826 | ||
f6519b40 | 827 | void wxTextCtrl::DoSetValue(const wxString& value, int flags) |
2bda0e17 | 828 | { |
b12915c1 VZ |
829 | // if the text is long enough, it's faster to just set it instead of first |
830 | // comparing it with the old one (chances are that it will be different | |
831 | // anyhow, this comparison is there to avoid flicker for small single-line | |
832 | // edit controls mostly) | |
135b23b2 | 833 | if ( (value.length() > 0x400) || (value != DoGetValue()) ) |
07cf98cb | 834 | { |
dd1c1631 | 835 | DoWriteText(value, flags /* doesn't include SelectionOnly here */); |
bfbb0b4c | 836 | |
63f84de8 VZ |
837 | // mark the control as being not dirty - we changed its text, not the |
838 | // user | |
839 | DiscardEdits(); | |
840 | ||
120249f6 JS |
841 | // for compatibility, don't move the cursor when doing SetValue() |
842 | SetInsertionPoint(0); | |
da32743f | 843 | } |
199fbd70 VZ |
844 | else // same text |
845 | { | |
63f84de8 VZ |
846 | // still reset the modified flag even if the value didn't really change |
847 | // because now it comes from the program and not the user (and do it | |
848 | // before generating the event so that the event handler could get the | |
849 | // expected value from IsModified()) | |
850 | DiscardEdits(); | |
851 | ||
199fbd70 | 852 | // still send an event for consistency |
f6519b40 VZ |
853 | if (flags & SetValue_SendEvent) |
854 | SendUpdateEvent(); | |
199fbd70 | 855 | } |
aac7e7fe | 856 | } |
a1b82138 | 857 | |
0b8e5844 | 858 | #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU) |
f6bcfd97 | 859 | |
7411f983 VZ |
860 | // TODO: using memcpy() would improve performance a lot for big amounts of text |
861 | ||
862 | DWORD CALLBACK | |
92715f1f | 863 | wxRichEditStreamIn(DWORD_PTR dwCookie, BYTE *buf, LONG cb, LONG *pcb) |
aac7e7fe VZ |
864 | { |
865 | *pcb = 0; | |
f6bcfd97 | 866 | |
7411f983 VZ |
867 | const wchar_t ** const ppws = (const wchar_t **)dwCookie; |
868 | ||
aac7e7fe | 869 | wchar_t *wbuf = (wchar_t *)buf; |
7411f983 | 870 | const wchar_t *wpc = *ppws; |
aac7e7fe VZ |
871 | while ( cb && *wpc ) |
872 | { | |
873 | *wbuf++ = *wpc++; | |
874 | ||
875 | cb -= sizeof(wchar_t); | |
876 | (*pcb) += sizeof(wchar_t); | |
07cf98cb | 877 | } |
aac7e7fe | 878 | |
7411f983 VZ |
879 | *ppws = wpc; |
880 | ||
881 | return 0; | |
882 | } | |
883 | ||
52a9e329 VZ |
884 | // helper struct used to pass parameters from wxTextCtrl to wxRichEditStreamOut |
885 | struct wxStreamOutData | |
886 | { | |
887 | wchar_t *wpc; | |
888 | size_t len; | |
889 | }; | |
890 | ||
7411f983 | 891 | DWORD CALLBACK |
975b6bcf | 892 | wxRichEditStreamOut(DWORD_PTR dwCookie, BYTE *buf, LONG cb, LONG *pcb) |
7411f983 VZ |
893 | { |
894 | *pcb = 0; | |
895 | ||
52a9e329 | 896 | wxStreamOutData *data = (wxStreamOutData *)dwCookie; |
7411f983 VZ |
897 | |
898 | const wchar_t *wbuf = (const wchar_t *)buf; | |
52a9e329 VZ |
899 | wchar_t *wpc = data->wpc; |
900 | while ( cb ) | |
7411f983 | 901 | { |
52a9e329 VZ |
902 | wchar_t wch = *wbuf++; |
903 | ||
904 | // turn "\r\n" into "\n" on the fly | |
905 | if ( wch != L'\r' ) | |
906 | *wpc++ = wch; | |
907 | else | |
908 | data->len--; | |
7411f983 VZ |
909 | |
910 | cb -= sizeof(wchar_t); | |
911 | (*pcb) += sizeof(wchar_t); | |
912 | } | |
913 | ||
52a9e329 | 914 | data->wpc = wpc; |
aac7e7fe VZ |
915 | |
916 | return 0; | |
2bda0e17 KB |
917 | } |
918 | ||
7411f983 | 919 | |
0b8e5844 | 920 | #if wxUSE_UNICODE_MSLU |
7411f983 VZ |
921 | #define UNUSED_IF_MSLU(param) |
922 | #else | |
923 | #define UNUSED_IF_MSLU(param) param | |
924 | #endif | |
925 | ||
926 | bool | |
927 | wxTextCtrl::StreamIn(const wxString& value, | |
928 | wxFontEncoding UNUSED_IF_MSLU(encoding), | |
929 | bool selectionOnly) | |
0b8e5844 | 930 | { |
7411f983 | 931 | #if wxUSE_UNICODE_MSLU |
0b8e5844 | 932 | const wchar_t *wpc = value.c_str(); |
79d26b32 | 933 | #else // !wxUSE_UNICODE_MSLU |
6a983211 | 934 | wxCSConv conv(encoding); |
aac7e7fe | 935 | |
414721d0 | 936 | const size_t len = conv.MB2WC(NULL, value.mb_str(), value.length()); |
855d6be7 | 937 | |
b212c69e JS |
938 | if (len == wxCONV_FAILED) |
939 | return false; | |
940 | ||
51725fc0 | 941 | wxWCharBuffer wchBuf(len); // allocates one extra character |
6a983211 | 942 | wchar_t *wpc = wchBuf.data(); |
855d6be7 | 943 | |
51725fc0 | 944 | conv.MB2WC(wpc, value.mb_str(), len + 1); |
0b8e5844 | 945 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe | 946 | |
6a983211 | 947 | // finally, stream it in the control |
aac7e7fe VZ |
948 | EDITSTREAM eds; |
949 | wxZeroMemory(eds); | |
92715f1f | 950 | eds.dwCookie = (DWORD_PTR)&wpc; |
7a25a27c VZ |
951 | // the cast below is needed for broken (very) old mingw32 headers |
952 | eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn; | |
92209a39 | 953 | |
2c62dd25 VZ |
954 | // same problem as in DoWriteText(): we can get multiple events here |
955 | UpdatesCountFilter ucf(m_updatesCount); | |
2b5f62a0 | 956 | |
07601f59 VZ |
957 | ::SendMessage(GetHwnd(), EM_STREAMIN, |
958 | SF_TEXT | | |
959 | SF_UNICODE | | |
960 | (selectionOnly ? SFF_SELECTION : 0), | |
961 | (LPARAM)&eds); | |
962 | ||
fa31aeda RD |
963 | // It's okay for EN_UPDATE to not be sent if the selection is empty and |
964 | // the text is empty, otherwise warn the programmer about it. | |
965 | wxASSERT_MSG( ucf.GotUpdate() || ( !HasSelection() && value.empty() ), | |
9a83f860 | 966 | wxT("EM_STREAMIN didn't send EN_UPDATE?") ); |
2c62dd25 | 967 | |
07601f59 | 968 | if ( eds.dwError ) |
aac7e7fe | 969 | { |
9a83f860 | 970 | wxLogLastError(wxT("EM_STREAMIN")); |
aac7e7fe VZ |
971 | } |
972 | ||
bfbb0b4c | 973 | return true; |
aac7e7fe VZ |
974 | } |
975 | ||
7411f983 VZ |
976 | #if !wxUSE_UNICODE_MSLU |
977 | ||
978 | wxString | |
979 | wxTextCtrl::StreamOut(wxFontEncoding encoding, bool selectionOnly) const | |
980 | { | |
981 | wxString out; | |
982 | ||
983 | const int len = GetWindowTextLength(GetHwnd()); | |
984 | ||
7411f983 VZ |
985 | wxWCharBuffer wchBuf(len); |
986 | wchar_t *wpc = wchBuf.data(); | |
7411f983 | 987 | |
52a9e329 VZ |
988 | wxStreamOutData data; |
989 | data.wpc = wpc; | |
990 | data.len = len; | |
991 | ||
7411f983 VZ |
992 | EDITSTREAM eds; |
993 | wxZeroMemory(eds); | |
52a9e329 | 994 | eds.dwCookie = (DWORD)&data; |
7411f983 VZ |
995 | eds.pfnCallback = wxRichEditStreamOut; |
996 | ||
997 | ::SendMessage | |
998 | ( | |
999 | GetHwnd(), | |
1000 | EM_STREAMOUT, | |
1001 | SF_TEXT | SF_UNICODE | (selectionOnly ? SFF_SELECTION : 0), | |
1002 | (LPARAM)&eds | |
1003 | ); | |
1004 | ||
1005 | if ( eds.dwError ) | |
1006 | { | |
9a83f860 | 1007 | wxLogLastError(wxT("EM_STREAMOUT")); |
7411f983 VZ |
1008 | } |
1009 | else // streamed out ok | |
1010 | { | |
52a9e329 VZ |
1011 | // NUL-terminate the string because its length could have been |
1012 | // decreased by wxRichEditStreamOut | |
1013 | *(wchBuf.data() + data.len) = L'\0'; | |
1014 | ||
b26613c2 VZ |
1015 | // now convert to the given encoding (this is a possibly lossful |
1016 | // conversion but what else can we do) | |
7411f983 | 1017 | wxCSConv conv(encoding); |
b26613c2 | 1018 | size_t lenNeeded = conv.WC2MB(NULL, wchBuf, 0); |
b212c69e JS |
1019 | |
1020 | if ( lenNeeded != wxCONV_FAILED && lenNeeded++ ) | |
7411f983 | 1021 | { |
b26613c2 | 1022 | conv.WC2MB(wxStringBuffer(out, lenNeeded), wchBuf, lenNeeded); |
7411f983 VZ |
1023 | } |
1024 | } | |
1025 | ||
7411f983 VZ |
1026 | return out; |
1027 | } | |
1028 | ||
1029 | #endif // !wxUSE_UNICODE_MSLU | |
1030 | ||
aac7e7fe VZ |
1031 | #endif // wxUSE_RICHEDIT |
1032 | ||
a1b82138 | 1033 | void wxTextCtrl::WriteText(const wxString& value) |
79d26b32 VZ |
1034 | { |
1035 | DoWriteText(value); | |
1036 | } | |
1037 | ||
f6519b40 | 1038 | void wxTextCtrl::DoWriteText(const wxString& value, int flags) |
2bda0e17 | 1039 | { |
f6519b40 | 1040 | bool selectionOnly = (flags & SetValue_SelectionOnly) != 0; |
a5aa8086 VZ |
1041 | wxString valueDos; |
1042 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1043 | valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
1044 | else | |
1045 | valueDos = value; | |
2bda0e17 | 1046 | |
4bc1afd5 | 1047 | #if wxUSE_RICHEDIT |
aac7e7fe | 1048 | // there are several complications with the rich edit controls here |
bfbb0b4c | 1049 | bool done = false; |
aac7e7fe | 1050 | if ( IsRich() ) |
4bc1afd5 | 1051 | { |
aac7e7fe VZ |
1052 | // first, ensure that the new text will be in the default style |
1053 | if ( !m_defaultStyle.IsDefault() ) | |
1054 | { | |
1055 | long start, end; | |
1056 | GetSelection(&start, &end); | |
0b8e5844 VS |
1057 | SetStyle(start, end, m_defaultStyle); |
1058 | } | |
1059 | ||
1060 | #if wxUSE_UNICODE_MSLU | |
1061 | // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x, | |
1062 | // but EM_STREAMIN works | |
136cb3c7 | 1063 | if ( wxUsingUnicowsDll() && GetRichVersion() > 1 ) |
0b8e5844 | 1064 | { |
79d26b32 | 1065 | done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly); |
aac7e7fe | 1066 | } |
0b8e5844 | 1067 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe | 1068 | |
b4da152e | 1069 | #if !wxUSE_UNICODE |
aac7e7fe VZ |
1070 | // next check if the text we're inserting must be shown in a non |
1071 | // default charset -- this only works for RichEdit > 1.0 | |
1072 | if ( GetRichVersion() > 1 ) | |
1073 | { | |
1074 | wxFont font = m_defaultStyle.GetFont(); | |
a1b806b9 | 1075 | if ( !font.IsOk() ) |
aac7e7fe VZ |
1076 | font = GetFont(); |
1077 | ||
a1b806b9 | 1078 | if ( font.IsOk() ) |
aac7e7fe VZ |
1079 | { |
1080 | wxFontEncoding encoding = font.GetEncoding(); | |
1081 | if ( encoding != wxFONTENCODING_SYSTEM ) | |
1082 | { | |
6a983211 VZ |
1083 | // we have to use EM_STREAMIN to force richedit control 2.0+ |
1084 | // to show any text in the non default charset -- otherwise | |
1085 | // it thinks it knows better than we do and always shows it | |
1086 | // in the default one | |
79d26b32 | 1087 | done = StreamIn(valueDos, encoding, selectionOnly); |
aac7e7fe VZ |
1088 | } |
1089 | } | |
1090 | } | |
9d7de3c2 | 1091 | #endif // !wxUSE_UNICODE |
4bc1afd5 | 1092 | } |
4bc1afd5 | 1093 | |
aac7e7fe VZ |
1094 | if ( !done ) |
1095 | #endif // wxUSE_RICHEDIT | |
1096 | { | |
2b5f62a0 | 1097 | // in some cases we get 2 EN_CHANGE notifications after the SendMessage |
2c62dd25 VZ |
1098 | // call (this happens for plain EDITs with EM_REPLACESEL and under some |
1099 | // -- undetermined -- conditions with rich edit) and sometimes we don't | |
1100 | // get any events at all (plain EDIT with WM_SETTEXT), so ensure that | |
1101 | // we generate exactly one of them by ignoring all but the first one in | |
1102 | // SendUpdateEvent() and generating one ourselves if we hadn't got any | |
1103 | // notifications from Windows | |
f6519b40 VZ |
1104 | if ( !(flags & SetValue_SendEvent) ) |
1105 | m_updatesCount = -2; // suppress any update event | |
1106 | ||
2c62dd25 | 1107 | UpdatesCountFilter ucf(m_updatesCount); |
79d26b32 | 1108 | |
5036ea90 | 1109 | ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT, |
6e9e2d94 | 1110 | // EM_REPLACESEL takes 1 to indicate the operation should be redoable |
c9f78968 | 1111 | selectionOnly ? 1 : 0, (LPARAM)valueDos.wx_str()); |
2b5f62a0 | 1112 | |
f6519b40 | 1113 | if ( !ucf.GotUpdate() && (flags & SetValue_SendEvent) ) |
2b5f62a0 | 1114 | { |
2c62dd25 | 1115 | SendUpdateEvent(); |
2b5f62a0 | 1116 | } |
aac7e7fe | 1117 | } |
2bda0e17 KB |
1118 | } |
1119 | ||
a1b82138 VZ |
1120 | void wxTextCtrl::AppendText(const wxString& text) |
1121 | { | |
fa2f57be | 1122 | wxTextEntry::AppendText(text); |
2b5f62a0 VZ |
1123 | |
1124 | #if wxUSE_RICHEDIT | |
caea50ac VZ |
1125 | // don't do this if we're frozen, saves some time |
1126 | if ( !IsFrozen() && IsMultiLine() && GetRichVersion() > 1 ) | |
2b5f62a0 | 1127 | { |
3d55f45e | 1128 | ::SendMessage(GetHwnd(), WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL); |
2b5f62a0 VZ |
1129 | } |
1130 | #endif // wxUSE_RICHEDIT | |
a1b82138 VZ |
1131 | } |
1132 | ||
1133 | void wxTextCtrl::Clear() | |
1134 | { | |
fda7962d | 1135 | ::SetWindowText(GetHwnd(), wxEmptyString); |
5036ea90 | 1136 | |
fa2f57be | 1137 | if ( IsMultiLine() && !IsRich() ) |
5036ea90 VZ |
1138 | { |
1139 | // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves | |
1140 | // but the normal ones don't -- make Clear() behaviour consistent by | |
1141 | // always sending this event | |
fa2f57be | 1142 | SendUpdateEvent(); |
5036ea90 | 1143 | } |
a1b82138 VZ |
1144 | } |
1145 | ||
94af7d45 VZ |
1146 | #ifdef __WIN32__ |
1147 | ||
1148 | bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event) | |
1149 | { | |
1150 | SetFocus(); | |
1151 | ||
1152 | size_t lenOld = GetValue().length(); | |
1153 | ||
1154 | wxUint32 code = event.GetRawKeyCode(); | |
5c519b6c WS |
1155 | ::keybd_event((BYTE)code, 0, 0 /* key press */, 0); |
1156 | ::keybd_event((BYTE)code, 0, KEYEVENTF_KEYUP, 0); | |
94af7d45 VZ |
1157 | |
1158 | // assume that any alphanumeric key changes the total number of characters | |
1159 | // in the control - this should work in 99% of cases | |
1160 | return GetValue().length() != lenOld; | |
1161 | } | |
1162 | ||
1163 | #endif // __WIN32__ | |
1164 | ||
a1b82138 VZ |
1165 | // ---------------------------------------------------------------------------- |
1166 | // Accessors | |
1167 | // ---------------------------------------------------------------------------- | |
1168 | ||
cd471848 | 1169 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 1170 | { |
a9d3434a VZ |
1171 | // we must not do anything if the caret is already there because calling |
1172 | // SetInsertionPoint() thaws the controls if Freeze() had been called even | |
1173 | // if it doesn't actually move the caret anywhere and so the simple fact of | |
1174 | // doing it results in horrible flicker when appending big amounts of text | |
1175 | // to the control in a few chunks (see DoAddText() test in the text sample) | |
37f11dee VZ |
1176 | const wxTextPos lastPosition = GetLastPosition(); |
1177 | if ( GetInsertionPoint() == lastPosition ) | |
caea50ac | 1178 | { |
a9d3434a | 1179 | return; |
caea50ac | 1180 | } |
a9d3434a | 1181 | |
fa2f57be | 1182 | SetInsertionPoint(lastPosition); |
2bda0e17 KB |
1183 | } |
1184 | ||
cd471848 | 1185 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 1186 | { |
57c208c5 | 1187 | #if wxUSE_RICHEDIT |
aac7e7fe | 1188 | if ( IsRich() ) |
a1b82138 VZ |
1189 | { |
1190 | CHARRANGE range; | |
1191 | range.cpMin = 0; | |
1192 | range.cpMax = 0; | |
bfbb0b4c | 1193 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); |
a1b82138 VZ |
1194 | return range.cpMin; |
1195 | } | |
aac7e7fe | 1196 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 1197 | |
fa2f57be | 1198 | return wxTextEntry::GetInsertionPoint(); |
2bda0e17 KB |
1199 | } |
1200 | ||
7d8268a1 | 1201 | wxTextPos wxTextCtrl::GetLastPosition() const |
2bda0e17 | 1202 | { |
fa2f57be VZ |
1203 | if ( IsMultiLine() ) |
1204 | { | |
1205 | int numLines = GetNumberOfLines(); | |
1206 | long posStartLastLine = XYToPosition(0, numLines - 1); | |
39136494 | 1207 | |
fa2f57be | 1208 | long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); |
2bda0e17 | 1209 | |
fa2f57be VZ |
1210 | return posStartLastLine + lenLastLine; |
1211 | } | |
1212 | ||
1213 | return wxTextEntry::GetLastPosition(); | |
2bda0e17 KB |
1214 | } |
1215 | ||
a1b82138 VZ |
1216 | // If the return values from and to are the same, there is no |
1217 | // selection. | |
fa2f57be | 1218 | void wxTextCtrl::GetSelection(long *from, long *to) const |
a1b82138 VZ |
1219 | { |
1220 | #if wxUSE_RICHEDIT | |
aac7e7fe | 1221 | if ( IsRich() ) |
a1b82138 VZ |
1222 | { |
1223 | CHARRANGE charRange; | |
aac7e7fe | 1224 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange); |
a1b82138 VZ |
1225 | |
1226 | *from = charRange.cpMin; | |
1227 | *to = charRange.cpMax; | |
a1b82138 | 1228 | } |
4bc1afd5 | 1229 | else |
aac7e7fe | 1230 | #endif // !wxUSE_RICHEDIT |
4bc1afd5 | 1231 | { |
fa2f57be | 1232 | wxTextEntry::GetSelection(from, to); |
4bc1afd5 | 1233 | } |
a1b82138 VZ |
1234 | } |
1235 | ||
a1b82138 | 1236 | // ---------------------------------------------------------------------------- |
aac7e7fe | 1237 | // selection |
a1b82138 VZ |
1238 | // ---------------------------------------------------------------------------- |
1239 | ||
fa2f57be | 1240 | void wxTextCtrl::DoSetSelection(long from, long to, int flags) |
a5aa8086 | 1241 | { |
789295bf | 1242 | HWND hWnd = GetHwnd(); |
39136494 | 1243 | |
aac7e7fe VZ |
1244 | #if wxUSE_RICHEDIT |
1245 | if ( IsRich() ) | |
1246 | { | |
2e771cb8 VS |
1247 | // if from and to are both -1, it means (in wxWidgets) that all text |
1248 | // should be selected, translate this into Windows convention | |
1249 | if ( (from == -1) && (to == -1) ) | |
1250 | { | |
1251 | from = 0; | |
1252 | } | |
1253 | ||
db50ec5a VZ |
1254 | CHARRANGE range; |
1255 | range.cpMin = from; | |
1256 | range.cpMax = to; | |
fa2f57be | 1257 | ::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM)&range); |
db50ec5a VZ |
1258 | } |
1259 | else | |
1260 | #endif // wxUSE_RICHEDIT | |
1261 | { | |
8df26961 | 1262 | wxTextEntry::DoSetSelection(from, to, flags); |
db50ec5a VZ |
1263 | } |
1264 | ||
fa2f57be | 1265 | if ( (flags & SetSel_Scroll) && !IsFrozen() ) |
db50ec5a VZ |
1266 | { |
1267 | #if wxUSE_RICHEDIT | |
98e19a58 VZ |
1268 | // richedit 3.0 (i.e. the version living in riched20.dll distributed |
1269 | // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when | |
1270 | // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL | |
1271 | // option is set (but it does work ok in richedit 1.0 mode...) | |
1272 | // | |
1273 | // so to make it work we either need to give focus to it here which | |
1274 | // will probably create many problems (dummy focus events; window | |
1275 | // containing the text control being brought to foreground | |
1276 | // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may | |
db50ec5a VZ |
1277 | // create other problems too -- and in fact it does because if we turn |
1278 | // on/off this style while appending the text to the control, the | |
1279 | // vertical scrollbar never appears in it even if we append tons of | |
1280 | // text and to work around this the only solution I found was to use | |
1281 | // ES_DISABLENOSCROLL | |
1282 | // | |
1283 | // this is very ugly but I don't see any other way to make this work | |
b7a3ba7d | 1284 | long style = 0; |
98e19a58 VZ |
1285 | if ( GetRichVersion() > 1 ) |
1286 | { | |
1287 | if ( !HasFlag(wxTE_NOHIDESEL) ) | |
1288 | { | |
b7a3ba7d JG |
1289 | // setting ECO_NOHIDESEL also sets WS_VISIBLE and possibly |
1290 | // others, remember the style so we can reset it later if needed | |
1291 | style = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
98e19a58 VZ |
1292 | ::SendMessage(GetHwnd(), EM_SETOPTIONS, |
1293 | ECOOP_OR, ECO_NOHIDESEL); | |
1294 | } | |
1295 | //else: everything is already ok | |
1296 | } | |
aac7e7fe | 1297 | #endif // wxUSE_RICHEDIT |
a1b82138 | 1298 | |
fa2f57be | 1299 | ::SendMessage(hWnd, EM_SCROLLCARET, 0, (LPARAM)0); |
98e19a58 VZ |
1300 | |
1301 | #if wxUSE_RICHEDIT | |
db50ec5a VZ |
1302 | // restore ECO_NOHIDESEL if we changed it |
1303 | if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL) ) | |
1304 | { | |
1305 | ::SendMessage(GetHwnd(), EM_SETOPTIONS, | |
1306 | ECOOP_AND, ~ECO_NOHIDESEL); | |
b7a3ba7d JG |
1307 | if ( style != ::GetWindowLong(GetHwnd(), GWL_STYLE) ) |
1308 | ::SetWindowLong(GetHwnd(), GWL_STYLE, style); | |
db50ec5a | 1309 | } |
98e19a58 | 1310 | #endif // wxUSE_RICHEDIT |
db50ec5a | 1311 | } |
aac7e7fe VZ |
1312 | } |
1313 | ||
efe66bbc VZ |
1314 | // ---------------------------------------------------------------------------- |
1315 | // Working with files | |
1316 | // ---------------------------------------------------------------------------- | |
1317 | ||
3306aec1 | 1318 | bool wxTextCtrl::DoLoadFile(const wxString& file, int fileType) |
efe66bbc | 1319 | { |
3306aec1 | 1320 | if ( wxTextCtrlBase::DoLoadFile(file, fileType) ) |
efe66bbc VZ |
1321 | { |
1322 | // update the size limit if needed | |
1323 | AdjustSpaceLimit(); | |
1324 | ||
bfbb0b4c | 1325 | return true; |
efe66bbc VZ |
1326 | } |
1327 | ||
bfbb0b4c | 1328 | return false; |
efe66bbc VZ |
1329 | } |
1330 | ||
aac7e7fe | 1331 | // ---------------------------------------------------------------------------- |
fa2f57be | 1332 | // dirty status |
aac7e7fe | 1333 | // ---------------------------------------------------------------------------- |
39136494 | 1334 | |
cd471848 | 1335 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 1336 | { |
bfbb0b4c | 1337 | return ::SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; |
2bda0e17 KB |
1338 | } |
1339 | ||
3a9fa0d6 VZ |
1340 | void wxTextCtrl::MarkDirty() |
1341 | { | |
fa2f57be | 1342 | ::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0); |
3a9fa0d6 VZ |
1343 | } |
1344 | ||
cd471848 | 1345 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 1346 | { |
fa2f57be | 1347 | ::SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0); |
2bda0e17 KB |
1348 | } |
1349 | ||
efe66bbc VZ |
1350 | // ---------------------------------------------------------------------------- |
1351 | // Positions <-> coords | |
1352 | // ---------------------------------------------------------------------------- | |
1353 | ||
fa2f57be VZ |
1354 | int wxTextCtrl::GetNumberOfLines() const |
1355 | { | |
1356 | return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT, 0, 0); | |
1357 | } | |
1358 | ||
debe6624 | 1359 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 1360 | { |
2bda0e17 | 1361 | // This gets the char index for the _beginning_ of this line |
fa2f57be | 1362 | long charIndex = ::SendMessage(GetHwnd(), EM_LINEINDEX, y, 0); |
a5aa8086 VZ |
1363 | |
1364 | return charIndex + x; | |
2bda0e17 KB |
1365 | } |
1366 | ||
0efe5ba7 | 1367 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 1368 | { |
789295bf | 1369 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1370 | |
1371 | // This gets the line number containing the character | |
a5aa8086 | 1372 | long lineNo; |
0efe5ba7 | 1373 | #if wxUSE_RICHEDIT |
aac7e7fe | 1374 | if ( IsRich() ) |
0efe5ba7 | 1375 | { |
fa2f57be | 1376 | lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, pos); |
0efe5ba7 VZ |
1377 | } |
1378 | else | |
1379 | #endif // wxUSE_RICHEDIT | |
a5aa8086 | 1380 | { |
fa2f57be | 1381 | lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0); |
a5aa8086 | 1382 | } |
0efe5ba7 VZ |
1383 | |
1384 | if ( lineNo == -1 ) | |
1385 | { | |
1386 | // no such line | |
bfbb0b4c | 1387 | return false; |
0efe5ba7 VZ |
1388 | } |
1389 | ||
2bda0e17 | 1390 | // This gets the char index for the _beginning_ of this line |
fa2f57be | 1391 | long charIndex = ::SendMessage(hWnd, EM_LINEINDEX, lineNo, 0); |
0efe5ba7 VZ |
1392 | if ( charIndex == -1 ) |
1393 | { | |
bfbb0b4c | 1394 | return false; |
0efe5ba7 VZ |
1395 | } |
1396 | ||
2bda0e17 | 1397 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 | 1398 | if ( x ) |
a5aa8086 | 1399 | *x = pos - charIndex; |
0efe5ba7 | 1400 | if ( y ) |
a5aa8086 | 1401 | *y = lineNo; |
0efe5ba7 | 1402 | |
bfbb0b4c | 1403 | return true; |
2bda0e17 KB |
1404 | } |
1405 | ||
efe66bbc | 1406 | wxTextCtrlHitTestResult |
6726a6b0 | 1407 | wxTextCtrl::HitTest(const wxPoint& pt, long *posOut) const |
efe66bbc VZ |
1408 | { |
1409 | // first get the position from Windows | |
1410 | LPARAM lParam; | |
1411 | ||
1412 | #if wxUSE_RICHEDIT | |
1413 | POINTL ptl; | |
1414 | if ( IsRich() ) | |
1415 | { | |
1416 | // for rich edit controls the position is passed iva the struct fields | |
1417 | ptl.x = pt.x; | |
1418 | ptl.y = pt.y; | |
1419 | lParam = (LPARAM)&ptl; | |
1420 | } | |
1421 | else | |
1422 | #endif // wxUSE_RICHEDIT | |
1423 | { | |
1424 | // for the plain ones, we are limited to 16 bit positions which are | |
1425 | // combined in a single 32 bit value | |
1426 | lParam = MAKELPARAM(pt.x, pt.y); | |
1427 | } | |
1428 | ||
bfbb0b4c | 1429 | LRESULT pos = ::SendMessage(GetHwnd(), EM_CHARFROMPOS, 0, lParam); |
efe66bbc VZ |
1430 | |
1431 | if ( pos == -1 ) | |
1432 | { | |
1433 | // this seems to indicate an error... | |
1434 | return wxTE_HT_UNKNOWN; | |
1435 | } | |
1436 | ||
1437 | #if wxUSE_RICHEDIT | |
1438 | if ( !IsRich() ) | |
1439 | #endif // wxUSE_RICHEDIT | |
1440 | { | |
1441 | // for plain EDIT controls the higher word contains something else | |
1442 | pos = LOWORD(pos); | |
1443 | } | |
1444 | ||
1445 | ||
1446 | // next determine where it is relatively to our point: EM_CHARFROMPOS | |
1447 | // always returns the closest character but we need to be more precise, so | |
1448 | // double check that we really are where it pretends | |
1449 | POINTL ptReal; | |
1450 | ||
1451 | #if wxUSE_RICHEDIT | |
1452 | // FIXME: we need to distinguish between richedit 2 and 3 here somehow but | |
1453 | // we don't know how to do it | |
1454 | if ( IsRich() ) | |
1455 | { | |
bfbb0b4c | 1456 | ::SendMessage(GetHwnd(), EM_POSFROMCHAR, (WPARAM)&ptReal, pos); |
efe66bbc VZ |
1457 | } |
1458 | else | |
1459 | #endif // wxUSE_RICHEDIT | |
1460 | { | |
bfbb0b4c | 1461 | LRESULT lRc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, pos, 0); |
efe66bbc VZ |
1462 | |
1463 | if ( lRc == -1 ) | |
1464 | { | |
1465 | // this is apparently returned when pos corresponds to the last | |
1466 | // position | |
1467 | ptReal.x = | |
1468 | ptReal.y = 0; | |
1469 | } | |
1470 | else | |
1471 | { | |
1472 | ptReal.x = LOWORD(lRc); | |
1473 | ptReal.y = HIWORD(lRc); | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | wxTextCtrlHitTestResult rc; | |
1478 | ||
1479 | if ( pt.y > ptReal.y + GetCharHeight() ) | |
1480 | rc = wxTE_HT_BELOW; | |
1481 | else if ( pt.x > ptReal.x + GetCharWidth() ) | |
1482 | rc = wxTE_HT_BEYOND; | |
1483 | else | |
1484 | rc = wxTE_HT_ON_TEXT; | |
1485 | ||
6726a6b0 VZ |
1486 | if ( posOut ) |
1487 | *posOut = pos; | |
efe66bbc VZ |
1488 | |
1489 | return rc; | |
1490 | } | |
1491 | ||
1492 | // ---------------------------------------------------------------------------- | |
bfbb0b4c | 1493 | // |
efe66bbc VZ |
1494 | // ---------------------------------------------------------------------------- |
1495 | ||
debe6624 | 1496 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 1497 | { |
789295bf | 1498 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1499 | |
1500 | // To scroll to a position, we pass the number of lines and characters | |
1501 | // to scroll *by*. This means that we need to: | |
1502 | // (1) Find the line position of the current line. | |
1503 | // (2) Find the line position of pos. | |
1504 | // (3) Scroll by (pos - current). | |
1505 | // For now, ignore the horizontal scrolling. | |
1506 | ||
1507 | // Is this where scrolling is relative to - the line containing the caret? | |
1508 | // Or is the first visible line??? Try first visible line. | |
fa2f57be | 1509 | // int currentLineLineNo1 = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, -1, 0L); |
2bda0e17 | 1510 | |
fa2f57be | 1511 | int currentLineLineNo = (int)::SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0); |
2bda0e17 | 1512 | |
fa2f57be | 1513 | int specifiedLineLineNo = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0); |
39136494 | 1514 | |
2bda0e17 KB |
1515 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
1516 | ||
2bda0e17 | 1517 | if (linesToScroll != 0) |
fa2f57be | 1518 | ::SendMessage(hWnd, EM_LINESCROLL, 0, linesToScroll); |
2bda0e17 KB |
1519 | } |
1520 | ||
a5aa8086 VZ |
1521 | long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const |
1522 | { | |
fa2f57be | 1523 | return ::SendMessage(GetHwnd(), EM_LINELENGTH, pos, 0); |
a5aa8086 VZ |
1524 | } |
1525 | ||
debe6624 | 1526 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 | 1527 | { |
a5aa8086 VZ |
1528 | long pos = XYToPosition(0, lineNo); |
1529 | ||
1530 | return GetLengthOfLineContainingPos(pos); | |
2bda0e17 KB |
1531 | } |
1532 | ||
debe6624 | 1533 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 1534 | { |
a1b82138 | 1535 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
488fe1fe | 1536 | |
f6bcfd97 BP |
1537 | // there must be at least enough place for the length WORD in the |
1538 | // buffer | |
1539 | len += sizeof(WORD); | |
4438caf4 | 1540 | |
f6bcfd97 | 1541 | wxString str; |
de564874 MB |
1542 | { |
1543 | wxStringBufferLength tmp(str, len); | |
1544 | wxChar *buf = tmp; | |
1545 | ||
1546 | *(WORD *)buf = (WORD)len; | |
60ab0c52 VZ |
1547 | len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); |
1548 | ||
1549 | #if wxUSE_RICHEDIT | |
1550 | if ( IsRich() ) | |
1551 | { | |
1552 | // remove the '\r' returned by the rich edit control, the user code | |
1553 | // should never see it | |
9a83f860 | 1554 | if ( buf[len - 2] == wxT('\r') && buf[len - 1] == wxT('\n') ) |
60ab0c52 | 1555 | { |
e1ef6b5d VZ |
1556 | // richedit 1.0 uses "\r\n" as line terminator, so remove "\r" |
1557 | // here and "\n" below | |
9a83f860 | 1558 | buf[len - 2] = wxT('\n'); |
60ab0c52 VZ |
1559 | len--; |
1560 | } | |
9a83f860 | 1561 | else if ( buf[len - 1] == wxT('\r') ) |
e1ef6b5d VZ |
1562 | { |
1563 | // richedit 2.0+ uses only "\r", replace it with "\n" | |
9a83f860 | 1564 | buf[len - 1] = wxT('\n'); |
e1ef6b5d | 1565 | } |
60ab0c52 VZ |
1566 | } |
1567 | #endif // wxUSE_RICHEDIT | |
1568 | ||
8f387d13 VZ |
1569 | // remove the '\n' at the end, if any (this is how this function is |
1570 | // supposed to work according to the docs) | |
9a83f860 | 1571 | if ( buf[len - 1] == wxT('\n') ) |
8f387d13 VZ |
1572 | { |
1573 | len--; | |
1574 | } | |
1575 | ||
de564874 MB |
1576 | buf[len] = 0; |
1577 | tmp.SetLength(len); | |
1578 | } | |
4438caf4 VZ |
1579 | |
1580 | return str; | |
2bda0e17 KB |
1581 | } |
1582 | ||
d7eee191 VZ |
1583 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1584 | { | |
4a82116e | 1585 | #if wxUSE_RICHEDIT |
4fa80851 VZ |
1586 | if ( IsRich() ) |
1587 | { | |
1588 | ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, len ? len : 0x7fffffff); | |
1589 | } | |
4a82116e | 1590 | else |
4fa80851 VZ |
1591 | #endif // wxUSE_RICHEDIT |
1592 | { | |
fa2f57be | 1593 | wxTextEntry::SetMaxLength(len); |
4fa80851 | 1594 | } |
d7eee191 VZ |
1595 | } |
1596 | ||
a1b82138 | 1597 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 1598 | // Undo/redo |
a1b82138 VZ |
1599 | // ---------------------------------------------------------------------------- |
1600 | ||
ca8b28f2 JS |
1601 | void wxTextCtrl::Redo() |
1602 | { | |
4a82116e | 1603 | #if wxUSE_RICHEDIT |
fa2f57be VZ |
1604 | if ( GetRichVersion() > 1 ) |
1605 | { | |
1606 | ::SendMessage(GetHwnd(), EM_REDO, 0, 0); | |
1607 | return; | |
ca8b28f2 | 1608 | } |
fa2f57be | 1609 | #endif // wxUSE_RICHEDIT |
ca8b28f2 | 1610 | |
fa2f57be | 1611 | wxTextEntry::Redo(); |
ca8b28f2 JS |
1612 | } |
1613 | ||
1614 | bool wxTextCtrl::CanRedo() const | |
1615 | { | |
4a82116e | 1616 | #if wxUSE_RICHEDIT |
fa2f57be | 1617 | if ( GetRichVersion() > 1 ) |
4a82116e | 1618 | return ::SendMessage(GetHwnd(), EM_CANREDO, 0, 0) != 0; |
fa2f57be VZ |
1619 | #endif // wxUSE_RICHEDIT |
1620 | ||
1621 | return wxTextEntry::CanRedo(); | |
ca8b28f2 JS |
1622 | } |
1623 | ||
e3a6a6b2 VZ |
1624 | // ---------------------------------------------------------------------------- |
1625 | // caret handling (Windows only) | |
1626 | // ---------------------------------------------------------------------------- | |
1627 | ||
1628 | bool wxTextCtrl::ShowNativeCaret(bool show) | |
1629 | { | |
1630 | if ( show != m_isNativeCaretShown ) | |
1631 | { | |
1632 | if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) ) | |
1633 | { | |
1634 | // not an error, may simply indicate that it's not shown/hidden | |
fa2f57be | 1635 | // yet (i.e. it had been hidden/shown 2 times before) |
e3a6a6b2 VZ |
1636 | return false; |
1637 | } | |
1638 | ||
1639 | m_isNativeCaretShown = show; | |
1640 | } | |
1641 | ||
1642 | return true; | |
1643 | } | |
1644 | ||
a1b82138 | 1645 | // ---------------------------------------------------------------------------- |
fa2f57be | 1646 | // implementation details |
a1b82138 | 1647 | // ---------------------------------------------------------------------------- |
39136494 | 1648 | |
2bda0e17 KB |
1649 | void wxTextCtrl::Command(wxCommandEvent & event) |
1650 | { | |
a1b82138 VZ |
1651 | SetValue(event.GetString()); |
1652 | ProcessCommand (event); | |
2bda0e17 KB |
1653 | } |
1654 | ||
1655 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
1656 | { | |
a1b82138 VZ |
1657 | // By default, load the first file into the text window. |
1658 | if (event.GetNumberOfFiles() > 0) | |
1659 | { | |
1660 | LoadFile(event.GetFiles()[0]); | |
1661 | } | |
2bda0e17 KB |
1662 | } |
1663 | ||
a37d422a VZ |
1664 | // ---------------------------------------------------------------------------- |
1665 | // kbd input processing | |
1666 | // ---------------------------------------------------------------------------- | |
1667 | ||
90c6edd7 | 1668 | bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* msg) |
a37d422a | 1669 | { |
a37d422a VZ |
1670 | // check for our special keys here: if we don't do it and the parent frame |
1671 | // uses them as accelerators, they wouldn't work at all, so we disable | |
1672 | // usual preprocessing for them | |
1673 | if ( msg->message == WM_KEYDOWN ) | |
1674 | { | |
90c6edd7 VZ |
1675 | const WPARAM vkey = msg->wParam; |
1676 | if ( HIWORD(msg->lParam) & KF_ALTDOWN ) | |
a37d422a | 1677 | { |
90c6edd7 | 1678 | // Alt-Backspace is accelerator for "Undo" |
a37d422a | 1679 | if ( vkey == VK_BACK ) |
bfbb0b4c | 1680 | return false; |
a37d422a VZ |
1681 | } |
1682 | else // no Alt | |
1683 | { | |
cf6e951c VZ |
1684 | // we want to process some Ctrl-foo and Shift-bar but no key |
1685 | // combinations without either Ctrl or Shift nor with both of them | |
1686 | // pressed | |
1687 | const int ctrl = wxIsCtrlDown(), | |
1688 | shift = wxIsShiftDown(); | |
1689 | switch ( ctrl + shift ) | |
a37d422a | 1690 | { |
cf6e951c | 1691 | default: |
9a83f860 | 1692 | wxFAIL_MSG( wxT("how many modifiers have we got?") ); |
cf6e951c VZ |
1693 | // fall through |
1694 | ||
1695 | case 0: | |
eedf954b | 1696 | if ( IsMultiLine() && vkey == VK_RETURN ) |
90c6edd7 VZ |
1697 | return false; |
1698 | // fall through | |
cf6e951c VZ |
1699 | case 2: |
1700 | break; | |
1701 | ||
1702 | case 1: | |
1703 | // either Ctrl or Shift pressed | |
1704 | if ( ctrl ) | |
1705 | { | |
1706 | switch ( vkey ) | |
1707 | { | |
1708 | case 'C': | |
1709 | case 'V': | |
1710 | case 'X': | |
1711 | case VK_INSERT: | |
1712 | case VK_DELETE: | |
1713 | case VK_HOME: | |
1714 | case VK_END: | |
bfbb0b4c | 1715 | return false; |
cf6e951c VZ |
1716 | } |
1717 | } | |
1718 | else // Shift is pressed | |
1719 | { | |
1720 | if ( vkey == VK_INSERT || vkey == VK_DELETE ) | |
bfbb0b4c | 1721 | return false; |
cf6e951c | 1722 | } |
a37d422a VZ |
1723 | } |
1724 | } | |
1725 | } | |
1726 | ||
90c6edd7 | 1727 | return wxControl::MSWShouldPreProcessMessage(msg); |
a37d422a VZ |
1728 | } |
1729 | ||
2bda0e17 KB |
1730 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
1731 | { | |
77e00fe9 | 1732 | switch ( event.GetKeyCode() ) |
cd471848 | 1733 | { |
cd471848 | 1734 | case WXK_RETURN: |
cd471848 VZ |
1735 | { |
1736 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
bfbd6dc1 | 1737 | InitCommandEvent(event); |
f6bcfd97 | 1738 | event.SetString(GetValue()); |
937013e0 | 1739 | if ( HandleWindowEvent(event) ) |
9a3952fa | 1740 | if ( !HasFlag(wxTE_MULTILINE) ) |
cd471848 | 1741 | return; |
9a3952fa | 1742 | //else: multiline controls need Enter for themselves |
cd471848 | 1743 | } |
5fb9fcfc | 1744 | break; |
4d91c1d1 | 1745 | |
cd471848 | 1746 | case WXK_TAB: |
818d407a VZ |
1747 | // ok, so this is getting absolutely ridiculous but I don't see |
1748 | // any other way to fix this bug: when a multiline text control is | |
1749 | // inside a wxFrame, we need to generate the navigation event as | |
1750 | // otherwise nothing happens at all, but when the same control is | |
1751 | // created inside a dialog, IsDialogMessage() *does* switch focus | |
1752 | // all by itself and so if we do it here as well, it is advanced | |
1753 | // twice and goes to the next control... to prevent this from | |
1754 | // happening we're doing this ugly check, the logic being that if | |
1755 | // we don't have focus then it had been already changed to the next | |
1756 | // control | |
1757 | // | |
1758 | // the right thing to do would, of course, be to understand what | |
1759 | // the hell is IsDialogMessage() doing but this is beyond my feeble | |
1760 | // forces at the moment unfortunately | |
5f6cfda7 | 1761 | if ( !(m_windowStyle & wxTE_PROCESS_TAB)) |
cd471848 | 1762 | { |
5f6cfda7 JS |
1763 | if ( FindFocus() == this ) |
1764 | { | |
eedc82f4 JS |
1765 | int flags = 0; |
1766 | if (!event.ShiftDown()) | |
1767 | flags |= wxNavigationKeyEvent::IsForward ; | |
1768 | if (event.ControlDown()) | |
1769 | flags |= wxNavigationKeyEvent::WinChange ; | |
1770 | if (Navigate(flags)) | |
5f6cfda7 JS |
1771 | return; |
1772 | } | |
1773 | } | |
1774 | else | |
1775 | { | |
1776 | // Insert tab since calling the default Windows handler | |
1777 | // doesn't seem to do it | |
1778 | WriteText(wxT("\t")); | |
d1fd98cc | 1779 | return; |
cd471848 | 1780 | } |
341c92a8 | 1781 | break; |
cd471848 | 1782 | } |
39136494 | 1783 | |
8614c467 | 1784 | // no, we didn't process it |
42e69d6b | 1785 | event.Skip(); |
2bda0e17 KB |
1786 | } |
1787 | ||
e2cf30aa VS |
1788 | void wxTextCtrl::OnKeyDown(wxKeyEvent& event) |
1789 | { | |
1790 | // richedit control doesn't send WM_PASTE, WM_CUT and WM_COPY messages | |
1791 | // when Ctrl-V, X or C is pressed and this prevents wxClipboardTextEvent | |
1792 | // from working. So we work around it by intercepting these shortcuts | |
1793 | // ourselves and emitting clipboard events (which richedit will handle, | |
1794 | // so everything works as before, including pasting of rich text): | |
1795 | if ( event.GetModifiers() == wxMOD_CONTROL && IsRich() ) | |
1796 | { | |
1797 | switch ( event.GetKeyCode() ) | |
1798 | { | |
1799 | case 'C': | |
1800 | Copy(); | |
1801 | return; | |
1802 | case 'X': | |
1803 | Cut(); | |
1804 | return; | |
1805 | case 'V': | |
1806 | Paste(); | |
1807 | return; | |
1808 | default: | |
1809 | break; | |
1810 | } | |
1811 | } | |
1812 | ||
d19d14c5 VZ |
1813 | // Default window procedure of multiline edit controls posts WM_CLOSE to |
1814 | // the parent window when it gets Escape key press for some reason, prevent | |
1815 | // it from doing this as this resulted in dialog boxes being closed on | |
1816 | // Escape even when they shouldn't be (we do handle Escape ourselves | |
1817 | // correctly in the situations when it should close them). | |
1818 | if ( event.GetKeyCode() == WXK_ESCAPE && IsMultiLine() ) | |
1819 | return; | |
1820 | ||
e2cf30aa VS |
1821 | // no, we didn't process it |
1822 | event.Skip(); | |
1823 | } | |
1824 | ||
c140b7e7 | 1825 | WXLRESULT wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
0cf5b099 | 1826 | { |
c140b7e7 | 1827 | WXLRESULT lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
e7e91e03 | 1828 | |
9fcbe076 | 1829 | switch ( nMsg ) |
0cf5b099 | 1830 | { |
9fcbe076 VZ |
1831 | case WM_GETDLGCODE: |
1832 | { | |
1833 | // we always want the chars and the arrows: the arrows for | |
1834 | // navigation and the chars because we want Ctrl-C to work even | |
1835 | // in a read only control | |
1836 | long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
2b5f62a0 | 1837 | |
9fcbe076 VZ |
1838 | if ( IsEditable() ) |
1839 | { | |
1840 | // we may have several different cases: | |
1841 | // 1. normal: both TAB and ENTER are used for navigation | |
1842 | // 2. ctrl wants TAB for itself: ENTER is used to pass to | |
1843 | // the next control in the dialog | |
1844 | // 3. ctrl wants ENTER for itself: TAB is used for dialog | |
1845 | // navigation | |
1846 | // 4. ctrl wants both TAB and ENTER: Ctrl-ENTER is used to | |
1847 | // go to the next control (we need some way to do it) | |
1848 | ||
1849 | // multiline controls should always get ENTER for themselves | |
1850 | if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) ) | |
1851 | lDlgCode |= DLGC_WANTMESSAGE; | |
1852 | ||
1853 | if ( HasFlag(wxTE_PROCESS_TAB) ) | |
1854 | lDlgCode |= DLGC_WANTTAB; | |
1855 | ||
1856 | lRc |= lDlgCode; | |
1857 | } | |
1858 | else // !editable | |
1859 | { | |
1860 | // NB: use "=", not "|=" as the base class version returns | |
4c81144c | 1861 | // the same flags in the disabled state as usual (i.e. |
9fcbe076 VZ |
1862 | // including DLGC_WANTMESSAGE). This is strange (how |
1863 | // does it work in the native Win32 apps?) but for now | |
1864 | // live with it. | |
1865 | lRc = lDlgCode; | |
1866 | } | |
1867 | } | |
1868 | break; | |
7b6126aa VZ |
1869 | |
1870 | #if wxUSE_MENUS | |
1871 | case WM_SETCURSOR: | |
1872 | // rich text controls seem to have a bug and don't change the | |
1873 | // cursor to the standard arrow one from the I-beam cursor usually | |
1874 | // used by them even when a popup menu is shown (this works fine | |
1875 | // for plain EDIT controls though), so explicitly work around this | |
1876 | if ( IsRich() ) | |
1877 | { | |
1878 | extern wxMenu *wxCurrentPopupMenu; | |
1879 | if ( wxCurrentPopupMenu && | |
1880 | wxCurrentPopupMenu->GetInvokingWindow() == this ) | |
1881 | ::SetCursor(GetHcursorOf(*wxSTANDARD_CURSOR)); | |
1882 | } | |
1883 | #endif // wxUSE_MENUS | |
0cf5b099 VZ |
1884 | } |
1885 | ||
e7e91e03 | 1886 | return lRc; |
129223d6 VZ |
1887 | } |
1888 | ||
1889 | // ---------------------------------------------------------------------------- | |
1890 | // text control event processing | |
1891 | // ---------------------------------------------------------------------------- | |
1892 | ||
5036ea90 VZ |
1893 | bool wxTextCtrl::SendUpdateEvent() |
1894 | { | |
2c62dd25 | 1895 | switch ( m_updatesCount ) |
5036ea90 | 1896 | { |
2c62dd25 VZ |
1897 | case 0: |
1898 | // remember that we've got an update | |
1899 | m_updatesCount++; | |
1900 | break; | |
5036ea90 | 1901 | |
2c62dd25 VZ |
1902 | case 1: |
1903 | // we had already sent one event since the last control modification | |
1904 | return false; | |
1905 | ||
1906 | default: | |
9a83f860 | 1907 | wxFAIL_MSG( wxT("unexpected wxTextCtrl::m_updatesCount value") ); |
2c62dd25 VZ |
1908 | // fall through |
1909 | ||
1910 | case -1: | |
1911 | // we hadn't updated the control ourselves, this event comes from | |
1912 | // the user, don't need to ignore it nor update the count | |
1913 | break; | |
f6519b40 VZ |
1914 | |
1915 | case -2: | |
1916 | // the control was updated programmatically and we do NOT want to | |
1917 | // send events | |
1918 | return false; | |
5036ea90 VZ |
1919 | } |
1920 | ||
fa2f57be | 1921 | return SendTextUpdatedEvent(); |
5036ea90 VZ |
1922 | } |
1923 | ||
debe6624 | 1924 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 1925 | { |
5036ea90 | 1926 | switch ( param ) |
789295bf | 1927 | { |
789295bf | 1928 | case EN_CHANGE: |
5036ea90 | 1929 | SendUpdateEvent(); |
789295bf | 1930 | break; |
2bda0e17 | 1931 | |
b12915c1 | 1932 | case EN_MAXTEXT: |
5036ea90 | 1933 | // the text size limit has been hit -- try to increase it |
d7eee191 VZ |
1934 | if ( !AdjustSpaceLimit() ) |
1935 | { | |
1936 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId); | |
1937 | InitCommandEvent(event); | |
1938 | event.SetString(GetValue()); | |
1939 | ProcessCommand(event); | |
1940 | } | |
789295bf VZ |
1941 | break; |
1942 | ||
4eb77d8b VZ |
1943 | // the other edit notification messages are not processed (or, in |
1944 | // the case of EN_{SET/KILL}FOCUS were already handled at WM_SET/ | |
1945 | // KILLFOCUS level) | |
789295bf | 1946 | default: |
bfbb0b4c | 1947 | return false; |
789295bf VZ |
1948 | } |
1949 | ||
1950 | // processed | |
bfbb0b4c | 1951 | return true; |
2bda0e17 KB |
1952 | } |
1953 | ||
2bae4332 | 1954 | WXHBRUSH wxTextCtrl::MSWControlColor(WXHDC hDC, WXHWND hWnd) |
f6bcfd97 | 1955 | { |
48fa6bd3 VZ |
1956 | if ( !IsEnabled() && !HasFlag(wxTE_MULTILINE) ) |
1957 | return MSWControlColorDisabled(hDC); | |
f6bcfd97 | 1958 | |
2bae4332 | 1959 | return wxTextCtrlBase::MSWControlColor(hDC, hWnd); |
f6bcfd97 BP |
1960 | } |
1961 | ||
4fa80851 | 1962 | bool wxTextCtrl::HasSpaceLimit(unsigned int *len) const |
789295bf | 1963 | { |
d7eee191 VZ |
1964 | // HACK: we try to automatically extend the limit for the amount of text |
1965 | // to allow (interactively) entering more than 64Kb of text under | |
1966 | // Win9x but we shouldn't reset the text limit which was previously | |
1967 | // set explicitly with SetMaxLength() | |
1968 | // | |
4fa80851 VZ |
1969 | // Unfortunately there is no EM_GETLIMITTEXTSETBYUSER and so we don't |
1970 | // know the limit we set (if any). We could solve this by storing the | |
1971 | // limit we set in wxTextCtrl but to save space we prefer to simply | |
1972 | // test here the actual limit value: we consider that SetMaxLength() | |
1973 | // can only be called for small values while EN_MAXTEXT is only sent | |
1974 | // for large values (in practice the default limit seems to be 30000 | |
1975 | // but make it smaller just to be on the safe side) | |
1976 | *len = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); | |
1977 | return *len < 10001; | |
1978 | ||
1979 | } | |
1980 | ||
1981 | bool wxTextCtrl::AdjustSpaceLimit() | |
1982 | { | |
1983 | unsigned int limit; | |
1984 | if ( HasSpaceLimit(&limit) ) | |
bfbb0b4c | 1985 | return false; |
d7eee191 VZ |
1986 | |
1987 | unsigned int len = ::GetWindowTextLength(GetHwnd()); | |
17d8ee1c | 1988 | if ( len >= limit ) |
789295bf | 1989 | { |
4fa80851 VZ |
1990 | // increment in 32Kb chunks |
1991 | SetMaxLength(len + 0x8000); | |
789295bf | 1992 | } |
d7eee191 VZ |
1993 | |
1994 | // we changed the limit | |
bfbb0b4c | 1995 | return true; |
789295bf | 1996 | } |
2bda0e17 | 1997 | |
fa2f57be | 1998 | bool wxTextCtrl::AcceptsFocusFromKeyboard() const |
2bda0e17 | 1999 | { |
589c7163 VZ |
2000 | // we don't want focus if we can't be edited unless we're a multiline |
2001 | // control because then it might be still nice to get focus from keyboard | |
2002 | // to be able to scroll it without mouse | |
2003 | return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus(); | |
a1b82138 | 2004 | } |
c085e333 | 2005 | |
f68586e5 | 2006 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
2007 | { |
2008 | int cx, cy; | |
7a5e53ab | 2009 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
a1b82138 VZ |
2010 | |
2011 | int wText = DEFAULT_ITEM_WIDTH; | |
2012 | ||
f60e797e | 2013 | int hText = cy; |
a1b82138 VZ |
2014 | if ( m_windowStyle & wxTE_MULTILINE ) |
2015 | { | |
b7a3ba7d | 2016 | hText *= wxMax(wxMin(GetNumberOfLines(), 10), 2); |
a1b82138 VZ |
2017 | } |
2018 | //else: for single line control everything is ok | |
2019 | ||
f60e797e VZ |
2020 | // we have to add the adjustments for the control height only once, not |
2021 | // once per line, so do it after multiplication above | |
2022 | hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy; | |
2023 | ||
a1b82138 | 2024 | return wxSize(wText, hText); |
2bda0e17 | 2025 | } |
a1b82138 VZ |
2026 | |
2027 | // ---------------------------------------------------------------------------- | |
2028 | // standard handlers for standard edit menu events | |
2029 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 2030 | |
bfbd6dc1 | 2031 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
2032 | { |
2033 | Cut(); | |
2034 | } | |
2035 | ||
bfbd6dc1 | 2036 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
2037 | { |
2038 | Copy(); | |
2039 | } | |
2040 | ||
bfbd6dc1 | 2041 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
2042 | { |
2043 | Paste(); | |
2044 | } | |
2045 | ||
bfbd6dc1 | 2046 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
2047 | { |
2048 | Undo(); | |
2049 | } | |
2050 | ||
bfbd6dc1 | 2051 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
2052 | { |
2053 | Redo(); | |
2054 | } | |
2055 | ||
2eb10e2a | 2056 | void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event)) |
2b5f62a0 | 2057 | { |
5a25f858 | 2058 | RemoveSelection(); |
2b5f62a0 VZ |
2059 | } |
2060 | ||
2eb10e2a | 2061 | void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) |
2b5f62a0 | 2062 | { |
c2208899 | 2063 | SelectAll(); |
2b5f62a0 VZ |
2064 | } |
2065 | ||
e702ff0f JS |
2066 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) |
2067 | { | |
2068 | event.Enable( CanCut() ); | |
2069 | } | |
2070 | ||
2071 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2072 | { | |
2073 | event.Enable( CanCopy() ); | |
2074 | } | |
2075 | ||
2076 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2077 | { | |
2078 | event.Enable( CanPaste() ); | |
2079 | } | |
2080 | ||
2081 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2082 | { | |
2083 | event.Enable( CanUndo() ); | |
2084 | } | |
2085 | ||
2086 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2087 | { | |
2088 | event.Enable( CanRedo() ); | |
2089 | } | |
2090 | ||
2b5f62a0 VZ |
2091 | void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event) |
2092 | { | |
c2208899 | 2093 | event.Enable( HasSelection() && IsEditable() ); |
2b5f62a0 VZ |
2094 | } |
2095 | ||
2096 | void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
2097 | { | |
c2208899 | 2098 | event.Enable( !IsEmpty() ); |
2b5f62a0 VZ |
2099 | } |
2100 | ||
26f60eb6 | 2101 | void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event) |
2b5f62a0 VZ |
2102 | { |
2103 | #if wxUSE_RICHEDIT | |
2104 | if (IsRich()) | |
2105 | { | |
2106 | if (!m_privateContextMenu) | |
2107 | { | |
2108 | m_privateContextMenu = new wxMenu; | |
2109 | m_privateContextMenu->Append(wxID_UNDO, _("&Undo")); | |
2110 | m_privateContextMenu->Append(wxID_REDO, _("&Redo")); | |
2111 | m_privateContextMenu->AppendSeparator(); | |
2112 | m_privateContextMenu->Append(wxID_CUT, _("Cu&t")); | |
2113 | m_privateContextMenu->Append(wxID_COPY, _("&Copy")); | |
2114 | m_privateContextMenu->Append(wxID_PASTE, _("&Paste")); | |
2115 | m_privateContextMenu->Append(wxID_CLEAR, _("&Delete")); | |
2116 | m_privateContextMenu->AppendSeparator(); | |
2117 | m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
2118 | } | |
26f60eb6 | 2119 | PopupMenu(m_privateContextMenu); |
2b5f62a0 VZ |
2120 | return; |
2121 | } | |
2122 | else | |
2123 | #endif | |
2124 | event.Skip(); | |
2125 | } | |
2126 | ||
50c6e05f | 2127 | void wxTextCtrl::OnSetFocus(wxFocusEvent& event) |
e3a6a6b2 VZ |
2128 | { |
2129 | // be sure the caret remains invisible if the user had hidden it | |
2130 | if ( !m_isNativeCaretShown ) | |
2131 | { | |
2132 | ::HideCaret(GetHwnd()); | |
2133 | } | |
50c6e05f VZ |
2134 | |
2135 | event.Skip(); | |
e3a6a6b2 VZ |
2136 | } |
2137 | ||
4bc1afd5 VZ |
2138 | // the rest of the file only deals with the rich edit controls |
2139 | #if wxUSE_RICHEDIT | |
2140 | ||
c57e3339 VZ |
2141 | // ---------------------------------------------------------------------------- |
2142 | // EN_LINK processing | |
2143 | // ---------------------------------------------------------------------------- | |
2144 | ||
a64c3b74 | 2145 | bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
c57e3339 VZ |
2146 | { |
2147 | NMHDR *hdr = (NMHDR* )lParam; | |
1dae1d00 | 2148 | switch ( hdr->code ) |
c57e3339 | 2149 | { |
3bce6687 | 2150 | case EN_MSGFILTER: |
1dae1d00 VZ |
2151 | { |
2152 | const MSGFILTER *msgf = (MSGFILTER *)lParam; | |
2153 | UINT msg = msgf->msg; | |
2154 | ||
2155 | // this is a bit crazy but richedit 1.0 sends us all mouse | |
2156 | // events _except_ WM_LBUTTONUP (don't ask me why) so we have | |
2157 | // generate the wxWin events for this message manually | |
2158 | // | |
2159 | // NB: in fact, this is still not totally correct as it does | |
2160 | // send us WM_LBUTTONUP if the selection was cleared by the | |
2161 | // last click -- so currently we get 2 events in this case, | |
2162 | // but as I don't see any obvious way to check for this I | |
2163 | // leave this code in place because it's still better than | |
2164 | // not getting left up events at all | |
2165 | if ( msg == WM_LBUTTONUP ) | |
c57e3339 | 2166 | { |
1dae1d00 VZ |
2167 | WXUINT flags = msgf->wParam; |
2168 | int x = GET_X_LPARAM(msgf->lParam), | |
2169 | y = GET_Y_LPARAM(msgf->lParam); | |
2170 | ||
2171 | HandleMouseEvent(msg, x, y, flags); | |
c57e3339 | 2172 | } |
1dae1d00 | 2173 | } |
c57e3339 | 2174 | |
bfbb0b4c WS |
2175 | // return true to process the event (and false to ignore it) |
2176 | return true; | |
1dae1d00 VZ |
2177 | |
2178 | case EN_LINK: | |
2179 | { | |
2180 | const ENLINK *enlink = (ENLINK *)hdr; | |
2181 | ||
2182 | switch ( enlink->msg ) | |
2183 | { | |
2184 | case WM_SETCURSOR: | |
2185 | // ok, so it is hardcoded - do we really nee to | |
2186 | // customize it? | |
5c519b6c WS |
2187 | { |
2188 | wxCursor cur(wxCURSOR_HAND); | |
2189 | ::SetCursor(GetHcursorOf(cur)); | |
2190 | *result = TRUE; | |
2191 | break; | |
2192 | } | |
1dae1d00 VZ |
2193 | |
2194 | case WM_MOUSEMOVE: | |
2195 | case WM_LBUTTONDOWN: | |
2196 | case WM_LBUTTONUP: | |
2197 | case WM_LBUTTONDBLCLK: | |
2198 | case WM_RBUTTONDOWN: | |
2199 | case WM_RBUTTONUP: | |
2200 | case WM_RBUTTONDBLCLK: | |
2201 | // send a mouse event | |
2202 | { | |
2203 | static const wxEventType eventsMouse[] = | |
2204 | { | |
2205 | wxEVT_MOTION, | |
2206 | wxEVT_LEFT_DOWN, | |
2207 | wxEVT_LEFT_UP, | |
2208 | wxEVT_LEFT_DCLICK, | |
2209 | wxEVT_RIGHT_DOWN, | |
2210 | wxEVT_RIGHT_UP, | |
2211 | wxEVT_RIGHT_DCLICK, | |
2212 | }; | |
2213 | ||
2214 | // the event ids are consecutive | |
2215 | wxMouseEvent | |
2216 | evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]); | |
2217 | ||
2218 | InitMouseEvent(evtMouse, | |
2219 | GET_X_LPARAM(enlink->lParam), | |
2220 | GET_Y_LPARAM(enlink->lParam), | |
2221 | enlink->wParam); | |
2222 | ||
2223 | wxTextUrlEvent event(m_windowId, evtMouse, | |
2224 | enlink->chrg.cpMin, | |
2225 | enlink->chrg.cpMax); | |
2226 | ||
2227 | InitCommandEvent(event); | |
2228 | ||
2229 | *result = ProcessCommand(event); | |
2230 | } | |
2231 | break; | |
2232 | } | |
2233 | } | |
bfbb0b4c | 2234 | return true; |
c57e3339 | 2235 | } |
7f5d8b00 | 2236 | |
d86ab8e2 VZ |
2237 | // not processed, leave it to the base class |
2238 | return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
c57e3339 VZ |
2239 | } |
2240 | ||
20a0e999 VZ |
2241 | #if wxUSE_DRAG_AND_DROP |
2242 | ||
2243 | void wxTextCtrl::SetDropTarget(wxDropTarget *dropTarget) | |
2244 | { | |
2245 | if ( m_dropTarget == wxRICHTEXT_DEFAULT_DROPTARGET ) | |
2246 | { | |
2247 | // get rid of the built-in drop target | |
2248 | ::RevokeDragDrop(GetHwnd()); | |
2249 | m_dropTarget = NULL; | |
2250 | } | |
2251 | ||
2252 | wxTextCtrlBase::SetDropTarget(dropTarget); | |
2253 | } | |
2254 | ||
2255 | #endif // wxUSE_DRAG_AND_DROP | |
2256 | ||
52f2f7b2 | 2257 | // ---------------------------------------------------------------------------- |
f6bcfd97 BP |
2258 | // colour setting for the rich edit controls |
2259 | // ---------------------------------------------------------------------------- | |
2260 | ||
f6bcfd97 BP |
2261 | bool wxTextCtrl::SetBackgroundColour(const wxColour& colour) |
2262 | { | |
2263 | if ( !wxTextCtrlBase::SetBackgroundColour(colour) ) | |
2264 | { | |
2265 | // colour didn't really change | |
bfbb0b4c | 2266 | return false; |
f6bcfd97 BP |
2267 | } |
2268 | ||
2269 | if ( IsRich() ) | |
2270 | { | |
2271 | // rich edit doesn't use WM_CTLCOLOR, hence we need to send | |
2272 | // EM_SETBKGNDCOLOR additionally | |
2273 | ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour)); | |
2274 | } | |
2275 | ||
bfbb0b4c | 2276 | return true; |
f6bcfd97 BP |
2277 | } |
2278 | ||
2279 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
2280 | { | |
2281 | if ( !wxTextCtrlBase::SetForegroundColour(colour) ) | |
2282 | { | |
2283 | // colour didn't really change | |
bfbb0b4c | 2284 | return false; |
f6bcfd97 BP |
2285 | } |
2286 | ||
2287 | if ( IsRich() ) | |
2288 | { | |
2289 | // change the colour of everything | |
2290 | CHARFORMAT cf; | |
2291 | wxZeroMemory(cf); | |
2292 | cf.cbSize = sizeof(cf); | |
2293 | cf.dwMask = CFM_COLOR; | |
2294 | cf.crTextColor = wxColourToRGB(colour); | |
2295 | ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); | |
2296 | } | |
2297 | ||
bfbb0b4c | 2298 | return true; |
f6bcfd97 BP |
2299 | } |
2300 | ||
81fb185e VZ |
2301 | bool wxTextCtrl::SetFont(const wxFont& font) |
2302 | { | |
2303 | if ( !wxTextCtrlBase::SetFont(font) ) | |
2304 | return false; | |
2305 | ||
179c657b | 2306 | if ( GetRichVersion() >= 4 ) |
81fb185e | 2307 | { |
179c657b VZ |
2308 | // Using WM_SETFONT is not enough with RichEdit 4.1: it does work but |
2309 | // for ASCII characters only and inserting a non-ASCII one into it | |
2310 | // later reverts to the default font so use EM_SETCHARFORMAT to change | |
2311 | // the default font for it. | |
81fb185e VZ |
2312 | wxTextAttr attr; |
2313 | attr.SetFont(font); | |
179c657b | 2314 | SetStyle(-1, -1, attr); |
81fb185e VZ |
2315 | } |
2316 | ||
2317 | return true; | |
2318 | } | |
2319 | ||
4bc1afd5 VZ |
2320 | // ---------------------------------------------------------------------------- |
2321 | // styling support for rich edit controls | |
2322 | // ---------------------------------------------------------------------------- | |
2323 | ||
a8abba41 | 2324 | bool wxTextCtrl::MSWSetCharFormat(const wxTextAttr& style, long start, long end) |
4bc1afd5 | 2325 | { |
4bc1afd5 | 2326 | // initialize CHARFORMAT struct |
be329a3d RD |
2327 | #if wxUSE_RICHEDIT2 |
2328 | CHARFORMAT2 cf; | |
2329 | #else | |
4bc1afd5 | 2330 | CHARFORMAT cf; |
be329a3d | 2331 | #endif |
a5aa8086 | 2332 | |
4bc1afd5 | 2333 | wxZeroMemory(cf); |
a5aa8086 VZ |
2334 | |
2335 | // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple | |
2336 | // CHARFORMAT in that case | |
2337 | #if wxUSE_RICHEDIT2 | |
2338 | if ( m_verRichEdit == 1 ) | |
2339 | { | |
2340 | // this is the only thing the control is going to grok | |
2341 | cf.cbSize = sizeof(CHARFORMAT); | |
2342 | } | |
2343 | else | |
2344 | #endif | |
2345 | { | |
2346 | // CHARFORMAT or CHARFORMAT2 | |
2347 | cf.cbSize = sizeof(cf); | |
2348 | } | |
4bc1afd5 VZ |
2349 | |
2350 | if ( style.HasFont() ) | |
2351 | { | |
aac7e7fe VZ |
2352 | // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0 |
2353 | // but using it doesn't seem to hurt neither so leaving it for now | |
2354 | ||
784164e1 VZ |
2355 | cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET | |
2356 | CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE; | |
4bc1afd5 VZ |
2357 | |
2358 | // fill in data from LOGFONT but recalculate lfHeight because we need | |
2359 | // the real height in twips and not the negative number which | |
2360 | // wxFillLogFont() returns (this is correct in general and works with | |
2361 | // the Windows font mapper, but not here) | |
44cc96a8 JS |
2362 | |
2363 | wxFont font(style.GetFont()); | |
2364 | ||
4bc1afd5 | 2365 | LOGFONT lf; |
44cc96a8 JS |
2366 | wxFillLogFont(&lf, &font); |
2367 | cf.yHeight = 20*font.GetPointSize(); // 1 pt = 20 twips | |
4bc1afd5 VZ |
2368 | cf.bCharSet = lf.lfCharSet; |
2369 | cf.bPitchAndFamily = lf.lfPitchAndFamily; | |
e408bf52 | 2370 | wxStrlcpy(cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName)); |
4bc1afd5 | 2371 | |
784164e1 VZ |
2372 | // also deal with underline/italic/bold attributes: note that we must |
2373 | // always set CFM_ITALIC &c bits in dwMask, even if we don't set the | |
2374 | // style to allow clearing it | |
4bc1afd5 VZ |
2375 | if ( lf.lfItalic ) |
2376 | { | |
4bc1afd5 VZ |
2377 | cf.dwEffects |= CFE_ITALIC; |
2378 | } | |
2379 | ||
2380 | if ( lf.lfWeight == FW_BOLD ) | |
2381 | { | |
4bc1afd5 VZ |
2382 | cf.dwEffects |= CFE_BOLD; |
2383 | } | |
2384 | ||
2385 | if ( lf.lfUnderline ) | |
2386 | { | |
4bc1afd5 VZ |
2387 | cf.dwEffects |= CFE_UNDERLINE; |
2388 | } | |
2389 | ||
77ffb593 | 2390 | // strikeout fonts are not supported by wxWidgets |
4bc1afd5 VZ |
2391 | } |
2392 | ||
2393 | if ( style.HasTextColour() ) | |
2394 | { | |
2395 | cf.dwMask |= CFM_COLOR; | |
2396 | cf.crTextColor = wxColourToRGB(style.GetTextColour()); | |
2397 | } | |
2398 | ||
be329a3d | 2399 | #if wxUSE_RICHEDIT2 |
a5aa8086 | 2400 | if ( m_verRichEdit != 1 && style.HasBackgroundColour() ) |
be329a3d RD |
2401 | { |
2402 | cf.dwMask |= CFM_BACKCOLOR; | |
2403 | cf.crBackColor = wxColourToRGB(style.GetBackgroundColour()); | |
2404 | } | |
784164e1 VZ |
2405 | #endif // wxUSE_RICHEDIT2 |
2406 | ||
179c657b VZ |
2407 | // Apply the style either to the selection or to the entire control. |
2408 | WPARAM selMode; | |
2409 | if ( start != -1 || end != -1 ) | |
2410 | { | |
2411 | DoSetSelection(start, end, SetSel_NoScroll); | |
2412 | selMode = SCF_SELECTION; | |
2413 | } | |
2414 | else | |
2415 | { | |
2416 | selMode = SCF_ALL; | |
2417 | } | |
a8abba41 | 2418 | |
179c657b | 2419 | if ( !::SendMessage(GetHwnd(), EM_SETCHARFORMAT, selMode, (LPARAM)&cf) ) |
4bc1afd5 | 2420 | { |
a8abba41 VZ |
2421 | wxLogLastError(wxT("SendMessage(EM_SETCHARFORMAT)")); |
2422 | return false; | |
4bc1afd5 VZ |
2423 | } |
2424 | ||
a8abba41 VZ |
2425 | return true; |
2426 | } | |
2427 | ||
2428 | bool wxTextCtrl::MSWSetParaFormat(const wxTextAttr& style, long start, long end) | |
2429 | { | |
2430 | #if wxUSE_RICHEDIT2 | |
e00a5d3c | 2431 | PARAFORMAT2 pf; |
a8abba41 VZ |
2432 | #else |
2433 | PARAFORMAT pf; | |
2434 | #endif | |
2435 | ||
e00a5d3c | 2436 | wxZeroMemory(pf); |
a8abba41 | 2437 | |
e00a5d3c JS |
2438 | // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple |
2439 | // PARAFORMAT in that case | |
2440 | #if wxUSE_RICHEDIT2 | |
2441 | if ( m_verRichEdit == 1 ) | |
2442 | { | |
2443 | // this is the only thing the control is going to grok | |
2444 | pf.cbSize = sizeof(PARAFORMAT); | |
2445 | } | |
2446 | else | |
2447 | #endif | |
2448 | { | |
2449 | // PARAFORMAT or PARAFORMAT2 | |
2450 | pf.cbSize = sizeof(pf); | |
2451 | } | |
2452 | ||
2453 | if (style.HasAlignment()) | |
2454 | { | |
2455 | pf.dwMask |= PFM_ALIGNMENT; | |
2456 | if (style.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT) | |
2457 | pf.wAlignment = PFA_RIGHT; | |
2458 | else if (style.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE) | |
2459 | pf.wAlignment = PFA_CENTER; | |
2460 | else if (style.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED) | |
2461 | pf.wAlignment = PFA_JUSTIFY; | |
2462 | else | |
2463 | pf.wAlignment = PFA_LEFT; | |
2464 | } | |
2465 | ||
2466 | if (style.HasLeftIndent()) | |
2467 | { | |
89b67477 | 2468 | pf.dwMask |= PFM_STARTINDENT | PFM_OFFSET; |
e00a5d3c JS |
2469 | |
2470 | // Convert from 1/10 mm to TWIPS | |
2471 | pf.dxStartIndent = (int) (((double) style.GetLeftIndent()) * mm2twips / 10.0) ; | |
89b67477 | 2472 | pf.dxOffset = (int) (((double) style.GetLeftSubIndent()) * mm2twips / 10.0) ; |
e00a5d3c JS |
2473 | } |
2474 | ||
2475 | if (style.HasRightIndent()) | |
2476 | { | |
2477 | pf.dwMask |= PFM_RIGHTINDENT; | |
2478 | ||
2479 | // Convert from 1/10 mm to TWIPS | |
2480 | pf.dxRightIndent = (int) (((double) style.GetRightIndent()) * mm2twips / 10.0) ; | |
2481 | } | |
2482 | ||
2483 | if (style.HasTabs()) | |
2484 | { | |
2485 | pf.dwMask |= PFM_TABSTOPS; | |
2486 | ||
2487 | const wxArrayInt& tabs = style.GetTabs(); | |
2488 | ||
5c519b6c | 2489 | pf.cTabCount = (SHORT)wxMin(tabs.GetCount(), MAX_TAB_STOPS); |
e00a5d3c JS |
2490 | size_t i; |
2491 | for (i = 0; i < (size_t) pf.cTabCount; i++) | |
2492 | { | |
2493 | // Convert from 1/10 mm to TWIPS | |
2494 | pf.rgxTabs[i] = (int) (((double) tabs[i]) * mm2twips / 10.0) ; | |
2495 | } | |
2496 | } | |
2497 | ||
3488be9c VZ |
2498 | #if wxUSE_RICHEDIT2 |
2499 | if ( m_verRichEdit > 1 ) | |
2500 | { | |
2501 | if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ) | |
2502 | { | |
2503 | // Use RTL paragraphs in RTL mode to get proper layout | |
2504 | pf.dwMask |= PFM_RTLPARA; | |
2505 | pf.wEffects |= PFE_RTLPARA; | |
2506 | } | |
2507 | } | |
2508 | #endif // wxUSE_RICHEDIT2 | |
2509 | ||
2510 | if ( pf.dwMask ) | |
e00a5d3c | 2511 | { |
a8abba41 VZ |
2512 | // Do format the selection. |
2513 | DoSetSelection(start, end, SetSel_NoScroll); | |
2514 | ||
2515 | if ( !::SendMessage(GetHwnd(), EM_SETPARAFORMAT, 0, (LPARAM) &pf) ) | |
e00a5d3c | 2516 | { |
a8abba41 VZ |
2517 | wxLogLastError(wxT("SendMessage(EM_SETPARAFORMAT)")); |
2518 | ||
2519 | return false; | |
e00a5d3c JS |
2520 | } |
2521 | } | |
2522 | ||
a8abba41 VZ |
2523 | return true; |
2524 | } | |
2525 | ||
2526 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) | |
2527 | { | |
2528 | if ( !IsRich() ) | |
2529 | { | |
2530 | // can't do it with normal text control | |
2531 | return false; | |
2532 | } | |
2533 | ||
2534 | // the richedit 1.0 doesn't handle setting background colour, so don't | |
2535 | // even try to do anything if it's the only thing we want to change | |
2536 | if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() && | |
2537 | !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() && | |
2538 | !style.HasTabs() ) | |
2539 | { | |
2540 | // nothing to do: return true if there was really nothing to do and | |
2541 | // false if we failed to set bg colour | |
2542 | return !style.HasBackgroundColour(); | |
2543 | } | |
2544 | ||
2545 | // order the range if needed | |
2546 | if ( start > end ) | |
2547 | wxSwap(start, end); | |
2548 | ||
2549 | // we can only change the format of the selection, so select the range we | |
2550 | // want and restore the old selection later, after MSWSetXXXFormat() | |
2551 | // functions (possibly) change it. | |
2552 | long startOld, endOld; | |
2553 | GetSelection(&startOld, &endOld); | |
2554 | ||
2555 | bool ok = MSWSetCharFormat(style, start, end); | |
2556 | if ( !MSWSetParaFormat(style, start, end) ) | |
2557 | ok = false; | |
2558 | ||
2559 | if ( start != startOld || end != endOld ) | |
4bc1afd5 VZ |
2560 | { |
2561 | // restore the original selection | |
fa2f57be | 2562 | DoSetSelection(startOld, endOld, SetSel_NoScroll); |
4bc1afd5 VZ |
2563 | } |
2564 | ||
2565 | return ok; | |
2566 | } | |
f6bcfd97 | 2567 | |
5bf75ae7 VZ |
2568 | bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) |
2569 | { | |
2570 | if ( !wxTextCtrlBase::SetDefaultStyle(style) ) | |
bfbb0b4c | 2571 | return false; |
5bf75ae7 | 2572 | |
caea50ac VZ |
2573 | if ( IsEditable() ) |
2574 | { | |
2575 | // we have to do this or the style wouldn't apply for the text typed by | |
2576 | // the user | |
7d8268a1 | 2577 | wxTextPos posLast = GetLastPosition(); |
caea50ac VZ |
2578 | SetStyle(posLast, posLast, m_defaultStyle); |
2579 | } | |
5bf75ae7 | 2580 | |
bfbb0b4c | 2581 | return true; |
5bf75ae7 VZ |
2582 | } |
2583 | ||
80185c6c | 2584 | bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) |
e00a5d3c | 2585 | { |
80185c6c JS |
2586 | if ( !IsRich() ) |
2587 | { | |
2588 | // can't do it with normal text control | |
bfbb0b4c | 2589 | return false; |
80185c6c JS |
2590 | } |
2591 | ||
2592 | // initialize CHARFORMAT struct | |
2593 | #if wxUSE_RICHEDIT2 | |
2594 | CHARFORMAT2 cf; | |
2595 | #else | |
2596 | CHARFORMAT cf; | |
2597 | #endif | |
2598 | ||
2599 | wxZeroMemory(cf); | |
2600 | ||
2601 | // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple | |
2602 | // CHARFORMAT in that case | |
2603 | #if wxUSE_RICHEDIT2 | |
2604 | if ( m_verRichEdit == 1 ) | |
2605 | { | |
2606 | // this is the only thing the control is going to grok | |
2607 | cf.cbSize = sizeof(CHARFORMAT); | |
2608 | } | |
2609 | else | |
2610 | #endif | |
2611 | { | |
2612 | // CHARFORMAT or CHARFORMAT2 | |
2613 | cf.cbSize = sizeof(cf); | |
2614 | } | |
2615 | // we can only change the format of the selection, so select the range we | |
2616 | // want and restore the old selection later | |
2617 | long startOld, endOld; | |
2618 | GetSelection(&startOld, &endOld); | |
2619 | ||
2620 | // but do we really have to change the selection? | |
2621 | bool changeSel = position != startOld || position != endOld; | |
2622 | ||
2623 | if ( changeSel ) | |
2624 | { | |
fa2f57be | 2625 | DoSetSelection(position, position + 1, SetSel_NoScroll); |
80185c6c JS |
2626 | } |
2627 | ||
2628 | // get the selection formatting | |
2629 | (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT, | |
2630 | SCF_SELECTION, (LPARAM)&cf) ; | |
2631 | ||
093dee5e | 2632 | |
80185c6c JS |
2633 | LOGFONT lf; |
2634 | lf.lfHeight = cf.yHeight; | |
2635 | lf.lfWidth = 0; | |
2636 | lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset? | |
2637 | lf.lfClipPrecision = 0; | |
2638 | lf.lfEscapement = 0; | |
2639 | wxStrcpy(lf.lfFaceName, cf.szFaceName); | |
093dee5e RN |
2640 | |
2641 | //NOTE: we _MUST_ set each of these values to _something_ since we | |
7d8268a1 | 2642 | //do not call wxZeroMemory on the LOGFONT lf |
80185c6c JS |
2643 | if (cf.dwEffects & CFE_ITALIC) |
2644 | lf.lfItalic = TRUE; | |
093dee5e RN |
2645 | else |
2646 | lf.lfItalic = FALSE; | |
2647 | ||
80185c6c JS |
2648 | lf.lfOrientation = 0; |
2649 | lf.lfPitchAndFamily = cf.bPitchAndFamily; | |
2650 | lf.lfQuality = 0; | |
093dee5e | 2651 | |
80185c6c JS |
2652 | if (cf.dwEffects & CFE_STRIKEOUT) |
2653 | lf.lfStrikeOut = TRUE; | |
093dee5e RN |
2654 | else |
2655 | lf.lfStrikeOut = FALSE; | |
2656 | ||
80185c6c JS |
2657 | if (cf.dwEffects & CFE_UNDERLINE) |
2658 | lf.lfUnderline = TRUE; | |
093dee5e RN |
2659 | else |
2660 | lf.lfUnderline = FALSE; | |
2661 | ||
80185c6c JS |
2662 | if (cf.dwEffects & CFE_BOLD) |
2663 | lf.lfWeight = FW_BOLD; | |
093dee5e RN |
2664 | else |
2665 | lf.lfWeight = FW_NORMAL; | |
80185c6c JS |
2666 | |
2667 | wxFont font = wxCreateFontFromLogFont(& lf); | |
a1b806b9 | 2668 | if (font.IsOk()) |
80185c6c JS |
2669 | { |
2670 | style.SetFont(font); | |
2671 | } | |
2672 | style.SetTextColour(wxColour(cf.crTextColor)); | |
2673 | ||
2674 | #if wxUSE_RICHEDIT2 | |
2675 | if ( m_verRichEdit != 1 ) | |
2676 | { | |
2677 | // cf.dwMask |= CFM_BACKCOLOR; | |
2678 | style.SetBackgroundColour(wxColour(cf.crBackColor)); | |
2679 | } | |
2680 | #endif // wxUSE_RICHEDIT2 | |
2681 | ||
2682 | // now get the paragraph formatting | |
2683 | PARAFORMAT2 pf; | |
2684 | wxZeroMemory(pf); | |
2685 | // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple | |
2686 | // PARAFORMAT in that case | |
2687 | #if wxUSE_RICHEDIT2 | |
2688 | if ( m_verRichEdit == 1 ) | |
2689 | { | |
2690 | // this is the only thing the control is going to grok | |
2691 | pf.cbSize = sizeof(PARAFORMAT); | |
2692 | } | |
2693 | else | |
2694 | #endif | |
2695 | { | |
2696 | // PARAFORMAT or PARAFORMAT2 | |
2697 | pf.cbSize = sizeof(pf); | |
2698 | } | |
2699 | ||
2700 | // do format the selection | |
2701 | (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT, 0, (LPARAM) &pf) ; | |
2702 | ||
89b67477 | 2703 | style.SetLeftIndent( (int) ((double) pf.dxStartIndent * twips2mm * 10.0), (int) ((double) pf.dxOffset * twips2mm * 10.0) ); |
80185c6c JS |
2704 | style.SetRightIndent( (int) ((double) pf.dxRightIndent * twips2mm * 10.0) ); |
2705 | ||
2706 | if (pf.wAlignment == PFA_CENTER) | |
2707 | style.SetAlignment(wxTEXT_ALIGNMENT_CENTRE); | |
2708 | else if (pf.wAlignment == PFA_RIGHT) | |
2709 | style.SetAlignment(wxTEXT_ALIGNMENT_RIGHT); | |
2710 | else if (pf.wAlignment == PFA_JUSTIFY) | |
2711 | style.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED); | |
2712 | else | |
2713 | style.SetAlignment(wxTEXT_ALIGNMENT_LEFT); | |
2714 | ||
2715 | wxArrayInt tabStops; | |
2716 | size_t i; | |
2717 | for (i = 0; i < (size_t) pf.cTabCount; i++) | |
2718 | { | |
89b67477 | 2719 | tabStops.Add( (int) ((double) (pf.rgxTabs[i] & 0xFFFF) * twips2mm * 10.0) ); |
80185c6c JS |
2720 | } |
2721 | ||
2722 | if ( changeSel ) | |
2723 | { | |
2724 | // restore the original selection | |
fa2f57be | 2725 | DoSetSelection(startOld, endOld, SetSel_NoScroll); |
80185c6c JS |
2726 | } |
2727 | ||
bfbb0b4c | 2728 | return true; |
e00a5d3c JS |
2729 | } |
2730 | ||
b12915c1 VZ |
2731 | // ---------------------------------------------------------------------------- |
2732 | // wxRichEditModule | |
2733 | // ---------------------------------------------------------------------------- | |
2734 | ||
628c219e VZ |
2735 | static const HINSTANCE INVALID_HINSTANCE = (HINSTANCE)-1; |
2736 | ||
b12915c1 VZ |
2737 | bool wxRichEditModule::OnInit() |
2738 | { | |
2739 | // don't do anything - we will load it when needed | |
bfbb0b4c | 2740 | return true; |
b12915c1 VZ |
2741 | } |
2742 | ||
2743 | void wxRichEditModule::OnExit() | |
2744 | { | |
136cb3c7 | 2745 | for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ ) |
b12915c1 | 2746 | { |
628c219e | 2747 | if ( ms_hRichEdit[i] && ms_hRichEdit[i] != INVALID_HINSTANCE ) |
a5aa8086 VZ |
2748 | { |
2749 | ::FreeLibrary(ms_hRichEdit[i]); | |
8c74e477 | 2750 | ms_hRichEdit[i] = NULL; |
a5aa8086 | 2751 | } |
b12915c1 | 2752 | } |
8ef51d67 JS |
2753 | #if wxUSE_INKEDIT |
2754 | if (ms_inkEditLib.IsLoaded()) | |
2755 | ms_inkEditLib.Unload(); | |
2756 | #endif | |
b12915c1 VZ |
2757 | } |
2758 | ||
2759 | /* static */ | |
628c219e | 2760 | bool wxRichEditModule::Load(Version version) |
b12915c1 | 2761 | { |
628c219e | 2762 | if ( ms_hRichEdit[version] == INVALID_HINSTANCE ) |
b12915c1 | 2763 | { |
a5aa8086 | 2764 | // we had already tried to load it and failed |
bfbb0b4c | 2765 | return false; |
b12915c1 VZ |
2766 | } |
2767 | ||
28978e0c VZ |
2768 | if ( ms_hRichEdit[version] ) |
2769 | { | |
2770 | // we've already got this one | |
bfbb0b4c | 2771 | return true; |
28978e0c VZ |
2772 | } |
2773 | ||
a243da29 | 2774 | static const wxChar *const dllnames[] = |
628c219e | 2775 | { |
9a83f860 VZ |
2776 | wxT("riched32"), |
2777 | wxT("riched20"), | |
2778 | wxT("msftedit"), | |
628c219e VZ |
2779 | }; |
2780 | ||
2781 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(dllnames) == Version_Max, | |
2782 | RichEditDllNamesVersionsMismatch ); | |
b12915c1 | 2783 | |
628c219e | 2784 | ms_hRichEdit[version] = ::LoadLibrary(dllnames[version]); |
b12915c1 | 2785 | |
a5aa8086 | 2786 | if ( !ms_hRichEdit[version] ) |
b12915c1 | 2787 | { |
628c219e | 2788 | ms_hRichEdit[version] = INVALID_HINSTANCE; |
b12915c1 | 2789 | |
bfbb0b4c | 2790 | return false; |
b12915c1 VZ |
2791 | } |
2792 | ||
bfbb0b4c | 2793 | return true; |
b12915c1 VZ |
2794 | } |
2795 | ||
8ef51d67 JS |
2796 | #if wxUSE_INKEDIT |
2797 | // load the InkEdit library | |
2798 | bool wxRichEditModule::LoadInkEdit() | |
2799 | { | |
8ef51d67 | 2800 | if (ms_inkEditLibLoadAttemped) |
a10f026b | 2801 | return ms_inkEditLib.IsLoaded(); |
40ff126a | 2802 | |
8ef51d67 | 2803 | ms_inkEditLibLoadAttemped = true; |
40ff126a | 2804 | |
8ef51d67 JS |
2805 | wxLogNull logNull; |
2806 | return ms_inkEditLib.Load(wxT("inked")); | |
2807 | } | |
fa2f57be | 2808 | #endif // wxUSE_INKEDIT |
8ef51d67 JS |
2809 | |
2810 | ||
b12915c1 VZ |
2811 | #endif // wxUSE_RICHEDIT |
2812 | ||
3180bc0e | 2813 | #endif // wxUSE_TEXTCTRL && !(__SMARTPHONE__ && __WXWINCE__) |