]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
pasting text from xterm works, finally
[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
RR
16#include "wx/intl.h"
17#include "wx/settings.h"
c801d85f 18
a81258be
RR
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <ctype.h>
22
83624f79
RR
23#include "gdk/gdk.h"
24#include "gtk/gtk.h"
b292e2f5
RR
25#include "gdk/gdkkeysyms.h"
26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
b292e2f5
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
65045edd
RR
38extern bool g_blockEventsOnDrag;
39extern wxCursor g_globalCursor;
b292e2f5 40
c801d85f 41//-----------------------------------------------------------------------------
2f2aa628 42// "changed"
c801d85f
KB
43//-----------------------------------------------------------------------------
44
805dd538 45static void
2830bf19 46gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 47{
a2053b27 48 if (!win->m_hasVMT) return;
805dd538 49
bb69661b 50 if (g_isIdle)
3c679789
RR
51 wxapp_install_idle_handler();
52
034be888 53 win->SetModified();
8bbe427f 54
f03fc89f 55 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
8a85884a 56 event.SetString( win->GetValue() );
2830bf19
RR
57 event.SetEventObject( win );
58 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 59}
112892b9 60
2830bf19 61//-----------------------------------------------------------------------------
034be888 62// "changed" from vertical scrollbar
2830bf19
RR
63//-----------------------------------------------------------------------------
64
805dd538 65static void
034be888 66gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
2830bf19 67{
3c679789 68 if (!win->m_hasVMT) return;
bb69661b
VZ
69
70 if (g_isIdle)
3c679789 71 wxapp_install_idle_handler();
acfd422a 72
2830bf19
RR
73 win->CalculateScrollbar();
74}
75
2f2aa628
RR
76//-----------------------------------------------------------------------------
77// wxTextCtrl
78//-----------------------------------------------------------------------------
79
80IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
81
c801d85f 82BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
2830bf19 83 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
84
85 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
86 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
87 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
88 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
89 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
90
91 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
92 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
93 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
94 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
95 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
c801d85f
KB
96END_EVENT_TABLE()
97
f5abe911
RR
98wxTextCtrl::wxTextCtrl()
99{
100 m_modified = FALSE;
101}
13289f04 102
f5abe911
RR
103wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
104 const wxPoint &pos, const wxSize &size,
105 int style, const wxValidator& validator, const wxString &name )
106{
107 m_modified = FALSE;
108 Create( parent, id, value, pos, size, style, validator, name );
109}
c801d85f 110
debe6624 111bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 112 const wxPoint &pos, const wxSize &size,
6de97a3b 113 int style, const wxValidator& validator, const wxString &name )
c801d85f 114{
2830bf19 115 m_needParent = TRUE;
b292e2f5 116 m_acceptsFocus = TRUE;
484e45bf 117
4dcaf11a
RR
118 if (!PreCreation( parent, pos, size ) ||
119 !CreateBase( parent, id, pos, size, style, validator, name ))
120 {
223d09f6 121 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
4dcaf11a
RR
122 return FALSE;
123 }
6de97a3b 124
805dd538 125
034be888 126 m_vScrollbarVisible = FALSE;
13289f04 127
2830bf19 128 bool multi_line = (style & wxTE_MULTILINE) != 0;
ab46dc18 129 if (multi_line)
2830bf19 130 {
7d6d2cd4 131#if (GTK_MINOR_VERSION > 2)
034be888
RR
132 /* a multi-line edit control: create a vertical scrollbar by default and
133 horizontal if requested */
2830bf19 134 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
7d6d2cd4
RR
135#else
136 bool bHasHScrollbar = FALSE;
137#endif
805dd538 138
034be888 139 /* create our control ... */
2830bf19
RR
140 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
141
034be888 142 /* ... and put into the upper left hand corner of the table */
2830bf19 143 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
5664fc32 144 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2830bf19 145 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
41dee9d0 146 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
f5368809
RR
147 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
148 0, 0);
bb69661b 149
5c387335
RR
150 /* always wrap words */
151 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
bb69661b 152
7d6d2cd4 153#if (GTK_MINOR_VERSION > 2)
034be888 154 /* put the horizontal scrollbar in the lower left hand corner */
2830bf19
RR
155 if (bHasHScrollbar)
156 {
157 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
5664fc32 158 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
2830bf19 159 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
8ce63e9d 160 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
13289f04
VZ
161 GTK_FILL,
162 0, 0);
2830bf19 163 gtk_widget_show(hscrollbar);
13289f04 164
3358d36e
VZ
165 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
166 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
5c387335 167 }
7d6d2cd4 168#endif
bb69661b 169
ab46dc18
RR
170 /* finally, put the vertical scrollbar in the upper right corner */
171 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
172 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
ab46dc18
RR
173 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
174 GTK_FILL,
175 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
176 0, 0);
2830bf19
RR
177 }
178 else
179 {
034be888 180 /* a single-line text control: no need for scrollbars */
2830bf19
RR
181 m_widget =
182 m_text = gtk_entry_new();
183 }
484e45bf 184
2830bf19
RR
185 wxSize newSize = size;
186 if (newSize.x == -1) newSize.x = 80;
187 if (newSize.y == -1) newSize.y = 26;
188 SetSize( newSize.x, newSize.y );
484e45bf 189
f03fc89f 190 m_parent->DoAddChild( this );
8bbe427f 191
2830bf19 192 PostCreation();
484e45bf 193
2830bf19 194 if (multi_line)
2830bf19 195 gtk_widget_show(m_text);
13289f04 196
034be888 197 /* we want to be notified about text changes */
b292e2f5
RR
198 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
199 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
484e45bf 200
ab46dc18
RR
201 if (multi_line)
202 {
203 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
204 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
205 }
3358d36e 206
291a8f20 207 if (!value.IsEmpty())
2830bf19
RR
208 {
209 gint tmp = 0;
3358d36e
VZ
210
211#if GTK_MINOR_VERSION == 0
212 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
213 // gtk_editable_insert_text()
214 gtk_widget_realize(m_text);
215#endif // GTK 1.0
216
05939a81 217#if wxUSE_UNICODE
3358d36e 218 wxWX2MBbuf val = value.mbc_str();
05939a81 219 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
3358d36e 220#else // !Unicode
2830bf19 221 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
3358d36e
VZ
222#endif // Unicode/!Unicode
223
291a8f20
RR
224 if (multi_line)
225 {
3358d36e
VZ
226 /* bring editable's cursor uptodate. bug in GTK. */
227
228 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
229 }
2830bf19 230 }
484e45bf 231
2830bf19
RR
232 if (style & wxTE_PASSWORD)
233 {
234 if (!multi_line)
235 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
236 }
8bbe427f 237
2830bf19
RR
238 if (style & wxTE_READONLY)
239 {
240 if (!multi_line)
241 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
242 }
243 else
244 {
245 if (multi_line)
246 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
247 }
3358d36e 248
012a03e0 249 SetBackgroundColour( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) );
034be888 250 SetForegroundColour( parent->GetForegroundColour() );
805dd538 251
65045edd
RR
252 m_cursor = wxCursor( wxCURSOR_IBEAM );
253
2830bf19 254 Show( TRUE );
805dd538 255
2830bf19
RR
256 return TRUE;
257}
484e45bf 258
2830bf19
RR
259void wxTextCtrl::CalculateScrollbar()
260{
261 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
f96aa4d9 262
2830bf19 263 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
805dd538 264
2830bf19
RR
265 if (adj->upper - adj->page_size < 0.8)
266 {
267 if (m_vScrollbarVisible)
268 {
034be888 269 gtk_widget_hide( m_vScrollbar );
034be888 270 m_vScrollbarVisible = FALSE;
2830bf19
RR
271 }
272 }
273 else
274 {
275 if (!m_vScrollbarVisible)
276 {
034be888 277 gtk_widget_show( m_vScrollbar );
034be888 278 m_vScrollbarVisible = TRUE;
2830bf19
RR
279 }
280 }
6de97a3b 281}
c801d85f 282
03f38c58 283wxString wxTextCtrl::GetValue() const
c801d85f 284{
223d09f6 285 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
8bbe427f 286
2830bf19
RR
287 wxString tmp;
288 if (m_windowStyle & wxTE_MULTILINE)
289 {
290 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
291 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
dcf924a3 292 tmp = wxString(text,*wxConvCurrent);
2830bf19
RR
293 g_free( text );
294 }
295 else
296 {
dcf924a3 297 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
2830bf19
RR
298 }
299 return tmp;
6de97a3b 300}
c801d85f
KB
301
302void wxTextCtrl::SetValue( const wxString &value )
303{
223d09f6 304 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 305
223d09f6 306 wxString tmp = wxT("");
2830bf19
RR
307 if (!value.IsNull()) tmp = value;
308 if (m_windowStyle & wxTE_MULTILINE)
309 {
310 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
311 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
312 len = 0;
05939a81 313#if wxUSE_UNICODE
3358d36e 314 wxWX2MBbuf tmpbuf = tmp.mbc_str();
05939a81
OK
315 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
316#else
317 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp.mbc_str(), tmp.Length(), &len );
318#endif
2830bf19
RR
319 }
320 else
321 {
05939a81 322 gtk_entry_set_text( GTK_ENTRY(m_text), tmp.mbc_str() );
2830bf19 323 }
6de97a3b 324}
c801d85f
KB
325
326void wxTextCtrl::WriteText( const wxString &text )
327{
223d09f6 328 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 329
2df7be7f 330 if (text.IsEmpty()) return;
484e45bf 331
2830bf19
RR
332 if (m_windowStyle & wxTE_MULTILINE)
333 {
291a8f20 334 /* this moves the cursor pos to behind the inserted text */
3358d36e
VZ
335 gint len = GTK_EDITABLE(m_text)->current_pos;
336
05939a81 337#if wxUSE_UNICODE
3358d36e 338 wxWX2MBbuf buf = text.mbc_str();
05939a81
OK
339 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
340#else
2830bf19 341 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
05939a81 342#endif
3358d36e
VZ
343
344 /* bring editable's cursor uptodate. bug in GTK. */
345 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
2830bf19
RR
346 }
347 else
348 {
c46554be 349 /* this moves the cursor pos to behind the inserted text */
3358d36e 350 gint len = GTK_EDITABLE(m_text)->current_pos;
05939a81 351#if wxUSE_UNICODE
3358d36e 352 wxWX2MBbuf buf = text.mbc_str();
05939a81
OK
353 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
354#else
c46554be 355 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
05939a81 356#endif
3358d36e
VZ
357
358 /* bring editable's cursor uptodate. bug in GTK. */
359 GTK_EDITABLE(m_text)->current_pos += text.Len();
360
361 /* bring entry's cursor uptodate. bug in GTK. */
362 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
2830bf19 363 }
6de97a3b 364}
c801d85f 365
a6e21573
HH
366void wxTextCtrl::AppendText( const wxString &text )
367{
223d09f6 368 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
a6e21573 369
2df7be7f
RR
370 if (text.IsEmpty()) return;
371
a6e21573
HH
372 if (m_windowStyle & wxTE_MULTILINE)
373 {
bb69661b
VZ
374 bool hasSpecialAttributes = m_font.Ok() ||
375 m_foregroundColour.Ok() ||
376 m_backgroundColour.Ok();
377 if ( hasSpecialAttributes )
378 {
379 gtk_text_insert( GTK_TEXT(m_text),
380 m_font.GetInternalFont(),
381 m_foregroundColour.GetColor(),
382 m_backgroundColour.GetColor(),
c980c992 383 text.mbc_str(), text.length());
bb69661b
VZ
384
385 }
386 else
387 {
388 /* we'll insert at the last position */
389 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
05939a81 390#if wxUSE_UNICODE
bb69661b
VZ
391 wxWX2MBbuf buf = text.mbc_str();
392 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
05939a81 393#else
bb69661b 394 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
05939a81 395#endif
bb69661b 396 }
3358d36e
VZ
397
398 /* bring editable's cursor uptodate. bug in GTK. */
399 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
a6e21573
HH
400 }
401 else
402 {
05939a81 403 gtk_entry_append_text( GTK_ENTRY(m_text), text.mbc_str() );
a6e21573
HH
404 }
405}
406
debe6624 407wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f 408{
a81258be
RR
409 if (m_windowStyle & wxTE_MULTILINE)
410 {
411 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
412 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
413
414 if (text)
415 {
223d09f6 416 wxString buf(wxT(""));
a81258be
RR
417 long i;
418 int currentLine = 0;
419 for (i = 0; currentLine != lineNo && text[i]; i++ )
420 if (text[i] == '\n')
421 currentLine++;
422 // Now get the text
423 int j;
424 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
425 buf += text[i];
8bbe427f 426
a81258be
RR
427 g_free( text );
428 return buf;
429 }
430 else
431 return wxEmptyString;
432 }
433 else
434 {
435 if (lineNo == 0) return GetValue();
436 return wxEmptyString;
437 }
6de97a3b 438}
c801d85f 439
a81258be
RR
440void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
441{
ac0d36b5
HH
442 /* If you implement this, don't forget to update the documentation!
443 * (file docs/latex/wx/text.tex) */
223d09f6 444 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
a81258be 445}
112892b9 446
0efe5ba7 447bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 448{
96385642 449 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 450 {
96385642
VZ
451 wxString text = GetValue();
452
6085b116 453 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 454 if( (unsigned long)pos > text.Len() )
96385642
VZ
455 return FALSE;
456
ac0d36b5
HH
457 *x=0; // First Col
458 *y=0; // First Line
2829d9e3 459
05939a81
OK
460 const wxChar* stop = text.c_str() + pos;
461 for ( const wxChar *p = text.c_str(); p < stop; p++ )
96385642 462 {
223d09f6 463 if (*p == wxT('\n'))
96385642
VZ
464 {
465 (*y)++;
ac0d36b5 466 *x=0;
96385642
VZ
467 }
468 else
469 (*x)++;
470 }
805dd538 471 }
96385642
VZ
472 else // single line control
473 {
2829d9e3 474 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 475 {
ac0d36b5 476 *y = 0;
96385642
VZ
477 *x = pos;
478 }
479 else
480 {
481 // index out of bounds
482 return FALSE;
483 }
8bbe427f 484 }
96385642
VZ
485
486 return TRUE;
6de97a3b 487}
c801d85f 488
e3ca08dd 489long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 490{
2830bf19 491 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 492
2830bf19 493 long pos=0;
ac0d36b5 494 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 495
3358d36e 496 pos += x;
2830bf19 497 return pos;
6de97a3b 498}
c801d85f 499
a81258be 500int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 501{
a81258be
RR
502 wxString str = GetLineText (lineNo);
503 return (int) str.Length();
6de97a3b 504}
c801d85f 505
a81258be 506int wxTextCtrl::GetNumberOfLines() const
c801d85f 507{
2830bf19 508 if (m_windowStyle & wxTE_MULTILINE)
a81258be 509 {
2830bf19
RR
510 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
511 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
512
513 if (text)
514 {
515 int currentLine = 0;
516 for (int i = 0; i < len; i++ )
96385642 517 {
2830bf19 518 if (text[i] == '\n')
96385642
VZ
519 currentLine++;
520 }
2830bf19 521 g_free( text );
2829d9e3
VZ
522
523 // currentLine is 0 based, add 1 to get number of lines
524 return currentLine + 1;
2830bf19
RR
525 }
526 else
96385642 527 {
2830bf19 528 return 0;
96385642 529 }
a81258be
RR
530 }
531 else
2830bf19 532 {
96385642 533 return 1;
2830bf19 534 }
6de97a3b 535}
c801d85f 536
debe6624 537void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 538{
223d09f6 539 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
3358d36e
VZ
540
541 if (m_windowStyle & wxTE_MULTILINE)
291a8f20
RR
542 {
543 /* seems to be broken in GTK 1.0.X:
3358d36e
VZ
544 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
545
291a8f20
RR
546 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
547 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 548
291a8f20 549 /* we fake a set_point by inserting and deleting. as the user
3358d36e
VZ
550 isn't supposed to get to know about thos non-sense, we
551 disconnect so that no events are sent to the user program. */
552
291a8f20
RR
553 gint tmp = (gint)pos;
554 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
555 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
556
291a8f20
RR
557 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
558 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e
VZ
559
560 /* bring editable's cursor uptodate. another bug in GTK. */
561
562 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
ac0d36b5 563 }
2830bf19 564 else
291a8f20 565 {
d59051dd 566 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e
VZ
567
568 /* bring editable's cursor uptodate. bug in GTK. */
569
b02da6b1 570 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
291a8f20 571 }
6de97a3b 572}
c801d85f 573
03f38c58 574void wxTextCtrl::SetInsertionPointEnd()
c801d85f 575{
223d09f6 576 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 577
d59051dd
VZ
578 if (m_windowStyle & wxTE_MULTILINE)
579 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
580 else
581 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
6de97a3b 582}
c801d85f 583
debe6624 584void wxTextCtrl::SetEditable( bool editable )
c801d85f 585{
223d09f6 586 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 587
2830bf19
RR
588 if (m_windowStyle & wxTE_MULTILINE)
589 gtk_text_set_editable( GTK_TEXT(m_text), editable );
590 else
591 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
6de97a3b 592}
c801d85f 593
0efe5ba7
VZ
594void wxTextCtrl::DiscardEdits()
595{
596 m_modified = FALSE;
597}
598
debe6624 599void wxTextCtrl::SetSelection( long from, long to )
c801d85f 600{
223d09f6 601 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 602
2830bf19 603 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 604}
c801d85f 605
910f1f8c 606void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 607{
910f1f8c 608// SetInsertionPoint( pos );
6de97a3b 609}
c801d85f 610
03f38c58 611long wxTextCtrl::GetInsertionPoint() const
c801d85f 612{
223d09f6 613 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 614
2830bf19 615 return (long) GTK_EDITABLE(m_text)->current_pos;
6de97a3b 616}
c801d85f 617
03f38c58 618long wxTextCtrl::GetLastPosition() const
c801d85f 619{
223d09f6 620 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
8bbe427f 621
2830bf19
RR
622 int pos = 0;
623 if (m_windowStyle & wxTE_MULTILINE)
624 pos = gtk_text_get_length( GTK_TEXT(m_text) );
625 else
626 pos = GTK_ENTRY(m_text)->text_length;
805dd538 627
ac0d36b5 628 return (long)pos;
6de97a3b 629}
c801d85f 630
debe6624 631void wxTextCtrl::Remove( long from, long to )
c801d85f 632{
223d09f6 633 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 634
2830bf19 635 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 636}
c801d85f 637
debe6624 638void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 639{
223d09f6 640 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 641
2830bf19 642 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
bb69661b 643
2df7be7f
RR
644 if (!value.IsEmpty())
645 {
646 gint pos = (gint)from;
05939a81 647#if wxUSE_UNICODE
2df7be7f
RR
648 wxWX2MBbuf buf = value.mbc_str();
649 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 650#else
2df7be7f 651 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
05939a81 652#endif
2df7be7f 653 }
6de97a3b 654}
c801d85f 655
03f38c58 656void wxTextCtrl::Cut()
c801d85f 657{
223d09f6 658 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 659
d345e841 660#if (GTK_MINOR_VERSION > 0)
2830bf19 661 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 662#else
2830bf19 663 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 664#endif
6de97a3b 665}
c801d85f 666
03f38c58 667void wxTextCtrl::Copy()
c801d85f 668{
223d09f6 669 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 670
d345e841 671#if (GTK_MINOR_VERSION > 0)
2830bf19 672 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 673#else
2830bf19 674 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 675#endif
6de97a3b 676}
c801d85f 677
03f38c58 678void wxTextCtrl::Paste()
c801d85f 679{
223d09f6 680 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
8bbe427f 681
d345e841 682#if (GTK_MINOR_VERSION > 0)
2830bf19 683 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 684#else
2830bf19 685 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 686#endif
6de97a3b 687}
c801d85f 688
ca8b28f2
JS
689bool wxTextCtrl::CanCopy() const
690{
691 // Can copy if there's a selection
692 long from, to;
693 GetSelection(& from, & to);
694 return (from != to) ;
695}
696
697bool wxTextCtrl::CanCut() const
698{
699 // Can cut if there's a selection
700 long from, to;
701 GetSelection(& from, & to);
702 return (from != to) ;
703}
704
705bool wxTextCtrl::CanPaste() const
706{
707 return IsEditable() ;
708}
709
710// Undo/redo
711void wxTextCtrl::Undo()
712{
713 // TODO
223d09f6 714 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
715}
716
717void wxTextCtrl::Redo()
718{
719 // TODO
223d09f6 720 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
721}
722
723bool wxTextCtrl::CanUndo() const
724{
725 // TODO
223d09f6 726 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
ca8b28f2
JS
727 return FALSE;
728}
729
730bool wxTextCtrl::CanRedo() const
731{
732 // TODO
223d09f6 733 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
ca8b28f2
JS
734 return FALSE;
735}
736
737// If the return values from and to are the same, there is no
738// selection.
739void wxTextCtrl::GetSelection(long* from, long* to) const
740{
223d09f6 741 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
bb69661b 742
05060eeb
RR
743 if (!(GTK_EDITABLE(m_text)->has_selection))
744 {
745 if (from) *from = 0;
bb69661b
VZ
746 if (to) *to = 0;
747 return;
05060eeb 748 }
bb69661b 749
05060eeb
RR
750 if (from) *from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
751 if (to) *to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
ca8b28f2
JS
752}
753
754bool wxTextCtrl::IsEditable() const
755{
223d09f6 756 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
05060eeb
RR
757
758 return GTK_EDITABLE(m_text)->editable;
ca8b28f2
JS
759}
760
0efe5ba7
VZ
761bool wxTextCtrl::IsModified() const
762{
763 return m_modified;
764}
765
03f38c58 766void wxTextCtrl::Clear()
c801d85f 767{
223d09f6 768 SetValue( wxT("") );
6de97a3b 769}
c801d85f 770
903f689b 771void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 772{
223d09f6 773 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
805dd538 774
2830bf19
RR
775 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
776 {
777 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
778 event.SetEventObject(this);
779 if (GetEventHandler()->ProcessEvent(event)) return;
780 }
903f689b 781
da048e3d
RR
782 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
783 {
784 wxWindow *top_frame = m_parent;
8487f887 785 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d
RR
786 top_frame = top_frame->GetParent();
787 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
788
789 if (window->default_widget)
790 {
791 gtk_widget_activate (window->default_widget);
792 return;
793 }
794 }
795
2830bf19 796 key_event.Skip();
6de97a3b 797}
c801d85f 798
03f38c58 799GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 800{
ae0bdb01 801 return GTK_WIDGET(m_text);
6de97a3b 802}
e3e65dac 803
903f689b
RR
804bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
805{
ae0bdb01
RR
806 if (m_windowStyle & wxTE_MULTILINE)
807 return (window == GTK_TEXT(m_text)->text_area);
808 else
809 return (window == GTK_ENTRY(m_text)->text_area);
903f689b 810}
e3e65dac 811
bb69661b
VZ
812// the font will change for subsequent text insertiongs
813bool wxTextCtrl::SetFont( const wxFont &font )
868a2826 814{
223d09f6 815 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 816
bb69661b
VZ
817 if ( !wxWindowBase::SetFont(font) )
818 {
819 // font didn't change, nothing to do
820 return FALSE;
821 }
822
823 if ( m_windowStyle & wxTE_MULTILINE )
824 {
825 // for compatibility with other ports: the font is a global controls
826 // characteristic, so change the font globally
827 wxString value = GetValue();
828 if ( !value.IsEmpty() )
829 {
830 Clear();
831
832 AppendText(value);
833 }
834 }
835
836 return TRUE;
58614078
RR
837}
838
f03fc89f 839bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
58614078 840{
223d09f6 841 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
8bbe427f 842
ae0bdb01 843 // doesn't work
f03fc89f 844 return FALSE;
868a2826 845}
e3e65dac 846
f03fc89f 847bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 848{
223d09f6 849 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
a81258be 850
ae0bdb01 851 wxControl::SetBackgroundColour( colour );
3358d36e 852
f03fc89f
VZ
853 if (!m_widget->window)
854 return FALSE;
8bbe427f 855
ae0bdb01 856 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
805dd538
VZ
857 if (sysbg.Red() == colour.Red() &&
858 sysbg.Green() == colour.Green() &&
ae0bdb01
RR
859 sysbg.Blue() == colour.Blue())
860 {
f03fc89f 861 return FALSE; // FIXME or TRUE?
805dd538
VZ
862 }
863
f03fc89f
VZ
864 if (!m_backgroundColour.Ok())
865 return FALSE;
8bbe427f 866
ae0bdb01
RR
867 if (m_windowStyle & wxTE_MULTILINE)
868 {
869 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f
VZ
870 if (!window)
871 return FALSE;
ae0bdb01
RR
872 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
873 gdk_window_set_background( window, m_backgroundColour.GetColor() );
874 gdk_window_clear( window );
875 }
f03fc89f
VZ
876
877 return TRUE;
58614078
RR
878}
879
880void wxTextCtrl::ApplyWidgetStyle()
881{
ae0bdb01
RR
882 if (m_windowStyle & wxTE_MULTILINE)
883 {
2830bf19 884 // how ?
805dd538 885 }
ae0bdb01
RR
886 else
887 {
888 SetWidgetStyle();
889 gtk_widget_set_style( m_text, m_widgetStyle );
890 }
68dda785 891}
f96aa4d9 892
e702ff0f
JS
893void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
894{
895 Cut();
896}
897
898void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
899{
900 Copy();
901}
902
903void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
904{
905 Paste();
906}
907
908void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
909{
910 Undo();
911}
912
913void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
914{
915 Redo();
916}
917
918void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
919{
920 event.Enable( CanCut() );
921}
922
923void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
924{
925 event.Enable( CanCopy() );
926}
927
928void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
929{
930 event.Enable( CanPaste() );
931}
932
933void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
934{
935 event.Enable( CanUndo() );
936}
937
938void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
939{
940 event.Enable( CanRedo() );
941}
65045edd
RR
942
943void wxTextCtrl::OnInternalIdle()
944{
945 wxCursor cursor = m_cursor;
946 if (g_globalCursor.Ok()) cursor = g_globalCursor;
947
307f16e8 948 if (cursor.Ok())
65045edd 949 {
65045edd
RR
950 GdkWindow *window = (GdkWindow*) NULL;
951 if (HasFlag(wxTE_MULTILINE))
952 window = GTK_TEXT(m_text)->text_area;
953 else
954 window = GTK_ENTRY(m_text)->text_area;
955
956 if (window)
957 gdk_window_set_cursor( window, cursor.GetCursor() );
958
959 if (!g_globalCursor.Ok())
960 cursor = *wxSTANDARD_CURSOR;
961
962 window = m_widget->window;
247e5b16 963 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
65045edd
RR
964 gdk_window_set_cursor( window, cursor.GetCursor() );
965 }
966}