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