moved wxapp_install_idle_handler and g_isIdle from many cpp files into gtk/private...
[wxWidgets.git] / src / gtk / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/textctrl.h"
14 #include "wx/utils.h"
15 #include "wx/intl.h"
16 #include "wx/log.h"
17 #include "wx/math.h"
18 #include "wx/settings.h"
19 #include "wx/panel.h"
20 #include "wx/strconv.h"
21 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <ctype.h>
26 #include "wx/math.h"
27
28 #include "wx/gtk/private.h"
29 #include <gdk/gdkkeysyms.h>
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern wxCursor g_globalCursor;
36 extern wxWindowGTK *g_delayedFocus;
37
38 // ----------------------------------------------------------------------------
39 // helpers
40 // ----------------------------------------------------------------------------
41
42 extern "C" {
43 static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
44 GtkTextTag *tag,
45 GtkTextIter *start,
46 GtkTextIter *end,
47 char *prefix)
48 {
49 gchar *name;
50 g_object_get (tag, "name", &name, NULL);
51
52 if (!name || strncmp(name, prefix, strlen(prefix)))
53 // anonymous tag or not starting with prefix - don't remove
54 g_signal_stop_emission_by_name (buffer, "remove_tag");
55
56 g_free(name);
57 }
58 }
59
60 extern "C" {
61 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
62 const wxTextAttr& attr,
63 GtkTextIter *start,
64 GtkTextIter *end)
65 {
66 static gchar buf[1024];
67 GtkTextTag *tag;
68
69 gulong remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
70 G_CALLBACK (wxGtkOnRemoveTag), gpointer("WX"));
71 gtk_text_buffer_remove_all_tags(text_buffer, start, end);
72 g_signal_handler_disconnect (text_buffer, remove_handler_id);
73
74 if (attr.HasFont())
75 {
76 char *font_string;
77 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
78 font_string = pango_font_description_to_string(font_description);
79 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
80 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
81 buf );
82 if (!tag)
83 tag = gtk_text_buffer_create_tag( text_buffer, buf,
84 "font-desc", font_description,
85 NULL );
86 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
87 g_free (font_string);
88 }
89
90 if (attr.HasTextColour())
91 {
92 GdkColor *colFg = attr.GetTextColour().GetColor();
93 g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
94 colFg->red, colFg->green, colFg->blue);
95 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
96 buf );
97 if (!tag)
98 tag = gtk_text_buffer_create_tag( text_buffer, buf,
99 "foreground-gdk", colFg, NULL );
100 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
101 }
102
103 if (attr.HasBackgroundColour())
104 {
105 GdkColor *colBg = attr.GetBackgroundColour().GetColor();
106 g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
107 colBg->red, colBg->green, colBg->blue);
108 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
109 buf );
110 if (!tag)
111 tag = gtk_text_buffer_create_tag( text_buffer, buf,
112 "background-gdk", colBg, NULL );
113 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
114 }
115
116 if (attr.HasAlignment())
117 {
118 GtkTextIter para_start, para_end = *end;
119 gtk_text_buffer_get_iter_at_line( text_buffer,
120 &para_start,
121 gtk_text_iter_get_line(start) );
122 gtk_text_iter_forward_line(&para_end);
123
124 remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
125 G_CALLBACK(wxGtkOnRemoveTag),
126 gpointer("WXALIGNMENT"));
127 gtk_text_buffer_remove_all_tags( text_buffer, &para_start, &para_end );
128 g_signal_handler_disconnect (text_buffer, remove_handler_id);
129
130 GtkJustification align;
131 switch (attr.GetAlignment())
132 {
133 default:
134 align = GTK_JUSTIFY_LEFT;
135 break;
136 case wxTEXT_ALIGNMENT_RIGHT:
137 align = GTK_JUSTIFY_RIGHT;
138 break;
139 case wxTEXT_ALIGNMENT_CENTER:
140 align = GTK_JUSTIFY_CENTER;
141 break;
142 // gtk+ doesn't support justify as of gtk+-2.7.4
143 }
144
145 g_snprintf(buf, sizeof(buf), "WXALIGNMENT %d", align);
146 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
147 buf );
148 if (!tag)
149 tag = gtk_text_buffer_create_tag( text_buffer, buf,
150 "justification", align, NULL );
151 gtk_text_buffer_apply_tag( text_buffer, tag, &para_start, &para_end );
152 }
153 }
154 }
155
156 extern "C" {
157 static void wxGtkTextInsert(GtkWidget *text,
158 GtkTextBuffer *text_buffer,
159 const wxTextAttr& attr,
160 const wxCharBuffer& buffer)
161
162 {
163 gint start_offset;
164 GtkTextIter iter, start;
165
166 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
167 gtk_text_buffer_get_insert (text_buffer) );
168 start_offset = gtk_text_iter_get_offset (&iter);
169 gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) );
170
171 gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
172
173 wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
174 }
175 }
176
177 // ----------------------------------------------------------------------------
178 // "insert_text" for GtkEntry
179 // ----------------------------------------------------------------------------
180
181 extern "C" {
182 static void
183 gtk_insert_text_callback(GtkEditable *editable,
184 const gchar *new_text,
185 gint new_text_length,
186 gint *position,
187 wxTextCtrl *win)
188 {
189 if (g_isIdle)
190 wxapp_install_idle_handler();
191
192 // we should only be called if we have a max len limit at all
193 GtkEntry *entry = GTK_ENTRY (editable);
194
195 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
196
197 // check that we don't overflow the max length limit
198 //
199 // FIXME: this doesn't work when we paste a string which is going to be
200 // truncated
201 if ( entry->text_length == entry->text_max_length )
202 {
203 // we don't need to run the base class version at all
204 g_signal_stop_emission_by_name (editable, "insert_text");
205
206 // remember that the next changed signal is to be ignored to avoid
207 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
208 win->IgnoreNextTextUpdate();
209
210 // and generate the correct one ourselves
211 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
212 event.SetEventObject(win);
213 event.SetString(win->GetValue());
214 win->GetEventHandler()->ProcessEvent( event );
215 }
216 }
217 }
218
219 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
220
221 extern "C" {
222 static void
223 au_apply_tag_callback(GtkTextBuffer *buffer,
224 GtkTextTag *tag,
225 GtkTextIter *start,
226 GtkTextIter *end,
227 gpointer textctrl)
228 {
229 if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"))
230 g_signal_stop_emission_by_name (buffer, "apply_tag");
231 }
232 }
233
234 //-----------------------------------------------------------------------------
235 // GtkTextCharPredicates for gtk_text_iter_*_find_char
236 //-----------------------------------------------------------------------------
237
238 extern "C" {
239 static gboolean
240 pred_whitespace (gunichar ch, gpointer user_data)
241 {
242 return g_unichar_isspace(ch);
243 }
244 }
245
246 extern "C" {
247 static gboolean
248 pred_non_whitespace (gunichar ch, gpointer user_data)
249 {
250 return !g_unichar_isspace(ch);
251 }
252 }
253
254 extern "C" {
255 static gboolean
256 pred_nonpunct (gunichar ch, gpointer user_data)
257 {
258 return !g_unichar_ispunct(ch);
259 }
260 }
261
262 extern "C" {
263 static gboolean
264 pred_nonpunct_or_slash (gunichar ch, gpointer user_data)
265 {
266 return !g_unichar_ispunct(ch) || ch == '/';
267 }
268 }
269
270 //-----------------------------------------------------------------------------
271 // Check for links between s and e and correct tags as necessary
272 //-----------------------------------------------------------------------------
273
274 // This function should be made match better while being efficient at one point.
275 // Most probably with a row of regular expressions.
276 extern "C" {
277 static void
278 au_check_word( GtkTextIter *s, GtkTextIter *e )
279 {
280 static const char *URIPrefixes[] =
281 {
282 "http://",
283 "ftp://",
284 "www.",
285 "ftp.",
286 "mailto://",
287 "https://",
288 "file://",
289 "nntp://",
290 "news://",
291 "telnet://",
292 "mms://",
293 "gopher://",
294 "prospero://",
295 "wais://",
296 };
297
298 GtkTextIter start = *s, end = *e;
299 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
300
301 // Get our special link tag
302 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
303
304 // Get rid of punctuation from beginning and end.
305 // Might want to move this to au_check_range if an improved link checking doesn't
306 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
307 if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) )
308 gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e );
309
310 gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start );
311 gtk_text_iter_forward_char(&end);
312
313 gchar* text = gtk_text_iter_get_text( &start, &end );
314 size_t len = strlen(text), prefix_len;
315 size_t n;
316
317 for( n = 0; n < WXSIZEOF(URIPrefixes); ++n )
318 {
319 prefix_len = strlen(URIPrefixes[n]);
320 if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len))
321 break;
322 }
323
324 if(n < WXSIZEOF(URIPrefixes))
325 {
326 gulong signal_id = g_signal_handler_find (buffer,
327 (GSignalMatchType) (G_SIGNAL_MATCH_FUNC),
328 0, 0, NULL,
329 (gpointer)au_apply_tag_callback, NULL);
330
331 g_signal_handler_block (buffer, signal_id);
332 gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
333 g_signal_handler_unblock (buffer, signal_id);
334 }
335 }
336 }
337
338 extern "C" {
339 static void
340 au_check_range(GtkTextIter *s,
341 GtkTextIter *range_end)
342 {
343 GtkTextIter range_start = *s;
344 GtkTextIter word_end;
345 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
346 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
347
348 gtk_text_buffer_remove_tag(buffer, tag, s, range_end);
349
350 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start)))
351 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
352
353 while(!gtk_text_iter_equal(&range_start, range_end))
354 {
355 word_end = range_start;
356 gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end);
357
358 // Now we should have a word delimited by range_start and word_end, correct link tags
359 au_check_word(&range_start, &word_end);
360
361 range_start = word_end;
362 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
363 }
364 }
365 }
366
367 //-----------------------------------------------------------------------------
368 // "insert-text" for GtkTextBuffer
369 //-----------------------------------------------------------------------------
370
371 extern "C" {
372 static void
373 au_insert_text_callback(GtkTextBuffer *buffer,
374 GtkTextIter *end,
375 gchar *text,
376 gint len,
377 wxTextCtrl *win)
378 {
379 if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
380 return;
381
382 GtkTextIter start = *end;
383 gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len));
384
385 GtkTextIter line_start = start;
386 GtkTextIter line_end = *end;
387 GtkTextIter words_start = start;
388 GtkTextIter words_end = *end;
389
390 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start));
391 gtk_text_iter_forward_to_line_end(&line_end);
392 gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start);
393 gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end);
394
395 au_check_range(&words_start, &words_end);
396 }
397 }
398
399 //-----------------------------------------------------------------------------
400 // "delete-range" for GtkTextBuffer
401 //-----------------------------------------------------------------------------
402
403 extern "C" {
404 static void
405 au_delete_range_callback(GtkTextBuffer *buffer,
406 GtkTextIter *start,
407 GtkTextIter *end,
408 wxTextCtrl *win)
409 {
410 if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
411 return;
412
413 GtkTextIter line_start = *start, line_end = *end;
414
415 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start));
416 gtk_text_iter_forward_to_line_end(&line_end);
417 gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start);
418 gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end);
419
420 au_check_range(start, end);
421 }
422 }
423
424
425 //-----------------------------------------------------------------------------
426 // "changed"
427 //-----------------------------------------------------------------------------
428
429 extern "C" {
430 static void
431 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
432 {
433 if ( win->IgnoreTextUpdate() )
434 return;
435
436 if (!win->m_hasVMT) return;
437
438 if (g_isIdle)
439 wxapp_install_idle_handler();
440
441 win->SetModified();
442
443 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
444 event.SetEventObject( win );
445 win->GetEventHandler()->ProcessEvent( event );
446 }
447 }
448
449 //-----------------------------------------------------------------------------
450 // "expose_event" from scrolled window and textview
451 //-----------------------------------------------------------------------------
452
453 extern "C" {
454 static gboolean
455 gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
456 {
457 return TRUE;
458 }
459 }
460
461
462 //-----------------------------------------------------------------------------
463 // wxTextCtrl
464 //-----------------------------------------------------------------------------
465
466 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
467
468 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
469 EVT_CHAR(wxTextCtrl::OnChar)
470
471 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
472 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
473 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
474 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
475 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
476
477 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
478 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
479 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
480 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
481 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
482
483 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
484 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
485 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent)
486 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent)
487 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent)
488 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent)
489 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent)
490 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent)
491 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent)
492 END_EVENT_TABLE()
493
494 void wxTextCtrl::Init()
495 {
496 m_ignoreNextUpdate =
497 m_modified = false;
498 SetUpdateFont(false);
499 m_text =
500 m_vScrollbar = (GtkWidget *)NULL;
501 m_frozenness = 0;
502 m_gdkHandCursor = NULL;
503 m_gdkXTermCursor = NULL;
504 }
505
506 wxTextCtrl::~wxTextCtrl()
507 {
508 if(m_gdkHandCursor)
509 gdk_cursor_unref(m_gdkHandCursor);
510 if(m_gdkXTermCursor)
511 gdk_cursor_unref(m_gdkXTermCursor);
512 }
513
514 wxTextCtrl::wxTextCtrl( wxWindow *parent,
515 wxWindowID id,
516 const wxString &value,
517 const wxPoint &pos,
518 const wxSize &size,
519 long style,
520 const wxValidator& validator,
521 const wxString &name )
522 {
523 Init();
524
525 Create( parent, id, value, pos, size, style, validator, name );
526 }
527
528 bool wxTextCtrl::Create( wxWindow *parent,
529 wxWindowID id,
530 const wxString &value,
531 const wxPoint &pos,
532 const wxSize &size,
533 long style,
534 const wxValidator& validator,
535 const wxString &name )
536 {
537 m_needParent = true;
538 m_acceptsFocus = true;
539
540 if (!PreCreation( parent, pos, size ) ||
541 !CreateBase( parent, id, pos, size, style, validator, name ))
542 {
543 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
544 return false;
545 }
546
547
548 m_vScrollbarVisible = false;
549
550 bool multi_line = (style & wxTE_MULTILINE) != 0;
551
552 if (multi_line)
553 {
554 // Create view
555 m_text = gtk_text_view_new();
556
557 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
558
559 // create scrolled window
560 m_widget = gtk_scrolled_window_new( NULL, NULL );
561 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
562 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
563
564 // Insert view into scrolled window
565 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
566
567 // translate wx wrapping style to GTK+
568 GtkWrapMode wrap;
569 if ( HasFlag( wxTE_DONTWRAP ) )
570 wrap = GTK_WRAP_NONE;
571 else if ( HasFlag( wxTE_CHARWRAP ) )
572 wrap = GTK_WRAP_CHAR;
573 else if ( HasFlag( wxTE_WORDWRAP ) )
574 wrap = GTK_WRAP_WORD;
575 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
576 {
577 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
578 #ifdef __WXGTK24__
579 if ( !gtk_check_version(2,4,0) )
580 {
581 wrap = GTK_WRAP_WORD_CHAR;
582 }
583 else
584 #endif
585 wrap = GTK_WRAP_WORD;
586 }
587
588 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
589
590 if (!HasFlag(wxNO_BORDER))
591 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
592
593 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
594
595 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
596 }
597 else
598 {
599 // a single-line text control: no need for scrollbars
600 m_widget =
601 m_text = gtk_entry_new();
602
603 if (style & wxNO_BORDER)
604 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
605 }
606
607 m_parent->DoAddChild( this );
608
609 m_focusWidget = m_text;
610
611 PostCreation(size);
612
613 if (multi_line)
614 {
615 gtk_widget_show(m_text);
616 SetVScrollAdjustment(gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)m_widget));
617 }
618
619 if (!value.empty())
620 {
621 SetValue( value );
622 }
623
624 if (style & wxTE_PASSWORD)
625 {
626 if (!multi_line)
627 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
628 }
629
630 if (style & wxTE_READONLY)
631 {
632 if (!multi_line)
633 gtk_editable_set_editable( GTK_EDITABLE(m_text), FALSE );
634 else
635 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
636 }
637
638 if (multi_line)
639 {
640 if (style & wxTE_RIGHT)
641 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
642 else if (style & wxTE_CENTRE)
643 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
644 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
645 }
646 else
647 {
648 #ifdef __WXGTK24__
649 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
650 if (!gtk_check_version(2,4,0))
651 {
652 if (style & wxTE_RIGHT)
653 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
654 else if (style & wxTE_CENTRE)
655 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
656 }
657 #endif
658 }
659
660 // We want to be notified about text changes.
661 if (multi_line)
662 {
663 g_signal_connect (m_buffer, "changed",
664 G_CALLBACK (gtk_text_changed_callback), this);
665
666 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
667 if (style & wxTE_AUTO_URL)
668 {
669 GtkTextIter start, end;
670 m_gdkHandCursor = gdk_cursor_new(GDK_HAND2);
671 m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM);
672
673 // We create our wxUrl tag here for slight efficiency gain - we
674 // don't have to check for the tag existance in callbacks,
675 // hereby it's guaranteed to exist.
676 gtk_text_buffer_create_tag(m_buffer, "wxUrl",
677 "foreground", "blue",
678 "underline", PANGO_UNDERLINE_SINGLE,
679 NULL);
680
681 // Check for URLs after each text change
682 g_signal_connect_after (m_buffer, "insert_text",
683 G_CALLBACK (au_insert_text_callback), this);
684 g_signal_connect_after (m_buffer, "delete_range",
685 G_CALLBACK (au_delete_range_callback), this);
686
687 // Block all wxUrl tag applying unless we do it ourselves, in which case we
688 // block this callback temporarily. This takes care of gtk+ internal
689 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
690 // which is undesired because only a part of the URL might be copied.
691 // The insert-text signal emitted inside it will take care of newly formed
692 // or wholly copied URLs.
693 g_signal_connect (m_buffer, "apply_tag",
694 G_CALLBACK (au_apply_tag_callback), NULL);
695
696 // Check for URLs in the initial string passed to Create
697 gtk_text_buffer_get_start_iter(m_buffer, &start);
698 gtk_text_buffer_get_end_iter(m_buffer, &end);
699 au_check_range(&start, &end);
700 }
701 }
702 else
703 {
704 g_signal_connect (m_text, "changed",
705 G_CALLBACK (gtk_text_changed_callback), this);
706 }
707
708 m_cursor = wxCursor( wxCURSOR_IBEAM );
709
710 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
711 SetDefaultStyle( attrDef );
712
713 return true;
714 }
715
716
717 void wxTextCtrl::CalculateScrollbar()
718 {
719 }
720
721 wxString wxTextCtrl::GetValue() const
722 {
723 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
724
725 wxString tmp;
726 if (m_windowStyle & wxTE_MULTILINE)
727 {
728 GtkTextIter start;
729 gtk_text_buffer_get_start_iter( m_buffer, &start );
730 GtkTextIter end;
731 gtk_text_buffer_get_end_iter( m_buffer, &end );
732 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
733
734 #if wxUSE_UNICODE
735 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
736 #else
737 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
738 #endif
739 if ( buffer )
740 tmp = buffer;
741
742 g_free( text );
743 }
744 else
745 {
746 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
747 }
748
749 return tmp;
750 }
751
752 void wxTextCtrl::SetValue( const wxString &value )
753 {
754 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
755
756 if (m_windowStyle & wxTE_MULTILINE)
757 {
758 #if wxUSE_UNICODE
759 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
760 #else
761 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
762 #endif
763 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
764 IgnoreNextTextUpdate();
765
766 if ( !buffer )
767 {
768 // what else can we do? at least don't crash...
769 return;
770 }
771
772 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
773 }
774 else
775 {
776 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
777 }
778
779 // GRG, Jun/2000: Changed this after a lot of discussion in
780 // the lists. wxWidgets 2.2 will have a set of flags to
781 // customize this behaviour.
782 SetInsertionPoint(0);
783
784 m_modified = false;
785 }
786
787 void wxTextCtrl::WriteText( const wxString &text )
788 {
789 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
790
791 if ( text.empty() )
792 return;
793
794 // gtk_text_changed_callback() will set m_modified to true but m_modified
795 // shouldn't be changed by the program writing to the text control itself,
796 // so save the old value and restore when we're done
797 bool oldModified = m_modified;
798
799 if ( m_windowStyle & wxTE_MULTILINE )
800 {
801 #if wxUSE_UNICODE
802 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
803 #else
804 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
805 #endif
806 if ( !buffer )
807 {
808 // what else can we do? at least don't crash...
809 return;
810 }
811
812 // TODO: Call whatever is needed to delete the selection.
813 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
814
815 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
816 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
817 if ( wxIsSameDouble(adj->value, adj->upper - adj->page_size) )
818 {
819 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
820 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
821 }
822 }
823 else // single line
824 {
825 // First remove the selection if there is one
826 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
827
828 // This moves the cursor pos to behind the inserted text.
829 gint len = gtk_editable_get_position(GTK_EDITABLE(m_text));
830
831 #if wxUSE_UNICODE
832 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
833 #else
834 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
835 #endif
836 if ( !buffer )
837 {
838 // what else can we do? at least don't crash...
839 return;
840 }
841
842 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
843
844 // Bring entry's cursor uptodate.
845 gtk_editable_set_position( GTK_EDITABLE(m_text), len );
846 }
847
848 m_modified = oldModified;
849 }
850
851 void wxTextCtrl::AppendText( const wxString &text )
852 {
853 SetInsertionPointEnd();
854 WriteText( text );
855 }
856
857 wxString wxTextCtrl::GetLineText( long lineNo ) const
858 {
859 if (m_windowStyle & wxTE_MULTILINE)
860 {
861 GtkTextIter line;
862 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
863 GtkTextIter end = line;
864 gtk_text_iter_forward_to_line_end(&end);
865 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
866 wxString result(wxGTK_CONV_BACK(text));
867 g_free(text);
868 return result;
869 }
870 else
871 {
872 if (lineNo == 0) return GetValue();
873 return wxEmptyString;
874 }
875 }
876
877 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
878 {
879 /* If you implement this, don't forget to update the documentation!
880 * (file docs/latex/wx/text.tex) */
881 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
882 }
883
884 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
885 {
886 if ( m_windowStyle & wxTE_MULTILINE )
887 {
888 GtkTextIter iter;
889 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
890 if (gtk_text_iter_is_end(&iter))
891 return false;
892
893 *y = gtk_text_iter_get_line(&iter);
894 *x = gtk_text_iter_get_line_offset(&iter);
895 }
896 else // single line control
897 {
898 if ( pos <= GTK_ENTRY(m_text)->text_length )
899 {
900 *y = 0;
901 *x = pos;
902 }
903 else
904 {
905 // index out of bounds
906 return false;
907 }
908 }
909
910 return true;
911 }
912
913 long wxTextCtrl::XYToPosition(long x, long y ) const
914 {
915 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
916
917 GtkTextIter iter;
918 if (y >= gtk_text_buffer_get_line_count (m_buffer))
919 return -1;
920
921 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
922 if (x >= gtk_text_iter_get_chars_in_line (&iter))
923 return -1;
924
925 return gtk_text_iter_get_offset(&iter) + x;
926 }
927
928 int wxTextCtrl::GetLineLength(long lineNo) const
929 {
930 if (m_windowStyle & wxTE_MULTILINE)
931 {
932 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
933 if (lineNo > last_line)
934 return -1;
935
936 GtkTextIter iter;
937 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
938 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
939 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
940 }
941 else
942 {
943 wxString str = GetLineText (lineNo);
944 return (int) str.Length();
945 }
946 }
947
948 int wxTextCtrl::GetNumberOfLines() const
949 {
950 if (m_windowStyle & wxTE_MULTILINE)
951 return gtk_text_buffer_get_line_count( m_buffer );
952 else
953 return 1;
954 }
955
956 void wxTextCtrl::SetInsertionPoint( long pos )
957 {
958 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
959
960 if ( IsMultiLine() )
961 {
962 GtkTextIter iter;
963 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
964 gtk_text_buffer_place_cursor( m_buffer, &iter );
965 gtk_text_view_scroll_mark_onscreen
966 (
967 GTK_TEXT_VIEW(m_text),
968 gtk_text_buffer_get_insert( m_buffer )
969 );
970 }
971 else
972 {
973 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
974 gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos));
975 }
976 }
977
978 void wxTextCtrl::SetInsertionPointEnd()
979 {
980 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
981
982 if (m_windowStyle & wxTE_MULTILINE)
983 {
984 GtkTextIter end;
985 gtk_text_buffer_get_end_iter( m_buffer, &end );
986 gtk_text_buffer_place_cursor( m_buffer, &end );
987 }
988 else
989 {
990 gtk_editable_set_position( GTK_EDITABLE(m_text), -1 );
991 }
992 }
993
994 void wxTextCtrl::SetEditable( bool editable )
995 {
996 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
997
998 if (m_windowStyle & wxTE_MULTILINE)
999 {
1000 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1001 }
1002 else
1003 {
1004 gtk_editable_set_editable( GTK_EDITABLE(m_text), editable );
1005 }
1006 }
1007
1008 bool wxTextCtrl::Enable( bool enable )
1009 {
1010 if (!wxWindowBase::Enable(enable))
1011 {
1012 // nothing to do
1013 return false;
1014 }
1015
1016 if (m_windowStyle & wxTE_MULTILINE)
1017 {
1018 SetEditable( enable );
1019 }
1020 else
1021 {
1022 gtk_widget_set_sensitive( m_text, enable );
1023 }
1024
1025 return true;
1026 }
1027
1028 // wxGTK-specific: called recursively by Enable,
1029 // to give widgets an oppprtunity to correct their colours after they
1030 // have been changed by Enable
1031 void wxTextCtrl::OnParentEnable( bool enable )
1032 {
1033 // If we have a custom background colour, we use this colour in both
1034 // disabled and enabled mode, or we end up with a different colour under the
1035 // text.
1036 wxColour oldColour = GetBackgroundColour();
1037 if (oldColour.Ok())
1038 {
1039 // Need to set twice or it'll optimize the useful stuff out
1040 if (oldColour == * wxWHITE)
1041 SetBackgroundColour(*wxBLACK);
1042 else
1043 SetBackgroundColour(*wxWHITE);
1044 SetBackgroundColour(oldColour);
1045 }
1046 }
1047
1048 void wxTextCtrl::MarkDirty()
1049 {
1050 m_modified = true;
1051 }
1052
1053 void wxTextCtrl::DiscardEdits()
1054 {
1055 m_modified = false;
1056 }
1057
1058 // ----------------------------------------------------------------------------
1059 // max text length support
1060 // ----------------------------------------------------------------------------
1061
1062 void wxTextCtrl::IgnoreNextTextUpdate()
1063 {
1064 m_ignoreNextUpdate = true;
1065 }
1066
1067 bool wxTextCtrl::IgnoreTextUpdate()
1068 {
1069 if ( m_ignoreNextUpdate )
1070 {
1071 m_ignoreNextUpdate = false;
1072
1073 return true;
1074 }
1075
1076 return false;
1077 }
1078
1079 void wxTextCtrl::SetMaxLength(unsigned long len)
1080 {
1081 if ( !HasFlag(wxTE_MULTILINE) )
1082 {
1083 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
1084
1085 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1086 // we had tried to enter more text than allowed by max text length and
1087 // the text wasn't really changed
1088 //
1089 // to detect this and generate TEXT_MAXLEN event instead of
1090 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1091 //
1092 // when max len is set to 0 we disconnect our handler as it means that
1093 // we shouldn't check anything any more
1094 if ( len )
1095 {
1096 g_signal_connect (m_text, "insert_text",
1097 G_CALLBACK (gtk_insert_text_callback), this);
1098 }
1099 else // no checking
1100 {
1101 g_signal_handlers_disconnect_by_func (m_text,
1102 (gpointer) gtk_insert_text_callback, this);
1103 }
1104 }
1105 }
1106
1107 void wxTextCtrl::SetSelection( long from, long to )
1108 {
1109 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1110
1111 if (from == -1 && to == -1)
1112 {
1113 from = 0;
1114 to = GetValue().Length();
1115 }
1116
1117 if (m_windowStyle & wxTE_MULTILINE)
1118 {
1119 GtkTextIter fromi, toi;
1120 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1121 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1122
1123 gtk_text_buffer_place_cursor( m_buffer, &toi );
1124 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
1125 }
1126 else
1127 {
1128 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1129 }
1130 }
1131
1132 void wxTextCtrl::ShowPosition( long pos )
1133 {
1134 if (m_windowStyle & wxTE_MULTILINE)
1135 {
1136 GtkTextIter iter;
1137 gtk_text_buffer_get_start_iter( m_buffer, &iter );
1138 gtk_text_iter_set_offset( &iter, pos );
1139 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
1140 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1141 }
1142 }
1143
1144 wxTextCtrlHitTestResult
1145 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1146 {
1147 if ( !IsMultiLine() )
1148 {
1149 // not supported
1150 return wxTE_HT_UNKNOWN;
1151 }
1152
1153 int x, y;
1154 gtk_text_view_window_to_buffer_coords
1155 (
1156 GTK_TEXT_VIEW(m_text),
1157 GTK_TEXT_WINDOW_TEXT,
1158 pt.x, pt.y,
1159 &x, &y
1160 );
1161
1162 GtkTextIter iter;
1163 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1164 if ( pos )
1165 *pos = gtk_text_iter_get_offset(&iter);
1166
1167 return wxTE_HT_ON_TEXT;
1168 }
1169
1170 long wxTextCtrl::GetInsertionPoint() const
1171 {
1172 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1173
1174 if (m_windowStyle & wxTE_MULTILINE)
1175 {
1176 // There is no direct accessor for the cursor, but
1177 // internally, the cursor is the "mark" called
1178 // "insert" in the text view's btree structure.
1179
1180 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1181 GtkTextIter cursor;
1182 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1183
1184 return gtk_text_iter_get_offset( &cursor );
1185 }
1186 else
1187 {
1188 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text));
1189 }
1190 }
1191
1192 wxTextPos wxTextCtrl::GetLastPosition() const
1193 {
1194 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1195
1196 int pos = 0;
1197
1198 if (m_windowStyle & wxTE_MULTILINE)
1199 {
1200 GtkTextIter end;
1201 gtk_text_buffer_get_end_iter( m_buffer, &end );
1202
1203 pos = gtk_text_iter_get_offset( &end );
1204 }
1205 else
1206 {
1207 pos = GTK_ENTRY(m_text)->text_length;
1208 }
1209
1210 return (long)pos;
1211 }
1212
1213 void wxTextCtrl::Remove( long from, long to )
1214 {
1215 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1216
1217 if (m_windowStyle & wxTE_MULTILINE)
1218 {
1219 GtkTextIter fromi, toi;
1220 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1221 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1222
1223 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1224 }
1225 else // single line
1226 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1227 }
1228
1229 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1230 {
1231 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1232
1233 Remove( from, to );
1234
1235 if (!value.empty())
1236 {
1237 SetInsertionPoint( from );
1238 WriteText( value );
1239 }
1240 }
1241
1242 void wxTextCtrl::Cut()
1243 {
1244 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1245
1246 if (m_windowStyle & wxTE_MULTILINE)
1247 g_signal_emit_by_name (m_text, "cut-clipboard");
1248 else
1249 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text));
1250 }
1251
1252 void wxTextCtrl::Copy()
1253 {
1254 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1255
1256 if (m_windowStyle & wxTE_MULTILINE)
1257 g_signal_emit_by_name (m_text, "copy-clipboard");
1258 else
1259 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text));
1260 }
1261
1262 void wxTextCtrl::Paste()
1263 {
1264 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1265
1266 if (m_windowStyle & wxTE_MULTILINE)
1267 g_signal_emit_by_name (m_text, "paste-clipboard");
1268 else
1269 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text));
1270 }
1271
1272 // Undo/redo
1273 void wxTextCtrl::Undo()
1274 {
1275 // TODO
1276 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1277 }
1278
1279 void wxTextCtrl::Redo()
1280 {
1281 // TODO
1282 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1283 }
1284
1285 bool wxTextCtrl::CanUndo() const
1286 {
1287 // TODO
1288 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1289 return false;
1290 }
1291
1292 bool wxTextCtrl::CanRedo() const
1293 {
1294 // TODO
1295 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1296 return false;
1297 }
1298
1299 // If the return values from and to are the same, there is no
1300 // selection.
1301 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1302 {
1303 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1304
1305 gint from = -1;
1306 gint to = -1;
1307 bool haveSelection = false;
1308
1309 if (m_windowStyle & wxTE_MULTILINE)
1310 {
1311 GtkTextIter ifrom, ito;
1312 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1313 {
1314 haveSelection = true;
1315 from = gtk_text_iter_get_offset(&ifrom);
1316 to = gtk_text_iter_get_offset(&ito);
1317 }
1318 }
1319 else // not multi-line
1320 {
1321 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1322 &from, &to) )
1323 {
1324 haveSelection = true;
1325 }
1326 }
1327
1328 if (! haveSelection )
1329 from = to = GetInsertionPoint();
1330
1331 if ( from > to )
1332 {
1333 // exchange them to be compatible with wxMSW
1334 gint tmp = from;
1335 from = to;
1336 to = tmp;
1337 }
1338
1339 if ( fromOut )
1340 *fromOut = from;
1341 if ( toOut )
1342 *toOut = to;
1343 }
1344
1345
1346 bool wxTextCtrl::IsEditable() const
1347 {
1348 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1349
1350 if (m_windowStyle & wxTE_MULTILINE)
1351 {
1352 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1353 }
1354 else
1355 {
1356 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1357 }
1358 }
1359
1360 bool wxTextCtrl::IsModified() const
1361 {
1362 return m_modified;
1363 }
1364
1365 void wxTextCtrl::Clear()
1366 {
1367 SetValue( wxEmptyString );
1368 }
1369
1370 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1371 {
1372 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1373
1374 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1375 {
1376 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1377 event.SetEventObject(this);
1378 event.SetString(GetValue());
1379 if (GetEventHandler()->ProcessEvent(event)) return;
1380 }
1381
1382 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1383 {
1384 // This will invoke the dialog default action, such
1385 // as the clicking the default button.
1386
1387 wxWindow *top_frame = m_parent;
1388 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1389 top_frame = top_frame->GetParent();
1390
1391 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1392 {
1393 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1394
1395 if (window->default_widget)
1396 {
1397 gtk_widget_activate (window->default_widget);
1398 return;
1399 }
1400 }
1401 }
1402
1403 key_event.Skip();
1404 }
1405
1406 GtkWidget* wxTextCtrl::GetConnectWidget()
1407 {
1408 return GTK_WIDGET(m_text);
1409 }
1410
1411 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1412 {
1413 if (m_windowStyle & wxTE_MULTILINE)
1414 {
1415 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1416 }
1417 else
1418 {
1419 return (window == GTK_ENTRY(m_text)->text_area);
1420 }
1421 }
1422
1423 // the font will change for subsequent text insertiongs
1424 bool wxTextCtrl::SetFont( const wxFont &font )
1425 {
1426 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1427
1428 if ( !wxTextCtrlBase::SetFont(font) )
1429 {
1430 // font didn't change, nothing to do
1431 return false;
1432 }
1433
1434 if ( m_windowStyle & wxTE_MULTILINE )
1435 {
1436 SetUpdateFont(true);
1437
1438 m_defaultStyle.SetFont(font);
1439
1440 ChangeFontGlobally();
1441 }
1442
1443 return true;
1444 }
1445
1446 void wxTextCtrl::ChangeFontGlobally()
1447 {
1448 // this method is very inefficient and hence should be called as rarely as
1449 // possible!
1450 //
1451 // TODO: it can be implemented much more efficiently for GTK2
1452 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1453 _T("shouldn't be called for single line controls") );
1454
1455 wxString value = GetValue();
1456 if ( !value.empty() )
1457 {
1458 SetUpdateFont(false);
1459
1460 Clear();
1461 AppendText(value);
1462 }
1463 }
1464
1465 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1466 {
1467 if ( !wxControl::SetForegroundColour(colour) )
1468 return false;
1469
1470 // update default fg colour too
1471 m_defaultStyle.SetTextColour(colour);
1472
1473 return true;
1474 }
1475
1476 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1477 {
1478 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1479
1480 if ( !wxControl::SetBackgroundColour( colour ) )
1481 return false;
1482
1483 if (!m_backgroundColour.Ok())
1484 return false;
1485
1486 // change active background color too
1487 m_defaultStyle.SetBackgroundColour( colour );
1488
1489 return true;
1490 }
1491
1492 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1493 {
1494 if ( m_windowStyle & wxTE_MULTILINE )
1495 {
1496 if ( style.IsDefault() )
1497 {
1498 // nothing to do
1499 return true;
1500 }
1501
1502 gint l = gtk_text_buffer_get_char_count( m_buffer );
1503
1504 wxCHECK_MSG( start >= 0 && end <= l, false,
1505 _T("invalid range in wxTextCtrl::SetStyle") );
1506
1507 GtkTextIter starti, endi;
1508 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1509 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1510
1511 // use the attributes from style which are set in it and fall back
1512 // first to the default style and then to the text control default
1513 // colours for the others
1514 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1515
1516 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
1517
1518 return true;
1519 }
1520
1521 // else single line
1522 // cannot do this for GTK+'s Entry widget
1523 return false;
1524 }
1525
1526 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1527 {
1528 gtk_widget_modify_style(m_text, style);
1529 }
1530
1531 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1532 {
1533 Cut();
1534 }
1535
1536 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1537 {
1538 Copy();
1539 }
1540
1541 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1542 {
1543 Paste();
1544 }
1545
1546 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1547 {
1548 Undo();
1549 }
1550
1551 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1552 {
1553 Redo();
1554 }
1555
1556 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1557 {
1558 event.Enable( CanCut() );
1559 }
1560
1561 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1562 {
1563 event.Enable( CanCopy() );
1564 }
1565
1566 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1567 {
1568 event.Enable( CanPaste() );
1569 }
1570
1571 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1572 {
1573 event.Enable( CanUndo() );
1574 }
1575
1576 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1577 {
1578 event.Enable( CanRedo() );
1579 }
1580
1581 void wxTextCtrl::OnInternalIdle()
1582 {
1583 wxCursor cursor = m_cursor;
1584 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1585
1586 if (g_delayedFocus == this)
1587 {
1588 if (GTK_WIDGET_REALIZED(m_widget))
1589 {
1590 gtk_widget_grab_focus( m_widget );
1591 g_delayedFocus = NULL;
1592 }
1593 }
1594
1595 if (wxUpdateUIEvent::CanUpdate(this))
1596 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1597 }
1598
1599 wxSize wxTextCtrl::DoGetBestSize() const
1600 {
1601 // FIXME should be different for multi-line controls...
1602 wxSize ret( wxControl::DoGetBestSize() );
1603 wxSize best(80, ret.y);
1604 CacheBestSize(best);
1605 return best;
1606 }
1607
1608 // ----------------------------------------------------------------------------
1609 // freeze/thaw
1610 // ----------------------------------------------------------------------------
1611
1612 void wxTextCtrl::Freeze()
1613 {
1614 if ( HasFlag(wxTE_MULTILINE) )
1615 {
1616 if ( !m_frozenness++ )
1617 {
1618 // freeze textview updates and remove buffer
1619 g_signal_connect (m_text, "expose_event",
1620 G_CALLBACK (gtk_text_exposed_callback), this);
1621 g_signal_connect (m_widget, "expose_event",
1622 G_CALLBACK (gtk_text_exposed_callback), this);
1623 gtk_widget_set_sensitive(m_widget, false);
1624 g_object_ref(m_buffer);
1625 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
1626 }
1627 }
1628 }
1629
1630 void wxTextCtrl::Thaw()
1631 {
1632 if ( HasFlag(wxTE_MULTILINE) )
1633 {
1634 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
1635
1636 if ( !--m_frozenness )
1637 {
1638 // Reattach buffer and thaw textview updates
1639 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1640 g_object_unref(m_buffer);
1641 gtk_widget_set_sensitive(m_widget, true);
1642 g_signal_handlers_disconnect_by_func (m_widget,
1643 (gpointer) gtk_text_exposed_callback, this);
1644 g_signal_handlers_disconnect_by_func (m_text,
1645 (gpointer) gtk_text_exposed_callback, this);
1646 }
1647 }
1648 }
1649
1650 // ----------------------------------------------------------------------------
1651 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1652 // ----------------------------------------------------------------------------
1653
1654 // FIXME: when dragging on a link the sample gets an "Unknown event".
1655 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1656 // a buggy sample, or something else
1657 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
1658 {
1659 event.Skip();
1660 if(!(m_windowStyle & wxTE_AUTO_URL))
1661 return;
1662
1663 gint x, y;
1664 GtkTextIter start, end;
1665 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
1666 "wxUrl");
1667
1668 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
1669 event.GetX(), event.GetY(), &x, &y);
1670
1671 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
1672 if (!gtk_text_iter_has_tag(&end, tag))
1673 {
1674 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1675 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
1676 return;
1677 }
1678
1679 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1680 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
1681
1682 start = end;
1683 if(!gtk_text_iter_begins_tag(&start, tag))
1684 gtk_text_iter_backward_to_tag_toggle(&start, tag);
1685 if(!gtk_text_iter_ends_tag(&end, tag))
1686 gtk_text_iter_forward_to_tag_toggle(&end, tag);
1687
1688 // Native context menu is probably not desired on an URL.
1689 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1690 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
1691 event.Skip(false);
1692
1693 wxTextUrlEvent url_event(m_windowId, event,
1694 gtk_text_iter_get_offset(&start),
1695 gtk_text_iter_get_offset(&end));
1696
1697 InitCommandEvent(url_event);
1698 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1699 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1700 GetEventHandler()->ProcessEvent(url_event);
1701 }
1702
1703 // static
1704 wxVisualAttributes
1705 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1706 {
1707 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1708 }