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