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"
13 #include "wx/textctrl.h"
19 #include "wx/settings.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
26 #include <sys/types.h>
30 #include "wx/gtk/private.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
37 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
44 g_object_get (tag
, "name", &name
, NULL
);
46 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
47 // anonymous tag or not starting with prefix - don't remove
48 g_signal_stop_emission_by_name (buffer
, "remove_tag");
54 // remove all tags starting with the given prefix from the start..end range
56 wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer
*text_buffer
,
61 gulong remove_handler_id
= g_signal_connect
65 G_CALLBACK(wxGtkOnRemoveTag
),
68 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
69 g_signal_handler_disconnect(text_buffer
, remove_handler_id
);
72 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
73 GtkTextBuffer
*text_buffer
,
74 const wxTextAttr
& attr
,
78 static gchar buf
[1024];
83 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFONT", start
, end
);
85 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
86 wxGtkString
font_string(pango_font_description_to_string(font_description
));
87 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
88 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
91 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
92 "font-desc", font_description
,
94 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
96 if (attr
.GetFont().GetUnderlined())
98 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
99 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
102 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
103 "underline-set", TRUE
,
104 "underline", PANGO_UNDERLINE_SINGLE
,
106 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
110 if (attr
.HasTextColour())
112 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFORECOLOR", start
, end
);
114 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
115 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
116 colFg
->red
, colFg
->green
, colFg
->blue
);
117 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
120 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
121 "foreground-gdk", colFg
, NULL
);
122 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
125 if (attr
.HasBackgroundColour())
127 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXBACKCOLOR", start
, end
);
129 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
130 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
131 colBg
->red
, colBg
->green
, colBg
->blue
);
132 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
135 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
136 "background-gdk", colBg
, NULL
);
137 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
140 if (attr
.HasAlignment())
142 GtkTextIter para_start
, para_end
= *end
;
143 gtk_text_buffer_get_iter_at_line( text_buffer
,
145 gtk_text_iter_get_line(start
) );
146 gtk_text_iter_forward_line(¶_end
);
148 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXALIGNMENT", start
, end
);
150 GtkJustification align
;
151 switch (attr
.GetAlignment())
154 align
= GTK_JUSTIFY_LEFT
;
156 case wxTEXT_ALIGNMENT_RIGHT
:
157 align
= GTK_JUSTIFY_RIGHT
;
159 case wxTEXT_ALIGNMENT_CENTER
:
160 align
= GTK_JUSTIFY_CENTER
;
162 // gtk+ doesn't support justify as of gtk+-2.7.4
165 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
166 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
169 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
170 "justification", align
, NULL
);
171 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
174 if (attr
.HasLeftIndent())
176 // Indentation attribute
178 // Clear old indentation tags
179 GtkTextIter para_start
, para_end
= *end
;
180 gtk_text_buffer_get_iter_at_line( text_buffer
,
182 gtk_text_iter_get_line(start
) );
183 gtk_text_iter_forward_line(¶_end
);
185 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", start
, end
);
187 // Convert indent from 1/10th of a mm into pixels
189 #if GTK_CHECK_VERSION(2,2,0)
190 if (!gtk_check_version(2,2,0))
191 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
192 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
195 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
197 const int indent
= (int)(factor
* attr
.GetLeftIndent());
198 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
206 gsubindent
= -subIndent
;
210 gindent
= -subIndent
;
214 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
215 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
218 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
219 "left-margin", gindent
, "indent", gsubindent
, NULL
);
220 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
228 GtkTextIter para_start
, para_end
= *end
;
229 gtk_text_buffer_get_iter_at_line( text_buffer
,
231 gtk_text_iter_get_line(start
) );
232 gtk_text_iter_forward_line(¶_end
);
234 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", start
, end
);
236 const wxArrayInt
& tabs
= attr
.GetTabs();
238 wxString tagname
= _T("WXTABS");
239 g_snprintf(buf
, sizeof(buf
), "WXTABS");
240 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
241 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
243 const wxWX2MBbuf buf
= tagname
.mb_str(wxConvUTF8
);
245 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
249 // Factor to convert from 1/10th of a mm into pixels
251 #if GTK_CHECK_VERSION(2,2,0)
252 if (!gtk_check_version(2,2,0))
253 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
254 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
257 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
259 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
260 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
261 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
262 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
263 "tabs", tabArray
, NULL
);
264 pango_tab_array_free(tabArray
);
266 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
270 static void wxGtkTextInsert(GtkWidget
*text
,
271 GtkTextBuffer
*text_buffer
,
272 const wxTextAttr
& attr
,
273 const wxCharBuffer
& buffer
)
277 GtkTextIter iter
, start
;
279 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
280 gtk_text_buffer_get_insert (text_buffer
) );
281 start_offset
= gtk_text_iter_get_offset (&iter
);
282 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
284 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
286 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
289 // ----------------------------------------------------------------------------
290 // "insert_text" for GtkEntry
291 // ----------------------------------------------------------------------------
295 gtk_insert_text_callback(GtkEditable
*editable
,
296 const gchar
*new_text
,
297 gint new_text_length
,
302 wxapp_install_idle_handler();
304 // we should only be called if we have a max len limit at all
305 GtkEntry
*entry
= GTK_ENTRY (editable
);
307 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
309 // check that we don't overflow the max length limit
311 // FIXME: this doesn't work when we paste a string which is going to be
313 if ( entry
->text_length
== entry
->text_max_length
)
315 // we don't need to run the base class version at all
316 g_signal_stop_emission_by_name (editable
, "insert_text");
318 // remember that the next changed signal is to be ignored to avoid
319 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
320 win
->IgnoreNextTextUpdate();
322 // and generate the correct one ourselves
323 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
324 event
.SetEventObject(win
);
325 event
.SetString(win
->GetValue());
326 win
->GetEventHandler()->ProcessEvent( event
);
331 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
335 au_apply_tag_callback(GtkTextBuffer
*buffer
,
341 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
342 g_signal_stop_emission_by_name (buffer
, "apply_tag");
346 //-----------------------------------------------------------------------------
347 // GtkTextCharPredicates for gtk_text_iter_*_find_char
348 //-----------------------------------------------------------------------------
352 pred_whitespace (gunichar ch
, gpointer user_data
)
354 return g_unichar_isspace(ch
);
360 pred_non_whitespace (gunichar ch
, gpointer user_data
)
362 return !g_unichar_isspace(ch
);
368 pred_nonpunct (gunichar ch
, gpointer user_data
)
370 return !g_unichar_ispunct(ch
);
376 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
378 return !g_unichar_ispunct(ch
) || ch
== '/';
382 //-----------------------------------------------------------------------------
383 // Check for links between s and e and correct tags as necessary
384 //-----------------------------------------------------------------------------
386 // This function should be made match better while being efficient at one point.
387 // Most probably with a row of regular expressions.
390 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
392 static const char *URIPrefixes
[] =
410 GtkTextIter start
= *s
, end
= *e
;
411 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
413 // Get our special link tag
414 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
416 // Get rid of punctuation from beginning and end.
417 // Might want to move this to au_check_range if an improved link checking doesn't
418 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
419 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
420 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
422 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
423 gtk_text_iter_forward_char(&end
);
425 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
426 size_t len
= strlen(text
), prefix_len
;
429 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
431 prefix_len
= strlen(URIPrefixes
[n
]);
432 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
436 if(n
< WXSIZEOF(URIPrefixes
))
438 gulong signal_id
= g_signal_handler_find (buffer
,
439 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
441 (gpointer
)au_apply_tag_callback
, NULL
);
443 g_signal_handler_block (buffer
, signal_id
);
444 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
445 g_signal_handler_unblock (buffer
, signal_id
);
452 au_check_range(GtkTextIter
*s
,
453 GtkTextIter
*range_end
)
455 GtkTextIter range_start
= *s
;
456 GtkTextIter word_end
;
457 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
458 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
460 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
462 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
463 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
465 while(!gtk_text_iter_equal(&range_start
, range_end
))
467 word_end
= range_start
;
468 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
470 // Now we should have a word delimited by range_start and word_end, correct link tags
471 au_check_word(&range_start
, &word_end
);
473 range_start
= word_end
;
474 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
479 //-----------------------------------------------------------------------------
480 // "insert-text" for GtkTextBuffer
481 //-----------------------------------------------------------------------------
485 au_insert_text_callback(GtkTextBuffer
*buffer
,
491 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
494 GtkTextIter start
= *end
;
495 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
497 GtkTextIter line_start
= start
;
498 GtkTextIter line_end
= *end
;
499 GtkTextIter words_start
= start
;
500 GtkTextIter words_end
= *end
;
502 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
503 gtk_text_iter_forward_to_line_end(&line_end
);
504 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
505 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
507 au_check_range(&words_start
, &words_end
);
511 //-----------------------------------------------------------------------------
512 // "delete-range" for GtkTextBuffer
513 //-----------------------------------------------------------------------------
517 au_delete_range_callback(GtkTextBuffer
*buffer
,
522 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
525 GtkTextIter line_start
= *start
, line_end
= *end
;
527 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
528 gtk_text_iter_forward_to_line_end(&line_end
);
529 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
530 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
532 au_check_range(start
, end
);
537 //-----------------------------------------------------------------------------
539 //-----------------------------------------------------------------------------
543 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
545 if ( win
->IgnoreTextUpdate() )
548 if (!win
->m_hasVMT
) return;
551 wxapp_install_idle_handler();
553 if ( win
->MarkDirtyOnChange() )
556 win
->SendTextUpdatedEvent();
560 //-----------------------------------------------------------------------------
561 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
562 //-----------------------------------------------------------------------------
564 // common part of the event handlers below
566 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
567 wxEventType eventType
, const gchar
* signal_name
)
569 wxClipboardTextEvent
event( eventType
, win
->GetId() );
570 event
.SetEventObject( win
);
571 if ( win
->GetEventHandler()->ProcessEvent( event
) )
573 // don't let the default processing to take place if we did something
574 // ourselves in the event handler
575 g_signal_stop_emission_by_name (widget
, signal_name
);
581 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
583 handle_text_clipboard_callback(
584 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
588 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
590 handle_text_clipboard_callback(
591 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
595 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
597 handle_text_clipboard_callback(
598 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
602 //-----------------------------------------------------------------------------
603 // "expose_event" from scrolled window and textview
604 //-----------------------------------------------------------------------------
608 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
615 //-----------------------------------------------------------------------------
617 //-----------------------------------------------------------------------------
619 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
621 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
622 EVT_CHAR(wxTextCtrl::OnChar
)
624 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
625 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
626 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
627 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
628 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
630 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
631 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
632 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
633 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
634 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
636 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
637 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
638 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
639 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
640 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
641 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
642 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
643 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
644 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
647 void wxTextCtrl::Init()
652 m_countUpdatesToIgnore
= 0;
654 SetUpdateFont(false);
658 m_showPositionOnThaw
= NULL
;
659 m_gdkHandCursor
= NULL
;
660 m_gdkXTermCursor
= NULL
;
663 wxTextCtrl::~wxTextCtrl()
666 gdk_cursor_unref(m_gdkHandCursor
);
668 gdk_cursor_unref(m_gdkXTermCursor
);
671 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
673 const wxString
&value
,
677 const wxValidator
& validator
,
678 const wxString
&name
)
682 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
685 bool wxTextCtrl::Create( wxWindow
*parent
,
687 const wxString
&value
,
691 const wxValidator
& validator
,
692 const wxString
&name
)
696 if (!PreCreation( parent
, pos
, size
) ||
697 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
699 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
703 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
708 m_text
= gtk_text_view_new();
710 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
712 // create "ShowPosition" marker
714 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
715 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
717 // create scrolled window
718 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
719 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
720 GTK_POLICY_AUTOMATIC
,
721 style
& wxTE_NO_VSCROLL
723 : GTK_POLICY_AUTOMATIC
);
724 // for ScrollLines/Pages
725 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
727 // Insert view into scrolled window
728 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
732 GtkScrolledWindowSetBorder(m_widget
, style
);
734 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
736 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
740 // a single-line text control: no need for scrollbars
742 m_text
= gtk_entry_new();
744 if (style
& wxNO_BORDER
)
745 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
748 m_parent
->DoAddChild( this );
750 m_focusWidget
= m_text
;
756 gtk_widget_show(m_text
);
764 if (style
& wxTE_PASSWORD
)
767 if (style
& wxTE_READONLY
)
770 // left justification (alignment) is the default anyhow
771 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
772 GTKSetJustification();
774 // We want to be notified about text changes.
777 g_signal_connect (m_buffer
, "changed",
778 G_CALLBACK (gtk_text_changed_callback
), this);
780 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
781 if (style
& wxTE_AUTO_URL
)
783 GtkTextIter start
, end
;
784 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
785 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
787 // We create our wxUrl tag here for slight efficiency gain - we
788 // don't have to check for the tag existance in callbacks,
789 // hereby it's guaranteed to exist.
790 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
791 "foreground", "blue",
792 "underline", PANGO_UNDERLINE_SINGLE
,
795 // Check for URLs after each text change
796 g_signal_connect_after (m_buffer
, "insert_text",
797 G_CALLBACK (au_insert_text_callback
), this);
798 g_signal_connect_after (m_buffer
, "delete_range",
799 G_CALLBACK (au_delete_range_callback
), this);
801 // Block all wxUrl tag applying unless we do it ourselves, in which case we
802 // block this callback temporarily. This takes care of gtk+ internal
803 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
804 // which is undesired because only a part of the URL might be copied.
805 // The insert-text signal emitted inside it will take care of newly formed
806 // or wholly copied URLs.
807 g_signal_connect (m_buffer
, "apply_tag",
808 G_CALLBACK (au_apply_tag_callback
), NULL
);
810 // Check for URLs in the initial string passed to Create
811 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
812 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
813 au_check_range(&start
, &end
);
818 g_signal_connect (m_text
, "changed",
819 G_CALLBACK (gtk_text_changed_callback
), this);
822 g_signal_connect (m_text
, "copy-clipboard",
823 G_CALLBACK (gtk_copy_clipboard_callback
), this);
824 g_signal_connect (m_text
, "cut-clipboard",
825 G_CALLBACK (gtk_cut_clipboard_callback
), this);
826 g_signal_connect (m_text
, "paste-clipboard",
827 G_CALLBACK (gtk_paste_clipboard_callback
), this);
829 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
834 // ----------------------------------------------------------------------------
836 // ----------------------------------------------------------------------------
838 void wxTextCtrl::GTKSetEditable()
840 gboolean editable
= !HasFlag(wxTE_READONLY
);
841 if ( IsSingleLine() )
842 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
844 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
847 void wxTextCtrl::GTKSetVisibility()
849 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
850 if ( IsSingleLine() )
851 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
854 void wxTextCtrl::GTKSetWrapMode()
856 // no wrapping in single line controls
857 if ( !IsMultiLine() )
860 // translate wx wrapping style to GTK+
862 if ( HasFlag( wxTE_DONTWRAP
) )
863 wrap
= GTK_WRAP_NONE
;
864 else if ( HasFlag( wxTE_CHARWRAP
) )
865 wrap
= GTK_WRAP_CHAR
;
866 else if ( HasFlag( wxTE_WORDWRAP
) )
867 wrap
= GTK_WRAP_WORD
;
868 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
870 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
872 if ( !gtk_check_version(2,4,0) )
874 wrap
= GTK_WRAP_WORD_CHAR
;
877 #endif // __WXGTK24__
878 wrap
= GTK_WRAP_WORD
;
881 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
884 void wxTextCtrl::GTKSetJustification()
888 GtkJustification just
;
889 if ( HasFlag(wxTE_RIGHT
) )
890 just
= GTK_JUSTIFY_RIGHT
;
891 else if ( HasFlag(wxTE_CENTRE
) )
892 just
= GTK_JUSTIFY_CENTER
;
893 else // wxTE_LEFT == 0
894 just
= GTK_JUSTIFY_LEFT
;
896 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
901 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
902 if (!gtk_check_version(2,4,0))
905 if ( HasFlag(wxTE_RIGHT
) )
907 else if ( HasFlag(wxTE_CENTRE
) )
912 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
914 #endif // __WXGTK24__
919 void wxTextCtrl::SetWindowStyleFlag(long style
)
921 long styleOld
= GetWindowStyleFlag();
923 wxTextCtrlBase::SetWindowStyleFlag(style
);
925 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
928 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
931 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
932 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
935 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
936 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
937 GTKSetJustification();
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
944 wxString
wxTextCtrl::GetValue() const
946 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
952 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
954 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
955 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
957 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
963 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
964 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
972 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
974 // GTK+ uses UTF-8 internally, we need to convert to it but from which
977 // first check the default text style (we intentionally don't check the
978 // style for the current position as it doesn't make sense for SetValue())
979 const wxTextAttr
& style
= GetDefaultStyle();
980 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
981 : wxFONTENCODING_SYSTEM
;
983 // fall back to the controls font if no style
984 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
985 enc
= GetFont().GetEncoding();
990 bool wxTextCtrl::IsEmpty() const
993 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
995 return wxTextCtrlBase::IsEmpty();
998 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
1000 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1002 // the control won't be modified any more as we programmatically replace
1003 // all the existing text, so reset the flag and don't set it again (and do
1004 // it now, before the text event handler is ran so that IsModified() called
1005 // from there returns the expected value)
1007 DontMarkDirtyOnNextChange();
1009 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1010 ? m_defaultStyle
.GetFont().GetEncoding()
1011 : wxFONTENCODING_SYSTEM
;
1012 if ( enc
== wxFONTENCODING_SYSTEM
)
1013 enc
= GetTextEncoding();
1015 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1018 // see comment in WriteText() as to why we must warn the user about
1020 wxLogWarning(_("Failed to set text in the text control."));
1024 // if the control is not empty, two "changed" signals are emitted,
1025 // otherwise only one and we need to ignore either both or one of them
1026 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
1031 IgnoreNextTextUpdate(ignore
);
1033 if ( IsMultiLine() )
1035 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1037 if ( !m_defaultStyle
.IsDefault() )
1039 GtkTextIter start
, end
;
1040 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1041 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1047 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
1050 // if, for whatever reason, the callback wasn't called the expected number
1051 // of times, still reset the flags to the default values
1052 m_dontMarkDirty
= false;
1053 m_countUpdatesToIgnore
= 0;
1056 // GRG, Jun/2000: Changed this after a lot of discussion in
1057 // the lists. wxWidgets 2.2 will have a set of flags to
1058 // customize this behaviour.
1059 SetInsertionPoint(0);
1062 void wxTextCtrl::WriteText( const wxString
&text
)
1064 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1069 // check if we have a specific style for the current position
1070 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1072 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1074 enc
= style
.GetFont().GetEncoding();
1077 if ( enc
== wxFONTENCODING_SYSTEM
)
1078 enc
= GetTextEncoding();
1080 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1083 // we must log an error here as losing the text like this can be a
1084 // serious problem (e.g. imagine the document edited by user being
1085 // empty instead of containing the correct text)
1086 wxLogWarning(_("Failed to insert text in the control."));
1090 // we're changing the text programmatically
1091 DontMarkDirtyOnNextChange();
1093 if ( IsMultiLine() )
1095 // First remove the selection if there is one
1096 // TODO: Is there an easier GTK specific way to do this?
1098 GetSelection(&from
, &to
);
1103 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1105 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1106 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1107 // won't work when frozen, text view is not using m_buffer then
1108 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1110 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1111 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1116 // First remove the selection if there is one
1117 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1119 // This moves the cursor pos to behind the inserted text.
1120 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1122 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1124 // Bring entry's cursor uptodate.
1125 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1129 void wxTextCtrl::AppendText( const wxString
&text
)
1131 SetInsertionPointEnd();
1135 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1138 if ( IsMultiLine() )
1141 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1142 GtkTextIter end
= line
;
1143 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(m_text
)->text_length
)
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 int wxTextCtrl::GetNumberOfLines() const
1235 if ( IsMultiLine() )
1237 return gtk_text_buffer_get_line_count( m_buffer
);
1245 void wxTextCtrl::SetInsertionPoint( long pos
)
1247 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1249 if ( IsMultiLine() )
1252 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1253 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1254 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1256 // defer until Thaw, text view is not using m_buffer now
1257 m_showPositionOnThaw
= mark
;
1259 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1263 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1264 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1268 void wxTextCtrl::SetInsertionPointEnd()
1270 SetInsertionPoint(-1);
1273 void wxTextCtrl::SetEditable( bool editable
)
1275 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1277 if ( IsMultiLine() )
1279 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1283 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1287 bool wxTextCtrl::Enable( bool enable
)
1289 if (!wxWindowBase::Enable(enable
))
1295 if ( IsMultiLine() )
1297 SetEditable( enable
);
1301 gtk_widget_set_sensitive( m_text
, enable
);
1307 // wxGTK-specific: called recursively by Enable,
1308 // to give widgets an opportunity to correct their colours after they
1309 // have been changed by Enable
1310 void wxTextCtrl::OnEnabled( bool enable
)
1312 // If we have a custom background colour, we use this colour in both
1313 // disabled and enabled mode, or we end up with a different colour under the
1315 wxColour oldColour
= GetBackgroundColour();
1318 // Need to set twice or it'll optimize the useful stuff out
1319 if (oldColour
== * wxWHITE
)
1320 SetBackgroundColour(*wxBLACK
);
1322 SetBackgroundColour(*wxWHITE
);
1323 SetBackgroundColour(oldColour
);
1327 void wxTextCtrl::MarkDirty()
1332 void wxTextCtrl::DiscardEdits()
1337 // ----------------------------------------------------------------------------
1338 // max text length support
1339 // ----------------------------------------------------------------------------
1341 bool wxTextCtrl::IgnoreTextUpdate()
1343 if ( m_countUpdatesToIgnore
> 0 )
1345 m_countUpdatesToIgnore
--;
1353 bool wxTextCtrl::MarkDirtyOnChange()
1355 if ( m_dontMarkDirty
)
1357 m_dontMarkDirty
= false;
1365 void wxTextCtrl::SetMaxLength(unsigned long len
)
1367 if ( !HasFlag(wxTE_MULTILINE
) )
1369 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1371 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1372 // we had tried to enter more text than allowed by max text length and
1373 // the text wasn't really changed
1375 // to detect this and generate TEXT_MAXLEN event instead of
1376 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1378 // when max len is set to 0 we disconnect our handler as it means that
1379 // we shouldn't check anything any more
1382 g_signal_connect (m_text
, "insert_text",
1383 G_CALLBACK (gtk_insert_text_callback
), this);
1387 g_signal_handlers_disconnect_by_func (m_text
,
1388 (gpointer
) gtk_insert_text_callback
, this);
1393 void wxTextCtrl::SetSelection( long from
, long to
)
1395 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1397 if (from
== -1 && to
== -1)
1400 to
= GetValue().length();
1403 if ( IsMultiLine() )
1405 GtkTextIter fromi
, toi
;
1406 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1407 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1409 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1410 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1414 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1418 void wxTextCtrl::ShowPosition( long pos
)
1423 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1424 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1425 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1427 // defer until Thaw, text view is not using m_buffer now
1428 m_showPositionOnThaw
= mark
;
1430 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1434 wxTextCtrlHitTestResult
1435 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1437 if ( !IsMultiLine() )
1440 return wxTE_HT_UNKNOWN
;
1444 gtk_text_view_window_to_buffer_coords
1446 GTK_TEXT_VIEW(m_text
),
1447 GTK_TEXT_WINDOW_TEXT
,
1453 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1455 *pos
= gtk_text_iter_get_offset(&iter
);
1457 return wxTE_HT_ON_TEXT
;
1460 long wxTextCtrl::GetInsertionPoint() const
1462 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1464 if ( IsMultiLine() )
1466 // There is no direct accessor for the cursor, but
1467 // internally, the cursor is the "mark" called
1468 // "insert" in the text view's btree structure.
1470 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1472 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1474 return gtk_text_iter_get_offset( &cursor
);
1478 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1482 wxTextPos
wxTextCtrl::GetLastPosition() const
1484 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1488 if ( IsMultiLine() )
1491 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1493 pos
= gtk_text_iter_get_offset( &end
);
1497 pos
= GTK_ENTRY(m_text
)->text_length
;
1503 void wxTextCtrl::Remove( long from
, long to
)
1505 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1507 if ( IsMultiLine() )
1509 GtkTextIter fromi
, toi
;
1510 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1511 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1513 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1516 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1519 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1521 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1527 SetInsertionPoint( from
);
1532 void wxTextCtrl::Cut()
1534 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1536 if ( IsMultiLine() )
1537 g_signal_emit_by_name (m_text
, "cut-clipboard");
1539 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1542 void wxTextCtrl::Copy()
1544 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1546 if ( IsMultiLine() )
1547 g_signal_emit_by_name (m_text
, "copy-clipboard");
1549 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1552 void wxTextCtrl::Paste()
1554 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1556 if ( IsMultiLine() )
1557 g_signal_emit_by_name (m_text
, "paste-clipboard");
1559 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1563 void wxTextCtrl::Undo()
1566 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1569 void wxTextCtrl::Redo()
1572 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1575 bool wxTextCtrl::CanUndo() const
1578 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1582 bool wxTextCtrl::CanRedo() const
1585 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1589 // If the return values from and to are the same, there is no
1591 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1593 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1597 bool haveSelection
= false;
1599 if ( IsMultiLine() )
1601 GtkTextIter ifrom
, ito
;
1602 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1604 haveSelection
= true;
1605 from
= gtk_text_iter_get_offset(&ifrom
);
1606 to
= gtk_text_iter_get_offset(&ito
);
1609 else // not multi-line
1611 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1614 haveSelection
= true;
1618 if (! haveSelection
)
1619 from
= to
= GetInsertionPoint();
1623 // exchange them to be compatible with wxMSW
1636 bool wxTextCtrl::IsEditable() const
1638 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1640 if ( IsMultiLine() )
1642 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1646 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1650 bool wxTextCtrl::IsModified() const
1655 void wxTextCtrl::Clear()
1657 SetValue( wxEmptyString
);
1660 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1662 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1664 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1666 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1668 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1669 event
.SetEventObject(this);
1670 event
.SetString(GetValue());
1671 if ( GetEventHandler()->ProcessEvent(event
) )
1675 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1677 if ( IsSingleLine() )
1679 // This will invoke the dialog default action, such
1680 // as the clicking the default button.
1682 wxWindow
*top_frame
= m_parent
;
1683 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1684 top_frame
= top_frame
->GetParent();
1686 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1688 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1690 if (window
->default_widget
)
1692 gtk_widget_activate (window
->default_widget
);
1702 GtkWidget
* wxTextCtrl::GetConnectWidget()
1704 return GTK_WIDGET(m_text
);
1707 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1709 if ( IsMultiLine() )
1711 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1712 GTK_TEXT_WINDOW_TEXT
);
1716 return GTK_ENTRY(m_text
)->text_area
;
1720 // the font will change for subsequent text insertiongs
1721 bool wxTextCtrl::SetFont( const wxFont
&font
)
1723 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1725 if ( !wxTextCtrlBase::SetFont(font
) )
1727 // font didn't change, nothing to do
1731 if ( IsMultiLine() )
1733 SetUpdateFont(true);
1735 m_defaultStyle
.SetFont(font
);
1737 ChangeFontGlobally();
1743 void wxTextCtrl::ChangeFontGlobally()
1745 // this method is very inefficient and hence should be called as rarely as
1748 // TODO: it can be implemented much more efficiently for GTK2
1749 wxASSERT_MSG( IsMultiLine(),
1750 _T("shouldn't be called for single line controls") );
1752 wxString value
= GetValue();
1753 if ( !value
.empty() )
1755 SetUpdateFont(false);
1762 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1764 if ( !wxControl::SetForegroundColour(colour
) )
1767 // update default fg colour too
1768 m_defaultStyle
.SetTextColour(colour
);
1773 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1775 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1777 if ( !wxControl::SetBackgroundColour( colour
) )
1780 if (!m_backgroundColour
.Ok())
1783 // change active background color too
1784 m_defaultStyle
.SetBackgroundColour( colour
);
1789 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1791 if ( IsMultiLine() )
1793 if ( style
.IsDefault() )
1799 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1801 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1802 _T("invalid range in wxTextCtrl::SetStyle") );
1804 GtkTextIter starti
, endi
;
1805 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1806 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1808 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1812 //else: single line text controls don't support styles
1817 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1819 gtk_widget_modify_style(m_text
, style
);
1822 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1827 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1832 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1837 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1842 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1847 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1849 event
.Enable( CanCut() );
1852 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1854 event
.Enable( CanCopy() );
1857 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1859 event
.Enable( CanPaste() );
1862 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1864 event
.Enable( CanUndo() );
1867 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1869 event
.Enable( CanRedo() );
1872 wxSize
wxTextCtrl::DoGetBestSize() const
1874 // FIXME should be different for multi-line controls...
1875 wxSize
ret( wxControl::DoGetBestSize() );
1876 wxSize
best(80, ret
.y
);
1877 CacheBestSize(best
);
1881 // ----------------------------------------------------------------------------
1883 // ----------------------------------------------------------------------------
1885 void wxTextCtrl::Freeze()
1887 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1889 if ( HasFlag(wxTE_MULTILINE
) )
1891 if (m_freezeCount
++ == 0)
1893 // freeze textview updates and remove buffer
1894 g_signal_connect (m_text
, "expose_event",
1895 G_CALLBACK (gtk_text_exposed_callback
), this);
1896 g_signal_connect (m_widget
, "expose_event",
1897 G_CALLBACK (gtk_text_exposed_callback
), this);
1898 gtk_widget_set_sensitive(m_widget
, false);
1899 g_object_ref(m_buffer
);
1900 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1901 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1902 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1903 // gtk_text_view_set_buffer adds its own reference
1904 g_object_unref(buf_new
);
1905 // This mark should be deleted when the buffer is changed,
1906 // but it's not (in GTK+ up to at least 2.10.6).
1907 // Otherwise these anonymous marks start to build up in the buffer,
1908 // and Freeze takes longer and longer each time it is called.
1909 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1910 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1915 void wxTextCtrl::Thaw()
1917 if ( HasFlag(wxTE_MULTILINE
) )
1919 wxCHECK_RET(m_freezeCount
!= 0, _T("Thaw() without matching Freeze()"));
1921 if (--m_freezeCount
== 0)
1923 // Reattach buffer and thaw textview updates
1924 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1925 g_object_unref(m_buffer
);
1926 gtk_widget_set_sensitive(m_widget
, true);
1927 g_signal_handlers_disconnect_by_func (m_widget
,
1928 (gpointer
) gtk_text_exposed_callback
, this);
1929 g_signal_handlers_disconnect_by_func (m_text
,
1930 (gpointer
) gtk_text_exposed_callback
, this);
1931 if (m_showPositionOnThaw
!= NULL
)
1933 gtk_text_view_scroll_mark_onscreen(
1934 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1935 m_showPositionOnThaw
= NULL
;
1941 // ----------------------------------------------------------------------------
1942 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1943 // ----------------------------------------------------------------------------
1945 // FIXME: when dragging on a link the sample gets an "Unknown event".
1946 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1947 // a buggy sample, or something else
1948 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1951 if( !HasFlag(wxTE_AUTO_URL
) )
1955 GtkTextIter start
, end
;
1956 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1959 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1960 event
.GetX(), event
.GetY(), &x
, &y
);
1962 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1963 if (!gtk_text_iter_has_tag(&end
, tag
))
1965 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1966 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1970 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1971 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1974 if(!gtk_text_iter_begins_tag(&start
, tag
))
1975 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1976 if(!gtk_text_iter_ends_tag(&end
, tag
))
1977 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1979 // Native context menu is probably not desired on an URL.
1980 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1981 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1984 wxTextUrlEvent
url_event(m_windowId
, event
,
1985 gtk_text_iter_get_offset(&start
),
1986 gtk_text_iter_get_offset(&end
));
1988 InitCommandEvent(url_event
);
1989 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1990 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1991 GetEventHandler()->ProcessEvent(url_event
);
1994 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1996 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1998 // GtkTextView starts a drag operation when left mouse button is pressed
1999 // and ends it when it is released and if it doesn't get the release event
2000 // the next click on a control results in an assertion failure inside
2001 // gtk_text_view_start_selection_drag() which simply *kills* the program
2002 // without anything we can do about it, so always let GTK+ have this event
2003 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
2008 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2010 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);