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