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