]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
minor changes a bit everywhere + a small wxLog change (Enable()/IsEnabled()
[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 if ( allocate()==EOF )
749 {
750 wxError("Streambuf allocation failed","Internal error");
751 return EOF;
752 }
753
754 // Verify that there are no characters in get area
755 if ( gptr() && gptr() < egptr() )
756 {
757 wxError("Who's trespassing my get area?","Internal error");
758 return EOF;
759 }
760
761 // Reset get area
762 setg(0,0,0);
763
764 // Make sure there is a put area
765 if ( ! pptr() )
766 {
767 /* This doesn't seem to be fatal so comment out error message */
768 // wxError("Put area not opened","Internal error");
769 setp( base(), base() );
770 }
771
772 // Determine how many characters have been inserted but no consumed
773 int plen = pptr() - pbase();
774
775 // Now Jerry relies on the fact that the buffer is at least 2 chars
776 // long, but the holding area "may be as small as 1" ???
777 // And we need an additional \0, so let's keep this inefficient but
778 // safe copy.
779
780 // If c!=EOF, it is a character that must also be comsumed
781 int xtra = c==EOF? 0 : 1;
782
783 // Write temporary C-string to wxTextWindow
784 {
785 char *txt = new char[plen+xtra+1];
786 memcpy(txt, pbase(), plen);
787 txt[plen] = (char)c; // append c
788 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
789 // If the put area already contained \0, output will be truncated there
790 WriteText(txt);
791 delete[] txt;
792 }
793
794 // Reset put area
795 setp(pbase(), epptr());
796
797 #if defined(__WATCOMC__)
798 return __NOT_EOF;
799 #elif defined(zapeof) // HP-UX (all cfront based?)
800 return zapeof(c);
801 #else
802 return c!=EOF ? c : 0; // this should make everybody happy
803 #endif
804
805 /* OLD CODE
806 int len = pptr() - pbase();
807 char *txt = new char[len+1];
808 strncpy(txt, pbase(), len);
809 txt[len] = '\0';
810 (*this) << txt;
811 setp(pbase(), epptr());
812 delete[] txt;
813 return EOF;
814 */
815 }
816
817 //=========================================================================
818 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
819 //=========================================================================
820 int wxTextCtrl::sync(void)
821 {
822 // Verify that there are no characters in get area
823 if ( gptr() && gptr() < egptr() )
824 {
825 wxError("Who's trespassing my get area?","Internal error");
826 return EOF;
827 }
828
829 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
830
831 return 0;
832 /* OLD CODE
833 int len = pptr() - pbase();
834 char *txt = new char[len+1];
835 strncpy(txt, pbase(), len);
836 txt[len] = '\0';
837 (*this) << txt;
838 setp(pbase(), epptr());
839 delete[] txt;
840 return 0;
841 */
842 }
843
844 //=========================================================================
845 // Should not be called by a "ostream". Used by a "istream"
846 //=========================================================================
847 int wxTextCtrl::underflow(void)
848 {
849 return EOF;
850 }
851 #endif
852
853 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
854 {
855 WriteText(s);
856 return *this;
857 }
858
859 wxTextCtrl& wxTextCtrl::operator<<(float f)
860 {
861 wxString str;
862 str.Printf("%.2f", f);
863 WriteText(str);
864 return *this;
865 }
866
867 wxTextCtrl& wxTextCtrl::operator<<(double d)
868 {
869 wxString str;
870 str.Printf("%.2f", d);
871 WriteText(str);
872 return *this;
873 }
874
875 wxTextCtrl& wxTextCtrl::operator<<(int i)
876 {
877 wxString str;
878 str.Printf("%d", i);
879 WriteText(str);
880 return *this;
881 }
882
883 wxTextCtrl& wxTextCtrl::operator<<(long i)
884 {
885 wxString str;
886 str.Printf("%ld", i);
887 WriteText(str);
888 return *this;
889 }
890
891 wxTextCtrl& wxTextCtrl::operator<<(const char c)
892 {
893 char buf[2];
894
895 buf[0] = c;
896 buf[1] = 0;
897 WriteText(buf);
898 return *this;
899 }
900
901 WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
902 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
903 {
904 #if CTL3D
905 if ( m_useCtl3D )
906 {
907 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
908 return (WXHBRUSH) hbrush;
909 }
910 #endif
911
912 if (GetParent()->GetTransparentBackground())
913 SetBkMode((HDC) pDC, TRANSPARENT);
914 else
915 SetBkMode((HDC) pDC, OPAQUE);
916
917 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
918 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
919
920 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
921
922 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
923 // has a zero usage count.
924 // NOT NOW - will be cleaned up at end of app.
925 // backgroundBrush->RealizeResource();
926 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
927 }
928
929 void wxTextCtrl::OnChar(wxKeyEvent& event)
930 {
931 if ( (event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
932 {
933 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
934 event.SetEventObject( this );
935 if ( GetEventHandler()->ProcessEvent(event) )
936 return;
937 }
938 else if ( event.KeyCode() == WXK_TAB ) {
939 wxNavigationKeyEvent event;
940 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
941 event.SetWindowChange(FALSE);
942 event.SetEventObject(this);
943
944 if ( GetEventHandler()->ProcessEvent(event) )
945 return;
946 }
947
948 event.Skip();
949 }
950
951 long wxTextCtrl::MSWGetDlgCode()
952 {
953 long lRc = DLGC_WANTCHARS | DLGC_WANTARROWS;
954 if ( m_windowStyle & wxPROCESS_ENTER )
955 lRc |= DLGC_WANTMESSAGE;
956 else if ( m_windowStyle & wxTE_MULTILINE )
957 lRc |= DLGC_WANTMESSAGE;
958
959 return lRc;
960 }
961
962 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
963 {
964 if ( m_windowStyle & wxTE_MULTILINE )
965 {
966 // No flicker - only problem is we probably can't change the background
967 Default();
968 /*
969 RECT rect;
970 ::GetClientRect((HWND) GetHWND(), &rect);
971
972 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
973 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
974
975 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
976 ::DeleteObject(hBrush);
977 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
978 */
979 }
980 // wxWindow::OnEraseBackground(event);
981 }
982
983 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
984 {
985 /*
986 // Debugging
987 wxDebugMsg("Edit control %d: ", (int)id);
988 switch (param)
989 {
990 case EN_SETFOCUS:
991 wxDebugMsg("EN_SETFOCUS\n");
992 break;
993 case EN_KILLFOCUS:
994 wxDebugMsg("EN_KILLFOCUS\n");
995 break;
996 case EN_CHANGE:
997 wxDebugMsg("EN_CHANGE\n");
998 break;
999 case EN_UPDATE:
1000 wxDebugMsg("EN_UPDATE\n");
1001 break;
1002 case EN_ERRSPACE:
1003 wxDebugMsg("EN_ERRSPACE\n");
1004 break;
1005 case EN_MAXTEXT:
1006 wxDebugMsg("EN_MAXTEXT\n");
1007 break;
1008 case EN_HSCROLL:
1009 wxDebugMsg("EN_HSCROLL\n");
1010 break;
1011 case EN_VSCROLL:
1012 wxDebugMsg("EN_VSCROLL\n");
1013 break;
1014 default:
1015 wxDebugMsg("Unknown EDIT notification\n");
1016 break;
1017 }
1018 */
1019 switch (param)
1020 {
1021 case EN_SETFOCUS:
1022 case EN_KILLFOCUS:
1023 {
1024 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1025 : wxEVT_SET_FOCUS,
1026 m_windowId);
1027 event.SetEventObject( this );
1028 ProcessEvent(event);
1029 }
1030 break;
1031
1032 case EN_CHANGE:
1033 {
1034 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1035 wxString val(GetValue());
1036 if ( !val.IsNull() )
1037 event.m_commandString = WXSTRINGCAST val;
1038 event.SetEventObject( this );
1039 ProcessCommand(event);
1040 }
1041 break;
1042
1043 // the other notification messages are not processed
1044 case EN_UPDATE:
1045 case EN_ERRSPACE:
1046 case EN_MAXTEXT:
1047 case EN_HSCROLL:
1048 case EN_VSCROLL:
1049 default:
1050 return FALSE;
1051 }
1052
1053 // processed
1054 return TRUE;
1055 }
1056
1057
1058 // For Rich Edit controls. Do we need it?
1059 #if 0
1060 #if defined(__WIN95__)
1061 bool wxTextCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam)
1062 {
1063 wxCommandEvent event(0, m_windowId);
1064 int eventType = 0;
1065 NMHDR *hdr1 = (NMHDR *) lParam;
1066 switch ( hdr1->code )
1067 {
1068 // Insert case code here
1069 default :
1070 return wxControl::MSWNotify(wParam, lParam);
1071 break;
1072 }
1073
1074 event.SetEventObject( this );
1075 event.SetEventType(eventType);
1076
1077 if ( !ProcessEvent(event) )
1078 return FALSE;
1079
1080 return TRUE;
1081 }
1082 #endif
1083 #endif
1084