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