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