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