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