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