]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "textctrl.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
c801d85f KB |
17 | #include "wx/textctrl.h" |
18 | #include "wx/utils.h" | |
ae0bdb01 | 19 | #include "wx/intl.h" |
a1f79c1e | 20 | #include "wx/log.h" |
ae0bdb01 | 21 | #include "wx/settings.h" |
fc71ef6e | 22 | #include "wx/panel.h" |
fab591c5 | 23 | #include "wx/strconv.h" |
cc3da3f8 | 24 | #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo()) |
c801d85f | 25 | |
a81258be RR |
26 | #include <sys/types.h> |
27 | #include <sys/stat.h> | |
28 | #include <ctype.h> | |
463c4d71 | 29 | #include "wx/math.h" |
a81258be | 30 | |
9e691f46 VZ |
31 | #include "wx/gtk/private.h" |
32 | #include <gdk/gdkkeysyms.h> | |
b292e2f5 | 33 | |
acfd422a RR |
34 | //----------------------------------------------------------------------------- |
35 | // idle system | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern void wxapp_install_idle_handler(); | |
39 | extern bool g_isIdle; | |
40 | ||
b292e2f5 RR |
41 | //----------------------------------------------------------------------------- |
42 | // data | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
65045edd | 45 | extern wxCursor g_globalCursor; |
d7fa7eaa | 46 | extern wxWindowGTK *g_delayedFocus; |
b292e2f5 | 47 | |
eda40bfc VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // helpers | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
cc3da3f8 | 52 | #ifdef __WXGTK20__ |
418cf02e JS |
53 | extern "C" { |
54 | static void wxGtkOnRemoveTag(GtkTextBuffer *buffer, | |
55 | GtkTextTag *tag, | |
56 | GtkTextIter *start, | |
57 | GtkTextIter *end, | |
58 | gpointer user_data) | |
59 | { | |
60 | gchar *name; | |
61 | g_object_get (tag, "name", &name, NULL); | |
62 | ||
63 | if (!name || strncmp(name, "WX", 2)) // anonymous tag or not starting with "WX" | |
64 | g_signal_stop_emission_by_name(buffer, "remove_tag"); | |
65 | ||
66 | g_free(name); | |
67 | } | |
68 | } | |
69 | ||
70 | extern "C" { | |
25497324 KH |
71 | static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer, |
72 | const wxTextAttr& attr, | |
73 | GtkTextIter *start, | |
74 | GtkTextIter *end) | |
75 | { | |
76 | static gchar buf[1024]; | |
77 | GtkTextTag *tag; | |
78 | ||
418cf02e JS |
79 | gulong remove_handler_id = g_signal_connect( text_buffer, "remove_tag", |
80 | G_CALLBACK(wxGtkOnRemoveTag), NULL); | |
81 | gtk_text_buffer_remove_all_tags(text_buffer, start, end); | |
82 | g_signal_handler_disconnect( text_buffer, remove_handler_id ); | |
83 | ||
25497324 KH |
84 | if (attr.HasFont()) |
85 | { | |
86 | char *font_string; | |
87 | PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description; | |
88 | font_string = pango_font_description_to_string(font_description); | |
89 | g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string); | |
90 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
91 | buf ); | |
92 | if (!tag) | |
93 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
94 | "font-desc", font_description, | |
95 | NULL ); | |
96 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
97 | g_free (font_string); | |
98 | } | |
99 | ||
100 | if (attr.HasTextColour()) | |
101 | { | |
102 | GdkColor *colFg = attr.GetTextColour().GetColor(); | |
103 | g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d", | |
104 | colFg->red, colFg->green, colFg->blue); | |
105 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
106 | buf ); | |
107 | if (!tag) | |
108 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
109 | "foreground-gdk", colFg, NULL ); | |
110 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
111 | } | |
112 | ||
113 | if (attr.HasBackgroundColour()) | |
114 | { | |
115 | GdkColor *colBg = attr.GetBackgroundColour().GetColor(); | |
116 | g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d", | |
117 | colBg->red, colBg->green, colBg->blue); | |
118 | tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), | |
119 | buf ); | |
120 | if (!tag) | |
121 | tag = gtk_text_buffer_create_tag( text_buffer, buf, | |
122 | "background-gdk", colBg, NULL ); | |
123 | gtk_text_buffer_apply_tag (text_buffer, tag, start, end); | |
124 | } | |
125 | } | |
418cf02e | 126 | } |
25497324 | 127 | |
418cf02e | 128 | extern "C" { |
cc3da3f8 RR |
129 | static void wxGtkTextInsert(GtkWidget *text, |
130 | GtkTextBuffer *text_buffer, | |
131 | const wxTextAttr& attr, | |
132 | wxCharBuffer buffer) | |
133 | ||
134 | { | |
25497324 KH |
135 | gint start_offset; |
136 | GtkTextIter iter, start; | |
cc3da3f8 | 137 | |
a61bbf87 JS |
138 | gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, |
139 | gtk_text_buffer_get_insert (text_buffer) ); | |
25497324 KH |
140 | start_offset = gtk_text_iter_get_offset (&iter); |
141 | gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) ); | |
142 | ||
143 | gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset); | |
a61bbf87 | 144 | |
25497324 | 145 | wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter); |
cc3da3f8 | 146 | } |
418cf02e | 147 | } |
cc3da3f8 | 148 | #else |
418cf02e | 149 | extern "C" { |
eda40bfc VZ |
150 | static void wxGtkTextInsert(GtkWidget *text, |
151 | const wxTextAttr& attr, | |
152 | const char *txt, | |
153 | size_t len) | |
154 | { | |
155 | GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont() | |
156 | : NULL; | |
157 | ||
158 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
159 | : NULL; | |
160 | ||
161 | GdkColor *colBg = attr.HasBackgroundColour() | |
162 | ? attr.GetBackgroundColour().GetColor() | |
163 | : NULL; | |
164 | ||
165 | gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len ); | |
166 | } | |
418cf02e | 167 | } |
a8bf1826 | 168 | #endif // GTK 1.x |
eda40bfc | 169 | |
ce2f50e3 VZ |
170 | // ---------------------------------------------------------------------------- |
171 | // "insert_text" for GtkEntry | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
865bb325 | 174 | extern "C" { |
ce2f50e3 VZ |
175 | static void |
176 | gtk_insert_text_callback(GtkEditable *editable, | |
177 | const gchar *new_text, | |
178 | gint new_text_length, | |
179 | gint *position, | |
180 | wxTextCtrl *win) | |
181 | { | |
76fcf0f2 RR |
182 | if (g_isIdle) |
183 | wxapp_install_idle_handler(); | |
184 | ||
ce2f50e3 VZ |
185 | // we should only be called if we have a max len limit at all |
186 | GtkEntry *entry = GTK_ENTRY (editable); | |
187 | ||
188 | wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") ); | |
189 | ||
190 | // check that we don't overflow the max length limit | |
191 | // | |
192 | // FIXME: this doesn't work when we paste a string which is going to be | |
193 | // truncated | |
194 | if ( entry->text_length == entry->text_max_length ) | |
195 | { | |
196 | // we don't need to run the base class version at all | |
197 | gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text"); | |
198 | ||
199 | // remember that the next changed signal is to be ignored to avoid | |
200 | // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event | |
201 | win->IgnoreNextTextUpdate(); | |
202 | ||
203 | // and generate the correct one ourselves | |
204 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId()); | |
205 | event.SetEventObject(win); | |
206 | event.SetString(win->GetValue()); | |
207 | win->GetEventHandler()->ProcessEvent( event ); | |
208 | } | |
209 | } | |
865bb325 | 210 | } |
ce2f50e3 | 211 | |
9440c3d0 KH |
212 | #ifdef __WXGTK20__ |
213 | // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp, | |
214 | ||
865bb325 | 215 | extern "C" { |
9440c3d0 KH |
216 | static void |
217 | au_apply_tag_callback(GtkTextBuffer *buffer, | |
218 | GtkTextTag *tag, | |
219 | GtkTextIter *start, | |
220 | GtkTextIter *end, | |
221 | gpointer textctrl) | |
222 | { | |
223 | if(tag == gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl")) | |
224 | g_signal_stop_emission_by_name(buffer, "apply_tag"); | |
225 | } | |
865bb325 | 226 | } |
9440c3d0 KH |
227 | |
228 | //----------------------------------------------------------------------------- | |
229 | // GtkTextCharPredicates for gtk_text_iter_*_find_char | |
230 | //----------------------------------------------------------------------------- | |
231 | ||
865bb325 | 232 | extern "C" { |
9440c3d0 KH |
233 | static gboolean |
234 | pred_whitespace (gunichar ch, gpointer user_data) | |
235 | { | |
236 | return g_unichar_isspace(ch); | |
237 | } | |
865bb325 | 238 | } |
9440c3d0 | 239 | |
865bb325 | 240 | extern "C" { |
9440c3d0 KH |
241 | static gboolean |
242 | pred_non_whitespace (gunichar ch, gpointer user_data) | |
243 | { | |
244 | return !g_unichar_isspace(ch); | |
245 | } | |
865bb325 | 246 | } |
9440c3d0 | 247 | |
865bb325 | 248 | extern "C" { |
9440c3d0 KH |
249 | static gboolean |
250 | pred_nonpunct (gunichar ch, gpointer user_data) | |
251 | { | |
252 | return !g_unichar_ispunct(ch); | |
253 | } | |
865bb325 | 254 | } |
9440c3d0 | 255 | |
865bb325 | 256 | extern "C" { |
9440c3d0 KH |
257 | static gboolean |
258 | pred_nonpunct_or_slash (gunichar ch, gpointer user_data) | |
259 | { | |
260 | return !g_unichar_ispunct(ch) || ch == '/'; | |
261 | } | |
865bb325 | 262 | } |
9440c3d0 KH |
263 | |
264 | //----------------------------------------------------------------------------- | |
265 | // Check for links between s and e and correct tags as necessary | |
266 | //----------------------------------------------------------------------------- | |
267 | ||
268 | // This function should be made match better while being efficient at one point. | |
269 | // Most probably with a row of regular expressions. | |
865bb325 | 270 | extern "C" { |
9440c3d0 KH |
271 | static void |
272 | au_check_word( GtkTextIter *s, GtkTextIter *e ) | |
273 | { | |
274 | static const char *URIPrefixes[] = | |
275 | { | |
276 | "http://", | |
277 | "ftp://", | |
278 | "www.", | |
279 | "ftp.", | |
280 | "mailto://", | |
281 | "https://", | |
282 | "file://", | |
283 | "nntp://", | |
284 | "news://", | |
285 | "telnet://", | |
286 | "mms://", | |
287 | "gopher://", | |
288 | "prospero://", | |
289 | "wais://", | |
290 | }; | |
291 | ||
292 | GtkTextIter start = *s, end = *e; | |
293 | GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s); | |
418cf02e | 294 | |
9440c3d0 KH |
295 | // Get our special link tag |
296 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"); | |
297 | ||
298 | // Get rid of punctuation from beginning and end. | |
299 | // Might want to move this to au_check_range if an improved link checking doesn't | |
300 | // use some intelligent punctuation checking itself (beware of undesired iter modifications). | |
301 | if(g_unichar_ispunct( gtk_text_iter_get_char( &start ) ) ) | |
302 | gtk_text_iter_forward_find_char( &start, pred_nonpunct, NULL, e ); | |
303 | ||
304 | gtk_text_iter_backward_find_char( &end, pred_nonpunct_or_slash, NULL, &start ); | |
305 | gtk_text_iter_forward_char(&end); | |
306 | ||
307 | gchar* text = gtk_text_iter_get_text( &start, &end ); | |
308 | size_t len = strlen(text), prefix_len; | |
309 | size_t n; | |
310 | ||
311 | for( n = 0; n < WXSIZEOF(URIPrefixes); ++n ) | |
312 | { | |
313 | prefix_len = strlen(URIPrefixes[n]); | |
314 | if((len > prefix_len) && !strncasecmp(text, URIPrefixes[n], prefix_len)) | |
315 | break; | |
316 | } | |
317 | ||
318 | if(n < WXSIZEOF(URIPrefixes)) | |
319 | { | |
320 | gulong signal_id = g_signal_handler_find(buffer, | |
321 | (GSignalMatchType) (G_SIGNAL_MATCH_FUNC), | |
322 | 0, 0, NULL, | |
323 | (gpointer)au_apply_tag_callback, NULL); | |
324 | ||
325 | g_signal_handler_block(buffer, signal_id); | |
326 | gtk_text_buffer_apply_tag(buffer, tag, &start, &end); | |
327 | g_signal_handler_unblock(buffer, signal_id); | |
328 | } | |
329 | } | |
865bb325 | 330 | } |
9440c3d0 | 331 | |
865bb325 | 332 | extern "C" { |
9440c3d0 KH |
333 | static void |
334 | au_check_range(GtkTextIter *s, | |
335 | GtkTextIter *range_end) | |
336 | { | |
337 | GtkTextIter range_start = *s; | |
338 | GtkTextIter word_end; | |
339 | GtkTextBuffer *buffer = gtk_text_iter_get_buffer(s); | |
340 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "wxUrl"); | |
341 | ||
342 | gtk_text_buffer_remove_tag(buffer, tag, s, range_end); | |
343 | ||
344 | if(g_unichar_isspace(gtk_text_iter_get_char(&range_start))) | |
345 | gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end); | |
346 | ||
347 | while(!gtk_text_iter_equal(&range_start, range_end)) | |
348 | { | |
349 | word_end = range_start; | |
350 | gtk_text_iter_forward_find_char(&word_end, pred_whitespace, NULL, range_end); | |
351 | ||
352 | // Now we should have a word delimited by range_start and word_end, correct link tags | |
353 | au_check_word(&range_start, &word_end); | |
354 | ||
355 | range_start = word_end; | |
356 | gtk_text_iter_forward_find_char(&range_start, pred_non_whitespace, NULL, range_end); | |
357 | } | |
358 | } | |
865bb325 | 359 | } |
9440c3d0 KH |
360 | |
361 | //----------------------------------------------------------------------------- | |
362 | // "insert-text" for GtkTextBuffer | |
363 | //----------------------------------------------------------------------------- | |
364 | ||
865bb325 | 365 | extern "C" { |
9440c3d0 KH |
366 | static void |
367 | au_insert_text_callback(GtkTextBuffer *buffer, | |
368 | GtkTextIter *end, | |
369 | gchar *text, | |
370 | gint len, | |
371 | wxTextCtrl *win) | |
372 | { | |
373 | if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) | |
374 | return; | |
375 | ||
376 | GtkTextIter start = *end; | |
377 | gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len)); | |
378 | ||
379 | GtkTextIter line_start = start; | |
380 | GtkTextIter line_end = *end; | |
381 | GtkTextIter words_start = start; | |
382 | GtkTextIter words_end = *end; | |
383 | ||
384 | gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(&start)); | |
385 | gtk_text_iter_forward_to_line_end(&line_end); | |
386 | gtk_text_iter_backward_find_char(&words_start, pred_whitespace, NULL, &line_start); | |
387 | gtk_text_iter_forward_find_char(&words_end, pred_whitespace, NULL, &line_end); | |
388 | ||
389 | au_check_range(&words_start, &words_end); | |
390 | } | |
865bb325 | 391 | } |
9440c3d0 KH |
392 | |
393 | //----------------------------------------------------------------------------- | |
394 | // "delete-range" for GtkTextBuffer | |
395 | //----------------------------------------------------------------------------- | |
396 | ||
865bb325 | 397 | extern "C" { |
9440c3d0 KH |
398 | static void |
399 | au_delete_range_callback(GtkTextBuffer *buffer, | |
400 | GtkTextIter *start, | |
401 | GtkTextIter *end, | |
402 | wxTextCtrl *win) | |
403 | { | |
404 | if( !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) | |
405 | return; | |
406 | ||
407 | GtkTextIter line_start = *start, line_end = *end; | |
408 | ||
409 | gtk_text_iter_set_line(&line_start, gtk_text_iter_get_line(start)); | |
410 | gtk_text_iter_forward_to_line_end(&line_end); | |
411 | gtk_text_iter_backward_find_char(start, pred_whitespace, NULL, &line_start); | |
412 | gtk_text_iter_forward_find_char(end, pred_whitespace, NULL, &line_end); | |
413 | ||
414 | au_check_range(start, end); | |
415 | } | |
865bb325 | 416 | } |
9440c3d0 KH |
417 | |
418 | ||
419 | #endif | |
420 | ||
c801d85f | 421 | //----------------------------------------------------------------------------- |
2f2aa628 | 422 | // "changed" |
c801d85f KB |
423 | //----------------------------------------------------------------------------- |
424 | ||
865bb325 | 425 | extern "C" { |
805dd538 | 426 | static void |
ba3f6b44 | 427 | gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win ) |
484e45bf | 428 | { |
ce2f50e3 VZ |
429 | if ( win->IgnoreTextUpdate() ) |
430 | return; | |
431 | ||
a2053b27 | 432 | if (!win->m_hasVMT) return; |
805dd538 | 433 | |
bb69661b | 434 | if (g_isIdle) |
3c679789 | 435 | wxapp_install_idle_handler(); |
a8bf1826 | 436 | |
034be888 | 437 | win->SetModified(); |
c04ec496 | 438 | #ifndef __WXGTK20__ |
01041145 | 439 | win->UpdateFontIfNeeded(); |
c04ec496 | 440 | #endif // !__WXGTK20__ |
a8bf1826 | 441 | |
f03fc89f | 442 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); |
2830bf19 RR |
443 | event.SetEventObject( win ); |
444 | win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 445 | } |
865bb325 | 446 | } |
112892b9 | 447 | |
41b81aed RR |
448 | //----------------------------------------------------------------------------- |
449 | // "expose_event" from scrolled window and textview | |
450 | //----------------------------------------------------------------------------- | |
451 | ||
452 | #ifdef __WXGTK20__ | |
865bb325 | 453 | extern "C" { |
41b81aed RR |
454 | static gboolean |
455 | gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win ) | |
456 | { | |
457 | return TRUE; | |
458 | } | |
865bb325 | 459 | } |
41b81aed RR |
460 | #endif |
461 | ||
2830bf19 | 462 | //----------------------------------------------------------------------------- |
034be888 | 463 | // "changed" from vertical scrollbar |
2830bf19 RR |
464 | //----------------------------------------------------------------------------- |
465 | ||
fab591c5 | 466 | #ifndef __WXGTK20__ |
865bb325 | 467 | extern "C" { |
805dd538 | 468 | static void |
034be888 | 469 | gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
2830bf19 | 470 | { |
3c679789 | 471 | if (!win->m_hasVMT) return; |
bb69661b VZ |
472 | |
473 | if (g_isIdle) | |
3c679789 | 474 | wxapp_install_idle_handler(); |
acfd422a | 475 | |
2830bf19 RR |
476 | win->CalculateScrollbar(); |
477 | } | |
865bb325 | 478 | } |
fab591c5 | 479 | #endif |
2830bf19 | 480 | |
1c35b54e VZ |
481 | // ---------------------------------------------------------------------------- |
482 | // redraw callback for multiline text | |
483 | // ---------------------------------------------------------------------------- | |
484 | ||
485 | #ifndef __WXGTK20__ | |
486 | ||
487 | // redrawing a GtkText from inside a wxYield() call results in crashes (the | |
488 | // text sample shows it in its "Add lines" command which shows wxProgressDialog | |
489 | // which implicitly calls wxYield()) so we override GtkText::draw() and simply | |
490 | // don't do anything if we're inside wxYield() | |
491 | ||
492 | extern bool wxIsInsideYield; | |
493 | ||
2e08b1a3 VZ |
494 | extern "C" { |
495 | typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect); | |
496 | } | |
1c35b54e VZ |
497 | |
498 | static GtkDrawCallback gs_gtk_text_draw = NULL; | |
499 | ||
865bb325 VZ |
500 | extern "C" { |
501 | static void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect) | |
1c35b54e VZ |
502 | { |
503 | if ( !wxIsInsideYield ) | |
504 | { | |
505 | wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw, | |
506 | _T("infinite recursion in wxgtk_text_draw aborted") ); | |
507 | ||
508 | gs_gtk_text_draw(widget, rect); | |
509 | } | |
510 | } | |
865bb325 | 511 | } |
1c35b54e VZ |
512 | |
513 | #endif // __WXGTK20__ | |
514 | ||
2f2aa628 RR |
515 | //----------------------------------------------------------------------------- |
516 | // wxTextCtrl | |
517 | //----------------------------------------------------------------------------- | |
518 | ||
519 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
520 | ||
c801d85f | 521 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
2830bf19 | 522 | EVT_CHAR(wxTextCtrl::OnChar) |
e702ff0f JS |
523 | |
524 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
525 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
526 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
527 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
528 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
529 | ||
530 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
531 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
532 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
533 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
534 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
9440c3d0 KH |
535 | |
536 | #ifdef __WXGTK20__ | |
537 | // wxTE_AUTO_URL wxTextUrl support. Currently only creates | |
538 | // wxTextUrlEvent in the same cases as wxMSW, more can be added here. | |
539 | EVT_MOTION (wxTextCtrl::OnUrlMouseEvent) | |
540 | EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
541 | EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent) | |
542 | EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent) | |
543 | EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent) | |
544 | EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent) | |
545 | EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent) | |
546 | #endif | |
c801d85f KB |
547 | END_EVENT_TABLE() |
548 | ||
01041145 | 549 | void wxTextCtrl::Init() |
f5abe911 | 550 | { |
ce2f50e3 | 551 | m_ignoreNextUpdate = |
7d8268a1 WS |
552 | m_modified = false; |
553 | SetUpdateFont(false); | |
3ca6a5f0 BP |
554 | m_text = |
555 | m_vScrollbar = (GtkWidget *)NULL; | |
41b81aed RR |
556 | #ifdef __WXGTK20__ |
557 | m_frozenness = 0; | |
9440c3d0 KH |
558 | m_gdkHandCursor = NULL; |
559 | m_gdkXTermCursor = NULL; | |
560 | #endif | |
561 | } | |
562 | ||
563 | wxTextCtrl::~wxTextCtrl() | |
564 | { | |
565 | #ifdef __WXGTK20__ | |
566 | if(m_gdkHandCursor) | |
567 | gdk_cursor_unref(m_gdkHandCursor); | |
568 | if(m_gdkXTermCursor) | |
569 | gdk_cursor_unref(m_gdkXTermCursor); | |
7d8268a1 | 570 | #endif |
f5abe911 | 571 | } |
13289f04 | 572 | |
13111b2a VZ |
573 | wxTextCtrl::wxTextCtrl( wxWindow *parent, |
574 | wxWindowID id, | |
575 | const wxString &value, | |
576 | const wxPoint &pos, | |
577 | const wxSize &size, | |
578 | long style, | |
579 | const wxValidator& validator, | |
580 | const wxString &name ) | |
f5abe911 | 581 | { |
01041145 VZ |
582 | Init(); |
583 | ||
f5abe911 RR |
584 | Create( parent, id, value, pos, size, style, validator, name ); |
585 | } | |
c801d85f | 586 | |
13111b2a VZ |
587 | bool wxTextCtrl::Create( wxWindow *parent, |
588 | wxWindowID id, | |
589 | const wxString &value, | |
590 | const wxPoint &pos, | |
591 | const wxSize &size, | |
592 | long style, | |
593 | const wxValidator& validator, | |
594 | const wxString &name ) | |
c801d85f | 595 | { |
7d8268a1 WS |
596 | m_needParent = true; |
597 | m_acceptsFocus = true; | |
484e45bf | 598 | |
4dcaf11a RR |
599 | if (!PreCreation( parent, pos, size ) || |
600 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
601 | { | |
223d09f6 | 602 | wxFAIL_MSG( wxT("wxTextCtrl creation failed") ); |
7d8268a1 | 603 | return false; |
4dcaf11a | 604 | } |
6de97a3b | 605 | |
805dd538 | 606 | |
7d8268a1 | 607 | m_vScrollbarVisible = false; |
13289f04 | 608 | |
2830bf19 | 609 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
a8bf1826 | 610 | |
ab46dc18 | 611 | if (multi_line) |
2830bf19 | 612 | { |
fab591c5 RR |
613 | #ifdef __WXGTK20__ |
614 | // Create view | |
615 | m_text = gtk_text_view_new(); | |
a8bf1826 | 616 | |
41b81aed | 617 | m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
a8bf1826 | 618 | |
fab591c5 RR |
619 | // create scrolled window |
620 | m_widget = gtk_scrolled_window_new( NULL, NULL ); | |
621 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ), | |
622 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
a8bf1826 | 623 | |
fab591c5 RR |
624 | // Insert view into scrolled window |
625 | gtk_container_add( GTK_CONTAINER(m_widget), m_text ); | |
a8bf1826 | 626 | |
c2110823 | 627 | // translate wx wrapping style to GTK+ |
9ddb3948 | 628 | GtkWrapMode wrap; |
c2110823 | 629 | if ( HasFlag( wxTE_DONTWRAP ) ) |
9ddb3948 | 630 | wrap = GTK_WRAP_NONE; |
c4590236 | 631 | else if ( HasFlag( wxTE_CHARWRAP ) ) |
9ddb3948 | 632 | wrap = GTK_WRAP_CHAR; |
c4590236 | 633 | else if ( HasFlag( wxTE_WORDWRAP ) ) |
9ddb3948 | 634 | wrap = GTK_WRAP_WORD; |
c4590236 VZ |
635 | else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0 |
636 | { | |
637 | // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4 | |
638 | #ifdef __WXGTK24__ | |
c79146df VZ |
639 | if ( !gtk_check_version(2,4,0) ) |
640 | { | |
641 | wrap = GTK_WRAP_WORD_CHAR; | |
642 | } | |
643 | else | |
c4590236 | 644 | #endif |
34b8f3ef | 645 | wrap = GTK_WRAP_WORD; |
c4590236 | 646 | } |
9ddb3948 VZ |
647 | |
648 | gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap ); | |
805dd538 | 649 | |
fab591c5 RR |
650 | if (!HasFlag(wxNO_BORDER)) |
651 | gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN ); | |
7d8268a1 | 652 | |
e327fddf KH |
653 | gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK ); |
654 | ||
055e633d | 655 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
c2110823 | 656 | #else // GTK+ 1 |
fab591c5 | 657 | // create our control ... |
2830bf19 RR |
658 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); |
659 | ||
fab591c5 | 660 | // ... and put into the upper left hand corner of the table |
7d8268a1 | 661 | bool bHasHScrollbar = false; |
2830bf19 | 662 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); |
5664fc32 | 663 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
2830bf19 | 664 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, |
41dee9d0 | 665 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
f5368809 RR |
666 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
667 | 0, 0); | |
bb69661b | 668 | |
fab591c5 | 669 | // always wrap words |
5c387335 | 670 | gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE ); |
bb69661b | 671 | |
fab591c5 | 672 | // finally, put the vertical scrollbar in the upper right corner |
ab46dc18 RR |
673 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); |
674 | GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS ); | |
ab46dc18 RR |
675 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, |
676 | GTK_FILL, | |
677 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), | |
678 | 0, 0); | |
c2110823 | 679 | #endif // GTK+ 2/1 |
2830bf19 RR |
680 | } |
681 | else | |
682 | { | |
fab591c5 | 683 | // a single-line text control: no need for scrollbars |
2830bf19 | 684 | m_widget = |
1c35b54e | 685 | m_text = gtk_entry_new(); |
44c5573d | 686 | |
7d8268a1 | 687 | #ifdef __WXGTK20__ |
02a6e358 RR |
688 | if (style & wxNO_BORDER) |
689 | g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL ); | |
44c5573d | 690 | #endif |
2830bf19 | 691 | } |
484e45bf | 692 | |
db434467 | 693 | m_parent->DoAddChild( this ); |
eda40bfc | 694 | |
76fcf0f2 | 695 | m_focusWidget = m_text; |
db434467 | 696 | |
abdeb9e7 | 697 | PostCreation(size); |
484e45bf | 698 | |
2830bf19 | 699 | if (multi_line) |
2830bf19 | 700 | gtk_widget_show(m_text); |
13289f04 | 701 | |
fab591c5 | 702 | #ifndef __WXGTK20__ |
ab46dc18 RR |
703 | if (multi_line) |
704 | { | |
705 | gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed", | |
706 | (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this ); | |
1c35b54e | 707 | |
1c35b54e VZ |
708 | // only initialize gs_gtk_text_draw once, starting from the next the |
709 | // klass::draw will already be wxgtk_text_draw | |
710 | if ( !gs_gtk_text_draw ) | |
711 | { | |
712 | GtkDrawCallback& | |
713 | draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw; | |
714 | ||
715 | gs_gtk_text_draw = draw; | |
716 | ||
717 | draw = wxgtk_text_draw; | |
718 | } | |
ab46dc18 | 719 | } |
fab591c5 | 720 | #endif // GTK+ 1.x |
3358d36e | 721 | |
7d8268a1 | 722 | if (!value.empty()) |
2830bf19 | 723 | { |
fab591c5 RR |
724 | #ifdef __WXGTK20__ |
725 | SetValue( value ); | |
726 | #else | |
3358d36e | 727 | |
9e691f46 | 728 | #if !GTK_CHECK_VERSION(1, 2, 0) |
3358d36e VZ |
729 | // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in |
730 | // gtk_editable_insert_text() | |
731 | gtk_widget_realize(m_text); | |
732 | #endif // GTK 1.0 | |
733 | ||
fab591c5 | 734 | gint tmp = 0; |
05939a81 | 735 | #if wxUSE_UNICODE |
3358d36e | 736 | wxWX2MBbuf val = value.mbc_str(); |
05939a81 | 737 | gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp ); |
fab591c5 | 738 | #else |
2830bf19 | 739 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); |
fab591c5 | 740 | #endif |
3358d36e | 741 | |
291a8f20 RR |
742 | if (multi_line) |
743 | { | |
fab591c5 | 744 | // Bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 745 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
3358d36e | 746 | } |
a8bf1826 | 747 | |
fab591c5 | 748 | #endif |
2830bf19 | 749 | } |
484e45bf | 750 | |
2830bf19 RR |
751 | if (style & wxTE_PASSWORD) |
752 | { | |
753 | if (!multi_line) | |
754 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
755 | } | |
8bbe427f | 756 | |
2830bf19 RR |
757 | if (style & wxTE_READONLY) |
758 | { | |
759 | if (!multi_line) | |
760 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
fab591c5 RR |
761 | #ifdef __WXGTK20__ |
762 | else | |
98a8daf4 VS |
763 | gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE); |
764 | #else | |
765 | } | |
766 | else | |
767 | { | |
768 | if (multi_line) | |
769 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
770 | #endif | |
771 | } | |
772 | ||
c663fbea VS |
773 | #ifdef __WXGTK20__ |
774 | if (multi_line) | |
775 | { | |
776 | if (style & wxTE_RIGHT) | |
777 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT ); | |
778 | else if (style & wxTE_CENTRE) | |
779 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER ); | |
780 | // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT | |
781 | } | |
c663fbea VS |
782 | else |
783 | { | |
77f70672 RR |
784 | #ifdef __WXGTK24__ |
785 | // gtk_entry_set_alignment was introduced in gtk+-2.3.5 | |
786 | if (!gtk_check_version(2,4,0)) | |
787 | { | |
788 | if (style & wxTE_RIGHT) | |
789 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 ); | |
790 | else if (style & wxTE_CENTRE) | |
791 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 ); | |
792 | } | |
793 | #endif | |
c663fbea | 794 | } |
c663fbea | 795 | #endif // __WXGTK20__ |
7d8268a1 | 796 | |
fab591c5 RR |
797 | // We want to be notified about text changes. |
798 | #ifdef __WXGTK20__ | |
799 | if (multi_line) | |
800 | { | |
41b81aed | 801 | g_signal_connect( G_OBJECT(m_buffer), "changed", |
fab591c5 | 802 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); |
9440c3d0 KH |
803 | |
804 | // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style | |
805 | if (style & wxTE_AUTO_URL) | |
806 | { | |
807 | GtkTextIter start, end; | |
808 | m_gdkHandCursor = gdk_cursor_new(GDK_HAND2); | |
809 | m_gdkXTermCursor = gdk_cursor_new(GDK_XTERM); | |
810 | ||
811 | // We create our wxUrl tag here for slight efficiency gain - we | |
812 | // don't have to check for the tag existance in callbacks, | |
813 | // hereby it's guaranteed to exist. | |
814 | gtk_text_buffer_create_tag(m_buffer, "wxUrl", | |
815 | "foreground", "blue", | |
816 | "underline", PANGO_UNDERLINE_SINGLE, | |
817 | NULL); | |
818 | ||
819 | // Check for URLs after each text change | |
820 | g_signal_connect_after( G_OBJECT(m_buffer), "insert_text", | |
821 | GTK_SIGNAL_FUNC(au_insert_text_callback), (gpointer)this); | |
822 | g_signal_connect_after( G_OBJECT(m_buffer), "delete_range", | |
823 | GTK_SIGNAL_FUNC(au_delete_range_callback), (gpointer)this); | |
824 | ||
825 | // Block all wxUrl tag applying unless we do it ourselves, in which case we | |
826 | // block this callback temporarily. This takes care of gtk+ internal | |
827 | // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise, | |
828 | // which is undesired because only a part of the URL might be copied. | |
829 | // The insert-text signal emitted inside it will take care of newly formed | |
830 | // or wholly copied URLs. | |
831 | g_signal_connect( G_OBJECT(m_buffer), "apply_tag", | |
832 | GTK_SIGNAL_FUNC(au_apply_tag_callback), NULL); | |
833 | ||
834 | // Check for URLs in the initial string passed to Create | |
835 | gtk_text_buffer_get_start_iter(m_buffer, &start); | |
836 | gtk_text_buffer_get_end_iter(m_buffer, &end); | |
837 | au_check_range(&start, &end); | |
838 | } | |
fab591c5 RR |
839 | } |
840 | else | |
841 | #endif | |
7d8268a1 | 842 | |
fab591c5 | 843 | { |
055e633d RR |
844 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
845 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
fab591c5 | 846 | } |
ce16e5d7 | 847 | |
65045edd | 848 | m_cursor = wxCursor( wxCURSOR_IBEAM ); |
13111b2a | 849 | |
9d522606 | 850 | wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont()); |
17665a2b VZ |
851 | SetDefaultStyle( attrDef ); |
852 | ||
7d8268a1 | 853 | return true; |
2830bf19 | 854 | } |
484e45bf | 855 | |
9d522606 | 856 | |
2830bf19 RR |
857 | void wxTextCtrl::CalculateScrollbar() |
858 | { | |
fab591c5 | 859 | #ifndef __WXGTK20__ |
2830bf19 | 860 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; |
f96aa4d9 | 861 | |
2830bf19 | 862 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; |
805dd538 | 863 | |
2830bf19 RR |
864 | if (adj->upper - adj->page_size < 0.8) |
865 | { | |
866 | if (m_vScrollbarVisible) | |
867 | { | |
034be888 | 868 | gtk_widget_hide( m_vScrollbar ); |
7d8268a1 | 869 | m_vScrollbarVisible = false; |
2830bf19 RR |
870 | } |
871 | } | |
872 | else | |
873 | { | |
874 | if (!m_vScrollbarVisible) | |
875 | { | |
034be888 | 876 | gtk_widget_show( m_vScrollbar ); |
7d8268a1 | 877 | m_vScrollbarVisible = true; |
2830bf19 RR |
878 | } |
879 | } | |
fab591c5 | 880 | #endif |
6de97a3b | 881 | } |
c801d85f | 882 | |
03f38c58 | 883 | wxString wxTextCtrl::GetValue() const |
c801d85f | 884 | { |
902725ee | 885 | wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") ); |
8bbe427f | 886 | |
2830bf19 RR |
887 | wxString tmp; |
888 | if (m_windowStyle & wxTE_MULTILINE) | |
889 | { | |
fab591c5 | 890 | #ifdef __WXGTK20__ |
fab591c5 | 891 | GtkTextIter start; |
41b81aed | 892 | gtk_text_buffer_get_start_iter( m_buffer, &start ); |
fab591c5 | 893 | GtkTextIter end; |
41b81aed RR |
894 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
895 | gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE ); | |
5b87f8bf | 896 | |
fab591c5 RR |
897 | #if wxUSE_UNICODE |
898 | wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) ); | |
899 | #else | |
900 | wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) ); | |
901 | #endif | |
39ccbf9a VZ |
902 | if ( buffer ) |
903 | tmp = buffer; | |
a8bf1826 | 904 | |
fab591c5 RR |
905 | g_free( text ); |
906 | #else | |
2830bf19 RR |
907 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
908 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
fab591c5 | 909 | tmp = text; |
2830bf19 | 910 | g_free( text ); |
fab591c5 | 911 | #endif |
2830bf19 RR |
912 | } |
913 | else | |
914 | { | |
fab591c5 | 915 | tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) ); |
2830bf19 | 916 | } |
a8bf1826 | 917 | |
2830bf19 | 918 | return tmp; |
6de97a3b | 919 | } |
c801d85f KB |
920 | |
921 | void wxTextCtrl::SetValue( const wxString &value ) | |
922 | { | |
223d09f6 | 923 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 924 | |
2830bf19 RR |
925 | if (m_windowStyle & wxTE_MULTILINE) |
926 | { | |
fab591c5 RR |
927 | #ifdef __WXGTK20__ |
928 | ||
929 | #if wxUSE_UNICODE | |
930 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) ); | |
931 | #else | |
932 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) ); | |
a8bf1826 | 933 | #endif |
b6ae8db8 | 934 | if (gtk_text_buffer_get_char_count(m_buffer) != 0) |
ad68ed32 RR |
935 | IgnoreNextTextUpdate(); |
936 | ||
430ae62e JS |
937 | if ( !buffer ) |
938 | { | |
939 | // what else can we do? at least don't crash... | |
940 | return; | |
941 | } | |
418cf02e | 942 | |
41b81aed | 943 | gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) ); |
a8bf1826 | 944 | |
fab591c5 | 945 | #else |
2830bf19 RR |
946 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
947 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
948 | len = 0; | |
01041145 | 949 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len ); |
05939a81 | 950 | #endif |
2830bf19 RR |
951 | } |
952 | else | |
953 | { | |
fab591c5 | 954 | gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) ); |
2830bf19 | 955 | } |
f6bcfd97 BP |
956 | |
957 | // GRG, Jun/2000: Changed this after a lot of discussion in | |
77ffb593 | 958 | // the lists. wxWidgets 2.2 will have a set of flags to |
f6bcfd97 BP |
959 | // customize this behaviour. |
960 | SetInsertionPoint(0); | |
a8bf1826 | 961 | |
7d8268a1 | 962 | m_modified = false; |
6de97a3b | 963 | } |
c801d85f KB |
964 | |
965 | void wxTextCtrl::WriteText( const wxString &text ) | |
966 | { | |
223d09f6 | 967 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 968 | |
17665a2b VZ |
969 | if ( text.empty() ) |
970 | return; | |
484e45bf | 971 | |
c04ec496 VZ |
972 | // gtk_text_changed_callback() will set m_modified to true but m_modified |
973 | // shouldn't be changed by the program writing to the text control itself, | |
974 | // so save the old value and restore when we're done | |
975 | bool oldModified = m_modified; | |
976 | ||
17665a2b | 977 | if ( m_windowStyle & wxTE_MULTILINE ) |
2830bf19 | 978 | { |
fab591c5 RR |
979 | #ifdef __WXGTK20__ |
980 | ||
981 | #if wxUSE_UNICODE | |
982 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) ); | |
983 | #else | |
984 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) ); | |
a8bf1826 | 985 | #endif |
8e033d81 VZ |
986 | if ( !buffer ) |
987 | { | |
988 | // what else can we do? at least don't crash... | |
989 | return; | |
990 | } | |
991 | ||
e136b747 | 992 | // TODO: Call whatever is needed to delete the selection. |
41b81aed | 993 | wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer ); |
a8bf1826 | 994 | |
71aba833 VZ |
995 | GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) ); |
996 | // Scroll to cursor, but only if scrollbar thumb is at the very bottom | |
997 | if ( adj->value == adj->upper - adj->page_size ) | |
998 | { | |
999 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), | |
41b81aed | 1000 | gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 ); |
71aba833 | 1001 | } |
a8bf1826 | 1002 | #else // GTK 1.x |
ba3f6b44 | 1003 | // After cursor movements, gtk_text_get_point() is wrong by one. |
9e691f46 | 1004 | gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) ); |
eda40bfc | 1005 | |
41b2e0e6 VZ |
1006 | // always use m_defaultStyle, even if it is empty as otherwise |
1007 | // resetting the style and appending some more text wouldn't work: if | |
1008 | // we don't specify the style explicitly, the old style would be used | |
2b5f62a0 | 1009 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); |
fab591c5 | 1010 | wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len()); |
eda40bfc | 1011 | |
07e9d4f0 VZ |
1012 | // we called wxGtkTextInsert with correct font, no need to do anything |
1013 | // in UpdateFontIfNeeded() any longer | |
1014 | if ( !text.empty() ) | |
1015 | { | |
7d8268a1 | 1016 | SetUpdateFont(false); |
07e9d4f0 VZ |
1017 | } |
1018 | ||
ba3f6b44 | 1019 | // Bring editable's cursor back uptodate. |
9e691f46 | 1020 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
a8bf1826 | 1021 | #endif // GTK 1.x/2.0 |
2830bf19 | 1022 | } |
17665a2b | 1023 | else // single line |
2830bf19 | 1024 | { |
2b5f62a0 VZ |
1025 | // First remove the selection if there is one |
1026 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); | |
1027 | ||
ba3f6b44 | 1028 | // This moves the cursor pos to behind the inserted text. |
9e691f46 | 1029 | gint len = GET_EDITABLE_POS(m_text); |
a8bf1826 | 1030 | |
fab591c5 RR |
1031 | #ifdef __WXGTK20__ |
1032 | ||
1033 | #if wxUSE_UNICODE | |
1034 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) ); | |
1035 | #else | |
1036 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) ); | |
a8bf1826 | 1037 | #endif |
82b76851 JS |
1038 | if ( !buffer ) |
1039 | { | |
1040 | // what else can we do? at least don't crash... | |
1041 | return; | |
1042 | } | |
1043 | ||
fab591c5 | 1044 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len ); |
a8bf1826 | 1045 | |
fab591c5 RR |
1046 | #else |
1047 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len ); | |
1048 | #endif | |
3358d36e | 1049 | |
ba3f6b44 | 1050 | // Bring entry's cursor uptodate. |
9e691f46 | 1051 | gtk_entry_set_position( GTK_ENTRY(m_text), len ); |
2830bf19 | 1052 | } |
eda40bfc | 1053 | |
c04ec496 | 1054 | m_modified = oldModified; |
6de97a3b | 1055 | } |
c801d85f | 1056 | |
a6e21573 HH |
1057 | void wxTextCtrl::AppendText( const wxString &text ) |
1058 | { | |
ba3f6b44 RR |
1059 | SetInsertionPointEnd(); |
1060 | WriteText( text ); | |
1061 | } | |
2df7be7f | 1062 | |
ba3f6b44 RR |
1063 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
1064 | { | |
a6e21573 HH |
1065 | if (m_windowStyle & wxTE_MULTILINE) |
1066 | { | |
fab591c5 | 1067 | #ifndef __WXGTK20__ |
ba3f6b44 RR |
1068 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
1069 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
bb69661b | 1070 | |
ba3f6b44 RR |
1071 | if (text) |
1072 | { | |
902725ee | 1073 | wxString buf; |
ba3f6b44 RR |
1074 | long i; |
1075 | int currentLine = 0; | |
1076 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
1077 | if (text[i] == '\n') | |
1078 | currentLine++; | |
1079 | // Now get the text | |
1080 | int j; | |
1081 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
1082 | buf += text[i]; | |
17665a2b | 1083 | |
ba3f6b44 RR |
1084 | g_free( text ); |
1085 | return buf; | |
bb69661b | 1086 | } |
ba3f6b44 | 1087 | else |
bb69661b | 1088 | { |
ba3f6b44 | 1089 | return wxEmptyString; |
bb69661b | 1090 | } |
905f2110 | 1091 | #else |
905f2110 | 1092 | GtkTextIter line; |
41b81aed | 1093 | gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo); |
8c6785f0 MR |
1094 | GtkTextIter end = line; |
1095 | gtk_text_iter_forward_to_line_end(&end); | |
41b81aed | 1096 | gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE); |
58192970 | 1097 | wxString result(wxGTK_CONV_BACK(text)); |
905f2110 | 1098 | g_free(text); |
8c6785f0 | 1099 | return result; |
905f2110 | 1100 | #endif |
a6e21573 | 1101 | } |
ba3f6b44 | 1102 | else |
a81258be | 1103 | { |
ba3f6b44 RR |
1104 | if (lineNo == 0) return GetValue(); |
1105 | return wxEmptyString; | |
a81258be | 1106 | } |
6de97a3b | 1107 | } |
c801d85f | 1108 | |
a81258be RR |
1109 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
1110 | { | |
ac0d36b5 HH |
1111 | /* If you implement this, don't forget to update the documentation! |
1112 | * (file docs/latex/wx/text.tex) */ | |
223d09f6 | 1113 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); |
a81258be | 1114 | } |
112892b9 | 1115 | |
0efe5ba7 | 1116 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 1117 | { |
96385642 | 1118 | if ( m_windowStyle & wxTE_MULTILINE ) |
805dd538 | 1119 | { |
f29a481a MR |
1120 | #ifdef __WXGTK20__ |
1121 | GtkTextIter iter; | |
1122 | gtk_text_buffer_get_iter_at_offset(m_buffer, &iter, pos); | |
1123 | if (gtk_text_iter_is_end(&iter)) | |
1124 | return false; | |
1125 | ||
1126 | *y = gtk_text_iter_get_line(&iter); | |
1127 | *x = gtk_text_iter_get_line_offset(&iter); | |
1128 | #else | |
96385642 VZ |
1129 | wxString text = GetValue(); |
1130 | ||
6085b116 | 1131 | // cast to prevent warning. But pos really should've been unsigned. |
2829d9e3 | 1132 | if( (unsigned long)pos > text.Len() ) |
7d8268a1 | 1133 | return false; |
96385642 | 1134 | |
ac0d36b5 HH |
1135 | *x=0; // First Col |
1136 | *y=0; // First Line | |
2829d9e3 | 1137 | |
05939a81 OK |
1138 | const wxChar* stop = text.c_str() + pos; |
1139 | for ( const wxChar *p = text.c_str(); p < stop; p++ ) | |
96385642 | 1140 | { |
223d09f6 | 1141 | if (*p == wxT('\n')) |
96385642 VZ |
1142 | { |
1143 | (*y)++; | |
ac0d36b5 | 1144 | *x=0; |
96385642 VZ |
1145 | } |
1146 | else | |
1147 | (*x)++; | |
1148 | } | |
f29a481a | 1149 | #endif |
805dd538 | 1150 | } |
96385642 VZ |
1151 | else // single line control |
1152 | { | |
2829d9e3 | 1153 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 | 1154 | { |
ac0d36b5 | 1155 | *y = 0; |
96385642 VZ |
1156 | *x = pos; |
1157 | } | |
1158 | else | |
1159 | { | |
1160 | // index out of bounds | |
7d8268a1 | 1161 | return false; |
96385642 | 1162 | } |
8bbe427f | 1163 | } |
96385642 | 1164 | |
7d8268a1 | 1165 | return true; |
6de97a3b | 1166 | } |
c801d85f | 1167 | |
e3ca08dd | 1168 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 1169 | { |
2830bf19 | 1170 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; |
805dd538 | 1171 | |
f29a481a MR |
1172 | #ifdef __WXGTK20__ |
1173 | GtkTextIter iter; | |
1174 | gtk_text_buffer_get_iter_at_line_offset(m_buffer, &iter, y, x); | |
1175 | return gtk_text_iter_get_offset(&iter); | |
1176 | #else | |
2830bf19 | 1177 | long pos=0; |
ac0d36b5 | 1178 | for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n' |
8bbe427f | 1179 | |
3358d36e | 1180 | pos += x; |
2830bf19 | 1181 | return pos; |
f29a481a | 1182 | #endif |
6de97a3b | 1183 | } |
c801d85f | 1184 | |
a81258be | 1185 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 1186 | { |
f29a481a MR |
1187 | #ifdef __WXGTK20__ |
1188 | if (m_windowStyle & wxTE_MULTILINE) | |
1189 | { | |
1190 | int last_line = gtk_text_buffer_get_line_count( m_buffer ) - 1; | |
1191 | if (lineNo > last_line) | |
1192 | return -1; | |
1193 | ||
1194 | GtkTextIter iter; | |
1195 | gtk_text_buffer_get_iter_at_line(m_buffer, &iter, lineNo); | |
1196 | // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line | |
1197 | return gtk_text_iter_get_chars_in_line(&iter) - ((lineNo == last_line) ? 0 : 1); | |
1198 | } | |
1199 | else | |
1200 | #endif | |
1201 | { | |
1202 | wxString str = GetLineText (lineNo); | |
1203 | return (int) str.Length(); | |
1204 | } | |
6de97a3b | 1205 | } |
c801d85f | 1206 | |
a81258be | 1207 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 1208 | { |
2830bf19 | 1209 | if (m_windowStyle & wxTE_MULTILINE) |
a81258be | 1210 | { |
fab591c5 | 1211 | #ifdef __WXGTK20__ |
41b81aed | 1212 | return gtk_text_buffer_get_line_count( m_buffer ); |
fab591c5 | 1213 | #else |
2830bf19 RR |
1214 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
1215 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
1216 | ||
1217 | if (text) | |
1218 | { | |
1219 | int currentLine = 0; | |
1220 | for (int i = 0; i < len; i++ ) | |
96385642 | 1221 | { |
2830bf19 | 1222 | if (text[i] == '\n') |
96385642 VZ |
1223 | currentLine++; |
1224 | } | |
2830bf19 | 1225 | g_free( text ); |
2829d9e3 VZ |
1226 | |
1227 | // currentLine is 0 based, add 1 to get number of lines | |
1228 | return currentLine + 1; | |
2830bf19 RR |
1229 | } |
1230 | else | |
96385642 | 1231 | { |
2830bf19 | 1232 | return 0; |
96385642 | 1233 | } |
fab591c5 | 1234 | #endif |
a81258be RR |
1235 | } |
1236 | else | |
2830bf19 | 1237 | { |
96385642 | 1238 | return 1; |
2830bf19 | 1239 | } |
6de97a3b | 1240 | } |
c801d85f | 1241 | |
debe6624 | 1242 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 1243 | { |
223d09f6 | 1244 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3358d36e | 1245 | |
74c5a810 | 1246 | if ( IsMultiLine() ) |
291a8f20 | 1247 | { |
fab591c5 | 1248 | #ifdef __WXGTK20__ |
fab591c5 | 1249 | GtkTextIter iter; |
41b81aed RR |
1250 | gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos ); |
1251 | gtk_text_buffer_place_cursor( m_buffer, &iter ); | |
74c5a810 VZ |
1252 | gtk_text_view_scroll_mark_onscreen |
1253 | ( | |
1254 | GTK_TEXT_VIEW(m_text), | |
41b81aed | 1255 | gtk_text_buffer_get_insert( m_buffer ) |
74c5a810 VZ |
1256 | ); |
1257 | #else // GTK+ 1.x | |
291a8f20 RR |
1258 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_text), |
1259 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 1260 | |
291a8f20 | 1261 | /* we fake a set_point by inserting and deleting. as the user |
fab591c5 | 1262 | isn't supposed to get to know about this non-sense, we |
3358d36e VZ |
1263 | disconnect so that no events are sent to the user program. */ |
1264 | ||
291a8f20 RR |
1265 | gint tmp = (gint)pos; |
1266 | gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp ); | |
3358d36e VZ |
1267 | gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp ); |
1268 | ||
291a8f20 RR |
1269 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
1270 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 1271 | |
fab591c5 | 1272 | // bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 1273 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
74c5a810 | 1274 | #endif // GTK+ 2/1 |
ac0d36b5 | 1275 | } |
2830bf19 | 1276 | else |
291a8f20 | 1277 | { |
d59051dd | 1278 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); |
3358d36e | 1279 | |
fab591c5 | 1280 | // Bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 1281 | SET_EDITABLE_POS(m_text, (guint32)pos); |
291a8f20 | 1282 | } |
6de97a3b | 1283 | } |
c801d85f | 1284 | |
03f38c58 | 1285 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 1286 | { |
223d09f6 | 1287 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1288 | |
d59051dd | 1289 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1290 | { |
1291 | #ifdef __WXGTK20__ | |
fab591c5 | 1292 | GtkTextIter end; |
41b81aed RR |
1293 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
1294 | gtk_text_buffer_place_cursor( m_buffer, &end ); | |
fab591c5 | 1295 | #else |
d59051dd | 1296 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); |
fab591c5 RR |
1297 | #endif |
1298 | } | |
d59051dd | 1299 | else |
fab591c5 | 1300 | { |
d59051dd | 1301 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); |
fab591c5 | 1302 | } |
6de97a3b | 1303 | } |
c801d85f | 1304 | |
debe6624 | 1305 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 1306 | { |
223d09f6 | 1307 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1308 | |
2830bf19 | 1309 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1310 | { |
1311 | #ifdef __WXGTK20__ | |
1312 | gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable ); | |
1313 | #else | |
2830bf19 | 1314 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); |
fab591c5 RR |
1315 | #endif |
1316 | } | |
2830bf19 | 1317 | else |
fab591c5 | 1318 | { |
2830bf19 | 1319 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); |
fab591c5 | 1320 | } |
6de97a3b | 1321 | } |
c801d85f | 1322 | |
68df5777 RR |
1323 | bool wxTextCtrl::Enable( bool enable ) |
1324 | { | |
1325 | if (!wxWindowBase::Enable(enable)) | |
1326 | { | |
1327 | // nothing to do | |
7d8268a1 | 1328 | return false; |
68df5777 | 1329 | } |
f6bcfd97 | 1330 | |
68df5777 RR |
1331 | if (m_windowStyle & wxTE_MULTILINE) |
1332 | { | |
fab591c5 RR |
1333 | #ifdef __WXGTK20__ |
1334 | SetEditable( enable ); | |
1335 | #else | |
68df5777 | 1336 | gtk_text_set_editable( GTK_TEXT(m_text), enable ); |
5abf8c9d | 1337 | OnParentEnable(enable); |
fab591c5 | 1338 | #endif |
68df5777 RR |
1339 | } |
1340 | else | |
1341 | { | |
1342 | gtk_widget_set_sensitive( m_text, enable ); | |
1343 | } | |
1344 | ||
7d8268a1 | 1345 | return true; |
68df5777 RR |
1346 | } |
1347 | ||
fdca68a6 JS |
1348 | // wxGTK-specific: called recursively by Enable, |
1349 | // to give widgets an oppprtunity to correct their colours after they | |
1350 | // have been changed by Enable | |
1351 | void wxTextCtrl::OnParentEnable( bool enable ) | |
1352 | { | |
1353 | // If we have a custom background colour, we use this colour in both | |
1354 | // disabled and enabled mode, or we end up with a different colour under the | |
1355 | // text. | |
1356 | wxColour oldColour = GetBackgroundColour(); | |
1357 | if (oldColour.Ok()) | |
1358 | { | |
1359 | // Need to set twice or it'll optimize the useful stuff out | |
1360 | if (oldColour == * wxWHITE) | |
1361 | SetBackgroundColour(*wxBLACK); | |
1362 | else | |
1363 | SetBackgroundColour(*wxWHITE); | |
1364 | SetBackgroundColour(oldColour); | |
1365 | } | |
1366 | } | |
1367 | ||
3a9fa0d6 VZ |
1368 | void wxTextCtrl::MarkDirty() |
1369 | { | |
7d8268a1 | 1370 | m_modified = true; |
3a9fa0d6 VZ |
1371 | } |
1372 | ||
0efe5ba7 VZ |
1373 | void wxTextCtrl::DiscardEdits() |
1374 | { | |
7d8268a1 | 1375 | m_modified = false; |
0efe5ba7 VZ |
1376 | } |
1377 | ||
ce2f50e3 VZ |
1378 | // ---------------------------------------------------------------------------- |
1379 | // max text length support | |
1380 | // ---------------------------------------------------------------------------- | |
1381 | ||
1382 | void wxTextCtrl::IgnoreNextTextUpdate() | |
1383 | { | |
7d8268a1 | 1384 | m_ignoreNextUpdate = true; |
ce2f50e3 VZ |
1385 | } |
1386 | ||
1387 | bool wxTextCtrl::IgnoreTextUpdate() | |
1388 | { | |
1389 | if ( m_ignoreNextUpdate ) | |
1390 | { | |
7d8268a1 | 1391 | m_ignoreNextUpdate = false; |
ce2f50e3 | 1392 | |
7d8268a1 | 1393 | return true; |
ce2f50e3 VZ |
1394 | } |
1395 | ||
7d8268a1 | 1396 | return false; |
ce2f50e3 VZ |
1397 | } |
1398 | ||
d7eee191 VZ |
1399 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1400 | { | |
1401 | if ( !HasFlag(wxTE_MULTILINE) ) | |
1402 | { | |
1403 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
ce2f50e3 VZ |
1404 | |
1405 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
1406 | // we had tried to enter more text than allowed by max text length and | |
1407 | // the text wasn't really changed | |
1408 | // | |
1409 | // to detect this and generate TEXT_MAXLEN event instead of | |
1410 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
1411 | // | |
1412 | // when max len is set to 0 we disconnect our handler as it means that | |
1413 | // we shouldn't check anything any more | |
1414 | if ( len ) | |
1415 | { | |
1416 | gtk_signal_connect( GTK_OBJECT(m_text), | |
1417 | "insert_text", | |
1418 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
1419 | (gpointer)this); | |
1420 | } | |
1421 | else // no checking | |
1422 | { | |
1423 | gtk_signal_disconnect_by_func | |
1424 | ( | |
1425 | GTK_OBJECT(m_text), | |
1426 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
1427 | (gpointer)this | |
1428 | ); | |
1429 | } | |
d7eee191 VZ |
1430 | } |
1431 | } | |
1432 | ||
debe6624 | 1433 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 1434 | { |
223d09f6 | 1435 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1436 | |
2b5f62a0 VZ |
1437 | if (from == -1 && to == -1) |
1438 | { | |
1439 | from = 0; | |
1440 | to = GetValue().Length(); | |
1441 | } | |
1442 | ||
fab591c5 | 1443 | #ifndef __WXGTK20__ |
a1f79c1e VZ |
1444 | if ( (m_windowStyle & wxTE_MULTILINE) && |
1445 | !GTK_TEXT(m_text)->line_start_cache ) | |
1446 | { | |
1447 | // tell the programmer that it didn't work | |
1448 | wxLogDebug(_T("Can't call SetSelection() before realizing the control")); | |
1449 | return; | |
1450 | } | |
fab591c5 | 1451 | #endif |
a1f79c1e | 1452 | |
fab591c5 RR |
1453 | if (m_windowStyle & wxTE_MULTILINE) |
1454 | { | |
1455 | #ifdef __WXGTK20__ | |
739e366a | 1456 | GtkTextIter fromi, toi; |
41b81aed RR |
1457 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); |
1458 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
739e366a | 1459 | |
41b81aed RR |
1460 | gtk_text_buffer_place_cursor( m_buffer, &toi ); |
1461 | gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi ); | |
fab591c5 RR |
1462 | #else |
1463 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1464 | #endif | |
1465 | } | |
1466 | else | |
1467 | { | |
1468 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1469 | } | |
6de97a3b | 1470 | } |
c801d85f | 1471 | |
afbe906a | 1472 | void wxTextCtrl::ShowPosition( long pos ) |
c801d85f | 1473 | { |
afbe906a RR |
1474 | if (m_windowStyle & wxTE_MULTILINE) |
1475 | { | |
71aba833 | 1476 | #ifdef __WXGTK20__ |
71aba833 | 1477 | GtkTextIter iter; |
41b81aed | 1478 | gtk_text_buffer_get_start_iter( m_buffer, &iter ); |
71aba833 | 1479 | gtk_text_iter_set_offset( &iter, pos ); |
41b81aed | 1480 | GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE ); |
71aba833 VZ |
1481 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 ); |
1482 | #else // GTK 1.x | |
afbe906a RR |
1483 | GtkAdjustment *vp = GTK_TEXT(m_text)->vadj; |
1484 | float totalLines = (float) GetNumberOfLines(); | |
1485 | long posX; | |
1486 | long posY; | |
1487 | PositionToXY(pos, &posX, &posY); | |
1488 | float posLine = (float) posY; | |
1489 | float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower; | |
1490 | gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p); | |
71aba833 | 1491 | #endif // GTK 1.x/2.x |
afbe906a | 1492 | } |
6de97a3b | 1493 | } |
c801d85f | 1494 | |
692c9b86 VZ |
1495 | #ifdef __WXGTK20__ |
1496 | ||
1497 | wxTextCtrlHitTestResult | |
1498 | wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const | |
1499 | { | |
1500 | if ( !IsMultiLine() ) | |
1501 | { | |
1502 | // not supported | |
1503 | return wxTE_HT_UNKNOWN; | |
1504 | } | |
1505 | ||
1506 | int x, y; | |
1507 | gtk_text_view_window_to_buffer_coords | |
1508 | ( | |
1509 | GTK_TEXT_VIEW(m_text), | |
1510 | GTK_TEXT_WINDOW_TEXT, | |
1511 | pt.x, pt.y, | |
1512 | &x, &y | |
1513 | ); | |
1514 | ||
1515 | GtkTextIter iter; | |
1516 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); | |
1517 | if ( pos ) | |
1518 | *pos = gtk_text_iter_get_offset(&iter); | |
1519 | ||
1520 | return wxTE_HT_ON_TEXT; | |
1521 | } | |
1522 | ||
1523 | #endif // __WXGTK20__ | |
1524 | ||
03f38c58 | 1525 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 1526 | { |
223d09f6 | 1527 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1528 | |
fab591c5 RR |
1529 | #ifdef __WXGTK20__ |
1530 | if (m_windowStyle & wxTE_MULTILINE) | |
1531 | { | |
fab591c5 RR |
1532 | // There is no direct accessor for the cursor, but |
1533 | // internally, the cursor is the "mark" called | |
1534 | // "insert" in the text view's btree structure. | |
a8bf1826 | 1535 | |
41b81aed | 1536 | GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer ); |
fab591c5 | 1537 | GtkTextIter cursor; |
41b81aed | 1538 | gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark ); |
a8bf1826 | 1539 | |
fab591c5 RR |
1540 | return gtk_text_iter_get_offset( &cursor ); |
1541 | } | |
1542 | else | |
1543 | #endif | |
1544 | { | |
0a164d4c | 1545 | return (long) GET_EDITABLE_POS(m_text); |
fab591c5 | 1546 | } |
6de97a3b | 1547 | } |
c801d85f | 1548 | |
7d8268a1 | 1549 | wxTextPos wxTextCtrl::GetLastPosition() const |
c801d85f | 1550 | { |
223d09f6 | 1551 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1552 | |
2830bf19 | 1553 | int pos = 0; |
a8bf1826 | 1554 | |
2830bf19 | 1555 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1556 | { |
1557 | #ifdef __WXGTK20__ | |
fab591c5 | 1558 | GtkTextIter end; |
41b81aed | 1559 | gtk_text_buffer_get_end_iter( m_buffer, &end ); |
a8bf1826 | 1560 | |
fab591c5 RR |
1561 | pos = gtk_text_iter_get_offset( &end ); |
1562 | #else | |
2830bf19 | 1563 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
fab591c5 RR |
1564 | #endif |
1565 | } | |
2830bf19 | 1566 | else |
fab591c5 | 1567 | { |
2830bf19 | 1568 | pos = GTK_ENTRY(m_text)->text_length; |
fab591c5 | 1569 | } |
805dd538 | 1570 | |
ac0d36b5 | 1571 | return (long)pos; |
6de97a3b | 1572 | } |
c801d85f | 1573 | |
debe6624 | 1574 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 1575 | { |
223d09f6 | 1576 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1577 | |
581ee8a9 | 1578 | #ifdef __WXGTK20__ |
fdd55287 | 1579 | if (m_windowStyle & wxTE_MULTILINE) |
581ee8a9 | 1580 | { |
581ee8a9 | 1581 | GtkTextIter fromi, toi; |
41b81aed RR |
1582 | gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from ); |
1583 | gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to ); | |
581ee8a9 | 1584 | |
41b81aed | 1585 | gtk_text_buffer_delete( m_buffer, &fromi, &toi ); |
581ee8a9 VZ |
1586 | } |
1587 | else // single line | |
fab591c5 | 1588 | #endif |
581ee8a9 | 1589 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 1590 | } |
c801d85f | 1591 | |
debe6624 | 1592 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 1593 | { |
223d09f6 | 1594 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1595 | |
581ee8a9 | 1596 | Remove( from, to ); |
bb69661b | 1597 | |
7d8268a1 | 1598 | if (!value.empty()) |
2df7be7f | 1599 | { |
581ee8a9 VZ |
1600 | #ifdef __WXGTK20__ |
1601 | SetInsertionPoint( from ); | |
1602 | WriteText( value ); | |
1603 | #else // GTK 1.x | |
2df7be7f | 1604 | gint pos = (gint)from; |
05939a81 | 1605 | #if wxUSE_UNICODE |
2df7be7f RR |
1606 | wxWX2MBbuf buf = value.mbc_str(); |
1607 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
05939a81 | 1608 | #else |
2df7be7f | 1609 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
581ee8a9 VZ |
1610 | #endif // wxUSE_UNICODE |
1611 | #endif // GTK 1.x/2.x | |
2df7be7f | 1612 | } |
6de97a3b | 1613 | } |
c801d85f | 1614 | |
03f38c58 | 1615 | void wxTextCtrl::Cut() |
c801d85f | 1616 | { |
223d09f6 | 1617 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1618 | |
bbde2e29 VS |
1619 | #ifdef __WXGTK20__ |
1620 | if (m_windowStyle & wxTE_MULTILINE) | |
1621 | g_signal_emit_by_name(m_text, "cut-clipboard"); | |
1622 | else | |
fab591c5 | 1623 | #endif |
bbde2e29 | 1624 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1625 | } |
c801d85f | 1626 | |
03f38c58 | 1627 | void wxTextCtrl::Copy() |
c801d85f | 1628 | { |
223d09f6 | 1629 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1630 | |
bbde2e29 VS |
1631 | #ifdef __WXGTK20__ |
1632 | if (m_windowStyle & wxTE_MULTILINE) | |
1633 | g_signal_emit_by_name(m_text, "copy-clipboard"); | |
1634 | else | |
fab591c5 | 1635 | #endif |
bbde2e29 | 1636 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1637 | } |
c801d85f | 1638 | |
03f38c58 | 1639 | void wxTextCtrl::Paste() |
c801d85f | 1640 | { |
223d09f6 | 1641 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1642 | |
bbde2e29 VS |
1643 | #ifdef __WXGTK20__ |
1644 | if (m_windowStyle & wxTE_MULTILINE) | |
1645 | g_signal_emit_by_name(m_text, "paste-clipboard"); | |
1646 | else | |
fab591c5 | 1647 | #endif |
bbde2e29 | 1648 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1649 | } |
c801d85f | 1650 | |
ca8b28f2 JS |
1651 | // Undo/redo |
1652 | void wxTextCtrl::Undo() | |
1653 | { | |
1654 | // TODO | |
223d09f6 | 1655 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
1656 | } |
1657 | ||
1658 | void wxTextCtrl::Redo() | |
1659 | { | |
1660 | // TODO | |
223d09f6 | 1661 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
1662 | } |
1663 | ||
1664 | bool wxTextCtrl::CanUndo() const | |
1665 | { | |
1666 | // TODO | |
4855a477 | 1667 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); |
7d8268a1 | 1668 | return false; |
ca8b28f2 JS |
1669 | } |
1670 | ||
1671 | bool wxTextCtrl::CanRedo() const | |
1672 | { | |
1673 | // TODO | |
4855a477 | 1674 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); |
7d8268a1 | 1675 | return false; |
ca8b28f2 JS |
1676 | } |
1677 | ||
1678 | // If the return values from and to are the same, there is no | |
1679 | // selection. | |
2d4cc5b6 | 1680 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const |
ca8b28f2 | 1681 | { |
223d09f6 | 1682 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
bb69661b | 1683 | |
5b87f8bf RD |
1684 | gint from = -1; |
1685 | gint to = -1; | |
7d8268a1 | 1686 | bool haveSelection = false; |
5b87f8bf | 1687 | |
fb47b99f | 1688 | #ifdef __WXGTK20__ |
5b87f8bf RD |
1689 | if (m_windowStyle & wxTE_MULTILINE) |
1690 | { | |
5b87f8bf | 1691 | GtkTextIter ifrom, ito; |
41b81aed | 1692 | if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) ) |
5b87f8bf | 1693 | { |
7d8268a1 | 1694 | haveSelection = true; |
5b87f8bf RD |
1695 | from = gtk_text_iter_get_offset(&ifrom); |
1696 | to = gtk_text_iter_get_offset(&ito); | |
1697 | } | |
1698 | } | |
1699 | else // not multi-line | |
1700 | { | |
1701 | if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text), | |
1702 | &from, &to) ) | |
1703 | { | |
7d8268a1 | 1704 | haveSelection = true; |
5b87f8bf RD |
1705 | } |
1706 | } | |
1707 | #else // not GTK2 | |
1708 | if ( (GTK_EDITABLE(m_text)->has_selection) ) | |
1709 | { | |
7d8268a1 | 1710 | haveSelection = true; |
5b87f8bf RD |
1711 | from = (long) GTK_EDITABLE(m_text)->selection_start_pos; |
1712 | to = (long) GTK_EDITABLE(m_text)->selection_end_pos; | |
1713 | } | |
9e691f46 | 1714 | #endif |
2d4cc5b6 | 1715 | |
5b87f8bf RD |
1716 | if (! haveSelection ) |
1717 | from = to = GetInsertionPoint(); | |
1718 | ||
1719 | if ( from > to ) | |
1720 | { | |
1721 | // exchange them to be compatible with wxMSW | |
1722 | gint tmp = from; | |
1723 | from = to; | |
1724 | to = tmp; | |
1725 | } | |
bb69661b | 1726 | |
2d4cc5b6 VZ |
1727 | if ( fromOut ) |
1728 | *fromOut = from; | |
1729 | if ( toOut ) | |
1730 | *toOut = to; | |
ca8b28f2 JS |
1731 | } |
1732 | ||
5b87f8bf | 1733 | |
ca8b28f2 JS |
1734 | bool wxTextCtrl::IsEditable() const |
1735 | { | |
7d8268a1 | 1736 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
05060eeb | 1737 | |
9e691f46 | 1738 | #ifdef __WXGTK20__ |
fdd55287 VZ |
1739 | if (m_windowStyle & wxTE_MULTILINE) |
1740 | { | |
1741 | return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)); | |
1742 | } | |
1743 | else | |
1744 | { | |
1745 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
1746 | } | |
9e691f46 | 1747 | #else |
05060eeb | 1748 | return GTK_EDITABLE(m_text)->editable; |
9e691f46 | 1749 | #endif |
ca8b28f2 JS |
1750 | } |
1751 | ||
0efe5ba7 VZ |
1752 | bool wxTextCtrl::IsModified() const |
1753 | { | |
1754 | return m_modified; | |
1755 | } | |
1756 | ||
03f38c58 | 1757 | void wxTextCtrl::Clear() |
c801d85f | 1758 | { |
902725ee | 1759 | SetValue( wxEmptyString ); |
6de97a3b | 1760 | } |
c801d85f | 1761 | |
903f689b | 1762 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 1763 | { |
223d09f6 | 1764 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
805dd538 | 1765 | |
12a3f227 | 1766 | if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
2830bf19 RR |
1767 | { |
1768 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1769 | event.SetEventObject(this); | |
f6bcfd97 | 1770 | event.SetString(GetValue()); |
2830bf19 RR |
1771 | if (GetEventHandler()->ProcessEvent(event)) return; |
1772 | } | |
903f689b | 1773 | |
12a3f227 | 1774 | if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE)) |
da048e3d | 1775 | { |
2b328fc9 RR |
1776 | // This will invoke the dialog default action, such |
1777 | // as the clicking the default button. | |
a8bf1826 | 1778 | |
da048e3d | 1779 | wxWindow *top_frame = m_parent; |
8487f887 | 1780 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
da048e3d | 1781 | top_frame = top_frame->GetParent(); |
a8bf1826 | 1782 | |
2b328fc9 | 1783 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
da048e3d | 1784 | { |
2b328fc9 RR |
1785 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
1786 | ||
1787 | if (window->default_widget) | |
1788 | { | |
1789 | gtk_widget_activate (window->default_widget); | |
1790 | return; | |
1791 | } | |
13111b2a | 1792 | } |
da048e3d RR |
1793 | } |
1794 | ||
2830bf19 | 1795 | key_event.Skip(); |
6de97a3b | 1796 | } |
c801d85f | 1797 | |
03f38c58 | 1798 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 1799 | { |
ae0bdb01 | 1800 | return GTK_WIDGET(m_text); |
6de97a3b | 1801 | } |
e3e65dac | 1802 | |
903f689b RR |
1803 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
1804 | { | |
ae0bdb01 | 1805 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1806 | { |
1807 | #ifdef __WXGTK20__ | |
1808 | return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork | |
1809 | #else | |
ae0bdb01 | 1810 | return (window == GTK_TEXT(m_text)->text_area); |
fab591c5 RR |
1811 | #endif |
1812 | } | |
ae0bdb01 | 1813 | else |
fab591c5 | 1814 | { |
ae0bdb01 | 1815 | return (window == GTK_ENTRY(m_text)->text_area); |
fab591c5 | 1816 | } |
903f689b | 1817 | } |
e3e65dac | 1818 | |
bb69661b VZ |
1819 | // the font will change for subsequent text insertiongs |
1820 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 1821 | { |
7d8268a1 | 1822 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
8bbe427f | 1823 | |
a66954a6 | 1824 | if ( !wxTextCtrlBase::SetFont(font) ) |
bb69661b VZ |
1825 | { |
1826 | // font didn't change, nothing to do | |
7d8268a1 | 1827 | return false; |
bb69661b VZ |
1828 | } |
1829 | ||
1830 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1831 | { | |
7d8268a1 | 1832 | SetUpdateFont(true); |
bb69661b | 1833 | |
1ff4714d VZ |
1834 | m_defaultStyle.SetFont(font); |
1835 | ||
01041145 | 1836 | ChangeFontGlobally(); |
bb69661b VZ |
1837 | } |
1838 | ||
7d8268a1 | 1839 | return true; |
58614078 RR |
1840 | } |
1841 | ||
01041145 VZ |
1842 | void wxTextCtrl::ChangeFontGlobally() |
1843 | { | |
1844 | // this method is very inefficient and hence should be called as rarely as | |
1845 | // possible! | |
c04ec496 VZ |
1846 | // |
1847 | // TODO: it can be implemented much more efficiently for GTK2 | |
c04ec496 | 1848 | #ifndef __WXGTK20__ |
22800f32 JS |
1849 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont, |
1850 | ||
01041145 | 1851 | _T("shouldn't be called for single line controls") ); |
22800f32 JS |
1852 | #else |
1853 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE), | |
1854 | _T("shouldn't be called for single line controls") ); | |
1855 | #endif | |
01041145 VZ |
1856 | |
1857 | wxString value = GetValue(); | |
7d8268a1 | 1858 | if ( !value.empty() ) |
01041145 | 1859 | { |
7d8268a1 | 1860 | SetUpdateFont(false); |
572aeb77 | 1861 | |
01041145 VZ |
1862 | Clear(); |
1863 | AppendText(value); | |
01041145 VZ |
1864 | } |
1865 | } | |
1866 | ||
c04ec496 VZ |
1867 | #ifndef __WXGTK20__ |
1868 | ||
01041145 VZ |
1869 | void wxTextCtrl::UpdateFontIfNeeded() |
1870 | { | |
1871 | if ( m_updateFont ) | |
1872 | ChangeFontGlobally(); | |
1873 | } | |
1874 | ||
c04ec496 VZ |
1875 | #endif // GTK+ 1.x |
1876 | ||
17665a2b VZ |
1877 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) |
1878 | { | |
1879 | if ( !wxControl::SetForegroundColour(colour) ) | |
7d8268a1 | 1880 | return false; |
17665a2b VZ |
1881 | |
1882 | // update default fg colour too | |
1883 | m_defaultStyle.SetTextColour(colour); | |
1884 | ||
7d8268a1 | 1885 | return true; |
17665a2b VZ |
1886 | } |
1887 | ||
f03fc89f | 1888 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 1889 | { |
7d8268a1 | 1890 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
a81258be | 1891 | |
1f477433 | 1892 | if ( !wxControl::SetBackgroundColour( colour ) ) |
7d8268a1 | 1893 | return false; |
3358d36e | 1894 | |
c2094564 | 1895 | #ifndef __WXGTK20__ |
f03fc89f | 1896 | if (!m_widget->window) |
7d8268a1 | 1897 | return false; |
c2094564 | 1898 | #endif |
8bbe427f | 1899 | |
f03fc89f | 1900 | if (!m_backgroundColour.Ok()) |
7d8268a1 | 1901 | return false; |
8bbe427f | 1902 | |
ae0bdb01 RR |
1903 | if (m_windowStyle & wxTE_MULTILINE) |
1904 | { | |
1fc32b12 | 1905 | #ifndef __WXGTK20__ |
ae0bdb01 | 1906 | GdkWindow *window = GTK_TEXT(m_text)->text_area; |
f03fc89f | 1907 | if (!window) |
7d8268a1 | 1908 | return false; |
ae0bdb01 RR |
1909 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); |
1910 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
1911 | gdk_window_clear( window ); | |
fab591c5 | 1912 | #endif |
ae0bdb01 | 1913 | } |
f03fc89f | 1914 | |
17665a2b VZ |
1915 | // change active background color too |
1916 | m_defaultStyle.SetBackgroundColour( colour ); | |
1917 | ||
7d8268a1 | 1918 | return true; |
58614078 RR |
1919 | } |
1920 | ||
eda40bfc | 1921 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) |
17665a2b | 1922 | { |
17665a2b VZ |
1923 | if ( m_windowStyle & wxTE_MULTILINE ) |
1924 | { | |
1925 | if ( style.IsDefault() ) | |
1926 | { | |
1927 | // nothing to do | |
7d8268a1 | 1928 | return true; |
17665a2b | 1929 | } |
902725ee | 1930 | |
cc3da3f8 | 1931 | #ifdef __WXGTK20__ |
41b81aed | 1932 | gint l = gtk_text_buffer_get_char_count( m_buffer ); |
17665a2b | 1933 | |
7d8268a1 | 1934 | wxCHECK_MSG( start >= 0 && end <= l, false, |
cc3da3f8 RR |
1935 | _T("invalid range in wxTextCtrl::SetStyle") ); |
1936 | ||
1937 | GtkTextIter starti, endi; | |
41b81aed RR |
1938 | gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start ); |
1939 | gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end ); | |
cc3da3f8 RR |
1940 | |
1941 | // use the attributes from style which are set in it and fall back | |
1942 | // first to the default style and then to the text control default | |
1943 | // colours for the others | |
1944 | wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this); | |
1945 | ||
41b81aed | 1946 | wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi ); |
cc3da3f8 RR |
1947 | #else |
1948 | // VERY dirty way to do that - removes the required text and re-adds it | |
1949 | // with styling (FIXME) | |
5b87f8bf | 1950 | |
17665a2b VZ |
1951 | gint l = gtk_text_get_length( GTK_TEXT(m_text) ); |
1952 | ||
7d8268a1 | 1953 | wxCHECK_MSG( start >= 0 && end <= l, false, |
17665a2b VZ |
1954 | _T("invalid range in wxTextCtrl::SetStyle") ); |
1955 | ||
1956 | gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) ); | |
1957 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end ); | |
1958 | wxString tmp(text,*wxConvCurrent); | |
1959 | g_free( text ); | |
1960 | ||
1961 | gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end ); | |
1962 | gtk_editable_set_position( GTK_EDITABLE(m_text), start ); | |
1963 | ||
902725ee | 1964 | #if wxUSE_UNICODE |
17665a2b VZ |
1965 | wxWX2MBbuf buf = tmp.mbc_str(); |
1966 | const char *txt = buf; | |
1967 | size_t txtlen = strlen(buf); | |
902725ee | 1968 | #else |
17665a2b VZ |
1969 | const char *txt = tmp; |
1970 | size_t txtlen = tmp.length(); | |
902725ee | 1971 | #endif |
17665a2b | 1972 | |
eda40bfc VZ |
1973 | // use the attributes from style which are set in it and fall back |
1974 | // first to the default style and then to the text control default | |
1975 | // colours for the others | |
1976 | wxGtkTextInsert(m_text, | |
1977 | wxTextAttr::Combine(style, m_defaultStyle, this), | |
1978 | txt, | |
1979 | txtlen); | |
17665a2b VZ |
1980 | |
1981 | /* does not seem to help under GTK+ 1.2 !!! | |
1982 | gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */ | |
1983 | SetInsertionPoint( old_pos ); | |
fab591c5 | 1984 | #endif |
902725ee | 1985 | |
7d8268a1 | 1986 | return true; |
17665a2b | 1987 | } |
902725ee WS |
1988 | |
1989 | // else single line | |
1990 | // cannot do this for GTK+'s Entry widget | |
1991 | return false; | |
17665a2b VZ |
1992 | } |
1993 | ||
f40fdaa3 | 1994 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) |
58614078 | 1995 | { |
f40fdaa3 | 1996 | gtk_widget_modify_style(m_text, style); |
68dda785 | 1997 | } |
f96aa4d9 | 1998 | |
e702ff0f JS |
1999 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
2000 | { | |
2001 | Cut(); | |
2002 | } | |
2003 | ||
2004 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
2005 | { | |
2006 | Copy(); | |
2007 | } | |
2008 | ||
2009 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
2010 | { | |
2011 | Paste(); | |
2012 | } | |
2013 | ||
2014 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
2015 | { | |
2016 | Undo(); | |
2017 | } | |
2018 | ||
2019 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
2020 | { | |
2021 | Redo(); | |
2022 | } | |
2023 | ||
2024 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
2025 | { | |
2026 | event.Enable( CanCut() ); | |
2027 | } | |
2028 | ||
2029 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2030 | { | |
2031 | event.Enable( CanCopy() ); | |
2032 | } | |
2033 | ||
2034 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2035 | { | |
2036 | event.Enable( CanPaste() ); | |
2037 | } | |
2038 | ||
2039 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2040 | { | |
2041 | event.Enable( CanUndo() ); | |
2042 | } | |
2043 | ||
2044 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2045 | { | |
2046 | event.Enable( CanRedo() ); | |
2047 | } | |
65045edd RR |
2048 | |
2049 | void wxTextCtrl::OnInternalIdle() | |
2050 | { | |
2051 | wxCursor cursor = m_cursor; | |
2052 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
2053 | ||
307f16e8 | 2054 | if (cursor.Ok()) |
65045edd | 2055 | { |
fab591c5 | 2056 | #ifndef __WXGTK20__ |
65045edd | 2057 | GdkWindow *window = (GdkWindow*) NULL; |
13111b2a | 2058 | if (HasFlag(wxTE_MULTILINE)) |
65045edd RR |
2059 | window = GTK_TEXT(m_text)->text_area; |
2060 | else | |
2061 | window = GTK_ENTRY(m_text)->text_area; | |
13111b2a | 2062 | |
65045edd RR |
2063 | if (window) |
2064 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
2065 | ||
2066 | if (!g_globalCursor.Ok()) | |
2067 | cursor = *wxSTANDARD_CURSOR; | |
2068 | ||
2069 | window = m_widget->window; | |
247e5b16 | 2070 | if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget))) |
65045edd | 2071 | gdk_window_set_cursor( window, cursor.GetCursor() ); |
fab591c5 | 2072 | #endif |
65045edd | 2073 | } |
26bf1ce0 | 2074 | |
d7fa7eaa RR |
2075 | if (g_delayedFocus == this) |
2076 | { | |
2077 | if (GTK_WIDGET_REALIZED(m_widget)) | |
2078 | { | |
2079 | gtk_widget_grab_focus( m_widget ); | |
2080 | g_delayedFocus = NULL; | |
2081 | } | |
2082 | } | |
2083 | ||
e39af974 JS |
2084 | if (wxUpdateUIEvent::CanUpdate(this)) |
2085 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
65045edd | 2086 | } |
f68586e5 VZ |
2087 | |
2088 | wxSize wxTextCtrl::DoGetBestSize() const | |
2089 | { | |
2090 | // FIXME should be different for multi-line controls... | |
0279e844 | 2091 | wxSize ret( wxControl::DoGetBestSize() ); |
9f884528 RD |
2092 | wxSize best(80, ret.y); |
2093 | CacheBestSize(best); | |
2094 | return best; | |
f68586e5 | 2095 | } |
0cc7251e | 2096 | |
9cd6d737 VZ |
2097 | // ---------------------------------------------------------------------------- |
2098 | // freeze/thaw | |
2099 | // ---------------------------------------------------------------------------- | |
2100 | ||
0cc7251e VZ |
2101 | void wxTextCtrl::Freeze() |
2102 | { | |
2103 | if ( HasFlag(wxTE_MULTILINE) ) | |
2104 | { | |
41b81aed RR |
2105 | #ifdef __WXGTK20__ |
2106 | if ( !m_frozenness++ ) | |
2107 | { | |
2108 | // freeze textview updates and remove buffer | |
2109 | g_signal_connect( G_OBJECT(m_text), "expose_event", | |
2110 | GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this); | |
2111 | g_signal_connect( G_OBJECT(m_widget), "expose_event", | |
2112 | GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this); | |
2113 | gtk_widget_set_sensitive(m_widget, false); | |
2114 | g_object_ref(m_buffer); | |
2115 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL)); | |
0a164d4c | 2116 | } |
41b81aed RR |
2117 | #else |
2118 | gtk_text_freeze(GTK_TEXT(m_text)); | |
fab591c5 | 2119 | #endif |
41b81aed | 2120 | } |
0cc7251e VZ |
2121 | } |
2122 | ||
2123 | void wxTextCtrl::Thaw() | |
2124 | { | |
2125 | if ( HasFlag(wxTE_MULTILINE) ) | |
2126 | { | |
41b81aed RR |
2127 | #ifdef __WXGTK20__ |
2128 | wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") ); | |
2129 | ||
2130 | if ( !--m_frozenness ) | |
2131 | { | |
2132 | // Reattach buffer and thaw textview updates | |
2133 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer); | |
2134 | g_object_unref(m_buffer); | |
2135 | gtk_widget_set_sensitive(m_widget, true); | |
2136 | g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this); | |
2137 | g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this); | |
2138 | } | |
2139 | #else | |
817ec43a VZ |
2140 | GTK_TEXT(m_text)->vadj->value = 0.0; |
2141 | ||
0cc7251e | 2142 | gtk_text_thaw(GTK_TEXT(m_text)); |
fab591c5 | 2143 | #endif |
41b81aed | 2144 | } |
0cc7251e | 2145 | } |
9cd6d737 | 2146 | |
9440c3d0 KH |
2147 | // ---------------------------------------------------------------------------- |
2148 | // wxTextUrlEvent passing if style & wxTE_AUTO_URL | |
2149 | // ---------------------------------------------------------------------------- | |
2150 | ||
2151 | #ifdef __WXGTK20__ | |
2152 | ||
2153 | // FIXME: when dragging on a link the sample gets an "Unknown event". | |
2154 | // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or | |
2155 | // a buggy sample, or something else | |
2156 | void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event) | |
2157 | { | |
2158 | event.Skip(); | |
2159 | if(!(m_windowStyle & wxTE_AUTO_URL)) | |
2160 | return; | |
2161 | ||
2162 | gint x, y; | |
2163 | GtkTextIter start, end; | |
2164 | GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer), | |
2165 | "wxUrl"); | |
2166 | ||
2167 | gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET, | |
2168 | event.GetX(), event.GetY(), &x, &y); | |
2169 | ||
2170 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y); | |
2171 | if (!gtk_text_iter_has_tag(&end, tag)) | |
2172 | { | |
2173 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
2174 | GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor); | |
2175 | return; | |
2176 | } | |
2177 | ||
2178 | gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text), | |
2179 | GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor); | |
2180 | ||
2181 | start = end; | |
2182 | if(!gtk_text_iter_begins_tag(&start, tag)) | |
2183 | gtk_text_iter_backward_to_tag_toggle(&start, tag); | |
2184 | if(!gtk_text_iter_ends_tag(&end, tag)) | |
2185 | gtk_text_iter_forward_to_tag_toggle(&end, tag); | |
2186 | ||
2187 | // Native context menu is probably not desired on an URL. | |
2188 | // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value | |
2189 | if(event.GetEventType() == wxEVT_RIGHT_DOWN) | |
2190 | event.Skip(false); | |
2191 | ||
2192 | wxTextUrlEvent url_event(m_windowId, event, | |
2193 | gtk_text_iter_get_offset(&start), | |
2194 | gtk_text_iter_get_offset(&end)); | |
2195 | ||
2196 | InitCommandEvent(url_event); | |
2197 | // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag) | |
2198 | //event.Skip(!GetEventHandler()->ProcessEvent(url_event)); | |
2199 | GetEventHandler()->ProcessEvent(url_event); | |
2200 | } | |
2201 | #endif // gtk2 | |
2202 | ||
9cd6d737 VZ |
2203 | // ---------------------------------------------------------------------------- |
2204 | // scrolling | |
2205 | // ---------------------------------------------------------------------------- | |
2206 | ||
2207 | GtkAdjustment *wxTextCtrl::GetVAdj() const | |
2208 | { | |
1ce52aa6 VZ |
2209 | if ( !IsMultiLine() ) |
2210 | return NULL; | |
2211 | ||
fab591c5 | 2212 | #ifdef __WXGTK20__ |
1ce52aa6 | 2213 | return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget)); |
fab591c5 | 2214 | #else |
1ce52aa6 | 2215 | return GTK_TEXT(m_text)->vadj; |
fab591c5 | 2216 | #endif |
9cd6d737 VZ |
2217 | } |
2218 | ||
2219 | bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) | |
2220 | { | |
2221 | float value = adj->value + diff; | |
2222 | ||
2223 | if ( value < 0 ) | |
2224 | value = 0; | |
2225 | ||
2226 | float upper = adj->upper - adj->page_size; | |
2227 | if ( value > upper ) | |
2228 | value = upper; | |
2229 | ||
2230 | // did we noticeably change the scroll position? | |
2231 | if ( fabs(adj->value - value) < 0.2 ) | |
2232 | { | |
2233 | // well, this is what Robert does in wxScrollBar, so it must be good... | |
7d8268a1 | 2234 | return false; |
9cd6d737 VZ |
2235 | } |
2236 | ||
2237 | adj->value = value; | |
2238 | ||
1ce52aa6 VZ |
2239 | #ifdef __WXGTK20__ |
2240 | gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj)); | |
2241 | #else | |
9cd6d737 | 2242 | gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); |
fab591c5 | 2243 | #endif |
1ce52aa6 | 2244 | |
7d8268a1 | 2245 | return true; |
9cd6d737 VZ |
2246 | } |
2247 | ||
2248 | bool wxTextCtrl::ScrollLines(int lines) | |
2249 | { | |
2250 | GtkAdjustment *adj = GetVAdj(); | |
2251 | if ( !adj ) | |
7d8268a1 | 2252 | return false; |
9cd6d737 | 2253 | |
1ce52aa6 VZ |
2254 | #ifdef __WXGTK20__ |
2255 | int diff = (int)ceil(lines*adj->step_increment); | |
2256 | #else | |
9cd6d737 | 2257 | // this is hardcoded to 10 in GTK+ 1.2 (great idea) |
1ce52aa6 | 2258 | int diff = 10*lines; |
fab591c5 | 2259 | #endif |
1ce52aa6 VZ |
2260 | |
2261 | return DoScroll(adj, diff); | |
9cd6d737 VZ |
2262 | } |
2263 | ||
2264 | bool wxTextCtrl::ScrollPages(int pages) | |
2265 | { | |
2266 | GtkAdjustment *adj = GetVAdj(); | |
2267 | if ( !adj ) | |
7d8268a1 | 2268 | return false; |
9cd6d737 | 2269 | |
817ec43a | 2270 | return DoScroll(adj, (int)ceil(pages*adj->page_increment)); |
9cd6d737 VZ |
2271 | } |
2272 | ||
9d522606 RD |
2273 | |
2274 | // static | |
2275 | wxVisualAttributes | |
2276 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
2277 | { | |
2278 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
2279 | } |