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