1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
20 #include "wx/settings.h"
24 #include "wx/strconv.h"
25 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #include <sys/types.h>
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
39 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
46 g_object_get (tag
, "name", &name
, NULL
);
48 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
49 // anonymous tag or not starting with prefix - don't remove
50 g_signal_stop_emission_by_name (buffer
, "remove_tag");
57 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer
*text_buffer
,
58 const wxTextAttr
& attr
,
62 static gchar buf
[1024];
65 gulong remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
66 G_CALLBACK (wxGtkOnRemoveTag
), gpointer("WX"));
67 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
68 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
73 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
74 font_string
= pango_font_description_to_string(font_description
);
75 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
);
76 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
79 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
80 "font-desc", font_description
,
82 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
85 if (attr
.GetFont().GetUnderlined())
87 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
88 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
91 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
92 "underline-set", TRUE
,
93 "underline", PANGO_UNDERLINE_SINGLE
,
95 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
99 if (attr
.HasTextColour())
101 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
102 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
103 colFg
->red
, colFg
->green
, colFg
->blue
);
104 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
107 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
108 "foreground-gdk", colFg
, NULL
);
109 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
112 if (attr
.HasBackgroundColour())
114 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
115 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
116 colBg
->red
, colBg
->green
, colBg
->blue
);
117 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
120 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
121 "background-gdk", colBg
, NULL
);
122 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
125 if (attr
.HasAlignment())
127 GtkTextIter para_start
, para_end
= *end
;
128 gtk_text_buffer_get_iter_at_line( text_buffer
,
130 gtk_text_iter_get_line(start
) );
131 gtk_text_iter_forward_line(¶_end
);
133 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
134 G_CALLBACK(wxGtkOnRemoveTag
),
135 gpointer("WXALIGNMENT"));
136 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
137 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
139 GtkJustification align
;
140 switch (attr
.GetAlignment())
143 align
= GTK_JUSTIFY_LEFT
;
145 case wxTEXT_ALIGNMENT_RIGHT
:
146 align
= GTK_JUSTIFY_RIGHT
;
148 case wxTEXT_ALIGNMENT_CENTER
:
149 align
= GTK_JUSTIFY_CENTER
;
151 // gtk+ doesn't support justify as of gtk+-2.7.4
154 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
155 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
158 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
159 "justification", align
, NULL
);
160 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
166 static void wxGtkTextInsert(GtkWidget
*text
,
167 GtkTextBuffer
*text_buffer
,
168 const wxTextAttr
& attr
,
169 const wxCharBuffer
& buffer
)
173 GtkTextIter iter
, start
;
175 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
176 gtk_text_buffer_get_insert (text_buffer
) );
177 start_offset
= gtk_text_iter_get_offset (&iter
);
178 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
180 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
182 wxGtkTextApplyTagsFromAttr(text_buffer
, attr
, &start
, &iter
);
186 // ----------------------------------------------------------------------------
187 // "insert_text" for GtkEntry
188 // ----------------------------------------------------------------------------
192 gtk_insert_text_callback(GtkEditable
*editable
,
193 const gchar
*new_text
,
194 gint new_text_length
,
199 wxapp_install_idle_handler();
201 // we should only be called if we have a max len limit at all
202 GtkEntry
*entry
= GTK_ENTRY (editable
);
204 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
206 // check that we don't overflow the max length limit
208 // FIXME: this doesn't work when we paste a string which is going to be
210 if ( entry
->text_length
== entry
->text_max_length
)
212 // we don't need to run the base class version at all
213 g_signal_stop_emission_by_name (editable
, "insert_text");
215 // remember that the next changed signal is to be ignored to avoid
216 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
217 win
->IgnoreNextTextUpdate();
219 // and generate the correct one ourselves
220 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
221 event
.SetEventObject(win
);
222 event
.SetString(win
->GetValue());
223 win
->GetEventHandler()->ProcessEvent( event
);
228 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
232 au_apply_tag_callback(GtkTextBuffer
*buffer
,
238 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
239 g_signal_stop_emission_by_name (buffer
, "apply_tag");
243 //-----------------------------------------------------------------------------
244 // GtkTextCharPredicates for gtk_text_iter_*_find_char
245 //-----------------------------------------------------------------------------
249 pred_whitespace (gunichar ch
, gpointer user_data
)
251 return g_unichar_isspace(ch
);
257 pred_non_whitespace (gunichar ch
, gpointer user_data
)
259 return !g_unichar_isspace(ch
);
265 pred_nonpunct (gunichar ch
, gpointer user_data
)
267 return !g_unichar_ispunct(ch
);
273 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
275 return !g_unichar_ispunct(ch
) || ch
== '/';
279 //-----------------------------------------------------------------------------
280 // Check for links between s and e and correct tags as necessary
281 //-----------------------------------------------------------------------------
283 // This function should be made match better while being efficient at one point.
284 // Most probably with a row of regular expressions.
287 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
289 static const char *URIPrefixes
[] =
307 GtkTextIter start
= *s
, end
= *e
;
308 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
310 // Get our special link tag
311 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
313 // Get rid of punctuation from beginning and end.
314 // Might want to move this to au_check_range if an improved link checking doesn't
315 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
316 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
317 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
319 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
320 gtk_text_iter_forward_char(&end
);
322 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
323 size_t len
= strlen(text
), prefix_len
;
326 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
328 prefix_len
= strlen(URIPrefixes
[n
]);
329 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
333 if(n
< WXSIZEOF(URIPrefixes
))
335 gulong signal_id
= g_signal_handler_find (buffer
,
336 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
338 (gpointer
)au_apply_tag_callback
, NULL
);
340 g_signal_handler_block (buffer
, signal_id
);
341 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
342 g_signal_handler_unblock (buffer
, signal_id
);
349 au_check_range(GtkTextIter
*s
,
350 GtkTextIter
*range_end
)
352 GtkTextIter range_start
= *s
;
353 GtkTextIter word_end
;
354 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
355 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
357 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
359 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
360 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
362 while(!gtk_text_iter_equal(&range_start
, range_end
))
364 word_end
= range_start
;
365 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
367 // Now we should have a word delimited by range_start and word_end, correct link tags
368 au_check_word(&range_start
, &word_end
);
370 range_start
= word_end
;
371 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
376 //-----------------------------------------------------------------------------
377 // "insert-text" for GtkTextBuffer
378 //-----------------------------------------------------------------------------
382 au_insert_text_callback(GtkTextBuffer
*buffer
,
388 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
391 GtkTextIter start
= *end
;
392 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
394 GtkTextIter line_start
= start
;
395 GtkTextIter line_end
= *end
;
396 GtkTextIter words_start
= start
;
397 GtkTextIter words_end
= *end
;
399 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
400 gtk_text_iter_forward_to_line_end(&line_end
);
401 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
402 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
404 au_check_range(&words_start
, &words_end
);
408 //-----------------------------------------------------------------------------
409 // "delete-range" for GtkTextBuffer
410 //-----------------------------------------------------------------------------
414 au_delete_range_callback(GtkTextBuffer
*buffer
,
419 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
422 GtkTextIter line_start
= *start
, line_end
= *end
;
424 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
425 gtk_text_iter_forward_to_line_end(&line_end
);
426 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
427 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
429 au_check_range(start
, end
);
434 //-----------------------------------------------------------------------------
436 //-----------------------------------------------------------------------------
440 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
442 if ( win
->IgnoreTextUpdate() )
445 if (!win
->m_hasVMT
) return;
448 wxapp_install_idle_handler();
450 if ( win
->MarkDirtyOnChange() )
453 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
454 event
.SetEventObject( win
);
455 win
->GetEventHandler()->ProcessEvent( event
);
459 //-----------------------------------------------------------------------------
460 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
461 //-----------------------------------------------------------------------------
463 // common part of the event handlers below
465 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
466 wxEventType eventType
, const gchar
* signal_name
)
468 wxClipboardTextEvent
event( eventType
, win
->GetId() );
469 event
.SetEventObject( win
);
470 if ( win
->GetEventHandler()->ProcessEvent( event
) )
472 // don't let the default processing to take place if we did something
473 // ourselves in the event handler
474 g_signal_stop_emission_by_name (widget
, signal_name
);
480 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
482 handle_text_clipboard_callback(
483 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
487 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
489 handle_text_clipboard_callback(
490 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
494 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
496 handle_text_clipboard_callback(
497 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
501 //-----------------------------------------------------------------------------
502 // "expose_event" from scrolled window and textview
503 //-----------------------------------------------------------------------------
507 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
514 //-----------------------------------------------------------------------------
516 //-----------------------------------------------------------------------------
518 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
520 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
521 EVT_CHAR(wxTextCtrl::OnChar
)
523 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
524 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
525 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
526 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
527 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
529 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
530 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
531 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
532 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
533 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
535 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
536 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
537 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
538 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
539 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
540 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
541 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
542 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
543 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
546 void wxTextCtrl::Init()
552 SetUpdateFont(false);
556 m_gdkHandCursor
= NULL
;
557 m_gdkXTermCursor
= NULL
;
560 wxTextCtrl::~wxTextCtrl()
563 gdk_cursor_unref(m_gdkHandCursor
);
565 gdk_cursor_unref(m_gdkXTermCursor
);
568 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
570 const wxString
&value
,
574 const wxValidator
& validator
,
575 const wxString
&name
)
579 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
582 bool wxTextCtrl::Create( wxWindow
*parent
,
584 const wxString
&value
,
588 const wxValidator
& validator
,
589 const wxString
&name
)
592 m_acceptsFocus
= true;
594 if (!PreCreation( parent
, pos
, size
) ||
595 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
597 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
601 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
606 m_text
= gtk_text_view_new();
608 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
610 // create scrolled window
611 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
612 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
613 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
614 // for ScrollLines/Pages
615 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
617 // Insert view into scrolled window
618 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
620 // translate wx wrapping style to GTK+
622 if ( HasFlag( wxTE_DONTWRAP
) )
623 wrap
= GTK_WRAP_NONE
;
624 else if ( HasFlag( wxTE_CHARWRAP
) )
625 wrap
= GTK_WRAP_CHAR
;
626 else if ( HasFlag( wxTE_WORDWRAP
) )
627 wrap
= GTK_WRAP_WORD
;
628 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
630 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
632 if ( !gtk_check_version(2,4,0) )
634 wrap
= GTK_WRAP_WORD_CHAR
;
638 wrap
= GTK_WRAP_WORD
;
641 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
643 GtkScrolledWindowSetBorder(m_widget
, style
);
645 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
647 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
651 // a single-line text control: no need for scrollbars
653 m_text
= gtk_entry_new();
655 if (style
& wxNO_BORDER
)
656 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
659 m_parent
->DoAddChild( this );
661 m_focusWidget
= m_text
;
667 gtk_widget_show(m_text
);
675 if (style
& wxTE_PASSWORD
)
678 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
681 if (style
& wxTE_READONLY
)
684 gtk_editable_set_editable( GTK_EDITABLE(m_text
), FALSE
);
686 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
691 if (style
& wxTE_RIGHT
)
692 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_RIGHT
);
693 else if (style
& wxTE_CENTRE
)
694 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_CENTER
);
695 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
700 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
701 if (!gtk_check_version(2,4,0))
703 if (style
& wxTE_RIGHT
)
704 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 1.0 );
705 else if (style
& wxTE_CENTRE
)
706 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 0.5 );
711 // We want to be notified about text changes.
714 g_signal_connect (m_buffer
, "changed",
715 G_CALLBACK (gtk_text_changed_callback
), this);
717 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
718 if (style
& wxTE_AUTO_URL
)
720 GtkTextIter start
, end
;
721 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
722 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
724 // We create our wxUrl tag here for slight efficiency gain - we
725 // don't have to check for the tag existance in callbacks,
726 // hereby it's guaranteed to exist.
727 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
728 "foreground", "blue",
729 "underline", PANGO_UNDERLINE_SINGLE
,
732 // Check for URLs after each text change
733 g_signal_connect_after (m_buffer
, "insert_text",
734 G_CALLBACK (au_insert_text_callback
), this);
735 g_signal_connect_after (m_buffer
, "delete_range",
736 G_CALLBACK (au_delete_range_callback
), this);
738 // Block all wxUrl tag applying unless we do it ourselves, in which case we
739 // block this callback temporarily. This takes care of gtk+ internal
740 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
741 // which is undesired because only a part of the URL might be copied.
742 // The insert-text signal emitted inside it will take care of newly formed
743 // or wholly copied URLs.
744 g_signal_connect (m_buffer
, "apply_tag",
745 G_CALLBACK (au_apply_tag_callback
), NULL
);
747 // Check for URLs in the initial string passed to Create
748 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
749 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
750 au_check_range(&start
, &end
);
755 g_signal_connect (m_text
, "changed",
756 G_CALLBACK (gtk_text_changed_callback
), this);
759 g_signal_connect (m_text
, "copy-clipboard",
760 G_CALLBACK (gtk_copy_clipboard_callback
), this);
761 g_signal_connect (m_text
, "cut-clipboard",
762 G_CALLBACK (gtk_cut_clipboard_callback
), this);
763 g_signal_connect (m_text
, "paste-clipboard",
764 G_CALLBACK (gtk_paste_clipboard_callback
), this);
766 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
768 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
769 SetDefaultStyle( attrDef
);
775 void wxTextCtrl::CalculateScrollbar()
779 wxString
wxTextCtrl::GetValue() const
781 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
787 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
789 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
790 gchar
*text
= gtk_text_buffer_get_text( m_buffer
, &start
, &end
, TRUE
);
792 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
800 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
801 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
809 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
811 // GTK+ uses UTF-8 internally, we need to convert to it but from which
814 // first check the default text style (we intentionally don't check the
815 // style for the current position as it doesn't make sense for SetValue())
816 const wxTextAttr
& style
= GetDefaultStyle();
817 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
818 : wxFONTENCODING_SYSTEM
;
820 // fall back to the controls font if no style
821 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
822 enc
= GetFont().GetEncoding();
827 void wxTextCtrl::SetValue( const wxString
&value
)
829 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
831 // the control won't be modified any more as we programmatically replace
832 // all the existing text, so reset the flag and don't set it again (and do
833 // it now, before the text event handler is ran so that IsModified() called
834 // from there returns the expected value)
836 DontMarkDirtyOnNextChange();
840 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, GetTextEncoding()));
843 // see comment in WriteText() as to why we must warn the user about
845 wxLogWarning(_("Failed to set text in the text control."));
849 if (gtk_text_buffer_get_char_count(m_buffer
) != 0)
850 IgnoreNextTextUpdate();
852 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
856 // gtk_entry_set_text() emits two "changed" signals if the control is
857 // not empty because internally it calls gtk_editable_delete_text() and
858 // gtk_editable_insert_text() but we want to have only one event
859 if ( !GetValue().empty() )
860 IgnoreNextTextUpdate();
862 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV(value
) );
865 // GRG, Jun/2000: Changed this after a lot of discussion in
866 // the lists. wxWidgets 2.2 will have a set of flags to
867 // customize this behaviour.
868 SetInsertionPoint(0);
871 void wxTextCtrl::WriteText( const wxString
&text
)
873 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
878 // check if we have a specific style for the current position
879 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
881 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
883 enc
= style
.GetFont().GetEncoding();
886 if ( enc
== wxFONTENCODING_SYSTEM
)
887 enc
= GetTextEncoding();
889 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
892 // we must log an error here as losing the text like this can be a
893 // serious problem (e.g. imagine the document edited by user being
894 // empty instead of containing the correct text)
895 wxLogWarning(_("Failed to insert text in the control."));
899 // we're changing the text programmatically
900 DontMarkDirtyOnNextChange();
904 // First remove the selection if there is one
905 // TODO: Is there an easier GTK specific way to do this?
907 GetSelection(&from
, &to
);
912 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
914 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
915 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
916 if ( wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
) )
918 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
919 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
924 // First remove the selection if there is one
925 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
927 // This moves the cursor pos to behind the inserted text.
928 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
930 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
932 // Bring entry's cursor uptodate.
933 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
937 void wxTextCtrl::AppendText( const wxString
&text
)
939 SetInsertionPointEnd();
943 wxString
wxTextCtrl::GetLineText( long lineNo
) const
948 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
949 GtkTextIter end
= line
;
950 gtk_text_iter_forward_to_line_end(&end
);
951 gchar
*text
= gtk_text_buffer_get_text(m_buffer
,&line
,&end
,TRUE
);
952 wxString
result(wxGTK_CONV_BACK(text
));
958 if (lineNo
== 0) return GetValue();
959 return wxEmptyString
;
963 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
965 /* If you implement this, don't forget to update the documentation!
966 * (file docs/latex/wx/text.tex) */
967 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
970 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
976 if (pos
> GetLastPosition())
979 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
982 *y
= gtk_text_iter_get_line(&iter
);
984 *x
= gtk_text_iter_get_line_offset(&iter
);
986 else // single line control
988 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
997 // index out of bounds
1005 long wxTextCtrl::XYToPosition(long x
, long y
) const
1007 if ( IsSingleLine() )
1011 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1014 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1015 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1018 return gtk_text_iter_get_offset(&iter
) + x
;
1021 int wxTextCtrl::GetLineLength(long lineNo
) const
1023 if ( IsMultiLine() )
1025 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1026 if (lineNo
> last_line
)
1030 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1031 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1032 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1036 wxString str
= GetLineText (lineNo
);
1037 return (int) str
.length();
1041 int wxTextCtrl::GetNumberOfLines() const
1043 if ( IsMultiLine() )
1046 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, 0 );
1048 // move forward by one display line until the end is reached
1050 while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text
), &iter
) )
1055 // If the last character in the text buffer is a newline,
1056 // gtk_text_view_forward_display_line() will return false without that
1057 // line being counted. Must add one manually in that case.
1058 GtkTextIter lastCharIter
;
1059 gtk_text_buffer_get_iter_at_offset
1063 gtk_text_buffer_get_char_count(m_buffer
) - 1
1065 gchar lastChar
= gtk_text_iter_get_char( &lastCharIter
);
1066 if ( lastChar
== wxT('\n') )
1077 void wxTextCtrl::SetInsertionPoint( long pos
)
1079 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1081 if ( IsMultiLine() )
1084 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1085 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1086 gtk_text_view_scroll_mark_onscreen
1088 GTK_TEXT_VIEW(m_text
),
1089 gtk_text_buffer_get_insert( m_buffer
)
1094 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1095 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1099 void wxTextCtrl::SetInsertionPointEnd()
1101 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1103 if ( IsMultiLine() )
1106 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1107 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1111 gtk_editable_set_position( GTK_EDITABLE(m_text
), -1 );
1115 void wxTextCtrl::SetEditable( bool editable
)
1117 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1119 if ( IsMultiLine() )
1121 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1125 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1129 bool wxTextCtrl::Enable( bool enable
)
1131 if (!wxWindowBase::Enable(enable
))
1137 if ( IsMultiLine() )
1139 SetEditable( enable
);
1143 gtk_widget_set_sensitive( m_text
, enable
);
1149 // wxGTK-specific: called recursively by Enable,
1150 // to give widgets an oppprtunity to correct their colours after they
1151 // have been changed by Enable
1152 void wxTextCtrl::OnParentEnable( bool enable
)
1154 // If we have a custom background colour, we use this colour in both
1155 // disabled and enabled mode, or we end up with a different colour under the
1157 wxColour oldColour
= GetBackgroundColour();
1160 // Need to set twice or it'll optimize the useful stuff out
1161 if (oldColour
== * wxWHITE
)
1162 SetBackgroundColour(*wxBLACK
);
1164 SetBackgroundColour(*wxWHITE
);
1165 SetBackgroundColour(oldColour
);
1169 void wxTextCtrl::MarkDirty()
1174 void wxTextCtrl::DiscardEdits()
1179 // ----------------------------------------------------------------------------
1180 // max text length support
1181 // ----------------------------------------------------------------------------
1183 bool wxTextCtrl::IgnoreTextUpdate()
1185 if ( m_ignoreNextUpdate
)
1187 m_ignoreNextUpdate
= false;
1195 bool wxTextCtrl::MarkDirtyOnChange()
1197 if ( m_dontMarkDirty
)
1199 m_dontMarkDirty
= false;
1207 void wxTextCtrl::SetMaxLength(unsigned long len
)
1209 if ( !HasFlag(wxTE_MULTILINE
) )
1211 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1213 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1214 // we had tried to enter more text than allowed by max text length and
1215 // the text wasn't really changed
1217 // to detect this and generate TEXT_MAXLEN event instead of
1218 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1220 // when max len is set to 0 we disconnect our handler as it means that
1221 // we shouldn't check anything any more
1224 g_signal_connect (m_text
, "insert_text",
1225 G_CALLBACK (gtk_insert_text_callback
), this);
1229 g_signal_handlers_disconnect_by_func (m_text
,
1230 (gpointer
) gtk_insert_text_callback
, this);
1235 void wxTextCtrl::SetSelection( long from
, long to
)
1237 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1239 if (from
== -1 && to
== -1)
1242 to
= GetValue().length();
1245 if ( IsMultiLine() )
1247 GtkTextIter fromi
, toi
;
1248 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1249 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1251 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1252 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1256 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1260 void wxTextCtrl::ShowPosition( long pos
)
1262 if ( IsMultiLine() )
1265 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1266 gtk_text_iter_set_offset( &iter
, pos
);
1267 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1268 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1272 wxTextCtrlHitTestResult
1273 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1275 if ( !IsMultiLine() )
1278 return wxTE_HT_UNKNOWN
;
1282 gtk_text_view_window_to_buffer_coords
1284 GTK_TEXT_VIEW(m_text
),
1285 GTK_TEXT_WINDOW_TEXT
,
1291 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1293 *pos
= gtk_text_iter_get_offset(&iter
);
1295 return wxTE_HT_ON_TEXT
;
1298 long wxTextCtrl::GetInsertionPoint() const
1300 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1302 if ( IsMultiLine() )
1304 // There is no direct accessor for the cursor, but
1305 // internally, the cursor is the "mark" called
1306 // "insert" in the text view's btree structure.
1308 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1310 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1312 return gtk_text_iter_get_offset( &cursor
);
1316 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1320 wxTextPos
wxTextCtrl::GetLastPosition() const
1322 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1326 if ( IsMultiLine() )
1329 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1331 pos
= gtk_text_iter_get_offset( &end
);
1335 pos
= GTK_ENTRY(m_text
)->text_length
;
1341 void wxTextCtrl::Remove( long from
, long to
)
1343 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1345 if ( IsMultiLine() )
1347 GtkTextIter fromi
, toi
;
1348 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1349 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1351 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1354 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1357 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1359 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1365 SetInsertionPoint( from
);
1370 void wxTextCtrl::Cut()
1372 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1374 if ( IsMultiLine() )
1375 g_signal_emit_by_name (m_text
, "cut-clipboard");
1377 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1380 void wxTextCtrl::Copy()
1382 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1384 if ( IsMultiLine() )
1385 g_signal_emit_by_name (m_text
, "copy-clipboard");
1387 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1390 void wxTextCtrl::Paste()
1392 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1394 if ( IsMultiLine() )
1395 g_signal_emit_by_name (m_text
, "paste-clipboard");
1397 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1401 void wxTextCtrl::Undo()
1404 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1407 void wxTextCtrl::Redo()
1410 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1413 bool wxTextCtrl::CanUndo() const
1416 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1420 bool wxTextCtrl::CanRedo() const
1423 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1427 // If the return values from and to are the same, there is no
1429 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1431 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1435 bool haveSelection
= false;
1437 if ( IsMultiLine() )
1439 GtkTextIter ifrom
, ito
;
1440 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1442 haveSelection
= true;
1443 from
= gtk_text_iter_get_offset(&ifrom
);
1444 to
= gtk_text_iter_get_offset(&ito
);
1447 else // not multi-line
1449 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1452 haveSelection
= true;
1456 if (! haveSelection
)
1457 from
= to
= GetInsertionPoint();
1461 // exchange them to be compatible with wxMSW
1474 bool wxTextCtrl::IsEditable() const
1476 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1478 if ( IsMultiLine() )
1480 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1484 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1488 bool wxTextCtrl::IsModified() const
1493 void wxTextCtrl::Clear()
1495 SetValue( wxEmptyString
);
1498 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1500 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1502 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1504 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1506 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1507 event
.SetEventObject(this);
1508 event
.SetString(GetValue());
1509 if ( GetEventHandler()->ProcessEvent(event
) )
1513 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1515 if ( IsSingleLine() )
1517 // This will invoke the dialog default action, such
1518 // as the clicking the default button.
1520 wxWindow
*top_frame
= m_parent
;
1521 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1522 top_frame
= top_frame
->GetParent();
1524 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1526 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1528 if (window
->default_widget
)
1530 gtk_widget_activate (window
->default_widget
);
1540 GtkWidget
* wxTextCtrl::GetConnectWidget()
1542 return GTK_WIDGET(m_text
);
1545 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1547 if ( IsMultiLine() )
1549 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1550 GTK_TEXT_WINDOW_TEXT
);
1554 return GTK_ENTRY(m_text
)->text_area
;
1558 // the font will change for subsequent text insertiongs
1559 bool wxTextCtrl::SetFont( const wxFont
&font
)
1561 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1563 if ( !wxTextCtrlBase::SetFont(font
) )
1565 // font didn't change, nothing to do
1569 if ( IsMultiLine() )
1571 SetUpdateFont(true);
1573 m_defaultStyle
.SetFont(font
);
1575 ChangeFontGlobally();
1581 void wxTextCtrl::ChangeFontGlobally()
1583 // this method is very inefficient and hence should be called as rarely as
1586 // TODO: it can be implemented much more efficiently for GTK2
1587 wxASSERT_MSG( IsMultiLine(),
1588 _T("shouldn't be called for single line controls") );
1590 wxString value
= GetValue();
1591 if ( !value
.empty() )
1593 SetUpdateFont(false);
1600 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1602 if ( !wxControl::SetForegroundColour(colour
) )
1605 // update default fg colour too
1606 m_defaultStyle
.SetTextColour(colour
);
1611 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1613 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1615 if ( !wxControl::SetBackgroundColour( colour
) )
1618 if (!m_backgroundColour
.Ok())
1621 // change active background color too
1622 m_defaultStyle
.SetBackgroundColour( colour
);
1627 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1629 if ( IsMultiLine() )
1631 if ( style
.IsDefault() )
1637 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1639 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1640 _T("invalid range in wxTextCtrl::SetStyle") );
1642 GtkTextIter starti
, endi
;
1643 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1644 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1646 // use the attributes from style which are set in it and fall back
1647 // first to the default style and then to the text control default
1648 // colours for the others
1649 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1651 wxGtkTextApplyTagsFromAttr( m_buffer
, attr
, &starti
, &endi
);
1657 // cannot do this for GTK+'s Entry widget
1661 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1663 gtk_widget_modify_style(m_text
, style
);
1666 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1671 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1676 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1681 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1686 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1691 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1693 event
.Enable( CanCut() );
1696 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1698 event
.Enable( CanCopy() );
1701 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1703 event
.Enable( CanPaste() );
1706 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1708 event
.Enable( CanUndo() );
1711 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1713 event
.Enable( CanRedo() );
1716 wxSize
wxTextCtrl::DoGetBestSize() const
1718 // FIXME should be different for multi-line controls...
1719 wxSize
ret( wxControl::DoGetBestSize() );
1720 wxSize
best(80, ret
.y
);
1721 CacheBestSize(best
);
1725 // ----------------------------------------------------------------------------
1727 // ----------------------------------------------------------------------------
1729 void wxTextCtrl::Freeze()
1731 if ( HasFlag(wxTE_MULTILINE
) )
1733 if ( !m_frozenness
++ )
1735 // freeze textview updates and remove buffer
1736 g_signal_connect (m_text
, "expose_event",
1737 G_CALLBACK (gtk_text_exposed_callback
), this);
1738 g_signal_connect (m_widget
, "expose_event",
1739 G_CALLBACK (gtk_text_exposed_callback
), this);
1740 gtk_widget_set_sensitive(m_widget
, false);
1741 g_object_ref(m_buffer
);
1742 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), gtk_text_buffer_new(NULL
));
1747 void wxTextCtrl::Thaw()
1749 if ( HasFlag(wxTE_MULTILINE
) )
1751 wxASSERT_MSG( m_frozenness
> 0, _T("Thaw() without matching Freeze()") );
1753 if ( !--m_frozenness
)
1755 // Reattach buffer and thaw textview updates
1756 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1757 g_object_unref(m_buffer
);
1758 gtk_widget_set_sensitive(m_widget
, true);
1759 g_signal_handlers_disconnect_by_func (m_widget
,
1760 (gpointer
) gtk_text_exposed_callback
, this);
1761 g_signal_handlers_disconnect_by_func (m_text
,
1762 (gpointer
) gtk_text_exposed_callback
, this);
1767 // ----------------------------------------------------------------------------
1768 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1769 // ----------------------------------------------------------------------------
1771 // FIXME: when dragging on a link the sample gets an "Unknown event".
1772 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1773 // a buggy sample, or something else
1774 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1777 if( !HasFlag(wxTE_AUTO_URL
) )
1781 GtkTextIter start
, end
;
1782 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1785 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1786 event
.GetX(), event
.GetY(), &x
, &y
);
1788 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1789 if (!gtk_text_iter_has_tag(&end
, tag
))
1791 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1792 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1796 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1797 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1800 if(!gtk_text_iter_begins_tag(&start
, tag
))
1801 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1802 if(!gtk_text_iter_ends_tag(&end
, tag
))
1803 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1805 // Native context menu is probably not desired on an URL.
1806 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1807 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1810 wxTextUrlEvent
url_event(m_windowId
, event
,
1811 gtk_text_iter_get_offset(&start
),
1812 gtk_text_iter_get_offset(&end
));
1814 InitCommandEvent(url_event
);
1815 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1816 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1817 GetEventHandler()->ProcessEvent(url_event
);
1822 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1824 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);