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