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