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