]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
remember unknown charsets in the config too
[wxWidgets.git] / src / gtk1 / textctrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
13289f04 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
15#include "wx/utils.h"
ae0bdb01 16#include "wx/intl.h"
a1f79c1e 17#include "wx/log.h"
ae0bdb01 18#include "wx/settings.h"
fc71ef6e 19#include "wx/panel.h"
c801d85f 20
a81258be
RR
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <ctype.h>
817ec43a 24#include <math.h> // for fabs
a81258be 25
83624f79
RR
26#include "gdk/gdk.h"
27#include "gtk/gtk.h"
b292e2f5
RR
28#include "gdk/gdkkeysyms.h"
29
acfd422a
RR
30//-----------------------------------------------------------------------------
31// idle system
32//-----------------------------------------------------------------------------
33
34extern void wxapp_install_idle_handler();
35extern bool g_isIdle;
36
b292e2f5
RR
37//-----------------------------------------------------------------------------
38// data
39//-----------------------------------------------------------------------------
40
65045edd
RR
41extern bool g_blockEventsOnDrag;
42extern wxCursor g_globalCursor;
b292e2f5 43
c801d85f 44//-----------------------------------------------------------------------------
2f2aa628 45// "changed"
c801d85f
KB
46//-----------------------------------------------------------------------------
47
805dd538 48static void
2830bf19 49gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 50{
a2053b27 51 if (!win->m_hasVMT) return;
805dd538 52
bb69661b 53 if (g_isIdle)
3c679789
RR
54 wxapp_install_idle_handler();
55
034be888 56 win->SetModified();
01041145 57 win->UpdateFontIfNeeded();
8bbe427f 58
f03fc89f 59 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
8a85884a 60 event.SetString( win->GetValue() );
2830bf19
RR
61 event.SetEventObject( win );
62 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 63}
112892b9 64
2830bf19 65//-----------------------------------------------------------------------------
034be888 66// "changed" from vertical scrollbar
2830bf19
RR
67//-----------------------------------------------------------------------------
68
805dd538 69static void
034be888 70gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
2830bf19 71{
3c679789 72 if (!win->m_hasVMT) return;
bb69661b
VZ
73
74 if (g_isIdle)
3c679789 75 wxapp_install_idle_handler();
acfd422a 76
2830bf19
RR
77 win->CalculateScrollbar();
78}
79
fc71ef6e
JS
80//-----------------------------------------------------------------------------
81// "focus_in_event"
82//-----------------------------------------------------------------------------
83
fc71ef6e
JS
84extern wxWindow *g_focusWindow;
85extern bool g_blockEventsOnDrag;
86// extern bool g_isIdle;
87
88static gint gtk_text_focus_in_callback( GtkWidget *widget, GdkEvent *WXUNUSED(event), wxWindow *win )
89{
90 // Necessary?
91#if 0
92 if (g_isIdle)
93 wxapp_install_idle_handler();
94#endif
95 if (!win->m_hasVMT) return FALSE;
96 if (g_blockEventsOnDrag) return FALSE;
97
98 g_focusWindow = win;
99
100 wxPanel *panel = wxDynamicCast(win->GetParent(), wxPanel);
101 if (panel)
102 {
103 panel->SetLastFocus(win);
104 }
105
106#ifdef HAVE_XIM
107 if (win->m_ic)
108 gdk_im_begin(win->m_ic, win->m_wxwindow->window);
109#endif
110
111#if 0
112#ifdef wxUSE_CARET
113 // caret needs to be informed about focus change
114 wxCaret *caret = win->GetCaret();
115 if ( caret )
116 {
117 caret->OnSetFocus();
118 }
119#endif // wxUSE_CARET
120#endif
121
122 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
123 event.SetEventObject( win );
124
125 if (win->GetEventHandler()->ProcessEvent( event ))
126 {
fc71ef6e
JS
127 return TRUE;
128 }
129
130 return FALSE;
131}
132
133//-----------------------------------------------------------------------------
134// "focus_out_event"
135//-----------------------------------------------------------------------------
136
137static gint gtk_text_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED(event), wxWindow *win )
138{
139#if 0
140 if (g_isIdle)
141 wxapp_install_idle_handler();
142#endif
143
144 if (!win->m_hasVMT) return FALSE;
145 if (g_blockEventsOnDrag) return FALSE;
146
147#if 0
148 // if the focus goes out of our app alltogether, OnIdle() will send
149 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
150 // g_sendActivateEvent to -1
151 g_sendActivateEvent = 0;
152#endif
153
3379ed37 154 wxWindow *winFocus = wxFindFocusedChild(win);
fc71ef6e
JS
155 if ( winFocus )
156 win = winFocus;
157
158 g_focusWindow = (wxWindow *)NULL;
159
160#ifdef HAVE_XIM
161 if (win->m_ic)
162 gdk_im_end();
163#endif
164
165#if 0
166#ifdef wxUSE_CARET
167 // caret needs to be informed about focus change
168 wxCaret *caret = win->GetCaret();
169 if ( caret )
170 {
171 caret->OnKillFocus();
172 }
173#endif // wxUSE_CARET
174#endif
175
176 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
177 event.SetEventObject( win );
178
179 if (win->GetEventHandler()->ProcessEvent( event ))
180 {
fc71ef6e
JS
181 return TRUE;
182 }
183
184 return FALSE;
185}
186
2f2aa628
RR
187//-----------------------------------------------------------------------------
188// wxTextCtrl
189//-----------------------------------------------------------------------------
190
191IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
192
c801d85f 193BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 194 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
195
196 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
197 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
198 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
199 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
200 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
201
202 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
203 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
204 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
205 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
206 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
c801d85f
KB
207END_EVENT_TABLE()
208
01041145 209void wxTextCtrl::Init()
f5abe911
RR
210{
211 m_modified = FALSE;
01041145 212 m_updateFont = FALSE;
3ca6a5f0
BP
213 m_text =
214 m_vScrollbar = (GtkWidget *)NULL;
f5abe911 215}
13289f04 216
13111b2a
VZ
217wxTextCtrl::wxTextCtrl( wxWindow *parent,
218 wxWindowID id,
219 const wxString &value,
220 const wxPoint &pos,
221 const wxSize &size,
222 long style,
223 const wxValidator& validator,
224 const wxString &name )
f5abe911 225{
01041145
VZ
226 Init();
227
f5abe911
RR
228 Create( parent, id, value, pos, size, style, validator, name );
229}
c801d85f 230
13111b2a
VZ
231bool wxTextCtrl::Create( wxWindow *parent,
232 wxWindowID id,
233 const wxString &value,
234 const wxPoint &pos,
235 const wxSize &size,
236 long style,
237 const wxValidator& validator,
238 const wxString &name )
c801d85f 239{
2830bf19 240 m_needParent = TRUE;
b292e2f5 241 m_acceptsFocus = TRUE;
484e45bf 242
4dcaf11a
RR
243 if (!PreCreation( parent, pos, size ) ||
244 !CreateBase( parent, id, pos, size, style, validator, name ))
245 {
223d09f6 246 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
13111b2a 247 return FALSE;
4dcaf11a 248 }
6de97a3b 249
805dd538 250
034be888 251 m_vScrollbarVisible = FALSE;
13289f04 252
2830bf19 253 bool multi_line = (style & wxTE_MULTILINE) != 0;
ab46dc18 254 if (multi_line)
2830bf19 255 {
7d6d2cd4 256#if (GTK_MINOR_VERSION > 2)
034be888
RR
257 /* a multi-line edit control: create a vertical scrollbar by default and
258 horizontal if requested */
2830bf19 259 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
7d6d2cd4
RR
260#else
261 bool bHasHScrollbar = FALSE;
262#endif
805dd538 263
034be888 264 /* create our control ... */
2830bf19
RR
265 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
266
034be888 267 /* ... and put into the upper left hand corner of the table */
2830bf19 268 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
5664fc32 269 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19 270 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 271 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
272 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
273 0, 0);
bb69661b 274
5c387335
RR
275 /* always wrap words */
276 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
bb69661b 277
7d6d2cd4 278#if (GTK_MINOR_VERSION > 2)
034be888 279 /* put the horizontal scrollbar in the lower left hand corner */
2830bf19
RR
280 if (bHasHScrollbar)
281 {
282 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
5664fc32 283 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
2830bf19 284 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
8ce63e9d 285 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
13289f04
VZ
286 GTK_FILL,
287 0, 0);
2830bf19 288 gtk_widget_show(hscrollbar);
13289f04 289
3358d36e
VZ
290 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
291 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
5c387335 292 }
7d6d2cd4 293#endif
bb69661b 294
ab46dc18
RR
295 /* finally, put the vertical scrollbar in the upper right corner */
296 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
297 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
ab46dc18
RR
298 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
299 GTK_FILL,
300 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
301 0, 0);
2830bf19
RR
302 }
303 else
304 {
034be888 305 /* a single-line text control: no need for scrollbars */
2830bf19
RR
306 m_widget =
307 m_text = gtk_entry_new();
308 }
484e45bf 309
db434467
RR
310 m_parent->DoAddChild( this );
311
312 PostCreation();
313
314 SetFont( parent->GetFont() );
315
316 wxSize size_best( DoGetBestSize() );
317 wxSize new_size( size );
0279e844 318 if (new_size.x == -1)
db434467 319 new_size.x = size_best.x;
0279e844 320 if (new_size.y == -1)
db434467 321 new_size.y = size_best.y;
0279e844
RR
322 if ((new_size.x != size.x) || (new_size.y != size.y))
323 SetSize( new_size.x, new_size.y );
484e45bf 324
2830bf19 325 if (multi_line)
2830bf19 326 gtk_widget_show(m_text);
13289f04 327
ab46dc18
RR
328 if (multi_line)
329 {
330 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
331 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
fc71ef6e
JS
332
333 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text)), "focus_in_event",
334 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback), (gpointer)this );
335
336 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text)), "focus_out_event",
337 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback), (gpointer)this );
338 }
339 else
340 {
341 gtk_signal_connect( GTK_OBJECT(m_text), "focus_in_event",
342 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback), (gpointer)this );
343
344 gtk_signal_connect( GTK_OBJECT(m_text), "focus_out_event",
345 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback), (gpointer)this );
ab46dc18 346 }
3358d36e 347
291a8f20 348 if (!value.IsEmpty())
2830bf19
RR
349 {
350 gint tmp = 0;
3358d36e
VZ
351
352#if GTK_MINOR_VERSION == 0
353 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
354 // gtk_editable_insert_text()
355 gtk_widget_realize(m_text);
356#endif // GTK 1.0
357
05939a81 358#if wxUSE_UNICODE
3358d36e 359 wxWX2MBbuf val = value.mbc_str();
05939a81 360 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
3358d36e 361#else // !Unicode
2830bf19 362 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
3358d36e
VZ
363#endif // Unicode/!Unicode
364
291a8f20
RR
365 if (multi_line)
366 {
3358d36e
VZ
367 /* bring editable's cursor uptodate. bug in GTK. */
368
369 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
370 }
2830bf19 371 }
484e45bf 372
2830bf19
RR
373 if (style & wxTE_PASSWORD)
374 {
375 if (!multi_line)
376 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
377 }
8bbe427f 378
2830bf19
RR
379 if (style & wxTE_READONLY)
380 {
381 if (!multi_line)
382 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
383 }
384 else
385 {
386 if (multi_line)
387 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
388 }
3358d36e 389
ce16e5d7
RR
390 /* we want to be notified about text changes */
391 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
392 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
393
a88acabd
JS
394 /* we don't set a valid background colour, because the window
395 manager should use a default one */
396 m_backgroundColour = wxColour();
17665a2b
VZ
397
398 wxColour colFg = parent->GetForegroundColour();
399 SetForegroundColour( colFg );
805dd538 400
65045edd 401 m_cursor = wxCursor( wxCURSOR_IBEAM );
13111b2a 402
17665a2b
VZ
403 // FIXME: is the bg colour correct here?
404 wxTextAttr attrDef( colFg,
405 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW),
406 parent->GetFont() );
407 SetDefaultStyle( attrDef );
408
2830bf19 409 Show( TRUE );
805dd538 410
2830bf19
RR
411 return TRUE;
412}
484e45bf 413
2830bf19
RR
414void wxTextCtrl::CalculateScrollbar()
415{
416 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
f96aa4d9 417
2830bf19 418 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
805dd538 419
2830bf19
RR
420 if (adj->upper - adj->page_size < 0.8)
421 {
422 if (m_vScrollbarVisible)
423 {
034be888 424 gtk_widget_hide( m_vScrollbar );
034be888 425 m_vScrollbarVisible = FALSE;
2830bf19
RR
426 }
427 }
428 else
429 {
430 if (!m_vScrollbarVisible)
431 {
034be888 432 gtk_widget_show( m_vScrollbar );
034be888 433 m_vScrollbarVisible = TRUE;
2830bf19
RR
434 }
435 }
6de97a3b 436}
c801d85f 437
03f38c58 438wxString wxTextCtrl::GetValue() const
c801d85f 439{
223d09f6 440 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
8bbe427f 441
2830bf19
RR
442 wxString tmp;
443 if (m_windowStyle & wxTE_MULTILINE)
444 {
445 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
446 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
dcf924a3 447 tmp = wxString(text,*wxConvCurrent);
2830bf19
RR
448 g_free( text );
449 }
450 else
451 {
dcf924a3 452 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
2830bf19
RR
453 }
454 return tmp;
6de97a3b 455}
c801d85f
KB
456
457void wxTextCtrl::SetValue( const wxString &value )
458{
223d09f6 459 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 460
2830bf19
RR
461 if (m_windowStyle & wxTE_MULTILINE)
462 {
463 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
464 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
465 len = 0;
05939a81 466#if wxUSE_UNICODE
01041145 467 wxWX2MBbuf tmpbuf = value.mbc_str();
05939a81
OK
468 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
469#else
01041145 470 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
05939a81 471#endif
2830bf19
RR
472 }
473 else
474 {
01041145 475 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
2830bf19 476 }
f6bcfd97
BP
477
478 // GRG, Jun/2000: Changed this after a lot of discussion in
479 // the lists. wxWindows 2.2 will have a set of flags to
480 // customize this behaviour.
481 SetInsertionPoint(0);
482
483 m_modified = FALSE;
6de97a3b 484}
c801d85f
KB
485
486void wxTextCtrl::WriteText( const wxString &text )
487{
223d09f6 488 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 489
17665a2b
VZ
490 if ( text.empty() )
491 return;
484e45bf 492
17665a2b
VZ
493#if wxUSE_UNICODE
494 wxWX2MBbuf buf = text.mbc_str();
495 const char *txt = buf;
496 size_t txtlen = strlen(buf);
497#else
498 const char *txt = text;
499 size_t txtlen = text.length();
500#endif
501
502 if ( m_windowStyle & wxTE_MULTILINE )
2830bf19 503 {
291a8f20 504 /* this moves the cursor pos to behind the inserted text */
3358d36e 505 gint len = GTK_EDITABLE(m_text)->current_pos;
13111b2a 506
17665a2b
VZ
507 // if we have any special style, use it
508 if ( !m_defaultStyle.IsDefault() )
509 {
510 GdkFont *font = m_defaultStyle.HasFont()
511 ? m_defaultStyle.GetFont().GetInternalFont()
512 : NULL;
513
514 GdkColor *colFg = m_defaultStyle.HasTextColour()
515 ? m_defaultStyle.GetTextColour().GetColor()
516 : NULL;
517
518 GdkColor *colBg = m_defaultStyle.HasBackgroundColour()
519 ? m_defaultStyle.GetBackgroundColour().GetColor()
520 : NULL;
521
522 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
523 }
524 else // no style
525 {
526 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
527 }
3358d36e
VZ
528
529 /* bring editable's cursor uptodate. bug in GTK. */
530 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
2830bf19 531 }
17665a2b 532 else // single line
2830bf19 533 {
c46554be 534 /* this moves the cursor pos to behind the inserted text */
3358d36e 535 gint len = GTK_EDITABLE(m_text)->current_pos;
17665a2b 536 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
3358d36e
VZ
537
538 /* bring editable's cursor uptodate. bug in GTK. */
539 GTK_EDITABLE(m_text)->current_pos += text.Len();
540
541 /* bring entry's cursor uptodate. bug in GTK. */
542 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
2830bf19 543 }
6de97a3b 544}
c801d85f 545
a6e21573
HH
546void wxTextCtrl::AppendText( const wxString &text )
547{
223d09f6 548 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
a6e21573 549
17665a2b
VZ
550 if ( text.empty() )
551 return;
552
553#if wxUSE_UNICODE
554 wxWX2MBbuf buf = text.mbc_str();
555 const char *txt = buf;
556 size_t txtlen = strlen(buf);
557#else
558 const char *txt = text;
559 size_t txtlen = text.length();
560#endif
2df7be7f 561
a6e21573
HH
562 if (m_windowStyle & wxTE_MULTILINE)
563 {
17665a2b 564 if ( !m_defaultStyle.IsDefault() )
bb69661b 565 {
1ff4714d
VZ
566 wxFont font = m_defaultStyle.HasFont() ? m_defaultStyle.GetFont()
567 : m_font;
568 GdkFont *fnt = font.Ok() ? font.GetInternalFont() : NULL;
17665a2b 569
1ff4714d
VZ
570 wxColour col = m_defaultStyle.HasTextColour()
571 ? m_defaultStyle.GetTextColour()
572 : m_foregroundColour;
573 GdkColor *colFg = col.Ok() ? col.GetColor() : NULL;
bb69661b 574
1ff4714d
VZ
575 col = m_defaultStyle.HasBackgroundColour()
576 ? m_defaultStyle.GetBackgroundColour()
577 : m_backgroundColour;
578 GdkColor *colBg = col.Ok() ? col.GetColor() : NULL;
17665a2b 579
1ff4714d 580 gtk_text_insert( GTK_TEXT(m_text), fnt, colFg, colBg, txt, txtlen );
bb69661b 581 }
17665a2b 582 else // no style
bb69661b
VZ
583 {
584 /* we'll insert at the last position */
585 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
17665a2b 586 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
bb69661b 587 }
3358d36e
VZ
588
589 /* bring editable's cursor uptodate. bug in GTK. */
590 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
a6e21573 591 }
17665a2b 592 else // single line
a6e21573 593 {
17665a2b 594 gtk_entry_append_text( GTK_ENTRY(m_text), txt );
a6e21573
HH
595 }
596}
597
debe6624 598wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f 599{
a81258be
RR
600 if (m_windowStyle & wxTE_MULTILINE)
601 {
602 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
603 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
604
605 if (text)
606 {
223d09f6 607 wxString buf(wxT(""));
a81258be
RR
608 long i;
609 int currentLine = 0;
610 for (i = 0; currentLine != lineNo && text[i]; i++ )
611 if (text[i] == '\n')
612 currentLine++;
613 // Now get the text
614 int j;
615 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
616 buf += text[i];
8bbe427f 617
a81258be
RR
618 g_free( text );
619 return buf;
620 }
621 else
622 return wxEmptyString;
623 }
624 else
625 {
626 if (lineNo == 0) return GetValue();
627 return wxEmptyString;
628 }
6de97a3b 629}
c801d85f 630
a81258be
RR
631void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
632{
ac0d36b5
HH
633 /* If you implement this, don't forget to update the documentation!
634 * (file docs/latex/wx/text.tex) */
223d09f6 635 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 636}
112892b9 637
0efe5ba7 638bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 639{
96385642 640 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 641 {
96385642
VZ
642 wxString text = GetValue();
643
6085b116 644 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 645 if( (unsigned long)pos > text.Len() )
96385642
VZ
646 return FALSE;
647
ac0d36b5
HH
648 *x=0; // First Col
649 *y=0; // First Line
2829d9e3 650
05939a81
OK
651 const wxChar* stop = text.c_str() + pos;
652 for ( const wxChar *p = text.c_str(); p < stop; p++ )
96385642 653 {
223d09f6 654 if (*p == wxT('\n'))
96385642
VZ
655 {
656 (*y)++;
ac0d36b5 657 *x=0;
96385642
VZ
658 }
659 else
660 (*x)++;
661 }
805dd538 662 }
96385642
VZ
663 else // single line control
664 {
2829d9e3 665 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 666 {
ac0d36b5 667 *y = 0;
96385642
VZ
668 *x = pos;
669 }
670 else
671 {
672 // index out of bounds
673 return FALSE;
674 }
8bbe427f 675 }
96385642
VZ
676
677 return TRUE;
6de97a3b 678}
c801d85f 679
e3ca08dd 680long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 681{
2830bf19 682 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 683
2830bf19 684 long pos=0;
ac0d36b5 685 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 686
3358d36e 687 pos += x;
2830bf19 688 return pos;
6de97a3b 689}
c801d85f 690
a81258be 691int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 692{
a81258be
RR
693 wxString str = GetLineText (lineNo);
694 return (int) str.Length();
6de97a3b 695}
c801d85f 696
a81258be 697int wxTextCtrl::GetNumberOfLines() const
c801d85f 698{
2830bf19 699 if (m_windowStyle & wxTE_MULTILINE)
a81258be 700 {
2830bf19
RR
701 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
702 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
703
704 if (text)
705 {
706 int currentLine = 0;
707 for (int i = 0; i < len; i++ )
96385642 708 {
2830bf19 709 if (text[i] == '\n')
96385642
VZ
710 currentLine++;
711 }
2830bf19 712 g_free( text );
2829d9e3
VZ
713
714 // currentLine is 0 based, add 1 to get number of lines
715 return currentLine + 1;
2830bf19
RR
716 }
717 else
96385642 718 {
2830bf19 719 return 0;
96385642 720 }
a81258be
RR
721 }
722 else
2830bf19 723 {
96385642 724 return 1;
2830bf19 725 }
6de97a3b 726}
c801d85f 727
debe6624 728void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 729{
223d09f6 730 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e
VZ
731
732 if (m_windowStyle & wxTE_MULTILINE)
291a8f20
RR
733 {
734 /* seems to be broken in GTK 1.0.X:
3358d36e
VZ
735 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
736
291a8f20
RR
737 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
738 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 739
291a8f20 740 /* we fake a set_point by inserting and deleting. as the user
3358d36e
VZ
741 isn't supposed to get to know about thos non-sense, we
742 disconnect so that no events are sent to the user program. */
743
291a8f20
RR
744 gint tmp = (gint)pos;
745 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
746 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
747
291a8f20
RR
748 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
749 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e
VZ
750
751 /* bring editable's cursor uptodate. another bug in GTK. */
752
753 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
ac0d36b5 754 }
2830bf19 755 else
291a8f20 756 {
d59051dd 757 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e
VZ
758
759 /* bring editable's cursor uptodate. bug in GTK. */
760
b02da6b1 761 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
291a8f20 762 }
6de97a3b 763}
c801d85f 764
03f38c58 765void wxTextCtrl::SetInsertionPointEnd()
c801d85f 766{
223d09f6 767 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 768
d59051dd
VZ
769 if (m_windowStyle & wxTE_MULTILINE)
770 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
771 else
772 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
6de97a3b 773}
c801d85f 774
debe6624 775void wxTextCtrl::SetEditable( bool editable )
c801d85f 776{
223d09f6 777 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 778
2830bf19
RR
779 if (m_windowStyle & wxTE_MULTILINE)
780 gtk_text_set_editable( GTK_TEXT(m_text), editable );
781 else
782 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
6de97a3b 783}
c801d85f 784
68df5777
RR
785bool wxTextCtrl::Enable( bool enable )
786{
787 if (!wxWindowBase::Enable(enable))
788 {
789 // nothing to do
790 return FALSE;
791 }
f6bcfd97 792
68df5777
RR
793 if (m_windowStyle & wxTE_MULTILINE)
794 {
795 gtk_text_set_editable( GTK_TEXT(m_text), enable );
5abf8c9d 796 OnParentEnable(enable);
68df5777
RR
797 }
798 else
799 {
800 gtk_widget_set_sensitive( m_text, enable );
801 }
802
803 return TRUE;
804}
805
fdca68a6
JS
806// wxGTK-specific: called recursively by Enable,
807// to give widgets an oppprtunity to correct their colours after they
808// have been changed by Enable
809void wxTextCtrl::OnParentEnable( bool enable )
810{
811 // If we have a custom background colour, we use this colour in both
812 // disabled and enabled mode, or we end up with a different colour under the
813 // text.
814 wxColour oldColour = GetBackgroundColour();
815 if (oldColour.Ok())
816 {
817 // Need to set twice or it'll optimize the useful stuff out
818 if (oldColour == * wxWHITE)
819 SetBackgroundColour(*wxBLACK);
820 else
821 SetBackgroundColour(*wxWHITE);
822 SetBackgroundColour(oldColour);
823 }
824}
825
0efe5ba7
VZ
826void wxTextCtrl::DiscardEdits()
827{
828 m_modified = FALSE;
829}
830
debe6624 831void wxTextCtrl::SetSelection( long from, long to )
c801d85f 832{
223d09f6 833 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 834
a1f79c1e
VZ
835 if ( (m_windowStyle & wxTE_MULTILINE) &&
836 !GTK_TEXT(m_text)->line_start_cache )
837 {
838 // tell the programmer that it didn't work
839 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
840 return;
841 }
842
2830bf19 843 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 844}
c801d85f 845
910f1f8c 846void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 847{
910f1f8c 848// SetInsertionPoint( pos );
6de97a3b 849}
c801d85f 850
03f38c58 851long wxTextCtrl::GetInsertionPoint() const
c801d85f 852{
223d09f6 853 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 854
2830bf19 855 return (long) GTK_EDITABLE(m_text)->current_pos;
6de97a3b 856}
c801d85f 857
03f38c58 858long wxTextCtrl::GetLastPosition() const
c801d85f 859{
223d09f6 860 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 861
2830bf19
RR
862 int pos = 0;
863 if (m_windowStyle & wxTE_MULTILINE)
864 pos = gtk_text_get_length( GTK_TEXT(m_text) );
865 else
866 pos = GTK_ENTRY(m_text)->text_length;
805dd538 867
ac0d36b5 868 return (long)pos;
6de97a3b 869}
c801d85f 870
debe6624 871void wxTextCtrl::Remove( long from, long to )
c801d85f 872{
223d09f6 873 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 874
2830bf19 875 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 876}
c801d85f 877
debe6624 878void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 879{
223d09f6 880 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 881
2830bf19 882 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
bb69661b 883
2df7be7f
RR
884 if (!value.IsEmpty())
885 {
886 gint pos = (gint)from;
05939a81 887#if wxUSE_UNICODE
2df7be7f
RR
888 wxWX2MBbuf buf = value.mbc_str();
889 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 890#else
2df7be7f 891 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
05939a81 892#endif
2df7be7f 893 }
6de97a3b 894}
c801d85f 895
03f38c58 896void wxTextCtrl::Cut()
c801d85f 897{
223d09f6 898 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 899
d345e841 900#if (GTK_MINOR_VERSION > 0)
2830bf19 901 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 902#else
2830bf19 903 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 904#endif
6de97a3b 905}
c801d85f 906
03f38c58 907void wxTextCtrl::Copy()
c801d85f 908{
223d09f6 909 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 910
d345e841 911#if (GTK_MINOR_VERSION > 0)
2830bf19 912 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 913#else
2830bf19 914 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 915#endif
6de97a3b 916}
c801d85f 917
03f38c58 918void wxTextCtrl::Paste()
c801d85f 919{
223d09f6 920 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 921
d345e841 922#if (GTK_MINOR_VERSION > 0)
2830bf19 923 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 924#else
2830bf19 925 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 926#endif
6de97a3b 927}
c801d85f 928
ca8b28f2
JS
929// Undo/redo
930void wxTextCtrl::Undo()
931{
932 // TODO
223d09f6 933 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
934}
935
936void wxTextCtrl::Redo()
937{
938 // TODO
223d09f6 939 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
940}
941
942bool wxTextCtrl::CanUndo() const
943{
944 // TODO
223d09f6 945 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
ca8b28f2
JS
946 return FALSE;
947}
948
949bool wxTextCtrl::CanRedo() const
950{
951 // TODO
223d09f6 952 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
ca8b28f2
JS
953 return FALSE;
954}
955
956// If the return values from and to are the same, there is no
957// selection.
2d4cc5b6 958void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
ca8b28f2 959{
223d09f6 960 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 961
2d4cc5b6
VZ
962 long from, to;
963 if ( !(GTK_EDITABLE(m_text)->has_selection) )
05060eeb 964 {
2d4cc5b6
VZ
965 from =
966 to = GetInsertionPoint();
967 }
968 else // got selection
969 {
970 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
971 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
972
973 if ( from > to )
974 {
975 // exchange them to be compatible with wxMSW
976 long tmp = from;
977 from = to;
978 to = tmp;
979 }
05060eeb 980 }
bb69661b 981
2d4cc5b6
VZ
982 if ( fromOut )
983 *fromOut = from;
984 if ( toOut )
985 *toOut = to;
ca8b28f2
JS
986}
987
988bool wxTextCtrl::IsEditable() const
989{
223d09f6 990 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
05060eeb
RR
991
992 return GTK_EDITABLE(m_text)->editable;
ca8b28f2
JS
993}
994
0efe5ba7
VZ
995bool wxTextCtrl::IsModified() const
996{
997 return m_modified;
998}
999
03f38c58 1000void wxTextCtrl::Clear()
c801d85f 1001{
223d09f6 1002 SetValue( wxT("") );
6de97a3b 1003}
c801d85f 1004
903f689b 1005void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 1006{
223d09f6 1007 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 1008
2830bf19
RR
1009 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1010 {
1011 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1012 event.SetEventObject(this);
f6bcfd97 1013 event.SetString(GetValue());
2830bf19
RR
1014 if (GetEventHandler()->ProcessEvent(event)) return;
1015 }
903f689b 1016
da048e3d
RR
1017 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1018 {
1019 wxWindow *top_frame = m_parent;
8487f887 1020 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 1021 top_frame = top_frame->GetParent();
13111b2a
VZ
1022 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1023
1024 if (window->default_widget)
da048e3d
RR
1025 {
1026 gtk_widget_activate (window->default_widget);
13111b2a
VZ
1027 return;
1028 }
da048e3d
RR
1029 }
1030
2830bf19 1031 key_event.Skip();
6de97a3b 1032}
c801d85f 1033
03f38c58 1034GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 1035{
ae0bdb01 1036 return GTK_WIDGET(m_text);
6de97a3b 1037}
e3e65dac 1038
903f689b
RR
1039bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1040{
ae0bdb01
RR
1041 if (m_windowStyle & wxTE_MULTILINE)
1042 return (window == GTK_TEXT(m_text)->text_area);
1043 else
1044 return (window == GTK_ENTRY(m_text)->text_area);
903f689b 1045}
e3e65dac 1046
bb69661b
VZ
1047// the font will change for subsequent text insertiongs
1048bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 1049{
223d09f6 1050 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 1051
bb69661b
VZ
1052 if ( !wxWindowBase::SetFont(font) )
1053 {
1054 // font didn't change, nothing to do
1055 return FALSE;
1056 }
1057
1058 if ( m_windowStyle & wxTE_MULTILINE )
1059 {
01041145 1060 m_updateFont = TRUE;
bb69661b 1061
1ff4714d
VZ
1062 m_defaultStyle.SetFont(font);
1063
01041145 1064 ChangeFontGlobally();
bb69661b
VZ
1065 }
1066
1067 return TRUE;
58614078
RR
1068}
1069
01041145
VZ
1070void wxTextCtrl::ChangeFontGlobally()
1071{
1072 // this method is very inefficient and hence should be called as rarely as
1073 // possible!
1074 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1075 _T("shouldn't be called for single line controls") );
1076
1077 wxString value = GetValue();
1078 if ( !value.IsEmpty() )
1079 {
1080 Clear();
1081 AppendText(value);
1082
1083 m_updateFont = FALSE;
1084 }
1085}
1086
1087void wxTextCtrl::UpdateFontIfNeeded()
1088{
1089 if ( m_updateFont )
1090 ChangeFontGlobally();
1091}
1092
17665a2b
VZ
1093bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1094{
1095 if ( !wxControl::SetForegroundColour(colour) )
1096 return FALSE;
1097
1098 // update default fg colour too
1099 m_defaultStyle.SetTextColour(colour);
1100
1101 return TRUE;
1102}
1103
f03fc89f 1104bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1105{
223d09f6 1106 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
a81258be 1107
1f477433
VZ
1108 if ( !wxControl::SetBackgroundColour( colour ) )
1109 return FALSE;
3358d36e 1110
f03fc89f
VZ
1111 if (!m_widget->window)
1112 return FALSE;
8bbe427f 1113
ae0bdb01 1114 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
805dd538
VZ
1115 if (sysbg.Red() == colour.Red() &&
1116 sysbg.Green() == colour.Green() &&
ae0bdb01
RR
1117 sysbg.Blue() == colour.Blue())
1118 {
f03fc89f 1119 return FALSE; // FIXME or TRUE?
805dd538
VZ
1120 }
1121
f03fc89f
VZ
1122 if (!m_backgroundColour.Ok())
1123 return FALSE;
8bbe427f 1124
ae0bdb01
RR
1125 if (m_windowStyle & wxTE_MULTILINE)
1126 {
1127 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f
VZ
1128 if (!window)
1129 return FALSE;
ae0bdb01
RR
1130 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1131 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1132 gdk_window_clear( window );
1133 }
f03fc89f 1134
17665a2b
VZ
1135 // change active background color too
1136 m_defaultStyle.SetBackgroundColour( colour );
1137
f03fc89f 1138 return TRUE;
58614078
RR
1139}
1140
17665a2b
VZ
1141bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr &style )
1142{
1143 /* VERY dirty way to do that - removes the required text and re-adds it
1144 with styling (FIXME) */
1145 if ( m_windowStyle & wxTE_MULTILINE )
1146 {
1147 if ( style.IsDefault() )
1148 {
1149 // nothing to do
1150 return TRUE;
1151 }
1152
1153 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1154
1155 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1156 _T("invalid range in wxTextCtrl::SetStyle") );
1157
1158 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1159 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1160 wxString tmp(text,*wxConvCurrent);
1161 g_free( text );
1162
1163 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1164 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1165
1166#if wxUSE_UNICODE
1167 wxWX2MBbuf buf = tmp.mbc_str();
1168 const char *txt = buf;
1169 size_t txtlen = strlen(buf);
1170#else
1171 const char *txt = tmp;
1172 size_t txtlen = tmp.length();
1173#endif
1174
1175 GdkFont *font = style.HasFont()
1176 ? style.GetFont().GetInternalFont()
1177 : NULL;
1178
1179 GdkColor *colFg = style.HasTextColour()
1180 ? style.GetTextColour().GetColor()
1181 : NULL;
1182
1183 GdkColor *colBg = style.HasBackgroundColour()
1184 ? style.GetBackgroundColour().GetColor()
1185 : NULL;
1186
1187 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
1188
1189 /* does not seem to help under GTK+ 1.2 !!!
1190 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1191 SetInsertionPoint( old_pos );
1192 return TRUE;
1193 }
1194 else // singe line
1195 {
1196 // cannot do this for GTK+'s Entry widget
1197 return FALSE;
1198 }
1199}
1200
58614078
RR
1201void wxTextCtrl::ApplyWidgetStyle()
1202{
ae0bdb01
RR
1203 if (m_windowStyle & wxTE_MULTILINE)
1204 {
2830bf19 1205 // how ?
805dd538 1206 }
ae0bdb01
RR
1207 else
1208 {
1209 SetWidgetStyle();
1210 gtk_widget_set_style( m_text, m_widgetStyle );
1211 }
68dda785 1212}
f96aa4d9 1213
e702ff0f
JS
1214void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1215{
1216 Cut();
1217}
1218
1219void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1220{
1221 Copy();
1222}
1223
1224void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1225{
1226 Paste();
1227}
1228
1229void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1230{
1231 Undo();
1232}
1233
1234void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1235{
1236 Redo();
1237}
1238
1239void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1240{
1241 event.Enable( CanCut() );
1242}
1243
1244void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1245{
1246 event.Enable( CanCopy() );
1247}
1248
1249void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1250{
1251 event.Enable( CanPaste() );
1252}
1253
1254void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1255{
1256 event.Enable( CanUndo() );
1257}
1258
1259void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1260{
1261 event.Enable( CanRedo() );
1262}
65045edd
RR
1263
1264void wxTextCtrl::OnInternalIdle()
1265{
1266 wxCursor cursor = m_cursor;
1267 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1268
307f16e8 1269 if (cursor.Ok())
65045edd 1270 {
65045edd 1271 GdkWindow *window = (GdkWindow*) NULL;
13111b2a 1272 if (HasFlag(wxTE_MULTILINE))
65045edd
RR
1273 window = GTK_TEXT(m_text)->text_area;
1274 else
1275 window = GTK_ENTRY(m_text)->text_area;
13111b2a 1276
65045edd
RR
1277 if (window)
1278 gdk_window_set_cursor( window, cursor.GetCursor() );
1279
1280 if (!g_globalCursor.Ok())
1281 cursor = *wxSTANDARD_CURSOR;
1282
1283 window = m_widget->window;
247e5b16 1284 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd
RR
1285 gdk_window_set_cursor( window, cursor.GetCursor() );
1286 }
26bf1ce0
VZ
1287
1288 UpdateWindowUI();
65045edd 1289}
f68586e5
VZ
1290
1291wxSize wxTextCtrl::DoGetBestSize() const
1292{
1293 // FIXME should be different for multi-line controls...
0279e844
RR
1294 wxSize ret( wxControl::DoGetBestSize() );
1295 return wxSize(80, ret.y);
f68586e5 1296}
0cc7251e 1297
9cd6d737
VZ
1298// ----------------------------------------------------------------------------
1299// freeze/thaw
1300// ----------------------------------------------------------------------------
1301
0cc7251e
VZ
1302void wxTextCtrl::Freeze()
1303{
1304 if ( HasFlag(wxTE_MULTILINE) )
1305 {
1306 gtk_text_freeze(GTK_TEXT(m_text));
1307 }
1308}
1309
1310void wxTextCtrl::Thaw()
1311{
1312 if ( HasFlag(wxTE_MULTILINE) )
1313 {
817ec43a
VZ
1314 GTK_TEXT(m_text)->vadj->value = 0.0;
1315
0cc7251e
VZ
1316 gtk_text_thaw(GTK_TEXT(m_text));
1317 }
1318}
9cd6d737
VZ
1319
1320// ----------------------------------------------------------------------------
1321// scrolling
1322// ----------------------------------------------------------------------------
1323
1324GtkAdjustment *wxTextCtrl::GetVAdj() const
1325{
1326 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1327}
1328
1329bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1330{
1331 float value = adj->value + diff;
1332
1333 if ( value < 0 )
1334 value = 0;
1335
1336 float upper = adj->upper - adj->page_size;
1337 if ( value > upper )
1338 value = upper;
1339
1340 // did we noticeably change the scroll position?
1341 if ( fabs(adj->value - value) < 0.2 )
1342 {
1343 // well, this is what Robert does in wxScrollBar, so it must be good...
1344 return FALSE;
1345 }
1346
1347 adj->value = value;
1348
1349 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1350
1351 return TRUE;
1352}
1353
1354bool wxTextCtrl::ScrollLines(int lines)
1355{
1356 GtkAdjustment *adj = GetVAdj();
1357 if ( !adj )
1358 return FALSE;
1359
1360 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1361 static const int KEY_SCROLL_PIXELS = 10;
1362
1363 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1364}
1365
1366bool wxTextCtrl::ScrollPages(int pages)
1367{
1368 GtkAdjustment *adj = GetVAdj();
1369 if ( !adj )
1370 return FALSE;
1371
817ec43a 1372 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
9cd6d737
VZ
1373}
1374