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