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