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