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