1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
19 #include "wx/settings.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
26 #include <sys/types.h>
30 #include "wx/gtk/private.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
37 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
44 g_object_get (tag
, "name", &name
, NULL
);
46 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
47 // anonymous tag or not starting with prefix - don't remove
48 g_signal_stop_emission_by_name (buffer
, "remove_tag");
54 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
55 GtkTextBuffer
*text_buffer
,
56 const wxTextAttr
& attr
,
60 static gchar buf
[1024];
63 gulong remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
64 G_CALLBACK (wxGtkOnRemoveTag
), gpointer("WX"));
65 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
66 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
70 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
71 wxGtkString
font_string(pango_font_description_to_string(font_description
));
72 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
73 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
76 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
77 "font-desc", font_description
,
79 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
81 if (attr
.GetFont().GetUnderlined())
83 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
84 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
87 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
88 "underline-set", TRUE
,
89 "underline", PANGO_UNDERLINE_SINGLE
,
91 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
95 if (attr
.HasTextColour())
97 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
98 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
99 colFg
->red
, colFg
->green
, colFg
->blue
);
100 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
103 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
104 "foreground-gdk", colFg
, NULL
);
105 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
108 if (attr
.HasBackgroundColour())
110 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
111 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
112 colBg
->red
, colBg
->green
, colBg
->blue
);
113 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
116 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
117 "background-gdk", colBg
, NULL
);
118 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
121 if (attr
.HasAlignment())
123 GtkTextIter para_start
, para_end
= *end
;
124 gtk_text_buffer_get_iter_at_line( text_buffer
,
126 gtk_text_iter_get_line(start
) );
127 gtk_text_iter_forward_line(¶_end
);
129 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
130 G_CALLBACK(wxGtkOnRemoveTag
),
131 gpointer("WXALIGNMENT"));
132 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
133 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
135 GtkJustification align
;
136 switch (attr
.GetAlignment())
139 align
= GTK_JUSTIFY_LEFT
;
141 case wxTEXT_ALIGNMENT_RIGHT
:
142 align
= GTK_JUSTIFY_RIGHT
;
144 case wxTEXT_ALIGNMENT_CENTER
:
145 align
= GTK_JUSTIFY_CENTER
;
147 // gtk+ doesn't support justify as of gtk+-2.7.4
150 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
151 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
154 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
155 "justification", align
, NULL
);
156 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
159 if (attr
.HasLeftIndent())
161 // Indentation attribute
163 // Clear old indentation tags
164 GtkTextIter para_start
, para_end
= *end
;
165 gtk_text_buffer_get_iter_at_line( text_buffer
,
167 gtk_text_iter_get_line(start
) );
168 gtk_text_iter_forward_line(¶_end
);
170 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
171 G_CALLBACK(wxGtkOnRemoveTag
),
172 gpointer("WXINDENT"));
173 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
174 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
176 // Convert indent from 1/10th of a mm into pixels
178 #if GTK_CHECK_VERSION(2,2,0)
179 if (!gtk_check_version(2,2,0))
180 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
181 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
184 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
186 const int indent
= (int)(factor
* attr
.GetLeftIndent());
187 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
195 gsubindent
= -subIndent
;
199 gindent
= -subIndent
;
203 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
204 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
207 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
208 "left-margin", gindent
, "indent", gsubindent
, NULL
);
209 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
217 GtkTextIter para_start
, para_end
= *end
;
218 gtk_text_buffer_get_iter_at_line( text_buffer
,
220 gtk_text_iter_get_line(start
) );
221 gtk_text_iter_forward_line(¶_end
);
223 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
224 G_CALLBACK(wxGtkOnRemoveTag
),
226 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
227 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
229 const wxArrayInt
& tabs
= attr
.GetTabs();
231 wxString tagname
= _T("WXTABS");
232 g_snprintf(buf
, sizeof(buf
), "WXTABS");
233 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
234 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
236 const wxWX2MBbuf buf
= tagname
.mb_str(wxConvUTF8
);
238 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
242 // Factor to convert from 1/10th of a mm into pixels
244 #if GTK_CHECK_VERSION(2,2,0)
245 if (!gtk_check_version(2,2,0))
246 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
247 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
250 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
252 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
253 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
254 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
255 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
256 "tabs", tabArray
, NULL
);
257 pango_tab_array_free(tabArray
);
259 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
263 static void wxGtkTextInsert(GtkWidget
*text
,
264 GtkTextBuffer
*text_buffer
,
265 const wxTextAttr
& attr
,
266 const wxCharBuffer
& buffer
)
270 GtkTextIter iter
, start
;
272 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
273 gtk_text_buffer_get_insert (text_buffer
) );
274 start_offset
= gtk_text_iter_get_offset (&iter
);
275 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
277 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
279 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
282 // ----------------------------------------------------------------------------
283 // "insert_text" for GtkEntry
284 // ----------------------------------------------------------------------------
288 gtk_insert_text_callback(GtkEditable
*editable
,
289 const gchar
*new_text
,
290 gint new_text_length
,
295 wxapp_install_idle_handler();
297 // we should only be called if we have a max len limit at all
298 GtkEntry
*entry
= GTK_ENTRY (editable
);
300 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
302 // check that we don't overflow the max length limit
304 // FIXME: this doesn't work when we paste a string which is going to be
306 if ( entry
->text_length
== entry
->text_max_length
)
308 // we don't need to run the base class version at all
309 g_signal_stop_emission_by_name (editable
, "insert_text");
311 // remember that the next changed signal is to be ignored to avoid
312 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
313 win
->IgnoreNextTextUpdate();
315 // and generate the correct one ourselves
316 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
317 event
.SetEventObject(win
);
318 event
.SetString(win
->GetValue());
319 win
->GetEventHandler()->ProcessEvent( event
);
324 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
328 au_apply_tag_callback(GtkTextBuffer
*buffer
,
334 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
335 g_signal_stop_emission_by_name (buffer
, "apply_tag");
339 //-----------------------------------------------------------------------------
340 // GtkTextCharPredicates for gtk_text_iter_*_find_char
341 //-----------------------------------------------------------------------------
345 pred_whitespace (gunichar ch
, gpointer user_data
)
347 return g_unichar_isspace(ch
);
353 pred_non_whitespace (gunichar ch
, gpointer user_data
)
355 return !g_unichar_isspace(ch
);
361 pred_nonpunct (gunichar ch
, gpointer user_data
)
363 return !g_unichar_ispunct(ch
);
369 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
371 return !g_unichar_ispunct(ch
) || ch
== '/';
375 //-----------------------------------------------------------------------------
376 // Check for links between s and e and correct tags as necessary
377 //-----------------------------------------------------------------------------
379 // This function should be made match better while being efficient at one point.
380 // Most probably with a row of regular expressions.
383 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
385 static const char *URIPrefixes
[] =
403 GtkTextIter start
= *s
, end
= *e
;
404 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
406 // Get our special link tag
407 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
409 // Get rid of punctuation from beginning and end.
410 // Might want to move this to au_check_range if an improved link checking doesn't
411 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
412 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
413 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
415 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
416 gtk_text_iter_forward_char(&end
);
418 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
419 size_t len
= strlen(text
), prefix_len
;
422 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
424 prefix_len
= strlen(URIPrefixes
[n
]);
425 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
429 if(n
< WXSIZEOF(URIPrefixes
))
431 gulong signal_id
= g_signal_handler_find (buffer
,
432 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
434 (gpointer
)au_apply_tag_callback
, NULL
);
436 g_signal_handler_block (buffer
, signal_id
);
437 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
438 g_signal_handler_unblock (buffer
, signal_id
);
445 au_check_range(GtkTextIter
*s
,
446 GtkTextIter
*range_end
)
448 GtkTextIter range_start
= *s
;
449 GtkTextIter word_end
;
450 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
451 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
453 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
455 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
456 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
458 while(!gtk_text_iter_equal(&range_start
, range_end
))
460 word_end
= range_start
;
461 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
463 // Now we should have a word delimited by range_start and word_end, correct link tags
464 au_check_word(&range_start
, &word_end
);
466 range_start
= word_end
;
467 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
472 //-----------------------------------------------------------------------------
473 // "insert-text" for GtkTextBuffer
474 //-----------------------------------------------------------------------------
478 au_insert_text_callback(GtkTextBuffer
*buffer
,
484 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
487 GtkTextIter start
= *end
;
488 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
490 GtkTextIter line_start
= start
;
491 GtkTextIter line_end
= *end
;
492 GtkTextIter words_start
= start
;
493 GtkTextIter words_end
= *end
;
495 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
496 gtk_text_iter_forward_to_line_end(&line_end
);
497 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
498 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
500 au_check_range(&words_start
, &words_end
);
504 //-----------------------------------------------------------------------------
505 // "delete-range" for GtkTextBuffer
506 //-----------------------------------------------------------------------------
510 au_delete_range_callback(GtkTextBuffer
*buffer
,
515 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
518 GtkTextIter line_start
= *start
, line_end
= *end
;
520 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
521 gtk_text_iter_forward_to_line_end(&line_end
);
522 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
523 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
525 au_check_range(start
, end
);
530 //-----------------------------------------------------------------------------
532 //-----------------------------------------------------------------------------
536 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
538 if ( win
->IgnoreTextUpdate() )
541 if (!win
->m_hasVMT
) return;
544 wxapp_install_idle_handler();
546 if ( win
->MarkDirtyOnChange() )
549 win
->SendTextUpdatedEvent();
553 //-----------------------------------------------------------------------------
554 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
555 //-----------------------------------------------------------------------------
557 // common part of the event handlers below
559 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
560 wxEventType eventType
, const gchar
* signal_name
)
562 wxClipboardTextEvent
event( eventType
, win
->GetId() );
563 event
.SetEventObject( win
);
564 if ( win
->GetEventHandler()->ProcessEvent( event
) )
566 // don't let the default processing to take place if we did something
567 // ourselves in the event handler
568 g_signal_stop_emission_by_name (widget
, signal_name
);
574 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
576 handle_text_clipboard_callback(
577 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
581 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
583 handle_text_clipboard_callback(
584 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
588 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
590 handle_text_clipboard_callback(
591 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
595 //-----------------------------------------------------------------------------
596 // "expose_event" from scrolled window and textview
597 //-----------------------------------------------------------------------------
601 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
608 //-----------------------------------------------------------------------------
610 //-----------------------------------------------------------------------------
612 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
614 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
615 EVT_CHAR(wxTextCtrl::OnChar
)
617 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
618 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
619 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
620 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
621 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
623 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
624 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
625 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
626 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
627 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
629 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
630 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
631 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
632 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
633 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
634 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
635 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
636 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
637 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
640 void wxTextCtrl::Init()
645 m_countUpdatesToIgnore
= 0;
647 SetUpdateFont(false);
651 m_gdkHandCursor
= NULL
;
652 m_gdkXTermCursor
= NULL
;
655 wxTextCtrl::~wxTextCtrl()
658 gdk_cursor_unref(m_gdkHandCursor
);
660 gdk_cursor_unref(m_gdkXTermCursor
);
663 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
665 const wxString
&value
,
669 const wxValidator
& validator
,
670 const wxString
&name
)
674 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
677 bool wxTextCtrl::Create( wxWindow
*parent
,
679 const wxString
&value
,
683 const wxValidator
& validator
,
684 const wxString
&name
)
687 m_acceptsFocus
= true;
689 if (!PreCreation( parent
, pos
, size
) ||
690 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
692 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
696 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
701 m_text
= gtk_text_view_new();
703 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
705 // create scrolled window
706 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
707 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
708 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
709 // for ScrollLines/Pages
710 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
712 // Insert view into scrolled window
713 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
717 GtkScrolledWindowSetBorder(m_widget
, style
);
719 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
721 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
725 // a single-line text control: no need for scrollbars
727 m_text
= gtk_entry_new();
729 if (style
& wxNO_BORDER
)
730 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
733 m_parent
->DoAddChild( this );
735 m_focusWidget
= m_text
;
741 gtk_widget_show(m_text
);
749 if (style
& wxTE_PASSWORD
)
752 if (style
& wxTE_READONLY
)
755 // left justification (alignment) is the default anyhow
756 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
757 GTKSetJustification();
759 // We want to be notified about text changes.
762 g_signal_connect (m_buffer
, "changed",
763 G_CALLBACK (gtk_text_changed_callback
), this);
765 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
766 if (style
& wxTE_AUTO_URL
)
768 GtkTextIter start
, end
;
769 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
770 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
772 // We create our wxUrl tag here for slight efficiency gain - we
773 // don't have to check for the tag existance in callbacks,
774 // hereby it's guaranteed to exist.
775 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
776 "foreground", "blue",
777 "underline", PANGO_UNDERLINE_SINGLE
,
780 // Check for URLs after each text change
781 g_signal_connect_after (m_buffer
, "insert_text",
782 G_CALLBACK (au_insert_text_callback
), this);
783 g_signal_connect_after (m_buffer
, "delete_range",
784 G_CALLBACK (au_delete_range_callback
), this);
786 // Block all wxUrl tag applying unless we do it ourselves, in which case we
787 // block this callback temporarily. This takes care of gtk+ internal
788 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
789 // which is undesired because only a part of the URL might be copied.
790 // The insert-text signal emitted inside it will take care of newly formed
791 // or wholly copied URLs.
792 g_signal_connect (m_buffer
, "apply_tag",
793 G_CALLBACK (au_apply_tag_callback
), NULL
);
795 // Check for URLs in the initial string passed to Create
796 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
797 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
798 au_check_range(&start
, &end
);
803 g_signal_connect (m_text
, "changed",
804 G_CALLBACK (gtk_text_changed_callback
), this);
807 g_signal_connect (m_text
, "copy-clipboard",
808 G_CALLBACK (gtk_copy_clipboard_callback
), this);
809 g_signal_connect (m_text
, "cut-clipboard",
810 G_CALLBACK (gtk_cut_clipboard_callback
), this);
811 g_signal_connect (m_text
, "paste-clipboard",
812 G_CALLBACK (gtk_paste_clipboard_callback
), this);
814 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
816 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
817 SetDefaultStyle( attrDef
);
822 // ----------------------------------------------------------------------------
824 // ----------------------------------------------------------------------------
826 void wxTextCtrl::GTKSetEditable()
828 gboolean editable
= !HasFlag(wxTE_READONLY
);
829 if ( IsSingleLine() )
830 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
832 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
835 void wxTextCtrl::GTKSetVisibility()
837 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
838 if ( IsSingleLine() )
839 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
842 void wxTextCtrl::GTKSetWrapMode()
844 // no wrapping in single line controls
845 if ( !IsMultiLine() )
848 // translate wx wrapping style to GTK+
850 if ( HasFlag( wxTE_DONTWRAP
) )
851 wrap
= GTK_WRAP_NONE
;
852 else if ( HasFlag( wxTE_CHARWRAP
) )
853 wrap
= GTK_WRAP_CHAR
;
854 else if ( HasFlag( wxTE_WORDWRAP
) )
855 wrap
= GTK_WRAP_WORD
;
856 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
858 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
860 if ( !gtk_check_version(2,4,0) )
862 wrap
= GTK_WRAP_WORD_CHAR
;
865 #endif // __WXGTK24__
866 wrap
= GTK_WRAP_WORD
;
869 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
872 void wxTextCtrl::GTKSetJustification()
876 GtkJustification just
;
877 if ( HasFlag(wxTE_RIGHT
) )
878 just
= GTK_JUSTIFY_RIGHT
;
879 else if ( HasFlag(wxTE_CENTRE
) )
880 just
= GTK_JUSTIFY_CENTER
;
881 else // wxTE_LEFT == 0
882 just
= GTK_JUSTIFY_LEFT
;
884 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
889 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
890 if (!gtk_check_version(2,4,0))
893 if ( HasFlag(wxTE_RIGHT
) )
895 else if ( HasFlag(wxTE_CENTRE
) )
900 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
902 #endif // __WXGTK24__
907 void wxTextCtrl::SetWindowStyleFlag(long style
)
909 long styleOld
= GetWindowStyleFlag();
911 wxTextCtrlBase::SetWindowStyleFlag(style
);
913 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
916 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
919 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
920 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
923 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
924 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
925 GTKSetJustification();
928 // ----------------------------------------------------------------------------
930 // ----------------------------------------------------------------------------
932 wxString
wxTextCtrl::GetValue() const
934 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
940 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
942 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
943 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
945 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
951 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
952 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
960 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
962 // GTK+ uses UTF-8 internally, we need to convert to it but from which
965 // first check the default text style (we intentionally don't check the
966 // style for the current position as it doesn't make sense for SetValue())
967 const wxTextAttr
& style
= GetDefaultStyle();
968 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
969 : wxFONTENCODING_SYSTEM
;
971 // fall back to the controls font if no style
972 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
973 enc
= GetFont().GetEncoding();
978 bool wxTextCtrl::IsEmpty() const
981 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
983 return wxTextCtrlBase::IsEmpty();
986 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
988 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
990 // the control won't be modified any more as we programmatically replace
991 // all the existing text, so reset the flag and don't set it again (and do
992 // it now, before the text event handler is ran so that IsModified() called
993 // from there returns the expected value)
995 DontMarkDirtyOnNextChange();
997 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, GetTextEncoding()));
1000 // see comment in WriteText() as to why we must warn the user about
1002 wxLogWarning(_("Failed to set text in the text control."));
1006 // if the control is not empty, two "changed" signals are emitted,
1007 // otherwise only one and we need to ignore either both or one of them
1008 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
1013 IgnoreNextTextUpdate(ignore
);
1015 if ( IsMultiLine() )
1017 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1021 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
1024 // if, for whatever reason, the callback wasn't called the expected number
1025 // of times, still reset the flags to the default values
1026 m_dontMarkDirty
= false;
1027 m_countUpdatesToIgnore
= 0;
1030 // GRG, Jun/2000: Changed this after a lot of discussion in
1031 // the lists. wxWidgets 2.2 will have a set of flags to
1032 // customize this behaviour.
1033 SetInsertionPoint(0);
1036 void wxTextCtrl::WriteText( const wxString
&text
)
1038 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1043 // check if we have a specific style for the current position
1044 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1046 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1048 enc
= style
.GetFont().GetEncoding();
1051 if ( enc
== wxFONTENCODING_SYSTEM
)
1052 enc
= GetTextEncoding();
1054 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1057 // we must log an error here as losing the text like this can be a
1058 // serious problem (e.g. imagine the document edited by user being
1059 // empty instead of containing the correct text)
1060 wxLogWarning(_("Failed to insert text in the control."));
1064 // we're changing the text programmatically
1065 DontMarkDirtyOnNextChange();
1067 if ( IsMultiLine() )
1069 // First remove the selection if there is one
1070 // TODO: Is there an easier GTK specific way to do this?
1072 GetSelection(&from
, &to
);
1077 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1079 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1080 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1081 // won't work when frozen, text view is not using m_buffer then
1082 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1084 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1085 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1090 // First remove the selection if there is one
1091 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1093 // This moves the cursor pos to behind the inserted text.
1094 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1096 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1098 // Bring entry's cursor uptodate.
1099 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1103 void wxTextCtrl::AppendText( const wxString
&text
)
1105 SetInsertionPointEnd();
1109 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1112 if ( IsMultiLine() )
1115 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1116 GtkTextIter end
= line
;
1117 gtk_text_iter_forward_to_line_end(&end
);
1118 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1119 result
= wxGTK_CONV_BACK(text
);
1124 result
= GetValue();
1129 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1131 /* If you implement this, don't forget to update the documentation!
1132 * (file docs/latex/wx/text.tex) */
1133 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1136 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1138 if ( IsMultiLine() )
1142 if (pos
> GetLastPosition())
1145 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1148 *y
= gtk_text_iter_get_line(&iter
);
1150 *x
= gtk_text_iter_get_line_offset(&iter
);
1152 else // single line control
1154 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1163 // index out of bounds
1171 long wxTextCtrl::XYToPosition(long x
, long y
) const
1173 if ( IsSingleLine() )
1177 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1180 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1181 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1184 return gtk_text_iter_get_offset(&iter
) + x
;
1187 int wxTextCtrl::GetLineLength(long lineNo
) const
1189 if ( IsMultiLine() )
1191 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1192 if (lineNo
> last_line
)
1196 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1197 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1198 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1202 wxString str
= GetLineText (lineNo
);
1203 return (int) str
.length();
1207 int wxTextCtrl::GetNumberOfLines() const
1209 if ( IsMultiLine() )
1211 return gtk_text_buffer_get_line_count( m_buffer
);
1219 void wxTextCtrl::SetInsertionPoint( long pos
)
1221 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1223 if ( IsMultiLine() )
1226 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1227 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1230 // won't work when frozen, text view is not using m_buffer then
1231 gtk_text_view_scroll_mark_onscreen
1233 GTK_TEXT_VIEW(m_text
),
1234 gtk_text_buffer_get_insert( m_buffer
)
1240 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1241 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1245 void wxTextCtrl::SetInsertionPointEnd()
1247 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1249 if ( IsMultiLine() )
1252 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1253 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1257 gtk_editable_set_position( GTK_EDITABLE(m_text
), -1 );
1261 void wxTextCtrl::SetEditable( bool editable
)
1263 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1265 if ( IsMultiLine() )
1267 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1271 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1275 bool wxTextCtrl::Enable( bool enable
)
1277 if (!wxWindowBase::Enable(enable
))
1283 if ( IsMultiLine() )
1285 SetEditable( enable
);
1289 gtk_widget_set_sensitive( m_text
, enable
);
1295 // wxGTK-specific: called recursively by Enable,
1296 // to give widgets an oppprtunity to correct their colours after they
1297 // have been changed by Enable
1298 void wxTextCtrl::OnParentEnable( bool enable
)
1300 // If we have a custom background colour, we use this colour in both
1301 // disabled and enabled mode, or we end up with a different colour under the
1303 wxColour oldColour
= GetBackgroundColour();
1306 // Need to set twice or it'll optimize the useful stuff out
1307 if (oldColour
== * wxWHITE
)
1308 SetBackgroundColour(*wxBLACK
);
1310 SetBackgroundColour(*wxWHITE
);
1311 SetBackgroundColour(oldColour
);
1315 void wxTextCtrl::MarkDirty()
1320 void wxTextCtrl::DiscardEdits()
1325 // ----------------------------------------------------------------------------
1326 // max text length support
1327 // ----------------------------------------------------------------------------
1329 bool wxTextCtrl::IgnoreTextUpdate()
1331 if ( m_countUpdatesToIgnore
> 0 )
1333 m_countUpdatesToIgnore
--;
1341 bool wxTextCtrl::MarkDirtyOnChange()
1343 if ( m_dontMarkDirty
)
1345 m_dontMarkDirty
= false;
1353 void wxTextCtrl::SetMaxLength(unsigned long len
)
1355 if ( !HasFlag(wxTE_MULTILINE
) )
1357 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1359 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1360 // we had tried to enter more text than allowed by max text length and
1361 // the text wasn't really changed
1363 // to detect this and generate TEXT_MAXLEN event instead of
1364 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1366 // when max len is set to 0 we disconnect our handler as it means that
1367 // we shouldn't check anything any more
1370 g_signal_connect (m_text
, "insert_text",
1371 G_CALLBACK (gtk_insert_text_callback
), this);
1375 g_signal_handlers_disconnect_by_func (m_text
,
1376 (gpointer
) gtk_insert_text_callback
, this);
1381 void wxTextCtrl::SetSelection( long from
, long to
)
1383 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1385 if (from
== -1 && to
== -1)
1388 to
= GetValue().length();
1391 if ( IsMultiLine() )
1393 GtkTextIter fromi
, toi
;
1394 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1395 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1397 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1398 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1402 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1406 void wxTextCtrl::ShowPosition( long pos
)
1408 // won't work when frozen, text view is not using m_buffer then
1409 if (IsMultiLine() && !IsFrozen())
1412 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1413 gtk_text_iter_set_offset( &iter
, pos
);
1414 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1415 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1419 wxTextCtrlHitTestResult
1420 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1422 if ( !IsMultiLine() )
1425 return wxTE_HT_UNKNOWN
;
1429 gtk_text_view_window_to_buffer_coords
1431 GTK_TEXT_VIEW(m_text
),
1432 GTK_TEXT_WINDOW_TEXT
,
1438 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1440 *pos
= gtk_text_iter_get_offset(&iter
);
1442 return wxTE_HT_ON_TEXT
;
1445 long wxTextCtrl::GetInsertionPoint() const
1447 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1449 if ( IsMultiLine() )
1451 // There is no direct accessor for the cursor, but
1452 // internally, the cursor is the "mark" called
1453 // "insert" in the text view's btree structure.
1455 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1457 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1459 return gtk_text_iter_get_offset( &cursor
);
1463 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1467 wxTextPos
wxTextCtrl::GetLastPosition() const
1469 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1473 if ( IsMultiLine() )
1476 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1478 pos
= gtk_text_iter_get_offset( &end
);
1482 pos
= GTK_ENTRY(m_text
)->text_length
;
1488 void wxTextCtrl::Remove( long from
, long to
)
1490 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1492 if ( IsMultiLine() )
1494 GtkTextIter fromi
, toi
;
1495 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1496 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1498 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1501 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1504 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1506 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1512 SetInsertionPoint( from
);
1517 void wxTextCtrl::Cut()
1519 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1521 if ( IsMultiLine() )
1522 g_signal_emit_by_name (m_text
, "cut-clipboard");
1524 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1527 void wxTextCtrl::Copy()
1529 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1531 if ( IsMultiLine() )
1532 g_signal_emit_by_name (m_text
, "copy-clipboard");
1534 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1537 void wxTextCtrl::Paste()
1539 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1541 if ( IsMultiLine() )
1542 g_signal_emit_by_name (m_text
, "paste-clipboard");
1544 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1548 void wxTextCtrl::Undo()
1551 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1554 void wxTextCtrl::Redo()
1557 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1560 bool wxTextCtrl::CanUndo() const
1563 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1567 bool wxTextCtrl::CanRedo() const
1570 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1574 // If the return values from and to are the same, there is no
1576 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1578 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1582 bool haveSelection
= false;
1584 if ( IsMultiLine() )
1586 GtkTextIter ifrom
, ito
;
1587 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1589 haveSelection
= true;
1590 from
= gtk_text_iter_get_offset(&ifrom
);
1591 to
= gtk_text_iter_get_offset(&ito
);
1594 else // not multi-line
1596 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1599 haveSelection
= true;
1603 if (! haveSelection
)
1604 from
= to
= GetInsertionPoint();
1608 // exchange them to be compatible with wxMSW
1621 bool wxTextCtrl::IsEditable() const
1623 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1625 if ( IsMultiLine() )
1627 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1631 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1635 bool wxTextCtrl::IsModified() const
1640 void wxTextCtrl::Clear()
1642 SetValue( wxEmptyString
);
1645 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1647 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1649 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1651 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1653 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1654 event
.SetEventObject(this);
1655 event
.SetString(GetValue());
1656 if ( GetEventHandler()->ProcessEvent(event
) )
1660 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1662 if ( IsSingleLine() )
1664 // This will invoke the dialog default action, such
1665 // as the clicking the default button.
1667 wxWindow
*top_frame
= m_parent
;
1668 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1669 top_frame
= top_frame
->GetParent();
1671 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1673 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1675 if (window
->default_widget
)
1677 gtk_widget_activate (window
->default_widget
);
1687 GtkWidget
* wxTextCtrl::GetConnectWidget()
1689 return GTK_WIDGET(m_text
);
1692 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1694 if ( IsMultiLine() )
1696 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1697 GTK_TEXT_WINDOW_TEXT
);
1701 return GTK_ENTRY(m_text
)->text_area
;
1705 // the font will change for subsequent text insertiongs
1706 bool wxTextCtrl::SetFont( const wxFont
&font
)
1708 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1710 if ( !wxTextCtrlBase::SetFont(font
) )
1712 // font didn't change, nothing to do
1716 if ( IsMultiLine() )
1718 SetUpdateFont(true);
1720 m_defaultStyle
.SetFont(font
);
1722 ChangeFontGlobally();
1728 void wxTextCtrl::ChangeFontGlobally()
1730 // this method is very inefficient and hence should be called as rarely as
1733 // TODO: it can be implemented much more efficiently for GTK2
1734 wxASSERT_MSG( IsMultiLine(),
1735 _T("shouldn't be called for single line controls") );
1737 wxString value
= GetValue();
1738 if ( !value
.empty() )
1740 SetUpdateFont(false);
1747 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1749 if ( !wxControl::SetForegroundColour(colour
) )
1752 // update default fg colour too
1753 m_defaultStyle
.SetTextColour(colour
);
1758 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1760 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1762 if ( !wxControl::SetBackgroundColour( colour
) )
1765 if (!m_backgroundColour
.Ok())
1768 // change active background color too
1769 m_defaultStyle
.SetBackgroundColour( colour
);
1774 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1776 if ( IsMultiLine() )
1778 if ( style
.IsDefault() )
1784 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1786 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1787 _T("invalid range in wxTextCtrl::SetStyle") );
1789 GtkTextIter starti
, endi
;
1790 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1791 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1793 // use the attributes from style which are set in it and fall back
1794 // first to the default style and then to the text control default
1795 // colours for the others
1796 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1798 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, attr
, &starti
, &endi
);
1804 // cannot do this for GTK+'s Entry widget
1808 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1810 gtk_widget_modify_style(m_text
, style
);
1813 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1818 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1823 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1828 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1833 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1838 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1840 event
.Enable( CanCut() );
1843 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1845 event
.Enable( CanCopy() );
1848 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1850 event
.Enable( CanPaste() );
1853 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1855 event
.Enable( CanUndo() );
1858 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1860 event
.Enable( CanRedo() );
1863 wxSize
wxTextCtrl::DoGetBestSize() const
1865 // FIXME should be different for multi-line controls...
1866 wxSize
ret( wxControl::DoGetBestSize() );
1867 wxSize
best(80, ret
.y
);
1868 CacheBestSize(best
);
1872 // ----------------------------------------------------------------------------
1874 // ----------------------------------------------------------------------------
1876 void wxTextCtrl::Freeze()
1878 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1880 if ( HasFlag(wxTE_MULTILINE
) )
1882 if (m_frozenness
++ == 0)
1884 // freeze textview updates and remove buffer
1885 g_signal_connect (m_text
, "expose_event",
1886 G_CALLBACK (gtk_text_exposed_callback
), this);
1887 g_signal_connect (m_widget
, "expose_event",
1888 G_CALLBACK (gtk_text_exposed_callback
), this);
1889 gtk_widget_set_sensitive(m_widget
, false);
1890 g_object_ref(m_buffer
);
1891 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1892 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1893 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1894 // gtk_text_view_set_buffer adds its own reference
1895 g_object_unref(buf_new
);
1896 // This mark should be deleted when the buffer is changed,
1897 // but it's not (in GTK+ up to at least 2.10.6).
1898 // Otherwise these anonymous marks start to build up in the buffer,
1899 // and Freeze takes longer and longer each time it is called.
1900 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1901 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1906 void wxTextCtrl::Thaw()
1908 if ( HasFlag(wxTE_MULTILINE
) )
1910 wxCHECK_RET(m_frozenness
!= 0, _T("Thaw() without matching Freeze()"));
1912 if (--m_frozenness
== 0)
1914 // Reattach buffer and thaw textview updates
1915 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1916 g_object_unref(m_buffer
);
1917 gtk_widget_set_sensitive(m_widget
, true);
1918 g_signal_handlers_disconnect_by_func (m_widget
,
1919 (gpointer
) gtk_text_exposed_callback
, this);
1920 g_signal_handlers_disconnect_by_func (m_text
,
1921 (gpointer
) gtk_text_exposed_callback
, this);
1926 // ----------------------------------------------------------------------------
1927 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1928 // ----------------------------------------------------------------------------
1930 // FIXME: when dragging on a link the sample gets an "Unknown event".
1931 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1932 // a buggy sample, or something else
1933 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1936 if( !HasFlag(wxTE_AUTO_URL
) )
1940 GtkTextIter start
, end
;
1941 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1944 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1945 event
.GetX(), event
.GetY(), &x
, &y
);
1947 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1948 if (!gtk_text_iter_has_tag(&end
, tag
))
1950 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1951 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1955 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1956 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1959 if(!gtk_text_iter_begins_tag(&start
, tag
))
1960 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1961 if(!gtk_text_iter_ends_tag(&end
, tag
))
1962 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1964 // Native context menu is probably not desired on an URL.
1965 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1966 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1969 wxTextUrlEvent
url_event(m_windowId
, event
,
1970 gtk_text_iter_get_offset(&start
),
1971 gtk_text_iter_get_offset(&end
));
1973 InitCommandEvent(url_event
);
1974 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1975 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1976 GetEventHandler()->ProcessEvent(url_event
);
1979 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1981 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1983 // GtkTextView starts a drag operation when left mouse button is pressed
1984 // and ends it when it is released and if it doesn't get the release event
1985 // the next click on a control results in an assertion failure inside
1986 // gtk_text_view_start_selection_drag() which simply *kills* the program
1987 // without anything we can do about it, so always let GTK+ have this event
1988 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1993 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1995 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);