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"
26 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
29 #include "wx/textentry.h"
30 #include "wx/window.h"
31 #include "wx/textctrl.h"
35 #include "wx/gtk/private.h"
36 #include "wx/gtk/private/gtk2-compat.h"
38 // ============================================================================
39 // signal handlers implementation
40 // ============================================================================
45 // "insert_text" handler for GtkEntry
47 wx_gtk_insert_text_callback(GtkEditable
*editable
,
48 const gchar
* WXUNUSED(new_text
),
49 gint
WXUNUSED(new_text_length
),
50 gint
* WXUNUSED(position
),
53 // we should only be called if we have a max len limit at all
54 GtkEntry
*entry
= GTK_ENTRY (editable
);
56 const int text_length
= gtk_entry_get_text_length(entry
);
57 #if GTK_CHECK_VERSION(3,0,0) || defined(GSEAL_ENABLE)
58 const int text_max_length
= gtk_entry_buffer_get_max_length(gtk_entry_get_buffer(entry
));
60 const int text_max_length
= entry
->text_max_length
;
62 wxCHECK_RET(text_max_length
, "shouldn't be called");
64 // check that we don't overflow the max length limit
66 // FIXME: this doesn't work when we paste a string which is going to be
68 if (text_length
== text_max_length
)
70 // we don't need to run the base class version at all
71 g_signal_stop_emission_by_name (editable
, "insert_text");
73 text
->SendMaxLenEvent();
79 // ============================================================================
80 // wxTextEntry implementation
81 // ============================================================================
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 void wxTextEntry::WriteText(const wxString
& value
)
89 GtkEditable
* const edit
= GetEditable();
91 // remove the selection if there is one and suppress the text change event
92 // generated by this: we only want to generate one event for this change,
95 EventsSuppressor
noevents(this);
96 gtk_editable_delete_selection(edit
);
99 // insert new text at the cursor position
100 gint len
= gtk_editable_get_position(edit
);
101 gtk_editable_insert_text
104 wxGTK_CONV_FONT(value
, GetEditableWindow()->GetFont()),
105 -1, // text: length: compute it using strlen()
106 &len
// will be updated to position after the text end
109 // and move cursor to the end of new text
110 gtk_editable_set_position(edit
, len
);
113 void wxTextEntry::DoSetValue(const wxString
& value
, int flags
)
115 if (value
!= GetValue())
117 // use Remove() rather than SelectAll() to avoid unnecessary clipboard
118 // operations, and prevent triggering an apparent bug in GTK which
119 // causes the the subsequent WriteText() to append rather than overwrite
121 EventsSuppressor
noevents(this);
124 EventsSuppressor
noeventsIf(this, !(flags
& SetValue_SendEvent
));
127 else if (flags
& SetValue_SendEvent
)
128 SendTextUpdatedEvent(GetEditableWindow());
130 SetInsertionPoint(0);
133 wxString
wxTextEntry::DoGetValue() const
135 const wxGtkString
value(gtk_editable_get_chars(GetEditable(), 0, -1));
137 return wxGTK_CONV_BACK_FONT(value
,
138 const_cast<wxTextEntry
*>(this)->GetEditableWindow()->GetFont());
141 void wxTextEntry::Remove(long from
, long to
)
143 gtk_editable_delete_text(GetEditable(), from
, to
);
146 // ----------------------------------------------------------------------------
147 // clipboard operations
148 // ----------------------------------------------------------------------------
150 void wxTextEntry::Copy()
152 gtk_editable_copy_clipboard(GetEditable());
155 void wxTextEntry::Cut()
157 gtk_editable_cut_clipboard(GetEditable());
160 void wxTextEntry::Paste()
162 gtk_editable_paste_clipboard(GetEditable());
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 void wxTextEntry::Undo()
171 // TODO: not implemented
174 void wxTextEntry::Redo()
176 // TODO: not implemented
179 bool wxTextEntry::CanUndo() const
184 bool wxTextEntry::CanRedo() const
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 void wxTextEntry::SetInsertionPoint(long pos
)
195 gtk_editable_set_position(GetEditable(), pos
);
198 long wxTextEntry::GetInsertionPoint() const
200 return gtk_editable_get_position(GetEditable());
203 long wxTextEntry::GetLastPosition() const
205 // this can't be implemented for arbitrary GtkEditable so only do it for
208 GtkEntry
* entry
= (GtkEntry
*)GetEditable();
209 if (GTK_IS_ENTRY(entry
))
210 pos
= gtk_entry_get_text_length(entry
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 void wxTextEntry::SetSelection(long from
, long to
)
221 // in wx convention, (-1, -1) means the entire range but GTK+ translates -1
222 // (or any negative number for that matter) into last position so we need
223 // to translate manually
224 if ( from
== -1 && to
== -1 )
227 // for compatibility with MSW, exchange from and to parameters so that the
228 // insertion point is set to the start of the selection and not its end as
229 // GTK+ does by default
230 gtk_editable_select_region(GetEditable(), to
, from
);
233 // avoid reported problem with RHEL 5 GTK+ 2.10 where selection is reset by
234 // a clipboard callback, see #13277
235 if (gtk_check_version(2,12,0))
237 GtkEntry
* entry
= GTK_ENTRY(GetEditable());
239 to
= entry
->text_length
;
240 entry
->selection_bound
= to
;
245 void wxTextEntry::GetSelection(long *from
, long *to
) const
248 if ( gtk_editable_get_selection_bounds(GetEditable(), &start
, &end
) )
250 // the output must always be in order, although in GTK+ it isn't
260 // for compatibility with MSW return the empty selection at cursor
262 end
= GetInsertionPoint();
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString
& choices
)
278 GtkEntry
* const entry
= (GtkEntry
*)GetEditable();
279 wxCHECK_MSG(GTK_IS_ENTRY(entry
), false, "auto completion doesn't work with this control");
281 GtkListStore
* const store
= gtk_list_store_new(1, G_TYPE_STRING
);
284 for ( wxArrayString::const_iterator i
= choices
.begin();
288 gtk_list_store_append(store
, &iter
);
289 gtk_list_store_set(store
, &iter
,
290 0, (const gchar
*)i
->utf8_str(),
294 GtkEntryCompletion
* const completion
= gtk_entry_completion_new();
295 gtk_entry_completion_set_model(completion
, GTK_TREE_MODEL(store
));
296 gtk_entry_completion_set_text_column(completion
, 0);
297 gtk_entry_set_completion(entry
, completion
);
298 g_object_unref(completion
);
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 bool wxTextEntry::IsEditable() const
308 return gtk_editable_get_editable(GetEditable()) != 0;
311 void wxTextEntry::SetEditable(bool editable
)
313 gtk_editable_set_editable(GetEditable(), editable
);
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 void wxTextEntry::SetMaxLength(unsigned long len
)
322 GtkEntry
* const entry
= (GtkEntry
*)GetEditable();
323 if (!GTK_IS_ENTRY(entry
))
326 gtk_entry_set_max_length(entry
, len
);
328 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if we had
329 // tried to enter more text than allowed by max text length and the text
330 // wasn't really changed
332 // to detect this and generate TEXT_MAXLEN event instead of TEXT_CHANGED
333 // one in this case we also catch "insert_text" signal
335 // when max len is set to 0 we disconnect our handler as it means that we
336 // shouldn't check anything any more
343 G_CALLBACK(wx_gtk_insert_text_callback
),
347 else // no max length
349 g_signal_handlers_disconnect_by_func
352 (gpointer
)wx_gtk_insert_text_callback
,
358 void wxTextEntry::SendMaxLenEvent()
360 // remember that the next changed signal is to be ignored to avoid
361 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
362 //IgnoreNextTextUpdate();
364 wxWindow
* const win
= GetEditableWindow();
365 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
366 event
.SetEventObject(win
);
367 event
.SetString(GetValue());
368 win
->HandleWindowEvent(event
);
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 bool wxTextEntry::DoSetMargins(const wxPoint
& margins
)
377 #if GTK_CHECK_VERSION(2,10,0)
378 GtkEntry
* entry
= GetEntry();
383 const GtkBorder
* oldBorder
= gtk_entry_get_inner_border(entry
);
384 GtkBorder
* newBorder
;
388 newBorder
= gtk_border_copy(oldBorder
);
392 #if GTK_CHECK_VERSION(2,14,0)
393 newBorder
= gtk_border_new();
395 newBorder
= g_slice_new0(GtkBorder
);
397 // Use some reasonable defaults for initial margins
399 newBorder
->right
= 2;
401 // These numbers seem to let the text remain vertically centered
402 // in common use scenarios when margins.y == -1.
404 newBorder
->bottom
= 3;
407 if ( margins
.x
!= -1 )
408 newBorder
->left
= (gint
) margins
.x
;
410 if ( margins
.y
!= -1 )
411 newBorder
->top
= (gint
) margins
.y
;
413 gtk_entry_set_inner_border(entry
, newBorder
);
415 #if GTK_CHECK_VERSION(2,14,0)
416 gtk_border_free(newBorder
);
418 g_slice_free(GtkBorder
, newBorder
);
423 wxUnusedVar(margins
);
428 wxPoint
wxTextEntry::DoGetMargins() const
430 #if GTK_CHECK_VERSION(2,10,0)
431 GtkEntry
* entry
= GetEntry();
434 return wxPoint(-1, -1);
436 const GtkBorder
* border
= gtk_entry_get_inner_border(entry
);
439 return wxPoint(-1, -1);
441 return wxPoint((wxCoord
) border
->left
, (wxCoord
) border
->top
);
443 return wxPoint(-1, -1);
447 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX