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