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 before gtk+-2.11.0 with pango-1.17 being available
163 // (but if new enough pango isn't available it's a mere gtk warning)
164 #if GTK_CHECK_VERSION(2,11,0)
165 case wxTEXT_ALIGNMENT_JUSTIFIED
:
166 if (!gtk_check_version(2,11,0))
167 align
= GTK_JUSTIFY_FILL
;
169 align
= GTK_JUSTIFY_LEFT
;
174 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
175 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
178 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
179 "justification", align
, NULL
);
180 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
183 if (attr
.HasLeftIndent())
185 // Indentation attribute
187 // Clear old indentation tags
188 GtkTextIter para_start
, para_end
= *end
;
189 gtk_text_buffer_get_iter_at_line( text_buffer
,
191 gtk_text_iter_get_line(start
) );
192 gtk_text_iter_forward_line(¶_end
);
194 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXINDENT", start
, end
);
196 // Convert indent from 1/10th of a mm into pixels
198 #if GTK_CHECK_VERSION(2,2,0)
199 if (!gtk_check_version(2,2,0))
200 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
201 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
204 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
206 const int indent
= (int)(factor
* attr
.GetLeftIndent());
207 const int subIndent
= (int)(factor
* attr
.GetLeftSubIndent());
215 gsubindent
= -subIndent
;
219 gindent
= -subIndent
;
223 g_snprintf(buf
, sizeof(buf
), "WXINDENT %d %d", gindent
, gsubindent
);
224 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
227 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
228 "left-margin", gindent
, "indent", gsubindent
, NULL
);
229 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
237 GtkTextIter para_start
, para_end
= *end
;
238 gtk_text_buffer_get_iter_at_line( text_buffer
,
240 gtk_text_iter_get_line(start
) );
241 gtk_text_iter_forward_line(¶_end
);
243 wxGtkTextRemoveTagsWithPrefix(text_buffer
, "WXTABS", start
, end
);
245 const wxArrayInt
& tabs
= attr
.GetTabs();
247 wxString tagname
= _T("WXTABS");
248 g_snprintf(buf
, sizeof(buf
), "WXTABS");
249 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
250 tagname
+= wxString::Format(_T(" %d"), tabs
[i
]);
252 const wxWX2MBbuf buf
= tagname
.mb_str(wxConvUTF8
);
254 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
258 // Factor to convert from 1/10th of a mm into pixels
260 #if GTK_CHECK_VERSION(2,2,0)
261 if (!gtk_check_version(2,2,0))
262 factor
= (float)gdk_screen_get_width(gtk_widget_get_screen(text
)) /
263 gdk_screen_get_width_mm(gtk_widget_get_screen(text
)) / 10;
266 factor
= (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
268 PangoTabArray
* tabArray
= pango_tab_array_new(tabs
.GetCount(), TRUE
);
269 for (size_t i
= 0; i
< tabs
.GetCount(); i
++)
270 pango_tab_array_set_tab(tabArray
, i
, PANGO_TAB_LEFT
, (gint
)(tabs
[i
] * factor
));
271 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
272 "tabs", tabArray
, NULL
);
273 pango_tab_array_free(tabArray
);
275 gtk_text_buffer_apply_tag (text_buffer
, tag
, ¶_start
, ¶_end
);
279 static void wxGtkTextInsert(GtkWidget
*text
,
280 GtkTextBuffer
*text_buffer
,
281 const wxTextAttr
& attr
,
282 const wxCharBuffer
& buffer
)
286 GtkTextIter iter
, start
;
288 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
289 gtk_text_buffer_get_insert (text_buffer
) );
290 start_offset
= gtk_text_iter_get_offset (&iter
);
291 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
293 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
295 wxGtkTextApplyTagsFromAttr(text
, text_buffer
, attr
, &start
, &iter
);
298 // ----------------------------------------------------------------------------
299 // "insert_text" for GtkEntry
300 // ----------------------------------------------------------------------------
304 gtk_insert_text_callback(GtkEditable
*editable
,
305 const gchar
*new_text
,
306 gint new_text_length
,
310 // we should only be called if we have a max len limit at all
311 GtkEntry
*entry
= GTK_ENTRY (editable
);
313 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
315 // check that we don't overflow the max length limit
317 // FIXME: this doesn't work when we paste a string which is going to be
319 if ( entry
->text_length
== entry
->text_max_length
)
321 // we don't need to run the base class version at all
322 g_signal_stop_emission_by_name (editable
, "insert_text");
324 // remember that the next changed signal is to be ignored to avoid
325 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
326 win
->IgnoreNextTextUpdate();
328 // and generate the correct one ourselves
329 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
330 event
.SetEventObject(win
);
331 event
.SetString(win
->GetValue());
332 win
->GetEventHandler()->ProcessEvent( event
);
337 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
341 au_apply_tag_callback(GtkTextBuffer
*buffer
,
347 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
348 g_signal_stop_emission_by_name (buffer
, "apply_tag");
352 //-----------------------------------------------------------------------------
353 // GtkTextCharPredicates for gtk_text_iter_*_find_char
354 //-----------------------------------------------------------------------------
358 pred_whitespace (gunichar ch
, gpointer user_data
)
360 return g_unichar_isspace(ch
);
366 pred_non_whitespace (gunichar ch
, gpointer user_data
)
368 return !g_unichar_isspace(ch
);
374 pred_nonpunct (gunichar ch
, gpointer user_data
)
376 return !g_unichar_ispunct(ch
);
382 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
384 return !g_unichar_ispunct(ch
) || ch
== '/';
388 //-----------------------------------------------------------------------------
389 // Check for links between s and e and correct tags as necessary
390 //-----------------------------------------------------------------------------
392 // This function should be made match better while being efficient at one point.
393 // Most probably with a row of regular expressions.
396 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
398 static const char *URIPrefixes
[] =
416 GtkTextIter start
= *s
, end
= *e
;
417 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
419 // Get our special link tag
420 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
422 // Get rid of punctuation from beginning and end.
423 // Might want to move this to au_check_range if an improved link checking doesn't
424 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
425 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
426 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
428 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
429 gtk_text_iter_forward_char(&end
);
431 wxGtkString
text(gtk_text_iter_get_text( &start
, &end
));
432 size_t len
= strlen(text
), prefix_len
;
435 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
437 prefix_len
= strlen(URIPrefixes
[n
]);
438 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
442 if(n
< WXSIZEOF(URIPrefixes
))
444 gulong signal_id
= g_signal_handler_find (buffer
,
445 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
447 (gpointer
)au_apply_tag_callback
, NULL
);
449 g_signal_handler_block (buffer
, signal_id
);
450 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
451 g_signal_handler_unblock (buffer
, signal_id
);
458 au_check_range(GtkTextIter
*s
,
459 GtkTextIter
*range_end
)
461 GtkTextIter range_start
= *s
;
462 GtkTextIter word_end
;
463 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
464 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
466 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
468 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
469 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
471 while(!gtk_text_iter_equal(&range_start
, range_end
))
473 word_end
= range_start
;
474 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
476 // Now we should have a word delimited by range_start and word_end, correct link tags
477 au_check_word(&range_start
, &word_end
);
479 range_start
= word_end
;
480 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
485 //-----------------------------------------------------------------------------
486 // "insert-text" for GtkTextBuffer
487 //-----------------------------------------------------------------------------
491 au_insert_text_callback(GtkTextBuffer
*buffer
,
497 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
500 GtkTextIter start
= *end
;
501 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
503 GtkTextIter line_start
= start
;
504 GtkTextIter line_end
= *end
;
505 GtkTextIter words_start
= start
;
506 GtkTextIter words_end
= *end
;
508 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
509 gtk_text_iter_forward_to_line_end(&line_end
);
510 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
511 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
513 au_check_range(&words_start
, &words_end
);
517 //-----------------------------------------------------------------------------
518 // "delete-range" for GtkTextBuffer
519 //-----------------------------------------------------------------------------
523 au_delete_range_callback(GtkTextBuffer
*buffer
,
528 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
531 GtkTextIter line_start
= *start
, line_end
= *end
;
533 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
534 gtk_text_iter_forward_to_line_end(&line_end
);
535 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
536 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
538 au_check_range(start
, end
);
543 //-----------------------------------------------------------------------------
545 //-----------------------------------------------------------------------------
549 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
551 if ( win
->IgnoreTextUpdate() )
554 if (!win
->m_hasVMT
) return;
556 if ( win
->MarkDirtyOnChange() )
559 win
->SendTextUpdatedEvent();
563 //-----------------------------------------------------------------------------
564 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
565 //-----------------------------------------------------------------------------
567 // common part of the event handlers below
569 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
570 wxEventType eventType
, const gchar
* signal_name
)
572 wxClipboardTextEvent
event( eventType
, win
->GetId() );
573 event
.SetEventObject( win
);
574 if ( win
->GetEventHandler()->ProcessEvent( event
) )
576 // don't let the default processing to take place if we did something
577 // ourselves in the event handler
578 g_signal_stop_emission_by_name (widget
, signal_name
);
584 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
586 handle_text_clipboard_callback(
587 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
591 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
593 handle_text_clipboard_callback(
594 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
598 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
600 handle_text_clipboard_callback(
601 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
605 //-----------------------------------------------------------------------------
606 // "expose_event" from scrolled window and textview
607 //-----------------------------------------------------------------------------
611 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
618 //-----------------------------------------------------------------------------
620 //-----------------------------------------------------------------------------
622 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
624 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
625 EVT_CHAR(wxTextCtrl::OnChar
)
627 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
628 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
629 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
630 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
631 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
633 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
634 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
635 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
636 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
637 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
639 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
640 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
641 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
642 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
643 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
644 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
645 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
646 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
647 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
650 void wxTextCtrl::Init()
655 m_countUpdatesToIgnore
= 0;
657 SetUpdateFont(false);
661 m_showPositionOnThaw
= NULL
;
662 m_gdkHandCursor
= NULL
;
663 m_gdkXTermCursor
= NULL
;
666 wxTextCtrl::~wxTextCtrl()
669 gdk_cursor_unref(m_gdkHandCursor
);
671 gdk_cursor_unref(m_gdkXTermCursor
);
674 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
676 const wxString
&value
,
680 const wxValidator
& validator
,
681 const wxString
&name
)
685 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
688 bool wxTextCtrl::Create( wxWindow
*parent
,
690 const wxString
&value
,
694 const wxValidator
& validator
,
695 const wxString
&name
)
697 if (!PreCreation( parent
, pos
, size
) ||
698 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
700 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
704 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
709 m_text
= gtk_text_view_new();
711 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
713 // create "ShowPosition" marker
715 gtk_text_buffer_get_start_iter(m_buffer
, &iter
);
716 gtk_text_buffer_create_mark(m_buffer
, "ShowPosition", &iter
, true);
718 // create scrolled window
719 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
720 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
721 GTK_POLICY_AUTOMATIC
,
722 style
& wxTE_NO_VSCROLL
724 : GTK_POLICY_AUTOMATIC
);
725 // for ScrollLines/Pages
726 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
728 // Insert view into scrolled window
729 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
733 GtkScrolledWindowSetBorder(m_widget
, style
);
735 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
737 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
741 // a single-line text control: no need for scrollbars
743 m_text
= gtk_entry_new();
745 if (style
& wxNO_BORDER
)
746 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
749 m_parent
->DoAddChild( this );
751 m_focusWidget
= m_text
;
757 gtk_widget_show(m_text
);
765 if (style
& wxTE_PASSWORD
)
768 if (style
& wxTE_READONLY
)
771 // left justification (alignment) is the default anyhow
772 if ( style
& (wxTE_RIGHT
| wxTE_CENTRE
) )
773 GTKSetJustification();
775 // We want to be notified about text changes.
778 g_signal_connect (m_buffer
, "changed",
779 G_CALLBACK (gtk_text_changed_callback
), this);
781 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
782 if (style
& wxTE_AUTO_URL
)
784 GtkTextIter start
, end
;
785 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
786 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
788 // We create our wxUrl tag here for slight efficiency gain - we
789 // don't have to check for the tag existance in callbacks,
790 // hereby it's guaranteed to exist.
791 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
792 "foreground", "blue",
793 "underline", PANGO_UNDERLINE_SINGLE
,
796 // Check for URLs after each text change
797 g_signal_connect_after (m_buffer
, "insert_text",
798 G_CALLBACK (au_insert_text_callback
), this);
799 g_signal_connect_after (m_buffer
, "delete_range",
800 G_CALLBACK (au_delete_range_callback
), this);
802 // Block all wxUrl tag applying unless we do it ourselves, in which case we
803 // block this callback temporarily. This takes care of gtk+ internal
804 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
805 // which is undesired because only a part of the URL might be copied.
806 // The insert-text signal emitted inside it will take care of newly formed
807 // or wholly copied URLs.
808 g_signal_connect (m_buffer
, "apply_tag",
809 G_CALLBACK (au_apply_tag_callback
), NULL
);
811 // Check for URLs in the initial string passed to Create
812 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
813 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
814 au_check_range(&start
, &end
);
819 g_signal_connect (m_text
, "changed",
820 G_CALLBACK (gtk_text_changed_callback
), this);
823 g_signal_connect (m_text
, "copy-clipboard",
824 G_CALLBACK (gtk_copy_clipboard_callback
), this);
825 g_signal_connect (m_text
, "cut-clipboard",
826 G_CALLBACK (gtk_cut_clipboard_callback
), this);
827 g_signal_connect (m_text
, "paste-clipboard",
828 G_CALLBACK (gtk_paste_clipboard_callback
), this);
830 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
835 // ----------------------------------------------------------------------------
837 // ----------------------------------------------------------------------------
839 void wxTextCtrl::GTKSetEditable()
841 gboolean editable
= !HasFlag(wxTE_READONLY
);
842 if ( IsSingleLine() )
843 gtk_editable_set_editable(GTK_EDITABLE(m_text
), editable
);
845 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text
), editable
);
848 void wxTextCtrl::GTKSetVisibility()
850 // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control?
851 if ( IsSingleLine() )
852 gtk_entry_set_visibility(GTK_ENTRY(m_text
), !HasFlag(wxTE_PASSWORD
));
855 void wxTextCtrl::GTKSetWrapMode()
857 // no wrapping in single line controls
858 if ( !IsMultiLine() )
861 // translate wx wrapping style to GTK+
863 if ( HasFlag( wxTE_DONTWRAP
) )
864 wrap
= GTK_WRAP_NONE
;
865 else if ( HasFlag( wxTE_CHARWRAP
) )
866 wrap
= GTK_WRAP_CHAR
;
867 else if ( HasFlag( wxTE_WORDWRAP
) )
868 wrap
= GTK_WRAP_WORD
;
869 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
871 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
873 if ( !gtk_check_version(2,4,0) )
875 wrap
= GTK_WRAP_WORD_CHAR
;
878 #endif // __WXGTK24__
879 wrap
= GTK_WRAP_WORD
;
882 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
885 void wxTextCtrl::GTKSetJustification()
889 GtkJustification just
;
890 if ( HasFlag(wxTE_RIGHT
) )
891 just
= GTK_JUSTIFY_RIGHT
;
892 else if ( HasFlag(wxTE_CENTRE
) )
893 just
= GTK_JUSTIFY_CENTER
;
894 else // wxTE_LEFT == 0
895 just
= GTK_JUSTIFY_LEFT
;
897 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text
), just
);
902 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
903 if (!gtk_check_version(2,4,0))
906 if ( HasFlag(wxTE_RIGHT
) )
908 else if ( HasFlag(wxTE_CENTRE
) )
913 gtk_entry_set_alignment(GTK_ENTRY(m_text
), align
);
915 #endif // __WXGTK24__
920 void wxTextCtrl::SetWindowStyleFlag(long style
)
922 long styleOld
= GetWindowStyleFlag();
924 wxTextCtrlBase::SetWindowStyleFlag(style
);
926 if ( (style
& wxTE_READONLY
) != (styleOld
& wxTE_READONLY
) )
929 if ( (style
& wxTE_PASSWORD
) != (styleOld
& wxTE_PASSWORD
) )
932 static const long flagsWrap
= wxTE_WORDWRAP
| wxTE_CHARWRAP
| wxTE_DONTWRAP
;
933 if ( (style
& flagsWrap
) != (styleOld
& flagsWrap
) )
936 static const long flagsAlign
= wxTE_LEFT
| wxTE_CENTRE
| wxTE_RIGHT
;
937 if ( (style
& flagsAlign
) != (styleOld
& flagsAlign
) )
938 GTKSetJustification();
941 // ----------------------------------------------------------------------------
943 // ----------------------------------------------------------------------------
945 wxString
wxTextCtrl::GetValue() const
947 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
953 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
955 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
956 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &start
, &end
, true));
958 return wxGTK_CONV_BACK(text
);
962 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
963 return wxGTK_CONV_BACK(text
);
969 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
971 // GTK+ uses UTF-8 internally, we need to convert to it but from which
974 // first check the default text style (we intentionally don't check the
975 // style for the current position as it doesn't make sense for SetValue())
976 const wxTextAttr
& style
= GetDefaultStyle();
977 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
978 : wxFONTENCODING_SYSTEM
;
980 // fall back to the controls font if no style
981 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
982 enc
= GetFont().GetEncoding();
987 bool wxTextCtrl::IsEmpty() const
990 return gtk_text_buffer_get_char_count(m_buffer
) == 0;
992 return wxTextCtrlBase::IsEmpty();
995 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
997 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
999 // the control won't be modified any more as we programmatically replace
1000 // all the existing text, so reset the flag and don't set it again (and do
1001 // it now, before the text event handler is ran so that IsModified() called
1002 // from there returns the expected value)
1004 DontMarkDirtyOnNextChange();
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 the control is not empty, two "changed" signals are emitted,
1022 // otherwise only one and we need to ignore either both or one of them
1023 int ignore
= flags
& SetValue_SendEvent
? 0 : 1;
1028 IgnoreNextTextUpdate(ignore
);
1030 if ( IsMultiLine() )
1032 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
1034 if ( !m_defaultStyle
.IsDefault() )
1036 GtkTextIter start
, end
;
1037 gtk_text_buffer_get_bounds( m_buffer
, &start
, &end
);
1038 wxGtkTextApplyTagsFromAttr(m_widget
, m_buffer
, m_defaultStyle
,
1044 gtk_entry_set_text( GTK_ENTRY(m_text
), buffer
);
1047 // if, for whatever reason, the callback wasn't called the expected number
1048 // of times, still reset the flags to the default values
1049 m_dontMarkDirty
= false;
1050 m_countUpdatesToIgnore
= 0;
1053 // GRG, Jun/2000: Changed this after a lot of discussion in
1054 // the lists. wxWidgets 2.2 will have a set of flags to
1055 // customize this behaviour.
1056 SetInsertionPoint(0);
1059 void wxTextCtrl::WriteText( const wxString
&text
)
1061 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1066 // check if we have a specific style for the current position
1067 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
1069 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
1071 enc
= style
.GetFont().GetEncoding();
1074 if ( enc
== wxFONTENCODING_SYSTEM
)
1075 enc
= GetTextEncoding();
1077 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
1080 // we must log an error here as losing the text like this can be a
1081 // serious problem (e.g. imagine the document edited by user being
1082 // empty instead of containing the correct text)
1083 wxLogWarning(_("Failed to insert text in the control."));
1087 // we're changing the text programmatically
1088 DontMarkDirtyOnNextChange();
1090 if ( IsMultiLine() )
1092 // First remove the selection if there is one
1093 // TODO: Is there an easier GTK specific way to do this?
1095 GetSelection(&from
, &to
);
1100 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
1102 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
1103 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1104 // won't work when frozen, text view is not using m_buffer then
1105 if (!IsFrozen() && wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
))
1107 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
1108 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
1113 // First remove the selection if there is one
1114 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
1116 // This moves the cursor pos to behind the inserted text.
1117 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
1119 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
1121 // Bring entry's cursor uptodate.
1122 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
1126 void wxTextCtrl::AppendText( const wxString
&text
)
1128 SetInsertionPointEnd();
1132 wxString
wxTextCtrl::GetLineText( long lineNo
) const
1135 if ( IsMultiLine() )
1138 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
1140 GtkTextIter end
= line
;
1141 // avoid skipping to the next line end if this one is empty
1142 if ( !gtk_text_iter_ends_line(&line
) )
1143 gtk_text_iter_forward_to_line_end(&end
);
1145 wxGtkString
text(gtk_text_buffer_get_text(m_buffer
, &line
, &end
, true));
1146 result
= wxGTK_CONV_BACK(text
);
1151 result
= GetValue();
1156 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
1158 /* If you implement this, don't forget to update the documentation!
1159 * (file docs/latex/wx/text.tex) */
1160 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1163 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1165 if ( IsMultiLine() )
1169 if (pos
> GetLastPosition())
1172 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
1175 *y
= gtk_text_iter_get_line(&iter
);
1177 *x
= gtk_text_iter_get_line_offset(&iter
);
1179 else // single line control
1181 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1190 // index out of bounds
1198 long wxTextCtrl::XYToPosition(long x
, long y
) const
1200 if ( IsSingleLine() )
1204 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1207 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1208 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1211 return gtk_text_iter_get_offset(&iter
) + x
;
1214 int wxTextCtrl::GetLineLength(long lineNo
) const
1216 if ( IsMultiLine() )
1218 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1219 if (lineNo
> last_line
)
1223 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1224 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1225 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1229 wxString str
= GetLineText (lineNo
);
1230 return (int) str
.length();
1234 int wxTextCtrl::GetNumberOfLines() const
1236 if ( IsMultiLine() )
1238 return gtk_text_buffer_get_line_count( m_buffer
);
1246 void wxTextCtrl::SetInsertionPoint( long pos
)
1248 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1250 if ( IsMultiLine() )
1253 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1254 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1255 GtkTextMark
* mark
= gtk_text_buffer_get_insert(m_buffer
);
1257 // defer until Thaw, text view is not using m_buffer now
1258 m_showPositionOnThaw
= mark
;
1260 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1264 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1265 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1269 void wxTextCtrl::SetInsertionPointEnd()
1271 SetInsertionPoint(-1);
1274 void wxTextCtrl::SetEditable( bool editable
)
1276 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1278 if ( IsMultiLine() )
1280 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1284 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1288 bool wxTextCtrl::Enable( bool enable
)
1290 if (!wxWindowBase::Enable(enable
))
1296 gtk_widget_set_sensitive( m_text
, enable
);
1301 // wxGTK-specific: called recursively by Enable,
1302 // to give widgets an opportunity to correct their colours after they
1303 // have been changed by Enable
1304 void wxTextCtrl::OnEnabled( bool enable
)
1306 // If we have a custom background colour, we use this colour in both
1307 // disabled and enabled mode, or we end up with a different colour under the
1309 wxColour oldColour
= GetBackgroundColour();
1312 // Need to set twice or it'll optimize the useful stuff out
1313 if (oldColour
== * wxWHITE
)
1314 SetBackgroundColour(*wxBLACK
);
1316 SetBackgroundColour(*wxWHITE
);
1317 SetBackgroundColour(oldColour
);
1321 void wxTextCtrl::MarkDirty()
1326 void wxTextCtrl::DiscardEdits()
1331 // ----------------------------------------------------------------------------
1332 // max text length support
1333 // ----------------------------------------------------------------------------
1335 bool wxTextCtrl::IgnoreTextUpdate()
1337 if ( m_countUpdatesToIgnore
> 0 )
1339 m_countUpdatesToIgnore
--;
1347 bool wxTextCtrl::MarkDirtyOnChange()
1349 if ( m_dontMarkDirty
)
1351 m_dontMarkDirty
= false;
1359 void wxTextCtrl::SetMaxLength(unsigned long len
)
1361 if ( !HasFlag(wxTE_MULTILINE
) )
1363 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1365 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1366 // we had tried to enter more text than allowed by max text length and
1367 // the text wasn't really changed
1369 // to detect this and generate TEXT_MAXLEN event instead of
1370 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1372 // when max len is set to 0 we disconnect our handler as it means that
1373 // we shouldn't check anything any more
1376 g_signal_connect (m_text
, "insert_text",
1377 G_CALLBACK (gtk_insert_text_callback
), this);
1381 g_signal_handlers_disconnect_by_func (m_text
,
1382 (gpointer
) gtk_insert_text_callback
, this);
1387 void wxTextCtrl::SetSelection( long from
, long to
)
1389 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1391 if (from
== -1 && to
== -1)
1394 to
= GetValue().length();
1397 if ( IsMultiLine() )
1399 GtkTextIter fromi
, toi
;
1400 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1401 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1403 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1404 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1408 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1412 void wxTextCtrl::ShowPosition( long pos
)
1417 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, int(pos
));
1418 GtkTextMark
* mark
= gtk_text_buffer_get_mark(m_buffer
, "ShowPosition");
1419 gtk_text_buffer_move_mark(m_buffer
, mark
, &iter
);
1421 // defer until Thaw, text view is not using m_buffer now
1422 m_showPositionOnThaw
= mark
;
1424 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text
), mark
);
1428 wxTextCtrlHitTestResult
1429 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1431 if ( !IsMultiLine() )
1434 return wxTE_HT_UNKNOWN
;
1438 gtk_text_view_window_to_buffer_coords
1440 GTK_TEXT_VIEW(m_text
),
1441 GTK_TEXT_WINDOW_TEXT
,
1447 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1449 *pos
= gtk_text_iter_get_offset(&iter
);
1451 return wxTE_HT_ON_TEXT
;
1454 long wxTextCtrl::GetInsertionPoint() const
1456 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1458 if ( IsMultiLine() )
1460 // There is no direct accessor for the cursor, but
1461 // internally, the cursor is the "mark" called
1462 // "insert" in the text view's btree structure.
1464 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1466 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1468 return gtk_text_iter_get_offset( &cursor
);
1472 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1476 wxTextPos
wxTextCtrl::GetLastPosition() const
1478 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1482 if ( IsMultiLine() )
1485 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1487 pos
= gtk_text_iter_get_offset( &end
);
1491 pos
= GTK_ENTRY(m_text
)->text_length
;
1497 void wxTextCtrl::Remove( long from
, long to
)
1499 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1501 if ( IsMultiLine() )
1503 GtkTextIter fromi
, toi
;
1504 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1505 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1507 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1510 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1513 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1515 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1521 SetInsertionPoint( from
);
1526 void wxTextCtrl::Cut()
1528 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1530 if ( IsMultiLine() )
1531 g_signal_emit_by_name (m_text
, "cut-clipboard");
1533 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1536 void wxTextCtrl::Copy()
1538 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1540 if ( IsMultiLine() )
1541 g_signal_emit_by_name (m_text
, "copy-clipboard");
1543 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1546 void wxTextCtrl::Paste()
1548 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1550 if ( IsMultiLine() )
1551 g_signal_emit_by_name (m_text
, "paste-clipboard");
1553 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1557 void wxTextCtrl::Undo()
1560 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1563 void wxTextCtrl::Redo()
1566 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1569 bool wxTextCtrl::CanUndo() const
1572 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1576 bool wxTextCtrl::CanRedo() const
1579 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1583 // If the return values from and to are the same, there is no
1585 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1587 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1591 bool haveSelection
= false;
1593 if ( IsMultiLine() )
1595 GtkTextIter ifrom
, ito
;
1596 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1598 haveSelection
= true;
1599 from
= gtk_text_iter_get_offset(&ifrom
);
1600 to
= gtk_text_iter_get_offset(&ito
);
1603 else // not multi-line
1605 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1608 haveSelection
= true;
1612 if (! haveSelection
)
1613 from
= to
= GetInsertionPoint();
1617 // exchange them to be compatible with wxMSW
1630 bool wxTextCtrl::IsEditable() const
1632 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1634 if ( IsMultiLine() )
1636 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1640 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1644 bool wxTextCtrl::IsModified() const
1649 void wxTextCtrl::Clear()
1651 SetValue( wxEmptyString
);
1654 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1656 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1658 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1660 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1662 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1663 event
.SetEventObject(this);
1664 event
.SetString(GetValue());
1665 if ( GetEventHandler()->ProcessEvent(event
) )
1669 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1671 if ( IsSingleLine() )
1673 // This will invoke the dialog default action, such
1674 // as the clicking the default button.
1676 wxWindow
*top_frame
= m_parent
;
1677 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1678 top_frame
= top_frame
->GetParent();
1680 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1682 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1684 if (window
->default_widget
)
1686 gtk_widget_activate (window
->default_widget
);
1696 GtkWidget
* wxTextCtrl::GetConnectWidget()
1698 return GTK_WIDGET(m_text
);
1701 GdkWindow
*wxTextCtrl::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1703 if ( IsMultiLine() )
1705 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1706 GTK_TEXT_WINDOW_TEXT
);
1710 return GTK_ENTRY(m_text
)->text_area
;
1714 // the font will change for subsequent text insertiongs
1715 bool wxTextCtrl::SetFont( const wxFont
&font
)
1717 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1719 if ( !wxTextCtrlBase::SetFont(font
) )
1721 // font didn't change, nothing to do
1725 if ( IsMultiLine() )
1727 SetUpdateFont(true);
1729 m_defaultStyle
.SetFont(font
);
1731 ChangeFontGlobally();
1737 void wxTextCtrl::ChangeFontGlobally()
1739 // this method is very inefficient and hence should be called as rarely as
1742 // TODO: it can be implemented much more efficiently for GTK2
1743 wxASSERT_MSG( IsMultiLine(),
1744 _T("shouldn't be called for single line controls") );
1746 wxString value
= GetValue();
1747 if ( !value
.empty() )
1749 SetUpdateFont(false);
1756 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1758 if ( !wxControl::SetForegroundColour(colour
) )
1761 // update default fg colour too
1762 m_defaultStyle
.SetTextColour(colour
);
1767 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1769 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1771 if ( !wxControl::SetBackgroundColour( colour
) )
1774 if (!m_backgroundColour
.Ok())
1777 // change active background color too
1778 m_defaultStyle
.SetBackgroundColour( colour
);
1783 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1785 if ( IsMultiLine() )
1787 if ( style
.IsDefault() )
1793 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1795 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1796 _T("invalid range in wxTextCtrl::SetStyle") );
1798 GtkTextIter starti
, endi
;
1799 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1800 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1802 wxGtkTextApplyTagsFromAttr( m_widget
, m_buffer
, style
, &starti
, &endi
);
1806 //else: single line text controls don't support styles
1811 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1813 gtk_widget_modify_style(m_text
, style
);
1816 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1821 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1826 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1831 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1836 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1841 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1843 event
.Enable( CanCut() );
1846 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1848 event
.Enable( CanCopy() );
1851 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1853 event
.Enable( CanPaste() );
1856 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1858 event
.Enable( CanUndo() );
1861 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1863 event
.Enable( CanRedo() );
1866 wxSize
wxTextCtrl::DoGetBestSize() const
1868 // FIXME should be different for multi-line controls...
1869 wxSize
ret( wxControl::DoGetBestSize() );
1870 wxSize
best(80, ret
.y
);
1871 CacheBestSize(best
);
1875 // ----------------------------------------------------------------------------
1877 // ----------------------------------------------------------------------------
1879 void wxTextCtrl::Freeze()
1881 wxCHECK_RET(m_text
!= NULL
, wxT("invalid text ctrl"));
1883 if ( HasFlag(wxTE_MULTILINE
) )
1885 if (m_freezeCount
++ == 0)
1887 // freeze textview updates and remove buffer
1888 g_signal_connect (m_text
, "expose_event",
1889 G_CALLBACK (gtk_text_exposed_callback
), this);
1890 g_signal_connect (m_widget
, "expose_event",
1891 G_CALLBACK (gtk_text_exposed_callback
), this);
1892 gtk_widget_set_sensitive(m_widget
, false);
1893 g_object_ref(m_buffer
);
1894 GtkTextBuffer
* buf_new
= gtk_text_buffer_new(NULL
);
1895 GtkTextMark
* mark
= GTK_TEXT_VIEW(m_text
)->first_para_mark
;
1896 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), buf_new
);
1897 // gtk_text_view_set_buffer adds its own reference
1898 g_object_unref(buf_new
);
1899 // This mark should be deleted when the buffer is changed,
1900 // but it's not (in GTK+ up to at least 2.10.6).
1901 // Otherwise these anonymous marks start to build up in the buffer,
1902 // and Freeze takes longer and longer each time it is called.
1903 if (GTK_IS_TEXT_MARK(mark
) && !gtk_text_mark_get_deleted(mark
))
1904 gtk_text_buffer_delete_mark(m_buffer
, mark
);
1909 void wxTextCtrl::Thaw()
1911 if ( HasFlag(wxTE_MULTILINE
) )
1913 wxCHECK_RET(m_freezeCount
!= 0, _T("Thaw() without matching Freeze()"));
1915 if (--m_freezeCount
== 0)
1917 // Reattach buffer and thaw textview updates
1918 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1919 g_object_unref(m_buffer
);
1920 gtk_widget_set_sensitive(m_widget
, true);
1921 g_signal_handlers_disconnect_by_func (m_widget
,
1922 (gpointer
) gtk_text_exposed_callback
, this);
1923 g_signal_handlers_disconnect_by_func (m_text
,
1924 (gpointer
) gtk_text_exposed_callback
, this);
1925 if (m_showPositionOnThaw
!= NULL
)
1927 gtk_text_view_scroll_mark_onscreen(
1928 GTK_TEXT_VIEW(m_text
), m_showPositionOnThaw
);
1929 m_showPositionOnThaw
= NULL
;
1935 // ----------------------------------------------------------------------------
1936 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1937 // ----------------------------------------------------------------------------
1939 // FIXME: when dragging on a link the sample gets an "Unknown event".
1940 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1941 // a buggy sample, or something else
1942 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1945 if( !HasFlag(wxTE_AUTO_URL
) )
1949 GtkTextIter start
, end
;
1950 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1953 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1954 event
.GetX(), event
.GetY(), &x
, &y
);
1956 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1957 if (!gtk_text_iter_has_tag(&end
, tag
))
1959 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1960 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1964 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1965 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1968 if(!gtk_text_iter_begins_tag(&start
, tag
))
1969 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1970 if(!gtk_text_iter_ends_tag(&end
, tag
))
1971 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1973 // Native context menu is probably not desired on an URL.
1974 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1975 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1978 wxTextUrlEvent
url_event(m_windowId
, event
,
1979 gtk_text_iter_get_offset(&start
),
1980 gtk_text_iter_get_offset(&end
));
1982 InitCommandEvent(url_event
);
1983 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1984 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1985 GetEventHandler()->ProcessEvent(url_event
);
1988 bool wxTextCtrl::GTKProcessEvent(wxEvent
& event
) const
1990 bool rc
= wxTextCtrlBase::GTKProcessEvent(event
);
1992 // GtkTextView starts a drag operation when left mouse button is pressed
1993 // and ends it when it is released and if it doesn't get the release event
1994 // the next click on a control results in an assertion failure inside
1995 // gtk_text_view_start_selection_drag() which simply *kills* the program
1996 // without anything we can do about it, so always let GTK+ have this event
1997 return rc
&& (IsSingleLine() || event
.GetEventType() != wxEVT_LEFT_UP
);
2002 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2004 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);