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