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