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