]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textentry.cpp
wxRTC: fixed guidelines overwriting adjacent cell borders; corrected capitalisation...
[wxWidgets.git] / src / gtk / textentry.cpp
CommitLineData
0ec1179b
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/textentry.cpp
3// Purpose: wxTextEntry implementation for wxGTK
4// Author: Vadim Zeitlin
5// Created: 2007-09-24
0ec1179b
VZ
6// Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18// for compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif
24
96a4cdeb
VZ
25#if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
26
0ec1179b 27#ifndef WX_PRECOMP
9dc44eff 28 #include "wx/textentry.h"
d290d2fe
RR
29 #include "wx/window.h"
30 #include "wx/textctrl.h"
0ec1179b
VZ
31#endif //WX_PRECOMP
32
9dc44eff 33#include <gtk/gtk.h>
0ec1179b 34#include "wx/gtk/private.h"
9dc44eff 35#include "wx/gtk/private/gtk2-compat.h"
0ec1179b
VZ
36
37// ============================================================================
38// signal handlers implementation
39// ============================================================================
40
0ec1179b 41// "insert_text" handler for GtkEntry
10434560
VZ
42extern "C"
43void
0ec1179b 44wx_gtk_insert_text_callback(GtkEditable *editable,
656b9f44 45 const gchar * new_text,
e4161a2a
VZ
46 gint WXUNUSED(new_text_length),
47 gint * WXUNUSED(position),
0ec1179b
VZ
48 wxTextEntry *text)
49{
0ec1179b
VZ
50 GtkEntry *entry = GTK_ENTRY (editable);
51
385e8575
PC
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));
54#else
55 const int text_max_length = entry->text_max_length;
56#endif
656b9f44 57
b2c35774 58 bool handled = false;
0ec1179b 59
b2c35774
VZ
60 // check that we don't overflow the max length limit if we have it
61 if ( text_max_length )
62 {
63 const int text_length = gtk_entry_get_text_length(entry);
656b9f44 64
b2c35774
VZ
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 )
68 {
69 // Prevent the new text from being inserted.
70 handled = true;
656b9f44 71
b2c35774
VZ
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.
0ec1179b 75
b2c35774
VZ
76 // Notify the user code about overflow.
77 text->SendMaxLenEvent();
78 }
79 }
656b9f44 80
b2c35774
VZ
81 if ( !handled && text->GTKEntryOnInsertText(new_text) )
82 {
83 // If we already handled the new text insertion, don't do it again.
84 handled = true;
0ec1179b 85 }
b2c35774
VZ
86
87 if ( handled )
88 g_signal_stop_emission_by_name (editable, "insert_text");
0ec1179b
VZ
89}
90
10434560
VZ
91//-----------------------------------------------------------------------------
92// clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
93//-----------------------------------------------------------------------------
94
95// common part of the event handlers below
96static void
97DoHandleClipboardCallback( GtkWidget *widget,
98 wxWindow *win,
99 wxEventType eventType,
100 const gchar* signal_name)
101{
102 wxClipboardTextEvent event( eventType, win->GetId() );
103 event.SetEventObject( win );
104 if ( win->HandleWindowEvent( event ) )
105 {
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);
109 }
110}
111
112extern "C"
113{
114
115static void
116wx_gtk_copy_clipboard_callback( GtkWidget *widget, wxWindow *win )
117{
118 DoHandleClipboardCallback(
ce7fe42e 119 widget, win, wxEVT_TEXT_COPY, "copy-clipboard" );
10434560
VZ
120}
121
122static void
123wx_gtk_cut_clipboard_callback( GtkWidget *widget, wxWindow *win )
124{
125 DoHandleClipboardCallback(
ce7fe42e 126 widget, win, wxEVT_TEXT_CUT, "cut-clipboard" );
10434560
VZ
127}
128
129static void
130wx_gtk_paste_clipboard_callback( GtkWidget *widget, wxWindow *win )
131{
132 DoHandleClipboardCallback(
ce7fe42e 133 widget, win, wxEVT_TEXT_PASTE, "paste-clipboard" );
10434560
VZ
134}
135
0ec1179b
VZ
136} // extern "C"
137
138// ============================================================================
139// wxTextEntry implementation
140// ============================================================================
141
142// ----------------------------------------------------------------------------
143// text operations
144// ----------------------------------------------------------------------------
145
146void wxTextEntry::WriteText(const wxString& value)
147{
148 GtkEditable * const edit = GetEditable();
149
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,
152 // not two
153 {
154 EventsSuppressor noevents(this);
155 gtk_editable_delete_selection(edit);
156 }
157
158 // insert new text at the cursor position
159 gint len = gtk_editable_get_position(edit);
160 gtk_editable_insert_text
161 (
162 edit,
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
166 );
167
168 // and move cursor to the end of new text
169 gtk_editable_set_position(edit, len);
170}
171
75377698
PC
172void wxTextEntry::DoSetValue(const wxString& value, int flags)
173{
155ce4f1 174 if (value != DoGetValue())
75377698
PC
175 {
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
179 {
180 EventsSuppressor noevents(this);
181 Remove(0, -1);
182 }
183 EventsSuppressor noeventsIf(this, !(flags & SetValue_SendEvent));
184 WriteText(value);
185 }
186 else if (flags & SetValue_SendEvent)
187 SendTextUpdatedEvent(GetEditableWindow());
188
189 SetInsertionPoint(0);
190}
191
135b23b2 192wxString wxTextEntry::DoGetValue() const
0ec1179b
VZ
193{
194 const wxGtkString value(gtk_editable_get_chars(GetEditable(), 0, -1));
195
ec592d7f
VZ
196 return wxGTK_CONV_BACK_FONT(value,
197 const_cast<wxTextEntry *>(this)->GetEditableWindow()->GetFont());
0ec1179b
VZ
198}
199
200void wxTextEntry::Remove(long from, long to)
201{
202 gtk_editable_delete_text(GetEditable(), from, to);
203}
204
205// ----------------------------------------------------------------------------
206// clipboard operations
207// ----------------------------------------------------------------------------
208
10434560
VZ
209void wxTextEntry::GTKConnectClipboardSignals(GtkWidget* entry)
210{
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());
220}
221
0ec1179b
VZ
222void wxTextEntry::Copy()
223{
224 gtk_editable_copy_clipboard(GetEditable());
225}
226
227void wxTextEntry::Cut()
228{
229 gtk_editable_cut_clipboard(GetEditable());
230}
231
232void wxTextEntry::Paste()
233{
234 gtk_editable_paste_clipboard(GetEditable());
235}
236
237// ----------------------------------------------------------------------------
238// undo/redo
239// ----------------------------------------------------------------------------
240
241void wxTextEntry::Undo()
242{
243 // TODO: not implemented
244}
245
246void wxTextEntry::Redo()
247{
248 // TODO: not implemented
249}
250
251bool wxTextEntry::CanUndo() const
252{
253 return false;
254}
255
256bool wxTextEntry::CanRedo() const
257{
258 return false;
259}
260
261// ----------------------------------------------------------------------------
262// insertion point
263// ----------------------------------------------------------------------------
264
265void wxTextEntry::SetInsertionPoint(long pos)
266{
267 gtk_editable_set_position(GetEditable(), pos);
268}
269
270long wxTextEntry::GetInsertionPoint() const
271{
272 return gtk_editable_get_position(GetEditable());
273}
274
275long wxTextEntry::GetLastPosition() const
276{
277 // this can't be implemented for arbitrary GtkEditable so only do it for
278 // GtkEntries
d962569a 279 long pos = -1;
7e3e162f
PC
280 GtkEntry* entry = (GtkEntry*)GetEditable();
281 if (GTK_IS_ENTRY(entry))
282 pos = gtk_entry_get_text_length(entry);
0ec1179b 283
d962569a 284 return pos;
0ec1179b
VZ
285}
286
287// ----------------------------------------------------------------------------
288// selection
289// ----------------------------------------------------------------------------
290
291void wxTextEntry::SetSelection(long from, long to)
292{
e0721133
VZ
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 )
297 from = 0;
298
8b2e0e6d
VZ
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);
3f6439ae 303
9dc44eff 304#ifndef __WXGTK3__
3f6439ae
PC
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))
308 {
309 GtkEntry* entry = GTK_ENTRY(GetEditable());
310 if (to < 0)
311 to = entry->text_length;
312 entry->selection_bound = to;
313 }
9dc44eff 314#endif
0ec1179b
VZ
315}
316
317void wxTextEntry::GetSelection(long *from, long *to) const
318{
319 gint start, end;
320 if ( gtk_editable_get_selection_bounds(GetEditable(), &start, &end) )
321 {
322 // the output must always be in order, although in GTK+ it isn't
323 if ( start > end )
324 {
325 gint tmp = start;
326 start = end;
327 end = tmp;
328 }
329 }
330 else // no selection
331 {
332 // for compatibility with MSW return the empty selection at cursor
333 start =
334 end = GetInsertionPoint();
335 }
336
337 if ( from )
338 *from = start;
339
340 if ( to )
341 *to = end;
342}
343
344// ----------------------------------------------------------------------------
ecaed0bc
VZ
345// auto completion
346// ----------------------------------------------------------------------------
347
574479e8 348bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
ecaed0bc 349{
7e3e162f
PC
350 GtkEntry* const entry = (GtkEntry*)GetEditable();
351 wxCHECK_MSG(GTK_IS_ENTRY(entry), false, "auto completion doesn't work with this control");
ecaed0bc
VZ
352
353 GtkListStore * const store = gtk_list_store_new(1, G_TYPE_STRING);
354 GtkTreeIter iter;
355
356 for ( wxArrayString::const_iterator i = choices.begin();
357 i != choices.end();
358 ++i )
359 {
360 gtk_list_store_append(store, &iter);
361 gtk_list_store_set(store, &iter,
362 0, (const gchar *)i->utf8_str(),
363 -1);
364 }
365
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);
0c3d1aa7 371 return true;
ecaed0bc
VZ
372}
373
374// ----------------------------------------------------------------------------
0ec1179b
VZ
375// editable status
376// ----------------------------------------------------------------------------
377
378bool wxTextEntry::IsEditable() const
379{
d5027818 380 return gtk_editable_get_editable(GetEditable()) != 0;
0ec1179b
VZ
381}
382
383void wxTextEntry::SetEditable(bool editable)
384{
385 gtk_editable_set_editable(GetEditable(), editable);
386}
387
388// ----------------------------------------------------------------------------
389// max text length
390// ----------------------------------------------------------------------------
391
392void wxTextEntry::SetMaxLength(unsigned long len)
393{
7e3e162f
PC
394 GtkEntry* const entry = (GtkEntry*)GetEditable();
395 if (!GTK_IS_ENTRY(entry))
0ec1179b
VZ
396 return;
397
398 gtk_entry_set_max_length(entry, len);
0ec1179b
VZ
399}
400
401void wxTextEntry::SendMaxLenEvent()
402{
403 // remember that the next changed signal is to be ignored to avoid
ce7fe42e 404 // generating a dummy wxEVT_TEXT event
0ec1179b
VZ
405 //IgnoreNextTextUpdate();
406
ec592d7f 407 wxWindow * const win = GetEditableWindow();
ce7fe42e 408 wxCommandEvent event(wxEVT_TEXT_MAXLEN, win->GetId());
0ec1179b
VZ
409 event.SetEventObject(win);
410 event.SetString(GetValue());
937013e0 411 win->HandleWindowEvent(event);
0ec1179b
VZ
412}
413
b2c35774
VZ
414// ----------------------------------------------------------------------------
415// IM handling
416// ----------------------------------------------------------------------------
417
418int wxTextEntry::GTKIMFilterKeypress(GdkEventKey* event) const
419{
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);
423#else // GTK+ < 2.22
424 wxUnusedVar(event);
425#endif // GTK+ 2.22+
426
427 return FALSE;
428}
429
430void wxTextEntry::GTKConnectInsertTextSignal(GtkEntry* entry)
431{
432 g_signal_connect(entry, "insert_text",
433 G_CALLBACK(wx_gtk_insert_text_callback), this);
434}
435
436bool wxTextEntry::GTKEntryOnInsertText(const char* text)
437{
438 return GetEditableWindow()->GTKOnInsertText(text);
439}
440
0847e36e
JS
441// ----------------------------------------------------------------------------
442// margins support
443// ----------------------------------------------------------------------------
444
445bool wxTextEntry::DoSetMargins(const wxPoint& margins)
446{
447#if GTK_CHECK_VERSION(2,10,0)
448 GtkEntry* entry = GetEntry();
449
450 if ( !entry )
451 return false;
452
453 const GtkBorder* oldBorder = gtk_entry_get_inner_border(entry);
454 GtkBorder* newBorder;
455
456 if ( oldBorder )
457 {
458 newBorder = gtk_border_copy(oldBorder);
459 }
460 else
461 {
462 #if GTK_CHECK_VERSION(2,14,0)
463 newBorder = gtk_border_new();
464 #else
c39b6e1e 465 newBorder = g_slice_new0(GtkBorder);
0847e36e
JS
466 #endif
467 // Use some reasonable defaults for initial margins
468 newBorder->left = 2;
469 newBorder->right = 2;
470
471 // These numbers seem to let the text remain vertically centered
472 // in common use scenarios when margins.y == -1.
473 newBorder->top = 3;
474 newBorder->bottom = 3;
475 }
476
477 if ( margins.x != -1 )
478 newBorder->left = (gint) margins.x;
479
480 if ( margins.y != -1 )
481 newBorder->top = (gint) margins.y;
482
483 gtk_entry_set_inner_border(entry, newBorder);
484
c39b6e1e
JS
485#if GTK_CHECK_VERSION(2,14,0)
486 gtk_border_free(newBorder);
487#else
488 g_slice_free(GtkBorder, newBorder);
489#endif
490
0847e36e
JS
491 return true;
492#else
493 wxUnusedVar(margins);
494 return false;
495#endif
496}
497
498wxPoint wxTextEntry::DoGetMargins() const
499{
500#if GTK_CHECK_VERSION(2,10,0)
501 GtkEntry* entry = GetEntry();
502
503 if ( !entry )
504 return wxPoint(-1, -1);
505
506 const GtkBorder* border = gtk_entry_get_inner_border(entry);
507
508 if ( !border )
509 return wxPoint(-1, -1);
510
511 return wxPoint((wxCoord) border->left, (wxCoord) border->top);
512#else
513 return wxPoint(-1, -1);
514#endif
515}
516
96a4cdeb 517#endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX