]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textctrl.cpp
8e07499441ba0c6ab7e4188e237f634b58350308
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "textctrl.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/textctrl.h"
18 #include "wx/utils.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #include "wx/settings.h"
22 #include "wx/panel.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
29 #include <math.h> // for fabs
30
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
33
34 //-----------------------------------------------------------------------------
35 // idle system
36 //-----------------------------------------------------------------------------
37
38 extern void wxapp_install_idle_handler();
39 extern bool g_isIdle;
40
41 //-----------------------------------------------------------------------------
42 // data
43 //-----------------------------------------------------------------------------
44
45 extern bool g_blockEventsOnDrag;
46 extern wxCursor g_globalCursor;
47 extern wxWindowGTK *g_delayedFocus;
48
49 // ----------------------------------------------------------------------------
50 // helpers
51 // ----------------------------------------------------------------------------
52
53 #ifdef __WXGTK20__
54 static void wxGtkTextInsert(GtkWidget *text,
55 GtkTextBuffer *text_buffer,
56 const wxTextAttr& attr,
57 wxCharBuffer buffer)
58
59 {
60 PangoFontDescription *font_description = attr.HasFont()
61 ? attr.GetFont().GetNativeFontInfo()->description
62 : NULL;
63
64 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
65 : NULL;
66
67 GdkColor *colBg = attr.HasBackgroundColour()
68 ? attr.GetBackgroundColour().GetColor()
69 : NULL;
70
71 GtkTextIter start, end;
72 GtkTextMark *mark;
73 // iterators are invalidated by any mutation that affects 'indexable' buffer contents,
74 // so we save current position in a mark
75 // we need a mark of left gravity, so we cannot use
76 // mark = gtk_text_buffer_get_insert (text_buffer)
77
78 gtk_text_buffer_get_iter_at_mark( text_buffer, &start,
79 gtk_text_buffer_get_insert (text_buffer) );
80 mark = gtk_text_buffer_create_mark( text_buffer, NULL, &start, TRUE/*left gravity*/ );
81
82 gtk_text_buffer_insert_at_cursor( text_buffer, buffer, strlen(buffer) );
83
84 gtk_text_buffer_get_iter_at_mark( text_buffer, &end,
85 gtk_text_buffer_get_insert (text_buffer) );
86 gtk_text_buffer_get_iter_at_mark( text_buffer, &start, mark );
87
88 GtkTextTag *tag;
89 tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description,
90 "foreground-gdk", colFg,
91 "background-gdk", colBg, NULL );
92 gtk_text_buffer_apply_tag( text_buffer, tag, &start, &end );
93 }
94 #else
95 static void wxGtkTextInsert(GtkWidget *text,
96 const wxTextAttr& attr,
97 const char *txt,
98 size_t len)
99 {
100 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
101 : NULL;
102
103 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
104 : NULL;
105
106 GdkColor *colBg = attr.HasBackgroundColour()
107 ? attr.GetBackgroundColour().GetColor()
108 : NULL;
109
110 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
111 }
112 #endif // GTK 1.x
113
114 // ----------------------------------------------------------------------------
115 // "insert_text" for GtkEntry
116 // ----------------------------------------------------------------------------
117
118 static void
119 gtk_insert_text_callback(GtkEditable *editable,
120 const gchar *new_text,
121 gint new_text_length,
122 gint *position,
123 wxTextCtrl *win)
124 {
125 if (g_isIdle)
126 wxapp_install_idle_handler();
127
128 // we should only be called if we have a max len limit at all
129 GtkEntry *entry = GTK_ENTRY (editable);
130
131 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
132
133 // check that we don't overflow the max length limit
134 //
135 // FIXME: this doesn't work when we paste a string which is going to be
136 // truncated
137 if ( entry->text_length == entry->text_max_length )
138 {
139 // we don't need to run the base class version at all
140 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
141
142 // remember that the next changed signal is to be ignored to avoid
143 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
144 win->IgnoreNextTextUpdate();
145
146 // and generate the correct one ourselves
147 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
148 event.SetEventObject(win);
149 event.SetString(win->GetValue());
150 win->GetEventHandler()->ProcessEvent( event );
151 }
152 }
153
154 //-----------------------------------------------------------------------------
155 // "changed"
156 //-----------------------------------------------------------------------------
157
158 static void
159 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
160 {
161 if ( win->IgnoreTextUpdate() )
162 return;
163
164 if (!win->m_hasVMT) return;
165
166 if (g_isIdle)
167 wxapp_install_idle_handler();
168
169 win->SetModified();
170 win->UpdateFontIfNeeded();
171
172 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
173 event.SetEventObject( win );
174 event.SetString( win->GetValue() );
175 win->GetEventHandler()->ProcessEvent( event );
176 }
177
178 //-----------------------------------------------------------------------------
179 // "changed" from vertical scrollbar
180 //-----------------------------------------------------------------------------
181
182 #ifndef __WXGTK20__
183 static void
184 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
185 {
186 if (!win->m_hasVMT) return;
187
188 if (g_isIdle)
189 wxapp_install_idle_handler();
190
191 win->CalculateScrollbar();
192 }
193 #endif
194
195 // ----------------------------------------------------------------------------
196 // redraw callback for multiline text
197 // ----------------------------------------------------------------------------
198
199 #ifndef __WXGTK20__
200
201 // redrawing a GtkText from inside a wxYield() call results in crashes (the
202 // text sample shows it in its "Add lines" command which shows wxProgressDialog
203 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
204 // don't do anything if we're inside wxYield()
205
206 extern bool wxIsInsideYield;
207
208 extern "C" {
209 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
210 }
211
212 static GtkDrawCallback gs_gtk_text_draw = NULL;
213
214 extern "C"
215 void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
216 {
217 if ( !wxIsInsideYield )
218 {
219 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
220 _T("infinite recursion in wxgtk_text_draw aborted") );
221
222 gs_gtk_text_draw(widget, rect);
223 }
224 }
225
226 #endif // __WXGTK20__
227
228 //-----------------------------------------------------------------------------
229 // wxTextCtrl
230 //-----------------------------------------------------------------------------
231
232 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
233
234 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
235 EVT_CHAR(wxTextCtrl::OnChar)
236
237 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
238 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
239 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
240 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
241 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
242
243 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
244 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
245 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
246 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
247 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
248 END_EVENT_TABLE()
249
250 void wxTextCtrl::Init()
251 {
252 m_ignoreNextUpdate =
253 m_modified = FALSE;
254 m_updateFont = FALSE;
255 m_text =
256 m_vScrollbar = (GtkWidget *)NULL;
257 }
258
259 wxTextCtrl::wxTextCtrl( wxWindow *parent,
260 wxWindowID id,
261 const wxString &value,
262 const wxPoint &pos,
263 const wxSize &size,
264 long style,
265 const wxValidator& validator,
266 const wxString &name )
267 {
268 Init();
269
270 Create( parent, id, value, pos, size, style, validator, name );
271 }
272
273 bool wxTextCtrl::Create( wxWindow *parent,
274 wxWindowID id,
275 const wxString &value,
276 const wxPoint &pos,
277 const wxSize &size,
278 long style,
279 const wxValidator& validator,
280 const wxString &name )
281 {
282 m_needParent = TRUE;
283 m_acceptsFocus = TRUE;
284
285 if (!PreCreation( parent, pos, size ) ||
286 !CreateBase( parent, id, pos, size, style, validator, name ))
287 {
288 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
289 return FALSE;
290 }
291
292
293 m_vScrollbarVisible = FALSE;
294
295 bool multi_line = (style & wxTE_MULTILINE) != 0;
296
297 #ifdef __WXGTK20__
298 GtkTextBuffer *buffer = NULL;
299 #endif
300
301 if (multi_line)
302 {
303 #ifdef __WXGTK20__
304 // Create view
305 m_text = gtk_text_view_new();
306
307 buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
308
309 // create scrolled window
310 m_widget = gtk_scrolled_window_new( NULL, NULL );
311 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
312 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
313
314 // Insert view into scrolled window
315 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
316
317 // Global settings which can be overridden by tags, I guess.
318 if (HasFlag( wxHSCROLL ))
319 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE );
320 else
321 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD );
322
323 if (!HasFlag(wxNO_BORDER))
324 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
325 #else
326 // create our control ...
327 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
328
329 // ... and put into the upper left hand corner of the table
330 bool bHasHScrollbar = FALSE;
331 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
332 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
333 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
334 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
335 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
336 0, 0);
337
338 // always wrap words
339 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
340
341 // finally, put the vertical scrollbar in the upper right corner
342 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
343 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
344 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
345 GTK_FILL,
346 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
347 0, 0);
348 #endif
349 }
350 else
351 {
352 // a single-line text control: no need for scrollbars
353 m_widget =
354 m_text = gtk_entry_new();
355 }
356
357 m_parent->DoAddChild( this );
358
359 m_focusWidget = m_text;
360
361 PostCreation();
362
363 SetFont( parent->GetFont() );
364
365 wxSize size_best( DoGetBestSize() );
366 wxSize new_size( size );
367 if (new_size.x == -1)
368 new_size.x = size_best.x;
369 if (new_size.y == -1)
370 new_size.y = size_best.y;
371 if ((new_size.x != size.x) || (new_size.y != size.y))
372 SetSize( new_size.x, new_size.y );
373
374 if (multi_line)
375 gtk_widget_show(m_text);
376
377 #ifndef __WXGTK20__
378 if (multi_line)
379 {
380 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
381 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
382
383 // only initialize gs_gtk_text_draw once, starting from the next the
384 // klass::draw will already be wxgtk_text_draw
385 if ( !gs_gtk_text_draw )
386 {
387 GtkDrawCallback&
388 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
389
390 gs_gtk_text_draw = draw;
391
392 draw = wxgtk_text_draw;
393 }
394 }
395 #endif // GTK+ 1.x
396
397 if (!value.IsEmpty())
398 {
399 #ifdef __WXGTK20__
400 SetValue( value );
401 #else
402
403 #if !GTK_CHECK_VERSION(1, 2, 0)
404 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
405 // gtk_editable_insert_text()
406 gtk_widget_realize(m_text);
407 #endif // GTK 1.0
408
409 gint tmp = 0;
410 #if wxUSE_UNICODE
411 wxWX2MBbuf val = value.mbc_str();
412 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
413 #else
414 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
415 #endif
416
417 if (multi_line)
418 {
419 // Bring editable's cursor uptodate. Bug in GTK.
420 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
421 }
422
423 #endif
424 }
425
426 if (style & wxTE_PASSWORD)
427 {
428 if (!multi_line)
429 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
430 }
431
432 if (style & wxTE_READONLY)
433 {
434 if (!multi_line)
435 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
436 #ifdef __WXGTK20__
437 else
438 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
439 }
440 #else
441 }
442 else
443 {
444 if (multi_line)
445 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
446 }
447 #endif
448
449 // We want to be notified about text changes.
450 #ifdef __WXGTK20__
451 if (multi_line)
452 {
453 g_signal_connect( G_OBJECT(buffer), "changed",
454 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
455 }
456 else
457 #endif
458 {
459 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
460 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
461 }
462
463 // we don't set a valid background colour, because the window
464 // manager should use a default one
465 m_backgroundColour = wxColour();
466
467 wxColour colFg = parent->GetForegroundColour();
468 SetForegroundColour( colFg );
469
470 m_cursor = wxCursor( wxCURSOR_IBEAM );
471
472 wxTextAttr attrDef( colFg, m_backgroundColour, parent->GetFont() );
473 SetDefaultStyle( attrDef );
474
475 Show( TRUE );
476
477 return TRUE;
478 }
479
480 void wxTextCtrl::CalculateScrollbar()
481 {
482 #ifndef __WXGTK20__
483 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
484
485 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
486
487 if (adj->upper - adj->page_size < 0.8)
488 {
489 if (m_vScrollbarVisible)
490 {
491 gtk_widget_hide( m_vScrollbar );
492 m_vScrollbarVisible = FALSE;
493 }
494 }
495 else
496 {
497 if (!m_vScrollbarVisible)
498 {
499 gtk_widget_show( m_vScrollbar );
500 m_vScrollbarVisible = TRUE;
501 }
502 }
503 #endif
504 }
505
506 wxString wxTextCtrl::GetValue() const
507 {
508 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
509
510 wxString tmp;
511 if (m_windowStyle & wxTE_MULTILINE)
512 {
513 #ifdef __WXGTK20__
514 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
515
516 GtkTextIter start;
517 gtk_text_buffer_get_start_iter( text_buffer, &start );
518 GtkTextIter end;
519 gtk_text_buffer_get_end_iter( text_buffer, &end );
520 gchar *text = gtk_text_buffer_get_text( text_buffer, &start, &end, TRUE );
521
522 #if wxUSE_UNICODE
523 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
524 #else
525 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
526 #endif
527 tmp = buffer;
528
529 g_free( text );
530 #else
531 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
532 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
533 tmp = text;
534 g_free( text );
535 #endif
536 }
537 else
538 {
539 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
540 }
541
542 return tmp;
543 }
544
545 void wxTextCtrl::SetValue( const wxString &value )
546 {
547 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
548
549 if (m_windowStyle & wxTE_MULTILINE)
550 {
551 #ifdef __WXGTK20__
552
553 #if wxUSE_UNICODE
554 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
555 #else
556 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
557 #endif
558 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
559 gtk_text_buffer_set_text( text_buffer, buffer, strlen(buffer) );
560
561 #else
562 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
563 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
564 len = 0;
565 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
566 #endif
567 }
568 else
569 {
570 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
571 }
572
573 // GRG, Jun/2000: Changed this after a lot of discussion in
574 // the lists. wxWindows 2.2 will have a set of flags to
575 // customize this behaviour.
576 SetInsertionPoint(0);
577
578 m_modified = FALSE;
579 }
580
581 void wxTextCtrl::WriteText( const wxString &text )
582 {
583 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
584
585 if ( text.empty() )
586 return;
587
588 if ( m_windowStyle & wxTE_MULTILINE )
589 {
590 #ifdef __WXGTK20__
591
592 #if wxUSE_UNICODE
593 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
594 #else
595 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
596 #endif
597 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
598
599 // TODO: Call whatever is needed to delete the selection.
600 wxGtkTextInsert( m_text, text_buffer, m_defaultStyle, buffer );
601
602 // Scroll to cursor.
603 GtkTextIter iter;
604 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, gtk_text_buffer_get_insert( text_buffer ) );
605 gtk_text_view_scroll_to_iter( GTK_TEXT_VIEW(m_text), &iter, 0.0, FALSE, 0.0, 1.0 );
606 #else // GTK 1.x
607 // After cursor movements, gtk_text_get_point() is wrong by one.
608 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
609
610 // always use m_defaultStyle, even if it is empty as otherwise
611 // resetting the style and appending some more text wouldn't work: if
612 // we don't specify the style explicitly, the old style would be used
613 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
614 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
615
616 // Bring editable's cursor back uptodate.
617 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
618 #endif // GTK 1.x/2.0
619 }
620 else // single line
621 {
622 // First remove the selection if there is one
623 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
624
625 // This moves the cursor pos to behind the inserted text.
626 gint len = GET_EDITABLE_POS(m_text);
627
628 #ifdef __WXGTK20__
629
630 #if wxUSE_UNICODE
631 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
632 #else
633 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
634 #endif
635 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
636
637 #else
638 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
639 #endif
640
641 // Bring entry's cursor uptodate.
642 gtk_entry_set_position( GTK_ENTRY(m_text), len );
643 }
644
645 m_modified = TRUE;
646 }
647
648 void wxTextCtrl::AppendText( const wxString &text )
649 {
650 SetInsertionPointEnd();
651 WriteText( text );
652 }
653
654 wxString wxTextCtrl::GetLineText( long lineNo ) const
655 {
656 if (m_windowStyle & wxTE_MULTILINE)
657 {
658 #ifndef __WXGTK20__
659 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
660 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
661
662 if (text)
663 {
664 wxString buf(wxT(""));
665 long i;
666 int currentLine = 0;
667 for (i = 0; currentLine != lineNo && text[i]; i++ )
668 if (text[i] == '\n')
669 currentLine++;
670 // Now get the text
671 int j;
672 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
673 buf += text[i];
674
675 g_free( text );
676 return buf;
677 }
678 else
679 #endif
680 {
681 return wxEmptyString;
682 }
683 }
684 else
685 {
686 if (lineNo == 0) return GetValue();
687 return wxEmptyString;
688 }
689 }
690
691 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
692 {
693 /* If you implement this, don't forget to update the documentation!
694 * (file docs/latex/wx/text.tex) */
695 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
696 }
697
698 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
699 {
700 if ( m_windowStyle & wxTE_MULTILINE )
701 {
702 wxString text = GetValue();
703
704 // cast to prevent warning. But pos really should've been unsigned.
705 if( (unsigned long)pos > text.Len() )
706 return FALSE;
707
708 *x=0; // First Col
709 *y=0; // First Line
710
711 const wxChar* stop = text.c_str() + pos;
712 for ( const wxChar *p = text.c_str(); p < stop; p++ )
713 {
714 if (*p == wxT('\n'))
715 {
716 (*y)++;
717 *x=0;
718 }
719 else
720 (*x)++;
721 }
722 }
723 else // single line control
724 {
725 if ( pos <= GTK_ENTRY(m_text)->text_length )
726 {
727 *y = 0;
728 *x = pos;
729 }
730 else
731 {
732 // index out of bounds
733 return FALSE;
734 }
735 }
736
737 return TRUE;
738 }
739
740 long wxTextCtrl::XYToPosition(long x, long y ) const
741 {
742 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
743
744 long pos=0;
745 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
746
747 pos += x;
748 return pos;
749 }
750
751 int wxTextCtrl::GetLineLength(long lineNo) const
752 {
753 wxString str = GetLineText (lineNo);
754 return (int) str.Length();
755 }
756
757 int wxTextCtrl::GetNumberOfLines() const
758 {
759 if (m_windowStyle & wxTE_MULTILINE)
760 {
761 #ifdef __WXGTK20__
762 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
763
764 return gtk_text_buffer_get_line_count( buffer );
765 #else
766 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
767 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
768
769 if (text)
770 {
771 int currentLine = 0;
772 for (int i = 0; i < len; i++ )
773 {
774 if (text[i] == '\n')
775 currentLine++;
776 }
777 g_free( text );
778
779 // currentLine is 0 based, add 1 to get number of lines
780 return currentLine + 1;
781 }
782 else
783 {
784 return 0;
785 }
786 #endif
787 }
788 else
789 {
790 return 1;
791 }
792 }
793
794 void wxTextCtrl::SetInsertionPoint( long pos )
795 {
796 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
797
798 if (m_windowStyle & wxTE_MULTILINE)
799 {
800 #ifdef __WXGTK20__
801 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
802 GtkTextIter iter;
803 gtk_text_buffer_get_iter_at_offset( text_buffer, &iter, pos );
804 gtk_text_buffer_place_cursor( text_buffer, &iter );
805 #else
806 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
807 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
808
809 /* we fake a set_point by inserting and deleting. as the user
810 isn't supposed to get to know about this non-sense, we
811 disconnect so that no events are sent to the user program. */
812
813 gint tmp = (gint)pos;
814 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
815 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
816
817 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
818 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
819
820 // bring editable's cursor uptodate. Bug in GTK.
821 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
822 #endif
823 }
824 else
825 {
826 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
827
828 // Bring editable's cursor uptodate. Bug in GTK.
829 SET_EDITABLE_POS(m_text, (guint32)pos);
830 }
831 }
832
833 void wxTextCtrl::SetInsertionPointEnd()
834 {
835 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
836
837 if (m_windowStyle & wxTE_MULTILINE)
838 {
839 #ifdef __WXGTK20__
840 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
841 GtkTextIter end;
842 gtk_text_buffer_get_end_iter( text_buffer, &end );
843 gtk_text_buffer_place_cursor( text_buffer, &end );
844 #else
845 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
846 #endif
847 }
848 else
849 {
850 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
851 }
852 }
853
854 void wxTextCtrl::SetEditable( bool editable )
855 {
856 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
857
858 if (m_windowStyle & wxTE_MULTILINE)
859 {
860 #ifdef __WXGTK20__
861 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
862 #else
863 gtk_text_set_editable( GTK_TEXT(m_text), editable );
864 #endif
865 }
866 else
867 {
868 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
869 }
870 }
871
872 bool wxTextCtrl::Enable( bool enable )
873 {
874 if (!wxWindowBase::Enable(enable))
875 {
876 // nothing to do
877 return FALSE;
878 }
879
880 if (m_windowStyle & wxTE_MULTILINE)
881 {
882 #ifdef __WXGTK20__
883 SetEditable( enable );
884 #else
885 gtk_text_set_editable( GTK_TEXT(m_text), enable );
886 OnParentEnable(enable);
887 #endif
888 }
889 else
890 {
891 gtk_widget_set_sensitive( m_text, enable );
892 }
893
894 return TRUE;
895 }
896
897 // wxGTK-specific: called recursively by Enable,
898 // to give widgets an oppprtunity to correct their colours after they
899 // have been changed by Enable
900 void wxTextCtrl::OnParentEnable( bool enable )
901 {
902 // If we have a custom background colour, we use this colour in both
903 // disabled and enabled mode, or we end up with a different colour under the
904 // text.
905 wxColour oldColour = GetBackgroundColour();
906 if (oldColour.Ok())
907 {
908 // Need to set twice or it'll optimize the useful stuff out
909 if (oldColour == * wxWHITE)
910 SetBackgroundColour(*wxBLACK);
911 else
912 SetBackgroundColour(*wxWHITE);
913 SetBackgroundColour(oldColour);
914 }
915 }
916
917 void wxTextCtrl::DiscardEdits()
918 {
919 m_modified = FALSE;
920 }
921
922 // ----------------------------------------------------------------------------
923 // max text length support
924 // ----------------------------------------------------------------------------
925
926 void wxTextCtrl::IgnoreNextTextUpdate()
927 {
928 m_ignoreNextUpdate = TRUE;
929 }
930
931 bool wxTextCtrl::IgnoreTextUpdate()
932 {
933 if ( m_ignoreNextUpdate )
934 {
935 m_ignoreNextUpdate = FALSE;
936
937 return TRUE;
938 }
939
940 return FALSE;
941 }
942
943 void wxTextCtrl::SetMaxLength(unsigned long len)
944 {
945 if ( !HasFlag(wxTE_MULTILINE) )
946 {
947 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
948
949 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
950 // we had tried to enter more text than allowed by max text length and
951 // the text wasn't really changed
952 //
953 // to detect this and generate TEXT_MAXLEN event instead of
954 // TEXT_CHANGED one in this case we also catch "insert_text" signal
955 //
956 // when max len is set to 0 we disconnect our handler as it means that
957 // we shouldn't check anything any more
958 if ( len )
959 {
960 gtk_signal_connect( GTK_OBJECT(m_text),
961 "insert_text",
962 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
963 (gpointer)this);
964 }
965 else // no checking
966 {
967 gtk_signal_disconnect_by_func
968 (
969 GTK_OBJECT(m_text),
970 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
971 (gpointer)this
972 );
973 }
974 }
975 }
976
977 void wxTextCtrl::SetSelection( long from, long to )
978 {
979 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
980
981 if (from == -1 && to == -1)
982 {
983 from = 0;
984 to = GetValue().Length();
985 }
986
987 #ifndef __WXGTK20__
988 if ( (m_windowStyle & wxTE_MULTILINE) &&
989 !GTK_TEXT(m_text)->line_start_cache )
990 {
991 // tell the programmer that it didn't work
992 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
993 return;
994 }
995 #endif
996
997 if (m_windowStyle & wxTE_MULTILINE)
998 {
999 #ifdef __WXGTK20__
1000 GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1001
1002 GtkTextIter fromi, toi;
1003 gtk_text_buffer_get_iter_at_offset( buf, &fromi, from );
1004 gtk_text_buffer_get_iter_at_offset( buf, &toi, to );
1005
1006 gtk_text_buffer_place_cursor( buf, &toi );
1007 gtk_text_buffer_move_mark_by_name( buf, "selection_bound", &fromi );
1008 #else
1009 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1010 #endif
1011 }
1012 else
1013 {
1014 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1015 }
1016 }
1017
1018 void wxTextCtrl::ShowPosition( long pos )
1019 {
1020 #ifndef __WXGTK20__
1021 if (m_windowStyle & wxTE_MULTILINE)
1022 {
1023 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1024 float totalLines = (float) GetNumberOfLines();
1025 long posX;
1026 long posY;
1027 PositionToXY(pos, &posX, &posY);
1028 float posLine = (float) posY;
1029 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1030 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
1031 }
1032 #endif
1033 }
1034
1035 long wxTextCtrl::GetInsertionPoint() const
1036 {
1037 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1038
1039 #ifdef __WXGTK20__
1040 if (m_windowStyle & wxTE_MULTILINE)
1041 {
1042 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1043
1044 // There is no direct accessor for the cursor, but
1045 // internally, the cursor is the "mark" called
1046 // "insert" in the text view's btree structure.
1047
1048 GtkTextMark *mark = gtk_text_buffer_get_insert( text_buffer );
1049 GtkTextIter cursor;
1050 gtk_text_buffer_get_iter_at_mark( text_buffer, &cursor, mark );
1051
1052 return gtk_text_iter_get_offset( &cursor );
1053 }
1054 else
1055 #endif
1056 {
1057 return (long) GET_EDITABLE_POS(m_text);
1058 }
1059 }
1060
1061 long wxTextCtrl::GetLastPosition() const
1062 {
1063 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1064
1065 int pos = 0;
1066
1067 if (m_windowStyle & wxTE_MULTILINE)
1068 {
1069 #ifdef __WXGTK20__
1070 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1071 GtkTextIter end;
1072 gtk_text_buffer_get_end_iter( text_buffer, &end );
1073
1074 pos = gtk_text_iter_get_offset( &end );
1075 #else
1076 pos = gtk_text_get_length( GTK_TEXT(m_text) );
1077 #endif
1078 }
1079 else
1080 {
1081 pos = GTK_ENTRY(m_text)->text_length;
1082 }
1083
1084 return (long)pos;
1085 }
1086
1087 void wxTextCtrl::Remove( long from, long to )
1088 {
1089 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1090
1091 #ifdef __WXGTK20__
1092 if (m_windowStyle & wxTE_MULTILINE)
1093 {
1094 GtkTextBuffer *
1095 text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1096
1097 GtkTextIter fromi, toi;
1098 gtk_text_buffer_get_iter_at_offset( text_buffer, &fromi, from );
1099 gtk_text_buffer_get_iter_at_offset( text_buffer, &toi, to );
1100
1101 gtk_text_buffer_delete( text_buffer, &fromi, &toi );
1102 }
1103 else // single line
1104 #endif
1105 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1106 }
1107
1108 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1109 {
1110 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1111
1112 Remove( from, to );
1113
1114 if (!value.IsEmpty())
1115 {
1116 #ifdef __WXGTK20__
1117 SetInsertionPoint( from );
1118 WriteText( value );
1119 #else // GTK 1.x
1120 gint pos = (gint)from;
1121 #if wxUSE_UNICODE
1122 wxWX2MBbuf buf = value.mbc_str();
1123 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
1124 #else
1125 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
1126 #endif // wxUSE_UNICODE
1127 #endif // GTK 1.x/2.x
1128 }
1129 }
1130
1131 void wxTextCtrl::Cut()
1132 {
1133 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1134
1135 #ifndef __WXGTK20__
1136 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
1137 #endif
1138 }
1139
1140 void wxTextCtrl::Copy()
1141 {
1142 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1143
1144 #ifndef __WXGTK20__
1145 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
1146 #endif
1147 }
1148
1149 void wxTextCtrl::Paste()
1150 {
1151 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1152
1153 #ifndef __WXGTK20__
1154 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG );
1155 #endif
1156 }
1157
1158 // Undo/redo
1159 void wxTextCtrl::Undo()
1160 {
1161 // TODO
1162 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1163 }
1164
1165 void wxTextCtrl::Redo()
1166 {
1167 // TODO
1168 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1169 }
1170
1171 bool wxTextCtrl::CanUndo() const
1172 {
1173 // TODO
1174 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1175 return FALSE;
1176 }
1177
1178 bool wxTextCtrl::CanRedo() const
1179 {
1180 // TODO
1181 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1182 return FALSE;
1183 }
1184
1185 // If the return values from and to are the same, there is no
1186 // selection.
1187 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1188 {
1189 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1190
1191 gint from = -1;
1192 gint to = -1;
1193 bool haveSelection = FALSE;
1194
1195 #ifdef __WXGTK20__
1196 if (m_windowStyle & wxTE_MULTILINE)
1197 {
1198 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (m_text));
1199 GtkTextIter ifrom, ito;
1200 if ( gtk_text_buffer_get_selection_bounds(buffer, &ifrom, &ito) )
1201 {
1202 haveSelection = TRUE;
1203 from = gtk_text_iter_get_offset(&ifrom);
1204 to = gtk_text_iter_get_offset(&ito);
1205 }
1206 }
1207 else // not multi-line
1208 {
1209 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1210 &from, &to) )
1211 {
1212 haveSelection = TRUE;
1213 }
1214 }
1215 #else // not GTK2
1216 if ( (GTK_EDITABLE(m_text)->has_selection) )
1217 {
1218 haveSelection = TRUE;
1219 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1220 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1221 }
1222 #endif
1223
1224 if (! haveSelection )
1225 from = to = GetInsertionPoint();
1226
1227 if ( from > to )
1228 {
1229 // exchange them to be compatible with wxMSW
1230 gint tmp = from;
1231 from = to;
1232 to = tmp;
1233 }
1234
1235 if ( fromOut )
1236 *fromOut = from;
1237 if ( toOut )
1238 *toOut = to;
1239 }
1240
1241
1242 bool wxTextCtrl::IsEditable() const
1243 {
1244 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1245
1246 #ifdef __WXGTK20__
1247 if (m_windowStyle & wxTE_MULTILINE)
1248 {
1249 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1250 }
1251 else
1252 {
1253 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1254 }
1255 #else
1256 return GTK_EDITABLE(m_text)->editable;
1257 #endif
1258 }
1259
1260 bool wxTextCtrl::IsModified() const
1261 {
1262 return m_modified;
1263 }
1264
1265 void wxTextCtrl::Clear()
1266 {
1267 SetValue( wxT("") );
1268 }
1269
1270 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1271 {
1272 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1273
1274 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1275 {
1276 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1277 event.SetEventObject(this);
1278 event.SetString(GetValue());
1279 if (GetEventHandler()->ProcessEvent(event)) return;
1280 }
1281
1282 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1283 {
1284 // This will invoke the dialog default action, such
1285 // as the clicking the default button.
1286
1287 wxWindow *top_frame = m_parent;
1288 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1289 top_frame = top_frame->GetParent();
1290
1291 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1292 {
1293 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1294
1295 if (window->default_widget)
1296 {
1297 gtk_widget_activate (window->default_widget);
1298 return;
1299 }
1300 }
1301 }
1302
1303 key_event.Skip();
1304 }
1305
1306 GtkWidget* wxTextCtrl::GetConnectWidget()
1307 {
1308 return GTK_WIDGET(m_text);
1309 }
1310
1311 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1312 {
1313 if (m_windowStyle & wxTE_MULTILINE)
1314 {
1315 #ifdef __WXGTK20__
1316 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1317 #else
1318 return (window == GTK_TEXT(m_text)->text_area);
1319 #endif
1320 }
1321 else
1322 {
1323 return (window == GTK_ENTRY(m_text)->text_area);
1324 }
1325 }
1326
1327 // the font will change for subsequent text insertiongs
1328 bool wxTextCtrl::SetFont( const wxFont &font )
1329 {
1330 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1331
1332 if ( !wxTextCtrlBase::SetFont(font) )
1333 {
1334 // font didn't change, nothing to do
1335 return FALSE;
1336 }
1337
1338 if ( m_windowStyle & wxTE_MULTILINE )
1339 {
1340 m_updateFont = TRUE;
1341
1342 m_defaultStyle.SetFont(font);
1343
1344 ChangeFontGlobally();
1345 }
1346
1347 return TRUE;
1348 }
1349
1350 void wxTextCtrl::ChangeFontGlobally()
1351 {
1352 // this method is very inefficient and hence should be called as rarely as
1353 // possible!
1354 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1355 _T("shouldn't be called for single line controls") );
1356
1357 wxString value = GetValue();
1358 if ( !value.IsEmpty() )
1359 {
1360 m_updateFont = FALSE;
1361
1362 Clear();
1363 AppendText(value);
1364 }
1365 }
1366
1367 void wxTextCtrl::UpdateFontIfNeeded()
1368 {
1369 if ( m_updateFont )
1370 ChangeFontGlobally();
1371 }
1372
1373 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1374 {
1375 if ( !wxControl::SetForegroundColour(colour) )
1376 return FALSE;
1377
1378 // update default fg colour too
1379 m_defaultStyle.SetTextColour(colour);
1380
1381 return TRUE;
1382 }
1383
1384 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1385 {
1386 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1387
1388 if ( !wxControl::SetBackgroundColour( colour ) )
1389 return FALSE;
1390
1391 if (!m_widget->window)
1392 return FALSE;
1393
1394 wxColour sysbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
1395 if (sysbg.Red() == colour.Red() &&
1396 sysbg.Green() == colour.Green() &&
1397 sysbg.Blue() == colour.Blue())
1398 {
1399 return FALSE; // FIXME or TRUE?
1400 }
1401
1402 if (!m_backgroundColour.Ok())
1403 return FALSE;
1404
1405 if (m_windowStyle & wxTE_MULTILINE)
1406 {
1407 #ifndef __WXGTK20__
1408 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1409 if (!window)
1410 return FALSE;
1411 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1412 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1413 gdk_window_clear( window );
1414 #endif
1415 }
1416
1417 // change active background color too
1418 m_defaultStyle.SetBackgroundColour( colour );
1419
1420 return TRUE;
1421 }
1422
1423 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1424 {
1425 if ( m_windowStyle & wxTE_MULTILINE )
1426 {
1427 if ( style.IsDefault() )
1428 {
1429 // nothing to do
1430 return TRUE;
1431 }
1432 #ifdef __WXGTK20__
1433 GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
1434 gint l = gtk_text_buffer_get_char_count( text_buffer );
1435
1436 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1437 _T("invalid range in wxTextCtrl::SetStyle") );
1438
1439 GtkTextIter starti, endi;
1440 gtk_text_buffer_get_iter_at_offset( text_buffer, &starti, start );
1441 gtk_text_buffer_get_iter_at_offset( text_buffer, &endi, end );
1442
1443 // use the attributes from style which are set in it and fall back
1444 // first to the default style and then to the text control default
1445 // colours for the others
1446 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1447
1448 PangoFontDescription *font_description = attr.HasFont()
1449 ? attr.GetFont().GetNativeFontInfo()->description
1450 : NULL;
1451
1452 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
1453 : NULL;
1454
1455 GdkColor *colBg = attr.HasBackgroundColour()
1456 ? attr.GetBackgroundColour().GetColor()
1457 : NULL;
1458
1459 GtkTextTag *tag;
1460 tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description,
1461 "foreground-gdk", colFg,
1462 "background-gdk", colBg, NULL );
1463 gtk_text_buffer_apply_tag( text_buffer, tag, &starti, &endi );
1464
1465 return TRUE;
1466 #else
1467 // VERY dirty way to do that - removes the required text and re-adds it
1468 // with styling (FIXME)
1469
1470 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1471
1472 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1473 _T("invalid range in wxTextCtrl::SetStyle") );
1474
1475 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1476 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1477 wxString tmp(text,*wxConvCurrent);
1478 g_free( text );
1479
1480 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1481 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1482
1483 #if wxUSE_UNICODE
1484 wxWX2MBbuf buf = tmp.mbc_str();
1485 const char *txt = buf;
1486 size_t txtlen = strlen(buf);
1487 #else
1488 const char *txt = tmp;
1489 size_t txtlen = tmp.length();
1490 #endif
1491
1492 // use the attributes from style which are set in it and fall back
1493 // first to the default style and then to the text control default
1494 // colours for the others
1495 wxGtkTextInsert(m_text,
1496 wxTextAttr::Combine(style, m_defaultStyle, this),
1497 txt,
1498 txtlen);
1499
1500 /* does not seem to help under GTK+ 1.2 !!!
1501 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1502 SetInsertionPoint( old_pos );
1503 #endif
1504 return TRUE;
1505 }
1506 else // singe line
1507 {
1508 // cannot do this for GTK+'s Entry widget
1509 return FALSE;
1510 }
1511 }
1512
1513 void wxTextCtrl::ApplyWidgetStyle()
1514 {
1515 if (m_windowStyle & wxTE_MULTILINE)
1516 {
1517 // how ?
1518 }
1519 else
1520 {
1521 SetWidgetStyle();
1522 gtk_widget_set_style( m_text, m_widgetStyle );
1523 }
1524 }
1525
1526 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1527 {
1528 Cut();
1529 }
1530
1531 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1532 {
1533 Copy();
1534 }
1535
1536 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1537 {
1538 Paste();
1539 }
1540
1541 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1542 {
1543 Undo();
1544 }
1545
1546 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1547 {
1548 Redo();
1549 }
1550
1551 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1552 {
1553 event.Enable( CanCut() );
1554 }
1555
1556 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1557 {
1558 event.Enable( CanCopy() );
1559 }
1560
1561 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1562 {
1563 event.Enable( CanPaste() );
1564 }
1565
1566 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1567 {
1568 event.Enable( CanUndo() );
1569 }
1570
1571 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1572 {
1573 event.Enable( CanRedo() );
1574 }
1575
1576 void wxTextCtrl::OnInternalIdle()
1577 {
1578 wxCursor cursor = m_cursor;
1579 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1580
1581 if (cursor.Ok())
1582 {
1583 #ifndef __WXGTK20__
1584 GdkWindow *window = (GdkWindow*) NULL;
1585 if (HasFlag(wxTE_MULTILINE))
1586 window = GTK_TEXT(m_text)->text_area;
1587 else
1588 window = GTK_ENTRY(m_text)->text_area;
1589
1590 if (window)
1591 gdk_window_set_cursor( window, cursor.GetCursor() );
1592
1593 if (!g_globalCursor.Ok())
1594 cursor = *wxSTANDARD_CURSOR;
1595
1596 window = m_widget->window;
1597 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1598 gdk_window_set_cursor( window, cursor.GetCursor() );
1599 #endif
1600 }
1601
1602 if (g_delayedFocus == this)
1603 {
1604 if (GTK_WIDGET_REALIZED(m_widget))
1605 {
1606 gtk_widget_grab_focus( m_widget );
1607 g_delayedFocus = NULL;
1608 }
1609 }
1610
1611 if (wxUpdateUIEvent::CanUpdate(this))
1612 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1613 }
1614
1615 wxSize wxTextCtrl::DoGetBestSize() const
1616 {
1617 // FIXME should be different for multi-line controls...
1618 wxSize ret( wxControl::DoGetBestSize() );
1619 return wxSize(80, ret.y);
1620 }
1621
1622 // ----------------------------------------------------------------------------
1623 // freeze/thaw
1624 // ----------------------------------------------------------------------------
1625
1626 void wxTextCtrl::Freeze()
1627 {
1628 #ifndef __WXGTK20__
1629 if ( HasFlag(wxTE_MULTILINE) )
1630 {
1631 gtk_text_freeze(GTK_TEXT(m_text));
1632 }
1633 #endif
1634 }
1635
1636 void wxTextCtrl::Thaw()
1637 {
1638 #ifndef __WXGTK20__
1639 if ( HasFlag(wxTE_MULTILINE) )
1640 {
1641 GTK_TEXT(m_text)->vadj->value = 0.0;
1642
1643 gtk_text_thaw(GTK_TEXT(m_text));
1644 }
1645 #endif
1646 }
1647
1648 // ----------------------------------------------------------------------------
1649 // scrolling
1650 // ----------------------------------------------------------------------------
1651
1652 GtkAdjustment *wxTextCtrl::GetVAdj() const
1653 {
1654 #ifdef __WXGTK20__
1655 return NULL;
1656 #else
1657 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1658 #endif
1659 }
1660
1661 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1662 {
1663 #ifndef __WXGTK20__
1664 float value = adj->value + diff;
1665
1666 if ( value < 0 )
1667 value = 0;
1668
1669 float upper = adj->upper - adj->page_size;
1670 if ( value > upper )
1671 value = upper;
1672
1673 // did we noticeably change the scroll position?
1674 if ( fabs(adj->value - value) < 0.2 )
1675 {
1676 // well, this is what Robert does in wxScrollBar, so it must be good...
1677 return FALSE;
1678 }
1679
1680 adj->value = value;
1681
1682 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1683
1684 #endif
1685 return TRUE;
1686 }
1687
1688 bool wxTextCtrl::ScrollLines(int lines)
1689 {
1690 #ifdef __WXGTK20__
1691 return FALSE;
1692 #else
1693 GtkAdjustment *adj = GetVAdj();
1694 if ( !adj )
1695 return FALSE;
1696
1697 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1698 static const int KEY_SCROLL_PIXELS = 10;
1699
1700 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1701 #endif
1702 }
1703
1704 bool wxTextCtrl::ScrollPages(int pages)
1705 {
1706 #ifdef __WXGTK20__
1707 return FALSE;
1708 #else
1709 GtkAdjustment *adj = GetVAdj();
1710 if ( !adj )
1711 return FALSE;
1712
1713 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1714 #endif
1715 }
1716