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