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"
20 #include "wx/settings.h"
24 #include "wx/strconv.h"
25 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #include <sys/types.h>
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
39 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
46 g_object_get (tag
, "name", &name
, NULL
);
48 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
49 // anonymous tag or not starting with prefix - don't remove
50 g_signal_stop_emission_by_name (buffer
, "remove_tag");
57 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
58 GtkTextBuffer
*text_buffer
,
59 const wxTextAttr
& attr
,
63 static gchar buf
[1024];
66 gulong remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
67 G_CALLBACK (wxGtkOnRemoveTag
), gpointer("WX"));
68 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
69 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
73 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
74 wxGtkString
font_string(pango_font_description_to_string(font_description
));
75 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
76 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
79 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
80 "font-desc", font_description
,
82 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
84 if (attr
.GetFont().GetUnderlined())
86 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
87 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
90 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
91 "underline-set", TRUE
,
92 "underline", PANGO_UNDERLINE_SINGLE
,
94 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
98 if (attr
.HasTextColour())
100 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
101 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
102 colFg
->red
, colFg
->green
, colFg
->blue
);
103 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
106 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
107 "foreground-gdk", colFg
, NULL
);
108 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
111 if (attr
.HasBackgroundColour())
113 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
114 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
115 colBg
->red
, colBg
->green
, colBg
->blue
);
116 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
119 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
120 "background-gdk", colBg
, NULL
);
121 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
124 if (attr
.HasAlignment())
126 GtkTextIter para_start
, para_end
= *end
;
127 gtk_text_buffer_get_iter_at_line( text_buffer
,
129 gtk_text_iter_get_line(start
) );
130 gtk_text_iter_forward_line(¶_end
);
132 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
133 G_CALLBACK(wxGtkOnRemoveTag
),
134 gpointer("WXALIGNMENT"));
135 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
136 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
138 GtkJustification align
;
139 switch (attr
.GetAlignment())
142 align
= GTK_JUSTIFY_LEFT
;
144 case wxTEXT_ALIGNMENT_RIGHT
:
145 align
= GTK_JUSTIFY_RIGHT
;
147 case wxTEXT_ALIGNMENT_CENTER
:
148 align
= GTK_JUSTIFY_CENTER
;
150 // gtk+ doesn't support justify as of gtk+-2.7.4
153 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
154 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
157 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
158 "justification", align
, NULL
);
159 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
162 if (attr
.HasLeftIndent())
164 // Indentation attribute
166 // Clear old indentation tags
167 GtkTextIter para_start
, para_end
= *end
;
168 gtk_text_buffer_get_iter_at_line( text_buffer
,
170 gtk_text_iter_get_line(start
) );
171 gtk_text_iter_forward_line(¶_end
);
173 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
174 G_CALLBACK(wxGtkOnRemoveTag
),
175 gpointer("WXINDENT"));
176 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
177 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
179 // Convert indent from 1/10th of a mm into pixels
181 #if GTK_CHECK_VERSION(2,2,0)
182 if (!gtk_check_version(2,2,0))
183 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
184 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
187 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
189 const int indent
= (int)(factor
* attr
.GetLeftIndent());
190 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
198 gsubindent
= -subIndent
;
202 gindent
= -subIndent
;
206 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
207 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
210 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
211 "left-margin", gindent
, "indent", gsubindent
, NULL
);
212 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
220 GtkTextIter para_start
, para_end
= *end
;
221 gtk_text_buffer_get_iter_at_line( text_buffer
,
223 gtk_text_iter_get_line(start
) );
224 gtk_text_iter_forward_line(¶_end
);
226 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
227 G_CALLBACK(wxGtkOnRemoveTag
),
229 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
230 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
232 const wxArrayInt
& tabs
= attr
.GetTabs();
234 wxString tagname
= _T("WXTABS");
235 g_snprintf(buf
, sizeof(buf
), "WXTABS");
236 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
237 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
239 const wxWX2MBbuf buf
= tagname
.mb_str(wxConvUTF8
);
241 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
245 // Factor to convert from 1/10th of a mm into pixels
247 #if GTK_CHECK_VERSION(2,2,0)
248 if (!gtk_check_version(2,2,0))
249 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
250 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
253 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
255 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
256 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
257 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
258 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
259 "tabs", tabArray
, NULL
);
260 pango_tab_array_free(tabArray
);
262 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
268 static void wxGtkTextInsert(GtkWidget
*text
,
269 GtkTextBuffer
*text_buffer
,
270 const wxTextAttr
& attr
,
271 const wxCharBuffer
& buffer
)
275 GtkTextIter iter
, start
;
277 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
278 gtk_text_buffer_get_insert (text_buffer
) );
279 start_offset
= gtk_text_iter_get_offset (&iter
);
280 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
282 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
284 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
288 // ----------------------------------------------------------------------------
289 // "insert_text" for GtkEntry
290 // ----------------------------------------------------------------------------
294 gtk_insert_text_callback(GtkEditable
*editable
,
295 const gchar
*new_text
,
296 gint new_text_length
,
301 wxapp_install_idle_handler();
303 // we should only be called if we have a max len limit at all
304 GtkEntry
*entry
= GTK_ENTRY (editable
);
306 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
308 // check that we don't overflow the max length limit
310 // FIXME: this doesn't work when we paste a string which is going to be
312 if ( entry
->text_length
== entry
->text_max_length
)
314 // we don't need to run the base class version at all
315 g_signal_stop_emission_by_name (editable
, "insert_text");
317 // remember that the next changed signal is to be ignored to avoid
318 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
319 win
->IgnoreNextTextUpdate();
321 // and generate the correct one ourselves
322 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
323 event
.SetEventObject(win
);
324 event
.SetString(win
->GetValue());
325 win
->GetEventHandler()->ProcessEvent( event
);
330 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
334 au_apply_tag_callback(GtkTextBuffer
*buffer
,
340 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
341 g_signal_stop_emission_by_name (buffer
, "apply_tag");
345 //-----------------------------------------------------------------------------
346 // GtkTextCharPredicates for gtk_text_iter_*_find_char
347 //-----------------------------------------------------------------------------
351 pred_whitespace (gunichar ch
, gpointer user_data
)
353 return g_unichar_isspace(ch
);
359 pred_non_whitespace (gunichar ch
, gpointer user_data
)
361 return !g_unichar_isspace(ch
);
367 pred_nonpunct (gunichar ch
, gpointer user_data
)
369 return !g_unichar_ispunct(ch
);
375 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
377 return !g_unichar_ispunct(ch
) || ch
== '/';
381 //-----------------------------------------------------------------------------
382 // Check for links between s and e and correct tags as necessary
383 //-----------------------------------------------------------------------------
385 // This function should be made match better while being efficient at one point.
386 // Most probably with a row of regular expressions.
389 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
391 static const char *URIPrefixes
[] =
409 GtkTextIter start
= *s
, end
= *e
;
410 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
412 // Get our special link tag
413 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
415 // Get rid of punctuation from beginning and end.
416 // Might want to move this to au_check_range if an improved link checking doesn't
417 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
418 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
419 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
421 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
422 gtk_text_iter_forward_char(&end
);
424 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
425 size_t len
= strlen(text
), prefix_len
;
428 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
430 prefix_len
= strlen(URIPrefixes
[n
]);
431 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
435 if(n
< WXSIZEOF(URIPrefixes
))
437 gulong signal_id
= g_signal_handler_find (buffer
,
438 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
440 (gpointer
)au_apply_tag_callback
, NULL
);
442 g_signal_handler_block (buffer
, signal_id
);
443 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
444 g_signal_handler_unblock (buffer
, signal_id
);
451 au_check_range(GtkTextIter
*s
,
452 GtkTextIter
*range_end
)
454 GtkTextIter range_start
= *s
;
455 GtkTextIter word_end
;
456 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
457 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
459 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
461 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
462 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
464 while(!gtk_text_iter_equal(&range_start
, range_end
))
466 word_end
= range_start
;
467 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
469 // Now we should have a word delimited by range_start and word_end, correct link tags
470 au_check_word(&range_start
, &word_end
);
472 range_start
= word_end
;
473 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
478 //-----------------------------------------------------------------------------
479 // "insert-text" for GtkTextBuffer
480 //-----------------------------------------------------------------------------
484 au_insert_text_callback(GtkTextBuffer
*buffer
,
490 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
493 GtkTextIter start
= *end
;
494 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
496 GtkTextIter line_start
= start
;
497 GtkTextIter line_end
= *end
;
498 GtkTextIter words_start
= start
;
499 GtkTextIter words_end
= *end
;
501 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
502 gtk_text_iter_forward_to_line_end(&line_end
);
503 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
504 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
506 au_check_range(&words_start
, &words_end
);
510 //-----------------------------------------------------------------------------
511 // "delete-range" for GtkTextBuffer
512 //-----------------------------------------------------------------------------
516 au_delete_range_callback(GtkTextBuffer
*buffer
,
521 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
524 GtkTextIter line_start
= *start
, line_end
= *end
;
526 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
527 gtk_text_iter_forward_to_line_end(&line_end
);
528 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
529 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
531 au_check_range(start
, end
);
536 //-----------------------------------------------------------------------------
538 //-----------------------------------------------------------------------------
542 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
544 if ( win
->IgnoreTextUpdate() )
547 if (!win
->m_hasVMT
) return;
550 wxapp_install_idle_handler();
552 if ( win
->MarkDirtyOnChange() )
555 win
->SendTextUpdatedEvent();
559 //-----------------------------------------------------------------------------
560 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
561 //-----------------------------------------------------------------------------
563 // common part of the event handlers below
565 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
566 wxEventType eventType
, const gchar
* signal_name
)
568 wxClipboardTextEvent
event( eventType
, win
->GetId() );
569 event
.SetEventObject( win
);
570 if ( win
->GetEventHandler()->ProcessEvent( event
) )
572 // don't let the default processing to take place if we did something
573 // ourselves in the event handler
574 g_signal_stop_emission_by_name (widget
, signal_name
);
580 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
582 handle_text_clipboard_callback(
583 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
587 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
589 handle_text_clipboard_callback(
590 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
594 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
596 handle_text_clipboard_callback(
597 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
601 //-----------------------------------------------------------------------------
602 // "expose_event" from scrolled window and textview
603 //-----------------------------------------------------------------------------
607 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
614 //-----------------------------------------------------------------------------
616 //-----------------------------------------------------------------------------
618 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
620 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
621 EVT_CHAR(wxTextCtrl::OnChar
)
623 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
624 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
625 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
626 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
627 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
629 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
630 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
631 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
632 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
633 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
635 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
636 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
637 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
638 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
639 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
640 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
641 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
642 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
643 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
646 void wxTextCtrl::Init()
651 m_countUpdatesToIgnore
= 0;
653 SetUpdateFont(false);
657 m_gdkHandCursor
= NULL
;
658 m_gdkXTermCursor
= NULL
;
661 wxTextCtrl::~wxTextCtrl()
664 gdk_cursor_unref(m_gdkHandCursor
);
666 gdk_cursor_unref(m_gdkXTermCursor
);
669 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
671 const wxString
&value
,
675 const wxValidator
& validator
,
676 const wxString
&name
)
680 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
683 bool wxTextCtrl::Create( wxWindow
*parent
,
685 const wxString
&value
,
689 const wxValidator
& validator
,
690 const wxString
&name
)
693 m_acceptsFocus
= true;
695 if (!PreCreation( parent
, pos
, size
) ||
696 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
698 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
702 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
707 m_text
= gtk_text_view_new();
709 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
711 // create scrolled window
712 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
713 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
714 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
715 // for ScrollLines/Pages
716 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
718 // Insert view into scrolled window
719 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
721 // translate wx wrapping style to GTK+
723 if ( HasFlag( wxTE_DONTWRAP
) )
724 wrap
= GTK_WRAP_NONE
;
725 else if ( HasFlag( wxTE_CHARWRAP
) )
726 wrap
= GTK_WRAP_CHAR
;
727 else if ( HasFlag( wxTE_WORDWRAP
) )
728 wrap
= GTK_WRAP_WORD
;
729 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
731 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
733 if ( !gtk_check_version(2,4,0) )
735 wrap
= GTK_WRAP_WORD_CHAR
;
739 wrap
= GTK_WRAP_WORD
;
742 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
744 GtkScrolledWindowSetBorder(m_widget
, style
);
746 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
748 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
752 // a single-line text control: no need for scrollbars
754 m_text
= gtk_entry_new();
756 if (style
& wxNO_BORDER
)
757 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
760 m_parent
->DoAddChild( this );
762 m_focusWidget
= m_text
;
768 gtk_widget_show(m_text
);
776 if (style
& wxTE_PASSWORD
)
779 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
782 if (style
& wxTE_READONLY
)
785 gtk_editable_set_editable( GTK_EDITABLE(m_text
), FALSE
);
787 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
792 if (style
& wxTE_RIGHT
)
793 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_RIGHT
);
794 else if (style
& wxTE_CENTRE
)
795 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_CENTER
);
796 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
801 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
802 if (!gtk_check_version(2,4,0))
804 if (style
& wxTE_RIGHT
)
805 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 1.0 );
806 else if (style
& wxTE_CENTRE
)
807 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 0.5 );
812 // We want to be notified about text changes.
815 g_signal_connect (m_buffer
, "changed",
816 G_CALLBACK (gtk_text_changed_callback
), this);
818 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
819 if (style
& wxTE_AUTO_URL
)
821 GtkTextIter start
, end
;
822 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
823 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
825 // We create our wxUrl tag here for slight efficiency gain - we
826 // don't have to check for the tag existance in callbacks,
827 // hereby it's guaranteed to exist.
828 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
829 "foreground", "blue",
830 "underline", PANGO_UNDERLINE_SINGLE
,
833 // Check for URLs after each text change
834 g_signal_connect_after (m_buffer
, "insert_text",
835 G_CALLBACK (au_insert_text_callback
), this);
836 g_signal_connect_after (m_buffer
, "delete_range",
837 G_CALLBACK (au_delete_range_callback
), this);
839 // Block all wxUrl tag applying unless we do it ourselves, in which case we
840 // block this callback temporarily. This takes care of gtk+ internal
841 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
842 // which is undesired because only a part of the URL might be copied.
843 // The insert-text signal emitted inside it will take care of newly formed
844 // or wholly copied URLs.
845 g_signal_connect (m_buffer
, "apply_tag",
846 G_CALLBACK (au_apply_tag_callback
), NULL
);
848 // Check for URLs in the initial string passed to Create
849 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
850 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
851 au_check_range(&start
, &end
);
856 g_signal_connect (m_text
, "changed",
857 G_CALLBACK (gtk_text_changed_callback
), this);
860 g_signal_connect (m_text
, "copy-clipboard",
861 G_CALLBACK (gtk_copy_clipboard_callback
), this);
862 g_signal_connect (m_text
, "cut-clipboard",
863 G_CALLBACK (gtk_cut_clipboard_callback
), this);
864 g_signal_connect (m_text
, "paste-clipboard",
865 G_CALLBACK (gtk_paste_clipboard_callback
), this);
867 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
869 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
870 SetDefaultStyle( attrDef
);
876 void wxTextCtrl::CalculateScrollbar()
880 wxString
wxTextCtrl::GetValue() const
882 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
888 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
890 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
891 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
893 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
899 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
900 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
908 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
910 // GTK+ uses UTF-8 internally, we need to convert to it but from which
913 // first check the default text style (we intentionally don't check the
914 // style for the current position as it doesn't make sense for SetValue())
915 const wxTextAttr
& style
= GetDefaultStyle();
916 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
917 : wxFONTENCODING_SYSTEM
;
919 // fall back to the controls font if no style
920 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
921 enc
= GetFont().GetEncoding();
926 bool wxTextCtrl::IsEmpty() const
929 return gtk_text_buffer_get_char_count(m_buffer
) != 0;
931 return wxTextCtrlBase::IsEmpty();
934 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
936 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
938 // the control won't be modified any more as we programmatically replace
939 // all the existing text, so reset the flag and don't set it again (and do
940 // it now, before the text event handler is ran so that IsModified() called
941 // from there returns the expected value)
943 DontMarkDirtyOnNextChange();
945 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, GetTextEncoding()));
948 // see comment in WriteText() as to why we must warn the user about
950 wxLogWarning(_("Failed to set text in the text control."));
954 // if the control is not empty, two "changed" signals are emitted,
955 // otherwise only one and we need to ignore either both or one of them
956 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
961 IgnoreNextTextUpdate(ignore
);
965 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
969 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
972 // GRG, Jun/2000: Changed this after a lot of discussion in
973 // the lists. wxWidgets 2.2 will have a set of flags to
974 // customize this behaviour.
975 SetInsertionPoint(0);
978 void wxTextCtrl::WriteText( const wxString
&text
)
980 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
985 // check if we have a specific style for the current position
986 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
988 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
990 enc
= style
.GetFont().GetEncoding();
993 if ( enc
== wxFONTENCODING_SYSTEM
)
994 enc
= GetTextEncoding();
996 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
999 // we must log an error here as losing the text like this can be a
1000 // serious problem (e.g. imagine the document edited by user being
1001 // empty instead of containing the correct text)
1002 wxLogWarning(_("Failed to insert text in the control."));
1006 // we're changing the text programmatically
1007 DontMarkDirtyOnNextChange();
1009 if ( IsMultiLine() )
1011 // First remove the selection if there is one
1012 // TODO: Is there an easier GTK specific way to do this?
1014 GetSelection(&from
, &to
);
1019 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1021 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1022 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1023 if ( wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
) )
1025 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1026 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1031 // First remove the selection if there is one
1032 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1034 // This moves the cursor pos to behind the inserted text.
1035 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1037 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1039 // Bring entry's cursor uptodate.
1040 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1044 void wxTextCtrl::AppendText( const wxString
&text
)
1046 SetInsertionPointEnd();
1050 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1053 if ( IsMultiLine() )
1056 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1057 GtkTextIter end
= line
;
1058 gtk_text_iter_forward_to_line_end(&end
);
1059 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1060 result
= wxGTK_CONV_BACK(text
);
1065 result
= GetValue();
1070 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1072 /* If you implement this, don't forget to update the documentation!
1073 * (file docs/latex/wx/text.tex) */
1074 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1077 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1079 if ( IsMultiLine() )
1083 if (pos
> GetLastPosition())
1086 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1089 *y
= gtk_text_iter_get_line(&iter
);
1091 *x
= gtk_text_iter_get_line_offset(&iter
);
1093 else // single line control
1095 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1104 // index out of bounds
1112 long wxTextCtrl::XYToPosition(long x
, long y
) const
1114 if ( IsSingleLine() )
1118 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1121 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1122 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1125 return gtk_text_iter_get_offset(&iter
) + x
;
1128 int wxTextCtrl::GetLineLength(long lineNo
) const
1130 if ( IsMultiLine() )
1132 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1133 if (lineNo
> last_line
)
1137 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1138 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1139 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1143 wxString str
= GetLineText (lineNo
);
1144 return (int) str
.length();
1148 int wxTextCtrl::GetNumberOfLines() const
1150 if ( IsMultiLine() )
1153 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, 0 );
1155 // move forward by one display line until the end is reached
1157 while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text
), &iter
) )
1162 // If the last character in the text buffer is a newline,
1163 // gtk_text_view_forward_display_line() will return false without that
1164 // line being counted. Must add one manually in that case.
1165 GtkTextIter lastCharIter
;
1166 gtk_text_buffer_get_iter_at_offset
1170 gtk_text_buffer_get_char_count(m_buffer
) - 1
1172 gchar lastChar
= gtk_text_iter_get_char( &lastCharIter
);
1173 if ( lastChar
== wxT('\n') )
1184 void wxTextCtrl::SetInsertionPoint( long pos
)
1186 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1188 if ( IsMultiLine() )
1191 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1192 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1193 gtk_text_view_scroll_mark_onscreen
1195 GTK_TEXT_VIEW(m_text
),
1196 gtk_text_buffer_get_insert( m_buffer
)
1201 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1202 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1206 void wxTextCtrl::SetInsertionPointEnd()
1208 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1210 if ( IsMultiLine() )
1213 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1214 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1218 gtk_editable_set_position( GTK_EDITABLE(m_text
), -1 );
1222 void wxTextCtrl::SetEditable( bool editable
)
1224 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1226 if ( IsMultiLine() )
1228 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1232 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1236 bool wxTextCtrl::Enable( bool enable
)
1238 if (!wxWindowBase::Enable(enable
))
1244 if ( IsMultiLine() )
1246 SetEditable( enable
);
1250 gtk_widget_set_sensitive( m_text
, enable
);
1256 // wxGTK-specific: called recursively by Enable,
1257 // to give widgets an oppprtunity to correct their colours after they
1258 // have been changed by Enable
1259 void wxTextCtrl::OnParentEnable( bool enable
)
1261 // If we have a custom background colour, we use this colour in both
1262 // disabled and enabled mode, or we end up with a different colour under the
1264 wxColour oldColour
= GetBackgroundColour();
1267 // Need to set twice or it'll optimize the useful stuff out
1268 if (oldColour
== * wxWHITE
)
1269 SetBackgroundColour(*wxBLACK
);
1271 SetBackgroundColour(*wxWHITE
);
1272 SetBackgroundColour(oldColour
);
1276 void wxTextCtrl::MarkDirty()
1281 void wxTextCtrl::DiscardEdits()
1286 // ----------------------------------------------------------------------------
1287 // max text length support
1288 // ----------------------------------------------------------------------------
1290 bool wxTextCtrl::IgnoreTextUpdate()
1292 if ( m_countUpdatesToIgnore
> 0 )
1294 m_countUpdatesToIgnore
--;
1302 bool wxTextCtrl::MarkDirtyOnChange()
1304 if ( m_dontMarkDirty
)
1306 m_dontMarkDirty
= false;
1314 void wxTextCtrl::SetMaxLength(unsigned long len
)
1316 if ( !HasFlag(wxTE_MULTILINE
) )
1318 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1320 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1321 // we had tried to enter more text than allowed by max text length and
1322 // the text wasn't really changed
1324 // to detect this and generate TEXT_MAXLEN event instead of
1325 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1327 // when max len is set to 0 we disconnect our handler as it means that
1328 // we shouldn't check anything any more
1331 g_signal_connect (m_text
, "insert_text",
1332 G_CALLBACK (gtk_insert_text_callback
), this);
1336 g_signal_handlers_disconnect_by_func (m_text
,
1337 (gpointer
) gtk_insert_text_callback
, this);
1342 void wxTextCtrl::SetSelection( long from
, long to
)
1344 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1346 if (from
== -1 && to
== -1)
1349 to
= GetValue().length();
1352 if ( IsMultiLine() )
1354 GtkTextIter fromi
, toi
;
1355 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1356 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1358 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1359 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1363 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1367 void wxTextCtrl::ShowPosition( long pos
)
1369 if ( IsMultiLine() )
1372 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1373 gtk_text_iter_set_offset( &iter
, pos
);
1374 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1375 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1379 wxTextCtrlHitTestResult
1380 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1382 if ( !IsMultiLine() )
1385 return wxTE_HT_UNKNOWN
;
1389 gtk_text_view_window_to_buffer_coords
1391 GTK_TEXT_VIEW(m_text
),
1392 GTK_TEXT_WINDOW_TEXT
,
1398 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1400 *pos
= gtk_text_iter_get_offset(&iter
);
1402 return wxTE_HT_ON_TEXT
;
1405 long wxTextCtrl::GetInsertionPoint() const
1407 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1409 if ( IsMultiLine() )
1411 // There is no direct accessor for the cursor, but
1412 // internally, the cursor is the "mark" called
1413 // "insert" in the text view's btree structure.
1415 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1417 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1419 return gtk_text_iter_get_offset( &cursor
);
1423 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1427 wxTextPos
wxTextCtrl::GetLastPosition() const
1429 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1433 if ( IsMultiLine() )
1436 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1438 pos
= gtk_text_iter_get_offset( &end
);
1442 pos
= GTK_ENTRY(m_text
)->text_length
;
1448 void wxTextCtrl::Remove( long from
, long to
)
1450 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1452 if ( IsMultiLine() )
1454 GtkTextIter fromi
, toi
;
1455 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1456 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1458 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1461 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1464 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1466 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1472 SetInsertionPoint( from
);
1477 void wxTextCtrl::Cut()
1479 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1481 if ( IsMultiLine() )
1482 g_signal_emit_by_name (m_text
, "cut-clipboard");
1484 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1487 void wxTextCtrl::Copy()
1489 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1491 if ( IsMultiLine() )
1492 g_signal_emit_by_name (m_text
, "copy-clipboard");
1494 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1497 void wxTextCtrl::Paste()
1499 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1501 if ( IsMultiLine() )
1502 g_signal_emit_by_name (m_text
, "paste-clipboard");
1504 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1508 void wxTextCtrl::Undo()
1511 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1514 void wxTextCtrl::Redo()
1517 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1520 bool wxTextCtrl::CanUndo() const
1523 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1527 bool wxTextCtrl::CanRedo() const
1530 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1534 // If the return values from and to are the same, there is no
1536 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1538 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1542 bool haveSelection
= false;
1544 if ( IsMultiLine() )
1546 GtkTextIter ifrom
, ito
;
1547 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1549 haveSelection
= true;
1550 from
= gtk_text_iter_get_offset(&ifrom
);
1551 to
= gtk_text_iter_get_offset(&ito
);
1554 else // not multi-line
1556 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1559 haveSelection
= true;
1563 if (! haveSelection
)
1564 from
= to
= GetInsertionPoint();
1568 // exchange them to be compatible with wxMSW
1581 bool wxTextCtrl::IsEditable() const
1583 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1585 if ( IsMultiLine() )
1587 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1591 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1595 bool wxTextCtrl::IsModified() const
1600 void wxTextCtrl::Clear()
1602 SetValue( wxEmptyString
);
1605 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1607 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1609 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1611 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1613 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1614 event
.SetEventObject(this);
1615 event
.SetString(GetValue());
1616 if ( GetEventHandler()->ProcessEvent(event
) )
1620 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1622 if ( IsSingleLine() )
1624 // This will invoke the dialog default action, such
1625 // as the clicking the default button.
1627 wxWindow
*top_frame
= m_parent
;
1628 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1629 top_frame
= top_frame
->GetParent();
1631 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1633 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1635 if (window
->default_widget
)
1637 gtk_widget_activate (window
->default_widget
);
1647 GtkWidget
* wxTextCtrl::GetConnectWidget()
1649 return GTK_WIDGET(m_text
);
1652 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1654 if ( IsMultiLine() )
1656 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1657 GTK_TEXT_WINDOW_TEXT
);
1661 return GTK_ENTRY(m_text
)->text_area
;
1665 // the font will change for subsequent text insertiongs
1666 bool wxTextCtrl::SetFont( const wxFont
&font
)
1668 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1670 if ( !wxTextCtrlBase::SetFont(font
) )
1672 // font didn't change, nothing to do
1676 if ( IsMultiLine() )
1678 SetUpdateFont(true);
1680 m_defaultStyle
.SetFont(font
);
1682 ChangeFontGlobally();
1688 void wxTextCtrl::ChangeFontGlobally()
1690 // this method is very inefficient and hence should be called as rarely as
1693 // TODO: it can be implemented much more efficiently for GTK2
1694 wxASSERT_MSG( IsMultiLine(),
1695 _T("shouldn't be called for single line controls") );
1697 wxString value
= GetValue();
1698 if ( !value
.empty() )
1700 SetUpdateFont(false);
1707 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1709 if ( !wxControl::SetForegroundColour(colour
) )
1712 // update default fg colour too
1713 m_defaultStyle
.SetTextColour(colour
);
1718 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1720 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1722 if ( !wxControl::SetBackgroundColour( colour
) )
1725 if (!m_backgroundColour
.Ok())
1728 // change active background color too
1729 m_defaultStyle
.SetBackgroundColour( colour
);
1734 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1736 if ( IsMultiLine() )
1738 if ( style
.IsDefault() )
1744 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1746 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1747 _T("invalid range in wxTextCtrl::SetStyle") );
1749 GtkTextIter starti
, endi
;
1750 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1751 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1753 // use the attributes from style which are set in it and fall back
1754 // first to the default style and then to the text control default
1755 // colours for the others
1756 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1758 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, attr
, &starti
, &endi
);
1764 // cannot do this for GTK+'s Entry widget
1768 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1770 gtk_widget_modify_style(m_text
, style
);
1773 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1778 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1783 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1788 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1793 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1798 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1800 event
.Enable( CanCut() );
1803 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1805 event
.Enable( CanCopy() );
1808 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1810 event
.Enable( CanPaste() );
1813 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1815 event
.Enable( CanUndo() );
1818 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1820 event
.Enable( CanRedo() );
1823 wxSize
wxTextCtrl::DoGetBestSize() const
1825 // FIXME should be different for multi-line controls...
1826 wxSize
ret( wxControl::DoGetBestSize() );
1827 wxSize
best(80, ret
.y
);
1828 CacheBestSize(best
);
1832 // ----------------------------------------------------------------------------
1834 // ----------------------------------------------------------------------------
1836 void wxTextCtrl::Freeze()
1838 if ( HasFlag(wxTE_MULTILINE
) )
1840 if ( !m_frozenness
++ )
1842 // freeze textview updates and remove buffer
1843 g_signal_connect (m_text
, "expose_event",
1844 G_CALLBACK (gtk_text_exposed_callback
), this);
1845 g_signal_connect (m_widget
, "expose_event",
1846 G_CALLBACK (gtk_text_exposed_callback
), this);
1847 gtk_widget_set_sensitive(m_widget
, false);
1848 g_object_ref(m_buffer
);
1849 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), gtk_text_buffer_new(NULL
));
1854 void wxTextCtrl::Thaw()
1856 if ( HasFlag(wxTE_MULTILINE
) )
1858 wxASSERT_MSG( m_frozenness
> 0, _T("Thaw() without matching Freeze()") );
1860 if ( !--m_frozenness
)
1862 // Reattach buffer and thaw textview updates
1863 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1864 g_object_unref(m_buffer
);
1865 gtk_widget_set_sensitive(m_widget
, true);
1866 g_signal_handlers_disconnect_by_func (m_widget
,
1867 (gpointer
) gtk_text_exposed_callback
, this);
1868 g_signal_handlers_disconnect_by_func (m_text
,
1869 (gpointer
) gtk_text_exposed_callback
, this);
1874 // ----------------------------------------------------------------------------
1875 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1876 // ----------------------------------------------------------------------------
1878 // FIXME: when dragging on a link the sample gets an "Unknown event".
1879 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1880 // a buggy sample, or something else
1881 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1884 if( !HasFlag(wxTE_AUTO_URL
) )
1888 GtkTextIter start
, end
;
1889 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1892 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1893 event
.GetX(), event
.GetY(), &x
, &y
);
1895 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1896 if (!gtk_text_iter_has_tag(&end
, tag
))
1898 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1899 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1903 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1904 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1907 if(!gtk_text_iter_begins_tag(&start
, tag
))
1908 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1909 if(!gtk_text_iter_ends_tag(&end
, tag
))
1910 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1912 // Native context menu is probably not desired on an URL.
1913 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1914 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1917 wxTextUrlEvent
url_event(m_windowId
, event
,
1918 gtk_text_iter_get_offset(&start
),
1919 gtk_text_iter_get_offset(&end
));
1921 InitCommandEvent(url_event
);
1922 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1923 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1924 GetEventHandler()->ProcessEvent(url_event
);
1927 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1929 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1931 // GtkTextView starts a drag operation when left mouse button is pressed
1932 // and ends it when it is released and if it doesn't get the release event
1933 // the next click on a control results in an assertion failure inside
1934 // gtk_text_view_start_selection_drag() which simply *kills* the program
1935 // without anything we can do about it, so always let GTK+ have this event
1936 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1941 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1943 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);