]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
Made tearoff menus work for gtk :-)
[wxWidgets.git] / src / gtk1 / textctrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
13289f04 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
15#include "wx/utils.h"
ae0bdb01
RR
16#include "wx/intl.h"
17#include "wx/settings.h"
c801d85f 18
a81258be
RR
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <ctype.h>
22
83624f79
RR
23#include "gdk/gdk.h"
24#include "gtk/gtk.h"
b292e2f5
RR
25#include "gdk/gdkkeysyms.h"
26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
b292e2f5
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
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
38830220 97#if wxUSE_STD_IOSTREAM
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
38830220 111#if wxUSE_STD_IOSTREAM
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
2df7be7f 348 if (text.IsEmpty()) return;
484e45bf 349
2df7be7f
RR
350 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
351 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
352
2830bf19
RR
353 if (m_windowStyle & wxTE_MULTILINE)
354 {
291a8f20 355 /* this moves the cursor pos to behind the inserted text */
3358d36e
VZ
356 gint len = GTK_EDITABLE(m_text)->current_pos;
357
05939a81 358#if wxUSE_UNICODE
3358d36e 359 wxWX2MBbuf buf = text.mbc_str();
05939a81
OK
360 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
361#else
2830bf19 362 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
05939a81 363#endif
3358d36e
VZ
364
365 /* bring editable's cursor uptodate. bug in GTK. */
366 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
2830bf19
RR
367 }
368 else
369 {
c46554be 370 /* this moves the cursor pos to behind the inserted text */
3358d36e 371 gint len = GTK_EDITABLE(m_text)->current_pos;
05939a81 372#if wxUSE_UNICODE
3358d36e 373 wxWX2MBbuf buf = text.mbc_str();
05939a81
OK
374 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
375#else
c46554be 376 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
05939a81 377#endif
3358d36e
VZ
378
379 /* bring editable's cursor uptodate. bug in GTK. */
380 GTK_EDITABLE(m_text)->current_pos += text.Len();
381
382 /* bring entry's cursor uptodate. bug in GTK. */
383 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
2830bf19 384 }
2df7be7f
RR
385
386 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
387 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
6de97a3b 388}
c801d85f 389
a6e21573
HH
390void wxTextCtrl::AppendText( const wxString &text )
391{
05939a81 392 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
a6e21573 393
2df7be7f
RR
394 if (text.IsEmpty()) return;
395
396 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
397 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
398
a6e21573
HH
399 if (m_windowStyle & wxTE_MULTILINE)
400 {
401 /* we'll insert at the last position */
402 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
05939a81 403#if wxUSE_UNICODE
3358d36e 404 wxWX2MBbuf buf = text.mbc_str();
05939a81
OK
405 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
406#else
a6e21573 407 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
05939a81 408#endif
3358d36e
VZ
409
410 /* bring editable's cursor uptodate. bug in GTK. */
411 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
a6e21573
HH
412 }
413 else
414 {
05939a81 415 gtk_entry_append_text( GTK_ENTRY(m_text), text.mbc_str() );
a6e21573 416 }
2df7be7f
RR
417
418 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
419 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
a6e21573
HH
420}
421
a81258be 422bool wxTextCtrl::LoadFile( const wxString &file )
c801d85f 423{
2df7be7f 424 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
8bbe427f 425
2df7be7f 426 if (!wxFileExists(file)) return FALSE;
2ad3a34e 427
2df7be7f 428 Clear();
a81258be 429
2df7be7f
RR
430 FILE *fp = (FILE*) NULL;
431 struct stat statb;
8bbe427f 432
2df7be7f
RR
433 if ((stat (FNSTRINGCAST file.fn_str(), &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
434 !(fp = fopen (FNSTRINGCAST file.fn_str(), "r")))
a81258be 435 {
2df7be7f 436 return FALSE;
a81258be 437 }
2df7be7f 438 else
805dd538 439 {
2df7be7f
RR
440 gint len = statb.st_size;
441 char *text;
442 if (!(text = (char*)malloc ((unsigned) (len + 1))))
443 {
444 fclose (fp);
445 return FALSE;
446 }
447 if (fread (text, sizeof (char), len, fp) != (size_t) len)
448 {
449 }
450 fclose (fp);
a81258be 451
2df7be7f 452 text[len] = 0;
8bbe427f 453
2df7be7f
RR
454 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
455 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
456
457 if (m_windowStyle & wxTE_MULTILINE)
458 {
459 gint pos = 0;
460 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos );
461 }
462 else
463 {
464 gtk_entry_set_text( GTK_ENTRY(m_text), text );
465 }
466
467 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
468 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
469
470 free (text);
471 m_modified = FALSE;
472 return TRUE;
a81258be 473 }
2df7be7f 474 return FALSE;
6de97a3b 475}
c801d85f 476
a81258be 477bool wxTextCtrl::SaveFile( const wxString &file )
c801d85f 478{
2df7be7f 479 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
8bbe427f 480
2df7be7f 481 if (file == _T("")) return FALSE;
8bbe427f 482
2df7be7f 483 FILE *fp;
2ad3a34e 484
2df7be7f 485 if (!(fp = fopen (FNSTRINGCAST file.fn_str(), "w")))
a81258be 486 {
2df7be7f 487 return FALSE;
a81258be 488 }
2df7be7f 489 else
a81258be 490 {
bbe0af5b 491 char *text = (char*) NULL;
a81258be 492 gint len = 0;
8bbe427f 493
a81258be
RR
494 if (m_windowStyle & wxTE_MULTILINE)
495 {
2df7be7f
RR
496 len = gtk_text_get_length( GTK_TEXT(m_text) );
497 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
a81258be
RR
498 }
499 else
500 {
2df7be7f 501 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
a81258be 502 }
8bbe427f 503
a81258be 504 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
96385642
VZ
505 {
506 // Did not write whole file
507 }
8bbe427f 508
a81258be
RR
509 // Make sure newline terminates the file
510 if (text[len - 1] != '\n')
96385642 511 fputc ('\n', fp);
a81258be
RR
512
513 fclose (fp);
8bbe427f 514
a81258be 515 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
8bbe427f 516
a81258be
RR
517 m_modified = FALSE;
518 return TRUE;
519 }
520
2df7be7f 521 return TRUE;
6de97a3b 522}
c801d85f 523
debe6624 524wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f 525{
a81258be
RR
526 if (m_windowStyle & wxTE_MULTILINE)
527 {
528 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
529 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
530
531 if (text)
532 {
05939a81 533 wxString buf(_T(""));
a81258be
RR
534 long i;
535 int currentLine = 0;
536 for (i = 0; currentLine != lineNo && text[i]; i++ )
537 if (text[i] == '\n')
538 currentLine++;
539 // Now get the text
540 int j;
541 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
542 buf += text[i];
8bbe427f 543
a81258be
RR
544 g_free( text );
545 return buf;
546 }
547 else
548 return wxEmptyString;
549 }
550 else
551 {
552 if (lineNo == 0) return GetValue();
553 return wxEmptyString;
554 }
6de97a3b 555}
c801d85f 556
a81258be
RR
557void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
558{
ac0d36b5
HH
559 /* If you implement this, don't forget to update the documentation!
560 * (file docs/latex/wx/text.tex) */
05939a81 561 wxFAIL_MSG( _T("wxTextCtrl::OnDropFiles not implemented") );
a81258be 562}
112892b9 563
e3ca08dd 564long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
c801d85f 565{
96385642 566 if ( m_windowStyle & wxTE_MULTILINE )
805dd538 567 {
96385642
VZ
568 wxString text = GetValue();
569
6085b116 570 // cast to prevent warning. But pos really should've been unsigned.
2829d9e3 571 if( (unsigned long)pos > text.Len() )
96385642
VZ
572 return FALSE;
573
ac0d36b5
HH
574 *x=0; // First Col
575 *y=0; // First Line
2829d9e3 576
05939a81
OK
577 const wxChar* stop = text.c_str() + pos;
578 for ( const wxChar *p = text.c_str(); p < stop; p++ )
96385642 579 {
05939a81 580 if (*p == _T('\n'))
96385642
VZ
581 {
582 (*y)++;
ac0d36b5 583 *x=0;
96385642
VZ
584 }
585 else
586 (*x)++;
587 }
805dd538 588 }
96385642
VZ
589 else // single line control
590 {
2829d9e3 591 if ( pos <= GTK_ENTRY(m_text)->text_length )
96385642 592 {
ac0d36b5 593 *y = 0;
96385642
VZ
594 *x = pos;
595 }
596 else
597 {
598 // index out of bounds
599 return FALSE;
600 }
8bbe427f 601 }
96385642
VZ
602
603 return TRUE;
6de97a3b 604}
c801d85f 605
e3ca08dd 606long wxTextCtrl::XYToPosition(long x, long y ) const
c801d85f 607{
2830bf19 608 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
805dd538 609
2830bf19 610 long pos=0;
ac0d36b5 611 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
8bbe427f 612
3358d36e 613 pos += x;
2830bf19 614 return pos;
6de97a3b 615}
c801d85f 616
a81258be 617int wxTextCtrl::GetLineLength(long lineNo) const
c801d85f 618{
a81258be
RR
619 wxString str = GetLineText (lineNo);
620 return (int) str.Length();
6de97a3b 621}
c801d85f 622
a81258be 623int wxTextCtrl::GetNumberOfLines() const
c801d85f 624{
2830bf19 625 if (m_windowStyle & wxTE_MULTILINE)
a81258be 626 {
2830bf19
RR
627 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
628 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
629
630 if (text)
631 {
632 int currentLine = 0;
633 for (int i = 0; i < len; i++ )
96385642 634 {
2830bf19 635 if (text[i] == '\n')
96385642
VZ
636 currentLine++;
637 }
2830bf19 638 g_free( text );
2829d9e3
VZ
639
640 // currentLine is 0 based, add 1 to get number of lines
641 return currentLine + 1;
2830bf19
RR
642 }
643 else
96385642 644 {
2830bf19 645 return 0;
96385642 646 }
a81258be
RR
647 }
648 else
2830bf19 649 {
96385642 650 return 1;
2830bf19 651 }
6de97a3b 652}
c801d85f 653
debe6624 654void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f 655{
05939a81 656 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
3358d36e
VZ
657
658 if (m_windowStyle & wxTE_MULTILINE)
291a8f20
RR
659 {
660 /* seems to be broken in GTK 1.0.X:
3358d36e
VZ
661 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
662
291a8f20
RR
663 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
664 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e 665
291a8f20 666 /* we fake a set_point by inserting and deleting. as the user
3358d36e
VZ
667 isn't supposed to get to know about thos non-sense, we
668 disconnect so that no events are sent to the user program. */
669
291a8f20
RR
670 gint tmp = (gint)pos;
671 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
3358d36e
VZ
672 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
673
291a8f20
RR
674 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
675 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
3358d36e
VZ
676
677 /* bring editable's cursor uptodate. another bug in GTK. */
678
679 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
ac0d36b5 680 }
2830bf19 681 else
291a8f20 682 {
d59051dd 683 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
3358d36e
VZ
684
685 /* bring editable's cursor uptodate. bug in GTK. */
686
687 GTK_EDITABLE(m_text)->current_pos = pos;
291a8f20 688 }
6de97a3b 689}
c801d85f 690
03f38c58 691void wxTextCtrl::SetInsertionPointEnd()
c801d85f 692{
05939a81 693 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 694
d59051dd
VZ
695 if (m_windowStyle & wxTE_MULTILINE)
696 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
697 else
698 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
6de97a3b 699}
c801d85f 700
debe6624 701void wxTextCtrl::SetEditable( bool editable )
c801d85f 702{
05939a81 703 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 704
2830bf19
RR
705 if (m_windowStyle & wxTE_MULTILINE)
706 gtk_text_set_editable( GTK_TEXT(m_text), editable );
707 else
708 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
6de97a3b 709}
c801d85f 710
debe6624 711void wxTextCtrl::SetSelection( long from, long to )
c801d85f 712{
05939a81 713 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 714
2830bf19 715 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 716}
c801d85f 717
910f1f8c 718void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 719{
910f1f8c 720// SetInsertionPoint( pos );
6de97a3b 721}
c801d85f 722
03f38c58 723long wxTextCtrl::GetInsertionPoint() const
c801d85f 724{
05939a81 725 wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") );
8bbe427f 726
2830bf19 727 return (long) GTK_EDITABLE(m_text)->current_pos;
6de97a3b 728}
c801d85f 729
03f38c58 730long wxTextCtrl::GetLastPosition() const
c801d85f 731{
05939a81 732 wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") );
8bbe427f 733
2830bf19
RR
734 int pos = 0;
735 if (m_windowStyle & wxTE_MULTILINE)
736 pos = gtk_text_get_length( GTK_TEXT(m_text) );
737 else
738 pos = GTK_ENTRY(m_text)->text_length;
805dd538 739
ac0d36b5 740 return (long)pos;
6de97a3b 741}
c801d85f 742
debe6624 743void wxTextCtrl::Remove( long from, long to )
c801d85f 744{
05939a81 745 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 746
2df7be7f
RR
747 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
748 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
749
2830bf19 750 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
2df7be7f
RR
751
752 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
753 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
6de97a3b 754}
c801d85f 755
debe6624 756void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 757{
05939a81 758 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 759
2df7be7f
RR
760 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
761 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
762
2830bf19 763 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
2df7be7f
RR
764
765 if (!value.IsEmpty())
766 {
767 gint pos = (gint)from;
05939a81 768#if wxUSE_UNICODE
2df7be7f
RR
769 wxWX2MBbuf buf = value.mbc_str();
770 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
05939a81 771#else
2df7be7f 772 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
05939a81 773#endif
2df7be7f
RR
774 }
775
776 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
777 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
6de97a3b 778}
c801d85f 779
03f38c58 780void wxTextCtrl::Cut()
c801d85f 781{
05939a81 782 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 783
2df7be7f
RR
784 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
785 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
786
d345e841 787#if (GTK_MINOR_VERSION > 0)
2830bf19 788 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 789#else
2830bf19 790 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 791#endif
2df7be7f
RR
792
793 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
794 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
6de97a3b 795}
c801d85f 796
03f38c58 797void wxTextCtrl::Copy()
c801d85f 798{
05939a81 799 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 800
d345e841 801#if (GTK_MINOR_VERSION > 0)
2830bf19 802 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 803#else
2830bf19 804 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 805#endif
6de97a3b 806}
c801d85f 807
03f38c58 808void wxTextCtrl::Paste()
c801d85f 809{
05939a81 810 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
8bbe427f 811
2df7be7f
RR
812 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
813 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
814
d345e841 815#if (GTK_MINOR_VERSION > 0)
2830bf19 816 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
75ed1d15 817#else
2830bf19 818 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 819#endif
2df7be7f
RR
820
821 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
822 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
6de97a3b 823}
c801d85f 824
ca8b28f2
JS
825bool wxTextCtrl::CanCopy() const
826{
827 // Can copy if there's a selection
828 long from, to;
829 GetSelection(& from, & to);
830 return (from != to) ;
831}
832
833bool wxTextCtrl::CanCut() const
834{
835 // Can cut if there's a selection
836 long from, to;
837 GetSelection(& from, & to);
838 return (from != to) ;
839}
840
841bool wxTextCtrl::CanPaste() const
842{
843 return IsEditable() ;
844}
845
846// Undo/redo
847void wxTextCtrl::Undo()
848{
849 // TODO
05939a81 850 wxFAIL_MSG( _T("wxTextCtrl::Undo not implemented") );
ca8b28f2
JS
851}
852
853void wxTextCtrl::Redo()
854{
855 // TODO
05939a81 856 wxFAIL_MSG( _T("wxTextCtrl::Redo not implemented") );
ca8b28f2
JS
857}
858
859bool wxTextCtrl::CanUndo() const
860{
861 // TODO
05939a81 862 wxFAIL_MSG( _T("wxTextCtrl::CanUndo not implemented") );
ca8b28f2
JS
863 return FALSE;
864}
865
866bool wxTextCtrl::CanRedo() const
867{
868 // TODO
05939a81 869 wxFAIL_MSG( _T("wxTextCtrl::CanRedo not implemented") );
ca8b28f2
JS
870 return FALSE;
871}
872
873// If the return values from and to are the same, there is no
874// selection.
875void wxTextCtrl::GetSelection(long* from, long* to) const
876{
05060eeb
RR
877 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
878
879 if (!(GTK_EDITABLE(m_text)->has_selection))
880 {
881 if (from) *from = 0;
882 if (to) *to = 0;
883 return;
884 }
885
886 if (from) *from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
887 if (to) *to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
ca8b28f2
JS
888}
889
890bool wxTextCtrl::IsEditable() const
891{
05060eeb
RR
892 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
893
894 return GTK_EDITABLE(m_text)->editable;
ca8b28f2
JS
895}
896
03f38c58 897void wxTextCtrl::Clear()
c801d85f 898{
05939a81 899 SetValue( _T("") );
6de97a3b 900}
c801d85f 901
903f689b 902void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 903{
05939a81 904 wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") );
805dd538 905
2830bf19
RR
906 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
907 {
908 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
909 event.SetEventObject(this);
910 if (GetEventHandler()->ProcessEvent(event)) return;
911 }
903f689b 912
2830bf19 913 key_event.Skip();
6de97a3b 914}
c801d85f 915
38830220 916#if wxUSE_STD_IOSTREAM
46dc76ba 917int wxTextCtrl::overflow( int WXUNUSED(c) )
c801d85f 918{
2830bf19
RR
919 int len = pptr() - pbase();
920 char *txt = new char[len+1];
921 strncpy(txt, pbase(), len);
922 txt[len] = '\0';
923 (*this) << txt;
924 setp(pbase(), epptr());
925 delete[] txt;
926 return EOF;
6de97a3b 927}
c801d85f 928
03f38c58 929int wxTextCtrl::sync()
c801d85f 930{
2830bf19
RR
931 int len = pptr() - pbase();
932 char *txt = new char[len+1];
933 strncpy(txt, pbase(), len);
934 txt[len] = '\0';
935 (*this) << txt;
936 setp(pbase(), epptr());
937 delete[] txt;
938 return 0;
6de97a3b 939}
c801d85f 940
03f38c58 941int wxTextCtrl::underflow()
c801d85f 942{
2830bf19 943 return EOF;
6de97a3b 944}
38830220 945#endif
c801d85f
KB
946
947wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
948{
a6e21573 949 AppendText(s);
2830bf19 950 return *this;
c801d85f
KB
951}
952
debe6624 953wxTextCtrl& wxTextCtrl::operator<<(float f)
c801d85f 954{
2830bf19
RR
955 static char buf[100];
956 sprintf(buf, "%.2f", f);
a6e21573 957 AppendText(buf);
2830bf19 958 return *this;
c801d85f
KB
959}
960
debe6624 961wxTextCtrl& wxTextCtrl::operator<<(double d)
c801d85f 962{
2830bf19
RR
963 static char buf[100];
964 sprintf(buf, "%.2f", d);
a6e21573 965 AppendText(buf);
2830bf19 966 return *this;
c801d85f
KB
967}
968
debe6624 969wxTextCtrl& wxTextCtrl::operator<<(int i)
c801d85f 970{
2830bf19
RR
971 static char buf[100];
972 sprintf(buf, "%i", i);
a6e21573 973 AppendText(buf);
2830bf19 974 return *this;
c801d85f
KB
975}
976
debe6624 977wxTextCtrl& wxTextCtrl::operator<<(long i)
c801d85f 978{
2830bf19
RR
979 static char buf[100];
980 sprintf(buf, "%ld", i);
a6e21573 981 AppendText(buf);
2830bf19 982 return *this;
c801d85f
KB
983}
984
985wxTextCtrl& wxTextCtrl::operator<<(const char c)
986{
2830bf19 987 char buf[2];
c801d85f 988
2830bf19
RR
989 buf[0] = c;
990 buf[1] = 0;
a6e21573 991 AppendText(buf);
2830bf19 992 return *this;
c801d85f
KB
993}
994
03f38c58 995GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac 996{
ae0bdb01 997 return GTK_WIDGET(m_text);
6de97a3b 998}
e3e65dac 999
903f689b
RR
1000bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1001{
ae0bdb01
RR
1002 if (m_windowStyle & wxTE_MULTILINE)
1003 return (window == GTK_TEXT(m_text)->text_area);
1004 else
1005 return (window == GTK_ENTRY(m_text)->text_area);
903f689b 1006}
e3e65dac 1007
f03fc89f 1008bool wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
868a2826 1009{
f03fc89f 1010 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
8bbe427f 1011
ae0bdb01 1012 // doesn't work
f03fc89f 1013 return FALSE;
58614078
RR
1014}
1015
f03fc89f 1016bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
58614078 1017{
f03fc89f 1018 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
8bbe427f 1019
ae0bdb01 1020 // doesn't work
f03fc89f 1021 return FALSE;
868a2826 1022}
e3e65dac 1023
f03fc89f 1024bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
68dda785 1025{
f03fc89f 1026 wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") );
a81258be 1027
ae0bdb01 1028 wxControl::SetBackgroundColour( colour );
3358d36e 1029
f03fc89f
VZ
1030 if (!m_widget->window)
1031 return FALSE;
8bbe427f 1032
ae0bdb01 1033 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
805dd538
VZ
1034 if (sysbg.Red() == colour.Red() &&
1035 sysbg.Green() == colour.Green() &&
ae0bdb01
RR
1036 sysbg.Blue() == colour.Blue())
1037 {
f03fc89f 1038 return FALSE; // FIXME or TRUE?
805dd538
VZ
1039 }
1040
f03fc89f
VZ
1041 if (!m_backgroundColour.Ok())
1042 return FALSE;
8bbe427f 1043
ae0bdb01
RR
1044 if (m_windowStyle & wxTE_MULTILINE)
1045 {
1046 GdkWindow *window = GTK_TEXT(m_text)->text_area;
f03fc89f
VZ
1047 if (!window)
1048 return FALSE;
ae0bdb01
RR
1049 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1050 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1051 gdk_window_clear( window );
1052 }
f03fc89f
VZ
1053
1054 return TRUE;
58614078
RR
1055}
1056
1057void wxTextCtrl::ApplyWidgetStyle()
1058{
ae0bdb01
RR
1059 if (m_windowStyle & wxTE_MULTILINE)
1060 {
2830bf19 1061 // how ?
805dd538 1062 }
ae0bdb01
RR
1063 else
1064 {
1065 SetWidgetStyle();
1066 gtk_widget_set_style( m_text, m_widgetStyle );
1067 }
68dda785 1068}
f96aa4d9 1069
e702ff0f
JS
1070void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1071{
1072 Cut();
1073}
1074
1075void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1076{
1077 Copy();
1078}
1079
1080void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1081{
1082 Paste();
1083}
1084
1085void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1086{
1087 Undo();
1088}
1089
1090void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1091{
1092 Redo();
1093}
1094
1095void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1096{
1097 event.Enable( CanCut() );
1098}
1099
1100void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1101{
1102 event.Enable( CanCopy() );
1103}
1104
1105void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1106{
1107 event.Enable( CanPaste() );
1108}
1109
1110void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1111{
1112 event.Enable( CanUndo() );
1113}
1114
1115void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1116{
1117 event.Enable( CanRedo() );
1118}