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