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