]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
Added regex to the build. New .def file for .dll builds.
[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 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 = wxFindFocusedChild(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
400 wxColour colFg = parent->GetForegroundColour();
401 SetForegroundColour( colFg );
402
403 m_cursor = wxCursor( wxCURSOR_IBEAM );
404
405 // FIXME: is the bg colour correct here?
406 wxTextAttr attrDef( colFg,
407 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW),
408 parent->GetFont() );
409 SetDefaultStyle( attrDef );
410
411 Show( TRUE );
412
413 return TRUE;
414 }
415
416 void wxTextCtrl::CalculateScrollbar()
417 {
418 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
419
420 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
421
422 if (adj->upper - adj->page_size < 0.8)
423 {
424 if (m_vScrollbarVisible)
425 {
426 gtk_widget_hide( m_vScrollbar );
427 m_vScrollbarVisible = FALSE;
428 }
429 }
430 else
431 {
432 if (!m_vScrollbarVisible)
433 {
434 gtk_widget_show( m_vScrollbar );
435 m_vScrollbarVisible = TRUE;
436 }
437 }
438 }
439
440 wxString wxTextCtrl::GetValue() const
441 {
442 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
443
444 wxString tmp;
445 if (m_windowStyle & wxTE_MULTILINE)
446 {
447 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
448 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
449 tmp = wxString(text,*wxConvCurrent);
450 g_free( text );
451 }
452 else
453 {
454 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
455 }
456 return tmp;
457 }
458
459 void wxTextCtrl::SetValue( const wxString &value )
460 {
461 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
462
463 if (m_windowStyle & wxTE_MULTILINE)
464 {
465 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
466 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
467 len = 0;
468 #if wxUSE_UNICODE
469 wxWX2MBbuf tmpbuf = value.mbc_str();
470 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
471 #else
472 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
473 #endif
474 }
475 else
476 {
477 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
478 }
479
480 // GRG, Jun/2000: Changed this after a lot of discussion in
481 // the lists. wxWindows 2.2 will have a set of flags to
482 // customize this behaviour.
483 SetInsertionPoint(0);
484
485 m_modified = FALSE;
486 }
487
488 void wxTextCtrl::WriteText( const wxString &text )
489 {
490 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
491
492 if ( text.empty() )
493 return;
494
495 #if wxUSE_UNICODE
496 wxWX2MBbuf buf = text.mbc_str();
497 const char *txt = buf;
498 size_t txtlen = strlen(buf);
499 #else
500 const char *txt = text;
501 size_t txtlen = text.length();
502 #endif
503
504 if ( m_windowStyle & wxTE_MULTILINE )
505 {
506 /* this moves the cursor pos to behind the inserted text */
507 gint len = GTK_EDITABLE(m_text)->current_pos;
508
509 // if we have any special style, use it
510 if ( !m_defaultStyle.IsDefault() )
511 {
512 GdkFont *font = m_defaultStyle.HasFont()
513 ? m_defaultStyle.GetFont().GetInternalFont()
514 : NULL;
515
516 GdkColor *colFg = m_defaultStyle.HasTextColour()
517 ? m_defaultStyle.GetTextColour().GetColor()
518 : NULL;
519
520 GdkColor *colBg = m_defaultStyle.HasBackgroundColour()
521 ? m_defaultStyle.GetBackgroundColour().GetColor()
522 : NULL;
523
524 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
525 }
526 else // no style
527 {
528 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
529 }
530
531 /* bring editable's cursor uptodate. bug in GTK. */
532 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
533 }
534 else // single line
535 {
536 /* this moves the cursor pos to behind the inserted text */
537 gint len = GTK_EDITABLE(m_text)->current_pos;
538 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
539
540 /* bring editable's cursor uptodate. bug in GTK. */
541 GTK_EDITABLE(m_text)->current_pos += text.Len();
542
543 /* bring entry's cursor uptodate. bug in GTK. */
544 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
545 }
546 }
547
548 void wxTextCtrl::AppendText( const wxString &text )
549 {
550 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
551
552 if ( text.empty() )
553 return;
554
555 #if wxUSE_UNICODE
556 wxWX2MBbuf buf = text.mbc_str();
557 const char *txt = buf;
558 size_t txtlen = strlen(buf);
559 #else
560 const char *txt = text;
561 size_t txtlen = text.length();
562 #endif
563
564 if (m_windowStyle & wxTE_MULTILINE)
565 {
566 if ( !m_defaultStyle.IsDefault() )
567 {
568 wxFont font = m_defaultStyle.HasFont() ? m_defaultStyle.GetFont()
569 : m_font;
570 GdkFont *fnt = font.Ok() ? font.GetInternalFont() : NULL;
571
572 wxColour col = m_defaultStyle.HasTextColour()
573 ? m_defaultStyle.GetTextColour()
574 : m_foregroundColour;
575 GdkColor *colFg = col.Ok() ? col.GetColor() : NULL;
576
577 col = m_defaultStyle.HasBackgroundColour()
578 ? m_defaultStyle.GetBackgroundColour()
579 : m_backgroundColour;
580 GdkColor *colBg = col.Ok() ? col.GetColor() : NULL;
581
582 gtk_text_insert( GTK_TEXT(m_text), fnt, colFg, colBg, txt, txtlen );
583 }
584 else // no style
585 {
586 /* we'll insert at the last position */
587 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
588 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
589 }
590
591 /* bring editable's cursor uptodate. bug in GTK. */
592 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
593 }
594 else // single line
595 {
596 gtk_entry_append_text( GTK_ENTRY(m_text), txt );
597 }
598 }
599
600 wxString wxTextCtrl::GetLineText( long lineNo ) const
601 {
602 if (m_windowStyle & wxTE_MULTILINE)
603 {
604 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
605 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
606
607 if (text)
608 {
609 wxString buf(wxT(""));
610 long i;
611 int currentLine = 0;
612 for (i = 0; currentLine != lineNo && text[i]; i++ )
613 if (text[i] == '\n')
614 currentLine++;
615 // Now get the text
616 int j;
617 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
618 buf += text[i];
619
620 g_free( text );
621 return buf;
622 }
623 else
624 return wxEmptyString;
625 }
626 else
627 {
628 if (lineNo == 0) return GetValue();
629 return wxEmptyString;
630 }
631 }
632
633 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
634 {
635 /* If you implement this, don't forget to update the documentation!
636 * (file docs/latex/wx/text.tex) */
637 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
638 }
639
640 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
641 {
642 if ( m_windowStyle & wxTE_MULTILINE )
643 {
644 wxString text = GetValue();
645
646 // cast to prevent warning. But pos really should've been unsigned.
647 if( (unsigned long)pos > text.Len() )
648 return FALSE;
649
650 *x=0; // First Col
651 *y=0; // First Line
652
653 const wxChar* stop = text.c_str() + pos;
654 for ( const wxChar *p = text.c_str(); p < stop; p++ )
655 {
656 if (*p == wxT('\n'))
657 {
658 (*y)++;
659 *x=0;
660 }
661 else
662 (*x)++;
663 }
664 }
665 else // single line control
666 {
667 if ( pos <= GTK_ENTRY(m_text)->text_length )
668 {
669 *y = 0;
670 *x = pos;
671 }
672 else
673 {
674 // index out of bounds
675 return FALSE;
676 }
677 }
678
679 return TRUE;
680 }
681
682 long wxTextCtrl::XYToPosition(long x, long y ) const
683 {
684 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
685
686 long pos=0;
687 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
688
689 pos += x;
690 return pos;
691 }
692
693 int wxTextCtrl::GetLineLength(long lineNo) const
694 {
695 wxString str = GetLineText (lineNo);
696 return (int) str.Length();
697 }
698
699 int wxTextCtrl::GetNumberOfLines() const
700 {
701 if (m_windowStyle & wxTE_MULTILINE)
702 {
703 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
704 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
705
706 if (text)
707 {
708 int currentLine = 0;
709 for (int i = 0; i < len; i++ )
710 {
711 if (text[i] == '\n')
712 currentLine++;
713 }
714 g_free( text );
715
716 // currentLine is 0 based, add 1 to get number of lines
717 return currentLine + 1;
718 }
719 else
720 {
721 return 0;
722 }
723 }
724 else
725 {
726 return 1;
727 }
728 }
729
730 void wxTextCtrl::SetInsertionPoint( long pos )
731 {
732 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
733
734 if (m_windowStyle & wxTE_MULTILINE)
735 {
736 /* seems to be broken in GTK 1.0.X:
737 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
738
739 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
740 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
741
742 /* we fake a set_point by inserting and deleting. as the user
743 isn't supposed to get to know about thos non-sense, we
744 disconnect so that no events are sent to the user program. */
745
746 gint tmp = (gint)pos;
747 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
748 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
749
750 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
751 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
752
753 /* bring editable's cursor uptodate. another bug in GTK. */
754
755 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
756 }
757 else
758 {
759 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
760
761 /* bring editable's cursor uptodate. bug in GTK. */
762
763 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
764 }
765 }
766
767 void wxTextCtrl::SetInsertionPointEnd()
768 {
769 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
770
771 if (m_windowStyle & wxTE_MULTILINE)
772 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
773 else
774 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
775 }
776
777 void wxTextCtrl::SetEditable( bool editable )
778 {
779 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
780
781 if (m_windowStyle & wxTE_MULTILINE)
782 gtk_text_set_editable( GTK_TEXT(m_text), editable );
783 else
784 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
785 }
786
787 bool wxTextCtrl::Enable( bool enable )
788 {
789 if (!wxWindowBase::Enable(enable))
790 {
791 // nothing to do
792 return FALSE;
793 }
794
795 if (m_windowStyle & wxTE_MULTILINE)
796 {
797 gtk_text_set_editable( GTK_TEXT(m_text), enable );
798 OnParentEnable(enable);
799 }
800 else
801 {
802 gtk_widget_set_sensitive( m_text, enable );
803 }
804
805 return TRUE;
806 }
807
808 // wxGTK-specific: called recursively by Enable,
809 // to give widgets an oppprtunity to correct their colours after they
810 // have been changed by Enable
811 void wxTextCtrl::OnParentEnable( bool enable )
812 {
813 // If we have a custom background colour, we use this colour in both
814 // disabled and enabled mode, or we end up with a different colour under the
815 // text.
816 wxColour oldColour = GetBackgroundColour();
817 if (oldColour.Ok())
818 {
819 // Need to set twice or it'll optimize the useful stuff out
820 if (oldColour == * wxWHITE)
821 SetBackgroundColour(*wxBLACK);
822 else
823 SetBackgroundColour(*wxWHITE);
824 SetBackgroundColour(oldColour);
825 }
826 }
827
828 void wxTextCtrl::DiscardEdits()
829 {
830 m_modified = FALSE;
831 }
832
833 void wxTextCtrl::SetSelection( long from, long to )
834 {
835 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
836
837 if ( (m_windowStyle & wxTE_MULTILINE) &&
838 !GTK_TEXT(m_text)->line_start_cache )
839 {
840 // tell the programmer that it didn't work
841 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
842 return;
843 }
844
845 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
846 }
847
848 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
849 {
850 // SetInsertionPoint( pos );
851 }
852
853 long wxTextCtrl::GetInsertionPoint() const
854 {
855 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
856
857 return (long) GTK_EDITABLE(m_text)->current_pos;
858 }
859
860 long wxTextCtrl::GetLastPosition() const
861 {
862 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
863
864 int pos = 0;
865 if (m_windowStyle & wxTE_MULTILINE)
866 pos = gtk_text_get_length( GTK_TEXT(m_text) );
867 else
868 pos = GTK_ENTRY(m_text)->text_length;
869
870 return (long)pos;
871 }
872
873 void wxTextCtrl::Remove( long from, long to )
874 {
875 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
876
877 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
878 }
879
880 void wxTextCtrl::Replace( long from, long to, const wxString &value )
881 {
882 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
883
884 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
885
886 if (!value.IsEmpty())
887 {
888 gint pos = (gint)from;
889 #if wxUSE_UNICODE
890 wxWX2MBbuf buf = value.mbc_str();
891 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
892 #else
893 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
894 #endif
895 }
896 }
897
898 void wxTextCtrl::Cut()
899 {
900 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
901
902 #if (GTK_MINOR_VERSION > 0)
903 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
904 #else
905 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
906 #endif
907 }
908
909 void wxTextCtrl::Copy()
910 {
911 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
912
913 #if (GTK_MINOR_VERSION > 0)
914 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
915 #else
916 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
917 #endif
918 }
919
920 void wxTextCtrl::Paste()
921 {
922 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
923
924 #if (GTK_MINOR_VERSION > 0)
925 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
926 #else
927 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
928 #endif
929 }
930
931 // Undo/redo
932 void wxTextCtrl::Undo()
933 {
934 // TODO
935 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
936 }
937
938 void wxTextCtrl::Redo()
939 {
940 // TODO
941 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
942 }
943
944 bool wxTextCtrl::CanUndo() const
945 {
946 // TODO
947 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
948 return FALSE;
949 }
950
951 bool wxTextCtrl::CanRedo() const
952 {
953 // TODO
954 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
955 return FALSE;
956 }
957
958 // If the return values from and to are the same, there is no
959 // selection.
960 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
961 {
962 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
963
964 long from, to;
965 if ( !(GTK_EDITABLE(m_text)->has_selection) )
966 {
967 from =
968 to = GetInsertionPoint();
969 }
970 else // got selection
971 {
972 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
973 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
974
975 if ( from > to )
976 {
977 // exchange them to be compatible with wxMSW
978 long tmp = from;
979 from = to;
980 to = tmp;
981 }
982 }
983
984 if ( fromOut )
985 *fromOut = from;
986 if ( toOut )
987 *toOut = to;
988 }
989
990 bool wxTextCtrl::IsEditable() const
991 {
992 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
993
994 return GTK_EDITABLE(m_text)->editable;
995 }
996
997 bool wxTextCtrl::IsModified() const
998 {
999 return m_modified;
1000 }
1001
1002 void wxTextCtrl::Clear()
1003 {
1004 SetValue( wxT("") );
1005 }
1006
1007 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1008 {
1009 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1010
1011 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1012 {
1013 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1014 event.SetEventObject(this);
1015 event.SetString(GetValue());
1016 if (GetEventHandler()->ProcessEvent(event)) return;
1017 }
1018
1019 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1020 {
1021 wxWindow *top_frame = m_parent;
1022 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1023 top_frame = top_frame->GetParent();
1024 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1025
1026 if (window->default_widget)
1027 {
1028 gtk_widget_activate (window->default_widget);
1029 return;
1030 }
1031 }
1032
1033 key_event.Skip();
1034 }
1035
1036 GtkWidget* wxTextCtrl::GetConnectWidget()
1037 {
1038 return GTK_WIDGET(m_text);
1039 }
1040
1041 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1042 {
1043 if (m_windowStyle & wxTE_MULTILINE)
1044 return (window == GTK_TEXT(m_text)->text_area);
1045 else
1046 return (window == GTK_ENTRY(m_text)->text_area);
1047 }
1048
1049 // the font will change for subsequent text insertiongs
1050 bool wxTextCtrl::SetFont( const wxFont &font )
1051 {
1052 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1053
1054 if ( !wxWindowBase::SetFont(font) )
1055 {
1056 // font didn't change, nothing to do
1057 return FALSE;
1058 }
1059
1060 if ( m_windowStyle & wxTE_MULTILINE )
1061 {
1062 m_updateFont = TRUE;
1063
1064 m_defaultStyle.SetFont(font);
1065
1066 ChangeFontGlobally();
1067 }
1068
1069 return TRUE;
1070 }
1071
1072 void wxTextCtrl::ChangeFontGlobally()
1073 {
1074 // this method is very inefficient and hence should be called as rarely as
1075 // possible!
1076 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1077 _T("shouldn't be called for single line controls") );
1078
1079 wxString value = GetValue();
1080 if ( !value.IsEmpty() )
1081 {
1082 Clear();
1083 AppendText(value);
1084
1085 m_updateFont = FALSE;
1086 }
1087 }
1088
1089 void wxTextCtrl::UpdateFontIfNeeded()
1090 {
1091 if ( m_updateFont )
1092 ChangeFontGlobally();
1093 }
1094
1095 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1096 {
1097 if ( !wxControl::SetForegroundColour(colour) )
1098 return FALSE;
1099
1100 // update default fg colour too
1101 m_defaultStyle.SetTextColour(colour);
1102
1103 return TRUE;
1104 }
1105
1106 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1107 {
1108 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1109
1110 if ( !wxControl::SetBackgroundColour( colour ) )
1111 return FALSE;
1112
1113 if (!m_widget->window)
1114 return FALSE;
1115
1116 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
1117 if (sysbg.Red() == colour.Red() &&
1118 sysbg.Green() == colour.Green() &&
1119 sysbg.Blue() == colour.Blue())
1120 {
1121 return FALSE; // FIXME or TRUE?
1122 }
1123
1124 if (!m_backgroundColour.Ok())
1125 return FALSE;
1126
1127 if (m_windowStyle & wxTE_MULTILINE)
1128 {
1129 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1130 if (!window)
1131 return FALSE;
1132 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1133 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1134 gdk_window_clear( window );
1135 }
1136
1137 // change active background color too
1138 m_defaultStyle.SetBackgroundColour( colour );
1139
1140 return TRUE;
1141 }
1142
1143 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr &style )
1144 {
1145 /* VERY dirty way to do that - removes the required text and re-adds it
1146 with styling (FIXME) */
1147 if ( m_windowStyle & wxTE_MULTILINE )
1148 {
1149 if ( style.IsDefault() )
1150 {
1151 // nothing to do
1152 return TRUE;
1153 }
1154
1155 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1156
1157 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1158 _T("invalid range in wxTextCtrl::SetStyle") );
1159
1160 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1161 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1162 wxString tmp(text,*wxConvCurrent);
1163 g_free( text );
1164
1165 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1166 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1167
1168 #if wxUSE_UNICODE
1169 wxWX2MBbuf buf = tmp.mbc_str();
1170 const char *txt = buf;
1171 size_t txtlen = strlen(buf);
1172 #else
1173 const char *txt = tmp;
1174 size_t txtlen = tmp.length();
1175 #endif
1176
1177 GdkFont *font = style.HasFont()
1178 ? style.GetFont().GetInternalFont()
1179 : NULL;
1180
1181 GdkColor *colFg = style.HasTextColour()
1182 ? style.GetTextColour().GetColor()
1183 : NULL;
1184
1185 GdkColor *colBg = style.HasBackgroundColour()
1186 ? style.GetBackgroundColour().GetColor()
1187 : NULL;
1188
1189 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
1190
1191 /* does not seem to help under GTK+ 1.2 !!!
1192 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1193 SetInsertionPoint( old_pos );
1194 return TRUE;
1195 }
1196 else // singe line
1197 {
1198 // cannot do this for GTK+'s Entry widget
1199 return FALSE;
1200 }
1201 }
1202
1203 void wxTextCtrl::ApplyWidgetStyle()
1204 {
1205 if (m_windowStyle & wxTE_MULTILINE)
1206 {
1207 // how ?
1208 }
1209 else
1210 {
1211 SetWidgetStyle();
1212 gtk_widget_set_style( m_text, m_widgetStyle );
1213 }
1214 }
1215
1216 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1217 {
1218 Cut();
1219 }
1220
1221 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1222 {
1223 Copy();
1224 }
1225
1226 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1227 {
1228 Paste();
1229 }
1230
1231 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1232 {
1233 Undo();
1234 }
1235
1236 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1237 {
1238 Redo();
1239 }
1240
1241 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1242 {
1243 event.Enable( CanCut() );
1244 }
1245
1246 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1247 {
1248 event.Enable( CanCopy() );
1249 }
1250
1251 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1252 {
1253 event.Enable( CanPaste() );
1254 }
1255
1256 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1257 {
1258 event.Enable( CanUndo() );
1259 }
1260
1261 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1262 {
1263 event.Enable( CanRedo() );
1264 }
1265
1266 void wxTextCtrl::OnInternalIdle()
1267 {
1268 wxCursor cursor = m_cursor;
1269 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1270
1271 if (cursor.Ok())
1272 {
1273 GdkWindow *window = (GdkWindow*) NULL;
1274 if (HasFlag(wxTE_MULTILINE))
1275 window = GTK_TEXT(m_text)->text_area;
1276 else
1277 window = GTK_ENTRY(m_text)->text_area;
1278
1279 if (window)
1280 gdk_window_set_cursor( window, cursor.GetCursor() );
1281
1282 if (!g_globalCursor.Ok())
1283 cursor = *wxSTANDARD_CURSOR;
1284
1285 window = m_widget->window;
1286 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1287 gdk_window_set_cursor( window, cursor.GetCursor() );
1288 }
1289
1290 UpdateWindowUI();
1291 }
1292
1293 wxSize wxTextCtrl::DoGetBestSize() const
1294 {
1295 // FIXME should be different for multi-line controls...
1296 wxSize ret( wxControl::DoGetBestSize() );
1297 return wxSize(80, ret.y);
1298 }
1299
1300 // ----------------------------------------------------------------------------
1301 // freeze/thaw
1302 // ----------------------------------------------------------------------------
1303
1304 void wxTextCtrl::Freeze()
1305 {
1306 if ( HasFlag(wxTE_MULTILINE) )
1307 {
1308 gtk_text_freeze(GTK_TEXT(m_text));
1309 }
1310 }
1311
1312 void wxTextCtrl::Thaw()
1313 {
1314 if ( HasFlag(wxTE_MULTILINE) )
1315 {
1316 GTK_TEXT(m_text)->vadj->value = 0.0;
1317
1318 gtk_text_thaw(GTK_TEXT(m_text));
1319 }
1320 }
1321
1322 // ----------------------------------------------------------------------------
1323 // scrolling
1324 // ----------------------------------------------------------------------------
1325
1326 GtkAdjustment *wxTextCtrl::GetVAdj() const
1327 {
1328 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1329 }
1330
1331 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1332 {
1333 float value = adj->value + diff;
1334
1335 if ( value < 0 )
1336 value = 0;
1337
1338 float upper = adj->upper - adj->page_size;
1339 if ( value > upper )
1340 value = upper;
1341
1342 // did we noticeably change the scroll position?
1343 if ( fabs(adj->value - value) < 0.2 )
1344 {
1345 // well, this is what Robert does in wxScrollBar, so it must be good...
1346 return FALSE;
1347 }
1348
1349 adj->value = value;
1350
1351 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1352
1353 return TRUE;
1354 }
1355
1356 bool wxTextCtrl::ScrollLines(int lines)
1357 {
1358 GtkAdjustment *adj = GetVAdj();
1359 if ( !adj )
1360 return FALSE;
1361
1362 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1363 static const int KEY_SCROLL_PIXELS = 10;
1364
1365 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1366 }
1367
1368 bool wxTextCtrl::ScrollPages(int pages)
1369 {
1370 GtkAdjustment *adj = GetVAdj();
1371 if ( !adj )
1372 return FALSE;
1373
1374 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1375 }
1376