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