]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
added FreeBSD
[wxWidgets.git] / src / gtk / textctrl.cpp
CommitLineData
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
83624f79
RR
23#include "gdk/gdk.h"
24#include "gtk/gtk.h"
b292e2f5
RR
25#include "gdk/gdkkeysyms.h"
26
27//-----------------------------------------------------------------------------
28// data
29//-----------------------------------------------------------------------------
30
31extern bool g_blockEventsOnDrag;
32
c801d85f 33//-----------------------------------------------------------------------------
2f2aa628 34// "changed"
c801d85f
KB
35//-----------------------------------------------------------------------------
36
805dd538 37static void
2830bf19 38gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 39{
2830bf19 40 win->SetModified();
805dd538 41
2830bf19 42 win->CalculateScrollbar();
8bbe427f 43
2830bf19 44 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
0c77152e 45 event.SetString( copystring(win->GetValue()) );
2830bf19
RR
46 event.SetEventObject( win );
47 win->GetEventHandler()->ProcessEvent( event );
0c77152e 48 delete[] event.GetString();
6de97a3b 49}
112892b9 50
2830bf19
RR
51//-----------------------------------------------------------------------------
52// "size_allocate"
53//-----------------------------------------------------------------------------
54
805dd538 55static void
2830bf19
RR
56gtk_text_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* WXUNUSED(alloc), wxTextCtrl *win )
57{
58 win->CalculateScrollbar();
59}
60
2f2aa628
RR
61//-----------------------------------------------------------------------------
62// wxTextCtrl
63//-----------------------------------------------------------------------------
64
65IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
66
c801d85f 67BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 68 EVT_CHAR(wxTextCtrl::OnChar)
c801d85f
KB
69END_EVENT_TABLE()
70
f5abe911 71#ifndef NO_TEXT_WINDOW_STREAM
03f38c58 72wxTextCtrl::wxTextCtrl() : streambuf()
c801d85f 73{
2830bf19 74 if (allocate()) setp(base(),ebuf());
13289f04 75
2830bf19 76 m_modified = FALSE;
6de97a3b 77}
f5abe911
RR
78#else
79wxTextCtrl::wxTextCtrl()
80{
81 m_modified = FALSE;
82}
83#endif
c801d85f 84
f5abe911 85#ifndef NO_TEXT_WINDOW_STREAM
debe6624 86wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 87 const wxPoint &pos, const wxSize &size,
6de97a3b 88 int style, const wxValidator& validator, const wxString &name ) : streambuf()
c801d85f 89{
2830bf19 90 if (allocate()) setp(base(),ebuf());
13289f04 91
2830bf19
RR
92 m_modified = FALSE;
93 Create( parent, id, value, pos, size, style, validator, name );
6de97a3b 94}
f5abe911
RR
95#else
96wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
97 const wxPoint &pos, const wxSize &size,
98 int style, const wxValidator& validator, const wxString &name )
99{
100 m_modified = FALSE;
101 Create( parent, id, value, pos, size, style, validator, name );
102}
103#endif
c801d85f 104
debe6624 105bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 106 const wxPoint &pos, const wxSize &size,
6de97a3b 107 int style, const wxValidator& validator, const wxString &name )
c801d85f 108{
2830bf19 109 m_needParent = TRUE;
b292e2f5 110 m_acceptsFocus = TRUE;
484e45bf 111
2830bf19 112 PreCreation( parent, id, pos, size, style, name );
6de97a3b 113
2830bf19 114 SetValidator( validator );
805dd538 115
2830bf19 116 m_vScrollbarVisible = TRUE;
13289f04 117
2830bf19
RR
118 bool multi_line = (style & wxTE_MULTILINE) != 0;
119 if ( multi_line )
120 {
121 // a multi-line edit control: create a vertical scrollbar by default and
122 // horizontal if requested
123 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
805dd538 124
2830bf19
RR
125 // create our control...
126 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
127
128 // ... and put into the upper left hand corner of the table
129 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
130 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 131 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
132 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
133 0, 0);
13289f04 134
2830bf19
RR
135 // put the horizontal scrollbar in the lower left hand corner
136 if (bHasHScrollbar)
137 {
138 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
139 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
f5368809 140 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
13289f04
VZ
141 GTK_FILL,
142 0, 0);
2830bf19
RR
143 gtk_widget_show(hscrollbar);
144 }
13289f04 145
2830bf19
RR
146 // finally, put the vertical scrollbar in the upper right corner
147 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
148 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
13289f04 149 GTK_FILL,
f5368809 150 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
13289f04 151 0, 0);
2830bf19 152 gtk_widget_show( m_vScrollbar );
805dd538 153
2830bf19
RR
154 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
155 GTK_SIGNAL_FUNC(gtk_text_size_callback), (gpointer)this );
156 }
157 else
158 {
159 // a single-line text control: no need for scrollbars
160 m_widget =
161 m_text = gtk_entry_new();
162 }
484e45bf 163
2830bf19
RR
164 wxSize newSize = size;
165 if (newSize.x == -1) newSize.x = 80;
166 if (newSize.y == -1) newSize.y = 26;
167 SetSize( newSize.x, newSize.y );
484e45bf 168
2830bf19 169 m_parent->AddChild( this );
6ca41e57 170
2830bf19 171 (m_parent->m_insertCallback)( m_parent, this );
8bbe427f 172
2830bf19 173 PostCreation();
484e45bf 174
2830bf19
RR
175 if (multi_line)
176 {
177 gtk_widget_realize(m_text);
178 gtk_widget_show(m_text);
179 }
13289f04 180
2830bf19 181 // we want to be notified about text changes
b292e2f5
RR
182 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
183 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
484e45bf 184
291a8f20 185 if (!value.IsEmpty())
2830bf19
RR
186 {
187 gint tmp = 0;
188 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
291a8f20
RR
189
190 if (multi_line)
191 {
192 /* bring editable's cursor uptodate. bug in GTK. */
193
194 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
195 }
2830bf19 196 }
484e45bf 197
2830bf19
RR
198 if (style & wxTE_PASSWORD)
199 {
200 if (!multi_line)
201 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
202 }
8bbe427f 203
2830bf19
RR
204 if (style & wxTE_READONLY)
205 {
206 if (!multi_line)
207 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
208 }
209 else
210 {
211 if (multi_line)
212 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
213 }
805dd538 214
2830bf19 215 Show( TRUE );
805dd538 216
2830bf19
RR
217 SetBackgroundColour( parent->GetBackgroundColour() );
218 SetForegroundColour( parent->GetForegroundColour() );
484e45bf 219
2830bf19
RR
220 return TRUE;
221}
484e45bf 222
2830bf19
RR
223void wxTextCtrl::CalculateScrollbar()
224{
225 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
f96aa4d9 226
2830bf19 227 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
805dd538 228
2830bf19
RR
229 if (adj->upper - adj->page_size < 0.8)
230 {
231 if (m_vScrollbarVisible)
232 {
805dd538
VZ
233 gtk_widget_hide( m_vScrollbar );
234
235 m_vScrollbarVisible = FALSE;
2830bf19
RR
236 }
237 }
238 else
239 {
240 if (!m_vScrollbarVisible)
241 {
805dd538
VZ
242 gtk_widget_show( m_vScrollbar );
243
244 m_vScrollbarVisible = TRUE;
2830bf19
RR
245 }
246 }
6de97a3b 247}
c801d85f 248
03f38c58 249wxString wxTextCtrl::GetValue() const
c801d85f 250{
2830bf19 251 wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" );
8bbe427f 252
2830bf19
RR
253 wxString tmp;
254 if (m_windowStyle & wxTE_MULTILINE)
255 {
256 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
257 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
258 tmp = text;
259 g_free( text );
260 }
261 else
262 {
263 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
264 }
265 return tmp;
6de97a3b 266}
c801d85f
KB
267
268void wxTextCtrl::SetValue( const wxString &value )
269{
2830bf19 270 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 271
2830bf19
RR
272 wxString tmp = "";
273 if (!value.IsNull()) tmp = value;
274 if (m_windowStyle & wxTE_MULTILINE)
275 {
276 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
277 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
278 len = 0;
279 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
280 }
281 else
282 {
283 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
284 }
6de97a3b 285}
c801d85f
KB
286
287void wxTextCtrl::WriteText( const wxString &text )
288{
2830bf19 289 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 290
2830bf19 291 if (text.IsNull()) return;
484e45bf 292
2830bf19
RR
293 if (m_windowStyle & wxTE_MULTILINE)
294 {
291a8f20 295 /* this moves the cursor pos to behind the inserted text */
ac0d36b5 296 gint len = GTK_EDITABLE(m_text)->current_pos;
2830bf19 297 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
291a8f20
RR
298
299 /* bring editable's cursor uptodate. bug in GTK. */
291a8f20 300 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
2830bf19
RR
301 }
302 else
303 {
c46554be
RR
304 /* this moves the cursor pos to behind the inserted text */
305 gint len = GTK_EDITABLE(m_text)->current_pos;
306 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
307
308 /* bring editable's cursor uptodate. bug in GTK. */
309 GTK_EDITABLE(m_text)->current_pos += text.Len();
310
311 /* bring entry's cursor uptodate. bug in GTK. */
312 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
2830bf19 313 }
6de97a3b 314}
c801d85f 315
a6e21573
HH
316void wxTextCtrl::AppendText( const wxString &text )
317{
318 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
319
320 if (m_windowStyle & wxTE_MULTILINE)
321 {
322 /* we'll insert at the last position */
323 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
324 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
c46554be
RR
325
326 /* bring editable's cursor uptodate. bug in GTK. */
a6e21573
HH
327 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
328 }
329 else
330 {
331 gtk_entry_append_text( GTK_ENTRY(m_text), text );
332 }
333}
334
a81258be 335bool wxTextCtrl::LoadFile( const wxString &file )
c801d85f 336{
a81258be 337 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
8bbe427f 338
a81258be 339 if (!wxFileExists(file)) return FALSE;
2ad3a34e 340
a81258be
RR
341 Clear();
342
bbe0af5b 343 FILE *fp = (FILE*) NULL;
a81258be 344 struct stat statb;
8bbe427f 345
a81258be
RR
346 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
347 !(fp = fopen ((char*) (const char*) file, "r")))
348 {
349 return FALSE;
350 }
351 else
352 {
353 gint len = statb.st_size;
354 char *text;
355 if (!(text = (char*)malloc ((unsigned) (len + 1))))
356 {
357 fclose (fp);
358 return FALSE;
359 }
360 if (fread (text, sizeof (char), len, fp) != (size_t) len)
805dd538
VZ
361 {
362 }
a81258be
RR
363 fclose (fp);
364
365 text[len] = 0;
8bbe427f 366
a81258be
RR
367 if (m_windowStyle & wxTE_MULTILINE)
368 {
435fe83e
RR
369 gint pos = 0;
370 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos );
a81258be
RR
371 }
372 else
373 {
374 gtk_entry_set_text( GTK_ENTRY(m_text), text );
375 }
8bbe427f 376
a81258be
RR
377 free (text);
378 m_modified = FALSE;
379 return TRUE;
380 }
112892b9 381 return FALSE;
6de97a3b 382}
c801d85f 383
a81258be 384bool wxTextCtrl::SaveFile( const wxString &file )
c801d85f 385{
a81258be 386 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
8bbe427f 387
a81258be 388 if (file == "") return FALSE;
8bbe427f 389
a81258be 390 FILE *fp;
2ad3a34e 391
a81258be
RR
392 if (!(fp = fopen ((char*) (const char*) file, "w")))
393 {
394 return FALSE;
395 }
396 else
397 {
bbe0af5b 398 char *text = (char*) NULL;
a81258be 399 gint len = 0;
8bbe427f 400
a81258be
RR
401 if (m_windowStyle & wxTE_MULTILINE)
402 {
403 len = gtk_text_get_length( GTK_TEXT(m_text) );
404 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
405 }
406 else
407 {
408 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
409 }
8bbe427f 410
a81258be 411 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
96385642
VZ
412 {
413 // Did not write whole file
414 }
8bbe427f 415
a81258be
RR
416 // Make sure newline terminates the file
417 if (text[len - 1] != '\n')
96385642 418 fputc ('\n', fp);
a81258be
RR
419
420 fclose (fp);
8bbe427f 421
a81258be 422 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
8bbe427f 423
a81258be
RR
424 m_modified = FALSE;
425 return TRUE;
426 }
427
428 return TRUE;
6de97a3b 429}
c801d85f 430
debe6624 431wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f 432{
a81258be
RR
433 if (m_windowStyle & wxTE_MULTILINE)
434 {
435 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
436 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
437
438 if (text)
439 {
440 wxString buf("");
441 long i;
442 int currentLine = 0;
443 for (i = 0; currentLine != lineNo && text[i]; i++ )
444 if (text[i] == '\n')
445 currentLine++;
446 // Now get the text
447 int j;
448 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
449 buf += text[i];
8bbe427f 450
a81258be
RR
451 g_free( text );
452 return buf;
453 }
454 else
455 return wxEmptyString;
456 }
457 else
458 {
459 if (lineNo == 0) return GetValue();
460 return wxEmptyString;
461 }
6de97a3b 462}
c801d85f 463
a81258be
RR
464void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
465{
ac0d36b5
HH
466 /* If you implement this, don't forget to update the documentation!
467 * (file docs/latex/wx/text.tex) */
2830bf19 468 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
a81258be 469}
112892b9 470
e3ca08dd 471long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 472{
96385642 473 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 474 {
96385642
VZ
475 wxString text = GetValue();
476
6085b116 477 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 478 if( (unsigned long)pos > text.Len() )
96385642
VZ
479 return FALSE;
480
ac0d36b5
HH
481 *x=0; // First Col
482 *y=0; // First Line
2829d9e3 483
6085b116
VZ
484 const char* stop = text.c_str() + pos;
485 for ( const char *p = text.c_str(); p < stop; p++ )
96385642
VZ
486 {
487 if (*p == '\n')
488 {
489 (*y)++;
ac0d36b5 490 *x=0;
96385642
VZ
491 }
492 else
493 (*x)++;
494 }
805dd538 495 }
96385642
VZ
496 else // single line control
497 {
2829d9e3 498 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 499 {
ac0d36b5 500 *y = 0;
96385642
VZ
501 *x = pos;
502 }
503 else
504 {
505 // index out of bounds
506 return FALSE;
507 }
8bbe427f 508 }
96385642
VZ
509
510 return TRUE;
6de97a3b 511}
c801d85f 512
e3ca08dd 513long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 514{
2830bf19 515 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 516
2830bf19 517 long pos=0;
ac0d36b5 518 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 519
ac0d36b5 520 pos += x;
2830bf19 521 return pos;
6de97a3b 522}
c801d85f 523
a81258be 524int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 525{
a81258be
RR
526 wxString str = GetLineText (lineNo);
527 return (int) str.Length();
6de97a3b 528}
c801d85f 529
a81258be 530int wxTextCtrl::GetNumberOfLines() const
c801d85f 531{
2830bf19 532 if (m_windowStyle & wxTE_MULTILINE)
a81258be 533 {
2830bf19
RR
534 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
535 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
536
537 if (text)
538 {
539 int currentLine = 0;
540 for (int i = 0; i < len; i++ )
96385642 541 {
2830bf19 542 if (text[i] == '\n')
96385642
VZ
543 currentLine++;
544 }
2830bf19 545 g_free( text );
2829d9e3
VZ
546
547 // currentLine is 0 based, add 1 to get number of lines
548 return currentLine + 1;
2830bf19
RR
549 }
550 else
96385642 551 {
2830bf19 552 return 0;
96385642 553 }
a81258be
RR
554 }
555 else
2830bf19 556 {
96385642 557 return 1;
2830bf19 558 }
6de97a3b 559}
c801d85f 560
debe6624 561void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 562{
2830bf19 563 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
291a8f20
RR
564
565 if (m_windowStyle & wxTE_MULTILINE)
566 {
567 /* seems to be broken in GTK 1.0.X:
568 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
569
570 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
571 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
572
573 /* we fake a set_point by inserting and deleting. as the user
574 isn't supposed to get to know about thos non-sense, we
575 disconnect so that no events are sent to the user program. */
576
577 gint tmp = (gint)pos;
578 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
579 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
580
581 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
582 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
583
584 /* bring editable's cursor uptodate. another bug in GTK. */
585
586 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
ac0d36b5 587 }
2830bf19 588 else
291a8f20 589 {
d59051dd 590 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
c46554be
RR
591
592 /* bring editable's cursor uptodate. bug in GTK. */
593
594 GTK_EDITABLE(m_text)->current_pos = pos;
291a8f20 595 }
6de97a3b 596}
c801d85f 597
03f38c58 598void wxTextCtrl::SetInsertionPointEnd()
c801d85f 599{
2830bf19 600 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 601
d59051dd
VZ
602 if (m_windowStyle & wxTE_MULTILINE)
603 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
604 else
605 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
6de97a3b 606}
c801d85f 607
debe6624 608void wxTextCtrl::SetEditable( bool editable )
c801d85f 609{
2830bf19 610 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 611
2830bf19
RR
612 if (m_windowStyle & wxTE_MULTILINE)
613 gtk_text_set_editable( GTK_TEXT(m_text), editable );
614 else
615 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
6de97a3b 616}
c801d85f 617
debe6624 618void wxTextCtrl::SetSelection( long from, long to )
c801d85f 619{
2830bf19 620 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 621
2830bf19 622 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 623}
c801d85f 624
debe6624 625void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 626{
2830bf19 627 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
6de97a3b 628}
c801d85f 629
03f38c58 630long wxTextCtrl::GetInsertionPoint() const
c801d85f 631{
2830bf19 632 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
8bbe427f 633
2830bf19 634 return (long) GTK_EDITABLE(m_text)->current_pos;
6de97a3b 635}
c801d85f 636
03f38c58 637long wxTextCtrl::GetLastPosition() const
c801d85f 638{
2830bf19 639 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
8bbe427f 640
2830bf19
RR
641 int pos = 0;
642 if (m_windowStyle & wxTE_MULTILINE)
643 pos = gtk_text_get_length( GTK_TEXT(m_text) );
644 else
645 pos = GTK_ENTRY(m_text)->text_length;
805dd538 646
ac0d36b5 647 return (long)pos;
6de97a3b 648}
c801d85f 649
debe6624 650void wxTextCtrl::Remove( long from, long to )
c801d85f 651{
2830bf19 652 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 653
2830bf19 654 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 655}
c801d85f 656
debe6624 657void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 658{
2830bf19 659 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 660
2830bf19
RR
661 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
662 if (value.IsNull()) return;
435fe83e 663 gint pos = (gint)from;
2830bf19 664 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
6de97a3b 665}
c801d85f 666
03f38c58 667void wxTextCtrl::Cut()
c801d85f 668{
2830bf19 669 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 670
75ed1d15 671#if (GTK_MINOR_VERSION == 1)
2830bf19 672 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 673#else
2830bf19 674 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 675#endif
6de97a3b 676}
c801d85f 677
03f38c58 678void wxTextCtrl::Copy()
c801d85f 679{
2830bf19 680 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 681
75ed1d15 682#if (GTK_MINOR_VERSION == 1)
2830bf19 683 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 684#else
2830bf19 685 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 686#endif
6de97a3b 687}
c801d85f 688
03f38c58 689void wxTextCtrl::Paste()
c801d85f 690{
2830bf19 691 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 692
75ed1d15 693#if (GTK_MINOR_VERSION == 1)
2830bf19 694 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 695#else
2830bf19 696 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 697#endif
6de97a3b 698}
c801d85f 699
03f38c58 700void wxTextCtrl::Clear()
c801d85f 701{
2830bf19 702 SetValue( "" );
6de97a3b 703}
c801d85f 704
903f689b 705void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 706{
2830bf19 707 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
805dd538 708
2830bf19
RR
709 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
710 {
711 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
712 event.SetEventObject(this);
713 if (GetEventHandler()->ProcessEvent(event)) return;
714 }
903f689b 715
2830bf19 716 key_event.Skip();
6de97a3b 717}
c801d85f 718
f5abe911 719#ifndef NO_TEXT_WINDOW_STREAM
46dc76ba 720int wxTextCtrl::overflow( int WXUNUSED(c) )
c801d85f 721{
2830bf19
RR
722 int len = pptr() - pbase();
723 char *txt = new char[len+1];
724 strncpy(txt, pbase(), len);
725 txt[len] = '\0';
726 (*this) << txt;
727 setp(pbase(), epptr());
728 delete[] txt;
729 return EOF;
6de97a3b 730}
c801d85f 731
03f38c58 732int wxTextCtrl::sync()
c801d85f 733{
2830bf19
RR
734 int len = pptr() - pbase();
735 char *txt = new char[len+1];
736 strncpy(txt, pbase(), len);
737 txt[len] = '\0';
738 (*this) << txt;
739 setp(pbase(), epptr());
740 delete[] txt;
741 return 0;
6de97a3b 742}
c801d85f 743
03f38c58 744int wxTextCtrl::underflow()
c801d85f 745{
2830bf19 746 return EOF;
6de97a3b 747}
c801d85f
KB
748
749wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
750{
a6e21573 751 AppendText(s);
2830bf19 752 return *this;
c801d85f
KB
753}
754
debe6624 755wxTextCtrl& wxTextCtrl::operator<<(float f)
c801d85f 756{
2830bf19
RR
757 static char buf[100];
758 sprintf(buf, "%.2f", f);
a6e21573 759 AppendText(buf);
2830bf19 760 return *this;
c801d85f
KB
761}
762
debe6624 763wxTextCtrl& wxTextCtrl::operator<<(double d)
c801d85f 764{
2830bf19
RR
765 static char buf[100];
766 sprintf(buf, "%.2f", d);
a6e21573 767 AppendText(buf);
2830bf19 768 return *this;
c801d85f
KB
769}
770
debe6624 771wxTextCtrl& wxTextCtrl::operator<<(int i)
c801d85f 772{
2830bf19
RR
773 static char buf[100];
774 sprintf(buf, "%i", i);
a6e21573 775 AppendText(buf);
2830bf19 776 return *this;
c801d85f
KB
777}
778
debe6624 779wxTextCtrl& wxTextCtrl::operator<<(long i)
c801d85f 780{
2830bf19
RR
781 static char buf[100];
782 sprintf(buf, "%ld", i);
a6e21573 783 AppendText(buf);
2830bf19 784 return *this;
c801d85f
KB
785}
786
787wxTextCtrl& wxTextCtrl::operator<<(const char c)
788{
2830bf19 789 char buf[2];
c801d85f 790
2830bf19
RR
791 buf[0] = c;
792 buf[1] = 0;
a6e21573 793 AppendText(buf);
2830bf19 794 return *this;
c801d85f 795}
f5abe911 796#endif
c801d85f 797
03f38c58 798GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 799{
ae0bdb01 800 return GTK_WIDGET(m_text);
6de97a3b 801}
e3e65dac 802
903f689b
RR
803bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
804{
ae0bdb01
RR
805 if (m_windowStyle & wxTE_MULTILINE)
806 return (window == GTK_TEXT(m_text)->text_area);
807 else
808 return (window == GTK_ENTRY(m_text)->text_area);
903f689b 809}
e3e65dac 810
58614078 811void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
868a2826 812{
ae0bdb01 813 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 814
ae0bdb01 815 // doesn't work
58614078
RR
816}
817
818void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
819{
ae0bdb01 820 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
8bbe427f 821
ae0bdb01 822 // doesn't work
868a2826 823}
e3e65dac 824
68dda785
VZ
825void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
826{
ae0bdb01 827 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
a81258be 828
ae0bdb01 829 wxControl::SetBackgroundColour( colour );
8bbe427f 830
ae0bdb01 831 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
805dd538
VZ
832 if (sysbg.Red() == colour.Red() &&
833 sysbg.Green() == colour.Green() &&
ae0bdb01
RR
834 sysbg.Blue() == colour.Blue())
835 {
836 return;
805dd538
VZ
837 }
838
ae0bdb01 839 if (!m_backgroundColour.Ok()) return;
8bbe427f 840
ae0bdb01
RR
841 if (m_windowStyle & wxTE_MULTILINE)
842 {
843 GdkWindow *window = GTK_TEXT(m_text)->text_area;
844 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
845 gdk_window_set_background( window, m_backgroundColour.GetColor() );
846 gdk_window_clear( window );
847 }
58614078
RR
848}
849
850void wxTextCtrl::ApplyWidgetStyle()
851{
ae0bdb01
RR
852 if (m_windowStyle & wxTE_MULTILINE)
853 {
2830bf19 854 // how ?
805dd538 855 }
ae0bdb01
RR
856 else
857 {
858 SetWidgetStyle();
859 gtk_widget_set_style( m_text, m_widgetStyle );
860 }
68dda785 861}
f96aa4d9 862