1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
18 #include "wx/settings.h"
20 #include "wx/strconv.h"
21 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
23 #include <sys/types.h>
28 #include "wx/gtk/private.h"
29 #include <gdk/gdkkeysyms.h>
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern wxCursor g_globalCursor
;
43 extern wxWindowGTK
*g_delayedFocus
;
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
51 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
58 g_object_get (tag
, "name", &name
, NULL
);
60 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
61 // anonymous tag or not starting with prefix - don't remove
62 g_signal_stop_emission_by_name(buffer
, "remove_tag");
69 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer
*text_buffer
,
70 const wxTextAttr
& attr
,
74 static gchar buf
[1024];
77 gulong remove_handler_id
= g_signal_connect( text_buffer
, "remove_tag",
78 G_CALLBACK(wxGtkOnRemoveTag
), gpointer("WX"));
79 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
80 g_signal_handler_disconnect( text_buffer
, remove_handler_id
);
85 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
86 font_string
= pango_font_description_to_string(font_description
);
87 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
);
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 "font-desc", font_description
,
94 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
98 if (attr
.HasTextColour())
100 GdkColor
*colFg
= attr
.GetTextColour().GetColor();
101 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
102 colFg
->red
, colFg
->green
, colFg
->blue
);
103 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
106 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
107 "foreground-gdk", colFg
, NULL
);
108 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
111 if (attr
.HasBackgroundColour())
113 GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
114 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
115 colBg
->red
, colBg
->green
, colBg
->blue
);
116 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
119 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
120 "background-gdk", colBg
, NULL
);
121 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
124 if (attr
.HasAlignment())
126 GtkTextIter para_start
, para_end
= *end
;
127 gtk_text_buffer_get_iter_at_line( text_buffer
,
129 gtk_text_iter_get_line(start
) );
130 gtk_text_iter_forward_line(¶_end
);
132 remove_handler_id
= g_signal_connect( text_buffer
, "remove_tag",
133 G_CALLBACK(wxGtkOnRemoveTag
),
134 gpointer("WXALIGNMENT"));
135 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
136 g_signal_handler_disconnect( text_buffer
, remove_handler_id
);
138 GtkJustification align
;
139 switch (attr
.GetAlignment())
142 align
= GTK_JUSTIFY_LEFT
;
144 case wxTEXT_ALIGNMENT_RIGHT
:
145 align
= GTK_JUSTIFY_RIGHT
;
147 case wxTEXT_ALIGNMENT_CENTER
:
148 align
= GTK_JUSTIFY_CENTER
;
150 // gtk+ doesn't support justify as of gtk+-2.7.4
153 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
154 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
157 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
158 "justification", align
, NULL
);
159 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
165 static void wxGtkTextInsert(GtkWidget
*text
,
166 GtkTextBuffer
*text_buffer
,
167 const wxTextAttr
& attr
,
168 const wxCharBuffer
& buffer
)
172 GtkTextIter iter
, start
;
174 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
175 gtk_text_buffer_get_insert (text_buffer
) );
176 start_offset
= gtk_text_iter_get_offset (&iter
);
177 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
179 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
181 wxGtkTextApplyTagsFromAttr(text_buffer
, attr
, &start
, &iter
);
186 static void wxGtkTextInsert(GtkWidget
*text
,
187 const wxTextAttr
& attr
,
191 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
194 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
197 GdkColor
*colBg
= attr
.HasBackgroundColour()
198 ? attr
.GetBackgroundColour().GetColor()
201 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
206 // ----------------------------------------------------------------------------
207 // "insert_text" for GtkEntry
208 // ----------------------------------------------------------------------------
212 gtk_insert_text_callback(GtkEditable
*editable
,
213 const gchar
*new_text
,
214 gint new_text_length
,
219 wxapp_install_idle_handler();
221 // we should only be called if we have a max len limit at all
222 GtkEntry
*entry
= GTK_ENTRY (editable
);
224 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
226 // check that we don't overflow the max length limit
228 // FIXME: this doesn't work when we paste a string which is going to be
230 if ( entry
->text_length
== entry
->text_max_length
)
232 // we don't need to run the base class version at all
233 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
235 // remember that the next changed signal is to be ignored to avoid
236 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
237 win
->IgnoreNextTextUpdate();
239 // and generate the correct one ourselves
240 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
241 event
.SetEventObject(win
);
242 event
.SetString(win
->GetValue());
243 win
->GetEventHandler()->ProcessEvent( event
);
249 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
253 au_apply_tag_callback(GtkTextBuffer
*buffer
,
259 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
260 g_signal_stop_emission_by_name(buffer
, "apply_tag");
264 //-----------------------------------------------------------------------------
265 // GtkTextCharPredicates for gtk_text_iter_*_find_char
266 //-----------------------------------------------------------------------------
270 pred_whitespace (gunichar ch
, gpointer user_data
)
272 return g_unichar_isspace(ch
);
278 pred_non_whitespace (gunichar ch
, gpointer user_data
)
280 return !g_unichar_isspace(ch
);
286 pred_nonpunct (gunichar ch
, gpointer user_data
)
288 return !g_unichar_ispunct(ch
);
294 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
296 return !g_unichar_ispunct(ch
) || ch
== '/';
300 //-----------------------------------------------------------------------------
301 // Check for links between s and e and correct tags as necessary
302 //-----------------------------------------------------------------------------
304 // This function should be made match better while being efficient at one point.
305 // Most probably with a row of regular expressions.
308 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
310 static const char *URIPrefixes
[] =
328 GtkTextIter start
= *s
, end
= *e
;
329 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
331 // Get our special link tag
332 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
334 // Get rid of punctuation from beginning and end.
335 // Might want to move this to au_check_range if an improved link checking doesn't
336 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
337 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
338 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
340 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
341 gtk_text_iter_forward_char(&end
);
343 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
344 size_t len
= strlen(text
), prefix_len
;
347 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
349 prefix_len
= strlen(URIPrefixes
[n
]);
350 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
354 if(n
< WXSIZEOF(URIPrefixes
))
356 gulong signal_id
= g_signal_handler_find(buffer
,
357 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
359 (gpointer
)au_apply_tag_callback
, NULL
);
361 g_signal_handler_block(buffer
, signal_id
);
362 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
363 g_signal_handler_unblock(buffer
, signal_id
);
370 au_check_range(GtkTextIter
*s
,
371 GtkTextIter
*range_end
)
373 GtkTextIter range_start
= *s
;
374 GtkTextIter word_end
;
375 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
376 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
378 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
380 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
381 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
383 while(!gtk_text_iter_equal(&range_start
, range_end
))
385 word_end
= range_start
;
386 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
388 // Now we should have a word delimited by range_start and word_end, correct link tags
389 au_check_word(&range_start
, &word_end
);
391 range_start
= word_end
;
392 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
397 //-----------------------------------------------------------------------------
398 // "insert-text" for GtkTextBuffer
399 //-----------------------------------------------------------------------------
403 au_insert_text_callback(GtkTextBuffer
*buffer
,
409 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
412 GtkTextIter start
= *end
;
413 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
415 GtkTextIter line_start
= start
;
416 GtkTextIter line_end
= *end
;
417 GtkTextIter words_start
= start
;
418 GtkTextIter words_end
= *end
;
420 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
421 gtk_text_iter_forward_to_line_end(&line_end
);
422 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
423 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
425 au_check_range(&words_start
, &words_end
);
429 //-----------------------------------------------------------------------------
430 // "delete-range" for GtkTextBuffer
431 //-----------------------------------------------------------------------------
435 au_delete_range_callback(GtkTextBuffer
*buffer
,
440 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
443 GtkTextIter line_start
= *start
, line_end
= *end
;
445 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
446 gtk_text_iter_forward_to_line_end(&line_end
);
447 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
448 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
450 au_check_range(start
, end
);
457 //-----------------------------------------------------------------------------
459 //-----------------------------------------------------------------------------
463 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
465 if ( win
->IgnoreTextUpdate() )
468 if (!win
->m_hasVMT
) return;
471 wxapp_install_idle_handler();
475 win
->UpdateFontIfNeeded();
476 #endif // !__WXGTK20__
478 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
479 event
.SetEventObject( win
);
480 win
->GetEventHandler()->ProcessEvent( event
);
484 //-----------------------------------------------------------------------------
485 // "expose_event" from scrolled window and textview
486 //-----------------------------------------------------------------------------
491 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
498 //-----------------------------------------------------------------------------
499 // "changed" from vertical scrollbar
500 //-----------------------------------------------------------------------------
505 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
507 if (!win
->m_hasVMT
) return;
510 wxapp_install_idle_handler();
512 win
->CalculateScrollbar();
517 // ----------------------------------------------------------------------------
518 // redraw callback for multiline text
519 // ----------------------------------------------------------------------------
523 // redrawing a GtkText from inside a wxYield() call results in crashes (the
524 // text sample shows it in its "Add lines" command which shows wxProgressDialog
525 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
526 // don't do anything if we're inside wxYield()
528 extern bool wxIsInsideYield
;
531 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
534 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
537 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
539 if ( !wxIsInsideYield
)
541 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
542 _T("infinite recursion in wxgtk_text_draw aborted") );
544 gs_gtk_text_draw(widget
, rect
);
549 #endif // __WXGTK20__
551 //-----------------------------------------------------------------------------
553 //-----------------------------------------------------------------------------
555 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
557 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
558 EVT_CHAR(wxTextCtrl::OnChar
)
560 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
561 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
562 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
563 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
564 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
566 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
567 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
568 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
569 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
570 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
573 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
574 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
575 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
576 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
577 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
578 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
579 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
580 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
581 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
585 void wxTextCtrl::Init()
589 SetUpdateFont(false);
591 m_vScrollbar
= (GtkWidget
*)NULL
;
594 m_gdkHandCursor
= NULL
;
595 m_gdkXTermCursor
= NULL
;
599 wxTextCtrl::~wxTextCtrl()
603 gdk_cursor_unref(m_gdkHandCursor
);
605 gdk_cursor_unref(m_gdkXTermCursor
);
609 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
611 const wxString
&value
,
615 const wxValidator
& validator
,
616 const wxString
&name
)
620 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
623 bool wxTextCtrl::Create( wxWindow
*parent
,
625 const wxString
&value
,
629 const wxValidator
& validator
,
630 const wxString
&name
)
633 m_acceptsFocus
= true;
635 if (!PreCreation( parent
, pos
, size
) ||
636 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
638 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
643 m_vScrollbarVisible
= false;
645 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
651 m_text
= gtk_text_view_new();
653 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
655 // create scrolled window
656 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
657 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
658 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
660 // Insert view into scrolled window
661 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
663 // translate wx wrapping style to GTK+
665 if ( HasFlag( wxTE_DONTWRAP
) )
666 wrap
= GTK_WRAP_NONE
;
667 else if ( HasFlag( wxTE_CHARWRAP
) )
668 wrap
= GTK_WRAP_CHAR
;
669 else if ( HasFlag( wxTE_WORDWRAP
) )
670 wrap
= GTK_WRAP_WORD
;
671 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
673 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
675 if ( !gtk_check_version(2,4,0) )
677 wrap
= GTK_WRAP_WORD_CHAR
;
681 wrap
= GTK_WRAP_WORD
;
684 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
686 if (!HasFlag(wxNO_BORDER
))
687 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget
), GTK_SHADOW_IN
);
689 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
691 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
693 // create our control ...
694 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
696 // ... and put into the upper left hand corner of the table
697 bool bHasHScrollbar
= false;
698 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
699 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
700 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
701 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
702 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
706 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
708 // finally, put the vertical scrollbar in the upper right corner
709 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
710 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
711 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
713 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
719 // a single-line text control: no need for scrollbars
721 m_text
= gtk_entry_new();
724 if (style
& wxNO_BORDER
)
725 g_object_set( GTK_ENTRY(m_text
), "has-frame", FALSE
, NULL
);
729 m_parent
->DoAddChild( this );
731 m_focusWidget
= m_text
;
736 gtk_widget_show(m_text
);
741 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
742 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
744 // only initialize gs_gtk_text_draw once, starting from the next the
745 // klass::draw will already be wxgtk_text_draw
746 if ( !gs_gtk_text_draw
)
749 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
751 gs_gtk_text_draw
= draw
;
753 draw
= wxgtk_text_draw
;
764 #if !GTK_CHECK_VERSION(1, 2, 0)
765 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
766 // gtk_editable_insert_text()
767 gtk_widget_realize(m_text
);
772 wxWX2MBbuf val
= value
.mbc_str();
773 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
775 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
780 // Bring editable's cursor uptodate. Bug in GTK.
781 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
787 if (style
& wxTE_PASSWORD
)
790 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
793 if (style
& wxTE_READONLY
)
796 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
799 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
805 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
812 if (style
& wxTE_RIGHT
)
813 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_RIGHT
);
814 else if (style
& wxTE_CENTRE
)
815 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_CENTER
);
816 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
821 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
822 if (!gtk_check_version(2,4,0))
824 if (style
& wxTE_RIGHT
)
825 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 1.0 );
826 else if (style
& wxTE_CENTRE
)
827 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 0.5 );
831 #endif // __WXGTK20__
833 // We want to be notified about text changes.
837 g_signal_connect( G_OBJECT(m_buffer
), "changed",
838 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
840 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
841 if (style
& wxTE_AUTO_URL
)
843 GtkTextIter start
, end
;
844 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
845 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
847 // We create our wxUrl tag here for slight efficiency gain - we
848 // don't have to check for the tag existance in callbacks,
849 // hereby it's guaranteed to exist.
850 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
851 "foreground", "blue",
852 "underline", PANGO_UNDERLINE_SINGLE
,
855 // Check for URLs after each text change
856 g_signal_connect_after( G_OBJECT(m_buffer
), "insert_text",
857 GTK_SIGNAL_FUNC(au_insert_text_callback
), (gpointer
)this);
858 g_signal_connect_after( G_OBJECT(m_buffer
), "delete_range",
859 GTK_SIGNAL_FUNC(au_delete_range_callback
), (gpointer
)this);
861 // Block all wxUrl tag applying unless we do it ourselves, in which case we
862 // block this callback temporarily. This takes care of gtk+ internal
863 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
864 // which is undesired because only a part of the URL might be copied.
865 // The insert-text signal emitted inside it will take care of newly formed
866 // or wholly copied URLs.
867 g_signal_connect( G_OBJECT(m_buffer
), "apply_tag",
868 GTK_SIGNAL_FUNC(au_apply_tag_callback
), NULL
);
870 // Check for URLs in the initial string passed to Create
871 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
872 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
873 au_check_range(&start
, &end
);
880 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
881 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
884 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
886 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
887 SetDefaultStyle( attrDef
);
893 void wxTextCtrl::CalculateScrollbar()
896 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
898 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
900 if (adj
->upper
- adj
->page_size
< 0.8)
902 if (m_vScrollbarVisible
)
904 gtk_widget_hide( m_vScrollbar
);
905 m_vScrollbarVisible
= false;
910 if (!m_vScrollbarVisible
)
912 gtk_widget_show( m_vScrollbar
);
913 m_vScrollbarVisible
= true;
919 wxString
wxTextCtrl::GetValue() const
921 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
924 if (m_windowStyle
& wxTE_MULTILINE
)
928 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
930 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
931 gchar
*text
= gtk_text_buffer_get_text( m_buffer
, &start
, &end
, TRUE
);
934 wxWCharBuffer
buffer( wxConvUTF8
.cMB2WX( text
) );
936 wxCharBuffer
buffer( wxConvLocal
.cWC2WX( wxConvUTF8
.cMB2WC( text
) ) );
943 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
944 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
951 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
957 void wxTextCtrl::SetValue( const wxString
&value
)
959 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
961 if (m_windowStyle
& wxTE_MULTILINE
)
966 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( value
) );
968 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( value
) ) );
970 if (gtk_text_buffer_get_char_count(m_buffer
) != 0)
971 IgnoreNextTextUpdate();
975 // what else can we do? at least don't crash...
979 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
982 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
983 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
985 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
990 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
993 // GRG, Jun/2000: Changed this after a lot of discussion in
994 // the lists. wxWidgets 2.2 will have a set of flags to
995 // customize this behaviour.
996 SetInsertionPoint(0);
1001 void wxTextCtrl::WriteText( const wxString
&text
)
1003 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1008 // gtk_text_changed_callback() will set m_modified to true but m_modified
1009 // shouldn't be changed by the program writing to the text control itself,
1010 // so save the old value and restore when we're done
1011 bool oldModified
= m_modified
;
1013 if ( m_windowStyle
& wxTE_MULTILINE
)
1018 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
1020 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
1024 // what else can we do? at least don't crash...
1028 // TODO: Call whatever is needed to delete the selection.
1029 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1031 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1032 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1033 if ( wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
) )
1035 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1036 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1039 // After cursor movements, gtk_text_get_point() is wrong by one.
1040 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
1042 // always use m_defaultStyle, even if it is empty as otherwise
1043 // resetting the style and appending some more text wouldn't work: if
1044 // we don't specify the style explicitly, the old style would be used
1045 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1046 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.Len());
1048 // we called wxGtkTextInsert with correct font, no need to do anything
1049 // in UpdateFontIfNeeded() any longer
1050 if ( !text
.empty() )
1052 SetUpdateFont(false);
1055 // Bring editable's cursor back uptodate.
1056 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
1057 #endif // GTK 1.x/2.0
1061 // First remove the selection if there is one
1062 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1064 // This moves the cursor pos to behind the inserted text.
1065 gint len
= GET_EDITABLE_POS(m_text
);
1070 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
1072 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
1076 // what else can we do? at least don't crash...
1080 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1083 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.Len(), &len
);
1086 // Bring entry's cursor uptodate.
1087 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
1090 m_modified
= oldModified
;
1093 void wxTextCtrl::AppendText( const wxString
&text
)
1095 SetInsertionPointEnd();
1099 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1101 if (m_windowStyle
& wxTE_MULTILINE
)
1104 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
1105 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
1111 int currentLine
= 0;
1112 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
1113 if (text
[i
] == '\n')
1117 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
1125 return wxEmptyString
;
1129 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1130 GtkTextIter end
= line
;
1131 gtk_text_iter_forward_to_line_end(&end
);
1132 gchar
*text
= gtk_text_buffer_get_text(m_buffer
,&line
,&end
,TRUE
);
1133 wxString
result(wxGTK_CONV_BACK(text
));
1140 if (lineNo
== 0) return GetValue();
1141 return wxEmptyString
;
1145 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1147 /* If you implement this, don't forget to update the documentation!
1148 * (file docs/latex/wx/text.tex) */
1149 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1152 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1154 if ( m_windowStyle
& wxTE_MULTILINE
)
1158 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1159 if (gtk_text_iter_is_end(&iter
))
1162 *y
= gtk_text_iter_get_line(&iter
);
1163 *x
= gtk_text_iter_get_line_offset(&iter
);
1165 wxString text
= GetValue();
1167 // cast to prevent warning. But pos really should've been unsigned.
1168 if( (unsigned long)pos
> text
.Len() )
1174 const wxChar
* stop
= text
.c_str() + pos
;
1175 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
1177 if (*p
== wxT('\n'))
1187 else // single line control
1189 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1196 // index out of bounds
1204 long wxTextCtrl::XYToPosition(long x
, long y
) const
1206 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
1210 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1213 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1214 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1217 return gtk_text_iter_get_offset(&iter
) + x
;
1220 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
1227 int wxTextCtrl::GetLineLength(long lineNo
) const
1230 if (m_windowStyle
& wxTE_MULTILINE
)
1232 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1233 if (lineNo
> last_line
)
1237 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1238 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1239 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1244 wxString str
= GetLineText (lineNo
);
1245 return (int) str
.Length();
1249 int wxTextCtrl::GetNumberOfLines() const
1251 if (m_windowStyle
& wxTE_MULTILINE
)
1254 return gtk_text_buffer_get_line_count( m_buffer
);
1256 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
1257 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
1261 int currentLine
= 0;
1262 for (int i
= 0; i
< len
; i
++ )
1264 if (text
[i
] == '\n')
1269 // currentLine is 0 based, add 1 to get number of lines
1270 return currentLine
+ 1;
1284 void wxTextCtrl::SetInsertionPoint( long pos
)
1286 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1288 if ( IsMultiLine() )
1292 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1293 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1294 gtk_text_view_scroll_mark_onscreen
1296 GTK_TEXT_VIEW(m_text
),
1297 gtk_text_buffer_get_insert( m_buffer
)
1300 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
1301 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
1303 /* we fake a set_point by inserting and deleting. as the user
1304 isn't supposed to get to know about this non-sense, we
1305 disconnect so that no events are sent to the user program. */
1307 gint tmp
= (gint
)pos
;
1308 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
1309 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
1311 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
1312 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
1314 // bring editable's cursor uptodate. Bug in GTK.
1315 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
1320 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
1322 // Bring editable's cursor uptodate. Bug in GTK.
1323 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
1327 void wxTextCtrl::SetInsertionPointEnd()
1329 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1331 if (m_windowStyle
& wxTE_MULTILINE
)
1335 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1336 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1338 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
1343 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
1347 void wxTextCtrl::SetEditable( bool editable
)
1349 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1351 if (m_windowStyle
& wxTE_MULTILINE
)
1354 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1356 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
1361 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
1365 bool wxTextCtrl::Enable( bool enable
)
1367 if (!wxWindowBase::Enable(enable
))
1373 if (m_windowStyle
& wxTE_MULTILINE
)
1376 SetEditable( enable
);
1378 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
1379 OnParentEnable(enable
);
1384 gtk_widget_set_sensitive( m_text
, enable
);
1390 // wxGTK-specific: called recursively by Enable,
1391 // to give widgets an oppprtunity to correct their colours after they
1392 // have been changed by Enable
1393 void wxTextCtrl::OnParentEnable( bool enable
)
1395 // If we have a custom background colour, we use this colour in both
1396 // disabled and enabled mode, or we end up with a different colour under the
1398 wxColour oldColour
= GetBackgroundColour();
1401 // Need to set twice or it'll optimize the useful stuff out
1402 if (oldColour
== * wxWHITE
)
1403 SetBackgroundColour(*wxBLACK
);
1405 SetBackgroundColour(*wxWHITE
);
1406 SetBackgroundColour(oldColour
);
1410 void wxTextCtrl::MarkDirty()
1415 void wxTextCtrl::DiscardEdits()
1420 // ----------------------------------------------------------------------------
1421 // max text length support
1422 // ----------------------------------------------------------------------------
1424 void wxTextCtrl::IgnoreNextTextUpdate()
1426 m_ignoreNextUpdate
= true;
1429 bool wxTextCtrl::IgnoreTextUpdate()
1431 if ( m_ignoreNextUpdate
)
1433 m_ignoreNextUpdate
= false;
1441 void wxTextCtrl::SetMaxLength(unsigned long len
)
1443 if ( !HasFlag(wxTE_MULTILINE
) )
1445 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1447 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1448 // we had tried to enter more text than allowed by max text length and
1449 // the text wasn't really changed
1451 // to detect this and generate TEXT_MAXLEN event instead of
1452 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1454 // when max len is set to 0 we disconnect our handler as it means that
1455 // we shouldn't check anything any more
1458 gtk_signal_connect( GTK_OBJECT(m_text
),
1460 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
1465 gtk_signal_disconnect_by_func
1468 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
1475 void wxTextCtrl::SetSelection( long from
, long to
)
1477 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1479 if (from
== -1 && to
== -1)
1482 to
= GetValue().Length();
1486 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
1487 !GTK_TEXT(m_text
)->line_start_cache
)
1489 // tell the programmer that it didn't work
1490 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1495 if (m_windowStyle
& wxTE_MULTILINE
)
1498 GtkTextIter fromi
, toi
;
1499 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1500 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1502 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1503 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1505 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1510 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1514 void wxTextCtrl::ShowPosition( long pos
)
1516 if (m_windowStyle
& wxTE_MULTILINE
)
1520 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1521 gtk_text_iter_set_offset( &iter
, pos
);
1522 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1523 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1525 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
1526 float totalLines
= (float) GetNumberOfLines();
1529 PositionToXY(pos
, &posX
, &posY
);
1530 float posLine
= (float) posY
;
1531 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
1532 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
1533 #endif // GTK 1.x/2.x
1539 wxTextCtrlHitTestResult
1540 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1542 if ( !IsMultiLine() )
1545 return wxTE_HT_UNKNOWN
;
1549 gtk_text_view_window_to_buffer_coords
1551 GTK_TEXT_VIEW(m_text
),
1552 GTK_TEXT_WINDOW_TEXT
,
1558 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1560 *pos
= gtk_text_iter_get_offset(&iter
);
1562 return wxTE_HT_ON_TEXT
;
1565 #endif // __WXGTK20__
1567 long wxTextCtrl::GetInsertionPoint() const
1569 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1572 if (m_windowStyle
& wxTE_MULTILINE
)
1574 // There is no direct accessor for the cursor, but
1575 // internally, the cursor is the "mark" called
1576 // "insert" in the text view's btree structure.
1578 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1580 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1582 return gtk_text_iter_get_offset( &cursor
);
1587 return (long) GET_EDITABLE_POS(m_text
);
1591 wxTextPos
wxTextCtrl::GetLastPosition() const
1593 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1597 if (m_windowStyle
& wxTE_MULTILINE
)
1601 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1603 pos
= gtk_text_iter_get_offset( &end
);
1605 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
1610 pos
= GTK_ENTRY(m_text
)->text_length
;
1616 void wxTextCtrl::Remove( long from
, long to
)
1618 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1621 if (m_windowStyle
& wxTE_MULTILINE
)
1623 GtkTextIter fromi
, toi
;
1624 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1625 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1627 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1631 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1634 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1636 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1643 SetInsertionPoint( from
);
1646 gint pos
= (gint
)from
;
1648 wxWX2MBbuf buf
= value
.mbc_str();
1649 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
1651 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
1652 #endif // wxUSE_UNICODE
1653 #endif // GTK 1.x/2.x
1657 void wxTextCtrl::Cut()
1659 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1662 if (m_windowStyle
& wxTE_MULTILINE
)
1663 g_signal_emit_by_name(m_text
, "cut-clipboard");
1666 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1669 void wxTextCtrl::Copy()
1671 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1674 if (m_windowStyle
& wxTE_MULTILINE
)
1675 g_signal_emit_by_name(m_text
, "copy-clipboard");
1678 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1681 void wxTextCtrl::Paste()
1683 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1686 if (m_windowStyle
& wxTE_MULTILINE
)
1687 g_signal_emit_by_name(m_text
, "paste-clipboard");
1690 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1694 void wxTextCtrl::Undo()
1697 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1700 void wxTextCtrl::Redo()
1703 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1706 bool wxTextCtrl::CanUndo() const
1709 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1713 bool wxTextCtrl::CanRedo() const
1716 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1720 // If the return values from and to are the same, there is no
1722 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1724 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1728 bool haveSelection
= false;
1731 if (m_windowStyle
& wxTE_MULTILINE
)
1733 GtkTextIter ifrom
, ito
;
1734 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1736 haveSelection
= true;
1737 from
= gtk_text_iter_get_offset(&ifrom
);
1738 to
= gtk_text_iter_get_offset(&ito
);
1741 else // not multi-line
1743 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1746 haveSelection
= true;
1750 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
1752 haveSelection
= true;
1753 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
1754 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
1758 if (! haveSelection
)
1759 from
= to
= GetInsertionPoint();
1763 // exchange them to be compatible with wxMSW
1776 bool wxTextCtrl::IsEditable() const
1778 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1781 if (m_windowStyle
& wxTE_MULTILINE
)
1783 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1787 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1790 return GTK_EDITABLE(m_text
)->editable
;
1794 bool wxTextCtrl::IsModified() const
1799 void wxTextCtrl::Clear()
1801 SetValue( wxEmptyString
);
1804 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1806 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1808 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1810 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1811 event
.SetEventObject(this);
1812 event
.SetString(GetValue());
1813 if (GetEventHandler()->ProcessEvent(event
)) return;
1816 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1818 // This will invoke the dialog default action, such
1819 // as the clicking the default button.
1821 wxWindow
*top_frame
= m_parent
;
1822 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1823 top_frame
= top_frame
->GetParent();
1825 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1827 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1829 if (window
->default_widget
)
1831 gtk_widget_activate (window
->default_widget
);
1840 GtkWidget
* wxTextCtrl::GetConnectWidget()
1842 return GTK_WIDGET(m_text
);
1845 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1847 if (m_windowStyle
& wxTE_MULTILINE
)
1850 return window
== gtk_text_view_get_window( GTK_TEXT_VIEW( m_text
), GTK_TEXT_WINDOW_TEXT
); // pure guesswork
1852 return (window
== GTK_TEXT(m_text
)->text_area
);
1857 return (window
== GTK_ENTRY(m_text
)->text_area
);
1861 // the font will change for subsequent text insertiongs
1862 bool wxTextCtrl::SetFont( const wxFont
&font
)
1864 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1866 if ( !wxTextCtrlBase::SetFont(font
) )
1868 // font didn't change, nothing to do
1872 if ( m_windowStyle
& wxTE_MULTILINE
)
1874 SetUpdateFont(true);
1876 m_defaultStyle
.SetFont(font
);
1878 ChangeFontGlobally();
1884 void wxTextCtrl::ChangeFontGlobally()
1886 // this method is very inefficient and hence should be called as rarely as
1889 // TODO: it can be implemented much more efficiently for GTK2
1891 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1893 _T("shouldn't be called for single line controls") );
1895 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
),
1896 _T("shouldn't be called for single line controls") );
1899 wxString value
= GetValue();
1900 if ( !value
.empty() )
1902 SetUpdateFont(false);
1911 void wxTextCtrl::UpdateFontIfNeeded()
1914 ChangeFontGlobally();
1919 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1921 if ( !wxControl::SetForegroundColour(colour
) )
1924 // update default fg colour too
1925 m_defaultStyle
.SetTextColour(colour
);
1930 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1932 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1934 if ( !wxControl::SetBackgroundColour( colour
) )
1938 if (!m_widget
->window
)
1942 if (!m_backgroundColour
.Ok())
1945 if (m_windowStyle
& wxTE_MULTILINE
)
1948 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1951 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1952 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1953 gdk_window_clear( window
);
1957 // change active background color too
1958 m_defaultStyle
.SetBackgroundColour( colour
);
1963 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1965 if ( m_windowStyle
& wxTE_MULTILINE
)
1967 if ( style
.IsDefault() )
1974 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1976 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1977 _T("invalid range in wxTextCtrl::SetStyle") );
1979 GtkTextIter starti
, endi
;
1980 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1981 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1983 // use the attributes from style which are set in it and fall back
1984 // first to the default style and then to the text control default
1985 // colours for the others
1986 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1988 wxGtkTextApplyTagsFromAttr( m_buffer
, attr
, &starti
, &endi
);
1990 // VERY dirty way to do that - removes the required text and re-adds it
1991 // with styling (FIXME)
1993 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1995 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1996 _T("invalid range in wxTextCtrl::SetStyle") );
1998 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1999 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
2000 wxString
tmp(text
,*wxConvCurrent
);
2003 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
2004 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
2007 wxWX2MBbuf buf
= tmp
.mbc_str();
2008 const char *txt
= buf
;
2009 size_t txtlen
= strlen(buf
);
2011 const char *txt
= tmp
;
2012 size_t txtlen
= tmp
.length();
2015 // use the attributes from style which are set in it and fall back
2016 // first to the default style and then to the text control default
2017 // colours for the others
2018 wxGtkTextInsert(m_text
,
2019 wxTextAttr::Combine(style
, m_defaultStyle
, this),
2023 /* does not seem to help under GTK+ 1.2 !!!
2024 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
2025 SetInsertionPoint( old_pos
);
2032 // cannot do this for GTK+'s Entry widget
2036 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
2038 gtk_widget_modify_style(m_text
, style
);
2041 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2046 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2051 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2056 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2061 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2066 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2068 event
.Enable( CanCut() );
2071 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2073 event
.Enable( CanCopy() );
2076 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2078 event
.Enable( CanPaste() );
2081 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2083 event
.Enable( CanUndo() );
2086 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2088 event
.Enable( CanRedo() );
2091 void wxTextCtrl::OnInternalIdle()
2093 wxCursor cursor
= m_cursor
;
2094 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
2099 GdkWindow
*window
= (GdkWindow
*) NULL
;
2100 if (HasFlag(wxTE_MULTILINE
))
2101 window
= GTK_TEXT(m_text
)->text_area
;
2103 window
= GTK_ENTRY(m_text
)->text_area
;
2106 gdk_window_set_cursor( window
, cursor
.GetCursor() );
2108 if (!g_globalCursor
.Ok())
2109 cursor
= *wxSTANDARD_CURSOR
;
2111 window
= m_widget
->window
;
2112 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
2113 gdk_window_set_cursor( window
, cursor
.GetCursor() );
2117 if (g_delayedFocus
== this)
2119 if (GTK_WIDGET_REALIZED(m_widget
))
2121 gtk_widget_grab_focus( m_widget
);
2122 g_delayedFocus
= NULL
;
2126 if (wxUpdateUIEvent::CanUpdate(this))
2127 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
2130 wxSize
wxTextCtrl::DoGetBestSize() const
2132 // FIXME should be different for multi-line controls...
2133 wxSize
ret( wxControl::DoGetBestSize() );
2134 wxSize
best(80, ret
.y
);
2135 CacheBestSize(best
);
2139 // ----------------------------------------------------------------------------
2141 // ----------------------------------------------------------------------------
2143 void wxTextCtrl::Freeze()
2145 if ( HasFlag(wxTE_MULTILINE
) )
2148 if ( !m_frozenness
++ )
2150 // freeze textview updates and remove buffer
2151 g_signal_connect( G_OBJECT(m_text
), "expose_event",
2152 GTK_SIGNAL_FUNC(gtk_text_exposed_callback
), (gpointer
)this);
2153 g_signal_connect( G_OBJECT(m_widget
), "expose_event",
2154 GTK_SIGNAL_FUNC(gtk_text_exposed_callback
), (gpointer
)this);
2155 gtk_widget_set_sensitive(m_widget
, false);
2156 g_object_ref(m_buffer
);
2157 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), gtk_text_buffer_new(NULL
));
2160 gtk_text_freeze(GTK_TEXT(m_text
));
2165 void wxTextCtrl::Thaw()
2167 if ( HasFlag(wxTE_MULTILINE
) )
2170 wxASSERT_MSG( m_frozenness
> 0, _T("Thaw() without matching Freeze()") );
2172 if ( !--m_frozenness
)
2174 // Reattach buffer and thaw textview updates
2175 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
2176 g_object_unref(m_buffer
);
2177 gtk_widget_set_sensitive(m_widget
, true);
2178 g_signal_handlers_disconnect_by_func(m_widget
, (gpointer
)gtk_text_exposed_callback
, this);
2179 g_signal_handlers_disconnect_by_func(m_text
, (gpointer
)gtk_text_exposed_callback
, this);
2182 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
2184 gtk_text_thaw(GTK_TEXT(m_text
));
2189 // ----------------------------------------------------------------------------
2190 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
2191 // ----------------------------------------------------------------------------
2195 // FIXME: when dragging on a link the sample gets an "Unknown event".
2196 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
2197 // a buggy sample, or something else
2198 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
2201 if(!(m_windowStyle
& wxTE_AUTO_URL
))
2205 GtkTextIter start
, end
;
2206 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
2209 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
2210 event
.GetX(), event
.GetY(), &x
, &y
);
2212 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
2213 if (!gtk_text_iter_has_tag(&end
, tag
))
2215 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
2216 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
2220 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
2221 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
2224 if(!gtk_text_iter_begins_tag(&start
, tag
))
2225 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
2226 if(!gtk_text_iter_ends_tag(&end
, tag
))
2227 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
2229 // Native context menu is probably not desired on an URL.
2230 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
2231 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
2234 wxTextUrlEvent
url_event(m_windowId
, event
,
2235 gtk_text_iter_get_offset(&start
),
2236 gtk_text_iter_get_offset(&end
));
2238 InitCommandEvent(url_event
);
2239 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2240 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
2241 GetEventHandler()->ProcessEvent(url_event
);
2245 // ----------------------------------------------------------------------------
2247 // ----------------------------------------------------------------------------
2249 GtkAdjustment
*wxTextCtrl::GetVAdj() const
2251 if ( !IsMultiLine() )
2255 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget
));
2257 return GTK_TEXT(m_text
)->vadj
;
2261 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
2263 float value
= adj
->value
+ diff
;
2268 float upper
= adj
->upper
- adj
->page_size
;
2269 if ( value
> upper
)
2272 // did we noticeably change the scroll position?
2273 if ( fabs(adj
->value
- value
) < 0.2 )
2275 // well, this is what Robert does in wxScrollBar, so it must be good...
2282 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj
));
2284 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
2290 bool wxTextCtrl::ScrollLines(int lines
)
2292 GtkAdjustment
*adj
= GetVAdj();
2297 int diff
= (int)ceil(lines
*adj
->step_increment
);
2299 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
2300 int diff
= 10*lines
;
2303 return DoScroll(adj
, diff
);
2306 bool wxTextCtrl::ScrollPages(int pages
)
2308 GtkAdjustment
*adj
= GetVAdj();
2312 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
2318 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2320 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);