Better scrolling to cursor.
[wxWidgets.git] / src / gtk1 / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "textctrl.h"
12 #endif
13
14 #include "wx/textctrl.h"
15 #include "wx/utils.h"
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #include "wx/settings.h"
19 #include "wx/panel.h"
20 #include "wx/strconv.h"
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <ctype.h>
25 #include <math.h> // for fabs
26
27 #include "wx/gtk/private.h"
28 #include <gdk/gdkkeysyms.h>
29
30 //-----------------------------------------------------------------------------
31 // idle system
32 //-----------------------------------------------------------------------------
33
34 extern void wxapp_install_idle_handler();
35 extern bool g_isIdle;
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern bool g_blockEventsOnDrag;
42 extern wxCursor g_globalCursor;
43 extern wxWindowGTK *g_delayedFocus;
44
45 // ----------------------------------------------------------------------------
46 // helpers
47 // ----------------------------------------------------------------------------
48
49 #ifndef __WXGTK20__
50 static void wxGtkTextInsert(GtkWidget *text,
51 const wxTextAttr& attr,
52 const char *txt,
53 size_t len)
54 {
55 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
56 : NULL;
57
58 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
59 : NULL;
60
61 GdkColor *colBg = attr.HasBackgroundColour()
62 ? attr.GetBackgroundColour().GetColor()
63 : NULL;
64
65 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
66 }
67 #endif // GTK 1.x
68
69 // ----------------------------------------------------------------------------
70 // "insert_text" for GtkEntry
71 // ----------------------------------------------------------------------------
72
73 static void
74 gtk_insert_text_callback(GtkEditable *editable,
75 const gchar *new_text,
76 gint new_text_length,
77 gint *position,
78 wxTextCtrl *win)
79 {
80 if (g_isIdle)
81 wxapp_install_idle_handler();
82
83 // we should only be called if we have a max len limit at all
84 GtkEntry *entry = GTK_ENTRY (editable);
85
86 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
87
88 // check that we don't overflow the max length limit
89 //
90 // FIXME: this doesn't work when we paste a string which is going to be
91 // truncated
92 if ( entry->text_length == entry->text_max_length )
93 {
94 // we don't need to run the base class version at all
95 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
96
97 // remember that the next changed signal is to be ignored to avoid
98 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
99 win->IgnoreNextTextUpdate();
100
101 // and generate the correct one ourselves
102 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
103 event.SetEventObject(win);
104 event.SetString(win->GetValue());
105 win->GetEventHandler()->ProcessEvent( event );
106 }
107 }
108
109 //-----------------------------------------------------------------------------
110 // "changed"
111 //-----------------------------------------------------------------------------
112
113 static void
114 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
115 {
116 if ( win->IgnoreTextUpdate() )
117 return;
118
119 if (!win->m_hasVMT) return;
120
121 if (g_isIdle)
122 wxapp_install_idle_handler();
123
124 win->SetModified();
125 win->UpdateFontIfNeeded();
126
127 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
128 event.SetEventObject( win );
129 event.SetString( win->GetValue() );
130 win->GetEventHandler()->ProcessEvent( event );
131 }
132
133 //-----------------------------------------------------------------------------
134 // "changed" from vertical scrollbar
135 //-----------------------------------------------------------------------------
136
137 #ifndef __WXGTK20__
138 static void
139 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
140 {
141 if (!win->m_hasVMT) return;
142
143 if (g_isIdle)
144 wxapp_install_idle_handler();
145
146 win->CalculateScrollbar();
147 }
148 #endif
149
150 // ----------------------------------------------------------------------------
151 // redraw callback for multiline text
152 // ----------------------------------------------------------------------------
153
154 #ifndef __WXGTK20__
155
156 // redrawing a GtkText from inside a wxYield() call results in crashes (the
157 // text sample shows it in its "Add lines" command which shows wxProgressDialog
158 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
159 // don't do anything if we're inside wxYield()
160
161 extern bool wxIsInsideYield;
162
163 extern "C" {
164 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
165 }
166
167 static GtkDrawCallback gs_gtk_text_draw = NULL;
168
169 extern "C"
170 void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
171 {
172 if ( !wxIsInsideYield )
173 {
174 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
175 _T("infinite recursion in wxgtk_text_draw aborted") );
176
177 gs_gtk_text_draw(widget, rect);
178 }
179 }
180
181 #endif // __WXGTK20__
182
183 //-----------------------------------------------------------------------------
184 // wxTextCtrl
185 //-----------------------------------------------------------------------------
186
187 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
188
189 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
190 EVT_CHAR(wxTextCtrl::OnChar)
191
192 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
193 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
194 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
195 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
196 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
197
198 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
199 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
200 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
201 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
202 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
203 END_EVENT_TABLE()
204
205 void wxTextCtrl::Init()
206 {
207 m_ignoreNextUpdate =
208 m_modified = FALSE;
209 m_updateFont = FALSE;
210 m_text =
211 m_vScrollbar = (GtkWidget *)NULL;
212 }
213
214 wxTextCtrl::wxTextCtrl( wxWindow *parent,
215 wxWindowID id,
216 const wxString &value,
217 const wxPoint &pos,
218 const wxSize &size,
219 long style,
220 const wxValidator& validator,
221 const wxString &name )
222 {
223 Init();
224
225 Create( parent, id, value, pos, size, style, validator, name );
226 }
227
228 bool wxTextCtrl::Create( wxWindow *parent,
229 wxWindowID id,
230 const wxString &value,
231 const wxPoint &pos,
232 const wxSize &size,
233 long style,
234 const wxValidator& validator,
235 const wxString &name )
236 {
237 m_needParent = TRUE;
238 m_acceptsFocus = TRUE;
239
240 if (!PreCreation( parent, pos, size ) ||
241 !CreateBase( parent, id, pos, size, style, validator, name ))
242 {
243 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
244 return FALSE;
245 }
246
247
248 m_vScrollbarVisible = FALSE;
249
250 bool multi_line = (style & wxTE_MULTILINE) != 0;
251
252 #ifdef __WXGTK20__
253 GtkTextBuffer *buffer = NULL;
254 #endif
255
256 if (multi_line)
257 {
258 #ifdef __WXGTK20__
259 // Create view
260 m_text = gtk_text_view_new();
261
262 buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
263
264 // create scrolled window
265 m_widget = gtk_scrolled_window_new( NULL, NULL );
266 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
267 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
268
269 // Insert view into scrolled window
270 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
271
272 // Global settings which can be overridden by tags, I guess.
273 if (HasFlag( wxHSCROLL ))
274 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE );
275 else
276 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD );
277
278 if (!HasFlag(wxNO_BORDER))
279 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
280 #else
281 // create our control ...
282 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
283
284 // ... and put into the upper left hand corner of the table
285 bool bHasHScrollbar = FALSE;
286 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
287 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
288 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
289 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
290 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
291 0, 0);
292
293 // always wrap words
294 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
295
296 // finally, put the vertical scrollbar in the upper right corner
297 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
298 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
299 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
300 GTK_FILL,
301 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
302 0, 0);
303 #endif
304 }
305 else
306 {
307 // a single-line text control: no need for scrollbars
308 m_widget =
309 m_text = gtk_entry_new();
310 }
311
312 m_parent->DoAddChild( this );
313
314 m_focusWidget = m_text;
315
316 PostCreation();
317
318 SetFont( parent->GetFont() );
319
320 wxSize size_best( DoGetBestSize() );
321 wxSize new_size( size );
322 if (new_size.x == -1)
323 new_size.x = size_best.x;
324 if (new_size.y == -1)
325 new_size.y = size_best.y;
326 if ((new_size.x != size.x) || (new_size.y != size.y))
327 SetSize( new_size.x, new_size.y );
328
329 if (multi_line)
330 gtk_widget_show(m_text);
331
332 #ifndef __WXGTK20__
333 if (multi_line)
334 {
335 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
336 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
337
338 // only initialize gs_gtk_text_draw once, starting from the next the
339 // klass::draw will already be wxgtk_text_draw
340 if ( !gs_gtk_text_draw )
341 {
342 GtkDrawCallback&
343 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
344
345 gs_gtk_text_draw = draw;
346
347 draw = wxgtk_text_draw;
348 }
349 }
350 #endif // GTK+ 1.x
351
352 if (!value.IsEmpty())
353 {
354 #ifdef __WXGTK20__
355 SetValue( value );
356 #else
357
358 #if !GTK_CHECK_VERSION(1, 2, 0)
359 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
360 // gtk_editable_insert_text()
361 gtk_widget_realize(m_text);
362 #endif // GTK 1.0
363
364 gint tmp = 0;
365 #if wxUSE_UNICODE
366 wxWX2MBbuf val = value.mbc_str();
367 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
368 #else
369 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
370 #endif
371
372 if (multi_line)
373 {
374 // Bring editable's cursor uptodate. Bug in GTK.
375 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
376 }
377
378 #endif
379 }
380
381 if (style & wxTE_PASSWORD)
382 {
383 if (!multi_line)
384 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
385 }
386
387 if (style & wxTE_READONLY)
388 {
389 if (!multi_line)
390 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
391 #ifdef __WXGTK20__
392 else
393 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
394 }
395 #else
396 }
397 else
398 {
399 if (multi_line)
400 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
401 }
402 #endif
403
404 // We want to be notified about text changes.
405 #ifdef __WXGTK20__
406 if (multi_line)
407 {
408 g_signal_connect( G_OBJECT(buffer), "changed",
409 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
410 }
411 else
412 #endif
413 {
414 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
415 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
416 }
417
418 // we don't set a valid background colour, because the window
419 // manager should use a default one
420 m_backgroundColour = wxColour();
421
422 wxColour colFg = parent->GetForegroundColour();
423 SetForegroundColour( colFg );
424
425 m_cursor = wxCursor( wxCURSOR_IBEAM );
426
427 #ifndef __WXGTK20__
428 wxTextAttr attrDef( colFg, m_backgroundColour, parent->GetFont() );
429 SetDefaultStyle( attrDef );
430 #endif
431
432 Show( TRUE );
433
434 return TRUE;
435 }
436
437 void wxTextCtrl::CalculateScrollbar()
438 {
439 #ifndef __WXGTK20__
440 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
441
442 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
443
444 if (adj->upper - adj->page_size < 0.8)
445 {
446 if (m_vScrollbarVisible)
447 {
448 gtk_widget_hide( m_vScrollbar );
449 m_vScrollbarVisible = FALSE;
450 }
451 }
452 else
453 {
454 if (!m_vScrollbarVisible)
455 {
456 gtk_widget_show( m_vScrollbar );
457 m_vScrollbarVisible = TRUE;
458 }
459 }
460 #endif
461 }
462
463 wxString wxTextCtrl::GetValue() const
464 {
465 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
466
467 wxString tmp;
468 if (m_windowStyle & wxTE_MULTILINE)
469 {
470 #ifdef __WXGTK20__
471 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
472
473 GtkTextIter start;
474 gtk_text_buffer_get_start_iter( text_buffer, &start );
475 GtkTextIter end;
476 gtk_text_buffer_get_end_iter( text_buffer, &end );
477 gchar *text = gtk_text_buffer_get_text( text_buffer, &start, &end, TRUE );
478
479 #if wxUSE_UNICODE
480 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
481 #else
482 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
483 #endif
484 tmp = buffer;
485
486 g_free( text );
487 #else
488 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
489 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
490 tmp = text;
491 g_free( text );
492 #endif
493 }
494 else
495 {
496 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
497 }
498
499 return tmp;
500 }
501
502 void wxTextCtrl::SetValue( const wxString &value )
503 {
504 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
505
506 if (m_windowStyle & wxTE_MULTILINE)
507 {
508 #ifdef __WXGTK20__
509
510 #if wxUSE_UNICODE
511 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
512 #else
513 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
514 #endif
515 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
516 gtk_text_buffer_set_text( text_buffer, buffer, strlen(buffer) );
517
518 #else
519 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
520 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
521 len = 0;
522 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
523 #endif
524 }
525 else
526 {
527 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
528 }
529
530 // GRG, Jun/2000: Changed this after a lot of discussion in
531 // the lists. wxWindows 2.2 will have a set of flags to
532 // customize this behaviour.
533 SetInsertionPoint(0);
534
535 m_modified = FALSE;
536 }
537
538 void wxTextCtrl::WriteText( const wxString &text )
539 {
540 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
541
542 if ( text.empty() )
543 return;
544
545 if ( m_windowStyle & wxTE_MULTILINE )
546 {
547 #ifdef __WXGTK20__
548
549 #if wxUSE_UNICODE
550 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
551 #else
552 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
553 #endif
554 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
555
556 // TODO: Call whatever is needed to delete the selection.
557 gtk_text_buffer_insert_at_cursor( text_buffer, buffer, strlen(buffer) );
558
559 // Scroll to cursor.
560 GtkTextIter iter;
561 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, gtk_text_buffer_get_insert( text_buffer ) );
562 gtk_text_view_scroll_to_iter( GTK_TEXT_VIEW(m_text), &iter, 0.0, FALSE, 0.0, 1.0 );
563 #else // GTK 1.x
564 // After cursor movements, gtk_text_get_point() is wrong by one.
565 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
566
567 // always use m_defaultStyle, even if it is empty as otherwise
568 // resetting the style and appending some more text wouldn't work: if
569 // we don't specify the style explicitly, the old style would be used
570 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
571 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
572
573 // Bring editable's cursor back uptodate.
574 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
575 #endif // GTK 1.x/2.0
576 }
577 else // single line
578 {
579 // First remove the selection if there is one
580 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
581
582 // This moves the cursor pos to behind the inserted text.
583 gint len = GET_EDITABLE_POS(m_text);
584
585 #ifdef __WXGTK20__
586
587 #if wxUSE_UNICODE
588 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
589 #else
590 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
591 #endif
592 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
593
594 #else
595 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
596 #endif
597
598 // Bring entry's cursor uptodate.
599 gtk_entry_set_position( GTK_ENTRY(m_text), len );
600 }
601
602 m_modified = TRUE;
603 }
604
605 void wxTextCtrl::AppendText( const wxString &text )
606 {
607 SetInsertionPointEnd();
608 WriteText( text );
609 }
610
611 wxString wxTextCtrl::GetLineText( long lineNo ) const
612 {
613 if (m_windowStyle & wxTE_MULTILINE)
614 {
615 #ifndef __WXGTK20__
616 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
617 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
618
619 if (text)
620 {
621 wxString buf(wxT(""));
622 long i;
623 int currentLine = 0;
624 for (i = 0; currentLine != lineNo && text[i]; i++ )
625 if (text[i] == '\n')
626 currentLine++;
627 // Now get the text
628 int j;
629 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
630 buf += text[i];
631
632 g_free( text );
633 return buf;
634 }
635 else
636 #endif
637 {
638 return wxEmptyString;
639 }
640 }
641 else
642 {
643 if (lineNo == 0) return GetValue();
644 return wxEmptyString;
645 }
646 }
647
648 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
649 {
650 /* If you implement this, don't forget to update the documentation!
651 * (file docs/latex/wx/text.tex) */
652 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
653 }
654
655 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
656 {
657 if ( m_windowStyle & wxTE_MULTILINE )
658 {
659 wxString text = GetValue();
660
661 // cast to prevent warning. But pos really should've been unsigned.
662 if( (unsigned long)pos > text.Len() )
663 return FALSE;
664
665 *x=0; // First Col
666 *y=0; // First Line
667
668 const wxChar* stop = text.c_str() + pos;
669 for ( const wxChar *p = text.c_str(); p < stop; p++ )
670 {
671 if (*p == wxT('\n'))
672 {
673 (*y)++;
674 *x=0;
675 }
676 else
677 (*x)++;
678 }
679 }
680 else // single line control
681 {
682 if ( pos <= GTK_ENTRY(m_text)->text_length )
683 {
684 *y = 0;
685 *x = pos;
686 }
687 else
688 {
689 // index out of bounds
690 return FALSE;
691 }
692 }
693
694 return TRUE;
695 }
696
697 long wxTextCtrl::XYToPosition(long x, long y ) const
698 {
699 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
700
701 long pos=0;
702 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
703
704 pos += x;
705 return pos;
706 }
707
708 int wxTextCtrl::GetLineLength(long lineNo) const
709 {
710 wxString str = GetLineText (lineNo);
711 return (int) str.Length();
712 }
713
714 int wxTextCtrl::GetNumberOfLines() const
715 {
716 if (m_windowStyle & wxTE_MULTILINE)
717 {
718 #ifdef __WXGTK20__
719 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
720
721 return gtk_text_buffer_get_line_count( buffer );
722 #else
723 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
724 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
725
726 if (text)
727 {
728 int currentLine = 0;
729 for (int i = 0; i < len; i++ )
730 {
731 if (text[i] == '\n')
732 currentLine++;
733 }
734 g_free( text );
735
736 // currentLine is 0 based, add 1 to get number of lines
737 return currentLine + 1;
738 }
739 else
740 {
741 return 0;
742 }
743 #endif
744 }
745 else
746 {
747 return 1;
748 }
749 }
750
751 void wxTextCtrl::SetInsertionPoint( long pos )
752 {
753 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
754
755 if (m_windowStyle & wxTE_MULTILINE)
756 {
757 #ifdef __WXGTK20__
758 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
759 GtkTextIter iter;
760 gtk_text_buffer_get_iter_at_offset( text_buffer, &iter, pos );
761 gtk_text_buffer_place_cursor( text_buffer, &iter );
762 #else
763 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
764 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
765
766 /* we fake a set_point by inserting and deleting. as the user
767 isn't supposed to get to know about this non-sense, we
768 disconnect so that no events are sent to the user program. */
769
770 gint tmp = (gint)pos;
771 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
772 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
773
774 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
775 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
776
777 // bring editable's cursor uptodate. Bug in GTK.
778 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
779 #endif
780 }
781 else
782 {
783 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
784
785 // Bring editable's cursor uptodate. Bug in GTK.
786 SET_EDITABLE_POS(m_text, (guint32)pos);
787 }
788 }
789
790 void wxTextCtrl::SetInsertionPointEnd()
791 {
792 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
793
794 if (m_windowStyle & wxTE_MULTILINE)
795 {
796 #ifdef __WXGTK20__
797 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
798 GtkTextIter end;
799 gtk_text_buffer_get_end_iter( text_buffer, &end );
800 gtk_text_buffer_place_cursor( text_buffer, &end );
801 #else
802 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
803 #endif
804 }
805 else
806 {
807 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
808 }
809 }
810
811 void wxTextCtrl::SetEditable( bool editable )
812 {
813 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
814
815 if (m_windowStyle & wxTE_MULTILINE)
816 {
817 #ifdef __WXGTK20__
818 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
819 #else
820 gtk_text_set_editable( GTK_TEXT(m_text), editable );
821 #endif
822 }
823 else
824 {
825 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
826 }
827 }
828
829 bool wxTextCtrl::Enable( bool enable )
830 {
831 if (!wxWindowBase::Enable(enable))
832 {
833 // nothing to do
834 return FALSE;
835 }
836
837 if (m_windowStyle & wxTE_MULTILINE)
838 {
839 #ifdef __WXGTK20__
840 SetEditable( enable );
841 #else
842 gtk_text_set_editable( GTK_TEXT(m_text), enable );
843 OnParentEnable(enable);
844 #endif
845 }
846 else
847 {
848 gtk_widget_set_sensitive( m_text, enable );
849 }
850
851 return TRUE;
852 }
853
854 // wxGTK-specific: called recursively by Enable,
855 // to give widgets an oppprtunity to correct their colours after they
856 // have been changed by Enable
857 void wxTextCtrl::OnParentEnable( bool enable )
858 {
859 // If we have a custom background colour, we use this colour in both
860 // disabled and enabled mode, or we end up with a different colour under the
861 // text.
862 wxColour oldColour = GetBackgroundColour();
863 if (oldColour.Ok())
864 {
865 // Need to set twice or it'll optimize the useful stuff out
866 if (oldColour == * wxWHITE)
867 SetBackgroundColour(*wxBLACK);
868 else
869 SetBackgroundColour(*wxWHITE);
870 SetBackgroundColour(oldColour);
871 }
872 }
873
874 void wxTextCtrl::DiscardEdits()
875 {
876 m_modified = FALSE;
877 }
878
879 // ----------------------------------------------------------------------------
880 // max text length support
881 // ----------------------------------------------------------------------------
882
883 void wxTextCtrl::IgnoreNextTextUpdate()
884 {
885 m_ignoreNextUpdate = TRUE;
886 }
887
888 bool wxTextCtrl::IgnoreTextUpdate()
889 {
890 if ( m_ignoreNextUpdate )
891 {
892 m_ignoreNextUpdate = FALSE;
893
894 return TRUE;
895 }
896
897 return FALSE;
898 }
899
900 void wxTextCtrl::SetMaxLength(unsigned long len)
901 {
902 if ( !HasFlag(wxTE_MULTILINE) )
903 {
904 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
905
906 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
907 // we had tried to enter more text than allowed by max text length and
908 // the text wasn't really changed
909 //
910 // to detect this and generate TEXT_MAXLEN event instead of
911 // TEXT_CHANGED one in this case we also catch "insert_text" signal
912 //
913 // when max len is set to 0 we disconnect our handler as it means that
914 // we shouldn't check anything any more
915 if ( len )
916 {
917 gtk_signal_connect( GTK_OBJECT(m_text),
918 "insert_text",
919 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
920 (gpointer)this);
921 }
922 else // no checking
923 {
924 gtk_signal_disconnect_by_func
925 (
926 GTK_OBJECT(m_text),
927 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
928 (gpointer)this
929 );
930 }
931 }
932 }
933
934 void wxTextCtrl::SetSelection( long from, long to )
935 {
936 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
937
938 if (from == -1 && to == -1)
939 {
940 from = 0;
941 to = GetValue().Length();
942 }
943
944 #ifndef __WXGTK20__
945 if ( (m_windowStyle & wxTE_MULTILINE) &&
946 !GTK_TEXT(m_text)->line_start_cache )
947 {
948 // tell the programmer that it didn't work
949 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
950 return;
951 }
952 #endif
953
954 if (m_windowStyle & wxTE_MULTILINE)
955 {
956 #ifdef __WXGTK20__
957 // ????
958 #else
959 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
960 #endif
961 }
962 else
963 {
964 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
965 }
966 }
967
968 void wxTextCtrl::ShowPosition( long pos )
969 {
970 #ifndef __WXGTK20__
971 if (m_windowStyle & wxTE_MULTILINE)
972 {
973 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
974 float totalLines = (float) GetNumberOfLines();
975 long posX;
976 long posY;
977 PositionToXY(pos, &posX, &posY);
978 float posLine = (float) posY;
979 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
980 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
981 }
982 #endif
983 }
984
985 long wxTextCtrl::GetInsertionPoint() const
986 {
987 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
988
989 #ifdef __WXGTK20__
990 if (m_windowStyle & wxTE_MULTILINE)
991 {
992 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
993
994 // There is no direct accessor for the cursor, but
995 // internally, the cursor is the "mark" called
996 // "insert" in the text view's btree structure.
997
998 GtkTextMark *mark = gtk_text_buffer_get_insert( text_buffer );
999 GtkTextIter cursor;
1000 gtk_text_buffer_get_iter_at_mark( text_buffer, &cursor, mark );
1001
1002 return gtk_text_iter_get_offset( &cursor );
1003 }
1004 else
1005 #endif
1006 {
1007 return (long) GET_EDITABLE_POS(m_text);
1008 }
1009 }
1010
1011 long wxTextCtrl::GetLastPosition() const
1012 {
1013 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1014
1015 int pos = 0;
1016
1017 if (m_windowStyle & wxTE_MULTILINE)
1018 {
1019 #ifdef __WXGTK20__
1020 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1021 GtkTextIter end;
1022 gtk_text_buffer_get_end_iter( text_buffer, &end );
1023
1024 pos = gtk_text_iter_get_offset( &end );
1025 #else
1026 pos = gtk_text_get_length( GTK_TEXT(m_text) );
1027 #endif
1028 }
1029 else
1030 {
1031 pos = GTK_ENTRY(m_text)->text_length;
1032 }
1033
1034 return (long)pos;
1035 }
1036
1037 void wxTextCtrl::Remove( long from, long to )
1038 {
1039 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1040
1041 #ifndef __WXGTK20__
1042 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1043 #endif
1044 }
1045
1046 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1047 {
1048 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1049
1050 #ifndef __WXGTK20__
1051 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1052
1053 if (!value.IsEmpty())
1054 {
1055 gint pos = (gint)from;
1056 #if wxUSE_UNICODE
1057 wxWX2MBbuf buf = value.mbc_str();
1058 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
1059 #else
1060 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
1061 #endif
1062 }
1063 #endif
1064 }
1065
1066 void wxTextCtrl::Cut()
1067 {
1068 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1069
1070 #ifndef __WXGTK20__
1071 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
1072 #endif
1073 }
1074
1075 void wxTextCtrl::Copy()
1076 {
1077 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1078
1079 #ifndef __WXGTK20__
1080 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
1081 #endif
1082 }
1083
1084 void wxTextCtrl::Paste()
1085 {
1086 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1087
1088 #ifndef __WXGTK20__
1089 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
1090 #endif
1091 }
1092
1093 // Undo/redo
1094 void wxTextCtrl::Undo()
1095 {
1096 // TODO
1097 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1098 }
1099
1100 void wxTextCtrl::Redo()
1101 {
1102 // TODO
1103 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1104 }
1105
1106 bool wxTextCtrl::CanUndo() const
1107 {
1108 // TODO
1109 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1110 return FALSE;
1111 }
1112
1113 bool wxTextCtrl::CanRedo() const
1114 {
1115 // TODO
1116 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1117 return FALSE;
1118 }
1119
1120 // If the return values from and to are the same, there is no
1121 // selection.
1122 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1123 {
1124 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1125
1126 gint from, to;
1127 #ifdef __WXGTK20__
1128 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text), &from, &to) )
1129 #else
1130 if ( !(GTK_EDITABLE(m_text)->has_selection) )
1131 #endif
1132 {
1133 from =
1134 to = GetInsertionPoint();
1135 }
1136 else // got selection
1137 {
1138 #ifndef __WXGTK20__
1139 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1140 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1141 #endif
1142
1143 if ( from > to )
1144 {
1145 // exchange them to be compatible with wxMSW
1146 gint tmp = from;
1147 from = to;
1148 to = tmp;
1149 }
1150 }
1151
1152 if ( fromOut )
1153 *fromOut = from;
1154 if ( toOut )
1155 *toOut = to;
1156 }
1157
1158 bool wxTextCtrl::IsEditable() const
1159 {
1160 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1161
1162 #ifdef __WXGTK20__
1163 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1164 #else
1165 return GTK_EDITABLE(m_text)->editable;
1166 #endif
1167 }
1168
1169 bool wxTextCtrl::IsModified() const
1170 {
1171 return m_modified;
1172 }
1173
1174 void wxTextCtrl::Clear()
1175 {
1176 SetValue( wxT("") );
1177 }
1178
1179 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1180 {
1181 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1182
1183 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1184 {
1185 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1186 event.SetEventObject(this);
1187 event.SetString(GetValue());
1188 if (GetEventHandler()->ProcessEvent(event)) return;
1189 }
1190
1191 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1192 {
1193 // This will invoke the dialog default action, such
1194 // as the clicking the default button.
1195
1196 wxWindow *top_frame = m_parent;
1197 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1198 top_frame = top_frame->GetParent();
1199
1200 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1201 {
1202 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1203
1204 if (window->default_widget)
1205 {
1206 gtk_widget_activate (window->default_widget);
1207 return;
1208 }
1209 }
1210 }
1211
1212 key_event.Skip();
1213 }
1214
1215 GtkWidget* wxTextCtrl::GetConnectWidget()
1216 {
1217 return GTK_WIDGET(m_text);
1218 }
1219
1220 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1221 {
1222 if (m_windowStyle & wxTE_MULTILINE)
1223 {
1224 #ifdef __WXGTK20__
1225 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1226 #else
1227 return (window == GTK_TEXT(m_text)->text_area);
1228 #endif
1229 }
1230 else
1231 {
1232 return (window == GTK_ENTRY(m_text)->text_area);
1233 }
1234 }
1235
1236 // the font will change for subsequent text insertiongs
1237 bool wxTextCtrl::SetFont( const wxFont &font )
1238 {
1239 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1240
1241 if ( !wxTextCtrlBase::SetFont(font) )
1242 {
1243 // font didn't change, nothing to do
1244 return FALSE;
1245 }
1246
1247 if ( m_windowStyle & wxTE_MULTILINE )
1248 {
1249 m_updateFont = TRUE;
1250
1251 m_defaultStyle.SetFont(font);
1252
1253 ChangeFontGlobally();
1254 }
1255
1256 return TRUE;
1257 }
1258
1259 void wxTextCtrl::ChangeFontGlobally()
1260 {
1261 // this method is very inefficient and hence should be called as rarely as
1262 // possible!
1263 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1264 _T("shouldn't be called for single line controls") );
1265
1266 wxString value = GetValue();
1267 if ( !value.IsEmpty() )
1268 {
1269 m_updateFont = FALSE;
1270
1271 Clear();
1272 AppendText(value);
1273 }
1274 }
1275
1276 void wxTextCtrl::UpdateFontIfNeeded()
1277 {
1278 if ( m_updateFont )
1279 ChangeFontGlobally();
1280 }
1281
1282 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1283 {
1284 if ( !wxControl::SetForegroundColour(colour) )
1285 return FALSE;
1286
1287 // update default fg colour too
1288 m_defaultStyle.SetTextColour(colour);
1289
1290 return TRUE;
1291 }
1292
1293 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1294 {
1295 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1296
1297 if ( !wxControl::SetBackgroundColour( colour ) )
1298 return FALSE;
1299
1300 if (!m_widget->window)
1301 return FALSE;
1302
1303 wxColour sysbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
1304 if (sysbg.Red() == colour.Red() &&
1305 sysbg.Green() == colour.Green() &&
1306 sysbg.Blue() == colour.Blue())
1307 {
1308 return FALSE; // FIXME or TRUE?
1309 }
1310
1311 if (!m_backgroundColour.Ok())
1312 return FALSE;
1313
1314 if (m_windowStyle & wxTE_MULTILINE)
1315 {
1316 #ifndef __WXGTK20__
1317 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1318 if (!window)
1319 return FALSE;
1320 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1321 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1322 gdk_window_clear( window );
1323 #endif
1324 }
1325
1326 // change active background color too
1327 m_defaultStyle.SetBackgroundColour( colour );
1328
1329 return TRUE;
1330 }
1331
1332 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1333 {
1334 /* VERY dirty way to do that - removes the required text and re-adds it
1335 with styling (FIXME) */
1336 if ( m_windowStyle & wxTE_MULTILINE )
1337 {
1338 #ifndef __WXGTK20__
1339 if ( style.IsDefault() )
1340 {
1341 // nothing to do
1342 return TRUE;
1343 }
1344
1345 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1346
1347 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1348 _T("invalid range in wxTextCtrl::SetStyle") );
1349
1350 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1351 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1352 wxString tmp(text,*wxConvCurrent);
1353 g_free( text );
1354
1355 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1356 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1357
1358 #if wxUSE_UNICODE
1359 wxWX2MBbuf buf = tmp.mbc_str();
1360 const char *txt = buf;
1361 size_t txtlen = strlen(buf);
1362 #else
1363 const char *txt = tmp;
1364 size_t txtlen = tmp.length();
1365 #endif
1366
1367 // use the attributes from style which are set in it and fall back
1368 // first to the default style and then to the text control default
1369 // colours for the others
1370 wxGtkTextInsert(m_text,
1371 wxTextAttr::Combine(style, m_defaultStyle, this),
1372 txt,
1373 txtlen);
1374
1375 /* does not seem to help under GTK+ 1.2 !!!
1376 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1377 SetInsertionPoint( old_pos );
1378 #endif
1379 return TRUE;
1380 }
1381 else // singe line
1382 {
1383 // cannot do this for GTK+'s Entry widget
1384 return FALSE;
1385 }
1386 }
1387
1388 void wxTextCtrl::ApplyWidgetStyle()
1389 {
1390 if (m_windowStyle & wxTE_MULTILINE)
1391 {
1392 // how ?
1393 }
1394 else
1395 {
1396 SetWidgetStyle();
1397 gtk_widget_set_style( m_text, m_widgetStyle );
1398 }
1399 }
1400
1401 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1402 {
1403 Cut();
1404 }
1405
1406 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1407 {
1408 Copy();
1409 }
1410
1411 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1412 {
1413 Paste();
1414 }
1415
1416 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1417 {
1418 Undo();
1419 }
1420
1421 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1422 {
1423 Redo();
1424 }
1425
1426 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1427 {
1428 event.Enable( CanCut() );
1429 }
1430
1431 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1432 {
1433 event.Enable( CanCopy() );
1434 }
1435
1436 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1437 {
1438 event.Enable( CanPaste() );
1439 }
1440
1441 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1442 {
1443 event.Enable( CanUndo() );
1444 }
1445
1446 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1447 {
1448 event.Enable( CanRedo() );
1449 }
1450
1451 void wxTextCtrl::OnInternalIdle()
1452 {
1453 wxCursor cursor = m_cursor;
1454 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1455
1456 if (cursor.Ok())
1457 {
1458 #ifndef __WXGTK20__
1459 GdkWindow *window = (GdkWindow*) NULL;
1460 if (HasFlag(wxTE_MULTILINE))
1461 window = GTK_TEXT(m_text)->text_area;
1462 else
1463 window = GTK_ENTRY(m_text)->text_area;
1464
1465 if (window)
1466 gdk_window_set_cursor( window, cursor.GetCursor() );
1467
1468 if (!g_globalCursor.Ok())
1469 cursor = *wxSTANDARD_CURSOR;
1470
1471 window = m_widget->window;
1472 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1473 gdk_window_set_cursor( window, cursor.GetCursor() );
1474 #endif
1475 }
1476
1477 if (g_delayedFocus == this)
1478 {
1479 if (GTK_WIDGET_REALIZED(m_widget))
1480 {
1481 gtk_widget_grab_focus( m_widget );
1482 g_delayedFocus = NULL;
1483 }
1484 }
1485
1486 UpdateWindowUI();
1487 }
1488
1489 wxSize wxTextCtrl::DoGetBestSize() const
1490 {
1491 // FIXME should be different for multi-line controls...
1492 wxSize ret( wxControl::DoGetBestSize() );
1493 return wxSize(80, ret.y);
1494 }
1495
1496 // ----------------------------------------------------------------------------
1497 // freeze/thaw
1498 // ----------------------------------------------------------------------------
1499
1500 void wxTextCtrl::Freeze()
1501 {
1502 #ifndef __WXGTK20__
1503 if ( HasFlag(wxTE_MULTILINE) )
1504 {
1505 gtk_text_freeze(GTK_TEXT(m_text));
1506 }
1507 #endif
1508 }
1509
1510 void wxTextCtrl::Thaw()
1511 {
1512 #ifndef __WXGTK20__
1513 if ( HasFlag(wxTE_MULTILINE) )
1514 {
1515 GTK_TEXT(m_text)->vadj->value = 0.0;
1516
1517 gtk_text_thaw(GTK_TEXT(m_text));
1518 }
1519 #endif
1520 }
1521
1522 // ----------------------------------------------------------------------------
1523 // scrolling
1524 // ----------------------------------------------------------------------------
1525
1526 GtkAdjustment *wxTextCtrl::GetVAdj() const
1527 {
1528 #ifdef __WXGTK20__
1529 return NULL;
1530 #else
1531 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1532 #endif
1533 }
1534
1535 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1536 {
1537 #ifndef __WXGTK20__
1538 float value = adj->value + diff;
1539
1540 if ( value < 0 )
1541 value = 0;
1542
1543 float upper = adj->upper - adj->page_size;
1544 if ( value > upper )
1545 value = upper;
1546
1547 // did we noticeably change the scroll position?
1548 if ( fabs(adj->value - value) < 0.2 )
1549 {
1550 // well, this is what Robert does in wxScrollBar, so it must be good...
1551 return FALSE;
1552 }
1553
1554 adj->value = value;
1555
1556 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1557
1558 #endif
1559 return TRUE;
1560 }
1561
1562 bool wxTextCtrl::ScrollLines(int lines)
1563 {
1564 #ifdef __WXGTK20__
1565 return FALSE;
1566 #else
1567 GtkAdjustment *adj = GetVAdj();
1568 if ( !adj )
1569 return FALSE;
1570
1571 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1572 static const int KEY_SCROLL_PIXELS = 10;
1573
1574 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1575 #endif
1576 }
1577
1578 bool wxTextCtrl::ScrollPages(int pages)
1579 {
1580 #ifdef __WXGTK20__
1581 return FALSE;
1582 #else
1583 GtkAdjustment *adj = GetVAdj();
1584 if ( !adj )
1585 return FALSE;
1586
1587 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1588 #endif
1589 }
1590