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