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