]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin |
13289f04 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "textctrl.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/textctrl.h" | |
15 | #include "wx/utils.h" | |
ae0bdb01 RR |
16 | #include "wx/intl.h" |
17 | #include "wx/settings.h" | |
c801d85f | 18 | |
a81258be RR |
19 | #include <sys/types.h> |
20 | #include <sys/stat.h> | |
21 | #include <ctype.h> | |
22 | ||
c801d85f | 23 | //----------------------------------------------------------------------------- |
2f2aa628 | 24 | // "changed" |
c801d85f KB |
25 | //----------------------------------------------------------------------------- |
26 | ||
6de97a3b | 27 | static void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
484e45bf | 28 | { |
9406d962 | 29 | win->SetModified(); |
8bbe427f | 30 | |
903f689b RR |
31 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId ); |
32 | wxString val( win->GetValue() ); | |
33 | if (!val.IsNull()) event.m_commandString = WXSTRINGCAST val; | |
34 | event.SetEventObject( win ); | |
35 | win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 36 | } |
112892b9 | 37 | |
2f2aa628 RR |
38 | //----------------------------------------------------------------------------- |
39 | // wxTextCtrl | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
43 | ||
c801d85f | 44 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
903f689b | 45 | EVT_CHAR(wxTextCtrl::OnChar) |
c801d85f KB |
46 | END_EVENT_TABLE() |
47 | ||
03f38c58 | 48 | wxTextCtrl::wxTextCtrl() : streambuf() |
c801d85f | 49 | { |
6de97a3b | 50 | if (allocate()) setp(base(),ebuf()); |
13289f04 | 51 | |
112892b9 | 52 | m_modified = FALSE; |
6de97a3b | 53 | } |
c801d85f | 54 | |
debe6624 | 55 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 56 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 57 | int style, const wxValidator& validator, const wxString &name ) : streambuf() |
c801d85f | 58 | { |
6de97a3b | 59 | if (allocate()) setp(base(),ebuf()); |
13289f04 | 60 | |
112892b9 | 61 | m_modified = FALSE; |
6de97a3b RR |
62 | Create( parent, id, value, pos, size, style, validator, name ); |
63 | } | |
c801d85f | 64 | |
debe6624 | 65 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 66 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 67 | int style, const wxValidator& validator, const wxString &name ) |
c801d85f KB |
68 | { |
69 | m_needParent = TRUE; | |
484e45bf | 70 | |
c801d85f | 71 | PreCreation( parent, id, pos, size, style, name ); |
484e45bf | 72 | |
6de97a3b RR |
73 | SetValidator( validator ); |
74 | ||
ec758a20 RR |
75 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
76 | if ( multi_line ) | |
47908e25 | 77 | { |
13289f04 VZ |
78 | // a multi-line edit control: create a vertical scrollbar by default and |
79 | // horizontal if requested | |
80 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; | |
81 | ||
82 | // create our control... | |
c67daf87 | 83 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); |
13289f04 VZ |
84 | |
85 | // ... and put into the upper left hand corner of the table | |
86 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); | |
f5368809 | 87 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, |
41dee9d0 | 88 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
f5368809 RR |
89 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
90 | 0, 0); | |
13289f04 VZ |
91 | |
92 | // put the horizontal scrollbar in the lower left hand corner | |
8bbe427f | 93 | if (bHasHScrollbar) |
903f689b | 94 | { |
13289f04 VZ |
95 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); |
96 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, | |
f5368809 | 97 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), |
13289f04 VZ |
98 | GTK_FILL, |
99 | 0, 0); | |
100 | gtk_widget_show(hscrollbar); | |
101 | } | |
102 | ||
103 | // finally, put the vertical scrollbar in the upper right corner | |
104 | GtkWidget *vscrollbar = gtk_vscrollbar_new(GTK_TEXT(m_text)->vadj); | |
105 | gtk_table_attach(GTK_TABLE(m_widget), vscrollbar, 1, 2, 0, 1, | |
106 | GTK_FILL, | |
f5368809 | 107 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), |
13289f04 | 108 | 0, 0); |
903f689b | 109 | gtk_widget_show( vscrollbar ); |
13289f04 | 110 | } |
8bbe427f | 111 | else |
903f689b | 112 | { |
13289f04 VZ |
113 | // a single-line text control: no need for scrollbars |
114 | m_widget = | |
115 | m_text = gtk_entry_new(); | |
116 | } | |
484e45bf | 117 | |
c801d85f KB |
118 | wxSize newSize = size; |
119 | if (newSize.x == -1) newSize.x = 80; | |
120 | if (newSize.y == -1) newSize.y = 26; | |
121 | SetSize( newSize.x, newSize.y ); | |
484e45bf | 122 | |
6ca41e57 RR |
123 | m_parent->AddChild( this ); |
124 | ||
125 | (m_parent->m_insertCallback)( m_parent, this ); | |
8bbe427f | 126 | |
c801d85f | 127 | PostCreation(); |
484e45bf | 128 | |
8bbe427f | 129 | if (multi_line) |
903f689b | 130 | { |
13289f04 VZ |
131 | gtk_widget_realize(m_text); |
132 | gtk_widget_show(m_text); | |
133 | } | |
134 | ||
484e45bf | 135 | // we want to be notified about text changes |
13289f04 | 136 | gtk_signal_connect(GTK_OBJECT(m_text), "changed", |
484e45bf VZ |
137 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), |
138 | (gpointer)this); | |
139 | ||
7f4dc78d RR |
140 | if (!value.IsNull()) |
141 | { | |
142 | gint tmp = 0; | |
13289f04 | 143 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); |
6ca41e57 | 144 | SetInsertionPointEnd(); |
6de97a3b | 145 | } |
484e45bf | 146 | |
ec758a20 | 147 | if (style & wxTE_PASSWORD) |
112892b9 | 148 | { |
ec758a20 RR |
149 | if (!multi_line) |
150 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
151 | } | |
8bbe427f | 152 | |
ec758a20 | 153 | if (style & wxTE_READONLY) |
8bbe427f | 154 | { |
ec758a20 RR |
155 | if (!multi_line) |
156 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
112892b9 RR |
157 | } |
158 | else | |
159 | { | |
ec758a20 | 160 | if (multi_line) |
13289f04 | 161 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); |
6de97a3b | 162 | } |
484e45bf | 163 | |
c801d85f | 164 | Show( TRUE ); |
484e45bf | 165 | |
f96aa4d9 | 166 | SetBackgroundColour( parent->GetBackgroundColour() ); |
58614078 | 167 | SetForegroundColour( parent->GetForegroundColour() ); |
f96aa4d9 | 168 | |
c801d85f | 169 | return TRUE; |
6de97a3b | 170 | } |
c801d85f | 171 | |
03f38c58 | 172 | wxString wxTextCtrl::GetValue() const |
c801d85f | 173 | { |
a81258be | 174 | wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" ); |
8bbe427f | 175 | |
c801d85f KB |
176 | wxString tmp; |
177 | if (m_windowStyle & wxTE_MULTILINE) | |
178 | { | |
13289f04 | 179 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
a81258be RR |
180 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); |
181 | tmp = text; | |
182 | g_free( text ); | |
c801d85f KB |
183 | } |
184 | else | |
185 | { | |
13289f04 | 186 | tmp = gtk_entry_get_text( GTK_ENTRY(m_text) ); |
6de97a3b | 187 | } |
c801d85f | 188 | return tmp; |
6de97a3b | 189 | } |
c801d85f KB |
190 | |
191 | void wxTextCtrl::SetValue( const wxString &value ) | |
192 | { | |
f96aa4d9 | 193 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 194 | |
c801d85f KB |
195 | wxString tmp = ""; |
196 | if (!value.IsNull()) tmp = value; | |
197 | if (m_windowStyle & wxTE_MULTILINE) | |
198 | { | |
13289f04 VZ |
199 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
200 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
c801d85f | 201 | len = 0; |
13289f04 | 202 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len ); |
c801d85f KB |
203 | } |
204 | else | |
205 | { | |
13289f04 | 206 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp ); |
6de97a3b RR |
207 | } |
208 | } | |
c801d85f KB |
209 | |
210 | void wxTextCtrl::WriteText( const wxString &text ) | |
211 | { | |
f96aa4d9 | 212 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 213 | |
c801d85f | 214 | if (text.IsNull()) return; |
484e45bf | 215 | |
c801d85f KB |
216 | if (m_windowStyle & wxTE_MULTILINE) |
217 | { | |
13289f04 VZ |
218 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
219 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); | |
c801d85f KB |
220 | } |
221 | else | |
222 | { | |
13289f04 | 223 | gtk_entry_append_text( GTK_ENTRY(m_text), text ); |
6de97a3b RR |
224 | } |
225 | } | |
c801d85f | 226 | |
a81258be | 227 | bool wxTextCtrl::LoadFile( const wxString &file ) |
c801d85f | 228 | { |
a81258be | 229 | wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" ); |
8bbe427f | 230 | |
a81258be | 231 | if (!wxFileExists(file)) return FALSE; |
2ad3a34e | 232 | |
a81258be RR |
233 | Clear(); |
234 | ||
235 | FILE *fp = NULL; | |
236 | struct stat statb; | |
8bbe427f | 237 | |
a81258be RR |
238 | if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || |
239 | !(fp = fopen ((char*) (const char*) file, "r"))) | |
240 | { | |
241 | return FALSE; | |
242 | } | |
243 | else | |
244 | { | |
245 | gint len = statb.st_size; | |
246 | char *text; | |
247 | if (!(text = (char*)malloc ((unsigned) (len + 1)))) | |
248 | { | |
249 | fclose (fp); | |
250 | return FALSE; | |
251 | } | |
252 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
253 | { | |
254 | } | |
255 | fclose (fp); | |
256 | ||
257 | text[len] = 0; | |
8bbe427f | 258 | |
a81258be RR |
259 | if (m_windowStyle & wxTE_MULTILINE) |
260 | { | |
261 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, 0, &len ); | |
262 | } | |
263 | else | |
264 | { | |
265 | gtk_entry_set_text( GTK_ENTRY(m_text), text ); | |
266 | } | |
8bbe427f | 267 | |
a81258be RR |
268 | free (text); |
269 | m_modified = FALSE; | |
270 | return TRUE; | |
271 | } | |
112892b9 | 272 | return FALSE; |
6de97a3b | 273 | } |
c801d85f | 274 | |
a81258be | 275 | bool wxTextCtrl::SaveFile( const wxString &file ) |
c801d85f | 276 | { |
a81258be | 277 | wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" ); |
8bbe427f | 278 | |
a81258be | 279 | if (file == "") return FALSE; |
8bbe427f | 280 | |
a81258be | 281 | FILE *fp; |
2ad3a34e | 282 | |
a81258be RR |
283 | if (!(fp = fopen ((char*) (const char*) file, "w"))) |
284 | { | |
285 | return FALSE; | |
286 | } | |
287 | else | |
288 | { | |
289 | char *text = NULL; | |
290 | gint len = 0; | |
8bbe427f | 291 | |
a81258be RR |
292 | if (m_windowStyle & wxTE_MULTILINE) |
293 | { | |
294 | len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
295 | text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
296 | } | |
297 | else | |
298 | { | |
299 | text = gtk_entry_get_text( GTK_ENTRY(m_text) ); | |
300 | } | |
8bbe427f | 301 | |
a81258be RR |
302 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) |
303 | { | |
304 | // Did not write whole file | |
305 | } | |
8bbe427f | 306 | |
a81258be RR |
307 | // Make sure newline terminates the file |
308 | if (text[len - 1] != '\n') | |
309 | fputc ('\n', fp); | |
310 | ||
311 | fclose (fp); | |
8bbe427f | 312 | |
a81258be | 313 | if (m_windowStyle & wxTE_MULTILINE) g_free( text ); |
8bbe427f | 314 | |
a81258be RR |
315 | m_modified = FALSE; |
316 | return TRUE; | |
317 | } | |
318 | ||
319 | return TRUE; | |
6de97a3b | 320 | } |
c801d85f | 321 | |
debe6624 | 322 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
c801d85f | 323 | { |
a81258be RR |
324 | if (m_windowStyle & wxTE_MULTILINE) |
325 | { | |
326 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
327 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
328 | ||
329 | if (text) | |
330 | { | |
331 | wxString buf(""); | |
332 | long i; | |
333 | int currentLine = 0; | |
334 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
335 | if (text[i] == '\n') | |
336 | currentLine++; | |
337 | // Now get the text | |
338 | int j; | |
339 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
340 | buf += text[i]; | |
8bbe427f | 341 | |
a81258be RR |
342 | g_free( text ); |
343 | return buf; | |
344 | } | |
345 | else | |
346 | return wxEmptyString; | |
347 | } | |
348 | else | |
349 | { | |
350 | if (lineNo == 0) return GetValue(); | |
351 | return wxEmptyString; | |
352 | } | |
6de97a3b | 353 | } |
c801d85f | 354 | |
a81258be RR |
355 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
356 | { | |
c67d8618 | 357 | wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" ); |
a81258be | 358 | } |
112892b9 | 359 | |
e3ca08dd | 360 | long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 361 | { |
e3ca08dd MR |
362 | if (!(m_windowStyle & wxTE_MULTILINE)) |
363 | return 0; | |
364 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
365 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
366 | if(!text) | |
367 | return 0; | |
368 | if( pos >= len) | |
369 | return pos=len-1; | |
8bbe427f | 370 | |
e3ca08dd MR |
371 | *x=1; // Col 1 |
372 | *y=1; // Line 1 | |
373 | for (int i = 0; i < pos; i++ ) | |
374 | { | |
375 | if (text[i] == '\n') | |
376 | { | |
377 | (*y)++; | |
378 | *x=1; | |
379 | } | |
380 | else | |
381 | (*x)++; | |
8bbe427f | 382 | } |
e3ca08dd MR |
383 | g_free( text ); |
384 | return 1; | |
6de97a3b | 385 | } |
c801d85f | 386 | |
e3ca08dd | 387 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 388 | { |
e3ca08dd MR |
389 | if (!(m_windowStyle & wxTE_MULTILINE)) |
390 | return 0; | |
391 | long pos=0; | |
8bbe427f | 392 | |
e3ca08dd MR |
393 | for(int i=1;i<y;i++) |
394 | pos +=GetLineLength(i); | |
395 | pos +=x-1; // Pos start with 0 | |
396 | return pos; | |
6de97a3b | 397 | } |
c801d85f | 398 | |
a81258be | 399 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 400 | { |
a81258be RR |
401 | wxString str = GetLineText (lineNo); |
402 | return (int) str.Length(); | |
6de97a3b | 403 | } |
c801d85f | 404 | |
a81258be | 405 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 406 | { |
a81258be RR |
407 | if (m_windowStyle & wxTE_MULTILINE) |
408 | { | |
409 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
410 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
411 | ||
412 | if (text) | |
413 | { | |
414 | int currentLine = 0; | |
415 | for (int i = 0; i < len; i++ ) | |
416 | if (text[i] == '\n') | |
417 | currentLine++; | |
8bbe427f | 418 | |
a81258be RR |
419 | g_free( text ); |
420 | return currentLine; | |
421 | } | |
422 | else | |
423 | return 0; | |
424 | } | |
425 | else | |
426 | { | |
427 | return 1; | |
428 | } | |
6de97a3b | 429 | } |
c801d85f | 430 | |
debe6624 | 431 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 432 | { |
a81258be | 433 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 434 | |
c801d85f KB |
435 | int tmp = (int) pos; |
436 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 437 | gtk_text_set_point( GTK_TEXT(m_text), tmp ); |
c801d85f | 438 | else |
13289f04 | 439 | gtk_entry_set_position( GTK_ENTRY(m_text), tmp ); |
6de97a3b | 440 | } |
c801d85f | 441 | |
03f38c58 | 442 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 443 | { |
f96aa4d9 | 444 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 445 | |
c801d85f KB |
446 | int pos = 0; |
447 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 448 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
c801d85f | 449 | else |
13289f04 | 450 | pos = GTK_ENTRY(m_text)->text_length; |
605c9c83 | 451 | SetInsertionPoint((pos-1)>0 ? (pos-1):0); |
6de97a3b | 452 | } |
c801d85f | 453 | |
debe6624 | 454 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 455 | { |
f96aa4d9 | 456 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 457 | |
c801d85f | 458 | if (m_windowStyle & wxTE_MULTILINE) |
13289f04 | 459 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); |
c801d85f | 460 | else |
13289f04 | 461 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); |
6de97a3b | 462 | } |
c801d85f | 463 | |
debe6624 | 464 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 465 | { |
f96aa4d9 | 466 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 467 | |
13289f04 | 468 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 469 | } |
c801d85f | 470 | |
debe6624 | 471 | void wxTextCtrl::ShowPosition( long WXUNUSED(pos) ) |
c801d85f | 472 | { |
f96aa4d9 | 473 | wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" ); |
6de97a3b | 474 | } |
c801d85f | 475 | |
03f38c58 | 476 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 477 | { |
f96aa4d9 | 478 | wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" ); |
8bbe427f | 479 | |
13289f04 | 480 | return (long) GTK_EDITABLE(m_text)->current_pos; |
6de97a3b | 481 | } |
c801d85f | 482 | |
03f38c58 | 483 | long wxTextCtrl::GetLastPosition() const |
c801d85f | 484 | { |
f96aa4d9 | 485 | wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" ); |
8bbe427f | 486 | |
c801d85f KB |
487 | int pos = 0; |
488 | if (m_windowStyle & wxTE_MULTILINE) | |
13289f04 | 489 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
c801d85f | 490 | else |
13289f04 | 491 | pos = GTK_ENTRY(m_text)->text_length; |
c801d85f | 492 | return (long)pos-1; |
6de97a3b | 493 | } |
c801d85f | 494 | |
debe6624 | 495 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 496 | { |
f96aa4d9 | 497 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 498 | |
13289f04 | 499 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 500 | } |
c801d85f | 501 | |
debe6624 | 502 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 503 | { |
f96aa4d9 | 504 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 505 | |
13289f04 | 506 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
c801d85f KB |
507 | if (value.IsNull()) return; |
508 | gint pos = (gint)to; | |
13289f04 | 509 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
6de97a3b | 510 | } |
c801d85f | 511 | |
03f38c58 | 512 | void wxTextCtrl::Cut() |
c801d85f | 513 | { |
f96aa4d9 | 514 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 515 | |
75ed1d15 GL |
516 | #if (GTK_MINOR_VERSION == 1) |
517 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) ); | |
518 | #else | |
13289f04 | 519 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 520 | #endif |
6de97a3b | 521 | } |
c801d85f | 522 | |
03f38c58 | 523 | void wxTextCtrl::Copy() |
c801d85f | 524 | { |
f96aa4d9 | 525 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 526 | |
75ed1d15 GL |
527 | #if (GTK_MINOR_VERSION == 1) |
528 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) ); | |
529 | #else | |
13289f04 | 530 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 531 | #endif |
6de97a3b | 532 | } |
c801d85f | 533 | |
03f38c58 | 534 | void wxTextCtrl::Paste() |
c801d85f | 535 | { |
f96aa4d9 | 536 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 537 | |
75ed1d15 GL |
538 | #if (GTK_MINOR_VERSION == 1) |
539 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) ); | |
540 | #else | |
13289f04 | 541 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 542 | #endif |
6de97a3b | 543 | } |
c801d85f | 544 | |
03f38c58 | 545 | void wxTextCtrl::Clear() |
c801d85f KB |
546 | { |
547 | SetValue( "" ); | |
6de97a3b | 548 | } |
c801d85f | 549 | |
903f689b | 550 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 551 | { |
903f689b RR |
552 | if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
553 | { | |
554 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
555 | event.SetEventObject(this); | |
903f689b RR |
556 | if (GetEventHandler()->ProcessEvent(event)) return; |
557 | } | |
8bbe427f | 558 | else if (key_event.KeyCode() == WXK_TAB) |
903f689b RR |
559 | { |
560 | wxNavigationKeyEvent event; | |
561 | event.SetDirection( key_event.m_shiftDown ); | |
562 | event.SetWindowChange(FALSE); | |
563 | event.SetEventObject(this); | |
564 | ||
565 | if (GetEventHandler()->ProcessEvent(event)) return; | |
566 | } | |
567 | key_event.Skip(); | |
6de97a3b | 568 | } |
c801d85f | 569 | |
46dc76ba | 570 | int wxTextCtrl::overflow( int WXUNUSED(c) ) |
c801d85f | 571 | { |
c801d85f KB |
572 | int len = pptr() - pbase(); |
573 | char *txt = new char[len+1]; | |
574 | strncpy(txt, pbase(), len); | |
575 | txt[len] = '\0'; | |
576 | (*this) << txt; | |
577 | setp(pbase(), epptr()); | |
578 | delete[] txt; | |
579 | return EOF; | |
6de97a3b | 580 | } |
c801d85f | 581 | |
03f38c58 | 582 | int wxTextCtrl::sync() |
c801d85f | 583 | { |
c801d85f KB |
584 | int len = pptr() - pbase(); |
585 | char *txt = new char[len+1]; | |
586 | strncpy(txt, pbase(), len); | |
587 | txt[len] = '\0'; | |
588 | (*this) << txt; | |
589 | setp(pbase(), epptr()); | |
590 | delete[] txt; | |
591 | return 0; | |
6de97a3b | 592 | } |
c801d85f | 593 | |
03f38c58 | 594 | int wxTextCtrl::underflow() |
c801d85f KB |
595 | { |
596 | return EOF; | |
6de97a3b | 597 | } |
c801d85f KB |
598 | |
599 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
600 | { | |
601 | WriteText(s); | |
602 | return *this; | |
603 | } | |
604 | ||
debe6624 | 605 | wxTextCtrl& wxTextCtrl::operator<<(float f) |
c801d85f KB |
606 | { |
607 | static char buf[100]; | |
608 | sprintf(buf, "%.2f", f); | |
609 | WriteText(buf); | |
610 | return *this; | |
611 | } | |
612 | ||
debe6624 | 613 | wxTextCtrl& wxTextCtrl::operator<<(double d) |
c801d85f KB |
614 | { |
615 | static char buf[100]; | |
616 | sprintf(buf, "%.2f", d); | |
617 | WriteText(buf); | |
618 | return *this; | |
619 | } | |
620 | ||
debe6624 | 621 | wxTextCtrl& wxTextCtrl::operator<<(int i) |
c801d85f KB |
622 | { |
623 | static char buf[100]; | |
624 | sprintf(buf, "%i", i); | |
625 | WriteText(buf); | |
626 | return *this; | |
627 | } | |
628 | ||
debe6624 | 629 | wxTextCtrl& wxTextCtrl::operator<<(long i) |
c801d85f KB |
630 | { |
631 | static char buf[100]; | |
632 | sprintf(buf, "%ld", i); | |
633 | WriteText(buf); | |
634 | return *this; | |
635 | } | |
636 | ||
637 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
638 | { | |
639 | char buf[2]; | |
640 | ||
641 | buf[0] = c; | |
642 | buf[1] = 0; | |
643 | WriteText(buf); | |
644 | return *this; | |
645 | } | |
646 | ||
03f38c58 | 647 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 648 | { |
ae0bdb01 | 649 | return GTK_WIDGET(m_text); |
6de97a3b | 650 | } |
e3e65dac | 651 | |
903f689b RR |
652 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
653 | { | |
ae0bdb01 RR |
654 | if (m_windowStyle & wxTE_MULTILINE) |
655 | return (window == GTK_TEXT(m_text)->text_area); | |
656 | else | |
657 | return (window == GTK_ENTRY(m_text)->text_area); | |
903f689b | 658 | } |
e3e65dac | 659 | |
58614078 | 660 | void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) ) |
868a2826 | 661 | { |
ae0bdb01 | 662 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 663 | |
ae0bdb01 | 664 | // doesn't work |
58614078 RR |
665 | } |
666 | ||
667 | void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) ) | |
668 | { | |
ae0bdb01 | 669 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 670 | |
ae0bdb01 | 671 | // doesn't work |
868a2826 | 672 | } |
e3e65dac | 673 | |
68dda785 VZ |
674 | void wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
675 | { | |
ae0bdb01 | 676 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
a81258be | 677 | |
ae0bdb01 | 678 | wxControl::SetBackgroundColour( colour ); |
8bbe427f | 679 | |
ae0bdb01 RR |
680 | wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ); |
681 | if (sysbg.Red() == colour.Red() && | |
682 | sysbg.Green() == colour.Green() && | |
683 | sysbg.Blue() == colour.Blue()) | |
684 | { | |
685 | return; | |
686 | } | |
687 | ||
688 | if (!m_backgroundColour.Ok()) return; | |
8bbe427f | 689 | |
ae0bdb01 RR |
690 | if (m_windowStyle & wxTE_MULTILINE) |
691 | { | |
692 | GdkWindow *window = GTK_TEXT(m_text)->text_area; | |
693 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); | |
694 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
695 | gdk_window_clear( window ); | |
696 | } | |
58614078 RR |
697 | } |
698 | ||
699 | void wxTextCtrl::ApplyWidgetStyle() | |
700 | { | |
ae0bdb01 RR |
701 | if (m_windowStyle & wxTE_MULTILINE) |
702 | { | |
703 | } | |
704 | else | |
705 | { | |
706 | SetWidgetStyle(); | |
707 | gtk_widget_set_style( m_text, m_widgetStyle ); | |
708 | } | |
68dda785 | 709 | } |
f96aa4d9 | 710 |