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