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