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