]>
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" |
88a7a4e1 WS |
20 | #endif |
21 | ||
c77a6796 | 22 | #include "wx/math.h" |
ae0bdb01 | 23 | #include "wx/settings.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 | |
034be888 | 457 | win->SetModified(); |
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 | |
41b81aed RR |
465 | //----------------------------------------------------------------------------- |
466 | // "expose_event" from scrolled window and textview | |
467 | //----------------------------------------------------------------------------- | |
468 | ||
865bb325 | 469 | extern "C" { |
41b81aed RR |
470 | static gboolean |
471 | gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win ) | |
472 | { | |
473 | return TRUE; | |
474 | } | |
865bb325 | 475 | } |
1c35b54e | 476 | |
1c35b54e | 477 | |
2f2aa628 RR |
478 | //----------------------------------------------------------------------------- |
479 | // wxTextCtrl | |
480 | //----------------------------------------------------------------------------- | |
481 | ||
482 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
483 | ||
c801d85f | 484 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
2830bf19 | 485 | EVT_CHAR(wxTextCtrl::OnChar) |
e702ff0f JS |
486 | |
487 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
488 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
489 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
490 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
491 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
492 | ||
493 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
494 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
495 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
496 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
497 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
9440c3d0 | 498 | |
9440c3d0 KH |
499 | // wxTE_AUTO_URL wxTextUrl support. Currently only creates |
500 | // wxTextUrlEvent in the same cases as wxMSW, more can be added here. | |
501 | EVT_MOTION (wxTextCtrl::OnUrlMouseEvent) | |
502 | EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
503 | EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent) | |
504 | EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent) | |
505 | EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
506 | EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent) | |
507 | EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent) | |
c801d85f KB |
508 | END_EVENT_TABLE() |
509 | ||
01041145 | 510 | void wxTextCtrl::Init() |
f5abe911 | 511 | { |
ce2f50e3 | 512 | m_ignoreNextUpdate = |
7d8268a1 WS |
513 | m_modified = false; |
514 | SetUpdateFont(false); | |
85396430 | 515 | m_text = NULL; |
41b81aed | 516 | m_frozenness = 0; |
9440c3d0 KH |
517 | m_gdkHandCursor = NULL; |
518 | m_gdkXTermCursor = NULL; | |
9440c3d0 KH |
519 | } |
520 | ||
521 | wxTextCtrl::~wxTextCtrl() | |
522 | { | |
9440c3d0 KH |
523 | if(m_gdkHandCursor) |
524 | gdk_cursor_unref(m_gdkHandCursor); | |
525 | if(m_gdkXTermCursor) | |
526 | gdk_cursor_unref(m_gdkXTermCursor); | |
f5abe911 | 527 | } |
13289f04 | 528 | |
13111b2a VZ |
529 | wxTextCtrl::wxTextCtrl( wxWindow *parent, |
530 | wxWindowID id, | |
531 | const wxString &value, | |
532 | const wxPoint &pos, | |
533 | const wxSize &size, | |
534 | long style, | |
535 | const wxValidator& validator, | |
536 | const wxString &name ) | |
f5abe911 | 537 | { |
01041145 VZ |
538 | Init(); |
539 | ||
f5abe911 RR |
540 | Create( parent, id, value, pos, size, style, validator, name ); |
541 | } | |
c801d85f | 542 | |
13111b2a VZ |
543 | bool wxTextCtrl::Create( wxWindow *parent, |
544 | wxWindowID id, | |
545 | const wxString &value, | |
546 | const wxPoint &pos, | |
547 | const wxSize &size, | |
548 | long style, | |
549 | const wxValidator& validator, | |
550 | const wxString &name ) | |
c801d85f | 551 | { |
7d8268a1 WS |
552 | m_needParent = true; |
553 | m_acceptsFocus = true; | |
484e45bf | 554 | |
4dcaf11a RR |
555 | if (!PreCreation( parent, pos, size ) || |
556 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
557 | { | |
223d09f6 | 558 | wxFAIL_MSG( wxT("wxTextCtrl creation failed") ); |
7d8268a1 | 559 | return false; |
4dcaf11a | 560 | } |
6de97a3b | 561 | |
2830bf19 | 562 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
a8bf1826 | 563 | |
ab46dc18 | 564 | if (multi_line) |
2830bf19 | 565 | { |
fab591c5 RR |
566 | // Create view |
567 | m_text = gtk_text_view_new(); | |
a8bf1826 | 568 | |
41b81aed | 569 | m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
a8bf1826 | 570 | |
fab591c5 RR |
571 | // create scrolled window |
572 | m_widget = gtk_scrolled_window_new( NULL, NULL ); | |
573 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ), | |
574 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
a8bf1826 | 575 | |
fab591c5 RR |
576 | // Insert view into scrolled window |
577 | gtk_container_add( GTK_CONTAINER(m_widget), m_text ); | |
a8bf1826 | 578 | |
c2110823 | 579 | // translate wx wrapping style to GTK+ |
9ddb3948 | 580 | GtkWrapMode wrap; |
c2110823 | 581 | if ( HasFlag( wxTE_DONTWRAP ) ) |
9ddb3948 | 582 | wrap = GTK_WRAP_NONE; |
c4590236 | 583 | else if ( HasFlag( wxTE_CHARWRAP ) ) |
9ddb3948 | 584 | wrap = GTK_WRAP_CHAR; |
c4590236 | 585 | else if ( HasFlag( wxTE_WORDWRAP ) ) |
9ddb3948 | 586 | wrap = GTK_WRAP_WORD; |
c4590236 VZ |
587 | else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 |
588 | { | |
589 | // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 | |
590 | #ifdef __WXGTK24__ | |
c79146df VZ |
591 | if ( !gtk_check_version(2,4,0) ) |
592 | { | |
593 | wrap = GTK_WRAP_WORD_CHAR; | |
594 | } | |
595 | else | |
c4590236 | 596 | #endif |
34b8f3ef | 597 | wrap = GTK_WRAP_WORD; |
c4590236 | 598 | } |
9ddb3948 VZ |
599 | |
600 | gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); | |
805dd538 | 601 | |
6493aaca | 602 | GtkScrolledWindowSetBorder(m_widget, style); |
7d8268a1 | 603 | |
e327fddf KH |
604 | gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK ); |
605 | ||
055e633d | 606 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
2830bf19 RR |
607 | } |
608 | else | |
609 | { | |
fab591c5 | 610 | // a single-line text control: no need for scrollbars |
2830bf19 | 611 | m_widget = |
1c35b54e | 612 | m_text = gtk_entry_new(); |
44c5573d | 613 | |
02a6e358 | 614 | if (style & wxNO_BORDER) |
7d1fea10 | 615 | g_object_set (m_text, "has-frame", FALSE, NULL); |
2830bf19 | 616 | } |
484e45bf | 617 | |
db434467 | 618 | m_parent->DoAddChild( this ); |
eda40bfc | 619 | |
76fcf0f2 | 620 | m_focusWidget = m_text; |
db434467 | 621 | |
abdeb9e7 | 622 | PostCreation(size); |
484e45bf | 623 | |
2830bf19 | 624 | if (multi_line) |
0c131a5a | 625 | { |
2830bf19 | 626 | gtk_widget_show(m_text); |
0c131a5a VZ |
627 | SetVScrollAdjustment(gtk_scrolled_window_get_vadjustment((GtkScrolledWindow*)m_widget)); |
628 | } | |
13289f04 | 629 | |
7d8268a1 | 630 | if (!value.empty()) |
2830bf19 | 631 | { |
fab591c5 | 632 | SetValue( value ); |
2830bf19 | 633 | } |
484e45bf | 634 | |
2830bf19 RR |
635 | if (style & wxTE_PASSWORD) |
636 | { | |
637 | if (!multi_line) | |
638 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
639 | } | |
8bbe427f | 640 | |
2830bf19 RR |
641 | if (style & wxTE_READONLY) |
642 | { | |
643 | if (!multi_line) | |
6f85e712 | 644 | gtk_editable_set_editable( GTK_EDITABLE(m_text), FALSE ); |
fab591c5 | 645 | else |
98a8daf4 | 646 | gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE); |
98a8daf4 VS |
647 | } |
648 | ||
c663fbea VS |
649 | if (multi_line) |
650 | { | |
651 | if (style & wxTE_RIGHT) | |
652 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT ); | |
653 | else if (style & wxTE_CENTRE) | |
654 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER ); | |
655 | // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT | |
656 | } | |
c663fbea VS |
657 | else |
658 | { | |
77f70672 RR |
659 | #ifdef __WXGTK24__ |
660 | // gtk_entry_set_alignment was introduced in gtk+-2.3.5 | |
661 | if (!gtk_check_version(2,4,0)) | |
662 | { | |
663 | if (style & wxTE_RIGHT) | |
664 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 ); | |
665 | else if (style & wxTE_CENTRE) | |
666 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 ); | |
667 | } | |
668 | #endif | |
c663fbea | 669 | } |
7d8268a1 | 670 | |
fab591c5 | 671 | // We want to be notified about text changes. |
fab591c5 RR |
672 | if (multi_line) |
673 | { | |
9fa72bd2 MR |
674 | g_signal_connect (m_buffer, "changed", |
675 | G_CALLBACK (gtk_text_changed_callback), this); | |
9440c3d0 KH |
676 | |
677 | // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style | |
678 | if (style & wxTE_AUTO_URL) | |
679 | { | |
680 | GtkTextIter start, end; | |
681 | m_gdkHandCursor = gdk_cursor_new(GDK_HAND2); | |
682 | m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM); | |
683 | ||
684 | // We create our wxUrl tag here for slight efficiency gain - we | |
685 | // don't have to check for the tag existance in callbacks, | |
686 | // hereby it's guaranteed to exist. | |
687 | gtk_text_buffer_create_tag(m_buffer, "wxUrl", | |
688 | "foreground", "blue", | |
689 | "underline", PANGO_UNDERLINE_SINGLE, | |
690 | NULL); | |
691 | ||
692 | // Check for URLs after each text change | |
9fa72bd2 MR |
693 | g_signal_connect_after (m_buffer, "insert_text", |
694 | G_CALLBACK (au_insert_text_callback), this); | |
695 | g_signal_connect_after (m_buffer, "delete_range", | |
696 | G_CALLBACK (au_delete_range_callback), this); | |
9440c3d0 KH |
697 | |
698 | // Block all wxUrl tag applying unless we do it ourselves, in which case we | |
699 | // block this callback temporarily. This takes care of gtk+ internal | |
700 | // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise, | |
701 | // which is undesired because only a part of the URL might be copied. | |
702 | // The insert-text signal emitted inside it will take care of newly formed | |
703 | // or wholly copied URLs. | |
9fa72bd2 MR |
704 | g_signal_connect (m_buffer, "apply_tag", |
705 | G_CALLBACK (au_apply_tag_callback), NULL); | |
9440c3d0 KH |
706 | |
707 | // Check for URLs in the initial string passed to Create | |
708 | gtk_text_buffer_get_start_iter(m_buffer, &start); | |
709 | gtk_text_buffer_get_end_iter(m_buffer, &end); | |
710 | au_check_range(&start, &end); | |
711 | } | |
fab591c5 RR |
712 | } |
713 | else | |
fab591c5 | 714 | { |
9fa72bd2 MR |
715 | g_signal_connect (m_text, "changed", |
716 | G_CALLBACK (gtk_text_changed_callback), this); | |
fab591c5 | 717 | } |
ce16e5d7 | 718 | |
65045edd | 719 | m_cursor = wxCursor( wxCURSOR_IBEAM ); |
13111b2a | 720 | |
9d522606 | 721 | wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont()); |
17665a2b VZ |
722 | SetDefaultStyle( attrDef ); |
723 | ||
7d8268a1 | 724 | return true; |
2830bf19 | 725 | } |
484e45bf | 726 | |
9d522606 | 727 | |
2830bf19 RR |
728 | void wxTextCtrl::CalculateScrollbar() |
729 | { | |
6de97a3b | 730 | } |
c801d85f | 731 | |
03f38c58 | 732 | wxString wxTextCtrl::GetValue() const |
c801d85f | 733 | { |
902725ee | 734 | wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") ); |
8bbe427f | 735 | |
2830bf19 RR |
736 | wxString tmp; |
737 | if (m_windowStyle & wxTE_MULTILINE) | |
738 | { | |
fab591c5 | 739 | GtkTextIter start; |
41b81aed | 740 | gtk_text_buffer_get_start_iter( m_buffer, &start ); |
fab591c5 | 741 | GtkTextIter end; |
41b81aed RR |
742 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
743 | gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE ); | |
5b87f8bf | 744 | |
a3669332 VZ |
745 | const wxWxCharBuffer buf = wxGTK_CONV_BACK(text); |
746 | if ( buf ) | |
747 | tmp = buf; | |
a8bf1826 | 748 | |
fab591c5 | 749 | g_free( text ); |
2830bf19 RR |
750 | } |
751 | else | |
752 | { | |
a3669332 VZ |
753 | const gchar *text = gtk_entry_get_text( GTK_ENTRY(m_text) ); |
754 | const wxWxCharBuffer buf = wxGTK_CONV_BACK( text ); | |
755 | if ( buf ) | |
756 | tmp = buf; | |
2830bf19 | 757 | } |
a8bf1826 | 758 | |
2830bf19 | 759 | return tmp; |
6de97a3b | 760 | } |
c801d85f KB |
761 | |
762 | void wxTextCtrl::SetValue( const wxString &value ) | |
763 | { | |
223d09f6 | 764 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 765 | |
2830bf19 RR |
766 | if (m_windowStyle & wxTE_MULTILINE) |
767 | { | |
a3669332 | 768 | const wxCharBuffer buffer(wxGTK_CONV(value)); |
430ae62e JS |
769 | if ( !buffer ) |
770 | { | |
771 | // what else can we do? at least don't crash... | |
772 | return; | |
773 | } | |
418cf02e | 774 | |
a3669332 VZ |
775 | if (gtk_text_buffer_get_char_count(m_buffer) != 0) |
776 | IgnoreNextTextUpdate(); | |
777 | ||
41b81aed | 778 | gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) ); |
2830bf19 | 779 | } |
265c34ee | 780 | else // single line |
2830bf19 | 781 | { |
265c34ee VZ |
782 | // gtk_entry_set_text() emits two "changed" signals because internally |
783 | // it calls gtk_editable_delete_text() and gtk_editable_insert_text() | |
784 | // but we want to have only one event | |
785 | IgnoreNextTextUpdate(); | |
786 | ||
787 | gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV(value) ); | |
2830bf19 | 788 | } |
f6bcfd97 BP |
789 | |
790 | // GRG, Jun/2000: Changed this after a lot of discussion in | |
77ffb593 | 791 | // the lists. wxWidgets 2.2 will have a set of flags to |
f6bcfd97 BP |
792 | // customize this behaviour. |
793 | SetInsertionPoint(0); | |
a8bf1826 | 794 | |
7d8268a1 | 795 | m_modified = false; |
6de97a3b | 796 | } |
c801d85f KB |
797 | |
798 | void wxTextCtrl::WriteText( const wxString &text ) | |
799 | { | |
223d09f6 | 800 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 801 | |
17665a2b VZ |
802 | if ( text.empty() ) |
803 | return; | |
484e45bf | 804 | |
a3669332 VZ |
805 | const wxCharBuffer buffer(wxGTK_CONV(text)); |
806 | if ( !buffer ) | |
807 | { | |
808 | // what else can we do? at least don't crash... | |
809 | return; | |
810 | } | |
811 | ||
c04ec496 VZ |
812 | // gtk_text_changed_callback() will set m_modified to true but m_modified |
813 | // shouldn't be changed by the program writing to the text control itself, | |
814 | // so save the old value and restore when we're done | |
815 | bool oldModified = m_modified; | |
816 | ||
17665a2b | 817 | if ( m_windowStyle & wxTE_MULTILINE ) |
2830bf19 | 818 | { |
ea5449ae RD |
819 | // First remove the selection if there is one |
820 | // TODO: Is there an easier GTK specific way to do this? | |
821 | long from, to; | |
822 | GetSelection(&from, &to); | |
823 | if (from != to) | |
824 | Remove(from, to); | |
825 | ||
826 | // Insert the text | |
41b81aed | 827 | wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer ); |
a8bf1826 | 828 | |
71aba833 VZ |
829 | GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) ); |
830 | // Scroll to cursor, but only if scrollbar thumb is at the very bottom | |
c77a6796 | 831 | if ( wxIsSameDouble(adj->value, adj->upper - adj->page_size) ) |
71aba833 VZ |
832 | { |
833 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), | |
41b81aed | 834 | gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 ); |
71aba833 | 835 | } |
2830bf19 | 836 | } |
17665a2b | 837 | else // single line |
2830bf19 | 838 | { |
2b5f62a0 VZ |
839 | // First remove the selection if there is one |
840 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); | |
841 | ||
ba3f6b44 | 842 | // This moves the cursor pos to behind the inserted text. |
afa7bd1e | 843 | gint len = gtk_editable_get_position(GTK_EDITABLE(m_text)); |
a8bf1826 | 844 | |
fab591c5 | 845 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len ); |
a8bf1826 | 846 | |
ba3f6b44 | 847 | // Bring entry's cursor uptodate. |
6f85e712 | 848 | gtk_editable_set_position( GTK_EDITABLE(m_text), len ); |
2830bf19 | 849 | } |
eda40bfc | 850 | |
c04ec496 | 851 | m_modified = oldModified; |
6de97a3b | 852 | } |
c801d85f | 853 | |
a6e21573 HH |
854 | void wxTextCtrl::AppendText( const wxString &text ) |
855 | { | |
ba3f6b44 RR |
856 | SetInsertionPointEnd(); |
857 | WriteText( text ); | |
858 | } | |
2df7be7f | 859 | |
ba3f6b44 RR |
860 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
861 | { | |
a6e21573 HH |
862 | if (m_windowStyle & wxTE_MULTILINE) |
863 | { | |
905f2110 | 864 | GtkTextIter line; |
41b81aed | 865 | gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo); |
8c6785f0 MR |
866 | GtkTextIter end = line; |
867 | gtk_text_iter_forward_to_line_end(&end); | |
41b81aed | 868 | gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE); |
58192970 | 869 | wxString result(wxGTK_CONV_BACK(text)); |
905f2110 | 870 | g_free(text); |
8c6785f0 | 871 | return result; |
a6e21573 | 872 | } |
ba3f6b44 | 873 | else |
a81258be | 874 | { |
ba3f6b44 RR |
875 | if (lineNo == 0) return GetValue(); |
876 | return wxEmptyString; | |
a81258be | 877 | } |
6de97a3b | 878 | } |
c801d85f | 879 | |
a81258be RR |
880 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
881 | { | |
ac0d36b5 HH |
882 | /* If you implement this, don't forget to update the documentation! |
883 | * (file docs/latex/wx/text.tex) */ | |
223d09f6 | 884 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); |
a81258be | 885 | } |
112892b9 | 886 | |
0efe5ba7 | 887 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 888 | { |
96385642 | 889 | if ( m_windowStyle & wxTE_MULTILINE ) |
805dd538 | 890 | { |
f29a481a MR |
891 | GtkTextIter iter; |
892 | gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos); | |
893 | if (gtk_text_iter_is_end(&iter)) | |
894 | return false; | |
895 | ||
896 | *y = gtk_text_iter_get_line(&iter); | |
897 | *x = gtk_text_iter_get_line_offset(&iter); | |
805dd538 | 898 | } |
96385642 VZ |
899 | else // single line control |
900 | { | |
2829d9e3 | 901 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 | 902 | { |
ac0d36b5 | 903 | *y = 0; |
96385642 VZ |
904 | *x = pos; |
905 | } | |
906 | else | |
907 | { | |
908 | // index out of bounds | |
7d8268a1 | 909 | return false; |
96385642 | 910 | } |
8bbe427f | 911 | } |
96385642 | 912 | |
7d8268a1 | 913 | return true; |
6de97a3b | 914 | } |
c801d85f | 915 | |
e3ca08dd | 916 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 917 | { |
2830bf19 | 918 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; |
805dd538 | 919 | |
f29a481a | 920 | GtkTextIter iter; |
21d23b88 MR |
921 | if (y >= gtk_text_buffer_get_line_count (m_buffer)) |
922 | return -1; | |
923 | ||
924 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y); | |
925 | if (x >= gtk_text_iter_get_chars_in_line (&iter)) | |
926 | return -1; | |
927 | ||
928 | return gtk_text_iter_get_offset(&iter) + x; | |
6de97a3b | 929 | } |
c801d85f | 930 | |
a81258be | 931 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 932 | { |
f29a481a MR |
933 | if (m_windowStyle & wxTE_MULTILINE) |
934 | { | |
935 | int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1; | |
936 | if (lineNo > last_line) | |
937 | return -1; | |
938 | ||
939 | GtkTextIter iter; | |
940 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo); | |
941 | // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line | |
942 | return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1); | |
943 | } | |
944 | else | |
f29a481a MR |
945 | { |
946 | wxString str = GetLineText (lineNo); | |
8e13c1ec | 947 | return (int) str.length(); |
f29a481a | 948 | } |
6de97a3b | 949 | } |
c801d85f | 950 | |
a81258be | 951 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 952 | { |
e894be20 VZ |
953 | if ( m_windowStyle & wxTE_MULTILINE ) |
954 | { | |
955 | GtkTextIter iter; | |
956 | gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, 0 ); | |
957 | ||
958 | // move forward by one display line until the end is reached | |
959 | int lineCount = 1; | |
960 | while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text), &iter) ) | |
961 | { | |
962 | lineCount++; | |
963 | } | |
964 | ||
965 | // If the last character in the text buffer is a newline, | |
966 | // gtk_text_view_forward_display_line() will return false without that | |
967 | // line being counted. Must add one manually in that case. | |
8e13c1ec | 968 | GtkTextIter lastCharIter; |
e894be20 VZ |
969 | gtk_text_buffer_get_iter_at_offset |
970 | ( | |
971 | m_buffer, | |
972 | &lastCharIter, | |
973 | gtk_text_buffer_get_char_count(m_buffer) - 1 | |
974 | ); | |
975 | gchar lastChar = gtk_text_iter_get_char( &lastCharIter ); | |
976 | if ( lastChar == wxT('\n') ) | |
977 | lineCount++; | |
978 | ||
979 | return lineCount; | |
980 | } | |
981 | else // single line | |
982 | { | |
96385642 | 983 | return 1; |
e894be20 | 984 | } |
6de97a3b | 985 | } |
c801d85f | 986 | |
debe6624 | 987 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 988 | { |
223d09f6 | 989 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3358d36e | 990 | |
74c5a810 | 991 | if ( IsMultiLine() ) |
291a8f20 | 992 | { |
fab591c5 | 993 | GtkTextIter iter; |
41b81aed RR |
994 | gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos ); |
995 | gtk_text_buffer_place_cursor( m_buffer, &iter ); | |
74c5a810 VZ |
996 | gtk_text_view_scroll_mark_onscreen |
997 | ( | |
998 | GTK_TEXT_VIEW(m_text), | |
41b81aed | 999 | gtk_text_buffer_get_insert( m_buffer ) |
74c5a810 | 1000 | ); |
ac0d36b5 | 1001 | } |
2830bf19 | 1002 | else |
291a8f20 | 1003 | { |
6f85e712 | 1004 | // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2? |
afa7bd1e | 1005 | gtk_editable_set_position(GTK_EDITABLE(m_text), int(pos)); |
291a8f20 | 1006 | } |
6de97a3b | 1007 | } |
c801d85f | 1008 | |
03f38c58 | 1009 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 1010 | { |
223d09f6 | 1011 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1012 | |
d59051dd | 1013 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 | 1014 | { |
fab591c5 | 1015 | GtkTextIter end; |
41b81aed RR |
1016 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
1017 | gtk_text_buffer_place_cursor( m_buffer, &end ); | |
fab591c5 | 1018 | } |
d59051dd | 1019 | else |
fab591c5 | 1020 | { |
6f85e712 | 1021 | gtk_editable_set_position( GTK_EDITABLE(m_text), -1 ); |
fab591c5 | 1022 | } |
6de97a3b | 1023 | } |
c801d85f | 1024 | |
debe6624 | 1025 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 1026 | { |
223d09f6 | 1027 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1028 | |
2830bf19 | 1029 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 | 1030 | { |
fab591c5 | 1031 | gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable ); |
fab591c5 | 1032 | } |
2830bf19 | 1033 | else |
fab591c5 | 1034 | { |
6f85e712 | 1035 | gtk_editable_set_editable( GTK_EDITABLE(m_text), editable ); |
fab591c5 | 1036 | } |
6de97a3b | 1037 | } |
c801d85f | 1038 | |
68df5777 RR |
1039 | bool wxTextCtrl::Enable( bool enable ) |
1040 | { | |
1041 | if (!wxWindowBase::Enable(enable)) | |
1042 | { | |
1043 | // nothing to do | |
7d8268a1 | 1044 | return false; |
68df5777 | 1045 | } |
f6bcfd97 | 1046 | |
68df5777 RR |
1047 | if (m_windowStyle & wxTE_MULTILINE) |
1048 | { | |
fab591c5 | 1049 | SetEditable( enable ); |
68df5777 RR |
1050 | } |
1051 | else | |
1052 | { | |
1053 | gtk_widget_set_sensitive( m_text, enable ); | |
1054 | } | |
1055 | ||
7d8268a1 | 1056 | return true; |
68df5777 RR |
1057 | } |
1058 | ||
fdca68a6 JS |
1059 | // wxGTK-specific: called recursively by Enable, |
1060 | // to give widgets an oppprtunity to correct their colours after they | |
1061 | // have been changed by Enable | |
1062 | void wxTextCtrl::OnParentEnable( bool enable ) | |
1063 | { | |
1064 | // If we have a custom background colour, we use this colour in both | |
1065 | // disabled and enabled mode, or we end up with a different colour under the | |
1066 | // text. | |
1067 | wxColour oldColour = GetBackgroundColour(); | |
1068 | if (oldColour.Ok()) | |
1069 | { | |
1070 | // Need to set twice or it'll optimize the useful stuff out | |
1071 | if (oldColour == * wxWHITE) | |
1072 | SetBackgroundColour(*wxBLACK); | |
1073 | else | |
1074 | SetBackgroundColour(*wxWHITE); | |
1075 | SetBackgroundColour(oldColour); | |
1076 | } | |
1077 | } | |
1078 | ||
3a9fa0d6 VZ |
1079 | void wxTextCtrl::MarkDirty() |
1080 | { | |
7d8268a1 | 1081 | m_modified = true; |
3a9fa0d6 VZ |
1082 | } |
1083 | ||
0efe5ba7 VZ |
1084 | void wxTextCtrl::DiscardEdits() |
1085 | { | |
7d8268a1 | 1086 | m_modified = false; |
0efe5ba7 VZ |
1087 | } |
1088 | ||
ce2f50e3 VZ |
1089 | // ---------------------------------------------------------------------------- |
1090 | // max text length support | |
1091 | // ---------------------------------------------------------------------------- | |
1092 | ||
1093 | void wxTextCtrl::IgnoreNextTextUpdate() | |
1094 | { | |
7d8268a1 | 1095 | m_ignoreNextUpdate = true; |
ce2f50e3 VZ |
1096 | } |
1097 | ||
1098 | bool wxTextCtrl::IgnoreTextUpdate() | |
1099 | { | |
1100 | if ( m_ignoreNextUpdate ) | |
1101 | { | |
7d8268a1 | 1102 | m_ignoreNextUpdate = false; |
ce2f50e3 | 1103 | |
7d8268a1 | 1104 | return true; |
ce2f50e3 VZ |
1105 | } |
1106 | ||
7d8268a1 | 1107 | return false; |
ce2f50e3 VZ |
1108 | } |
1109 | ||
d7eee191 VZ |
1110 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1111 | { | |
1112 | if ( !HasFlag(wxTE_MULTILINE) ) | |
1113 | { | |
1114 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
ce2f50e3 VZ |
1115 | |
1116 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
1117 | // we had tried to enter more text than allowed by max text length and | |
1118 | // the text wasn't really changed | |
1119 | // | |
1120 | // to detect this and generate TEXT_MAXLEN event instead of | |
1121 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
1122 | // | |
1123 | // when max len is set to 0 we disconnect our handler as it means that | |
1124 | // we shouldn't check anything any more | |
1125 | if ( len ) | |
1126 | { | |
9fa72bd2 MR |
1127 | g_signal_connect (m_text, "insert_text", |
1128 | G_CALLBACK (gtk_insert_text_callback), this); | |
ce2f50e3 VZ |
1129 | } |
1130 | else // no checking | |
1131 | { | |
9fa72bd2 MR |
1132 | g_signal_handlers_disconnect_by_func (m_text, |
1133 | (gpointer) gtk_insert_text_callback, this); | |
ce2f50e3 | 1134 | } |
d7eee191 VZ |
1135 | } |
1136 | } | |
1137 | ||
debe6624 | 1138 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 1139 | { |
223d09f6 | 1140 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1141 | |
2b5f62a0 VZ |
1142 | if (from == -1 && to == -1) |
1143 | { | |
1144 | from = 0; | |
8e13c1ec | 1145 | to = GetValue().length(); |
2b5f62a0 VZ |
1146 | } |
1147 | ||
fab591c5 RR |
1148 | if (m_windowStyle & wxTE_MULTILINE) |
1149 | { | |
739e366a | 1150 | GtkTextIter fromi, toi; |
41b81aed RR |
1151 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); |
1152 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
739e366a | 1153 | |
41b81aed RR |
1154 | gtk_text_buffer_place_cursor( m_buffer, &toi ); |
1155 | gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi ); | |
fab591c5 RR |
1156 | } |
1157 | else | |
1158 | { | |
1159 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1160 | } | |
6de97a3b | 1161 | } |
c801d85f | 1162 | |
afbe906a | 1163 | void wxTextCtrl::ShowPosition( long pos ) |
c801d85f | 1164 | { |
afbe906a RR |
1165 | if (m_windowStyle & wxTE_MULTILINE) |
1166 | { | |
71aba833 | 1167 | GtkTextIter iter; |
41b81aed | 1168 | gtk_text_buffer_get_start_iter( m_buffer, &iter ); |
71aba833 | 1169 | gtk_text_iter_set_offset( &iter, pos ); |
41b81aed | 1170 | GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE ); |
71aba833 | 1171 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 ); |
afbe906a | 1172 | } |
6de97a3b | 1173 | } |
c801d85f | 1174 | |
692c9b86 VZ |
1175 | wxTextCtrlHitTestResult |
1176 | wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const | |
1177 | { | |
1178 | if ( !IsMultiLine() ) | |
1179 | { | |
1180 | // not supported | |
1181 | return wxTE_HT_UNKNOWN; | |
1182 | } | |
1183 | ||
1184 | int x, y; | |
1185 | gtk_text_view_window_to_buffer_coords | |
1186 | ( | |
1187 | GTK_TEXT_VIEW(m_text), | |
1188 | GTK_TEXT_WINDOW_TEXT, | |
1189 | pt.x, pt.y, | |
1190 | &x, &y | |
1191 | ); | |
1192 | ||
1193 | GtkTextIter iter; | |
1194 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); | |
1195 | if ( pos ) | |
1196 | *pos = gtk_text_iter_get_offset(&iter); | |
1197 | ||
1198 | return wxTE_HT_ON_TEXT; | |
1199 | } | |
1200 | ||
03f38c58 | 1201 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 1202 | { |
223d09f6 | 1203 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1204 | |
fab591c5 RR |
1205 | if (m_windowStyle & wxTE_MULTILINE) |
1206 | { | |
fab591c5 RR |
1207 | // There is no direct accessor for the cursor, but |
1208 | // internally, the cursor is the "mark" called | |
1209 | // "insert" in the text view's btree structure. | |
a8bf1826 | 1210 | |
41b81aed | 1211 | GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer ); |
fab591c5 | 1212 | GtkTextIter cursor; |
41b81aed | 1213 | gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark ); |
a8bf1826 | 1214 | |
fab591c5 RR |
1215 | return gtk_text_iter_get_offset( &cursor ); |
1216 | } | |
1217 | else | |
fab591c5 | 1218 | { |
afa7bd1e | 1219 | return (long) gtk_editable_get_position(GTK_EDITABLE(m_text)); |
fab591c5 | 1220 | } |
6de97a3b | 1221 | } |
c801d85f | 1222 | |
7d8268a1 | 1223 | wxTextPos wxTextCtrl::GetLastPosition() const |
c801d85f | 1224 | { |
223d09f6 | 1225 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1226 | |
2830bf19 | 1227 | int pos = 0; |
a8bf1826 | 1228 | |
2830bf19 | 1229 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 | 1230 | { |
fab591c5 | 1231 | GtkTextIter end; |
41b81aed | 1232 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
a8bf1826 | 1233 | |
fab591c5 | 1234 | pos = gtk_text_iter_get_offset( &end ); |
fab591c5 | 1235 | } |
2830bf19 | 1236 | else |
fab591c5 | 1237 | { |
2830bf19 | 1238 | pos = GTK_ENTRY(m_text)->text_length; |
fab591c5 | 1239 | } |
805dd538 | 1240 | |
ac0d36b5 | 1241 | return (long)pos; |
6de97a3b | 1242 | } |
c801d85f | 1243 | |
debe6624 | 1244 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 1245 | { |
223d09f6 | 1246 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1247 | |
fdd55287 | 1248 | if (m_windowStyle & wxTE_MULTILINE) |
581ee8a9 | 1249 | { |
581ee8a9 | 1250 | GtkTextIter fromi, toi; |
41b81aed RR |
1251 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); |
1252 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
581ee8a9 | 1253 | |
41b81aed | 1254 | gtk_text_buffer_delete( m_buffer, &fromi, &toi ); |
581ee8a9 VZ |
1255 | } |
1256 | else // single line | |
68567a96 | 1257 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 1258 | } |
c801d85f | 1259 | |
debe6624 | 1260 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 1261 | { |
223d09f6 | 1262 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1263 | |
581ee8a9 | 1264 | Remove( from, to ); |
bb69661b | 1265 | |
7d8268a1 | 1266 | if (!value.empty()) |
2df7be7f | 1267 | { |
581ee8a9 VZ |
1268 | SetInsertionPoint( from ); |
1269 | WriteText( value ); | |
2df7be7f | 1270 | } |
6de97a3b | 1271 | } |
c801d85f | 1272 | |
03f38c58 | 1273 | void wxTextCtrl::Cut() |
c801d85f | 1274 | { |
223d09f6 | 1275 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1276 | |
bbde2e29 | 1277 | if (m_windowStyle & wxTE_MULTILINE) |
9fa72bd2 | 1278 | g_signal_emit_by_name (m_text, "cut-clipboard"); |
bbde2e29 | 1279 | else |
afa7bd1e | 1280 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text)); |
6de97a3b | 1281 | } |
c801d85f | 1282 | |
03f38c58 | 1283 | void wxTextCtrl::Copy() |
c801d85f | 1284 | { |
223d09f6 | 1285 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1286 | |
bbde2e29 | 1287 | if (m_windowStyle & wxTE_MULTILINE) |
9fa72bd2 | 1288 | g_signal_emit_by_name (m_text, "copy-clipboard"); |
bbde2e29 | 1289 | else |
afa7bd1e | 1290 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text)); |
6de97a3b | 1291 | } |
c801d85f | 1292 | |
03f38c58 | 1293 | void wxTextCtrl::Paste() |
c801d85f | 1294 | { |
223d09f6 | 1295 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1296 | |
bbde2e29 | 1297 | if (m_windowStyle & wxTE_MULTILINE) |
9fa72bd2 | 1298 | g_signal_emit_by_name (m_text, "paste-clipboard"); |
bbde2e29 | 1299 | else |
afa7bd1e | 1300 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text)); |
6de97a3b | 1301 | } |
c801d85f | 1302 | |
ca8b28f2 JS |
1303 | // Undo/redo |
1304 | void wxTextCtrl::Undo() | |
1305 | { | |
1306 | // TODO | |
223d09f6 | 1307 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
1308 | } |
1309 | ||
1310 | void wxTextCtrl::Redo() | |
1311 | { | |
1312 | // TODO | |
223d09f6 | 1313 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
1314 | } |
1315 | ||
1316 | bool wxTextCtrl::CanUndo() const | |
1317 | { | |
1318 | // TODO | |
4855a477 | 1319 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); |
7d8268a1 | 1320 | return false; |
ca8b28f2 JS |
1321 | } |
1322 | ||
1323 | bool wxTextCtrl::CanRedo() const | |
1324 | { | |
1325 | // TODO | |
4855a477 | 1326 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); |
7d8268a1 | 1327 | return false; |
ca8b28f2 JS |
1328 | } |
1329 | ||
1330 | // If the return values from and to are the same, there is no | |
1331 | // selection. | |
2d4cc5b6 | 1332 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const |
ca8b28f2 | 1333 | { |
223d09f6 | 1334 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
bb69661b | 1335 | |
5b87f8bf RD |
1336 | gint from = -1; |
1337 | gint to = -1; | |
7d8268a1 | 1338 | bool haveSelection = false; |
5b87f8bf | 1339 | |
5b87f8bf RD |
1340 | if (m_windowStyle & wxTE_MULTILINE) |
1341 | { | |
5b87f8bf | 1342 | GtkTextIter ifrom, ito; |
41b81aed | 1343 | if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) ) |
5b87f8bf | 1344 | { |
7d8268a1 | 1345 | haveSelection = true; |
5b87f8bf RD |
1346 | from = gtk_text_iter_get_offset(&ifrom); |
1347 | to = gtk_text_iter_get_offset(&ito); | |
1348 | } | |
1349 | } | |
1350 | else // not multi-line | |
1351 | { | |
1352 | if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text), | |
1353 | &from, &to) ) | |
1354 | { | |
7d8268a1 | 1355 | haveSelection = true; |
5b87f8bf RD |
1356 | } |
1357 | } | |
2d4cc5b6 | 1358 | |
5b87f8bf RD |
1359 | if (! haveSelection ) |
1360 | from = to = GetInsertionPoint(); | |
1361 | ||
1362 | if ( from > to ) | |
1363 | { | |
1364 | // exchange them to be compatible with wxMSW | |
1365 | gint tmp = from; | |
1366 | from = to; | |
1367 | to = tmp; | |
1368 | } | |
bb69661b | 1369 | |
2d4cc5b6 VZ |
1370 | if ( fromOut ) |
1371 | *fromOut = from; | |
1372 | if ( toOut ) | |
1373 | *toOut = to; | |
ca8b28f2 JS |
1374 | } |
1375 | ||
5b87f8bf | 1376 | |
ca8b28f2 JS |
1377 | bool wxTextCtrl::IsEditable() const |
1378 | { | |
7d8268a1 | 1379 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
05060eeb | 1380 | |
fdd55287 VZ |
1381 | if (m_windowStyle & wxTE_MULTILINE) |
1382 | { | |
1383 | return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)); | |
1384 | } | |
1385 | else | |
1386 | { | |
1387 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
1388 | } | |
ca8b28f2 JS |
1389 | } |
1390 | ||
0efe5ba7 VZ |
1391 | bool wxTextCtrl::IsModified() const |
1392 | { | |
1393 | return m_modified; | |
1394 | } | |
1395 | ||
03f38c58 | 1396 | void wxTextCtrl::Clear() |
c801d85f | 1397 | { |
902725ee | 1398 | SetValue( wxEmptyString ); |
6de97a3b | 1399 | } |
c801d85f | 1400 | |
903f689b | 1401 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 1402 | { |
223d09f6 | 1403 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
805dd538 | 1404 | |
8e13c1ec | 1405 | if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER)) |
2830bf19 RR |
1406 | { |
1407 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1408 | event.SetEventObject(this); | |
f6bcfd97 | 1409 | event.SetString(GetValue()); |
2830bf19 RR |
1410 | if (GetEventHandler()->ProcessEvent(event)) return; |
1411 | } | |
903f689b | 1412 | |
12a3f227 | 1413 | if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE)) |
da048e3d | 1414 | { |
2b328fc9 RR |
1415 | // This will invoke the dialog default action, such |
1416 | // as the clicking the default button. | |
a8bf1826 | 1417 | |
da048e3d | 1418 | wxWindow *top_frame = m_parent; |
8487f887 | 1419 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
da048e3d | 1420 | top_frame = top_frame->GetParent(); |
a8bf1826 | 1421 | |
2b328fc9 | 1422 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
da048e3d | 1423 | { |
2b328fc9 RR |
1424 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
1425 | ||
1426 | if (window->default_widget) | |
1427 | { | |
1428 | gtk_widget_activate (window->default_widget); | |
1429 | return; | |
1430 | } | |
13111b2a | 1431 | } |
da048e3d RR |
1432 | } |
1433 | ||
2830bf19 | 1434 | key_event.Skip(); |
6de97a3b | 1435 | } |
c801d85f | 1436 | |
03f38c58 | 1437 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 1438 | { |
ae0bdb01 | 1439 | return GTK_WIDGET(m_text); |
6de97a3b | 1440 | } |
e3e65dac | 1441 | |
903f689b RR |
1442 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
1443 | { | |
ae0bdb01 | 1444 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 | 1445 | { |
fab591c5 | 1446 | return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork |
fab591c5 | 1447 | } |
ae0bdb01 | 1448 | else |
fab591c5 | 1449 | { |
ae0bdb01 | 1450 | return (window == GTK_ENTRY(m_text)->text_area); |
fab591c5 | 1451 | } |
903f689b | 1452 | } |
e3e65dac | 1453 | |
bb69661b VZ |
1454 | // the font will change for subsequent text insertiongs |
1455 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 1456 | { |
7d8268a1 | 1457 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
8bbe427f | 1458 | |
a66954a6 | 1459 | if ( !wxTextCtrlBase::SetFont(font) ) |
bb69661b VZ |
1460 | { |
1461 | // font didn't change, nothing to do | |
7d8268a1 | 1462 | return false; |
bb69661b VZ |
1463 | } |
1464 | ||
1465 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1466 | { | |
7d8268a1 | 1467 | SetUpdateFont(true); |
bb69661b | 1468 | |
1ff4714d VZ |
1469 | m_defaultStyle.SetFont(font); |
1470 | ||
01041145 | 1471 | ChangeFontGlobally(); |
bb69661b VZ |
1472 | } |
1473 | ||
7d8268a1 | 1474 | return true; |
58614078 RR |
1475 | } |
1476 | ||
01041145 VZ |
1477 | void wxTextCtrl::ChangeFontGlobally() |
1478 | { | |
1479 | // this method is very inefficient and hence should be called as rarely as | |
1480 | // possible! | |
c04ec496 VZ |
1481 | // |
1482 | // TODO: it can be implemented much more efficiently for GTK2 | |
22800f32 JS |
1483 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE), |
1484 | _T("shouldn't be called for single line controls") ); | |
01041145 VZ |
1485 | |
1486 | wxString value = GetValue(); | |
7d8268a1 | 1487 | if ( !value.empty() ) |
01041145 | 1488 | { |
7d8268a1 | 1489 | SetUpdateFont(false); |
572aeb77 | 1490 | |
01041145 VZ |
1491 | Clear(); |
1492 | AppendText(value); | |
01041145 VZ |
1493 | } |
1494 | } | |
1495 | ||
17665a2b VZ |
1496 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) |
1497 | { | |
1498 | if ( !wxControl::SetForegroundColour(colour) ) | |
7d8268a1 | 1499 | return false; |
17665a2b VZ |
1500 | |
1501 | // update default fg colour too | |
1502 | m_defaultStyle.SetTextColour(colour); | |
1503 | ||
7d8268a1 | 1504 | return true; |
17665a2b VZ |
1505 | } |
1506 | ||
f03fc89f | 1507 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 1508 | { |
7d8268a1 | 1509 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
a81258be | 1510 | |
1f477433 | 1511 | if ( !wxControl::SetBackgroundColour( colour ) ) |
7d8268a1 | 1512 | return false; |
3358d36e | 1513 | |
f03fc89f | 1514 | if (!m_backgroundColour.Ok()) |
7d8268a1 | 1515 | return false; |
8bbe427f | 1516 | |
17665a2b VZ |
1517 | // change active background color too |
1518 | m_defaultStyle.SetBackgroundColour( colour ); | |
1519 | ||
7d8268a1 | 1520 | return true; |
58614078 RR |
1521 | } |
1522 | ||
eda40bfc | 1523 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) |
17665a2b | 1524 | { |
17665a2b VZ |
1525 | if ( m_windowStyle & wxTE_MULTILINE ) |
1526 | { | |
1527 | if ( style.IsDefault() ) | |
1528 | { | |
1529 | // nothing to do | |
7d8268a1 | 1530 | return true; |
17665a2b | 1531 | } |
902725ee | 1532 | |
41b81aed | 1533 | gint l = gtk_text_buffer_get_char_count( m_buffer ); |
17665a2b | 1534 | |
7d8268a1 | 1535 | wxCHECK_MSG( start >= 0 && end <= l, false, |
cc3da3f8 RR |
1536 | _T("invalid range in wxTextCtrl::SetStyle") ); |
1537 | ||
1538 | GtkTextIter starti, endi; | |
41b81aed RR |
1539 | gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start ); |
1540 | gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end ); | |
cc3da3f8 RR |
1541 | |
1542 | // use the attributes from style which are set in it and fall back | |
1543 | // first to the default style and then to the text control default | |
1544 | // colours for the others | |
1545 | wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this); | |
1546 | ||
41b81aed | 1547 | wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi ); |
902725ee | 1548 | |
7d8268a1 | 1549 | return true; |
17665a2b | 1550 | } |
902725ee WS |
1551 | |
1552 | // else single line | |
1553 | // cannot do this for GTK+'s Entry widget | |
1554 | return false; | |
17665a2b VZ |
1555 | } |
1556 | ||
f40fdaa3 | 1557 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) |
58614078 | 1558 | { |
f40fdaa3 | 1559 | gtk_widget_modify_style(m_text, style); |
68dda785 | 1560 | } |
f96aa4d9 | 1561 | |
e702ff0f JS |
1562 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
1563 | { | |
1564 | Cut(); | |
1565 | } | |
1566 | ||
1567 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1568 | { | |
1569 | Copy(); | |
1570 | } | |
1571 | ||
1572 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1573 | { | |
1574 | Paste(); | |
1575 | } | |
1576 | ||
1577 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1578 | { | |
1579 | Undo(); | |
1580 | } | |
1581 | ||
1582 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1583 | { | |
1584 | Redo(); | |
1585 | } | |
1586 | ||
1587 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1588 | { | |
1589 | event.Enable( CanCut() ); | |
1590 | } | |
1591 | ||
1592 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1593 | { | |
1594 | event.Enable( CanCopy() ); | |
1595 | } | |
1596 | ||
1597 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1598 | { | |
1599 | event.Enable( CanPaste() ); | |
1600 | } | |
1601 | ||
1602 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1603 | { | |
1604 | event.Enable( CanUndo() ); | |
1605 | } | |
1606 | ||
1607 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1608 | { | |
1609 | event.Enable( CanRedo() ); | |
1610 | } | |
65045edd RR |
1611 | |
1612 | void wxTextCtrl::OnInternalIdle() | |
1613 | { | |
d7fa7eaa RR |
1614 | if (g_delayedFocus == this) |
1615 | { | |
1616 | if (GTK_WIDGET_REALIZED(m_widget)) | |
1617 | { | |
1618 | gtk_widget_grab_focus( m_widget ); | |
1619 | g_delayedFocus = NULL; | |
1620 | } | |
1621 | } | |
1622 | ||
e39af974 JS |
1623 | if (wxUpdateUIEvent::CanUpdate(this)) |
1624 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
65045edd | 1625 | } |
f68586e5 VZ |
1626 | |
1627 | wxSize wxTextCtrl::DoGetBestSize() const | |
1628 | { | |
1629 | // FIXME should be different for multi-line controls... | |
0279e844 | 1630 | wxSize ret( wxControl::DoGetBestSize() ); |
9f884528 RD |
1631 | wxSize best(80, ret.y); |
1632 | CacheBestSize(best); | |
1633 | return best; | |
f68586e5 | 1634 | } |
0cc7251e | 1635 | |
9cd6d737 VZ |
1636 | // ---------------------------------------------------------------------------- |
1637 | // freeze/thaw | |
1638 | // ---------------------------------------------------------------------------- | |
1639 | ||
0cc7251e VZ |
1640 | void wxTextCtrl::Freeze() |
1641 | { | |
1642 | if ( HasFlag(wxTE_MULTILINE) ) | |
1643 | { | |
41b81aed RR |
1644 | if ( !m_frozenness++ ) |
1645 | { | |
1646 | // freeze textview updates and remove buffer | |
9fa72bd2 MR |
1647 | g_signal_connect (m_text, "expose_event", |
1648 | G_CALLBACK (gtk_text_exposed_callback), this); | |
1649 | g_signal_connect (m_widget, "expose_event", | |
1650 | G_CALLBACK (gtk_text_exposed_callback), this); | |
41b81aed RR |
1651 | gtk_widget_set_sensitive(m_widget, false); |
1652 | g_object_ref(m_buffer); | |
1653 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL)); | |
0a164d4c | 1654 | } |
41b81aed | 1655 | } |
0cc7251e VZ |
1656 | } |
1657 | ||
1658 | void wxTextCtrl::Thaw() | |
1659 | { | |
1660 | if ( HasFlag(wxTE_MULTILINE) ) | |
1661 | { | |
41b81aed RR |
1662 | wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") ); |
1663 | ||
1664 | if ( !--m_frozenness ) | |
1665 | { | |
1666 | // Reattach buffer and thaw textview updates | |
1667 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer); | |
1668 | g_object_unref(m_buffer); | |
1669 | gtk_widget_set_sensitive(m_widget, true); | |
9fa72bd2 MR |
1670 | g_signal_handlers_disconnect_by_func (m_widget, |
1671 | (gpointer) gtk_text_exposed_callback, this); | |
1672 | g_signal_handlers_disconnect_by_func (m_text, | |
1673 | (gpointer) gtk_text_exposed_callback, this); | |
41b81aed | 1674 | } |
41b81aed | 1675 | } |
0cc7251e | 1676 | } |
9cd6d737 | 1677 | |
9440c3d0 KH |
1678 | // ---------------------------------------------------------------------------- |
1679 | // wxTextUrlEvent passing if style & wxTE_AUTO_URL | |
1680 | // ---------------------------------------------------------------------------- | |
1681 | ||
9440c3d0 KH |
1682 | // FIXME: when dragging on a link the sample gets an "Unknown event". |
1683 | // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or | |
1684 | // a buggy sample, or something else | |
1685 | void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event) | |
1686 | { | |
1687 | event.Skip(); | |
1688 | if(!(m_windowStyle & wxTE_AUTO_URL)) | |
1689 | return; | |
1690 | ||
1691 | gint x, y; | |
1692 | GtkTextIter start, end; | |
1693 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer), | |
1694 | "wxUrl"); | |
1695 | ||
1696 | gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET, | |
1697 | event.GetX(), event.GetY(), &x, &y); | |
1698 | ||
1699 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y); | |
1700 | if (!gtk_text_iter_has_tag(&end, tag)) | |
1701 | { | |
1702 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1703 | GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor); | |
1704 | return; | |
1705 | } | |
1706 | ||
1707 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
1708 | GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor); | |
1709 | ||
1710 | start = end; | |
1711 | if(!gtk_text_iter_begins_tag(&start, tag)) | |
1712 | gtk_text_iter_backward_to_tag_toggle(&start, tag); | |
1713 | if(!gtk_text_iter_ends_tag(&end, tag)) | |
1714 | gtk_text_iter_forward_to_tag_toggle(&end, tag); | |
1715 | ||
1716 | // Native context menu is probably not desired on an URL. | |
1717 | // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value | |
1718 | if(event.GetEventType() == wxEVT_RIGHT_DOWN) | |
1719 | event.Skip(false); | |
1720 | ||
1721 | wxTextUrlEvent url_event(m_windowId, event, | |
1722 | gtk_text_iter_get_offset(&start), | |
1723 | gtk_text_iter_get_offset(&end)); | |
1724 | ||
1725 | InitCommandEvent(url_event); | |
1726 | // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag) | |
1727 | //event.Skip(!GetEventHandler()->ProcessEvent(url_event)); | |
1728 | GetEventHandler()->ProcessEvent(url_event); | |
1729 | } | |
9440c3d0 | 1730 | |
9d522606 RD |
1731 | // static |
1732 | wxVisualAttributes | |
1733 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1734 | { | |
1735 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
1736 | } |