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