]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/textctrl.cpp
Added overloaded AddChild from contributor
[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#ifdef __GNUG__
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
15#include "wx/utils.h"
16#include "wx/intl.h"
17#include "wx/log.h"
18#include "wx/settings.h"
19#include "wx/panel.h"
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <ctype.h>
24#include <math.h> // for fabs
25
26// TODO: reimplement wxTextCtrl using GtkTextView
27#ifdef __WXGTK20__
28 #define GTK_ENABLE_BROKEN // need this to get GtkText at all
29#endif // __WXGTK20__
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
53static void wxGtkTextInsert(GtkWidget *text,
54 const wxTextAttr& attr,
55 const char *txt,
56 size_t len)
57{
58 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
59 : NULL;
60
61 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
62 : NULL;
63
64 GdkColor *colBg = attr.HasBackgroundColour()
65 ? attr.GetBackgroundColour().GetColor()
66 : NULL;
67
68 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
69}
70
71// ----------------------------------------------------------------------------
72// "insert_text" for GtkEntry
73// ----------------------------------------------------------------------------
74
75static void
76gtk_insert_text_callback(GtkEditable *editable,
77 const gchar *new_text,
78 gint new_text_length,
79 gint *position,
80 wxTextCtrl *win)
81{
82 if (g_isIdle)
83 wxapp_install_idle_handler();
84
85 // we should only be called if we have a max len limit at all
86 GtkEntry *entry = GTK_ENTRY (editable);
87
88 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
89
90 // check that we don't overflow the max length limit
91 //
92 // FIXME: this doesn't work when we paste a string which is going to be
93 // truncated
94 if ( entry->text_length == entry->text_max_length )
95 {
96 // we don't need to run the base class version at all
97 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
98
99 // remember that the next changed signal is to be ignored to avoid
100 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
101 win->IgnoreNextTextUpdate();
102
103 // and generate the correct one ourselves
104 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
105 event.SetEventObject(win);
106 event.SetString(win->GetValue());
107 win->GetEventHandler()->ProcessEvent( event );
108 }
109}
110
111//-----------------------------------------------------------------------------
112// "changed"
113//-----------------------------------------------------------------------------
114
115static void
116gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
117{
118 if ( win->IgnoreTextUpdate() )
119 return;
120
121 if (!win->m_hasVMT) return;
122
123 if (g_isIdle)
124 wxapp_install_idle_handler();
125
126 win->SetModified();
127 win->UpdateFontIfNeeded();
128
129 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
130 event.SetEventObject( win );
131 event.SetString( win->GetValue() );
132 win->GetEventHandler()->ProcessEvent( event );
133}
134
135//-----------------------------------------------------------------------------
136// "changed" from vertical scrollbar
137//-----------------------------------------------------------------------------
138
139static void
140gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
141{
142 if (!win->m_hasVMT) return;
143
144 if (g_isIdle)
145 wxapp_install_idle_handler();
146
147 win->CalculateScrollbar();
148}
149
150// ----------------------------------------------------------------------------
151// redraw callback for multiline text
152// ----------------------------------------------------------------------------
153
154#ifndef __WXGTK20__
155
156// redrawing a GtkText from inside a wxYield() call results in crashes (the
157// text sample shows it in its "Add lines" command which shows wxProgressDialog
158// which implicitly calls wxYield()) so we override GtkText::draw() and simply
159// don't do anything if we're inside wxYield()
160
161extern bool wxIsInsideYield;
162
163typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
164
165static GtkDrawCallback gs_gtk_text_draw = NULL;
166
167extern "C"
168void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
169{
170 if ( !wxIsInsideYield )
171 {
172 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
173 _T("infinite recursion in wxgtk_text_draw aborted") );
174
175 gs_gtk_text_draw(widget, rect);
176 }
177}
178
179#endif // __WXGTK20__
180
181//-----------------------------------------------------------------------------
182// wxTextCtrl
183//-----------------------------------------------------------------------------
184
185IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
186
187BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
188 EVT_CHAR(wxTextCtrl::OnChar)
189
190 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
191 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
192 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
193 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
194 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
195
196 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
197 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
198 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
199 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
200 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
201END_EVENT_TABLE()
202
203void wxTextCtrl::Init()
204{
205 m_ignoreNextUpdate =
206 m_modified = FALSE;
207 m_updateFont = FALSE;
208 m_text =
209 m_vScrollbar = (GtkWidget *)NULL;
210}
211
212wxTextCtrl::wxTextCtrl( wxWindow *parent,
213 wxWindowID id,
214 const wxString &value,
215 const wxPoint &pos,
216 const wxSize &size,
217 long style,
218 const wxValidator& validator,
219 const wxString &name )
220{
221 Init();
222
223 Create( parent, id, value, pos, size, style, validator, name );
224}
225
226bool wxTextCtrl::Create( wxWindow *parent,
227 wxWindowID id,
228 const wxString &value,
229 const wxPoint &pos,
230 const wxSize &size,
231 long style,
232 const wxValidator& validator,
233 const wxString &name )
234{
235 m_needParent = TRUE;
236 m_acceptsFocus = TRUE;
237
238 if (!PreCreation( parent, pos, size ) ||
239 !CreateBase( parent, id, pos, size, style, validator, name ))
240 {
241 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
242 return FALSE;
243 }
244
245
246 m_vScrollbarVisible = FALSE;
247
248 bool multi_line = (style & wxTE_MULTILINE) != 0;
249 if (multi_line)
250 {
251#ifdef __WXGTK13__
252 /* a multi-line edit control: create a vertical scrollbar by default and
253 horizontal if requested */
254 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
255#else
256 bool bHasHScrollbar = FALSE;
257#endif
258
259 /* create our control ... */
260 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
261
262 /* ... and put into the upper left hand corner of the table */
263 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
264 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
265 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
266 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
267 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
268 0, 0);
269
270 /* always wrap words */
271 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
272
273#ifdef __WXGTK13__
274 /* put the horizontal scrollbar in the lower left hand corner */
275 if (bHasHScrollbar)
276 {
277 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
278 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
279 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
280 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
281 GTK_FILL,
282 0, 0);
283 gtk_widget_show(hscrollbar);
284
285 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
286 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
287 }
288#endif
289
290 /* finally, put the vertical scrollbar in the upper right corner */
291 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
292 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
293 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
294 GTK_FILL,
295 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
296 0, 0);
297 }
298 else
299 {
300 /* a single-line text control: no need for scrollbars */
301 m_widget =
302 m_text = gtk_entry_new();
303 }
304
305 m_parent->DoAddChild( this );
306
307 m_focusWidget = m_text;
308
309 PostCreation();
310
311 SetFont( parent->GetFont() );
312
313 wxSize size_best( DoGetBestSize() );
314 wxSize new_size( size );
315 if (new_size.x == -1)
316 new_size.x = size_best.x;
317 if (new_size.y == -1)
318 new_size.y = size_best.y;
319 if ((new_size.x != size.x) || (new_size.y != size.y))
320 SetSize( new_size.x, new_size.y );
321
322 if (multi_line)
323 gtk_widget_show(m_text);
324
325 if (multi_line)
326 {
327 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
328 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
329
330#ifndef __WXGTK20__
331 // only initialize gs_gtk_text_draw once, starting from the next the
332 // klass::draw will already be wxgtk_text_draw
333 if ( !gs_gtk_text_draw )
334 {
335 GtkDrawCallback&
336 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
337
338 gs_gtk_text_draw = draw;
339
340 draw = wxgtk_text_draw;
341 }
342#endif // GTK+ 1.x
343 }
344
345 if (!value.IsEmpty())
346 {
347 gint tmp = 0;
348
349#if !GTK_CHECK_VERSION(1, 2, 0)
350 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
351 // gtk_editable_insert_text()
352 gtk_widget_realize(m_text);
353#endif // GTK 1.0
354
355#if wxUSE_UNICODE
356 wxWX2MBbuf val = value.mbc_str();
357 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
358#else // !Unicode
359 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
360#endif // Unicode/!Unicode
361
362 if (multi_line)
363 {
364 /* bring editable's cursor uptodate. bug in GTK. */
365 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
366 }
367 }
368
369 if (style & wxTE_PASSWORD)
370 {
371 if (!multi_line)
372 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
373 }
374
375 if (style & wxTE_READONLY)
376 {
377 if (!multi_line)
378 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
379 }
380 else
381 {
382 if (multi_line)
383 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
384 }
385
386 /* we want to be notified about text changes */
387 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
388 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
389
390 /* we don't set a valid background colour, because the window
391 manager should use a default one */
392 m_backgroundColour = wxColour();
393
394 wxColour colFg = parent->GetForegroundColour();
395 SetForegroundColour( colFg );
396
397 m_cursor = wxCursor( wxCURSOR_IBEAM );
398
399 wxTextAttr attrDef( colFg, m_backgroundColour, parent->GetFont() );
400 SetDefaultStyle( attrDef );
401
402 Show( TRUE );
403
404 return TRUE;
405}
406
407void wxTextCtrl::CalculateScrollbar()
408{
409 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
410
411 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
412
413 if (adj->upper - adj->page_size < 0.8)
414 {
415 if (m_vScrollbarVisible)
416 {
417 gtk_widget_hide( m_vScrollbar );
418 m_vScrollbarVisible = FALSE;
419 }
420 }
421 else
422 {
423 if (!m_vScrollbarVisible)
424 {
425 gtk_widget_show( m_vScrollbar );
426 m_vScrollbarVisible = TRUE;
427 }
428 }
429}
430
431wxString wxTextCtrl::GetValue() const
432{
433 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
434
435 wxString tmp;
436 if (m_windowStyle & wxTE_MULTILINE)
437 {
438 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
439 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
440 tmp = wxString(text,*wxConvCurrent);
441 g_free( text );
442 }
443 else
444 {
445 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
446 }
447 return tmp;
448}
449
450void wxTextCtrl::SetValue( const wxString &value )
451{
452 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
453
454 if (m_windowStyle & wxTE_MULTILINE)
455 {
456 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
457 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
458 len = 0;
459#if wxUSE_UNICODE
460 wxWX2MBbuf tmpbuf = value.mbc_str();
461 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
462#else
463 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
464#endif
465 }
466 else
467 {
468 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
469 }
470
471 // GRG, Jun/2000: Changed this after a lot of discussion in
472 // the lists. wxWindows 2.2 will have a set of flags to
473 // customize this behaviour.
474 SetInsertionPoint(0);
475
476 m_modified = FALSE;
477}
478
479void wxTextCtrl::WriteText( const wxString &text )
480{
481 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
482
483 if ( text.empty() )
484 return;
485
486#if wxUSE_UNICODE
487 wxWX2MBbuf buf = text.mbc_str();
488 const char *txt = buf;
489 size_t txtlen = strlen(buf);
490#else
491 const char *txt = text;
492 size_t txtlen = text.length();
493#endif
494
495 if ( m_windowStyle & wxTE_MULTILINE )
496 {
497 // After cursor movements, gtk_text_get_point() is wrong by one.
498 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
499
500 // always use m_defaultStyle, even if it is empty as otherwise
501 // resetting the style and appending some more text wouldn't work: if
502 // we don't specify the style explicitly, the old style would be used
503 wxGtkTextInsert(m_text, m_defaultStyle, txt, txtlen);
504
505 // Bring editable's cursor back uptodate.
506 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
507 }
508 else // single line
509 {
510 // This moves the cursor pos to behind the inserted text.
511 gint len = GET_EDITABLE_POS(m_text);
512 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
513
514 // Bring editable's cursor uptodate.
515 len += text.Len();
516
517 // Bring entry's cursor uptodate.
518 gtk_entry_set_position( GTK_ENTRY(m_text), len );
519 }
520
521 m_modified = TRUE;
522}
523
524void wxTextCtrl::AppendText( const wxString &text )
525{
526 SetInsertionPointEnd();
527 WriteText( text );
528}
529
530wxString wxTextCtrl::GetLineText( long lineNo ) const
531{
532 if (m_windowStyle & wxTE_MULTILINE)
533 {
534 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
535 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
536
537 if (text)
538 {
539 wxString buf(wxT(""));
540 long i;
541 int currentLine = 0;
542 for (i = 0; currentLine != lineNo && text[i]; i++ )
543 if (text[i] == '\n')
544 currentLine++;
545 // Now get the text
546 int j;
547 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
548 buf += text[i];
549
550 g_free( text );
551 return buf;
552 }
553 else
554 {
555 return wxEmptyString;
556 }
557 }
558 else
559 {
560 if (lineNo == 0) return GetValue();
561 return wxEmptyString;
562 }
563}
564
565void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
566{
567 /* If you implement this, don't forget to update the documentation!
568 * (file docs/latex/wx/text.tex) */
569 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
570}
571
572bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
573{
574 if ( m_windowStyle & wxTE_MULTILINE )
575 {
576 wxString text = GetValue();
577
578 // cast to prevent warning. But pos really should've been unsigned.
579 if( (unsigned long)pos > text.Len() )
580 return FALSE;
581
582 *x=0; // First Col
583 *y=0; // First Line
584
585 const wxChar* stop = text.c_str() + pos;
586 for ( const wxChar *p = text.c_str(); p < stop; p++ )
587 {
588 if (*p == wxT('\n'))
589 {
590 (*y)++;
591 *x=0;
592 }
593 else
594 (*x)++;
595 }
596 }
597 else // single line control
598 {
599 if ( pos <= GTK_ENTRY(m_text)->text_length )
600 {
601 *y = 0;
602 *x = pos;
603 }
604 else
605 {
606 // index out of bounds
607 return FALSE;
608 }
609 }
610
611 return TRUE;
612}
613
614long wxTextCtrl::XYToPosition(long x, long y ) const
615{
616 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
617
618 long pos=0;
619 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
620
621 pos += x;
622 return pos;
623}
624
625int wxTextCtrl::GetLineLength(long lineNo) const
626{
627 wxString str = GetLineText (lineNo);
628 return (int) str.Length();
629}
630
631int wxTextCtrl::GetNumberOfLines() const
632{
633 if (m_windowStyle & wxTE_MULTILINE)
634 {
635 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
636 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
637
638 if (text)
639 {
640 int currentLine = 0;
641 for (int i = 0; i < len; i++ )
642 {
643 if (text[i] == '\n')
644 currentLine++;
645 }
646 g_free( text );
647
648 // currentLine is 0 based, add 1 to get number of lines
649 return currentLine + 1;
650 }
651 else
652 {
653 return 0;
654 }
655 }
656 else
657 {
658 return 1;
659 }
660}
661
662void wxTextCtrl::SetInsertionPoint( long pos )
663{
664 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
665
666 if (m_windowStyle & wxTE_MULTILINE)
667 {
668 /* seems to be broken in GTK 1.0.X:
669 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
670
671 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
672 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
673
674 /* we fake a set_point by inserting and deleting. as the user
675 isn't supposed to get to know about thos non-sense, we
676 disconnect so that no events are sent to the user program. */
677
678 gint tmp = (gint)pos;
679 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
680 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
681
682 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
683 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
684
685 /* bring editable's cursor uptodate. another bug in GTK. */
686
687 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
688 }
689 else
690 {
691 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
692
693 /* bring editable's cursor uptodate. bug in GTK. */
694
695 SET_EDITABLE_POS(m_text, (guint32)pos);
696 }
697}
698
699void wxTextCtrl::SetInsertionPointEnd()
700{
701 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
702
703 if (m_windowStyle & wxTE_MULTILINE)
704 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
705 else
706 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
707}
708
709void wxTextCtrl::SetEditable( bool editable )
710{
711 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
712
713 if (m_windowStyle & wxTE_MULTILINE)
714 gtk_text_set_editable( GTK_TEXT(m_text), editable );
715 else
716 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
717}
718
719bool wxTextCtrl::Enable( bool enable )
720{
721 if (!wxWindowBase::Enable(enable))
722 {
723 // nothing to do
724 return FALSE;
725 }
726
727 if (m_windowStyle & wxTE_MULTILINE)
728 {
729 gtk_text_set_editable( GTK_TEXT(m_text), enable );
730 OnParentEnable(enable);
731 }
732 else
733 {
734 gtk_widget_set_sensitive( m_text, enable );
735 }
736
737 return TRUE;
738}
739
740// wxGTK-specific: called recursively by Enable,
741// to give widgets an oppprtunity to correct their colours after they
742// have been changed by Enable
743void wxTextCtrl::OnParentEnable( bool enable )
744{
745 // If we have a custom background colour, we use this colour in both
746 // disabled and enabled mode, or we end up with a different colour under the
747 // text.
748 wxColour oldColour = GetBackgroundColour();
749 if (oldColour.Ok())
750 {
751 // Need to set twice or it'll optimize the useful stuff out
752 if (oldColour == * wxWHITE)
753 SetBackgroundColour(*wxBLACK);
754 else
755 SetBackgroundColour(*wxWHITE);
756 SetBackgroundColour(oldColour);
757 }
758}
759
760void wxTextCtrl::DiscardEdits()
761{
762 m_modified = FALSE;
763}
764
765// ----------------------------------------------------------------------------
766// max text length support
767// ----------------------------------------------------------------------------
768
769void wxTextCtrl::IgnoreNextTextUpdate()
770{
771 m_ignoreNextUpdate = TRUE;
772}
773
774bool wxTextCtrl::IgnoreTextUpdate()
775{
776 if ( m_ignoreNextUpdate )
777 {
778 m_ignoreNextUpdate = FALSE;
779
780 return TRUE;
781 }
782
783 return FALSE;
784}
785
786void wxTextCtrl::SetMaxLength(unsigned long len)
787{
788 if ( !HasFlag(wxTE_MULTILINE) )
789 {
790 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
791
792 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
793 // we had tried to enter more text than allowed by max text length and
794 // the text wasn't really changed
795 //
796 // to detect this and generate TEXT_MAXLEN event instead of
797 // TEXT_CHANGED one in this case we also catch "insert_text" signal
798 //
799 // when max len is set to 0 we disconnect our handler as it means that
800 // we shouldn't check anything any more
801 if ( len )
802 {
803 gtk_signal_connect( GTK_OBJECT(m_text),
804 "insert_text",
805 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
806 (gpointer)this);
807 }
808 else // no checking
809 {
810 gtk_signal_disconnect_by_func
811 (
812 GTK_OBJECT(m_text),
813 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
814 (gpointer)this
815 );
816 }
817 }
818}
819
820void wxTextCtrl::SetSelection( long from, long to )
821{
822 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
823
824 if ( (m_windowStyle & wxTE_MULTILINE) &&
825 !GTK_TEXT(m_text)->line_start_cache )
826 {
827 // tell the programmer that it didn't work
828 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
829 return;
830 }
831
832 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
833}
834
835void wxTextCtrl::ShowPosition( long pos )
836{
837 if (m_windowStyle & wxTE_MULTILINE)
838 {
839 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
840 float totalLines = (float) GetNumberOfLines();
841 long posX;
842 long posY;
843 PositionToXY(pos, &posX, &posY);
844 float posLine = (float) posY;
845 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
846 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
847 }
848}
849
850long wxTextCtrl::GetInsertionPoint() const
851{
852 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
853
854 return (long) GET_EDITABLE_POS(m_text);
855}
856
857long wxTextCtrl::GetLastPosition() const
858{
859 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
860
861 int pos = 0;
862 if (m_windowStyle & wxTE_MULTILINE)
863 pos = gtk_text_get_length( GTK_TEXT(m_text) );
864 else
865 pos = GTK_ENTRY(m_text)->text_length;
866
867 return (long)pos;
868}
869
870void wxTextCtrl::Remove( long from, long to )
871{
872 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
873
874 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
875}
876
877void wxTextCtrl::Replace( long from, long to, const wxString &value )
878{
879 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
880
881 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
882
883 if (!value.IsEmpty())
884 {
885 gint pos = (gint)from;
886#if wxUSE_UNICODE
887 wxWX2MBbuf buf = value.mbc_str();
888 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
889#else
890 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
891#endif
892 }
893}
894
895void wxTextCtrl::Cut()
896{
897 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
898
899 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
900}
901
902void wxTextCtrl::Copy()
903{
904 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
905
906 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
907}
908
909void wxTextCtrl::Paste()
910{
911 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
912
913 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
914}
915
916// Undo/redo
917void wxTextCtrl::Undo()
918{
919 // TODO
920 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
921}
922
923void wxTextCtrl::Redo()
924{
925 // TODO
926 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
927}
928
929bool wxTextCtrl::CanUndo() const
930{
931 // TODO
932 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
933 return FALSE;
934}
935
936bool wxTextCtrl::CanRedo() const
937{
938 // TODO
939 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
940 return FALSE;
941}
942
943// If the return values from and to are the same, there is no
944// selection.
945void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
946{
947 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
948
949 gint from, to;
950#ifdef __WXGTK20__
951 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text), &from, &to) )
952#else
953 if ( !(GTK_EDITABLE(m_text)->has_selection) )
954#endif
955 {
956 from =
957 to = GetInsertionPoint();
958 }
959 else // got selection
960 {
961#ifndef __WXGTK20__
962 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
963 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
964#endif
965
966 if ( from > to )
967 {
968 // exchange them to be compatible with wxMSW
969 gint tmp = from;
970 from = to;
971 to = tmp;
972 }
973 }
974
975 if ( fromOut )
976 *fromOut = from;
977 if ( toOut )
978 *toOut = to;
979}
980
981bool wxTextCtrl::IsEditable() const
982{
983 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
984
985#ifdef __WXGTK20__
986 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
987#else
988 return GTK_EDITABLE(m_text)->editable;
989#endif
990}
991
992bool wxTextCtrl::IsModified() const
993{
994 return m_modified;
995}
996
997void wxTextCtrl::Clear()
998{
999 SetValue( wxT("") );
1000}
1001
1002void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1003{
1004 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1005
1006 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1007 {
1008 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1009 event.SetEventObject(this);
1010 event.SetString(GetValue());
1011 if (GetEventHandler()->ProcessEvent(event)) return;
1012 }
1013
1014 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1015 {
1016 // This will invoke the dialog default action, such
1017 // as the clicking the default button.
1018
1019 wxWindow *top_frame = m_parent;
1020 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1021 top_frame = top_frame->GetParent();
1022
1023 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1024 {
1025 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1026
1027 if (window->default_widget)
1028 {
1029 gtk_widget_activate (window->default_widget);
1030 return;
1031 }
1032 }
1033 }
1034
1035 key_event.Skip();
1036}
1037
1038GtkWidget* wxTextCtrl::GetConnectWidget()
1039{
1040 return GTK_WIDGET(m_text);
1041}
1042
1043bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1044{
1045 if (m_windowStyle & wxTE_MULTILINE)
1046 return (window == GTK_TEXT(m_text)->text_area);
1047 else
1048 return (window == GTK_ENTRY(m_text)->text_area);
1049}
1050
1051// the font will change for subsequent text insertiongs
1052bool wxTextCtrl::SetFont( const wxFont &font )
1053{
1054 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1055
1056 if ( !wxTextCtrlBase::SetFont(font) )
1057 {
1058 // font didn't change, nothing to do
1059 return FALSE;
1060 }
1061
1062 if ( m_windowStyle & wxTE_MULTILINE )
1063 {
1064 m_updateFont = TRUE;
1065
1066 m_defaultStyle.SetFont(font);
1067
1068 ChangeFontGlobally();
1069 }
1070
1071 return TRUE;
1072}
1073
1074void wxTextCtrl::ChangeFontGlobally()
1075{
1076 // this method is very inefficient and hence should be called as rarely as
1077 // possible!
1078 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1079 _T("shouldn't be called for single line controls") );
1080
1081 wxString value = GetValue();
1082 if ( !value.IsEmpty() )
1083 {
1084 m_updateFont = FALSE;
1085
1086 Clear();
1087 AppendText(value);
1088 }
1089}
1090
1091void wxTextCtrl::UpdateFontIfNeeded()
1092{
1093 if ( m_updateFont )
1094 ChangeFontGlobally();
1095}
1096
1097bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1098{
1099 if ( !wxControl::SetForegroundColour(colour) )
1100 return FALSE;
1101
1102 // update default fg colour too
1103 m_defaultStyle.SetTextColour(colour);
1104
1105 return TRUE;
1106}
1107
1108bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1109{
1110 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1111
1112 if ( !wxControl::SetBackgroundColour( colour ) )
1113 return FALSE;
1114
1115 if (!m_widget->window)
1116 return FALSE;
1117
1118 wxColour sysbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
1119 if (sysbg.Red() == colour.Red() &&
1120 sysbg.Green() == colour.Green() &&
1121 sysbg.Blue() == colour.Blue())
1122 {
1123 return FALSE; // FIXME or TRUE?
1124 }
1125
1126 if (!m_backgroundColour.Ok())
1127 return FALSE;
1128
1129 if (m_windowStyle & wxTE_MULTILINE)
1130 {
1131 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1132 if (!window)
1133 return FALSE;
1134 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1135 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1136 gdk_window_clear( window );
1137 }
1138
1139 // change active background color too
1140 m_defaultStyle.SetBackgroundColour( colour );
1141
1142 return TRUE;
1143}
1144
1145bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1146{
1147 /* VERY dirty way to do that - removes the required text and re-adds it
1148 with styling (FIXME) */
1149 if ( m_windowStyle & wxTE_MULTILINE )
1150 {
1151 if ( style.IsDefault() )
1152 {
1153 // nothing to do
1154 return TRUE;
1155 }
1156
1157 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1158
1159 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1160 _T("invalid range in wxTextCtrl::SetStyle") );
1161
1162 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1163 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1164 wxString tmp(text,*wxConvCurrent);
1165 g_free( text );
1166
1167 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1168 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1169
1170#if wxUSE_UNICODE
1171 wxWX2MBbuf buf = tmp.mbc_str();
1172 const char *txt = buf;
1173 size_t txtlen = strlen(buf);
1174#else
1175 const char *txt = tmp;
1176 size_t txtlen = tmp.length();
1177#endif
1178
1179 // use the attributes from style which are set in it and fall back
1180 // first to the default style and then to the text control default
1181 // colours for the others
1182 wxGtkTextInsert(m_text,
1183 wxTextAttr::Combine(style, m_defaultStyle, this),
1184 txt,
1185 txtlen);
1186
1187 /* does not seem to help under GTK+ 1.2 !!!
1188 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1189 SetInsertionPoint( old_pos );
1190 return TRUE;
1191 }
1192 else // singe line
1193 {
1194 // cannot do this for GTK+'s Entry widget
1195 return FALSE;
1196 }
1197}
1198
1199void wxTextCtrl::ApplyWidgetStyle()
1200{
1201 if (m_windowStyle & wxTE_MULTILINE)
1202 {
1203 // how ?
1204 }
1205 else
1206 {
1207 SetWidgetStyle();
1208 gtk_widget_set_style( m_text, m_widgetStyle );
1209 }
1210}
1211
1212void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1213{
1214 Cut();
1215}
1216
1217void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1218{
1219 Copy();
1220}
1221
1222void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1223{
1224 Paste();
1225}
1226
1227void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1228{
1229 Undo();
1230}
1231
1232void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1233{
1234 Redo();
1235}
1236
1237void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1238{
1239 event.Enable( CanCut() );
1240}
1241
1242void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1243{
1244 event.Enable( CanCopy() );
1245}
1246
1247void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1248{
1249 event.Enable( CanPaste() );
1250}
1251
1252void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1253{
1254 event.Enable( CanUndo() );
1255}
1256
1257void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1258{
1259 event.Enable( CanRedo() );
1260}
1261
1262void wxTextCtrl::OnInternalIdle()
1263{
1264 wxCursor cursor = m_cursor;
1265 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1266
1267 if (cursor.Ok())
1268 {
1269 GdkWindow *window = (GdkWindow*) NULL;
1270 if (HasFlag(wxTE_MULTILINE))
1271 window = GTK_TEXT(m_text)->text_area;
1272 else
1273 window = GTK_ENTRY(m_text)->text_area;
1274
1275 if (window)
1276 gdk_window_set_cursor( window, cursor.GetCursor() );
1277
1278 if (!g_globalCursor.Ok())
1279 cursor = *wxSTANDARD_CURSOR;
1280
1281 window = m_widget->window;
1282 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1283 gdk_window_set_cursor( window, cursor.GetCursor() );
1284 }
1285
1286 if (g_delayedFocus == this)
1287 {
1288 if (GTK_WIDGET_REALIZED(m_widget))
1289 {
1290 gtk_widget_grab_focus( m_widget );
1291 g_delayedFocus = NULL;
1292 }
1293 }
1294
1295 UpdateWindowUI();
1296}
1297
1298wxSize wxTextCtrl::DoGetBestSize() const
1299{
1300 // FIXME should be different for multi-line controls...
1301 wxSize ret( wxControl::DoGetBestSize() );
1302 return wxSize(80, ret.y);
1303}
1304
1305// ----------------------------------------------------------------------------
1306// freeze/thaw
1307// ----------------------------------------------------------------------------
1308
1309void wxTextCtrl::Freeze()
1310{
1311 if ( HasFlag(wxTE_MULTILINE) )
1312 {
1313 gtk_text_freeze(GTK_TEXT(m_text));
1314 }
1315}
1316
1317void wxTextCtrl::Thaw()
1318{
1319 if ( HasFlag(wxTE_MULTILINE) )
1320 {
1321 GTK_TEXT(m_text)->vadj->value = 0.0;
1322
1323 gtk_text_thaw(GTK_TEXT(m_text));
1324 }
1325}
1326
1327// ----------------------------------------------------------------------------
1328// scrolling
1329// ----------------------------------------------------------------------------
1330
1331GtkAdjustment *wxTextCtrl::GetVAdj() const
1332{
1333 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1334}
1335
1336bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1337{
1338 float value = adj->value + diff;
1339
1340 if ( value < 0 )
1341 value = 0;
1342
1343 float upper = adj->upper - adj->page_size;
1344 if ( value > upper )
1345 value = upper;
1346
1347 // did we noticeably change the scroll position?
1348 if ( fabs(adj->value - value) < 0.2 )
1349 {
1350 // well, this is what Robert does in wxScrollBar, so it must be good...
1351 return FALSE;
1352 }
1353
1354 adj->value = value;
1355
1356 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1357
1358 return TRUE;
1359}
1360
1361bool wxTextCtrl::ScrollLines(int lines)
1362{
1363 GtkAdjustment *adj = GetVAdj();
1364 if ( !adj )
1365 return FALSE;
1366
1367 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1368 static const int KEY_SCROLL_PIXELS = 10;
1369
1370 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1371}
1372
1373bool wxTextCtrl::ScrollPages(int pages)
1374{
1375 GtkAdjustment *adj = GetVAdj();
1376 if ( !adj )
1377 return FALSE;
1378
1379 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1380}
1381