]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
Removed wxVectorBase and wxClientDataDictionary symbols
[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;
1213 gtk_text_buffer_get_iter_at_line_offset(m_buffer, &iter, y, x);
1214 return gtk_text_iter_get_offset(&iter);
1215#else
2830bf19 1216 long pos=0;
ac0d36b5 1217 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 1218
3358d36e 1219 pos += x;
2830bf19 1220 return pos;
f29a481a 1221#endif
6de97a3b 1222}
c801d85f 1223
a81258be 1224int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 1225{
f29a481a
MR
1226#ifdef __WXGTK20__
1227 if (m_windowStyle & wxTE_MULTILINE)
1228 {
1229 int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1;
1230 if (lineNo > last_line)
1231 return -1;
1232
1233 GtkTextIter iter;
1234 gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo);
1235 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1236 return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1);
1237 }
1238 else
1239#endif
1240 {
1241 wxString str = GetLineText (lineNo);
1242 return (int) str.Length();
1243 }
6de97a3b 1244}
c801d85f 1245
a81258be 1246int wxTextCtrl::GetNumberOfLines() const
c801d85f 1247{
2830bf19 1248 if (m_windowStyle & wxTE_MULTILINE)
a81258be 1249 {
fab591c5 1250#ifdef __WXGTK20__
41b81aed 1251 return gtk_text_buffer_get_line_count( m_buffer );
fab591c5 1252#else
2830bf19
RR
1253 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1254 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
1255
1256 if (text)
1257 {
1258 int currentLine = 0;
1259 for (int i = 0; i < len; i++ )
96385642 1260 {
2830bf19 1261 if (text[i] == '\n')
96385642
VZ
1262 currentLine++;
1263 }
2830bf19 1264 g_free( text );
2829d9e3
VZ
1265
1266 // currentLine is 0 based, add 1 to get number of lines
1267 return currentLine + 1;
2830bf19
RR
1268 }
1269 else
96385642 1270 {
2830bf19 1271 return 0;
96385642 1272 }
fab591c5 1273#endif
a81258be
RR
1274 }
1275 else
2830bf19 1276 {
96385642 1277 return 1;
2830bf19 1278 }
6de97a3b 1279}
c801d85f 1280
debe6624 1281void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 1282{
223d09f6 1283 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e 1284
74c5a810 1285 if ( IsMultiLine() )
291a8f20 1286 {
fab591c5 1287#ifdef __WXGTK20__
fab591c5 1288 GtkTextIter iter;
41b81aed
RR
1289 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1290 gtk_text_buffer_place_cursor( m_buffer, &iter );
74c5a810
VZ
1291 gtk_text_view_scroll_mark_onscreen
1292 (
1293 GTK_TEXT_VIEW(m_text),
41b81aed 1294 gtk_text_buffer_get_insert( m_buffer )
74c5a810
VZ
1295 );
1296#else // GTK+ 1.x
291a8f20
RR
1297 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
1298 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 1299
291a8f20 1300 /* we fake a set_point by inserting and deleting. as the user
fab591c5 1301 isn't supposed to get to know about this non-sense, we
3358d36e
VZ
1302 disconnect so that no events are sent to the user program. */
1303
291a8f20
RR
1304 gint tmp = (gint)pos;
1305 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
1306 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
1307
291a8f20
RR
1308 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
1309 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 1310
fab591c5 1311 // bring editable's cursor uptodate. Bug in GTK.
9e691f46 1312 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
74c5a810 1313#endif // GTK+ 2/1
ac0d36b5 1314 }
2830bf19 1315 else
291a8f20 1316 {
d59051dd 1317 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e 1318
fab591c5 1319 // Bring editable's cursor uptodate. Bug in GTK.
9e691f46 1320 SET_EDITABLE_POS(m_text, (guint32)pos);
291a8f20 1321 }
6de97a3b 1322}
c801d85f 1323
03f38c58 1324void wxTextCtrl::SetInsertionPointEnd()
c801d85f 1325{
223d09f6 1326 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1327
d59051dd 1328 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1329 {
1330#ifdef __WXGTK20__
fab591c5 1331 GtkTextIter end;
41b81aed
RR
1332 gtk_text_buffer_get_end_iter( m_buffer, &end );
1333 gtk_text_buffer_place_cursor( m_buffer, &end );
fab591c5 1334#else
d59051dd 1335 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
fab591c5
RR
1336#endif
1337 }
d59051dd 1338 else
fab591c5 1339 {
d59051dd 1340 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
fab591c5 1341 }
6de97a3b 1342}
c801d85f 1343
debe6624 1344void wxTextCtrl::SetEditable( bool editable )
c801d85f 1345{
223d09f6 1346 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1347
2830bf19 1348 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1349 {
1350#ifdef __WXGTK20__
1351 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1352#else
2830bf19 1353 gtk_text_set_editable( GTK_TEXT(m_text), editable );
fab591c5
RR
1354#endif
1355 }
2830bf19 1356 else
fab591c5 1357 {
2830bf19 1358 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
fab591c5 1359 }
6de97a3b 1360}
c801d85f 1361
68df5777
RR
1362bool wxTextCtrl::Enable( bool enable )
1363{
1364 if (!wxWindowBase::Enable(enable))
1365 {
1366 // nothing to do
7d8268a1 1367 return false;
68df5777 1368 }
f6bcfd97 1369
68df5777
RR
1370 if (m_windowStyle & wxTE_MULTILINE)
1371 {
fab591c5
RR
1372#ifdef __WXGTK20__
1373 SetEditable( enable );
1374#else
68df5777 1375 gtk_text_set_editable( GTK_TEXT(m_text), enable );
5abf8c9d 1376 OnParentEnable(enable);
fab591c5 1377#endif
68df5777
RR
1378 }
1379 else
1380 {
1381 gtk_widget_set_sensitive( m_text, enable );
1382 }
1383
7d8268a1 1384 return true;
68df5777
RR
1385}
1386
fdca68a6
JS
1387// wxGTK-specific: called recursively by Enable,
1388// to give widgets an oppprtunity to correct their colours after they
1389// have been changed by Enable
1390void wxTextCtrl::OnParentEnable( bool enable )
1391{
1392 // If we have a custom background colour, we use this colour in both
1393 // disabled and enabled mode, or we end up with a different colour under the
1394 // text.
1395 wxColour oldColour = GetBackgroundColour();
1396 if (oldColour.Ok())
1397 {
1398 // Need to set twice or it'll optimize the useful stuff out
1399 if (oldColour == * wxWHITE)
1400 SetBackgroundColour(*wxBLACK);
1401 else
1402 SetBackgroundColour(*wxWHITE);
1403 SetBackgroundColour(oldColour);
1404 }
1405}
1406
3a9fa0d6
VZ
1407void wxTextCtrl::MarkDirty()
1408{
7d8268a1 1409 m_modified = true;
3a9fa0d6
VZ
1410}
1411
0efe5ba7
VZ
1412void wxTextCtrl::DiscardEdits()
1413{
7d8268a1 1414 m_modified = false;
0efe5ba7
VZ
1415}
1416
ce2f50e3
VZ
1417// ----------------------------------------------------------------------------
1418// max text length support
1419// ----------------------------------------------------------------------------
1420
1421void wxTextCtrl::IgnoreNextTextUpdate()
1422{
7d8268a1 1423 m_ignoreNextUpdate = true;
ce2f50e3
VZ
1424}
1425
1426bool wxTextCtrl::IgnoreTextUpdate()
1427{
1428 if ( m_ignoreNextUpdate )
1429 {
7d8268a1 1430 m_ignoreNextUpdate = false;
ce2f50e3 1431
7d8268a1 1432 return true;
ce2f50e3
VZ
1433 }
1434
7d8268a1 1435 return false;
ce2f50e3
VZ
1436}
1437
d7eee191
VZ
1438void wxTextCtrl::SetMaxLength(unsigned long len)
1439{
1440 if ( !HasFlag(wxTE_MULTILINE) )
1441 {
1442 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
ce2f50e3
VZ
1443
1444 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1445 // we had tried to enter more text than allowed by max text length and
1446 // the text wasn't really changed
1447 //
1448 // to detect this and generate TEXT_MAXLEN event instead of
1449 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1450 //
1451 // when max len is set to 0 we disconnect our handler as it means that
1452 // we shouldn't check anything any more
1453 if ( len )
1454 {
1455 gtk_signal_connect( GTK_OBJECT(m_text),
1456 "insert_text",
1457 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1458 (gpointer)this);
1459 }
1460 else // no checking
1461 {
1462 gtk_signal_disconnect_by_func
1463 (
1464 GTK_OBJECT(m_text),
1465 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1466 (gpointer)this
1467 );
1468 }
d7eee191
VZ
1469 }
1470}
1471
debe6624 1472void wxTextCtrl::SetSelection( long from, long to )
c801d85f 1473{
223d09f6 1474 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1475
2b5f62a0
VZ
1476 if (from == -1 && to == -1)
1477 {
1478 from = 0;
1479 to = GetValue().Length();
1480 }
1481
fab591c5 1482#ifndef __WXGTK20__
a1f79c1e
VZ
1483 if ( (m_windowStyle & wxTE_MULTILINE) &&
1484 !GTK_TEXT(m_text)->line_start_cache )
1485 {
1486 // tell the programmer that it didn't work
1487 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1488 return;
1489 }
fab591c5 1490#endif
a1f79c1e 1491
fab591c5
RR
1492 if (m_windowStyle & wxTE_MULTILINE)
1493 {
1494#ifdef __WXGTK20__
739e366a 1495 GtkTextIter fromi, toi;
41b81aed
RR
1496 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1497 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
739e366a 1498
41b81aed
RR
1499 gtk_text_buffer_place_cursor( m_buffer, &toi );
1500 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
fab591c5
RR
1501#else
1502 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1503#endif
1504 }
1505 else
1506 {
1507 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1508 }
6de97a3b 1509}
c801d85f 1510
afbe906a 1511void wxTextCtrl::ShowPosition( long pos )
c801d85f 1512{
afbe906a
RR
1513 if (m_windowStyle & wxTE_MULTILINE)
1514 {
71aba833 1515#ifdef __WXGTK20__
71aba833 1516 GtkTextIter iter;
41b81aed 1517 gtk_text_buffer_get_start_iter( m_buffer, &iter );
71aba833 1518 gtk_text_iter_set_offset( &iter, pos );
41b81aed 1519 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
71aba833
VZ
1520 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1521#else // GTK 1.x
afbe906a
RR
1522 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1523 float totalLines = (float) GetNumberOfLines();
1524 long posX;
1525 long posY;
1526 PositionToXY(pos, &posX, &posY);
1527 float posLine = (float) posY;
1528 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1529 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
71aba833 1530#endif // GTK 1.x/2.x
afbe906a 1531 }
6de97a3b 1532}
c801d85f 1533
692c9b86
VZ
1534#ifdef __WXGTK20__
1535
1536wxTextCtrlHitTestResult
1537wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1538{
1539 if ( !IsMultiLine() )
1540 {
1541 // not supported
1542 return wxTE_HT_UNKNOWN;
1543 }
1544
1545 int x, y;
1546 gtk_text_view_window_to_buffer_coords
1547 (
1548 GTK_TEXT_VIEW(m_text),
1549 GTK_TEXT_WINDOW_TEXT,
1550 pt.x, pt.y,
1551 &x, &y
1552 );
1553
1554 GtkTextIter iter;
1555 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1556 if ( pos )
1557 *pos = gtk_text_iter_get_offset(&iter);
1558
1559 return wxTE_HT_ON_TEXT;
1560}
1561
1562#endif // __WXGTK20__
1563
03f38c58 1564long wxTextCtrl::GetInsertionPoint() const
c801d85f 1565{
223d09f6 1566 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1567
fab591c5
RR
1568#ifdef __WXGTK20__
1569 if (m_windowStyle & wxTE_MULTILINE)
1570 {
fab591c5
RR
1571 // There is no direct accessor for the cursor, but
1572 // internally, the cursor is the "mark" called
1573 // "insert" in the text view's btree structure.
a8bf1826 1574
41b81aed 1575 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
fab591c5 1576 GtkTextIter cursor;
41b81aed 1577 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
a8bf1826 1578
fab591c5
RR
1579 return gtk_text_iter_get_offset( &cursor );
1580 }
1581 else
1582#endif
1583 {
0a164d4c 1584 return (long) GET_EDITABLE_POS(m_text);
fab591c5 1585 }
6de97a3b 1586}
c801d85f 1587
7d8268a1 1588wxTextPos wxTextCtrl::GetLastPosition() const
c801d85f 1589{
223d09f6 1590 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 1591
2830bf19 1592 int pos = 0;
a8bf1826 1593
2830bf19 1594 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1595 {
1596#ifdef __WXGTK20__
fab591c5 1597 GtkTextIter end;
41b81aed 1598 gtk_text_buffer_get_end_iter( m_buffer, &end );
a8bf1826 1599
fab591c5
RR
1600 pos = gtk_text_iter_get_offset( &end );
1601#else
2830bf19 1602 pos = gtk_text_get_length( GTK_TEXT(m_text) );
fab591c5
RR
1603#endif
1604 }
2830bf19 1605 else
fab591c5 1606 {
2830bf19 1607 pos = GTK_ENTRY(m_text)->text_length;
fab591c5 1608 }
805dd538 1609
ac0d36b5 1610 return (long)pos;
6de97a3b 1611}
c801d85f 1612
debe6624 1613void wxTextCtrl::Remove( long from, long to )
c801d85f 1614{
223d09f6 1615 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1616
581ee8a9 1617#ifdef __WXGTK20__
fdd55287 1618 if (m_windowStyle & wxTE_MULTILINE)
581ee8a9 1619 {
581ee8a9 1620 GtkTextIter fromi, toi;
41b81aed
RR
1621 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1622 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
581ee8a9 1623
41b81aed 1624 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
581ee8a9
VZ
1625 }
1626 else // single line
fab591c5 1627#endif
581ee8a9 1628 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 1629}
c801d85f 1630
debe6624 1631void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 1632{
223d09f6 1633 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1634
581ee8a9 1635 Remove( from, to );
bb69661b 1636
7d8268a1 1637 if (!value.empty())
2df7be7f 1638 {
581ee8a9
VZ
1639#ifdef __WXGTK20__
1640 SetInsertionPoint( from );
1641 WriteText( value );
1642#else // GTK 1.x
2df7be7f 1643 gint pos = (gint)from;
05939a81 1644#if wxUSE_UNICODE
2df7be7f
RR
1645 wxWX2MBbuf buf = value.mbc_str();
1646 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 1647#else
2df7be7f 1648 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
581ee8a9
VZ
1649#endif // wxUSE_UNICODE
1650#endif // GTK 1.x/2.x
2df7be7f 1651 }
6de97a3b 1652}
c801d85f 1653
03f38c58 1654void wxTextCtrl::Cut()
c801d85f 1655{
223d09f6 1656 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1657
bbde2e29
VS
1658#ifdef __WXGTK20__
1659 if (m_windowStyle & wxTE_MULTILINE)
1660 g_signal_emit_by_name(m_text, "cut-clipboard");
1661 else
fab591c5 1662#endif
bbde2e29 1663 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1664}
c801d85f 1665
03f38c58 1666void wxTextCtrl::Copy()
c801d85f 1667{
223d09f6 1668 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1669
bbde2e29
VS
1670#ifdef __WXGTK20__
1671 if (m_windowStyle & wxTE_MULTILINE)
1672 g_signal_emit_by_name(m_text, "copy-clipboard");
1673 else
fab591c5 1674#endif
bbde2e29 1675 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1676}
c801d85f 1677
03f38c58 1678void wxTextCtrl::Paste()
c801d85f 1679{
223d09f6 1680 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 1681
bbde2e29
VS
1682#ifdef __WXGTK20__
1683 if (m_windowStyle & wxTE_MULTILINE)
1684 g_signal_emit_by_name(m_text, "paste-clipboard");
1685 else
fab591c5 1686#endif
bbde2e29 1687 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
6de97a3b 1688}
c801d85f 1689
ca8b28f2
JS
1690// Undo/redo
1691void wxTextCtrl::Undo()
1692{
1693 // TODO
223d09f6 1694 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
1695}
1696
1697void wxTextCtrl::Redo()
1698{
1699 // TODO
223d09f6 1700 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
1701}
1702
1703bool wxTextCtrl::CanUndo() const
1704{
1705 // TODO
4855a477 1706 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
7d8268a1 1707 return false;
ca8b28f2
JS
1708}
1709
1710bool wxTextCtrl::CanRedo() const
1711{
1712 // TODO
4855a477 1713 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
7d8268a1 1714 return false;
ca8b28f2
JS
1715}
1716
1717// If the return values from and to are the same, there is no
1718// selection.
2d4cc5b6 1719void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 1720{
223d09f6 1721 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 1722
5b87f8bf
RD
1723 gint from = -1;
1724 gint to = -1;
7d8268a1 1725 bool haveSelection = false;
5b87f8bf 1726
fb47b99f 1727#ifdef __WXGTK20__
5b87f8bf
RD
1728 if (m_windowStyle & wxTE_MULTILINE)
1729 {
5b87f8bf 1730 GtkTextIter ifrom, ito;
41b81aed 1731 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
5b87f8bf 1732 {
7d8268a1 1733 haveSelection = true;
5b87f8bf
RD
1734 from = gtk_text_iter_get_offset(&ifrom);
1735 to = gtk_text_iter_get_offset(&ito);
1736 }
1737 }
1738 else // not multi-line
1739 {
1740 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1741 &from, &to) )
1742 {
7d8268a1 1743 haveSelection = true;
5b87f8bf
RD
1744 }
1745 }
1746#else // not GTK2
1747 if ( (GTK_EDITABLE(m_text)->has_selection) )
1748 {
7d8268a1 1749 haveSelection = true;
5b87f8bf
RD
1750 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1751 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1752 }
9e691f46 1753#endif
2d4cc5b6 1754
5b87f8bf
RD
1755 if (! haveSelection )
1756 from = to = GetInsertionPoint();
1757
1758 if ( from > to )
1759 {
1760 // exchange them to be compatible with wxMSW
1761 gint tmp = from;
1762 from = to;
1763 to = tmp;
1764 }
bb69661b 1765
2d4cc5b6
VZ
1766 if ( fromOut )
1767 *fromOut = from;
1768 if ( toOut )
1769 *toOut = to;
ca8b28f2
JS
1770}
1771
5b87f8bf 1772
ca8b28f2
JS
1773bool wxTextCtrl::IsEditable() const
1774{
7d8268a1 1775 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
05060eeb 1776
9e691f46 1777#ifdef __WXGTK20__
fdd55287
VZ
1778 if (m_windowStyle & wxTE_MULTILINE)
1779 {
1780 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1781 }
1782 else
1783 {
1784 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1785 }
9e691f46 1786#else
05060eeb 1787 return GTK_EDITABLE(m_text)->editable;
9e691f46 1788#endif
ca8b28f2
JS
1789}
1790
0efe5ba7
VZ
1791bool wxTextCtrl::IsModified() const
1792{
1793 return m_modified;
1794}
1795
03f38c58 1796void wxTextCtrl::Clear()
c801d85f 1797{
902725ee 1798 SetValue( wxEmptyString );
6de97a3b 1799}
c801d85f 1800
903f689b 1801void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 1802{
223d09f6 1803 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 1804
12a3f227 1805 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
2830bf19
RR
1806 {
1807 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1808 event.SetEventObject(this);
f6bcfd97 1809 event.SetString(GetValue());
2830bf19
RR
1810 if (GetEventHandler()->ProcessEvent(event)) return;
1811 }
903f689b 1812
12a3f227 1813 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
da048e3d 1814 {
2b328fc9
RR
1815 // This will invoke the dialog default action, such
1816 // as the clicking the default button.
a8bf1826 1817
da048e3d 1818 wxWindow *top_frame = m_parent;
8487f887 1819 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 1820 top_frame = top_frame->GetParent();
a8bf1826 1821
2b328fc9 1822 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
da048e3d 1823 {
2b328fc9
RR
1824 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1825
1826 if (window->default_widget)
1827 {
1828 gtk_widget_activate (window->default_widget);
1829 return;
1830 }
13111b2a 1831 }
da048e3d
RR
1832 }
1833
2830bf19 1834 key_event.Skip();
6de97a3b 1835}
c801d85f 1836
03f38c58 1837GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1838{
ae0bdb01 1839 return GTK_WIDGET(m_text);
6de97a3b 1840}
e3e65dac 1841
903f689b
RR
1842bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1843{
ae0bdb01 1844 if (m_windowStyle & wxTE_MULTILINE)
fab591c5
RR
1845 {
1846#ifdef __WXGTK20__
1847 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1848#else
ae0bdb01 1849 return (window == GTK_TEXT(m_text)->text_area);
fab591c5
RR
1850#endif
1851 }
ae0bdb01 1852 else
fab591c5 1853 {
ae0bdb01 1854 return (window == GTK_ENTRY(m_text)->text_area);
fab591c5 1855 }
903f689b 1856}
e3e65dac 1857
bb69661b
VZ
1858// the font will change for subsequent text insertiongs
1859bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1860{
7d8268a1 1861 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
8bbe427f 1862
a66954a6 1863 if ( !wxTextCtrlBase::SetFont(font) )
bb69661b
VZ
1864 {
1865 // font didn't change, nothing to do
7d8268a1 1866 return false;
bb69661b
VZ
1867 }
1868
1869 if ( m_windowStyle & wxTE_MULTILINE )
1870 {
7d8268a1 1871 SetUpdateFont(true);
bb69661b 1872
1ff4714d
VZ
1873 m_defaultStyle.SetFont(font);
1874
01041145 1875 ChangeFontGlobally();
bb69661b
VZ
1876 }
1877
7d8268a1 1878 return true;
58614078
RR
1879}
1880
01041145
VZ
1881void wxTextCtrl::ChangeFontGlobally()
1882{
1883 // this method is very inefficient and hence should be called as rarely as
1884 // possible!
c04ec496
VZ
1885 //
1886 // TODO: it can be implemented much more efficiently for GTK2
c04ec496 1887#ifndef __WXGTK20__
22800f32
JS
1888 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1889
01041145 1890 _T("shouldn't be called for single line controls") );
22800f32
JS
1891#else
1892 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1893 _T("shouldn't be called for single line controls") );
1894#endif
01041145
VZ
1895
1896 wxString value = GetValue();
7d8268a1 1897 if ( !value.empty() )
01041145 1898 {
7d8268a1 1899 SetUpdateFont(false);
572aeb77 1900
01041145
VZ
1901 Clear();
1902 AppendText(value);
01041145
VZ
1903 }
1904}
1905
c04ec496
VZ
1906#ifndef __WXGTK20__
1907
01041145
VZ
1908void wxTextCtrl::UpdateFontIfNeeded()
1909{
1910 if ( m_updateFont )
1911 ChangeFontGlobally();
1912}
1913
c04ec496
VZ
1914#endif // GTK+ 1.x
1915
17665a2b
VZ
1916bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1917{
1918 if ( !wxControl::SetForegroundColour(colour) )
7d8268a1 1919 return false;
17665a2b
VZ
1920
1921 // update default fg colour too
1922 m_defaultStyle.SetTextColour(colour);
1923
7d8268a1 1924 return true;
17665a2b
VZ
1925}
1926
f03fc89f 1927bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1928{
7d8268a1 1929 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
a81258be 1930
1f477433 1931 if ( !wxControl::SetBackgroundColour( colour ) )
7d8268a1 1932 return false;
3358d36e 1933
c2094564 1934#ifndef __WXGTK20__
f03fc89f 1935 if (!m_widget->window)
7d8268a1 1936 return false;
c2094564 1937#endif
8bbe427f 1938
f03fc89f 1939 if (!m_backgroundColour.Ok())
7d8268a1 1940 return false;
8bbe427f 1941
ae0bdb01
RR
1942 if (m_windowStyle & wxTE_MULTILINE)
1943 {
1fc32b12 1944#ifndef __WXGTK20__
ae0bdb01 1945 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f 1946 if (!window)
7d8268a1 1947 return false;
ae0bdb01
RR
1948 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1949 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1950 gdk_window_clear( window );
fab591c5 1951#endif
ae0bdb01 1952 }
f03fc89f 1953
17665a2b
VZ
1954 // change active background color too
1955 m_defaultStyle.SetBackgroundColour( colour );
1956
7d8268a1 1957 return true;
58614078
RR
1958}
1959
eda40bfc 1960bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
17665a2b 1961{
17665a2b
VZ
1962 if ( m_windowStyle & wxTE_MULTILINE )
1963 {
1964 if ( style.IsDefault() )
1965 {
1966 // nothing to do
7d8268a1 1967 return true;
17665a2b 1968 }
902725ee 1969
cc3da3f8 1970#ifdef __WXGTK20__
41b81aed 1971 gint l = gtk_text_buffer_get_char_count( m_buffer );
17665a2b 1972
7d8268a1 1973 wxCHECK_MSG( start >= 0 && end <= l, false,
cc3da3f8
RR
1974 _T("invalid range in wxTextCtrl::SetStyle") );
1975
1976 GtkTextIter starti, endi;
41b81aed
RR
1977 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1978 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
cc3da3f8
RR
1979
1980 // use the attributes from style which are set in it and fall back
1981 // first to the default style and then to the text control default
1982 // colours for the others
1983 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1984
41b81aed 1985 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
cc3da3f8
RR
1986#else
1987 // VERY dirty way to do that - removes the required text and re-adds it
1988 // with styling (FIXME)
5b87f8bf 1989
17665a2b
VZ
1990 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1991
7d8268a1 1992 wxCHECK_MSG( start >= 0 && end <= l, false,
17665a2b
VZ
1993 _T("invalid range in wxTextCtrl::SetStyle") );
1994
1995 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1996 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1997 wxString tmp(text,*wxConvCurrent);
1998 g_free( text );
1999
2000 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
2001 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
2002
902725ee 2003 #if wxUSE_UNICODE
17665a2b
VZ
2004 wxWX2MBbuf buf = tmp.mbc_str();
2005 const char *txt = buf;
2006 size_t txtlen = strlen(buf);
902725ee 2007 #else
17665a2b
VZ
2008 const char *txt = tmp;
2009 size_t txtlen = tmp.length();
902725ee 2010 #endif
17665a2b 2011
eda40bfc
VZ
2012 // use the attributes from style which are set in it and fall back
2013 // first to the default style and then to the text control default
2014 // colours for the others
2015 wxGtkTextInsert(m_text,
2016 wxTextAttr::Combine(style, m_defaultStyle, this),
2017 txt,
2018 txtlen);
17665a2b
VZ
2019
2020 /* does not seem to help under GTK+ 1.2 !!!
2021 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
2022 SetInsertionPoint( old_pos );
fab591c5 2023#endif
902725ee 2024
7d8268a1 2025 return true;
17665a2b 2026 }
902725ee
WS
2027
2028 // else single line
2029 // cannot do this for GTK+'s Entry widget
2030 return false;
17665a2b
VZ
2031}
2032
f40fdaa3 2033void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
58614078 2034{
f40fdaa3 2035 gtk_widget_modify_style(m_text, style);
68dda785 2036}
f96aa4d9 2037
e702ff0f
JS
2038void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2039{
2040 Cut();
2041}
2042
2043void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2044{
2045 Copy();
2046}
2047
2048void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2049{
2050 Paste();
2051}
2052
2053void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2054{
2055 Undo();
2056}
2057
2058void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2059{
2060 Redo();
2061}
2062
2063void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2064{
2065 event.Enable( CanCut() );
2066}
2067
2068void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2069{
2070 event.Enable( CanCopy() );
2071}
2072
2073void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2074{
2075 event.Enable( CanPaste() );
2076}
2077
2078void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2079{
2080 event.Enable( CanUndo() );
2081}
2082
2083void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2084{
2085 event.Enable( CanRedo() );
2086}
65045edd
RR
2087
2088void wxTextCtrl::OnInternalIdle()
2089{
2090 wxCursor cursor = m_cursor;
2091 if (g_globalCursor.Ok()) cursor = g_globalCursor;
2092
307f16e8 2093 if (cursor.Ok())
65045edd 2094 {
fab591c5 2095#ifndef __WXGTK20__
65045edd 2096 GdkWindow *window = (GdkWindow*) NULL;
13111b2a 2097 if (HasFlag(wxTE_MULTILINE))
65045edd
RR
2098 window = GTK_TEXT(m_text)->text_area;
2099 else
2100 window = GTK_ENTRY(m_text)->text_area;
13111b2a 2101
65045edd
RR
2102 if (window)
2103 gdk_window_set_cursor( window, cursor.GetCursor() );
2104
2105 if (!g_globalCursor.Ok())
2106 cursor = *wxSTANDARD_CURSOR;
2107
2108 window = m_widget->window;
247e5b16 2109 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd 2110 gdk_window_set_cursor( window, cursor.GetCursor() );
fab591c5 2111#endif
65045edd 2112 }
26bf1ce0 2113
d7fa7eaa
RR
2114 if (g_delayedFocus == this)
2115 {
2116 if (GTK_WIDGET_REALIZED(m_widget))
2117 {
2118 gtk_widget_grab_focus( m_widget );
2119 g_delayedFocus = NULL;
2120 }
2121 }
2122
e39af974
JS
2123 if (wxUpdateUIEvent::CanUpdate(this))
2124 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
65045edd 2125}
f68586e5
VZ
2126
2127wxSize wxTextCtrl::DoGetBestSize() const
2128{
2129 // FIXME should be different for multi-line controls...
0279e844 2130 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
2131 wxSize best(80, ret.y);
2132 CacheBestSize(best);
2133 return best;
f68586e5 2134}
0cc7251e 2135
9cd6d737
VZ
2136// ----------------------------------------------------------------------------
2137// freeze/thaw
2138// ----------------------------------------------------------------------------
2139
0cc7251e
VZ
2140void wxTextCtrl::Freeze()
2141{
2142 if ( HasFlag(wxTE_MULTILINE) )
2143 {
41b81aed
RR
2144#ifdef __WXGTK20__
2145 if ( !m_frozenness++ )
2146 {
2147 // freeze textview updates and remove buffer
2148 g_signal_connect( G_OBJECT(m_text), "expose_event",
2149 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2150 g_signal_connect( G_OBJECT(m_widget), "expose_event",
2151 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2152 gtk_widget_set_sensitive(m_widget, false);
2153 g_object_ref(m_buffer);
2154 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
0a164d4c 2155 }
41b81aed
RR
2156#else
2157 gtk_text_freeze(GTK_TEXT(m_text));
fab591c5 2158#endif
41b81aed 2159 }
0cc7251e
VZ
2160}
2161
2162void wxTextCtrl::Thaw()
2163{
2164 if ( HasFlag(wxTE_MULTILINE) )
2165 {
41b81aed
RR
2166#ifdef __WXGTK20__
2167 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2168
2169 if ( !--m_frozenness )
2170 {
2171 // Reattach buffer and thaw textview updates
2172 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
2173 g_object_unref(m_buffer);
2174 gtk_widget_set_sensitive(m_widget, true);
2175 g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this);
2176 g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this);
2177 }
2178#else
817ec43a
VZ
2179 GTK_TEXT(m_text)->vadj->value = 0.0;
2180
0cc7251e 2181 gtk_text_thaw(GTK_TEXT(m_text));
fab591c5 2182#endif
41b81aed 2183 }
0cc7251e 2184}
9cd6d737 2185
9440c3d0
KH
2186// ----------------------------------------------------------------------------
2187// wxTextUrlEvent passing if style & wxTE_AUTO_URL
2188// ----------------------------------------------------------------------------
2189
2190#ifdef __WXGTK20__
2191
2192// FIXME: when dragging on a link the sample gets an "Unknown event".
2193// This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
2194// a buggy sample, or something else
2195void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
2196{
2197 event.Skip();
2198 if(!(m_windowStyle & wxTE_AUTO_URL))
2199 return;
2200
2201 gint x, y;
2202 GtkTextIter start, end;
2203 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
2204 "wxUrl");
2205
2206 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
2207 event.GetX(), event.GetY(), &x, &y);
2208
2209 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
2210 if (!gtk_text_iter_has_tag(&end, tag))
2211 {
2212 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2213 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
2214 return;
2215 }
2216
2217 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2218 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
2219
2220 start = end;
2221 if(!gtk_text_iter_begins_tag(&start, tag))
2222 gtk_text_iter_backward_to_tag_toggle(&start, tag);
2223 if(!gtk_text_iter_ends_tag(&end, tag))
2224 gtk_text_iter_forward_to_tag_toggle(&end, tag);
2225
2226 // Native context menu is probably not desired on an URL.
2227 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
2228 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
2229 event.Skip(false);
2230
2231 wxTextUrlEvent url_event(m_windowId, event,
2232 gtk_text_iter_get_offset(&start),
2233 gtk_text_iter_get_offset(&end));
2234
2235 InitCommandEvent(url_event);
2236 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2237 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
2238 GetEventHandler()->ProcessEvent(url_event);
2239}
2240#endif // gtk2
2241
9cd6d737
VZ
2242// ----------------------------------------------------------------------------
2243// scrolling
2244// ----------------------------------------------------------------------------
2245
2246GtkAdjustment *wxTextCtrl::GetVAdj() const
2247{
1ce52aa6
VZ
2248 if ( !IsMultiLine() )
2249 return NULL;
2250
fab591c5 2251#ifdef __WXGTK20__
1ce52aa6 2252 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
fab591c5 2253#else
1ce52aa6 2254 return GTK_TEXT(m_text)->vadj;
fab591c5 2255#endif
9cd6d737
VZ
2256}
2257
2258bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
2259{
2260 float value = adj->value + diff;
2261
2262 if ( value < 0 )
2263 value = 0;
2264
2265 float upper = adj->upper - adj->page_size;
2266 if ( value > upper )
2267 value = upper;
2268
2269 // did we noticeably change the scroll position?
2270 if ( fabs(adj->value - value) < 0.2 )
2271 {
2272 // well, this is what Robert does in wxScrollBar, so it must be good...
7d8268a1 2273 return false;
9cd6d737
VZ
2274 }
2275
2276 adj->value = value;
2277
1ce52aa6
VZ
2278#ifdef __WXGTK20__
2279 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
2280#else
9cd6d737 2281 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
fab591c5 2282#endif
1ce52aa6 2283
7d8268a1 2284 return true;
9cd6d737
VZ
2285}
2286
2287bool wxTextCtrl::ScrollLines(int lines)
2288{
2289 GtkAdjustment *adj = GetVAdj();
2290 if ( !adj )
7d8268a1 2291 return false;
9cd6d737 2292
1ce52aa6
VZ
2293#ifdef __WXGTK20__
2294 int diff = (int)ceil(lines*adj->step_increment);
2295#else
9cd6d737 2296 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1ce52aa6 2297 int diff = 10*lines;
fab591c5 2298#endif
1ce52aa6
VZ
2299
2300 return DoScroll(adj, diff);
9cd6d737
VZ
2301}
2302
2303bool wxTextCtrl::ScrollPages(int pages)
2304{
2305 GtkAdjustment *adj = GetVAdj();
2306 if ( !adj )
7d8268a1 2307 return false;
9cd6d737 2308
817ec43a 2309 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
9cd6d737
VZ
2310}
2311
9d522606
RD
2312
2313// static
2314wxVisualAttributes
2315wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2316{
2317 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
2318}