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