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