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