]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
Just correct the path in the header comment.
[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" {
9440c3d0 464static void
e4161a2a 465au_insert_text_callback(GtkTextBuffer * WXUNUSED(buffer),
9440c3d0
KH
466 GtkTextIter *end,
467 gchar *text,
468 gint len,
469 wxTextCtrl *win)
470{
471 if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
472 return;
473
474 GtkTextIter start = *end;
475 gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len));
476
477 GtkTextIter line_start = start;
478 GtkTextIter line_end = *end;
479 GtkTextIter words_start = start;
480 GtkTextIter words_end = *end;
481
482 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start));
483 gtk_text_iter_forward_to_line_end(&line_end);
484 gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start);
485 gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end);
486
487 au_check_range(&words_start, &words_end);
488}
865bb325 489}
9440c3d0
KH
490
491//-----------------------------------------------------------------------------
492// "delete-range" for GtkTextBuffer
493//-----------------------------------------------------------------------------
494
865bb325 495extern "C" {
9440c3d0 496static void
e4161a2a 497au_delete_range_callback(GtkTextBuffer * WXUNUSED(buffer),
9440c3d0
KH
498 GtkTextIter *start,
499 GtkTextIter *end,
500 wxTextCtrl *win)
501{
502 if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
503 return;
504
505 GtkTextIter line_start = *start, line_end = *end;
506
507 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start));
508 gtk_text_iter_forward_to_line_end(&line_end);
509 gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start);
510 gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end);
511
512 au_check_range(start, end);
513}
865bb325 514}
9440c3d0 515
5edfb384
RR
516//-----------------------------------------------------------------------------
517// "populate_popup" from text control and "unmap" from its poup menu
518//-----------------------------------------------------------------------------
519
520extern "C" {
521static void
522gtk_textctrl_popup_unmap( GtkMenu *WXUNUSED(menu), wxTextCtrl* win )
523{
524 win->GTKEnableFocusOutEvent();
525}
526}
527
528extern "C" {
529static void
530gtk_textctrl_populate_popup( GtkEntry *WXUNUSED(entry), GtkMenu *menu, wxTextCtrl *win )
531{
532 win->GTKDisableFocusOutEvent();
533
534 g_signal_connect (menu, "unmap", G_CALLBACK (gtk_textctrl_popup_unmap), win );
535}
536}
9440c3d0 537
c801d85f 538//-----------------------------------------------------------------------------
2f2aa628 539// "changed"
c801d85f
KB
540//-----------------------------------------------------------------------------
541
865bb325 542extern "C" {
805dd538 543static void
5edfb384 544gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 545{
ce2f50e3
VZ
546 if ( win->IgnoreTextUpdate() )
547 return;
548
a2053b27 549 if (!win->m_hasVMT) return;
805dd538 550
6964cbba
VZ
551 if ( win->MarkDirtyOnChange() )
552 win->MarkDirty();
a8bf1826 553
ee2ec18e 554 win->SendTextUpdatedEvent();
6de97a3b 555}
865bb325 556}
112892b9 557
385e8575
PC
558//-----------------------------------------------------------------------------
559// "mark_set"
560//-----------------------------------------------------------------------------
561
562extern "C" {
563static void mark_set(GtkTextBuffer*, GtkTextIter*, GtkTextMark* mark, GSList** markList)
564{
565 if (gtk_text_mark_get_name(mark) == NULL)
566 *markList = g_slist_prepend(*markList, mark);
567}
568}
569
2f2aa628
RR
570//-----------------------------------------------------------------------------
571// wxTextCtrl
572//-----------------------------------------------------------------------------
573
9d112688 574BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
2830bf19 575 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
576
577 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
578 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
579 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
580 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
581 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
582
583 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
584 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
585 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
586 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
587 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
9440c3d0 588
9440c3d0
KH
589 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
590 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
591 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent)
592 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent)
593 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent)
594 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent)
595 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent)
596 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent)
597 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent)
c801d85f
KB
598END_EVENT_TABLE()
599
01041145 600void wxTextCtrl::Init()
f5abe911 601{
6964cbba 602 m_dontMarkDirty =
7d8268a1 603 m_modified = false;
6964cbba 604
f6519b40
VZ
605 m_countUpdatesToIgnore = 0;
606
7d8268a1 607 SetUpdateFont(false);
6964cbba 608
85396430 609 m_text = NULL;
5a3ef194 610 m_showPositionOnThaw = NULL;
385e8575 611 m_anonymousMarkList = NULL;
9440c3d0
KH
612}
613
614wxTextCtrl::~wxTextCtrl()
615{
6be306d4
PC
616 // this is also done by wxWindowGTK dtor, but has to be done here so our
617 // DoThaw() override is called
618 while (IsFrozen())
619 Thaw();
620
385e8575
PC
621 if (m_anonymousMarkList)
622 g_slist_free(m_anonymousMarkList);
f5abe911 623}
13289f04 624
13111b2a
VZ
625wxTextCtrl::wxTextCtrl( wxWindow *parent,
626 wxWindowID id,
627 const wxString &value,
628 const wxPoint &pos,
629 const wxSize &size,
630 long style,
631 const wxValidator& validator,
632 const wxString &name )
f5abe911 633{
01041145
VZ
634 Init();
635
f5abe911
RR
636 Create( parent, id, value, pos, size, style, validator, name );
637}
c801d85f 638
13111b2a
VZ
639bool wxTextCtrl::Create( wxWindow *parent,
640 wxWindowID id,
641 const wxString &value,
642 const wxPoint &pos,
643 const wxSize &size,
644 long style,
645 const wxValidator& validator,
646 const wxString &name )
c801d85f 647{
4dcaf11a
RR
648 if (!PreCreation( parent, pos, size ) ||
649 !CreateBase( parent, id, pos, size, style, validator, name ))
650 {
223d09f6 651 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
7d8268a1 652 return false;
4dcaf11a 653 }
6de97a3b 654
2830bf19 655 bool multi_line = (style & wxTE_MULTILINE) != 0;
a8bf1826 656
ab46dc18 657 if (multi_line)
2830bf19 658 {
385e8575
PC
659 m_buffer = gtk_text_buffer_new(NULL);
660 gulong sig_id = g_signal_connect(m_buffer, "mark_set", G_CALLBACK(mark_set), &m_anonymousMarkList);
fab591c5 661 // Create view
385e8575
PC
662 m_text = gtk_text_view_new_with_buffer(m_buffer);
663 // gtk_text_view_set_buffer adds its own reference
664 g_object_unref(m_buffer);
665 g_signal_handler_disconnect(m_buffer, sig_id);
a8bf1826 666
5a3ef194
PC
667 // create "ShowPosition" marker
668 GtkTextIter iter;
669 gtk_text_buffer_get_start_iter(m_buffer, &iter);
670 gtk_text_buffer_create_mark(m_buffer, "ShowPosition", &iter, true);
671
fab591c5
RR
672 // create scrolled window
673 m_widget = gtk_scrolled_window_new( NULL, NULL );
674 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
09b86e69
VZ
675 GTK_POLICY_AUTOMATIC,
676 style & wxTE_NO_VSCROLL
677 ? GTK_POLICY_NEVER
678 : GTK_POLICY_AUTOMATIC );
add7cadd 679 // for ScrollLines/Pages
385e8575 680 m_scrollBar[1] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(m_widget)));
a8bf1826 681
fab591c5
RR
682 // Insert view into scrolled window
683 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
a8bf1826 684
927637fd 685 GTKSetWrapMode();
805dd538 686
496e7ec6 687 GTKScrolledWindowSetBorder(m_widget, style);
7d8268a1 688
e327fddf
KH
689 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
690
fc9ab22a 691 gtk_widget_set_can_focus(m_widget, FALSE);
2830bf19
RR
692 }
693 else
694 {
fab591c5 695 // a single-line text control: no need for scrollbars
2830bf19 696 m_widget =
1c35b54e 697 m_text = gtk_entry_new();
69cecdb1
PC
698 // work around probable bug in GTK+ 2.18 when calling WriteText on a
699 // new, empty control, see http://trac.wxwidgets.org/ticket/11409
700 gtk_entry_get_text((GtkEntry*)m_text);
44c5573d 701
02a6e358 702 if (style & wxNO_BORDER)
7d1fea10 703 g_object_set (m_text, "has-frame", FALSE, NULL);
e2ccb250 704
2830bf19 705 }
9ff9d30c 706 g_object_ref(m_widget);
484e45bf 707
db434467 708 m_parent->DoAddChild( this );
eda40bfc 709
76fcf0f2 710 m_focusWidget = m_text;
db434467 711
abdeb9e7 712 PostCreation(size);
484e45bf 713
2830bf19 714 if (multi_line)
0c131a5a 715 {
2830bf19 716 gtk_widget_show(m_text);
0c131a5a 717 }
13289f04 718
4a3f5ad0
RR
719 // We want to be notified about text changes.
720 if (multi_line)
721 {
722 g_signal_connect (m_buffer, "changed",
723 G_CALLBACK (gtk_text_changed_callback), this);
724 }
725 else
726 {
727 g_signal_connect (m_text, "changed",
728 G_CALLBACK (gtk_text_changed_callback), this);
729 }
730
5edfb384
RR
731 // Catch to disable focus out handling
732 g_signal_connect (m_text, "populate_popup",
733 G_CALLBACK (gtk_textctrl_populate_popup),
734 this);
735
7d8268a1 736 if (!value.empty())
2830bf19 737 {
fab591c5 738 SetValue( value );
2830bf19 739 }
484e45bf 740
2830bf19 741 if (style & wxTE_PASSWORD)
927637fd 742 GTKSetVisibility();
8bbe427f 743
2830bf19 744 if (style & wxTE_READONLY)
927637fd 745 GTKSetEditable();
98a8daf4 746
927637fd
VZ
747 // left justification (alignment) is the default anyhow
748 if ( style & (wxTE_RIGHT | wxTE_CENTRE) )
749 GTKSetJustification();
7d8268a1 750
fab591c5
RR
751 if (multi_line)
752 {
4a3f5ad0 753 // Handle URLs on multi-line controls with wxTE_AUTO_URL style
9440c3d0
KH
754 if (style & wxTE_AUTO_URL)
755 {
756 GtkTextIter start, end;
9440c3d0
KH
757
758 // We create our wxUrl tag here for slight efficiency gain - we
eccace04 759 // don't have to check for the tag existence in callbacks,
9440c3d0
KH
760 // hereby it's guaranteed to exist.
761 gtk_text_buffer_create_tag(m_buffer, "wxUrl",
762 "foreground", "blue",
763 "underline", PANGO_UNDERLINE_SINGLE,
764 NULL);
765
766 // Check for URLs after each text change
9fa72bd2
MR
767 g_signal_connect_after (m_buffer, "insert_text",
768 G_CALLBACK (au_insert_text_callback), this);
769 g_signal_connect_after (m_buffer, "delete_range",
770 G_CALLBACK (au_delete_range_callback), this);
9440c3d0
KH
771
772 // Block all wxUrl tag applying unless we do it ourselves, in which case we
773 // block this callback temporarily. This takes care of gtk+ internal
774 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
775 // which is undesired because only a part of the URL might be copied.
776 // The insert-text signal emitted inside it will take care of newly formed
777 // or wholly copied URLs.
9fa72bd2
MR
778 g_signal_connect (m_buffer, "apply_tag",
779 G_CALLBACK (au_apply_tag_callback), NULL);
9440c3d0
KH
780
781 // Check for URLs in the initial string passed to Create
782 gtk_text_buffer_get_start_iter(m_buffer, &start);
783 gtk_text_buffer_get_end_iter(m_buffer, &end);
784 au_check_range(&start, &end);
785 }
fab591c5 786 }
3cfe87da
VZ
787 else // single line
788 {
789 // do the right thing with Enter presses depending on whether we have
790 // wxTE_PROCESS_ENTER or not
791 GTKSetActivatesDefault();
792 }
793
0ec1179b 794
10434560 795 GTKConnectClipboardSignals(m_text);
c85f2eb1 796
65045edd 797 m_cursor = wxCursor( wxCURSOR_IBEAM );
13111b2a 798
7d8268a1 799 return true;
2830bf19 800}
484e45bf 801
0ec1179b
VZ
802GtkEditable *wxTextCtrl::GetEditable() const
803{
804 wxCHECK_MSG( IsSingleLine(), NULL, "shouldn't be called for multiline" );
805
806 return GTK_EDITABLE(m_text);
807}
808
0847e36e
JS
809GtkEntry *wxTextCtrl::GetEntry() const
810{
811 return GTK_ENTRY(m_text);
812}
813
927637fd
VZ
814// ----------------------------------------------------------------------------
815// flags handling
816// ----------------------------------------------------------------------------
817
818void wxTextCtrl::GTKSetEditable()
819{
820 gboolean editable = !HasFlag(wxTE_READONLY);
821 if ( IsSingleLine() )
822 gtk_editable_set_editable(GTK_EDITABLE(m_text), editable);
823 else
824 gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text), editable);
825}
826
827void wxTextCtrl::GTKSetVisibility()
828{
ff805cac
VZ
829 wxCHECK_RET( IsSingleLine(),
830 "wxTE_PASSWORD is for single line text controls only" );
831
832 gtk_entry_set_visibility(GTK_ENTRY(m_text), !HasFlag(wxTE_PASSWORD));
833}
834
835void wxTextCtrl::GTKSetActivatesDefault()
836{
837 wxCHECK_RET( IsSingleLine(),
838 "wxTE_PROCESS_ENTER is for single line text controls only" );
839
840 gtk_entry_set_activates_default(GTK_ENTRY(m_text),
841 !HasFlag(wxTE_PROCESS_ENTER));
927637fd
VZ
842}
843
844void wxTextCtrl::GTKSetWrapMode()
845{
846 // no wrapping in single line controls
847 if ( !IsMultiLine() )
848 return;
849
850 // translate wx wrapping style to GTK+
851 GtkWrapMode wrap;
852 if ( HasFlag( wxTE_DONTWRAP ) )
853 wrap = GTK_WRAP_NONE;
854 else if ( HasFlag( wxTE_CHARWRAP ) )
855 wrap = GTK_WRAP_CHAR;
856 else if ( HasFlag( wxTE_WORDWRAP ) )
857 wrap = GTK_WRAP_WORD;
858 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
ff654490 859 wrap = GTK_WRAP_WORD_CHAR;
927637fd
VZ
860
861 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
862}
863
864void wxTextCtrl::GTKSetJustification()
865{
866 if ( IsMultiLine() )
867 {
868 GtkJustification just;
869 if ( HasFlag(wxTE_RIGHT) )
870 just = GTK_JUSTIFY_RIGHT;
871 else if ( HasFlag(wxTE_CENTRE) )
872 just = GTK_JUSTIFY_CENTER;
873 else // wxTE_LEFT == 0
874 just = GTK_JUSTIFY_LEFT;
875
41e2aad5 876 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text), just);
927637fd
VZ
877 }
878 else // single line
879 {
ff654490
VZ
880 gfloat align;
881 if ( HasFlag(wxTE_RIGHT) )
882 align = 1.0;
883 else if ( HasFlag(wxTE_CENTRE) )
884 align = 0.5;
885 else // single line
886 align = 0.0;
927637fd 887
ff654490
VZ
888 gtk_entry_set_alignment(GTK_ENTRY(m_text), align);
889 }
927637fd
VZ
890}
891
892void wxTextCtrl::SetWindowStyleFlag(long style)
893{
894 long styleOld = GetWindowStyleFlag();
895
896 wxTextCtrlBase::SetWindowStyleFlag(style);
897
898 if ( (style & wxTE_READONLY) != (styleOld & wxTE_READONLY) )
899 GTKSetEditable();
900
901 if ( (style & wxTE_PASSWORD) != (styleOld & wxTE_PASSWORD) )
902 GTKSetVisibility();
903
ff805cac
VZ
904 if ( (style & wxTE_PROCESS_ENTER) != (styleOld & wxTE_PROCESS_ENTER) )
905 GTKSetActivatesDefault();
906
927637fd
VZ
907 static const long flagsWrap = wxTE_WORDWRAP | wxTE_CHARWRAP | wxTE_DONTWRAP;
908 if ( (style & flagsWrap) != (styleOld & flagsWrap) )
909 GTKSetWrapMode();
910
911 static const long flagsAlign = wxTE_LEFT | wxTE_CENTRE | wxTE_RIGHT;
912 if ( (style & flagsAlign) != (styleOld & flagsAlign) )
913 GTKSetJustification();
914}
915
916// ----------------------------------------------------------------------------
917// control value
918// ----------------------------------------------------------------------------
919
03f38c58 920wxString wxTextCtrl::GetValue() const
c801d85f 921{
902725ee 922 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
8bbe427f 923
75812722 924 if ( IsMultiLine() )
2830bf19 925 {
fab591c5 926 GtkTextIter start;
41b81aed 927 gtk_text_buffer_get_start_iter( m_buffer, &start );
fab591c5 928 GtkTextIter end;
41b81aed 929 gtk_text_buffer_get_end_iter( m_buffer, &end );
e808cf8a 930 wxGtkString text(gtk_text_buffer_get_text(m_buffer, &start, &end, true));
5b87f8bf 931
6256849f 932 return wxGTK_CONV_BACK(text);
2830bf19 933 }
0ec1179b 934 else // single line
2830bf19 935 {
0ec1179b 936 return wxTextEntry::GetValue();
2830bf19 937 }
6de97a3b 938}
c801d85f 939
0d91b234
VZ
940wxFontEncoding wxTextCtrl::GetTextEncoding() const
941{
942 // GTK+ uses UTF-8 internally, we need to convert to it but from which
943 // encoding?
944
945 // first check the default text style (we intentionally don't check the
946 // style for the current position as it doesn't make sense for SetValue())
947 const wxTextAttr& style = GetDefaultStyle();
44cc96a8 948 wxFontEncoding enc = style.HasFontEncoding() ? style.GetFontEncoding()
0d91b234
VZ
949 : wxFONTENCODING_SYSTEM;
950
951 // fall back to the controls font if no style
952 if ( enc == wxFONTENCODING_SYSTEM && m_hasFont )
953 enc = GetFont().GetEncoding();
954
955 return enc;
956}
957
28fdd8db
VZ
958bool wxTextCtrl::IsEmpty() const
959{
960 if ( IsMultiLine() )
14be4734 961 return gtk_text_buffer_get_char_count(m_buffer) == 0;
28fdd8db 962
0ec1179b 963 return wxTextEntry::IsEmpty();
28fdd8db
VZ
964}
965
f6519b40 966void wxTextCtrl::DoSetValue( const wxString &value, int flags )
c801d85f 967{
223d09f6 968 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 969
6964cbba 970 m_modified = false;
6964cbba 971
0ec1179b
VZ
972 if ( !IsMultiLine() )
973 {
974 wxTextEntry::DoSetValue(value, flags);
975 return;
976 }
977
d1263a39
RR
978 if (value.IsEmpty())
979 {
980 if ( !(flags & SetValue_SendEvent) )
981 EnableTextChangedEvents(false);
e2ccb250 982
d1263a39 983 gtk_text_buffer_set_text( m_buffer, "", 0 );
e2ccb250 984
d1263a39
RR
985 if ( !(flags & SetValue_SendEvent) )
986 EnableTextChangedEvents(true);
e2ccb250 987
d1263a39
RR
988 return;
989 }
990
e2ccb250 991#if wxUSE_UNICODE
d5d20078
RR
992 const wxCharBuffer buffer(value.utf8_str());
993#else
785d8330
VZ
994 wxFontEncoding enc = m_defaultStyle.HasFont()
995 ? m_defaultStyle.GetFont().GetEncoding()
996 : wxFONTENCODING_SYSTEM;
997 if ( enc == wxFONTENCODING_SYSTEM )
998 enc = GetTextEncoding();
999
1000 const wxCharBuffer buffer(wxGTK_CONV_ENC(value, enc));
7e0fff42 1001 if ( !buffer )
2830bf19 1002 {
7e0fff42
VZ
1003 // see comment in WriteText() as to why we must warn the user about
1004 // this
1005 wxLogWarning(_("Failed to set text in the text control."));
1006 return;
1007 }
d5d20078 1008#endif
418cf02e 1009
86e37f69
VZ
1010 if ( !(flags & SetValue_SendEvent) )
1011 {
0ec1179b 1012 EnableTextChangedEvents(false);
86e37f69
VZ
1013 }
1014
0ec1179b
VZ
1015 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
1016
1017 if ( !m_defaultStyle.IsDefault() )
2830bf19 1018 {
0ec1179b
VZ
1019 GtkTextIter start, end;
1020 gtk_text_buffer_get_bounds( m_buffer, &start, &end );
1021 wxGtkTextApplyTagsFromAttr(m_widget, m_buffer, m_defaultStyle,
1022 &start, &end);
2830bf19 1023 }
86e37f69
VZ
1024
1025 if ( !(flags & SetValue_SendEvent) )
1026 {
0ec1179b 1027 EnableTextChangedEvents(true);
86e37f69 1028 }
6de97a3b 1029}
c801d85f
KB
1030
1031void wxTextCtrl::WriteText( const wxString &text )
1032{
223d09f6 1033 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1034
0ec1179b
VZ
1035 // we're changing the text programmatically
1036 DontMarkDirtyOnNextChange();
1037
1038 if ( !IsMultiLine() )
1039 {
1040 wxTextEntry::WriteText(text);
17665a2b 1041 return;
0ec1179b 1042 }
484e45bf 1043
e2ccb250 1044#if wxUSE_UNICODE
d5d20078
RR
1045 const wxCharBuffer buffer(text.utf8_str());
1046#else
0d91b234
VZ
1047 // check if we have a specific style for the current position
1048 wxFontEncoding enc = wxFONTENCODING_SYSTEM;
1049 wxTextAttr style;
44cc96a8 1050 if ( GetStyle(GetInsertionPoint(), style) && style.HasFontEncoding() )
0d91b234 1051 {
44cc96a8 1052 enc = style.GetFontEncoding();
0d91b234
VZ
1053 }
1054
1055 if ( enc == wxFONTENCODING_SYSTEM )
1056 enc = GetTextEncoding();
1057
1058 const wxCharBuffer buffer(wxGTK_CONV_ENC(text, enc));
a3669332
VZ
1059 if ( !buffer )
1060 {
0d91b234
VZ
1061 // we must log an error here as losing the text like this can be a
1062 // serious problem (e.g. imagine the document edited by user being
1063 // empty instead of containing the correct text)
1064 wxLogWarning(_("Failed to insert text in the control."));
a3669332
VZ
1065 return;
1066 }
d5d20078 1067#endif
a3669332 1068
0ec1179b
VZ
1069 // First remove the selection if there is one
1070 // TODO: Is there an easier GTK specific way to do this?
1071 long from, to;
1072 GetSelection(&from, &to);
1073 if (from != to)
1074 Remove(from, to);
ea5449ae 1075
0ec1179b
VZ
1076 // Insert the text
1077 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
a8bf1826 1078
0ec1179b
VZ
1079 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1080 // won't work when frozen, text view is not using m_buffer then
385e8575 1081 if (!IsFrozen())
2830bf19 1082 {
385e8575
PC
1083 GtkAdjustment* adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
1084 const double value = gtk_adjustment_get_value(adj);
1085 const double upper = gtk_adjustment_get_upper(adj);
1086 const double page_size = gtk_adjustment_get_page_size(adj);
1087 if (wxIsSameDouble(value, upper - page_size))
1088 {
1089 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(m_text),
1090 gtk_text_buffer_get_insert(m_buffer), 0, false, 0, 1);
1091 }
2830bf19 1092 }
6de97a3b 1093}
c801d85f 1094
ba3f6b44
RR
1095wxString wxTextCtrl::GetLineText( long lineNo ) const
1096{
e808cf8a 1097 wxString result;
75812722 1098 if ( IsMultiLine() )
a6e21573 1099 {
905f2110 1100 GtkTextIter line;
41b81aed 1101 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
5440a04f 1102
8c6785f0 1103 GtkTextIter end = line;
5440a04f
VZ
1104 // avoid skipping to the next line end if this one is empty
1105 if ( !gtk_text_iter_ends_line(&line) )
1106 gtk_text_iter_forward_to_line_end(&end);
1107
e808cf8a
PC
1108 wxGtkString text(gtk_text_buffer_get_text(m_buffer, &line, &end, true));
1109 result = wxGTK_CONV_BACK(text);
a6e21573 1110 }
ba3f6b44 1111 else
a81258be 1112 {
e808cf8a
PC
1113 if (lineNo == 0)
1114 result = GetValue();
a81258be 1115 }
e808cf8a 1116 return result;
6de97a3b 1117}
c801d85f 1118
a81258be
RR
1119void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
1120{
ac0d36b5
HH
1121 /* If you implement this, don't forget to update the documentation!
1122 * (file docs/latex/wx/text.tex) */
223d09f6 1123 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 1124}
112892b9 1125
0efe5ba7 1126bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 1127{
75812722 1128 if ( IsMultiLine() )
805dd538 1129 {
f29a481a 1130 GtkTextIter iter;
01207937
MR
1131
1132 if (pos > GetLastPosition())
1133 return false;
1134
f29a481a 1135 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
f29a481a 1136
50259962
MR
1137 if ( y )
1138 *y = gtk_text_iter_get_line(&iter);
1139 if ( x )
1140 *x = gtk_text_iter_get_line_offset(&iter);
805dd538 1141 }
96385642
VZ
1142 else // single line control
1143 {
385e8575 1144 if (pos <= gtk_entry_get_text_length(GTK_ENTRY(m_text)))
96385642 1145 {
50259962
MR
1146 if ( y )
1147 *y = 0;
1148 if ( x )
1149 *x = pos;
96385642
VZ
1150 }
1151 else
1152 {
1153 // index out of bounds
7d8268a1 1154 return false;
96385642 1155 }
8bbe427f 1156 }
96385642 1157
7d8268a1 1158 return true;
6de97a3b 1159}
c801d85f 1160
e3ca08dd 1161long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 1162{
75812722
VZ
1163 if ( IsSingleLine() )
1164 return 0;
805dd538 1165
f29a481a 1166 GtkTextIter iter;
21d23b88
MR
1167 if (y >= gtk_text_buffer_get_line_count (m_buffer))
1168 return -1;
1169
1170 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
1171 if (x >= gtk_text_iter_get_chars_in_line (&iter))
1172 return -1;
1173
1174 return gtk_text_iter_get_offset(&iter) + x;
6de97a3b 1175}
c801d85f 1176
a81258be 1177int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 1178{
75812722 1179 if ( IsMultiLine() )
f29a481a
MR
1180 {
1181 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
1182 if (lineNo > last_line)
1183 return -1;
1184
1185 GtkTextIter iter;
1186 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
1187 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1188 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
1189 }
1190 else
f29a481a
MR
1191 {
1192 wxString str = GetLineText (lineNo);
8e13c1ec 1193 return (int) str.length();
f29a481a 1194 }
6de97a3b 1195}
c801d85f 1196
6ce83213
VZ
1197wxPoint wxTextCtrl::DoPositionToCoords(long pos) const
1198{
1199 if ( !IsMultiLine() )
1200 {
1201 // Single line text entry (GtkTextEntry) doesn't have support for
1202 // getting the coordinates for the given offset. Perhaps we could
1203 // find them ourselves by using GetTextExtent() but for now just leave
1204 // it unimplemented, this function is more useful for multiline
1205 // controls anyhow.
1206 return wxDefaultPosition;
1207 }
1208
1209 // Window coordinates for the given position is calculated by getting
1210 // the buffer coordinates and converting them to window coordinates.
1211 GtkTextView *textview = GTK_TEXT_VIEW(m_text);
1212
1213 GtkTextIter iter;
1214 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
1215
1216 GdkRectangle bufferCoords;
1217 gtk_text_view_get_iter_location(textview, &iter, &bufferCoords);
1218
1219 gint winCoordX = 0,
1220 winCoordY = 0;
1221 gtk_text_view_buffer_to_window_coords(textview, GTK_TEXT_WINDOW_WIDGET,
1222 bufferCoords.x, bufferCoords.y,
1223 &winCoordX, &winCoordY);
1224
1225 return wxPoint(winCoordX, winCoordY);
1226}
1227
a81258be 1228int wxTextCtrl::GetNumberOfLines() const
c801d85f 1229{
75812722 1230 if ( IsMultiLine() )
e894be20 1231 {
c68dea66 1232 return gtk_text_buffer_get_line_count( m_buffer );
e894be20
VZ
1233 }
1234 else // single line
1235 {
96385642 1236 return 1;
e894be20 1237 }
6de97a3b 1238}
c801d85f 1239
debe6624 1240void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 1241{
223d09f6 1242 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e 1243
74c5a810 1244 if ( IsMultiLine() )
291a8f20 1245 {
fab591c5 1246 GtkTextIter iter;
41b81aed
RR
1247 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1248 gtk_text_buffer_place_cursor( m_buffer, &iter );
5a3ef194
PC
1249 GtkTextMark* mark = gtk_text_buffer_get_insert(m_buffer);
1250 if (IsFrozen())
1251 // defer until Thaw, text view is not using m_buffer now
1252 m_showPositionOnThaw = mark;
1253 else
1254 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text), mark);
ac0d36b5 1255 }
0ec1179b 1256 else // single line
291a8f20 1257 {
0ec1179b 1258 wxTextEntry::SetInsertionPoint(pos);
291a8f20 1259 }
6de97a3b 1260}
c801d85f 1261
debe6624 1262void wxTextCtrl::SetEditable( bool editable )
c801d85f 1263{
223d09f6 1264 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1265
75812722 1266 if ( IsMultiLine() )
fab591c5 1267 {
fab591c5 1268 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
fab591c5 1269 }
0ec1179b 1270 else // single line
fab591c5 1271 {
0ec1179b 1272 wxTextEntry::SetEditable(editable);
fab591c5 1273 }
6de97a3b 1274}
c801d85f 1275
68df5777
RR
1276bool wxTextCtrl::Enable( bool enable )
1277{
1278 if (!wxWindowBase::Enable(enable))
1279 {
1280 // nothing to do
7d8268a1 1281 return false;
68df5777 1282 }
f6bcfd97 1283
81c882b6 1284 gtk_widget_set_sensitive( m_text, enable );
8caabedb 1285 SetCursor(enable ? wxCursor(wxCURSOR_IBEAM) : wxCursor());
68df5777 1286
7d8268a1 1287 return true;
68df5777
RR
1288}
1289
fdca68a6 1290// wxGTK-specific: called recursively by Enable,
47a8a4d5 1291// to give widgets an opportunity to correct their colours after they
fdca68a6 1292// have been changed by Enable
e4161a2a 1293void wxTextCtrl::OnEnabled(bool WXUNUSED(enable))
fdca68a6
JS
1294{
1295 // If we have a custom background colour, we use this colour in both
1296 // disabled and enabled mode, or we end up with a different colour under the
1297 // text.
1298 wxColour oldColour = GetBackgroundColour();
a1b806b9 1299 if (oldColour.IsOk())
fdca68a6
JS
1300 {
1301 // Need to set twice or it'll optimize the useful stuff out
1302 if (oldColour == * wxWHITE)
1303 SetBackgroundColour(*wxBLACK);
1304 else
1305 SetBackgroundColour(*wxWHITE);
1306 SetBackgroundColour(oldColour);
1307 }
1308}
1309
3a9fa0d6
VZ
1310void wxTextCtrl::MarkDirty()
1311{
7d8268a1 1312 m_modified = true;
3a9fa0d6
VZ
1313}
1314
0efe5ba7
VZ
1315void wxTextCtrl::DiscardEdits()
1316{
7d8268a1 1317 m_modified = false;
0efe5ba7
VZ
1318}
1319
ce2f50e3 1320// ----------------------------------------------------------------------------
0ec1179b 1321// event handling
ce2f50e3
VZ
1322// ----------------------------------------------------------------------------
1323
0ec1179b
VZ
1324void wxTextCtrl::EnableTextChangedEvents(bool enable)
1325{
1326 if ( enable )
1327 {
1328 g_signal_handlers_unblock_by_func(GetTextObject(),
1329 (gpointer)gtk_text_changed_callback, this);
1330 }
1331 else // disable events
1332 {
1333 g_signal_handlers_block_by_func(GetTextObject(),
1334 (gpointer)gtk_text_changed_callback, this);
1335 }
1336}
1337
ce2f50e3
VZ
1338bool wxTextCtrl::IgnoreTextUpdate()
1339{
f6519b40 1340 if ( m_countUpdatesToIgnore > 0 )
ce2f50e3 1341 {
f6519b40 1342 m_countUpdatesToIgnore--;
ce2f50e3 1343
7d8268a1 1344 return true;
ce2f50e3
VZ
1345 }
1346
7d8268a1 1347 return false;
ce2f50e3
VZ
1348}
1349
6964cbba
VZ
1350bool wxTextCtrl::MarkDirtyOnChange()
1351{
1352 if ( m_dontMarkDirty )
1353 {
1354 m_dontMarkDirty = false;
1355
1356 return false;
1357 }
1358
1359 return true;
1360}
1361
debe6624 1362void wxTextCtrl::SetSelection( long from, long to )
c801d85f 1363{
223d09f6 1364 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1365
75812722 1366 if ( IsMultiLine() )
fab591c5 1367 {
6eb60628
VZ
1368 if (from == -1 && to == -1)
1369 {
1370 from = 0;
1371 to = GetValue().length();
1372 }
1373
739e366a 1374 GtkTextIter fromi, toi;
41b81aed
RR
1375 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1376 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
739e366a 1377
183b82cc 1378 gtk_text_buffer_select_range( m_buffer, &fromi, &toi );
fab591c5 1379 }
0ec1179b 1380 else // single line
fab591c5 1381 {
0ec1179b 1382 wxTextEntry::SetSelection(from, to);
fab591c5 1383 }
6de97a3b 1384}
c801d85f 1385
afbe906a 1386void wxTextCtrl::ShowPosition( long pos )
c801d85f 1387{
5a3ef194 1388 if (IsMultiLine())
afbe906a 1389 {
71aba833 1390 GtkTextIter iter;
5a3ef194
PC
1391 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, int(pos));
1392 GtkTextMark* mark = gtk_text_buffer_get_mark(m_buffer, "ShowPosition");
1393 gtk_text_buffer_move_mark(m_buffer, mark, &iter);
1394 if (IsFrozen())
1395 // defer until Thaw, text view is not using m_buffer now
1396 m_showPositionOnThaw = mark;
1397 else
1398 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text), mark);
afbe906a 1399 }
6de97a3b 1400}
c801d85f 1401
692c9b86
VZ
1402wxTextCtrlHitTestResult
1403wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1404{
1405 if ( !IsMultiLine() )
1406 {
1407 // not supported
1408 return wxTE_HT_UNKNOWN;
1409 }
1410
1411 int x, y;
1412 gtk_text_view_window_to_buffer_coords
1413 (
1414 GTK_TEXT_VIEW(m_text),
1415 GTK_TEXT_WINDOW_TEXT,
1416 pt.x, pt.y,
1417 &x, &y
1418 );
1419
1420 GtkTextIter iter;
1421 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1422 if ( pos )
1423 *pos = gtk_text_iter_get_offset(&iter);
1424
1425 return wxTE_HT_ON_TEXT;
1426}
1427
03f38c58 1428long wxTextCtrl::GetInsertionPoint() const
c801d85f 1429{
223d09f6 1430 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1431
75812722 1432 if ( IsMultiLine() )
fab591c5 1433 {
fab591c5
RR
1434 // There is no direct accessor for the cursor, but
1435 // internally, the cursor is the "mark" called
1436 // "insert" in the text view's btree structure.
a8bf1826 1437
41b81aed 1438 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
fab591c5 1439 GtkTextIter cursor;
41b81aed 1440 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
a8bf1826 1441
fab591c5
RR
1442 return gtk_text_iter_get_offset( &cursor );
1443 }
1444 else
fab591c5 1445 {
0ec1179b 1446 return wxTextEntry::GetInsertionPoint();
fab591c5 1447 }
6de97a3b 1448}
c801d85f 1449
7d8268a1 1450wxTextPos wxTextCtrl::GetLastPosition() const
c801d85f 1451{
223d09f6 1452 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1453
2830bf19 1454 int pos = 0;
a8bf1826 1455
75812722 1456 if ( IsMultiLine() )
fab591c5 1457 {
fab591c5 1458 GtkTextIter end;
41b81aed 1459 gtk_text_buffer_get_end_iter( m_buffer, &end );
a8bf1826 1460
fab591c5 1461 pos = gtk_text_iter_get_offset( &end );
fab591c5 1462 }
0ec1179b 1463 else // single line
fab591c5 1464 {
0ec1179b 1465 pos = wxTextEntry::GetLastPosition();
fab591c5 1466 }
805dd538 1467
ac0d36b5 1468 return (long)pos;
6de97a3b 1469}
c801d85f 1470
debe6624 1471void wxTextCtrl::Remove( long from, long to )
c801d85f 1472{
223d09f6 1473 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1474
75812722 1475 if ( IsMultiLine() )
581ee8a9 1476 {
581ee8a9 1477 GtkTextIter fromi, toi;
41b81aed
RR
1478 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1479 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
581ee8a9 1480
41b81aed 1481 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
581ee8a9
VZ
1482 }
1483 else // single line
2df7be7f 1484 {
0ec1179b 1485 wxTextEntry::Remove(from, to);
2df7be7f 1486 }
6de97a3b 1487}
c801d85f 1488
03f38c58 1489void wxTextCtrl::Cut()
c801d85f 1490{
223d09f6 1491 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1492
75812722 1493 if ( IsMultiLine() )
9fa72bd2 1494 g_signal_emit_by_name (m_text, "cut-clipboard");
bbde2e29 1495 else
0ec1179b 1496 wxTextEntry::Cut();
6de97a3b 1497}
c801d85f 1498
03f38c58 1499void wxTextCtrl::Copy()
c801d85f 1500{
223d09f6 1501 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1502
75812722 1503 if ( IsMultiLine() )
9fa72bd2 1504 g_signal_emit_by_name (m_text, "copy-clipboard");
bbde2e29 1505 else
0ec1179b 1506 wxTextEntry::Copy();
6de97a3b 1507}
c801d85f 1508
03f38c58 1509void wxTextCtrl::Paste()
c801d85f 1510{
223d09f6 1511 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1512
75812722 1513 if ( IsMultiLine() )
9fa72bd2 1514 g_signal_emit_by_name (m_text, "paste-clipboard");
bbde2e29 1515 else
0ec1179b 1516 wxTextEntry::Paste();
ca8b28f2
JS
1517}
1518
1519// If the return values from and to are the same, there is no
1520// selection.
2d4cc5b6 1521void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 1522{
223d09f6 1523 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 1524
0ec1179b
VZ
1525 if ( !IsMultiLine() )
1526 {
1527 wxTextEntry::GetSelection(fromOut, toOut);
1528 return;
1529 }
1530
1531 gint from, to;
1532
1533 GtkTextIter ifrom, ito;
1534 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1535 {
1536 from = gtk_text_iter_get_offset(&ifrom);
1537 to = gtk_text_iter_get_offset(&ito);
1538
1539 if ( from > to )
1540 {
1541 // exchange them to be compatible with wxMSW
1542 gint tmp = from;
1543 from = to;
1544 to = tmp;
1545 }
1546 }
1547 else // no selection
1548 {
1549 from =
1550 to = GetInsertionPoint();
1551 }
bb69661b 1552
2d4cc5b6
VZ
1553 if ( fromOut )
1554 *fromOut = from;
1555 if ( toOut )
1556 *toOut = to;
ca8b28f2
JS
1557}
1558
5b87f8bf 1559
ca8b28f2
JS
1560bool wxTextCtrl::IsEditable() const
1561{
7d8268a1 1562 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
05060eeb 1563
75812722 1564 if ( IsMultiLine() )
fdd55287 1565 {
d5027818 1566 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)) != 0;
fdd55287
VZ
1567 }
1568 else
1569 {
0ec1179b 1570 return wxTextEntry::IsEditable();
fdd55287 1571 }
ca8b28f2
JS
1572}
1573
0efe5ba7
VZ
1574bool wxTextCtrl::IsModified() const
1575{
1576 return m_modified;
1577}
1578
903f689b 1579void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 1580{
223d09f6 1581 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 1582
75812722 1583 if ( key_event.GetKeyCode() == WXK_RETURN )
da048e3d 1584 {
75812722
VZ
1585 if ( HasFlag(wxTE_PROCESS_ENTER) )
1586 {
1587 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1588 event.SetEventObject(this);
1589 event.SetString(GetValue());
937013e0 1590 if ( HandleWindowEvent(event) )
75812722
VZ
1591 return;
1592 }
da048e3d
RR
1593 }
1594
2830bf19 1595 key_event.Skip();
6de97a3b 1596}
c801d85f 1597
03f38c58 1598GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1599{
ae0bdb01 1600 return GTK_WIDGET(m_text);
6de97a3b 1601}
e3e65dac 1602
ef5c70f9 1603GdkWindow *wxTextCtrl::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
903f689b 1604{
75812722 1605 if ( IsMultiLine() )
fab591c5 1606 {
ef5c70f9
VZ
1607 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1608 GTK_TEXT_WINDOW_TEXT );
fab591c5 1609 }
ae0bdb01 1610 else
fab591c5 1611 {
9dc44eff
PC
1612#ifdef __WXGTK3__
1613 // no access to internal GdkWindows
1614 return NULL;
1615#else
385e8575 1616 return gtk_entry_get_text_window(GTK_ENTRY(m_text));
9dc44eff 1617#endif
fab591c5 1618 }
903f689b 1619}
e3e65dac 1620
bb69661b
VZ
1621// the font will change for subsequent text insertiongs
1622bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1623{
7d8268a1 1624 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
8bbe427f 1625
a66954a6 1626 if ( !wxTextCtrlBase::SetFont(font) )
bb69661b
VZ
1627 {
1628 // font didn't change, nothing to do
7d8268a1 1629 return false;
bb69661b
VZ
1630 }
1631
75812722 1632 if ( IsMultiLine() )
bb69661b 1633 {
7d8268a1 1634 SetUpdateFont(true);
bb69661b 1635
1ff4714d
VZ
1636 m_defaultStyle.SetFont(font);
1637
01041145 1638 ChangeFontGlobally();
bb69661b
VZ
1639 }
1640
7d8268a1 1641 return true;
58614078
RR
1642}
1643
01041145
VZ
1644void wxTextCtrl::ChangeFontGlobally()
1645{
1646 // this method is very inefficient and hence should be called as rarely as
1647 // possible!
c04ec496
VZ
1648 //
1649 // TODO: it can be implemented much more efficiently for GTK2
75812722 1650 wxASSERT_MSG( IsMultiLine(),
9a83f860 1651 wxT("shouldn't be called for single line controls") );
01041145
VZ
1652
1653 wxString value = GetValue();
7d8268a1 1654 if ( !value.empty() )
01041145 1655 {
7d8268a1 1656 SetUpdateFont(false);
572aeb77 1657
01041145
VZ
1658 Clear();
1659 AppendText(value);
01041145
VZ
1660 }
1661}
1662
17665a2b
VZ
1663bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1664{
1665 if ( !wxControl::SetForegroundColour(colour) )
7d8268a1 1666 return false;
17665a2b
VZ
1667
1668 // update default fg colour too
1669 m_defaultStyle.SetTextColour(colour);
1670
7d8268a1 1671 return true;
17665a2b
VZ
1672}
1673
f03fc89f 1674bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1675{
7d8268a1 1676 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
a81258be 1677
1f477433 1678 if ( !wxControl::SetBackgroundColour( colour ) )
7d8268a1 1679 return false;
3358d36e 1680
a1b806b9 1681 if (!m_backgroundColour.IsOk())
7d8268a1 1682 return false;
8bbe427f 1683
17665a2b
VZ
1684 // change active background color too
1685 m_defaultStyle.SetBackgroundColour( colour );
1686
7d8268a1 1687 return true;
58614078
RR
1688}
1689
eda40bfc 1690bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
17665a2b 1691{
75812722 1692 if ( IsMultiLine() )
17665a2b
VZ
1693 {
1694 if ( style.IsDefault() )
1695 {
1696 // nothing to do
7d8268a1 1697 return true;
17665a2b 1698 }
902725ee 1699
41b81aed 1700 gint l = gtk_text_buffer_get_char_count( m_buffer );
17665a2b 1701
7d8268a1 1702 wxCHECK_MSG( start >= 0 && end <= l, false,
9a83f860 1703 wxT("invalid range in wxTextCtrl::SetStyle") );
cc3da3f8
RR
1704
1705 GtkTextIter starti, endi;
41b81aed
RR
1706 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1707 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
cc3da3f8 1708
7b377ed9 1709 wxGtkTextApplyTagsFromAttr( m_widget, m_buffer, style, &starti, &endi );
902725ee 1710
7d8268a1 1711 return true;
17665a2b 1712 }
7b377ed9 1713 //else: single line text controls don't support styles
902725ee 1714
902725ee 1715 return false;
17665a2b
VZ
1716}
1717
e9ee2270
VZ
1718bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
1719{
1720 if ( !IsMultiLine() )
1721 {
1722 // no styles for GtkEntry
1723 return false;
1724 }
1725
1726 gint l = gtk_text_buffer_get_char_count( m_buffer );
1727
1728 wxCHECK_MSG( position >= 0 && position <= l, false,
490e22fd 1729 wxT("invalid range in wxTextCtrl::GetStyle") );
e9ee2270
VZ
1730
1731 GtkTextIter positioni;
1732 gtk_text_buffer_get_iter_at_offset(m_buffer, &positioni, position);
1733
1734 // Obtain a copy of the default attributes
1735 GtkTextAttributes * const
1736 pattr = gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(m_text));
990cf3b9 1737 wxON_BLOCK_EXIT1(gtk_text_attributes_unref, pattr);
e9ee2270
VZ
1738
1739 // And query GTK for the attributes at the given position using it as base
1740 if ( !gtk_text_iter_get_attributes(&positioni, pattr) )
1741 {
1742 style = m_defaultStyle;
1743 }
1744 else // have custom attributes
1745 {
1746 style.SetBackgroundColour(pattr->appearance.bg_color);
1747 style.SetTextColour(pattr->appearance.fg_color);
1748
2205ca74
VZ
1749 const wxGtkString
1750 pangoFontString(pango_font_description_to_string(pattr->font));
1751
1752 wxFont font;
1753 if ( font.SetNativeFontInfo(wxString(pangoFontString)) )
1754 style.SetFont(font);
1755
1756 // TODO: set alignment, tabs and indents
e9ee2270
VZ
1757 }
1758
1759 return true;
1760}
1761
f40fdaa3 1762void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
58614078 1763{
9dc44eff 1764 GTKApplyStyle(m_text, style);
68dda785 1765}
f96aa4d9 1766
e702ff0f
JS
1767void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1768{
1769 Cut();
1770}
1771
1772void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1773{
1774 Copy();
1775}
1776
1777void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1778{
1779 Paste();
1780}
1781
1782void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1783{
1784 Undo();
1785}
1786
1787void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1788{
1789 Redo();
1790}
1791
1792void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1793{
1794 event.Enable( CanCut() );
1795}
1796
1797void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1798{
1799 event.Enable( CanCopy() );
1800}
1801
1802void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1803{
1804 event.Enable( CanPaste() );
1805}
1806
1807void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1808{
1809 event.Enable( CanUndo() );
1810}
1811
1812void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1813{
1814 event.Enable( CanRedo() );
1815}
65045edd 1816
f68586e5
VZ
1817wxSize wxTextCtrl::DoGetBestSize() const
1818{
1819 // FIXME should be different for multi-line controls...
0279e844 1820 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
1821 wxSize best(80, ret.y);
1822 CacheBestSize(best);
1823 return best;
f68586e5 1824}
0cc7251e 1825
9cd6d737
VZ
1826// ----------------------------------------------------------------------------
1827// freeze/thaw
1828// ----------------------------------------------------------------------------
1829
17808a75 1830void wxTextCtrl::DoFreeze()
0cc7251e 1831{
442a960c
PC
1832 wxCHECK_RET(m_text != NULL, wxT("invalid text ctrl"));
1833
603e7f6d 1834 GTKFreezeWidget(m_text);
5f346ddc 1835
0cc7251e
VZ
1836 if ( HasFlag(wxTE_MULTILINE) )
1837 {
5f346ddc 1838 // removing buffer dramatically speeds up insertion:
17808a75
VZ
1839 g_object_ref(m_buffer);
1840 GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL);
17808a75
VZ
1841 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new);
1842 // gtk_text_view_set_buffer adds its own reference
1843 g_object_unref(buf_new);
385e8575
PC
1844 // These marks should be deleted when the buffer is changed,
1845 // but they are not (in GTK+ up to at least 3.0.1).
17808a75
VZ
1846 // Otherwise these anonymous marks start to build up in the buffer,
1847 // and Freeze takes longer and longer each time it is called.
385e8575
PC
1848 if (m_anonymousMarkList)
1849 {
1850 for (GSList* item = m_anonymousMarkList; item; item = item->next)
1851 {
1852 GtkTextMark* mark = static_cast<GtkTextMark*>(item->data);
1853 if (GTK_IS_TEXT_MARK(mark) && !gtk_text_mark_get_deleted(mark))
1854 gtk_text_buffer_delete_mark(m_buffer, mark);
1855 }
1856 g_slist_free(m_anonymousMarkList);
1857 m_anonymousMarkList = NULL;
1858 }
17808a75
VZ
1859 }
1860}
1861
1862void wxTextCtrl::DoThaw()
0cc7251e
VZ
1863{
1864 if ( HasFlag(wxTE_MULTILINE) )
1865 {
5f346ddc 1866 // reattach buffer:
385e8575 1867 gulong sig_id = g_signal_connect(m_buffer, "mark_set", G_CALLBACK(mark_set), &m_anonymousMarkList);
17808a75
VZ
1868 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1869 g_object_unref(m_buffer);
385e8575 1870 g_signal_handler_disconnect(m_buffer, sig_id);
5f346ddc 1871
17808a75 1872 if (m_showPositionOnThaw != NULL)
41b81aed 1873 {
17808a75
VZ
1874 gtk_text_view_scroll_mark_onscreen(
1875 GTK_TEXT_VIEW(m_text), m_showPositionOnThaw);
1876 m_showPositionOnThaw = NULL;
41b81aed 1877 }
41b81aed 1878 }
5f346ddc 1879
603e7f6d 1880 GTKThawWidget(m_text);
0cc7251e 1881}
9cd6d737 1882
9440c3d0
KH
1883// ----------------------------------------------------------------------------
1884// wxTextUrlEvent passing if style & wxTE_AUTO_URL
1885// ----------------------------------------------------------------------------
1886
9440c3d0
KH
1887// FIXME: when dragging on a link the sample gets an "Unknown event".
1888// This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1889// a buggy sample, or something else
1890void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
1891{
1892 event.Skip();
75812722 1893 if( !HasFlag(wxTE_AUTO_URL) )
9440c3d0
KH
1894 return;
1895
1896 gint x, y;
1897 GtkTextIter start, end;
1898 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
1899 "wxUrl");
1900
1901 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
1902 event.GetX(), event.GetY(), &x, &y);
1903
1904 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
1905 if (!gtk_text_iter_has_tag(&end, tag))
1906 {
c2246a38 1907 SetCursor(wxCursor(wxCURSOR_IBEAM));
9440c3d0
KH
1908 return;
1909 }
1910
c2246a38 1911 SetCursor(wxCursor(wxCURSOR_HAND));
9440c3d0
KH
1912
1913 start = end;
1914 if(!gtk_text_iter_begins_tag(&start, tag))
1915 gtk_text_iter_backward_to_tag_toggle(&start, tag);
1916 if(!gtk_text_iter_ends_tag(&end, tag))
1917 gtk_text_iter_forward_to_tag_toggle(&end, tag);
1918
1919 // Native context menu is probably not desired on an URL.
eccace04 1920 // Consider making this dependent on ProcessEvent(wxTextUrlEvent) return value
9440c3d0
KH
1921 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
1922 event.Skip(false);
1923
1924 wxTextUrlEvent url_event(m_windowId, event,
1925 gtk_text_iter_get_offset(&start),
1926 gtk_text_iter_get_offset(&end));
1927
1928 InitCommandEvent(url_event);
1929 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
937013e0
VS
1930 //event.Skip(!HandleWindowEvent(url_event));
1931 HandleWindowEvent(url_event);
9440c3d0 1932}
9440c3d0 1933
8312c461
VZ
1934bool wxTextCtrl::GTKProcessEvent(wxEvent& event) const
1935{
1936 bool rc = wxTextCtrlBase::GTKProcessEvent(event);
1937
1938 // GtkTextView starts a drag operation when left mouse button is pressed
1939 // and ends it when it is released and if it doesn't get the release event
1940 // the next click on a control results in an assertion failure inside
1941 // gtk_text_view_start_selection_drag() which simply *kills* the program
1942 // without anything we can do about it, so always let GTK+ have this event
1943 return rc && (IsSingleLine() || event.GetEventType() != wxEVT_LEFT_UP);
1944}
1945
9d522606
RD
1946// static
1947wxVisualAttributes
1948wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1949{
1950 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1951}
28fcfbfe
VZ
1952
1953#endif // wxUSE_TEXTCTRL