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