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