]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textcmn.cpp
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 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 wxTextCtrlBase::wxTextCtrlBase()
55 #ifndef NO_TEXT_WINDOW_STREAM
60 m_streambuf
= new char[64];
61 setp(m_streambuf
, m_streambuf
+ 64);
62 #endif //wxUSE_IOSTREAMH
63 #endif // NO_TEXT_WINDOW_STREAM
66 wxTextCtrlBase::~wxTextCtrlBase()
68 #ifndef NO_TEXT_WINDOW_STREAM
75 // ----------------------------------------------------------------------------
76 // style functions - not implemented here
77 // ----------------------------------------------------------------------------
79 // apply styling to text range
80 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
81 const wxTextAttr
& WXUNUSED(style
))
83 // to be implemented in derived TextCtrl classes
87 // change default text attributes
88 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
&style
)
90 m_defaultStyle
= style
;
94 // get default text attributes
95 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
97 return m_defaultStyle
;
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
107 wxFFile
file(filename
);
108 if ( file
.IsOpened() )
111 if ( file
.ReadAll(&text
) )
117 m_filename
= filename
;
123 wxLogError(_("File couldn't be loaded."));
124 #endif // wxUSE_FFILE
129 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
131 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
132 if ( !filenameToUse
)
134 // what kind of message to give? is it an error or a program bug?
135 wxLogDebug(wxT("Can't save textctrl to file without filename."));
141 wxFFile
file(filename
, "w");
142 if ( file
.IsOpened() && file
.Write(GetValue()) )
144 // it's not modified any longer
147 m_filename
= filename
;
152 wxLogError(_("The text couldn't be saved."));
153 #endif // wxUSE_FFILE
158 // ----------------------------------------------------------------------------
159 // stream-like insertion operator
160 // ----------------------------------------------------------------------------
162 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
165 return *TEXTCTRL(this);
168 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
171 str
.Printf(wxT("%.2f"), f
);
173 return *TEXTCTRL(this);
176 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
179 str
.Printf(wxT("%.2f"), d
);
181 return *TEXTCTRL(this);
184 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
187 str
.Printf(wxT("%d"), i
);
189 return *TEXTCTRL(this);
192 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
195 str
.Printf(wxT("%ld"), i
);
197 return *TEXTCTRL(this);
200 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
202 return operator<<(wxString(c
));
205 // ----------------------------------------------------------------------------
206 // streambuf methods implementation
207 // ----------------------------------------------------------------------------
209 #ifndef NO_TEXT_WINDOW_STREAM
211 int wxTextCtrlBase::overflow( int WXUNUSED(c
) )
213 int len
= pptr() - pbase();
214 char *txt
= new char[len
+1];
215 strncpy(txt
, pbase(), len
);
218 setp(pbase(), epptr());
223 int wxTextCtrlBase::sync()
225 int len
= pptr() - pbase();
226 char *txt
= new char[len
+1];
227 strncpy(txt
, pbase(), len
);
230 setp(pbase(), epptr());
235 int wxTextCtrlBase::underflow()
240 #endif // NO_TEXT_WINDOW_STREAM
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 bool wxTextCtrlBase::CanCopy() const
248 // can copy if there's a selection
250 GetSelection(&from
, &to
);
254 bool wxTextCtrlBase::CanCut() const
256 // can cut if there's a selection and if we're not read only
257 return CanCopy() && IsEditable();
260 bool wxTextCtrlBase::CanPaste() const
262 // can paste if we are not read only
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 void wxTextCtrlBase::SelectAll()
272 SetSelection(0, GetLastPosition());
275 #endif // wxUSE_TEXTCTRL