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