1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textentry.cpp
3 // Purpose: wxTextEntry implementation for wxGTK
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/textentry.h"
31 #include "wx/gtk/private.h"
33 // ============================================================================
34 // signal handlers implementation
35 // ============================================================================
40 // "insert_text" handler for GtkEntry
42 wx_gtk_insert_text_callback(GtkEditable
*editable
,
43 const gchar
*new_text
,
48 // we should only be called if we have a max len limit at all
49 GtkEntry
*entry
= GTK_ENTRY (editable
);
51 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
53 // check that we don't overflow the max length limit
55 // FIXME: this doesn't work when we paste a string which is going to be
57 if ( entry
->text_length
== entry
->text_max_length
)
59 // we don't need to run the base class version at all
60 g_signal_stop_emission_by_name (editable
, "insert_text");
62 text
->SendMaxLenEvent();
68 // ============================================================================
69 // wxTextEntry implementation
70 // ============================================================================
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 void wxTextEntry::WriteText(const wxString
& value
)
78 GtkEditable
* const edit
= GetEditable();
80 // remove the selection if there is one and suppress the text change event
81 // generated by this: we only want to generate one event for this change,
84 EventsSuppressor
noevents(this);
85 gtk_editable_delete_selection(edit
);
88 // insert new text at the cursor position
89 gint len
= gtk_editable_get_position(edit
);
90 gtk_editable_insert_text
93 wxGTK_CONV_FONT(value
, GetEditableWindow()->GetFont()),
94 -1, // text: length: compute it using strlen()
95 &len
// will be updated to position after the text end
98 // and move cursor to the end of new text
99 gtk_editable_set_position(edit
, len
);
102 wxString
wxTextEntry::GetValue() const
104 const wxGtkString
value(gtk_editable_get_chars(GetEditable(), 0, -1));
106 return wxGTK_CONV_BACK_FONT(value
, GetEditableWindow()->GetFont());
109 void wxTextEntry::Remove(long from
, long to
)
111 gtk_editable_delete_text(GetEditable(), from
, to
);
114 // ----------------------------------------------------------------------------
115 // clipboard operations
116 // ----------------------------------------------------------------------------
118 void wxTextEntry::Copy()
120 gtk_editable_copy_clipboard(GetEditable());
123 void wxTextEntry::Cut()
125 gtk_editable_cut_clipboard(GetEditable());
128 void wxTextEntry::Paste()
130 gtk_editable_paste_clipboard(GetEditable());
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 void wxTextEntry::Undo()
139 // TODO: not implemented
142 void wxTextEntry::Redo()
144 // TODO: not implemented
147 bool wxTextEntry::CanUndo() const
152 bool wxTextEntry::CanRedo() const
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void wxTextEntry::SetInsertionPoint(long pos
)
163 gtk_editable_set_position(GetEditable(), pos
);
166 long wxTextEntry::GetInsertionPoint() const
168 return gtk_editable_get_position(GetEditable());
171 long wxTextEntry::GetLastPosition() const
173 // this can't be implemented for arbitrary GtkEditable so only do it for
175 GtkEntry
* const entry
= GTK_ENTRY(GetEditable());
177 return entry
? entry
->text_length
: - 1;
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 void wxTextEntry::SetSelection(long from
, long to
)
186 gtk_editable_select_region(GetEditable(), from
, to
);
189 void wxTextEntry::GetSelection(long *from
, long *to
) const
192 if ( gtk_editable_get_selection_bounds(GetEditable(), &start
, &end
) )
194 // the output must always be in order, although in GTK+ it isn't
204 // for compatibility with MSW return the empty selection at cursor
206 end
= GetInsertionPoint();
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 bool wxTextEntry::IsEditable() const
222 return gtk_editable_get_editable(GetEditable());
225 void wxTextEntry::SetEditable(bool editable
)
227 gtk_editable_set_editable(GetEditable(), editable
);
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 void wxTextEntry::SetMaxLength(unsigned long len
)
236 GtkEntry
* const entry
= GTK_ENTRY(GetEditable());
240 gtk_entry_set_max_length(entry
, len
);
242 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if we had
243 // tried to enter more text than allowed by max text length and the text
244 // wasn't really changed
246 // to detect this and generate TEXT_MAXLEN event instead of TEXT_CHANGED
247 // one in this case we also catch "insert_text" signal
249 // when max len is set to 0 we disconnect our handler as it means that we
250 // shouldn't check anything any more
257 G_CALLBACK(wx_gtk_insert_text_callback
),
261 else // no max length
263 g_signal_handlers_disconnect_by_func
266 (gpointer
)wx_gtk_insert_text_callback
,
272 void wxTextEntry::SendMaxLenEvent()
274 // remember that the next changed signal is to be ignored to avoid
275 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
276 //IgnoreNextTextUpdate();
278 wxWindow
* const win
= const_cast<wxWindow
*>(GetEditableWindow());
279 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
280 event
.SetEventObject(win
);
281 event
.SetString(GetValue());
282 win
->GetEventHandler()->ProcessEvent(event
);