]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a1b82138 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
2bda0e17 | 16 | #ifdef __GNUG__ |
a1b82138 | 17 | #pragma implementation "textctrl.h" |
2bda0e17 KB |
18 | #endif |
19 | ||
a1b82138 VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
2bda0e17 KB |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
a1b82138 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_TEXTCTRL |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
a1b82138 VZ |
34 | #include "wx/textctrl.h" |
35 | #include "wx/settings.h" | |
36 | #include "wx/brush.h" | |
37 | #include "wx/utils.h" | |
381dd4bf | 38 | #include "wx/intl.h" |
a1b82138 | 39 | #include "wx/log.h" |
381dd4bf | 40 | #include "wx/app.h" |
2bda0e17 KB |
41 | #endif |
42 | ||
f6bcfd97 BP |
43 | #include "wx/module.h" |
44 | ||
47d67540 | 45 | #if wxUSE_CLIPBOARD |
a1b82138 | 46 | #include "wx/clipbrd.h" |
2bda0e17 KB |
47 | #endif |
48 | ||
a1b82138 VZ |
49 | #include "wx/textfile.h" |
50 | ||
51 | #include <windowsx.h> | |
52 | ||
2bda0e17 KB |
53 | #include "wx/msw/private.h" |
54 | ||
a1b82138 | 55 | #include <string.h> |
2bda0e17 | 56 | #include <stdlib.h> |
a1b82138 | 57 | #include <sys/types.h> |
fbc535ff | 58 | |
ae090fdb | 59 | #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)) |
cd471848 | 60 | #include <richedit.h> |
2bda0e17 KB |
61 | #endif |
62 | ||
e80591b9 VZ |
63 | // old mingw32 doesn't define this |
64 | #ifndef CFM_CHARSET | |
65 | #define CFM_CHARSET 0x08000000 | |
66 | #endif // CFM_CHARSET | |
67 | ||
784164e1 VZ |
68 | #ifndef CFM_BACKCOLOR |
69 | #define CFM_BACKCOLOR 0x04000000 | |
70 | #endif | |
71 | ||
733e8cf3 VZ |
72 | // cygwin does not have these defined for richedit |
73 | #ifndef ENM_LINK | |
74 | #define ENM_LINK 0x04000000 | |
75 | #endif | |
76 | ||
77 | #ifndef EM_AUTOURLDETECT | |
78 | #define EM_AUTOURLDETECT (WM_USER + 91) | |
79 | #endif | |
80 | ||
81 | #ifndef EN_LINK | |
82 | #define EN_LINK 0x070b | |
83 | ||
84 | typedef struct _enlink | |
85 | { | |
86 | NMHDR nmhdr; | |
87 | UINT msg; | |
88 | WPARAM wParam; | |
89 | LPARAM lParam; | |
90 | CHARRANGE chrg; | |
91 | } ENLINK; | |
92 | #endif // ENLINK | |
93 | ||
7a25a27c VZ |
94 | #ifndef SF_UNICODE |
95 | #define SF_UNICODE 0x0010 | |
96 | #endif | |
97 | ||
a5aa8086 VZ |
98 | // Watcom C++ doesn't define this |
99 | #ifndef SCF_ALL | |
100 | #define SCF_ALL 0x0004 | |
101 | #endif | |
102 | ||
aac7e7fe VZ |
103 | // ---------------------------------------------------------------------------- |
104 | // private functions | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | #if wxUSE_RICHEDIT | |
108 | ||
109 | DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb); | |
110 | ||
111 | #endif // wxUSE_RICHEDIT | |
112 | ||
b12915c1 VZ |
113 | // ---------------------------------------------------------------------------- |
114 | // private classes | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
117 | #if wxUSE_RICHEDIT | |
118 | ||
a5aa8086 | 119 | // this module initializes RichEdit DLL(s) if needed |
b12915c1 VZ |
120 | class wxRichEditModule : public wxModule |
121 | { | |
122 | public: | |
123 | virtual bool OnInit(); | |
124 | virtual void OnExit(); | |
125 | ||
b12915c1 VZ |
126 | // load the richedit DLL of at least of required version |
127 | static bool Load(int version = 1); | |
128 | ||
129 | private: | |
a5aa8086 VZ |
130 | // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs |
131 | static HINSTANCE ms_hRichEdit[2]; | |
b12915c1 VZ |
132 | |
133 | DECLARE_DYNAMIC_CLASS(wxRichEditModule) | |
134 | }; | |
135 | ||
a5aa8086 | 136 | HINSTANCE wxRichEditModule::ms_hRichEdit[2] = { NULL, NULL }; |
b12915c1 VZ |
137 | |
138 | IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule) | |
139 | ||
140 | #endif // wxUSE_RICHEDIT | |
e702ff0f | 141 | |
a1b82138 VZ |
142 | // ---------------------------------------------------------------------------- |
143 | // event tables and other macros | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
2bda0e17 KB |
146 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
147 | ||
148 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
a1b82138 VZ |
149 | EVT_CHAR(wxTextCtrl::OnChar) |
150 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
151 | ||
152 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
153 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
154 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
155 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
156 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
157 | ||
158 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
159 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
160 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
161 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
162 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
f6bcfd97 BP |
163 | #ifdef __WIN16__ |
164 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
165 | #endif | |
2bda0e17 | 166 | END_EVENT_TABLE() |
e702ff0f | 167 | |
a1b82138 VZ |
168 | // ============================================================================ |
169 | // implementation | |
170 | // ============================================================================ | |
171 | ||
172 | // ---------------------------------------------------------------------------- | |
173 | // creation | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
aac7e7fe | 176 | void wxTextCtrl::Init() |
2bda0e17 | 177 | { |
cd471848 | 178 | #if wxUSE_RICHEDIT |
aac7e7fe | 179 | m_verRichEdit = 0; |
cd471848 | 180 | #endif |
2bda0e17 KB |
181 | } |
182 | ||
debe6624 | 183 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
184 | const wxString& value, |
185 | const wxPoint& pos, | |
a1b82138 VZ |
186 | const wxSize& size, |
187 | long style, | |
c085e333 VZ |
188 | const wxValidator& validator, |
189 | const wxString& name) | |
2bda0e17 | 190 | { |
a1b82138 | 191 | // base initialization |
8d99be5f | 192 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) |
a1b82138 | 193 | return FALSE; |
2bda0e17 | 194 | |
a1b82138 VZ |
195 | if ( parent ) |
196 | parent->AddChild(this); | |
2bda0e17 | 197 | |
a1b82138 VZ |
198 | // translate wxWin style flags to MSW ones, checking for consistency while |
199 | // doing it | |
7da982c4 | 200 | long msStyle = ES_LEFT | WS_TABSTOP; |
b0766406 JS |
201 | |
202 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
203 | msStyle |= WS_CLIPSIBLINGS; | |
204 | ||
a1b82138 VZ |
205 | if ( m_windowStyle & wxTE_MULTILINE ) |
206 | { | |
207 | wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER), | |
f6bcfd97 | 208 | wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); |
5fb9fcfc | 209 | |
b70ababc JS |
210 | msStyle |= ES_MULTILINE | ES_WANTRETURN; |
211 | if ((m_windowStyle & wxTE_NO_VSCROLL) == 0) | |
212 | msStyle |= WS_VSCROLL; | |
a1b82138 VZ |
213 | m_windowStyle |= wxTE_PROCESS_ENTER; |
214 | } | |
5a8f04e3 VZ |
215 | else // !multiline |
216 | { | |
217 | // there is really no reason to not have this style for single line | |
218 | // text controls | |
a1b82138 | 219 | msStyle |= ES_AUTOHSCROLL; |
5a8f04e3 | 220 | } |
2bda0e17 | 221 | |
5a8f04e3 VZ |
222 | if ( m_windowStyle & wxHSCROLL ) |
223 | msStyle |= WS_HSCROLL | ES_AUTOHSCROLL; | |
2c738dd8 | 224 | |
5a8f04e3 | 225 | if ( m_windowStyle & wxTE_READONLY ) |
a1b82138 | 226 | msStyle |= ES_READONLY; |
2bda0e17 | 227 | |
5a8f04e3 | 228 | if ( m_windowStyle & wxTE_PASSWORD ) |
a1b82138 | 229 | msStyle |= ES_PASSWORD; |
2bda0e17 | 230 | |
5a8f04e3 VZ |
231 | if ( m_windowStyle & wxTE_AUTO_SCROLL ) |
232 | msStyle |= ES_AUTOHSCROLL; | |
cfdd3a98 | 233 | |
5a8f04e3 VZ |
234 | if ( m_windowStyle & wxTE_NOHIDESEL ) |
235 | msStyle |= ES_NOHIDESEL; | |
cfdd3a98 | 236 | |
101f488c VZ |
237 | // we always want the characters and the arrows |
238 | m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
239 | ||
240 | // we may have several different cases: | |
241 | // 1. normal case: both TAB and ENTER are used for dialog navigation | |
242 | // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next | |
243 | // control in the dialog | |
244 | // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation | |
245 | // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to | |
246 | // the next control | |
247 | if ( m_windowStyle & wxTE_PROCESS_ENTER ) | |
248 | m_lDlgCode |= DLGC_WANTMESSAGE; | |
249 | if ( m_windowStyle & wxTE_PROCESS_TAB ) | |
250 | m_lDlgCode |= DLGC_WANTTAB; | |
251 | ||
a1b82138 | 252 | // do create the control - either an EDIT or RICHEDIT |
b12915c1 | 253 | wxString windowClass = wxT("EDIT"); |
cd471848 | 254 | |
57c208c5 | 255 | #if wxUSE_RICHEDIT |
a5aa8086 VZ |
256 | if ( m_windowStyle & wxTE_AUTO_URL ) |
257 | { | |
258 | // automatic URL detection only works in RichEdit 2.0+ | |
259 | m_windowStyle |= wxTE_RICH2; | |
260 | } | |
261 | ||
262 | if ( m_windowStyle & wxTE_RICH2 ) | |
263 | { | |
264 | // using richedit 2.0 implies using wxTE_RICH | |
265 | m_windowStyle |= wxTE_RICH; | |
266 | } | |
267 | ||
268 | // we need to load the richedit DLL before creating the rich edit control | |
c49245f8 | 269 | if ( m_windowStyle & wxTE_RICH ) |
a1b82138 | 270 | { |
a5aa8086 VZ |
271 | static bool s_errorGiven = FALSE;// MT-FIXME |
272 | ||
273 | // Which version do we need? Use 1.0 by default because it is much more | |
274 | // like the the standard EDIT or 2.0 if explicitly requested, but use | |
275 | // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all | |
276 | // | |
277 | // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0 | |
278 | // (and thus EDIT) much better than RichEdit 2.0 so we probably | |
279 | // should use 3.0 if available as it is the best of both worlds - | |
280 | // but as I can't test it right now I don't do it (VZ) | |
281 | #if wxUSE_UNICODE | |
282 | const int verRichEdit = 2; | |
283 | #else // !wxUSE_UNICODE | |
284 | int verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1; | |
285 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
0d0512bd VZ |
286 | |
287 | // only give the error msg once if the DLL can't be loaded | |
288 | if ( !s_errorGiven ) | |
289 | { | |
a5aa8086 VZ |
290 | // try to load the RichEdit DLL (will do nothing if already done) |
291 | if ( !wxRichEditModule::Load(verRichEdit) ) | |
0d0512bd | 292 | { |
a5aa8086 VZ |
293 | #if !wxUSE_UNICODE |
294 | // try another version? | |
295 | verRichEdit = 3 - verRichEdit; // 1 <-> 2 | |
296 | ||
297 | if ( !wxRichEditModule::Load(verRichEdit) ) | |
298 | #endif // wxUSE_UNICODE | |
299 | { | |
300 | wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll")); | |
0d0512bd | 301 | |
a5aa8086 VZ |
302 | s_errorGiven = TRUE; |
303 | } | |
0d0512bd VZ |
304 | } |
305 | } | |
306 | ||
a5aa8086 | 307 | // have we managed to load any richedit version? |
aac7e7fe | 308 | if ( !s_errorGiven ) |
0d0512bd VZ |
309 | { |
310 | msStyle |= ES_AUTOVSCROLL; | |
aac7e7fe | 311 | |
a5aa8086 | 312 | m_verRichEdit = verRichEdit; |
aac7e7fe | 313 | if ( m_verRichEdit == 1 ) |
b12915c1 VZ |
314 | { |
315 | windowClass = wxT("RICHEDIT"); | |
316 | } | |
317 | else | |
318 | { | |
319 | #ifndef RICHEDIT_CLASS | |
320 | wxString RICHEDIT_CLASS; | |
aac7e7fe | 321 | RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), m_verRichEdit); |
5fb2f4ba | 322 | #if wxUSE_UNICODE |
b12915c1 VZ |
323 | RICHEDIT_CLASS += _T('W'); |
324 | #else // ANSI | |
325 | RICHEDIT_CLASS += _T('A'); | |
326 | #endif // Unicode/ANSI | |
327 | #endif // !RICHEDIT_CLASS | |
328 | ||
329 | windowClass = RICHEDIT_CLASS; | |
330 | } | |
0d0512bd | 331 | } |
a1b82138 | 332 | } |
b12915c1 | 333 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 334 | |
c8b204e6 VZ |
335 | // we need to turn '\n's into "\r\n"s for the multiline controls |
336 | wxString valueWin; | |
337 | if ( m_windowStyle & wxTE_MULTILINE ) | |
338 | { | |
339 | valueWin = wxTextFile::Translate(value, wxTextFileType_Dos); | |
340 | } | |
341 | else // single line | |
342 | { | |
343 | valueWin = value; | |
344 | } | |
345 | ||
346 | if ( !MSWCreateControl(windowClass, msStyle, pos, size, valueWin) ) | |
7da982c4 | 347 | return FALSE; |
2bda0e17 | 348 | |
a756f210 | 349 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
86f14582 | 350 | |
57c208c5 | 351 | #if wxUSE_RICHEDIT |
aac7e7fe | 352 | if ( IsRich() ) |
a1b82138 | 353 | { |
c57e3339 VZ |
354 | // have to enable events manually |
355 | LPARAM mask = ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE; | |
356 | ||
357 | if ( m_windowStyle & wxTE_AUTO_URL ) | |
358 | { | |
359 | mask |= ENM_LINK; | |
360 | ||
361 | ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0); | |
362 | } | |
363 | ||
364 | ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask); | |
a1b82138 | 365 | } |
c57e3339 | 366 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 367 | |
a1b82138 | 368 | return TRUE; |
2bda0e17 KB |
369 | } |
370 | ||
371 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 372 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 373 | { |
aac7e7fe | 374 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 375 | |
aac7e7fe VZ |
376 | HWND hWnd = GetHwnd(); |
377 | long style = ::GetWindowLong(hWnd, GWL_STYLE); | |
2bda0e17 | 378 | |
aac7e7fe | 379 | // retrieve the style to see whether this is an edit or richedit ctrl |
cd471848 | 380 | #if wxUSE_RICHEDIT |
aac7e7fe | 381 | wxString classname = wxGetWindowClass(GetHWND()); |
2bda0e17 | 382 | |
aac7e7fe VZ |
383 | if ( classname.IsSameAs(_T("EDIT"), FALSE /* no case */) ) |
384 | { | |
385 | m_verRichEdit = 0; | |
386 | } | |
387 | else // rich edit? | |
388 | { | |
389 | wxChar c; | |
390 | if ( wxSscanf(classname, _T("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 ) | |
391 | { | |
392 | wxLogDebug(_T("Unknown edit control '%s'."), classname.c_str()); | |
2bda0e17 | 393 | |
aac7e7fe VZ |
394 | m_verRichEdit = 0; |
395 | } | |
396 | } | |
a1b82138 | 397 | #endif // wxUSE_RICHEDIT |
c085e333 | 398 | |
aac7e7fe VZ |
399 | if (style & ES_MULTILINE) |
400 | m_windowStyle |= wxTE_MULTILINE; | |
401 | if (style & ES_PASSWORD) | |
402 | m_windowStyle |= wxTE_PASSWORD; | |
403 | if (style & ES_READONLY) | |
404 | m_windowStyle |= wxTE_READONLY; | |
405 | if (style & ES_WANTRETURN) | |
406 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
2bda0e17 KB |
407 | } |
408 | ||
a1b82138 VZ |
409 | // ---------------------------------------------------------------------------- |
410 | // set/get the controls text | |
411 | // ---------------------------------------------------------------------------- | |
412 | ||
cd471848 | 413 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 414 | { |
a5aa8086 VZ |
415 | // range 0..-1 is special for GetRange() and means to retrieve all text |
416 | return GetRange(0, -1); | |
417 | } | |
418 | ||
419 | wxString wxTextCtrl::GetRange(long from, long to) const | |
420 | { | |
421 | wxString str; | |
422 | ||
423 | if ( from >= to && to != -1 ) | |
424 | { | |
425 | // nothing to retrieve | |
426 | return str; | |
427 | } | |
428 | ||
b12915c1 | 429 | #if wxUSE_RICHEDIT |
aac7e7fe | 430 | if ( IsRich() ) |
b12915c1 | 431 | { |
3988b155 | 432 | int len = GetWindowTextLength(GetHwnd()); |
a5aa8086 | 433 | if ( len > from ) |
3988b155 VZ |
434 | { |
435 | // alloc one extra WORD as needed by the control | |
436 | wxChar *p = str.GetWriteBuf(++len); | |
b12915c1 | 437 | |
3988b155 | 438 | TEXTRANGE textRange; |
a5aa8086 VZ |
439 | textRange.chrg.cpMin = from; |
440 | textRange.chrg.cpMax = to == -1 ? len : to; | |
3988b155 | 441 | textRange.lpstrText = p; |
b12915c1 | 442 | |
3988b155 | 443 | (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange); |
b12915c1 | 444 | |
a5aa8086 | 445 | if ( m_verRichEdit > 1 ) |
3988b155 | 446 | { |
a5aa8086 VZ |
447 | // RichEdit 2.0 uses just CR ('\r') for the newlines which is |
448 | // neither Unix nor Windows style - convert it to something | |
449 | // reasonable | |
450 | for ( ; *p; p++ ) | |
451 | { | |
452 | if ( *p == _T('\r') ) | |
453 | *p = _T('\n'); | |
454 | } | |
3988b155 VZ |
455 | } |
456 | ||
457 | str.UngetWriteBuf(); | |
a5aa8086 VZ |
458 | |
459 | if ( m_verRichEdit == 1 ) | |
460 | { | |
461 | // convert to the canonical form - see comment below | |
462 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
463 | } | |
3988b155 VZ |
464 | } |
465 | //else: no text at all, leave the string empty | |
b12915c1 | 466 | } |
a5aa8086 | 467 | else |
b12915c1 | 468 | #endif // wxUSE_RICHEDIT |
a5aa8086 VZ |
469 | { |
470 | // retrieve all text | |
471 | str = wxGetWindowText(GetHWND()); | |
b12915c1 | 472 | |
a5aa8086 VZ |
473 | // need only a range? |
474 | if ( from < to ) | |
475 | { | |
476 | str = str.Mid(from, to - from); | |
477 | } | |
478 | ||
479 | // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the | |
480 | // canonical one (same one as above) for consistency with the other kinds | |
481 | // of controls and, more importantly, with the other ports | |
482 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
483 | } | |
b12915c1 | 484 | |
a5aa8086 | 485 | return str; |
2bda0e17 KB |
486 | } |
487 | ||
488 | void wxTextCtrl::SetValue(const wxString& value) | |
489 | { | |
b12915c1 VZ |
490 | // if the text is long enough, it's faster to just set it instead of first |
491 | // comparing it with the old one (chances are that it will be different | |
492 | // anyhow, this comparison is there to avoid flicker for small single-line | |
493 | // edit controls mostly) | |
494 | if ( (value.length() > 0x400) || (value != GetValue()) ) | |
07cf98cb | 495 | { |
79d26b32 | 496 | DoWriteText(value, FALSE /* not selection only */); |
af01f1ba | 497 | |
104d7404 VZ |
498 | // mark the control as being not dirty - we changed its text, not the |
499 | // user | |
500 | DiscardEdits(); | |
501 | ||
af01f1ba VZ |
502 | // for compatibility, don't move the cursor when doing SetValue() |
503 | SetInsertionPoint(0); | |
aac7e7fe VZ |
504 | } |
505 | } | |
a1b82138 | 506 | |
0b8e5844 | 507 | #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU) |
f6bcfd97 | 508 | |
aac7e7fe VZ |
509 | DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb) |
510 | { | |
511 | *pcb = 0; | |
f6bcfd97 | 512 | |
aac7e7fe VZ |
513 | wchar_t *wbuf = (wchar_t *)buf; |
514 | const wchar_t *wpc = *(const wchar_t **)dwCookie; | |
515 | while ( cb && *wpc ) | |
516 | { | |
517 | *wbuf++ = *wpc++; | |
518 | ||
519 | cb -= sizeof(wchar_t); | |
520 | (*pcb) += sizeof(wchar_t); | |
07cf98cb | 521 | } |
aac7e7fe VZ |
522 | |
523 | *(const wchar_t **)dwCookie = wpc; | |
524 | ||
525 | return 0; | |
2bda0e17 KB |
526 | } |
527 | ||
aac7e7fe VZ |
528 | extern long wxEncodingToCodepage(wxFontEncoding encoding); // from strconv.cpp |
529 | ||
0b8e5844 | 530 | #if wxUSE_UNICODE_MSLU |
79d26b32 VZ |
531 | bool wxTextCtrl::StreamIn(const wxString& value, |
532 | wxFontEncoding WXUNUSED(encoding), | |
533 | bool selectionOnly) | |
0b8e5844 VS |
534 | { |
535 | const wchar_t *wpc = value.c_str(); | |
79d26b32 VZ |
536 | #else // !wxUSE_UNICODE_MSLU |
537 | bool wxTextCtrl::StreamIn(const wxString& value, | |
538 | wxFontEncoding encoding, | |
539 | bool selectionOnly) | |
aac7e7fe VZ |
540 | { |
541 | // we have to use EM_STREAMIN to force richedit control 2.0+ to show any | |
542 | // text in the non default charset - otherwise it thinks it knows better | |
543 | // than we do and always shows it in the default one | |
544 | ||
545 | // first get the Windows code page for this encoding | |
546 | long codepage = wxEncodingToCodepage(encoding); | |
547 | if ( codepage == -1 ) | |
548 | { | |
549 | // unknown encoding | |
550 | return FALSE; | |
551 | } | |
552 | ||
553 | // next translate to Unicode using this code page | |
554 | int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0); | |
855d6be7 VZ |
555 | |
556 | #if wxUSE_WCHAR_T | |
aac7e7fe | 557 | wxWCharBuffer wchBuf(len); |
855d6be7 VZ |
558 | #else |
559 | wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t)); | |
560 | #endif | |
561 | ||
aac7e7fe | 562 | if ( !::MultiByteToWideChar(codepage, 0, value, -1, |
855d6be7 | 563 | (wchar_t *)(const wchar_t *)wchBuf, len) ) |
aac7e7fe VZ |
564 | { |
565 | wxLogLastError(_T("MultiByteToWideChar")); | |
566 | } | |
567 | ||
568 | // finally, stream it in the control | |
569 | const wchar_t *wpc = wchBuf; | |
0b8e5844 | 570 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe VZ |
571 | |
572 | EDITSTREAM eds; | |
573 | wxZeroMemory(eds); | |
574 | eds.dwCookie = (DWORD)&wpc; | |
7a25a27c VZ |
575 | // the cast below is needed for broken (very) old mingw32 headers |
576 | eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn; | |
92209a39 | 577 | |
aac7e7fe | 578 | if ( !::SendMessage(GetHwnd(), EM_STREAMIN, |
79d26b32 VZ |
579 | SF_TEXT | |
580 | SF_UNICODE | | |
581 | (selectionOnly ? SFF_SELECTION : 0), | |
aac7e7fe VZ |
582 | (LPARAM)&eds) || eds.dwError ) |
583 | { | |
584 | wxLogLastError(_T("EM_STREAMIN")); | |
aac7e7fe VZ |
585 | } |
586 | ||
855d6be7 VZ |
587 | #if !wxUSE_WCHAR_T |
588 | free(wchBuf); | |
589 | #endif // !wxUSE_WCHAR_T | |
590 | ||
aac7e7fe VZ |
591 | return TRUE; |
592 | } | |
593 | ||
594 | #endif // wxUSE_RICHEDIT | |
595 | ||
a1b82138 | 596 | void wxTextCtrl::WriteText(const wxString& value) |
79d26b32 VZ |
597 | { |
598 | DoWriteText(value); | |
599 | } | |
600 | ||
601 | void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly) | |
2bda0e17 | 602 | { |
a5aa8086 VZ |
603 | wxString valueDos; |
604 | if ( m_windowStyle & wxTE_MULTILINE ) | |
605 | valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
606 | else | |
607 | valueDos = value; | |
2bda0e17 | 608 | |
4bc1afd5 | 609 | #if wxUSE_RICHEDIT |
aac7e7fe VZ |
610 | // there are several complications with the rich edit controls here |
611 | bool done = FALSE; | |
612 | if ( IsRich() ) | |
4bc1afd5 | 613 | { |
aac7e7fe VZ |
614 | // first, ensure that the new text will be in the default style |
615 | if ( !m_defaultStyle.IsDefault() ) | |
616 | { | |
617 | long start, end; | |
618 | GetSelection(&start, &end); | |
0b8e5844 VS |
619 | SetStyle(start, end, m_defaultStyle); |
620 | } | |
621 | ||
622 | #if wxUSE_UNICODE_MSLU | |
623 | // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x, | |
624 | // but EM_STREAMIN works | |
625 | if ( wxGetOsVersion() == wxWIN95 && GetRichVersion() > 1 ) | |
626 | { | |
79d26b32 | 627 | done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly); |
aac7e7fe | 628 | } |
0b8e5844 | 629 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe | 630 | |
9d7de3c2 | 631 | #if !wxUSE_UNICODE |
aac7e7fe VZ |
632 | // next check if the text we're inserting must be shown in a non |
633 | // default charset -- this only works for RichEdit > 1.0 | |
634 | if ( GetRichVersion() > 1 ) | |
635 | { | |
636 | wxFont font = m_defaultStyle.GetFont(); | |
637 | if ( !font.Ok() ) | |
638 | font = GetFont(); | |
639 | ||
640 | if ( font.Ok() ) | |
641 | { | |
642 | wxFontEncoding encoding = font.GetEncoding(); | |
643 | if ( encoding != wxFONTENCODING_SYSTEM ) | |
644 | { | |
79d26b32 | 645 | done = StreamIn(valueDos, encoding, selectionOnly); |
aac7e7fe VZ |
646 | } |
647 | } | |
648 | } | |
9d7de3c2 | 649 | #endif // !wxUSE_UNICODE |
4bc1afd5 | 650 | } |
4bc1afd5 | 651 | |
aac7e7fe VZ |
652 | if ( !done ) |
653 | #endif // wxUSE_RICHEDIT | |
654 | { | |
79d26b32 VZ |
655 | if ( !selectionOnly ) |
656 | { | |
657 | SetSelection(-1, -1); | |
658 | } | |
659 | ||
aac7e7fe VZ |
660 | ::SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str()); |
661 | } | |
2bda0e17 | 662 | |
a1b82138 | 663 | AdjustSpaceLimit(); |
2bda0e17 KB |
664 | } |
665 | ||
a1b82138 VZ |
666 | void wxTextCtrl::AppendText(const wxString& text) |
667 | { | |
668 | SetInsertionPointEnd(); | |
aac7e7fe | 669 | |
a1b82138 VZ |
670 | WriteText(text); |
671 | } | |
672 | ||
673 | void wxTextCtrl::Clear() | |
674 | { | |
aac7e7fe | 675 | ::SetWindowText(GetHwnd(), wxT("")); |
a1b82138 VZ |
676 | } |
677 | ||
678 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 679 | // Clipboard operations |
a1b82138 VZ |
680 | // ---------------------------------------------------------------------------- |
681 | ||
cd471848 | 682 | void wxTextCtrl::Copy() |
2bda0e17 | 683 | { |
e702ff0f JS |
684 | if (CanCopy()) |
685 | { | |
a5aa8086 | 686 | ::SendMessage(GetHwnd(), WM_COPY, 0, 0L); |
e702ff0f | 687 | } |
2bda0e17 KB |
688 | } |
689 | ||
cd471848 | 690 | void wxTextCtrl::Cut() |
2bda0e17 | 691 | { |
e702ff0f JS |
692 | if (CanCut()) |
693 | { | |
a5aa8086 | 694 | ::SendMessage(GetHwnd(), WM_CUT, 0, 0L); |
e702ff0f | 695 | } |
2bda0e17 KB |
696 | } |
697 | ||
cd471848 | 698 | void wxTextCtrl::Paste() |
2bda0e17 | 699 | { |
e702ff0f JS |
700 | if (CanPaste()) |
701 | { | |
a5aa8086 | 702 | ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L); |
e702ff0f | 703 | } |
2bda0e17 KB |
704 | } |
705 | ||
a1b82138 VZ |
706 | bool wxTextCtrl::CanCopy() const |
707 | { | |
708 | // Can copy if there's a selection | |
709 | long from, to; | |
a5aa8086 VZ |
710 | GetSelection(&from, &to); |
711 | return from != to; | |
a1b82138 VZ |
712 | } |
713 | ||
714 | bool wxTextCtrl::CanCut() const | |
715 | { | |
a5aa8086 | 716 | return CanCopy() && IsEditable(); |
a1b82138 VZ |
717 | } |
718 | ||
719 | bool wxTextCtrl::CanPaste() const | |
720 | { | |
aac7e7fe VZ |
721 | if ( !IsEditable() ) |
722 | return FALSE; | |
723 | ||
a1b82138 | 724 | #if wxUSE_RICHEDIT |
aac7e7fe | 725 | if ( IsRich() ) |
a1b82138 | 726 | { |
aac7e7fe VZ |
727 | UINT cf = 0; // 0 == any format |
728 | ||
729 | return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0; | |
a1b82138 | 730 | } |
aac7e7fe | 731 | #endif // wxUSE_RICHEDIT |
a1b82138 VZ |
732 | |
733 | // Standard edit control: check for straight text on clipboard | |
aac7e7fe VZ |
734 | if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) |
735 | return FALSE; | |
736 | ||
737 | bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; | |
738 | ::CloseClipboard(); | |
a1b82138 VZ |
739 | |
740 | return isTextAvailable; | |
741 | } | |
742 | ||
743 | // ---------------------------------------------------------------------------- | |
744 | // Accessors | |
745 | // ---------------------------------------------------------------------------- | |
746 | ||
debe6624 | 747 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 748 | { |
a1b82138 VZ |
749 | HWND hWnd = GetHwnd(); |
750 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
2bda0e17 KB |
751 | } |
752 | ||
debe6624 | 753 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 754 | { |
a5aa8086 | 755 | DoSetSelection(pos, pos); |
2bda0e17 KB |
756 | } |
757 | ||
cd471848 | 758 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 759 | { |
a5aa8086 VZ |
760 | long pos; |
761 | ||
762 | #if wxUSE_RICHEDIT | |
763 | if ( m_verRichEdit == 1 ) | |
764 | { | |
765 | // we don't have to waste time calling GetLastPosition() in this case | |
766 | pos = -1; | |
767 | } | |
768 | else // !RichEdit 1.0 | |
769 | #endif // wxUSE_RICHEDIT | |
770 | { | |
771 | pos = GetLastPosition(); | |
772 | } | |
773 | ||
a1b82138 | 774 | SetInsertionPoint(pos); |
2bda0e17 KB |
775 | } |
776 | ||
cd471848 | 777 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 778 | { |
57c208c5 | 779 | #if wxUSE_RICHEDIT |
aac7e7fe | 780 | if ( IsRich() ) |
a1b82138 VZ |
781 | { |
782 | CHARRANGE range; | |
783 | range.cpMin = 0; | |
784 | range.cpMax = 0; | |
785 | SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); | |
786 | return range.cpMin; | |
787 | } | |
aac7e7fe | 788 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 789 | |
a1b82138 VZ |
790 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
791 | return Pos & 0xFFFF; | |
2bda0e17 KB |
792 | } |
793 | ||
cd471848 | 794 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 795 | { |
a5aa8086 VZ |
796 | int numLines = GetNumberOfLines(); |
797 | long posStartLastLine = XYToPosition(0, numLines - 1); | |
39136494 | 798 | |
a5aa8086 | 799 | long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); |
2bda0e17 | 800 | |
a5aa8086 | 801 | return posStartLastLine + lenLastLine; |
2bda0e17 KB |
802 | } |
803 | ||
a1b82138 VZ |
804 | // If the return values from and to are the same, there is no |
805 | // selection. | |
806 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
807 | { | |
808 | #if wxUSE_RICHEDIT | |
aac7e7fe | 809 | if ( IsRich() ) |
a1b82138 VZ |
810 | { |
811 | CHARRANGE charRange; | |
aac7e7fe | 812 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange); |
a1b82138 VZ |
813 | |
814 | *from = charRange.cpMin; | |
815 | *to = charRange.cpMax; | |
a1b82138 | 816 | } |
4bc1afd5 | 817 | else |
aac7e7fe | 818 | #endif // !wxUSE_RICHEDIT |
4bc1afd5 VZ |
819 | { |
820 | DWORD dwStart, dwEnd; | |
aac7e7fe | 821 | ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); |
a1b82138 | 822 | |
4bc1afd5 VZ |
823 | *from = dwStart; |
824 | *to = dwEnd; | |
825 | } | |
a1b82138 VZ |
826 | } |
827 | ||
828 | bool wxTextCtrl::IsEditable() const | |
829 | { | |
830 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
831 | ||
a5aa8086 | 832 | return (style & ES_READONLY) == 0; |
a1b82138 VZ |
833 | } |
834 | ||
835 | // ---------------------------------------------------------------------------- | |
aac7e7fe | 836 | // selection |
a1b82138 VZ |
837 | // ---------------------------------------------------------------------------- |
838 | ||
aac7e7fe | 839 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 840 | { |
aac7e7fe VZ |
841 | // if from and to are both -1, it means (in wxWindows) that all text should |
842 | // be selected - translate into Windows convention | |
843 | if ( (from == -1) && (to == -1) ) | |
844 | { | |
845 | from = 0; | |
846 | to = -1; | |
847 | } | |
848 | ||
a5aa8086 VZ |
849 | DoSetSelection(from, to); |
850 | } | |
851 | ||
852 | void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) | |
853 | { | |
789295bf | 854 | HWND hWnd = GetHwnd(); |
39136494 | 855 | |
2bda0e17 | 856 | #ifdef __WIN32__ |
aac7e7fe VZ |
857 | #if wxUSE_RICHEDIT |
858 | if ( IsRich() ) | |
859 | { | |
860 | CHARRANGE range; | |
861 | range.cpMin = from; | |
862 | range.cpMax = to; | |
863 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
864 | } | |
865 | else | |
866 | #endif // wxUSE_RICHEDIT | |
867 | { | |
868 | SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to); | |
869 | } | |
a1b82138 | 870 | |
aac7e7fe | 871 | if ( scrollCaret ) |
2bda0e17 | 872 | { |
aac7e7fe | 873 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); |
2bda0e17 | 874 | } |
aac7e7fe VZ |
875 | #else // Win16 |
876 | // WPARAM is 0: selection is scrolled into view | |
877 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to)); | |
878 | #endif // Win32/16 | |
879 | } | |
880 | ||
881 | // ---------------------------------------------------------------------------- | |
882 | // Editing | |
883 | // ---------------------------------------------------------------------------- | |
39136494 | 884 | |
aac7e7fe VZ |
885 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
886 | { | |
887 | // Set selection and remove it | |
f882d57e | 888 | DoSetSelection(from, to, FALSE /* don't scroll caret into view */); |
aac7e7fe VZ |
889 | |
890 | SendMessage(GetHwnd(), EM_REPLACESEL, | |
2bda0e17 | 891 | #ifdef __WIN32__ |
aac7e7fe | 892 | TRUE, |
2bda0e17 | 893 | #else |
aac7e7fe | 894 | FALSE, |
2bda0e17 | 895 | #endif |
aac7e7fe VZ |
896 | (LPARAM)value.c_str()); |
897 | } | |
898 | ||
899 | void wxTextCtrl::Remove(long from, long to) | |
900 | { | |
901 | Replace(from, to, _T("")); | |
2bda0e17 KB |
902 | } |
903 | ||
904 | bool wxTextCtrl::LoadFile(const wxString& file) | |
905 | { | |
a1b82138 | 906 | if ( wxTextCtrlBase::LoadFile(file) ) |
cd471848 | 907 | { |
a1b82138 VZ |
908 | // update the size limit if needed |
909 | AdjustSpaceLimit(); | |
2bda0e17 | 910 | |
a1b82138 | 911 | return TRUE; |
2bda0e17 | 912 | } |
2bda0e17 | 913 | |
a1b82138 | 914 | return FALSE; |
2bda0e17 KB |
915 | } |
916 | ||
cd471848 | 917 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 918 | { |
a5aa8086 | 919 | return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; |
2bda0e17 KB |
920 | } |
921 | ||
922 | // Makes 'unmodified' | |
cd471848 | 923 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 924 | { |
a1b82138 | 925 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
926 | } |
927 | ||
cd471848 | 928 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 929 | { |
789295bf | 930 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
931 | } |
932 | ||
debe6624 | 933 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 934 | { |
2bda0e17 | 935 | // This gets the char index for the _beginning_ of this line |
a5aa8086 VZ |
936 | long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0); |
937 | ||
938 | return charIndex + x; | |
2bda0e17 KB |
939 | } |
940 | ||
0efe5ba7 | 941 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 942 | { |
789295bf | 943 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
944 | |
945 | // This gets the line number containing the character | |
a5aa8086 | 946 | long lineNo; |
0efe5ba7 | 947 | #if wxUSE_RICHEDIT |
aac7e7fe | 948 | if ( IsRich() ) |
0efe5ba7 | 949 | { |
a5aa8086 | 950 | lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); |
0efe5ba7 VZ |
951 | } |
952 | else | |
953 | #endif // wxUSE_RICHEDIT | |
a5aa8086 VZ |
954 | { |
955 | lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); | |
956 | } | |
0efe5ba7 VZ |
957 | |
958 | if ( lineNo == -1 ) | |
959 | { | |
960 | // no such line | |
961 | return FALSE; | |
962 | } | |
963 | ||
2bda0e17 | 964 | // This gets the char index for the _beginning_ of this line |
a5aa8086 | 965 | long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); |
0efe5ba7 VZ |
966 | if ( charIndex == -1 ) |
967 | { | |
968 | return FALSE; | |
969 | } | |
970 | ||
2bda0e17 | 971 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 | 972 | if ( x ) |
a5aa8086 | 973 | *x = pos - charIndex; |
0efe5ba7 | 974 | if ( y ) |
a5aa8086 | 975 | *y = lineNo; |
0efe5ba7 VZ |
976 | |
977 | return TRUE; | |
2bda0e17 KB |
978 | } |
979 | ||
debe6624 | 980 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 981 | { |
789295bf | 982 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
983 | |
984 | // To scroll to a position, we pass the number of lines and characters | |
985 | // to scroll *by*. This means that we need to: | |
986 | // (1) Find the line position of the current line. | |
987 | // (2) Find the line position of pos. | |
988 | // (3) Scroll by (pos - current). | |
989 | // For now, ignore the horizontal scrolling. | |
990 | ||
991 | // Is this where scrolling is relative to - the line containing the caret? | |
992 | // Or is the first visible line??? Try first visible line. | |
993 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
994 | ||
995 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
996 | ||
997 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
39136494 | 998 | |
2bda0e17 KB |
999 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
1000 | ||
2bda0e17 | 1001 | if (linesToScroll != 0) |
de4d7713 | 1002 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
1003 | } |
1004 | ||
a5aa8086 VZ |
1005 | long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const |
1006 | { | |
1007 | return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0); | |
1008 | } | |
1009 | ||
debe6624 | 1010 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 | 1011 | { |
a5aa8086 VZ |
1012 | long pos = XYToPosition(0, lineNo); |
1013 | ||
1014 | return GetLengthOfLineContainingPos(pos); | |
2bda0e17 KB |
1015 | } |
1016 | ||
debe6624 | 1017 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 1018 | { |
a1b82138 | 1019 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
488fe1fe | 1020 | |
f6bcfd97 BP |
1021 | // there must be at least enough place for the length WORD in the |
1022 | // buffer | |
1023 | len += sizeof(WORD); | |
4438caf4 | 1024 | |
f6bcfd97 BP |
1025 | wxString str; |
1026 | wxChar *buf = str.GetWriteBuf(len); | |
1027 | ||
33ac7e6f | 1028 | *(WORD *)buf = (WORD)len; |
f6bcfd97 BP |
1029 | len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); |
1030 | buf[len] = 0; | |
4438caf4 | 1031 | |
f6bcfd97 | 1032 | str.UngetWriteBuf(len); |
4438caf4 VZ |
1033 | |
1034 | return str; | |
2bda0e17 KB |
1035 | } |
1036 | ||
d7eee191 VZ |
1037 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1038 | { | |
1039 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0); | |
1040 | } | |
1041 | ||
a1b82138 | 1042 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 1043 | // Undo/redo |
a1b82138 VZ |
1044 | // ---------------------------------------------------------------------------- |
1045 | ||
ca8b28f2 JS |
1046 | void wxTextCtrl::Undo() |
1047 | { | |
1048 | if (CanUndo()) | |
1049 | { | |
789295bf | 1050 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1051 | } |
1052 | } | |
1053 | ||
1054 | void wxTextCtrl::Redo() | |
1055 | { | |
1056 | if (CanRedo()) | |
1057 | { | |
1058 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
789295bf | 1059 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1060 | } |
1061 | } | |
1062 | ||
1063 | bool wxTextCtrl::CanUndo() const | |
1064 | { | |
a5aa8086 | 1065 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1066 | } |
1067 | ||
1068 | bool wxTextCtrl::CanRedo() const | |
1069 | { | |
a5aa8086 | 1070 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1071 | } |
1072 | ||
a1b82138 VZ |
1073 | // ---------------------------------------------------------------------------- |
1074 | // implemenation details | |
1075 | // ---------------------------------------------------------------------------- | |
39136494 | 1076 | |
2bda0e17 KB |
1077 | void wxTextCtrl::Command(wxCommandEvent & event) |
1078 | { | |
a1b82138 VZ |
1079 | SetValue(event.GetString()); |
1080 | ProcessCommand (event); | |
2bda0e17 KB |
1081 | } |
1082 | ||
1083 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
1084 | { | |
a1b82138 VZ |
1085 | // By default, load the first file into the text window. |
1086 | if (event.GetNumberOfFiles() > 0) | |
1087 | { | |
1088 | LoadFile(event.GetFiles()[0]); | |
1089 | } | |
2bda0e17 KB |
1090 | } |
1091 | ||
a37d422a VZ |
1092 | // ---------------------------------------------------------------------------- |
1093 | // kbd input processing | |
1094 | // ---------------------------------------------------------------------------- | |
1095 | ||
1096 | bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg) | |
1097 | { | |
1098 | MSG *msg = (MSG *)pMsg; | |
1099 | ||
1100 | // check for our special keys here: if we don't do it and the parent frame | |
1101 | // uses them as accelerators, they wouldn't work at all, so we disable | |
1102 | // usual preprocessing for them | |
1103 | if ( msg->message == WM_KEYDOWN ) | |
1104 | { | |
1105 | WORD vkey = msg->wParam; | |
1106 | if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN ) | |
1107 | { | |
1108 | if ( vkey == VK_BACK ) | |
1109 | return FALSE; | |
1110 | } | |
1111 | else // no Alt | |
1112 | { | |
1113 | if ( wxIsCtrlDown() ) | |
1114 | { | |
1115 | switch ( vkey ) | |
1116 | { | |
1117 | case 'C': | |
1118 | case 'V': | |
1119 | case 'X': | |
1120 | case VK_INSERT: | |
1121 | case VK_DELETE: | |
1122 | case VK_HOME: | |
1123 | case VK_END: | |
1124 | return FALSE; | |
1125 | } | |
1126 | } | |
1127 | else if ( wxIsShiftDown() ) | |
1128 | { | |
1129 | if ( vkey == VK_INSERT || vkey == VK_DELETE ) | |
1130 | return FALSE; | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | return wxControl::MSWShouldPreProcessMessage(pMsg); | |
1136 | } | |
1137 | ||
2bda0e17 KB |
1138 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
1139 | { | |
42e69d6b | 1140 | switch ( event.KeyCode() ) |
cd471848 | 1141 | { |
cd471848 | 1142 | case WXK_RETURN: |
39136494 | 1143 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
1144 | { |
1145 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
bfbd6dc1 | 1146 | InitCommandEvent(event); |
f6bcfd97 | 1147 | event.SetString(GetValue()); |
cd471848 VZ |
1148 | if ( GetEventHandler()->ProcessEvent(event) ) |
1149 | return; | |
1150 | } | |
5fb9fcfc VZ |
1151 | //else: multiline controls need Enter for themselves |
1152 | ||
1153 | break; | |
4d91c1d1 | 1154 | |
cd471848 | 1155 | case WXK_TAB: |
319fefa9 VZ |
1156 | // always produce navigation event - even if we process TAB |
1157 | // ourselves the fact that we got here means that the user code | |
1158 | // decided to skip processing of this TAB - probably to let it | |
1159 | // do its default job. | |
cd471848 | 1160 | { |
5fb9fcfc VZ |
1161 | wxNavigationKeyEvent eventNav; |
1162 | eventNav.SetDirection(!event.ShiftDown()); | |
8614c467 | 1163 | eventNav.SetWindowChange(event.ControlDown()); |
5fb9fcfc | 1164 | eventNav.SetEventObject(this); |
39136494 | 1165 | |
d9506e77 | 1166 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
1167 | return; |
1168 | } | |
341c92a8 | 1169 | break; |
cd471848 | 1170 | } |
39136494 | 1171 | |
8614c467 | 1172 | // no, we didn't process it |
42e69d6b | 1173 | event.Skip(); |
2bda0e17 KB |
1174 | } |
1175 | ||
debe6624 | 1176 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 1177 | { |
789295bf VZ |
1178 | switch (param) |
1179 | { | |
1180 | case EN_SETFOCUS: | |
1181 | case EN_KILLFOCUS: | |
1182 | { | |
1183 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
d7eee191 VZ |
1184 | : wxEVT_SET_FOCUS, |
1185 | m_windowId); | |
789295bf VZ |
1186 | event.SetEventObject( this ); |
1187 | GetEventHandler()->ProcessEvent(event); | |
1188 | } | |
1189 | break; | |
ae29de83 | 1190 | |
789295bf VZ |
1191 | case EN_CHANGE: |
1192 | { | |
1193 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
bfbd6dc1 VZ |
1194 | InitCommandEvent(event); |
1195 | event.SetString(GetValue()); | |
1196 | ProcessCommand(event); | |
789295bf VZ |
1197 | } |
1198 | break; | |
2bda0e17 | 1199 | |
b12915c1 | 1200 | case EN_MAXTEXT: |
789295bf | 1201 | // the text size limit has been hit - increase it |
d7eee191 VZ |
1202 | if ( !AdjustSpaceLimit() ) |
1203 | { | |
1204 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId); | |
1205 | InitCommandEvent(event); | |
1206 | event.SetString(GetValue()); | |
1207 | ProcessCommand(event); | |
1208 | } | |
789295bf VZ |
1209 | break; |
1210 | ||
1211 | // the other notification messages are not processed | |
1212 | case EN_UPDATE: | |
b12915c1 | 1213 | case EN_ERRSPACE: |
789295bf VZ |
1214 | case EN_HSCROLL: |
1215 | case EN_VSCROLL: | |
f6bcfd97 | 1216 | return FALSE; |
789295bf VZ |
1217 | default: |
1218 | return FALSE; | |
1219 | } | |
1220 | ||
1221 | // processed | |
1222 | return TRUE; | |
2bda0e17 KB |
1223 | } |
1224 | ||
33ac7e6f | 1225 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
1226 | #if wxUSE_CTL3D |
1227 | WXUINT message, | |
1228 | WXWPARAM wParam, | |
1229 | WXLPARAM lParam | |
1230 | #else | |
33ac7e6f KB |
1231 | WXUINT WXUNUSED(message), |
1232 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
1233 | WXLPARAM WXUNUSED(lParam) |
1234 | #endif | |
1235 | ) | |
f6bcfd97 BP |
1236 | { |
1237 | #if wxUSE_CTL3D | |
1238 | if ( m_useCtl3D ) | |
1239 | { | |
1240 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
1241 | return (WXHBRUSH) hbrush; | |
1242 | } | |
1243 | #endif // wxUSE_CTL3D | |
1244 | ||
1245 | HDC hdc = (HDC)pDC; | |
1246 | if (GetParent()->GetTransparentBackground()) | |
1247 | SetBkMode(hdc, TRANSPARENT); | |
1248 | else | |
1249 | SetBkMode(hdc, OPAQUE); | |
1250 | ||
1251 | wxColour colBack = GetBackgroundColour(); | |
1252 | ||
1253 | if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0) | |
a756f210 | 1254 | colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
f6bcfd97 BP |
1255 | |
1256 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
1257 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
1258 | ||
1259 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
1260 | ||
1261 | return (WXHBRUSH)brush->GetResourceHandle(); | |
1262 | } | |
1263 | ||
1264 | // In WIN16, need to override normal erasing because | |
1265 | // Ctl3D doesn't use the wxWindows background colour. | |
1266 | #ifdef __WIN16__ | |
1267 | void wxTextCtrl::OnEraseBackground(wxEraseEvent& event) | |
1268 | { | |
1269 | wxColour col(m_backgroundColour); | |
1270 | ||
1271 | #if wxUSE_CTL3D | |
1272 | if (m_useCtl3D) | |
a756f210 | 1273 | col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); |
f6bcfd97 BP |
1274 | #endif |
1275 | ||
1276 | RECT rect; | |
1277 | ::GetClientRect(GetHwnd(), &rect); | |
1278 | ||
a5aa8086 | 1279 | COLORREF ref = wxColourToRGB(col); |
f6bcfd97 BP |
1280 | HBRUSH hBrush = ::CreateSolidBrush(ref); |
1281 | if ( !hBrush ) | |
1282 | wxLogLastError(wxT("CreateSolidBrush")); | |
1283 | ||
1284 | HDC hdc = (HDC)event.GetDC()->GetHDC(); | |
1285 | ||
1286 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
1287 | ||
1288 | ::FillRect(hdc, &rect, hBrush); | |
1289 | ::DeleteObject(hBrush); | |
1290 | ::SetMapMode(hdc, mode); | |
1291 | ||
1292 | } | |
aac7e7fe | 1293 | #endif // Win16 |
f6bcfd97 | 1294 | |
d7eee191 | 1295 | bool wxTextCtrl::AdjustSpaceLimit() |
789295bf | 1296 | { |
25889d3c | 1297 | #ifndef __WIN16__ |
d7eee191 VZ |
1298 | unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); |
1299 | ||
1300 | // HACK: we try to automatically extend the limit for the amount of text | |
1301 | // to allow (interactively) entering more than 64Kb of text under | |
1302 | // Win9x but we shouldn't reset the text limit which was previously | |
1303 | // set explicitly with SetMaxLength() | |
1304 | // | |
1305 | // we could solve this by storing the limit we set in wxTextCtrl but | |
1306 | // to save space we prefer to simply test here the actual limit | |
1307 | // value: we consider that SetMaxLength() can only be called for | |
1308 | // values < 32Kb | |
1309 | if ( limit < 0x8000 ) | |
1310 | { | |
1311 | // we've got more text than limit set by SetMaxLength() | |
1312 | return FALSE; | |
1313 | } | |
1314 | ||
1315 | unsigned int len = ::GetWindowTextLength(GetHwnd()); | |
17d8ee1c | 1316 | if ( len >= limit ) |
789295bf VZ |
1317 | { |
1318 | limit = len + 0x8000; // 32Kb | |
1319 | ||
5ea105e0 | 1320 | #if wxUSE_RICHEDIT |
aac7e7fe | 1321 | if ( IsRich() ) |
b12915c1 VZ |
1322 | { |
1323 | // as a nice side effect, this also allows passing limit > 64Kb | |
1324 | ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit); | |
1325 | } | |
789295bf | 1326 | else |
b12915c1 VZ |
1327 | #endif // wxUSE_RICHEDIT |
1328 | { | |
1329 | if ( limit > 0xffff ) | |
1330 | { | |
1331 | // this will set it to a platform-dependent maximum (much more | |
1332 | // than 64Kb under NT) | |
1333 | limit = 0; | |
1334 | } | |
1335 | ||
789295bf | 1336 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); |
b12915c1 | 1337 | } |
789295bf | 1338 | } |
b12915c1 | 1339 | #endif // !Win16 |
d7eee191 VZ |
1340 | |
1341 | // we changed the limit | |
1342 | return TRUE; | |
789295bf | 1343 | } |
2bda0e17 | 1344 | |
a1b82138 | 1345 | bool wxTextCtrl::AcceptsFocus() const |
2bda0e17 | 1346 | { |
a1b82138 VZ |
1347 | // we don't want focus if we can't be edited |
1348 | return IsEditable() && wxControl::AcceptsFocus(); | |
1349 | } | |
c085e333 | 1350 | |
f68586e5 | 1351 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
1352 | { |
1353 | int cx, cy; | |
1354 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
1355 | ||
1356 | int wText = DEFAULT_ITEM_WIDTH; | |
1357 | ||
1358 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
1359 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1360 | { | |
3988b155 | 1361 | hText *= wxMax(GetNumberOfLines(), 5); |
a1b82138 VZ |
1362 | } |
1363 | //else: for single line control everything is ok | |
1364 | ||
1365 | return wxSize(wText, hText); | |
2bda0e17 | 1366 | } |
a1b82138 VZ |
1367 | |
1368 | // ---------------------------------------------------------------------------- | |
1369 | // standard handlers for standard edit menu events | |
1370 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1371 | |
bfbd6dc1 | 1372 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1373 | { |
1374 | Cut(); | |
1375 | } | |
1376 | ||
bfbd6dc1 | 1377 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1378 | { |
1379 | Copy(); | |
1380 | } | |
1381 | ||
bfbd6dc1 | 1382 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1383 | { |
1384 | Paste(); | |
1385 | } | |
1386 | ||
bfbd6dc1 | 1387 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1388 | { |
1389 | Undo(); | |
1390 | } | |
1391 | ||
bfbd6dc1 | 1392 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1393 | { |
1394 | Redo(); | |
1395 | } | |
1396 | ||
1397 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1398 | { | |
1399 | event.Enable( CanCut() ); | |
1400 | } | |
1401 | ||
1402 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1403 | { | |
1404 | event.Enable( CanCopy() ); | |
1405 | } | |
1406 | ||
1407 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1408 | { | |
1409 | event.Enable( CanPaste() ); | |
1410 | } | |
1411 | ||
1412 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1413 | { | |
1414 | event.Enable( CanUndo() ); | |
1415 | } | |
1416 | ||
1417 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1418 | { | |
1419 | event.Enable( CanRedo() ); | |
1420 | } | |
1421 | ||
4bc1afd5 VZ |
1422 | // the rest of the file only deals with the rich edit controls |
1423 | #if wxUSE_RICHEDIT | |
1424 | ||
c57e3339 VZ |
1425 | // ---------------------------------------------------------------------------- |
1426 | // EN_LINK processing | |
1427 | // ---------------------------------------------------------------------------- | |
1428 | ||
1429 | bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
1430 | { | |
1431 | NMHDR *hdr = (NMHDR* )lParam; | |
1432 | if ( hdr->code == EN_LINK ) | |
1433 | { | |
1434 | ENLINK *enlink = (ENLINK *)hdr; | |
1435 | ||
1436 | switch ( enlink->msg ) | |
1437 | { | |
1438 | case WM_SETCURSOR: | |
1439 | // ok, so it is hardcoded - do we really nee to customize it? | |
1440 | ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND))); | |
1441 | *result = TRUE; | |
1442 | break; | |
1443 | ||
1444 | case WM_MOUSEMOVE: | |
1445 | case WM_LBUTTONDOWN: | |
1446 | case WM_LBUTTONUP: | |
1447 | case WM_LBUTTONDBLCLK: | |
1448 | case WM_RBUTTONDOWN: | |
1449 | case WM_RBUTTONUP: | |
1450 | case WM_RBUTTONDBLCLK: | |
1451 | // send a mouse event | |
1452 | { | |
1453 | static const wxEventType eventsMouse[] = | |
1454 | { | |
1455 | wxEVT_MOTION, | |
1456 | wxEVT_LEFT_DOWN, | |
1457 | wxEVT_LEFT_UP, | |
1458 | wxEVT_LEFT_DCLICK, | |
1459 | wxEVT_RIGHT_DOWN, | |
1460 | wxEVT_RIGHT_UP, | |
1461 | wxEVT_RIGHT_DCLICK, | |
1462 | }; | |
1463 | ||
1464 | // the event ids are consecutive | |
1465 | wxMouseEvent | |
1466 | evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]); | |
1467 | ||
1468 | InitMouseEvent(evtMouse, | |
1469 | GET_X_LPARAM(enlink->lParam), | |
1470 | GET_Y_LPARAM(enlink->lParam), | |
1471 | enlink->wParam); | |
1472 | ||
1473 | wxTextUrlEvent event(m_windowId, evtMouse, | |
1474 | enlink->chrg.cpMin, | |
1475 | enlink->chrg.cpMax); | |
1476 | ||
1477 | InitCommandEvent(event); | |
1478 | ||
1479 | *result = ProcessCommand(event); | |
1480 | } | |
1481 | break; | |
1482 | } | |
1483 | ||
1484 | return TRUE; | |
1485 | } | |
1486 | ||
1487 | // not processed | |
1488 | return FALSE; | |
1489 | } | |
1490 | ||
f6bcfd97 BP |
1491 | // ---------------------------------------------------------------------------- |
1492 | // colour setting for the rich edit controls | |
1493 | // ---------------------------------------------------------------------------- | |
1494 | ||
f6bcfd97 BP |
1495 | bool wxTextCtrl::SetBackgroundColour(const wxColour& colour) |
1496 | { | |
1497 | if ( !wxTextCtrlBase::SetBackgroundColour(colour) ) | |
1498 | { | |
1499 | // colour didn't really change | |
1500 | return FALSE; | |
1501 | } | |
1502 | ||
1503 | if ( IsRich() ) | |
1504 | { | |
1505 | // rich edit doesn't use WM_CTLCOLOR, hence we need to send | |
1506 | // EM_SETBKGNDCOLOR additionally | |
1507 | ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour)); | |
1508 | } | |
1509 | ||
1510 | return TRUE; | |
1511 | } | |
1512 | ||
1513 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
1514 | { | |
1515 | if ( !wxTextCtrlBase::SetForegroundColour(colour) ) | |
1516 | { | |
1517 | // colour didn't really change | |
1518 | return FALSE; | |
1519 | } | |
1520 | ||
1521 | if ( IsRich() ) | |
1522 | { | |
1523 | // change the colour of everything | |
1524 | CHARFORMAT cf; | |
1525 | wxZeroMemory(cf); | |
1526 | cf.cbSize = sizeof(cf); | |
1527 | cf.dwMask = CFM_COLOR; | |
1528 | cf.crTextColor = wxColourToRGB(colour); | |
1529 | ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); | |
1530 | } | |
1531 | ||
1532 | return TRUE; | |
1533 | } | |
1534 | ||
4bc1afd5 VZ |
1535 | // ---------------------------------------------------------------------------- |
1536 | // styling support for rich edit controls | |
1537 | // ---------------------------------------------------------------------------- | |
1538 | ||
1539 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) | |
1540 | { | |
1541 | if ( !IsRich() ) | |
1542 | { | |
1543 | // can't do it with normal text control | |
1544 | return FALSE; | |
1545 | } | |
1546 | ||
a5aa8086 VZ |
1547 | // the richedit 1.0 doesn't handle setting background colour, so don't |
1548 | // even try to do anything if it's the only thing we want to change | |
1549 | if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() ) | |
4bc1afd5 | 1550 | { |
be329a3d RD |
1551 | // nothing to do: return TRUE if there was really nothing to do and |
1552 | // FALSE if we failed to set bg colour | |
4bc1afd5 VZ |
1553 | return !style.HasBackgroundColour(); |
1554 | } | |
1555 | ||
1556 | // order the range if needed | |
1557 | if ( start > end ) | |
1558 | { | |
1559 | long tmp = start; | |
1560 | start = end; | |
1561 | end = tmp; | |
1562 | } | |
1563 | ||
1564 | // we can only change the format of the selection, so select the range we | |
1565 | // want and restore the old selection later | |
1566 | long startOld, endOld; | |
1567 | GetSelection(&startOld, &endOld); | |
1568 | ||
1569 | // but do we really have to change the selection? | |
1570 | bool changeSel = start != startOld || end != endOld; | |
1571 | ||
1572 | if ( changeSel ) | |
aac7e7fe | 1573 | { |
f882d57e | 1574 | DoSetSelection(start, end, FALSE /* don't scroll caret into view */); |
aac7e7fe | 1575 | } |
4bc1afd5 VZ |
1576 | |
1577 | // initialize CHARFORMAT struct | |
be329a3d RD |
1578 | #if wxUSE_RICHEDIT2 |
1579 | CHARFORMAT2 cf; | |
1580 | #else | |
4bc1afd5 | 1581 | CHARFORMAT cf; |
be329a3d | 1582 | #endif |
a5aa8086 | 1583 | |
4bc1afd5 | 1584 | wxZeroMemory(cf); |
a5aa8086 VZ |
1585 | |
1586 | // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple | |
1587 | // CHARFORMAT in that case | |
1588 | #if wxUSE_RICHEDIT2 | |
1589 | if ( m_verRichEdit == 1 ) | |
1590 | { | |
1591 | // this is the only thing the control is going to grok | |
1592 | cf.cbSize = sizeof(CHARFORMAT); | |
1593 | } | |
1594 | else | |
1595 | #endif | |
1596 | { | |
1597 | // CHARFORMAT or CHARFORMAT2 | |
1598 | cf.cbSize = sizeof(cf); | |
1599 | } | |
4bc1afd5 VZ |
1600 | |
1601 | if ( style.HasFont() ) | |
1602 | { | |
aac7e7fe VZ |
1603 | // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0 |
1604 | // but using it doesn't seem to hurt neither so leaving it for now | |
1605 | ||
784164e1 VZ |
1606 | cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET | |
1607 | CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE; | |
4bc1afd5 VZ |
1608 | |
1609 | // fill in data from LOGFONT but recalculate lfHeight because we need | |
1610 | // the real height in twips and not the negative number which | |
1611 | // wxFillLogFont() returns (this is correct in general and works with | |
1612 | // the Windows font mapper, but not here) | |
1613 | LOGFONT lf; | |
1614 | wxFillLogFont(&lf, &style.GetFont()); | |
1615 | cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips | |
1616 | cf.bCharSet = lf.lfCharSet; | |
1617 | cf.bPitchAndFamily = lf.lfPitchAndFamily; | |
1618 | wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) ); | |
1619 | ||
784164e1 VZ |
1620 | // also deal with underline/italic/bold attributes: note that we must |
1621 | // always set CFM_ITALIC &c bits in dwMask, even if we don't set the | |
1622 | // style to allow clearing it | |
4bc1afd5 VZ |
1623 | if ( lf.lfItalic ) |
1624 | { | |
4bc1afd5 VZ |
1625 | cf.dwEffects |= CFE_ITALIC; |
1626 | } | |
1627 | ||
1628 | if ( lf.lfWeight == FW_BOLD ) | |
1629 | { | |
4bc1afd5 VZ |
1630 | cf.dwEffects |= CFE_BOLD; |
1631 | } | |
1632 | ||
1633 | if ( lf.lfUnderline ) | |
1634 | { | |
4bc1afd5 VZ |
1635 | cf.dwEffects |= CFE_UNDERLINE; |
1636 | } | |
1637 | ||
1638 | // strikeout fonts are not supported by wxWindows | |
1639 | } | |
1640 | ||
1641 | if ( style.HasTextColour() ) | |
1642 | { | |
1643 | cf.dwMask |= CFM_COLOR; | |
1644 | cf.crTextColor = wxColourToRGB(style.GetTextColour()); | |
1645 | } | |
1646 | ||
be329a3d | 1647 | #if wxUSE_RICHEDIT2 |
a5aa8086 | 1648 | if ( m_verRichEdit != 1 && style.HasBackgroundColour() ) |
be329a3d RD |
1649 | { |
1650 | cf.dwMask |= CFM_BACKCOLOR; | |
1651 | cf.crBackColor = wxColourToRGB(style.GetBackgroundColour()); | |
1652 | } | |
784164e1 VZ |
1653 | #endif // wxUSE_RICHEDIT2 |
1654 | ||
4bc1afd5 VZ |
1655 | // do format the selection |
1656 | bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, | |
1657 | SCF_SELECTION, (LPARAM)&cf) != 0; | |
1658 | if ( !ok ) | |
1659 | { | |
1660 | wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed")); | |
1661 | } | |
1662 | ||
1663 | if ( changeSel ) | |
1664 | { | |
1665 | // restore the original selection | |
aac7e7fe | 1666 | DoSetSelection(startOld, endOld, FALSE); |
4bc1afd5 VZ |
1667 | } |
1668 | ||
1669 | return ok; | |
1670 | } | |
f6bcfd97 | 1671 | |
b12915c1 VZ |
1672 | // ---------------------------------------------------------------------------- |
1673 | // wxRichEditModule | |
1674 | // ---------------------------------------------------------------------------- | |
1675 | ||
b12915c1 VZ |
1676 | bool wxRichEditModule::OnInit() |
1677 | { | |
1678 | // don't do anything - we will load it when needed | |
1679 | return TRUE; | |
1680 | } | |
1681 | ||
1682 | void wxRichEditModule::OnExit() | |
1683 | { | |
a5aa8086 | 1684 | for ( int i = 0; i < WXSIZEOF(ms_hRichEdit); i++ ) |
b12915c1 | 1685 | { |
a5aa8086 VZ |
1686 | if ( ms_hRichEdit[i] ) |
1687 | { | |
1688 | ::FreeLibrary(ms_hRichEdit[i]); | |
1689 | } | |
b12915c1 VZ |
1690 | } |
1691 | } | |
1692 | ||
1693 | /* static */ | |
1694 | bool wxRichEditModule::Load(int version) | |
1695 | { | |
a5aa8086 VZ |
1696 | // we don't support loading richedit 3.0 as I don't know how to distinguish |
1697 | // it from 2.0 anyhow | |
1698 | wxCHECK_MSG( version == 1 || version == 2, FALSE, | |
b12915c1 VZ |
1699 | _T("incorrect richedit control version requested") ); |
1700 | ||
a5aa8086 VZ |
1701 | // make it the index in the array |
1702 | version--; | |
1703 | ||
a5aa8086 | 1704 | if ( ms_hRichEdit[version] == (HINSTANCE)-1 ) |
b12915c1 | 1705 | { |
a5aa8086 VZ |
1706 | // we had already tried to load it and failed |
1707 | return FALSE; | |
b12915c1 VZ |
1708 | } |
1709 | ||
28978e0c VZ |
1710 | if ( ms_hRichEdit[version] ) |
1711 | { | |
1712 | // we've already got this one | |
1713 | return TRUE; | |
1714 | } | |
1715 | ||
a5aa8086 VZ |
1716 | wxString dllname = version ? _T("riched20") : _T("riched32"); |
1717 | dllname += _T(".dll"); | |
b12915c1 | 1718 | |
a5aa8086 | 1719 | ms_hRichEdit[version] = ::LoadLibrary(dllname); |
b12915c1 | 1720 | |
a5aa8086 | 1721 | if ( !ms_hRichEdit[version] ) |
b12915c1 VZ |
1722 | { |
1723 | wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str()); | |
1724 | ||
a5aa8086 | 1725 | ms_hRichEdit[version] = (HINSTANCE)-1; |
b12915c1 VZ |
1726 | |
1727 | return FALSE; | |
1728 | } | |
1729 | ||
1730 | return TRUE; | |
1731 | } | |
1732 | ||
1733 | #endif // wxUSE_RICHEDIT | |
1734 | ||
1e6feb95 | 1735 | #endif // wxUSE_TEXTCTRL |