]> git.saurik.com Git - wxWidgets.git/blob - src/os2/textctrl.cpp
fixed make uninstall
[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 bool wxTextCtrl::OS2Command(
1070 WXUINT uParam
1071 , WXWORD WXUNUSED(vId)
1072 )
1073 {
1074 switch (uParam)
1075 {
1076 case EN_SETFOCUS:
1077 case EN_KILLFOCUS:
1078 {
1079 wxFocusEvent vEvent( uParam == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1080 : wxEVT_SET_FOCUS
1081 ,m_windowId
1082 );
1083
1084 vEvent.SetEventObject(this);
1085 GetEventHandler()->ProcessEvent(vEvent);
1086 }
1087 break;
1088
1089 case EN_CHANGE:
1090 {
1091 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED
1092 ,m_windowId
1093 );
1094
1095 InitCommandEvent(vEvent);
1096 vEvent.SetString((char*)GetValue().c_str());
1097 ProcessCommand(vEvent);
1098 }
1099 break;
1100
1101 case EN_OVERFLOW:
1102 //
1103 // The text size limit has been hit - increase it
1104 //
1105 AdjustSpaceLimit();
1106 break;
1107
1108 case EN_SCROLL:
1109 case EN_INSERTMODETOGGLE:
1110 case EN_MEMERROR:
1111 return FALSE;
1112 default:
1113 return FALSE;
1114 }
1115
1116 //
1117 // Processed
1118 //
1119 return TRUE;
1120 } // end of wxTextCtrl::OS2Command
1121
1122 void wxTextCtrl::AdjustSpaceLimit()
1123 {
1124 unsigned int uLen = 0;
1125 unsigned int uLimit = 0;
1126
1127 uLen = ::WinQueryWindowTextLength(GetHwnd());
1128 if (m_bIsMLE)
1129 {
1130 uLimit = (unsigned int)::WinSendMsg( GetHwnd()
1131 ,MLM_QUERYTEXTLIMIT
1132 ,0
1133 ,0
1134 );
1135 }
1136 else
1137 {
1138 ENTRYFDATA* pEfd;
1139 WNDPARAMS vParams;
1140
1141 vParams.fsStatus = WPM_CBCTLDATA;
1142 vParams.cbCtlData = sizeof(ENTRYFDATA);
1143
1144 if (::WinSendMsg( GetHwnd()
1145 ,WM_QUERYWINDOWPARAMS
1146 ,&vParams
1147 ,0
1148 ))
1149 {
1150 pEfd = (ENTRYFDATA*)vParams.pCtlData;
1151 uLimit = (unsigned int)pEfd->cchEditLimit;
1152 }
1153 else
1154 uLimit = 32; //PM's default
1155 }
1156 if (uLen >= uLimit)
1157 {
1158 uLimit = uLen + 0x8000; // 32Kb
1159 if (uLimit > 0xffff)
1160 {
1161 uLimit = 0L;
1162 }
1163 if (m_bIsMLE)
1164 ::WinSendMsg(GetHwnd(), MLM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
1165 else
1166 ::WinSendMsg(GetHwnd(), EM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
1167 }
1168 } // end of wxTextCtrl::AdjustSpaceLimit
1169
1170 bool wxTextCtrl::AcceptsFocus() const
1171 {
1172 //
1173 // We don't want focus if we can't be edited
1174 //
1175 return IsEditable() && wxControl::AcceptsFocus();
1176 } // end of wxTextCtrl::Command
1177
1178 wxSize wxTextCtrl::DoGetBestSize() const
1179 {
1180 int nCx;
1181 int nCy;
1182
1183 wxGetCharSize(GetHWND(), &nCx, &nCy, (wxFont*)&GetFont());
1184
1185 int wText = DEFAULT_ITEM_WIDTH;
1186 int hText = (EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * .8);
1187
1188 if (m_windowStyle & wxTE_MULTILINE)
1189 {
1190 hText *= wxMax(GetNumberOfLines(), 5);
1191 }
1192 //else: for single line control everything is ok
1193 return wxSize(wText, hText);
1194 } // end of wxTextCtrl::DoGetBestSize
1195
1196 // ----------------------------------------------------------------------------
1197 // standard handlers for standard edit menu events
1198 // ----------------------------------------------------------------------------
1199
1200 void wxTextCtrl::OnCut(
1201 wxCommandEvent& rEvent
1202 )
1203 {
1204 Cut();
1205 } // end of wxTextCtrl::OnCut
1206
1207 void wxTextCtrl::OnCopy(
1208 wxCommandEvent& rEvent
1209 )
1210 {
1211 Copy();
1212 } // end of wxTextCtrl::OnCopy
1213
1214 void wxTextCtrl::OnPaste(
1215 wxCommandEvent& rEvent
1216 )
1217 {
1218 Paste();
1219 } // end of wxTextCtrl::OnPaste
1220
1221 void wxTextCtrl::OnUndo(
1222 wxCommandEvent& rEvent
1223 )
1224 {
1225 Undo();
1226 } // end of wxTextCtrl::OnUndo
1227
1228 void wxTextCtrl::OnRedo(
1229 wxCommandEvent& rEvent
1230 )
1231 {
1232 Redo();
1233 } // end of wxTextCtrl::OnRedo
1234
1235 void wxTextCtrl::OnUpdateCut(
1236 wxUpdateUIEvent& rEvent
1237 )
1238 {
1239 rEvent.Enable(CanCut());
1240 } // end of wxTextCtrl::OnUpdateCut
1241
1242 void wxTextCtrl::OnUpdateCopy(
1243 wxUpdateUIEvent& rEvent
1244 )
1245 {
1246 rEvent.Enable(CanCopy());
1247 } // end of wxTextCtrl::OnUpdateCopy
1248
1249 void wxTextCtrl::OnUpdatePaste(
1250 wxUpdateUIEvent& rEvent
1251 )
1252 {
1253 rEvent.Enable(CanPaste());
1254 } // end of wxTextCtrl::OnUpdatePaste
1255
1256 void wxTextCtrl::OnUpdateUndo(
1257 wxUpdateUIEvent& rEvent
1258 )
1259 {
1260 rEvent.Enable(CanUndo());
1261 } // end of wxTextCtrl::OnUpdateUndo
1262
1263 void wxTextCtrl::OnUpdateRedo(
1264 wxUpdateUIEvent& rEvent
1265 )
1266 {
1267 rEvent.Enable(CanRedo());
1268 } // end of wxTextCtrl::OnUpdateRedo
1269
1270 bool wxTextCtrl::SetBackgroundColour(
1271 const wxColour& rColour
1272 )
1273 {
1274 if (m_bIsMLE)
1275 ::WinSendMsg(GetHwnd(), MLM_SETBACKCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1276 return TRUE;
1277 } // end of wxTextCtrl::SetBackgroundColour
1278
1279 bool wxTextCtrl::SetForegroundColour(
1280 const wxColour& rColour
1281 )
1282 {
1283 if (m_bIsMLE)
1284 ::WinSendMsg(GetHwnd(), MLM_SETTEXTCOLOR, (MPARAM)rColour.GetPixel(), MLE_INDEX);
1285 return TRUE;
1286 } // end of wxTextCtrl::SetForegroundColour
1287
1288 bool wxTextCtrl::SetStyle(
1289 long lStart
1290 , long lEnd
1291 , const wxTextAttr& rStyle
1292 )
1293 {
1294 HWND hWnd = GetHwnd();
1295
1296 if (lStart > lEnd)
1297 {
1298 long lTmp = lStart;
1299
1300 lStart = lEnd;
1301 lEnd = lTmp;
1302 }
1303
1304 //
1305 // We can only change the format of the selection, so select the range we
1306 // want and restore the old selection later
1307 //
1308 long lStartOld;
1309 long lEndOld;
1310
1311 GetSelection( &lStartOld
1312 ,&lEndOld
1313 );
1314
1315 //
1316 // But do we really have to change the selection?
1317 //
1318 bool bChangeSel = lStart != lStartOld ||
1319 lEnd != lEndOld;
1320
1321 if (bChangeSel)
1322 {
1323 if (m_bIsMLE)
1324 ::WinSendMsg(hWnd, MLM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1325 else
1326 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lStart, (USHORT)lEnd), 0);
1327 }
1328
1329 //
1330 // TODO:: finish this part
1331 //
1332 return TRUE;
1333 } // end of wxTextCtrl::SetStyle
1334