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