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