1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "textctrlbase.h"
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/textctrl.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // we don't have any objects of type wxTextCtrlBase in the program, only
42 // wxTextCtrl, so this cast is safe
43 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
45 // ============================================================================
47 // ============================================================================
49 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent
, wxCommandEvent
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxTextCtrlBase::wxTextCtrlBase()
64 wxTextCtrlBase::~wxTextCtrlBase()
68 // ----------------------------------------------------------------------------
69 // style functions - not implemented here
70 // ----------------------------------------------------------------------------
72 wxTextAttr::wxTextAttr(const wxColour
& colText
,
73 const wxColour
& colBack
,
75 wxTextAttrAlignment alignment
)
76 : m_colText(colText
), m_colBack(colBack
), m_font(font
), m_textAlignment(alignment
)
81 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
82 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
83 if (m_font
.Ok()) m_flags
|= wxTEXT_ATTR_FONT
;
84 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
85 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
88 void wxTextAttr::Init()
90 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
97 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
98 const wxTextAttr
& attrDef
,
99 const wxTextCtrlBase
*text
)
101 wxFont font
= attr
.GetFont();
104 font
= attrDef
.GetFont();
106 if ( text
&& !font
.Ok() )
107 font
= text
->GetFont();
110 wxColour colFg
= attr
.GetTextColour();
113 colFg
= attrDef
.GetTextColour();
115 if ( text
&& !colFg
.Ok() )
116 colFg
= text
->GetForegroundColour();
119 wxColour colBg
= attr
.GetBackgroundColour();
122 colBg
= attrDef
.GetBackgroundColour();
124 if ( text
&& !colBg
.Ok() )
125 colBg
= text
->GetBackgroundColour();
128 wxTextAttr
newAttr(colFg
, colBg
, font
);
130 if (attr
.HasAlignment())
131 newAttr
.SetAlignment(attr
.GetAlignment());
132 else if (attrDef
.HasAlignment())
133 newAttr
.SetAlignment(attrDef
.GetAlignment());
136 newAttr
.SetTabs(attr
.GetTabs());
137 else if (attrDef
.HasTabs())
138 newAttr
.SetTabs(attrDef
.GetTabs());
140 if (attr
.HasLeftIndent())
141 newAttr
.SetLeftIndent(attr
.GetLeftIndent());
142 else if (attrDef
.HasLeftIndent())
143 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent());
145 if (attr
.HasRightIndent())
146 newAttr
.SetRightIndent(attr
.GetRightIndent());
147 else if (attrDef
.HasRightIndent())
148 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
153 void wxTextAttr::operator= (const wxTextAttr
& attr
)
155 m_font
= attr
.m_font
;
156 m_colText
= attr
.m_colText
;
157 m_colBack
= attr
.m_colBack
;
158 m_textAlignment
= attr
.m_textAlignment
;
159 m_leftIndent
= attr
.m_leftIndent
;
160 m_rightIndent
= attr
.m_rightIndent
;
161 m_tabs
= attr
.m_tabs
;
162 m_flags
= attr
.m_flags
;
166 // apply styling to text range
167 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
168 const wxTextAttr
& WXUNUSED(style
))
170 // to be implemented in derived TextCtrl classes
174 // get the styling at the given position
175 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
177 // to be implemented in derived TextCtrl classes
181 // change default text attributes
182 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
184 // keep the old attributes if the new style doesn't specify them unless the
185 // new style is empty - then reset m_defaultStyle (as there is no other way
187 if ( style
.IsDefault() )
188 m_defaultStyle
= style
;
190 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
195 // get default text attributes
196 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
198 return m_defaultStyle
;
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
208 wxFFile
file(filename
);
209 if ( file
.IsOpened() )
212 if ( file
.ReadAll(&text
) )
218 m_filename
= filename
;
224 wxLogError(_("File couldn't be loaded."));
225 #endif // wxUSE_FFILE
230 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
232 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
233 if ( filenameToUse
.empty() )
235 // what kind of message to give? is it an error or a program bug?
236 wxLogDebug(wxT("Can't save textctrl to file without filename."));
242 wxFFile
file(filenameToUse
, _T("w"));
243 if ( file
.IsOpened() && file
.Write(GetValue()) )
245 // it's not modified any longer
248 // if it worked, save for future calls
249 m_filename
= filenameToUse
;
253 #endif // wxUSE_FFILE
255 wxLogError(_("The text couldn't be saved."));
260 // ----------------------------------------------------------------------------
261 // stream-like insertion operator
262 // ----------------------------------------------------------------------------
264 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
267 return *TEXTCTRL(this);
270 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
273 str
.Printf(wxT("%.2f"), f
);
275 return *TEXTCTRL(this);
278 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
281 str
.Printf(wxT("%.2f"), d
);
283 return *TEXTCTRL(this);
286 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
289 str
.Printf(wxT("%d"), i
);
291 return *TEXTCTRL(this);
294 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
297 str
.Printf(wxT("%ld"), i
);
299 return *TEXTCTRL(this);
302 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
304 return operator<<(wxString(c
));
307 // ----------------------------------------------------------------------------
308 // streambuf methods implementation
309 // ----------------------------------------------------------------------------
311 #ifndef NO_TEXT_WINDOW_STREAM
313 int wxTextCtrlBase::overflow(int c
)
315 AppendText((wxChar
)c
);
317 // return something different from EOF
321 #endif // NO_TEXT_WINDOW_STREAM
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 bool wxTextCtrlBase::CanCopy() const
329 // can copy if there's a selection
331 GetSelection(&from
, &to
);
335 bool wxTextCtrlBase::CanCut() const
337 // can cut if there's a selection and if we're not read only
338 return CanCopy() && IsEditable();
341 bool wxTextCtrlBase::CanPaste() const
343 // can paste if we are not read only
347 // ----------------------------------------------------------------------------
348 // emulating key presses
349 // ----------------------------------------------------------------------------
352 // the generic version is unused in wxMSW
353 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& WXUNUSED(event
))
358 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
361 int keycode
= event
.GetKeyCode();
374 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
378 case WXK_NUMPAD_MULTIPLY
:
388 case WXK_NUMPAD_SUBTRACT
:
393 case WXK_NUMPAD_DECIMAL
:
398 case WXK_NUMPAD_DIVIDE
:
403 case WXK_NUMPAD_DELETE
:
404 // delete the character at cursor
406 const long pos
= GetInsertionPoint(),
407 last
= GetLastPosition();
409 Remove(pos
, pos
+ 1);
414 // delete the character before the cursor
416 const long pos
= GetInsertionPoint();
418 Remove(pos
- 1, pos
);
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 if ( event
.GetSetEnabled() )
482 Enable(event
.GetEnabled());
484 if ( event
.GetSetText() )
486 if ( event
.GetText() != GetValue() )
487 SetValue(event
.GetText());
492 #else // !wxUSE_TEXTCTRL
494 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
495 // controls (wxComboBox and wxSpinCtrl)
496 #include "wx/event.h"
498 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
500 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL