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