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