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