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