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