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