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