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