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