1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
19 #include "wx/settings.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
26 #include <sys/types.h>
30 #include "wx/gtk/private.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
37 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
44 g_object_get (tag
, "name", &name
, NULL
);
46 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
47 // anonymous tag or not starting with prefix - don't remove
48 g_signal_stop_emission_by_name (buffer
, "remove_tag");
54 // remove all tags starting with the given prefix from the start..end range
56 wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer
*text_buffer
,
61 gulong remove_handler_id
= g_signal_connect
65 G_CALLBACK(wxGtkOnRemoveTag
),
68 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
69 g_signal_handler_disconnect(text_buffer
, remove_handler_id
);
72 static void wxGtkTextApplyTagsFromAttr(GtkWidget
*text
,
73 GtkTextBuffer
*text_buffer
,
74 const wxTextAttr
& attr
,
78 static gchar buf
[1024];
83 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFONT", start
, end
);
85 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
86 wxGtkString
font_string(pango_font_description_to_string(font_description
));
87 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
.c_str());
88 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
91 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
92 "font-desc", font_description
,
94 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
96 if (attr
.GetFont().GetUnderlined())
98 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
99 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
102 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
103 "underline-set", TRUE
,
104 "underline", PANGO_UNDERLINE_SINGLE
,
106 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
110 if (attr
.HasTextColour())
112 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXFORECOLOR", start
, end
);
114 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
115 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
116 colFg
->red
, colFg
->green
, colFg
->blue
);
117 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
120 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
121 "foreground-gdk", colFg
, NULL
);
122 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
125 if (attr
.HasBackgroundColour())
127 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXBACKCOLOR", start
, end
);
129 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
130 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
131 colBg
->red
, colBg
->green
, colBg
->blue
);
132 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
135 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
136 "background-gdk", colBg
, NULL
);
137 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
140 if (attr
.HasAlignment())
142 GtkTextIter para_start
, para_end
= *end
;
143 gtk_text_buffer_get_iter_at_line( text_buffer
,
145 gtk_text_iter_get_line(start
) );
146 gtk_text_iter_forward_line(¶_end
);
148 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXALIGNMENT", start
, end
);
150 GtkJustification align
;
151 switch (attr
.GetAlignment())
154 align
= GTK_JUSTIFY_LEFT
;
156 case wxTEXT_ALIGNMENT_RIGHT
:
157 align
= GTK_JUSTIFY_RIGHT
;
159 case wxTEXT_ALIGNMENT_CENTER
:
160 align
= GTK_JUSTIFY_CENTER
;
162 // gtk+ doesn't support justify as of gtk+-2.7.4
165 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
166 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
169 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
170 "justification", align
, NULL
);
171 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
174 if (attr
.HasLeftIndent())
176 // Indentation attribute
178 // Clear old indentation tags
179 GtkTextIter para_start
, para_end
= *end
;
180 gtk_text_buffer_get_iter_at_line( text_buffer
,
182 gtk_text_iter_get_line(start
) );
183 gtk_text_iter_forward_line(¶_end
);
185 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", start
, end
);
187 // Convert indent from 1/10th of a mm into pixels
189 #if GTK_CHECK_VERSION(2,2,0)
190 if (!gtk_check_version(2,2,0))
191 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
192 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
195 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
197 const int indent
= (int)(factor
* attr
.GetLeftIndent());
198 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
206 gsubindent
= -subIndent
;
210 gindent
= -subIndent
;
214 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
215 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
218 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
219 "left-margin", gindent
, "indent", gsubindent
, NULL
);
220 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
228 GtkTextIter para_start
, para_end
= *end
;
229 gtk_text_buffer_get_iter_at_line( text_buffer
,
231 gtk_text_iter_get_line(start
) );
232 gtk_text_iter_forward_line(¶_end
);
234 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", start
, end
);
236 const wxArrayInt
& tabs
= attr
.GetTabs();
238 wxString tagname
= _T("WXTABS");
239 g_snprintf(buf
, sizeof(buf
), "WXTABS");
240 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
241 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
243 const wxWX2MBbuf buf
= tagname
.mb_str(wxConvUTF8
);
245 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
249 // Factor to convert from 1/10th of a mm into pixels
251 #if GTK_CHECK_VERSION(2,2,0)
252 if (!gtk_check_version(2,2,0))
253 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
254 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
257 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
259 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
260 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
261 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
262 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
263 "tabs", tabArray
, NULL
);
264 pango_tab_array_free(tabArray
);
266 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
270 static void wxGtkTextInsert(GtkWidget
*text
,
271 GtkTextBuffer
*text_buffer
,
272 const wxTextAttr
& attr
,
273 const wxCharBuffer
& buffer
)
277 GtkTextIter iter
, start
;
279 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
280 gtk_text_buffer_get_insert (text_buffer
) );
281 start_offset
= gtk_text_iter_get_offset (&iter
);
282 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
284 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
286 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
289 // ----------------------------------------------------------------------------
290 // "insert_text" for GtkEntry
291 // ----------------------------------------------------------------------------
295 gtk_insert_text_callback(GtkEditable
*editable
,
296 const gchar
*new_text
,
297 gint new_text_length
,
301 // we should only be called if we have a max len limit at all
302 GtkEntry
*entry
= GTK_ENTRY (editable
);
304 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
306 // check that we don't overflow the max length limit
308 // FIXME: this doesn't work when we paste a string which is going to be
310 if ( entry
->text_length
== entry
->text_max_length
)
312 // we don't need to run the base class version at all
313 g_signal_stop_emission_by_name (editable
, "insert_text");
315 // remember that the next changed signal is to be ignored to avoid
316 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
317 win
->IgnoreNextTextUpdate();
319 // and generate the correct one ourselves
320 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
321 event
.SetEventObject(win
);
322 event
.SetString(win
->GetValue());
323 win
->GetEventHandler()->ProcessEvent( event
);
328 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
332 au_apply_tag_callback(GtkTextBuffer
*buffer
,
338 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
339 g_signal_stop_emission_by_name (buffer
, "apply_tag");
343 //-----------------------------------------------------------------------------
344 // GtkTextCharPredicates for gtk_text_iter_*_find_char
345 //-----------------------------------------------------------------------------
349 pred_whitespace (gunichar ch
, gpointer user_data
)
351 return g_unichar_isspace(ch
);
357 pred_non_whitespace (gunichar ch
, gpointer user_data
)
359 return !g_unichar_isspace(ch
);
365 pred_nonpunct (gunichar ch
, gpointer user_data
)
367 return !g_unichar_ispunct(ch
);
373 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
375 return !g_unichar_ispunct(ch
) || ch
== '/';
379 //-----------------------------------------------------------------------------
380 // Check for links between s and e and correct tags as necessary
381 //-----------------------------------------------------------------------------
383 // This function should be made match better while being efficient at one point.
384 // Most probably with a row of regular expressions.
387 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
389 static const char *URIPrefixes
[] =
407 GtkTextIter start
= *s
, end
= *e
;
408 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
410 // Get our special link tag
411 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
413 // Get rid of punctuation from beginning and end.
414 // Might want to move this to au_check_range if an improved link checking doesn't
415 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
416 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
417 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
419 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
420 gtk_text_iter_forward_char(&end
);
422 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
423 size_t len
= strlen(text
), prefix_len
;
426 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
428 prefix_len
= strlen(URIPrefixes
[n
]);
429 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
433 if(n
< WXSIZEOF(URIPrefixes
))
435 gulong signal_id
= g_signal_handler_find (buffer
,
436 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
438 (gpointer
)au_apply_tag_callback
, NULL
);
440 g_signal_handler_block (buffer
, signal_id
);
441 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
442 g_signal_handler_unblock (buffer
, signal_id
);
449 au_check_range(GtkTextIter
*s
,
450 GtkTextIter
*range_end
)
452 GtkTextIter range_start
= *s
;
453 GtkTextIter word_end
;
454 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
455 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
457 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
459 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
460 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
462 while(!gtk_text_iter_equal(&range_start
, range_end
))
464 word_end
= range_start
;
465 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
467 // Now we should have a word delimited by range_start and word_end, correct link tags
468 au_check_word(&range_start
, &word_end
);
470 range_start
= word_end
;
471 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
476 //-----------------------------------------------------------------------------
477 // "insert-text" for GtkTextBuffer
478 //-----------------------------------------------------------------------------
482 au_insert_text_callback(GtkTextBuffer
*buffer
,
488 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
491 GtkTextIter start
= *end
;
492 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
494 GtkTextIter line_start
= start
;
495 GtkTextIter line_end
= *end
;
496 GtkTextIter words_start
= start
;
497 GtkTextIter words_end
= *end
;
499 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
500 gtk_text_iter_forward_to_line_end(&line_end
);
501 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
502 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
504 au_check_range(&words_start
, &words_end
);
508 //-----------------------------------------------------------------------------
509 // "delete-range" for GtkTextBuffer
510 //-----------------------------------------------------------------------------
514 au_delete_range_callback(GtkTextBuffer
*buffer
,
519 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
522 GtkTextIter line_start
= *start
, line_end
= *end
;
524 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
525 gtk_text_iter_forward_to_line_end(&line_end
);
526 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
527 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
529 au_check_range(start
, end
);
534 //-----------------------------------------------------------------------------
536 //-----------------------------------------------------------------------------
540 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
542 if ( win
->IgnoreTextUpdate() )
545 if (!win
->m_hasVMT
) return;
547 if ( win
->MarkDirtyOnChange() )
550 win
->SendTextUpdatedEvent();
554 //-----------------------------------------------------------------------------
555 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
556 //-----------------------------------------------------------------------------
558 // common part of the event handlers below
560 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
561 wxEventType eventType
, const gchar
* signal_name
)
563 wxClipboardTextEvent
event( eventType
, win
->GetId() );
564 event
.SetEventObject( win
);
565 if ( win
->GetEventHandler()->ProcessEvent( event
) )
567 // don't let the default processing to take place if we did something
568 // ourselves in the event handler
569 g_signal_stop_emission_by_name (widget
, signal_name
);
575 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
577 handle_text_clipboard_callback(
578 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
582 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
584 handle_text_clipboard_callback(
585 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
589 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
591 handle_text_clipboard_callback(
592 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
596 //-----------------------------------------------------------------------------
597 // "expose_event" from scrolled window and textview
598 //-----------------------------------------------------------------------------
602 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
609 //-----------------------------------------------------------------------------
611 //-----------------------------------------------------------------------------
613 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
615 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
616 EVT_CHAR(wxTextCtrl::OnChar
)
618 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
619 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
620 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
621 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
622 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
624 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
625 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
626 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
627 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
628 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
630 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
631 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
632 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
633 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
634 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
635 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
636 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
637 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
638 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
641 void wxTextCtrl::Init()
646 m_countUpdatesToIgnore
= 0;
648 SetUpdateFont(false);
652 m_showPositionOnThaw
= NULL
;
653 m_gdkHandCursor
= NULL
;
654 m_gdkXTermCursor
= NULL
;
657 wxTextCtrl::~wxTextCtrl()
660 gdk_cursor_unref(m_gdkHandCursor
);
662 gdk_cursor_unref(m_gdkXTermCursor
);
665 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
667 const wxString
&value
,
671 const wxValidator
& validator
,
672 const wxString
&name
)
676 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
679 bool wxTextCtrl::Create( wxWindow
*parent
,
681 const wxString
&value
,
685 const wxValidator
& validator
,
686 const wxString
&name
)
690 if (!PreCreation( parent
, pos
, size
) ||
691 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
693 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
697 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
702 m_text
= gtk_text_view_new();
704 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
706 // create "ShowPosition" marker
708 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
709 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
711 // create scrolled window
712 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
713 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
714 GTK_POLICY_AUTOMATIC
,
715 style
& wxTE_NO_VSCROLL
717 : GTK_POLICY_AUTOMATIC
);
718 // for ScrollLines/Pages
719 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
721 // Insert view into scrolled window
722 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
726 GtkScrolledWindowSetBorder(m_widget
, style
);
728 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
730 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
734 // a single-line text control: no need for scrollbars
736 m_text
= gtk_entry_new();
738 if (style
& wxNO_BORDER
)
739 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
742 m_parent
->DoAddChild( this );
744 m_focusWidget
= m_text
;
750 gtk_widget_show(m_text
);
758 if (style
& wxTE_PASSWORD
)
761 if (style
& wxTE_READONLY
)
764 // left justification (alignment) is the default anyhow
765 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
766 GTKSetJustification();
768 // We want to be notified about text changes.
771 g_signal_connect (m_buffer
, "changed",
772 G_CALLBACK (gtk_text_changed_callback
), this);
774 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
775 if (style
& wxTE_AUTO_URL
)
777 GtkTextIter start
, end
;
778 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
779 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
781 // We create our wxUrl tag here for slight efficiency gain - we
782 // don't have to check for the tag existance in callbacks,
783 // hereby it's guaranteed to exist.
784 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
785 "foreground", "blue",
786 "underline", PANGO_UNDERLINE_SINGLE
,
789 // Check for URLs after each text change
790 g_signal_connect_after (m_buffer
, "insert_text",
791 G_CALLBACK (au_insert_text_callback
), this);
792 g_signal_connect_after (m_buffer
, "delete_range",
793 G_CALLBACK (au_delete_range_callback
), this);
795 // Block all wxUrl tag applying unless we do it ourselves, in which case we
796 // block this callback temporarily. This takes care of gtk+ internal
797 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
798 // which is undesired because only a part of the URL might be copied.
799 // The insert-text signal emitted inside it will take care of newly formed
800 // or wholly copied URLs.
801 g_signal_connect (m_buffer
, "apply_tag",
802 G_CALLBACK (au_apply_tag_callback
), NULL
);
804 // Check for URLs in the initial string passed to Create
805 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
806 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
807 au_check_range(&start
, &end
);
812 g_signal_connect (m_text
, "changed",
813 G_CALLBACK (gtk_text_changed_callback
), this);
816 g_signal_connect (m_text
, "copy-clipboard",
817 G_CALLBACK (gtk_copy_clipboard_callback
), this);
818 g_signal_connect (m_text
, "cut-clipboard",
819 G_CALLBACK (gtk_cut_clipboard_callback
), this);
820 g_signal_connect (m_text
, "paste-clipboard",
821 G_CALLBACK (gtk_paste_clipboard_callback
), this);
823 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
828 // ----------------------------------------------------------------------------
830 // ----------------------------------------------------------------------------
832 void wxTextCtrl::GTKSetEditable()
834 gboolean editable
= !HasFlag(wxTE_READONLY
);
835 if ( IsSingleLine() )
836 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
838 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
841 void wxTextCtrl::GTKSetVisibility()
843 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
844 if ( IsSingleLine() )
845 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
848 void wxTextCtrl::GTKSetWrapMode()
850 // no wrapping in single line controls
851 if ( !IsMultiLine() )
854 // translate wx wrapping style to GTK+
856 if ( HasFlag( wxTE_DONTWRAP
) )
857 wrap
= GTK_WRAP_NONE
;
858 else if ( HasFlag( wxTE_CHARWRAP
) )
859 wrap
= GTK_WRAP_CHAR
;
860 else if ( HasFlag( wxTE_WORDWRAP
) )
861 wrap
= GTK_WRAP_WORD
;
862 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
864 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
866 if ( !gtk_check_version(2,4,0) )
868 wrap
= GTK_WRAP_WORD_CHAR
;
871 #endif // __WXGTK24__
872 wrap
= GTK_WRAP_WORD
;
875 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
878 void wxTextCtrl::GTKSetJustification()
882 GtkJustification just
;
883 if ( HasFlag(wxTE_RIGHT
) )
884 just
= GTK_JUSTIFY_RIGHT
;
885 else if ( HasFlag(wxTE_CENTRE
) )
886 just
= GTK_JUSTIFY_CENTER
;
887 else // wxTE_LEFT == 0
888 just
= GTK_JUSTIFY_LEFT
;
890 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
895 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
896 if (!gtk_check_version(2,4,0))
899 if ( HasFlag(wxTE_RIGHT
) )
901 else if ( HasFlag(wxTE_CENTRE
) )
906 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
908 #endif // __WXGTK24__
913 void wxTextCtrl::SetWindowStyleFlag(long style
)
915 long styleOld
= GetWindowStyleFlag();
917 wxTextCtrlBase::SetWindowStyleFlag(style
);
919 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
922 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
925 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
926 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
929 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
930 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
931 GTKSetJustification();
934 // ----------------------------------------------------------------------------
936 // ----------------------------------------------------------------------------
938 wxString
wxTextCtrl::GetValue() const
940 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
946 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
948 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
949 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
951 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
957 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
958 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
966 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
968 // GTK+ uses UTF-8 internally, we need to convert to it but from which
971 // first check the default text style (we intentionally don't check the
972 // style for the current position as it doesn't make sense for SetValue())
973 const wxTextAttr
& style
= GetDefaultStyle();
974 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
975 : wxFONTENCODING_SYSTEM
;
977 // fall back to the controls font if no style
978 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
979 enc
= GetFont().GetEncoding();
984 bool wxTextCtrl::IsEmpty() const
987 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
989 return wxTextCtrlBase::IsEmpty();
992 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
994 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
996 // the control won't be modified any more as we programmatically replace
997 // all the existing text, so reset the flag and don't set it again (and do
998 // it now, before the text event handler is ran so that IsModified() called
999 // from there returns the expected value)
1001 DontMarkDirtyOnNextChange();
1003 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1004 ? m_defaultStyle
.GetFont().GetEncoding()
1005 : wxFONTENCODING_SYSTEM
;
1006 if ( enc
== wxFONTENCODING_SYSTEM
)
1007 enc
= GetTextEncoding();
1009 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1012 // see comment in WriteText() as to why we must warn the user about
1014 wxLogWarning(_("Failed to set text in the text control."));
1018 // if the control is not empty, two "changed" signals are emitted,
1019 // otherwise only one and we need to ignore either both or one of them
1020 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
1025 IgnoreNextTextUpdate(ignore
);
1027 if ( IsMultiLine() )
1029 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1031 if ( !m_defaultStyle
.IsDefault() )
1033 GtkTextIter start
, end
;
1034 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1035 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1041 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
1044 // if, for whatever reason, the callback wasn't called the expected number
1045 // of times, still reset the flags to the default values
1046 m_dontMarkDirty
= false;
1047 m_countUpdatesToIgnore
= 0;
1050 // GRG, Jun/2000: Changed this after a lot of discussion in
1051 // the lists. wxWidgets 2.2 will have a set of flags to
1052 // customize this behaviour.
1053 SetInsertionPoint(0);
1056 void wxTextCtrl::WriteText( const wxString
&text
)
1058 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1063 // check if we have a specific style for the current position
1064 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1066 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1068 enc
= style
.GetFont().GetEncoding();
1071 if ( enc
== wxFONTENCODING_SYSTEM
)
1072 enc
= GetTextEncoding();
1074 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1077 // we must log an error here as losing the text like this can be a
1078 // serious problem (e.g. imagine the document edited by user being
1079 // empty instead of containing the correct text)
1080 wxLogWarning(_("Failed to insert text in the control."));
1084 // we're changing the text programmatically
1085 DontMarkDirtyOnNextChange();
1087 if ( IsMultiLine() )
1089 // First remove the selection if there is one
1090 // TODO: Is there an easier GTK specific way to do this?
1092 GetSelection(&from
, &to
);
1097 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1099 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1100 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1101 // won't work when frozen, text view is not using m_buffer then
1102 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1104 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1105 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1110 // First remove the selection if there is one
1111 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1113 // This moves the cursor pos to behind the inserted text.
1114 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1116 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1118 // Bring entry's cursor uptodate.
1119 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1123 void wxTextCtrl::AppendText( const wxString
&text
)
1125 SetInsertionPointEnd();
1129 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1132 if ( IsMultiLine() )
1135 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1137 GtkTextIter end
= line
;
1138 // avoid skipping to the next line end if this one is empty
1139 if ( !gtk_text_iter_ends_line(&line
) )
1140 gtk_text_iter_forward_to_line_end(&end
);
1142 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1143 result
= wxGTK_CONV_BACK(text
);
1148 result
= GetValue();
1153 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1155 /* If you implement this, don't forget to update the documentation!
1156 * (file docs/latex/wx/text.tex) */
1157 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1160 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1162 if ( IsMultiLine() )
1166 if (pos
> GetLastPosition())
1169 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1172 *y
= gtk_text_iter_get_line(&iter
);
1174 *x
= gtk_text_iter_get_line_offset(&iter
);
1176 else // single line control
1178 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1187 // index out of bounds
1195 long wxTextCtrl::XYToPosition(long x
, long y
) const
1197 if ( IsSingleLine() )
1201 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1204 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1205 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1208 return gtk_text_iter_get_offset(&iter
) + x
;
1211 int wxTextCtrl::GetLineLength(long lineNo
) const
1213 if ( IsMultiLine() )
1215 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1216 if (lineNo
> last_line
)
1220 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1221 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1222 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1226 wxString str
= GetLineText (lineNo
);
1227 return (int) str
.length();
1231 int wxTextCtrl::GetNumberOfLines() const
1233 if ( IsMultiLine() )
1235 return gtk_text_buffer_get_line_count( m_buffer
);
1243 void wxTextCtrl::SetInsertionPoint( long pos
)
1245 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1247 if ( IsMultiLine() )
1250 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1251 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1252 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1254 // defer until Thaw, text view is not using m_buffer now
1255 m_showPositionOnThaw
= mark
;
1257 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1261 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1262 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1266 void wxTextCtrl::SetInsertionPointEnd()
1268 SetInsertionPoint(-1);
1271 void wxTextCtrl::SetEditable( bool editable
)
1273 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1275 if ( IsMultiLine() )
1277 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1281 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1285 bool wxTextCtrl::Enable( bool enable
)
1287 if (!wxWindowBase::Enable(enable
))
1293 if ( IsMultiLine() )
1295 SetEditable( enable
);
1299 gtk_widget_set_sensitive( m_text
, enable
);
1305 // wxGTK-specific: called recursively by Enable,
1306 // to give widgets an opportunity to correct their colours after they
1307 // have been changed by Enable
1308 void wxTextCtrl::OnEnabled( bool enable
)
1310 // If we have a custom background colour, we use this colour in both
1311 // disabled and enabled mode, or we end up with a different colour under the
1313 wxColour oldColour
= GetBackgroundColour();
1316 // Need to set twice or it'll optimize the useful stuff out
1317 if (oldColour
== * wxWHITE
)
1318 SetBackgroundColour(*wxBLACK
);
1320 SetBackgroundColour(*wxWHITE
);
1321 SetBackgroundColour(oldColour
);
1325 void wxTextCtrl::MarkDirty()
1330 void wxTextCtrl::DiscardEdits()
1335 // ----------------------------------------------------------------------------
1336 // max text length support
1337 // ----------------------------------------------------------------------------
1339 bool wxTextCtrl::IgnoreTextUpdate()
1341 if ( m_countUpdatesToIgnore
> 0 )
1343 m_countUpdatesToIgnore
--;
1351 bool wxTextCtrl::MarkDirtyOnChange()
1353 if ( m_dontMarkDirty
)
1355 m_dontMarkDirty
= false;
1363 void wxTextCtrl::SetMaxLength(unsigned long len
)
1365 if ( !HasFlag(wxTE_MULTILINE
) )
1367 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1369 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1370 // we had tried to enter more text than allowed by max text length and
1371 // the text wasn't really changed
1373 // to detect this and generate TEXT_MAXLEN event instead of
1374 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1376 // when max len is set to 0 we disconnect our handler as it means that
1377 // we shouldn't check anything any more
1380 g_signal_connect (m_text
, "insert_text",
1381 G_CALLBACK (gtk_insert_text_callback
), this);
1385 g_signal_handlers_disconnect_by_func (m_text
,
1386 (gpointer
) gtk_insert_text_callback
, this);
1391 void wxTextCtrl::SetSelection( long from
, long to
)
1393 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1395 if (from
== -1 && to
== -1)
1398 to
= GetValue().length();
1401 if ( IsMultiLine() )
1403 GtkTextIter fromi
, toi
;
1404 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1405 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1407 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1408 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1412 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1416 void wxTextCtrl::ShowPosition( long pos
)
1421 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1422 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1423 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1425 // defer until Thaw, text view is not using m_buffer now
1426 m_showPositionOnThaw
= mark
;
1428 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1432 wxTextCtrlHitTestResult
1433 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1435 if ( !IsMultiLine() )
1438 return wxTE_HT_UNKNOWN
;
1442 gtk_text_view_window_to_buffer_coords
1444 GTK_TEXT_VIEW(m_text
),
1445 GTK_TEXT_WINDOW_TEXT
,
1451 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1453 *pos
= gtk_text_iter_get_offset(&iter
);
1455 return wxTE_HT_ON_TEXT
;
1458 long wxTextCtrl::GetInsertionPoint() const
1460 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1462 if ( IsMultiLine() )
1464 // There is no direct accessor for the cursor, but
1465 // internally, the cursor is the "mark" called
1466 // "insert" in the text view's btree structure.
1468 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1470 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1472 return gtk_text_iter_get_offset( &cursor
);
1476 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1480 wxTextPos
wxTextCtrl::GetLastPosition() const
1482 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1486 if ( IsMultiLine() )
1489 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1491 pos
= gtk_text_iter_get_offset( &end
);
1495 pos
= GTK_ENTRY(m_text
)->text_length
;
1501 void wxTextCtrl::Remove( long from
, long to
)
1503 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1505 if ( IsMultiLine() )
1507 GtkTextIter fromi
, toi
;
1508 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1509 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1511 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1514 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1517 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1519 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1525 SetInsertionPoint( from
);
1530 void wxTextCtrl::Cut()
1532 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1534 if ( IsMultiLine() )
1535 g_signal_emit_by_name (m_text
, "cut-clipboard");
1537 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1540 void wxTextCtrl::Copy()
1542 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1544 if ( IsMultiLine() )
1545 g_signal_emit_by_name (m_text
, "copy-clipboard");
1547 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1550 void wxTextCtrl::Paste()
1552 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1554 if ( IsMultiLine() )
1555 g_signal_emit_by_name (m_text
, "paste-clipboard");
1557 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1561 void wxTextCtrl::Undo()
1564 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1567 void wxTextCtrl::Redo()
1570 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1573 bool wxTextCtrl::CanUndo() const
1576 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1580 bool wxTextCtrl::CanRedo() const
1583 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1587 // If the return values from and to are the same, there is no
1589 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1591 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1595 bool haveSelection
= false;
1597 if ( IsMultiLine() )
1599 GtkTextIter ifrom
, ito
;
1600 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1602 haveSelection
= true;
1603 from
= gtk_text_iter_get_offset(&ifrom
);
1604 to
= gtk_text_iter_get_offset(&ito
);
1607 else // not multi-line
1609 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1612 haveSelection
= true;
1616 if (! haveSelection
)
1617 from
= to
= GetInsertionPoint();
1621 // exchange them to be compatible with wxMSW
1634 bool wxTextCtrl::IsEditable() const
1636 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1638 if ( IsMultiLine() )
1640 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1644 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1648 bool wxTextCtrl::IsModified() const
1653 void wxTextCtrl::Clear()
1655 SetValue( wxEmptyString
);
1658 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1660 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1662 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1664 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1666 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1667 event
.SetEventObject(this);
1668 event
.SetString(GetValue());
1669 if ( GetEventHandler()->ProcessEvent(event
) )
1673 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1675 if ( IsSingleLine() )
1677 // This will invoke the dialog default action, such
1678 // as the clicking the default button.
1680 wxWindow
*top_frame
= m_parent
;
1681 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1682 top_frame
= top_frame
->GetParent();
1684 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1686 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1688 if (window
->default_widget
)
1690 gtk_widget_activate (window
->default_widget
);
1700 GtkWidget
* wxTextCtrl::GetConnectWidget()
1702 return GTK_WIDGET(m_text
);
1705 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1707 if ( IsMultiLine() )
1709 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1710 GTK_TEXT_WINDOW_TEXT
);
1714 return GTK_ENTRY(m_text
)->text_area
;
1718 // the font will change for subsequent text insertiongs
1719 bool wxTextCtrl::SetFont( const wxFont
&font
)
1721 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1723 if ( !wxTextCtrlBase::SetFont(font
) )
1725 // font didn't change, nothing to do
1729 if ( IsMultiLine() )
1731 SetUpdateFont(true);
1733 m_defaultStyle
.SetFont(font
);
1735 ChangeFontGlobally();
1741 void wxTextCtrl::ChangeFontGlobally()
1743 // this method is very inefficient and hence should be called as rarely as
1746 // TODO: it can be implemented much more efficiently for GTK2
1747 wxASSERT_MSG( IsMultiLine(),
1748 _T("shouldn't be called for single line controls") );
1750 wxString value
= GetValue();
1751 if ( !value
.empty() )
1753 SetUpdateFont(false);
1760 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1762 if ( !wxControl::SetForegroundColour(colour
) )
1765 // update default fg colour too
1766 m_defaultStyle
.SetTextColour(colour
);
1771 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1773 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1775 if ( !wxControl::SetBackgroundColour( colour
) )
1778 if (!m_backgroundColour
.Ok())
1781 // change active background color too
1782 m_defaultStyle
.SetBackgroundColour( colour
);
1787 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1789 if ( IsMultiLine() )
1791 if ( style
.IsDefault() )
1797 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1799 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1800 _T("invalid range in wxTextCtrl::SetStyle") );
1802 GtkTextIter starti
, endi
;
1803 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1804 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1806 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1810 //else: single line text controls don't support styles
1815 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1817 gtk_widget_modify_style(m_text
, style
);
1820 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1825 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1830 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1835 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1840 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1845 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1847 event
.Enable( CanCut() );
1850 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1852 event
.Enable( CanCopy() );
1855 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1857 event
.Enable( CanPaste() );
1860 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1862 event
.Enable( CanUndo() );
1865 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1867 event
.Enable( CanRedo() );
1870 wxSize
wxTextCtrl::DoGetBestSize() const
1872 // FIXME should be different for multi-line controls...
1873 wxSize
ret( wxControl::DoGetBestSize() );
1874 wxSize
best(80, ret
.y
);
1875 CacheBestSize(best
);
1879 // ----------------------------------------------------------------------------
1881 // ----------------------------------------------------------------------------
1883 void wxTextCtrl::Freeze()
1885 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1887 if ( HasFlag(wxTE_MULTILINE
) )
1889 if (m_freezeCount
++ == 0)
1891 // freeze textview updates and remove buffer
1892 g_signal_connect (m_text
, "expose_event",
1893 G_CALLBACK (gtk_text_exposed_callback
), this);
1894 g_signal_connect (m_widget
, "expose_event",
1895 G_CALLBACK (gtk_text_exposed_callback
), this);
1896 gtk_widget_set_sensitive(m_widget
, false);
1897 g_object_ref(m_buffer
);
1898 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1899 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1900 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1901 // gtk_text_view_set_buffer adds its own reference
1902 g_object_unref(buf_new
);
1903 // This mark should be deleted when the buffer is changed,
1904 // but it's not (in GTK+ up to at least 2.10.6).
1905 // Otherwise these anonymous marks start to build up in the buffer,
1906 // and Freeze takes longer and longer each time it is called.
1907 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1908 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1913 void wxTextCtrl::Thaw()
1915 if ( HasFlag(wxTE_MULTILINE
) )
1917 wxCHECK_RET(m_freezeCount
!= 0, _T("Thaw() without matching Freeze()"));
1919 if (--m_freezeCount
== 0)
1921 // Reattach buffer and thaw textview updates
1922 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1923 g_object_unref(m_buffer
);
1924 gtk_widget_set_sensitive(m_widget
, true);
1925 g_signal_handlers_disconnect_by_func (m_widget
,
1926 (gpointer
) gtk_text_exposed_callback
, this);
1927 g_signal_handlers_disconnect_by_func (m_text
,
1928 (gpointer
) gtk_text_exposed_callback
, this);
1929 if (m_showPositionOnThaw
!= NULL
)
1931 gtk_text_view_scroll_mark_onscreen(
1932 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1933 m_showPositionOnThaw
= NULL
;
1939 // ----------------------------------------------------------------------------
1940 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1941 // ----------------------------------------------------------------------------
1943 // FIXME: when dragging on a link the sample gets an "Unknown event".
1944 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1945 // a buggy sample, or something else
1946 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1949 if( !HasFlag(wxTE_AUTO_URL
) )
1953 GtkTextIter start
, end
;
1954 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1957 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1958 event
.GetX(), event
.GetY(), &x
, &y
);
1960 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1961 if (!gtk_text_iter_has_tag(&end
, tag
))
1963 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1964 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1968 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1969 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1972 if(!gtk_text_iter_begins_tag(&start
, tag
))
1973 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1974 if(!gtk_text_iter_ends_tag(&end
, tag
))
1975 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1977 // Native context menu is probably not desired on an URL.
1978 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1979 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1982 wxTextUrlEvent
url_event(m_windowId
, event
,
1983 gtk_text_iter_get_offset(&start
),
1984 gtk_text_iter_get_offset(&end
));
1986 InitCommandEvent(url_event
);
1987 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1988 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1989 GetEventHandler()->ProcessEvent(url_event
);
1992 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1994 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1996 // GtkTextView starts a drag operation when left mouse button is pressed
1997 // and ends it when it is released and if it doesn't get the release event
1998 // the next click on a control results in an assertion failure inside
1999 // gtk_text_view_start_selection_drag() which simply *kills* the program
2000 // without anything we can do about it, so always let GTK+ have this event
2001 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
2006 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2008 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);