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