]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
64 bit compilation fix (pointer can't be cast to int); code simplification (don't...
[wxWidgets.git] / src / gtk / 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 45extern wxCursor g_globalCursor;
d7fa7eaa 46extern wxWindowGTK *g_delayedFocus;
b292e2f5 47
eda40bfc
VZ
48// ----------------------------------------------------------------------------
49// helpers
50// ----------------------------------------------------------------------------
51
cc3da3f8 52#ifdef __WXGTK20__
418cf02e
JS
53extern "C" {
54static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
55 GtkTextTag *tag,
56 GtkTextIter *start,
57 GtkTextIter *end,
50aee613 58 char *prefix)
418cf02e
JS
59{
60 gchar *name;
61 g_object_get (tag, "name", &name, NULL);
62
50aee613
MR
63 if (!name || strncmp(name, prefix, strlen(prefix)))
64 // anonymous tag or not starting with prefix - don't remove
418cf02e
JS
65 g_signal_stop_emission_by_name(buffer, "remove_tag");
66
67 g_free(name);
68}
69}
70
71extern "C" {
25497324
KH
72static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
73 const wxTextAttr& attr,
74 GtkTextIter *start,
75 GtkTextIter *end)
76{
77 static gchar buf[1024];
78 GtkTextTag *tag;
79
418cf02e 80 gulong remove_handler_id = g_signal_connect( text_buffer, "remove_tag",
50aee613 81 G_CALLBACK(wxGtkOnRemoveTag), gpointer("WX"));
418cf02e
JS
82 gtk_text_buffer_remove_all_tags(text_buffer, start, end);
83 g_signal_handler_disconnect( text_buffer, remove_handler_id );
84
25497324
KH
85 if (attr.HasFont())
86 {
87 char *font_string;
88 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
89 font_string = pango_font_description_to_string(font_description);
90 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
91 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
92 buf );
93 if (!tag)
94 tag = gtk_text_buffer_create_tag( text_buffer, buf,
95 "font-desc", font_description,
96 NULL );
97 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
98 g_free (font_string);
99 }
100
101 if (attr.HasTextColour())
102 {
103 GdkColor *colFg = attr.GetTextColour().GetColor();
104 g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
105 colFg->red, colFg->green, colFg->blue);
106 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
107 buf );
108 if (!tag)
109 tag = gtk_text_buffer_create_tag( text_buffer, buf,
110 "foreground-gdk", colFg, NULL );
111 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
112 }
113
114 if (attr.HasBackgroundColour())
115 {
116 GdkColor *colBg = attr.GetBackgroundColour().GetColor();
117 g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
118 colBg->red, colBg->green, colBg->blue);
119 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
120 buf );
121 if (!tag)
122 tag = gtk_text_buffer_create_tag( text_buffer, buf,
123 "background-gdk", colBg, NULL );
124 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
125 }
50aee613
MR
126
127 if (attr.HasAlignment())
128 {
129 GtkTextIter para_start, para_end = *end;
130 gtk_text_buffer_get_iter_at_line( text_buffer,
131 &para_start,
132 gtk_text_iter_get_line(start) );
133 gtk_text_iter_forward_line(&para_end);
134
135 remove_handler_id = g_signal_connect( text_buffer, "remove_tag",
136 G_CALLBACK(wxGtkOnRemoveTag),
137 gpointer("WXALIGNMENT"));
138 gtk_text_buffer_remove_all_tags( text_buffer, &para_start, &para_end );
139 g_signal_handler_disconnect( text_buffer, remove_handler_id );
140
141 GtkJustification align;
142 switch (attr.GetAlignment())
143 {
144 default:
145 align = GTK_JUSTIFY_LEFT;
146 break;
147 case wxTEXT_ALIGNMENT_RIGHT:
148 align = GTK_JUSTIFY_RIGHT;
149 break;
150 case wxTEXT_ALIGNMENT_CENTER:
151 align = GTK_JUSTIFY_CENTER;
152 break;
153 // gtk+ doesn't support justify as of gtk+-2.7.4
154 }
155
156 g_snprintf(buf, sizeof(buf), "WXALIGNMENT %d", align);
157 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
158 buf );
159 if (!tag)
160 tag = gtk_text_buffer_create_tag( text_buffer, buf,
161 "justification", align, NULL );
162 gtk_text_buffer_apply_tag( text_buffer, tag, &para_start, &para_end );
163 }
25497324 164}
418cf02e 165}
25497324 166
418cf02e 167extern "C" {
cc3da3f8
RR
168static void wxGtkTextInsert(GtkWidget *text,
169 GtkTextBuffer *text_buffer,
170 const wxTextAttr& attr,
171 wxCharBuffer buffer)
172
173{
25497324
KH
174 gint start_offset;
175 GtkTextIter iter, start;
cc3da3f8 176
a61bbf87
JS
177 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
178 gtk_text_buffer_get_insert (text_buffer) );
25497324
KH
179 start_offset = gtk_text_iter_get_offset (&iter);
180 gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) );
181
182 gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
a61bbf87 183
25497324 184 wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
cc3da3f8 185}
418cf02e 186}
cc3da3f8 187#else
418cf02e 188extern "C" {
eda40bfc
VZ
189static void wxGtkTextInsert(GtkWidget *text,
190 const wxTextAttr& attr,
191 const char *txt,
192 size_t len)
193{
194 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
195 : NULL;
196
197 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
198 : NULL;
199
200 GdkColor *colBg = attr.HasBackgroundColour()
201 ? attr.GetBackgroundColour().GetColor()
202 : NULL;
203
204 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
205}
418cf02e 206}
a8bf1826 207#endif // GTK 1.x
eda40bfc 208
ce2f50e3
VZ
209// ----------------------------------------------------------------------------
210// "insert_text" for GtkEntry
211// ----------------------------------------------------------------------------
212
865bb325 213extern "C" {
ce2f50e3
VZ
214static void
215gtk_insert_text_callback(GtkEditable *editable,
216 const gchar *new_text,
217 gint new_text_length,
218 gint *position,
219 wxTextCtrl *win)
220{
76fcf0f2
RR
221 if (g_isIdle)
222 wxapp_install_idle_handler();
223
ce2f50e3
VZ
224 // we should only be called if we have a max len limit at all
225 GtkEntry *entry = GTK_ENTRY (editable);
226
227 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
228
229 // check that we don't overflow the max length limit
230 //
231 // FIXME: this doesn't work when we paste a string which is going to be
232 // truncated
233 if ( entry->text_length == entry->text_max_length )
234 {
235 // we don't need to run the base class version at all
236 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
237
238 // remember that the next changed signal is to be ignored to avoid
239 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
240 win->IgnoreNextTextUpdate();
241
242 // and generate the correct one ourselves
243 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
244 event.SetEventObject(win);
245 event.SetString(win->GetValue());
246 win->GetEventHandler()->ProcessEvent( event );
247 }
248}
865bb325 249}
ce2f50e3 250
9440c3d0
KH
251#ifdef __WXGTK20__
252// Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
253
865bb325 254extern "C" {
9440c3d0
KH
255static void
256au_apply_tag_callback(GtkTextBuffer *buffer,
257 GtkTextTag *tag,
258 GtkTextIter *start,
259 GtkTextIter *end,
260 gpointer textctrl)
261{
262 if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"))
263 g_signal_stop_emission_by_name(buffer, "apply_tag");
264}
865bb325 265}
9440c3d0
KH
266
267//-----------------------------------------------------------------------------
268// GtkTextCharPredicates for gtk_text_iter_*_find_char
269//-----------------------------------------------------------------------------
270
865bb325 271extern "C" {
9440c3d0
KH
272static gboolean
273pred_whitespace (gunichar ch, gpointer user_data)
274{
275 return g_unichar_isspace(ch);
276}
865bb325 277}
9440c3d0 278
865bb325 279extern "C" {
9440c3d0
KH
280static gboolean
281pred_non_whitespace (gunichar ch, gpointer user_data)
282{
283 return !g_unichar_isspace(ch);
284}
865bb325 285}
9440c3d0 286
865bb325 287extern "C" {
9440c3d0
KH
288static gboolean
289pred_nonpunct (gunichar ch, gpointer user_data)
290{
291 return !g_unichar_ispunct(ch);
292}
865bb325 293}
9440c3d0 294
865bb325 295extern "C" {
9440c3d0
KH
296static gboolean
297pred_nonpunct_or_slash (gunichar ch, gpointer user_data)
298{
299 return !g_unichar_ispunct(ch) || ch == '/';
300}
865bb325 301}
9440c3d0
KH
302
303//-----------------------------------------------------------------------------
304// Check for links between s and e and correct tags as necessary
305//-----------------------------------------------------------------------------
306
307// This function should be made match better while being efficient at one point.
308// Most probably with a row of regular expressions.
865bb325 309extern "C" {
9440c3d0
KH
310static void
311au_check_word( GtkTextIter *s, GtkTextIter *e )
312{
313 static const char *URIPrefixes[] =
314 {
315 "http://",
316 "ftp://",
317 "www.",
318 "ftp.",
319 "mailto://",
320 "https://",
321 "file://",
322 "nntp://",
323 "news://",
324 "telnet://",
325 "mms://",
326 "gopher://",
327 "prospero://",
328 "wais://",
329 };
330
331 GtkTextIter start = *s, end = *e;
332 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
418cf02e 333
9440c3d0
KH
334 // Get our special link tag
335 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
336
337 // Get rid of punctuation from beginning and end.
338 // Might want to move this to au_check_range if an improved link checking doesn't
339 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
340 if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) )
341 gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e );
342
343 gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start );
344 gtk_text_iter_forward_char(&end);
345
346 gchar* text = gtk_text_iter_get_text( &start, &end );
347 size_t len = strlen(text), prefix_len;
348 size_t n;
349
350 for( n = 0; n < WXSIZEOF(URIPrefixes); ++n )
351 {
352 prefix_len = strlen(URIPrefixes[n]);
353 if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len))
354 break;
355 }
356
357 if(n < WXSIZEOF(URIPrefixes))
358 {
359 gulong signal_id = g_signal_handler_find(buffer,
360 (GSignalMatchType) (G_SIGNAL_MATCH_FUNC),
361 0, 0, NULL,
362 (gpointer)au_apply_tag_callback, NULL);
363
364 g_signal_handler_block(buffer, signal_id);
365 gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
366 g_signal_handler_unblock(buffer, signal_id);
367 }
368}
865bb325 369}
9440c3d0 370
865bb325 371extern "C" {
9440c3d0
KH
372static void
373au_check_range(GtkTextIter *s,
374 GtkTextIter *range_end)
375{
376 GtkTextIter range_start = *s;
377 GtkTextIter word_end;
378 GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s);
379 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl");
380
381 gtk_text_buffer_remove_tag(buffer, tag, s, range_end);
382
383 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start)))
384 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
385
386 while(!gtk_text_iter_equal(&range_start, range_end))
387 {
388 word_end = range_start;
389 gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end);
390
391 // Now we should have a word delimited by range_start and word_end, correct link tags
392 au_check_word(&range_start, &word_end);
393
394 range_start = word_end;
395 gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end);
396 }
397}
865bb325 398}
9440c3d0
KH
399
400//-----------------------------------------------------------------------------
401// "insert-text" for GtkTextBuffer
402//-----------------------------------------------------------------------------
403
865bb325 404extern "C" {
9440c3d0
KH
405static void
406au_insert_text_callback(GtkTextBuffer *buffer,
407 GtkTextIter *end,
408 gchar *text,
409 gint len,
410 wxTextCtrl *win)
411{
412 if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
413 return;
414
415 GtkTextIter start = *end;
416 gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len));
417
418 GtkTextIter line_start = start;
419 GtkTextIter line_end = *end;
420 GtkTextIter words_start = start;
421 GtkTextIter words_end = *end;
422
423 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start));
424 gtk_text_iter_forward_to_line_end(&line_end);
425 gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start);
426 gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end);
427
428 au_check_range(&words_start, &words_end);
429}
865bb325 430}
9440c3d0
KH
431
432//-----------------------------------------------------------------------------
433// "delete-range" for GtkTextBuffer
434//-----------------------------------------------------------------------------
435
865bb325 436extern "C" {
9440c3d0
KH
437static void
438au_delete_range_callback(GtkTextBuffer *buffer,
439 GtkTextIter *start,
440 GtkTextIter *end,
441 wxTextCtrl *win)
442{
443 if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) )
444 return;
445
446 GtkTextIter line_start = *start, line_end = *end;
447
448 gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start));
449 gtk_text_iter_forward_to_line_end(&line_end);
450 gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start);
451 gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end);
452
453 au_check_range(start, end);
454}
865bb325 455}
9440c3d0
KH
456
457
458#endif
459
c801d85f 460//-----------------------------------------------------------------------------
2f2aa628 461// "changed"
c801d85f
KB
462//-----------------------------------------------------------------------------
463
865bb325 464extern "C" {
805dd538 465static void
ba3f6b44 466gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
484e45bf 467{
ce2f50e3
VZ
468 if ( win->IgnoreTextUpdate() )
469 return;
470
a2053b27 471 if (!win->m_hasVMT) return;
805dd538 472
bb69661b 473 if (g_isIdle)
3c679789 474 wxapp_install_idle_handler();
a8bf1826 475
034be888 476 win->SetModified();
c04ec496 477#ifndef __WXGTK20__
01041145 478 win->UpdateFontIfNeeded();
c04ec496 479#endif // !__WXGTK20__
a8bf1826 480
f03fc89f 481 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
2830bf19
RR
482 event.SetEventObject( win );
483 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 484}
865bb325 485}
112892b9 486
41b81aed
RR
487//-----------------------------------------------------------------------------
488// "expose_event" from scrolled window and textview
489//-----------------------------------------------------------------------------
490
491#ifdef __WXGTK20__
865bb325 492extern "C" {
41b81aed
RR
493static gboolean
494gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
495{
496 return TRUE;
497}
865bb325 498}
41b81aed
RR
499#endif
500
2830bf19 501//-----------------------------------------------------------------------------
034be888 502// "changed" from vertical scrollbar
2830bf19
RR
503//-----------------------------------------------------------------------------
504
fab591c5 505#ifndef __WXGTK20__
865bb325 506extern "C" {
805dd538 507static void
034be888 508gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
2830bf19 509{
3c679789 510 if (!win->m_hasVMT) return;
bb69661b
VZ
511
512 if (g_isIdle)
3c679789 513 wxapp_install_idle_handler();
acfd422a 514
2830bf19
RR
515 win->CalculateScrollbar();
516}
865bb325 517}
fab591c5 518#endif
2830bf19 519
1c35b54e
VZ
520// ----------------------------------------------------------------------------
521// redraw callback for multiline text
522// ----------------------------------------------------------------------------
523
524#ifndef __WXGTK20__
525
526// redrawing a GtkText from inside a wxYield() call results in crashes (the
527// text sample shows it in its "Add lines" command which shows wxProgressDialog
528// which implicitly calls wxYield()) so we override GtkText::draw() and simply
529// don't do anything if we're inside wxYield()
530
531extern bool wxIsInsideYield;
532
2e08b1a3
VZ
533extern "C" {
534 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
535}
1c35b54e
VZ
536
537static GtkDrawCallback gs_gtk_text_draw = NULL;
538
865bb325
VZ
539extern "C" {
540static void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
1c35b54e
VZ
541{
542 if ( !wxIsInsideYield )
543 {
544 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
545 _T("infinite recursion in wxgtk_text_draw aborted") );
546
547 gs_gtk_text_draw(widget, rect);
548 }
549}
865bb325 550}
1c35b54e
VZ
551
552#endif // __WXGTK20__
553
2f2aa628
RR
554//-----------------------------------------------------------------------------
555// wxTextCtrl
556//-----------------------------------------------------------------------------
557
558IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
559
c801d85f 560BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 561 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
562
563 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
564 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
565 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
566 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
567 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
568
569 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
570 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
571 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
572 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
573 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
9440c3d0
KH
574
575#ifdef __WXGTK20__
576 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
577 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
578 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent)
579 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent)
580 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent)
581 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent)
582 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent)
583 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent)
584 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent)
585#endif
c801d85f
KB
586END_EVENT_TABLE()
587
01041145 588void wxTextCtrl::Init()
f5abe911 589{
ce2f50e3 590 m_ignoreNextUpdate =
7d8268a1
WS
591 m_modified = false;
592 SetUpdateFont(false);
3ca6a5f0
BP
593 m_text =
594 m_vScrollbar = (GtkWidget *)NULL;
41b81aed
RR
595#ifdef __WXGTK20__
596 m_frozenness = 0;
9440c3d0
KH
597 m_gdkHandCursor = NULL;
598 m_gdkXTermCursor = NULL;
599#endif
600}
601
602wxTextCtrl::~wxTextCtrl()
603{
604#ifdef __WXGTK20__
605 if(m_gdkHandCursor)
606 gdk_cursor_unref(m_gdkHandCursor);
607 if(m_gdkXTermCursor)
608 gdk_cursor_unref(m_gdkXTermCursor);
7d8268a1 609#endif
f5abe911 610}
13289f04 611
13111b2a
VZ
612wxTextCtrl::wxTextCtrl( wxWindow *parent,
613 wxWindowID id,
614 const wxString &value,
615 const wxPoint &pos,
616 const wxSize &size,
617 long style,
618 const wxValidator& validator,
619 const wxString &name )
f5abe911 620{
01041145
VZ
621 Init();
622
f5abe911
RR
623 Create( parent, id, value, pos, size, style, validator, name );
624}
c801d85f 625
13111b2a
VZ
626bool wxTextCtrl::Create( wxWindow *parent,
627 wxWindowID id,
628 const wxString &value,
629 const wxPoint &pos,
630 const wxSize &size,
631 long style,
632 const wxValidator& validator,
633 const wxString &name )
c801d85f 634{
7d8268a1
WS
635 m_needParent = true;
636 m_acceptsFocus = true;
484e45bf 637
4dcaf11a
RR
638 if (!PreCreation( parent, pos, size ) ||
639 !CreateBase( parent, id, pos, size, style, validator, name ))
640 {
223d09f6 641 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
7d8268a1 642 return false;
4dcaf11a 643 }
6de97a3b 644
805dd538 645
7d8268a1 646 m_vScrollbarVisible = false;
13289f04 647
2830bf19 648 bool multi_line = (style & wxTE_MULTILINE) != 0;
a8bf1826 649
ab46dc18 650 if (multi_line)
2830bf19 651 {
fab591c5
RR
652#ifdef __WXGTK20__
653 // Create view
654 m_text = gtk_text_view_new();
a8bf1826 655
41b81aed 656 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
a8bf1826 657
fab591c5
RR
658 // create scrolled window
659 m_widget = gtk_scrolled_window_new( NULL, NULL );
660 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
661 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
a8bf1826 662
fab591c5
RR
663 // Insert view into scrolled window
664 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
a8bf1826 665
c2110823 666 // translate wx wrapping style to GTK+
9ddb3948 667 GtkWrapMode wrap;
c2110823 668 if ( HasFlag( wxTE_DONTWRAP ) )
9ddb3948 669 wrap = GTK_WRAP_NONE;
c4590236 670 else if ( HasFlag( wxTE_CHARWRAP ) )
9ddb3948 671 wrap = GTK_WRAP_CHAR;
c4590236 672 else if ( HasFlag( wxTE_WORDWRAP ) )
9ddb3948 673 wrap = GTK_WRAP_WORD;
c4590236
VZ
674 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
675 {
676 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
677#ifdef __WXGTK24__
c79146df
VZ
678 if ( !gtk_check_version(2,4,0) )
679 {
680 wrap = GTK_WRAP_WORD_CHAR;
681 }
682 else
c4590236 683#endif
34b8f3ef 684 wrap = GTK_WRAP_WORD;
c4590236 685 }
9ddb3948
VZ
686
687 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
805dd538 688
fab591c5
RR
689 if (!HasFlag(wxNO_BORDER))
690 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
7d8268a1 691
e327fddf
KH
692 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
693
055e633d 694 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
c2110823 695#else // GTK+ 1
fab591c5 696 // create our control ...
2830bf19
RR
697 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
698
fab591c5 699 // ... and put into the upper left hand corner of the table
7d8268a1 700 bool bHasHScrollbar = false;
2830bf19 701 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
5664fc32 702 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19 703 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 704 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
705 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
706 0, 0);
bb69661b 707
fab591c5 708 // always wrap words
5c387335 709 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
bb69661b 710
fab591c5 711 // finally, put the vertical scrollbar in the upper right corner
ab46dc18
RR
712 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
713 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
ab46dc18
RR
714 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
715 GTK_FILL,
716 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
717 0, 0);
c2110823 718#endif // GTK+ 2/1
2830bf19
RR
719 }
720 else
721 {
fab591c5 722 // a single-line text control: no need for scrollbars
2830bf19 723 m_widget =
1c35b54e 724 m_text = gtk_entry_new();
44c5573d 725
7d8268a1 726#ifdef __WXGTK20__
02a6e358
RR
727 if (style & wxNO_BORDER)
728 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
44c5573d 729#endif
2830bf19 730 }
484e45bf 731
db434467 732 m_parent->DoAddChild( this );
eda40bfc 733
76fcf0f2 734 m_focusWidget = m_text;
db434467 735
abdeb9e7 736 PostCreation(size);
484e45bf 737
2830bf19 738 if (multi_line)
2830bf19 739 gtk_widget_show(m_text);
13289f04 740
fab591c5 741#ifndef __WXGTK20__
ab46dc18
RR
742 if (multi_line)
743 {
744 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
745 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
1c35b54e 746
1c35b54e
VZ
747 // only initialize gs_gtk_text_draw once, starting from the next the
748 // klass::draw will already be wxgtk_text_draw
749 if ( !gs_gtk_text_draw )
750 {
751 GtkDrawCallback&
752 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
753
754 gs_gtk_text_draw = draw;
755
756 draw = wxgtk_text_draw;
757 }
ab46dc18 758 }
fab591c5 759#endif // GTK+ 1.x
3358d36e 760
7d8268a1 761 if (!value.empty())
2830bf19 762 {
fab591c5
RR
763#ifdef __WXGTK20__
764 SetValue( value );
765#else
3358d36e 766
9e691f46 767#if !GTK_CHECK_VERSION(1, 2, 0)
3358d36e
VZ
768 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
769 // gtk_editable_insert_text()
770 gtk_widget_realize(m_text);
771#endif // GTK 1.0
772
fab591c5 773 gint tmp = 0;
05939a81 774#if wxUSE_UNICODE
3358d36e 775 wxWX2MBbuf val = value.mbc_str();
05939a81 776 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
fab591c5 777#else
2830bf19 778 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
fab591c5 779#endif
3358d36e 780
291a8f20
RR
781 if (multi_line)
782 {
fab591c5 783 // Bring editable's cursor uptodate. Bug in GTK.
9e691f46 784 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
3358d36e 785 }
a8bf1826 786
fab591c5 787#endif
2830bf19 788 }
484e45bf 789
2830bf19
RR
790 if (style & wxTE_PASSWORD)
791 {
792 if (!multi_line)
793 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
794 }
8bbe427f 795
2830bf19
RR
796 if (style & wxTE_READONLY)
797 {
798 if (!multi_line)
799 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
fab591c5
RR
800#ifdef __WXGTK20__
801 else
98a8daf4
VS
802 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
803#else
804 }
805 else
806 {
807 if (multi_line)
808 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
809#endif
810 }
811
c663fbea
VS
812#ifdef __WXGTK20__
813 if (multi_line)
814 {
815 if (style & wxTE_RIGHT)
816 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
817 else if (style & wxTE_CENTRE)
818 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
819 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
820 }
c663fbea
VS
821 else
822 {
77f70672
RR
823#ifdef __WXGTK24__
824 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
825 if (!gtk_check_version(2,4,0))
826 {
827 if (style & wxTE_RIGHT)
828 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
829 else if (style & wxTE_CENTRE)
830 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
831 }
832#endif
c663fbea 833 }
c663fbea 834#endif // __WXGTK20__
7d8268a1 835
fab591c5
RR
836 // We want to be notified about text changes.
837#ifdef __WXGTK20__
838 if (multi_line)
839 {
41b81aed 840 g_signal_connect( G_OBJECT(m_buffer), "changed",
fab591c5 841 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
9440c3d0
KH
842
843 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
844 if (style & wxTE_AUTO_URL)
845 {
846 GtkTextIter start, end;
847 m_gdkHandCursor = gdk_cursor_new(GDK_HAND2);
848 m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM);
849
850 // We create our wxUrl tag here for slight efficiency gain - we
851 // don't have to check for the tag existance in callbacks,
852 // hereby it's guaranteed to exist.
853 gtk_text_buffer_create_tag(m_buffer, "wxUrl",
854 "foreground", "blue",
855 "underline", PANGO_UNDERLINE_SINGLE,
856 NULL);
857
858 // Check for URLs after each text change
859 g_signal_connect_after( G_OBJECT(m_buffer), "insert_text",
860 GTK_SIGNAL_FUNC(au_insert_text_callback), (gpointer)this);
861 g_signal_connect_after( G_OBJECT(m_buffer), "delete_range",
862 GTK_SIGNAL_FUNC(au_delete_range_callback), (gpointer)this);
863
864 // Block all wxUrl tag applying unless we do it ourselves, in which case we
865 // block this callback temporarily. This takes care of gtk+ internal
866 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
867 // which is undesired because only a part of the URL might be copied.
868 // The insert-text signal emitted inside it will take care of newly formed
869 // or wholly copied URLs.
870 g_signal_connect( G_OBJECT(m_buffer), "apply_tag",
871 GTK_SIGNAL_FUNC(au_apply_tag_callback), NULL);
872
873 // Check for URLs in the initial string passed to Create
874 gtk_text_buffer_get_start_iter(m_buffer, &start);
875 gtk_text_buffer_get_end_iter(m_buffer, &end);
876 au_check_range(&start, &end);
877 }
fab591c5
RR
878 }
879 else
880#endif
7d8268a1 881
fab591c5 882 {
055e633d
RR
883 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
884 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
fab591c5 885 }
ce16e5d7 886
65045edd 887 m_cursor = wxCursor( wxCURSOR_IBEAM );
13111b2a 888
9d522606 889 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
17665a2b
VZ
890 SetDefaultStyle( attrDef );
891
7d8268a1 892 return true;
2830bf19 893}
484e45bf 894
9d522606 895
2830bf19
RR
896void wxTextCtrl::CalculateScrollbar()
897{
fab591c5 898#ifndef __WXGTK20__
2830bf19 899 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
f96aa4d9 900
2830bf19 901 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
805dd538 902
2830bf19
RR
903 if (adj->upper - adj->page_size < 0.8)
904 {
905 if (m_vScrollbarVisible)
906 {
034be888 907 gtk_widget_hide( m_vScrollbar );
7d8268a1 908 m_vScrollbarVisible = false;
2830bf19
RR
909 }
910 }
911 else
912 {
913 if (!m_vScrollbarVisible)
914 {
034be888 915 gtk_widget_show( m_vScrollbar );
7d8268a1 916 m_vScrollbarVisible = true;
2830bf19
RR
917 }
918 }
fab591c5 919#endif
6de97a3b 920}
c801d85f 921
03f38c58 922wxString wxTextCtrl::GetValue() const
c801d85f 923{
902725ee 924 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
8bbe427f 925
2830bf19
RR
926 wxString tmp;
927 if (m_windowStyle & wxTE_MULTILINE)
928 {
fab591c5 929#ifdef __WXGTK20__
fab591c5 930 GtkTextIter start;
41b81aed 931 gtk_text_buffer_get_start_iter( m_buffer, &start );
fab591c5 932 GtkTextIter end;
41b81aed
RR
933 gtk_text_buffer_get_end_iter( m_buffer, &end );
934 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
5b87f8bf 935
fab591c5
RR
936#if wxUSE_UNICODE
937 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
938#else
939 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
940#endif
39ccbf9a
VZ
941 if ( buffer )
942 tmp = buffer;
a8bf1826 943
fab591c5
RR
944 g_free( text );
945#else
2830bf19
RR
946 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
947 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
fab591c5 948 tmp = text;
2830bf19 949 g_free( text );
fab591c5 950#endif
2830bf19
RR
951 }
952 else
953 {
fab591c5 954 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
2830bf19 955 }
a8bf1826 956
2830bf19 957 return tmp;
6de97a3b 958}
c801d85f
KB
959
960void wxTextCtrl::SetValue( const wxString &value )
961{
223d09f6 962 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 963
2830bf19
RR
964 if (m_windowStyle & wxTE_MULTILINE)
965 {
fab591c5
RR
966#ifdef __WXGTK20__
967
968#if wxUSE_UNICODE
969 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
970#else
971 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
a8bf1826 972#endif
b6ae8db8 973 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
ad68ed32
RR
974 IgnoreNextTextUpdate();
975
430ae62e
JS
976 if ( !buffer )
977 {
978 // what else can we do? at least don't crash...
979 return;
980 }
418cf02e 981
41b81aed 982 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
a8bf1826 983
fab591c5 984#else
2830bf19
RR
985 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
986 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
987 len = 0;
01041145 988 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
05939a81 989#endif
2830bf19
RR
990 }
991 else
992 {
fab591c5 993 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
2830bf19 994 }
f6bcfd97
BP
995
996 // GRG, Jun/2000: Changed this after a lot of discussion in
77ffb593 997 // the lists. wxWidgets 2.2 will have a set of flags to
f6bcfd97
BP
998 // customize this behaviour.
999 SetInsertionPoint(0);
a8bf1826 1000
7d8268a1 1001 m_modified = false;
6de97a3b 1002}
c801d85f
KB
1003
1004void wxTextCtrl::WriteText( const wxString &text )
1005{
223d09f6 1006 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1007
17665a2b
VZ
1008 if ( text.empty() )
1009 return;
484e45bf 1010
c04ec496
VZ
1011 // gtk_text_changed_callback() will set m_modified to true but m_modified
1012 // shouldn't be changed by the program writing to the text control itself,
1013 // so save the old value and restore when we're done
1014 bool oldModified = m_modified;
1015
17665a2b 1016 if ( m_windowStyle & wxTE_MULTILINE )
2830bf19 1017 {
fab591c5
RR
1018#ifdef __WXGTK20__
1019
1020#if wxUSE_UNICODE
1021 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
1022#else
1023 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
a8bf1826 1024#endif
8e033d81
VZ
1025 if ( !buffer )
1026 {
1027 // what else can we do? at least don't crash...
1028 return;
1029 }
1030
e136b747 1031 // TODO: Call whatever is needed to delete the selection.
41b81aed 1032 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
a8bf1826 1033
71aba833
VZ
1034 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
1035 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
1036 if ( adj->value == adj->upper - adj->page_size )
1037 {
1038 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
41b81aed 1039 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
71aba833 1040 }
a8bf1826 1041#else // GTK 1.x
ba3f6b44 1042 // After cursor movements, gtk_text_get_point() is wrong by one.
9e691f46 1043 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
eda40bfc 1044
41b2e0e6
VZ
1045 // always use m_defaultStyle, even if it is empty as otherwise
1046 // resetting the style and appending some more text wouldn't work: if
1047 // we don't specify the style explicitly, the old style would be used
2b5f62a0 1048 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
fab591c5 1049 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
eda40bfc 1050
07e9d4f0
VZ
1051 // we called wxGtkTextInsert with correct font, no need to do anything
1052 // in UpdateFontIfNeeded() any longer
1053 if ( !text.empty() )
1054 {
7d8268a1 1055 SetUpdateFont(false);
07e9d4f0
VZ
1056 }
1057
ba3f6b44 1058 // Bring editable's cursor back uptodate.
9e691f46 1059 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
a8bf1826 1060#endif // GTK 1.x/2.0
2830bf19 1061 }
17665a2b 1062 else // single line
2830bf19 1063 {
2b5f62a0
VZ
1064 // First remove the selection if there is one
1065 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
1066
ba3f6b44 1067 // This moves the cursor pos to behind the inserted text.
9e691f46 1068 gint len = GET_EDITABLE_POS(m_text);
a8bf1826 1069
fab591c5
RR
1070#ifdef __WXGTK20__
1071
1072#if wxUSE_UNICODE
1073 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
1074#else
1075 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
a8bf1826 1076#endif
82b76851
JS
1077 if ( !buffer )
1078 {
1079 // what else can we do? at least don't crash...
1080 return;
1081 }
1082
fab591c5 1083 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
a8bf1826 1084
fab591c5
RR
1085#else
1086 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
1087#endif
3358d36e 1088
ba3f6b44 1089 // Bring entry's cursor uptodate.
9e691f46 1090 gtk_entry_set_position( GTK_ENTRY(m_text), len );
2830bf19 1091 }
eda40bfc 1092
c04ec496 1093 m_modified = oldModified;
6de97a3b 1094}
c801d85f 1095
a6e21573
HH
1096void wxTextCtrl::AppendText( const wxString &text )
1097{
ba3f6b44
RR
1098 SetInsertionPointEnd();
1099 WriteText( text );
1100}
2df7be7f 1101
ba3f6b44
RR
1102wxString wxTextCtrl::GetLineText( long lineNo ) const
1103{
a6e21573
HH
1104 if (m_windowStyle & wxTE_MULTILINE)
1105 {
fab591c5 1106#ifndef __WXGTK20__
ba3f6b44
RR
1107 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1108 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
bb69661b 1109
ba3f6b44
RR
1110 if (text)
1111 {
902725ee 1112 wxString buf;
ba3f6b44
RR
1113 long i;
1114 int currentLine = 0;
1115 for (i = 0; currentLine != lineNo && text[i]; i++ )
1116 if (text[i] == '\n')
1117 currentLine++;
1118 // Now get the text
1119 int j;
1120 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
1121 buf += text[i];
17665a2b 1122
ba3f6b44
RR
1123 g_free( text );
1124 return buf;
bb69661b 1125 }
ba3f6b44 1126 else
bb69661b 1127 {
ba3f6b44 1128 return wxEmptyString;
bb69661b 1129 }
905f2110 1130#else
905f2110 1131 GtkTextIter line;
41b81aed 1132 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
8c6785f0
MR
1133 GtkTextIter end = line;
1134 gtk_text_iter_forward_to_line_end(&end);
41b81aed 1135 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
58192970 1136 wxString result(wxGTK_CONV_BACK(text));
905f2110 1137 g_free(text);
8c6785f0 1138 return result;
905f2110 1139#endif
a6e21573 1140 }
ba3f6b44 1141 else
a81258be 1142 {
ba3f6b44
RR
1143 if (lineNo == 0) return GetValue();
1144 return wxEmptyString;
a81258be 1145 }
6de97a3b 1146}
c801d85f 1147
a81258be
RR
1148void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
1149{
ac0d36b5
HH
1150 /* If you implement this, don't forget to update the documentation!
1151 * (file docs/latex/wx/text.tex) */
223d09f6 1152 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 1153}
112892b9 1154
0efe5ba7 1155bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 1156{
96385642 1157 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 1158 {
f29a481a
MR
1159#ifdef __WXGTK20__
1160 GtkTextIter iter;
1161 gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos);
1162 if (gtk_text_iter_is_end(&iter))
1163 return false;
1164
1165 *y = gtk_text_iter_get_line(&iter);
1166 *x = gtk_text_iter_get_line_offset(&iter);
1167#else
96385642
VZ
1168 wxString text = GetValue();
1169
6085b116 1170 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 1171 if( (unsigned long)pos > text.Len() )
7d8268a1 1172 return false;
96385642 1173
ac0d36b5
HH
1174 *x=0; // First Col
1175 *y=0; // First Line
2829d9e3 1176
05939a81
OK
1177 const wxChar* stop = text.c_str() + pos;
1178 for ( const wxChar *p = text.c_str(); p < stop; p++ )
96385642 1179 {
223d09f6 1180 if (*p == wxT('\n'))
96385642
VZ
1181 {
1182 (*y)++;
ac0d36b5 1183 *x=0;
96385642
VZ
1184 }
1185 else
1186 (*x)++;
1187 }
f29a481a 1188#endif
805dd538 1189 }
96385642
VZ
1190 else // single line control
1191 {
2829d9e3 1192 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 1193 {
ac0d36b5 1194 *y = 0;
96385642
VZ
1195 *x = pos;
1196 }
1197 else
1198 {
1199 // index out of bounds
7d8268a1 1200 return false;
96385642 1201 }
8bbe427f 1202 }
96385642 1203
7d8268a1 1204 return true;
6de97a3b 1205}
c801d85f 1206
e3ca08dd 1207long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 1208{
2830bf19 1209 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 1210
f29a481a
MR
1211#ifdef __WXGTK20__
1212 GtkTextIter iter;
21d23b88
MR
1213 if (y >= gtk_text_buffer_get_line_count (m_buffer))
1214 return -1;
1215
1216 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
1217 if (x >= gtk_text_iter_get_chars_in_line (&iter))
1218 return -1;
1219
1220 return gtk_text_iter_get_offset(&iter) + x;
f29a481a 1221#else
2830bf19 1222 long pos=0;
ac0d36b5 1223 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 1224
3358d36e 1225 pos += x;
2830bf19 1226 return pos;
f29a481a 1227#endif
6de97a3b 1228}
c801d85f 1229
a81258be 1230int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 1231{
f29a481a
MR
1232#ifdef __WXGTK20__
1233 if (m_windowStyle & wxTE_MULTILINE)
1234 {
1235 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
1236 if (lineNo > last_line)
1237 return -1;
1238
1239 GtkTextIter iter;
1240 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
1241 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1242 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
1243 }
1244 else
1245#endif
1246 {
1247 wxString str = GetLineText (lineNo);
1248 return (int) str.Length();
1249 }
6de97a3b 1250}
c801d85f 1251
a81258be 1252int wxTextCtrl::GetNumberOfLines() const
c801d85f 1253{
2830bf19 1254 if (m_windowStyle & wxTE_MULTILINE)
a81258be 1255 {
fab591c5 1256#ifdef __WXGTK20__
41b81aed 1257 return gtk_text_buffer_get_line_count( m_buffer );
fab591c5 1258#else
2830bf19
RR
1259 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1260 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
1261
1262 if (text)
1263 {
1264 int currentLine = 0;
1265 for (int i = 0; i < len; i++ )
96385642 1266 {
2830bf19 1267 if (text[i] == '\n')
96385642
VZ
1268 currentLine++;
1269 }
2830bf19 1270 g_free( text );
2829d9e3
VZ
1271
1272 // currentLine is 0 based, add 1 to get number of lines
1273 return currentLine + 1;
2830bf19
RR
1274 }
1275 else
96385642 1276 {
2830bf19 1277 return 0;
96385642 1278 }
fab591c5 1279#endif
a81258be
RR
1280 }
1281 else
2830bf19 1282 {
96385642 1283 return 1;
2830bf19 1284 }
6de97a3b 1285}
c801d85f 1286
debe6624 1287void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 1288{
223d09f6 1289 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e 1290
74c5a810 1291 if ( IsMultiLine() )
291a8f20 1292 {
fab591c5 1293#ifdef __WXGTK20__
fab591c5 1294 GtkTextIter iter;
41b81aed
RR
1295 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1296 gtk_text_buffer_place_cursor( m_buffer, &iter );
74c5a810
VZ
1297 gtk_text_view_scroll_mark_onscreen
1298 (
1299 GTK_TEXT_VIEW(m_text),
41b81aed 1300 gtk_text_buffer_get_insert( m_buffer )
74c5a810
VZ
1301 );
1302#else // GTK+ 1.x
291a8f20
RR
1303 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
1304 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 1305
291a8f20 1306 /* we fake a set_point by inserting and deleting. as the user
fab591c5 1307 isn't supposed to get to know about this non-sense, we
3358d36e
VZ
1308 disconnect so that no events are sent to the user program. */
1309
291a8f20
RR
1310 gint tmp = (gint)pos;
1311 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
1312 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
1313
291a8f20
RR
1314 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
1315 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 1316
fab591c5 1317 // bring editable's cursor uptodate. Bug in GTK.
9e691f46 1318 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
74c5a810 1319#endif // GTK+ 2/1
ac0d36b5 1320 }
2830bf19 1321 else
291a8f20 1322 {
d59051dd 1323 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e 1324
fab591c5 1325 // Bring editable's cursor uptodate. Bug in GTK.
9e691f46 1326 SET_EDITABLE_POS(m_text, (guint32)pos);
291a8f20 1327 }
6de97a3b 1328}
c801d85f 1329
03f38c58 1330void wxTextCtrl::SetInsertionPointEnd()
c801d85f 1331{
223d09f6 1332 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1333
d59051dd 1334 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1335 {
1336#ifdef __WXGTK20__
fab591c5 1337 GtkTextIter end;
41b81aed
RR
1338 gtk_text_buffer_get_end_iter( m_buffer, &end );
1339 gtk_text_buffer_place_cursor( m_buffer, &end );
fab591c5 1340#else
d59051dd 1341 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
fab591c5
RR
1342#endif
1343 }
d59051dd 1344 else
fab591c5 1345 {
d59051dd 1346 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
fab591c5 1347 }
6de97a3b 1348}
c801d85f 1349
debe6624 1350void wxTextCtrl::SetEditable( bool editable )
c801d85f 1351{
223d09f6 1352 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1353
2830bf19 1354 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1355 {
1356#ifdef __WXGTK20__
1357 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1358#else
2830bf19 1359 gtk_text_set_editable( GTK_TEXT(m_text), editable );
fab591c5
RR
1360#endif
1361 }
2830bf19 1362 else
fab591c5 1363 {
2830bf19 1364 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
fab591c5 1365 }
6de97a3b 1366}
c801d85f 1367
68df5777
RR
1368bool wxTextCtrl::Enable( bool enable )
1369{
1370 if (!wxWindowBase::Enable(enable))
1371 {
1372 // nothing to do
7d8268a1 1373 return false;
68df5777 1374 }
f6bcfd97 1375
68df5777
RR
1376 if (m_windowStyle & wxTE_MULTILINE)
1377 {
fab591c5
RR
1378#ifdef __WXGTK20__
1379 SetEditable( enable );
1380#else
68df5777 1381 gtk_text_set_editable( GTK_TEXT(m_text), enable );
5abf8c9d 1382 OnParentEnable(enable);
fab591c5 1383#endif
68df5777
RR
1384 }
1385 else
1386 {
1387 gtk_widget_set_sensitive( m_text, enable );
1388 }
1389
7d8268a1 1390 return true;
68df5777
RR
1391}
1392
fdca68a6
JS
1393// wxGTK-specific: called recursively by Enable,
1394// to give widgets an oppprtunity to correct their colours after they
1395// have been changed by Enable
1396void wxTextCtrl::OnParentEnable( bool enable )
1397{
1398 // If we have a custom background colour, we use this colour in both
1399 // disabled and enabled mode, or we end up with a different colour under the
1400 // text.
1401 wxColour oldColour = GetBackgroundColour();
1402 if (oldColour.Ok())
1403 {
1404 // Need to set twice or it'll optimize the useful stuff out
1405 if (oldColour == * wxWHITE)
1406 SetBackgroundColour(*wxBLACK);
1407 else
1408 SetBackgroundColour(*wxWHITE);
1409 SetBackgroundColour(oldColour);
1410 }
1411}
1412
3a9fa0d6
VZ
1413void wxTextCtrl::MarkDirty()
1414{
7d8268a1 1415 m_modified = true;
3a9fa0d6
VZ
1416}
1417
0efe5ba7
VZ
1418void wxTextCtrl::DiscardEdits()
1419{
7d8268a1 1420 m_modified = false;
0efe5ba7
VZ
1421}
1422
ce2f50e3
VZ
1423// ----------------------------------------------------------------------------
1424// max text length support
1425// ----------------------------------------------------------------------------
1426
1427void wxTextCtrl::IgnoreNextTextUpdate()
1428{
7d8268a1 1429 m_ignoreNextUpdate = true;
ce2f50e3
VZ
1430}
1431
1432bool wxTextCtrl::IgnoreTextUpdate()
1433{
1434 if ( m_ignoreNextUpdate )
1435 {
7d8268a1 1436 m_ignoreNextUpdate = false;
ce2f50e3 1437
7d8268a1 1438 return true;
ce2f50e3
VZ
1439 }
1440
7d8268a1 1441 return false;
ce2f50e3
VZ
1442}
1443
d7eee191
VZ
1444void wxTextCtrl::SetMaxLength(unsigned long len)
1445{
1446 if ( !HasFlag(wxTE_MULTILINE) )
1447 {
1448 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
ce2f50e3
VZ
1449
1450 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1451 // we had tried to enter more text than allowed by max text length and
1452 // the text wasn't really changed
1453 //
1454 // to detect this and generate TEXT_MAXLEN event instead of
1455 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1456 //
1457 // when max len is set to 0 we disconnect our handler as it means that
1458 // we shouldn't check anything any more
1459 if ( len )
1460 {
1461 gtk_signal_connect( GTK_OBJECT(m_text),
1462 "insert_text",
1463 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1464 (gpointer)this);
1465 }
1466 else // no checking
1467 {
1468 gtk_signal_disconnect_by_func
1469 (
1470 GTK_OBJECT(m_text),
1471 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1472 (gpointer)this
1473 );
1474 }
d7eee191
VZ
1475 }
1476}
1477
debe6624 1478void wxTextCtrl::SetSelection( long from, long to )
c801d85f 1479{
223d09f6 1480 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1481
2b5f62a0
VZ
1482 if (from == -1 && to == -1)
1483 {
1484 from = 0;
1485 to = GetValue().Length();
1486 }
1487
fab591c5 1488#ifndef __WXGTK20__
a1f79c1e
VZ
1489 if ( (m_windowStyle & wxTE_MULTILINE) &&
1490 !GTK_TEXT(m_text)->line_start_cache )
1491 {
1492 // tell the programmer that it didn't work
1493 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1494 return;
1495 }
fab591c5 1496#endif
a1f79c1e 1497
fab591c5
RR
1498 if (m_windowStyle & wxTE_MULTILINE)
1499 {
1500#ifdef __WXGTK20__
739e366a 1501 GtkTextIter fromi, toi;
41b81aed
RR
1502 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1503 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
739e366a 1504
41b81aed
RR
1505 gtk_text_buffer_place_cursor( m_buffer, &toi );
1506 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
fab591c5
RR
1507#else
1508 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1509#endif
1510 }
1511 else
1512 {
1513 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1514 }
6de97a3b 1515}
c801d85f 1516
afbe906a 1517void wxTextCtrl::ShowPosition( long pos )
c801d85f 1518{
afbe906a
RR
1519 if (m_windowStyle & wxTE_MULTILINE)
1520 {
71aba833 1521#ifdef __WXGTK20__
71aba833 1522 GtkTextIter iter;
41b81aed 1523 gtk_text_buffer_get_start_iter( m_buffer, &iter );
71aba833 1524 gtk_text_iter_set_offset( &iter, pos );
41b81aed 1525 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
71aba833
VZ
1526 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1527#else // GTK 1.x
afbe906a
RR
1528 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1529 float totalLines = (float) GetNumberOfLines();
1530 long posX;
1531 long posY;
1532 PositionToXY(pos, &posX, &posY);
1533 float posLine = (float) posY;
1534 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1535 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
71aba833 1536#endif // GTK 1.x/2.x
afbe906a 1537 }
6de97a3b 1538}
c801d85f 1539
692c9b86
VZ
1540#ifdef __WXGTK20__
1541
1542wxTextCtrlHitTestResult
1543wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1544{
1545 if ( !IsMultiLine() )
1546 {
1547 // not supported
1548 return wxTE_HT_UNKNOWN;
1549 }
1550
1551 int x, y;
1552 gtk_text_view_window_to_buffer_coords
1553 (
1554 GTK_TEXT_VIEW(m_text),
1555 GTK_TEXT_WINDOW_TEXT,
1556 pt.x, pt.y,
1557 &x, &y
1558 );
1559
1560 GtkTextIter iter;
1561 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1562 if ( pos )
1563 *pos = gtk_text_iter_get_offset(&iter);
1564
1565 return wxTE_HT_ON_TEXT;
1566}
1567
1568#endif // __WXGTK20__
1569
03f38c58 1570long wxTextCtrl::GetInsertionPoint() const
c801d85f 1571{
223d09f6 1572 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1573
fab591c5
RR
1574#ifdef __WXGTK20__
1575 if (m_windowStyle & wxTE_MULTILINE)
1576 {
fab591c5
RR
1577 // There is no direct accessor for the cursor, but
1578 // internally, the cursor is the "mark" called
1579 // "insert" in the text view's btree structure.
a8bf1826 1580
41b81aed 1581 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
fab591c5 1582 GtkTextIter cursor;
41b81aed 1583 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
a8bf1826 1584
fab591c5
RR
1585 return gtk_text_iter_get_offset( &cursor );
1586 }
1587 else
1588#endif
1589 {
0a164d4c 1590 return (long) GET_EDITABLE_POS(m_text);
fab591c5 1591 }
6de97a3b 1592}
c801d85f 1593
7d8268a1 1594wxTextPos wxTextCtrl::GetLastPosition() const
c801d85f 1595{
223d09f6 1596 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1597
2830bf19 1598 int pos = 0;
a8bf1826 1599
2830bf19 1600 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1601 {
1602#ifdef __WXGTK20__
fab591c5 1603 GtkTextIter end;
41b81aed 1604 gtk_text_buffer_get_end_iter( m_buffer, &end );
a8bf1826 1605
fab591c5
RR
1606 pos = gtk_text_iter_get_offset( &end );
1607#else
2830bf19 1608 pos = gtk_text_get_length( GTK_TEXT(m_text) );
fab591c5
RR
1609#endif
1610 }
2830bf19 1611 else
fab591c5 1612 {
2830bf19 1613 pos = GTK_ENTRY(m_text)->text_length;
fab591c5 1614 }
805dd538 1615
ac0d36b5 1616 return (long)pos;
6de97a3b 1617}
c801d85f 1618
debe6624 1619void wxTextCtrl::Remove( long from, long to )
c801d85f 1620{
223d09f6 1621 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1622
581ee8a9 1623#ifdef __WXGTK20__
fdd55287 1624 if (m_windowStyle & wxTE_MULTILINE)
581ee8a9 1625 {
581ee8a9 1626 GtkTextIter fromi, toi;
41b81aed
RR
1627 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1628 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
581ee8a9 1629
41b81aed 1630 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
581ee8a9
VZ
1631 }
1632 else // single line
fab591c5 1633#endif
581ee8a9 1634 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 1635}
c801d85f 1636
debe6624 1637void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 1638{
223d09f6 1639 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1640
581ee8a9 1641 Remove( from, to );
bb69661b 1642
7d8268a1 1643 if (!value.empty())
2df7be7f 1644 {
581ee8a9
VZ
1645#ifdef __WXGTK20__
1646 SetInsertionPoint( from );
1647 WriteText( value );
1648#else // GTK 1.x
2df7be7f 1649 gint pos = (gint)from;
05939a81 1650#if wxUSE_UNICODE
2df7be7f
RR
1651 wxWX2MBbuf buf = value.mbc_str();
1652 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 1653#else
2df7be7f 1654 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
581ee8a9
VZ
1655#endif // wxUSE_UNICODE
1656#endif // GTK 1.x/2.x
2df7be7f 1657 }
6de97a3b 1658}
c801d85f 1659
03f38c58 1660void wxTextCtrl::Cut()
c801d85f 1661{
223d09f6 1662 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1663
bbde2e29
VS
1664#ifdef __WXGTK20__
1665 if (m_windowStyle & wxTE_MULTILINE)
1666 g_signal_emit_by_name(m_text, "cut-clipboard");
1667 else
fab591c5 1668#endif
bbde2e29 1669 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1670}
c801d85f 1671
03f38c58 1672void wxTextCtrl::Copy()
c801d85f 1673{
223d09f6 1674 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1675
bbde2e29
VS
1676#ifdef __WXGTK20__
1677 if (m_windowStyle & wxTE_MULTILINE)
1678 g_signal_emit_by_name(m_text, "copy-clipboard");
1679 else
fab591c5 1680#endif
bbde2e29 1681 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1682}
c801d85f 1683
03f38c58 1684void wxTextCtrl::Paste()
c801d85f 1685{
223d09f6 1686 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1687
bbde2e29
VS
1688#ifdef __WXGTK20__
1689 if (m_windowStyle & wxTE_MULTILINE)
1690 g_signal_emit_by_name(m_text, "paste-clipboard");
1691 else
fab591c5 1692#endif
bbde2e29 1693 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1694}
c801d85f 1695
ca8b28f2
JS
1696// Undo/redo
1697void wxTextCtrl::Undo()
1698{
1699 // TODO
223d09f6 1700 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
1701}
1702
1703void wxTextCtrl::Redo()
1704{
1705 // TODO
223d09f6 1706 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
1707}
1708
1709bool wxTextCtrl::CanUndo() const
1710{
1711 // TODO
4855a477 1712 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
7d8268a1 1713 return false;
ca8b28f2
JS
1714}
1715
1716bool wxTextCtrl::CanRedo() const
1717{
1718 // TODO
4855a477 1719 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
7d8268a1 1720 return false;
ca8b28f2
JS
1721}
1722
1723// If the return values from and to are the same, there is no
1724// selection.
2d4cc5b6 1725void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 1726{
223d09f6 1727 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 1728
5b87f8bf
RD
1729 gint from = -1;
1730 gint to = -1;
7d8268a1 1731 bool haveSelection = false;
5b87f8bf 1732
fb47b99f 1733#ifdef __WXGTK20__
5b87f8bf
RD
1734 if (m_windowStyle & wxTE_MULTILINE)
1735 {
5b87f8bf 1736 GtkTextIter ifrom, ito;
41b81aed 1737 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
5b87f8bf 1738 {
7d8268a1 1739 haveSelection = true;
5b87f8bf
RD
1740 from = gtk_text_iter_get_offset(&ifrom);
1741 to = gtk_text_iter_get_offset(&ito);
1742 }
1743 }
1744 else // not multi-line
1745 {
1746 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1747 &from, &to) )
1748 {
7d8268a1 1749 haveSelection = true;
5b87f8bf
RD
1750 }
1751 }
1752#else // not GTK2
1753 if ( (GTK_EDITABLE(m_text)->has_selection) )
1754 {
7d8268a1 1755 haveSelection = true;
5b87f8bf
RD
1756 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1757 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1758 }
9e691f46 1759#endif
2d4cc5b6 1760
5b87f8bf
RD
1761 if (! haveSelection )
1762 from = to = GetInsertionPoint();
1763
1764 if ( from > to )
1765 {
1766 // exchange them to be compatible with wxMSW
1767 gint tmp = from;
1768 from = to;
1769 to = tmp;
1770 }
bb69661b 1771
2d4cc5b6
VZ
1772 if ( fromOut )
1773 *fromOut = from;
1774 if ( toOut )
1775 *toOut = to;
ca8b28f2
JS
1776}
1777
5b87f8bf 1778
ca8b28f2
JS
1779bool wxTextCtrl::IsEditable() const
1780{
7d8268a1 1781 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
05060eeb 1782
9e691f46 1783#ifdef __WXGTK20__
fdd55287
VZ
1784 if (m_windowStyle & wxTE_MULTILINE)
1785 {
1786 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1787 }
1788 else
1789 {
1790 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1791 }
9e691f46 1792#else
05060eeb 1793 return GTK_EDITABLE(m_text)->editable;
9e691f46 1794#endif
ca8b28f2
JS
1795}
1796
0efe5ba7
VZ
1797bool wxTextCtrl::IsModified() const
1798{
1799 return m_modified;
1800}
1801
03f38c58 1802void wxTextCtrl::Clear()
c801d85f 1803{
902725ee 1804 SetValue( wxEmptyString );
6de97a3b 1805}
c801d85f 1806
903f689b 1807void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 1808{
223d09f6 1809 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 1810
12a3f227 1811 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
2830bf19
RR
1812 {
1813 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1814 event.SetEventObject(this);
f6bcfd97 1815 event.SetString(GetValue());
2830bf19
RR
1816 if (GetEventHandler()->ProcessEvent(event)) return;
1817 }
903f689b 1818
12a3f227 1819 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
da048e3d 1820 {
2b328fc9
RR
1821 // This will invoke the dialog default action, such
1822 // as the clicking the default button.
a8bf1826 1823
da048e3d 1824 wxWindow *top_frame = m_parent;
8487f887 1825 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 1826 top_frame = top_frame->GetParent();
a8bf1826 1827
2b328fc9 1828 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
da048e3d 1829 {
2b328fc9
RR
1830 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1831
1832 if (window->default_widget)
1833 {
1834 gtk_widget_activate (window->default_widget);
1835 return;
1836 }
13111b2a 1837 }
da048e3d
RR
1838 }
1839
2830bf19 1840 key_event.Skip();
6de97a3b 1841}
c801d85f 1842
03f38c58 1843GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1844{
ae0bdb01 1845 return GTK_WIDGET(m_text);
6de97a3b 1846}
e3e65dac 1847
903f689b
RR
1848bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1849{
ae0bdb01 1850 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1851 {
1852#ifdef __WXGTK20__
1853 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1854#else
ae0bdb01 1855 return (window == GTK_TEXT(m_text)->text_area);
fab591c5
RR
1856#endif
1857 }
ae0bdb01 1858 else
fab591c5 1859 {
ae0bdb01 1860 return (window == GTK_ENTRY(m_text)->text_area);
fab591c5 1861 }
903f689b 1862}
e3e65dac 1863
bb69661b
VZ
1864// the font will change for subsequent text insertiongs
1865bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1866{
7d8268a1 1867 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
8bbe427f 1868
a66954a6 1869 if ( !wxTextCtrlBase::SetFont(font) )
bb69661b
VZ
1870 {
1871 // font didn't change, nothing to do
7d8268a1 1872 return false;
bb69661b
VZ
1873 }
1874
1875 if ( m_windowStyle & wxTE_MULTILINE )
1876 {
7d8268a1 1877 SetUpdateFont(true);
bb69661b 1878
1ff4714d
VZ
1879 m_defaultStyle.SetFont(font);
1880
01041145 1881 ChangeFontGlobally();
bb69661b
VZ
1882 }
1883
7d8268a1 1884 return true;
58614078
RR
1885}
1886
01041145
VZ
1887void wxTextCtrl::ChangeFontGlobally()
1888{
1889 // this method is very inefficient and hence should be called as rarely as
1890 // possible!
c04ec496
VZ
1891 //
1892 // TODO: it can be implemented much more efficiently for GTK2
c04ec496 1893#ifndef __WXGTK20__
22800f32
JS
1894 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1895
01041145 1896 _T("shouldn't be called for single line controls") );
22800f32
JS
1897#else
1898 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1899 _T("shouldn't be called for single line controls") );
1900#endif
01041145
VZ
1901
1902 wxString value = GetValue();
7d8268a1 1903 if ( !value.empty() )
01041145 1904 {
7d8268a1 1905 SetUpdateFont(false);
572aeb77 1906
01041145
VZ
1907 Clear();
1908 AppendText(value);
01041145
VZ
1909 }
1910}
1911
c04ec496
VZ
1912#ifndef __WXGTK20__
1913
01041145
VZ
1914void wxTextCtrl::UpdateFontIfNeeded()
1915{
1916 if ( m_updateFont )
1917 ChangeFontGlobally();
1918}
1919
c04ec496
VZ
1920#endif // GTK+ 1.x
1921
17665a2b
VZ
1922bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1923{
1924 if ( !wxControl::SetForegroundColour(colour) )
7d8268a1 1925 return false;
17665a2b
VZ
1926
1927 // update default fg colour too
1928 m_defaultStyle.SetTextColour(colour);
1929
7d8268a1 1930 return true;
17665a2b
VZ
1931}
1932
f03fc89f 1933bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1934{
7d8268a1 1935 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
a81258be 1936
1f477433 1937 if ( !wxControl::SetBackgroundColour( colour ) )
7d8268a1 1938 return false;
3358d36e 1939
c2094564 1940#ifndef __WXGTK20__
f03fc89f 1941 if (!m_widget->window)
7d8268a1 1942 return false;
c2094564 1943#endif
8bbe427f 1944
f03fc89f 1945 if (!m_backgroundColour.Ok())
7d8268a1 1946 return false;
8bbe427f 1947
ae0bdb01
RR
1948 if (m_windowStyle & wxTE_MULTILINE)
1949 {
1fc32b12 1950#ifndef __WXGTK20__
ae0bdb01 1951 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f 1952 if (!window)
7d8268a1 1953 return false;
ae0bdb01
RR
1954 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1955 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1956 gdk_window_clear( window );
fab591c5 1957#endif
ae0bdb01 1958 }
f03fc89f 1959
17665a2b
VZ
1960 // change active background color too
1961 m_defaultStyle.SetBackgroundColour( colour );
1962
7d8268a1 1963 return true;
58614078
RR
1964}
1965
eda40bfc 1966bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
17665a2b 1967{
17665a2b
VZ
1968 if ( m_windowStyle & wxTE_MULTILINE )
1969 {
1970 if ( style.IsDefault() )
1971 {
1972 // nothing to do
7d8268a1 1973 return true;
17665a2b 1974 }
902725ee 1975
cc3da3f8 1976#ifdef __WXGTK20__
41b81aed 1977 gint l = gtk_text_buffer_get_char_count( m_buffer );
17665a2b 1978
7d8268a1 1979 wxCHECK_MSG( start >= 0 && end <= l, false,
cc3da3f8
RR
1980 _T("invalid range in wxTextCtrl::SetStyle") );
1981
1982 GtkTextIter starti, endi;
41b81aed
RR
1983 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1984 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
cc3da3f8
RR
1985
1986 // use the attributes from style which are set in it and fall back
1987 // first to the default style and then to the text control default
1988 // colours for the others
1989 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1990
41b81aed 1991 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
cc3da3f8
RR
1992#else
1993 // VERY dirty way to do that - removes the required text and re-adds it
1994 // with styling (FIXME)
5b87f8bf 1995
17665a2b
VZ
1996 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1997
7d8268a1 1998 wxCHECK_MSG( start >= 0 && end <= l, false,
17665a2b
VZ
1999 _T("invalid range in wxTextCtrl::SetStyle") );
2000
2001 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
2002 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
2003 wxString tmp(text,*wxConvCurrent);
2004 g_free( text );
2005
2006 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
2007 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
2008
902725ee 2009 #if wxUSE_UNICODE
17665a2b
VZ
2010 wxWX2MBbuf buf = tmp.mbc_str();
2011 const char *txt = buf;
2012 size_t txtlen = strlen(buf);
902725ee 2013 #else
17665a2b
VZ
2014 const char *txt = tmp;
2015 size_t txtlen = tmp.length();
902725ee 2016 #endif
17665a2b 2017
eda40bfc
VZ
2018 // use the attributes from style which are set in it and fall back
2019 // first to the default style and then to the text control default
2020 // colours for the others
2021 wxGtkTextInsert(m_text,
2022 wxTextAttr::Combine(style, m_defaultStyle, this),
2023 txt,
2024 txtlen);
17665a2b
VZ
2025
2026 /* does not seem to help under GTK+ 1.2 !!!
2027 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
2028 SetInsertionPoint( old_pos );
fab591c5 2029#endif
902725ee 2030
7d8268a1 2031 return true;
17665a2b 2032 }
902725ee
WS
2033
2034 // else single line
2035 // cannot do this for GTK+'s Entry widget
2036 return false;
17665a2b
VZ
2037}
2038
f40fdaa3 2039void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
58614078 2040{
f40fdaa3 2041 gtk_widget_modify_style(m_text, style);
68dda785 2042}
f96aa4d9 2043
e702ff0f
JS
2044void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2045{
2046 Cut();
2047}
2048
2049void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2050{
2051 Copy();
2052}
2053
2054void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2055{
2056 Paste();
2057}
2058
2059void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2060{
2061 Undo();
2062}
2063
2064void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2065{
2066 Redo();
2067}
2068
2069void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2070{
2071 event.Enable( CanCut() );
2072}
2073
2074void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2075{
2076 event.Enable( CanCopy() );
2077}
2078
2079void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2080{
2081 event.Enable( CanPaste() );
2082}
2083
2084void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2085{
2086 event.Enable( CanUndo() );
2087}
2088
2089void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2090{
2091 event.Enable( CanRedo() );
2092}
65045edd
RR
2093
2094void wxTextCtrl::OnInternalIdle()
2095{
2096 wxCursor cursor = m_cursor;
2097 if (g_globalCursor.Ok()) cursor = g_globalCursor;
2098
307f16e8 2099 if (cursor.Ok())
65045edd 2100 {
fab591c5 2101#ifndef __WXGTK20__
65045edd 2102 GdkWindow *window = (GdkWindow*) NULL;
13111b2a 2103 if (HasFlag(wxTE_MULTILINE))
65045edd
RR
2104 window = GTK_TEXT(m_text)->text_area;
2105 else
2106 window = GTK_ENTRY(m_text)->text_area;
13111b2a 2107
65045edd
RR
2108 if (window)
2109 gdk_window_set_cursor( window, cursor.GetCursor() );
2110
2111 if (!g_globalCursor.Ok())
2112 cursor = *wxSTANDARD_CURSOR;
2113
2114 window = m_widget->window;
247e5b16 2115 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd 2116 gdk_window_set_cursor( window, cursor.GetCursor() );
fab591c5 2117#endif
65045edd 2118 }
26bf1ce0 2119
d7fa7eaa
RR
2120 if (g_delayedFocus == this)
2121 {
2122 if (GTK_WIDGET_REALIZED(m_widget))
2123 {
2124 gtk_widget_grab_focus( m_widget );
2125 g_delayedFocus = NULL;
2126 }
2127 }
2128
e39af974
JS
2129 if (wxUpdateUIEvent::CanUpdate(this))
2130 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
65045edd 2131}
f68586e5
VZ
2132
2133wxSize wxTextCtrl::DoGetBestSize() const
2134{
2135 // FIXME should be different for multi-line controls...
0279e844 2136 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
2137 wxSize best(80, ret.y);
2138 CacheBestSize(best);
2139 return best;
f68586e5 2140}
0cc7251e 2141
9cd6d737
VZ
2142// ----------------------------------------------------------------------------
2143// freeze/thaw
2144// ----------------------------------------------------------------------------
2145
0cc7251e
VZ
2146void wxTextCtrl::Freeze()
2147{
2148 if ( HasFlag(wxTE_MULTILINE) )
2149 {
41b81aed
RR
2150#ifdef __WXGTK20__
2151 if ( !m_frozenness++ )
2152 {
2153 // freeze textview updates and remove buffer
2154 g_signal_connect( G_OBJECT(m_text), "expose_event",
2155 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2156 g_signal_connect( G_OBJECT(m_widget), "expose_event",
2157 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2158 gtk_widget_set_sensitive(m_widget, false);
2159 g_object_ref(m_buffer);
2160 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
0a164d4c 2161 }
41b81aed
RR
2162#else
2163 gtk_text_freeze(GTK_TEXT(m_text));
fab591c5 2164#endif
41b81aed 2165 }
0cc7251e
VZ
2166}
2167
2168void wxTextCtrl::Thaw()
2169{
2170 if ( HasFlag(wxTE_MULTILINE) )
2171 {
41b81aed
RR
2172#ifdef __WXGTK20__
2173 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2174
2175 if ( !--m_frozenness )
2176 {
2177 // Reattach buffer and thaw textview updates
2178 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
2179 g_object_unref(m_buffer);
2180 gtk_widget_set_sensitive(m_widget, true);
2181 g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this);
2182 g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this);
2183 }
2184#else
817ec43a
VZ
2185 GTK_TEXT(m_text)->vadj->value = 0.0;
2186
0cc7251e 2187 gtk_text_thaw(GTK_TEXT(m_text));
fab591c5 2188#endif
41b81aed 2189 }
0cc7251e 2190}
9cd6d737 2191
9440c3d0
KH
2192// ----------------------------------------------------------------------------
2193// wxTextUrlEvent passing if style & wxTE_AUTO_URL
2194// ----------------------------------------------------------------------------
2195
2196#ifdef __WXGTK20__
2197
2198// FIXME: when dragging on a link the sample gets an "Unknown event".
2199// This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
2200// a buggy sample, or something else
2201void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
2202{
2203 event.Skip();
2204 if(!(m_windowStyle & wxTE_AUTO_URL))
2205 return;
2206
2207 gint x, y;
2208 GtkTextIter start, end;
2209 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
2210 "wxUrl");
2211
2212 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
2213 event.GetX(), event.GetY(), &x, &y);
2214
2215 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
2216 if (!gtk_text_iter_has_tag(&end, tag))
2217 {
2218 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2219 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
2220 return;
2221 }
2222
2223 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2224 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
2225
2226 start = end;
2227 if(!gtk_text_iter_begins_tag(&start, tag))
2228 gtk_text_iter_backward_to_tag_toggle(&start, tag);
2229 if(!gtk_text_iter_ends_tag(&end, tag))
2230 gtk_text_iter_forward_to_tag_toggle(&end, tag);
2231
2232 // Native context menu is probably not desired on an URL.
2233 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
2234 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
2235 event.Skip(false);
2236
2237 wxTextUrlEvent url_event(m_windowId, event,
2238 gtk_text_iter_get_offset(&start),
2239 gtk_text_iter_get_offset(&end));
2240
2241 InitCommandEvent(url_event);
2242 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2243 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
2244 GetEventHandler()->ProcessEvent(url_event);
2245}
2246#endif // gtk2
2247
9cd6d737
VZ
2248// ----------------------------------------------------------------------------
2249// scrolling
2250// ----------------------------------------------------------------------------
2251
2252GtkAdjustment *wxTextCtrl::GetVAdj() const
2253{
1ce52aa6
VZ
2254 if ( !IsMultiLine() )
2255 return NULL;
2256
fab591c5 2257#ifdef __WXGTK20__
1ce52aa6 2258 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
fab591c5 2259#else
1ce52aa6 2260 return GTK_TEXT(m_text)->vadj;
fab591c5 2261#endif
9cd6d737
VZ
2262}
2263
2264bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
2265{
2266 float value = adj->value + diff;
2267
2268 if ( value < 0 )
2269 value = 0;
2270
2271 float upper = adj->upper - adj->page_size;
2272 if ( value > upper )
2273 value = upper;
2274
2275 // did we noticeably change the scroll position?
2276 if ( fabs(adj->value - value) < 0.2 )
2277 {
2278 // well, this is what Robert does in wxScrollBar, so it must be good...
7d8268a1 2279 return false;
9cd6d737
VZ
2280 }
2281
2282 adj->value = value;
2283
1ce52aa6
VZ
2284#ifdef __WXGTK20__
2285 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
2286#else
9cd6d737 2287 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
fab591c5 2288#endif
1ce52aa6 2289
7d8268a1 2290 return true;
9cd6d737
VZ
2291}
2292
2293bool wxTextCtrl::ScrollLines(int lines)
2294{
2295 GtkAdjustment *adj = GetVAdj();
2296 if ( !adj )
7d8268a1 2297 return false;
9cd6d737 2298
1ce52aa6
VZ
2299#ifdef __WXGTK20__
2300 int diff = (int)ceil(lines*adj->step_increment);
2301#else
9cd6d737 2302 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1ce52aa6 2303 int diff = 10*lines;
fab591c5 2304#endif
1ce52aa6
VZ
2305
2306 return DoScroll(adj, diff);
9cd6d737
VZ
2307}
2308
2309bool wxTextCtrl::ScrollPages(int pages)
2310{
2311 GtkAdjustment *adj = GetVAdj();
2312 if ( !adj )
7d8268a1 2313 return false;
9cd6d737 2314
817ec43a 2315 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
9cd6d737
VZ
2316}
2317
9d522606
RD
2318
2319// static
2320wxVisualAttributes
2321wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2322{
2323 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
2324}