]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textctrl.cpp
added support for <bg> tag for toolbars in XRC
[wxWidgets.git] / src / gtk / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/utils.h"
15 #include "wx/intl.h"
16 #include "wx/log.h"
17 #include "wx/math.h"
18 #include "wx/settings.h"
19 #include "wx/panel.h"
20 #include "wx/strconv.h"
21 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <ctype.h>
26 #include "wx/math.h"
27
28 #include "wx/gtk/private.h"
29 #include <gdk/gdkkeysyms.h>
30
31 //-----------------------------------------------------------------------------
32 // idle system
33 //-----------------------------------------------------------------------------
34
35 extern void wxapp_install_idle_handler();
36 extern bool g_isIdle;
37
38 //-----------------------------------------------------------------------------
39 // data
40 //-----------------------------------------------------------------------------
41
42 extern wxCursor g_globalCursor;
43 extern wxWindowGTK *g_delayedFocus;
44
45 // ----------------------------------------------------------------------------
46 // helpers
47 // ----------------------------------------------------------------------------
48
49 extern "C" {
50 static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
51 GtkTextTag *tag,
52 GtkTextIter *start,
53 GtkTextIter *end,
54 char *prefix)
55 {
56 gchar *name;
57 g_object_get (tag, "name", &name, NULL);
58
59 if (!name || strncmp(name, prefix, strlen(prefix)))
60 // anonymous tag or not starting with prefix - don't remove
61 g_signal_stop_emission_by_name (buffer, "remove_tag");
62
63 g_free(name);
64 }
65 }
66
67 extern "C" {
68 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
69 const wxTextAttr& attr,
70 GtkTextIter *start,
71 GtkTextIter *end)
72 {
73 static gchar buf[1024];
74 GtkTextTag *tag;
75
76 gulong remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
77 G_CALLBACK (wxGtkOnRemoveTag), gpointer("WX"));
78 gtk_text_buffer_remove_all_tags(text_buffer, start, end);
79 g_signal_handler_disconnect (text_buffer, remove_handler_id);
80
81 if (attr.HasFont())
82 {
83 char *font_string;
84 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
85 font_string = pango_font_description_to_string(font_description);
86 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
87 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
88 buf );
89 if (!tag)
90 tag = gtk_text_buffer_create_tag( text_buffer, buf,
91 "font-desc", font_description,
92 NULL );
93 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
94 g_free (font_string);
95 }
96
97 if (attr.HasTextColour())
98 {
99 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 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 win->SetModified();
449
450 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
451 event.SetEventObject( win );
452 win->GetEventHandler()->ProcessEvent( event );
453 }
454 }
455
456 //-----------------------------------------------------------------------------
457 // "expose_event" from scrolled window and textview
458 //-----------------------------------------------------------------------------
459
460 extern "C" {
461 static gboolean
462 gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
463 {
464 return TRUE;
465 }
466 }
467
468
469 //-----------------------------------------------------------------------------
470 // wxTextCtrl
471 //-----------------------------------------------------------------------------
472
473 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
474
475 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
476 EVT_CHAR(wxTextCtrl::OnChar)
477
478 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
479 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
480 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
481 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
482 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
483
484 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
485 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
486 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
487 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
488 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
489
490 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
491 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
492 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent)
493 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent)
494 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent)
495 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent)
496 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent)
497 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent)
498 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent)
499 END_EVENT_TABLE()
500
501 void wxTextCtrl::Init()
502 {
503 m_ignoreNextUpdate =
504 m_modified = false;
505 SetUpdateFont(false);
506 m_text =
507 m_vScrollbar = (GtkWidget *)NULL;
508 m_frozenness = 0;
509 m_gdkHandCursor = NULL;
510 m_gdkXTermCursor = NULL;
511 }
512
513 wxTextCtrl::~wxTextCtrl()
514 {
515 if(m_gdkHandCursor)
516 gdk_cursor_unref(m_gdkHandCursor);
517 if(m_gdkXTermCursor)
518 gdk_cursor_unref(m_gdkXTermCursor);
519 }
520
521 wxTextCtrl::wxTextCtrl( wxWindow *parent,
522 wxWindowID id,
523 const wxString &value,
524 const wxPoint &pos,
525 const wxSize &size,
526 long style,
527 const wxValidator& validator,
528 const wxString &name )
529 {
530 Init();
531
532 Create( parent, id, value, pos, size, style, validator, name );
533 }
534
535 bool wxTextCtrl::Create( wxWindow *parent,
536 wxWindowID id,
537 const wxString &value,
538 const wxPoint &pos,
539 const wxSize &size,
540 long style,
541 const wxValidator& validator,
542 const wxString &name )
543 {
544 m_needParent = true;
545 m_acceptsFocus = true;
546
547 if (!PreCreation( parent, pos, size ) ||
548 !CreateBase( parent, id, pos, size, style, validator, name ))
549 {
550 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
551 return false;
552 }
553
554
555 m_vScrollbarVisible = false;
556
557 bool multi_line = (style & wxTE_MULTILINE) != 0;
558
559 if (multi_line)
560 {
561 // Create view
562 m_text = gtk_text_view_new();
563
564 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
565
566 // create scrolled window
567 m_widget = gtk_scrolled_window_new( NULL, NULL );
568 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
569 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
570
571 // Insert view into scrolled window
572 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
573
574 // translate wx wrapping style to GTK+
575 GtkWrapMode wrap;
576 if ( HasFlag( wxTE_DONTWRAP ) )
577 wrap = GTK_WRAP_NONE;
578 else if ( HasFlag( wxTE_CHARWRAP ) )
579 wrap = GTK_WRAP_CHAR;
580 else if ( HasFlag( wxTE_WORDWRAP ) )
581 wrap = GTK_WRAP_WORD;
582 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
583 {
584 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
585 #ifdef __WXGTK24__
586 if ( !gtk_check_version(2,4,0) )
587 {
588 wrap = GTK_WRAP_WORD_CHAR;
589 }
590 else
591 #endif
592 wrap = GTK_WRAP_WORD;
593 }
594
595 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
596
597 if (!HasFlag(wxNO_BORDER))
598 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
599
600 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
601
602 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
603 }
604 else
605 {
606 // a single-line text control: no need for scrollbars
607 m_widget =
608 m_text = gtk_entry_new();
609
610 if (style & wxNO_BORDER)
611 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
612 }
613
614 m_parent->DoAddChild( this );
615
616 m_focusWidget = m_text;
617
618 PostCreation(size);
619
620 if (multi_line)
621 gtk_widget_show(m_text);
622
623 if (!value.empty())
624 {
625 SetValue( value );
626 }
627
628 if (style & wxTE_PASSWORD)
629 {
630 if (!multi_line)
631 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
632 }
633
634 if (style & wxTE_READONLY)
635 {
636 if (!multi_line)
637 gtk_editable_set_editable( GTK_EDITABLE(m_text), FALSE );
638 else
639 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
640 }
641
642 if (multi_line)
643 {
644 if (style & wxTE_RIGHT)
645 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
646 else if (style & wxTE_CENTRE)
647 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
648 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
649 }
650 else
651 {
652 #ifdef __WXGTK24__
653 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
654 if (!gtk_check_version(2,4,0))
655 {
656 if (style & wxTE_RIGHT)
657 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
658 else if (style & wxTE_CENTRE)
659 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
660 }
661 #endif
662 }
663
664 // We want to be notified about text changes.
665 if (multi_line)
666 {
667 g_signal_connect (m_buffer, "changed",
668 G_CALLBACK (gtk_text_changed_callback), this);
669
670 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
671 if (style & wxTE_AUTO_URL)
672 {
673 GtkTextIter start, end;
674 m_gdkHandCursor = gdk_cursor_new(GDK_HAND2);
675 m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM);
676
677 // We create our wxUrl tag here for slight efficiency gain - we
678 // don't have to check for the tag existance in callbacks,
679 // hereby it's guaranteed to exist.
680 gtk_text_buffer_create_tag(m_buffer, "wxUrl",
681 "foreground", "blue",
682 "underline", PANGO_UNDERLINE_SINGLE,
683 NULL);
684
685 // Check for URLs after each text change
686 g_signal_connect_after (m_buffer, "insert_text",
687 G_CALLBACK (au_insert_text_callback), this);
688 g_signal_connect_after (m_buffer, "delete_range",
689 G_CALLBACK (au_delete_range_callback), this);
690
691 // Block all wxUrl tag applying unless we do it ourselves, in which case we
692 // block this callback temporarily. This takes care of gtk+ internal
693 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
694 // which is undesired because only a part of the URL might be copied.
695 // The insert-text signal emitted inside it will take care of newly formed
696 // or wholly copied URLs.
697 g_signal_connect (m_buffer, "apply_tag",
698 G_CALLBACK (au_apply_tag_callback), NULL);
699
700 // Check for URLs in the initial string passed to Create
701 gtk_text_buffer_get_start_iter(m_buffer, &start);
702 gtk_text_buffer_get_end_iter(m_buffer, &end);
703 au_check_range(&start, &end);
704 }
705 }
706 else
707 {
708 g_signal_connect (m_text, "changed",
709 G_CALLBACK (gtk_text_changed_callback), this);
710 }
711
712 m_cursor = wxCursor( wxCURSOR_IBEAM );
713
714 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
715 SetDefaultStyle( attrDef );
716
717 return true;
718 }
719
720
721 void wxTextCtrl::CalculateScrollbar()
722 {
723 }
724
725 wxString wxTextCtrl::GetValue() const
726 {
727 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
728
729 wxString tmp;
730 if (m_windowStyle & wxTE_MULTILINE)
731 {
732 GtkTextIter start;
733 gtk_text_buffer_get_start_iter( m_buffer, &start );
734 GtkTextIter end;
735 gtk_text_buffer_get_end_iter( m_buffer, &end );
736 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
737
738 #if wxUSE_UNICODE
739 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
740 #else
741 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
742 #endif
743 if ( buffer )
744 tmp = buffer;
745
746 g_free( text );
747 }
748 else
749 {
750 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
751 }
752
753 return tmp;
754 }
755
756 void wxTextCtrl::SetValue( const wxString &value )
757 {
758 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
759
760 if (m_windowStyle & wxTE_MULTILINE)
761 {
762 #if wxUSE_UNICODE
763 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
764 #else
765 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
766 #endif
767 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
768 IgnoreNextTextUpdate();
769
770 if ( !buffer )
771 {
772 // what else can we do? at least don't crash...
773 return;
774 }
775
776 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
777 }
778 else
779 {
780 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
781 }
782
783 // GRG, Jun/2000: Changed this after a lot of discussion in
784 // the lists. wxWidgets 2.2 will have a set of flags to
785 // customize this behaviour.
786 SetInsertionPoint(0);
787
788 m_modified = false;
789 }
790
791 void wxTextCtrl::WriteText( const wxString &text )
792 {
793 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
794
795 if ( text.empty() )
796 return;
797
798 // gtk_text_changed_callback() will set m_modified to true but m_modified
799 // shouldn't be changed by the program writing to the text control itself,
800 // so save the old value and restore when we're done
801 bool oldModified = m_modified;
802
803 if ( m_windowStyle & wxTE_MULTILINE )
804 {
805 #if wxUSE_UNICODE
806 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
807 #else
808 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
809 #endif
810 if ( !buffer )
811 {
812 // what else can we do? at least don't crash...
813 return;
814 }
815
816 // TODO: Call whatever is needed to delete the selection.
817 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
818
819 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
820 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
821 if ( wxIsSameDouble(adj->value, adj->upper - adj->page_size) )
822 {
823 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
824 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
825 }
826 }
827 else // single line
828 {
829 // First remove the selection if there is one
830 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
831
832 // This moves the cursor pos to behind the inserted text.
833 gint len = gtk_editable_get_position(GTK_EDITABLE(m_text));
834
835 #if wxUSE_UNICODE
836 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
837 #else
838 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
839 #endif
840 if ( !buffer )
841 {
842 // what else can we do? at least don't crash...
843 return;
844 }
845
846 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
847
848 // Bring entry's cursor uptodate.
849 gtk_editable_set_position( GTK_EDITABLE(m_text), len );
850 }
851
852 m_modified = oldModified;
853 }
854
855 void wxTextCtrl::AppendText( const wxString &text )
856 {
857 SetInsertionPointEnd();
858 WriteText( text );
859 }
860
861 wxString wxTextCtrl::GetLineText( long lineNo ) const
862 {
863 if (m_windowStyle & wxTE_MULTILINE)
864 {
865 GtkTextIter line;
866 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
867 GtkTextIter end = line;
868 gtk_text_iter_forward_to_line_end(&end);
869 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
870 wxString result(wxGTK_CONV_BACK(text));
871 g_free(text);
872 return result;
873 }
874 else
875 {
876 if (lineNo == 0) return GetValue();
877 return wxEmptyString;
878 }
879 }
880
881 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
882 {
883 /* If you implement this, don't forget to update the documentation!
884 * (file docs/latex/wx/text.tex) */
885 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
886 }
887
888 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
889 {
890 if ( m_windowStyle & wxTE_MULTILINE )
891 {
892 GtkTextIter iter;
893 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
894 if (gtk_text_iter_is_end(&iter))
895 return false;
896
897 *y = gtk_text_iter_get_line(&iter);
898 *x = gtk_text_iter_get_line_offset(&iter);
899 }
900 else // single line control
901 {
902 if ( pos <= GTK_ENTRY(m_text)->text_length )
903 {
904 *y = 0;
905 *x = pos;
906 }
907 else
908 {
909 // index out of bounds
910 return false;
911 }
912 }
913
914 return true;
915 }
916
917 long wxTextCtrl::XYToPosition(long x, long y ) const
918 {
919 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
920
921 GtkTextIter iter;
922 if (y >= gtk_text_buffer_get_line_count (m_buffer))
923 return -1;
924
925 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
926 if (x >= gtk_text_iter_get_chars_in_line (&iter))
927 return -1;
928
929 return gtk_text_iter_get_offset(&iter) + x;
930 }
931
932 int wxTextCtrl::GetLineLength(long lineNo) const
933 {
934 if (m_windowStyle & wxTE_MULTILINE)
935 {
936 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
937 if (lineNo > last_line)
938 return -1;
939
940 GtkTextIter iter;
941 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
942 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
943 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
944 }
945 else
946 {
947 wxString str = GetLineText (lineNo);
948 return (int) str.Length();
949 }
950 }
951
952 int wxTextCtrl::GetNumberOfLines() const
953 {
954 if (m_windowStyle & wxTE_MULTILINE)
955 return gtk_text_buffer_get_line_count( m_buffer );
956 else
957 return 1;
958 }
959
960 void wxTextCtrl::SetInsertionPoint( long pos )
961 {
962 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
963
964 if ( IsMultiLine() )
965 {
966 GtkTextIter iter;
967 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
968 gtk_text_buffer_place_cursor( m_buffer, &iter );
969 gtk_text_view_scroll_mark_onscreen
970 (
971 GTK_TEXT_VIEW(m_text),
972 gtk_text_buffer_get_insert( m_buffer )
973 );
974 }
975 else
976 {
977 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
978 gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos));
979 }
980 }
981
982 void wxTextCtrl::SetInsertionPointEnd()
983 {
984 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
985
986 if (m_windowStyle & wxTE_MULTILINE)
987 {
988 GtkTextIter end;
989 gtk_text_buffer_get_end_iter( m_buffer, &end );
990 gtk_text_buffer_place_cursor( m_buffer, &end );
991 }
992 else
993 {
994 gtk_editable_set_position( GTK_EDITABLE(m_text), -1 );
995 }
996 }
997
998 void wxTextCtrl::SetEditable( bool editable )
999 {
1000 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1001
1002 if (m_windowStyle & wxTE_MULTILINE)
1003 {
1004 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1005 }
1006 else
1007 {
1008 gtk_editable_set_editable( GTK_EDITABLE(m_text), editable );
1009 }
1010 }
1011
1012 bool wxTextCtrl::Enable( bool enable )
1013 {
1014 if (!wxWindowBase::Enable(enable))
1015 {
1016 // nothing to do
1017 return false;
1018 }
1019
1020 if (m_windowStyle & wxTE_MULTILINE)
1021 {
1022 SetEditable( enable );
1023 }
1024 else
1025 {
1026 gtk_widget_set_sensitive( m_text, enable );
1027 }
1028
1029 return true;
1030 }
1031
1032 // wxGTK-specific: called recursively by Enable,
1033 // to give widgets an oppprtunity to correct their colours after they
1034 // have been changed by Enable
1035 void wxTextCtrl::OnParentEnable( bool enable )
1036 {
1037 // If we have a custom background colour, we use this colour in both
1038 // disabled and enabled mode, or we end up with a different colour under the
1039 // text.
1040 wxColour oldColour = GetBackgroundColour();
1041 if (oldColour.Ok())
1042 {
1043 // Need to set twice or it'll optimize the useful stuff out
1044 if (oldColour == * wxWHITE)
1045 SetBackgroundColour(*wxBLACK);
1046 else
1047 SetBackgroundColour(*wxWHITE);
1048 SetBackgroundColour(oldColour);
1049 }
1050 }
1051
1052 void wxTextCtrl::MarkDirty()
1053 {
1054 m_modified = true;
1055 }
1056
1057 void wxTextCtrl::DiscardEdits()
1058 {
1059 m_modified = false;
1060 }
1061
1062 // ----------------------------------------------------------------------------
1063 // max text length support
1064 // ----------------------------------------------------------------------------
1065
1066 void wxTextCtrl::IgnoreNextTextUpdate()
1067 {
1068 m_ignoreNextUpdate = true;
1069 }
1070
1071 bool wxTextCtrl::IgnoreTextUpdate()
1072 {
1073 if ( m_ignoreNextUpdate )
1074 {
1075 m_ignoreNextUpdate = false;
1076
1077 return true;
1078 }
1079
1080 return false;
1081 }
1082
1083 void wxTextCtrl::SetMaxLength(unsigned long len)
1084 {
1085 if ( !HasFlag(wxTE_MULTILINE) )
1086 {
1087 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
1088
1089 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1090 // we had tried to enter more text than allowed by max text length and
1091 // the text wasn't really changed
1092 //
1093 // to detect this and generate TEXT_MAXLEN event instead of
1094 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1095 //
1096 // when max len is set to 0 we disconnect our handler as it means that
1097 // we shouldn't check anything any more
1098 if ( len )
1099 {
1100 g_signal_connect (m_text, "insert_text",
1101 G_CALLBACK (gtk_insert_text_callback), this);
1102 }
1103 else // no checking
1104 {
1105 g_signal_handlers_disconnect_by_func (m_text,
1106 (gpointer) gtk_insert_text_callback, this);
1107 }
1108 }
1109 }
1110
1111 void wxTextCtrl::SetSelection( long from, long to )
1112 {
1113 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1114
1115 if (from == -1 && to == -1)
1116 {
1117 from = 0;
1118 to = GetValue().Length();
1119 }
1120
1121 if (m_windowStyle & wxTE_MULTILINE)
1122 {
1123 GtkTextIter fromi, toi;
1124 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1125 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1126
1127 gtk_text_buffer_place_cursor( m_buffer, &toi );
1128 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
1129 }
1130 else
1131 {
1132 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1133 }
1134 }
1135
1136 void wxTextCtrl::ShowPosition( long pos )
1137 {
1138 if (m_windowStyle & wxTE_MULTILINE)
1139 {
1140 GtkTextIter iter;
1141 gtk_text_buffer_get_start_iter( m_buffer, &iter );
1142 gtk_text_iter_set_offset( &iter, pos );
1143 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
1144 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1145 }
1146 }
1147
1148 wxTextCtrlHitTestResult
1149 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1150 {
1151 if ( !IsMultiLine() )
1152 {
1153 // not supported
1154 return wxTE_HT_UNKNOWN;
1155 }
1156
1157 int x, y;
1158 gtk_text_view_window_to_buffer_coords
1159 (
1160 GTK_TEXT_VIEW(m_text),
1161 GTK_TEXT_WINDOW_TEXT,
1162 pt.x, pt.y,
1163 &x, &y
1164 );
1165
1166 GtkTextIter iter;
1167 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1168 if ( pos )
1169 *pos = gtk_text_iter_get_offset(&iter);
1170
1171 return wxTE_HT_ON_TEXT;
1172 }
1173
1174 long wxTextCtrl::GetInsertionPoint() const
1175 {
1176 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1177
1178 if (m_windowStyle & wxTE_MULTILINE)
1179 {
1180 // There is no direct accessor for the cursor, but
1181 // internally, the cursor is the "mark" called
1182 // "insert" in the text view's btree structure.
1183
1184 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1185 GtkTextIter cursor;
1186 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1187
1188 return gtk_text_iter_get_offset( &cursor );
1189 }
1190 else
1191 {
1192 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text));
1193 }
1194 }
1195
1196 wxTextPos wxTextCtrl::GetLastPosition() const
1197 {
1198 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1199
1200 int pos = 0;
1201
1202 if (m_windowStyle & wxTE_MULTILINE)
1203 {
1204 GtkTextIter end;
1205 gtk_text_buffer_get_end_iter( m_buffer, &end );
1206
1207 pos = gtk_text_iter_get_offset( &end );
1208 }
1209 else
1210 {
1211 pos = GTK_ENTRY(m_text)->text_length;
1212 }
1213
1214 return (long)pos;
1215 }
1216
1217 void wxTextCtrl::Remove( long from, long to )
1218 {
1219 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1220
1221 if (m_windowStyle & wxTE_MULTILINE)
1222 {
1223 GtkTextIter fromi, toi;
1224 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1225 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1226
1227 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1228 }
1229 else // single line
1230 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1231 }
1232
1233 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1234 {
1235 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1236
1237 Remove( from, to );
1238
1239 if (!value.empty())
1240 {
1241 SetInsertionPoint( from );
1242 WriteText( value );
1243 }
1244 }
1245
1246 void wxTextCtrl::Cut()
1247 {
1248 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1249
1250 if (m_windowStyle & wxTE_MULTILINE)
1251 g_signal_emit_by_name (m_text, "cut-clipboard");
1252 else
1253 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text));
1254 }
1255
1256 void wxTextCtrl::Copy()
1257 {
1258 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1259
1260 if (m_windowStyle & wxTE_MULTILINE)
1261 g_signal_emit_by_name (m_text, "copy-clipboard");
1262 else
1263 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text));
1264 }
1265
1266 void wxTextCtrl::Paste()
1267 {
1268 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1269
1270 if (m_windowStyle & wxTE_MULTILINE)
1271 g_signal_emit_by_name (m_text, "paste-clipboard");
1272 else
1273 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text));
1274 }
1275
1276 // Undo/redo
1277 void wxTextCtrl::Undo()
1278 {
1279 // TODO
1280 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1281 }
1282
1283 void wxTextCtrl::Redo()
1284 {
1285 // TODO
1286 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1287 }
1288
1289 bool wxTextCtrl::CanUndo() const
1290 {
1291 // TODO
1292 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1293 return false;
1294 }
1295
1296 bool wxTextCtrl::CanRedo() const
1297 {
1298 // TODO
1299 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1300 return false;
1301 }
1302
1303 // If the return values from and to are the same, there is no
1304 // selection.
1305 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1306 {
1307 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1308
1309 gint from = -1;
1310 gint to = -1;
1311 bool haveSelection = false;
1312
1313 if (m_windowStyle & wxTE_MULTILINE)
1314 {
1315 GtkTextIter ifrom, ito;
1316 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1317 {
1318 haveSelection = true;
1319 from = gtk_text_iter_get_offset(&ifrom);
1320 to = gtk_text_iter_get_offset(&ito);
1321 }
1322 }
1323 else // not multi-line
1324 {
1325 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1326 &from, &to) )
1327 {
1328 haveSelection = true;
1329 }
1330 }
1331
1332 if (! haveSelection )
1333 from = to = GetInsertionPoint();
1334
1335 if ( from > to )
1336 {
1337 // exchange them to be compatible with wxMSW
1338 gint tmp = from;
1339 from = to;
1340 to = tmp;
1341 }
1342
1343 if ( fromOut )
1344 *fromOut = from;
1345 if ( toOut )
1346 *toOut = to;
1347 }
1348
1349
1350 bool wxTextCtrl::IsEditable() const
1351 {
1352 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1353
1354 if (m_windowStyle & wxTE_MULTILINE)
1355 {
1356 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1357 }
1358 else
1359 {
1360 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1361 }
1362 }
1363
1364 bool wxTextCtrl::IsModified() const
1365 {
1366 return m_modified;
1367 }
1368
1369 void wxTextCtrl::Clear()
1370 {
1371 SetValue( wxEmptyString );
1372 }
1373
1374 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1375 {
1376 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1377
1378 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1379 {
1380 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1381 event.SetEventObject(this);
1382 event.SetString(GetValue());
1383 if (GetEventHandler()->ProcessEvent(event)) return;
1384 }
1385
1386 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1387 {
1388 // This will invoke the dialog default action, such
1389 // as the clicking the default button.
1390
1391 wxWindow *top_frame = m_parent;
1392 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1393 top_frame = top_frame->GetParent();
1394
1395 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1396 {
1397 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1398
1399 if (window->default_widget)
1400 {
1401 gtk_widget_activate (window->default_widget);
1402 return;
1403 }
1404 }
1405 }
1406
1407 key_event.Skip();
1408 }
1409
1410 GtkWidget* wxTextCtrl::GetConnectWidget()
1411 {
1412 return GTK_WIDGET(m_text);
1413 }
1414
1415 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1416 {
1417 if (m_windowStyle & wxTE_MULTILINE)
1418 {
1419 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1420 }
1421 else
1422 {
1423 return (window == GTK_ENTRY(m_text)->text_area);
1424 }
1425 }
1426
1427 // the font will change for subsequent text insertiongs
1428 bool wxTextCtrl::SetFont( const wxFont &font )
1429 {
1430 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1431
1432 if ( !wxTextCtrlBase::SetFont(font) )
1433 {
1434 // font didn't change, nothing to do
1435 return false;
1436 }
1437
1438 if ( m_windowStyle & wxTE_MULTILINE )
1439 {
1440 SetUpdateFont(true);
1441
1442 m_defaultStyle.SetFont(font);
1443
1444 ChangeFontGlobally();
1445 }
1446
1447 return true;
1448 }
1449
1450 void wxTextCtrl::ChangeFontGlobally()
1451 {
1452 // this method is very inefficient and hence should be called as rarely as
1453 // possible!
1454 //
1455 // TODO: it can be implemented much more efficiently for GTK2
1456 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1457 _T("shouldn't be called for single line controls") );
1458
1459 wxString value = GetValue();
1460 if ( !value.empty() )
1461 {
1462 SetUpdateFont(false);
1463
1464 Clear();
1465 AppendText(value);
1466 }
1467 }
1468
1469 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1470 {
1471 if ( !wxControl::SetForegroundColour(colour) )
1472 return false;
1473
1474 // update default fg colour too
1475 m_defaultStyle.SetTextColour(colour);
1476
1477 return true;
1478 }
1479
1480 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1481 {
1482 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1483
1484 if ( !wxControl::SetBackgroundColour( colour ) )
1485 return false;
1486
1487 if (!m_backgroundColour.Ok())
1488 return false;
1489
1490 // change active background color too
1491 m_defaultStyle.SetBackgroundColour( colour );
1492
1493 return true;
1494 }
1495
1496 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1497 {
1498 if ( m_windowStyle & wxTE_MULTILINE )
1499 {
1500 if ( style.IsDefault() )
1501 {
1502 // nothing to do
1503 return true;
1504 }
1505
1506 gint l = gtk_text_buffer_get_char_count( m_buffer );
1507
1508 wxCHECK_MSG( start >= 0 && end <= l, false,
1509 _T("invalid range in wxTextCtrl::SetStyle") );
1510
1511 GtkTextIter starti, endi;
1512 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1513 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1514
1515 // use the attributes from style which are set in it and fall back
1516 // first to the default style and then to the text control default
1517 // colours for the others
1518 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1519
1520 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
1521
1522 return true;
1523 }
1524
1525 // else single line
1526 // cannot do this for GTK+'s Entry widget
1527 return false;
1528 }
1529
1530 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1531 {
1532 gtk_widget_modify_style(m_text, style);
1533 }
1534
1535 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1536 {
1537 Cut();
1538 }
1539
1540 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1541 {
1542 Copy();
1543 }
1544
1545 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1546 {
1547 Paste();
1548 }
1549
1550 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1551 {
1552 Undo();
1553 }
1554
1555 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1556 {
1557 Redo();
1558 }
1559
1560 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1561 {
1562 event.Enable( CanCut() );
1563 }
1564
1565 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1566 {
1567 event.Enable( CanCopy() );
1568 }
1569
1570 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1571 {
1572 event.Enable( CanPaste() );
1573 }
1574
1575 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1576 {
1577 event.Enable( CanUndo() );
1578 }
1579
1580 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1581 {
1582 event.Enable( CanRedo() );
1583 }
1584
1585 void wxTextCtrl::OnInternalIdle()
1586 {
1587 wxCursor cursor = m_cursor;
1588 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1589
1590 if (g_delayedFocus == this)
1591 {
1592 if (GTK_WIDGET_REALIZED(m_widget))
1593 {
1594 gtk_widget_grab_focus( m_widget );
1595 g_delayedFocus = NULL;
1596 }
1597 }
1598
1599 if (wxUpdateUIEvent::CanUpdate(this))
1600 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1601 }
1602
1603 wxSize wxTextCtrl::DoGetBestSize() const
1604 {
1605 // FIXME should be different for multi-line controls...
1606 wxSize ret( wxControl::DoGetBestSize() );
1607 wxSize best(80, ret.y);
1608 CacheBestSize(best);
1609 return best;
1610 }
1611
1612 // ----------------------------------------------------------------------------
1613 // freeze/thaw
1614 // ----------------------------------------------------------------------------
1615
1616 void wxTextCtrl::Freeze()
1617 {
1618 if ( HasFlag(wxTE_MULTILINE) )
1619 {
1620 if ( !m_frozenness++ )
1621 {
1622 // freeze textview updates and remove buffer
1623 g_signal_connect (m_text, "expose_event",
1624 G_CALLBACK (gtk_text_exposed_callback), this);
1625 g_signal_connect (m_widget, "expose_event",
1626 G_CALLBACK (gtk_text_exposed_callback), this);
1627 gtk_widget_set_sensitive(m_widget, false);
1628 g_object_ref(m_buffer);
1629 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
1630 }
1631 }
1632 }
1633
1634 void wxTextCtrl::Thaw()
1635 {
1636 if ( HasFlag(wxTE_MULTILINE) )
1637 {
1638 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
1639
1640 if ( !--m_frozenness )
1641 {
1642 // Reattach buffer and thaw textview updates
1643 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1644 g_object_unref(m_buffer);
1645 gtk_widget_set_sensitive(m_widget, true);
1646 g_signal_handlers_disconnect_by_func (m_widget,
1647 (gpointer) gtk_text_exposed_callback, this);
1648 g_signal_handlers_disconnect_by_func (m_text,
1649 (gpointer) gtk_text_exposed_callback, this);
1650 }
1651 }
1652 }
1653
1654 // ----------------------------------------------------------------------------
1655 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1656 // ----------------------------------------------------------------------------
1657
1658 // FIXME: when dragging on a link the sample gets an "Unknown event".
1659 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1660 // a buggy sample, or something else
1661 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
1662 {
1663 event.Skip();
1664 if(!(m_windowStyle & wxTE_AUTO_URL))
1665 return;
1666
1667 gint x, y;
1668 GtkTextIter start, end;
1669 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
1670 "wxUrl");
1671
1672 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
1673 event.GetX(), event.GetY(), &x, &y);
1674
1675 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
1676 if (!gtk_text_iter_has_tag(&end, tag))
1677 {
1678 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1679 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
1680 return;
1681 }
1682
1683 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1684 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
1685
1686 start = end;
1687 if(!gtk_text_iter_begins_tag(&start, tag))
1688 gtk_text_iter_backward_to_tag_toggle(&start, tag);
1689 if(!gtk_text_iter_ends_tag(&end, tag))
1690 gtk_text_iter_forward_to_tag_toggle(&end, tag);
1691
1692 // Native context menu is probably not desired on an URL.
1693 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1694 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
1695 event.Skip(false);
1696
1697 wxTextUrlEvent url_event(m_windowId, event,
1698 gtk_text_iter_get_offset(&start),
1699 gtk_text_iter_get_offset(&end));
1700
1701 InitCommandEvent(url_event);
1702 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1703 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1704 GetEventHandler()->ProcessEvent(url_event);
1705 }
1706
1707 // ----------------------------------------------------------------------------
1708 // scrolling
1709 // ----------------------------------------------------------------------------
1710
1711 GtkAdjustment *wxTextCtrl::GetVAdj() const
1712 {
1713 if ( !IsMultiLine() )
1714 return NULL;
1715
1716 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
1717 }
1718
1719 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1720 {
1721 float value = adj->value + diff;
1722
1723 if ( value < 0 )
1724 value = 0;
1725
1726 float upper = adj->upper - adj->page_size;
1727 if ( value > upper )
1728 value = upper;
1729
1730 // did we noticeably change the scroll position?
1731 if ( fabs(adj->value - value) < 0.2 )
1732 {
1733 // well, this is what Robert does in wxScrollBar, so it must be good...
1734 return false;
1735 }
1736
1737 adj->value = value;
1738
1739 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
1740
1741 return true;
1742 }
1743
1744 bool wxTextCtrl::ScrollLines(int lines)
1745 {
1746 GtkAdjustment *adj = GetVAdj();
1747 if ( !adj )
1748 return false;
1749
1750 int diff = (int)ceil(lines*adj->step_increment);
1751
1752 return DoScroll(adj, diff);
1753 }
1754
1755 bool wxTextCtrl::ScrollPages(int pages)
1756 {
1757 GtkAdjustment *adj = GetVAdj();
1758 if ( !adj )
1759 return false;
1760
1761 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1762 }
1763
1764
1765 // static
1766 wxVisualAttributes
1767 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1768 {
1769 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1770 }