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