]>
Commit | Line | Data |
---|---|---|
a1b82138 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/textcmn.cpp | |
3 | // Purpose: implementation of platform-independent functions of wxTextCtrl | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 13.07.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWindows team | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
1e6feb95 | 15 | |
bf3e0fbd KB |
16 | #ifdef __GNUG__ |
17 | #pragma implementation "textctrlbase.h" | |
18 | #endif | |
1e6feb95 | 19 | |
a1b82138 VZ |
20 | // for compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_TEXTCTRL |
28 | ||
a1b82138 | 29 | #ifndef WX_PRECOMP |
0efe5ba7 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
a1b82138 VZ |
32 | #include "wx/textctrl.h" |
33 | #endif // WX_PRECOMP | |
34 | ||
35 | #include "wx/ffile.h" | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // macros | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
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)) | |
44 | ||
45 | // ============================================================================ | |
46 | // implementation | |
47 | // ============================================================================ | |
48 | ||
c57e3339 VZ |
49 | IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent) |
50 | ||
51 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED) | |
52 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER) | |
53 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL) | |
d7eee191 | 54 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN) |
c57e3339 | 55 | |
a1b82138 VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // ctor | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | wxTextCtrlBase::wxTextCtrlBase() | |
61 | { | |
fa40e7a1 SB |
62 | } |
63 | ||
64 | wxTextCtrlBase::~wxTextCtrlBase() | |
65 | { | |
a1b82138 VZ |
66 | } |
67 | ||
4bc1afd5 VZ |
68 | // ---------------------------------------------------------------------------- |
69 | // style functions - not implemented here | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | // apply styling to text range | |
73 | bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end), | |
74 | const wxTextAttr& WXUNUSED(style)) | |
75 | { | |
76 | // to be implemented in derived TextCtrl classes | |
77 | return FALSE; | |
78 | } | |
79 | ||
80 | // change default text attributes | |
81 | bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr &style) | |
82 | { | |
83 | m_defaultStyle = style; | |
84 | return TRUE; | |
85 | } | |
86 | ||
87 | // get default text attributes | |
88 | const wxTextAttr& wxTextCtrlBase::GetDefaultStyle() const | |
89 | { | |
90 | return m_defaultStyle; | |
91 | } | |
92 | ||
a1b82138 VZ |
93 | // ---------------------------------------------------------------------------- |
94 | // file IO functions | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | bool wxTextCtrlBase::LoadFile(const wxString& filename) | |
98 | { | |
1e6feb95 | 99 | #if wxUSE_FFILE |
a1b82138 VZ |
100 | wxFFile file(filename); |
101 | if ( file.IsOpened() ) | |
102 | { | |
103 | wxString text; | |
104 | if ( file.ReadAll(&text) ) | |
105 | { | |
106 | SetValue(text); | |
107 | ||
108 | DiscardEdits(); | |
109 | ||
110 | m_filename = filename; | |
111 | ||
112 | return TRUE; | |
113 | } | |
114 | } | |
115 | ||
116 | wxLogError(_("File couldn't be loaded.")); | |
1e6feb95 | 117 | #endif // wxUSE_FFILE |
a1b82138 VZ |
118 | |
119 | return FALSE; | |
120 | } | |
121 | ||
122 | bool wxTextCtrlBase::SaveFile(const wxString& filename) | |
123 | { | |
124 | wxString filenameToUse = filename.IsEmpty() ? m_filename : filename; | |
125 | if ( !filenameToUse ) | |
126 | { | |
127 | // what kind of message to give? is it an error or a program bug? | |
223d09f6 | 128 | wxLogDebug(wxT("Can't save textctrl to file without filename.")); |
a1b82138 VZ |
129 | |
130 | return FALSE; | |
131 | } | |
132 | ||
1e6feb95 | 133 | #if wxUSE_FFILE |
a1b82138 VZ |
134 | wxFFile file(filename, "w"); |
135 | if ( file.IsOpened() && file.Write(GetValue()) ) | |
136 | { | |
137 | // it's not modified any longer | |
138 | DiscardEdits(); | |
139 | ||
140 | m_filename = filename; | |
141 | ||
142 | return TRUE; | |
143 | } | |
144 | ||
145 | wxLogError(_("The text couldn't be saved.")); | |
1e6feb95 | 146 | #endif // wxUSE_FFILE |
a1b82138 VZ |
147 | |
148 | return FALSE; | |
149 | } | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // stream-like insertion operator | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s) | |
156 | { | |
157 | AppendText(s); | |
158 | return *TEXTCTRL(this); | |
159 | } | |
160 | ||
161 | wxTextCtrl& wxTextCtrlBase::operator<<(float f) | |
162 | { | |
163 | wxString str; | |
223d09f6 | 164 | str.Printf(wxT("%.2f"), f); |
a1b82138 VZ |
165 | AppendText(str); |
166 | return *TEXTCTRL(this); | |
167 | } | |
168 | ||
169 | wxTextCtrl& wxTextCtrlBase::operator<<(double d) | |
170 | { | |
171 | wxString str; | |
223d09f6 | 172 | str.Printf(wxT("%.2f"), d); |
a1b82138 VZ |
173 | AppendText(str); |
174 | return *TEXTCTRL(this); | |
175 | } | |
176 | ||
177 | wxTextCtrl& wxTextCtrlBase::operator<<(int i) | |
178 | { | |
179 | wxString str; | |
223d09f6 | 180 | str.Printf(wxT("%d"), i); |
a1b82138 VZ |
181 | AppendText(str); |
182 | return *TEXTCTRL(this); | |
183 | } | |
184 | ||
185 | wxTextCtrl& wxTextCtrlBase::operator<<(long i) | |
186 | { | |
187 | wxString str; | |
223d09f6 | 188 | str.Printf(wxT("%ld"), i); |
a1b82138 VZ |
189 | AppendText(str); |
190 | return *TEXTCTRL(this); | |
191 | } | |
192 | ||
a324a7bc | 193 | wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c) |
a1b82138 VZ |
194 | { |
195 | return operator<<(wxString(c)); | |
196 | } | |
197 | ||
198 | // ---------------------------------------------------------------------------- | |
199 | // streambuf methods implementation | |
200 | // ---------------------------------------------------------------------------- | |
201 | ||
202 | #ifndef NO_TEXT_WINDOW_STREAM | |
203 | ||
d73e6791 | 204 | int wxTextCtrlBase::overflow(int c) |
a1b82138 | 205 | { |
d73e6791 | 206 | AppendText((wxChar)c); |
a1b82138 | 207 | |
d73e6791 | 208 | // return something different from EOF |
a1b82138 VZ |
209 | return 0; |
210 | } | |
211 | ||
a1b82138 VZ |
212 | #endif // NO_TEXT_WINDOW_STREAM |
213 | ||
1e6feb95 VZ |
214 | // ---------------------------------------------------------------------------- |
215 | // clipboard stuff | |
216 | // ---------------------------------------------------------------------------- | |
217 | ||
218 | bool wxTextCtrlBase::CanCopy() const | |
219 | { | |
220 | // can copy if there's a selection | |
221 | long from, to; | |
222 | GetSelection(&from, &to); | |
223 | return from != to; | |
224 | } | |
225 | ||
226 | bool wxTextCtrlBase::CanCut() const | |
227 | { | |
228 | // can cut if there's a selection and if we're not read only | |
229 | return CanCopy() && IsEditable(); | |
230 | } | |
231 | ||
232 | bool wxTextCtrlBase::CanPaste() const | |
233 | { | |
234 | // can paste if we are not read only | |
235 | return IsEditable(); | |
236 | } | |
237 | ||
238 | // ---------------------------------------------------------------------------- | |
239 | // misc | |
240 | // ---------------------------------------------------------------------------- | |
241 | ||
242 | void wxTextCtrlBase::SelectAll() | |
243 | { | |
244 | SetSelection(0, GetLastPosition()); | |
245 | } | |
246 | ||
eef97940 | 247 | wxString wxTextCtrlBase::GetStringSelection() const |
18414479 VZ |
248 | { |
249 | long from, to; | |
250 | GetSelection(&from, &to); | |
251 | ||
252 | wxString sel; | |
253 | if ( from < to ) | |
254 | { | |
255 | sel = GetValue().Mid(from, to - from); | |
256 | } | |
257 | ||
258 | return sel; | |
259 | } | |
260 | ||
ad0bae85 VZ |
261 | #else // !wxUSE_TEXTCTRL |
262 | ||
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" | |
266 | ||
267 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED) | |
268 | ||
269 | #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL | |
1e6feb95 | 270 |