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