]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
many fixes to wxTextCrtl, wxTreeCrtl, wxListBox,
[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 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, wxWindowID id,
80 const wxString& value,
81 const wxPoint& pos,
82 const wxSize& size, 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 ? (HINSTANCE) 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(int x, int y, int width, int height, int sizeFlags)
304 {
305 int currentX, currentY;
306 GetPosition(&currentX, &currentY);
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
350 // Clipboard operations
351 void wxTextCtrl::Copy(void)
352 {
353 HWND hWnd = (HWND) GetHWND();
354 SendMessage(hWnd, WM_COPY, 0, 0L);
355 }
356
357 void wxTextCtrl::Cut(void)
358 {
359 HWND hWnd = (HWND) GetHWND();
360 SendMessage(hWnd, WM_CUT, 0, 0L);
361 }
362
363 void wxTextCtrl::Paste(void)
364 {
365 HWND hWnd = (HWND) GetHWND();
366 SendMessage(hWnd, WM_PASTE, 0, 0L);
367 }
368
369 void wxTextCtrl::SetEditable(bool editable)
370 {
371 HWND hWnd = (HWND) GetHWND();
372 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
373 }
374
375 void wxTextCtrl::SetInsertionPoint(long pos)
376 {
377 HWND hWnd = (HWND) GetHWND();
378 #ifdef __WIN32__
379 #if defined(__WIN95__)
380 if ( m_isRich)
381 {
382 CHARRANGE range;
383 range.cpMin = pos;
384 range.cpMax = pos;
385 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
386 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
387 }
388 else
389 #endif
390 {
391 SendMessage(hWnd, EM_SETSEL, pos, pos);
392 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
393 }
394 #else
395 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
396 #endif
397 char *nothing = "";
398 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
399 }
400
401 void wxTextCtrl::SetInsertionPointEnd(void)
402 {
403 long pos = GetLastPosition();
404 SetInsertionPoint(pos);
405 }
406
407 long wxTextCtrl::GetInsertionPoint(void) const
408 {
409 #if defined(__WIN95__)
410 if (m_isRich)
411 {
412 CHARRANGE range;
413 range.cpMin = 0;
414 range.cpMax = 0;
415 SendMessage((HWND) GetHWND(), EM_EXGETSEL, 0, (LPARAM) &range);
416 return range.cpMin;
417 }
418 #endif
419
420 DWORD Pos=(DWORD)SendMessage((HWND) GetHWND(), EM_GETSEL, 0, 0L);
421 return Pos&0xFFFF;
422 }
423
424 long wxTextCtrl::GetLastPosition(void) const
425 {
426 HWND hWnd = (HWND) GetHWND();
427
428 // Will always return a number > 0 (according to docs)
429 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
430
431 // This gets the char index for the _beginning_ of the last line
432 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
433
434 // Get number of characters in the last line. We'll add this to the character
435 // index for the last line, 1st position.
436 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
437
438 return (long)(charIndex + lineLength);
439 }
440
441 void wxTextCtrl::Replace(long from, long to, const wxString& value)
442 {
443 HWND hWnd = (HWND) GetHWND();
444 long fromChar = from;
445 long toChar = to;
446
447 // Set selection and remove it
448 #ifdef __WIN32__
449 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
450 #else
451 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
452 #endif
453 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
454
455 // Now replace with 'value', by pasting.
456 wxSetClipboardData(wxCF_TEXT, (wxObject *) (const char *)value, 0, 0);
457
458 // Paste into edit control
459 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
460 }
461
462 void wxTextCtrl::Remove(long from, long to)
463 {
464 HWND hWnd = (HWND) GetHWND();
465 long fromChar = from;
466 long toChar = to;
467
468 // Cut all selected text
469 #ifdef __WIN32__
470 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
471 #else
472 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
473 #endif
474 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
475 }
476
477 void wxTextCtrl::SetSelection(long from, long to)
478 {
479 HWND hWnd = (HWND) GetHWND();
480 long fromChar = from;
481 long toChar = to;
482 // if from and to are both -1, it means
483 // (in wxWindows) that all text should be selected.
484 // This translates into Windows convention
485 if ((from == -1) && (to == -1))
486 {
487 fromChar = 0;
488 toChar = -1;
489 }
490
491 #ifdef __WIN32__
492 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
493 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
494 #else
495 // WPARAM is 0: selection is scrolled into view
496 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
497 #endif
498 }
499
500 bool wxTextCtrl::LoadFile(const wxString& file)
501 {
502 if (!FileExists(WXSTRINGCAST file))
503 return FALSE;
504
505 fileName = file;
506
507 Clear();
508
509 ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in);
510
511 if (!input.bad())
512 {
513 // Previously a SETSEL/REPLACESEL call-pair were done to insert
514 // line by line into the control. Apart from being very slow this
515 // was limited to 32K of text by the external interface presenting
516 // positions as signed shorts. Now load in one chunk...
517 // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits...
518
519 struct stat stat_buf;
520 if (stat(file, &stat_buf) < 0)
521 return FALSE;
522 // char *tmp_buffer = (char*)farmalloc(stat_buf.st_size+1);
523 // This may need to be a bigger buffer than the file size suggests,
524 // if it's a UNIX file. Give it an extra 1000 just in case.
525 char *tmp_buffer = (char*)farmalloc((size_t)(stat_buf.st_size+1+1000));
526 long no_lines = 0;
527 long pos = 0;
528 while (!input.eof() && input.peek() != EOF)
529 {
530 input.getline(wxBuffer, 500);
531 int len = strlen(wxBuffer);
532 wxBuffer[len] = 13;
533 wxBuffer[len+1] = 10;
534 wxBuffer[len+2] = 0;
535 strcpy(tmp_buffer+pos, wxBuffer);
536 pos += strlen(wxBuffer);
537 no_lines++;
538 }
539
540 // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)tmp_buffer);
541 SetWindowText((HWND) GetHWND(), tmp_buffer);
542 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
543 farfree(tmp_buffer);
544
545 return TRUE;
546 }
547 return FALSE;
548 }
549
550 // If file is null, try saved file name first
551 // Returns TRUE if succeeds.
552 bool wxTextCtrl::SaveFile(const wxString& file)
553 {
554 wxString theFile;
555 if (file == "")
556 theFile = fileName;
557 if (file == "")
558 return FALSE;
559 fileName = theFile;
560
561 ofstream output(WXSTRINGCAST file);
562 if (output.bad())
563 return FALSE;
564
565 // This will only save 64K max
566 unsigned long nbytes = SendMessage((HWND) GetHWND(), WM_GETTEXTLENGTH, 0, 0);
567 char *tmp_buffer = (char*)farmalloc((size_t)(nbytes+1));
568 SendMessage((HWND) GetHWND(), WM_GETTEXT, (WPARAM)(nbytes+1), (LPARAM)tmp_buffer);
569 char *pstr = tmp_buffer;
570
571 // Convert \r\n to just \n
572 while (*pstr)
573 {
574 if (*pstr != '\r')
575 output << *pstr;
576 pstr++;
577 }
578
579 farfree(tmp_buffer);
580 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
581
582 return TRUE;
583 }
584
585 void wxTextCtrl::WriteText(const wxString& text)
586 {
587 // Covert \n to \r\n
588 int len = text.Length();
589 char *newtext = new char[(len*2)+1];
590 int i = 0;
591 int j = 0;
592 while (i < len)
593 {
594 if (text[i] == '\n')
595 {
596 newtext[j] = '\r';
597 j ++;
598 }
599 newtext[j] = text[i];
600 i ++;
601 j ++;
602 }
603 newtext[j] = 0;
604 SendMessage((HWND) GetHWND(), EM_REPLACESEL, 0, (LPARAM)newtext);
605 delete[] newtext;
606 }
607
608 void wxTextCtrl::Clear(void)
609 {
610 // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)"");
611 SetWindowText((HWND) GetHWND(), "");
612 }
613
614 bool wxTextCtrl::IsModified(void) const
615 {
616 return (SendMessage((HWND) GetHWND(), EM_GETMODIFY, 0, 0) != 0);
617 }
618
619 // Makes 'unmodified'
620 void wxTextCtrl::DiscardEdits(void)
621 {
622 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
623 }
624
625 /*
626 * Some of the following functions are yet to be implemented
627 *
628 */
629
630 int wxTextCtrl::GetNumberOfLines(void) const
631 {
632 return (int)SendMessage((HWND) GetHWND(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
633 }
634
635 long wxTextCtrl::XYToPosition(long x, long y) const
636 {
637 HWND hWnd = (HWND) GetHWND();
638
639 // This gets the char index for the _beginning_ of this line
640 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
641 return (long)(x + charIndex);
642 }
643
644 void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
645 {
646 HWND hWnd = (HWND) GetHWND();
647
648 // This gets the line number containing the character
649 int lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0);
650 // This gets the char index for the _beginning_ of this line
651 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
652 // The X position must therefore be the different between pos and charIndex
653 *x = (long)(pos - charIndex);
654 *y = (long)lineNo;
655 }
656
657 void wxTextCtrl::ShowPosition(long pos)
658 {
659 HWND hWnd = (HWND) GetHWND();
660
661 // To scroll to a position, we pass the number of lines and characters
662 // to scroll *by*. This means that we need to:
663 // (1) Find the line position of the current line.
664 // (2) Find the line position of pos.
665 // (3) Scroll by (pos - current).
666 // For now, ignore the horizontal scrolling.
667
668 // Is this where scrolling is relative to - the line containing the caret?
669 // Or is the first visible line??? Try first visible line.
670 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
671
672 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
673
674 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
675
676 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
677
678 /*
679 wxDebugMsg("Caret line: %d; Current visible line: %d; Specified line: %d; lines to scroll: %d\n",
680 currentLineLineNo1, currentLineLineNo, specifiedLineLineNo, linesToScroll);
681 */
682
683 if (linesToScroll != 0)
684 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)MAKELPARAM(linesToScroll, 0));
685 }
686
687 int wxTextCtrl::GetLineLength(long lineNo) const
688 {
689 long charIndex = XYToPosition(0, lineNo);
690 HWND hWnd = (HWND) GetHWND();
691 int len = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0);
692 return len;
693 }
694
695 wxString wxTextCtrl::GetLineText(long lineNo) const
696 {
697 HWND hWnd = (HWND) GetHWND();
698 *(WORD *)wxBuffer = 512;
699 int noChars = (int)SendMessage(hWnd, EM_GETLINE, (WPARAM)lineNo, (LPARAM)wxBuffer);
700 wxBuffer[noChars] = 0;
701 return wxString(wxBuffer);
702 }
703
704 /*
705 * Text item
706 */
707
708 void wxTextCtrl::Command(wxCommandEvent & event)
709 {
710 SetValue (event.GetString());
711 ProcessCommand (event);
712 }
713
714 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
715 {
716 // By default, load the first file into the text window.
717 if (event.GetNumberOfFiles() > 0)
718 {
719 LoadFile(event.GetFiles()[0]);
720 }
721 }
722
723 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
724 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
725
726 //=========================================================================
727 // Called then the buffer is full (gcc 2.6.3)
728 // or when "endl" is output (Borland 4.5)
729 //=========================================================================
730 // Class declaration using multiple inheritance doesn't work properly for
731 // Borland. See note in wb_text.h.
732 #ifndef NO_TEXT_WINDOW_STREAM
733 int wxTextCtrl::overflow(int c)
734 {
735 // Make sure there is a holding area
736 if ( allocate()==EOF )
737 {
738 wxError("Streambuf allocation failed","Internal error");
739 return EOF;
740 }
741
742 // Verify that there are no characters in get area
743 if ( gptr() && gptr() < egptr() )
744 {
745 wxError("Who's trespassing my get area?","Internal error");
746 return EOF;
747 }
748
749 // Reset get area
750 setg(0,0,0);
751
752 // Make sure there is a put area
753 if ( ! pptr() )
754 {
755 /* This doesn't seem to be fatal so comment out error message */
756 // wxError("Put area not opened","Internal error");
757 setp( base(), base() );
758 }
759
760 // Determine how many characters have been inserted but no consumed
761 int plen = pptr() - pbase();
762
763 // Now Jerry relies on the fact that the buffer is at least 2 chars
764 // long, but the holding area "may be as small as 1" ???
765 // And we need an additional \0, so let's keep this inefficient but
766 // safe copy.
767
768 // If c!=EOF, it is a character that must also be comsumed
769 int xtra = c==EOF? 0 : 1;
770
771 // Write temporary C-string to wxTextWindow
772 {
773 char *txt = new char[plen+xtra+1];
774 memcpy(txt, pbase(), plen);
775 txt[plen] = (char)c; // append c
776 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
777 // If the put area already contained \0, output will be truncated there
778 WriteText(txt);
779 delete[] txt;
780 }
781
782 // Reset put area
783 setp(pbase(), epptr());
784
785 #if defined(__WATCOMC__)
786 return __NOT_EOF;
787 #elif defined(zapeof) // HP-UX (all cfront based?)
788 return zapeof(c);
789 #else
790 return c!=EOF ? c : 0; // this should make everybody happy
791 #endif
792
793 /* OLD CODE
794 int len = pptr() - pbase();
795 char *txt = new char[len+1];
796 strncpy(txt, pbase(), len);
797 txt[len] = '\0';
798 (*this) << txt;
799 setp(pbase(), epptr());
800 delete[] txt;
801 return EOF;
802 */
803 }
804
805 //=========================================================================
806 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
807 //=========================================================================
808 int wxTextCtrl::sync(void)
809 {
810 // Verify that there are no characters in get area
811 if ( gptr() && gptr() < egptr() )
812 {
813 wxError("Who's trespassing my get area?","Internal error");
814 return EOF;
815 }
816
817 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
818
819 return 0;
820 /* OLD CODE
821 int len = pptr() - pbase();
822 char *txt = new char[len+1];
823 strncpy(txt, pbase(), len);
824 txt[len] = '\0';
825 (*this) << txt;
826 setp(pbase(), epptr());
827 delete[] txt;
828 return 0;
829 */
830 }
831
832 //=========================================================================
833 // Should not be called by a "ostream". Used by a "istream"
834 //=========================================================================
835 int wxTextCtrl::underflow(void)
836 {
837 return EOF;
838 }
839 #endif
840
841 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
842 {
843 WriteText(s);
844 return *this;
845 }
846
847 wxTextCtrl& wxTextCtrl::operator<<(float f)
848 {
849 static char buf[100];
850 sprintf(buf, "%.2f", f);
851 WriteText(buf);
852 return *this;
853 }
854
855 wxTextCtrl& wxTextCtrl::operator<<(double d)
856 {
857 static char buf[100];
858 sprintf(buf, "%.2f", d);
859 WriteText(buf);
860 return *this;
861 }
862
863 wxTextCtrl& wxTextCtrl::operator<<(int i)
864 {
865 static char buf[100];
866 sprintf(buf, "%i", i);
867 WriteText(buf);
868 return *this;
869 }
870
871 wxTextCtrl& wxTextCtrl::operator<<(long i)
872 {
873 static char buf[100];
874 sprintf(buf, "%ld", i);
875 WriteText(buf);
876 return *this;
877 }
878
879 wxTextCtrl& wxTextCtrl::operator<<(const char c)
880 {
881 char buf[2];
882
883 buf[0] = c;
884 buf[1] = 0;
885 WriteText(buf);
886 return *this;
887 }
888
889
890 WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
891 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
892 {
893 #if CTL3D
894 if ( m_useCtl3D )
895 {
896 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
897 return (WXHBRUSH) hbrush;
898 }
899 #endif
900
901 if (GetParent()->GetTransparentBackground())
902 SetBkMode((HDC) pDC, TRANSPARENT);
903 else
904 SetBkMode((HDC) pDC, OPAQUE);
905
906 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
907 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
908
909 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
910
911 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
912 // has a zero usage count.
913 // NOT NOW - will be cleaned up at end of app.
914 // backgroundBrush->RealizeResource();
915 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
916 }
917
918 void wxTextCtrl::OnChar(wxKeyEvent& event)
919 {
920 if ( (event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
921 {
922 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
923 event.SetEventObject( this );
924 if ( GetEventHandler()->ProcessEvent(event) )
925 return;
926 }
927 else if ( event.KeyCode() == WXK_TAB ) {
928 wxNavigationKeyEvent event;
929 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
930 event.SetWindowChange(FALSE);
931 event.SetEventObject(this);
932
933 if ( GetEventHandler()->ProcessEvent(event) )
934 return;
935 }
936
937 event.Skip();
938 }
939
940 long wxTextCtrl::MSWGetDlgCode()
941 {
942 long lRc = DLGC_WANTCHARS | DLGC_WANTARROWS;
943 if ( m_windowStyle & wxPROCESS_ENTER ) {
944 lRc |= DLGC_WANTMESSAGE;
945 }
946
947 return lRc;
948 }
949
950 /*
951 long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
952 {
953 switch (nMsg)
954 {
955 case WM_GETDLGCODE:
956 {
957 if (GetWindowStyleFlag() & wxPROCESS_ENTER)
958 return DLGC_WANTALLKEYS;
959 break;
960 }
961 case WM_CHAR: // Always an ASCII character
962 {
963 if (wParam == VK_RETURN)
964 {
965 wxCommandEvent event(wxEVENT_TYPE_TEXT_ENTER_COMMAND);
966 event.commandString = ((wxTextCtrl *)item)->GetValue();
967 event.eventObject = item;
968 item->ProcessCommand(event);
969 return FALSE;
970 }
971 break;
972 }
973 default:
974 break;
975 }
976
977 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
978 }
979 */
980
981 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
982 {
983 if ( m_windowStyle & wxTE_MULTILINE )
984 {
985 // No flicker - only problem is we probably can't change the background
986 Default();
987 /*
988 RECT rect;
989 ::GetClientRect((HWND) GetHWND(), &rect);
990
991 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
992 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
993
994 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
995 ::DeleteObject(hBrush);
996 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
997 */
998 }
999 // wxWindow::OnEraseBackground(event);
1000 }
1001
1002 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
1003 {
1004 /*
1005 // Debugging
1006 wxDebugMsg("Edit control %d: ", (int)id);
1007 switch (param)
1008 {
1009 case EN_SETFOCUS:
1010 wxDebugMsg("EN_SETFOCUS\n");
1011 break;
1012 case EN_KILLFOCUS:
1013 wxDebugMsg("EN_KILLFOCUS\n");
1014 break;
1015 case EN_CHANGE:
1016 wxDebugMsg("EN_CHANGE\n");
1017 break;
1018 case EN_UPDATE:
1019 wxDebugMsg("EN_UPDATE\n");
1020 break;
1021 case EN_ERRSPACE:
1022 wxDebugMsg("EN_ERRSPACE\n");
1023 break;
1024 case EN_MAXTEXT:
1025 wxDebugMsg("EN_MAXTEXT\n");
1026 break;
1027 case EN_HSCROLL:
1028 wxDebugMsg("EN_HSCROLL\n");
1029 break;
1030 case EN_VSCROLL:
1031 wxDebugMsg("EN_VSCROLL\n");
1032 break;
1033 default:
1034 wxDebugMsg("Unknown EDIT notification\n");
1035 break;
1036 }
1037 */
1038 switch (param)
1039 {
1040 case EN_SETFOCUS:
1041 case EN_KILLFOCUS:
1042 {
1043 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1044 : wxEVT_SET_FOCUS,
1045 m_windowId);
1046 event.SetEventObject( this );
1047 ProcessEvent(event);
1048 }
1049 break;
1050
1051 case EN_CHANGE:
1052 {
1053 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1054 wxString val(GetValue());
1055 if ( !val.IsNull() )
1056 event.m_commandString = WXSTRINGCAST val;
1057 event.SetEventObject( this );
1058 ProcessCommand(event);
1059 }
1060 break;
1061
1062 // the other notification messages are not processed
1063 case EN_UPDATE:
1064 case EN_ERRSPACE:
1065 case EN_MAXTEXT:
1066 case EN_HSCROLL:
1067 case EN_VSCROLL:
1068 default:
1069 return FALSE;
1070 }
1071
1072 // processed
1073 return TRUE;
1074 }
1075
1076
1077 // For Rich Edit controls. Do we need it?
1078 #if 0
1079 #if defined(__WIN95__)
1080 bool wxTextCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam)
1081 {
1082 wxCommandEvent event(0, m_windowId);
1083 int eventType = 0;
1084 NMHDR *hdr1 = (NMHDR *) lParam;
1085 switch ( hdr1->code )
1086 {
1087 // Insert case code here
1088 default :
1089 return wxControl::MSWNotify(wParam, lParam);
1090 break;
1091 }
1092
1093 event.SetEventObject( this );
1094 event.SetEventType(eventType);
1095
1096 if ( !ProcessEvent(event) )
1097 return FALSE;
1098
1099 return TRUE;
1100 }
1101 #endif
1102 #endif
1103