]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
fixed a memory leak and an initialization problem
[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 16#include "wx/intl.h"
a1f79c1e 17#include "wx/log.h"
ae0bdb01 18#include "wx/settings.h"
fc71ef6e 19#include "wx/panel.h"
c801d85f 20
a81258be
RR
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <ctype.h>
817ec43a 24#include <math.h> // for fabs
a81258be 25
83624f79
RR
26#include "gdk/gdk.h"
27#include "gtk/gtk.h"
b292e2f5
RR
28#include "gdk/gdkkeysyms.h"
29
acfd422a
RR
30//-----------------------------------------------------------------------------
31// idle system
32//-----------------------------------------------------------------------------
33
34extern void wxapp_install_idle_handler();
35extern bool g_isIdle;
36
b292e2f5
RR
37//-----------------------------------------------------------------------------
38// data
39//-----------------------------------------------------------------------------
40
65045edd
RR
41extern bool g_blockEventsOnDrag;
42extern wxCursor g_globalCursor;
b292e2f5 43
ce2f50e3
VZ
44// ----------------------------------------------------------------------------
45// "insert_text" for GtkEntry
46// ----------------------------------------------------------------------------
47
48static void
49gtk_insert_text_callback(GtkEditable *editable,
50 const gchar *new_text,
51 gint new_text_length,
52 gint *position,
53 wxTextCtrl *win)
54{
76fcf0f2
RR
55 if (g_isIdle)
56 wxapp_install_idle_handler();
57
ce2f50e3
VZ
58 // we should only be called if we have a max len limit at all
59 GtkEntry *entry = GTK_ENTRY (editable);
60
61 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
62
63 // check that we don't overflow the max length limit
64 //
65 // FIXME: this doesn't work when we paste a string which is going to be
66 // truncated
67 if ( entry->text_length == entry->text_max_length )
68 {
69 // we don't need to run the base class version at all
70 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
71
72 // remember that the next changed signal is to be ignored to avoid
73 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
74 win->IgnoreNextTextUpdate();
75
76 // and generate the correct one ourselves
77 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
78 event.SetEventObject(win);
79 event.SetString(win->GetValue());
80 win->GetEventHandler()->ProcessEvent( event );
81 }
82}
83
c801d85f 84//-----------------------------------------------------------------------------
2f2aa628 85// "changed"
c801d85f
KB
86//-----------------------------------------------------------------------------
87
805dd538 88static void
ba3f6b44 89gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
484e45bf 90{
ce2f50e3
VZ
91 if ( win->IgnoreTextUpdate() )
92 return;
93
a2053b27 94 if (!win->m_hasVMT) return;
805dd538 95
bb69661b 96 if (g_isIdle)
3c679789
RR
97 wxapp_install_idle_handler();
98
034be888 99 win->SetModified();
01041145 100 win->UpdateFontIfNeeded();
8bbe427f 101
f03fc89f 102 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
2830bf19 103 event.SetEventObject( win );
ce2f50e3 104 event.SetString( win->GetValue() );
2830bf19 105 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 106}
112892b9 107
2830bf19 108//-----------------------------------------------------------------------------
034be888 109// "changed" from vertical scrollbar
2830bf19
RR
110//-----------------------------------------------------------------------------
111
805dd538 112static void
034be888 113gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
2830bf19 114{
3c679789 115 if (!win->m_hasVMT) return;
bb69661b
VZ
116
117 if (g_isIdle)
3c679789 118 wxapp_install_idle_handler();
acfd422a 119
2830bf19
RR
120 win->CalculateScrollbar();
121}
122
2f2aa628
RR
123//-----------------------------------------------------------------------------
124// wxTextCtrl
125//-----------------------------------------------------------------------------
126
127IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
128
c801d85f 129BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 130 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
131
132 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
133 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
134 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
135 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
136 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
137
138 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
139 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
140 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
141 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
142 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
c801d85f
KB
143END_EVENT_TABLE()
144
01041145 145void wxTextCtrl::Init()
f5abe911 146{
ce2f50e3 147 m_ignoreNextUpdate =
f5abe911 148 m_modified = FALSE;
01041145 149 m_updateFont = FALSE;
3ca6a5f0
BP
150 m_text =
151 m_vScrollbar = (GtkWidget *)NULL;
f5abe911 152}
13289f04 153
13111b2a
VZ
154wxTextCtrl::wxTextCtrl( wxWindow *parent,
155 wxWindowID id,
156 const wxString &value,
157 const wxPoint &pos,
158 const wxSize &size,
159 long style,
160 const wxValidator& validator,
161 const wxString &name )
f5abe911 162{
01041145
VZ
163 Init();
164
f5abe911
RR
165 Create( parent, id, value, pos, size, style, validator, name );
166}
c801d85f 167
13111b2a
VZ
168bool wxTextCtrl::Create( wxWindow *parent,
169 wxWindowID id,
170 const wxString &value,
171 const wxPoint &pos,
172 const wxSize &size,
173 long style,
174 const wxValidator& validator,
175 const wxString &name )
c801d85f 176{
2830bf19 177 m_needParent = TRUE;
b292e2f5 178 m_acceptsFocus = TRUE;
484e45bf 179
4dcaf11a
RR
180 if (!PreCreation( parent, pos, size ) ||
181 !CreateBase( parent, id, pos, size, style, validator, name ))
182 {
223d09f6 183 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
13111b2a 184 return FALSE;
4dcaf11a 185 }
6de97a3b 186
805dd538 187
034be888 188 m_vScrollbarVisible = FALSE;
13289f04 189
2830bf19 190 bool multi_line = (style & wxTE_MULTILINE) != 0;
ab46dc18 191 if (multi_line)
2830bf19 192 {
7d6d2cd4 193#if (GTK_MINOR_VERSION > 2)
034be888
RR
194 /* a multi-line edit control: create a vertical scrollbar by default and
195 horizontal if requested */
2830bf19 196 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
7d6d2cd4
RR
197#else
198 bool bHasHScrollbar = FALSE;
199#endif
805dd538 200
034be888 201 /* create our control ... */
2830bf19
RR
202 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
203
034be888 204 /* ... and put into the upper left hand corner of the table */
2830bf19 205 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
5664fc32 206 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19 207 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 208 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
209 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
210 0, 0);
bb69661b 211
5c387335
RR
212 /* always wrap words */
213 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
bb69661b 214
7d6d2cd4 215#if (GTK_MINOR_VERSION > 2)
034be888 216 /* put the horizontal scrollbar in the lower left hand corner */
2830bf19
RR
217 if (bHasHScrollbar)
218 {
219 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
5664fc32 220 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
2830bf19 221 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
8ce63e9d 222 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
13289f04
VZ
223 GTK_FILL,
224 0, 0);
2830bf19 225 gtk_widget_show(hscrollbar);
13289f04 226
3358d36e
VZ
227 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
228 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
5c387335 229 }
7d6d2cd4 230#endif
bb69661b 231
ab46dc18
RR
232 /* finally, put the vertical scrollbar in the upper right corner */
233 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
234 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
ab46dc18
RR
235 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
236 GTK_FILL,
237 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
238 0, 0);
2830bf19
RR
239 }
240 else
241 {
034be888 242 /* a single-line text control: no need for scrollbars */
2830bf19
RR
243 m_widget =
244 m_text = gtk_entry_new();
245 }
484e45bf 246
db434467 247 m_parent->DoAddChild( this );
76fcf0f2
RR
248
249 m_focusWidget = m_text;
db434467
RR
250
251 PostCreation();
252
253 SetFont( parent->GetFont() );
254
255 wxSize size_best( DoGetBestSize() );
256 wxSize new_size( size );
0279e844 257 if (new_size.x == -1)
db434467 258 new_size.x = size_best.x;
0279e844 259 if (new_size.y == -1)
db434467 260 new_size.y = size_best.y;
0279e844
RR
261 if ((new_size.x != size.x) || (new_size.y != size.y))
262 SetSize( new_size.x, new_size.y );
484e45bf 263
2830bf19 264 if (multi_line)
2830bf19 265 gtk_widget_show(m_text);
13289f04 266
ab46dc18
RR
267 if (multi_line)
268 {
269 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
270 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
271 }
3358d36e 272
291a8f20 273 if (!value.IsEmpty())
2830bf19
RR
274 {
275 gint tmp = 0;
3358d36e
VZ
276
277#if GTK_MINOR_VERSION == 0
278 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
279 // gtk_editable_insert_text()
280 gtk_widget_realize(m_text);
281#endif // GTK 1.0
282
05939a81 283#if wxUSE_UNICODE
3358d36e 284 wxWX2MBbuf val = value.mbc_str();
05939a81 285 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
3358d36e 286#else // !Unicode
2830bf19 287 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
3358d36e
VZ
288#endif // Unicode/!Unicode
289
291a8f20
RR
290 if (multi_line)
291 {
3358d36e
VZ
292 /* bring editable's cursor uptodate. bug in GTK. */
293
294 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
295 }
2830bf19 296 }
484e45bf 297
2830bf19
RR
298 if (style & wxTE_PASSWORD)
299 {
300 if (!multi_line)
301 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
302 }
8bbe427f 303
2830bf19
RR
304 if (style & wxTE_READONLY)
305 {
306 if (!multi_line)
307 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
308 }
309 else
310 {
311 if (multi_line)
312 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
313 }
3358d36e 314
ce16e5d7
RR
315 /* we want to be notified about text changes */
316 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
317 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
318
a88acabd
JS
319 /* we don't set a valid background colour, because the window
320 manager should use a default one */
321 m_backgroundColour = wxColour();
17665a2b
VZ
322
323 wxColour colFg = parent->GetForegroundColour();
324 SetForegroundColour( colFg );
805dd538 325
65045edd 326 m_cursor = wxCursor( wxCURSOR_IBEAM );
13111b2a 327
6c5ac6e1 328 wxTextAttr attrDef( colFg, m_backgroundColour, parent->GetFont() );
17665a2b
VZ
329 SetDefaultStyle( attrDef );
330
2830bf19 331 Show( TRUE );
805dd538 332
2830bf19
RR
333 return TRUE;
334}
484e45bf 335
2830bf19
RR
336void wxTextCtrl::CalculateScrollbar()
337{
338 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
f96aa4d9 339
2830bf19 340 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
805dd538 341
2830bf19
RR
342 if (adj->upper - adj->page_size < 0.8)
343 {
344 if (m_vScrollbarVisible)
345 {
034be888 346 gtk_widget_hide( m_vScrollbar );
034be888 347 m_vScrollbarVisible = FALSE;
2830bf19
RR
348 }
349 }
350 else
351 {
352 if (!m_vScrollbarVisible)
353 {
034be888 354 gtk_widget_show( m_vScrollbar );
034be888 355 m_vScrollbarVisible = TRUE;
2830bf19
RR
356 }
357 }
6de97a3b 358}
c801d85f 359
03f38c58 360wxString wxTextCtrl::GetValue() const
c801d85f 361{
223d09f6 362 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
8bbe427f 363
2830bf19
RR
364 wxString tmp;
365 if (m_windowStyle & wxTE_MULTILINE)
366 {
367 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
368 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
dcf924a3 369 tmp = wxString(text,*wxConvCurrent);
2830bf19
RR
370 g_free( text );
371 }
372 else
373 {
dcf924a3 374 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
2830bf19
RR
375 }
376 return tmp;
6de97a3b 377}
c801d85f
KB
378
379void wxTextCtrl::SetValue( const wxString &value )
380{
223d09f6 381 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 382
2830bf19
RR
383 if (m_windowStyle & wxTE_MULTILINE)
384 {
385 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
386 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
387 len = 0;
05939a81 388#if wxUSE_UNICODE
01041145 389 wxWX2MBbuf tmpbuf = value.mbc_str();
05939a81
OK
390 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
391#else
01041145 392 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
05939a81 393#endif
2830bf19
RR
394 }
395 else
396 {
01041145 397 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
2830bf19 398 }
f6bcfd97
BP
399
400 // GRG, Jun/2000: Changed this after a lot of discussion in
401 // the lists. wxWindows 2.2 will have a set of flags to
402 // customize this behaviour.
403 SetInsertionPoint(0);
404
405 m_modified = FALSE;
6de97a3b 406}
c801d85f
KB
407
408void wxTextCtrl::WriteText( const wxString &text )
409{
223d09f6 410 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 411
17665a2b
VZ
412 if ( text.empty() )
413 return;
484e45bf 414
17665a2b
VZ
415#if wxUSE_UNICODE
416 wxWX2MBbuf buf = text.mbc_str();
417 const char *txt = buf;
418 size_t txtlen = strlen(buf);
419#else
420 const char *txt = text;
421 size_t txtlen = text.length();
422#endif
423
424 if ( m_windowStyle & wxTE_MULTILINE )
2830bf19 425 {
ba3f6b44
RR
426 // After cursor movements, gtk_text_get_point() is wrong by one.
427 gtk_text_set_point( GTK_TEXT(m_text), GTK_EDITABLE(m_text)->current_pos );
428
17665a2b
VZ
429 // if we have any special style, use it
430 if ( !m_defaultStyle.IsDefault() )
431 {
432 GdkFont *font = m_defaultStyle.HasFont()
433 ? m_defaultStyle.GetFont().GetInternalFont()
434 : NULL;
435
436 GdkColor *colFg = m_defaultStyle.HasTextColour()
437 ? m_defaultStyle.GetTextColour().GetColor()
438 : NULL;
439
440 GdkColor *colBg = m_defaultStyle.HasBackgroundColour()
441 ? m_defaultStyle.GetBackgroundColour().GetColor()
442 : NULL;
443
ba3f6b44
RR
444 GetInsertionPoint();
445 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, -1 );
17665a2b
VZ
446 }
447 else // no style
448 {
ba3f6b44 449 gint len = GTK_EDITABLE(m_text)->current_pos;
17665a2b
VZ
450 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
451 }
ba3f6b44
RR
452
453 // Bring editable's cursor back uptodate.
3358d36e 454 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
2830bf19 455 }
17665a2b 456 else // single line
2830bf19 457 {
ba3f6b44 458 // This moves the cursor pos to behind the inserted text.
3358d36e 459 gint len = GTK_EDITABLE(m_text)->current_pos;
17665a2b 460 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
3358d36e 461
ba3f6b44 462 // Bring editable's cursor uptodate.
3358d36e
VZ
463 GTK_EDITABLE(m_text)->current_pos += text.Len();
464
ba3f6b44 465 // Bring entry's cursor uptodate.
3358d36e 466 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
2830bf19 467 }
ba3f6b44
RR
468
469 m_modified = TRUE;
6de97a3b 470}
c801d85f 471
a6e21573
HH
472void wxTextCtrl::AppendText( const wxString &text )
473{
ba3f6b44
RR
474 SetInsertionPointEnd();
475 WriteText( text );
476}
2df7be7f 477
ba3f6b44
RR
478wxString wxTextCtrl::GetLineText( long lineNo ) const
479{
a6e21573
HH
480 if (m_windowStyle & wxTE_MULTILINE)
481 {
ba3f6b44
RR
482 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
483 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
bb69661b 484
ba3f6b44
RR
485 if (text)
486 {
487 wxString buf(wxT(""));
488 long i;
489 int currentLine = 0;
490 for (i = 0; currentLine != lineNo && text[i]; i++ )
491 if (text[i] == '\n')
492 currentLine++;
493 // Now get the text
494 int j;
495 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
496 buf += text[i];
17665a2b 497
ba3f6b44
RR
498 g_free( text );
499 return buf;
bb69661b 500 }
ba3f6b44 501 else
bb69661b 502 {
ba3f6b44 503 return wxEmptyString;
bb69661b 504 }
a6e21573 505 }
ba3f6b44 506 else
a81258be 507 {
ba3f6b44
RR
508 if (lineNo == 0) return GetValue();
509 return wxEmptyString;
a81258be 510 }
6de97a3b 511}
c801d85f 512
a81258be
RR
513void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
514{
ac0d36b5
HH
515 /* If you implement this, don't forget to update the documentation!
516 * (file docs/latex/wx/text.tex) */
223d09f6 517 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 518}
112892b9 519
0efe5ba7 520bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 521{
96385642 522 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 523 {
96385642
VZ
524 wxString text = GetValue();
525
6085b116 526 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 527 if( (unsigned long)pos > text.Len() )
96385642
VZ
528 return FALSE;
529
ac0d36b5
HH
530 *x=0; // First Col
531 *y=0; // First Line
2829d9e3 532
05939a81
OK
533 const wxChar* stop = text.c_str() + pos;
534 for ( const wxChar *p = text.c_str(); p < stop; p++ )
96385642 535 {
223d09f6 536 if (*p == wxT('\n'))
96385642
VZ
537 {
538 (*y)++;
ac0d36b5 539 *x=0;
96385642
VZ
540 }
541 else
542 (*x)++;
543 }
805dd538 544 }
96385642
VZ
545 else // single line control
546 {
2829d9e3 547 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 548 {
ac0d36b5 549 *y = 0;
96385642
VZ
550 *x = pos;
551 }
552 else
553 {
554 // index out of bounds
555 return FALSE;
556 }
8bbe427f 557 }
96385642
VZ
558
559 return TRUE;
6de97a3b 560}
c801d85f 561
e3ca08dd 562long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 563{
2830bf19 564 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 565
2830bf19 566 long pos=0;
ac0d36b5 567 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 568
3358d36e 569 pos += x;
2830bf19 570 return pos;
6de97a3b 571}
c801d85f 572
a81258be 573int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 574{
a81258be
RR
575 wxString str = GetLineText (lineNo);
576 return (int) str.Length();
6de97a3b 577}
c801d85f 578
a81258be 579int wxTextCtrl::GetNumberOfLines() const
c801d85f 580{
2830bf19 581 if (m_windowStyle & wxTE_MULTILINE)
a81258be 582 {
2830bf19
RR
583 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
584 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
585
586 if (text)
587 {
588 int currentLine = 0;
589 for (int i = 0; i < len; i++ )
96385642 590 {
2830bf19 591 if (text[i] == '\n')
96385642
VZ
592 currentLine++;
593 }
2830bf19 594 g_free( text );
2829d9e3
VZ
595
596 // currentLine is 0 based, add 1 to get number of lines
597 return currentLine + 1;
2830bf19
RR
598 }
599 else
96385642 600 {
2830bf19 601 return 0;
96385642 602 }
a81258be
RR
603 }
604 else
2830bf19 605 {
96385642 606 return 1;
2830bf19 607 }
6de97a3b 608}
c801d85f 609
debe6624 610void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 611{
223d09f6 612 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e
VZ
613
614 if (m_windowStyle & wxTE_MULTILINE)
291a8f20
RR
615 {
616 /* seems to be broken in GTK 1.0.X:
3358d36e
VZ
617 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
618
291a8f20
RR
619 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
620 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 621
291a8f20 622 /* we fake a set_point by inserting and deleting. as the user
3358d36e
VZ
623 isn't supposed to get to know about thos non-sense, we
624 disconnect so that no events are sent to the user program. */
625
291a8f20
RR
626 gint tmp = (gint)pos;
627 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
628 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
629
291a8f20
RR
630 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
631 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e
VZ
632
633 /* bring editable's cursor uptodate. another bug in GTK. */
634
635 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
ac0d36b5 636 }
2830bf19 637 else
291a8f20 638 {
d59051dd 639 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e
VZ
640
641 /* bring editable's cursor uptodate. bug in GTK. */
642
b02da6b1 643 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
291a8f20 644 }
6de97a3b 645}
c801d85f 646
03f38c58 647void wxTextCtrl::SetInsertionPointEnd()
c801d85f 648{
223d09f6 649 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 650
d59051dd
VZ
651 if (m_windowStyle & wxTE_MULTILINE)
652 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
653 else
654 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
6de97a3b 655}
c801d85f 656
debe6624 657void wxTextCtrl::SetEditable( bool editable )
c801d85f 658{
223d09f6 659 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 660
2830bf19
RR
661 if (m_windowStyle & wxTE_MULTILINE)
662 gtk_text_set_editable( GTK_TEXT(m_text), editable );
663 else
664 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
6de97a3b 665}
c801d85f 666
68df5777
RR
667bool wxTextCtrl::Enable( bool enable )
668{
669 if (!wxWindowBase::Enable(enable))
670 {
671 // nothing to do
672 return FALSE;
673 }
f6bcfd97 674
68df5777
RR
675 if (m_windowStyle & wxTE_MULTILINE)
676 {
677 gtk_text_set_editable( GTK_TEXT(m_text), enable );
5abf8c9d 678 OnParentEnable(enable);
68df5777
RR
679 }
680 else
681 {
682 gtk_widget_set_sensitive( m_text, enable );
683 }
684
685 return TRUE;
686}
687
fdca68a6
JS
688// wxGTK-specific: called recursively by Enable,
689// to give widgets an oppprtunity to correct their colours after they
690// have been changed by Enable
691void wxTextCtrl::OnParentEnable( bool enable )
692{
693 // If we have a custom background colour, we use this colour in both
694 // disabled and enabled mode, or we end up with a different colour under the
695 // text.
696 wxColour oldColour = GetBackgroundColour();
697 if (oldColour.Ok())
698 {
699 // Need to set twice or it'll optimize the useful stuff out
700 if (oldColour == * wxWHITE)
701 SetBackgroundColour(*wxBLACK);
702 else
703 SetBackgroundColour(*wxWHITE);
704 SetBackgroundColour(oldColour);
705 }
706}
707
0efe5ba7
VZ
708void wxTextCtrl::DiscardEdits()
709{
710 m_modified = FALSE;
711}
712
ce2f50e3
VZ
713// ----------------------------------------------------------------------------
714// max text length support
715// ----------------------------------------------------------------------------
716
717void wxTextCtrl::IgnoreNextTextUpdate()
718{
719 m_ignoreNextUpdate = TRUE;
720}
721
722bool wxTextCtrl::IgnoreTextUpdate()
723{
724 if ( m_ignoreNextUpdate )
725 {
726 m_ignoreNextUpdate = FALSE;
727
728 return TRUE;
729 }
730
731 return FALSE;
732}
733
d7eee191
VZ
734void wxTextCtrl::SetMaxLength(unsigned long len)
735{
736 if ( !HasFlag(wxTE_MULTILINE) )
737 {
738 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
ce2f50e3
VZ
739
740 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
741 // we had tried to enter more text than allowed by max text length and
742 // the text wasn't really changed
743 //
744 // to detect this and generate TEXT_MAXLEN event instead of
745 // TEXT_CHANGED one in this case we also catch "insert_text" signal
746 //
747 // when max len is set to 0 we disconnect our handler as it means that
748 // we shouldn't check anything any more
749 if ( len )
750 {
751 gtk_signal_connect( GTK_OBJECT(m_text),
752 "insert_text",
753 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
754 (gpointer)this);
755 }
756 else // no checking
757 {
758 gtk_signal_disconnect_by_func
759 (
760 GTK_OBJECT(m_text),
761 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
762 (gpointer)this
763 );
764 }
d7eee191
VZ
765 }
766}
767
debe6624 768void wxTextCtrl::SetSelection( long from, long to )
c801d85f 769{
223d09f6 770 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 771
a1f79c1e
VZ
772 if ( (m_windowStyle & wxTE_MULTILINE) &&
773 !GTK_TEXT(m_text)->line_start_cache )
774 {
775 // tell the programmer that it didn't work
776 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
777 return;
778 }
779
2830bf19 780 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 781}
c801d85f 782
910f1f8c 783void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 784{
910f1f8c 785// SetInsertionPoint( pos );
6de97a3b 786}
c801d85f 787
03f38c58 788long wxTextCtrl::GetInsertionPoint() const
c801d85f 789{
223d09f6 790 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 791
2830bf19 792 return (long) GTK_EDITABLE(m_text)->current_pos;
6de97a3b 793}
c801d85f 794
03f38c58 795long wxTextCtrl::GetLastPosition() const
c801d85f 796{
223d09f6 797 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 798
2830bf19
RR
799 int pos = 0;
800 if (m_windowStyle & wxTE_MULTILINE)
801 pos = gtk_text_get_length( GTK_TEXT(m_text) );
802 else
803 pos = GTK_ENTRY(m_text)->text_length;
805dd538 804
ac0d36b5 805 return (long)pos;
6de97a3b 806}
c801d85f 807
debe6624 808void wxTextCtrl::Remove( long from, long to )
c801d85f 809{
223d09f6 810 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 811
2830bf19 812 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 813}
c801d85f 814
debe6624 815void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 816{
223d09f6 817 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 818
2830bf19 819 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
bb69661b 820
2df7be7f
RR
821 if (!value.IsEmpty())
822 {
823 gint pos = (gint)from;
05939a81 824#if wxUSE_UNICODE
2df7be7f
RR
825 wxWX2MBbuf buf = value.mbc_str();
826 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 827#else
2df7be7f 828 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
05939a81 829#endif
2df7be7f 830 }
6de97a3b 831}
c801d85f 832
03f38c58 833void wxTextCtrl::Cut()
c801d85f 834{
223d09f6 835 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 836
d345e841 837#if (GTK_MINOR_VERSION > 0)
2830bf19 838 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 839#else
2830bf19 840 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 841#endif
6de97a3b 842}
c801d85f 843
03f38c58 844void wxTextCtrl::Copy()
c801d85f 845{
223d09f6 846 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 847
d345e841 848#if (GTK_MINOR_VERSION > 0)
2830bf19 849 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 850#else
2830bf19 851 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 852#endif
6de97a3b 853}
c801d85f 854
03f38c58 855void wxTextCtrl::Paste()
c801d85f 856{
223d09f6 857 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 858
d345e841 859#if (GTK_MINOR_VERSION > 0)
2830bf19 860 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 861#else
2830bf19 862 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 863#endif
6de97a3b 864}
c801d85f 865
ca8b28f2
JS
866// Undo/redo
867void wxTextCtrl::Undo()
868{
869 // TODO
223d09f6 870 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
871}
872
873void wxTextCtrl::Redo()
874{
875 // TODO
223d09f6 876 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
877}
878
879bool wxTextCtrl::CanUndo() const
880{
881 // TODO
223d09f6 882 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
ca8b28f2
JS
883 return FALSE;
884}
885
886bool wxTextCtrl::CanRedo() const
887{
888 // TODO
223d09f6 889 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
ca8b28f2
JS
890 return FALSE;
891}
892
893// If the return values from and to are the same, there is no
894// selection.
2d4cc5b6 895void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 896{
223d09f6 897 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 898
2d4cc5b6
VZ
899 long from, to;
900 if ( !(GTK_EDITABLE(m_text)->has_selection) )
05060eeb 901 {
2d4cc5b6
VZ
902 from =
903 to = GetInsertionPoint();
904 }
905 else // got selection
906 {
907 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
908 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
909
910 if ( from > to )
911 {
912 // exchange them to be compatible with wxMSW
913 long tmp = from;
914 from = to;
915 to = tmp;
916 }
05060eeb 917 }
bb69661b 918
2d4cc5b6
VZ
919 if ( fromOut )
920 *fromOut = from;
921 if ( toOut )
922 *toOut = to;
ca8b28f2
JS
923}
924
925bool wxTextCtrl::IsEditable() const
926{
223d09f6 927 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
05060eeb
RR
928
929 return GTK_EDITABLE(m_text)->editable;
ca8b28f2
JS
930}
931
0efe5ba7
VZ
932bool wxTextCtrl::IsModified() const
933{
934 return m_modified;
935}
936
03f38c58 937void wxTextCtrl::Clear()
c801d85f 938{
223d09f6 939 SetValue( wxT("") );
6de97a3b 940}
c801d85f 941
903f689b 942void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 943{
223d09f6 944 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 945
2830bf19
RR
946 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
947 {
948 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
949 event.SetEventObject(this);
f6bcfd97 950 event.SetString(GetValue());
2830bf19
RR
951 if (GetEventHandler()->ProcessEvent(event)) return;
952 }
903f689b 953
da048e3d
RR
954 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
955 {
956 wxWindow *top_frame = m_parent;
8487f887 957 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 958 top_frame = top_frame->GetParent();
13111b2a
VZ
959 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
960
961 if (window->default_widget)
da048e3d
RR
962 {
963 gtk_widget_activate (window->default_widget);
13111b2a
VZ
964 return;
965 }
da048e3d
RR
966 }
967
2830bf19 968 key_event.Skip();
6de97a3b 969}
c801d85f 970
03f38c58 971GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 972{
ae0bdb01 973 return GTK_WIDGET(m_text);
6de97a3b 974}
e3e65dac 975
903f689b
RR
976bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
977{
ae0bdb01
RR
978 if (m_windowStyle & wxTE_MULTILINE)
979 return (window == GTK_TEXT(m_text)->text_area);
980 else
981 return (window == GTK_ENTRY(m_text)->text_area);
903f689b 982}
e3e65dac 983
bb69661b
VZ
984// the font will change for subsequent text insertiongs
985bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 986{
223d09f6 987 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 988
bb69661b
VZ
989 if ( !wxWindowBase::SetFont(font) )
990 {
991 // font didn't change, nothing to do
992 return FALSE;
993 }
994
995 if ( m_windowStyle & wxTE_MULTILINE )
996 {
01041145 997 m_updateFont = TRUE;
bb69661b 998
1ff4714d
VZ
999 m_defaultStyle.SetFont(font);
1000
01041145 1001 ChangeFontGlobally();
bb69661b
VZ
1002 }
1003
1004 return TRUE;
58614078
RR
1005}
1006
01041145
VZ
1007void wxTextCtrl::ChangeFontGlobally()
1008{
1009 // this method is very inefficient and hence should be called as rarely as
1010 // possible!
1011 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1012 _T("shouldn't be called for single line controls") );
1013
1014 wxString value = GetValue();
1015 if ( !value.IsEmpty() )
1016 {
572aeb77
VZ
1017 m_updateFont = FALSE;
1018
01041145
VZ
1019 Clear();
1020 AppendText(value);
01041145
VZ
1021 }
1022}
1023
1024void wxTextCtrl::UpdateFontIfNeeded()
1025{
1026 if ( m_updateFont )
1027 ChangeFontGlobally();
1028}
1029
17665a2b
VZ
1030bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1031{
1032 if ( !wxControl::SetForegroundColour(colour) )
1033 return FALSE;
1034
1035 // update default fg colour too
1036 m_defaultStyle.SetTextColour(colour);
1037
1038 return TRUE;
1039}
1040
f03fc89f 1041bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1042{
223d09f6 1043 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
a81258be 1044
1f477433
VZ
1045 if ( !wxControl::SetBackgroundColour( colour ) )
1046 return FALSE;
3358d36e 1047
f03fc89f
VZ
1048 if (!m_widget->window)
1049 return FALSE;
8bbe427f 1050
ae0bdb01 1051 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
805dd538
VZ
1052 if (sysbg.Red() == colour.Red() &&
1053 sysbg.Green() == colour.Green() &&
ae0bdb01
RR
1054 sysbg.Blue() == colour.Blue())
1055 {
f03fc89f 1056 return FALSE; // FIXME or TRUE?
805dd538
VZ
1057 }
1058
f03fc89f
VZ
1059 if (!m_backgroundColour.Ok())
1060 return FALSE;
8bbe427f 1061
ae0bdb01
RR
1062 if (m_windowStyle & wxTE_MULTILINE)
1063 {
1064 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f
VZ
1065 if (!window)
1066 return FALSE;
ae0bdb01
RR
1067 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1068 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1069 gdk_window_clear( window );
1070 }
f03fc89f 1071
17665a2b
VZ
1072 // change active background color too
1073 m_defaultStyle.SetBackgroundColour( colour );
1074
f03fc89f 1075 return TRUE;
58614078
RR
1076}
1077
17665a2b
VZ
1078bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr &style )
1079{
1080 /* VERY dirty way to do that - removes the required text and re-adds it
1081 with styling (FIXME) */
1082 if ( m_windowStyle & wxTE_MULTILINE )
1083 {
1084 if ( style.IsDefault() )
1085 {
1086 // nothing to do
1087 return TRUE;
1088 }
1089
1090 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1091
1092 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1093 _T("invalid range in wxTextCtrl::SetStyle") );
1094
1095 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1096 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1097 wxString tmp(text,*wxConvCurrent);
1098 g_free( text );
1099
1100 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1101 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1102
1103#if wxUSE_UNICODE
1104 wxWX2MBbuf buf = tmp.mbc_str();
1105 const char *txt = buf;
1106 size_t txtlen = strlen(buf);
1107#else
1108 const char *txt = tmp;
1109 size_t txtlen = tmp.length();
1110#endif
1111
1112 GdkFont *font = style.HasFont()
1113 ? style.GetFont().GetInternalFont()
1114 : NULL;
1115
1116 GdkColor *colFg = style.HasTextColour()
1117 ? style.GetTextColour().GetColor()
1118 : NULL;
1119
1120 GdkColor *colBg = style.HasBackgroundColour()
1121 ? style.GetBackgroundColour().GetColor()
1122 : NULL;
1123
1124 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
1125
1126 /* does not seem to help under GTK+ 1.2 !!!
1127 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1128 SetInsertionPoint( old_pos );
1129 return TRUE;
1130 }
1131 else // singe line
1132 {
1133 // cannot do this for GTK+'s Entry widget
1134 return FALSE;
1135 }
1136}
1137
58614078
RR
1138void wxTextCtrl::ApplyWidgetStyle()
1139{
ae0bdb01
RR
1140 if (m_windowStyle & wxTE_MULTILINE)
1141 {
2830bf19 1142 // how ?
805dd538 1143 }
ae0bdb01
RR
1144 else
1145 {
1146 SetWidgetStyle();
1147 gtk_widget_set_style( m_text, m_widgetStyle );
1148 }
68dda785 1149}
f96aa4d9 1150
e702ff0f
JS
1151void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1152{
1153 Cut();
1154}
1155
1156void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1157{
1158 Copy();
1159}
1160
1161void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1162{
1163 Paste();
1164}
1165
1166void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1167{
1168 Undo();
1169}
1170
1171void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1172{
1173 Redo();
1174}
1175
1176void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1177{
1178 event.Enable( CanCut() );
1179}
1180
1181void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1182{
1183 event.Enable( CanCopy() );
1184}
1185
1186void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1187{
1188 event.Enable( CanPaste() );
1189}
1190
1191void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1192{
1193 event.Enable( CanUndo() );
1194}
1195
1196void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1197{
1198 event.Enable( CanRedo() );
1199}
65045edd
RR
1200
1201void wxTextCtrl::OnInternalIdle()
1202{
1203 wxCursor cursor = m_cursor;
1204 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1205
307f16e8 1206 if (cursor.Ok())
65045edd 1207 {
65045edd 1208 GdkWindow *window = (GdkWindow*) NULL;
13111b2a 1209 if (HasFlag(wxTE_MULTILINE))
65045edd
RR
1210 window = GTK_TEXT(m_text)->text_area;
1211 else
1212 window = GTK_ENTRY(m_text)->text_area;
13111b2a 1213
65045edd
RR
1214 if (window)
1215 gdk_window_set_cursor( window, cursor.GetCursor() );
1216
1217 if (!g_globalCursor.Ok())
1218 cursor = *wxSTANDARD_CURSOR;
1219
1220 window = m_widget->window;
247e5b16 1221 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd
RR
1222 gdk_window_set_cursor( window, cursor.GetCursor() );
1223 }
26bf1ce0
VZ
1224
1225 UpdateWindowUI();
65045edd 1226}
f68586e5
VZ
1227
1228wxSize wxTextCtrl::DoGetBestSize() const
1229{
1230 // FIXME should be different for multi-line controls...
0279e844
RR
1231 wxSize ret( wxControl::DoGetBestSize() );
1232 return wxSize(80, ret.y);
f68586e5 1233}
0cc7251e 1234
9cd6d737
VZ
1235// ----------------------------------------------------------------------------
1236// freeze/thaw
1237// ----------------------------------------------------------------------------
1238
0cc7251e
VZ
1239void wxTextCtrl::Freeze()
1240{
1241 if ( HasFlag(wxTE_MULTILINE) )
1242 {
1243 gtk_text_freeze(GTK_TEXT(m_text));
1244 }
1245}
1246
1247void wxTextCtrl::Thaw()
1248{
1249 if ( HasFlag(wxTE_MULTILINE) )
1250 {
817ec43a
VZ
1251 GTK_TEXT(m_text)->vadj->value = 0.0;
1252
0cc7251e
VZ
1253 gtk_text_thaw(GTK_TEXT(m_text));
1254 }
1255}
9cd6d737
VZ
1256
1257// ----------------------------------------------------------------------------
1258// scrolling
1259// ----------------------------------------------------------------------------
1260
1261GtkAdjustment *wxTextCtrl::GetVAdj() const
1262{
1263 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1264}
1265
1266bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1267{
1268 float value = adj->value + diff;
1269
1270 if ( value < 0 )
1271 value = 0;
1272
1273 float upper = adj->upper - adj->page_size;
1274 if ( value > upper )
1275 value = upper;
1276
1277 // did we noticeably change the scroll position?
1278 if ( fabs(adj->value - value) < 0.2 )
1279 {
1280 // well, this is what Robert does in wxScrollBar, so it must be good...
1281 return FALSE;
1282 }
1283
1284 adj->value = value;
1285
1286 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1287
1288 return TRUE;
1289}
1290
1291bool wxTextCtrl::ScrollLines(int lines)
1292{
1293 GtkAdjustment *adj = GetVAdj();
1294 if ( !adj )
1295 return FALSE;
1296
1297 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1298 static const int KEY_SCROLL_PIXELS = 10;
1299
1300 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1301}
1302
1303bool wxTextCtrl::ScrollPages(int pages)
1304{
1305 GtkAdjustment *adj = GetVAdj();
1306 if ( !adj )
1307 return FALSE;
1308
817ec43a 1309 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
9cd6d737
VZ
1310}
1311