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