1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "textctrl.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/textctrl.h"
21 #include "wx/settings.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
26 #include <sys/types.h>
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern wxCursor g_globalCursor
;
46 extern wxWindowGTK
*g_delayedFocus
;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
54 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
61 g_object_get (tag
, "name", &name
, NULL
);
63 if (!name
|| strncmp(name
, "WX", 2)) // anonymous tag or not starting with "WX"
64 g_signal_stop_emission_by_name(buffer
, "remove_tag");
71 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer
*text_buffer
,
72 const wxTextAttr
& attr
,
76 static gchar buf
[1024];
79 gulong remove_handler_id
= g_signal_connect( text_buffer
, "remove_tag",
80 G_CALLBACK(wxGtkOnRemoveTag
), NULL
);
81 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
82 g_signal_handler_disconnect( text_buffer
, remove_handler_id
);
87 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
88 font_string
= pango_font_description_to_string(font_description
);
89 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
);
90 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
93 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
94 "font-desc", font_description
,
96 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
100 if (attr
.HasTextColour())
102 GdkColor
*colFg
= attr
.GetTextColour().GetColor();
103 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
104 colFg
->red
, colFg
->green
, colFg
->blue
);
105 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
108 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
109 "foreground-gdk", colFg
, NULL
);
110 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
113 if (attr
.HasBackgroundColour())
115 GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
116 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
117 colBg
->red
, colBg
->green
, colBg
->blue
);
118 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
121 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
122 "background-gdk", colBg
, NULL
);
123 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
129 static void wxGtkTextInsert(GtkWidget
*text
,
130 GtkTextBuffer
*text_buffer
,
131 const wxTextAttr
& attr
,
136 GtkTextIter iter
, start
;
138 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
139 gtk_text_buffer_get_insert (text_buffer
) );
140 start_offset
= gtk_text_iter_get_offset (&iter
);
141 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
143 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
145 wxGtkTextApplyTagsFromAttr(text_buffer
, attr
, &start
, &iter
);
150 static void wxGtkTextInsert(GtkWidget
*text
,
151 const wxTextAttr
& attr
,
155 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
158 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
161 GdkColor
*colBg
= attr
.HasBackgroundColour()
162 ? attr
.GetBackgroundColour().GetColor()
165 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
170 // ----------------------------------------------------------------------------
171 // "insert_text" for GtkEntry
172 // ----------------------------------------------------------------------------
176 gtk_insert_text_callback(GtkEditable
*editable
,
177 const gchar
*new_text
,
178 gint new_text_length
,
183 wxapp_install_idle_handler();
185 // we should only be called if we have a max len limit at all
186 GtkEntry
*entry
= GTK_ENTRY (editable
);
188 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
190 // check that we don't overflow the max length limit
192 // FIXME: this doesn't work when we paste a string which is going to be
194 if ( entry
->text_length
== entry
->text_max_length
)
196 // we don't need to run the base class version at all
197 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
199 // remember that the next changed signal is to be ignored to avoid
200 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
201 win
->IgnoreNextTextUpdate();
203 // and generate the correct one ourselves
204 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
205 event
.SetEventObject(win
);
206 event
.SetString(win
->GetValue());
207 win
->GetEventHandler()->ProcessEvent( event
);
213 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
217 au_apply_tag_callback(GtkTextBuffer
*buffer
,
223 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
224 g_signal_stop_emission_by_name(buffer
, "apply_tag");
228 //-----------------------------------------------------------------------------
229 // GtkTextCharPredicates for gtk_text_iter_*_find_char
230 //-----------------------------------------------------------------------------
234 pred_whitespace (gunichar ch
, gpointer user_data
)
236 return g_unichar_isspace(ch
);
242 pred_non_whitespace (gunichar ch
, gpointer user_data
)
244 return !g_unichar_isspace(ch
);
250 pred_nonpunct (gunichar ch
, gpointer user_data
)
252 return !g_unichar_ispunct(ch
);
258 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
260 return !g_unichar_ispunct(ch
) || ch
== '/';
264 //-----------------------------------------------------------------------------
265 // Check for links between s and e and correct tags as necessary
266 //-----------------------------------------------------------------------------
268 // This function should be made match better while being efficient at one point.
269 // Most probably with a row of regular expressions.
272 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
274 static const char *URIPrefixes
[] =
292 GtkTextIter start
= *s
, end
= *e
;
293 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
295 // Get our special link tag
296 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
298 // Get rid of punctuation from beginning and end.
299 // Might want to move this to au_check_range if an improved link checking doesn't
300 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
301 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
302 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
304 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
305 gtk_text_iter_forward_char(&end
);
307 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
308 size_t len
= strlen(text
), prefix_len
;
311 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
313 prefix_len
= strlen(URIPrefixes
[n
]);
314 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
318 if(n
< WXSIZEOF(URIPrefixes
))
320 gulong signal_id
= g_signal_handler_find(buffer
,
321 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
323 (gpointer
)au_apply_tag_callback
, NULL
);
325 g_signal_handler_block(buffer
, signal_id
);
326 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
327 g_signal_handler_unblock(buffer
, signal_id
);
334 au_check_range(GtkTextIter
*s
,
335 GtkTextIter
*range_end
)
337 GtkTextIter range_start
= *s
;
338 GtkTextIter word_end
;
339 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
340 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
342 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
344 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
345 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
347 while(!gtk_text_iter_equal(&range_start
, range_end
))
349 word_end
= range_start
;
350 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
352 // Now we should have a word delimited by range_start and word_end, correct link tags
353 au_check_word(&range_start
, &word_end
);
355 range_start
= word_end
;
356 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
361 //-----------------------------------------------------------------------------
362 // "insert-text" for GtkTextBuffer
363 //-----------------------------------------------------------------------------
367 au_insert_text_callback(GtkTextBuffer
*buffer
,
373 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
376 GtkTextIter start
= *end
;
377 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
379 GtkTextIter line_start
= start
;
380 GtkTextIter line_end
= *end
;
381 GtkTextIter words_start
= start
;
382 GtkTextIter words_end
= *end
;
384 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
385 gtk_text_iter_forward_to_line_end(&line_end
);
386 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
387 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
389 au_check_range(&words_start
, &words_end
);
393 //-----------------------------------------------------------------------------
394 // "delete-range" for GtkTextBuffer
395 //-----------------------------------------------------------------------------
399 au_delete_range_callback(GtkTextBuffer
*buffer
,
404 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
407 GtkTextIter line_start
= *start
, line_end
= *end
;
409 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
410 gtk_text_iter_forward_to_line_end(&line_end
);
411 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
412 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
414 au_check_range(start
, end
);
421 //-----------------------------------------------------------------------------
423 //-----------------------------------------------------------------------------
427 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
429 if ( win
->IgnoreTextUpdate() )
432 if (!win
->m_hasVMT
) return;
435 wxapp_install_idle_handler();
439 win
->UpdateFontIfNeeded();
440 #endif // !__WXGTK20__
442 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
443 event
.SetEventObject( win
);
444 win
->GetEventHandler()->ProcessEvent( event
);
448 //-----------------------------------------------------------------------------
449 // "expose_event" from scrolled window and textview
450 //-----------------------------------------------------------------------------
455 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
462 //-----------------------------------------------------------------------------
463 // "changed" from vertical scrollbar
464 //-----------------------------------------------------------------------------
469 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
471 if (!win
->m_hasVMT
) return;
474 wxapp_install_idle_handler();
476 win
->CalculateScrollbar();
481 // ----------------------------------------------------------------------------
482 // redraw callback for multiline text
483 // ----------------------------------------------------------------------------
487 // redrawing a GtkText from inside a wxYield() call results in crashes (the
488 // text sample shows it in its "Add lines" command which shows wxProgressDialog
489 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
490 // don't do anything if we're inside wxYield()
492 extern bool wxIsInsideYield
;
495 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
498 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
501 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
503 if ( !wxIsInsideYield
)
505 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
506 _T("infinite recursion in wxgtk_text_draw aborted") );
508 gs_gtk_text_draw(widget
, rect
);
513 #endif // __WXGTK20__
515 //-----------------------------------------------------------------------------
517 //-----------------------------------------------------------------------------
519 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
521 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
522 EVT_CHAR(wxTextCtrl::OnChar
)
524 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
525 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
526 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
527 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
528 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
530 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
531 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
532 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
533 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
534 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
537 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
538 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
539 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
540 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
541 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
542 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
543 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
544 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
545 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
549 void wxTextCtrl::Init()
553 SetUpdateFont(false);
555 m_vScrollbar
= (GtkWidget
*)NULL
;
558 m_gdkHandCursor
= NULL
;
559 m_gdkXTermCursor
= NULL
;
563 wxTextCtrl::~wxTextCtrl()
567 gdk_cursor_unref(m_gdkHandCursor
);
569 gdk_cursor_unref(m_gdkXTermCursor
);
573 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
575 const wxString
&value
,
579 const wxValidator
& validator
,
580 const wxString
&name
)
584 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
587 bool wxTextCtrl::Create( wxWindow
*parent
,
589 const wxString
&value
,
593 const wxValidator
& validator
,
594 const wxString
&name
)
597 m_acceptsFocus
= true;
599 if (!PreCreation( parent
, pos
, size
) ||
600 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
602 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
607 m_vScrollbarVisible
= false;
609 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
615 m_text
= gtk_text_view_new();
617 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
619 // create scrolled window
620 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
621 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
622 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
624 // Insert view into scrolled window
625 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
627 // translate wx wrapping style to GTK+
629 if ( HasFlag( wxTE_DONTWRAP
) )
630 wrap
= GTK_WRAP_NONE
;
631 else if ( HasFlag( wxTE_CHARWRAP
) )
632 wrap
= GTK_WRAP_CHAR
;
633 else if ( HasFlag( wxTE_WORDWRAP
) )
634 wrap
= GTK_WRAP_WORD
;
635 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
637 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
639 if ( !gtk_check_version(2,4,0) )
641 wrap
= GTK_WRAP_WORD_CHAR
;
645 wrap
= GTK_WRAP_WORD
;
648 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
650 if (!HasFlag(wxNO_BORDER
))
651 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget
), GTK_SHADOW_IN
);
653 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
655 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
657 // create our control ...
658 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
660 // ... and put into the upper left hand corner of the table
661 bool bHasHScrollbar
= false;
662 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
663 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
664 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
665 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
666 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
670 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
672 // finally, put the vertical scrollbar in the upper right corner
673 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
674 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
675 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
677 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
683 // a single-line text control: no need for scrollbars
685 m_text
= gtk_entry_new();
688 if (style
& wxNO_BORDER
)
689 g_object_set( GTK_ENTRY(m_text
), "has-frame", FALSE
, NULL
);
693 m_parent
->DoAddChild( this );
695 m_focusWidget
= m_text
;
700 gtk_widget_show(m_text
);
705 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
706 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
708 // only initialize gs_gtk_text_draw once, starting from the next the
709 // klass::draw will already be wxgtk_text_draw
710 if ( !gs_gtk_text_draw
)
713 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
715 gs_gtk_text_draw
= draw
;
717 draw
= wxgtk_text_draw
;
728 #if !GTK_CHECK_VERSION(1, 2, 0)
729 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
730 // gtk_editable_insert_text()
731 gtk_widget_realize(m_text
);
736 wxWX2MBbuf val
= value
.mbc_str();
737 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
739 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
744 // Bring editable's cursor uptodate. Bug in GTK.
745 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
751 if (style
& wxTE_PASSWORD
)
754 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
757 if (style
& wxTE_READONLY
)
760 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
763 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
769 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
776 if (style
& wxTE_RIGHT
)
777 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_RIGHT
);
778 else if (style
& wxTE_CENTRE
)
779 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_CENTER
);
780 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
785 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
786 if (!gtk_check_version(2,4,0))
788 if (style
& wxTE_RIGHT
)
789 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 1.0 );
790 else if (style
& wxTE_CENTRE
)
791 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 0.5 );
795 #endif // __WXGTK20__
797 // We want to be notified about text changes.
801 g_signal_connect( G_OBJECT(m_buffer
), "changed",
802 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
804 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
805 if (style
& wxTE_AUTO_URL
)
807 GtkTextIter start
, end
;
808 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
809 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
811 // We create our wxUrl tag here for slight efficiency gain - we
812 // don't have to check for the tag existance in callbacks,
813 // hereby it's guaranteed to exist.
814 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
815 "foreground", "blue",
816 "underline", PANGO_UNDERLINE_SINGLE
,
819 // Check for URLs after each text change
820 g_signal_connect_after( G_OBJECT(m_buffer
), "insert_text",
821 GTK_SIGNAL_FUNC(au_insert_text_callback
), (gpointer
)this);
822 g_signal_connect_after( G_OBJECT(m_buffer
), "delete_range",
823 GTK_SIGNAL_FUNC(au_delete_range_callback
), (gpointer
)this);
825 // Block all wxUrl tag applying unless we do it ourselves, in which case we
826 // block this callback temporarily. This takes care of gtk+ internal
827 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
828 // which is undesired because only a part of the URL might be copied.
829 // The insert-text signal emitted inside it will take care of newly formed
830 // or wholly copied URLs.
831 g_signal_connect( G_OBJECT(m_buffer
), "apply_tag",
832 GTK_SIGNAL_FUNC(au_apply_tag_callback
), NULL
);
834 // Check for URLs in the initial string passed to Create
835 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
836 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
837 au_check_range(&start
, &end
);
844 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
845 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
848 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
850 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
851 SetDefaultStyle( attrDef
);
857 void wxTextCtrl::CalculateScrollbar()
860 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
862 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
864 if (adj
->upper
- adj
->page_size
< 0.8)
866 if (m_vScrollbarVisible
)
868 gtk_widget_hide( m_vScrollbar
);
869 m_vScrollbarVisible
= false;
874 if (!m_vScrollbarVisible
)
876 gtk_widget_show( m_vScrollbar
);
877 m_vScrollbarVisible
= true;
883 wxString
wxTextCtrl::GetValue() const
885 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
888 if (m_windowStyle
& wxTE_MULTILINE
)
892 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
894 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
895 gchar
*text
= gtk_text_buffer_get_text( m_buffer
, &start
, &end
, TRUE
);
898 wxWCharBuffer
buffer( wxConvUTF8
.cMB2WX( text
) );
900 wxCharBuffer
buffer( wxConvLocal
.cWC2WX( wxConvUTF8
.cMB2WC( text
) ) );
907 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
908 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
915 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
921 void wxTextCtrl::SetValue( const wxString
&value
)
923 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
925 if (m_windowStyle
& wxTE_MULTILINE
)
930 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( value
) );
932 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( value
) ) );
934 if (gtk_text_buffer_get_char_count(m_buffer
) != 0)
935 IgnoreNextTextUpdate();
939 // what else can we do? at least don't crash...
943 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
946 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
947 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
949 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
954 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
957 // GRG, Jun/2000: Changed this after a lot of discussion in
958 // the lists. wxWidgets 2.2 will have a set of flags to
959 // customize this behaviour.
960 SetInsertionPoint(0);
965 void wxTextCtrl::WriteText( const wxString
&text
)
967 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
972 // gtk_text_changed_callback() will set m_modified to true but m_modified
973 // shouldn't be changed by the program writing to the text control itself,
974 // so save the old value and restore when we're done
975 bool oldModified
= m_modified
;
977 if ( m_windowStyle
& wxTE_MULTILINE
)
982 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
984 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
988 // what else can we do? at least don't crash...
992 // TODO: Call whatever is needed to delete the selection.
993 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
995 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
996 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
997 if ( adj
->value
== adj
->upper
- adj
->page_size
)
999 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1000 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1003 // After cursor movements, gtk_text_get_point() is wrong by one.
1004 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
1006 // always use m_defaultStyle, even if it is empty as otherwise
1007 // resetting the style and appending some more text wouldn't work: if
1008 // we don't specify the style explicitly, the old style would be used
1009 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1010 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.Len());
1012 // we called wxGtkTextInsert with correct font, no need to do anything
1013 // in UpdateFontIfNeeded() any longer
1014 if ( !text
.empty() )
1016 SetUpdateFont(false);
1019 // Bring editable's cursor back uptodate.
1020 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
1021 #endif // GTK 1.x/2.0
1025 // First remove the selection if there is one
1026 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1028 // This moves the cursor pos to behind the inserted text.
1029 gint len
= GET_EDITABLE_POS(m_text
);
1034 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
1036 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
1040 // what else can we do? at least don't crash...
1044 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1047 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.Len(), &len
);
1050 // Bring entry's cursor uptodate.
1051 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
1054 m_modified
= oldModified
;
1057 void wxTextCtrl::AppendText( const wxString
&text
)
1059 SetInsertionPointEnd();
1063 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1065 if (m_windowStyle
& wxTE_MULTILINE
)
1068 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
1069 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
1075 int currentLine
= 0;
1076 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
1077 if (text
[i
] == '\n')
1081 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
1089 return wxEmptyString
;
1093 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1094 GtkTextIter end
= line
;
1095 gtk_text_iter_forward_to_line_end(&end
);
1096 gchar
*text
= gtk_text_buffer_get_text(m_buffer
,&line
,&end
,TRUE
);
1097 wxString
result(wxGTK_CONV_BACK(text
));
1104 if (lineNo
== 0) return GetValue();
1105 return wxEmptyString
;
1109 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1111 /* If you implement this, don't forget to update the documentation!
1112 * (file docs/latex/wx/text.tex) */
1113 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1116 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1118 if ( m_windowStyle
& wxTE_MULTILINE
)
1122 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1123 if (gtk_text_iter_is_end(&iter
))
1126 *y
= gtk_text_iter_get_line(&iter
);
1127 *x
= gtk_text_iter_get_line_offset(&iter
);
1129 wxString text
= GetValue();
1131 // cast to prevent warning. But pos really should've been unsigned.
1132 if( (unsigned long)pos
> text
.Len() )
1138 const wxChar
* stop
= text
.c_str() + pos
;
1139 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
1141 if (*p
== wxT('\n'))
1151 else // single line control
1153 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1160 // index out of bounds
1168 long wxTextCtrl::XYToPosition(long x
, long y
) const
1170 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
1174 gtk_text_buffer_get_iter_at_line_offset(m_buffer
, &iter
, y
, x
);
1175 return gtk_text_iter_get_offset(&iter
);
1178 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
1185 int wxTextCtrl::GetLineLength(long lineNo
) const
1188 if (m_windowStyle
& wxTE_MULTILINE
)
1190 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1191 if (lineNo
> last_line
)
1195 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1196 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1197 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1202 wxString str
= GetLineText (lineNo
);
1203 return (int) str
.Length();
1207 int wxTextCtrl::GetNumberOfLines() const
1209 if (m_windowStyle
& wxTE_MULTILINE
)
1212 return gtk_text_buffer_get_line_count( m_buffer
);
1214 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
1215 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
1219 int currentLine
= 0;
1220 for (int i
= 0; i
< len
; i
++ )
1222 if (text
[i
] == '\n')
1227 // currentLine is 0 based, add 1 to get number of lines
1228 return currentLine
+ 1;
1242 void wxTextCtrl::SetInsertionPoint( long pos
)
1244 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1246 if ( IsMultiLine() )
1250 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1251 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1252 gtk_text_view_scroll_mark_onscreen
1254 GTK_TEXT_VIEW(m_text
),
1255 gtk_text_buffer_get_insert( m_buffer
)
1258 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
1259 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
1261 /* we fake a set_point by inserting and deleting. as the user
1262 isn't supposed to get to know about this non-sense, we
1263 disconnect so that no events are sent to the user program. */
1265 gint tmp
= (gint
)pos
;
1266 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
1267 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
1269 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
1270 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
1272 // bring editable's cursor uptodate. Bug in GTK.
1273 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
1278 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
1280 // Bring editable's cursor uptodate. Bug in GTK.
1281 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
1285 void wxTextCtrl::SetInsertionPointEnd()
1287 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1289 if (m_windowStyle
& wxTE_MULTILINE
)
1293 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1294 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1296 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
1301 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
1305 void wxTextCtrl::SetEditable( bool editable
)
1307 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1309 if (m_windowStyle
& wxTE_MULTILINE
)
1312 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1314 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
1319 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
1323 bool wxTextCtrl::Enable( bool enable
)
1325 if (!wxWindowBase::Enable(enable
))
1331 if (m_windowStyle
& wxTE_MULTILINE
)
1334 SetEditable( enable
);
1336 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
1337 OnParentEnable(enable
);
1342 gtk_widget_set_sensitive( m_text
, enable
);
1348 // wxGTK-specific: called recursively by Enable,
1349 // to give widgets an oppprtunity to correct their colours after they
1350 // have been changed by Enable
1351 void wxTextCtrl::OnParentEnable( bool enable
)
1353 // If we have a custom background colour, we use this colour in both
1354 // disabled and enabled mode, or we end up with a different colour under the
1356 wxColour oldColour
= GetBackgroundColour();
1359 // Need to set twice or it'll optimize the useful stuff out
1360 if (oldColour
== * wxWHITE
)
1361 SetBackgroundColour(*wxBLACK
);
1363 SetBackgroundColour(*wxWHITE
);
1364 SetBackgroundColour(oldColour
);
1368 void wxTextCtrl::MarkDirty()
1373 void wxTextCtrl::DiscardEdits()
1378 // ----------------------------------------------------------------------------
1379 // max text length support
1380 // ----------------------------------------------------------------------------
1382 void wxTextCtrl::IgnoreNextTextUpdate()
1384 m_ignoreNextUpdate
= true;
1387 bool wxTextCtrl::IgnoreTextUpdate()
1389 if ( m_ignoreNextUpdate
)
1391 m_ignoreNextUpdate
= false;
1399 void wxTextCtrl::SetMaxLength(unsigned long len
)
1401 if ( !HasFlag(wxTE_MULTILINE
) )
1403 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1405 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1406 // we had tried to enter more text than allowed by max text length and
1407 // the text wasn't really changed
1409 // to detect this and generate TEXT_MAXLEN event instead of
1410 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1412 // when max len is set to 0 we disconnect our handler as it means that
1413 // we shouldn't check anything any more
1416 gtk_signal_connect( GTK_OBJECT(m_text
),
1418 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
1423 gtk_signal_disconnect_by_func
1426 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
1433 void wxTextCtrl::SetSelection( long from
, long to
)
1435 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1437 if (from
== -1 && to
== -1)
1440 to
= GetValue().Length();
1444 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
1445 !GTK_TEXT(m_text
)->line_start_cache
)
1447 // tell the programmer that it didn't work
1448 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1453 if (m_windowStyle
& wxTE_MULTILINE
)
1456 GtkTextIter fromi
, toi
;
1457 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1458 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1460 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1461 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1463 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1468 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1472 void wxTextCtrl::ShowPosition( long pos
)
1474 if (m_windowStyle
& wxTE_MULTILINE
)
1478 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1479 gtk_text_iter_set_offset( &iter
, pos
);
1480 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1481 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1483 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
1484 float totalLines
= (float) GetNumberOfLines();
1487 PositionToXY(pos
, &posX
, &posY
);
1488 float posLine
= (float) posY
;
1489 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
1490 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
1491 #endif // GTK 1.x/2.x
1497 wxTextCtrlHitTestResult
1498 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1500 if ( !IsMultiLine() )
1503 return wxTE_HT_UNKNOWN
;
1507 gtk_text_view_window_to_buffer_coords
1509 GTK_TEXT_VIEW(m_text
),
1510 GTK_TEXT_WINDOW_TEXT
,
1516 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1518 *pos
= gtk_text_iter_get_offset(&iter
);
1520 return wxTE_HT_ON_TEXT
;
1523 #endif // __WXGTK20__
1525 long wxTextCtrl::GetInsertionPoint() const
1527 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1530 if (m_windowStyle
& wxTE_MULTILINE
)
1532 // There is no direct accessor for the cursor, but
1533 // internally, the cursor is the "mark" called
1534 // "insert" in the text view's btree structure.
1536 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1538 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1540 return gtk_text_iter_get_offset( &cursor
);
1545 return (long) GET_EDITABLE_POS(m_text
);
1549 wxTextPos
wxTextCtrl::GetLastPosition() const
1551 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1555 if (m_windowStyle
& wxTE_MULTILINE
)
1559 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1561 pos
= gtk_text_iter_get_offset( &end
);
1563 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
1568 pos
= GTK_ENTRY(m_text
)->text_length
;
1574 void wxTextCtrl::Remove( long from
, long to
)
1576 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1579 if (m_windowStyle
& wxTE_MULTILINE
)
1581 GtkTextIter fromi
, toi
;
1582 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1583 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1585 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1589 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1592 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1594 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1601 SetInsertionPoint( from
);
1604 gint pos
= (gint
)from
;
1606 wxWX2MBbuf buf
= value
.mbc_str();
1607 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
1609 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
1610 #endif // wxUSE_UNICODE
1611 #endif // GTK 1.x/2.x
1615 void wxTextCtrl::Cut()
1617 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1620 if (m_windowStyle
& wxTE_MULTILINE
)
1621 g_signal_emit_by_name(m_text
, "cut-clipboard");
1624 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1627 void wxTextCtrl::Copy()
1629 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1632 if (m_windowStyle
& wxTE_MULTILINE
)
1633 g_signal_emit_by_name(m_text
, "copy-clipboard");
1636 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1639 void wxTextCtrl::Paste()
1641 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1644 if (m_windowStyle
& wxTE_MULTILINE
)
1645 g_signal_emit_by_name(m_text
, "paste-clipboard");
1648 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1652 void wxTextCtrl::Undo()
1655 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1658 void wxTextCtrl::Redo()
1661 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1664 bool wxTextCtrl::CanUndo() const
1667 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1671 bool wxTextCtrl::CanRedo() const
1674 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1678 // If the return values from and to are the same, there is no
1680 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1682 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1686 bool haveSelection
= false;
1689 if (m_windowStyle
& wxTE_MULTILINE
)
1691 GtkTextIter ifrom
, ito
;
1692 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1694 haveSelection
= true;
1695 from
= gtk_text_iter_get_offset(&ifrom
);
1696 to
= gtk_text_iter_get_offset(&ito
);
1699 else // not multi-line
1701 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1704 haveSelection
= true;
1708 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
1710 haveSelection
= true;
1711 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
1712 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
1716 if (! haveSelection
)
1717 from
= to
= GetInsertionPoint();
1721 // exchange them to be compatible with wxMSW
1734 bool wxTextCtrl::IsEditable() const
1736 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1739 if (m_windowStyle
& wxTE_MULTILINE
)
1741 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1745 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1748 return GTK_EDITABLE(m_text
)->editable
;
1752 bool wxTextCtrl::IsModified() const
1757 void wxTextCtrl::Clear()
1759 SetValue( wxEmptyString
);
1762 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1764 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1766 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1768 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1769 event
.SetEventObject(this);
1770 event
.SetString(GetValue());
1771 if (GetEventHandler()->ProcessEvent(event
)) return;
1774 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1776 // This will invoke the dialog default action, such
1777 // as the clicking the default button.
1779 wxWindow
*top_frame
= m_parent
;
1780 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1781 top_frame
= top_frame
->GetParent();
1783 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1785 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1787 if (window
->default_widget
)
1789 gtk_widget_activate (window
->default_widget
);
1798 GtkWidget
* wxTextCtrl::GetConnectWidget()
1800 return GTK_WIDGET(m_text
);
1803 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1805 if (m_windowStyle
& wxTE_MULTILINE
)
1808 return window
== gtk_text_view_get_window( GTK_TEXT_VIEW( m_text
), GTK_TEXT_WINDOW_TEXT
); // pure guesswork
1810 return (window
== GTK_TEXT(m_text
)->text_area
);
1815 return (window
== GTK_ENTRY(m_text
)->text_area
);
1819 // the font will change for subsequent text insertiongs
1820 bool wxTextCtrl::SetFont( const wxFont
&font
)
1822 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1824 if ( !wxTextCtrlBase::SetFont(font
) )
1826 // font didn't change, nothing to do
1830 if ( m_windowStyle
& wxTE_MULTILINE
)
1832 SetUpdateFont(true);
1834 m_defaultStyle
.SetFont(font
);
1836 ChangeFontGlobally();
1842 void wxTextCtrl::ChangeFontGlobally()
1844 // this method is very inefficient and hence should be called as rarely as
1847 // TODO: it can be implemented much more efficiently for GTK2
1849 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1851 _T("shouldn't be called for single line controls") );
1853 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
),
1854 _T("shouldn't be called for single line controls") );
1857 wxString value
= GetValue();
1858 if ( !value
.empty() )
1860 SetUpdateFont(false);
1869 void wxTextCtrl::UpdateFontIfNeeded()
1872 ChangeFontGlobally();
1877 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1879 if ( !wxControl::SetForegroundColour(colour
) )
1882 // update default fg colour too
1883 m_defaultStyle
.SetTextColour(colour
);
1888 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1890 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1892 if ( !wxControl::SetBackgroundColour( colour
) )
1896 if (!m_widget
->window
)
1900 if (!m_backgroundColour
.Ok())
1903 if (m_windowStyle
& wxTE_MULTILINE
)
1906 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1909 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1910 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1911 gdk_window_clear( window
);
1915 // change active background color too
1916 m_defaultStyle
.SetBackgroundColour( colour
);
1921 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1923 if ( m_windowStyle
& wxTE_MULTILINE
)
1925 if ( style
.IsDefault() )
1932 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1934 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1935 _T("invalid range in wxTextCtrl::SetStyle") );
1937 GtkTextIter starti
, endi
;
1938 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1939 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1941 // use the attributes from style which are set in it and fall back
1942 // first to the default style and then to the text control default
1943 // colours for the others
1944 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1946 wxGtkTextApplyTagsFromAttr( m_buffer
, attr
, &starti
, &endi
);
1948 // VERY dirty way to do that - removes the required text and re-adds it
1949 // with styling (FIXME)
1951 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1953 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1954 _T("invalid range in wxTextCtrl::SetStyle") );
1956 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1957 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1958 wxString
tmp(text
,*wxConvCurrent
);
1961 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1962 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1965 wxWX2MBbuf buf
= tmp
.mbc_str();
1966 const char *txt
= buf
;
1967 size_t txtlen
= strlen(buf
);
1969 const char *txt
= tmp
;
1970 size_t txtlen
= tmp
.length();
1973 // use the attributes from style which are set in it and fall back
1974 // first to the default style and then to the text control default
1975 // colours for the others
1976 wxGtkTextInsert(m_text
,
1977 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1981 /* does not seem to help under GTK+ 1.2 !!!
1982 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1983 SetInsertionPoint( old_pos
);
1990 // cannot do this for GTK+'s Entry widget
1994 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1996 gtk_widget_modify_style(m_text
, style
);
1999 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2004 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2009 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2014 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2019 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2024 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2026 event
.Enable( CanCut() );
2029 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2031 event
.Enable( CanCopy() );
2034 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2036 event
.Enable( CanPaste() );
2039 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2041 event
.Enable( CanUndo() );
2044 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2046 event
.Enable( CanRedo() );
2049 void wxTextCtrl::OnInternalIdle()
2051 wxCursor cursor
= m_cursor
;
2052 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
2057 GdkWindow
*window
= (GdkWindow
*) NULL
;
2058 if (HasFlag(wxTE_MULTILINE
))
2059 window
= GTK_TEXT(m_text
)->text_area
;
2061 window
= GTK_ENTRY(m_text
)->text_area
;
2064 gdk_window_set_cursor( window
, cursor
.GetCursor() );
2066 if (!g_globalCursor
.Ok())
2067 cursor
= *wxSTANDARD_CURSOR
;
2069 window
= m_widget
->window
;
2070 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
2071 gdk_window_set_cursor( window
, cursor
.GetCursor() );
2075 if (g_delayedFocus
== this)
2077 if (GTK_WIDGET_REALIZED(m_widget
))
2079 gtk_widget_grab_focus( m_widget
);
2080 g_delayedFocus
= NULL
;
2084 if (wxUpdateUIEvent::CanUpdate(this))
2085 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
2088 wxSize
wxTextCtrl::DoGetBestSize() const
2090 // FIXME should be different for multi-line controls...
2091 wxSize
ret( wxControl::DoGetBestSize() );
2092 wxSize
best(80, ret
.y
);
2093 CacheBestSize(best
);
2097 // ----------------------------------------------------------------------------
2099 // ----------------------------------------------------------------------------
2101 void wxTextCtrl::Freeze()
2103 if ( HasFlag(wxTE_MULTILINE
) )
2106 if ( !m_frozenness
++ )
2108 // freeze textview updates and remove buffer
2109 g_signal_connect( G_OBJECT(m_text
), "expose_event",
2110 GTK_SIGNAL_FUNC(gtk_text_exposed_callback
), (gpointer
)this);
2111 g_signal_connect( G_OBJECT(m_widget
), "expose_event",
2112 GTK_SIGNAL_FUNC(gtk_text_exposed_callback
), (gpointer
)this);
2113 gtk_widget_set_sensitive(m_widget
, false);
2114 g_object_ref(m_buffer
);
2115 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), gtk_text_buffer_new(NULL
));
2118 gtk_text_freeze(GTK_TEXT(m_text
));
2123 void wxTextCtrl::Thaw()
2125 if ( HasFlag(wxTE_MULTILINE
) )
2128 wxASSERT_MSG( m_frozenness
> 0, _T("Thaw() without matching Freeze()") );
2130 if ( !--m_frozenness
)
2132 // Reattach buffer and thaw textview updates
2133 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
2134 g_object_unref(m_buffer
);
2135 gtk_widget_set_sensitive(m_widget
, true);
2136 g_signal_handlers_disconnect_by_func(m_widget
, (gpointer
)gtk_text_exposed_callback
, this);
2137 g_signal_handlers_disconnect_by_func(m_text
, (gpointer
)gtk_text_exposed_callback
, this);
2140 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
2142 gtk_text_thaw(GTK_TEXT(m_text
));
2147 // ----------------------------------------------------------------------------
2148 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
2149 // ----------------------------------------------------------------------------
2153 // FIXME: when dragging on a link the sample gets an "Unknown event".
2154 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
2155 // a buggy sample, or something else
2156 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
2159 if(!(m_windowStyle
& wxTE_AUTO_URL
))
2163 GtkTextIter start
, end
;
2164 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
2167 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
2168 event
.GetX(), event
.GetY(), &x
, &y
);
2170 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
2171 if (!gtk_text_iter_has_tag(&end
, tag
))
2173 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
2174 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
2178 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
2179 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
2182 if(!gtk_text_iter_begins_tag(&start
, tag
))
2183 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
2184 if(!gtk_text_iter_ends_tag(&end
, tag
))
2185 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
2187 // Native context menu is probably not desired on an URL.
2188 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
2189 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
2192 wxTextUrlEvent
url_event(m_windowId
, event
,
2193 gtk_text_iter_get_offset(&start
),
2194 gtk_text_iter_get_offset(&end
));
2196 InitCommandEvent(url_event
);
2197 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2198 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
2199 GetEventHandler()->ProcessEvent(url_event
);
2203 // ----------------------------------------------------------------------------
2205 // ----------------------------------------------------------------------------
2207 GtkAdjustment
*wxTextCtrl::GetVAdj() const
2209 if ( !IsMultiLine() )
2213 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget
));
2215 return GTK_TEXT(m_text
)->vadj
;
2219 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
2221 float value
= adj
->value
+ diff
;
2226 float upper
= adj
->upper
- adj
->page_size
;
2227 if ( value
> upper
)
2230 // did we noticeably change the scroll position?
2231 if ( fabs(adj
->value
- value
) < 0.2 )
2233 // well, this is what Robert does in wxScrollBar, so it must be good...
2240 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj
));
2242 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
2248 bool wxTextCtrl::ScrollLines(int lines
)
2250 GtkAdjustment
*adj
= GetVAdj();
2255 int diff
= (int)ceil(lines
*adj
->step_increment
);
2257 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
2258 int diff
= 10*lines
;
2261 return DoScroll(adj
, diff
);
2264 bool wxTextCtrl::ScrollPages(int pages
)
2266 GtkAdjustment
*adj
= GetVAdj();
2270 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
2276 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2278 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);