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