]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
*** empty log message ***
[wxWidgets.git] / src / gtk1 / 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
65045edd
RR
38extern bool g_blockEventsOnDrag;
39extern wxCursor g_globalCursor;
b292e2f5 40
c801d85f 41//-----------------------------------------------------------------------------
2f2aa628 42// "changed"
c801d85f
KB
43//-----------------------------------------------------------------------------
44
805dd538 45static void
2830bf19 46gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 47{
a2053b27 48 if (!win->m_hasVMT) return;
805dd538 49
bb69661b 50 if (g_isIdle)
3c679789
RR
51 wxapp_install_idle_handler();
52
034be888 53 win->SetModified();
8bbe427f 54
f03fc89f 55 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
8a85884a 56 event.SetString( win->GetValue() );
2830bf19
RR
57 event.SetEventObject( win );
58 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 59}
112892b9 60
2830bf19 61//-----------------------------------------------------------------------------
034be888 62// "changed" from vertical scrollbar
2830bf19
RR
63//-----------------------------------------------------------------------------
64
805dd538 65static void
034be888 66gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
2830bf19 67{
3c679789 68 if (!win->m_hasVMT) return;
bb69661b
VZ
69
70 if (g_isIdle)
3c679789 71 wxapp_install_idle_handler();
acfd422a 72
2830bf19
RR
73 win->CalculateScrollbar();
74}
75
2f2aa628
RR
76//-----------------------------------------------------------------------------
77// wxTextCtrl
78//-----------------------------------------------------------------------------
79
80IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
81
c801d85f 82BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 83 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
84
85 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
86 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
87 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
88 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
89 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
90
91 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
92 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
93 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
94 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
95 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
c801d85f
KB
96END_EVENT_TABLE()
97
f5abe911
RR
98wxTextCtrl::wxTextCtrl()
99{
100 m_modified = FALSE;
101}
13289f04 102
f5abe911
RR
103wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
104 const wxPoint &pos, const wxSize &size,
105 int style, const wxValidator& validator, const wxString &name )
106{
107 m_modified = FALSE;
108 Create( parent, id, value, pos, size, style, validator, name );
109}
c801d85f 110
debe6624 111bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 112 const wxPoint &pos, const wxSize &size,
6de97a3b 113 int style, const wxValidator& validator, const wxString &name )
c801d85f 114{
2830bf19 115 m_needParent = TRUE;
b292e2f5 116 m_acceptsFocus = TRUE;
484e45bf 117
4dcaf11a
RR
118 if (!PreCreation( parent, pos, size ) ||
119 !CreateBase( parent, id, pos, size, style, validator, name ))
120 {
223d09f6 121 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
4dcaf11a
RR
122 return FALSE;
123 }
6de97a3b 124
805dd538 125
034be888 126 m_vScrollbarVisible = FALSE;
13289f04 127
2830bf19 128 bool multi_line = (style & wxTE_MULTILINE) != 0;
ab46dc18 129 if (multi_line)
2830bf19 130 {
7d6d2cd4 131#if (GTK_MINOR_VERSION > 2)
034be888
RR
132 /* a multi-line edit control: create a vertical scrollbar by default and
133 horizontal if requested */
2830bf19 134 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
7d6d2cd4
RR
135#else
136 bool bHasHScrollbar = FALSE;
137#endif
805dd538 138
034be888 139 /* create our control ... */
2830bf19
RR
140 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
141
034be888 142 /* ... and put into the upper left hand corner of the table */
2830bf19 143 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
5664fc32 144 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19 145 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 146 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
147 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
148 0, 0);
bb69661b 149
5c387335
RR
150 /* always wrap words */
151 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
bb69661b 152
7d6d2cd4 153#if (GTK_MINOR_VERSION > 2)
034be888 154 /* put the horizontal scrollbar in the lower left hand corner */
2830bf19
RR
155 if (bHasHScrollbar)
156 {
157 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
5664fc32 158 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
2830bf19 159 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
8ce63e9d 160 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
13289f04
VZ
161 GTK_FILL,
162 0, 0);
2830bf19 163 gtk_widget_show(hscrollbar);
13289f04 164
3358d36e
VZ
165 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
166 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
5c387335 167 }
7d6d2cd4 168#endif
bb69661b 169
ab46dc18
RR
170 /* finally, put the vertical scrollbar in the upper right corner */
171 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
172 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
ab46dc18
RR
173 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
174 GTK_FILL,
175 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
176 0, 0);
2830bf19
RR
177 }
178 else
179 {
034be888 180 /* a single-line text control: no need for scrollbars */
2830bf19
RR
181 m_widget =
182 m_text = gtk_entry_new();
183 }
484e45bf 184
f68586e5 185 SetSizeOrDefault( size );
484e45bf 186
f03fc89f 187 m_parent->DoAddChild( this );
8bbe427f 188
2830bf19 189 PostCreation();
484e45bf 190
2830bf19 191 if (multi_line)
2830bf19 192 gtk_widget_show(m_text);
13289f04 193
034be888 194 /* we want to be notified about text changes */
b292e2f5
RR
195 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
196 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
484e45bf 197
ab46dc18
RR
198 if (multi_line)
199 {
200 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
201 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
202 }
3358d36e 203
291a8f20 204 if (!value.IsEmpty())
2830bf19
RR
205 {
206 gint tmp = 0;
3358d36e
VZ
207
208#if GTK_MINOR_VERSION == 0
209 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
210 // gtk_editable_insert_text()
211 gtk_widget_realize(m_text);
212#endif // GTK 1.0
213
05939a81 214#if wxUSE_UNICODE
3358d36e 215 wxWX2MBbuf val = value.mbc_str();
05939a81 216 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
3358d36e 217#else // !Unicode
2830bf19 218 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
3358d36e
VZ
219#endif // Unicode/!Unicode
220
291a8f20
RR
221 if (multi_line)
222 {
3358d36e
VZ
223 /* bring editable's cursor uptodate. bug in GTK. */
224
225 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
226 }
2830bf19 227 }
484e45bf 228
2830bf19
RR
229 if (style & wxTE_PASSWORD)
230 {
231 if (!multi_line)
232 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
233 }
8bbe427f 234
2830bf19
RR
235 if (style & wxTE_READONLY)
236 {
237 if (!multi_line)
238 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
239 }
240 else
241 {
242 if (multi_line)
243 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
244 }
3358d36e 245
012a03e0 246 SetBackgroundColour( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) );
034be888 247 SetForegroundColour( parent->GetForegroundColour() );
805dd538 248
65045edd
RR
249 m_cursor = wxCursor( wxCURSOR_IBEAM );
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{
223d09f6 282 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("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{
223d09f6 301 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 302
223d09f6 303 wxString tmp = wxT("");
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{
223d09f6 325 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 365 wxCHECK_RET( m_text != NULL, wxT("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 {
223d09f6 413 wxString buf(wxT(""));
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) */
223d09f6 441 wxFAIL_MSG( wxT("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 {
223d09f6 460 if (*p == wxT('\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{
223d09f6 536 wxCHECK_RET( m_text != NULL, wxT("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
b02da6b1 567 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
291a8f20 568 }
6de97a3b 569}
c801d85f 570
03f38c58 571void wxTextCtrl::SetInsertionPointEnd()
c801d85f 572{
223d09f6 573 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 583 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 598 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 610 wxCHECK_MSG( m_text != NULL, 0, wxT("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{
223d09f6 617 wxCHECK_MSG( m_text != NULL, 0, wxT("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{
223d09f6 630 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 637 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 655 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 666 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 677 wxCHECK_RET( m_text != NULL, wxT("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
223d09f6 711 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
712}
713
714void wxTextCtrl::Redo()
715{
716 // TODO
223d09f6 717 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
718}
719
720bool wxTextCtrl::CanUndo() const
721{
722 // TODO
223d09f6 723 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
ca8b28f2
JS
724 return FALSE;
725}
726
727bool wxTextCtrl::CanRedo() const
728{
729 // TODO
223d09f6 730 wxFAIL_MSG( wxT("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{
223d09f6 738 wxCHECK_RET( m_text != NULL, wxT("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{
223d09f6 753 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
05060eeb
RR
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{
223d09f6 765 SetValue( wxT("") );
6de97a3b 766}
c801d85f 767
903f689b 768void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 769{
223d09f6 770 wxCHECK_RET( m_text != NULL, wxT("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
da048e3d
RR
779 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
780 {
781 wxWindow *top_frame = m_parent;
8487f887 782 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d
RR
783 top_frame = top_frame->GetParent();
784 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
785
786 if (window->default_widget)
787 {
788 gtk_widget_activate (window->default_widget);
789 return;
790 }
791 }
792
2830bf19 793 key_event.Skip();
6de97a3b 794}
c801d85f 795
03f38c58 796GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 797{
ae0bdb01 798 return GTK_WIDGET(m_text);
6de97a3b 799}
e3e65dac 800
903f689b
RR
801bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
802{
ae0bdb01
RR
803 if (m_windowStyle & wxTE_MULTILINE)
804 return (window == GTK_TEXT(m_text)->text_area);
805 else
806 return (window == GTK_ENTRY(m_text)->text_area);
903f689b 807}
e3e65dac 808
bb69661b
VZ
809// the font will change for subsequent text insertiongs
810bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 811{
223d09f6 812 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 813
bb69661b
VZ
814 if ( !wxWindowBase::SetFont(font) )
815 {
816 // font didn't change, nothing to do
817 return FALSE;
818 }
819
820 if ( m_windowStyle & wxTE_MULTILINE )
821 {
822 // for compatibility with other ports: the font is a global controls
823 // characteristic, so change the font globally
824 wxString value = GetValue();
825 if ( !value.IsEmpty() )
826 {
827 Clear();
828
829 AppendText(value);
830 }
831 }
832
833 return TRUE;
58614078
RR
834}
835
f03fc89f 836bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
58614078 837{
223d09f6 838 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 839
ae0bdb01 840 // doesn't work
f03fc89f 841 return FALSE;
868a2826 842}
e3e65dac 843
f03fc89f 844bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 845{
223d09f6 846 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
a81258be 847
ae0bdb01 848 wxControl::SetBackgroundColour( colour );
3358d36e 849
f03fc89f
VZ
850 if (!m_widget->window)
851 return FALSE;
8bbe427f 852
ae0bdb01 853 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
805dd538
VZ
854 if (sysbg.Red() == colour.Red() &&
855 sysbg.Green() == colour.Green() &&
ae0bdb01
RR
856 sysbg.Blue() == colour.Blue())
857 {
f03fc89f 858 return FALSE; // FIXME or TRUE?
805dd538
VZ
859 }
860
f03fc89f
VZ
861 if (!m_backgroundColour.Ok())
862 return FALSE;
8bbe427f 863
ae0bdb01
RR
864 if (m_windowStyle & wxTE_MULTILINE)
865 {
866 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f
VZ
867 if (!window)
868 return FALSE;
ae0bdb01
RR
869 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
870 gdk_window_set_background( window, m_backgroundColour.GetColor() );
871 gdk_window_clear( window );
872 }
f03fc89f
VZ
873
874 return TRUE;
58614078
RR
875}
876
877void wxTextCtrl::ApplyWidgetStyle()
878{
ae0bdb01
RR
879 if (m_windowStyle & wxTE_MULTILINE)
880 {
2830bf19 881 // how ?
805dd538 882 }
ae0bdb01
RR
883 else
884 {
885 SetWidgetStyle();
886 gtk_widget_set_style( m_text, m_widgetStyle );
887 }
68dda785 888}
f96aa4d9 889
e702ff0f
JS
890void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
891{
892 Cut();
893}
894
895void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
896{
897 Copy();
898}
899
900void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
901{
902 Paste();
903}
904
905void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
906{
907 Undo();
908}
909
910void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
911{
912 Redo();
913}
914
915void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
916{
917 event.Enable( CanCut() );
918}
919
920void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
921{
922 event.Enable( CanCopy() );
923}
924
925void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
926{
927 event.Enable( CanPaste() );
928}
929
930void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
931{
932 event.Enable( CanUndo() );
933}
934
935void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
936{
937 event.Enable( CanRedo() );
938}
65045edd
RR
939
940void wxTextCtrl::OnInternalIdle()
941{
942 wxCursor cursor = m_cursor;
943 if (g_globalCursor.Ok()) cursor = g_globalCursor;
944
307f16e8 945 if (cursor.Ok())
65045edd 946 {
65045edd
RR
947 GdkWindow *window = (GdkWindow*) NULL;
948 if (HasFlag(wxTE_MULTILINE))
949 window = GTK_TEXT(m_text)->text_area;
950 else
951 window = GTK_ENTRY(m_text)->text_area;
952
953 if (window)
954 gdk_window_set_cursor( window, cursor.GetCursor() );
955
956 if (!g_globalCursor.Ok())
957 cursor = *wxSTANDARD_CURSOR;
958
959 window = m_widget->window;
247e5b16 960 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd
RR
961 gdk_window_set_cursor( window, cursor.GetCursor() );
962 }
26bf1ce0
VZ
963
964 UpdateWindowUI();
65045edd 965}
f68586e5
VZ
966
967wxSize wxTextCtrl::DoGetBestSize() const
968{
969 // FIXME should be different for multi-line controls...
970 return wxSize(80, 26);
971}