]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
wxToolTip
[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
VZ
127 {
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
2bda0e17
KB
180#if CTL3D
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
KB
456{
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);
474}
475
debe6624 476void wxTextCtrl::Remove(long from, long to)
2bda0e17
KB
477{
478 HWND hWnd = (HWND) GetHWND();
479 long fromChar = from;
480 long toChar = to;
481
482 // Cut all selected text
483#ifdef __WIN32__
484 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
485#else
486 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
487#endif
488 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
489}
490
debe6624 491void wxTextCtrl::SetSelection(long from, long to)
2bda0e17
KB
492{
493 HWND hWnd = (HWND) GetHWND();
494 long fromChar = from;
495 long toChar = to;
496 // if from and to are both -1, it means
497 // (in wxWindows) that all text should be selected.
498 // This translates into Windows convention
499 if ((from == -1) && (to == -1))
500 {
501 fromChar = 0;
502 toChar = -1;
503 }
504
505#ifdef __WIN32__
506 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
507 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
508#else
509 // WPARAM is 0: selection is scrolled into view
510 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
511#endif
512}
513
514bool wxTextCtrl::LoadFile(const wxString& file)
515{
2432b92d 516 if (!wxFileExists(WXSTRINGCAST file))
2bda0e17
KB
517 return FALSE;
518
b823f5a1 519 m_fileName = file;
2bda0e17
KB
520
521 Clear();
522
fbc535ff
JS
523// ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in);
524 ifstream input(WXSTRINGCAST file, ios::in);
2bda0e17
KB
525
526 if (!input.bad())
527 {
528 // Previously a SETSEL/REPLACESEL call-pair were done to insert
529 // line by line into the control. Apart from being very slow this
530 // was limited to 32K of text by the external interface presenting
531 // positions as signed shorts. Now load in one chunk...
532 // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits...
533
ce3ed50d
JS
534#ifdef __SALFORDC__
535 struct _stat stat_buf;
536 if (stat((char*) (const char*) file, &stat_buf) < 0)
537 return FALSE;
538#else
2bda0e17
KB
539 struct stat stat_buf;
540 if (stat(file, &stat_buf) < 0)
541 return FALSE;
ce3ed50d
JS
542#endif
543
2bda0e17
KB
544// char *tmp_buffer = (char*)farmalloc(stat_buf.st_size+1);
545 // This may need to be a bigger buffer than the file size suggests,
546 // if it's a UNIX file. Give it an extra 1000 just in case.
547 char *tmp_buffer = (char*)farmalloc((size_t)(stat_buf.st_size+1+1000));
548 long no_lines = 0;
549 long pos = 0;
550 while (!input.eof() && input.peek() != EOF)
551 {
552 input.getline(wxBuffer, 500);
c085e333
VZ
553 int len = strlen(wxBuffer);
554 wxBuffer[len] = 13;
555 wxBuffer[len+1] = 10;
556 wxBuffer[len+2] = 0;
557 strcpy(tmp_buffer+pos, wxBuffer);
558 pos += strlen(wxBuffer);
559 no_lines++;
2bda0e17
KB
560 }
561
562// SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)tmp_buffer);
563 SetWindowText((HWND) GetHWND(), tmp_buffer);
564 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
565 farfree(tmp_buffer);
566
567 return TRUE;
568 }
569 return FALSE;
570}
571
572// If file is null, try saved file name first
573// Returns TRUE if succeeds.
574bool wxTextCtrl::SaveFile(const wxString& file)
575{
cd471848 576 wxString theFile(file);
2bda0e17 577
cd471848
VZ
578 if (theFile == "")
579 theFile = m_fileName;
580
581 if (theFile == "")
582 return FALSE;
583
584 m_fileName = theFile;
585
586 ofstream output((char*) (const char*) theFile);
587 if (output.bad())
588 return FALSE;
2bda0e17
KB
589
590 // This will only save 64K max
591 unsigned long nbytes = SendMessage((HWND) GetHWND(), WM_GETTEXTLENGTH, 0, 0);
592 char *tmp_buffer = (char*)farmalloc((size_t)(nbytes+1));
593 SendMessage((HWND) GetHWND(), WM_GETTEXT, (WPARAM)(nbytes+1), (LPARAM)tmp_buffer);
594 char *pstr = tmp_buffer;
595
cd471848
VZ
596 // Convert \r\n to just \n
597 while (*pstr)
598 {
599 if (*pstr != '\r')
600 output << *pstr;
601 pstr++;
602 }
2bda0e17
KB
603
604 farfree(tmp_buffer);
605 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
606
607 return TRUE;
608}
609
610void wxTextCtrl::WriteText(const wxString& text)
611{
612 // Covert \n to \r\n
613 int len = text.Length();
614 char *newtext = new char[(len*2)+1];
615 int i = 0;
616 int j = 0;
617 while (i < len)
618 {
619 if (text[i] == '\n')
620 {
621 newtext[j] = '\r';
622 j ++;
623 }
624 newtext[j] = text[i];
625 i ++;
626 j ++;
627 }
628 newtext[j] = 0;
629 SendMessage((HWND) GetHWND(), EM_REPLACESEL, 0, (LPARAM)newtext);
630 delete[] newtext;
631}
632
cd471848 633void wxTextCtrl::Clear()
2bda0e17
KB
634{
635// SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)"");
636 SetWindowText((HWND) GetHWND(), "");
637}
638
cd471848 639bool wxTextCtrl::IsModified() const
2bda0e17
KB
640{
641 return (SendMessage((HWND) GetHWND(), EM_GETMODIFY, 0, 0) != 0);
642}
643
644// Makes 'unmodified'
cd471848 645void wxTextCtrl::DiscardEdits()
2bda0e17
KB
646{
647 SendMessage((HWND) GetHWND(), EM_SETMODIFY, FALSE, 0L);
648}
649
650/*
651 * Some of the following functions are yet to be implemented
652 *
653 */
654
cd471848 655int wxTextCtrl::GetNumberOfLines() const
2bda0e17
KB
656{
657 return (int)SendMessage((HWND) GetHWND(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
658}
659
debe6624 660long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17
KB
661{
662 HWND hWnd = (HWND) GetHWND();
663
664 // This gets the char index for the _beginning_ of this line
665 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
666 return (long)(x + charIndex);
667}
668
debe6624 669void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17
KB
670{
671 HWND hWnd = (HWND) GetHWND();
672
673 // This gets the line number containing the character
674 int lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0);
675 // This gets the char index for the _beginning_ of this line
676 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
677 // The X position must therefore be the different between pos and charIndex
678 *x = (long)(pos - charIndex);
679 *y = (long)lineNo;
680}
681
debe6624 682void wxTextCtrl::ShowPosition(long pos)
2bda0e17
KB
683{
684 HWND hWnd = (HWND) GetHWND();
685
686 // To scroll to a position, we pass the number of lines and characters
687 // to scroll *by*. This means that we need to:
688 // (1) Find the line position of the current line.
689 // (2) Find the line position of pos.
690 // (3) Scroll by (pos - current).
691 // For now, ignore the horizontal scrolling.
692
693 // Is this where scrolling is relative to - the line containing the caret?
694 // Or is the first visible line??? Try first visible line.
695// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
696
697 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
698
699 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
700
701 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
702
703/*
704 wxDebugMsg("Caret line: %d; Current visible line: %d; Specified line: %d; lines to scroll: %d\n",
705 currentLineLineNo1, currentLineLineNo, specifiedLineLineNo, linesToScroll);
706*/
707
708 if (linesToScroll != 0)
709 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)MAKELPARAM(linesToScroll, 0));
710}
711
debe6624 712int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
713{
714 long charIndex = XYToPosition(0, lineNo);
715 HWND hWnd = (HWND) GetHWND();
716 int len = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0);
717 return len;
718}
719
debe6624 720wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17
KB
721{
722 HWND hWnd = (HWND) GetHWND();
723 *(WORD *)wxBuffer = 512;
724 int noChars = (int)SendMessage(hWnd, EM_GETLINE, (WPARAM)lineNo, (LPARAM)wxBuffer);
725 wxBuffer[noChars] = 0;
c085e333 726 return wxString(wxBuffer);
2bda0e17
KB
727}
728
729/*
730 * Text item
731 */
732
733void wxTextCtrl::Command(wxCommandEvent & event)
734{
735 SetValue (event.GetString());
736 ProcessCommand (event);
737}
738
739void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
740{
741 // By default, load the first file into the text window.
742 if (event.GetNumberOfFiles() > 0)
743 {
744 LoadFile(event.GetFiles()[0]);
745 }
746}
747
748// The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
749// AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
750
751//=========================================================================
752// Called then the buffer is full (gcc 2.6.3)
753// or when "endl" is output (Borland 4.5)
754//=========================================================================
755// Class declaration using multiple inheritance doesn't work properly for
3f1af920 756// Borland. See note in textctrl.h.
2bda0e17
KB
757#ifndef NO_TEXT_WINDOW_STREAM
758int wxTextCtrl::overflow(int c)
759{
760 // Make sure there is a holding area
7cf98a65
UU
761 // this is not needed in <iostream> usage as it automagically allocates
762 // it, but does someone want to emulate it for safety's sake?
763#if wxUSE_IOSTREAMH
2bda0e17
KB
764 if ( allocate()==EOF )
765 {
2432b92d 766 wxLogError("Streambuf allocation failed");
2bda0e17
KB
767 return EOF;
768 }
7cf98a65 769#endif
2bda0e17
KB
770
771 // Verify that there are no characters in get area
772 if ( gptr() && gptr() < egptr() )
773 {
774 wxError("Who's trespassing my get area?","Internal error");
775 return EOF;
776 }
777
778 // Reset get area
779 setg(0,0,0);
780
781 // Make sure there is a put area
782 if ( ! pptr() )
783 {
784/* This doesn't seem to be fatal so comment out error message */
785// wxError("Put area not opened","Internal error");
7cf98a65
UU
786
787#if wxUSE_IOSTREAMH
788 setp( base(), base() );
789#else
790 setp( pbase(), pbase() );
791#endif
2bda0e17
KB
792 }
793
794 // Determine how many characters have been inserted but no consumed
795 int plen = pptr() - pbase();
796
797 // Now Jerry relies on the fact that the buffer is at least 2 chars
798 // long, but the holding area "may be as small as 1" ???
799 // And we need an additional \0, so let's keep this inefficient but
800 // safe copy.
801
802 // If c!=EOF, it is a character that must also be comsumed
803 int xtra = c==EOF? 0 : 1;
804
805 // Write temporary C-string to wxTextWindow
806 {
807 char *txt = new char[plen+xtra+1];
808 memcpy(txt, pbase(), plen);
809 txt[plen] = (char)c; // append c
810 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
811 // If the put area already contained \0, output will be truncated there
812 WriteText(txt);
813 delete[] txt;
814 }
815
816 // Reset put area
817 setp(pbase(), epptr());
818
819#if defined(__WATCOMC__)
820 return __NOT_EOF;
821#elif defined(zapeof) // HP-UX (all cfront based?)
822 return zapeof(c);
823#else
824 return c!=EOF ? c : 0; // this should make everybody happy
825#endif
826
827/* OLD CODE
828 int len = pptr() - pbase();
829 char *txt = new char[len+1];
830 strncpy(txt, pbase(), len);
831 txt[len] = '\0';
832 (*this) << txt;
833 setp(pbase(), epptr());
834 delete[] txt;
835 return EOF;
836*/
837}
838
839//=========================================================================
840// called then "endl" is output (gcc) or then explicit sync is done (Borland)
841//=========================================================================
cd471848 842int wxTextCtrl::sync()
2bda0e17
KB
843{
844 // Verify that there are no characters in get area
845 if ( gptr() && gptr() < egptr() )
846 {
847 wxError("Who's trespassing my get area?","Internal error");
848 return EOF;
849 }
850
851 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
852
853 return 0;
854/* OLD CODE
855 int len = pptr() - pbase();
856 char *txt = new char[len+1];
857 strncpy(txt, pbase(), len);
858 txt[len] = '\0';
859 (*this) << txt;
860 setp(pbase(), epptr());
861 delete[] txt;
862 return 0;
863*/
864}
865
866//=========================================================================
867// Should not be called by a "ostream". Used by a "istream"
868//=========================================================================
cd471848 869int wxTextCtrl::underflow()
2bda0e17
KB
870{
871 return EOF;
872}
873#endif
874
875wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
876{
877 WriteText(s);
878 return *this;
879}
880
debe6624 881wxTextCtrl& wxTextCtrl::operator<<(float f)
2bda0e17 882{
b823f5a1
JS
883 wxString str;
884 str.Printf("%.2f", f);
885 WriteText(str);
886 return *this;
2bda0e17
KB
887}
888
debe6624 889wxTextCtrl& wxTextCtrl::operator<<(double d)
2bda0e17 890{
b823f5a1
JS
891 wxString str;
892 str.Printf("%.2f", d);
893 WriteText(str);
894 return *this;
2bda0e17
KB
895}
896
debe6624 897wxTextCtrl& wxTextCtrl::operator<<(int i)
2bda0e17 898{
b823f5a1
JS
899 wxString str;
900 str.Printf("%d", i);
901 WriteText(str);
902 return *this;
2bda0e17
KB
903}
904
debe6624 905wxTextCtrl& wxTextCtrl::operator<<(long i)
2bda0e17 906{
b823f5a1
JS
907 wxString str;
908 str.Printf("%ld", i);
909 WriteText(str);
910 return *this;
2bda0e17
KB
911}
912
913wxTextCtrl& wxTextCtrl::operator<<(const char c)
914{
b823f5a1 915 char buf[2];
2bda0e17 916
b823f5a1
JS
917 buf[0] = c;
918 buf[1] = 0;
919 WriteText(buf);
920 return *this;
2bda0e17
KB
921}
922
debe6624 923WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
c085e333 924 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17
KB
925{
926#if CTL3D
927 if ( m_useCtl3D )
928 {
929 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
930 return (WXHBRUSH) hbrush;
931 }
932#endif
933
934 if (GetParent()->GetTransparentBackground())
935 SetBkMode((HDC) pDC, TRANSPARENT);
936 else
937 SetBkMode((HDC) pDC, OPAQUE);
938
939 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
940 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
941
942 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
943
944 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
945 // has a zero usage count.
946 // NOT NOW - will be cleaned up at end of app.
947// backgroundBrush->RealizeResource();
948 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
949}
950
951void wxTextCtrl::OnChar(wxKeyEvent& event)
952{
cd471848
VZ
953 switch( event.KeyCode() )
954 {
955 // Fix by Marcel Rasche to allow Alt-Ctrl insertion of special characters
956 case '{':
957 case '}':
958 case '[':
959 case ']':
960 case '|':
961 case '~':
962 case '\\':
963 {
964 char c = (char)event.KeyCode();
965 *this << c;
966 }
967 break;
968
969 case WXK_RETURN:
970 wxASSERT_MSG( m_windowStyle & wxTE_PROCESS_ENTER,
971 "this text ctrl should never receive return" );
972 {
973 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
974 event.SetEventObject( this );
975 if ( GetEventHandler()->ProcessEvent(event) )
976 return;
977 }
978
979 case WXK_TAB:
980 // only produce navigation event if we don't process TAB ourself
981 if ( !(m_windowStyle & wxTE_PROCESS_TAB) )
982 {
983 wxNavigationKeyEvent event;
984 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
985 event.SetWindowChange(FALSE);
986 event.SetEventObject(this);
987
988 if ( GetEventHandler()->ProcessEvent(event) )
989 return;
990 }
991 }
992
993 // don't just call event.Skip() because this will cause TABs and ENTERs
994 // be passed upwards and we don't always want this - instead process it
995 // right here
996 Default();
2bda0e17
KB
997}
998
ae29de83 999long wxTextCtrl::MSWGetDlgCode()
2bda0e17 1000{
cd471848
VZ
1001 // we always want the characters and the arrows
1002 long lRc = DLGC_WANTCHARS | DLGC_WANTARROWS;
1003
1004 // we may have several different cases:
1005 // 1. normal case: both TAB and ENTER are used for dialog navigation
1006 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1007 // control in the dialog
1008 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1009 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1010 // the next control
1011 if ( m_windowStyle & wxTE_PROCESS_ENTER )
1012 lRc |= DLGC_WANTMESSAGE;
1013 if ( m_windowStyle & wxTE_PROCESS_TAB )
1014 lRc |= DLGC_WANTTAB;
1015
1016 return lRc;
ae29de83
VZ
1017}
1018
2bda0e17
KB
1019void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1020{
1021 if ( m_windowStyle & wxTE_MULTILINE )
1022 {
1023 // No flicker - only problem is we probably can't change the background
1024 Default();
1025/*
1026 RECT rect;
1027 ::GetClientRect((HWND) GetHWND(), &rect);
1028
1029 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1030 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
1031
1032 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
1033 ::DeleteObject(hBrush);
1034 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
1035*/
1036 }
1037// wxWindow::OnEraseBackground(event);
1038}
1039
debe6624 1040bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17
KB
1041{
1042/*
1043 // Debugging
1044 wxDebugMsg("Edit control %d: ", (int)id);
1045 switch (param)
1046 {
1047 case EN_SETFOCUS:
1048 wxDebugMsg("EN_SETFOCUS\n");
1049 break;
1050 case EN_KILLFOCUS:
1051 wxDebugMsg("EN_KILLFOCUS\n");
1052 break;
1053 case EN_CHANGE:
1054 wxDebugMsg("EN_CHANGE\n");
1055 break;
1056 case EN_UPDATE:
1057 wxDebugMsg("EN_UPDATE\n");
1058 break;
1059 case EN_ERRSPACE:
1060 wxDebugMsg("EN_ERRSPACE\n");
1061 break;
1062 case EN_MAXTEXT:
1063 wxDebugMsg("EN_MAXTEXT\n");
1064 break;
1065 case EN_HSCROLL:
1066 wxDebugMsg("EN_HSCROLL\n");
1067 break;
1068 case EN_VSCROLL:
1069 wxDebugMsg("EN_VSCROLL\n");
1070 break;
1071 default:
1072 wxDebugMsg("Unknown EDIT notification\n");
1073 break;
1074 }
1075*/
2bda0e17
KB
1076 switch (param)
1077 {
1078 case EN_SETFOCUS:
2bda0e17 1079 case EN_KILLFOCUS:
ae29de83
VZ
1080 {
1081 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1082 : wxEVT_SET_FOCUS,
1083 m_windowId);
1084 event.SetEventObject( this );
02800301 1085 GetEventHandler()->ProcessEvent(event);
ae29de83 1086 }
2bda0e17 1087 break;
ae29de83 1088
2bda0e17 1089 case EN_CHANGE:
ae29de83
VZ
1090 {
1091 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1092 wxString val(GetValue());
1093 if ( !val.IsNull() )
1094 event.m_commandString = WXSTRINGCAST val;
1095 event.SetEventObject( this );
1096 ProcessCommand(event);
1097 }
2bda0e17 1098 break;
ae29de83
VZ
1099
1100 // the other notification messages are not processed
1101 case EN_UPDATE:
2bda0e17 1102 case EN_ERRSPACE:
2bda0e17 1103 case EN_MAXTEXT:
2bda0e17 1104 case EN_HSCROLL:
2bda0e17 1105 case EN_VSCROLL:
2bda0e17 1106 default:
ae29de83 1107 return FALSE;
2bda0e17 1108 }
2bda0e17 1109
ae29de83
VZ
1110 // processed
1111 return TRUE;
2bda0e17
KB
1112}
1113
1114
1115// For Rich Edit controls. Do we need it?
1116#if 0
57c208c5 1117#if wxUSE_RICHEDIT
debe6624 1118bool wxTextCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 1119{
c085e333
VZ
1120 wxCommandEvent event(0, m_windowId);
1121 int eventType = 0;
1122 NMHDR *hdr1 = (NMHDR *) lParam;
1123 switch ( hdr1->code )
1124 {
1125 // Insert case code here
1126 default :
1127 return wxControl::MSWNotify(wParam, lParam);
1128 break;
1129 }
1130
1131 event.SetEventObject( this );
1132 event.SetEventType(eventType);
1133
02800301 1134 if ( !GetEventHandler()->ProcessEvent(event) )
c085e333
VZ
1135 return FALSE;
1136
1137 return TRUE;
2bda0e17
KB
1138}
1139#endif
1140#endif
1141