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