]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows license | |
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" | |
26 | #endif | |
27 | ||
28 | #if USE_CLIPBOARD | |
29 | #include "wx/app.h" | |
30 | #include "wx/clipbrd.h" | |
31 | #endif | |
32 | ||
33 | #include "wx/msw/private.h" | |
34 | ||
35 | #include <windows.h> | |
36 | #include <stdlib.h> | |
37 | #include "fstream.h" | |
38 | ||
39 | #include <sys/types.h> | |
40 | #include <sys/stat.h> | |
41 | #if defined(__BORLANDC__) && !defined(__WIN32__) | |
42 | #include <alloc.h> | |
43 | #else | |
44 | #ifndef __GNUWIN32__ | |
45 | #include <malloc.h> | |
46 | #endif | |
47 | #define farmalloc malloc | |
48 | #define farfree free | |
49 | #endif | |
50 | #include <windowsx.h> | |
51 | ||
52 | #include <string.h> | |
53 | ||
54 | #if defined(__WIN95__) && !defined(__GNUWIN32__) | |
55 | #include <richedit.h> | |
56 | #endif | |
57 | ||
58 | #if !USE_SHARED_LIBRARY | |
59 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
60 | ||
61 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
62 | EVT_CHAR(wxTextCtrl::OnChar) | |
63 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
64 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
65 | END_EVENT_TABLE() | |
66 | ||
67 | #endif | |
68 | ||
69 | // Text item | |
70 | wxTextCtrl::wxTextCtrl(void) | |
71 | #ifndef NO_TEXT_WINDOW_STREAM | |
72 | :streambuf() | |
73 | #endif | |
74 | { | |
75 | fileName = ""; | |
76 | m_isRich = FALSE; | |
77 | } | |
78 | ||
79 | bool wxTextCtrl::Create(wxWindow *parent, const wxWindowID id, | |
80 | const wxString& value, | |
81 | const wxPoint& pos, | |
82 | const wxSize& size, const long style, | |
83 | const wxValidator& validator, | |
84 | const wxString& name) | |
85 | { | |
86 | fileName = ""; | |
87 | SetName(name); | |
88 | SetValidator(validator); | |
89 | if (parent) parent->AddChild(this); | |
90 | ||
91 | m_windowStyle = style; | |
92 | ||
93 | // Should this be taken from the system colours? | |
94 | // SetBackgroundColour(wxColour(255, 255, 255)); | |
95 | ||
96 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
97 | ||
98 | SetForegroundColour(parent->GetDefaultForegroundColour()) ; | |
99 | ||
100 | if ( id == -1 ) | |
101 | m_windowId = (int)NewControlId(); | |
102 | else | |
103 | m_windowId = id; | |
104 | ||
105 | int x = pos.x; | |
106 | int y = pos.y; | |
107 | int width = size.x; | |
108 | int height = size.y; | |
109 | ||
110 | #ifdef __WIN32__ | |
111 | WXHGLOBAL m_globalHandle = 0; | |
112 | #else | |
113 | // Obscure method from the MS Developer's Network Disk for | |
114 | // using global memory instead of the local heap, which | |
115 | // runs out far too soon. Solves the problem with | |
116 | // failing to appear. | |
117 | ||
118 | // Doesn't seem to work for Win95, so removing. | |
119 | m_globalHandle=0; | |
120 | // if ((wxGetOsVersion() != wxWINDOWS_NT) && (wxGetOsVersion() != wxWIN95)) | |
121 | // m_globalHandle = (WXHGLOBAL) GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, | |
122 | // 256L); | |
123 | #endif | |
124 | long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP; | |
125 | if (m_windowStyle & wxTE_MULTILINE) | |
126 | msStyle |= ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL ; // WS_BORDER | |
127 | else | |
128 | msStyle |= ES_AUTOHSCROLL ; | |
129 | ||
130 | if (m_windowStyle & wxTE_READONLY) | |
131 | msStyle |= ES_READONLY; | |
132 | ||
133 | if (m_windowStyle & wxHSCROLL) | |
134 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL) ; | |
135 | if (m_windowStyle & wxTE_PASSWORD) // hidden input | |
136 | msStyle |= ES_PASSWORD; | |
137 | ||
138 | char *windowClass = "EDIT"; | |
139 | #if defined(__WIN95__) | |
140 | if ( m_windowStyle & wxTE_MULTILINE ) | |
141 | #else | |
142 | if ( FALSE ) | |
143 | #endif | |
144 | { | |
145 | msStyle |= ES_AUTOVSCROLL; | |
146 | m_isRich = TRUE; | |
147 | windowClass = "RichEdit" ; | |
148 | } | |
149 | else | |
150 | m_isRich = FALSE; | |
151 | ||
152 | bool want3D; | |
153 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
154 | ||
155 | // If we're in Win95, and we want a simple 2D border, | |
156 | // then make it an EDIT control instead. | |
157 | #if defined(__WIN95__) | |
158 | if (m_windowStyle & wxSIMPLE_BORDER) | |
159 | { | |
160 | windowClass = "EDIT"; | |
161 | m_isRich = FALSE; | |
162 | } | |
163 | #endif | |
164 | ||
165 | // Even with extended styles, need to combine with WS_BORDER | |
166 | // for them to look right. | |
167 | if (want3D || (m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) || | |
168 | (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER)) | |
169 | msStyle |= WS_BORDER; | |
170 | ||
171 | HWND edit = CreateWindowEx(exStyle, windowClass, NULL, | |
172 | msStyle, | |
173 | 0, 0, 0, 0, (HWND) ((wxWindow*)parent)->GetHWND(), (HMENU)m_windowId, | |
174 | m_globalHandle ? (HANDLE) m_globalHandle : wxGetInstance(), NULL); | |
175 | ||
176 | #if CTL3D | |
177 | if ( want3D ) | |
178 | { | |
179 | Ctl3dSubclassCtl(edit); | |
180 | m_useCtl3D = TRUE; | |
181 | } | |
182 | #endif | |
183 | ||
184 | m_hWnd = (WXHWND)edit; | |
185 | ||
186 | #if defined(__WIN95__) | |
187 | if (m_isRich) | |
188 | { | |
189 | // Have to enable events | |
190 | ::SendMessage(edit, EM_SETEVENTMASK, 0, ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE); | |
191 | } | |
192 | #endif | |
193 | ||
194 | SubclassWin(GetHWND()); | |
195 | ||
196 | if ( parent->GetFont() && parent->GetFont()->Ok() ) | |
197 | { | |
198 | SetFont(* parent->GetFont()); | |
199 | } | |
200 | else | |
201 | { | |
202 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT)); | |
203 | } | |
204 | ||
205 | SetSize(x, y, width, height); | |
206 | ||
207 | // Causes a crash for Symantec C++ and WIN32 for some reason | |
208 | #if !(defined(__SC__) && defined(__WIN32__)) | |
209 | if (value != "") | |
210 | SetWindowText(edit, (const char *)value); | |
211 | #endif | |
212 | ||
213 | return TRUE; | |
214 | } | |
215 | ||
216 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
217 | void wxTextCtrl::AdoptAttributesFromHWND(void) | |
218 | { | |
219 | wxWindow::AdoptAttributesFromHWND(); | |
220 | ||
221 | HWND hWnd = (HWND) GetHWND(); | |
222 | long style = GetWindowLong((HWND) hWnd, GWL_STYLE); | |
223 | ||
224 | char buf[256]; | |
225 | ||
226 | #ifndef __WIN32__ | |
227 | GetClassName((HWND) hWnd, buf, 256); | |
228 | #else | |
229 | #ifdef UNICODE | |
230 | GetClassNameW((HWND) hWnd, buf, 256); | |
231 | #else | |
232 | GetClassNameA((HWND) hWnd, buf, 256); | |
233 | #endif | |
234 | #endif | |
235 | ||
236 | wxString str(buf); | |
237 | str.UpperCase(); | |
238 | ||
239 | if (str == "EDIT") | |
240 | m_isRich = FALSE; | |
241 | else | |
242 | m_isRich = TRUE; | |
243 | ||
244 | if (style & ES_MULTILINE) | |
245 | m_windowStyle |= wxTE_MULTILINE; | |
246 | if (style & ES_PASSWORD) | |
247 | m_windowStyle |= wxTE_PASSWORD; | |
248 | if (style & ES_READONLY) | |
249 | m_windowStyle |= wxTE_READONLY; | |
250 | if (style & ES_WANTRETURN) | |
251 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
252 | } | |
253 | ||
254 | void wxTextCtrl::SetupColours(void) | |
255 | { | |
256 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
257 | SetForegroundColour(GetParent()->GetDefaultForegroundColour()); | |
258 | } | |
259 | ||
260 | wxString wxTextCtrl::GetValue(void) const | |
261 | { | |
262 | int length = GetWindowTextLength((HWND) GetHWND()); | |
263 | char *s = new char[length+1]; | |
264 | GetWindowText((HWND) GetHWND(), s, length+1); | |
265 | wxString str(s); | |
266 | delete[] s; | |
267 | return str; | |
268 | } | |
269 | ||
270 | void wxTextCtrl::SetValue(const wxString& value) | |
271 | { | |
272 | // If newlines are denoted by just 10, must stick 13 in front. | |
273 | int singletons = 0; | |
274 | int len = value.Length(); | |
275 | int i; | |
276 | for (i = 0; i < len; i ++) | |
277 | { | |
278 | if ((i > 0) && (value[i] == 10) && (value[i-1] != 13)) | |
279 | singletons ++; | |
280 | } | |
281 | if (singletons > 0) | |
282 | { | |
283 | char *tmp = new char[len + singletons + 1]; | |
284 | int j = 0; | |
285 | for (i = 0; i < len; i ++) | |
286 | { | |
287 | if ((i > 0) && (value[i] == 10) && (value[i-1] != 13)) | |
288 | { | |
289 | tmp[j] = 13; | |
290 | j ++; | |
291 | } | |
292 | tmp[j] = value[i]; | |
293 | j ++; | |
294 | } | |
295 | tmp[j] = 0; | |
296 | SetWindowText((HWND) GetHWND(), tmp); | |
297 | delete[] tmp; | |
298 | } | |
299 | else | |
300 | SetWindowText((HWND) GetHWND(), (const char *)value); | |
301 | } | |
302 | ||
303 | void wxTextCtrl::SetSize(const int x, const int y, const int width, const int height, const int sizeFlags) | |
304 | { | |
305 | int currentX, currentY; | |
306 | GetPosition(¤tX, ¤tY); | |
307 | int x1 = x; | |
308 | int y1 = y; | |
309 | int w1 = width; | |
310 | int h1 = height; | |
311 | ||
312 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
313 | x1 = currentX; | |
314 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
315 | y1 = currentY; | |
316 | ||
317 | int cx; // button font dimensions | |
318 | int cy; | |
319 | ||
320 | wxGetCharSize(GetHWND(), &cx, &cy,GetFont()); | |
321 | ||
322 | float control_width, control_height, control_x, control_y; | |
323 | ||
324 | // If we're prepared to use the existing size, then... | |
325 | if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) | |
326 | { | |
327 | GetSize(&w1, &h1); | |
328 | } | |
329 | ||
330 | // Deal with default size (using -1 values) | |
331 | if (w1<=0) | |
332 | w1 = DEFAULT_ITEM_WIDTH; | |
333 | ||
334 | control_x = (float)x1; | |
335 | control_y = (float)y1; | |
336 | control_width = (float)w1; | |
337 | control_height = (float)h1; | |
338 | ||
339 | // Calculations may have made text size too small | |
340 | if (control_height <= 0) | |
341 | control_height = (float)(int)(cy*EDIT_CONTROL_FACTOR) ; | |
342 | ||
343 | if (control_width <= 0) | |
344 | control_width = (float)DEFAULT_ITEM_WIDTH; | |
345 | ||
346 | MoveWindow((HWND) GetHWND(), (int)control_x, (int)control_y, | |
347 | (int)control_width, (int)control_height, TRUE); | |
348 | /* | |
349 | #if WXWIN_COMPATIBILITY | |
350 | GetEventHandler()->OldOnSize(width, height); | |
351 | #else | |
352 | wxSizeEvent event(wxSize(width, height), m_windowId); | |
353 | event.eventObject = this; | |
354 | GetEventHandler()->ProcessEvent(event); | |
355 | #endif | |
356 | */ | |
357 | } | |
358 | ||
359 | // Clipboard operations | |
360 | void wxTextCtrl::Copy(void) | |
361 | { | |
362 | HWND hWnd = (HWND) GetHWND(); | |
363 | SendMessage(hWnd, WM_COPY, 0, 0L); | |
364 | } | |
365 | ||
366 | void wxTextCtrl::Cut(void) | |
367 | { | |
368 | HWND hWnd = (HWND) GetHWND(); | |
369 | SendMessage(hWnd, WM_CUT, 0, 0L); | |
370 | } | |
371 | ||
372 | void wxTextCtrl::Paste(void) | |
373 | { | |
374 | HWND hWnd = (HWND) GetHWND(); | |
375 | SendMessage(hWnd, WM_PASTE, 0, 0L); | |
376 | } | |
377 | ||
378 | void wxTextCtrl::SetEditable(const bool editable) | |
379 | { | |
380 | HWND hWnd = (HWND) GetHWND(); | |
381 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
382 | } | |
383 | ||
384 | void wxTextCtrl::SetInsertionPoint(const long pos) | |
385 | { | |
386 | HWND hWnd = (HWND) GetHWND(); | |
387 | #ifdef __WIN32__ | |
388 | #if defined(__WIN95__) | |
389 | if ( m_isRich) | |
390 | { | |
391 | CHARRANGE range; | |
392 | range.cpMin = pos; | |
393 | range.cpMax = pos; | |
394 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
395 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
396 | } | |
397 | else | |
398 | #endif | |
399 | { | |
400 | SendMessage(hWnd, EM_SETSEL, pos, pos); | |
401 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
402 | } | |
403 | #else | |
404 | SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos)); | |
405 | #endif | |
406 | char *nothing = ""; | |
407 | SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing); | |
408 | } | |
409 | ||
410 | void wxTextCtrl::SetInsertionPointEnd(void) | |
411 | { | |
412 | long pos = GetLastPosition(); | |
413 | SetInsertionPoint(pos); | |
414 | } | |
415 | ||
416 | long wxTextCtrl::GetInsertionPoint(void) const | |
417 | { | |
418 | #if defined(__WIN95__) | |
419 | if (m_isRich) | |
420 | { | |
421 | CHARRANGE range; | |
422 | range.cpMin = 0; | |
423 | range.cpMax = 0; | |
424 | SendMessage((HWND) GetHWND(), EM_EXGETSEL, 0, (LPARAM) &range); | |
425 | return range.cpMin; | |
426 | } | |
427 | #endif | |
428 | ||
429 | DWORD Pos=(DWORD)SendMessage((HWND) GetHWND(), EM_GETSEL, 0, 0L); | |
430 | return Pos&0xFFFF; | |
431 | } | |
432 | ||
433 | long wxTextCtrl::GetLastPosition(void) const | |
434 | { | |
435 | HWND hWnd = (HWND) GetHWND(); | |
436 | ||
437 | // Will always return a number > 0 (according to docs) | |
438 | int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L); | |
439 | ||
440 | // This gets the char index for the _beginning_ of the last line | |
441 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L); | |
442 | ||
443 | // Get number of characters in the last line. We'll add this to the character | |
444 | // index for the last line, 1st position. | |
445 | int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L); | |
446 | ||
447 | return (long)(charIndex + lineLength); | |
448 | } | |
449 | ||
450 | void wxTextCtrl::Replace(const long from, const long to, const wxString& value) | |
451 | { | |
452 | HWND hWnd = (HWND) GetHWND(); | |
453 | long fromChar = from; | |
454 | long toChar = to; | |
455 | ||
456 | // Set selection and remove it | |
457 | #ifdef __WIN32__ | |
458 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
459 | #else | |
460 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
461 | #endif | |
462 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
463 | ||
464 | // Now replace with 'value', by pasting. | |
465 | wxSetClipboardData(wxCF_TEXT, (wxObject *) (const char *)value, 0, 0); | |
466 | ||
467 | // Paste into edit control | |
468 | SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
469 | } | |
470 | ||
471 | void wxTextCtrl::Remove(const long from, const long to) | |
472 | { | |
473 | HWND hWnd = (HWND) GetHWND(); | |
474 | long fromChar = from; | |
475 | long toChar = to; | |
476 | ||
477 | // Cut all selected text | |
478 | #ifdef __WIN32__ | |
479 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
480 | #else | |
481 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
482 | #endif | |
483 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
484 | } | |
485 | ||
486 | void wxTextCtrl::SetSelection(const long from, const long to) | |
487 | { | |
488 | HWND hWnd = (HWND) GetHWND(); | |
489 | long fromChar = from; | |
490 | long toChar = to; | |
491 | // if from and to are both -1, it means | |
492 | // (in wxWindows) that all text should be selected. | |
493 | // This translates into Windows convention | |
494 | if ((from == -1) && (to == -1)) | |
495 | { | |
496 | fromChar = 0; | |
497 | toChar = -1; | |
498 | } | |
499 | ||
500 | #ifdef __WIN32__ | |
501 | SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar); | |
502 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
503 | #else | |
504 | // WPARAM is 0: selection is scrolled into view | |
505 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
506 | #endif | |
507 | } | |
508 | ||
509 | bool wxTextCtrl::LoadFile(const wxString& file) | |
510 | { | |
511 | if (!FileExists(WXSTRINGCAST file)) | |
512 | return FALSE; | |
513 | ||
514 | fileName = file; | |
515 | ||
516 | Clear(); | |
517 | ||
518 | ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in); | |
519 | ||
520 | if (!input.bad()) | |
521 | { | |
522 | // Previously a SETSEL/REPLACESEL call-pair were done to insert | |
523 | // line by line into the control. Apart from being very slow this | |
524 | // was limited to 32K of text by the external interface presenting | |
525 | // positions as signed shorts. Now load in one chunk... | |
526 | // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits... | |
527 | ||
528 | struct stat stat_buf; | |
529 | if (stat(file, &stat_buf) < 0) | |
530 | return FALSE; | |
531 | // char *tmp_buffer = (char*)farmalloc(stat_buf.st_size+1); | |
532 | // This may need to be a bigger buffer than the file size suggests, | |
533 | // if it's a UNIX file. Give it an extra 1000 just in case. | |
534 | char *tmp_buffer = (char*)farmalloc((size_t)(stat_buf.st_size+1+1000)); | |
535 | long no_lines = 0; | |
536 | long pos = 0; | |
537 | while (!input.eof() && input.peek() != EOF) | |
538 | { | |
539 | input.getline(wxBuffer, 500); | |
540 | int len = strlen(wxBuffer); | |
541 | wxBuffer[len] = 13; | |
542 | wxBuffer[len+1] = 10; | |
543 | wxBuffer[len+2] = 0; | |
544 | strcpy(tmp_buffer+pos, wxBuffer); | |
545 | pos += strlen(wxBuffer); | |
546 | no_lines++; | |
547 | } | |
548 | ||
549 | // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)tmp_buffer); | |
550 | SetWindowText((HWND) GetHWND(), tmp_buffer); | |
551 | SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L); | |
552 | farfree(tmp_buffer); | |
553 | ||
554 | return TRUE; | |
555 | } | |
556 | return FALSE; | |
557 | } | |
558 | ||
559 | // If file is null, try saved file name first | |
560 | // Returns TRUE if succeeds. | |
561 | bool wxTextCtrl::SaveFile(const wxString& file) | |
562 | { | |
563 | wxString theFile; | |
564 | if (file == "") | |
565 | theFile = fileName; | |
566 | if (file == "") | |
567 | return FALSE; | |
568 | fileName = theFile; | |
569 | ||
570 | ofstream output(WXSTRINGCAST file); | |
571 | if (output.bad()) | |
572 | return FALSE; | |
573 | ||
574 | // This will only save 64K max | |
575 | unsigned long nbytes = SendMessage((HWND) GetHWND(), WM_GETTEXTLENGTH, 0, 0); | |
576 | char *tmp_buffer = (char*)farmalloc((size_t)(nbytes+1)); | |
577 | SendMessage((HWND) GetHWND(), WM_GETTEXT, (WPARAM)(nbytes+1), (LPARAM)tmp_buffer); | |
578 | char *pstr = tmp_buffer; | |
579 | ||
580 | // Convert \r\n to just \n | |
581 | while (*pstr) | |
582 | { | |
583 | if (*pstr != '\r') | |
584 | output << *pstr; | |
585 | pstr++; | |
586 | } | |
587 | ||
588 | farfree(tmp_buffer); | |
589 | SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L); | |
590 | ||
591 | return TRUE; | |
592 | } | |
593 | ||
594 | void wxTextCtrl::WriteText(const wxString& text) | |
595 | { | |
596 | // Covert \n to \r\n | |
597 | int len = text.Length(); | |
598 | char *newtext = new char[(len*2)+1]; | |
599 | int i = 0; | |
600 | int j = 0; | |
601 | while (i < len) | |
602 | { | |
603 | if (text[i] == '\n') | |
604 | { | |
605 | newtext[j] = '\r'; | |
606 | j ++; | |
607 | } | |
608 | newtext[j] = text[i]; | |
609 | i ++; | |
610 | j ++; | |
611 | } | |
612 | newtext[j] = 0; | |
613 | SendMessage((HWND) GetHWND(), EM_REPLACESEL, 0, (LPARAM)newtext); | |
614 | delete[] newtext; | |
615 | } | |
616 | ||
617 | void wxTextCtrl::Clear(void) | |
618 | { | |
619 | // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)""); | |
620 | SetWindowText((HWND) GetHWND(), ""); | |
621 | } | |
622 | ||
623 | bool wxTextCtrl::IsModified(void) const | |
624 | { | |
625 | return (SendMessage((HWND) GetHWND(), EM_GETMODIFY, 0, 0) != 0); | |
626 | } | |
627 | ||
628 | // Makes 'unmodified' | |
629 | void wxTextCtrl::DiscardEdits(void) | |
630 | { | |
631 | SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L); | |
632 | } | |
633 | ||
634 | /* | |
635 | * Some of the following functions are yet to be implemented | |
636 | * | |
637 | */ | |
638 | ||
639 | int wxTextCtrl::GetNumberOfLines(void) const | |
640 | { | |
641 | return (int)SendMessage((HWND) GetHWND(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); | |
642 | } | |
643 | ||
644 | long wxTextCtrl::XYToPosition(const long x, const long y) const | |
645 | { | |
646 | HWND hWnd = (HWND) GetHWND(); | |
647 | ||
648 | // This gets the char index for the _beginning_ of this line | |
649 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0); | |
650 | return (long)(x + charIndex); | |
651 | } | |
652 | ||
653 | void wxTextCtrl::PositionToXY(const long pos, long *x, long *y) const | |
654 | { | |
655 | HWND hWnd = (HWND) GetHWND(); | |
656 | ||
657 | // This gets the line number containing the character | |
658 | int lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0); | |
659 | // This gets the char index for the _beginning_ of this line | |
660 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); | |
661 | // The X position must therefore be the different between pos and charIndex | |
662 | *x = (long)(pos - charIndex); | |
663 | *y = (long)lineNo; | |
664 | } | |
665 | ||
666 | void wxTextCtrl::ShowPosition(const long pos) | |
667 | { | |
668 | HWND hWnd = (HWND) GetHWND(); | |
669 | ||
670 | // To scroll to a position, we pass the number of lines and characters | |
671 | // to scroll *by*. This means that we need to: | |
672 | // (1) Find the line position of the current line. | |
673 | // (2) Find the line position of pos. | |
674 | // (3) Scroll by (pos - current). | |
675 | // For now, ignore the horizontal scrolling. | |
676 | ||
677 | // Is this where scrolling is relative to - the line containing the caret? | |
678 | // Or is the first visible line??? Try first visible line. | |
679 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
680 | ||
681 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
682 | ||
683 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
684 | ||
685 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; | |
686 | ||
687 | /* | |
688 | wxDebugMsg("Caret line: %d; Current visible line: %d; Specified line: %d; lines to scroll: %d\n", | |
689 | currentLineLineNo1, currentLineLineNo, specifiedLineLineNo, linesToScroll); | |
690 | */ | |
691 | ||
692 | if (linesToScroll != 0) | |
693 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)MAKELPARAM(linesToScroll, 0)); | |
694 | } | |
695 | ||
696 | int wxTextCtrl::GetLineLength(const long lineNo) const | |
697 | { | |
698 | long charIndex = XYToPosition(0, lineNo); | |
699 | HWND hWnd = (HWND) GetHWND(); | |
700 | int len = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0); | |
701 | return len; | |
702 | } | |
703 | ||
704 | wxString wxTextCtrl::GetLineText(const long lineNo) const | |
705 | { | |
706 | HWND hWnd = (HWND) GetHWND(); | |
707 | *(WORD *)wxBuffer = 512; | |
708 | int noChars = (int)SendMessage(hWnd, EM_GETLINE, (WPARAM)lineNo, (LPARAM)wxBuffer); | |
709 | wxBuffer[noChars] = 0; | |
710 | return wxString(wxBuffer); | |
711 | } | |
712 | ||
713 | /* | |
714 | * Text item | |
715 | */ | |
716 | ||
717 | void wxTextCtrl::Command(wxCommandEvent & event) | |
718 | { | |
719 | SetValue (event.GetString()); | |
720 | ProcessCommand (event); | |
721 | } | |
722 | ||
723 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
724 | { | |
725 | // By default, load the first file into the text window. | |
726 | if (event.GetNumberOfFiles() > 0) | |
727 | { | |
728 | LoadFile(event.GetFiles()[0]); | |
729 | } | |
730 | } | |
731 | ||
732 | // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of | |
733 | // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers | |
734 | ||
735 | //========================================================================= | |
736 | // Called then the buffer is full (gcc 2.6.3) | |
737 | // or when "endl" is output (Borland 4.5) | |
738 | //========================================================================= | |
739 | // Class declaration using multiple inheritance doesn't work properly for | |
740 | // Borland. See note in wb_text.h. | |
741 | #ifndef NO_TEXT_WINDOW_STREAM | |
742 | int wxTextCtrl::overflow(int c) | |
743 | { | |
744 | // Make sure there is a holding area | |
745 | if ( allocate()==EOF ) | |
746 | { | |
747 | wxError("Streambuf allocation failed","Internal error"); | |
748 | return EOF; | |
749 | } | |
750 | ||
751 | // Verify that there are no characters in get area | |
752 | if ( gptr() && gptr() < egptr() ) | |
753 | { | |
754 | wxError("Who's trespassing my get area?","Internal error"); | |
755 | return EOF; | |
756 | } | |
757 | ||
758 | // Reset get area | |
759 | setg(0,0,0); | |
760 | ||
761 | // Make sure there is a put area | |
762 | if ( ! pptr() ) | |
763 | { | |
764 | /* This doesn't seem to be fatal so comment out error message */ | |
765 | // wxError("Put area not opened","Internal error"); | |
766 | setp( base(), base() ); | |
767 | } | |
768 | ||
769 | // Determine how many characters have been inserted but no consumed | |
770 | int plen = pptr() - pbase(); | |
771 | ||
772 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
773 | // long, but the holding area "may be as small as 1" ??? | |
774 | // And we need an additional \0, so let's keep this inefficient but | |
775 | // safe copy. | |
776 | ||
777 | // If c!=EOF, it is a character that must also be comsumed | |
778 | int xtra = c==EOF? 0 : 1; | |
779 | ||
780 | // Write temporary C-string to wxTextWindow | |
781 | { | |
782 | char *txt = new char[plen+xtra+1]; | |
783 | memcpy(txt, pbase(), plen); | |
784 | txt[plen] = (char)c; // append c | |
785 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
786 | // If the put area already contained \0, output will be truncated there | |
787 | WriteText(txt); | |
788 | delete[] txt; | |
789 | } | |
790 | ||
791 | // Reset put area | |
792 | setp(pbase(), epptr()); | |
793 | ||
794 | #if defined(__WATCOMC__) | |
795 | return __NOT_EOF; | |
796 | #elif defined(zapeof) // HP-UX (all cfront based?) | |
797 | return zapeof(c); | |
798 | #else | |
799 | return c!=EOF ? c : 0; // this should make everybody happy | |
800 | #endif | |
801 | ||
802 | /* OLD CODE | |
803 | int len = pptr() - pbase(); | |
804 | char *txt = new char[len+1]; | |
805 | strncpy(txt, pbase(), len); | |
806 | txt[len] = '\0'; | |
807 | (*this) << txt; | |
808 | setp(pbase(), epptr()); | |
809 | delete[] txt; | |
810 | return EOF; | |
811 | */ | |
812 | } | |
813 | ||
814 | //========================================================================= | |
815 | // called then "endl" is output (gcc) or then explicit sync is done (Borland) | |
816 | //========================================================================= | |
817 | int wxTextCtrl::sync(void) | |
818 | { | |
819 | // Verify that there are no characters in get area | |
820 | if ( gptr() && gptr() < egptr() ) | |
821 | { | |
822 | wxError("Who's trespassing my get area?","Internal error"); | |
823 | return EOF; | |
824 | } | |
825 | ||
826 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
827 | ||
828 | return 0; | |
829 | /* OLD CODE | |
830 | int len = pptr() - pbase(); | |
831 | char *txt = new char[len+1]; | |
832 | strncpy(txt, pbase(), len); | |
833 | txt[len] = '\0'; | |
834 | (*this) << txt; | |
835 | setp(pbase(), epptr()); | |
836 | delete[] txt; | |
837 | return 0; | |
838 | */ | |
839 | } | |
840 | ||
841 | //========================================================================= | |
842 | // Should not be called by a "ostream". Used by a "istream" | |
843 | //========================================================================= | |
844 | int wxTextCtrl::underflow(void) | |
845 | { | |
846 | return EOF; | |
847 | } | |
848 | #endif | |
849 | ||
850 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
851 | { | |
852 | WriteText(s); | |
853 | return *this; | |
854 | } | |
855 | ||
856 | wxTextCtrl& wxTextCtrl::operator<<(const float f) | |
857 | { | |
858 | static char buf[100]; | |
859 | sprintf(buf, "%.2f", f); | |
860 | WriteText(buf); | |
861 | return *this; | |
862 | } | |
863 | ||
864 | wxTextCtrl& wxTextCtrl::operator<<(const double d) | |
865 | { | |
866 | static char buf[100]; | |
867 | sprintf(buf, "%.2f", d); | |
868 | WriteText(buf); | |
869 | return *this; | |
870 | } | |
871 | ||
872 | wxTextCtrl& wxTextCtrl::operator<<(const int i) | |
873 | { | |
874 | static char buf[100]; | |
875 | sprintf(buf, "%i", i); | |
876 | WriteText(buf); | |
877 | return *this; | |
878 | } | |
879 | ||
880 | wxTextCtrl& wxTextCtrl::operator<<(const long i) | |
881 | { | |
882 | static char buf[100]; | |
883 | sprintf(buf, "%ld", i); | |
884 | WriteText(buf); | |
885 | return *this; | |
886 | } | |
887 | ||
888 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
889 | { | |
890 | char buf[2]; | |
891 | ||
892 | buf[0] = c; | |
893 | buf[1] = 0; | |
894 | WriteText(buf); | |
895 | return *this; | |
896 | } | |
897 | ||
898 | ||
899 | WXHBRUSH wxTextCtrl::OnCtlColor(const WXHDC pDC, const WXHWND pWnd, const WXUINT nCtlColor, | |
900 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
901 | { | |
902 | #if CTL3D | |
903 | if ( m_useCtl3D ) | |
904 | { | |
905 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
906 | return (WXHBRUSH) hbrush; | |
907 | } | |
908 | #endif | |
909 | ||
910 | if (GetParent()->GetTransparentBackground()) | |
911 | SetBkMode((HDC) pDC, TRANSPARENT); | |
912 | else | |
913 | SetBkMode((HDC) pDC, OPAQUE); | |
914 | ||
915 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
916 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
917 | ||
918 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
919 | ||
920 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
921 | // has a zero usage count. | |
922 | // NOT NOW - will be cleaned up at end of app. | |
923 | // backgroundBrush->RealizeResource(); | |
924 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
925 | } | |
926 | ||
927 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
928 | { | |
929 | if ( (event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) | |
930 | { | |
931 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
932 | event.SetEventObject( this ); | |
933 | if ( !GetEventHandler()->ProcessEvent(event) ) | |
934 | event.Skip(); | |
935 | } | |
936 | else | |
937 | event.Skip(); | |
938 | } | |
939 | ||
940 | long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
941 | { | |
942 | switch (nMsg) | |
943 | { | |
944 | /* | |
945 | case WM_GETDLGCODE: | |
946 | { | |
947 | if (GetWindowStyleFlag() & wxPROCESS_ENTER) | |
948 | return DLGC_WANTALLKEYS; | |
949 | break; | |
950 | } | |
951 | */ | |
952 | /* | |
953 | case WM_CHAR: // Always an ASCII character | |
954 | { | |
955 | if (wParam == VK_RETURN) | |
956 | { | |
957 | wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND); | |
958 | event.commandString = ((wxTextCtrl *)item)->GetValue(); | |
959 | event.eventObject = item; | |
960 | item->ProcessCommand(event); | |
961 | return FALSE; | |
962 | } | |
963 | break; | |
964 | } | |
965 | */ | |
966 | default: | |
967 | break; | |
968 | } | |
969 | ||
970 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
971 | } | |
972 | ||
973 | void wxTextCtrl::OnEraseBackground(wxEraseEvent& event) | |
974 | { | |
975 | if ( m_windowStyle & wxTE_MULTILINE ) | |
976 | { | |
977 | // No flicker - only problem is we probably can't change the background | |
978 | Default(); | |
979 | /* | |
980 | RECT rect; | |
981 | ::GetClientRect((HWND) GetHWND(), &rect); | |
982 | ||
983 | HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
984 | int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT); | |
985 | ||
986 | ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush); | |
987 | ::DeleteObject(hBrush); | |
988 | ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode); | |
989 | */ | |
990 | } | |
991 | // wxWindow::OnEraseBackground(event); | |
992 | } | |
993 | ||
994 | bool wxTextCtrl::MSWCommand(const WXUINT param, const WXWORD WXUNUSED(id)) | |
995 | { | |
996 | /* | |
997 | // Debugging | |
998 | wxDebugMsg("Edit control %d: ", (int)id); | |
999 | switch (param) | |
1000 | { | |
1001 | case EN_SETFOCUS: | |
1002 | wxDebugMsg("EN_SETFOCUS\n"); | |
1003 | break; | |
1004 | case EN_KILLFOCUS: | |
1005 | wxDebugMsg("EN_KILLFOCUS\n"); | |
1006 | break; | |
1007 | case EN_CHANGE: | |
1008 | wxDebugMsg("EN_CHANGE\n"); | |
1009 | break; | |
1010 | case EN_UPDATE: | |
1011 | wxDebugMsg("EN_UPDATE\n"); | |
1012 | break; | |
1013 | case EN_ERRSPACE: | |
1014 | wxDebugMsg("EN_ERRSPACE\n"); | |
1015 | break; | |
1016 | case EN_MAXTEXT: | |
1017 | wxDebugMsg("EN_MAXTEXT\n"); | |
1018 | break; | |
1019 | case EN_HSCROLL: | |
1020 | wxDebugMsg("EN_HSCROLL\n"); | |
1021 | break; | |
1022 | case EN_VSCROLL: | |
1023 | wxDebugMsg("EN_VSCROLL\n"); | |
1024 | break; | |
1025 | default: | |
1026 | wxDebugMsg("Unknown EDIT notification\n"); | |
1027 | break; | |
1028 | } | |
1029 | */ | |
1030 | WXTYPE eventTyp = 0; | |
1031 | switch (param) | |
1032 | { | |
1033 | case EN_SETFOCUS: | |
1034 | eventTyp = wxEVENT_TYPE_SET_FOCUS; | |
1035 | break; | |
1036 | case EN_KILLFOCUS: | |
1037 | eventTyp = wxEVENT_TYPE_KILL_FOCUS; | |
1038 | break; | |
1039 | case EN_UPDATE: | |
1040 | break; | |
1041 | case EN_CHANGE: | |
1042 | eventTyp = wxEVENT_TYPE_TEXT_COMMAND; | |
1043 | break; | |
1044 | case EN_ERRSPACE: | |
1045 | break; | |
1046 | case EN_MAXTEXT: | |
1047 | break; | |
1048 | case EN_HSCROLL: | |
1049 | break; | |
1050 | case EN_VSCROLL: | |
1051 | break; | |
1052 | default: | |
1053 | break; | |
1054 | } | |
1055 | if (eventTyp != 0) | |
1056 | { | |
1057 | wxCommandEvent event(eventTyp, m_windowId); | |
1058 | wxString val(GetValue()); | |
1059 | if ( !val.IsNull() ) | |
1060 | event.m_commandString = WXSTRINGCAST val; | |
1061 | event.SetEventObject( this ); | |
1062 | ProcessCommand(event); | |
1063 | ||
1064 | return TRUE; | |
1065 | } | |
1066 | else | |
1067 | return FALSE; | |
1068 | } | |
1069 | ||
1070 | ||
1071 | // For Rich Edit controls. Do we need it? | |
1072 | #if 0 | |
1073 | #if defined(__WIN95__) | |
1074 | bool wxTextCtrl::MSWNotify(const WXWPARAM wParam, const WXLPARAM lParam) | |
1075 | { | |
1076 | wxCommandEvent event(0, m_windowId); | |
1077 | int eventType = 0; | |
1078 | NMHDR *hdr1 = (NMHDR *) lParam; | |
1079 | switch ( hdr1->code ) | |
1080 | { | |
1081 | // Insert case code here | |
1082 | default : | |
1083 | return wxControl::MSWNotify(wParam, lParam); | |
1084 | break; | |
1085 | } | |
1086 | ||
1087 | event.SetEventObject( this ); | |
1088 | event.SetEventType(eventType); | |
1089 | ||
1090 | if ( !ProcessEvent(event) ) | |
1091 | return FALSE; | |
1092 | ||
1093 | return TRUE; | |
1094 | } | |
1095 | #endif | |
1096 | #endif | |
1097 |