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