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