]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
Added dnd classes, data object classes, changed clipboard class doc,
[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 #ifndef __GNUWIN32__
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, & 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 struct stat stat_buf;
539 if (stat(file, &stat_buf) < 0)
540 return FALSE;
541 // char *tmp_buffer = (char*)farmalloc(stat_buf.st_size+1);
542 // This may need to be a bigger buffer than the file size suggests,
543 // if it's a UNIX file. Give it an extra 1000 just in case.
544 char *tmp_buffer = (char*)farmalloc((size_t)(stat_buf.st_size+1+1000));
545 long no_lines = 0;
546 long pos = 0;
547 while (!input.eof() && input.peek() != EOF)
548 {
549 input.getline(wxBuffer, 500);
550 int len = strlen(wxBuffer);
551 wxBuffer[len] = 13;
552 wxBuffer[len+1] = 10;
553 wxBuffer[len+2] = 0;
554 strcpy(tmp_buffer+pos, wxBuffer);
555 pos += strlen(wxBuffer);
556 no_lines++;
557 }
558
559 // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)tmp_buffer);
560 SetWindowText((HWND) GetHWND(), tmp_buffer);
561 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
562 farfree(tmp_buffer);
563
564 return TRUE;
565 }
566 return FALSE;
567 }
568
569 // If file is null, try saved file name first
570 // Returns TRUE if succeeds.
571 bool wxTextCtrl::SaveFile(const wxString& file)
572 {
573 wxString theFile(file);
574 if (theFile == "")
575 theFile = m_fileName;
576 if (theFile == "")
577 return FALSE;
578 m_fileName = theFile;
579
580 ofstream output((char*) (const char*) theFile);
581 if (output.bad())
582 return FALSE;
583
584 // This will only save 64K max
585 unsigned long nbytes = SendMessage((HWND) GetHWND(), WM_GETTEXTLENGTH, 0, 0);
586 char *tmp_buffer = (char*)farmalloc((size_t)(nbytes+1));
587 SendMessage((HWND) GetHWND(), WM_GETTEXT, (WPARAM)(nbytes+1), (LPARAM)tmp_buffer);
588 char *pstr = tmp_buffer;
589
590 // Convert \r\n to just \n
591 while (*pstr)
592 {
593 if (*pstr != '\r')
594 output << *pstr;
595 pstr++;
596 }
597
598 farfree(tmp_buffer);
599 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
600
601 return TRUE;
602 }
603
604 void wxTextCtrl::WriteText(const wxString& text)
605 {
606 // Covert \n to \r\n
607 int len = text.Length();
608 char *newtext = new char[(len*2)+1];
609 int i = 0;
610 int j = 0;
611 while (i < len)
612 {
613 if (text[i] == '\n')
614 {
615 newtext[j] = '\r';
616 j ++;
617 }
618 newtext[j] = text[i];
619 i ++;
620 j ++;
621 }
622 newtext[j] = 0;
623 SendMessage((HWND) GetHWND(), EM_REPLACESEL, 0, (LPARAM)newtext);
624 delete[] newtext;
625 }
626
627 void wxTextCtrl::Clear(void)
628 {
629 // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)"");
630 SetWindowText((HWND) GetHWND(), "");
631 }
632
633 bool wxTextCtrl::IsModified(void) const
634 {
635 return (SendMessage((HWND) GetHWND(), EM_GETMODIFY, 0, 0) != 0);
636 }
637
638 // Makes 'unmodified'
639 void wxTextCtrl::DiscardEdits(void)
640 {
641 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
642 }
643
644 /*
645 * Some of the following functions are yet to be implemented
646 *
647 */
648
649 int wxTextCtrl::GetNumberOfLines(void) const
650 {
651 return (int)SendMessage((HWND) GetHWND(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
652 }
653
654 long wxTextCtrl::XYToPosition(long x, long y) const
655 {
656 HWND hWnd = (HWND) GetHWND();
657
658 // This gets the char index for the _beginning_ of this line
659 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
660 return (long)(x + charIndex);
661 }
662
663 void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
664 {
665 HWND hWnd = (HWND) GetHWND();
666
667 // This gets the line number containing the character
668 int lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0);
669 // This gets the char index for the _beginning_ of this line
670 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
671 // The X position must therefore be the different between pos and charIndex
672 *x = (long)(pos - charIndex);
673 *y = (long)lineNo;
674 }
675
676 void wxTextCtrl::ShowPosition(long pos)
677 {
678 HWND hWnd = (HWND) GetHWND();
679
680 // To scroll to a position, we pass the number of lines and characters
681 // to scroll *by*. This means that we need to:
682 // (1) Find the line position of the current line.
683 // (2) Find the line position of pos.
684 // (3) Scroll by (pos - current).
685 // For now, ignore the horizontal scrolling.
686
687 // Is this where scrolling is relative to - the line containing the caret?
688 // Or is the first visible line??? Try first visible line.
689 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
690
691 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
692
693 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
694
695 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
696
697 /*
698 wxDebugMsg("Caret line: %d; Current visible line: %d; Specified line: %d; lines to scroll: %d\n",
699 currentLineLineNo1, currentLineLineNo, specifiedLineLineNo, linesToScroll);
700 */
701
702 if (linesToScroll != 0)
703 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)MAKELPARAM(linesToScroll, 0));
704 }
705
706 int wxTextCtrl::GetLineLength(long lineNo) const
707 {
708 long charIndex = XYToPosition(0, lineNo);
709 HWND hWnd = (HWND) GetHWND();
710 int len = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0);
711 return len;
712 }
713
714 wxString wxTextCtrl::GetLineText(long lineNo) const
715 {
716 HWND hWnd = (HWND) GetHWND();
717 *(WORD *)wxBuffer = 512;
718 int noChars = (int)SendMessage(hWnd, EM_GETLINE, (WPARAM)lineNo, (LPARAM)wxBuffer);
719 wxBuffer[noChars] = 0;
720 return wxString(wxBuffer);
721 }
722
723 /*
724 * Text item
725 */
726
727 void wxTextCtrl::Command(wxCommandEvent & event)
728 {
729 SetValue (event.GetString());
730 ProcessCommand (event);
731 }
732
733 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
734 {
735 // By default, load the first file into the text window.
736 if (event.GetNumberOfFiles() > 0)
737 {
738 LoadFile(event.GetFiles()[0]);
739 }
740 }
741
742 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
743 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
744
745 //=========================================================================
746 // Called then the buffer is full (gcc 2.6.3)
747 // or when "endl" is output (Borland 4.5)
748 //=========================================================================
749 // Class declaration using multiple inheritance doesn't work properly for
750 // Borland. See note in textctrl.h.
751 #ifndef NO_TEXT_WINDOW_STREAM
752 int wxTextCtrl::overflow(int c)
753 {
754 // Make sure there is a holding area
755 // this is not needed in <iostream> usage as it automagically allocates
756 // it, but does someone want to emulate it for safety's sake?
757 #if wxUSE_IOSTREAMH
758 if ( allocate()==EOF )
759 {
760 wxLogError("Streambuf allocation failed");
761 return EOF;
762 }
763 #endif
764
765 // Verify that there are no characters in get area
766 if ( gptr() && gptr() < egptr() )
767 {
768 wxError("Who's trespassing my get area?","Internal error");
769 return EOF;
770 }
771
772 // Reset get area
773 setg(0,0,0);
774
775 // Make sure there is a put area
776 if ( ! pptr() )
777 {
778 /* This doesn't seem to be fatal so comment out error message */
779 // wxError("Put area not opened","Internal error");
780
781 #if wxUSE_IOSTREAMH
782 setp( base(), base() );
783 #else
784 setp( pbase(), pbase() );
785 #endif
786 }
787
788 // Determine how many characters have been inserted but no consumed
789 int plen = pptr() - pbase();
790
791 // Now Jerry relies on the fact that the buffer is at least 2 chars
792 // long, but the holding area "may be as small as 1" ???
793 // And we need an additional \0, so let's keep this inefficient but
794 // safe copy.
795
796 // If c!=EOF, it is a character that must also be comsumed
797 int xtra = c==EOF? 0 : 1;
798
799 // Write temporary C-string to wxTextWindow
800 {
801 char *txt = new char[plen+xtra+1];
802 memcpy(txt, pbase(), plen);
803 txt[plen] = (char)c; // append c
804 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
805 // If the put area already contained \0, output will be truncated there
806 WriteText(txt);
807 delete[] txt;
808 }
809
810 // Reset put area
811 setp(pbase(), epptr());
812
813 #if defined(__WATCOMC__)
814 return __NOT_EOF;
815 #elif defined(zapeof) // HP-UX (all cfront based?)
816 return zapeof(c);
817 #else
818 return c!=EOF ? c : 0; // this should make everybody happy
819 #endif
820
821 /* OLD CODE
822 int len = pptr() - pbase();
823 char *txt = new char[len+1];
824 strncpy(txt, pbase(), len);
825 txt[len] = '\0';
826 (*this) << txt;
827 setp(pbase(), epptr());
828 delete[] txt;
829 return EOF;
830 */
831 }
832
833 //=========================================================================
834 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
835 //=========================================================================
836 int wxTextCtrl::sync(void)
837 {
838 // Verify that there are no characters in get area
839 if ( gptr() && gptr() < egptr() )
840 {
841 wxError("Who's trespassing my get area?","Internal error");
842 return EOF;
843 }
844
845 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
846
847 return 0;
848 /* OLD CODE
849 int len = pptr() - pbase();
850 char *txt = new char[len+1];
851 strncpy(txt, pbase(), len);
852 txt[len] = '\0';
853 (*this) << txt;
854 setp(pbase(), epptr());
855 delete[] txt;
856 return 0;
857 */
858 }
859
860 //=========================================================================
861 // Should not be called by a "ostream". Used by a "istream"
862 //=========================================================================
863 int wxTextCtrl::underflow(void)
864 {
865 return EOF;
866 }
867 #endif
868
869 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
870 {
871 WriteText(s);
872 return *this;
873 }
874
875 wxTextCtrl& wxTextCtrl::operator<<(float f)
876 {
877 wxString str;
878 str.Printf("%.2f", f);
879 WriteText(str);
880 return *this;
881 }
882
883 wxTextCtrl& wxTextCtrl::operator<<(double d)
884 {
885 wxString str;
886 str.Printf("%.2f", d);
887 WriteText(str);
888 return *this;
889 }
890
891 wxTextCtrl& wxTextCtrl::operator<<(int i)
892 {
893 wxString str;
894 str.Printf("%d", i);
895 WriteText(str);
896 return *this;
897 }
898
899 wxTextCtrl& wxTextCtrl::operator<<(long i)
900 {
901 wxString str;
902 str.Printf("%ld", i);
903 WriteText(str);
904 return *this;
905 }
906
907 wxTextCtrl& wxTextCtrl::operator<<(const char c)
908 {
909 char buf[2];
910
911 buf[0] = c;
912 buf[1] = 0;
913 WriteText(buf);
914 return *this;
915 }
916
917 WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
918 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
919 {
920 #if CTL3D
921 if ( m_useCtl3D )
922 {
923 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
924 return (WXHBRUSH) hbrush;
925 }
926 #endif
927
928 if (GetParent()->GetTransparentBackground())
929 SetBkMode((HDC) pDC, TRANSPARENT);
930 else
931 SetBkMode((HDC) pDC, OPAQUE);
932
933 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
934 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
935
936 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
937
938 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
939 // has a zero usage count.
940 // NOT NOW - will be cleaned up at end of app.
941 // backgroundBrush->RealizeResource();
942 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
943 }
944
945 void wxTextCtrl::OnChar(wxKeyEvent& event)
946 {
947 // Fix by Marcel Rasche to allow Alt-Ctrl insertion of special characters
948 switch(event.KeyCode())
949 {
950 case '{':
951 case '}':
952 case '[':
953 case ']':
954 case '|':
955 case '~':
956 case '\\':
957 {
958 char c=(char)event.KeyCode();
959 *this << c;
960 }
961 break;
962 }
963 if ( (event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
964 {
965 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
966 event.SetEventObject( this );
967 if ( GetEventHandler()->ProcessEvent(event) )
968 return;
969 }
970 else if ( event.KeyCode() == WXK_TAB ) {
971 wxNavigationKeyEvent event;
972 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
973 event.SetWindowChange(FALSE);
974 event.SetEventObject(this);
975
976 if ( GetEventHandler()->ProcessEvent(event) )
977 return;
978 }
979
980 event.Skip();
981 }
982
983 long wxTextCtrl::MSWGetDlgCode()
984 {
985 long lRc = DLGC_WANTCHARS | DLGC_WANTARROWS;
986 if ( m_windowStyle & wxPROCESS_ENTER )
987 lRc |= DLGC_WANTMESSAGE;
988 else if ( m_windowStyle & wxTE_MULTILINE )
989 lRc |= DLGC_WANTMESSAGE;
990
991 return lRc;
992 }
993
994 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
995 {
996 if ( m_windowStyle & wxTE_MULTILINE )
997 {
998 // No flicker - only problem is we probably can't change the background
999 Default();
1000 /*
1001 RECT rect;
1002 ::GetClientRect((HWND) GetHWND(), &rect);
1003
1004 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1005 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
1006
1007 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
1008 ::DeleteObject(hBrush);
1009 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
1010 */
1011 }
1012 // wxWindow::OnEraseBackground(event);
1013 }
1014
1015 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
1016 {
1017 /*
1018 // Debugging
1019 wxDebugMsg("Edit control %d: ", (int)id);
1020 switch (param)
1021 {
1022 case EN_SETFOCUS:
1023 wxDebugMsg("EN_SETFOCUS\n");
1024 break;
1025 case EN_KILLFOCUS:
1026 wxDebugMsg("EN_KILLFOCUS\n");
1027 break;
1028 case EN_CHANGE:
1029 wxDebugMsg("EN_CHANGE\n");
1030 break;
1031 case EN_UPDATE:
1032 wxDebugMsg("EN_UPDATE\n");
1033 break;
1034 case EN_ERRSPACE:
1035 wxDebugMsg("EN_ERRSPACE\n");
1036 break;
1037 case EN_MAXTEXT:
1038 wxDebugMsg("EN_MAXTEXT\n");
1039 break;
1040 case EN_HSCROLL:
1041 wxDebugMsg("EN_HSCROLL\n");
1042 break;
1043 case EN_VSCROLL:
1044 wxDebugMsg("EN_VSCROLL\n");
1045 break;
1046 default:
1047 wxDebugMsg("Unknown EDIT notification\n");
1048 break;
1049 }
1050 */
1051 switch (param)
1052 {
1053 case EN_SETFOCUS:
1054 case EN_KILLFOCUS:
1055 {
1056 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1057 : wxEVT_SET_FOCUS,
1058 m_windowId);
1059 event.SetEventObject( this );
1060 GetEventHandler()->ProcessEvent(event);
1061 }
1062 break;
1063
1064 case EN_CHANGE:
1065 {
1066 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1067 wxString val(GetValue());
1068 if ( !val.IsNull() )
1069 event.m_commandString = WXSTRINGCAST val;
1070 event.SetEventObject( this );
1071 ProcessCommand(event);
1072 }
1073 break;
1074
1075 // the other notification messages are not processed
1076 case EN_UPDATE:
1077 case EN_ERRSPACE:
1078 case EN_MAXTEXT:
1079 case EN_HSCROLL:
1080 case EN_VSCROLL:
1081 default:
1082 return FALSE;
1083 }
1084
1085 // processed
1086 return TRUE;
1087 }
1088
1089
1090 // For Rich Edit controls. Do we need it?
1091 #if 0
1092 #if defined(__WIN95__)
1093 bool wxTextCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam)
1094 {
1095 wxCommandEvent event(0, m_windowId);
1096 int eventType = 0;
1097 NMHDR *hdr1 = (NMHDR *) lParam;
1098 switch ( hdr1->code )
1099 {
1100 // Insert case code here
1101 default :
1102 return wxControl::MSWNotify(wParam, lParam);
1103 break;
1104 }
1105
1106 event.SetEventObject( this );
1107 event.SetEventType(eventType);
1108
1109 if ( !GetEventHandler()->ProcessEvent(event) )
1110 return FALSE;
1111
1112 return TRUE;
1113 }
1114 #endif
1115 #endif
1116