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