]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
New module definition file for the ever chaning treectrl class interface.
[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 // FIXME: is the bg colour correct here?
329 wxTextAttr attrDef( colFg,
330 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW),
331 parent->GetFont() );
332 SetDefaultStyle( attrDef );
333
334 Show( TRUE );
335
336 return TRUE;
337 }
338
339 void wxTextCtrl::CalculateScrollbar()
340 {
341 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
342
343 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
344
345 if (adj->upper - adj->page_size < 0.8)
346 {
347 if (m_vScrollbarVisible)
348 {
349 gtk_widget_hide( m_vScrollbar );
350 m_vScrollbarVisible = FALSE;
351 }
352 }
353 else
354 {
355 if (!m_vScrollbarVisible)
356 {
357 gtk_widget_show( m_vScrollbar );
358 m_vScrollbarVisible = TRUE;
359 }
360 }
361 }
362
363 wxString wxTextCtrl::GetValue() const
364 {
365 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
366
367 wxString tmp;
368 if (m_windowStyle & wxTE_MULTILINE)
369 {
370 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
371 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
372 tmp = wxString(text,*wxConvCurrent);
373 g_free( text );
374 }
375 else
376 {
377 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
378 }
379 return tmp;
380 }
381
382 void wxTextCtrl::SetValue( const wxString &value )
383 {
384 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
385
386 if (m_windowStyle & wxTE_MULTILINE)
387 {
388 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
389 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
390 len = 0;
391 #if wxUSE_UNICODE
392 wxWX2MBbuf tmpbuf = value.mbc_str();
393 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
394 #else
395 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
396 #endif
397 }
398 else
399 {
400 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
401 }
402
403 // GRG, Jun/2000: Changed this after a lot of discussion in
404 // the lists. wxWindows 2.2 will have a set of flags to
405 // customize this behaviour.
406 SetInsertionPoint(0);
407
408 m_modified = FALSE;
409 }
410
411 void wxTextCtrl::WriteText( const wxString &text )
412 {
413 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
414
415 if ( text.empty() )
416 return;
417
418 #if wxUSE_UNICODE
419 wxWX2MBbuf buf = text.mbc_str();
420 const char *txt = buf;
421 size_t txtlen = strlen(buf);
422 #else
423 const char *txt = text;
424 size_t txtlen = text.length();
425 #endif
426
427 if ( m_windowStyle & wxTE_MULTILINE )
428 {
429 /* this moves the cursor pos to behind the inserted text */
430 gint len = GTK_EDITABLE(m_text)->current_pos;
431
432 // if we have any special style, use it
433 if ( !m_defaultStyle.IsDefault() )
434 {
435 GdkFont *font = m_defaultStyle.HasFont()
436 ? m_defaultStyle.GetFont().GetInternalFont()
437 : NULL;
438
439 GdkColor *colFg = m_defaultStyle.HasTextColour()
440 ? m_defaultStyle.GetTextColour().GetColor()
441 : NULL;
442
443 GdkColor *colBg = m_defaultStyle.HasBackgroundColour()
444 ? m_defaultStyle.GetBackgroundColour().GetColor()
445 : NULL;
446
447 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
448 }
449 else // no style
450 {
451 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
452 }
453
454 /* bring editable's cursor uptodate. bug in GTK. */
455 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
456 }
457 else // single line
458 {
459 /* this moves the cursor pos to behind the inserted text */
460 gint len = GTK_EDITABLE(m_text)->current_pos;
461 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
462
463 /* bring editable's cursor uptodate. bug in GTK. */
464 GTK_EDITABLE(m_text)->current_pos += text.Len();
465
466 /* bring entry's cursor uptodate. bug in GTK. */
467 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
468 }
469 }
470
471 void wxTextCtrl::AppendText( const wxString &text )
472 {
473 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
474
475 if ( text.empty() )
476 return;
477
478 #if wxUSE_UNICODE
479 wxWX2MBbuf buf = text.mbc_str();
480 const char *txt = buf;
481 size_t txtlen = strlen(buf);
482 #else
483 const char *txt = text;
484 size_t txtlen = text.length();
485 #endif
486
487 if (m_windowStyle & wxTE_MULTILINE)
488 {
489 if ( !m_defaultStyle.IsDefault() )
490 {
491 wxFont font = m_defaultStyle.HasFont() ? m_defaultStyle.GetFont()
492 : m_font;
493 GdkFont *fnt = font.Ok() ? font.GetInternalFont() : NULL;
494
495 wxColour col = m_defaultStyle.HasTextColour()
496 ? m_defaultStyle.GetTextColour()
497 : m_foregroundColour;
498 GdkColor *colFg = col.Ok() ? col.GetColor() : NULL;
499
500 col = m_defaultStyle.HasBackgroundColour()
501 ? m_defaultStyle.GetBackgroundColour()
502 : m_backgroundColour;
503 GdkColor *colBg = col.Ok() ? col.GetColor() : NULL;
504
505 gtk_text_insert( GTK_TEXT(m_text), fnt, colFg, colBg, txt, txtlen );
506 }
507 else // no style
508 {
509 /* we'll insert at the last position */
510 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
511 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
512 }
513
514 /* bring editable's cursor uptodate. bug in GTK. */
515 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
516 }
517 else // single line
518 {
519 gtk_entry_append_text( GTK_ENTRY(m_text), txt );
520 }
521 }
522
523 wxString wxTextCtrl::GetLineText( long lineNo ) const
524 {
525 if (m_windowStyle & wxTE_MULTILINE)
526 {
527 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
528 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
529
530 if (text)
531 {
532 wxString buf(wxT(""));
533 long i;
534 int currentLine = 0;
535 for (i = 0; currentLine != lineNo && text[i]; i++ )
536 if (text[i] == '\n')
537 currentLine++;
538 // Now get the text
539 int j;
540 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
541 buf += text[i];
542
543 g_free( text );
544 return buf;
545 }
546 else
547 return wxEmptyString;
548 }
549 else
550 {
551 if (lineNo == 0) return GetValue();
552 return wxEmptyString;
553 }
554 }
555
556 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
557 {
558 /* If you implement this, don't forget to update the documentation!
559 * (file docs/latex/wx/text.tex) */
560 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
561 }
562
563 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
564 {
565 if ( m_windowStyle & wxTE_MULTILINE )
566 {
567 wxString text = GetValue();
568
569 // cast to prevent warning. But pos really should've been unsigned.
570 if( (unsigned long)pos > text.Len() )
571 return FALSE;
572
573 *x=0; // First Col
574 *y=0; // First Line
575
576 const wxChar* stop = text.c_str() + pos;
577 for ( const wxChar *p = text.c_str(); p < stop; p++ )
578 {
579 if (*p == wxT('\n'))
580 {
581 (*y)++;
582 *x=0;
583 }
584 else
585 (*x)++;
586 }
587 }
588 else // single line control
589 {
590 if ( pos <= GTK_ENTRY(m_text)->text_length )
591 {
592 *y = 0;
593 *x = pos;
594 }
595 else
596 {
597 // index out of bounds
598 return FALSE;
599 }
600 }
601
602 return TRUE;
603 }
604
605 long wxTextCtrl::XYToPosition(long x, long y ) const
606 {
607 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
608
609 long pos=0;
610 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
611
612 pos += x;
613 return pos;
614 }
615
616 int wxTextCtrl::GetLineLength(long lineNo) const
617 {
618 wxString str = GetLineText (lineNo);
619 return (int) str.Length();
620 }
621
622 int wxTextCtrl::GetNumberOfLines() const
623 {
624 if (m_windowStyle & wxTE_MULTILINE)
625 {
626 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
627 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
628
629 if (text)
630 {
631 int currentLine = 0;
632 for (int i = 0; i < len; i++ )
633 {
634 if (text[i] == '\n')
635 currentLine++;
636 }
637 g_free( text );
638
639 // currentLine is 0 based, add 1 to get number of lines
640 return currentLine + 1;
641 }
642 else
643 {
644 return 0;
645 }
646 }
647 else
648 {
649 return 1;
650 }
651 }
652
653 void wxTextCtrl::SetInsertionPoint( long pos )
654 {
655 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
656
657 if (m_windowStyle & wxTE_MULTILINE)
658 {
659 /* seems to be broken in GTK 1.0.X:
660 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
661
662 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
663 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
664
665 /* we fake a set_point by inserting and deleting. as the user
666 isn't supposed to get to know about thos non-sense, we
667 disconnect so that no events are sent to the user program. */
668
669 gint tmp = (gint)pos;
670 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
671 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
672
673 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
674 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
675
676 /* bring editable's cursor uptodate. another bug in GTK. */
677
678 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
679 }
680 else
681 {
682 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
683
684 /* bring editable's cursor uptodate. bug in GTK. */
685
686 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
687 }
688 }
689
690 void wxTextCtrl::SetInsertionPointEnd()
691 {
692 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
693
694 if (m_windowStyle & wxTE_MULTILINE)
695 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
696 else
697 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
698 }
699
700 void wxTextCtrl::SetEditable( bool editable )
701 {
702 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
703
704 if (m_windowStyle & wxTE_MULTILINE)
705 gtk_text_set_editable( GTK_TEXT(m_text), editable );
706 else
707 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
708 }
709
710 bool wxTextCtrl::Enable( bool enable )
711 {
712 if (!wxWindowBase::Enable(enable))
713 {
714 // nothing to do
715 return FALSE;
716 }
717
718 if (m_windowStyle & wxTE_MULTILINE)
719 {
720 gtk_text_set_editable( GTK_TEXT(m_text), enable );
721 OnParentEnable(enable);
722 }
723 else
724 {
725 gtk_widget_set_sensitive( m_text, enable );
726 }
727
728 return TRUE;
729 }
730
731 // wxGTK-specific: called recursively by Enable,
732 // to give widgets an oppprtunity to correct their colours after they
733 // have been changed by Enable
734 void wxTextCtrl::OnParentEnable( bool enable )
735 {
736 // If we have a custom background colour, we use this colour in both
737 // disabled and enabled mode, or we end up with a different colour under the
738 // text.
739 wxColour oldColour = GetBackgroundColour();
740 if (oldColour.Ok())
741 {
742 // Need to set twice or it'll optimize the useful stuff out
743 if (oldColour == * wxWHITE)
744 SetBackgroundColour(*wxBLACK);
745 else
746 SetBackgroundColour(*wxWHITE);
747 SetBackgroundColour(oldColour);
748 }
749 }
750
751 void wxTextCtrl::DiscardEdits()
752 {
753 m_modified = FALSE;
754 }
755
756 // ----------------------------------------------------------------------------
757 // max text length support
758 // ----------------------------------------------------------------------------
759
760 void wxTextCtrl::IgnoreNextTextUpdate()
761 {
762 m_ignoreNextUpdate = TRUE;
763 }
764
765 bool wxTextCtrl::IgnoreTextUpdate()
766 {
767 if ( m_ignoreNextUpdate )
768 {
769 m_ignoreNextUpdate = FALSE;
770
771 return TRUE;
772 }
773
774 return FALSE;
775 }
776
777 void wxTextCtrl::SetMaxLength(unsigned long len)
778 {
779 if ( !HasFlag(wxTE_MULTILINE) )
780 {
781 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
782
783 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
784 // we had tried to enter more text than allowed by max text length and
785 // the text wasn't really changed
786 //
787 // to detect this and generate TEXT_MAXLEN event instead of
788 // TEXT_CHANGED one in this case we also catch "insert_text" signal
789 //
790 // when max len is set to 0 we disconnect our handler as it means that
791 // we shouldn't check anything any more
792 if ( len )
793 {
794 gtk_signal_connect( GTK_OBJECT(m_text),
795 "insert_text",
796 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
797 (gpointer)this);
798 }
799 else // no checking
800 {
801 gtk_signal_disconnect_by_func
802 (
803 GTK_OBJECT(m_text),
804 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
805 (gpointer)this
806 );
807 }
808 }
809 }
810
811 void wxTextCtrl::SetSelection( long from, long to )
812 {
813 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
814
815 if ( (m_windowStyle & wxTE_MULTILINE) &&
816 !GTK_TEXT(m_text)->line_start_cache )
817 {
818 // tell the programmer that it didn't work
819 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
820 return;
821 }
822
823 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
824 }
825
826 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
827 {
828 // SetInsertionPoint( pos );
829 }
830
831 long wxTextCtrl::GetInsertionPoint() const
832 {
833 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
834
835 return (long) GTK_EDITABLE(m_text)->current_pos;
836 }
837
838 long wxTextCtrl::GetLastPosition() const
839 {
840 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
841
842 int pos = 0;
843 if (m_windowStyle & wxTE_MULTILINE)
844 pos = gtk_text_get_length( GTK_TEXT(m_text) );
845 else
846 pos = GTK_ENTRY(m_text)->text_length;
847
848 return (long)pos;
849 }
850
851 void wxTextCtrl::Remove( long from, long to )
852 {
853 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
854
855 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
856 }
857
858 void wxTextCtrl::Replace( long from, long to, const wxString &value )
859 {
860 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
861
862 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
863
864 if (!value.IsEmpty())
865 {
866 gint pos = (gint)from;
867 #if wxUSE_UNICODE
868 wxWX2MBbuf buf = value.mbc_str();
869 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
870 #else
871 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
872 #endif
873 }
874 }
875
876 void wxTextCtrl::Cut()
877 {
878 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
879
880 #if (GTK_MINOR_VERSION > 0)
881 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
882 #else
883 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
884 #endif
885 }
886
887 void wxTextCtrl::Copy()
888 {
889 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
890
891 #if (GTK_MINOR_VERSION > 0)
892 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
893 #else
894 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
895 #endif
896 }
897
898 void wxTextCtrl::Paste()
899 {
900 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
901
902 #if (GTK_MINOR_VERSION > 0)
903 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
904 #else
905 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
906 #endif
907 }
908
909 // Undo/redo
910 void wxTextCtrl::Undo()
911 {
912 // TODO
913 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
914 }
915
916 void wxTextCtrl::Redo()
917 {
918 // TODO
919 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
920 }
921
922 bool wxTextCtrl::CanUndo() const
923 {
924 // TODO
925 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
926 return FALSE;
927 }
928
929 bool wxTextCtrl::CanRedo() const
930 {
931 // TODO
932 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
933 return FALSE;
934 }
935
936 // If the return values from and to are the same, there is no
937 // selection.
938 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
939 {
940 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
941
942 long from, to;
943 if ( !(GTK_EDITABLE(m_text)->has_selection) )
944 {
945 from =
946 to = GetInsertionPoint();
947 }
948 else // got selection
949 {
950 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
951 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
952
953 if ( from > to )
954 {
955 // exchange them to be compatible with wxMSW
956 long tmp = from;
957 from = to;
958 to = tmp;
959 }
960 }
961
962 if ( fromOut )
963 *fromOut = from;
964 if ( toOut )
965 *toOut = to;
966 }
967
968 bool wxTextCtrl::IsEditable() const
969 {
970 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
971
972 return GTK_EDITABLE(m_text)->editable;
973 }
974
975 bool wxTextCtrl::IsModified() const
976 {
977 return m_modified;
978 }
979
980 void wxTextCtrl::Clear()
981 {
982 SetValue( wxT("") );
983 }
984
985 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
986 {
987 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
988
989 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
990 {
991 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
992 event.SetEventObject(this);
993 event.SetString(GetValue());
994 if (GetEventHandler()->ProcessEvent(event)) return;
995 }
996
997 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
998 {
999 wxWindow *top_frame = m_parent;
1000 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1001 top_frame = top_frame->GetParent();
1002 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1003
1004 if (window->default_widget)
1005 {
1006 gtk_widget_activate (window->default_widget);
1007 return;
1008 }
1009 }
1010
1011 key_event.Skip();
1012 }
1013
1014 GtkWidget* wxTextCtrl::GetConnectWidget()
1015 {
1016 return GTK_WIDGET(m_text);
1017 }
1018
1019 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1020 {
1021 if (m_windowStyle & wxTE_MULTILINE)
1022 return (window == GTK_TEXT(m_text)->text_area);
1023 else
1024 return (window == GTK_ENTRY(m_text)->text_area);
1025 }
1026
1027 // the font will change for subsequent text insertiongs
1028 bool wxTextCtrl::SetFont( const wxFont &font )
1029 {
1030 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1031
1032 if ( !wxWindowBase::SetFont(font) )
1033 {
1034 // font didn't change, nothing to do
1035 return FALSE;
1036 }
1037
1038 if ( m_windowStyle & wxTE_MULTILINE )
1039 {
1040 m_updateFont = TRUE;
1041
1042 m_defaultStyle.SetFont(font);
1043
1044 ChangeFontGlobally();
1045 }
1046
1047 return TRUE;
1048 }
1049
1050 void wxTextCtrl::ChangeFontGlobally()
1051 {
1052 // this method is very inefficient and hence should be called as rarely as
1053 // possible!
1054 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1055 _T("shouldn't be called for single line controls") );
1056
1057 wxString value = GetValue();
1058 if ( !value.IsEmpty() )
1059 {
1060 m_updateFont = FALSE;
1061
1062 Clear();
1063 AppendText(value);
1064 }
1065 }
1066
1067 void wxTextCtrl::UpdateFontIfNeeded()
1068 {
1069 if ( m_updateFont )
1070 ChangeFontGlobally();
1071 }
1072
1073 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1074 {
1075 if ( !wxControl::SetForegroundColour(colour) )
1076 return FALSE;
1077
1078 // update default fg colour too
1079 m_defaultStyle.SetTextColour(colour);
1080
1081 return TRUE;
1082 }
1083
1084 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1085 {
1086 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1087
1088 if ( !wxControl::SetBackgroundColour( colour ) )
1089 return FALSE;
1090
1091 if (!m_widget->window)
1092 return FALSE;
1093
1094 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
1095 if (sysbg.Red() == colour.Red() &&
1096 sysbg.Green() == colour.Green() &&
1097 sysbg.Blue() == colour.Blue())
1098 {
1099 return FALSE; // FIXME or TRUE?
1100 }
1101
1102 if (!m_backgroundColour.Ok())
1103 return FALSE;
1104
1105 if (m_windowStyle & wxTE_MULTILINE)
1106 {
1107 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1108 if (!window)
1109 return FALSE;
1110 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1111 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1112 gdk_window_clear( window );
1113 }
1114
1115 // change active background color too
1116 m_defaultStyle.SetBackgroundColour( colour );
1117
1118 return TRUE;
1119 }
1120
1121 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr &style )
1122 {
1123 /* VERY dirty way to do that - removes the required text and re-adds it
1124 with styling (FIXME) */
1125 if ( m_windowStyle & wxTE_MULTILINE )
1126 {
1127 if ( style.IsDefault() )
1128 {
1129 // nothing to do
1130 return TRUE;
1131 }
1132
1133 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1134
1135 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1136 _T("invalid range in wxTextCtrl::SetStyle") );
1137
1138 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1139 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1140 wxString tmp(text,*wxConvCurrent);
1141 g_free( text );
1142
1143 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1144 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1145
1146 #if wxUSE_UNICODE
1147 wxWX2MBbuf buf = tmp.mbc_str();
1148 const char *txt = buf;
1149 size_t txtlen = strlen(buf);
1150 #else
1151 const char *txt = tmp;
1152 size_t txtlen = tmp.length();
1153 #endif
1154
1155 GdkFont *font = style.HasFont()
1156 ? style.GetFont().GetInternalFont()
1157 : NULL;
1158
1159 GdkColor *colFg = style.HasTextColour()
1160 ? style.GetTextColour().GetColor()
1161 : NULL;
1162
1163 GdkColor *colBg = style.HasBackgroundColour()
1164 ? style.GetBackgroundColour().GetColor()
1165 : NULL;
1166
1167 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
1168
1169 /* does not seem to help under GTK+ 1.2 !!!
1170 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1171 SetInsertionPoint( old_pos );
1172 return TRUE;
1173 }
1174 else // singe line
1175 {
1176 // cannot do this for GTK+'s Entry widget
1177 return FALSE;
1178 }
1179 }
1180
1181 void wxTextCtrl::ApplyWidgetStyle()
1182 {
1183 if (m_windowStyle & wxTE_MULTILINE)
1184 {
1185 // how ?
1186 }
1187 else
1188 {
1189 SetWidgetStyle();
1190 gtk_widget_set_style( m_text, m_widgetStyle );
1191 }
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 = (GdkWindow*) 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 UpdateWindowUI();
1269 }
1270
1271 wxSize wxTextCtrl::DoGetBestSize() const
1272 {
1273 // FIXME should be different for multi-line controls...
1274 wxSize ret( wxControl::DoGetBestSize() );
1275 return wxSize(80, ret.y);
1276 }
1277
1278 // ----------------------------------------------------------------------------
1279 // freeze/thaw
1280 // ----------------------------------------------------------------------------
1281
1282 void wxTextCtrl::Freeze()
1283 {
1284 if ( HasFlag(wxTE_MULTILINE) )
1285 {
1286 gtk_text_freeze(GTK_TEXT(m_text));
1287 }
1288 }
1289
1290 void wxTextCtrl::Thaw()
1291 {
1292 if ( HasFlag(wxTE_MULTILINE) )
1293 {
1294 GTK_TEXT(m_text)->vadj->value = 0.0;
1295
1296 gtk_text_thaw(GTK_TEXT(m_text));
1297 }
1298 }
1299
1300 // ----------------------------------------------------------------------------
1301 // scrolling
1302 // ----------------------------------------------------------------------------
1303
1304 GtkAdjustment *wxTextCtrl::GetVAdj() const
1305 {
1306 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1307 }
1308
1309 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1310 {
1311 float value = adj->value + diff;
1312
1313 if ( value < 0 )
1314 value = 0;
1315
1316 float upper = adj->upper - adj->page_size;
1317 if ( value > upper )
1318 value = upper;
1319
1320 // did we noticeably change the scroll position?
1321 if ( fabs(adj->value - value) < 0.2 )
1322 {
1323 // well, this is what Robert does in wxScrollBar, so it must be good...
1324 return FALSE;
1325 }
1326
1327 adj->value = value;
1328
1329 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1330
1331 return TRUE;
1332 }
1333
1334 bool wxTextCtrl::ScrollLines(int lines)
1335 {
1336 GtkAdjustment *adj = GetVAdj();
1337 if ( !adj )
1338 return FALSE;
1339
1340 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1341 static const int KEY_SCROLL_PIXELS = 10;
1342
1343 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1344 }
1345
1346 bool wxTextCtrl::ScrollPages(int pages)
1347 {
1348 GtkAdjustment *adj = GetVAdj();
1349 if ( !adj )
1350 return FALSE;
1351
1352 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1353 }
1354