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