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 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 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 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 bool wxTextCtrlBase::DoLoadFile(const wxString
& filename
, int WXUNUSED(fileType
))
196 wxFFile
file(filename
);
197 if ( file
.IsOpened() )
200 if ( file
.ReadAll(&text
) )
206 m_filename
= filename
;
212 wxLogError(_("File couldn't be loaded."));
213 #endif // wxUSE_FFILE
218 bool wxTextAreaBase::SaveFile(const wxString
& filename
, int fileType
)
220 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
221 if ( filenameToUse
.empty() )
223 // what kind of message to give? is it an error or a program bug?
224 wxLogDebug(wxT("Can't save textctrl to file without filename."));
229 return DoSaveFile(filenameToUse
, fileType
);
232 bool wxTextCtrlBase::DoSaveFile(const wxString
& filename
, int WXUNUSED(fileType
))
235 wxFFile
file(filename
, _T("w"));
236 if ( file
.IsOpened() && file
.Write(GetValue()) )
238 // if it worked, save for future calls
239 m_filename
= filename
;
241 // it's not modified any longer
246 #endif // wxUSE_FFILE
248 wxLogError(_("The text couldn't be saved."));
253 // ----------------------------------------------------------------------------
254 // stream-like insertion operator
255 // ----------------------------------------------------------------------------
257 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
260 return *TEXTCTRL(this);
263 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
266 str
.Printf(wxT("%.2f"), f
);
268 return *TEXTCTRL(this);
271 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
274 str
.Printf(wxT("%.2f"), d
);
276 return *TEXTCTRL(this);
279 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
282 str
.Printf(wxT("%d"), i
);
284 return *TEXTCTRL(this);
287 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
290 str
.Printf(wxT("%ld"), i
);
292 return *TEXTCTRL(this);
295 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
297 return operator<<(wxString(c
));
300 // ----------------------------------------------------------------------------
301 // streambuf methods implementation
302 // ----------------------------------------------------------------------------
304 #if wxHAS_TEXT_WINDOW_STREAM
306 int wxTextCtrlBase::overflow(int c
)
308 AppendText((wxChar
)c
);
310 // return something different from EOF
314 #endif // wxHAS_TEXT_WINDOW_STREAM
316 // ----------------------------------------------------------------------------
317 // emulating key presses
318 // ----------------------------------------------------------------------------
320 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
322 // we have a native implementation for Win32 and so don't need this one
325 int keycode
= event
.GetKeyCode();
338 ch
= (wxChar
)(_T('0') + keycode
- WXK_NUMPAD0
);
342 case WXK_NUMPAD_MULTIPLY
:
352 case WXK_NUMPAD_SUBTRACT
:
357 case WXK_NUMPAD_DECIMAL
:
362 case WXK_NUMPAD_DIVIDE
:
367 case WXK_NUMPAD_DELETE
:
368 // delete the character at cursor
370 const long pos
= GetInsertionPoint();
371 if ( pos
< GetLastPosition() )
372 Remove(pos
, pos
+ 1);
377 // delete the character before the cursor
379 const long pos
= GetInsertionPoint();
381 Remove(pos
- 1, pos
);
387 if ( event
.GetUnicodeKey() )
389 ch
= event
.GetUnicodeKey();
393 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
395 // FIXME this is not going to work for non letters...
396 if ( !event
.ShiftDown() )
398 keycode
= wxTolower(keycode
);
401 ch
= (wxChar
)keycode
;
417 #endif // !__WIN32__/__WIN32__
422 // do the window-specific processing after processing the update event
423 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
425 // call inherited, but skip the wxControl's version, and call directly the
426 // wxWindow's one instead, because the only reason why we are overriding this
427 // function is that we want to use SetValue() instead of wxControl::SetLabel()
428 wxWindowBase::DoUpdateWindowUI(event
);
431 if ( event
.GetSetText() )
433 if ( event
.GetText() != GetValue() )
434 SetValue(event
.GetText());
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 wxTextCtrlHitTestResult
443 wxTextAreaBase::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
445 // implement in terms of the other overload as the native ports typically
446 // can get the position and not (x, y) pair directly (although wxUniv
447 // directly gets x and y -- and so overrides this method as well)
449 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
451 if ( rc
!= wxTE_HT_UNKNOWN
)
453 PositionToXY(pos
, x
, y
);
459 wxTextCtrlHitTestResult
460 wxTextAreaBase::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(pos
)) const
463 return wxTE_HT_UNKNOWN
;
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
471 bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow
*win
)
473 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId());
475 // do not do this as it could be very inefficient if the text control
476 // contains a lot of text and we're not using ref-counted wxString
477 // implementation -- instead, event.GetString() will query the control for
478 // its current text if needed
479 //event.SetString(win->GetValue());
481 event
.SetEventObject(win
);
482 return win
->GetEventHandler()->ProcessEvent(event
);
485 #else // !wxUSE_TEXTCTRL
487 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
488 // controls (wxComboBox and wxSpinCtrl)
490 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
492 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL