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