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
,
301 // we should only be called if we have a max len limit at all
302 GtkEntry
*entry
= GTK_ENTRY (editable
);
304 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
306 // check that we don't overflow the max length limit
308 // FIXME: this doesn't work when we paste a string which is going to be
310 if ( entry
->text_length
== entry
->text_max_length
)
312 // we don't need to run the base class version at all
313 g_signal_stop_emission_by_name (editable
, "insert_text");
315 // remember that the next changed signal is to be ignored to avoid
316 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
317 win
->IgnoreNextTextUpdate();
319 // and generate the correct one ourselves
320 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
321 event
.SetEventObject(win
);
322 event
.SetString(win
->GetValue());
323 win
->GetEventHandler()->ProcessEvent( event
);
328 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
332 au_apply_tag_callback(GtkTextBuffer
*buffer
,
338 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
339 g_signal_stop_emission_by_name (buffer
, "apply_tag");
343 //-----------------------------------------------------------------------------
344 // GtkTextCharPredicates for gtk_text_iter_*_find_char
345 //-----------------------------------------------------------------------------
349 pred_whitespace (gunichar ch
, gpointer user_data
)
351 return g_unichar_isspace(ch
);
357 pred_non_whitespace (gunichar ch
, gpointer user_data
)
359 return !g_unichar_isspace(ch
);
365 pred_nonpunct (gunichar ch
, gpointer user_data
)
367 return !g_unichar_ispunct(ch
);
373 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
375 return !g_unichar_ispunct(ch
) || ch
== '/';
379 //-----------------------------------------------------------------------------
380 // Check for links between s and e and correct tags as necessary
381 //-----------------------------------------------------------------------------
383 // This function should be made match better while being efficient at one point.
384 // Most probably with a row of regular expressions.
387 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
389 static const char *URIPrefixes
[] =
407 GtkTextIter start
= *s
, end
= *e
;
408 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
410 // Get our special link tag
411 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
413 // Get rid of punctuation from beginning and end.
414 // Might want to move this to au_check_range if an improved link checking doesn't
415 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
416 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
417 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
419 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
420 gtk_text_iter_forward_char(&end
);
422 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
423 size_t len
= strlen(text
), prefix_len
;
426 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
428 prefix_len
= strlen(URIPrefixes
[n
]);
429 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
433 if(n
< WXSIZEOF(URIPrefixes
))
435 gulong signal_id
= g_signal_handler_find (buffer
,
436 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
438 (gpointer
)au_apply_tag_callback
, NULL
);
440 g_signal_handler_block (buffer
, signal_id
);
441 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
442 g_signal_handler_unblock (buffer
, signal_id
);
449 au_check_range(GtkTextIter
*s
,
450 GtkTextIter
*range_end
)
452 GtkTextIter range_start
= *s
;
453 GtkTextIter word_end
;
454 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
455 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
457 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
459 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
460 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
462 while(!gtk_text_iter_equal(&range_start
, range_end
))
464 word_end
= range_start
;
465 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
467 // Now we should have a word delimited by range_start and word_end, correct link tags
468 au_check_word(&range_start
, &word_end
);
470 range_start
= word_end
;
471 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
476 //-----------------------------------------------------------------------------
477 // "insert-text" for GtkTextBuffer
478 //-----------------------------------------------------------------------------
482 au_insert_text_callback(GtkTextBuffer
*buffer
,
488 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
491 GtkTextIter start
= *end
;
492 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
494 GtkTextIter line_start
= start
;
495 GtkTextIter line_end
= *end
;
496 GtkTextIter words_start
= start
;
497 GtkTextIter words_end
= *end
;
499 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
500 gtk_text_iter_forward_to_line_end(&line_end
);
501 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
502 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
504 au_check_range(&words_start
, &words_end
);
508 //-----------------------------------------------------------------------------
509 // "delete-range" for GtkTextBuffer
510 //-----------------------------------------------------------------------------
514 au_delete_range_callback(GtkTextBuffer
*buffer
,
519 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
522 GtkTextIter line_start
= *start
, line_end
= *end
;
524 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
525 gtk_text_iter_forward_to_line_end(&line_end
);
526 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
527 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
529 au_check_range(start
, end
);
534 //-----------------------------------------------------------------------------
536 //-----------------------------------------------------------------------------
540 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
542 if ( win
->IgnoreTextUpdate() )
545 if (!win
->m_hasVMT
) return;
547 if ( win
->MarkDirtyOnChange() )
550 win
->SendTextUpdatedEvent();
554 //-----------------------------------------------------------------------------
555 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
556 //-----------------------------------------------------------------------------
558 // common part of the event handlers below
560 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
561 wxEventType eventType
, const gchar
* signal_name
)
563 wxClipboardTextEvent
event( eventType
, win
->GetId() );
564 event
.SetEventObject( win
);
565 if ( win
->GetEventHandler()->ProcessEvent( event
) )
567 // don't let the default processing to take place if we did something
568 // ourselves in the event handler
569 g_signal_stop_emission_by_name (widget
, signal_name
);
575 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
577 handle_text_clipboard_callback(
578 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
582 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
584 handle_text_clipboard_callback(
585 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
589 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
591 handle_text_clipboard_callback(
592 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
596 //-----------------------------------------------------------------------------
597 // "expose_event" from scrolled window and textview
598 //-----------------------------------------------------------------------------
602 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
609 //-----------------------------------------------------------------------------
611 //-----------------------------------------------------------------------------
613 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
615 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
616 EVT_CHAR(wxTextCtrl::OnChar
)
618 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
619 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
620 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
621 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
622 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
624 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
625 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
626 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
627 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
628 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
630 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
631 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
632 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
633 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
634 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
635 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
636 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
637 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
638 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
641 void wxTextCtrl::Init()
646 m_countUpdatesToIgnore
= 0;
648 SetUpdateFont(false);
652 m_showPositionOnThaw
= NULL
;
653 m_gdkHandCursor
= NULL
;
654 m_gdkXTermCursor
= NULL
;
657 wxTextCtrl::~wxTextCtrl()
660 gdk_cursor_unref(m_gdkHandCursor
);
662 gdk_cursor_unref(m_gdkXTermCursor
);
665 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
667 const wxString
&value
,
671 const wxValidator
& validator
,
672 const wxString
&name
)
676 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
679 bool wxTextCtrl::Create( wxWindow
*parent
,
681 const wxString
&value
,
685 const wxValidator
& validator
,
686 const wxString
&name
)
688 if (!PreCreation( parent
, pos
, size
) ||
689 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
691 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
695 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
700 m_text
= gtk_text_view_new();
702 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
704 // create "ShowPosition" marker
706 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
707 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
709 // create scrolled window
710 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
711 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
712 GTK_POLICY_AUTOMATIC
,
713 style
& wxTE_NO_VSCROLL
715 : GTK_POLICY_AUTOMATIC
);
716 // for ScrollLines/Pages
717 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
719 // Insert view into scrolled window
720 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
724 GtkScrolledWindowSetBorder(m_widget
, style
);
726 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
728 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
732 // a single-line text control: no need for scrollbars
734 m_text
= gtk_entry_new();
736 if (style
& wxNO_BORDER
)
737 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
740 m_parent
->DoAddChild( this );
742 m_focusWidget
= m_text
;
748 gtk_widget_show(m_text
);
756 if (style
& wxTE_PASSWORD
)
759 if (style
& wxTE_READONLY
)
762 // left justification (alignment) is the default anyhow
763 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
764 GTKSetJustification();
766 // We want to be notified about text changes.
769 g_signal_connect (m_buffer
, "changed",
770 G_CALLBACK (gtk_text_changed_callback
), this);
772 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
773 if (style
& wxTE_AUTO_URL
)
775 GtkTextIter start
, end
;
776 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
777 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
779 // We create our wxUrl tag here for slight efficiency gain - we
780 // don't have to check for the tag existance in callbacks,
781 // hereby it's guaranteed to exist.
782 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
783 "foreground", "blue",
784 "underline", PANGO_UNDERLINE_SINGLE
,
787 // Check for URLs after each text change
788 g_signal_connect_after (m_buffer
, "insert_text",
789 G_CALLBACK (au_insert_text_callback
), this);
790 g_signal_connect_after (m_buffer
, "delete_range",
791 G_CALLBACK (au_delete_range_callback
), this);
793 // Block all wxUrl tag applying unless we do it ourselves, in which case we
794 // block this callback temporarily. This takes care of gtk+ internal
795 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
796 // which is undesired because only a part of the URL might be copied.
797 // The insert-text signal emitted inside it will take care of newly formed
798 // or wholly copied URLs.
799 g_signal_connect (m_buffer
, "apply_tag",
800 G_CALLBACK (au_apply_tag_callback
), NULL
);
802 // Check for URLs in the initial string passed to Create
803 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
804 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
805 au_check_range(&start
, &end
);
810 g_signal_connect (m_text
, "changed",
811 G_CALLBACK (gtk_text_changed_callback
), this);
814 g_signal_connect (m_text
, "copy-clipboard",
815 G_CALLBACK (gtk_copy_clipboard_callback
), this);
816 g_signal_connect (m_text
, "cut-clipboard",
817 G_CALLBACK (gtk_cut_clipboard_callback
), this);
818 g_signal_connect (m_text
, "paste-clipboard",
819 G_CALLBACK (gtk_paste_clipboard_callback
), this);
821 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
826 // ----------------------------------------------------------------------------
828 // ----------------------------------------------------------------------------
830 void wxTextCtrl::GTKSetEditable()
832 gboolean editable
= !HasFlag(wxTE_READONLY
);
833 if ( IsSingleLine() )
834 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
836 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
839 void wxTextCtrl::GTKSetVisibility()
841 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
842 if ( IsSingleLine() )
843 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
846 void wxTextCtrl::GTKSetWrapMode()
848 // no wrapping in single line controls
849 if ( !IsMultiLine() )
852 // translate wx wrapping style to GTK+
854 if ( HasFlag( wxTE_DONTWRAP
) )
855 wrap
= GTK_WRAP_NONE
;
856 else if ( HasFlag( wxTE_CHARWRAP
) )
857 wrap
= GTK_WRAP_CHAR
;
858 else if ( HasFlag( wxTE_WORDWRAP
) )
859 wrap
= GTK_WRAP_WORD
;
860 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
862 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
864 if ( !gtk_check_version(2,4,0) )
866 wrap
= GTK_WRAP_WORD_CHAR
;
869 #endif // __WXGTK24__
870 wrap
= GTK_WRAP_WORD
;
873 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
876 void wxTextCtrl::GTKSetJustification()
880 GtkJustification just
;
881 if ( HasFlag(wxTE_RIGHT
) )
882 just
= GTK_JUSTIFY_RIGHT
;
883 else if ( HasFlag(wxTE_CENTRE
) )
884 just
= GTK_JUSTIFY_CENTER
;
885 else // wxTE_LEFT == 0
886 just
= GTK_JUSTIFY_LEFT
;
888 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
893 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
894 if (!gtk_check_version(2,4,0))
897 if ( HasFlag(wxTE_RIGHT
) )
899 else if ( HasFlag(wxTE_CENTRE
) )
904 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
906 #endif // __WXGTK24__
911 void wxTextCtrl::SetWindowStyleFlag(long style
)
913 long styleOld
= GetWindowStyleFlag();
915 wxTextCtrlBase::SetWindowStyleFlag(style
);
917 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
920 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
923 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
924 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
927 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
928 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
929 GTKSetJustification();
932 // ----------------------------------------------------------------------------
934 // ----------------------------------------------------------------------------
936 wxString
wxTextCtrl::GetValue() const
938 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
944 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
946 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
947 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
949 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
955 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
956 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
964 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
966 // GTK+ uses UTF-8 internally, we need to convert to it but from which
969 // first check the default text style (we intentionally don't check the
970 // style for the current position as it doesn't make sense for SetValue())
971 const wxTextAttr
& style
= GetDefaultStyle();
972 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
973 : wxFONTENCODING_SYSTEM
;
975 // fall back to the controls font if no style
976 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
977 enc
= GetFont().GetEncoding();
982 bool wxTextCtrl::IsEmpty() const
985 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
987 return wxTextCtrlBase::IsEmpty();
990 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
992 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
994 // the control won't be modified any more as we programmatically replace
995 // all the existing text, so reset the flag and don't set it again (and do
996 // it now, before the text event handler is ran so that IsModified() called
997 // from there returns the expected value)
999 DontMarkDirtyOnNextChange();
1001 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1002 ? m_defaultStyle
.GetFont().GetEncoding()
1003 : wxFONTENCODING_SYSTEM
;
1004 if ( enc
== wxFONTENCODING_SYSTEM
)
1005 enc
= GetTextEncoding();
1007 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1010 // see comment in WriteText() as to why we must warn the user about
1012 wxLogWarning(_("Failed to set text in the text control."));
1016 // if the control is not empty, two "changed" signals are emitted,
1017 // otherwise only one and we need to ignore either both or one of them
1018 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
1023 IgnoreNextTextUpdate(ignore
);
1025 if ( IsMultiLine() )
1027 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1029 if ( !m_defaultStyle
.IsDefault() )
1031 GtkTextIter start
, end
;
1032 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1033 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1039 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
1042 // if, for whatever reason, the callback wasn't called the expected number
1043 // of times, still reset the flags to the default values
1044 m_dontMarkDirty
= false;
1045 m_countUpdatesToIgnore
= 0;
1048 // GRG, Jun/2000: Changed this after a lot of discussion in
1049 // the lists. wxWidgets 2.2 will have a set of flags to
1050 // customize this behaviour.
1051 SetInsertionPoint(0);
1054 void wxTextCtrl::WriteText( const wxString
&text
)
1056 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1061 // check if we have a specific style for the current position
1062 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1064 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1066 enc
= style
.GetFont().GetEncoding();
1069 if ( enc
== wxFONTENCODING_SYSTEM
)
1070 enc
= GetTextEncoding();
1072 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1075 // we must log an error here as losing the text like this can be a
1076 // serious problem (e.g. imagine the document edited by user being
1077 // empty instead of containing the correct text)
1078 wxLogWarning(_("Failed to insert text in the control."));
1082 // we're changing the text programmatically
1083 DontMarkDirtyOnNextChange();
1085 if ( IsMultiLine() )
1087 // First remove the selection if there is one
1088 // TODO: Is there an easier GTK specific way to do this?
1090 GetSelection(&from
, &to
);
1095 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1097 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1098 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1099 // won't work when frozen, text view is not using m_buffer then
1100 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1102 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1103 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1108 // First remove the selection if there is one
1109 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1111 // This moves the cursor pos to behind the inserted text.
1112 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1114 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1116 // Bring entry's cursor uptodate.
1117 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1121 void wxTextCtrl::AppendText( const wxString
&text
)
1123 SetInsertionPointEnd();
1127 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1130 if ( IsMultiLine() )
1133 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1135 GtkTextIter end
= line
;
1136 // avoid skipping to the next line end if this one is empty
1137 if ( !gtk_text_iter_ends_line(&line
) )
1138 gtk_text_iter_forward_to_line_end(&end
);
1140 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1141 result
= wxGTK_CONV_BACK(text
);
1146 result
= GetValue();
1151 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1153 /* If you implement this, don't forget to update the documentation!
1154 * (file docs/latex/wx/text.tex) */
1155 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1158 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1160 if ( IsMultiLine() )
1164 if (pos
> GetLastPosition())
1167 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1170 *y
= gtk_text_iter_get_line(&iter
);
1172 *x
= gtk_text_iter_get_line_offset(&iter
);
1174 else // single line control
1176 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1185 // index out of bounds
1193 long wxTextCtrl::XYToPosition(long x
, long y
) const
1195 if ( IsSingleLine() )
1199 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1202 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1203 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1206 return gtk_text_iter_get_offset(&iter
) + x
;
1209 int wxTextCtrl::GetLineLength(long lineNo
) const
1211 if ( IsMultiLine() )
1213 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1214 if (lineNo
> last_line
)
1218 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1219 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1220 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1224 wxString str
= GetLineText (lineNo
);
1225 return (int) str
.length();
1229 int wxTextCtrl::GetNumberOfLines() const
1231 if ( IsMultiLine() )
1233 return gtk_text_buffer_get_line_count( m_buffer
);
1241 void wxTextCtrl::SetInsertionPoint( long pos
)
1243 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1245 if ( IsMultiLine() )
1248 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1249 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1250 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1252 // defer until Thaw, text view is not using m_buffer now
1253 m_showPositionOnThaw
= mark
;
1255 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1259 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1260 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1264 void wxTextCtrl::SetInsertionPointEnd()
1266 SetInsertionPoint(-1);
1269 void wxTextCtrl::SetEditable( bool editable
)
1271 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1273 if ( IsMultiLine() )
1275 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1279 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1283 bool wxTextCtrl::Enable( bool enable
)
1285 if (!wxWindowBase::Enable(enable
))
1291 if ( IsMultiLine() )
1293 SetEditable( enable
);
1297 gtk_widget_set_sensitive( m_text
, enable
);
1303 // wxGTK-specific: called recursively by Enable,
1304 // to give widgets an opportunity to correct their colours after they
1305 // have been changed by Enable
1306 void wxTextCtrl::OnEnabled( bool enable
)
1308 // If we have a custom background colour, we use this colour in both
1309 // disabled and enabled mode, or we end up with a different colour under the
1311 wxColour oldColour
= GetBackgroundColour();
1314 // Need to set twice or it'll optimize the useful stuff out
1315 if (oldColour
== * wxWHITE
)
1316 SetBackgroundColour(*wxBLACK
);
1318 SetBackgroundColour(*wxWHITE
);
1319 SetBackgroundColour(oldColour
);
1323 void wxTextCtrl::MarkDirty()
1328 void wxTextCtrl::DiscardEdits()
1333 // ----------------------------------------------------------------------------
1334 // max text length support
1335 // ----------------------------------------------------------------------------
1337 bool wxTextCtrl::IgnoreTextUpdate()
1339 if ( m_countUpdatesToIgnore
> 0 )
1341 m_countUpdatesToIgnore
--;
1349 bool wxTextCtrl::MarkDirtyOnChange()
1351 if ( m_dontMarkDirty
)
1353 m_dontMarkDirty
= false;
1361 void wxTextCtrl::SetMaxLength(unsigned long len
)
1363 if ( !HasFlag(wxTE_MULTILINE
) )
1365 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1367 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1368 // we had tried to enter more text than allowed by max text length and
1369 // the text wasn't really changed
1371 // to detect this and generate TEXT_MAXLEN event instead of
1372 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1374 // when max len is set to 0 we disconnect our handler as it means that
1375 // we shouldn't check anything any more
1378 g_signal_connect (m_text
, "insert_text",
1379 G_CALLBACK (gtk_insert_text_callback
), this);
1383 g_signal_handlers_disconnect_by_func (m_text
,
1384 (gpointer
) gtk_insert_text_callback
, this);
1389 void wxTextCtrl::SetSelection( long from
, long to
)
1391 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1393 if (from
== -1 && to
== -1)
1396 to
= GetValue().length();
1399 if ( IsMultiLine() )
1401 GtkTextIter fromi
, toi
;
1402 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1403 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1405 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1406 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1410 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1414 void wxTextCtrl::ShowPosition( long pos
)
1419 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1420 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1421 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1423 // defer until Thaw, text view is not using m_buffer now
1424 m_showPositionOnThaw
= mark
;
1426 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1430 wxTextCtrlHitTestResult
1431 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1433 if ( !IsMultiLine() )
1436 return wxTE_HT_UNKNOWN
;
1440 gtk_text_view_window_to_buffer_coords
1442 GTK_TEXT_VIEW(m_text
),
1443 GTK_TEXT_WINDOW_TEXT
,
1449 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1451 *pos
= gtk_text_iter_get_offset(&iter
);
1453 return wxTE_HT_ON_TEXT
;
1456 long wxTextCtrl::GetInsertionPoint() const
1458 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1460 if ( IsMultiLine() )
1462 // There is no direct accessor for the cursor, but
1463 // internally, the cursor is the "mark" called
1464 // "insert" in the text view's btree structure.
1466 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1468 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1470 return gtk_text_iter_get_offset( &cursor
);
1474 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1478 wxTextPos
wxTextCtrl::GetLastPosition() const
1480 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1484 if ( IsMultiLine() )
1487 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1489 pos
= gtk_text_iter_get_offset( &end
);
1493 pos
= GTK_ENTRY(m_text
)->text_length
;
1499 void wxTextCtrl::Remove( long from
, long to
)
1501 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1503 if ( IsMultiLine() )
1505 GtkTextIter fromi
, toi
;
1506 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1507 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1509 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1512 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1515 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1517 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1523 SetInsertionPoint( from
);
1528 void wxTextCtrl::Cut()
1530 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1532 if ( IsMultiLine() )
1533 g_signal_emit_by_name (m_text
, "cut-clipboard");
1535 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1538 void wxTextCtrl::Copy()
1540 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1542 if ( IsMultiLine() )
1543 g_signal_emit_by_name (m_text
, "copy-clipboard");
1545 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1548 void wxTextCtrl::Paste()
1550 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1552 if ( IsMultiLine() )
1553 g_signal_emit_by_name (m_text
, "paste-clipboard");
1555 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1559 void wxTextCtrl::Undo()
1562 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1565 void wxTextCtrl::Redo()
1568 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1571 bool wxTextCtrl::CanUndo() const
1574 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1578 bool wxTextCtrl::CanRedo() const
1581 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1585 // If the return values from and to are the same, there is no
1587 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1589 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1593 bool haveSelection
= false;
1595 if ( IsMultiLine() )
1597 GtkTextIter ifrom
, ito
;
1598 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1600 haveSelection
= true;
1601 from
= gtk_text_iter_get_offset(&ifrom
);
1602 to
= gtk_text_iter_get_offset(&ito
);
1605 else // not multi-line
1607 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1610 haveSelection
= true;
1614 if (! haveSelection
)
1615 from
= to
= GetInsertionPoint();
1619 // exchange them to be compatible with wxMSW
1632 bool wxTextCtrl::IsEditable() const
1634 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1636 if ( IsMultiLine() )
1638 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1642 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1646 bool wxTextCtrl::IsModified() const
1651 void wxTextCtrl::Clear()
1653 SetValue( wxEmptyString
);
1656 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1658 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1660 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1662 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1664 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1665 event
.SetEventObject(this);
1666 event
.SetString(GetValue());
1667 if ( GetEventHandler()->ProcessEvent(event
) )
1671 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1673 if ( IsSingleLine() )
1675 // This will invoke the dialog default action, such
1676 // as the clicking the default button.
1678 wxWindow
*top_frame
= m_parent
;
1679 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1680 top_frame
= top_frame
->GetParent();
1682 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1684 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1686 if (window
->default_widget
)
1688 gtk_widget_activate (window
->default_widget
);
1698 GtkWidget
* wxTextCtrl::GetConnectWidget()
1700 return GTK_WIDGET(m_text
);
1703 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1705 if ( IsMultiLine() )
1707 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1708 GTK_TEXT_WINDOW_TEXT
);
1712 return GTK_ENTRY(m_text
)->text_area
;
1716 // the font will change for subsequent text insertiongs
1717 bool wxTextCtrl::SetFont( const wxFont
&font
)
1719 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1721 if ( !wxTextCtrlBase::SetFont(font
) )
1723 // font didn't change, nothing to do
1727 if ( IsMultiLine() )
1729 SetUpdateFont(true);
1731 m_defaultStyle
.SetFont(font
);
1733 ChangeFontGlobally();
1739 void wxTextCtrl::ChangeFontGlobally()
1741 // this method is very inefficient and hence should be called as rarely as
1744 // TODO: it can be implemented much more efficiently for GTK2
1745 wxASSERT_MSG( IsMultiLine(),
1746 _T("shouldn't be called for single line controls") );
1748 wxString value
= GetValue();
1749 if ( !value
.empty() )
1751 SetUpdateFont(false);
1758 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1760 if ( !wxControl::SetForegroundColour(colour
) )
1763 // update default fg colour too
1764 m_defaultStyle
.SetTextColour(colour
);
1769 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1771 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1773 if ( !wxControl::SetBackgroundColour( colour
) )
1776 if (!m_backgroundColour
.Ok())
1779 // change active background color too
1780 m_defaultStyle
.SetBackgroundColour( colour
);
1785 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1787 if ( IsMultiLine() )
1789 if ( style
.IsDefault() )
1795 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1797 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1798 _T("invalid range in wxTextCtrl::SetStyle") );
1800 GtkTextIter starti
, endi
;
1801 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1802 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1804 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1808 //else: single line text controls don't support styles
1813 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1815 gtk_widget_modify_style(m_text
, style
);
1818 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1823 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1828 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1833 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1838 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1843 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1845 event
.Enable( CanCut() );
1848 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1850 event
.Enable( CanCopy() );
1853 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1855 event
.Enable( CanPaste() );
1858 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1860 event
.Enable( CanUndo() );
1863 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1865 event
.Enable( CanRedo() );
1868 wxSize
wxTextCtrl::DoGetBestSize() const
1870 // FIXME should be different for multi-line controls...
1871 wxSize
ret( wxControl::DoGetBestSize() );
1872 wxSize
best(80, ret
.y
);
1873 CacheBestSize(best
);
1877 // ----------------------------------------------------------------------------
1879 // ----------------------------------------------------------------------------
1881 void wxTextCtrl::Freeze()
1883 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1885 if ( HasFlag(wxTE_MULTILINE
) )
1887 if (m_freezeCount
++ == 0)
1889 // freeze textview updates and remove buffer
1890 g_signal_connect (m_text
, "expose_event",
1891 G_CALLBACK (gtk_text_exposed_callback
), this);
1892 g_signal_connect (m_widget
, "expose_event",
1893 G_CALLBACK (gtk_text_exposed_callback
), this);
1894 gtk_widget_set_sensitive(m_widget
, false);
1895 g_object_ref(m_buffer
);
1896 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1897 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1898 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1899 // gtk_text_view_set_buffer adds its own reference
1900 g_object_unref(buf_new
);
1901 // This mark should be deleted when the buffer is changed,
1902 // but it's not (in GTK+ up to at least 2.10.6).
1903 // Otherwise these anonymous marks start to build up in the buffer,
1904 // and Freeze takes longer and longer each time it is called.
1905 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1906 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1911 void wxTextCtrl::Thaw()
1913 if ( HasFlag(wxTE_MULTILINE
) )
1915 wxCHECK_RET(m_freezeCount
!= 0, _T("Thaw() without matching Freeze()"));
1917 if (--m_freezeCount
== 0)
1919 // Reattach buffer and thaw textview updates
1920 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1921 g_object_unref(m_buffer
);
1922 gtk_widget_set_sensitive(m_widget
, true);
1923 g_signal_handlers_disconnect_by_func (m_widget
,
1924 (gpointer
) gtk_text_exposed_callback
, this);
1925 g_signal_handlers_disconnect_by_func (m_text
,
1926 (gpointer
) gtk_text_exposed_callback
, this);
1927 if (m_showPositionOnThaw
!= NULL
)
1929 gtk_text_view_scroll_mark_onscreen(
1930 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1931 m_showPositionOnThaw
= NULL
;
1937 // ----------------------------------------------------------------------------
1938 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1939 // ----------------------------------------------------------------------------
1941 // FIXME: when dragging on a link the sample gets an "Unknown event".
1942 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1943 // a buggy sample, or something else
1944 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1947 if( !HasFlag(wxTE_AUTO_URL
) )
1951 GtkTextIter start
, end
;
1952 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1955 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1956 event
.GetX(), event
.GetY(), &x
, &y
);
1958 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1959 if (!gtk_text_iter_has_tag(&end
, tag
))
1961 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1962 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1966 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1967 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1970 if(!gtk_text_iter_begins_tag(&start
, tag
))
1971 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1972 if(!gtk_text_iter_ends_tag(&end
, tag
))
1973 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1975 // Native context menu is probably not desired on an URL.
1976 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1977 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1980 wxTextUrlEvent
url_event(m_windowId
, event
,
1981 gtk_text_iter_get_offset(&start
),
1982 gtk_text_iter_get_offset(&end
));
1984 InitCommandEvent(url_event
);
1985 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1986 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1987 GetEventHandler()->ProcessEvent(url_event
);
1990 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1992 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1994 // GtkTextView starts a drag operation when left mouse button is pressed
1995 // and ends it when it is released and if it doesn't get the release event
1996 // the next click on a control results in an assertion failure inside
1997 // gtk_text_view_start_selection_drag() which simply *kills* the program
1998 // without anything we can do about it, so always let GTK+ have this event
1999 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
2004 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2006 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);