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