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