]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
icon support moved to wxBitmap, allowing for fully transparent usage
[wxWidgets.git] / src / gtk1 / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "textctrl.h"
12 #endif
13
14 #include "wx/textctrl.h"
15 #include "wx/utils.h"
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #include "wx/settings.h"
19 #include "wx/panel.h"
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <ctype.h>
24
25 #include "gdk/gdk.h"
26 #include "gtk/gtk.h"
27 #include "gdk/gdkkeysyms.h"
28
29 //-----------------------------------------------------------------------------
30 // idle system
31 //-----------------------------------------------------------------------------
32
33 extern void wxapp_install_idle_handler();
34 extern bool g_isIdle;
35
36 //-----------------------------------------------------------------------------
37 // data
38 //-----------------------------------------------------------------------------
39
40 extern bool g_blockEventsOnDrag;
41 extern wxCursor g_globalCursor;
42
43 //-----------------------------------------------------------------------------
44 // "changed"
45 //-----------------------------------------------------------------------------
46
47 static void
48 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
49 {
50 if (!win->m_hasVMT) return;
51
52 if (g_isIdle)
53 wxapp_install_idle_handler();
54
55 win->SetModified();
56 win->UpdateFontIfNeeded();
57
58 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
59 event.SetString( win->GetValue() );
60 event.SetEventObject( win );
61 win->GetEventHandler()->ProcessEvent( event );
62 }
63
64 //-----------------------------------------------------------------------------
65 // "changed" from vertical scrollbar
66 //-----------------------------------------------------------------------------
67
68 static void
69 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
70 {
71 if (!win->m_hasVMT) return;
72
73 if (g_isIdle)
74 wxapp_install_idle_handler();
75
76 win->CalculateScrollbar();
77 }
78
79 //-----------------------------------------------------------------------------
80 // "focus_in_event"
81 //-----------------------------------------------------------------------------
82
83 wxWindow *FindFocusedChild(wxWindow *win);
84 extern wxWindow *g_focusWindow;
85 extern bool g_blockEventsOnDrag;
86 // extern bool g_isIdle;
87
88 static gint gtk_text_focus_in_callback( GtkWidget *widget, GdkEvent *WXUNUSED(event), wxWindow *win )
89 {
90 // Necessary?
91 #if 0
92 if (g_isIdle)
93 wxapp_install_idle_handler();
94 #endif
95 if (!win->m_hasVMT) return FALSE;
96 if (g_blockEventsOnDrag) return FALSE;
97
98 g_focusWindow = win;
99
100 wxPanel *panel = wxDynamicCast(win->GetParent(), wxPanel);
101 if (panel)
102 {
103 panel->SetLastFocus(win);
104 }
105
106 #ifdef HAVE_XIM
107 if (win->m_ic)
108 gdk_im_begin(win->m_ic, win->m_wxwindow->window);
109 #endif
110
111 #if 0
112 #ifdef wxUSE_CARET
113 // caret needs to be informed about focus change
114 wxCaret *caret = win->GetCaret();
115 if ( caret )
116 {
117 caret->OnSetFocus();
118 }
119 #endif // wxUSE_CARET
120 #endif
121
122 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
123 event.SetEventObject( win );
124
125 if (win->GetEventHandler()->ProcessEvent( event ))
126 {
127 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus_in_event" );
128 return TRUE;
129 }
130
131 return FALSE;
132 }
133
134 //-----------------------------------------------------------------------------
135 // "focus_out_event"
136 //-----------------------------------------------------------------------------
137
138 static gint gtk_text_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED(event), wxWindow *win )
139 {
140 #if 0
141 if (g_isIdle)
142 wxapp_install_idle_handler();
143 #endif
144
145 if (!win->m_hasVMT) return FALSE;
146 if (g_blockEventsOnDrag) return FALSE;
147
148 #if 0
149 // if the focus goes out of our app alltogether, OnIdle() will send
150 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
151 // g_sendActivateEvent to -1
152 g_sendActivateEvent = 0;
153 #endif
154
155 wxWindow *winFocus = FindFocusedChild(win);
156 if ( winFocus )
157 win = winFocus;
158
159 g_focusWindow = (wxWindow *)NULL;
160
161 #ifdef HAVE_XIM
162 if (win->m_ic)
163 gdk_im_end();
164 #endif
165
166 #if 0
167 #ifdef wxUSE_CARET
168 // caret needs to be informed about focus change
169 wxCaret *caret = win->GetCaret();
170 if ( caret )
171 {
172 caret->OnKillFocus();
173 }
174 #endif // wxUSE_CARET
175 #endif
176
177 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
178 event.SetEventObject( win );
179
180 if (win->GetEventHandler()->ProcessEvent( event ))
181 {
182 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus_out_event" );
183 return TRUE;
184 }
185
186 return FALSE;
187 }
188
189 //-----------------------------------------------------------------------------
190 // wxTextCtrl
191 //-----------------------------------------------------------------------------
192
193 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
194
195 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
196 EVT_CHAR(wxTextCtrl::OnChar)
197
198 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
199 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
200 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
201 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
202 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
203
204 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
205 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
206 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
207 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
208 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
209 END_EVENT_TABLE()
210
211 void wxTextCtrl::Init()
212 {
213 m_modified = FALSE;
214 m_updateFont = FALSE;
215 m_text =
216 m_vScrollbar = (GtkWidget *)NULL;
217 }
218
219 wxTextCtrl::wxTextCtrl( wxWindow *parent,
220 wxWindowID id,
221 const wxString &value,
222 const wxPoint &pos,
223 const wxSize &size,
224 long style,
225 const wxValidator& validator,
226 const wxString &name )
227 {
228 Init();
229
230 Create( parent, id, value, pos, size, style, validator, name );
231 }
232
233 bool wxTextCtrl::Create( wxWindow *parent,
234 wxWindowID id,
235 const wxString &value,
236 const wxPoint &pos,
237 const wxSize &size,
238 long style,
239 const wxValidator& validator,
240 const wxString &name )
241 {
242 m_needParent = TRUE;
243 m_acceptsFocus = TRUE;
244
245 if (!PreCreation( parent, pos, size ) ||
246 !CreateBase( parent, id, pos, size, style, validator, name ))
247 {
248 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
249 return FALSE;
250 }
251
252
253 m_vScrollbarVisible = FALSE;
254
255 bool multi_line = (style & wxTE_MULTILINE) != 0;
256 if (multi_line)
257 {
258 #if (GTK_MINOR_VERSION > 2)
259 /* a multi-line edit control: create a vertical scrollbar by default and
260 horizontal if requested */
261 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
262 #else
263 bool bHasHScrollbar = FALSE;
264 #endif
265
266 /* create our control ... */
267 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
268
269 /* ... and put into the upper left hand corner of the table */
270 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
271 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
272 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
273 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
274 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
275 0, 0);
276
277 /* always wrap words */
278 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
279
280 #if (GTK_MINOR_VERSION > 2)
281 /* put the horizontal scrollbar in the lower left hand corner */
282 if (bHasHScrollbar)
283 {
284 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
285 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
286 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
287 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
288 GTK_FILL,
289 0, 0);
290 gtk_widget_show(hscrollbar);
291
292 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
293 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
294 }
295 #endif
296
297 /* finally, put the vertical scrollbar in the upper right corner */
298 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
299 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
300 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
301 GTK_FILL,
302 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
303 0, 0);
304 }
305 else
306 {
307 /* a single-line text control: no need for scrollbars */
308 m_widget =
309 m_text = gtk_entry_new();
310 }
311
312 m_parent->DoAddChild( this );
313
314 PostCreation();
315
316 SetFont( parent->GetFont() );
317
318 wxSize size_best( DoGetBestSize() );
319 wxSize new_size( size );
320 if (new_size.x == -1)
321 new_size.x = size_best.x;
322 if (new_size.y == -1)
323 new_size.y = size_best.y;
324 if ((new_size.x != size.x) || (new_size.y != size.y))
325 SetSize( new_size.x, new_size.y );
326
327 if (multi_line)
328 gtk_widget_show(m_text);
329
330 if (multi_line)
331 {
332 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
333 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
334
335 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text)), "focus_in_event",
336 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback), (gpointer)this );
337
338 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text)), "focus_out_event",
339 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback), (gpointer)this );
340 }
341 else
342 {
343 gtk_signal_connect( GTK_OBJECT(m_text), "focus_in_event",
344 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback), (gpointer)this );
345
346 gtk_signal_connect( GTK_OBJECT(m_text), "focus_out_event",
347 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback), (gpointer)this );
348 }
349
350 if (!value.IsEmpty())
351 {
352 gint tmp = 0;
353
354 #if GTK_MINOR_VERSION == 0
355 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
356 // gtk_editable_insert_text()
357 gtk_widget_realize(m_text);
358 #endif // GTK 1.0
359
360 #if wxUSE_UNICODE
361 wxWX2MBbuf val = value.mbc_str();
362 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
363 #else // !Unicode
364 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
365 #endif // Unicode/!Unicode
366
367 if (multi_line)
368 {
369 /* bring editable's cursor uptodate. bug in GTK. */
370
371 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
372 }
373 }
374
375 if (style & wxTE_PASSWORD)
376 {
377 if (!multi_line)
378 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
379 }
380
381 if (style & wxTE_READONLY)
382 {
383 if (!multi_line)
384 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
385 }
386 else
387 {
388 if (multi_line)
389 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
390 }
391
392 /* we want to be notified about text changes */
393 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
394 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
395
396 /* we don't set a valid background colour, because the window
397 manager should use a default one */
398 m_backgroundColour = wxColour();
399 SetForegroundColour( parent->GetForegroundColour() );
400
401 m_cursor = wxCursor( wxCURSOR_IBEAM );
402
403 Show( TRUE );
404
405 return TRUE;
406 }
407
408 void wxTextCtrl::CalculateScrollbar()
409 {
410 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
411
412 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
413
414 if (adj->upper - adj->page_size < 0.8)
415 {
416 if (m_vScrollbarVisible)
417 {
418 gtk_widget_hide( m_vScrollbar );
419 m_vScrollbarVisible = FALSE;
420 }
421 }
422 else
423 {
424 if (!m_vScrollbarVisible)
425 {
426 gtk_widget_show( m_vScrollbar );
427 m_vScrollbarVisible = TRUE;
428 }
429 }
430 }
431
432 wxString wxTextCtrl::GetValue() const
433 {
434 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
435
436 wxString tmp;
437 if (m_windowStyle & wxTE_MULTILINE)
438 {
439 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
440 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
441 tmp = wxString(text,*wxConvCurrent);
442 g_free( text );
443 }
444 else
445 {
446 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
447 }
448 return tmp;
449 }
450
451 void wxTextCtrl::SetValue( const wxString &value )
452 {
453 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
454
455 if (m_windowStyle & wxTE_MULTILINE)
456 {
457 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
458 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
459 len = 0;
460 #if wxUSE_UNICODE
461 wxWX2MBbuf tmpbuf = value.mbc_str();
462 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
463 #else
464 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
465 #endif
466 }
467 else
468 {
469 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
470 }
471
472 // GRG, Jun/2000: Changed this after a lot of discussion in
473 // the lists. wxWindows 2.2 will have a set of flags to
474 // customize this behaviour.
475 SetInsertionPoint(0);
476
477 m_modified = FALSE;
478 }
479
480 void wxTextCtrl::WriteText( const wxString &text )
481 {
482 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
483
484 if (text.IsEmpty()) return;
485
486 if (m_windowStyle & wxTE_MULTILINE)
487 {
488 /* this moves the cursor pos to behind the inserted text */
489 gint len = GTK_EDITABLE(m_text)->current_pos;
490
491 #if wxUSE_UNICODE
492 wxWX2MBbuf buf = text.mbc_str();
493 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
494 #else
495 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
496 #endif
497
498 /* bring editable's cursor uptodate. bug in GTK. */
499 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
500 }
501 else
502 {
503 /* this moves the cursor pos to behind the inserted text */
504 gint len = GTK_EDITABLE(m_text)->current_pos;
505 #if wxUSE_UNICODE
506 wxWX2MBbuf buf = text.mbc_str();
507 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
508 #else
509 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
510 #endif
511
512 /* bring editable's cursor uptodate. bug in GTK. */
513 GTK_EDITABLE(m_text)->current_pos += text.Len();
514
515 /* bring entry's cursor uptodate. bug in GTK. */
516 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
517 }
518 }
519
520 void wxTextCtrl::AppendText( const wxString &text )
521 {
522 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
523
524 if (text.IsEmpty()) return;
525
526 if (m_windowStyle & wxTE_MULTILINE)
527 {
528 bool hasSpecialAttributes = m_font.Ok() ||
529 m_foregroundColour.Ok();
530 if ( hasSpecialAttributes )
531 {
532 gtk_text_insert( GTK_TEXT(m_text),
533 m_font.GetInternalFont(),
534 m_foregroundColour.GetColor(),
535 m_backgroundColour.Ok() ?
536 m_backgroundColour.GetColor(): NULL,
537 text.mbc_str(), text.length());
538
539 }
540 else
541 {
542 /* we'll insert at the last position */
543 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
544 #if wxUSE_UNICODE
545 wxWX2MBbuf buf = text.mbc_str();
546 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len );
547 #else
548 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
549 #endif
550 }
551
552 /* bring editable's cursor uptodate. bug in GTK. */
553 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
554 }
555 else
556 {
557 gtk_entry_append_text( GTK_ENTRY(m_text), text.mbc_str() );
558 }
559 }
560
561 wxString wxTextCtrl::GetLineText( long lineNo ) const
562 {
563 if (m_windowStyle & wxTE_MULTILINE)
564 {
565 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
566 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
567
568 if (text)
569 {
570 wxString buf(wxT(""));
571 long i;
572 int currentLine = 0;
573 for (i = 0; currentLine != lineNo && text[i]; i++ )
574 if (text[i] == '\n')
575 currentLine++;
576 // Now get the text
577 int j;
578 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
579 buf += text[i];
580
581 g_free( text );
582 return buf;
583 }
584 else
585 return wxEmptyString;
586 }
587 else
588 {
589 if (lineNo == 0) return GetValue();
590 return wxEmptyString;
591 }
592 }
593
594 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
595 {
596 /* If you implement this, don't forget to update the documentation!
597 * (file docs/latex/wx/text.tex) */
598 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
599 }
600
601 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
602 {
603 if ( m_windowStyle & wxTE_MULTILINE )
604 {
605 wxString text = GetValue();
606
607 // cast to prevent warning. But pos really should've been unsigned.
608 if( (unsigned long)pos > text.Len() )
609 return FALSE;
610
611 *x=0; // First Col
612 *y=0; // First Line
613
614 const wxChar* stop = text.c_str() + pos;
615 for ( const wxChar *p = text.c_str(); p < stop; p++ )
616 {
617 if (*p == wxT('\n'))
618 {
619 (*y)++;
620 *x=0;
621 }
622 else
623 (*x)++;
624 }
625 }
626 else // single line control
627 {
628 if ( pos <= GTK_ENTRY(m_text)->text_length )
629 {
630 *y = 0;
631 *x = pos;
632 }
633 else
634 {
635 // index out of bounds
636 return FALSE;
637 }
638 }
639
640 return TRUE;
641 }
642
643 long wxTextCtrl::XYToPosition(long x, long y ) const
644 {
645 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
646
647 long pos=0;
648 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
649
650 pos += x;
651 return pos;
652 }
653
654 int wxTextCtrl::GetLineLength(long lineNo) const
655 {
656 wxString str = GetLineText (lineNo);
657 return (int) str.Length();
658 }
659
660 int wxTextCtrl::GetNumberOfLines() const
661 {
662 if (m_windowStyle & wxTE_MULTILINE)
663 {
664 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
665 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
666
667 if (text)
668 {
669 int currentLine = 0;
670 for (int i = 0; i < len; i++ )
671 {
672 if (text[i] == '\n')
673 currentLine++;
674 }
675 g_free( text );
676
677 // currentLine is 0 based, add 1 to get number of lines
678 return currentLine + 1;
679 }
680 else
681 {
682 return 0;
683 }
684 }
685 else
686 {
687 return 1;
688 }
689 }
690
691 void wxTextCtrl::SetInsertionPoint( long pos )
692 {
693 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
694
695 if (m_windowStyle & wxTE_MULTILINE)
696 {
697 /* seems to be broken in GTK 1.0.X:
698 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
699
700 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
701 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
702
703 /* we fake a set_point by inserting and deleting. as the user
704 isn't supposed to get to know about thos non-sense, we
705 disconnect so that no events are sent to the user program. */
706
707 gint tmp = (gint)pos;
708 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
709 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
710
711 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
712 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
713
714 /* bring editable's cursor uptodate. another bug in GTK. */
715
716 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
717 }
718 else
719 {
720 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
721
722 /* bring editable's cursor uptodate. bug in GTK. */
723
724 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
725 }
726 }
727
728 void wxTextCtrl::SetInsertionPointEnd()
729 {
730 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
731
732 if (m_windowStyle & wxTE_MULTILINE)
733 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
734 else
735 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
736 }
737
738 void wxTextCtrl::SetEditable( bool editable )
739 {
740 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
741
742 if (m_windowStyle & wxTE_MULTILINE)
743 gtk_text_set_editable( GTK_TEXT(m_text), editable );
744 else
745 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
746 }
747
748 bool wxTextCtrl::Enable( bool enable )
749 {
750 if (!wxWindowBase::Enable(enable))
751 {
752 // nothing to do
753 return FALSE;
754 }
755
756 if (m_windowStyle & wxTE_MULTILINE)
757 {
758 gtk_text_set_editable( GTK_TEXT(m_text), enable );
759
760 // If we have a custom background colour, we use this colour in both
761 // disabled and enabled mode, or we end up with a different colour under the
762 // text.
763 wxColour oldColour = GetBackgroundColour();
764 if (oldColour.Ok())
765 {
766 // Need to set twice or it'll optimize the useful stuff out
767 if (oldColour == * wxWHITE)
768 SetBackgroundColour(*wxBLACK);
769 else
770 SetBackgroundColour(*wxWHITE);
771 SetBackgroundColour(oldColour);
772 }
773 }
774 else
775 {
776 gtk_widget_set_sensitive( m_text, enable );
777 }
778
779 return TRUE;
780 }
781
782 void wxTextCtrl::DiscardEdits()
783 {
784 m_modified = FALSE;
785 }
786
787 void wxTextCtrl::SetSelection( long from, long to )
788 {
789 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
790
791 if ( (m_windowStyle & wxTE_MULTILINE) &&
792 !GTK_TEXT(m_text)->line_start_cache )
793 {
794 // tell the programmer that it didn't work
795 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
796 return;
797 }
798
799 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
800 }
801
802 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
803 {
804 // SetInsertionPoint( pos );
805 }
806
807 long wxTextCtrl::GetInsertionPoint() const
808 {
809 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
810
811 return (long) GTK_EDITABLE(m_text)->current_pos;
812 }
813
814 long wxTextCtrl::GetLastPosition() const
815 {
816 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
817
818 int pos = 0;
819 if (m_windowStyle & wxTE_MULTILINE)
820 pos = gtk_text_get_length( GTK_TEXT(m_text) );
821 else
822 pos = GTK_ENTRY(m_text)->text_length;
823
824 return (long)pos;
825 }
826
827 void wxTextCtrl::Remove( long from, long to )
828 {
829 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
830
831 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
832 }
833
834 void wxTextCtrl::Replace( long from, long to, const wxString &value )
835 {
836 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
837
838 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
839
840 if (!value.IsEmpty())
841 {
842 gint pos = (gint)from;
843 #if wxUSE_UNICODE
844 wxWX2MBbuf buf = value.mbc_str();
845 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
846 #else
847 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
848 #endif
849 }
850 }
851
852 void wxTextCtrl::Cut()
853 {
854 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
855
856 #if (GTK_MINOR_VERSION > 0)
857 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
858 #else
859 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
860 #endif
861 }
862
863 void wxTextCtrl::Copy()
864 {
865 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
866
867 #if (GTK_MINOR_VERSION > 0)
868 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
869 #else
870 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
871 #endif
872 }
873
874 void wxTextCtrl::Paste()
875 {
876 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
877
878 #if (GTK_MINOR_VERSION > 0)
879 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
880 #else
881 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
882 #endif
883 }
884
885 bool wxTextCtrl::CanCopy() const
886 {
887 // Can copy if there's a selection
888 long from, to;
889 GetSelection(& from, & to);
890 return (from != to) ;
891 }
892
893 bool wxTextCtrl::CanCut() const
894 {
895 // Can cut if there's a selection
896 long from, to;
897 GetSelection(& from, & to);
898 return (from != to) && (IsEditable());
899 }
900
901 bool wxTextCtrl::CanPaste() const
902 {
903 return IsEditable() ;
904 }
905
906 // Undo/redo
907 void wxTextCtrl::Undo()
908 {
909 // TODO
910 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
911 }
912
913 void wxTextCtrl::Redo()
914 {
915 // TODO
916 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
917 }
918
919 bool wxTextCtrl::CanUndo() const
920 {
921 // TODO
922 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
923 return FALSE;
924 }
925
926 bool wxTextCtrl::CanRedo() const
927 {
928 // TODO
929 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
930 return FALSE;
931 }
932
933 // If the return values from and to are the same, there is no
934 // selection.
935 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
936 {
937 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
938
939 long from, to;
940 if ( !(GTK_EDITABLE(m_text)->has_selection) )
941 {
942 from =
943 to = GetInsertionPoint();
944 }
945 else // got selection
946 {
947 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
948 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
949
950 if ( from > to )
951 {
952 // exchange them to be compatible with wxMSW
953 long tmp = from;
954 from = to;
955 to = tmp;
956 }
957 }
958
959 if ( fromOut )
960 *fromOut = from;
961 if ( toOut )
962 *toOut = to;
963 }
964
965 bool wxTextCtrl::IsEditable() const
966 {
967 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
968
969 return GTK_EDITABLE(m_text)->editable;
970 }
971
972 bool wxTextCtrl::IsModified() const
973 {
974 return m_modified;
975 }
976
977 void wxTextCtrl::Clear()
978 {
979 SetValue( wxT("") );
980 }
981
982 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
983 {
984 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
985
986 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
987 {
988 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
989 event.SetEventObject(this);
990 event.SetString(GetValue());
991 if (GetEventHandler()->ProcessEvent(event)) return;
992 }
993
994 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
995 {
996 wxWindow *top_frame = m_parent;
997 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
998 top_frame = top_frame->GetParent();
999 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1000
1001 if (window->default_widget)
1002 {
1003 gtk_widget_activate (window->default_widget);
1004 return;
1005 }
1006 }
1007
1008 key_event.Skip();
1009 }
1010
1011 GtkWidget* wxTextCtrl::GetConnectWidget()
1012 {
1013 return GTK_WIDGET(m_text);
1014 }
1015
1016 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1017 {
1018 if (m_windowStyle & wxTE_MULTILINE)
1019 return (window == GTK_TEXT(m_text)->text_area);
1020 else
1021 return (window == GTK_ENTRY(m_text)->text_area);
1022 }
1023
1024 // the font will change for subsequent text insertiongs
1025 bool wxTextCtrl::SetFont( const wxFont &font )
1026 {
1027 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1028
1029 if ( !wxWindowBase::SetFont(font) )
1030 {
1031 // font didn't change, nothing to do
1032 return FALSE;
1033 }
1034
1035 if ( m_windowStyle & wxTE_MULTILINE )
1036 {
1037 m_updateFont = TRUE;
1038
1039 ChangeFontGlobally();
1040 }
1041
1042 return TRUE;
1043 }
1044
1045 void wxTextCtrl::ChangeFontGlobally()
1046 {
1047 // this method is very inefficient and hence should be called as rarely as
1048 // possible!
1049 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1050 _T("shouldn't be called for single line controls") );
1051
1052 wxString value = GetValue();
1053 if ( !value.IsEmpty() )
1054 {
1055 Clear();
1056 AppendText(value);
1057
1058 m_updateFont = FALSE;
1059 }
1060 }
1061
1062 void wxTextCtrl::UpdateFontIfNeeded()
1063 {
1064 if ( m_updateFont )
1065 ChangeFontGlobally();
1066 }
1067
1068 bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
1069 {
1070 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1071
1072 // doesn't work
1073 return FALSE;
1074 }
1075
1076 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1077 {
1078 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1079
1080 wxControl::SetBackgroundColour( colour );
1081
1082 if (!m_widget->window)
1083 return FALSE;
1084
1085 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
1086 if (sysbg.Red() == colour.Red() &&
1087 sysbg.Green() == colour.Green() &&
1088 sysbg.Blue() == colour.Blue())
1089 {
1090 return FALSE; // FIXME or TRUE?
1091 }
1092
1093 if (!m_backgroundColour.Ok())
1094 return FALSE;
1095
1096 if (m_windowStyle & wxTE_MULTILINE)
1097 {
1098 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1099 if (!window)
1100 return FALSE;
1101 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1102 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1103 gdk_window_clear( window );
1104 }
1105
1106 return TRUE;
1107 }
1108
1109 void wxTextCtrl::ApplyWidgetStyle()
1110 {
1111 if (m_windowStyle & wxTE_MULTILINE)
1112 {
1113 // how ?
1114 }
1115 else
1116 {
1117 SetWidgetStyle();
1118 gtk_widget_set_style( m_text, m_widgetStyle );
1119 }
1120 }
1121
1122 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1123 {
1124 Cut();
1125 }
1126
1127 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1128 {
1129 Copy();
1130 }
1131
1132 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1133 {
1134 Paste();
1135 }
1136
1137 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1138 {
1139 Undo();
1140 }
1141
1142 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1143 {
1144 Redo();
1145 }
1146
1147 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1148 {
1149 event.Enable( CanCut() );
1150 }
1151
1152 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1153 {
1154 event.Enable( CanCopy() );
1155 }
1156
1157 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1158 {
1159 event.Enable( CanPaste() );
1160 }
1161
1162 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1163 {
1164 event.Enable( CanUndo() );
1165 }
1166
1167 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1168 {
1169 event.Enable( CanRedo() );
1170 }
1171
1172 void wxTextCtrl::OnInternalIdle()
1173 {
1174 wxCursor cursor = m_cursor;
1175 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1176
1177 if (cursor.Ok())
1178 {
1179 GdkWindow *window = (GdkWindow*) NULL;
1180 if (HasFlag(wxTE_MULTILINE))
1181 window = GTK_TEXT(m_text)->text_area;
1182 else
1183 window = GTK_ENTRY(m_text)->text_area;
1184
1185 if (window)
1186 gdk_window_set_cursor( window, cursor.GetCursor() );
1187
1188 if (!g_globalCursor.Ok())
1189 cursor = *wxSTANDARD_CURSOR;
1190
1191 window = m_widget->window;
1192 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1193 gdk_window_set_cursor( window, cursor.GetCursor() );
1194 }
1195
1196 UpdateWindowUI();
1197 }
1198
1199 wxSize wxTextCtrl::DoGetBestSize() const
1200 {
1201 // FIXME should be different for multi-line controls...
1202 wxSize ret( wxControl::DoGetBestSize() );
1203 return wxSize(80, ret.y);
1204 }