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