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 // ----------------------------------------------------------------------------
72 // apply styling to text range
73 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
74 const wxTextAttr
& WXUNUSED(style
))
76 // to be implemented in derived TextCtrl classes
80 // change default text attributes
81 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
&style
)
83 m_defaultStyle
= style
;
87 // get default text attributes
88 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
90 return m_defaultStyle
;
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
100 wxFFile
file(filename
);
101 if ( file
.IsOpened() )
104 if ( file
.ReadAll(&text
) )
110 m_filename
= filename
;
116 wxLogError(_("File couldn't be loaded."));
117 #endif // wxUSE_FFILE
122 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
124 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
125 if ( !filenameToUse
)
127 // what kind of message to give? is it an error or a program bug?
128 wxLogDebug(wxT("Can't save textctrl to file without filename."));
134 wxFFile
file(filename
, "w");
135 if ( file
.IsOpened() && file
.Write(GetValue()) )
137 // it's not modified any longer
140 m_filename
= filename
;
145 wxLogError(_("The text couldn't be saved."));
146 #endif // wxUSE_FFILE
151 // ----------------------------------------------------------------------------
152 // stream-like insertion operator
153 // ----------------------------------------------------------------------------
155 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
158 return *TEXTCTRL(this);
161 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
164 str
.Printf(wxT("%.2f"), f
);
166 return *TEXTCTRL(this);
169 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
172 str
.Printf(wxT("%.2f"), d
);
174 return *TEXTCTRL(this);
177 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
180 str
.Printf(wxT("%d"), i
);
182 return *TEXTCTRL(this);
185 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
188 str
.Printf(wxT("%ld"), i
);
190 return *TEXTCTRL(this);
193 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
195 return operator<<(wxString(c
));
198 // ----------------------------------------------------------------------------
199 // streambuf methods implementation
200 // ----------------------------------------------------------------------------
202 #ifndef NO_TEXT_WINDOW_STREAM
204 int wxTextCtrlBase::overflow(int c
)
206 AppendText((wxChar
)c
);
208 // return something different from EOF
212 #endif // NO_TEXT_WINDOW_STREAM
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxTextCtrlBase::CanCopy() const
220 // can copy if there's a selection
222 GetSelection(&from
, &to
);
226 bool wxTextCtrlBase::CanCut() const
228 // can cut if there's a selection and if we're not read only
229 return CanCopy() && IsEditable();
232 bool wxTextCtrlBase::CanPaste() const
234 // can paste if we are not read only
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 void wxTextCtrlBase::SelectAll()
244 SetSelection(0, GetLastPosition());
247 wxString
wxTextCtrlBase::GetStringSelection() const
250 GetSelection(&from
, &to
);
255 sel
= GetValue().Mid(from
, to
- from
);
261 #else // !wxUSE_TEXTCTRL
263 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
264 // controls (wxComboBox and wxSpinCtrl)
265 #include "wx/event.h"
267 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
269 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL