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