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