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