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