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