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