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"
15 #include "wx/textctrl.h"
21 #include "wx/settings.h"
25 #include "wx/strconv.h"
26 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
28 #include <sys/types.h>
32 #include "wx/gtk/private.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");
56 // remove all tags starting with the given prefix from the start..end range
58 wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer
*text_buffer
,
63 gulong remove_handler_id
= g_signal_connect
67 G_CALLBACK(wxGtkOnRemoveTag
),
70 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
71 g_signal_handler_disconnect(text_buffer
, remove_handler_id
);
74 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
75 GtkTextBuffer
*text_buffer
,
76 const wxTextAttr
& attr
,
80 static gchar buf
[1024];
85 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFONT", start
, end
);
87 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
88 wxGtkString
font_string(pango_font_description_to_string(font_description
));
89 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
90 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
93 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
94 "font-desc", font_description
,
96 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
98 if (attr
.GetFont().GetUnderlined())
100 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
101 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
104 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
105 "underline-set", TRUE
,
106 "underline", PANGO_UNDERLINE_SINGLE
,
108 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
112 if (attr
.HasTextColour())
114 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFORECOLOR", start
, end
);
116 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
117 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
118 colFg
->red
, colFg
->green
, colFg
->blue
);
119 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
122 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
123 "foreground-gdk", colFg
, NULL
);
124 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
127 if (attr
.HasBackgroundColour())
129 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXBACKCOLOR", start
, end
);
131 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
132 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
133 colBg
->red
, colBg
->green
, colBg
->blue
);
134 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
137 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
138 "background-gdk", colBg
, NULL
);
139 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
142 if (attr
.HasAlignment())
144 GtkTextIter para_start
, para_end
= *end
;
145 gtk_text_buffer_get_iter_at_line( text_buffer
,
147 gtk_text_iter_get_line(start
) );
148 gtk_text_iter_forward_line(¶_end
);
150 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXALIGNMENT", start
, end
);
152 GtkJustification align
;
153 switch (attr
.GetAlignment())
156 align
= GTK_JUSTIFY_LEFT
;
158 case wxTEXT_ALIGNMENT_RIGHT
:
159 align
= GTK_JUSTIFY_RIGHT
;
161 case wxTEXT_ALIGNMENT_CENTER
:
162 align
= GTK_JUSTIFY_CENTER
;
164 // gtk+ doesn't support justify before gtk+-2.11.0 with pango-1.17 being available
165 // (but if new enough pango isn't available it's a mere gtk warning)
166 #if GTK_CHECK_VERSION(2,11,0)
167 case wxTEXT_ALIGNMENT_JUSTIFIED
:
168 if (!gtk_check_version(2,11,0))
169 align
= GTK_JUSTIFY_FILL
;
171 align
= GTK_JUSTIFY_LEFT
;
176 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
177 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
180 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
181 "justification", align
, NULL
);
182 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
185 if (attr
.HasLeftIndent())
187 // Indentation attribute
189 // Clear old indentation tags
190 GtkTextIter para_start
, para_end
= *end
;
191 gtk_text_buffer_get_iter_at_line( text_buffer
,
193 gtk_text_iter_get_line(start
) );
194 gtk_text_iter_forward_line(¶_end
);
196 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", start
, end
);
198 // Convert indent from 1/10th of a mm into pixels
200 #if GTK_CHECK_VERSION(2,2,0)
201 if (!gtk_check_version(2,2,0))
202 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
203 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
206 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
208 const int indent
= (int)(factor
* attr
.GetLeftIndent());
209 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
217 gsubindent
= -subIndent
;
221 gindent
= -subIndent
;
225 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
226 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
229 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
230 "left-margin", gindent
, "indent", gsubindent
, NULL
);
231 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
239 GtkTextIter para_start
, para_end
= *end
;
240 gtk_text_buffer_get_iter_at_line( text_buffer
,
242 gtk_text_iter_get_line(start
) );
243 gtk_text_iter_forward_line(¶_end
);
245 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", start
, end
);
247 const wxArrayInt
& tabs
= attr
.GetTabs();
249 wxString tagname
= _T("WXTABS");
250 g_snprintf(buf
, sizeof(buf
), "WXTABS");
251 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
252 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
254 const wxWX2MBbuf buftag
= tagname
.utf8_str();
256 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
260 // Factor to convert from 1/10th of a mm into pixels
262 #if GTK_CHECK_VERSION(2,2,0)
263 if (!gtk_check_version(2,2,0))
264 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
265 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
268 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
270 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
271 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
272 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
273 tag
= gtk_text_buffer_create_tag( text_buffer
, buftag
,
274 "tabs", tabArray
, NULL
);
275 pango_tab_array_free(tabArray
);
277 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
281 static void wxGtkTextInsert(GtkWidget
*text
,
282 GtkTextBuffer
*text_buffer
,
283 const wxTextAttr
& attr
,
284 const wxCharBuffer
& buffer
)
288 GtkTextIter iter
, start
;
290 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
291 gtk_text_buffer_get_insert (text_buffer
) );
292 start_offset
= gtk_text_iter_get_offset (&iter
);
293 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
295 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
297 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
300 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
304 au_apply_tag_callback(GtkTextBuffer
*buffer
,
310 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
311 g_signal_stop_emission_by_name (buffer
, "apply_tag");
315 //-----------------------------------------------------------------------------
316 // GtkTextCharPredicates for gtk_text_iter_*_find_char
317 //-----------------------------------------------------------------------------
321 pred_whitespace (gunichar ch
, gpointer user_data
)
323 return g_unichar_isspace(ch
);
329 pred_non_whitespace (gunichar ch
, gpointer user_data
)
331 return !g_unichar_isspace(ch
);
337 pred_nonpunct (gunichar ch
, gpointer user_data
)
339 return !g_unichar_ispunct(ch
);
345 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
347 return !g_unichar_ispunct(ch
) || ch
== '/';
351 //-----------------------------------------------------------------------------
352 // Check for links between s and e and correct tags as necessary
353 //-----------------------------------------------------------------------------
355 // This function should be made match better while being efficient at one point.
356 // Most probably with a row of regular expressions.
359 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
361 static const char *URIPrefixes
[] =
379 GtkTextIter start
= *s
, end
= *e
;
380 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
382 // Get our special link tag
383 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
385 // Get rid of punctuation from beginning and end.
386 // Might want to move this to au_check_range if an improved link checking doesn't
387 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
388 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
389 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
391 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
392 gtk_text_iter_forward_char(&end
);
394 wxGtkString
text(gtk_text_iter_get_text( &start
, &end
));
395 size_t len
= strlen(text
), prefix_len
;
398 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
400 prefix_len
= strlen(URIPrefixes
[n
]);
401 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
405 if(n
< WXSIZEOF(URIPrefixes
))
407 gulong signal_id
= g_signal_handler_find (buffer
,
408 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
410 (gpointer
)au_apply_tag_callback
, NULL
);
412 g_signal_handler_block (buffer
, signal_id
);
413 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
414 g_signal_handler_unblock (buffer
, signal_id
);
421 au_check_range(GtkTextIter
*s
,
422 GtkTextIter
*range_end
)
424 GtkTextIter range_start
= *s
;
425 GtkTextIter word_end
;
426 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
427 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
429 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
431 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
432 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
434 while(!gtk_text_iter_equal(&range_start
, range_end
))
436 word_end
= range_start
;
437 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
439 // Now we should have a word delimited by range_start and word_end, correct link tags
440 au_check_word(&range_start
, &word_end
);
442 range_start
= word_end
;
443 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
448 //-----------------------------------------------------------------------------
449 // "insert-text" for GtkTextBuffer
450 //-----------------------------------------------------------------------------
454 au_insert_text_callback(GtkTextBuffer
*buffer
,
460 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
463 GtkTextIter start
= *end
;
464 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
466 GtkTextIter line_start
= start
;
467 GtkTextIter line_end
= *end
;
468 GtkTextIter words_start
= start
;
469 GtkTextIter words_end
= *end
;
471 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
472 gtk_text_iter_forward_to_line_end(&line_end
);
473 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
474 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
476 au_check_range(&words_start
, &words_end
);
480 //-----------------------------------------------------------------------------
481 // "delete-range" for GtkTextBuffer
482 //-----------------------------------------------------------------------------
486 au_delete_range_callback(GtkTextBuffer
*buffer
,
491 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
494 GtkTextIter line_start
= *start
, line_end
= *end
;
496 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
497 gtk_text_iter_forward_to_line_end(&line_end
);
498 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
499 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
501 au_check_range(start
, end
);
506 //-----------------------------------------------------------------------------
508 //-----------------------------------------------------------------------------
512 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
514 if ( win
->IgnoreTextUpdate() )
517 if (!win
->m_hasVMT
) return;
519 if ( win
->MarkDirtyOnChange() )
522 win
->SendTextUpdatedEvent();
526 //-----------------------------------------------------------------------------
527 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
528 //-----------------------------------------------------------------------------
530 // common part of the event handlers below
532 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
533 wxEventType eventType
, const gchar
* signal_name
)
535 wxClipboardTextEvent
event( eventType
, win
->GetId() );
536 event
.SetEventObject( win
);
537 if ( win
->GetEventHandler()->ProcessEvent( event
) )
539 // don't let the default processing to take place if we did something
540 // ourselves in the event handler
541 g_signal_stop_emission_by_name (widget
, signal_name
);
547 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
549 handle_text_clipboard_callback(
550 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
554 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
556 handle_text_clipboard_callback(
557 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
561 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
563 handle_text_clipboard_callback(
564 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
568 //-----------------------------------------------------------------------------
569 // "expose_event" from scrolled window and textview
570 //-----------------------------------------------------------------------------
574 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
581 //-----------------------------------------------------------------------------
583 //-----------------------------------------------------------------------------
585 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
587 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
588 EVT_CHAR(wxTextCtrl::OnChar
)
590 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
591 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
592 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
593 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
594 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
596 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
597 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
598 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
599 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
600 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
602 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
603 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
604 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
605 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
606 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
607 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
608 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
609 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
610 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
613 void wxTextCtrl::Init()
618 m_countUpdatesToIgnore
= 0;
620 SetUpdateFont(false);
624 m_showPositionOnThaw
= NULL
;
625 m_gdkHandCursor
= NULL
;
626 m_gdkXTermCursor
= NULL
;
629 wxTextCtrl::~wxTextCtrl()
632 gdk_cursor_unref(m_gdkHandCursor
);
634 gdk_cursor_unref(m_gdkXTermCursor
);
637 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
639 const wxString
&value
,
643 const wxValidator
& validator
,
644 const wxString
&name
)
648 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
651 bool wxTextCtrl::Create( wxWindow
*parent
,
653 const wxString
&value
,
657 const wxValidator
& validator
,
658 const wxString
&name
)
660 if (!PreCreation( parent
, pos
, size
) ||
661 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
663 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
667 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
672 m_text
= gtk_text_view_new();
674 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
676 // create "ShowPosition" marker
678 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
679 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
681 // create scrolled window
682 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
683 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
684 GTK_POLICY_AUTOMATIC
,
685 style
& wxTE_NO_VSCROLL
687 : GTK_POLICY_AUTOMATIC
);
688 // for ScrollLines/Pages
689 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
691 // Insert view into scrolled window
692 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
696 GtkScrolledWindowSetBorder(m_widget
, style
);
698 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
700 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
704 // a single-line text control: no need for scrollbars
706 m_text
= gtk_entry_new();
708 if (style
& wxNO_BORDER
)
709 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
712 m_parent
->DoAddChild( this );
714 m_focusWidget
= m_text
;
720 gtk_widget_show(m_text
);
723 // We want to be notified about text changes.
726 g_signal_connect (m_buffer
, "changed",
727 G_CALLBACK (gtk_text_changed_callback
), this);
731 g_signal_connect (m_text
, "changed",
732 G_CALLBACK (gtk_text_changed_callback
), this);
740 if (style
& wxTE_PASSWORD
)
743 if (style
& wxTE_READONLY
)
746 // left justification (alignment) is the default anyhow
747 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
748 GTKSetJustification();
752 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
753 if (style
& wxTE_AUTO_URL
)
755 GtkTextIter start
, end
;
756 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
757 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
759 // We create our wxUrl tag here for slight efficiency gain - we
760 // don't have to check for the tag existance in callbacks,
761 // hereby it's guaranteed to exist.
762 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
763 "foreground", "blue",
764 "underline", PANGO_UNDERLINE_SINGLE
,
767 // Check for URLs after each text change
768 g_signal_connect_after (m_buffer
, "insert_text",
769 G_CALLBACK (au_insert_text_callback
), this);
770 g_signal_connect_after (m_buffer
, "delete_range",
771 G_CALLBACK (au_delete_range_callback
), this);
773 // Block all wxUrl tag applying unless we do it ourselves, in which case we
774 // block this callback temporarily. This takes care of gtk+ internal
775 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
776 // which is undesired because only a part of the URL might be copied.
777 // The insert-text signal emitted inside it will take care of newly formed
778 // or wholly copied URLs.
779 g_signal_connect (m_buffer
, "apply_tag",
780 G_CALLBACK (au_apply_tag_callback
), NULL
);
782 // Check for URLs in the initial string passed to Create
783 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
784 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
785 au_check_range(&start
, &end
);
789 g_signal_connect (m_text
, "copy-clipboard",
790 G_CALLBACK (gtk_copy_clipboard_callback
), this);
791 g_signal_connect (m_text
, "cut-clipboard",
792 G_CALLBACK (gtk_cut_clipboard_callback
), this);
793 g_signal_connect (m_text
, "paste-clipboard",
794 G_CALLBACK (gtk_paste_clipboard_callback
), this);
796 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
801 GtkEditable
*wxTextCtrl::GetEditable() const
803 wxCHECK_MSG( IsSingleLine(), NULL
, "shouldn't be called for multiline" );
805 return GTK_EDITABLE(m_text
);
808 // ----------------------------------------------------------------------------
810 // ----------------------------------------------------------------------------
812 void wxTextCtrl::GTKSetEditable()
814 gboolean editable
= !HasFlag(wxTE_READONLY
);
815 if ( IsSingleLine() )
816 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
818 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
821 void wxTextCtrl::GTKSetVisibility()
823 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
824 if ( IsSingleLine() )
825 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
828 void wxTextCtrl::GTKSetWrapMode()
830 // no wrapping in single line controls
831 if ( !IsMultiLine() )
834 // translate wx wrapping style to GTK+
836 if ( HasFlag( wxTE_DONTWRAP
) )
837 wrap
= GTK_WRAP_NONE
;
838 else if ( HasFlag( wxTE_CHARWRAP
) )
839 wrap
= GTK_WRAP_CHAR
;
840 else if ( HasFlag( wxTE_WORDWRAP
) )
841 wrap
= GTK_WRAP_WORD
;
842 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
844 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
846 if ( !gtk_check_version(2,4,0) )
848 wrap
= GTK_WRAP_WORD_CHAR
;
851 #endif // __WXGTK24__
852 wrap
= GTK_WRAP_WORD
;
855 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
858 void wxTextCtrl::GTKSetJustification()
862 GtkJustification just
;
863 if ( HasFlag(wxTE_RIGHT
) )
864 just
= GTK_JUSTIFY_RIGHT
;
865 else if ( HasFlag(wxTE_CENTRE
) )
866 just
= GTK_JUSTIFY_CENTER
;
867 else // wxTE_LEFT == 0
868 just
= GTK_JUSTIFY_LEFT
;
870 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
875 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
876 if (!gtk_check_version(2,4,0))
879 if ( HasFlag(wxTE_RIGHT
) )
881 else if ( HasFlag(wxTE_CENTRE
) )
886 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
888 #endif // __WXGTK24__
893 void wxTextCtrl::SetWindowStyleFlag(long style
)
895 long styleOld
= GetWindowStyleFlag();
897 wxTextCtrlBase::SetWindowStyleFlag(style
);
899 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
902 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
905 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
906 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
909 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
910 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
911 GTKSetJustification();
914 // ----------------------------------------------------------------------------
916 // ----------------------------------------------------------------------------
918 wxString
wxTextCtrl::GetValue() const
920 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
925 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
927 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
928 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
930 return wxGTK_CONV_BACK(text
);
934 return wxTextEntry::GetValue();
938 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
940 // GTK+ uses UTF-8 internally, we need to convert to it but from which
943 // first check the default text style (we intentionally don't check the
944 // style for the current position as it doesn't make sense for SetValue())
945 const wxTextAttr
& style
= GetDefaultStyle();
946 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
947 : wxFONTENCODING_SYSTEM
;
949 // fall back to the controls font if no style
950 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
951 enc
= GetFont().GetEncoding();
956 bool wxTextCtrl::IsEmpty() const
959 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
961 return wxTextEntry::IsEmpty();
964 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
966 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
970 if ( !IsMultiLine() )
972 wxTextEntry::DoSetValue(value
, flags
);
976 wxFontEncoding enc
= m_defaultStyle
.HasFont()
977 ? m_defaultStyle
.GetFont().GetEncoding()
978 : wxFONTENCODING_SYSTEM
;
979 if ( enc
== wxFONTENCODING_SYSTEM
)
980 enc
= GetTextEncoding();
982 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
985 // see comment in WriteText() as to why we must warn the user about
987 wxLogWarning(_("Failed to set text in the text control."));
991 if ( !(flags
& SetValue_SendEvent
) )
993 EnableTextChangedEvents(false);
996 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
998 if ( !m_defaultStyle
.IsDefault() )
1000 GtkTextIter start
, end
;
1001 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1002 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1006 if ( !(flags
& SetValue_SendEvent
) )
1008 EnableTextChangedEvents(true);
1011 // This was added after discussion on the list
1012 SetInsertionPoint(0);
1015 void wxTextCtrl::WriteText( const wxString
&text
)
1017 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1019 // we're changing the text programmatically
1020 DontMarkDirtyOnNextChange();
1022 if ( !IsMultiLine() )
1024 wxTextEntry::WriteText(text
);
1028 // check if we have a specific style for the current position
1029 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1031 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1033 enc
= style
.GetFont().GetEncoding();
1036 if ( enc
== wxFONTENCODING_SYSTEM
)
1037 enc
= GetTextEncoding();
1039 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1042 // we must log an error here as losing the text like this can be a
1043 // serious problem (e.g. imagine the document edited by user being
1044 // empty instead of containing the correct text)
1045 wxLogWarning(_("Failed to insert text in the control."));
1049 // First remove the selection if there is one
1050 // TODO: Is there an easier GTK specific way to do this?
1052 GetSelection(&from
, &to
);
1057 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1059 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1060 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1061 // won't work when frozen, text view is not using m_buffer then
1062 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1064 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1065 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1069 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1072 if ( IsMultiLine() )
1075 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1077 GtkTextIter end
= line
;
1078 // avoid skipping to the next line end if this one is empty
1079 if ( !gtk_text_iter_ends_line(&line
) )
1080 gtk_text_iter_forward_to_line_end(&end
);
1082 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1083 result
= wxGTK_CONV_BACK(text
);
1088 result
= GetValue();
1093 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1095 /* If you implement this, don't forget to update the documentation!
1096 * (file docs/latex/wx/text.tex) */
1097 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1100 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1102 if ( IsMultiLine() )
1106 if (pos
> GetLastPosition())
1109 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1112 *y
= gtk_text_iter_get_line(&iter
);
1114 *x
= gtk_text_iter_get_line_offset(&iter
);
1116 else // single line control
1118 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1127 // index out of bounds
1135 long wxTextCtrl::XYToPosition(long x
, long y
) const
1137 if ( IsSingleLine() )
1141 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1144 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1145 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1148 return gtk_text_iter_get_offset(&iter
) + x
;
1151 int wxTextCtrl::GetLineLength(long lineNo
) const
1153 if ( IsMultiLine() )
1155 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1156 if (lineNo
> last_line
)
1160 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1161 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1162 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1166 wxString str
= GetLineText (lineNo
);
1167 return (int) str
.length();
1171 int wxTextCtrl::GetNumberOfLines() const
1173 if ( IsMultiLine() )
1175 return gtk_text_buffer_get_line_count( m_buffer
);
1183 void wxTextCtrl::SetInsertionPoint( long pos
)
1185 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1187 if ( IsMultiLine() )
1190 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1191 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1192 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1194 // defer until Thaw, text view is not using m_buffer now
1195 m_showPositionOnThaw
= mark
;
1197 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1201 wxTextEntry::SetInsertionPoint(pos
);
1205 void wxTextCtrl::SetEditable( bool editable
)
1207 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1209 if ( IsMultiLine() )
1211 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1215 wxTextEntry::SetEditable(editable
);
1219 bool wxTextCtrl::Enable( bool enable
)
1221 if (!wxWindowBase::Enable(enable
))
1227 gtk_widget_set_sensitive( m_text
, enable
);
1232 // wxGTK-specific: called recursively by Enable,
1233 // to give widgets an opportunity to correct their colours after they
1234 // have been changed by Enable
1235 void wxTextCtrl::OnEnabled( bool enable
)
1237 // If we have a custom background colour, we use this colour in both
1238 // disabled and enabled mode, or we end up with a different colour under the
1240 wxColour oldColour
= GetBackgroundColour();
1243 // Need to set twice or it'll optimize the useful stuff out
1244 if (oldColour
== * wxWHITE
)
1245 SetBackgroundColour(*wxBLACK
);
1247 SetBackgroundColour(*wxWHITE
);
1248 SetBackgroundColour(oldColour
);
1252 void wxTextCtrl::MarkDirty()
1257 void wxTextCtrl::DiscardEdits()
1262 // ----------------------------------------------------------------------------
1264 // ----------------------------------------------------------------------------
1266 void wxTextCtrl::EnableTextChangedEvents(bool enable
)
1270 g_signal_handlers_unblock_by_func(GetTextObject(),
1271 (gpointer
)gtk_text_changed_callback
, this);
1273 else // disable events
1275 g_signal_handlers_block_by_func(GetTextObject(),
1276 (gpointer
)gtk_text_changed_callback
, this);
1280 bool wxTextCtrl::IgnoreTextUpdate()
1282 if ( m_countUpdatesToIgnore
> 0 )
1284 m_countUpdatesToIgnore
--;
1292 bool wxTextCtrl::MarkDirtyOnChange()
1294 if ( m_dontMarkDirty
)
1296 m_dontMarkDirty
= false;
1304 void wxTextCtrl::SetSelection( long from
, long to
)
1306 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1308 if (from
== -1 && to
== -1)
1311 to
= GetValue().length();
1314 if ( IsMultiLine() )
1316 GtkTextIter fromi
, toi
;
1317 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1318 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1320 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1321 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1325 wxTextEntry::SetSelection(from
, to
);
1329 void wxTextCtrl::ShowPosition( long pos
)
1334 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1335 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1336 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1338 // defer until Thaw, text view is not using m_buffer now
1339 m_showPositionOnThaw
= mark
;
1341 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1345 wxTextCtrlHitTestResult
1346 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1348 if ( !IsMultiLine() )
1351 return wxTE_HT_UNKNOWN
;
1355 gtk_text_view_window_to_buffer_coords
1357 GTK_TEXT_VIEW(m_text
),
1358 GTK_TEXT_WINDOW_TEXT
,
1364 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1366 *pos
= gtk_text_iter_get_offset(&iter
);
1368 return wxTE_HT_ON_TEXT
;
1371 long wxTextCtrl::GetInsertionPoint() const
1373 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1375 if ( IsMultiLine() )
1377 // There is no direct accessor for the cursor, but
1378 // internally, the cursor is the "mark" called
1379 // "insert" in the text view's btree structure.
1381 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1383 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1385 return gtk_text_iter_get_offset( &cursor
);
1389 return wxTextEntry::GetInsertionPoint();
1393 wxTextPos
wxTextCtrl::GetLastPosition() const
1395 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1399 if ( IsMultiLine() )
1402 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1404 pos
= gtk_text_iter_get_offset( &end
);
1408 pos
= wxTextEntry::GetLastPosition();
1414 void wxTextCtrl::Remove( long from
, long to
)
1416 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1418 if ( IsMultiLine() )
1420 GtkTextIter fromi
, toi
;
1421 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1422 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1424 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1428 wxTextEntry::Remove(from
, to
);
1432 void wxTextCtrl::Cut()
1434 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1436 if ( IsMultiLine() )
1437 g_signal_emit_by_name (m_text
, "cut-clipboard");
1442 void wxTextCtrl::Copy()
1444 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1446 if ( IsMultiLine() )
1447 g_signal_emit_by_name (m_text
, "copy-clipboard");
1449 wxTextEntry::Copy();
1452 void wxTextCtrl::Paste()
1454 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1456 if ( IsMultiLine() )
1457 g_signal_emit_by_name (m_text
, "paste-clipboard");
1459 wxTextEntry::Paste();
1462 // If the return values from and to are the same, there is no
1464 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1466 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1468 if ( !IsMultiLine() )
1470 wxTextEntry::GetSelection(fromOut
, toOut
);
1476 GtkTextIter ifrom
, ito
;
1477 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1479 from
= gtk_text_iter_get_offset(&ifrom
);
1480 to
= gtk_text_iter_get_offset(&ito
);
1484 // exchange them to be compatible with wxMSW
1490 else // no selection
1493 to
= GetInsertionPoint();
1503 bool wxTextCtrl::IsEditable() const
1505 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1507 if ( IsMultiLine() )
1509 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1513 return wxTextEntry::IsEditable();
1517 bool wxTextCtrl::IsModified() const
1522 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1524 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1526 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1528 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1530 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1531 event
.SetEventObject(this);
1532 event
.SetString(GetValue());
1533 if ( GetEventHandler()->ProcessEvent(event
) )
1537 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1539 if ( IsSingleLine() )
1541 // This will invoke the dialog default action, such
1542 // as the clicking the default button.
1544 wxWindow
*top_frame
= m_parent
;
1545 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1546 top_frame
= top_frame
->GetParent();
1548 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1550 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1552 if (window
->default_widget
)
1554 gtk_widget_activate (window
->default_widget
);
1564 GtkWidget
* wxTextCtrl::GetConnectWidget()
1566 return GTK_WIDGET(m_text
);
1569 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1571 if ( IsMultiLine() )
1573 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1574 GTK_TEXT_WINDOW_TEXT
);
1578 return GTK_ENTRY(m_text
)->text_area
;
1582 // the font will change for subsequent text insertiongs
1583 bool wxTextCtrl::SetFont( const wxFont
&font
)
1585 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1587 if ( !wxTextCtrlBase::SetFont(font
) )
1589 // font didn't change, nothing to do
1593 if ( IsMultiLine() )
1595 SetUpdateFont(true);
1597 m_defaultStyle
.SetFont(font
);
1599 ChangeFontGlobally();
1605 void wxTextCtrl::ChangeFontGlobally()
1607 // this method is very inefficient and hence should be called as rarely as
1610 // TODO: it can be implemented much more efficiently for GTK2
1611 wxASSERT_MSG( IsMultiLine(),
1612 _T("shouldn't be called for single line controls") );
1614 wxString value
= GetValue();
1615 if ( !value
.empty() )
1617 SetUpdateFont(false);
1624 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1626 if ( !wxControl::SetForegroundColour(colour
) )
1629 // update default fg colour too
1630 m_defaultStyle
.SetTextColour(colour
);
1635 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1637 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1639 if ( !wxControl::SetBackgroundColour( colour
) )
1642 if (!m_backgroundColour
.Ok())
1645 // change active background color too
1646 m_defaultStyle
.SetBackgroundColour( colour
);
1651 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1653 if ( IsMultiLine() )
1655 if ( style
.IsDefault() )
1661 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1663 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1664 _T("invalid range in wxTextCtrl::SetStyle") );
1666 GtkTextIter starti
, endi
;
1667 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1668 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1670 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1674 //else: single line text controls don't support styles
1679 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1681 gtk_widget_modify_style(m_text
, style
);
1684 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1689 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1694 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1699 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1704 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1709 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1711 event
.Enable( CanCut() );
1714 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1716 event
.Enable( CanCopy() );
1719 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1721 event
.Enable( CanPaste() );
1724 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1726 event
.Enable( CanUndo() );
1729 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1731 event
.Enable( CanRedo() );
1734 wxSize
wxTextCtrl::DoGetBestSize() const
1736 // FIXME should be different for multi-line controls...
1737 wxSize
ret( wxControl::DoGetBestSize() );
1738 wxSize
best(80, ret
.y
);
1739 CacheBestSize(best
);
1743 // ----------------------------------------------------------------------------
1745 // ----------------------------------------------------------------------------
1747 void wxTextCtrl::Freeze()
1749 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1751 if ( HasFlag(wxTE_MULTILINE
) )
1753 if (m_freezeCount
++ == 0)
1755 // freeze textview updates and remove buffer
1756 g_signal_connect (m_text
, "expose_event",
1757 G_CALLBACK (gtk_text_exposed_callback
), this);
1758 g_signal_connect (m_widget
, "expose_event",
1759 G_CALLBACK (gtk_text_exposed_callback
), this);
1760 gtk_widget_set_sensitive(m_widget
, false);
1761 g_object_ref(m_buffer
);
1762 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1763 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1764 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1765 // gtk_text_view_set_buffer adds its own reference
1766 g_object_unref(buf_new
);
1767 // This mark should be deleted when the buffer is changed,
1768 // but it's not (in GTK+ up to at least 2.10.6).
1769 // Otherwise these anonymous marks start to build up in the buffer,
1770 // and Freeze takes longer and longer each time it is called.
1771 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1772 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1777 void wxTextCtrl::Thaw()
1779 if ( HasFlag(wxTE_MULTILINE
) )
1781 wxCHECK_RET(m_freezeCount
!= 0, _T("Thaw() without matching Freeze()"));
1783 if (--m_freezeCount
== 0)
1785 // Reattach buffer and thaw textview updates
1786 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1787 g_object_unref(m_buffer
);
1788 gtk_widget_set_sensitive(m_widget
, true);
1789 g_signal_handlers_disconnect_by_func (m_widget
,
1790 (gpointer
) gtk_text_exposed_callback
, this);
1791 g_signal_handlers_disconnect_by_func (m_text
,
1792 (gpointer
) gtk_text_exposed_callback
, this);
1793 if (m_showPositionOnThaw
!= NULL
)
1795 gtk_text_view_scroll_mark_onscreen(
1796 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1797 m_showPositionOnThaw
= NULL
;
1803 // ----------------------------------------------------------------------------
1804 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1805 // ----------------------------------------------------------------------------
1807 // FIXME: when dragging on a link the sample gets an "Unknown event".
1808 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1809 // a buggy sample, or something else
1810 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1813 if( !HasFlag(wxTE_AUTO_URL
) )
1817 GtkTextIter start
, end
;
1818 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1821 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1822 event
.GetX(), event
.GetY(), &x
, &y
);
1824 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1825 if (!gtk_text_iter_has_tag(&end
, tag
))
1827 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1828 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1832 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1833 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1836 if(!gtk_text_iter_begins_tag(&start
, tag
))
1837 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1838 if(!gtk_text_iter_ends_tag(&end
, tag
))
1839 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1841 // Native context menu is probably not desired on an URL.
1842 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1843 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1846 wxTextUrlEvent
url_event(m_windowId
, event
,
1847 gtk_text_iter_get_offset(&start
),
1848 gtk_text_iter_get_offset(&end
));
1850 InitCommandEvent(url_event
);
1851 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1852 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1853 GetEventHandler()->ProcessEvent(url_event
);
1856 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1858 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1860 // GtkTextView starts a drag operation when left mouse button is pressed
1861 // and ends it when it is released and if it doesn't get the release event
1862 // the next click on a control results in an assertion failure inside
1863 // gtk_text_view_start_selection_drag() which simply *kills* the program
1864 // without anything we can do about it, so always let GTK+ have this event
1865 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1870 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1872 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
1875 #endif // wxUSE_TEXTCTRL