1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textctrl.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/textctrl.h"
20 #include "wx/settings.h"
24 #include "wx/scopeguard.h"
25 #include "wx/strconv.h"
26 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
28 #include <sys/types.h>
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/private/gtk2-compat.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
41 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
43 GtkTextIter
* WXUNUSED(start
),
44 GtkTextIter
* WXUNUSED(end
),
48 g_object_get (tag
, "name", &name
, NULL
);
50 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
51 // anonymous tag or not starting with prefix - don't remove
52 g_signal_stop_emission_by_name (buffer
, "remove_tag");
58 // remove all tags starting with the given prefix from the start..end range
60 wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer
*text_buffer
,
65 gulong remove_handler_id
= g_signal_connect
69 G_CALLBACK(wxGtkOnRemoveTag
),
72 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
73 g_signal_handler_disconnect(text_buffer
, remove_handler_id
);
76 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
77 GtkTextBuffer
*text_buffer
,
78 const wxTextAttr
& attr
,
82 static gchar buf
[1024];
87 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFONT", start
, end
);
89 wxFont
font(attr
.GetFont());
91 PangoFontDescription
*font_description
= font
.GetNativeFontInfo()->description
;
92 wxGtkString
font_string(pango_font_description_to_string(font_description
));
93 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
94 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
97 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
98 "font-desc", font_description
,
100 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
102 if (font
.GetUnderlined())
104 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
105 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
108 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
109 "underline-set", TRUE
,
110 "underline", PANGO_UNDERLINE_SINGLE
,
112 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
114 if ( font
.GetStrikethrough() )
116 g_snprintf(buf
, sizeof(buf
), "WXFONTSTRIKETHROUGH");
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 "strikethrough-set", TRUE
,
122 "strikethrough", TRUE
,
124 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
128 if (attr
.HasTextColour())
130 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFORECOLOR", start
, end
);
132 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
133 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
134 colFg
->red
, colFg
->green
, colFg
->blue
);
135 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
138 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
139 "foreground-gdk", colFg
, NULL
);
140 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
143 if (attr
.HasBackgroundColour())
145 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXBACKCOLOR", start
, end
);
147 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
148 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
149 colBg
->red
, colBg
->green
, colBg
->blue
);
150 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
153 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
154 "background-gdk", colBg
, NULL
);
155 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
158 if (attr
.HasAlignment())
160 GtkTextIter para_start
, para_end
= *end
;
161 gtk_text_buffer_get_iter_at_line( text_buffer
,
163 gtk_text_iter_get_line(start
) );
164 gtk_text_iter_forward_line(¶_end
);
166 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXALIGNMENT", ¶_start
, ¶_end
);
168 GtkJustification align
;
169 switch (attr
.GetAlignment())
171 case wxTEXT_ALIGNMENT_RIGHT
:
172 align
= GTK_JUSTIFY_RIGHT
;
174 case wxTEXT_ALIGNMENT_CENTER
:
175 align
= GTK_JUSTIFY_CENTER
;
177 case wxTEXT_ALIGNMENT_JUSTIFIED
:
179 align
= GTK_JUSTIFY_FILL
;
181 #elif GTK_CHECK_VERSION(2,11,0)
182 // gtk+ doesn't support justify before gtk+-2.11.0 with pango-1.17 being available
183 // (but if new enough pango isn't available it's a mere gtk warning)
184 if (!gtk_check_version(2,11,0))
186 align
= GTK_JUSTIFY_FILL
;
192 align
= GTK_JUSTIFY_LEFT
;
196 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
197 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
200 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
201 "justification", align
, NULL
);
202 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
205 if (attr
.HasLeftIndent())
207 // Indentation attribute
209 // Clear old indentation tags
210 GtkTextIter para_start
, para_end
= *end
;
211 gtk_text_buffer_get_iter_at_line( text_buffer
,
213 gtk_text_iter_get_line(start
) );
214 gtk_text_iter_forward_line(¶_end
);
216 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", ¶_start
, ¶_end
);
218 // Convert indent from 1/10th of a mm into pixels
220 (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
221 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
223 const int indent
= (int)(factor
* attr
.GetLeftIndent());
224 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
232 gsubindent
= -subIndent
;
236 gindent
= -subIndent
;
240 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
241 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
244 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
245 "left-margin", gindent
, "indent", gsubindent
, NULL
);
246 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
254 GtkTextIter para_start
, para_end
= *end
;
255 gtk_text_buffer_get_iter_at_line( text_buffer
,
257 gtk_text_iter_get_line(start
) );
258 gtk_text_iter_forward_line(¶_end
);
260 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", ¶_start
, ¶_end
);
262 const wxArrayInt
& tabs
= attr
.GetTabs();
264 wxString tagname
= wxT("WXTABS");
265 g_snprintf(buf
, sizeof(buf
), "WXTABS");
266 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
267 tagname
+= wxString::Format(wxT(" %d"), tabs
[i
]);
269 const wxWX2MBbuf buftag
= tagname
.utf8_str();
271 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
275 // Factor to convert from 1/10th of a mm into pixels
277 (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
278 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
280 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
281 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
282 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
283 tag
= gtk_text_buffer_create_tag( text_buffer
, buftag
,
284 "tabs", tabArray
, NULL
);
285 pango_tab_array_free(tabArray
);
287 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
291 static void wxGtkTextInsert(GtkWidget
*text
,
292 GtkTextBuffer
*text_buffer
,
293 const wxTextAttr
& attr
,
294 const wxCharBuffer
& buffer
)
298 GtkTextIter iter
, start
;
300 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
301 gtk_text_buffer_get_insert (text_buffer
) );
302 start_offset
= gtk_text_iter_get_offset (&iter
);
303 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
305 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
307 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
310 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
314 au_apply_tag_callback(GtkTextBuffer
*buffer
,
316 GtkTextIter
* WXUNUSED(start
),
317 GtkTextIter
* WXUNUSED(end
),
318 gpointer
WXUNUSED(textctrl
))
320 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
321 g_signal_stop_emission_by_name (buffer
, "apply_tag");
325 //-----------------------------------------------------------------------------
326 // GtkTextCharPredicates for gtk_text_iter_*_find_char
327 //-----------------------------------------------------------------------------
331 pred_whitespace(gunichar ch
, gpointer
WXUNUSED(user_data
))
333 return g_unichar_isspace(ch
);
339 pred_non_whitespace (gunichar ch
, gpointer
WXUNUSED(user_data
))
341 return !g_unichar_isspace(ch
);
347 pred_nonpunct (gunichar ch
, gpointer
WXUNUSED(user_data
))
349 return !g_unichar_ispunct(ch
);
355 pred_nonpunct_or_slash (gunichar ch
, gpointer
WXUNUSED(user_data
))
357 return !g_unichar_ispunct(ch
) || ch
== '/';
361 //-----------------------------------------------------------------------------
362 // Check for links between s and e and correct tags as necessary
363 //-----------------------------------------------------------------------------
365 // This function should be made match better while being efficient at one point.
366 // Most probably with a row of regular expressions.
369 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
371 static const char *const URIPrefixes
[] =
389 GtkTextIter start
= *s
, end
= *e
;
390 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
392 // Get our special link tag
393 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
395 // Get rid of punctuation from beginning and end.
396 // Might want to move this to au_check_range if an improved link checking doesn't
397 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
398 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
399 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
401 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
402 gtk_text_iter_forward_char(&end
);
404 wxGtkString
text(gtk_text_iter_get_text( &start
, &end
));
405 size_t len
= strlen(text
), prefix_len
;
408 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
410 prefix_len
= strlen(URIPrefixes
[n
]);
411 if((len
> prefix_len
) && !wxStrnicmp(text
, URIPrefixes
[n
], prefix_len
))
415 if(n
< WXSIZEOF(URIPrefixes
))
417 gulong signal_id
= g_signal_handler_find (buffer
,
418 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
420 (gpointer
)au_apply_tag_callback
, NULL
);
422 g_signal_handler_block (buffer
, signal_id
);
423 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
424 g_signal_handler_unblock (buffer
, signal_id
);
431 au_check_range(GtkTextIter
*s
,
432 GtkTextIter
*range_end
)
434 GtkTextIter range_start
= *s
;
435 GtkTextIter word_end
;
436 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
437 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
439 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
441 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
442 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
444 while(!gtk_text_iter_equal(&range_start
, range_end
))
446 word_end
= range_start
;
447 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
449 // Now we should have a word delimited by range_start and word_end, correct link tags
450 au_check_word(&range_start
, &word_end
);
452 range_start
= word_end
;
453 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
458 //-----------------------------------------------------------------------------
459 // "insert-text" for GtkTextBuffer
460 //-----------------------------------------------------------------------------
464 // Normal version used for detecting IME input and generating appropriate
467 wx_insert_text_callback(GtkTextBuffer
* buffer
,
468 GtkTextIter
* WXUNUSED(end
),
473 if ( win
->GTKOnInsertText(text
) )
475 // If we already handled the new text insertion, don't do it again.
476 g_signal_stop_emission_by_name (buffer
, "insert_text");
481 // And an "after" version used for detecting URLs in the text.
483 au_insert_text_callback(GtkTextBuffer
* WXUNUSED(buffer
),
489 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
492 GtkTextIter start
= *end
;
493 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
495 GtkTextIter line_start
= start
;
496 GtkTextIter line_end
= *end
;
497 GtkTextIter words_start
= start
;
498 GtkTextIter words_end
= *end
;
500 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
501 gtk_text_iter_forward_to_line_end(&line_end
);
502 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
503 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
505 au_check_range(&words_start
, &words_end
);
509 //-----------------------------------------------------------------------------
510 // "delete-range" for GtkTextBuffer
511 //-----------------------------------------------------------------------------
515 au_delete_range_callback(GtkTextBuffer
* WXUNUSED(buffer
),
520 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
523 GtkTextIter line_start
= *start
, line_end
= *end
;
525 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
526 gtk_text_iter_forward_to_line_end(&line_end
);
527 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
528 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
530 au_check_range(start
, end
);
534 //-----------------------------------------------------------------------------
535 // "populate_popup" from text control and "unmap" from its poup menu
536 //-----------------------------------------------------------------------------
540 gtk_textctrl_popup_unmap( GtkMenu
*WXUNUSED(menu
), wxTextCtrl
* win
)
542 win
->GTKEnableFocusOutEvent();
548 gtk_textctrl_populate_popup( GtkEntry
*WXUNUSED(entry
), GtkMenu
*menu
, wxTextCtrl
*win
)
550 win
->GTKDisableFocusOutEvent();
552 g_signal_connect (menu
, "unmap", G_CALLBACK (gtk_textctrl_popup_unmap
), win
);
556 //-----------------------------------------------------------------------------
558 //-----------------------------------------------------------------------------
562 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
564 if ( win
->IgnoreTextUpdate() )
567 if ( win
->MarkDirtyOnChange() )
570 win
->SendTextUpdatedEvent();
574 //-----------------------------------------------------------------------------
576 //-----------------------------------------------------------------------------
579 static void mark_set(GtkTextBuffer
*, GtkTextIter
*, GtkTextMark
* mark
, GSList
** markList
)
581 if (gtk_text_mark_get_name(mark
) == NULL
)
582 *markList
= g_slist_prepend(*markList
, mark
);
586 //-----------------------------------------------------------------------------
588 //-----------------------------------------------------------------------------
590 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
591 EVT_CHAR(wxTextCtrl::OnChar
)
593 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
594 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
595 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
596 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
597 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
599 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
600 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
601 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
602 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
603 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
605 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
606 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
607 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
608 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
609 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
610 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
611 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
612 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
613 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
616 void wxTextCtrl::Init()
621 m_countUpdatesToIgnore
= 0;
623 SetUpdateFont(false);
627 m_showPositionOnThaw
= NULL
;
628 m_anonymousMarkList
= NULL
;
631 wxTextCtrl::~wxTextCtrl()
634 GTKDisconnect(m_text
);
636 GTKDisconnect(m_buffer
);
638 // this is also done by wxWindowGTK dtor, but has to be done here so our
639 // DoThaw() override is called
643 if (m_anonymousMarkList
)
644 g_slist_free(m_anonymousMarkList
);
647 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
649 const wxString
&value
,
653 const wxValidator
& validator
,
654 const wxString
&name
)
658 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
661 bool wxTextCtrl::Create( wxWindow
*parent
,
663 const wxString
&value
,
667 const wxValidator
& validator
,
668 const wxString
&name
)
670 if (!PreCreation( parent
, pos
, size
) ||
671 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
673 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
677 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
681 m_buffer
= gtk_text_buffer_new(NULL
);
682 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
684 m_text
= gtk_text_view_new_with_buffer(m_buffer
);
685 // gtk_text_view_set_buffer adds its own reference
686 g_object_unref(m_buffer
);
687 g_signal_handler_disconnect(m_buffer
, sig_id
);
689 // create "ShowPosition" marker
691 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
692 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
694 // create scrolled window
695 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
696 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
697 GTK_POLICY_AUTOMATIC
,
698 style
& wxTE_NO_VSCROLL
700 : GTK_POLICY_AUTOMATIC
);
701 // for ScrollLines/Pages
702 m_scrollBar
[1] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(m_widget
)));
704 // Insert view into scrolled window
705 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
709 GTKScrolledWindowSetBorder(m_widget
, style
);
711 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
713 gtk_widget_set_can_focus(m_widget
, FALSE
);
717 // a single-line text control: no need for scrollbars
719 m_text
= gtk_entry_new();
720 // work around probable bug in GTK+ 2.18 when calling WriteText on a
721 // new, empty control, see http://trac.wxwidgets.org/ticket/11409
722 gtk_entry_get_text((GtkEntry
*)m_text
);
724 if (style
& wxNO_BORDER
)
725 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
728 g_object_ref(m_widget
);
730 m_parent
->DoAddChild( this );
732 m_focusWidget
= m_text
;
738 gtk_widget_show(m_text
);
741 // We want to be notified about text changes.
744 g_signal_connect (m_buffer
, "changed",
745 G_CALLBACK (gtk_text_changed_callback
), this);
749 g_signal_connect (m_text
, "changed",
750 G_CALLBACK (gtk_text_changed_callback
), this);
753 // Catch to disable focus out handling
754 g_signal_connect (m_text
, "populate_popup",
755 G_CALLBACK (gtk_textctrl_populate_popup
),
763 if (style
& wxTE_PASSWORD
)
766 if (style
& wxTE_READONLY
)
769 // left justification (alignment) is the default anyhow
770 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
771 GTKSetJustification();
775 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
776 if (style
& wxTE_AUTO_URL
)
778 GtkTextIter start
, end
;
780 // We create our wxUrl tag here for slight efficiency gain - we
781 // don't have to check for the tag existence in callbacks,
782 // hereby it's guaranteed to exist.
783 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
784 "foreground", "blue",
785 "underline", PANGO_UNDERLINE_SINGLE
,
788 // Check for URLs after each text change
789 g_signal_connect_after (m_buffer
, "insert_text",
790 G_CALLBACK (au_insert_text_callback
), this);
791 g_signal_connect_after (m_buffer
, "delete_range",
792 G_CALLBACK (au_delete_range_callback
), this);
794 // Block all wxUrl tag applying unless we do it ourselves, in which case we
795 // block this callback temporarily. This takes care of gtk+ internal
796 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
797 // which is undesired because only a part of the URL might be copied.
798 // The insert-text signal emitted inside it will take care of newly formed
799 // or wholly copied URLs.
800 g_signal_connect (m_buffer
, "apply_tag",
801 G_CALLBACK (au_apply_tag_callback
), NULL
);
803 // Check for URLs in the initial string passed to Create
804 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
805 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
806 au_check_range(&start
, &end
);
809 // Also connect a normal (not "after") signal handler for checking for
810 // the IME-generated input.
811 g_signal_connect(m_buffer
, "insert_text",
812 G_CALLBACK(wx_insert_text_callback
), this);
816 // do the right thing with Enter presses depending on whether we have
817 // wxTE_PROCESS_ENTER or not
818 GTKSetActivatesDefault();
820 GTKConnectInsertTextSignal(GTK_ENTRY(m_text
));
824 GTKConnectClipboardSignals(m_text
);
826 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
831 GtkEditable
*wxTextCtrl::GetEditable() const
833 wxCHECK_MSG( IsSingleLine(), NULL
, "shouldn't be called for multiline" );
835 return GTK_EDITABLE(m_text
);
838 GtkEntry
*wxTextCtrl::GetEntry() const
840 return GTK_ENTRY(m_text
);
843 int wxTextCtrl::GTKIMFilterKeypress(GdkEventKey
* event
) const
845 #if GTK_CHECK_VERSION(2, 22, 0)
846 if ( gtk_check_version(2, 12, 0) == 0 )
848 if ( IsSingleLine() )
850 return wxTextEntry::GTKIMFilterKeypress(event
);
854 return gtk_text_view_im_context_filter_keypress(
855 GTK_TEXT_VIEW(m_text
),
867 // ----------------------------------------------------------------------------
869 // ----------------------------------------------------------------------------
871 void wxTextCtrl::GTKSetEditable()
873 gboolean editable
= !HasFlag(wxTE_READONLY
);
874 if ( IsSingleLine() )
875 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
877 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
880 void wxTextCtrl::GTKSetVisibility()
882 wxCHECK_RET( IsSingleLine(),
883 "wxTE_PASSWORD is for single line text controls only" );
885 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
888 void wxTextCtrl::GTKSetActivatesDefault()
890 wxCHECK_RET( IsSingleLine(),
891 "wxTE_PROCESS_ENTER is for single line text controls only" );
893 gtk_entry_set_activates_default(GTK_ENTRY(m_text
),
894 !HasFlag(wxTE_PROCESS_ENTER
));
897 void wxTextCtrl::GTKSetWrapMode()
899 // no wrapping in single line controls
900 if ( !IsMultiLine() )
903 // translate wx wrapping style to GTK+
905 if ( HasFlag( wxTE_DONTWRAP
) )
906 wrap
= GTK_WRAP_NONE
;
907 else if ( HasFlag( wxTE_CHARWRAP
) )
908 wrap
= GTK_WRAP_CHAR
;
909 else if ( HasFlag( wxTE_WORDWRAP
) )
910 wrap
= GTK_WRAP_WORD
;
911 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
912 wrap
= GTK_WRAP_WORD_CHAR
;
914 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
917 void wxTextCtrl::GTKSetJustification()
921 GtkJustification just
;
922 if ( HasFlag(wxTE_RIGHT
) )
923 just
= GTK_JUSTIFY_RIGHT
;
924 else if ( HasFlag(wxTE_CENTRE
) )
925 just
= GTK_JUSTIFY_CENTER
;
926 else // wxTE_LEFT == 0
927 just
= GTK_JUSTIFY_LEFT
;
929 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
934 if ( HasFlag(wxTE_RIGHT
) )
936 else if ( HasFlag(wxTE_CENTRE
) )
941 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
945 void wxTextCtrl::SetWindowStyleFlag(long style
)
947 long styleOld
= GetWindowStyleFlag();
949 wxTextCtrlBase::SetWindowStyleFlag(style
);
951 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
954 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
957 if ( (style
& wxTE_PROCESS_ENTER
) != (styleOld
& wxTE_PROCESS_ENTER
) )
958 GTKSetActivatesDefault();
960 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
961 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
964 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
965 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
966 GTKSetJustification();
969 // ----------------------------------------------------------------------------
971 // ----------------------------------------------------------------------------
973 wxString
wxTextCtrl::GetValue() const
975 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
980 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
982 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
983 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
985 return wxGTK_CONV_BACK(text
);
989 return wxTextEntry::GetValue();
993 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
995 // GTK+ uses UTF-8 internally, we need to convert to it but from which
998 // first check the default text style (we intentionally don't check the
999 // style for the current position as it doesn't make sense for SetValue())
1000 const wxTextAttr
& style
= GetDefaultStyle();
1001 wxFontEncoding enc
= style
.HasFontEncoding() ? style
.GetFontEncoding()
1002 : wxFONTENCODING_SYSTEM
;
1004 // fall back to the controls font if no style
1005 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
1006 enc
= GetFont().GetEncoding();
1011 bool wxTextCtrl::IsEmpty() const
1013 if ( IsMultiLine() )
1014 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
1016 return wxTextEntry::IsEmpty();
1019 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
1021 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1025 if ( !IsMultiLine() )
1027 wxTextEntry::DoSetValue(value
, flags
);
1031 if (value
.IsEmpty())
1033 if ( !(flags
& SetValue_SendEvent
) )
1034 EnableTextChangedEvents(false);
1036 gtk_text_buffer_set_text( m_buffer
, "", 0 );
1038 if ( !(flags
& SetValue_SendEvent
) )
1039 EnableTextChangedEvents(true);
1045 const wxCharBuffer
buffer(value
.utf8_str());
1047 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1048 ? m_defaultStyle
.GetFont().GetEncoding()
1049 : wxFONTENCODING_SYSTEM
;
1050 if ( enc
== wxFONTENCODING_SYSTEM
)
1051 enc
= GetTextEncoding();
1053 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1056 // see comment in WriteText() as to why we must warn the user about
1058 wxLogWarning(_("Failed to set text in the text control."));
1063 if ( !(flags
& SetValue_SendEvent
) )
1065 EnableTextChangedEvents(false);
1068 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1070 if ( !m_defaultStyle
.IsDefault() )
1072 GtkTextIter start
, end
;
1073 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1074 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1078 if ( !(flags
& SetValue_SendEvent
) )
1080 EnableTextChangedEvents(true);
1084 void wxTextCtrl::WriteText( const wxString
&text
)
1086 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1088 // we're changing the text programmatically
1089 DontMarkDirtyOnNextChange();
1091 if ( !IsMultiLine() )
1093 wxTextEntry::WriteText(text
);
1098 const wxCharBuffer
buffer(text
.utf8_str());
1100 // check if we have a specific style for the current position
1101 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1103 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFontEncoding() )
1105 enc
= style
.GetFontEncoding();
1108 if ( enc
== wxFONTENCODING_SYSTEM
)
1109 enc
= GetTextEncoding();
1111 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1114 // we must log an error here as losing the text like this can be a
1115 // serious problem (e.g. imagine the document edited by user being
1116 // empty instead of containing the correct text)
1117 wxLogWarning(_("Failed to insert text in the control."));
1122 // First remove the selection if there is one
1123 gtk_text_buffer_delete_selection(m_buffer
, false, true);
1126 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1128 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1129 // won't work when frozen, text view is not using m_buffer then
1132 GtkAdjustment
* adj
= gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget
));
1133 const double value
= gtk_adjustment_get_value(adj
);
1134 const double upper
= gtk_adjustment_get_upper(adj
);
1135 const double page_size
= gtk_adjustment_get_page_size(adj
);
1136 if (wxIsSameDouble(value
, upper
- page_size
))
1138 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(m_text
),
1139 gtk_text_buffer_get_insert(m_buffer
), 0, false, 0, 1);
1144 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1147 if ( IsMultiLine() )
1150 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1152 GtkTextIter end
= line
;
1153 // avoid skipping to the next line end if this one is empty
1154 if ( !gtk_text_iter_ends_line(&line
) )
1155 gtk_text_iter_forward_to_line_end(&end
);
1157 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1158 result
= wxGTK_CONV_BACK(text
);
1163 result
= GetValue();
1168 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1170 /* If you implement this, don't forget to update the documentation!
1171 * (file docs/latex/wx/text.tex) */
1172 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1175 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1177 if ( IsMultiLine() )
1181 if (pos
> GetLastPosition())
1184 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1187 *y
= gtk_text_iter_get_line(&iter
);
1189 *x
= gtk_text_iter_get_line_offset(&iter
);
1191 else // single line control
1193 if (pos
<= gtk_entry_get_text_length(GTK_ENTRY(m_text
)))
1202 // index out of bounds
1210 long wxTextCtrl::XYToPosition(long x
, long y
) const
1212 if ( IsSingleLine() )
1216 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1219 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1220 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1223 return gtk_text_iter_get_offset(&iter
) + x
;
1226 int wxTextCtrl::GetLineLength(long lineNo
) const
1228 if ( IsMultiLine() )
1230 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1231 if (lineNo
> last_line
)
1235 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1236 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1237 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1241 wxString str
= GetLineText (lineNo
);
1242 return (int) str
.length();
1246 wxPoint
wxTextCtrl::DoPositionToCoords(long pos
) const
1248 if ( !IsMultiLine() )
1250 // Single line text entry (GtkTextEntry) doesn't have support for
1251 // getting the coordinates for the given offset. Perhaps we could
1252 // find them ourselves by using GetTextExtent() but for now just leave
1253 // it unimplemented, this function is more useful for multiline
1255 return wxDefaultPosition
;
1258 // Window coordinates for the given position is calculated by getting
1259 // the buffer coordinates and converting them to window coordinates.
1260 GtkTextView
*textview
= GTK_TEXT_VIEW(m_text
);
1263 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1265 GdkRectangle bufferCoords
;
1266 gtk_text_view_get_iter_location(textview
, &iter
, &bufferCoords
);
1270 gtk_text_view_buffer_to_window_coords(textview
, GTK_TEXT_WINDOW_WIDGET
,
1271 bufferCoords
.x
, bufferCoords
.y
,
1272 &winCoordX
, &winCoordY
);
1274 return wxPoint(winCoordX
, winCoordY
);
1277 int wxTextCtrl::GetNumberOfLines() const
1279 if ( IsMultiLine() )
1281 return gtk_text_buffer_get_line_count( m_buffer
);
1289 void wxTextCtrl::SetInsertionPoint( long pos
)
1291 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1293 if ( IsMultiLine() )
1296 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1297 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1298 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1300 // defer until Thaw, text view is not using m_buffer now
1301 m_showPositionOnThaw
= mark
;
1303 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1307 wxTextEntry::SetInsertionPoint(pos
);
1311 void wxTextCtrl::SetEditable( bool editable
)
1313 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1315 if ( IsMultiLine() )
1317 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1321 wxTextEntry::SetEditable(editable
);
1325 bool wxTextCtrl::Enable( bool enable
)
1327 if (!wxWindowBase::Enable(enable
))
1333 gtk_widget_set_sensitive( m_text
, enable
);
1334 SetCursor(enable
? wxCursor(wxCURSOR_IBEAM
) : wxCursor());
1339 void wxTextCtrl::MarkDirty()
1344 void wxTextCtrl::DiscardEdits()
1349 // ----------------------------------------------------------------------------
1351 // ----------------------------------------------------------------------------
1353 void wxTextCtrl::EnableTextChangedEvents(bool enable
)
1357 g_signal_handlers_unblock_by_func(GetTextObject(),
1358 (gpointer
)gtk_text_changed_callback
, this);
1360 else // disable events
1362 g_signal_handlers_block_by_func(GetTextObject(),
1363 (gpointer
)gtk_text_changed_callback
, this);
1367 bool wxTextCtrl::IgnoreTextUpdate()
1369 if ( m_countUpdatesToIgnore
> 0 )
1371 m_countUpdatesToIgnore
--;
1379 bool wxTextCtrl::MarkDirtyOnChange()
1381 if ( m_dontMarkDirty
)
1383 m_dontMarkDirty
= false;
1391 void wxTextCtrl::SetSelection( long from
, long to
)
1393 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1395 if ( IsMultiLine() )
1397 if (from
== -1 && to
== -1)
1400 to
= GetValue().length();
1403 GtkTextIter fromi
, toi
;
1404 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1405 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1407 gtk_text_buffer_select_range( m_buffer
, &fromi
, &toi
);
1411 wxTextEntry::SetSelection(from
, to
);
1415 void wxTextCtrl::ShowPosition( long pos
)
1420 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1421 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1422 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1424 // defer until Thaw, text view is not using m_buffer now
1425 m_showPositionOnThaw
= mark
;
1427 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1431 wxTextCtrlHitTestResult
1432 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1434 if ( !IsMultiLine() )
1437 return wxTE_HT_UNKNOWN
;
1441 gtk_text_view_window_to_buffer_coords
1443 GTK_TEXT_VIEW(m_text
),
1444 GTK_TEXT_WINDOW_TEXT
,
1450 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1452 *pos
= gtk_text_iter_get_offset(&iter
);
1454 return wxTE_HT_ON_TEXT
;
1457 long wxTextCtrl::GetInsertionPoint() const
1459 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1461 if ( IsMultiLine() )
1463 // There is no direct accessor for the cursor, but
1464 // internally, the cursor is the "mark" called
1465 // "insert" in the text view's btree structure.
1467 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1469 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1471 return gtk_text_iter_get_offset( &cursor
);
1475 return wxTextEntry::GetInsertionPoint();
1479 wxTextPos
wxTextCtrl::GetLastPosition() const
1481 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1485 if ( IsMultiLine() )
1488 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1490 pos
= gtk_text_iter_get_offset( &end
);
1494 pos
= wxTextEntry::GetLastPosition();
1500 void wxTextCtrl::Remove( long from
, long to
)
1502 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1504 if ( IsMultiLine() )
1506 GtkTextIter fromi
, toi
;
1507 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1508 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1510 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1514 wxTextEntry::Remove(from
, to
);
1518 void wxTextCtrl::Cut()
1520 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1522 if ( IsMultiLine() )
1523 g_signal_emit_by_name (m_text
, "cut-clipboard");
1528 void wxTextCtrl::Copy()
1530 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1532 if ( IsMultiLine() )
1533 g_signal_emit_by_name (m_text
, "copy-clipboard");
1535 wxTextEntry::Copy();
1538 void wxTextCtrl::Paste()
1540 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1542 if ( IsMultiLine() )
1543 g_signal_emit_by_name (m_text
, "paste-clipboard");
1545 wxTextEntry::Paste();
1548 // If the return values from and to are the same, there is no
1550 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1552 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1554 if ( !IsMultiLine() )
1556 wxTextEntry::GetSelection(fromOut
, toOut
);
1562 GtkTextIter ifrom
, ito
;
1563 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1565 from
= gtk_text_iter_get_offset(&ifrom
);
1566 to
= gtk_text_iter_get_offset(&ito
);
1570 // exchange them to be compatible with wxMSW
1576 else // no selection
1579 to
= GetInsertionPoint();
1589 bool wxTextCtrl::IsEditable() const
1591 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1593 if ( IsMultiLine() )
1595 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
)) != 0;
1599 return wxTextEntry::IsEditable();
1603 bool wxTextCtrl::IsModified() const
1608 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1610 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1612 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1614 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1616 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
1617 event
.SetEventObject(this);
1618 event
.SetString(GetValue());
1619 if ( HandleWindowEvent(event
) )
1627 GtkWidget
* wxTextCtrl::GetConnectWidget()
1629 return GTK_WIDGET(m_text
);
1632 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1634 if ( IsMultiLine() )
1636 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1637 GTK_TEXT_WINDOW_TEXT
);
1642 // no access to internal GdkWindows
1645 return gtk_entry_get_text_window(GTK_ENTRY(m_text
));
1650 // the font will change for subsequent text insertiongs
1651 bool wxTextCtrl::SetFont( const wxFont
&font
)
1653 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1655 if ( !wxTextCtrlBase::SetFont(font
) )
1657 // font didn't change, nothing to do
1661 if ( IsMultiLine() )
1663 SetUpdateFont(true);
1665 m_defaultStyle
.SetFont(font
);
1667 ChangeFontGlobally();
1673 void wxTextCtrl::ChangeFontGlobally()
1675 // this method is very inefficient and hence should be called as rarely as
1678 // TODO: it can be implemented much more efficiently for GTK2
1679 wxASSERT_MSG( IsMultiLine(),
1680 wxT("shouldn't be called for single line controls") );
1682 wxString value
= GetValue();
1683 if ( !value
.empty() )
1685 SetUpdateFont(false);
1692 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1694 if ( !wxControl::SetForegroundColour(colour
) )
1697 // update default fg colour too
1698 m_defaultStyle
.SetTextColour(colour
);
1703 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1705 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1707 if ( !wxControl::SetBackgroundColour( colour
) )
1710 if (!m_backgroundColour
.IsOk())
1713 // change active background color too
1714 m_defaultStyle
.SetBackgroundColour( colour
);
1719 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1721 if ( IsMultiLine() )
1723 if ( style
.IsDefault() )
1729 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1731 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1732 wxT("invalid range in wxTextCtrl::SetStyle") );
1734 GtkTextIter starti
, endi
;
1735 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1736 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1738 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1742 //else: single line text controls don't support styles
1747 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
1749 if ( !IsMultiLine() )
1751 // no styles for GtkEntry
1755 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1757 wxCHECK_MSG( position
>= 0 && position
<= l
, false,
1758 wxT("invalid range in wxTextCtrl::GetStyle") );
1760 GtkTextIter positioni
;
1761 gtk_text_buffer_get_iter_at_offset(m_buffer
, &positioni
, position
);
1763 // Obtain a copy of the default attributes
1764 GtkTextAttributes
* const
1765 pattr
= gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text
));
1766 wxON_BLOCK_EXIT1(gtk_text_attributes_unref
, pattr
);
1768 // And query GTK for the attributes at the given position using it as base
1769 if ( !gtk_text_iter_get_attributes(&positioni
, pattr
) )
1771 style
= m_defaultStyle
;
1773 else // have custom attributes
1775 style
.SetBackgroundColour(pattr
->appearance
.bg_color
);
1776 style
.SetTextColour(pattr
->appearance
.fg_color
);
1779 pangoFontString(pango_font_description_to_string(pattr
->font
));
1782 if ( font
.SetNativeFontInfo(wxString(pangoFontString
)) )
1783 style
.SetFont(font
);
1785 // TODO: set alignment, tabs and indents
1791 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1793 GTKApplyStyle(m_text
, style
);
1796 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1801 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1806 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1811 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1816 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1821 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1823 event
.Enable( CanCut() );
1826 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1828 event
.Enable( CanCopy() );
1831 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1833 event
.Enable( CanPaste() );
1836 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1838 event
.Enable( CanUndo() );
1841 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1843 event
.Enable( CanRedo() );
1846 wxSize
wxTextCtrl::DoGetBestSize() const
1848 return DoGetSizeFromTextSize(80);
1851 wxSize
wxTextCtrl::DoGetSizeFromTextSize(int xlen
, int ylen
) const
1853 wxASSERT_MSG( m_widget
, wxS("GetSizeFromTextSize called before creation") );
1855 wxSize
tsize(xlen
, 0);
1856 int cHeight
= GetCharHeight();
1858 if ( IsSingleLine() )
1860 if ( HasFlag(wxBORDER_NONE
) )
1872 tsize
.y
= GTKGetPreferredSize(m_widget
).y
;
1873 // Add the margins we have previously set, but only the horizontal border
1874 // as vertical one has been taken account at GTKGetPreferredSize().
1875 // Also get other GTK+ margins.
1876 tsize
.IncBy( GTKGetEntryMargins(GetEntry()).x
, 0);
1883 // add space for vertical scrollbar
1884 if ( m_scrollBar
[1] && !(m_windowStyle
& wxTE_NO_VSCROLL
) )
1885 tsize
.IncBy(GTKGetPreferredSize(GTK_WIDGET(m_scrollBar
[1])).x
+ 3, 0);
1891 tsize
.y
= 1 + cHeight
* wxMax(wxMin(GetNumberOfLines(), 10), 2);
1892 // add space for horizontal scrollbar
1893 if ( m_scrollBar
[0] && (m_windowStyle
& wxHSCROLL
) )
1894 tsize
.IncBy(0, GTKGetPreferredSize(GTK_WIDGET(m_scrollBar
[0])).y
+ 3);
1897 if ( !HasFlag(wxBORDER_NONE
) )
1899 // hardcode borders, margins, etc
1904 // Perhaps the user wants something different from CharHeight, or ylen
1905 // is used as the height of a multiline text.
1907 tsize
.IncBy(0, ylen
- cHeight
);
1913 // ----------------------------------------------------------------------------
1915 // ----------------------------------------------------------------------------
1917 void wxTextCtrl::DoFreeze()
1919 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1921 GTKFreezeWidget(m_text
);
1923 if ( HasFlag(wxTE_MULTILINE
) )
1925 // removing buffer dramatically speeds up insertion:
1926 g_object_ref(m_buffer
);
1927 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1928 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1929 // gtk_text_view_set_buffer adds its own reference
1930 g_object_unref(buf_new
);
1931 // These marks should be deleted when the buffer is changed,
1932 // but they are not (in GTK+ up to at least 3.0.1).
1933 // Otherwise these anonymous marks start to build up in the buffer,
1934 // and Freeze takes longer and longer each time it is called.
1935 if (m_anonymousMarkList
)
1937 for (GSList
* item
= m_anonymousMarkList
; item
; item
= item
->next
)
1939 GtkTextMark
* mark
= static_cast<GtkTextMark
*>(item
->data
);
1940 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1941 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1943 g_slist_free(m_anonymousMarkList
);
1944 m_anonymousMarkList
= NULL
;
1949 void wxTextCtrl::DoThaw()
1951 if ( HasFlag(wxTE_MULTILINE
) )
1954 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
1955 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1956 g_object_unref(m_buffer
);
1957 g_signal_handler_disconnect(m_buffer
, sig_id
);
1959 if (m_showPositionOnThaw
!= NULL
)
1961 gtk_text_view_scroll_mark_onscreen(
1962 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1963 m_showPositionOnThaw
= NULL
;
1967 GTKThawWidget(m_text
);
1970 // ----------------------------------------------------------------------------
1971 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1972 // ----------------------------------------------------------------------------
1974 // FIXME: when dragging on a link the sample gets an "Unknown event".
1975 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1976 // a buggy sample, or something else
1977 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1980 if( !HasFlag(wxTE_AUTO_URL
) )
1984 GtkTextIter start
, end
;
1985 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1988 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1989 event
.GetX(), event
.GetY(), &x
, &y
);
1991 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1992 if (!gtk_text_iter_has_tag(&end
, tag
))
1994 SetCursor(wxCursor(wxCURSOR_IBEAM
));
1998 SetCursor(wxCursor(wxCURSOR_HAND
));
2001 if(!gtk_text_iter_begins_tag(&start
, tag
))
2002 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
2003 if(!gtk_text_iter_ends_tag(&end
, tag
))
2004 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
2006 // Native context menu is probably not desired on an URL.
2007 // Consider making this dependent on ProcessEvent(wxTextUrlEvent) return value
2008 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
2011 wxTextUrlEvent
url_event(m_windowId
, event
,
2012 gtk_text_iter_get_offset(&start
),
2013 gtk_text_iter_get_offset(&end
));
2015 InitCommandEvent(url_event
);
2016 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2017 //event.Skip(!HandleWindowEvent(url_event));
2018 HandleWindowEvent(url_event
);
2021 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
2023 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
2025 // GtkTextView starts a drag operation when left mouse button is pressed
2026 // and ends it when it is released and if it doesn't get the release event
2027 // the next click on a control results in an assertion failure inside
2028 // gtk_text_view_start_selection_drag() which simply *kills* the program
2029 // without anything we can do about it, so always let GTK+ have this event
2030 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
2035 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2037 return GetDefaultAttributesFromGTKWidget(gtk_entry_new(), true);
2040 #endif // wxUSE_TEXTCTRL