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