]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_TEXTCTRL | |
14 | ||
15 | #include "wx/textctrl.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
19 | #include "wx/log.h" | |
20 | #include "wx/utils.h" | |
21 | #include "wx/settings.h" | |
22 | #include "wx/math.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/strconv.h" | |
26 | #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo()) | |
27 | ||
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <ctype.h> | |
31 | ||
32 | #include "wx/gtk/private.h" | |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
35 | // helpers | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | extern "C" { | |
39 | static void wxGtkOnRemoveTag(GtkTextBuffer *buffer, | |
40 | GtkTextTag *tag, | |
41 | GtkTextIter *start, | |
42 | GtkTextIter *end, | |
43 | char *prefix) | |
44 | { | |
45 | gchar *name; | |
46 | g_object_get (tag, "name", &name, NULL); | |
47 | ||
48 | if (!name || strncmp(name, prefix, strlen(prefix))) | |
49 | // anonymous tag or not starting with prefix - don't remove | |
50 | g_signal_stop_emission_by_name (buffer, "remove_tag"); | |
51 | ||
52 | g_free(name); | |
53 | } | |
54 | } | |
55 | ||
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 | ||
74 | static void wxGtkTextApplyTagsFromAttr(GtkWidget *text, | |
75 | GtkTextBuffer *text_buffer, | |
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 | { | |
85 | wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXFONT", start, end); | |
86 | ||
87 | PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description; | |
88 | wxGtkString font_string(pango_font_description_to_string(font_description)); | |
89 | g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string.c_str()); | |
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); | |
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 | } | |
110 | } | |
111 | ||
112 | if (attr.HasTextColour()) | |
113 | { | |
114 | wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXFORECOLOR", start, end); | |
115 | ||
116 | const GdkColor *colFg = attr.GetTextColour().GetColor(); | |
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 | { | |
129 | wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXBACKCOLOR", start, end); | |
130 | ||
131 | const GdkColor *colBg = attr.GetBackgroundColour().GetColor(); | |
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 | } | |
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 | ||
150 | wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXALIGNMENT", start, end); | |
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; | |
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) | |
167 | case wxTEXT_ALIGNMENT_JUSTIFIED: | |
168 | if (!gtk_check_version(2,11,0)) | |
169 | align = GTK_JUSTIFY_FILL; | |
170 | else | |
171 | align = GTK_JUSTIFY_LEFT; | |
172 | break; | |
173 | #endif | |
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 | } | |
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 | ||
196 | wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXINDENT", start, end); | |
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 | ||
245 | wxGtkTextRemoveTagsWithPrefix(text_buffer, "WXTABS", start, end); | |
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 | ||
254 | const wxWX2MBbuf buftag = tagname.utf8_str(); | |
255 | ||
256 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
257 | buftag ); | |
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)); | |
273 | tag = gtk_text_buffer_create_tag( text_buffer, buftag, | |
274 | "tabs", tabArray, NULL ); | |
275 | pango_tab_array_free(tabArray); | |
276 | } | |
277 | gtk_text_buffer_apply_tag (text_buffer, tag, ¶_start, ¶_end); | |
278 | } | |
279 | } | |
280 | ||
281 | static void wxGtkTextInsert(GtkWidget *text, | |
282 | GtkTextBuffer *text_buffer, | |
283 | const wxTextAttr& attr, | |
284 | const wxCharBuffer& buffer) | |
285 | ||
286 | { | |
287 | gint start_offset; | |
288 | GtkTextIter iter, start; | |
289 | ||
290 | gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, | |
291 | gtk_text_buffer_get_insert (text_buffer) ); | |
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); | |
296 | ||
297 | wxGtkTextApplyTagsFromAttr(text, text_buffer, attr, &start, &iter); | |
298 | } | |
299 | ||
300 | // ---------------------------------------------------------------------------- | |
301 | // "insert_text" for GtkEntry | |
302 | // ---------------------------------------------------------------------------- | |
303 | ||
304 | extern "C" { | |
305 | static void | |
306 | gtk_insert_text_callback(GtkEditable *editable, | |
307 | const gchar *new_text, | |
308 | gint new_text_length, | |
309 | gint *position, | |
310 | wxTextCtrl *win) | |
311 | { | |
312 | // we should only be called if we have a max len limit at all | |
313 | GtkEntry *entry = GTK_ENTRY (editable); | |
314 | ||
315 | wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") ); | |
316 | ||
317 | // check that we don't overflow the max length limit | |
318 | // | |
319 | // FIXME: this doesn't work when we paste a string which is going to be | |
320 | // truncated | |
321 | if ( entry->text_length == entry->text_max_length ) | |
322 | { | |
323 | // we don't need to run the base class version at all | |
324 | g_signal_stop_emission_by_name (editable, "insert_text"); | |
325 | ||
326 | // remember that the next changed signal is to be ignored to avoid | |
327 | // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event | |
328 | win->IgnoreNextTextUpdate(); | |
329 | ||
330 | // and generate the correct one ourselves | |
331 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId()); | |
332 | event.SetEventObject(win); | |
333 | event.SetString(win->GetValue()); | |
334 | win->GetEventHandler()->ProcessEvent( event ); | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp, | |
340 | ||
341 | extern "C" { | |
342 | static void | |
343 | au_apply_tag_callback(GtkTextBuffer *buffer, | |
344 | GtkTextTag *tag, | |
345 | GtkTextIter *start, | |
346 | GtkTextIter *end, | |
347 | gpointer textctrl) | |
348 | { | |
349 | if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl")) | |
350 | g_signal_stop_emission_by_name (buffer, "apply_tag"); | |
351 | } | |
352 | } | |
353 | ||
354 | //----------------------------------------------------------------------------- | |
355 | // GtkTextCharPredicates for gtk_text_iter_*_find_char | |
356 | //----------------------------------------------------------------------------- | |
357 | ||
358 | extern "C" { | |
359 | static gboolean | |
360 | pred_whitespace (gunichar ch, gpointer user_data) | |
361 | { | |
362 | return g_unichar_isspace(ch); | |
363 | } | |
364 | } | |
365 | ||
366 | extern "C" { | |
367 | static gboolean | |
368 | pred_non_whitespace (gunichar ch, gpointer user_data) | |
369 | { | |
370 | return !g_unichar_isspace(ch); | |
371 | } | |
372 | } | |
373 | ||
374 | extern "C" { | |
375 | static gboolean | |
376 | pred_nonpunct (gunichar ch, gpointer user_data) | |
377 | { | |
378 | return !g_unichar_ispunct(ch); | |
379 | } | |
380 | } | |
381 | ||
382 | extern "C" { | |
383 | static gboolean | |
384 | pred_nonpunct_or_slash (gunichar ch, gpointer user_data) | |
385 | { | |
386 | return !g_unichar_ispunct(ch) || ch == '/'; | |
387 | } | |
388 | } | |
389 | ||
390 | //----------------------------------------------------------------------------- | |
391 | // Check for links between s and e and correct tags as necessary | |
392 | //----------------------------------------------------------------------------- | |
393 | ||
394 | // This function should be made match better while being efficient at one point. | |
395 | // Most probably with a row of regular expressions. | |
396 | extern "C" { | |
397 | static void | |
398 | au_check_word( GtkTextIter *s, GtkTextIter *e ) | |
399 | { | |
400 | static const char *URIPrefixes[] = | |
401 | { | |
402 | "http://", | |
403 | "ftp://", | |
404 | "www.", | |
405 | "ftp.", | |
406 | "mailto://", | |
407 | "https://", | |
408 | "file://", | |
409 | "nntp://", | |
410 | "news://", | |
411 | "telnet://", | |
412 | "mms://", | |
413 | "gopher://", | |
414 | "prospero://", | |
415 | "wais://", | |
416 | }; | |
417 | ||
418 | GtkTextIter start = *s, end = *e; | |
419 | GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s); | |
420 | ||
421 | // Get our special link tag | |
422 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"); | |
423 | ||
424 | // Get rid of punctuation from beginning and end. | |
425 | // Might want to move this to au_check_range if an improved link checking doesn't | |
426 | // use some intelligent punctuation checking itself (beware of undesired iter modifications). | |
427 | if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) ) | |
428 | gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e ); | |
429 | ||
430 | gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start ); | |
431 | gtk_text_iter_forward_char(&end); | |
432 | ||
433 | wxGtkString text(gtk_text_iter_get_text( &start, &end )); | |
434 | size_t len = strlen(text), prefix_len; | |
435 | size_t n; | |
436 | ||
437 | for( n = 0; n < WXSIZEOF(URIPrefixes); ++n ) | |
438 | { | |
439 | prefix_len = strlen(URIPrefixes[n]); | |
440 | if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len)) | |
441 | break; | |
442 | } | |
443 | ||
444 | if(n < WXSIZEOF(URIPrefixes)) | |
445 | { | |
446 | gulong signal_id = g_signal_handler_find (buffer, | |
447 | (GSignalMatchType) (G_SIGNAL_MATCH_FUNC), | |
448 | 0, 0, NULL, | |
449 | (gpointer)au_apply_tag_callback, NULL); | |
450 | ||
451 | g_signal_handler_block (buffer, signal_id); | |
452 | gtk_text_buffer_apply_tag(buffer, tag, &start, &end); | |
453 | g_signal_handler_unblock (buffer, signal_id); | |
454 | } | |
455 | } | |
456 | } | |
457 | ||
458 | extern "C" { | |
459 | static void | |
460 | au_check_range(GtkTextIter *s, | |
461 | GtkTextIter *range_end) | |
462 | { | |
463 | GtkTextIter range_start = *s; | |
464 | GtkTextIter word_end; | |
465 | GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s); | |
466 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"); | |
467 | ||
468 | gtk_text_buffer_remove_tag(buffer, tag, s, range_end); | |
469 | ||
470 | if(g_unichar_isspace(gtk_text_iter_get_char(&range_start))) | |
471 | gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end); | |
472 | ||
473 | while(!gtk_text_iter_equal(&range_start, range_end)) | |
474 | { | |
475 | word_end = range_start; | |
476 | gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end); | |
477 | ||
478 | // Now we should have a word delimited by range_start and word_end, correct link tags | |
479 | au_check_word(&range_start, &word_end); | |
480 | ||
481 | range_start = word_end; | |
482 | gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end); | |
483 | } | |
484 | } | |
485 | } | |
486 | ||
487 | //----------------------------------------------------------------------------- | |
488 | // "insert-text" for GtkTextBuffer | |
489 | //----------------------------------------------------------------------------- | |
490 | ||
491 | extern "C" { | |
492 | static void | |
493 | au_insert_text_callback(GtkTextBuffer *buffer, | |
494 | GtkTextIter *end, | |
495 | gchar *text, | |
496 | gint len, | |
497 | wxTextCtrl *win) | |
498 | { | |
499 | if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) | |
500 | return; | |
501 | ||
502 | GtkTextIter start = *end; | |
503 | gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len)); | |
504 | ||
505 | GtkTextIter line_start = start; | |
506 | GtkTextIter line_end = *end; | |
507 | GtkTextIter words_start = start; | |
508 | GtkTextIter words_end = *end; | |
509 | ||
510 | gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start)); | |
511 | gtk_text_iter_forward_to_line_end(&line_end); | |
512 | gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start); | |
513 | gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end); | |
514 | ||
515 | au_check_range(&words_start, &words_end); | |
516 | } | |
517 | } | |
518 | ||
519 | //----------------------------------------------------------------------------- | |
520 | // "delete-range" for GtkTextBuffer | |
521 | //----------------------------------------------------------------------------- | |
522 | ||
523 | extern "C" { | |
524 | static void | |
525 | au_delete_range_callback(GtkTextBuffer *buffer, | |
526 | GtkTextIter *start, | |
527 | GtkTextIter *end, | |
528 | wxTextCtrl *win) | |
529 | { | |
530 | if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) | |
531 | return; | |
532 | ||
533 | GtkTextIter line_start = *start, line_end = *end; | |
534 | ||
535 | gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start)); | |
536 | gtk_text_iter_forward_to_line_end(&line_end); | |
537 | gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start); | |
538 | gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end); | |
539 | ||
540 | au_check_range(start, end); | |
541 | } | |
542 | } | |
543 | ||
544 | ||
545 | //----------------------------------------------------------------------------- | |
546 | // "changed" | |
547 | //----------------------------------------------------------------------------- | |
548 | ||
549 | extern "C" { | |
550 | static void | |
551 | gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win ) | |
552 | { | |
553 | if ( win->IgnoreTextUpdate() ) | |
554 | return; | |
555 | ||
556 | if (!win->m_hasVMT) return; | |
557 | ||
558 | if ( win->MarkDirtyOnChange() ) | |
559 | win->MarkDirty(); | |
560 | ||
561 | win->SendTextUpdatedEvent(); | |
562 | } | |
563 | } | |
564 | ||
565 | //----------------------------------------------------------------------------- | |
566 | // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard" | |
567 | //----------------------------------------------------------------------------- | |
568 | ||
569 | // common part of the event handlers below | |
570 | static void | |
571 | handle_text_clipboard_callback( GtkWidget *widget, wxTextCtrl *win, | |
572 | wxEventType eventType, const gchar * signal_name) | |
573 | { | |
574 | wxClipboardTextEvent event( eventType, win->GetId() ); | |
575 | event.SetEventObject( win ); | |
576 | if ( win->GetEventHandler()->ProcessEvent( event ) ) | |
577 | { | |
578 | // don't let the default processing to take place if we did something | |
579 | // ourselves in the event handler | |
580 | g_signal_stop_emission_by_name (widget, signal_name); | |
581 | } | |
582 | } | |
583 | ||
584 | extern "C" { | |
585 | static void | |
586 | gtk_copy_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) | |
587 | { | |
588 | handle_text_clipboard_callback( | |
589 | widget, win, wxEVT_COMMAND_TEXT_COPY, "copy-clipboard" ); | |
590 | } | |
591 | ||
592 | static void | |
593 | gtk_cut_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) | |
594 | { | |
595 | handle_text_clipboard_callback( | |
596 | widget, win, wxEVT_COMMAND_TEXT_CUT, "cut-clipboard" ); | |
597 | } | |
598 | ||
599 | static void | |
600 | gtk_paste_clipboard_callback( GtkWidget *widget, wxTextCtrl *win ) | |
601 | { | |
602 | handle_text_clipboard_callback( | |
603 | widget, win, wxEVT_COMMAND_TEXT_PASTE, "paste-clipboard" ); | |
604 | } | |
605 | } | |
606 | ||
607 | //----------------------------------------------------------------------------- | |
608 | // "expose_event" from scrolled window and textview | |
609 | //----------------------------------------------------------------------------- | |
610 | ||
611 | extern "C" { | |
612 | static gboolean | |
613 | gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win ) | |
614 | { | |
615 | return TRUE; | |
616 | } | |
617 | } | |
618 | ||
619 | ||
620 | //----------------------------------------------------------------------------- | |
621 | // wxTextCtrl | |
622 | //----------------------------------------------------------------------------- | |
623 | ||
624 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) | |
625 | ||
626 | BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) | |
627 | EVT_CHAR(wxTextCtrl::OnChar) | |
628 | ||
629 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
630 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
631 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
632 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
633 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
634 | ||
635 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
636 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
637 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
638 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
639 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
640 | ||
641 | // wxTE_AUTO_URL wxTextUrl support. Currently only creates | |
642 | // wxTextUrlEvent in the same cases as wxMSW, more can be added here. | |
643 | EVT_MOTION (wxTextCtrl::OnUrlMouseEvent) | |
644 | EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
645 | EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent) | |
646 | EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent) | |
647 | EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
648 | EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent) | |
649 | EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent) | |
650 | END_EVENT_TABLE() | |
651 | ||
652 | void wxTextCtrl::Init() | |
653 | { | |
654 | m_dontMarkDirty = | |
655 | m_modified = false; | |
656 | ||
657 | m_countUpdatesToIgnore = 0; | |
658 | ||
659 | SetUpdateFont(false); | |
660 | ||
661 | m_text = NULL; | |
662 | m_freezeCount = 0; | |
663 | m_showPositionOnThaw = NULL; | |
664 | m_gdkHandCursor = NULL; | |
665 | m_gdkXTermCursor = NULL; | |
666 | } | |
667 | ||
668 | wxTextCtrl::~wxTextCtrl() | |
669 | { | |
670 | if(m_gdkHandCursor) | |
671 | gdk_cursor_unref(m_gdkHandCursor); | |
672 | if(m_gdkXTermCursor) | |
673 | gdk_cursor_unref(m_gdkXTermCursor); | |
674 | } | |
675 | ||
676 | wxTextCtrl::wxTextCtrl( wxWindow *parent, | |
677 | wxWindowID id, | |
678 | const wxString &value, | |
679 | const wxPoint &pos, | |
680 | const wxSize &size, | |
681 | long style, | |
682 | const wxValidator& validator, | |
683 | const wxString &name ) | |
684 | { | |
685 | Init(); | |
686 | ||
687 | Create( parent, id, value, pos, size, style, validator, name ); | |
688 | } | |
689 | ||
690 | bool wxTextCtrl::Create( wxWindow *parent, | |
691 | wxWindowID id, | |
692 | const wxString &value, | |
693 | const wxPoint &pos, | |
694 | const wxSize &size, | |
695 | long style, | |
696 | const wxValidator& validator, | |
697 | const wxString &name ) | |
698 | { | |
699 | if (!PreCreation( parent, pos, size ) || | |
700 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
701 | { | |
702 | wxFAIL_MSG( wxT("wxTextCtrl creation failed") ); | |
703 | return false; | |
704 | } | |
705 | ||
706 | bool multi_line = (style & wxTE_MULTILINE) != 0; | |
707 | ||
708 | if (multi_line) | |
709 | { | |
710 | // Create view | |
711 | m_text = gtk_text_view_new(); | |
712 | ||
713 | m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
714 | ||
715 | // create "ShowPosition" marker | |
716 | GtkTextIter iter; | |
717 | gtk_text_buffer_get_start_iter(m_buffer, &iter); | |
718 | gtk_text_buffer_create_mark(m_buffer, "ShowPosition", &iter, true); | |
719 | ||
720 | // create scrolled window | |
721 | m_widget = gtk_scrolled_window_new( NULL, NULL ); | |
722 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ), | |
723 | GTK_POLICY_AUTOMATIC, | |
724 | style & wxTE_NO_VSCROLL | |
725 | ? GTK_POLICY_NEVER | |
726 | : GTK_POLICY_AUTOMATIC ); | |
727 | // for ScrollLines/Pages | |
728 | m_scrollBar[1] = (GtkRange*)((GtkScrolledWindow*)m_widget)->vscrollbar; | |
729 | ||
730 | // Insert view into scrolled window | |
731 | gtk_container_add( GTK_CONTAINER(m_widget), m_text ); | |
732 | ||
733 | GTKSetWrapMode(); | |
734 | ||
735 | GtkScrolledWindowSetBorder(m_widget, style); | |
736 | ||
737 | gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK ); | |
738 | ||
739 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
740 | } | |
741 | else | |
742 | { | |
743 | // a single-line text control: no need for scrollbars | |
744 | m_widget = | |
745 | m_text = gtk_entry_new(); | |
746 | ||
747 | if (style & wxNO_BORDER) | |
748 | g_object_set (m_text, "has-frame", FALSE, NULL); | |
749 | } | |
750 | ||
751 | m_parent->DoAddChild( this ); | |
752 | ||
753 | m_focusWidget = m_text; | |
754 | ||
755 | PostCreation(size); | |
756 | ||
757 | if (multi_line) | |
758 | { | |
759 | gtk_widget_show(m_text); | |
760 | } | |
761 | ||
762 | // We want to be notified about text changes. | |
763 | if (multi_line) | |
764 | { | |
765 | g_signal_connect (m_buffer, "changed", | |
766 | G_CALLBACK (gtk_text_changed_callback), this); | |
767 | } | |
768 | else | |
769 | { | |
770 | g_signal_connect (m_text, "changed", | |
771 | G_CALLBACK (gtk_text_changed_callback), this); | |
772 | } | |
773 | ||
774 | if (!value.empty()) | |
775 | { | |
776 | SetValue( value ); | |
777 | } | |
778 | ||
779 | if (style & wxTE_PASSWORD) | |
780 | GTKSetVisibility(); | |
781 | ||
782 | if (style & wxTE_READONLY) | |
783 | GTKSetEditable(); | |
784 | ||
785 | // left justification (alignment) is the default anyhow | |
786 | if ( style & (wxTE_RIGHT | wxTE_CENTRE) ) | |
787 | GTKSetJustification(); | |
788 | ||
789 | if (multi_line) | |
790 | { | |
791 | // Handle URLs on multi-line controls with wxTE_AUTO_URL style | |
792 | if (style & wxTE_AUTO_URL) | |
793 | { | |
794 | GtkTextIter start, end; | |
795 | m_gdkHandCursor = gdk_cursor_new(GDK_HAND2); | |
796 | m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM); | |
797 | ||
798 | // We create our wxUrl tag here for slight efficiency gain - we | |
799 | // don't have to check for the tag existance in callbacks, | |
800 | // hereby it's guaranteed to exist. | |
801 | gtk_text_buffer_create_tag(m_buffer, "wxUrl", | |
802 | "foreground", "blue", | |
803 | "underline", PANGO_UNDERLINE_SINGLE, | |
804 | NULL); | |
805 | ||
806 | // Check for URLs after each text change | |
807 | g_signal_connect_after (m_buffer, "insert_text", | |
808 | G_CALLBACK (au_insert_text_callback), this); | |
809 | g_signal_connect_after (m_buffer, "delete_range", | |
810 | G_CALLBACK (au_delete_range_callback), this); | |
811 | ||
812 | // Block all wxUrl tag applying unless we do it ourselves, in which case we | |
813 | // block this callback temporarily. This takes care of gtk+ internal | |
814 | // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise, | |
815 | // which is undesired because only a part of the URL might be copied. | |
816 | // The insert-text signal emitted inside it will take care of newly formed | |
817 | // or wholly copied URLs. | |
818 | g_signal_connect (m_buffer, "apply_tag", | |
819 | G_CALLBACK (au_apply_tag_callback), NULL); | |
820 | ||
821 | // Check for URLs in the initial string passed to Create | |
822 | gtk_text_buffer_get_start_iter(m_buffer, &start); | |
823 | gtk_text_buffer_get_end_iter(m_buffer, &end); | |
824 | au_check_range(&start, &end); | |
825 | } | |
826 | } | |
827 | ||
828 | g_signal_connect (m_text, "copy-clipboard", | |
829 | G_CALLBACK (gtk_copy_clipboard_callback), this); | |
830 | g_signal_connect (m_text, "cut-clipboard", | |
831 | G_CALLBACK (gtk_cut_clipboard_callback), this); | |
832 | g_signal_connect (m_text, "paste-clipboard", | |
833 | G_CALLBACK (gtk_paste_clipboard_callback), this); | |
834 | ||
835 | m_cursor = wxCursor( wxCURSOR_IBEAM ); | |
836 | ||
837 | return true; | |
838 | } | |
839 | ||
840 | // ---------------------------------------------------------------------------- | |
841 | // flags handling | |
842 | // ---------------------------------------------------------------------------- | |
843 | ||
844 | void wxTextCtrl::GTKSetEditable() | |
845 | { | |
846 | gboolean editable = !HasFlag(wxTE_READONLY); | |
847 | if ( IsSingleLine() ) | |
848 | gtk_editable_set_editable(GTK_EDITABLE(m_text), editable); | |
849 | else | |
850 | gtk_text_view_set_editable(GTK_TEXT_VIEW(m_text), editable); | |
851 | } | |
852 | ||
853 | void wxTextCtrl::GTKSetVisibility() | |
854 | { | |
855 | // VZ: shouldn't we assert if wxTE_PASSWORD is set for multiline control? | |
856 | if ( IsSingleLine() ) | |
857 | gtk_entry_set_visibility(GTK_ENTRY(m_text), !HasFlag(wxTE_PASSWORD)); | |
858 | } | |
859 | ||
860 | void wxTextCtrl::GTKSetWrapMode() | |
861 | { | |
862 | // no wrapping in single line controls | |
863 | if ( !IsMultiLine() ) | |
864 | return; | |
865 | ||
866 | // translate wx wrapping style to GTK+ | |
867 | GtkWrapMode wrap; | |
868 | if ( HasFlag( wxTE_DONTWRAP ) ) | |
869 | wrap = GTK_WRAP_NONE; | |
870 | else if ( HasFlag( wxTE_CHARWRAP ) ) | |
871 | wrap = GTK_WRAP_CHAR; | |
872 | else if ( HasFlag( wxTE_WORDWRAP ) ) | |
873 | wrap = GTK_WRAP_WORD; | |
874 | else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 | |
875 | { | |
876 | // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 | |
877 | #ifdef __WXGTK24__ | |
878 | if ( !gtk_check_version(2,4,0) ) | |
879 | { | |
880 | wrap = GTK_WRAP_WORD_CHAR; | |
881 | } | |
882 | else | |
883 | #endif // __WXGTK24__ | |
884 | wrap = GTK_WRAP_WORD; | |
885 | } | |
886 | ||
887 | gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); | |
888 | } | |
889 | ||
890 | void wxTextCtrl::GTKSetJustification() | |
891 | { | |
892 | if ( IsMultiLine() ) | |
893 | { | |
894 | GtkJustification just; | |
895 | if ( HasFlag(wxTE_RIGHT) ) | |
896 | just = GTK_JUSTIFY_RIGHT; | |
897 | else if ( HasFlag(wxTE_CENTRE) ) | |
898 | just = GTK_JUSTIFY_CENTER; | |
899 | else // wxTE_LEFT == 0 | |
900 | just = GTK_JUSTIFY_LEFT; | |
901 | ||
902 | gtk_text_view_set_justification(GTK_TEXT_VIEW(m_text), just); | |
903 | } | |
904 | else // single line | |
905 | { | |
906 | #ifdef __WXGTK24__ | |
907 | // gtk_entry_set_alignment was introduced in gtk+-2.3.5 | |
908 | if (!gtk_check_version(2,4,0)) | |
909 | { | |
910 | gfloat align; | |
911 | if ( HasFlag(wxTE_RIGHT) ) | |
912 | align = 1.0; | |
913 | else if ( HasFlag(wxTE_CENTRE) ) | |
914 | align = 0.5; | |
915 | else // single line | |
916 | align = 0.0; | |
917 | ||
918 | gtk_entry_set_alignment(GTK_ENTRY(m_text), align); | |
919 | } | |
920 | #endif // __WXGTK24__ | |
921 | } | |
922 | ||
923 | } | |
924 | ||
925 | void wxTextCtrl::SetWindowStyleFlag(long style) | |
926 | { | |
927 | long styleOld = GetWindowStyleFlag(); | |
928 | ||
929 | wxTextCtrlBase::SetWindowStyleFlag(style); | |
930 | ||
931 | if ( (style & wxTE_READONLY) != (styleOld & wxTE_READONLY) ) | |
932 | GTKSetEditable(); | |
933 | ||
934 | if ( (style & wxTE_PASSWORD) != (styleOld & wxTE_PASSWORD) ) | |
935 | GTKSetVisibility(); | |
936 | ||
937 | static const long flagsWrap = wxTE_WORDWRAP | wxTE_CHARWRAP | wxTE_DONTWRAP; | |
938 | if ( (style & flagsWrap) != (styleOld & flagsWrap) ) | |
939 | GTKSetWrapMode(); | |
940 | ||
941 | static const long flagsAlign = wxTE_LEFT | wxTE_CENTRE | wxTE_RIGHT; | |
942 | if ( (style & flagsAlign) != (styleOld & flagsAlign) ) | |
943 | GTKSetJustification(); | |
944 | } | |
945 | ||
946 | // ---------------------------------------------------------------------------- | |
947 | // control value | |
948 | // ---------------------------------------------------------------------------- | |
949 | ||
950 | wxString wxTextCtrl::GetValue() const | |
951 | { | |
952 | wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") ); | |
953 | ||
954 | if ( IsMultiLine() ) | |
955 | { | |
956 | GtkTextIter start; | |
957 | gtk_text_buffer_get_start_iter( m_buffer, &start ); | |
958 | GtkTextIter end; | |
959 | gtk_text_buffer_get_end_iter( m_buffer, &end ); | |
960 | wxGtkString text(gtk_text_buffer_get_text(m_buffer, &start, &end, true)); | |
961 | ||
962 | return wxGTK_CONV_BACK(text); | |
963 | } | |
964 | else | |
965 | { | |
966 | const gchar *text = gtk_entry_get_text( GTK_ENTRY(m_text) ); | |
967 | return wxGTK_CONV_BACK(text); | |
968 | } | |
969 | } | |
970 | ||
971 | wxFontEncoding wxTextCtrl::GetTextEncoding() const | |
972 | { | |
973 | // GTK+ uses UTF-8 internally, we need to convert to it but from which | |
974 | // encoding? | |
975 | ||
976 | // first check the default text style (we intentionally don't check the | |
977 | // style for the current position as it doesn't make sense for SetValue()) | |
978 | const wxTextAttr& style = GetDefaultStyle(); | |
979 | wxFontEncoding enc = style.HasFont() ? style.GetFont().GetEncoding() | |
980 | : wxFONTENCODING_SYSTEM; | |
981 | ||
982 | // fall back to the controls font if no style | |
983 | if ( enc == wxFONTENCODING_SYSTEM && m_hasFont ) | |
984 | enc = GetFont().GetEncoding(); | |
985 | ||
986 | return enc; | |
987 | } | |
988 | ||
989 | bool wxTextCtrl::IsEmpty() const | |
990 | { | |
991 | if ( IsMultiLine() ) | |
992 | return gtk_text_buffer_get_char_count(m_buffer) == 0; | |
993 | ||
994 | return wxTextCtrlBase::IsEmpty(); | |
995 | } | |
996 | ||
997 | void wxTextCtrl::DoSetValue( const wxString &value, int flags ) | |
998 | { | |
999 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1000 | ||
1001 | m_modified = false; | |
1002 | ||
1003 | wxFontEncoding enc = m_defaultStyle.HasFont() | |
1004 | ? m_defaultStyle.GetFont().GetEncoding() | |
1005 | : wxFONTENCODING_SYSTEM; | |
1006 | if ( enc == wxFONTENCODING_SYSTEM ) | |
1007 | enc = GetTextEncoding(); | |
1008 | ||
1009 | const wxCharBuffer buffer(wxGTK_CONV_ENC(value, enc)); | |
1010 | if ( !buffer ) | |
1011 | { | |
1012 | // see comment in WriteText() as to why we must warn the user about | |
1013 | // this | |
1014 | wxLogWarning(_("Failed to set text in the text control.")); | |
1015 | return; | |
1016 | } | |
1017 | ||
1018 | if ( !(flags & SetValue_SendEvent) ) | |
1019 | { | |
1020 | g_signal_handlers_block_by_func(GetTextObject(), | |
1021 | (gpointer)gtk_text_changed_callback, this); | |
1022 | } | |
1023 | ||
1024 | if ( IsMultiLine() ) | |
1025 | { | |
1026 | gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) ); | |
1027 | ||
1028 | if ( !m_defaultStyle.IsDefault() ) | |
1029 | { | |
1030 | GtkTextIter start, end; | |
1031 | gtk_text_buffer_get_bounds( m_buffer, &start, &end ); | |
1032 | wxGtkTextApplyTagsFromAttr(m_widget, m_buffer, m_defaultStyle, | |
1033 | &start, &end); | |
1034 | } | |
1035 | } | |
1036 | else // single line | |
1037 | { | |
1038 | gtk_entry_set_text( GTK_ENTRY(m_text), buffer ); | |
1039 | } | |
1040 | ||
1041 | if ( !(flags & SetValue_SendEvent) ) | |
1042 | { | |
1043 | g_signal_handlers_unblock_by_func(GetTextObject(), | |
1044 | (gpointer)gtk_text_changed_callback, this); | |
1045 | } | |
1046 | ||
1047 | // This was added after discussion on the list | |
1048 | SetInsertionPoint(0); | |
1049 | } | |
1050 | ||
1051 | void wxTextCtrl::WriteText( const wxString &text ) | |
1052 | { | |
1053 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1054 | ||
1055 | if ( text.empty() ) | |
1056 | return; | |
1057 | ||
1058 | // check if we have a specific style for the current position | |
1059 | wxFontEncoding enc = wxFONTENCODING_SYSTEM; | |
1060 | wxTextAttr style; | |
1061 | if ( GetStyle(GetInsertionPoint(), style) && style.HasFont() ) | |
1062 | { | |
1063 | enc = style.GetFont().GetEncoding(); | |
1064 | } | |
1065 | ||
1066 | if ( enc == wxFONTENCODING_SYSTEM ) | |
1067 | enc = GetTextEncoding(); | |
1068 | ||
1069 | const wxCharBuffer buffer(wxGTK_CONV_ENC(text, enc)); | |
1070 | if ( !buffer ) | |
1071 | { | |
1072 | // we must log an error here as losing the text like this can be a | |
1073 | // serious problem (e.g. imagine the document edited by user being | |
1074 | // empty instead of containing the correct text) | |
1075 | wxLogWarning(_("Failed to insert text in the control.")); | |
1076 | return; | |
1077 | } | |
1078 | ||
1079 | // we're changing the text programmatically | |
1080 | DontMarkDirtyOnNextChange(); | |
1081 | ||
1082 | if ( IsMultiLine() ) | |
1083 | { | |
1084 | // First remove the selection if there is one | |
1085 | // TODO: Is there an easier GTK specific way to do this? | |
1086 | long from, to; | |
1087 | GetSelection(&from, &to); | |
1088 | if (from != to) | |
1089 | Remove(from, to); | |
1090 | ||
1091 | // Insert the text | |
1092 | wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer ); | |
1093 | ||
1094 | GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) ); | |
1095 | // Scroll to cursor, but only if scrollbar thumb is at the very bottom | |
1096 | // won't work when frozen, text view is not using m_buffer then | |
1097 | if (!IsFrozen() && wxIsSameDouble(adj->value, adj->upper - adj->page_size)) | |
1098 | { | |
1099 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), | |
1100 | gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 ); | |
1101 | } | |
1102 | } | |
1103 | else // single line | |
1104 | { | |
1105 | // First remove the selection if there is one | |
1106 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); | |
1107 | ||
1108 | // This moves the cursor pos to behind the inserted text. | |
1109 | gint len = gtk_editable_get_position(GTK_EDITABLE(m_text)); | |
1110 | ||
1111 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len ); | |
1112 | ||
1113 | // Bring entry's cursor uptodate. | |
1114 | gtk_editable_set_position( GTK_EDITABLE(m_text), len ); | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | void wxTextCtrl::AppendText( const wxString &text ) | |
1119 | { | |
1120 | SetInsertionPointEnd(); | |
1121 | WriteText( text ); | |
1122 | } | |
1123 | ||
1124 | wxString wxTextCtrl::GetLineText( long lineNo ) const | |
1125 | { | |
1126 | wxString result; | |
1127 | if ( IsMultiLine() ) | |
1128 | { | |
1129 | GtkTextIter line; | |
1130 | gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo); | |
1131 | ||
1132 | GtkTextIter end = line; | |
1133 | // avoid skipping to the next line end if this one is empty | |
1134 | if ( !gtk_text_iter_ends_line(&line) ) | |
1135 | gtk_text_iter_forward_to_line_end(&end); | |
1136 | ||
1137 | wxGtkString text(gtk_text_buffer_get_text(m_buffer, &line, &end, true)); | |
1138 | result = wxGTK_CONV_BACK(text); | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | if (lineNo == 0) | |
1143 | result = GetValue(); | |
1144 | } | |
1145 | return result; | |
1146 | } | |
1147 | ||
1148 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) | |
1149 | { | |
1150 | /* If you implement this, don't forget to update the documentation! | |
1151 | * (file docs/latex/wx/text.tex) */ | |
1152 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); | |
1153 | } | |
1154 | ||
1155 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const | |
1156 | { | |
1157 | if ( IsMultiLine() ) | |
1158 | { | |
1159 | GtkTextIter iter; | |
1160 | ||
1161 | if (pos > GetLastPosition()) | |
1162 | return false; | |
1163 | ||
1164 | gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos); | |
1165 | ||
1166 | if ( y ) | |
1167 | *y = gtk_text_iter_get_line(&iter); | |
1168 | if ( x ) | |
1169 | *x = gtk_text_iter_get_line_offset(&iter); | |
1170 | } | |
1171 | else // single line control | |
1172 | { | |
1173 | if ( pos <= GTK_ENTRY(m_text)->text_length ) | |
1174 | { | |
1175 | if ( y ) | |
1176 | *y = 0; | |
1177 | if ( x ) | |
1178 | *x = pos; | |
1179 | } | |
1180 | else | |
1181 | { | |
1182 | // index out of bounds | |
1183 | return false; | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | return true; | |
1188 | } | |
1189 | ||
1190 | long wxTextCtrl::XYToPosition(long x, long y ) const | |
1191 | { | |
1192 | if ( IsSingleLine() ) | |
1193 | return 0; | |
1194 | ||
1195 | GtkTextIter iter; | |
1196 | if (y >= gtk_text_buffer_get_line_count (m_buffer)) | |
1197 | return -1; | |
1198 | ||
1199 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y); | |
1200 | if (x >= gtk_text_iter_get_chars_in_line (&iter)) | |
1201 | return -1; | |
1202 | ||
1203 | return gtk_text_iter_get_offset(&iter) + x; | |
1204 | } | |
1205 | ||
1206 | int wxTextCtrl::GetLineLength(long lineNo) const | |
1207 | { | |
1208 | if ( IsMultiLine() ) | |
1209 | { | |
1210 | int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1; | |
1211 | if (lineNo > last_line) | |
1212 | return -1; | |
1213 | ||
1214 | GtkTextIter iter; | |
1215 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo); | |
1216 | // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line | |
1217 | return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1); | |
1218 | } | |
1219 | else | |
1220 | { | |
1221 | wxString str = GetLineText (lineNo); | |
1222 | return (int) str.length(); | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | int wxTextCtrl::GetNumberOfLines() const | |
1227 | { | |
1228 | if ( IsMultiLine() ) | |
1229 | { | |
1230 | return gtk_text_buffer_get_line_count( m_buffer ); | |
1231 | } | |
1232 | else // single line | |
1233 | { | |
1234 | return 1; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | void wxTextCtrl::SetInsertionPoint( long pos ) | |
1239 | { | |
1240 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1241 | ||
1242 | if ( IsMultiLine() ) | |
1243 | { | |
1244 | GtkTextIter iter; | |
1245 | gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos ); | |
1246 | gtk_text_buffer_place_cursor( m_buffer, &iter ); | |
1247 | GtkTextMark* mark = gtk_text_buffer_get_insert(m_buffer); | |
1248 | if (IsFrozen()) | |
1249 | // defer until Thaw, text view is not using m_buffer now | |
1250 | m_showPositionOnThaw = mark; | |
1251 | else | |
1252 | gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text), mark); | |
1253 | } | |
1254 | else | |
1255 | { | |
1256 | // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2? | |
1257 | gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos)); | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | void wxTextCtrl::SetInsertionPointEnd() | |
1262 | { | |
1263 | SetInsertionPoint(-1); | |
1264 | } | |
1265 | ||
1266 | void wxTextCtrl::SetEditable( bool editable ) | |
1267 | { | |
1268 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1269 | ||
1270 | if ( IsMultiLine() ) | |
1271 | { | |
1272 | gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable ); | |
1273 | } | |
1274 | else | |
1275 | { | |
1276 | gtk_editable_set_editable( GTK_EDITABLE(m_text), editable ); | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | bool wxTextCtrl::Enable( bool enable ) | |
1281 | { | |
1282 | if (!wxWindowBase::Enable(enable)) | |
1283 | { | |
1284 | // nothing to do | |
1285 | return false; | |
1286 | } | |
1287 | ||
1288 | gtk_widget_set_sensitive( m_text, enable ); | |
1289 | ||
1290 | return true; | |
1291 | } | |
1292 | ||
1293 | // wxGTK-specific: called recursively by Enable, | |
1294 | // to give widgets an opportunity to correct their colours after they | |
1295 | // have been changed by Enable | |
1296 | void wxTextCtrl::OnEnabled( bool enable ) | |
1297 | { | |
1298 | // If we have a custom background colour, we use this colour in both | |
1299 | // disabled and enabled mode, or we end up with a different colour under the | |
1300 | // text. | |
1301 | wxColour oldColour = GetBackgroundColour(); | |
1302 | if (oldColour.Ok()) | |
1303 | { | |
1304 | // Need to set twice or it'll optimize the useful stuff out | |
1305 | if (oldColour == * wxWHITE) | |
1306 | SetBackgroundColour(*wxBLACK); | |
1307 | else | |
1308 | SetBackgroundColour(*wxWHITE); | |
1309 | SetBackgroundColour(oldColour); | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | void wxTextCtrl::MarkDirty() | |
1314 | { | |
1315 | m_modified = true; | |
1316 | } | |
1317 | ||
1318 | void wxTextCtrl::DiscardEdits() | |
1319 | { | |
1320 | m_modified = false; | |
1321 | } | |
1322 | ||
1323 | // ---------------------------------------------------------------------------- | |
1324 | // max text length support | |
1325 | // ---------------------------------------------------------------------------- | |
1326 | ||
1327 | bool wxTextCtrl::IgnoreTextUpdate() | |
1328 | { | |
1329 | if ( m_countUpdatesToIgnore > 0 ) | |
1330 | { | |
1331 | m_countUpdatesToIgnore--; | |
1332 | ||
1333 | return true; | |
1334 | } | |
1335 | ||
1336 | return false; | |
1337 | } | |
1338 | ||
1339 | bool wxTextCtrl::MarkDirtyOnChange() | |
1340 | { | |
1341 | if ( m_dontMarkDirty ) | |
1342 | { | |
1343 | m_dontMarkDirty = false; | |
1344 | ||
1345 | return false; | |
1346 | } | |
1347 | ||
1348 | return true; | |
1349 | } | |
1350 | ||
1351 | void wxTextCtrl::SetMaxLength(unsigned long len) | |
1352 | { | |
1353 | if ( !HasFlag(wxTE_MULTILINE) ) | |
1354 | { | |
1355 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
1356 | ||
1357 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
1358 | // we had tried to enter more text than allowed by max text length and | |
1359 | // the text wasn't really changed | |
1360 | // | |
1361 | // to detect this and generate TEXT_MAXLEN event instead of | |
1362 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
1363 | // | |
1364 | // when max len is set to 0 we disconnect our handler as it means that | |
1365 | // we shouldn't check anything any more | |
1366 | if ( len ) | |
1367 | { | |
1368 | g_signal_connect (m_text, "insert_text", | |
1369 | G_CALLBACK (gtk_insert_text_callback), this); | |
1370 | } | |
1371 | else // no checking | |
1372 | { | |
1373 | g_signal_handlers_disconnect_by_func (m_text, | |
1374 | (gpointer) gtk_insert_text_callback, this); | |
1375 | } | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | void wxTextCtrl::SetSelection( long from, long to ) | |
1380 | { | |
1381 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1382 | ||
1383 | if (from == -1 && to == -1) | |
1384 | { | |
1385 | from = 0; | |
1386 | to = GetValue().length(); | |
1387 | } | |
1388 | ||
1389 | if ( IsMultiLine() ) | |
1390 | { | |
1391 | GtkTextIter fromi, toi; | |
1392 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); | |
1393 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
1394 | ||
1395 | gtk_text_buffer_place_cursor( m_buffer, &toi ); | |
1396 | gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi ); | |
1397 | } | |
1398 | else | |
1399 | { | |
1400 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | void wxTextCtrl::ShowPosition( long pos ) | |
1405 | { | |
1406 | if (IsMultiLine()) | |
1407 | { | |
1408 | GtkTextIter iter; | |
1409 | gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, int(pos)); | |
1410 | GtkTextMark* mark = gtk_text_buffer_get_mark(m_buffer, "ShowPosition"); | |
1411 | gtk_text_buffer_move_mark(m_buffer, mark, &iter); | |
1412 | if (IsFrozen()) | |
1413 | // defer until Thaw, text view is not using m_buffer now | |
1414 | m_showPositionOnThaw = mark; | |
1415 | else | |
1416 | gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(m_text), mark); | |
1417 | } | |
1418 | } | |
1419 | ||
1420 | wxTextCtrlHitTestResult | |
1421 | wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const | |
1422 | { | |
1423 | if ( !IsMultiLine() ) | |
1424 | { | |
1425 | // not supported | |
1426 | return wxTE_HT_UNKNOWN; | |
1427 | } | |
1428 | ||
1429 | int x, y; | |
1430 | gtk_text_view_window_to_buffer_coords | |
1431 | ( | |
1432 | GTK_TEXT_VIEW(m_text), | |
1433 | GTK_TEXT_WINDOW_TEXT, | |
1434 | pt.x, pt.y, | |
1435 | &x, &y | |
1436 | ); | |
1437 | ||
1438 | GtkTextIter iter; | |
1439 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); | |
1440 | if ( pos ) | |
1441 | *pos = gtk_text_iter_get_offset(&iter); | |
1442 | ||
1443 | return wxTE_HT_ON_TEXT; | |
1444 | } | |
1445 | ||
1446 | long wxTextCtrl::GetInsertionPoint() const | |
1447 | { | |
1448 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); | |
1449 | ||
1450 | if ( IsMultiLine() ) | |
1451 | { | |
1452 | // There is no direct accessor for the cursor, but | |
1453 | // internally, the cursor is the "mark" called | |
1454 | // "insert" in the text view's btree structure. | |
1455 | ||
1456 | GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer ); | |
1457 | GtkTextIter cursor; | |
1458 | gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark ); | |
1459 | ||
1460 | return gtk_text_iter_get_offset( &cursor ); | |
1461 | } | |
1462 | else | |
1463 | { | |
1464 | return (long) gtk_editable_get_position(GTK_EDITABLE(m_text)); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | wxTextPos wxTextCtrl::GetLastPosition() const | |
1469 | { | |
1470 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); | |
1471 | ||
1472 | int pos = 0; | |
1473 | ||
1474 | if ( IsMultiLine() ) | |
1475 | { | |
1476 | GtkTextIter end; | |
1477 | gtk_text_buffer_get_end_iter( m_buffer, &end ); | |
1478 | ||
1479 | pos = gtk_text_iter_get_offset( &end ); | |
1480 | } | |
1481 | else | |
1482 | { | |
1483 | pos = GTK_ENTRY(m_text)->text_length; | |
1484 | } | |
1485 | ||
1486 | return (long)pos; | |
1487 | } | |
1488 | ||
1489 | void wxTextCtrl::Remove( long from, long to ) | |
1490 | { | |
1491 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1492 | ||
1493 | if ( IsMultiLine() ) | |
1494 | { | |
1495 | GtkTextIter fromi, toi; | |
1496 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); | |
1497 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
1498 | ||
1499 | gtk_text_buffer_delete( m_buffer, &fromi, &toi ); | |
1500 | } | |
1501 | else // single line | |
1502 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1503 | } | |
1504 | ||
1505 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) | |
1506 | { | |
1507 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1508 | ||
1509 | Remove( from, to ); | |
1510 | ||
1511 | if (!value.empty()) | |
1512 | { | |
1513 | SetInsertionPoint( from ); | |
1514 | WriteText( value ); | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | void wxTextCtrl::Cut() | |
1519 | { | |
1520 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1521 | ||
1522 | if ( IsMultiLine() ) | |
1523 | g_signal_emit_by_name (m_text, "cut-clipboard"); | |
1524 | else | |
1525 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text)); | |
1526 | } | |
1527 | ||
1528 | void wxTextCtrl::Copy() | |
1529 | { | |
1530 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1531 | ||
1532 | if ( IsMultiLine() ) | |
1533 | g_signal_emit_by_name (m_text, "copy-clipboard"); | |
1534 | else | |
1535 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text)); | |
1536 | } | |
1537 | ||
1538 | void wxTextCtrl::Paste() | |
1539 | { | |
1540 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1541 | ||
1542 | if ( IsMultiLine() ) | |
1543 | g_signal_emit_by_name (m_text, "paste-clipboard"); | |
1544 | else | |
1545 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text)); | |
1546 | } | |
1547 | ||
1548 | // Undo/redo | |
1549 | void wxTextCtrl::Undo() | |
1550 | { | |
1551 | // TODO | |
1552 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); | |
1553 | } | |
1554 | ||
1555 | void wxTextCtrl::Redo() | |
1556 | { | |
1557 | // TODO | |
1558 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); | |
1559 | } | |
1560 | ||
1561 | bool wxTextCtrl::CanUndo() const | |
1562 | { | |
1563 | // TODO | |
1564 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); | |
1565 | return false; | |
1566 | } | |
1567 | ||
1568 | bool wxTextCtrl::CanRedo() const | |
1569 | { | |
1570 | // TODO | |
1571 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); | |
1572 | return false; | |
1573 | } | |
1574 | ||
1575 | // If the return values from and to are the same, there is no | |
1576 | // selection. | |
1577 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const | |
1578 | { | |
1579 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1580 | ||
1581 | gint from = -1; | |
1582 | gint to = -1; | |
1583 | bool haveSelection = false; | |
1584 | ||
1585 | if ( IsMultiLine() ) | |
1586 | { | |
1587 | GtkTextIter ifrom, ito; | |
1588 | if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) ) | |
1589 | { | |
1590 | haveSelection = true; | |
1591 | from = gtk_text_iter_get_offset(&ifrom); | |
1592 | to = gtk_text_iter_get_offset(&ito); | |
1593 | } | |
1594 | } | |
1595 | else // not multi-line | |
1596 | { | |
1597 | if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text), | |
1598 | &from, &to) ) | |
1599 | { | |
1600 | haveSelection = true; | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | if (! haveSelection ) | |
1605 | from = to = GetInsertionPoint(); | |
1606 | ||
1607 | if ( from > to ) | |
1608 | { | |
1609 | // exchange them to be compatible with wxMSW | |
1610 | gint tmp = from; | |
1611 | from = to; | |
1612 | to = tmp; | |
1613 | } | |
1614 | ||
1615 | if ( fromOut ) | |
1616 | *fromOut = from; | |
1617 | if ( toOut ) | |
1618 | *toOut = to; | |
1619 | } | |
1620 | ||
1621 | ||
1622 | bool wxTextCtrl::IsEditable() const | |
1623 | { | |
1624 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); | |
1625 | ||
1626 | if ( IsMultiLine() ) | |
1627 | { | |
1628 | return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)); | |
1629 | } | |
1630 | else | |
1631 | { | |
1632 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | bool wxTextCtrl::IsModified() const | |
1637 | { | |
1638 | return m_modified; | |
1639 | } | |
1640 | ||
1641 | void wxTextCtrl::Clear() | |
1642 | { | |
1643 | SetValue( wxEmptyString ); | |
1644 | } | |
1645 | ||
1646 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) | |
1647 | { | |
1648 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
1649 | ||
1650 | if ( key_event.GetKeyCode() == WXK_RETURN ) | |
1651 | { | |
1652 | if ( HasFlag(wxTE_PROCESS_ENTER) ) | |
1653 | { | |
1654 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1655 | event.SetEventObject(this); | |
1656 | event.SetString(GetValue()); | |
1657 | if ( GetEventHandler()->ProcessEvent(event) ) | |
1658 | return; | |
1659 | } | |
1660 | ||
1661 | // FIXME: this is not the right place to do it, wxDialog::OnCharHook() | |
1662 | // probably is | |
1663 | if ( IsSingleLine() ) | |
1664 | { | |
1665 | // This will invoke the dialog default action, such | |
1666 | // as the clicking the default button. | |
1667 | ||
1668 | wxWindow *top_frame = m_parent; | |
1669 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
1670 | top_frame = top_frame->GetParent(); | |
1671 | ||
1672 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
1673 | { | |
1674 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
1675 | ||
1676 | if (window->default_widget) | |
1677 | { | |
1678 | gtk_widget_activate (window->default_widget); | |
1679 | return; | |
1680 | } | |
1681 | } | |
1682 | } | |
1683 | } | |
1684 | ||
1685 | key_event.Skip(); | |
1686 | } | |
1687 | ||
1688 | GtkWidget* wxTextCtrl::GetConnectWidget() | |
1689 | { | |
1690 | return GTK_WIDGET(m_text); | |
1691 | } | |
1692 | ||
1693 | GdkWindow *wxTextCtrl::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
1694 | { | |
1695 | if ( IsMultiLine() ) | |
1696 | { | |
1697 | return gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1698 | GTK_TEXT_WINDOW_TEXT ); | |
1699 | } | |
1700 | else | |
1701 | { | |
1702 | return GTK_ENTRY(m_text)->text_area; | |
1703 | } | |
1704 | } | |
1705 | ||
1706 | // the font will change for subsequent text insertiongs | |
1707 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
1708 | { | |
1709 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); | |
1710 | ||
1711 | if ( !wxTextCtrlBase::SetFont(font) ) | |
1712 | { | |
1713 | // font didn't change, nothing to do | |
1714 | return false; | |
1715 | } | |
1716 | ||
1717 | if ( IsMultiLine() ) | |
1718 | { | |
1719 | SetUpdateFont(true); | |
1720 | ||
1721 | m_defaultStyle.SetFont(font); | |
1722 | ||
1723 | ChangeFontGlobally(); | |
1724 | } | |
1725 | ||
1726 | return true; | |
1727 | } | |
1728 | ||
1729 | void wxTextCtrl::ChangeFontGlobally() | |
1730 | { | |
1731 | // this method is very inefficient and hence should be called as rarely as | |
1732 | // possible! | |
1733 | // | |
1734 | // TODO: it can be implemented much more efficiently for GTK2 | |
1735 | wxASSERT_MSG( IsMultiLine(), | |
1736 | _T("shouldn't be called for single line controls") ); | |
1737 | ||
1738 | wxString value = GetValue(); | |
1739 | if ( !value.empty() ) | |
1740 | { | |
1741 | SetUpdateFont(false); | |
1742 | ||
1743 | Clear(); | |
1744 | AppendText(value); | |
1745 | } | |
1746 | } | |
1747 | ||
1748 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
1749 | { | |
1750 | if ( !wxControl::SetForegroundColour(colour) ) | |
1751 | return false; | |
1752 | ||
1753 | // update default fg colour too | |
1754 | m_defaultStyle.SetTextColour(colour); | |
1755 | ||
1756 | return true; | |
1757 | } | |
1758 | ||
1759 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) | |
1760 | { | |
1761 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); | |
1762 | ||
1763 | if ( !wxControl::SetBackgroundColour( colour ) ) | |
1764 | return false; | |
1765 | ||
1766 | if (!m_backgroundColour.Ok()) | |
1767 | return false; | |
1768 | ||
1769 | // change active background color too | |
1770 | m_defaultStyle.SetBackgroundColour( colour ); | |
1771 | ||
1772 | return true; | |
1773 | } | |
1774 | ||
1775 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) | |
1776 | { | |
1777 | if ( IsMultiLine() ) | |
1778 | { | |
1779 | if ( style.IsDefault() ) | |
1780 | { | |
1781 | // nothing to do | |
1782 | return true; | |
1783 | } | |
1784 | ||
1785 | gint l = gtk_text_buffer_get_char_count( m_buffer ); | |
1786 | ||
1787 | wxCHECK_MSG( start >= 0 && end <= l, false, | |
1788 | _T("invalid range in wxTextCtrl::SetStyle") ); | |
1789 | ||
1790 | GtkTextIter starti, endi; | |
1791 | gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start ); | |
1792 | gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end ); | |
1793 | ||
1794 | wxGtkTextApplyTagsFromAttr( m_widget, m_buffer, style, &starti, &endi ); | |
1795 | ||
1796 | return true; | |
1797 | } | |
1798 | //else: single line text controls don't support styles | |
1799 | ||
1800 | return false; | |
1801 | } | |
1802 | ||
1803 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) | |
1804 | { | |
1805 | gtk_widget_modify_style(m_text, style); | |
1806 | } | |
1807 | ||
1808 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
1809 | { | |
1810 | Cut(); | |
1811 | } | |
1812 | ||
1813 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1814 | { | |
1815 | Copy(); | |
1816 | } | |
1817 | ||
1818 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1819 | { | |
1820 | Paste(); | |
1821 | } | |
1822 | ||
1823 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1824 | { | |
1825 | Undo(); | |
1826 | } | |
1827 | ||
1828 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1829 | { | |
1830 | Redo(); | |
1831 | } | |
1832 | ||
1833 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1834 | { | |
1835 | event.Enable( CanCut() ); | |
1836 | } | |
1837 | ||
1838 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1839 | { | |
1840 | event.Enable( CanCopy() ); | |
1841 | } | |
1842 | ||
1843 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1844 | { | |
1845 | event.Enable( CanPaste() ); | |
1846 | } | |
1847 | ||
1848 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1849 | { | |
1850 | event.Enable( CanUndo() ); | |
1851 | } | |
1852 | ||
1853 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1854 | { | |
1855 | event.Enable( CanRedo() ); | |
1856 | } | |
1857 | ||
1858 | wxSize wxTextCtrl::DoGetBestSize() const | |
1859 | { | |
1860 | // FIXME should be different for multi-line controls... | |
1861 | wxSize ret( wxControl::DoGetBestSize() ); | |
1862 | wxSize best(80, ret.y); | |
1863 | CacheBestSize(best); | |
1864 | return best; | |
1865 | } | |
1866 | ||
1867 | // ---------------------------------------------------------------------------- | |
1868 | // freeze/thaw | |
1869 | // ---------------------------------------------------------------------------- | |
1870 | ||
1871 | void wxTextCtrl::Freeze() | |
1872 | { | |
1873 | wxCHECK_RET(m_text != NULL, wxT("invalid text ctrl")); | |
1874 | ||
1875 | if ( HasFlag(wxTE_MULTILINE) ) | |
1876 | { | |
1877 | if (m_freezeCount++ == 0) | |
1878 | { | |
1879 | // freeze textview updates and remove buffer | |
1880 | g_signal_connect (m_text, "expose_event", | |
1881 | G_CALLBACK (gtk_text_exposed_callback), this); | |
1882 | g_signal_connect (m_widget, "expose_event", | |
1883 | G_CALLBACK (gtk_text_exposed_callback), this); | |
1884 | gtk_widget_set_sensitive(m_widget, false); | |
1885 | g_object_ref(m_buffer); | |
1886 | GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL); | |
1887 | GtkTextMark* mark = GTK_TEXT_VIEW(m_text)->first_para_mark; | |
1888 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new); | |
1889 | // gtk_text_view_set_buffer adds its own reference | |
1890 | g_object_unref(buf_new); | |
1891 | // This mark should be deleted when the buffer is changed, | |
1892 | // but it's not (in GTK+ up to at least 2.10.6). | |
1893 | // Otherwise these anonymous marks start to build up in the buffer, | |
1894 | // and Freeze takes longer and longer each time it is called. | |
1895 | if (GTK_IS_TEXT_MARK(mark) && !gtk_text_mark_get_deleted(mark)) | |
1896 | gtk_text_buffer_delete_mark(m_buffer, mark); | |
1897 | } | |
1898 | } | |
1899 | } | |
1900 | ||
1901 | void wxTextCtrl::Thaw() | |
1902 | { | |
1903 | if ( HasFlag(wxTE_MULTILINE) ) | |
1904 | { | |
1905 | wxCHECK_RET(m_freezeCount != 0, _T("Thaw() without matching Freeze()")); | |
1906 | ||
1907 | if (--m_freezeCount == 0) | |
1908 | { | |
1909 | // Reattach buffer and thaw textview updates | |
1910 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer); | |
1911 | g_object_unref(m_buffer); | |
1912 | gtk_widget_set_sensitive(m_widget, true); | |
1913 | g_signal_handlers_disconnect_by_func (m_widget, | |
1914 | (gpointer) gtk_text_exposed_callback, this); | |
1915 | g_signal_handlers_disconnect_by_func (m_text, | |
1916 | (gpointer) gtk_text_exposed_callback, this); | |
1917 | if (m_showPositionOnThaw != NULL) | |
1918 | { | |
1919 | gtk_text_view_scroll_mark_onscreen( | |
1920 | GTK_TEXT_VIEW(m_text), m_showPositionOnThaw); | |
1921 | m_showPositionOnThaw = NULL; | |
1922 | } | |
1923 | } | |
1924 | } | |
1925 | } | |
1926 | ||
1927 | // ---------------------------------------------------------------------------- | |
1928 | // wxTextUrlEvent passing if style & wxTE_AUTO_URL | |
1929 | // ---------------------------------------------------------------------------- | |
1930 | ||
1931 | // FIXME: when dragging on a link the sample gets an "Unknown event". | |
1932 | // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or | |
1933 | // a buggy sample, or something else | |
1934 | void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event) | |
1935 | { | |
1936 | event.Skip(); | |
1937 | if( !HasFlag(wxTE_AUTO_URL) ) | |
1938 | return; | |
1939 | ||
1940 | gint x, y; | |
1941 | GtkTextIter start, end; | |
1942 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer), | |
1943 | "wxUrl"); | |
1944 | ||
1945 | gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET, | |
1946 | event.GetX(), event.GetY(), &x, &y); | |
1947 | ||
1948 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y); | |
1949 | if (!gtk_text_iter_has_tag(&end, tag)) | |
1950 | { | |
1951 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1952 | GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor); | |
1953 | return; | |
1954 | } | |
1955 | ||
1956 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1957 | GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor); | |
1958 | ||
1959 | start = end; | |
1960 | if(!gtk_text_iter_begins_tag(&start, tag)) | |
1961 | gtk_text_iter_backward_to_tag_toggle(&start, tag); | |
1962 | if(!gtk_text_iter_ends_tag(&end, tag)) | |
1963 | gtk_text_iter_forward_to_tag_toggle(&end, tag); | |
1964 | ||
1965 | // Native context menu is probably not desired on an URL. | |
1966 | // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value | |
1967 | if(event.GetEventType() == wxEVT_RIGHT_DOWN) | |
1968 | event.Skip(false); | |
1969 | ||
1970 | wxTextUrlEvent url_event(m_windowId, event, | |
1971 | gtk_text_iter_get_offset(&start), | |
1972 | gtk_text_iter_get_offset(&end)); | |
1973 | ||
1974 | InitCommandEvent(url_event); | |
1975 | // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag) | |
1976 | //event.Skip(!GetEventHandler()->ProcessEvent(url_event)); | |
1977 | GetEventHandler()->ProcessEvent(url_event); | |
1978 | } | |
1979 | ||
1980 | bool wxTextCtrl::GTKProcessEvent(wxEvent& event) const | |
1981 | { | |
1982 | bool rc = wxTextCtrlBase::GTKProcessEvent(event); | |
1983 | ||
1984 | // GtkTextView starts a drag operation when left mouse button is pressed | |
1985 | // and ends it when it is released and if it doesn't get the release event | |
1986 | // the next click on a control results in an assertion failure inside | |
1987 | // gtk_text_view_start_selection_drag() which simply *kills* the program | |
1988 | // without anything we can do about it, so always let GTK+ have this event | |
1989 | return rc && (IsSingleLine() || event.GetEventType() != wxEVT_LEFT_UP); | |
1990 | } | |
1991 | ||
1992 | // static | |
1993 | wxVisualAttributes | |
1994 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1995 | { | |
1996 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
1997 | } | |
1998 | ||
1999 | #endif // wxUSE_TEXTCTRL |