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");
55 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
56 GtkTextBuffer
*text_buffer
,
57 const wxTextAttr
& attr
,
61 static gchar buf
[1024];
64 gulong remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
65 G_CALLBACK (wxGtkOnRemoveTag
), gpointer("WX"));
66 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
67 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
71 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
72 wxGtkString
font_string(pango_font_description_to_string(font_description
));
73 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
74 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
77 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
78 "font-desc", font_description
,
80 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
82 if (attr
.GetFont().GetUnderlined())
84 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
85 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
88 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
89 "underline-set", TRUE
,
90 "underline", PANGO_UNDERLINE_SINGLE
,
92 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
96 if (attr
.HasTextColour())
98 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
99 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
100 colFg
->red
, colFg
->green
, colFg
->blue
);
101 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
104 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
105 "foreground-gdk", colFg
, NULL
);
106 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
109 if (attr
.HasBackgroundColour())
111 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
112 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
113 colBg
->red
, colBg
->green
, colBg
->blue
);
114 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
117 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
118 "background-gdk", colBg
, NULL
);
119 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
122 if (attr
.HasAlignment())
124 GtkTextIter para_start
, para_end
= *end
;
125 gtk_text_buffer_get_iter_at_line( text_buffer
,
127 gtk_text_iter_get_line(start
) );
128 gtk_text_iter_forward_line(¶_end
);
130 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
131 G_CALLBACK(wxGtkOnRemoveTag
),
132 gpointer("WXALIGNMENT"));
133 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
134 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
136 GtkJustification align
;
137 switch (attr
.GetAlignment())
140 align
= GTK_JUSTIFY_LEFT
;
142 case wxTEXT_ALIGNMENT_RIGHT
:
143 align
= GTK_JUSTIFY_RIGHT
;
145 case wxTEXT_ALIGNMENT_CENTER
:
146 align
= GTK_JUSTIFY_CENTER
;
148 // gtk+ doesn't support justify as of gtk+-2.7.4
151 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
152 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
155 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
156 "justification", align
, NULL
);
157 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
160 if (attr
.HasLeftIndent())
162 // Indentation attribute
164 // Clear old indentation tags
165 GtkTextIter para_start
, para_end
= *end
;
166 gtk_text_buffer_get_iter_at_line( text_buffer
,
168 gtk_text_iter_get_line(start
) );
169 gtk_text_iter_forward_line(¶_end
);
171 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
172 G_CALLBACK(wxGtkOnRemoveTag
),
173 gpointer("WXINDENT"));
174 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
175 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
177 // Convert indent from 1/10th of a mm into pixels
179 #if GTK_CHECK_VERSION(2,2,0)
180 if (!gtk_check_version(2,2,0))
181 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
182 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
185 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
187 const int indent
= (int)(factor
* attr
.GetLeftIndent());
188 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
196 gsubindent
= -subIndent
;
200 gindent
= -subIndent
;
204 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
205 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
208 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
209 "left-margin", gindent
, "indent", gsubindent
, NULL
);
210 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
218 GtkTextIter para_start
, para_end
= *end
;
219 gtk_text_buffer_get_iter_at_line( text_buffer
,
221 gtk_text_iter_get_line(start
) );
222 gtk_text_iter_forward_line(¶_end
);
224 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
225 G_CALLBACK(wxGtkOnRemoveTag
),
227 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
228 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
230 const wxArrayInt
& tabs
= attr
.GetTabs();
232 wxString tagname
= _T("WXTABS");
233 g_snprintf(buf
, sizeof(buf
), "WXTABS");
234 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
235 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
237 const wxWX2MBbuf buf
= tagname
.mb_str(wxConvUTF8
);
239 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
243 // Factor to convert from 1/10th of a mm into pixels
245 #if GTK_CHECK_VERSION(2,2,0)
246 if (!gtk_check_version(2,2,0))
247 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
248 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
251 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
253 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
254 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
255 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
256 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
257 "tabs", tabArray
, NULL
);
258 pango_tab_array_free(tabArray
);
260 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
266 static void wxGtkTextInsert(GtkWidget
*text
,
267 GtkTextBuffer
*text_buffer
,
268 const wxTextAttr
& attr
,
269 const wxCharBuffer
& buffer
)
273 GtkTextIter iter
, start
;
275 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
276 gtk_text_buffer_get_insert (text_buffer
) );
277 start_offset
= gtk_text_iter_get_offset (&iter
);
278 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
280 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
282 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
286 // ----------------------------------------------------------------------------
287 // "insert_text" for GtkEntry
288 // ----------------------------------------------------------------------------
292 gtk_insert_text_callback(GtkEditable
*editable
,
293 const gchar
*new_text
,
294 gint new_text_length
,
299 wxapp_install_idle_handler();
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;
548 wxapp_install_idle_handler();
550 if ( win
->MarkDirtyOnChange() )
553 win
->SendTextUpdatedEvent();
557 //-----------------------------------------------------------------------------
558 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
559 //-----------------------------------------------------------------------------
561 // common part of the event handlers below
563 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
564 wxEventType eventType
, const gchar
* signal_name
)
566 wxClipboardTextEvent
event( eventType
, win
->GetId() );
567 event
.SetEventObject( win
);
568 if ( win
->GetEventHandler()->ProcessEvent( event
) )
570 // don't let the default processing to take place if we did something
571 // ourselves in the event handler
572 g_signal_stop_emission_by_name (widget
, signal_name
);
578 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
580 handle_text_clipboard_callback(
581 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
585 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
587 handle_text_clipboard_callback(
588 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
592 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
594 handle_text_clipboard_callback(
595 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
599 //-----------------------------------------------------------------------------
600 // "expose_event" from scrolled window and textview
601 //-----------------------------------------------------------------------------
605 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
612 //-----------------------------------------------------------------------------
614 //-----------------------------------------------------------------------------
616 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
618 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
619 EVT_CHAR(wxTextCtrl::OnChar
)
621 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
622 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
623 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
624 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
625 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
627 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
628 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
629 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
630 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
631 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
633 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
634 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
635 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
636 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
637 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
638 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
639 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
640 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
641 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
644 void wxTextCtrl::Init()
649 m_countUpdatesToIgnore
= 0;
651 SetUpdateFont(false);
655 m_gdkHandCursor
= NULL
;
656 m_gdkXTermCursor
= NULL
;
659 wxTextCtrl::~wxTextCtrl()
662 gdk_cursor_unref(m_gdkHandCursor
);
664 gdk_cursor_unref(m_gdkXTermCursor
);
667 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
669 const wxString
&value
,
673 const wxValidator
& validator
,
674 const wxString
&name
)
678 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
681 bool wxTextCtrl::Create( wxWindow
*parent
,
683 const wxString
&value
,
687 const wxValidator
& validator
,
688 const wxString
&name
)
691 m_acceptsFocus
= true;
693 if (!PreCreation( parent
, pos
, size
) ||
694 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
696 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
700 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
705 m_text
= gtk_text_view_new();
707 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
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
, GTK_POLICY_AUTOMATIC
);
713 // for ScrollLines/Pages
714 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
716 // Insert view into scrolled window
717 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
719 // translate wx wrapping style to GTK+
721 if ( HasFlag( wxTE_DONTWRAP
) )
722 wrap
= GTK_WRAP_NONE
;
723 else if ( HasFlag( wxTE_CHARWRAP
) )
724 wrap
= GTK_WRAP_CHAR
;
725 else if ( HasFlag( wxTE_WORDWRAP
) )
726 wrap
= GTK_WRAP_WORD
;
727 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
729 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
731 if ( !gtk_check_version(2,4,0) )
733 wrap
= GTK_WRAP_WORD_CHAR
;
737 wrap
= GTK_WRAP_WORD
;
740 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
742 GtkScrolledWindowSetBorder(m_widget
, style
);
744 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
746 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
750 // a single-line text control: no need for scrollbars
752 m_text
= gtk_entry_new();
754 if (style
& wxNO_BORDER
)
755 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
758 m_parent
->DoAddChild( this );
760 m_focusWidget
= m_text
;
766 gtk_widget_show(m_text
);
774 if (style
& wxTE_PASSWORD
)
777 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
780 if (style
& wxTE_READONLY
)
783 gtk_editable_set_editable( GTK_EDITABLE(m_text
), FALSE
);
785 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
790 if (style
& wxTE_RIGHT
)
791 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_RIGHT
);
792 else if (style
& wxTE_CENTRE
)
793 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_CENTER
);
794 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
799 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
800 if (!gtk_check_version(2,4,0))
802 if (style
& wxTE_RIGHT
)
803 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 1.0 );
804 else if (style
& wxTE_CENTRE
)
805 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 0.5 );
810 // We want to be notified about text changes.
813 g_signal_connect (m_buffer
, "changed",
814 G_CALLBACK (gtk_text_changed_callback
), this);
816 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
817 if (style
& wxTE_AUTO_URL
)
819 GtkTextIter start
, end
;
820 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
821 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
823 // We create our wxUrl tag here for slight efficiency gain - we
824 // don't have to check for the tag existance in callbacks,
825 // hereby it's guaranteed to exist.
826 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
827 "foreground", "blue",
828 "underline", PANGO_UNDERLINE_SINGLE
,
831 // Check for URLs after each text change
832 g_signal_connect_after (m_buffer
, "insert_text",
833 G_CALLBACK (au_insert_text_callback
), this);
834 g_signal_connect_after (m_buffer
, "delete_range",
835 G_CALLBACK (au_delete_range_callback
), this);
837 // Block all wxUrl tag applying unless we do it ourselves, in which case we
838 // block this callback temporarily. This takes care of gtk+ internal
839 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
840 // which is undesired because only a part of the URL might be copied.
841 // The insert-text signal emitted inside it will take care of newly formed
842 // or wholly copied URLs.
843 g_signal_connect (m_buffer
, "apply_tag",
844 G_CALLBACK (au_apply_tag_callback
), NULL
);
846 // Check for URLs in the initial string passed to Create
847 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
848 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
849 au_check_range(&start
, &end
);
854 g_signal_connect (m_text
, "changed",
855 G_CALLBACK (gtk_text_changed_callback
), this);
858 g_signal_connect (m_text
, "copy-clipboard",
859 G_CALLBACK (gtk_copy_clipboard_callback
), this);
860 g_signal_connect (m_text
, "cut-clipboard",
861 G_CALLBACK (gtk_cut_clipboard_callback
), this);
862 g_signal_connect (m_text
, "paste-clipboard",
863 G_CALLBACK (gtk_paste_clipboard_callback
), this);
865 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
867 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
868 SetDefaultStyle( attrDef
);
874 void wxTextCtrl::CalculateScrollbar()
878 wxString
wxTextCtrl::GetValue() const
880 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
886 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
888 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
889 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
891 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
897 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
898 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
906 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
908 // GTK+ uses UTF-8 internally, we need to convert to it but from which
911 // first check the default text style (we intentionally don't check the
912 // style for the current position as it doesn't make sense for SetValue())
913 const wxTextAttr
& style
= GetDefaultStyle();
914 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
915 : wxFONTENCODING_SYSTEM
;
917 // fall back to the controls font if no style
918 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
919 enc
= GetFont().GetEncoding();
924 bool wxTextCtrl::IsEmpty() const
927 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
929 return wxTextCtrlBase::IsEmpty();
932 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
934 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
936 // the control won't be modified any more as we programmatically replace
937 // all the existing text, so reset the flag and don't set it again (and do
938 // it now, before the text event handler is ran so that IsModified() called
939 // from there returns the expected value)
941 DontMarkDirtyOnNextChange();
943 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, GetTextEncoding()));
946 // see comment in WriteText() as to why we must warn the user about
948 wxLogWarning(_("Failed to set text in the text control."));
952 // if the control is not empty, two "changed" signals are emitted,
953 // otherwise only one and we need to ignore either both or one of them
954 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
959 IgnoreNextTextUpdate(ignore
);
963 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
967 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
970 // if, for whatever reason, the callback wasn't called the expected number
971 // of times, still reset the flags to the default values
972 m_dontMarkDirty
= false;
973 m_countUpdatesToIgnore
= 0;
976 // GRG, Jun/2000: Changed this after a lot of discussion in
977 // the lists. wxWidgets 2.2 will have a set of flags to
978 // customize this behaviour.
979 SetInsertionPoint(0);
982 void wxTextCtrl::WriteText( const wxString
&text
)
984 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
989 // check if we have a specific style for the current position
990 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
992 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
994 enc
= style
.GetFont().GetEncoding();
997 if ( enc
== wxFONTENCODING_SYSTEM
)
998 enc
= GetTextEncoding();
1000 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1003 // we must log an error here as losing the text like this can be a
1004 // serious problem (e.g. imagine the document edited by user being
1005 // empty instead of containing the correct text)
1006 wxLogWarning(_("Failed to insert text in the control."));
1010 // we're changing the text programmatically
1011 DontMarkDirtyOnNextChange();
1013 if ( IsMultiLine() )
1015 // First remove the selection if there is one
1016 // TODO: Is there an easier GTK specific way to do this?
1018 GetSelection(&from
, &to
);
1023 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1025 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1026 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1027 if ( wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
) )
1029 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1030 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1035 // First remove the selection if there is one
1036 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1038 // This moves the cursor pos to behind the inserted text.
1039 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1041 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1043 // Bring entry's cursor uptodate.
1044 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1048 void wxTextCtrl::AppendText( const wxString
&text
)
1050 SetInsertionPointEnd();
1054 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1057 if ( IsMultiLine() )
1060 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1061 GtkTextIter end
= line
;
1062 gtk_text_iter_forward_to_line_end(&end
);
1063 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1064 result
= wxGTK_CONV_BACK(text
);
1069 result
= GetValue();
1074 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1076 /* If you implement this, don't forget to update the documentation!
1077 * (file docs/latex/wx/text.tex) */
1078 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1081 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1083 if ( IsMultiLine() )
1087 if (pos
> GetLastPosition())
1090 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1093 *y
= gtk_text_iter_get_line(&iter
);
1095 *x
= gtk_text_iter_get_line_offset(&iter
);
1097 else // single line control
1099 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1108 // index out of bounds
1116 long wxTextCtrl::XYToPosition(long x
, long y
) const
1118 if ( IsSingleLine() )
1122 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1125 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1126 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1129 return gtk_text_iter_get_offset(&iter
) + x
;
1132 int wxTextCtrl::GetLineLength(long lineNo
) const
1134 if ( IsMultiLine() )
1136 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1137 if (lineNo
> last_line
)
1141 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1142 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1143 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1147 wxString str
= GetLineText (lineNo
);
1148 return (int) str
.length();
1152 int wxTextCtrl::GetNumberOfLines() const
1154 if ( IsMultiLine() )
1157 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, 0 );
1159 // move forward by one display line until the end is reached
1161 while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text
), &iter
) )
1166 // If the last character in the text buffer is a newline,
1167 // gtk_text_view_forward_display_line() will return false without that
1168 // line being counted. Must add one manually in that case.
1169 GtkTextIter lastCharIter
;
1170 gtk_text_buffer_get_iter_at_offset
1174 gtk_text_buffer_get_char_count(m_buffer
) - 1
1176 gchar lastChar
= gtk_text_iter_get_char( &lastCharIter
);
1177 if ( lastChar
== wxT('\n') )
1188 void wxTextCtrl::SetInsertionPoint( long pos
)
1190 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1192 if ( IsMultiLine() )
1195 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1196 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1197 gtk_text_view_scroll_mark_onscreen
1199 GTK_TEXT_VIEW(m_text
),
1200 gtk_text_buffer_get_insert( m_buffer
)
1205 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1206 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1210 void wxTextCtrl::SetInsertionPointEnd()
1212 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1214 if ( IsMultiLine() )
1217 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1218 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1222 gtk_editable_set_position( GTK_EDITABLE(m_text
), -1 );
1226 void wxTextCtrl::SetEditable( bool editable
)
1228 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1230 if ( IsMultiLine() )
1232 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1236 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1240 bool wxTextCtrl::Enable( bool enable
)
1242 if (!wxWindowBase::Enable(enable
))
1248 if ( IsMultiLine() )
1250 SetEditable( enable
);
1254 gtk_widget_set_sensitive( m_text
, enable
);
1260 // wxGTK-specific: called recursively by Enable,
1261 // to give widgets an oppprtunity to correct their colours after they
1262 // have been changed by Enable
1263 void wxTextCtrl::OnParentEnable( bool enable
)
1265 // If we have a custom background colour, we use this colour in both
1266 // disabled and enabled mode, or we end up with a different colour under the
1268 wxColour oldColour
= GetBackgroundColour();
1271 // Need to set twice or it'll optimize the useful stuff out
1272 if (oldColour
== * wxWHITE
)
1273 SetBackgroundColour(*wxBLACK
);
1275 SetBackgroundColour(*wxWHITE
);
1276 SetBackgroundColour(oldColour
);
1280 void wxTextCtrl::MarkDirty()
1285 void wxTextCtrl::DiscardEdits()
1290 // ----------------------------------------------------------------------------
1291 // max text length support
1292 // ----------------------------------------------------------------------------
1294 bool wxTextCtrl::IgnoreTextUpdate()
1296 if ( m_countUpdatesToIgnore
> 0 )
1298 m_countUpdatesToIgnore
--;
1306 bool wxTextCtrl::MarkDirtyOnChange()
1308 if ( m_dontMarkDirty
)
1310 m_dontMarkDirty
= false;
1318 void wxTextCtrl::SetMaxLength(unsigned long len
)
1320 if ( !HasFlag(wxTE_MULTILINE
) )
1322 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1324 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1325 // we had tried to enter more text than allowed by max text length and
1326 // the text wasn't really changed
1328 // to detect this and generate TEXT_MAXLEN event instead of
1329 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1331 // when max len is set to 0 we disconnect our handler as it means that
1332 // we shouldn't check anything any more
1335 g_signal_connect (m_text
, "insert_text",
1336 G_CALLBACK (gtk_insert_text_callback
), this);
1340 g_signal_handlers_disconnect_by_func (m_text
,
1341 (gpointer
) gtk_insert_text_callback
, this);
1346 void wxTextCtrl::SetSelection( long from
, long to
)
1348 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1350 if (from
== -1 && to
== -1)
1353 to
= GetValue().length();
1356 if ( IsMultiLine() )
1358 GtkTextIter fromi
, toi
;
1359 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1360 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1362 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1363 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1367 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1371 void wxTextCtrl::ShowPosition( long pos
)
1373 if ( IsMultiLine() )
1376 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1377 gtk_text_iter_set_offset( &iter
, pos
);
1378 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1379 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1383 wxTextCtrlHitTestResult
1384 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1386 if ( !IsMultiLine() )
1389 return wxTE_HT_UNKNOWN
;
1393 gtk_text_view_window_to_buffer_coords
1395 GTK_TEXT_VIEW(m_text
),
1396 GTK_TEXT_WINDOW_TEXT
,
1402 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1404 *pos
= gtk_text_iter_get_offset(&iter
);
1406 return wxTE_HT_ON_TEXT
;
1409 long wxTextCtrl::GetInsertionPoint() const
1411 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1413 if ( IsMultiLine() )
1415 // There is no direct accessor for the cursor, but
1416 // internally, the cursor is the "mark" called
1417 // "insert" in the text view's btree structure.
1419 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1421 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1423 return gtk_text_iter_get_offset( &cursor
);
1427 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1431 wxTextPos
wxTextCtrl::GetLastPosition() const
1433 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1437 if ( IsMultiLine() )
1440 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1442 pos
= gtk_text_iter_get_offset( &end
);
1446 pos
= GTK_ENTRY(m_text
)->text_length
;
1452 void wxTextCtrl::Remove( long from
, long to
)
1454 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1456 if ( IsMultiLine() )
1458 GtkTextIter fromi
, toi
;
1459 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1460 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1462 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1465 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1468 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1470 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1476 SetInsertionPoint( from
);
1481 void wxTextCtrl::Cut()
1483 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1485 if ( IsMultiLine() )
1486 g_signal_emit_by_name (m_text
, "cut-clipboard");
1488 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1491 void wxTextCtrl::Copy()
1493 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1495 if ( IsMultiLine() )
1496 g_signal_emit_by_name (m_text
, "copy-clipboard");
1498 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1501 void wxTextCtrl::Paste()
1503 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1505 if ( IsMultiLine() )
1506 g_signal_emit_by_name (m_text
, "paste-clipboard");
1508 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1512 void wxTextCtrl::Undo()
1515 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1518 void wxTextCtrl::Redo()
1521 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1524 bool wxTextCtrl::CanUndo() const
1527 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1531 bool wxTextCtrl::CanRedo() const
1534 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1538 // If the return values from and to are the same, there is no
1540 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1542 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1546 bool haveSelection
= false;
1548 if ( IsMultiLine() )
1550 GtkTextIter ifrom
, ito
;
1551 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1553 haveSelection
= true;
1554 from
= gtk_text_iter_get_offset(&ifrom
);
1555 to
= gtk_text_iter_get_offset(&ito
);
1558 else // not multi-line
1560 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1563 haveSelection
= true;
1567 if (! haveSelection
)
1568 from
= to
= GetInsertionPoint();
1572 // exchange them to be compatible with wxMSW
1585 bool wxTextCtrl::IsEditable() const
1587 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1589 if ( IsMultiLine() )
1591 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1595 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1599 bool wxTextCtrl::IsModified() const
1604 void wxTextCtrl::Clear()
1606 SetValue( wxEmptyString
);
1609 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1611 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1613 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1615 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1617 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1618 event
.SetEventObject(this);
1619 event
.SetString(GetValue());
1620 if ( GetEventHandler()->ProcessEvent(event
) )
1624 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1626 if ( IsSingleLine() )
1628 // This will invoke the dialog default action, such
1629 // as the clicking the default button.
1631 wxWindow
*top_frame
= m_parent
;
1632 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1633 top_frame
= top_frame
->GetParent();
1635 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1637 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1639 if (window
->default_widget
)
1641 gtk_widget_activate (window
->default_widget
);
1651 GtkWidget
* wxTextCtrl::GetConnectWidget()
1653 return GTK_WIDGET(m_text
);
1656 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1658 if ( IsMultiLine() )
1660 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1661 GTK_TEXT_WINDOW_TEXT
);
1665 return GTK_ENTRY(m_text
)->text_area
;
1669 // the font will change for subsequent text insertiongs
1670 bool wxTextCtrl::SetFont( const wxFont
&font
)
1672 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1674 if ( !wxTextCtrlBase::SetFont(font
) )
1676 // font didn't change, nothing to do
1680 if ( IsMultiLine() )
1682 SetUpdateFont(true);
1684 m_defaultStyle
.SetFont(font
);
1686 ChangeFontGlobally();
1692 void wxTextCtrl::ChangeFontGlobally()
1694 // this method is very inefficient and hence should be called as rarely as
1697 // TODO: it can be implemented much more efficiently for GTK2
1698 wxASSERT_MSG( IsMultiLine(),
1699 _T("shouldn't be called for single line controls") );
1701 wxString value
= GetValue();
1702 if ( !value
.empty() )
1704 SetUpdateFont(false);
1711 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1713 if ( !wxControl::SetForegroundColour(colour
) )
1716 // update default fg colour too
1717 m_defaultStyle
.SetTextColour(colour
);
1722 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1724 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1726 if ( !wxControl::SetBackgroundColour( colour
) )
1729 if (!m_backgroundColour
.Ok())
1732 // change active background color too
1733 m_defaultStyle
.SetBackgroundColour( colour
);
1738 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1740 if ( IsMultiLine() )
1742 if ( style
.IsDefault() )
1748 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1750 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1751 _T("invalid range in wxTextCtrl::SetStyle") );
1753 GtkTextIter starti
, endi
;
1754 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1755 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1757 // use the attributes from style which are set in it and fall back
1758 // first to the default style and then to the text control default
1759 // colours for the others
1760 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1762 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, attr
, &starti
, &endi
);
1768 // cannot do this for GTK+'s Entry widget
1772 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1774 gtk_widget_modify_style(m_text
, style
);
1777 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1782 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1787 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1792 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1797 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1802 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1804 event
.Enable( CanCut() );
1807 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1809 event
.Enable( CanCopy() );
1812 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1814 event
.Enable( CanPaste() );
1817 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1819 event
.Enable( CanUndo() );
1822 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1824 event
.Enable( CanRedo() );
1827 wxSize
wxTextCtrl::DoGetBestSize() const
1829 // FIXME should be different for multi-line controls...
1830 wxSize
ret( wxControl::DoGetBestSize() );
1831 wxSize
best(80, ret
.y
);
1832 CacheBestSize(best
);
1836 // ----------------------------------------------------------------------------
1838 // ----------------------------------------------------------------------------
1840 void wxTextCtrl::Freeze()
1842 if ( HasFlag(wxTE_MULTILINE
) )
1844 if ( !m_frozenness
++ )
1846 // freeze textview updates and remove buffer
1847 g_signal_connect (m_text
, "expose_event",
1848 G_CALLBACK (gtk_text_exposed_callback
), this);
1849 g_signal_connect (m_widget
, "expose_event",
1850 G_CALLBACK (gtk_text_exposed_callback
), this);
1851 gtk_widget_set_sensitive(m_widget
, false);
1852 g_object_ref(m_buffer
);
1853 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1854 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1855 // gtk_text_view_set_buffer adds its own reference
1856 g_object_unref(buf_new
);
1861 void wxTextCtrl::Thaw()
1863 if ( HasFlag(wxTE_MULTILINE
) )
1865 wxASSERT_MSG( m_frozenness
> 0, _T("Thaw() without matching Freeze()") );
1867 if ( !--m_frozenness
)
1869 // Reattach buffer and thaw textview updates
1870 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1871 g_object_unref(m_buffer
);
1872 gtk_widget_set_sensitive(m_widget
, true);
1873 g_signal_handlers_disconnect_by_func (m_widget
,
1874 (gpointer
) gtk_text_exposed_callback
, this);
1875 g_signal_handlers_disconnect_by_func (m_text
,
1876 (gpointer
) gtk_text_exposed_callback
, this);
1881 // ----------------------------------------------------------------------------
1882 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1883 // ----------------------------------------------------------------------------
1885 // FIXME: when dragging on a link the sample gets an "Unknown event".
1886 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1887 // a buggy sample, or something else
1888 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1891 if( !HasFlag(wxTE_AUTO_URL
) )
1895 GtkTextIter start
, end
;
1896 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1899 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1900 event
.GetX(), event
.GetY(), &x
, &y
);
1902 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1903 if (!gtk_text_iter_has_tag(&end
, tag
))
1905 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1906 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1910 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1911 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1914 if(!gtk_text_iter_begins_tag(&start
, tag
))
1915 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1916 if(!gtk_text_iter_ends_tag(&end
, tag
))
1917 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1919 // Native context menu is probably not desired on an URL.
1920 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1921 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1924 wxTextUrlEvent
url_event(m_windowId
, event
,
1925 gtk_text_iter_get_offset(&start
),
1926 gtk_text_iter_get_offset(&end
));
1928 InitCommandEvent(url_event
);
1929 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1930 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1931 GetEventHandler()->ProcessEvent(url_event
);
1934 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1936 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1938 // GtkTextView starts a drag operation when left mouse button is pressed
1939 // and ends it when it is released and if it doesn't get the release event
1940 // the next click on a control results in an assertion failure inside
1941 // gtk_text_view_start_selection_drag() which simply *kills* the program
1942 // without anything we can do about it, so always let GTK+ have this event
1943 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1948 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1950 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);