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