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