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