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