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()
62 #ifndef NO_TEXT_WINDOW_STREAM
67 m_streambuf
= new char[64];
68 setp(m_streambuf
, m_streambuf
+ 64);
69 #endif //wxUSE_IOSTREAMH
70 #endif // NO_TEXT_WINDOW_STREAM
73 wxTextCtrlBase::~wxTextCtrlBase()
75 #ifndef NO_TEXT_WINDOW_STREAM
82 // ----------------------------------------------------------------------------
83 // style functions - not implemented here
84 // ----------------------------------------------------------------------------
86 // apply styling to text range
87 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
88 const wxTextAttr
& WXUNUSED(style
))
90 // to be implemented in derived TextCtrl classes
94 // change default text attributes
95 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
&style
)
97 m_defaultStyle
= style
;
101 // get default text attributes
102 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
104 return m_defaultStyle
;
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
114 wxFFile
file(filename
);
115 if ( file
.IsOpened() )
118 if ( file
.ReadAll(&text
) )
124 m_filename
= filename
;
130 wxLogError(_("File couldn't be loaded."));
131 #endif // wxUSE_FFILE
136 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
138 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
139 if ( !filenameToUse
)
141 // what kind of message to give? is it an error or a program bug?
142 wxLogDebug(wxT("Can't save textctrl to file without filename."));
148 wxFFile
file(filename
, "w");
149 if ( file
.IsOpened() && file
.Write(GetValue()) )
151 // it's not modified any longer
154 m_filename
= filename
;
159 wxLogError(_("The text couldn't be saved."));
160 #endif // wxUSE_FFILE
165 // ----------------------------------------------------------------------------
166 // stream-like insertion operator
167 // ----------------------------------------------------------------------------
169 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
172 return *TEXTCTRL(this);
175 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
178 str
.Printf(wxT("%.2f"), f
);
180 return *TEXTCTRL(this);
183 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
186 str
.Printf(wxT("%.2f"), d
);
188 return *TEXTCTRL(this);
191 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
194 str
.Printf(wxT("%d"), i
);
196 return *TEXTCTRL(this);
199 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
202 str
.Printf(wxT("%ld"), i
);
204 return *TEXTCTRL(this);
207 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
209 return operator<<(wxString(c
));
212 // ----------------------------------------------------------------------------
213 // streambuf methods implementation
214 // ----------------------------------------------------------------------------
216 #ifndef NO_TEXT_WINDOW_STREAM
218 int wxTextCtrlBase::overflow( int WXUNUSED(c
) )
220 int len
= pptr() - pbase();
221 char *txt
= new char[len
+1];
222 strncpy(txt
, pbase(), len
);
225 setp(pbase(), epptr());
230 int wxTextCtrlBase::sync()
232 int len
= pptr() - pbase();
233 char *txt
= new char[len
+1];
234 strncpy(txt
, pbase(), len
);
237 setp(pbase(), epptr());
242 int wxTextCtrlBase::underflow()
247 #endif // NO_TEXT_WINDOW_STREAM
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 bool wxTextCtrlBase::CanCopy() const
255 // can copy if there's a selection
257 GetSelection(&from
, &to
);
261 bool wxTextCtrlBase::CanCut() const
263 // can cut if there's a selection and if we're not read only
264 return CanCopy() && IsEditable();
267 bool wxTextCtrlBase::CanPaste() const
269 // can paste if we are not read only
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void wxTextCtrlBase::SelectAll()
279 SetSelection(0, GetLastPosition());
282 #else // !wxUSE_TEXTCTRL
284 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
285 // controls (wxComboBox and wxSpinCtrl)
286 #include "wx/event.h"
288 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
290 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL