]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "textctrl.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/textctrl.h" | |
25 | #include "wx/settings.h" | |
2432b92d JS |
26 | #include "wx/brush.h" |
27 | #include "wx/utils.h" | |
28 | #include "wx/log.h" | |
2bda0e17 KB |
29 | #endif |
30 | ||
47d67540 | 31 | #if wxUSE_CLIPBOARD |
2bda0e17 KB |
32 | #include "wx/app.h" |
33 | #include "wx/clipbrd.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/msw/private.h" | |
37 | ||
38 | #include <windows.h> | |
39 | #include <stdlib.h> | |
fbc535ff JS |
40 | |
41 | #if wxUSE_IOSTREAMH | |
3f4a0c5b | 42 | # include <fstream.h> |
fbc535ff | 43 | #else |
3f4a0c5b | 44 | # include <fstream> |
fbc535ff | 45 | #endif |
2bda0e17 KB |
46 | |
47 | #include <sys/types.h> | |
17dff81c | 48 | #ifndef __MWERKS__ |
2bda0e17 | 49 | #include <sys/stat.h> |
17dff81c SC |
50 | #else |
51 | #include <stat.h> | |
52 | #endif | |
2bda0e17 KB |
53 | #if defined(__BORLANDC__) && !defined(__WIN32__) |
54 | #include <alloc.h> | |
55 | #else | |
ce3ed50d | 56 | #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) |
2bda0e17 KB |
57 | #include <malloc.h> |
58 | #endif | |
59 | #define farmalloc malloc | |
60 | #define farfree free | |
61 | #endif | |
62 | #include <windowsx.h> | |
63 | ||
64 | #include <string.h> | |
65 | ||
57c208c5 | 66 | #if wxUSE_RICHEDIT && !defined(__GNUWIN32__) |
cd471848 | 67 | #include <richedit.h> |
2bda0e17 KB |
68 | #endif |
69 | ||
70 | #if !USE_SHARED_LIBRARY | |
e702ff0f | 71 | |
2bda0e17 KB |
72 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
73 | ||
74 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
c085e333 VZ |
75 | EVT_CHAR(wxTextCtrl::OnChar) |
76 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
e702ff0f JS |
77 | |
78 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
79 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
80 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
81 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
82 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
83 | ||
84 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
85 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
86 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
87 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
88 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
2bda0e17 | 89 | END_EVENT_TABLE() |
e702ff0f | 90 | |
cd471848 | 91 | #endif // USE_SHARED_LIBRARY |
2bda0e17 KB |
92 | |
93 | // Text item | |
cd471848 | 94 | wxTextCtrl::wxTextCtrl() |
2bda0e17 | 95 | #ifndef NO_TEXT_WINDOW_STREAM |
cd471848 | 96 | : streambuf() |
2bda0e17 KB |
97 | #endif |
98 | { | |
cd471848 | 99 | #if wxUSE_RICHEDIT |
2bda0e17 | 100 | m_isRich = FALSE; |
cd471848 | 101 | #endif |
2bda0e17 KB |
102 | } |
103 | ||
debe6624 | 104 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
105 | const wxString& value, |
106 | const wxPoint& pos, | |
107 | const wxSize& size, long style, | |
108 | const wxValidator& validator, | |
109 | const wxString& name) | |
2bda0e17 | 110 | { |
2bda0e17 KB |
111 | SetName(name); |
112 | SetValidator(validator); | |
113 | if (parent) parent->AddChild(this); | |
114 | ||
115 | m_windowStyle = style; | |
116 | ||
117 | // Should this be taken from the system colours? | |
118 | // SetBackgroundColour(wxColour(255, 255, 255)); | |
119 | ||
120 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
121 | ||
fd71308f | 122 | SetForegroundColour(parent->GetForegroundColour()) ; |
2bda0e17 KB |
123 | |
124 | if ( id == -1 ) | |
c085e333 | 125 | m_windowId = (int)NewControlId(); |
2bda0e17 | 126 | else |
c085e333 | 127 | m_windowId = id; |
2bda0e17 KB |
128 | |
129 | int x = pos.x; | |
130 | int y = pos.y; | |
131 | int width = size.x; | |
132 | int height = size.y; | |
133 | ||
2bda0e17 KB |
134 | long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP; |
135 | if (m_windowStyle & wxTE_MULTILINE) | |
cd471848 | 136 | { |
5fb9fcfc | 137 | wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER), |
837e5743 | 138 | _T("wxTE_PROCESS_ENTER style is ignored for multiline controls") ); |
5fb9fcfc | 139 | |
cd471848 VZ |
140 | msStyle |= ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL ; // WS_BORDER |
141 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
142 | } | |
2bda0e17 KB |
143 | else |
144 | msStyle |= ES_AUTOHSCROLL ; | |
145 | ||
146 | if (m_windowStyle & wxTE_READONLY) | |
147 | msStyle |= ES_READONLY; | |
148 | ||
149 | if (m_windowStyle & wxHSCROLL) | |
150 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL) ; | |
151 | if (m_windowStyle & wxTE_PASSWORD) // hidden input | |
152 | msStyle |= ES_PASSWORD; | |
153 | ||
837e5743 | 154 | const wxChar *windowClass = _T("EDIT"); |
cd471848 | 155 | |
57c208c5 | 156 | #if wxUSE_RICHEDIT |
2bda0e17 | 157 | if ( m_windowStyle & wxTE_MULTILINE ) |
2bda0e17 | 158 | { |
c085e333 VZ |
159 | msStyle |= ES_AUTOVSCROLL; |
160 | m_isRich = TRUE; | |
837e5743 | 161 | windowClass = _T("RichEdit") ; |
2bda0e17 KB |
162 | } |
163 | else | |
c085e333 | 164 | m_isRich = FALSE; |
cd471848 | 165 | #endif |
2bda0e17 KB |
166 | |
167 | bool want3D; | |
168 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
169 | ||
170 | // If we're in Win95, and we want a simple 2D border, | |
171 | // then make it an EDIT control instead. | |
57c208c5 | 172 | #if wxUSE_RICHEDIT |
2bda0e17 KB |
173 | if (m_windowStyle & wxSIMPLE_BORDER) |
174 | { | |
837e5743 | 175 | windowClass = _T("EDIT"); |
cd471848 | 176 | m_isRich = FALSE; |
2bda0e17 KB |
177 | } |
178 | #endif | |
179 | ||
180 | // Even with extended styles, need to combine with WS_BORDER | |
181 | // for them to look right. | |
c085e333 | 182 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
183 | msStyle |= WS_BORDER; |
184 | ||
c085e333 | 185 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, windowClass, NULL, |
2bda0e17 KB |
186 | msStyle, |
187 | 0, 0, 0, 0, (HWND) ((wxWindow*)parent)->GetHWND(), (HMENU)m_windowId, | |
cd471848 | 188 | wxGetInstance(), NULL); |
2bda0e17 | 189 | |
837e5743 | 190 | wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create text ctrl") ); |
c085e333 | 191 | |
1f112209 | 192 | #if wxUSE_CTL3D |
2bda0e17 KB |
193 | if ( want3D ) |
194 | { | |
c085e333 VZ |
195 | Ctl3dSubclassCtl((HWND)m_hWnd); |
196 | m_useCtl3D = TRUE; | |
2bda0e17 KB |
197 | } |
198 | #endif | |
199 | ||
57c208c5 | 200 | #if wxUSE_RICHEDIT |
2bda0e17 KB |
201 | if (m_isRich) |
202 | { | |
c085e333 VZ |
203 | // Have to enable events |
204 | ::SendMessage((HWND)m_hWnd, EM_SETEVENTMASK, 0, | |
205 | ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE); | |
2bda0e17 KB |
206 | } |
207 | #endif | |
208 | ||
209 | SubclassWin(GetHWND()); | |
210 | ||
c0ed460c | 211 | if ( parent->GetFont().Ok() && parent->GetFont().Ok() ) |
2bda0e17 | 212 | { |
c0ed460c | 213 | SetFont(parent->GetFont()); |
2bda0e17 KB |
214 | } |
215 | else | |
216 | { | |
c085e333 | 217 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT)); |
2bda0e17 KB |
218 | } |
219 | ||
220 | SetSize(x, y, width, height); | |
221 | ||
222 | // Causes a crash for Symantec C++ and WIN32 for some reason | |
223 | #if !(defined(__SC__) && defined(__WIN32__)) | |
c085e333 VZ |
224 | if ( !value.IsEmpty() ) |
225 | { | |
226 | SetValue(value); | |
227 | } | |
2bda0e17 KB |
228 | #endif |
229 | ||
230 | return TRUE; | |
231 | } | |
232 | ||
233 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 234 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 235 | { |
c085e333 | 236 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 237 | |
789295bf | 238 | HWND hWnd = GetHwnd(); |
c085e333 | 239 | long style = GetWindowLong((HWND) hWnd, GWL_STYLE); |
2bda0e17 | 240 | |
cd471848 VZ |
241 | // retrieve the style to see whether this is an edit or richedit ctrl |
242 | #if wxUSE_RICHEDIT | |
837e5743 | 243 | wxChar buf[256]; |
2bda0e17 KB |
244 | |
245 | #ifndef __WIN32__ | |
c085e333 | 246 | GetClassName((HWND) hWnd, buf, 256); |
2bda0e17 KB |
247 | #else |
248 | #ifdef UNICODE | |
c085e333 | 249 | GetClassNameW((HWND) hWnd, buf, 256); |
57c208c5 JS |
250 | #else |
251 | #ifdef __TWIN32__ | |
252 | GetClassName((HWND) hWnd, buf, 256); | |
2bda0e17 | 253 | #else |
c085e333 | 254 | GetClassNameA((HWND) hWnd, buf, 256); |
2bda0e17 | 255 | #endif |
57c208c5 | 256 | #endif |
2bda0e17 KB |
257 | #endif |
258 | ||
c085e333 VZ |
259 | wxString str(buf); |
260 | str.UpperCase(); | |
261 | ||
837e5743 | 262 | if (str == _T("EDIT")) |
c085e333 VZ |
263 | m_isRich = FALSE; |
264 | else | |
265 | m_isRich = TRUE; | |
cd471848 | 266 | #endif |
c085e333 VZ |
267 | |
268 | if (style & ES_MULTILINE) | |
269 | m_windowStyle |= wxTE_MULTILINE; | |
270 | if (style & ES_PASSWORD) | |
271 | m_windowStyle |= wxTE_PASSWORD; | |
272 | if (style & ES_READONLY) | |
273 | m_windowStyle |= wxTE_READONLY; | |
274 | if (style & ES_WANTRETURN) | |
275 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
2bda0e17 KB |
276 | } |
277 | ||
cd471848 | 278 | void wxTextCtrl::SetupColours() |
2bda0e17 KB |
279 | { |
280 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
fd71308f | 281 | SetForegroundColour(GetParent()->GetForegroundColour()); |
2bda0e17 KB |
282 | } |
283 | ||
cd471848 | 284 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 285 | { |
789295bf | 286 | return wxGetWindowText(GetHWND()); |
2bda0e17 KB |
287 | } |
288 | ||
289 | void wxTextCtrl::SetValue(const wxString& value) | |
290 | { | |
291 | // If newlines are denoted by just 10, must stick 13 in front. | |
292 | int singletons = 0; | |
293 | int len = value.Length(); | |
294 | int i; | |
295 | for (i = 0; i < len; i ++) | |
296 | { | |
297 | if ((i > 0) && (value[i] == 10) && (value[i-1] != 13)) | |
298 | singletons ++; | |
299 | } | |
300 | if (singletons > 0) | |
301 | { | |
837e5743 | 302 | wxChar *tmp = new wxChar[len + singletons + 1]; |
2bda0e17 KB |
303 | int j = 0; |
304 | for (i = 0; i < len; i ++) | |
305 | { | |
306 | if ((i > 0) && (value[i] == 10) && (value[i-1] != 13)) | |
307 | { | |
308 | tmp[j] = 13; | |
309 | j ++; | |
310 | } | |
311 | tmp[j] = value[i]; | |
312 | j ++; | |
313 | } | |
314 | tmp[j] = 0; | |
789295bf | 315 | SetWindowText(GetHwnd(), tmp); |
2bda0e17 KB |
316 | delete[] tmp; |
317 | } | |
318 | else | |
837e5743 | 319 | SetWindowText(GetHwnd(), (const wxChar *)value); |
789295bf VZ |
320 | |
321 | AdjustSpaceLimit(); | |
2bda0e17 KB |
322 | } |
323 | ||
bfc6fde4 | 324 | void wxTextCtrl::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
325 | { |
326 | int currentX, currentY; | |
327 | GetPosition(¤tX, ¤tY); | |
328 | int x1 = x; | |
329 | int y1 = y; | |
330 | int w1 = width; | |
331 | int h1 = height; | |
332 | ||
333 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
334 | x1 = currentX; | |
335 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
336 | y1 = currentY; | |
337 | ||
81d66cf3 JS |
338 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
339 | ||
2bda0e17 KB |
340 | int cx; // button font dimensions |
341 | int cy; | |
342 | ||
ce3ed50d | 343 | wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont()); |
2bda0e17 | 344 | |
1c4a764c | 345 | int control_width, control_height, control_x, control_y; |
2bda0e17 KB |
346 | |
347 | // If we're prepared to use the existing size, then... | |
348 | if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) | |
349 | { | |
350 | GetSize(&w1, &h1); | |
351 | } | |
352 | ||
353 | // Deal with default size (using -1 values) | |
354 | if (w1<=0) | |
355 | w1 = DEFAULT_ITEM_WIDTH; | |
356 | ||
1c4a764c VZ |
357 | control_x = x1; |
358 | control_y = y1; | |
359 | control_width = w1; | |
360 | control_height = h1; | |
2bda0e17 KB |
361 | |
362 | // Calculations may have made text size too small | |
363 | if (control_height <= 0) | |
1c4a764c | 364 | control_height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); |
2bda0e17 KB |
365 | |
366 | if (control_width <= 0) | |
1c4a764c | 367 | control_width = DEFAULT_ITEM_WIDTH; |
2bda0e17 | 368 | |
789295bf | 369 | MoveWindow(GetHwnd(), (int)control_x, (int)control_y, |
2bda0e17 | 370 | (int)control_width, (int)control_height, TRUE); |
2bda0e17 KB |
371 | } |
372 | ||
373 | // Clipboard operations | |
cd471848 | 374 | void wxTextCtrl::Copy() |
2bda0e17 | 375 | { |
e702ff0f JS |
376 | if (CanCopy()) |
377 | { | |
789295bf | 378 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
379 | SendMessage(hWnd, WM_COPY, 0, 0L); |
380 | } | |
2bda0e17 KB |
381 | } |
382 | ||
cd471848 | 383 | void wxTextCtrl::Cut() |
2bda0e17 | 384 | { |
e702ff0f JS |
385 | if (CanCut()) |
386 | { | |
789295bf | 387 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
388 | SendMessage(hWnd, WM_CUT, 0, 0L); |
389 | } | |
2bda0e17 KB |
390 | } |
391 | ||
cd471848 | 392 | void wxTextCtrl::Paste() |
2bda0e17 | 393 | { |
e702ff0f JS |
394 | if (CanPaste()) |
395 | { | |
789295bf | 396 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
397 | SendMessage(hWnd, WM_PASTE, 0, 0L); |
398 | } | |
2bda0e17 KB |
399 | } |
400 | ||
debe6624 | 401 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 402 | { |
789295bf | 403 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
404 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); |
405 | } | |
406 | ||
debe6624 | 407 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 408 | { |
789295bf | 409 | HWND hWnd = GetHwnd(); |
2bda0e17 | 410 | #ifdef __WIN32__ |
57c208c5 | 411 | #if wxUSE_RICHEDIT |
2bda0e17 KB |
412 | if ( m_isRich) |
413 | { | |
414 | CHARRANGE range; | |
415 | range.cpMin = pos; | |
416 | range.cpMax = pos; | |
417 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
418 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
419 | } | |
420 | else | |
421 | #endif | |
422 | { | |
423 | SendMessage(hWnd, EM_SETSEL, pos, pos); | |
424 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
425 | } | |
426 | #else | |
427 | SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos)); | |
428 | #endif | |
429 | char *nothing = ""; | |
430 | SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing); | |
431 | } | |
432 | ||
cd471848 | 433 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 KB |
434 | { |
435 | long pos = GetLastPosition(); | |
436 | SetInsertionPoint(pos); | |
437 | } | |
438 | ||
cd471848 | 439 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 440 | { |
57c208c5 | 441 | #if wxUSE_RICHEDIT |
2bda0e17 KB |
442 | if (m_isRich) |
443 | { | |
444 | CHARRANGE range; | |
445 | range.cpMin = 0; | |
446 | range.cpMax = 0; | |
789295bf | 447 | SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); |
2bda0e17 KB |
448 | return range.cpMin; |
449 | } | |
450 | #endif | |
451 | ||
789295bf | 452 | DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
2bda0e17 KB |
453 | return Pos&0xFFFF; |
454 | } | |
455 | ||
cd471848 | 456 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 457 | { |
789295bf | 458 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
459 | |
460 | // Will always return a number > 0 (according to docs) | |
461 | int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L); | |
462 | ||
463 | // This gets the char index for the _beginning_ of the last line | |
464 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L); | |
39136494 | 465 | |
2bda0e17 KB |
466 | // Get number of characters in the last line. We'll add this to the character |
467 | // index for the last line, 1st position. | |
468 | int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L); | |
469 | ||
470 | return (long)(charIndex + lineLength); | |
471 | } | |
472 | ||
debe6624 | 473 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
2bda0e17 | 474 | { |
acbd13a3 | 475 | #if wxUSE_CLIPBOARD |
789295bf | 476 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
477 | long fromChar = from; |
478 | long toChar = to; | |
39136494 | 479 | |
2bda0e17 KB |
480 | // Set selection and remove it |
481 | #ifdef __WIN32__ | |
482 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
483 | #else | |
484 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
485 | #endif | |
486 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
487 | ||
488 | // Now replace with 'value', by pasting. | |
837e5743 | 489 | wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0); |
2bda0e17 KB |
490 | |
491 | // Paste into edit control | |
492 | SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
acbd13a3 JS |
493 | #else |
494 | wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0."); | |
495 | #endif | |
2bda0e17 KB |
496 | } |
497 | ||
debe6624 | 498 | void wxTextCtrl::Remove(long from, long to) |
2bda0e17 | 499 | { |
789295bf | 500 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
501 | long fromChar = from; |
502 | long toChar = to; | |
39136494 | 503 | |
2bda0e17 KB |
504 | // Cut all selected text |
505 | #ifdef __WIN32__ | |
506 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
507 | #else | |
508 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
509 | #endif | |
510 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
511 | } | |
512 | ||
debe6624 | 513 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 514 | { |
789295bf | 515 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
516 | long fromChar = from; |
517 | long toChar = to; | |
518 | // if from and to are both -1, it means | |
519 | // (in wxWindows) that all text should be selected. | |
520 | // This translates into Windows convention | |
521 | if ((from == -1) && (to == -1)) | |
522 | { | |
523 | fromChar = 0; | |
524 | toChar = -1; | |
525 | } | |
39136494 | 526 | |
2bda0e17 KB |
527 | #ifdef __WIN32__ |
528 | SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar); | |
529 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
530 | #else | |
531 | // WPARAM is 0: selection is scrolled into view | |
532 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
533 | #endif | |
534 | } | |
535 | ||
536 | bool wxTextCtrl::LoadFile(const wxString& file) | |
537 | { | |
2432b92d | 538 | if (!wxFileExists(WXSTRINGCAST file)) |
2bda0e17 KB |
539 | return FALSE; |
540 | ||
b823f5a1 | 541 | m_fileName = file; |
2bda0e17 KB |
542 | |
543 | Clear(); | |
544 | ||
fbc535ff | 545 | // ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in); |
837e5743 | 546 | ifstream input(MBSTRINGCAST file.mb_str(wxConvFile), ios::in); |
2bda0e17 KB |
547 | |
548 | if (!input.bad()) | |
549 | { | |
550 | // Previously a SETSEL/REPLACESEL call-pair were done to insert | |
551 | // line by line into the control. Apart from being very slow this | |
552 | // was limited to 32K of text by the external interface presenting | |
553 | // positions as signed shorts. Now load in one chunk... | |
554 | // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits... | |
555 | ||
ce3ed50d JS |
556 | #ifdef __SALFORDC__ |
557 | struct _stat stat_buf; | |
837e5743 | 558 | if (stat(MBSTRINGCAST file.mb_str(wxConvFile), &stat_buf) < 0) |
ce3ed50d JS |
559 | return FALSE; |
560 | #else | |
2bda0e17 | 561 | struct stat stat_buf; |
837e5743 | 562 | if (stat(file.mb_str(wxConvFile), &stat_buf) < 0) |
2bda0e17 | 563 | return FALSE; |
ce3ed50d JS |
564 | #endif |
565 | ||
837e5743 | 566 | // wxChar *tmp_buffer = (wxChar*)farmalloc(stat_buf.st_size+1); |
2bda0e17 KB |
567 | // This may need to be a bigger buffer than the file size suggests, |
568 | // if it's a UNIX file. Give it an extra 1000 just in case. | |
837e5743 OK |
569 | wxChar *tmp_buffer = (wxChar*)farmalloc((size_t)(stat_buf.st_size+1+1000)); |
570 | char *read_buffer = new char[512]; | |
2bda0e17 KB |
571 | long no_lines = 0; |
572 | long pos = 0; | |
573 | while (!input.eof() && input.peek() != EOF) | |
574 | { | |
837e5743 OK |
575 | input.getline(read_buffer, 500); |
576 | int len = strlen(read_buffer); | |
c085e333 VZ |
577 | wxBuffer[len] = 13; |
578 | wxBuffer[len+1] = 10; | |
579 | wxBuffer[len+2] = 0; | |
837e5743 OK |
580 | #if wxUSE_UNICODE |
581 | pos += wxConvCurrent->MB2WC(tmp_buffer+pos, read_buffer, (size_t)-1); | |
582 | #else | |
583 | strcpy(tmp_buffer+pos, read_buffer); | |
584 | pos += strlen(read_buffer); | |
585 | #endif | |
c085e333 | 586 | no_lines++; |
2bda0e17 | 587 | } |
837e5743 | 588 | delete[] read_buffer; |
2bda0e17 | 589 | |
789295bf VZ |
590 | SetWindowText(GetHwnd(), tmp_buffer); |
591 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); | |
2bda0e17 KB |
592 | farfree(tmp_buffer); |
593 | ||
789295bf VZ |
594 | // update the size limit if needed |
595 | AdjustSpaceLimit(); | |
596 | ||
2bda0e17 KB |
597 | return TRUE; |
598 | } | |
599 | return FALSE; | |
600 | } | |
601 | ||
602 | // If file is null, try saved file name first | |
603 | // Returns TRUE if succeeds. | |
604 | bool wxTextCtrl::SaveFile(const wxString& file) | |
605 | { | |
cd471848 | 606 | wxString theFile(file); |
2bda0e17 | 607 | |
837e5743 | 608 | if (theFile == _T("")) |
cd471848 VZ |
609 | theFile = m_fileName; |
610 | ||
837e5743 | 611 | if (theFile == _T("")) |
cd471848 VZ |
612 | return FALSE; |
613 | ||
614 | m_fileName = theFile; | |
615 | ||
837e5743 | 616 | ofstream output(MBSTRINGCAST theFile.mb_str(wxConvFile)); |
cd471848 VZ |
617 | if (output.bad()) |
618 | return FALSE; | |
2bda0e17 KB |
619 | |
620 | // This will only save 64K max | |
789295bf | 621 | unsigned long nbytes = SendMessage(GetHwnd(), WM_GETTEXTLENGTH, 0, 0); |
2bda0e17 | 622 | char *tmp_buffer = (char*)farmalloc((size_t)(nbytes+1)); |
789295bf | 623 | SendMessage(GetHwnd(), WM_GETTEXT, (WPARAM)(nbytes+1), (LPARAM)tmp_buffer); |
2bda0e17 KB |
624 | char *pstr = tmp_buffer; |
625 | ||
cd471848 VZ |
626 | // Convert \r\n to just \n |
627 | while (*pstr) | |
628 | { | |
629 | if (*pstr != '\r') | |
630 | output << *pstr; | |
631 | pstr++; | |
632 | } | |
2bda0e17 KB |
633 | |
634 | farfree(tmp_buffer); | |
789295bf | 635 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
636 | |
637 | return TRUE; | |
638 | } | |
639 | ||
640 | void wxTextCtrl::WriteText(const wxString& text) | |
641 | { | |
642 | // Covert \n to \r\n | |
643 | int len = text.Length(); | |
644 | char *newtext = new char[(len*2)+1]; | |
645 | int i = 0; | |
646 | int j = 0; | |
647 | while (i < len) | |
648 | { | |
649 | if (text[i] == '\n') | |
650 | { | |
651 | newtext[j] = '\r'; | |
652 | j ++; | |
653 | } | |
654 | newtext[j] = text[i]; | |
655 | i ++; | |
656 | j ++; | |
657 | } | |
658 | newtext[j] = 0; | |
789295bf | 659 | SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)newtext); |
2bda0e17 | 660 | delete[] newtext; |
789295bf VZ |
661 | |
662 | AdjustSpaceLimit(); | |
2bda0e17 KB |
663 | } |
664 | ||
5fb9fcfc VZ |
665 | void wxTextCtrl::AppendText(const wxString& text) |
666 | { | |
667 | SetInsertionPointEnd(); | |
668 | WriteText(text); | |
669 | } | |
670 | ||
cd471848 | 671 | void wxTextCtrl::Clear() |
2bda0e17 | 672 | { |
837e5743 | 673 | SetWindowText(GetHwnd(), _T("")); |
2bda0e17 KB |
674 | } |
675 | ||
cd471848 | 676 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 677 | { |
789295bf | 678 | return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0); |
2bda0e17 KB |
679 | } |
680 | ||
681 | // Makes 'unmodified' | |
cd471848 | 682 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 683 | { |
789295bf | 684 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
685 | } |
686 | ||
687 | /* | |
688 | * Some of the following functions are yet to be implemented | |
689 | * | |
690 | */ | |
39136494 | 691 | |
cd471848 | 692 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 693 | { |
789295bf | 694 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
695 | } |
696 | ||
debe6624 | 697 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 698 | { |
789295bf | 699 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
700 | |
701 | // This gets the char index for the _beginning_ of this line | |
702 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0); | |
703 | return (long)(x + charIndex); | |
704 | } | |
705 | ||
debe6624 | 706 | void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 707 | { |
789295bf | 708 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
709 | |
710 | // This gets the line number containing the character | |
711 | int lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0); | |
712 | // This gets the char index for the _beginning_ of this line | |
713 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); | |
714 | // The X position must therefore be the different between pos and charIndex | |
715 | *x = (long)(pos - charIndex); | |
716 | *y = (long)lineNo; | |
717 | } | |
718 | ||
debe6624 | 719 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 720 | { |
789295bf | 721 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
722 | |
723 | // To scroll to a position, we pass the number of lines and characters | |
724 | // to scroll *by*. This means that we need to: | |
725 | // (1) Find the line position of the current line. | |
726 | // (2) Find the line position of pos. | |
727 | // (3) Scroll by (pos - current). | |
728 | // For now, ignore the horizontal scrolling. | |
729 | ||
730 | // Is this where scrolling is relative to - the line containing the caret? | |
731 | // Or is the first visible line??? Try first visible line. | |
732 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
733 | ||
734 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
735 | ||
736 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
39136494 | 737 | |
2bda0e17 KB |
738 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
739 | ||
2bda0e17 | 740 | if (linesToScroll != 0) |
de4d7713 | 741 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
742 | } |
743 | ||
debe6624 | 744 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 KB |
745 | { |
746 | long charIndex = XYToPosition(0, lineNo); | |
789295bf | 747 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
748 | int len = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0); |
749 | return len; | |
750 | } | |
751 | ||
debe6624 | 752 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 753 | { |
789295bf | 754 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
755 | *(WORD *)wxBuffer = 512; |
756 | int noChars = (int)SendMessage(hWnd, EM_GETLINE, (WPARAM)lineNo, (LPARAM)wxBuffer); | |
757 | wxBuffer[noChars] = 0; | |
c085e333 | 758 | return wxString(wxBuffer); |
2bda0e17 KB |
759 | } |
760 | ||
ca8b28f2 JS |
761 | bool wxTextCtrl::CanCopy() const |
762 | { | |
763 | // Can copy if there's a selection | |
764 | long from, to; | |
765 | GetSelection(& from, & to); | |
766 | return (from != to) ; | |
767 | } | |
768 | ||
769 | bool wxTextCtrl::CanCut() const | |
770 | { | |
771 | // Can cut if there's a selection | |
772 | long from, to; | |
773 | GetSelection(& from, & to); | |
774 | return (from != to) ; | |
775 | } | |
776 | ||
777 | bool wxTextCtrl::CanPaste() const | |
778 | { | |
b80ec6db JS |
779 | #if wxUSE_RICHEDIT |
780 | if (m_isRich) | |
781 | { | |
782 | int dataFormat = 0; // 0 == any format | |
789295bf | 783 | return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0); |
b80ec6db JS |
784 | } |
785 | #endif | |
786 | if (!IsEditable()) | |
787 | return FALSE; | |
788 | ||
789 | // Standard edit control: check for straight text on clipboard | |
790 | bool isTextAvailable = FALSE; | |
791 | if (::OpenClipboard((HWND) wxTheApp->GetTopWindow()->GetHWND())) | |
792 | { | |
793 | isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0); | |
794 | ::CloseClipboard(); | |
795 | } | |
796 | return isTextAvailable; | |
ca8b28f2 JS |
797 | } |
798 | ||
799 | // Undo/redo | |
800 | void wxTextCtrl::Undo() | |
801 | { | |
802 | if (CanUndo()) | |
803 | { | |
789295bf | 804 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
805 | } |
806 | } | |
807 | ||
808 | void wxTextCtrl::Redo() | |
809 | { | |
810 | if (CanRedo()) | |
811 | { | |
812 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
789295bf | 813 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
814 | } |
815 | } | |
816 | ||
817 | bool wxTextCtrl::CanUndo() const | |
818 | { | |
789295bf | 819 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); |
ca8b28f2 JS |
820 | } |
821 | ||
822 | bool wxTextCtrl::CanRedo() const | |
823 | { | |
789295bf | 824 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); |
ca8b28f2 JS |
825 | } |
826 | ||
827 | // If the return values from and to are the same, there is no | |
828 | // selection. | |
829 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
830 | { | |
831 | #if wxUSE_RICHEDIT | |
832 | if (m_isRich) | |
833 | { | |
834 | CHARRANGE charRange; | |
789295bf | 835 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange); |
ca8b28f2 JS |
836 | |
837 | *from = charRange.cpMin; | |
838 | *to = charRange.cpMax; | |
839 | ||
840 | return; | |
841 | } | |
842 | #endif | |
843 | DWORD dwStart, dwEnd; | |
b80ec6db JS |
844 | WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position |
845 | LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position | |
ca8b28f2 | 846 | |
789295bf | 847 | ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam); |
ca8b28f2 JS |
848 | |
849 | *from = dwStart; | |
850 | *to = dwEnd; | |
851 | } | |
852 | ||
853 | bool wxTextCtrl::IsEditable() const | |
854 | { | |
789295bf VZ |
855 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
856 | ||
ca8b28f2 JS |
857 | return ((style & ES_READONLY) == 0); |
858 | } | |
39136494 | 859 | |
2bda0e17 KB |
860 | void wxTextCtrl::Command(wxCommandEvent & event) |
861 | { | |
862 | SetValue (event.GetString()); | |
863 | ProcessCommand (event); | |
864 | } | |
865 | ||
866 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
867 | { | |
868 | // By default, load the first file into the text window. | |
869 | if (event.GetNumberOfFiles() > 0) | |
870 | { | |
871 | LoadFile(event.GetFiles()[0]); | |
872 | } | |
873 | } | |
874 | ||
875 | // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of | |
876 | // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers | |
877 | ||
878 | //========================================================================= | |
39136494 | 879 | // Called then the buffer is full (gcc 2.6.3) |
2bda0e17 KB |
880 | // or when "endl" is output (Borland 4.5) |
881 | //========================================================================= | |
882 | // Class declaration using multiple inheritance doesn't work properly for | |
3f1af920 | 883 | // Borland. See note in textctrl.h. |
2bda0e17 KB |
884 | #ifndef NO_TEXT_WINDOW_STREAM |
885 | int wxTextCtrl::overflow(int c) | |
886 | { | |
887 | // Make sure there is a holding area | |
7cf98a65 UU |
888 | // this is not needed in <iostream> usage as it automagically allocates |
889 | // it, but does someone want to emulate it for safety's sake? | |
890 | #if wxUSE_IOSTREAMH | |
2bda0e17 KB |
891 | if ( allocate()==EOF ) |
892 | { | |
2432b92d | 893 | wxLogError("Streambuf allocation failed"); |
2bda0e17 KB |
894 | return EOF; |
895 | } | |
7cf98a65 | 896 | #endif |
39136494 | 897 | |
2bda0e17 KB |
898 | // Verify that there are no characters in get area |
899 | if ( gptr() && gptr() < egptr() ) | |
900 | { | |
901 | wxError("Who's trespassing my get area?","Internal error"); | |
902 | return EOF; | |
903 | } | |
904 | ||
905 | // Reset get area | |
906 | setg(0,0,0); | |
907 | ||
908 | // Make sure there is a put area | |
909 | if ( ! pptr() ) | |
910 | { | |
911 | /* This doesn't seem to be fatal so comment out error message */ | |
912 | // wxError("Put area not opened","Internal error"); | |
7cf98a65 UU |
913 | |
914 | #if wxUSE_IOSTREAMH | |
915 | setp( base(), base() ); | |
916 | #else | |
917 | setp( pbase(), pbase() ); | |
918 | #endif | |
2bda0e17 KB |
919 | } |
920 | ||
921 | // Determine how many characters have been inserted but no consumed | |
922 | int plen = pptr() - pbase(); | |
923 | ||
924 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
925 | // long, but the holding area "may be as small as 1" ??? | |
926 | // And we need an additional \0, so let's keep this inefficient but | |
927 | // safe copy. | |
928 | ||
929 | // If c!=EOF, it is a character that must also be comsumed | |
930 | int xtra = c==EOF? 0 : 1; | |
931 | ||
932 | // Write temporary C-string to wxTextWindow | |
933 | { | |
934 | char *txt = new char[plen+xtra+1]; | |
935 | memcpy(txt, pbase(), plen); | |
936 | txt[plen] = (char)c; // append c | |
937 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
938 | // If the put area already contained \0, output will be truncated there | |
5fb9fcfc | 939 | AppendText(txt); |
2bda0e17 KB |
940 | delete[] txt; |
941 | } | |
942 | ||
943 | // Reset put area | |
944 | setp(pbase(), epptr()); | |
945 | ||
946 | #if defined(__WATCOMC__) | |
947 | return __NOT_EOF; | |
948 | #elif defined(zapeof) // HP-UX (all cfront based?) | |
949 | return zapeof(c); | |
950 | #else | |
951 | return c!=EOF ? c : 0; // this should make everybody happy | |
952 | #endif | |
953 | ||
954 | /* OLD CODE | |
955 | int len = pptr() - pbase(); | |
956 | char *txt = new char[len+1]; | |
957 | strncpy(txt, pbase(), len); | |
958 | txt[len] = '\0'; | |
959 | (*this) << txt; | |
960 | setp(pbase(), epptr()); | |
961 | delete[] txt; | |
962 | return EOF; | |
963 | */ | |
964 | } | |
965 | ||
966 | //========================================================================= | |
967 | // called then "endl" is output (gcc) or then explicit sync is done (Borland) | |
968 | //========================================================================= | |
cd471848 | 969 | int wxTextCtrl::sync() |
2bda0e17 KB |
970 | { |
971 | // Verify that there are no characters in get area | |
972 | if ( gptr() && gptr() < egptr() ) | |
973 | { | |
974 | wxError("Who's trespassing my get area?","Internal error"); | |
975 | return EOF; | |
976 | } | |
977 | ||
978 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
979 | ||
980 | return 0; | |
981 | /* OLD CODE | |
982 | int len = pptr() - pbase(); | |
983 | char *txt = new char[len+1]; | |
984 | strncpy(txt, pbase(), len); | |
985 | txt[len] = '\0'; | |
986 | (*this) << txt; | |
987 | setp(pbase(), epptr()); | |
988 | delete[] txt; | |
989 | return 0; | |
990 | */ | |
991 | } | |
992 | ||
993 | //========================================================================= | |
994 | // Should not be called by a "ostream". Used by a "istream" | |
995 | //========================================================================= | |
cd471848 | 996 | int wxTextCtrl::underflow() |
2bda0e17 KB |
997 | { |
998 | return EOF; | |
999 | } | |
1000 | #endif | |
1001 | ||
1002 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
1003 | { | |
5fb9fcfc VZ |
1004 | AppendText(s); |
1005 | return *this; | |
2bda0e17 KB |
1006 | } |
1007 | ||
debe6624 | 1008 | wxTextCtrl& wxTextCtrl::operator<<(float f) |
2bda0e17 | 1009 | { |
b823f5a1 | 1010 | wxString str; |
837e5743 | 1011 | str.Printf(_T("%.2f"), f); |
5fb9fcfc | 1012 | AppendText(str); |
b823f5a1 | 1013 | return *this; |
2bda0e17 KB |
1014 | } |
1015 | ||
debe6624 | 1016 | wxTextCtrl& wxTextCtrl::operator<<(double d) |
2bda0e17 | 1017 | { |
b823f5a1 | 1018 | wxString str; |
837e5743 | 1019 | str.Printf(_T("%.2f"), d); |
5fb9fcfc | 1020 | AppendText(str); |
b823f5a1 | 1021 | return *this; |
2bda0e17 KB |
1022 | } |
1023 | ||
debe6624 | 1024 | wxTextCtrl& wxTextCtrl::operator<<(int i) |
2bda0e17 | 1025 | { |
b823f5a1 | 1026 | wxString str; |
837e5743 | 1027 | str.Printf(_T("%d"), i); |
5fb9fcfc | 1028 | AppendText(str); |
b823f5a1 | 1029 | return *this; |
2bda0e17 KB |
1030 | } |
1031 | ||
debe6624 | 1032 | wxTextCtrl& wxTextCtrl::operator<<(long i) |
2bda0e17 | 1033 | { |
b823f5a1 | 1034 | wxString str; |
837e5743 | 1035 | str.Printf(_T("%ld"), i); |
5fb9fcfc | 1036 | AppendText(str); |
b823f5a1 | 1037 | return *this; |
2bda0e17 KB |
1038 | } |
1039 | ||
1040 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
1041 | { | |
b823f5a1 | 1042 | char buf[2]; |
2bda0e17 | 1043 | |
b823f5a1 JS |
1044 | buf[0] = c; |
1045 | buf[1] = 0; | |
5fb9fcfc | 1046 | AppendText(buf); |
b823f5a1 | 1047 | return *this; |
2bda0e17 KB |
1048 | } |
1049 | ||
debe6624 | 1050 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
c085e333 | 1051 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
2bda0e17 | 1052 | { |
1f112209 | 1053 | #if wxUSE_CTL3D |
2bda0e17 KB |
1054 | if ( m_useCtl3D ) |
1055 | { | |
1056 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
1057 | return (WXHBRUSH) hbrush; | |
1058 | } | |
1059 | #endif | |
1060 | ||
1061 | if (GetParent()->GetTransparentBackground()) | |
1062 | SetBkMode((HDC) pDC, TRANSPARENT); | |
1063 | else | |
1064 | SetBkMode((HDC) pDC, OPAQUE); | |
1065 | ||
1066 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
1067 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
1068 | ||
1069 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
1070 | ||
1071 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
1072 | // has a zero usage count. | |
1073 | // NOT NOW - will be cleaned up at end of app. | |
1074 | // backgroundBrush->RealizeResource(); | |
1075 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
1076 | } | |
1077 | ||
1078 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
1079 | { | |
42e69d6b | 1080 | switch ( event.KeyCode() ) |
cd471848 | 1081 | { |
cd471848 | 1082 | case WXK_RETURN: |
39136494 | 1083 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
1084 | { |
1085 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1086 | event.SetEventObject( this ); | |
1087 | if ( GetEventHandler()->ProcessEvent(event) ) | |
1088 | return; | |
1089 | } | |
5fb9fcfc VZ |
1090 | //else: multiline controls need Enter for themselves |
1091 | ||
1092 | break; | |
4d91c1d1 | 1093 | |
cd471848 | 1094 | case WXK_TAB: |
5fb9fcfc VZ |
1095 | // only produce navigation event if we don't process TAB ourself or |
1096 | // if it's a Shift-Tab keypress (we assume nobody will ever need | |
1097 | // this key combo for himself) | |
1098 | // | |
1099 | // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is | |
1100 | // handled by Windows | |
1101 | if ( event.ShiftDown() || !(m_windowStyle & wxTE_PROCESS_TAB) ) | |
cd471848 | 1102 | { |
5fb9fcfc VZ |
1103 | wxNavigationKeyEvent eventNav; |
1104 | eventNav.SetDirection(!event.ShiftDown()); | |
1105 | eventNav.SetWindowChange(FALSE); | |
1106 | eventNav.SetEventObject(this); | |
39136494 | 1107 | |
5fb9fcfc | 1108 | if ( GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
1109 | return; |
1110 | } | |
341c92a8 | 1111 | break; |
4d91c1d1 VZ |
1112 | |
1113 | default: | |
1114 | event.Skip(); | |
39136494 | 1115 | return; |
cd471848 | 1116 | } |
39136494 | 1117 | |
cd471848 VZ |
1118 | // don't just call event.Skip() because this will cause TABs and ENTERs |
1119 | // be passed upwards and we don't always want this - instead process it | |
1120 | // right here | |
42e69d6b VZ |
1121 | |
1122 | // FIXME | |
1123 | event.Skip(); | |
2bda0e17 KB |
1124 | } |
1125 | ||
ae29de83 | 1126 | long wxTextCtrl::MSWGetDlgCode() |
2bda0e17 | 1127 | { |
cd471848 VZ |
1128 | // we always want the characters and the arrows |
1129 | long lRc = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
1130 | ||
1131 | // we may have several different cases: | |
1132 | // 1. normal case: both TAB and ENTER are used for dialog navigation | |
1133 | // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next | |
1134 | // control in the dialog | |
1135 | // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation | |
1136 | // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to | |
1137 | // the next control | |
1138 | if ( m_windowStyle & wxTE_PROCESS_ENTER ) | |
1139 | lRc |= DLGC_WANTMESSAGE; | |
1140 | if ( m_windowStyle & wxTE_PROCESS_TAB ) | |
1141 | lRc |= DLGC_WANTTAB; | |
39136494 | 1142 | |
cd471848 | 1143 | return lRc; |
ae29de83 VZ |
1144 | } |
1145 | ||
debe6624 | 1146 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 1147 | { |
789295bf VZ |
1148 | switch (param) |
1149 | { | |
1150 | case EN_SETFOCUS: | |
1151 | case EN_KILLFOCUS: | |
1152 | { | |
1153 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
1154 | : wxEVT_SET_FOCUS, | |
1155 | m_windowId); | |
1156 | event.SetEventObject( this ); | |
1157 | GetEventHandler()->ProcessEvent(event); | |
1158 | } | |
1159 | break; | |
ae29de83 | 1160 | |
789295bf VZ |
1161 | case EN_CHANGE: |
1162 | { | |
1163 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
1164 | wxString val(GetValue()); | |
1165 | if ( !val.IsNull() ) | |
1166 | event.m_commandString = WXSTRINGCAST val; | |
1167 | event.SetEventObject( this ); | |
1168 | ProcessCommand(event); | |
1169 | } | |
1170 | break; | |
2bda0e17 | 1171 | |
789295bf VZ |
1172 | case EN_ERRSPACE: |
1173 | // the text size limit has been hit - increase it | |
1174 | AdjustSpaceLimit(); | |
1175 | break; | |
1176 | ||
1177 | // the other notification messages are not processed | |
1178 | case EN_UPDATE: | |
1179 | case EN_MAXTEXT: | |
1180 | case EN_HSCROLL: | |
1181 | case EN_VSCROLL: | |
1182 | default: | |
1183 | return FALSE; | |
1184 | } | |
1185 | ||
1186 | // processed | |
1187 | return TRUE; | |
2bda0e17 KB |
1188 | } |
1189 | ||
789295bf VZ |
1190 | void wxTextCtrl::AdjustSpaceLimit() |
1191 | { | |
25889d3c | 1192 | #ifndef __WIN16__ |
789295bf VZ |
1193 | unsigned int len = ::GetWindowTextLength(GetHwnd()), |
1194 | limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); | |
1195 | if ( len > limit ) | |
1196 | { | |
1197 | limit = len + 0x8000; // 32Kb | |
1198 | ||
5ea105e0 | 1199 | #if wxUSE_RICHEDIT |
789295bf | 1200 | if ( m_isRich || limit > 0xffff ) |
5ea105e0 RR |
1201 | #else |
1202 | if ( limit > 0xffff ) | |
1203 | #endif | |
789295bf VZ |
1204 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit); |
1205 | else | |
1206 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); | |
1207 | } | |
25889d3c | 1208 | #endif |
789295bf | 1209 | } |
2bda0e17 KB |
1210 | |
1211 | // For Rich Edit controls. Do we need it? | |
1212 | #if 0 | |
57c208c5 | 1213 | #if wxUSE_RICHEDIT |
a23fd0e1 | 1214 | bool wxTextCtrl::MSWOnNotify(WXWPARAM wParam, WXLPARAM lParam) |
2bda0e17 | 1215 | { |
c085e333 VZ |
1216 | wxCommandEvent event(0, m_windowId); |
1217 | int eventType = 0; | |
1218 | NMHDR *hdr1 = (NMHDR *) lParam; | |
1219 | switch ( hdr1->code ) | |
1220 | { | |
1221 | // Insert case code here | |
1222 | default : | |
a23fd0e1 | 1223 | return wxControl::MSWOnNotify(wParam, lParam); |
c085e333 VZ |
1224 | break; |
1225 | } | |
1226 | ||
1227 | event.SetEventObject( this ); | |
1228 | event.SetEventType(eventType); | |
1229 | ||
02800301 | 1230 | if ( !GetEventHandler()->ProcessEvent(event) ) |
c085e333 VZ |
1231 | return FALSE; |
1232 | ||
1233 | return TRUE; | |
2bda0e17 KB |
1234 | } |
1235 | #endif | |
1236 | #endif | |
1237 | ||
e702ff0f JS |
1238 | void wxTextCtrl::OnCut(wxCommandEvent& event) |
1239 | { | |
1240 | Cut(); | |
1241 | } | |
1242 | ||
1243 | void wxTextCtrl::OnCopy(wxCommandEvent& event) | |
1244 | { | |
1245 | Copy(); | |
1246 | } | |
1247 | ||
1248 | void wxTextCtrl::OnPaste(wxCommandEvent& event) | |
1249 | { | |
1250 | Paste(); | |
1251 | } | |
1252 | ||
1253 | void wxTextCtrl::OnUndo(wxCommandEvent& event) | |
1254 | { | |
1255 | Undo(); | |
1256 | } | |
1257 | ||
1258 | void wxTextCtrl::OnRedo(wxCommandEvent& event) | |
1259 | { | |
1260 | Redo(); | |
1261 | } | |
1262 | ||
1263 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1264 | { | |
1265 | event.Enable( CanCut() ); | |
1266 | } | |
1267 | ||
1268 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1269 | { | |
1270 | event.Enable( CanCopy() ); | |
1271 | } | |
1272 | ||
1273 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1274 | { | |
1275 | event.Enable( CanPaste() ); | |
1276 | } | |
1277 | ||
1278 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1279 | { | |
1280 | event.Enable( CanUndo() ); | |
1281 | } | |
1282 | ||
1283 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1284 | { | |
1285 | event.Enable( CanRedo() ); | |
1286 | } | |
1287 |