1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/textctrl.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // we don't have any objects of type wxTextCtrlBase in the program, only
43 // wxTextCtrl, so this cast is safe
44 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
46 // ============================================================================
48 // ============================================================================
50 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent
, wxCommandEvent
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN
)
57 IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase
, wxControl
)
59 // ----------------------------------------------------------------------------
60 // style functions - not implemented here
61 // ----------------------------------------------------------------------------
63 wxTextAttr::wxTextAttr(const wxColour
& colText
,
64 const wxColour
& colBack
,
66 wxTextAttrAlignment alignment
)
67 : m_colText(colText
), m_colBack(colBack
), m_font(font
), m_textAlignment(alignment
)
73 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
74 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
75 if (m_font
.Ok()) m_flags
|= wxTEXT_ATTR_FONT
;
76 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
77 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
80 void wxTextAttr::Init()
82 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
90 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
91 const wxTextAttr
& attrDef
,
92 const wxTextCtrlBase
*text
)
94 wxFont font
= attr
.GetFont();
97 font
= attrDef
.GetFont();
99 if ( text
&& !font
.Ok() )
100 font
= text
->GetFont();
103 wxColour colFg
= attr
.GetTextColour();
106 colFg
= attrDef
.GetTextColour();
108 if ( text
&& !colFg
.Ok() )
109 colFg
= text
->GetForegroundColour();
112 wxColour colBg
= attr
.GetBackgroundColour();
115 colBg
= attrDef
.GetBackgroundColour();
117 if ( text
&& !colBg
.Ok() )
118 colBg
= text
->GetBackgroundColour();
121 wxTextAttr
newAttr(colFg
, colBg
, font
);
123 if (attr
.HasAlignment())
124 newAttr
.SetAlignment(attr
.GetAlignment());
125 else if (attrDef
.HasAlignment())
126 newAttr
.SetAlignment(attrDef
.GetAlignment());
129 newAttr
.SetTabs(attr
.GetTabs());
130 else if (attrDef
.HasTabs())
131 newAttr
.SetTabs(attrDef
.GetTabs());
133 if (attr
.HasLeftIndent())
134 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
135 else if (attrDef
.HasLeftIndent())
136 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
138 if (attr
.HasRightIndent())
139 newAttr
.SetRightIndent(attr
.GetRightIndent());
140 else if (attrDef
.HasRightIndent())
141 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
146 void wxTextAttr::operator= (const wxTextAttr
& attr
)
148 m_font
= attr
.m_font
;
149 m_colText
= attr
.m_colText
;
150 m_colBack
= attr
.m_colBack
;
151 m_textAlignment
= attr
.m_textAlignment
;
152 m_leftIndent
= attr
.m_leftIndent
;
153 m_leftSubIndent
= attr
.m_leftSubIndent
;
154 m_rightIndent
= attr
.m_rightIndent
;
155 m_tabs
= attr
.m_tabs
;
156 m_flags
= attr
.m_flags
;
160 // apply styling to text range
161 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
162 const wxTextAttr
& WXUNUSED(style
))
164 // to be implemented in derived TextCtrl classes
168 // get the styling at the given position
169 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
171 // to be implemented in derived TextCtrl classes
175 // change default text attributes
176 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
178 // keep the old attributes if the new style doesn't specify them unless the
179 // new style is empty - then reset m_defaultStyle (as there is no other way
181 if ( style
.IsDefault() )
182 m_defaultStyle
= style
;
184 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
189 // get default text attributes
190 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
192 return m_defaultStyle
;
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
202 wxFFile
file(filename
);
203 if ( file
.IsOpened() )
206 if ( file
.ReadAll(&text
) )
212 m_filename
= filename
;
218 wxLogError(_("File couldn't be loaded."));
219 #endif // wxUSE_FFILE
224 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
226 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
227 if ( filenameToUse
.empty() )
229 // what kind of message to give? is it an error or a program bug?
230 wxLogDebug(wxT("Can't save textctrl to file without filename."));
236 wxFFile
file(filenameToUse
, _T("w"));
237 if ( file
.IsOpened() && file
.Write(GetValue()) )
239 // it's not modified any longer
242 // if it worked, save for future calls
243 m_filename
= filenameToUse
;
247 #endif // wxUSE_FFILE
249 wxLogError(_("The text couldn't be saved."));
254 // ----------------------------------------------------------------------------
255 // stream-like insertion operator
256 // ----------------------------------------------------------------------------
258 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
261 return *TEXTCTRL(this);
264 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
267 str
.Printf(wxT("%.2f"), f
);
269 return *TEXTCTRL(this);
272 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
275 str
.Printf(wxT("%.2f"), d
);
277 return *TEXTCTRL(this);
280 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
283 str
.Printf(wxT("%d"), i
);
285 return *TEXTCTRL(this);
288 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
291 str
.Printf(wxT("%ld"), i
);
293 return *TEXTCTRL(this);
296 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
298 return operator<<(wxString(c
));
301 // ----------------------------------------------------------------------------
302 // streambuf methods implementation
303 // ----------------------------------------------------------------------------
305 #if wxHAS_TEXT_WINDOW_STREAM
307 int wxTextCtrlBase::overflow(int c
)
309 AppendText((wxChar
)c
);
311 // return something different from EOF
315 #endif // wxHAS_TEXT_WINDOW_STREAM
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 bool wxTextCtrlBase::CanCopy() const
323 // can copy if there's a selection
325 GetSelection(&from
, &to
);
329 bool wxTextCtrlBase::CanCut() const
331 // can cut if there's a selection and if we're not read only
332 return CanCopy() && IsEditable();
335 bool wxTextCtrlBase::CanPaste() const
337 // can paste if we are not read only
341 // ----------------------------------------------------------------------------
342 // emulating key presses
343 // ----------------------------------------------------------------------------
346 // the generic version is unused in wxMSW
347 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& WXUNUSED(event
))
352 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
355 int keycode
= event
.GetKeyCode();
368 ch
= (wxChar
)(_T('0') + keycode
- WXK_NUMPAD0
);
372 case WXK_NUMPAD_MULTIPLY
:
382 case WXK_NUMPAD_SUBTRACT
:
387 case WXK_NUMPAD_DECIMAL
:
392 case WXK_NUMPAD_DIVIDE
:
397 case WXK_NUMPAD_DELETE
:
398 // delete the character at cursor
400 const long pos
= GetInsertionPoint();
401 if ( pos
< GetLastPosition() )
402 Remove(pos
, pos
+ 1);
407 // delete the character before the cursor
409 const long pos
= GetInsertionPoint();
411 Remove(pos
- 1, pos
);
417 if ( event
.GetUnicodeKey() )
419 ch
= event
.GetUnicodeKey();
423 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
425 // FIXME this is not going to work for non letters...
426 if ( !event
.ShiftDown() )
428 keycode
= wxTolower(keycode
);
431 ch
= (wxChar
)keycode
;
450 // ----------------------------------------------------------------------------
451 // selection and ranges
452 // ----------------------------------------------------------------------------
454 void wxTextCtrlBase::SelectAll()
456 SetSelection(0, GetLastPosition());
459 wxString
wxTextCtrlBase::GetStringSelection() const
462 GetSelection(&from
, &to
);
464 return GetRange(from
, to
);
467 wxString
wxTextCtrlBase::GetRange(long from
, long to
) const
472 sel
= GetValue().Mid(from
, to
- from
);
478 // do the window-specific processing after processing the update event
479 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
481 // call inherited, but skip the wxControl's version, and call directly the
482 // wxWindow's one instead, because the only reason why we are overriding this
483 // function is that we want to use SetValue() instead of wxControl::SetLabel()
484 wxWindowBase::DoUpdateWindowUI(event
);
487 if ( event
.GetSetText() )
489 if ( event
.GetText() != GetValue() )
490 SetValue(event
.GetText());
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
498 wxTextCtrlHitTestResult
499 wxTextCtrlBase::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
501 // implement in terms of the other overload as the native ports typically
502 // can get the position and not (x, y) pair directly (although wxUniv
503 // directly gets x and y -- and so overrides this method as well)
505 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
507 if ( rc
!= wxTE_HT_UNKNOWN
)
509 PositionToXY(pos
, x
, y
);
515 wxTextCtrlHitTestResult
516 wxTextCtrlBase::HitTest(const wxPoint
& WXUNUSED(pt
),
517 long * WXUNUSED(pos
)) const
520 return wxTE_HT_UNKNOWN
;
523 #else // !wxUSE_TEXTCTRL
525 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
526 // controls (wxComboBox and wxSpinCtrl)
528 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
530 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL