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