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