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