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 //-----------------------------------------------------------------------------
559 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
560 //-----------------------------------------------------------------------------
562 // common part of the event handlers below
564 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
565 wxEventType eventType
, const gchar
* signal_name
)
567 wxClipboardTextEvent
event( eventType
, win
->GetId() );
568 event
.SetEventObject( win
);
569 if ( win
->HandleWindowEvent( event
) )
571 // don't let the default processing to take place if we did something
572 // ourselves in the event handler
573 g_signal_stop_emission_by_name (widget
, signal_name
);
579 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
581 handle_text_clipboard_callback(
582 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
586 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
588 handle_text_clipboard_callback(
589 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
593 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
595 handle_text_clipboard_callback(
596 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
600 //-----------------------------------------------------------------------------
602 //-----------------------------------------------------------------------------
605 static void mark_set(GtkTextBuffer
*, GtkTextIter
*, GtkTextMark
* mark
, GSList
** markList
)
607 if (gtk_text_mark_get_name(mark
) == NULL
)
608 *markList
= g_slist_prepend(*markList
, mark
);
612 //-----------------------------------------------------------------------------
614 //-----------------------------------------------------------------------------
616 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
617 EVT_CHAR(wxTextCtrl::OnChar
)
619 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
620 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
621 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
622 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
623 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
625 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
626 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
627 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
628 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
629 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
631 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
632 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
633 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
634 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
635 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
636 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
637 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
638 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
639 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
642 void wxTextCtrl::Init()
647 m_countUpdatesToIgnore
= 0;
649 SetUpdateFont(false);
652 m_showPositionOnThaw
= NULL
;
653 m_anonymousMarkList
= NULL
;
656 wxTextCtrl::~wxTextCtrl()
658 if (m_anonymousMarkList
)
659 g_slist_free(m_anonymousMarkList
);
662 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
664 const wxString
&value
,
668 const wxValidator
& validator
,
669 const wxString
&name
)
673 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
676 bool wxTextCtrl::Create( wxWindow
*parent
,
678 const wxString
&value
,
682 const wxValidator
& validator
,
683 const wxString
&name
)
685 if (!PreCreation( parent
, pos
, size
) ||
686 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
688 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
692 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
696 m_buffer
= gtk_text_buffer_new(NULL
);
697 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
699 m_text
= gtk_text_view_new_with_buffer(m_buffer
);
700 // gtk_text_view_set_buffer adds its own reference
701 g_object_unref(m_buffer
);
702 g_signal_handler_disconnect(m_buffer
, sig_id
);
704 // create "ShowPosition" marker
706 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
707 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
709 // create scrolled window
710 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
711 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
712 GTK_POLICY_AUTOMATIC
,
713 style
& wxTE_NO_VSCROLL
715 : GTK_POLICY_AUTOMATIC
);
716 // for ScrollLines/Pages
717 m_scrollBar
[1] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(m_widget
)));
719 // Insert view into scrolled window
720 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
724 GTKScrolledWindowSetBorder(m_widget
, style
);
726 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
728 gtk_widget_set_can_focus(m_widget
, FALSE
);
732 // a single-line text control: no need for scrollbars
734 m_text
= gtk_entry_new();
735 // work around probable bug in GTK+ 2.18 when calling WriteText on a
736 // new, empty control, see http://trac.wxwidgets.org/ticket/11409
737 gtk_entry_get_text((GtkEntry
*)m_text
);
739 if (style
& wxNO_BORDER
)
740 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
743 g_object_ref(m_widget
);
745 m_parent
->DoAddChild( this );
747 m_focusWidget
= m_text
;
753 gtk_widget_show(m_text
);
756 // We want to be notified about text changes.
759 g_signal_connect (m_buffer
, "changed",
760 G_CALLBACK (gtk_text_changed_callback
), this);
764 g_signal_connect (m_text
, "changed",
765 G_CALLBACK (gtk_text_changed_callback
), this);
768 // Catch to disable focus out handling
769 g_signal_connect (m_text
, "populate_popup",
770 G_CALLBACK (gtk_textctrl_populate_popup
),
778 if (style
& wxTE_PASSWORD
)
781 if (style
& wxTE_READONLY
)
784 // left justification (alignment) is the default anyhow
785 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
786 GTKSetJustification();
790 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
791 if (style
& wxTE_AUTO_URL
)
793 GtkTextIter start
, end
;
795 // We create our wxUrl tag here for slight efficiency gain - we
796 // don't have to check for the tag existence in callbacks,
797 // hereby it's guaranteed to exist.
798 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
799 "foreground", "blue",
800 "underline", PANGO_UNDERLINE_SINGLE
,
803 // Check for URLs after each text change
804 g_signal_connect_after (m_buffer
, "insert_text",
805 G_CALLBACK (au_insert_text_callback
), this);
806 g_signal_connect_after (m_buffer
, "delete_range",
807 G_CALLBACK (au_delete_range_callback
), this);
809 // Block all wxUrl tag applying unless we do it ourselves, in which case we
810 // block this callback temporarily. This takes care of gtk+ internal
811 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
812 // which is undesired because only a part of the URL might be copied.
813 // The insert-text signal emitted inside it will take care of newly formed
814 // or wholly copied URLs.
815 g_signal_connect (m_buffer
, "apply_tag",
816 G_CALLBACK (au_apply_tag_callback
), NULL
);
818 // Check for URLs in the initial string passed to Create
819 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
820 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
821 au_check_range(&start
, &end
);
826 // do the right thing with Enter presses depending on whether we have
827 // wxTE_PROCESS_ENTER or not
828 GTKSetActivatesDefault();
832 g_signal_connect (m_text
, "copy-clipboard",
833 G_CALLBACK (gtk_copy_clipboard_callback
), this);
834 g_signal_connect (m_text
, "cut-clipboard",
835 G_CALLBACK (gtk_cut_clipboard_callback
), this);
836 g_signal_connect (m_text
, "paste-clipboard",
837 G_CALLBACK (gtk_paste_clipboard_callback
), this);
839 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
844 GtkEditable
*wxTextCtrl::GetEditable() const
846 wxCHECK_MSG( IsSingleLine(), NULL
, "shouldn't be called for multiline" );
848 return GTK_EDITABLE(m_text
);
851 GtkEntry
*wxTextCtrl::GetEntry() const
853 return GTK_ENTRY(m_text
);
856 // ----------------------------------------------------------------------------
858 // ----------------------------------------------------------------------------
860 void wxTextCtrl::GTKSetEditable()
862 gboolean editable
= !HasFlag(wxTE_READONLY
);
863 if ( IsSingleLine() )
864 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
866 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
869 void wxTextCtrl::GTKSetVisibility()
871 wxCHECK_RET( IsSingleLine(),
872 "wxTE_PASSWORD is for single line text controls only" );
874 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
877 void wxTextCtrl::GTKSetActivatesDefault()
879 wxCHECK_RET( IsSingleLine(),
880 "wxTE_PROCESS_ENTER is for single line text controls only" );
882 gtk_entry_set_activates_default(GTK_ENTRY(m_text
),
883 !HasFlag(wxTE_PROCESS_ENTER
));
886 void wxTextCtrl::GTKSetWrapMode()
888 // no wrapping in single line controls
889 if ( !IsMultiLine() )
892 // translate wx wrapping style to GTK+
894 if ( HasFlag( wxTE_DONTWRAP
) )
895 wrap
= GTK_WRAP_NONE
;
896 else if ( HasFlag( wxTE_CHARWRAP
) )
897 wrap
= GTK_WRAP_CHAR
;
898 else if ( HasFlag( wxTE_WORDWRAP
) )
899 wrap
= GTK_WRAP_WORD
;
900 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
901 wrap
= GTK_WRAP_WORD_CHAR
;
903 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
906 void wxTextCtrl::GTKSetJustification()
910 GtkJustification just
;
911 if ( HasFlag(wxTE_RIGHT
) )
912 just
= GTK_JUSTIFY_RIGHT
;
913 else if ( HasFlag(wxTE_CENTRE
) )
914 just
= GTK_JUSTIFY_CENTER
;
915 else // wxTE_LEFT == 0
916 just
= GTK_JUSTIFY_LEFT
;
918 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
923 if ( HasFlag(wxTE_RIGHT
) )
925 else if ( HasFlag(wxTE_CENTRE
) )
930 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
934 void wxTextCtrl::SetWindowStyleFlag(long style
)
936 long styleOld
= GetWindowStyleFlag();
938 wxTextCtrlBase::SetWindowStyleFlag(style
);
940 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
943 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
946 if ( (style
& wxTE_PROCESS_ENTER
) != (styleOld
& wxTE_PROCESS_ENTER
) )
947 GTKSetActivatesDefault();
949 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
950 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
953 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
954 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
955 GTKSetJustification();
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
962 wxString
wxTextCtrl::GetValue() const
964 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
969 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
971 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
972 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
974 return wxGTK_CONV_BACK(text
);
978 return wxTextEntry::GetValue();
982 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
984 // GTK+ uses UTF-8 internally, we need to convert to it but from which
987 // first check the default text style (we intentionally don't check the
988 // style for the current position as it doesn't make sense for SetValue())
989 const wxTextAttr
& style
= GetDefaultStyle();
990 wxFontEncoding enc
= style
.HasFontEncoding() ? style
.GetFontEncoding()
991 : wxFONTENCODING_SYSTEM
;
993 // fall back to the controls font if no style
994 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
995 enc
= GetFont().GetEncoding();
1000 bool wxTextCtrl::IsEmpty() const
1002 if ( IsMultiLine() )
1003 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
1005 return wxTextEntry::IsEmpty();
1008 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
1010 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1014 if ( !IsMultiLine() )
1016 wxTextEntry::DoSetValue(value
, flags
);
1020 if (value
.IsEmpty())
1022 if ( !(flags
& SetValue_SendEvent
) )
1023 EnableTextChangedEvents(false);
1025 gtk_text_buffer_set_text( m_buffer
, "", 0 );
1027 if ( !(flags
& SetValue_SendEvent
) )
1028 EnableTextChangedEvents(true);
1034 const wxCharBuffer
buffer(value
.utf8_str());
1036 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1037 ? m_defaultStyle
.GetFont().GetEncoding()
1038 : wxFONTENCODING_SYSTEM
;
1039 if ( enc
== wxFONTENCODING_SYSTEM
)
1040 enc
= GetTextEncoding();
1042 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1045 // see comment in WriteText() as to why we must warn the user about
1047 wxLogWarning(_("Failed to set text in the text control."));
1052 if ( !(flags
& SetValue_SendEvent
) )
1054 EnableTextChangedEvents(false);
1057 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1059 if ( !m_defaultStyle
.IsDefault() )
1061 GtkTextIter start
, end
;
1062 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1063 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1067 if ( !(flags
& SetValue_SendEvent
) )
1069 EnableTextChangedEvents(true);
1073 void wxTextCtrl::WriteText( const wxString
&text
)
1075 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1077 // we're changing the text programmatically
1078 DontMarkDirtyOnNextChange();
1080 if ( !IsMultiLine() )
1082 wxTextEntry::WriteText(text
);
1087 const wxCharBuffer
buffer(text
.utf8_str());
1089 // check if we have a specific style for the current position
1090 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1092 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFontEncoding() )
1094 enc
= style
.GetFontEncoding();
1097 if ( enc
== wxFONTENCODING_SYSTEM
)
1098 enc
= GetTextEncoding();
1100 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1103 // we must log an error here as losing the text like this can be a
1104 // serious problem (e.g. imagine the document edited by user being
1105 // empty instead of containing the correct text)
1106 wxLogWarning(_("Failed to insert text in the control."));
1111 // First remove the selection if there is one
1112 // TODO: Is there an easier GTK specific way to do this?
1114 GetSelection(&from
, &to
);
1119 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1121 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1122 // won't work when frozen, text view is not using m_buffer then
1125 GtkAdjustment
* adj
= gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget
));
1126 const double value
= gtk_adjustment_get_value(adj
);
1127 const double upper
= gtk_adjustment_get_upper(adj
);
1128 const double page_size
= gtk_adjustment_get_page_size(adj
);
1129 if (wxIsSameDouble(value
, upper
- page_size
))
1131 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(m_text
),
1132 gtk_text_buffer_get_insert(m_buffer
), 0, false, 0, 1);
1137 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1140 if ( IsMultiLine() )
1143 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1145 GtkTextIter end
= line
;
1146 // avoid skipping to the next line end if this one is empty
1147 if ( !gtk_text_iter_ends_line(&line
) )
1148 gtk_text_iter_forward_to_line_end(&end
);
1150 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1151 result
= wxGTK_CONV_BACK(text
);
1156 result
= GetValue();
1161 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1163 /* If you implement this, don't forget to update the documentation!
1164 * (file docs/latex/wx/text.tex) */
1165 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1168 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1170 if ( IsMultiLine() )
1174 if (pos
> GetLastPosition())
1177 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1180 *y
= gtk_text_iter_get_line(&iter
);
1182 *x
= gtk_text_iter_get_line_offset(&iter
);
1184 else // single line control
1186 if (pos
<= gtk_entry_get_text_length(GTK_ENTRY(m_text
)))
1195 // index out of bounds
1203 long wxTextCtrl::XYToPosition(long x
, long y
) const
1205 if ( IsSingleLine() )
1209 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1212 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1213 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1216 return gtk_text_iter_get_offset(&iter
) + x
;
1219 int wxTextCtrl::GetLineLength(long lineNo
) const
1221 if ( IsMultiLine() )
1223 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1224 if (lineNo
> last_line
)
1228 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1229 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1230 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1234 wxString str
= GetLineText (lineNo
);
1235 return (int) str
.length();
1239 wxPoint
wxTextCtrl::DoPositionToCoords(long pos
) const
1241 if ( !IsMultiLine() )
1243 // Single line text entry (GtkTextEntry) doesn't have support for
1244 // getting the coordinates for the given offset. Perhaps we could
1245 // find them ourselves by using GetTextExtent() but for now just leave
1246 // it unimplemented, this function is more useful for multiline
1248 return wxDefaultPosition
;
1251 // Window coordinates for the given position is calculated by getting
1252 // the buffer coordinates and converting them to window coordinates.
1253 GtkTextView
*textview
= GTK_TEXT_VIEW(m_text
);
1256 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1258 GdkRectangle bufferCoords
;
1259 gtk_text_view_get_iter_location(textview
, &iter
, &bufferCoords
);
1263 gtk_text_view_buffer_to_window_coords(textview
, GTK_TEXT_WINDOW_WIDGET
,
1264 bufferCoords
.x
, bufferCoords
.y
,
1265 &winCoordX
, &winCoordY
);
1267 return wxPoint(winCoordX
, winCoordY
);
1270 int wxTextCtrl::GetNumberOfLines() const
1272 if ( IsMultiLine() )
1274 return gtk_text_buffer_get_line_count( m_buffer
);
1282 void wxTextCtrl::SetInsertionPoint( long pos
)
1284 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1286 if ( IsMultiLine() )
1289 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1290 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1291 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1293 // defer until Thaw, text view is not using m_buffer now
1294 m_showPositionOnThaw
= mark
;
1296 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1300 wxTextEntry::SetInsertionPoint(pos
);
1304 void wxTextCtrl::SetEditable( bool editable
)
1306 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1308 if ( IsMultiLine() )
1310 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1314 wxTextEntry::SetEditable(editable
);
1318 bool wxTextCtrl::Enable( bool enable
)
1320 if (!wxWindowBase::Enable(enable
))
1326 gtk_widget_set_sensitive( m_text
, enable
);
1327 SetCursor(enable
? wxCursor(wxCURSOR_IBEAM
) : wxCursor());
1332 // wxGTK-specific: called recursively by Enable,
1333 // to give widgets an opportunity to correct their colours after they
1334 // have been changed by Enable
1335 void wxTextCtrl::OnEnabled(bool WXUNUSED(enable
))
1337 // If we have a custom background colour, we use this colour in both
1338 // disabled and enabled mode, or we end up with a different colour under the
1340 wxColour oldColour
= GetBackgroundColour();
1341 if (oldColour
.IsOk())
1343 // Need to set twice or it'll optimize the useful stuff out
1344 if (oldColour
== * wxWHITE
)
1345 SetBackgroundColour(*wxBLACK
);
1347 SetBackgroundColour(*wxWHITE
);
1348 SetBackgroundColour(oldColour
);
1352 void wxTextCtrl::MarkDirty()
1357 void wxTextCtrl::DiscardEdits()
1362 // ----------------------------------------------------------------------------
1364 // ----------------------------------------------------------------------------
1366 void wxTextCtrl::EnableTextChangedEvents(bool enable
)
1370 g_signal_handlers_unblock_by_func(GetTextObject(),
1371 (gpointer
)gtk_text_changed_callback
, this);
1373 else // disable events
1375 g_signal_handlers_block_by_func(GetTextObject(),
1376 (gpointer
)gtk_text_changed_callback
, this);
1380 bool wxTextCtrl::IgnoreTextUpdate()
1382 if ( m_countUpdatesToIgnore
> 0 )
1384 m_countUpdatesToIgnore
--;
1392 bool wxTextCtrl::MarkDirtyOnChange()
1394 if ( m_dontMarkDirty
)
1396 m_dontMarkDirty
= false;
1404 void wxTextCtrl::SetSelection( long from
, long to
)
1406 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1408 if ( IsMultiLine() )
1410 if (from
== -1 && to
== -1)
1413 to
= GetValue().length();
1416 GtkTextIter fromi
, toi
;
1417 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1418 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1420 gtk_text_buffer_select_range( m_buffer
, &fromi
, &toi
);
1424 wxTextEntry::SetSelection(from
, to
);
1428 void wxTextCtrl::ShowPosition( long pos
)
1433 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1434 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1435 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1437 // defer until Thaw, text view is not using m_buffer now
1438 m_showPositionOnThaw
= mark
;
1440 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1444 wxTextCtrlHitTestResult
1445 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1447 if ( !IsMultiLine() )
1450 return wxTE_HT_UNKNOWN
;
1454 gtk_text_view_window_to_buffer_coords
1456 GTK_TEXT_VIEW(m_text
),
1457 GTK_TEXT_WINDOW_TEXT
,
1463 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1465 *pos
= gtk_text_iter_get_offset(&iter
);
1467 return wxTE_HT_ON_TEXT
;
1470 long wxTextCtrl::GetInsertionPoint() const
1472 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1474 if ( IsMultiLine() )
1476 // There is no direct accessor for the cursor, but
1477 // internally, the cursor is the "mark" called
1478 // "insert" in the text view's btree structure.
1480 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1482 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1484 return gtk_text_iter_get_offset( &cursor
);
1488 return wxTextEntry::GetInsertionPoint();
1492 wxTextPos
wxTextCtrl::GetLastPosition() const
1494 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1498 if ( IsMultiLine() )
1501 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1503 pos
= gtk_text_iter_get_offset( &end
);
1507 pos
= wxTextEntry::GetLastPosition();
1513 void wxTextCtrl::Remove( long from
, long to
)
1515 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1517 if ( IsMultiLine() )
1519 GtkTextIter fromi
, toi
;
1520 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1521 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1523 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1527 wxTextEntry::Remove(from
, to
);
1531 void wxTextCtrl::Cut()
1533 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1535 if ( IsMultiLine() )
1536 g_signal_emit_by_name (m_text
, "cut-clipboard");
1541 void wxTextCtrl::Copy()
1543 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1545 if ( IsMultiLine() )
1546 g_signal_emit_by_name (m_text
, "copy-clipboard");
1548 wxTextEntry::Copy();
1551 void wxTextCtrl::Paste()
1553 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1555 if ( IsMultiLine() )
1556 g_signal_emit_by_name (m_text
, "paste-clipboard");
1558 wxTextEntry::Paste();
1561 // If the return values from and to are the same, there is no
1563 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1565 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1567 if ( !IsMultiLine() )
1569 wxTextEntry::GetSelection(fromOut
, toOut
);
1575 GtkTextIter ifrom
, ito
;
1576 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1578 from
= gtk_text_iter_get_offset(&ifrom
);
1579 to
= gtk_text_iter_get_offset(&ito
);
1583 // exchange them to be compatible with wxMSW
1589 else // no selection
1592 to
= GetInsertionPoint();
1602 bool wxTextCtrl::IsEditable() const
1604 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1606 if ( IsMultiLine() )
1608 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
)) != 0;
1612 return wxTextEntry::IsEditable();
1616 bool wxTextCtrl::IsModified() const
1621 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1623 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1625 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1627 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1629 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1630 event
.SetEventObject(this);
1631 event
.SetString(GetValue());
1632 if ( HandleWindowEvent(event
) )
1640 GtkWidget
* wxTextCtrl::GetConnectWidget()
1642 return GTK_WIDGET(m_text
);
1645 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1647 if ( IsMultiLine() )
1649 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1650 GTK_TEXT_WINDOW_TEXT
);
1655 // no access to internal GdkWindows
1658 return gtk_entry_get_text_window(GTK_ENTRY(m_text
));
1663 // the font will change for subsequent text insertiongs
1664 bool wxTextCtrl::SetFont( const wxFont
&font
)
1666 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1668 if ( !wxTextCtrlBase::SetFont(font
) )
1670 // font didn't change, nothing to do
1674 if ( IsMultiLine() )
1676 SetUpdateFont(true);
1678 m_defaultStyle
.SetFont(font
);
1680 ChangeFontGlobally();
1686 void wxTextCtrl::ChangeFontGlobally()
1688 // this method is very inefficient and hence should be called as rarely as
1691 // TODO: it can be implemented much more efficiently for GTK2
1692 wxASSERT_MSG( IsMultiLine(),
1693 wxT("shouldn't be called for single line controls") );
1695 wxString value
= GetValue();
1696 if ( !value
.empty() )
1698 SetUpdateFont(false);
1705 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1707 if ( !wxControl::SetForegroundColour(colour
) )
1710 // update default fg colour too
1711 m_defaultStyle
.SetTextColour(colour
);
1716 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1718 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1720 if ( !wxControl::SetBackgroundColour( colour
) )
1723 if (!m_backgroundColour
.IsOk())
1726 // change active background color too
1727 m_defaultStyle
.SetBackgroundColour( colour
);
1732 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1734 if ( IsMultiLine() )
1736 if ( style
.IsDefault() )
1742 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1744 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1745 wxT("invalid range in wxTextCtrl::SetStyle") );
1747 GtkTextIter starti
, endi
;
1748 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1749 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1751 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1755 //else: single line text controls don't support styles
1760 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
1762 if ( !IsMultiLine() )
1764 // no styles for GtkEntry
1768 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1770 wxCHECK_MSG( position
>= 0 && position
<= l
, false,
1771 wxT("invalid range in wxTextCtrl::GetStyle") );
1773 GtkTextIter positioni
;
1774 gtk_text_buffer_get_iter_at_offset(m_buffer
, &positioni
, position
);
1776 // Obtain a copy of the default attributes
1777 GtkTextAttributes
* const
1778 pattr
= gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text
));
1779 wxON_BLOCK_EXIT1(gtk_text_attributes_unref
, pattr
);
1781 // And query GTK for the attributes at the given position using it as base
1782 if ( !gtk_text_iter_get_attributes(&positioni
, pattr
) )
1784 style
= m_defaultStyle
;
1786 else // have custom attributes
1788 style
.SetBackgroundColour(pattr
->appearance
.bg_color
);
1789 style
.SetTextColour(pattr
->appearance
.fg_color
);
1792 pangoFontString(pango_font_description_to_string(pattr
->font
));
1795 if ( font
.SetNativeFontInfo(wxString(pangoFontString
)) )
1796 style
.SetFont(font
);
1798 // TODO: set alignment, tabs and indents
1804 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1806 GTKApplyStyle(m_text
, style
);
1809 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1814 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1819 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1824 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1829 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1834 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1836 event
.Enable( CanCut() );
1839 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1841 event
.Enable( CanCopy() );
1844 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1846 event
.Enable( CanPaste() );
1849 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1851 event
.Enable( CanUndo() );
1854 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1856 event
.Enable( CanRedo() );
1859 wxSize
wxTextCtrl::DoGetBestSize() const
1861 // FIXME should be different for multi-line controls...
1862 wxSize
ret( wxControl::DoGetBestSize() );
1863 wxSize
best(80, ret
.y
);
1864 CacheBestSize(best
);
1868 // ----------------------------------------------------------------------------
1870 // ----------------------------------------------------------------------------
1872 void wxTextCtrl::DoFreeze()
1874 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1876 wxWindow::DoFreeze();
1878 if ( HasFlag(wxTE_MULTILINE
) )
1880 GTKFreezeWidget(m_text
);
1882 // removing buffer dramatically speeds up insertion:
1883 g_object_ref(m_buffer
);
1884 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1885 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1886 // gtk_text_view_set_buffer adds its own reference
1887 g_object_unref(buf_new
);
1888 // These marks should be deleted when the buffer is changed,
1889 // but they are not (in GTK+ up to at least 3.0.1).
1890 // Otherwise these anonymous marks start to build up in the buffer,
1891 // and Freeze takes longer and longer each time it is called.
1892 if (m_anonymousMarkList
)
1894 for (GSList
* item
= m_anonymousMarkList
; item
; item
= item
->next
)
1896 GtkTextMark
* mark
= static_cast<GtkTextMark
*>(item
->data
);
1897 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1898 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1900 g_slist_free(m_anonymousMarkList
);
1901 m_anonymousMarkList
= NULL
;
1906 void wxTextCtrl::DoThaw()
1908 if ( HasFlag(wxTE_MULTILINE
) )
1911 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
1912 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1913 g_object_unref(m_buffer
);
1914 g_signal_handler_disconnect(m_buffer
, sig_id
);
1916 if (m_showPositionOnThaw
!= NULL
)
1918 gtk_text_view_scroll_mark_onscreen(
1919 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1920 m_showPositionOnThaw
= NULL
;
1923 // and thaw the window
1924 GTKThawWidget(m_text
);
1930 // ----------------------------------------------------------------------------
1931 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1932 // ----------------------------------------------------------------------------
1934 // FIXME: when dragging on a link the sample gets an "Unknown event".
1935 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1936 // a buggy sample, or something else
1937 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1940 if( !HasFlag(wxTE_AUTO_URL
) )
1944 GtkTextIter start
, end
;
1945 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1948 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1949 event
.GetX(), event
.GetY(), &x
, &y
);
1951 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1952 if (!gtk_text_iter_has_tag(&end
, tag
))
1954 SetCursor(wxCursor(wxCURSOR_IBEAM
));
1958 SetCursor(wxCursor(wxCURSOR_HAND
));
1961 if(!gtk_text_iter_begins_tag(&start
, tag
))
1962 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1963 if(!gtk_text_iter_ends_tag(&end
, tag
))
1964 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1966 // Native context menu is probably not desired on an URL.
1967 // Consider making this dependent on ProcessEvent(wxTextUrlEvent) return value
1968 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1971 wxTextUrlEvent
url_event(m_windowId
, event
,
1972 gtk_text_iter_get_offset(&start
),
1973 gtk_text_iter_get_offset(&end
));
1975 InitCommandEvent(url_event
);
1976 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1977 //event.Skip(!HandleWindowEvent(url_event));
1978 HandleWindowEvent(url_event
);
1981 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1983 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1985 // GtkTextView starts a drag operation when left mouse button is pressed
1986 // and ends it when it is released and if it doesn't get the release event
1987 // the next click on a control results in an assertion failure inside
1988 // gtk_text_view_start_selection_drag() which simply *kills* the program
1989 // without anything we can do about it, so always let GTK+ have this event
1990 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1995 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1997 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
2000 #endif // wxUSE_TEXTCTRL