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