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