]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
Add some missing runtime gtk+ version checks. There is more to spot.
[wxWidgets.git] / src / gtk1 / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "textctrl.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/textctrl.h"
18 #include "wx/utils.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #include "wx/settings.h"
22 #include "wx/panel.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
29 #include "wx/math.h"
30
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
33
34 //-----------------------------------------------------------------------------
35 // idle system
36 //-----------------------------------------------------------------------------
37
38 extern void wxapp_install_idle_handler();
39 extern bool g_isIdle;
40
41 //-----------------------------------------------------------------------------
42 // data
43 //-----------------------------------------------------------------------------
44
45 extern wxCursor g_globalCursor;
46 extern wxWindowGTK *g_delayedFocus;
47
48 // ----------------------------------------------------------------------------
49 // helpers
50 // ----------------------------------------------------------------------------
51
52 #ifdef __WXGTK20__
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" {
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
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
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 }
126 }
127
128 extern "C" {
129 static void wxGtkTextInsert(GtkWidget *text,
130 GtkTextBuffer *text_buffer,
131 const wxTextAttr& attr,
132 wxCharBuffer buffer)
133
134 {
135 gint start_offset;
136 GtkTextIter iter, start;
137
138 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
139 gtk_text_buffer_get_insert (text_buffer) );
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);
144
145 wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
146 }
147 }
148 #else
149 extern "C" {
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 }
167 }
168 #endif // GTK 1.x
169
170 // ----------------------------------------------------------------------------
171 // "insert_text" for GtkEntry
172 // ----------------------------------------------------------------------------
173
174 extern "C" {
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 {
182 if (g_isIdle)
183 wxapp_install_idle_handler();
184
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 }
210 }
211
212 #ifdef __WXGTK20__
213 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
214
215 extern "C" {
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 }
226 }
227
228 //-----------------------------------------------------------------------------
229 // GtkTextCharPredicates for gtk_text_iter_*_find_char
230 //-----------------------------------------------------------------------------
231
232 extern "C" {
233 static gboolean
234 pred_whitespace (gunichar ch, gpointer user_data)
235 {
236 return g_unichar_isspace(ch);
237 }
238 }
239
240 extern "C" {
241 static gboolean
242 pred_non_whitespace (gunichar ch, gpointer user_data)
243 {
244 return !g_unichar_isspace(ch);
245 }
246 }
247
248 extern "C" {
249 static gboolean
250 pred_nonpunct (gunichar ch, gpointer user_data)
251 {
252 return !g_unichar_ispunct(ch);
253 }
254 }
255
256 extern "C" {
257 static gboolean
258 pred_nonpunct_or_slash (gunichar ch, gpointer user_data)
259 {
260 return !g_unichar_ispunct(ch) || ch == '/';
261 }
262 }
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.
270 extern "C" {
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);
294
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 }
330 }
331
332 extern "C" {
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 }
359 }
360
361 //-----------------------------------------------------------------------------
362 // "insert-text" for GtkTextBuffer
363 //-----------------------------------------------------------------------------
364
365 extern "C" {
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 }
391 }
392
393 //-----------------------------------------------------------------------------
394 // "delete-range" for GtkTextBuffer
395 //-----------------------------------------------------------------------------
396
397 extern "C" {
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 }
416 }
417
418
419 #endif
420
421 //-----------------------------------------------------------------------------
422 // "changed"
423 //-----------------------------------------------------------------------------
424
425 extern "C" {
426 static void
427 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
428 {
429 if ( win->IgnoreTextUpdate() )
430 return;
431
432 if (!win->m_hasVMT) return;
433
434 if (g_isIdle)
435 wxapp_install_idle_handler();
436
437 win->SetModified();
438 #ifndef __WXGTK20__
439 win->UpdateFontIfNeeded();
440 #endif // !__WXGTK20__
441
442 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
443 event.SetEventObject( win );
444 win->GetEventHandler()->ProcessEvent( event );
445 }
446 }
447
448 //-----------------------------------------------------------------------------
449 // "expose_event" from scrolled window and textview
450 //-----------------------------------------------------------------------------
451
452 #ifdef __WXGTK20__
453 extern "C" {
454 static gboolean
455 gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
456 {
457 return TRUE;
458 }
459 }
460 #endif
461
462 //-----------------------------------------------------------------------------
463 // "changed" from vertical scrollbar
464 //-----------------------------------------------------------------------------
465
466 #ifndef __WXGTK20__
467 extern "C" {
468 static void
469 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
470 {
471 if (!win->m_hasVMT) return;
472
473 if (g_isIdle)
474 wxapp_install_idle_handler();
475
476 win->CalculateScrollbar();
477 }
478 }
479 #endif
480
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
494 extern "C" {
495 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
496 }
497
498 static GtkDrawCallback gs_gtk_text_draw = NULL;
499
500 extern "C" {
501 static void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
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 }
511 }
512
513 #endif // __WXGTK20__
514
515 //-----------------------------------------------------------------------------
516 // wxTextCtrl
517 //-----------------------------------------------------------------------------
518
519 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
520
521 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
522 EVT_CHAR(wxTextCtrl::OnChar)
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)
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
547 END_EVENT_TABLE()
548
549 void wxTextCtrl::Init()
550 {
551 m_ignoreNextUpdate =
552 m_modified = false;
553 SetUpdateFont(false);
554 m_text =
555 m_vScrollbar = (GtkWidget *)NULL;
556 #ifdef __WXGTK20__
557 m_frozenness = 0;
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);
570 #endif
571 }
572
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 )
581 {
582 Init();
583
584 Create( parent, id, value, pos, size, style, validator, name );
585 }
586
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 )
595 {
596 m_needParent = true;
597 m_acceptsFocus = true;
598
599 if (!PreCreation( parent, pos, size ) ||
600 !CreateBase( parent, id, pos, size, style, validator, name ))
601 {
602 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
603 return false;
604 }
605
606
607 m_vScrollbarVisible = false;
608
609 bool multi_line = (style & wxTE_MULTILINE) != 0;
610
611 if (multi_line)
612 {
613 #ifdef __WXGTK20__
614 // Create view
615 m_text = gtk_text_view_new();
616
617 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
618
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 );
623
624 // Insert view into scrolled window
625 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
626
627 // translate wx wrapping style to GTK+
628 GtkWrapMode wrap;
629 if ( HasFlag( wxTE_DONTWRAP ) )
630 wrap = GTK_WRAP_NONE;
631 else if ( HasFlag( wxTE_CHARWRAP ) )
632 wrap = GTK_WRAP_CHAR;
633 else if ( HasFlag( wxTE_WORDWRAP ) )
634 wrap = GTK_WRAP_WORD;
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__
639 if ( !gtk_check_version(2,4,0) )
640 {
641 wrap = GTK_WRAP_WORD_CHAR;
642 }
643 else
644 #endif
645 wrap = GTK_WRAP_WORD;
646 }
647
648 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), wrap );
649
650 if (!HasFlag(wxNO_BORDER))
651 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
652
653 gtk_widget_add_events( GTK_WIDGET(m_text), GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
654
655 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
656 #else // GTK+ 1
657 // create our control ...
658 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
659
660 // ... and put into the upper left hand corner of the table
661 bool bHasHScrollbar = false;
662 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
663 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
664 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
665 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
666 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
667 0, 0);
668
669 // always wrap words
670 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
671
672 // finally, put the vertical scrollbar in the upper right corner
673 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
674 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
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);
679 #endif // GTK+ 2/1
680 }
681 else
682 {
683 // a single-line text control: no need for scrollbars
684 m_widget =
685 m_text = gtk_entry_new();
686
687 #ifdef __WXGTK20__
688 if (style & wxNO_BORDER)
689 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
690 #endif
691 }
692
693 m_parent->DoAddChild( this );
694
695 m_focusWidget = m_text;
696
697 PostCreation(size);
698
699 if (multi_line)
700 gtk_widget_show(m_text);
701
702 #ifndef __WXGTK20__
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 );
707
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 }
719 }
720 #endif // GTK+ 1.x
721
722 if (!value.empty())
723 {
724 #ifdef __WXGTK20__
725 SetValue( value );
726 #else
727
728 #if !GTK_CHECK_VERSION(1, 2, 0)
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
734 gint tmp = 0;
735 #if wxUSE_UNICODE
736 wxWX2MBbuf val = value.mbc_str();
737 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
738 #else
739 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
740 #endif
741
742 if (multi_line)
743 {
744 // Bring editable's cursor uptodate. Bug in GTK.
745 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
746 }
747
748 #endif
749 }
750
751 if (style & wxTE_PASSWORD)
752 {
753 if (!multi_line)
754 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
755 }
756
757 if (style & wxTE_READONLY)
758 {
759 if (!multi_line)
760 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
761 #ifdef __WXGTK20__
762 else
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
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 }
782 else
783 {
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
794 }
795 #endif // __WXGTK20__
796
797 // We want to be notified about text changes.
798 #ifdef __WXGTK20__
799 if (multi_line)
800 {
801 g_signal_connect( G_OBJECT(m_buffer), "changed",
802 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
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 }
839 }
840 else
841 #endif
842
843 {
844 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
845 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
846 }
847
848 m_cursor = wxCursor( wxCURSOR_IBEAM );
849
850 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
851 SetDefaultStyle( attrDef );
852
853 return true;
854 }
855
856
857 void wxTextCtrl::CalculateScrollbar()
858 {
859 #ifndef __WXGTK20__
860 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
861
862 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
863
864 if (adj->upper - adj->page_size < 0.8)
865 {
866 if (m_vScrollbarVisible)
867 {
868 gtk_widget_hide( m_vScrollbar );
869 m_vScrollbarVisible = false;
870 }
871 }
872 else
873 {
874 if (!m_vScrollbarVisible)
875 {
876 gtk_widget_show( m_vScrollbar );
877 m_vScrollbarVisible = true;
878 }
879 }
880 #endif
881 }
882
883 wxString wxTextCtrl::GetValue() const
884 {
885 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
886
887 wxString tmp;
888 if (m_windowStyle & wxTE_MULTILINE)
889 {
890 #ifdef __WXGTK20__
891 GtkTextIter start;
892 gtk_text_buffer_get_start_iter( m_buffer, &start );
893 GtkTextIter end;
894 gtk_text_buffer_get_end_iter( m_buffer, &end );
895 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
896
897 #if wxUSE_UNICODE
898 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
899 #else
900 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
901 #endif
902 if ( buffer )
903 tmp = buffer;
904
905 g_free( text );
906 #else
907 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
908 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
909 tmp = text;
910 g_free( text );
911 #endif
912 }
913 else
914 {
915 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
916 }
917
918 return tmp;
919 }
920
921 void wxTextCtrl::SetValue( const wxString &value )
922 {
923 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
924
925 if (m_windowStyle & wxTE_MULTILINE)
926 {
927 #ifdef __WXGTK20__
928
929 #if wxUSE_UNICODE
930 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
931 #else
932 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
933 #endif
934 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
935 IgnoreNextTextUpdate();
936
937 if ( !buffer )
938 {
939 // what else can we do? at least don't crash...
940 return;
941 }
942
943 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
944
945 #else
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;
949 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
950 #endif
951 }
952 else
953 {
954 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
955 }
956
957 // GRG, Jun/2000: Changed this after a lot of discussion in
958 // the lists. wxWidgets 2.2 will have a set of flags to
959 // customize this behaviour.
960 SetInsertionPoint(0);
961
962 m_modified = false;
963 }
964
965 void wxTextCtrl::WriteText( const wxString &text )
966 {
967 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
968
969 if ( text.empty() )
970 return;
971
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
977 if ( m_windowStyle & wxTE_MULTILINE )
978 {
979 #ifdef __WXGTK20__
980
981 #if wxUSE_UNICODE
982 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
983 #else
984 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
985 #endif
986 if ( !buffer )
987 {
988 // what else can we do? at least don't crash...
989 return;
990 }
991
992 // TODO: Call whatever is needed to delete the selection.
993 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
994
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),
1000 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
1001 }
1002 #else // GTK 1.x
1003 // After cursor movements, gtk_text_get_point() is wrong by one.
1004 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
1005
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
1009 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
1010 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
1011
1012 // we called wxGtkTextInsert with correct font, no need to do anything
1013 // in UpdateFontIfNeeded() any longer
1014 if ( !text.empty() )
1015 {
1016 SetUpdateFont(false);
1017 }
1018
1019 // Bring editable's cursor back uptodate.
1020 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
1021 #endif // GTK 1.x/2.0
1022 }
1023 else // single line
1024 {
1025 // First remove the selection if there is one
1026 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
1027
1028 // This moves the cursor pos to behind the inserted text.
1029 gint len = GET_EDITABLE_POS(m_text);
1030
1031 #ifdef __WXGTK20__
1032
1033 #if wxUSE_UNICODE
1034 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
1035 #else
1036 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
1037 #endif
1038 if ( !buffer )
1039 {
1040 // what else can we do? at least don't crash...
1041 return;
1042 }
1043
1044 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
1045
1046 #else
1047 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
1048 #endif
1049
1050 // Bring entry's cursor uptodate.
1051 gtk_entry_set_position( GTK_ENTRY(m_text), len );
1052 }
1053
1054 m_modified = oldModified;
1055 }
1056
1057 void wxTextCtrl::AppendText( const wxString &text )
1058 {
1059 SetInsertionPointEnd();
1060 WriteText( text );
1061 }
1062
1063 wxString wxTextCtrl::GetLineText( long lineNo ) const
1064 {
1065 if (m_windowStyle & wxTE_MULTILINE)
1066 {
1067 #ifndef __WXGTK20__
1068 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1069 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
1070
1071 if (text)
1072 {
1073 wxString buf;
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];
1083
1084 g_free( text );
1085 return buf;
1086 }
1087 else
1088 {
1089 return wxEmptyString;
1090 }
1091 #else
1092 GtkTextIter line;
1093 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
1094 GtkTextIter end;
1095 gtk_text_buffer_get_end_iter(m_buffer,&end );
1096 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
1097 wxString result(wxGTK_CONV_BACK(text));
1098 g_free(text);
1099 return result.BeforeFirst(wxT('\n'));
1100 #endif
1101 }
1102 else
1103 {
1104 if (lineNo == 0) return GetValue();
1105 return wxEmptyString;
1106 }
1107 }
1108
1109 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
1110 {
1111 /* If you implement this, don't forget to update the documentation!
1112 * (file docs/latex/wx/text.tex) */
1113 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
1114 }
1115
1116 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
1117 {
1118 if ( m_windowStyle & wxTE_MULTILINE )
1119 {
1120 wxString text = GetValue();
1121
1122 // cast to prevent warning. But pos really should've been unsigned.
1123 if( (unsigned long)pos > text.Len() )
1124 return false;
1125
1126 *x=0; // First Col
1127 *y=0; // First Line
1128
1129 const wxChar* stop = text.c_str() + pos;
1130 for ( const wxChar *p = text.c_str(); p < stop; p++ )
1131 {
1132 if (*p == wxT('\n'))
1133 {
1134 (*y)++;
1135 *x=0;
1136 }
1137 else
1138 (*x)++;
1139 }
1140 }
1141 else // single line control
1142 {
1143 if ( pos <= GTK_ENTRY(m_text)->text_length )
1144 {
1145 *y = 0;
1146 *x = pos;
1147 }
1148 else
1149 {
1150 // index out of bounds
1151 return false;
1152 }
1153 }
1154
1155 return true;
1156 }
1157
1158 long wxTextCtrl::XYToPosition(long x, long y ) const
1159 {
1160 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
1161
1162 long pos=0;
1163 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
1164
1165 pos += x;
1166 return pos;
1167 }
1168
1169 int wxTextCtrl::GetLineLength(long lineNo) const
1170 {
1171 wxString str = GetLineText (lineNo);
1172 return (int) str.Length();
1173 }
1174
1175 int wxTextCtrl::GetNumberOfLines() const
1176 {
1177 if (m_windowStyle & wxTE_MULTILINE)
1178 {
1179 #ifdef __WXGTK20__
1180 return gtk_text_buffer_get_line_count( m_buffer );
1181 #else
1182 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
1183 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
1184
1185 if (text)
1186 {
1187 int currentLine = 0;
1188 for (int i = 0; i < len; i++ )
1189 {
1190 if (text[i] == '\n')
1191 currentLine++;
1192 }
1193 g_free( text );
1194
1195 // currentLine is 0 based, add 1 to get number of lines
1196 return currentLine + 1;
1197 }
1198 else
1199 {
1200 return 0;
1201 }
1202 #endif
1203 }
1204 else
1205 {
1206 return 1;
1207 }
1208 }
1209
1210 void wxTextCtrl::SetInsertionPoint( long pos )
1211 {
1212 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1213
1214 if ( IsMultiLine() )
1215 {
1216 #ifdef __WXGTK20__
1217 GtkTextIter iter;
1218 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
1219 gtk_text_buffer_place_cursor( m_buffer, &iter );
1220 gtk_text_view_scroll_mark_onscreen
1221 (
1222 GTK_TEXT_VIEW(m_text),
1223 gtk_text_buffer_get_insert( m_buffer )
1224 );
1225 #else // GTK+ 1.x
1226 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
1227 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
1228
1229 /* we fake a set_point by inserting and deleting. as the user
1230 isn't supposed to get to know about this non-sense, we
1231 disconnect so that no events are sent to the user program. */
1232
1233 gint tmp = (gint)pos;
1234 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
1235 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
1236
1237 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
1238 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
1239
1240 // bring editable's cursor uptodate. Bug in GTK.
1241 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
1242 #endif // GTK+ 2/1
1243 }
1244 else
1245 {
1246 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
1247
1248 // Bring editable's cursor uptodate. Bug in GTK.
1249 SET_EDITABLE_POS(m_text, (guint32)pos);
1250 }
1251 }
1252
1253 void wxTextCtrl::SetInsertionPointEnd()
1254 {
1255 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1256
1257 if (m_windowStyle & wxTE_MULTILINE)
1258 {
1259 #ifdef __WXGTK20__
1260 GtkTextIter end;
1261 gtk_text_buffer_get_end_iter( m_buffer, &end );
1262 gtk_text_buffer_place_cursor( m_buffer, &end );
1263 #else
1264 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
1265 #endif
1266 }
1267 else
1268 {
1269 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
1270 }
1271 }
1272
1273 void wxTextCtrl::SetEditable( bool editable )
1274 {
1275 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1276
1277 if (m_windowStyle & wxTE_MULTILINE)
1278 {
1279 #ifdef __WXGTK20__
1280 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
1281 #else
1282 gtk_text_set_editable( GTK_TEXT(m_text), editable );
1283 #endif
1284 }
1285 else
1286 {
1287 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
1288 }
1289 }
1290
1291 bool wxTextCtrl::Enable( bool enable )
1292 {
1293 if (!wxWindowBase::Enable(enable))
1294 {
1295 // nothing to do
1296 return false;
1297 }
1298
1299 if (m_windowStyle & wxTE_MULTILINE)
1300 {
1301 #ifdef __WXGTK20__
1302 SetEditable( enable );
1303 #else
1304 gtk_text_set_editable( GTK_TEXT(m_text), enable );
1305 OnParentEnable(enable);
1306 #endif
1307 }
1308 else
1309 {
1310 gtk_widget_set_sensitive( m_text, enable );
1311 }
1312
1313 return true;
1314 }
1315
1316 // wxGTK-specific: called recursively by Enable,
1317 // to give widgets an oppprtunity to correct their colours after they
1318 // have been changed by Enable
1319 void wxTextCtrl::OnParentEnable( bool enable )
1320 {
1321 // If we have a custom background colour, we use this colour in both
1322 // disabled and enabled mode, or we end up with a different colour under the
1323 // text.
1324 wxColour oldColour = GetBackgroundColour();
1325 if (oldColour.Ok())
1326 {
1327 // Need to set twice or it'll optimize the useful stuff out
1328 if (oldColour == * wxWHITE)
1329 SetBackgroundColour(*wxBLACK);
1330 else
1331 SetBackgroundColour(*wxWHITE);
1332 SetBackgroundColour(oldColour);
1333 }
1334 }
1335
1336 void wxTextCtrl::MarkDirty()
1337 {
1338 m_modified = true;
1339 }
1340
1341 void wxTextCtrl::DiscardEdits()
1342 {
1343 m_modified = false;
1344 }
1345
1346 // ----------------------------------------------------------------------------
1347 // max text length support
1348 // ----------------------------------------------------------------------------
1349
1350 void wxTextCtrl::IgnoreNextTextUpdate()
1351 {
1352 m_ignoreNextUpdate = true;
1353 }
1354
1355 bool wxTextCtrl::IgnoreTextUpdate()
1356 {
1357 if ( m_ignoreNextUpdate )
1358 {
1359 m_ignoreNextUpdate = false;
1360
1361 return true;
1362 }
1363
1364 return false;
1365 }
1366
1367 void wxTextCtrl::SetMaxLength(unsigned long len)
1368 {
1369 if ( !HasFlag(wxTE_MULTILINE) )
1370 {
1371 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
1372
1373 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1374 // we had tried to enter more text than allowed by max text length and
1375 // the text wasn't really changed
1376 //
1377 // to detect this and generate TEXT_MAXLEN event instead of
1378 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1379 //
1380 // when max len is set to 0 we disconnect our handler as it means that
1381 // we shouldn't check anything any more
1382 if ( len )
1383 {
1384 gtk_signal_connect( GTK_OBJECT(m_text),
1385 "insert_text",
1386 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1387 (gpointer)this);
1388 }
1389 else // no checking
1390 {
1391 gtk_signal_disconnect_by_func
1392 (
1393 GTK_OBJECT(m_text),
1394 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1395 (gpointer)this
1396 );
1397 }
1398 }
1399 }
1400
1401 void wxTextCtrl::SetSelection( long from, long to )
1402 {
1403 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1404
1405 if (from == -1 && to == -1)
1406 {
1407 from = 0;
1408 to = GetValue().Length();
1409 }
1410
1411 #ifndef __WXGTK20__
1412 if ( (m_windowStyle & wxTE_MULTILINE) &&
1413 !GTK_TEXT(m_text)->line_start_cache )
1414 {
1415 // tell the programmer that it didn't work
1416 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1417 return;
1418 }
1419 #endif
1420
1421 if (m_windowStyle & wxTE_MULTILINE)
1422 {
1423 #ifdef __WXGTK20__
1424 GtkTextIter fromi, toi;
1425 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1426 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1427
1428 gtk_text_buffer_place_cursor( m_buffer, &toi );
1429 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
1430 #else
1431 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1432 #endif
1433 }
1434 else
1435 {
1436 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1437 }
1438 }
1439
1440 void wxTextCtrl::ShowPosition( long pos )
1441 {
1442 if (m_windowStyle & wxTE_MULTILINE)
1443 {
1444 #ifdef __WXGTK20__
1445 GtkTextIter iter;
1446 gtk_text_buffer_get_start_iter( m_buffer, &iter );
1447 gtk_text_iter_set_offset( &iter, pos );
1448 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
1449 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1450 #else // GTK 1.x
1451 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1452 float totalLines = (float) GetNumberOfLines();
1453 long posX;
1454 long posY;
1455 PositionToXY(pos, &posX, &posY);
1456 float posLine = (float) posY;
1457 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1458 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
1459 #endif // GTK 1.x/2.x
1460 }
1461 }
1462
1463 #ifdef __WXGTK20__
1464
1465 wxTextCtrlHitTestResult
1466 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1467 {
1468 if ( !IsMultiLine() )
1469 {
1470 // not supported
1471 return wxTE_HT_UNKNOWN;
1472 }
1473
1474 int x, y;
1475 gtk_text_view_window_to_buffer_coords
1476 (
1477 GTK_TEXT_VIEW(m_text),
1478 GTK_TEXT_WINDOW_TEXT,
1479 pt.x, pt.y,
1480 &x, &y
1481 );
1482
1483 GtkTextIter iter;
1484 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1485 if ( pos )
1486 *pos = gtk_text_iter_get_offset(&iter);
1487
1488 return wxTE_HT_ON_TEXT;
1489 }
1490
1491 #endif // __WXGTK20__
1492
1493 long wxTextCtrl::GetInsertionPoint() const
1494 {
1495 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1496
1497 #ifdef __WXGTK20__
1498 if (m_windowStyle & wxTE_MULTILINE)
1499 {
1500 // There is no direct accessor for the cursor, but
1501 // internally, the cursor is the "mark" called
1502 // "insert" in the text view's btree structure.
1503
1504 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1505 GtkTextIter cursor;
1506 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1507
1508 return gtk_text_iter_get_offset( &cursor );
1509 }
1510 else
1511 #endif
1512 {
1513 return (long) GET_EDITABLE_POS(m_text);
1514 }
1515 }
1516
1517 wxTextPos wxTextCtrl::GetLastPosition() const
1518 {
1519 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1520
1521 int pos = 0;
1522
1523 if (m_windowStyle & wxTE_MULTILINE)
1524 {
1525 #ifdef __WXGTK20__
1526 GtkTextIter end;
1527 gtk_text_buffer_get_end_iter( m_buffer, &end );
1528
1529 pos = gtk_text_iter_get_offset( &end );
1530 #else
1531 pos = gtk_text_get_length( GTK_TEXT(m_text) );
1532 #endif
1533 }
1534 else
1535 {
1536 pos = GTK_ENTRY(m_text)->text_length;
1537 }
1538
1539 return (long)pos;
1540 }
1541
1542 void wxTextCtrl::Remove( long from, long to )
1543 {
1544 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1545
1546 #ifdef __WXGTK20__
1547 if (m_windowStyle & wxTE_MULTILINE)
1548 {
1549 GtkTextIter fromi, toi;
1550 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1551 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1552
1553 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1554 }
1555 else // single line
1556 #endif
1557 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1558 }
1559
1560 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1561 {
1562 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1563
1564 Remove( from, to );
1565
1566 if (!value.empty())
1567 {
1568 #ifdef __WXGTK20__
1569 SetInsertionPoint( from );
1570 WriteText( value );
1571 #else // GTK 1.x
1572 gint pos = (gint)from;
1573 #if wxUSE_UNICODE
1574 wxWX2MBbuf buf = value.mbc_str();
1575 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
1576 #else
1577 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
1578 #endif // wxUSE_UNICODE
1579 #endif // GTK 1.x/2.x
1580 }
1581 }
1582
1583 void wxTextCtrl::Cut()
1584 {
1585 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1586
1587 #ifdef __WXGTK20__
1588 if (m_windowStyle & wxTE_MULTILINE)
1589 g_signal_emit_by_name(m_text, "cut-clipboard");
1590 else
1591 #endif
1592 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1593 }
1594
1595 void wxTextCtrl::Copy()
1596 {
1597 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1598
1599 #ifdef __WXGTK20__
1600 if (m_windowStyle & wxTE_MULTILINE)
1601 g_signal_emit_by_name(m_text, "copy-clipboard");
1602 else
1603 #endif
1604 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1605 }
1606
1607 void wxTextCtrl::Paste()
1608 {
1609 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1610
1611 #ifdef __WXGTK20__
1612 if (m_windowStyle & wxTE_MULTILINE)
1613 g_signal_emit_by_name(m_text, "paste-clipboard");
1614 else
1615 #endif
1616 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1617 }
1618
1619 // Undo/redo
1620 void wxTextCtrl::Undo()
1621 {
1622 // TODO
1623 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1624 }
1625
1626 void wxTextCtrl::Redo()
1627 {
1628 // TODO
1629 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1630 }
1631
1632 bool wxTextCtrl::CanUndo() const
1633 {
1634 // TODO
1635 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1636 return false;
1637 }
1638
1639 bool wxTextCtrl::CanRedo() const
1640 {
1641 // TODO
1642 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1643 return false;
1644 }
1645
1646 // If the return values from and to are the same, there is no
1647 // selection.
1648 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1649 {
1650 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1651
1652 gint from = -1;
1653 gint to = -1;
1654 bool haveSelection = false;
1655
1656 #ifdef __WXGTK20__
1657 if (m_windowStyle & wxTE_MULTILINE)
1658 {
1659 GtkTextIter ifrom, ito;
1660 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1661 {
1662 haveSelection = true;
1663 from = gtk_text_iter_get_offset(&ifrom);
1664 to = gtk_text_iter_get_offset(&ito);
1665 }
1666 }
1667 else // not multi-line
1668 {
1669 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1670 &from, &to) )
1671 {
1672 haveSelection = true;
1673 }
1674 }
1675 #else // not GTK2
1676 if ( (GTK_EDITABLE(m_text)->has_selection) )
1677 {
1678 haveSelection = true;
1679 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1680 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1681 }
1682 #endif
1683
1684 if (! haveSelection )
1685 from = to = GetInsertionPoint();
1686
1687 if ( from > to )
1688 {
1689 // exchange them to be compatible with wxMSW
1690 gint tmp = from;
1691 from = to;
1692 to = tmp;
1693 }
1694
1695 if ( fromOut )
1696 *fromOut = from;
1697 if ( toOut )
1698 *toOut = to;
1699 }
1700
1701
1702 bool wxTextCtrl::IsEditable() const
1703 {
1704 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1705
1706 #ifdef __WXGTK20__
1707 if (m_windowStyle & wxTE_MULTILINE)
1708 {
1709 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1710 }
1711 else
1712 {
1713 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1714 }
1715 #else
1716 return GTK_EDITABLE(m_text)->editable;
1717 #endif
1718 }
1719
1720 bool wxTextCtrl::IsModified() const
1721 {
1722 return m_modified;
1723 }
1724
1725 void wxTextCtrl::Clear()
1726 {
1727 SetValue( wxEmptyString );
1728 }
1729
1730 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1731 {
1732 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1733
1734 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1735 {
1736 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1737 event.SetEventObject(this);
1738 event.SetString(GetValue());
1739 if (GetEventHandler()->ProcessEvent(event)) return;
1740 }
1741
1742 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1743 {
1744 // This will invoke the dialog default action, such
1745 // as the clicking the default button.
1746
1747 wxWindow *top_frame = m_parent;
1748 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1749 top_frame = top_frame->GetParent();
1750
1751 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1752 {
1753 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1754
1755 if (window->default_widget)
1756 {
1757 gtk_widget_activate (window->default_widget);
1758 return;
1759 }
1760 }
1761 }
1762
1763 key_event.Skip();
1764 }
1765
1766 GtkWidget* wxTextCtrl::GetConnectWidget()
1767 {
1768 return GTK_WIDGET(m_text);
1769 }
1770
1771 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1772 {
1773 if (m_windowStyle & wxTE_MULTILINE)
1774 {
1775 #ifdef __WXGTK20__
1776 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1777 #else
1778 return (window == GTK_TEXT(m_text)->text_area);
1779 #endif
1780 }
1781 else
1782 {
1783 return (window == GTK_ENTRY(m_text)->text_area);
1784 }
1785 }
1786
1787 // the font will change for subsequent text insertiongs
1788 bool wxTextCtrl::SetFont( const wxFont &font )
1789 {
1790 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1791
1792 if ( !wxTextCtrlBase::SetFont(font) )
1793 {
1794 // font didn't change, nothing to do
1795 return false;
1796 }
1797
1798 if ( m_windowStyle & wxTE_MULTILINE )
1799 {
1800 SetUpdateFont(true);
1801
1802 m_defaultStyle.SetFont(font);
1803
1804 ChangeFontGlobally();
1805 }
1806
1807 return true;
1808 }
1809
1810 void wxTextCtrl::ChangeFontGlobally()
1811 {
1812 // this method is very inefficient and hence should be called as rarely as
1813 // possible!
1814 //
1815 // TODO: it can be implemented much more efficiently for GTK2
1816 #ifndef __WXGTK20__
1817 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1818
1819 _T("shouldn't be called for single line controls") );
1820 #else
1821 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1822 _T("shouldn't be called for single line controls") );
1823 #endif
1824
1825 wxString value = GetValue();
1826 if ( !value.empty() )
1827 {
1828 SetUpdateFont(false);
1829
1830 Clear();
1831 AppendText(value);
1832 }
1833 }
1834
1835 #ifndef __WXGTK20__
1836
1837 void wxTextCtrl::UpdateFontIfNeeded()
1838 {
1839 if ( m_updateFont )
1840 ChangeFontGlobally();
1841 }
1842
1843 #endif // GTK+ 1.x
1844
1845 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1846 {
1847 if ( !wxControl::SetForegroundColour(colour) )
1848 return false;
1849
1850 // update default fg colour too
1851 m_defaultStyle.SetTextColour(colour);
1852
1853 return true;
1854 }
1855
1856 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1857 {
1858 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1859
1860 if ( !wxControl::SetBackgroundColour( colour ) )
1861 return false;
1862
1863 #ifndef __WXGTK20__
1864 if (!m_widget->window)
1865 return false;
1866 #endif
1867
1868 if (!m_backgroundColour.Ok())
1869 return false;
1870
1871 if (m_windowStyle & wxTE_MULTILINE)
1872 {
1873 #ifndef __WXGTK20__
1874 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1875 if (!window)
1876 return false;
1877 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1878 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1879 gdk_window_clear( window );
1880 #endif
1881 }
1882
1883 // change active background color too
1884 m_defaultStyle.SetBackgroundColour( colour );
1885
1886 return true;
1887 }
1888
1889 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1890 {
1891 if ( m_windowStyle & wxTE_MULTILINE )
1892 {
1893 if ( style.IsDefault() )
1894 {
1895 // nothing to do
1896 return true;
1897 }
1898
1899 #ifdef __WXGTK20__
1900 gint l = gtk_text_buffer_get_char_count( m_buffer );
1901
1902 wxCHECK_MSG( start >= 0 && end <= l, false,
1903 _T("invalid range in wxTextCtrl::SetStyle") );
1904
1905 GtkTextIter starti, endi;
1906 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1907 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1908
1909 // use the attributes from style which are set in it and fall back
1910 // first to the default style and then to the text control default
1911 // colours for the others
1912 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1913
1914 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
1915 #else
1916 // VERY dirty way to do that - removes the required text and re-adds it
1917 // with styling (FIXME)
1918
1919 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1920
1921 wxCHECK_MSG( start >= 0 && end <= l, false,
1922 _T("invalid range in wxTextCtrl::SetStyle") );
1923
1924 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1925 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1926 wxString tmp(text,*wxConvCurrent);
1927 g_free( text );
1928
1929 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1930 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1931
1932 #if wxUSE_UNICODE
1933 wxWX2MBbuf buf = tmp.mbc_str();
1934 const char *txt = buf;
1935 size_t txtlen = strlen(buf);
1936 #else
1937 const char *txt = tmp;
1938 size_t txtlen = tmp.length();
1939 #endif
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 wxGtkTextInsert(m_text,
1945 wxTextAttr::Combine(style, m_defaultStyle, this),
1946 txt,
1947 txtlen);
1948
1949 /* does not seem to help under GTK+ 1.2 !!!
1950 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1951 SetInsertionPoint( old_pos );
1952 #endif
1953
1954 return true;
1955 }
1956
1957 // else single line
1958 // cannot do this for GTK+'s Entry widget
1959 return false;
1960 }
1961
1962 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1963 {
1964 gtk_widget_modify_style(m_text, style);
1965 }
1966
1967 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1968 {
1969 Cut();
1970 }
1971
1972 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1973 {
1974 Copy();
1975 }
1976
1977 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1978 {
1979 Paste();
1980 }
1981
1982 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1983 {
1984 Undo();
1985 }
1986
1987 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1988 {
1989 Redo();
1990 }
1991
1992 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1993 {
1994 event.Enable( CanCut() );
1995 }
1996
1997 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1998 {
1999 event.Enable( CanCopy() );
2000 }
2001
2002 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2003 {
2004 event.Enable( CanPaste() );
2005 }
2006
2007 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2008 {
2009 event.Enable( CanUndo() );
2010 }
2011
2012 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2013 {
2014 event.Enable( CanRedo() );
2015 }
2016
2017 void wxTextCtrl::OnInternalIdle()
2018 {
2019 wxCursor cursor = m_cursor;
2020 if (g_globalCursor.Ok()) cursor = g_globalCursor;
2021
2022 if (cursor.Ok())
2023 {
2024 #ifndef __WXGTK20__
2025 GdkWindow *window = (GdkWindow*) NULL;
2026 if (HasFlag(wxTE_MULTILINE))
2027 window = GTK_TEXT(m_text)->text_area;
2028 else
2029 window = GTK_ENTRY(m_text)->text_area;
2030
2031 if (window)
2032 gdk_window_set_cursor( window, cursor.GetCursor() );
2033
2034 if (!g_globalCursor.Ok())
2035 cursor = *wxSTANDARD_CURSOR;
2036
2037 window = m_widget->window;
2038 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
2039 gdk_window_set_cursor( window, cursor.GetCursor() );
2040 #endif
2041 }
2042
2043 if (g_delayedFocus == this)
2044 {
2045 if (GTK_WIDGET_REALIZED(m_widget))
2046 {
2047 gtk_widget_grab_focus( m_widget );
2048 g_delayedFocus = NULL;
2049 }
2050 }
2051
2052 if (wxUpdateUIEvent::CanUpdate(this))
2053 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2054 }
2055
2056 wxSize wxTextCtrl::DoGetBestSize() const
2057 {
2058 // FIXME should be different for multi-line controls...
2059 wxSize ret( wxControl::DoGetBestSize() );
2060 wxSize best(80, ret.y);
2061 CacheBestSize(best);
2062 return best;
2063 }
2064
2065 // ----------------------------------------------------------------------------
2066 // freeze/thaw
2067 // ----------------------------------------------------------------------------
2068
2069 void wxTextCtrl::Freeze()
2070 {
2071 if ( HasFlag(wxTE_MULTILINE) )
2072 {
2073 #ifdef __WXGTK20__
2074 if ( !m_frozenness++ )
2075 {
2076 // freeze textview updates and remove buffer
2077 g_signal_connect( G_OBJECT(m_text), "expose_event",
2078 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2079 g_signal_connect( G_OBJECT(m_widget), "expose_event",
2080 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
2081 gtk_widget_set_sensitive(m_widget, false);
2082 g_object_ref(m_buffer);
2083 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
2084 }
2085 #else
2086 gtk_text_freeze(GTK_TEXT(m_text));
2087 #endif
2088 }
2089 }
2090
2091 void wxTextCtrl::Thaw()
2092 {
2093 if ( HasFlag(wxTE_MULTILINE) )
2094 {
2095 #ifdef __WXGTK20__
2096 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2097
2098 if ( !--m_frozenness )
2099 {
2100 // Reattach buffer and thaw textview updates
2101 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
2102 g_object_unref(m_buffer);
2103 gtk_widget_set_sensitive(m_widget, true);
2104 g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this);
2105 g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this);
2106 }
2107 #else
2108 GTK_TEXT(m_text)->vadj->value = 0.0;
2109
2110 gtk_text_thaw(GTK_TEXT(m_text));
2111 #endif
2112 }
2113 }
2114
2115 // ----------------------------------------------------------------------------
2116 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
2117 // ----------------------------------------------------------------------------
2118
2119 #ifdef __WXGTK20__
2120
2121 // FIXME: when dragging on a link the sample gets an "Unknown event".
2122 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
2123 // a buggy sample, or something else
2124 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
2125 {
2126 event.Skip();
2127 if(!(m_windowStyle & wxTE_AUTO_URL))
2128 return;
2129
2130 gint x, y;
2131 GtkTextIter start, end;
2132 GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer),
2133 "wxUrl");
2134
2135 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text), GTK_TEXT_WINDOW_WIDGET,
2136 event.GetX(), event.GetY(), &x, &y);
2137
2138 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &end, x, y);
2139 if (!gtk_text_iter_has_tag(&end, tag))
2140 {
2141 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2142 GTK_TEXT_WINDOW_TEXT), m_gdkXTermCursor);
2143 return;
2144 }
2145
2146 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text),
2147 GTK_TEXT_WINDOW_TEXT), m_gdkHandCursor);
2148
2149 start = end;
2150 if(!gtk_text_iter_begins_tag(&start, tag))
2151 gtk_text_iter_backward_to_tag_toggle(&start, tag);
2152 if(!gtk_text_iter_ends_tag(&end, tag))
2153 gtk_text_iter_forward_to_tag_toggle(&end, tag);
2154
2155 // Native context menu is probably not desired on an URL.
2156 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
2157 if(event.GetEventType() == wxEVT_RIGHT_DOWN)
2158 event.Skip(false);
2159
2160 wxTextUrlEvent url_event(m_windowId, event,
2161 gtk_text_iter_get_offset(&start),
2162 gtk_text_iter_get_offset(&end));
2163
2164 InitCommandEvent(url_event);
2165 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
2166 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
2167 GetEventHandler()->ProcessEvent(url_event);
2168 }
2169 #endif // gtk2
2170
2171 // ----------------------------------------------------------------------------
2172 // scrolling
2173 // ----------------------------------------------------------------------------
2174
2175 GtkAdjustment *wxTextCtrl::GetVAdj() const
2176 {
2177 if ( !IsMultiLine() )
2178 return NULL;
2179
2180 #ifdef __WXGTK20__
2181 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
2182 #else
2183 return GTK_TEXT(m_text)->vadj;
2184 #endif
2185 }
2186
2187 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
2188 {
2189 float value = adj->value + diff;
2190
2191 if ( value < 0 )
2192 value = 0;
2193
2194 float upper = adj->upper - adj->page_size;
2195 if ( value > upper )
2196 value = upper;
2197
2198 // did we noticeably change the scroll position?
2199 if ( fabs(adj->value - value) < 0.2 )
2200 {
2201 // well, this is what Robert does in wxScrollBar, so it must be good...
2202 return false;
2203 }
2204
2205 adj->value = value;
2206
2207 #ifdef __WXGTK20__
2208 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
2209 #else
2210 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
2211 #endif
2212
2213 return true;
2214 }
2215
2216 bool wxTextCtrl::ScrollLines(int lines)
2217 {
2218 GtkAdjustment *adj = GetVAdj();
2219 if ( !adj )
2220 return false;
2221
2222 #ifdef __WXGTK20__
2223 int diff = (int)ceil(lines*adj->step_increment);
2224 #else
2225 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
2226 int diff = 10*lines;
2227 #endif
2228
2229 return DoScroll(adj, diff);
2230 }
2231
2232 bool wxTextCtrl::ScrollPages(int pages)
2233 {
2234 GtkAdjustment *adj = GetVAdj();
2235 if ( !adj )
2236 return false;
2237
2238 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
2239 }
2240
2241
2242 // static
2243 wxVisualAttributes
2244 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2245 {
2246 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
2247 }