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