]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/textctrl.h"
21 #include "wx/settings.h"
29 #include "wx/clipbrd.h"
32 #include "wx/textfile.h"
34 #include "wx/os2/private.h"
38 #include <sys/types.h>
46 #if !USE_SHARED_LIBRARY
48 // ----------------------------------------------------------------------------
49 // event tables and other macros
50 // ----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
54 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
55 EVT_CHAR(wxTextCtrl::OnChar
)
56 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
58 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
59 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
60 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
61 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
62 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
64 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
65 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
66 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
67 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
68 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
71 #endif // USE_SHARED_LIBRARY
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 wxTextCtrl::wxTextCtrl()
85 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
86 const wxString
& value
,
91 const wxValidator
& validator
,
95 // base initialization
96 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
99 // Validator was set in CreateBase
100 //SetValidator(validator);
102 parent
->AddChild(this);
107 // translate wxWin style flags to MSW ones, checking for consistency while
111 long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
112 if ( m_windowStyle & wxTE_MULTILINE )
114 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
115 wxT("wxTE_PROCESS_ENTER style is ignored for multiline "
116 "text controls (they always process it)") );
118 msStyle |= ES_MULTILINE | ES_WANTRETURN;
119 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
120 msStyle |= WS_VSCROLL;
121 m_windowStyle |= wxTE_PROCESS_ENTER;
124 msStyle |= ES_AUTOHSCROLL;
126 if (m_windowStyle & wxHSCROLL)
127 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
129 if (m_windowStyle & wxTE_READONLY)
130 msStyle |= ES_READONLY;
132 if (m_windowStyle & wxHSCROLL)
133 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
134 if (m_windowStyle & wxTE_PASSWORD) // hidden input
135 msStyle |= ES_PASSWORD;
137 // we always want the characters and the arrows
138 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
140 // we may have several different cases:
141 // 1. normal case: both TAB and ENTER are used for dialog navigation
142 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
143 // control in the dialog
144 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
145 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
147 if ( m_windowStyle & wxTE_PROCESS_ENTER )
148 m_lDlgCode |= DLGC_WANTMESSAGE;
149 if ( m_windowStyle & wxTE_PROCESS_TAB )
150 m_lDlgCode |= DLGC_WANTTAB;
152 // do create the control - either an EDIT or RICHEDIT
153 const wxChar *windowClass = wxT("EDIT");
156 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
158 // Even with extended styles, need to combine with WS_BORDER for them to
160 if ( want3D || wxStyleHasBorder(m_windowStyle) )
161 msStyle |= WS_BORDER;
163 // NB: don't use pos and size as CreateWindowEx arguments because they
164 // might be -1 in which case we should use the default values (and
165 // SetSize called below takes care of it)
166 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
176 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") );
178 SubclassWin(GetHWND());
180 // set font, position, size and initial value
181 wxFont
& fontParent
= parent
->GetFont();
182 if ( fontParent
.Ok() )
188 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
191 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
196 // Make sure the window style (etc.) reflects the HWND style (roughly)
197 void wxTextCtrl::AdoptAttributesFromHWND()
199 wxWindow::AdoptAttributesFromHWND();
201 HWND hWnd
= GetHwnd();
204 long style = GetWindowLong(hWnd, GWL_STYLE);
206 if (style & ES_MULTILINE)
207 m_windowStyle |= wxTE_MULTILINE;
208 if (style & ES_PASSWORD)
209 m_windowStyle |= wxTE_PASSWORD;
210 if (style & ES_READONLY)
211 m_windowStyle |= wxTE_READONLY;
212 if (style & ES_WANTRETURN)
213 m_windowStyle |= wxTE_PROCESS_ENTER;
217 void wxTextCtrl::SetupColours()
219 // FIXME why is bg colour not inherited from parent?
220 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
221 SetForegroundColour(GetParent()->GetForegroundColour());
224 // ----------------------------------------------------------------------------
225 // set/get the controls text
226 // ----------------------------------------------------------------------------
228 wxString
wxTextCtrl::GetValue() const
230 return wxGetWindowText(GetHWND());
233 void wxTextCtrl::SetValue(const wxString
& value
)
237 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
239 SetWindowText(GetHwnd(), valueDos);
245 void wxTextCtrl::WriteText(const wxString
& value
)
249 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
251 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
257 void wxTextCtrl::AppendText(const wxString
& text
)
261 SetInsertionPointEnd();
266 void wxTextCtrl::Clear()
268 // SetWindowText(GetHwnd(), wxT(""));
271 // ----------------------------------------------------------------------------
272 // Clipboard operations
273 // ----------------------------------------------------------------------------
275 void wxTextCtrl::Copy()
279 HWND hWnd
= GetHwnd();
280 // SendMessage(hWnd, WM_COPY, 0, 0L);
284 void wxTextCtrl::Cut()
288 HWND hWnd
= GetHwnd();
289 // SendMessage(hWnd, WM_CUT, 0, 0L);
293 void wxTextCtrl::Paste()
297 HWND hWnd
= GetHwnd();
298 // SendMessage(hWnd, WM_PASTE, 0, 0L);
302 bool wxTextCtrl::CanCopy() const
304 // Can copy if there's a selection
306 // GetSelection(& from, & to);
310 bool wxTextCtrl::CanCut() const
312 // Can cut if there's a selection
314 // GetSelection(& from, & to);
318 bool wxTextCtrl::CanPaste() const
323 // Standard edit control: check for straight text on clipboard
324 bool isTextAvailable
= FALSE
;
327 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
329 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
333 return isTextAvailable
;
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 void wxTextCtrl::SetEditable(bool editable
)
342 HWND hWnd
= GetHwnd();
343 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
346 void wxTextCtrl::SetInsertionPoint(long pos
)
350 HWND hWnd = GetHwnd();
352 SendMessage(hWnd, EM_SETSEL, pos, pos);
353 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
355 static const char *nothing = "";
356 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
360 void wxTextCtrl::SetInsertionPointEnd()
364 long pos = GetLastPosition();
365 SetInsertionPoint(pos);
369 long wxTextCtrl::GetInsertionPoint() const
373 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
379 long wxTextCtrl::GetLastPosition() const
381 HWND hWnd
= GetHwnd();
385 // Will always return a number > 0 (according to docs)
386 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
388 // This gets the char index for the _beginning_ of the last line
389 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
391 // Get number of characters in the last line. We'll add this to the character
392 // index for the last line, 1st position.
393 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
395 return (long)(charIndex + lineLength);
400 // If the return values from and to are the same, there is no
402 void wxTextCtrl::GetSelection(long* from
, long* to
) const
404 DWORD dwStart
, dwEnd
;
405 MPARAM wParam
= (MPARAM
) (DWORD
*) & dwStart
; // receives starting position
406 MPARAM lParam
= (MPARAM
) (DWORD
*) & dwEnd
; // receives ending position
408 // ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
414 bool wxTextCtrl::IsEditable() const
418 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
420 return ((style & ES_READONLY) == 0);
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
432 HWND hWnd
= GetHwnd();
433 long fromChar
= from
;
436 // Set selection and remove it
437 // SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
438 // SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
440 // Now replace with 'value', by pasting.
441 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
443 // Paste into edit control
444 // SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
446 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
450 void wxTextCtrl::Remove(long from
, long to
)
452 HWND hWnd
= GetHwnd();
453 long fromChar
= from
;
456 // Cut all selected text
457 // SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
458 // SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
461 void wxTextCtrl::SetSelection(long from
, long to
)
463 HWND hWnd
= GetHwnd();
464 long fromChar
= from
;
467 // if from and to are both -1, it means (in wxWindows) that all text should
468 // be selected. Translate into Windows convention
469 if ((from
== -1) && (to
== -1))
475 // SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
476 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
479 bool wxTextCtrl::LoadFile(const wxString
& file
)
483 if ( wxTextCtrlBase::LoadFile(file) )
485 // update the size limit if needed
494 bool wxTextCtrl::IsModified() const
496 // return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
500 // Makes 'unmodified'
501 void wxTextCtrl::DiscardEdits()
503 // SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
506 int wxTextCtrl::GetNumberOfLines() const
508 // return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
512 long wxTextCtrl::XYToPosition(long x
, long y
) const
514 HWND hWnd
= GetHwnd();
516 // This gets the char index for the _beginning_ of this line
519 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
520 return (long)(x + charIndex);
525 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
527 HWND hWnd
= GetHwnd();
529 // This gets the line number containing the character
531 // lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
539 // This gets the char index for the _beginning_ of this line
540 int charIndex
= 0; // TODO: (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
541 if ( charIndex
== -1 )
546 // The X position must therefore be the different between pos and charIndex
548 *x
= (long)(pos
- charIndex
);
555 void wxTextCtrl::ShowPosition(long pos
)
557 HWND hWnd
= GetHwnd();
559 // To scroll to a position, we pass the number of lines and characters
560 // to scroll *by*. This means that we need to:
561 // (1) Find the line position of the current line.
562 // (2) Find the line position of pos.
563 // (3) Scroll by (pos - current).
564 // For now, ignore the horizontal scrolling.
566 // Is this where scrolling is relative to - the line containing the caret?
567 // Or is the first visible line??? Try first visible line.
568 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
572 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
574 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
576 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
578 if (linesToScroll != 0)
579 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
583 int wxTextCtrl::GetLineLength(long lineNo
) const
588 long charIndex = XYToPosition(0, lineNo);
589 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
595 wxString
wxTextCtrl::GetLineText(long lineNo
) const
597 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
598 char *buf
= (char *)malloc(len
);
600 int noChars
= 0; // TODO:(int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
610 // ----------------------------------------------------------------------------
612 // ----------------------------------------------------------------------------
614 void wxTextCtrl::Undo()
618 // ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
622 void wxTextCtrl::Redo()
626 // Same as Undo, since Undo undoes the undo, i.e. a redo.
627 // ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
631 bool wxTextCtrl::CanUndo() const
633 // return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
637 bool wxTextCtrl::CanRedo() const
639 // return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
643 // ----------------------------------------------------------------------------
644 // implemenation details
645 // ----------------------------------------------------------------------------
647 void wxTextCtrl::Command(wxCommandEvent
& event
)
649 SetValue(event
.GetString());
650 ProcessCommand (event
);
653 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
655 // By default, load the first file into the text window.
656 if (event
.GetNumberOfFiles() > 0)
658 LoadFile(event
.GetFiles()[0]);
662 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
663 WXUINT message
, WXWPARAM wParam
,
669 SetBkMode(hdc, GetParent()->GetTransparentBackground() ? TRANSPARENT
672 ::SetBkColor(hdc, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
673 ::SetTextColor(hdc, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
675 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
677 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
680 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
682 switch ( event
.KeyCode() )
687 if ( !(m_windowStyle & wxTE_MULTILINE) )
689 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
690 event.SetEventObject( this );
691 if ( GetEventHandler()->ProcessEvent(event) )
694 //else: multiline controls need Enter for themselves
699 // always produce navigation event - even if we process TAB
700 // ourselves the fact that we got here means that the user code
701 // decided to skip processing of this TAB - probably to let it
702 // do its default job.
704 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
705 // handled by Windows
707 wxNavigationKeyEvent eventNav;
708 eventNav.SetDirection(!event.ShiftDown());
709 eventNav.SetWindowChange(FALSE);
710 eventNav.SetEventObject(this);
712 if ( GetEventHandler()->ProcessEvent(eventNav) )
722 // don't just call event.Skip() because this will cause TABs and ENTERs
723 // be passed upwards and we don't always want this - instead process it
730 bool wxTextCtrl::OS2Command(WXUINT param
, WXWORD
WXUNUSED(id
))
739 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
742 event.SetEventObject( this );
743 GetEventHandler()->ProcessEvent(event);
749 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
750 wxString val(GetValue());
752 event.m_commandString = WXSTRINGCAST val;
753 event.SetEventObject( this );
754 ProcessCommand(event);
759 // the text size limit has been hit - increase it
763 // the other notification messages are not processed
777 void wxTextCtrl::AdjustSpaceLimit()
781 unsigned int len = ::GetWindowTextLength(GetHwnd()),
782 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
785 limit = len + 0x8000; // 32Kb
787 if ( limit > 0xffff )
788 ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit);
790 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
795 bool wxTextCtrl::AcceptsFocus() const
797 // we don't want focus if we can't be edited
798 return IsEditable() && wxControl::AcceptsFocus();
801 wxSize
wxTextCtrl::DoGetBestSize() const
804 wxGetCharSize(GetHWND(), &cx
, &cy
, (wxFont
*)&GetFont());
806 int wText
= DEFAULT_ITEM_WIDTH
;
808 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
809 if ( m_windowStyle
& wxTE_MULTILINE
)
811 hText
*= wxMin(GetNumberOfLines(), 5);
813 //else: for single line control everything is ok
815 return wxSize(wText
, hText
);
818 // ----------------------------------------------------------------------------
819 // standard handlers for standard edit menu events
820 // ----------------------------------------------------------------------------
822 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
827 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
832 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
837 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
842 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
847 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
849 event
.Enable( CanCut() );
852 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
854 event
.Enable( CanCopy() );
857 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
859 event
.Enable( CanPaste() );
862 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
864 event
.Enable( CanUndo() );
867 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
869 event
.Enable( CanRedo() );