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