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