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