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