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