#include "wx/intl.h"
#include "wx/log.h"
#include "wx/utils.h"
- #include "wx/panel.h"
#include "wx/settings.h"
#include "wx/math.h"
#endif
#include <ctype.h>
#include "wx/gtk/private.h"
-#include <gdk/gdkkeysyms.h>
// ----------------------------------------------------------------------------
// helpers
}
extern "C" {
-static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
+static void wxGtkTextApplyTagsFromAttr(GtkWidget *text,
+ GtkTextBuffer *text_buffer,
const wxTextAttr& attr,
GtkTextIter *start,
GtkTextIter *end)
"justification", align, NULL );
gtk_text_buffer_apply_tag( text_buffer, tag, ¶_start, ¶_end );
}
+
+ if (attr.HasLeftIndent())
+ {
+ // Indentation attribute
+
+ // Clear old indentation tags
+ GtkTextIter para_start, para_end = *end;
+ gtk_text_buffer_get_iter_at_line( text_buffer,
+ ¶_start,
+ gtk_text_iter_get_line(start) );
+ gtk_text_iter_forward_line(¶_end);
+
+ remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
+ G_CALLBACK(wxGtkOnRemoveTag),
+ gpointer("WXINDENT"));
+ gtk_text_buffer_remove_all_tags( text_buffer, ¶_start, ¶_end );
+ g_signal_handler_disconnect (text_buffer, remove_handler_id);
+
+ // 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)) /
+ 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());
+
+ gint gindent;
+ gint gsubindent;
+
+ if (subIndent >= 0)
+ {
+ gindent = indent;
+ gsubindent = -subIndent;
+ }
+ else
+ {
+ gindent = -subIndent;
+ gsubindent = indent;
+ }
+
+ g_snprintf(buf, sizeof(buf), "WXINDENT %d %d", gindent, gsubindent);
+ tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
+ buf );
+ if (!tag)
+ tag = gtk_text_buffer_create_tag( text_buffer, buf,
+ "left-margin", gindent, "indent", gsubindent, NULL );
+ gtk_text_buffer_apply_tag (text_buffer, tag, ¶_start, ¶_end);
+ }
+
+ if (attr.HasTabs())
+ {
+ // Set tab stops
+
+ // Clear old tabs
+ GtkTextIter para_start, para_end = *end;
+ gtk_text_buffer_get_iter_at_line( text_buffer,
+ ¶_start,
+ gtk_text_iter_get_line(start) );
+ gtk_text_iter_forward_line(¶_end);
+
+ remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
+ G_CALLBACK(wxGtkOnRemoveTag),
+ gpointer("WXTABS"));
+ gtk_text_buffer_remove_all_tags( text_buffer, ¶_start, ¶_end );
+ g_signal_handler_disconnect (text_buffer, remove_handler_id);
+
+ const wxArrayInt& tabs = attr.GetTabs();
+
+ wxString tagname = _T("WXTABS");
+ g_snprintf(buf, sizeof(buf), "WXTABS");
+ for (size_t i = 0; i < tabs.GetCount(); i++)
+ tagname += wxString::Format(_T(" %d"), tabs[i]);
+
+ const wxWX2MBbuf buf = tagname.mb_str(wxConvUTF8);
+
+ tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
+ buf );
+ 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)) /
+ 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++)
+ pango_tab_array_set_tab(tabArray, i, PANGO_TAB_LEFT, (gint)(tabs[i] * factor));
+ tag = gtk_text_buffer_create_tag( text_buffer, buf,
+ "tabs", tabArray, NULL );
+ pango_tab_array_free(tabArray);
+ }
+ gtk_text_buffer_apply_tag (text_buffer, tag, ¶_start, ¶_end);
+ }
}
}
gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
- wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
+ wxGtkTextApplyTagsFromAttr(text, text_buffer, attr, &start, &iter);
}
}
if ( win->MarkDirtyOnChange() )
win->MarkDirty();
- wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
- event.SetEventObject( win );
- win->GetEventHandler()->ProcessEvent( event );
+ win->SendTextUpdatedEvent();
}
}
void wxTextCtrl::Init()
{
m_dontMarkDirty =
- m_ignoreNextUpdate =
m_modified = false;
+ m_countUpdatesToIgnore = 0;
+
SetUpdateFont(false);
m_text = NULL;
return enc;
}
-void wxTextCtrl::SetValue( const wxString &value )
+bool wxTextCtrl::IsEmpty() const
+{
+ if ( IsMultiLine() )
+ return gtk_text_buffer_get_char_count(m_buffer) != 0;
+
+ return wxTextCtrlBase::IsEmpty();
+}
+
+void wxTextCtrl::DoSetValue( const wxString &value, int flags )
{
wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
m_modified = false;
DontMarkDirtyOnNextChange();
- if ( IsMultiLine() )
+ const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding()));
+ if ( !buffer )
{
- const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding()));
- if ( !buffer )
- {
- // see comment in WriteText() as to why we must warn the user about
- // this
- wxLogWarning(_("Failed to set text in the text control."));
- return;
- }
+ // see comment in WriteText() as to why we must warn the user about
+ // this
+ wxLogWarning(_("Failed to set text in the text control."));
+ return;
+ }
- if (gtk_text_buffer_get_char_count(m_buffer) != 0)
- IgnoreNextTextUpdate();
+ // if the control is not empty, two "changed" signals are emitted,
+ // otherwise only one and we need to ignore either both or one of them
+ int ignore = flags & SetValue_SendEvent ? 0 : 1;
+ if ( !IsEmpty() )
+ ignore++;
+ if ( ignore )
+ IgnoreNextTextUpdate(ignore);
+
+ if ( IsMultiLine() )
+ {
gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
}
else // single line
{
- // gtk_entry_set_text() emits two "changed" signals if the control is
- // not empty because internally it calls gtk_editable_delete_text() and
- // gtk_editable_insert_text() but we want to have only one event
- if ( !GetValue().empty() )
- IgnoreNextTextUpdate();
-
- gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV(value) );
+ gtk_entry_set_text( GTK_ENTRY(m_text), buffer );
}
+ // if, for whatever reason, the callback wasn't called the expected number
+ // of times, still reset the flags to the default values
+ m_dontMarkDirty = false;
+ m_countUpdatesToIgnore = 0;
+
+
// GRG, Jun/2000: Changed this after a lot of discussion in
// the lists. wxWidgets 2.2 will have a set of flags to
// customize this behaviour.
bool wxTextCtrl::IgnoreTextUpdate()
{
- if ( m_ignoreNextUpdate )
+ if ( m_countUpdatesToIgnore > 0 )
{
- m_ignoreNextUpdate = false;
+ m_countUpdatesToIgnore--;
return true;
}
// colours for the others
wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
- wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
+ wxGtkTextApplyTagsFromAttr( m_widget, m_buffer, attr, &starti, &endi );
return true;
}
G_CALLBACK (gtk_text_exposed_callback), this);
gtk_widget_set_sensitive(m_widget, false);
g_object_ref(m_buffer);
- gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
+ GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL);
+ 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);
}
}
}