GetNumberOfLines() now returns the number of physical, not logical, lines in the...
[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 {
952 GtkTextIter iter;
953 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, 0 );
954
955 // move forward by one display line until the end is reached
956 int lineCount = 1;
957 while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text), &iter) )
958 {
959 lineCount++;
960 }
961
962 // If the last character in the text buffer is a newline,
963 // gtk_text_view_forward_display_line() will return false without that
964 // line being counted. Must add one manually in that case.
965 GtkTextIter lastCharIter;
966 gtk_text_buffer_get_iter_at_offset
967 (
968 m_buffer,
969 &lastCharIter,
970 gtk_text_buffer_get_char_count(m_buffer) - 1
971 );
972 gchar lastChar = gtk_text_iter_get_char( &lastCharIter );
973 if ( lastChar == wxT('\n') )
974 lineCount++;
975
976 return lineCount;
977 }
978 else // single line
979 {
980 return 1;
981 }
982 }
983
984 void wxTextCtrl::SetInsertionPoint( long pos )
985 {
986 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
987
988 if ( IsMultiLine() )
989 {
990 GtkTextIter iter;
991 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
992 gtk_text_buffer_place_cursor( m_buffer, &iter );
993 gtk_text_view_scroll_mark_onscreen
994 (
995 GTK_TEXT_VIEW(m_text),
996 gtk_text_buffer_get_insert( m_buffer )
997 );
998 }
999 else
1000 {
1001 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1002 gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos));
1003 }
1004 }
1005
1006 void wxTextCtrl::SetInsertionPointEnd()
1007 {
1008 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1009
1010 if (m_windowStyle & wxTE_MULTILINE)
1011 {
1012 GtkTextIter end;
1013 gtk_text_buffer_get_end_iter( m_buffer, &end );
1014 gtk_text_buffer_place_cursor( m_buffer, &end );
1015 }
1016 else
1017 {
1018 gtk_editable_set_position( GTK_EDITABLE(m_text), -1 );
1019 }
1020 }
1021
1022 void wxTextCtrl::SetEditable( bool editable )
1023 {
1024 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1025
1026 if (m_windowStyle & wxTE_MULTILINE)
1027 {
1028 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1029 }
1030 else
1031 {
1032 gtk_editable_set_editable( GTK_EDITABLE(m_text), editable );
1033 }
1034 }
1035
1036 bool wxTextCtrl::Enable( bool enable )
1037 {
1038 if (!wxWindowBase::Enable(enable))
1039 {
1040 // nothing to do
1041 return false;
1042 }
1043
1044 if (m_windowStyle & wxTE_MULTILINE)
1045 {
1046 SetEditable( enable );
1047 }
1048 else
1049 {
1050 gtk_widget_set_sensitive( m_text, enable );
1051 }
1052
1053 return true;
1054 }
1055
1056 // wxGTK-specific: called recursively by Enable,
1057 // to give widgets an oppprtunity to correct their colours after they
1058 // have been changed by Enable
1059 void wxTextCtrl::OnParentEnable( bool enable )
1060 {
1061 // If we have a custom background colour, we use this colour in both
1062 // disabled and enabled mode, or we end up with a different colour under the
1063 // text.
1064 wxColour oldColour = GetBackgroundColour();
1065 if (oldColour.Ok())
1066 {
1067 // Need to set twice or it'll optimize the useful stuff out
1068 if (oldColour == * wxWHITE)
1069 SetBackgroundColour(*wxBLACK);
1070 else
1071 SetBackgroundColour(*wxWHITE);
1072 SetBackgroundColour(oldColour);
1073 }
1074 }
1075
1076 void wxTextCtrl::MarkDirty()
1077 {
1078 m_modified = true;
1079 }
1080
1081 void wxTextCtrl::DiscardEdits()
1082 {
1083 m_modified = false;
1084 }
1085
1086 // ----------------------------------------------------------------------------
1087 // max text length support
1088 // ----------------------------------------------------------------------------
1089
1090 void wxTextCtrl::IgnoreNextTextUpdate()
1091 {
1092 m_ignoreNextUpdate = true;
1093 }
1094
1095 bool wxTextCtrl::IgnoreTextUpdate()
1096 {
1097 if ( m_ignoreNextUpdate )
1098 {
1099 m_ignoreNextUpdate = false;
1100
1101 return true;
1102 }
1103
1104 return false;
1105 }
1106
1107 void wxTextCtrl::SetMaxLength(unsigned long len)
1108 {
1109 if ( !HasFlag(wxTE_MULTILINE) )
1110 {
1111 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
1112
1113 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1114 // we had tried to enter more text than allowed by max text length and
1115 // the text wasn't really changed
1116 //
1117 // to detect this and generate TEXT_MAXLEN event instead of
1118 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1119 //
1120 // when max len is set to 0 we disconnect our handler as it means that
1121 // we shouldn't check anything any more
1122 if ( len )
1123 {
1124 g_signal_connect (m_text, "insert_text",
1125 G_CALLBACK (gtk_insert_text_callback), this);
1126 }
1127 else // no checking
1128 {
1129 g_signal_handlers_disconnect_by_func (m_text,
1130 (gpointer) gtk_insert_text_callback, this);
1131 }
1132 }
1133 }
1134
1135 void wxTextCtrl::SetSelection( long from, long to )
1136 {
1137 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1138
1139 if (from == -1 && to == -1)
1140 {
1141 from = 0;
1142 to = GetValue().Length();
1143 }
1144
1145 if (m_windowStyle & wxTE_MULTILINE)
1146 {
1147 GtkTextIter fromi, toi;
1148 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1149 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1150
1151 gtk_text_buffer_place_cursor( m_buffer, &toi );
1152 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
1153 }
1154 else
1155 {
1156 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1157 }
1158 }
1159
1160 void wxTextCtrl::ShowPosition( long pos )
1161 {
1162 if (m_windowStyle & wxTE_MULTILINE)
1163 {
1164 GtkTextIter iter;
1165 gtk_text_buffer_get_start_iter( m_buffer, &iter );
1166 gtk_text_iter_set_offset( &iter, pos );
1167 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
1168 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1169 }
1170 }
1171
1172 wxTextCtrlHitTestResult
1173 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1174 {
1175 if ( !IsMultiLine() )
1176 {
1177 // not supported
1178 return wxTE_HT_UNKNOWN;
1179 }
1180
1181 int x, y;
1182 gtk_text_view_window_to_buffer_coords
1183 (
1184 GTK_TEXT_VIEW(m_text),
1185 GTK_TEXT_WINDOW_TEXT,
1186 pt.x, pt.y,
1187 &x, &y
1188 );
1189
1190 GtkTextIter iter;
1191 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1192 if ( pos )
1193 *pos = gtk_text_iter_get_offset(&iter);
1194
1195 return wxTE_HT_ON_TEXT;
1196 }
1197
1198 long wxTextCtrl::GetInsertionPoint() const
1199 {
1200 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1201
1202 if (m_windowStyle & wxTE_MULTILINE)
1203 {
1204 // There is no direct accessor for the cursor, but
1205 // internally, the cursor is the "mark" called
1206 // "insert" in the text view's btree structure.
1207
1208 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1209 GtkTextIter cursor;
1210 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1211
1212 return gtk_text_iter_get_offset( &cursor );
1213 }
1214 else
1215 {
1216 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text));
1217 }
1218 }
1219
1220 wxTextPos wxTextCtrl::GetLastPosition() const
1221 {
1222 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1223
1224 int pos = 0;
1225
1226 if (m_windowStyle & wxTE_MULTILINE)
1227 {
1228 GtkTextIter end;
1229 gtk_text_buffer_get_end_iter( m_buffer, &end );
1230
1231 pos = gtk_text_iter_get_offset( &end );
1232 }
1233 else
1234 {
1235 pos = GTK_ENTRY(m_text)->text_length;
1236 }
1237
1238 return (long)pos;
1239 }
1240
1241 void wxTextCtrl::Remove( long from, long to )
1242 {
1243 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1244
1245 if (m_windowStyle & wxTE_MULTILINE)
1246 {
1247 GtkTextIter fromi, toi;
1248 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1249 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1250
1251 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1252 }
1253 else // single line
1254 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1255 }
1256
1257 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1258 {
1259 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1260
1261 Remove( from, to );
1262
1263 if (!value.empty())
1264 {
1265 SetInsertionPoint( from );
1266 WriteText( value );
1267 }
1268 }
1269
1270 void wxTextCtrl::Cut()
1271 {
1272 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1273
1274 if (m_windowStyle & wxTE_MULTILINE)
1275 g_signal_emit_by_name (m_text, "cut-clipboard");
1276 else
1277 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text));
1278 }
1279
1280 void wxTextCtrl::Copy()
1281 {
1282 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1283
1284 if (m_windowStyle & wxTE_MULTILINE)
1285 g_signal_emit_by_name (m_text, "copy-clipboard");
1286 else
1287 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text));
1288 }
1289
1290 void wxTextCtrl::Paste()
1291 {
1292 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1293
1294 if (m_windowStyle & wxTE_MULTILINE)
1295 g_signal_emit_by_name (m_text, "paste-clipboard");
1296 else
1297 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text));
1298 }
1299
1300 // Undo/redo
1301 void wxTextCtrl::Undo()
1302 {
1303 // TODO
1304 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1305 }
1306
1307 void wxTextCtrl::Redo()
1308 {
1309 // TODO
1310 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1311 }
1312
1313 bool wxTextCtrl::CanUndo() const
1314 {
1315 // TODO
1316 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1317 return false;
1318 }
1319
1320 bool wxTextCtrl::CanRedo() const
1321 {
1322 // TODO
1323 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1324 return false;
1325 }
1326
1327 // If the return values from and to are the same, there is no
1328 // selection.
1329 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1330 {
1331 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1332
1333 gint from = -1;
1334 gint to = -1;
1335 bool haveSelection = false;
1336
1337 if (m_windowStyle & wxTE_MULTILINE)
1338 {
1339 GtkTextIter ifrom, ito;
1340 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1341 {
1342 haveSelection = true;
1343 from = gtk_text_iter_get_offset(&ifrom);
1344 to = gtk_text_iter_get_offset(&ito);
1345 }
1346 }
1347 else // not multi-line
1348 {
1349 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1350 &from, &to) )
1351 {
1352 haveSelection = true;
1353 }
1354 }
1355
1356 if (! haveSelection )
1357 from = to = GetInsertionPoint();
1358
1359 if ( from > to )
1360 {
1361 // exchange them to be compatible with wxMSW
1362 gint tmp = from;
1363 from = to;
1364 to = tmp;
1365 }
1366
1367 if ( fromOut )
1368 *fromOut = from;
1369 if ( toOut )
1370 *toOut = to;
1371 }
1372
1373
1374 bool wxTextCtrl::IsEditable() const
1375 {
1376 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1377
1378 if (m_windowStyle & wxTE_MULTILINE)
1379 {
1380 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1381 }
1382 else
1383 {
1384 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1385 }
1386 }
1387
1388 bool wxTextCtrl::IsModified() const
1389 {
1390 return m_modified;
1391 }
1392
1393 void wxTextCtrl::Clear()
1394 {
1395 SetValue( wxEmptyString );
1396 }
1397
1398 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1399 {
1400 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1401
1402 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1403 {
1404 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1405 event.SetEventObject(this);
1406 event.SetString(GetValue());
1407 if (GetEventHandler()->ProcessEvent(event)) return;
1408 }
1409
1410 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1411 {
1412 // This will invoke the dialog default action, such
1413 // as the clicking the default button.
1414
1415 wxWindow *top_frame = m_parent;
1416 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1417 top_frame = top_frame->GetParent();
1418
1419 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1420 {
1421 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1422
1423 if (window->default_widget)
1424 {
1425 gtk_widget_activate (window->default_widget);
1426 return;
1427 }
1428 }
1429 }
1430
1431 key_event.Skip();
1432 }
1433
1434 GtkWidget* wxTextCtrl::GetConnectWidget()
1435 {
1436 return GTK_WIDGET(m_text);
1437 }
1438
1439 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1440 {
1441 if (m_windowStyle & wxTE_MULTILINE)
1442 {
1443 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1444 }
1445 else
1446 {
1447 return (window == GTK_ENTRY(m_text)->text_area);
1448 }
1449 }
1450
1451 // the font will change for subsequent text insertiongs
1452 bool wxTextCtrl::SetFont( const wxFont &font )
1453 {
1454 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1455
1456 if ( !wxTextCtrlBase::SetFont(font) )
1457 {
1458 // font didn't change, nothing to do
1459 return false;
1460 }
1461
1462 if ( m_windowStyle & wxTE_MULTILINE )
1463 {
1464 SetUpdateFont(true);
1465
1466 m_defaultStyle.SetFont(font);
1467
1468 ChangeFontGlobally();
1469 }
1470
1471 return true;
1472 }
1473
1474 void wxTextCtrl::ChangeFontGlobally()
1475 {
1476 // this method is very inefficient and hence should be called as rarely as
1477 // possible!
1478 //
1479 // TODO: it can be implemented much more efficiently for GTK2
1480 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1481 _T("shouldn't be called for single line controls") );
1482
1483 wxString value = GetValue();
1484 if ( !value.empty() )
1485 {
1486 SetUpdateFont(false);
1487
1488 Clear();
1489 AppendText(value);
1490 }
1491 }
1492
1493 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1494 {
1495 if ( !wxControl::SetForegroundColour(colour) )
1496 return false;
1497
1498 // update default fg colour too
1499 m_defaultStyle.SetTextColour(colour);
1500
1501 return true;
1502 }
1503
1504 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1505 {
1506 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1507
1508 if ( !wxControl::SetBackgroundColour( colour ) )
1509 return false;
1510
1511 if (!m_backgroundColour.Ok())
1512 return false;
1513
1514 // change active background color too
1515 m_defaultStyle.SetBackgroundColour( colour );
1516
1517 return true;
1518 }
1519
1520 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1521 {
1522 if ( m_windowStyle & wxTE_MULTILINE )
1523 {
1524 if ( style.IsDefault() )
1525 {
1526 // nothing to do
1527 return true;
1528 }
1529
1530 gint l = gtk_text_buffer_get_char_count( m_buffer );
1531
1532 wxCHECK_MSG( start >= 0 && end <= l, false,
1533 _T("invalid range in wxTextCtrl::SetStyle") );
1534
1535 GtkTextIter starti, endi;
1536 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1537 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1538
1539 // use the attributes from style which are set in it and fall back
1540 // first to the default style and then to the text control default
1541 // colours for the others
1542 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1543
1544 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
1545
1546 return true;
1547 }
1548
1549 // else single line
1550 // cannot do this for GTK+'s Entry widget
1551 return false;
1552 }
1553
1554 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1555 {
1556 gtk_widget_modify_style(m_text, style);
1557 }
1558
1559 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1560 {
1561 Cut();
1562 }
1563
1564 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1565 {
1566 Copy();
1567 }
1568
1569 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1570 {
1571 Paste();
1572 }
1573
1574 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1575 {
1576 Undo();
1577 }
1578
1579 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1580 {
1581 Redo();
1582 }
1583
1584 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1585 {
1586 event.Enable( CanCut() );
1587 }
1588
1589 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1590 {
1591 event.Enable( CanCopy() );
1592 }
1593
1594 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1595 {
1596 event.Enable( CanPaste() );
1597 }
1598
1599 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1600 {
1601 event.Enable( CanUndo() );
1602 }
1603
1604 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1605 {
1606 event.Enable( CanRedo() );
1607 }
1608
1609 void wxTextCtrl::OnInternalIdle()
1610 {
1611 wxCursor cursor = m_cursor;
1612 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1613
1614 if (g_delayedFocus == this)
1615 {
1616 if (GTK_WIDGET_REALIZED(m_widget))
1617 {
1618 gtk_widget_grab_focus( m_widget );
1619 g_delayedFocus = NULL;
1620 }
1621 }
1622
1623 if (wxUpdateUIEvent::CanUpdate(this))
1624 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1625 }
1626
1627 wxSize wxTextCtrl::DoGetBestSize() const
1628 {
1629 // FIXME should be different for multi-line controls...
1630 wxSize ret( wxControl::DoGetBestSize() );
1631 wxSize best(80, ret.y);
1632 CacheBestSize(best);
1633 return best;
1634 }
1635
1636 // ----------------------------------------------------------------------------
1637 // freeze/thaw
1638 // ----------------------------------------------------------------------------
1639
1640 void wxTextCtrl::Freeze()
1641 {
1642 if ( HasFlag(wxTE_MULTILINE) )
1643 {
1644 if ( !m_frozenness++ )
1645 {
1646 // freeze textview updates and remove buffer
1647 g_signal_connect (m_text, "expose_event",
1648 G_CALLBACK (gtk_text_exposed_callback), this);
1649 g_signal_connect (m_widget, "expose_event",
1650 G_CALLBACK (gtk_text_exposed_callback), this);
1651 gtk_widget_set_sensitive(m_widget, false);
1652 g_object_ref(m_buffer);
1653 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
1654 }
1655 }
1656 }
1657
1658 void wxTextCtrl::Thaw()
1659 {
1660 if ( HasFlag(wxTE_MULTILINE) )
1661 {
1662 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
1663
1664 if ( !--m_frozenness )
1665 {
1666 // Reattach buffer and thaw textview updates
1667 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1668 g_object_unref(m_buffer);
1669 gtk_widget_set_sensitive(m_widget, true);
1670 g_signal_handlers_disconnect_by_func (m_widget,
1671 (gpointer) gtk_text_exposed_callback, this);
1672 g_signal_handlers_disconnect_by_func (m_text,
1673 (gpointer) gtk_text_exposed_callback, this);
1674 }
1675 }
1676 }
1677
1678 // ----------------------------------------------------------------------------
1679 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1680 // ----------------------------------------------------------------------------
1681
1682 // FIXME: when dragging on a link the sample gets an "Unknown event".
1683 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1684 // a buggy sample, or something else
1685 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
1686 {
1687 event.Skip();
1688 if(!(m_windowStyle & wxTE_AUTO_URL))
1689 return;
1690
1691 gint x, y;
1692 GtkTextIter start, end;
1693 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
1694 "wxUrl");
1695
1696 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
1697 event.GetX(), event.GetY(), &x, &y);
1698
1699 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
1700 if (!gtk_text_iter_has_tag(&end, tag))
1701 {
1702 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1703 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
1704 return;
1705 }
1706
1707 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1708 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
1709
1710 start = end;
1711 if(!gtk_text_iter_begins_tag(&start, tag))
1712 gtk_text_iter_backward_to_tag_toggle(&start, tag);
1713 if(!gtk_text_iter_ends_tag(&end, tag))
1714 gtk_text_iter_forward_to_tag_toggle(&end, tag);
1715
1716 // Native context menu is probably not desired on an URL.
1717 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1718 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
1719 event.Skip(false);
1720
1721 wxTextUrlEvent url_event(m_windowId, event,
1722 gtk_text_iter_get_offset(&start),
1723 gtk_text_iter_get_offset(&end));
1724
1725 InitCommandEvent(url_event);
1726 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1727 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1728 GetEventHandler()->ProcessEvent(url_event);
1729 }
1730
1731 // static
1732 wxVisualAttributes
1733 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1734 {
1735 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1736 }