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