]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textctrl.cpp
'mark' is already used in this scope so make it clearer.
[wxWidgets.git] / src / gtk / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "textctrl.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/textctrl.h"
18 #include "wx/utils.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #include "wx/settings.h"
22 #include "wx/panel.h"
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 #include "wx/math.h"
30
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
33
34 //-----------------------------------------------------------------------------
35 // idle system
36 //-----------------------------------------------------------------------------
37
38 extern void wxapp_install_idle_handler();
39 extern bool g_isIdle;
40
41 //-----------------------------------------------------------------------------
42 // data
43 //-----------------------------------------------------------------------------
44
45 extern wxCursor g_globalCursor;
46 extern wxWindowGTK *g_delayedFocus;
47
48 // ----------------------------------------------------------------------------
49 // helpers
50 // ----------------------------------------------------------------------------
51
52 #ifdef __WXGTK20__
53 extern "C" {
54 static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
55 GtkTextTag *tag,
56 GtkTextIter *start,
57 GtkTextIter *end,
58 char *prefix)
59 {
60 gchar *name;
61 g_object_get (tag, "name", &name, NULL);
62
63 if (!name || strncmp(name, prefix, strlen(prefix)))
64 // anonymous tag or not starting with prefix - don't remove
65 g_signal_stop_emission_by_name(buffer, "remove_tag");
66
67 g_free(name);
68 }
69 }
70
71 extern "C" {
72 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
73 const wxTextAttr& attr,
74 GtkTextIter *start,
75 GtkTextIter *end)
76 {
77 static gchar buf[1024];
78 GtkTextTag *tag;
79
80 gulong remove_handler_id = g_signal_connect( text_buffer, "remove_tag",
81 G_CALLBACK(wxGtkOnRemoveTag), gpointer("WX"));
82 gtk_text_buffer_remove_all_tags(text_buffer, start, end);
83 g_signal_handler_disconnect( text_buffer, remove_handler_id );
84
85 if (attr.HasFont())
86 {
87 char *font_string;
88 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
89 font_string = pango_font_description_to_string(font_description);
90 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
91 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
92 buf );
93 if (!tag)
94 tag = gtk_text_buffer_create_tag( text_buffer, buf,
95 "font-desc", font_description,
96 NULL );
97 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
98 g_free (font_string);
99 }
100
101 if (attr.HasTextColour())
102 {
103 GdkColor *colFg = attr.GetTextColour().GetColor();
104 g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
105 colFg->red, colFg->green, colFg->blue);
106 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
107 buf );
108 if (!tag)
109 tag = gtk_text_buffer_create_tag( text_buffer, buf,
110 "foreground-gdk", colFg, NULL );
111 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
112 }
113
114 if (attr.HasBackgroundColour())
115 {
116 GdkColor *colBg = attr.GetBackgroundColour().GetColor();
117 g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
118 colBg->red, colBg->green, colBg->blue);
119 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
120 buf );
121 if (!tag)
122 tag = gtk_text_buffer_create_tag( text_buffer, buf,
123 "background-gdk", colBg, NULL );
124 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
125 }
126
127 if (attr.HasAlignment())
128 {
129 GtkTextIter para_start, para_end = *end;
130 gtk_text_buffer_get_iter_at_line( text_buffer,
131 &para_start,
132 gtk_text_iter_get_line(start) );
133 gtk_text_iter_forward_line(&para_end);
134
135 remove_handler_id = g_signal_connect( text_buffer, "remove_tag",
136 G_CALLBACK(wxGtkOnRemoveTag),
137 gpointer("WXALIGNMENT"));
138 gtk_text_buffer_remove_all_tags( text_buffer, &para_start, &para_end );
139 g_signal_handler_disconnect( text_buffer, remove_handler_id );
140
141 GtkJustification align;
142 switch (attr.GetAlignment())
143 {
144 default:
145 align = GTK_JUSTIFY_LEFT;
146 break;
147 case wxTEXT_ALIGNMENT_RIGHT:
148 align = GTK_JUSTIFY_RIGHT;
149 break;
150 case wxTEXT_ALIGNMENT_CENTER:
151 align = GTK_JUSTIFY_CENTER;
152 break;
153 // gtk+ doesn't support justify as of gtk+-2.7.4
154 }
155
156 g_snprintf(buf, sizeof(buf), "WXALIGNMENT %d", align);
157 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
158 buf );
159 if (!tag)
160 tag = gtk_text_buffer_create_tag( text_buffer, buf,
161 "justification", align, NULL );
162 gtk_text_buffer_apply_tag( text_buffer, tag, &para_start, &para_end );
163 }
164 }
165 }
166
167 extern "C" {
168 static void wxGtkTextInsert(GtkWidget *text,
169 GtkTextBuffer *text_buffer,
170 const wxTextAttr& attr,
171 wxCharBuffer buffer)
172
173 {
174 gint start_offset;
175 GtkTextIter iter, start;
176
177 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
178 gtk_text_buffer_get_insert (text_buffer) );
179 start_offset = gtk_text_iter_get_offset (&iter);
180 gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) );
181
182 gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
183
184 wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
185 }
186 }
187 #else
188 extern "C" {
189 static void wxGtkTextInsert(GtkWidget *text,
190 const wxTextAttr& attr,
191 const char *txt,
192 size_t len)
193 {
194 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
195 : NULL;
196
197 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
198 : NULL;
199
200 GdkColor *colBg = attr.HasBackgroundColour()
201 ? attr.GetBackgroundColour().GetColor()
202 : NULL;
203
204 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
205 }
206 }
207 #endif // GTK 1.x
208
209 // ----------------------------------------------------------------------------
210 // "insert_text" for GtkEntry
211 // ----------------------------------------------------------------------------
212
213 extern "C" {
214 static void
215 gtk_insert_text_callback(GtkEditable *editable,
216 const gchar *new_text,
217 gint new_text_length,
218 gint *position,
219 wxTextCtrl *win)
220 {
221 if (g_isIdle)
222 wxapp_install_idle_handler();
223
224 // we should only be called if we have a max len limit at all
225 GtkEntry *entry = GTK_ENTRY (editable);
226
227 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
228
229 // check that we don't overflow the max length limit
230 //
231 // FIXME: this doesn't work when we paste a string which is going to be
232 // truncated
233 if ( entry->text_length == entry->text_max_length )
234 {
235 // we don't need to run the base class version at all
236 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
237
238 // remember that the next changed signal is to be ignored to avoid
239 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
240 win->IgnoreNextTextUpdate();
241
242 // and generate the correct one ourselves
243 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
244 event.SetEventObject(win);
245 event.SetString(win->GetValue());
246 win->GetEventHandler()->ProcessEvent( event );
247 }
248 }
249 }
250
251 #ifdef __WXGTK20__
252 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
253
254 extern "C" {
255 static void
256 au_apply_tag_callback(GtkTextBuffer *buffer,
257 GtkTextTag *tag,
258 GtkTextIter *start,
259 GtkTextIter *end,
260 gpointer textctrl)
261 {
262 if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"))
263 g_signal_stop_emission_by_name(buffer, "apply_tag");
264 }
265 }
266
267 //-----------------------------------------------------------------------------
268 // GtkTextCharPredicates for gtk_text_iter_*_find_char
269 //-----------------------------------------------------------------------------
270
271 extern "C" {
272 static gboolean
273 pred_whitespace (gunichar ch, gpointer user_data)
274 {
275 return g_unichar_isspace(ch);
276 }
277 }
278
279 extern "C" {
280 static gboolean
281 pred_non_whitespace (gunichar ch, gpointer user_data)
282 {
283 return !g_unichar_isspace(ch);
284 }
285 }
286
287 extern "C" {
288 static gboolean
289 pred_nonpunct (gunichar ch, gpointer user_data)
290 {
291 return !g_unichar_ispunct(ch);
292 }
293 }
294
295 extern "C" {
296 static gboolean
297 pred_nonpunct_or_slash (gunichar ch, gpointer user_data)
298 {
299 return !g_unichar_ispunct(ch) || ch == '/';
300 }
301 }
302
303 //-----------------------------------------------------------------------------
304 // Check for links between s and e and correct tags as necessary
305 //-----------------------------------------------------------------------------
306
307 // This function should be made match better while being efficient at one point.
308 // Most probably with a row of regular expressions.
309 extern "C" {
310 static void
311 au_check_word( GtkTextIter *s, GtkTextIter *e )
312 {
313 static const char *URIPrefixes[] =
314 {
315 "http://",
316 "ftp://",
317 "www.",
318 "ftp.",
319 "mailto://",
320 "https://",
321 "file://",
322 "nntp://",
323 "news://",
324 "telnet://",
325 "mms://",
326 "gopher://",
327 "prospero://",
328 "wais://",
329 };
330
331 GtkTextIter start = *s, end = *e;
332 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
333
334 // Get our special link tag
335 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
336
337 // Get rid of punctuation from beginning and end.
338 // Might want to move this to au_check_range if an improved link checking doesn't
339 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
340 if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) )
341 gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e );
342
343 gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start );
344 gtk_text_iter_forward_char(&end);
345
346 gchar* text = gtk_text_iter_get_text( &start, &end );
347 size_t len = strlen(text), prefix_len;
348 size_t n;
349
350 for( n = 0; n < WXSIZEOF(URIPrefixes); ++n )
351 {
352 prefix_len = strlen(URIPrefixes[n]);
353 if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len))
354 break;
355 }
356
357 if(n < WXSIZEOF(URIPrefixes))
358 {
359 gulong signal_id = g_signal_handler_find(buffer,
360 (GSignalMatchType) (G_SIGNAL_MATCH_FUNC),
361 0, 0, NULL,
362 (gpointer)au_apply_tag_callback, NULL);
363
364 g_signal_handler_block(buffer, signal_id);
365 gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
366 g_signal_handler_unblock(buffer, signal_id);
367 }
368 }
369 }
370
371 extern "C" {
372 static void
373 au_check_range(GtkTextIter *s,
374 GtkTextIter *range_end)
375 {
376 GtkTextIter range_start = *s;
377 GtkTextIter word_end;
378 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
379 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
380
381 gtk_text_buffer_remove_tag(buffer, tag, s, range_end);
382
383 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start)))
384 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
385
386 while(!gtk_text_iter_equal(&range_start, range_end))
387 {
388 word_end = range_start;
389 gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end);
390
391 // Now we should have a word delimited by range_start and word_end, correct link tags
392 au_check_word(&range_start, &word_end);
393
394 range_start = word_end;
395 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
396 }
397 }
398 }
399
400 //-----------------------------------------------------------------------------
401 // "insert-text" for GtkTextBuffer
402 //-----------------------------------------------------------------------------
403
404 extern "C" {
405 static void
406 au_insert_text_callback(GtkTextBuffer *buffer,
407 GtkTextIter *end,
408 gchar *text,
409 gint len,
410 wxTextCtrl *win)
411 {
412 if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
413 return;
414
415 GtkTextIter start = *end;
416 gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len));
417
418 GtkTextIter line_start = start;
419 GtkTextIter line_end = *end;
420 GtkTextIter words_start = start;
421 GtkTextIter words_end = *end;
422
423 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start));
424 gtk_text_iter_forward_to_line_end(&line_end);
425 gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start);
426 gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end);
427
428 au_check_range(&words_start, &words_end);
429 }
430 }
431
432 //-----------------------------------------------------------------------------
433 // "delete-range" for GtkTextBuffer
434 //-----------------------------------------------------------------------------
435
436 extern "C" {
437 static void
438 au_delete_range_callback(GtkTextBuffer *buffer,
439 GtkTextIter *start,
440 GtkTextIter *end,
441 wxTextCtrl *win)
442 {
443 if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
444 return;
445
446 GtkTextIter line_start = *start, line_end = *end;
447
448 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start));
449 gtk_text_iter_forward_to_line_end(&line_end);
450 gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start);
451 gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end);
452
453 au_check_range(start, end);
454 }
455 }
456
457
458 #endif
459
460 //-----------------------------------------------------------------------------
461 // "changed"
462 //-----------------------------------------------------------------------------
463
464 extern "C" {
465 static void
466 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
467 {
468 if ( win->IgnoreTextUpdate() )
469 return;
470
471 if (!win->m_hasVMT) return;
472
473 if (g_isIdle)
474 wxapp_install_idle_handler();
475
476 win->SetModified();
477 #ifndef __WXGTK20__
478 win->UpdateFontIfNeeded();
479 #endif // !__WXGTK20__
480
481 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
482 event.SetEventObject( win );
483 win->GetEventHandler()->ProcessEvent( event );
484 }
485 }
486
487 //-----------------------------------------------------------------------------
488 // "expose_event" from scrolled window and textview
489 //-----------------------------------------------------------------------------
490
491 #ifdef __WXGTK20__
492 extern "C" {
493 static gboolean
494 gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
495 {
496 return TRUE;
497 }
498 }
499 #endif
500
501 //-----------------------------------------------------------------------------
502 // "changed" from vertical scrollbar
503 //-----------------------------------------------------------------------------
504
505 #ifndef __WXGTK20__
506 extern "C" {
507 static void
508 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
509 {
510 if (!win->m_hasVMT) return;
511
512 if (g_isIdle)
513 wxapp_install_idle_handler();
514
515 win->CalculateScrollbar();
516 }
517 }
518 #endif
519
520 // ----------------------------------------------------------------------------
521 // redraw callback for multiline text
522 // ----------------------------------------------------------------------------
523
524 #ifndef __WXGTK20__
525
526 // redrawing a GtkText from inside a wxYield() call results in crashes (the
527 // text sample shows it in its "Add lines" command which shows wxProgressDialog
528 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
529 // don't do anything if we're inside wxYield()
530
531 extern bool wxIsInsideYield;
532
533 extern "C" {
534 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
535 }
536
537 static GtkDrawCallback gs_gtk_text_draw = NULL;
538
539 extern "C" {
540 static void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
541 {
542 if ( !wxIsInsideYield )
543 {
544 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
545 _T("infinite recursion in wxgtk_text_draw aborted") );
546
547 gs_gtk_text_draw(widget, rect);
548 }
549 }
550 }
551
552 #endif // __WXGTK20__
553
554 //-----------------------------------------------------------------------------
555 // wxTextCtrl
556 //-----------------------------------------------------------------------------
557
558 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
559
560 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
561 EVT_CHAR(wxTextCtrl::OnChar)
562
563 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
564 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
565 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
566 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
567 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
568
569 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
570 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
571 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
572 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
573 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
574
575 #ifdef __WXGTK20__
576 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
577 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
578 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent)
579 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent)
580 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent)
581 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent)
582 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent)
583 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent)
584 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent)
585 #endif
586 END_EVENT_TABLE()
587
588 void wxTextCtrl::Init()
589 {
590 m_ignoreNextUpdate =
591 m_modified = false;
592 SetUpdateFont(false);
593 m_text =
594 m_vScrollbar = (GtkWidget *)NULL;
595 #ifdef __WXGTK20__
596 m_frozenness = 0;
597 m_gdkHandCursor = NULL;
598 m_gdkXTermCursor = NULL;
599 #endif
600 }
601
602 wxTextCtrl::~wxTextCtrl()
603 {
604 #ifdef __WXGTK20__
605 if(m_gdkHandCursor)
606 gdk_cursor_unref(m_gdkHandCursor);
607 if(m_gdkXTermCursor)
608 gdk_cursor_unref(m_gdkXTermCursor);
609 #endif
610 }
611
612 wxTextCtrl::wxTextCtrl( wxWindow *parent,
613 wxWindowID id,
614 const wxString &value,
615 const wxPoint &pos,
616 const wxSize &size,
617 long style,
618 const wxValidator& validator,
619 const wxString &name )
620 {
621 Init();
622
623 Create( parent, id, value, pos, size, style, validator, name );
624 }
625
626 bool wxTextCtrl::Create( wxWindow *parent,
627 wxWindowID id,
628 const wxString &value,
629 const wxPoint &pos,
630 const wxSize &size,
631 long style,
632 const wxValidator& validator,
633 const wxString &name )
634 {
635 m_needParent = true;
636 m_acceptsFocus = true;
637
638 if (!PreCreation( parent, pos, size ) ||
639 !CreateBase( parent, id, pos, size, style, validator, name ))
640 {
641 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
642 return false;
643 }
644
645
646 m_vScrollbarVisible = false;
647
648 bool multi_line = (style & wxTE_MULTILINE) != 0;
649
650 if (multi_line)
651 {
652 #ifdef __WXGTK20__
653 // Create view
654 m_text = gtk_text_view_new();
655
656 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
657
658 // create scrolled window
659 m_widget = gtk_scrolled_window_new( NULL, NULL );
660 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
661 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
662
663 // Insert view into scrolled window
664 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
665
666 // translate wx wrapping style to GTK+
667 GtkWrapMode wrap;
668 if ( HasFlag( wxTE_DONTWRAP ) )
669 wrap = GTK_WRAP_NONE;
670 else if ( HasFlag( wxTE_CHARWRAP ) )
671 wrap = GTK_WRAP_CHAR;
672 else if ( HasFlag( wxTE_WORDWRAP ) )
673 wrap = GTK_WRAP_WORD;
674 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
675 {
676 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
677 #ifdef __WXGTK24__
678 if ( !gtk_check_version(2,4,0) )
679 {
680 wrap = GTK_WRAP_WORD_CHAR;
681 }
682 else
683 #endif
684 wrap = GTK_WRAP_WORD;
685 }
686
687 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
688
689 if (!HasFlag(wxNO_BORDER))
690 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
691
692 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
693
694 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
695 #else // GTK+ 1
696 // create our control ...
697 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
698
699 // ... and put into the upper left hand corner of the table
700 bool bHasHScrollbar = false;
701 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
702 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
703 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
704 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
705 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
706 0, 0);
707
708 // always wrap words
709 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
710
711 // finally, put the vertical scrollbar in the upper right corner
712 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
713 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
714 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
715 GTK_FILL,
716 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
717 0, 0);
718 #endif // GTK+ 2/1
719 }
720 else
721 {
722 // a single-line text control: no need for scrollbars
723 m_widget =
724 m_text = gtk_entry_new();
725
726 #ifdef __WXGTK20__
727 if (style & wxNO_BORDER)
728 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
729 #endif
730 }
731
732 m_parent->DoAddChild( this );
733
734 m_focusWidget = m_text;
735
736 PostCreation(size);
737
738 if (multi_line)
739 gtk_widget_show(m_text);
740
741 #ifndef __WXGTK20__
742 if (multi_line)
743 {
744 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
745 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
746
747 // only initialize gs_gtk_text_draw once, starting from the next the
748 // klass::draw will already be wxgtk_text_draw
749 if ( !gs_gtk_text_draw )
750 {
751 GtkDrawCallback&
752 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
753
754 gs_gtk_text_draw = draw;
755
756 draw = wxgtk_text_draw;
757 }
758 }
759 #endif // GTK+ 1.x
760
761 if (!value.empty())
762 {
763 #ifdef __WXGTK20__
764 SetValue( value );
765 #else
766
767 #if !GTK_CHECK_VERSION(1, 2, 0)
768 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
769 // gtk_editable_insert_text()
770 gtk_widget_realize(m_text);
771 #endif // GTK 1.0
772
773 gint tmp = 0;
774 #if wxUSE_UNICODE
775 wxWX2MBbuf val = value.mbc_str();
776 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
777 #else
778 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
779 #endif
780
781 if (multi_line)
782 {
783 // Bring editable's cursor uptodate. Bug in GTK.
784 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
785 }
786
787 #endif
788 }
789
790 if (style & wxTE_PASSWORD)
791 {
792 if (!multi_line)
793 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
794 }
795
796 if (style & wxTE_READONLY)
797 {
798 if (!multi_line)
799 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
800 #ifdef __WXGTK20__
801 else
802 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
803 #else
804 }
805 else
806 {
807 if (multi_line)
808 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
809 #endif
810 }
811
812 #ifdef __WXGTK20__
813 if (multi_line)
814 {
815 if (style & wxTE_RIGHT)
816 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
817 else if (style & wxTE_CENTRE)
818 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
819 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
820 }
821 else
822 {
823 #ifdef __WXGTK24__
824 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
825 if (!gtk_check_version(2,4,0))
826 {
827 if (style & wxTE_RIGHT)
828 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
829 else if (style & wxTE_CENTRE)
830 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
831 }
832 #endif
833 }
834 #endif // __WXGTK20__
835
836 // We want to be notified about text changes.
837 #ifdef __WXGTK20__
838 if (multi_line)
839 {
840 g_signal_connect( G_OBJECT(m_buffer), "changed",
841 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
842
843 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
844 if (style & wxTE_AUTO_URL)
845 {
846 GtkTextIter start, end;
847 m_gdkHandCursor = gdk_cursor_new(GDK_HAND2);
848 m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM);
849
850 // We create our wxUrl tag here for slight efficiency gain - we
851 // don't have to check for the tag existance in callbacks,
852 // hereby it's guaranteed to exist.
853 gtk_text_buffer_create_tag(m_buffer, "wxUrl",
854 "foreground", "blue",
855 "underline", PANGO_UNDERLINE_SINGLE,
856 NULL);
857
858 // Check for URLs after each text change
859 g_signal_connect_after( G_OBJECT(m_buffer), "insert_text",
860 GTK_SIGNAL_FUNC(au_insert_text_callback), (gpointer)this);
861 g_signal_connect_after( G_OBJECT(m_buffer), "delete_range",
862 GTK_SIGNAL_FUNC(au_delete_range_callback), (gpointer)this);
863
864 // Block all wxUrl tag applying unless we do it ourselves, in which case we
865 // block this callback temporarily. This takes care of gtk+ internal
866 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
867 // which is undesired because only a part of the URL might be copied.
868 // The insert-text signal emitted inside it will take care of newly formed
869 // or wholly copied URLs.
870 g_signal_connect( G_OBJECT(m_buffer), "apply_tag",
871 GTK_SIGNAL_FUNC(au_apply_tag_callback), NULL);
872
873 // Check for URLs in the initial string passed to Create
874 gtk_text_buffer_get_start_iter(m_buffer, &start);
875 gtk_text_buffer_get_end_iter(m_buffer, &end);
876 au_check_range(&start, &end);
877 }
878 }
879 else
880 #endif
881
882 {
883 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
884 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
885 }
886
887 m_cursor = wxCursor( wxCURSOR_IBEAM );
888
889 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
890 SetDefaultStyle( attrDef );
891
892 return true;
893 }
894
895
896 void wxTextCtrl::CalculateScrollbar()
897 {
898 #ifndef __WXGTK20__
899 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
900
901 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
902
903 if (adj->upper - adj->page_size < 0.8)
904 {
905 if (m_vScrollbarVisible)
906 {
907 gtk_widget_hide( m_vScrollbar );
908 m_vScrollbarVisible = false;
909 }
910 }
911 else
912 {
913 if (!m_vScrollbarVisible)
914 {
915 gtk_widget_show( m_vScrollbar );
916 m_vScrollbarVisible = true;
917 }
918 }
919 #endif
920 }
921
922 wxString wxTextCtrl::GetValue() const
923 {
924 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
925
926 wxString tmp;
927 if (m_windowStyle & wxTE_MULTILINE)
928 {
929 #ifdef __WXGTK20__
930 GtkTextIter start;
931 gtk_text_buffer_get_start_iter( m_buffer, &start );
932 GtkTextIter end;
933 gtk_text_buffer_get_end_iter( m_buffer, &end );
934 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
935
936 #if wxUSE_UNICODE
937 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
938 #else
939 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
940 #endif
941 if ( buffer )
942 tmp = buffer;
943
944 g_free( text );
945 #else
946 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
947 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
948 tmp = text;
949 g_free( text );
950 #endif
951 }
952 else
953 {
954 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
955 }
956
957 return tmp;
958 }
959
960 void wxTextCtrl::SetValue( const wxString &value )
961 {
962 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
963
964 if (m_windowStyle & wxTE_MULTILINE)
965 {
966 #ifdef __WXGTK20__
967
968 #if wxUSE_UNICODE
969 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
970 #else
971 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
972 #endif
973 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
974 IgnoreNextTextUpdate();
975
976 if ( !buffer )
977 {
978 // what else can we do? at least don't crash...
979 return;
980 }
981
982 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
983
984 #else
985 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
986 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
987 len = 0;
988 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
989 #endif
990 }
991 else
992 {
993 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
994 }
995
996 // GRG, Jun/2000: Changed this after a lot of discussion in
997 // the lists. wxWidgets 2.2 will have a set of flags to
998 // customize this behaviour.
999 SetInsertionPoint(0);
1000
1001 m_modified = false;
1002 }
1003
1004 void wxTextCtrl::WriteText( const wxString &text )
1005 {
1006 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1007
1008 if ( text.empty() )
1009 return;
1010
1011 // gtk_text_changed_callback() will set m_modified to true but m_modified
1012 // shouldn't be changed by the program writing to the text control itself,
1013 // so save the old value and restore when we're done
1014 bool oldModified = m_modified;
1015
1016 if ( m_windowStyle & wxTE_MULTILINE )
1017 {
1018 #ifdef __WXGTK20__
1019
1020 #if wxUSE_UNICODE
1021 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
1022 #else
1023 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
1024 #endif
1025 if ( !buffer )
1026 {
1027 // what else can we do? at least don't crash...
1028 return;
1029 }
1030
1031 // TODO: Call whatever is needed to delete the selection.
1032 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
1033
1034 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
1035 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1036 if ( adj->value == adj->upper - adj->page_size )
1037 {
1038 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
1039 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
1040 }
1041 #else // GTK 1.x
1042 // After cursor movements, gtk_text_get_point() is wrong by one.
1043 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
1044
1045 // always use m_defaultStyle, even if it is empty as otherwise
1046 // resetting the style and appending some more text wouldn't work: if
1047 // we don't specify the style explicitly, the old style would be used
1048 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
1049 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
1050
1051 // we called wxGtkTextInsert with correct font, no need to do anything
1052 // in UpdateFontIfNeeded() any longer
1053 if ( !text.empty() )
1054 {
1055 SetUpdateFont(false);
1056 }
1057
1058 // Bring editable's cursor back uptodate.
1059 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
1060 #endif // GTK 1.x/2.0
1061 }
1062 else // single line
1063 {
1064 // First remove the selection if there is one
1065 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
1066
1067 // This moves the cursor pos to behind the inserted text.
1068 gint len = GET_EDITABLE_POS(m_text);
1069
1070 #ifdef __WXGTK20__
1071
1072 #if wxUSE_UNICODE
1073 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
1074 #else
1075 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
1076 #endif
1077 if ( !buffer )
1078 {
1079 // what else can we do? at least don't crash...
1080 return;
1081 }
1082
1083 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
1084
1085 #else
1086 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
1087 #endif
1088
1089 // Bring entry's cursor uptodate.
1090 gtk_entry_set_position( GTK_ENTRY(m_text), len );
1091 }
1092
1093 m_modified = oldModified;
1094 }
1095
1096 void wxTextCtrl::AppendText( const wxString &text )
1097 {
1098 SetInsertionPointEnd();
1099 WriteText( text );
1100 }
1101
1102 wxString wxTextCtrl::GetLineText( long lineNo ) const
1103 {
1104 if (m_windowStyle & wxTE_MULTILINE)
1105 {
1106 #ifndef __WXGTK20__
1107 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1108 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
1109
1110 if (text)
1111 {
1112 wxString buf;
1113 long i;
1114 int currentLine = 0;
1115 for (i = 0; currentLine != lineNo && text[i]; i++ )
1116 if (text[i] == '\n')
1117 currentLine++;
1118 // Now get the text
1119 int j;
1120 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
1121 buf += text[i];
1122
1123 g_free( text );
1124 return buf;
1125 }
1126 else
1127 {
1128 return wxEmptyString;
1129 }
1130 #else
1131 GtkTextIter line;
1132 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
1133 GtkTextIter end = line;
1134 gtk_text_iter_forward_to_line_end(&end);
1135 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
1136 wxString result(wxGTK_CONV_BACK(text));
1137 g_free(text);
1138 return result;
1139 #endif
1140 }
1141 else
1142 {
1143 if (lineNo == 0) return GetValue();
1144 return wxEmptyString;
1145 }
1146 }
1147
1148 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
1149 {
1150 /* If you implement this, don't forget to update the documentation!
1151 * (file docs/latex/wx/text.tex) */
1152 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1153 }
1154
1155 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
1156 {
1157 if ( m_windowStyle & wxTE_MULTILINE )
1158 {
1159 #ifdef __WXGTK20__
1160 GtkTextIter iter;
1161 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
1162 if (gtk_text_iter_is_end(&iter))
1163 return false;
1164
1165 *y = gtk_text_iter_get_line(&iter);
1166 *x = gtk_text_iter_get_line_offset(&iter);
1167 #else
1168 wxString text = GetValue();
1169
1170 // cast to prevent warning. But pos really should've been unsigned.
1171 if( (unsigned long)pos > text.Len() )
1172 return false;
1173
1174 *x=0; // First Col
1175 *y=0; // First Line
1176
1177 const wxChar* stop = text.c_str() + pos;
1178 for ( const wxChar *p = text.c_str(); p < stop; p++ )
1179 {
1180 if (*p == wxT('\n'))
1181 {
1182 (*y)++;
1183 *x=0;
1184 }
1185 else
1186 (*x)++;
1187 }
1188 #endif
1189 }
1190 else // single line control
1191 {
1192 if ( pos <= GTK_ENTRY(m_text)->text_length )
1193 {
1194 *y = 0;
1195 *x = pos;
1196 }
1197 else
1198 {
1199 // index out of bounds
1200 return false;
1201 }
1202 }
1203
1204 return true;
1205 }
1206
1207 long wxTextCtrl::XYToPosition(long x, long y ) const
1208 {
1209 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
1210
1211 #ifdef __WXGTK20__
1212 GtkTextIter iter;
1213 if (y >= gtk_text_buffer_get_line_count (m_buffer))
1214 return -1;
1215
1216 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
1217 if (x >= gtk_text_iter_get_chars_in_line (&iter))
1218 return -1;
1219
1220 return gtk_text_iter_get_offset(&iter) + x;
1221 #else
1222 long pos=0;
1223 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
1224
1225 pos += x;
1226 return pos;
1227 #endif
1228 }
1229
1230 int wxTextCtrl::GetLineLength(long lineNo) const
1231 {
1232 #ifdef __WXGTK20__
1233 if (m_windowStyle & wxTE_MULTILINE)
1234 {
1235 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
1236 if (lineNo > last_line)
1237 return -1;
1238
1239 GtkTextIter iter;
1240 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
1241 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1242 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
1243 }
1244 else
1245 #endif
1246 {
1247 wxString str = GetLineText (lineNo);
1248 return (int) str.Length();
1249 }
1250 }
1251
1252 int wxTextCtrl::GetNumberOfLines() const
1253 {
1254 if (m_windowStyle & wxTE_MULTILINE)
1255 {
1256 #ifdef __WXGTK20__
1257 return gtk_text_buffer_get_line_count( m_buffer );
1258 #else
1259 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1260 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
1261
1262 if (text)
1263 {
1264 int currentLine = 0;
1265 for (int i = 0; i < len; i++ )
1266 {
1267 if (text[i] == '\n')
1268 currentLine++;
1269 }
1270 g_free( text );
1271
1272 // currentLine is 0 based, add 1 to get number of lines
1273 return currentLine + 1;
1274 }
1275 else
1276 {
1277 return 0;
1278 }
1279 #endif
1280 }
1281 else
1282 {
1283 return 1;
1284 }
1285 }
1286
1287 void wxTextCtrl::SetInsertionPoint( long pos )
1288 {
1289 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1290
1291 if ( IsMultiLine() )
1292 {
1293 #ifdef __WXGTK20__
1294 GtkTextIter iter;
1295 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1296 gtk_text_buffer_place_cursor( m_buffer, &iter );
1297 gtk_text_view_scroll_mark_onscreen
1298 (
1299 GTK_TEXT_VIEW(m_text),
1300 gtk_text_buffer_get_insert( m_buffer )
1301 );
1302 #else // GTK+ 1.x
1303 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
1304 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
1305
1306 /* we fake a set_point by inserting and deleting. as the user
1307 isn't supposed to get to know about this non-sense, we
1308 disconnect so that no events are sent to the user program. */
1309
1310 gint tmp = (gint)pos;
1311 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
1312 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
1313
1314 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
1315 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
1316
1317 // bring editable's cursor uptodate. Bug in GTK.
1318 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
1319 #endif // GTK+ 2/1
1320 }
1321 else
1322 {
1323 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
1324
1325 // Bring editable's cursor uptodate. Bug in GTK.
1326 SET_EDITABLE_POS(m_text, (guint32)pos);
1327 }
1328 }
1329
1330 void wxTextCtrl::SetInsertionPointEnd()
1331 {
1332 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1333
1334 if (m_windowStyle & wxTE_MULTILINE)
1335 {
1336 #ifdef __WXGTK20__
1337 GtkTextIter end;
1338 gtk_text_buffer_get_end_iter( m_buffer, &end );
1339 gtk_text_buffer_place_cursor( m_buffer, &end );
1340 #else
1341 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
1342 #endif
1343 }
1344 else
1345 {
1346 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
1347 }
1348 }
1349
1350 void wxTextCtrl::SetEditable( bool editable )
1351 {
1352 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1353
1354 if (m_windowStyle & wxTE_MULTILINE)
1355 {
1356 #ifdef __WXGTK20__
1357 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1358 #else
1359 gtk_text_set_editable( GTK_TEXT(m_text), editable );
1360 #endif
1361 }
1362 else
1363 {
1364 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
1365 }
1366 }
1367
1368 bool wxTextCtrl::Enable( bool enable )
1369 {
1370 if (!wxWindowBase::Enable(enable))
1371 {
1372 // nothing to do
1373 return false;
1374 }
1375
1376 if (m_windowStyle & wxTE_MULTILINE)
1377 {
1378 #ifdef __WXGTK20__
1379 SetEditable( enable );
1380 #else
1381 gtk_text_set_editable( GTK_TEXT(m_text), enable );
1382 OnParentEnable(enable);
1383 #endif
1384 }
1385 else
1386 {
1387 gtk_widget_set_sensitive( m_text, enable );
1388 }
1389
1390 return true;
1391 }
1392
1393 // wxGTK-specific: called recursively by Enable,
1394 // to give widgets an oppprtunity to correct their colours after they
1395 // have been changed by Enable
1396 void wxTextCtrl::OnParentEnable( bool enable )
1397 {
1398 // If we have a custom background colour, we use this colour in both
1399 // disabled and enabled mode, or we end up with a different colour under the
1400 // text.
1401 wxColour oldColour = GetBackgroundColour();
1402 if (oldColour.Ok())
1403 {
1404 // Need to set twice or it'll optimize the useful stuff out
1405 if (oldColour == * wxWHITE)
1406 SetBackgroundColour(*wxBLACK);
1407 else
1408 SetBackgroundColour(*wxWHITE);
1409 SetBackgroundColour(oldColour);
1410 }
1411 }
1412
1413 void wxTextCtrl::MarkDirty()
1414 {
1415 m_modified = true;
1416 }
1417
1418 void wxTextCtrl::DiscardEdits()
1419 {
1420 m_modified = false;
1421 }
1422
1423 // ----------------------------------------------------------------------------
1424 // max text length support
1425 // ----------------------------------------------------------------------------
1426
1427 void wxTextCtrl::IgnoreNextTextUpdate()
1428 {
1429 m_ignoreNextUpdate = true;
1430 }
1431
1432 bool wxTextCtrl::IgnoreTextUpdate()
1433 {
1434 if ( m_ignoreNextUpdate )
1435 {
1436 m_ignoreNextUpdate = false;
1437
1438 return true;
1439 }
1440
1441 return false;
1442 }
1443
1444 void wxTextCtrl::SetMaxLength(unsigned long len)
1445 {
1446 if ( !HasFlag(wxTE_MULTILINE) )
1447 {
1448 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
1449
1450 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1451 // we had tried to enter more text than allowed by max text length and
1452 // the text wasn't really changed
1453 //
1454 // to detect this and generate TEXT_MAXLEN event instead of
1455 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1456 //
1457 // when max len is set to 0 we disconnect our handler as it means that
1458 // we shouldn't check anything any more
1459 if ( len )
1460 {
1461 gtk_signal_connect( GTK_OBJECT(m_text),
1462 "insert_text",
1463 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1464 (gpointer)this);
1465 }
1466 else // no checking
1467 {
1468 gtk_signal_disconnect_by_func
1469 (
1470 GTK_OBJECT(m_text),
1471 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1472 (gpointer)this
1473 );
1474 }
1475 }
1476 }
1477
1478 void wxTextCtrl::SetSelection( long from, long to )
1479 {
1480 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1481
1482 if (from == -1 && to == -1)
1483 {
1484 from = 0;
1485 to = GetValue().Length();
1486 }
1487
1488 #ifndef __WXGTK20__
1489 if ( (m_windowStyle & wxTE_MULTILINE) &&
1490 !GTK_TEXT(m_text)->line_start_cache )
1491 {
1492 // tell the programmer that it didn't work
1493 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1494 return;
1495 }
1496 #endif
1497
1498 if (m_windowStyle & wxTE_MULTILINE)
1499 {
1500 #ifdef __WXGTK20__
1501 GtkTextIter fromi, toi;
1502 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1503 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1504
1505 gtk_text_buffer_place_cursor( m_buffer, &toi );
1506 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
1507 #else
1508 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1509 #endif
1510 }
1511 else
1512 {
1513 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1514 }
1515 }
1516
1517 void wxTextCtrl::ShowPosition( long pos )
1518 {
1519 if (m_windowStyle & wxTE_MULTILINE)
1520 {
1521 #ifdef __WXGTK20__
1522 GtkTextIter iter;
1523 gtk_text_buffer_get_start_iter( m_buffer, &iter );
1524 gtk_text_iter_set_offset( &iter, pos );
1525 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
1526 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1527 #else // GTK 1.x
1528 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1529 float totalLines = (float) GetNumberOfLines();
1530 long posX;
1531 long posY;
1532 PositionToXY(pos, &posX, &posY);
1533 float posLine = (float) posY;
1534 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1535 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
1536 #endif // GTK 1.x/2.x
1537 }
1538 }
1539
1540 #ifdef __WXGTK20__
1541
1542 wxTextCtrlHitTestResult
1543 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1544 {
1545 if ( !IsMultiLine() )
1546 {
1547 // not supported
1548 return wxTE_HT_UNKNOWN;
1549 }
1550
1551 int x, y;
1552 gtk_text_view_window_to_buffer_coords
1553 (
1554 GTK_TEXT_VIEW(m_text),
1555 GTK_TEXT_WINDOW_TEXT,
1556 pt.x, pt.y,
1557 &x, &y
1558 );
1559
1560 GtkTextIter iter;
1561 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1562 if ( pos )
1563 *pos = gtk_text_iter_get_offset(&iter);
1564
1565 return wxTE_HT_ON_TEXT;
1566 }
1567
1568 #endif // __WXGTK20__
1569
1570 long wxTextCtrl::GetInsertionPoint() const
1571 {
1572 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1573
1574 #ifdef __WXGTK20__
1575 if (m_windowStyle & wxTE_MULTILINE)
1576 {
1577 // There is no direct accessor for the cursor, but
1578 // internally, the cursor is the "mark" called
1579 // "insert" in the text view's btree structure.
1580
1581 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1582 GtkTextIter cursor;
1583 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1584
1585 return gtk_text_iter_get_offset( &cursor );
1586 }
1587 else
1588 #endif
1589 {
1590 return (long) GET_EDITABLE_POS(m_text);
1591 }
1592 }
1593
1594 wxTextPos wxTextCtrl::GetLastPosition() const
1595 {
1596 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1597
1598 int pos = 0;
1599
1600 if (m_windowStyle & wxTE_MULTILINE)
1601 {
1602 #ifdef __WXGTK20__
1603 GtkTextIter end;
1604 gtk_text_buffer_get_end_iter( m_buffer, &end );
1605
1606 pos = gtk_text_iter_get_offset( &end );
1607 #else
1608 pos = gtk_text_get_length( GTK_TEXT(m_text) );
1609 #endif
1610 }
1611 else
1612 {
1613 pos = GTK_ENTRY(m_text)->text_length;
1614 }
1615
1616 return (long)pos;
1617 }
1618
1619 void wxTextCtrl::Remove( long from, long to )
1620 {
1621 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1622
1623 #ifdef __WXGTK20__
1624 if (m_windowStyle & wxTE_MULTILINE)
1625 {
1626 GtkTextIter fromi, toi;
1627 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1628 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1629
1630 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1631 }
1632 else // single line
1633 #endif
1634 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1635 }
1636
1637 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1638 {
1639 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1640
1641 Remove( from, to );
1642
1643 if (!value.empty())
1644 {
1645 #ifdef __WXGTK20__
1646 SetInsertionPoint( from );
1647 WriteText( value );
1648 #else // GTK 1.x
1649 gint pos = (gint)from;
1650 #if wxUSE_UNICODE
1651 wxWX2MBbuf buf = value.mbc_str();
1652 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
1653 #else
1654 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
1655 #endif // wxUSE_UNICODE
1656 #endif // GTK 1.x/2.x
1657 }
1658 }
1659
1660 void wxTextCtrl::Cut()
1661 {
1662 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1663
1664 #ifdef __WXGTK20__
1665 if (m_windowStyle & wxTE_MULTILINE)
1666 g_signal_emit_by_name(m_text, "cut-clipboard");
1667 else
1668 #endif
1669 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1670 }
1671
1672 void wxTextCtrl::Copy()
1673 {
1674 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1675
1676 #ifdef __WXGTK20__
1677 if (m_windowStyle & wxTE_MULTILINE)
1678 g_signal_emit_by_name(m_text, "copy-clipboard");
1679 else
1680 #endif
1681 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1682 }
1683
1684 void wxTextCtrl::Paste()
1685 {
1686 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1687
1688 #ifdef __WXGTK20__
1689 if (m_windowStyle & wxTE_MULTILINE)
1690 g_signal_emit_by_name(m_text, "paste-clipboard");
1691 else
1692 #endif
1693 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1694 }
1695
1696 // Undo/redo
1697 void wxTextCtrl::Undo()
1698 {
1699 // TODO
1700 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1701 }
1702
1703 void wxTextCtrl::Redo()
1704 {
1705 // TODO
1706 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1707 }
1708
1709 bool wxTextCtrl::CanUndo() const
1710 {
1711 // TODO
1712 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1713 return false;
1714 }
1715
1716 bool wxTextCtrl::CanRedo() const
1717 {
1718 // TODO
1719 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1720 return false;
1721 }
1722
1723 // If the return values from and to are the same, there is no
1724 // selection.
1725 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1726 {
1727 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1728
1729 gint from = -1;
1730 gint to = -1;
1731 bool haveSelection = false;
1732
1733 #ifdef __WXGTK20__
1734 if (m_windowStyle & wxTE_MULTILINE)
1735 {
1736 GtkTextIter ifrom, ito;
1737 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1738 {
1739 haveSelection = true;
1740 from = gtk_text_iter_get_offset(&ifrom);
1741 to = gtk_text_iter_get_offset(&ito);
1742 }
1743 }
1744 else // not multi-line
1745 {
1746 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1747 &from, &to) )
1748 {
1749 haveSelection = true;
1750 }
1751 }
1752 #else // not GTK2
1753 if ( (GTK_EDITABLE(m_text)->has_selection) )
1754 {
1755 haveSelection = true;
1756 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1757 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1758 }
1759 #endif
1760
1761 if (! haveSelection )
1762 from = to = GetInsertionPoint();
1763
1764 if ( from > to )
1765 {
1766 // exchange them to be compatible with wxMSW
1767 gint tmp = from;
1768 from = to;
1769 to = tmp;
1770 }
1771
1772 if ( fromOut )
1773 *fromOut = from;
1774 if ( toOut )
1775 *toOut = to;
1776 }
1777
1778
1779 bool wxTextCtrl::IsEditable() const
1780 {
1781 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1782
1783 #ifdef __WXGTK20__
1784 if (m_windowStyle & wxTE_MULTILINE)
1785 {
1786 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1787 }
1788 else
1789 {
1790 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1791 }
1792 #else
1793 return GTK_EDITABLE(m_text)->editable;
1794 #endif
1795 }
1796
1797 bool wxTextCtrl::IsModified() const
1798 {
1799 return m_modified;
1800 }
1801
1802 void wxTextCtrl::Clear()
1803 {
1804 SetValue( wxEmptyString );
1805 }
1806
1807 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1808 {
1809 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1810
1811 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1812 {
1813 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1814 event.SetEventObject(this);
1815 event.SetString(GetValue());
1816 if (GetEventHandler()->ProcessEvent(event)) return;
1817 }
1818
1819 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1820 {
1821 // This will invoke the dialog default action, such
1822 // as the clicking the default button.
1823
1824 wxWindow *top_frame = m_parent;
1825 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1826 top_frame = top_frame->GetParent();
1827
1828 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1829 {
1830 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1831
1832 if (window->default_widget)
1833 {
1834 gtk_widget_activate (window->default_widget);
1835 return;
1836 }
1837 }
1838 }
1839
1840 key_event.Skip();
1841 }
1842
1843 GtkWidget* wxTextCtrl::GetConnectWidget()
1844 {
1845 return GTK_WIDGET(m_text);
1846 }
1847
1848 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1849 {
1850 if (m_windowStyle & wxTE_MULTILINE)
1851 {
1852 #ifdef __WXGTK20__
1853 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1854 #else
1855 return (window == GTK_TEXT(m_text)->text_area);
1856 #endif
1857 }
1858 else
1859 {
1860 return (window == GTK_ENTRY(m_text)->text_area);
1861 }
1862 }
1863
1864 // the font will change for subsequent text insertiongs
1865 bool wxTextCtrl::SetFont( const wxFont &font )
1866 {
1867 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1868
1869 if ( !wxTextCtrlBase::SetFont(font) )
1870 {
1871 // font didn't change, nothing to do
1872 return false;
1873 }
1874
1875 if ( m_windowStyle & wxTE_MULTILINE )
1876 {
1877 SetUpdateFont(true);
1878
1879 m_defaultStyle.SetFont(font);
1880
1881 ChangeFontGlobally();
1882 }
1883
1884 return true;
1885 }
1886
1887 void wxTextCtrl::ChangeFontGlobally()
1888 {
1889 // this method is very inefficient and hence should be called as rarely as
1890 // possible!
1891 //
1892 // TODO: it can be implemented much more efficiently for GTK2
1893 #ifndef __WXGTK20__
1894 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1895
1896 _T("shouldn't be called for single line controls") );
1897 #else
1898 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1899 _T("shouldn't be called for single line controls") );
1900 #endif
1901
1902 wxString value = GetValue();
1903 if ( !value.empty() )
1904 {
1905 SetUpdateFont(false);
1906
1907 Clear();
1908 AppendText(value);
1909 }
1910 }
1911
1912 #ifndef __WXGTK20__
1913
1914 void wxTextCtrl::UpdateFontIfNeeded()
1915 {
1916 if ( m_updateFont )
1917 ChangeFontGlobally();
1918 }
1919
1920 #endif // GTK+ 1.x
1921
1922 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1923 {
1924 if ( !wxControl::SetForegroundColour(colour) )
1925 return false;
1926
1927 // update default fg colour too
1928 m_defaultStyle.SetTextColour(colour);
1929
1930 return true;
1931 }
1932
1933 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1934 {
1935 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1936
1937 if ( !wxControl::SetBackgroundColour( colour ) )
1938 return false;
1939
1940 #ifndef __WXGTK20__
1941 if (!m_widget->window)
1942 return false;
1943 #endif
1944
1945 if (!m_backgroundColour.Ok())
1946 return false;
1947
1948 if (m_windowStyle & wxTE_MULTILINE)
1949 {
1950 #ifndef __WXGTK20__
1951 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1952 if (!window)
1953 return false;
1954 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1955 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1956 gdk_window_clear( window );
1957 #endif
1958 }
1959
1960 // change active background color too
1961 m_defaultStyle.SetBackgroundColour( colour );
1962
1963 return true;
1964 }
1965
1966 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1967 {
1968 if ( m_windowStyle & wxTE_MULTILINE )
1969 {
1970 if ( style.IsDefault() )
1971 {
1972 // nothing to do
1973 return true;
1974 }
1975
1976 #ifdef __WXGTK20__
1977 gint l = gtk_text_buffer_get_char_count( m_buffer );
1978
1979 wxCHECK_MSG( start >= 0 && end <= l, false,
1980 _T("invalid range in wxTextCtrl::SetStyle") );
1981
1982 GtkTextIter starti, endi;
1983 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1984 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1985
1986 // use the attributes from style which are set in it and fall back
1987 // first to the default style and then to the text control default
1988 // colours for the others
1989 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1990
1991 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
1992 #else
1993 // VERY dirty way to do that - removes the required text and re-adds it
1994 // with styling (FIXME)
1995
1996 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1997
1998 wxCHECK_MSG( start >= 0 && end <= l, false,
1999 _T("invalid range in wxTextCtrl::SetStyle") );
2000
2001 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
2002 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
2003 wxString tmp(text,*wxConvCurrent);
2004 g_free( text );
2005
2006 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
2007 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
2008
2009 #if wxUSE_UNICODE
2010 wxWX2MBbuf buf = tmp.mbc_str();
2011 const char *txt = buf;
2012 size_t txtlen = strlen(buf);
2013 #else
2014 const char *txt = tmp;
2015 size_t txtlen = tmp.length();
2016 #endif
2017
2018 // use the attributes from style which are set in it and fall back
2019 // first to the default style and then to the text control default
2020 // colours for the others
2021 wxGtkTextInsert(m_text,
2022 wxTextAttr::Combine(style, m_defaultStyle, this),
2023 txt,
2024 txtlen);
2025
2026 /* does not seem to help under GTK+ 1.2 !!!
2027 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
2028 SetInsertionPoint( old_pos );
2029 #endif
2030
2031 return true;
2032 }
2033
2034 // else single line
2035 // cannot do this for GTK+'s Entry widget
2036 return false;
2037 }
2038
2039 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
2040 {
2041 gtk_widget_modify_style(m_text, style);
2042 }
2043
2044 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2045 {
2046 Cut();
2047 }
2048
2049 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2050 {
2051 Copy();
2052 }
2053
2054 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2055 {
2056 Paste();
2057 }
2058
2059 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2060 {
2061 Undo();
2062 }
2063
2064 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2065 {
2066 Redo();
2067 }
2068
2069 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2070 {
2071 event.Enable( CanCut() );
2072 }
2073
2074 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2075 {
2076 event.Enable( CanCopy() );
2077 }
2078
2079 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2080 {
2081 event.Enable( CanPaste() );
2082 }
2083
2084 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2085 {
2086 event.Enable( CanUndo() );
2087 }
2088
2089 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2090 {
2091 event.Enable( CanRedo() );
2092 }
2093
2094 void wxTextCtrl::OnInternalIdle()
2095 {
2096 wxCursor cursor = m_cursor;
2097 if (g_globalCursor.Ok()) cursor = g_globalCursor;
2098
2099 if (cursor.Ok())
2100 {
2101 #ifndef __WXGTK20__
2102 GdkWindow *window = (GdkWindow*) NULL;
2103 if (HasFlag(wxTE_MULTILINE))
2104 window = GTK_TEXT(m_text)->text_area;
2105 else
2106 window = GTK_ENTRY(m_text)->text_area;
2107
2108 if (window)
2109 gdk_window_set_cursor( window, cursor.GetCursor() );
2110
2111 if (!g_globalCursor.Ok())
2112 cursor = *wxSTANDARD_CURSOR;
2113
2114 window = m_widget->window;
2115 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
2116 gdk_window_set_cursor( window, cursor.GetCursor() );
2117 #endif
2118 }
2119
2120 if (g_delayedFocus == this)
2121 {
2122 if (GTK_WIDGET_REALIZED(m_widget))
2123 {
2124 gtk_widget_grab_focus( m_widget );
2125 g_delayedFocus = NULL;
2126 }
2127 }
2128
2129 if (wxUpdateUIEvent::CanUpdate(this))
2130 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2131 }
2132
2133 wxSize wxTextCtrl::DoGetBestSize() const
2134 {
2135 // FIXME should be different for multi-line controls...
2136 wxSize ret( wxControl::DoGetBestSize() );
2137 wxSize best(80, ret.y);
2138 CacheBestSize(best);
2139 return best;
2140 }
2141
2142 // ----------------------------------------------------------------------------
2143 // freeze/thaw
2144 // ----------------------------------------------------------------------------
2145
2146 void wxTextCtrl::Freeze()
2147 {
2148 if ( HasFlag(wxTE_MULTILINE) )
2149 {
2150 #ifdef __WXGTK20__
2151 if ( !m_frozenness++ )
2152 {
2153 // freeze textview updates and remove buffer
2154 g_signal_connect( G_OBJECT(m_text), "expose_event",
2155 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2156 g_signal_connect( G_OBJECT(m_widget), "expose_event",
2157 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2158 gtk_widget_set_sensitive(m_widget, false);
2159 g_object_ref(m_buffer);
2160 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
2161 }
2162 #else
2163 gtk_text_freeze(GTK_TEXT(m_text));
2164 #endif
2165 }
2166 }
2167
2168 void wxTextCtrl::Thaw()
2169 {
2170 if ( HasFlag(wxTE_MULTILINE) )
2171 {
2172 #ifdef __WXGTK20__
2173 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2174
2175 if ( !--m_frozenness )
2176 {
2177 // Reattach buffer and thaw textview updates
2178 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
2179 g_object_unref(m_buffer);
2180 gtk_widget_set_sensitive(m_widget, true);
2181 g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this);
2182 g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this);
2183 }
2184 #else
2185 GTK_TEXT(m_text)->vadj->value = 0.0;
2186
2187 gtk_text_thaw(GTK_TEXT(m_text));
2188 #endif
2189 }
2190 }
2191
2192 // ----------------------------------------------------------------------------
2193 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
2194 // ----------------------------------------------------------------------------
2195
2196 #ifdef __WXGTK20__
2197
2198 // FIXME: when dragging on a link the sample gets an "Unknown event".
2199 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
2200 // a buggy sample, or something else
2201 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
2202 {
2203 event.Skip();
2204 if(!(m_windowStyle & wxTE_AUTO_URL))
2205 return;
2206
2207 gint x, y;
2208 GtkTextIter start, end;
2209 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
2210 "wxUrl");
2211
2212 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
2213 event.GetX(), event.GetY(), &x, &y);
2214
2215 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
2216 if (!gtk_text_iter_has_tag(&end, tag))
2217 {
2218 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2219 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
2220 return;
2221 }
2222
2223 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2224 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
2225
2226 start = end;
2227 if(!gtk_text_iter_begins_tag(&start, tag))
2228 gtk_text_iter_backward_to_tag_toggle(&start, tag);
2229 if(!gtk_text_iter_ends_tag(&end, tag))
2230 gtk_text_iter_forward_to_tag_toggle(&end, tag);
2231
2232 // Native context menu is probably not desired on an URL.
2233 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
2234 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
2235 event.Skip(false);
2236
2237 wxTextUrlEvent url_event(m_windowId, event,
2238 gtk_text_iter_get_offset(&start),
2239 gtk_text_iter_get_offset(&end));
2240
2241 InitCommandEvent(url_event);
2242 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2243 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
2244 GetEventHandler()->ProcessEvent(url_event);
2245 }
2246 #endif // gtk2
2247
2248 // ----------------------------------------------------------------------------
2249 // scrolling
2250 // ----------------------------------------------------------------------------
2251
2252 GtkAdjustment *wxTextCtrl::GetVAdj() const
2253 {
2254 if ( !IsMultiLine() )
2255 return NULL;
2256
2257 #ifdef __WXGTK20__
2258 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
2259 #else
2260 return GTK_TEXT(m_text)->vadj;
2261 #endif
2262 }
2263
2264 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
2265 {
2266 float value = adj->value + diff;
2267
2268 if ( value < 0 )
2269 value = 0;
2270
2271 float upper = adj->upper - adj->page_size;
2272 if ( value > upper )
2273 value = upper;
2274
2275 // did we noticeably change the scroll position?
2276 if ( fabs(adj->value - value) < 0.2 )
2277 {
2278 // well, this is what Robert does in wxScrollBar, so it must be good...
2279 return false;
2280 }
2281
2282 adj->value = value;
2283
2284 #ifdef __WXGTK20__
2285 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
2286 #else
2287 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
2288 #endif
2289
2290 return true;
2291 }
2292
2293 bool wxTextCtrl::ScrollLines(int lines)
2294 {
2295 GtkAdjustment *adj = GetVAdj();
2296 if ( !adj )
2297 return false;
2298
2299 #ifdef __WXGTK20__
2300 int diff = (int)ceil(lines*adj->step_increment);
2301 #else
2302 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
2303 int diff = 10*lines;
2304 #endif
2305
2306 return DoScroll(adj, diff);
2307 }
2308
2309 bool wxTextCtrl::ScrollPages(int pages)
2310 {
2311 GtkAdjustment *adj = GetVAdj();
2312 if ( !adj )
2313 return false;
2314
2315 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
2316 }
2317
2318
2319 // static
2320 wxVisualAttributes
2321 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2322 {
2323 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
2324 }