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