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