]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
Add a public wxModalDialogHook class for intercepting modal dialogs.
[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
ce7fe42e 117 // generating a dummy wxEVT_TEXT event
ce2f50e3
VZ
118 win->IgnoreNextTextUpdate();
119
120 // and generate the correct one ourselves
ce7fe42e 121 wxCommandEvent event(wxEVT_TEXT_MAXLEN, win->GetId());
ce2f50e3
VZ
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
ce7fe42e 148 wxCommandEvent event( wxEVT_TEXT, 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
a0e55920 229wxTextCtrl::~wxTextCtrl()
9440c3d0 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
3a9fa0d6
VZ
717void wxTextCtrl::MarkDirty()
718{
7d8268a1 719 m_modified = true;
3a9fa0d6
VZ
720}
721
0efe5ba7
VZ
722void wxTextCtrl::DiscardEdits()
723{
7d8268a1 724 m_modified = false;
0efe5ba7
VZ
725}
726
ce2f50e3
VZ
727// ----------------------------------------------------------------------------
728// max text length support
729// ----------------------------------------------------------------------------
730
731void wxTextCtrl::IgnoreNextTextUpdate()
732{
7d8268a1 733 m_ignoreNextUpdate = true;
ce2f50e3
VZ
734}
735
736bool wxTextCtrl::IgnoreTextUpdate()
737{
738 if ( m_ignoreNextUpdate )
739 {
7d8268a1 740 m_ignoreNextUpdate = false;
ce2f50e3 741
7d8268a1 742 return true;
ce2f50e3
VZ
743 }
744
7d8268a1 745 return false;
ce2f50e3
VZ
746}
747
d7eee191
VZ
748void wxTextCtrl::SetMaxLength(unsigned long len)
749{
750 if ( !HasFlag(wxTE_MULTILINE) )
751 {
752 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
ce2f50e3
VZ
753
754 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
755 // we had tried to enter more text than allowed by max text length and
756 // the text wasn't really changed
757 //
758 // to detect this and generate TEXT_MAXLEN event instead of
759 // TEXT_CHANGED one in this case we also catch "insert_text" signal
760 //
761 // when max len is set to 0 we disconnect our handler as it means that
762 // we shouldn't check anything any more
763 if ( len )
764 {
765 gtk_signal_connect( GTK_OBJECT(m_text),
766 "insert_text",
767 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
768 (gpointer)this);
769 }
770 else // no checking
771 {
772 gtk_signal_disconnect_by_func
773 (
774 GTK_OBJECT(m_text),
775 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
776 (gpointer)this
777 );
778 }
d7eee191
VZ
779 }
780}
781
debe6624 782void wxTextCtrl::SetSelection( long from, long to )
c801d85f 783{
223d09f6 784 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 785
2b5f62a0
VZ
786 if (from == -1 && to == -1)
787 {
788 from = 0;
8e13c1ec 789 to = GetValue().length();
2b5f62a0
VZ
790 }
791
a1f79c1e
VZ
792 if ( (m_windowStyle & wxTE_MULTILINE) &&
793 !GTK_TEXT(m_text)->line_start_cache )
794 {
795 // tell the programmer that it didn't work
9a83f860 796 wxLogDebug(wxT("Can't call SetSelection() before realizing the control"));
a1f79c1e
VZ
797 return;
798 }
799
fab591c5
RR
800 if (m_windowStyle & wxTE_MULTILINE)
801 {
fab591c5 802 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
fab591c5
RR
803 }
804 else
805 {
806 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
807 }
6de97a3b 808}
c801d85f 809
afbe906a 810void wxTextCtrl::ShowPosition( long pos )
c801d85f 811{
afbe906a
RR
812 if (m_windowStyle & wxTE_MULTILINE)
813 {
814 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
815 float totalLines = (float) GetNumberOfLines();
816 long posX;
817 long posY;
818 PositionToXY(pos, &posX, &posY);
819 float posLine = (float) posY;
820 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
821 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
692c9b86 822 }
692c9b86
VZ
823}
824
03f38c58 825long wxTextCtrl::GetInsertionPoint() const
c801d85f 826{
223d09f6 827 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
3cbab641 828 return (long) GET_EDITABLE_POS(m_text);
6de97a3b 829}
c801d85f 830
7d8268a1 831wxTextPos wxTextCtrl::GetLastPosition() const
c801d85f 832{
223d09f6 833 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 834
2830bf19 835 int pos = 0;
a8bf1826 836
2830bf19 837 if (m_windowStyle & wxTE_MULTILINE)
fab591c5 838 {
2830bf19 839 pos = gtk_text_get_length( GTK_TEXT(m_text) );
fab591c5 840 }
2830bf19 841 else
fab591c5 842 {
2830bf19 843 pos = GTK_ENTRY(m_text)->text_length;
fab591c5 844 }
805dd538 845
ac0d36b5 846 return (long)pos;
6de97a3b 847}
c801d85f 848
debe6624 849void wxTextCtrl::Remove( long from, long to )
c801d85f 850{
223d09f6 851 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
581ee8a9 852 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 853}
c801d85f 854
debe6624 855void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 856{
223d09f6 857 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 858
581ee8a9 859 Remove( from, to );
bb69661b 860
7d8268a1 861 if (!value.empty())
2df7be7f
RR
862 {
863 gint pos = (gint)from;
05939a81 864#if wxUSE_UNICODE
2df7be7f
RR
865 wxWX2MBbuf buf = value.mbc_str();
866 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 867#else
8e13c1ec 868 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.length(), &pos );
581ee8a9 869#endif // wxUSE_UNICODE
2df7be7f 870 }
6de97a3b 871}
c801d85f 872
03f38c58 873void wxTextCtrl::Cut()
c801d85f 874{
223d09f6 875 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3cbab641 876 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 877}
c801d85f 878
03f38c58 879void wxTextCtrl::Copy()
c801d85f 880{
223d09f6 881 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3cbab641 882 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 883}
c801d85f 884
03f38c58 885void wxTextCtrl::Paste()
c801d85f 886{
223d09f6 887 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3cbab641 888 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 889}
c801d85f 890
ca8b28f2
JS
891// Undo/redo
892void wxTextCtrl::Undo()
893{
894 // TODO
223d09f6 895 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
896}
897
898void wxTextCtrl::Redo()
899{
900 // TODO
223d09f6 901 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
902}
903
904bool wxTextCtrl::CanUndo() const
905{
906 // TODO
4855a477 907 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
7d8268a1 908 return false;
ca8b28f2
JS
909}
910
911bool wxTextCtrl::CanRedo() const
912{
913 // TODO
4855a477 914 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
7d8268a1 915 return false;
ca8b28f2
JS
916}
917
918// If the return values from and to are the same, there is no
919// selection.
2d4cc5b6 920void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 921{
223d09f6 922 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 923
5b87f8bf
RD
924 gint from = -1;
925 gint to = -1;
7d8268a1 926 bool haveSelection = false;
5b87f8bf 927
5b87f8bf
RD
928 if ( (GTK_EDITABLE(m_text)->has_selection) )
929 {
7d8268a1 930 haveSelection = true;
5b87f8bf
RD
931 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
932 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
933 }
2d4cc5b6 934
5b87f8bf
RD
935 if (! haveSelection )
936 from = to = GetInsertionPoint();
937
938 if ( from > to )
939 {
940 // exchange them to be compatible with wxMSW
941 gint tmp = from;
942 from = to;
943 to = tmp;
944 }
bb69661b 945
2d4cc5b6
VZ
946 if ( fromOut )
947 *fromOut = from;
948 if ( toOut )
949 *toOut = to;
ca8b28f2
JS
950}
951
5b87f8bf 952
ca8b28f2
JS
953bool wxTextCtrl::IsEditable() const
954{
7d8268a1 955 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
05060eeb 956 return GTK_EDITABLE(m_text)->editable;
ca8b28f2
JS
957}
958
0efe5ba7
VZ
959bool wxTextCtrl::IsModified() const
960{
961 return m_modified;
962}
963
03f38c58 964void wxTextCtrl::Clear()
c801d85f 965{
902725ee 966 SetValue( wxEmptyString );
6de97a3b 967}
c801d85f 968
903f689b 969void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 970{
223d09f6 971 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 972
8e13c1ec 973 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
2830bf19 974 {
ce7fe42e 975 wxCommandEvent event(wxEVT_TEXT_ENTER, m_windowId);
2830bf19 976 event.SetEventObject(this);
f6bcfd97 977 event.SetString(GetValue());
937013e0 978 if (HandleWindowEvent(event)) return;
2830bf19 979 }
903f689b 980
12a3f227 981 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
da048e3d 982 {
2b328fc9
RR
983 // This will invoke the dialog default action, such
984 // as the clicking the default button.
a8bf1826 985
da048e3d 986 wxWindow *top_frame = m_parent;
8487f887 987 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 988 top_frame = top_frame->GetParent();
a8bf1826 989
2b328fc9 990 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
da048e3d 991 {
2b328fc9
RR
992 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
993
994 if (window->default_widget)
995 {
996 gtk_widget_activate (window->default_widget);
997 return;
998 }
13111b2a 999 }
da048e3d
RR
1000 }
1001
2830bf19 1002 key_event.Skip();
6de97a3b 1003}
c801d85f 1004
03f38c58 1005GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1006{
ae0bdb01 1007 return GTK_WIDGET(m_text);
6de97a3b 1008}
e3e65dac 1009
903f689b
RR
1010bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1011{
ae0bdb01 1012 if (m_windowStyle & wxTE_MULTILINE)
fab591c5 1013 {
ae0bdb01 1014 return (window == GTK_TEXT(m_text)->text_area);
fab591c5 1015 }
ae0bdb01 1016 else
fab591c5 1017 {
ae0bdb01 1018 return (window == GTK_ENTRY(m_text)->text_area);
fab591c5 1019 }
903f689b 1020}
e3e65dac 1021
bb69661b
VZ
1022// the font will change for subsequent text insertiongs
1023bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1024{
7d8268a1 1025 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
8bbe427f 1026
a66954a6 1027 if ( !wxTextCtrlBase::SetFont(font) )
bb69661b
VZ
1028 {
1029 // font didn't change, nothing to do
7d8268a1 1030 return false;
bb69661b
VZ
1031 }
1032
1033 if ( m_windowStyle & wxTE_MULTILINE )
1034 {
7d8268a1 1035 SetUpdateFont(true);
bb69661b 1036
1ff4714d
VZ
1037 m_defaultStyle.SetFont(font);
1038
01041145 1039 ChangeFontGlobally();
bb69661b
VZ
1040 }
1041
7d8268a1 1042 return true;
58614078
RR
1043}
1044
01041145
VZ
1045void wxTextCtrl::ChangeFontGlobally()
1046{
1047 // this method is very inefficient and hence should be called as rarely as
1048 // possible!
22800f32
JS
1049 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1050
9a83f860 1051 wxT("shouldn't be called for single line controls") );
01041145
VZ
1052
1053 wxString value = GetValue();
7d8268a1 1054 if ( !value.empty() )
01041145 1055 {
7d8268a1 1056 SetUpdateFont(false);
572aeb77 1057
01041145
VZ
1058 Clear();
1059 AppendText(value);
01041145
VZ
1060 }
1061}
1062
1063void wxTextCtrl::UpdateFontIfNeeded()
1064{
1065 if ( m_updateFont )
1066 ChangeFontGlobally();
1067}
1068
17665a2b
VZ
1069bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1070{
1071 if ( !wxControl::SetForegroundColour(colour) )
7d8268a1 1072 return false;
17665a2b
VZ
1073
1074 // update default fg colour too
1075 m_defaultStyle.SetTextColour(colour);
1076
7d8268a1 1077 return true;
17665a2b
VZ
1078}
1079
f03fc89f 1080bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1081{
7d8268a1 1082 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
a81258be 1083
1f477433 1084 if ( !wxControl::SetBackgroundColour( colour ) )
7d8268a1 1085 return false;
3358d36e 1086
f03fc89f 1087 if (!m_widget->window)
7d8268a1 1088 return false;
8bbe427f 1089
a1b806b9 1090 if (!m_backgroundColour.IsOk())
7d8268a1 1091 return false;
8bbe427f 1092
ae0bdb01
RR
1093 if (m_windowStyle & wxTE_MULTILINE)
1094 {
1095 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f 1096 if (!window)
7d8268a1 1097 return false;
ae0bdb01
RR
1098 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1099 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1100 gdk_window_clear( window );
1101 }
f03fc89f 1102
17665a2b
VZ
1103 // change active background color too
1104 m_defaultStyle.SetBackgroundColour( colour );
1105
7d8268a1 1106 return true;
58614078
RR
1107}
1108
eda40bfc 1109bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
17665a2b 1110{
17665a2b
VZ
1111 if ( m_windowStyle & wxTE_MULTILINE )
1112 {
1113 if ( style.IsDefault() )
1114 {
1115 // nothing to do
7d8268a1 1116 return true;
17665a2b 1117 }
902725ee 1118
cc3da3f8
RR
1119 // VERY dirty way to do that - removes the required text and re-adds it
1120 // with styling (FIXME)
5b87f8bf 1121
17665a2b
VZ
1122 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1123
7d8268a1 1124 wxCHECK_MSG( start >= 0 && end <= l, false,
9a83f860 1125 wxT("invalid range in wxTextCtrl::SetStyle") );
17665a2b
VZ
1126
1127 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1128 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1129 wxString tmp(text,*wxConvCurrent);
1130 g_free( text );
1131
1132 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1133 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1134
902725ee 1135 #if wxUSE_UNICODE
17665a2b
VZ
1136 wxWX2MBbuf buf = tmp.mbc_str();
1137 const char *txt = buf;
1138 size_t txtlen = strlen(buf);
902725ee 1139 #else
17665a2b
VZ
1140 const char *txt = tmp;
1141 size_t txtlen = tmp.length();
902725ee 1142 #endif
17665a2b 1143
eda40bfc
VZ
1144 // use the attributes from style which are set in it and fall back
1145 // first to the default style and then to the text control default
1146 // colours for the others
1147 wxGtkTextInsert(m_text,
1148 wxTextAttr::Combine(style, m_defaultStyle, this),
1149 txt,
1150 txtlen);
17665a2b
VZ
1151
1152 /* does not seem to help under GTK+ 1.2 !!!
1153 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1154 SetInsertionPoint( old_pos );
902725ee 1155
7d8268a1 1156 return true;
17665a2b 1157 }
902725ee
WS
1158
1159 // else single line
1160 // cannot do this for GTK+'s Entry widget
1161 return false;
17665a2b
VZ
1162}
1163
f40fdaa3 1164void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
58614078 1165{
f40fdaa3 1166 gtk_widget_modify_style(m_text, style);
68dda785 1167}
f96aa4d9 1168
e702ff0f
JS
1169void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1170{
1171 Cut();
1172}
1173
1174void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1175{
1176 Copy();
1177}
1178
1179void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1180{
1181 Paste();
1182}
1183
1184void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1185{
1186 Undo();
1187}
1188
1189void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1190{
1191 Redo();
1192}
1193
1194void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1195{
1196 event.Enable( CanCut() );
1197}
1198
1199void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1200{
1201 event.Enable( CanCopy() );
1202}
1203
1204void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1205{
1206 event.Enable( CanPaste() );
1207}
1208
1209void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1210{
1211 event.Enable( CanUndo() );
1212}
1213
1214void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1215{
1216 event.Enable( CanRedo() );
1217}
65045edd
RR
1218
1219void wxTextCtrl::OnInternalIdle()
1220{
1221 wxCursor cursor = m_cursor;
a1b806b9 1222 if (g_globalCursor.IsOk()) cursor = g_globalCursor;
65045edd 1223
a1b806b9 1224 if (cursor.IsOk())
65045edd 1225 {
d3b9f782 1226 GdkWindow *window = NULL;
13111b2a 1227 if (HasFlag(wxTE_MULTILINE))
65045edd
RR
1228 window = GTK_TEXT(m_text)->text_area;
1229 else
1230 window = GTK_ENTRY(m_text)->text_area;
13111b2a 1231
65045edd
RR
1232 if (window)
1233 gdk_window_set_cursor( window, cursor.GetCursor() );
1234
a1b806b9 1235 if (!g_globalCursor.IsOk())
65045edd
RR
1236 cursor = *wxSTANDARD_CURSOR;
1237
1238 window = m_widget->window;
247e5b16 1239 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd
RR
1240 gdk_window_set_cursor( window, cursor.GetCursor() );
1241 }
26bf1ce0 1242
d7fa7eaa
RR
1243 if (g_delayedFocus == this)
1244 {
1245 if (GTK_WIDGET_REALIZED(m_widget))
1246 {
1247 gtk_widget_grab_focus( m_widget );
1248 g_delayedFocus = NULL;
1249 }
1250 }
1251
e39af974
JS
1252 if (wxUpdateUIEvent::CanUpdate(this))
1253 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
65045edd 1254}
f68586e5
VZ
1255
1256wxSize wxTextCtrl::DoGetBestSize() const
1257{
1258 // FIXME should be different for multi-line controls...
0279e844 1259 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
1260 wxSize best(80, ret.y);
1261 CacheBestSize(best);
1262 return best;
f68586e5 1263}
0cc7251e 1264
9cd6d737
VZ
1265// ----------------------------------------------------------------------------
1266// freeze/thaw
1267// ----------------------------------------------------------------------------
1268
17808a75 1269void wxTextCtrl::DoFreeze()
0cc7251e
VZ
1270{
1271 if ( HasFlag(wxTE_MULTILINE) )
1272 {
41b81aed 1273 gtk_text_freeze(GTK_TEXT(m_text));
41b81aed 1274 }
0cc7251e
VZ
1275}
1276
17808a75 1277void wxTextCtrl::DoThaw()
0cc7251e
VZ
1278{
1279 if ( HasFlag(wxTE_MULTILINE) )
1280 {
817ec43a
VZ
1281 GTK_TEXT(m_text)->vadj->value = 0.0;
1282
0cc7251e 1283 gtk_text_thaw(GTK_TEXT(m_text));
9440c3d0 1284 }
9440c3d0 1285}
9440c3d0 1286
9cd6d737
VZ
1287// ----------------------------------------------------------------------------
1288// scrolling
1289// ----------------------------------------------------------------------------
1290
1291GtkAdjustment *wxTextCtrl::GetVAdj() const
1292{
1ce52aa6
VZ
1293 if ( !IsMultiLine() )
1294 return NULL;
1295
1ce52aa6 1296 return GTK_TEXT(m_text)->vadj;
9cd6d737
VZ
1297}
1298
1299bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1300{
1301 float value = adj->value + diff;
1302
1303 if ( value < 0 )
1304 value = 0;
1305
1306 float upper = adj->upper - adj->page_size;
1307 if ( value > upper )
1308 value = upper;
1309
1310 // did we noticeably change the scroll position?
1311 if ( fabs(adj->value - value) < 0.2 )
1312 {
1313 // well, this is what Robert does in wxScrollBar, so it must be good...
7d8268a1 1314 return false;
9cd6d737
VZ
1315 }
1316
1317 adj->value = value;
9cd6d737 1318 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1ce52aa6 1319
7d8268a1 1320 return true;
9cd6d737
VZ
1321}
1322
1323bool wxTextCtrl::ScrollLines(int lines)
1324{
1325 GtkAdjustment *adj = GetVAdj();
1326 if ( !adj )
7d8268a1 1327 return false;
9cd6d737
VZ
1328
1329 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1ce52aa6 1330 int diff = 10*lines;
1ce52aa6
VZ
1331
1332 return DoScroll(adj, diff);
9cd6d737
VZ
1333}
1334
1335bool wxTextCtrl::ScrollPages(int pages)
1336{
1337 GtkAdjustment *adj = GetVAdj();
1338 if ( !adj )
7d8268a1 1339 return false;
9cd6d737 1340
817ec43a 1341 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
9cd6d737
VZ
1342}
1343
9d522606
RD
1344
1345// static
1346wxVisualAttributes
1347wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1348{
1349 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1350}