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