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