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