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