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