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