]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/textctrl.cpp
More for wxPython 2.0b9 (hopefully the last...)
[wxWidgets.git] / src / gtk / textctrl.cpp
... / ...
CommitLineData
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/settings.h"
18
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <ctype.h>
22
23#include "gdk/gdk.h"
24#include "gtk/gtk.h"
25#include "gdk/gdkkeysyms.h"
26
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
38extern bool g_blockEventsOnDrag;
39
40//-----------------------------------------------------------------------------
41// "changed"
42//-----------------------------------------------------------------------------
43
44static void
45gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
46{
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50
51 win->SetModified();
52
53 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
54 event.SetString( win->GetValue() );
55 event.SetEventObject( win );
56 win->GetEventHandler()->ProcessEvent( event );
57}
58
59//-----------------------------------------------------------------------------
60// "changed" from vertical scrollbar
61//-----------------------------------------------------------------------------
62
63static void
64gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
65{
66 if (g_isIdle) wxapp_install_idle_handler();
67
68 win->CalculateScrollbar();
69
70 if (!win->m_hasVMT) return;
71}
72
73//-----------------------------------------------------------------------------
74// wxTextCtrl
75//-----------------------------------------------------------------------------
76
77IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
78
79BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
80 EVT_CHAR(wxTextCtrl::OnChar)
81
82 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
83 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
84 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
85 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
86 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
87
88 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
89 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
90 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
91 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
92 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
93END_EVENT_TABLE()
94
95#ifndef NO_TEXT_WINDOW_STREAM
96wxTextCtrl::wxTextCtrl() : streambuf()
97{
98 if (allocate()) setp(base(),ebuf());
99
100 m_modified = FALSE;
101}
102#else
103wxTextCtrl::wxTextCtrl()
104{
105 m_modified = FALSE;
106}
107#endif
108
109#ifndef NO_TEXT_WINDOW_STREAM
110wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
111 const wxPoint &pos, const wxSize &size,
112 int style, const wxValidator& validator, const wxString &name ) : streambuf()
113{
114 if (allocate()) setp(base(),ebuf());
115
116 m_modified = FALSE;
117 Create( parent, id, value, pos, size, style, validator, name );
118}
119#else
120wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
121 const wxPoint &pos, const wxSize &size,
122 int style, const wxValidator& validator, const wxString &name )
123{
124 m_modified = FALSE;
125 Create( parent, id, value, pos, size, style, validator, name );
126}
127#endif
128
129bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
130 const wxPoint &pos, const wxSize &size,
131 int style, const wxValidator& validator, const wxString &name )
132{
133 m_needParent = TRUE;
134 m_acceptsFocus = TRUE;
135
136 PreCreation( parent, id, pos, size, style, name );
137
138 SetValidator( validator );
139
140 m_vScrollbarVisible = FALSE;
141
142 bool multi_line = (style & wxTE_MULTILINE) != 0;
143 if (multi_line)
144 {
145 /* a multi-line edit control: create a vertical scrollbar by default and
146 horizontal if requested */
147 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
148
149 /* create our control ... */
150 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
151
152 /* ... and put into the upper left hand corner of the table */
153 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
154 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
155
156 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
157 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
158 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
159 0, 0);
160
161 /* put the horizontal scrollbar in the lower left hand corner */
162 if (bHasHScrollbar)
163 {
164 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
165 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
166
167 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
168 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
169 GTK_FILL,
170 0, 0);
171 gtk_widget_show(hscrollbar);
172 }
173
174 /* finally, put the vertical scrollbar in the upper right corner */
175 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
176 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
177
178 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
179 GTK_FILL,
180 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
181 0, 0);
182 }
183 else
184 {
185 /* a single-line text control: no need for scrollbars */
186 m_widget =
187 m_text = gtk_entry_new();
188 }
189
190 wxSize newSize = size;
191 if (newSize.x == -1) newSize.x = 80;
192 if (newSize.y == -1) newSize.y = 26;
193 SetSize( newSize.x, newSize.y );
194
195 m_parent->AddChild( this );
196
197 (m_parent->m_insertCallback)( m_parent, this );
198
199 PostCreation();
200
201 if (multi_line)
202 gtk_widget_show(m_text);
203
204 /* we want to be notified about text changes */
205 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
206 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
207
208 if (multi_line)
209 {
210 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
211 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
212 }
213
214 if (!value.IsEmpty())
215 {
216 gint tmp = 0;
217#if wxUSE_UNICODE
218 wxWX2MBbuf val = value.mbc_str();
219 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
220#else
221 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
222#endif
223
224 if (multi_line)
225 {
226 /* bring editable's cursor uptodate. bug in GTK. */
227
228 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
229 }
230 }
231
232 if (style & wxTE_PASSWORD)
233 {
234 if (!multi_line)
235 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
236 }
237
238 if (style & wxTE_READONLY)
239 {
240 if (!multi_line)
241 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
242 }
243 else
244 {
245 if (multi_line)
246 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
247 }
248
249 SetBackgroundColour( parent->GetBackgroundColour() );
250 SetForegroundColour( parent->GetForegroundColour() );
251
252 Show( TRUE );
253
254 return TRUE;
255}
256
257void wxTextCtrl::CalculateScrollbar()
258{
259 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
260
261 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
262
263 if (adj->upper - adj->page_size < 0.8)
264 {
265 if (m_vScrollbarVisible)
266 {
267 gtk_widget_hide( m_vScrollbar );
268
269 m_vScrollbarVisible = FALSE;
270 }
271 }
272 else
273 {
274 if (!m_vScrollbarVisible)
275 {
276 gtk_widget_show( m_vScrollbar );
277
278 m_vScrollbarVisible = TRUE;
279 }
280 }
281}
282
283wxString wxTextCtrl::GetValue() const
284{
285 wxCHECK_MSG( m_text != NULL, _T(""), _T("invalid text ctrl") );
286
287 wxString tmp;
288 if (m_windowStyle & wxTE_MULTILINE)
289 {
290 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
291 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
292 tmp = wxString(text,*wxConv_current);
293 g_free( text );
294 }
295 else
296 {
297 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConv_current);
298 }
299 return tmp;
300}
301
302void wxTextCtrl::SetValue( const wxString &value )
303{
304 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
305
306 wxString tmp = _T("");
307 if (!value.IsNull()) tmp = value;
308 if (m_windowStyle & wxTE_MULTILINE)
309 {
310 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
311 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
312 len = 0;
313#if wxUSE_UNICODE
314 wxWX2MBbuf tmpbuf = tmp.mbc_str();
315 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
316#else
317 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp.mbc_str(), tmp.Length(), &len );
318#endif
319 }
320 else
321 {
322 gtk_entry_set_text( GTK_ENTRY(m_text), tmp.mbc_str() );
323 }
324}
325
326void wxTextCtrl::WriteText( const wxString &text )
327{
328 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
329
330 if (text.IsNull()) return;
331
332 if (m_windowStyle & wxTE_MULTILINE)
333 {
334 /* this moves the cursor pos to behind the inserted text */
335 gint len = GTK_EDITABLE(m_text)->current_pos;
336
337#if wxUSE_UNICODE
338 wxWX2MBbuf buf = text.mbc_str();
339 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
340#else
341 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
342#endif
343
344 /* bring editable's cursor uptodate. bug in GTK. */
345 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
346 }
347 else
348 {
349 /* this moves the cursor pos to behind the inserted text */
350 gint len = GTK_EDITABLE(m_text)->current_pos;
351#if wxUSE_UNICODE
352 wxWX2MBbuf buf = text.mbc_str();
353 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
354#else
355 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
356#endif
357
358 /* bring editable's cursor uptodate. bug in GTK. */
359 GTK_EDITABLE(m_text)->current_pos += text.Len();
360
361 /* bring entry's cursor uptodate. bug in GTK. */
362 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
363 }
364}
365
366void wxTextCtrl::AppendText( const wxString &text )
367{
368 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
369
370 if (m_windowStyle & wxTE_MULTILINE)
371 {
372 /* we'll insert at the last position */
373 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
374#if wxUSE_UNICODE
375 wxWX2MBbuf buf = text.mbc_str();
376 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
377#else
378 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
379#endif
380
381 /* bring editable's cursor uptodate. bug in GTK. */
382 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
383 }
384 else
385 {
386 gtk_entry_append_text( GTK_ENTRY(m_text), text.mbc_str() );
387 }
388}
389
390bool wxTextCtrl::LoadFile( const wxString &file )
391{
392 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
393
394 if (!wxFileExists(file)) return FALSE;
395
396 Clear();
397
398 FILE *fp = (FILE*) NULL;
399 struct stat statb;
400
401 if ((stat (FNSTRINGCAST file.fn_str(), &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
402 !(fp = fopen (FNSTRINGCAST file.fn_str(), "r")))
403 {
404 return FALSE;
405 }
406 else
407 {
408 gint len = statb.st_size;
409 char *text;
410 if (!(text = (char*)malloc ((unsigned) (len + 1))))
411 {
412 fclose (fp);
413 return FALSE;
414 }
415 if (fread (text, sizeof (char), len, fp) != (size_t) len)
416 {
417 }
418 fclose (fp);
419
420 text[len] = 0;
421
422 if (m_windowStyle & wxTE_MULTILINE)
423 {
424 gint pos = 0;
425 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos );
426 }
427 else
428 {
429 gtk_entry_set_text( GTK_ENTRY(m_text), text );
430 }
431
432 free (text);
433 m_modified = FALSE;
434 return TRUE;
435 }
436 return FALSE;
437}
438
439bool wxTextCtrl::SaveFile( const wxString &file )
440{
441 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
442
443 if (file == _T("")) return FALSE;
444
445 FILE *fp;
446
447 if (!(fp = fopen (FNSTRINGCAST file.fn_str(), "w")))
448 {
449 return FALSE;
450 }
451 else
452 {
453 char *text = (char*) NULL;
454 gint len = 0;
455
456 if (m_windowStyle & wxTE_MULTILINE)
457 {
458 len = gtk_text_get_length( GTK_TEXT(m_text) );
459 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
460 }
461 else
462 {
463 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
464 }
465
466 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
467 {
468 // Did not write whole file
469 }
470
471 // Make sure newline terminates the file
472 if (text[len - 1] != '\n')
473 fputc ('\n', fp);
474
475 fclose (fp);
476
477 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
478
479 m_modified = FALSE;
480 return TRUE;
481 }
482
483 return TRUE;
484}
485
486wxString wxTextCtrl::GetLineText( long lineNo ) const
487{
488 if (m_windowStyle & wxTE_MULTILINE)
489 {
490 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
491 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
492
493 if (text)
494 {
495 wxString buf(_T(""));
496 long i;
497 int currentLine = 0;
498 for (i = 0; currentLine != lineNo && text[i]; i++ )
499 if (text[i] == '\n')
500 currentLine++;
501 // Now get the text
502 int j;
503 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
504 buf += text[i];
505
506 g_free( text );
507 return buf;
508 }
509 else
510 return wxEmptyString;
511 }
512 else
513 {
514 if (lineNo == 0) return GetValue();
515 return wxEmptyString;
516 }
517}
518
519void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
520{
521 /* If you implement this, don't forget to update the documentation!
522 * (file docs/latex/wx/text.tex) */
523 wxFAIL_MSG( _T("wxTextCtrl::OnDropFiles not implemented") );
524}
525
526long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
527{
528 if ( m_windowStyle & wxTE_MULTILINE )
529 {
530 wxString text = GetValue();
531
532 // cast to prevent warning. But pos really should've been unsigned.
533 if( (unsigned long)pos > text.Len() )
534 return FALSE;
535
536 *x=0; // First Col
537 *y=0; // First Line
538
539 const wxChar* stop = text.c_str() + pos;
540 for ( const wxChar *p = text.c_str(); p < stop; p++ )
541 {
542 if (*p == _T('\n'))
543 {
544 (*y)++;
545 *x=0;
546 }
547 else
548 (*x)++;
549 }
550 }
551 else // single line control
552 {
553 if ( pos <= GTK_ENTRY(m_text)->text_length )
554 {
555 *y = 0;
556 *x = pos;
557 }
558 else
559 {
560 // index out of bounds
561 return FALSE;
562 }
563 }
564
565 return TRUE;
566}
567
568long wxTextCtrl::XYToPosition(long x, long y ) const
569{
570 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
571
572 long pos=0;
573 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
574
575 pos += x;
576 return pos;
577}
578
579int wxTextCtrl::GetLineLength(long lineNo) const
580{
581 wxString str = GetLineText (lineNo);
582 return (int) str.Length();
583}
584
585int wxTextCtrl::GetNumberOfLines() const
586{
587 if (m_windowStyle & wxTE_MULTILINE)
588 {
589 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
590 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
591
592 if (text)
593 {
594 int currentLine = 0;
595 for (int i = 0; i < len; i++ )
596 {
597 if (text[i] == '\n')
598 currentLine++;
599 }
600 g_free( text );
601
602 // currentLine is 0 based, add 1 to get number of lines
603 return currentLine + 1;
604 }
605 else
606 {
607 return 0;
608 }
609 }
610 else
611 {
612 return 1;
613 }
614}
615
616void wxTextCtrl::SetInsertionPoint( long pos )
617{
618 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
619
620 if (m_windowStyle & wxTE_MULTILINE)
621 {
622 /* seems to be broken in GTK 1.0.X:
623 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
624
625 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
626 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
627
628 /* we fake a set_point by inserting and deleting. as the user
629 isn't supposed to get to know about thos non-sense, we
630 disconnect so that no events are sent to the user program. */
631
632 gint tmp = (gint)pos;
633 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
634 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
635
636 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
637 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
638
639 /* bring editable's cursor uptodate. another bug in GTK. */
640
641 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
642 }
643 else
644 {
645 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
646
647 /* bring editable's cursor uptodate. bug in GTK. */
648
649 GTK_EDITABLE(m_text)->current_pos = pos;
650 }
651}
652
653void wxTextCtrl::SetInsertionPointEnd()
654{
655 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
656
657 if (m_windowStyle & wxTE_MULTILINE)
658 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
659 else
660 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
661}
662
663void wxTextCtrl::SetEditable( bool editable )
664{
665 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
666
667 if (m_windowStyle & wxTE_MULTILINE)
668 gtk_text_set_editable( GTK_TEXT(m_text), editable );
669 else
670 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
671}
672
673void wxTextCtrl::SetSelection( long from, long to )
674{
675 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
676
677 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
678}
679
680void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
681{
682 wxFAIL_MSG( _T("wxTextCtrl::ShowPosition not implemented") );
683}
684
685long wxTextCtrl::GetInsertionPoint() const
686{
687 wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") );
688
689 return (long) GTK_EDITABLE(m_text)->current_pos;
690}
691
692long wxTextCtrl::GetLastPosition() const
693{
694 wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") );
695
696 int pos = 0;
697 if (m_windowStyle & wxTE_MULTILINE)
698 pos = gtk_text_get_length( GTK_TEXT(m_text) );
699 else
700 pos = GTK_ENTRY(m_text)->text_length;
701
702 return (long)pos;
703}
704
705void wxTextCtrl::Remove( long from, long to )
706{
707 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
708
709 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
710}
711
712void wxTextCtrl::Replace( long from, long to, const wxString &value )
713{
714 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
715
716 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
717 if (value.IsNull()) return;
718 gint pos = (gint)from;
719#if wxUSE_UNICODE
720 wxWX2MBbuf buf = value.mbc_str();
721 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
722#else
723 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
724#endif
725}
726
727void wxTextCtrl::Cut()
728{
729 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
730
731#if (GTK_MINOR_VERSION > 0)
732 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
733#else
734 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
735#endif
736}
737
738void wxTextCtrl::Copy()
739{
740 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
741
742#if (GTK_MINOR_VERSION > 0)
743 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
744#else
745 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
746#endif
747}
748
749void wxTextCtrl::Paste()
750{
751 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
752
753#if (GTK_MINOR_VERSION > 0)
754 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
755#else
756 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
757#endif
758}
759
760bool wxTextCtrl::CanCopy() const
761{
762 // Can copy if there's a selection
763 long from, to;
764 GetSelection(& from, & to);
765 return (from != to) ;
766}
767
768bool wxTextCtrl::CanCut() const
769{
770 // Can cut if there's a selection
771 long from, to;
772 GetSelection(& from, & to);
773 return (from != to) ;
774}
775
776bool wxTextCtrl::CanPaste() const
777{
778 return IsEditable() ;
779}
780
781// Undo/redo
782void wxTextCtrl::Undo()
783{
784 // TODO
785 wxFAIL_MSG( _T("wxTextCtrl::Undo not implemented") );
786}
787
788void wxTextCtrl::Redo()
789{
790 // TODO
791 wxFAIL_MSG( _T("wxTextCtrl::Redo not implemented") );
792}
793
794bool wxTextCtrl::CanUndo() const
795{
796 // TODO
797 wxFAIL_MSG( _T("wxTextCtrl::CanUndo not implemented") );
798 return FALSE;
799}
800
801bool wxTextCtrl::CanRedo() const
802{
803 // TODO
804 wxFAIL_MSG( _T("wxTextCtrl::CanRedo not implemented") );
805 return FALSE;
806}
807
808// If the return values from and to are the same, there is no
809// selection.
810void wxTextCtrl::GetSelection(long* from, long* to) const
811{
812 // TODO
813 *from = 0;
814 *to = 0;
815 wxFAIL_MSG( _T("wxTextCtrl::GetSelection not implemented") );
816}
817
818bool wxTextCtrl::IsEditable() const
819{
820 // TODO
821 wxFAIL_MSG( _T("wxTextCtrl::IsEditable not implemented") );
822 return FALSE;
823}
824
825void wxTextCtrl::Clear()
826{
827 SetValue( _T("") );
828}
829
830void wxTextCtrl::OnChar( wxKeyEvent &key_event )
831{
832 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
833
834 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
835 {
836 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
837 event.SetEventObject(this);
838 if (GetEventHandler()->ProcessEvent(event)) return;
839 }
840
841 key_event.Skip();
842}
843
844#ifndef NO_TEXT_WINDOW_STREAM
845int wxTextCtrl::overflow( int WXUNUSED(c) )
846{
847 int len = pptr() - pbase();
848 char *txt = new char[len+1];
849 strncpy(txt, pbase(), len);
850 txt[len] = '\0';
851 (*this) << txt;
852 setp(pbase(), epptr());
853 delete[] txt;
854 return EOF;
855}
856
857int wxTextCtrl::sync()
858{
859 int len = pptr() - pbase();
860 char *txt = new char[len+1];
861 strncpy(txt, pbase(), len);
862 txt[len] = '\0';
863 (*this) << txt;
864 setp(pbase(), epptr());
865 delete[] txt;
866 return 0;
867}
868
869int wxTextCtrl::underflow()
870{
871 return EOF;
872}
873
874wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
875{
876 AppendText(s);
877 return *this;
878}
879
880wxTextCtrl& wxTextCtrl::operator<<(float f)
881{
882 static char buf[100];
883 sprintf(buf, "%.2f", f);
884 AppendText(buf);
885 return *this;
886}
887
888wxTextCtrl& wxTextCtrl::operator<<(double d)
889{
890 static char buf[100];
891 sprintf(buf, "%.2f", d);
892 AppendText(buf);
893 return *this;
894}
895
896wxTextCtrl& wxTextCtrl::operator<<(int i)
897{
898 static char buf[100];
899 sprintf(buf, "%i", i);
900 AppendText(buf);
901 return *this;
902}
903
904wxTextCtrl& wxTextCtrl::operator<<(long i)
905{
906 static char buf[100];
907 sprintf(buf, "%ld", i);
908 AppendText(buf);
909 return *this;
910}
911
912wxTextCtrl& wxTextCtrl::operator<<(const char c)
913{
914 char buf[2];
915
916 buf[0] = c;
917 buf[1] = 0;
918 AppendText(buf);
919 return *this;
920}
921#endif
922
923GtkWidget* wxTextCtrl::GetConnectWidget()
924{
925 return GTK_WIDGET(m_text);
926}
927
928bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
929{
930 if (m_windowStyle & wxTE_MULTILINE)
931 return (window == GTK_TEXT(m_text)->text_area);
932 else
933 return (window == GTK_ENTRY(m_text)->text_area);
934}
935
936void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
937{
938 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
939
940 // doesn't work
941}
942
943void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
944{
945 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
946
947 // doesn't work
948}
949
950void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
951{
952 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
953
954 wxControl::SetBackgroundColour( colour );
955
956 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
957 if (sysbg.Red() == colour.Red() &&
958 sysbg.Green() == colour.Green() &&
959 sysbg.Blue() == colour.Blue())
960 {
961 return;
962 }
963
964 if (!m_backgroundColour.Ok()) return;
965
966 if (m_windowStyle & wxTE_MULTILINE)
967 {
968 GdkWindow *window = GTK_TEXT(m_text)->text_area;
969 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
970 gdk_window_set_background( window, m_backgroundColour.GetColor() );
971 gdk_window_clear( window );
972 }
973}
974
975void wxTextCtrl::ApplyWidgetStyle()
976{
977 if (m_windowStyle & wxTE_MULTILINE)
978 {
979 // how ?
980 }
981 else
982 {
983 SetWidgetStyle();
984 gtk_widget_set_style( m_text, m_widgetStyle );
985 }
986}
987
988void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
989{
990 Cut();
991}
992
993void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
994{
995 Copy();
996}
997
998void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
999{
1000 Paste();
1001}
1002
1003void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1004{
1005 Undo();
1006}
1007
1008void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1009{
1010 Redo();
1011}
1012
1013void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1014{
1015 event.Enable( CanCut() );
1016}
1017
1018void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1019{
1020 event.Enable( CanCopy() );
1021}
1022
1023void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1024{
1025 event.Enable( CanPaste() );
1026}
1027
1028void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1029{
1030 event.Enable( CanUndo() );
1031}
1032
1033void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1034{
1035 event.Enable( CanRedo() );
1036}