]> git.saurik.com Git - wxWidgets.git/blob - src/os2/textctrl.cpp
use correct colour for the toolbar initially (patch 1150657)
[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.empty())
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 wxTextPos 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 wxTextPos 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 ProcessCommand(vEvent);
1107 }
1108 break;
1109
1110 case EN_OVERFLOW:
1111 //
1112 // The text size limit has been hit - increase it
1113 //
1114 AdjustSpaceLimit();
1115 break;
1116
1117 case EN_SCROLL:
1118 case EN_INSERTMODETOGGLE:
1119 case EN_MEMERROR:
1120 return false;
1121 default:
1122 return false;
1123 }
1124
1125 //
1126 // Processed
1127 //
1128 return true;
1129 } // end of wxTextCtrl::OS2Command
1130
1131 void wxTextCtrl::AdjustSpaceLimit()
1132 {
1133 unsigned int uLen = 0;
1134 unsigned int uLimit = 0;
1135
1136 uLen = ::WinQueryWindowTextLength(GetHwnd());
1137 if (m_bIsMLE)
1138 {
1139 uLimit = (unsigned int)::WinSendMsg( GetHwnd()
1140 ,MLM_QUERYTEXTLIMIT
1141 ,0
1142 ,0
1143 );
1144 }
1145 else
1146 {
1147 ENTRYFDATA Efd;
1148 WNDPARAMS vParams;
1149
1150 vParams.fsStatus = WPM_CBCTLDATA;
1151 vParams.pCtlData = &Efd;
1152 vParams.cbCtlData = sizeof(ENTRYFDATA);
1153
1154 if (::WinSendMsg( GetHwnd()
1155 ,WM_QUERYWINDOWPARAMS
1156 ,&vParams
1157 ,0
1158 ))
1159 uLimit = (unsigned int)Efd.cchEditLimit;
1160 else
1161 uLimit = 32; //PM's default
1162 }
1163 if (uLen >= uLimit)
1164 {
1165 uLimit = uLen + 0x8000; // 32Kb
1166 if (uLimit > 0xffff)
1167 {
1168 uLimit = 0L;
1169 }
1170 if (m_bIsMLE)
1171 ::WinSendMsg(GetHwnd(), MLM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
1172 else
1173 ::WinSendMsg(GetHwnd(), EM_SETTEXTLIMIT, MPFROMSHORT(uLimit), 0);
1174 }
1175 } // end of wxTextCtrl::AdjustSpaceLimit
1176
1177 bool wxTextCtrl::AcceptsFocus() const
1178 {
1179 //
1180 // We don't want focus if we can't be edited unless we're a multiline
1181 // control because then it might be still nice to get focus from keyboard
1182 // to be able to scroll it without mouse
1183 //
1184 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1185 } // end of wxTextCtrl::Command
1186
1187 wxSize wxTextCtrl::DoGetBestSize() const
1188 {
1189 int nCx;
1190 int nCy;
1191
1192 wxGetCharSize(GetHWND(), &nCx, &nCy, (wxFont*)&GetFont());
1193
1194 int wText = DEFAULT_ITEM_WIDTH;
1195 int hText = (int)(EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * .8);
1196
1197 if (m_windowStyle & wxTE_MULTILINE)
1198 {
1199 hText *= wxMax(GetNumberOfLines(), 5);
1200 }
1201 //else: for single line control everything is ok
1202 return wxSize(wText, hText);
1203 } // end of wxTextCtrl::DoGetBestSize
1204
1205 // ----------------------------------------------------------------------------
1206 // standard handlers for standard edit menu events
1207 // ----------------------------------------------------------------------------
1208
1209 void wxTextCtrl::OnCut(
1210 wxCommandEvent& rEvent
1211 )
1212 {
1213 Cut();
1214 } // end of wxTextCtrl::OnCut
1215
1216 void wxTextCtrl::OnCopy(
1217 wxCommandEvent& rEvent
1218 )
1219 {
1220 Copy();
1221 } // end of wxTextCtrl::OnCopy
1222
1223 void wxTextCtrl::OnPaste(
1224 wxCommandEvent& rEvent
1225 )
1226 {
1227 Paste();
1228 } // end of wxTextCtrl::OnPaste
1229
1230 void wxTextCtrl::OnUndo(
1231 wxCommandEvent& rEvent
1232 )
1233 {
1234 Undo();
1235 } // end of wxTextCtrl::OnUndo
1236
1237 void wxTextCtrl::OnRedo(
1238 wxCommandEvent& rEvent
1239 )
1240 {
1241 Redo();
1242 } // end of wxTextCtrl::OnRedo
1243
1244 void wxTextCtrl::OnDelete(
1245 wxCommandEvent& rEvent
1246 )
1247 {
1248 long lFrom;
1249 long lTo;
1250
1251 GetSelection( &lFrom
1252 ,&lTo
1253 );
1254 if (lFrom != -1 && lTo != -1)
1255 Remove( lFrom
1256 ,lTo
1257 );
1258 } // end of wxTextCtrl::OnDelete
1259
1260 void wxTextCtrl::OnSelectAll(
1261 wxCommandEvent& rEvent
1262 )
1263 {
1264 SetSelection(-1, -1);
1265 } // end of wxTextCtrl::OnSelectAll
1266
1267 void wxTextCtrl::OnUpdateCut(
1268 wxUpdateUIEvent& rEvent
1269 )
1270 {
1271 rEvent.Enable(CanCut());
1272 } // end of wxTextCtrl::OnUpdateCut
1273
1274 void wxTextCtrl::OnUpdateCopy(
1275 wxUpdateUIEvent& rEvent
1276 )
1277 {
1278 rEvent.Enable(CanCopy());
1279 } // end of wxTextCtrl::OnUpdateCopy
1280
1281 void wxTextCtrl::OnUpdatePaste(
1282 wxUpdateUIEvent& rEvent
1283 )
1284 {
1285 rEvent.Enable(CanPaste());
1286 } // end of wxTextCtrl::OnUpdatePaste
1287
1288 void wxTextCtrl::OnUpdateUndo(
1289 wxUpdateUIEvent& rEvent
1290 )
1291 {
1292 rEvent.Enable(CanUndo());
1293 } // end of wxTextCtrl::OnUpdateUndo
1294
1295 void wxTextCtrl::OnUpdateRedo(
1296 wxUpdateUIEvent& rEvent
1297 )
1298 {
1299 rEvent.Enable(CanRedo());
1300 } // end of wxTextCtrl::OnUpdateRedo
1301
1302 void wxTextCtrl::OnUpdateDelete(
1303 wxUpdateUIEvent& rEvent
1304 )
1305 {
1306 long lFrom;
1307 long lTo;
1308
1309 GetSelection( &lFrom
1310 ,&lTo
1311 );
1312 rEvent.Enable( lFrom != -1L && lTo != -1L && lFrom != lTo && IsEditable()) ;
1313 } // end of wxTextCtrl::OnUpdateDelete
1314
1315 void wxTextCtrl::OnUpdateSelectAll(
1316 wxUpdateUIEvent& rEvent
1317 )
1318 {
1319 rEvent.Enable(GetLastPosition() > 0);
1320 } // end of wxTextCtrl::OnUpdateSelectAll
1321
1322 bool wxTextCtrl::SetBackgroundColour(
1323 const wxColour& rColour
1324 )
1325 {
1326 if (m_bIsMLE)
1327 ::WinSendMsg(GetHwnd(), MLM_SETBACKCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1328 return true;
1329 } // end of wxTextCtrl::SetBackgroundColour
1330
1331 bool wxTextCtrl::SetForegroundColour(
1332 const wxColour& rColour
1333 )
1334 {
1335 if (m_bIsMLE)
1336 ::WinSendMsg(GetHwnd(), MLM_SETTEXTCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1337 return true;
1338 } // end of wxTextCtrl::SetForegroundColour
1339
1340 bool wxTextCtrl::SetStyle(
1341 long lStart
1342 , long lEnd
1343 , const wxTextAttr& rStyle
1344 )
1345 {
1346 HWND hWnd = GetHwnd();
1347
1348 if (lStart > lEnd)
1349 {
1350 long lTmp = lStart;
1351
1352 lStart = lEnd;
1353 lEnd = lTmp;
1354 }
1355
1356 //
1357 // We can only change the format of the selection, so select the range we
1358 // want and restore the old selection later
1359 //
1360 long lStartOld;
1361 long lEndOld;
1362
1363 GetSelection( &lStartOld
1364 ,&lEndOld
1365 );
1366
1367 //
1368 // But do we really have to change the selection?
1369 //
1370 bool bChangeSel = lStart != lStartOld ||
1371 lEnd != lEndOld;
1372
1373 if (bChangeSel)
1374 {
1375 if (m_bIsMLE)
1376 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1377 else
1378 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1379 }
1380
1381 //
1382 // TODO:: finish this part
1383 //
1384 return true;
1385 } // end of wxTextCtrl::SetStyle
1386