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