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