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