]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/textctrl.cpp
Forward declare wxFont in wx/gtk/private.h.
[wxWidgets.git] / src / gtk / textctrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_TEXTCTRL
14
15#include "wx/textctrl.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/log.h"
20 #include "wx/utils.h"
21 #include "wx/settings.h"
22 #include "wx/math.h"
23#endif
24
25#include "wx/scopeguard.h"
26#include "wx/strconv.h"
27#include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <ctype.h>
32
33#include "wx/gtk/private.h"
34
35// ----------------------------------------------------------------------------
36// helpers
37// ----------------------------------------------------------------------------
38
39extern "C" {
40static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
41 GtkTextTag *tag,
42 GtkTextIter * WXUNUSED(start),
43 GtkTextIter * WXUNUSED(end),
44 char *prefix)
45{
46 gchar *name;
47 g_object_get (tag, "name", &name, NULL);
48
49 if (!name || strncmp(name, prefix, strlen(prefix)))
50 // anonymous tag or not starting with prefix - don't remove
51 g_signal_stop_emission_by_name (buffer, "remove_tag");
52
53 g_free(name);
54}
55}
56
57// remove all tags starting with the given prefix from the start..end range
58static void
59wxGtkTextRemoveTagsWithPrefix(GtkTextBuffer *text_buffer,
60 const char *prefix,
61 GtkTextIter *start,
62 GtkTextIter *end)
63{
64 gulong remove_handler_id = g_signal_connect
65 (
66 text_buffer,
67 "remove_tag",
68 G_CALLBACK(wxGtkOnRemoveTag),
69 gpointer(prefix)
70 );
71 gtk_text_buffer_remove_all_tags(text_buffer, start, end);
72 g_signal_handler_disconnect(text_buffer, remove_handler_id);
73}
74
75static void wxGtkTextApplyTagsFromAttr(GtkWidget *text,
76 GtkTextBuffer *text_buffer,
77 const wxTextAttr& attr,
78 GtkTextIter *start,
79 GtkTextIter *end)
80{
81 static gchar buf[1024];
82 GtkTextTag *tag;
83
84 if (attr.HasFont())
85 {
86 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXFONT", start, end);
87
88 wxFont font(attr.GetFont());
89
90 PangoFontDescription *font_description = font.GetNativeFontInfo()->description;
91 wxGtkString font_string(pango_font_description_to_string(font_description));
92 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string.c_str());
93 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
94 buf );
95 if (!tag)
96 tag = gtk_text_buffer_create_tag( text_buffer, buf,
97 "font-desc", font_description,
98 NULL );
99 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
100
101 if (font.GetUnderlined())
102 {
103 g_snprintf(buf, sizeof(buf), "WXFONTUNDERLINE");
104 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
105 buf );
106 if (!tag)
107 tag = gtk_text_buffer_create_tag( text_buffer, buf,
108 "underline-set", TRUE,
109 "underline", PANGO_UNDERLINE_SINGLE,
110 NULL );
111 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
112 }
113 if ( font.GetStrikethrough() )
114 {
115 g_snprintf(buf, sizeof(buf), "WXFONTSTRIKETHROUGH");
116 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
117 buf );
118 if (!tag)
119 tag = gtk_text_buffer_create_tag( text_buffer, buf,
120 "strikethrough-set", TRUE,
121 "strikethrough", TRUE,
122 NULL );
123 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
124 }
125 }
126
127 if (attr.HasTextColour())
128 {
129 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXFORECOLOR", start, end);
130
131 const GdkColor *colFg = attr.GetTextColour().GetColor();
132 g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
133 colFg->red, colFg->green, colFg->blue);
134 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
135 buf );
136 if (!tag)
137 tag = gtk_text_buffer_create_tag( text_buffer, buf,
138 "foreground-gdk", colFg, NULL );
139 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
140 }
141
142 if (attr.HasBackgroundColour())
143 {
144 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXBACKCOLOR", start, end);
145
146 const GdkColor *colBg = attr.GetBackgroundColour().GetColor();
147 g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
148 colBg->red, colBg->green, colBg->blue);
149 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
150 buf );
151 if (!tag)
152 tag = gtk_text_buffer_create_tag( text_buffer, buf,
153 "background-gdk", colBg, NULL );
154 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
155 }
156
157 if (attr.HasAlignment())
158 {
159 GtkTextIter para_start, para_end = *end;
160 gtk_text_buffer_get_iter_at_line( text_buffer,
161 &para_start,
162 gtk_text_iter_get_line(start) );
163 gtk_text_iter_forward_line(&para_end);
164
165 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXALIGNMENT", &para_start, &para_end);
166
167 GtkJustification align;
168 switch (attr.GetAlignment())
169 {
170 default:
171 align = GTK_JUSTIFY_LEFT;
172 break;
173 case wxTEXT_ALIGNMENT_RIGHT:
174 align = GTK_JUSTIFY_RIGHT;
175 break;
176 case wxTEXT_ALIGNMENT_CENTER:
177 align = GTK_JUSTIFY_CENTER;
178 break;
179// gtk+ doesn't support justify before gtk+-2.11.0 with pango-1.17 being available
180// (but if new enough pango isn't available it's a mere gtk warning)
181#if GTK_CHECK_VERSION(2,11,0)
182 case wxTEXT_ALIGNMENT_JUSTIFIED:
183 if (!gtk_check_version(2,11,0))
184 align = GTK_JUSTIFY_FILL;
185 else
186 align = GTK_JUSTIFY_LEFT;
187 break;
188#endif
189 }
190
191 g_snprintf(buf, sizeof(buf), "WXALIGNMENT %d", align);
192 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
193 buf );
194 if (!tag)
195 tag = gtk_text_buffer_create_tag( text_buffer, buf,
196 "justification", align, NULL );
197 gtk_text_buffer_apply_tag( text_buffer, tag, &para_start, &para_end );
198 }
199
200 if (attr.HasLeftIndent())
201 {
202 // Indentation attribute
203
204 // Clear old indentation tags
205 GtkTextIter para_start, para_end = *end;
206 gtk_text_buffer_get_iter_at_line( text_buffer,
207 &para_start,
208 gtk_text_iter_get_line(start) );
209 gtk_text_iter_forward_line(&para_end);
210
211 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXINDENT", &para_start, &para_end);
212
213 // Convert indent from 1/10th of a mm into pixels
214 float factor =
215 (float)gdk_screen_get_width(gtk_widget_get_screen(text)) /
216 gdk_screen_get_width_mm(gtk_widget_get_screen(text)) / 10;
217
218 const int indent = (int)(factor * attr.GetLeftIndent());
219 const int subIndent = (int)(factor * attr.GetLeftSubIndent());
220
221 gint gindent;
222 gint gsubindent;
223
224 if (subIndent >= 0)
225 {
226 gindent = indent;
227 gsubindent = -subIndent;
228 }
229 else
230 {
231 gindent = -subIndent;
232 gsubindent = indent;
233 }
234
235 g_snprintf(buf, sizeof(buf), "WXINDENT %d %d", gindent, gsubindent);
236 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
237 buf );
238 if (!tag)
239 tag = gtk_text_buffer_create_tag( text_buffer, buf,
240 "left-margin", gindent, "indent", gsubindent, NULL );
241 gtk_text_buffer_apply_tag (text_buffer, tag, &para_start, &para_end);
242 }
243
244 if (attr.HasTabs())
245 {
246 // Set tab stops
247
248 // Clear old tabs
249 GtkTextIter para_start, para_end = *end;
250 gtk_text_buffer_get_iter_at_line( text_buffer,
251 &para_start,
252 gtk_text_iter_get_line(start) );
253 gtk_text_iter_forward_line(&para_end);
254
255 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXTABS", &para_start, &para_end);
256
257 const wxArrayInt& tabs = attr.GetTabs();
258
259 wxString tagname = wxT("WXTABS");
260 g_snprintf(buf, sizeof(buf), "WXTABS");
261 for (size_t i = 0; i < tabs.GetCount(); i++)
262 tagname += wxString::Format(wxT(" %d"), tabs[i]);
263
264 const wxWX2MBbuf buftag = tagname.utf8_str();
265
266 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
267 buftag );
268 if (!tag)
269 {
270 // Factor to convert from 1/10th of a mm into pixels
271 float factor =
272 (float)gdk_screen_get_width(gtk_widget_get_screen(text)) /
273 gdk_screen_get_width_mm(gtk_widget_get_screen(text)) / 10;
274
275 PangoTabArray* tabArray = pango_tab_array_new(tabs.GetCount(), TRUE);
276 for (size_t i = 0; i < tabs.GetCount(); i++)
277 pango_tab_array_set_tab(tabArray, i, PANGO_TAB_LEFT, (gint)(tabs[i] * factor));
278 tag = gtk_text_buffer_create_tag( text_buffer, buftag,
279 "tabs", tabArray, NULL );
280 pango_tab_array_free(tabArray);
281 }
282 gtk_text_buffer_apply_tag (text_buffer, tag, &para_start, &para_end);
283 }
284}
285
286static void wxGtkTextInsert(GtkWidget *text,
287 GtkTextBuffer *text_buffer,
288 const wxTextAttr& attr,
289 const wxCharBuffer& buffer)
290
291{
292 gint start_offset;
293 GtkTextIter iter, start;
294
295 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
296 gtk_text_buffer_get_insert (text_buffer) );
297 start_offset = gtk_text_iter_get_offset (&iter);
298 gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) );
299
300 gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
301
302 wxGtkTextApplyTagsFromAttr(text, text_buffer, attr, &start, &iter);
303}
304
305// Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
306
307extern "C" {
308static void
309au_apply_tag_callback(GtkTextBuffer *buffer,
310 GtkTextTag *tag,
311 GtkTextIter * WXUNUSED(start),
312 GtkTextIter * WXUNUSED(end),
313 gpointer WXUNUSED(textctrl))
314{
315 if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"))
316 g_signal_stop_emission_by_name (buffer, "apply_tag");
317}
318}
319
320//-----------------------------------------------------------------------------
321// GtkTextCharPredicates for gtk_text_iter_*_find_char
322//-----------------------------------------------------------------------------
323
324extern "C" {
325static gboolean
326pred_whitespace(gunichar ch, gpointer WXUNUSED(user_data))
327{
328 return g_unichar_isspace(ch);
329}
330}
331
332extern "C" {
333static gboolean
334pred_non_whitespace (gunichar ch, gpointer WXUNUSED(user_data))
335{
336 return !g_unichar_isspace(ch);
337}
338}
339
340extern "C" {
341static gboolean
342pred_nonpunct (gunichar ch, gpointer WXUNUSED(user_data))
343{
344 return !g_unichar_ispunct(ch);
345}
346}
347
348extern "C" {
349static gboolean
350pred_nonpunct_or_slash (gunichar ch, gpointer WXUNUSED(user_data))
351{
352 return !g_unichar_ispunct(ch) || ch == '/';
353}
354}
355
356//-----------------------------------------------------------------------------
357// Check for links between s and e and correct tags as necessary
358//-----------------------------------------------------------------------------
359
360// This function should be made match better while being efficient at one point.
361// Most probably with a row of regular expressions.
362extern "C" {
363static void
364au_check_word( GtkTextIter *s, GtkTextIter *e )
365{
366 static const char *const URIPrefixes[] =
367 {
368 "http://",
369 "ftp://",
370 "www.",
371 "ftp.",
372 "mailto://",
373 "https://",
374 "file://",
375 "nntp://",
376 "news://",
377 "telnet://",
378 "mms://",
379 "gopher://",
380 "prospero://",
381 "wais://",
382 };
383
384 GtkTextIter start = *s, end = *e;
385 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
386
387 // Get our special link tag
388 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
389
390 // Get rid of punctuation from beginning and end.
391 // Might want to move this to au_check_range if an improved link checking doesn't
392 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
393 if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) )
394 gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e );
395
396 gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start );
397 gtk_text_iter_forward_char(&end);
398
399 wxGtkString text(gtk_text_iter_get_text( &start, &end ));
400 size_t len = strlen(text), prefix_len;
401 size_t n;
402
403 for( n = 0; n < WXSIZEOF(URIPrefixes); ++n )
404 {
405 prefix_len = strlen(URIPrefixes[n]);
406 if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len))
407 break;
408 }
409
410 if(n < WXSIZEOF(URIPrefixes))
411 {
412 gulong signal_id = g_signal_handler_find (buffer,
413 (GSignalMatchType) (G_SIGNAL_MATCH_FUNC),
414 0, 0, NULL,
415 (gpointer)au_apply_tag_callback, NULL);
416
417 g_signal_handler_block (buffer, signal_id);
418 gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
419 g_signal_handler_unblock (buffer, signal_id);
420 }
421}
422}
423
424extern "C" {
425static void
426au_check_range(GtkTextIter *s,
427 GtkTextIter *range_end)
428{
429 GtkTextIter range_start = *s;
430 GtkTextIter word_end;
431 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
432 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
433
434 gtk_text_buffer_remove_tag(buffer, tag, s, range_end);
435
436 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start)))
437 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
438
439 while(!gtk_text_iter_equal(&range_start, range_end))
440 {
441 word_end = range_start;
442 gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end);
443
444 // Now we should have a word delimited by range_start and word_end, correct link tags
445 au_check_word(&range_start, &word_end);
446
447 range_start = word_end;
448 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
449 }
450}
451}
452
453//-----------------------------------------------------------------------------
454// "insert-text" for GtkTextBuffer
455//-----------------------------------------------------------------------------
456
457extern "C" {
458static void
459au_insert_text_callback(GtkTextBuffer * WXUNUSED(buffer),
460 GtkTextIter *end,
461 gchar *text,
462 gint len,
463 wxTextCtrl *win)
464{
465 if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
466 return;
467
468 GtkTextIter start = *end;
469 gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len));
470
471 GtkTextIter line_start = start;
472 GtkTextIter line_end = *end;
473 GtkTextIter words_start = start;
474 GtkTextIter words_end = *end;
475
476 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start));
477 gtk_text_iter_forward_to_line_end(&line_end);
478 gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start);
479 gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end);
480
481 au_check_range(&words_start, &words_end);
482}
483}
484
485//-----------------------------------------------------------------------------
486// "delete-range" for GtkTextBuffer
487//-----------------------------------------------------------------------------
488
489extern "C" {
490static void
491au_delete_range_callback(GtkTextBuffer * WXUNUSED(buffer),
492 GtkTextIter *start,
493 GtkTextIter *end,
494 wxTextCtrl *win)
495{
496 if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
497 return;
498
499 GtkTextIter line_start = *start, line_end = *end;
500
501 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start));
502 gtk_text_iter_forward_to_line_end(&line_end);
503 gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start);
504 gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end);
505
506 au_check_range(start, end);
507}
508}
509
510//-----------------------------------------------------------------------------
511// "populate_popup" from text control and "unmap" from its poup menu
512//-----------------------------------------------------------------------------
513
514extern "C" {
515static void
516gtk_textctrl_popup_unmap( GtkMenu *WXUNUSED(menu), wxTextCtrl* win )
517{
518 win->GTKEnableFocusOutEvent();
519}
520}
521
522extern "C" {
523static void
524gtk_textctrl_populate_popup( GtkEntry *WXUNUSED(entry), GtkMenu *menu, wxTextCtrl *win )
525{
526 win->GTKDisableFocusOutEvent();
527
528 g_signal_connect (menu, "unmap", G_CALLBACK (gtk_textctrl_popup_unmap), win );
529}
530}
531
532//-----------------------------------------------------------------------------
533// "changed"
534//-----------------------------------------------------------------------------
535
536extern "C" {
537static void
538gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
539{
540 if ( win->IgnoreTextUpdate() )
541 return;
542
543 if (!win->m_hasVMT) return;
544
545 if ( win->MarkDirtyOnChange() )
546 win->MarkDirty();
547
548 win->SendTextUpdatedEvent();
549}
550}
551
552//-----------------------------------------------------------------------------
553// clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
554//-----------------------------------------------------------------------------
555
556// common part of the event handlers below
557static void
558handle_text_clipboard_callback( GtkWidget *widget, wxTextCtrl *win,
559 wxEventType eventType, const gchar * signal_name)
560{
561 wxClipboardTextEvent event( eventType, win->GetId() );
562 event.SetEventObject( win );
563 if ( win->HandleWindowEvent( event ) )
564 {
565 // don't let the default processing to take place if we did something
566 // ourselves in the event handler
567 g_signal_stop_emission_by_name (widget, signal_name);
568 }
569}
570
571extern "C" {
572static void
573gtk_copy_clipboard_callback( GtkWidget *widget, wxTextCtrl *win )
574{
575 handle_text_clipboard_callback(
576 widget, win, wxEVT_COMMAND_TEXT_COPY, "copy-clipboard" );
577}
578
579static void
580gtk_cut_clipboard_callback( GtkWidget *widget, wxTextCtrl *win )
581{
582 handle_text_clipboard_callback(
583 widget, win, wxEVT_COMMAND_TEXT_CUT, "cut-clipboard" );
584}
585
586static void
587gtk_paste_clipboard_callback( GtkWidget *widget, wxTextCtrl *win )
588{
589 handle_text_clipboard_callback(
590 widget, win, wxEVT_COMMAND_TEXT_PASTE, "paste-clipboard" );
591}
592}
593
594//-----------------------------------------------------------------------------
595// "mark_set"
596//-----------------------------------------------------------------------------
597
598extern "C" {
599static void mark_set(GtkTextBuffer*, GtkTextIter*, GtkTextMark* mark, GSList** markList)
600{
601 if (gtk_text_mark_get_name(mark) == NULL)
602 *markList = g_slist_prepend(*markList, mark);
603}
604}
605
606//-----------------------------------------------------------------------------
607// wxTextCtrl
608//-----------------------------------------------------------------------------
609
610BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
611 EVT_CHAR(wxTextCtrl::OnChar)
612
613 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
614 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
615 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
616 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
617 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
618
619 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
620 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
621 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
622 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
623 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
624
625 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
626 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
627 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent)
628 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent)
629 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent)
630 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent)
631 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent)
632 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent)
633 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent)
634END_EVENT_TABLE()
635
636void wxTextCtrl::Init()
637{
638 m_dontMarkDirty =
639 m_modified = false;
640
641 m_countUpdatesToIgnore = 0;
642
643 SetUpdateFont(false);
644
645 m_text = NULL;
646 m_showPositionOnThaw = NULL;
647 m_anonymousMarkList = NULL;
648}
649
650wxTextCtrl::~wxTextCtrl()
651{
652 if (m_anonymousMarkList)
653 g_slist_free(m_anonymousMarkList);
654}
655
656wxTextCtrl::wxTextCtrl( wxWindow *parent,
657 wxWindowID id,
658 const wxString &value,
659 const wxPoint &pos,
660 const wxSize &size,
661 long style,
662 const wxValidator& validator,
663 const wxString &name )
664{
665 Init();
666
667 Create( parent, id, value, pos, size, style, validator, name );
668}
669
670bool wxTextCtrl::Create( wxWindow *parent,
671 wxWindowID id,
672 const wxString &value,
673 const wxPoint &pos,
674 const wxSize &size,
675 long style,
676 const wxValidator& validator,
677 const wxString &name )
678{
679 if (!PreCreation( parent, pos, size ) ||
680 !CreateBase( parent, id, pos, size, style, validator, name ))
681 {
682 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
683 return false;
684 }
685
686 bool multi_line = (style & wxTE_MULTILINE) != 0;
687
688 if (multi_line)
689 {
690 m_buffer = gtk_text_buffer_new(NULL);
691 gulong sig_id = g_signal_connect(m_buffer, "mark_set", G_CALLBACK(mark_set), &m_anonymousMarkList);
692 // Create view
693 m_text = gtk_text_view_new_with_buffer(m_buffer);
694 // gtk_text_view_set_buffer adds its own reference
695 g_object_unref(m_buffer);
696 g_signal_handler_disconnect(m_buffer, sig_id);
697
698 // create "ShowPosition" marker
699 GtkTextIter iter;
700 gtk_text_buffer_get_start_iter(m_buffer, &iter);
701 gtk_text_buffer_create_mark(m_buffer, "ShowPosition", &iter, true);
702
703 // create scrolled window
704 m_widget = gtk_scrolled_window_new( NULL, NULL );
705 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
706 GTK_POLICY_AUTOMATIC,
707 style & wxTE_NO_VSCROLL
708 ? GTK_POLICY_NEVER
709 : GTK_POLICY_AUTOMATIC );
710 // for ScrollLines/Pages
711 m_scrollBar[1] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(m_widget)));
712
713 // Insert view into scrolled window
714 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
715
716 GTKSetWrapMode();
717
718 GTKScrolledWindowSetBorder(m_widget, style);
719
720 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
721
722 gtk_widget_set_can_focus(m_widget, FALSE);
723 }
724 else
725 {
726 // a single-line text control: no need for scrollbars
727 m_widget =
728 m_text = gtk_entry_new();
729 // work around probable bug in GTK+ 2.18 when calling WriteText on a
730 // new, empty control, see http://trac.wxwidgets.org/ticket/11409
731 gtk_entry_get_text((GtkEntry*)m_text);
732
733 if (style & wxNO_BORDER)
734 g_object_set (m_text, "has-frame", FALSE, NULL);
735
736 }
737 g_object_ref(m_widget);
738
739 m_parent->DoAddChild( this );
740
741 m_focusWidget = m_text;
742
743 PostCreation(size);
744
745 if (multi_line)
746 {
747 gtk_widget_show(m_text);
748 }
749
750 // We want to be notified about text changes.
751 if (multi_line)
752 {
753 g_signal_connect (m_buffer, "changed",
754 G_CALLBACK (gtk_text_changed_callback), this);
755 }
756 else
757 {
758 g_signal_connect (m_text, "changed",
759 G_CALLBACK (gtk_text_changed_callback), this);
760 }
761
762 // Catch to disable focus out handling
763 g_signal_connect (m_text, "populate_popup",
764 G_CALLBACK (gtk_textctrl_populate_popup),
765 this);
766
767 if (!value.empty())
768 {
769 SetValue( value );
770 }
771
772 if (style & wxTE_PASSWORD)
773 GTKSetVisibility();
774
775 if (style & wxTE_READONLY)
776 GTKSetEditable();
777
778 // left justification (alignment) is the default anyhow
779 if ( style & (wxTE_RIGHT | wxTE_CENTRE) )
780 GTKSetJustification();
781
782 if (multi_line)
783 {
784 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
785 if (style & wxTE_AUTO_URL)
786 {
787 GtkTextIter start, end;
788
789 // We create our wxUrl tag here for slight efficiency gain - we
790 // don't have to check for the tag existance in callbacks,
791 // hereby it's guaranteed to exist.
792 gtk_text_buffer_create_tag(m_buffer, "wxUrl",
793 "foreground", "blue",
794 "underline", PANGO_UNDERLINE_SINGLE,
795 NULL);
796
797 // Check for URLs after each text change
798 g_signal_connect_after (m_buffer, "insert_text",
799 G_CALLBACK (au_insert_text_callback), this);
800 g_signal_connect_after (m_buffer, "delete_range",
801 G_CALLBACK (au_delete_range_callback), this);
802
803 // Block all wxUrl tag applying unless we do it ourselves, in which case we
804 // block this callback temporarily. This takes care of gtk+ internal
805 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
806 // which is undesired because only a part of the URL might be copied.
807 // The insert-text signal emitted inside it will take care of newly formed
808 // or wholly copied URLs.
809 g_signal_connect (m_buffer, "apply_tag",
810 G_CALLBACK (au_apply_tag_callback), NULL);
811
812 // Check for URLs in the initial string passed to Create
813 gtk_text_buffer_get_start_iter(m_buffer, &start);
814 gtk_text_buffer_get_end_iter(m_buffer, &end);
815 au_check_range(&start, &end);
816 }
817 }
818 else // single line
819 {
820 // do the right thing with Enter presses depending on whether we have
821 // wxTE_PROCESS_ENTER or not
822 GTKSetActivatesDefault();
823 }
824
825
826 g_signal_connect (m_text, "copy-clipboard",
827 G_CALLBACK (gtk_copy_clipboard_callback), this);
828 g_signal_connect (m_text, "cut-clipboard",
829 G_CALLBACK (gtk_cut_clipboard_callback), this);
830 g_signal_connect (m_text, "paste-clipboard",
831 G_CALLBACK (gtk_paste_clipboard_callback), this);
832
833 m_cursor = wxCursor( wxCURSOR_IBEAM );
834
835 return true;
836}
837
838GtkEditable *wxTextCtrl::GetEditable() const
839{
840 wxCHECK_MSG( IsSingleLine(), NULL, "shouldn't be called for multiline" );
841
842 return GTK_EDITABLE(m_text);
843}
844
845GtkEntry *wxTextCtrl::GetEntry() const
846{
847 return GTK_ENTRY(m_text);
848}
849
850// ----------------------------------------------------------------------------
851// flags handling
852// ----------------------------------------------------------------------------
853
854void wxTextCtrl::GTKSetEditable()
855{
856 gboolean editable = !HasFlag(wxTE_READONLY);
857 if ( IsSingleLine() )
858 gtk_editable_set_editable(GTK_EDITABLE(m_text), editable);
859 else
860 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text), editable);
861}
862
863void wxTextCtrl::GTKSetVisibility()
864{
865 wxCHECK_RET( IsSingleLine(),
866 "wxTE_PASSWORD is for single line text controls only" );
867
868 gtk_entry_set_visibility(GTK_ENTRY(m_text), !HasFlag(wxTE_PASSWORD));
869}
870
871void wxTextCtrl::GTKSetActivatesDefault()
872{
873 wxCHECK_RET( IsSingleLine(),
874 "wxTE_PROCESS_ENTER is for single line text controls only" );
875
876 gtk_entry_set_activates_default(GTK_ENTRY(m_text),
877 !HasFlag(wxTE_PROCESS_ENTER));
878}
879
880void wxTextCtrl::GTKSetWrapMode()
881{
882 // no wrapping in single line controls
883 if ( !IsMultiLine() )
884 return;
885
886 // translate wx wrapping style to GTK+
887 GtkWrapMode wrap;
888 if ( HasFlag( wxTE_DONTWRAP ) )
889 wrap = GTK_WRAP_NONE;
890 else if ( HasFlag( wxTE_CHARWRAP ) )
891 wrap = GTK_WRAP_CHAR;
892 else if ( HasFlag( wxTE_WORDWRAP ) )
893 wrap = GTK_WRAP_WORD;
894 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
895 wrap = GTK_WRAP_WORD_CHAR;
896
897 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
898}
899
900void wxTextCtrl::GTKSetJustification()
901{
902 if ( IsMultiLine() )
903 {
904 GtkJustification just;
905 if ( HasFlag(wxTE_RIGHT) )
906 just = GTK_JUSTIFY_RIGHT;
907 else if ( HasFlag(wxTE_CENTRE) )
908 just = GTK_JUSTIFY_CENTER;
909 else // wxTE_LEFT == 0
910 just = GTK_JUSTIFY_LEFT;
911
912 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text), just);
913 }
914 else // single line
915 {
916 gfloat align;
917 if ( HasFlag(wxTE_RIGHT) )
918 align = 1.0;
919 else if ( HasFlag(wxTE_CENTRE) )
920 align = 0.5;
921 else // single line
922 align = 0.0;
923
924 gtk_entry_set_alignment(GTK_ENTRY(m_text), align);
925 }
926}
927
928void wxTextCtrl::SetWindowStyleFlag(long style)
929{
930 long styleOld = GetWindowStyleFlag();
931
932 wxTextCtrlBase::SetWindowStyleFlag(style);
933
934 if ( (style & wxTE_READONLY) != (styleOld & wxTE_READONLY) )
935 GTKSetEditable();
936
937 if ( (style & wxTE_PASSWORD) != (styleOld & wxTE_PASSWORD) )
938 GTKSetVisibility();
939
940 if ( (style & wxTE_PROCESS_ENTER) != (styleOld & wxTE_PROCESS_ENTER) )
941 GTKSetActivatesDefault();
942
943 static const long flagsWrap = wxTE_WORDWRAP | wxTE_CHARWRAP | wxTE_DONTWRAP;
944 if ( (style & flagsWrap) != (styleOld & flagsWrap) )
945 GTKSetWrapMode();
946
947 static const long flagsAlign = wxTE_LEFT | wxTE_CENTRE | wxTE_RIGHT;
948 if ( (style & flagsAlign) != (styleOld & flagsAlign) )
949 GTKSetJustification();
950}
951
952// ----------------------------------------------------------------------------
953// control value
954// ----------------------------------------------------------------------------
955
956wxString wxTextCtrl::GetValue() const
957{
958 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
959
960 if ( IsMultiLine() )
961 {
962 GtkTextIter start;
963 gtk_text_buffer_get_start_iter( m_buffer, &start );
964 GtkTextIter end;
965 gtk_text_buffer_get_end_iter( m_buffer, &end );
966 wxGtkString text(gtk_text_buffer_get_text(m_buffer, &start, &end, true));
967
968 return wxGTK_CONV_BACK(text);
969 }
970 else // single line
971 {
972 return wxTextEntry::GetValue();
973 }
974}
975
976wxFontEncoding wxTextCtrl::GetTextEncoding() const
977{
978 // GTK+ uses UTF-8 internally, we need to convert to it but from which
979 // encoding?
980
981 // first check the default text style (we intentionally don't check the
982 // style for the current position as it doesn't make sense for SetValue())
983 const wxTextAttr& style = GetDefaultStyle();
984 wxFontEncoding enc = style.HasFontEncoding() ? style.GetFontEncoding()
985 : wxFONTENCODING_SYSTEM;
986
987 // fall back to the controls font if no style
988 if ( enc == wxFONTENCODING_SYSTEM && m_hasFont )
989 enc = GetFont().GetEncoding();
990
991 return enc;
992}
993
994bool wxTextCtrl::IsEmpty() const
995{
996 if ( IsMultiLine() )
997 return gtk_text_buffer_get_char_count(m_buffer) == 0;
998
999 return wxTextEntry::IsEmpty();
1000}
1001
1002void wxTextCtrl::DoSetValue( const wxString &value, int flags )
1003{
1004 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1005
1006 m_modified = false;
1007
1008 if ( !IsMultiLine() )
1009 {
1010 wxTextEntry::DoSetValue(value, flags);
1011 return;
1012 }
1013
1014 if (value.IsEmpty())
1015 {
1016 if ( !(flags & SetValue_SendEvent) )
1017 EnableTextChangedEvents(false);
1018
1019 gtk_text_buffer_set_text( m_buffer, "", 0 );
1020
1021 if ( !(flags & SetValue_SendEvent) )
1022 EnableTextChangedEvents(true);
1023
1024 return;
1025 }
1026
1027#if wxUSE_UNICODE
1028 const wxCharBuffer buffer(value.utf8_str());
1029#else
1030 wxFontEncoding enc = m_defaultStyle.HasFont()
1031 ? m_defaultStyle.GetFont().GetEncoding()
1032 : wxFONTENCODING_SYSTEM;
1033 if ( enc == wxFONTENCODING_SYSTEM )
1034 enc = GetTextEncoding();
1035
1036 const wxCharBuffer buffer(wxGTK_CONV_ENC(value, enc));
1037 if ( !buffer )
1038 {
1039 // see comment in WriteText() as to why we must warn the user about
1040 // this
1041 wxLogWarning(_("Failed to set text in the text control."));
1042 return;
1043 }
1044#endif
1045
1046 if ( !(flags & SetValue_SendEvent) )
1047 {
1048 EnableTextChangedEvents(false);
1049 }
1050
1051 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
1052
1053 if ( !m_defaultStyle.IsDefault() )
1054 {
1055 GtkTextIter start, end;
1056 gtk_text_buffer_get_bounds( m_buffer, &start, &end );
1057 wxGtkTextApplyTagsFromAttr(m_widget, m_buffer, m_defaultStyle,
1058 &start, &end);
1059 }
1060
1061 if ( !(flags & SetValue_SendEvent) )
1062 {
1063 EnableTextChangedEvents(true);
1064 }
1065}
1066
1067void wxTextCtrl::WriteText( const wxString &text )
1068{
1069 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1070
1071 // we're changing the text programmatically
1072 DontMarkDirtyOnNextChange();
1073
1074 if ( !IsMultiLine() )
1075 {
1076 wxTextEntry::WriteText(text);
1077 return;
1078 }
1079
1080#if wxUSE_UNICODE
1081 const wxCharBuffer buffer(text.utf8_str());
1082#else
1083 // check if we have a specific style for the current position
1084 wxFontEncoding enc = wxFONTENCODING_SYSTEM;
1085 wxTextAttr style;
1086 if ( GetStyle(GetInsertionPoint(), style) && style.HasFontEncoding() )
1087 {
1088 enc = style.GetFontEncoding();
1089 }
1090
1091 if ( enc == wxFONTENCODING_SYSTEM )
1092 enc = GetTextEncoding();
1093
1094 const wxCharBuffer buffer(wxGTK_CONV_ENC(text, enc));
1095 if ( !buffer )
1096 {
1097 // we must log an error here as losing the text like this can be a
1098 // serious problem (e.g. imagine the document edited by user being
1099 // empty instead of containing the correct text)
1100 wxLogWarning(_("Failed to insert text in the control."));
1101 return;
1102 }
1103#endif
1104
1105 // First remove the selection if there is one
1106 // TODO: Is there an easier GTK specific way to do this?
1107 long from, to;
1108 GetSelection(&from, &to);
1109 if (from != to)
1110 Remove(from, to);
1111
1112 // Insert the text
1113 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
1114
1115 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1116 // won't work when frozen, text view is not using m_buffer then
1117 if (!IsFrozen())
1118 {
1119 GtkAdjustment* adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
1120 const double value = gtk_adjustment_get_value(adj);
1121 const double upper = gtk_adjustment_get_upper(adj);
1122 const double page_size = gtk_adjustment_get_page_size(adj);
1123 if (wxIsSameDouble(value, upper - page_size))
1124 {
1125 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(m_text),
1126 gtk_text_buffer_get_insert(m_buffer), 0, false, 0, 1);
1127 }
1128 }
1129}
1130
1131wxString wxTextCtrl::GetLineText( long lineNo ) const
1132{
1133 wxString result;
1134 if ( IsMultiLine() )
1135 {
1136 GtkTextIter line;
1137 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
1138
1139 GtkTextIter end = line;
1140 // avoid skipping to the next line end if this one is empty
1141 if ( !gtk_text_iter_ends_line(&line) )
1142 gtk_text_iter_forward_to_line_end(&end);
1143
1144 wxGtkString text(gtk_text_buffer_get_text(m_buffer, &line, &end, true));
1145 result = wxGTK_CONV_BACK(text);
1146 }
1147 else
1148 {
1149 if (lineNo == 0)
1150 result = GetValue();
1151 }
1152 return result;
1153}
1154
1155void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
1156{
1157 /* If you implement this, don't forget to update the documentation!
1158 * (file docs/latex/wx/text.tex) */
1159 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1160}
1161
1162bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
1163{
1164 if ( IsMultiLine() )
1165 {
1166 GtkTextIter iter;
1167
1168 if (pos > GetLastPosition())
1169 return false;
1170
1171 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
1172
1173 if ( y )
1174 *y = gtk_text_iter_get_line(&iter);
1175 if ( x )
1176 *x = gtk_text_iter_get_line_offset(&iter);
1177 }
1178 else // single line control
1179 {
1180 if (pos <= gtk_entry_get_text_length(GTK_ENTRY(m_text)))
1181 {
1182 if ( y )
1183 *y = 0;
1184 if ( x )
1185 *x = pos;
1186 }
1187 else
1188 {
1189 // index out of bounds
1190 return false;
1191 }
1192 }
1193
1194 return true;
1195}
1196
1197long wxTextCtrl::XYToPosition(long x, long y ) const
1198{
1199 if ( IsSingleLine() )
1200 return 0;
1201
1202 GtkTextIter iter;
1203 if (y >= gtk_text_buffer_get_line_count (m_buffer))
1204 return -1;
1205
1206 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
1207 if (x >= gtk_text_iter_get_chars_in_line (&iter))
1208 return -1;
1209
1210 return gtk_text_iter_get_offset(&iter) + x;
1211}
1212
1213int wxTextCtrl::GetLineLength(long lineNo) const
1214{
1215 if ( IsMultiLine() )
1216 {
1217 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
1218 if (lineNo > last_line)
1219 return -1;
1220
1221 GtkTextIter iter;
1222 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
1223 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1224 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
1225 }
1226 else
1227 {
1228 wxString str = GetLineText (lineNo);
1229 return (int) str.length();
1230 }
1231}
1232
1233wxPoint wxTextCtrl::DoPositionToCoords(long pos) const
1234{
1235 if ( !IsMultiLine() )
1236 {
1237 // Single line text entry (GtkTextEntry) doesn't have support for
1238 // getting the coordinates for the given offset. Perhaps we could
1239 // find them ourselves by using GetTextExtent() but for now just leave
1240 // it unimplemented, this function is more useful for multiline
1241 // controls anyhow.
1242 return wxDefaultPosition;
1243 }
1244
1245 // Window coordinates for the given position is calculated by getting
1246 // the buffer coordinates and converting them to window coordinates.
1247 GtkTextView *textview = GTK_TEXT_VIEW(m_text);
1248
1249 GtkTextIter iter;
1250 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
1251
1252 GdkRectangle bufferCoords;
1253 gtk_text_view_get_iter_location(textview, &iter, &bufferCoords);
1254
1255 gint winCoordX = 0,
1256 winCoordY = 0;
1257 gtk_text_view_buffer_to_window_coords(textview, GTK_TEXT_WINDOW_WIDGET,
1258 bufferCoords.x, bufferCoords.y,
1259 &winCoordX, &winCoordY);
1260
1261 return wxPoint(winCoordX, winCoordY);
1262}
1263
1264int wxTextCtrl::GetNumberOfLines() const
1265{
1266 if ( IsMultiLine() )
1267 {
1268 return gtk_text_buffer_get_line_count( m_buffer );
1269 }
1270 else // single line
1271 {
1272 return 1;
1273 }
1274}
1275
1276void wxTextCtrl::SetInsertionPoint( long pos )
1277{
1278 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1279
1280 if ( IsMultiLine() )
1281 {
1282 GtkTextIter iter;
1283 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1284 gtk_text_buffer_place_cursor( m_buffer, &iter );
1285 GtkTextMark* mark = gtk_text_buffer_get_insert(m_buffer);
1286 if (IsFrozen())
1287 // defer until Thaw, text view is not using m_buffer now
1288 m_showPositionOnThaw = mark;
1289 else
1290 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text), mark);
1291 }
1292 else // single line
1293 {
1294 wxTextEntry::SetInsertionPoint(pos);
1295 }
1296}
1297
1298void wxTextCtrl::SetEditable( bool editable )
1299{
1300 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1301
1302 if ( IsMultiLine() )
1303 {
1304 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1305 }
1306 else // single line
1307 {
1308 wxTextEntry::SetEditable(editable);
1309 }
1310}
1311
1312bool wxTextCtrl::Enable( bool enable )
1313{
1314 if (!wxWindowBase::Enable(enable))
1315 {
1316 // nothing to do
1317 return false;
1318 }
1319
1320 gtk_widget_set_sensitive( m_text, enable );
1321 SetCursor(enable ? wxCursor(wxCURSOR_IBEAM) : wxCursor());
1322
1323 return true;
1324}
1325
1326// wxGTK-specific: called recursively by Enable,
1327// to give widgets an opportunity to correct their colours after they
1328// have been changed by Enable
1329void wxTextCtrl::OnEnabled(bool WXUNUSED(enable))
1330{
1331 // If we have a custom background colour, we use this colour in both
1332 // disabled and enabled mode, or we end up with a different colour under the
1333 // text.
1334 wxColour oldColour = GetBackgroundColour();
1335 if (oldColour.IsOk())
1336 {
1337 // Need to set twice or it'll optimize the useful stuff out
1338 if (oldColour == * wxWHITE)
1339 SetBackgroundColour(*wxBLACK);
1340 else
1341 SetBackgroundColour(*wxWHITE);
1342 SetBackgroundColour(oldColour);
1343 }
1344}
1345
1346void wxTextCtrl::MarkDirty()
1347{
1348 m_modified = true;
1349}
1350
1351void wxTextCtrl::DiscardEdits()
1352{
1353 m_modified = false;
1354}
1355
1356// ----------------------------------------------------------------------------
1357// event handling
1358// ----------------------------------------------------------------------------
1359
1360void wxTextCtrl::EnableTextChangedEvents(bool enable)
1361{
1362 if ( enable )
1363 {
1364 g_signal_handlers_unblock_by_func(GetTextObject(),
1365 (gpointer)gtk_text_changed_callback, this);
1366 }
1367 else // disable events
1368 {
1369 g_signal_handlers_block_by_func(GetTextObject(),
1370 (gpointer)gtk_text_changed_callback, this);
1371 }
1372}
1373
1374bool wxTextCtrl::IgnoreTextUpdate()
1375{
1376 if ( m_countUpdatesToIgnore > 0 )
1377 {
1378 m_countUpdatesToIgnore--;
1379
1380 return true;
1381 }
1382
1383 return false;
1384}
1385
1386bool wxTextCtrl::MarkDirtyOnChange()
1387{
1388 if ( m_dontMarkDirty )
1389 {
1390 m_dontMarkDirty = false;
1391
1392 return false;
1393 }
1394
1395 return true;
1396}
1397
1398void wxTextCtrl::SetSelection( long from, long to )
1399{
1400 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1401
1402 if ( IsMultiLine() )
1403 {
1404 if (from == -1 && to == -1)
1405 {
1406 from = 0;
1407 to = GetValue().length();
1408 }
1409
1410 GtkTextIter fromi, toi;
1411 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1412 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1413
1414 gtk_text_buffer_select_range( m_buffer, &fromi, &toi );
1415 }
1416 else // single line
1417 {
1418 wxTextEntry::SetSelection(from, to);
1419 }
1420}
1421
1422void wxTextCtrl::ShowPosition( long pos )
1423{
1424 if (IsMultiLine())
1425 {
1426 GtkTextIter iter;
1427 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, int(pos));
1428 GtkTextMark* mark = gtk_text_buffer_get_mark(m_buffer, "ShowPosition");
1429 gtk_text_buffer_move_mark(m_buffer, mark, &iter);
1430 if (IsFrozen())
1431 // defer until Thaw, text view is not using m_buffer now
1432 m_showPositionOnThaw = mark;
1433 else
1434 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text), mark);
1435 }
1436}
1437
1438wxTextCtrlHitTestResult
1439wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1440{
1441 if ( !IsMultiLine() )
1442 {
1443 // not supported
1444 return wxTE_HT_UNKNOWN;
1445 }
1446
1447 int x, y;
1448 gtk_text_view_window_to_buffer_coords
1449 (
1450 GTK_TEXT_VIEW(m_text),
1451 GTK_TEXT_WINDOW_TEXT,
1452 pt.x, pt.y,
1453 &x, &y
1454 );
1455
1456 GtkTextIter iter;
1457 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1458 if ( pos )
1459 *pos = gtk_text_iter_get_offset(&iter);
1460
1461 return wxTE_HT_ON_TEXT;
1462}
1463
1464long wxTextCtrl::GetInsertionPoint() const
1465{
1466 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1467
1468 if ( IsMultiLine() )
1469 {
1470 // There is no direct accessor for the cursor, but
1471 // internally, the cursor is the "mark" called
1472 // "insert" in the text view's btree structure.
1473
1474 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1475 GtkTextIter cursor;
1476 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1477
1478 return gtk_text_iter_get_offset( &cursor );
1479 }
1480 else
1481 {
1482 return wxTextEntry::GetInsertionPoint();
1483 }
1484}
1485
1486wxTextPos wxTextCtrl::GetLastPosition() const
1487{
1488 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1489
1490 int pos = 0;
1491
1492 if ( IsMultiLine() )
1493 {
1494 GtkTextIter end;
1495 gtk_text_buffer_get_end_iter( m_buffer, &end );
1496
1497 pos = gtk_text_iter_get_offset( &end );
1498 }
1499 else // single line
1500 {
1501 pos = wxTextEntry::GetLastPosition();
1502 }
1503
1504 return (long)pos;
1505}
1506
1507void wxTextCtrl::Remove( long from, long to )
1508{
1509 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1510
1511 if ( IsMultiLine() )
1512 {
1513 GtkTextIter fromi, toi;
1514 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1515 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1516
1517 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1518 }
1519 else // single line
1520 {
1521 wxTextEntry::Remove(from, to);
1522 }
1523}
1524
1525void wxTextCtrl::Cut()
1526{
1527 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1528
1529 if ( IsMultiLine() )
1530 g_signal_emit_by_name (m_text, "cut-clipboard");
1531 else
1532 wxTextEntry::Cut();
1533}
1534
1535void wxTextCtrl::Copy()
1536{
1537 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1538
1539 if ( IsMultiLine() )
1540 g_signal_emit_by_name (m_text, "copy-clipboard");
1541 else
1542 wxTextEntry::Copy();
1543}
1544
1545void wxTextCtrl::Paste()
1546{
1547 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1548
1549 if ( IsMultiLine() )
1550 g_signal_emit_by_name (m_text, "paste-clipboard");
1551 else
1552 wxTextEntry::Paste();
1553}
1554
1555// If the return values from and to are the same, there is no
1556// selection.
1557void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1558{
1559 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1560
1561 if ( !IsMultiLine() )
1562 {
1563 wxTextEntry::GetSelection(fromOut, toOut);
1564 return;
1565 }
1566
1567 gint from, to;
1568
1569 GtkTextIter ifrom, ito;
1570 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1571 {
1572 from = gtk_text_iter_get_offset(&ifrom);
1573 to = gtk_text_iter_get_offset(&ito);
1574
1575 if ( from > to )
1576 {
1577 // exchange them to be compatible with wxMSW
1578 gint tmp = from;
1579 from = to;
1580 to = tmp;
1581 }
1582 }
1583 else // no selection
1584 {
1585 from =
1586 to = GetInsertionPoint();
1587 }
1588
1589 if ( fromOut )
1590 *fromOut = from;
1591 if ( toOut )
1592 *toOut = to;
1593}
1594
1595
1596bool wxTextCtrl::IsEditable() const
1597{
1598 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1599
1600 if ( IsMultiLine() )
1601 {
1602 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1603 }
1604 else
1605 {
1606 return wxTextEntry::IsEditable();
1607 }
1608}
1609
1610bool wxTextCtrl::IsModified() const
1611{
1612 return m_modified;
1613}
1614
1615void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1616{
1617 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1618
1619 if ( key_event.GetKeyCode() == WXK_RETURN )
1620 {
1621 if ( HasFlag(wxTE_PROCESS_ENTER) )
1622 {
1623 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1624 event.SetEventObject(this);
1625 event.SetString(GetValue());
1626 if ( HandleWindowEvent(event) )
1627 return;
1628 }
1629 }
1630
1631 key_event.Skip();
1632}
1633
1634GtkWidget* wxTextCtrl::GetConnectWidget()
1635{
1636 return GTK_WIDGET(m_text);
1637}
1638
1639GdkWindow *wxTextCtrl::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
1640{
1641 if ( IsMultiLine() )
1642 {
1643 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1644 GTK_TEXT_WINDOW_TEXT );
1645 }
1646 else
1647 {
1648 return gtk_entry_get_text_window(GTK_ENTRY(m_text));
1649 }
1650}
1651
1652// the font will change for subsequent text insertiongs
1653bool wxTextCtrl::SetFont( const wxFont &font )
1654{
1655 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1656
1657 if ( !wxTextCtrlBase::SetFont(font) )
1658 {
1659 // font didn't change, nothing to do
1660 return false;
1661 }
1662
1663 if ( IsMultiLine() )
1664 {
1665 SetUpdateFont(true);
1666
1667 m_defaultStyle.SetFont(font);
1668
1669 ChangeFontGlobally();
1670 }
1671
1672 return true;
1673}
1674
1675void wxTextCtrl::ChangeFontGlobally()
1676{
1677 // this method is very inefficient and hence should be called as rarely as
1678 // possible!
1679 //
1680 // TODO: it can be implemented much more efficiently for GTK2
1681 wxASSERT_MSG( IsMultiLine(),
1682 wxT("shouldn't be called for single line controls") );
1683
1684 wxString value = GetValue();
1685 if ( !value.empty() )
1686 {
1687 SetUpdateFont(false);
1688
1689 Clear();
1690 AppendText(value);
1691 }
1692}
1693
1694bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1695{
1696 if ( !wxControl::SetForegroundColour(colour) )
1697 return false;
1698
1699 // update default fg colour too
1700 m_defaultStyle.SetTextColour(colour);
1701
1702 return true;
1703}
1704
1705bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1706{
1707 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1708
1709 if ( !wxControl::SetBackgroundColour( colour ) )
1710 return false;
1711
1712 if (!m_backgroundColour.IsOk())
1713 return false;
1714
1715 // change active background color too
1716 m_defaultStyle.SetBackgroundColour( colour );
1717
1718 return true;
1719}
1720
1721bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1722{
1723 if ( IsMultiLine() )
1724 {
1725 if ( style.IsDefault() )
1726 {
1727 // nothing to do
1728 return true;
1729 }
1730
1731 gint l = gtk_text_buffer_get_char_count( m_buffer );
1732
1733 wxCHECK_MSG( start >= 0 && end <= l, false,
1734 wxT("invalid range in wxTextCtrl::SetStyle") );
1735
1736 GtkTextIter starti, endi;
1737 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1738 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1739
1740 wxGtkTextApplyTagsFromAttr( m_widget, m_buffer, style, &starti, &endi );
1741
1742 return true;
1743 }
1744 //else: single line text controls don't support styles
1745
1746 return false;
1747}
1748
1749bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
1750{
1751 if ( !IsMultiLine() )
1752 {
1753 // no styles for GtkEntry
1754 return false;
1755 }
1756
1757 gint l = gtk_text_buffer_get_char_count( m_buffer );
1758
1759 wxCHECK_MSG( position >= 0 && position <= l, false,
1760 wxT("invalid range in wxTextCtrl::GetStyle") );
1761
1762 GtkTextIter positioni;
1763 gtk_text_buffer_get_iter_at_offset(m_buffer, &positioni, position);
1764
1765 // Obtain a copy of the default attributes
1766 GtkTextAttributes * const
1767 pattr = gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text));
1768 wxON_BLOCK_EXIT1(gtk_text_attributes_unref, pattr);
1769
1770 // And query GTK for the attributes at the given position using it as base
1771 if ( !gtk_text_iter_get_attributes(&positioni, pattr) )
1772 {
1773 style = m_defaultStyle;
1774 }
1775 else // have custom attributes
1776 {
1777 style.SetBackgroundColour(pattr->appearance.bg_color);
1778 style.SetTextColour(pattr->appearance.fg_color);
1779
1780 const wxGtkString
1781 pangoFontString(pango_font_description_to_string(pattr->font));
1782
1783 wxFont font;
1784 if ( font.SetNativeFontInfo(wxString(pangoFontString)) )
1785 style.SetFont(font);
1786
1787 // TODO: set alignment, tabs and indents
1788 }
1789
1790 return true;
1791}
1792
1793void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1794{
1795 gtk_widget_modify_style(m_text, style);
1796}
1797
1798void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1799{
1800 Cut();
1801}
1802
1803void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1804{
1805 Copy();
1806}
1807
1808void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1809{
1810 Paste();
1811}
1812
1813void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1814{
1815 Undo();
1816}
1817
1818void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1819{
1820 Redo();
1821}
1822
1823void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1824{
1825 event.Enable( CanCut() );
1826}
1827
1828void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1829{
1830 event.Enable( CanCopy() );
1831}
1832
1833void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1834{
1835 event.Enable( CanPaste() );
1836}
1837
1838void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1839{
1840 event.Enable( CanUndo() );
1841}
1842
1843void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1844{
1845 event.Enable( CanRedo() );
1846}
1847
1848wxSize wxTextCtrl::DoGetBestSize() const
1849{
1850 // FIXME should be different for multi-line controls...
1851 wxSize ret( wxControl::DoGetBestSize() );
1852 wxSize best(80, ret.y);
1853 CacheBestSize(best);
1854 return best;
1855}
1856
1857// ----------------------------------------------------------------------------
1858// freeze/thaw
1859// ----------------------------------------------------------------------------
1860
1861void wxTextCtrl::DoFreeze()
1862{
1863 wxCHECK_RET(m_text != NULL, wxT("invalid text ctrl"));
1864
1865 wxWindow::DoFreeze();
1866
1867 if ( HasFlag(wxTE_MULTILINE) )
1868 {
1869 GTKFreezeWidget(m_text);
1870
1871 // removing buffer dramatically speeds up insertion:
1872 g_object_ref(m_buffer);
1873 GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL);
1874 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new);
1875 // gtk_text_view_set_buffer adds its own reference
1876 g_object_unref(buf_new);
1877 // These marks should be deleted when the buffer is changed,
1878 // but they are not (in GTK+ up to at least 3.0.1).
1879 // Otherwise these anonymous marks start to build up in the buffer,
1880 // and Freeze takes longer and longer each time it is called.
1881 if (m_anonymousMarkList)
1882 {
1883 for (GSList* item = m_anonymousMarkList; item; item = item->next)
1884 {
1885 GtkTextMark* mark = static_cast<GtkTextMark*>(item->data);
1886 if (GTK_IS_TEXT_MARK(mark) && !gtk_text_mark_get_deleted(mark))
1887 gtk_text_buffer_delete_mark(m_buffer, mark);
1888 }
1889 g_slist_free(m_anonymousMarkList);
1890 m_anonymousMarkList = NULL;
1891 }
1892 }
1893}
1894
1895void wxTextCtrl::DoThaw()
1896{
1897 if ( HasFlag(wxTE_MULTILINE) )
1898 {
1899 // reattach buffer:
1900 gulong sig_id = g_signal_connect(m_buffer, "mark_set", G_CALLBACK(mark_set), &m_anonymousMarkList);
1901 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1902 g_object_unref(m_buffer);
1903 g_signal_handler_disconnect(m_buffer, sig_id);
1904
1905 if (m_showPositionOnThaw != NULL)
1906 {
1907 gtk_text_view_scroll_mark_onscreen(
1908 GTK_TEXT_VIEW(m_text), m_showPositionOnThaw);
1909 m_showPositionOnThaw = NULL;
1910 }
1911
1912 // and thaw the window
1913 GTKThawWidget(m_text);
1914 }
1915
1916 wxWindow::DoThaw();
1917}
1918
1919// ----------------------------------------------------------------------------
1920// wxTextUrlEvent passing if style & wxTE_AUTO_URL
1921// ----------------------------------------------------------------------------
1922
1923// FIXME: when dragging on a link the sample gets an "Unknown event".
1924// This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1925// a buggy sample, or something else
1926void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
1927{
1928 event.Skip();
1929 if( !HasFlag(wxTE_AUTO_URL) )
1930 return;
1931
1932 gint x, y;
1933 GtkTextIter start, end;
1934 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
1935 "wxUrl");
1936
1937 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
1938 event.GetX(), event.GetY(), &x, &y);
1939
1940 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
1941 if (!gtk_text_iter_has_tag(&end, tag))
1942 {
1943 SetCursor(wxCursor(wxCURSOR_IBEAM));
1944 return;
1945 }
1946
1947 SetCursor(wxCursor(wxCURSOR_HAND));
1948
1949 start = end;
1950 if(!gtk_text_iter_begins_tag(&start, tag))
1951 gtk_text_iter_backward_to_tag_toggle(&start, tag);
1952 if(!gtk_text_iter_ends_tag(&end, tag))
1953 gtk_text_iter_forward_to_tag_toggle(&end, tag);
1954
1955 // Native context menu is probably not desired on an URL.
1956 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1957 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
1958 event.Skip(false);
1959
1960 wxTextUrlEvent url_event(m_windowId, event,
1961 gtk_text_iter_get_offset(&start),
1962 gtk_text_iter_get_offset(&end));
1963
1964 InitCommandEvent(url_event);
1965 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1966 //event.Skip(!HandleWindowEvent(url_event));
1967 HandleWindowEvent(url_event);
1968}
1969
1970bool wxTextCtrl::GTKProcessEvent(wxEvent& event) const
1971{
1972 bool rc = wxTextCtrlBase::GTKProcessEvent(event);
1973
1974 // GtkTextView starts a drag operation when left mouse button is pressed
1975 // and ends it when it is released and if it doesn't get the release event
1976 // the next click on a control results in an assertion failure inside
1977 // gtk_text_view_start_selection_drag() which simply *kills* the program
1978 // without anything we can do about it, so always let GTK+ have this event
1979 return rc && (IsSingleLine() || event.GetEventType() != wxEVT_LEFT_UP);
1980}
1981
1982// static
1983wxVisualAttributes
1984wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1985{
1986 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1987}
1988
1989#endif // wxUSE_TEXTCTRL