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