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>
33 #include "wx/gtk/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
40 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
42 GtkTextIter
* WXUNUSED(start
),
43 GtkTextIter
* WXUNUSED(end
),
47 g_object_get (tag
, "name", &name
, NULL
);
49 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
50 // anonymous tag or not starting with prefix - don't remove
51 g_signal_stop_emission_by_name (buffer
, "remove_tag");
57 // remove all tags starting with the given prefix from the start..end range
59 wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer
*text_buffer
,
64 gulong remove_handler_id
= g_signal_connect
68 G_CALLBACK(wxGtkOnRemoveTag
),
71 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
72 g_signal_handler_disconnect(text_buffer
, remove_handler_id
);
75 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
76 GtkTextBuffer
*text_buffer
,
77 const wxTextAttr
& attr
,
81 static gchar buf
[1024];
86 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFONT", start
, end
);
88 wxFont
font(attr
.GetFont());
90 PangoFontDescription
*font_description
= font
.GetNativeFontInfo()->description
;
91 wxGtkString
font_string(pango_font_description_to_string(font_description
));
92 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
93 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
96 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
97 "font-desc", font_description
,
99 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
101 if (font
.GetUnderlined())
103 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
104 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
107 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
108 "underline-set", TRUE
,
109 "underline", PANGO_UNDERLINE_SINGLE
,
111 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
113 if ( font
.GetStrikethrough() )
115 g_snprintf(buf
, sizeof(buf
), "WXFONTSTRIKETHROUGH");
116 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
119 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
120 "strikethrough-set", TRUE
,
121 "strikethrough", TRUE
,
123 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
127 if (attr
.HasTextColour())
129 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFORECOLOR", start
, end
);
131 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
132 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
133 colFg
->red
, colFg
->green
, colFg
->blue
);
134 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
137 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
138 "foreground-gdk", colFg
, NULL
);
139 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
142 if (attr
.HasBackgroundColour())
144 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXBACKCOLOR", start
, end
);
146 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
147 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
148 colBg
->red
, colBg
->green
, colBg
->blue
);
149 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
152 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
153 "background-gdk", colBg
, NULL
);
154 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
157 if (attr
.HasAlignment())
159 GtkTextIter para_start
, para_end
= *end
;
160 gtk_text_buffer_get_iter_at_line( text_buffer
,
162 gtk_text_iter_get_line(start
) );
163 gtk_text_iter_forward_line(¶_end
);
165 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXALIGNMENT", ¶_start
, ¶_end
);
167 GtkJustification align
;
168 switch (attr
.GetAlignment())
171 align
= GTK_JUSTIFY_LEFT
;
173 case wxTEXT_ALIGNMENT_RIGHT
:
174 align
= GTK_JUSTIFY_RIGHT
;
176 case wxTEXT_ALIGNMENT_CENTER
:
177 align
= GTK_JUSTIFY_CENTER
;
179 // gtk+ doesn't support justify before gtk+-2.11.0 with pango-1.17 being available
180 // (but if new enough pango isn't available it's a mere gtk warning)
181 #if GTK_CHECK_VERSION(2,11,0)
182 case wxTEXT_ALIGNMENT_JUSTIFIED
:
183 if (!gtk_check_version(2,11,0))
184 align
= GTK_JUSTIFY_FILL
;
186 align
= GTK_JUSTIFY_LEFT
;
191 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
192 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
195 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
196 "justification", align
, NULL
);
197 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
200 if (attr
.HasLeftIndent())
202 // Indentation attribute
204 // Clear old indentation tags
205 GtkTextIter para_start
, para_end
= *end
;
206 gtk_text_buffer_get_iter_at_line( text_buffer
,
208 gtk_text_iter_get_line(start
) );
209 gtk_text_iter_forward_line(¶_end
);
211 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", ¶_start
, ¶_end
);
213 // Convert indent from 1/10th of a mm into pixels
215 (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
216 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
218 const int indent
= (int)(factor
* attr
.GetLeftIndent());
219 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
227 gsubindent
= -subIndent
;
231 gindent
= -subIndent
;
235 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
236 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
239 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
240 "left-margin", gindent
, "indent", gsubindent
, NULL
);
241 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
249 GtkTextIter para_start
, para_end
= *end
;
250 gtk_text_buffer_get_iter_at_line( text_buffer
,
252 gtk_text_iter_get_line(start
) );
253 gtk_text_iter_forward_line(¶_end
);
255 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", ¶_start
, ¶_end
);
257 const wxArrayInt
& tabs
= attr
.GetTabs();
259 wxString tagname
= wxT("WXTABS");
260 g_snprintf(buf
, sizeof(buf
), "WXTABS");
261 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
262 tagname
+= wxString::Format(wxT(" %d"), tabs
[i
]);
264 const wxWX2MBbuf buftag
= tagname
.utf8_str();
266 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
270 // Factor to convert from 1/10th of a mm into pixels
272 (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
273 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
275 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
276 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
277 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
278 tag
= gtk_text_buffer_create_tag( text_buffer
, buftag
,
279 "tabs", tabArray
, NULL
);
280 pango_tab_array_free(tabArray
);
282 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
286 static void wxGtkTextInsert(GtkWidget
*text
,
287 GtkTextBuffer
*text_buffer
,
288 const wxTextAttr
& attr
,
289 const wxCharBuffer
& buffer
)
293 GtkTextIter iter
, start
;
295 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
296 gtk_text_buffer_get_insert (text_buffer
) );
297 start_offset
= gtk_text_iter_get_offset (&iter
);
298 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
300 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
302 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
305 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
309 au_apply_tag_callback(GtkTextBuffer
*buffer
,
311 GtkTextIter
* WXUNUSED(start
),
312 GtkTextIter
* WXUNUSED(end
),
313 gpointer
WXUNUSED(textctrl
))
315 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
316 g_signal_stop_emission_by_name (buffer
, "apply_tag");
320 //-----------------------------------------------------------------------------
321 // GtkTextCharPredicates for gtk_text_iter_*_find_char
322 //-----------------------------------------------------------------------------
326 pred_whitespace(gunichar ch
, gpointer
WXUNUSED(user_data
))
328 return g_unichar_isspace(ch
);
334 pred_non_whitespace (gunichar ch
, gpointer
WXUNUSED(user_data
))
336 return !g_unichar_isspace(ch
);
342 pred_nonpunct (gunichar ch
, gpointer
WXUNUSED(user_data
))
344 return !g_unichar_ispunct(ch
);
350 pred_nonpunct_or_slash (gunichar ch
, gpointer
WXUNUSED(user_data
))
352 return !g_unichar_ispunct(ch
) || ch
== '/';
356 //-----------------------------------------------------------------------------
357 // Check for links between s and e and correct tags as necessary
358 //-----------------------------------------------------------------------------
360 // This function should be made match better while being efficient at one point.
361 // Most probably with a row of regular expressions.
364 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
366 static const char *const URIPrefixes
[] =
384 GtkTextIter start
= *s
, end
= *e
;
385 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
387 // Get our special link tag
388 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
390 // Get rid of punctuation from beginning and end.
391 // Might want to move this to au_check_range if an improved link checking doesn't
392 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
393 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
394 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
396 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
397 gtk_text_iter_forward_char(&end
);
399 wxGtkString
text(gtk_text_iter_get_text( &start
, &end
));
400 size_t len
= strlen(text
), prefix_len
;
403 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
405 prefix_len
= strlen(URIPrefixes
[n
]);
406 if((len
> prefix_len
) && !wxStrnicmp(text
, URIPrefixes
[n
], prefix_len
))
410 if(n
< WXSIZEOF(URIPrefixes
))
412 gulong signal_id
= g_signal_handler_find (buffer
,
413 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
415 (gpointer
)au_apply_tag_callback
, NULL
);
417 g_signal_handler_block (buffer
, signal_id
);
418 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
419 g_signal_handler_unblock (buffer
, signal_id
);
426 au_check_range(GtkTextIter
*s
,
427 GtkTextIter
*range_end
)
429 GtkTextIter range_start
= *s
;
430 GtkTextIter word_end
;
431 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
432 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
434 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
436 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
437 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
439 while(!gtk_text_iter_equal(&range_start
, range_end
))
441 word_end
= range_start
;
442 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
444 // Now we should have a word delimited by range_start and word_end, correct link tags
445 au_check_word(&range_start
, &word_end
);
447 range_start
= word_end
;
448 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
453 //-----------------------------------------------------------------------------
454 // "insert-text" for GtkTextBuffer
455 //-----------------------------------------------------------------------------
459 au_insert_text_callback(GtkTextBuffer
* WXUNUSED(buffer
),
465 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
468 GtkTextIter start
= *end
;
469 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
471 GtkTextIter line_start
= start
;
472 GtkTextIter line_end
= *end
;
473 GtkTextIter words_start
= start
;
474 GtkTextIter words_end
= *end
;
476 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
477 gtk_text_iter_forward_to_line_end(&line_end
);
478 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
479 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
481 au_check_range(&words_start
, &words_end
);
485 //-----------------------------------------------------------------------------
486 // "delete-range" for GtkTextBuffer
487 //-----------------------------------------------------------------------------
491 au_delete_range_callback(GtkTextBuffer
* WXUNUSED(buffer
),
496 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
499 GtkTextIter line_start
= *start
, line_end
= *end
;
501 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
502 gtk_text_iter_forward_to_line_end(&line_end
);
503 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
504 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
506 au_check_range(start
, end
);
510 //-----------------------------------------------------------------------------
511 // "populate_popup" from text control and "unmap" from its poup menu
512 //-----------------------------------------------------------------------------
516 gtk_textctrl_popup_unmap( GtkMenu
*WXUNUSED(menu
), wxTextCtrl
* win
)
518 win
->GTKEnableFocusOutEvent();
524 gtk_textctrl_populate_popup( GtkEntry
*WXUNUSED(entry
), GtkMenu
*menu
, wxTextCtrl
*win
)
526 win
->GTKDisableFocusOutEvent();
528 g_signal_connect (menu
, "unmap", G_CALLBACK (gtk_textctrl_popup_unmap
), win
);
532 //-----------------------------------------------------------------------------
534 //-----------------------------------------------------------------------------
538 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
540 if ( win
->IgnoreTextUpdate() )
543 if (!win
->m_hasVMT
) return;
545 if ( win
->MarkDirtyOnChange() )
548 win
->SendTextUpdatedEvent();
552 //-----------------------------------------------------------------------------
553 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
554 //-----------------------------------------------------------------------------
556 // common part of the event handlers below
558 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
559 wxEventType eventType
, const gchar
* signal_name
)
561 wxClipboardTextEvent
event( eventType
, win
->GetId() );
562 event
.SetEventObject( win
);
563 if ( win
->HandleWindowEvent( event
) )
565 // don't let the default processing to take place if we did something
566 // ourselves in the event handler
567 g_signal_stop_emission_by_name (widget
, signal_name
);
573 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
575 handle_text_clipboard_callback(
576 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
580 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
582 handle_text_clipboard_callback(
583 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
587 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
589 handle_text_clipboard_callback(
590 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
594 //-----------------------------------------------------------------------------
596 //-----------------------------------------------------------------------------
599 static void mark_set(GtkTextBuffer
*, GtkTextIter
*, GtkTextMark
* mark
, GSList
** markList
)
601 if (gtk_text_mark_get_name(mark
) == NULL
)
602 *markList
= g_slist_prepend(*markList
, mark
);
606 //-----------------------------------------------------------------------------
608 //-----------------------------------------------------------------------------
610 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
611 EVT_CHAR(wxTextCtrl::OnChar
)
613 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
614 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
615 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
616 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
617 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
619 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
620 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
621 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
622 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
623 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
625 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
626 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
627 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
628 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
629 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
630 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
631 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
632 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
633 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
636 void wxTextCtrl::Init()
641 m_countUpdatesToIgnore
= 0;
643 SetUpdateFont(false);
646 m_showPositionOnThaw
= NULL
;
647 m_anonymousMarkList
= NULL
;
650 wxTextCtrl::~wxTextCtrl()
652 if (m_anonymousMarkList
)
653 g_slist_free(m_anonymousMarkList
);
656 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
658 const wxString
&value
,
662 const wxValidator
& validator
,
663 const wxString
&name
)
667 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
670 bool wxTextCtrl::Create( wxWindow
*parent
,
672 const wxString
&value
,
676 const wxValidator
& validator
,
677 const wxString
&name
)
679 if (!PreCreation( parent
, pos
, size
) ||
680 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
682 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
686 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
690 m_buffer
= gtk_text_buffer_new(NULL
);
691 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
693 m_text
= gtk_text_view_new_with_buffer(m_buffer
);
694 // gtk_text_view_set_buffer adds its own reference
695 g_object_unref(m_buffer
);
696 g_signal_handler_disconnect(m_buffer
, sig_id
);
698 // create "ShowPosition" marker
700 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
701 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
703 // create scrolled window
704 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
705 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
706 GTK_POLICY_AUTOMATIC
,
707 style
& wxTE_NO_VSCROLL
709 : GTK_POLICY_AUTOMATIC
);
710 // for ScrollLines/Pages
711 m_scrollBar
[1] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(m_widget
)));
713 // Insert view into scrolled window
714 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
718 GTKScrolledWindowSetBorder(m_widget
, style
);
720 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
722 gtk_widget_set_can_focus(m_widget
, FALSE
);
726 // a single-line text control: no need for scrollbars
728 m_text
= gtk_entry_new();
729 // work around probable bug in GTK+ 2.18 when calling WriteText on a
730 // new, empty control, see http://trac.wxwidgets.org/ticket/11409
731 gtk_entry_get_text((GtkEntry
*)m_text
);
733 if (style
& wxNO_BORDER
)
734 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
737 g_object_ref(m_widget
);
739 m_parent
->DoAddChild( this );
741 m_focusWidget
= m_text
;
747 gtk_widget_show(m_text
);
750 // We want to be notified about text changes.
753 g_signal_connect (m_buffer
, "changed",
754 G_CALLBACK (gtk_text_changed_callback
), this);
758 g_signal_connect (m_text
, "changed",
759 G_CALLBACK (gtk_text_changed_callback
), this);
762 // Catch to disable focus out handling
763 g_signal_connect (m_text
, "populate_popup",
764 G_CALLBACK (gtk_textctrl_populate_popup
),
772 if (style
& wxTE_PASSWORD
)
775 if (style
& wxTE_READONLY
)
778 // left justification (alignment) is the default anyhow
779 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
780 GTKSetJustification();
784 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
785 if (style
& wxTE_AUTO_URL
)
787 GtkTextIter start
, end
;
789 // We create our wxUrl tag here for slight efficiency gain - we
790 // don't have to check for the tag existence in callbacks,
791 // hereby it's guaranteed to exist.
792 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
793 "foreground", "blue",
794 "underline", PANGO_UNDERLINE_SINGLE
,
797 // Check for URLs after each text change
798 g_signal_connect_after (m_buffer
, "insert_text",
799 G_CALLBACK (au_insert_text_callback
), this);
800 g_signal_connect_after (m_buffer
, "delete_range",
801 G_CALLBACK (au_delete_range_callback
), this);
803 // Block all wxUrl tag applying unless we do it ourselves, in which case we
804 // block this callback temporarily. This takes care of gtk+ internal
805 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
806 // which is undesired because only a part of the URL might be copied.
807 // The insert-text signal emitted inside it will take care of newly formed
808 // or wholly copied URLs.
809 g_signal_connect (m_buffer
, "apply_tag",
810 G_CALLBACK (au_apply_tag_callback
), NULL
);
812 // Check for URLs in the initial string passed to Create
813 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
814 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
815 au_check_range(&start
, &end
);
820 // do the right thing with Enter presses depending on whether we have
821 // wxTE_PROCESS_ENTER or not
822 GTKSetActivatesDefault();
826 g_signal_connect (m_text
, "copy-clipboard",
827 G_CALLBACK (gtk_copy_clipboard_callback
), this);
828 g_signal_connect (m_text
, "cut-clipboard",
829 G_CALLBACK (gtk_cut_clipboard_callback
), this);
830 g_signal_connect (m_text
, "paste-clipboard",
831 G_CALLBACK (gtk_paste_clipboard_callback
), this);
833 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
838 GtkEditable
*wxTextCtrl::GetEditable() const
840 wxCHECK_MSG( IsSingleLine(), NULL
, "shouldn't be called for multiline" );
842 return GTK_EDITABLE(m_text
);
845 GtkEntry
*wxTextCtrl::GetEntry() const
847 return GTK_ENTRY(m_text
);
850 // ----------------------------------------------------------------------------
852 // ----------------------------------------------------------------------------
854 void wxTextCtrl::GTKSetEditable()
856 gboolean editable
= !HasFlag(wxTE_READONLY
);
857 if ( IsSingleLine() )
858 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
860 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
863 void wxTextCtrl::GTKSetVisibility()
865 wxCHECK_RET( IsSingleLine(),
866 "wxTE_PASSWORD is for single line text controls only" );
868 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
871 void wxTextCtrl::GTKSetActivatesDefault()
873 wxCHECK_RET( IsSingleLine(),
874 "wxTE_PROCESS_ENTER is for single line text controls only" );
876 gtk_entry_set_activates_default(GTK_ENTRY(m_text
),
877 !HasFlag(wxTE_PROCESS_ENTER
));
880 void wxTextCtrl::GTKSetWrapMode()
882 // no wrapping in single line controls
883 if ( !IsMultiLine() )
886 // translate wx wrapping style to GTK+
888 if ( HasFlag( wxTE_DONTWRAP
) )
889 wrap
= GTK_WRAP_NONE
;
890 else if ( HasFlag( wxTE_CHARWRAP
) )
891 wrap
= GTK_WRAP_CHAR
;
892 else if ( HasFlag( wxTE_WORDWRAP
) )
893 wrap
= GTK_WRAP_WORD
;
894 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
895 wrap
= GTK_WRAP_WORD_CHAR
;
897 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
900 void wxTextCtrl::GTKSetJustification()
904 GtkJustification just
;
905 if ( HasFlag(wxTE_RIGHT
) )
906 just
= GTK_JUSTIFY_RIGHT
;
907 else if ( HasFlag(wxTE_CENTRE
) )
908 just
= GTK_JUSTIFY_CENTER
;
909 else // wxTE_LEFT == 0
910 just
= GTK_JUSTIFY_LEFT
;
912 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
917 if ( HasFlag(wxTE_RIGHT
) )
919 else if ( HasFlag(wxTE_CENTRE
) )
924 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
928 void wxTextCtrl::SetWindowStyleFlag(long style
)
930 long styleOld
= GetWindowStyleFlag();
932 wxTextCtrlBase::SetWindowStyleFlag(style
);
934 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
937 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
940 if ( (style
& wxTE_PROCESS_ENTER
) != (styleOld
& wxTE_PROCESS_ENTER
) )
941 GTKSetActivatesDefault();
943 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
944 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
947 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
948 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
949 GTKSetJustification();
952 // ----------------------------------------------------------------------------
954 // ----------------------------------------------------------------------------
956 wxString
wxTextCtrl::GetValue() const
958 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
963 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
965 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
966 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
968 return wxGTK_CONV_BACK(text
);
972 return wxTextEntry::GetValue();
976 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
978 // GTK+ uses UTF-8 internally, we need to convert to it but from which
981 // first check the default text style (we intentionally don't check the
982 // style for the current position as it doesn't make sense for SetValue())
983 const wxTextAttr
& style
= GetDefaultStyle();
984 wxFontEncoding enc
= style
.HasFontEncoding() ? style
.GetFontEncoding()
985 : wxFONTENCODING_SYSTEM
;
987 // fall back to the controls font if no style
988 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
989 enc
= GetFont().GetEncoding();
994 bool wxTextCtrl::IsEmpty() const
997 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
999 return wxTextEntry::IsEmpty();
1002 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
1004 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1008 if ( !IsMultiLine() )
1010 wxTextEntry::DoSetValue(value
, flags
);
1014 if (value
.IsEmpty())
1016 if ( !(flags
& SetValue_SendEvent
) )
1017 EnableTextChangedEvents(false);
1019 gtk_text_buffer_set_text( m_buffer
, "", 0 );
1021 if ( !(flags
& SetValue_SendEvent
) )
1022 EnableTextChangedEvents(true);
1028 const wxCharBuffer
buffer(value
.utf8_str());
1030 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1031 ? m_defaultStyle
.GetFont().GetEncoding()
1032 : wxFONTENCODING_SYSTEM
;
1033 if ( enc
== wxFONTENCODING_SYSTEM
)
1034 enc
= GetTextEncoding();
1036 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1039 // see comment in WriteText() as to why we must warn the user about
1041 wxLogWarning(_("Failed to set text in the text control."));
1046 if ( !(flags
& SetValue_SendEvent
) )
1048 EnableTextChangedEvents(false);
1051 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1053 if ( !m_defaultStyle
.IsDefault() )
1055 GtkTextIter start
, end
;
1056 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1057 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1061 if ( !(flags
& SetValue_SendEvent
) )
1063 EnableTextChangedEvents(true);
1067 void wxTextCtrl::WriteText( const wxString
&text
)
1069 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1071 // we're changing the text programmatically
1072 DontMarkDirtyOnNextChange();
1074 if ( !IsMultiLine() )
1076 wxTextEntry::WriteText(text
);
1081 const wxCharBuffer
buffer(text
.utf8_str());
1083 // check if we have a specific style for the current position
1084 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1086 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFontEncoding() )
1088 enc
= style
.GetFontEncoding();
1091 if ( enc
== wxFONTENCODING_SYSTEM
)
1092 enc
= GetTextEncoding();
1094 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1097 // we must log an error here as losing the text like this can be a
1098 // serious problem (e.g. imagine the document edited by user being
1099 // empty instead of containing the correct text)
1100 wxLogWarning(_("Failed to insert text in the control."));
1105 // First remove the selection if there is one
1106 // TODO: Is there an easier GTK specific way to do this?
1108 GetSelection(&from
, &to
);
1113 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1115 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1116 // won't work when frozen, text view is not using m_buffer then
1119 GtkAdjustment
* adj
= gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget
));
1120 const double value
= gtk_adjustment_get_value(adj
);
1121 const double upper
= gtk_adjustment_get_upper(adj
);
1122 const double page_size
= gtk_adjustment_get_page_size(adj
);
1123 if (wxIsSameDouble(value
, upper
- page_size
))
1125 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(m_text
),
1126 gtk_text_buffer_get_insert(m_buffer
), 0, false, 0, 1);
1131 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1134 if ( IsMultiLine() )
1137 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1139 GtkTextIter end
= line
;
1140 // avoid skipping to the next line end if this one is empty
1141 if ( !gtk_text_iter_ends_line(&line
) )
1142 gtk_text_iter_forward_to_line_end(&end
);
1144 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1145 result
= wxGTK_CONV_BACK(text
);
1150 result
= GetValue();
1155 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1157 /* If you implement this, don't forget to update the documentation!
1158 * (file docs/latex/wx/text.tex) */
1159 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1162 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1164 if ( IsMultiLine() )
1168 if (pos
> GetLastPosition())
1171 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1174 *y
= gtk_text_iter_get_line(&iter
);
1176 *x
= gtk_text_iter_get_line_offset(&iter
);
1178 else // single line control
1180 if (pos
<= gtk_entry_get_text_length(GTK_ENTRY(m_text
)))
1189 // index out of bounds
1197 long wxTextCtrl::XYToPosition(long x
, long y
) const
1199 if ( IsSingleLine() )
1203 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1206 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1207 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1210 return gtk_text_iter_get_offset(&iter
) + x
;
1213 int wxTextCtrl::GetLineLength(long lineNo
) const
1215 if ( IsMultiLine() )
1217 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1218 if (lineNo
> last_line
)
1222 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1223 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1224 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1228 wxString str
= GetLineText (lineNo
);
1229 return (int) str
.length();
1233 wxPoint
wxTextCtrl::DoPositionToCoords(long pos
) const
1235 if ( !IsMultiLine() )
1237 // Single line text entry (GtkTextEntry) doesn't have support for
1238 // getting the coordinates for the given offset. Perhaps we could
1239 // find them ourselves by using GetTextExtent() but for now just leave
1240 // it unimplemented, this function is more useful for multiline
1242 return wxDefaultPosition
;
1245 // Window coordinates for the given position is calculated by getting
1246 // the buffer coordinates and converting them to window coordinates.
1247 GtkTextView
*textview
= GTK_TEXT_VIEW(m_text
);
1250 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1252 GdkRectangle bufferCoords
;
1253 gtk_text_view_get_iter_location(textview
, &iter
, &bufferCoords
);
1257 gtk_text_view_buffer_to_window_coords(textview
, GTK_TEXT_WINDOW_WIDGET
,
1258 bufferCoords
.x
, bufferCoords
.y
,
1259 &winCoordX
, &winCoordY
);
1261 return wxPoint(winCoordX
, winCoordY
);
1264 int wxTextCtrl::GetNumberOfLines() const
1266 if ( IsMultiLine() )
1268 return gtk_text_buffer_get_line_count( m_buffer
);
1276 void wxTextCtrl::SetInsertionPoint( long pos
)
1278 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1280 if ( IsMultiLine() )
1283 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1284 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1285 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1287 // defer until Thaw, text view is not using m_buffer now
1288 m_showPositionOnThaw
= mark
;
1290 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1294 wxTextEntry::SetInsertionPoint(pos
);
1298 void wxTextCtrl::SetEditable( bool editable
)
1300 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1302 if ( IsMultiLine() )
1304 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1308 wxTextEntry::SetEditable(editable
);
1312 bool wxTextCtrl::Enable( bool enable
)
1314 if (!wxWindowBase::Enable(enable
))
1320 gtk_widget_set_sensitive( m_text
, enable
);
1321 SetCursor(enable
? wxCursor(wxCURSOR_IBEAM
) : wxCursor());
1326 // wxGTK-specific: called recursively by Enable,
1327 // to give widgets an opportunity to correct their colours after they
1328 // have been changed by Enable
1329 void wxTextCtrl::OnEnabled(bool WXUNUSED(enable
))
1331 // If we have a custom background colour, we use this colour in both
1332 // disabled and enabled mode, or we end up with a different colour under the
1334 wxColour oldColour
= GetBackgroundColour();
1335 if (oldColour
.IsOk())
1337 // Need to set twice or it'll optimize the useful stuff out
1338 if (oldColour
== * wxWHITE
)
1339 SetBackgroundColour(*wxBLACK
);
1341 SetBackgroundColour(*wxWHITE
);
1342 SetBackgroundColour(oldColour
);
1346 void wxTextCtrl::MarkDirty()
1351 void wxTextCtrl::DiscardEdits()
1356 // ----------------------------------------------------------------------------
1358 // ----------------------------------------------------------------------------
1360 void wxTextCtrl::EnableTextChangedEvents(bool enable
)
1364 g_signal_handlers_unblock_by_func(GetTextObject(),
1365 (gpointer
)gtk_text_changed_callback
, this);
1367 else // disable events
1369 g_signal_handlers_block_by_func(GetTextObject(),
1370 (gpointer
)gtk_text_changed_callback
, this);
1374 bool wxTextCtrl::IgnoreTextUpdate()
1376 if ( m_countUpdatesToIgnore
> 0 )
1378 m_countUpdatesToIgnore
--;
1386 bool wxTextCtrl::MarkDirtyOnChange()
1388 if ( m_dontMarkDirty
)
1390 m_dontMarkDirty
= false;
1398 void wxTextCtrl::SetSelection( long from
, long to
)
1400 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1402 if ( IsMultiLine() )
1404 if (from
== -1 && to
== -1)
1407 to
= GetValue().length();
1410 GtkTextIter fromi
, toi
;
1411 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1412 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1414 gtk_text_buffer_select_range( m_buffer
, &fromi
, &toi
);
1418 wxTextEntry::SetSelection(from
, to
);
1422 void wxTextCtrl::ShowPosition( long pos
)
1427 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1428 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1429 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1431 // defer until Thaw, text view is not using m_buffer now
1432 m_showPositionOnThaw
= mark
;
1434 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1438 wxTextCtrlHitTestResult
1439 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1441 if ( !IsMultiLine() )
1444 return wxTE_HT_UNKNOWN
;
1448 gtk_text_view_window_to_buffer_coords
1450 GTK_TEXT_VIEW(m_text
),
1451 GTK_TEXT_WINDOW_TEXT
,
1457 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1459 *pos
= gtk_text_iter_get_offset(&iter
);
1461 return wxTE_HT_ON_TEXT
;
1464 long wxTextCtrl::GetInsertionPoint() const
1466 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1468 if ( IsMultiLine() )
1470 // There is no direct accessor for the cursor, but
1471 // internally, the cursor is the "mark" called
1472 // "insert" in the text view's btree structure.
1474 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1476 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1478 return gtk_text_iter_get_offset( &cursor
);
1482 return wxTextEntry::GetInsertionPoint();
1486 wxTextPos
wxTextCtrl::GetLastPosition() const
1488 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1492 if ( IsMultiLine() )
1495 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1497 pos
= gtk_text_iter_get_offset( &end
);
1501 pos
= wxTextEntry::GetLastPosition();
1507 void wxTextCtrl::Remove( long from
, long to
)
1509 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1511 if ( IsMultiLine() )
1513 GtkTextIter fromi
, toi
;
1514 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1515 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1517 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1521 wxTextEntry::Remove(from
, to
);
1525 void wxTextCtrl::Cut()
1527 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1529 if ( IsMultiLine() )
1530 g_signal_emit_by_name (m_text
, "cut-clipboard");
1535 void wxTextCtrl::Copy()
1537 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1539 if ( IsMultiLine() )
1540 g_signal_emit_by_name (m_text
, "copy-clipboard");
1542 wxTextEntry::Copy();
1545 void wxTextCtrl::Paste()
1547 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1549 if ( IsMultiLine() )
1550 g_signal_emit_by_name (m_text
, "paste-clipboard");
1552 wxTextEntry::Paste();
1555 // If the return values from and to are the same, there is no
1557 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1559 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1561 if ( !IsMultiLine() )
1563 wxTextEntry::GetSelection(fromOut
, toOut
);
1569 GtkTextIter ifrom
, ito
;
1570 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1572 from
= gtk_text_iter_get_offset(&ifrom
);
1573 to
= gtk_text_iter_get_offset(&ito
);
1577 // exchange them to be compatible with wxMSW
1583 else // no selection
1586 to
= GetInsertionPoint();
1596 bool wxTextCtrl::IsEditable() const
1598 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1600 if ( IsMultiLine() )
1602 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1606 return wxTextEntry::IsEditable();
1610 bool wxTextCtrl::IsModified() const
1615 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1617 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1619 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1621 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1623 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1624 event
.SetEventObject(this);
1625 event
.SetString(GetValue());
1626 if ( HandleWindowEvent(event
) )
1634 GtkWidget
* wxTextCtrl::GetConnectWidget()
1636 return GTK_WIDGET(m_text
);
1639 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1641 if ( IsMultiLine() )
1643 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1644 GTK_TEXT_WINDOW_TEXT
);
1648 return gtk_entry_get_text_window(GTK_ENTRY(m_text
));
1652 // the font will change for subsequent text insertiongs
1653 bool wxTextCtrl::SetFont( const wxFont
&font
)
1655 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1657 if ( !wxTextCtrlBase::SetFont(font
) )
1659 // font didn't change, nothing to do
1663 if ( IsMultiLine() )
1665 SetUpdateFont(true);
1667 m_defaultStyle
.SetFont(font
);
1669 ChangeFontGlobally();
1675 void wxTextCtrl::ChangeFontGlobally()
1677 // this method is very inefficient and hence should be called as rarely as
1680 // TODO: it can be implemented much more efficiently for GTK2
1681 wxASSERT_MSG( IsMultiLine(),
1682 wxT("shouldn't be called for single line controls") );
1684 wxString value
= GetValue();
1685 if ( !value
.empty() )
1687 SetUpdateFont(false);
1694 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1696 if ( !wxControl::SetForegroundColour(colour
) )
1699 // update default fg colour too
1700 m_defaultStyle
.SetTextColour(colour
);
1705 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1707 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1709 if ( !wxControl::SetBackgroundColour( colour
) )
1712 if (!m_backgroundColour
.IsOk())
1715 // change active background color too
1716 m_defaultStyle
.SetBackgroundColour( colour
);
1721 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1723 if ( IsMultiLine() )
1725 if ( style
.IsDefault() )
1731 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1733 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1734 wxT("invalid range in wxTextCtrl::SetStyle") );
1736 GtkTextIter starti
, endi
;
1737 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1738 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1740 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1744 //else: single line text controls don't support styles
1749 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
1751 if ( !IsMultiLine() )
1753 // no styles for GtkEntry
1757 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1759 wxCHECK_MSG( position
>= 0 && position
<= l
, false,
1760 wxT("invalid range in wxTextCtrl::GetStyle") );
1762 GtkTextIter positioni
;
1763 gtk_text_buffer_get_iter_at_offset(m_buffer
, &positioni
, position
);
1765 // Obtain a copy of the default attributes
1766 GtkTextAttributes
* const
1767 pattr
= gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text
));
1768 wxON_BLOCK_EXIT1(gtk_text_attributes_unref
, pattr
);
1770 // And query GTK for the attributes at the given position using it as base
1771 if ( !gtk_text_iter_get_attributes(&positioni
, pattr
) )
1773 style
= m_defaultStyle
;
1775 else // have custom attributes
1777 style
.SetBackgroundColour(pattr
->appearance
.bg_color
);
1778 style
.SetTextColour(pattr
->appearance
.fg_color
);
1781 pangoFontString(pango_font_description_to_string(pattr
->font
));
1784 if ( font
.SetNativeFontInfo(wxString(pangoFontString
)) )
1785 style
.SetFont(font
);
1787 // TODO: set alignment, tabs and indents
1793 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1795 gtk_widget_modify_style(m_text
, style
);
1798 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1803 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1808 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1813 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1818 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1823 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1825 event
.Enable( CanCut() );
1828 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1830 event
.Enable( CanCopy() );
1833 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1835 event
.Enable( CanPaste() );
1838 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1840 event
.Enable( CanUndo() );
1843 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1845 event
.Enable( CanRedo() );
1848 wxSize
wxTextCtrl::DoGetBestSize() const
1850 // FIXME should be different for multi-line controls...
1851 wxSize
ret( wxControl::DoGetBestSize() );
1852 wxSize
best(80, ret
.y
);
1853 CacheBestSize(best
);
1857 // ----------------------------------------------------------------------------
1859 // ----------------------------------------------------------------------------
1861 void wxTextCtrl::DoFreeze()
1863 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1865 wxWindow::DoFreeze();
1867 if ( HasFlag(wxTE_MULTILINE
) )
1869 GTKFreezeWidget(m_text
);
1871 // removing buffer dramatically speeds up insertion:
1872 g_object_ref(m_buffer
);
1873 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1874 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1875 // gtk_text_view_set_buffer adds its own reference
1876 g_object_unref(buf_new
);
1877 // These marks should be deleted when the buffer is changed,
1878 // but they are not (in GTK+ up to at least 3.0.1).
1879 // Otherwise these anonymous marks start to build up in the buffer,
1880 // and Freeze takes longer and longer each time it is called.
1881 if (m_anonymousMarkList
)
1883 for (GSList
* item
= m_anonymousMarkList
; item
; item
= item
->next
)
1885 GtkTextMark
* mark
= static_cast<GtkTextMark
*>(item
->data
);
1886 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1887 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1889 g_slist_free(m_anonymousMarkList
);
1890 m_anonymousMarkList
= NULL
;
1895 void wxTextCtrl::DoThaw()
1897 if ( HasFlag(wxTE_MULTILINE
) )
1900 gulong sig_id
= g_signal_connect(m_buffer
, "mark_set", G_CALLBACK(mark_set
), &m_anonymousMarkList
);
1901 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1902 g_object_unref(m_buffer
);
1903 g_signal_handler_disconnect(m_buffer
, sig_id
);
1905 if (m_showPositionOnThaw
!= NULL
)
1907 gtk_text_view_scroll_mark_onscreen(
1908 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1909 m_showPositionOnThaw
= NULL
;
1912 // and thaw the window
1913 GTKThawWidget(m_text
);
1919 // ----------------------------------------------------------------------------
1920 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1921 // ----------------------------------------------------------------------------
1923 // FIXME: when dragging on a link the sample gets an "Unknown event".
1924 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1925 // a buggy sample, or something else
1926 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1929 if( !HasFlag(wxTE_AUTO_URL
) )
1933 GtkTextIter start
, end
;
1934 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1937 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1938 event
.GetX(), event
.GetY(), &x
, &y
);
1940 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1941 if (!gtk_text_iter_has_tag(&end
, tag
))
1943 SetCursor(wxCursor(wxCURSOR_IBEAM
));
1947 SetCursor(wxCursor(wxCURSOR_HAND
));
1950 if(!gtk_text_iter_begins_tag(&start
, tag
))
1951 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1952 if(!gtk_text_iter_ends_tag(&end
, tag
))
1953 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1955 // Native context menu is probably not desired on an URL.
1956 // Consider making this dependent on ProcessEvent(wxTextUrlEvent) return value
1957 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1960 wxTextUrlEvent
url_event(m_windowId
, event
,
1961 gtk_text_iter_get_offset(&start
),
1962 gtk_text_iter_get_offset(&end
));
1964 InitCommandEvent(url_event
);
1965 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1966 //event.Skip(!HandleWindowEvent(url_event));
1967 HandleWindowEvent(url_event
);
1970 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1972 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1974 // GtkTextView starts a drag operation when left mouse button is pressed
1975 // and ends it when it is released and if it doesn't get the release event
1976 // the next click on a control results in an assertion failure inside
1977 // gtk_text_view_start_selection_drag() which simply *kills* the program
1978 // without anything we can do about it, so always let GTK+ have this event
1979 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1984 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1986 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
1989 #endif // wxUSE_TEXTCTRL