]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textentry.cpp
Add doc for wxGraphicsContext.Create(), which is a lightweight context with no target...
[wxWidgets.git] / src / gtk / textentry.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textentry.cpp
3 // Purpose: wxTextEntry implementation for wxGTK
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-24
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
27
28 #ifndef WX_PRECOMP
29 #include "wx/textentry.h"
30 #include "wx/window.h"
31 #include "wx/textctrl.h"
32 #endif //WX_PRECOMP
33
34 #include <gtk/gtk.h>
35 #include "wx/gtk/private.h"
36 #include "wx/gtk/private/gtk2-compat.h"
37
38 // ============================================================================
39 // signal handlers implementation
40 // ============================================================================
41
42 // "insert_text" handler for GtkEntry
43 extern "C"
44 void
45 wx_gtk_insert_text_callback(GtkEditable *editable,
46 const gchar * new_text,
47 gint WXUNUSED(new_text_length),
48 gint * WXUNUSED(position),
49 wxTextEntry *text)
50 {
51 GtkEntry *entry = GTK_ENTRY (editable);
52
53 #if GTK_CHECK_VERSION(3,0,0) || defined(GSEAL_ENABLE)
54 const int text_max_length = gtk_entry_buffer_get_max_length(gtk_entry_get_buffer(entry));
55 #else
56 const int text_max_length = entry->text_max_length;
57 #endif
58
59 bool handled = false;
60
61 // check that we don't overflow the max length limit if we have it
62 if ( text_max_length )
63 {
64 const int text_length = gtk_entry_get_text_length(entry);
65
66 // We can't use new_text_length as it is in bytes while we want to count
67 // characters (in first approximation, anyhow...).
68 if ( text_length + g_utf8_strlen(new_text, -1) > text_max_length )
69 {
70 // Prevent the new text from being inserted.
71 handled = true;
72
73 // Currently we don't insert anything at all, but it would be better to
74 // insert as many characters as would fit into the text control and
75 // only discard the rest.
76
77 // Notify the user code about overflow.
78 text->SendMaxLenEvent();
79 }
80 }
81
82 if ( !handled && text->GTKEntryOnInsertText(new_text) )
83 {
84 // If we already handled the new text insertion, don't do it again.
85 handled = true;
86 }
87
88 if ( handled )
89 g_signal_stop_emission_by_name (editable, "insert_text");
90 }
91
92 //-----------------------------------------------------------------------------
93 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
94 //-----------------------------------------------------------------------------
95
96 // common part of the event handlers below
97 static void
98 DoHandleClipboardCallback( GtkWidget *widget,
99 wxWindow *win,
100 wxEventType eventType,
101 const gchar* signal_name)
102 {
103 wxClipboardTextEvent event( eventType, win->GetId() );
104 event.SetEventObject( win );
105 if ( win->HandleWindowEvent( event ) )
106 {
107 // don't let the default processing to take place if we did something
108 // ourselves in the event handler
109 g_signal_stop_emission_by_name (widget, signal_name);
110 }
111 }
112
113 extern "C"
114 {
115
116 static void
117 wx_gtk_copy_clipboard_callback( GtkWidget *widget, wxWindow *win )
118 {
119 DoHandleClipboardCallback(
120 widget, win, wxEVT_TEXT_COPY, "copy-clipboard" );
121 }
122
123 static void
124 wx_gtk_cut_clipboard_callback( GtkWidget *widget, wxWindow *win )
125 {
126 DoHandleClipboardCallback(
127 widget, win, wxEVT_TEXT_CUT, "cut-clipboard" );
128 }
129
130 static void
131 wx_gtk_paste_clipboard_callback( GtkWidget *widget, wxWindow *win )
132 {
133 DoHandleClipboardCallback(
134 widget, win, wxEVT_TEXT_PASTE, "paste-clipboard" );
135 }
136
137 } // extern "C"
138
139 // ============================================================================
140 // wxTextEntry implementation
141 // ============================================================================
142
143 // ----------------------------------------------------------------------------
144 // text operations
145 // ----------------------------------------------------------------------------
146
147 void wxTextEntry::WriteText(const wxString& value)
148 {
149 GtkEditable * const edit = GetEditable();
150
151 // remove the selection if there is one and suppress the text change event
152 // generated by this: we only want to generate one event for this change,
153 // not two
154 {
155 EventsSuppressor noevents(this);
156 gtk_editable_delete_selection(edit);
157 }
158
159 // insert new text at the cursor position
160 gint len = gtk_editable_get_position(edit);
161 gtk_editable_insert_text
162 (
163 edit,
164 wxGTK_CONV_FONT(value, GetEditableWindow()->GetFont()),
165 -1, // text: length: compute it using strlen()
166 &len // will be updated to position after the text end
167 );
168
169 // and move cursor to the end of new text
170 gtk_editable_set_position(edit, len);
171 }
172
173 void wxTextEntry::DoSetValue(const wxString& value, int flags)
174 {
175 if (value != DoGetValue())
176 {
177 // use Remove() rather than SelectAll() to avoid unnecessary clipboard
178 // operations, and prevent triggering an apparent bug in GTK which
179 // causes the the subsequent WriteText() to append rather than overwrite
180 {
181 EventsSuppressor noevents(this);
182 Remove(0, -1);
183 }
184 EventsSuppressor noeventsIf(this, !(flags & SetValue_SendEvent));
185 WriteText(value);
186 }
187 else if (flags & SetValue_SendEvent)
188 SendTextUpdatedEvent(GetEditableWindow());
189
190 SetInsertionPoint(0);
191 }
192
193 wxString wxTextEntry::DoGetValue() const
194 {
195 const wxGtkString value(gtk_editable_get_chars(GetEditable(), 0, -1));
196
197 return wxGTK_CONV_BACK_FONT(value,
198 const_cast<wxTextEntry *>(this)->GetEditableWindow()->GetFont());
199 }
200
201 void wxTextEntry::Remove(long from, long to)
202 {
203 gtk_editable_delete_text(GetEditable(), from, to);
204 }
205
206 // ----------------------------------------------------------------------------
207 // clipboard operations
208 // ----------------------------------------------------------------------------
209
210 void wxTextEntry::GTKConnectClipboardSignals(GtkWidget* entry)
211 {
212 g_signal_connect(entry, "copy-clipboard",
213 G_CALLBACK (wx_gtk_copy_clipboard_callback),
214 GetEditableWindow());
215 g_signal_connect(entry, "cut-clipboard",
216 G_CALLBACK (wx_gtk_cut_clipboard_callback),
217 GetEditableWindow());
218 g_signal_connect(entry, "paste-clipboard",
219 G_CALLBACK (wx_gtk_paste_clipboard_callback),
220 GetEditableWindow());
221 }
222
223 void wxTextEntry::Copy()
224 {
225 gtk_editable_copy_clipboard(GetEditable());
226 }
227
228 void wxTextEntry::Cut()
229 {
230 gtk_editable_cut_clipboard(GetEditable());
231 }
232
233 void wxTextEntry::Paste()
234 {
235 gtk_editable_paste_clipboard(GetEditable());
236 }
237
238 // ----------------------------------------------------------------------------
239 // undo/redo
240 // ----------------------------------------------------------------------------
241
242 void wxTextEntry::Undo()
243 {
244 // TODO: not implemented
245 }
246
247 void wxTextEntry::Redo()
248 {
249 // TODO: not implemented
250 }
251
252 bool wxTextEntry::CanUndo() const
253 {
254 return false;
255 }
256
257 bool wxTextEntry::CanRedo() const
258 {
259 return false;
260 }
261
262 // ----------------------------------------------------------------------------
263 // insertion point
264 // ----------------------------------------------------------------------------
265
266 void wxTextEntry::SetInsertionPoint(long pos)
267 {
268 gtk_editable_set_position(GetEditable(), pos);
269 }
270
271 long wxTextEntry::GetInsertionPoint() const
272 {
273 return gtk_editable_get_position(GetEditable());
274 }
275
276 long wxTextEntry::GetLastPosition() const
277 {
278 // this can't be implemented for arbitrary GtkEditable so only do it for
279 // GtkEntries
280 long pos = -1;
281 GtkEntry* entry = (GtkEntry*)GetEditable();
282 if (GTK_IS_ENTRY(entry))
283 pos = gtk_entry_get_text_length(entry);
284
285 return pos;
286 }
287
288 // ----------------------------------------------------------------------------
289 // selection
290 // ----------------------------------------------------------------------------
291
292 void wxTextEntry::SetSelection(long from, long to)
293 {
294 // in wx convention, (-1, -1) means the entire range but GTK+ translates -1
295 // (or any negative number for that matter) into last position so we need
296 // to translate manually
297 if ( from == -1 && to == -1 )
298 from = 0;
299
300 // for compatibility with MSW, exchange from and to parameters so that the
301 // insertion point is set to the start of the selection and not its end as
302 // GTK+ does by default
303 gtk_editable_select_region(GetEditable(), to, from);
304
305 #ifndef __WXGTK3__
306 // avoid reported problem with RHEL 5 GTK+ 2.10 where selection is reset by
307 // a clipboard callback, see #13277
308 if (gtk_check_version(2,12,0))
309 {
310 GtkEntry* entry = GTK_ENTRY(GetEditable());
311 if (to < 0)
312 to = entry->text_length;
313 entry->selection_bound = to;
314 }
315 #endif
316 }
317
318 void wxTextEntry::GetSelection(long *from, long *to) const
319 {
320 gint start, end;
321 if ( gtk_editable_get_selection_bounds(GetEditable(), &start, &end) )
322 {
323 // the output must always be in order, although in GTK+ it isn't
324 if ( start > end )
325 {
326 gint tmp = start;
327 start = end;
328 end = tmp;
329 }
330 }
331 else // no selection
332 {
333 // for compatibility with MSW return the empty selection at cursor
334 start =
335 end = GetInsertionPoint();
336 }
337
338 if ( from )
339 *from = start;
340
341 if ( to )
342 *to = end;
343 }
344
345 // ----------------------------------------------------------------------------
346 // auto completion
347 // ----------------------------------------------------------------------------
348
349 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
350 {
351 GtkEntry* const entry = (GtkEntry*)GetEditable();
352 wxCHECK_MSG(GTK_IS_ENTRY(entry), false, "auto completion doesn't work with this control");
353
354 GtkListStore * const store = gtk_list_store_new(1, G_TYPE_STRING);
355 GtkTreeIter iter;
356
357 for ( wxArrayString::const_iterator i = choices.begin();
358 i != choices.end();
359 ++i )
360 {
361 gtk_list_store_append(store, &iter);
362 gtk_list_store_set(store, &iter,
363 0, (const gchar *)i->utf8_str(),
364 -1);
365 }
366
367 GtkEntryCompletion * const completion = gtk_entry_completion_new();
368 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(store));
369 gtk_entry_completion_set_text_column(completion, 0);
370 gtk_entry_set_completion(entry, completion);
371 g_object_unref(completion);
372 return true;
373 }
374
375 // ----------------------------------------------------------------------------
376 // editable status
377 // ----------------------------------------------------------------------------
378
379 bool wxTextEntry::IsEditable() const
380 {
381 return gtk_editable_get_editable(GetEditable()) != 0;
382 }
383
384 void wxTextEntry::SetEditable(bool editable)
385 {
386 gtk_editable_set_editable(GetEditable(), editable);
387 }
388
389 // ----------------------------------------------------------------------------
390 // max text length
391 // ----------------------------------------------------------------------------
392
393 void wxTextEntry::SetMaxLength(unsigned long len)
394 {
395 GtkEntry* const entry = (GtkEntry*)GetEditable();
396 if (!GTK_IS_ENTRY(entry))
397 return;
398
399 gtk_entry_set_max_length(entry, len);
400 }
401
402 void wxTextEntry::SendMaxLenEvent()
403 {
404 // remember that the next changed signal is to be ignored to avoid
405 // generating a dummy wxEVT_TEXT event
406 //IgnoreNextTextUpdate();
407
408 wxWindow * const win = GetEditableWindow();
409 wxCommandEvent event(wxEVT_TEXT_MAXLEN, win->GetId());
410 event.SetEventObject(win);
411 event.SetString(GetValue());
412 win->HandleWindowEvent(event);
413 }
414
415 // ----------------------------------------------------------------------------
416 // IM handling
417 // ----------------------------------------------------------------------------
418
419 int wxTextEntry::GTKIMFilterKeypress(GdkEventKey* event) const
420 {
421 #if GTK_CHECK_VERSION(2, 22, 0)
422 if ( gtk_check_version(2, 12, 0) == 0 )
423 return gtk_entry_im_context_filter_keypress(GetEntry(), event);
424 #else // GTK+ < 2.22
425 wxUnusedVar(event);
426 #endif // GTK+ 2.22+
427
428 return FALSE;
429 }
430
431 void wxTextEntry::GTKConnectInsertTextSignal(GtkEntry* entry)
432 {
433 g_signal_connect(entry, "insert_text",
434 G_CALLBACK(wx_gtk_insert_text_callback), this);
435 }
436
437 bool wxTextEntry::GTKEntryOnInsertText(const char* text)
438 {
439 return GetEditableWindow()->GTKOnInsertText(text);
440 }
441
442 // ----------------------------------------------------------------------------
443 // margins support
444 // ----------------------------------------------------------------------------
445
446 bool wxTextEntry::DoSetMargins(const wxPoint& margins)
447 {
448 #if GTK_CHECK_VERSION(2,10,0)
449 GtkEntry* entry = GetEntry();
450
451 if ( !entry )
452 return false;
453
454 const GtkBorder* oldBorder = gtk_entry_get_inner_border(entry);
455 GtkBorder* newBorder;
456
457 if ( oldBorder )
458 {
459 newBorder = gtk_border_copy(oldBorder);
460 }
461 else
462 {
463 #if GTK_CHECK_VERSION(2,14,0)
464 newBorder = gtk_border_new();
465 #else
466 newBorder = g_slice_new0(GtkBorder);
467 #endif
468 // Use some reasonable defaults for initial margins
469 newBorder->left = 2;
470 newBorder->right = 2;
471
472 // These numbers seem to let the text remain vertically centered
473 // in common use scenarios when margins.y == -1.
474 newBorder->top = 3;
475 newBorder->bottom = 3;
476 }
477
478 if ( margins.x != -1 )
479 newBorder->left = (gint) margins.x;
480
481 if ( margins.y != -1 )
482 newBorder->top = (gint) margins.y;
483
484 gtk_entry_set_inner_border(entry, newBorder);
485
486 #if GTK_CHECK_VERSION(2,14,0)
487 gtk_border_free(newBorder);
488 #else
489 g_slice_free(GtkBorder, newBorder);
490 #endif
491
492 return true;
493 #else
494 wxUnusedVar(margins);
495 return false;
496 #endif
497 }
498
499 wxPoint wxTextEntry::DoGetMargins() const
500 {
501 #if GTK_CHECK_VERSION(2,10,0)
502 GtkEntry* entry = GetEntry();
503
504 if ( !entry )
505 return wxPoint(-1, -1);
506
507 const GtkBorder* border = gtk_entry_get_inner_border(entry);
508
509 if ( !border )
510 return wxPoint(-1, -1);
511
512 return wxPoint((wxCoord) border->left, (wxCoord) border->top);
513 #else
514 return wxPoint(-1, -1);
515 #endif
516 }
517
518 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX