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