don't crash if WriteText() argument can't be converted to UTF-8
[wxWidgets.git] / src / gtk1 / 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 <math.h> // for fabs
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 bool g_blockEventsOnDrag;
46 extern wxCursor g_globalCursor;
47 extern wxWindowGTK *g_delayedFocus;
48
49 // ----------------------------------------------------------------------------
50 // helpers
51 // ----------------------------------------------------------------------------
52
53 #ifdef __WXGTK20__
54 static void wxGtkTextInsert(GtkWidget *text,
55 GtkTextBuffer *text_buffer,
56 const wxTextAttr& attr,
57 wxCharBuffer buffer)
58
59 {
60 PangoFontDescription *font_description = attr.HasFont()
61 ? attr.GetFont().GetNativeFontInfo()->description
62 : NULL;
63
64 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
65 : NULL;
66
67 GdkColor *colBg = attr.HasBackgroundColour()
68 ? attr.GetBackgroundColour().GetColor()
69 : NULL;
70
71 GtkTextTag *tag;
72 tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description,
73 "foreground-gdk", colFg,
74 "background-gdk", colBg, NULL );
75
76 GtkTextIter iter;
77 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
78 gtk_text_buffer_get_insert (text_buffer) );
79
80 gtk_text_buffer_insert_with_tags( text_buffer, &iter, buffer, strlen(buffer), tag, NULL );
81 }
82 #else
83 static void wxGtkTextInsert(GtkWidget *text,
84 const wxTextAttr& attr,
85 const char *txt,
86 size_t len)
87 {
88 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
89 : NULL;
90
91 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
92 : NULL;
93
94 GdkColor *colBg = attr.HasBackgroundColour()
95 ? attr.GetBackgroundColour().GetColor()
96 : NULL;
97
98 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
99 }
100 #endif // GTK 1.x
101
102 // ----------------------------------------------------------------------------
103 // "insert_text" for GtkEntry
104 // ----------------------------------------------------------------------------
105
106 static void
107 gtk_insert_text_callback(GtkEditable *editable,
108 const gchar *new_text,
109 gint new_text_length,
110 gint *position,
111 wxTextCtrl *win)
112 {
113 if (g_isIdle)
114 wxapp_install_idle_handler();
115
116 // we should only be called if we have a max len limit at all
117 GtkEntry *entry = GTK_ENTRY (editable);
118
119 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
120
121 // check that we don't overflow the max length limit
122 //
123 // FIXME: this doesn't work when we paste a string which is going to be
124 // truncated
125 if ( entry->text_length == entry->text_max_length )
126 {
127 // we don't need to run the base class version at all
128 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
129
130 // remember that the next changed signal is to be ignored to avoid
131 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
132 win->IgnoreNextTextUpdate();
133
134 // and generate the correct one ourselves
135 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
136 event.SetEventObject(win);
137 event.SetString(win->GetValue());
138 win->GetEventHandler()->ProcessEvent( event );
139 }
140 }
141
142 //-----------------------------------------------------------------------------
143 // "changed"
144 //-----------------------------------------------------------------------------
145
146 static void
147 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
148 {
149 if ( win->IgnoreTextUpdate() )
150 return;
151
152 if (!win->m_hasVMT) return;
153
154 if (g_isIdle)
155 wxapp_install_idle_handler();
156
157 win->SetModified();
158 #ifndef __WXGTK20__
159 win->UpdateFontIfNeeded();
160 #endif // !__WXGTK20__
161
162 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
163 event.SetEventObject( win );
164 event.SetString( win->GetValue() );
165 win->GetEventHandler()->ProcessEvent( event );
166 }
167
168 //-----------------------------------------------------------------------------
169 // "changed" from vertical scrollbar
170 //-----------------------------------------------------------------------------
171
172 #ifndef __WXGTK20__
173 static void
174 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
175 {
176 if (!win->m_hasVMT) return;
177
178 if (g_isIdle)
179 wxapp_install_idle_handler();
180
181 win->CalculateScrollbar();
182 }
183 #endif
184
185 // ----------------------------------------------------------------------------
186 // redraw callback for multiline text
187 // ----------------------------------------------------------------------------
188
189 #ifndef __WXGTK20__
190
191 // redrawing a GtkText from inside a wxYield() call results in crashes (the
192 // text sample shows it in its "Add lines" command which shows wxProgressDialog
193 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
194 // don't do anything if we're inside wxYield()
195
196 extern bool wxIsInsideYield;
197
198 extern "C" {
199 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
200 }
201
202 static GtkDrawCallback gs_gtk_text_draw = NULL;
203
204 extern "C"
205 void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
206 {
207 if ( !wxIsInsideYield )
208 {
209 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
210 _T("infinite recursion in wxgtk_text_draw aborted") );
211
212 gs_gtk_text_draw(widget, rect);
213 }
214 }
215
216 #endif // __WXGTK20__
217
218 //-----------------------------------------------------------------------------
219 // wxTextCtrl
220 //-----------------------------------------------------------------------------
221
222 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
223
224 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
225 EVT_CHAR(wxTextCtrl::OnChar)
226
227 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
228 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
229 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
230 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
231 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
232
233 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
234 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
235 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
236 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
237 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
238 END_EVENT_TABLE()
239
240 void wxTextCtrl::Init()
241 {
242 m_ignoreNextUpdate =
243 m_modified = FALSE;
244 SetUpdateFont(FALSE);
245 m_text =
246 m_vScrollbar = (GtkWidget *)NULL;
247 }
248
249 wxTextCtrl::wxTextCtrl( wxWindow *parent,
250 wxWindowID id,
251 const wxString &value,
252 const wxPoint &pos,
253 const wxSize &size,
254 long style,
255 const wxValidator& validator,
256 const wxString &name )
257 {
258 Init();
259
260 Create( parent, id, value, pos, size, style, validator, name );
261 }
262
263 bool wxTextCtrl::Create( wxWindow *parent,
264 wxWindowID id,
265 const wxString &value,
266 const wxPoint &pos,
267 const wxSize &size,
268 long style,
269 const wxValidator& validator,
270 const wxString &name )
271 {
272 m_needParent = TRUE;
273 m_acceptsFocus = TRUE;
274
275 if (!PreCreation( parent, pos, size ) ||
276 !CreateBase( parent, id, pos, size, style, validator, name ))
277 {
278 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
279 return FALSE;
280 }
281
282
283 m_vScrollbarVisible = FALSE;
284
285 bool multi_line = (style & wxTE_MULTILINE) != 0;
286
287 #ifdef __WXGTK20__
288 GtkTextBuffer *buffer = NULL;
289 #endif
290
291 if (multi_line)
292 {
293 #ifdef __WXGTK20__
294 // Create view
295 m_text = gtk_text_view_new();
296
297 buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
298
299 // create scrolled window
300 m_widget = gtk_scrolled_window_new( NULL, NULL );
301 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
302 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
303
304 // Insert view into scrolled window
305 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
306
307 // Global settings which can be overridden by tags, I guess.
308 if (HasFlag( wxHSCROLL ) || HasFlag( wxTE_DONTWRAP ))
309 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE );
310 else
311 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD );
312
313 if (!HasFlag(wxNO_BORDER))
314 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
315
316 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
317 #else
318 // create our control ...
319 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
320
321 // ... and put into the upper left hand corner of the table
322 bool bHasHScrollbar = FALSE;
323 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
324 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
325 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
326 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
327 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
328 0, 0);
329
330 // always wrap words
331 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
332
333 // finally, put the vertical scrollbar in the upper right corner
334 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
335 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
336 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
337 GTK_FILL,
338 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
339 0, 0);
340 #endif
341 }
342 else
343 {
344 // a single-line text control: no need for scrollbars
345 m_widget =
346 m_text = gtk_entry_new();
347 }
348
349 m_parent->DoAddChild( this );
350
351 m_focusWidget = m_text;
352
353 PostCreation(size);
354
355 if (multi_line)
356 gtk_widget_show(m_text);
357
358 #ifndef __WXGTK20__
359 if (multi_line)
360 {
361 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
362 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
363
364 // only initialize gs_gtk_text_draw once, starting from the next the
365 // klass::draw will already be wxgtk_text_draw
366 if ( !gs_gtk_text_draw )
367 {
368 GtkDrawCallback&
369 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
370
371 gs_gtk_text_draw = draw;
372
373 draw = wxgtk_text_draw;
374 }
375 }
376 #endif // GTK+ 1.x
377
378 if (!value.IsEmpty())
379 {
380 #ifdef __WXGTK20__
381 SetValue( value );
382 #else
383
384 #if !GTK_CHECK_VERSION(1, 2, 0)
385 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
386 // gtk_editable_insert_text()
387 gtk_widget_realize(m_text);
388 #endif // GTK 1.0
389
390 gint tmp = 0;
391 #if wxUSE_UNICODE
392 wxWX2MBbuf val = value.mbc_str();
393 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
394 #else
395 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
396 #endif
397
398 if (multi_line)
399 {
400 // Bring editable's cursor uptodate. Bug in GTK.
401 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
402 }
403
404 #endif
405 }
406
407 if (style & wxTE_PASSWORD)
408 {
409 if (!multi_line)
410 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
411 }
412
413 if (style & wxTE_READONLY)
414 {
415 if (!multi_line)
416 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
417 #ifdef __WXGTK20__
418 else
419 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
420 #else
421 }
422 else
423 {
424 if (multi_line)
425 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
426 #endif
427 }
428
429 #ifdef __WXGTK20__
430 if (multi_line)
431 {
432 if (style & wxTE_RIGHT)
433 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
434 else if (style & wxTE_CENTRE)
435 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
436 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
437 }
438 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
439 #if GTK_CHECK_VERSION(2, 3, 5)
440 else
441 {
442 if (style & wxTE_RIGHT)
443 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
444 else if (style & wxTE_CENTRE)
445 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
446 }
447 #endif // gtk+-2.3.5
448 #endif // __WXGTK20__
449
450 // We want to be notified about text changes.
451 #ifdef __WXGTK20__
452 if (multi_line)
453 {
454 g_signal_connect( G_OBJECT(buffer), "changed",
455 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
456 }
457 else
458 #endif
459
460 {
461 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
462 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
463 }
464
465 m_cursor = wxCursor( wxCURSOR_IBEAM );
466
467 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
468 SetDefaultStyle( attrDef );
469
470 return TRUE;
471 }
472
473
474 void wxTextCtrl::CalculateScrollbar()
475 {
476 #ifndef __WXGTK20__
477 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
478
479 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
480
481 if (adj->upper - adj->page_size < 0.8)
482 {
483 if (m_vScrollbarVisible)
484 {
485 gtk_widget_hide( m_vScrollbar );
486 m_vScrollbarVisible = FALSE;
487 }
488 }
489 else
490 {
491 if (!m_vScrollbarVisible)
492 {
493 gtk_widget_show( m_vScrollbar );
494 m_vScrollbarVisible = TRUE;
495 }
496 }
497 #endif
498 }
499
500 wxString wxTextCtrl::GetValue() const
501 {
502 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
503
504 wxString tmp;
505 if (m_windowStyle & wxTE_MULTILINE)
506 {
507 #ifdef __WXGTK20__
508 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
509
510 GtkTextIter start;
511 gtk_text_buffer_get_start_iter( text_buffer, &start );
512 GtkTextIter end;
513 gtk_text_buffer_get_end_iter( text_buffer, &end );
514 gchar *text = gtk_text_buffer_get_text( text_buffer, &start, &end, TRUE );
515
516 #if wxUSE_UNICODE
517 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
518 #else
519 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
520 #endif
521 tmp = buffer;
522
523 g_free( text );
524 #else
525 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
526 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
527 tmp = text;
528 g_free( text );
529 #endif
530 }
531 else
532 {
533 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
534 }
535
536 return tmp;
537 }
538
539 void wxTextCtrl::SetValue( const wxString &value )
540 {
541 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
542
543 if (m_windowStyle & wxTE_MULTILINE)
544 {
545 #ifdef __WXGTK20__
546
547 #if wxUSE_UNICODE
548 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
549 #else
550 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
551 #endif
552 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
553 gtk_text_buffer_set_text( text_buffer, buffer, strlen(buffer) );
554
555 #else
556 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
557 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
558 len = 0;
559 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
560 #endif
561 }
562 else
563 {
564 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
565 }
566
567 // GRG, Jun/2000: Changed this after a lot of discussion in
568 // the lists. wxWidgets 2.2 will have a set of flags to
569 // customize this behaviour.
570 SetInsertionPoint(0);
571
572 m_modified = FALSE;
573 }
574
575 void wxTextCtrl::WriteText( const wxString &text )
576 {
577 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
578
579 if ( text.empty() )
580 return;
581
582 // gtk_text_changed_callback() will set m_modified to true but m_modified
583 // shouldn't be changed by the program writing to the text control itself,
584 // so save the old value and restore when we're done
585 bool oldModified = m_modified;
586
587 if ( m_windowStyle & wxTE_MULTILINE )
588 {
589 #ifdef __WXGTK20__
590
591 #if wxUSE_UNICODE
592 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
593 #else
594 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
595 #endif
596 if ( !buffer )
597 {
598 // what else can we do? at least don't crash...
599 return;
600 }
601
602 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
603
604 // TODO: Call whatever is needed to delete the selection.
605 wxGtkTextInsert( m_text, text_buffer, m_defaultStyle, buffer );
606
607 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
608 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
609 if ( adj->value == adj->upper - adj->page_size )
610 {
611 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
612 gtk_text_buffer_get_insert( text_buffer ), 0.0, FALSE, 0.0, 1.0 );
613 }
614 #else // GTK 1.x
615 // After cursor movements, gtk_text_get_point() is wrong by one.
616 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
617
618 // always use m_defaultStyle, even if it is empty as otherwise
619 // resetting the style and appending some more text wouldn't work: if
620 // we don't specify the style explicitly, the old style would be used
621 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
622 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
623
624 // we called wxGtkTextInsert with correct font, no need to do anything
625 // in UpdateFontIfNeeded() any longer
626 if ( !text.empty() )
627 {
628 SetUpdateFont(FALSE);
629 }
630
631 // Bring editable's cursor back uptodate.
632 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
633 #endif // GTK 1.x/2.0
634 }
635 else // single line
636 {
637 // First remove the selection if there is one
638 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
639
640 // This moves the cursor pos to behind the inserted text.
641 gint len = GET_EDITABLE_POS(m_text);
642
643 #ifdef __WXGTK20__
644
645 #if wxUSE_UNICODE
646 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
647 #else
648 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
649 #endif
650 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
651
652 #else
653 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
654 #endif
655
656 // Bring entry's cursor uptodate.
657 gtk_entry_set_position( GTK_ENTRY(m_text), len );
658 }
659
660 m_modified = oldModified;
661 }
662
663 void wxTextCtrl::AppendText( const wxString &text )
664 {
665 SetInsertionPointEnd();
666 WriteText( text );
667 }
668
669 wxString wxTextCtrl::GetLineText( long lineNo ) const
670 {
671 if (m_windowStyle & wxTE_MULTILINE)
672 {
673 #ifndef __WXGTK20__
674 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
675 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
676
677 if (text)
678 {
679 wxString buf(wxT(""));
680 long i;
681 int currentLine = 0;
682 for (i = 0; currentLine != lineNo && text[i]; i++ )
683 if (text[i] == '\n')
684 currentLine++;
685 // Now get the text
686 int j;
687 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
688 buf += text[i];
689
690 g_free( text );
691 return buf;
692 }
693 else
694 {
695 return wxEmptyString;
696 }
697 #else
698 GtkTextBuffer *text_buffer;
699 text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(m_text));
700 GtkTextIter line;
701 gtk_text_buffer_get_iter_at_line(text_buffer,&line,lineNo);
702 GtkTextIter end;
703 gtk_text_buffer_get_end_iter(text_buffer,&end );
704 gchar *text = gtk_text_buffer_get_text(text_buffer,&line,&end,TRUE);
705 wxString result(wxGTK_CONV_BACK(text));
706 g_free(text);
707 return result.BeforeFirst(wxT('\n'));
708 #endif
709 }
710 else
711 {
712 if (lineNo == 0) return GetValue();
713 return wxEmptyString;
714 }
715 }
716
717 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
718 {
719 /* If you implement this, don't forget to update the documentation!
720 * (file docs/latex/wx/text.tex) */
721 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
722 }
723
724 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
725 {
726 if ( m_windowStyle & wxTE_MULTILINE )
727 {
728 wxString text = GetValue();
729
730 // cast to prevent warning. But pos really should've been unsigned.
731 if( (unsigned long)pos > text.Len() )
732 return FALSE;
733
734 *x=0; // First Col
735 *y=0; // First Line
736
737 const wxChar* stop = text.c_str() + pos;
738 for ( const wxChar *p = text.c_str(); p < stop; p++ )
739 {
740 if (*p == wxT('\n'))
741 {
742 (*y)++;
743 *x=0;
744 }
745 else
746 (*x)++;
747 }
748 }
749 else // single line control
750 {
751 if ( pos <= GTK_ENTRY(m_text)->text_length )
752 {
753 *y = 0;
754 *x = pos;
755 }
756 else
757 {
758 // index out of bounds
759 return FALSE;
760 }
761 }
762
763 return TRUE;
764 }
765
766 long wxTextCtrl::XYToPosition(long x, long y ) const
767 {
768 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
769
770 long pos=0;
771 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
772
773 pos += x;
774 return pos;
775 }
776
777 int wxTextCtrl::GetLineLength(long lineNo) const
778 {
779 wxString str = GetLineText (lineNo);
780 return (int) str.Length();
781 }
782
783 int wxTextCtrl::GetNumberOfLines() const
784 {
785 if (m_windowStyle & wxTE_MULTILINE)
786 {
787 #ifdef __WXGTK20__
788 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
789
790 return gtk_text_buffer_get_line_count( buffer );
791 #else
792 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
793 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
794
795 if (text)
796 {
797 int currentLine = 0;
798 for (int i = 0; i < len; i++ )
799 {
800 if (text[i] == '\n')
801 currentLine++;
802 }
803 g_free( text );
804
805 // currentLine is 0 based, add 1 to get number of lines
806 return currentLine + 1;
807 }
808 else
809 {
810 return 0;
811 }
812 #endif
813 }
814 else
815 {
816 return 1;
817 }
818 }
819
820 void wxTextCtrl::SetInsertionPoint( long pos )
821 {
822 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
823
824 if (m_windowStyle & wxTE_MULTILINE)
825 {
826 #ifdef __WXGTK20__
827 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
828 GtkTextIter iter;
829 gtk_text_buffer_get_iter_at_offset( text_buffer, &iter, pos );
830 gtk_text_buffer_place_cursor( text_buffer, &iter );
831 #else
832 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
833 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
834
835 /* we fake a set_point by inserting and deleting. as the user
836 isn't supposed to get to know about this non-sense, we
837 disconnect so that no events are sent to the user program. */
838
839 gint tmp = (gint)pos;
840 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
841 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
842
843 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
844 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
845
846 // bring editable's cursor uptodate. Bug in GTK.
847 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
848 #endif
849 }
850 else
851 {
852 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
853
854 // Bring editable's cursor uptodate. Bug in GTK.
855 SET_EDITABLE_POS(m_text, (guint32)pos);
856 }
857 }
858
859 void wxTextCtrl::SetInsertionPointEnd()
860 {
861 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
862
863 if (m_windowStyle & wxTE_MULTILINE)
864 {
865 #ifdef __WXGTK20__
866 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
867 GtkTextIter end;
868 gtk_text_buffer_get_end_iter( text_buffer, &end );
869 gtk_text_buffer_place_cursor( text_buffer, &end );
870 #else
871 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
872 #endif
873 }
874 else
875 {
876 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
877 }
878 }
879
880 void wxTextCtrl::SetEditable( bool editable )
881 {
882 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
883
884 if (m_windowStyle & wxTE_MULTILINE)
885 {
886 #ifdef __WXGTK20__
887 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
888 #else
889 gtk_text_set_editable( GTK_TEXT(m_text), editable );
890 #endif
891 }
892 else
893 {
894 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
895 }
896 }
897
898 bool wxTextCtrl::Enable( bool enable )
899 {
900 if (!wxWindowBase::Enable(enable))
901 {
902 // nothing to do
903 return FALSE;
904 }
905
906 if (m_windowStyle & wxTE_MULTILINE)
907 {
908 #ifdef __WXGTK20__
909 SetEditable( enable );
910 #else
911 gtk_text_set_editable( GTK_TEXT(m_text), enable );
912 OnParentEnable(enable);
913 #endif
914 }
915 else
916 {
917 gtk_widget_set_sensitive( m_text, enable );
918 }
919
920 return TRUE;
921 }
922
923 // wxGTK-specific: called recursively by Enable,
924 // to give widgets an oppprtunity to correct their colours after they
925 // have been changed by Enable
926 void wxTextCtrl::OnParentEnable( bool enable )
927 {
928 // If we have a custom background colour, we use this colour in both
929 // disabled and enabled mode, or we end up with a different colour under the
930 // text.
931 wxColour oldColour = GetBackgroundColour();
932 if (oldColour.Ok())
933 {
934 // Need to set twice or it'll optimize the useful stuff out
935 if (oldColour == * wxWHITE)
936 SetBackgroundColour(*wxBLACK);
937 else
938 SetBackgroundColour(*wxWHITE);
939 SetBackgroundColour(oldColour);
940 }
941 }
942
943 void wxTextCtrl::MarkDirty()
944 {
945 m_modified = TRUE;
946 }
947
948 void wxTextCtrl::DiscardEdits()
949 {
950 m_modified = FALSE;
951 }
952
953 // ----------------------------------------------------------------------------
954 // max text length support
955 // ----------------------------------------------------------------------------
956
957 void wxTextCtrl::IgnoreNextTextUpdate()
958 {
959 m_ignoreNextUpdate = TRUE;
960 }
961
962 bool wxTextCtrl::IgnoreTextUpdate()
963 {
964 if ( m_ignoreNextUpdate )
965 {
966 m_ignoreNextUpdate = FALSE;
967
968 return TRUE;
969 }
970
971 return FALSE;
972 }
973
974 void wxTextCtrl::SetMaxLength(unsigned long len)
975 {
976 if ( !HasFlag(wxTE_MULTILINE) )
977 {
978 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
979
980 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
981 // we had tried to enter more text than allowed by max text length and
982 // the text wasn't really changed
983 //
984 // to detect this and generate TEXT_MAXLEN event instead of
985 // TEXT_CHANGED one in this case we also catch "insert_text" signal
986 //
987 // when max len is set to 0 we disconnect our handler as it means that
988 // we shouldn't check anything any more
989 if ( len )
990 {
991 gtk_signal_connect( GTK_OBJECT(m_text),
992 "insert_text",
993 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
994 (gpointer)this);
995 }
996 else // no checking
997 {
998 gtk_signal_disconnect_by_func
999 (
1000 GTK_OBJECT(m_text),
1001 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1002 (gpointer)this
1003 );
1004 }
1005 }
1006 }
1007
1008 void wxTextCtrl::SetSelection( long from, long to )
1009 {
1010 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1011
1012 if (from == -1 && to == -1)
1013 {
1014 from = 0;
1015 to = GetValue().Length();
1016 }
1017
1018 #ifndef __WXGTK20__
1019 if ( (m_windowStyle & wxTE_MULTILINE) &&
1020 !GTK_TEXT(m_text)->line_start_cache )
1021 {
1022 // tell the programmer that it didn't work
1023 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1024 return;
1025 }
1026 #endif
1027
1028 if (m_windowStyle & wxTE_MULTILINE)
1029 {
1030 #ifdef __WXGTK20__
1031 GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1032
1033 GtkTextIter fromi, toi;
1034 gtk_text_buffer_get_iter_at_offset( buf, &fromi, from );
1035 gtk_text_buffer_get_iter_at_offset( buf, &toi, to );
1036
1037 gtk_text_buffer_place_cursor( buf, &toi );
1038 gtk_text_buffer_move_mark_by_name( buf, "selection_bound", &fromi );
1039 #else
1040 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1041 #endif
1042 }
1043 else
1044 {
1045 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1046 }
1047 }
1048
1049 void wxTextCtrl::ShowPosition( long pos )
1050 {
1051 if (m_windowStyle & wxTE_MULTILINE)
1052 {
1053 #ifdef __WXGTK20__
1054 GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1055 GtkTextIter iter;
1056 gtk_text_buffer_get_start_iter( buf, &iter );
1057 gtk_text_iter_set_offset( &iter, pos );
1058 GtkTextMark *mark = gtk_text_buffer_create_mark( buf, NULL, &iter, TRUE );
1059 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1060 #else // GTK 1.x
1061 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1062 float totalLines = (float) GetNumberOfLines();
1063 long posX;
1064 long posY;
1065 PositionToXY(pos, &posX, &posY);
1066 float posLine = (float) posY;
1067 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1068 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
1069 #endif // GTK 1.x/2.x
1070 }
1071 }
1072
1073 #ifdef __WXGTK20__
1074
1075 wxTextCtrlHitTestResult
1076 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1077 {
1078 if ( !IsMultiLine() )
1079 {
1080 // not supported
1081 return wxTE_HT_UNKNOWN;
1082 }
1083
1084 int x, y;
1085 gtk_text_view_window_to_buffer_coords
1086 (
1087 GTK_TEXT_VIEW(m_text),
1088 GTK_TEXT_WINDOW_TEXT,
1089 pt.x, pt.y,
1090 &x, &y
1091 );
1092
1093 GtkTextIter iter;
1094 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1095 if ( pos )
1096 *pos = gtk_text_iter_get_offset(&iter);
1097
1098 return wxTE_HT_ON_TEXT;
1099 }
1100
1101 #endif // __WXGTK20__
1102
1103 long wxTextCtrl::GetInsertionPoint() const
1104 {
1105 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1106
1107 #ifdef __WXGTK20__
1108 if (m_windowStyle & wxTE_MULTILINE)
1109 {
1110 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1111
1112 // There is no direct accessor for the cursor, but
1113 // internally, the cursor is the "mark" called
1114 // "insert" in the text view's btree structure.
1115
1116 GtkTextMark *mark = gtk_text_buffer_get_insert( text_buffer );
1117 GtkTextIter cursor;
1118 gtk_text_buffer_get_iter_at_mark( text_buffer, &cursor, mark );
1119
1120 return gtk_text_iter_get_offset( &cursor );
1121 }
1122 else
1123 #endif
1124 {
1125 return (long) GET_EDITABLE_POS(m_text);
1126 }
1127 }
1128
1129 long wxTextCtrl::GetLastPosition() const
1130 {
1131 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1132
1133 int pos = 0;
1134
1135 if (m_windowStyle & wxTE_MULTILINE)
1136 {
1137 #ifdef __WXGTK20__
1138 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1139 GtkTextIter end;
1140 gtk_text_buffer_get_end_iter( text_buffer, &end );
1141
1142 pos = gtk_text_iter_get_offset( &end );
1143 #else
1144 pos = gtk_text_get_length( GTK_TEXT(m_text) );
1145 #endif
1146 }
1147 else
1148 {
1149 pos = GTK_ENTRY(m_text)->text_length;
1150 }
1151
1152 return (long)pos;
1153 }
1154
1155 void wxTextCtrl::Remove( long from, long to )
1156 {
1157 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1158
1159 #ifdef __WXGTK20__
1160 if (m_windowStyle & wxTE_MULTILINE)
1161 {
1162 GtkTextBuffer *
1163 text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1164
1165 GtkTextIter fromi, toi;
1166 gtk_text_buffer_get_iter_at_offset( text_buffer, &fromi, from );
1167 gtk_text_buffer_get_iter_at_offset( text_buffer, &toi, to );
1168
1169 gtk_text_buffer_delete( text_buffer, &fromi, &toi );
1170 }
1171 else // single line
1172 #endif
1173 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1174 }
1175
1176 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1177 {
1178 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1179
1180 Remove( from, to );
1181
1182 if (!value.IsEmpty())
1183 {
1184 #ifdef __WXGTK20__
1185 SetInsertionPoint( from );
1186 WriteText( value );
1187 #else // GTK 1.x
1188 gint pos = (gint)from;
1189 #if wxUSE_UNICODE
1190 wxWX2MBbuf buf = value.mbc_str();
1191 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
1192 #else
1193 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
1194 #endif // wxUSE_UNICODE
1195 #endif // GTK 1.x/2.x
1196 }
1197 }
1198
1199 void wxTextCtrl::Cut()
1200 {
1201 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1202
1203 #ifdef __WXGTK20__
1204 if (m_windowStyle & wxTE_MULTILINE)
1205 g_signal_emit_by_name(m_text, "cut-clipboard");
1206 else
1207 #endif
1208 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1209 }
1210
1211 void wxTextCtrl::Copy()
1212 {
1213 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1214
1215 #ifdef __WXGTK20__
1216 if (m_windowStyle & wxTE_MULTILINE)
1217 g_signal_emit_by_name(m_text, "copy-clipboard");
1218 else
1219 #endif
1220 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1221 }
1222
1223 void wxTextCtrl::Paste()
1224 {
1225 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1226
1227 #ifdef __WXGTK20__
1228 if (m_windowStyle & wxTE_MULTILINE)
1229 g_signal_emit_by_name(m_text, "paste-clipboard");
1230 else
1231 #endif
1232 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1233 }
1234
1235 // Undo/redo
1236 void wxTextCtrl::Undo()
1237 {
1238 // TODO
1239 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1240 }
1241
1242 void wxTextCtrl::Redo()
1243 {
1244 // TODO
1245 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1246 }
1247
1248 bool wxTextCtrl::CanUndo() const
1249 {
1250 // TODO
1251 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1252 return FALSE;
1253 }
1254
1255 bool wxTextCtrl::CanRedo() const
1256 {
1257 // TODO
1258 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1259 return FALSE;
1260 }
1261
1262 // If the return values from and to are the same, there is no
1263 // selection.
1264 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1265 {
1266 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1267
1268 gint from = -1;
1269 gint to = -1;
1270 bool haveSelection = FALSE;
1271
1272 #ifdef __WXGTK20__
1273 if (m_windowStyle & wxTE_MULTILINE)
1274 {
1275 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (m_text));
1276 GtkTextIter ifrom, ito;
1277 if ( gtk_text_buffer_get_selection_bounds(buffer, &ifrom, &ito) )
1278 {
1279 haveSelection = TRUE;
1280 from = gtk_text_iter_get_offset(&ifrom);
1281 to = gtk_text_iter_get_offset(&ito);
1282 }
1283 }
1284 else // not multi-line
1285 {
1286 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1287 &from, &to) )
1288 {
1289 haveSelection = TRUE;
1290 }
1291 }
1292 #else // not GTK2
1293 if ( (GTK_EDITABLE(m_text)->has_selection) )
1294 {
1295 haveSelection = TRUE;
1296 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1297 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1298 }
1299 #endif
1300
1301 if (! haveSelection )
1302 from = to = GetInsertionPoint();
1303
1304 if ( from > to )
1305 {
1306 // exchange them to be compatible with wxMSW
1307 gint tmp = from;
1308 from = to;
1309 to = tmp;
1310 }
1311
1312 if ( fromOut )
1313 *fromOut = from;
1314 if ( toOut )
1315 *toOut = to;
1316 }
1317
1318
1319 bool wxTextCtrl::IsEditable() const
1320 {
1321 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1322
1323 #ifdef __WXGTK20__
1324 if (m_windowStyle & wxTE_MULTILINE)
1325 {
1326 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1327 }
1328 else
1329 {
1330 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1331 }
1332 #else
1333 return GTK_EDITABLE(m_text)->editable;
1334 #endif
1335 }
1336
1337 bool wxTextCtrl::IsModified() const
1338 {
1339 return m_modified;
1340 }
1341
1342 void wxTextCtrl::Clear()
1343 {
1344 SetValue( wxT("") );
1345 }
1346
1347 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1348 {
1349 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1350
1351 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1352 {
1353 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1354 event.SetEventObject(this);
1355 event.SetString(GetValue());
1356 if (GetEventHandler()->ProcessEvent(event)) return;
1357 }
1358
1359 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1360 {
1361 // This will invoke the dialog default action, such
1362 // as the clicking the default button.
1363
1364 wxWindow *top_frame = m_parent;
1365 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1366 top_frame = top_frame->GetParent();
1367
1368 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1369 {
1370 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1371
1372 if (window->default_widget)
1373 {
1374 gtk_widget_activate (window->default_widget);
1375 return;
1376 }
1377 }
1378 }
1379
1380 key_event.Skip();
1381 }
1382
1383 GtkWidget* wxTextCtrl::GetConnectWidget()
1384 {
1385 return GTK_WIDGET(m_text);
1386 }
1387
1388 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1389 {
1390 if (m_windowStyle & wxTE_MULTILINE)
1391 {
1392 #ifdef __WXGTK20__
1393 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1394 #else
1395 return (window == GTK_TEXT(m_text)->text_area);
1396 #endif
1397 }
1398 else
1399 {
1400 return (window == GTK_ENTRY(m_text)->text_area);
1401 }
1402 }
1403
1404 // the font will change for subsequent text insertiongs
1405 bool wxTextCtrl::SetFont( const wxFont &font )
1406 {
1407 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1408
1409 if ( !wxTextCtrlBase::SetFont(font) )
1410 {
1411 // font didn't change, nothing to do
1412 return FALSE;
1413 }
1414
1415 if ( m_windowStyle & wxTE_MULTILINE )
1416 {
1417 SetUpdateFont(TRUE);
1418
1419 m_defaultStyle.SetFont(font);
1420
1421 ChangeFontGlobally();
1422 }
1423
1424 return TRUE;
1425 }
1426
1427 void wxTextCtrl::ChangeFontGlobally()
1428 {
1429 // this method is very inefficient and hence should be called as rarely as
1430 // possible!
1431 //
1432 // TODO: it can be implemented much more efficiently for GTK2
1433 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE)
1434 #ifndef __WXGTK20__
1435 && m_updateFont
1436 #endif // GTK+ 1.x
1437 ,
1438 _T("shouldn't be called for single line controls") );
1439
1440 wxString value = GetValue();
1441 if ( !value.IsEmpty() )
1442 {
1443 SetUpdateFont(FALSE);
1444
1445 Clear();
1446 AppendText(value);
1447 }
1448 }
1449
1450 #ifndef __WXGTK20__
1451
1452 void wxTextCtrl::UpdateFontIfNeeded()
1453 {
1454 if ( m_updateFont )
1455 ChangeFontGlobally();
1456 }
1457
1458 #endif // GTK+ 1.x
1459
1460 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1461 {
1462 if ( !wxControl::SetForegroundColour(colour) )
1463 return FALSE;
1464
1465 // update default fg colour too
1466 m_defaultStyle.SetTextColour(colour);
1467
1468 return TRUE;
1469 }
1470
1471 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1472 {
1473 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1474
1475 if ( !wxControl::SetBackgroundColour( colour ) )
1476 return FALSE;
1477
1478 if (!m_widget->window)
1479 return FALSE;
1480
1481 if (!m_backgroundColour.Ok())
1482 return FALSE;
1483
1484 if (m_windowStyle & wxTE_MULTILINE)
1485 {
1486 #ifndef __WXGTK20__
1487 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1488 if (!window)
1489 return FALSE;
1490 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1491 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1492 gdk_window_clear( window );
1493 #endif
1494 }
1495
1496 // change active background color too
1497 m_defaultStyle.SetBackgroundColour( colour );
1498
1499 return TRUE;
1500 }
1501
1502 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1503 {
1504 if ( m_windowStyle & wxTE_MULTILINE )
1505 {
1506 if ( style.IsDefault() )
1507 {
1508 // nothing to do
1509 return TRUE;
1510 }
1511 #ifdef __WXGTK20__
1512 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1513 gint l = gtk_text_buffer_get_char_count( text_buffer );
1514
1515 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1516 _T("invalid range in wxTextCtrl::SetStyle") );
1517
1518 GtkTextIter starti, endi;
1519 gtk_text_buffer_get_iter_at_offset( text_buffer, &starti, start );
1520 gtk_text_buffer_get_iter_at_offset( text_buffer, &endi, end );
1521
1522 // use the attributes from style which are set in it and fall back
1523 // first to the default style and then to the text control default
1524 // colours for the others
1525 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1526
1527 PangoFontDescription *font_description = attr.HasFont()
1528 ? attr.GetFont().GetNativeFontInfo()->description
1529 : NULL;
1530
1531 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
1532 : NULL;
1533
1534 GdkColor *colBg = attr.HasBackgroundColour()
1535 ? attr.GetBackgroundColour().GetColor()
1536 : NULL;
1537
1538 GtkTextTag *tag;
1539 tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description,
1540 "foreground-gdk", colFg,
1541 "background-gdk", colBg, NULL );
1542 gtk_text_buffer_apply_tag( text_buffer, tag, &starti, &endi );
1543
1544 return TRUE;
1545 #else
1546 // VERY dirty way to do that - removes the required text and re-adds it
1547 // with styling (FIXME)
1548
1549 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1550
1551 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1552 _T("invalid range in wxTextCtrl::SetStyle") );
1553
1554 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1555 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1556 wxString tmp(text,*wxConvCurrent);
1557 g_free( text );
1558
1559 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1560 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1561
1562 #if wxUSE_UNICODE
1563 wxWX2MBbuf buf = tmp.mbc_str();
1564 const char *txt = buf;
1565 size_t txtlen = strlen(buf);
1566 #else
1567 const char *txt = tmp;
1568 size_t txtlen = tmp.length();
1569 #endif
1570
1571 // use the attributes from style which are set in it and fall back
1572 // first to the default style and then to the text control default
1573 // colours for the others
1574 wxGtkTextInsert(m_text,
1575 wxTextAttr::Combine(style, m_defaultStyle, this),
1576 txt,
1577 txtlen);
1578
1579 /* does not seem to help under GTK+ 1.2 !!!
1580 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1581 SetInsertionPoint( old_pos );
1582 #endif
1583 return TRUE;
1584 }
1585 else // singe line
1586 {
1587 // cannot do this for GTK+'s Entry widget
1588 return FALSE;
1589 }
1590 }
1591
1592 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1593 {
1594 gtk_widget_modify_style(m_text, style);
1595 }
1596
1597 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1598 {
1599 Cut();
1600 }
1601
1602 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1603 {
1604 Copy();
1605 }
1606
1607 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1608 {
1609 Paste();
1610 }
1611
1612 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1613 {
1614 Undo();
1615 }
1616
1617 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1618 {
1619 Redo();
1620 }
1621
1622 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1623 {
1624 event.Enable( CanCut() );
1625 }
1626
1627 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1628 {
1629 event.Enable( CanCopy() );
1630 }
1631
1632 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1633 {
1634 event.Enable( CanPaste() );
1635 }
1636
1637 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1638 {
1639 event.Enable( CanUndo() );
1640 }
1641
1642 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1643 {
1644 event.Enable( CanRedo() );
1645 }
1646
1647 void wxTextCtrl::OnInternalIdle()
1648 {
1649 wxCursor cursor = m_cursor;
1650 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1651
1652 if (cursor.Ok())
1653 {
1654 #ifndef __WXGTK20__
1655 GdkWindow *window = (GdkWindow*) NULL;
1656 if (HasFlag(wxTE_MULTILINE))
1657 window = GTK_TEXT(m_text)->text_area;
1658 else
1659 window = GTK_ENTRY(m_text)->text_area;
1660
1661 if (window)
1662 gdk_window_set_cursor( window, cursor.GetCursor() );
1663
1664 if (!g_globalCursor.Ok())
1665 cursor = *wxSTANDARD_CURSOR;
1666
1667 window = m_widget->window;
1668 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1669 gdk_window_set_cursor( window, cursor.GetCursor() );
1670 #endif
1671 }
1672
1673 if (g_delayedFocus == this)
1674 {
1675 if (GTK_WIDGET_REALIZED(m_widget))
1676 {
1677 gtk_widget_grab_focus( m_widget );
1678 g_delayedFocus = NULL;
1679 }
1680 }
1681
1682 if (wxUpdateUIEvent::CanUpdate(this))
1683 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1684 }
1685
1686 wxSize wxTextCtrl::DoGetBestSize() const
1687 {
1688 // FIXME should be different for multi-line controls...
1689 wxSize ret( wxControl::DoGetBestSize() );
1690 wxSize best(80, ret.y);
1691 CacheBestSize(best);
1692 return best;
1693 }
1694
1695 // ----------------------------------------------------------------------------
1696 // freeze/thaw
1697 // ----------------------------------------------------------------------------
1698
1699 void wxTextCtrl::Freeze()
1700 {
1701 #ifndef __WXGTK20__
1702 if ( HasFlag(wxTE_MULTILINE) )
1703 {
1704 gtk_text_freeze(GTK_TEXT(m_text));
1705 }
1706 #endif
1707 }
1708
1709 void wxTextCtrl::Thaw()
1710 {
1711 #ifndef __WXGTK20__
1712 if ( HasFlag(wxTE_MULTILINE) )
1713 {
1714 GTK_TEXT(m_text)->vadj->value = 0.0;
1715
1716 gtk_text_thaw(GTK_TEXT(m_text));
1717 }
1718 #endif
1719 }
1720
1721 // ----------------------------------------------------------------------------
1722 // scrolling
1723 // ----------------------------------------------------------------------------
1724
1725 GtkAdjustment *wxTextCtrl::GetVAdj() const
1726 {
1727 if ( !IsMultiLine() )
1728 return NULL;
1729
1730 #ifdef __WXGTK20__
1731 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
1732 #else
1733 return GTK_TEXT(m_text)->vadj;
1734 #endif
1735 }
1736
1737 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1738 {
1739 float value = adj->value + diff;
1740
1741 if ( value < 0 )
1742 value = 0;
1743
1744 float upper = adj->upper - adj->page_size;
1745 if ( value > upper )
1746 value = upper;
1747
1748 // did we noticeably change the scroll position?
1749 if ( fabs(adj->value - value) < 0.2 )
1750 {
1751 // well, this is what Robert does in wxScrollBar, so it must be good...
1752 return FALSE;
1753 }
1754
1755 adj->value = value;
1756
1757 #ifdef __WXGTK20__
1758 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
1759 #else
1760 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1761 #endif
1762
1763 return TRUE;
1764 }
1765
1766 bool wxTextCtrl::ScrollLines(int lines)
1767 {
1768 GtkAdjustment *adj = GetVAdj();
1769 if ( !adj )
1770 return FALSE;
1771
1772 #ifdef __WXGTK20__
1773 int diff = (int)ceil(lines*adj->step_increment);
1774 #else
1775 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1776 int diff = 10*lines;
1777 #endif
1778
1779 return DoScroll(adj, diff);
1780 }
1781
1782 bool wxTextCtrl::ScrollPages(int pages)
1783 {
1784 GtkAdjustment *adj = GetVAdj();
1785 if ( !adj )
1786 return FALSE;
1787
1788 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1789 }
1790
1791
1792 // static
1793 wxVisualAttributes
1794 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1795 {
1796 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1797 }