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