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
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 wxTextCtrlBase::wxTextCtrlBase()
61 #ifndef NO_TEXT_WINDOW_STREAM
66 m_streambuf
= new char[64];
67 setp(m_streambuf
, m_streambuf
+ 64);
68 #endif //wxUSE_IOSTREAMH
69 #endif // NO_TEXT_WINDOW_STREAM
72 wxTextCtrlBase::~wxTextCtrlBase()
74 #ifndef NO_TEXT_WINDOW_STREAM
81 // ----------------------------------------------------------------------------
82 // style functions - not implemented here
83 // ----------------------------------------------------------------------------
85 // apply styling to text range
86 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
87 const wxTextAttr
& WXUNUSED(style
))
89 // to be implemented in derived TextCtrl classes
93 // change default text attributes
94 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
&style
)
96 m_defaultStyle
= style
;
100 // get default text attributes
101 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
103 return m_defaultStyle
;
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
113 wxFFile
file(filename
);
114 if ( file
.IsOpened() )
117 if ( file
.ReadAll(&text
) )
123 m_filename
= filename
;
129 wxLogError(_("File couldn't be loaded."));
130 #endif // wxUSE_FFILE
135 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
137 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
138 if ( !filenameToUse
)
140 // what kind of message to give? is it an error or a program bug?
141 wxLogDebug(wxT("Can't save textctrl to file without filename."));
147 wxFFile
file(filename
, "w");
148 if ( file
.IsOpened() && file
.Write(GetValue()) )
150 // it's not modified any longer
153 m_filename
= filename
;
158 wxLogError(_("The text couldn't be saved."));
159 #endif // wxUSE_FFILE
164 // ----------------------------------------------------------------------------
165 // stream-like insertion operator
166 // ----------------------------------------------------------------------------
168 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
171 return *TEXTCTRL(this);
174 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
177 str
.Printf(wxT("%.2f"), f
);
179 return *TEXTCTRL(this);
182 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
185 str
.Printf(wxT("%.2f"), d
);
187 return *TEXTCTRL(this);
190 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
193 str
.Printf(wxT("%d"), i
);
195 return *TEXTCTRL(this);
198 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
201 str
.Printf(wxT("%ld"), i
);
203 return *TEXTCTRL(this);
206 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
208 return operator<<(wxString(c
));
211 // ----------------------------------------------------------------------------
212 // streambuf methods implementation
213 // ----------------------------------------------------------------------------
215 #ifndef NO_TEXT_WINDOW_STREAM
217 int wxTextCtrlBase::overflow( int WXUNUSED(c
) )
219 int len
= pptr() - pbase();
220 char *txt
= new char[len
+1];
221 strncpy(txt
, pbase(), len
);
224 setp(pbase(), epptr());
229 int wxTextCtrlBase::sync()
231 int len
= pptr() - pbase();
232 char *txt
= new char[len
+1];
233 strncpy(txt
, pbase(), len
);
236 setp(pbase(), epptr());
241 int wxTextCtrlBase::underflow()
246 #endif // NO_TEXT_WINDOW_STREAM
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 bool wxTextCtrlBase::CanCopy() const
254 // can copy if there's a selection
256 GetSelection(&from
, &to
);
260 bool wxTextCtrlBase::CanCut() const
262 // can cut if there's a selection and if we're not read only
263 return CanCopy() && IsEditable();
266 bool wxTextCtrlBase::CanPaste() const
268 // can paste if we are not read only
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 void wxTextCtrlBase::SelectAll()
278 SetSelection(0, GetLastPosition());
281 #else // !wxUSE_TEXTCTRL
283 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
284 // controls (wxComboBox and wxSpinCtrl)
285 #include "wx/event.h"
287 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
289 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL