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