]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
8e13c1ec | 2 | // Name: src/gtk/textctrl.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
f96aa4d9 | 5 | // Id: $Id$ |
9fa72bd2 | 6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
c801d85f | 13 | #include "wx/textctrl.h" |
88a7a4e1 WS |
14 | |
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/intl.h" | |
e4db172a | 17 | #include "wx/log.h" |
de6185e2 | 18 | #include "wx/utils.h" |
8e609c82 | 19 | #include "wx/panel.h" |
9eddec69 | 20 | #include "wx/settings.h" |
18680f86 | 21 | #include "wx/math.h" |
88a7a4e1 WS |
22 | #endif |
23 | ||
fab591c5 | 24 | #include "wx/strconv.h" |
cc3da3f8 | 25 | #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo()) |
c801d85f | 26 | |
a81258be RR |
27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | |
29 | #include <ctype.h> | |
30 | ||
9e691f46 VZ |
31 | #include "wx/gtk/private.h" |
32 | #include <gdk/gdkkeysyms.h> | |
b292e2f5 | 33 | |
eda40bfc VZ |
34 | // ---------------------------------------------------------------------------- |
35 | // helpers | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
418cf02e JS |
38 | extern "C" { |
39 | static void wxGtkOnRemoveTag(GtkTextBuffer *buffer, | |
40 | GtkTextTag *tag, | |
41 | GtkTextIter *start, | |
42 | GtkTextIter *end, | |
50aee613 | 43 | char *prefix) |
418cf02e JS |
44 | { |
45 | gchar *name; | |
46 | g_object_get (tag, "name", &name, NULL); | |
47 | ||
50aee613 MR |
48 | if (!name || strncmp(name, prefix, strlen(prefix))) |
49 | // anonymous tag or not starting with prefix - don't remove | |
9fa72bd2 | 50 | g_signal_stop_emission_by_name (buffer, "remove_tag"); |
418cf02e JS |
51 | |
52 | g_free(name); | |
53 | } | |
54 | } | |
55 | ||
56 | extern "C" { | |
25497324 KH |
57 | static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer, |
58 | const wxTextAttr& attr, | |
59 | GtkTextIter *start, | |
60 | GtkTextIter *end) | |
61 | { | |
62 | static gchar buf[1024]; | |
63 | GtkTextTag *tag; | |
64 | ||
9fa72bd2 MR |
65 | gulong remove_handler_id = g_signal_connect (text_buffer, "remove_tag", |
66 | G_CALLBACK (wxGtkOnRemoveTag), gpointer("WX")); | |
418cf02e | 67 | gtk_text_buffer_remove_all_tags(text_buffer, start, end); |
9fa72bd2 | 68 | g_signal_handler_disconnect (text_buffer, remove_handler_id); |
418cf02e | 69 | |
25497324 KH |
70 | if (attr.HasFont()) |
71 | { | |
25497324 | 72 | PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description; |
e808cf8a PC |
73 | wxGtkString font_string(pango_font_description_to_string(font_description)); |
74 | g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string.c_str()); | |
25497324 KH |
75 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), |
76 | buf ); | |
77 | if (!tag) | |
78 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
79 | "font-desc", font_description, | |
80 | NULL ); | |
81 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
18634476 VZ |
82 | |
83 | if (attr.GetFont().GetUnderlined()) | |
84 | { | |
85 | g_snprintf(buf, sizeof(buf), "WXFONTUNDERLINE"); | |
86 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
87 | buf ); | |
88 | if (!tag) | |
89 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
90 | "underline-set", TRUE, | |
91 | "underline", PANGO_UNDERLINE_SINGLE, | |
92 | NULL ); | |
93 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
94 | } | |
25497324 KH |
95 | } |
96 | ||
97 | if (attr.HasTextColour()) | |
98 | { | |
c6685317 | 99 | const GdkColor *colFg = attr.GetTextColour().GetColor(); |
25497324 KH |
100 | g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d", |
101 | colFg->red, colFg->green, colFg->blue); | |
102 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
103 | buf ); | |
104 | if (!tag) | |
105 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
106 | "foreground-gdk", colFg, NULL ); | |
107 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
108 | } | |
109 | ||
110 | if (attr.HasBackgroundColour()) | |
111 | { | |
c6685317 | 112 | const GdkColor *colBg = attr.GetBackgroundColour().GetColor(); |
25497324 KH |
113 | g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d", |
114 | colBg->red, colBg->green, colBg->blue); | |
115 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
116 | buf ); | |
117 | if (!tag) | |
118 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
119 | "background-gdk", colBg, NULL ); | |
120 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
121 | } | |
50aee613 MR |
122 | |
123 | if (attr.HasAlignment()) | |
124 | { | |
125 | GtkTextIter para_start, para_end = *end; | |
126 | gtk_text_buffer_get_iter_at_line( text_buffer, | |
127 | ¶_start, | |
128 | gtk_text_iter_get_line(start) ); | |
129 | gtk_text_iter_forward_line(¶_end); | |
130 | ||
9fa72bd2 | 131 | remove_handler_id = g_signal_connect (text_buffer, "remove_tag", |
50aee613 MR |
132 | G_CALLBACK(wxGtkOnRemoveTag), |
133 | gpointer("WXALIGNMENT")); | |
134 | gtk_text_buffer_remove_all_tags( text_buffer, ¶_start, ¶_end ); | |
9fa72bd2 | 135 | g_signal_handler_disconnect (text_buffer, remove_handler_id); |
50aee613 MR |
136 | |
137 | GtkJustification align; | |
138 | switch (attr.GetAlignment()) | |
139 | { | |
140 | default: | |
141 | align = GTK_JUSTIFY_LEFT; | |
142 | break; | |
143 | case wxTEXT_ALIGNMENT_RIGHT: | |
144 | align = GTK_JUSTIFY_RIGHT; | |
145 | break; | |
146 | case wxTEXT_ALIGNMENT_CENTER: | |
147 | align = GTK_JUSTIFY_CENTER; | |
148 | break; | |
149 | // gtk+ doesn't support justify as of gtk+-2.7.4 | |
150 | } | |
151 | ||
152 | g_snprintf(buf, sizeof(buf), "WXALIGNMENT %d", align); | |
153 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
154 | buf ); | |
155 | if (!tag) | |
156 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
157 | "justification", align, NULL ); | |
158 | gtk_text_buffer_apply_tag( text_buffer, tag, ¶_start, ¶_end ); | |
159 | } | |
25497324 | 160 | } |
418cf02e | 161 | } |
25497324 | 162 | |
418cf02e | 163 | extern "C" { |
cc3da3f8 RR |
164 | static void wxGtkTextInsert(GtkWidget *text, |
165 | GtkTextBuffer *text_buffer, | |
166 | const wxTextAttr& attr, | |
fbfb8bcc | 167 | const wxCharBuffer& buffer) |
cc3da3f8 RR |
168 | |
169 | { | |
25497324 KH |
170 | gint start_offset; |
171 | GtkTextIter iter, start; | |
cc3da3f8 | 172 | |
a61bbf87 JS |
173 | gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, |
174 | gtk_text_buffer_get_insert (text_buffer) ); | |
25497324 KH |
175 | start_offset = gtk_text_iter_get_offset (&iter); |
176 | gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) ); | |
177 | ||
178 | gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset); | |
a61bbf87 | 179 | |
25497324 | 180 | wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter); |
cc3da3f8 | 181 | } |
418cf02e | 182 | } |
eda40bfc | 183 | |
ce2f50e3 VZ |
184 | // ---------------------------------------------------------------------------- |
185 | // "insert_text" for GtkEntry | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
865bb325 | 188 | extern "C" { |
ce2f50e3 VZ |
189 | static void |
190 | gtk_insert_text_callback(GtkEditable *editable, | |
191 | const gchar *new_text, | |
192 | gint new_text_length, | |
193 | gint *position, | |
194 | wxTextCtrl *win) | |
195 | { | |
76fcf0f2 RR |
196 | if (g_isIdle) |
197 | wxapp_install_idle_handler(); | |
198 | ||
ce2f50e3 VZ |
199 | // we should only be called if we have a max len limit at all |
200 | GtkEntry *entry = GTK_ENTRY (editable); | |
201 | ||
202 | wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") ); | |
203 | ||
204 | // check that we don't overflow the max length limit | |
205 | // | |
206 | // FIXME: this doesn't work when we paste a string which is going to be | |
207 | // truncated | |
208 | if ( entry->text_length == entry->text_max_length ) | |
209 | { | |
210 | // we don't need to run the base class version at all | |
9fa72bd2 | 211 | g_signal_stop_emission_by_name (editable, "insert_text"); |
ce2f50e3 VZ |
212 | |
213 | // remember that the next changed signal is to be ignored to avoid | |
214 | // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event | |
215 | win->IgnoreNextTextUpdate(); | |
216 | ||
217 | // and generate the correct one ourselves | |
218 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId()); | |
219 | event.SetEventObject(win); | |
220 | event.SetString(win->GetValue()); | |
221 | win->GetEventHandler()->ProcessEvent( event ); | |
222 | } | |
223 | } | |
865bb325 | 224 | } |
ce2f50e3 | 225 | |
9440c3d0 KH |
226 | // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp, |
227 | ||
865bb325 | 228 | extern "C" { |
9440c3d0 KH |
229 | static void |
230 | au_apply_tag_callback(GtkTextBuffer *buffer, | |
231 | GtkTextTag *tag, | |
232 | GtkTextIter *start, | |
233 | GtkTextIter *end, | |
234 | gpointer textctrl) | |
235 | { | |
236 | if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl")) | |
9fa72bd2 | 237 | g_signal_stop_emission_by_name (buffer, "apply_tag"); |
9440c3d0 | 238 | } |
865bb325 | 239 | } |
9440c3d0 KH |
240 | |
241 | //----------------------------------------------------------------------------- | |
242 | // GtkTextCharPredicates for gtk_text_iter_*_find_char | |
243 | //----------------------------------------------------------------------------- | |
244 | ||
865bb325 | 245 | extern "C" { |
9440c3d0 KH |
246 | static gboolean |
247 | pred_whitespace (gunichar ch, gpointer user_data) | |
248 | { | |
249 | return g_unichar_isspace(ch); | |
250 | } | |
865bb325 | 251 | } |
9440c3d0 | 252 | |
865bb325 | 253 | extern "C" { |
9440c3d0 KH |
254 | static gboolean |
255 | pred_non_whitespace (gunichar ch, gpointer user_data) | |
256 | { | |
257 | return !g_unichar_isspace(ch); | |
258 | } | |
865bb325 | 259 | } |
9440c3d0 | 260 | |
865bb325 | 261 | extern "C" { |
9440c3d0 KH |
262 | static gboolean |
263 | pred_nonpunct (gunichar ch, gpointer user_data) | |
264 | { | |
265 | return !g_unichar_ispunct(ch); | |
266 | } | |
865bb325 | 267 | } |
9440c3d0 | 268 | |
865bb325 | 269 | extern "C" { |
9440c3d0 KH |
270 | static gboolean |
271 | pred_nonpunct_or_slash (gunichar ch, gpointer user_data) | |
272 | { | |
273 | return !g_unichar_ispunct(ch) || ch == '/'; | |
274 | } | |
865bb325 | 275 | } |
9440c3d0 KH |
276 | |
277 | //----------------------------------------------------------------------------- | |
278 | // Check for links between s and e and correct tags as necessary | |
279 | //----------------------------------------------------------------------------- | |
280 | ||
281 | // This function should be made match better while being efficient at one point. | |
282 | // Most probably with a row of regular expressions. | |
865bb325 | 283 | extern "C" { |
9440c3d0 KH |
284 | static void |
285 | au_check_word( GtkTextIter *s, GtkTextIter *e ) | |
286 | { | |
287 | static const char *URIPrefixes[] = | |
288 | { | |
289 | "http://", | |
290 | "ftp://", | |
291 | "www.", | |
292 | "ftp.", | |
293 | "mailto://", | |
294 | "https://", | |
295 | "file://", | |
296 | "nntp://", | |
297 | "news://", | |
298 | "telnet://", | |
299 | "mms://", | |
300 | "gopher://", | |
301 | "prospero://", | |
302 | "wais://", | |
303 | }; | |
304 | ||
305 | GtkTextIter start = *s, end = *e; | |
306 | GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s); | |
418cf02e | 307 | |
9440c3d0 KH |
308 | // Get our special link tag |
309 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"); | |
310 | ||
311 | // Get rid of punctuation from beginning and end. | |
312 | // Might want to move this to au_check_range if an improved link checking doesn't | |
313 | // use some intelligent punctuation checking itself (beware of undesired iter modifications). | |
314 | if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) ) | |
315 | gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e ); | |
316 | ||
317 | gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start ); | |
318 | gtk_text_iter_forward_char(&end); | |
319 | ||
320 | gchar* text = gtk_text_iter_get_text( &start, &end ); | |
321 | size_t len = strlen(text), prefix_len; | |
322 | size_t n; | |
323 | ||
324 | for( n = 0; n < WXSIZEOF(URIPrefixes); ++n ) | |
325 | { | |
326 | prefix_len = strlen(URIPrefixes[n]); | |
327 | if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len)) | |
328 | break; | |
329 | } | |
330 | ||
331 | if(n < WXSIZEOF(URIPrefixes)) | |
332 | { | |
9fa72bd2 MR |
333 | gulong signal_id = g_signal_handler_find (buffer, |
334 | (GSignalMatchType) (G_SIGNAL_MATCH_FUNC), | |
335 | 0, 0, NULL, | |
336 | (gpointer)au_apply_tag_callback, NULL); | |
9440c3d0 | 337 | |
9fa72bd2 | 338 | g_signal_handler_block (buffer, signal_id); |
9440c3d0 | 339 | gtk_text_buffer_apply_tag(buffer, tag, &start, &end); |
9fa72bd2 | 340 | g_signal_handler_unblock (buffer, signal_id); |
9440c3d0 KH |
341 | } |
342 | } | |
865bb325 | 343 | } |
9440c3d0 | 344 | |
865bb325 | 345 | extern "C" { |
9440c3d0 KH |
346 | static void |
347 | au_check_range(GtkTextIter *s, | |
348 | GtkTextIter *range_end) | |
349 | { | |
350 | GtkTextIter range_start = *s; | |
351 | GtkTextIter word_end; | |
352 | GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s); | |
353 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"); | |
354 | ||
355 | gtk_text_buffer_remove_tag(buffer, tag, s, range_end); | |
356 | ||
357 | if(g_unichar_isspace(gtk_text_iter_get_char(&range_start))) | |
358 | gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end); | |
359 | ||
360 | while(!gtk_text_iter_equal(&range_start, range_end)) | |
361 | { | |
362 | word_end = range_start; | |
363 | gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end); | |
364 | ||
365 | // Now we should have a word delimited by range_start and word_end, correct link tags | |
366 | au_check_word(&range_start, &word_end); | |
367 | ||
368 | range_start = word_end; | |
369 | gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end); | |
370 | } | |
371 | } | |
865bb325 | 372 | } |
9440c3d0 KH |
373 | |
374 | //----------------------------------------------------------------------------- | |
375 | // "insert-text" for GtkTextBuffer | |
376 | //----------------------------------------------------------------------------- | |
377 | ||
865bb325 | 378 | extern "C" { |
9440c3d0 KH |
379 | static void |
380 | au_insert_text_callback(GtkTextBuffer *buffer, | |
381 | GtkTextIter *end, | |
382 | gchar *text, | |
383 | gint len, | |
384 | wxTextCtrl *win) | |
385 | { | |
386 | if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) | |
387 | return; | |
388 | ||
389 | GtkTextIter start = *end; | |
390 | gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len)); | |
391 | ||
392 | GtkTextIter line_start = start; | |
393 | GtkTextIter line_end = *end; | |
394 | GtkTextIter words_start = start; | |
395 | GtkTextIter words_end = *end; | |
396 | ||
397 | gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start)); | |
398 | gtk_text_iter_forward_to_line_end(&line_end); | |
399 | gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start); | |
400 | gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end); | |
401 | ||
402 | au_check_range(&words_start, &words_end); | |
403 | } | |
865bb325 | 404 | } |
9440c3d0 KH |
405 | |
406 | //----------------------------------------------------------------------------- | |
407 | // "delete-range" for GtkTextBuffer | |
408 | //----------------------------------------------------------------------------- | |
409 | ||
865bb325 | 410 | extern "C" { |
9440c3d0 KH |
411 | static void |
412 | au_delete_range_callback(GtkTextBuffer *buffer, | |
413 | GtkTextIter *start, | |
414 | GtkTextIter *end, | |
415 | wxTextCtrl *win) | |
416 | { | |
417 | if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) | |
418 | return; | |
419 | ||
420 | GtkTextIter line_start = *start, line_end = *end; | |
421 | ||
422 | gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start)); | |
423 | gtk_text_iter_forward_to_line_end(&line_end); | |
424 | gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start); | |
425 | gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end); | |
426 | ||
427 | au_check_range(start, end); | |
428 | } | |
865bb325 | 429 | } |
9440c3d0 KH |
430 | |
431 | ||
c801d85f | 432 | //----------------------------------------------------------------------------- |
2f2aa628 | 433 | // "changed" |
c801d85f KB |
434 | //----------------------------------------------------------------------------- |
435 | ||
865bb325 | 436 | extern "C" { |
805dd538 | 437 | static void |
ba3f6b44 | 438 | gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win ) |
484e45bf | 439 | { |
ce2f50e3 VZ |
440 | if ( win->IgnoreTextUpdate() ) |
441 | return; | |
442 | ||
a2053b27 | 443 | if (!win->m_hasVMT) return; |
805dd538 | 444 | |
bb69661b | 445 | if (g_isIdle) |
3c679789 | 446 | wxapp_install_idle_handler(); |
a8bf1826 | 447 | |
6964cbba VZ |
448 | if ( win->MarkDirtyOnChange() ) |
449 | win->MarkDirty(); | |
a8bf1826 | 450 | |
ee2ec18e | 451 | win->SendTextUpdatedEvent(); |
6de97a3b | 452 | } |
865bb325 | 453 | } |
112892b9 | 454 | |
c85f2eb1 VZ |
455 | //----------------------------------------------------------------------------- |
456 | // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard" | |
457 | //----------------------------------------------------------------------------- | |
458 | ||
459 | // common part of the event handlers below | |
460 | static void | |
461 | handle_text_clipboard_callback( GtkWidget *widget, wxTextCtrl *win, | |
9eddec69 | 462 | wxEventType eventType, const gchar * signal_name) |
c85f2eb1 VZ |
463 | { |
464 | wxClipboardTextEvent event( eventType, win->GetId() ); | |
465 | event.SetEventObject( win ); | |
466 | if ( win->GetEventHandler()->ProcessEvent( event ) ) | |
467 | { | |
468 | // don't let the default processing to take place if we did something | |
469 | // ourselves in the event handler | |
9eddec69 | 470 | g_signal_stop_emission_by_name (widget, signal_name); |
c85f2eb1 VZ |
471 | } |
472 | } | |
473 | ||
474 | extern "C" { | |
475 | static void | |
476 | gtk_copy_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) | |
477 | { | |
9eddec69 WS |
478 | handle_text_clipboard_callback( |
479 | widget, win, wxEVT_COMMAND_TEXT_COPY, "copy-clipboard" ); | |
c85f2eb1 VZ |
480 | } |
481 | ||
482 | static void | |
483 | gtk_cut_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) | |
484 | { | |
9eddec69 WS |
485 | handle_text_clipboard_callback( |
486 | widget, win, wxEVT_COMMAND_TEXT_CUT, "cut-clipboard" ); | |
c85f2eb1 VZ |
487 | } |
488 | ||
489 | static void | |
490 | gtk_paste_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) | |
491 | { | |
9eddec69 WS |
492 | handle_text_clipboard_callback( |
493 | widget, win, wxEVT_COMMAND_TEXT_PASTE, "paste-clipboard" ); | |
c85f2eb1 VZ |
494 | } |
495 | } | |
496 | ||
41b81aed RR |
497 | //----------------------------------------------------------------------------- |
498 | // "expose_event" from scrolled window and textview | |
499 | //----------------------------------------------------------------------------- | |
500 | ||
865bb325 | 501 | extern "C" { |
41b81aed RR |
502 | static gboolean |
503 | gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win ) | |
504 | { | |
505 | return TRUE; | |
506 | } | |
865bb325 | 507 | } |
1c35b54e | 508 | |
1c35b54e | 509 | |
2f2aa628 RR |
510 | //----------------------------------------------------------------------------- |
511 | // wxTextCtrl | |
512 | //----------------------------------------------------------------------------- | |
513 | ||
9d112688 | 514 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) |
2f2aa628 | 515 | |
9d112688 | 516 | BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) |
2830bf19 | 517 | EVT_CHAR(wxTextCtrl::OnChar) |
e702ff0f JS |
518 | |
519 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
520 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
521 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
522 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
523 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
524 | ||
525 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
526 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
527 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
528 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
529 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
9440c3d0 | 530 | |
9440c3d0 KH |
531 | // wxTE_AUTO_URL wxTextUrl support. Currently only creates |
532 | // wxTextUrlEvent in the same cases as wxMSW, more can be added here. | |
533 | EVT_MOTION (wxTextCtrl::OnUrlMouseEvent) | |
534 | EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
535 | EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent) | |
536 | EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent) | |
537 | EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
538 | EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent) | |
539 | EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent) | |
c801d85f KB |
540 | END_EVENT_TABLE() |
541 | ||
01041145 | 542 | void wxTextCtrl::Init() |
f5abe911 | 543 | { |
6964cbba | 544 | m_dontMarkDirty = |
7d8268a1 | 545 | m_modified = false; |
6964cbba | 546 | |
f6519b40 VZ |
547 | m_countUpdatesToIgnore = 0; |
548 | ||
7d8268a1 | 549 | SetUpdateFont(false); |
6964cbba | 550 | |
85396430 | 551 | m_text = NULL; |
41b81aed | 552 | m_frozenness = 0; |
9440c3d0 KH |
553 | m_gdkHandCursor = NULL; |
554 | m_gdkXTermCursor = NULL; | |
9440c3d0 KH |
555 | } |
556 | ||
557 | wxTextCtrl::~wxTextCtrl() | |
558 | { | |
9440c3d0 KH |
559 | if(m_gdkHandCursor) |
560 | gdk_cursor_unref(m_gdkHandCursor); | |
561 | if(m_gdkXTermCursor) | |
562 | gdk_cursor_unref(m_gdkXTermCursor); | |
f5abe911 | 563 | } |
13289f04 | 564 | |
13111b2a VZ |
565 | wxTextCtrl::wxTextCtrl( wxWindow *parent, |
566 | wxWindowID id, | |
567 | const wxString &value, | |
568 | const wxPoint &pos, | |
569 | const wxSize &size, | |
570 | long style, | |
571 | const wxValidator& validator, | |
572 | const wxString &name ) | |
f5abe911 | 573 | { |
01041145 VZ |
574 | Init(); |
575 | ||
f5abe911 RR |
576 | Create( parent, id, value, pos, size, style, validator, name ); |
577 | } | |
c801d85f | 578 | |
13111b2a VZ |
579 | bool wxTextCtrl::Create( wxWindow *parent, |
580 | wxWindowID id, | |
581 | const wxString &value, | |
582 | const wxPoint &pos, | |
583 | const wxSize &size, | |
584 | long style, | |
585 | const wxValidator& validator, | |
586 | const wxString &name ) | |
c801d85f | 587 | { |
7d8268a1 WS |
588 | m_needParent = true; |
589 | m_acceptsFocus = true; | |
484e45bf | 590 | |
4dcaf11a RR |
591 | if (!PreCreation( parent, pos, size ) || |
592 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
593 | { | |
223d09f6 | 594 | wxFAIL_MSG( wxT("wxTextCtrl creation failed") ); |
7d8268a1 | 595 | return false; |
4dcaf11a | 596 | } |
6de97a3b | 597 | |
2830bf19 | 598 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
a8bf1826 | 599 | |
ab46dc18 | 600 | if (multi_line) |
2830bf19 | 601 | { |
fab591c5 RR |
602 | // Create view |
603 | m_text = gtk_text_view_new(); | |
a8bf1826 | 604 | |
41b81aed | 605 | m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
a8bf1826 | 606 | |
fab591c5 RR |
607 | // create scrolled window |
608 | m_widget = gtk_scrolled_window_new( NULL, NULL ); | |
609 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ), | |
610 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
add7cadd PC |
611 | // for ScrollLines/Pages |
612 | m_scrollBar[1] = (GtkRange*)((GtkScrolledWindow*)m_widget)->vscrollbar; | |
a8bf1826 | 613 | |
fab591c5 RR |
614 | // Insert view into scrolled window |
615 | gtk_container_add( GTK_CONTAINER(m_widget), m_text ); | |
a8bf1826 | 616 | |
c2110823 | 617 | // translate wx wrapping style to GTK+ |
9ddb3948 | 618 | GtkWrapMode wrap; |
c2110823 | 619 | if ( HasFlag( wxTE_DONTWRAP ) ) |
9ddb3948 | 620 | wrap = GTK_WRAP_NONE; |
c4590236 | 621 | else if ( HasFlag( wxTE_CHARWRAP ) ) |
9ddb3948 | 622 | wrap = GTK_WRAP_CHAR; |
c4590236 | 623 | else if ( HasFlag( wxTE_WORDWRAP ) ) |
9ddb3948 | 624 | wrap = GTK_WRAP_WORD; |
c4590236 VZ |
625 | else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 |
626 | { | |
627 | // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 | |
628 | #ifdef __WXGTK24__ | |
c79146df VZ |
629 | if ( !gtk_check_version(2,4,0) ) |
630 | { | |
631 | wrap = GTK_WRAP_WORD_CHAR; | |
632 | } | |
633 | else | |
c4590236 | 634 | #endif |
34b8f3ef | 635 | wrap = GTK_WRAP_WORD; |
c4590236 | 636 | } |
9ddb3948 VZ |
637 | |
638 | gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); | |
805dd538 | 639 | |
6493aaca | 640 | GtkScrolledWindowSetBorder(m_widget, style); |
7d8268a1 | 641 | |
e327fddf KH |
642 | gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK ); |
643 | ||
055e633d | 644 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
2830bf19 RR |
645 | } |
646 | else | |
647 | { | |
fab591c5 | 648 | // a single-line text control: no need for scrollbars |
2830bf19 | 649 | m_widget = |
1c35b54e | 650 | m_text = gtk_entry_new(); |
44c5573d | 651 | |
02a6e358 | 652 | if (style & wxNO_BORDER) |
7d1fea10 | 653 | g_object_set (m_text, "has-frame", FALSE, NULL); |
2830bf19 | 654 | } |
484e45bf | 655 | |
db434467 | 656 | m_parent->DoAddChild( this ); |
eda40bfc | 657 | |
76fcf0f2 | 658 | m_focusWidget = m_text; |
db434467 | 659 | |
abdeb9e7 | 660 | PostCreation(size); |
484e45bf | 661 | |
2830bf19 | 662 | if (multi_line) |
0c131a5a | 663 | { |
2830bf19 | 664 | gtk_widget_show(m_text); |
0c131a5a | 665 | } |
13289f04 | 666 | |
7d8268a1 | 667 | if (!value.empty()) |
2830bf19 | 668 | { |
fab591c5 | 669 | SetValue( value ); |
2830bf19 | 670 | } |
484e45bf | 671 | |
2830bf19 RR |
672 | if (style & wxTE_PASSWORD) |
673 | { | |
674 | if (!multi_line) | |
675 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
676 | } | |
8bbe427f | 677 | |
2830bf19 RR |
678 | if (style & wxTE_READONLY) |
679 | { | |
680 | if (!multi_line) | |
6f85e712 | 681 | gtk_editable_set_editable( GTK_EDITABLE(m_text), FALSE ); |
fab591c5 | 682 | else |
98a8daf4 | 683 | gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE); |
98a8daf4 VS |
684 | } |
685 | ||
c663fbea VS |
686 | if (multi_line) |
687 | { | |
688 | if (style & wxTE_RIGHT) | |
689 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT ); | |
690 | else if (style & wxTE_CENTRE) | |
691 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER ); | |
692 | // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT | |
693 | } | |
c663fbea VS |
694 | else |
695 | { | |
77f70672 RR |
696 | #ifdef __WXGTK24__ |
697 | // gtk_entry_set_alignment was introduced in gtk+-2.3.5 | |
698 | if (!gtk_check_version(2,4,0)) | |
699 | { | |
700 | if (style & wxTE_RIGHT) | |
701 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 ); | |
702 | else if (style & wxTE_CENTRE) | |
703 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 ); | |
704 | } | |
705 | #endif | |
c663fbea | 706 | } |
7d8268a1 | 707 | |
fab591c5 | 708 | // We want to be notified about text changes. |
fab591c5 RR |
709 | if (multi_line) |
710 | { | |
9fa72bd2 MR |
711 | g_signal_connect (m_buffer, "changed", |
712 | G_CALLBACK (gtk_text_changed_callback), this); | |
9440c3d0 KH |
713 | |
714 | // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style | |
715 | if (style & wxTE_AUTO_URL) | |
716 | { | |
717 | GtkTextIter start, end; | |
718 | m_gdkHandCursor = gdk_cursor_new(GDK_HAND2); | |
719 | m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM); | |
720 | ||
721 | // We create our wxUrl tag here for slight efficiency gain - we | |
722 | // don't have to check for the tag existance in callbacks, | |
723 | // hereby it's guaranteed to exist. | |
724 | gtk_text_buffer_create_tag(m_buffer, "wxUrl", | |
725 | "foreground", "blue", | |
726 | "underline", PANGO_UNDERLINE_SINGLE, | |
727 | NULL); | |
728 | ||
729 | // Check for URLs after each text change | |
9fa72bd2 MR |
730 | g_signal_connect_after (m_buffer, "insert_text", |
731 | G_CALLBACK (au_insert_text_callback), this); | |
732 | g_signal_connect_after (m_buffer, "delete_range", | |
733 | G_CALLBACK (au_delete_range_callback), this); | |
9440c3d0 KH |
734 | |
735 | // Block all wxUrl tag applying unless we do it ourselves, in which case we | |
736 | // block this callback temporarily. This takes care of gtk+ internal | |
737 | // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise, | |
738 | // which is undesired because only a part of the URL might be copied. | |
739 | // The insert-text signal emitted inside it will take care of newly formed | |
740 | // or wholly copied URLs. | |
9fa72bd2 MR |
741 | g_signal_connect (m_buffer, "apply_tag", |
742 | G_CALLBACK (au_apply_tag_callback), NULL); | |
9440c3d0 KH |
743 | |
744 | // Check for URLs in the initial string passed to Create | |
745 | gtk_text_buffer_get_start_iter(m_buffer, &start); | |
746 | gtk_text_buffer_get_end_iter(m_buffer, &end); | |
747 | au_check_range(&start, &end); | |
748 | } | |
fab591c5 RR |
749 | } |
750 | else | |
fab591c5 | 751 | { |
9fa72bd2 MR |
752 | g_signal_connect (m_text, "changed", |
753 | G_CALLBACK (gtk_text_changed_callback), this); | |
fab591c5 | 754 | } |
ce16e5d7 | 755 | |
c85f2eb1 VZ |
756 | g_signal_connect (m_text, "copy-clipboard", |
757 | G_CALLBACK (gtk_copy_clipboard_callback), this); | |
758 | g_signal_connect (m_text, "cut-clipboard", | |
759 | G_CALLBACK (gtk_cut_clipboard_callback), this); | |
760 | g_signal_connect (m_text, "paste-clipboard", | |
761 | G_CALLBACK (gtk_paste_clipboard_callback), this); | |
762 | ||
65045edd | 763 | m_cursor = wxCursor( wxCURSOR_IBEAM ); |
13111b2a | 764 | |
9d522606 | 765 | wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont()); |
17665a2b VZ |
766 | SetDefaultStyle( attrDef ); |
767 | ||
7d8268a1 | 768 | return true; |
2830bf19 | 769 | } |
484e45bf | 770 | |
9d522606 | 771 | |
2830bf19 RR |
772 | void wxTextCtrl::CalculateScrollbar() |
773 | { | |
6de97a3b | 774 | } |
c801d85f | 775 | |
03f38c58 | 776 | wxString wxTextCtrl::GetValue() const |
c801d85f | 777 | { |
902725ee | 778 | wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") ); |
8bbe427f | 779 | |
2830bf19 | 780 | wxString tmp; |
75812722 | 781 | if ( IsMultiLine() ) |
2830bf19 | 782 | { |
fab591c5 | 783 | GtkTextIter start; |
41b81aed | 784 | gtk_text_buffer_get_start_iter( m_buffer, &start ); |
fab591c5 | 785 | GtkTextIter end; |
41b81aed | 786 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
e808cf8a | 787 | wxGtkString text(gtk_text_buffer_get_text(m_buffer, &start, &end, true)); |
5b87f8bf | 788 | |
a3669332 VZ |
789 | const wxWxCharBuffer buf = wxGTK_CONV_BACK(text); |
790 | if ( buf ) | |
791 | tmp = buf; | |
2830bf19 RR |
792 | } |
793 | else | |
794 | { | |
a3669332 VZ |
795 | const gchar *text = gtk_entry_get_text( GTK_ENTRY(m_text) ); |
796 | const wxWxCharBuffer buf = wxGTK_CONV_BACK( text ); | |
797 | if ( buf ) | |
798 | tmp = buf; | |
2830bf19 | 799 | } |
a8bf1826 | 800 | |
2830bf19 | 801 | return tmp; |
6de97a3b | 802 | } |
c801d85f | 803 | |
0d91b234 VZ |
804 | wxFontEncoding wxTextCtrl::GetTextEncoding() const |
805 | { | |
806 | // GTK+ uses UTF-8 internally, we need to convert to it but from which | |
807 | // encoding? | |
808 | ||
809 | // first check the default text style (we intentionally don't check the | |
810 | // style for the current position as it doesn't make sense for SetValue()) | |
811 | const wxTextAttr& style = GetDefaultStyle(); | |
812 | wxFontEncoding enc = style.HasFont() ? style.GetFont().GetEncoding() | |
813 | : wxFONTENCODING_SYSTEM; | |
814 | ||
815 | // fall back to the controls font if no style | |
816 | if ( enc == wxFONTENCODING_SYSTEM && m_hasFont ) | |
817 | enc = GetFont().GetEncoding(); | |
818 | ||
819 | return enc; | |
820 | } | |
821 | ||
28fdd8db VZ |
822 | bool wxTextCtrl::IsEmpty() const |
823 | { | |
824 | if ( IsMultiLine() ) | |
825 | return gtk_text_buffer_get_char_count(m_buffer) != 0; | |
826 | ||
827 | return wxTextCtrlBase::IsEmpty(); | |
828 | } | |
829 | ||
f6519b40 | 830 | void wxTextCtrl::DoSetValue( const wxString &value, int flags ) |
c801d85f | 831 | { |
223d09f6 | 832 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 833 | |
6964cbba VZ |
834 | // the control won't be modified any more as we programmatically replace |
835 | // all the existing text, so reset the flag and don't set it again (and do | |
836 | // it now, before the text event handler is ran so that IsModified() called | |
837 | // from there returns the expected value) | |
838 | m_modified = false; | |
839 | DontMarkDirtyOnNextChange(); | |
840 | ||
7e0fff42 VZ |
841 | const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding())); |
842 | if ( !buffer ) | |
2830bf19 | 843 | { |
7e0fff42 VZ |
844 | // see comment in WriteText() as to why we must warn the user about |
845 | // this | |
846 | wxLogWarning(_("Failed to set text in the text control.")); | |
847 | return; | |
848 | } | |
418cf02e | 849 | |
7e0fff42 VZ |
850 | // if the control is not empty, two "changed" signals are emitted, |
851 | // otherwise only one and we need to ignore either both or one of them | |
852 | int ignore = flags & SetValue_SendEvent ? 0 : 1; | |
853 | if ( !IsEmpty() ) | |
854 | ignore++; | |
f6519b40 | 855 | |
7e0fff42 VZ |
856 | if ( ignore ) |
857 | IgnoreNextTextUpdate(ignore); | |
a3669332 | 858 | |
7e0fff42 VZ |
859 | if ( IsMultiLine() ) |
860 | { | |
41b81aed | 861 | gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) ); |
2830bf19 | 862 | } |
265c34ee | 863 | else // single line |
2830bf19 | 864 | { |
7e0fff42 | 865 | gtk_entry_set_text( GTK_ENTRY(m_text), buffer ); |
2830bf19 | 866 | } |
f6bcfd97 BP |
867 | |
868 | // GRG, Jun/2000: Changed this after a lot of discussion in | |
77ffb593 | 869 | // the lists. wxWidgets 2.2 will have a set of flags to |
f6bcfd97 BP |
870 | // customize this behaviour. |
871 | SetInsertionPoint(0); | |
6de97a3b | 872 | } |
c801d85f KB |
873 | |
874 | void wxTextCtrl::WriteText( const wxString &text ) | |
875 | { | |
223d09f6 | 876 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 877 | |
17665a2b VZ |
878 | if ( text.empty() ) |
879 | return; | |
484e45bf | 880 | |
0d91b234 VZ |
881 | // check if we have a specific style for the current position |
882 | wxFontEncoding enc = wxFONTENCODING_SYSTEM; | |
883 | wxTextAttr style; | |
884 | if ( GetStyle(GetInsertionPoint(), style) && style.HasFont() ) | |
885 | { | |
886 | enc = style.GetFont().GetEncoding(); | |
887 | } | |
888 | ||
889 | if ( enc == wxFONTENCODING_SYSTEM ) | |
890 | enc = GetTextEncoding(); | |
891 | ||
892 | const wxCharBuffer buffer(wxGTK_CONV_ENC(text, enc)); | |
a3669332 VZ |
893 | if ( !buffer ) |
894 | { | |
0d91b234 VZ |
895 | // we must log an error here as losing the text like this can be a |
896 | // serious problem (e.g. imagine the document edited by user being | |
897 | // empty instead of containing the correct text) | |
898 | wxLogWarning(_("Failed to insert text in the control.")); | |
a3669332 VZ |
899 | return; |
900 | } | |
901 | ||
6964cbba VZ |
902 | // we're changing the text programmatically |
903 | DontMarkDirtyOnNextChange(); | |
c04ec496 | 904 | |
75812722 | 905 | if ( IsMultiLine() ) |
2830bf19 | 906 | { |
ea5449ae RD |
907 | // First remove the selection if there is one |
908 | // TODO: Is there an easier GTK specific way to do this? | |
909 | long from, to; | |
910 | GetSelection(&from, &to); | |
911 | if (from != to) | |
912 | Remove(from, to); | |
913 | ||
914 | // Insert the text | |
41b81aed | 915 | wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer ); |
a8bf1826 | 916 | |
71aba833 VZ |
917 | GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) ); |
918 | // Scroll to cursor, but only if scrollbar thumb is at the very bottom | |
c77a6796 | 919 | if ( wxIsSameDouble(adj->value, adj->upper - adj->page_size) ) |
71aba833 VZ |
920 | { |
921 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), | |
41b81aed | 922 | gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 ); |
71aba833 | 923 | } |
2830bf19 | 924 | } |
17665a2b | 925 | else // single line |
2830bf19 | 926 | { |
2b5f62a0 VZ |
927 | // First remove the selection if there is one |
928 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); | |
929 | ||
ba3f6b44 | 930 | // This moves the cursor pos to behind the inserted text. |
afa7bd1e | 931 | gint len = gtk_editable_get_position(GTK_EDITABLE(m_text)); |
a8bf1826 | 932 | |
fab591c5 | 933 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len ); |
a8bf1826 | 934 | |
ba3f6b44 | 935 | // Bring entry's cursor uptodate. |
6f85e712 | 936 | gtk_editable_set_position( GTK_EDITABLE(m_text), len ); |
2830bf19 | 937 | } |
6de97a3b | 938 | } |
c801d85f | 939 | |
a6e21573 HH |
940 | void wxTextCtrl::AppendText( const wxString &text ) |
941 | { | |
ba3f6b44 RR |
942 | SetInsertionPointEnd(); |
943 | WriteText( text ); | |
944 | } | |
2df7be7f | 945 | |
ba3f6b44 RR |
946 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
947 | { | |
e808cf8a | 948 | wxString result; |
75812722 | 949 | if ( IsMultiLine() ) |
a6e21573 | 950 | { |
905f2110 | 951 | GtkTextIter line; |
41b81aed | 952 | gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo); |
8c6785f0 MR |
953 | GtkTextIter end = line; |
954 | gtk_text_iter_forward_to_line_end(&end); | |
e808cf8a PC |
955 | wxGtkString text(gtk_text_buffer_get_text(m_buffer, &line, &end, true)); |
956 | result = wxGTK_CONV_BACK(text); | |
a6e21573 | 957 | } |
ba3f6b44 | 958 | else |
a81258be | 959 | { |
e808cf8a PC |
960 | if (lineNo == 0) |
961 | result = GetValue(); | |
a81258be | 962 | } |
e808cf8a | 963 | return result; |
6de97a3b | 964 | } |
c801d85f | 965 | |
a81258be RR |
966 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
967 | { | |
ac0d36b5 HH |
968 | /* If you implement this, don't forget to update the documentation! |
969 | * (file docs/latex/wx/text.tex) */ | |
223d09f6 | 970 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); |
a81258be | 971 | } |
112892b9 | 972 | |
0efe5ba7 | 973 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 974 | { |
75812722 | 975 | if ( IsMultiLine() ) |
805dd538 | 976 | { |
f29a481a | 977 | GtkTextIter iter; |
01207937 MR |
978 | |
979 | if (pos > GetLastPosition()) | |
980 | return false; | |
981 | ||
f29a481a | 982 | gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos); |
f29a481a | 983 | |
50259962 MR |
984 | if ( y ) |
985 | *y = gtk_text_iter_get_line(&iter); | |
986 | if ( x ) | |
987 | *x = gtk_text_iter_get_line_offset(&iter); | |
805dd538 | 988 | } |
96385642 VZ |
989 | else // single line control |
990 | { | |
2829d9e3 | 991 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 | 992 | { |
50259962 MR |
993 | if ( y ) |
994 | *y = 0; | |
995 | if ( x ) | |
996 | *x = pos; | |
96385642 VZ |
997 | } |
998 | else | |
999 | { | |
1000 | // index out of bounds | |
7d8268a1 | 1001 | return false; |
96385642 | 1002 | } |
8bbe427f | 1003 | } |
96385642 | 1004 | |
7d8268a1 | 1005 | return true; |
6de97a3b | 1006 | } |
c801d85f | 1007 | |
e3ca08dd | 1008 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 1009 | { |
75812722 VZ |
1010 | if ( IsSingleLine() ) |
1011 | return 0; | |
805dd538 | 1012 | |
f29a481a | 1013 | GtkTextIter iter; |
21d23b88 MR |
1014 | if (y >= gtk_text_buffer_get_line_count (m_buffer)) |
1015 | return -1; | |
1016 | ||
1017 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y); | |
1018 | if (x >= gtk_text_iter_get_chars_in_line (&iter)) | |
1019 | return -1; | |
1020 | ||
1021 | return gtk_text_iter_get_offset(&iter) + x; | |
6de97a3b | 1022 | } |
c801d85f | 1023 | |
a81258be | 1024 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 1025 | { |
75812722 | 1026 | if ( IsMultiLine() ) |
f29a481a MR |
1027 | { |
1028 | int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1; | |
1029 | if (lineNo > last_line) | |
1030 | return -1; | |
1031 | ||
1032 | GtkTextIter iter; | |
1033 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo); | |
1034 | // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line | |
1035 | return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1); | |
1036 | } | |
1037 | else | |
f29a481a MR |
1038 | { |
1039 | wxString str = GetLineText (lineNo); | |
8e13c1ec | 1040 | return (int) str.length(); |
f29a481a | 1041 | } |
6de97a3b | 1042 | } |
c801d85f | 1043 | |
a81258be | 1044 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 1045 | { |
75812722 | 1046 | if ( IsMultiLine() ) |
e894be20 VZ |
1047 | { |
1048 | GtkTextIter iter; | |
1049 | gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, 0 ); | |
1050 | ||
1051 | // move forward by one display line until the end is reached | |
1052 | int lineCount = 1; | |
1053 | while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text), &iter) ) | |
1054 | { | |
1055 | lineCount++; | |
1056 | } | |
1057 | ||
1058 | // If the last character in the text buffer is a newline, | |
1059 | // gtk_text_view_forward_display_line() will return false without that | |
1060 | // line being counted. Must add one manually in that case. | |
8e13c1ec | 1061 | GtkTextIter lastCharIter; |
e894be20 VZ |
1062 | gtk_text_buffer_get_iter_at_offset |
1063 | ( | |
1064 | m_buffer, | |
1065 | &lastCharIter, | |
1066 | gtk_text_buffer_get_char_count(m_buffer) - 1 | |
1067 | ); | |
1068 | gchar lastChar = gtk_text_iter_get_char( &lastCharIter ); | |
1069 | if ( lastChar == wxT('\n') ) | |
1070 | lineCount++; | |
1071 | ||
1072 | return lineCount; | |
1073 | } | |
1074 | else // single line | |
1075 | { | |
96385642 | 1076 | return 1; |
e894be20 | 1077 | } |
6de97a3b | 1078 | } |
c801d85f | 1079 | |
debe6624 | 1080 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 1081 | { |
223d09f6 | 1082 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3358d36e | 1083 | |
74c5a810 | 1084 | if ( IsMultiLine() ) |
291a8f20 | 1085 | { |
fab591c5 | 1086 | GtkTextIter iter; |
41b81aed RR |
1087 | gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos ); |
1088 | gtk_text_buffer_place_cursor( m_buffer, &iter ); | |
74c5a810 VZ |
1089 | gtk_text_view_scroll_mark_onscreen |
1090 | ( | |
1091 | GTK_TEXT_VIEW(m_text), | |
41b81aed | 1092 | gtk_text_buffer_get_insert( m_buffer ) |
74c5a810 | 1093 | ); |
ac0d36b5 | 1094 | } |
2830bf19 | 1095 | else |
291a8f20 | 1096 | { |
6f85e712 | 1097 | // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2? |
afa7bd1e | 1098 | gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos)); |
291a8f20 | 1099 | } |
6de97a3b | 1100 | } |
c801d85f | 1101 | |
03f38c58 | 1102 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 1103 | { |
223d09f6 | 1104 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1105 | |
75812722 | 1106 | if ( IsMultiLine() ) |
fab591c5 | 1107 | { |
fab591c5 | 1108 | GtkTextIter end; |
41b81aed RR |
1109 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
1110 | gtk_text_buffer_place_cursor( m_buffer, &end ); | |
fab591c5 | 1111 | } |
d59051dd | 1112 | else |
fab591c5 | 1113 | { |
6f85e712 | 1114 | gtk_editable_set_position( GTK_EDITABLE(m_text), -1 ); |
fab591c5 | 1115 | } |
6de97a3b | 1116 | } |
c801d85f | 1117 | |
debe6624 | 1118 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 1119 | { |
223d09f6 | 1120 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1121 | |
75812722 | 1122 | if ( IsMultiLine() ) |
fab591c5 | 1123 | { |
fab591c5 | 1124 | gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable ); |
fab591c5 | 1125 | } |
2830bf19 | 1126 | else |
fab591c5 | 1127 | { |
6f85e712 | 1128 | gtk_editable_set_editable( GTK_EDITABLE(m_text), editable ); |
fab591c5 | 1129 | } |
6de97a3b | 1130 | } |
c801d85f | 1131 | |
68df5777 RR |
1132 | bool wxTextCtrl::Enable( bool enable ) |
1133 | { | |
1134 | if (!wxWindowBase::Enable(enable)) | |
1135 | { | |
1136 | // nothing to do | |
7d8268a1 | 1137 | return false; |
68df5777 | 1138 | } |
f6bcfd97 | 1139 | |
75812722 | 1140 | if ( IsMultiLine() ) |
68df5777 | 1141 | { |
fab591c5 | 1142 | SetEditable( enable ); |
68df5777 RR |
1143 | } |
1144 | else | |
1145 | { | |
1146 | gtk_widget_set_sensitive( m_text, enable ); | |
1147 | } | |
1148 | ||
7d8268a1 | 1149 | return true; |
68df5777 RR |
1150 | } |
1151 | ||
fdca68a6 JS |
1152 | // wxGTK-specific: called recursively by Enable, |
1153 | // to give widgets an oppprtunity to correct their colours after they | |
1154 | // have been changed by Enable | |
1155 | void wxTextCtrl::OnParentEnable( bool enable ) | |
1156 | { | |
1157 | // If we have a custom background colour, we use this colour in both | |
1158 | // disabled and enabled mode, or we end up with a different colour under the | |
1159 | // text. | |
1160 | wxColour oldColour = GetBackgroundColour(); | |
1161 | if (oldColour.Ok()) | |
1162 | { | |
1163 | // Need to set twice or it'll optimize the useful stuff out | |
1164 | if (oldColour == * wxWHITE) | |
1165 | SetBackgroundColour(*wxBLACK); | |
1166 | else | |
1167 | SetBackgroundColour(*wxWHITE); | |
1168 | SetBackgroundColour(oldColour); | |
1169 | } | |
1170 | } | |
1171 | ||
3a9fa0d6 VZ |
1172 | void wxTextCtrl::MarkDirty() |
1173 | { | |
7d8268a1 | 1174 | m_modified = true; |
3a9fa0d6 VZ |
1175 | } |
1176 | ||
0efe5ba7 VZ |
1177 | void wxTextCtrl::DiscardEdits() |
1178 | { | |
7d8268a1 | 1179 | m_modified = false; |
0efe5ba7 VZ |
1180 | } |
1181 | ||
ce2f50e3 VZ |
1182 | // ---------------------------------------------------------------------------- |
1183 | // max text length support | |
1184 | // ---------------------------------------------------------------------------- | |
1185 | ||
ce2f50e3 VZ |
1186 | bool wxTextCtrl::IgnoreTextUpdate() |
1187 | { | |
f6519b40 | 1188 | if ( m_countUpdatesToIgnore > 0 ) |
ce2f50e3 | 1189 | { |
f6519b40 | 1190 | m_countUpdatesToIgnore--; |
ce2f50e3 | 1191 | |
7d8268a1 | 1192 | return true; |
ce2f50e3 VZ |
1193 | } |
1194 | ||
7d8268a1 | 1195 | return false; |
ce2f50e3 VZ |
1196 | } |
1197 | ||
6964cbba VZ |
1198 | bool wxTextCtrl::MarkDirtyOnChange() |
1199 | { | |
1200 | if ( m_dontMarkDirty ) | |
1201 | { | |
1202 | m_dontMarkDirty = false; | |
1203 | ||
1204 | return false; | |
1205 | } | |
1206 | ||
1207 | return true; | |
1208 | } | |
1209 | ||
d7eee191 VZ |
1210 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1211 | { | |
1212 | if ( !HasFlag(wxTE_MULTILINE) ) | |
1213 | { | |
1214 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
ce2f50e3 VZ |
1215 | |
1216 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
1217 | // we had tried to enter more text than allowed by max text length and | |
1218 | // the text wasn't really changed | |
1219 | // | |
1220 | // to detect this and generate TEXT_MAXLEN event instead of | |
1221 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
1222 | // | |
1223 | // when max len is set to 0 we disconnect our handler as it means that | |
1224 | // we shouldn't check anything any more | |
1225 | if ( len ) | |
1226 | { | |
9fa72bd2 MR |
1227 | g_signal_connect (m_text, "insert_text", |
1228 | G_CALLBACK (gtk_insert_text_callback), this); | |
ce2f50e3 VZ |
1229 | } |
1230 | else // no checking | |
1231 | { | |
9fa72bd2 MR |
1232 | g_signal_handlers_disconnect_by_func (m_text, |
1233 | (gpointer) gtk_insert_text_callback, this); | |
ce2f50e3 | 1234 | } |
d7eee191 VZ |
1235 | } |
1236 | } | |
1237 | ||
debe6624 | 1238 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 1239 | { |
223d09f6 | 1240 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1241 | |
2b5f62a0 VZ |
1242 | if (from == -1 && to == -1) |
1243 | { | |
1244 | from = 0; | |
8e13c1ec | 1245 | to = GetValue().length(); |
2b5f62a0 VZ |
1246 | } |
1247 | ||
75812722 | 1248 | if ( IsMultiLine() ) |
fab591c5 | 1249 | { |
739e366a | 1250 | GtkTextIter fromi, toi; |
41b81aed RR |
1251 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); |
1252 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
739e366a | 1253 | |
41b81aed RR |
1254 | gtk_text_buffer_place_cursor( m_buffer, &toi ); |
1255 | gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi ); | |
fab591c5 RR |
1256 | } |
1257 | else | |
1258 | { | |
1259 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1260 | } | |
6de97a3b | 1261 | } |
c801d85f | 1262 | |
afbe906a | 1263 | void wxTextCtrl::ShowPosition( long pos ) |
c801d85f | 1264 | { |
75812722 | 1265 | if ( IsMultiLine() ) |
afbe906a | 1266 | { |
71aba833 | 1267 | GtkTextIter iter; |
41b81aed | 1268 | gtk_text_buffer_get_start_iter( m_buffer, &iter ); |
71aba833 | 1269 | gtk_text_iter_set_offset( &iter, pos ); |
41b81aed | 1270 | GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE ); |
71aba833 | 1271 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 ); |
afbe906a | 1272 | } |
6de97a3b | 1273 | } |
c801d85f | 1274 | |
692c9b86 VZ |
1275 | wxTextCtrlHitTestResult |
1276 | wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const | |
1277 | { | |
1278 | if ( !IsMultiLine() ) | |
1279 | { | |
1280 | // not supported | |
1281 | return wxTE_HT_UNKNOWN; | |
1282 | } | |
1283 | ||
1284 | int x, y; | |
1285 | gtk_text_view_window_to_buffer_coords | |
1286 | ( | |
1287 | GTK_TEXT_VIEW(m_text), | |
1288 | GTK_TEXT_WINDOW_TEXT, | |
1289 | pt.x, pt.y, | |
1290 | &x, &y | |
1291 | ); | |
1292 | ||
1293 | GtkTextIter iter; | |
1294 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); | |
1295 | if ( pos ) | |
1296 | *pos = gtk_text_iter_get_offset(&iter); | |
1297 | ||
1298 | return wxTE_HT_ON_TEXT; | |
1299 | } | |
1300 | ||
03f38c58 | 1301 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 1302 | { |
223d09f6 | 1303 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1304 | |
75812722 | 1305 | if ( IsMultiLine() ) |
fab591c5 | 1306 | { |
fab591c5 RR |
1307 | // There is no direct accessor for the cursor, but |
1308 | // internally, the cursor is the "mark" called | |
1309 | // "insert" in the text view's btree structure. | |
a8bf1826 | 1310 | |
41b81aed | 1311 | GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer ); |
fab591c5 | 1312 | GtkTextIter cursor; |
41b81aed | 1313 | gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark ); |
a8bf1826 | 1314 | |
fab591c5 RR |
1315 | return gtk_text_iter_get_offset( &cursor ); |
1316 | } | |
1317 | else | |
fab591c5 | 1318 | { |
afa7bd1e | 1319 | return (long) gtk_editable_get_position(GTK_EDITABLE(m_text)); |
fab591c5 | 1320 | } |
6de97a3b | 1321 | } |
c801d85f | 1322 | |
7d8268a1 | 1323 | wxTextPos wxTextCtrl::GetLastPosition() const |
c801d85f | 1324 | { |
223d09f6 | 1325 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1326 | |
2830bf19 | 1327 | int pos = 0; |
a8bf1826 | 1328 | |
75812722 | 1329 | if ( IsMultiLine() ) |
fab591c5 | 1330 | { |
fab591c5 | 1331 | GtkTextIter end; |
41b81aed | 1332 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
a8bf1826 | 1333 | |
fab591c5 | 1334 | pos = gtk_text_iter_get_offset( &end ); |
fab591c5 | 1335 | } |
2830bf19 | 1336 | else |
fab591c5 | 1337 | { |
2830bf19 | 1338 | pos = GTK_ENTRY(m_text)->text_length; |
fab591c5 | 1339 | } |
805dd538 | 1340 | |
ac0d36b5 | 1341 | return (long)pos; |
6de97a3b | 1342 | } |
c801d85f | 1343 | |
debe6624 | 1344 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 1345 | { |
223d09f6 | 1346 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1347 | |
75812722 | 1348 | if ( IsMultiLine() ) |
581ee8a9 | 1349 | { |
581ee8a9 | 1350 | GtkTextIter fromi, toi; |
41b81aed RR |
1351 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); |
1352 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
581ee8a9 | 1353 | |
41b81aed | 1354 | gtk_text_buffer_delete( m_buffer, &fromi, &toi ); |
581ee8a9 VZ |
1355 | } |
1356 | else // single line | |
68567a96 | 1357 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 1358 | } |
c801d85f | 1359 | |
debe6624 | 1360 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 1361 | { |
223d09f6 | 1362 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1363 | |
581ee8a9 | 1364 | Remove( from, to ); |
bb69661b | 1365 | |
7d8268a1 | 1366 | if (!value.empty()) |
2df7be7f | 1367 | { |
581ee8a9 VZ |
1368 | SetInsertionPoint( from ); |
1369 | WriteText( value ); | |
2df7be7f | 1370 | } |
6de97a3b | 1371 | } |
c801d85f | 1372 | |
03f38c58 | 1373 | void wxTextCtrl::Cut() |
c801d85f | 1374 | { |
223d09f6 | 1375 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1376 | |
75812722 | 1377 | if ( IsMultiLine() ) |
9fa72bd2 | 1378 | g_signal_emit_by_name (m_text, "cut-clipboard"); |
bbde2e29 | 1379 | else |
afa7bd1e | 1380 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text)); |
6de97a3b | 1381 | } |
c801d85f | 1382 | |
03f38c58 | 1383 | void wxTextCtrl::Copy() |
c801d85f | 1384 | { |
223d09f6 | 1385 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1386 | |
75812722 | 1387 | if ( IsMultiLine() ) |
9fa72bd2 | 1388 | g_signal_emit_by_name (m_text, "copy-clipboard"); |
bbde2e29 | 1389 | else |
afa7bd1e | 1390 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text)); |
6de97a3b | 1391 | } |
c801d85f | 1392 | |
03f38c58 | 1393 | void wxTextCtrl::Paste() |
c801d85f | 1394 | { |
223d09f6 | 1395 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1396 | |
75812722 | 1397 | if ( IsMultiLine() ) |
9fa72bd2 | 1398 | g_signal_emit_by_name (m_text, "paste-clipboard"); |
bbde2e29 | 1399 | else |
afa7bd1e | 1400 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text)); |
6de97a3b | 1401 | } |
c801d85f | 1402 | |
ca8b28f2 JS |
1403 | // Undo/redo |
1404 | void wxTextCtrl::Undo() | |
1405 | { | |
1406 | // TODO | |
223d09f6 | 1407 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
1408 | } |
1409 | ||
1410 | void wxTextCtrl::Redo() | |
1411 | { | |
1412 | // TODO | |
223d09f6 | 1413 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
1414 | } |
1415 | ||
1416 | bool wxTextCtrl::CanUndo() const | |
1417 | { | |
1418 | // TODO | |
4855a477 | 1419 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); |
7d8268a1 | 1420 | return false; |
ca8b28f2 JS |
1421 | } |
1422 | ||
1423 | bool wxTextCtrl::CanRedo() const | |
1424 | { | |
1425 | // TODO | |
4855a477 | 1426 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); |
7d8268a1 | 1427 | return false; |
ca8b28f2 JS |
1428 | } |
1429 | ||
1430 | // If the return values from and to are the same, there is no | |
1431 | // selection. | |
2d4cc5b6 | 1432 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const |
ca8b28f2 | 1433 | { |
223d09f6 | 1434 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
bb69661b | 1435 | |
5b87f8bf RD |
1436 | gint from = -1; |
1437 | gint to = -1; | |
7d8268a1 | 1438 | bool haveSelection = false; |
5b87f8bf | 1439 | |
75812722 | 1440 | if ( IsMultiLine() ) |
5b87f8bf | 1441 | { |
5b87f8bf | 1442 | GtkTextIter ifrom, ito; |
41b81aed | 1443 | if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) ) |
5b87f8bf | 1444 | { |
7d8268a1 | 1445 | haveSelection = true; |
5b87f8bf RD |
1446 | from = gtk_text_iter_get_offset(&ifrom); |
1447 | to = gtk_text_iter_get_offset(&ito); | |
1448 | } | |
1449 | } | |
1450 | else // not multi-line | |
1451 | { | |
1452 | if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text), | |
1453 | &from, &to) ) | |
1454 | { | |
7d8268a1 | 1455 | haveSelection = true; |
5b87f8bf RD |
1456 | } |
1457 | } | |
2d4cc5b6 | 1458 | |
5b87f8bf RD |
1459 | if (! haveSelection ) |
1460 | from = to = GetInsertionPoint(); | |
1461 | ||
1462 | if ( from > to ) | |
1463 | { | |
1464 | // exchange them to be compatible with wxMSW | |
1465 | gint tmp = from; | |
1466 | from = to; | |
1467 | to = tmp; | |
1468 | } | |
bb69661b | 1469 | |
2d4cc5b6 VZ |
1470 | if ( fromOut ) |
1471 | *fromOut = from; | |
1472 | if ( toOut ) | |
1473 | *toOut = to; | |
ca8b28f2 JS |
1474 | } |
1475 | ||
5b87f8bf | 1476 | |
ca8b28f2 JS |
1477 | bool wxTextCtrl::IsEditable() const |
1478 | { | |
7d8268a1 | 1479 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
05060eeb | 1480 | |
75812722 | 1481 | if ( IsMultiLine() ) |
fdd55287 VZ |
1482 | { |
1483 | return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)); | |
1484 | } | |
1485 | else | |
1486 | { | |
1487 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
1488 | } | |
ca8b28f2 JS |
1489 | } |
1490 | ||
0efe5ba7 VZ |
1491 | bool wxTextCtrl::IsModified() const |
1492 | { | |
1493 | return m_modified; | |
1494 | } | |
1495 | ||
03f38c58 | 1496 | void wxTextCtrl::Clear() |
c801d85f | 1497 | { |
902725ee | 1498 | SetValue( wxEmptyString ); |
6de97a3b | 1499 | } |
c801d85f | 1500 | |
903f689b | 1501 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 1502 | { |
223d09f6 | 1503 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
805dd538 | 1504 | |
75812722 | 1505 | if ( key_event.GetKeyCode() == WXK_RETURN ) |
da048e3d | 1506 | { |
75812722 VZ |
1507 | if ( HasFlag(wxTE_PROCESS_ENTER) ) |
1508 | { | |
1509 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1510 | event.SetEventObject(this); | |
1511 | event.SetString(GetValue()); | |
1512 | if ( GetEventHandler()->ProcessEvent(event) ) | |
1513 | return; | |
1514 | } | |
a8bf1826 | 1515 | |
75812722 VZ |
1516 | // FIXME: this is not the right place to do it, wxDialog::OnCharHook() |
1517 | // probably is | |
1518 | if ( IsSingleLine() ) | |
da048e3d | 1519 | { |
75812722 VZ |
1520 | // This will invoke the dialog default action, such |
1521 | // as the clicking the default button. | |
2b328fc9 | 1522 | |
75812722 VZ |
1523 | wxWindow *top_frame = m_parent; |
1524 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
1525 | top_frame = top_frame->GetParent(); | |
1526 | ||
1527 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
2b328fc9 | 1528 | { |
75812722 VZ |
1529 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
1530 | ||
1531 | if (window->default_widget) | |
1532 | { | |
1533 | gtk_widget_activate (window->default_widget); | |
1534 | return; | |
1535 | } | |
2b328fc9 | 1536 | } |
13111b2a | 1537 | } |
da048e3d RR |
1538 | } |
1539 | ||
2830bf19 | 1540 | key_event.Skip(); |
6de97a3b | 1541 | } |
c801d85f | 1542 | |
03f38c58 | 1543 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 1544 | { |
ae0bdb01 | 1545 | return GTK_WIDGET(m_text); |
6de97a3b | 1546 | } |
e3e65dac | 1547 | |
ef5c70f9 | 1548 | GdkWindow *wxTextCtrl::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
903f689b | 1549 | { |
75812722 | 1550 | if ( IsMultiLine() ) |
fab591c5 | 1551 | { |
ef5c70f9 VZ |
1552 | return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), |
1553 | GTK_TEXT_WINDOW_TEXT ); | |
fab591c5 | 1554 | } |
ae0bdb01 | 1555 | else |
fab591c5 | 1556 | { |
ef5c70f9 | 1557 | return GTK_ENTRY(m_text)->text_area; |
fab591c5 | 1558 | } |
903f689b | 1559 | } |
e3e65dac | 1560 | |
bb69661b VZ |
1561 | // the font will change for subsequent text insertiongs |
1562 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 1563 | { |
7d8268a1 | 1564 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
8bbe427f | 1565 | |
a66954a6 | 1566 | if ( !wxTextCtrlBase::SetFont(font) ) |
bb69661b VZ |
1567 | { |
1568 | // font didn't change, nothing to do | |
7d8268a1 | 1569 | return false; |
bb69661b VZ |
1570 | } |
1571 | ||
75812722 | 1572 | if ( IsMultiLine() ) |
bb69661b | 1573 | { |
7d8268a1 | 1574 | SetUpdateFont(true); |
bb69661b | 1575 | |
1ff4714d VZ |
1576 | m_defaultStyle.SetFont(font); |
1577 | ||
01041145 | 1578 | ChangeFontGlobally(); |
bb69661b VZ |
1579 | } |
1580 | ||
7d8268a1 | 1581 | return true; |
58614078 RR |
1582 | } |
1583 | ||
01041145 VZ |
1584 | void wxTextCtrl::ChangeFontGlobally() |
1585 | { | |
1586 | // this method is very inefficient and hence should be called as rarely as | |
1587 | // possible! | |
c04ec496 VZ |
1588 | // |
1589 | // TODO: it can be implemented much more efficiently for GTK2 | |
75812722 | 1590 | wxASSERT_MSG( IsMultiLine(), |
22800f32 | 1591 | _T("shouldn't be called for single line controls") ); |
01041145 VZ |
1592 | |
1593 | wxString value = GetValue(); | |
7d8268a1 | 1594 | if ( !value.empty() ) |
01041145 | 1595 | { |
7d8268a1 | 1596 | SetUpdateFont(false); |
572aeb77 | 1597 | |
01041145 VZ |
1598 | Clear(); |
1599 | AppendText(value); | |
01041145 VZ |
1600 | } |
1601 | } | |
1602 | ||
17665a2b VZ |
1603 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) |
1604 | { | |
1605 | if ( !wxControl::SetForegroundColour(colour) ) | |
7d8268a1 | 1606 | return false; |
17665a2b VZ |
1607 | |
1608 | // update default fg colour too | |
1609 | m_defaultStyle.SetTextColour(colour); | |
1610 | ||
7d8268a1 | 1611 | return true; |
17665a2b VZ |
1612 | } |
1613 | ||
f03fc89f | 1614 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 1615 | { |
7d8268a1 | 1616 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
a81258be | 1617 | |
1f477433 | 1618 | if ( !wxControl::SetBackgroundColour( colour ) ) |
7d8268a1 | 1619 | return false; |
3358d36e | 1620 | |
f03fc89f | 1621 | if (!m_backgroundColour.Ok()) |
7d8268a1 | 1622 | return false; |
8bbe427f | 1623 | |
17665a2b VZ |
1624 | // change active background color too |
1625 | m_defaultStyle.SetBackgroundColour( colour ); | |
1626 | ||
7d8268a1 | 1627 | return true; |
58614078 RR |
1628 | } |
1629 | ||
eda40bfc | 1630 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) |
17665a2b | 1631 | { |
75812722 | 1632 | if ( IsMultiLine() ) |
17665a2b VZ |
1633 | { |
1634 | if ( style.IsDefault() ) | |
1635 | { | |
1636 | // nothing to do | |
7d8268a1 | 1637 | return true; |
17665a2b | 1638 | } |
902725ee | 1639 | |
41b81aed | 1640 | gint l = gtk_text_buffer_get_char_count( m_buffer ); |
17665a2b | 1641 | |
7d8268a1 | 1642 | wxCHECK_MSG( start >= 0 && end <= l, false, |
cc3da3f8 RR |
1643 | _T("invalid range in wxTextCtrl::SetStyle") ); |
1644 | ||
1645 | GtkTextIter starti, endi; | |
41b81aed RR |
1646 | gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start ); |
1647 | gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end ); | |
cc3da3f8 RR |
1648 | |
1649 | // use the attributes from style which are set in it and fall back | |
1650 | // first to the default style and then to the text control default | |
1651 | // colours for the others | |
1652 | wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this); | |
1653 | ||
41b81aed | 1654 | wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi ); |
902725ee | 1655 | |
7d8268a1 | 1656 | return true; |
17665a2b | 1657 | } |
902725ee WS |
1658 | |
1659 | // else single line | |
1660 | // cannot do this for GTK+'s Entry widget | |
1661 | return false; | |
17665a2b VZ |
1662 | } |
1663 | ||
f40fdaa3 | 1664 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) |
58614078 | 1665 | { |
f40fdaa3 | 1666 | gtk_widget_modify_style(m_text, style); |
68dda785 | 1667 | } |
f96aa4d9 | 1668 | |
e702ff0f JS |
1669 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
1670 | { | |
1671 | Cut(); | |
1672 | } | |
1673 | ||
1674 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1675 | { | |
1676 | Copy(); | |
1677 | } | |
1678 | ||
1679 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1680 | { | |
1681 | Paste(); | |
1682 | } | |
1683 | ||
1684 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1685 | { | |
1686 | Undo(); | |
1687 | } | |
1688 | ||
1689 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1690 | { | |
1691 | Redo(); | |
1692 | } | |
1693 | ||
1694 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1695 | { | |
1696 | event.Enable( CanCut() ); | |
1697 | } | |
1698 | ||
1699 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1700 | { | |
1701 | event.Enable( CanCopy() ); | |
1702 | } | |
1703 | ||
1704 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1705 | { | |
1706 | event.Enable( CanPaste() ); | |
1707 | } | |
1708 | ||
1709 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1710 | { | |
1711 | event.Enable( CanUndo() ); | |
1712 | } | |
1713 | ||
1714 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1715 | { | |
1716 | event.Enable( CanRedo() ); | |
1717 | } | |
65045edd | 1718 | |
f68586e5 VZ |
1719 | wxSize wxTextCtrl::DoGetBestSize() const |
1720 | { | |
1721 | // FIXME should be different for multi-line controls... | |
0279e844 | 1722 | wxSize ret( wxControl::DoGetBestSize() ); |
9f884528 RD |
1723 | wxSize best(80, ret.y); |
1724 | CacheBestSize(best); | |
1725 | return best; | |
f68586e5 | 1726 | } |
0cc7251e | 1727 | |
9cd6d737 VZ |
1728 | // ---------------------------------------------------------------------------- |
1729 | // freeze/thaw | |
1730 | // ---------------------------------------------------------------------------- | |
1731 | ||
0cc7251e VZ |
1732 | void wxTextCtrl::Freeze() |
1733 | { | |
1734 | if ( HasFlag(wxTE_MULTILINE) ) | |
1735 | { | |
41b81aed RR |
1736 | if ( !m_frozenness++ ) |
1737 | { | |
1738 | // freeze textview updates and remove buffer | |
9fa72bd2 MR |
1739 | g_signal_connect (m_text, "expose_event", |
1740 | G_CALLBACK (gtk_text_exposed_callback), this); | |
1741 | g_signal_connect (m_widget, "expose_event", | |
1742 | G_CALLBACK (gtk_text_exposed_callback), this); | |
41b81aed RR |
1743 | gtk_widget_set_sensitive(m_widget, false); |
1744 | g_object_ref(m_buffer); | |
1745 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL)); | |
0a164d4c | 1746 | } |
41b81aed | 1747 | } |
0cc7251e VZ |
1748 | } |
1749 | ||
1750 | void wxTextCtrl::Thaw() | |
1751 | { | |
1752 | if ( HasFlag(wxTE_MULTILINE) ) | |
1753 | { | |
41b81aed RR |
1754 | wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") ); |
1755 | ||
1756 | if ( !--m_frozenness ) | |
1757 | { | |
1758 | // Reattach buffer and thaw textview updates | |
1759 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer); | |
1760 | g_object_unref(m_buffer); | |
1761 | gtk_widget_set_sensitive(m_widget, true); | |
9fa72bd2 MR |
1762 | g_signal_handlers_disconnect_by_func (m_widget, |
1763 | (gpointer) gtk_text_exposed_callback, this); | |
1764 | g_signal_handlers_disconnect_by_func (m_text, | |
1765 | (gpointer) gtk_text_exposed_callback, this); | |
41b81aed | 1766 | } |
41b81aed | 1767 | } |
0cc7251e | 1768 | } |
9cd6d737 | 1769 | |
9440c3d0 KH |
1770 | // ---------------------------------------------------------------------------- |
1771 | // wxTextUrlEvent passing if style & wxTE_AUTO_URL | |
1772 | // ---------------------------------------------------------------------------- | |
1773 | ||
9440c3d0 KH |
1774 | // FIXME: when dragging on a link the sample gets an "Unknown event". |
1775 | // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or | |
1776 | // a buggy sample, or something else | |
1777 | void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event) | |
1778 | { | |
1779 | event.Skip(); | |
75812722 | 1780 | if( !HasFlag(wxTE_AUTO_URL) ) |
9440c3d0 KH |
1781 | return; |
1782 | ||
1783 | gint x, y; | |
1784 | GtkTextIter start, end; | |
1785 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer), | |
1786 | "wxUrl"); | |
1787 | ||
1788 | gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET, | |
1789 | event.GetX(), event.GetY(), &x, &y); | |
1790 | ||
1791 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y); | |
1792 | if (!gtk_text_iter_has_tag(&end, tag)) | |
1793 | { | |
1794 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1795 | GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor); | |
1796 | return; | |
1797 | } | |
1798 | ||
1799 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1800 | GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor); | |
1801 | ||
1802 | start = end; | |
1803 | if(!gtk_text_iter_begins_tag(&start, tag)) | |
1804 | gtk_text_iter_backward_to_tag_toggle(&start, tag); | |
1805 | if(!gtk_text_iter_ends_tag(&end, tag)) | |
1806 | gtk_text_iter_forward_to_tag_toggle(&end, tag); | |
1807 | ||
1808 | // Native context menu is probably not desired on an URL. | |
1809 | // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value | |
1810 | if(event.GetEventType() == wxEVT_RIGHT_DOWN) | |
1811 | event.Skip(false); | |
1812 | ||
1813 | wxTextUrlEvent url_event(m_windowId, event, | |
1814 | gtk_text_iter_get_offset(&start), | |
1815 | gtk_text_iter_get_offset(&end)); | |
1816 | ||
1817 | InitCommandEvent(url_event); | |
1818 | // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag) | |
1819 | //event.Skip(!GetEventHandler()->ProcessEvent(url_event)); | |
1820 | GetEventHandler()->ProcessEvent(url_event); | |
1821 | } | |
9440c3d0 | 1822 | |
8312c461 VZ |
1823 | bool wxTextCtrl::GTKProcessEvent(wxEvent& event) const |
1824 | { | |
1825 | bool rc = wxTextCtrlBase::GTKProcessEvent(event); | |
1826 | ||
1827 | // GtkTextView starts a drag operation when left mouse button is pressed | |
1828 | // and ends it when it is released and if it doesn't get the release event | |
1829 | // the next click on a control results in an assertion failure inside | |
1830 | // gtk_text_view_start_selection_drag() which simply *kills* the program | |
1831 | // without anything we can do about it, so always let GTK+ have this event | |
1832 | return rc && (IsSingleLine() || event.GetEventType() != wxEVT_LEFT_UP); | |
1833 | } | |
1834 | ||
9d522606 RD |
1835 | // static |
1836 | wxVisualAttributes | |
1837 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1838 | { | |
1839 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
1840 | } |