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