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