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