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