1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/textctrl.h"
21 #include "wx/settings.h"
25 #include "wx/scopeguard.h"
26 #include "wx/strconv.h"
27 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
29 #include <sys/types.h>
34 #include "wx/gtk/private.h"
35 #include "wx/gtk/private/gtk2-compat.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
44 GtkTextIter
* WXUNUSED(start
),
45 GtkTextIter
* WXUNUSED(end
),
49 g_object_get (tag
, "name", &name
, NULL
);
51 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
52 // anonymous tag or not starting with prefix - don't remove
53 g_signal_stop_emission_by_name (buffer
, "remove_tag");
59 // remove all tags starting with the given prefix from the start..end range
61 wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer
*text_buffer
,
66 gulong remove_handler_id
= g_signal_connect
70 G_CALLBACK(wxGtkOnRemoveTag
),
73 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
74 g_signal_handler_disconnect(text_buffer
, remove_handler_id
);
77 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
78 GtkTextBuffer
*text_buffer
,
79 const wxTextAttr
& attr
,
83 static gchar buf
[1024];
88 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFONT", start
, end
);
90 wxFont
font(attr
.GetFont());
92 PangoFontDescription
*font_description
= font
.GetNativeFontInfo()->description
;
93 wxGtkString
font_string(pango_font_description_to_string(font_description
));
94 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
95 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
98 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
99 "font-desc", font_description
,
101 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
103 if (font
.GetUnderlined())
105 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
106 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
109 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
110 "underline-set", TRUE
,
111 "underline", PANGO_UNDERLINE_SINGLE
,
113 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
115 if ( font
.GetStrikethrough() )
117 g_snprintf(buf
, sizeof(buf
), "WXFONTSTRIKETHROUGH");
118 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
121 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
122 "strikethrough-set", TRUE
,
123 "strikethrough", TRUE
,
125 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
129 if (attr
.HasTextColour())
131 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFORECOLOR", start
, end
);
133 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
134 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
135 colFg
->red
, colFg
->green
, colFg
->blue
);
136 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
139 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
140 "foreground-gdk", colFg
, NULL
);
141 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
144 if (attr
.HasBackgroundColour())
146 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXBACKCOLOR", start
, end
);
148 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
149 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
150 colBg
->red
, colBg
->green
, colBg
->blue
);
151 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
154 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
155 "background-gdk", colBg
, NULL
);
156 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
159 if (attr
.HasAlignment())
161 GtkTextIter para_start
, para_end
= *end
;
162 gtk_text_buffer_get_iter_at_line( text_buffer
,
164 gtk_text_iter_get_line(start
) );
165 gtk_text_iter_forward_line(¶_end
);
167 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXALIGNMENT", ¶_start
, ¶_end
);
169 GtkJustification align
;
170 switch (attr
.GetAlignment())
172 case wxTEXT_ALIGNMENT_RIGHT
:
173 align
= GTK_JUSTIFY_RIGHT
;
175 case wxTEXT_ALIGNMENT_CENTER
:
176 align
= GTK_JUSTIFY_CENTER
;
178 case wxTEXT_ALIGNMENT_JUSTIFIED
:
180 align
= GTK_JUSTIFY_FILL
;
182 #elif GTK_CHECK_VERSION(2,11,0)
183 // gtk+ doesn't support justify before gtk+-2.11.0 with pango-1.17 being available
184 // (but if new enough pango isn't available it's a mere gtk warning)
185 if (!gtk_check_version(2,11,0))
187 align
= GTK_JUSTIFY_FILL
;
193 align
= GTK_JUSTIFY_LEFT
;
197 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
198 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
201 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
202 "justification", align
, NULL
);
203 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
206 if (attr
.HasLeftIndent())
208 // Indentation attribute
210 // Clear old indentation tags
211 GtkTextIter para_start
, para_end
= *end
;
212 gtk_text_buffer_get_iter_at_line( text_buffer
,
214 gtk_text_iter_get_line(start
) );
215 gtk_text_iter_forward_line(¶_end
);
217 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", ¶_start
, ¶_end
);
219 // Convert indent from 1/10th of a mm into pixels
221 (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
222 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
224 const int indent
= (int)(factor
* attr
.GetLeftIndent());
225 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
233 gsubindent
= -subIndent
;
237 gindent
= -subIndent
;
241 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
242 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
245 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
246 "left-margin", gindent
, "indent", gsubindent
, NULL
);
247 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
255 GtkTextIter para_start
, para_end
= *end
;
256 gtk_text_buffer_get_iter_at_line( text_buffer
,
258 gtk_text_iter_get_line(start
) );
259 gtk_text_iter_forward_line(¶_end
);
261 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", ¶_start
, ¶_end
);
263 const wxArrayInt
& tabs
= attr
.GetTabs();
265 wxString tagname
= wxT("WXTABS");
266 g_snprintf(buf
, sizeof(buf
), "WXTABS");
267 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
268 tagname
+= wxString::Format(wxT(" %d"), tabs
[i
]);
270 const wxWX2MBbuf buftag
= tagname
.utf8_str();
272 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
276 // Factor to convert from 1/10th of a mm into pixels
278 (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
279 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
281 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
282 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
283 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
284 tag
= gtk_text_buffer_create_tag( text_buffer
, buftag
,
285 "tabs", tabArray
, NULL
);
286 pango_tab_array_free(tabArray
);
288 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
292 static void wxGtkTextInsert(GtkWidget
*text
,
293 GtkTextBuffer
*text_buffer
,
294 const wxTextAttr
& attr
,
295 const wxCharBuffer
& buffer
)
299 GtkTextIter iter
, start
;
301 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
302 gtk_text_buffer_get_insert (text_buffer
) );
303 start_offset
= gtk_text_iter_get_offset (&iter
);
304 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
306 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
308 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
311 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
315 au_apply_tag_callback(GtkTextBuffer
*buffer
,
317 GtkTextIter
* WXUNUSED(start
),
318 GtkTextIter
* WXUNUSED(end
),
319 gpointer
WXUNUSED(textctrl
))
321 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
322 g_signal_stop_emission_by_name (buffer
, "apply_tag");
326 //-----------------------------------------------------------------------------
327 // GtkTextCharPredicates for gtk_text_iter_*_find_char
328 //-----------------------------------------------------------------------------
332 pred_whitespace(gunichar ch
, gpointer
WXUNUSED(user_data
))
334 return g_unichar_isspace(ch
);
340 pred_non_whitespace (gunichar ch
, gpointer
WXUNUSED(user_data
))
342 return !g_unichar_isspace(ch
);
348 pred_nonpunct (gunichar ch
, gpointer
WXUNUSED(user_data
))
350 return !g_unichar_ispunct(ch
);
356 pred_nonpunct_or_slash (gunichar ch
, gpointer
WXUNUSED(user_data
))
358 return !g_unichar_ispunct(ch
) || ch
== '/';
362 //-----------------------------------------------------------------------------
363 // Check for links between s and e and correct tags as necessary
364 //-----------------------------------------------------------------------------
366 // This function should be made match better while being efficient at one point.
367 // Most probably with a row of regular expressions.
370 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
372 static const char *const URIPrefixes
[] =
390 GtkTextIter start
= *s
, end
= *e
;
391 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
393 // Get our special link tag
394 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
396 // Get rid of punctuation from beginning and end.
397 // Might want to move this to au_check_range if an improved link checking doesn't
398 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
399 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
400 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
402 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
403 gtk_text_iter_forward_char(&end
);
405 wxGtkString
text(gtk_text_iter_get_text( &start
, &end
));
406 size_t len
= strlen(text
), prefix_len
;
409 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
411 prefix_len
= strlen(URIPrefixes
[n
]);
412 if((len
> prefix_len
) && !wxStrnicmp(text
, URIPrefixes
[n
], prefix_len
))
416 if(n
< WXSIZEOF(URIPrefixes
))
418 gulong signal_id
= g_signal_handler_find (buffer
,
419 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
421 (gpointer
)au_apply_tag_callback
, NULL
);
423 g_signal_handler_block (buffer
, signal_id
);
424 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
425 g_signal_handler_unblock (buffer
, signal_id
);
432 au_check_range(GtkTextIter
*s
,
433 GtkTextIter
*range_end
)
435 GtkTextIter range_start
= *s
;
436 GtkTextIter word_end
;
437 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
438 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
440 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
442 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
443 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
445 while(!gtk_text_iter_equal(&range_start
, range_end
))
447 word_end
= range_start
;
448 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
450 // Now we should have a word delimited by range_start and word_end, correct link tags
451 au_check_word(&range_start
, &word_end
);
453 range_start
= word_end
;
454 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
459 //-----------------------------------------------------------------------------
460 // "insert-text" for GtkTextBuffer
461 //-----------------------------------------------------------------------------
465 au_insert_text_callback(GtkTextBuffer
* WXUNUSED(buffer
),
471 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
474 GtkTextIter start
= *end
;
475 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
477 GtkTextIter line_start
= start
;
478 GtkTextIter line_end
= *end
;
479 GtkTextIter words_start
= start
;
480 GtkTextIter words_end
= *end
;
482 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
483 gtk_text_iter_forward_to_line_end(&line_end
);
484 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
485 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
487 au_check_range(&words_start
, &words_end
);
491 //-----------------------------------------------------------------------------
492 // "delete-range" for GtkTextBuffer
493 //-----------------------------------------------------------------------------
497 au_delete_range_callback(GtkTextBuffer
* WXUNUSED(buffer
),
502 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
505 GtkTextIter line_start
= *start
, line_end
= *end
;
507 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
508 gtk_text_iter_forward_to_line_end(&line_end
);
509 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
510 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
512 au_check_range(start
, end
);
516 //-----------------------------------------------------------------------------
517 // "populate_popup" from text control and "unmap" from its poup menu
518 //-----------------------------------------------------------------------------
522 gtk_textctrl_popup_unmap( GtkMenu
*WXUNUSED(menu
), wxTextCtrl
* win
)
524 win
->GTKEnableFocusOutEvent();
530 gtk_textctrl_populate_popup( GtkEntry
*WXUNUSED(entry
), GtkMenu
*menu
, wxTextCtrl
*win
)
532 win
->GTKDisableFocusOutEvent();
534 g_signal_connect (menu
, "unmap", G_CALLBACK (gtk_textctrl_popup_unmap
), win
);
538 //-----------------------------------------------------------------------------
540 //-----------------------------------------------------------------------------
544 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
546 if ( win
->IgnoreTextUpdate() )
549 if (!win
->m_hasVMT
) return;
551 if ( win
->MarkDirtyOnChange() )
554 win
->SendTextUpdatedEvent();
558 //-----------------------------------------------------------------------------
560 //-----------------------------------------------------------------------------
563 static void mark_set(GtkTextBuffer
*, GtkTextIter
*, GtkTextMark
* mark
, GSList
** markList
)
565 if (gtk_text_mark_get_name(mark
) == NULL
)
566 *markList
= g_slist_prepend(*markList
, mark
);
570 //-----------------------------------------------------------------------------
572 //-----------------------------------------------------------------------------
574 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
575 EVT_CHAR(wxTextCtrl::OnChar
)
577 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
578 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
579 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
580 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
581 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
583 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
584 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
585 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
586 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
587 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
589 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
590 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
591 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
592 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
593 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
594 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
595 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
596 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
597 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
600 void wxTextCtrl::Init()
605 m_countUpdatesToIgnore
= 0;
607 SetUpdateFont(false);
610 m_showPositionOnThaw
= NULL
;
611 m_anonymousMarkList
= NULL
;
614 wxTextCtrl::~wxTextCtrl()
616 // this is also done by wxWindowGTK dtor, but has to be done here so our
617 // DoThaw() override is called
621 if (m_anonymousMarkList
)
622 g_slist_free(m_anonymousMarkList
);
625 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
627 const wxString
&value
,
631 const wxValidator
& validator
,
632 const wxString
&name
)
636 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
639 bool wxTextCtrl::Create( wxWindow
*parent
,
641 const wxString
&value
,
645 const wxValidator
& validator
,
646 const wxString
&name
)
648 if (!PreCreation( parent
, pos
, size
) ||
649 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
651 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
655 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
659 m_buffer
= gtk_text_buffer_new(NULL
);
660 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
662 m_text
= gtk_text_view_new_with_buffer(m_buffer
);
663 // gtk_text_view_set_buffer adds its own reference
664 g_object_unref(m_buffer
);
665 g_signal_handler_disconnect(m_buffer
, sig_id
);
667 // create "ShowPosition" marker
669 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
670 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
672 // create scrolled window
673 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
674 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
675 GTK_POLICY_AUTOMATIC
,
676 style
& wxTE_NO_VSCROLL
678 : GTK_POLICY_AUTOMATIC
);
679 // for ScrollLines/Pages
680 m_scrollBar
[1] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(m_widget
)));
682 // Insert view into scrolled window
683 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
687 GTKScrolledWindowSetBorder(m_widget
, style
);
689 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
691 gtk_widget_set_can_focus(m_widget
, FALSE
);
695 // a single-line text control: no need for scrollbars
697 m_text
= gtk_entry_new();
698 // work around probable bug in GTK+ 2.18 when calling WriteText on a
699 // new, empty control, see http://trac.wxwidgets.org/ticket/11409
700 gtk_entry_get_text((GtkEntry
*)m_text
);
702 if (style
& wxNO_BORDER
)
703 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
706 g_object_ref(m_widget
);
708 m_parent
->DoAddChild( this );
710 m_focusWidget
= m_text
;
716 gtk_widget_show(m_text
);
719 // We want to be notified about text changes.
722 g_signal_connect (m_buffer
, "changed",
723 G_CALLBACK (gtk_text_changed_callback
), this);
727 g_signal_connect (m_text
, "changed",
728 G_CALLBACK (gtk_text_changed_callback
), this);
731 // Catch to disable focus out handling
732 g_signal_connect (m_text
, "populate_popup",
733 G_CALLBACK (gtk_textctrl_populate_popup
),
741 if (style
& wxTE_PASSWORD
)
744 if (style
& wxTE_READONLY
)
747 // left justification (alignment) is the default anyhow
748 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
749 GTKSetJustification();
753 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
754 if (style
& wxTE_AUTO_URL
)
756 GtkTextIter start
, end
;
758 // We create our wxUrl tag here for slight efficiency gain - we
759 // don't have to check for the tag existence in callbacks,
760 // hereby it's guaranteed to exist.
761 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
762 "foreground", "blue",
763 "underline", PANGO_UNDERLINE_SINGLE
,
766 // Check for URLs after each text change
767 g_signal_connect_after (m_buffer
, "insert_text",
768 G_CALLBACK (au_insert_text_callback
), this);
769 g_signal_connect_after (m_buffer
, "delete_range",
770 G_CALLBACK (au_delete_range_callback
), this);
772 // Block all wxUrl tag applying unless we do it ourselves, in which case we
773 // block this callback temporarily. This takes care of gtk+ internal
774 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
775 // which is undesired because only a part of the URL might be copied.
776 // The insert-text signal emitted inside it will take care of newly formed
777 // or wholly copied URLs.
778 g_signal_connect (m_buffer
, "apply_tag",
779 G_CALLBACK (au_apply_tag_callback
), NULL
);
781 // Check for URLs in the initial string passed to Create
782 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
783 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
784 au_check_range(&start
, &end
);
789 // do the right thing with Enter presses depending on whether we have
790 // wxTE_PROCESS_ENTER or not
791 GTKSetActivatesDefault();
795 GTKConnectClipboardSignals(m_text
);
797 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
802 GtkEditable
*wxTextCtrl::GetEditable() const
804 wxCHECK_MSG( IsSingleLine(), NULL
, "shouldn't be called for multiline" );
806 return GTK_EDITABLE(m_text
);
809 GtkEntry
*wxTextCtrl::GetEntry() const
811 return GTK_ENTRY(m_text
);
814 // ----------------------------------------------------------------------------
816 // ----------------------------------------------------------------------------
818 void wxTextCtrl::GTKSetEditable()
820 gboolean editable
= !HasFlag(wxTE_READONLY
);
821 if ( IsSingleLine() )
822 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
824 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
827 void wxTextCtrl::GTKSetVisibility()
829 wxCHECK_RET( IsSingleLine(),
830 "wxTE_PASSWORD is for single line text controls only" );
832 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
835 void wxTextCtrl::GTKSetActivatesDefault()
837 wxCHECK_RET( IsSingleLine(),
838 "wxTE_PROCESS_ENTER is for single line text controls only" );
840 gtk_entry_set_activates_default(GTK_ENTRY(m_text
),
841 !HasFlag(wxTE_PROCESS_ENTER
));
844 void wxTextCtrl::GTKSetWrapMode()
846 // no wrapping in single line controls
847 if ( !IsMultiLine() )
850 // translate wx wrapping style to GTK+
852 if ( HasFlag( wxTE_DONTWRAP
) )
853 wrap
= GTK_WRAP_NONE
;
854 else if ( HasFlag( wxTE_CHARWRAP
) )
855 wrap
= GTK_WRAP_CHAR
;
856 else if ( HasFlag( wxTE_WORDWRAP
) )
857 wrap
= GTK_WRAP_WORD
;
858 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
859 wrap
= GTK_WRAP_WORD_CHAR
;
861 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
864 void wxTextCtrl::GTKSetJustification()
868 GtkJustification just
;
869 if ( HasFlag(wxTE_RIGHT
) )
870 just
= GTK_JUSTIFY_RIGHT
;
871 else if ( HasFlag(wxTE_CENTRE
) )
872 just
= GTK_JUSTIFY_CENTER
;
873 else // wxTE_LEFT == 0
874 just
= GTK_JUSTIFY_LEFT
;
876 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
881 if ( HasFlag(wxTE_RIGHT
) )
883 else if ( HasFlag(wxTE_CENTRE
) )
888 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
892 void wxTextCtrl::SetWindowStyleFlag(long style
)
894 long styleOld
= GetWindowStyleFlag();
896 wxTextCtrlBase::SetWindowStyleFlag(style
);
898 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
901 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
904 if ( (style
& wxTE_PROCESS_ENTER
) != (styleOld
& wxTE_PROCESS_ENTER
) )
905 GTKSetActivatesDefault();
907 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
908 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
911 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
912 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
913 GTKSetJustification();
916 // ----------------------------------------------------------------------------
918 // ----------------------------------------------------------------------------
920 wxString
wxTextCtrl::GetValue() const
922 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
927 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
929 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
930 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
932 return wxGTK_CONV_BACK(text
);
936 return wxTextEntry::GetValue();
940 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
942 // GTK+ uses UTF-8 internally, we need to convert to it but from which
945 // first check the default text style (we intentionally don't check the
946 // style for the current position as it doesn't make sense for SetValue())
947 const wxTextAttr
& style
= GetDefaultStyle();
948 wxFontEncoding enc
= style
.HasFontEncoding() ? style
.GetFontEncoding()
949 : wxFONTENCODING_SYSTEM
;
951 // fall back to the controls font if no style
952 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
953 enc
= GetFont().GetEncoding();
958 bool wxTextCtrl::IsEmpty() const
961 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
963 return wxTextEntry::IsEmpty();
966 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
968 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
972 if ( !IsMultiLine() )
974 wxTextEntry::DoSetValue(value
, flags
);
980 if ( !(flags
& SetValue_SendEvent
) )
981 EnableTextChangedEvents(false);
983 gtk_text_buffer_set_text( m_buffer
, "", 0 );
985 if ( !(flags
& SetValue_SendEvent
) )
986 EnableTextChangedEvents(true);
992 const wxCharBuffer
buffer(value
.utf8_str());
994 wxFontEncoding enc
= m_defaultStyle
.HasFont()
995 ? m_defaultStyle
.GetFont().GetEncoding()
996 : wxFONTENCODING_SYSTEM
;
997 if ( enc
== wxFONTENCODING_SYSTEM
)
998 enc
= GetTextEncoding();
1000 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1003 // see comment in WriteText() as to why we must warn the user about
1005 wxLogWarning(_("Failed to set text in the text control."));
1010 if ( !(flags
& SetValue_SendEvent
) )
1012 EnableTextChangedEvents(false);
1015 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1017 if ( !m_defaultStyle
.IsDefault() )
1019 GtkTextIter start
, end
;
1020 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1021 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1025 if ( !(flags
& SetValue_SendEvent
) )
1027 EnableTextChangedEvents(true);
1031 void wxTextCtrl::WriteText( const wxString
&text
)
1033 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1035 // we're changing the text programmatically
1036 DontMarkDirtyOnNextChange();
1038 if ( !IsMultiLine() )
1040 wxTextEntry::WriteText(text
);
1045 const wxCharBuffer
buffer(text
.utf8_str());
1047 // check if we have a specific style for the current position
1048 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1050 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFontEncoding() )
1052 enc
= style
.GetFontEncoding();
1055 if ( enc
== wxFONTENCODING_SYSTEM
)
1056 enc
= GetTextEncoding();
1058 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1061 // we must log an error here as losing the text like this can be a
1062 // serious problem (e.g. imagine the document edited by user being
1063 // empty instead of containing the correct text)
1064 wxLogWarning(_("Failed to insert text in the control."));
1069 // First remove the selection if there is one
1070 // TODO: Is there an easier GTK specific way to do this?
1072 GetSelection(&from
, &to
);
1077 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1079 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1080 // won't work when frozen, text view is not using m_buffer then
1083 GtkAdjustment
* adj
= gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget
));
1084 const double value
= gtk_adjustment_get_value(adj
);
1085 const double upper
= gtk_adjustment_get_upper(adj
);
1086 const double page_size
= gtk_adjustment_get_page_size(adj
);
1087 if (wxIsSameDouble(value
, upper
- page_size
))
1089 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(m_text
),
1090 gtk_text_buffer_get_insert(m_buffer
), 0, false, 0, 1);
1095 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1098 if ( IsMultiLine() )
1101 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1103 GtkTextIter end
= line
;
1104 // avoid skipping to the next line end if this one is empty
1105 if ( !gtk_text_iter_ends_line(&line
) )
1106 gtk_text_iter_forward_to_line_end(&end
);
1108 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1109 result
= wxGTK_CONV_BACK(text
);
1114 result
= GetValue();
1119 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1121 /* If you implement this, don't forget to update the documentation!
1122 * (file docs/latex/wx/text.tex) */
1123 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1126 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1128 if ( IsMultiLine() )
1132 if (pos
> GetLastPosition())
1135 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1138 *y
= gtk_text_iter_get_line(&iter
);
1140 *x
= gtk_text_iter_get_line_offset(&iter
);
1142 else // single line control
1144 if (pos
<= gtk_entry_get_text_length(GTK_ENTRY(m_text
)))
1153 // index out of bounds
1161 long wxTextCtrl::XYToPosition(long x
, long y
) const
1163 if ( IsSingleLine() )
1167 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1170 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1171 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1174 return gtk_text_iter_get_offset(&iter
) + x
;
1177 int wxTextCtrl::GetLineLength(long lineNo
) const
1179 if ( IsMultiLine() )
1181 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1182 if (lineNo
> last_line
)
1186 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1187 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1188 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1192 wxString str
= GetLineText (lineNo
);
1193 return (int) str
.length();
1197 wxPoint
wxTextCtrl::DoPositionToCoords(long pos
) const
1199 if ( !IsMultiLine() )
1201 // Single line text entry (GtkTextEntry) doesn't have support for
1202 // getting the coordinates for the given offset. Perhaps we could
1203 // find them ourselves by using GetTextExtent() but for now just leave
1204 // it unimplemented, this function is more useful for multiline
1206 return wxDefaultPosition
;
1209 // Window coordinates for the given position is calculated by getting
1210 // the buffer coordinates and converting them to window coordinates.
1211 GtkTextView
*textview
= GTK_TEXT_VIEW(m_text
);
1214 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1216 GdkRectangle bufferCoords
;
1217 gtk_text_view_get_iter_location(textview
, &iter
, &bufferCoords
);
1221 gtk_text_view_buffer_to_window_coords(textview
, GTK_TEXT_WINDOW_WIDGET
,
1222 bufferCoords
.x
, bufferCoords
.y
,
1223 &winCoordX
, &winCoordY
);
1225 return wxPoint(winCoordX
, winCoordY
);
1228 int wxTextCtrl::GetNumberOfLines() const
1230 if ( IsMultiLine() )
1232 return gtk_text_buffer_get_line_count( m_buffer
);
1240 void wxTextCtrl::SetInsertionPoint( long pos
)
1242 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1244 if ( IsMultiLine() )
1247 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1248 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1249 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1251 // defer until Thaw, text view is not using m_buffer now
1252 m_showPositionOnThaw
= mark
;
1254 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1258 wxTextEntry::SetInsertionPoint(pos
);
1262 void wxTextCtrl::SetEditable( bool editable
)
1264 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1266 if ( IsMultiLine() )
1268 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1272 wxTextEntry::SetEditable(editable
);
1276 bool wxTextCtrl::Enable( bool enable
)
1278 if (!wxWindowBase::Enable(enable
))
1284 gtk_widget_set_sensitive( m_text
, enable
);
1285 SetCursor(enable
? wxCursor(wxCURSOR_IBEAM
) : wxCursor());
1290 // wxGTK-specific: called recursively by Enable,
1291 // to give widgets an opportunity to correct their colours after they
1292 // have been changed by Enable
1293 void wxTextCtrl::OnEnabled(bool WXUNUSED(enable
))
1295 // If we have a custom background colour, we use this colour in both
1296 // disabled and enabled mode, or we end up with a different colour under the
1298 wxColour oldColour
= GetBackgroundColour();
1299 if (oldColour
.IsOk())
1301 // Need to set twice or it'll optimize the useful stuff out
1302 if (oldColour
== * wxWHITE
)
1303 SetBackgroundColour(*wxBLACK
);
1305 SetBackgroundColour(*wxWHITE
);
1306 SetBackgroundColour(oldColour
);
1310 void wxTextCtrl::MarkDirty()
1315 void wxTextCtrl::DiscardEdits()
1320 // ----------------------------------------------------------------------------
1322 // ----------------------------------------------------------------------------
1324 void wxTextCtrl::EnableTextChangedEvents(bool enable
)
1328 g_signal_handlers_unblock_by_func(GetTextObject(),
1329 (gpointer
)gtk_text_changed_callback
, this);
1331 else // disable events
1333 g_signal_handlers_block_by_func(GetTextObject(),
1334 (gpointer
)gtk_text_changed_callback
, this);
1338 bool wxTextCtrl::IgnoreTextUpdate()
1340 if ( m_countUpdatesToIgnore
> 0 )
1342 m_countUpdatesToIgnore
--;
1350 bool wxTextCtrl::MarkDirtyOnChange()
1352 if ( m_dontMarkDirty
)
1354 m_dontMarkDirty
= false;
1362 void wxTextCtrl::SetSelection( long from
, long to
)
1364 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1366 if ( IsMultiLine() )
1368 if (from
== -1 && to
== -1)
1371 to
= GetValue().length();
1374 GtkTextIter fromi
, toi
;
1375 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1376 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1378 gtk_text_buffer_select_range( m_buffer
, &fromi
, &toi
);
1382 wxTextEntry::SetSelection(from
, to
);
1386 void wxTextCtrl::ShowPosition( long pos
)
1391 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1392 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1393 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1395 // defer until Thaw, text view is not using m_buffer now
1396 m_showPositionOnThaw
= mark
;
1398 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1402 wxTextCtrlHitTestResult
1403 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1405 if ( !IsMultiLine() )
1408 return wxTE_HT_UNKNOWN
;
1412 gtk_text_view_window_to_buffer_coords
1414 GTK_TEXT_VIEW(m_text
),
1415 GTK_TEXT_WINDOW_TEXT
,
1421 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1423 *pos
= gtk_text_iter_get_offset(&iter
);
1425 return wxTE_HT_ON_TEXT
;
1428 long wxTextCtrl::GetInsertionPoint() const
1430 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1432 if ( IsMultiLine() )
1434 // There is no direct accessor for the cursor, but
1435 // internally, the cursor is the "mark" called
1436 // "insert" in the text view's btree structure.
1438 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1440 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1442 return gtk_text_iter_get_offset( &cursor
);
1446 return wxTextEntry::GetInsertionPoint();
1450 wxTextPos
wxTextCtrl::GetLastPosition() const
1452 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1456 if ( IsMultiLine() )
1459 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1461 pos
= gtk_text_iter_get_offset( &end
);
1465 pos
= wxTextEntry::GetLastPosition();
1471 void wxTextCtrl::Remove( long from
, long to
)
1473 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1475 if ( IsMultiLine() )
1477 GtkTextIter fromi
, toi
;
1478 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1479 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1481 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1485 wxTextEntry::Remove(from
, to
);
1489 void wxTextCtrl::Cut()
1491 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1493 if ( IsMultiLine() )
1494 g_signal_emit_by_name (m_text
, "cut-clipboard");
1499 void wxTextCtrl::Copy()
1501 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1503 if ( IsMultiLine() )
1504 g_signal_emit_by_name (m_text
, "copy-clipboard");
1506 wxTextEntry::Copy();
1509 void wxTextCtrl::Paste()
1511 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1513 if ( IsMultiLine() )
1514 g_signal_emit_by_name (m_text
, "paste-clipboard");
1516 wxTextEntry::Paste();
1519 // If the return values from and to are the same, there is no
1521 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1523 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1525 if ( !IsMultiLine() )
1527 wxTextEntry::GetSelection(fromOut
, toOut
);
1533 GtkTextIter ifrom
, ito
;
1534 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1536 from
= gtk_text_iter_get_offset(&ifrom
);
1537 to
= gtk_text_iter_get_offset(&ito
);
1541 // exchange them to be compatible with wxMSW
1547 else // no selection
1550 to
= GetInsertionPoint();
1560 bool wxTextCtrl::IsEditable() const
1562 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1564 if ( IsMultiLine() )
1566 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
)) != 0;
1570 return wxTextEntry::IsEditable();
1574 bool wxTextCtrl::IsModified() const
1579 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1581 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1583 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1585 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1587 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1588 event
.SetEventObject(this);
1589 event
.SetString(GetValue());
1590 if ( HandleWindowEvent(event
) )
1598 GtkWidget
* wxTextCtrl::GetConnectWidget()
1600 return GTK_WIDGET(m_text
);
1603 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1605 if ( IsMultiLine() )
1607 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1608 GTK_TEXT_WINDOW_TEXT
);
1613 // no access to internal GdkWindows
1616 return gtk_entry_get_text_window(GTK_ENTRY(m_text
));
1621 // the font will change for subsequent text insertiongs
1622 bool wxTextCtrl::SetFont( const wxFont
&font
)
1624 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1626 if ( !wxTextCtrlBase::SetFont(font
) )
1628 // font didn't change, nothing to do
1632 if ( IsMultiLine() )
1634 SetUpdateFont(true);
1636 m_defaultStyle
.SetFont(font
);
1638 ChangeFontGlobally();
1644 void wxTextCtrl::ChangeFontGlobally()
1646 // this method is very inefficient and hence should be called as rarely as
1649 // TODO: it can be implemented much more efficiently for GTK2
1650 wxASSERT_MSG( IsMultiLine(),
1651 wxT("shouldn't be called for single line controls") );
1653 wxString value
= GetValue();
1654 if ( !value
.empty() )
1656 SetUpdateFont(false);
1663 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1665 if ( !wxControl::SetForegroundColour(colour
) )
1668 // update default fg colour too
1669 m_defaultStyle
.SetTextColour(colour
);
1674 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1676 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1678 if ( !wxControl::SetBackgroundColour( colour
) )
1681 if (!m_backgroundColour
.IsOk())
1684 // change active background color too
1685 m_defaultStyle
.SetBackgroundColour( colour
);
1690 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1692 if ( IsMultiLine() )
1694 if ( style
.IsDefault() )
1700 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1702 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1703 wxT("invalid range in wxTextCtrl::SetStyle") );
1705 GtkTextIter starti
, endi
;
1706 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1707 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1709 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1713 //else: single line text controls don't support styles
1718 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
1720 if ( !IsMultiLine() )
1722 // no styles for GtkEntry
1726 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1728 wxCHECK_MSG( position
>= 0 && position
<= l
, false,
1729 wxT("invalid range in wxTextCtrl::GetStyle") );
1731 GtkTextIter positioni
;
1732 gtk_text_buffer_get_iter_at_offset(m_buffer
, &positioni
, position
);
1734 // Obtain a copy of the default attributes
1735 GtkTextAttributes
* const
1736 pattr
= gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text
));
1737 wxON_BLOCK_EXIT1(gtk_text_attributes_unref
, pattr
);
1739 // And query GTK for the attributes at the given position using it as base
1740 if ( !gtk_text_iter_get_attributes(&positioni
, pattr
) )
1742 style
= m_defaultStyle
;
1744 else // have custom attributes
1746 style
.SetBackgroundColour(pattr
->appearance
.bg_color
);
1747 style
.SetTextColour(pattr
->appearance
.fg_color
);
1750 pangoFontString(pango_font_description_to_string(pattr
->font
));
1753 if ( font
.SetNativeFontInfo(wxString(pangoFontString
)) )
1754 style
.SetFont(font
);
1756 // TODO: set alignment, tabs and indents
1762 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1764 GTKApplyStyle(m_text
, style
);
1767 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1772 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1777 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1782 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1787 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1792 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1794 event
.Enable( CanCut() );
1797 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1799 event
.Enable( CanCopy() );
1802 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1804 event
.Enable( CanPaste() );
1807 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1809 event
.Enable( CanUndo() );
1812 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1814 event
.Enable( CanRedo() );
1817 wxSize
wxTextCtrl::DoGetBestSize() const
1819 // FIXME should be different for multi-line controls...
1820 wxSize
ret( wxControl::DoGetBestSize() );
1821 wxSize
best(80, ret
.y
);
1822 CacheBestSize(best
);
1826 // ----------------------------------------------------------------------------
1828 // ----------------------------------------------------------------------------
1830 void wxTextCtrl::DoFreeze()
1832 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1834 GTKFreezeWidget(m_text
);
1836 if ( HasFlag(wxTE_MULTILINE
) )
1838 // removing buffer dramatically speeds up insertion:
1839 g_object_ref(m_buffer
);
1840 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1841 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1842 // gtk_text_view_set_buffer adds its own reference
1843 g_object_unref(buf_new
);
1844 // These marks should be deleted when the buffer is changed,
1845 // but they are not (in GTK+ up to at least 3.0.1).
1846 // Otherwise these anonymous marks start to build up in the buffer,
1847 // and Freeze takes longer and longer each time it is called.
1848 if (m_anonymousMarkList
)
1850 for (GSList
* item
= m_anonymousMarkList
; item
; item
= item
->next
)
1852 GtkTextMark
* mark
= static_cast<GtkTextMark
*>(item
->data
);
1853 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1854 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1856 g_slist_free(m_anonymousMarkList
);
1857 m_anonymousMarkList
= NULL
;
1862 void wxTextCtrl::DoThaw()
1864 if ( HasFlag(wxTE_MULTILINE
) )
1867 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
1868 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1869 g_object_unref(m_buffer
);
1870 g_signal_handler_disconnect(m_buffer
, sig_id
);
1872 if (m_showPositionOnThaw
!= NULL
)
1874 gtk_text_view_scroll_mark_onscreen(
1875 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1876 m_showPositionOnThaw
= NULL
;
1880 GTKThawWidget(m_text
);
1883 // ----------------------------------------------------------------------------
1884 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1885 // ----------------------------------------------------------------------------
1887 // FIXME: when dragging on a link the sample gets an "Unknown event".
1888 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1889 // a buggy sample, or something else
1890 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1893 if( !HasFlag(wxTE_AUTO_URL
) )
1897 GtkTextIter start
, end
;
1898 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1901 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1902 event
.GetX(), event
.GetY(), &x
, &y
);
1904 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1905 if (!gtk_text_iter_has_tag(&end
, tag
))
1907 SetCursor(wxCursor(wxCURSOR_IBEAM
));
1911 SetCursor(wxCursor(wxCURSOR_HAND
));
1914 if(!gtk_text_iter_begins_tag(&start
, tag
))
1915 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1916 if(!gtk_text_iter_ends_tag(&end
, tag
))
1917 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1919 // Native context menu is probably not desired on an URL.
1920 // Consider making this dependent on ProcessEvent(wxTextUrlEvent) return value
1921 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1924 wxTextUrlEvent
url_event(m_windowId
, event
,
1925 gtk_text_iter_get_offset(&start
),
1926 gtk_text_iter_get_offset(&end
));
1928 InitCommandEvent(url_event
);
1929 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1930 //event.Skip(!HandleWindowEvent(url_event));
1931 HandleWindowEvent(url_event
);
1934 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1936 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1938 // GtkTextView starts a drag operation when left mouse button is pressed
1939 // and ends it when it is released and if it doesn't get the release event
1940 // the next click on a control results in an assertion failure inside
1941 // gtk_text_view_start_selection_drag() which simply *kills* the program
1942 // without anything we can do about it, so always let GTK+ have this event
1943 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1948 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1950 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
1953 #endif // wxUSE_TEXTCTRL