]> git.saurik.com Git - wxWidgets.git/blob - src/os2/textctrl.cpp
centralized the handling of border styles; added borders support for wxListBox and...
[wxWidgets.git] / src / os2 / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/textctrl.h"
21 #include "wx/scrolwin.h"
22 #include "wx/settings.h"
23 #include "wx/brush.h"
24 #include "wx/utils.h"
25 #include "wx/log.h"
26 #endif
27
28 #include "wx/app.h"
29
30 #if wxUSE_CLIPBOARD
31 #include "wx/clipbrd.h"
32 #endif
33
34 #include "wx/textfile.h"
35
36 #include "wx/os2/private.h"
37
38 #include <string.h>
39 #include <stdlib.h>
40 #include <sys/types.h>
41
42 #if wxUSE_IOSTREAMH
43 # include <fstream.h>
44 #else
45 # include <fstream>
46 #endif
47
48 #if !defined(MLE_INDEX)
49 #define MLE_INDEX 0
50 #define MLE_RGB 1
51 #endif
52
53
54 // ----------------------------------------------------------------------------
55 // event tables and other macros
56 // ----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
59
60 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
61 EVT_CHAR(wxTextCtrl::OnChar)
62 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
63
64 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
65 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
66 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
67 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
68 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
69
70 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
71 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
72 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
73 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
74 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
75 END_EVENT_TABLE()
76
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // creation
84 // ----------------------------------------------------------------------------
85
86 wxTextCtrl::wxTextCtrl()
87 {
88 }
89
90 wxTextCtrl::~wxTextCtrl()
91 {
92 }
93
94 bool wxTextCtrl::Create(
95 wxWindow* pParent
96 , wxWindowID vId
97 , const wxString& rsValue
98 , const wxPoint& rPos
99 , const wxSize& rSize
100 , long lStyle
101 , const wxValidator& rValidator
102 , const wxString& rsName
103 )
104 {
105 //
106 // Base initialization
107 //
108 if ( !CreateBase( pParent
109 ,vId
110 ,rPos
111 ,rSize
112 ,lStyle
113 ,rValidator
114 ,rsName
115 ))
116 return false;
117
118 wxPoint vPos = rPos; // The OS/2 position
119 SWP vSwp;
120
121 if (pParent )
122 {
123 pParent->AddChild(this);
124 }
125
126 m_windowStyle = lStyle;
127 m_bIsMLE = false;
128
129 long lSstyle = WS_VISIBLE | WS_TABSTOP;
130
131 //
132 // Single and multiline edit fields are two different controls in PM
133 //
134 if ( m_windowStyle & wxTE_MULTILINE )
135 {
136 lSstyle |= MLS_BORDER | MLS_WORDWRAP;
137 m_bIsMLE = true;
138
139 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
140 lSstyle |= MLS_VSCROLL;
141 if (m_windowStyle & wxHSCROLL)
142 lSstyle |= MLS_HSCROLL;
143 if (m_windowStyle & wxTE_READONLY)
144 lSstyle |= MLS_READONLY;
145 }
146 else
147 {
148 lSstyle |= ES_LEFT | ES_AUTOSCROLL | ES_MARGIN;
149
150 if (m_windowStyle & wxHSCROLL)
151 lSstyle |= ES_AUTOSCROLL;
152 if (m_windowStyle & wxTE_READONLY)
153 lSstyle |= ES_READONLY;
154 if (m_windowStyle & wxTE_PASSWORD) // hidden input
155 lSstyle |= ES_UNREADABLE;
156 }
157
158 if (m_bIsMLE)
159 {
160 m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
161 ,WC_MLE // Window class
162 ,(PSZ)rsValue.c_str() // Initial Text
163 ,(ULONG)lSstyle // Style flags
164 ,(LONG)0 // X pos of origin
165 ,(LONG)0 // Y pos of origin
166 ,(LONG)0 // field width
167 ,(LONG)0 // field height
168 ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent
169 ,HWND_TOP // initial z position
170 ,(ULONG)vId // Window identifier
171 ,NULL // no control data
172 ,NULL // no Presentation parameters
173 );
174 }
175 else
176 {
177 m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
178 ,WC_ENTRYFIELD // Window class
179 ,(PSZ)rsValue.c_str() // Initial Text
180 ,(ULONG)lSstyle // Style flags
181 ,(LONG)0 // X pos of origin
182 ,(LONG)0 // Y pos of origin
183 ,(LONG)0 // field width
184 ,(LONG)0 // field height
185 ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent
186 ,HWND_TOP // initial z position
187 ,(ULONG)vId // Window identifier
188 ,NULL // no control data
189 ,NULL // no Presentation parameters
190 );
191 }
192
193 if (m_hWnd == 0)
194 {
195 return false;
196 }
197
198 SubclassWin(GetHWND());
199
200 //
201 // Set font, position, size and initial value
202 //
203 wxFont* pTextFont = new wxFont( 8
204 ,wxMODERN
205 ,wxNORMAL
206 ,wxNORMAL
207 );
208 SetFont(*pTextFont);
209 if (!rsValue.empty())
210 {
211 SetValue(rsValue);
212 }
213 SetupColours();
214 //
215 // If X and/or Y are not zero the difference is the compensation value
216 // for margins for OS/2 controls.
217 //
218 ::WinQueryWindowPos(m_hWnd, &vSwp);
219 SetXComp(vSwp.x);
220 SetYComp(vSwp.y);
221 SetSize( vPos.x - GetXComp()
222 ,vPos.y - GetYComp()
223 ,rSize.x
224 ,rSize.y
225 );
226 delete pTextFont;
227 return true;
228 } // end of wxTextCtrl::Create
229
230 //
231 // Make sure the window style (etc.) reflects the HWND style (roughly)
232 //
233 void wxTextCtrl::AdoptAttributesFromHWND()
234 {
235 HWND hWnd = GetHwnd();
236 LONG lStyle = ::WinQueryWindowULong(hWnd, QWL_STYLE);
237
238 wxWindow::AdoptAttributesFromHWND();
239
240 if (m_bIsMLE)
241 {
242 m_windowStyle |= wxTE_MULTILINE;
243 if (lStyle & MLS_READONLY)
244 m_windowStyle |= wxTE_READONLY;
245 }
246 else
247 {
248 if (lStyle & ES_UNREADABLE)
249 m_windowStyle |= wxTE_PASSWORD;
250 if (lStyle & ES_READONLY)
251 m_windowStyle |= wxTE_READONLY;
252 }
253 } // end of wxTextCtrl::AdoptAttributesFromHWND
254
255 WXDWORD wxTextCtrl::OS2GetStyle(
256 long lStyle
257 , WXDWORD* pdwExstyle
258 ) const
259 {
260 //
261 // Default border for the text controls is the sunken one
262 //
263 if ((lStyle & wxBORDER_MASK) == wxBORDER_DEFAULT )
264 {
265 lStyle |= wxBORDER_SUNKEN;
266 }
267
268 long dwStyle = wxControl::OS2GetStyle( lStyle
269 ,pdwExstyle
270 );
271
272 dwStyle = WS_VISIBLE | WS_TABSTOP;
273
274 //
275 // Single and multiline edit fields are two different controls in PM
276 //
277 if ( m_windowStyle & wxTE_MULTILINE )
278 {
279 dwStyle |= MLS_BORDER | MLS_WORDWRAP;
280 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
281 dwStyle |= MLS_VSCROLL;
282 if (m_windowStyle & wxHSCROLL)
283 dwStyle |= MLS_HSCROLL;
284 if (m_windowStyle & wxTE_READONLY)
285 dwStyle |= MLS_READONLY;
286 }
287 else
288 {
289 dwStyle |= ES_LEFT | ES_AUTOSCROLL | ES_MARGIN;
290 if (m_windowStyle & wxHSCROLL)
291 dwStyle |= ES_AUTOSCROLL;
292 if (m_windowStyle & wxTE_READONLY)
293 dwStyle |= ES_READONLY;
294 if (m_windowStyle & wxTE_PASSWORD) // hidden input
295 dwStyle |= ES_UNREADABLE;
296 }
297 return dwStyle;
298 } // end of wxTextCtrl::OS2GetStyle
299
300 void wxTextCtrl::SetWindowStyleFlag(
301 long lStyle
302 )
303 {
304 wxControl::SetWindowStyleFlag(lStyle);
305 } // end of wxTextCtrl::SetWindowStyleFlag
306
307 void wxTextCtrl::SetupColours()
308 {
309 wxColour vBkgndColour;
310
311 vBkgndColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
312 SetBackgroundColour(vBkgndColour);
313 SetForegroundColour(GetParent()->GetForegroundColour());
314 if (m_bIsMLE)
315 {
316 ::WinSendMsg( GetHwnd()
317 ,MLM_SETTEXTCOLOR
318 ,(MPARAM)GetParent()->GetForegroundColour().GetPixel()
319 ,(MPARAM)MLE_RGB
320 );
321 }
322 } // end of wxTextCtrl::SetupColours
323
324 // ----------------------------------------------------------------------------
325 // set/get the controls text
326 // ----------------------------------------------------------------------------
327
328 wxString wxTextCtrl::GetValue() const
329 {
330 wxString sStr = wxGetWindowText(GetHWND());
331 char* zStr = (char*)sStr.c_str();
332
333 for ( ; *zStr; zStr++ )
334 {
335 //
336 // this will replace \r\n with just \n
337 //
338 if (*zStr == '\n')
339 *zStr = '\0';
340 if (*zStr == '\r')
341 *zStr = '\n';
342 }
343 return sStr;
344 } // end of wxTextCtrl::GetValue
345
346 void wxTextCtrl::SetValue(
347 const wxString& rsValue
348 )
349 {
350 //
351 // If the text is long enough, it's faster to just set it instead of first
352 // comparing it with the old one (chances are that it will be different
353 // anyhow, this comparison is there to avoid flicker for small single-line
354 // edit controls mostly)
355 //
356 if ((rsValue.length() > 0x400) || (rsValue != GetValue()))
357 {
358 ::WinSetWindowText(GetHwnd(), (PSZ)rsValue.c_str());
359 AdjustSpaceLimit();
360 }
361 } // end of wxTextCtrl::SetValue
362
363 void wxTextCtrl::WriteText(
364 const wxString& rsValue
365 )
366 {
367 if (m_bIsMLE)
368 ::WinSendMsg(GetHwnd(), MLM_INSERT, MPARAM((PCHAR)rsValue.c_str()), MPARAM(0));
369 else
370 ::WinSetWindowText(GetHwnd(), (PSZ)rsValue.c_str());
371 AdjustSpaceLimit();
372 } // end of wxTextCtrl::WriteText
373
374 void wxTextCtrl::AppendText(
375 const wxString& rsText
376 )
377 {
378 SetInsertionPointEnd();
379 WriteText(rsText);
380 } // end of wxTextCtrl::AppendText
381
382 void wxTextCtrl::Clear()
383 {
384 ::WinSetWindowText(GetHwnd(), "");
385 } // end of wxTextCtrl::Clear
386
387 bool wxTextCtrl::EmulateKeyPress(
388 const wxKeyEvent& rEvent
389 )
390 {
391 SetFocus();
392 return(wxTextCtrlBase::EmulateKeyPress(rEvent));
393 } // end of wxTextCtrl::EmulateKeyPress
394
395 // ----------------------------------------------------------------------------
396 // Clipboard operations
397 // ----------------------------------------------------------------------------
398
399 void wxTextCtrl::Copy()
400 {
401 if (CanCopy())
402 {
403 HWND hWnd = GetHwnd();
404 if (m_bIsMLE)
405 ::WinSendMsg(hWnd, MLM_COPY, 0, 0);
406 else
407 ::WinSendMsg(hWnd, EM_COPY, 0, 0);
408 }
409 } // end of wxTextCtrl::Copy
410
411 void wxTextCtrl::Cut()
412 {
413 if (CanCut())
414 {
415 HWND hWnd = GetHwnd();
416
417 if (m_bIsMLE)
418 ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
419 else
420 ::WinSendMsg(hWnd, EM_CUT, 0, 0);
421 }
422 } // end of wxTextCtrl::Cut
423
424 void wxTextCtrl::Paste()
425 {
426 if (CanPaste())
427 {
428 HWND hWnd = GetHwnd();
429
430 ::WinSendMsg(hWnd, EM_PASTE, 0, 0);
431 }
432 } // end of wxTextCtrl::Paste
433
434 bool wxTextCtrl::CanCopy() const
435 {
436 //
437 // Can copy if there's a selection
438 //
439 long lFrom = 0L;
440 long lTo = 0L;
441
442 GetSelection(&lFrom, &lTo);
443 return (lFrom != lTo);
444 } // end of wxTextCtrl::CanCopy
445
446 bool wxTextCtrl::CanCut() const
447 {
448 //
449 // Can cut if there's a selection
450 //
451 long lFrom = 0L;
452 long lTo = 0L;
453
454 GetSelection(&lFrom, &lTo);
455 return (lFrom != lTo);
456 } // end of wxTextCtrl::CanCut
457
458 bool wxTextCtrl::CanPaste() const
459 {
460 bool bIsTextAvailable = false;
461
462 if (!IsEditable())
463 return false;
464
465 //
466 // Check for straight text on clipboard
467 //
468 if (::WinOpenClipbrd(vHabmain))
469 {
470 bIsTextAvailable = (::WinQueryClipbrdData(vHabmain, CF_TEXT) != 0);
471 ::WinCloseClipbrd(vHabmain);
472 }
473 return bIsTextAvailable;
474 } // end of wxTextCtrl::CanPaste
475
476 // ----------------------------------------------------------------------------
477 // Accessors
478 // ----------------------------------------------------------------------------
479
480 void wxTextCtrl::SetEditable(
481 bool bEditable
482 )
483 {
484 HWND hWnd = GetHwnd();
485
486 if (m_bIsMLE)
487 ::WinSendMsg(hWnd, MLM_SETREADONLY, MPFROMLONG(!bEditable), (MPARAM)0);
488 else
489 ::WinSendMsg(hWnd, EM_SETREADONLY, MPFROMLONG(!bEditable), (MPARAM)0);
490 } // end of wxTextCtrl::SetEditable
491
492 void wxTextCtrl::SetInsertionPoint(
493 long lPos
494 )
495 {
496 HWND hWnd = GetHwnd();
497
498 if (m_bIsMLE)
499 ::WinSendMsg(hWnd, MLM_SETSEL, (MPARAM)lPos, (MPARAM)lPos);
500 else
501 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lPos, (USHORT)lPos), (MPARAM)0);
502 } // end of wxTextCtrl::SetInsertionPoint
503
504 void wxTextCtrl::SetInsertionPointEnd()
505 {
506 wxTextPos lPos = GetLastPosition();
507
508 //
509 // We must not do anything if the caret is already there because calling
510 // SetInsertionPoint() thaws the controls if Freeze() had been called even
511 // if it doesn't actually move the caret anywhere and so the simple fact of
512 // doing it results in horrible flicker when appending big amounts of text
513 // to the control in a few chunks (see DoAddText() test in the text sample)
514 //
515 if (GetInsertionPoint() == GetLastPosition())
516 return;
517 SetInsertionPoint(lPos);
518 } // end of wxTextCtrl::SetInsertionPointEnd
519
520 long wxTextCtrl::GetInsertionPoint() const
521 {
522 WXDWORD dwPos = 0L;
523
524 if (m_bIsMLE)
525 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), MLM_QUERYSEL, (MPARAM)MLFQS_MINSEL, 0);
526 else
527 {
528 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), EM_QUERYSEL, 0, 0);
529 dwPos = SHORT1FROMMP((MPARAM)dwPos); // the first 16 bit value is the min pos
530 }
531 return (dwPos & 0xFFFF);
532 } // end of wxTextCtrl::GetInsertionPoint
533
534 wxTextPos wxTextCtrl::GetLastPosition() const
535 {
536 HWND hWnd = GetHwnd();
537 long lCharIndex;
538 long lLineLength;
539
540 if (m_bIsMLE)
541 {
542 lCharIndex = 0;
543
544 //
545 // This just gets the total text length. The last will be this value
546 //
547 lLineLength = (long)::WinSendMsg(hWnd, MLM_QUERYTEXTLENGTH, 0, 0);
548 }
549 else
550 {
551 WNDPARAMS vParams;
552
553 lCharIndex = 0;
554 vParams.fsStatus = WPM_CCHTEXT;
555 if (::WinSendMsg( GetHwnd()
556 ,WM_QUERYWINDOWPARAMS
557 ,&vParams
558 ,0
559 ))
560 {
561 lLineLength = (long)vParams.cchText;
562 }
563 else
564 lLineLength = 0;
565 }
566 return(lCharIndex + lLineLength);
567 } // end of wxTextCtrl::GetLastPosition
568
569 // If the return values from and to are the same, there is no
570 // selection.
571 void wxTextCtrl::GetSelection(
572 long* plFrom
573 , long* plTo
574 ) const
575 {
576 WXDWORD dwPos;
577
578 if (m_bIsMLE)
579 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), MLM_QUERYSEL, (MPARAM)MLFQS_MINSEL, 0);
580 else
581 {
582 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), EM_QUERYSEL, 0, 0);
583 }
584 *plFrom = SHORT1FROMMP((MPARAM)dwPos); // the first 16 bit value is the min pos
585 *plTo = SHORT2FROMMP((MPARAM)dwPos); // the first 16 bit value is the min pos
586 } // end of wxTextCtrl::GetSelection
587
588 bool wxTextCtrl::IsEditable() const
589 {
590 if (m_bIsMLE)
591 return((bool)LONGFROMMR(::WinSendMsg(GetHwnd(), MLM_QUERYREADONLY, 0, 0)));
592 else
593 return((bool)LONGFROMMR(::WinSendMsg(GetHwnd(), EM_QUERYREADONLY, 0, 0)));
594 } // end of wxTextCtrl::IsEditable
595
596 // ----------------------------------------------------------------------------
597 // Editing
598 // ----------------------------------------------------------------------------
599
600 void wxTextCtrl::Replace( long lFrom,
601 long lTo,
602 const wxString& rsValue )
603 {
604 #if wxUSE_CLIPBOARD
605 HWND hWnd = GetHwnd();
606
607 //
608 // Set selection and remove it
609 //
610 if (m_bIsMLE)
611 {
612 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
613 ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
614 }
615 else
616 {
617 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
618 ::WinSendMsg(hWnd, EM_CUT, 0, 0);
619 }
620
621 //
622 // Now replace with 'value', by pasting.
623 //
624 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)rsValue, 0, 0);
625
626 // Paste into edit control
627 if (m_bIsMLE)
628 ::WinSendMsg(hWnd, MLM_PASTE, (MPARAM)0, (MPARAM)0);
629 else
630 ::WinSendMsg(hWnd, EM_PASTE, (MPARAM)0, (MPARAM)0);
631 #else
632 wxUnusedVar(lFrom);
633 wxUnusedVar(lTo);
634 wxUnusedVar(rsValue);
635 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
636 #endif
637 } // end of wxTextCtrl::Replace
638
639 void wxTextCtrl::Remove(
640 long lFrom
641 , long lTo
642 )
643 {
644 HWND hWnd = GetHwnd();
645
646 if (m_bIsMLE)
647 {
648 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
649 ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
650 }
651 else
652 {
653 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
654 ::WinSendMsg(hWnd, EM_CUT, 0, 0);
655 }
656 } // end of wxTextCtrl::Remove
657
658 void wxTextCtrl::SetSelection(
659 long lFrom
660 , long lTo
661 )
662 {
663 HWND hWnd = GetHwnd();
664 long lFromChar = lFrom;
665 long lToChar = lTo;
666
667 //
668 // If from and to are both -1, it means (in wxWidgets) that all text should
669 // be selected. Translate into Windows convention
670 //
671 if ((lFrom == -1L) && (lTo == -1L))
672 {
673 lFromChar = 0L;
674 lToChar = -1L;
675 }
676 if (m_bIsMLE)
677 ::WinSendMsg(hWnd, MLM_SETSEL, (MPARAM)lFromChar, (MPARAM)lToChar);
678 else
679 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFromChar, (USHORT)lToChar), (MPARAM)0);
680 } // end of wxTextCtrl::SetSelection
681
682 bool wxTextCtrl::LoadFile(
683 const wxString& rsFile
684 )
685 {
686 if ( wxTextCtrlBase::LoadFile(rsFile) )
687 {
688 //
689 // Update the size limit if needed
690 //
691 AdjustSpaceLimit();
692 return true;
693 }
694 return false;
695 } // end of wxTextCtrl::LoadFile
696
697 bool wxTextCtrl::IsModified() const
698 {
699 bool bRc;
700
701 if (m_bIsMLE)
702 bRc = (bool)LONGFROMMR(::WinSendMsg(GetHwnd(), MLM_QUERYCHANGED, 0, 0));
703 else
704 bRc = (bool)LONGFROMMR(::WinSendMsg(GetHwnd(), EM_QUERYCHANGED, 0, 0));
705 return bRc;
706 } // end of wxTextCtrl::IsModified
707
708 void wxTextCtrl::MarkDirty()
709 {
710 if (m_bIsMLE)
711 ::WinSendMsg(GetHwnd(), MLM_SETCHANGED, MPFROMLONG(TRUE), 0);
712 else
713 // EM controls do not have a SETCHANGED, what can we do??
714 wxFAIL_MSG( _T("not implemented") );
715 }
716
717 //
718 // Makes 'unmodified'
719 //
720 void wxTextCtrl::DiscardEdits()
721 {
722 if (m_bIsMLE)
723 ::WinSendMsg(GetHwnd(), MLM_SETCHANGED, MPFROMLONG(FALSE), 0);
724 else
725 //
726 // EM controls do not have a SETCHANGED but issuing a query should reset it
727 //
728 ::WinSendMsg(GetHwnd(), EM_QUERYCHANGED, 0, 0);
729 } // end of wxTextCtrl::DiscardEdits
730
731 int wxTextCtrl::GetNumberOfLines() const
732 {
733 int nNumLines;
734
735 if (m_bIsMLE)
736 nNumLines = (int)::WinSendMsg(GetHwnd(), MLM_QUERYLINECOUNT, 0, 0);
737 else
738 nNumLines = 1;
739 return nNumLines;
740 } // end of wxTextCtrl::GetNumberOfLines
741
742 long wxTextCtrl::XYToPosition(
743 long lX
744 , long lY
745 ) const
746 {
747 long lCharIndex = 0L;
748 long lLen;
749
750 if (m_bIsMLE)
751 {
752 lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
753 lCharIndex = ((lLen * lY) + lX);
754 }
755 else
756 lCharIndex = lX;
757 return lCharIndex;
758 } // end of wxTextCtrl::XYToPosition
759
760 bool wxTextCtrl::PositionToXY(
761 long lPos
762 , long* plX
763 , long* plY
764 ) const
765 {
766 HWND hWnd = GetHwnd();
767 long nLineNo = -1;
768 long lCharIndex = 0;
769
770 if (m_bIsMLE)
771 nLineNo = (long)::WinSendMsg(hWnd, MLM_LINEFROMCHAR, (MPARAM)lPos, 0);
772 else
773 nLineNo = 0;
774
775 if (nLineNo == -1)
776 {
777 // no such line
778 return false;
779 }
780
781 //
782 // This gets the char index for the _beginning_ of this line
783 //
784 long lLineWidth;
785
786 if (m_bIsMLE)
787 {
788 lLineWidth = (long)::WinSendMsg(hWnd, MLM_QUERYLINELENGTH, (MPARAM)0, (MPARAM)0);
789 lCharIndex = (nLineNo + 1) * lLineWidth;
790 }
791 else
792 {
793 WNDPARAMS vParams;
794
795 vParams.fsStatus = WPM_CCHTEXT;
796 if (::WinSendMsg( hWnd
797 ,WM_QUERYWINDOWPARAMS
798 ,&vParams
799 ,0
800 ))
801 {
802 lCharIndex = vParams.cchText;
803 }
804 else
805 lCharIndex = 32;
806 }
807
808 if (lCharIndex == -1)
809 {
810 return false;
811 }
812
813 //
814 // The X position must therefore be the difference between pos and charIndex
815 //
816 if (plX)
817 *plX = lPos - lCharIndex;
818 if (plY)
819 *plY = nLineNo;
820
821 return true;
822 } // end of wxTextCtrl::PositionToXY
823
824 void wxTextCtrl::ShowPosition( long WXUNUSED(lPos) )
825 {
826 HWND hWnd = GetHwnd();
827 long lCurrentLineLineNo = 0L;
828
829 // To scroll to a position, we pass the number of lines and characters
830 // to scroll *by*. This means that we need to:
831 // (1) Find the line position of the current line.
832 // (2) Find the line position of pos.
833 // (3) Scroll by (pos - current).
834 // For now, ignore the horizontal scrolling.
835
836 //
837 // Is this where scrolling is relative to - the line containing the caret?
838 // Or is the first visible line??? Try first visible line.
839 //
840 if (m_bIsMLE)
841 {
842 //
843 // In PM this is the actual char position
844 //
845 lCurrentLineLineNo = (long)::WinSendMsg(hWnd, MLM_QUERYFIRSTCHAR, (MPARAM)0, (MPARAM)0);
846
847 //
848 // This will cause a scroll to the selected position
849 //
850 ::WinSendMsg(hWnd, MLM_SETSEL, (MPARAM)lCurrentLineLineNo, (MPARAM)lCurrentLineLineNo);
851 }
852 } // end of wxTextCtrl::ShowPosition
853
854 int wxTextCtrl::GetLineLength( long WXUNUSED(lLineNo) ) const
855 {
856 long lLen = 0L;
857
858 if (m_bIsMLE)
859 {
860 lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
861 }
862 else
863 {
864 WNDPARAMS vParams;
865
866 vParams.fsStatus = WPM_CCHTEXT;
867 if (::WinSendMsg( GetHwnd()
868 ,WM_QUERYWINDOWPARAMS
869 ,&vParams
870 ,0
871 ))
872 {
873 lLen = vParams.cchText;
874 }
875 else
876 lLen = 32;
877 }
878 return lLen;
879 } // end ofwxTextCtrl::GetLineLength
880
881 wxString wxTextCtrl::GetLineText(
882 long lLineNo
883 ) const
884 {
885 long lLen = (long)GetLineLength((long)lLineNo) + 1;
886 wxString sStr;
887 wxChar* zBuf;
888
889 //
890 // There must be at least enough place for the length WORD in the
891 // buffer
892 //
893 lLen += sizeof(WORD);
894 zBuf = new wxChar[lLen];
895 if (m_bIsMLE)
896 {
897 long lIndex;
898 long lBuflen;
899 long lCopied;
900
901 lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
902 lIndex = lLen * lLineNo;
903
904 ::WinSendMsg(GetHwnd(), MLM_SETSEL, (MPARAM)lIndex, (MPARAM)lIndex);
905 ::WinSendMsg(GetHwnd(), MLM_SETIMPORTEXPORT, MPFROMP(zBuf), MPFROMSHORT((USHORT)WXSIZEOF(zBuf)));
906 lBuflen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYFORMATTEXTLENGTH, MPFROMLONG(lIndex), MPFROMLONG(-1));
907 lCopied = (long)::WinSendMsg(GetHwnd(), MLM_EXPORT, MPFROMP(&lIndex), MPFROMP(&lBuflen));
908 zBuf[lCopied] = '\0';
909 }
910 else
911 {
912 WNDPARAMS vParams;
913
914 vParams.fsStatus = WPM_CCHTEXT;
915 if (::WinSendMsg( GetHwnd()
916 ,WM_QUERYWINDOWPARAMS
917 ,&vParams
918 ,0
919 ))
920 memcpy((char*)zBuf, vParams.pszText, vParams.cchText);
921 zBuf[vParams.cchText] = '\0';
922 }
923 sStr = zBuf;
924 delete [] zBuf;
925 return sStr;
926 } // end of wxTextCtrl::GetLineText
927
928 // ----------------------------------------------------------------------------
929 // Undo/redo
930 // ----------------------------------------------------------------------------
931
932 void wxTextCtrl::Undo()
933 {
934 if (CanUndo())
935 {
936 if (m_bIsMLE)
937 ::WinSendMsg(GetHwnd(), MLM_UNDO, 0, 0);
938 // Simple entryfields cannot be undone
939 }
940 } // end of wxTextCtrl::Undo
941
942 void wxTextCtrl::Redo()
943 {
944 if (CanRedo())
945 {
946 if (m_bIsMLE)
947 ::WinSendMsg(GetHwnd(), MLM_UNDO, 0, 0);
948 // Simple entryfields cannot be undone
949 }
950 } // end of wxTextCtrl::Redo
951
952 bool wxTextCtrl::CanUndo() const
953 {
954 bool bOk;
955
956 if (m_bIsMLE)
957 bOk = (::WinSendMsg(GetHwnd(), MLM_QUERYUNDO, 0, 0) != 0);
958 else
959 bOk = false; // can't undo regular edit fields in PM
960 return bOk;
961 } // end of wxTextCtrl::CanUndo
962
963 bool wxTextCtrl::CanRedo() const
964 {
965 bool bOk;
966
967 if (m_bIsMLE)
968 bOk = (::WinSendMsg(GetHwnd(), MLM_QUERYUNDO, 0, 0) != 0);
969 else
970 bOk = false; // can't undo regular edit fields in PM
971 return bOk;
972 } // end of wxTextCtrl::CanRedo
973
974 // ----------------------------------------------------------------------------
975 // implemenation details
976 // ----------------------------------------------------------------------------
977
978 void wxTextCtrl::Command(
979 wxCommandEvent& rEvent
980 )
981 {
982 SetValue(rEvent.GetString());
983 ProcessCommand (rEvent);
984 } // end of wxTextCtrl::Command
985
986 void wxTextCtrl::OnDropFiles(
987 wxDropFilesEvent& rEvent
988 )
989 {
990 // By default, load the first file into the text window.
991 if (rEvent.GetNumberOfFiles() > 0)
992 {
993 LoadFile(rEvent.GetFiles()[0]);
994 }
995 } // end of wxTextCtrl::OnDropFiles
996
997 WXHBRUSH wxTextCtrl::OnCtlColor( WXHDC hWxDC,
998 WXHWND WXUNUSED(hWnd),
999 WXUINT WXUNUSED(uCtlColor),
1000 WXUINT WXUNUSED(uMessage),
1001 WXWPARAM WXUNUSED(wParam),
1002 WXLPARAM WXUNUSED(lParam) )
1003 {
1004 HPS hPS = (HPS)hWxDC;
1005 wxColour vColBack = GetBackgroundColour();
1006 wxColour vColFore = GetForegroundColour();
1007 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( vColBack, wxSOLID );
1008
1009 if (m_bUseCtl3D)
1010 {
1011 HBRUSH hBrush = NULLHANDLE;
1012
1013 return hBrush;
1014 }
1015 if (GetParent()->GetTransparentBackground())
1016 ::GpiSetBackMix(hPS, BM_LEAVEALONE);
1017 else
1018 ::GpiSetBackMix(hPS, BM_OVERPAINT);
1019 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1020 vColBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1021 ::GpiSetBackColor(hPS, vColBack.GetPixel());
1022 ::GpiSetColor(hPS, vColFore.GetPixel());
1023 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
1024 } // end of wxTextCtrl::OnCtlColor
1025
1026 bool wxTextCtrl::OS2ShouldPreProcessMessage(
1027 WXMSG* pMsg
1028 )
1029 {
1030 return wxControl::OS2ShouldPreProcessMessage(pMsg);
1031 } // end of wxTextCtrl::OS2ShouldPreProcessMessage
1032
1033 void wxTextCtrl::OnChar(
1034 wxKeyEvent& rEvent
1035 )
1036 {
1037 switch (rEvent.GetKeyCode())
1038 {
1039 case WXK_RETURN:
1040 if ( !(m_windowStyle & wxTE_MULTILINE) )
1041 {
1042 wxCommandEvent vEvent(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1043
1044 vEvent.SetEventObject(this);
1045 if ( GetEventHandler()->ProcessEvent(vEvent))
1046 return;
1047 }
1048 //else: multiline controls need Enter for themselves
1049
1050 break;
1051
1052 case WXK_TAB:
1053 // always produce navigation event - even if we process TAB
1054 // ourselves the fact that we got here means that the user code
1055 // decided to skip processing of this TAB - probably to let it
1056 // do its default job.
1057 //
1058 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
1059 // handled by Windows
1060 {
1061 wxNavigationKeyEvent vEventNav;
1062
1063 vEventNav.SetDirection(!rEvent.ShiftDown());
1064 vEventNav.SetWindowChange(false);
1065 vEventNav.SetEventObject(this);
1066
1067 if ( GetEventHandler()->ProcessEvent(vEventNav) )
1068 return;
1069 }
1070 break;
1071 }
1072 rEvent.Skip();
1073 } // end of wxTextCtrl::OnChar
1074
1075 bool wxTextCtrl::OS2Command(
1076 WXUINT uParam
1077 , WXWORD WXUNUSED(vId)
1078 )
1079 {
1080 switch (uParam)
1081 {
1082 case EN_SETFOCUS:
1083 case EN_KILLFOCUS:
1084 {
1085 wxFocusEvent vEvent( uParam == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1086 : wxEVT_SET_FOCUS
1087 ,m_windowId
1088 );
1089
1090 vEvent.SetEventObject(this);
1091 GetEventHandler()->ProcessEvent(vEvent);
1092 }
1093 break;
1094
1095 case EN_CHANGE:
1096 {
1097 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED
1098 ,m_windowId
1099 );
1100
1101 InitCommandEvent(vEvent);
1102 ProcessCommand(vEvent);
1103 }
1104 break;
1105
1106 case EN_OVERFLOW:
1107 //
1108 // The text size limit has been hit - increase it
1109 //
1110 AdjustSpaceLimit();
1111 break;
1112
1113 case EN_SCROLL:
1114 case EN_INSERTMODETOGGLE:
1115 case EN_MEMERROR:
1116 return false;
1117 default:
1118 return false;
1119 }
1120
1121 //
1122 // Processed
1123 //
1124 return true;
1125 } // end of wxTextCtrl::OS2Command
1126
1127 void wxTextCtrl::AdjustSpaceLimit()
1128 {
1129 unsigned int uLen = 0;
1130 unsigned int uLimit = 0;
1131
1132 uLen = ::WinQueryWindowTextLength(GetHwnd());
1133 if (m_bIsMLE)
1134 {
1135 uLimit = (unsigned int)::WinSendMsg( GetHwnd()
1136 ,MLM_QUERYTEXTLIMIT
1137 ,0
1138 ,0
1139 );
1140 }
1141 else
1142 {
1143 ENTRYFDATA Efd;
1144 WNDPARAMS vParams;
1145
1146 vParams.fsStatus = WPM_CBCTLDATA;
1147 vParams.pCtlData = &Efd;
1148 vParams.cbCtlData = sizeof(ENTRYFDATA);
1149
1150 if (::WinSendMsg( GetHwnd()
1151 ,WM_QUERYWINDOWPARAMS
1152 ,&vParams
1153 ,0
1154 ))
1155 uLimit = (unsigned int)Efd.cchEditLimit;
1156 else
1157 uLimit = 32; //PM's default
1158 }
1159 if (uLen >= uLimit)
1160 {
1161 uLimit = uLen + 0x8000; // 32Kb
1162 if (uLimit > 0xffff)
1163 {
1164 uLimit = 0L;
1165 }
1166 if (m_bIsMLE)
1167 ::WinSendMsg(GetHwnd(), MLM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
1168 else
1169 ::WinSendMsg(GetHwnd(), EM_SETTEXTLIMIT, MPFROMSHORT(uLimit), 0);
1170 }
1171 } // end of wxTextCtrl::AdjustSpaceLimit
1172
1173 bool wxTextCtrl::AcceptsFocus() const
1174 {
1175 //
1176 // We don't want focus if we can't be edited unless we're a multiline
1177 // control because then it might be still nice to get focus from keyboard
1178 // to be able to scroll it without mouse
1179 //
1180 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1181 } // end of wxTextCtrl::Command
1182
1183 wxSize wxTextCtrl::DoGetBestSize() const
1184 {
1185 int nCx;
1186 int nCy;
1187 wxFont vFont = (wxFont)GetFont();
1188
1189 wxGetCharSize(GetHWND(), &nCx, &nCy, &vFont);
1190
1191 int wText = DEFAULT_ITEM_WIDTH;
1192 int hText = (int)(EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * .8);
1193
1194 if (m_windowStyle & wxTE_MULTILINE)
1195 {
1196 hText *= wxMax(GetNumberOfLines(), 5);
1197 }
1198 //else: for single line control everything is ok
1199 return wxSize(wText, hText);
1200 } // end of wxTextCtrl::DoGetBestSize
1201
1202 // ----------------------------------------------------------------------------
1203 // standard handlers for standard edit menu events
1204 // ----------------------------------------------------------------------------
1205
1206 void wxTextCtrl::OnCut( wxCommandEvent& WXUNUSED(rEvent) )
1207 {
1208 Cut();
1209 } // end of wxTextCtrl::OnCut
1210
1211 void wxTextCtrl::OnCopy( wxCommandEvent& WXUNUSED(rEvent) )
1212 {
1213 Copy();
1214 } // end of wxTextCtrl::OnCopy
1215
1216 void wxTextCtrl::OnPaste( wxCommandEvent& WXUNUSED(rEvent) )
1217 {
1218 Paste();
1219 } // end of wxTextCtrl::OnPaste
1220
1221 void wxTextCtrl::OnUndo( wxCommandEvent& WXUNUSED(rEvent) )
1222 {
1223 Undo();
1224 } // end of wxTextCtrl::OnUndo
1225
1226 void wxTextCtrl::OnRedo( wxCommandEvent& WXUNUSED(rEvent) )
1227 {
1228 Redo();
1229 } // end of wxTextCtrl::OnRedo
1230
1231 void wxTextCtrl::OnDelete( wxCommandEvent& WXUNUSED(rEvent) )
1232 {
1233 long lFrom, lTo;
1234
1235 GetSelection( &lFrom, &lTo );
1236
1237 if (lFrom != -1 && lTo != -1)
1238 Remove( lFrom, lTo );
1239 } // end of wxTextCtrl::OnDelete
1240
1241 void wxTextCtrl::OnSelectAll( wxCommandEvent& WXUNUSED(rEvent) )
1242 {
1243 SetSelection(-1, -1);
1244 } // end of wxTextCtrl::OnSelectAll
1245
1246 void wxTextCtrl::OnUpdateCut( wxUpdateUIEvent& rEvent )
1247 {
1248 rEvent.Enable(CanCut());
1249 } // end of wxTextCtrl::OnUpdateCut
1250
1251 void wxTextCtrl::OnUpdateCopy( wxUpdateUIEvent& rEvent )
1252 {
1253 rEvent.Enable(CanCopy());
1254 } // end of wxTextCtrl::OnUpdateCopy
1255
1256 void wxTextCtrl::OnUpdatePaste( wxUpdateUIEvent& rEvent )
1257 {
1258 rEvent.Enable(CanPaste());
1259 } // end of wxTextCtrl::OnUpdatePaste
1260
1261 void wxTextCtrl::OnUpdateUndo( wxUpdateUIEvent& rEvent )
1262 {
1263 rEvent.Enable(CanUndo());
1264 } // end of wxTextCtrl::OnUpdateUndo
1265
1266 void wxTextCtrl::OnUpdateRedo( wxUpdateUIEvent& rEvent )
1267 {
1268 rEvent.Enable(CanRedo());
1269 } // end of wxTextCtrl::OnUpdateRedo
1270
1271 void wxTextCtrl::OnUpdateDelete( wxUpdateUIEvent& rEvent )
1272 {
1273 long lFrom, lTo;
1274
1275 GetSelection( &lFrom, &lTo );
1276 rEvent.Enable( lFrom != -1L && lTo != -1L && lFrom != lTo && IsEditable()) ;
1277 } // end of wxTextCtrl::OnUpdateDelete
1278
1279 void wxTextCtrl::OnUpdateSelectAll( wxUpdateUIEvent& rEvent )
1280 {
1281 rEvent.Enable(GetLastPosition() > 0);
1282 } // end of wxTextCtrl::OnUpdateSelectAll
1283
1284 bool wxTextCtrl::SetBackgroundColour( const wxColour& rColour )
1285 {
1286 if (m_bIsMLE)
1287 ::WinSendMsg(GetHwnd(), MLM_SETBACKCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1288 return true;
1289 } // end of wxTextCtrl::SetBackgroundColour
1290
1291 bool wxTextCtrl::SetForegroundColour( const wxColour& rColour )
1292 {
1293 if (m_bIsMLE)
1294 ::WinSendMsg(GetHwnd(), MLM_SETTEXTCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1295 return true;
1296 } // end of wxTextCtrl::SetForegroundColour
1297
1298 bool wxTextCtrl::SetStyle( long lStart,
1299 long lEnd,
1300 const wxTextAttr& WXUNUSED(rStyle) )
1301 {
1302 HWND hWnd = GetHwnd();
1303
1304 if (lStart > lEnd)
1305 {
1306 long lTmp = lStart;
1307
1308 lStart = lEnd;
1309 lEnd = lTmp;
1310 }
1311
1312 //
1313 // We can only change the format of the selection, so select the range we
1314 // want and restore the old selection later
1315 //
1316 long lStartOld, lEndOld;
1317
1318 GetSelection( &lStartOld, &lEndOld );
1319
1320 //
1321 // But do we really have to change the selection?
1322 //
1323 bool bChangeSel = lStart != lStartOld ||
1324 lEnd != lEndOld;
1325
1326 if (bChangeSel)
1327 {
1328 if (m_bIsMLE)
1329 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1330 else
1331 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1332 }
1333
1334 //
1335 // TODO:: finish this part
1336 //
1337 return true;
1338 } // end of wxTextCtrl::SetStyle