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