More updates for gdi
[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 bool wxTextCtrl::EmulateKeyPress(
389 const wxKeyEvent& rEvent
390 )
391 {
392 SetFocus();
393 return(wxTextCtrlBase::EmulateKeyPress(rEvent));
394 } // end of wxTextCtrl::EmulateKeyPress
395
396 // ----------------------------------------------------------------------------
397 // Clipboard operations
398 // ----------------------------------------------------------------------------
399
400 void wxTextCtrl::Copy()
401 {
402 if (CanCopy())
403 {
404 HWND hWnd = GetHwnd();
405 if (m_bIsMLE)
406 ::WinSendMsg(hWnd, MLM_COPY, 0, 0);
407 else
408 ::WinSendMsg(hWnd, EM_COPY, 0, 0);
409 }
410 } // end of wxTextCtrl::Copy
411
412 void wxTextCtrl::Cut()
413 {
414 if (CanCut())
415 {
416 HWND hWnd = GetHwnd();
417
418 if (m_bIsMLE)
419 ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
420 else
421 ::WinSendMsg(hWnd, EM_CUT, 0, 0);
422 }
423 } // end of wxTextCtrl::Cut
424
425 void wxTextCtrl::Paste()
426 {
427 if (CanPaste())
428 {
429 HWND hWnd = GetHwnd();
430
431 ::WinSendMsg(hWnd, EM_PASTE, 0, 0);
432 }
433 } // end of wxTextCtrl::Paste
434
435 bool wxTextCtrl::CanCopy() const
436 {
437 //
438 // Can copy if there's a selection
439 //
440 long lFrom = 0L;
441 long lTo = 0L;
442
443 GetSelection(&lFrom, &lTo);
444 return (lFrom != lTo);
445 } // end of wxTextCtrl::CanCopy
446
447 bool wxTextCtrl::CanCut() const
448 {
449 //
450 // Can cut if there's a selection
451 //
452 long lFrom = 0L;
453 long lTo = 0L;
454
455 GetSelection(&lFrom, &lTo);
456 return (lFrom != lTo);
457 } // end of wxTextCtrl::CanCut
458
459 bool wxTextCtrl::CanPaste() const
460 {
461 bool bIsTextAvailable = FALSE;
462
463 if (!IsEditable())
464 return FALSE;
465
466 //
467 // Check for straight text on clipboard
468 //
469 if (::WinOpenClipbrd(vHabmain))
470 {
471 bIsTextAvailable = (::WinQueryClipbrdData(vHabmain, CF_TEXT) != 0);
472 ::WinCloseClipbrd(vHabmain);
473 }
474 return bIsTextAvailable;
475 } // end of wxTextCtrl::CanPaste
476
477 // ----------------------------------------------------------------------------
478 // Accessors
479 // ----------------------------------------------------------------------------
480
481 void wxTextCtrl::SetEditable(
482 bool bEditable
483 )
484 {
485 HWND hWnd = GetHwnd();
486
487 if (m_bIsMLE)
488 ::WinSendMsg(hWnd, MLM_SETREADONLY, MPFROMLONG(!bEditable), (MPARAM)0);
489 else
490 ::WinSendMsg(hWnd, EM_SETREADONLY, MPFROMLONG(!bEditable), (MPARAM)0);
491 } // end of wxTextCtrl::SetEditable
492
493 void wxTextCtrl::SetInsertionPoint(
494 long lPos
495 )
496 {
497 HWND hWnd = GetHwnd();
498
499 if (m_bIsMLE)
500 ::WinSendMsg(hWnd, MLM_SETSEL, (MPARAM)lPos, (MPARAM)lPos);
501 else
502 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lPos, (USHORT)lPos), (MPARAM)0);
503 } // end of wxTextCtrl::SetInsertionPoint
504
505 void wxTextCtrl::SetInsertionPointEnd()
506 {
507 long lPos = GetLastPosition();
508
509 SetInsertionPoint(lPos);
510 } // end of wxTextCtrl::SetInsertionPointEnd
511
512 long wxTextCtrl::GetInsertionPoint() const
513 {
514 WXDWORD dwPos = 0L;
515
516 if (m_bIsMLE)
517 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), MLM_QUERYSEL, (MPARAM)MLFQS_MINSEL, 0);
518 else
519 {
520 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), EM_QUERYSEL, 0, 0);
521 dwPos = SHORT1FROMMP((MPARAM)dwPos); // the first 16 bit value is the min pos
522 }
523 return (dwPos & 0xFFFF);
524 } // end of wxTextCtrl::GetInsertionPoint
525
526 long wxTextCtrl::GetLastPosition() const
527 {
528 HWND hWnd = GetHwnd();
529 long lCharIndex;
530 long lLineLength;
531
532 if (m_bIsMLE)
533 {
534 lCharIndex = 0;
535
536 //
537 // This just gets the total text length. The last will be this value
538 //
539 lLineLength = (long)::WinSendMsg(hWnd, MLM_QUERYTEXTLENGTH, 0, 0);
540 }
541 else
542 {
543 WNDPARAMS vParams;
544
545 lCharIndex = 0;
546 vParams.fsStatus = WPM_CCHTEXT;
547 if (::WinSendMsg( GetHwnd()
548 ,WM_QUERYWINDOWPARAMS
549 ,&vParams
550 ,0
551 ))
552 {
553 lLineLength = (long)vParams.cchText;
554 }
555 else
556 lLineLength = 0;
557 }
558 return(lCharIndex + lLineLength);
559 } // end of wxTextCtrl::GetLastPosition
560
561 // If the return values from and to are the same, there is no
562 // selection.
563 void wxTextCtrl::GetSelection(
564 long* plFrom
565 , long* plTo
566 ) const
567 {
568 WXDWORD dwPos;
569
570 if (m_bIsMLE)
571 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), MLM_QUERYSEL, (MPARAM)MLFQS_MINSEL, 0);
572 else
573 {
574 dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), EM_QUERYSEL, 0, 0);
575 }
576 *plFrom = SHORT1FROMMP((MPARAM)dwPos); // the first 16 bit value is the min pos
577 *plTo = SHORT2FROMMP((MPARAM)dwPos); // the first 16 bit value is the min pos
578 } // end of wxTextCtrl::GetSelection
579
580 bool wxTextCtrl::IsEditable() const
581 {
582 if (m_bIsMLE)
583 return((bool)LONGFROMMR(::WinSendMsg(GetHwnd(), MLM_QUERYREADONLY, 0, 0)));
584 else
585 return((bool)LONGFROMMR(::WinSendMsg(GetHwnd(), EM_QUERYREADONLY, 0, 0)));
586 } // end of wxTextCtrl::IsEditable
587
588 // ----------------------------------------------------------------------------
589 // Editing
590 // ----------------------------------------------------------------------------
591
592 void wxTextCtrl::Replace(
593 long lFrom
594 , long lTo
595 , const wxString& rsValue
596 )
597 {
598 #if wxUSE_CLIPBOARD
599 HWND hWnd = GetHwnd();
600 long lFromChar = lFrom;
601 long lToChar = lTo;
602
603 //
604 // Set selection and remove it
605 //
606 if (m_bIsMLE)
607 {
608 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
609 ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
610 }
611 else
612 {
613 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
614 ::WinSendMsg(hWnd, EM_CUT, 0, 0);
615 }
616
617 //
618 // Now replace with 'value', by pasting.
619 //
620 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)rsValue, 0, 0);
621
622 // Paste into edit control
623 if (m_bIsMLE)
624 ::WinSendMsg(hWnd, MLM_PASTE, (MPARAM)0, (MPARAM)0);
625 else
626 ::WinSendMsg(hWnd, EM_PASTE, (MPARAM)0, (MPARAM)0);
627 #else
628 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
629 #endif
630 } // end of wxTextCtrl::Replace
631
632 void wxTextCtrl::Remove(
633 long lFrom
634 , long lTo
635 )
636 {
637 HWND hWnd = GetHwnd();
638 long lFromChar = lFrom;
639 long lToChar = lTo;
640
641 if (m_bIsMLE)
642 {
643 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
644 ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
645 }
646 else
647 {
648 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
649 ::WinSendMsg(hWnd, EM_CUT, 0, 0);
650 }
651 } // end of wxTextCtrl::Remove
652
653 void wxTextCtrl::SetSelection(
654 long lFrom
655 , long lTo
656 )
657 {
658 HWND hWnd = GetHwnd();
659 long lFromChar = lFrom;
660 long lToChar = lTo;
661
662 //
663 // If from and to are both -1, it means (in wxWindows) that all text should
664 // be selected. Translate into Windows convention
665 //
666 if ((lFrom == -1L) && (lTo == -1L))
667 {
668 lFromChar = 0L;
669 lToChar = -1L;
670 }
671 if (m_bIsMLE)
672 ::WinSendMsg(hWnd, MLM_SETSEL, (MPARAM)lFromChar, (MPARAM)lToChar);
673 else
674 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFromChar, (USHORT)lToChar), (MPARAM)0);
675 } // end of wxTextCtrl::SetSelection
676
677 bool wxTextCtrl::LoadFile(
678 const wxString& rsFile
679 )
680 {
681 if ( wxTextCtrlBase::LoadFile(rsFile) )
682 {
683 //
684 // Update the size limit if needed
685 //
686 AdjustSpaceLimit();
687 return TRUE;
688 }
689 return FALSE;
690 } // end of wxTextCtrl::LoadFile
691
692 bool wxTextCtrl::IsModified() const
693 {
694 bool bRc;
695
696 if (m_bIsMLE)
697 bRc = (bool)LONGFROMMR(::WinSendMsg(GetHwnd(), MLM_QUERYCHANGED, 0, 0));
698 else
699 bRc = (bool)LONGFROMMR(::WinSendMsg(GetHwnd(), EM_QUERYCHANGED, 0, 0));
700 return bRc;
701 } // end of wxTextCtrl::IsModified
702
703 //
704 // Makes 'unmodified'
705 //
706 void wxTextCtrl::DiscardEdits()
707 {
708 if (m_bIsMLE)
709 ::WinSendMsg(GetHwnd(), MLM_SETCHANGED, MPFROMLONG(FALSE), 0);
710 else
711 //
712 // EM controls do not have a SETCHANGED but issuing a query should reset it
713 //
714 ::WinSendMsg(GetHwnd(), EM_QUERYCHANGED, 0, 0);
715 } // end of wxTextCtrl::DiscardEdits
716
717 int wxTextCtrl::GetNumberOfLines() const
718 {
719 int nNumLines;
720
721 if (m_bIsMLE)
722 nNumLines = (int)::WinSendMsg(GetHwnd(), MLM_QUERYLINECOUNT, 0, 0);
723 else
724 nNumLines = 1;
725 return nNumLines;
726 } // end of wxTextCtrl::GetNumberOfLines
727
728 long wxTextCtrl::XYToPosition(
729 long lX
730 , long lY
731 ) const
732 {
733 HWND hWnd = GetHwnd();
734 long lCharIndex = 0L;
735 long lLen;
736
737 if (m_bIsMLE)
738 {
739 lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
740 lCharIndex = ((lLen * lY) + lX);
741 }
742 else
743 lCharIndex = lX;
744 return lCharIndex;
745 } // end of wxTextCtrl::XYToPosition
746
747 bool wxTextCtrl::PositionToXY(
748 long lPos
749 , long* plX
750 , long* plY
751 ) const
752 {
753 HWND hWnd = GetHwnd();
754 long nLineNo = -1;
755 long lCharIndex = 0;
756
757 if (m_bIsMLE)
758 nLineNo = (long)::WinSendMsg(hWnd, MLM_LINEFROMCHAR, (MPARAM)lPos, 0);
759 else
760 nLineNo = 0;
761
762 if (nLineNo == -1)
763 {
764 // no such line
765 return FALSE;
766 }
767
768 //
769 // This gets the char index for the _beginning_ of this line
770 //
771 long lLineWidth;
772
773 if (m_bIsMLE)
774 {
775 lLineWidth = (long)::WinSendMsg(hWnd, MLM_QUERYLINELENGTH, (MPARAM)0, (MPARAM)0);
776 lCharIndex = (nLineNo + 1) * lLineWidth;
777 }
778 else
779 {
780 WNDPARAMS vParams;
781
782 vParams.fsStatus = WPM_CCHTEXT;
783 if (::WinSendMsg( hWnd
784 ,WM_QUERYWINDOWPARAMS
785 ,&vParams
786 ,0
787 ))
788 {
789 lCharIndex = vParams.cchText;
790 }
791 else
792 lCharIndex = 32;
793 }
794
795 if (lCharIndex == -1)
796 {
797 return FALSE;
798 }
799
800 //
801 // The X position must therefore be the difference between pos and charIndex
802 //
803 if (plX)
804 *plX = lPos - lCharIndex;
805 if (plY)
806 *plY = nLineNo;
807
808 return TRUE;
809 } // end of wxTextCtrl::PositionToXY
810
811 void wxTextCtrl::ShowPosition(
812 long lPos
813 )
814 {
815 HWND hWnd = GetHwnd();
816 long lCurrentLineLineNo = 0L;
817
818 // To scroll to a position, we pass the number of lines and characters
819 // to scroll *by*. This means that we need to:
820 // (1) Find the line position of the current line.
821 // (2) Find the line position of pos.
822 // (3) Scroll by (pos - current).
823 // For now, ignore the horizontal scrolling.
824
825 //
826 // Is this where scrolling is relative to - the line containing the caret?
827 // Or is the first visible line??? Try first visible line.
828 //
829 if (m_bIsMLE)
830 {
831 //
832 // In PM this is the actual char position
833 //
834 lCurrentLineLineNo = (long)::WinSendMsg(hWnd, MLM_QUERYFIRSTCHAR, (MPARAM)0, (MPARAM)0);
835
836 //
837 // This will cause a scroll to the selected position
838 //
839 ::WinSendMsg(hWnd, MLM_SETSEL, (MPARAM)lCurrentLineLineNo, (MPARAM)lCurrentLineLineNo);
840 }
841 } // end of wxTextCtrl::ShowPosition
842
843 int wxTextCtrl::GetLineLength(
844 long lLineNo
845 ) const
846 {
847 long lLen = 0L;
848
849 if (m_bIsMLE)
850 lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
851 else
852 {
853 WNDPARAMS vParams;
854
855 vParams.fsStatus = WPM_CCHTEXT;
856 if (::WinSendMsg( GetHwnd()
857 ,WM_QUERYWINDOWPARAMS
858 ,&vParams
859 ,0
860 ))
861 {
862 lLen = vParams.cchText;
863 }
864 else
865 lLen = 32;
866 }
867 return lLen;
868 } // end ofwxTextCtrl::GetLineLength
869
870 wxString wxTextCtrl::GetLineText(
871 long lLineNo
872 ) const
873 {
874 long lLen = (long)GetLineLength((long)lLineNo) + 1;
875 wxString sStr;
876 char* zBuf;
877
878 //
879 // There must be at least enough place for the length WORD in the
880 // buffer
881 //
882 lLen += sizeof(WORD);
883 zBuf = new char[lLen];
884 if (m_bIsMLE)
885 {
886 long lIndex;
887 long lBuflen;
888 long lCopied;
889
890 lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
891 lIndex = lLen * lLineNo;
892
893 ::WinSendMsg(GetHwnd(), MLM_SETSEL, (MPARAM)lIndex, (MPARAM)lIndex);
894 ::WinSendMsg(GetHwnd(), MLM_SETIMPORTEXPORT, MPFROMP(zBuf), MPFROMSHORT((USHORT)sizeof(zBuf)));
895 lBuflen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYFORMATTEXTLENGTH, MPFROMLONG(lIndex), MPFROMLONG(-1));
896 lCopied = (long)::WinSendMsg(GetHwnd(), MLM_EXPORT, MPFROMP(&lIndex), MPFROMP(&lBuflen));
897 zBuf[lCopied] = '\0';
898 }
899 else
900 {
901 WNDPARAMS vParams;
902
903 vParams.fsStatus = WPM_CCHTEXT;
904 if (::WinSendMsg( GetHwnd()
905 ,WM_QUERYWINDOWPARAMS
906 ,&vParams
907 ,0
908 ))
909 memcpy(zBuf, vParams.pszText, vParams.cchText);
910 zBuf[vParams.cchText] = '\0';
911 }
912 sStr = zBuf;
913 delete [] zBuf;
914 return sStr;
915 } // end of wxTextCtrl::GetLineText
916
917 // ----------------------------------------------------------------------------
918 // Undo/redo
919 // ----------------------------------------------------------------------------
920
921 void wxTextCtrl::Undo()
922 {
923 if (CanUndo())
924 {
925 if (m_bIsMLE)
926 ::WinSendMsg(GetHwnd(), MLM_UNDO, 0, 0);
927 // Simple entryfields cannot be undone
928 }
929 } // end of wxTextCtrl::Undo
930
931 void wxTextCtrl::Redo()
932 {
933 if (CanRedo())
934 {
935 if (m_bIsMLE)
936 ::WinSendMsg(GetHwnd(), MLM_UNDO, 0, 0);
937 // Simple entryfields cannot be undone
938 }
939 } // end of wxTextCtrl::Redo
940
941 bool wxTextCtrl::CanUndo() const
942 {
943 bool bOk;
944
945 if (m_bIsMLE)
946 bOk = (::WinSendMsg(GetHwnd(), MLM_QUERYUNDO, 0, 0) != 0);
947 else
948 bOk = FALSE; // can't undo regular edit fields in PM
949 return bOk;
950 } // end of wxTextCtrl::CanUndo
951
952 bool wxTextCtrl::CanRedo() const
953 {
954 bool bOk;
955
956 if (m_bIsMLE)
957 bOk = (::WinSendMsg(GetHwnd(), MLM_QUERYUNDO, 0, 0) != 0);
958 else
959 bOk = FALSE; // can't undo regular edit fields in PM
960 return bOk;
961 } // end of wxTextCtrl::CanRedo
962
963 // ----------------------------------------------------------------------------
964 // implemenation details
965 // ----------------------------------------------------------------------------
966
967 void wxTextCtrl::Command(
968 wxCommandEvent& rEvent
969 )
970 {
971 SetValue(rEvent.GetString());
972 ProcessCommand (rEvent);
973 } // end of wxTextCtrl::Command
974
975 void wxTextCtrl::OnDropFiles(
976 wxDropFilesEvent& rEvent
977 )
978 {
979 // By default, load the first file into the text window.
980 if (rEvent.GetNumberOfFiles() > 0)
981 {
982 LoadFile(rEvent.GetFiles()[0]);
983 }
984 } // end of wxTextCtrl::OnDropFiles
985
986 WXHBRUSH wxTextCtrl::OnCtlColor(
987 WXHDC hWxDC
988 , WXHWND hWnd
989 , WXUINT uCtlColor
990 , WXUINT uMessage
991 , WXWPARAM wParam
992 , WXLPARAM lParam
993 )
994 {
995 HPS hPS = (HPS)hWxDC;
996 wxBrush* pBrush = NULL;
997 wxColour vColBack = GetBackgroundColour();
998 wxColour vColFore = GetForegroundColour();
999 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
1000 ,wxSOLID
1001 );
1002
1003 if (m_bUseCtl3D)
1004 {
1005 HBRUSH hBrush = NULLHANDLE;
1006
1007 return hBrush;
1008 }
1009 if (GetParent()->GetTransparentBackground())
1010 ::GpiSetBackMix(hPS, BM_LEAVEALONE);
1011 else
1012 ::GpiSetBackMix(hPS, BM_OVERPAINT);
1013 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1014 vColBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1015 ::GpiSetBackColor(hPS, vColBack.GetPixel());
1016 ::GpiSetColor(hPS, vColFore.GetPixel());
1017 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
1018 } // end of wxTextCtrl::OnCtlColor
1019
1020 bool wxTextCtrl::OS2ShouldPreProcessMessage(
1021 WXMSG* pMsg
1022 )
1023 {
1024 return wxControl::OS2ShouldPreProcessMessage(pMsg);
1025 } // end of wxTextCtrl::OS2ShouldPreProcessMessage
1026
1027 void wxTextCtrl::OnChar(
1028 wxKeyEvent& rEvent
1029 )
1030 {
1031 switch (rEvent.KeyCode())
1032 {
1033 case WXK_RETURN:
1034 if ( !(m_windowStyle & wxTE_MULTILINE) )
1035 {
1036 wxCommandEvent vEvent(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1037
1038 vEvent.SetEventObject(this);
1039 if ( GetEventHandler()->ProcessEvent(vEvent))
1040 return;
1041 }
1042 //else: multiline controls need Enter for themselves
1043
1044 break;
1045
1046 case WXK_TAB:
1047 // always produce navigation event - even if we process TAB
1048 // ourselves the fact that we got here means that the user code
1049 // decided to skip processing of this TAB - probably to let it
1050 // do its default job.
1051 //
1052 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
1053 // handled by Windows
1054 {
1055 wxNavigationKeyEvent vEventNav;
1056
1057 vEventNav.SetDirection(!rEvent.ShiftDown());
1058 vEventNav.SetWindowChange(FALSE);
1059 vEventNav.SetEventObject(this);
1060
1061 if ( GetEventHandler()->ProcessEvent(vEventNav) )
1062 return;
1063 }
1064 break;
1065 }
1066 rEvent.Skip();
1067 } // end of wxTextCtrl::OnChar
1068
1069 void wxTextCtrl::SetFocus()
1070 {
1071 wxTextCtrlBase::SetFocus();
1072 if ( !HasFlag(wxTE_MULTILINE) )
1073 {
1074 SetSelection(-1, -1);
1075 }
1076 } // end of wxTextCtrl::SetFocus
1077
1078 bool wxTextCtrl::OS2Command(
1079 WXUINT uParam
1080 , WXWORD WXUNUSED(vId)
1081 )
1082 {
1083 switch (uParam)
1084 {
1085 case EN_SETFOCUS:
1086 case EN_KILLFOCUS:
1087 {
1088 wxFocusEvent vEvent( uParam == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1089 : wxEVT_SET_FOCUS
1090 ,m_windowId
1091 );
1092
1093 vEvent.SetEventObject(this);
1094 GetEventHandler()->ProcessEvent(vEvent);
1095 }
1096 break;
1097
1098 case EN_CHANGE:
1099 {
1100 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED
1101 ,m_windowId
1102 );
1103
1104 InitCommandEvent(vEvent);
1105 vEvent.SetString((char*)GetValue().c_str());
1106 ProcessCommand(vEvent);
1107 }
1108 break;
1109
1110 case EN_OVERFLOW:
1111 //
1112 // The text size limit has been hit - increase it
1113 //
1114 AdjustSpaceLimit();
1115 break;
1116
1117 case EN_SCROLL:
1118 case EN_INSERTMODETOGGLE:
1119 case EN_MEMERROR:
1120 return FALSE;
1121 default:
1122 return FALSE;
1123 }
1124
1125 //
1126 // Processed
1127 //
1128 return TRUE;
1129 } // end of wxTextCtrl::OS2Command
1130
1131 void wxTextCtrl::AdjustSpaceLimit()
1132 {
1133 unsigned int uLen = 0;
1134 unsigned int uLimit = 0;
1135
1136 uLen = ::WinQueryWindowTextLength(GetHwnd());
1137 if (m_bIsMLE)
1138 {
1139 uLimit = (unsigned int)::WinSendMsg( GetHwnd()
1140 ,MLM_QUERYTEXTLIMIT
1141 ,0
1142 ,0
1143 );
1144 }
1145 else
1146 {
1147 ENTRYFDATA* pEfd;
1148 WNDPARAMS vParams;
1149
1150 vParams.fsStatus = WPM_CBCTLDATA;
1151 vParams.cbCtlData = sizeof(ENTRYFDATA);
1152
1153 if (::WinSendMsg( GetHwnd()
1154 ,WM_QUERYWINDOWPARAMS
1155 ,&vParams
1156 ,0
1157 ))
1158 {
1159 pEfd = (ENTRYFDATA*)vParams.pCtlData;
1160 uLimit = (unsigned int)pEfd->cchEditLimit;
1161 }
1162 else
1163 uLimit = 32; //PM's default
1164 }
1165 if (uLen >= uLimit)
1166 {
1167 uLimit = uLen + 0x8000; // 32Kb
1168 if (uLimit > 0xffff)
1169 {
1170 uLimit = 0L;
1171 }
1172 if (m_bIsMLE)
1173 ::WinSendMsg(GetHwnd(), MLM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
1174 else
1175 ::WinSendMsg(GetHwnd(), EM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
1176 }
1177 } // end of wxTextCtrl::AdjustSpaceLimit
1178
1179 bool wxTextCtrl::AcceptsFocus() const
1180 {
1181 //
1182 // We don't want focus if we can't be edited
1183 //
1184 return IsEditable() && wxControl::AcceptsFocus();
1185 } // end of wxTextCtrl::Command
1186
1187 wxSize wxTextCtrl::DoGetBestSize() const
1188 {
1189 int nCx;
1190 int nCy;
1191
1192 wxGetCharSize(GetHWND(), &nCx, &nCy, (wxFont*)&GetFont());
1193
1194 int wText = DEFAULT_ITEM_WIDTH;
1195 int hText = (EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * .8);
1196
1197 if (m_windowStyle & wxTE_MULTILINE)
1198 {
1199 hText *= wxMax(GetNumberOfLines(), 5);
1200 }
1201 //else: for single line control everything is ok
1202 return wxSize(wText, hText);
1203 } // end of wxTextCtrl::DoGetBestSize
1204
1205 // ----------------------------------------------------------------------------
1206 // standard handlers for standard edit menu events
1207 // ----------------------------------------------------------------------------
1208
1209 void wxTextCtrl::OnCut(
1210 wxCommandEvent& rEvent
1211 )
1212 {
1213 Cut();
1214 } // end of wxTextCtrl::OnCut
1215
1216 void wxTextCtrl::OnCopy(
1217 wxCommandEvent& rEvent
1218 )
1219 {
1220 Copy();
1221 } // end of wxTextCtrl::OnCopy
1222
1223 void wxTextCtrl::OnPaste(
1224 wxCommandEvent& rEvent
1225 )
1226 {
1227 Paste();
1228 } // end of wxTextCtrl::OnPaste
1229
1230 void wxTextCtrl::OnUndo(
1231 wxCommandEvent& rEvent
1232 )
1233 {
1234 Undo();
1235 } // end of wxTextCtrl::OnUndo
1236
1237 void wxTextCtrl::OnRedo(
1238 wxCommandEvent& rEvent
1239 )
1240 {
1241 Redo();
1242 } // end of wxTextCtrl::OnRedo
1243
1244 void wxTextCtrl::OnUpdateCut(
1245 wxUpdateUIEvent& rEvent
1246 )
1247 {
1248 rEvent.Enable(CanCut());
1249 } // end of wxTextCtrl::OnUpdateCut
1250
1251 void wxTextCtrl::OnUpdateCopy(
1252 wxUpdateUIEvent& rEvent
1253 )
1254 {
1255 rEvent.Enable(CanCopy());
1256 } // end of wxTextCtrl::OnUpdateCopy
1257
1258 void wxTextCtrl::OnUpdatePaste(
1259 wxUpdateUIEvent& rEvent
1260 )
1261 {
1262 rEvent.Enable(CanPaste());
1263 } // end of wxTextCtrl::OnUpdatePaste
1264
1265 void wxTextCtrl::OnUpdateUndo(
1266 wxUpdateUIEvent& rEvent
1267 )
1268 {
1269 rEvent.Enable(CanUndo());
1270 } // end of wxTextCtrl::OnUpdateUndo
1271
1272 void wxTextCtrl::OnUpdateRedo(
1273 wxUpdateUIEvent& rEvent
1274 )
1275 {
1276 rEvent.Enable(CanRedo());
1277 } // end of wxTextCtrl::OnUpdateRedo
1278
1279 bool wxTextCtrl::SetBackgroundColour(
1280 const wxColour& rColour
1281 )
1282 {
1283 if (m_bIsMLE)
1284 ::WinSendMsg(GetHwnd(), MLM_SETBACKCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1285 return TRUE;
1286 } // end of wxTextCtrl::SetBackgroundColour
1287
1288 bool wxTextCtrl::SetForegroundColour(
1289 const wxColour& rColour
1290 )
1291 {
1292 if (m_bIsMLE)
1293 ::WinSendMsg(GetHwnd(), MLM_SETTEXTCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1294 return TRUE;
1295 } // end of wxTextCtrl::SetForegroundColour
1296
1297 bool wxTextCtrl::SetStyle(
1298 long lStart
1299 , long lEnd
1300 , const wxTextAttr& rStyle
1301 )
1302 {
1303 HWND hWnd = GetHwnd();
1304
1305 if (lStart > lEnd)
1306 {
1307 long lTmp = lStart;
1308
1309 lStart = lEnd;
1310 lEnd = lTmp;
1311 }
1312
1313 //
1314 // We can only change the format of the selection, so select the range we
1315 // want and restore the old selection later
1316 //
1317 long lStartOld;
1318 long lEndOld;
1319
1320 GetSelection( &lStartOld
1321 ,&lEndOld
1322 );
1323
1324 //
1325 // But do we really have to change the selection?
1326 //
1327 bool bChangeSel = lStart != lStartOld ||
1328 lEnd != lEndOld;
1329
1330 if (bChangeSel)
1331 {
1332 if (m_bIsMLE)
1333 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1334 else
1335 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1336 }
1337
1338 //
1339 // TODO:: finish this part
1340 //
1341 return TRUE;
1342 } // end of wxTextCtrl::SetStyle
1343