-wxTextCtrl::wxTextCtrl(void) : streambuf()
-{
-};
-
-wxTextCtrl::wxTextCtrl( wxWindow *parent, const wxWindowID id, const wxString &value,
- const wxPoint &pos, const wxSize &size,
- const int style, const wxString &name ) : streambuf()
-{
- Create( parent, id, value, pos, size, style, name );
-};
-
-bool wxTextCtrl::Create( wxWindow *parent, const wxWindowID id, const wxString &value,
- const wxPoint &pos, const wxSize &size,
- const int style, const wxString &name )
-{
- m_needParent = TRUE;
-
- PreCreation( parent, id, pos, size, style, name );
-
- if (style & wxTE_MULTILINE)
- m_widget = gtk_text_new( NULL, NULL );
- else
- m_widget = gtk_entry_new();
-
- wxSize newSize = size;
- if (newSize.x == -1) newSize.x = 80;
- if (newSize.y == -1) newSize.y = 26;
- SetSize( newSize.x, newSize.y );
-
- PostCreation();
-
- if (!value.IsNull())
- {
- gint tmp = 0;
- gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &tmp );
- };
-
- Show( TRUE );
-
- return TRUE;
-};
-
-wxString wxTextCtrl::GetValue(void) const
-{
- wxString tmp;
- if (m_windowStyle & wxTE_MULTILINE)
- {
- gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
- tmp = gtk_editable_get_chars( GTK_EDITABLE(m_widget), 0, len-1 );
- }
- else
- {
- tmp = gtk_entry_get_text( GTK_ENTRY(m_widget) );
- };
- return tmp;
-};
+void wxTextCtrl::Init()
+{
+ m_ignoreNextUpdate =
+ m_modified = FALSE;
+ m_updateFont = FALSE;
+ m_text =
+ m_vScrollbar = (GtkWidget *)NULL;
+}
+
+wxTextCtrl::wxTextCtrl( wxWindow *parent,
+ wxWindowID id,
+ const wxString &value,
+ const wxPoint &pos,
+ const wxSize &size,
+ long style,
+ const wxValidator& validator,
+ const wxString &name )
+{
+ Init();
+
+ Create( parent, id, value, pos, size, style, validator, name );
+}
+
+bool wxTextCtrl::Create( wxWindow *parent,
+ wxWindowID id,
+ const wxString &value,
+ const wxPoint &pos,
+ const wxSize &size,
+ long style,
+ const wxValidator& validator,
+ const wxString &name )
+{
+ m_needParent = TRUE;
+ m_acceptsFocus = TRUE;
+
+ if (!PreCreation( parent, pos, size ) ||
+ !CreateBase( parent, id, pos, size, style, validator, name ))
+ {
+ wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
+ return FALSE;
+ }
+
+
+ m_vScrollbarVisible = FALSE;
+
+ bool multi_line = (style & wxTE_MULTILINE) != 0;
+
+#ifdef __WXGTK20__
+ GtkTextBuffer *buffer = NULL;
+#endif
+
+ if (multi_line)
+ {
+#ifdef __WXGTK20__
+ // Create view
+ m_text = gtk_text_view_new();
+
+ buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
+
+ // create scrolled window
+ m_widget = gtk_scrolled_window_new( NULL, NULL );
+ gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
+ GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
+
+ // Insert view into scrolled window
+ gtk_container_add( GTK_CONTAINER(m_widget), m_text );
+
+ // Global settings which can be overridden by tags, I guess.
+ if (HasFlag( wxHSCROLL ))
+ gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE );
+ else
+ gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD );
+
+ if (!HasFlag(wxNO_BORDER))
+ gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
+#else
+ // create our control ...
+ m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
+
+ // ... and put into the upper left hand corner of the table
+ bool bHasHScrollbar = FALSE;
+ m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
+ GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
+ gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
+ (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
+ (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
+ 0, 0);
+
+ // always wrap words
+ gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
+
+ // finally, put the vertical scrollbar in the upper right corner
+ m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
+ GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
+ gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
+ GTK_FILL,
+ (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
+ 0, 0);
+#endif
+ }
+ else
+ {
+ // a single-line text control: no need for scrollbars
+ m_widget =
+ m_text = gtk_entry_new();
+ }
+
+ m_parent->DoAddChild( this );
+
+ m_focusWidget = m_text;
+
+ PostCreation();
+
+ SetFont( parent->GetFont() );
+
+ wxSize size_best( DoGetBestSize() );
+ wxSize new_size( size );
+ if (new_size.x == -1)
+ new_size.x = size_best.x;
+ if (new_size.y == -1)
+ new_size.y = size_best.y;
+ if ((new_size.x != size.x) || (new_size.y != size.y))
+ SetSize( new_size.x, new_size.y );
+
+ if (multi_line)
+ gtk_widget_show(m_text);
+
+#ifndef __WXGTK20__
+ if (multi_line)
+ {
+ gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
+ (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
+
+ // only initialize gs_gtk_text_draw once, starting from the next the
+ // klass::draw will already be wxgtk_text_draw
+ if ( !gs_gtk_text_draw )
+ {
+ GtkDrawCallback&
+ draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
+
+ gs_gtk_text_draw = draw;
+
+ draw = wxgtk_text_draw;
+ }
+ }
+#endif // GTK+ 1.x
+
+ if (!value.IsEmpty())
+ {
+#ifdef __WXGTK20__
+ SetValue( value );
+#else
+
+#if !GTK_CHECK_VERSION(1, 2, 0)
+ // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
+ // gtk_editable_insert_text()
+ gtk_widget_realize(m_text);
+#endif // GTK 1.0
+
+ gint tmp = 0;
+#if wxUSE_UNICODE
+ wxWX2MBbuf val = value.mbc_str();
+ gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
+#else
+ gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
+#endif
+
+ if (multi_line)
+ {
+ // Bring editable's cursor uptodate. Bug in GTK.
+ SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
+ }
+
+#endif
+ }
+
+ if (style & wxTE_PASSWORD)
+ {
+ if (!multi_line)
+ gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
+ }
+
+ if (style & wxTE_READONLY)
+ {
+ if (!multi_line)
+ gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
+#ifdef __WXGTK20__
+ else
+ gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
+ }
+#else
+ }
+ else
+ {
+ if (multi_line)
+ gtk_text_set_editable( GTK_TEXT(m_text), 1 );
+ }
+#endif
+
+ // We want to be notified about text changes.
+#ifdef __WXGTK20__
+ if (multi_line)
+ {
+ g_signal_connect( G_OBJECT(buffer), "changed",
+ GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
+ }
+ else
+#endif
+ {
+ gtk_signal_connect( GTK_OBJECT(m_text), "changed",
+ GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
+ }
+
+ // we don't set a valid background colour, because the window
+ // manager should use a default one
+ m_backgroundColour = wxColour();
+
+ wxColour colFg = parent->GetForegroundColour();
+ SetForegroundColour( colFg );
+
+ m_cursor = wxCursor( wxCURSOR_IBEAM );
+
+ wxTextAttr attrDef( colFg, m_backgroundColour, parent->GetFont() );
+ SetDefaultStyle( attrDef );
+
+ Show( TRUE );
+
+ return TRUE;
+}
+
+void wxTextCtrl::CalculateScrollbar()
+{
+#ifndef __WXGTK20__
+ if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
+
+ GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
+
+ if (adj->upper - adj->page_size < 0.8)
+ {
+ if (m_vScrollbarVisible)
+ {
+ gtk_widget_hide( m_vScrollbar );
+ m_vScrollbarVisible = FALSE;
+ }
+ }
+ else
+ {
+ if (!m_vScrollbarVisible)
+ {
+ gtk_widget_show( m_vScrollbar );
+ m_vScrollbarVisible = TRUE;
+ }
+ }
+#endif
+}
+
+wxString wxTextCtrl::GetValue() const
+{
+ wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
+
+ wxString tmp;
+ if (m_windowStyle & wxTE_MULTILINE)
+ {
+#ifdef __WXGTK20__
+ GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
+
+ GtkTextIter start;
+ gtk_text_buffer_get_start_iter( text_buffer, &start );
+ GtkTextIter end;
+ gtk_text_buffer_get_end_iter( text_buffer, &end );
+ gchar *text = gtk_text_buffer_get_text( text_buffer, &start, &end, TRUE );
+
+#if wxUSE_UNICODE
+ wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
+#else
+ wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
+#endif
+ tmp = buffer;
+
+ g_free( text );
+#else
+ gint len = gtk_text_get_length( GTK_TEXT(m_text) );
+ char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
+ tmp = text;
+ g_free( text );
+#endif
+ }
+ else
+ {
+ tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
+ }
+
+ return tmp;
+}