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