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