]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
Separate NSAffineTransform creation out of wxDC::CocoaApplyTransform into
[wxWidgets.git] / src / gtk1 / textctrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
11#pragma implementation "textctrl.h"
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
c801d85f
KB
17#include "wx/textctrl.h"
18#include "wx/utils.h"
ae0bdb01 19#include "wx/intl.h"
a1f79c1e 20#include "wx/log.h"
ae0bdb01 21#include "wx/settings.h"
fc71ef6e 22#include "wx/panel.h"
fab591c5 23#include "wx/strconv.h"
cc3da3f8 24#include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
c801d85f 25
a81258be
RR
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <ctype.h>
463c4d71 29#include "wx/math.h"
a81258be 30
9e691f46
VZ
31#include "wx/gtk/private.h"
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
RR
45extern bool g_blockEventsOnDrag;
46extern wxCursor g_globalCursor;
d7fa7eaa 47extern wxWindowGTK *g_delayedFocus;
b292e2f5 48
eda40bfc
VZ
49// ----------------------------------------------------------------------------
50// helpers
51// ----------------------------------------------------------------------------
52
cc3da3f8 53#ifdef __WXGTK20__
25497324
KH
54static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
55 const wxTextAttr& attr,
56 GtkTextIter *start,
57 GtkTextIter *end)
58{
59 static gchar buf[1024];
60 GtkTextTag *tag;
61
62 if (attr.HasFont())
63 {
64 char *font_string;
65 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
66 font_string = pango_font_description_to_string(font_description);
67 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
68 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
69 buf );
70 if (!tag)
71 tag = gtk_text_buffer_create_tag( text_buffer, buf,
72 "font-desc", font_description,
73 NULL );
74 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
75 g_free (font_string);
76 }
77
78 if (attr.HasTextColour())
79 {
80 GdkColor *colFg = attr.GetTextColour().GetColor();
81 g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
82 colFg->red, colFg->green, colFg->blue);
83 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
84 buf );
85 if (!tag)
86 tag = gtk_text_buffer_create_tag( text_buffer, buf,
87 "foreground-gdk", colFg, NULL );
88 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
89 }
90
91 if (attr.HasBackgroundColour())
92 {
93 GdkColor *colBg = attr.GetBackgroundColour().GetColor();
94 g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
95 colBg->red, colBg->green, colBg->blue);
96 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
97 buf );
98 if (!tag)
99 tag = gtk_text_buffer_create_tag( text_buffer, buf,
100 "background-gdk", colBg, NULL );
101 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
102 }
103}
104
cc3da3f8
RR
105static void wxGtkTextInsert(GtkWidget *text,
106 GtkTextBuffer *text_buffer,
107 const wxTextAttr& attr,
108 wxCharBuffer buffer)
109
110{
25497324
KH
111 gint start_offset;
112 GtkTextIter iter, start;
cc3da3f8 113
a61bbf87
JS
114 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
115 gtk_text_buffer_get_insert (text_buffer) );
25497324
KH
116 start_offset = gtk_text_iter_get_offset (&iter);
117 gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) );
118
119 gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
a61bbf87 120
25497324 121 wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
cc3da3f8
RR
122}
123#else
eda40bfc
VZ
124static void wxGtkTextInsert(GtkWidget *text,
125 const wxTextAttr& attr,
126 const char *txt,
127 size_t len)
128{
129 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
130 : NULL;
131
132 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
133 : NULL;
134
135 GdkColor *colBg = attr.HasBackgroundColour()
136 ? attr.GetBackgroundColour().GetColor()
137 : NULL;
138
139 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
140}
a8bf1826 141#endif // GTK 1.x
eda40bfc 142
ce2f50e3
VZ
143// ----------------------------------------------------------------------------
144// "insert_text" for GtkEntry
145// ----------------------------------------------------------------------------
146
147static void
148gtk_insert_text_callback(GtkEditable *editable,
149 const gchar *new_text,
150 gint new_text_length,
151 gint *position,
152 wxTextCtrl *win)
153{
76fcf0f2
RR
154 if (g_isIdle)
155 wxapp_install_idle_handler();
156
ce2f50e3
VZ
157 // we should only be called if we have a max len limit at all
158 GtkEntry *entry = GTK_ENTRY (editable);
159
160 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
161
162 // check that we don't overflow the max length limit
163 //
164 // FIXME: this doesn't work when we paste a string which is going to be
165 // truncated
166 if ( entry->text_length == entry->text_max_length )
167 {
168 // we don't need to run the base class version at all
169 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
170
171 // remember that the next changed signal is to be ignored to avoid
172 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
173 win->IgnoreNextTextUpdate();
174
175 // and generate the correct one ourselves
176 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
177 event.SetEventObject(win);
178 event.SetString(win->GetValue());
179 win->GetEventHandler()->ProcessEvent( event );
180 }
181}
182
c801d85f 183//-----------------------------------------------------------------------------
2f2aa628 184// "changed"
c801d85f
KB
185//-----------------------------------------------------------------------------
186
805dd538 187static void
ba3f6b44 188gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
484e45bf 189{
ce2f50e3
VZ
190 if ( win->IgnoreTextUpdate() )
191 return;
192
a2053b27 193 if (!win->m_hasVMT) return;
805dd538 194
bb69661b 195 if (g_isIdle)
3c679789 196 wxapp_install_idle_handler();
a8bf1826 197
034be888 198 win->SetModified();
c04ec496 199#ifndef __WXGTK20__
01041145 200 win->UpdateFontIfNeeded();
c04ec496 201#endif // !__WXGTK20__
a8bf1826 202
f03fc89f 203 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
2830bf19 204 event.SetEventObject( win );
ce2f50e3 205 event.SetString( win->GetValue() );
2830bf19 206 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 207}
112892b9 208
41b81aed
RR
209//-----------------------------------------------------------------------------
210// "expose_event" from scrolled window and textview
211//-----------------------------------------------------------------------------
212
213#ifdef __WXGTK20__
214static gboolean
215gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
216{
217 return TRUE;
218}
219#endif
220
2830bf19 221//-----------------------------------------------------------------------------
034be888 222// "changed" from vertical scrollbar
2830bf19
RR
223//-----------------------------------------------------------------------------
224
fab591c5 225#ifndef __WXGTK20__
805dd538 226static void
034be888 227gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
2830bf19 228{
3c679789 229 if (!win->m_hasVMT) return;
bb69661b
VZ
230
231 if (g_isIdle)
3c679789 232 wxapp_install_idle_handler();
acfd422a 233
2830bf19
RR
234 win->CalculateScrollbar();
235}
fab591c5 236#endif
2830bf19 237
1c35b54e
VZ
238// ----------------------------------------------------------------------------
239// redraw callback for multiline text
240// ----------------------------------------------------------------------------
241
242#ifndef __WXGTK20__
243
244// redrawing a GtkText from inside a wxYield() call results in crashes (the
245// text sample shows it in its "Add lines" command which shows wxProgressDialog
246// which implicitly calls wxYield()) so we override GtkText::draw() and simply
247// don't do anything if we're inside wxYield()
248
249extern bool wxIsInsideYield;
250
2e08b1a3
VZ
251extern "C" {
252 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
253}
1c35b54e
VZ
254
255static GtkDrawCallback gs_gtk_text_draw = NULL;
256
257extern "C"
258void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
259{
260 if ( !wxIsInsideYield )
261 {
262 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
263 _T("infinite recursion in wxgtk_text_draw aborted") );
264
265 gs_gtk_text_draw(widget, rect);
266 }
267}
268
269#endif // __WXGTK20__
270
2f2aa628
RR
271//-----------------------------------------------------------------------------
272// wxTextCtrl
273//-----------------------------------------------------------------------------
274
275IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
276
c801d85f 277BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 278 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
279
280 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
281 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
282 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
283 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
284 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
285
286 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
287 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
288 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
289 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
290 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
c801d85f
KB
291END_EVENT_TABLE()
292
01041145 293void wxTextCtrl::Init()
f5abe911 294{
ce2f50e3 295 m_ignoreNextUpdate =
f5abe911 296 m_modified = FALSE;
c04ec496 297 SetUpdateFont(FALSE);
3ca6a5f0
BP
298 m_text =
299 m_vScrollbar = (GtkWidget *)NULL;
41b81aed
RR
300#ifdef __WXGTK20__
301 m_frozenness = 0;
302#endif
f5abe911 303}
13289f04 304
13111b2a
VZ
305wxTextCtrl::wxTextCtrl( wxWindow *parent,
306 wxWindowID id,
307 const wxString &value,
308 const wxPoint &pos,
309 const wxSize &size,
310 long style,
311 const wxValidator& validator,
312 const wxString &name )
f5abe911 313{
01041145
VZ
314 Init();
315
f5abe911
RR
316 Create( parent, id, value, pos, size, style, validator, name );
317}
c801d85f 318
13111b2a
VZ
319bool wxTextCtrl::Create( wxWindow *parent,
320 wxWindowID id,
321 const wxString &value,
322 const wxPoint &pos,
323 const wxSize &size,
324 long style,
325 const wxValidator& validator,
326 const wxString &name )
c801d85f 327{
2830bf19 328 m_needParent = TRUE;
b292e2f5 329 m_acceptsFocus = TRUE;
484e45bf 330
4dcaf11a
RR
331 if (!PreCreation( parent, pos, size ) ||
332 !CreateBase( parent, id, pos, size, style, validator, name ))
333 {
223d09f6 334 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
13111b2a 335 return FALSE;
4dcaf11a 336 }
6de97a3b 337
805dd538 338
034be888 339 m_vScrollbarVisible = FALSE;
13289f04 340
2830bf19 341 bool multi_line = (style & wxTE_MULTILINE) != 0;
a8bf1826 342
ab46dc18 343 if (multi_line)
2830bf19 344 {
fab591c5
RR
345#ifdef __WXGTK20__
346 // Create view
347 m_text = gtk_text_view_new();
a8bf1826 348
41b81aed 349 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
a8bf1826 350
fab591c5
RR
351 // create scrolled window
352 m_widget = gtk_scrolled_window_new( NULL, NULL );
353 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
354 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
a8bf1826 355
fab591c5
RR
356 // Insert view into scrolled window
357 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
a8bf1826 358
fab591c5 359 // Global settings which can be overridden by tags, I guess.
9d522606 360 if (HasFlag( wxHSCROLL ) || HasFlag( wxTE_DONTWRAP ))
fab591c5
RR
361 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE );
362 else
363 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD );
805dd538 364
fab591c5
RR
365 if (!HasFlag(wxNO_BORDER))
366 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
055e633d
RR
367
368 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
fab591c5
RR
369#else
370 // create our control ...
2830bf19
RR
371 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
372
fab591c5
RR
373 // ... and put into the upper left hand corner of the table
374 bool bHasHScrollbar = FALSE;
2830bf19 375 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
5664fc32 376 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19 377 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 378 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
379 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
380 0, 0);
bb69661b 381
fab591c5 382 // always wrap words
5c387335 383 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
bb69661b 384
fab591c5 385 // finally, put the vertical scrollbar in the upper right corner
ab46dc18
RR
386 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
387 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
ab46dc18
RR
388 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
389 GTK_FILL,
390 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
391 0, 0);
fab591c5 392#endif
2830bf19
RR
393 }
394 else
395 {
fab591c5 396 // a single-line text control: no need for scrollbars
2830bf19 397 m_widget =
1c35b54e 398 m_text = gtk_entry_new();
44c5573d
RR
399
400#ifdef __WXGTK20__
02a6e358
RR
401 if (style & wxNO_BORDER)
402 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
44c5573d 403#endif
2830bf19 404 }
484e45bf 405
db434467 406 m_parent->DoAddChild( this );
eda40bfc 407
76fcf0f2 408 m_focusWidget = m_text;
db434467 409
abdeb9e7 410 PostCreation(size);
484e45bf 411
2830bf19 412 if (multi_line)
2830bf19 413 gtk_widget_show(m_text);
13289f04 414
fab591c5 415#ifndef __WXGTK20__
ab46dc18
RR
416 if (multi_line)
417 {
418 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
419 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
1c35b54e 420
1c35b54e
VZ
421 // only initialize gs_gtk_text_draw once, starting from the next the
422 // klass::draw will already be wxgtk_text_draw
423 if ( !gs_gtk_text_draw )
424 {
425 GtkDrawCallback&
426 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
427
428 gs_gtk_text_draw = draw;
429
430 draw = wxgtk_text_draw;
431 }
ab46dc18 432 }
fab591c5 433#endif // GTK+ 1.x
3358d36e 434
291a8f20 435 if (!value.IsEmpty())
2830bf19 436 {
fab591c5
RR
437#ifdef __WXGTK20__
438 SetValue( value );
439#else
3358d36e 440
9e691f46 441#if !GTK_CHECK_VERSION(1, 2, 0)
3358d36e
VZ
442 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
443 // gtk_editable_insert_text()
444 gtk_widget_realize(m_text);
445#endif // GTK 1.0
446
fab591c5 447 gint tmp = 0;
05939a81 448#if wxUSE_UNICODE
3358d36e 449 wxWX2MBbuf val = value.mbc_str();
05939a81 450 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
fab591c5 451#else
2830bf19 452 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
fab591c5 453#endif
3358d36e 454
291a8f20
RR
455 if (multi_line)
456 {
fab591c5 457 // Bring editable's cursor uptodate. Bug in GTK.
9e691f46 458 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
3358d36e 459 }
a8bf1826 460
fab591c5 461#endif
2830bf19 462 }
484e45bf 463
2830bf19
RR
464 if (style & wxTE_PASSWORD)
465 {
466 if (!multi_line)
467 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
468 }
8bbe427f 469
2830bf19
RR
470 if (style & wxTE_READONLY)
471 {
472 if (!multi_line)
473 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
fab591c5
RR
474#ifdef __WXGTK20__
475 else
98a8daf4
VS
476 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
477#else
478 }
479 else
480 {
481 if (multi_line)
482 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
483#endif
484 }
485
c663fbea
VS
486#ifdef __WXGTK20__
487 if (multi_line)
488 {
489 if (style & wxTE_RIGHT)
490 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
491 else if (style & wxTE_CENTRE)
492 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
493 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
494 }
c663fbea
VS
495 else
496 {
77f70672
RR
497#ifdef __WXGTK24__
498 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
499 if (!gtk_check_version(2,4,0))
500 {
501 if (style & wxTE_RIGHT)
502 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
503 else if (style & wxTE_CENTRE)
504 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
505 }
506#endif
c663fbea 507 }
c663fbea 508#endif // __WXGTK20__
9d522606 509
fab591c5
RR
510 // We want to be notified about text changes.
511#ifdef __WXGTK20__
512 if (multi_line)
513 {
41b81aed 514 g_signal_connect( G_OBJECT(m_buffer), "changed",
fab591c5
RR
515 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
516 }
517 else
518#endif
9d522606 519
fab591c5 520 {
055e633d
RR
521 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
522 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
fab591c5 523 }
ce16e5d7 524
65045edd 525 m_cursor = wxCursor( wxCURSOR_IBEAM );
13111b2a 526
9d522606 527 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
17665a2b
VZ
528 SetDefaultStyle( attrDef );
529
2830bf19
RR
530 return TRUE;
531}
484e45bf 532
9d522606 533
2830bf19
RR
534void wxTextCtrl::CalculateScrollbar()
535{
fab591c5 536#ifndef __WXGTK20__
2830bf19 537 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
f96aa4d9 538
2830bf19 539 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
805dd538 540
2830bf19
RR
541 if (adj->upper - adj->page_size < 0.8)
542 {
543 if (m_vScrollbarVisible)
544 {
034be888 545 gtk_widget_hide( m_vScrollbar );
034be888 546 m_vScrollbarVisible = FALSE;
2830bf19
RR
547 }
548 }
549 else
550 {
551 if (!m_vScrollbarVisible)
552 {
034be888 553 gtk_widget_show( m_vScrollbar );
034be888 554 m_vScrollbarVisible = TRUE;
2830bf19
RR
555 }
556 }
fab591c5 557#endif
6de97a3b 558}
c801d85f 559
03f38c58 560wxString wxTextCtrl::GetValue() const
c801d85f 561{
223d09f6 562 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
8bbe427f 563
2830bf19
RR
564 wxString tmp;
565 if (m_windowStyle & wxTE_MULTILINE)
566 {
fab591c5 567#ifdef __WXGTK20__
fab591c5 568 GtkTextIter start;
41b81aed 569 gtk_text_buffer_get_start_iter( m_buffer, &start );
fab591c5 570 GtkTextIter end;
41b81aed
RR
571 gtk_text_buffer_get_end_iter( m_buffer, &end );
572 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
5b87f8bf 573
fab591c5
RR
574#if wxUSE_UNICODE
575 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
576#else
577 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
578#endif
39ccbf9a
VZ
579 if ( buffer )
580 tmp = buffer;
a8bf1826 581
fab591c5
RR
582 g_free( text );
583#else
2830bf19
RR
584 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
585 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
fab591c5 586 tmp = text;
2830bf19 587 g_free( text );
fab591c5 588#endif
2830bf19
RR
589 }
590 else
591 {
fab591c5 592 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
2830bf19 593 }
a8bf1826 594
2830bf19 595 return tmp;
6de97a3b 596}
c801d85f
KB
597
598void wxTextCtrl::SetValue( const wxString &value )
599{
223d09f6 600 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 601
2830bf19
RR
602 if (m_windowStyle & wxTE_MULTILINE)
603 {
fab591c5
RR
604#ifdef __WXGTK20__
605
606#if wxUSE_UNICODE
607 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
608#else
609 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
a8bf1826 610#endif
b6ae8db8 611 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
ad68ed32
RR
612 IgnoreNextTextUpdate();
613
41b81aed 614 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
a8bf1826 615
fab591c5 616#else
2830bf19
RR
617 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
618 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
619 len = 0;
01041145 620 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
05939a81 621#endif
2830bf19
RR
622 }
623 else
624 {
fab591c5 625 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
2830bf19 626 }
f6bcfd97
BP
627
628 // GRG, Jun/2000: Changed this after a lot of discussion in
77ffb593 629 // the lists. wxWidgets 2.2 will have a set of flags to
f6bcfd97
BP
630 // customize this behaviour.
631 SetInsertionPoint(0);
a8bf1826 632
f6bcfd97 633 m_modified = FALSE;
6de97a3b 634}
c801d85f
KB
635
636void wxTextCtrl::WriteText( const wxString &text )
637{
223d09f6 638 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 639
17665a2b
VZ
640 if ( text.empty() )
641 return;
484e45bf 642
c04ec496
VZ
643 // gtk_text_changed_callback() will set m_modified to true but m_modified
644 // shouldn't be changed by the program writing to the text control itself,
645 // so save the old value and restore when we're done
646 bool oldModified = m_modified;
647
17665a2b 648 if ( m_windowStyle & wxTE_MULTILINE )
2830bf19 649 {
fab591c5
RR
650#ifdef __WXGTK20__
651
652#if wxUSE_UNICODE
653 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
654#else
655 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
a8bf1826 656#endif
8e033d81
VZ
657 if ( !buffer )
658 {
659 // what else can we do? at least don't crash...
660 return;
661 }
662
e136b747 663 // TODO: Call whatever is needed to delete the selection.
41b81aed 664 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
a8bf1826 665
71aba833
VZ
666 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
667 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
668 if ( adj->value == adj->upper - adj->page_size )
669 {
670 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
41b81aed 671 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
71aba833 672 }
a8bf1826 673#else // GTK 1.x
ba3f6b44 674 // After cursor movements, gtk_text_get_point() is wrong by one.
9e691f46 675 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
eda40bfc 676
41b2e0e6
VZ
677 // always use m_defaultStyle, even if it is empty as otherwise
678 // resetting the style and appending some more text wouldn't work: if
679 // we don't specify the style explicitly, the old style would be used
2b5f62a0 680 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
fab591c5 681 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
eda40bfc 682
07e9d4f0
VZ
683 // we called wxGtkTextInsert with correct font, no need to do anything
684 // in UpdateFontIfNeeded() any longer
685 if ( !text.empty() )
686 {
c04ec496 687 SetUpdateFont(FALSE);
07e9d4f0
VZ
688 }
689
ba3f6b44 690 // Bring editable's cursor back uptodate.
9e691f46 691 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
a8bf1826 692#endif // GTK 1.x/2.0
2830bf19 693 }
17665a2b 694 else // single line
2830bf19 695 {
2b5f62a0
VZ
696 // First remove the selection if there is one
697 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
698
ba3f6b44 699 // This moves the cursor pos to behind the inserted text.
9e691f46 700 gint len = GET_EDITABLE_POS(m_text);
a8bf1826 701
fab591c5
RR
702#ifdef __WXGTK20__
703
704#if wxUSE_UNICODE
705 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
706#else
707 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
a8bf1826 708#endif
fab591c5 709 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
a8bf1826 710
fab591c5
RR
711#else
712 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
713#endif
3358d36e 714
ba3f6b44 715 // Bring entry's cursor uptodate.
9e691f46 716 gtk_entry_set_position( GTK_ENTRY(m_text), len );
2830bf19 717 }
eda40bfc 718
c04ec496 719 m_modified = oldModified;
6de97a3b 720}
c801d85f 721
a6e21573
HH
722void wxTextCtrl::AppendText( const wxString &text )
723{
ba3f6b44
RR
724 SetInsertionPointEnd();
725 WriteText( text );
726}
2df7be7f 727
ba3f6b44
RR
728wxString wxTextCtrl::GetLineText( long lineNo ) const
729{
a6e21573
HH
730 if (m_windowStyle & wxTE_MULTILINE)
731 {
fab591c5 732#ifndef __WXGTK20__
ba3f6b44
RR
733 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
734 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
bb69661b 735
ba3f6b44
RR
736 if (text)
737 {
738 wxString buf(wxT(""));
739 long i;
740 int currentLine = 0;
741 for (i = 0; currentLine != lineNo && text[i]; i++ )
742 if (text[i] == '\n')
743 currentLine++;
744 // Now get the text
745 int j;
746 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
747 buf += text[i];
17665a2b 748
ba3f6b44
RR
749 g_free( text );
750 return buf;
bb69661b 751 }
ba3f6b44 752 else
bb69661b 753 {
ba3f6b44 754 return wxEmptyString;
bb69661b 755 }
905f2110 756#else
905f2110 757 GtkTextIter line;
41b81aed 758 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
905f2110 759 GtkTextIter end;
41b81aed
RR
760 gtk_text_buffer_get_end_iter(m_buffer,&end );
761 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
58192970 762 wxString result(wxGTK_CONV_BACK(text));
905f2110
JS
763 g_free(text);
764 return result.BeforeFirst(wxT('\n'));
765#endif
a6e21573 766 }
ba3f6b44 767 else
a81258be 768 {
ba3f6b44
RR
769 if (lineNo == 0) return GetValue();
770 return wxEmptyString;
a81258be 771 }
6de97a3b 772}
c801d85f 773
a81258be
RR
774void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
775{
ac0d36b5
HH
776 /* If you implement this, don't forget to update the documentation!
777 * (file docs/latex/wx/text.tex) */
223d09f6 778 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 779}
112892b9 780
0efe5ba7 781bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 782{
96385642 783 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 784 {
96385642
VZ
785 wxString text = GetValue();
786
6085b116 787 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 788 if( (unsigned long)pos > text.Len() )
96385642
VZ
789 return FALSE;
790
ac0d36b5
HH
791 *x=0; // First Col
792 *y=0; // First Line
2829d9e3 793
05939a81
OK
794 const wxChar* stop = text.c_str() + pos;
795 for ( const wxChar *p = text.c_str(); p < stop; p++ )
96385642 796 {
223d09f6 797 if (*p == wxT('\n'))
96385642
VZ
798 {
799 (*y)++;
ac0d36b5 800 *x=0;
96385642
VZ
801 }
802 else
803 (*x)++;
804 }
805dd538 805 }
96385642
VZ
806 else // single line control
807 {
2829d9e3 808 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 809 {
ac0d36b5 810 *y = 0;
96385642
VZ
811 *x = pos;
812 }
813 else
814 {
815 // index out of bounds
816 return FALSE;
817 }
8bbe427f 818 }
96385642
VZ
819
820 return TRUE;
6de97a3b 821}
c801d85f 822
e3ca08dd 823long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 824{
2830bf19 825 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 826
2830bf19 827 long pos=0;
ac0d36b5 828 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 829
3358d36e 830 pos += x;
2830bf19 831 return pos;
6de97a3b 832}
c801d85f 833
a81258be 834int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 835{
a81258be
RR
836 wxString str = GetLineText (lineNo);
837 return (int) str.Length();
6de97a3b 838}
c801d85f 839
a81258be 840int wxTextCtrl::GetNumberOfLines() const
c801d85f 841{
2830bf19 842 if (m_windowStyle & wxTE_MULTILINE)
a81258be 843 {
fab591c5 844#ifdef __WXGTK20__
41b81aed 845 return gtk_text_buffer_get_line_count( m_buffer );
fab591c5 846#else
2830bf19
RR
847 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
848 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
849
850 if (text)
851 {
852 int currentLine = 0;
853 for (int i = 0; i < len; i++ )
96385642 854 {
2830bf19 855 if (text[i] == '\n')
96385642
VZ
856 currentLine++;
857 }
2830bf19 858 g_free( text );
2829d9e3
VZ
859
860 // currentLine is 0 based, add 1 to get number of lines
861 return currentLine + 1;
2830bf19
RR
862 }
863 else
96385642 864 {
2830bf19 865 return 0;
96385642 866 }
fab591c5 867#endif
a81258be
RR
868 }
869 else
2830bf19 870 {
96385642 871 return 1;
2830bf19 872 }
6de97a3b 873}
c801d85f 874
debe6624 875void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 876{
223d09f6 877 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e 878
74c5a810 879 if ( IsMultiLine() )
291a8f20 880 {
fab591c5 881#ifdef __WXGTK20__
fab591c5 882 GtkTextIter iter;
41b81aed
RR
883 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
884 gtk_text_buffer_place_cursor( m_buffer, &iter );
74c5a810
VZ
885 gtk_text_view_scroll_mark_onscreen
886 (
887 GTK_TEXT_VIEW(m_text),
41b81aed 888 gtk_text_buffer_get_insert( m_buffer )
74c5a810
VZ
889 );
890#else // GTK+ 1.x
291a8f20
RR
891 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
892 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 893
291a8f20 894 /* we fake a set_point by inserting and deleting. as the user
fab591c5 895 isn't supposed to get to know about this non-sense, we
3358d36e
VZ
896 disconnect so that no events are sent to the user program. */
897
291a8f20
RR
898 gint tmp = (gint)pos;
899 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
900 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
901
291a8f20
RR
902 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
903 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 904
fab591c5 905 // bring editable's cursor uptodate. Bug in GTK.
9e691f46 906 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
74c5a810 907#endif // GTK+ 2/1
ac0d36b5 908 }
2830bf19 909 else
291a8f20 910 {
d59051dd 911 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e 912
fab591c5 913 // Bring editable's cursor uptodate. Bug in GTK.
9e691f46 914 SET_EDITABLE_POS(m_text, (guint32)pos);
291a8f20 915 }
6de97a3b 916}
c801d85f 917
03f38c58 918void wxTextCtrl::SetInsertionPointEnd()
c801d85f 919{
223d09f6 920 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 921
d59051dd 922 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
923 {
924#ifdef __WXGTK20__
fab591c5 925 GtkTextIter end;
41b81aed
RR
926 gtk_text_buffer_get_end_iter( m_buffer, &end );
927 gtk_text_buffer_place_cursor( m_buffer, &end );
fab591c5 928#else
d59051dd 929 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
fab591c5
RR
930#endif
931 }
d59051dd 932 else
fab591c5 933 {
d59051dd 934 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
fab591c5 935 }
6de97a3b 936}
c801d85f 937
debe6624 938void wxTextCtrl::SetEditable( bool editable )
c801d85f 939{
223d09f6 940 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 941
2830bf19 942 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
943 {
944#ifdef __WXGTK20__
945 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
946#else
2830bf19 947 gtk_text_set_editable( GTK_TEXT(m_text), editable );
fab591c5
RR
948#endif
949 }
2830bf19 950 else
fab591c5 951 {
2830bf19 952 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
fab591c5 953 }
6de97a3b 954}
c801d85f 955
68df5777
RR
956bool wxTextCtrl::Enable( bool enable )
957{
958 if (!wxWindowBase::Enable(enable))
959 {
960 // nothing to do
961 return FALSE;
962 }
f6bcfd97 963
68df5777
RR
964 if (m_windowStyle & wxTE_MULTILINE)
965 {
fab591c5
RR
966#ifdef __WXGTK20__
967 SetEditable( enable );
968#else
68df5777 969 gtk_text_set_editable( GTK_TEXT(m_text), enable );
5abf8c9d 970 OnParentEnable(enable);
fab591c5 971#endif
68df5777
RR
972 }
973 else
974 {
975 gtk_widget_set_sensitive( m_text, enable );
976 }
977
978 return TRUE;
979}
980
fdca68a6
JS
981// wxGTK-specific: called recursively by Enable,
982// to give widgets an oppprtunity to correct their colours after they
983// have been changed by Enable
984void wxTextCtrl::OnParentEnable( bool enable )
985{
986 // If we have a custom background colour, we use this colour in both
987 // disabled and enabled mode, or we end up with a different colour under the
988 // text.
989 wxColour oldColour = GetBackgroundColour();
990 if (oldColour.Ok())
991 {
992 // Need to set twice or it'll optimize the useful stuff out
993 if (oldColour == * wxWHITE)
994 SetBackgroundColour(*wxBLACK);
995 else
996 SetBackgroundColour(*wxWHITE);
997 SetBackgroundColour(oldColour);
998 }
999}
1000
3a9fa0d6
VZ
1001void wxTextCtrl::MarkDirty()
1002{
1003 m_modified = TRUE;
1004}
1005
0efe5ba7
VZ
1006void wxTextCtrl::DiscardEdits()
1007{
1008 m_modified = FALSE;
1009}
1010
ce2f50e3
VZ
1011// ----------------------------------------------------------------------------
1012// max text length support
1013// ----------------------------------------------------------------------------
1014
1015void wxTextCtrl::IgnoreNextTextUpdate()
1016{
1017 m_ignoreNextUpdate = TRUE;
1018}
1019
1020bool wxTextCtrl::IgnoreTextUpdate()
1021{
1022 if ( m_ignoreNextUpdate )
1023 {
1024 m_ignoreNextUpdate = FALSE;
1025
1026 return TRUE;
1027 }
1028
1029 return FALSE;
1030}
1031
d7eee191
VZ
1032void wxTextCtrl::SetMaxLength(unsigned long len)
1033{
1034 if ( !HasFlag(wxTE_MULTILINE) )
1035 {
1036 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
ce2f50e3
VZ
1037
1038 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1039 // we had tried to enter more text than allowed by max text length and
1040 // the text wasn't really changed
1041 //
1042 // to detect this and generate TEXT_MAXLEN event instead of
1043 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1044 //
1045 // when max len is set to 0 we disconnect our handler as it means that
1046 // we shouldn't check anything any more
1047 if ( len )
1048 {
1049 gtk_signal_connect( GTK_OBJECT(m_text),
1050 "insert_text",
1051 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1052 (gpointer)this);
1053 }
1054 else // no checking
1055 {
1056 gtk_signal_disconnect_by_func
1057 (
1058 GTK_OBJECT(m_text),
1059 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1060 (gpointer)this
1061 );
1062 }
d7eee191
VZ
1063 }
1064}
1065
debe6624 1066void wxTextCtrl::SetSelection( long from, long to )
c801d85f 1067{
223d09f6 1068 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1069
2b5f62a0
VZ
1070 if (from == -1 && to == -1)
1071 {
1072 from = 0;
1073 to = GetValue().Length();
1074 }
1075
fab591c5 1076#ifndef __WXGTK20__
a1f79c1e
VZ
1077 if ( (m_windowStyle & wxTE_MULTILINE) &&
1078 !GTK_TEXT(m_text)->line_start_cache )
1079 {
1080 // tell the programmer that it didn't work
1081 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1082 return;
1083 }
fab591c5 1084#endif
a1f79c1e 1085
fab591c5
RR
1086 if (m_windowStyle & wxTE_MULTILINE)
1087 {
1088#ifdef __WXGTK20__
739e366a 1089 GtkTextIter fromi, toi;
41b81aed
RR
1090 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1091 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
739e366a 1092
41b81aed
RR
1093 gtk_text_buffer_place_cursor( m_buffer, &toi );
1094 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
fab591c5
RR
1095#else
1096 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1097#endif
1098 }
1099 else
1100 {
1101 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1102 }
6de97a3b 1103}
c801d85f 1104
afbe906a 1105void wxTextCtrl::ShowPosition( long pos )
c801d85f 1106{
afbe906a
RR
1107 if (m_windowStyle & wxTE_MULTILINE)
1108 {
71aba833 1109#ifdef __WXGTK20__
71aba833 1110 GtkTextIter iter;
41b81aed 1111 gtk_text_buffer_get_start_iter( m_buffer, &iter );
71aba833 1112 gtk_text_iter_set_offset( &iter, pos );
41b81aed 1113 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
71aba833
VZ
1114 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1115#else // GTK 1.x
afbe906a
RR
1116 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1117 float totalLines = (float) GetNumberOfLines();
1118 long posX;
1119 long posY;
1120 PositionToXY(pos, &posX, &posY);
1121 float posLine = (float) posY;
1122 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1123 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
71aba833 1124#endif // GTK 1.x/2.x
afbe906a 1125 }
6de97a3b 1126}
c801d85f 1127
692c9b86
VZ
1128#ifdef __WXGTK20__
1129
1130wxTextCtrlHitTestResult
1131wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1132{
1133 if ( !IsMultiLine() )
1134 {
1135 // not supported
1136 return wxTE_HT_UNKNOWN;
1137 }
1138
1139 int x, y;
1140 gtk_text_view_window_to_buffer_coords
1141 (
1142 GTK_TEXT_VIEW(m_text),
1143 GTK_TEXT_WINDOW_TEXT,
1144 pt.x, pt.y,
1145 &x, &y
1146 );
1147
1148 GtkTextIter iter;
1149 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1150 if ( pos )
1151 *pos = gtk_text_iter_get_offset(&iter);
1152
1153 return wxTE_HT_ON_TEXT;
1154}
1155
1156#endif // __WXGTK20__
1157
03f38c58 1158long wxTextCtrl::GetInsertionPoint() const
c801d85f 1159{
223d09f6 1160 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1161
fab591c5
RR
1162#ifdef __WXGTK20__
1163 if (m_windowStyle & wxTE_MULTILINE)
1164 {
fab591c5
RR
1165 // There is no direct accessor for the cursor, but
1166 // internally, the cursor is the "mark" called
1167 // "insert" in the text view's btree structure.
a8bf1826 1168
41b81aed 1169 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
fab591c5 1170 GtkTextIter cursor;
41b81aed 1171 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
a8bf1826 1172
fab591c5
RR
1173 return gtk_text_iter_get_offset( &cursor );
1174 }
1175 else
1176#endif
1177 {
9e691f46 1178 return (long) GET_EDITABLE_POS(m_text);
fab591c5 1179 }
6de97a3b 1180}
c801d85f 1181
03f38c58 1182long wxTextCtrl::GetLastPosition() const
c801d85f 1183{
223d09f6 1184 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1185
2830bf19 1186 int pos = 0;
a8bf1826 1187
2830bf19 1188 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1189 {
1190#ifdef __WXGTK20__
fab591c5 1191 GtkTextIter end;
41b81aed 1192 gtk_text_buffer_get_end_iter( m_buffer, &end );
a8bf1826 1193
fab591c5
RR
1194 pos = gtk_text_iter_get_offset( &end );
1195#else
2830bf19 1196 pos = gtk_text_get_length( GTK_TEXT(m_text) );
fab591c5
RR
1197#endif
1198 }
2830bf19 1199 else
fab591c5 1200 {
2830bf19 1201 pos = GTK_ENTRY(m_text)->text_length;
fab591c5 1202 }
805dd538 1203
ac0d36b5 1204 return (long)pos;
6de97a3b 1205}
c801d85f 1206
debe6624 1207void wxTextCtrl::Remove( long from, long to )
c801d85f 1208{
223d09f6 1209 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1210
581ee8a9 1211#ifdef __WXGTK20__
fdd55287 1212 if (m_windowStyle & wxTE_MULTILINE)
581ee8a9 1213 {
581ee8a9 1214 GtkTextIter fromi, toi;
41b81aed
RR
1215 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1216 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
581ee8a9 1217
41b81aed 1218 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
581ee8a9
VZ
1219 }
1220 else // single line
fab591c5 1221#endif
581ee8a9 1222 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 1223}
c801d85f 1224
debe6624 1225void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 1226{
223d09f6 1227 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1228
581ee8a9 1229 Remove( from, to );
bb69661b 1230
2df7be7f
RR
1231 if (!value.IsEmpty())
1232 {
581ee8a9
VZ
1233#ifdef __WXGTK20__
1234 SetInsertionPoint( from );
1235 WriteText( value );
1236#else // GTK 1.x
2df7be7f 1237 gint pos = (gint)from;
05939a81 1238#if wxUSE_UNICODE
2df7be7f
RR
1239 wxWX2MBbuf buf = value.mbc_str();
1240 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 1241#else
2df7be7f 1242 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
581ee8a9
VZ
1243#endif // wxUSE_UNICODE
1244#endif // GTK 1.x/2.x
2df7be7f 1245 }
6de97a3b 1246}
c801d85f 1247
03f38c58 1248void wxTextCtrl::Cut()
c801d85f 1249{
223d09f6 1250 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1251
bbde2e29
VS
1252#ifdef __WXGTK20__
1253 if (m_windowStyle & wxTE_MULTILINE)
1254 g_signal_emit_by_name(m_text, "cut-clipboard");
1255 else
fab591c5 1256#endif
bbde2e29 1257 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1258}
c801d85f 1259
03f38c58 1260void wxTextCtrl::Copy()
c801d85f 1261{
223d09f6 1262 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1263
bbde2e29
VS
1264#ifdef __WXGTK20__
1265 if (m_windowStyle & wxTE_MULTILINE)
1266 g_signal_emit_by_name(m_text, "copy-clipboard");
1267 else
fab591c5 1268#endif
bbde2e29 1269 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1270}
c801d85f 1271
03f38c58 1272void wxTextCtrl::Paste()
c801d85f 1273{
223d09f6 1274 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1275
bbde2e29
VS
1276#ifdef __WXGTK20__
1277 if (m_windowStyle & wxTE_MULTILINE)
1278 g_signal_emit_by_name(m_text, "paste-clipboard");
1279 else
fab591c5 1280#endif
bbde2e29 1281 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1282}
c801d85f 1283
ca8b28f2
JS
1284// Undo/redo
1285void wxTextCtrl::Undo()
1286{
1287 // TODO
223d09f6 1288 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
1289}
1290
1291void wxTextCtrl::Redo()
1292{
1293 // TODO
223d09f6 1294 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
1295}
1296
1297bool wxTextCtrl::CanUndo() const
1298{
1299 // TODO
4855a477 1300 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
ca8b28f2
JS
1301 return FALSE;
1302}
1303
1304bool wxTextCtrl::CanRedo() const
1305{
1306 // TODO
4855a477 1307 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
ca8b28f2
JS
1308 return FALSE;
1309}
1310
1311// If the return values from and to are the same, there is no
1312// selection.
2d4cc5b6 1313void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 1314{
223d09f6 1315 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 1316
5b87f8bf
RD
1317 gint from = -1;
1318 gint to = -1;
1319 bool haveSelection = FALSE;
1320
fb47b99f 1321#ifdef __WXGTK20__
5b87f8bf
RD
1322 if (m_windowStyle & wxTE_MULTILINE)
1323 {
5b87f8bf 1324 GtkTextIter ifrom, ito;
41b81aed 1325 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
5b87f8bf
RD
1326 {
1327 haveSelection = TRUE;
1328 from = gtk_text_iter_get_offset(&ifrom);
1329 to = gtk_text_iter_get_offset(&ito);
1330 }
1331 }
1332 else // not multi-line
1333 {
1334 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1335 &from, &to) )
1336 {
1337 haveSelection = TRUE;
1338 }
1339 }
1340#else // not GTK2
1341 if ( (GTK_EDITABLE(m_text)->has_selection) )
1342 {
1343 haveSelection = TRUE;
1344 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1345 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1346 }
9e691f46 1347#endif
2d4cc5b6 1348
5b87f8bf
RD
1349 if (! haveSelection )
1350 from = to = GetInsertionPoint();
1351
1352 if ( from > to )
1353 {
1354 // exchange them to be compatible with wxMSW
1355 gint tmp = from;
1356 from = to;
1357 to = tmp;
1358 }
bb69661b 1359
2d4cc5b6
VZ
1360 if ( fromOut )
1361 *fromOut = from;
1362 if ( toOut )
1363 *toOut = to;
ca8b28f2
JS
1364}
1365
5b87f8bf 1366
ca8b28f2
JS
1367bool wxTextCtrl::IsEditable() const
1368{
223d09f6 1369 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
05060eeb 1370
9e691f46 1371#ifdef __WXGTK20__
fdd55287
VZ
1372 if (m_windowStyle & wxTE_MULTILINE)
1373 {
1374 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1375 }
1376 else
1377 {
1378 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1379 }
9e691f46 1380#else
05060eeb 1381 return GTK_EDITABLE(m_text)->editable;
9e691f46 1382#endif
ca8b28f2
JS
1383}
1384
0efe5ba7
VZ
1385bool wxTextCtrl::IsModified() const
1386{
1387 return m_modified;
1388}
1389
03f38c58 1390void wxTextCtrl::Clear()
c801d85f 1391{
223d09f6 1392 SetValue( wxT("") );
6de97a3b 1393}
c801d85f 1394
903f689b 1395void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 1396{
223d09f6 1397 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 1398
12a3f227 1399 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
2830bf19
RR
1400 {
1401 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1402 event.SetEventObject(this);
f6bcfd97 1403 event.SetString(GetValue());
2830bf19
RR
1404 if (GetEventHandler()->ProcessEvent(event)) return;
1405 }
903f689b 1406
12a3f227 1407 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
da048e3d 1408 {
2b328fc9
RR
1409 // This will invoke the dialog default action, such
1410 // as the clicking the default button.
a8bf1826 1411
da048e3d 1412 wxWindow *top_frame = m_parent;
8487f887 1413 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 1414 top_frame = top_frame->GetParent();
a8bf1826 1415
2b328fc9 1416 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
da048e3d 1417 {
2b328fc9
RR
1418 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1419
1420 if (window->default_widget)
1421 {
1422 gtk_widget_activate (window->default_widget);
1423 return;
1424 }
13111b2a 1425 }
da048e3d
RR
1426 }
1427
2830bf19 1428 key_event.Skip();
6de97a3b 1429}
c801d85f 1430
03f38c58 1431GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1432{
ae0bdb01 1433 return GTK_WIDGET(m_text);
6de97a3b 1434}
e3e65dac 1435
903f689b
RR
1436bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1437{
ae0bdb01 1438 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1439 {
1440#ifdef __WXGTK20__
1441 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1442#else
ae0bdb01 1443 return (window == GTK_TEXT(m_text)->text_area);
fab591c5
RR
1444#endif
1445 }
ae0bdb01 1446 else
fab591c5 1447 {
ae0bdb01 1448 return (window == GTK_ENTRY(m_text)->text_area);
fab591c5 1449 }
903f689b 1450}
e3e65dac 1451
bb69661b
VZ
1452// the font will change for subsequent text insertiongs
1453bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1454{
223d09f6 1455 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 1456
a66954a6 1457 if ( !wxTextCtrlBase::SetFont(font) )
bb69661b
VZ
1458 {
1459 // font didn't change, nothing to do
1460 return FALSE;
1461 }
1462
1463 if ( m_windowStyle & wxTE_MULTILINE )
1464 {
c04ec496 1465 SetUpdateFont(TRUE);
bb69661b 1466
1ff4714d
VZ
1467 m_defaultStyle.SetFont(font);
1468
01041145 1469 ChangeFontGlobally();
bb69661b
VZ
1470 }
1471
1472 return TRUE;
58614078
RR
1473}
1474
01041145
VZ
1475void wxTextCtrl::ChangeFontGlobally()
1476{
1477 // this method is very inefficient and hence should be called as rarely as
1478 // possible!
c04ec496
VZ
1479 //
1480 // TODO: it can be implemented much more efficiently for GTK2
c04ec496 1481#ifndef __WXGTK20__
22800f32
JS
1482 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1483
01041145 1484 _T("shouldn't be called for single line controls") );
22800f32
JS
1485#else
1486 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1487 _T("shouldn't be called for single line controls") );
1488#endif
01041145
VZ
1489
1490 wxString value = GetValue();
1491 if ( !value.IsEmpty() )
1492 {
c04ec496 1493 SetUpdateFont(FALSE);
572aeb77 1494
01041145
VZ
1495 Clear();
1496 AppendText(value);
01041145
VZ
1497 }
1498}
1499
c04ec496
VZ
1500#ifndef __WXGTK20__
1501
01041145
VZ
1502void wxTextCtrl::UpdateFontIfNeeded()
1503{
1504 if ( m_updateFont )
1505 ChangeFontGlobally();
1506}
1507
c04ec496
VZ
1508#endif // GTK+ 1.x
1509
17665a2b
VZ
1510bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1511{
1512 if ( !wxControl::SetForegroundColour(colour) )
1513 return FALSE;
1514
1515 // update default fg colour too
1516 m_defaultStyle.SetTextColour(colour);
1517
1518 return TRUE;
1519}
1520
f03fc89f 1521bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1522{
223d09f6 1523 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
a81258be 1524
1f477433
VZ
1525 if ( !wxControl::SetBackgroundColour( colour ) )
1526 return FALSE;
3358d36e 1527
c2094564 1528#ifndef __WXGTK20__
f03fc89f
VZ
1529 if (!m_widget->window)
1530 return FALSE;
c2094564 1531#endif
8bbe427f 1532
f03fc89f
VZ
1533 if (!m_backgroundColour.Ok())
1534 return FALSE;
8bbe427f 1535
ae0bdb01
RR
1536 if (m_windowStyle & wxTE_MULTILINE)
1537 {
1fc32b12 1538#ifndef __WXGTK20__
ae0bdb01 1539 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f
VZ
1540 if (!window)
1541 return FALSE;
ae0bdb01
RR
1542 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1543 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1544 gdk_window_clear( window );
fab591c5 1545#endif
ae0bdb01 1546 }
f03fc89f 1547
17665a2b
VZ
1548 // change active background color too
1549 m_defaultStyle.SetBackgroundColour( colour );
1550
f03fc89f 1551 return TRUE;
58614078
RR
1552}
1553
eda40bfc 1554bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
17665a2b 1555{
17665a2b
VZ
1556 if ( m_windowStyle & wxTE_MULTILINE )
1557 {
1558 if ( style.IsDefault() )
1559 {
1560 // nothing to do
1561 return TRUE;
1562 }
cc3da3f8 1563#ifdef __WXGTK20__
41b81aed 1564 gint l = gtk_text_buffer_get_char_count( m_buffer );
17665a2b 1565
cc3da3f8
RR
1566 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1567 _T("invalid range in wxTextCtrl::SetStyle") );
1568
1569 GtkTextIter starti, endi;
41b81aed
RR
1570 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1571 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
cc3da3f8
RR
1572
1573 // use the attributes from style which are set in it and fall back
1574 // first to the default style and then to the text control default
1575 // colours for the others
1576 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1577
41b81aed 1578 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
cc3da3f8
RR
1579
1580 return TRUE;
1581#else
1582 // VERY dirty way to do that - removes the required text and re-adds it
1583 // with styling (FIXME)
5b87f8bf 1584
17665a2b
VZ
1585 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1586
1587 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1588 _T("invalid range in wxTextCtrl::SetStyle") );
1589
1590 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1591 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1592 wxString tmp(text,*wxConvCurrent);
1593 g_free( text );
1594
1595 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1596 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1597
1598#if wxUSE_UNICODE
1599 wxWX2MBbuf buf = tmp.mbc_str();
1600 const char *txt = buf;
1601 size_t txtlen = strlen(buf);
1602#else
1603 const char *txt = tmp;
1604 size_t txtlen = tmp.length();
1605#endif
1606
eda40bfc
VZ
1607 // use the attributes from style which are set in it and fall back
1608 // first to the default style and then to the text control default
1609 // colours for the others
1610 wxGtkTextInsert(m_text,
1611 wxTextAttr::Combine(style, m_defaultStyle, this),
1612 txt,
1613 txtlen);
17665a2b
VZ
1614
1615 /* does not seem to help under GTK+ 1.2 !!!
1616 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1617 SetInsertionPoint( old_pos );
fab591c5 1618#endif
17665a2b
VZ
1619 return TRUE;
1620 }
1621 else // singe line
1622 {
1623 // cannot do this for GTK+'s Entry widget
1624 return FALSE;
1625 }
1626}
1627
f40fdaa3 1628void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
58614078 1629{
f40fdaa3 1630 gtk_widget_modify_style(m_text, style);
68dda785 1631}
f96aa4d9 1632
e702ff0f
JS
1633void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1634{
1635 Cut();
1636}
1637
1638void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1639{
1640 Copy();
1641}
1642
1643void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1644{
1645 Paste();
1646}
1647
1648void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1649{
1650 Undo();
1651}
1652
1653void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1654{
1655 Redo();
1656}
1657
1658void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1659{
1660 event.Enable( CanCut() );
1661}
1662
1663void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1664{
1665 event.Enable( CanCopy() );
1666}
1667
1668void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1669{
1670 event.Enable( CanPaste() );
1671}
1672
1673void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1674{
1675 event.Enable( CanUndo() );
1676}
1677
1678void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1679{
1680 event.Enable( CanRedo() );
1681}
65045edd
RR
1682
1683void wxTextCtrl::OnInternalIdle()
1684{
1685 wxCursor cursor = m_cursor;
1686 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1687
307f16e8 1688 if (cursor.Ok())
65045edd 1689 {
fab591c5 1690#ifndef __WXGTK20__
65045edd 1691 GdkWindow *window = (GdkWindow*) NULL;
13111b2a 1692 if (HasFlag(wxTE_MULTILINE))
65045edd
RR
1693 window = GTK_TEXT(m_text)->text_area;
1694 else
1695 window = GTK_ENTRY(m_text)->text_area;
13111b2a 1696
65045edd
RR
1697 if (window)
1698 gdk_window_set_cursor( window, cursor.GetCursor() );
1699
1700 if (!g_globalCursor.Ok())
1701 cursor = *wxSTANDARD_CURSOR;
1702
1703 window = m_widget->window;
247e5b16 1704 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd 1705 gdk_window_set_cursor( window, cursor.GetCursor() );
fab591c5 1706#endif
65045edd 1707 }
26bf1ce0 1708
d7fa7eaa
RR
1709 if (g_delayedFocus == this)
1710 {
1711 if (GTK_WIDGET_REALIZED(m_widget))
1712 {
1713 gtk_widget_grab_focus( m_widget );
1714 g_delayedFocus = NULL;
1715 }
1716 }
1717
e39af974
JS
1718 if (wxUpdateUIEvent::CanUpdate(this))
1719 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
65045edd 1720}
f68586e5
VZ
1721
1722wxSize wxTextCtrl::DoGetBestSize() const
1723{
1724 // FIXME should be different for multi-line controls...
0279e844 1725 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
1726 wxSize best(80, ret.y);
1727 CacheBestSize(best);
1728 return best;
f68586e5 1729}
0cc7251e 1730
9cd6d737
VZ
1731// ----------------------------------------------------------------------------
1732// freeze/thaw
1733// ----------------------------------------------------------------------------
1734
0cc7251e
VZ
1735void wxTextCtrl::Freeze()
1736{
1737 if ( HasFlag(wxTE_MULTILINE) )
1738 {
41b81aed
RR
1739#ifdef __WXGTK20__
1740 if ( !m_frozenness++ )
1741 {
1742 // freeze textview updates and remove buffer
1743 g_signal_connect( G_OBJECT(m_text), "expose_event",
1744 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
1745 g_signal_connect( G_OBJECT(m_widget), "expose_event",
1746 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
1747 gtk_widget_set_sensitive(m_widget, false);
1748 g_object_ref(m_buffer);
1749 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
0cc7251e 1750 }
41b81aed
RR
1751#else
1752 gtk_text_freeze(GTK_TEXT(m_text));
fab591c5 1753#endif
41b81aed 1754 }
0cc7251e
VZ
1755}
1756
1757void wxTextCtrl::Thaw()
1758{
1759 if ( HasFlag(wxTE_MULTILINE) )
1760 {
41b81aed
RR
1761#ifdef __WXGTK20__
1762 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
1763
1764 if ( !--m_frozenness )
1765 {
1766 // Reattach buffer and thaw textview updates
1767 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1768 g_object_unref(m_buffer);
1769 gtk_widget_set_sensitive(m_widget, true);
1770 g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this);
1771 g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this);
1772 }
1773#else
817ec43a
VZ
1774 GTK_TEXT(m_text)->vadj->value = 0.0;
1775
0cc7251e 1776 gtk_text_thaw(GTK_TEXT(m_text));
fab591c5 1777#endif
41b81aed 1778 }
0cc7251e 1779}
9cd6d737
VZ
1780
1781// ----------------------------------------------------------------------------
1782// scrolling
1783// ----------------------------------------------------------------------------
1784
1785GtkAdjustment *wxTextCtrl::GetVAdj() const
1786{
1ce52aa6
VZ
1787 if ( !IsMultiLine() )
1788 return NULL;
1789
fab591c5 1790#ifdef __WXGTK20__
1ce52aa6 1791 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
fab591c5 1792#else
1ce52aa6 1793 return GTK_TEXT(m_text)->vadj;
fab591c5 1794#endif
9cd6d737
VZ
1795}
1796
1797bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1798{
1799 float value = adj->value + diff;
1800
1801 if ( value < 0 )
1802 value = 0;
1803
1804 float upper = adj->upper - adj->page_size;
1805 if ( value > upper )
1806 value = upper;
1807
1808 // did we noticeably change the scroll position?
1809 if ( fabs(adj->value - value) < 0.2 )
1810 {
1811 // well, this is what Robert does in wxScrollBar, so it must be good...
1812 return FALSE;
1813 }
1814
1815 adj->value = value;
1816
1ce52aa6
VZ
1817#ifdef __WXGTK20__
1818 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
1819#else
9cd6d737 1820 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
fab591c5 1821#endif
1ce52aa6 1822
9cd6d737
VZ
1823 return TRUE;
1824}
1825
1826bool wxTextCtrl::ScrollLines(int lines)
1827{
1828 GtkAdjustment *adj = GetVAdj();
1829 if ( !adj )
1830 return FALSE;
1831
1ce52aa6
VZ
1832#ifdef __WXGTK20__
1833 int diff = (int)ceil(lines*adj->step_increment);
1834#else
9cd6d737 1835 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1ce52aa6 1836 int diff = 10*lines;
fab591c5 1837#endif
1ce52aa6
VZ
1838
1839 return DoScroll(adj, diff);
9cd6d737
VZ
1840}
1841
1842bool wxTextCtrl::ScrollPages(int pages)
1843{
1844 GtkAdjustment *adj = GetVAdj();
1845 if ( !adj )
1846 return FALSE;
1847
817ec43a 1848 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
9cd6d737
VZ
1849}
1850
9d522606
RD
1851
1852// static
1853wxVisualAttributes
1854wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1855{
1856 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1857}