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