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