X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ff805cac748d91edbf55d82a7f1b567d5d4d3cc0..e3b2f973e8278b4509710a5a06bede1297b317eb:/src/gtk/textctrl.cpp diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index b44668c172..e88d1e2aed 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -22,6 +22,7 @@ #include "wx/math.h" #endif +#include "wx/scopeguard.h" #include "wx/strconv.h" #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo()) @@ -149,7 +150,7 @@ static void wxGtkTextApplyTagsFromAttr(GtkWidget *text, gtk_text_iter_get_line(start) ); gtk_text_iter_forward_line(¶_end); - wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXALIGNMENT", start, end); + wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXALIGNMENT", ¶_start, ¶_end); GtkJustification align; switch (attr.GetAlignment()) @@ -195,17 +196,12 @@ static void wxGtkTextApplyTagsFromAttr(GtkWidget *text, gtk_text_iter_get_line(start) ); gtk_text_iter_forward_line(¶_end); - wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXINDENT", start, end); + wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXINDENT", ¶_start, ¶_end); // Convert indent from 1/10th of a mm into pixels - float factor; -#if GTK_CHECK_VERSION(2,2,0) - if (!gtk_check_version(2,2,0)) - factor = (float)gdk_screen_get_width(gtk_widget_get_screen(text)) / + float factor = + (float)gdk_screen_get_width(gtk_widget_get_screen(text)) / gdk_screen_get_width_mm(gtk_widget_get_screen(text)) / 10; - else -#endif - factor = (float)gdk_screen_width() / gdk_screen_width_mm() / 10; const int indent = (int)(factor * attr.GetLeftIndent()); const int subIndent = (int)(factor * attr.GetLeftSubIndent()); @@ -244,14 +240,14 @@ static void wxGtkTextApplyTagsFromAttr(GtkWidget *text, gtk_text_iter_get_line(start) ); gtk_text_iter_forward_line(¶_end); - wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXTABS", start, end); + wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXTABS", ¶_start, ¶_end); const wxArrayInt& tabs = attr.GetTabs(); - wxString tagname = _T("WXTABS"); + wxString tagname = wxT("WXTABS"); g_snprintf(buf, sizeof(buf), "WXTABS"); for (size_t i = 0; i < tabs.GetCount(); i++) - tagname += wxString::Format(_T(" %d"), tabs[i]); + tagname += wxString::Format(wxT(" %d"), tabs[i]); const wxWX2MBbuf buftag = tagname.utf8_str(); @@ -260,14 +256,9 @@ static void wxGtkTextApplyTagsFromAttr(GtkWidget *text, if (!tag) { // Factor to convert from 1/10th of a mm into pixels - float factor; -#if GTK_CHECK_VERSION(2,2,0) - if (!gtk_check_version(2,2,0)) - factor = (float)gdk_screen_get_width(gtk_widget_get_screen(text)) / + float factor = + (float)gdk_screen_get_width(gtk_widget_get_screen(text)) / gdk_screen_get_width_mm(gtk_widget_get_screen(text)) / 10; - else -#endif - factor = (float)gdk_screen_width() / gdk_screen_width_mm() / 10; PangoTabArray* tabArray = pango_tab_array_new(tabs.GetCount(), TRUE); for (size_t i = 0; i < tabs.GetCount(); i++) @@ -360,7 +351,7 @@ extern "C" { static void au_check_word( GtkTextIter *s, GtkTextIter *e ) { - static const char *URIPrefixes[] = + static const char *const URIPrefixes[] = { "http://", "ftp://", @@ -504,6 +495,27 @@ au_delete_range_callback(GtkTextBuffer * WXUNUSED(buffer), } } +//----------------------------------------------------------------------------- +// "populate_popup" from text control and "unmap" from its poup menu +//----------------------------------------------------------------------------- + +extern "C" { +static void +gtk_textctrl_popup_unmap( GtkMenu *WXUNUSED(menu), wxTextCtrl* win ) +{ + win->GTKEnableFocusOutEvent(); +} +} + +extern "C" { +static void +gtk_textctrl_populate_popup( GtkEntry *WXUNUSED(entry), GtkMenu *menu, wxTextCtrl *win ) +{ + win->GTKDisableFocusOutEvent(); + + g_signal_connect (menu, "unmap", G_CALLBACK (gtk_textctrl_popup_unmap), win ); +} +} //----------------------------------------------------------------------------- // "changed" @@ -511,7 +523,7 @@ au_delete_range_callback(GtkTextBuffer * WXUNUSED(buffer), extern "C" { static void -gtk_text_changed_callback( GtkWidget * WXUNUSED(widget), wxTextCtrl *win ) +gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) { if ( win->IgnoreTextUpdate() ) return; @@ -536,7 +548,7 @@ handle_text_clipboard_callback( GtkWidget *widget, wxTextCtrl *win, { wxClipboardTextEvent event( eventType, win->GetId() ); event.SetEventObject( win ); - if ( win->GetEventHandler()->ProcessEvent( event ) ) + if ( win->HandleWindowEvent( event ) ) { // don't let the default processing to take place if we did something // ourselves in the event handler @@ -567,27 +579,10 @@ gtk_paste_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) } } -//----------------------------------------------------------------------------- -// "expose_event" from scrolled window and textview -//----------------------------------------------------------------------------- - -extern "C" { -static gboolean -gtk_text_exposed_callback( GtkWidget * WXUNUSED(widget), - GdkEventExpose * WXUNUSED(event), - wxTextCtrl * WXUNUSED(win) ) -{ - return TRUE; -} -} - - //----------------------------------------------------------------------------- // wxTextCtrl //----------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) - BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) EVT_CHAR(wxTextCtrl::OnChar) @@ -624,18 +619,11 @@ void wxTextCtrl::Init() SetUpdateFont(false); m_text = NULL; - m_freezeCount = 0; m_showPositionOnThaw = NULL; - m_gdkHandCursor = NULL; - m_gdkXTermCursor = NULL; } wxTextCtrl::~wxTextCtrl() { - if(m_gdkHandCursor) - gdk_cursor_unref(m_gdkHandCursor); - if(m_gdkXTermCursor) - gdk_cursor_unref(m_gdkXTermCursor); } wxTextCtrl::wxTextCtrl( wxWindow *parent, @@ -697,7 +685,7 @@ bool wxTextCtrl::Create( wxWindow *parent, GTKSetWrapMode(); - GtkScrolledWindowSetBorder(m_widget, style); + GTKScrolledWindowSetBorder(m_widget, style); gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK ); @@ -708,10 +696,15 @@ bool wxTextCtrl::Create( wxWindow *parent, // a single-line text control: no need for scrollbars m_widget = m_text = gtk_entry_new(); + // work around probable bug in GTK+ 2.18 when calling WriteText on a + // new, empty control, see http://trac.wxwidgets.org/ticket/11409 + gtk_entry_get_text((GtkEntry*)m_text); if (style & wxNO_BORDER) g_object_set (m_text, "has-frame", FALSE, NULL); + } + g_object_ref(m_widget); m_parent->DoAddChild( this ); @@ -736,6 +729,11 @@ bool wxTextCtrl::Create( wxWindow *parent, G_CALLBACK (gtk_text_changed_callback), this); } + // Catch to disable focus out handling + g_signal_connect (m_text, "populate_popup", + G_CALLBACK (gtk_textctrl_populate_popup), + this); + if (!value.empty()) { SetValue( value ); @@ -747,9 +745,6 @@ bool wxTextCtrl::Create( wxWindow *parent, if (style & wxTE_READONLY) GTKSetEditable(); - if (style & wxTE_PROCESS_ENTER) - GTKSetActivatesDefault(); - // left justification (alignment) is the default anyhow if ( style & (wxTE_RIGHT | wxTE_CENTRE) ) GTKSetJustification(); @@ -760,8 +755,6 @@ bool wxTextCtrl::Create( wxWindow *parent, if (style & wxTE_AUTO_URL) { GtkTextIter start, end; - m_gdkHandCursor = gdk_cursor_new(GDK_HAND2); - m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM); // We create our wxUrl tag here for slight efficiency gain - we // don't have to check for the tag existance in callbacks, @@ -792,6 +785,13 @@ bool wxTextCtrl::Create( wxWindow *parent, au_check_range(&start, &end); } } + else // single line + { + // do the right thing with Enter presses depending on whether we have + // wxTE_PROCESS_ENTER or not + GTKSetActivatesDefault(); + } + g_signal_connect (m_text, "copy-clipboard", G_CALLBACK (gtk_copy_clipboard_callback), this); @@ -812,6 +812,11 @@ GtkEditable *wxTextCtrl::GetEditable() const return GTK_EDITABLE(m_text); } +GtkEntry *wxTextCtrl::GetEntry() const +{ + return GTK_ENTRY(m_text); +} + // ---------------------------------------------------------------------------- // flags handling // ---------------------------------------------------------------------------- @@ -857,17 +862,7 @@ void wxTextCtrl::GTKSetWrapMode() else if ( HasFlag( wxTE_WORDWRAP ) ) wrap = GTK_WRAP_WORD; else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 - { - // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 -#ifdef __WXGTK24__ - if ( !gtk_check_version(2,4,0) ) - { - wrap = GTK_WRAP_WORD_CHAR; - } - else -#endif // __WXGTK24__ - wrap = GTK_WRAP_WORD; - } + wrap = GTK_WRAP_WORD_CHAR; gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); } @@ -888,23 +883,16 @@ void wxTextCtrl::GTKSetJustification() } else // single line { -#ifdef __WXGTK24__ - // gtk_entry_set_alignment was introduced in gtk+-2.3.5 - if (!gtk_check_version(2,4,0)) - { - gfloat align; - if ( HasFlag(wxTE_RIGHT) ) - align = 1.0; - else if ( HasFlag(wxTE_CENTRE) ) - align = 0.5; - else // single line - align = 0.0; - - gtk_entry_set_alignment(GTK_ENTRY(m_text), align); - } -#endif // __WXGTK24__ - } + gfloat align; + if ( HasFlag(wxTE_RIGHT) ) + align = 1.0; + else if ( HasFlag(wxTE_CENTRE) ) + align = 0.5; + else // single line + align = 0.0; + gtk_entry_set_alignment(GTK_ENTRY(m_text), align); + } } void wxTextCtrl::SetWindowStyleFlag(long style) @@ -993,6 +981,22 @@ void wxTextCtrl::DoSetValue( const wxString &value, int flags ) return; } + if (value.IsEmpty()) + { + if ( !(flags & SetValue_SendEvent) ) + EnableTextChangedEvents(false); + + gtk_text_buffer_set_text( m_buffer, "", 0 ); + + if ( !(flags & SetValue_SendEvent) ) + EnableTextChangedEvents(true); + + return; + } + +#if wxUSE_UNICODE + const wxCharBuffer buffer(value.utf8_str()); +#else wxFontEncoding enc = m_defaultStyle.HasFont() ? m_defaultStyle.GetFont().GetEncoding() : wxFONTENCODING_SYSTEM; @@ -1007,6 +1011,7 @@ void wxTextCtrl::DoSetValue( const wxString &value, int flags ) wxLogWarning(_("Failed to set text in the text control.")); return; } +#endif if ( !(flags & SetValue_SendEvent) ) { @@ -1027,9 +1032,6 @@ void wxTextCtrl::DoSetValue( const wxString &value, int flags ) { EnableTextChangedEvents(true); } - - // This was added after discussion on the list - SetInsertionPoint(0); } void wxTextCtrl::WriteText( const wxString &text ) @@ -1045,6 +1047,9 @@ void wxTextCtrl::WriteText( const wxString &text ) return; } +#if wxUSE_UNICODE + const wxCharBuffer buffer(text.utf8_str()); +#else // check if we have a specific style for the current position wxFontEncoding enc = wxFONTENCODING_SYSTEM; wxTextAttr style; @@ -1065,6 +1070,7 @@ void wxTextCtrl::WriteText( const wxString &text ) wxLogWarning(_("Failed to insert text in the control.")); return; } +#endif // First remove the selection if there is one // TODO: Is there an easier GTK specific way to do this? @@ -1245,6 +1251,7 @@ bool wxTextCtrl::Enable( bool enable ) } gtk_widget_set_sensitive( m_text, enable ); + SetCursor(enable ? wxCursor(wxCURSOR_IBEAM) : wxCursor()); return true; } @@ -1325,20 +1332,19 @@ void wxTextCtrl::SetSelection( long from, long to ) { wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); - if (from == -1 && to == -1) - { - from = 0; - to = GetValue().length(); - } - if ( IsMultiLine() ) { + if (from == -1 && to == -1) + { + from = 0; + to = GetValue().length(); + } + GtkTextIter fromi, toi; gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); - gtk_text_buffer_place_cursor( m_buffer, &toi ); - gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi ); + gtk_text_buffer_select_range( m_buffer, &fromi, &toi ); } else // single line { @@ -1550,7 +1556,7 @@ void wxTextCtrl::OnChar( wxKeyEvent &key_event ) wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); event.SetEventObject(this); event.SetString(GetValue()); - if ( GetEventHandler()->ProcessEvent(event) ) + if ( HandleWindowEvent(event) ) return; } } @@ -1606,7 +1612,7 @@ void wxTextCtrl::ChangeFontGlobally() // // TODO: it can be implemented much more efficiently for GTK2 wxASSERT_MSG( IsMultiLine(), - _T("shouldn't be called for single line controls") ); + wxT("shouldn't be called for single line controls") ); wxString value = GetValue(); if ( !value.empty() ) @@ -1658,7 +1664,7 @@ bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) gint l = gtk_text_buffer_get_char_count( m_buffer ); wxCHECK_MSG( start >= 0 && end <= l, false, - _T("invalid range in wxTextCtrl::SetStyle") ); + wxT("invalid range in wxTextCtrl::SetStyle") ); GtkTextIter starti, endi; gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start ); @@ -1673,6 +1679,50 @@ bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) return false; } +bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) +{ + if ( !IsMultiLine() ) + { + // no styles for GtkEntry + return false; + } + + gint l = gtk_text_buffer_get_char_count( m_buffer ); + + wxCHECK_MSG( position >= 0 && position <= l, false, + wxT("invalid range in wxTextCtrl::GetStyle") ); + + GtkTextIter positioni; + gtk_text_buffer_get_iter_at_offset(m_buffer, &positioni, position); + + // Obtain a copy of the default attributes + GtkTextAttributes * const + pattr = gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text)); + wxON_BLOCK_EXIT1( g_free, pattr ); + + // And query GTK for the attributes at the given position using it as base + if ( !gtk_text_iter_get_attributes(&positioni, pattr) ) + { + style = m_defaultStyle; + } + else // have custom attributes + { + style.SetBackgroundColour(pattr->appearance.bg_color); + style.SetTextColour(pattr->appearance.fg_color); + + const wxGtkString + pangoFontString(pango_font_description_to_string(pattr->font)); + + wxFont font; + if ( font.SetNativeFontInfo(wxString(pangoFontString)) ) + style.SetFont(font); + + // TODO: set alignment, tabs and indents + } + + return true; +} + void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) { gtk_widget_modify_style(m_text, style); @@ -1741,60 +1791,52 @@ wxSize wxTextCtrl::DoGetBestSize() const // freeze/thaw // ---------------------------------------------------------------------------- -void wxTextCtrl::Freeze() +void wxTextCtrl::DoFreeze() { wxCHECK_RET(m_text != NULL, wxT("invalid text ctrl")); + wxWindow::DoFreeze(); + if ( HasFlag(wxTE_MULTILINE) ) { - if (m_freezeCount++ == 0) - { - // freeze textview updates and remove buffer - g_signal_connect (m_text, "expose_event", - G_CALLBACK (gtk_text_exposed_callback), this); - g_signal_connect (m_widget, "expose_event", - G_CALLBACK (gtk_text_exposed_callback), this); - gtk_widget_set_sensitive(m_widget, false); - g_object_ref(m_buffer); - GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL); - GtkTextMark* mark = GTK_TEXT_VIEW(m_text)->first_para_mark; - gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new); - // gtk_text_view_set_buffer adds its own reference - g_object_unref(buf_new); - // This mark should be deleted when the buffer is changed, - // but it's not (in GTK+ up to at least 2.10.6). - // Otherwise these anonymous marks start to build up in the buffer, - // and Freeze takes longer and longer each time it is called. - if (GTK_IS_TEXT_MARK(mark) && !gtk_text_mark_get_deleted(mark)) - gtk_text_buffer_delete_mark(m_buffer, mark); - } + GTKFreezeWidget(m_text); + + // removing buffer dramatically speeds up insertion: + g_object_ref(m_buffer); + GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL); + GtkTextMark* mark = GTK_TEXT_VIEW(m_text)->first_para_mark; + gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new); + // gtk_text_view_set_buffer adds its own reference + g_object_unref(buf_new); + // This mark should be deleted when the buffer is changed, + // but it's not (in GTK+ up to at least 2.10.6). + // Otherwise these anonymous marks start to build up in the buffer, + // and Freeze takes longer and longer each time it is called. + if (GTK_IS_TEXT_MARK(mark) && !gtk_text_mark_get_deleted(mark)) + gtk_text_buffer_delete_mark(m_buffer, mark); } } -void wxTextCtrl::Thaw() +void wxTextCtrl::DoThaw() { if ( HasFlag(wxTE_MULTILINE) ) { - wxCHECK_RET(m_freezeCount != 0, _T("Thaw() without matching Freeze()")); + // reattach buffer: + gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer); + g_object_unref(m_buffer); - if (--m_freezeCount == 0) + if (m_showPositionOnThaw != NULL) { - // Reattach buffer and thaw textview updates - gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer); - g_object_unref(m_buffer); - gtk_widget_set_sensitive(m_widget, true); - g_signal_handlers_disconnect_by_func (m_widget, - (gpointer) gtk_text_exposed_callback, this); - g_signal_handlers_disconnect_by_func (m_text, - (gpointer) gtk_text_exposed_callback, this); - if (m_showPositionOnThaw != NULL) - { - gtk_text_view_scroll_mark_onscreen( - GTK_TEXT_VIEW(m_text), m_showPositionOnThaw); - m_showPositionOnThaw = NULL; - } + gtk_text_view_scroll_mark_onscreen( + GTK_TEXT_VIEW(m_text), m_showPositionOnThaw); + m_showPositionOnThaw = NULL; } + + // and thaw the window + GTKThawWidget(m_text); } + + wxWindow::DoThaw(); } // ---------------------------------------------------------------------------- @@ -1821,13 +1863,11 @@ void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event) gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y); if (!gtk_text_iter_has_tag(&end, tag)) { - gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), - GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor); + SetCursor(wxCursor(wxCURSOR_IBEAM)); return; } - gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), - GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor); + SetCursor(wxCursor(wxCURSOR_HAND)); start = end; if(!gtk_text_iter_begins_tag(&start, tag)) @@ -1846,8 +1886,8 @@ void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event) InitCommandEvent(url_event); // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag) - //event.Skip(!GetEventHandler()->ProcessEvent(url_event)); - GetEventHandler()->ProcessEvent(url_event); + //event.Skip(!HandleWindowEvent(url_event)); + HandleWindowEvent(url_event); } bool wxTextCtrl::GTKProcessEvent(wxEvent& event) const