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