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