Applied wxNO_BORDER patch for text control and combo box.
[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 bool g_blockEventsOnDrag;
46 extern wxCursor g_globalCursor;
47 extern wxWindowGTK *g_delayedFocus;
48
49 // ----------------------------------------------------------------------------
50 // helpers
51 // ----------------------------------------------------------------------------
52
53 #ifdef __WXGTK20__
54 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
55 const wxTextAttr& attr,
56 GtkTextIter *start,
57 GtkTextIter *end)
58 {
59 static gchar buf[1024];
60 GtkTextTag *tag;
61
62 if (attr.HasFont())
63 {
64 char *font_string;
65 PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
66 font_string = pango_font_description_to_string(font_description);
67 g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
68 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
69 buf );
70 if (!tag)
71 tag = gtk_text_buffer_create_tag( text_buffer, buf,
72 "font-desc", font_description,
73 NULL );
74 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
75 g_free (font_string);
76 }
77
78 if (attr.HasTextColour())
79 {
80 GdkColor *colFg = attr.GetTextColour().GetColor();
81 g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
82 colFg->red, colFg->green, colFg->blue);
83 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
84 buf );
85 if (!tag)
86 tag = gtk_text_buffer_create_tag( text_buffer, buf,
87 "foreground-gdk", colFg, NULL );
88 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
89 }
90
91 if (attr.HasBackgroundColour())
92 {
93 GdkColor *colBg = attr.GetBackgroundColour().GetColor();
94 g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
95 colBg->red, colBg->green, colBg->blue);
96 tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
97 buf );
98 if (!tag)
99 tag = gtk_text_buffer_create_tag( text_buffer, buf,
100 "background-gdk", colBg, NULL );
101 gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
102 }
103 }
104
105 static void wxGtkTextInsert(GtkWidget *text,
106 GtkTextBuffer *text_buffer,
107 const wxTextAttr& attr,
108 wxCharBuffer buffer)
109
110 {
111 gint start_offset;
112 GtkTextIter iter, start;
113
114 gtk_text_buffer_get_iter_at_mark( text_buffer, &iter,
115 gtk_text_buffer_get_insert (text_buffer) );
116 start_offset = gtk_text_iter_get_offset (&iter);
117 gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) );
118
119 gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
120
121 wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
122 }
123 #else
124 static void wxGtkTextInsert(GtkWidget *text,
125 const wxTextAttr& attr,
126 const char *txt,
127 size_t len)
128 {
129 GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont()
130 : NULL;
131
132 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
133 : NULL;
134
135 GdkColor *colBg = attr.HasBackgroundColour()
136 ? attr.GetBackgroundColour().GetColor()
137 : NULL;
138
139 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
140 }
141 #endif // GTK 1.x
142
143 // ----------------------------------------------------------------------------
144 // "insert_text" for GtkEntry
145 // ----------------------------------------------------------------------------
146
147 static void
148 gtk_insert_text_callback(GtkEditable *editable,
149 const gchar *new_text,
150 gint new_text_length,
151 gint *position,
152 wxTextCtrl *win)
153 {
154 if (g_isIdle)
155 wxapp_install_idle_handler();
156
157 // we should only be called if we have a max len limit at all
158 GtkEntry *entry = GTK_ENTRY (editable);
159
160 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
161
162 // check that we don't overflow the max length limit
163 //
164 // FIXME: this doesn't work when we paste a string which is going to be
165 // truncated
166 if ( entry->text_length == entry->text_max_length )
167 {
168 // we don't need to run the base class version at all
169 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
170
171 // remember that the next changed signal is to be ignored to avoid
172 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
173 win->IgnoreNextTextUpdate();
174
175 // and generate the correct one ourselves
176 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
177 event.SetEventObject(win);
178 event.SetString(win->GetValue());
179 win->GetEventHandler()->ProcessEvent( event );
180 }
181 }
182
183 //-----------------------------------------------------------------------------
184 // "changed"
185 //-----------------------------------------------------------------------------
186
187 static void
188 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
189 {
190 if ( win->IgnoreTextUpdate() )
191 return;
192
193 if (!win->m_hasVMT) return;
194
195 if (g_isIdle)
196 wxapp_install_idle_handler();
197
198 win->SetModified();
199 #ifndef __WXGTK20__
200 win->UpdateFontIfNeeded();
201 #endif // !__WXGTK20__
202
203 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
204 event.SetEventObject( win );
205 event.SetString( win->GetValue() );
206 win->GetEventHandler()->ProcessEvent( event );
207 }
208
209 //-----------------------------------------------------------------------------
210 // "expose_event" from scrolled window and textview
211 //-----------------------------------------------------------------------------
212
213 #ifdef __WXGTK20__
214 static gboolean
215 gtk_text_exposed_callback( GtkWidget *widget, GdkEventExpose *event, wxTextCtrl *win )
216 {
217 return TRUE;
218 }
219 #endif
220
221 //-----------------------------------------------------------------------------
222 // "changed" from vertical scrollbar
223 //-----------------------------------------------------------------------------
224
225 #ifndef __WXGTK20__
226 static void
227 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
228 {
229 if (!win->m_hasVMT) return;
230
231 if (g_isIdle)
232 wxapp_install_idle_handler();
233
234 win->CalculateScrollbar();
235 }
236 #endif
237
238 // ----------------------------------------------------------------------------
239 // redraw callback for multiline text
240 // ----------------------------------------------------------------------------
241
242 #ifndef __WXGTK20__
243
244 // redrawing a GtkText from inside a wxYield() call results in crashes (the
245 // text sample shows it in its "Add lines" command which shows wxProgressDialog
246 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
247 // don't do anything if we're inside wxYield()
248
249 extern bool wxIsInsideYield;
250
251 extern "C" {
252 typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect);
253 }
254
255 static GtkDrawCallback gs_gtk_text_draw = NULL;
256
257 extern "C"
258 void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect)
259 {
260 if ( !wxIsInsideYield )
261 {
262 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
263 _T("infinite recursion in wxgtk_text_draw aborted") );
264
265 gs_gtk_text_draw(widget, rect);
266 }
267 }
268
269 #endif // __WXGTK20__
270
271 //-----------------------------------------------------------------------------
272 // wxTextCtrl
273 //-----------------------------------------------------------------------------
274
275 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
276
277 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
278 EVT_CHAR(wxTextCtrl::OnChar)
279
280 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
281 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
282 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
283 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
284 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
285
286 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
287 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
288 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
289 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
290 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
291 END_EVENT_TABLE()
292
293 void wxTextCtrl::Init()
294 {
295 m_ignoreNextUpdate =
296 m_modified = FALSE;
297 SetUpdateFont(FALSE);
298 m_text =
299 m_vScrollbar = (GtkWidget *)NULL;
300 #ifdef __WXGTK20__
301 m_frozenness = 0;
302 #endif
303 }
304
305 wxTextCtrl::wxTextCtrl( wxWindow *parent,
306 wxWindowID id,
307 const wxString &value,
308 const wxPoint &pos,
309 const wxSize &size,
310 long style,
311 const wxValidator& validator,
312 const wxString &name )
313 {
314 Init();
315
316 Create( parent, id, value, pos, size, style, validator, name );
317 }
318
319 bool wxTextCtrl::Create( wxWindow *parent,
320 wxWindowID id,
321 const wxString &value,
322 const wxPoint &pos,
323 const wxSize &size,
324 long style,
325 const wxValidator& validator,
326 const wxString &name )
327 {
328 m_needParent = TRUE;
329 m_acceptsFocus = TRUE;
330
331 if (!PreCreation( parent, pos, size ) ||
332 !CreateBase( parent, id, pos, size, style, validator, name ))
333 {
334 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
335 return FALSE;
336 }
337
338
339 m_vScrollbarVisible = FALSE;
340
341 bool multi_line = (style & wxTE_MULTILINE) != 0;
342
343 if (multi_line)
344 {
345 #ifdef __WXGTK20__
346 // Create view
347 m_text = gtk_text_view_new();
348
349 m_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) );
350
351 // create scrolled window
352 m_widget = gtk_scrolled_window_new( NULL, NULL );
353 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ),
354 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
355
356 // Insert view into scrolled window
357 gtk_container_add( GTK_CONTAINER(m_widget), m_text );
358
359 // Global settings which can be overridden by tags, I guess.
360 if (HasFlag( wxHSCROLL ) || HasFlag( wxTE_DONTWRAP ))
361 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE );
362 else
363 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD );
364
365 if (!HasFlag(wxNO_BORDER))
366 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN );
367
368 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
369 #else
370 // create our control ...
371 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
372
373 // ... and put into the upper left hand corner of the table
374 bool bHasHScrollbar = FALSE;
375 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
376 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
377 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
378 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
379 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
380 0, 0);
381
382 // always wrap words
383 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
384
385 // finally, put the vertical scrollbar in the upper right corner
386 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
387 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
388 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
389 GTK_FILL,
390 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
391 0, 0);
392 #endif
393 }
394 else
395 {
396 // a single-line text control: no need for scrollbars
397 m_widget =
398 m_text = gtk_entry_new();
399
400 if (style & wxNO_BORDER)
401 g_object_set( GTK_ENTRY(m_text), "has-frame", FALSE, NULL );
402 }
403
404 m_parent->DoAddChild( this );
405
406 m_focusWidget = m_text;
407
408 PostCreation(size);
409
410 if (multi_line)
411 gtk_widget_show(m_text);
412
413 #ifndef __WXGTK20__
414 if (multi_line)
415 {
416 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
417 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
418
419 // only initialize gs_gtk_text_draw once, starting from the next the
420 // klass::draw will already be wxgtk_text_draw
421 if ( !gs_gtk_text_draw )
422 {
423 GtkDrawCallback&
424 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
425
426 gs_gtk_text_draw = draw;
427
428 draw = wxgtk_text_draw;
429 }
430 }
431 #endif // GTK+ 1.x
432
433 if (!value.IsEmpty())
434 {
435 #ifdef __WXGTK20__
436 SetValue( value );
437 #else
438
439 #if !GTK_CHECK_VERSION(1, 2, 0)
440 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
441 // gtk_editable_insert_text()
442 gtk_widget_realize(m_text);
443 #endif // GTK 1.0
444
445 gint tmp = 0;
446 #if wxUSE_UNICODE
447 wxWX2MBbuf val = value.mbc_str();
448 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
449 #else
450 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
451 #endif
452
453 if (multi_line)
454 {
455 // Bring editable's cursor uptodate. Bug in GTK.
456 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
457 }
458
459 #endif
460 }
461
462 if (style & wxTE_PASSWORD)
463 {
464 if (!multi_line)
465 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
466 }
467
468 if (style & wxTE_READONLY)
469 {
470 if (!multi_line)
471 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
472 #ifdef __WXGTK20__
473 else
474 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE);
475 #else
476 }
477 else
478 {
479 if (multi_line)
480 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
481 #endif
482 }
483
484 #ifdef __WXGTK20__
485 if (multi_line)
486 {
487 if (style & wxTE_RIGHT)
488 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT );
489 else if (style & wxTE_CENTRE)
490 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER );
491 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
492 }
493 else
494 {
495 #ifdef __WXGTK24__
496 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
497 if (!gtk_check_version(2,4,0))
498 {
499 if (style & wxTE_RIGHT)
500 gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 );
501 else if (style & wxTE_CENTRE)
502 gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 );
503 }
504 #endif
505 }
506 #endif // __WXGTK20__
507
508 // We want to be notified about text changes.
509 #ifdef __WXGTK20__
510 if (multi_line)
511 {
512 g_signal_connect( G_OBJECT(m_buffer), "changed",
513 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
514 }
515 else
516 #endif
517
518 {
519 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
520 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
521 }
522
523 m_cursor = wxCursor( wxCURSOR_IBEAM );
524
525 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
526 SetDefaultStyle( attrDef );
527
528 return TRUE;
529 }
530
531
532 void wxTextCtrl::CalculateScrollbar()
533 {
534 #ifndef __WXGTK20__
535 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
536
537 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
538
539 if (adj->upper - adj->page_size < 0.8)
540 {
541 if (m_vScrollbarVisible)
542 {
543 gtk_widget_hide( m_vScrollbar );
544 m_vScrollbarVisible = FALSE;
545 }
546 }
547 else
548 {
549 if (!m_vScrollbarVisible)
550 {
551 gtk_widget_show( m_vScrollbar );
552 m_vScrollbarVisible = TRUE;
553 }
554 }
555 #endif
556 }
557
558 wxString wxTextCtrl::GetValue() const
559 {
560 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
561
562 wxString tmp;
563 if (m_windowStyle & wxTE_MULTILINE)
564 {
565 #ifdef __WXGTK20__
566 GtkTextIter start;
567 gtk_text_buffer_get_start_iter( m_buffer, &start );
568 GtkTextIter end;
569 gtk_text_buffer_get_end_iter( m_buffer, &end );
570 gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
571
572 #if wxUSE_UNICODE
573 wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) );
574 #else
575 wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) );
576 #endif
577 if ( buffer )
578 tmp = buffer;
579
580 g_free( text );
581 #else
582 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
583 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
584 tmp = text;
585 g_free( text );
586 #endif
587 }
588 else
589 {
590 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
591 }
592
593 return tmp;
594 }
595
596 void wxTextCtrl::SetValue( const wxString &value )
597 {
598 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
599
600 if (m_windowStyle & wxTE_MULTILINE)
601 {
602 #ifdef __WXGTK20__
603
604 #if wxUSE_UNICODE
605 wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) );
606 #else
607 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) );
608 #endif
609 if (gtk_text_buffer_get_char_count(m_buffer) != 0)
610 IgnoreNextTextUpdate();
611
612 gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
613
614 #else
615 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
616 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
617 len = 0;
618 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
619 #endif
620 }
621 else
622 {
623 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
624 }
625
626 // GRG, Jun/2000: Changed this after a lot of discussion in
627 // the lists. wxWidgets 2.2 will have a set of flags to
628 // customize this behaviour.
629 SetInsertionPoint(0);
630
631 m_modified = FALSE;
632 }
633
634 void wxTextCtrl::WriteText( const wxString &text )
635 {
636 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
637
638 if ( text.empty() )
639 return;
640
641 // gtk_text_changed_callback() will set m_modified to true but m_modified
642 // shouldn't be changed by the program writing to the text control itself,
643 // so save the old value and restore when we're done
644 bool oldModified = m_modified;
645
646 if ( m_windowStyle & wxTE_MULTILINE )
647 {
648 #ifdef __WXGTK20__
649
650 #if wxUSE_UNICODE
651 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
652 #else
653 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
654 #endif
655 if ( !buffer )
656 {
657 // what else can we do? at least don't crash...
658 return;
659 }
660
661 // TODO: Call whatever is needed to delete the selection.
662 wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer );
663
664 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) );
665 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
666 if ( adj->value == adj->upper - adj->page_size )
667 {
668 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text),
669 gtk_text_buffer_get_insert( m_buffer ), 0.0, FALSE, 0.0, 1.0 );
670 }
671 #else // GTK 1.x
672 // After cursor movements, gtk_text_get_point() is wrong by one.
673 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
674
675 // always use m_defaultStyle, even if it is empty as otherwise
676 // resetting the style and appending some more text wouldn't work: if
677 // we don't specify the style explicitly, the old style would be used
678 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
679 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len());
680
681 // we called wxGtkTextInsert with correct font, no need to do anything
682 // in UpdateFontIfNeeded() any longer
683 if ( !text.empty() )
684 {
685 SetUpdateFont(FALSE);
686 }
687
688 // Bring editable's cursor back uptodate.
689 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
690 #endif // GTK 1.x/2.0
691 }
692 else // single line
693 {
694 // First remove the selection if there is one
695 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
696
697 // This moves the cursor pos to behind the inserted text.
698 gint len = GET_EDITABLE_POS(m_text);
699
700 #ifdef __WXGTK20__
701
702 #if wxUSE_UNICODE
703 wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) );
704 #else
705 wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) );
706 #endif
707 gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len );
708
709 #else
710 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len );
711 #endif
712
713 // Bring entry's cursor uptodate.
714 gtk_entry_set_position( GTK_ENTRY(m_text), len );
715 }
716
717 m_modified = oldModified;
718 }
719
720 void wxTextCtrl::AppendText( const wxString &text )
721 {
722 SetInsertionPointEnd();
723 WriteText( text );
724 }
725
726 wxString wxTextCtrl::GetLineText( long lineNo ) const
727 {
728 if (m_windowStyle & wxTE_MULTILINE)
729 {
730 #ifndef __WXGTK20__
731 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
732 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
733
734 if (text)
735 {
736 wxString buf(wxT(""));
737 long i;
738 int currentLine = 0;
739 for (i = 0; currentLine != lineNo && text[i]; i++ )
740 if (text[i] == '\n')
741 currentLine++;
742 // Now get the text
743 int j;
744 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
745 buf += text[i];
746
747 g_free( text );
748 return buf;
749 }
750 else
751 {
752 return wxEmptyString;
753 }
754 #else
755 GtkTextIter line;
756 gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
757 GtkTextIter end;
758 gtk_text_buffer_get_end_iter(m_buffer,&end );
759 gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
760 wxString result(wxGTK_CONV_BACK(text));
761 g_free(text);
762 return result.BeforeFirst(wxT('\n'));
763 #endif
764 }
765 else
766 {
767 if (lineNo == 0) return GetValue();
768 return wxEmptyString;
769 }
770 }
771
772 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
773 {
774 /* If you implement this, don't forget to update the documentation!
775 * (file docs/latex/wx/text.tex) */
776 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
777 }
778
779 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
780 {
781 if ( m_windowStyle & wxTE_MULTILINE )
782 {
783 wxString text = GetValue();
784
785 // cast to prevent warning. But pos really should've been unsigned.
786 if( (unsigned long)pos > text.Len() )
787 return FALSE;
788
789 *x=0; // First Col
790 *y=0; // First Line
791
792 const wxChar* stop = text.c_str() + pos;
793 for ( const wxChar *p = text.c_str(); p < stop; p++ )
794 {
795 if (*p == wxT('\n'))
796 {
797 (*y)++;
798 *x=0;
799 }
800 else
801 (*x)++;
802 }
803 }
804 else // single line control
805 {
806 if ( pos <= GTK_ENTRY(m_text)->text_length )
807 {
808 *y = 0;
809 *x = pos;
810 }
811 else
812 {
813 // index out of bounds
814 return FALSE;
815 }
816 }
817
818 return TRUE;
819 }
820
821 long wxTextCtrl::XYToPosition(long x, long y ) const
822 {
823 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
824
825 long pos=0;
826 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
827
828 pos += x;
829 return pos;
830 }
831
832 int wxTextCtrl::GetLineLength(long lineNo) const
833 {
834 wxString str = GetLineText (lineNo);
835 return (int) str.Length();
836 }
837
838 int wxTextCtrl::GetNumberOfLines() const
839 {
840 if (m_windowStyle & wxTE_MULTILINE)
841 {
842 #ifdef __WXGTK20__
843 return gtk_text_buffer_get_line_count( m_buffer );
844 #else
845 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
846 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
847
848 if (text)
849 {
850 int currentLine = 0;
851 for (int i = 0; i < len; i++ )
852 {
853 if (text[i] == '\n')
854 currentLine++;
855 }
856 g_free( text );
857
858 // currentLine is 0 based, add 1 to get number of lines
859 return currentLine + 1;
860 }
861 else
862 {
863 return 0;
864 }
865 #endif
866 }
867 else
868 {
869 return 1;
870 }
871 }
872
873 void wxTextCtrl::SetInsertionPoint( long pos )
874 {
875 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
876
877 if ( IsMultiLine() )
878 {
879 #ifdef __WXGTK20__
880 GtkTextIter iter;
881 gtk_text_buffer_get_iter_at_offset( m_buffer, &iter, pos );
882 gtk_text_buffer_place_cursor( m_buffer, &iter );
883 gtk_text_view_scroll_mark_onscreen
884 (
885 GTK_TEXT_VIEW(m_text),
886 gtk_text_buffer_get_insert( m_buffer )
887 );
888 #else // GTK+ 1.x
889 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
890 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
891
892 /* we fake a set_point by inserting and deleting. as the user
893 isn't supposed to get to know about this non-sense, we
894 disconnect so that no events are sent to the user program. */
895
896 gint tmp = (gint)pos;
897 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
898 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
899
900 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
901 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
902
903 // bring editable's cursor uptodate. Bug in GTK.
904 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
905 #endif // GTK+ 2/1
906 }
907 else
908 {
909 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
910
911 // Bring editable's cursor uptodate. Bug in GTK.
912 SET_EDITABLE_POS(m_text, (guint32)pos);
913 }
914 }
915
916 void wxTextCtrl::SetInsertionPointEnd()
917 {
918 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
919
920 if (m_windowStyle & wxTE_MULTILINE)
921 {
922 #ifdef __WXGTK20__
923 GtkTextIter end;
924 gtk_text_buffer_get_end_iter( m_buffer, &end );
925 gtk_text_buffer_place_cursor( m_buffer, &end );
926 #else
927 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
928 #endif
929 }
930 else
931 {
932 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
933 }
934 }
935
936 void wxTextCtrl::SetEditable( bool editable )
937 {
938 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
939
940 if (m_windowStyle & wxTE_MULTILINE)
941 {
942 #ifdef __WXGTK20__
943 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable );
944 #else
945 gtk_text_set_editable( GTK_TEXT(m_text), editable );
946 #endif
947 }
948 else
949 {
950 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
951 }
952 }
953
954 bool wxTextCtrl::Enable( bool enable )
955 {
956 if (!wxWindowBase::Enable(enable))
957 {
958 // nothing to do
959 return FALSE;
960 }
961
962 if (m_windowStyle & wxTE_MULTILINE)
963 {
964 #ifdef __WXGTK20__
965 SetEditable( enable );
966 #else
967 gtk_text_set_editable( GTK_TEXT(m_text), enable );
968 OnParentEnable(enable);
969 #endif
970 }
971 else
972 {
973 gtk_widget_set_sensitive( m_text, enable );
974 }
975
976 return TRUE;
977 }
978
979 // wxGTK-specific: called recursively by Enable,
980 // to give widgets an oppprtunity to correct their colours after they
981 // have been changed by Enable
982 void wxTextCtrl::OnParentEnable( bool enable )
983 {
984 // If we have a custom background colour, we use this colour in both
985 // disabled and enabled mode, or we end up with a different colour under the
986 // text.
987 wxColour oldColour = GetBackgroundColour();
988 if (oldColour.Ok())
989 {
990 // Need to set twice or it'll optimize the useful stuff out
991 if (oldColour == * wxWHITE)
992 SetBackgroundColour(*wxBLACK);
993 else
994 SetBackgroundColour(*wxWHITE);
995 SetBackgroundColour(oldColour);
996 }
997 }
998
999 void wxTextCtrl::MarkDirty()
1000 {
1001 m_modified = TRUE;
1002 }
1003
1004 void wxTextCtrl::DiscardEdits()
1005 {
1006 m_modified = FALSE;
1007 }
1008
1009 // ----------------------------------------------------------------------------
1010 // max text length support
1011 // ----------------------------------------------------------------------------
1012
1013 void wxTextCtrl::IgnoreNextTextUpdate()
1014 {
1015 m_ignoreNextUpdate = TRUE;
1016 }
1017
1018 bool wxTextCtrl::IgnoreTextUpdate()
1019 {
1020 if ( m_ignoreNextUpdate )
1021 {
1022 m_ignoreNextUpdate = FALSE;
1023
1024 return TRUE;
1025 }
1026
1027 return FALSE;
1028 }
1029
1030 void wxTextCtrl::SetMaxLength(unsigned long len)
1031 {
1032 if ( !HasFlag(wxTE_MULTILINE) )
1033 {
1034 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
1035
1036 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1037 // we had tried to enter more text than allowed by max text length and
1038 // the text wasn't really changed
1039 //
1040 // to detect this and generate TEXT_MAXLEN event instead of
1041 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1042 //
1043 // when max len is set to 0 we disconnect our handler as it means that
1044 // we shouldn't check anything any more
1045 if ( len )
1046 {
1047 gtk_signal_connect( GTK_OBJECT(m_text),
1048 "insert_text",
1049 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1050 (gpointer)this);
1051 }
1052 else // no checking
1053 {
1054 gtk_signal_disconnect_by_func
1055 (
1056 GTK_OBJECT(m_text),
1057 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
1058 (gpointer)this
1059 );
1060 }
1061 }
1062 }
1063
1064 void wxTextCtrl::SetSelection( long from, long to )
1065 {
1066 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1067
1068 if (from == -1 && to == -1)
1069 {
1070 from = 0;
1071 to = GetValue().Length();
1072 }
1073
1074 #ifndef __WXGTK20__
1075 if ( (m_windowStyle & wxTE_MULTILINE) &&
1076 !GTK_TEXT(m_text)->line_start_cache )
1077 {
1078 // tell the programmer that it didn't work
1079 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
1080 return;
1081 }
1082 #endif
1083
1084 if (m_windowStyle & wxTE_MULTILINE)
1085 {
1086 #ifdef __WXGTK20__
1087 GtkTextIter fromi, toi;
1088 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1089 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1090
1091 gtk_text_buffer_place_cursor( m_buffer, &toi );
1092 gtk_text_buffer_move_mark_by_name( m_buffer, "selection_bound", &fromi );
1093 #else
1094 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1095 #endif
1096 }
1097 else
1098 {
1099 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1100 }
1101 }
1102
1103 void wxTextCtrl::ShowPosition( long pos )
1104 {
1105 if (m_windowStyle & wxTE_MULTILINE)
1106 {
1107 #ifdef __WXGTK20__
1108 GtkTextIter iter;
1109 gtk_text_buffer_get_start_iter( m_buffer, &iter );
1110 gtk_text_iter_set_offset( &iter, pos );
1111 GtkTextMark *mark = gtk_text_buffer_create_mark( m_buffer, NULL, &iter, TRUE );
1112 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 );
1113 #else // GTK 1.x
1114 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
1115 float totalLines = (float) GetNumberOfLines();
1116 long posX;
1117 long posY;
1118 PositionToXY(pos, &posX, &posY);
1119 float posLine = (float) posY;
1120 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
1121 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
1122 #endif // GTK 1.x/2.x
1123 }
1124 }
1125
1126 #ifdef __WXGTK20__
1127
1128 wxTextCtrlHitTestResult
1129 wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const
1130 {
1131 if ( !IsMultiLine() )
1132 {
1133 // not supported
1134 return wxTE_HT_UNKNOWN;
1135 }
1136
1137 int x, y;
1138 gtk_text_view_window_to_buffer_coords
1139 (
1140 GTK_TEXT_VIEW(m_text),
1141 GTK_TEXT_WINDOW_TEXT,
1142 pt.x, pt.y,
1143 &x, &y
1144 );
1145
1146 GtkTextIter iter;
1147 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y);
1148 if ( pos )
1149 *pos = gtk_text_iter_get_offset(&iter);
1150
1151 return wxTE_HT_ON_TEXT;
1152 }
1153
1154 #endif // __WXGTK20__
1155
1156 long wxTextCtrl::GetInsertionPoint() const
1157 {
1158 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1159
1160 #ifdef __WXGTK20__
1161 if (m_windowStyle & wxTE_MULTILINE)
1162 {
1163 // There is no direct accessor for the cursor, but
1164 // internally, the cursor is the "mark" called
1165 // "insert" in the text view's btree structure.
1166
1167 GtkTextMark *mark = gtk_text_buffer_get_insert( m_buffer );
1168 GtkTextIter cursor;
1169 gtk_text_buffer_get_iter_at_mark( m_buffer, &cursor, mark );
1170
1171 return gtk_text_iter_get_offset( &cursor );
1172 }
1173 else
1174 #endif
1175 {
1176 return (long) GET_EDITABLE_POS(m_text);
1177 }
1178 }
1179
1180 long wxTextCtrl::GetLastPosition() const
1181 {
1182 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
1183
1184 int pos = 0;
1185
1186 if (m_windowStyle & wxTE_MULTILINE)
1187 {
1188 #ifdef __WXGTK20__
1189 GtkTextIter end;
1190 gtk_text_buffer_get_end_iter( m_buffer, &end );
1191
1192 pos = gtk_text_iter_get_offset( &end );
1193 #else
1194 pos = gtk_text_get_length( GTK_TEXT(m_text) );
1195 #endif
1196 }
1197 else
1198 {
1199 pos = GTK_ENTRY(m_text)->text_length;
1200 }
1201
1202 return (long)pos;
1203 }
1204
1205 void wxTextCtrl::Remove( long from, long to )
1206 {
1207 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1208
1209 #ifdef __WXGTK20__
1210 if (m_windowStyle & wxTE_MULTILINE)
1211 {
1212 GtkTextIter fromi, toi;
1213 gtk_text_buffer_get_iter_at_offset( m_buffer, &fromi, from );
1214 gtk_text_buffer_get_iter_at_offset( m_buffer, &toi, to );
1215
1216 gtk_text_buffer_delete( m_buffer, &fromi, &toi );
1217 }
1218 else // single line
1219 #endif
1220 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
1221 }
1222
1223 void wxTextCtrl::Replace( long from, long to, const wxString &value )
1224 {
1225 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1226
1227 Remove( from, to );
1228
1229 if (!value.IsEmpty())
1230 {
1231 #ifdef __WXGTK20__
1232 SetInsertionPoint( from );
1233 WriteText( value );
1234 #else // GTK 1.x
1235 gint pos = (gint)from;
1236 #if wxUSE_UNICODE
1237 wxWX2MBbuf buf = value.mbc_str();
1238 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
1239 #else
1240 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
1241 #endif // wxUSE_UNICODE
1242 #endif // GTK 1.x/2.x
1243 }
1244 }
1245
1246 void wxTextCtrl::Cut()
1247 {
1248 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1249
1250 #ifdef __WXGTK20__
1251 if (m_windowStyle & wxTE_MULTILINE)
1252 g_signal_emit_by_name(m_text, "cut-clipboard");
1253 else
1254 #endif
1255 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1256 }
1257
1258 void wxTextCtrl::Copy()
1259 {
1260 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1261
1262 #ifdef __WXGTK20__
1263 if (m_windowStyle & wxTE_MULTILINE)
1264 g_signal_emit_by_name(m_text, "copy-clipboard");
1265 else
1266 #endif
1267 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1268 }
1269
1270 void wxTextCtrl::Paste()
1271 {
1272 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1273
1274 #ifdef __WXGTK20__
1275 if (m_windowStyle & wxTE_MULTILINE)
1276 g_signal_emit_by_name(m_text, "paste-clipboard");
1277 else
1278 #endif
1279 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
1280 }
1281
1282 // Undo/redo
1283 void wxTextCtrl::Undo()
1284 {
1285 // TODO
1286 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1287 }
1288
1289 void wxTextCtrl::Redo()
1290 {
1291 // TODO
1292 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1293 }
1294
1295 bool wxTextCtrl::CanUndo() const
1296 {
1297 // TODO
1298 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1299 return FALSE;
1300 }
1301
1302 bool wxTextCtrl::CanRedo() const
1303 {
1304 // TODO
1305 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1306 return FALSE;
1307 }
1308
1309 // If the return values from and to are the same, there is no
1310 // selection.
1311 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
1312 {
1313 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1314
1315 gint from = -1;
1316 gint to = -1;
1317 bool haveSelection = FALSE;
1318
1319 #ifdef __WXGTK20__
1320 if (m_windowStyle & wxTE_MULTILINE)
1321 {
1322 GtkTextIter ifrom, ito;
1323 if ( gtk_text_buffer_get_selection_bounds(m_buffer, &ifrom, &ito) )
1324 {
1325 haveSelection = TRUE;
1326 from = gtk_text_iter_get_offset(&ifrom);
1327 to = gtk_text_iter_get_offset(&ito);
1328 }
1329 }
1330 else // not multi-line
1331 {
1332 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text),
1333 &from, &to) )
1334 {
1335 haveSelection = TRUE;
1336 }
1337 }
1338 #else // not GTK2
1339 if ( (GTK_EDITABLE(m_text)->has_selection) )
1340 {
1341 haveSelection = TRUE;
1342 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
1343 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
1344 }
1345 #endif
1346
1347 if (! haveSelection )
1348 from = to = GetInsertionPoint();
1349
1350 if ( from > to )
1351 {
1352 // exchange them to be compatible with wxMSW
1353 gint tmp = from;
1354 from = to;
1355 to = tmp;
1356 }
1357
1358 if ( fromOut )
1359 *fromOut = from;
1360 if ( toOut )
1361 *toOut = to;
1362 }
1363
1364
1365 bool wxTextCtrl::IsEditable() const
1366 {
1367 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1368
1369 #ifdef __WXGTK20__
1370 if (m_windowStyle & wxTE_MULTILINE)
1371 {
1372 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text));
1373 }
1374 else
1375 {
1376 return gtk_editable_get_editable(GTK_EDITABLE(m_text));
1377 }
1378 #else
1379 return GTK_EDITABLE(m_text)->editable;
1380 #endif
1381 }
1382
1383 bool wxTextCtrl::IsModified() const
1384 {
1385 return m_modified;
1386 }
1387
1388 void wxTextCtrl::Clear()
1389 {
1390 SetValue( wxT("") );
1391 }
1392
1393 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1394 {
1395 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1396
1397 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1398 {
1399 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1400 event.SetEventObject(this);
1401 event.SetString(GetValue());
1402 if (GetEventHandler()->ProcessEvent(event)) return;
1403 }
1404
1405 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1406 {
1407 // This will invoke the dialog default action, such
1408 // as the clicking the default button.
1409
1410 wxWindow *top_frame = m_parent;
1411 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1412 top_frame = top_frame->GetParent();
1413
1414 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1415 {
1416 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1417
1418 if (window->default_widget)
1419 {
1420 gtk_widget_activate (window->default_widget);
1421 return;
1422 }
1423 }
1424 }
1425
1426 key_event.Skip();
1427 }
1428
1429 GtkWidget* wxTextCtrl::GetConnectWidget()
1430 {
1431 return GTK_WIDGET(m_text);
1432 }
1433
1434 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1435 {
1436 if (m_windowStyle & wxTE_MULTILINE)
1437 {
1438 #ifdef __WXGTK20__
1439 return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork
1440 #else
1441 return (window == GTK_TEXT(m_text)->text_area);
1442 #endif
1443 }
1444 else
1445 {
1446 return (window == GTK_ENTRY(m_text)->text_area);
1447 }
1448 }
1449
1450 // the font will change for subsequent text insertiongs
1451 bool wxTextCtrl::SetFont( const wxFont &font )
1452 {
1453 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1454
1455 if ( !wxTextCtrlBase::SetFont(font) )
1456 {
1457 // font didn't change, nothing to do
1458 return FALSE;
1459 }
1460
1461 if ( m_windowStyle & wxTE_MULTILINE )
1462 {
1463 SetUpdateFont(TRUE);
1464
1465 m_defaultStyle.SetFont(font);
1466
1467 ChangeFontGlobally();
1468 }
1469
1470 return TRUE;
1471 }
1472
1473 void wxTextCtrl::ChangeFontGlobally()
1474 {
1475 // this method is very inefficient and hence should be called as rarely as
1476 // possible!
1477 //
1478 // TODO: it can be implemented much more efficiently for GTK2
1479 #ifndef __WXGTK20__
1480 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1481
1482 _T("shouldn't be called for single line controls") );
1483 #else
1484 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE),
1485 _T("shouldn't be called for single line controls") );
1486 #endif
1487
1488 wxString value = GetValue();
1489 if ( !value.IsEmpty() )
1490 {
1491 SetUpdateFont(FALSE);
1492
1493 Clear();
1494 AppendText(value);
1495 }
1496 }
1497
1498 #ifndef __WXGTK20__
1499
1500 void wxTextCtrl::UpdateFontIfNeeded()
1501 {
1502 if ( m_updateFont )
1503 ChangeFontGlobally();
1504 }
1505
1506 #endif // GTK+ 1.x
1507
1508 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1509 {
1510 if ( !wxControl::SetForegroundColour(colour) )
1511 return FALSE;
1512
1513 // update default fg colour too
1514 m_defaultStyle.SetTextColour(colour);
1515
1516 return TRUE;
1517 }
1518
1519 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1520 {
1521 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1522
1523 if ( !wxControl::SetBackgroundColour( colour ) )
1524 return FALSE;
1525
1526 #ifndef __WXGTK20__
1527 if (!m_widget->window)
1528 return FALSE;
1529 #endif
1530
1531 if (!m_backgroundColour.Ok())
1532 return FALSE;
1533
1534 if (m_windowStyle & wxTE_MULTILINE)
1535 {
1536 #ifndef __WXGTK20__
1537 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1538 if (!window)
1539 return FALSE;
1540 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1541 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1542 gdk_window_clear( window );
1543 #endif
1544 }
1545
1546 // change active background color too
1547 m_defaultStyle.SetBackgroundColour( colour );
1548
1549 return TRUE;
1550 }
1551
1552 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1553 {
1554 if ( m_windowStyle & wxTE_MULTILINE )
1555 {
1556 if ( style.IsDefault() )
1557 {
1558 // nothing to do
1559 return TRUE;
1560 }
1561 #ifdef __WXGTK20__
1562 gint l = gtk_text_buffer_get_char_count( m_buffer );
1563
1564 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1565 _T("invalid range in wxTextCtrl::SetStyle") );
1566
1567 GtkTextIter starti, endi;
1568 gtk_text_buffer_get_iter_at_offset( m_buffer, &starti, start );
1569 gtk_text_buffer_get_iter_at_offset( m_buffer, &endi, end );
1570
1571 // use the attributes from style which are set in it and fall back
1572 // first to the default style and then to the text control default
1573 // colours for the others
1574 wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
1575
1576 wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
1577
1578 return TRUE;
1579 #else
1580 // VERY dirty way to do that - removes the required text and re-adds it
1581 // with styling (FIXME)
1582
1583 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1584
1585 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1586 _T("invalid range in wxTextCtrl::SetStyle") );
1587
1588 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1589 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1590 wxString tmp(text,*wxConvCurrent);
1591 g_free( text );
1592
1593 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1594 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1595
1596 #if wxUSE_UNICODE
1597 wxWX2MBbuf buf = tmp.mbc_str();
1598 const char *txt = buf;
1599 size_t txtlen = strlen(buf);
1600 #else
1601 const char *txt = tmp;
1602 size_t txtlen = tmp.length();
1603 #endif
1604
1605 // use the attributes from style which are set in it and fall back
1606 // first to the default style and then to the text control default
1607 // colours for the others
1608 wxGtkTextInsert(m_text,
1609 wxTextAttr::Combine(style, m_defaultStyle, this),
1610 txt,
1611 txtlen);
1612
1613 /* does not seem to help under GTK+ 1.2 !!!
1614 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1615 SetInsertionPoint( old_pos );
1616 #endif
1617 return TRUE;
1618 }
1619 else // singe line
1620 {
1621 // cannot do this for GTK+'s Entry widget
1622 return FALSE;
1623 }
1624 }
1625
1626 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1627 {
1628 gtk_widget_modify_style(m_text, style);
1629 }
1630
1631 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1632 {
1633 Cut();
1634 }
1635
1636 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1637 {
1638 Copy();
1639 }
1640
1641 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1642 {
1643 Paste();
1644 }
1645
1646 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1647 {
1648 Undo();
1649 }
1650
1651 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1652 {
1653 Redo();
1654 }
1655
1656 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1657 {
1658 event.Enable( CanCut() );
1659 }
1660
1661 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1662 {
1663 event.Enable( CanCopy() );
1664 }
1665
1666 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1667 {
1668 event.Enable( CanPaste() );
1669 }
1670
1671 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1672 {
1673 event.Enable( CanUndo() );
1674 }
1675
1676 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1677 {
1678 event.Enable( CanRedo() );
1679 }
1680
1681 void wxTextCtrl::OnInternalIdle()
1682 {
1683 wxCursor cursor = m_cursor;
1684 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1685
1686 if (cursor.Ok())
1687 {
1688 #ifndef __WXGTK20__
1689 GdkWindow *window = (GdkWindow*) NULL;
1690 if (HasFlag(wxTE_MULTILINE))
1691 window = GTK_TEXT(m_text)->text_area;
1692 else
1693 window = GTK_ENTRY(m_text)->text_area;
1694
1695 if (window)
1696 gdk_window_set_cursor( window, cursor.GetCursor() );
1697
1698 if (!g_globalCursor.Ok())
1699 cursor = *wxSTANDARD_CURSOR;
1700
1701 window = m_widget->window;
1702 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1703 gdk_window_set_cursor( window, cursor.GetCursor() );
1704 #endif
1705 }
1706
1707 if (g_delayedFocus == this)
1708 {
1709 if (GTK_WIDGET_REALIZED(m_widget))
1710 {
1711 gtk_widget_grab_focus( m_widget );
1712 g_delayedFocus = NULL;
1713 }
1714 }
1715
1716 if (wxUpdateUIEvent::CanUpdate(this))
1717 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1718 }
1719
1720 wxSize wxTextCtrl::DoGetBestSize() const
1721 {
1722 // FIXME should be different for multi-line controls...
1723 wxSize ret( wxControl::DoGetBestSize() );
1724 wxSize best(80, ret.y);
1725 CacheBestSize(best);
1726 return best;
1727 }
1728
1729 // ----------------------------------------------------------------------------
1730 // freeze/thaw
1731 // ----------------------------------------------------------------------------
1732
1733 void wxTextCtrl::Freeze()
1734 {
1735 if ( HasFlag(wxTE_MULTILINE) )
1736 {
1737 #ifdef __WXGTK20__
1738 if ( !m_frozenness++ )
1739 {
1740 // freeze textview updates and remove buffer
1741 g_signal_connect( G_OBJECT(m_text), "expose_event",
1742 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
1743 g_signal_connect( G_OBJECT(m_widget), "expose_event",
1744 GTK_SIGNAL_FUNC(gtk_text_exposed_callback), (gpointer)this);
1745 gtk_widget_set_sensitive(m_widget, false);
1746 g_object_ref(m_buffer);
1747 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
1748 }
1749 #else
1750 gtk_text_freeze(GTK_TEXT(m_text));
1751 #endif
1752 }
1753 }
1754
1755 void wxTextCtrl::Thaw()
1756 {
1757 if ( HasFlag(wxTE_MULTILINE) )
1758 {
1759 #ifdef __WXGTK20__
1760 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
1761
1762 if ( !--m_frozenness )
1763 {
1764 // Reattach buffer and thaw textview updates
1765 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), m_buffer);
1766 g_object_unref(m_buffer);
1767 gtk_widget_set_sensitive(m_widget, true);
1768 g_signal_handlers_disconnect_by_func(m_widget, (gpointer)gtk_text_exposed_callback, this);
1769 g_signal_handlers_disconnect_by_func(m_text, (gpointer)gtk_text_exposed_callback, this);
1770 }
1771 #else
1772 GTK_TEXT(m_text)->vadj->value = 0.0;
1773
1774 gtk_text_thaw(GTK_TEXT(m_text));
1775 #endif
1776 }
1777 }
1778
1779 // ----------------------------------------------------------------------------
1780 // scrolling
1781 // ----------------------------------------------------------------------------
1782
1783 GtkAdjustment *wxTextCtrl::GetVAdj() const
1784 {
1785 if ( !IsMultiLine() )
1786 return NULL;
1787
1788 #ifdef __WXGTK20__
1789 return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget));
1790 #else
1791 return GTK_TEXT(m_text)->vadj;
1792 #endif
1793 }
1794
1795 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1796 {
1797 float value = adj->value + diff;
1798
1799 if ( value < 0 )
1800 value = 0;
1801
1802 float upper = adj->upper - adj->page_size;
1803 if ( value > upper )
1804 value = upper;
1805
1806 // did we noticeably change the scroll position?
1807 if ( fabs(adj->value - value) < 0.2 )
1808 {
1809 // well, this is what Robert does in wxScrollBar, so it must be good...
1810 return FALSE;
1811 }
1812
1813 adj->value = value;
1814
1815 #ifdef __WXGTK20__
1816 gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj));
1817 #else
1818 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1819 #endif
1820
1821 return TRUE;
1822 }
1823
1824 bool wxTextCtrl::ScrollLines(int lines)
1825 {
1826 GtkAdjustment *adj = GetVAdj();
1827 if ( !adj )
1828 return FALSE;
1829
1830 #ifdef __WXGTK20__
1831 int diff = (int)ceil(lines*adj->step_increment);
1832 #else
1833 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1834 int diff = 10*lines;
1835 #endif
1836
1837 return DoScroll(adj, diff);
1838 }
1839
1840 bool wxTextCtrl::ScrollPages(int pages)
1841 {
1842 GtkAdjustment *adj = GetVAdj();
1843 if ( !adj )
1844 return FALSE;
1845
1846 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1847 }
1848
1849
1850 // static
1851 wxVisualAttributes
1852 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1853 {
1854 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
1855 }