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