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