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