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