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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
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 // ----------------------------------------------------------------------------
73 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
74 const wxTextAttr
& attrDef
,
75 const wxTextCtrlBase
*text
)
77 wxFont font
= attr
.GetFont();
80 font
= attrDef
.GetFont();
82 if ( text
&& !font
.Ok() )
83 font
= text
->GetFont();
86 wxColour colFg
= attr
.GetTextColour();
89 colFg
= attrDef
.GetTextColour();
91 if ( text
&& !colFg
.Ok() )
92 colFg
= text
->GetForegroundColour();
95 wxColour colBg
= attr
.GetBackgroundColour();
98 colBg
= attrDef
.GetBackgroundColour();
100 if ( text
&& !colBg
.Ok() )
101 colBg
= text
->GetBackgroundColour();
104 return wxTextAttr(colFg
, colBg
, font
);
107 // apply styling to text range
108 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
109 const wxTextAttr
& WXUNUSED(style
))
111 // to be implemented in derived TextCtrl classes
115 // change default text attributes
116 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
118 // keep the old attributes if the new style doesn't specify them
119 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
124 // get default text attributes
125 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
127 return m_defaultStyle
;
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
137 wxFFile
file(filename
);
138 if ( file
.IsOpened() )
141 if ( file
.ReadAll(&text
) )
147 m_filename
= filename
;
153 wxLogError(_("File couldn't be loaded."));
154 #endif // wxUSE_FFILE
159 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
161 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
162 if ( !filenameToUse
)
164 // what kind of message to give? is it an error or a program bug?
165 wxLogDebug(wxT("Can't save textctrl to file without filename."));
171 wxFFile
file(filename
, "w");
172 if ( file
.IsOpened() && file
.Write(GetValue()) )
174 // it's not modified any longer
177 m_filename
= filename
;
182 wxLogError(_("The text couldn't be saved."));
183 #endif // wxUSE_FFILE
188 // ----------------------------------------------------------------------------
189 // stream-like insertion operator
190 // ----------------------------------------------------------------------------
192 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
195 return *TEXTCTRL(this);
198 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
201 str
.Printf(wxT("%.2f"), f
);
203 return *TEXTCTRL(this);
206 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
209 str
.Printf(wxT("%.2f"), d
);
211 return *TEXTCTRL(this);
214 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
217 str
.Printf(wxT("%d"), i
);
219 return *TEXTCTRL(this);
222 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
225 str
.Printf(wxT("%ld"), i
);
227 return *TEXTCTRL(this);
230 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
232 return operator<<(wxString(c
));
235 // ----------------------------------------------------------------------------
236 // streambuf methods implementation
237 // ----------------------------------------------------------------------------
239 #ifndef NO_TEXT_WINDOW_STREAM
241 int wxTextCtrlBase::overflow(int c
)
243 AppendText((wxChar
)c
);
245 // return something different from EOF
249 #endif // NO_TEXT_WINDOW_STREAM
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 bool wxTextCtrlBase::CanCopy() const
257 // can copy if there's a selection
259 GetSelection(&from
, &to
);
263 bool wxTextCtrlBase::CanCut() const
265 // can cut if there's a selection and if we're not read only
266 return CanCopy() && IsEditable();
269 bool wxTextCtrlBase::CanPaste() const
271 // can paste if we are not read only
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 void wxTextCtrlBase::SelectAll()
281 SetSelection(0, GetLastPosition());
284 wxString
wxTextCtrlBase::GetStringSelection() const
287 GetSelection(&from
, &to
);
292 sel
= GetValue().Mid(from
, to
- from
);
298 #else // !wxUSE_TEXTCTRL
300 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
301 // controls (wxComboBox and wxSpinCtrl)
302 #include "wx/event.h"
304 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
306 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL