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