]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
13289f04 | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "textctrl.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/textctrl.h" | |
16 | #include "wx/utils.h" | |
17 | ||
18 | //----------------------------------------------------------------------------- | |
19 | // wxTextCtrl | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
23 | ||
112892b9 | 24 | void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
484e45bf | 25 | { |
9406d962 | 26 | win->SetModified(); |
112892b9 RR |
27 | }; |
28 | ||
c801d85f KB |
29 | |
30 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
31 | // EVT_CHAR(wxTextCtrl::OnChar) | |
32 | END_EVENT_TABLE() | |
33 | ||
34 | wxTextCtrl::wxTextCtrl(void) : streambuf() | |
35 | { | |
e2414cbe | 36 | if( allocate() ) |
13289f04 VZ |
37 | setp(base(),ebuf()); |
38 | ||
112892b9 | 39 | m_modified = FALSE; |
c801d85f KB |
40 | }; |
41 | ||
debe6624 | 42 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 43 | const wxPoint &pos, const wxSize &size, |
debe6624 | 44 | int style, const wxString &name ) : streambuf() |
c801d85f | 45 | { |
13289f04 VZ |
46 | if( allocate() ) |
47 | setp(base(),ebuf()); | |
48 | ||
112892b9 | 49 | m_modified = FALSE; |
c801d85f KB |
50 | Create( parent, id, value, pos, size, style, name ); |
51 | }; | |
52 | ||
debe6624 | 53 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 54 | const wxPoint &pos, const wxSize &size, |
debe6624 | 55 | int style, const wxString &name ) |
c801d85f KB |
56 | { |
57 | m_needParent = TRUE; | |
484e45bf | 58 | |
c801d85f | 59 | PreCreation( parent, id, pos, size, style, name ); |
484e45bf | 60 | |
13289f04 | 61 | bool bMultiLine = (style & wxTE_MULTILINE) != 0; |
47908e25 RR |
62 | if ( bMultiLine ) |
63 | { | |
13289f04 VZ |
64 | // a multi-line edit control: create a vertical scrollbar by default and |
65 | // horizontal if requested | |
66 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; | |
67 | ||
68 | // create our control... | |
69 | m_text = gtk_text_new( NULL, NULL ); | |
70 | ||
71 | // ... and put into the upper left hand corner of the table | |
72 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); | |
73 | gtk_table_attach(GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, | |
74 | GTK_FILL | GTK_EXPAND, | |
75 | GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
76 | 0, 0); | |
77 | ||
78 | // put the horizontal scrollbar in the lower left hand corner | |
79 | if ( bHasHScrollbar ) { | |
80 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); | |
81 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, | |
82 | GTK_EXPAND | GTK_FILL, | |
83 | GTK_FILL, | |
84 | 0, 0); | |
85 | gtk_widget_show(hscrollbar); | |
86 | } | |
87 | ||
88 | // finally, put the vertical scrollbar in the upper right corner | |
89 | GtkWidget *vscrollbar = gtk_vscrollbar_new(GTK_TEXT(m_text)->vadj); | |
90 | gtk_table_attach(GTK_TABLE(m_widget), vscrollbar, 1, 2, 0, 1, | |
91 | GTK_FILL, | |
92 | GTK_EXPAND | GTK_FILL | GTK_SHRINK, | |
93 | 0, 0); | |
94 | gtk_widget_show(vscrollbar); | |
95 | } | |
96 | else { | |
97 | // a single-line text control: no need for scrollbars | |
98 | m_widget = | |
99 | m_text = gtk_entry_new(); | |
100 | } | |
484e45bf | 101 | |
c801d85f KB |
102 | wxSize newSize = size; |
103 | if (newSize.x == -1) newSize.x = 80; | |
104 | if (newSize.y == -1) newSize.y = 26; | |
105 | SetSize( newSize.x, newSize.y ); | |
484e45bf | 106 | |
c801d85f | 107 | PostCreation(); |
484e45bf | 108 | |
13289f04 VZ |
109 | if ( bMultiLine ) { |
110 | gtk_widget_realize(m_text); | |
111 | gtk_widget_show(m_text); | |
112 | } | |
113 | ||
484e45bf | 114 | // we want to be notified about text changes |
13289f04 | 115 | gtk_signal_connect(GTK_OBJECT(m_text), "changed", |
484e45bf VZ |
116 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), |
117 | (gpointer)this); | |
118 | ||
7f4dc78d RR |
119 | if (!value.IsNull()) |
120 | { | |
121 | gint tmp = 0; | |
13289f04 | 122 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); |
112892b9 | 123 | }; |
484e45bf | 124 | |
112892b9 RR |
125 | if (style & wxREADONLY) |
126 | { | |
127 | } | |
128 | else | |
129 | { | |
13289f04 VZ |
130 | if ( bMultiLine ) |
131 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
7f4dc78d | 132 | }; |
484e45bf | 133 | |
c801d85f | 134 | Show( TRUE ); |
484e45bf | 135 | |
c801d85f KB |
136 | return TRUE; |
137 | }; | |
138 | ||
139 | wxString wxTextCtrl::GetValue(void) const | |
140 | { | |
141 | wxString tmp; | |
142 | if (m_windowStyle & wxTE_MULTILINE) | |
143 | { | |
13289f04 VZ |
144 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
145 | tmp = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
c801d85f KB |
146 | } |
147 | else | |
148 | { | |
13289f04 | 149 | tmp = gtk_entry_get_text( GTK_ENTRY(m_text) ); |
c801d85f KB |
150 | }; |
151 | return tmp; | |
152 | }; | |
153 | ||
154 | void wxTextCtrl::SetValue( const wxString &value ) | |
155 | { | |
156 | wxString tmp = ""; | |
157 | if (!value.IsNull()) tmp = value; | |
158 | if (m_windowStyle & wxTE_MULTILINE) | |
159 | { | |
13289f04 VZ |
160 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
161 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
c801d85f | 162 | len = 0; |
13289f04 | 163 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len ); |
c801d85f KB |
164 | } |
165 | else | |
166 | { | |
13289f04 | 167 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp ); |
c801d85f KB |
168 | }; |
169 | }; | |
170 | ||
171 | void wxTextCtrl::WriteText( const wxString &text ) | |
172 | { | |
173 | if (text.IsNull()) return; | |
484e45bf | 174 | |
c801d85f KB |
175 | if (m_windowStyle & wxTE_MULTILINE) |
176 | { | |
13289f04 VZ |
177 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
178 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); | |
c801d85f KB |
179 | } |
180 | else | |
181 | { | |
13289f04 | 182 | gtk_entry_append_text( GTK_ENTRY(m_text), text ); |
c801d85f KB |
183 | }; |
184 | }; | |
185 | ||
112892b9 | 186 | bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) ) |
c801d85f | 187 | { |
2ad3a34e VZ |
188 | wxFAIL_MSG("wxTextCtrl::LoadFile not implemented"); |
189 | ||
112892b9 | 190 | return FALSE; |
c801d85f KB |
191 | }; |
192 | ||
112892b9 | 193 | bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) ) |
c801d85f | 194 | { |
2ad3a34e VZ |
195 | wxFAIL_MSG("wxTextCtrl::SaveFile not implemented"); |
196 | ||
112892b9 | 197 | return FALSE; |
c801d85f KB |
198 | }; |
199 | ||
112892b9 | 200 | /* |
debe6624 | 201 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
c801d85f KB |
202 | { |
203 | }; | |
204 | ||
112892b9 | 205 | |
c801d85f KB |
206 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event ) |
207 | { | |
208 | }; | |
209 | ||
debe6624 | 210 | long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const |
c801d85f KB |
211 | { |
212 | }; | |
213 | ||
debe6624 | 214 | long wxTextCtrl::XYToPosition( long x, long y ) |
c801d85f KB |
215 | { |
216 | }; | |
217 | ||
218 | int wxTextCtrl::GetNumberOfLines(void) | |
219 | { | |
220 | }; | |
221 | ||
222 | */ | |
debe6624 | 223 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f KB |
224 | { |
225 | int tmp = (int) pos; | |
226 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 227 | gtk_text_set_point( GTK_TEXT(m_text), tmp ); |
c801d85f | 228 | else |
13289f04 | 229 | gtk_entry_set_position( GTK_ENTRY(m_text), tmp ); |
c801d85f KB |
230 | }; |
231 | ||
232 | void wxTextCtrl::SetInsertionPointEnd(void) | |
233 | { | |
234 | int pos = 0; | |
235 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 236 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
c801d85f | 237 | else |
13289f04 | 238 | pos = GTK_ENTRY(m_text)->text_length; |
c801d85f KB |
239 | SetInsertionPoint( pos-1 ); |
240 | }; | |
241 | ||
debe6624 | 242 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f KB |
243 | { |
244 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 245 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); |
c801d85f | 246 | else |
13289f04 | 247 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); |
c801d85f KB |
248 | }; |
249 | ||
debe6624 | 250 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 251 | { |
13289f04 | 252 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
c801d85f KB |
253 | }; |
254 | ||
debe6624 | 255 | void wxTextCtrl::ShowPosition( long WXUNUSED(pos) ) |
c801d85f | 256 | { |
2ad3a34e | 257 | wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented"); |
c801d85f KB |
258 | }; |
259 | ||
260 | long wxTextCtrl::GetInsertionPoint(void) const | |
261 | { | |
13289f04 | 262 | return (long) GTK_EDITABLE(m_text)->current_pos; |
c801d85f KB |
263 | }; |
264 | ||
265 | long wxTextCtrl::GetLastPosition(void) const | |
266 | { | |
267 | int pos = 0; | |
268 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 269 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
c801d85f | 270 | else |
13289f04 | 271 | pos = GTK_ENTRY(m_text)->text_length; |
c801d85f KB |
272 | return (long)pos-1; |
273 | }; | |
274 | ||
debe6624 | 275 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 276 | { |
13289f04 | 277 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
c801d85f KB |
278 | }; |
279 | ||
debe6624 | 280 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 281 | { |
13289f04 | 282 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
c801d85f KB |
283 | if (value.IsNull()) return; |
284 | gint pos = (gint)to; | |
13289f04 | 285 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
c801d85f KB |
286 | }; |
287 | ||
288 | void wxTextCtrl::Cut(void) | |
289 | { | |
13289f04 | 290 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 ); |
c801d85f KB |
291 | }; |
292 | ||
293 | void wxTextCtrl::Copy(void) | |
294 | { | |
13289f04 | 295 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 ); |
c801d85f KB |
296 | }; |
297 | ||
298 | void wxTextCtrl::Paste(void) | |
299 | { | |
13289f04 | 300 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 ); |
c801d85f KB |
301 | }; |
302 | ||
303 | void wxTextCtrl::Delete(void) | |
304 | { | |
305 | SetValue( "" ); | |
306 | }; | |
307 | ||
308 | void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) ) | |
309 | { | |
310 | }; | |
311 | ||
46dc76ba | 312 | int wxTextCtrl::overflow( int WXUNUSED(c) ) |
c801d85f | 313 | { |
c801d85f KB |
314 | int len = pptr() - pbase(); |
315 | char *txt = new char[len+1]; | |
316 | strncpy(txt, pbase(), len); | |
317 | txt[len] = '\0'; | |
318 | (*this) << txt; | |
319 | setp(pbase(), epptr()); | |
320 | delete[] txt; | |
321 | return EOF; | |
c801d85f KB |
322 | }; |
323 | ||
324 | int wxTextCtrl::sync(void) | |
325 | { | |
c801d85f KB |
326 | int len = pptr() - pbase(); |
327 | char *txt = new char[len+1]; | |
328 | strncpy(txt, pbase(), len); | |
329 | txt[len] = '\0'; | |
330 | (*this) << txt; | |
331 | setp(pbase(), epptr()); | |
332 | delete[] txt; | |
333 | return 0; | |
c801d85f KB |
334 | }; |
335 | ||
336 | int wxTextCtrl::underflow(void) | |
337 | { | |
338 | return EOF; | |
339 | }; | |
340 | ||
341 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
342 | { | |
343 | WriteText(s); | |
344 | return *this; | |
345 | } | |
346 | ||
debe6624 | 347 | wxTextCtrl& wxTextCtrl::operator<<(float f) |
c801d85f KB |
348 | { |
349 | static char buf[100]; | |
350 | sprintf(buf, "%.2f", f); | |
351 | WriteText(buf); | |
352 | return *this; | |
353 | } | |
354 | ||
debe6624 | 355 | wxTextCtrl& wxTextCtrl::operator<<(double d) |
c801d85f KB |
356 | { |
357 | static char buf[100]; | |
358 | sprintf(buf, "%.2f", d); | |
359 | WriteText(buf); | |
360 | return *this; | |
361 | } | |
362 | ||
debe6624 | 363 | wxTextCtrl& wxTextCtrl::operator<<(int i) |
c801d85f KB |
364 | { |
365 | static char buf[100]; | |
366 | sprintf(buf, "%i", i); | |
367 | WriteText(buf); | |
368 | return *this; | |
369 | } | |
370 | ||
debe6624 | 371 | wxTextCtrl& wxTextCtrl::operator<<(long i) |
c801d85f KB |
372 | { |
373 | static char buf[100]; | |
374 | sprintf(buf, "%ld", i); | |
375 | WriteText(buf); | |
376 | return *this; | |
377 | } | |
378 | ||
379 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
380 | { | |
381 | char buf[2]; | |
382 | ||
383 | buf[0] = c; | |
384 | buf[1] = 0; | |
385 | WriteText(buf); | |
386 | return *this; | |
387 | } | |
388 | ||
e3e65dac RR |
389 | GtkWidget* wxTextCtrl::GetDropTargetWidget(void) |
390 | { | |
391 | return GTK_WIDGET(m_text); | |
392 | }; | |
393 | ||
394 | ||
395 | ||
396 |