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