]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
Moved all the coordinate system calculation to wxDCBase
[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"
9eddec69 19 #include "wx/settings.h"
18680f86 20 #include "wx/math.h"
88a7a4e1
WS
21#endif
22
fab591c5 23#include "wx/strconv.h"
cc3da3f8 24#include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
c801d85f 25
a81258be
RR
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <ctype.h>
29
9e691f46 30#include "wx/gtk/private.h"
b292e2f5 31
eda40bfc
VZ
32// ----------------------------------------------------------------------------
33// helpers
34// ----------------------------------------------------------------------------
35
418cf02e
JS
36extern "C" {
37static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
38 GtkTextTag *tag,
39 GtkTextIter *start,
40 GtkTextIter *end,
50aee613 41 char *prefix)
418cf02e
JS
42{
43 gchar *name;
44 g_object_get (tag, "name", &name, NULL);
45
50aee613
MR
46 if (!name || strncmp(name, prefix, strlen(prefix)))
47 // anonymous tag or not starting with prefix - don't remove
9fa72bd2 48 g_signal_stop_emission_by_name (buffer, "remove_tag");
418cf02e
JS
49
50 g_free(name);
51}
52}
53
7b377ed9
VZ
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
ae6a64b6
MR
72static void wxGtkTextApplyTagsFromAttr(GtkWidget *text,
73 GtkTextBuffer *text_buffer,
25497324
KH
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 {
7b377ed9
VZ
83 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXFONT", start, end);
84
25497324 85 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
e808cf8a
PC
86 wxGtkString font_string(pango_font_description_to_string(font_description));
87 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string.c_str());
25497324
KH
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);
18634476
VZ
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 }
25497324
KH
108 }
109
110 if (attr.HasTextColour())
111 {
7b377ed9
VZ
112 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXFORECOLOR", start, end);
113
c6685317 114 const GdkColor *colFg = attr.GetTextColour().GetColor();
25497324
KH
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 {
7b377ed9
VZ
127 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXBACKCOLOR", start, end);
128
c6685317 129 const GdkColor *colBg = attr.GetBackgroundColour().GetColor();
25497324
KH
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 }
50aee613
MR
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
7b377ed9 148 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXALIGNMENT", start, end);
50aee613
MR
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 }
ae6a64b6
MR
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
7b377ed9 185 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXINDENT", start, end);
ae6a64b6
MR
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
7b377ed9 234 wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXTABS", start, end);
ae6a64b6
MR
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 }
25497324
KH
268}
269
cc3da3f8
RR
270static void wxGtkTextInsert(GtkWidget *text,
271 GtkTextBuffer *text_buffer,
272 const wxTextAttr& attr,
fbfb8bcc 273 const wxCharBuffer& buffer)
cc3da3f8
RR
274
275{
25497324
KH
276 gint start_offset;
277 GtkTextIter iter, start;
cc3da3f8 278
a61bbf87
JS
279 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
280 gtk_text_buffer_get_insert (text_buffer) );
25497324
KH
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);
a61bbf87 285
ae6a64b6 286 wxGtkTextApplyTagsFromAttr(text, text_buffer, attr, &start, &iter);
cc3da3f8 287}
eda40bfc 288
ce2f50e3
VZ
289// ----------------------------------------------------------------------------
290// "insert_text" for GtkEntry
291// ----------------------------------------------------------------------------
292
865bb325 293extern "C" {
ce2f50e3
VZ
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
9fa72bd2 313 g_signal_stop_emission_by_name (editable, "insert_text");
ce2f50e3
VZ
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}
865bb325 326}
ce2f50e3 327
9440c3d0
KH
328// Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
329
865bb325 330extern "C" {
9440c3d0
KH
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"))
9fa72bd2 339 g_signal_stop_emission_by_name (buffer, "apply_tag");
9440c3d0 340}
865bb325 341}
9440c3d0
KH
342
343//-----------------------------------------------------------------------------
344// GtkTextCharPredicates for gtk_text_iter_*_find_char
345//-----------------------------------------------------------------------------
346
865bb325 347extern "C" {
9440c3d0
KH
348static gboolean
349pred_whitespace (gunichar ch, gpointer user_data)
350{
351 return g_unichar_isspace(ch);
352}
865bb325 353}
9440c3d0 354
865bb325 355extern "C" {
9440c3d0
KH
356static gboolean
357pred_non_whitespace (gunichar ch, gpointer user_data)
358{
359 return !g_unichar_isspace(ch);
360}
865bb325 361}
9440c3d0 362
865bb325 363extern "C" {
9440c3d0
KH
364static gboolean
365pred_nonpunct (gunichar ch, gpointer user_data)
366{
367 return !g_unichar_ispunct(ch);
368}
865bb325 369}
9440c3d0 370
865bb325 371extern "C" {
9440c3d0
KH
372static gboolean
373pred_nonpunct_or_slash (gunichar ch, gpointer user_data)
374{
375 return !g_unichar_ispunct(ch) || ch == '/';
376}
865bb325 377}
9440c3d0
KH
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.
865bb325 385extern "C" {
9440c3d0
KH
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);
418cf02e 409
9440c3d0
KH
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 {
9fa72bd2
MR
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);
9440c3d0 439
9fa72bd2 440 g_signal_handler_block (buffer, signal_id);
9440c3d0 441 gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
9fa72bd2 442 g_signal_handler_unblock (buffer, signal_id);
9440c3d0
KH
443 }
444}
865bb325 445}
9440c3d0 446
865bb325 447extern "C" {
9440c3d0
KH
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}
865bb325 474}
9440c3d0
KH
475
476//-----------------------------------------------------------------------------
477// "insert-text" for GtkTextBuffer
478//-----------------------------------------------------------------------------
479
865bb325 480extern "C" {
9440c3d0
KH
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}
865bb325 506}
9440c3d0
KH
507
508//-----------------------------------------------------------------------------
509// "delete-range" for GtkTextBuffer
510//-----------------------------------------------------------------------------
511
865bb325 512extern "C" {
9440c3d0
KH
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}
865bb325 531}
9440c3d0
KH
532
533
c801d85f 534//-----------------------------------------------------------------------------
2f2aa628 535// "changed"
c801d85f
KB
536//-----------------------------------------------------------------------------
537
865bb325 538extern "C" {
805dd538 539static void
ba3f6b44 540gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
484e45bf 541{
ce2f50e3
VZ
542 if ( win->IgnoreTextUpdate() )
543 return;
544
a2053b27 545 if (!win->m_hasVMT) return;
805dd538 546
6964cbba
VZ
547 if ( win->MarkDirtyOnChange() )
548 win->MarkDirty();
a8bf1826 549
ee2ec18e 550 win->SendTextUpdatedEvent();
6de97a3b 551}
865bb325 552}
112892b9 553
c85f2eb1
VZ
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,
9eddec69 561 wxEventType eventType, const gchar * signal_name)
c85f2eb1
VZ
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
9eddec69 569 g_signal_stop_emission_by_name (widget, signal_name);
c85f2eb1
VZ
570 }
571}
572
573extern "C" {
574static void
575gtk_copy_clipboard_callback( GtkWidget *widget, wxTextCtrl *win )
576{
9eddec69
WS
577 handle_text_clipboard_callback(
578 widget, win, wxEVT_COMMAND_TEXT_COPY, "copy-clipboard" );
c85f2eb1
VZ
579}
580
581static void
582gtk_cut_clipboard_callback( GtkWidget *widget, wxTextCtrl *win )
583{
9eddec69
WS
584 handle_text_clipboard_callback(
585 widget, win, wxEVT_COMMAND_TEXT_CUT, "cut-clipboard" );
c85f2eb1
VZ
586}
587
588static void
589gtk_paste_clipboard_callback( GtkWidget *widget, wxTextCtrl *win )
590{
9eddec69
WS
591 handle_text_clipboard_callback(
592 widget, win, wxEVT_COMMAND_TEXT_PASTE, "paste-clipboard" );
c85f2eb1
VZ
593}
594}
595
41b81aed
RR
596//-----------------------------------------------------------------------------
597// "expose_event" from scrolled window and textview
598//-----------------------------------------------------------------------------
599
865bb325 600extern "C" {
41b81aed
RR
601static gboolean
602gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
603{
604 return TRUE;
605}
865bb325 606}
1c35b54e 607
1c35b54e 608
2f2aa628
RR
609//-----------------------------------------------------------------------------
610// wxTextCtrl
611//-----------------------------------------------------------------------------
612
9d112688 613IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
2f2aa628 614
9d112688 615BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
2830bf19 616 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
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)
9440c3d0 629
9440c3d0
KH
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)
c801d85f
KB
639END_EVENT_TABLE()
640
01041145 641void wxTextCtrl::Init()
f5abe911 642{
6964cbba 643 m_dontMarkDirty =
7d8268a1 644 m_modified = false;
6964cbba 645
f6519b40
VZ
646 m_countUpdatesToIgnore = 0;
647
7d8268a1 648 SetUpdateFont(false);
6964cbba 649
85396430 650 m_text = NULL;
5a3ef194
PC
651 m_freezeCount = 0;
652 m_showPositionOnThaw = NULL;
9440c3d0
KH
653 m_gdkHandCursor = NULL;
654 m_gdkXTermCursor = NULL;
9440c3d0
KH
655}
656
657wxTextCtrl::~wxTextCtrl()
658{
9440c3d0
KH
659 if(m_gdkHandCursor)
660 gdk_cursor_unref(m_gdkHandCursor);
661 if(m_gdkXTermCursor)
662 gdk_cursor_unref(m_gdkXTermCursor);
f5abe911 663}
13289f04 664
13111b2a
VZ
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 )
f5abe911 673{
01041145
VZ
674 Init();
675
f5abe911
RR
676 Create( parent, id, value, pos, size, style, validator, name );
677}
c801d85f 678
13111b2a
VZ
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 )
c801d85f 687{
4dcaf11a
RR
688 if (!PreCreation( parent, pos, size ) ||
689 !CreateBase( parent, id, pos, size, style, validator, name ))
690 {
223d09f6 691 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
7d8268a1 692 return false;
4dcaf11a 693 }
6de97a3b 694
2830bf19 695 bool multi_line = (style & wxTE_MULTILINE) != 0;
a8bf1826 696
ab46dc18 697 if (multi_line)
2830bf19 698 {
fab591c5
RR
699 // Create view
700 m_text = gtk_text_view_new();
a8bf1826 701
41b81aed 702 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
a8bf1826 703
5a3ef194
PC
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
fab591c5
RR
709 // create scrolled window
710 m_widget = gtk_scrolled_window_new( NULL, NULL );
711 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
09b86e69
VZ
712 GTK_POLICY_AUTOMATIC,
713 style & wxTE_NO_VSCROLL
714 ? GTK_POLICY_NEVER
715 : GTK_POLICY_AUTOMATIC );
add7cadd
PC
716 // for ScrollLines/Pages
717 m_scrollBar[1] = (GtkRange*)((GtkScrolledWindow*)m_widget)->vscrollbar;
a8bf1826 718
fab591c5
RR
719 // Insert view into scrolled window
720 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
a8bf1826 721
927637fd 722 GTKSetWrapMode();
805dd538 723
6493aaca 724 GtkScrolledWindowSetBorder(m_widget, style);
7d8268a1 725
e327fddf
KH
726 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
727
055e633d 728 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19
RR
729 }
730 else
731 {
fab591c5 732 // a single-line text control: no need for scrollbars
2830bf19 733 m_widget =
1c35b54e 734 m_text = gtk_entry_new();
44c5573d 735
02a6e358 736 if (style & wxNO_BORDER)
7d1fea10 737 g_object_set (m_text, "has-frame", FALSE, NULL);
2830bf19 738 }
484e45bf 739
db434467 740 m_parent->DoAddChild( this );
eda40bfc 741
76fcf0f2 742 m_focusWidget = m_text;
db434467 743
abdeb9e7 744 PostCreation(size);
484e45bf 745
2830bf19 746 if (multi_line)
0c131a5a 747 {
2830bf19 748 gtk_widget_show(m_text);
0c131a5a 749 }
13289f04 750
7d8268a1 751 if (!value.empty())
2830bf19 752 {
fab591c5 753 SetValue( value );
2830bf19 754 }
484e45bf 755
2830bf19 756 if (style & wxTE_PASSWORD)
927637fd 757 GTKSetVisibility();
8bbe427f 758
2830bf19 759 if (style & wxTE_READONLY)
927637fd 760 GTKSetEditable();
98a8daf4 761
927637fd
VZ
762 // left justification (alignment) is the default anyhow
763 if ( style & (wxTE_RIGHT | wxTE_CENTRE) )
764 GTKSetJustification();
7d8268a1 765
fab591c5 766 // We want to be notified about text changes.
fab591c5
RR
767 if (multi_line)
768 {
9fa72bd2
MR
769 g_signal_connect (m_buffer, "changed",
770 G_CALLBACK (gtk_text_changed_callback), this);
9440c3d0
KH
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
9fa72bd2
MR
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);
9440c3d0
KH
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.
9fa72bd2
MR
799 g_signal_connect (m_buffer, "apply_tag",
800 G_CALLBACK (au_apply_tag_callback), NULL);
9440c3d0
KH
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 }
fab591c5
RR
807 }
808 else
fab591c5 809 {
9fa72bd2
MR
810 g_signal_connect (m_text, "changed",
811 G_CALLBACK (gtk_text_changed_callback), this);
fab591c5 812 }
ce16e5d7 813
c85f2eb1
VZ
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
65045edd 821 m_cursor = wxCursor( wxCURSOR_IBEAM );
13111b2a 822
7d8268a1 823 return true;
2830bf19 824}
484e45bf 825
927637fd
VZ
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
41e2aad5 888 gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text), just);
927637fd
VZ
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
03f38c58 936wxString wxTextCtrl::GetValue() const
c801d85f 937{
902725ee 938 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
8bbe427f 939
2830bf19 940 wxString tmp;
75812722 941 if ( IsMultiLine() )
2830bf19 942 {
fab591c5 943 GtkTextIter start;
41b81aed 944 gtk_text_buffer_get_start_iter( m_buffer, &start );
fab591c5 945 GtkTextIter end;
41b81aed 946 gtk_text_buffer_get_end_iter( m_buffer, &end );
e808cf8a 947 wxGtkString text(gtk_text_buffer_get_text(m_buffer, &start, &end, true));
5b87f8bf 948
a3669332
VZ
949 const wxWxCharBuffer buf = wxGTK_CONV_BACK(text);
950 if ( buf )
951 tmp = buf;
2830bf19
RR
952 }
953 else
954 {
a3669332
VZ
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;
2830bf19 959 }
a8bf1826 960
2830bf19 961 return tmp;
6de97a3b 962}
c801d85f 963
0d91b234
VZ
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
28fdd8db
VZ
982bool wxTextCtrl::IsEmpty() const
983{
984 if ( IsMultiLine() )
14be4734 985 return gtk_text_buffer_get_char_count(m_buffer) == 0;
28fdd8db
VZ
986
987 return wxTextCtrlBase::IsEmpty();
988}
989
f6519b40 990void wxTextCtrl::DoSetValue( const wxString &value, int flags )
c801d85f 991{
223d09f6 992 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 993
6964cbba
VZ
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
785d8330
VZ
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));
7e0fff42 1008 if ( !buffer )
2830bf19 1009 {
7e0fff42
VZ
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 }
418cf02e 1015
7e0fff42
VZ
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++;
f6519b40 1021
7e0fff42
VZ
1022 if ( ignore )
1023 IgnoreNextTextUpdate(ignore);
a3669332 1024
7e0fff42
VZ
1025 if ( IsMultiLine() )
1026 {
41b81aed 1027 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
785d8330
VZ
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 }
2830bf19 1036 }
265c34ee 1037 else // single line
2830bf19 1038 {
7e0fff42 1039 gtk_entry_set_text( GTK_ENTRY(m_text), buffer );
2830bf19 1040 }
f6bcfd97 1041
f8bf59a9
VZ
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
f6bcfd97 1048 // GRG, Jun/2000: Changed this after a lot of discussion in
77ffb593 1049 // the lists. wxWidgets 2.2 will have a set of flags to
f6bcfd97
BP
1050 // customize this behaviour.
1051 SetInsertionPoint(0);
6de97a3b 1052}
c801d85f
KB
1053
1054void wxTextCtrl::WriteText( const wxString &text )
1055{
223d09f6 1056 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1057
17665a2b
VZ
1058 if ( text.empty() )
1059 return;
484e45bf 1060
0d91b234
VZ
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));
a3669332
VZ
1073 if ( !buffer )
1074 {
0d91b234
VZ
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."));
a3669332
VZ
1079 return;
1080 }
1081
6964cbba
VZ
1082 // we're changing the text programmatically
1083 DontMarkDirtyOnNextChange();
c04ec496 1084
75812722 1085 if ( IsMultiLine() )
2830bf19 1086 {
ea5449ae
RD
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
41b81aed 1095 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
a8bf1826 1096
71aba833
VZ
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
442a960c
PC
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))
71aba833
VZ
1101 {
1102 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
41b81aed 1103 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
71aba833 1104 }
2830bf19 1105 }
17665a2b 1106 else // single line
2830bf19 1107 {
2b5f62a0
VZ
1108 // First remove the selection if there is one
1109 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
1110
ba3f6b44 1111 // This moves the cursor pos to behind the inserted text.
afa7bd1e 1112 gint len = gtk_editable_get_position(GTK_EDITABLE(m_text));
a8bf1826 1113
fab591c5 1114 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
a8bf1826 1115
ba3f6b44 1116 // Bring entry's cursor uptodate.
6f85e712 1117 gtk_editable_set_position( GTK_EDITABLE(m_text), len );
2830bf19 1118 }
6de97a3b 1119}
c801d85f 1120
a6e21573
HH
1121void wxTextCtrl::AppendText( const wxString &text )
1122{
ba3f6b44
RR
1123 SetInsertionPointEnd();
1124 WriteText( text );
1125}
2df7be7f 1126
ba3f6b44
RR
1127wxString wxTextCtrl::GetLineText( long lineNo ) const
1128{
e808cf8a 1129 wxString result;
75812722 1130 if ( IsMultiLine() )
a6e21573 1131 {
905f2110 1132 GtkTextIter line;
41b81aed 1133 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
5440a04f 1134
8c6785f0 1135 GtkTextIter end = line;
5440a04f
VZ
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
e808cf8a
PC
1140 wxGtkString text(gtk_text_buffer_get_text(m_buffer, &line, &end, true));
1141 result = wxGTK_CONV_BACK(text);
a6e21573 1142 }
ba3f6b44 1143 else
a81258be 1144 {
e808cf8a
PC
1145 if (lineNo == 0)
1146 result = GetValue();
a81258be 1147 }
e808cf8a 1148 return result;
6de97a3b 1149}
c801d85f 1150
a81258be
RR
1151void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
1152{
ac0d36b5
HH
1153 /* If you implement this, don't forget to update the documentation!
1154 * (file docs/latex/wx/text.tex) */
223d09f6 1155 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 1156}
112892b9 1157
0efe5ba7 1158bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 1159{
75812722 1160 if ( IsMultiLine() )
805dd538 1161 {
f29a481a 1162 GtkTextIter iter;
01207937
MR
1163
1164 if (pos > GetLastPosition())
1165 return false;
1166
f29a481a 1167 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
f29a481a 1168
50259962
MR
1169 if ( y )
1170 *y = gtk_text_iter_get_line(&iter);
1171 if ( x )
1172 *x = gtk_text_iter_get_line_offset(&iter);
805dd538 1173 }
96385642
VZ
1174 else // single line control
1175 {
2829d9e3 1176 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 1177 {
50259962
MR
1178 if ( y )
1179 *y = 0;
1180 if ( x )
1181 *x = pos;
96385642
VZ
1182 }
1183 else
1184 {
1185 // index out of bounds
7d8268a1 1186 return false;
96385642 1187 }
8bbe427f 1188 }
96385642 1189
7d8268a1 1190 return true;
6de97a3b 1191}
c801d85f 1192
e3ca08dd 1193long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 1194{
75812722
VZ
1195 if ( IsSingleLine() )
1196 return 0;
805dd538 1197
f29a481a 1198 GtkTextIter iter;
21d23b88
MR
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;
6de97a3b 1207}
c801d85f 1208
a81258be 1209int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 1210{
75812722 1211 if ( IsMultiLine() )
f29a481a
MR
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
f29a481a
MR
1223 {
1224 wxString str = GetLineText (lineNo);
8e13c1ec 1225 return (int) str.length();
f29a481a 1226 }
6de97a3b 1227}
c801d85f 1228
a81258be 1229int wxTextCtrl::GetNumberOfLines() const
c801d85f 1230{
75812722 1231 if ( IsMultiLine() )
e894be20 1232 {
c68dea66 1233 return gtk_text_buffer_get_line_count( m_buffer );
e894be20
VZ
1234 }
1235 else // single line
1236 {
96385642 1237 return 1;
e894be20 1238 }
6de97a3b 1239}
c801d85f 1240
debe6624 1241void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 1242{
223d09f6 1243 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e 1244
74c5a810 1245 if ( IsMultiLine() )
291a8f20 1246 {
fab591c5 1247 GtkTextIter iter;
41b81aed
RR
1248 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1249 gtk_text_buffer_place_cursor( m_buffer, &iter );
5a3ef194
PC
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);
ac0d36b5 1256 }
2830bf19 1257 else
291a8f20 1258 {
6f85e712 1259 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
afa7bd1e 1260 gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos));
291a8f20 1261 }
6de97a3b 1262}
c801d85f 1263
03f38c58 1264void wxTextCtrl::SetInsertionPointEnd()
c801d85f 1265{
b3ffb94d 1266 SetInsertionPoint(-1);
6de97a3b 1267}
c801d85f 1268
debe6624 1269void wxTextCtrl::SetEditable( bool editable )
c801d85f 1270{
223d09f6 1271 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1272
75812722 1273 if ( IsMultiLine() )
fab591c5 1274 {
fab591c5 1275 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
fab591c5 1276 }
2830bf19 1277 else
fab591c5 1278 {
6f85e712 1279 gtk_editable_set_editable( GTK_EDITABLE(m_text), editable );
fab591c5 1280 }
6de97a3b 1281}
c801d85f 1282
68df5777
RR
1283bool wxTextCtrl::Enable( bool enable )
1284{
1285 if (!wxWindowBase::Enable(enable))
1286 {
1287 // nothing to do
7d8268a1 1288 return false;
68df5777 1289 }
f6bcfd97 1290
75812722 1291 if ( IsMultiLine() )
68df5777 1292 {
fab591c5 1293 SetEditable( enable );
68df5777
RR
1294 }
1295 else
1296 {
1297 gtk_widget_set_sensitive( m_text, enable );
1298 }
1299
7d8268a1 1300 return true;
68df5777
RR
1301}
1302
fdca68a6 1303// wxGTK-specific: called recursively by Enable,
47a8a4d5 1304// to give widgets an opportunity to correct their colours after they
fdca68a6 1305// have been changed by Enable
47a8a4d5 1306void wxTextCtrl::OnEnabled( bool enable )
fdca68a6
JS
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
3a9fa0d6
VZ
1323void wxTextCtrl::MarkDirty()
1324{
7d8268a1 1325 m_modified = true;
3a9fa0d6
VZ
1326}
1327
0efe5ba7
VZ
1328void wxTextCtrl::DiscardEdits()
1329{
7d8268a1 1330 m_modified = false;
0efe5ba7
VZ
1331}
1332
ce2f50e3
VZ
1333// ----------------------------------------------------------------------------
1334// max text length support
1335// ----------------------------------------------------------------------------
1336
ce2f50e3
VZ
1337bool wxTextCtrl::IgnoreTextUpdate()
1338{
f6519b40 1339 if ( m_countUpdatesToIgnore > 0 )
ce2f50e3 1340 {
f6519b40 1341 m_countUpdatesToIgnore--;
ce2f50e3 1342
7d8268a1 1343 return true;
ce2f50e3
VZ
1344 }
1345
7d8268a1 1346 return false;
ce2f50e3
VZ
1347}
1348
6964cbba
VZ
1349bool wxTextCtrl::MarkDirtyOnChange()
1350{
1351 if ( m_dontMarkDirty )
1352 {
1353 m_dontMarkDirty = false;
1354
1355 return false;
1356 }
1357
1358 return true;
1359}
1360
d7eee191
VZ
1361void wxTextCtrl::SetMaxLength(unsigned long len)
1362{
1363 if ( !HasFlag(wxTE_MULTILINE) )
1364 {
1365 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
ce2f50e3
VZ
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 {
9fa72bd2
MR
1378 g_signal_connect (m_text, "insert_text",
1379 G_CALLBACK (gtk_insert_text_callback), this);
ce2f50e3
VZ
1380 }
1381 else // no checking
1382 {
9fa72bd2
MR
1383 g_signal_handlers_disconnect_by_func (m_text,
1384 (gpointer) gtk_insert_text_callback, this);
ce2f50e3 1385 }
d7eee191
VZ
1386 }
1387}
1388
debe6624 1389void wxTextCtrl::SetSelection( long from, long to )
c801d85f 1390{
223d09f6 1391 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1392
2b5f62a0
VZ
1393 if (from == -1 && to == -1)
1394 {
1395 from = 0;
8e13c1ec 1396 to = GetValue().length();
2b5f62a0
VZ
1397 }
1398
75812722 1399 if ( IsMultiLine() )
fab591c5 1400 {
739e366a 1401 GtkTextIter fromi, toi;
41b81aed
RR
1402 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1403 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
739e366a 1404
41b81aed
RR
1405 gtk_text_buffer_place_cursor( m_buffer, &toi );
1406 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
fab591c5
RR
1407 }
1408 else
1409 {
1410 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1411 }
6de97a3b 1412}
c801d85f 1413
afbe906a 1414void wxTextCtrl::ShowPosition( long pos )
c801d85f 1415{
5a3ef194 1416 if (IsMultiLine())
afbe906a 1417 {
71aba833 1418 GtkTextIter iter;
5a3ef194
PC
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);
afbe906a 1427 }
6de97a3b 1428}
c801d85f 1429
692c9b86
VZ
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
03f38c58 1456long wxTextCtrl::GetInsertionPoint() const
c801d85f 1457{
223d09f6 1458 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1459
75812722 1460 if ( IsMultiLine() )
fab591c5 1461 {
fab591c5
RR
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.
a8bf1826 1465
41b81aed 1466 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
fab591c5 1467 GtkTextIter cursor;
41b81aed 1468 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
a8bf1826 1469
fab591c5
RR
1470 return gtk_text_iter_get_offset( &cursor );
1471 }
1472 else
fab591c5 1473 {
afa7bd1e 1474 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text));
fab591c5 1475 }
6de97a3b 1476}
c801d85f 1477
7d8268a1 1478wxTextPos wxTextCtrl::GetLastPosition() const
c801d85f 1479{
223d09f6 1480 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1481
2830bf19 1482 int pos = 0;
a8bf1826 1483
75812722 1484 if ( IsMultiLine() )
fab591c5 1485 {
fab591c5 1486 GtkTextIter end;
41b81aed 1487 gtk_text_buffer_get_end_iter( m_buffer, &end );
a8bf1826 1488
fab591c5 1489 pos = gtk_text_iter_get_offset( &end );
fab591c5 1490 }
2830bf19 1491 else
fab591c5 1492 {
2830bf19 1493 pos = GTK_ENTRY(m_text)->text_length;
fab591c5 1494 }
805dd538 1495
ac0d36b5 1496 return (long)pos;
6de97a3b 1497}
c801d85f 1498
debe6624 1499void wxTextCtrl::Remove( long from, long to )
c801d85f 1500{
223d09f6 1501 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1502
75812722 1503 if ( IsMultiLine() )
581ee8a9 1504 {
581ee8a9 1505 GtkTextIter fromi, toi;
41b81aed
RR
1506 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1507 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
581ee8a9 1508
41b81aed 1509 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
581ee8a9
VZ
1510 }
1511 else // single line
68567a96 1512 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 1513}
c801d85f 1514
debe6624 1515void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 1516{
223d09f6 1517 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1518
581ee8a9 1519 Remove( from, to );
bb69661b 1520
7d8268a1 1521 if (!value.empty())
2df7be7f 1522 {
581ee8a9
VZ
1523 SetInsertionPoint( from );
1524 WriteText( value );
2df7be7f 1525 }
6de97a3b 1526}
c801d85f 1527
03f38c58 1528void wxTextCtrl::Cut()
c801d85f 1529{
223d09f6 1530 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1531
75812722 1532 if ( IsMultiLine() )
9fa72bd2 1533 g_signal_emit_by_name (m_text, "cut-clipboard");
bbde2e29 1534 else
afa7bd1e 1535 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text));
6de97a3b 1536}
c801d85f 1537
03f38c58 1538void wxTextCtrl::Copy()
c801d85f 1539{
223d09f6 1540 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1541
75812722 1542 if ( IsMultiLine() )
9fa72bd2 1543 g_signal_emit_by_name (m_text, "copy-clipboard");
bbde2e29 1544 else
afa7bd1e 1545 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text));
6de97a3b 1546}
c801d85f 1547
03f38c58 1548void wxTextCtrl::Paste()
c801d85f 1549{
223d09f6 1550 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1551
75812722 1552 if ( IsMultiLine() )
9fa72bd2 1553 g_signal_emit_by_name (m_text, "paste-clipboard");
bbde2e29 1554 else
afa7bd1e 1555 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text));
6de97a3b 1556}
c801d85f 1557
ca8b28f2
JS
1558// Undo/redo
1559void wxTextCtrl::Undo()
1560{
1561 // TODO
223d09f6 1562 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
1563}
1564
1565void wxTextCtrl::Redo()
1566{
1567 // TODO
223d09f6 1568 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
1569}
1570
1571bool wxTextCtrl::CanUndo() const
1572{
1573 // TODO
4855a477 1574 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
7d8268a1 1575 return false;
ca8b28f2
JS
1576}
1577
1578bool wxTextCtrl::CanRedo() const
1579{
1580 // TODO
4855a477 1581 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
7d8268a1 1582 return false;
ca8b28f2
JS
1583}
1584
1585// If the return values from and to are the same, there is no
1586// selection.
2d4cc5b6 1587void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 1588{
223d09f6 1589 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 1590
5b87f8bf
RD
1591 gint from = -1;
1592 gint to = -1;
7d8268a1 1593 bool haveSelection = false;
5b87f8bf 1594
75812722 1595 if ( IsMultiLine() )
5b87f8bf 1596 {
5b87f8bf 1597 GtkTextIter ifrom, ito;
41b81aed 1598 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
5b87f8bf 1599 {
7d8268a1 1600 haveSelection = true;
5b87f8bf
RD
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 {
7d8268a1 1610 haveSelection = true;
5b87f8bf
RD
1611 }
1612 }
2d4cc5b6 1613
5b87f8bf
RD
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 }
bb69661b 1624
2d4cc5b6
VZ
1625 if ( fromOut )
1626 *fromOut = from;
1627 if ( toOut )
1628 *toOut = to;
ca8b28f2
JS
1629}
1630
5b87f8bf 1631
ca8b28f2
JS
1632bool wxTextCtrl::IsEditable() const
1633{
7d8268a1 1634 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
05060eeb 1635
75812722 1636 if ( IsMultiLine() )
fdd55287
VZ
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 }
ca8b28f2
JS
1644}
1645
0efe5ba7
VZ
1646bool wxTextCtrl::IsModified() const
1647{
1648 return m_modified;
1649}
1650
03f38c58 1651void wxTextCtrl::Clear()
c801d85f 1652{
902725ee 1653 SetValue( wxEmptyString );
6de97a3b 1654}
c801d85f 1655
903f689b 1656void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 1657{
223d09f6 1658 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 1659
75812722 1660 if ( key_event.GetKeyCode() == WXK_RETURN )
da048e3d 1661 {
75812722
VZ
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 }
a8bf1826 1670
75812722
VZ
1671 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1672 // probably is
1673 if ( IsSingleLine() )
da048e3d 1674 {
75812722
VZ
1675 // This will invoke the dialog default action, such
1676 // as the clicking the default button.
2b328fc9 1677
75812722
VZ
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))
2b328fc9 1683 {
75812722
VZ
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 }
2b328fc9 1691 }
13111b2a 1692 }
da048e3d
RR
1693 }
1694
2830bf19 1695 key_event.Skip();
6de97a3b 1696}
c801d85f 1697
03f38c58 1698GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1699{
ae0bdb01 1700 return GTK_WIDGET(m_text);
6de97a3b 1701}
e3e65dac 1702
ef5c70f9 1703GdkWindow *wxTextCtrl::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
903f689b 1704{
75812722 1705 if ( IsMultiLine() )
fab591c5 1706 {
ef5c70f9
VZ
1707 return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
1708 GTK_TEXT_WINDOW_TEXT );
fab591c5 1709 }
ae0bdb01 1710 else
fab591c5 1711 {
ef5c70f9 1712 return GTK_ENTRY(m_text)->text_area;
fab591c5 1713 }
903f689b 1714}
e3e65dac 1715
bb69661b
VZ
1716// the font will change for subsequent text insertiongs
1717bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1718{
7d8268a1 1719 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
8bbe427f 1720
a66954a6 1721 if ( !wxTextCtrlBase::SetFont(font) )
bb69661b
VZ
1722 {
1723 // font didn't change, nothing to do
7d8268a1 1724 return false;
bb69661b
VZ
1725 }
1726
75812722 1727 if ( IsMultiLine() )
bb69661b 1728 {
7d8268a1 1729 SetUpdateFont(true);
bb69661b 1730
1ff4714d
VZ
1731 m_defaultStyle.SetFont(font);
1732
01041145 1733 ChangeFontGlobally();
bb69661b
VZ
1734 }
1735
7d8268a1 1736 return true;
58614078
RR
1737}
1738
01041145
VZ
1739void wxTextCtrl::ChangeFontGlobally()
1740{
1741 // this method is very inefficient and hence should be called as rarely as
1742 // possible!
c04ec496
VZ
1743 //
1744 // TODO: it can be implemented much more efficiently for GTK2
75812722 1745 wxASSERT_MSG( IsMultiLine(),
22800f32 1746 _T("shouldn't be called for single line controls") );
01041145
VZ
1747
1748 wxString value = GetValue();
7d8268a1 1749 if ( !value.empty() )
01041145 1750 {
7d8268a1 1751 SetUpdateFont(false);
572aeb77 1752
01041145
VZ
1753 Clear();
1754 AppendText(value);
01041145
VZ
1755 }
1756}
1757
17665a2b
VZ
1758bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1759{
1760 if ( !wxControl::SetForegroundColour(colour) )
7d8268a1 1761 return false;
17665a2b
VZ
1762
1763 // update default fg colour too
1764 m_defaultStyle.SetTextColour(colour);
1765
7d8268a1 1766 return true;
17665a2b
VZ
1767}
1768
f03fc89f 1769bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1770{
7d8268a1 1771 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
a81258be 1772
1f477433 1773 if ( !wxControl::SetBackgroundColour( colour ) )
7d8268a1 1774 return false;
3358d36e 1775
f03fc89f 1776 if (!m_backgroundColour.Ok())
7d8268a1 1777 return false;
8bbe427f 1778
17665a2b
VZ
1779 // change active background color too
1780 m_defaultStyle.SetBackgroundColour( colour );
1781
7d8268a1 1782 return true;
58614078
RR
1783}
1784
eda40bfc 1785bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
17665a2b 1786{
75812722 1787 if ( IsMultiLine() )
17665a2b
VZ
1788 {
1789 if ( style.IsDefault() )
1790 {
1791 // nothing to do
7d8268a1 1792 return true;
17665a2b 1793 }
902725ee 1794
41b81aed 1795 gint l = gtk_text_buffer_get_char_count( m_buffer );
17665a2b 1796
7d8268a1 1797 wxCHECK_MSG( start >= 0 && end <= l, false,
cc3da3f8
RR
1798 _T("invalid range in wxTextCtrl::SetStyle") );
1799
1800 GtkTextIter starti, endi;
41b81aed
RR
1801 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1802 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
cc3da3f8 1803
7b377ed9 1804 wxGtkTextApplyTagsFromAttr( m_widget, m_buffer, style, &starti, &endi );
902725ee 1805
7d8268a1 1806 return true;
17665a2b 1807 }
7b377ed9 1808 //else: single line text controls don't support styles
902725ee 1809
902725ee 1810 return false;
17665a2b
VZ
1811}
1812
f40fdaa3 1813void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
58614078 1814{
f40fdaa3 1815 gtk_widget_modify_style(m_text, style);
68dda785 1816}
f96aa4d9 1817
e702ff0f
JS
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}
65045edd 1867
f68586e5
VZ
1868wxSize wxTextCtrl::DoGetBestSize() const
1869{
1870 // FIXME should be different for multi-line controls...
0279e844 1871 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
1872 wxSize best(80, ret.y);
1873 CacheBestSize(best);
1874 return best;
f68586e5 1875}
0cc7251e 1876
9cd6d737
VZ
1877// ----------------------------------------------------------------------------
1878// freeze/thaw
1879// ----------------------------------------------------------------------------
1880
0cc7251e
VZ
1881void wxTextCtrl::Freeze()
1882{
442a960c
PC
1883 wxCHECK_RET(m_text != NULL, wxT("invalid text ctrl"));
1884
0cc7251e
VZ
1885 if ( HasFlag(wxTE_MULTILINE) )
1886 {
5a3ef194 1887 if (m_freezeCount++ == 0)
41b81aed
RR
1888 {
1889 // freeze textview updates and remove buffer
9fa72bd2
MR
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);
41b81aed
RR
1894 gtk_widget_set_sensitive(m_widget, false);
1895 g_object_ref(m_buffer);
45388803 1896 GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL);
442a960c 1897 GtkTextMark* mark = GTK_TEXT_VIEW(m_text)->first_para_mark;
45388803 1898 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new);
442a960c
PC
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);
0a164d4c 1907 }
41b81aed 1908 }
0cc7251e
VZ
1909}
1910
1911void wxTextCtrl::Thaw()
1912{
1913 if ( HasFlag(wxTE_MULTILINE) )
1914 {
5a3ef194 1915 wxCHECK_RET(m_freezeCount != 0, _T("Thaw() without matching Freeze()"));
41b81aed 1916
5a3ef194 1917 if (--m_freezeCount == 0)
41b81aed
RR
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);
9fa72bd2
MR
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);
5a3ef194
PC
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 }
41b81aed 1933 }
41b81aed 1934 }
0cc7251e 1935}
9cd6d737 1936
9440c3d0
KH
1937// ----------------------------------------------------------------------------
1938// wxTextUrlEvent passing if style & wxTE_AUTO_URL
1939// ----------------------------------------------------------------------------
1940
9440c3d0
KH
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();
75812722 1947 if( !HasFlag(wxTE_AUTO_URL) )
9440c3d0
KH
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}
9440c3d0 1989
8312c461
VZ
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
9d522606
RD
2002// static
2003wxVisualAttributes
2004wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2005{
2006 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
2007}