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