]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
bfbd6dc1 | 2 | // Name: msw/textctrl.cpp |
2bda0e17 KB |
3 | // Purpose: wxTextCtrl |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a1b82138 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
2bda0e17 | 16 | #ifdef __GNUG__ |
a1b82138 | 17 | #pragma implementation "textctrl.h" |
2bda0e17 KB |
18 | #endif |
19 | ||
a1b82138 VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
2bda0e17 KB |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
a1b82138 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
a1b82138 VZ |
32 | #include "wx/textctrl.h" |
33 | #include "wx/settings.h" | |
34 | #include "wx/brush.h" | |
35 | #include "wx/utils.h" | |
381dd4bf | 36 | #include "wx/intl.h" |
a1b82138 | 37 | #include "wx/log.h" |
381dd4bf | 38 | #include "wx/app.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
f6bcfd97 BP |
41 | #include "wx/module.h" |
42 | ||
47d67540 | 43 | #if wxUSE_CLIPBOARD |
a1b82138 | 44 | #include "wx/clipbrd.h" |
2bda0e17 KB |
45 | #endif |
46 | ||
a1b82138 VZ |
47 | #include "wx/textfile.h" |
48 | ||
49 | #include <windowsx.h> | |
50 | ||
2bda0e17 KB |
51 | #include "wx/msw/private.h" |
52 | ||
a1b82138 | 53 | #include <string.h> |
2bda0e17 | 54 | #include <stdlib.h> |
a1b82138 | 55 | #include <sys/types.h> |
fbc535ff JS |
56 | |
57 | #if wxUSE_IOSTREAMH | |
3f4a0c5b | 58 | # include <fstream.h> |
fbc535ff | 59 | #else |
3f4a0c5b | 60 | # include <fstream> |
fbc535ff | 61 | #endif |
2bda0e17 | 62 | |
c42404a5 | 63 | #if wxUSE_RICHEDIT && !defined(__GNUWIN32_OLD__) |
cd471848 | 64 | #include <richedit.h> |
2bda0e17 KB |
65 | #endif |
66 | ||
b12915c1 VZ |
67 | // ---------------------------------------------------------------------------- |
68 | // private classes | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | #if wxUSE_RICHEDIT | |
72 | ||
73 | // this module initializes RichEdit DLL if needed | |
74 | class wxRichEditModule : public wxModule | |
75 | { | |
76 | public: | |
77 | virtual bool OnInit(); | |
78 | virtual void OnExit(); | |
79 | ||
80 | // get the version currently loaded, -1 if none | |
81 | static int GetLoadedVersion() { return ms_verRichEdit; } | |
82 | ||
83 | // load the richedit DLL of at least of required version | |
84 | static bool Load(int version = 1); | |
85 | ||
86 | private: | |
87 | // the handle to richedit DLL and the version of the DLL loaded | |
88 | static HINSTANCE ms_hRichEdit; | |
89 | ||
90 | // the DLL version loaded or -1 if none | |
91 | static int ms_verRichEdit; | |
92 | ||
93 | DECLARE_DYNAMIC_CLASS(wxRichEditModule) | |
94 | }; | |
95 | ||
96 | HINSTANCE wxRichEditModule::ms_hRichEdit = (HINSTANCE)NULL; | |
97 | int wxRichEditModule::ms_verRichEdit = -1; | |
98 | ||
99 | IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule) | |
100 | ||
101 | #endif // wxUSE_RICHEDIT | |
e702ff0f | 102 | |
a1b82138 VZ |
103 | // ---------------------------------------------------------------------------- |
104 | // event tables and other macros | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
2bda0e17 KB |
107 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
108 | ||
109 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
a1b82138 VZ |
110 | EVT_CHAR(wxTextCtrl::OnChar) |
111 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
112 | ||
113 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
114 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
115 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
116 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
117 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
118 | ||
119 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
120 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
121 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
122 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
123 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
f6bcfd97 BP |
124 | #ifdef __WIN16__ |
125 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
126 | #endif | |
2bda0e17 | 127 | END_EVENT_TABLE() |
e702ff0f | 128 | |
2bda0e17 | 129 | |
a1b82138 VZ |
130 | // ============================================================================ |
131 | // implementation | |
132 | // ============================================================================ | |
133 | ||
134 | // ---------------------------------------------------------------------------- | |
135 | // creation | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
cd471848 | 138 | wxTextCtrl::wxTextCtrl() |
2bda0e17 | 139 | { |
cd471848 | 140 | #if wxUSE_RICHEDIT |
a1b82138 | 141 | m_isRich = FALSE; |
cd471848 | 142 | #endif |
2bda0e17 KB |
143 | } |
144 | ||
debe6624 | 145 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
146 | const wxString& value, |
147 | const wxPoint& pos, | |
a1b82138 VZ |
148 | const wxSize& size, |
149 | long style, | |
c085e333 VZ |
150 | const wxValidator& validator, |
151 | const wxString& name) | |
2bda0e17 | 152 | { |
a1b82138 | 153 | // base initialization |
8d99be5f | 154 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) |
a1b82138 | 155 | return FALSE; |
2bda0e17 | 156 | |
a1b82138 VZ |
157 | if ( parent ) |
158 | parent->AddChild(this); | |
2bda0e17 | 159 | |
a1b82138 VZ |
160 | // translate wxWin style flags to MSW ones, checking for consistency while |
161 | // doing it | |
f6bcfd97 | 162 | long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP /* | WS_CLIPSIBLINGS */ ; |
a1b82138 VZ |
163 | if ( m_windowStyle & wxTE_MULTILINE ) |
164 | { | |
165 | wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER), | |
f6bcfd97 | 166 | wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); |
5fb9fcfc | 167 | |
b70ababc JS |
168 | msStyle |= ES_MULTILINE | ES_WANTRETURN; |
169 | if ((m_windowStyle & wxTE_NO_VSCROLL) == 0) | |
170 | msStyle |= WS_VSCROLL; | |
a1b82138 VZ |
171 | m_windowStyle |= wxTE_PROCESS_ENTER; |
172 | } | |
173 | else | |
174 | msStyle |= ES_AUTOHSCROLL; | |
2bda0e17 | 175 | |
2c738dd8 UM |
176 | if (m_windowStyle & wxHSCROLL) |
177 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL); | |
178 | ||
a1b82138 VZ |
179 | if (m_windowStyle & wxTE_READONLY) |
180 | msStyle |= ES_READONLY; | |
2bda0e17 | 181 | |
a1b82138 VZ |
182 | if (m_windowStyle & wxTE_PASSWORD) // hidden input |
183 | msStyle |= ES_PASSWORD; | |
2bda0e17 | 184 | |
cfdd3a98 UM |
185 | if (m_windowStyle & wxTE_AUTO_SCROLL) |
186 | msStyle |= ES_AUTOHSCROLL; | |
187 | ||
188 | ||
101f488c VZ |
189 | // we always want the characters and the arrows |
190 | m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
191 | ||
192 | // we may have several different cases: | |
193 | // 1. normal case: both TAB and ENTER are used for dialog navigation | |
194 | // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next | |
195 | // control in the dialog | |
196 | // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation | |
197 | // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to | |
198 | // the next control | |
199 | if ( m_windowStyle & wxTE_PROCESS_ENTER ) | |
200 | m_lDlgCode |= DLGC_WANTMESSAGE; | |
201 | if ( m_windowStyle & wxTE_PROCESS_TAB ) | |
202 | m_lDlgCode |= DLGC_WANTTAB; | |
203 | ||
a1b82138 | 204 | // do create the control - either an EDIT or RICHEDIT |
b12915c1 | 205 | wxString windowClass = wxT("EDIT"); |
cd471848 | 206 | |
57c208c5 | 207 | #if wxUSE_RICHEDIT |
c49245f8 | 208 | if ( m_windowStyle & wxTE_RICH ) |
a1b82138 | 209 | { |
0d0512bd VZ |
210 | static bool s_errorGiven = FALSE; // MT-FIXME |
211 | ||
212 | // only give the error msg once if the DLL can't be loaded | |
213 | if ( !s_errorGiven ) | |
214 | { | |
215 | // first try to load the RichEdit DLL (will do nothing if already | |
216 | // done) | |
b12915c1 | 217 | if ( !wxRichEditModule::Load() ) |
0d0512bd | 218 | { |
f6bcfd97 | 219 | wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll")); |
0d0512bd VZ |
220 | |
221 | s_errorGiven = TRUE; | |
222 | } | |
223 | } | |
224 | ||
225 | if ( s_errorGiven ) | |
226 | { | |
227 | m_isRich = FALSE; | |
228 | } | |
229 | else | |
230 | { | |
231 | msStyle |= ES_AUTOVSCROLL; | |
232 | m_isRich = TRUE; | |
b12915c1 VZ |
233 | |
234 | int ver = wxRichEditModule::GetLoadedVersion(); | |
235 | if ( ver == 1 ) | |
236 | { | |
237 | windowClass = wxT("RICHEDIT"); | |
238 | } | |
239 | else | |
240 | { | |
241 | #ifndef RICHEDIT_CLASS | |
242 | wxString RICHEDIT_CLASS; | |
243 | RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), ver); | |
5fb2f4ba | 244 | #if wxUSE_UNICODE |
b12915c1 VZ |
245 | RICHEDIT_CLASS += _T('W'); |
246 | #else // ANSI | |
247 | RICHEDIT_CLASS += _T('A'); | |
248 | #endif // Unicode/ANSI | |
249 | #endif // !RICHEDIT_CLASS | |
250 | ||
251 | windowClass = RICHEDIT_CLASS; | |
252 | } | |
0d0512bd | 253 | } |
a1b82138 VZ |
254 | } |
255 | else | |
256 | m_isRich = FALSE; | |
b12915c1 | 257 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 258 | |
a1b82138 VZ |
259 | bool want3D; |
260 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); | |
2bda0e17 | 261 | |
a1b82138 VZ |
262 | // Even with extended styles, need to combine with WS_BORDER for them to |
263 | // look right. | |
264 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
265 | msStyle |= WS_BORDER; | |
2bda0e17 | 266 | |
a1b82138 VZ |
267 | // NB: don't use pos and size as CreateWindowEx arguments because they |
268 | // might be -1 in which case we should use the default values (and | |
269 | // SetSize called below takes care of it) | |
270 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, | |
5fb2f4ba | 271 | windowClass.c_str(), |
a1b82138 VZ |
272 | NULL, |
273 | msStyle, | |
274 | 0, 0, 0, 0, | |
275 | GetHwndOf(parent), | |
276 | (HMENU)m_windowId, | |
277 | wxGetInstance(), | |
278 | NULL); | |
2bda0e17 | 279 | |
223d09f6 | 280 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") ); |
c085e333 | 281 | |
1f112209 | 282 | #if wxUSE_CTL3D |
a1b82138 VZ |
283 | if ( want3D ) |
284 | { | |
285 | Ctl3dSubclassCtl(GetHwnd()); | |
286 | m_useCtl3D = TRUE; | |
287 | } | |
2bda0e17 KB |
288 | #endif |
289 | ||
57c208c5 | 290 | #if wxUSE_RICHEDIT |
a1b82138 VZ |
291 | if (m_isRich) |
292 | { | |
293 | // Have to enable events | |
294 | ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, | |
295 | ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE); | |
296 | } | |
2bda0e17 KB |
297 | #endif |
298 | ||
a1b82138 | 299 | SubclassWin(GetHWND()); |
2bda0e17 | 300 | |
a1b82138 VZ |
301 | // set font, position, size and initial value |
302 | wxFont& fontParent = parent->GetFont(); | |
303 | if ( fontParent.Ok() ) | |
304 | { | |
305 | SetFont(fontParent); | |
306 | } | |
307 | else | |
308 | { | |
309 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT)); | |
310 | } | |
2bda0e17 | 311 | |
a1b82138 | 312 | // Causes a crash for Symantec C++ and WIN32 for some reason |
2bda0e17 | 313 | #if !(defined(__SC__) && defined(__WIN32__)) |
a1b82138 VZ |
314 | if ( !value.IsEmpty() ) |
315 | { | |
316 | SetValue(value); | |
317 | } | |
2bda0e17 KB |
318 | #endif |
319 | ||
d47ebd1e JS |
320 | // set colours |
321 | SetupColours(); | |
322 | ||
a1b82138 VZ |
323 | SetSize(pos.x, pos.y, size.x, size.y); |
324 | ||
325 | return TRUE; | |
2bda0e17 KB |
326 | } |
327 | ||
328 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 329 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 330 | { |
c085e333 | 331 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 332 | |
789295bf | 333 | HWND hWnd = GetHwnd(); |
a1b82138 | 334 | long style = GetWindowLong(hWnd, GWL_STYLE); |
2bda0e17 | 335 | |
cd471848 VZ |
336 | // retrieve the style to see whether this is an edit or richedit ctrl |
337 | #if wxUSE_RICHEDIT | |
837e5743 | 338 | wxChar buf[256]; |
2bda0e17 | 339 | |
a1b82138 | 340 | GetClassName(hWnd, buf, WXSIZEOF(buf)); |
2bda0e17 | 341 | |
223d09f6 | 342 | if ( wxStricmp(buf, wxT("EDIT")) == 0 ) |
c085e333 VZ |
343 | m_isRich = FALSE; |
344 | else | |
345 | m_isRich = TRUE; | |
a1b82138 | 346 | #endif // wxUSE_RICHEDIT |
c085e333 VZ |
347 | |
348 | if (style & ES_MULTILINE) | |
349 | m_windowStyle |= wxTE_MULTILINE; | |
350 | if (style & ES_PASSWORD) | |
351 | m_windowStyle |= wxTE_PASSWORD; | |
352 | if (style & ES_READONLY) | |
353 | m_windowStyle |= wxTE_READONLY; | |
354 | if (style & ES_WANTRETURN) | |
355 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
2bda0e17 KB |
356 | } |
357 | ||
cd471848 | 358 | void wxTextCtrl::SetupColours() |
2bda0e17 | 359 | { |
d47ebd1e | 360 | wxColour bkgndColour; |
f6bcfd97 | 361 | // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE)) |
d47ebd1e | 362 | bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW); |
f6bcfd97 BP |
363 | // else |
364 | // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE); | |
d47ebd1e JS |
365 | |
366 | SetBackgroundColour(bkgndColour); | |
a1b82138 | 367 | SetForegroundColour(GetParent()->GetForegroundColour()); |
2bda0e17 KB |
368 | } |
369 | ||
a1b82138 VZ |
370 | // ---------------------------------------------------------------------------- |
371 | // set/get the controls text | |
372 | // ---------------------------------------------------------------------------- | |
373 | ||
cd471848 | 374 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 375 | { |
b12915c1 VZ |
376 | // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for |
377 | // retrieving more than 64Kb under Win9x | |
378 | #if wxUSE_RICHEDIT | |
379 | if ( m_isRich ) | |
380 | { | |
b12915c1 | 381 | int len = GetWindowTextLength(GetHwnd()) + 1; |
5fb2f4ba GRG |
382 | |
383 | wxString str; | |
b12915c1 VZ |
384 | wxChar *p = str.GetWriteBuf(len); |
385 | ||
386 | TEXTRANGE textRange; | |
387 | textRange.chrg.cpMin = 0; | |
388 | textRange.chrg.cpMax = -1; | |
389 | textRange.lpstrText = p; | |
390 | ||
391 | (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange); | |
392 | ||
393 | // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for the | |
394 | // newlines which is neither Unix nor Windows style (Win95 with | |
395 | // riched20.dll shows this behaviour) - convert it to something | |
396 | // reasonable | |
397 | for ( ; *p; p++ ) | |
398 | { | |
399 | if ( *p == _T('\r') ) | |
400 | *p = _T('\n'); | |
401 | } | |
402 | ||
403 | str.UngetWriteBuf(); | |
404 | ||
405 | return str; | |
406 | } | |
407 | #endif // wxUSE_RICHEDIT | |
408 | ||
409 | // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the | |
410 | // same one as above for consitency | |
411 | wxString str = wxGetWindowText(GetHWND()); | |
412 | ||
413 | return wxTextFile::Translate(str, wxTextFileType_Unix); | |
2bda0e17 KB |
414 | } |
415 | ||
416 | void wxTextCtrl::SetValue(const wxString& value) | |
417 | { | |
b12915c1 VZ |
418 | // if the text is long enough, it's faster to just set it instead of first |
419 | // comparing it with the old one (chances are that it will be different | |
420 | // anyhow, this comparison is there to avoid flicker for small single-line | |
421 | // edit controls mostly) | |
422 | if ( (value.length() > 0x400) || (value != GetValue()) ) | |
07cf98cb | 423 | { |
b12915c1 | 424 | wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); |
c4608a8a | 425 | |
5fb2f4ba | 426 | SetWindowText(GetHwnd(), valueDos.c_str()); |
a1b82138 | 427 | |
f6bcfd97 BP |
428 | // for compatibility with the GTK and because it is more logical, we |
429 | // move the cursor to the end of the text after SetValue() | |
430 | ||
431 | // GRG, Jun/2000: Changed this back after a lot of discussion | |
432 | // in the lists. wxWindows 2.2 will have a set of flags to | |
433 | // customize this behaviour. | |
434 | //SetInsertionPointEnd(); | |
435 | ||
07cf98cb VZ |
436 | AdjustSpaceLimit(); |
437 | } | |
2bda0e17 KB |
438 | } |
439 | ||
a1b82138 | 440 | void wxTextCtrl::WriteText(const wxString& value) |
2bda0e17 | 441 | { |
a1b82138 | 442 | wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); |
2bda0e17 | 443 | |
a1b82138 | 444 | SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str()); |
2bda0e17 | 445 | |
a1b82138 | 446 | AdjustSpaceLimit(); |
2bda0e17 KB |
447 | } |
448 | ||
a1b82138 VZ |
449 | void wxTextCtrl::AppendText(const wxString& text) |
450 | { | |
451 | SetInsertionPointEnd(); | |
452 | WriteText(text); | |
453 | } | |
454 | ||
455 | void wxTextCtrl::Clear() | |
456 | { | |
223d09f6 | 457 | SetWindowText(GetHwnd(), wxT("")); |
a1b82138 VZ |
458 | } |
459 | ||
460 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 461 | // Clipboard operations |
a1b82138 VZ |
462 | // ---------------------------------------------------------------------------- |
463 | ||
cd471848 | 464 | void wxTextCtrl::Copy() |
2bda0e17 | 465 | { |
e702ff0f JS |
466 | if (CanCopy()) |
467 | { | |
789295bf | 468 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
469 | SendMessage(hWnd, WM_COPY, 0, 0L); |
470 | } | |
2bda0e17 KB |
471 | } |
472 | ||
cd471848 | 473 | void wxTextCtrl::Cut() |
2bda0e17 | 474 | { |
e702ff0f JS |
475 | if (CanCut()) |
476 | { | |
789295bf | 477 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
478 | SendMessage(hWnd, WM_CUT, 0, 0L); |
479 | } | |
2bda0e17 KB |
480 | } |
481 | ||
cd471848 | 482 | void wxTextCtrl::Paste() |
2bda0e17 | 483 | { |
e702ff0f JS |
484 | if (CanPaste()) |
485 | { | |
789295bf | 486 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
487 | SendMessage(hWnd, WM_PASTE, 0, 0L); |
488 | } | |
2bda0e17 KB |
489 | } |
490 | ||
a1b82138 VZ |
491 | bool wxTextCtrl::CanCopy() const |
492 | { | |
493 | // Can copy if there's a selection | |
494 | long from, to; | |
495 | GetSelection(& from, & to); | |
dbf28859 | 496 | return (from != to) ; |
a1b82138 VZ |
497 | } |
498 | ||
499 | bool wxTextCtrl::CanCut() const | |
500 | { | |
501 | // Can cut if there's a selection | |
502 | long from, to; | |
503 | GetSelection(& from, & to); | |
dbf28859 | 504 | return (from != to) && (IsEditable()); |
a1b82138 VZ |
505 | } |
506 | ||
507 | bool wxTextCtrl::CanPaste() const | |
508 | { | |
509 | #if wxUSE_RICHEDIT | |
510 | if (m_isRich) | |
511 | { | |
512 | int dataFormat = 0; // 0 == any format | |
513 | return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0); | |
514 | } | |
515 | #endif | |
516 | if (!IsEditable()) | |
517 | return FALSE; | |
518 | ||
519 | // Standard edit control: check for straight text on clipboard | |
520 | bool isTextAvailable = FALSE; | |
521 | if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) | |
522 | { | |
523 | isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0); | |
524 | ::CloseClipboard(); | |
525 | } | |
526 | ||
527 | return isTextAvailable; | |
528 | } | |
529 | ||
530 | // ---------------------------------------------------------------------------- | |
531 | // Accessors | |
532 | // ---------------------------------------------------------------------------- | |
533 | ||
debe6624 | 534 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 535 | { |
a1b82138 VZ |
536 | HWND hWnd = GetHwnd(); |
537 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
2bda0e17 KB |
538 | } |
539 | ||
debe6624 | 540 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 541 | { |
a1b82138 | 542 | HWND hWnd = GetHwnd(); |
2bda0e17 | 543 | #ifdef __WIN32__ |
57c208c5 | 544 | #if wxUSE_RICHEDIT |
a1b82138 VZ |
545 | if ( m_isRich) |
546 | { | |
547 | CHARRANGE range; | |
548 | range.cpMin = pos; | |
549 | range.cpMax = pos; | |
550 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
551 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
552 | } | |
553 | else | |
554 | #endif // wxUSE_RICHEDIT | |
555 | { | |
556 | SendMessage(hWnd, EM_SETSEL, pos, pos); | |
557 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
558 | } | |
559 | #else // Win16 | |
560 | SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos)); | |
561 | #endif // Win32/16 | |
562 | ||
c4608a8a | 563 | static const wxChar *nothing = _T(""); |
a1b82138 | 564 | SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing); |
2bda0e17 KB |
565 | } |
566 | ||
cd471848 | 567 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 568 | { |
a1b82138 VZ |
569 | long pos = GetLastPosition(); |
570 | SetInsertionPoint(pos); | |
2bda0e17 KB |
571 | } |
572 | ||
cd471848 | 573 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 574 | { |
57c208c5 | 575 | #if wxUSE_RICHEDIT |
a1b82138 VZ |
576 | if (m_isRich) |
577 | { | |
578 | CHARRANGE range; | |
579 | range.cpMin = 0; | |
580 | range.cpMax = 0; | |
581 | SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); | |
582 | return range.cpMin; | |
583 | } | |
2bda0e17 KB |
584 | #endif |
585 | ||
a1b82138 VZ |
586 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
587 | return Pos & 0xFFFF; | |
2bda0e17 KB |
588 | } |
589 | ||
cd471848 | 590 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 591 | { |
789295bf | 592 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
593 | |
594 | // Will always return a number > 0 (according to docs) | |
595 | int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L); | |
596 | ||
597 | // This gets the char index for the _beginning_ of the last line | |
598 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L); | |
39136494 | 599 | |
2bda0e17 KB |
600 | // Get number of characters in the last line. We'll add this to the character |
601 | // index for the last line, 1st position. | |
602 | int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L); | |
603 | ||
604 | return (long)(charIndex + lineLength); | |
605 | } | |
606 | ||
a1b82138 VZ |
607 | // If the return values from and to are the same, there is no |
608 | // selection. | |
609 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
610 | { | |
611 | #if wxUSE_RICHEDIT | |
612 | if (m_isRich) | |
613 | { | |
614 | CHARRANGE charRange; | |
615 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange); | |
616 | ||
617 | *from = charRange.cpMin; | |
618 | *to = charRange.cpMax; | |
619 | ||
620 | return; | |
621 | } | |
622 | #endif | |
623 | DWORD dwStart, dwEnd; | |
624 | WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position | |
625 | LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position | |
626 | ||
627 | ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam); | |
628 | ||
629 | *from = dwStart; | |
630 | *to = dwEnd; | |
631 | } | |
632 | ||
633 | bool wxTextCtrl::IsEditable() const | |
634 | { | |
635 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
636 | ||
637 | return ((style & ES_READONLY) == 0); | |
638 | } | |
639 | ||
640 | // ---------------------------------------------------------------------------- | |
641 | // Editing | |
642 | // ---------------------------------------------------------------------------- | |
643 | ||
debe6624 | 644 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
2bda0e17 | 645 | { |
acbd13a3 | 646 | #if wxUSE_CLIPBOARD |
789295bf | 647 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
648 | long fromChar = from; |
649 | long toChar = to; | |
39136494 | 650 | |
2bda0e17 KB |
651 | // Set selection and remove it |
652 | #ifdef __WIN32__ | |
653 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
654 | #else | |
655 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
656 | #endif | |
657 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
658 | ||
659 | // Now replace with 'value', by pasting. | |
837e5743 | 660 | wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0); |
2bda0e17 KB |
661 | |
662 | // Paste into edit control | |
663 | SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
acbd13a3 JS |
664 | #else |
665 | wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0."); | |
666 | #endif | |
2bda0e17 KB |
667 | } |
668 | ||
debe6624 | 669 | void wxTextCtrl::Remove(long from, long to) |
2bda0e17 | 670 | { |
789295bf | 671 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
672 | long fromChar = from; |
673 | long toChar = to; | |
39136494 | 674 | |
2bda0e17 KB |
675 | // Cut all selected text |
676 | #ifdef __WIN32__ | |
677 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
678 | #else | |
679 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
680 | #endif | |
681 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
682 | } | |
683 | ||
debe6624 | 684 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 685 | { |
789295bf | 686 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
687 | long fromChar = from; |
688 | long toChar = to; | |
a1b82138 VZ |
689 | |
690 | // if from and to are both -1, it means (in wxWindows) that all text should | |
691 | // be selected. Translate into Windows convention | |
2bda0e17 KB |
692 | if ((from == -1) && (to == -1)) |
693 | { | |
694 | fromChar = 0; | |
695 | toChar = -1; | |
696 | } | |
39136494 | 697 | |
2bda0e17 KB |
698 | #ifdef __WIN32__ |
699 | SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar); | |
700 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
701 | #else | |
702 | // WPARAM is 0: selection is scrolled into view | |
703 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
704 | #endif | |
705 | } | |
706 | ||
707 | bool wxTextCtrl::LoadFile(const wxString& file) | |
708 | { | |
a1b82138 | 709 | if ( wxTextCtrlBase::LoadFile(file) ) |
cd471848 | 710 | { |
a1b82138 VZ |
711 | // update the size limit if needed |
712 | AdjustSpaceLimit(); | |
2bda0e17 | 713 | |
a1b82138 | 714 | return TRUE; |
2bda0e17 | 715 | } |
2bda0e17 | 716 | |
a1b82138 | 717 | return FALSE; |
2bda0e17 KB |
718 | } |
719 | ||
cd471848 | 720 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 721 | { |
789295bf | 722 | return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0); |
2bda0e17 KB |
723 | } |
724 | ||
725 | // Makes 'unmodified' | |
cd471848 | 726 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 727 | { |
a1b82138 | 728 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
729 | } |
730 | ||
cd471848 | 731 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 732 | { |
789295bf | 733 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
734 | } |
735 | ||
debe6624 | 736 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 737 | { |
789295bf | 738 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
739 | |
740 | // This gets the char index for the _beginning_ of this line | |
741 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0); | |
742 | return (long)(x + charIndex); | |
743 | } | |
744 | ||
0efe5ba7 | 745 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 746 | { |
789295bf | 747 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
748 | |
749 | // This gets the line number containing the character | |
0efe5ba7 VZ |
750 | int lineNo; |
751 | #if wxUSE_RICHEDIT | |
752 | if ( m_isRich ) | |
753 | { | |
754 | lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); | |
755 | } | |
756 | else | |
757 | #endif // wxUSE_RICHEDIT | |
758 | lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); | |
759 | ||
760 | if ( lineNo == -1 ) | |
761 | { | |
762 | // no such line | |
763 | return FALSE; | |
764 | } | |
765 | ||
2bda0e17 KB |
766 | // This gets the char index for the _beginning_ of this line |
767 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); | |
0efe5ba7 VZ |
768 | if ( charIndex == -1 ) |
769 | { | |
770 | return FALSE; | |
771 | } | |
772 | ||
2bda0e17 | 773 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 VZ |
774 | if ( x ) |
775 | *x = (long)(pos - charIndex); | |
776 | if ( y ) | |
777 | *y = (long)lineNo; | |
778 | ||
779 | return TRUE; | |
2bda0e17 KB |
780 | } |
781 | ||
debe6624 | 782 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 783 | { |
789295bf | 784 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
785 | |
786 | // To scroll to a position, we pass the number of lines and characters | |
787 | // to scroll *by*. This means that we need to: | |
788 | // (1) Find the line position of the current line. | |
789 | // (2) Find the line position of pos. | |
790 | // (3) Scroll by (pos - current). | |
791 | // For now, ignore the horizontal scrolling. | |
792 | ||
793 | // Is this where scrolling is relative to - the line containing the caret? | |
794 | // Or is the first visible line??? Try first visible line. | |
795 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
796 | ||
797 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
798 | ||
799 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
39136494 | 800 | |
2bda0e17 KB |
801 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
802 | ||
2bda0e17 | 803 | if (linesToScroll != 0) |
de4d7713 | 804 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
805 | } |
806 | ||
debe6624 | 807 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 KB |
808 | { |
809 | long charIndex = XYToPosition(0, lineNo); | |
4438caf4 | 810 | int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0); |
2bda0e17 KB |
811 | return len; |
812 | } | |
813 | ||
debe6624 | 814 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 815 | { |
a1b82138 | 816 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
488fe1fe | 817 | |
f6bcfd97 BP |
818 | // there must be at least enough place for the length WORD in the |
819 | // buffer | |
820 | len += sizeof(WORD); | |
4438caf4 | 821 | |
f6bcfd97 BP |
822 | wxString str; |
823 | wxChar *buf = str.GetWriteBuf(len); | |
824 | ||
825 | *(WORD *)buf = len; | |
826 | len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); | |
827 | buf[len] = 0; | |
4438caf4 | 828 | |
f6bcfd97 | 829 | str.UngetWriteBuf(len); |
4438caf4 VZ |
830 | |
831 | return str; | |
2bda0e17 KB |
832 | } |
833 | ||
a1b82138 | 834 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 835 | // Undo/redo |
a1b82138 VZ |
836 | // ---------------------------------------------------------------------------- |
837 | ||
ca8b28f2 JS |
838 | void wxTextCtrl::Undo() |
839 | { | |
840 | if (CanUndo()) | |
841 | { | |
789295bf | 842 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
843 | } |
844 | } | |
845 | ||
846 | void wxTextCtrl::Redo() | |
847 | { | |
848 | if (CanRedo()) | |
849 | { | |
850 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
789295bf | 851 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
852 | } |
853 | } | |
854 | ||
855 | bool wxTextCtrl::CanUndo() const | |
856 | { | |
789295bf | 857 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); |
ca8b28f2 JS |
858 | } |
859 | ||
860 | bool wxTextCtrl::CanRedo() const | |
861 | { | |
789295bf | 862 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); |
ca8b28f2 JS |
863 | } |
864 | ||
a1b82138 VZ |
865 | // ---------------------------------------------------------------------------- |
866 | // implemenation details | |
867 | // ---------------------------------------------------------------------------- | |
39136494 | 868 | |
2bda0e17 KB |
869 | void wxTextCtrl::Command(wxCommandEvent & event) |
870 | { | |
a1b82138 VZ |
871 | SetValue(event.GetString()); |
872 | ProcessCommand (event); | |
2bda0e17 KB |
873 | } |
874 | ||
875 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
876 | { | |
a1b82138 VZ |
877 | // By default, load the first file into the text window. |
878 | if (event.GetNumberOfFiles() > 0) | |
879 | { | |
880 | LoadFile(event.GetFiles()[0]); | |
881 | } | |
2bda0e17 KB |
882 | } |
883 | ||
2bda0e17 KB |
884 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
885 | { | |
42e69d6b | 886 | switch ( event.KeyCode() ) |
cd471848 | 887 | { |
cd471848 | 888 | case WXK_RETURN: |
39136494 | 889 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
890 | { |
891 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
bfbd6dc1 | 892 | InitCommandEvent(event); |
f6bcfd97 | 893 | event.SetString(GetValue()); |
cd471848 VZ |
894 | if ( GetEventHandler()->ProcessEvent(event) ) |
895 | return; | |
896 | } | |
5fb9fcfc VZ |
897 | //else: multiline controls need Enter for themselves |
898 | ||
899 | break; | |
4d91c1d1 | 900 | |
cd471848 | 901 | case WXK_TAB: |
319fefa9 VZ |
902 | // always produce navigation event - even if we process TAB |
903 | // ourselves the fact that we got here means that the user code | |
904 | // decided to skip processing of this TAB - probably to let it | |
905 | // do its default job. | |
cd471848 | 906 | { |
5fb9fcfc VZ |
907 | wxNavigationKeyEvent eventNav; |
908 | eventNav.SetDirection(!event.ShiftDown()); | |
8614c467 | 909 | eventNav.SetWindowChange(event.ControlDown()); |
5fb9fcfc | 910 | eventNav.SetEventObject(this); |
39136494 | 911 | |
d9506e77 | 912 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
913 | return; |
914 | } | |
341c92a8 | 915 | break; |
cd471848 | 916 | } |
39136494 | 917 | |
8614c467 | 918 | // no, we didn't process it |
42e69d6b | 919 | event.Skip(); |
2bda0e17 KB |
920 | } |
921 | ||
debe6624 | 922 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 923 | { |
789295bf VZ |
924 | switch (param) |
925 | { | |
926 | case EN_SETFOCUS: | |
927 | case EN_KILLFOCUS: | |
928 | { | |
929 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
930 | : wxEVT_SET_FOCUS, | |
931 | m_windowId); | |
932 | event.SetEventObject( this ); | |
933 | GetEventHandler()->ProcessEvent(event); | |
934 | } | |
935 | break; | |
ae29de83 | 936 | |
789295bf VZ |
937 | case EN_CHANGE: |
938 | { | |
939 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
bfbd6dc1 VZ |
940 | InitCommandEvent(event); |
941 | event.SetString(GetValue()); | |
942 | ProcessCommand(event); | |
789295bf VZ |
943 | } |
944 | break; | |
2bda0e17 | 945 | |
b12915c1 | 946 | case EN_MAXTEXT: |
789295bf VZ |
947 | // the text size limit has been hit - increase it |
948 | AdjustSpaceLimit(); | |
949 | break; | |
950 | ||
951 | // the other notification messages are not processed | |
952 | case EN_UPDATE: | |
b12915c1 | 953 | case EN_ERRSPACE: |
789295bf VZ |
954 | case EN_HSCROLL: |
955 | case EN_VSCROLL: | |
f6bcfd97 | 956 | return FALSE; |
789295bf VZ |
957 | default: |
958 | return FALSE; | |
959 | } | |
960 | ||
961 | // processed | |
962 | return TRUE; | |
2bda0e17 KB |
963 | } |
964 | ||
f6bcfd97 BP |
965 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
966 | WXUINT message, | |
967 | WXWPARAM wParam, | |
968 | WXLPARAM lParam) | |
969 | { | |
970 | #if wxUSE_CTL3D | |
971 | if ( m_useCtl3D ) | |
972 | { | |
973 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
974 | return (WXHBRUSH) hbrush; | |
975 | } | |
976 | #endif // wxUSE_CTL3D | |
977 | ||
978 | HDC hdc = (HDC)pDC; | |
979 | if (GetParent()->GetTransparentBackground()) | |
980 | SetBkMode(hdc, TRANSPARENT); | |
981 | else | |
982 | SetBkMode(hdc, OPAQUE); | |
983 | ||
984 | wxColour colBack = GetBackgroundColour(); | |
985 | ||
986 | if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0) | |
987 | colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE); | |
988 | ||
989 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
990 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
991 | ||
992 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
993 | ||
994 | return (WXHBRUSH)brush->GetResourceHandle(); | |
995 | } | |
996 | ||
997 | // In WIN16, need to override normal erasing because | |
998 | // Ctl3D doesn't use the wxWindows background colour. | |
999 | #ifdef __WIN16__ | |
1000 | void wxTextCtrl::OnEraseBackground(wxEraseEvent& event) | |
1001 | { | |
1002 | wxColour col(m_backgroundColour); | |
1003 | ||
1004 | #if wxUSE_CTL3D | |
1005 | if (m_useCtl3D) | |
1006 | col = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW); | |
1007 | #endif | |
1008 | ||
1009 | RECT rect; | |
1010 | ::GetClientRect(GetHwnd(), &rect); | |
1011 | ||
1012 | COLORREF ref = PALETTERGB(col.Red(), | |
1013 | col.Green(), | |
1014 | col.Blue()); | |
1015 | HBRUSH hBrush = ::CreateSolidBrush(ref); | |
1016 | if ( !hBrush ) | |
1017 | wxLogLastError(wxT("CreateSolidBrush")); | |
1018 | ||
1019 | HDC hdc = (HDC)event.GetDC()->GetHDC(); | |
1020 | ||
1021 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
1022 | ||
1023 | ::FillRect(hdc, &rect, hBrush); | |
1024 | ::DeleteObject(hBrush); | |
1025 | ::SetMapMode(hdc, mode); | |
1026 | ||
1027 | } | |
1028 | #endif | |
1029 | ||
789295bf VZ |
1030 | void wxTextCtrl::AdjustSpaceLimit() |
1031 | { | |
25889d3c | 1032 | #ifndef __WIN16__ |
789295bf VZ |
1033 | unsigned int len = ::GetWindowTextLength(GetHwnd()), |
1034 | limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); | |
17d8ee1c | 1035 | if ( len >= limit ) |
789295bf VZ |
1036 | { |
1037 | limit = len + 0x8000; // 32Kb | |
1038 | ||
5ea105e0 | 1039 | #if wxUSE_RICHEDIT |
b12915c1 VZ |
1040 | if ( m_isRich ) |
1041 | { | |
1042 | // as a nice side effect, this also allows passing limit > 64Kb | |
1043 | ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit); | |
1044 | } | |
789295bf | 1045 | else |
b12915c1 VZ |
1046 | #endif // wxUSE_RICHEDIT |
1047 | { | |
1048 | if ( limit > 0xffff ) | |
1049 | { | |
1050 | // this will set it to a platform-dependent maximum (much more | |
1051 | // than 64Kb under NT) | |
1052 | limit = 0; | |
1053 | } | |
1054 | ||
789295bf | 1055 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); |
b12915c1 | 1056 | } |
789295bf | 1057 | } |
b12915c1 | 1058 | #endif // !Win16 |
789295bf | 1059 | } |
2bda0e17 | 1060 | |
a1b82138 | 1061 | bool wxTextCtrl::AcceptsFocus() const |
2bda0e17 | 1062 | { |
a1b82138 VZ |
1063 | // we don't want focus if we can't be edited |
1064 | return IsEditable() && wxControl::AcceptsFocus(); | |
1065 | } | |
c085e333 | 1066 | |
f68586e5 | 1067 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
1068 | { |
1069 | int cx, cy; | |
1070 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
1071 | ||
1072 | int wText = DEFAULT_ITEM_WIDTH; | |
1073 | ||
1074 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
1075 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1076 | { | |
1077 | hText *= wxMin(GetNumberOfLines(), 5); | |
1078 | } | |
1079 | //else: for single line control everything is ok | |
1080 | ||
1081 | return wxSize(wText, hText); | |
2bda0e17 | 1082 | } |
a1b82138 VZ |
1083 | |
1084 | // ---------------------------------------------------------------------------- | |
1085 | // standard handlers for standard edit menu events | |
1086 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1087 | |
bfbd6dc1 | 1088 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1089 | { |
1090 | Cut(); | |
1091 | } | |
1092 | ||
bfbd6dc1 | 1093 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1094 | { |
1095 | Copy(); | |
1096 | } | |
1097 | ||
bfbd6dc1 | 1098 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1099 | { |
1100 | Paste(); | |
1101 | } | |
1102 | ||
bfbd6dc1 | 1103 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1104 | { |
1105 | Undo(); | |
1106 | } | |
1107 | ||
bfbd6dc1 | 1108 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1109 | { |
1110 | Redo(); | |
1111 | } | |
1112 | ||
1113 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1114 | { | |
1115 | event.Enable( CanCut() ); | |
1116 | } | |
1117 | ||
1118 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1119 | { | |
1120 | event.Enable( CanCopy() ); | |
1121 | } | |
1122 | ||
1123 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1124 | { | |
1125 | event.Enable( CanPaste() ); | |
1126 | } | |
1127 | ||
1128 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1129 | { | |
1130 | event.Enable( CanUndo() ); | |
1131 | } | |
1132 | ||
1133 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1134 | { | |
1135 | event.Enable( CanRedo() ); | |
1136 | } | |
1137 | ||
f6bcfd97 BP |
1138 | // ---------------------------------------------------------------------------- |
1139 | // colour setting for the rich edit controls | |
1140 | // ---------------------------------------------------------------------------- | |
1141 | ||
1142 | #if wxUSE_RICHEDIT | |
1143 | ||
1144 | // Watcom C++ doesn't define this | |
1145 | #ifndef SCF_ALL | |
1146 | #define SCF_ALL 0x0004 | |
1147 | #endif | |
1148 | ||
1149 | bool wxTextCtrl::SetBackgroundColour(const wxColour& colour) | |
1150 | { | |
1151 | if ( !wxTextCtrlBase::SetBackgroundColour(colour) ) | |
1152 | { | |
1153 | // colour didn't really change | |
1154 | return FALSE; | |
1155 | } | |
1156 | ||
1157 | if ( IsRich() ) | |
1158 | { | |
1159 | // rich edit doesn't use WM_CTLCOLOR, hence we need to send | |
1160 | // EM_SETBKGNDCOLOR additionally | |
1161 | ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour)); | |
1162 | } | |
1163 | ||
1164 | return TRUE; | |
1165 | } | |
1166 | ||
1167 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
1168 | { | |
1169 | if ( !wxTextCtrlBase::SetForegroundColour(colour) ) | |
1170 | { | |
1171 | // colour didn't really change | |
1172 | return FALSE; | |
1173 | } | |
1174 | ||
1175 | if ( IsRich() ) | |
1176 | { | |
1177 | // change the colour of everything | |
1178 | CHARFORMAT cf; | |
1179 | wxZeroMemory(cf); | |
1180 | cf.cbSize = sizeof(cf); | |
1181 | cf.dwMask = CFM_COLOR; | |
1182 | cf.crTextColor = wxColourToRGB(colour); | |
1183 | ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); | |
1184 | } | |
1185 | ||
1186 | return TRUE; | |
1187 | } | |
1188 | ||
1189 | #endif // wxUSE_RICHEDIT | |
1190 | ||
b12915c1 VZ |
1191 | // ---------------------------------------------------------------------------- |
1192 | // wxRichEditModule | |
1193 | // ---------------------------------------------------------------------------- | |
1194 | ||
1195 | #if wxUSE_RICHEDIT | |
1196 | ||
1197 | bool wxRichEditModule::OnInit() | |
1198 | { | |
1199 | // don't do anything - we will load it when needed | |
1200 | return TRUE; | |
1201 | } | |
1202 | ||
1203 | void wxRichEditModule::OnExit() | |
1204 | { | |
1205 | if ( ms_hRichEdit ) | |
1206 | { | |
1207 | FreeLibrary(ms_hRichEdit); | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | /* static */ | |
1212 | bool wxRichEditModule::Load(int version) | |
1213 | { | |
1214 | wxCHECK_MSG( version >= 1 && version <= 3, FALSE, | |
1215 | _T("incorrect richedit control version requested") ); | |
1216 | ||
1217 | if ( version <= ms_verRichEdit ) | |
1218 | { | |
1219 | // we've already got this or better | |
1220 | return TRUE; | |
1221 | } | |
1222 | ||
1223 | if ( ms_hRichEdit ) | |
1224 | { | |
1225 | ::FreeLibrary(ms_hRichEdit); | |
1226 | } | |
1227 | ||
1228 | // always try load riched20.dll first - like this we won't have to reload | |
1229 | // it later if we're first asked for RE 1 and then for RE 2 or 3 | |
1230 | wxString dllname = _T("riched20.dll"); | |
1231 | ms_hRichEdit = ::LoadLibrary(dllname); | |
1232 | ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2 | |
1233 | ||
1234 | if ( !ms_hRichEdit && (version == 1) ) | |
1235 | { | |
1236 | // fall back to RE 1 | |
1237 | dllname = _T("riched32.dll"); | |
1238 | ms_hRichEdit = ::LoadLibrary(dllname); | |
1239 | ms_verRichEdit = 1; | |
1240 | } | |
1241 | ||
1242 | if ( !ms_hRichEdit ) | |
1243 | { | |
1244 | wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str()); | |
1245 | ||
1246 | ms_verRichEdit = -1; | |
1247 | ||
1248 | return FALSE; | |
1249 | } | |
1250 | ||
1251 | return TRUE; | |
1252 | } | |
1253 | ||
1254 | #endif // wxUSE_RICHEDIT | |
1255 |