1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textentry.cpp
3 // Purpose: wxTextEntry implementation for wxGTK
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
28 #include "wx/textentry.h"
29 #include "wx/window.h"
30 #include "wx/textctrl.h"
34 #include "wx/gtk/private.h"
35 #include "wx/gtk/private/gtk2-compat.h"
37 // ============================================================================
38 // signal handlers implementation
39 // ============================================================================
41 // "insert_text" handler for GtkEntry
44 wx_gtk_insert_text_callback(GtkEditable
*editable
,
45 const gchar
* new_text
,
46 gint
WXUNUSED(new_text_length
),
47 gint
* WXUNUSED(position
),
50 GtkEntry
*entry
= GTK_ENTRY (editable
);
52 #if GTK_CHECK_VERSION(3,0,0) || defined(GSEAL_ENABLE)
53 const int text_max_length
= gtk_entry_buffer_get_max_length(gtk_entry_get_buffer(entry
));
55 const int text_max_length
= entry
->text_max_length
;
60 // check that we don't overflow the max length limit if we have it
61 if ( text_max_length
)
63 const int text_length
= gtk_entry_get_text_length(entry
);
65 // We can't use new_text_length as it is in bytes while we want to count
66 // characters (in first approximation, anyhow...).
67 if ( text_length
+ g_utf8_strlen(new_text
, -1) > text_max_length
)
69 // Prevent the new text from being inserted.
72 // Currently we don't insert anything at all, but it would be better to
73 // insert as many characters as would fit into the text control and
74 // only discard the rest.
76 // Notify the user code about overflow.
77 text
->SendMaxLenEvent();
81 if ( !handled
&& text
->GTKEntryOnInsertText(new_text
) )
83 // If we already handled the new text insertion, don't do it again.
88 g_signal_stop_emission_by_name (editable
, "insert_text");
91 //-----------------------------------------------------------------------------
92 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
93 //-----------------------------------------------------------------------------
95 // common part of the event handlers below
97 DoHandleClipboardCallback( GtkWidget
*widget
,
99 wxEventType eventType
,
100 const gchar
* signal_name
)
102 wxClipboardTextEvent
event( eventType
, win
->GetId() );
103 event
.SetEventObject( win
);
104 if ( win
->HandleWindowEvent( event
) )
106 // don't let the default processing to take place if we did something
107 // ourselves in the event handler
108 g_signal_stop_emission_by_name (widget
, signal_name
);
116 wx_gtk_copy_clipboard_callback( GtkWidget
*widget
, wxWindow
*win
)
118 DoHandleClipboardCallback(
119 widget
, win
, wxEVT_TEXT_COPY
, "copy-clipboard" );
123 wx_gtk_cut_clipboard_callback( GtkWidget
*widget
, wxWindow
*win
)
125 DoHandleClipboardCallback(
126 widget
, win
, wxEVT_TEXT_CUT
, "cut-clipboard" );
130 wx_gtk_paste_clipboard_callback( GtkWidget
*widget
, wxWindow
*win
)
132 DoHandleClipboardCallback(
133 widget
, win
, wxEVT_TEXT_PASTE
, "paste-clipboard" );
138 // ============================================================================
139 // wxTextEntry implementation
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 void wxTextEntry::WriteText(const wxString
& value
)
148 GtkEditable
* const edit
= GetEditable();
150 // remove the selection if there is one and suppress the text change event
151 // generated by this: we only want to generate one event for this change,
154 EventsSuppressor
noevents(this);
155 gtk_editable_delete_selection(edit
);
158 // insert new text at the cursor position
159 gint len
= gtk_editable_get_position(edit
);
160 gtk_editable_insert_text
163 wxGTK_CONV_FONT(value
, GetEditableWindow()->GetFont()),
164 -1, // text: length: compute it using strlen()
165 &len
// will be updated to position after the text end
168 // and move cursor to the end of new text
169 gtk_editable_set_position(edit
, len
);
172 void wxTextEntry::DoSetValue(const wxString
& value
, int flags
)
174 if (value
!= DoGetValue())
176 // use Remove() rather than SelectAll() to avoid unnecessary clipboard
177 // operations, and prevent triggering an apparent bug in GTK which
178 // causes the the subsequent WriteText() to append rather than overwrite
180 EventsSuppressor
noevents(this);
183 EventsSuppressor
noeventsIf(this, !(flags
& SetValue_SendEvent
));
186 else if (flags
& SetValue_SendEvent
)
187 SendTextUpdatedEvent(GetEditableWindow());
189 SetInsertionPoint(0);
192 wxString
wxTextEntry::DoGetValue() const
194 const wxGtkString
value(gtk_editable_get_chars(GetEditable(), 0, -1));
196 return wxGTK_CONV_BACK_FONT(value
,
197 const_cast<wxTextEntry
*>(this)->GetEditableWindow()->GetFont());
200 void wxTextEntry::Remove(long from
, long to
)
202 gtk_editable_delete_text(GetEditable(), from
, to
);
205 // ----------------------------------------------------------------------------
206 // clipboard operations
207 // ----------------------------------------------------------------------------
209 void wxTextEntry::GTKConnectClipboardSignals(GtkWidget
* entry
)
211 g_signal_connect(entry
, "copy-clipboard",
212 G_CALLBACK (wx_gtk_copy_clipboard_callback
),
213 GetEditableWindow());
214 g_signal_connect(entry
, "cut-clipboard",
215 G_CALLBACK (wx_gtk_cut_clipboard_callback
),
216 GetEditableWindow());
217 g_signal_connect(entry
, "paste-clipboard",
218 G_CALLBACK (wx_gtk_paste_clipboard_callback
),
219 GetEditableWindow());
222 void wxTextEntry::Copy()
224 gtk_editable_copy_clipboard(GetEditable());
227 void wxTextEntry::Cut()
229 gtk_editable_cut_clipboard(GetEditable());
232 void wxTextEntry::Paste()
234 gtk_editable_paste_clipboard(GetEditable());
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 void wxTextEntry::Undo()
243 // TODO: not implemented
246 void wxTextEntry::Redo()
248 // TODO: not implemented
251 bool wxTextEntry::CanUndo() const
256 bool wxTextEntry::CanRedo() const
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void wxTextEntry::SetInsertionPoint(long pos
)
267 gtk_editable_set_position(GetEditable(), pos
);
270 long wxTextEntry::GetInsertionPoint() const
272 return gtk_editable_get_position(GetEditable());
275 long wxTextEntry::GetLastPosition() const
277 // this can't be implemented for arbitrary GtkEditable so only do it for
280 GtkEntry
* entry
= (GtkEntry
*)GetEditable();
281 if (GTK_IS_ENTRY(entry
))
282 pos
= gtk_entry_get_text_length(entry
);
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 void wxTextEntry::SetSelection(long from
, long to
)
293 // in wx convention, (-1, -1) means the entire range but GTK+ translates -1
294 // (or any negative number for that matter) into last position so we need
295 // to translate manually
296 if ( from
== -1 && to
== -1 )
299 // for compatibility with MSW, exchange from and to parameters so that the
300 // insertion point is set to the start of the selection and not its end as
301 // GTK+ does by default
302 gtk_editable_select_region(GetEditable(), to
, from
);
305 // avoid reported problem with RHEL 5 GTK+ 2.10 where selection is reset by
306 // a clipboard callback, see #13277
307 if (gtk_check_version(2,12,0))
309 GtkEntry
* entry
= GTK_ENTRY(GetEditable());
311 to
= entry
->text_length
;
312 entry
->selection_bound
= to
;
317 void wxTextEntry::GetSelection(long *from
, long *to
) const
320 if ( gtk_editable_get_selection_bounds(GetEditable(), &start
, &end
) )
322 // the output must always be in order, although in GTK+ it isn't
332 // for compatibility with MSW return the empty selection at cursor
334 end
= GetInsertionPoint();
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString
& choices
)
350 GtkEntry
* const entry
= (GtkEntry
*)GetEditable();
351 wxCHECK_MSG(GTK_IS_ENTRY(entry
), false, "auto completion doesn't work with this control");
353 GtkListStore
* const store
= gtk_list_store_new(1, G_TYPE_STRING
);
356 for ( wxArrayString::const_iterator i
= choices
.begin();
360 gtk_list_store_append(store
, &iter
);
361 gtk_list_store_set(store
, &iter
,
362 0, (const gchar
*)i
->utf8_str(),
366 GtkEntryCompletion
* const completion
= gtk_entry_completion_new();
367 gtk_entry_completion_set_model(completion
, GTK_TREE_MODEL(store
));
368 gtk_entry_completion_set_text_column(completion
, 0);
369 gtk_entry_set_completion(entry
, completion
);
370 g_object_unref(completion
);
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 bool wxTextEntry::IsEditable() const
380 return gtk_editable_get_editable(GetEditable()) != 0;
383 void wxTextEntry::SetEditable(bool editable
)
385 gtk_editable_set_editable(GetEditable(), editable
);
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 void wxTextEntry::SetMaxLength(unsigned long len
)
394 GtkEntry
* const entry
= (GtkEntry
*)GetEditable();
395 if (!GTK_IS_ENTRY(entry
))
398 gtk_entry_set_max_length(entry
, len
);
401 void wxTextEntry::SendMaxLenEvent()
403 // remember that the next changed signal is to be ignored to avoid
404 // generating a dummy wxEVT_TEXT event
405 //IgnoreNextTextUpdate();
407 wxWindow
* const win
= GetEditableWindow();
408 wxCommandEvent
event(wxEVT_TEXT_MAXLEN
, win
->GetId());
409 event
.SetEventObject(win
);
410 event
.SetString(GetValue());
411 win
->HandleWindowEvent(event
);
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 int wxTextEntry::GTKIMFilterKeypress(GdkEventKey
* event
) const
420 #if GTK_CHECK_VERSION(2, 22, 0)
421 if ( gtk_check_version(2, 12, 0) == 0 )
422 return gtk_entry_im_context_filter_keypress(GetEntry(), event
);
430 void wxTextEntry::GTKConnectInsertTextSignal(GtkEntry
* entry
)
432 g_signal_connect(entry
, "insert_text",
433 G_CALLBACK(wx_gtk_insert_text_callback
), this);
436 bool wxTextEntry::GTKEntryOnInsertText(const char* text
)
438 return GetEditableWindow()->GTKOnInsertText(text
);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 bool wxTextEntry::DoSetMargins(const wxPoint
& margins
)
447 #if GTK_CHECK_VERSION(2,10,0)
448 GtkEntry
* entry
= GetEntry();
453 const GtkBorder
* oldBorder
= gtk_entry_get_inner_border(entry
);
454 GtkBorder
* newBorder
;
458 newBorder
= gtk_border_copy(oldBorder
);
462 #if GTK_CHECK_VERSION(2,14,0)
463 newBorder
= gtk_border_new();
465 newBorder
= g_slice_new0(GtkBorder
);
467 // Use some reasonable defaults for initial margins
469 newBorder
->right
= 2;
471 // These numbers seem to let the text remain vertically centered
472 // in common use scenarios when margins.y == -1.
474 newBorder
->bottom
= 3;
477 if ( margins
.x
!= -1 )
478 newBorder
->left
= (gint
) margins
.x
;
480 if ( margins
.y
!= -1 )
481 newBorder
->top
= (gint
) margins
.y
;
483 gtk_entry_set_inner_border(entry
, newBorder
);
485 #if GTK_CHECK_VERSION(2,14,0)
486 gtk_border_free(newBorder
);
488 g_slice_free(GtkBorder
, newBorder
);
493 wxUnusedVar(margins
);
498 wxPoint
wxTextEntry::DoGetMargins() const
500 #if GTK_CHECK_VERSION(2,10,0)
501 GtkEntry
* entry
= GetEntry();
504 return wxPoint(-1, -1);
506 const GtkBorder
* border
= gtk_entry_get_inner_border(entry
);
509 return wxPoint(-1, -1);
511 return wxPoint((wxCoord
) border
->left
, (wxCoord
) border
->top
);
513 return wxPoint(-1, -1);
517 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX