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 buf
= tagname
.mb_str(wxConvUTF8
);
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
, buf
,
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 // ----------------------------------------------------------------------------
301 // "insert_text" for GtkEntry
302 // ----------------------------------------------------------------------------
306 gtk_insert_text_callback(GtkEditable
*editable
,
307 const gchar
*new_text
,
308 gint new_text_length
,
312 // we should only be called if we have a max len limit at all
313 GtkEntry
*entry
= GTK_ENTRY (editable
);
315 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
317 // check that we don't overflow the max length limit
319 // FIXME: this doesn't work when we paste a string which is going to be
321 if ( entry
->text_length
== entry
->text_max_length
)
323 // we don't need to run the base class version at all
324 g_signal_stop_emission_by_name (editable
, "insert_text");
326 // remember that the next changed signal is to be ignored to avoid
327 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
328 win
->IgnoreNextTextUpdate();
330 // and generate the correct one ourselves
331 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
332 event
.SetEventObject(win
);
333 event
.SetString(win
->GetValue());
334 win
->GetEventHandler()->ProcessEvent( event
);
339 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
343 au_apply_tag_callback(GtkTextBuffer
*buffer
,
349 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
350 g_signal_stop_emission_by_name (buffer
, "apply_tag");
354 //-----------------------------------------------------------------------------
355 // GtkTextCharPredicates for gtk_text_iter_*_find_char
356 //-----------------------------------------------------------------------------
360 pred_whitespace (gunichar ch
, gpointer user_data
)
362 return g_unichar_isspace(ch
);
368 pred_non_whitespace (gunichar ch
, gpointer user_data
)
370 return !g_unichar_isspace(ch
);
376 pred_nonpunct (gunichar ch
, gpointer user_data
)
378 return !g_unichar_ispunct(ch
);
384 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
386 return !g_unichar_ispunct(ch
) || ch
== '/';
390 //-----------------------------------------------------------------------------
391 // Check for links between s and e and correct tags as necessary
392 //-----------------------------------------------------------------------------
394 // This function should be made match better while being efficient at one point.
395 // Most probably with a row of regular expressions.
398 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
400 static const char *URIPrefixes
[] =
418 GtkTextIter start
= *s
, end
= *e
;
419 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
421 // Get our special link tag
422 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
424 // Get rid of punctuation from beginning and end.
425 // Might want to move this to au_check_range if an improved link checking doesn't
426 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
427 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
428 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
430 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
431 gtk_text_iter_forward_char(&end
);
433 wxGtkString
text(gtk_text_iter_get_text( &start
, &end
));
434 size_t len
= strlen(text
), prefix_len
;
437 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
439 prefix_len
= strlen(URIPrefixes
[n
]);
440 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
444 if(n
< WXSIZEOF(URIPrefixes
))
446 gulong signal_id
= g_signal_handler_find (buffer
,
447 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
449 (gpointer
)au_apply_tag_callback
, NULL
);
451 g_signal_handler_block (buffer
, signal_id
);
452 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
453 g_signal_handler_unblock (buffer
, signal_id
);
460 au_check_range(GtkTextIter
*s
,
461 GtkTextIter
*range_end
)
463 GtkTextIter range_start
= *s
;
464 GtkTextIter word_end
;
465 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
466 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
468 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
470 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
471 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
473 while(!gtk_text_iter_equal(&range_start
, range_end
))
475 word_end
= range_start
;
476 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
478 // Now we should have a word delimited by range_start and word_end, correct link tags
479 au_check_word(&range_start
, &word_end
);
481 range_start
= word_end
;
482 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
487 //-----------------------------------------------------------------------------
488 // "insert-text" for GtkTextBuffer
489 //-----------------------------------------------------------------------------
493 au_insert_text_callback(GtkTextBuffer
*buffer
,
499 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
502 GtkTextIter start
= *end
;
503 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
505 GtkTextIter line_start
= start
;
506 GtkTextIter line_end
= *end
;
507 GtkTextIter words_start
= start
;
508 GtkTextIter words_end
= *end
;
510 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
511 gtk_text_iter_forward_to_line_end(&line_end
);
512 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
513 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
515 au_check_range(&words_start
, &words_end
);
519 //-----------------------------------------------------------------------------
520 // "delete-range" for GtkTextBuffer
521 //-----------------------------------------------------------------------------
525 au_delete_range_callback(GtkTextBuffer
*buffer
,
530 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
533 GtkTextIter line_start
= *start
, line_end
= *end
;
535 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
536 gtk_text_iter_forward_to_line_end(&line_end
);
537 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
538 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
540 au_check_range(start
, end
);
545 //-----------------------------------------------------------------------------
547 //-----------------------------------------------------------------------------
551 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
553 if ( win
->IgnoreTextUpdate() )
556 if (!win
->m_hasVMT
) return;
558 if ( win
->MarkDirtyOnChange() )
561 win
->SendTextUpdatedEvent();
565 //-----------------------------------------------------------------------------
566 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
567 //-----------------------------------------------------------------------------
569 // common part of the event handlers below
571 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
572 wxEventType eventType
, const gchar
* signal_name
)
574 wxClipboardTextEvent
event( eventType
, win
->GetId() );
575 event
.SetEventObject( win
);
576 if ( win
->GetEventHandler()->ProcessEvent( event
) )
578 // don't let the default processing to take place if we did something
579 // ourselves in the event handler
580 g_signal_stop_emission_by_name (widget
, signal_name
);
586 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
588 handle_text_clipboard_callback(
589 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
593 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
595 handle_text_clipboard_callback(
596 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
600 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
602 handle_text_clipboard_callback(
603 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
607 //-----------------------------------------------------------------------------
608 // "expose_event" from scrolled window and textview
609 //-----------------------------------------------------------------------------
613 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
620 //-----------------------------------------------------------------------------
622 //-----------------------------------------------------------------------------
624 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
626 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
627 EVT_CHAR(wxTextCtrl::OnChar
)
629 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
630 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
631 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
632 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
633 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
635 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
636 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
637 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
638 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
639 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
641 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
642 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
643 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
644 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
645 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
646 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
647 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
648 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
649 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
652 void wxTextCtrl::Init()
657 m_countUpdatesToIgnore
= 0;
659 SetUpdateFont(false);
663 m_showPositionOnThaw
= NULL
;
664 m_gdkHandCursor
= NULL
;
665 m_gdkXTermCursor
= NULL
;
668 wxTextCtrl::~wxTextCtrl()
671 gdk_cursor_unref(m_gdkHandCursor
);
673 gdk_cursor_unref(m_gdkXTermCursor
);
676 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
678 const wxString
&value
,
682 const wxValidator
& validator
,
683 const wxString
&name
)
687 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
690 bool wxTextCtrl::Create( wxWindow
*parent
,
692 const wxString
&value
,
696 const wxValidator
& validator
,
697 const wxString
&name
)
699 if (!PreCreation( parent
, pos
, size
) ||
700 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
702 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
706 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
711 m_text
= gtk_text_view_new();
713 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
715 // create "ShowPosition" marker
717 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
718 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
720 // create scrolled window
721 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
722 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
723 GTK_POLICY_AUTOMATIC
,
724 style
& wxTE_NO_VSCROLL
726 : GTK_POLICY_AUTOMATIC
);
727 // for ScrollLines/Pages
728 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
730 // Insert view into scrolled window
731 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
735 GtkScrolledWindowSetBorder(m_widget
, style
);
737 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
739 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
743 // a single-line text control: no need for scrollbars
745 m_text
= gtk_entry_new();
747 if (style
& wxNO_BORDER
)
748 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
751 m_parent
->DoAddChild( this );
753 m_focusWidget
= m_text
;
759 gtk_widget_show(m_text
);
762 // We want to be notified about text changes.
765 g_signal_connect (m_buffer
, "changed",
766 G_CALLBACK (gtk_text_changed_callback
), this);
770 g_signal_connect (m_text
, "changed",
771 G_CALLBACK (gtk_text_changed_callback
), this);
779 if (style
& wxTE_PASSWORD
)
782 if (style
& wxTE_READONLY
)
785 // left justification (alignment) is the default anyhow
786 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
787 GTKSetJustification();
791 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
792 if (style
& wxTE_AUTO_URL
)
794 GtkTextIter start
, end
;
795 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
796 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
798 // We create our wxUrl tag here for slight efficiency gain - we
799 // don't have to check for the tag existance in callbacks,
800 // hereby it's guaranteed to exist.
801 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
802 "foreground", "blue",
803 "underline", PANGO_UNDERLINE_SINGLE
,
806 // Check for URLs after each text change
807 g_signal_connect_after (m_buffer
, "insert_text",
808 G_CALLBACK (au_insert_text_callback
), this);
809 g_signal_connect_after (m_buffer
, "delete_range",
810 G_CALLBACK (au_delete_range_callback
), this);
812 // Block all wxUrl tag applying unless we do it ourselves, in which case we
813 // block this callback temporarily. This takes care of gtk+ internal
814 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
815 // which is undesired because only a part of the URL might be copied.
816 // The insert-text signal emitted inside it will take care of newly formed
817 // or wholly copied URLs.
818 g_signal_connect (m_buffer
, "apply_tag",
819 G_CALLBACK (au_apply_tag_callback
), NULL
);
821 // Check for URLs in the initial string passed to Create
822 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
823 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
824 au_check_range(&start
, &end
);
828 g_signal_connect (m_text
, "copy-clipboard",
829 G_CALLBACK (gtk_copy_clipboard_callback
), this);
830 g_signal_connect (m_text
, "cut-clipboard",
831 G_CALLBACK (gtk_cut_clipboard_callback
), this);
832 g_signal_connect (m_text
, "paste-clipboard",
833 G_CALLBACK (gtk_paste_clipboard_callback
), this);
835 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
840 // ----------------------------------------------------------------------------
842 // ----------------------------------------------------------------------------
844 void wxTextCtrl::GTKSetEditable()
846 gboolean editable
= !HasFlag(wxTE_READONLY
);
847 if ( IsSingleLine() )
848 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
850 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
853 void wxTextCtrl::GTKSetVisibility()
855 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
856 if ( IsSingleLine() )
857 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
860 void wxTextCtrl::GTKSetWrapMode()
862 // no wrapping in single line controls
863 if ( !IsMultiLine() )
866 // translate wx wrapping style to GTK+
868 if ( HasFlag( wxTE_DONTWRAP
) )
869 wrap
= GTK_WRAP_NONE
;
870 else if ( HasFlag( wxTE_CHARWRAP
) )
871 wrap
= GTK_WRAP_CHAR
;
872 else if ( HasFlag( wxTE_WORDWRAP
) )
873 wrap
= GTK_WRAP_WORD
;
874 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
876 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
878 if ( !gtk_check_version(2,4,0) )
880 wrap
= GTK_WRAP_WORD_CHAR
;
883 #endif // __WXGTK24__
884 wrap
= GTK_WRAP_WORD
;
887 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
890 void wxTextCtrl::GTKSetJustification()
894 GtkJustification just
;
895 if ( HasFlag(wxTE_RIGHT
) )
896 just
= GTK_JUSTIFY_RIGHT
;
897 else if ( HasFlag(wxTE_CENTRE
) )
898 just
= GTK_JUSTIFY_CENTER
;
899 else // wxTE_LEFT == 0
900 just
= GTK_JUSTIFY_LEFT
;
902 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
907 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
908 if (!gtk_check_version(2,4,0))
911 if ( HasFlag(wxTE_RIGHT
) )
913 else if ( HasFlag(wxTE_CENTRE
) )
918 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
920 #endif // __WXGTK24__
925 void wxTextCtrl::SetWindowStyleFlag(long style
)
927 long styleOld
= GetWindowStyleFlag();
929 wxTextCtrlBase::SetWindowStyleFlag(style
);
931 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
934 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
937 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
938 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
941 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
942 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
943 GTKSetJustification();
946 // ----------------------------------------------------------------------------
948 // ----------------------------------------------------------------------------
950 wxString
wxTextCtrl::GetValue() const
952 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
958 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
960 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
961 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
963 return wxGTK_CONV_BACK(text
);
967 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
968 return wxGTK_CONV_BACK(text
);
974 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
976 // GTK+ uses UTF-8 internally, we need to convert to it but from which
979 // first check the default text style (we intentionally don't check the
980 // style for the current position as it doesn't make sense for SetValue())
981 const wxTextAttr
& style
= GetDefaultStyle();
982 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
983 : wxFONTENCODING_SYSTEM
;
985 // fall back to the controls font if no style
986 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
987 enc
= GetFont().GetEncoding();
992 bool wxTextCtrl::IsEmpty() const
995 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
997 return wxTextCtrlBase::IsEmpty();
1000 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
1002 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1006 wxFontEncoding enc
= m_defaultStyle
.HasFont()
1007 ? m_defaultStyle
.GetFont().GetEncoding()
1008 : wxFONTENCODING_SYSTEM
;
1009 if ( enc
== wxFONTENCODING_SYSTEM
)
1010 enc
= GetTextEncoding();
1012 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, enc
));
1015 // see comment in WriteText() as to why we must warn the user about
1017 wxLogWarning(_("Failed to set text in the text control."));
1021 if ( !(flags
& SetValue_SendEvent
) )
1023 g_signal_handlers_block_by_func(GetTextObject(),
1024 (gpointer
)gtk_text_changed_callback
, this);
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 ( !(flags
& SetValue_SendEvent
) )
1046 g_signal_handlers_unblock_by_func(GetTextObject(),
1047 (gpointer
)gtk_text_changed_callback
, this);
1050 // This was added after discussion on the list
1051 SetInsertionPoint(0);
1054 void wxTextCtrl::WriteText( const wxString
&text
)
1056 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1061 // check if we have a specific style for the current position
1062 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1064 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1066 enc
= style
.GetFont().GetEncoding();
1069 if ( enc
== wxFONTENCODING_SYSTEM
)
1070 enc
= GetTextEncoding();
1072 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1075 // we must log an error here as losing the text like this can be a
1076 // serious problem (e.g. imagine the document edited by user being
1077 // empty instead of containing the correct text)
1078 wxLogWarning(_("Failed to insert text in the control."));
1082 // we're changing the text programmatically
1083 DontMarkDirtyOnNextChange();
1085 if ( IsMultiLine() )
1087 // First remove the selection if there is one
1088 // TODO: Is there an easier GTK specific way to do this?
1090 GetSelection(&from
, &to
);
1095 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1097 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1098 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1099 // won't work when frozen, text view is not using m_buffer then
1100 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1102 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1103 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1108 // First remove the selection if there is one
1109 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1111 // This moves the cursor pos to behind the inserted text.
1112 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1114 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1116 // Bring entry's cursor uptodate.
1117 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1121 void wxTextCtrl::AppendText( const wxString
&text
)
1123 SetInsertionPointEnd();
1127 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1130 if ( IsMultiLine() )
1133 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1135 GtkTextIter end
= line
;
1136 // avoid skipping to the next line end if this one is empty
1137 if ( !gtk_text_iter_ends_line(&line
) )
1138 gtk_text_iter_forward_to_line_end(&end
);
1140 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1141 result
= wxGTK_CONV_BACK(text
);
1146 result
= GetValue();
1151 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1153 /* If you implement this, don't forget to update the documentation!
1154 * (file docs/latex/wx/text.tex) */
1155 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1158 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1160 if ( IsMultiLine() )
1164 if (pos
> GetLastPosition())
1167 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1170 *y
= gtk_text_iter_get_line(&iter
);
1172 *x
= gtk_text_iter_get_line_offset(&iter
);
1174 else // single line control
1176 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1185 // index out of bounds
1193 long wxTextCtrl::XYToPosition(long x
, long y
) const
1195 if ( IsSingleLine() )
1199 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1202 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1203 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1206 return gtk_text_iter_get_offset(&iter
) + x
;
1209 int wxTextCtrl::GetLineLength(long lineNo
) const
1211 if ( IsMultiLine() )
1213 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1214 if (lineNo
> last_line
)
1218 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1219 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1220 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1224 wxString str
= GetLineText (lineNo
);
1225 return (int) str
.length();
1229 int wxTextCtrl::GetNumberOfLines() const
1231 if ( IsMultiLine() )
1233 return gtk_text_buffer_get_line_count( m_buffer
);
1241 void wxTextCtrl::SetInsertionPoint( long pos
)
1243 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1245 if ( IsMultiLine() )
1248 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1249 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1250 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1252 // defer until Thaw, text view is not using m_buffer now
1253 m_showPositionOnThaw
= mark
;
1255 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1259 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1260 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1264 void wxTextCtrl::SetInsertionPointEnd()
1266 SetInsertionPoint(-1);
1269 void wxTextCtrl::SetEditable( bool editable
)
1271 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1273 if ( IsMultiLine() )
1275 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1279 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1283 bool wxTextCtrl::Enable( bool enable
)
1285 if (!wxWindowBase::Enable(enable
))
1291 gtk_widget_set_sensitive( m_text
, enable
);
1296 // wxGTK-specific: called recursively by Enable,
1297 // to give widgets an opportunity to correct their colours after they
1298 // have been changed by Enable
1299 void wxTextCtrl::OnEnabled( bool enable
)
1301 // If we have a custom background colour, we use this colour in both
1302 // disabled and enabled mode, or we end up with a different colour under the
1304 wxColour oldColour
= GetBackgroundColour();
1307 // Need to set twice or it'll optimize the useful stuff out
1308 if (oldColour
== * wxWHITE
)
1309 SetBackgroundColour(*wxBLACK
);
1311 SetBackgroundColour(*wxWHITE
);
1312 SetBackgroundColour(oldColour
);
1316 void wxTextCtrl::MarkDirty()
1321 void wxTextCtrl::DiscardEdits()
1326 // ----------------------------------------------------------------------------
1327 // max text length support
1328 // ----------------------------------------------------------------------------
1330 bool wxTextCtrl::IgnoreTextUpdate()
1332 if ( m_countUpdatesToIgnore
> 0 )
1334 m_countUpdatesToIgnore
--;
1342 bool wxTextCtrl::MarkDirtyOnChange()
1344 if ( m_dontMarkDirty
)
1346 m_dontMarkDirty
= false;
1354 void wxTextCtrl::SetMaxLength(unsigned long len
)
1356 if ( !HasFlag(wxTE_MULTILINE
) )
1358 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1360 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1361 // we had tried to enter more text than allowed by max text length and
1362 // the text wasn't really changed
1364 // to detect this and generate TEXT_MAXLEN event instead of
1365 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1367 // when max len is set to 0 we disconnect our handler as it means that
1368 // we shouldn't check anything any more
1371 g_signal_connect (m_text
, "insert_text",
1372 G_CALLBACK (gtk_insert_text_callback
), this);
1376 g_signal_handlers_disconnect_by_func (m_text
,
1377 (gpointer
) gtk_insert_text_callback
, this);
1382 void wxTextCtrl::SetSelection( long from
, long to
)
1384 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1386 if (from
== -1 && to
== -1)
1389 to
= GetValue().length();
1392 if ( IsMultiLine() )
1394 GtkTextIter fromi
, toi
;
1395 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1396 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1398 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1399 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1403 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1407 void wxTextCtrl::ShowPosition( long pos
)
1412 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1413 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1414 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1416 // defer until Thaw, text view is not using m_buffer now
1417 m_showPositionOnThaw
= mark
;
1419 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1423 wxTextCtrlHitTestResult
1424 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1426 if ( !IsMultiLine() )
1429 return wxTE_HT_UNKNOWN
;
1433 gtk_text_view_window_to_buffer_coords
1435 GTK_TEXT_VIEW(m_text
),
1436 GTK_TEXT_WINDOW_TEXT
,
1442 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1444 *pos
= gtk_text_iter_get_offset(&iter
);
1446 return wxTE_HT_ON_TEXT
;
1449 long wxTextCtrl::GetInsertionPoint() const
1451 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1453 if ( IsMultiLine() )
1455 // There is no direct accessor for the cursor, but
1456 // internally, the cursor is the "mark" called
1457 // "insert" in the text view's btree structure.
1459 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1461 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1463 return gtk_text_iter_get_offset( &cursor
);
1467 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1471 wxTextPos
wxTextCtrl::GetLastPosition() const
1473 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1477 if ( IsMultiLine() )
1480 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1482 pos
= gtk_text_iter_get_offset( &end
);
1486 pos
= GTK_ENTRY(m_text
)->text_length
;
1492 void wxTextCtrl::Remove( long from
, long to
)
1494 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1496 if ( IsMultiLine() )
1498 GtkTextIter fromi
, toi
;
1499 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1500 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1502 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1505 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1508 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1510 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1516 SetInsertionPoint( from
);
1521 void wxTextCtrl::Cut()
1523 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1525 if ( IsMultiLine() )
1526 g_signal_emit_by_name (m_text
, "cut-clipboard");
1528 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1531 void wxTextCtrl::Copy()
1533 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1535 if ( IsMultiLine() )
1536 g_signal_emit_by_name (m_text
, "copy-clipboard");
1538 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1541 void wxTextCtrl::Paste()
1543 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1545 if ( IsMultiLine() )
1546 g_signal_emit_by_name (m_text
, "paste-clipboard");
1548 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1552 void wxTextCtrl::Undo()
1555 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1558 void wxTextCtrl::Redo()
1561 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1564 bool wxTextCtrl::CanUndo() const
1567 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1571 bool wxTextCtrl::CanRedo() const
1574 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1578 // If the return values from and to are the same, there is no
1580 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1582 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1586 bool haveSelection
= false;
1588 if ( IsMultiLine() )
1590 GtkTextIter ifrom
, ito
;
1591 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1593 haveSelection
= true;
1594 from
= gtk_text_iter_get_offset(&ifrom
);
1595 to
= gtk_text_iter_get_offset(&ito
);
1598 else // not multi-line
1600 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1603 haveSelection
= true;
1607 if (! haveSelection
)
1608 from
= to
= GetInsertionPoint();
1612 // exchange them to be compatible with wxMSW
1625 bool wxTextCtrl::IsEditable() const
1627 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1629 if ( IsMultiLine() )
1631 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1635 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1639 bool wxTextCtrl::IsModified() const
1644 void wxTextCtrl::Clear()
1646 SetValue( wxEmptyString
);
1649 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1651 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1653 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1655 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1657 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1658 event
.SetEventObject(this);
1659 event
.SetString(GetValue());
1660 if ( GetEventHandler()->ProcessEvent(event
) )
1664 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1666 if ( IsSingleLine() )
1668 // This will invoke the dialog default action, such
1669 // as the clicking the default button.
1671 wxWindow
*top_frame
= m_parent
;
1672 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1673 top_frame
= top_frame
->GetParent();
1675 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1677 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1679 if (window
->default_widget
)
1681 gtk_widget_activate (window
->default_widget
);
1691 GtkWidget
* wxTextCtrl::GetConnectWidget()
1693 return GTK_WIDGET(m_text
);
1696 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1698 if ( IsMultiLine() )
1700 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1701 GTK_TEXT_WINDOW_TEXT
);
1705 return GTK_ENTRY(m_text
)->text_area
;
1709 // the font will change for subsequent text insertiongs
1710 bool wxTextCtrl::SetFont( const wxFont
&font
)
1712 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1714 if ( !wxTextCtrlBase::SetFont(font
) )
1716 // font didn't change, nothing to do
1720 if ( IsMultiLine() )
1722 SetUpdateFont(true);
1724 m_defaultStyle
.SetFont(font
);
1726 ChangeFontGlobally();
1732 void wxTextCtrl::ChangeFontGlobally()
1734 // this method is very inefficient and hence should be called as rarely as
1737 // TODO: it can be implemented much more efficiently for GTK2
1738 wxASSERT_MSG( IsMultiLine(),
1739 _T("shouldn't be called for single line controls") );
1741 wxString value
= GetValue();
1742 if ( !value
.empty() )
1744 SetUpdateFont(false);
1751 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1753 if ( !wxControl::SetForegroundColour(colour
) )
1756 // update default fg colour too
1757 m_defaultStyle
.SetTextColour(colour
);
1762 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1764 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1766 if ( !wxControl::SetBackgroundColour( colour
) )
1769 if (!m_backgroundColour
.Ok())
1772 // change active background color too
1773 m_defaultStyle
.SetBackgroundColour( colour
);
1778 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1780 if ( IsMultiLine() )
1782 if ( style
.IsDefault() )
1788 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1790 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1791 _T("invalid range in wxTextCtrl::SetStyle") );
1793 GtkTextIter starti
, endi
;
1794 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1795 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1797 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1801 //else: single line text controls don't support styles
1806 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1808 gtk_widget_modify_style(m_text
, style
);
1811 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1816 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1821 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1826 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1831 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1836 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1838 event
.Enable( CanCut() );
1841 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1843 event
.Enable( CanCopy() );
1846 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1848 event
.Enable( CanPaste() );
1851 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1853 event
.Enable( CanUndo() );
1856 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1858 event
.Enable( CanRedo() );
1861 wxSize
wxTextCtrl::DoGetBestSize() const
1863 // FIXME should be different for multi-line controls...
1864 wxSize
ret( wxControl::DoGetBestSize() );
1865 wxSize
best(80, ret
.y
);
1866 CacheBestSize(best
);
1870 // ----------------------------------------------------------------------------
1872 // ----------------------------------------------------------------------------
1874 void wxTextCtrl::Freeze()
1876 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1878 if ( HasFlag(wxTE_MULTILINE
) )
1880 if (m_freezeCount
++ == 0)
1882 // freeze textview updates and remove buffer
1883 g_signal_connect (m_text
, "expose_event",
1884 G_CALLBACK (gtk_text_exposed_callback
), this);
1885 g_signal_connect (m_widget
, "expose_event",
1886 G_CALLBACK (gtk_text_exposed_callback
), this);
1887 gtk_widget_set_sensitive(m_widget
, false);
1888 g_object_ref(m_buffer
);
1889 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1890 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1891 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1892 // gtk_text_view_set_buffer adds its own reference
1893 g_object_unref(buf_new
);
1894 // This mark should be deleted when the buffer is changed,
1895 // but it's not (in GTK+ up to at least 2.10.6).
1896 // Otherwise these anonymous marks start to build up in the buffer,
1897 // and Freeze takes longer and longer each time it is called.
1898 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1899 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1904 void wxTextCtrl::Thaw()
1906 if ( HasFlag(wxTE_MULTILINE
) )
1908 wxCHECK_RET(m_freezeCount
!= 0, _T("Thaw() without matching Freeze()"));
1910 if (--m_freezeCount
== 0)
1912 // Reattach buffer and thaw textview updates
1913 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1914 g_object_unref(m_buffer
);
1915 gtk_widget_set_sensitive(m_widget
, true);
1916 g_signal_handlers_disconnect_by_func (m_widget
,
1917 (gpointer
) gtk_text_exposed_callback
, this);
1918 g_signal_handlers_disconnect_by_func (m_text
,
1919 (gpointer
) gtk_text_exposed_callback
, this);
1920 if (m_showPositionOnThaw
!= NULL
)
1922 gtk_text_view_scroll_mark_onscreen(
1923 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1924 m_showPositionOnThaw
= NULL
;
1930 // ----------------------------------------------------------------------------
1931 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1932 // ----------------------------------------------------------------------------
1934 // FIXME: when dragging on a link the sample gets an "Unknown event".
1935 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1936 // a buggy sample, or something else
1937 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1940 if( !HasFlag(wxTE_AUTO_URL
) )
1944 GtkTextIter start
, end
;
1945 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1948 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1949 event
.GetX(), event
.GetY(), &x
, &y
);
1951 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1952 if (!gtk_text_iter_has_tag(&end
, tag
))
1954 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1955 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1959 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1960 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1963 if(!gtk_text_iter_begins_tag(&start
, tag
))
1964 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1965 if(!gtk_text_iter_ends_tag(&end
, tag
))
1966 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1968 // Native context menu is probably not desired on an URL.
1969 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1970 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1973 wxTextUrlEvent
url_event(m_windowId
, event
,
1974 gtk_text_iter_get_offset(&start
),
1975 gtk_text_iter_get_offset(&end
));
1977 InitCommandEvent(url_event
);
1978 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1979 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1980 GetEventHandler()->ProcessEvent(url_event
);
1983 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1985 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1987 // GtkTextView starts a drag operation when left mouse button is pressed
1988 // and ends it when it is released and if it doesn't get the release event
1989 // the next click on a control results in an assertion failure inside
1990 // gtk_text_view_start_selection_drag() which simply *kills* the program
1991 // without anything we can do about it, so always let GTK+ have this event
1992 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
1997 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1999 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
2002 #endif // wxUSE_TEXTCTRL